Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.15.
  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
 31struct 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
 40struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
 41struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];
 42
 43static pthread_t pt_vcpu_run[KVM_MAX_VCPUS];
 44static unsigned long *vcpu_done_map;
 45static pthread_mutex_t vcpu_done_map_lock;
 46
 47static 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
 81static uint32_t test_get_pcpu(void)
 82{
 83	uint32_t pcpu;
 84	unsigned int nproc_conf;
 85	cpu_set_t online_cpuset;
 86
 87	nproc_conf = get_nprocs_conf();
 88	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
 98static int test_migrate_vcpu(unsigned int vcpu_idx)
 99{
100	int ret;
101	cpu_set_t cpuset;
102	uint32_t new_pcpu = test_get_pcpu();
103
104	CPU_ZERO(&cpuset);
105	CPU_SET(new_pcpu, &cpuset);
106
107	pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu);
108
109	ret = pthread_setaffinity_np(pt_vcpu_run[vcpu_idx],
110				     sizeof(cpuset), &cpuset);
111
112	/* Allow the error where the vCPU thread is already finished */
113	TEST_ASSERT(ret == 0 || ret == ESRCH,
114		    "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d",
115		    vcpu_idx, new_pcpu, ret);
116
117	return ret;
118}
119
120static void *test_vcpu_migration(void *arg)
121{
122	unsigned int i, n_done;
123	bool vcpu_done;
124
125	do {
126		usleep(msecs_to_usecs(test_args.migration_freq_ms));
127
128		for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
129			pthread_mutex_lock(&vcpu_done_map_lock);
130			vcpu_done = test_bit(i, vcpu_done_map);
131			pthread_mutex_unlock(&vcpu_done_map_lock);
132
133			if (vcpu_done) {
134				n_done++;
135				continue;
136			}
137
138			test_migrate_vcpu(i);
139		}
140	} while (test_args.nr_vcpus != n_done);
141
142	return NULL;
143}
144
145static void test_run(struct kvm_vm *vm)
146{
147	pthread_t pt_vcpu_migration;
148	unsigned int i;
149	int ret;
150
151	pthread_mutex_init(&vcpu_done_map_lock, NULL);
152	vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
153	TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap");
154
155	for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) {
156		ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run,
157				     (void *)(unsigned long)i);
158		TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread", i);
159	}
160
161	/* Spawn a thread to control the vCPU migrations */
162	if (test_args.migration_freq_ms) {
163		srand(time(NULL));
164
165		ret = pthread_create(&pt_vcpu_migration, NULL,
166					test_vcpu_migration, NULL);
167		TEST_ASSERT(!ret, "Failed to create the migration pthread");
168	}
169
170
171	for (i = 0; i < test_args.nr_vcpus; i++)
172		pthread_join(pt_vcpu_run[i], NULL);
173
174	if (test_args.migration_freq_ms)
175		pthread_join(pt_vcpu_migration, NULL);
176
177	bitmap_free(vcpu_done_map);
178}
179
180static void test_print_help(char *name)
181{
182	pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n"
183		"\t\t    [-m migration_freq_ms] [-o counter_offset]\n"
184		"\t\t    [-e timer_err_margin_us]\n", name);
185	pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",
186		NR_VCPUS_DEF, KVM_MAX_VCPUS);
187	pr_info("\t-i: Number of iterations per stage (default: %u)\n",
188		NR_TEST_ITERS_DEF);
189	pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
190		TIMER_TEST_PERIOD_MS_DEF);
191	pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
192		TIMER_TEST_MIGRATION_FREQ_MS);
193	pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n");
194	pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n",
195		TIMER_TEST_ERR_MARGIN_US);
196	pr_info("\t-h: print this help screen\n");
197}
198
199static bool parse_args(int argc, char *argv[])
200{
201	int opt;
202
203	while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) {
204		switch (opt) {
205		case 'n':
206			test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
207			if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
208				pr_info("Max allowed vCPUs: %u\n",
209					KVM_MAX_VCPUS);
210				goto err;
211			}
212			break;
213		case 'i':
214			test_args.nr_iter = atoi_positive("Number of iterations", optarg);
215			break;
216		case 'p':
217			test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
218			break;
219		case 'm':
220			test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
221			break;
222		case 'e':
223			test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg);
224			break;
225		case 'o':
226			test_args.counter_offset = strtol(optarg, NULL, 0);
227			test_args.reserved = 0;
228			break;
229		case 'h':
230		default:
231			goto err;
232		}
233	}
234
235	return true;
236
237err:
238	test_print_help(argv[0]);
239	return false;
240}
241
242int main(int argc, char *argv[])
243{
244	struct kvm_vm *vm;
245
246	if (!parse_args(argc, argv))
247		exit(KSFT_SKIP);
248
249	__TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2,
250		       "At least two physical CPUs needed for vCPU migration");
251
252	vm = test_vm_create();
253	test_run(vm);
254	test_vm_cleanup(vm);
255
256	return 0;
257}