userfaultfd: selftest: exercise UFFDIO_COPY/ZEROPAGE -EEXIST
[sfrench/cifs-2.6.git] / tools / testing / selftests / vm / userfaultfd.c
index 1eae79ae5b4e93f8862732b13fd2bd57d08d168f..4549ae425f3ecec636b140dc26539e171a2c1275 100644 (file)
@@ -66,6 +66,8 @@
 #include <sys/wait.h>
 #include <pthread.h>
 #include <linux/userfaultfd.h>
+#include <setjmp.h>
+#include <stdbool.h>
 
 #ifdef __NR_userfaultfd
 
@@ -82,11 +84,17 @@ static int bounces;
 #define TEST_SHMEM     3
 static int test_type;
 
+/* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
+#define ALARM_INTERVAL_SECS 10
+static volatile bool test_uffdio_copy_eexist = true;
+static volatile bool test_uffdio_zeropage_eexist = true;
+
+static bool map_shared;
 static int huge_fd;
 static char *huge_fd_off0;
 static unsigned long long *count_verify;
 static int uffd, uffd_flags, finished, *pipefd;
-static char *area_src, *area_dst;
+static char *area_src, *area_src_alias, *area_dst, *area_dst_alias;
 static char *zeropage;
 pthread_attr_t attr;
 
@@ -125,6 +133,9 @@ static void anon_allocate_area(void **alloc_area)
        }
 }
 
+static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset)
+{
+}
 
 /* HugeTLB memory */
 static int hugetlb_release_pages(char *rel_area)
@@ -145,17 +156,51 @@ static int hugetlb_release_pages(char *rel_area)
 
 static void hugetlb_allocate_area(void **alloc_area)
 {
+       void *area_alias = NULL;
+       char **alloc_area_alias;
        *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
-                               MAP_PRIVATE | MAP_HUGETLB, huge_fd,
-                               *alloc_area == area_src ? 0 :
-                               nr_pages * page_size);
+                          (map_shared ? MAP_SHARED : MAP_PRIVATE) |
+                          MAP_HUGETLB,
+                          huge_fd, *alloc_area == area_src ? 0 :
+                          nr_pages * page_size);
        if (*alloc_area == MAP_FAILED) {
                fprintf(stderr, "mmap of hugetlbfs file failed\n");
                *alloc_area = NULL;
        }
 
-       if (*alloc_area == area_src)
+       if (map_shared) {
+               area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE,
+                                 MAP_SHARED | MAP_HUGETLB,
+                                 huge_fd, *alloc_area == area_src ? 0 :
+                                 nr_pages * page_size);
+               if (area_alias == MAP_FAILED) {
+                       if (munmap(*alloc_area, nr_pages * page_size) < 0)
+                               perror("hugetlb munmap"), exit(1);
+                       *alloc_area = NULL;
+                       return;
+               }
+       }
+       if (*alloc_area == area_src) {
                huge_fd_off0 = *alloc_area;
+               alloc_area_alias = &area_src_alias;
+       } else {
+               alloc_area_alias = &area_dst_alias;
+       }
+       if (area_alias)
+               *alloc_area_alias = area_alias;
+}
+
+static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
+{
+       if (!map_shared)
+               return;
+       /*
+        * We can't zap just the pagetable with hugetlbfs because
+        * MADV_DONTEED won't work. So exercise -EEXIST on a alias
+        * mapping where the pagetables are not established initially,
+        * this way we'll exercise the -EEXEC at the fs level.
+        */
+       *start = (unsigned long) area_dst_alias + offset;
 }
 
 /* Shared memory */
@@ -185,6 +230,7 @@ struct uffd_test_ops {
        unsigned long expected_ioctls;
        void (*allocate_area)(void **alloc_area);
        int (*release_pages)(char *rel_area);
+       void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
 };
 
 #define ANON_EXPECTED_IOCTLS           ((1 << _UFFDIO_WAKE) | \
@@ -195,18 +241,21 @@ static struct uffd_test_ops anon_uffd_test_ops = {
        .expected_ioctls = ANON_EXPECTED_IOCTLS,
        .allocate_area  = anon_allocate_area,
        .release_pages  = anon_release_pages,
+       .alias_mapping = noop_alias_mapping,
 };
 
 static struct uffd_test_ops shmem_uffd_test_ops = {
-       .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
+       .expected_ioctls = ANON_EXPECTED_IOCTLS,
        .allocate_area  = shmem_allocate_area,
        .release_pages  = shmem_release_pages,
+       .alias_mapping = noop_alias_mapping,
 };
 
 static struct uffd_test_ops hugetlb_uffd_test_ops = {
        .expected_ioctls = UFFD_API_RANGE_IOCTLS_BASIC,
        .allocate_area  = hugetlb_allocate_area,
        .release_pages  = hugetlb_release_pages,
+       .alias_mapping = hugetlb_alias_mapping,
 };
 
 static struct uffd_test_ops *uffd_test_ops;
@@ -331,6 +380,23 @@ static void *locking_thread(void *arg)
        return NULL;
 }
 
+static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
+                           unsigned long offset)
+{
+       uffd_test_ops->alias_mapping(&uffdio_copy->dst,
+                                    uffdio_copy->len,
+                                    offset);
+       if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
+               /* real retval in ufdio_copy.copy */
+               if (uffdio_copy->copy != -EEXIST)
+                       fprintf(stderr, "UFFDIO_COPY retry error %Ld\n",
+                               uffdio_copy->copy), exit(1);
+       } else {
+               fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
+                       uffdio_copy->copy), exit(1);
+       }
+}
+
 static int copy_page(int ufd, unsigned long offset)
 {
        struct uffdio_copy uffdio_copy;
@@ -351,8 +417,13 @@ static int copy_page(int ufd, unsigned long offset)
        } else if (uffdio_copy.copy != page_size) {
                fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
                        uffdio_copy.copy), exit(1);
-       } else
+       } else {
+               if (test_uffdio_copy_eexist) {
+                       test_uffdio_copy_eexist = false;
+                       retry_copy_page(ufd, &uffdio_copy, offset);
+               }
                return 1;
+       }
        return 0;
 }
 
@@ -408,6 +479,7 @@ static void *uffd_poll_thread(void *arg)
                                userfaults++;
                        break;
                case UFFD_EVENT_FORK:
+                       close(uffd);
                        uffd = msg.arg.fork.ufd;
                        pollfd[0].fd = uffd;
                        break;
@@ -572,6 +644,17 @@ static int userfaultfd_open(int features)
        return 0;
 }
 
+sigjmp_buf jbuf, *sigbuf;
+
+static void sighndl(int sig, siginfo_t *siginfo, void *ptr)
+{
+       if (sig == SIGBUS) {
+               if (sigbuf)
+                       siglongjmp(*sigbuf, 1);
+               abort();
+       }
+}
+
 /*
  * For non-cooperative userfaultfd test we fork() a process that will
  * generate pagefaults, will mremap the area monitored by the
@@ -585,19 +668,59 @@ static int userfaultfd_open(int features)
  * The release of the pages currently generates event for shmem and
  * anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked
  * for hugetlb.
+ * For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register
+ * monitored area, generate pagefaults and test that signal is delivered.
+ * Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2
+ * test robustness use case - we release monitored area, fork a process
+ * that will generate pagefaults and verify signal is generated.
+ * This also tests UFFD_FEATURE_EVENT_FORK event along with the signal
+ * feature. Using monitor thread, verify no userfault events are generated.
  */
-static int faulting_process(void)
+static int faulting_process(int signal_test)
 {
        unsigned long nr;
        unsigned long long count;
        unsigned long split_nr_pages;
+       unsigned long lastnr;
+       struct sigaction act;
+       unsigned long signalled = 0;
 
        if (test_type != TEST_HUGETLB)
                split_nr_pages = (nr_pages + 1) / 2;
        else
                split_nr_pages = nr_pages;
 
+       if (signal_test) {
+               sigbuf = &jbuf;
+               memset(&act, 0, sizeof(act));
+               act.sa_sigaction = sighndl;
+               act.sa_flags = SA_SIGINFO;
+               if (sigaction(SIGBUS, &act, 0)) {
+                       perror("sigaction");
+                       return 1;
+               }
+               lastnr = (unsigned long)-1;
+       }
+
        for (nr = 0; nr < split_nr_pages; nr++) {
+               if (signal_test) {
+                       if (sigsetjmp(*sigbuf, 1) != 0) {
+                               if (nr == lastnr) {
+                                       fprintf(stderr, "Signal repeated\n");
+                                       return 1;
+                               }
+
+                               lastnr = nr;
+                               if (signal_test == 1) {
+                                       if (copy_page(uffd, nr * page_size))
+                                               signalled++;
+                               } else {
+                                       signalled++;
+                                       continue;
+                               }
+                       }
+               }
+
                count = *area_count(area_dst, nr);
                if (count != count_verify[nr]) {
                        fprintf(stderr,
@@ -607,6 +730,9 @@ static int faulting_process(void)
                }
        }
 
+       if (signal_test)
+               return signalled != split_nr_pages;
+
        if (test_type == TEST_HUGETLB)
                return 0;
 
@@ -636,6 +762,23 @@ static int faulting_process(void)
        return 0;
 }
 
+static void retry_uffdio_zeropage(int ufd,
+                                 struct uffdio_zeropage *uffdio_zeropage,
+                                 unsigned long offset)
+{
+       uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start,
+                                    uffdio_zeropage->range.len,
+                                    offset);
+       if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
+               if (uffdio_zeropage->zeropage != -EEXIST)
+                       fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n",
+                               uffdio_zeropage->zeropage), exit(1);
+       } else {
+               fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
+                       uffdio_zeropage->zeropage), exit(1);
+       }
+}
+
 static int uffdio_zeropage(int ufd, unsigned long offset)
 {
        struct uffdio_zeropage uffdio_zeropage;
@@ -670,8 +813,14 @@ static int uffdio_zeropage(int ufd, unsigned long offset)
                if (uffdio_zeropage.zeropage != page_size) {
                        fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
                                uffdio_zeropage.zeropage), exit(1);
-               } else
+               } else {
+                       if (test_uffdio_zeropage_eexist) {
+                               test_uffdio_zeropage_eexist = false;
+                               retry_uffdio_zeropage(ufd, &uffdio_zeropage,
+                                                     offset);
+                       }
                        return 1;
+               }
        } else {
                fprintf(stderr,
                        "UFFDIO_ZEROPAGE succeeded %Ld\n",
@@ -761,7 +910,7 @@ static int userfaultfd_events_test(void)
                perror("fork"), exit(1);
 
        if (!pid)
-               return faulting_process();
+               return faulting_process(0);
 
        waitpid(pid, &err, 0);
        if (err)
@@ -778,6 +927,70 @@ static int userfaultfd_events_test(void)
        return userfaults != nr_pages;
 }
 
+static int userfaultfd_sig_test(void)
+{
+       struct uffdio_register uffdio_register;
+       unsigned long expected_ioctls;
+       unsigned long userfaults;
+       pthread_t uffd_mon;
+       int err, features;
+       pid_t pid;
+       char c;
+
+       printf("testing signal delivery: ");
+       fflush(stdout);
+
+       if (uffd_test_ops->release_pages(area_dst))
+               return 1;
+
+       features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
+       if (userfaultfd_open(features) < 0)
+               return 1;
+       fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
+
+       uffdio_register.range.start = (unsigned long) area_dst;
+       uffdio_register.range.len = nr_pages * page_size;
+       uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
+       if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register))
+               fprintf(stderr, "register failure\n"), exit(1);
+
+       expected_ioctls = uffd_test_ops->expected_ioctls;
+       if ((uffdio_register.ioctls & expected_ioctls) !=
+           expected_ioctls)
+               fprintf(stderr,
+                       "unexpected missing ioctl for anon memory\n"),
+                       exit(1);
+
+       if (faulting_process(1))
+               fprintf(stderr, "faulting process failed\n"), exit(1);
+
+       if (uffd_test_ops->release_pages(area_dst))
+               return 1;
+
+       if (pthread_create(&uffd_mon, &attr, uffd_poll_thread, NULL))
+               perror("uffd_poll_thread create"), exit(1);
+
+       pid = fork();
+       if (pid < 0)
+               perror("fork"), exit(1);
+
+       if (!pid)
+               exit(faulting_process(2));
+
+       waitpid(pid, &err, 0);
+       if (err)
+               fprintf(stderr, "faulting process failed\n"), exit(1);
+
+       if (write(pipefd[1], &c, sizeof(c)) != sizeof(c))
+               perror("pipe write"), exit(1);
+       if (pthread_join(uffd_mon, (void **)&userfaults))
+               return 1;
+
+       printf("done.\n");
+       printf(" Signal test userfaults: %ld\n", userfaults);
+       close(uffd);
+       return userfaults != 0;
+}
 static int userfaultfd_stress(void)
 {
        void *area;
@@ -879,6 +1092,15 @@ static int userfaultfd_stress(void)
                        return 1;
                }
 
+               if (area_dst_alias) {
+                       uffdio_register.range.start = (unsigned long)
+                               area_dst_alias;
+                       if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) {
+                               fprintf(stderr, "register failure alias\n");
+                               return 1;
+                       }
+               }
+
                /*
                 * The madvise done previously isn't enough: some
                 * uffd_thread could have read userfaults (one of
@@ -912,9 +1134,17 @@ static int userfaultfd_stress(void)
 
                /* unregister */
                if (ioctl(uffd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
-                       fprintf(stderr, "register failure\n");
+                       fprintf(stderr, "unregister failure\n");
                        return 1;
                }
+               if (area_dst_alias) {
+                       uffdio_register.range.start = (unsigned long) area_dst;
+                       if (ioctl(uffd, UFFDIO_UNREGISTER,
+                                 &uffdio_register.range)) {
+                               fprintf(stderr, "unregister failure alias\n");
+                               return 1;
+                       }
+               }
 
                /* verification */
                if (bounces & BOUNCE_VERIFY) {
@@ -936,6 +1166,10 @@ static int userfaultfd_stress(void)
                area_src = area_dst;
                area_dst = tmp_area;
 
+               tmp_area = area_src_alias;
+               area_src_alias = area_dst_alias;
+               area_dst_alias = tmp_area;
+
                printf("userfaults:");
                for (cpu = 0; cpu < nr_cpus; cpu++)
                        printf(" %lu", userfaults[cpu]);
@@ -946,7 +1180,8 @@ static int userfaultfd_stress(void)
                return err;
 
        close(uffd);
-       return userfaultfd_zeropage_test() || userfaultfd_events_test();
+       return userfaultfd_zeropage_test() || userfaultfd_sig_test()
+               || userfaultfd_events_test();
 }
 
 /*
@@ -981,7 +1216,12 @@ static void set_test_type(const char *type)
        } else if (!strcmp(type, "hugetlb")) {
                test_type = TEST_HUGETLB;
                uffd_test_ops = &hugetlb_uffd_test_ops;
+       } else if (!strcmp(type, "hugetlb_shared")) {
+               map_shared = true;
+               test_type = TEST_HUGETLB;
+               uffd_test_ops = &hugetlb_uffd_test_ops;
        } else if (!strcmp(type, "shmem")) {
+               map_shared = true;
                test_type = TEST_SHMEM;
                uffd_test_ops = &shmem_uffd_test_ops;
        } else {
@@ -1001,12 +1241,25 @@ static void set_test_type(const char *type)
                fprintf(stderr, "Impossible to run this test\n"), exit(2);
 }
 
+static void sigalrm(int sig)
+{
+       if (sig != SIGALRM)
+               abort();
+       test_uffdio_copy_eexist = true;
+       test_uffdio_zeropage_eexist = true;
+       alarm(ALARM_INTERVAL_SECS);
+}
+
 int main(int argc, char **argv)
 {
        if (argc < 4)
                fprintf(stderr, "Usage: <test type> <MiB> <bounces> [hugetlbfs_file]\n"),
                                exit(1);
 
+       if (signal(SIGALRM, sigalrm) == SIG_ERR)
+               fprintf(stderr, "failed to arm SIGALRM"), exit(1);
+       alarm(ALARM_INTERVAL_SECS);
+
        set_test_type(argv[1]);
 
        nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);