xref: /openbmc/linux/tools/testing/selftests/kvm/rseq_test.c (revision e923b0537d28e15c9d31ce8b38f810b325816903)
161e52f16SSean Christopherson // SPDX-License-Identifier: GPL-2.0-only
261e52f16SSean Christopherson #define _GNU_SOURCE /* for program_invocation_short_name */
361e52f16SSean Christopherson #include <errno.h>
461e52f16SSean Christopherson #include <fcntl.h>
561e52f16SSean Christopherson #include <pthread.h>
661e52f16SSean Christopherson #include <sched.h>
761e52f16SSean Christopherson #include <stdio.h>
861e52f16SSean Christopherson #include <stdlib.h>
961e52f16SSean Christopherson #include <string.h>
1061e52f16SSean Christopherson #include <signal.h>
1161e52f16SSean Christopherson #include <syscall.h>
1261e52f16SSean Christopherson #include <sys/ioctl.h>
137b0035eaSSean Christopherson #include <sys/sysinfo.h>
1461e52f16SSean Christopherson #include <asm/barrier.h>
1561e52f16SSean Christopherson #include <linux/atomic.h>
1661e52f16SSean Christopherson #include <linux/rseq.h>
1761e52f16SSean Christopherson #include <linux/unistd.h>
1861e52f16SSean Christopherson 
1961e52f16SSean Christopherson #include "kvm_util.h"
2061e52f16SSean Christopherson #include "processor.h"
2161e52f16SSean Christopherson #include "test_util.h"
2261e52f16SSean Christopherson 
2361e52f16SSean Christopherson #define VCPU_ID 0
2461e52f16SSean Christopherson 
2561e52f16SSean Christopherson static __thread volatile struct rseq __rseq = {
2661e52f16SSean Christopherson 	.cpu_id = RSEQ_CPU_ID_UNINITIALIZED,
2761e52f16SSean Christopherson };
2861e52f16SSean Christopherson 
2961e52f16SSean Christopherson /*
3061e52f16SSean Christopherson  * Use an arbitrary, bogus signature for configuring rseq, this test does not
3161e52f16SSean Christopherson  * actually enter an rseq critical section.
3261e52f16SSean Christopherson  */
3361e52f16SSean Christopherson #define RSEQ_SIG 0xdeadbeef
3461e52f16SSean Christopherson 
3561e52f16SSean Christopherson /*
3661e52f16SSean Christopherson  * Any bug related to task migration is likely to be timing-dependent; perform
3761e52f16SSean Christopherson  * a large number of migrations to reduce the odds of a false negative.
3861e52f16SSean Christopherson  */
3961e52f16SSean Christopherson #define NR_TASK_MIGRATIONS 100000
4061e52f16SSean Christopherson 
4161e52f16SSean Christopherson static pthread_t migration_thread;
4261e52f16SSean Christopherson static cpu_set_t possible_mask;
437b0035eaSSean Christopherson static int min_cpu, max_cpu;
4461e52f16SSean Christopherson static bool done;
4561e52f16SSean Christopherson 
4661e52f16SSean Christopherson static atomic_t seq_cnt;
4761e52f16SSean Christopherson 
4861e52f16SSean Christopherson static void guest_code(void)
4961e52f16SSean Christopherson {
5061e52f16SSean Christopherson 	for (;;)
5161e52f16SSean Christopherson 		GUEST_SYNC(0);
5261e52f16SSean Christopherson }
5361e52f16SSean Christopherson 
5461e52f16SSean Christopherson static void sys_rseq(int flags)
5561e52f16SSean Christopherson {
5661e52f16SSean Christopherson 	int r;
5761e52f16SSean Christopherson 
5861e52f16SSean Christopherson 	r = syscall(__NR_rseq, &__rseq, sizeof(__rseq), flags, RSEQ_SIG);
5961e52f16SSean Christopherson 	TEST_ASSERT(!r, "rseq failed, errno = %d (%s)", errno, strerror(errno));
6061e52f16SSean Christopherson }
6161e52f16SSean Christopherson 
627b0035eaSSean Christopherson static int next_cpu(int cpu)
637b0035eaSSean Christopherson {
647b0035eaSSean Christopherson 	/*
657b0035eaSSean Christopherson 	 * Advance to the next CPU, skipping those that weren't in the original
667b0035eaSSean Christopherson 	 * affinity set.  Sadly, there is no CPU_SET_FOR_EACH, and cpu_set_t's
677b0035eaSSean Christopherson 	 * data storage is considered as opaque.  Note, if this task is pinned
687b0035eaSSean Christopherson 	 * to a small set of discontigous CPUs, e.g. 2 and 1023, this loop will
697b0035eaSSean Christopherson 	 * burn a lot cycles and the test will take longer than normal to
707b0035eaSSean Christopherson 	 * complete.
717b0035eaSSean Christopherson 	 */
727b0035eaSSean Christopherson 	do {
737b0035eaSSean Christopherson 		cpu++;
747b0035eaSSean Christopherson 		if (cpu > max_cpu) {
757b0035eaSSean Christopherson 			cpu = min_cpu;
767b0035eaSSean Christopherson 			TEST_ASSERT(CPU_ISSET(cpu, &possible_mask),
777b0035eaSSean Christopherson 				    "Min CPU = %d must always be usable", cpu);
787b0035eaSSean Christopherson 			break;
797b0035eaSSean Christopherson 		}
807b0035eaSSean Christopherson 	} while (!CPU_ISSET(cpu, &possible_mask));
817b0035eaSSean Christopherson 
827b0035eaSSean Christopherson 	return cpu;
837b0035eaSSean Christopherson }
847b0035eaSSean Christopherson 
85*e923b053SGavin Shan static void *migration_worker(void *__rseq_tid)
8661e52f16SSean Christopherson {
87*e923b053SGavin Shan 	pid_t rseq_tid = (pid_t)(unsigned long)__rseq_tid;
8861e52f16SSean Christopherson 	cpu_set_t allowed_mask;
897b0035eaSSean Christopherson 	int r, i, cpu;
9061e52f16SSean Christopherson 
9161e52f16SSean Christopherson 	CPU_ZERO(&allowed_mask);
9261e52f16SSean Christopherson 
937b0035eaSSean Christopherson 	for (i = 0, cpu = min_cpu; i < NR_TASK_MIGRATIONS; i++, cpu = next_cpu(cpu)) {
9461e52f16SSean Christopherson 		CPU_SET(cpu, &allowed_mask);
9561e52f16SSean Christopherson 
9661e52f16SSean Christopherson 		/*
9761e52f16SSean Christopherson 		 * Bump the sequence count twice to allow the reader to detect
9861e52f16SSean Christopherson 		 * that a migration may have occurred in between rseq and sched
9961e52f16SSean Christopherson 		 * CPU ID reads.  An odd sequence count indicates a migration
10061e52f16SSean Christopherson 		 * is in-progress, while a completely different count indicates
10161e52f16SSean Christopherson 		 * a migration occurred since the count was last read.
10261e52f16SSean Christopherson 		 */
10361e52f16SSean Christopherson 		atomic_inc(&seq_cnt);
10461e52f16SSean Christopherson 
10561e52f16SSean Christopherson 		/*
10661e52f16SSean Christopherson 		 * Ensure the odd count is visible while sched_getcpu() isn't
10761e52f16SSean Christopherson 		 * stable, i.e. while changing affinity is in-progress.
10861e52f16SSean Christopherson 		 */
10961e52f16SSean Christopherson 		smp_wmb();
110*e923b053SGavin Shan 		r = sched_setaffinity(rseq_tid, sizeof(allowed_mask), &allowed_mask);
11161e52f16SSean Christopherson 		TEST_ASSERT(!r, "sched_setaffinity failed, errno = %d (%s)",
11261e52f16SSean Christopherson 			    errno, strerror(errno));
11361e52f16SSean Christopherson 		smp_wmb();
11461e52f16SSean Christopherson 		atomic_inc(&seq_cnt);
11561e52f16SSean Christopherson 
11661e52f16SSean Christopherson 		CPU_CLR(cpu, &allowed_mask);
11761e52f16SSean Christopherson 
11861e52f16SSean Christopherson 		/*
11961e52f16SSean Christopherson 		 * Wait 1-10us before proceeding to the next iteration and more
12061e52f16SSean Christopherson 		 * specifically, before bumping seq_cnt again.  A delay is
12161e52f16SSean Christopherson 		 * needed on three fronts:
12261e52f16SSean Christopherson 		 *
12361e52f16SSean Christopherson 		 *  1. To allow sched_setaffinity() to prompt migration before
12461e52f16SSean Christopherson 		 *     ioctl(KVM_RUN) enters the guest so that TIF_NOTIFY_RESUME
12561e52f16SSean Christopherson 		 *     (or TIF_NEED_RESCHED, which indirectly leads to handling
12661e52f16SSean Christopherson 		 *     NOTIFY_RESUME) is handled in KVM context.
12761e52f16SSean Christopherson 		 *
12861e52f16SSean Christopherson 		 *     If NOTIFY_RESUME/NEED_RESCHED is set after KVM enters
12961e52f16SSean Christopherson 		 *     the guest, the guest will trigger a IO/MMIO exit all the
13061e52f16SSean Christopherson 		 *     way to userspace and the TIF flags will be handled by
13161e52f16SSean Christopherson 		 *     the generic "exit to userspace" logic, not by KVM.  The
13261e52f16SSean Christopherson 		 *     exit to userspace is necessary to give the test a chance
13361e52f16SSean Christopherson 		 *     to check the rseq CPU ID (see #2).
13461e52f16SSean Christopherson 		 *
13561e52f16SSean Christopherson 		 *     Alternatively, guest_code() could include an instruction
13661e52f16SSean Christopherson 		 *     to trigger an exit that is handled by KVM, but any such
13761e52f16SSean Christopherson 		 *     exit requires architecture specific code.
13861e52f16SSean Christopherson 		 *
13961e52f16SSean Christopherson 		 *  2. To let ioctl(KVM_RUN) make its way back to the test
14061e52f16SSean Christopherson 		 *     before the next round of migration.  The test's check on
14161e52f16SSean Christopherson 		 *     the rseq CPU ID must wait for migration to complete in
14261e52f16SSean Christopherson 		 *     order to avoid false positive, thus any kernel rseq bug
14361e52f16SSean Christopherson 		 *     will be missed if the next migration starts before the
14461e52f16SSean Christopherson 		 *     check completes.
14561e52f16SSean Christopherson 		 *
14661e52f16SSean Christopherson 		 *  3. To ensure the read-side makes efficient forward progress,
14761e52f16SSean Christopherson 		 *     e.g. if sched_getcpu() involves a syscall.  Stalling the
14861e52f16SSean Christopherson 		 *     read-side means the test will spend more time waiting for
14961e52f16SSean Christopherson 		 *     sched_getcpu() to stabilize and less time trying to hit
15061e52f16SSean Christopherson 		 *     the timing-dependent bug.
15161e52f16SSean Christopherson 		 *
15261e52f16SSean Christopherson 		 * Because any bug in this area is likely to be timing-dependent,
15361e52f16SSean Christopherson 		 * run with a range of delays at 1us intervals from 1us to 10us
15461e52f16SSean Christopherson 		 * as a best effort to avoid tuning the test to the point where
15561e52f16SSean Christopherson 		 * it can hit _only_ the original bug and not detect future
15661e52f16SSean Christopherson 		 * regressions.
15761e52f16SSean Christopherson 		 *
15861e52f16SSean Christopherson 		 * The original bug can reproduce with a delay up to ~500us on
15961e52f16SSean Christopherson 		 * x86-64, but starts to require more iterations to reproduce
16061e52f16SSean Christopherson 		 * as the delay creeps above ~10us, and the average runtime of
16161e52f16SSean Christopherson 		 * each iteration obviously increases as well.  Cap the delay
16261e52f16SSean Christopherson 		 * at 10us to keep test runtime reasonable while minimizing
16361e52f16SSean Christopherson 		 * potential coverage loss.
16461e52f16SSean Christopherson 		 *
16561e52f16SSean Christopherson 		 * The lower bound for reproducing the bug is likely below 1us,
16661e52f16SSean Christopherson 		 * e.g. failures occur on x86-64 with nanosleep(0), but at that
16761e52f16SSean Christopherson 		 * point the overhead of the syscall likely dominates the delay.
16861e52f16SSean Christopherson 		 * Use usleep() for simplicity and to avoid unnecessary kernel
16961e52f16SSean Christopherson 		 * dependencies.
17061e52f16SSean Christopherson 		 */
17161e52f16SSean Christopherson 		usleep((i % 10) + 1);
17261e52f16SSean Christopherson 	}
17361e52f16SSean Christopherson 	done = true;
17461e52f16SSean Christopherson 	return NULL;
17561e52f16SSean Christopherson }
17661e52f16SSean Christopherson 
1777b0035eaSSean Christopherson static int calc_min_max_cpu(void)
1787b0035eaSSean Christopherson {
1797b0035eaSSean Christopherson 	int i, cnt, nproc;
1807b0035eaSSean Christopherson 
1817b0035eaSSean Christopherson 	if (CPU_COUNT(&possible_mask) < 2)
1827b0035eaSSean Christopherson 		return -EINVAL;
1837b0035eaSSean Christopherson 
1847b0035eaSSean Christopherson 	/*
1857b0035eaSSean Christopherson 	 * CPU_SET doesn't provide a FOR_EACH helper, get the min/max CPU that
1867b0035eaSSean Christopherson 	 * this task is affined to in order to reduce the time spent querying
1877b0035eaSSean Christopherson 	 * unusable CPUs, e.g. if this task is pinned to a small percentage of
1887b0035eaSSean Christopherson 	 * total CPUs.
1897b0035eaSSean Christopherson 	 */
1907b0035eaSSean Christopherson 	nproc = get_nprocs_conf();
1917b0035eaSSean Christopherson 	min_cpu = -1;
1927b0035eaSSean Christopherson 	max_cpu = -1;
1937b0035eaSSean Christopherson 	cnt = 0;
1947b0035eaSSean Christopherson 
1957b0035eaSSean Christopherson 	for (i = 0; i < nproc; i++) {
1967b0035eaSSean Christopherson 		if (!CPU_ISSET(i, &possible_mask))
1977b0035eaSSean Christopherson 			continue;
1987b0035eaSSean Christopherson 		if (min_cpu == -1)
1997b0035eaSSean Christopherson 			min_cpu = i;
2007b0035eaSSean Christopherson 		max_cpu = i;
2017b0035eaSSean Christopherson 		cnt++;
2027b0035eaSSean Christopherson 	}
2037b0035eaSSean Christopherson 
2047b0035eaSSean Christopherson 	return (cnt < 2) ? -EINVAL : 0;
2057b0035eaSSean Christopherson }
2067b0035eaSSean Christopherson 
20761e52f16SSean Christopherson int main(int argc, char *argv[])
20861e52f16SSean Christopherson {
20961e52f16SSean Christopherson 	int r, i, snapshot;
21061e52f16SSean Christopherson 	struct kvm_vm *vm;
21161e52f16SSean Christopherson 	u32 cpu, rseq_cpu;
21261e52f16SSean Christopherson 
21361e52f16SSean Christopherson 	/* Tell stdout not to buffer its content */
21461e52f16SSean Christopherson 	setbuf(stdout, NULL);
21561e52f16SSean Christopherson 
21661e52f16SSean Christopherson 	r = sched_getaffinity(0, sizeof(possible_mask), &possible_mask);
21761e52f16SSean Christopherson 	TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)", errno,
21861e52f16SSean Christopherson 		    strerror(errno));
21961e52f16SSean Christopherson 
2207b0035eaSSean Christopherson 	if (calc_min_max_cpu()) {
2217b0035eaSSean Christopherson 		print_skip("Only one usable CPU, task migration not possible");
22261e52f16SSean Christopherson 		exit(KSFT_SKIP);
22361e52f16SSean Christopherson 	}
22461e52f16SSean Christopherson 
22561e52f16SSean Christopherson 	sys_rseq(0);
22661e52f16SSean Christopherson 
22761e52f16SSean Christopherson 	/*
22861e52f16SSean Christopherson 	 * Create and run a dummy VM that immediately exits to userspace via
22961e52f16SSean Christopherson 	 * GUEST_SYNC, while concurrently migrating the process by setting its
23061e52f16SSean Christopherson 	 * CPU affinity.
23161e52f16SSean Christopherson 	 */
23261e52f16SSean Christopherson 	vm = vm_create_default(VCPU_ID, 0, guest_code);
233fbf094ceSOliver Upton 	ucall_init(vm, NULL);
23461e52f16SSean Christopherson 
235*e923b053SGavin Shan 	pthread_create(&migration_thread, NULL, migration_worker,
236*e923b053SGavin Shan 		       (void *)(unsigned long)gettid());
23761e52f16SSean Christopherson 
23861e52f16SSean Christopherson 	for (i = 0; !done; i++) {
23961e52f16SSean Christopherson 		vcpu_run(vm, VCPU_ID);
24061e52f16SSean Christopherson 		TEST_ASSERT(get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC,
24161e52f16SSean Christopherson 			    "Guest failed?");
24261e52f16SSean Christopherson 
24361e52f16SSean Christopherson 		/*
24461e52f16SSean Christopherson 		 * Verify rseq's CPU matches sched's CPU.  Ensure migration
24561e52f16SSean Christopherson 		 * doesn't occur between sched_getcpu() and reading the rseq
24661e52f16SSean Christopherson 		 * cpu_id by rereading both if the sequence count changes, or
24761e52f16SSean Christopherson 		 * if the count is odd (migration in-progress).
24861e52f16SSean Christopherson 		 */
24961e52f16SSean Christopherson 		do {
25061e52f16SSean Christopherson 			/*
25161e52f16SSean Christopherson 			 * Drop bit 0 to force a mismatch if the count is odd,
25261e52f16SSean Christopherson 			 * i.e. if a migration is in-progress.
25361e52f16SSean Christopherson 			 */
25461e52f16SSean Christopherson 			snapshot = atomic_read(&seq_cnt) & ~1;
25561e52f16SSean Christopherson 
25661e52f16SSean Christopherson 			/*
25761e52f16SSean Christopherson 			 * Ensure reading sched_getcpu() and rseq.cpu_id
25861e52f16SSean Christopherson 			 * complete in a single "no migration" window, i.e. are
25961e52f16SSean Christopherson 			 * not reordered across the seq_cnt reads.
26061e52f16SSean Christopherson 			 */
26161e52f16SSean Christopherson 			smp_rmb();
26261e52f16SSean Christopherson 			cpu = sched_getcpu();
26361e52f16SSean Christopherson 			rseq_cpu = READ_ONCE(__rseq.cpu_id);
26461e52f16SSean Christopherson 			smp_rmb();
26561e52f16SSean Christopherson 		} while (snapshot != atomic_read(&seq_cnt));
26661e52f16SSean Christopherson 
26761e52f16SSean Christopherson 		TEST_ASSERT(rseq_cpu == cpu,
26861e52f16SSean Christopherson 			    "rseq CPU = %d, sched CPU = %d\n", rseq_cpu, cpu);
26961e52f16SSean Christopherson 	}
27061e52f16SSean Christopherson 
27161e52f16SSean Christopherson 	/*
27261e52f16SSean Christopherson 	 * Sanity check that the test was able to enter the guest a reasonable
27361e52f16SSean Christopherson 	 * number of times, e.g. didn't get stalled too often/long waiting for
27461e52f16SSean Christopherson 	 * sched_getcpu() to stabilize.  A 2:1 migration:KVM_RUN ratio is a
27561e52f16SSean Christopherson 	 * fairly conservative ratio on x86-64, which can do _more_ KVM_RUNs
27661e52f16SSean Christopherson 	 * than migrations given the 1us+ delay in the migration task.
27761e52f16SSean Christopherson 	 */
27861e52f16SSean Christopherson 	TEST_ASSERT(i > (NR_TASK_MIGRATIONS / 2),
27961e52f16SSean Christopherson 		    "Only performed %d KVM_RUNs, task stalled too much?\n", i);
28061e52f16SSean Christopherson 
28161e52f16SSean Christopherson 	pthread_join(migration_thread, NULL);
28261e52f16SSean Christopherson 
28361e52f16SSean Christopherson 	kvm_vm_free(vm);
28461e52f16SSean Christopherson 
28561e52f16SSean Christopherson 	sys_rseq(RSEQ_FLAG_UNREGISTER);
28661e52f16SSean Christopherson 
28761e52f16SSean Christopherson 	return 0;
28861e52f16SSean Christopherson }
289