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/20260731195612.2697986-6-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson
2026-07-31 12:56:05 -07:00
parent 8de0e85f65
commit d58cc7bbc5
2 changed files with 12 additions and 6 deletions

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)
{

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;
}