[LTP] [PATCH v6 2/2] userfaultfd: Add new test using UFFDIO_CONTINUE

Cyril Hrubis chrubis@suse.cz
Tue May 5 16:12:41 CEST 2026


Hi!
> +static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
> +{
> +	static struct uffd_msg msg;
> +	struct uffdio_continue uffdio_continue = {};
> +	struct pollfd pollfd;
> +	int nready;
> +	char z = 'Z';
> +
> +	pollfd.fd = uffd;
> +	pollfd.events = POLLIN;
> +	nready = poll(&pollfd, 1, -1);
> +	if (nready == -1)
> +		tst_brk(TBROK | TERRNO, "Error on poll");
> +
> +	SAFE_READ(1, uffd, &msg, sizeof(msg));
> +
> +	if (msg.event != UFFD_EVENT_PAGEFAULT)
> +		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
> +
> +	if (!(msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_MINOR)) {
> +		tst_brk(TBROK, "expected MINOR fault, got flags=0x%llx",
                          ^
			  TFAIL

This is clearly a test assertion and thus a test failure.

> +			(unsigned long long)msg.arg.pagefault.flags);
> +	}
> +
> +	/* Update the shmem page in page cache before resuming the fault. */
> +	SAFE_PWRITE(1, memfd, &z, 1, 0);
> +
> +	uffdio_continue.range.start =
> +		msg.arg.pagefault.address & ~((unsigned long)page_size - 1);
> +	uffdio_continue.range.len = page_size;
> +
> +	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
> +
> +	SAFE_CLOSE(uffd);
> +	return NULL;
> +}
> +
> +static void run(void)
> +{
> +	pthread_t thr;
> +	struct uffdio_api uffdio_api = {};
> +	struct uffdio_register uffdio_register;
> +
> +	set_pages();
> +
> +	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, true);
> +	uffdio_api.api = UFFD_API;
> +	uffdio_api.features = 0;
> +	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
> +	if (!(uffdio_api.features & UFFD_FEATURE_MINOR_SHMEM))
> +		tst_brk(TCONF, "UFFD_FEATURE_MINOR_SHMEM not supported");
> +	SAFE_CLOSE(uffd);
> +
> +	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
> +
> +	uffdio_api.api = UFFD_API;
> +	uffdio_api.features = UFFD_FEATURE_MINOR_SHMEM;
> +
> +	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
> +
> +	uffdio_register.range.start = (unsigned long) page;
> +	uffdio_register.range.len = page_size;
> +	uffdio_register.mode = UFFDIO_REGISTER_MODE_MINOR;
> +
> +	SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
> +
> +	/*
> +	 * Drop PTEs while retaining the cached shmem page so the next access
> +	 * faults in MINOR mode.
> +	 */
> +	if (madvise(page, page_size, MADV_DONTNEED) < 0)
> +		tst_brk(TBROK | TERRNO, "madvise MADV_DONTNEED failed");
> +
> +	SAFE_PTHREAD_CREATE(&thr, NULL, (void *) handle_thread, NULL);
> +
> +	char c = page[0];
> +
> +	if (c == 'Z')
> +		tst_res(TPASS, "Pagefault handled via UFFDIO_CONTINUE");
> +	else
> +		tst_res(TFAIL, "pagefault not handled via UFFDIO_CONTINUE, got '%c'", c);
> +
> +	SAFE_PTHREAD_JOIN(thr, NULL);
> +	reset_pages();
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	/*
> +	 * UFFDIO_CONTINUE is available since 5.13 but
> +	 * UFFD_FEATURE_MINOR_SHMEM appeared in 5.14
> +	 */
> +	.min_kver = "5.14",

Should we do a runtime check in the test setup here as well?

> +	.cleanup = reset_pages,
> +};
> -- 
> 2.53.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list