Merge tag 'linux-kselftest-next-5.11-rc1' of git://git.kernel.org/pub/scm/linux/kerne...
[platform/kernel/linux-starfive.git] / tools / testing / selftests / vm / userfaultfd.c
index 9132fae..92b8ec4 100644 (file)
@@ -55,6 +55,8 @@
 #include <setjmp.h>
 #include <stdbool.h>
 #include <assert.h>
+#include <inttypes.h>
+#include <stdint.h>
 
 #include "../kselftest.h"
 
@@ -135,6 +137,13 @@ static void usage(void)
        exit(1);
 }
 
+#define uffd_error(code, fmt, ...)                                             \
+       do {                                                                   \
+               fprintf(stderr, fmt, ##__VA_ARGS__);                           \
+               fprintf(stderr, ": %" PRId64 "\n", (int64_t)(code));           \
+               exit(1);                                                       \
+       } while (0)
+
 static void uffd_stats_reset(struct uffd_stats *uffd_stats,
                             unsigned long n_cpus)
 {
@@ -206,19 +215,19 @@ static int hugetlb_release_pages(char *rel_area)
        return ret;
 }
 
-
 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_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;
+               perror("mmap of hugetlbfs file failed");
+               goto fail;
        }
 
        if (map_shared) {
@@ -227,14 +236,11 @@ static void hugetlb_allocate_area(void **alloc_area)
                                  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;
+                       perror("mmap of hugetlb file alias failed");
+                       goto fail_munmap;
                }
        }
+
        if (*alloc_area == area_src) {
                huge_fd_off0 = *alloc_area;
                alloc_area_alias = &area_src_alias;
@@ -243,6 +249,16 @@ static void hugetlb_allocate_area(void **alloc_area)
        }
        if (area_alias)
                *alloc_area_alias = area_alias;
+
+       return;
+
+fail_munmap:
+       if (munmap(*alloc_area, nr_pages * page_size) < 0) {
+               perror("hugetlb munmap");
+               exit(1);
+       }
+fail:
+       *alloc_area = NULL;
 }
 
 static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset)
@@ -331,7 +347,7 @@ static int my_bcmp(char *str1, char *str2, size_t n)
 
 static void wp_range(int ufd, __u64 start, __u64 len, bool wp)
 {
-       struct uffdio_writeprotect prms = { 0 };
+       struct uffdio_writeprotect prms;
 
        /* Write protection page faults */
        prms.range.start = start;
@@ -340,7 +356,8 @@ static void wp_range(int ufd, __u64 start, __u64 len, bool wp)
        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);
+               fprintf(stderr, "clear WP failed for address 0x%" PRIx64 "\n",
+                       (uint64_t)start);
                exit(1);
        }
 }
@@ -474,14 +491,11 @@ static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
        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);
+                       uffd_error(uffdio_copy->copy,
+                                  "UFFDIO_COPY retry error");
                }
-       } else {
-               fprintf(stderr, "UFFDIO_COPY retry unexpected %Ld\n",
-                       uffdio_copy->copy); exit(1);
-       }
+       } else
+               uffd_error(uffdio_copy->copy, "UFFDIO_COPY retry unexpected");
 }
 
 static int __copy_page(int ufd, unsigned long offset, bool retry)
@@ -502,14 +516,10 @@ static int __copy_page(int ufd, unsigned long offset, bool retry)
        uffdio_copy.copy = 0;
        if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
                /* real retval in ufdio_copy.copy */
-               if (uffdio_copy.copy != -EEXIST) {
-                       fprintf(stderr, "UFFDIO_COPY error %Ld\n",
-                               uffdio_copy.copy);
-                       exit(1);
-               }
+               if (uffdio_copy.copy != -EEXIST)
+                       uffd_error(uffdio_copy.copy, "UFFDIO_COPY error");
        } else if (uffdio_copy.copy != page_size) {
-               fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
-                       uffdio_copy.copy); exit(1);
+               uffd_error(uffdio_copy.copy, "UFFDIO_COPY unexpected copy");
        } else {
                if (test_uffdio_copy_eexist && retry) {
                        test_uffdio_copy_eexist = false;
@@ -784,11 +794,13 @@ static int userfaultfd_open(int features)
        uffdio_api.api = UFFD_API;
        uffdio_api.features = features;
        if (ioctl(uffd, UFFDIO_API, &uffdio_api)) {
-               fprintf(stderr, "UFFDIO_API\n");
+               fprintf(stderr, "UFFDIO_API failed.\nPlease make sure to "
+                       "run with either root or ptrace capability.\n");
                return 1;
        }
        if (uffdio_api.api != UFFD_API) {
-               fprintf(stderr, "UFFDIO_API error %Lu\n", uffdio_api.api);
+               fprintf(stderr, "UFFDIO_API error: %" PRIu64 "\n",
+                       (uint64_t)uffdio_api.api);
                return 1;
        }
 
@@ -950,13 +962,12 @@ static void retry_uffdio_zeropage(int ufd,
                                     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);
+                       uffd_error(uffdio_zeropage->zeropage,
+                                  "UFFDIO_ZEROPAGE retry error");
                }
        } else {
-               fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
-                       uffdio_zeropage->zeropage); exit(1);
+               uffd_error(uffdio_zeropage->zeropage,
+                          "UFFDIO_ZEROPAGE retry unexpected");
        }
 }
 
@@ -965,6 +976,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
        struct uffdio_zeropage uffdio_zeropage;
        int ret;
        unsigned long has_zeropage;
+       __s64 res;
 
        has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
 
@@ -976,29 +988,17 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
        uffdio_zeropage.range.len = page_size;
        uffdio_zeropage.mode = 0;
        ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
+       res = uffdio_zeropage.zeropage;
        if (ret) {
                /* real retval in ufdio_zeropage.zeropage */
                if (has_zeropage) {
-                       if (uffdio_zeropage.zeropage == -EEXIST) {
-                               fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n");
-                               exit(1);
-                       } else {
-                               fprintf(stderr, "UFFDIO_ZEROPAGE error %Ld\n",
-                                       uffdio_zeropage.zeropage);
-                               exit(1);
-                       }
-               } else {
-                       if (uffdio_zeropage.zeropage != -EINVAL) {
-                               fprintf(stderr,
-                                       "UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
-                                       uffdio_zeropage.zeropage);
-                               exit(1);
-                       }
-               }
+                       uffd_error(res, "UFFDIO_ZEROPAGE %s",
+                                  res == -EEXIST ? "-EEXIST" : "error");
+               } else if (res != -EINVAL)
+                       uffd_error(res, "UFFDIO_ZEROPAGE not -EINVAL");
        } else if (has_zeropage) {
-               if (uffdio_zeropage.zeropage != page_size) {
-                       fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
-                               uffdio_zeropage.zeropage); exit(1);
+               if (res != page_size) {
+                       uffd_error(res, "UFFDIO_ZEROPAGE unexpected");
                } else {
                        if (test_uffdio_zeropage_eexist && retry) {
                                test_uffdio_zeropage_eexist = false;
@@ -1007,11 +1007,8 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
                        }
                        return 1;
                }
-       } else {
-               fprintf(stderr,
-                       "UFFDIO_ZEROPAGE succeeded %Ld\n",
-                       uffdio_zeropage.zeropage); exit(1);
-       }
+       } else
+               uffd_error(res, "UFFDIO_ZEROPAGE succeeded");
 
        return 0;
 }
@@ -1033,7 +1030,7 @@ static int userfaultfd_zeropage_test(void)
        if (uffd_test_ops->release_pages(area_dst))
                return 1;
 
-       if (userfaultfd_open(0) < 0)
+       if (userfaultfd_open(0))
                return 1;
        uffdio_register.range.start = (unsigned long) area_dst;
        uffdio_register.range.len = nr_pages * page_size;
@@ -1083,7 +1080,7 @@ static int userfaultfd_events_test(void)
 
        features = UFFD_FEATURE_EVENT_FORK | UFFD_FEATURE_EVENT_REMAP |
                UFFD_FEATURE_EVENT_REMOVE;
-       if (userfaultfd_open(features) < 0)
+       if (userfaultfd_open(features))
                return 1;
        fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -1155,7 +1152,7 @@ static int userfaultfd_sig_test(void)
                return 1;
 
        features = UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_SIGBUS;
-       if (userfaultfd_open(features) < 0)
+       if (userfaultfd_open(features))
                return 1;
        fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
 
@@ -1235,7 +1232,7 @@ static int userfaultfd_stress(void)
        if (!area_dst)
                return 1;
 
-       if (userfaultfd_open(0) < 0)
+       if (userfaultfd_open(0))
                return 1;
 
        count_verify = malloc(nr_pages * sizeof(unsigned long long));
@@ -1295,6 +1292,8 @@ static int userfaultfd_stress(void)
                        printf(" ver");
                if (bounces & BOUNCE_POLL)
                        printf(" poll");
+               else
+                       printf(" read");
                printf(", ");
                fflush(stdout);