userfaultfd: Use two-step handshake to probe features
PATCH 1/1 userfaultfd: Use two-step handshake to probe features
userfaultfd05 and userfaultfd06 assume that UFFD_FEATURE_PAGEFAULT_FLAG_WP and UFFD_FEATURE_POISON are available when setting up userfaultfd.
On kernels where either feature is unavailable at runtime, UFFDIO_API fails with EINVAL and the tests end up as TBROK instead of being skipped.
Fix this by using the required two-step handshake and skip with TCONF if the required feature bit is absent.
Also remove userfaultfd05's needs_kconfigs check, since runtime feature probing is sufficient here.
--- testcases/kernel/syscalls/userfaultfd/userfaultfd05.c | 11 +++++++---- testcases/kernel/syscalls/userfaultfd/userfaultfd06.c | 10 +++++++--- 2 files changed, 14 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +7 -4
@@ -89,7 +89,14 @@ static void run(void)set_pages (); uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); + uffdio_api.api =UFFD_API ; + uffdio_api.features =0 ; +SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_PAGEFAULT_FLAG_WP )) +tst_brk (TCONF ,"UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported" ); +SAFE_CLOSE (uffd); + uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); uffdio_api.api =UFFD_API ; uffdio_api.features =UFFD_FEATURE_PAGEFAULT_FLAG_WP ;
@@ -124,8 +131,4 @@ static void run(void)static struct tst_test test = { .test_all = run, .min_kver ="5.7" , - .needs_kconfigs = (const char *[]) { -"CONFIG_HAVE_ARCH_USERFAULTFD_WP=y" , -NULL - } };
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +7 -3
@@ -100,15 +100,19 @@ static void run(void)set_pages (); uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); + uffdio_api.api =UFFD_API ; + uffdio_api.features =0 ; +SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_POISON )) +tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); +SAFE_CLOSE (uffd); + uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); uffdio_api.api =UFFD_API ; uffdio_api.features =UFFD_FEATURE_POISON ;SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); -if (!(uffdio_api.features &UFFD_FEATURE_POISON )) -tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); - uffdio_register.range.start = (unsigned long ) page; uffdio_register.range.len = page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Ricardo,
our agent completed the review of the patch.
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c static void run(void) · line 97
Quoted 5 lines · Ricardo Branco
userfaultfd: Use two-step handshake to probe features
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +3
-0+if (!(uffdio_api.features &UFFD_FEATURE_PAGEFAULT_FLAG_WP )) +tst_brk (TCONF ,"UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported" ); +SAFE_CLOSE (uffd);
tst_brk() longjmps out of run(), so SAFE_CLOSE never executes — close uffd before calling tst_brk(). userfaultfd05.c also has no .cleanup, so the page mmap from set_pages() leaks too; add .cleanup = reset_pages to struct tst_test.
[...]
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c static void run(void) · line 108
Quoted 3 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +3
-0+if (!(uffdio_api.features &UFFD_FEATURE_POISON )) +tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); +SAFE_CLOSE (uffd);
Same issue: close uffd before calling tst_brk().
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Ricardo,
our agent completed the review of the patch.
Quoted 4 lines · Ricardo Branco
userfaultfd: Use two-step handshake to probe features
Fix this by using the required two-step handshake and skip with TCONF if the required feature bit is absent.
The patch is fixing a bug (UFFDIO_API EINVAL causing TBROK instead of TCONF); please add a Fixes: tag pointing to the commit that introduced the incorrect single-step setup.
Quoted 1 line
[...]
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/1 userfaultfd: Use two-step handshake to probe features
In reply to Ricardo Brancouserfaultfd05 and userfaultfd06 assume that UFFD_FEATURE_PAGEFAULT_FLAG_WP and UFFD_FEATURE_POISON are available when setting up userfaultfd.
On kernels where either feature is unavailable at runtime, UFFDIO_API fails with EINVAL and the tests end up as TBROK instead of being skipped.
Fix this by using the required two-step handshake and skip with TCONF if the required feature bit is absent.
--- .../kernel/syscalls/userfaultfd/userfaultfd05.c | 12 +++++++++++- .../kernel/syscalls/userfaultfd/userfaultfd06.c | 15 +++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-)
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +11 -1
@@ -82,10 +82,20 @@ static void *handle_thread(void)static void run (void ) {pthread_t thr; -struct uffdio_api uffdio_api; +struct uffdio_api uffdio_api = {};struct uffdio_register uffdio_register;struct uffdio_writeprotect uffdio_writeprotect; +/* +* Two-step handshake to check if feature is available +*/ + uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); + uffdio_api.api =UFFD_API ; +SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_PAGEFAULT_FLAG_WP )) +tst_brk (TCONF ,"UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported" ); +SAFE_CLOSE (uffd); +set_pages (); uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false);
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +11 -4
@@ -97,18 +97,25 @@ static void run(void) poison_fault_seen =0 ; sigbus_seen =0 ; -set_pages (); +/* +* Two-step handshake to check if feature is available +*/ uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); + uffdio_api.api =UFFD_API ; +SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_POISON )) +tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); +SAFE_CLOSE (uffd); +set_pages (); + + uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); uffdio_api.api =UFFD_API ; uffdio_api.features =UFFD_FEATURE_POISON ;SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); -if (!(uffdio_api.features &UFFD_FEATURE_POISON )) -tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); - uffdio_register.range.start = (unsigned long ) page; uffdio_register.range.len = page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Ricardo,
Quoted 1 line · Ricardo Branco
userfaultfd: Use two-step handshake to probe features
---
Note:
Our agent completed the review of the patch. The full review can be found at: https://patchwork.ozlabs.org/project/ltp/list/?series=499941
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Review
Quoted 3 lines
(quoted diff) +3
-0+ /* + * Two-step handshake to check if feature is available + */
Please no obvious comments like this.
Review
Quoted 6 lines
(quoted diff) +6
-0+ uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false); + uffdio_api.api = UFFD_API; + SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api); + if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) + tst_brk(TCONF, "UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported"); + SAFE_CLOSE(uffd);
I do not get why are we adding this code when we do the same just a few lines below. Shouldn't just this suffice?
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +3 -0
@@ -95,6 +95,9 @@ static void run(void)SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_PAGEFAULT_FLAG_WP )) +tst_brk (TCONF ,"UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported" ); + uffdio_register.range.start = (unsigned long ) page; uffdio_register.range.len = page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_WP ;
Quoted 20 lines
index
5b1252c35..e1b3c2f6f100644testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +9
-1@@ -97,18 +97,25 @@ static void run(void) poison_fault_seen =0 ; sigbus_seen =0 ; -set_pages (); +/* +* Two-step handshake to check if feature is available +*/ uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); + uffdio_api.api =UFFD_API ; +SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_POISON )) +tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); +SAFE_CLOSE (uffd); +set_pages ();
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c static void run(void) · line 111
Quoted 10 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +2
-3+ + uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); uffdio_api.api =UFFD_API ; uffdio_api.features =UFFD_FEATURE_POISON ;SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); -if (!(uffdio_api.features &UFFD_FEATURE_POISON )) -tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); -
This does not seem to make any sense. The code already checks for the feature, or do I miss something?
Quoted 9 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +0
-0uffdio_register.range.start = (unsigned long ) page; uffdio_register.range.len = page_size; uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ;Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 8 lines · Cyril Hrubis
Review
Quoted 6 lines
(quoted diff) +6
-0+ uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false); + uffdio_api.api = UFFD_API; + SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api); + if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) + tst_brk(TCONF, "UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported"); + SAFE_CLOSE(uffd);I do not get why are we adding this code when we do the same just a few lines below. Shouldn't just this suffice?
That's why the comment is needed because it isn't obvious.
We need to do the 2 step handshake, first with uffdio_api.features set to zero, then again with the desired features.
Quoted 25 lines
Quoted 25 lines
index
5b1252c35..e1b3c2f6f100644testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +11
-1@@ -97,18 +97,25 @@ static void run(void) poison_fault_seen =0 ; sigbus_seen =0 ; -set_pages (); +/* +* Two-step handshake to check if feature is available +*/ uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); + uffdio_api.api =UFFD_API ; +SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); +if (!(uffdio_api.features &UFFD_FEATURE_POISON )) +tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); +SAFE_CLOSE (uffd); +set_pages (); + + uffd =SAFE_USERFAULTFD (O_CLOEXEC |O_NONBLOCK , false); uffdio_api.api =UFFD_API ; uffdio_api.features =UFFD_FEATURE_POISON ;
Quoted 7 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c static void run(void) · line 111
Quoted 5 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +0
-3SAFE_IOCTL (uffd,UFFDIO_API , &uffdio_api); -if (!(uffdio_api.features &UFFD_FEATURE_POISON )) -tst_brk (TCONF ,"UFFD_FEATURE_POISON not supported" ); -This does not seem to make any sense. The code already checks for the feature, or do I miss something?
Context: https://github.com/linux-test-project/ltp/issues/1289
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 9 lines
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false); + uffdio_api.api = UFFD_API; + SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api); + if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) + tst_brk(TCONF, "UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported"); + SAFE_CLOSE(uffd);I do not get why are we adding this code when we do the same just a few lines below. Shouldn't just this suffice?
Quoted 4 lines
That's why the comment is needed because it isn't obvious.
We need to do the 2 step handshake, first with uffdio_api.features set to zero, then again with the desired features.
The obviously the comment is not useful because it does not explain anything :-).
Looking at the kernel code, it seems to set the output flags uncoditionally so we can use that.
Or check for EINVAL when we pass unsupported flags.
However I would put this code into a common header and call it only in the test setup (so that it's not executed on each test iteration).
#define CHECK_UFFD_FLAG(flag) do { \
... \
if (!(uffdio_api.features & (flag))) \
tst_brk(TCONF, #flag " not supported"); \
... \
} while (0)Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 24 lines · Cyril Hrubis
Hi!
Quoted 8 lines
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false); + uffdio_api.api = UFFD_API; + SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api); + if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) + tst_brk(TCONF, "UFFD_FEATURE_PAGEFAULT_FLAG_WP not supported"); + SAFE_CLOSE(uffd);I do not get why are we adding this code when we do the same just a few lines below. Shouldn't just this suffice?
Quoted 4 lines
That's why the comment is needed because it isn't obvious.
We need to do the 2 step handshake, first with uffdio_api.features set to zero, then again with the desired features.
The obviously the comment is not useful because it does not explain anything :-).
Looking at the kernel code, it seems to set the output flags uncoditionally so we can use that.
Or check for EINVAL when we pass unsupported flags.
However I would put this code into a common header and call it only in the test setup (so that it's not executed on each test iteration).
Quoted 7 lines
#define CHECK_UFFD_FLAG(flag) do { \
... \
if (!(uffdio_api.features & (flag))) \
tst_brk(TCONF, #flag " not supported"); \
... \
} while (0)Will add this macro. Let's keep doing the 2 step handshake though.
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 11 lines
Quoted 10 lines
However I would put this code into a common header and call it only in the test setup (so that it's not executed on each test iteration).
#define CHECK_UFFD_FLAG(flag) do { \ ... \ if (!(uffdio_api.features & (flag))) \ tst_brk(TCONF, #flag " not supported"); \ ... \ } while (0)
Quoted 1 line
Will add this macro. Let's keep doing the 2 step handshake though.
Sure, sounds good to me.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 11 lines · Cyril Hrubis
Hi!
Quoted 10 lines
Quoted 10 lines
However I would put this code into a common header and call it only in the test setup (so that it's not executed on each test iteration).
#define CHECK_UFFD_FLAG(flag) do { \ ... \ if (!(uffdio_api.features & (flag))) \ tst_brk(TCONF, #flag " not supported"); \ ... \ } while (0)
Quoted 3 lines
Quoted 1 line
Will add this macro. Let's keep doing the 2 step handshake though.
Sure, sounds good to me.
Just did it in a 2-part v3.
Cheers,
R
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp