From: Petr Vorel <pvorel@suse.cz>
To: Ricardo Branco <rbranco@suse.de>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH] userfaultfd: Add test using UFFDIO_POISON
Date: Fri, 6 Feb 2026 15:47:54 +0100 [thread overview]
Message-ID: <20260206144754.GA404539@pevik> (raw)
In-Reply-To: <20260205205609.147851-1-rbranco@suse.de>
Hi Ricardo,
...
> +static void sigbus_handler(int sig)
> +{
> + if (sig == SIGBUS) {
> + sigbus_seen = 1;
> + siglongjmp(jmpbuf, 1);
> + }
> +}
> +
> +static void set_pages(void)
> +{
> + page_size = 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);
> +}
> +
> +static void *handle_thread(void)
> +{
> + static struct uffd_msg msg;
> + struct uffdio_poison uffdio_poison = {};
> + struct pollfd pollfd;
> + int nready;
> +
> + SAFE_PTHREAD_BARRIER_WAIT(&barrier);
> +
> + 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);
> +
> + poison_fault_seen = 1;
> +
> + /* Poison the page that triggered the fault */
> + uffdio_poison.range.start = msg.arg.pagefault.address & ~(page_size - 1);
> + uffdio_poison.range.len = page_size;
> +
> + SAFE_IOCTL(uffd, UFFDIO_POISON, &uffdio_poison);
CI shows old toolchains fail due:
error: 'UFFDIO_POISON' undeclared
We need a fallback definition in include/lapi/userfaultfd.h.
> +
> + close(uffd);
> + return NULL;
> +}
> +
> +static void run(void)
> +{
> + pthread_t thr;
> + struct uffdio_api uffdio_api = {};
> + struct uffdio_register uffdio_register;
> + struct sigaction sa = {};
> + volatile char dummy;
> +
> + sa.sa_handler = sigbus_handler;
> + sigemptyset(&sa.sa_mask);
> + SAFE_SIGACTION(SIGBUS, &sa, NULL);
> +
> + set_pages();
> +
> + uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
> +
> + uffdio_api.api = UFFD_API;
> + uffdio_api.features = UFFD_FEATURE_POISON;
> +
> + 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_MISSING;
> +
> + SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
> +
> + SAFE_PTHREAD_BARRIER_INIT(&barrier, NULL, 2);
> + SAFE_PTHREAD_CREATE(&thr, NULL, (void *) handle_thread, NULL);
> +
> + SAFE_PTHREAD_BARRIER_WAIT(&barrier);
> +
> + /* Try to read from the page: should trigger fault, get poisoned, then SIGBUS */
> + if (sigsetjmp(jmpbuf, 1) == 0) {
> + dummy = page[0];
> + (void)dummy;
> + }
> +
> + SAFE_PTHREAD_JOIN(thr, NULL);
> + SAFE_PTHREAD_BARRIER_DESTROY(&barrier);
> + reset_pages();
If any of the SAFE_* functions fail, reset_pages() is not called.
We should call it also in cleanup(), guard it with variable not to be munmapped
twice.
The rest LGTM.
Kind regards,
Petr
> +
> + if (poison_fault_seen && sigbus_seen) {
> + tst_res(TPASS, "POISON successfully triggered SIGBUS");
> + } else if (poison_fault_seen && !sigbus_seen) {
> + tst_res(TFAIL, "POISON fault seen but no SIGBUS received");
> + } else if (!poison_fault_seen && sigbus_seen) {
> + tst_res(TFAIL, "SIGBUS received but no poison fault seen");
> + } else {
> + tst_res(TFAIL, "No poison fault or SIGBUS observed");
> + }
> +}
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .min_kver = "6.6",
> +};
--
Mailing list info: https://lists.linux.it/listinfo/ltp
prev parent reply other threads:[~2026-02-06 14:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-05 20:56 [LTP] [PATCH] userfaultfd: Add test using UFFDIO_POISON Ricardo Branco
2026-02-06 14:47 ` Cyril Hrubis
2026-02-06 15:47 ` Ricardo Branco
2026-02-06 14:47 ` Petr Vorel [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260206144754.GA404539@pevik \
--to=pvorel@suse.cz \
--cc=ltp@lists.linux.it \
--cc=rbranco@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.