KVM: selftests: Use KVM's task pinning APIs in steal_time
PATCH 1/1 KVM: selftests: Use KVM's task pinning APIs in steal_time
From: Sean Christopherson <[email protected]>
Use pin_self_to_cpu() and pin_task_to_cpu() to pin the vCPU thread and the stealer thread to pCPU0 in the steal_time test. Eliminating the usage of pthread_attr_setaffinity_np() in particular allows building the test against non-glibc C libraries.
--- tools/testing/selftests/kvm/steal_time.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/steal_time.c +4 -8
@@ -508,9 +508,7 @@ int main(int ac, char **av) {struct kvm_vcpu *vcpus[NR_VCPUS ];struct kvm_vm *vm; -pthread_attr_t attr;pthread_t thread; -cpu_set_t cpuset;unsigned int gpages;long stolen_time;long run_delay;
@@ -520,11 +518,7 @@ int main(int ac, char **av) verbose = ac >1 && (!strncmp (av[1 ],"-v" ,3 ) || !strncmp (av[1 ],"--verbose" ,10 ));/* Set CPU affinity so we can force preemption of the VCPU */ -CPU_ZERO (&cpuset); -CPU_SET (0 , &cpuset); -pthread_attr_init (&attr); -pthread_attr_setaffinity_np (&attr,sizeof (cpu_set_t), &cpuset); -pthread_setaffinity_np (pthread_self (),sizeof (cpu_set_t), &cpuset); +pin_self_to_cpu (0 );/* Create a VM and an identity mapped memslot for the steal time structure */ vm =vm_create_with_vcpus (NR_VCPUS , guest_code, vcpus);
@@ -558,7 +552,9 @@ int main(int ac, char **av)/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */ run_delay =get_run_delay (); -pthread_create (&thread, &attr, do_steal_time,NULL ); +pthread_create (&thread,NULL , do_steal_time,NULL ); +pin_task_to_cpu (thread,0 ); +do sched_yield ();while (get_run_delay () - run_delay <MIN_RUN_DELAY_NS );
Signature · 1 line
2.51.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Race condition causing intermittent test failure (ESRCH) due to late thread pinning --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Race condition causing intermittent test failure (ESRCH) due to late thread pinning --
commit 27b6bbfae99d5564cda41cbc7b8c36c7b9a4ed64
Author: Sean Christopherson <[email protected]>
KVM: selftests: Use KVM's task pinning APIs in steal_time
This commit updates the steal_time test to use KVM's task pinning APIs (pin_self_to_cpu and pin_task_to_cpu) to pin the vCPU and stealer threads to pCPU0. This removes the use of pthread_attr_setaffinity_np, allowing the test to build against non-glibc C libraries.
Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +0
-0
[ ... ]
tools/testing/selftests/kvm/steal_time.c int main(int ac, char **av) · line 556
Quoted 7 lines
tools/testing/selftests/kvm/steal_time.c +2
-1@@ -558,7 +552,9 @@ int main(int ac, char **av)/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */ run_delay =get_run_delay (); -pthread_create (&thread, &attr, do_steal_time,NULL ); +pthread_create (&thread,NULL , do_steal_time,NULL ); +pin_task_to_cpu (thread,0 );
[Severity: Medium]
Could this introduce a race condition that causes intermittent test failures?
Since pthread_create is called before pinning, there is a small window where the child thread (do_steal_time) could execute and complete its very brief 200us run before the parent thread executes pin_task_to_cpu.
If the child thread exits before pin_task_to_cpu attempts to set its affinity, it will return ESRCH and trigger the TEST_ASSERT inside pin_task_to_cpu, abruptly aborting the test.
Is the explicit pin_task_to_cpu call necessary here? Since the main thread already called pin_self_to_cpu(0) earlier, the new thread created by pthread_create should automatically inherit the parent's CPU affinity mask.
Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +1
-0+do sched_yield ();while (get_run_delay () - run_delay <MIN_RUN_DELAY_NS );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
reviewerQuoted 36 lines · On Mon, Jul 06, 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Race condition causing intermittent test failure (ESRCH) due to late thread pinning --commit
27b6bbfae99d5564cda41cbc7b8c36c7b9a4ed64
Author: Sean Christopherson <[email protected]>KVM: selftests: Use KVM's task pinning APIs in
steal_timeThis commit updates the
steal_timetest to use KVM's task pinning APIs (pin_self_to_cpuandpin_task_to_cpu) to pin the vCPU and stealer threads to pCPU0. This removes the use ofpthread_attr_setaffinity_np, allowing the test to build against non-glibc C libraries.Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +0
-0[ ... ]
tools/testing/selftests/kvm/steal_time.c int main(int ac, char **av) · line 556
Quoted 7 lines
tools/testing/selftests/kvm/steal_time.c +2
-1@@ -558,7 +552,9 @@ int main(int ac, char **av)/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */ run_delay =get_run_delay (); -pthread_create (&thread, &attr, do_steal_time,NULL ); +pthread_create (&thread,NULL , do_steal_time,NULL ); +pin_task_to_cpu (thread,0 );[Severity: Medium]
Could this introduce a race condition that causes intermittent test failures?Since
pthread_createis called before pinning, there is a small window where the child thread (do_steal_time) could execute and complete its very brief 200us run before the parent thread executespin_task_to_cpu.
Quoted 5 lines
If the child thread exits before
pin_task_to_cpuattempts to set its affinity, it will return ESRCH and trigger theTEST_ASSERTinsidepin_task_to_cpu, abruptly aborting the test.Is the explicit
pin_task_to_cpucall necessary here?
Nope, which means we can simply drop the problematic pthread_attr_setaffinity_np(), though I think it makes sense to add a sanity check that the worker did indeed inherit the affinity, if only to document what's happening.
I'll send a v2.
Thanks for posting this Hisam!
Quoted 3 lines
Since the main thread already called
pin_self_to_cpu(0)earlier, the new thread created bypthread_createshould automatically inherit the parent's CPU affinity mask.