Skip to contentSkip to conversation

mm/selftests: improve UFFD-WP feature detection in KSM test

PATCH In review Li Wang 4 participants 13 messages Jun 22, 2025 – Jun 25, 2025

Subsystems (4)
lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 mm/selftests: improve UFFD-WP feature detection in KSM test

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on kernels that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

  # running ./ksm_functional_tests
  # ------------------------------
  # TAP version 13
  # 1..9
  # # [RUN] test_unmerge
  # ok 1 Pages were unmerged
  # # [RUN] test_unmerge_zero_pages
  # ok 2 KSM zero pages were unmerged
  # # [RUN] test_unmerge_discarded
  # ok 3 Pages were unmerged
  # # [RUN] test_unmerge_uffd_wp
  # not ok 4 UFFDIO_API failed     <-----
  # # [RUN] test_prot_none
  # ok 5 Pages were unmerged
  # # [RUN] test_prctl
  # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
  # # [RUN] test_prctl_fork
  # # No pages got merged
  # # [RUN] test_prctl_fork_exec
  # ok 7 PR_SET_MEMORY_MERGE value is inherited
  # # [RUN] test_prctl_unmerge
  # ok 8 Pages were unmerged
  # Bail out! 1 out of 8 tests failed
  # # Planned tests != run tests (9 != 8)
  # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
  # [FAIL]

This patch improves compatibility and error handling by:

1. Changes the feature check to first query supported features (features=0) rather than specifically requesting WP support.

2. Gracefully skipping the test if:
- UFFDIO_API fails with EINVAL (feature not supported), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Providing better diagnostics by distinguishing expected failures (e.g., EINVAL) from unexpected ones and reporting them using strerror().

The updated logic makes the test more robust across different kernel versions and configurations, while preserving existing behavior on systems that do support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>
---
 tools/testing/selftests/mm/ksm_functional_tests.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +6 -2
index b61803e36d1c..f3db257dc555 100644
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
 	/* See if UFFD-WP is around. */
 	uffdio_api.api = UFFD_API;
-	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	uffdio_api.features = 0;
 	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		ksft_test_result_fail("UFFDIO_API failed\n");
+		if (errno == EINVAL)
+			ksft_test_result_skip("UFFDIO_API not supported (EINVAL)\n");
+		else
+			ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+
 		goto close_uffd;
 	}
 	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
Signature · 1 line
2.49.0
#1 lore raw
Quoted 74 lines · Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on kernels that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

   # running ./ksm_functional_tests
   # ------------------------------
   # TAP version 13
   # 1..9
   # # [RUN] test_unmerge
   # ok 1 Pages were unmerged
   # # [RUN] test_unmerge_zero_pages
   # ok 2 KSM zero pages were unmerged
   # # [RUN] test_unmerge_discarded
   # ok 3 Pages were unmerged
   # # [RUN] test_unmerge_uffd_wp
   # not ok 4 UFFDIO_API failed     <-----
   # # [RUN] test_prot_none
   # ok 5 Pages were unmerged
   # # [RUN] test_prctl
   # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
   # # [RUN] test_prctl_fork
   # # No pages got merged
   # # [RUN] test_prctl_fork_exec
   # ok 7 PR_SET_MEMORY_MERGE value is inherited
   # # [RUN] test_prctl_unmerge
   # ok 8 Pages were unmerged
   # Bail out! 1 out of 8 tests failed
   # # Planned tests != run tests (9 != 8)
   # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
   # [FAIL]

This patch improves compatibility and error handling by:

1. Changes the feature check to first query supported features (features=0) rather than specifically requesting WP support.

2. Gracefully skipping the test if:
- UFFDIO_API fails with EINVAL (feature not supported), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Providing better diagnostics by distinguishing expected failures (e.g., EINVAL) from unexpected ones and reporting them using strerror().

The updated logic makes the test more robust across different kernel versions and configurations, while preserving existing behavior on systems that do support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>
---
  tools/testing/selftests/mm/ksm_functional_tests.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +1 -1
index b61803e36d1c..f3db257dc555 100644
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
  	/* See if UFFD-WP is around. */
  	uffdio_api.api = UFFD_API;
-	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	uffdio_api.features = 0;

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 405

Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +5 -1
  	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		ksft_test_result_fail("UFFDIO_API failed\n");
+		if (errno == EINVAL)
+			ksft_test_result_skip("UFFDIO_API not supported (EINVAL)\n");
+		else
+			ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+
  		goto close_uffd;
  	}
  	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {

The man page (man UFFDIO_API) documents:

Since Linux 4.11, applications should use the features field to perform a two-step handshake. First, UFFDIO_API is called with the features field set to zero. The kernel responds by setting all supported feature bits.

Applications which do not require any specific features can begin using the userfaultfd immedi‐
ately. Applications which do need specific features should call UFFDIO_API again with a subset
of the reported feature bits set to enable those features.

So likely, what you want in this patch here is something like:

tools/testing/selftests/mm/ksm_functional_tests.c +9 -1
index b61803e36d1cf..5cf819ac958d0 100644
@@ -393,7 +393,7 @@ static void test_unmerge_uffd_wp(void)
 
        /* See if UFFD-WP is around. */
        uffdio_api.api = UFFD_API;
-       uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+       uffdio_api.features = 0;
        if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
                ksft_test_result_fail("UFFDIO_API failed\n");
                goto close_uffd;
@@ -403,6 +403,14 @@ static void test_unmerge_uffd_wp(void)
                goto close_uffd;
        }
 
+       /* Now, enable it ("two-step handshake") */
+       uffdio_api.api = UFFD_API;
+       uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+       if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
+               ksft_test_result_fail("UFFDIO_API failed\n");
+               goto close_uffd;
+       }
+
        /* Register UFFD-WP, no need for an actual handler. */
        if (uffd_register(uffd, map, size, false, true, false)) {
                ksft_test_result_fail("UFFDIO_REGISTER_MODE_WP failed\n");
Signature · 3 lines
Cheers,

David / dhildenb
author
#2 lore raw

Hi David,

Quoted 102 lines · David Hildenbrand
Quoted 3 lines · Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on

kernels

Quoted 30 lines

that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

   # running ./ksm_functional_tests
   # ------------------------------
   # TAP version 13
   # 1..9
   # # [RUN] test_unmerge
   # ok 1 Pages were unmerged
   # # [RUN] test_unmerge_zero_pages
   # ok 2 KSM zero pages were unmerged
   # # [RUN] test_unmerge_discarded
   # ok 3 Pages were unmerged
   # # [RUN] test_unmerge_uffd_wp
   # not ok 4 UFFDIO_API failed     <-----
   # # [RUN] test_prot_none
   # ok 5 Pages were unmerged
   # # [RUN] test_prctl
   # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
   # # [RUN] test_prctl_fork
   # # No pages got merged
   # # [RUN] test_prctl_fork_exec
   # ok 7 PR_SET_MEMORY_MERGE value is inherited
   # # [RUN] test_prctl_unmerge
   # ok 8 Pages were unmerged
   # Bail out! 1 out of 8 tests failed
   # # Planned tests != run tests (9 != 8)
   # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
   # [FAIL]
Quoted 3 lines

This patch improves compatibility and error handling by:

1. Changes the feature check to first query supported features

(features=0)

Quoted 7 lines

rather than specifically requesting WP support.

2. Gracefully skipping the test if:
- UFFDIO_API fails with EINVAL (feature not supported), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Providing better diagnostics by distinguishing expected failures

(e.g.,

Quoted 3 lines

EINVAL) from unexpected ones and reporting them using strerror().

The updated logic makes the test more robust across different kernel

versions

Quoted 14 lines

and configurations, while preserving existing behavior on systems that do support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>

Review

Quoted 5 lines
---
  tools/testing/selftests/mm/ksm_functional_tests.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/tools/testing/selftests/mm/ksm_functional_tests.c

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 399

Quoted 13 lines

index b61803e36d1c..f3db257dc555 100644

tools/testing/selftests/mm/ksm_functional_tests.c +3 -2
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
      /* See if UFFD-WP is around. */
      uffdio_api.api = UFFD_API;
-     uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+     uffdio_api.features = 0;
      if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-             ksft_test_result_fail("UFFDIO_API failed\n");
+             if (errno == EINVAL)
+                     ksft_test_result_skip("UFFDIO_API not supported

(EINVAL)\n");

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 401

Quoted 2 lines
tools/testing/selftests/mm/ksm_functional_tests.c +2 -0
+             else
+                     ksft_test_result_fail("UFFDIO_API failed: %s\n",

strerror(errno));

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void)

Quoted 4 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1 -0
+
              goto close_uffd;
      }
      if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {

The man page (man UFFDIO_API) documents:

Since Linux 4.11, applications should use the features field to
perform a two-step handshake.
First, UFFDIO_API is called with the features field set to zero.
The kernel responds by setting
all supported feature bits.

Quoted 6 lines

Applications which do not require any specific features can begin
using the userfaultfd immedi‐
ately. Applications which do need specific features should call
UFFDIO_API again with a subset
of the reported feature bits set to enable those features.

Thanks for pointing this out, very helpful.

Will send a patch v2 soon.

Quoted 40 lines

So likely, what you want in this patch here is something like:

tools/testing/selftests/mm/ksm_functional_tests.c +9 -2
index b61803e36d1cf..5cf819ac958d0 100644
@@ -393,7 +393,7 @@ static void test_unmerge_uffd_wp(void)
 
         /* See if UFFD-WP is around. */
         uffdio_api.api = UFFD_API;
-       uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+       uffdio_api.features = 0;
         if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
                 ksft_test_result_fail("UFFDIO_API failed\n");
                 goto close_uffd;
@@ -403,6 +403,14 @@ static void test_unmerge_uffd_wp(void)
                 goto close_uffd;
         }
 
+       /* Now, enable it ("two-step handshake") */
+       uffdio_api.api = UFFD_API;
+       uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+       if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
+               ksft_test_result_fail("UFFDIO_API failed\n");
+               goto close_uffd;
+       }
+
         /* Register UFFD-WP, no need for an actual handler. */
         if (uffd_register(uffd, map, size, false, true, false)) {
                 ksft_test_result_fail("UFFDIO_REGISTER_MODE_WP failed\n");
 
 
--

Cheers,

David / dhildenb

Signature · 2 lines
Regards,
Li Wang
author
#3 lore raw

PATCH v2 1/1 selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

In reply to Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on kernels that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

  # running ./ksm_functional_tests
  # ------------------------------
  # TAP version 13
  # 1..9
  # # [RUN] test_unmerge
  # ok 1 Pages were unmerged
  # # [RUN] test_unmerge_zero_pages
  # ok 2 KSM zero pages were unmerged
  # # [RUN] test_unmerge_discarded
  # ok 3 Pages were unmerged
  # # [RUN] test_unmerge_uffd_wp
  # not ok 4 UFFDIO_API failed     <-----
  # # [RUN] test_prot_none
  # ok 5 Pages were unmerged
  # # [RUN] test_prctl
  # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
  # # [RUN] test_prctl_fork
  # # No pages got merged
  # # [RUN] test_prctl_fork_exec
  # ok 7 PR_SET_MEMORY_MERGE value is inherited
  # # [RUN] test_prctl_unmerge
  # ok 8 Pages were unmerged
  # Bail out! 1 out of 8 tests failed
  # # Planned tests != run tests (9 != 8)
  # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
  # [FAIL]

This patch improves compatibility and robustness of the UFFD-WP test (test_unmerge_uffd_wp) by correctly implementing the UFFDIO_API two-step handshake as recommended by the userfaultfd(2) man page.

Key changes:

1. Use features=0 in the initial UFFDIO_API call to query supported feature bits, rather than immediately requesting WP support.

2. Skip the test gracefully if:
- UFFDIO_API fails with EINVAL (e.g. unsupported API version), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Close the initial userfaultfd and create a new one before enabling the required feature, since UFFDIO_API can only be called once per fd.

4. Improve diagnostics by distinguishing between expected and unexpected failures, using strerror() to report errors.

This ensures the test behaves correctly across a wider range of kernel versions and configurations, while preserving the intended behavior on kernels that support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>

---

Notes:
v1 --> v2:
* Close the original userfaultfd and open a new one before enabling features
* Reworked UFFDIO_API negotiation to follow the official two-step handshake

 .../selftests/mm/ksm_functional_tests.c       | 28 +++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +26 -2
index b61803e36d1c..19e5b741893a 100644
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
 	/* See if UFFD-WP is around. */
 	uffdio_api.api = UFFD_API;
-	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	uffdio_api.features = 0;
 	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		ksft_test_result_fail("UFFDIO_API failed\n");
+		if (errno == EINVAL)
+			ksft_test_result_skip("The API version requested is not supported\n");
+		else
+			ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+
 		goto close_uffd;
 	}
 	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
@@ -403,6 +407,26 @@ static void test_unmerge_uffd_wp(void)
 		goto close_uffd;
 	}
 
+	/*
+	 * UFFDIO_API must only be called once to enable features.
+	 * So we close the old userfaultfd and create a new one to
+	 * actually enable UFFD_FEATURE_PAGEFAULT_FLAG_WP.
+	 */
+	close(uffd);
+	uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+	if (uffd < 0) {
+		ksft_test_result_skip("__NR_userfaultfd failed\n");
+		goto unmap;
+	}
+
+	/* Now, enable it ("two-step handshake") */
+	uffdio_api.api = UFFD_API;
+	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
+		ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+		goto close_uffd;
+	}
+
 	/* Register UFFD-WP, no need for an actual handler. */
 	if (uffd_register(uffd, map, size, false, true, false)) {
 		ksft_test_result_fail("UFFDIO_REGISTER_MODE_WP failed\n");
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 84 lines · Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on kernels that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

   # running ./ksm_functional_tests
   # ------------------------------
   # TAP version 13
   # 1..9
   # # [RUN] test_unmerge
   # ok 1 Pages were unmerged
   # # [RUN] test_unmerge_zero_pages
   # ok 2 KSM zero pages were unmerged
   # # [RUN] test_unmerge_discarded
   # ok 3 Pages were unmerged
   # # [RUN] test_unmerge_uffd_wp
   # not ok 4 UFFDIO_API failed     <-----
   # # [RUN] test_prot_none
   # ok 5 Pages were unmerged
   # # [RUN] test_prctl
   # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
   # # [RUN] test_prctl_fork
   # # No pages got merged
   # # [RUN] test_prctl_fork_exec
   # ok 7 PR_SET_MEMORY_MERGE value is inherited
   # # [RUN] test_prctl_unmerge
   # ok 8 Pages were unmerged
   # Bail out! 1 out of 8 tests failed
   # # Planned tests != run tests (9 != 8)
   # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
   # [FAIL]

This patch improves compatibility and robustness of the UFFD-WP test (test_unmerge_uffd_wp) by correctly implementing the UFFDIO_API two-step handshake as recommended by the userfaultfd(2) man page.

Key changes:

1. Use features=0 in the initial UFFDIO_API call to query supported feature bits, rather than immediately requesting WP support.

2. Skip the test gracefully if:
- UFFDIO_API fails with EINVAL (e.g. unsupported API version), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Close the initial userfaultfd and create a new one before enabling the required feature, since UFFDIO_API can only be called once per fd.

4. Improve diagnostics by distinguishing between expected and unexpected failures, using strerror() to report errors.

This ensures the test behaves correctly across a wider range of kernel versions and configurations, while preserving the intended behavior on kernels that support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>

---

Notes:
v1 --> v2:
* Close the original userfaultfd and open a new one before enabling features
* Reworked UFFDIO_API negotiation to follow the official two-step handshake

  .../selftests/mm/ksm_functional_tests.c       | 28 +++++++++++++++++--
  1 file changed, 26 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +0 -0
index b61803e36d1c..19e5b741893a 100644
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
  	/* See if UFFD-WP is around. */

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 402

Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +6 -2
  	uffdio_api.api = UFFD_API;
-	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	uffdio_api.features = 0;
  	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		ksft_test_result_fail("UFFDIO_API failed\n");
+		if (errno == EINVAL)
+			ksft_test_result_skip("The API version requested is not supported\n");
+		else
+			ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+

Not sure if that is really required. If UFFDIO_API failed after __NR_userfaultfd worked something unexpected is happening.

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 415

Quoted 13 lines
tools/testing/selftests/mm/ksm_functional_tests.c +6 -0
  		goto close_uffd;
  	}
  	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
@@ -403,6 +407,26 @@ static void test_unmerge_uffd_wp(void)
  		goto close_uffd;
  	}
 
+	/*
+	 * UFFDIO_API must only be called once to enable features.
+	 * So we close the old userfaultfd and create a new one to
+	 * actually enable UFFD_FEATURE_PAGEFAULT_FLAG_WP.
+	 */
+	close(uffd);

Is that actually required?

The man page explicitly documents:

" EINVAL A previous UFFDIO_API call already enabled one or more features for this userfaultfd. Calling UFF‐
DIO_API twice, the first time with no features set, is
explicitly allowed as per the two-step feature
detection handshake.
"

So if that doesn't work, something might be broken.

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 117 lines · David Hildenbrand
Quoted 84 lines · Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on kernels that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------
# running ./ksm_functional_tests
# ------------------------------
# TAP version 13
# 1..9
# # [RUN] test_unmerge
# ok 1 Pages were unmerged
# # [RUN] test_unmerge_zero_pages
# ok 2 KSM zero pages were unmerged
# # [RUN] test_unmerge_discarded
# ok 3 Pages were unmerged
# # [RUN] test_unmerge_uffd_wp
# not ok 4 UFFDIO_API failed <-----
# # [RUN] test_prot_none
# ok 5 Pages were unmerged
# # [RUN] test_prctl
# ok 6 Setting/clearing PR_SET_MEMORY_MERGE works # # [RUN] test_prctl_fork
# # No pages got merged
# # [RUN] test_prctl_fork_exec
# ok 7 PR_SET_MEMORY_MERGE value is inherited # # [RUN] test_prctl_unmerge
# ok 8 Pages were unmerged
# Bail out! 1 out of 8 tests failed
# # Planned tests != run tests (9 != 8)
# # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0 # [FAIL]

This patch improves compatibility and robustness of the UFFD-WP test (test_unmerge_uffd_wp) by correctly implementing the UFFDIO_API two-step handshake as recommended by the userfaultfd(2) man page.

Key changes:

1. Use features=0 in the initial UFFDIO_API call to query supported feature bits, rather than immediately requesting WP support.

2. Skip the test gracefully if:
- UFFDIO_API fails with EINVAL (e.g. unsupported API version), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Close the initial userfaultfd and create a new one before enabling the required feature, since UFFDIO_API can only be called once per fd.

4. Improve diagnostics by distinguishing between expected and unexpected failures, using strerror() to report errors.

This ensures the test behaves correctly across a wider range of kernel versions and configurations, while preserving the intended behavior on kernels that support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>

---

Notes:
v1 --> v2:
* Close the original userfaultfd and open a new one before enabling features
* Reworked UFFDIO_API negotiation to follow the official two-step handshake

   .../selftests/mm/ksm_functional_tests.c       | 28 +++++++++++++++++--
   1 file changed, 26 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +0 -0
index b61803e36d1c..19e5b741893a 100644
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
   	/* See if UFFD-WP is around. */

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 402

Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +6 -2
   	uffdio_api.api = UFFD_API;
-	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	uffdio_api.features = 0;
   	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		ksft_test_result_fail("UFFDIO_API failed\n");
+		if (errno == EINVAL)
+			ksft_test_result_skip("The API version requested is not supported\n");
+		else
+			ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+

Not sure if that is really required. If UFFDIO_API failed after __NR_userfaultfd worked something unexpected is happening.

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 415

Quoted 13 lines
tools/testing/selftests/mm/ksm_functional_tests.c +6 -0
   		goto close_uffd;
   	}
   	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
@@ -403,6 +407,26 @@ static void test_unmerge_uffd_wp(void)
   		goto close_uffd;
   	}
 
+	/*
+	 * UFFDIO_API must only be called once to enable features.
+	 * So we close the old userfaultfd and create a new one to
+	 * actually enable UFFD_FEATURE_PAGEFAULT_FLAG_WP.
+	 */
+	close(uffd);

Is that actually required?

The man page explicitly documents:

Quoted 8 lines

" EINVAL A previous UFFDIO_API call already enabled one or more
features for this userfaultfd. Calling UFF‐
DIO_API twice, the first time with no features set, is
explicitly allowed as per the two-step feature
detection handshake.
"

So if that doesn't work, something might be broken.

CCing Nadav and Peter:

Could it be that

commit 22e5fe2a2a279d9a6fcbdfb4dffe73821bef1c90

Author: Nadav Amit <[email protected]>
Date:   Thu Sep 2 14:58:59 2021 -0700

userfaultfd: prevent concurrent API initialization

userfaultfd assumes that the enabled features are set once and never changed after UFFDIO_API ioctl succeeded.

However, currently, UFFDIO_API can be called concurrently from two different threads, succeed on both threads and leave userfaultfd's features in non-deterministic state. Theoretically, other uffd operations (ioctl's and page-faults) can be dispatched while adversely affected by such changes of features.

Moreover, the writes to ctx->state and ctx->features are not ordered, which can - theoretically, again - let userfaultfd_ioctl() think that userfaultfd API completed, while the features are still not initialized.

To avoid races, it is arguably best to get rid of ctx->state. Since there are only 2 states, record the API initialization in ctx->features as the uppermost bit and remove ctx->state.

Accidentally broke the documented two-step handshake in the man page where we can avoid closing + reopening the fd?

Without testing, the following might fix it if I am right:

fs/userfaultfd.c +6 -3
index 22f4bf956ba1c..f03e7c980e1c5 100644
@@ -1944,9 +1944,9 @@ static int userfaultfd_move(struct userfaultfd_ctx *ctx,
 static int userfaultfd_api(struct userfaultfd_ctx *ctx,
                           unsigned long arg)
 {
+       unsigned int new_features, old_features = 0;
        struct uffdio_api uffdio_api;
        void __user *buf = (void __user *)arg;
-       unsigned int ctx_features;
        int ret;
        __u64 features;
 
@@ -1990,9 +1990,12 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
                goto out;
 
        /* only enable the requested features for this uffd context */
-       ctx_features = uffd_ctx_features(features);
+       new_features = uffd_ctx_features(features);
+       /* allow two-step handshake */
+       if (userfaultfd_is_initialized(ctx))
+               old_features = UFFD_FEATURE_INITIALIZED;
        ret = -EINVAL;
-       if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
+       if (cmpxchg(&ctx->features, old_features, new_features) != old_features)
                goto err_out;
 
        ret = 0;
Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 36 lines · David Hildenbrand
Quoted 10 lines · David Hildenbrand
Quoted 1 line

Is that actually required?
The man page explicitly documents:
" EINVAL A previous UFFDIO_API call already enabled one or more
features for this userfaultfd. Calling UFF‐
DIO_API twice, the first time with no features set, is
explicitly allowed as per the two-step feature
detection handshake.
"
So if that doesn't work, something might be broken.

CCing Nadav and Peter:

Could it be that

commit 22e5fe2a2a279d9a6fcbdfb4dffe73821bef1c90

Author: Nadav Amit <[email protected]>
Date:   Thu Sep 2 14:58:59 2021 -0700

userfaultfd: prevent concurrent API initialization
userfaultfd assumes that the enabled features are set once and never
changed after UFFDIO_API ioctl succeeded.
However, currently, UFFDIO_API can be called concurrently from two
different threads, succeed on both threads and leave userfaultfd's features in non-deterministic state. Theoretically, other uffd operations (ioctl's and page-faults) can be dispatched while adversely affected by such changes of features.

       Moreover, the writes to ctx->state and ctx->features are not ordered,
   which can - theoretically, again - let userfaultfd_ioctl() think that
   userfaultfd API completed, while the features are still not initialized.
       To avoid races, it is arguably best to get rid of ctx->state.  Since there
Quoted 5 lines

are only 2 states, record the API initialization in ctx->features as the uppermost bit and remove ctx->state.

Accidentally broke the documented two-step handshake in the man page where we can avoid closing + reopening the fd?

I agree the code is not correct (and my patch didn’t address this issue), but I don’t see it broke it either.

Unless I’m missing something the code before my patch, when uffdio_api.features == 0, also set ctx->state to UFFD_STATE_RUNNING, which meant another invocation would see (ctx->state != UFFD_STATE_WAIT_API) and fail.

Quoted 20 lines

Without testing, the following might fix it if I am right:

fs/userfaultfd.c +1 -1
index 22f4bf956ba1c..f03e7c980e1c5 100644
@@ -1944,9 +1944,9 @@ static int userfaultfd_move(struct userfaultfd_ctx *ctx,
 static int userfaultfd_api(struct userfaultfd_ctx *ctx,
                          unsigned long arg)
 {
+       unsigned int new_features, old_features = 0;
       struct uffdio_api uffdio_api;
       void __user *buf = (void __user *)arg;
-       unsigned int ctx_features;
       int ret;
       __u64 features;
@@ -1990,9 +1990,12 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
               goto out;
        /* only enable the requested features for this uffd context */

fs/userfaultfd.c static int userfaultfd_api(struct userfaultfd_ctx *ctx, · line 1999

Quoted 10 lines
fs/userfaultfd.c +5 -2
-       ctx_features = uffd_ctx_features(features);
+       new_features = uffd_ctx_features(features);
+       /* allow two-step handshake */
+       if (userfaultfd_is_initialized(ctx))
+               old_features = UFFD_FEATURE_INITIALIZED;
       ret = -EINVAL;
-       if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
+       if (cmpxchg(&ctx->features, old_features, new_features) != old_features)
               goto err_out;
        ret = 0;

I am not sure it is right since you would return EINVAL in this case. It also looks a bit overly complicated - are you concerned about a race? My whole concern about race was that somebody would exploit it to overcome non-cooperative UFFD (IIRC).

So perhaps just add a check for the case features if 0 and be done with it? Something like adding:

	ret = 0;
	if (ctx->features == 0 && features == 0)
		goto err_out; 		/* no error but copying of uffdio_api required */

before enabling the requested features for this uffd context.

#7 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 42 lines · Nadav Amit
Quoted 39 lines · David Hildenbrand
Quoted 10 lines · David Hildenbrand
Quoted 1 line

Is that actually required?
The man page explicitly documents:
" EINVAL A previous UFFDIO_API call already enabled one or more
features for this userfaultfd. Calling UFF‐
DIO_API twice, the first time with no features set, is
explicitly allowed as per the two-step feature
detection handshake.
"
So if that doesn't work, something might be broken.

CCing Nadav and Peter:

Could it be that

commit 22e5fe2a2a279d9a6fcbdfb4dffe73821bef1c90

Author: Nadav Amit <[email protected]>
Date:   Thu Sep 2 14:58:59 2021 -0700

userfaultfd: prevent concurrent API initialization
userfaultfd assumes that the enabled features are set once and never
changed after UFFDIO_API ioctl succeeded.
However, currently, UFFDIO_API can be called concurrently from two
different threads, succeed on both threads and leave userfaultfd's features in non-deterministic state. Theoretically, other uffd operations (ioctl's and page-faults) can be dispatched while adversely affected by such changes of features.
Moreover, the writes to ctx->state and ctx->features are not ordered,
which can - theoretically, again - let userfaultfd_ioctl() think that userfaultfd API completed, while the features are still not initialized.
To avoid races, it is arguably best to get rid of ctx->state. Since there
are only 2 states, record the API initialization in ctx->features as the uppermost bit and remove ctx->state.

Quoted 2 lines

Accidentally broke the documented two-step handshake in the man page where we can avoid closing + reopening the fd?

Quoted 7 lines

I agree the code is not correct (and my patch didn’t address this issue), but I don’t see it broke it either.

Unless I’m missing something the code before my patch, when uffdio_api.features == 0, also set ctx->state to UFFD_STATE_RUNNING, which meant another invocation would see (ctx->state != UFFD_STATE_WAIT_API) and fail.

You might be right, I only checked the cmpxchg, assuming it was working before that.

... but staring at the history of the "ctx->state = UFFD_STATE_RUNNING;", I am not sure if it ever behaved that way.

Do maybe, the man page is simply wrong (although I wonder why that case was described that detailed)

Quoted 26 lines
Quoted 26 lines

Without testing, the following might fix it if I am right:

fs/userfaultfd.c +5 -2
index 22f4bf956ba1c..f03e7c980e1c5 100644
@@ -1944,9 +1944,9 @@ static int userfaultfd_move(struct userfaultfd_ctx *ctx,
 static int userfaultfd_api(struct userfaultfd_ctx *ctx,
                           unsigned long arg)
 {
+       unsigned int new_features, old_features = 0;
        struct uffdio_api uffdio_api;
        void __user *buf = (void __user *)arg;
-       unsigned int ctx_features;
        int ret;
        __u64 features;
@@ -1990,9 +1990,12 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
                goto out;
         /* only enable the requested features for this uffd context */
-       ctx_features = uffd_ctx_features(features);
+       new_features = uffd_ctx_features(features);
+       /* allow two-step handshake */
+       if (userfaultfd_is_initialized(ctx))
+               old_features = UFFD_FEATURE_INITIALIZED;
Quoted 8 lines

fs/userfaultfd.c static int userfaultfd_api(struct userfaultfd_ctx *ctx, · line 2001

Quoted 5 lines
fs/userfaultfd.c +1 -1
        ret = -EINVAL;
-       if (cmpxchg(&ctx->features, 0, ctx_features) != 0)
+       if (cmpxchg(&ctx->features, old_features, new_features) != old_features)
                goto err_out;
         ret = 0;

I am not sure it is right since you would return EINVAL in this case. It also looks a bit overly complicated - are you concerned about a race?

Yes.

Quoted 9 lines

My whole concern about race was that somebody would exploit it to overcome non-cooperative UFFD (IIRC).

So perhaps just add a check for the case features if 0 and be done with it? Something like adding:

	ret = 0;
	if (ctx->features == 0 && features == 0)
		goto err_out; 		/* no error but copying of uffdio_api required */

Probably would also work. But let's find out first if we even want to fix this, given that it never seemed to have behaved that way from a quick glimpse.

Signature · 3 lines
Cheers,

David / dhildenb
#8 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 51 lines · David Hildenbrand
Quoted 42 lines · Nadav Amit
Quoted 39 lines · David Hildenbrand
Quoted 10 lines · David Hildenbrand
Quoted 1 line

Is that actually required?
The man page explicitly documents:
" EINVAL A previous UFFDIO_API call already enabled one or more
features for this userfaultfd. Calling UFF‐
DIO_API twice, the first time with no features set, is
explicitly allowed as per the two-step feature
detection handshake.
"
So if that doesn't work, something might be broken.

CCing Nadav and Peter:

Could it be that

commit 22e5fe2a2a279d9a6fcbdfb4dffe73821bef1c90

Author: Nadav Amit <[email protected]>
Date:   Thu Sep 2 14:58:59 2021 -0700

userfaultfd: prevent concurrent API initialization
userfaultfd assumes that the enabled features are set once and never
changed after UFFDIO_API ioctl succeeded.
However, currently, UFFDIO_API can be called concurrently from two
different threads, succeed on both threads and leave userfaultfd's features in non-deterministic state. Theoretically, other uffd operations (ioctl's and page-faults) can be dispatched while adversely affected by such changes of features.
Moreover, the writes to ctx->state and ctx->features are not ordered,
which can - theoretically, again - let userfaultfd_ioctl() think that userfaultfd API completed, while the features are still not initialized.
To avoid races, it is arguably best to get rid of ctx->state. Since there
are only 2 states, record the API initialization in ctx->features as the uppermost bit and remove ctx->state.

Quoted 2 lines

Accidentally broke the documented two-step handshake in the man page where we can avoid closing + reopening the fd?

Quoted 7 lines

I agree the code is not correct (and my patch didn’t address this issue), but I don’t see it broke it either.

Unless I’m missing something the code before my patch, when uffdio_api.features == 0, also set ctx->state to UFFD_STATE_RUNNING, which meant another invocation would see (ctx->state != UFFD_STATE_WAIT_API) and fail.

Quoted 8 lines

You might be right, I only checked the cmpxchg, assuming it was working before that.

... but staring at the history of the "ctx->state = UFFD_STATE_RUNNING;", I am not sure if it ever behaved that way.

Do maybe, the man page is simply wrong (although I wonder why that case was described that detailed)

The man page was updated with

commit db3d5cc1a17b0ace008ebe1eaf0ac4d96b4b519a

Author: Axel Rasmussen <[email protected]>
Date:   Tue Oct 3 12:45:44 2023 -0700

ioctl_userfaultfd.2: Correct and update UFFDIO_API ioctl error codes

First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.

Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

@Axel, did you ignore the automatically-set UFFD_FEATURE_INITIALIZED and the repeated calls never worked, or was there actually a time where repeated UFFDIO_API calls would not result in EINVAL?

Signature · 3 lines
Cheers,

David / dhildenb
#9 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 16 lines · David Hildenbrand

The man page was updated with

commit db3d5cc1a17b0ace008ebe1eaf0ac4d96b4b519a

Author: Axel Rasmussen <[email protected]>
Date:   Tue Oct 3 12:45:44 2023 -0700

ioctl_userfaultfd.2: Correct and update UFFDIO_API ioctl error codes First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.
Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

Quoted 3 lines

@Axel, did you ignore the automatically-set UFFD_FEATURE_INITIALIZED and the repeated calls never worked, or was there actually a time where repeated UFFDIO_API calls would not result in EINVAL?

The man-pages was inaccurate before. It got updated recently after Kyle asking similar questions, see:

http://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?id=6a949e9b08fd1ad4e77584276b9566e45fc07a93

Li's v2 change on using the temp fd looks correct.

Thanks,

Signature · 1 line
Peter Xu
#10 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 17 lines · Peter Xu
Quoted 16 lines · David Hildenbrand

The man page was updated with

commit db3d5cc1a17b0ace008ebe1eaf0ac4d96b4b519a

Author: Axel Rasmussen <[email protected]>
Date:   Tue Oct 3 12:45:44 2023 -0700

ioctl_userfaultfd.2: Correct and update UFFDIO_API ioctl error codes First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.
Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

Quoted 6 lines
Quoted 3 lines

@Axel, did you ignore the automatically-set UFFD_FEATURE_INITIALIZED and the repeated calls never worked, or was there actually a time where repeated UFFDIO_API calls would not result in EINVAL?

The man-pages was inaccurate before. It got updated recently after Kyle asking similar questions, see:

Ah, great, thanks!

Signature · 3 lines
Cheers,

David / dhildenb
Acked-by on v2 1/1
#11 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

In reply to Li Wang
Quoted 100 lines · Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on kernels that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

   # running ./ksm_functional_tests
   # ------------------------------
   # TAP version 13
   # 1..9
   # # [RUN] test_unmerge
   # ok 1 Pages were unmerged
   # # [RUN] test_unmerge_zero_pages
   # ok 2 KSM zero pages were unmerged
   # # [RUN] test_unmerge_discarded
   # ok 3 Pages were unmerged
   # # [RUN] test_unmerge_uffd_wp
   # not ok 4 UFFDIO_API failed     <-----
   # # [RUN] test_prot_none
   # ok 5 Pages were unmerged
   # # [RUN] test_prctl
   # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
   # # [RUN] test_prctl_fork
   # # No pages got merged
   # # [RUN] test_prctl_fork_exec
   # ok 7 PR_SET_MEMORY_MERGE value is inherited
   # # [RUN] test_prctl_unmerge
   # ok 8 Pages were unmerged
   # Bail out! 1 out of 8 tests failed
   # # Planned tests != run tests (9 != 8)
   # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
   # [FAIL]

This patch improves compatibility and robustness of the UFFD-WP test (test_unmerge_uffd_wp) by correctly implementing the UFFDIO_API two-step handshake as recommended by the userfaultfd(2) man page.

Key changes:

1. Use features=0 in the initial UFFDIO_API call to query supported feature bits, rather than immediately requesting WP support.

2. Skip the test gracefully if:
- UFFDIO_API fails with EINVAL (e.g. unsupported API version), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Close the initial userfaultfd and create a new one before enabling the required feature, since UFFDIO_API can only be called once per fd.

4. Improve diagnostics by distinguishing between expected and unexpected failures, using strerror() to report errors.

This ensures the test behaves correctly across a wider range of kernel versions and configurations, while preserving the intended behavior on kernels that support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>

---

Notes:
v1 --> v2:
* Close the original userfaultfd and open a new one before enabling features
* Reworked UFFDIO_API negotiation to follow the official two-step handshake

  .../selftests/mm/ksm_functional_tests.c       | 28 +++++++++++++++++--
  1 file changed, 26 insertions(+), 2 deletions(-)
tools/testing/selftests/mm/ksm_functional_tests.c +6 -2
index b61803e36d1c..19e5b741893a 100644
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
  	/* See if UFFD-WP is around. */
  	uffdio_api.api = UFFD_API;
-	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+	uffdio_api.features = 0;
  	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		ksft_test_result_fail("UFFDIO_API failed\n");
+		if (errno == EINVAL)
+			ksft_test_result_skip("The API version requested is not supported\n");
+		else
+			ksft_test_result_fail("UFFDIO_API failed: %s\n", strerror(errno));
+
  		goto close_uffd;
  	}
  	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
@@ -403,6 +407,26 @@ static void test_unmerge_uffd_wp(void)
  		goto close_uffd;
  	}

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 418

Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +9 -0
 
+	/*
+	 * UFFDIO_API must only be called once to enable features.
+	 * So we close the old userfaultfd and create a new one to
+	 * actually enable UFFD_FEATURE_PAGEFAULT_FLAG_WP.
+	 */
+	close(uffd);
+	uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+	if (uffd < 0) {
+		ksft_test_result_skip("__NR_userfaultfd failed\n");

If it now suddenly fails (after it working above), this sure is a fail, right?

Apart from that

Signature · 3 lines
Cheers,

David / dhildenb
author
#12 lore raw

Re: [PATCH v2] selftests/mm: Fix UFFDIO_API usage with proper two-step feature negotiation

Quoted 109 lines · David Hildenbrand
Quoted 3 lines · Li Wang

The current implementation of test_unmerge_uffd_wp() explicitly sets uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP before calling UFFDIO_API. This can cause the ioctl() call to fail with EINVAL on

kernels

Quoted 39 lines

that do not support UFFD-WP, leading the test to fail unnecessarily:

# ------------------------------

   # running ./ksm_functional_tests
   # ------------------------------
   # TAP version 13
   # 1..9
   # # [RUN] test_unmerge
   # ok 1 Pages were unmerged
   # # [RUN] test_unmerge_zero_pages
   # ok 2 KSM zero pages were unmerged
   # # [RUN] test_unmerge_discarded
   # ok 3 Pages were unmerged
   # # [RUN] test_unmerge_uffd_wp
   # not ok 4 UFFDIO_API failed     <-----
   # # [RUN] test_prot_none
   # ok 5 Pages were unmerged
   # # [RUN] test_prctl
   # ok 6 Setting/clearing PR_SET_MEMORY_MERGE works
   # # [RUN] test_prctl_fork
   # # No pages got merged
   # # [RUN] test_prctl_fork_exec
   # ok 7 PR_SET_MEMORY_MERGE value is inherited
   # # [RUN] test_prctl_unmerge
   # ok 8 Pages were unmerged
   # Bail out! 1 out of 8 tests failed
   # # Planned tests != run tests (9 != 8)
   # # Totals: pass:7 fail:1 xfail:0 xpass:0 skip:0 error:0
   # [FAIL]

This patch improves compatibility and robustness of the UFFD-WP test (test_unmerge_uffd_wp) by correctly implementing the UFFDIO_API two-step handshake as recommended by the userfaultfd(2) man page.

Key changes:

1. Use features=0 in the initial UFFDIO_API call to query supported feature bits, rather than immediately requesting WP support.

Quoted 6 lines

2. Skip the test gracefully if:
- UFFDIO_API fails with EINVAL (e.g. unsupported API version), or
- UFFD_FEATURE_PAGEFAULT_FLAG_WP is not advertised by the kernel.

3. Close the initial userfaultfd and create a new one before enabling the required feature, since UFFDIO_API can only be called once per

fd.

Quoted 19 lines

4. Improve diagnostics by distinguishing between expected and unexpected failures, using strerror() to report errors.

This ensures the test behaves correctly across a wider range of kernel versions and configurations, while preserving the intended behavior on kernels that support UFFD-WP.

Cc Aruna Ramakrishna <[email protected]>
Cc Bagas Sanjaya <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Joey Gouly <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Keith Lucas <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Shuah Khan <[email protected]>
Quoted 5 lines

---

Notes:
v1 --> v2:
* Close the original userfaultfd and open a new one before

enabling features

Quoted 1 line

* Reworked UFFDIO_API negotiation to follow the official two-step

handshake

Review

Quoted 5 lines
  .../selftests/mm/ksm_functional_tests.c       | 28 +++++++++++++++++--
  1 file changed, 26 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/tools/testing/selftests/mm/ksm_functional_tests.c

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 399

Quoted 13 lines

index b61803e36d1c..19e5b741893a 100644

tools/testing/selftests/mm/ksm_functional_tests.c +3 -2
@@ -393,9 +393,13 @@ static void test_unmerge_uffd_wp(void)
 
      /* See if UFFD-WP is around. */
      uffdio_api.api = UFFD_API;
-     uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+     uffdio_api.features = 0;
      if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-             ksft_test_result_fail("UFFDIO_API failed\n");
+             if (errno == EINVAL)
+                     ksft_test_result_skip("The API version requested

is not supported\n");

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 401

Quoted 2 lines
tools/testing/selftests/mm/ksm_functional_tests.c +2 -0
+             else
+                     ksft_test_result_fail("UFFDIO_API failed: %s\n",

strerror(errno));

Quoted 8 lines
tools/testing/selftests/mm/ksm_functional_tests.c +1 -0
+
              goto close_uffd;
      }
      if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
@@ -403,6 +407,26 @@ static void test_unmerge_uffd_wp(void)
              goto close_uffd;
      }
 
Quoted 13 lines

tools/testing/selftests/mm/ksm_functional_tests.c static void test_unmerge_uffd_wp(void) · line 418

Quoted 9 lines
tools/testing/selftests/mm/ksm_functional_tests.c +9 -0
+     /*
+      * UFFDIO_API must only be called once to enable features.
+      * So we close the old userfaultfd and create a new one to
+      * actually enable UFFD_FEATURE_PAGEFAULT_FLAG_WP.
+      */
+     close(uffd);
+     uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+     if (uffd < 0) {
+             ksft_test_result_skip("__NR_userfaultfd failed\n");

If it now suddenly fails (after it working above), this sure is a fail, right?

Yes, you're right, here should ksft_test_result_fail().

Quoted 5 lines

Apart from that

Thanks for the review!

Signature · 2 lines
Regards,
Li Wang