KVM: selftests: Extract picking of random CPU from cpu_set_t to separate API

Extract kvm_pick_random_cpu() out of pin_task_to_random_cpu() so that tests
can choose a random CPU without having to immediately pin a task to that
CPU.

No functional change intended.

Link: https://patch.msgid.link/[email protected]
Signed-off-by: Sean Christopherson <[email protected]>
This commit is contained in:
Sean Christopherson committed 2026-07-31 13:54:09 -07:00
1 parent 8de0e85f65
commit d58cc7bbc5
2 files changed
+12 -6

No files matched your search

+10 -1
View File
@@ -1084,6 +1084,8 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm);
void kvm_set_files_rlimit(u32 nr_vcpus);
int kvm_pick_random_cpu(cpu_set_t *possible_cpus);
int __pin_task_to_cpu(pthread_t task, int cpu);
static inline void pin_task_to_cpu(pthread_t task, int cpu)
@@ -1094,7 +1096,14 @@ static inline void pin_task_to_cpu(pthread_t task, int cpu)
TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu);
}
int pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus);
static inline int pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus)
{
int cpu;
cpu = kvm_pick_random_cpu(possible_cpus);
pin_task_to_cpu(task, cpu);
return cpu;
}
static inline int pin_task_to_any_cpu(pthread_t task)
{
+2 -5
View File
@@ -668,7 +668,7 @@ void kvm_print_vcpu_pinning_help(void)
" (default: no pinning)\n", name, name);
}
int pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus)
int kvm_pick_random_cpu(cpu_set_t *possible_cpus)
{
int target_idx;
int nr_cpus;
@@ -680,12 +680,9 @@ int pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus)
target_idx = kvm_random_u64(&kvm_rng) % nr_cpus;
for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
if (CPU_ISSET(cpu, possible_cpus) && target_idx-- == 0) {
pin_task_to_cpu(task, cpu);
if (CPU_ISSET(cpu, possible_cpus) && target_idx-- == 0)
return cpu;
}
}
TEST_FAIL("Failed to find random CPU in possible_cpus");
return -1;
}