Commit 77f962e7 authored by Axel Rasmussen's avatar Axel Rasmussen Committed by Linus Torvalds

userfaultfd: selftests: make __{s,u}64 format specifiers portable

On certain platforms (powerpcle is the one on which I ran into this),
"%Ld" and "%Lu" are unsuitable for printing __s64 and __u64, respectively,
resulting in build warnings.  Cast to {u,}int64_t, and use the PRI{d,u}64
macros defined in inttypes.h to print them.  This ought to be portable to
all platforms.

Splitting this off into a separate macro lets us remove some lines, and
get rid of some (I would argue) stylistically odd cases where we joined
printf() and exit() into a single statement with a ,.

Finally, this also fixes a "missing braces around initializer" warning
when we initialize prms in wp_range().

[axelrasmussen@google.com: v2]
  Link: https://lkml.kernel.org/r/20201203180244.1811601-1-axelrasmussen@google.com

Link: https://lkml.kernel.org/r/20201202211542.1121189-1-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
Acked-by: default avatarPeter Xu <peterx@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Joe Perches <joe@perches.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: David Alan Gilbert <dgilbert@redhat.com>
Cc: Greg Thelen <gthelen@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d0d4730a
...@@ -55,6 +55,8 @@ ...@@ -55,6 +55,8 @@
#include <setjmp.h> #include <setjmp.h>
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include "../kselftest.h" #include "../kselftest.h"
...@@ -135,6 +137,13 @@ static void usage(void) ...@@ -135,6 +137,13 @@ static void usage(void)
exit(1); 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, static void uffd_stats_reset(struct uffd_stats *uffd_stats,
unsigned long n_cpus) unsigned long n_cpus)
{ {
...@@ -338,7 +347,7 @@ static int my_bcmp(char *str1, char *str2, size_t n) ...@@ -338,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) 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 */ /* Write protection page faults */
prms.range.start = start; prms.range.start = start;
...@@ -347,7 +356,8 @@ static void wp_range(int ufd, __u64 start, __u64 len, bool wp) ...@@ -347,7 +356,8 @@ static void wp_range(int ufd, __u64 start, __u64 len, bool wp)
prms.mode = wp ? UFFDIO_WRITEPROTECT_MODE_WP : 0; prms.mode = wp ? UFFDIO_WRITEPROTECT_MODE_WP : 0;
if (ioctl(ufd, UFFDIO_WRITEPROTECT, &prms)) { 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); exit(1);
} }
} }
...@@ -481,14 +491,11 @@ static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy, ...@@ -481,14 +491,11 @@ static void retry_copy_page(int ufd, struct uffdio_copy *uffdio_copy,
if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) { if (ioctl(ufd, UFFDIO_COPY, uffdio_copy)) {
/* real retval in ufdio_copy.copy */ /* real retval in ufdio_copy.copy */
if (uffdio_copy->copy != -EEXIST) { if (uffdio_copy->copy != -EEXIST) {
fprintf(stderr, "UFFDIO_COPY retry error %Ld\n", uffd_error(uffdio_copy->copy,
uffdio_copy->copy); "UFFDIO_COPY retry error");
exit(1);
}
} 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) static int __copy_page(int ufd, unsigned long offset, bool retry)
...@@ -509,14 +516,10 @@ static int __copy_page(int ufd, unsigned long offset, bool retry) ...@@ -509,14 +516,10 @@ static int __copy_page(int ufd, unsigned long offset, bool retry)
uffdio_copy.copy = 0; uffdio_copy.copy = 0;
if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) { if (ioctl(ufd, UFFDIO_COPY, &uffdio_copy)) {
/* real retval in ufdio_copy.copy */ /* real retval in ufdio_copy.copy */
if (uffdio_copy.copy != -EEXIST) { if (uffdio_copy.copy != -EEXIST)
fprintf(stderr, "UFFDIO_COPY error %Ld\n", uffd_error(uffdio_copy.copy, "UFFDIO_COPY error");
uffdio_copy.copy);
exit(1);
}
} else if (uffdio_copy.copy != page_size) { } else if (uffdio_copy.copy != page_size) {
fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n", uffd_error(uffdio_copy.copy, "UFFDIO_COPY unexpected copy");
uffdio_copy.copy); exit(1);
} else { } else {
if (test_uffdio_copy_eexist && retry) { if (test_uffdio_copy_eexist && retry) {
test_uffdio_copy_eexist = false; test_uffdio_copy_eexist = false;
...@@ -795,7 +798,8 @@ static int userfaultfd_open(int features) ...@@ -795,7 +798,8 @@ static int userfaultfd_open(int features)
return 1; return 1;
} }
if (uffdio_api.api != UFFD_API) { 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; return 1;
} }
...@@ -957,13 +961,12 @@ static void retry_uffdio_zeropage(int ufd, ...@@ -957,13 +961,12 @@ static void retry_uffdio_zeropage(int ufd,
offset); offset);
if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) { if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) {
if (uffdio_zeropage->zeropage != -EEXIST) { if (uffdio_zeropage->zeropage != -EEXIST) {
fprintf(stderr, "UFFDIO_ZEROPAGE retry error %Ld\n", uffd_error(uffdio_zeropage->zeropage,
uffdio_zeropage->zeropage); "UFFDIO_ZEROPAGE retry error");
exit(1);
} }
} else { } else {
fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n", uffd_error(uffdio_zeropage->zeropage,
uffdio_zeropage->zeropage); exit(1); "UFFDIO_ZEROPAGE retry unexpected");
} }
} }
...@@ -972,6 +975,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) ...@@ -972,6 +975,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
struct uffdio_zeropage uffdio_zeropage; struct uffdio_zeropage uffdio_zeropage;
int ret; int ret;
unsigned long has_zeropage; unsigned long has_zeropage;
__s64 res;
has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE); has_zeropage = uffd_test_ops->expected_ioctls & (1 << _UFFDIO_ZEROPAGE);
...@@ -983,29 +987,17 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) ...@@ -983,29 +987,17 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
uffdio_zeropage.range.len = page_size; uffdio_zeropage.range.len = page_size;
uffdio_zeropage.mode = 0; uffdio_zeropage.mode = 0;
ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage); ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
res = uffdio_zeropage.zeropage;
if (ret) { if (ret) {
/* real retval in ufdio_zeropage.zeropage */ /* real retval in ufdio_zeropage.zeropage */
if (has_zeropage) { if (has_zeropage) {
if (uffdio_zeropage.zeropage == -EEXIST) { uffd_error(res, "UFFDIO_ZEROPAGE %s",
fprintf(stderr, "UFFDIO_ZEROPAGE -EEXIST\n"); res == -EEXIST ? "-EEXIST" : "error");
exit(1); } else if (res != -EINVAL)
} else { uffd_error(res, "UFFDIO_ZEROPAGE not -EINVAL");
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);
}
}
} else if (has_zeropage) { } else if (has_zeropage) {
if (uffdio_zeropage.zeropage != page_size) { if (res != page_size) {
fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n", uffd_error(res, "UFFDIO_ZEROPAGE unexpected");
uffdio_zeropage.zeropage); exit(1);
} else { } else {
if (test_uffdio_zeropage_eexist && retry) { if (test_uffdio_zeropage_eexist && retry) {
test_uffdio_zeropage_eexist = false; test_uffdio_zeropage_eexist = false;
...@@ -1014,11 +1006,8 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) ...@@ -1014,11 +1006,8 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
} }
return 1; return 1;
} }
} else { } else
fprintf(stderr, uffd_error(res, "UFFDIO_ZEROPAGE succeeded");
"UFFDIO_ZEROPAGE succeeded %Ld\n",
uffdio_zeropage.zeropage); exit(1);
}
return 0; return 0;
} }
......
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