tests/userfaultfd: handle __NR_userfaultfd not being defined
PATCH 1/1 tests/userfaultfd: handle __NR_userfaultfd not being defined
On some old kernels (think RHEL-7) __NR_userfaultfd may not be defined on certain arches, even though the <linux/userfaultfd.h> header is available. To avoid build errors in such environments, abstract the userfaultfd syscall into a helper function and make it fail with ENOSYS when __NR_userfaultfd is not defined.
--- tests/userfaultfd/userfaultfd.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
tests/userfaultfd/userfaultfd.c +14 -3
@@ -19,7 +19,7 @@ int page_size;void *fault_handler_thread (void *arg) { -long uffd = (long )arg; +int uffd = (int )(intptr_t )arg;struct uffd_msg msg = {0 };struct uffdio_copy uffdio_copy = {0 };ssize_t nread;
@@ -83,6 +83,16 @@ void *fault_handler_thread(void *arg) } } +int syscall_userfaultfd (int flags) +{ +#ifdef __NR_userfaultfd +return (int )syscall (__NR_userfaultfd, flags); +#else + errno =ENOSYS ; +return -1 ; +#endif +} +int main (int argc,char *argv[]) {char *addr;
@@ -92,7 +102,7 @@ int main (int argc, char *argv[])pthread_t thr;// ID of thread that handles page faults ssize_t ret; -long uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +int uffd =syscall_userfaultfd (O_CLOEXEC |O_NONBLOCK );if (uffd <0 ) {if (errno ==ENOSYS ) {return 8 ;
@@ -159,7 +169,8 @@ int main (int argc, char *argv[]) }// Create a thread that will process the userfaultfd events - ret =pthread_create (&thr,NULL , fault_handler_thread, (void *) uffd); + ret =pthread_create (&thr,NULL , fault_handler_thread, + (void *)(intptr_t )uffd);if (ret !=0 ) { errno = ret;perror ("pthread_create" );
Signature · 1 line
2.30.2
A message that is not in the archive (sent off-list or not yet received): CA+EESO4km4ADU0mAcXWR_MjG_v69-4U6tHqgZfPTmCdacSL_sw@mail.gmail.com
Quoted 11 lines · Lokesh Gidra
Quoted 7 lines · Ondrej Mosnacek
On some old kernels (think RHEL-7)
__NR_userfaultfdmay not be defined on certain arches, even though the <linux/userfaultfd.h> header is available. To avoid build errors in such environments, abstract the userfaultfd syscall into a helper function and make it fail with ENOSYS when__NR_userfaultfdis not defined.
Quoted 4 lines
Quoted 1 line
Signed-off-by Ondrej Mosnacek <[email protected]>Reviewed-by Lokesh Gidra <[email protected]>
Thanks!
Quoted 4 lines
Thanks for this fix.
BTW, does it make sense to keep this test enabled on old kernels which don't have SELinux support for userfaultfd?
No, it doesn't - that's why I made it return ENOSYS in this case, which will lead to the test being skipped.
Quoted 57 lines
Quoted 57 lines
--- tests/userfaultfd/userfaultfd.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)tests/userfaultfd/userfaultfd.c +14
-4@@ -19,7 +19,7 @@ int page_size;void *fault_handler_thread (void *arg) { -long uffd = (long )arg; +int uffd = (int )(intptr_t )arg;struct uffd_msg msg = {0 };struct uffdio_copy uffdio_copy = {0 };ssize_t nread;@@ -83,6 +83,16 @@ void *fault_handler_thread(void *arg) } } +int syscall_userfaultfd (int flags) +{ +#ifdef __NR_userfaultfd +return (int )syscall (__NR_userfaultfd, flags); +#else + errno =ENOSYS ; +return -1 ; +#endif +} +int main (int argc,char *argv[]) {char *addr;@@ -92,7 +102,7 @@ int main (int argc, char *argv[])pthread_t thr;// ID of thread that handles page faults ssize_t ret; -long uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +int uffd =syscall_userfaultfd (O_CLOEXEC |O_NONBLOCK );if (uffd <0 ) {if (errno ==ENOSYS ) {return 8 ;@@ -159,7 +169,8 @@ int main (int argc, char *argv[]) }// Create a thread that will process the userfaultfd events - ret =pthread_create (&thr,NULL , fault_handler_thread, (void *) uffd); + ret =pthread_create (&thr,NULL , fault_handler_thread, + (void *)(intptr_t )uffd);if (ret !=0 ) { errno = ret;perror ("pthread_create" ); --2.30.2
Signature · 3 lines
Ondrej Mosnacek Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.
Quoted 12 lines · Ondrej Mosnacek
Quoted 11 lines · Lokesh Gidra
Quoted 7 lines · Ondrej Mosnacek
On some old kernels (think RHEL-7)
__NR_userfaultfdmay not be defined on certain arches, even though the <linux/userfaultfd.h> header is available. To avoid build errors in such environments, abstract the userfaultfd syscall into a helper function and make it fail with ENOSYS when__NR_userfaultfdis not defined.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Ondrej Mosnacek <[email protected]>Reviewed-by Lokesh Gidra <[email protected]>Thanks!
And now it's applied:
https://github.com/SELinuxProject/selinux-testsuite/commit/611d5247e464c69c343e4c5268f346809cf09bbd
Signature · 3 lines
Ondrej Mosnacek Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.