Skip to contentSkip to conversation

block: fix write with zero flag set and iovector provided

PATCH Superseded Anton Nefedov 4 participants 7 messages Feb 1, 2018 – Feb 6, 2018 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix write with zero flag set and iovector provided

The normal bdrv_co_pwritev() use is either
- BDRV_REQ_ZERO_WRITE reset and iovector provided
- BDRV_REQ_ZERO_WRITE set and iovector == NULL

while
- the flag reset and iovector == NULL is an assertion failure in bdrv_co_do_zero_pwritev()
- the flag set and iovector provided is in fact allowed (the flag prevails and zeroes are written)

However the alignment logic does not support the latter case so the padding areas get overwritten with zeroes.

Solution could be to forbid such case or just use bdrv_co_do_zero_pwritev() alignment for it which also makes the code a bit more obvious anyway.

---
 block/io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/io.c +1 -1
index 7ea4023..cf63fd0 100644
@@ -1701,7 +1701,7 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
      */
     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
 
-    if (!qiov) {
+    if (flags & BDRV_REQ_ZERO_WRITE) {
         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);
         goto out;
     }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 7 lines · Anton Nefedov

The normal bdrv_co_pwritev() use is either
- BDRV_REQ_ZERO_WRITE reset and iovector provided
- BDRV_REQ_ZERO_WRITE set and iovector == NULL

while
- the flag reset and iovector == NULL is an assertion failure in bdrv_co_do_zero_pwritev()

Where is that assertion?

Berto

Reviewed-by on v1 1/1
#3 lore raw
In reply to Anton Nefedov
Quoted 2 lines · Anton Nefedov

The normal bdrv_co_pwritev() use is either
- BDRV_REQ_ZERO_WRITE reset and iovector provided

s/reset/clear/

Quoted 4 lines

- BDRV_REQ_ZERO_WRITE set and iovector == NULL

while
- the flag reset and iovector == NULL is an assertion failure

again

Quoted 20 lines

in bdrv_co_do_zero_pwritev()
- the flag set and iovector provided is in fact allowed (the flag prevails and zeroes are written)

However the alignment logic does not support the latter case so the padding areas get overwritten with zeroes.

Solution could be to forbid such case or just use bdrv_co_do_zero_pwritev() alignment for it which also makes the code a bit more obvious anyway.

---
 block/io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/io.c +0 -0
index 7ea4023..cf63fd0 100644

block/io.c int coroutine_fn bdrv_co_pwritev(BdrvChild *child, · line 1705

Quoted 6 lines
block/io.c +1 -1
@@ -1701,7 +1701,7 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
      */
     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
 
-    if (!qiov) {
+    if (flags & BDRV_REQ_ZERO_WRITE) {
         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);

So now, the flag rules, but we assert that !qiov (so it would only break a caller that passed the flag but used qiov, which you argued shouldn't exist).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
In reply to Alberto Garcia
Quoted 5 lines · Alberto Garcia
Quoted 4 lines · Anton Nefedov

The normal bdrv_co_pwritev() use is either
- BDRV_REQ_ZERO_WRITE reset and iovector provided
- BDRV_REQ_ZERO_WRITE set and iovector == NULL

Quoted 8 lines
Quoted 3 lines

while
- the flag reset and iovector == NULL is an assertion failure in bdrv_co_do_zero_pwritev()

Where is that assertion?

Berto

beginning of bdrv_co_do_zero_pwritev():

    assert(flags & BDRV_REQ_ZERO_WRITE);

and bdrv_co_do_zero_pwritev() was only called with qiov==NULL.

Now this case will instead segfault at some point. Don't know if it needs a separate assertion.

/Anton

#4 lore raw
In reply to Eric Blake
Quoted 36 lines · Eric Blake
Quoted 2 lines · Anton Nefedov

The normal bdrv_co_pwritev() use is either
- BDRV_REQ_ZERO_WRITE reset and iovector provided

s/reset/clear/

Quoted 4 lines

- BDRV_REQ_ZERO_WRITE set and iovector == NULL

while
- the flag reset and iovector == NULL is an assertion failure

again

Quoted 23 lines

in bdrv_co_do_zero_pwritev()
- the flag set and iovector provided is in fact allowed (the flag prevails and zeroes are written)

However the alignment logic does not support the latter case so the padding areas get overwritten with zeroes.

Solution could be to forbid such case or just use bdrv_co_do_zero_pwritev() alignment for it which also makes the code a bit more obvious anyway.

---
 block/io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/io.c +0 -0
index 7ea4023..cf63fd0 100644
@@ -1701,7 +1701,7 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
      */
     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
 
Quoted 7 lines

block/io.c int coroutine_fn bdrv_co_pwritev(BdrvChild *child, · line 1705

Quoted 3 lines
block/io.c +1 -1
-    if (!qiov) {
+    if (flags & BDRV_REQ_ZERO_WRITE) {
         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);

So now, the flag rules, but we assert that !qiov (so it would only break a caller that passed the flag but used qiov, which you argued shouldn't exist).

Sorry, I hit send too soon. I'm asking if we should have assert(!qiov) right before calling bdrv_co_do_zero_pwritev (it would break a caller that passed the flag and qiov, but you were arguing that such callers previously misbehaved, so we don't want such callers).

But adding such an assertion may trigger failures that we'd have to fix, while leaving things without the assertion conservatively seems okay.

Quoted 3 lines
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw
Quoted 14 lines · Eric Blake
Quoted 6 lines
Quoted 6 lines
block/io.c +0 -0
@@ -1701,7 +1701,7 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
      */
     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
 
Quoted 7 lines

block/io.c int coroutine_fn bdrv_co_pwritev(BdrvChild *child, · line 1705

Quoted 3 lines
block/io.c +1 -1
-    if (!qiov) {
+    if (flags & BDRV_REQ_ZERO_WRITE) {
         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);

So now, the flag rules, but we assert that !qiov (so it would only break a caller that passed the flag but used qiov, which you argued shouldn't exist).

Quoted 5 lines

Sorry, I hit send too soon. I'm asking if we should have assert(!qiov) right before calling bdrv_co_do_zero_pwritev (it would break a caller that passed the flag and qiov, but you were arguing that such callers previously misbehaved, so we don't want such callers).

Those callers do exist as a matter of fact: bdrv_rw_co_entry() always passes a qiov to bdrv_co_pwritev() regardless of the flags (the request size is actually taken from the very qiov).

bdrv_pwrite_zeroes() is one example:

$ qemu-img create -f qcow2 base.img 100M
$ qemu-img create -f qcow2 -b base.img active.img
$ qemu-io -c 'write -z 0 128k' -f qcow2 active.img 
$ qemu-img amend -o compat=0.10 active.img 

It even uses an iovec with iov_base = NULL but iov_len != 0, which looks like an abuse of the data structure.

Berto

#6 lore raw
In reply to Anton Nefedov
Quoted 4 lines · Anton Nefedov

The normal bdrv_co_pwritev() use is either
- BDRV_REQ_ZERO_WRITE reset and iovector provided
- BDRV_REQ_ZERO_WRITE set and iovector == NULL

Quoted 8 lines

while
- the flag reset and iovector == NULL is an assertion failure in bdrv_co_do_zero_pwritev()
- the flag set and iovector provided is in fact allowed (the flag prevails and zeroes are written)

However the alignment logic does not support the latter case so the padding areas get overwritten with zeroes.

Please include a test case. Berto mentioned that bdrv_pwrite_zeroes() hits this issue, that might be one way to test it.

Quoted 13 lines

Solution could be to forbid such case or just use bdrv_co_do_zero_pwritev() alignment for it which also makes the code a bit more obvious anyway.

---
 block/io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/io.c +0 -0
index 7ea4023..cf63fd0 100644

block/io.c int coroutine_fn bdrv_co_pwritev(BdrvChild *child, · line 1707

Quoted 8 lines
block/io.c +1 -1
@@ -1701,7 +1701,7 @@ int coroutine_fn bdrv_co_pwritev(BdrvChild *child,
      */
     tracked_request_begin(&req, bs, offset, bytes, BDRV_TRACKED_WRITE);
 
-    if (!qiov) {
+    if (flags & BDRV_REQ_ZERO_WRITE) {
         ret = bdrv_co_do_zero_pwritev(child, offset, bytes, flags, &req);
         goto out;
     }

Looks good.