[PATCH v2 03/12] KVM: selftests: Pre-set threads affinity in hardware disable test when possible
From: Sean Christopherson
Date: Fri Jul 31 2026 - 13:16:08 EST
When building against a libc that provides pthread_attr_setaffinity_np(),
pre-set the grandchildren threads CPU affinity in the hardware disable test
to further reduce the perceived latency of pthread_create(). On large NUMA
systems, this reduces the average runtime from ~10s to ~3.5s.
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
.../testing/selftests/kvm/hardware_disable_test.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/kvm/hardware_disable_test.c b/tools/testing/selftests/kvm/hardware_disable_test.c
index f088b4af3ccc..d8288147b0a6 100644
--- a/tools/testing/selftests/kvm/hardware_disable_test.c
+++ b/tools/testing/selftests/kvm/hardware_disable_test.c
@@ -36,7 +36,9 @@ static void *run_vcpu(void *arg)
struct kvm_vcpu *vcpu = arg;
struct kvm_run *run = vcpu->run;
+#ifndef __USE_GNU
kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
+#endif
vcpu_run(vcpu);
@@ -50,7 +52,9 @@ static void *sleeping_thread(void *arg)
{
int fd;
+#ifndef __USE_GNU
kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
+#endif
while (true) {
fd = open("/dev/null", O_RDWR);
@@ -80,22 +84,28 @@ static inline void check_join(pthread_t thread, void **retval)
static void run_test(u32 run)
{
struct kvm_vcpu *vcpu;
+ pthread_attr_t attr;
struct kvm_vm *vm;
pthread_t threads[VCPU_NUM];
pthread_t throw_away;
void *b;
u32 i, j;
+ pthread_attr_init(&attr);
+#ifdef __USE_GNU
+ pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &threads_cpu_set);
+#endif
+
vm = vm_create(VCPU_NUM);
pr_debug("%s: [%d] start vcpus\n", __func__, run);
for (i = 0; i < VCPU_NUM; ++i) {
vcpu = vm_vcpu_add(vm, i, guest_code);
- check_create_thread(&threads[i], NULL, run_vcpu, vcpu);
+ check_create_thread(&threads[i], &attr, run_vcpu, vcpu);
for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
- check_create_thread(&throw_away, NULL, sleeping_thread,
+ check_create_thread(&throw_away, &attr, sleeping_thread,
(void *)NULL);
}
}
--
2.55.0.508.g3f0d502094-goog