162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Stress userfaultfd syscall.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 2015  Red Hat, Inc.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * This test allocates two virtual areas and bounces the physical
862306a36Sopenharmony_ci * memory across the two virtual areas (from area_src to area_dst)
962306a36Sopenharmony_ci * using userfaultfd.
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * There are three threads running per CPU:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * 1) one per-CPU thread takes a per-page pthread_mutex in a random
1462306a36Sopenharmony_ci *    page of the area_dst (while the physical page may still be in
1562306a36Sopenharmony_ci *    area_src), and increments a per-page counter in the same page,
1662306a36Sopenharmony_ci *    and checks its value against a verification region.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * 2) another per-CPU thread handles the userfaults generated by
1962306a36Sopenharmony_ci *    thread 1 above. userfaultfd blocking reads or poll() modes are
2062306a36Sopenharmony_ci *    exercised interleaved.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * 3) one last per-CPU thread transfers the memory in the background
2362306a36Sopenharmony_ci *    at maximum bandwidth (if not already transferred by thread
2462306a36Sopenharmony_ci *    2). Each cpu thread takes cares of transferring a portion of the
2562306a36Sopenharmony_ci *    area.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * When all threads of type 3 completed the transfer, one bounce is
2862306a36Sopenharmony_ci * complete. area_src and area_dst are then swapped. All threads are
2962306a36Sopenharmony_ci * respawned and so the bounce is immediately restarted in the
3062306a36Sopenharmony_ci * opposite direction.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * per-CPU threads 1 by triggering userfaults inside
3362306a36Sopenharmony_ci * pthread_mutex_lock will also verify the atomicity of the memory
3462306a36Sopenharmony_ci * transfer (UFFDIO_COPY).
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include "uffd-common.h"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#ifdef __NR_userfaultfd
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define BOUNCE_RANDOM		(1<<0)
4262306a36Sopenharmony_ci#define BOUNCE_RACINGFAULTS	(1<<1)
4362306a36Sopenharmony_ci#define BOUNCE_VERIFY		(1<<2)
4462306a36Sopenharmony_ci#define BOUNCE_POLL		(1<<3)
4562306a36Sopenharmony_cistatic int bounces;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */
4862306a36Sopenharmony_ci#define ALARM_INTERVAL_SECS 10
4962306a36Sopenharmony_cistatic char *zeropage;
5062306a36Sopenharmony_cipthread_attr_t attr;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define swap(a, b) \
5362306a36Sopenharmony_ci	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciconst char *examples =
5662306a36Sopenharmony_ci	"# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
5762306a36Sopenharmony_ci	"./uffd-stress anon 100 99999\n\n"
5862306a36Sopenharmony_ci	"# Run share memory test on 1GiB region with 99 bounces:\n"
5962306a36Sopenharmony_ci	"./uffd-stress shmem 1000 99\n\n"
6062306a36Sopenharmony_ci	"# Run hugetlb memory test on 256MiB region with 50 bounces:\n"
6162306a36Sopenharmony_ci	"./uffd-stress hugetlb 256 50\n\n"
6262306a36Sopenharmony_ci	"# Run the same hugetlb test but using private file:\n"
6362306a36Sopenharmony_ci	"./uffd-stress hugetlb-private 256 50\n\n"
6462306a36Sopenharmony_ci	"# 10MiB-~6GiB 999 bounces anonymous test, "
6562306a36Sopenharmony_ci	"continue forever unless an error triggers\n"
6662306a36Sopenharmony_ci	"while ./uffd-stress anon $[RANDOM % 6000 + 10] 999; do true; done\n\n";
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic void usage(void)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	fprintf(stderr, "\nUsage: ./uffd-stress <test type> <MiB> <bounces>\n\n");
7162306a36Sopenharmony_ci	fprintf(stderr, "Supported <test type>: anon, hugetlb, "
7262306a36Sopenharmony_ci		"hugetlb-private, shmem, shmem-private\n\n");
7362306a36Sopenharmony_ci	fprintf(stderr, "Examples:\n\n");
7462306a36Sopenharmony_ci	fprintf(stderr, "%s", examples);
7562306a36Sopenharmony_ci	exit(1);
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic void uffd_stats_reset(struct uffd_args *args, unsigned long n_cpus)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	int i;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	for (i = 0; i < n_cpus; i++) {
8362306a36Sopenharmony_ci		args[i].cpu = i;
8462306a36Sopenharmony_ci		args[i].apply_wp = test_uffdio_wp;
8562306a36Sopenharmony_ci		args[i].missing_faults = 0;
8662306a36Sopenharmony_ci		args[i].wp_faults = 0;
8762306a36Sopenharmony_ci		args[i].minor_faults = 0;
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic void *locking_thread(void *arg)
9262306a36Sopenharmony_ci{
9362306a36Sopenharmony_ci	unsigned long cpu = (unsigned long) arg;
9462306a36Sopenharmony_ci	unsigned long page_nr;
9562306a36Sopenharmony_ci	unsigned long long count;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	if (!(bounces & BOUNCE_RANDOM)) {
9862306a36Sopenharmony_ci		page_nr = -bounces;
9962306a36Sopenharmony_ci		if (!(bounces & BOUNCE_RACINGFAULTS))
10062306a36Sopenharmony_ci			page_nr += cpu * nr_pages_per_cpu;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	while (!finished) {
10462306a36Sopenharmony_ci		if (bounces & BOUNCE_RANDOM) {
10562306a36Sopenharmony_ci			if (getrandom(&page_nr, sizeof(page_nr), 0) != sizeof(page_nr))
10662306a36Sopenharmony_ci				err("getrandom failed");
10762306a36Sopenharmony_ci		} else
10862306a36Sopenharmony_ci			page_nr += 1;
10962306a36Sopenharmony_ci		page_nr %= nr_pages;
11062306a36Sopenharmony_ci		pthread_mutex_lock(area_mutex(area_dst, page_nr));
11162306a36Sopenharmony_ci		count = *area_count(area_dst, page_nr);
11262306a36Sopenharmony_ci		if (count != count_verify[page_nr])
11362306a36Sopenharmony_ci			err("page_nr %lu memory corruption %llu %llu",
11462306a36Sopenharmony_ci			    page_nr, count, count_verify[page_nr]);
11562306a36Sopenharmony_ci		count++;
11662306a36Sopenharmony_ci		*area_count(area_dst, page_nr) = count_verify[page_nr] = count;
11762306a36Sopenharmony_ci		pthread_mutex_unlock(area_mutex(area_dst, page_nr));
11862306a36Sopenharmony_ci	}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	return NULL;
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic int copy_page_retry(int ufd, unsigned long offset)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	return __copy_page(ufd, offset, true, test_uffdio_wp);
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cipthread_mutex_t uffd_read_mutex = PTHREAD_MUTEX_INITIALIZER;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic void *uffd_read_thread(void *arg)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	struct uffd_args *args = (struct uffd_args *)arg;
13362306a36Sopenharmony_ci	struct uffd_msg msg;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	pthread_mutex_unlock(&uffd_read_mutex);
13662306a36Sopenharmony_ci	/* from here cancellation is ok */
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	for (;;) {
13962306a36Sopenharmony_ci		if (uffd_read_msg(uffd, &msg))
14062306a36Sopenharmony_ci			continue;
14162306a36Sopenharmony_ci		uffd_handle_page_fault(&msg, args);
14262306a36Sopenharmony_ci	}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	return NULL;
14562306a36Sopenharmony_ci}
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_cistatic void *background_thread(void *arg)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci	unsigned long cpu = (unsigned long) arg;
15062306a36Sopenharmony_ci	unsigned long page_nr, start_nr, mid_nr, end_nr;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	start_nr = cpu * nr_pages_per_cpu;
15362306a36Sopenharmony_ci	end_nr = (cpu+1) * nr_pages_per_cpu;
15462306a36Sopenharmony_ci	mid_nr = (start_nr + end_nr) / 2;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/* Copy the first half of the pages */
15762306a36Sopenharmony_ci	for (page_nr = start_nr; page_nr < mid_nr; page_nr++)
15862306a36Sopenharmony_ci		copy_page_retry(uffd, page_nr * page_size);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	/*
16162306a36Sopenharmony_ci	 * If we need to test uffd-wp, set it up now.  Then we'll have
16262306a36Sopenharmony_ci	 * at least the first half of the pages mapped already which
16362306a36Sopenharmony_ci	 * can be write-protected for testing
16462306a36Sopenharmony_ci	 */
16562306a36Sopenharmony_ci	if (test_uffdio_wp)
16662306a36Sopenharmony_ci		wp_range(uffd, (unsigned long)area_dst + start_nr * page_size,
16762306a36Sopenharmony_ci			nr_pages_per_cpu * page_size, true);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/*
17062306a36Sopenharmony_ci	 * Continue the 2nd half of the page copying, handling write
17162306a36Sopenharmony_ci	 * protection faults if any
17262306a36Sopenharmony_ci	 */
17362306a36Sopenharmony_ci	for (page_nr = mid_nr; page_nr < end_nr; page_nr++)
17462306a36Sopenharmony_ci		copy_page_retry(uffd, page_nr * page_size);
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	return NULL;
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic int stress(struct uffd_args *args)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	unsigned long cpu;
18262306a36Sopenharmony_ci	pthread_t locking_threads[nr_cpus];
18362306a36Sopenharmony_ci	pthread_t uffd_threads[nr_cpus];
18462306a36Sopenharmony_ci	pthread_t background_threads[nr_cpus];
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	finished = 0;
18762306a36Sopenharmony_ci	for (cpu = 0; cpu < nr_cpus; cpu++) {
18862306a36Sopenharmony_ci		if (pthread_create(&locking_threads[cpu], &attr,
18962306a36Sopenharmony_ci				   locking_thread, (void *)cpu))
19062306a36Sopenharmony_ci			return 1;
19162306a36Sopenharmony_ci		if (bounces & BOUNCE_POLL) {
19262306a36Sopenharmony_ci			if (pthread_create(&uffd_threads[cpu], &attr, uffd_poll_thread, &args[cpu]))
19362306a36Sopenharmony_ci				err("uffd_poll_thread create");
19462306a36Sopenharmony_ci		} else {
19562306a36Sopenharmony_ci			if (pthread_create(&uffd_threads[cpu], &attr,
19662306a36Sopenharmony_ci					   uffd_read_thread,
19762306a36Sopenharmony_ci					   (void *)&args[cpu]))
19862306a36Sopenharmony_ci				return 1;
19962306a36Sopenharmony_ci			pthread_mutex_lock(&uffd_read_mutex);
20062306a36Sopenharmony_ci		}
20162306a36Sopenharmony_ci		if (pthread_create(&background_threads[cpu], &attr,
20262306a36Sopenharmony_ci				   background_thread, (void *)cpu))
20362306a36Sopenharmony_ci			return 1;
20462306a36Sopenharmony_ci	}
20562306a36Sopenharmony_ci	for (cpu = 0; cpu < nr_cpus; cpu++)
20662306a36Sopenharmony_ci		if (pthread_join(background_threads[cpu], NULL))
20762306a36Sopenharmony_ci			return 1;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	/*
21062306a36Sopenharmony_ci	 * Be strict and immediately zap area_src, the whole area has
21162306a36Sopenharmony_ci	 * been transferred already by the background treads. The
21262306a36Sopenharmony_ci	 * area_src could then be faulted in a racy way by still
21362306a36Sopenharmony_ci	 * running uffdio_threads reading zeropages after we zapped
21462306a36Sopenharmony_ci	 * area_src (but they're guaranteed to get -EEXIST from
21562306a36Sopenharmony_ci	 * UFFDIO_COPY without writing zero pages into area_dst
21662306a36Sopenharmony_ci	 * because the background threads already completed).
21762306a36Sopenharmony_ci	 */
21862306a36Sopenharmony_ci	uffd_test_ops->release_pages(area_src);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	finished = 1;
22162306a36Sopenharmony_ci	for (cpu = 0; cpu < nr_cpus; cpu++)
22262306a36Sopenharmony_ci		if (pthread_join(locking_threads[cpu], NULL))
22362306a36Sopenharmony_ci			return 1;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	for (cpu = 0; cpu < nr_cpus; cpu++) {
22662306a36Sopenharmony_ci		char c;
22762306a36Sopenharmony_ci		if (bounces & BOUNCE_POLL) {
22862306a36Sopenharmony_ci			if (write(pipefd[cpu*2+1], &c, 1) != 1)
22962306a36Sopenharmony_ci				err("pipefd write error");
23062306a36Sopenharmony_ci			if (pthread_join(uffd_threads[cpu],
23162306a36Sopenharmony_ci					 (void *)&args[cpu]))
23262306a36Sopenharmony_ci				return 1;
23362306a36Sopenharmony_ci		} else {
23462306a36Sopenharmony_ci			if (pthread_cancel(uffd_threads[cpu]))
23562306a36Sopenharmony_ci				return 1;
23662306a36Sopenharmony_ci			if (pthread_join(uffd_threads[cpu], NULL))
23762306a36Sopenharmony_ci				return 1;
23862306a36Sopenharmony_ci		}
23962306a36Sopenharmony_ci	}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	return 0;
24262306a36Sopenharmony_ci}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_cistatic int userfaultfd_stress(void)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci	void *area;
24762306a36Sopenharmony_ci	unsigned long nr;
24862306a36Sopenharmony_ci	struct uffd_args args[nr_cpus];
24962306a36Sopenharmony_ci	uint64_t mem_size = nr_pages * page_size;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	memset(args, 0, sizeof(struct uffd_args) * nr_cpus);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	if (uffd_test_ctx_init(UFFD_FEATURE_WP_UNPOPULATED, NULL))
25462306a36Sopenharmony_ci		err("context init failed");
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	if (posix_memalign(&area, page_size, page_size))
25762306a36Sopenharmony_ci		err("out of memory");
25862306a36Sopenharmony_ci	zeropage = area;
25962306a36Sopenharmony_ci	bzero(zeropage, page_size);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	pthread_mutex_lock(&uffd_read_mutex);
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	pthread_attr_init(&attr);
26462306a36Sopenharmony_ci	pthread_attr_setstacksize(&attr, 16*1024*1024);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	while (bounces--) {
26762306a36Sopenharmony_ci		printf("bounces: %d, mode:", bounces);
26862306a36Sopenharmony_ci		if (bounces & BOUNCE_RANDOM)
26962306a36Sopenharmony_ci			printf(" rnd");
27062306a36Sopenharmony_ci		if (bounces & BOUNCE_RACINGFAULTS)
27162306a36Sopenharmony_ci			printf(" racing");
27262306a36Sopenharmony_ci		if (bounces & BOUNCE_VERIFY)
27362306a36Sopenharmony_ci			printf(" ver");
27462306a36Sopenharmony_ci		if (bounces & BOUNCE_POLL)
27562306a36Sopenharmony_ci			printf(" poll");
27662306a36Sopenharmony_ci		else
27762306a36Sopenharmony_ci			printf(" read");
27862306a36Sopenharmony_ci		printf(", ");
27962306a36Sopenharmony_ci		fflush(stdout);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci		if (bounces & BOUNCE_POLL)
28262306a36Sopenharmony_ci			fcntl(uffd, F_SETFL, uffd_flags | O_NONBLOCK);
28362306a36Sopenharmony_ci		else
28462306a36Sopenharmony_ci			fcntl(uffd, F_SETFL, uffd_flags & ~O_NONBLOCK);
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci		/* register */
28762306a36Sopenharmony_ci		if (uffd_register(uffd, area_dst, mem_size,
28862306a36Sopenharmony_ci				  true, test_uffdio_wp, false))
28962306a36Sopenharmony_ci			err("register failure");
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci		if (area_dst_alias) {
29262306a36Sopenharmony_ci			if (uffd_register(uffd, area_dst_alias, mem_size,
29362306a36Sopenharmony_ci					  true, test_uffdio_wp, false))
29462306a36Sopenharmony_ci				err("register failure alias");
29562306a36Sopenharmony_ci		}
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci		/*
29862306a36Sopenharmony_ci		 * The madvise done previously isn't enough: some
29962306a36Sopenharmony_ci		 * uffd_thread could have read userfaults (one of
30062306a36Sopenharmony_ci		 * those already resolved by the background thread)
30162306a36Sopenharmony_ci		 * and it may be in the process of calling
30262306a36Sopenharmony_ci		 * UFFDIO_COPY. UFFDIO_COPY will read the zapped
30362306a36Sopenharmony_ci		 * area_src and it would map a zero page in it (of
30462306a36Sopenharmony_ci		 * course such a UFFDIO_COPY is perfectly safe as it'd
30562306a36Sopenharmony_ci		 * return -EEXIST). The problem comes at the next
30662306a36Sopenharmony_ci		 * bounce though: that racing UFFDIO_COPY would
30762306a36Sopenharmony_ci		 * generate zeropages in the area_src, so invalidating
30862306a36Sopenharmony_ci		 * the previous MADV_DONTNEED. Without this additional
30962306a36Sopenharmony_ci		 * MADV_DONTNEED those zeropages leftovers in the
31062306a36Sopenharmony_ci		 * area_src would lead to -EEXIST failure during the
31162306a36Sopenharmony_ci		 * next bounce, effectively leaving a zeropage in the
31262306a36Sopenharmony_ci		 * area_dst.
31362306a36Sopenharmony_ci		 *
31462306a36Sopenharmony_ci		 * Try to comment this out madvise to see the memory
31562306a36Sopenharmony_ci		 * corruption being caught pretty quick.
31662306a36Sopenharmony_ci		 *
31762306a36Sopenharmony_ci		 * khugepaged is also inhibited to collapse THP after
31862306a36Sopenharmony_ci		 * MADV_DONTNEED only after the UFFDIO_REGISTER, so it's
31962306a36Sopenharmony_ci		 * required to MADV_DONTNEED here.
32062306a36Sopenharmony_ci		 */
32162306a36Sopenharmony_ci		uffd_test_ops->release_pages(area_dst);
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci		uffd_stats_reset(args, nr_cpus);
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci		/* bounce pass */
32662306a36Sopenharmony_ci		if (stress(args))
32762306a36Sopenharmony_ci			return 1;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci		/* Clear all the write protections if there is any */
33062306a36Sopenharmony_ci		if (test_uffdio_wp)
33162306a36Sopenharmony_ci			wp_range(uffd, (unsigned long)area_dst,
33262306a36Sopenharmony_ci				 nr_pages * page_size, false);
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci		/* unregister */
33562306a36Sopenharmony_ci		if (uffd_unregister(uffd, area_dst, mem_size))
33662306a36Sopenharmony_ci			err("unregister failure");
33762306a36Sopenharmony_ci		if (area_dst_alias) {
33862306a36Sopenharmony_ci			if (uffd_unregister(uffd, area_dst_alias, mem_size))
33962306a36Sopenharmony_ci				err("unregister failure alias");
34062306a36Sopenharmony_ci		}
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci		/* verification */
34362306a36Sopenharmony_ci		if (bounces & BOUNCE_VERIFY)
34462306a36Sopenharmony_ci			for (nr = 0; nr < nr_pages; nr++)
34562306a36Sopenharmony_ci				if (*area_count(area_dst, nr) != count_verify[nr])
34662306a36Sopenharmony_ci					err("error area_count %llu %llu %lu\n",
34762306a36Sopenharmony_ci					    *area_count(area_src, nr),
34862306a36Sopenharmony_ci					    count_verify[nr], nr);
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci		/* prepare next bounce */
35162306a36Sopenharmony_ci		swap(area_src, area_dst);
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci		swap(area_src_alias, area_dst_alias);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci		uffd_stats_report(args, nr_cpus);
35662306a36Sopenharmony_ci	}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	return 0;
35962306a36Sopenharmony_ci}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_cistatic void set_test_type(const char *type)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	if (!strcmp(type, "anon")) {
36462306a36Sopenharmony_ci		test_type = TEST_ANON;
36562306a36Sopenharmony_ci		uffd_test_ops = &anon_uffd_test_ops;
36662306a36Sopenharmony_ci	} else if (!strcmp(type, "hugetlb")) {
36762306a36Sopenharmony_ci		test_type = TEST_HUGETLB;
36862306a36Sopenharmony_ci		uffd_test_ops = &hugetlb_uffd_test_ops;
36962306a36Sopenharmony_ci		map_shared = true;
37062306a36Sopenharmony_ci	} else if (!strcmp(type, "hugetlb-private")) {
37162306a36Sopenharmony_ci		test_type = TEST_HUGETLB;
37262306a36Sopenharmony_ci		uffd_test_ops = &hugetlb_uffd_test_ops;
37362306a36Sopenharmony_ci	} else if (!strcmp(type, "shmem")) {
37462306a36Sopenharmony_ci		map_shared = true;
37562306a36Sopenharmony_ci		test_type = TEST_SHMEM;
37662306a36Sopenharmony_ci		uffd_test_ops = &shmem_uffd_test_ops;
37762306a36Sopenharmony_ci	} else if (!strcmp(type, "shmem-private")) {
37862306a36Sopenharmony_ci		test_type = TEST_SHMEM;
37962306a36Sopenharmony_ci		uffd_test_ops = &shmem_uffd_test_ops;
38062306a36Sopenharmony_ci	}
38162306a36Sopenharmony_ci}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_cistatic void parse_test_type_arg(const char *raw_type)
38462306a36Sopenharmony_ci{
38562306a36Sopenharmony_ci	uint64_t features = UFFD_API_FEATURES;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	set_test_type(raw_type);
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	if (!test_type)
39062306a36Sopenharmony_ci		err("failed to parse test type argument: '%s'", raw_type);
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	if (test_type == TEST_HUGETLB)
39362306a36Sopenharmony_ci		page_size = default_huge_page_size();
39462306a36Sopenharmony_ci	else
39562306a36Sopenharmony_ci		page_size = sysconf(_SC_PAGE_SIZE);
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	if (!page_size)
39862306a36Sopenharmony_ci		err("Unable to determine page size");
39962306a36Sopenharmony_ci	if ((unsigned long) area_count(NULL, 0) + sizeof(unsigned long long) * 2
40062306a36Sopenharmony_ci	    > page_size)
40162306a36Sopenharmony_ci		err("Impossible to run this test");
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	/*
40462306a36Sopenharmony_ci	 * Whether we can test certain features depends not just on test type,
40562306a36Sopenharmony_ci	 * but also on whether or not this particular kernel supports the
40662306a36Sopenharmony_ci	 * feature.
40762306a36Sopenharmony_ci	 */
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	if (userfaultfd_open(&features))
41062306a36Sopenharmony_ci		err("Userfaultfd open failed");
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	test_uffdio_wp = test_uffdio_wp &&
41362306a36Sopenharmony_ci		(features & UFFD_FEATURE_PAGEFAULT_FLAG_WP);
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	close(uffd);
41662306a36Sopenharmony_ci	uffd = -1;
41762306a36Sopenharmony_ci}
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_cistatic void sigalrm(int sig)
42062306a36Sopenharmony_ci{
42162306a36Sopenharmony_ci	if (sig != SIGALRM)
42262306a36Sopenharmony_ci		abort();
42362306a36Sopenharmony_ci	test_uffdio_copy_eexist = true;
42462306a36Sopenharmony_ci	alarm(ALARM_INTERVAL_SECS);
42562306a36Sopenharmony_ci}
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ciint main(int argc, char **argv)
42862306a36Sopenharmony_ci{
42962306a36Sopenharmony_ci	size_t bytes;
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci	if (argc < 4)
43262306a36Sopenharmony_ci		usage();
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	if (signal(SIGALRM, sigalrm) == SIG_ERR)
43562306a36Sopenharmony_ci		err("failed to arm SIGALRM");
43662306a36Sopenharmony_ci	alarm(ALARM_INTERVAL_SECS);
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	parse_test_type_arg(argv[1]);
43962306a36Sopenharmony_ci	bytes = atol(argv[2]) * 1024 * 1024;
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	nr_pages_per_cpu = bytes / page_size / nr_cpus;
44462306a36Sopenharmony_ci	if (!nr_pages_per_cpu) {
44562306a36Sopenharmony_ci		_err("invalid MiB");
44662306a36Sopenharmony_ci		usage();
44762306a36Sopenharmony_ci	}
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	bounces = atoi(argv[3]);
45062306a36Sopenharmony_ci	if (bounces <= 0) {
45162306a36Sopenharmony_ci		_err("invalid bounces");
45262306a36Sopenharmony_ci		usage();
45362306a36Sopenharmony_ci	}
45462306a36Sopenharmony_ci	nr_pages = nr_pages_per_cpu * nr_cpus;
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n",
45762306a36Sopenharmony_ci	       nr_pages, nr_pages_per_cpu);
45862306a36Sopenharmony_ci	return userfaultfd_stress();
45962306a36Sopenharmony_ci}
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci#else /* __NR_userfaultfd */
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci#warning "missing __NR_userfaultfd definition"
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ciint main(void)
46662306a36Sopenharmony_ci{
46762306a36Sopenharmony_ci	printf("skip: Skipping userfaultfd test (missing __NR_userfaultfd)\n");
46862306a36Sopenharmony_ci	return KSFT_SKIP;
46962306a36Sopenharmony_ci}
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci#endif /* __NR_userfaultfd */
472