From nobody Thu Apr 2 02:58:39 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 EE89B3C9ECE for ; Mon, 30 Mar 2026 12:53:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875212; cv=none; b=Tks7O/sCCOsZCNmX/1xU9JoFh2QUVdWC5iS6OnKtx3xeqgLbY5cWp4xpm0fdL6PnvS4gopBHlIb/rYNQ/hGFmav07ovQY/dZsP23ix2tId19BCX7f7atTwSvLEROztdxxm+gvgm0EjSl9j37pULuO46l7Yb8arkPeUG2hPuqXxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774875212; c=relaxed/simple; bh=mE7a421+mR6aUe90SYOzNZOzGzVPWHAp6ts1tqQ7+i0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ElxxwYFtt5ukDCCo1G314TBODOWFLqemAfOmNfjPV4Fc/taQ01n+b1p86Umi82X39s7hujHq2BNpqQGEqrKY1N4rl3UJ4KsYwKSPMd9QWqzFSCCwf6qtoB+yzyMmklTYbV0Sn4Sl5hfsQUuNxTg9624uKvJJMAyBEli41ezPJ8I= 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=grW4M1Ce; arc=none smtp.client-ip=170.10.129.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="grW4M1Ce" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774875210; 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=wDzMvwa7hHQR0J91vvkERkkKwoQTKWjm2iHbf3ig1ak=; b=grW4M1Ce8w0CYR4cASMvJhxwCTf0aENqEobMJnXoxgNwzzfYJecjjj1x0YxaSV9/WAnxKd rh0fP5ts1u/Y9hUBe5pXrjsihPEWA+cmCt+ePqJ72Fy51neoBWPyS3RKBgbDxU5rgLfLzD XxNi7F20JxTniX91lYkvtPSoNgvWehA= Received: from mx-prod-mc-01.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-553-YP-4SDj_P5mGu4L44vaIfQ-1; Mon, 30 Mar 2026 08:53:26 -0400 X-MC-Unique: YP-4SDj_P5mGu4L44vaIfQ-1 X-Mimecast-MFC-AGG-ID: YP-4SDj_P5mGu4L44vaIfQ_1774875204 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5B69C195396B; Mon, 30 Mar 2026 12:53:18 +0000 (UTC) Received: from fedora-laptop-x1.redhat.com (unknown [10.72.112.10]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 80E5C1955D96; Mon, 30 Mar 2026 12:53:12 +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 v4] selftests/mm: skip hugetlb_dio tests when DIO alignment is incompatible Date: Mon, 30 Mar 2026 20:53:07 +0800 Message-ID: <20260330125307.98581-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.0 on 10.30.177.12 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: 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 | 97 ++++++++++++++++++------ 1 file changed, 75 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/mm/hugetlb_dio.c b/tools/testing/selft= ests/mm/hugetlb_dio.c index 9ac62eb4c97d..1c673c6c685e 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) { - int fd; char *buffer =3D NULL; char *orig_buffer =3D NULL; size_t h_pagesize =3D 0; @@ -39,10 +84,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 +115,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(); @@ -87,39 +130,49 @@ void run_dio_using_hugetlb(unsigned int start_off, uns= igned int end_off) "free huge pages from %u-%u\n", start_off, end_off); } =20 +static void run_test(int fd, unsigned int start_off, + unsigned int end_off, unsigned int align) +{ + if (!check_dio_alignment(start_off, end_off, align)) + return; + + run_dio_using_hugetlb(fd, start_off, end_off); +} + 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_test(fd, 0, (pagesize * 3), align); =20 /* start is aligned but end is not aligned */ - run_dio_using_hugetlb(0, (pagesize * 3) - (pagesize / 2)); + run_test(fd, 0, (pagesize * 3) - (pagesize / 2), align); =20 /* start is unaligned and end is aligned */ - run_dio_using_hugetlb(pagesize / 2, (pagesize * 3)); + run_test(fd, pagesize / 2, (pagesize * 3), align); =20 /* both start and end are unaligned */ - run_dio_using_hugetlb(pagesize / 2, (pagesize * 3) + (pagesize / 2)); + run_test(fd, pagesize / 2, (pagesize * 3) + (pagesize / 2), align); + + close(fd); =20 ksft_finished(); } --=20 2.53.0