diff mbox series

[v2,04/11] bdrv_child_try_change_aio_context: add transaction parameter

Message ID 20220725122120.309236-5-eesposit@redhat.com
State New
Headers show
Series Refactor bdrv_try_set_aio_context using transactions | expand

Commit Message

Emanuele Giuseppe Esposito July 25, 2022, 12:21 p.m. UTC
This enables the caller to use the same transaction to also
keep track of aiocontext changes.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 block.c                            | 31 ++++++++++++++++++++++++------
 include/block/block-global-state.h |  5 +++++
 2 files changed, 30 insertions(+), 6 deletions(-)

Comments

Kevin Wolf Oct. 7, 2022, 4:10 p.m. UTC | #1
Am 25.07.2022 um 14:21 hat Emanuele Giuseppe Esposito geschrieben:
> This enables the caller to use the same transaction to also
> keep track of aiocontext changes.
> 
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>

What you're really doing here is factoring out the recursive phase.
However, the factored out function is never used from anywhere else,
so I don't understand the purpose of this patch. It feels like an
unnecessary complication of the code.

The commit message is unclear to me, too: Who is the caller of
bdrv_child_try_change_aio_context() that it mentions, and why does it
make a difference to it how the code is organised internally?

Is this some artifact of changes you made and we don't need it any more
now?

>  block.c                            | 31 ++++++++++++++++++++++++------
>  include/block/block-global-state.h |  5 +++++
>  2 files changed, 30 insertions(+), 6 deletions(-)
> 
> diff --git a/block.c b/block.c
> index c02a628336..221bf90268 100644
> --- a/block.c
> +++ b/block.c
> @@ -7643,17 +7643,16 @@ int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
>   * For the same reason, it temporarily holds also the new AioContext, since
>   * bdrv_drained_end calls BDRV_POLL_WHILE that assumes the lock is taken too.
>   */
> -int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
> -                                      BdrvChild *ignore_child, Error **errp)
> +int bdrv_child_try_change_aio_context_tran(BlockDriverState *bs,
> +                                           AioContext *ctx,
> +                                           BdrvChild *ignore_child,
> +                                           Transaction *tran,
> +                                           Error **errp)

As mentioned above, this is never used anywhere else than from
bdrv_child_try_change_aio_context(), so if we want to keep the patch, it
should be static at least.

Maybe find a better name, too, because all of the transaction related
operations are in the caller.

The function comment is not accurate any more either because it
described the whole of bdrv_child_try_change_aio_context(), while this
function only contains the recursive part.

Kevin
Emanuele Giuseppe Esposito Oct. 24, 2022, 2:52 p.m. UTC | #2
Am 07/10/2022 um 18:10 schrieb Kevin Wolf:
> Am 25.07.2022 um 14:21 hat Emanuele Giuseppe Esposito geschrieben:
>> This enables the caller to use the same transaction to also
>> keep track of aiocontext changes.
>>
>> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
> 
> What you're really doing here is factoring out the recursive phase.
> However, the factored out function is never used from anywhere else,
> so I don't understand the purpose of this patch. It feels like an
> unnecessary complication of the code.
> 
> The commit message is unclear to me, too: Who is the caller of
> bdrv_child_try_change_aio_context() that it mentions, and why does it
> make a difference to it how the code is organised internally?

So this was an initial (mis)understanding from what Vladimir suggested,
where everything should be part of a single transaction.
If you want, I can drop this.

Thank you,
Emanuele

> 
> Is this some artifact of changes you made and we don't need it any more
> now?
> 
>>  block.c                            | 31 ++++++++++++++++++++++++------
>>  include/block/block-global-state.h |  5 +++++
>>  2 files changed, 30 insertions(+), 6 deletions(-)
>>
>> diff --git a/block.c b/block.c
>> index c02a628336..221bf90268 100644
>> --- a/block.c
>> +++ b/block.c
>> @@ -7643,17 +7643,16 @@ int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
>>   * For the same reason, it temporarily holds also the new AioContext, since
>>   * bdrv_drained_end calls BDRV_POLL_WHILE that assumes the lock is taken too.
>>   */
>> -int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
>> -                                      BdrvChild *ignore_child, Error **errp)
>> +int bdrv_child_try_change_aio_context_tran(BlockDriverState *bs,
>> +                                           AioContext *ctx,
>> +                                           BdrvChild *ignore_child,
>> +                                           Transaction *tran,
>> +                                           Error **errp)
> 
> As mentioned above, this is never used anywhere else than from
> bdrv_child_try_change_aio_context(), so if we want to keep the patch, it
> should be static at least.
> 
> Maybe find a better name, too, because all of the transaction related
> operations are in the caller.
> 
> The function comment is not accurate any more either because it
> described the whole of bdrv_child_try_change_aio_context(), while this
> function only contains the recursive part.
> 
> Kevin
>
diff mbox series

Patch

diff --git a/block.c b/block.c
index c02a628336..221bf90268 100644
--- a/block.c
+++ b/block.c
@@ -7643,17 +7643,16 @@  int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
  * For the same reason, it temporarily holds also the new AioContext, since
  * bdrv_drained_end calls BDRV_POLL_WHILE that assumes the lock is taken too.
  */
-int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
-                                      BdrvChild *ignore_child, Error **errp)
+int bdrv_child_try_change_aio_context_tran(BlockDriverState *bs,
+                                           AioContext *ctx,
+                                           BdrvChild *ignore_child,
+                                           Transaction *tran,
+                                           Error **errp)
 {
-    Transaction *tran;
     GHashTable *visited;
     int ret;
-    AioContext *old_context = bdrv_get_aio_context(bs);
     GLOBAL_STATE_CODE();
 
-    /* Recursion phase: go through all nodes of the graph */
-    tran = tran_new();
     visited = g_hash_table_new(NULL, NULL);
     if (ignore_child) {
         g_hash_table_add(visited, ignore_child);
@@ -7661,6 +7660,26 @@  int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
     ret = bdrv_change_aio_context(bs, ctx, visited, tran, errp);
     g_hash_table_destroy(visited);
 
+    return ret;
+}
+
+/*
+ * See bdrv_child_try_change_aio_context_tran for invariants on
+ * AioContext locks.
+ */
+int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
+                                      BdrvChild *ignore_child, Error **errp)
+{
+    Transaction *tran;
+    int ret;
+    AioContext *old_context = bdrv_get_aio_context(bs);
+    GLOBAL_STATE_CODE();
+
+    /* Recursion phase: go through all nodes of the graph */
+    tran = tran_new();
+    ret = bdrv_child_try_change_aio_context_tran(bs, ctx, ignore_child, tran,
+                                                 errp);
+
     /* Linear phase: go through all callbacks collected in the transaction */
 
     if (!ret) {
diff --git a/include/block/block-global-state.h b/include/block/block-global-state.h
index ceecf0aa8e..1bd445b507 100644
--- a/include/block/block-global-state.h
+++ b/include/block/block-global-state.h
@@ -234,6 +234,11 @@  bool bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx,
                                    Error **errp);
 int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
                                       BdrvChild *ignore_child, Error **errp);
+int bdrv_child_try_change_aio_context_tran(BlockDriverState *bs,
+                                           AioContext *ctx,
+                                           BdrvChild *ignore_child,
+                                           Transaction *tran,
+                                           Error **errp);
 
 int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz);
 int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo);