Skip to content

Commit 760aee0

Browse files
xzpeterakpm00
authored andcommitted
selftests/mm: add tests for RO pinning vs fork()
Add a test suite (with 10 more sub-tests) to cover RO pinning against fork() over uffd-wp. It covers both: (1) Early CoW test in fork() when page pinned, (2) page unshare due to RO longterm pin. They are: Testing wp-fork-pin on anon... done Testing wp-fork-pin on shmem... done Testing wp-fork-pin on shmem-private... done Testing wp-fork-pin on hugetlb... done Testing wp-fork-pin on hugetlb-private... done Testing wp-fork-pin-with-event on anon... done Testing wp-fork-pin-with-event on shmem... done Testing wp-fork-pin-with-event on shmem-private... done Testing wp-fork-pin-with-event on hugetlb... done Testing wp-fork-pin-with-event on hugetlb-private... done CONFIG_GUP_TEST needed or they'll be skipped. Testing wp-fork-pin on anon... skipped [reason: Possibly CONFIG_GUP_TEST missing or unprivileged] Note that the major test goal is on private memory, but no hurt to also run all of them over shared because shared memory should work the same. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Peter Xu <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Axel Rasmussen <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Mika Penttilä <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Nadav Amit <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 71fc41e commit 760aee0

File tree

1 file changed

+141
-3
lines changed

1 file changed

+141
-3
lines changed

tools/testing/selftests/mm/uffd-unit-tests.c

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "uffd-common.h"
99

10+
#include "../../../../mm/gup_test.h"
11+
1012
#ifdef __NR_userfaultfd
1113

1214
/* The unit test doesn't need a large or random size, make it 32MB for now */
@@ -247,7 +249,53 @@ static void *fork_event_consumer(void *data)
247249
return NULL;
248250
}
249251

250-
static int pagemap_test_fork(int uffd, bool with_event)
252+
typedef struct {
253+
int gup_fd;
254+
bool pinned;
255+
} pin_args;
256+
257+
/*
258+
* Returns 0 if succeed, <0 for errors. pin_pages() needs to be paired
259+
* with unpin_pages(). Currently it needs to be RO longterm pin to satisfy
260+
* all needs of the test cases (e.g., trigger unshare, trigger fork() early
261+
* CoW, etc.).
262+
*/
263+
static int pin_pages(pin_args *args, void *buffer, size_t size)
264+
{
265+
struct pin_longterm_test test = {
266+
.addr = (uintptr_t)buffer,
267+
.size = size,
268+
/* Read-only pins */
269+
.flags = 0,
270+
};
271+
272+
if (args->pinned)
273+
err("already pinned");
274+
275+
args->gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR);
276+
if (args->gup_fd < 0)
277+
return -errno;
278+
279+
if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_START, &test)) {
280+
/* Even if gup_test existed, can be an old gup_test / kernel */
281+
close(args->gup_fd);
282+
return -errno;
283+
}
284+
args->pinned = true;
285+
return 0;
286+
}
287+
288+
static void unpin_pages(pin_args *args)
289+
{
290+
if (!args->pinned)
291+
err("unpin without pin first");
292+
if (ioctl(args->gup_fd, PIN_LONGTERM_TEST_STOP))
293+
err("PIN_LONGTERM_TEST_STOP");
294+
close(args->gup_fd);
295+
args->pinned = false;
296+
}
297+
298+
static int pagemap_test_fork(int uffd, bool with_event, bool test_pin)
251299
{
252300
fork_event_args args = { .parent_uffd = uffd, .child_uffd = -1 };
253301
pthread_t thread;
@@ -264,7 +312,17 @@ static int pagemap_test_fork(int uffd, bool with_event)
264312
child = fork();
265313
if (!child) {
266314
/* Open the pagemap fd of the child itself */
315+
pin_args args = {};
316+
267317
fd = pagemap_open();
318+
319+
if (test_pin && pin_pages(&args, area_dst, page_size))
320+
/*
321+
* Normally when reach here we have pinned in
322+
* previous tests, so shouldn't fail anymore
323+
*/
324+
err("pin page failed in child");
325+
268326
value = pagemap_get_entry(fd, area_dst);
269327
/*
270328
* After fork(), we should handle uffd-wp bit differently:
@@ -273,6 +331,8 @@ static int pagemap_test_fork(int uffd, bool with_event)
273331
* (2) when without EVENT_FORK, it should be dropped
274332
*/
275333
pagemap_check_wp(value, with_event);
334+
if (test_pin)
335+
unpin_pages(&args);
276336
/* Succeed */
277337
exit(0);
278338
}
@@ -352,7 +412,7 @@ static void uffd_wp_fork_test_common(uffd_test_args_t *args,
352412
wp_range(uffd, (uint64_t)area_dst, page_size, true);
353413
value = pagemap_get_entry(pagemap_fd, area_dst);
354414
pagemap_check_wp(value, true);
355-
if (pagemap_test_fork(uffd, with_event)) {
415+
if (pagemap_test_fork(uffd, with_event, false)) {
356416
uffd_test_fail("Detected %s uffd-wp bit in child in present pte",
357417
with_event ? "missing" : "stall");
358418
goto out;
@@ -383,7 +443,7 @@ static void uffd_wp_fork_test_common(uffd_test_args_t *args,
383443
/* Uffd-wp should persist even swapped out */
384444
value = pagemap_get_entry(pagemap_fd, area_dst);
385445
pagemap_check_wp(value, true);
386-
if (pagemap_test_fork(uffd, with_event)) {
446+
if (pagemap_test_fork(uffd, with_event, false)) {
387447
uffd_test_fail("Detected %s uffd-wp bit in child in zapped pte",
388448
with_event ? "missing" : "stall");
389449
goto out;
@@ -415,6 +475,68 @@ static void uffd_wp_fork_with_event_test(uffd_test_args_t *args)
415475
uffd_wp_fork_test_common(args, true);
416476
}
417477

478+
static void uffd_wp_fork_pin_test_common(uffd_test_args_t *args,
479+
bool with_event)
480+
{
481+
int pagemap_fd;
482+
pin_args pin_args = {};
483+
484+
if (uffd_register(uffd, area_dst, page_size, false, true, false))
485+
err("register failed");
486+
487+
pagemap_fd = pagemap_open();
488+
489+
/* Touch the page */
490+
*area_dst = 1;
491+
wp_range(uffd, (uint64_t)area_dst, page_size, true);
492+
493+
/*
494+
* 1. First pin, then fork(). This tests fork() special path when
495+
* doing early CoW if the page is private.
496+
*/
497+
if (pin_pages(&pin_args, area_dst, page_size)) {
498+
uffd_test_skip("Possibly CONFIG_GUP_TEST missing "
499+
"or unprivileged");
500+
close(pagemap_fd);
501+
uffd_unregister(uffd, area_dst, page_size);
502+
return;
503+
}
504+
505+
if (pagemap_test_fork(uffd, with_event, false)) {
506+
uffd_test_fail("Detected %s uffd-wp bit in early CoW of fork()",
507+
with_event ? "missing" : "stall");
508+
unpin_pages(&pin_args);
509+
goto out;
510+
}
511+
512+
unpin_pages(&pin_args);
513+
514+
/*
515+
* 2. First fork(), then pin (in the child, where test_pin==true).
516+
* This tests COR, aka, page unsharing on private memories.
517+
*/
518+
if (pagemap_test_fork(uffd, with_event, true)) {
519+
uffd_test_fail("Detected %s uffd-wp bit when RO pin",
520+
with_event ? "missing" : "stall");
521+
goto out;
522+
}
523+
uffd_test_pass();
524+
out:
525+
if (uffd_unregister(uffd, area_dst, page_size))
526+
err("register failed");
527+
close(pagemap_fd);
528+
}
529+
530+
static void uffd_wp_fork_pin_test(uffd_test_args_t *args)
531+
{
532+
uffd_wp_fork_pin_test_common(args, false);
533+
}
534+
535+
static void uffd_wp_fork_pin_with_event_test(uffd_test_args_t *args)
536+
{
537+
uffd_wp_fork_pin_test_common(args, true);
538+
}
539+
418540
static void check_memory_contents(char *p)
419541
{
420542
unsigned long i, j;
@@ -923,6 +1045,22 @@ uffd_test_case_t uffd_tests[] = {
9231045
/* when set, child process should inherit uffd-wp bits */
9241046
UFFD_FEATURE_EVENT_FORK,
9251047
},
1048+
{
1049+
.name = "wp-fork-pin",
1050+
.uffd_fn = uffd_wp_fork_pin_test,
1051+
.mem_targets = MEM_ALL,
1052+
.uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1053+
UFFD_FEATURE_WP_HUGETLBFS_SHMEM,
1054+
},
1055+
{
1056+
.name = "wp-fork-pin-with-event",
1057+
.uffd_fn = uffd_wp_fork_pin_with_event_test,
1058+
.mem_targets = MEM_ALL,
1059+
.uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP |
1060+
UFFD_FEATURE_WP_HUGETLBFS_SHMEM |
1061+
/* when set, child process should inherit uffd-wp bits */
1062+
UFFD_FEATURE_EVENT_FORK,
1063+
},
9261064
{
9271065
.name = "wp-unpopulated",
9281066
.uffd_fn = uffd_wp_unpopulated_test,

0 commit comments

Comments
 (0)