Skip to contentSkip to conversation

userfaultfd05: handle kernels rejecting WP feature in UFFDIO_API

PATCH In review Li Wang via ltp 3 participants 8 messages Jan 23 – Jan 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 userfaultfd05: handle kernels rejecting WP feature in UFFDIO_API

Commit 485a4cd2ba3 ("userfaultfd05: allow TCONF when UFFD-WP is unsupported") added a TCONF path for missing UFFD-WP, but it relied on checking uffdio_api.features after a failed ioctl (on RHEL-10).

That is not sufficient: it did not handle the case where UFFDIO_API succeeds but the kernel does not advertise UFFD_FEATURE_PAGEFAULT_FLAG_WP in the returned features mask.

So userfaultfd05 still fails on RHEL-9 s390x platform:

    userfaultfd05.c:106: TBROK: ioctl(3,((((2U|1U) << (((0+8)+8)+14)) |
		    (((0xAA)) << (0+8)) | ((((0x00))) << 0) |
		    ((((sizeof(struct uffdio_register)))) << ((0+8)+8)))),...)
		    failed: EINVAL (22)

Now, let's handle both behaviours by retrying UFFDIO_API with features=0 on EINVAL and treating a successful retry as "WP unsupported" (TCONF). Also check the returned features mask after a successful UFFDIO_API and skip when WP is not advertised.

And preserve the original errno so real UFFDIO_API failures are still reported as 'TBROK | TERRNO'.

Follow-up: 485a4cd2ba3 ("userfaultfd05: allow TCONF when UFFD-WP is unsupported")

Cc Ricardo Branco <[email protected]>
---
 .../kernel/syscalls/userfaultfd/userfaultfd05.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +14 -3
index 6cae45f20..4158b7b46 100644
@@ -92,13 +92,24 @@ static void run(void)
 
 	uffdio_api.api = UFFD_API;
 	uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+
 	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP))
-			tst_brk(TCONF, "UFFD write-protect unsupported");
+		int err = errno;
+		if (err == EINVAL) {
+			uffdio_api.api = UFFD_API;
+			uffdio_api.features = 0;
+
+			if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0)
+				tst_brk(TCONF, "UFFD write-protect unsupported");
+		}
 
-		tst_brk(TBROK | TERRNO, "ioctl() on userfaultfd failed");
+		errno = err;
+		tst_brk(TBROK | TERRNO, "ioctl(UFFDIO_API) failed");
 	}
 
+	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP))
+		tst_brk(TCONF, "UFFD write-protect unsupported");
+
 	uffdio_register.range.start = (unsigned long) page;
 	uffdio_register.range.len = page_size;
 	uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hi Li, Ricardo,

Quoted 19 lines

Commit 485a4cd2ba3 ("userfaultfd05: allow TCONF when UFFD-WP is unsupported") added a TCONF path for missing UFFD-WP, but it relied on checking uffdio_api.features after a failed ioctl (on RHEL-10).

That is not sufficient: it did not handle the case where UFFDIO_API succeeds but the kernel does not advertise UFFD_FEATURE_PAGEFAULT_FLAG_WP in the returned features mask.

So userfaultfd05 still fails on RHEL-9 s390x platform:

    userfaultfd05.c:106: TBROK: ioctl(3,((((2U|1U) << (((0+8)+8)+14)) |
		    (((0xAA)) << (0+8)) | ((((0x00))) << 0) |
		    ((((sizeof(struct uffdio_register)))) << ((0+8)+8)))),...)
		    failed: EINVAL (22)

Now, let's handle both behaviours by retrying UFFDIO_API with features=0 on EINVAL and treating a successful retry as "WP unsupported" (TCONF). Also check the returned features mask after a successful UFFDIO_API and skip when WP is not advertised.

...

testcases/kernel/syscalls/userfaultfd/userfaultfd05.c static void run(void) · line 104

Quoted 11 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +8 -2
 	if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
-		if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP))
-			tst_brk(TCONF, "UFFD write-protect unsupported");
+		int err = errno;
+		if (err == EINVAL) {
+			uffdio_api.api = UFFD_API;
+			uffdio_api.features = 0;
+
+			if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0)
+				tst_brk(TCONF, "UFFD write-protect unsupported");
+		}

Wouldn't be better in this case to check kconfig for CONFIG_HAVE_ARCH_USERFAULTFD_WP (untested, but it should work

Back to our discussion about how often using kconfig [1]. While I prefer to avoid using it for tristate (kernel might be configured but module missing), but here is just a feature.

[1] https://lore.kernel.org/ltp/CAASaF6wOSvi+07Pq5O6+f1Hkrq6WWMgpCaooJxWrO9uOvRM3pw@mail.gmail.com/

But I'm ok with it if you prefer exact check.

testcases/kernel/syscalls/userfaultfd/userfaultfd05.c static void run(void) · line 107

Quoted 3 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +2 -1
-		tst_brk(TBROK | TERRNO, "ioctl() on userfaultfd failed");
+		errno = err;
+		tst_brk(TBROK | TERRNO, "ioctl(UFFDIO_API) failed");

+1

Quoted 1 line

}

Kind regards,
Petr

Quoted 6 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +3 -0
+	if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP))
+		tst_brk(TCONF, "UFFD write-protect unsupported");
+
 	uffdio_register.range.start = (unsigned long) page;
 	uffdio_register.range.len = page_size;
 	uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 12 lines

testcases/kernel/syscalls/userfaultfd/userfaultfd05.c static void run(void) · line 104

Quoted 11 lines
      if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +8 -2
-             if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP))
-                     tst_brk(TCONF, "UFFD write-protect unsupported");
+             int err = errno;
+             if (err == EINVAL) {
+                     uffdio_api.api = UFFD_API;
+                     uffdio_api.features = 0;
+
+                     if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0)
+                             tst_brk(TCONF, "UFFD write-protect unsupported");
+             }
Quoted 2 lines

Wouldn't be better in this case to check kconfig for CONFIG_HAVE_ARCH_USERFAULTFD_WP (untested, but it should work

That's true, it would be simpler, let's go with this method.

Quoted 5 lines

Back to our discussion about how often using kconfig [1]. While I prefer to avoid using it for tristate (kernel might be configured but module missing), but here is just a feature.

[1] https://lore.kernel.org/ltp/CAASaF6wOSvi+07Pq5O6+f1Hkrq6WWMgpCaooJxWrO9uOvRM3pw@mail.gmail.com/

I don’t think there is a single “standard” answer for feature detection; it really depends on the specific situation.

For the UFFD-WP feature the situation is: there isn’t really a boot parameter or runtime knob that disables UFFD-WP independently once the interface is present. Given that, a simpler approach is to rely on Kconfig checks.

This is especially relevant here because different kernels report “WP unsupported” differently (e.g. return -1/EINVAL vs return 0 with a different feature mask), which makes runtime-based detection more complicated.

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 31 lines
Quoted 12 lines

testcases/kernel/syscalls/userfaultfd/userfaultfd05.c static void run(void) · line 104

Quoted 11 lines
      if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) {
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +8 -2
-             if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP))
-                     tst_brk(TCONF, "UFFD write-protect unsupported");
+             int err = errno;
+             if (err == EINVAL) {
+                     uffdio_api.api = UFFD_API;
+                     uffdio_api.features = 0;
+
+                     if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0)
+                             tst_brk(TCONF, "UFFD write-protect unsupported");
+             }
Quoted 2 lines

Wouldn't be better in this case to check kconfig for CONFIG_HAVE_ARCH_USERFAULTFD_WP (untested, but it should work

That's true, it would be simpler, let's go with this method.

Quoted 5 lines

Back to our discussion about how often using kconfig [1]. While I prefer to avoid using it for tristate (kernel might be configured but module missing), but here is just a feature.

[1] https://lore.kernel.org/ltp/CAASaF6wOSvi+07Pq5O6+f1Hkrq6WWMgpCaooJxWrO9uOvRM3pw@mail.gmail.com/

I don’t think there is a single “standard” answer for feature detection; it really depends on the specific situation.

For the UFFD-WP feature the situation is: there isn’t really a boot parameter or runtime knob that disables UFFD-WP independently once the interface is present. Given that, a simpler approach is to rely on Kconfig checks.

Quoted 4 lines

This is especially relevant here because different kernels report “WP unsupported” differently (e.g. return -1/EINVAL vs return 0 with a different feature mask), which makes runtime-based detection more complicated.

Yes, for the discussion when to use I'd propose to *not* use kconfig if:
* boot parameter to enable/disable exist
* check for tristate (functionality which can be compiled as module)
* kernel new functionality which is unlikely to be backported (use .min_kver instead)

Unless some objection, I'll post a patch for ground rules.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 5 lines

Yes, for the discussion when to use I'd propose to *not* use kconfig if:

* boot parameter to enable/disable exist
* check for tristate (functionality which can be compiled as module)
* kernel new functionality which is unlikely to be backported (use .min_kver instead)

That sounds great to me.

And, plus one more:
* kconfig file may be unavailable for some reasons

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 1 line
Quoted 1 line

Yes, for the discussion when to use I'd propose to *not* use kconfig

Maybe to correct myself:

*Use* kconfig if there is no other way to detect the functionality [1]. We prefer to use kconfig detection, but do *not* use kconfig when there is another way to detect the functionality (e.g. via detecting functionality via /proc|sys) *and* and one of these three rules:

Quoted 1 line
Quoted 1 line

* boot parameter to enable/disable exist

* allow to disable via kernel boot parameter or via /sys file => because it can be disabled

Quoted 1 line
Quoted 1 line

* check for tristate (functionality which can be compiled as module)

=> modul might not be available

Quoted 1 line
Quoted 1 line

* kernel new functionality which is unlikely to be backported (use .min_kver instead)

=> probably faster

Quoted 1 line

That sounds great to me.

Thank you!

Quoted 2 lines

And, plus one more:
* kconfig file may be unavailable for some reasons

Yes, but we gave up on this (or at least Cyril) [1]:

As for the missing config there is 95 testcases that have needs_kconfigs set at this moment and the number is growing steadily. I would argue that you cannot run LTP without having config available. And the config location is autodetected on common distributions as well.

me: + at least 2 IMA tests require kconfig via tst_require_kconfigs().

Therefore I accepted it and I'm not against using kconfig. But I would prefer using it only when it works reliably (100%).

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/[email protected]/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
Quoted 35 lines · Petr Vorel
Quoted 1 line
Quoted 1 line

Yes, for the discussion when to use I'd propose to *not* use kconfig

Maybe to correct myself:

*Use* kconfig if there is no other way to detect the functionality [1]. We prefer to use kconfig detection, but do *not* use kconfig when there is another way to detect the functionality (e.g. via detecting functionality via
/proc|sys) *and* and one of these three rules:

Quoted 1 line
Quoted 1 line

* boot parameter to enable/disable exist

* allow to disable via kernel boot parameter or via /sys file => because it can be disabled

Quoted 1 line
Quoted 1 line

* check for tristate (functionality which can be compiled as module)

=> modul might not be available

Quoted 1 line
Quoted 1 line

* kernel new functionality which is unlikely to be backported (use

.min_kver instead)
=> probably faster

Quoted 1 line

That sounds great to me.

Thank you!

Quoted 2 lines

And, plus one more:
* kconfig file may be unavailable for some reasons

Yes, but we gave up on this (or at least Cyril) [1]:

As for the missing config there is 95 testcases that have
needs_kconfigs
set at this moment and the number is growing steadily. I would
argue
that you cannot run LTP without having config available. And the
config
location is autodetected on common distributions as well.

Quoted 6 lines

me: + at least 2 IMA tests require kconfig via tst_require_kconfigs().

Therefore I accepted it and I'm not against using kconfig. But I would prefer
using it only when it works reliably (100%).

Make sense!

Quoted 6 lines

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/[email protected]/

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp