From nobody Wed Apr 1 22:34:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2923536E477 for ; Wed, 1 Apr 2026 09:05:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775034341; cv=none; b=tUqId06Z8jVHBXI58WbRhCdOGUuL29Gwsi45OeN8CO7rs1pKKhRb5Qmp0kHDvRM1p0qbVGvjqJyVMsz0zBB5m+3rV4putjDHlB3dwlqDzWahCFMHXtWA3qGNw1Mm9uvD2kZdBDaZfHCz3bDole+0QOZ6FLcR2zQkGqk6XHKs+/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775034341; c=relaxed/simple; bh=0BJE9RQrwr8D74Ts1d3VtpEAs00vU1r/WZJ+7T5Dgug=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=X8PjaCnWgcDgQBCVBvhYfkp+Vj2chAmW1pXSDTT9HkMvk1doLDNXw6nH481dCKvBQUI1iH5Za+/6ZrpoToY6JLNHtqXbC2jSRYi/Z8l1yRPbI0jmh3XVHPIBPnI6Blaw5aHIXELtkcxofTKonLTU3VEbF2D15UII7QL3GIZmL+o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=CoqEmL5X; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CoqEmL5X" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775034339; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=G1mkLUm4prh4qAYbnuC/D7JWciihmbxYUROMBFnTIno=; b=CoqEmL5XqhUWGo6su4G5BDi4VC0JBIL5FcOxiojMtG8bP/EQx0aKTrJxkf7mjXtaDvffXO F4gRqtJguKPCJOd9mUXm3xy7ZucC81Mu40bzqvijvkZztpj7uGiYbT+NeI1SmRJcjDrNjL dKwpomu57Y4AYw/8WpSRb/VjhAH52to= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-376-hcwD6IBlMGiPpYHhKxqwfA-1; Wed, 01 Apr 2026 05:05:33 -0400 X-MC-Unique: hcwD6IBlMGiPpYHhKxqwfA-1 X-Mimecast-MFC-AGG-ID: hcwD6IBlMGiPpYHhKxqwfA_1775034331 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 98F30195C25F; Wed, 1 Apr 2026 09:05:31 +0000 (UTC) Received: from fedora-laptop-x1.redhat.com (unknown [10.72.112.195]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 331541800107; Wed, 1 Apr 2026 09:05:24 +0000 (UTC) From: Li Wang To: akpm@linux-foundation.org, rppt@kernel.org, david@kernel.org, ljs@kernel.org, Liam.Howlett@oracle.com, vbabka@kernel.org, surenb@google.com, mhocko@suse.com, shuah@kernel.org Cc: aubaker@redhat.com, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v5] selftests/mm: skip hugetlb_dio tests when DIO alignment is incompatible Date: Wed, 1 Apr 2026 17:05:20 +0800 Message-ID: <20260401090520.24018-1-liwang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" hugetlb_dio test uses sub-page offsets (pagesize / 2) to verify that hugepages used as DIO user buffers are correctly unpinned at completion. However, on filesystems with a logical block size larger than half the page size (e.g., 4K-sector block devices), these unaligned DIO writes are rejected with -EINVAL, causing the test to fail unexpectedly. Add get_dio_alignment() to query the filesystem's required DIO alignment via statx(STATX_DIOALIGN) and skip individual test cases whose file offset or write size is not a multiple of that alignment. Aligned cases continue to run so the core coverage is preserved. While here, open the temporary file once in main() and share the fd across all test cases instead of reopening it in each invocation. =3D=3D=3D Reproduce Steps =3D=3D=3D # dd if=3D/dev/zero of=3D/tmp/test.img bs=3D1M count=3D512 # losetup --sector-size 4096 /dev/loop0 /tmp/test.img # mkfs.xfs /dev/loop0 # mkdir -p /mnt/dio_test # mount /dev/loop0 /mnt/dio_test // Modify test to open /mnt/dio_test and rebuild it: - fd =3D open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664); + fd =3D open("/mnt/dio_test", O_TMPFILE | O_RDWR | O_DIRECT, 0664); # getconf PAGESIZE 4096 # echo 100 >/proc/sys/vm/nr_hugepages # ./hugetlb_dio TAP version 13 1..4 # No. Free pages before allocation : 100 # No. Free pages after munmap : 100 ok 1 free huge pages from 0-12288 Bail out! Error writing to file : Invalid argument (22) # Planned tests !=3D run tests (4 !=3D 1) # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Li Wang Suggested-by: Mike Rapoport Suggested-by: David Hildenbrand Acked-by: David Hildenbrand (Arm) --- Notes: v5: - Perform that align checks at the beginning of run_dio_using_hugetlb() but not addinital run_test(). v4: - Open the file once and pass the fd to functions. - Add check_dio_alignment dedicated to alignment checking. v3: - Adopt statx raw syscall to build on older glibc. - add buf offset alignment check as well. v2: - Pass dio_align as a parameter to run_dio_using_hugetlb(). instead of generally page_size/2 alignment check. - Add O_DIRECT flag back to the first open(). - Add stx_dio_offset_align zero check. tools/testing/selftests/mm/hugetlb_dio.c | 91 ++++++++++++++++++------ 1 file changed, 69 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/mm/hugetlb_dio.c b/tools/testing/selft= ests/mm/hugetlb_dio.c index 9ac62eb4c97d..31a054fa8134 100644 --- a/tools/testing/selftests/mm/hugetlb_dio.c +++ b/tools/testing/selftests/mm/hugetlb_dio.c @@ -17,12 +17,57 @@ #include #include #include +#include #include "vm_util.h" #include "kselftest.h" =20 -void run_dio_using_hugetlb(unsigned int start_off, unsigned int end_off) +#ifndef STATX_DIOALIGN +#define STATX_DIOALIGN 0x00002000U +#endif + +static int get_dio_alignment(int fd) +{ + struct statx stx; + int ret; + + ret =3D syscall(__NR_statx, fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx); + if (ret < 0) + return -1; + + /* + * If STATX_DIOALIGN is unsupported, assume no alignment + * constraint and let the test proceed. + */ + if (!(stx.stx_mask & STATX_DIOALIGN) || !stx.stx_dio_offset_align) + return 1; + + return stx.stx_dio_offset_align; +} + +static bool check_dio_alignment(unsigned int start_off, + unsigned int end_off, unsigned int align) +{ + unsigned int writesize =3D end_off - start_off; + + /* + * The kernel's DIO path checks that file offset, length, and + * buffer address are all multiples of dio_offset_align. When + * this test case's parameters don't satisfy that, the write + * would fail with -EINVAL before exercising the hugetlb unpin + * path, so skip. + */ + if (start_off % align !=3D 0 || writesize % align !=3D 0) { + ksft_test_result_skip("DIO align=3D%u incompatible with offset %u writes= ize %u\n", + align, start_off, writesize); + return false; + } + + return true; +} + +static void run_dio_using_hugetlb(int fd, unsigned int start_off, + unsigned int end_off, unsigned int align) { - int fd; char *buffer =3D NULL; char *orig_buffer =3D NULL; size_t h_pagesize =3D 0; @@ -32,6 +77,9 @@ void run_dio_using_hugetlb(unsigned int start_off, unsign= ed int end_off) const int mmap_flags =3D MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB; const int mmap_prot =3D PROT_READ | PROT_WRITE; =20 + if (!check_dio_alignment(start_off, end_off, align)) + return; + writesize =3D end_off - start_off; =20 /* Get the default huge page size */ @@ -39,10 +87,9 @@ void run_dio_using_hugetlb(unsigned int start_off, unsig= ned int end_off) if (!h_pagesize) ksft_exit_fail_msg("Unable to determine huge page size\n"); =20 - /* Open the file to DIO */ - fd =3D open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664); - if (fd < 0) - ksft_exit_fail_perror("Error opening file\n"); + /* Reset file position since fd is shared across tests */ + if (lseek(fd, 0, SEEK_SET) < 0) + ksft_exit_fail_perror("lseek failed\n"); =20 /* Get the free huge pages before allocation */ free_hpage_b =3D get_free_hugepages(); @@ -71,7 +118,6 @@ void run_dio_using_hugetlb(unsigned int start_off, unsig= ned int end_off) =20 /* unmap the huge page */ munmap(orig_buffer, h_pagesize); - close(fd); =20 /* Get the free huge pages after unmap*/ free_hpage_a =3D get_free_hugepages(); @@ -89,37 +135,38 @@ void run_dio_using_hugetlb(unsigned int start_off, uns= igned int end_off) =20 int main(void) { - size_t pagesize =3D 0; - int fd; + int fd, align; + const size_t pagesize =3D psize(); =20 ksft_print_header(); =20 - /* Open the file to DIO */ - fd =3D open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664); - if (fd < 0) - ksft_exit_skip("Unable to allocate file: %s\n", strerror(errno)); - close(fd); - /* Check if huge pages are free */ if (!get_free_hugepages()) ksft_exit_skip("No free hugepage, exiting\n"); =20 - ksft_set_plan(4); + fd =3D open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664); + if (fd < 0) + ksft_exit_skip("Unable to allocate file: %s\n", strerror(errno)); =20 - /* Get base page size */ - pagesize =3D psize(); + align =3D get_dio_alignment(fd); + if (align < 0) + ksft_exit_skip("Unable to obtain DIO alignment: %s\n", + strerror(errno)); + ksft_set_plan(4); =20 /* start and end is aligned to pagesize */ - run_dio_using_hugetlb(0, (pagesize * 3)); + run_dio_using_hugetlb(fd, 0, (pagesize * 3), align); =20 /* start is aligned but end is not aligned */ - run_dio_using_hugetlb(0, (pagesize * 3) - (pagesize / 2)); + run_dio_using_hugetlb(fd, 0, (pagesize * 3) - (pagesize / 2), align); =20 /* start is unaligned and end is aligned */ - run_dio_using_hugetlb(pagesize / 2, (pagesize * 3)); + run_dio_using_hugetlb(fd, pagesize / 2, (pagesize * 3), align); =20 /* both start and end are unaligned */ - run_dio_using_hugetlb(pagesize / 2, (pagesize * 3) + (pagesize / 2)); + run_dio_using_hugetlb(fd, pagesize / 2, (pagesize * 3) + (pagesize / 2), = align); + + close(fd); =20 ksft_finished(); } --=20 2.53.0