Skip to contentSkip to conversation

file-posix: Fix write_zeroes with unmap on block devices

PATCH In review Kevin Wolf 3 participants 16 messages Jul 26, 2018 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 file-posix: Fix write_zeroes with unmap on block devices

The BLKDISCARD ioctl doesn't guarantee that the discarded blocks read as all-zero afterwards, so don't try to abuse it for zero writing. We try to only use this if BLKDISCARDZEROES tells us that it is safe, but this is unreliable on older kernels and a constant 0 in newer kernels. In other words, this code path is never actually used with newer kernels, so we don't even try to unmap while writing zeros.

This patch removes the abuse of discard for writing zeroes from file-posix and instead adds a new function that uses interfaces that are actually meant to deallocate and zero out at the same time. Only if those fail, it falls back to zeroing out without unmap. We never fall back to a discard operation any more that may or may not result in zeros.

---
 block/file-posix.c | 62 +++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 47 insertions(+), 15 deletions(-)
block/file-posix.c +47 -15
index 60af4b3d51..9c66cd87d1 100644
@@ -648,7 +648,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     }
 #endif
 
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
     ret = 0;
 fail:
     if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
@@ -1487,6 +1487,38 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
     return -ENOTSUP;
 }
 
+static ssize_t handle_aiocb_write_zeroes_unmap(RawPosixAIOData *aiocb)
+{
+    BDRVRawState *s = aiocb->bs->opaque;
+    int ret;
+
+    /* First try to write zeros and unmap at the same time */
+
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);
+    if (ret != -ENOTSUP) {
+        return ret;
+    }
+#endif
+
+#ifdef CONFIG_XFS
+    if (s->is_xfs) {
+        /* xfs_discard() guarantees that the discarded area reads as all-zero
+         * afterwards, so we can use it here. */
+        return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
+    }
+#endif
+
+    /* Make the compiler happy if neither of the above is compiled in */
+    (void) s;
+
+    /* If we couldn't manage to unmap while guaranteed that the area reads as
+     * all-zero afterwards, just write zeroes without unmapping */
+    ret = handle_aiocb_write_zeroes(aiocb);
+    return ret;
+}
+
 #ifndef HAVE_COPY_FILE_RANGE
 static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
                              off_t *out_off, size_t len, unsigned int flags)
@@ -1729,6 +1761,9 @@ static int aio_worker(void *arg)
     case QEMU_AIO_WRITE_ZEROES:
         ret = handle_aiocb_write_zeroes(aiocb);
         break;
+    case QEMU_AIO_WRITE_ZEROES | QEMU_AIO_DISCARD:
+        ret = handle_aiocb_write_zeroes_unmap(aiocb);
+        break;
     case QEMU_AIO_COPY_RANGE:
         ret = handle_aiocb_copy_range(aiocb);
         break;
@@ -2553,15 +2588,13 @@ static int coroutine_fn raw_co_pwrite_zeroes(
     int bytes, BdrvRequestFlags flags)
 {
     BDRVRawState *s = bs->opaque;
+    int operation = QEMU_AIO_WRITE_ZEROES;
 
-    if (!(flags & BDRV_REQ_MAY_UNMAP)) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_WRITE_ZEROES);
-    } else if (s->discard_zeroes) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_DISCARD);
+    if (flags & BDRV_REQ_MAY_UNMAP) {
+        operation |= QEMU_AIO_DISCARD;
     }
-    return -ENOTSUP;
+
+    return paio_submit_co(bs, s->fd, offset, NULL, bytes, operation);
 }
 
 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
@@ -3054,20 +3087,19 @@ static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs,
     int64_t offset, int bytes, BdrvRequestFlags flags)
 {
     BDRVRawState *s = bs->opaque;
+    int operation = QEMU_AIO_WRITE_ZEROES | QEMU_AIO_BLKDEV;
     int rc;
 
     rc = fd_open(bs);
     if (rc < 0) {
         return rc;
     }
-    if (!(flags & BDRV_REQ_MAY_UNMAP)) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
-    } else if (s->discard_zeroes) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
+
+    if (flags & BDRV_REQ_MAY_UNMAP) {
+        operation |= QEMU_AIO_DISCARD;
     }
-    return -ENOTSUP;
+
+    return paio_submit_co(bs, s->fd, offset, NULL, bytes, operation);
 }
 
 static int coroutine_fn hdev_co_create_opts(const char *filename, QemuOpts *opts,
Signature · 1 line
2.13.6
#1 lore raw
Quoted 4 lines · Kevin Wolf

The BLKDISCARD ioctl doesn't guarantee that the discarded blocks read as all-zero afterwards, so don't try to abuse it for zero writing. We try to only use this if BLKDISCARDZEROES tells us that it is safe, but this is unreliable on older kernels and a constant 0 in newer kernels. In

For my own curiosity, which kernel commit switched to constant 0, so I can read the rationale in that commit message?

Quoted 9 lines

other words, this code path is never actually used with newer kernels, so we don't even try to unmap while writing zeros.

This patch removes the abuse of discard for writing zeroes from file-posix and instead adds a new function that uses interfaces that are actually meant to deallocate and zero out at the same time. Only if those fail, it falls back to zeroing out without unmap. We never fall back to a discard operation any more that may or may not result in zeros.

Makes sense.

Quoted 23 lines
---
  block/file-posix.c | 62 +++++++++++++++++++++++++++++++++++++++++-------------
  1 file changed, 47 insertions(+), 15 deletions(-)
block/file-posix.c +1 -1
index 60af4b3d51..9c66cd87d1 100644
@@ -648,7 +648,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
      }
  #endif
 
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
      ret = 0;
  fail:
      if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
@@ -1487,6 +1487,38 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
      return -ENOTSUP;
  }
 

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1499

Quoted 10 lines
block/file-posix.c +10 -0
+static ssize_t handle_aiocb_write_zeroes_unmap(RawPosixAIOData *aiocb)
+{
+    BDRVRawState *s = aiocb->bs->opaque;
+    int ret;
+
+    /* First try to write zeros and unmap at the same time */
+
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);

Umm, doesn't this have to use FALLOC_FL_ZERO_RANGE? FALLOC_FL_PUNCH_HOLE deallocs, but is not required to write zeroes.

Quoted 5 lines
block/file-posix.c +5 -0
+    if (ret != -ENOTSUP) {
+        return ret;
+    }
+#endif
+

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1514

Quoted 10 lines
block/file-posix.c +10 -0
+#ifdef CONFIG_XFS
+    if (s->is_xfs) {
+        /* xfs_discard() guarantees that the discarded area reads as all-zero
+         * afterwards, so we can use it here. */
+        return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
+    }
+#endif
+
+    /* Make the compiler happy if neither of the above is compiled in */
+    (void) s;

Could also be done in fewer lines by use of:

   BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque;

since that attribute means "might be unused, don't warn if it is actually unused" (and not the stricter "must be unused, warn if it got used anyway")

Quoted 10 lines
block/file-posix.c +7 -0
+
+    /* If we couldn't manage to unmap while guaranteed that the area reads as
+     * all-zero afterwards, just write zeroes without unmapping */
+    ret = handle_aiocb_write_zeroes(aiocb);
+    return ret;
+}
+
  #ifndef HAVE_COPY_FILE_RANGE
  static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
                               off_t *out_off, size_t len, unsigned int flags)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 9 lines · Eric Blake
Quoted 4 lines · Kevin Wolf

The BLKDISCARD ioctl doesn't guarantee that the discarded blocks read as all-zero afterwards, so don't try to abuse it for zero writing. We try to only use this if BLKDISCARDZEROES tells us that it is safe, but this is unreliable on older kernels and a constant 0 in newer kernels. In

For my own curiosity, which kernel commit switched to constant 0, so I can read the rationale in that commit message?

This should help
https://patchwork.kernel.org/patch/9903757/

Nir

author
#3 lore raw
In reply to Eric Blake

Am 26.07.2018 um 16:28 hat Eric Blake geschrieben:

Quoted 8 lines · Kevin Wolf
Quoted 4 lines

The BLKDISCARD ioctl doesn't guarantee that the discarded blocks read as all-zero afterwards, so don't try to abuse it for zero writing. We try to only use this if BLKDISCARDZEROES tells us that it is safe, but this is unreliable on older kernels and a constant 0 in newer kernels. In

For my own curiosity, which kernel commit switched to constant 0, so I can read the rationale in that commit message?

I think 48920ff2a5a9 is the commit that actually changes the value.

Essentially the kernel abused discard for zero writes previously (so it needed something like BLKDISCARDZEROES) and then switched to a separate write_zeroes operation like we do in QEMU. Once this was done, second guessing the behaviour of discard wasn't necessary any more because you just tell what you really want to do (discard or zero out).

Quoted 38 lines
Quoted 9 lines

other words, this code path is never actually used with newer kernels, so we don't even try to unmap while writing zeros.

This patch removes the abuse of discard for writing zeroes from file-posix and instead adds a new function that uses interfaces that are actually meant to deallocate and zero out at the same time. Only if those fail, it falls back to zeroing out without unmap. We never fall back to a discard operation any more that may or may not result in zeros.

Makes sense.

Quoted 26 lines
---
  block/file-posix.c | 62 +++++++++++++++++++++++++++++++++++++++++-------------
  1 file changed, 47 insertions(+), 15 deletions(-)
block/file-posix.c +6 -1
index 60af4b3d51..9c66cd87d1 100644
@@ -648,7 +648,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
      }
  #endif
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
      ret = 0;
  fail:
      if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
@@ -1487,6 +1487,38 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
      return -ENOTSUP;
  }
+static ssize_t handle_aiocb_write_zeroes_unmap(RawPosixAIOData *aiocb)
+{
+    BDRVRawState *s = aiocb->bs->opaque;
+    int ret;
+
Quoted 8 lines

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1499

Quoted 5 lines
block/file-posix.c +5 -0
+    /* First try to write zeros and unmap at the same time */
+
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);

Umm, doesn't this have to use FALLOC_FL_ZERO_RANGE? FALLOC_FL_PUNCH_HOLE deallocs, but is not required to write zeroes.

Yes, it is. See the man page:

Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for len bytes. Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file. After a successful call, subsequent reads from this range will return zeroes.

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without unmap.

Quoted 18 lines
Quoted 5 lines
block/file-posix.c +5 -0
+    if (ret != -ENOTSUP) {
+        return ret;
+    }
+#endif
+

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1514

Quoted 10 lines
block/file-posix.c +10 -0
+#ifdef CONFIG_XFS
+    if (s->is_xfs) {
+        /* xfs_discard() guarantees that the discarded area reads as all-zero
+         * afterwards, so we can use it here. */
+        return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
+    }
+#endif
+
+    /* Make the compiler happy if neither of the above is compiled in */
+    (void) s;

Could also be done in fewer lines by use of:

Quoted 4 lines
   BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque;

since that attribute means "might be unused, don't warn if it is actually unused" (and not the stricter "must be unused, warn if it got used anyway")

Thanks, I wasn't aware of G_GNUC_UNUSED (and didn't want to add a __attribute__ here without a macro that wraps it).

Kevin

#4 lore raw
Quoted 7 lines · Kevin Wolf
Quoted 6 lines

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1499

Quoted 3 lines
block/file-posix.c +3 -0
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);

Umm, doesn't this have to use FALLOC_FL_ZERO_RANGE? FALLOC_FL_PUNCH_HOLE deallocs, but is not required to write zeroes.

Quoted 8 lines

Yes, it is. See the man page:

Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for len bytes. Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file. After a successful call, subsequent reads from this range will return zeroes.

That's true for file-system fds, but not for block device fds.

As pointed out by Nir,

Quoted 1 line

https://patchwork.kernel.org/patch/9903757/

Which says, among other things:

Quoted 12 lines
Quoted 2 lines

Do we also know that the blocks were discarded as we do with BLKDISCARD ?

There never was a way to know for sure.

ATA DSM TRIM and SCSI UNMAP are hints by definition. We attempted to bend their semantics towards getting predictable behavior but ultimately failed. Too many corner cases.

Quoted 2 lines

As I mentioned before. We relied on discard_zeroes_data in mkfs.ext4 to make sure that inode tables are zeroed after discard.

Quoted 7 lines

The point is that you shouldn't have an if (discard_zeroes_data) conditional in the first place.

- If you need to dellocate a block range and you don't care about its contents in the future, use BLKDISCARD / FL_PUNCH_HOLE.

- If you need to zero a block range, use BLKZEROOUT / FL_ZERO_RANGE.

PUNCH_HOLE deallocates; but can only guarantee a read back of zero on file systems.

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a new flag not present/documented on Fedora 28. I wonder if it helps, too.

Quoted 2 lines

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without unmap.

I thought the opposite: FALLOC_FL_ZERO_RANGE guarantees that you read back 0, using whatever is most efficient under the hood (in the case of block devices, unmapping that reliably reads back as zero is favored).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#5 lore raw

Am 26.07.2018 um 17:23 hat Eric Blake geschrieben:

Quoted 11 lines · Kevin Wolf
Quoted 10 lines
Quoted 7 lines

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1499

Quoted 4 lines
block/file-posix.c +3 -0
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);

Umm, doesn't this have to use FALLOC_FL_ZERO_RANGE? FALLOC_FL_PUNCH_HOLE deallocs, but is not required to write zeroes.

Yes, it is. See the man page:

Quoted 8 lines
Quoted 6 lines

Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for len bytes. Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file. After a successful call, subsequent reads from this range will return zeroes.

That's true for file-system fds, but not for block device fds.

It is true for block device fds, too. Look at fs/block_dev.c, specifically blkdev_fallocate():

        switch (mode) {
        case FALLOC_FL_ZERO_RANGE:
        case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: 
                error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
                                            GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
                break;
        case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: 
                error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
                                             GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
                break;
        case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
                error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
                                             GFP_KERNEL, 0);
                break;
        default:
                return -EOPNOTSUPP;
        }
Quoted 20 lines

As pointed out by Nir,

Quoted 1 line

https://patchwork.kernel.org/patch/9903757/

Which says, among other things:

Quoted 15 lines
Quoted 2 lines

Do we also know that the blocks were discarded as we do with BLKDISCARD ?

There never was a way to know for sure.

ATA DSM TRIM and SCSI UNMAP are hints by definition. We attempted to bend their semantics towards getting predictable behavior but ultimately failed. Too many corner cases.

Quoted 2 lines

As I mentioned before. We relied on discard_zeroes_data in mkfs.ext4 to make sure that inode tables are zeroed after discard.

The point is that you shouldn't have an if (discard_zeroes_data) conditional in the first place.

Quoted 7 lines
Quoted 4 lines

- If you need to dellocate a block range and you don't care about its contents in the future, use BLKDISCARD / FL_PUNCH_HOLE.

- If you need to zero a block range, use BLKZEROOUT / FL_ZERO_RANGE.

PUNCH_HOLE deallocates; but can only guarantee a read back of zero on file systems.

As far as I know, the comment you quoted is accurate for BLKDISCARD and BLKZEROOUT, but not for the fallocate() flags.

Quoted 9 lines

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a new flag not present/documented on Fedora 28. I wonder if it helps, too.

Quoted 2 lines

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without unmap.

I thought the opposite: FALLOC_FL_ZERO_RANGE guarantees that you read back 0, using whatever is most efficient under the hood (in the case of block devices, unmapping that reliably reads back as zero is favored).

See the code I quoted above, FALLOC_FL_ZERO_RANGE calls blkdev_issue_zeroout() with BLKDEV_ZERO_NOUNMAP internally.

Kevin

#6 lore raw
Quoted 4 lines · Kevin Wolf

As far as I know, the comment you quoted is accurate for BLKDISCARD and BLKZEROOUT, but not for the fallocate() flags.

I sure wish the man pages were more explicit on what guarantees each flag offers.

Quoted 4 lines
Quoted 4 lines

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a new flag not present/documented on Fedora 28. I wonder if it helps, too.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without unmap.

I thought the opposite: FALLOC_FL_ZERO_RANGE guarantees that you read back 0, using whatever is most efficient under the hood (in the case of block devices, unmapping that reliably reads back as zero is favored).

See the code I quoted above, FALLOC_FL_ZERO_RANGE calls blkdev_issue_zeroout() with BLKDEV_ZERO_NOUNMAP internally.

Having to resort to reading the kernel code to learn what the guarantees are is annoying (it's nice that GPL guarantees that we CAN do that, but it means the man pages could use some TLC so that we don't have to). Especially since the kernel code has changed over time.

But your paste of current kernel code is rather hard to argue against.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#13 lore raw
In reply to Kevin Wolf
Quoted 41 lines · Kevin Wolf

The BLKDISCARD ioctl doesn't guarantee that the discarded blocks read as all-zero afterwards, so don't try to abuse it for zero writing. We try to only use this if BLKDISCARDZEROES tells us that it is safe, but this is unreliable on older kernels and a constant 0 in newer kernels. In other words, this code path is never actually used with newer kernels, so we don't even try to unmap while writing zeros.

This patch removes the abuse of discard for writing zeroes from file-posix and instead adds a new function that uses interfaces that are actually meant to deallocate and zero out at the same time. Only if those fail, it falls back to zeroing out without unmap. We never fall back to a discard operation any more that may or may not result in zeros.

---
 block/file-posix.c | 62
+++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 47 insertions(+), 15 deletions(-)
block/file-posix.c +1 -1
index 60af4b3d51..9c66cd87d1 100644
@@ -648,7 +648,7 @@ static int raw_open_common(BlockDriverState *bs, QDict
 *options,
     }
 #endif
 
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
     ret = 0;
 fail:
     if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
block/file-posix.c +2 -0
@@ -1487,6 +1487,38 @@ static ssize_t
 handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
     return -ENOTSUP;
 }
 
+static ssize_t handle_aiocb_write_zeroes_unmap(RawPosixAIOData *aiocb)
+{

block/file-posix.c static ssize_t · line 1502

Quoted 10 lines
block/file-posix.c +9 -0
+    BDRVRawState *s = aiocb->bs->opaque;
+    int ret;
+
+    /* First try to write zeros and unmap at the same time */
+
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);
+    if (ret != -ENOTSUP) {
 

This fails with ENODEV on RHEL 7.5 when fd is a block device.

The manual says:

ENODEV fd does not refer to a regular file or a directory. (If fd
is a pipe or FIFO, a different error results.)

Same issue exists in this patch:
http://lists.gnu.org/archive/html/qemu-devel/2018-03/msg07194.html

+ return ret;

Quoted 91 lines
block/file-posix.c +6 -0
+    }
+#endif
+
+#ifdef CONFIG_XFS
+    if (s->is_xfs) {
+        /* xfs_discard() guarantees that the discarded area reads as

all-zero

block/file-posix.c +9 -0
+         * afterwards, so we can use it here. */
+        return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
+    }
+#endif
+
+    /* Make the compiler happy if neither of the above is compiled in */
+    (void) s;
+
+    /* If we couldn't manage to unmap while guaranteed that the area

reads as

block/file-posix.c +5 -0
+     * all-zero afterwards, just write zeroes without unmapping */
+    ret = handle_aiocb_write_zeroes(aiocb);
+    return ret;
+}
+
 #ifndef HAVE_COPY_FILE_RANGE
 static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
                              off_t *out_off, size_t len, unsigned int

flags)

block/file-posix.c +14 -14
@@ -1729,6 +1761,9 @@ static int aio_worker(void *arg)
     case QEMU_AIO_WRITE_ZEROES:
         ret = handle_aiocb_write_zeroes(aiocb);
         break;
+    case QEMU_AIO_WRITE_ZEROES | QEMU_AIO_DISCARD:
+        ret = handle_aiocb_write_zeroes_unmap(aiocb);
+        break;
     case QEMU_AIO_COPY_RANGE:
         ret = handle_aiocb_copy_range(aiocb);
         break;
@@ -2553,15 +2588,13 @@ static int coroutine_fn raw_co_pwrite_zeroes(
     int bytes, BdrvRequestFlags flags)
 {
     BDRVRawState *s = bs->opaque;
+    int operation = QEMU_AIO_WRITE_ZEROES;
 
-    if (!(flags & BDRV_REQ_MAY_UNMAP)) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_WRITE_ZEROES);
-    } else if (s->discard_zeroes) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_DISCARD);
+    if (flags & BDRV_REQ_MAY_UNMAP) {
+        operation |= QEMU_AIO_DISCARD;
     }
-    return -ENOTSUP;
+
+    return paio_submit_co(bs, s->fd, offset, NULL, bytes, operation);
 }
 
 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
@@ -3054,20 +3087,19 @@ static coroutine_fn int
 hdev_co_pwrite_zeroes(BlockDriverState *bs,
     int64_t offset, int bytes, BdrvRequestFlags flags)
 {
     BDRVRawState *s = bs->opaque;
+    int operation = QEMU_AIO_WRITE_ZEROES | QEMU_AIO_BLKDEV;
     int rc;
 
     rc = fd_open(bs);
     if (rc < 0) {
         return rc;
     }
-    if (!(flags & BDRV_REQ_MAY_UNMAP)) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
-    } else if (s->discard_zeroes) {
-        return paio_submit_co(bs, s->fd, offset, NULL, bytes,
-                              QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
+
+    if (flags & BDRV_REQ_MAY_UNMAP) {
+        operation |= QEMU_AIO_DISCARD;
     }
-    return -ENOTSUP;
+
+    return paio_submit_co(bs, s->fd, offset, NULL, bytes, operation);
 }
 

static int coroutine_fn hdev_co_create_opts(const char *filename,
QemuOpts *opts,
--
2.13.6

author
#14 lore raw

Am 26.07.2018 um 18:22 hat Nir Soffer geschrieben:

Quoted 56 lines · Kevin Wolf
Quoted 42 lines

The BLKDISCARD ioctl doesn't guarantee that the discarded blocks read as all-zero afterwards, so don't try to abuse it for zero writing. We try to only use this if BLKDISCARDZEROES tells us that it is safe, but this is unreliable on older kernels and a constant 0 in newer kernels. In other words, this code path is never actually used with newer kernels, so we don't even try to unmap while writing zeros.

This patch removes the abuse of discard for writing zeroes from file-posix and instead adds a new function that uses interfaces that are actually meant to deallocate and zero out at the same time. Only if those fail, it falls back to zeroing out without unmap. We never fall back to a discard operation any more that may or may not result in zeros.

---
 block/file-posix.c | 62
+++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 47 insertions(+), 15 deletions(-)
block/file-posix.c +1 -1
index 60af4b3d51..9c66cd87d1 100644
@@ -648,7 +648,7 @@ static int raw_open_common(BlockDriverState *bs, QDict
 *options,
     }
 #endif
 
-    bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+    bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
     ret = 0;
 fail:
     if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
block/file-posix.c +2 -0
@@ -1487,6 +1487,38 @@ static ssize_t
 handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
     return -ENOTSUP;
 }
 
+static ssize_t handle_aiocb_write_zeroes_unmap(RawPosixAIOData *aiocb)
+{

block/file-posix.c static ssize_t · line 1502

Quoted 10 lines
block/file-posix.c +9 -0
+    BDRVRawState *s = aiocb->bs->opaque;
+    int ret;
+
+    /* First try to write zeros and unmap at the same time */
+
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                       aiocb->aio_offset, aiocb->aio_nbytes);
+    if (ret != -ENOTSUP) {
 

This fails with ENODEV on RHEL 7.5 when fd is a block device.

Quoted 4 lines

The manual says:

ENODEV fd does not refer to a regular file or a directory. (If fd
is a pipe or FIFO, a different error results.)

do_fallocate() is a wrapper around fallocate() that handles EINTR and translates a few errno values (including ENODEV) into a -ENOTSUP return code.

Kevin

#7 lore raw
In reply to Eric Blake
Quoted 25 lines · Eric Blake
Quoted 4 lines · Kevin Wolf

As far as I know, the comment you quoted is accurate for BLKDISCARD and BLKZEROOUT, but not for the fallocate() flags.

I sure wish the man pages were more explicit on what guarantees each flag offers.

Quoted 1 line
Quoted 1 line

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a new

flag

Quoted 6 lines
Quoted 6 lines

not present/documented on Fedora 28. I wonder if it helps, too.

Quoted 2 lines

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without unmap.

I thought the opposite: FALLOC_FL_ZERO_RANGE guarantees that you read

back

Quoted 5 lines
Quoted 2 lines

0, using whatever is most efficient under the hood (in the case of block devices, unmapping that reliably reads back as zero is favored).

See the code I quoted above, FALLOC_FL_ZERO_RANGE calls blkdev_issue_zeroout() with BLKDEV_ZERO_NOUNMAP internally.

Quoted 7 lines

Having to resort to reading the kernel code to learn what the guarantees are is annoying (it's nice that GPL guarantees that we CAN do that, but it means the man pages could use some TLC so that we don't have to). Especially since the kernel code has changed over time.

But your paste of current kernel code is rather hard to argue against.

I don't think we should depend on undocumented kernel code.

Nir

#15 lore raw
In reply to Kevin Wolf

On Thu, Jul 26, 2018 at 7:41 PM Kevin Wolf <[email protected]> wrote:
...

Quoted 9 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+    ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE |

FALLOC_FL_KEEP_SIZE,

Quoted 6 lines

block/file-posix.c static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) · line 1500

Quoted 3 lines
block/file-posix.c +2 -0
+                       aiocb->aio_offset, aiocb->aio_nbytes);
+    if (ret != -ENOTSUP) {

This fails with ENODEV on RHEL 7.5 when fd is a block device.

Quoted 9 lines
Quoted 4 lines

The manual says:

ENODEV fd does not refer to a regular file or a directory. (If fd
is a pipe or FIFO, a different error results.)

do_fallocate() is a wrapper around fallocate() that handles EINTR and translates a few errno values (including ENODEV) into a -ENOTSUP return code.

I missed that, so this looks correct.

Nir

author
#8 lore raw
In reply to Nir Soffer

Am 26.07.2018 um 18:46 hat Nir Soffer geschrieben:

Quoted 27 lines · Eric Blake
Quoted 26 lines · Kevin Wolf
Quoted 5 lines

As far as I know, the comment you quoted is accurate for BLKDISCARD and BLKZEROOUT, but not for the fallocate() flags.

I sure wish the man pages were more explicit on what guarantees each flag offers.

Quoted 1 line
Quoted 1 line

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a new

flag

Quoted 6 lines
Quoted 6 lines

not present/documented on Fedora 28. I wonder if it helps, too.

Quoted 2 lines

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without unmap.

I thought the opposite: FALLOC_FL_ZERO_RANGE guarantees that you read

back

Quoted 5 lines
Quoted 2 lines

0, using whatever is most efficient under the hood (in the case of block devices, unmapping that reliably reads back as zero is favored).

See the code I quoted above, FALLOC_FL_ZERO_RANGE calls blkdev_issue_zeroout() with BLKDEV_ZERO_NOUNMAP internally.

Quoted 8 lines
Quoted 6 lines

Having to resort to reading the kernel code to learn what the guarantees are is annoying (it's nice that GPL guarantees that we CAN do that, but it means the man pages could use some TLC so that we don't have to). Especially since the kernel code has changed over time.

But your paste of current kernel code is rather hard to argue against.

I don't think we should depend on undocumented kernel code.

Yes, the man page says "filesystem blocks" instead of just "blocks". If you're in a nit-picking mood, that excludes block devices. But I think it's pretty obvious what the intended meaning is and how it consistently extends to block devices, and the code confirms it.

That's by far not "undocumented kernel code" in my book.

Kevin

#9 lore raw
Quoted 41 lines · Kevin Wolf

Am 26.07.2018 um 18:46 hat Nir Soffer geschrieben:

Quoted 6 lines · Eric Blake
Quoted 5 lines · Kevin Wolf
Quoted 3 lines

As far as I know, the comment you quoted is accurate for BLKDISCARD

and

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

BLKZEROOUT, but not for the fallocate() flags.

I sure wish the man pages were more explicit on what guarantees each flag offers.

Quoted 1 line
Quoted 1 line

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a

new

Quoted 5 lines
Quoted 5 lines

flag

Quoted 4 lines
Quoted 4 lines

not present/documented on Fedora 28. I wonder if it helps, too.

Quoted 2 lines

FALLOC_FL_ZERO_RANGE in contrast implements write_zeroes without

unmap.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I thought the opposite: FALLOC_FL_ZERO_RANGE guarantees that you

read

Quoted 2 lines
Quoted 2 lines

back

Quoted 1 line
Quoted 1 line

0, using whatever is most efficient under the hood (in the case of

block

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

devices, unmapping that reliably reads back as zero is favored).

See the code I quoted above, FALLOC_FL_ZERO_RANGE calls blkdev_issue_zeroout() with BLKDEV_ZERO_NOUNMAP internally.

Having to resort to reading the kernel code to learn what the

guarantees

Quoted 6 lines
Quoted 5 lines

are is annoying (it's nice that GPL guarantees that we CAN do that, but it means the man pages could use some TLC so that we don't have to). Especially since the kernel code has changed over time.

But your paste of current kernel code is rather hard to argue against.

Quoted 9 lines
Quoted 1 line

I don't think we should depend on undocumented kernel code.

Yes, the man page says "filesystem blocks" instead of just "blocks". If you're in a nit-picking mood, that excludes block devices. But I think it's pretty obvious what the intended meaning is and how it consistently extends to block devices, and the code confirms it.

That's by far not "undocumented kernel code" in my book.

Maybe it is more correct to say this is poorly documented.

The manual is very specific about supported filesystems, but there nothing about
block devices:

Deallocating file space

Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for len bytes. Within
the
specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file. After a successful
call,
subsequent reads from this range will return zeroes.

The FALLOC_FL_PUNCH_HOLE flag must be ORed with
FALLOC_FL_KEEP_SIZE
in mode; in other words, even when punching off the end of the file,
the
file size (as reported by stat(2)) does not change.

Not all filesystems support FALLOC_FL_PUNCH_HOLE; if a filesystem doesn't support the operation, an error is returned. The operation
is
supported on at least the following filesystems:

* XFS (since Linux 2.6.38)
* ext4 (since Linux 3.0)
* Btrfs (since Linux 3.7)
* tmpfs(5) (since Linux 3.5)

I expect to see something like:

* block devices (since Linux 4.9)

Anyway this looks the same level of documentation as FALLOC_FL_ZERO_RANGE, so if we are ok with using it, FALLOC_FL_PUNCH_HOLE is fine.

Does this change mean that oVirt can start to enable discard for VM when disks are using wipe-after-delete?

Currently we disable discard if a disk is marked for wiping, since discard does not
guarantee that discarded ranges are zeroed.

Nir

#10 lore raw
Quoted 10 lines · Nir Soffer

Anyway this looks the same level of documentation as FALLOC_FL_ZERO_RANGE, so if we are ok with using it, FALLOC_FL_PUNCH_HOLE is fine.

Does this change mean that oVirt can start to enable discard for VM when disks are using wipe-after-delete?

Currently we disable discard if a disk is marked for wiping, since discard does not
guarantee that discarded ranges are zeroed.

Discard does not ever guarantee anything (it is ALWAYS advisory, and thus may be ignored at any point in the stack). If you NEED to guarantee that something reads as zeros, you have to either explicitly write zeroes, or do a combination of discard first and write zeroes second (and hopefully, the combination of discard+write zeroes is sufficiently optimized in the case where you were lucky that discard does result in a reads-as-zeroes to not do any extra work on the write zeroes call).

Oddly enough, write zeroes first and discard second is NOT guaranteed to read zeroes; the most obvious case would be if discard across an encrypted volume has some bulk-erase mechanism that writes all 0s to the hardware but decrypting all 0s now sees garbage, which is different from writing 0s which gets encrypted to something not all 0s on hardware (that said, such a scenario might still be considered reasonably clean for a wipe-after-delete disk - although with discard being advisory, it's hard to make guarantees).

The current kernel implementation that Kevin posted makes it look like for block devices, you now get:

FL_PUNCH_HOLE: guarantee that you read zeroes, using the fastest means possible (possibly by discarding - but only when discard reads as zeroes)

FL_PUNCH_HOLE | FL_NO_HIDE_STALE: deallocate, what you read is no longer guaranteed (a true discard, regardless of whether discard reads as zeroes)

FL_ZERO_RANGE: guarantee that you read zeroes, without using discard

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#11 lore raw
In reply to Nir Soffer

Am 26.07.2018 um 19:34 hat Nir Soffer geschrieben:

Quoted 9 lines · Kevin Wolf
Quoted 8 lines

Am 26.07.2018 um 18:46 hat Nir Soffer geschrieben:

Quoted 1 line

I don't think we should depend on undocumented kernel code.

Yes, the man page says "filesystem blocks" instead of just "blocks". If you're in a nit-picking mood, that excludes block devices. But I think it's pretty obvious what the intended meaning is and how it consistently extends to block devices, and the code confirms it.

Quoted 6 lines
Quoted 1 line

That's by far not "undocumented kernel code" in my book.

Maybe it is more correct to say this is poorly documented.

The manual is very specific about supported filesystems, but there nothing about block devices:

Well, as specific as "_at least_ the following filesystems" can be.

Quoted 5 lines

Not all filesystems support FALLOC_FL_PUNCH_HOLE; if a filesystem doesn't support the operation, an error is returned. The operation
is
supported on at least the following filesystems:

Quoted 8 lines

* XFS (since Linux 2.6.38)
* ext4 (since Linux 3.0)
* Btrfs (since Linux 3.7)
* tmpfs(5) (since Linux 3.5)

I expect to see something like:

* block devices (since Linux 4.9)

Yup, the man page does need an update. But it says "at least", so technically it's not wrong.

Quoted 8 lines

Anyway this looks the same level of documentation as FALLOC_FL_ZERO_RANGE, so if we are ok with using it, FALLOC_FL_PUNCH_HOLE is fine.

Does this change mean that oVirt can start to enable discard for VM when disks are using wipe-after-delete?

Currently we disable discard if a disk is marked for wiping, since discard does not guarantee that discarded ranges are zeroed.

How do you wipe the disk? Does this really involve a write_zeroes operation in qemu? But even if so, we always implemented that correctly (modulo (mostly theoretical?) inaccuracy of BLKDISCARDZEROS in older kernels). This patch fixes that we fall back to inefficient explicit zero writing instead of offloading with recent kernels, but we didn't just discard.

The explanation I've heard before from your team was that you're wiping the disk with regular writes, but they feared that this doesn't take effect for blocks that have previously been discarded. This is bullshit without any basis in reality and has never been correct. When you zero out data regions, they read as zeros, period. It doens't make any difference whether the block had been discarded previously.

So either way, I feel like I already explained it to your team a thousand times, but once again: Get rid of that behaviour yesterday. It doesn't make sense and it never made sense.

Kevin

#12 lore raw
In reply to Eric Blake
Quoted 2 lines · Eric Blake

Hmm - that thread also mentions FALLOC_FL_NO_HIDE_STALE, which is a new flag not present/documented on Fedora 28. I wonder if it helps, too.

Even more odd - that flag has been proposed to the kernel since 2012 but still not mentioned in the F28 man page:

https://lwn.net/Articles/492920/

and that its use on block devices is much more recent, just last month:

https://webcache.googleusercontent.com/search?q=cache:1LV2Y06XnjkJ:https://patchwork.kernel.org/patch/10466183/+&cd=5&hl=en&ct=clnk&gl=us&client=firefox-b-1

but now that I've read that, it makes total sense that the combination PUNCH_HOLE|NO_HIDE_STALE is explicitly asking for semantics of "discard as fast as possible regardless of whatever later reads will see", while plain PUNCH_HOLE means "guarantee that I can read zeroes, using the fastest means possible [discard, if that works]"

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org