Skip to contentSkip to conversation

block-copy: small fix and refactor

PATCH In review Vladimir Sementsov-Ogievskiy 5 participants 12 messages May 28, 2021 – Jun 7, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

cover block-copy: small fix and refactor

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
block-copy: fix block_copy_task_entry() progress update
block-copy: refactor copy_range handling

 block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 26 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/2 block-copy: fix block_copy_task_entry() progress update

Don't report successful progress on failure, when call_state->ret is set.

---
 block/block-copy.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
block/block-copy.c +5 -3
index c2e5090412..f9e871b64f 100644
@@ -439,9 +439,11 @@ static coroutine_fn int block_copy_task_entry(AioTask *task)
 
     ret = block_copy_do_copy(t->s, t->offset, t->bytes, t->zeroes,
                              &error_is_read);
-    if (ret < 0 && !t->call_state->ret) {
-        t->call_state->ret = ret;
-        t->call_state->error_is_read = error_is_read;
+    if (ret < 0) {
+        if (!t->call_state->ret) {
+            t->call_state->ret = ret;
+            t->call_state->error_is_read = error_is_read;
+        }
     } else {
         progress_work_done(t->s->progress, t->bytes);
     }
Signature · 1 line
2.29.2
#2 lore raw

PATCH 2/2 block-copy: refactor copy_range handling

In reply to Vladimir Sementsov-Ogievskiy

Currently we update s->use_copy_range and s->copy_size in block_copy_do_copy().

It's not very good:

1. block_copy_do_copy() is intended to be a simple function, that wraps bdrv_co_<io> functions for need of block copy. That's why we don't pass BlockCopyTask into it. So, block_copy_do_copy() is bad place for manipulation with generic state of block-copy process

2. We are going to make block-copy thread-safe. So, it's good to move manipulation with state of block-copy to the places where we'll need critical sections anyway, to not introduce extra synchronization primitives in block_copy_do_copy().

---
 block/block-copy.c | 71 +++++++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 23 deletions(-)
block/block-copy.c +48 -23
index f9e871b64f..c96fe31054 100644
@@ -65,6 +65,7 @@ typedef struct BlockCopyTask {
     int64_t offset;
     int64_t bytes;
     bool zeroes;
+    bool copy_range;
     QLIST_ENTRY(BlockCopyTask) list;
     CoQueue wait_queue; /* coroutines blocked on this task */
 } BlockCopyTask;
@@ -183,6 +184,7 @@ static BlockCopyTask *block_copy_task_create(BlockCopyState *s,
         .call_state = call_state,
         .offset = offset,
         .bytes = bytes,
+        .copy_range = s->use_copy_range,
     };
     qemu_co_queue_init(&task->wait_queue);
     QLIST_INSERT_HEAD(&s->tasks, task, list);
@@ -342,11 +344,17 @@ static coroutine_fn int block_copy_task_run(AioTaskPool *pool,
  *
  * No sync here: nor bitmap neighter intersecting requests handling, only copy.
  *
+ * @copy_range is in-out argument: if *copy_range is false, copy_range is not
+ * done. If *copy_range is true, copy_range attempt is done. If copy_range
+ * attempt failed, the function fallback to usual read+write and *copy_range is
+ * set to false. *copy_range and zeroes must not be true simultaneously.
+ *
  * Returns 0 on success.
  */
 static int coroutine_fn block_copy_do_copy(BlockCopyState *s,
                                            int64_t offset, int64_t bytes,
-                                           bool zeroes, bool *error_is_read)
+                                           bool zeroes, bool *copy_range,
+                                           bool *error_is_read)
 {
     int ret;
     int64_t nbytes = MIN(offset + bytes, s->len) - offset;
@@ -359,6 +367,7 @@ static int coroutine_fn block_copy_do_copy(BlockCopyState *s,
     assert(offset + bytes <= s->len ||
            offset + bytes == QEMU_ALIGN_UP(s->len, s->cluster_size));
     assert(nbytes < INT_MAX);
+    assert(!(*copy_range && zeroes));
 
     if (zeroes) {
         ret = bdrv_co_pwrite_zeroes(s->target, offset, nbytes, s->write_flags &
@@ -370,32 +379,15 @@ static int coroutine_fn block_copy_do_copy(BlockCopyState *s,
         return ret;
     }
 
-    if (s->use_copy_range) {
+    if (*copy_range) {
         ret = bdrv_co_copy_range(s->source, offset, s->target, offset, nbytes,
                                  0, s->write_flags);
         if (ret < 0) {
             trace_block_copy_copy_range_fail(s, offset, ret);
-            s->use_copy_range = false;
-            s->copy_size = MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER);
+            *copy_range = false;
             /* Fallback to read+write with allocated buffer */
         } else {
-            if (s->use_copy_range) {
-                /*
-                 * Successful copy-range. Now increase copy_size.  copy_range
-                 * does not respect max_transfer (it's a TODO), so we factor
-                 * that in here.
-                 *
-                 * Note: we double-check s->use_copy_range for the case when
-                 * parallel block-copy request unsets it during previous
-                 * bdrv_co_copy_range call.
-                 */
-                s->copy_size =
-                        MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE),
-                            QEMU_ALIGN_DOWN(block_copy_max_transfer(s->source,
-                                                                    s->target),
-                                            s->cluster_size));
-            }
-            goto out;
+            return 0;
         }
     }
 
@@ -431,14 +423,44 @@ out:
     return ret;
 }
 
+static void block_copy_handle_copy_range_result(BlockCopyState *s,
+                                                bool is_success)
+{
+    if (!s->use_copy_range) {
+        /* already disabled */
+        return;
+    }
+
+    if (is_success) {
+        /*
+         * Successful copy-range. Now increase copy_size.  copy_range
+         * does not respect max_transfer (it's a TODO), so we factor
+         * that in here.
+         */
+        s->copy_size =
+                MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE),
+                    QEMU_ALIGN_DOWN(block_copy_max_transfer(s->source,
+                                                            s->target),
+                                    s->cluster_size));
+    } else {
+        /* Copy-range failed, disable it. */
+        s->use_copy_range = false;
+        s->copy_size = MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER);
+    }
+}
+
 static coroutine_fn int block_copy_task_entry(AioTask *task)
 {
     BlockCopyTask *t = container_of(task, BlockCopyTask, task);
     bool error_is_read = false;
+    bool copy_range = t->copy_range;
     int ret;
 
     ret = block_copy_do_copy(t->s, t->offset, t->bytes, t->zeroes,
-                             &error_is_read);
+                             &copy_range, &error_is_read);
+    if (t->copy_range) {
+        block_copy_handle_copy_range_result(t->s, copy_range);
+    }
     if (ret < 0) {
         if (!t->call_state->ret) {
             t->call_state->ret = ret;
@@ -619,7 +641,10 @@ block_copy_dirty_clusters(BlockCopyCallState *call_state)
             g_free(task);
             continue;
         }
-        task->zeroes = ret & BDRV_BLOCK_ZERO;
+        if (ret & BDRV_BLOCK_ZERO) {
+            task->zeroes = true;
+            task->copy_range = false;
+        }
 
         if (s->speed) {
             if (!call_state->ignore_ratelimit) {
Signature · 1 line
2.29.2
#3 lore raw

Re: [PATCH 2/2] block-copy: refactor copy_range handling

block/block-copy.c static coroutine_fn int block_copy_task_run(AioTaskPool *pool, · line 347

Quoted 13 lines · Vladimir Sementsov-Ogievskiy
block/block-copy.c +2 -0
@@ -183,6 +184,7 @@ static BlockCopyTask *block_copy_task_create(BlockCopyState *s,
         .call_state = call_state,
         .offset = offset,
         .bytes = bytes,
+        .copy_range = s->use_copy_range,
     };
     qemu_co_queue_init(&task->wait_queue);
     QLIST_INSERT_HEAD(&s->tasks, task, list);
@@ -342,11 +344,17 @@ static coroutine_fn int block_copy_task_run(AioTaskPool *pool,
  *
  * No sync here: nor bitmap neighter intersecting requests handling, only copy.
  *
+ * @copy_range is in-out argument: if *copy_range is false, copy_range is not

s/is in-out argument/is an in-out argument/

block/block-copy.c static coroutine_fn int block_copy_task_run(AioTaskPool *pool, · line 348

Quoted 1 line
block/block-copy.c +1 -0
+ * done. If *copy_range is true, copy_range attempt is done. If copy_range

s/copy_range attempt is done/copy_range is attempted/

block/block-copy.c static coroutine_fn int block_copy_task_run(AioTaskPool *pool, · line 349

Quoted 1 line
block/block-copy.c +1 -0
+ * attempt failed, the function fallback to usual read+write and *copy_range is

If the copy_range attempt fails, the function falls back to the usual read+write and ...

#4 lore raw

Re: [PATCH 2/2] block-copy: refactor copy_range handling

Quoted 17 lines · Stefan Hajnoczi

block/block-copy.c static coroutine_fn int block_copy_task_run(AioTaskPool *pool, · line 347

Quoted 13 lines · Vladimir Sementsov-Ogievskiy
block/block-copy.c +2 -0
@@ -183,6 +184,7 @@ static BlockCopyTask *block_copy_task_create(BlockCopyState *s,
          .call_state = call_state,
          .offset = offset,
          .bytes = bytes,
+        .copy_range = s->use_copy_range,
      };
      qemu_co_queue_init(&task->wait_queue);
      QLIST_INSERT_HEAD(&s->tasks, task, list);
@@ -342,11 +344,17 @@ static coroutine_fn int block_copy_task_run(AioTaskPool *pool,
   *
   * No sync here: nor bitmap neighter intersecting requests handling, only copy.
   *
+ * @copy_range is in-out argument: if *copy_range is false, copy_range is not

s/is in-out argument/is an in-out argument/

Quoted 9 lines

block/block-copy.c static coroutine_fn int block_copy_task_run(AioTaskPool *pool, · line 348

Quoted 1 line
block/block-copy.c +1 -0
+ * done. If *copy_range is true, copy_range attempt is done. If copy_range

s/copy_range attempt is done/copy_range is attempted/

block/block-copy.c static coroutine_fn int block_copy_task_run(AioTaskPool *pool, · line 349

Quoted 1 line
block/block-copy.c +1 -0
+ * attempt failed, the function fallback to usual read+write and *copy_range is

If the copy_range attempt fails, the function falls back to the usual read+write and ...

That's better, thanks!

Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on the v1 series
#5 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Quoted 6 lines

Vladimir Sementsov-Ogievskiy (2):
block-copy: fix block_copy_task_entry() progress update
block-copy: refactor copy_range handling

 block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 26 deletions(-)

I posted suggestions for the doc comment on Patch 2, otherwise:

#6 lore raw

Am 02.06.2021 um 11:13 hat Stefan Hajnoczi geschrieben:

Quoted 11 lines · Vladimir Sementsov-Ogievskiy
Quoted 10 lines

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
block-copy: fix block_copy_task_entry() progress update
block-copy: refactor copy_range handling

Quoted 6 lines
Quoted 2 lines
 block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 26 deletions(-)

I posted suggestions for the doc comment on Patch 2, otherwise:

Thanks, fixed up the comment accordingly and applied to the block branch.

Kevin

#7 lore raw
Quoted 15 lines · Kevin Wolf

Am 02.06.2021 um 11:13 hat Stefan Hajnoczi geschrieben:

Quoted 14 lines · Vladimir Sementsov-Ogievskiy
Quoted 12 lines

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
block-copy: fix block_copy_task_entry() progress update
block-copy: refactor copy_range handling

  block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------
  1 file changed, 53 insertions(+), 26 deletions(-)
Quoted 6 lines
Quoted 3 lines

I posted suggestions for the doc comment on Patch 2, otherwise:

Thanks, fixed up the comment accordingly and applied to the block branch.

I'm a bit confused. Vladimir said in his review of Emanuele's patches that he was okay with patch 7 and that he would rebase this refactoring on top of it.

Vladimir's main complaint for the s->method state machine was the extra lines of code. Here we have just as many new lines of code and new parameters that are passed by reference. Kevin, can you please look at Emanuele's patches and possibly unqueue the second patch here? It seems to me that it should have been tagged as RFC.

Paolo

[1] https://patchew.org/QEMU/[email protected]/[email protected]/

#8 lore raw

Am 03.06.2021 um 09:38 hat Paolo Bonzini geschrieben:

Quoted 27 lines · Kevin Wolf
Quoted 15 lines

Am 02.06.2021 um 11:13 hat Stefan Hajnoczi geschrieben:

Quoted 14 lines · Vladimir Sementsov-Ogievskiy
Quoted 12 lines

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
block-copy: fix block_copy_task_entry() progress update
block-copy: refactor copy_range handling

  block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------
  1 file changed, 53 insertions(+), 26 deletions(-)
Quoted 6 lines
Quoted 3 lines

I posted suggestions for the doc comment on Patch 2, otherwise:

Thanks, fixed up the comment accordingly and applied to the block branch.

I'm a bit confused. Vladimir said in his review of Emanuele's patches that he was okay with patch 7 and that he would rebase this refactoring on top of it.

Quoted 5 lines

Vladimir's main complaint for the s->method state machine was the extra lines of code. Here we have just as many new lines of code and new parameters that are passed by reference. Kevin, can you please look at Emanuele's patches and possibly unqueue the second patch here? It seems to me that it should have been tagged as RFC.

Sorry, I was not aware that Vladimir intended to rebase this one. This has already landed in master, so if rebasing the other patch is a real problem, we'd have to revert this one first.

Kevin

#9 lore raw
Quoted 34 lines · Kevin Wolf

Am 03.06.2021 um 09:38 hat Paolo Bonzini geschrieben:

Quoted 27 lines · Kevin Wolf
Quoted 15 lines

Am 02.06.2021 um 11:13 hat Stefan Hajnoczi geschrieben:

Quoted 14 lines · Vladimir Sementsov-Ogievskiy
Quoted 12 lines

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
block-copy: fix block_copy_task_entry() progress update
block-copy: refactor copy_range handling

   block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------
   1 file changed, 53 insertions(+), 26 deletions(-)
Quoted 6 lines
Quoted 3 lines

I posted suggestions for the doc comment on Patch 2, otherwise:

Thanks, fixed up the comment accordingly and applied to the block branch.

I'm a bit confused. Vladimir said in his review of Emanuele's patches that he was okay with patch 7 and that he would rebase this refactoring on top of it.

Quoted 5 lines

Vladimir's main complaint for the s->method state machine was the extra lines of code. Here we have just as many new lines of code and new parameters that are passed by reference. Kevin, can you please look at Emanuele's patches and possibly unqueue the second patch here? It seems to me that it should have been tagged as RFC.

Quoted 4 lines

Sorry, I was not aware that Vladimir intended to rebase this one. This has already landed in master, so if rebasing the other patch is a real problem, we'd have to revert this one first.

It shouldn't be a problem, I have already rebased on top of it. I will re-spin a new series with this and other minor (and hopefully final) fixes soon.

Emanuele

#10 lore raw
Quoted 37 lines · Emanuele Giuseppe Esposito
Quoted 34 lines · Kevin Wolf

Am 03.06.2021 um 09:38 hat Paolo Bonzini geschrieben:

Quoted 32 lines · Kevin Wolf
Quoted 15 lines

Am 02.06.2021 um 11:13 hat Stefan Hajnoczi geschrieben:

Quoted 14 lines · Vladimir Sementsov-Ogievskiy
Quoted 12 lines

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
    block-copy: fix block_copy_task_entry() progress update
    block-copy: refactor copy_range handling

   block/block-copy.c | 79 +++++++++++++++++++++++++++++++---------------

   1 file changed, 53 insertions(+), 26 deletions(-)
Quoted 6 lines
Quoted 3 lines

I posted suggestions for the doc comment on Patch 2, otherwise:

Thanks, fixed up the comment accordingly and applied to the block branch.

I'm a bit confused.  Vladimir said in his review of Emanuele's patches that he was okay with patch 7 and that he would rebase this refactoring on top of it.

Vladimir's main complaint for the s->method state machine was the extra lines of code.  Here we have just as many new lines of code and new parameters that are passed by reference.  Kevin, can you please look at Emanuele's patches and possibly unqueue the second patch here? It seems to me that it should have been tagged as RFC.

Quoted 6 lines
Quoted 4 lines

Sorry, I was not aware that Vladimir intended to rebase this one. This has already landed in master, so if rebasing the other patch is a real problem, we'd have to revert this one first.

It shouldn't be a problem, I have already rebased on top of it. I will re-spin a new series with this and other minor (and hopefully final) fixes soon.

Thanks, and sorry for the mess!

Hmm, actually, I said

Quoted 1 line

OK, I'm OK with patch as is. Finally I can refactor it later on top if needed.. I'll try now do some refactoring, you'll probably want to base on it, or vise-versa, I'll rebase it later on top of these patches.

So, I considered both variants. Then I sent patches, everybody in CC, everybody were silent.

Honestly, I'm a bit confused too. I find my complains valid (independently of me being "I'm OK and can refactor later") and you agreed with them in general. I'm an author and maintainer of the component. I do refactoring that makes it simple to follow my suggestion. So for me it's a bit like doing your work for you. And you ask to roll-back it.

Still, misunderstanding and the mess with two parallel conflicting series is my fault, sorry for this. At least I should have answered to your series when Stefan gave an r-b to my series.

Signature · 2 lines
Best regards,
Vladimir
#11 lore raw
Quoted 48 lines · Vladimir Sementsov-Ogievskiy
Quoted 38 lines · Emanuele Giuseppe Esposito
Quoted 35 lines · Kevin Wolf

Am 03.06.2021 um 09:38 hat Paolo Bonzini geschrieben:

Quoted 33 lines · Kevin Wolf
Quoted 16 lines

Am 02.06.2021 um 11:13 hat Stefan Hajnoczi geschrieben:

Quoted 15 lines · Vladimir Sementsov-Ogievskiy
Quoted 13 lines

Hi all!

This is my suggestion how to refactor block-copy to avoid extra atomic operations in
"[PATCH v2 0/7] block-copy: protect block-copy internal structures"

Vladimir Sementsov-Ogievskiy (2):
    block-copy: fix block_copy_task_entry() progress update
    block-copy: refactor copy_range handling

   block/block-copy.c | 79
+++++++++++++++++++++++++++++++---------------

   1 file changed, 53 insertions(+), 26 deletions(-)
Quoted 6 lines
Quoted 3 lines

I posted suggestions for the doc comment on Patch 2, otherwise:

Thanks, fixed up the comment accordingly and applied to the block branch.

I'm a bit confused.  Vladimir said in his review of Emanuele's patches that he was okay with patch 7 and that he would rebase this refactoring on top of it.

Vladimir's main complaint for the s->method state machine was the extra lines of code.  Here we have just as many new lines of code and new parameters that are passed by reference.  Kevin, can you please look at Emanuele's patches and possibly unqueue the second patch here? It seems to me that it should have been tagged as RFC.

Quoted 6 lines
Quoted 4 lines

Sorry, I was not aware that Vladimir intended to rebase this one. This has already landed in master, so if rebasing the other patch is a real problem, we'd have to revert this one first.

It shouldn't be a problem, I have already rebased on top of it. I will re-spin a new series with this and other minor (and hopefully final) fixes soon.

Thanks, and sorry for the mess!

Quoted 8 lines

Hmm, actually, I said

Quoted 1 line

OK, I'm OK with patch as is. Finally I can refactor it later on top if needed.. I'll try now do some refactoring, you'll probably want to base on it, or vise-versa, I'll rebase it later on top of these patches.

So, I considered both variants. Then I sent patches, everybody in CC, everybody were silent.

Honestly, I'm a bit confused too. I find my complains valid (independently of me being "I'm OK and can refactor later") and you agreed with them in general. I'm an author and maintainer of the component. I do refactoring that makes it simple to follow my suggestion. So for me it's a bit like doing your work for you. And you ask to roll-back it.

I think it's useless to discuss about these things now. I rebased, all is clear and I am positive that in the next version we will have something that makes everyone happy :) and if not, feel free to comment it!

Emanuele

Quoted 3 lines

Still, misunderstanding and the mess with two parallel conflicting series is my fault, sorry for this. At least I should have answered to your series when Stefan gave an r-b to my series.