Commit 78391f64 authored by Peter Xu's avatar Peter Xu Committed by Andrew Morton

selftests/mm: uffd_open_{dev|sys}()

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.

Link: https://lkml.kernel.org/r/20230412164254.328335-1-peterx@redhat.comSigned-off-by: default avatarPeter Xu <peterx@redhat.com>
Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
Reviewed-by: default avatarMike Rapoport (IBM) <rppt@kernel.org>
Reviewed-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Zach O'Keefe <zokeefe@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent c4277cb6
...@@ -192,34 +192,16 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus) ...@@ -192,34 +192,16 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus)
printf("\n"); 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) void userfaultfd_open(uint64_t *features)
{ {
struct uffdio_api uffdio_api; struct uffdio_api uffdio_api;
if (test_dev_userfaultfd) if (test_dev_userfaultfd)
uffd = __userfaultfd_open_dev(); uffd = uffd_open_dev(UFFD_FLAGS);
else { else
uffd = syscall(__NR_userfaultfd, UFFD_FLAGS); uffd = uffd_open_sys(UFFD_FLAGS);
if (uffd < 0) if (uffd < 0)
errexit(errno == ENOSYS ? KSFT_SKIP : 1, err("uffd open failed (dev=%d)", test_dev_userfaultfd);
"creating userfaultfd failed");
}
uffd_flags = fcntl(uffd, F_GETFD, NULL); uffd_flags = fcntl(uffd, F_GETFD, NULL);
uffdio_api.api = UFFD_API; uffdio_api.api = UFFD_API;
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/userfaultfd.h> #include <linux/userfaultfd.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "../kselftest.h" #include "../kselftest.h"
#include "vm_util.h" #include "vm_util.h"
...@@ -230,3 +232,25 @@ int uffd_unregister(int uffd, void *addr, uint64_t len) ...@@ -230,3 +232,25 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)
return ret; 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
}
...@@ -48,6 +48,8 @@ unsigned long default_huge_page_size(void); ...@@ -48,6 +48,8 @@ unsigned long default_huge_page_size(void);
int uffd_register(int uffd, void *addr, uint64_t len, int uffd_register(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor); bool miss, bool wp, bool minor);
int uffd_unregister(int uffd, void *addr, uint64_t len); int uffd_unregister(int uffd, void *addr, uint64_t len);
int uffd_open_dev(unsigned int flags);
int uffd_open_sys(unsigned int flags);
/* /*
* On ppc64 this will only work with radix 2M hugepage size * On ppc64 this will only work with radix 2M hugepage size
......
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