From nobody Thu Apr 2 15:41:23 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 BC00633A708 for ; Fri, 27 Mar 2026 12:03:24 +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=1774613006; cv=none; b=Mx7jObq3pvwDe6JbWJaZJNmwomXkJdgYai+ctxQYO4ULy8WDneB4uougCCE3Ia4kZL2hSGuvoQ8flVeUZT5smmvQ+1OHLeSEx1KnNibsIqSd1V7DWqUnmTTrs7KYIVay+qKtku9IPiWhLQdHIX7WpepKoF52PHi4BfRZTCRu5rY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774613006; c=relaxed/simple; bh=lhJdUY0Mhi6aXaX+nWl4o10djbKJKushXLkCbuHA1E4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=uaCtaE/uNCMifggk5fwrrbnWSOiGdryMdYOSWYwI1LFJTeQzrEPDlsvh9Rr2HGbiG44urQpuzeNFauUqGwNypr6f4OIXPfjaMNL6b8nJNRI5z4PuUTeLlfk9Hcg0AJTintLtaEnnUrB6B4ciA29ceRxp0d5qW3lVfJ8er3Xg4C4= 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=FzidPEvx; 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="FzidPEvx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774613003; 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=m7nefQy6MzFPaVFBX4vR9wqZM8EfsR8dpshxHtPRdhI=; b=FzidPEvx/fepkGKVwFieXXgYhJmfbnG5yZh5JhWbT548L7G/+Lu3d3CyepQ42v+JGAc8X1 5cfM4PrS99xb+4c+CcqwEJbSxEpgnYVWPR3P76Zv3kLZO4eetQiQGJRIqRRXb4pZZrR+9q XwXHLCIrNuIzl0chNgEZaodp5CQ6z0o= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-652-dgl2yJLpOAahmfcCskk-Zw-1; Fri, 27 Mar 2026 08:03:18 -0400 X-MC-Unique: dgl2yJLpOAahmfcCskk-Zw-1 X-Mimecast-MFC-AGG-ID: dgl2yJLpOAahmfcCskk-Zw_1774612997 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 95A5B18005B6; Fri, 27 Mar 2026 12:03:16 +0000 (UTC) Received: from fedora-laptop-x1.redhat.com (unknown [10.72.112.57]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 13F051955D71; Fri, 27 Mar 2026 12:03:09 +0000 (UTC) From: Li Wang To: rppt@kernel.org, akpm@linux-foundation.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, liwang@redhat.com, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] selftests/mm: skip hugetlb_dio tests when DIO alignment is incompatible Date: Fri, 27 Mar 2026 20:03:05 +0800 Message-ID: <20260327120305.58653-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.17 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 pass it to run_dio_using_hugetlb(). Skip individual test cases whose write length is not a multiple of the alignment, so that aligned cases are still tested. =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 --- Notes: v2: - Pass dio_align as a parameter to run_dio_using_hugetlb() instead of generally page_size/2 alignment check. tools/testing/selftests/mm/hugetlb_dio.c | 56 +++++++++++++++++------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/mm/hugetlb_dio.c b/tools/testing/selft= ests/mm/hugetlb_dio.c index 9ac62eb4c97d..638a8abe3ae2 100644 --- a/tools/testing/selftests/mm/hugetlb_dio.c +++ b/tools/testing/selftests/mm/hugetlb_dio.c @@ -20,7 +20,35 @@ #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 + +unsigned int get_dio_alignment(void) +{ + int fd, ret; + struct statx stx; + unsigned int dio_align =3D 1; + + 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)); + + ret =3D statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx); + if (ret < 0) { + ksft_perror("statx() failed"); + } else if ((stx.stx_mask & STATX_DIOALIGN) && + stx.stx_dio_offset_align) { + dio_align =3D stx.stx_dio_offset_align; + } + + close(fd); + + return dio_align; +} + +void run_dio_using_hugetlb(unsigned int start_off, unsigned int end_off, + unsigned int dio_align) { int fd; char *buffer =3D NULL; @@ -33,6 +61,11 @@ void run_dio_using_hugetlb(unsigned int start_off, unsig= ned int end_off) const int mmap_prot =3D PROT_READ | PROT_WRITE; =20 writesize =3D end_off - start_off; + if (writesize % dio_align !=3D 0) { + ksft_test_result_skip("DIO alignment (%u) incompatible with offset %zu\n= ", + dio_align, writesize); + return; + } =20 /* Get the default huge page size */ h_pagesize =3D default_huge_page_size(); @@ -89,37 +122,28 @@ 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; + size_t pagesize =3D psize(); + unsigned int dio_align =3D get_dio_alignment(); =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); =20 - /* Get base page size */ - pagesize =3D psize(); - /* start and end is aligned to pagesize */ - run_dio_using_hugetlb(0, (pagesize * 3)); + run_dio_using_hugetlb(0, (pagesize * 3), dio_align); =20 /* start is aligned but end is not aligned */ - run_dio_using_hugetlb(0, (pagesize * 3) - (pagesize / 2)); + run_dio_using_hugetlb(0, (pagesize * 3) - (pagesize / 2), dio_align); =20 /* start is unaligned and end is aligned */ - run_dio_using_hugetlb(pagesize / 2, (pagesize * 3)); + run_dio_using_hugetlb(pagesize / 2, (pagesize * 3), dio_align); =20 /* both start and end are unaligned */ - run_dio_using_hugetlb(pagesize / 2, (pagesize * 3) + (pagesize / 2)); + run_dio_using_hugetlb(pagesize / 2, (pagesize * 3) + (pagesize / 2), dio_= align); =20 ksft_finished(); } --=20 2.53.0