Skip to contentSkip to conversation

tests/userfaultfd: handle __NR_userfaultfd not being defined

PATCH In review Ondrej Mosnacek 1 participant 3 messages Mar 24, 2021 – Mar 29, 2021 1 message not in the archive SELinux security module

lore
Share & tools
lore
#0 lore raw

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
index a283a83..dd3a9f3 100644
@@ -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

#2 lore raw
Quoted 11 lines · Lokesh Gidra
Quoted 7 lines · Ondrej Mosnacek

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.

Quoted 4 lines
Quoted 1 line

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
index a283a83..dd3a9f3 100644
@@ -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.
#3 lore raw
Quoted 12 lines · Ondrej Mosnacek
Quoted 11 lines · Lokesh Gidra
Quoted 7 lines · Ondrej Mosnacek

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.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

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.