Skip to content

Commit 61f6fad

Browse files
rananta468Marc Zyngier
authored andcommitted
KVM: arm64: selftests: arch_timer: Support vCPU migration
Since the timer stack (hardware and KVM) is per-CPU, there are potential chances for races to occur when the scheduler decides to migrate a vCPU thread to a different physical CPU. Hence, include an option to stress-test this part as well by forcing the vCPUs to migrate across physical CPUs in the system at a particular rate. Originally, the bug for the fix with commit 3134cc8 ("KVM: arm64: vgic: Resample HW pending state on deactivation") was discovered using arch_timer test with vCPU migrations and can be easily reproduced. Signed-off-by: Raghavendra Rao Ananta <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4959d86 commit 61f6fad

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

tools/testing/selftests/kvm/aarch64/arch_timer.c

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*
1515
* The test provides command-line options to configure the timer's
1616
* period (-p), number of vCPUs (-n), and iterations per stage (-i).
17+
* To stress-test the timer stack even more, an option to migrate the
18+
* vCPUs across pCPUs (-m), at a particular rate, is also provided.
1719
*
1820
* Copyright (c) 2021, Google LLC.
1921
*/
@@ -24,6 +26,8 @@
2426
#include <pthread.h>
2527
#include <linux/kvm.h>
2628
#include <linux/sizes.h>
29+
#include <linux/bitmap.h>
30+
#include <sys/sysinfo.h>
2731

2832
#include "kvm_util.h"
2933
#include "processor.h"
@@ -36,17 +40,20 @@
3640
#define NR_TEST_ITERS_DEF 5
3741
#define TIMER_TEST_PERIOD_MS_DEF 10
3842
#define TIMER_TEST_ERR_MARGIN_US 100
43+
#define TIMER_TEST_MIGRATION_FREQ_MS 2
3944

4045
struct test_args {
4146
int nr_vcpus;
4247
int nr_iter;
4348
int timer_period_ms;
49+
int migration_freq_ms;
4450
};
4551

4652
static struct test_args test_args = {
4753
.nr_vcpus = NR_VCPUS_DEF,
4854
.nr_iter = NR_TEST_ITERS_DEF,
4955
.timer_period_ms = TIMER_TEST_PERIOD_MS_DEF,
56+
.migration_freq_ms = TIMER_TEST_MIGRATION_FREQ_MS,
5057
};
5158

5259
#define msecs_to_usecs(msec) ((msec) * 1000LL)
@@ -80,6 +87,9 @@ static struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];
8087

8188
static int vtimer_irq, ptimer_irq;
8289

90+
static unsigned long *vcpu_done_map;
91+
static pthread_mutex_t vcpu_done_map_lock;
92+
8393
static void
8494
guest_configure_timer_action(struct test_vcpu_shared_data *shared_data)
8595
{
@@ -215,6 +225,11 @@ static void *test_vcpu_run(void *arg)
215225

216226
vcpu_run(vm, vcpuid);
217227

228+
/* Currently, any exit from guest is an indication of completion */
229+
pthread_mutex_lock(&vcpu_done_map_lock);
230+
set_bit(vcpuid, vcpu_done_map);
231+
pthread_mutex_unlock(&vcpu_done_map_lock);
232+
218233
switch (get_ucall(vm, vcpuid, &uc)) {
219234
case UCALL_SYNC:
220235
case UCALL_DONE:
@@ -233,18 +248,102 @@ static void *test_vcpu_run(void *arg)
233248
return NULL;
234249
}
235250

251+
static uint32_t test_get_pcpu(void)
252+
{
253+
uint32_t pcpu;
254+
unsigned int nproc_conf;
255+
cpu_set_t online_cpuset;
256+
257+
nproc_conf = get_nprocs_conf();
258+
sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset);
259+
260+
/* Randomly find an available pCPU to place a vCPU on */
261+
do {
262+
pcpu = rand() % nproc_conf;
263+
} while (!CPU_ISSET(pcpu, &online_cpuset));
264+
265+
return pcpu;
266+
}
267+
268+
static int test_migrate_vcpu(struct test_vcpu *vcpu)
269+
{
270+
int ret;
271+
cpu_set_t cpuset;
272+
uint32_t new_pcpu = test_get_pcpu();
273+
274+
CPU_ZERO(&cpuset);
275+
CPU_SET(new_pcpu, &cpuset);
276+
277+
pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu->vcpuid, new_pcpu);
278+
279+
ret = pthread_setaffinity_np(vcpu->pt_vcpu_run,
280+
sizeof(cpuset), &cpuset);
281+
282+
/* Allow the error where the vCPU thread is already finished */
283+
TEST_ASSERT(ret == 0 || ret == ESRCH,
284+
"Failed to migrate the vCPU:%u to pCPU: %u; ret: %d\n",
285+
vcpu->vcpuid, new_pcpu, ret);
286+
287+
return ret;
288+
}
289+
290+
static void *test_vcpu_migration(void *arg)
291+
{
292+
unsigned int i, n_done;
293+
bool vcpu_done;
294+
295+
do {
296+
usleep(msecs_to_usecs(test_args.migration_freq_ms));
297+
298+
for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
299+
pthread_mutex_lock(&vcpu_done_map_lock);
300+
vcpu_done = test_bit(i, vcpu_done_map);
301+
pthread_mutex_unlock(&vcpu_done_map_lock);
302+
303+
if (vcpu_done) {
304+
n_done++;
305+
continue;
306+
}
307+
308+
test_migrate_vcpu(&test_vcpu[i]);
309+
}
310+
} while (test_args.nr_vcpus != n_done);
311+
312+
return NULL;
313+
}
314+
236315
static void test_run(struct kvm_vm *vm)
237316
{
238317
int i, ret;
318+
pthread_t pt_vcpu_migration;
319+
320+
pthread_mutex_init(&vcpu_done_map_lock, NULL);
321+
vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
322+
TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap\n");
239323

240324
for (i = 0; i < test_args.nr_vcpus; i++) {
241325
ret = pthread_create(&test_vcpu[i].pt_vcpu_run, NULL,
242326
test_vcpu_run, &test_vcpu[i]);
243327
TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread\n", i);
244328
}
245329

330+
/* Spawn a thread to control the vCPU migrations */
331+
if (test_args.migration_freq_ms) {
332+
srand(time(NULL));
333+
334+
ret = pthread_create(&pt_vcpu_migration, NULL,
335+
test_vcpu_migration, NULL);
336+
TEST_ASSERT(!ret, "Failed to create the migration pthread\n");
337+
}
338+
339+
246340
for (i = 0; i < test_args.nr_vcpus; i++)
247341
pthread_join(test_vcpu[i].pt_vcpu_run, NULL);
342+
343+
if (test_args.migration_freq_ms)
344+
pthread_join(pt_vcpu_migration, NULL);
345+
346+
bitmap_free(vcpu_done_map);
248347
}
249348

250349
static void test_init_timer_irq(struct kvm_vm *vm)
@@ -301,14 +400,16 @@ static void test_print_help(char *name)
301400
NR_TEST_ITERS_DEF);
302401
pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
303402
TIMER_TEST_PERIOD_MS_DEF);
403+
pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
404+
TIMER_TEST_MIGRATION_FREQ_MS);
304405
pr_info("\t-h: print this help screen\n");
305406
}
306407

307408
static bool parse_args(int argc, char *argv[])
308409
{
309410
int opt;
310411

311-
while ((opt = getopt(argc, argv, "hn:i:p:")) != -1) {
412+
while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) {
312413
switch (opt) {
313414
case 'n':
314415
test_args.nr_vcpus = atoi(optarg);
@@ -335,6 +436,13 @@ static bool parse_args(int argc, char *argv[])
335436
goto err;
336437
}
337438
break;
439+
case 'm':
440+
test_args.migration_freq_ms = atoi(optarg);
441+
if (test_args.migration_freq_ms < 0) {
442+
pr_info("0 or positive value needed for -m\n");
443+
goto err;
444+
}
445+
break;
338446
case 'h':
339447
default:
340448
goto err;
@@ -358,6 +466,11 @@ int main(int argc, char *argv[])
358466
if (!parse_args(argc, argv))
359467
exit(KSFT_SKIP);
360468

469+
if (test_args.migration_freq_ms && get_nprocs() < 2) {
470+
print_skip("At least two physical CPUs needed for vCPU migration");
471+
exit(KSFT_SKIP);
472+
}
473+
361474
vm = test_vm_create();
362475
test_run(vm);
363476
kvm_vm_free(vm);

0 commit comments

Comments
 (0)