All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v4] userfaultfd: Minor fixes
@ 2026-05-05 13:14 Ricardo Branco
  2026-05-05 17:32 ` [LTP] " linuxtestproject.agent
  2026-05-06 10:55 ` [LTP] [PATCH v4] " Andrea Cervesato via ltp
  0 siblings, 2 replies; 5+ messages in thread
From: Ricardo Branco @ 2026-05-05 13:14 UTC (permalink / raw)
  To: ltp

- Use POSIX semantics for thread function
- Set cleanup to call reset_pages
- Use long for page_size and use unsigned cast
- Call the SAFE_ versions of some system calls
- Use TFAIL consistently for unexpected UFFD_EVENTs

Signed-off-by: Ricardo Branco <rbranco@suse.de>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
---
 .../syscalls/userfaultfd/userfaultfd01.c      | 27 +++++++++++------
 .../syscalls/userfaultfd/userfaultfd02.c      | 27 +++++++++++------
 .../syscalls/userfaultfd/userfaultfd03.c      | 29 ++++++++++++-------
 .../syscalls/userfaultfd/userfaultfd04.c      | 22 +++++++++-----
 .../syscalls/userfaultfd/userfaultfd05.c      | 22 +++++++++-----
 .../syscalls/userfaultfd/userfaultfd06.c      | 14 +++++----
 6 files changed, 91 insertions(+), 50 deletions(-)

diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd01.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd01.c
index 7368d3863..3af0e8240 100644
--- a/testcases/kernel/syscalls/userfaultfd/userfaultfd01.c
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd01.c
@@ -28,10 +28,10 @@ static struct tcase {
 	{ DESC(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY),  .kver = AFTER_5_11, },
 };
 
-static int page_size;
+static long page_size;
 static char *page;
 static void *copy_page;
-static int uffd;
+static int uffd = -1;
 static int kver;
 
 static void setup(void)
@@ -44,7 +44,7 @@ static void setup(void)
 
 static void set_pages(void)
 {
-	page_size = sysconf(_SC_PAGE_SIZE);
+	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
 	page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 	copy_page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
@@ -53,11 +53,19 @@ static void set_pages(void)
 
 static void reset_pages(void)
 {
-	SAFE_MUNMAP(page, page_size);
-	SAFE_MUNMAP(copy_page, page_size);
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+	if (copy_page) {
+		SAFE_MUNMAP(copy_page, page_size);
+		copy_page = NULL;
+	}
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
 }
 
-static void *handle_thread(void)
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	static struct uffd_msg msg;
 	struct uffdio_copy uffdio_copy = {};
@@ -74,18 +82,18 @@ static void *handle_thread(void)
 	SAFE_READ(1, uffd, &msg, sizeof(msg));
 
 	if (msg.event != UFFD_EVENT_PAGEFAULT)
-		tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT %d", msg.event);
+		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
 
 	memset(copy_page, 'X', page_size);
 
 	uffdio_copy.src = (unsigned long) copy_page;
 
 	uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address
-			& ~(page_size - 1);
+			& ~((unsigned long)page_size - 1);
 	uffdio_copy.len = page_size;
 	SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
 
-	close(uffd);
+	SAFE_CLOSE(uffd);
 	return NULL;
 }
 
@@ -129,4 +137,5 @@ static struct tst_test test = {
 	.setup = setup,
 	.test = run,
 	.tcnt = ARRAY_SIZE(tcases),
+	.cleanup = reset_pages,
 };
diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd02.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd02.c
index 2fd5ba5d8..70dd844b3 100644
--- a/testcases/kernel/syscalls/userfaultfd/userfaultfd02.c
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd02.c
@@ -16,14 +16,14 @@
 #include "tst_safe_pthread.h"
 #include "lapi/userfaultfd.h"
 
-static int page_size;
+static long page_size;
 static char *page;
 static void *move_page;
-static int uffd;
+static int uffd = -1;
 
 static void set_pages(void)
 {
-	page_size = sysconf(_SC_PAGE_SIZE);
+	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
 	page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 	move_page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
@@ -32,11 +32,19 @@ static void set_pages(void)
 
 static void reset_pages(void)
 {
-	SAFE_MUNMAP(page, page_size);
-	SAFE_MUNMAP(move_page, page_size);
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+	if (move_page) {
+		SAFE_MUNMAP(move_page, page_size);
+		move_page = NULL;
+	}
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
 }
 
-static void *handle_thread(void)
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	static struct uffd_msg msg;
 	struct uffdio_move uffdio_move = {};
@@ -44,18 +52,18 @@ static void *handle_thread(void)
 	SAFE_READ(1, uffd, &msg, sizeof(msg));
 
 	if (msg.event != UFFD_EVENT_PAGEFAULT)
-		tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT %d", msg.event);
+		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
 
 	memset(move_page, 'X', page_size);
 
 	uffdio_move.src = (unsigned long) move_page;
 
 	uffdio_move.dst = (unsigned long) msg.arg.pagefault.address
-			& ~(page_size - 1);
+			& ~((unsigned long)page_size - 1);
 	uffdio_move.len = page_size;
 	SAFE_IOCTL(uffd, UFFDIO_MOVE, &uffdio_move);
 
-	close(uffd);
+	SAFE_CLOSE(uffd);
 	return NULL;
 }
 
@@ -94,4 +102,5 @@ static void run(void)
 static struct tst_test test = {
 	.test_all = run,
 	.min_kver = "6.8",
+	.cleanup = reset_pages,
 };
diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd03.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd03.c
index b65f39eca..8a720e41a 100644
--- a/testcases/kernel/syscalls/userfaultfd/userfaultfd03.c
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd03.c
@@ -19,10 +19,10 @@
 #include "tst_safe_pthread.h"
 #include "lapi/userfaultfd.h"
 
-static int page_size;
+static long page_size;
 static char *page;
 static void *copy_page;
-static int uffd;
+static int uffd = -1;
 
 static void setup(void)
 {
@@ -48,7 +48,7 @@ static int open_userfaultfd(int flags)
 
 static void set_pages(void)
 {
-	page_size = sysconf(_SC_PAGE_SIZE);
+	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
 	page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 	copy_page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
@@ -57,11 +57,19 @@ static void set_pages(void)
 
 static void reset_pages(void)
 {
-	SAFE_MUNMAP(page, page_size);
-	SAFE_MUNMAP(copy_page, page_size);
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+	if (copy_page) {
+		SAFE_MUNMAP(copy_page, page_size);
+		copy_page = NULL;
+	}
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
 }
 
-static void *handle_thread(void)
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	static struct uffd_msg msg;
 	struct uffdio_copy uffdio_copy = {};
@@ -78,18 +86,18 @@ static void *handle_thread(void)
 	SAFE_READ(1, uffd, &msg, sizeof(msg));
 
 	if (msg.event != UFFD_EVENT_PAGEFAULT)
-		tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT %d", msg.event);
+		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
 
 	memset(copy_page, 'X', page_size);
 
 	uffdio_copy.src = (unsigned long) copy_page;
 
 	uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address
-			& ~(page_size - 1);
+			& ~((unsigned long)page_size - 1);
 	uffdio_copy.len = page_size;
 	SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
 
-	close(uffd);
+	SAFE_CLOSE(uffd);
 	return NULL;
 }
 
@@ -132,5 +140,6 @@ static struct tst_test test = {
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USERFAULTFD=y",
 		NULL
-	}
+	},
+	.cleanup = reset_pages,
 };
diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd04.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd04.c
index 4eb811e45..926812649 100644
--- a/testcases/kernel/syscalls/userfaultfd/userfaultfd04.c
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd04.c
@@ -17,23 +17,28 @@
 #include "tst_safe_pthread.h"
 #include "lapi/userfaultfd.h"
 
-static int page_size;
+static long page_size;
 static char *page;
-static int uffd;
+static int uffd = -1;
 
 static void set_pages(void)
 {
-	page_size = sysconf(_SC_PAGE_SIZE);
+	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
 	page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 }
 
 static void reset_pages(void)
 {
-	SAFE_MUNMAP(page, page_size);
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
 }
 
-static void *handle_thread(void)
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	static struct uffd_msg msg;
 	struct uffdio_zeropage uffdio_zeropage = {};
@@ -50,15 +55,15 @@ static void *handle_thread(void)
 	SAFE_READ(1, uffd, &msg, sizeof(msg));
 
 	if (msg.event != UFFD_EVENT_PAGEFAULT)
-		tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT %d", msg.event);
+		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
 
 	uffdio_zeropage.range.start	= msg.arg.pagefault.address
-					& ~(page_size - 1);
+					& ~((unsigned long)page_size - 1);
 	uffdio_zeropage.range.len	= page_size;
 
 	SAFE_IOCTL(uffd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
 
-	close(uffd);
+	SAFE_CLOSE(uffd);
 	return NULL;
 }
 
@@ -98,4 +103,5 @@ static void run(void)
 
 static struct tst_test test = {
 	.test_all = run,
+	.cleanup = reset_pages,
 };
diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd05.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd05.c
index e25a227cf..59956d85d 100644
--- a/testcases/kernel/syscalls/userfaultfd/userfaultfd05.c
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd05.c
@@ -17,14 +17,14 @@
 #include "tst_safe_pthread.h"
 #include "lapi/userfaultfd.h"
 
-static int page_size;
+static long page_size;
 static char *page;
-static int uffd;
+static int uffd = -1;
 static volatile int wp_fault_seen;
 
 static void set_pages(void)
 {
-	page_size = sysconf(_SC_PAGE_SIZE);
+	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
 	page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 
@@ -33,10 +33,15 @@ static void set_pages(void)
 
 static void reset_pages(void)
 {
-	SAFE_MUNMAP(page, page_size);
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
 }
 
-static void *handle_thread(void)
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	static struct uffd_msg msg;
 	struct uffdio_writeprotect uffdio_writeprotect = {};
@@ -70,12 +75,12 @@ static void *handle_thread(void)
 	wp_fault_seen = 1;
 
 	/* Resolve the fault by clearing WP so the writer can resume. */
-	uffdio_writeprotect.range.start	= msg.arg.pagefault.address & ~(page_size - 1);
+	uffdio_writeprotect.range.start	= msg.arg.pagefault.address & ~((unsigned long)page_size - 1);
 	uffdio_writeprotect.range.len	= page_size;
 
 	SAFE_IOCTL(uffd, UFFDIO_WRITEPROTECT, &uffdio_writeprotect);
 
-	close(uffd);
+	SAFE_CLOSE(uffd);
 	return NULL;
 }
 
@@ -127,5 +132,6 @@ static struct tst_test test = {
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_HAVE_ARCH_USERFAULTFD_WP=y",
 		NULL
-	}
+	},
+	.cleanup = reset_pages,
 };
diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd06.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd06.c
index 5b1252c35..ec93d8ad6 100644
--- a/testcases/kernel/syscalls/userfaultfd/userfaultfd06.c
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd06.c
@@ -19,9 +19,9 @@
 #include "tst_safe_pthread.h"
 #include "lapi/userfaultfd.h"
 
-static int page_size;
+static long page_size;
 static char *page;
-static int uffd;
+static int uffd = -1;
 static int poison_fault_seen;
 static volatile int sigbus_seen;
 static sigjmp_buf jmpbuf;
@@ -45,7 +45,7 @@ static void setup(void)
 
 static void set_pages(void)
 {
-	page_size = sysconf(_SC_PAGE_SIZE);
+	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
 	page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 }
@@ -56,9 +56,11 @@ static void reset_pages(void)
 		SAFE_MUNMAP(page, page_size);
 		page = NULL;
 	}
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
 }
 
-static void *handle_thread(void)
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	static struct uffd_msg msg;
 	struct uffdio_poison uffdio_poison = {};
@@ -79,12 +81,12 @@ static void *handle_thread(void)
 	tst_atomic_store(1, &poison_fault_seen);
 
 	/* Poison the page that triggered the fault */
-	uffdio_poison.range.start = msg.arg.pagefault.address & ~(page_size - 1);
+	uffdio_poison.range.start = msg.arg.pagefault.address & ~((unsigned long)page_size - 1);
 	uffdio_poison.range.len = page_size;
 
 	SAFE_IOCTL(uffd, UFFDIO_POISON, &uffdio_poison);
 
-	close(uffd);
+	SAFE_CLOSE(uffd);
 	return NULL;
 }
 
-- 
2.54.0


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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [LTP] userfaultfd: Minor fixes
  2026-05-05 13:14 [LTP] [PATCH v4] userfaultfd: Minor fixes Ricardo Branco
@ 2026-05-05 17:32 ` linuxtestproject.agent
  2026-05-06 10:55 ` [LTP] [PATCH v4] " Andrea Cervesato via ltp
  1 sibling, 0 replies; 5+ messages in thread
From: linuxtestproject.agent @ 2026-05-05 17:32 UTC (permalink / raw)
  To: Ricardo Branco; +Cc: ltp

Hi Ricardo,

On Tue, 5 May 2026 15:14:41 +0200, Ricardo Branco wrote:
> userfaultfd: Minor fixes
>
> - Use POSIX semantics for thread function
> - Set cleanup to call reset_pages
> - Use long for page_size and use unsigned cast
> - Call the SAFE_ versions of some system calls
> - Use TFAIL consistently for unexpected UFFD_EVENTs

The body only lists what was changed, not why. Please add at least a
sentence per item explaining the problem: e.g. the thread function had
the wrong signature (UB when cast to pthread start routine), missing
.cleanup caused leaks on early abort, int page_size causes sign-extension
in the bitwise address mask, etc.

> -static void *handle_thread(void)
> +static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
>
> -       tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT %d", msg.event);
> +       tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
>
> -       page_size = sysconf(_SC_PAGE_SIZE);
> +       page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
> +       .cleanup = reset_pages,

These are five distinct fix categories (thread signature, resource
management, type correctness, SAFE-macro adoption, result-code change).
Please split them into focused commits so each can be reviewed and
bisected independently.

Pre-existing issues noticed in the surrounding code (not introduced
by this patch):

- userfaultfd04.c (and similar): early-return TFAIL path skips
  SAFE_PTHREAD_JOIN, leaving the handler thread un-joined.
- userfaultfd03.c: .needs_root = 1 may be unnecessary since
  /dev/userfaultfd was designed for unprivileged access.

---
Note:

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.

Regards,
LTP AI Reviewer

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v4] userfaultfd: Minor fixes
  2026-05-05 13:14 [LTP] [PATCH v4] userfaultfd: Minor fixes Ricardo Branco
  2026-05-05 17:32 ` [LTP] " linuxtestproject.agent
@ 2026-05-06 10:55 ` Andrea Cervesato via ltp
  2026-05-06 11:22   ` Andrea Cervesato via ltp
  2026-05-06 11:23   ` Cyril Hrubis
  1 sibling, 2 replies; 5+ messages in thread
From: Andrea Cervesato via ltp @ 2026-05-06 10:55 UTC (permalink / raw)
  To: Ricardo Branco; +Cc: ltp

Hi Ricardo,

> +		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);

I think Cyril meant tst_res(TFAIL, ..) + return in the previous review.

@Cyril am I correct?

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v4] userfaultfd: Minor fixes
  2026-05-06 10:55 ` [LTP] [PATCH v4] " Andrea Cervesato via ltp
@ 2026-05-06 11:22   ` Andrea Cervesato via ltp
  2026-05-06 11:23   ` Cyril Hrubis
  1 sibling, 0 replies; 5+ messages in thread
From: Andrea Cervesato via ltp @ 2026-05-06 11:22 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Sorry I was over-reading the various userfaultfd testing suite.
This one is correct and I merged it.

Thanks,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v4] userfaultfd: Minor fixes
  2026-05-06 10:55 ` [LTP] [PATCH v4] " Andrea Cervesato via ltp
  2026-05-06 11:22   ` Andrea Cervesato via ltp
@ 2026-05-06 11:23   ` Cyril Hrubis
  1 sibling, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2026-05-06 11:23 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi!
> > +		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
> 
> I think Cyril meant tst_res(TFAIL, ..) + return in the previous review.
> 
> @Cyril am I correct?

Actually I've fixed the test library to work fine with
tst_brk(TFAIL, ...) in a1f82704c2

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-05-06 11:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-05 13:14 [LTP] [PATCH v4] userfaultfd: Minor fixes Ricardo Branco
2026-05-05 17:32 ` [LTP] " linuxtestproject.agent
2026-05-06 10:55 ` [LTP] [PATCH v4] " Andrea Cervesato via ltp
2026-05-06 11:22   ` Andrea Cervesato via ltp
2026-05-06 11:23   ` Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.