Skip to content

Commit e83ee6f

Browse files
committed
KVM: selftests: Expand set of APIs for pinning tasks to a single CPU
Expand kvm_pin_this_task_to_pcpu() into a set of APIs to allow pinning a task (or self) to a CPU (any or specific). This will allow deduplicating code throughout a variety of selftests. Opportunistically use "self" instead of "this_task" as it is both more concise and less ambiguous. Link: https://lore.kernel.org/r/20250626001225.744268-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent a7cec20 commit e83ee6f

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <sys/ioctl.h>
2222

23+
#include <pthread.h>
24+
2325
#include "kvm_util_arch.h"
2426
#include "kvm_util_types.h"
2527
#include "sparsebit.h"
@@ -1013,7 +1015,34 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm);
10131015

10141016
void kvm_set_files_rlimit(uint32_t nr_vcpus);
10151017

1016-
void kvm_pin_this_task_to_pcpu(uint32_t pcpu);
1018+
int __pin_task_to_cpu(pthread_t task, int cpu);
1019+
1020+
static inline void pin_task_to_cpu(pthread_t task, int cpu)
1021+
{
1022+
int r;
1023+
1024+
r = __pin_task_to_cpu(task, cpu);
1025+
TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu);
1026+
}
1027+
1028+
static inline int pin_task_to_any_cpu(pthread_t task)
1029+
{
1030+
int cpu = sched_getcpu();
1031+
1032+
pin_task_to_cpu(task, cpu);
1033+
return cpu;
1034+
}
1035+
1036+
static inline void pin_self_to_cpu(int cpu)
1037+
{
1038+
pin_task_to_cpu(pthread_self(), cpu);
1039+
}
1040+
1041+
static inline int pin_self_to_any_cpu(void)
1042+
{
1043+
return pin_task_to_any_cpu(pthread_self());
1044+
}
1045+
10171046
void kvm_print_vcpu_pinning_help(void);
10181047
void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
10191048
int nr_vcpus);

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -605,15 +605,14 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm)
605605
return vm_vcpu_recreate(vm, 0);
606606
}
607607

608-
void kvm_pin_this_task_to_pcpu(uint32_t pcpu)
608+
int __pin_task_to_cpu(pthread_t task, int cpu)
609609
{
610-
cpu_set_t mask;
611-
int r;
610+
cpu_set_t cpuset;
612611

613-
CPU_ZERO(&mask);
614-
CPU_SET(pcpu, &mask);
615-
r = sched_setaffinity(0, sizeof(mask), &mask);
616-
TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.", pcpu);
612+
CPU_ZERO(&cpuset);
613+
CPU_SET(cpu, &cpuset);
614+
615+
return pthread_setaffinity_np(task, sizeof(cpuset), &cpuset);
617616
}
618617

619618
static uint32_t parse_pcpu(const char *cpu_str, const cpu_set_t *allowed_mask)
@@ -667,7 +666,7 @@ void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
667666

668667
/* 2. Check if the main worker needs to be pinned. */
669668
if (cpu) {
670-
kvm_pin_this_task_to_pcpu(parse_pcpu(cpu, &allowed_mask));
669+
pin_self_to_cpu(parse_pcpu(cpu, &allowed_mask));
671670
cpu = strtok(NULL, delim);
672671
}
673672

tools/testing/selftests/kvm/lib/memstress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void *vcpu_thread_main(void *data)
265265
int vcpu_idx = vcpu->vcpu_idx;
266266

267267
if (memstress_args.pin_vcpus)
268-
kvm_pin_this_task_to_pcpu(memstress_args.vcpu_to_pcpu[vcpu_idx]);
268+
pin_self_to_cpu(memstress_args.vcpu_to_pcpu[vcpu_idx]);
269269

270270
WRITE_ONCE(vcpu->running, true);
271271

0 commit comments

Comments
 (0)