mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-12 19:44:51 -04:00
Add two helpers to register/unregister to an uffd. Use them to drop
duplicate codes.
This patch also drops assert_expected_ioctls_present() and
get_expected_ioctls(). Reasons:
- It'll need a lot of effort to pass test_type==HUGETLB into it from
the upper, so it's the simplest way to get rid of another global var
- The ioctls returned in UFFDIO_REGISTER is hardly useful at all,
because any app can already detect kernel support on any ioctl via its
corresponding UFFD_FEATURE_*. The check here is for sanity mostly but
it's probably destined no user app will even use it.
- It's not friendly to one future goal of uffd to run on old
kernels, the problem is get_expected_ioctls() compiles against
UFFD_API_RANGE_IOCTLS, which is a value that can change depending on
where the test is compiled, rather than reflecting what the kernel
underneath has. It means it'll report false negatives on old kernels
so it's against our will.
So let's make our lives easier.
[peterx@redhat.com; tools/testing/selftests/mm/hugepage-mremap.c: add headers]
Link: https://lkml.kernel.org/r/ZDxrvZh/cw357D8P@x1n
Link: https://lkml.kernel.org/r/20230412164247.328293-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Dmitry Safonov <0x7f454c46@gmail.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Zach O'Keefe <zokeefe@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
116 lines
3.3 KiB
C
116 lines
3.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Userfaultfd tests common header
|
|
*
|
|
* Copyright (C) 2015-2023 Red Hat, Inc.
|
|
*/
|
|
#ifndef __UFFD_COMMON_H__
|
|
#define __UFFD_COMMON_H__
|
|
|
|
#define _GNU_SOURCE
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <time.h>
|
|
#include <signal.h>
|
|
#include <poll.h>
|
|
#include <string.h>
|
|
#include <linux/mman.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/syscall.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/wait.h>
|
|
#include <pthread.h>
|
|
#include <linux/userfaultfd.h>
|
|
#include <setjmp.h>
|
|
#include <stdbool.h>
|
|
#include <assert.h>
|
|
#include <inttypes.h>
|
|
#include <stdint.h>
|
|
#include <sys/random.h>
|
|
|
|
#include "../kselftest.h"
|
|
#include "vm_util.h"
|
|
|
|
#define UFFD_FLAGS (O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY)
|
|
|
|
#define _err(fmt, ...) \
|
|
do { \
|
|
int ret = errno; \
|
|
fprintf(stderr, "ERROR: " fmt, ##__VA_ARGS__); \
|
|
fprintf(stderr, " (errno=%d, @%s:%d)\n", \
|
|
ret, __FILE__, __LINE__); \
|
|
} while (0)
|
|
|
|
#define errexit(exitcode, fmt, ...) \
|
|
do { \
|
|
_err(fmt, ##__VA_ARGS__); \
|
|
exit(exitcode); \
|
|
} while (0)
|
|
|
|
#define err(fmt, ...) errexit(1, fmt, ##__VA_ARGS__)
|
|
|
|
/* pthread_mutex_t starts at page offset 0 */
|
|
#define area_mutex(___area, ___nr) \
|
|
((pthread_mutex_t *) ((___area) + (___nr)*page_size))
|
|
/*
|
|
* count is placed in the page after pthread_mutex_t naturally aligned
|
|
* to avoid non alignment faults on non-x86 archs.
|
|
*/
|
|
#define area_count(___area, ___nr) \
|
|
((volatile unsigned long long *) ((unsigned long) \
|
|
((___area) + (___nr)*page_size + \
|
|
sizeof(pthread_mutex_t) + \
|
|
sizeof(unsigned long long) - 1) & \
|
|
~(unsigned long)(sizeof(unsigned long long) \
|
|
- 1)))
|
|
|
|
/* Userfaultfd test statistics */
|
|
struct uffd_stats {
|
|
int cpu;
|
|
unsigned long missing_faults;
|
|
unsigned long wp_faults;
|
|
unsigned long minor_faults;
|
|
};
|
|
|
|
struct uffd_test_ops {
|
|
void (*allocate_area)(void **alloc_area, bool is_src);
|
|
void (*release_pages)(char *rel_area);
|
|
void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset);
|
|
void (*check_pmd_mapping)(void *p, int expect_nr_hpages);
|
|
};
|
|
typedef struct uffd_test_ops uffd_test_ops_t;
|
|
|
|
extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size;
|
|
extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;
|
|
extern int mem_fd, uffd, uffd_flags, finished, *pipefd, test_type;
|
|
extern bool map_shared, test_collapse, test_dev_userfaultfd;
|
|
extern bool test_uffdio_wp, test_uffdio_minor;
|
|
extern unsigned long long *count_verify;
|
|
extern volatile bool test_uffdio_copy_eexist;
|
|
|
|
extern uffd_test_ops_t anon_uffd_test_ops;
|
|
extern uffd_test_ops_t shmem_uffd_test_ops;
|
|
extern uffd_test_ops_t hugetlb_uffd_test_ops;
|
|
extern uffd_test_ops_t *uffd_test_ops;
|
|
|
|
void uffd_stats_report(struct uffd_stats *stats, int n_cpus);
|
|
void uffd_test_ctx_init(uint64_t features);
|
|
void userfaultfd_open(uint64_t *features);
|
|
int uffd_read_msg(int ufd, struct uffd_msg *msg);
|
|
void wp_range(int ufd, __u64 start, __u64 len, bool wp);
|
|
void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_stats *stats);
|
|
int __copy_page(int ufd, unsigned long offset, bool retry);
|
|
int copy_page(int ufd, unsigned long offset);
|
|
void *uffd_poll_thread(void *arg);
|
|
|
|
#define TEST_ANON 1
|
|
#define TEST_HUGETLB 2
|
|
#define TEST_SHMEM 3
|
|
|
|
#endif
|