mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
synced 2026-04-27 03:49:57 -04:00
selftests/mm: create uffd-common.[ch]
Move common utility functions into uffd-common.[ch] files from the original userfaultfd.c. This prepares for a split of userfaultfd.c into two tests: one to only cover the old but powerful stress test, the other one covers all the functional tests. This movement is kind of a brute-force effort for now, with light touch-ups but nothing should really change. There's chances to optimize more, but let's leave that for later. Link: https://lkml.kernel.org/r/20230412164241.328259-1-peterx@redhat.com Signed-off-by: Peter Xu <peterx@redhat.com> Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org> 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: Zach O'Keefe <zokeefe@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
117
tools/testing/selftests/mm/uffd-common.h
Normal file
117
tools/testing/selftests/mm/uffd-common.h
Normal file
@@ -0,0 +1,117 @@
|
||||
// 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);
|
||||
uint64_t get_expected_ioctls(uint64_t mode);
|
||||
void assert_expected_ioctls_present(uint64_t mode, uint64_t ioctls);
|
||||
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
|
||||
Reference in New Issue
Block a user