[PATCH 15/29] selftests/mm: uffd_open_{dev|sys}()

Peter Xu posted 29 patches 2 years, 8 months ago
There is a newer version of this series
[PATCH 15/29] selftests/mm: uffd_open_{dev|sys}()
Posted by Peter Xu 2 years, 8 months ago
Provide two helpers to open an uffd handle.  Drop the error checks around
SKIPs because it's inside an errexit() anyway, which IMHO doesn't really
help much if the test will not continue.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/testing/selftests/mm/uffd-common.c | 28 +++++-------------------
 tools/testing/selftests/mm/vm_util.c     | 24 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
index 17f2bb82c3db..3a9b5c1aca9d 100644
--- a/tools/testing/selftests/mm/uffd-common.c
+++ b/tools/testing/selftests/mm/uffd-common.c
@@ -192,34 +192,16 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus)
 	printf("\n");
 }
 
-static int __userfaultfd_open_dev(void)
-{
-	int fd, _uffd;
-
-	fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
-	if (fd < 0)
-		errexit(KSFT_SKIP, "opening /dev/userfaultfd failed");
-
-	_uffd = ioctl(fd, USERFAULTFD_IOC_NEW, UFFD_FLAGS);
-	if (_uffd < 0)
-		errexit(errno == ENOTTY ? KSFT_SKIP : 1,
-			"creating userfaultfd failed");
-	close(fd);
-	return _uffd;
-}
-
 void userfaultfd_open(uint64_t *features)
 {
 	struct uffdio_api uffdio_api;
 
 	if (test_dev_userfaultfd)
-		uffd = __userfaultfd_open_dev();
-	else {
-		uffd = syscall(__NR_userfaultfd, UFFD_FLAGS);
-		if (uffd < 0)
-			errexit(errno == ENOSYS ? KSFT_SKIP : 1,
-				"creating userfaultfd failed");
-	}
+		uffd = uffd_open_dev(UFFD_FLAGS);
+	else
+		uffd = uffd_open_sys(UFFD_FLAGS);
+	if (uffd < 0)
+		err("uffd open failed (dev=%d)", test_dev_userfaultfd);
 	uffd_flags = fcntl(uffd, F_GETFD, NULL);
 
 	uffdio_api.api = UFFD_API;
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 10e76400ed70..7c2bf88d6393 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -3,6 +3,8 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <linux/userfaultfd.h>
+#include <sys/syscall.h>
+#include <unistd.h>
 #include "../kselftest.h"
 #include "vm_util.h"
 
@@ -230,3 +232,25 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)
 
 	return ret;
 }
+
+int uffd_open_dev(unsigned int flags)
+{
+	int fd, uffd;
+
+	fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
+	if (fd < 0)
+		return fd;
+	uffd = ioctl(fd, USERFAULTFD_IOC_NEW, flags);
+	close(fd);
+
+	return uffd;
+}
+
+int uffd_open_sys(unsigned int flags)
+{
+#ifdef __NR_userfaultfd
+	return syscall(__NR_userfaultfd, flags);
+#else
+	return -1;
+#endif
+}
-- 
2.39.1
Re: [PATCH 15/29] selftests/mm: uffd_open_{dev|sys}()
Posted by Mike Rapoport 2 years, 8 months ago
On Thu, Mar 30, 2023 at 12:07:49PM -0400, Peter Xu wrote:
> Provide two helpers to open an uffd handle.  Drop the error checks around
> SKIPs because it's inside an errexit() anyway, which IMHO doesn't really
> help much if the test will not continue.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>

Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>

> ---
>  tools/testing/selftests/mm/uffd-common.c | 28 +++++-------------------
>  tools/testing/selftests/mm/vm_util.c     | 24 ++++++++++++++++++++
>  2 files changed, 29 insertions(+), 23 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
> index 17f2bb82c3db..3a9b5c1aca9d 100644
> --- a/tools/testing/selftests/mm/uffd-common.c
> +++ b/tools/testing/selftests/mm/uffd-common.c
> @@ -192,34 +192,16 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus)
>  	printf("\n");
>  }
> 
> -static int __userfaultfd_open_dev(void)
> -{
> -	int fd, _uffd;
> -
> -	fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
> -	if (fd < 0)
> -		errexit(KSFT_SKIP, "opening /dev/userfaultfd failed");
> -
> -	_uffd = ioctl(fd, USERFAULTFD_IOC_NEW, UFFD_FLAGS);
> -	if (_uffd < 0)
> -		errexit(errno == ENOTTY ? KSFT_SKIP : 1,
> -			"creating userfaultfd failed");
> -	close(fd);
> -	return _uffd;
> -}
> -
>  void userfaultfd_open(uint64_t *features)
>  {
>  	struct uffdio_api uffdio_api;
> 
>  	if (test_dev_userfaultfd)
> -		uffd = __userfaultfd_open_dev();
> -	else {
> -		uffd = syscall(__NR_userfaultfd, UFFD_FLAGS);
> -		if (uffd < 0)
> -			errexit(errno == ENOSYS ? KSFT_SKIP : 1,
> -				"creating userfaultfd failed");
> -	}
> +		uffd = uffd_open_dev(UFFD_FLAGS);
> +	else
> +		uffd = uffd_open_sys(UFFD_FLAGS);
> +	if (uffd < 0)
> +		err("uffd open failed (dev=%d)", test_dev_userfaultfd);
>  	uffd_flags = fcntl(uffd, F_GETFD, NULL);
> 
>  	uffdio_api.api = UFFD_API;
> diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
> index 10e76400ed70..7c2bf88d6393 100644
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -3,6 +3,8 @@
>  #include <fcntl.h>
>  #include <sys/ioctl.h>
>  #include <linux/userfaultfd.h>
> +#include <sys/syscall.h>
> +#include <unistd.h>
>  #include "../kselftest.h"
>  #include "vm_util.h"
> 
> @@ -230,3 +232,25 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)
> 
>  	return ret;
>  }
> +
> +int uffd_open_dev(unsigned int flags)
> +{
> +	int fd, uffd;
> +
> +	fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
> +	if (fd < 0)
> +		return fd;
> +	uffd = ioctl(fd, USERFAULTFD_IOC_NEW, flags);
> +	close(fd);
> +
> +	return uffd;
> +}
> +
> +int uffd_open_sys(unsigned int flags)
> +{
> +#ifdef __NR_userfaultfd
> +	return syscall(__NR_userfaultfd, flags);
> +#else
> +	return -1;
> +#endif
> +}
> -- 
> 2.39.1
> 

-- 
Sincerely yours,
Mike.
Re: [PATCH 15/29] selftests/mm: uffd_open_{dev|sys}()
Posted by David Hildenbrand 2 years, 8 months ago
On 30.03.23 18:07, Peter Xu wrote:
> Provide two helpers to open an uffd handle.  Drop the error checks around
> SKIPs because it's inside an errexit() anyway, which IMHO doesn't really
> help much if the test will not continue.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb