xref: /linux/tools/testing/selftests/kvm/arch_timer.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * arch_timer.c - Tests the arch timer IRQ functionality
4  *
5  * The guest's main thread configures the timer interrupt and waits
6  * for it to fire, with a timeout equal to the timer period.
7  * It asserts that the timeout doesn't exceed the timer period plus
8  * a user configurable error margin(default to 100us)
9  *
10  * On the other hand, upon receipt of an interrupt, the guest's interrupt
11  * handler validates the interrupt by checking if the architectural state
12  * is in compliance with the specifications.
13  *
14  * The test provides command-line options to configure the timer's
15  * period (-p), number of vCPUs (-n), iterations per stage (-i) and timer
16  * interrupt arrival error margin (-e). To stress-test the timer stack
17  * even more, an option to migrate the vCPUs across pCPUs (-m), at a
18  * particular rate, is also provided.
19  *
20  * Copyright (c) 2021, Google LLC.
21  */
22 #include <stdlib.h>
23 #include <pthread.h>
24 #include <linux/sizes.h>
25 #include <linux/bitmap.h>
26 #include <sys/sysinfo.h>
27 
28 #include "timer_test.h"
29 #include "ucall_common.h"
30 
31 struct test_args test_args = {
32 	.nr_vcpus = NR_VCPUS_DEF,
33 	.nr_iter = NR_TEST_ITERS_DEF,
34 	.timer_period_ms = TIMER_TEST_PERIOD_MS_DEF,
35 	.migration_freq_ms = TIMER_TEST_MIGRATION_FREQ_MS,
36 	.timer_err_margin_us = TIMER_TEST_ERR_MARGIN_US,
37 	.reserved = 1,
38 };
39 
40 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
41 struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];
42 
43 static pthread_t pt_vcpu_run[KVM_MAX_VCPUS];
44 static unsigned long *vcpu_done_map;
45 static pthread_mutex_t vcpu_done_map_lock;
46 
47 static void *test_vcpu_run(void *arg)
48 {
49 	unsigned int vcpu_idx = (unsigned long)arg;
50 	struct ucall uc;
51 	struct kvm_vcpu *vcpu = vcpus[vcpu_idx];
52 	struct kvm_vm *vm = vcpu->vm;
53 	struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[vcpu_idx];
54 
55 	vcpu_run(vcpu);
56 
57 	/* Currently, any exit from guest is an indication of completion */
58 	pthread_mutex_lock(&vcpu_done_map_lock);
59 	__set_bit(vcpu_idx, vcpu_done_map);
60 	pthread_mutex_unlock(&vcpu_done_map_lock);
61 
62 	switch (get_ucall(vcpu, &uc)) {
63 	case UCALL_SYNC:
64 	case UCALL_DONE:
65 		break;
66 	case UCALL_ABORT:
67 		sync_global_from_guest(vm, *shared_data);
68 		fprintf(stderr, "Guest assert failed,  vcpu %u; stage; %u; iter: %u\n",
69 			vcpu_idx, shared_data->guest_stage, shared_data->nr_iter);
70 		REPORT_GUEST_ASSERT(uc);
71 		break;
72 	default:
73 		TEST_FAIL("Unexpected guest exit");
74 	}
75 
76 	pr_info("PASS(vCPU-%d).\n", vcpu_idx);
77 
78 	return NULL;
79 }
80 
81 static u32 test_get_pcpu(void)
82 {
83 	u32 pcpu;
84 	unsigned int nproc_conf;
85 	cpu_set_t online_cpuset;
86 
87 	nproc_conf = get_nprocs_conf();
88 	kvm_sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset);
89 
90 	/* Randomly find an available pCPU to place a vCPU on */
91 	do {
92 		pcpu = rand() % nproc_conf;
93 	} while (!CPU_ISSET(pcpu, &online_cpuset));
94 
95 	return pcpu;
96 }
97 
98 static int test_migrate_vcpu(unsigned int vcpu_idx)
99 {
100 	int ret;
101 	u32 new_pcpu = test_get_pcpu();
102 
103 	pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu);
104 
105 	ret = __pin_task_to_cpu(pt_vcpu_run[vcpu_idx], new_pcpu);
106 
107 	/* Allow the error where the vCPU thread is already finished */
108 	TEST_ASSERT(ret == 0 || ret == ESRCH,
109 		    "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d",
110 		    vcpu_idx, new_pcpu, ret);
111 
112 	return ret;
113 }
114 
115 static void *test_vcpu_migration(void *arg)
116 {
117 	unsigned int i, n_done;
118 	bool vcpu_done;
119 
120 	do {
121 		usleep(msecs_to_usecs(test_args.migration_freq_ms));
122 
123 		for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
124 			pthread_mutex_lock(&vcpu_done_map_lock);
125 			vcpu_done = test_bit(i, vcpu_done_map);
126 			pthread_mutex_unlock(&vcpu_done_map_lock);
127 
128 			if (vcpu_done) {
129 				n_done++;
130 				continue;
131 			}
132 
133 			test_migrate_vcpu(i);
134 		}
135 	} while (test_args.nr_vcpus != n_done);
136 
137 	return NULL;
138 }
139 
140 static void test_run(struct kvm_vm *vm)
141 {
142 	pthread_t pt_vcpu_migration;
143 	unsigned int i;
144 
145 	pthread_mutex_init(&vcpu_done_map_lock, NULL);
146 	vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
147 	TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap");
148 
149 	for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++)
150 		kvm_pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run,
151 				   (void *)(unsigned long)i);
152 
153 	/* Spawn a thread to control the vCPU migrations */
154 	if (test_args.migration_freq_ms) {
155 		srand(time(NULL));
156 
157 		kvm_pthread_create(&pt_vcpu_migration, NULL, test_vcpu_migration, NULL);
158 	}
159 
160 	for (i = 0; i < test_args.nr_vcpus; i++)
161 		kvm_pthread_join(pt_vcpu_run[i], NULL);
162 
163 	if (test_args.migration_freq_ms)
164 		kvm_pthread_join(pt_vcpu_migration, NULL);
165 
166 	bitmap_free(vcpu_done_map);
167 }
168 
169 static void test_print_help(char *name)
170 {
171 	pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n"
172 		"\t\t    [-m migration_freq_ms] [-o counter_offset]\n"
173 		"\t\t    [-e timer_err_margin_us]\n", name);
174 	pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",
175 		NR_VCPUS_DEF, KVM_MAX_VCPUS);
176 	pr_info("\t-i: Number of iterations per stage (default: %u)\n",
177 		NR_TEST_ITERS_DEF);
178 	pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
179 		TIMER_TEST_PERIOD_MS_DEF);
180 	pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
181 		TIMER_TEST_MIGRATION_FREQ_MS);
182 	pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n");
183 	pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n",
184 		TIMER_TEST_ERR_MARGIN_US);
185 	pr_info("\t-h: print this help screen\n");
186 }
187 
188 static bool parse_args(int argc, char *argv[])
189 {
190 	int opt;
191 
192 	while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) {
193 		switch (opt) {
194 		case 'n':
195 			test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
196 			if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
197 				pr_info("Max allowed vCPUs: %u\n",
198 					KVM_MAX_VCPUS);
199 				goto err;
200 			}
201 			break;
202 		case 'i':
203 			test_args.nr_iter = atoi_positive("Number of iterations", optarg);
204 			break;
205 		case 'p':
206 			test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
207 			break;
208 		case 'm':
209 			test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
210 			break;
211 		case 'e':
212 			test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg);
213 			break;
214 		case 'o':
215 			test_args.counter_offset = strtol(optarg, NULL, 0);
216 			test_args.reserved = 0;
217 			break;
218 		case 'h':
219 		default:
220 			goto err;
221 		}
222 	}
223 
224 	return true;
225 
226 err:
227 	test_print_help(argv[0]);
228 	return false;
229 }
230 
231 int main(int argc, char *argv[])
232 {
233 	struct kvm_vm *vm;
234 
235 	if (!parse_args(argc, argv))
236 		exit(KSFT_SKIP);
237 
238 	__TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2,
239 		       "At least two physical CPUs needed for vCPU migration");
240 
241 	vm = test_vm_create();
242 	test_run(vm);
243 	test_vm_cleanup(vm);
244 
245 	return 0;
246 }
247