[LTP] [PATCH v4] userfaultfd: Add tests for UFFDIO_RWPROTECT and UFFDIO_SET_MODE

Cyril Hrubis chrubis@suse.cz
Wed Nov 25 13:22:06 CET 2026


Hi!
> +static void run(unsigned int n)
> +{
> +	pthread_t thr;
> +	struct uffdio_api uffdio_api = { .api = UFFD_API };
> +	struct uffdio_register uffdio_register;
> +	struct uffdio_set_mode uffdio_set_mode = {
> +		.disable = UFFD_FEATURE_RWP_ASYNC,
> +	};
> +	struct pollfd pollfd;
> +	unsigned char val;
> +
> +	uffdio_api.features = UFFD_FEATURE_RWP;
> +	if (n) {
> +		CHECK_UFFD_FEATURE(UFFD_FEATURE_RWP_ASYNC);
> +		uffdio_api.features |= UFFD_FEATURE_RWP_ASYNC;
> +	}
> +
> +	set_page();
> +	rwp_fault_seen = 0;
> +
> +	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
> +
> +	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_RWP;
> +
> +	SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
> +
> +	if (n) {
> +		/* Async mode is active, access must auto-resolve. */
> +		rwprotect((unsigned long)page, UFFDIO_RWPROTECT_MODE_RWP);
> +
> +		val = page[0];
> +
> +		if (val != PAGE_BYTE) {
> +			tst_res(TFAIL, "Async access corrupted content: got 0x%02x, expected 0x%02x",
> +				val, PAGE_BYTE);
> +			goto out;
> +		}
> +
> +		pollfd.fd = uffd;
> +		pollfd.events = POLLIN;
> +		if (SAFE_POLL(&pollfd, 1, 0) != 0) {
> +			tst_res(TFAIL, "Unexpected pagefault message in async mode");
> +			goto out;
> +		}
> +
> +		/* Switch to sync mode via UFFDIO_SET_MODE. */
> +		SAFE_IOCTL(uffd, UFFDIO_SET_MODE, &uffdio_set_mode);
> +	}
> +
> +	rwprotect((unsigned long)page, UFFDIO_RWPROTECT_MODE_RWP);
> +
> +	SAFE_PTHREAD_CREATE(&thr, NULL, handle_thread, NULL);
> +
> +	/* Try to read the RWP-protected page. */
> +	val = page[0];
> +
> +	SAFE_PTHREAD_JOIN(thr, NULL);
> +
> +	if (!rwp_fault_seen) {
> +		tst_res(TFAIL, "No RWPROTECT pagefault observed");
> +		goto out;
> +	}
> +
> +	if (val != PAGE_BYTE) {
> +		tst_res(TFAIL, "Page content corrupted: got 0x%02x, expected 0x%02x",
> +			val, PAGE_BYTE);
> +		goto out;
> +	}

Shouldn't this part be in else branch? Otherwise we run both tests for n
= 1.

I suppose that we would have to run the handle_thread in the first if
branch as well, but not as a thread, since it's asynchronous we can run
it as a normal function.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list