All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] file-posix: add statx(STATX_DIOALIGN) support
@ 2026-09-22 15:43 Stefan Hajnoczi
  2026-09-25 18:53 ` Ben Chaney
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2026-09-22 15:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Hanna Reitz, Kevin Wolf, qemu-block, nsoffer, Stefan Hajnoczi,
	Eric Biggers

Linux v6.1 commit 825cf206ed51 ("statx: add direct I/O alignment
information") added an interface to fetch O_DIRECT alignment values for
block devices and file systems.

Prefer STATX_DIOALIGN to older interfaces and probing, but keep them as
fallbacks in case STATX_DIOALIGN cannot provide the information.

STATX_DIOALIGN support as of Linux 6.1:
- btrfs: no
- ext4: yes
- XFS: yes
- block devices: yes
- dm-crypt: yes

This change fixes #1290 ("IO alignment probing delivers incorrect
results on Linux when used with e.g. dm-crypt") on Linux host kernel
version 6.1 or later by avoiding the problematic probing code in
raw_probe_alignment(). It would be nice to fix the probing code but I
have not been able to reproduce it. Stable/LTS distros ship Linux 6.1+,
so the number of users without STATX_DIOALIGN is becoming small.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1290
Cc: Eric Biggers <ebiggers@google.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
v3:
- Rebase and update commit description
v2:
- Make sure that XFS_IOC_DIOINFO takes priority over logical blocksize [Eric Biggers]
---
 block/file-posix.c | 56 +++++++++++++++++++++++++++++++---------------
 1 file changed, 38 insertions(+), 18 deletions(-)

diff --git a/block/file-posix.c b/block/file-posix.c
index 9aad156aad4..0dedd090068 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -426,28 +426,48 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
 
     bs->bl.request_alignment = 0;
     s->buf_align = 0;
-    /* Let's try to use the logical blocksize for the alignment. */
-    if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
-        bs->bl.request_alignment = 0;
+
+#if defined(__linux__) && defined(STATX_DIOALIGN)
+    struct statx stx;
+
+    /*
+     * Linux 6.1 introduced an interface for both block devices and file
+     * systems. The system call returns with the STATX_DIOALIGN bit cleared
+     * when the information is unavailable.
+     */
+    if (statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx) == 0 &&
+        (stx.stx_mask & STATX_DIOALIGN)) {
+        bs->bl.request_alignment = stx.stx_dio_offset_align;
+        s->buf_align = stx.stx_dio_mem_align;
     }
+#endif /* defined(__linux__) && defined(STATX_DIOALIGN) */
 
 #ifdef __linux__
-    /*
-     * The XFS ioctl definitions are shipped in extra packages that might
-     * not always be available. Since we just need the XFS_IOC_DIOINFO ioctl
-     * here, we simply use our own definition instead:
-     */
-    struct xfs_dioattr {
-        uint32_t d_mem;
-        uint32_t d_miniosz;
-        uint32_t d_maxiosz;
-    } da;
-    if (ioctl(fd, _IOR('X', 30, struct xfs_dioattr), &da) >= 0) {
-        bs->bl.request_alignment = da.d_miniosz;
-        /* The kernel returns wrong information for d_mem */
-        /* s->buf_align = da.d_mem; */
+    if (!bs->bl.request_alignment) {
+        /*
+         * The XFS ioctl definitions are shipped in extra packages that might
+         * not always be available. Since we just need the XFS_IOC_DIOINFO ioctl
+         * here, we simply use our own definition instead:
+         */
+        struct xfs_dioattr {
+            uint32_t d_mem;
+            uint32_t d_miniosz;
+            uint32_t d_maxiosz;
+        } da;
+        if (ioctl(fd, _IOR('X', 30, struct xfs_dioattr), &da) >= 0) {
+            bs->bl.request_alignment = da.d_miniosz;
+            /* The kernel returns wrong information for d_mem */
+            /* s->buf_align = da.d_mem; */
+        }
+    }
+#endif /* __linux__ */
+
+    /* Let's try to use the logical blocksize for the alignment. */
+    if (!bs->bl.request_alignment) {
+        if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
+            bs->bl.request_alignment = 0;
+        }
     }
-#endif
 
     /*
      * If we could not get the sizes so far, we can only guess them. First try
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] file-posix: add statx(STATX_DIOALIGN) support
  2026-09-22 15:43 [PATCH] file-posix: add statx(STATX_DIOALIGN) support Stefan Hajnoczi
@ 2026-09-25 18:53 ` Ben Chaney
  2026-10-01 19:39 ` Stefan Hajnoczi
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ben Chaney @ 2026-09-25 18:53 UTC (permalink / raw)
  To: stefanha
  Cc: ebiggers, hreitz, kwolf, nsoffer, qemu-block, qemu-devel,
	Ben Chaney

> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1290

We encountered a similar issue and this patch resolves it for us.

Reviewed-by: Ben Chaney <bchaney@akamai.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] file-posix: add statx(STATX_DIOALIGN) support
  2026-09-22 15:43 [PATCH] file-posix: add statx(STATX_DIOALIGN) support Stefan Hajnoczi
  2026-09-25 18:53 ` Ben Chaney
@ 2026-10-01 19:39 ` Stefan Hajnoczi
  2026-10-05 14:13 ` Kevin Wolf
  2026-10-11  3:23 ` Michael Tokarev
  3 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2026-10-01 19:39 UTC (permalink / raw)
  To: Hanna Reitz, Kevin Wolf; +Cc: qemu-devel, qemu-block, nsoffer, Eric Biggers

On Tue, Sep 22, 2026 at 11:43:51AM -0400, Stefan Hajnoczi wrote:
> Linux v6.1 commit 825cf206ed51 ("statx: add direct I/O alignment
> information") added an interface to fetch O_DIRECT alignment values for
> block devices and file systems.
> 
> Prefer STATX_DIOALIGN to older interfaces and probing, but keep them as
> fallbacks in case STATX_DIOALIGN cannot provide the information.
> 
> STATX_DIOALIGN support as of Linux 6.1:
> - btrfs: no
> - ext4: yes
> - XFS: yes
> - block devices: yes
> - dm-crypt: yes
> 
> This change fixes #1290 ("IO alignment probing delivers incorrect
> results on Linux when used with e.g. dm-crypt") on Linux host kernel
> version 6.1 or later by avoiding the problematic probing code in
> raw_probe_alignment(). It would be nice to fix the probing code but I
> have not been able to reproduce it. Stable/LTS distros ship Linux 6.1+,
> so the number of users without STATX_DIOALIGN is becoming small.
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1290
> Cc: Eric Biggers <ebiggers@google.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> v3:
> - Rebase and update commit description
> v2:
> - Make sure that XFS_IOC_DIOINFO takes priority over logical blocksize [Eric Biggers]
> ---
>  block/file-posix.c | 56 +++++++++++++++++++++++++++++++---------------
>  1 file changed, 38 insertions(+), 18 deletions(-)

Ping

> 
> diff --git a/block/file-posix.c b/block/file-posix.c
> index 9aad156aad4..0dedd090068 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -426,28 +426,48 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
>  
>      bs->bl.request_alignment = 0;
>      s->buf_align = 0;
> -    /* Let's try to use the logical blocksize for the alignment. */
> -    if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
> -        bs->bl.request_alignment = 0;
> +
> +#if defined(__linux__) && defined(STATX_DIOALIGN)
> +    struct statx stx;
> +
> +    /*
> +     * Linux 6.1 introduced an interface for both block devices and file
> +     * systems. The system call returns with the STATX_DIOALIGN bit cleared
> +     * when the information is unavailable.
> +     */
> +    if (statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx) == 0 &&
> +        (stx.stx_mask & STATX_DIOALIGN)) {
> +        bs->bl.request_alignment = stx.stx_dio_offset_align;
> +        s->buf_align = stx.stx_dio_mem_align;
>      }
> +#endif /* defined(__linux__) && defined(STATX_DIOALIGN) */
>  
>  #ifdef __linux__
> -    /*
> -     * The XFS ioctl definitions are shipped in extra packages that might
> -     * not always be available. Since we just need the XFS_IOC_DIOINFO ioctl
> -     * here, we simply use our own definition instead:
> -     */
> -    struct xfs_dioattr {
> -        uint32_t d_mem;
> -        uint32_t d_miniosz;
> -        uint32_t d_maxiosz;
> -    } da;
> -    if (ioctl(fd, _IOR('X', 30, struct xfs_dioattr), &da) >= 0) {
> -        bs->bl.request_alignment = da.d_miniosz;
> -        /* The kernel returns wrong information for d_mem */
> -        /* s->buf_align = da.d_mem; */
> +    if (!bs->bl.request_alignment) {
> +        /*
> +         * The XFS ioctl definitions are shipped in extra packages that might
> +         * not always be available. Since we just need the XFS_IOC_DIOINFO ioctl
> +         * here, we simply use our own definition instead:
> +         */
> +        struct xfs_dioattr {
> +            uint32_t d_mem;
> +            uint32_t d_miniosz;
> +            uint32_t d_maxiosz;
> +        } da;
> +        if (ioctl(fd, _IOR('X', 30, struct xfs_dioattr), &da) >= 0) {
> +            bs->bl.request_alignment = da.d_miniosz;
> +            /* The kernel returns wrong information for d_mem */
> +            /* s->buf_align = da.d_mem; */
> +        }
> +    }
> +#endif /* __linux__ */
> +
> +    /* Let's try to use the logical blocksize for the alignment. */
> +    if (!bs->bl.request_alignment) {
> +        if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
> +            bs->bl.request_alignment = 0;
> +        }
>      }
> -#endif
>  
>      /*
>       * If we could not get the sizes so far, we can only guess them. First try
> -- 
> 2.55.0
> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] file-posix: add statx(STATX_DIOALIGN) support
  2026-09-22 15:43 [PATCH] file-posix: add statx(STATX_DIOALIGN) support Stefan Hajnoczi
  2026-09-25 18:53 ` Ben Chaney
  2026-10-01 19:39 ` Stefan Hajnoczi
@ 2026-10-05 14:13 ` Kevin Wolf
  2026-10-11  3:23 ` Michael Tokarev
  3 siblings, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2026-10-05 14:13 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: qemu-devel, Hanna Reitz, qemu-block, nsoffer, Eric Biggers

Am 22.09.2026 um 17:43 hat Stefan Hajnoczi geschrieben:
> Linux v6.1 commit 825cf206ed51 ("statx: add direct I/O alignment
> information") added an interface to fetch O_DIRECT alignment values for
> block devices and file systems.
> 
> Prefer STATX_DIOALIGN to older interfaces and probing, but keep them as
> fallbacks in case STATX_DIOALIGN cannot provide the information.
> 
> STATX_DIOALIGN support as of Linux 6.1:
> - btrfs: no
> - ext4: yes
> - XFS: yes
> - block devices: yes
> - dm-crypt: yes
> 
> This change fixes #1290 ("IO alignment probing delivers incorrect
> results on Linux when used with e.g. dm-crypt") on Linux host kernel
> version 6.1 or later by avoiding the problematic probing code in
> raw_probe_alignment(). It would be nice to fix the probing code but I
> have not been able to reproduce it. Stable/LTS distros ship Linux 6.1+,
> so the number of users without STATX_DIOALIGN is becoming small.
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1290
> Cc: Eric Biggers <ebiggers@google.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> v3:
> - Rebase and update commit description
> v2:
> - Make sure that XFS_IOC_DIOINFO takes priority over logical blocksize [Eric Biggers]

Thanks, applied to the block branch.

Kevin



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] file-posix: add statx(STATX_DIOALIGN) support
  2026-09-22 15:43 [PATCH] file-posix: add statx(STATX_DIOALIGN) support Stefan Hajnoczi
                   ` (2 preceding siblings ...)
  2026-10-05 14:13 ` Kevin Wolf
@ 2026-10-11  3:23 ` Michael Tokarev
  3 siblings, 0 replies; 5+ messages in thread
From: Michael Tokarev @ 2026-10-11  3:23 UTC (permalink / raw)
  To: Stefan Hajnoczi, qemu-devel
  Cc: Hanna Reitz, Kevin Wolf, qemu-block, nsoffer, Eric Biggers,
	qemu-stable

On 22.09.2026 18:43, Stefan Hajnoczi wrote:
> Linux v6.1 commit 825cf206ed51 ("statx: add direct I/O alignment
> information") added an interface to fetch O_DIRECT alignment values for
> block devices and file systems.
> 
> Prefer STATX_DIOALIGN to older interfaces and probing, but keep them as
> fallbacks in case STATX_DIOALIGN cannot provide the information.
> 
> STATX_DIOALIGN support as of Linux 6.1:
> - btrfs: no
> - ext4: yes
> - XFS: yes
> - block devices: yes
> - dm-crypt: yes
> 
> This change fixes #1290 ("IO alignment probing delivers incorrect
> results on Linux when used with e.g. dm-crypt") on Linux host kernel
> version 6.1 or later by avoiding the problematic probing code in
> raw_probe_alignment(). It would be nice to fix the probing code but I
> have not been able to reproduce it. Stable/LTS distros ship Linux 6.1+,
> so the number of users without STATX_DIOALIGN is becoming small.
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/1290
> Cc: Eric Biggers <ebiggers@google.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>

I'm picking this up for the currently active qemu stable series too,
it should be a nice addition there.

Thanks,

/mjt


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-11  3:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-22 15:43 [PATCH] file-posix: add statx(STATX_DIOALIGN) support Stefan Hajnoczi
2026-09-25 18:53 ` Ben Chaney
2026-10-01 19:39 ` Stefan Hajnoczi
2026-10-05 14:13 ` Kevin Wolf
2026-10-11  3:23 ` Michael Tokarev

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.