linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable
@ 2020-12-02 21:15 Axel Rasmussen
  2020-12-02 23:09 ` Peter Xu
  0 siblings, 1 reply; 5+ messages in thread
From: Axel Rasmussen @ 2020-12-02 21:15 UTC (permalink / raw)
  To: Shuah Khan, Peter Xu, Andrew Morton, Joe Perches, Mike Rapoport,
	Andrea Arcangeli, David Alan Gilbert
  Cc: Greg Thelen, linux-kselftest, linux-kernel, Axel Rasmussen

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 a build warning. 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 function 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().

Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
 tools/testing/selftests/vm/userfaultfd.c | 77 +++++++++++-------------
 1 file changed, 36 insertions(+), 41 deletions(-)

diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c
index 9b0912a01777..31e1ff887e4b 100644
--- a/tools/testing/selftests/vm/userfaultfd.c
+++ b/tools/testing/selftests/vm/userfaultfd.c
@@ -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,12 @@ static void usage(void)
 	exit(1);
 }
 
+static void uffd_error(const char *message, __s64 code)
+{
+	fprintf(stderr, "%s: %" PRId64 "\n", message, (int64_t)code);
+	exit(1);
+}
+
 static void uffd_stats_reset(struct uffd_stats *uffd_stats,
 			     unsigned long n_cpus)
 {
@@ -331,7 +339,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 +348,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 +483,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 retry error",
+				   uffdio_copy->copy);
 		}
-	} else {
-		fprintf(stderr,	"UFFDIO_COPY retry unexpected %Ld\n",
-			uffdio_copy->copy); exit(1);
-	}
+	} else
+		uffd_error("UFFDIO_COPY retry unexpected", uffdio_copy->copy);
 }
 
 static int __copy_page(int ufd, unsigned long offset, bool retry)
@@ -502,15 +508,11 @@ 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);
-		}
-	} else if (uffdio_copy.copy != page_size) {
-		fprintf(stderr, "UFFDIO_COPY unexpected copy %Ld\n",
-			uffdio_copy.copy); exit(1);
-	} else {
+		if (uffdio_copy.copy != -EEXIST)
+			uffd_error("UFFDIO_COPY error", uffdio_copy.copy);
+	} else if (uffdio_copy.copy != page_size)
+		uffd_error("UFFDIO_COPY unexpected copy", uffdio_copy.copy);
+	else {
 		if (test_uffdio_copy_eexist && retry) {
 			test_uffdio_copy_eexist = false;
 			retry_copy_page(ufd, &uffdio_copy, offset);
@@ -788,7 +790,8 @@ static int userfaultfd_open(int features)
 		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 +953,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 retry error",
+				   uffdio_zeropage->zeropage);
 		}
 	} else {
-		fprintf(stderr, "UFFDIO_ZEROPAGE retry unexpected %Ld\n",
-			uffdio_zeropage->zeropage); exit(1);
+		uffd_error("UFFDIO_ZEROPAGE retry unexpected",
+			   uffdio_zeropage->zeropage);
 	}
 }
 
@@ -979,26 +981,20 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
 	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);
-			}
+			uffd_error(uffdio_zeropage.zeropage == -EEXIST ?
+						 "UFFDIO_ZEROPAGE -EEXIST" :
+						 "UFFDIO_ZEROPAGE error",
+				   uffdio_zeropage.zeropage);
 		} else {
 			if (uffdio_zeropage.zeropage != -EINVAL) {
-				fprintf(stderr,
-					"UFFDIO_ZEROPAGE not -EINVAL %Ld\n",
-					uffdio_zeropage.zeropage);
-				exit(1);
+				uffd_error("UFFDIO_ZEROPAGE not -EINVAL",
+					   uffdio_zeropage.zeropage);
 			}
 		}
 	} else if (has_zeropage) {
 		if (uffdio_zeropage.zeropage != page_size) {
-			fprintf(stderr, "UFFDIO_ZEROPAGE unexpected %Ld\n",
-				uffdio_zeropage.zeropage); exit(1);
+			uffd_error("UFFDIO_ZEROPAGE unexpected",
+				   uffdio_zeropage.zeropage);
 		} else {
 			if (test_uffdio_zeropage_eexist && retry) {
 				test_uffdio_zeropage_eexist = false;
@@ -1008,9 +1004,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);
+		uffd_error("UFFDIO_ZEROPAGE succeeded",
+			   uffdio_zeropage.zeropage);
 	}
 
 	return 0;
-- 
2.29.2.454.gaff20da3a2-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable
  2020-12-02 21:15 [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable Axel Rasmussen
@ 2020-12-02 23:09 ` Peter Xu
  2020-12-02 23:52   ` Axel Rasmussen
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Xu @ 2020-12-02 23:09 UTC (permalink / raw)
  To: Axel Rasmussen
  Cc: Shuah Khan, Andrew Morton, Joe Perches, Mike Rapoport,
	Andrea Arcangeli, David Alan Gilbert, Greg Thelen,
	linux-kselftest, linux-kernel

Hi, Axel,

Looks mostly good to me, but a few nitpickings below.

On Wed, Dec 02, 2020 at 01:15:42PM -0800, Axel Rasmussen wrote:

[...]

> +static void uffd_error(const char *message, __s64 code)
> +{
> +	fprintf(stderr, "%s: %" PRId64 "\n", message, (int64_t)code);
> +	exit(1);
> +}

IMHO a macro that can take arbitrary parameters would be nicer, but if it
satisfy our need, definitely ok too.

[...]

> @@ -340,7 +348,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);

Is it intended to not use uffd_error() here?

>  	}
>  }

[...]

> @@ -979,26 +981,20 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
>  	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);
> -			}
> +			uffd_error(uffdio_zeropage.zeropage == -EEXIST ?
> +						 "UFFDIO_ZEROPAGE -EEXIST" :
> +						 "UFFDIO_ZEROPAGE error",

Nit: The indents here are a bit odd..

Thanks,

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable
  2020-12-02 23:09 ` Peter Xu
@ 2020-12-02 23:52   ` Axel Rasmussen
  2020-12-03  0:09     ` Miguel Ojeda
  2020-12-03  1:32     ` Peter Xu
  0 siblings, 2 replies; 5+ messages in thread
From: Axel Rasmussen @ 2020-12-02 23:52 UTC (permalink / raw)
  To: Peter Xu
  Cc: Shuah Khan, Andrew Morton, Joe Perches, Mike Rapoport,
	Andrea Arcangeli, David Alan Gilbert, Greg Thelen,
	linux-kselftest, LKML

On Wed, Dec 2, 2020 at 3:10 PM Peter Xu <peterx@redhat.com> wrote:
>
> Hi, Axel,
>
> Looks mostly good to me, but a few nitpickings below.
>
> On Wed, Dec 02, 2020 at 01:15:42PM -0800, Axel Rasmussen wrote:
>
> [...]
>
> > +static void uffd_error(const char *message, __s64 code)
> > +{
> > +     fprintf(stderr, "%s: %" PRId64 "\n", message, (int64_t)code);
> > +     exit(1);
> > +}
>
> IMHO a macro that can take arbitrary parameters would be nicer, but if it
> satisfy our need, definitely ok too.
>
> [...]
>
> > @@ -340,7 +348,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);
>
> Is it intended to not use uffd_error() here?

Yes, this is intentional. This particular case prints the value in
hexadecimal, rather than decimal.

(Agree that uffd_error() could be made more general to cover cases
like this. I opted for the simplest thing which covers all but two
cases - this one, and one where we "return 1;" instead of "exit(1);" -
but I don't feel strongly.)

>
> >       }
> >  }
>
> [...]
>
> > @@ -979,26 +981,20 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
> >       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);
> > -                     }
> > +                     uffd_error(uffdio_zeropage.zeropage == -EEXIST ?
> > +                                              "UFFDIO_ZEROPAGE -EEXIST" :
> > +                                              "UFFDIO_ZEROPAGE error",
>
> Nit: The indents here are a bit odd..

This is what clang-format yields. Are you thinking it would be better
to line everything up with the ( in uffd_error( ?

Or, perhaps this case is a good reason to make uffd_error() a variadic
macro so we can insert "-EEXIST" || "error" with a "%s".

>
> Thanks,
>
> --
> Peter Xu
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable
  2020-12-02 23:52   ` Axel Rasmussen
@ 2020-12-03  0:09     ` Miguel Ojeda
  2020-12-03  1:32     ` Peter Xu
  1 sibling, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2020-12-03  0:09 UTC (permalink / raw)
  To: Axel Rasmussen
  Cc: Peter Xu, Shuah Khan, Andrew Morton, Joe Perches, Mike Rapoport,
	Andrea Arcangeli, David Alan Gilbert, Greg Thelen,
	open list:KERNEL SELFTEST FRAMEWORK, LKML

On Thu, Dec 3, 2020 at 12:55 AM Axel Rasmussen <axelrasmussen@google.com> wrote:
>
> This is what clang-format yields. Are you thinking it would be better
> to line everything up with the ( in uffd_error( ?

Yeah, sometimes clang-format cannot do a good job with the 80 column
limit + 8 tabs.

You are definitely not forced to follow clang-format output by any
means. Subsystem maintainers decide what style they prefer anyway,
which could range from a manual approach to following clang-format
strictly. Clang-format implements the general kernel style as closely
as we could get it so far (it will improve more in the future when we
raise the minimum clang-format version required). See
Doc/process/clang-format.rst.

> Or, perhaps this case is a good reason to make uffd_error() a variadic
> macro so we can insert "-EEXIST" || "error" with a "%s".

...and indeed, sometimes it is a hint that simplifying things could help :-)

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable
  2020-12-02 23:52   ` Axel Rasmussen
  2020-12-03  0:09     ` Miguel Ojeda
@ 2020-12-03  1:32     ` Peter Xu
  1 sibling, 0 replies; 5+ messages in thread
From: Peter Xu @ 2020-12-03  1:32 UTC (permalink / raw)
  To: Axel Rasmussen
  Cc: Shuah Khan, Andrew Morton, Joe Perches, Mike Rapoport,
	Andrea Arcangeli, David Alan Gilbert, Greg Thelen,
	linux-kselftest, LKML

On Wed, Dec 02, 2020 at 03:52:56PM -0800, Axel Rasmussen wrote:
> On Wed, Dec 2, 2020 at 3:10 PM Peter Xu <peterx@redhat.com> wrote:
> >
> > Hi, Axel,
> >
> > Looks mostly good to me, but a few nitpickings below.
> >
> > On Wed, Dec 02, 2020 at 01:15:42PM -0800, Axel Rasmussen wrote:
> >
> > [...]
> >
> > > +static void uffd_error(const char *message, __s64 code)
> > > +{
> > > +     fprintf(stderr, "%s: %" PRId64 "\n", message, (int64_t)code);
> > > +     exit(1);
> > > +}
> >
> > IMHO a macro that can take arbitrary parameters would be nicer, but if it
> > satisfy our need, definitely ok too.
> >
> > [...]
> >
> > > @@ -340,7 +348,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);
> >
> > Is it intended to not use uffd_error() here?
> 
> Yes, this is intentional. This particular case prints the value in
> hexadecimal, rather than decimal.
> 
> (Agree that uffd_error() could be made more general to cover cases
> like this. I opted for the simplest thing which covers all but two
> cases - this one, and one where we "return 1;" instead of "exit(1);" -
> but I don't feel strongly.)

Actually it's as simple as:

#define  uffd_error(...)  do {                  \
        fprintf(stderr, __VA_ARGS__);           \
        fprintf(stderr, "\n");                  \
        exit(1);                                \
    } while (0)

But it's okay, I think.

> 
> >
> > >       }
> > >  }
> >
> > [...]
> >
> > > @@ -979,26 +981,20 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)
> > >       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);
> > > -                     }
> > > +                     uffd_error(uffdio_zeropage.zeropage == -EEXIST ?
> > > +                                              "UFFDIO_ZEROPAGE -EEXIST" :
> > > +                                              "UFFDIO_ZEROPAGE error",
> >
> > Nit: The indents here are a bit odd..
> 
> This is what clang-format yields. Are you thinking it would be better
> to line everything up with the ( in uffd_error( ?
> 
> Or, perhaps this case is a good reason to make uffd_error() a variadic
> macro so we can insert "-EEXIST" || "error" with a "%s".

Yes.  It fixes a build warning, so I think current patch is fine too.

No matter whether you'd like a v2, please feel free to take:

Acked-by: Peter Xu <peterx@redhat.com>

Thanks,

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-03  1:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-02 21:15 [PATCH] userfaultfd: selftests: make __{s,u}64 format specifiers portable Axel Rasmussen
2020-12-02 23:09 ` Peter Xu
2020-12-02 23:52   ` Axel Rasmussen
2020-12-03  0:09     ` Miguel Ojeda
2020-12-03  1:32     ` Peter Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).