Commit 9b12488a authored by Peter Xu's avatar Peter Xu Committed by Linus Torvalds

userfaultfd: selftests: add write-protect test

Add uffd tests for write protection.

Instead of introducing new tests for it, let's simply squashing uffd-wp
tests into existing uffd-missing test cases.  Changes are:

(1) Bouncing tests

  We do the write-protection in two ways during the bouncing test:

  - By using UFFDIO_COPY_MODE_WP when resolving MISSING pages: then
    we'll make sure for each bounce process every single page will be
    at least fault twice: once for MISSING, once for WP.

  - By direct call UFFDIO_WRITEPROTECT on existing faulted memories:
    To further torture the explicit page protection procedures of
    uffd-wp, we split each bounce procedure into two halves (in the
    background thread): the first half will be MISSING+WP for each
    page as explained above.  After the first half, we write protect
    the faulted region in the background thread to make sure at least
    half of the pages will be write protected again which is the first
    half to test the new UFFDIO_WRITEPROTECT call.  Then we continue
    with the 2nd half, which will contain both MISSING and WP faulting
    tests for the 2nd half and WP-only faults from the 1st half.

(2) Event/Signal test

  Mostly previous tests but will do MISSING+WP for each page.  For
  sigbus-mode test we'll need to provide standalone path to handle the
  write protection faults.

For all tests, do statistics as well for uffd-wp pages.
Signed-off-by: default avatarPeter Xu <peterx@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Bobby Powers <bobbypowers@gmail.com>
Cc: Brian Geffon <bgeffon@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Denis Plotnikov <dplotnikov@virtuozzo.com>
Cc: "Dr . David Alan Gilbert" <dgilbert@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jerome Glisse <jglisse@redhat.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: "Kirill A . Shutemov" <kirill@shutemov.name>
Cc: Martin Cracauer <cracauer@cons.org>
Cc: Marty McFadden <mcfadden8@llnl.gov>
Cc: Maya Gokhale <gokhale2@llnl.gov>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Pavel Emelyanov <xemul@openvz.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Shaohua Li <shli@fb.com>
Link: http://lkml.kernel.org/r/20200220163112.11409-20-peterx@redhat.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 5c8aed6c
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include <linux/userfaultfd.h> #include <linux/userfaultfd.h>
#include <setjmp.h> #include <setjmp.h>
#include <stdbool.h> #include <stdbool.h>
#include <assert.h>
#include "../kselftest.h" #include "../kselftest.h"
...@@ -76,6 +77,8 @@ static int test_type; ...@@ -76,6 +77,8 @@ static int test_type;
#define ALARM_INTERVAL_SECS 10 #define ALARM_INTERVAL_SECS 10
static volatile bool test_uffdio_copy_eexist = true; static volatile bool test_uffdio_copy_eexist = true;
static volatile bool test_uffdio_zeropage_eexist = true; static volatile bool test_uffdio_zeropage_eexist = true;
/* Whether to test uffd write-protection */
static bool test_uffdio_wp = false;
static bool map_shared; static bool map_shared;
static int huge_fd; static int huge_fd;
...@@ -90,6 +93,7 @@ pthread_attr_t attr; ...@@ -90,6 +93,7 @@ pthread_attr_t attr;
struct uffd_stats { struct uffd_stats {
int cpu; int cpu;
unsigned long missing_faults; unsigned long missing_faults;
unsigned long wp_faults;
}; };
/* pthread_mutex_t starts at page offset 0 */ /* pthread_mutex_t starts at page offset 0 */
...@@ -139,9 +143,29 @@ static void uffd_stats_reset(struct uffd_stats *uffd_stats, ...@@ -139,9 +143,29 @@ static void uffd_stats_reset(struct uffd_stats *uffd_stats,
for (i = 0; i < n_cpus; i++) { for (i = 0; i < n_cpus; i++) {
uffd_stats[i].cpu = i; uffd_stats[i].cpu = i;
uffd_stats[i].missing_faults = 0; uffd_stats[i].missing_faults = 0;
uffd_stats[i].wp_faults = 0;
} }
} }
static void uffd_stats_report(struct uffd_stats *stats, int n_cpus)
{
int i;
unsigned long long miss_total = 0, wp_total = 0;
for (i = 0; i < n_cpus; i++) {
miss_total += stats[i].missing_faults;
wp_total += stats[i].wp_faults;
}
printf("userfaults: %llu missing (", miss_total);
for (i = 0; i < n_cpus; i++)
printf("%lu+", stats[i].missing_faults);
printf("\b), %llu wp (", wp_total);
for (i = 0; i < n_cpus; i++)
printf("%lu+", stats[i].wp_faults);
printf("\b)\n");
}
static int anon_release_pages(char *rel_area) static int anon_release_pages(char *rel_area)
{ {
int ret = 0; int ret = 0;
...@@ -262,10 +286,15 @@ struct uffd_test_ops { ...@@ -262,10 +286,15 @@ struct uffd_test_ops {
void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset); void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
}; };
#define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \ #define SHMEM_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
(1 << _UFFDIO_COPY) | \ (1 << _UFFDIO_COPY) | \
(1 << _UFFDIO_ZEROPAGE)) (1 << _UFFDIO_ZEROPAGE))
#define ANON_EXPECTED_IOCTLS ((1 << _UFFDIO_WAKE) | \
(1 << _UFFDIO_COPY) | \
(1 << _UFFDIO_ZEROPAGE) | \
(1 << _UFFDIO_WRITEPROTECT))
static struct uffd_test_ops anon_uffd_test_ops = { static struct uffd_test_ops anon_uffd_test_ops = {
.expected_ioctls = ANON_EXPECTED_IOCTLS, .expected_ioctls = ANON_EXPECTED_IOCTLS,
.allocate_area = anon_allocate_area, .allocate_area = anon_allocate_area,
...@@ -274,7 +303,7 @@ static struct uffd_test_ops anon_uffd_test_ops = { ...@@ -274,7 +303,7 @@ static struct uffd_test_ops anon_uffd_test_ops = {
}; };
static struct uffd_test_ops shmem_uffd_test_ops = { static struct uffd_test_ops shmem_uffd_test_ops = {
.expected_ioctls = ANON_EXPECTED_IOCTLS, .expected_ioctls = SHMEM_EXPECTED_IOCTLS,
.allocate_area = shmem_allocate_area, .allocate_area = shmem_allocate_area,
.release_pages = shmem_release_pages, .release_pages = shmem_release_pages,
.alias_mapping = noop_alias_mapping, .alias_mapping = noop_alias_mapping,
...@@ -298,6 +327,21 @@ static int my_bcmp(char *str1, char *str2, size_t n) ...@@ -298,6 +327,21 @@ static int my_bcmp(char *str1, char *str2, size_t n)
return 0; return 0;
} }
static void wp_range(int ufd, __u64 start, __u64 len, bool wp)
{
struct uffdio_writeprotect prms = { 0 };
/* Write protection page faults */
prms.range.start = start;
prms.range.len = len;
/* Undo write-protect, do wakeup after that */
prms.mode = wp ? UFFDIO_WRITEPROTECT_MODE_WP : 0;
if (ioctl(ufd, UFFDIO_WRITEPROTECT, &prms))
fprintf(stderr, "clear WP failed for address 0x%Lx\n",
start), exit(1);
}
static void *locking_thread(void *arg) static void *locking_thread(void *arg)
{ {
unsigned long cpu = (unsigned long) arg; unsigned long cpu = (unsigned long) arg;
...@@ -436,6 +480,9 @@ static int __copy_page(int ufd, unsigned long offset, bool retry) ...@@ -436,6 +480,9 @@ static int __copy_page(int ufd, unsigned long offset, bool retry)
uffdio_copy.dst = (unsigned long) area_dst + offset; uffdio_copy.dst = (unsigned long) area_dst + offset;
uffdio_copy.src = (unsigned long) area_src + offset; uffdio_copy.src = (unsigned long) area_src + offset;
uffdio_copy.len = page_size; uffdio_copy.len = page_size;
if (test_uffdio_wp)
uffdio_copy.mode = UFFDIO_COPY_MODE_WP;
else
uffdio_copy.mode = 0; uffdio_copy.mode = 0;
uffdio_copy.copy = 0; uffdio_copy.copy = 0;
if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) { if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
...@@ -493,6 +540,11 @@ static void uffd_handle_page_fault(struct uffd_msg *msg, ...@@ -493,6 +540,11 @@ static void uffd_handle_page_fault(struct uffd_msg *msg,
fprintf(stderr, "unexpected msg event %u\n", fprintf(stderr, "unexpected msg event %u\n",
msg->event), exit(1); msg->event), exit(1);
if (msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP) {
wp_range(uffd, msg->arg.pagefault.address, page_size, false);
stats->wp_faults++;
} else {
/* Missing page faults */
if (bounces & BOUNCE_VERIFY && if (bounces & BOUNCE_VERIFY &&
msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) msg->arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)
fprintf(stderr, "unexpected write fault\n"), exit(1); fprintf(stderr, "unexpected write fault\n"), exit(1);
...@@ -502,6 +554,7 @@ static void uffd_handle_page_fault(struct uffd_msg *msg, ...@@ -502,6 +554,7 @@ static void uffd_handle_page_fault(struct uffd_msg *msg,
if (copy_page(uffd, offset)) if (copy_page(uffd, offset))
stats->missing_faults++; stats->missing_faults++;
}
} }
static void *uffd_poll_thread(void *arg) static void *uffd_poll_thread(void *arg)
...@@ -587,11 +640,30 @@ static void *uffd_read_thread(void *arg) ...@@ -587,11 +640,30 @@ static void *uffd_read_thread(void *arg)
static void *background_thread(void *arg) static void *background_thread(void *arg)
{ {
unsigned long cpu = (unsigned long) arg; unsigned long cpu = (unsigned long) arg;
unsigned long page_nr; unsigned long page_nr, start_nr, mid_nr, end_nr;
for (page_nr = cpu * nr_pages_per_cpu; start_nr = cpu * nr_pages_per_cpu;
page_nr < (cpu+1) * nr_pages_per_cpu; end_nr = (cpu+1) * nr_pages_per_cpu;
page_nr++) mid_nr = (start_nr + end_nr) / 2;
/* Copy the first half of the pages */
for (page_nr = start_nr; page_nr < mid_nr; page_nr++)
copy_page_retry(uffd, page_nr * page_size);
/*
* If we need to test uffd-wp, set it up now. Then we'll have
* at least the first half of the pages mapped already which
* can be write-protected for testing
*/
if (test_uffdio_wp)
wp_range(uffd, (unsigned long)area_dst + start_nr * page_size,
nr_pages_per_cpu * page_size, true);
/*
* Continue the 2nd half of the page copying, handling write
* protection faults if any
*/
for (page_nr = mid_nr; page_nr < end_nr; page_nr++)
copy_page_retry(uffd, page_nr * page_size); copy_page_retry(uffd, page_nr * page_size);
return NULL; return NULL;
...@@ -753,17 +825,31 @@ static int faulting_process(int signal_test) ...@@ -753,17 +825,31 @@ static int faulting_process(int signal_test)
} }
for (nr = 0; nr < split_nr_pages; nr++) { for (nr = 0; nr < split_nr_pages; nr++) {
int steps = 1;
unsigned long offset = nr * page_size;
if (signal_test) { if (signal_test) {
if (sigsetjmp(*sigbuf, 1) != 0) { if (sigsetjmp(*sigbuf, 1) != 0) {
if (nr == lastnr) { if (steps == 1 && nr == lastnr) {
fprintf(stderr, "Signal repeated\n"); fprintf(stderr, "Signal repeated\n");
return 1; return 1;
} }
lastnr = nr; lastnr = nr;
if (signal_test == 1) { if (signal_test == 1) {
if (copy_page(uffd, nr * page_size)) if (steps == 1) {
/* This is a MISSING request */
steps++;
if (copy_page(uffd, offset))
signalled++; signalled++;
} else {
/* This is a WP request */
assert(steps == 2);
wp_range(uffd,
(__u64)area_dst +
offset,
page_size, false);
}
} else { } else {
signalled++; signalled++;
continue; continue;
...@@ -776,8 +862,13 @@ static int faulting_process(int signal_test) ...@@ -776,8 +862,13 @@ static int faulting_process(int signal_test)
fprintf(stderr, fprintf(stderr,
"nr %lu memory corruption %Lu %Lu\n", "nr %lu memory corruption %Lu %Lu\n",
nr, count, nr, count,
count_verify[nr]), exit(1); count_verify[nr]);
} }
/*
* Trigger write protection if there is by writting
* the same value back.
*/
*area_count(area_dst, nr) = count;
} }
if (signal_test) if (signal_test)
...@@ -799,6 +890,11 @@ static int faulting_process(int signal_test) ...@@ -799,6 +890,11 @@ static int faulting_process(int signal_test)
nr, count, nr, count,
count_verify[nr]), exit(1); count_verify[nr]), exit(1);
} }
/*
* Trigger write protection if there is by writting
* the same value back.
*/
*area_count(area_dst, nr) = count;
} }
if (uffd_test_ops->release_pages(area_dst)) if (uffd_test_ops->release_pages(area_dst))
...@@ -902,6 +998,8 @@ static int userfaultfd_zeropage_test(void) ...@@ -902,6 +998,8 @@ static int userfaultfd_zeropage_test(void)
uffdio_register.range.start = (unsigned long) area_dst; uffdio_register.range.start = (unsigned long) area_dst;
uffdio_register.range.len = nr_pages * page_size; uffdio_register.range.len = nr_pages * page_size;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if (test_uffdio_wp)
uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
fprintf(stderr, "register failure\n"), exit(1); fprintf(stderr, "register failure\n"), exit(1);
...@@ -947,6 +1045,8 @@ static int userfaultfd_events_test(void) ...@@ -947,6 +1045,8 @@ static int userfaultfd_events_test(void)
uffdio_register.range.start = (unsigned long) area_dst; uffdio_register.range.start = (unsigned long) area_dst;
uffdio_register.range.len = nr_pages * page_size; uffdio_register.range.len = nr_pages * page_size;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if (test_uffdio_wp)
uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
fprintf(stderr, "register failure\n"), exit(1); fprintf(stderr, "register failure\n"), exit(1);
...@@ -977,7 +1077,8 @@ static int userfaultfd_events_test(void) ...@@ -977,7 +1077,8 @@ static int userfaultfd_events_test(void)
return 1; return 1;
close(uffd); close(uffd);
printf("userfaults: %ld\n", stats.missing_faults);
uffd_stats_report(&stats, 1);
return stats.missing_faults != nr_pages; return stats.missing_faults != nr_pages;
} }
...@@ -1007,6 +1108,8 @@ static int userfaultfd_sig_test(void) ...@@ -1007,6 +1108,8 @@ static int userfaultfd_sig_test(void)
uffdio_register.range.start = (unsigned long) area_dst; uffdio_register.range.start = (unsigned long) area_dst;
uffdio_register.range.len = nr_pages * page_size; uffdio_register.range.len = nr_pages * page_size;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if (test_uffdio_wp)
uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
fprintf(stderr, "register failure\n"), exit(1); fprintf(stderr, "register failure\n"), exit(1);
...@@ -1139,6 +1242,8 @@ static int userfaultfd_stress(void) ...@@ -1139,6 +1242,8 @@ static int userfaultfd_stress(void)
uffdio_register.range.start = (unsigned long) area_dst; uffdio_register.range.start = (unsigned long) area_dst;
uffdio_register.range.len = nr_pages * page_size; uffdio_register.range.len = nr_pages * page_size;
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
if (test_uffdio_wp)
uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) { if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
fprintf(stderr, "register failure\n"); fprintf(stderr, "register failure\n");
return 1; return 1;
...@@ -1193,6 +1298,11 @@ static int userfaultfd_stress(void) ...@@ -1193,6 +1298,11 @@ static int userfaultfd_stress(void)
if (stress(uffd_stats)) if (stress(uffd_stats))
return 1; return 1;
/* Clear all the write protections if there is any */
if (test_uffdio_wp)
wp_range(uffd, (unsigned long)area_dst,
nr_pages * page_size, false);
/* unregister */ /* unregister */
if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) { if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
fprintf(stderr, "unregister failure\n"); fprintf(stderr, "unregister failure\n");
...@@ -1231,10 +1341,7 @@ static int userfaultfd_stress(void) ...@@ -1231,10 +1341,7 @@ static int userfaultfd_stress(void)
area_src_alias = area_dst_alias; area_src_alias = area_dst_alias;
area_dst_alias = tmp_area; area_dst_alias = tmp_area;
printf("userfaults:"); uffd_stats_report(uffd_stats, nr_cpus);
for (cpu = 0; cpu < nr_cpus; cpu++)
printf(" %lu", uffd_stats[cpu].missing_faults);
printf("\n");
} }
if (err) if (err)
...@@ -1274,6 +1381,8 @@ static void set_test_type(const char *type) ...@@ -1274,6 +1381,8 @@ static void set_test_type(const char *type)
if (!strcmp(type, "anon")) { if (!strcmp(type, "anon")) {
test_type = TEST_ANON; test_type = TEST_ANON;
uffd_test_ops = &anon_uffd_test_ops; uffd_test_ops = &anon_uffd_test_ops;
/* Only enable write-protect test for anonymous test */
test_uffdio_wp = true;
} else if (!strcmp(type, "hugetlb")) { } else if (!strcmp(type, "hugetlb")) {
test_type = TEST_HUGETLB; test_type = TEST_HUGETLB;
uffd_test_ops = &hugetlb_uffd_test_ops; uffd_test_ops = &hugetlb_uffd_test_ops;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment