Skip to contentSkip to conversation

potentially broken drive-mirror/drive-backup in bdrv_co_discard

PATCH Superseded Denis V. Lunev 4 participants 10 messages Jun 16, 2016 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover potentially broken drive-mirror/drive-backup in bdrv_co_discard

Actually I have found this problem running iotest 132 for active async mirror I have sent yesturday. Anyway, the problem is actual for current backup/mirror implementation.

bdrv_co_discard must mark areas dirty after writing zeroes, it must call before_write_notifier chain to push underlying data to backup and it also must properly fill tracked request information.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
#1 lore raw

PATCH 1/3 block: fixed BdrvTrackedRequest filling in bdrv_co_discard

The request is area is specified in bytes, not in sectors.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
 block/io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/io.c +2 -2
index afb99c4..b51f681 100644
@@ -2263,8 +2263,8 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
         return 0;
     }
 
-    tracked_request_begin(&req, bs, NULL, sector_num, nb_sectors,
-                          BDRV_TRACKED_DISCARD);
+    tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
+                          nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
     bdrv_set_dirty(bs, sector_num, nb_sectors);
 
     max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
Signature · 1 line
2.5.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] block: fixed BdrvTrackedRequest filling in bdrv_co_discard

Quoted 1 line · Denis V. Lunev

The request is area is specified in bytes, not in sectors.

s/request is/request/

Quoted 29 lines
CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
 block/io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/io.c +2 -2
index afb99c4..b51f681 100644
@@ -2263,8 +2263,8 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
         return 0;
     }
 
-    tracked_request_begin(&req, bs, NULL, sector_num, nb_sectors,
-                          BDRV_TRACKED_DISCARD);
+    tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
+                          nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
     bdrv_set_dirty(bs, sector_num, nb_sectors);
 
     max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
Signature · 1 line
2.5.0
#3 lore raw

PATCH 2/3 block: fix race in bdrv_co_discard with drive-mirror

In reply to Denis V. Lunev

Actually we must set dirty bitmap dirty after we have written all our zeroes for correct processing in drive mirror code. In the other case we can face not zeroes in this dirty area there in mirror_iteration.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
 block/io.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/io.c +2 -1
index b51f681..513bd99 100644
@@ -2265,7 +2265,6 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
 
     tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
                           nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
-    bdrv_set_dirty(bs, sector_num, nb_sectors);
 
     max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
     while (nb_sectors > 0) {
@@ -2314,6 +2313,8 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
     }
     ret = 0;
 out:
+    bdrv_set_dirty(bs, req.offset << BDRV_SECTOR_BITS,
+                   req.bytes << BDRV_SECTOR_BITS);
     tracked_request_end(&req);
     return ret;
 }
Signature · 1 line
2.5.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: fix race in bdrv_co_discard with drive-mirror

Quoted 38 lines · Denis V. Lunev

Actually we must set dirty bitmap dirty after we have written all our zeroes for correct processing in drive mirror code. In the other case we can face not zeroes in this dirty area there in mirror_iteration.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
 block/io.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/io.c +2 -1
index b51f681..513bd99 100644
@@ -2265,7 +2265,6 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
 
     tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
                           nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
-    bdrv_set_dirty(bs, sector_num, nb_sectors);
 
     max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
     while (nb_sectors > 0) {
@@ -2314,6 +2313,8 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
     }
     ret = 0;
 out:
+    bdrv_set_dirty(bs, req.offset << BDRV_SECTOR_BITS,
+                   req.bytes << BDRV_SECTOR_BITS);
     tracked_request_end(&req);
     return ret;
 }
Signature · 1 line
2.5.0
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: fix race in bdrv_co_discard with drive-mirror

In reply to Denis V. Lunev
Quoted 27 lines · Denis V. Lunev

Actually we must set dirty bitmap dirty after we have written all our zeroes for correct processing in drive mirror code. In the other case we can face not zeroes in this dirty area there in mirror_iteration.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
  block/io.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
block/io.c +0 -1
index b51f681..513bd99 100644
@@ -2265,7 +2265,6 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
 
      tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
                            nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
-    bdrv_set_dirty(bs, sector_num, nb_sectors);
 
      max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
      while (nb_sectors > 0) {

block/io.c int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, · line 2316

Quoted 4 lines
block/io.c +1 -0
@@ -2314,6 +2313,8 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
      }
      ret = 0;
  out:
+    bdrv_set_dirty(bs, req.offset << BDRV_SECTOR_BITS,

s/<</>>/

block/io.c int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, · line 2317

Quoted 1 line
block/io.c +1 -0
+                   req.bytes << BDRV_SECTOR_BITS);

and here

Quoted 3 lines
block/io.c +0 -0
      tracked_request_end(&req);
      return ret;
  }
Signature · 2 lines
Best regards,
Vladimir
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: fix race in bdrv_co_discard with drive-mirror

Quoted 36 lines · Vladimir Sementsov-Ogievskiy
Quoted 29 lines · Denis V. Lunev

Actually we must set dirty bitmap dirty after we have written all our zeroes for correct processing in drive mirror code. In the other case we can face not zeroes in this dirty area there in mirror_iteration.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
  block/io.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
block/io.c +0 -1
index b51f681..513bd99 100644
@@ -2265,7 +2265,6 @@ int coroutine_fn 
 bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
        tracked_request_begin(&req, bs, NULL, sector_num << 
 BDRV_SECTOR_BITS,
                            nb_sectors << BDRV_SECTOR_BITS, 
 BDRV_TRACKED_DISCARD);
-    bdrv_set_dirty(bs, sector_num, nb_sectors);
        max_discard = MIN_NON_ZERO(bs->bl.max_discard, 
BDRV_REQUEST_MAX_SECTORS);
      while (nb_sectors > 0) {

block/io.c int coroutine_fn · line 2317

Quoted 5 lines
block/io.c +1 -0
@@ -2314,6 +2313,8 @@ int coroutine_fn 
 bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
      }
      ret = 0;
  out:
+    bdrv_set_dirty(bs, req.offset << BDRV_SECTOR_BITS,

s/<</>>/

Quoted 7 lines

block/io.c int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, · line 2317

Quoted 1 line
block/io.c +1 -0
+                   req.bytes << BDRV_SECTOR_BITS);

and here

Quoted 3 lines
block/io.c +0 -0
      tracked_request_end(&req);
      return ret;
  }

very nice, thanks ;)

#7 lore raw

PATCH 3/3 block: process before_write_notifiers in bdrv_co_discard

In reply to Denis V. Lunev

This is mandatory for correct backup creation. In the other case the content under this area would be lost.

Dirty bits are set exactly like in bdrv_aligned_pwritev, i.e. they are set even if notifier has returned a error.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
 block/io.c | 5 +++++
 1 file changed, 5 insertions(+)
block/io.c +5 -0
index 513bd99..92d9fe3 100644
@@ -2266,6 +2266,11 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
     tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
                           nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
 
+    ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
+    if (ret < 0) {
+        goto out;
+    }
+
     max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
     while (nb_sectors > 0) {
         int ret;
Signature · 1 line
2.5.0
Reviewed-by on v1 3/3
#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] block: process before_write_notifiers in bdrv_co_discard

Quoted 35 lines · Denis V. Lunev

This is mandatory for correct backup creation. In the other case the content under this area would be lost.

Dirty bits are set exactly like in bdrv_aligned_pwritev, i.e. they are set even if notifier has returned a error.

CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>
---
 block/io.c | 5 +++++
 1 file changed, 5 insertions(+)
block/io.c +5 -0
index 513bd99..92d9fe3 100644
@@ -2266,6 +2266,11 @@ int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
     tracked_request_begin(&req, bs, NULL, sector_num << BDRV_SECTOR_BITS,
                           nb_sectors << BDRV_SECTOR_BITS, BDRV_TRACKED_DISCARD);
 
+    ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
+    if (ret < 0) {
+        goto out;
+    }
+
     max_discard = MIN_NON_ZERO(bs->bl.max_discard, BDRV_REQUEST_MAX_SECTORS);
     while (nb_sectors > 0) {
         int ret;
Signature · 1 line
2.5.0
Reviewed-by on the v1 series
#9 lore raw
In reply to Denis V. Lunev
Quoted 8 lines · Denis V. Lunev

Actually I have found this problem running iotest 132 for active async mirror I have sent yesturday. Anyway, the problem is actual for current backup/mirror implementation.

bdrv_co_discard must mark areas dirty after writing zeroes, it must call before_write_notifier chain to push underlying data to backup and it also must properly fill tracked request information.

Quoted 6 lines
CC Vladimir Sementsov-Ogievskiy<[email protected]>
CC Stefan Hajnoczi <[email protected]>
CC Fam Zheng <[email protected]>
CC Kevin Wolf <[email protected]>
CC Max Reitz <[email protected]>

Three good catches! Thanks for these patches. I think 2 and 3 should be Cced to qemu-stable too.

Paolo