Skip to contentSkip to conversation

block: discard alignment fixes

PATCH Superseded Stefan Hajnoczi 4 participants 9 messages Apr 10, 2025 – Apr 16, 2025 Block layer

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover block: discard alignment fixes

v2:
- Fix inverted logic in alignment check [Qing Wang]

Two discard alignment issues were identified in
https://issues.redhat.com/browse/RHEL-86032:
1. pdiscard_alignment is not populated for host_device in file-posix.c.
2. Misaligned head/tail discard requests are not skipped when file-posix.c returns -EINVAL. This causes an undesired pause when guests are configured with werror=stop.

Stefan Hajnoczi (2):
file-posix: probe discard alignment on Linux block devices block/io: skip head/tail requests on EINVAL

 block/file-posix.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-
 block/io.c         |  6 ++++-
 2 files changed, 60 insertions(+), 2 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/2 file-posix: probe discard alignment on Linux block devices

Populate the pdiscard_alignment block limit so the block layer is able align discard requests correctly.

---
 block/file-posix.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)
block/file-posix.c +55 -1
index 56d1972d15..2a1e1f48c0 100644
@@ -1276,10 +1276,10 @@ static int get_sysfs_zoned_model(struct stat *st, BlockZoneModel *zoned)
 }
 #endif /* defined(CONFIG_BLKZONED) */
 
+#ifdef CONFIG_LINUX
 /*
  * Get a sysfs attribute value as a long integer.
  */
-#ifdef CONFIG_LINUX
 static long get_sysfs_long_val(struct stat *st, const char *attribute)
 {
     g_autofree char *str = NULL;
@@ -1299,6 +1299,30 @@ static long get_sysfs_long_val(struct stat *st, const char *attribute)
     }
     return ret;
 }
+
+/*
+ * Get a sysfs attribute value as a uint32_t.
+ */
+static int get_sysfs_u32_val(struct stat *st, const char *attribute,
+                             uint32_t *u32)
+{
+    g_autofree char *str = NULL;
+    const char *end;
+    unsigned int val;
+    int ret;
+
+    ret = get_sysfs_str_val(st, attribute, &str);
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* The file is ended with '\n', pass 'end' to accept that. */
+    ret = qemu_strtoui(str, &end, 10, &val);
+    if (ret == 0 && end && *end == '\0') {
+        *u32 = val;
+    }
+    return ret;
+}
 #endif
 
 static int hdev_get_max_segments(int fd, struct stat *st)
@@ -1318,6 +1342,23 @@ static int hdev_get_max_segments(int fd, struct stat *st)
 #endif
 }
 
+/*
+ * Fills in *dalign with the discard alignment and returns 0 on success,
+ * -errno otherwise.
+ */
+static int hdev_get_pdiscard_alignment(struct stat *st, uint32_t *dalign)
+{
+#ifdef CONFIG_LINUX
+    /*
+     * Note that Linux "discard_granularity" is QEMU "discard_alignment". Linux
+     * "discard_alignment" is something else.
+     */
+    return get_sysfs_u32_val(st, "discard_granularity", dalign);
+#else
+    return -ENOTSUP;
+#endif
+}
+
 #if defined(CONFIG_BLKZONED)
 /*
  * If the reset_all flag is true, then the wps of zone whose state is
@@ -1527,6 +1568,19 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
         }
     }
 
+    if (S_ISBLK(st.st_mode)) {
+        uint32_t dalign = 0;
+        int ret;
+
+        ret = hdev_get_pdiscard_alignment(&st, &dalign);
+        if (ret == 0) {
+            /* Must be a multiple of request_alignment */
+            assert(dalign % bs->bl.request_alignment == 0);
+
+            bs->bl.pdiscard_alignment = dalign;
+        }
+    }
+
     raw_refresh_zoned_limits(bs, &st, errp);
 }
 
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH v2 1/2] file-posix: probe discard alignment on Linux block devices

Quoted 7 lines · Stefan Hajnoczi

Populate the pdiscard_alignment block limit so the block layer is able align discard requests correctly.

---
  block/file-posix.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 55 insertions(+), 1 deletion(-)

Ah, I didn’t know sysfs is actually fair game.  Should we not also get the maximum discard length then, too?

Quoted 73 lines
block/file-posix.c +42 -1
index 56d1972d15..2a1e1f48c0 100644
@@ -1276,10 +1276,10 @@ static int get_sysfs_zoned_model(struct stat *st, BlockZoneModel *zoned)
  }
  #endif /* defined(CONFIG_BLKZONED) */
 
+#ifdef CONFIG_LINUX
  /*
   * Get a sysfs attribute value as a long integer.
   */
-#ifdef CONFIG_LINUX
  static long get_sysfs_long_val(struct stat *st, const char *attribute)
  {
      g_autofree char *str = NULL;
@@ -1299,6 +1299,30 @@ static long get_sysfs_long_val(struct stat *st, const char *attribute)
      }
      return ret;
  }
+
+/*
+ * Get a sysfs attribute value as a uint32_t.
+ */
+static int get_sysfs_u32_val(struct stat *st, const char *attribute,
+                             uint32_t *u32)
+{
+    g_autofree char *str = NULL;
+    const char *end;
+    unsigned int val;
+    int ret;
+
+    ret = get_sysfs_str_val(st, attribute, &str);
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* The file is ended with '\n', pass 'end' to accept that. */
+    ret = qemu_strtoui(str, &end, 10, &val);
+    if (ret == 0 && end && *end == '\0') {
+        *u32 = val;
+    }
+    return ret;
+}
  #endif
 
  static int hdev_get_max_segments(int fd, struct stat *st)
@@ -1318,6 +1342,23 @@ static int hdev_get_max_segments(int fd, struct stat *st)
  #endif
  }
 
+/*
+ * Fills in *dalign with the discard alignment and returns 0 on success,
+ * -errno otherwise.
+ */
+static int hdev_get_pdiscard_alignment(struct stat *st, uint32_t *dalign)
+{
+#ifdef CONFIG_LINUX
+    /*
+     * Note that Linux "discard_granularity" is QEMU "discard_alignment". Linux
+     * "discard_alignment" is something else.
+     */
+    return get_sysfs_u32_val(st, "discard_granularity", dalign);
+#else
+    return -ENOTSUP;
+#endif
+}
+
  #if defined(CONFIG_BLKZONED)
  /*
   * If the reset_all flag is true, then the wps of zone whose state is
@@ -1527,6 +1568,19 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
          }

block/file-posix.c static void raw_refresh_limits(BlockDriverState *bs, Error **errp) · line 1578

Quoted 10 lines
block/file-posix.c +8 -0
      }
 
+    if (S_ISBLK(st.st_mode)) {
+        uint32_t dalign = 0;
+        int ret;
+
+        ret = hdev_get_pdiscard_alignment(&st, &dalign);
+        if (ret == 0) {
+            /* Must be a multiple of request_alignment */
+            assert(dalign % bs->bl.request_alignment == 0);

Is it fair to crash qemu if the kernel reports a value that is not a multiple of request_alignment?  Wouldn’t it make more sense to take the maximum, and if that still isn’t a multiple, return an error here?

Hanna

Quoted 8 lines
block/file-posix.c +5 -0
+
+            bs->bl.pdiscard_alignment = dalign;
+        }
+    }
+
      raw_refresh_zoned_limits(bs, &st, errp);
  }
 
#3 lore raw

Re: [PATCH v2 1/2] file-posix: probe discard alignment on Linux block devices

Quoted 11 lines · Hanna Czenczek
Quoted 7 lines · Stefan Hajnoczi

Populate the pdiscard_alignment block limit so the block layer is able align discard requests correctly.

---
  block/file-posix.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 55 insertions(+), 1 deletion(-)

Ah, I didn’t know sysfs is actually fair game.  Should we not also get the maximum discard length then, too?

The maximum discard length behaves differently: the Linux block layer splits requests according to the maximum discard length. If the guest submits a discard request that is too large for the host, the host block layer will split it and the request succeeds. That is why I didn't make any changes to the maximum discard length in this series.

Quoted 76 lines
Quoted 76 lines
block/file-posix.c +46 -1
index 56d1972d15..2a1e1f48c0 100644
@@ -1276,10 +1276,10 @@ static int get_sysfs_zoned_model(struct stat *st, BlockZoneModel *zoned)
  }
  #endif /* defined(CONFIG_BLKZONED) */
+#ifdef CONFIG_LINUX
  /*
   * Get a sysfs attribute value as a long integer.
   */
-#ifdef CONFIG_LINUX
  static long get_sysfs_long_val(struct stat *st, const char *attribute)
  {
      g_autofree char *str = NULL;
@@ -1299,6 +1299,30 @@ static long get_sysfs_long_val(struct stat *st, const char *attribute)
      }
      return ret;
  }
+
+/*
+ * Get a sysfs attribute value as a uint32_t.
+ */
+static int get_sysfs_u32_val(struct stat *st, const char *attribute,
+                             uint32_t *u32)
+{
+    g_autofree char *str = NULL;
+    const char *end;
+    unsigned int val;
+    int ret;
+
+    ret = get_sysfs_str_val(st, attribute, &str);
+    if (ret < 0) {
+        return ret;
+    }
+
+    /* The file is ended with '\n', pass 'end' to accept that. */
+    ret = qemu_strtoui(str, &end, 10, &val);
+    if (ret == 0 && end && *end == '\0') {
+        *u32 = val;
+    }
+    return ret;
+}
  #endif
  static int hdev_get_max_segments(int fd, struct stat *st)
@@ -1318,6 +1342,23 @@ static int hdev_get_max_segments(int fd, struct stat *st)
  #endif
  }
+/*
+ * Fills in *dalign with the discard alignment and returns 0 on success,
+ * -errno otherwise.
+ */
+static int hdev_get_pdiscard_alignment(struct stat *st, uint32_t *dalign)
+{
+#ifdef CONFIG_LINUX
+    /*
+     * Note that Linux "discard_granularity" is QEMU "discard_alignment". Linux
+     * "discard_alignment" is something else.
+     */
+    return get_sysfs_u32_val(st, "discard_granularity", dalign);
+#else
+    return -ENOTSUP;
+#endif
+}
+
  #if defined(CONFIG_BLKZONED)
  /*
   * If the reset_all flag is true, then the wps of zone whose state is
@@ -1527,6 +1568,19 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
          }
      }
+    if (S_ISBLK(st.st_mode)) {
+        uint32_t dalign = 0;
+        int ret;
+
Quoted 8 lines

block/file-posix.c static void raw_refresh_limits(BlockDriverState *bs, Error **errp) · line 1578

Quoted 4 lines
block/file-posix.c +4 -0
+        ret = hdev_get_pdiscard_alignment(&st, &dalign);
+        if (ret == 0) {
+            /* Must be a multiple of request_alignment */
+            assert(dalign % bs->bl.request_alignment == 0);

Is it fair to crash qemu if the kernel reports a value that is not a multiple of request_alignment?  Wouldn’t it make more sense to take the maximum, and if that still isn’t a multiple, return an error here?

I'll replace the assertion with an error.

The Linux block layer sysfs documentation says:

[RO] Devices that support discard functionality may internally allocate space using units that are bigger than the logical block size.

I don't expect dalign to be smaller than request_alignment, but it doesn't hurt the check if request_alignment would work.

Quoted 11 lines

Hanna

Quoted 7 lines
block/file-posix.c +5 -0
+
+            bs->bl.pdiscard_alignment = dalign;
+        }
+    }
+
      raw_refresh_zoned_limits(bs, &st, errp);
  }
#4 lore raw

Re: [PATCH v2 1/2] file-posix: probe discard alignment on Linux block devices

Am 14.04.2025 um 17:34 hat Stefan Hajnoczi geschrieben:

Quoted 10 lines · Hanna Czenczek
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines

Populate the pdiscard_alignment block limit so the block layer is able align discard requests correctly.

---
  block/file-posix.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 55 insertions(+), 1 deletion(-)
Quoted 8 lines
Quoted 2 lines

Ah, I didn’t know sysfs is actually fair game.  Should we not also get the maximum discard length then, too?

The maximum discard length behaves differently: the Linux block layer splits requests according to the maximum discard length. If the guest submits a discard request that is too large for the host, the host block layer will split it and the request succeeds. That is why I didn't make any changes to the maximum discard length in this series.

Do we need to do something with it for SCSI passthrough? Similar to how we expose bs->bl.max_hw_transfer/iov in the block limits VPD page?

Kevin

#5 lore raw

PATCH v2 2/2 block/io: skip head/tail requests on EINVAL

In reply to Stefan Hajnoczi

When guests send misaligned discard requests, the block layer breaks them up into a misaligned head, an aligned main body, and a misaligned tail.

The file-posix block driver on Linux returns -EINVAL on misaligned discard requests. This causes bdrv_co_pdiscard() to fail and guests configured with werror=stop will pause.

Add a special case for misaligned head/tail requests. Simply continue when EINVAL is encountered so that the aligned main body of the request can be completed and the guest is not paused. This is the best we can do when guest discard limits do not match the host discard limits.

---
 block/io.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
block/io.c +5 -1
index 1ba8d1aeea..a0d0b31a3e 100644
@@ -3180,7 +3180,11 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
             }
         }
         if (ret && ret != -ENOTSUP) {
-            goto out;
+            if (ret == -EINVAL && (offset % align != 0 || num % align != 0)) {
+                /* Silently skip rejected unaligned head/tail requests */
+            } else {
+                goto out; /* bail out */
+            }
         }
 
         offset += num;
Signature · 1 line
2.49.0
Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] block/io: skip head/tail requests on EINVAL

Quoted 24 lines · Stefan Hajnoczi

When guests send misaligned discard requests, the block layer breaks them up into a misaligned head, an aligned main body, and a misaligned tail.

The file-posix block driver on Linux returns -EINVAL on misaligned discard requests. This causes bdrv_co_pdiscard() to fail and guests configured with werror=stop will pause.

Add a special case for misaligned head/tail requests. Simply continue when EINVAL is encountered so that the aligned main body of the request can be completed and the guest is not paused. This is the best we can do when guest discard limits do not match the host discard limits.

---
  block/io.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
block/io.c +0 -0
index 1ba8d1aeea..a0d0b31a3e 100644

block/io.c int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, · line 3183

Quoted 5 lines
block/io.c +1 -1
@@ -3180,7 +3180,11 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
              }
          }
          if (ret && ret != -ENOTSUP) {
-            goto out;
+            if (ret == -EINVAL && (offset % align != 0 || num % align != 0)) {

Could use (offset | num) % align != 0, but either way:

Quoted 7 lines
block/io.c +4 -0
+                /* Silently skip rejected unaligned head/tail requests */
+            } else {
+                goto out; /* bail out */
+            }
          }
 
          offset += num;
#7 lore raw

Re: [PATCH v2 2/2] block/io: skip head/tail requests on EINVAL

Quoted 5 lines · Hanna Czenczek

block/io.c int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, · line 3183

Quoted 3 lines
          if (ret && ret != -ENOTSUP) {
block/io.c +1 -1
-            goto out;
+            if (ret == -EINVAL && (offset % align != 0 || num % align != 0)) {

Could use (offset | num) % align != 0, but either way:

Use of | and & to perform alignment checks only works if align is guaranteed to be a power of 2. But isn't there (odd) hardware out there with something like a 15M alignment, at which point you HAVE to do separate checks with % because bitwise ops no longer work?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#8 lore raw

Re: [PATCH v2 2/2] block/io: skip head/tail requests on EINVAL

Quoted 9 lines · Eric Blake
Quoted 4 lines · Hanna Czenczek

block/io.c int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset, · line 3183

Quoted 3 lines
           if (ret && ret != -ENOTSUP) {
block/io.c +1 -1
-            goto out;
+            if (ret == -EINVAL && (offset % align != 0 || num % align != 0)) {

Could use (offset | num) % align != 0, but either way:

Use of | and & to perform alignment checks only works if align is guaranteed to be a power of 2. But isn't there (odd) hardware out there with something like a 15M alignment, at which point you HAVE to do separate checks with % because bitwise ops no longer work?

Ah, true, thanks!

Hanna