selftests/mm: uffd-stress fixes
Subsystems (5)
cover selftests/mm: uffd-stress fixes
This patchset ensures that the number of hugepages is correctly set in the system so that the uffd-stress test does not fail due to the racy nature of the test. Patch 1 corrects the hugepage constraint in the run_vmtests.sh script, whereas patch 2 corrects the constraint in the test itself.
Dev Jain (2):
selftests/mm/uffd-stress: Make test operate on less hugetlb memory selftests/mm/uffd-stress: Stricten constraint on free hugepages before
the test
tools/testing/selftests/mm/run_vmtests.sh | 2 +- tools/testing/selftests/mm/uffd-stress.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.30.2
PATCH 1/2 selftests/mm/uffd-stress: Make test operate on less hugetlb memory
We observed uffd-stress selftest failure on arm64 and intermittent failures on x86 too:
running ./uffd-stress hugetlb-private 128 32
bounces: 17, mode: rnd read, ERROR: UFFDIO_COPY error: -12 (errno=12, @uffd-common.c:617) [FAIL] not ok 18 uffd-stress hugetlb-private 128 32 # exit=1
For this particular case, the number of free hugepages from run_vmtests.sh will be 128, and the test will allocate 64 hugepages in the source location. The stress() function will start spawning threads which will operate on the destination location, triggering uffd-operations like UFFDIO_COPY from src to dst, which means that we will require 64 more hugepages for the dst location.
Let us observe the locking_thread() function. It will lock the mutex kept at dst, triggering uffd-copy. Suppose that 127 (64 for src and 63 for dst) hugepages have been reserved. In case of BOUNCE_RANDOM, it may happen that two threads trying to lock the mutex at dst, try to do so at the same hugepage number. If one thread succeeds in reserving the last hugepage, then the other thread may fail in alloc_hugetlb_folio(), returning -ENOMEM. I can confirm that this is indeed the case by this hacky patch:
mm/hugetlb.c +5 -0
@@ -6929,6 +6929,11 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, folio =alloc_hugetlb_folio (dst_vma, dst_addr, false);if (IS_ERR (folio)) { +pte_t *actual_pte =hugetlb_walk (dst_vma, dst_addr,PMD_SIZE ); +if (actual_pte) { + ret = -EEXIST ; + goto out; + } ret = -ENOMEM ; goto out; }
This code path gets triggered indicating that the PMD at which one thread is trying to map a hugepage, gets filled by a racing thread.
Therefore, instead of using freepgs to compute the amount of memory, use freepgs - 10, so that the test still has some extra hugepages to use. Note that, in case this value underflows, there is a check for the number of free hugepages in the test itself, which will fail, so we are safe.
--- tools/testing/selftests/mm/run_vmtests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/run_vmtests.sh +1 -1
@@ -326,7 +326,7 @@ CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 # the size of the free pages we have, which is used for *each*. # uffd-stress expects a region expressed in MiB, so we adjust # half_ufd_size_MB accordingly. -half_ufd_size_MB=$(((freepgs * hpgsize_KB) / 1024 / 2)) +half_ufd_size_MB=$((((freepgs - 10) * hpgsize_KB) / 1024 / 2)) CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16
Signature · 1 line
2.30.2
Re: [PATCH 1/2] selftests/mm/uffd-stress: Make test operate on less hugetlb memory
Quoted 57 lines · Dev Jain
We observed uffd-stress selftest failure on arm64 and intermittent failures on x86 too:
running ./uffd-stress hugetlb-private 128 32bounces: 17, mode: rnd read, ERROR:
UFFDIO_COPYerror: -12 (errno=12, @uffd-common.c:617) [FAIL] not ok 18 uffd-stress hugetlb-private 128 32 # exit=1For this particular case, the number of free hugepages from
run_vmtests.shwill be 128, and the test will allocate 64 hugepages in the source location. Thestress()function will start spawning threads which will operate on the destination location, triggering uffd-operations likeUFFDIO_COPYfrom src to dst, which means that we will require 64 more hugepages for the dst location.Let us observe the
locking_thread()function. It will lock the mutex kept at dst, triggering uffd-copy. Suppose that 127 (64 for src and 63 for dst) hugepages have been reserved. In case ofBOUNCE_RANDOM, it may happen that two threads trying to lock the mutex at dst, try to do so at the same hugepage number. If one thread succeeds in reserving the last hugepage, then the other thread may fail inalloc_hugetlb_folio(), returning-ENOMEM. I can confirm that this is indeed the case by this hacky patch:mm/hugetlb.c +5
-0@@ -6929,6 +6929,11 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, folio =alloc_hugetlb_folio (dst_vma, dst_addr, false);if (IS_ERR (folio)) { +pte_t *actual_pte =hugetlb_walk (dst_vma, dst_addr,PMD_SIZE ); +if (actual_pte) { + ret = -EEXIST ; + goto out; + } ret = -ENOMEM ; goto out; }This code path gets triggered indicating that the PMD at which one thread is trying to map a hugepage, gets filled by a racing thread.
Therefore, instead of using freepgs to compute the amount of memory, use freepgs - 10, so that the test still has some extra hugepages to use. Note that, in case this value underflows, there is a check for the number of free hugepages in the test itself, which will fail, so we are safe.
Signed-off-by Dev Jain <[email protected]>--- tools/testing/selftests/mm/run_vmtests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/run_vmtests.sh +0
-0
tools/testing/selftests/mm/run_vmtests.sh CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 · line 329
Quoted 5 lines
tools/testing/selftests/mm/run_vmtests.sh +1
-1@@ -326,7 +326,7 @@ CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 # the size of the free pages we have, which is used for *each*. # uffd-stress expects a region expressed in MiB, so we adjust # half_ufd_size_MB accordingly. -half_ufd_size_MB=$(((freepgs * hpgsize_KB) / 1024 / 2)) +half_ufd_size_MB=$((((freepgs - 10) * hpgsize_KB) / 1024 / 2))
Why 10? I don't know much about uffd-stress but the comment at the top says it runs 3 threads per CPU, so does the number of potential races increase with the number of CPUs? Perhaps this number needs to be a function of nrcpu?
I tested it and it works though so:
Quoted 3 lines
tools/testing/selftests/mm/run_vmtests.sh +0
-0CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16
Re: [PATCH 1/2] selftests/mm/uffd-stress: Make test operate on less hugetlb memory
Quoted 53 lines · Ryan Roberts
Quoted 52 lines · Dev Jain
We observed uffd-stress selftest failure on arm64 and intermittent failures on x86 too:
running ./uffd-stress hugetlb-private 128 32bounces: 17, mode: rnd read, ERROR:
UFFDIO_COPYerror: -12 (errno=12, @uffd-common.c:617) [FAIL] not ok 18 uffd-stress hugetlb-private 128 32 # exit=1For this particular case, the number of free hugepages from
run_vmtests.shwill be 128, and the test will allocate 64 hugepages in the source location. Thestress()function will start spawning threads which will operate on the destination location, triggering uffd-operations likeUFFDIO_COPYfrom src to dst, which means that we will require 64 more hugepages for the dst location.Let us observe the
locking_thread()function. It will lock the mutex kept at dst, triggering uffd-copy. Suppose that 127 (64 for src and 63 for dst) hugepages have been reserved. In case ofBOUNCE_RANDOM, it may happen that two threads trying to lock the mutex at dst, try to do so at the same hugepage number. If one thread succeeds in reserving the last hugepage, then the other thread may fail inalloc_hugetlb_folio(), returning-ENOMEM. I can confirm that this is indeed the case by this hacky patch:mm/hugetlb.c +5
-0@@ -6929,6 +6929,11 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, folio =alloc_hugetlb_folio (dst_vma, dst_addr, false);if (IS_ERR (folio)) { +pte_t *actual_pte =hugetlb_walk (dst_vma, dst_addr,PMD_SIZE ); +if (actual_pte) { + ret = -EEXIST ; + goto out; + } ret = -ENOMEM ; goto out; }This code path gets triggered indicating that the PMD at which one thread is trying to map a hugepage, gets filled by a racing thread.
Therefore, instead of using freepgs to compute the amount of memory, use freepgs - 10, so that the test still has some extra hugepages to use. Note that, in case this value underflows, there is a check for the number of free hugepages in the test itself, which will fail, so we are safe.
Signed-off-by Dev Jain <[email protected]>--- tools/testing/selftests/mm/run_vmtests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
tools/testing/selftests/mm/run_vmtests.sh CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 · line 329
Quoted 10 lines
tools/testing/selftests/mm/run_vmtests.sh +1
-1@@ -326,7 +326,7 @@ CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 # the size of the free pages we have, which is used for *each*. # uffd-stress expects a region expressed in MiB, so we adjust # half_ufd_size_MB accordingly. -half_ufd_size_MB=$(((freepgs * hpgsize_KB) / 1024 / 2)) +half_ufd_size_MB=$((((freepgs - 10) * hpgsize_KB) / 1024 / 2))Why 10? I don't know much about uffd-stress but the comment at the top says it runs 3 threads per CPU, so does the number of potential races increase with the number of CPUs? Perhaps this number needs to be a function of nrcpu?
Yes the race will amplify with nrcpus, technically we need nr_cpus - 1 extra hugepages - the worst case is that all threads will try to perform uffd-copy on the same address, one of them will reserve the last hugepage and others will fail. 10 was just a random number; I see that run_vmtests.sh already has nr_cpus computed so I can easily reuse that. I'll send a v2.
Quoted 4 lines
I tested it and it works though so:
Tested-by Ryan Roberts <[email protected]>
Thanks.
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/mm/run_vmtests.sh +0
-0CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16
PATCH 2/2 selftests/mm/uffd-stress: Stricten constraint on free hugepages before the test
In reply to Dev JainThe test requires at least 2 * (bytes/page_size) hugetlb memory, since we require identical number of hugepages for src and dst location. Fix this.
Along with the above, as explained in patch "selftests/mm/uffd-stress:
Make test operate on less hugetlb memory", the racy nature of the test requires that we have some extra number of hugepages left beyond what is required. Therefore, stricten this constraint.
--- tools/testing/selftests/mm/uffd-stress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/uffd-stress.c +1 -1
@@ -449,7 +449,7 @@ int main(int argc, char **argv) bytes =atol (argv[2 ]) *1024 *1024 ;if (test_type ==TEST_HUGETLB && -get_free_hugepages () < bytes / page_size) { +get_free_hugepages () <2 * (bytes / page_size) +10 ) {printf ("skip: Skipping userfaultfd... not enough hugepages\n" );return KSFT_SKIP ; }
Signature · 1 line
2.30.2
Quoted 5 lines · Dev Jain
This patchset ensures that the number of hugepages is correctly set in the system so that the uffd-stress test does not fail due to the racy nature of the test. Patch 1 corrects the hugepage constraint in the
run_vmtests.shscript, whereas patch 2 corrects the constraint in the test itself.
Quoted 8 lines
Dev Jain (2):
selftests/mm/uffd-stress: Make test operate on less hugetlb memory selftests/mm/uffd-stress: Stricten constraint on free hugepages before
the testtools/testing/selftests/mm/run_vmtests.sh | 2 +- tools/testing/selftests/mm/uffd-stress.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Hi Andrew,
I wanted to make a v2 of this series to replace 10 with min(32, nrcpus - 1) (see computation of nr_parallel in uffd-stress.c) but I see that it has been pulled into mm-new, and on top of that, the following patch makes things complicated to just revert my commits in mm-new and make v2 on top of that -
https://lore.kernel.org/all/[email protected]/
So shall I just send a new separate patch based off mm-new?
Andrew Morton
maintainerQuoted 12 lines · On Mon, 8 Sep 2025 13:52:05 +0530 Dev Jain <[email protected]> wrote:
Quoted 10 lines · Dev Jain
This patchset ensures that the number of hugepages is correctly set in the system so that the uffd-stress test does not fail due to the racy nature of the test. Patch 1 corrects the hugepage constraint in the
run_vmtests.shscript, whereas patch 2 corrects the constraint in the test itself.Dev Jain (2):
selftests/mm/uffd-stress: Make test operate on less hugetlb memory selftests/mm/uffd-stress: Stricten constraint on free hugepages before
the test
Quoted 7 lines
Quoted 3 lines
tools/testing/selftests/mm/run_vmtests.sh | 2 +- tools/testing/selftests/mm/uffd-stress.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)Hi Andrew,
I wanted to make a v2 of this series to replace 10 with
min(32, nrcpus - 1)
That sounds line a one-line change? Send a one-line change ;)
Quoted 6 lines
(see computation of
nr_parallelinuffd-stress.c) but I see that it has been pulled into mm-new, and on top of that, the following patch makes things complicated to just revert my commits in mm-new and make v2 on top of that -
https://lore.kernel.org/all/[email protected]/So shall I just send a new separate patch based off mm-new?
Or just resend the whole series based on mainline or something?
Quoted 21 lines · Andrew Morton
Quoted 11 lines · On Mon, 8 Sep 2025 13:52:05 +0530 Dev Jain <[email protected]> wrote:
Quoted 10 lines · Dev Jain
This patchset ensures that the number of hugepages is correctly set in the system so that the uffd-stress test does not fail due to the racy nature of the test. Patch 1 corrects the hugepage constraint in the
run_vmtests.shscript, whereas patch 2 corrects the constraint in the test itself.Dev Jain (2):
selftests/mm/uffd-stress: Make test operate on less hugetlb memory selftests/mm/uffd-stress: Stricten constraint on free hugepages before
the testQuoted 6 lines
Quoted 3 lines
tools/testing/selftests/mm/run_vmtests.sh | 2 +- tools/testing/selftests/mm/uffd-stress.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)Hi Andrew,
I wanted to make a v2 of this series to replace 10 with
min(32, nrcpus - 1)That sounds line a one-line change? Send a one-line change ;)
Quoted 7 lines
Quoted 6 lines
(see computation of
nr_parallelinuffd-stress.c) but I see that it has been pulled into mm-new, and on top of that, the following patch makes things complicated to just revert my commits in mm-new and make v2 on top of that -
https://lore.kernel.org/all/[email protected]/So shall I just send a new separate patch based off mm-new?
Or just resend the whole series based on mainline or something?
I'll do this, since the change will be more than a line. Thanks.