KVM: selftests: Add more syscall wrappers, fix hardware_disable_test
Sean Christopherson
maintainercover KVM: selftests: Add more syscall wrappers, fix hardware_disable_test
Buried in the middle of this is a fix for the hardware_disable_test where it can end up with a runtime of multiple minutes on preemptible kernels due to blindly pinning 68 spinning tasks to pCPUs 0-4, which is "mildly" problematic if the parent is also affined to those CPUs (or the scheduler keeps trying to run the parent on those CPUs for whatever reason).
After wasting almost half a day bisecting the kernel to the auto-enablement of PREEMPT_LAZY, I got fed up with the test and went scorched earth on it, to try and clean up its many warts.
The vast, vast majority of the changes are adding kvm_xxx() syscall wrappers so that tests don't need to manually assert that a syscall succeeds (the hardware disable test had some ugly equivalents).
Note, this somewhat unfortunately applies on top of the bad-in-hindsight "irq_test" branch in kvm-x86, as I didn't want to re-invent the "pick a random CPU from a cpuset" functionality. Luckily, AFAICT none of the nearly tree-wide changes conflict with anything else that's in-flight in other KVM arch trees, so I'm planning on smashing the "I'm Feeling Lucky" button on this and throwing it on top of the irq_test (though I'll rename the branch so it's not completely ridiculous).
Tested on x86 and arm64, compile tested on other architectures.
Sean Christopherson (9):
KVM: selftests: Return the target CPU from pin_task_to_random_cpu()
KVM: selftests: Extract picking of random CPU from cpu_set_t to
separate API
KVM: selftests: Affine child tasks to other pCPUs in hardware disable
test
KVM: selftests: Drop unreachable, dead code from hardware disable test
KVM: selftests: Add KVM syscall wrapper for pthread_create()
KVM: selftests: Add KVM syscall wrappers for pthread_{cancel,join}()
KVM: selftests: Add helper APIs to cancel+join pthreads
KVM: selftests: Add KVM syscall wrappers for
pthread_{g,s}etaffinity_np()
KVM: selftests: Clean up global constants in hardware disable test
tools/testing/selftests/kvm/arch_timer.c | 18 ++-- tools/testing/selftests/kvm/arm64/vgic_irq.c | 14 +-- .../selftests/kvm/arm64/vgic_lpi_stress.c | 8 +- tools/testing/selftests/kvm/dirty_log_test.c | 4 +- .../selftests/kvm/hardware_disable_test.c | 101 ++++++++---------- .../selftests/kvm/include/kvm_syscalls.h | 47 ++++++++ .../testing/selftests/kvm/include/kvm_util.h | 11 +- tools/testing/selftests/kvm/irq_test.c | 4 +- tools/testing/selftests/kvm/irqfd_test.c | 4 +- .../selftests/kvm/kvm_page_table_test.c | 6 +- tools/testing/selftests/kvm/lib/kvm_util.c | 10 +- tools/testing/selftests/kvm/lib/memstress.c | 4 +- .../selftests/kvm/lib/userfaultfd_util.c | 7 +- .../testing/selftests/kvm/memslot_perf_test.c | 4 +- tools/testing/selftests/kvm/mmu_stress_test.c | 4 +- .../selftests/kvm/pre_fault_memory_test.c | 4 +- tools/testing/selftests/kvm/rseq_test.c | 6 +- tools/testing/selftests/kvm/s390/memop.c | 4 +- .../selftests/kvm/set_memory_region_test.c | 6 +- tools/testing/selftests/kvm/steal_time.c | 6 +- tools/testing/selftests/kvm/x86/hyperv_ipi.c | 32 ++---- .../selftests/kvm/x86/hyperv_tlb_flush.c | 30 +----- .../kvm/x86/private_mem_conversions_test.c | 4 +- .../kvm/x86/private_mem_kvm_exits_test.c | 7 +- .../selftests/kvm/x86/recalc_apic_map_test.c | 5 +- .../selftests/kvm/x86/sev_migrate_tests.c | 4 +- .../selftests/kvm/x86/sync_regs_test.c | 5 +- .../selftests/kvm/x86/tsc_scaling_sync.c | 4 +- .../selftests/kvm/x86/xapic_ipi_test.c | 31 +----- .../selftests/kvm/x86/xen_shinfo_test.c | 10 +- 30 files changed, 179 insertions(+), 225 deletions(-)
base-commit: 3c7d7f908d574277a845423ec32250a8d8df44c8
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH 1/9 KVM: selftests: Return the target CPU from pin_task_to_random_cpu()
When pinning a task to a random CPU, return which CPU the task was pinned to so that the caller can do things like avoid running other tasks on the target CPU.
--- tools/testing/selftests/kvm/include/kvm_util.h | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +1 -1
@@ -1094,7 +1094,7 @@ static inline void pin_task_to_cpu(pthread_t task, int cpu)TEST_ASSERT (!r,"Failed to set thread affinity to pCPU '%u'" , cpu); } -void pin_task_to_random_cpu (pthread_t task,cpu_set_t *possible_cpus); +int pin_task_to_random_cpu (pthread_t task,cpu_set_t *possible_cpus);static inline int pin_task_to_any_cpu (pthread_t task) {
tools/testing/selftests/kvm/lib/kvm_util.c +3 -2
@@ -668,7 +668,7 @@ void kvm_print_vcpu_pinning_help(void) " (default: no pinning)\n", name, name); } -void pin_task_to_random_cpu (pthread_t task,cpu_set_t *possible_cpus) +int pin_task_to_random_cpu (pthread_t task,cpu_set_t *possible_cpus) {int target_idx;int nr_cpus;
@@ -682,11 +682,12 @@ void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus)for (cpu =0 ; cpu <CPU_SETSIZE ; cpu++) {if (CPU_ISSET (cpu, possible_cpus) && target_idx-- ==0 ) {pin_task_to_cpu (task, cpu); -return ; +return cpu; } }TEST_FAIL ("Failed to find random CPU in possible_cpus" ); +return -1 ; } void kvm_parse_vcpu_pinning(const char *pcpus_string, u32 vcpu_to_pcpu[],
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH 2/9 KVM: selftests: Extract picking of random CPU from cpu_set_t to separate API
In reply to Sean ChristophersonExtract 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.
--- tools/testing/selftests/kvm/include/kvm_util.h | 11 ++++++++++- tools/testing/selftests/kvm/lib/kvm_util.c | 7 ++----- 2 files changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +10 -1
@@ -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) {
tools/testing/selftests/kvm/lib/kvm_util.c +2 -5
@@ -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 ; }
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH 3/9 KVM: selftests: Affine child tasks to other pCPUs in hardware disable test
In reply to Sean ChristophersonWhen possible, affine child tasks to a different pCPU than the parent task in the hardware disable test. On preemptible kernels, running 4 + 4*16 child tasks on the same 4 pCPUs as the parent can result in runtimes of several minutes due to the children making it difficult for the parent to make forward progress.
--- .../selftests/kvm/hardware_disable_test.c | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +21 -7
@@ -22,6 +22,7 @@#define DELAY_US_MAX 2000sem_t *sem; +static cpu_set_t child_cpu_set;static void guest_code (void ) {
@@ -84,16 +85,11 @@ static void run_test(u32 run) {struct kvm_vcpu *vcpu;struct kvm_vm *vm; -cpu_set_t cpu_set;pthread_t threads[VCPU_NUM ];pthread_t throw_away;void *b;u32 i, j; -CPU_ZERO (&cpu_set); -for (i =0 ; i <VCPU_NUM ; i++) -CPU_SET (i, &cpu_set); - vm =vm_create (VCPU_NUM );pr_debug ("%s: [%d] start vcpus\n" , __func__, run);
@@ -101,12 +97,12 @@ static void run_test(u32 run) vcpu =vm_vcpu_add (vm, i, guest_code);check_create_thread (&threads[i],NULL , run_vcpu, vcpu); -check_set_affinity (threads[i], &cpu_set); +check_set_affinity (threads[i], &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) {check_create_thread (&throw_away,NULL , sleeping_thread, (void *)NULL ); -check_set_affinity (throw_away, &cpu_set); +check_set_affinity (throw_away, &child_cpu_set); } }pr_debug ("%s: [%d] all threads launched\n" , __func__, run);
@@ -147,12 +143,30 @@ void wait_for_child_setup(pid_t pid) } } +static void setup_child_cpu_set (void ) +{ +int cpu; + +kvm_sched_getaffinity (0 ,sizeof (child_cpu_set), &child_cpu_set); + +if (CPU_COUNT (&child_cpu_set) <2 ) +return ; + + cpu =pin_task_to_random_cpu (pthread_self (), &child_cpu_set); +CPU_CLR (cpu, &child_cpu_set); + +while (CPU_COUNT (&child_cpu_set) >VCPU_NUM ) +CPU_CLR (kvm_pick_random_cpu (&child_cpu_set), &child_cpu_set); +} +int main (int argc,char **argv) {u32 i;int s, r;pid_t pid; +setup_child_cpu_set (); + sem =sem_open ("vm_sem" ,O_CREAT |O_EXCL ,0644 ,0 );sem_unlink ("vm_sem" );
Signature · 1 line
2.55.0.508.g3f0d502094-goog
[email protected]
AI reviewRe: [PATCH 3/9] KVM: selftests: Affine child tasks to other pCPUs in hardware disable test
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Thread initialization race causing severe CPU contention on the parent's single CPU, defeating…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Thread initialization race causing severe CPU contention on the parent's single CPU, defeating the patch's purpose. --
--- Patch [3]: [PATCH 3/9] KVM: selftests: Affine child tasks to other pCPUs in hardware disable test --- commit b00682d387a4f6e1d76a822e983d388f7469a322
Author: Sean Christopherson <[email protected]>
KVM: selftests: Affine child tasks to other pCPUs in hardware disable test
This commit modifies the hardware disable test to affine child tasks to different pCPUs than the parent task. This is intended to prevent the child tasks from starving the parent task on preemptible kernels.
Quoted 4 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -84,16 +85,11 @@ static void run_test(u32 run)
[ ... ]
Quoted 11 lines
tools/testing/selftests/kvm/hardware_disable_test.c +2
-2check_create_thread (&threads[i],NULL , run_vcpu, vcpu); -check_set_affinity (threads[i], &cpu_set); +check_set_affinity (threads[i], &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) {check_create_thread (&throw_away,NULL , sleeping_thread, (void *)NULL ); -check_set_affinity (throw_away, &cpu_set); +check_set_affinity (throw_away, &child_cpu_set); } }
[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -147,12 +143,30 @@ void wait_for_child_setup(pid_t pid)
[ ... ]
Quoted 5 lines
tools/testing/selftests/kvm/hardware_disable_test.c +5
-0+static void setup_child_cpu_set (void ) +{ +int cpu; + +kvm_sched_getaffinity (0 ,sizeof (child_cpu_set), &child_cpu_set);
tools/testing/selftests/kvm/hardware_disable_test.c void wait_for_child_setup(pid_t pid) · line 160
Quoted 10 lines
tools/testing/selftests/kvm/hardware_disable_test.c +10
-0+ +if (CPU_COUNT (&child_cpu_set) <2 ) +return ; + + cpu =pin_task_to_random_cpu (pthread_self (), &child_cpu_set); +CPU_CLR (cpu, &child_cpu_set); + +while (CPU_COUNT (&child_cpu_set) >VCPU_NUM ) +CPU_CLR (kvm_pick_random_cpu (&child_cpu_set), &child_cpu_set); +}
[Severity: Medium]
Since setup_child_cpu_set() pins the parent thread to a single random CPU prior to spawning children, won't the newly created threads in run_test() inherit this single-CPU affinity upon creation?
Because check_create_thread() spawns the threads before their affinity is explicitly updated to child_cpu_set, they might immediately begin executing on the exact same CPU as the parent.
Could this result in the child threads starving the parent on that single CPU before the parent gets a chance to execute check_set_affinity()? This seems like it might heavily delay the migration and trigger the same CPU contention that this patch aims to fix.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Sean Christopherson
maintainerRe: [PATCH 3/9] KVM: selftests: Affine child tasks to other pCPUs in hardware disable test
Quoted 42 lines · On Thu, Jul 30, 2026, [email protected] wrote:
Quoted 4 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -84,16 +85,11 @@ static void run_test(u32 run)[ ... ]
Quoted 11 lines
tools/testing/selftests/kvm/hardware_disable_test.c +2
-2check_create_thread (&threads[i],NULL , run_vcpu, vcpu); -check_set_affinity (threads[i], &cpu_set); +check_set_affinity (threads[i], &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) {check_create_thread (&throw_away,NULL , sleeping_thread, (void *)NULL ); -check_set_affinity (throw_away, &cpu_set); +check_set_affinity (throw_away, &child_cpu_set); } }[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -147,12 +143,30 @@ void wait_for_child_setup(pid_t pid)[ ... ]
Quoted 5 lines
tools/testing/selftests/kvm/hardware_disable_test.c +5
-0+static void setup_child_cpu_set (void ) +{ +int cpu; + +kvm_sched_getaffinity (0 ,sizeof (child_cpu_set), &child_cpu_set);tools/testing/selftests/kvm/hardware_disable_test.c void wait_for_child_setup(pid_t pid) · line 160
Quoted 10 lines
tools/testing/selftests/kvm/hardware_disable_test.c +10
-0+ +if (CPU_COUNT (&child_cpu_set) <2 ) +return ; + + cpu =pin_task_to_random_cpu (pthread_self (), &child_cpu_set); +CPU_CLR (cpu, &child_cpu_set); + +while (CPU_COUNT (&child_cpu_set) >VCPU_NUM ) +CPU_CLR (kvm_pick_random_cpu (&child_cpu_set), &child_cpu_set); +}[Severity: Medium]
Sincesetup_child_cpu_set()pins the parent thread to a single random CPU prior to spawning children, won't the newly created threads inrun_test()inherit this single-CPU affinity upon creation?
Quoted 3 lines
Because
check_create_thread()spawns the threads before their affinity is explicitly updated tochild_cpu_set, they might immediately begin executing on the exact same CPU as the parent.
Yeah, I was trying to avoid this since pthread_attr_setaffinity_np() is only available with __USE_GNU.
Quoted 4 lines
Could this result in the child threads starving the parent on that single CPU before the parent gets a chance to execute
check_set_affinity()? This seems like it might heavily delay the migration and trigger the same CPU contention that this patch aims to fix.
After far too much testing, the problem isn't direct CPU contention, AFAICT it's due to incurring cross-node NUMA penalties, and presumably scheduler contention when the pool of possible CPUs to run on is huge.
I tried a bunch of fancy approaches, e.g. making parent, child, and grandchildren all mutually exclusive, and while the fanciness can provide marginal benefits when pthread_attr_setaffinity_np() isn't available, all that really matters is that the worker threads get affined before they start doing work.
Sean Christopherson
maintainerRe: [PATCH 3/9] KVM: selftests: Affine child tasks to other pCPUs in hardware disable test
Quoted 55 lines · Sean Christopherson
Quoted 42 lines · On Thu, Jul 30, 2026, [email protected] wrote:
Quoted 4 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -84,16 +85,11 @@ static void run_test(u32 run)[ ... ]
Quoted 11 lines
tools/testing/selftests/kvm/hardware_disable_test.c +2
-2check_create_thread (&threads[i],NULL , run_vcpu, vcpu); -check_set_affinity (threads[i], &cpu_set); +check_set_affinity (threads[i], &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) {check_create_thread (&throw_away,NULL , sleeping_thread, (void *)NULL ); -check_set_affinity (throw_away, &cpu_set); +check_set_affinity (throw_away, &child_cpu_set); } }[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -147,12 +143,30 @@ void wait_for_child_setup(pid_t pid)[ ... ]
Quoted 5 lines
tools/testing/selftests/kvm/hardware_disable_test.c +5
-0+static void setup_child_cpu_set (void ) +{ +int cpu; + +kvm_sched_getaffinity (0 ,sizeof (child_cpu_set), &child_cpu_set);tools/testing/selftests/kvm/hardware_disable_test.c void wait_for_child_setup(pid_t pid) · line 160
Quoted 10 lines
tools/testing/selftests/kvm/hardware_disable_test.c +10
-0+ +if (CPU_COUNT (&child_cpu_set) <2 ) +return ; + + cpu =pin_task_to_random_cpu (pthread_self (), &child_cpu_set); +CPU_CLR (cpu, &child_cpu_set); + +while (CPU_COUNT (&child_cpu_set) >VCPU_NUM ) +CPU_CLR (kvm_pick_random_cpu (&child_cpu_set), &child_cpu_set); +}[Severity: Medium]
Sincesetup_child_cpu_set()pins the parent thread to a single random CPU prior to spawning children, won't the newly created threads inrun_test()inherit this single-CPU affinity upon creation?Quoted 3 lines
Because
check_create_thread()spawns the threads before their affinity is explicitly updated tochild_cpu_set, they might immediately begin executing on the exact same CPU as the parent.Yeah, I was trying to avoid this since
pthread_attr_setaffinity_np()is only available with__USE_GNU.Quoted 4 lines
Could this result in the child threads starving the parent on that single CPU before the parent gets a chance to execute
check_set_affinity()? This seems like it might heavily delay the migration and trigger the same CPU contention that this patch aims to fix.
Quoted 8 lines
After far too much testing, the problem isn't direct CPU contention, AFAICT it's due to incurring cross-node NUMA penalties, and presumably scheduler contention when the pool of possible CPUs to run on is huge.
I tried a bunch of fancy approaches, e.g. making parent, child, and grandchildren all mutually exclusive, and while the fanciness can provide marginal benefits when
pthread_attr_setaffinity_np()isn't available, all that really matters is that the worker threads get affined before they start doing work.
Oh, and I also ruled out wakeup latency, i.e. this isn't the same underlying cause that prompted 0297cdc12a87 ("KVM: selftests: Add option to rseq test to override /dev/cpu_dma_latency").
Sean Christopherson
maintainerPATCH 4/9 KVM: selftests: Drop unreachable, dead code from hardware disable test
In reply to Sean ChristophersonDrop all of the code that is unreachable in the hardware disable test, as it adds a lot of noise that makes the test seem far more complicated than it actually is.
--- .../selftests/kvm/hardware_disable_test.c | 42 +++++++------------ 1 file changed, 14 insertions(+), 28 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +14 -28
@@ -15,6 +15,7 @@#include <test_util.h> #include "kvm_util.h" +#include "ucall_common.h" #define VCPU_NUM 4#define SLEEPING_THREAD_NUM (1 << 4)
@@ -28,7 +29,7 @@ static void guest_code(void) {for (;;) ;/* Some busy work */ -printf ("Should not be reached.\n" ); +GUEST_ASSERT (0 ); }static void *run_vcpu (void *arg)
@@ -38,22 +39,19 @@ static void *run_vcpu(void *arg)vcpu_run (vcpu); -TEST_ASSERT (false,"%s: exited with reason %d: %s" , - __func__, run->exit_reason, -exit_reason_str (run->exit_reason)); -pthread_exit (NULL ); +TEST_FAIL ("vCPU%d exited with reason %d: %s" , + vcpu->id, run->exit_reason,exit_reason_str (run->exit_reason)); }static void *sleeping_thread (void *arg) {int fd; -while (true) { +while (1 ) { fd =open ("/dev/null" ,O_RDWR );close (fd); } -TEST_ASSERT (false,"%s: exited" , __func__); -pthread_exit (NULL ); +TEST_FAIL ("%s: exited" , __func__); } static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
@@ -73,21 +71,11 @@ static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set)TEST_ASSERT (r ==0 ,"%s: failed set affinity" , __func__); } -static inline void check_join (pthread_t thread,void **retval) -{ -int r; - - r =pthread_join (thread, retval); -TEST_ASSERT (r ==0 ,"%s: failed to join thread" , __func__); -} -static void run_test (u32 run) {struct kvm_vcpu *vcpu;struct kvm_vm *vm; -pthread_t threads[VCPU_NUM ]; -pthread_t throw_away; -void *b; +pthread_t thread;u32 i, j; vm =vm_create (VCPU_NUM );
@@ -96,21 +84,19 @@ static void run_test(u32 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_set_affinity (threads[i], &child_cpu_set); +check_create_thread (&thread,NULL , run_vcpu, vcpu); +check_set_affinity (thread, &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) { -check_create_thread (&throw_away,NULL , sleeping_thread, - (void *)NULL ); -check_set_affinity (throw_away, &child_cpu_set); +check_create_thread (&thread,NULL , sleeping_thread, (void *)NULL ); +check_set_affinity (thread, &child_cpu_set); } }pr_debug ("%s: [%d] all threads launched\n" , __func__, run);sem_post (sem); -for (i =0 ; i <VCPU_NUM ; ++i) -check_join (threads[i], &b); -/* Should not be reached */ -TEST_ASSERT (false,"%s: [%d] child escaped the ninja" , __func__, run); + +/* Wait for the parent to SIGKILL this child. */ +while (1 ); }void wait_for_child_setup (pid_t pid)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
[email protected]
AI reviewRe: [PATCH 4/9] KVM: selftests: Drop unreachable, dead code from hardware disable test
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Replacing a blocking `pthread_join` with an infinite busy-wait loop (`while (1);`) in the child…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Replacing a blocking pthread_join with an infinite busy-wait loop (while (1);) in the child process unnecessarily burns CPU and causes scheduler contention with the parent process. --
--- Patch [4]: [PATCH 4/9] KVM: selftests: Drop unreachable, dead code from hardware disable test --- commit 80b1aa13e14f4a3f65225bf79adc03a7a8eda318
Author: Sean Christopherson <[email protected]>
KVM: selftests: Drop unreachable, dead code from hardware disable test
This commit removes unreachable code from the hardware disable test to reduce noise and simplify the test logic. It refactors thread creation and replaces a blocking wait with a busy loop in the child process.
Quoted 4 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -96,21 +84,19 @@ static void run_test(u32 run)
[ ... ]
tools/testing/selftests/kvm/hardware_disable_test.c static void run_test(u32 run) · line 99
Quoted 9 lines
tools/testing/selftests/kvm/hardware_disable_test.c +3
-4pr_debug ("%s: [%d] all threads launched\n" , __func__, run);sem_post (sem); -for (i =0 ; i <VCPU_NUM ; ++i) -check_join (threads[i], &b); -/* Should not be reached */ -TEST_ASSERT (false,"%s: [%d] child escaped the ninja" , __func__, run); + +/* Wait for the parent to SIGKILL this child. */ +while (1 );
[Severity: Medium]
Since the child and parent share the same CPU affinity, will this busy spin loop cause unnecessary scheduler contention with the parent process when the parent's timer fires?
Because the processes are pinned to a single CPU core, spinning in this loop burns CPU cycles and forces the parent to contend with the spinning child for CPU time.
Would it be better to use a blocking wait like pause() or sleep() here to reduce scheduler latency and avoid 100% CPU utilization?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Sean Christopherson
maintainerRe: [PATCH 4/9] KVM: selftests: Drop unreachable, dead code from hardware disable test
Quoted 10 lines · On Thu, Jul 30, 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Quoted 4 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/hardware_disable_test.c +0
-0@@ -96,21 +84,19 @@ static void run_test(u32 run)[ ... ]
Quoted 14 lines
tools/testing/selftests/kvm/hardware_disable_test.c static void run_test(u32 run) · line 99
Quoted 9 lines
tools/testing/selftests/kvm/hardware_disable_test.c +3
-4pr_debug ("%s: [%d] all threads launched\n" , __func__, run);sem_post (sem); -for (i =0 ; i <VCPU_NUM ; ++i) -check_join (threads[i], &b); -/* Should not be reached */ -TEST_ASSERT (false,"%s: [%d] child escaped the ninja" , __func__, run); + +/* Wait for the parent to SIGKILL this child. */ +while (1 );[Severity: Medium]
Since the child and parent share the same CPU affinity, will this busy spin loop cause unnecessary scheduler contention with the parent process when the parent's timer fires?
Doesn't seem like it? Or rather, any contention between the parent and the immediate child doesn't seem to matter.
Quoted 3 lines
Because the processes are pinned to a single CPU core, spinning in this loop burns CPU cycles and forces the parent to contend with the spinning child for CPU time.
Yep.
Quoted 2 lines
Would it be better to use a blocking wait like
pause()orsleep()here to reduce scheduler latency and avoid 100% CPU utilization?
Yeah, it makes sense to throw pause() in here, if only to maintain equivalent functionality. Because that's effectively what pthread_join() will do since the grandchildren never exit.
Sean Christopherson
maintainerPATCH 5/9 KVM: selftests: Add KVM syscall wrapper for pthread_create()
In reply to Sean ChristophersonAdd and use a KVM wrapper for pthread_create() syscall so that selftests don't need to manually assert that the syscall succeeded.
Note, most tests don't actually assert success, but they all obviously rely on the syscall to succeed.
Other than explicitly failing if pthread_create() fails, no functional change intended.
--- tools/testing/selftests/kvm/arch_timer.c | 14 ++++---------- tools/testing/selftests/kvm/arm64/vgic_irq.c | 10 +++------- .../testing/selftests/kvm/arm64/vgic_lpi_stress.c | 4 ++-- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- .../testing/selftests/kvm/hardware_disable_test.c | 15 +++------------ .../testing/selftests/kvm/include/kvm_syscalls.h | 5 +++++ tools/testing/selftests/kvm/irq_test.c | 2 +- tools/testing/selftests/kvm/irqfd_test.c | 2 +- tools/testing/selftests/kvm/kvm_page_table_test.c | 4 ++-- tools/testing/selftests/kvm/lib/memstress.c | 2 +- .../testing/selftests/kvm/lib/userfaultfd_util.c | 4 ++-- tools/testing/selftests/kvm/memslot_perf_test.c | 2 +- tools/testing/selftests/kvm/mmu_stress_test.c | 2 +- .../testing/selftests/kvm/pre_fault_memory_test.c | 2 +- tools/testing/selftests/kvm/rseq_test.c | 4 ++-- tools/testing/selftests/kvm/s390/memop.c | 2 +- .../selftests/kvm/set_memory_region_test.c | 2 +- tools/testing/selftests/kvm/steal_time.c | 2 +- tools/testing/selftests/kvm/x86/hyperv_ipi.c | 11 ++++------- .../testing/selftests/kvm/x86/hyperv_tlb_flush.c | 9 +++------ .../kvm/x86/private_mem_conversions_test.c | 2 +- .../kvm/x86/private_mem_kvm_exits_test.c | 5 ++--- .../selftests/kvm/x86/recalc_apic_map_test.c | 2 +- .../testing/selftests/kvm/x86/sev_migrate_tests.c | 2 +- tools/testing/selftests/kvm/x86/sync_regs_test.c | 2 +- .../testing/selftests/kvm/x86/tsc_scaling_sync.c | 2 +- tools/testing/selftests/kvm/x86/xapic_ipi_test.c | 8 ++------ tools/testing/selftests/kvm/x86/xen_shinfo_test.c | 3 +-- 28 files changed, 50 insertions(+), 76 deletions(-)
tools/testing/selftests/kvm/arch_timer.c +4 -10
@@ -141,28 +141,22 @@ static void test_run(struct kvm_vm *vm) {pthread_t pt_vcpu_migration;unsigned int i; -int ret;pthread_mutex_init (&vcpu_done_map_lock,NULL ); vcpu_done_map =bitmap_zalloc (test_args.nr_vcpus);TEST_ASSERT (vcpu_done_map,"Failed to allocate vcpu done bitmap" ); -for (i =0 ; i < (unsigned long )test_args.nr_vcpus; i++) { - ret =pthread_create (&pt_vcpu_run[i],NULL , test_vcpu_run, - (void *)(unsigned long )i); -TEST_ASSERT (!ret,"Failed to create vCPU-%d pthread" , i); - } +for (i =0 ; i < (unsigned long )test_args.nr_vcpus; i++) +kvm_pthread_create (&pt_vcpu_run[i],NULL , test_vcpu_run, + (void *)(unsigned long )i);/* Spawn a thread to control the vCPU migrations */ if (test_args.migration_freq_ms) {srand (time (NULL )); - ret =pthread_create (&pt_vcpu_migration,NULL , - test_vcpu_migration,NULL ); -TEST_ASSERT (!ret,"Failed to create the migration pthread" ); +kvm_pthread_create (&pt_vcpu_migration,NULL , test_vcpu_migration,NULL ); } -for (i =0 ; i < test_args.nr_vcpus; i++)pthread_join (pt_vcpu_run[i],NULL );
tools/testing/selftests/kvm/arm64/vgic_irq.c +3 -7
@@ -988,7 +988,7 @@ static void test_vgic_two_cpus(void *gcode)struct test_args args = {};struct kvm_vm *vm;gva_t args_gva; -int gic_fd, ret; +int gic_fd; vm =vm_create_with_vcpus (2 , gcode, vcpus);
@@ -1004,12 +1004,8 @@ static void test_vgic_two_cpus(void *gcode) gic_fd =vgic_v3_setup (vm,2 ,64 ); - ret =pthread_create (&thr[0 ],NULL , test_vcpu_run, vcpus[0 ]); -if (ret) -TEST_FAIL ("Can't create thread for vcpu 0 (%d)\n" , ret); - ret =pthread_create (&thr[1 ],NULL , test_vcpu_run, vcpus[1 ]); -if (ret) -TEST_FAIL ("Can't create thread for vcpu 1 (%d)\n" , ret); +kvm_pthread_create (&thr[0 ],NULL , test_vcpu_run, vcpus[0 ]); +kvm_pthread_create (&thr[1 ],NULL , test_vcpu_run, vcpus[1 ]);pthread_join (thr[0 ],NULL );pthread_join (thr[1 ],NULL );
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +2 -2
@@ -311,10 +311,10 @@ static void run_test(void)pthread_barrier_init (&test_setup_barrier,NULL , nr_vcpus + nr_devices +1 );for (i =0 ; i < nr_vcpus; i++) -pthread_create (&vcpu_threads[i],NULL , vcpu_worker_thread, vcpus[i]); +kvm_pthread_create (&vcpu_threads[i],NULL , vcpu_worker_thread, vcpus[i]);for (i =0 ; i < nr_devices; i++) -pthread_create (&lpi_threads[i],NULL , lpi_worker_thread, (void *)i); +kvm_pthread_create (&lpi_threads[i],NULL , lpi_worker_thread, (void *)i);pthread_barrier_wait (&test_setup_barrier);
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -691,7 +691,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)TEST_ASSERT_EQ (vcpu_stop, false); -pthread_create (&vcpu_thread,NULL , vcpu_worker, vcpu); +kvm_pthread_create (&vcpu_thread,NULL , vcpu_worker, vcpu);for (iteration =1 ; iteration <= p->iterations; iteration++) {unsigned long i, reap_i;
tools/testing/selftests/kvm/hardware_disable_test.c +3 -12
@@ -5,7 +5,6 @@* return notifiers. */ #include <fcntl.h> -#include <pthread.h> #include <semaphore.h> #include <stdint.h> #include <stdlib.h>
@@ -14,6 +13,7 @@#include <test_util.h> +#include "kvm_syscalls.h" #include "kvm_util.h" #include "ucall_common.h"
@@ -54,15 +54,6 @@ static void *sleeping_thread(void *arg)TEST_FAIL ("%s: exited" , __func__); } -static inline void check_create_thread (pthread_t *thread,pthread_attr_t *attr, -void *(*f)(void *),void *arg) -{ -int r; - - r =pthread_create (thread, attr, f, arg); -TEST_ASSERT (r ==0 ,"%s: failed to create thread" , __func__); -} -static inline void check_set_affinity (pthread_t thread,cpu_set_t *cpu_set) {int r;
@@ -84,11 +75,11 @@ static void run_test(u32 run)for (i =0 ; i <VCPU_NUM ; ++i) { vcpu =vm_vcpu_add (vm, i, guest_code); -check_create_thread (&thread,NULL , run_vcpu, vcpu); +kvm_pthread_create (&thread,NULL , run_vcpu, vcpu);check_set_affinity (thread, &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) { -check_create_thread (&thread,NULL , sleeping_thread, (void *)NULL ); +kvm_pthread_create (&thread,NULL , sleeping_thread, (void *)NULL );check_set_affinity (thread, &child_cpu_set); } }
tools/testing/selftests/kvm/include/kvm_syscalls.h +5 -0
@@ -12,6 +12,7 @@#include <sys/mman.h> #include <sys/syscall.h> +#include <pthread.h> #include <sched.h> #include <test_util.h>
@@ -96,6 +97,10 @@ __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice); __KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask); +typedef void *(*pthread_fn_t )(void *); +__KVM_SYSCALL_DEFINE(pthread_create, 4, pthread_t *, thread, + const pthread_attr_t *, attr, pthread_fn_t, fn, void *, arg); +#define kvm_free_fd (fd) \ do { \ kvm_close(fd); \
tools/testing/selftests/kvm/irq_test.c +1 -1
@@ -296,7 +296,7 @@ int main(int argc, char **argv)kvm_sched_getaffinity (0 ,sizeof (available_cpus), &available_cpus);for (i =0 ; i < nr_vcpus; i++) -pthread_create (&vcpu_threads[i],NULL , vcpu_thread_main, vcpus[i]); +kvm_pthread_create (&vcpu_threads[i],NULL , vcpu_thread_main, vcpus[i]);for (i =0 ; i < nr_vcpus; i++) {struct kvm_vcpu *vcpu = vcpus[i];
tools/testing/selftests/kvm/irqfd_test.c +1 -1
@@ -128,7 +128,7 @@ int main(int argc, char *argv[])close (__eventfd); -pthread_create (&racing_thread,NULL , secondary_irqfd_juggler, vm2); +kvm_pthread_create (&racing_thread,NULL , secondary_irqfd_juggler, vm2);for (i =0 ; i <10000 ; i++) {WRITE_ONCE (__eventfd,kvm_new_eventfd ());
tools/testing/selftests/kvm/kvm_page_table_test.c +2 -2
@@ -364,8 +364,8 @@ static void run_test(enum vm_guest_mode mode, void *arg) *current_stage =KVM_BEFORE_MAPPINGS ;for (i =0 ; i < nr_vcpus; i++) -pthread_create (&vcpu_threads[i],NULL , vcpu_worker, - test_args.vcpus[i]); +kvm_pthread_create (&vcpu_threads[i],NULL , vcpu_worker, + test_args.vcpus[i]);vcpus_complete_new_stage (*current_stage);pr_info ("Started all vCPUs successfully\n" );
tools/testing/selftests/kvm/lib/memstress.c +1 -1
@@ -294,7 +294,7 @@ void memstress_start_vcpu_threads(int nr_vcpus, vcpu->vcpu_idx = i;WRITE_ONCE (vcpu->running, false); -pthread_create (&vcpu->thread,NULL , vcpu_thread_main, vcpu); +kvm_pthread_create (&vcpu->thread,NULL , vcpu_thread_main, vcpu); }for (i =0 ; i < nr_vcpus; i++) {
tools/testing/selftests/kvm/lib/userfaultfd_util.c +2 -2
@@ -167,8 +167,8 @@ struct uffd_desc *uffd_setup_demand_paging(int uffd_mode, useconds_t delay, uffd_desc->reader_args[i].handler = handler; uffd_desc->reader_args[i].pipe = pipes[0 ]; -pthread_create (&uffd_desc->readers[i],NULL , uffd_handler_thread_fn, - &uffd_desc->reader_args[i]); +kvm_pthread_create (&uffd_desc->readers[i],NULL , uffd_handler_thread_fn, + &uffd_desc->reader_args[i]);PER_VCPU_DEBUG ("Created uffd thread %i for HVA range [%p, %p)\n" , i, hva, hva + len);
tools/testing/selftests/kvm/memslot_perf_test.c +1 -1
@@ -366,7 +366,7 @@ static void launch_vm(struct vm_data *data) {pr_info_v ("Launching the test VM\n" ); -pthread_create (&data->vcpu_thread,NULL , vcpu_worker, data); +kvm_pthread_create (&data->vcpu_thread,NULL , vcpu_worker, data);/* Ensure the guest thread is spun up. */ wait_for_vcpu ();
tools/testing/selftests/kvm/mmu_stress_test.c +1 -1
@@ -222,7 +222,7 @@ static pthread_t *spawn_workers(struct kvm_vm *vm, struct kvm_vcpu **vcpus, info[i].vcpu = vcpus[i]; info[i].start_gpa = gpa; info[i].end_gpa = gpa + nr_bytes; -pthread_create (&threads[i],NULL , vcpu_worker, &info[i]); +kvm_pthread_create (&threads[i],NULL , vcpu_worker, &info[i]); }return threads; }
tools/testing/selftests/kvm/pre_fault_memory_test.c +1 -1
@@ -84,7 +84,7 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 base_gpa, u64 offset,* Concurrently delete (and recreate) the slot to test KVM's handling * of a racing memslot deletion with prefaulting. */ -pthread_create (&slot_worker,NULL , delete_slot_worker, &data); +kvm_pthread_create (&slot_worker,NULL , delete_slot_worker, &data);while (!READ_ONCE (data.worker_ready))cpu_relax ();
tools/testing/selftests/kvm/rseq_test.c +2 -2
@@ -241,8 +241,8 @@ int main(int argc, char *argv[])*/ vm =vm_create_with_one_vcpu (&vcpu, guest_code); -pthread_create (&migration_thread,NULL , migration_worker, - (void *)(unsigned long )kvm_gettid ()); +kvm_pthread_create (&migration_thread,NULL , migration_worker, + (void *)(unsigned long )kvm_gettid ());if (latency >=0 ) {/*
tools/testing/selftests/kvm/s390/memop.c +1 -1
@@ -678,7 +678,7 @@ static void test_cmpxchg_key_concurrent(void)HOST_SYNC (t.vcpu,STAGE_SKEYS_SET );prepare_mem12 ();MOP (t.vcpu,LOGICAL ,WRITE , mem1, max_block,GADDR_V (mem2)); -pthread_create (&thread,NULL , run_guest, &t.vcpu); +kvm_pthread_create (&thread,NULL , run_guest, &t.vcpu);for (int i =0 ; i < cmpxchg_iter_outer; i++) { do {
tools/testing/selftests/kvm/set_memory_region_test.c +1 -1
@@ -133,7 +133,7 @@ static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread, hva =addr_gpa2hva (vm,MEM_REGION_GPA );memset (hva,0 ,2 *4096 ); -pthread_create (vcpu_thread,NULL , vcpu_worker, *vcpu); +kvm_pthread_create (vcpu_thread,NULL , vcpu_worker, *vcpu);/* Ensure the guest thread is spun up. */ wait_for_vcpu ();
tools/testing/selftests/kvm/steal_time.c +1 -1
@@ -548,7 +548,7 @@ 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,NULL , do_steal_time,NULL ); +kvm_pthread_create (&thread,NULL , do_steal_time,NULL );pthread_getaffinity_np (thread,sizeof (cpuset), &cpuset);TEST_ASSERT (CPU_COUNT (&cpuset) ==1 &&CPU_ISSET (cpu, &cpuset),"Worker failed to inherit parent's CPU affinity" );
tools/testing/selftests/kvm/x86/hyperv_ipi.c +4 -7
@@ -245,7 +245,7 @@ int main(int argc, char *argv[])struct kvm_vcpu *vcpu[3 ];gva_t hcall_page;pthread_t threads[2 ]; -int stage =1 , r; +int stage =1 ;struct ucall uc;TEST_REQUIRE (kvm_has_cap (KVM_CAP_HYPERV_SEND_IPI ));
@@ -272,11 +272,8 @@ int main(int argc, char *argv[])vcpu_args_set (vcpu[0 ],2 , hcall_page,addr_gva2gpa (vm, hcall_page));vcpu_set_hv_cpuid (vcpu[0 ]); - r =pthread_create (&threads[0 ],NULL , vcpu_thread, vcpu[1 ]); -TEST_ASSERT (!r,"pthread_create failed errno=%d" , r); - - r =pthread_create (&threads[1 ],NULL , vcpu_thread, vcpu[2 ]); -TEST_ASSERT (!r,"pthread_create failed errno=%d" , errno); +kvm_pthread_create (&threads[0 ],NULL , vcpu_thread, vcpu[1 ]); +kvm_pthread_create (&threads[1 ],NULL , vcpu_thread, vcpu[2 ]);while (true) {vcpu_run (vcpu[0 ]);
@@ -306,5 +303,5 @@ int main(int argc, char *argv[])cancel_join_vcpu_thread (threads[1 ], vcpu[2 ]);kvm_vm_free (vm); -return r; +return 0 ; }
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +3 -6
@@ -575,7 +575,7 @@ int main(int argc, char *argv[])u64 *pte;struct test_data *data;struct ucall uc; -int stage =1 , r, i; +int stage =1 , i;TEST_REQUIRE (kvm_has_cap (KVM_CAP_HYPERV_TLBFLUSH ));
@@ -632,11 +632,8 @@ int main(int argc, char *argv[])vcpu_set_msr (vcpu[2 ],HV_X64_MSR_VP_INDEX ,WORKER_VCPU_ID_2 );vcpu_set_hv_cpuid (vcpu[2 ]); - r =pthread_create (&threads[0 ],NULL , vcpu_thread, vcpu[1 ]); -TEST_ASSERT (!r,"pthread_create() failed" ); - - r =pthread_create (&threads[1 ],NULL , vcpu_thread, vcpu[2 ]); -TEST_ASSERT (!r,"pthread_create() failed" ); +kvm_pthread_create (&threads[0 ],NULL , vcpu_thread, vcpu[1 ]); +kvm_pthread_create (&threads[1 ],NULL , vcpu_thread, vcpu[2 ]);while (true) {vcpu_run (vcpu[0 ]);
tools/testing/selftests/kvm/x86/private_mem_conversions_test.c +1 -1
@@ -412,7 +412,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, u32 nr_v*/ virt_map (vm, gpa, gpa,PER_CPU_DATA_SIZE / vm->page_size); -pthread_create (&threads[i],NULL , __test_mem_conversions, vcpus[i]); +kvm_pthread_create (&threads[i],NULL , __test_mem_conversions, vcpus[i]); }WRITE_ONCE (run_vcpus, true);
tools/testing/selftests/kvm/x86/private_mem_kvm_exits_test.c +2 -3
@@ -65,9 +65,8 @@ static void test_private_access_memslot_deleted(void)/* Request to access page privately */ vm_mem_set_private (vm,EXITS_TEST_GPA ,EXITS_TEST_SIZE ); -pthread_create (&vm_thread,NULL , - (void *(*)(void *))run_vcpu_get_exit_reason, - (void *)vcpu); +kvm_pthread_create (&vm_thread,NULL , + (pthread_fn_t )run_vcpu_get_exit_reason, (void *)vcpu);vm_mem_region_delete (vm,EXITS_TEST_SLOT );
tools/testing/selftests/kvm/x86/recalc_apic_map_test.c +1 -1
@@ -57,7 +57,7 @@ int main(void)for (i =0 ; i <KVM_MAX_VCPUS ; i++)vcpu_set_msr (vcpus[i],MSR_IA32_APICBASE ,LAPIC_X2APIC ); -TEST_ASSERT_EQ (pthread_create (&thread,NULL , race, vcpus[0 ]),0 ); +kvm_pthread_create (&thread,NULL , race, vcpus[0 ]); vcpuN = vcpus[KVM_MAX_VCPUS -1 ];for (t =time (NULL ) +TIMEOUT ;time (NULL ) < t;) {
tools/testing/selftests/kvm/x86/sev_migrate_tests.c +1 -1
@@ -128,7 +128,7 @@ static void test_sev_migrate_locking(void) sizeof(input[i].source_vms));for (i =0 ; i <NR_LOCK_TESTING_THREADS ; ++i) -pthread_create (&pt[i],NULL , locking_test_thread, &input[i]); +kvm_pthread_create (&pt[i],NULL , locking_test_thread, &input[i]);for (i =0 ; i <NR_LOCK_TESTING_THREADS ; ++i)pthread_join (pt[i],NULL );
tools/testing/selftests/kvm/x86/sync_regs_test.c +1 -1
@@ -181,7 +181,7 @@ static void race_sync_regs(struct kvm_vcpu *vcpu, void *racer) !!(run->s.regs.sregs.cr4 &X86_CR4_PAE ), !!(run->s.regs.sregs.efer & EFER_LME)); -TEST_ASSERT_EQ (pthread_create (&thread,NULL , racer, (void *)run),0 ); +kvm_pthread_create (&thread,NULL , racer, (void *)run);for (t =time (NULL ) +TIMEOUT ;time (NULL ) < t;) {/*
tools/testing/selftests/kvm/x86/tsc_scaling_sync.c +1 -1
@@ -94,7 +94,7 @@ int main(int argc, char *argv[])pthread_t cpu_threads[NR_TEST_VCPUS ];unsigned long cpu;for (cpu =0 ; cpu <NR_TEST_VCPUS ; cpu++) -pthread_create (&cpu_threads[cpu],NULL , run_vcpu, (void *)cpu); +kvm_pthread_create (&cpu_threads[cpu],NULL , run_vcpu, (void *)cpu);unsigned long failures =0 ;for (cpu =0 ; cpu <NR_TEST_VCPUS ; cpu++) {
tools/testing/selftests/kvm/x86/xapic_ipi_test.c +2 -6
@@ -387,7 +387,6 @@ void get_cmdline_args(int argc, char *argv[], int *run_secs,int main (int argc,char *argv[]) { -int r;int wait_secs;const int max_halter_wait =10 ;int run_secs =0 ;
@@ -428,9 +427,7 @@ int main(int argc, char *argv[]) params[1 ].pipis_rcvd = pipis_rcvd;/* Start halter vCPU thread and wait for it to execute first HLT. */ - r =pthread_create (&threads[0 ],NULL , vcpu_thread, ¶ms[0 ]); -TEST_ASSERT (r ==0 , -"pthread_create halter failed errno=%d" , errno); +kvm_pthread_create (&threads[0 ],NULL , vcpu_thread, ¶ms[0 ]);fprintf (stderr,"Halter vCPU thread started\n" ); wait_secs =0 ;
@@ -447,8 +444,7 @@ int main(int argc, char *argv[])"Halter vCPU thread reported its APIC ID: %u after %d seconds.\n" , data->halter_apic_id, wait_secs); - r =pthread_create (&threads[1 ],NULL , vcpu_thread, ¶ms[1 ]); -TEST_ASSERT (r ==0 ,"pthread_create sender failed errno=%d" , errno); +kvm_pthread_create (&threads[1 ],NULL , vcpu_thread, ¶ms[1 ]); fprintf(stderr, "IPI sender vCPU thread started. Letting vCPUs run for %d seconds.\n",
tools/testing/selftests/kvm/x86/xen_shinfo_test.c +1 -2
@@ -894,8 +894,7 @@ int main(int argc, char *argv[])if (verbose)printf ("Testing shinfo lock corruption (KVM_XEN_HVM_EVTCHN_SEND)\n" ); - ret =pthread_create (&thread,NULL , &juggle_shinfo_state, (void *)vm); -TEST_ASSERT (ret ==0 ,"pthread_create() failed: %s" ,strerror (ret)); +kvm_pthread_create (&thread,NULL , &juggle_shinfo_state, (void *)vm);struct kvm_irq_routing_xen_evtchn uxe = { .port =1 ,
Signature · 1 line
2.55.0.508.g3f0d502094-goog
[email protected]
AI reviewRe: [PATCH 5/9] KVM: selftests: Add KVM syscall wrapper for pthread_create()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Misleading error reporting when pthread_create fails due to reliance on errno. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Misleading error reporting when pthread_create fails due to reliance on errno. --
--- Patch [5]: [PATCH 5/9] KVM: selftests: Add KVM syscall wrapper for pthread_create() --- commit c5c266b1e7b797f8b82c5a3cecf097ba6720b060
Author: Sean Christopherson <[email protected]>
KVM: selftests: Add KVM syscall wrapper for pthread_create()
This commit adds a KVM wrapper for pthread_create() to eliminate manual success assertions in selftests. It replaces direct calls to pthread_create() with kvm_pthread_create() across multiple test files.
Quoted 3 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +0
-0
[ ... ]
tools/testing/selftests/kvm/include/kvm_syscalls.h __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); · line 102
Quoted 7 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +3
-0@@ -96,6 +97,10 @@ __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice); __KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask); +typedef void *(*pthread_fn_t )(void *); +__KVM_SYSCALL_DEFINE(pthread_create, 4, pthread_t *, thread, + const pthread_attr_t *, attr, pthread_fn_t, fn, void *, arg);
[Severity: Low]
Does this lead to misleading error messages on failure?
The __KVM_SYSCALL_DEFINE macro seems to assume the wrapped function sets errno on failure, which gets printed when the test asserts via __KVM_SYSCALL_ERROR. However, pthread_create() returns its error code directly rather than setting errno.
If thread creation fails, it looks like the test framework might print a misleading message containing "errno: 0 (Success)" alongside the actual non-zero return code. Should this use a different wrapper or assert strategy that handles direct error returns?
Quoted 4 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +1
-0+#define kvm_free_fd (fd) \ do { \ kvm_close(fd); \
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Sean Christopherson
maintainerPATCH 6/9 KVM: selftests: Add KVM syscall wrappers for pthread_{cancel,join}()
In reply to Sean ChristophersonAdd and use KVM wrappers for pthread_{cancel,join}() so that selftests don't need to manually assert that the syscalls succeeded.
Note, the vast majority tests don't actually assert success, but they all obviously rely on the syscall to succeed.
Other than explicitly failing if a syscall fails, no functional change intended.
--- tools/testing/selftests/kvm/arch_timer.c | 4 ++-- tools/testing/selftests/kvm/arm64/vgic_irq.c | 4 ++-- tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c | 4 ++-- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- tools/testing/selftests/kvm/include/kvm_syscalls.h | 2 ++ tools/testing/selftests/kvm/irq_test.c | 2 +- tools/testing/selftests/kvm/irqfd_test.c | 2 +- tools/testing/selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/memstress.c | 2 +- tools/testing/selftests/kvm/lib/userfaultfd_util.c | 3 +-- tools/testing/selftests/kvm/memslot_perf_test.c | 2 +- tools/testing/selftests/kvm/mmu_stress_test.c | 2 +- tools/testing/selftests/kvm/pre_fault_memory_test.c | 2 +- tools/testing/selftests/kvm/rseq_test.c | 2 +- tools/testing/selftests/kvm/s390/memop.c | 2 +- tools/testing/selftests/kvm/set_memory_region_test.c | 4 ++-- tools/testing/selftests/kvm/steal_time.c | 2 +- tools/testing/selftests/kvm/x86/hyperv_ipi.c | 10 ++-------- tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 10 ++-------- .../selftests/kvm/x86/private_mem_conversions_test.c | 2 +- .../selftests/kvm/x86/private_mem_kvm_exits_test.c | 2 +- .../testing/selftests/kvm/x86/recalc_apic_map_test.c | 4 ++-- tools/testing/selftests/kvm/x86/sev_migrate_tests.c | 2 +- tools/testing/selftests/kvm/x86/sync_regs_test.c | 4 ++-- tools/testing/selftests/kvm/x86/tsc_scaling_sync.c | 2 +- tools/testing/selftests/kvm/x86/xapic_ipi_test.c | 12 ++---------- tools/testing/selftests/kvm/x86/xen_shinfo_test.c | 8 ++------ 27 files changed, 38 insertions(+), 61 deletions(-)
tools/testing/selftests/kvm/arch_timer.c +2 -2
@@ -158,10 +158,10 @@ static void test_run(struct kvm_vm *vm) }for (i =0 ; i < test_args.nr_vcpus; i++) -pthread_join (pt_vcpu_run[i],NULL ); +kvm_pthread_join (pt_vcpu_run[i],NULL );if (test_args.migration_freq_ms) -pthread_join (pt_vcpu_migration,NULL ); +kvm_pthread_join (pt_vcpu_migration,NULL );bitmap_free (vcpu_done_map); }
tools/testing/selftests/kvm/arm64/vgic_irq.c +2 -2
@@ -1007,8 +1007,8 @@ static void test_vgic_two_cpus(void *gcode)kvm_pthread_create (&thr[0 ],NULL , test_vcpu_run, vcpus[0 ]);kvm_pthread_create (&thr[1 ],NULL , test_vcpu_run, vcpus[1 ]); -pthread_join (thr[0 ],NULL ); -pthread_join (thr[1 ],NULL ); +kvm_pthread_join (thr[0 ],NULL ); +kvm_pthread_join (thr[1 ],NULL );close (gic_fd);kvm_vm_free (vm);
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +2 -2
@@ -321,13 +321,13 @@ static void run_test(void)clock_gettime (CLOCK_MONOTONIC , &start);for (i =0 ; i < nr_devices; i++) -pthread_join (lpi_threads[i],NULL ); +kvm_pthread_join (lpi_threads[i],NULL ); delta =timespec_elapsed (start);write_guest_global (vm, test_data.request_vcpus_stop, true);for (i =0 ; i < nr_vcpus; i++) -pthread_join (vcpu_threads[i],NULL ); +kvm_pthread_join (vcpu_threads[i],NULL );report_stats (delta); }
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -805,7 +805,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)WRITE_ONCE (host_quit, true);sem_post (&sem_vcpu_cont); -pthread_join (vcpu_thread,NULL ); +kvm_pthread_join (vcpu_thread,NULL );pr_info ("Total bits checked: dirty (%lu), clear (%lu)\n" , host_dirty_count, host_clear_count);
tools/testing/selftests/kvm/include/kvm_syscalls.h +2 -0
@@ -100,6 +100,8 @@ __KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_stypedef void *(*pthread_fn_t )(void *); __KVM_SYSCALL_DEFINE(pthread_create, 4, pthread_t *, thread, const pthread_attr_t *, attr, pthread_fn_t, fn, void *, arg); +__KVM_SYSCALL_DEFINE(pthread_join, 2, pthread_t, thread, void **, thread_return); +__KVM_SYSCALL_DEFINE (pthread_cancel,1 , pthread_t, thread);#define kvm_free_fd (fd) \ do { \
tools/testing/selftests/kvm/irq_test.c +1 -1
@@ -356,7 +356,7 @@ int main(int argc, char **argv)WRITE_AND_SYNC_TO_GUEST (vm, done, true);for (i =0 ; i < nr_vcpus; i++) -pthread_join (vcpu_threads[i],NULL ); +kvm_pthread_join (vcpu_threads[i],NULL );return 0 ; }
tools/testing/selftests/kvm/irqfd_test.c +1 -1
@@ -139,5 +139,5 @@ int main(int argc, char *argv[]) }WRITE_ONCE (done, true); -pthread_join (racing_thread,NULL ); +kvm_pthread_join (racing_thread,NULL ); }
tools/testing/selftests/kvm/kvm_page_table_test.c +1 -1
@@ -413,7 +413,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) }for (i =0 ; i < nr_vcpus; i++) -pthread_join (vcpu_threads[i],NULL ); +kvm_pthread_join (vcpu_threads[i],NULL ); ret =sem_destroy (&test_stage_updated);TEST_ASSERT (ret ==0 ,"Error in sem_destroy" );
tools/testing/selftests/kvm/lib/memstress.c +1 -1
@@ -312,7 +312,7 @@ void memstress_join_vcpu_threads(int nr_vcpus)WRITE_ONCE (memstress_args.stop_vcpus, true);for (i =0 ; i < nr_vcpus; i++) -pthread_join (vcpu_threads[i].thread,NULL ); +kvm_pthread_join (vcpu_threads[i].thread,NULL ); }static void toggle_dirty_logging (struct kvm_vm *vm,int slots,bool enable)
tools/testing/selftests/kvm/lib/userfaultfd_util.c +1 -2
@@ -187,8 +187,7 @@ void uffd_stop_demand_paging(struct uffd_desc *uffd) "Unable to write to pipefd %i for uffd_desc %p", i, uffd);for (i =0 ; i < uffd->num_readers; ++i) -TEST_ASSERT (!pthread_join (uffd->readers[i],NULL ), -"Pthread_join failed on reader %i for uffd_desc %p" , i, uffd); +kvm_pthread_join (uffd->readers[i],NULL );close (uffd->uffd);
tools/testing/selftests/kvm/memslot_perf_test.c +1 -1
@@ -381,7 +381,7 @@ static void free_vm(struct vm_data *data)static void wait_guest_exit (struct vm_data *data) { -pthread_join (data->vcpu_thread,NULL ); +kvm_pthread_join (data->vcpu_thread,NULL ); }static void let_guest_run (struct sync_area *sync)
tools/testing/selftests/kvm/mmu_stress_test.c +1 -1
@@ -409,7 +409,7 @@ int main(int argc, char *argv[])/* Sanity check that the vCPUs actually ran. */ for (i =0 ; i < nr_vcpus; i++) -pthread_join (threads[i],NULL ); +kvm_pthread_join (threads[i],NULL );/* * Deliberately exit without deleting the remaining memslots or closing
tools/testing/selftests/kvm/pre_fault_memory_test.c +1 -1
@@ -116,7 +116,7 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 base_gpa, u64 offset,*/ if (!slot_recreated) {WRITE_ONCE (data.recreate_slot, true); -pthread_join (slot_worker,NULL ); +kvm_pthread_join (slot_worker,NULL ); slot_recreated = true;/*
tools/testing/selftests/kvm/rseq_test.c +1 -1
@@ -314,7 +314,7 @@ int main(int argc, char *argv[])" e.g. via cpuidle.off=1 or via -l <latency>, or run with -u to\n" " disable this sanity check.", i); -pthread_join (migration_thread,NULL ); +kvm_pthread_join (migration_thread,NULL );kvm_vm_free (vm);
tools/testing/selftests/kvm/s390/memop.c +1 -1
@@ -701,7 +701,7 @@ static void test_cmpxchg_key_concurrent(void) } } -pthread_join (thread,NULL ); +kvm_pthread_join (thread,NULL );MOP (t.vcpu,LOGICAL ,READ , mem2, max_block,GADDR_V (mem2)); TEST_ASSERT(popcount_eq(*(__uint128_t *)mem1, *(__uint128_t *)mem2),
tools/testing/selftests/kvm/set_memory_region_test.c +2 -2
@@ -216,7 +216,7 @@ static void test_move_memory_region(bool disable_slot_zap_quirk)/* Defered sync from when the memslot was misaligned (above). */ wait_for_vcpu (); -pthread_join (vcpu_thread,NULL ); +kvm_pthread_join (vcpu_thread,NULL );kvm_vm_free (vm); }
@@ -302,7 +302,7 @@ static void test_delete_memory_region(bool disable_slot_zap_quirk)*/ vm_mem_region_delete (vm,0 ); -pthread_join (vcpu_thread,NULL ); +kvm_pthread_join (vcpu_thread,NULL ); run = vcpu->run;
tools/testing/selftests/kvm/steal_time.c +1 -1
@@ -556,7 +556,7 @@ int main(int ac, char **av)do sched_yield ();while (get_run_delay () - run_delay <MIN_RUN_DELAY_NS ); -pthread_join (thread,NULL ); +kvm_pthread_join (thread,NULL ); run_delay =get_run_delay () - run_delay; TEST_ASSERT(run_delay >= MIN_RUN_DELAY_NS, "Expected run_delay >= %ld, got %ld",
tools/testing/selftests/kvm/x86/hyperv_ipi.c +2 -8
@@ -225,15 +225,9 @@ static void *vcpu_thread(void *arg)static void cancel_join_vcpu_thread (pthread_t thread,struct kvm_vcpu *vcpu) {void *retval; -int r; - r =pthread_cancel (thread); -TEST_ASSERT (!r,"pthread_cancel on vcpu_id=%d failed with errno=%d" , - vcpu->id, r); - - r =pthread_join (thread, &retval); -TEST_ASSERT (!r,"pthread_join on vcpu_id=%d failed with errno=%d" , - vcpu->id, r); +kvm_pthread_cancel (thread); +kvm_pthread_join (thread, &retval);TEST_ASSERT (retval ==PTHREAD_CANCELED ,"expected retval=%p, got %p" ,PTHREAD_CANCELED , retval);
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +2 -8
@@ -551,15 +551,9 @@ static void *vcpu_thread(void *arg)static void cancel_join_vcpu_thread (pthread_t thread,struct kvm_vcpu *vcpu) {void *retval; -int r; - r =pthread_cancel (thread); -TEST_ASSERT (!r,"pthread_cancel on vcpu_id=%d failed with errno=%d" , - vcpu->id, r); - - r =pthread_join (thread, &retval); -TEST_ASSERT (!r,"pthread_join on vcpu_id=%d failed with errno=%d" , - vcpu->id, r); +kvm_pthread_cancel (thread); +kvm_pthread_join (thread, &retval);TEST_ASSERT (retval ==PTHREAD_CANCELED ,"expected retval=%p, got %p" ,PTHREAD_CANCELED , retval);
tools/testing/selftests/kvm/x86/private_mem_conversions_test.c +1 -1
@@ -418,7 +418,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, u32 nr_vWRITE_ONCE (run_vcpus, true);for (i =0 ; i < nr_vcpus; i++) -pthread_join (threads[i],NULL ); +kvm_pthread_join (threads[i],NULL );kvm_vm_free (vm);
tools/testing/selftests/kvm/x86/private_mem_kvm_exits_test.c +1 -1
@@ -70,7 +70,7 @@ static void test_private_access_memslot_deleted(void)vm_mem_region_delete (vm,EXITS_TEST_SLOT ); -pthread_join (vm_thread, &thread_return); +kvm_pthread_join (vm_thread, &thread_return); exit_reason = (u32 )(u64 )thread_return;TEST_ASSERT_EQ (exit_reason,KVM_EXIT_MEMORY_FAULT );
tools/testing/selftests/kvm/x86/recalc_apic_map_test.c +2 -2
@@ -65,8 +65,8 @@ int main(void)vcpu_set_msr (vcpuN,MSR_IA32_APICBASE ,LAPIC_DISABLED ); } -TEST_ASSERT_EQ (pthread_cancel (thread),0 ); -TEST_ASSERT_EQ (pthread_join (thread,NULL ),0 ); +kvm_pthread_cancel (thread); +kvm_pthread_join (thread,NULL );kvm_vm_free (vm);
tools/testing/selftests/kvm/x86/sev_migrate_tests.c +1 -1
@@ -131,7 +131,7 @@ static void test_sev_migrate_locking(void)kvm_pthread_create (&pt[i],NULL , locking_test_thread, &input[i]);for (i =0 ; i <NR_LOCK_TESTING_THREADS ; ++i) -pthread_join (pt[i],NULL ); +kvm_pthread_join (pt[i],NULL );for (i =0 ; i <NR_LOCK_TESTING_THREADS ; ++i)kvm_vm_free (input[i].vm); }
tools/testing/selftests/kvm/x86/sync_regs_test.c +2 -2
@@ -199,8 +199,8 @@ static void race_sync_regs(struct kvm_vcpu *vcpu, void *racer) } } -TEST_ASSERT_EQ (pthread_cancel (thread),0 ); -TEST_ASSERT_EQ (pthread_join (thread,NULL ),0 ); +kvm_pthread_cancel (thread); +kvm_pthread_join (thread,NULL );kvm_x86_state_cleanup (state); }
tools/testing/selftests/kvm/x86/tsc_scaling_sync.c +1 -1
@@ -99,7 +99,7 @@ int main(int argc, char *argv[])unsigned long failures =0 ;for (cpu =0 ; cpu <NR_TEST_VCPUS ; cpu++) {void *this_cpu_failures; -pthread_join (cpu_threads[cpu], &this_cpu_failures); +kvm_pthread_join (cpu_threads[cpu], &this_cpu_failures); failures += (unsigned long )this_cpu_failures; }
tools/testing/selftests/kvm/x86/xapic_ipi_test.c +2 -10
@@ -231,17 +231,9 @@ static void *vcpu_thread(void *arg)static void cancel_join_vcpu_thread (pthread_t thread,struct kvm_vcpu *vcpu) {void *retval; -int r; - r =pthread_cancel (thread); -TEST_ASSERT (r ==0 , -"pthread_cancel on vcpu_id=%d failed with errno=%d" , - vcpu->id, r); - - r =pthread_join (thread, &retval); -TEST_ASSERT (r ==0 , -"pthread_join on vcpu_id=%d failed with errno=%d" , - vcpu->id, r); +kvm_pthread_cancel (thread); +kvm_pthread_join (thread, &retval);TEST_ASSERT (retval ==PTHREAD_CANCELED ,"expected retval=%p, got %p" ,PTHREAD_CANCELED , retval);
tools/testing/selftests/kvm/x86/xen_shinfo_test.c +2 -6
@@ -437,7 +437,6 @@ int main(int argc, char *argv[])struct kvm_vm *vm;pthread_t thread;bool verbose; -int ret; verbose = argc >1 && (!strncmp (argv[1 ],"-v" ,3 ) || !strncmp (argv[1 ],"--verbose" ,10 ));
@@ -948,11 +947,8 @@ int main(int argc, char *argv[])TEST_ASSERT (!evtchn_irq_expected,"Expected event channel IRQ but it didn't happen" ); - ret =pthread_cancel (thread); -TEST_ASSERT (ret ==0 ,"pthread_cancel() failed: %s" ,strerror (ret)); - - ret =pthread_join (thread,0 ); -TEST_ASSERT (ret ==0 ,"pthread_join() failed: %s" ,strerror (ret)); +kvm_pthread_cancel (thread); +kvm_pthread_join (thread,0 ); goto done;case TEST_GUEST_SAW_IRQ :
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH 7/9 KVM: selftests: Add helper APIs to cancel+join pthreads
In reply to Sean ChristophersonDeduplicate test code that does cancel()+join() on pthreads by adding a collection of syscall wrappers. The tests that asynchronously cancel pthreads, i.e. use PTHREAD_CANCEL_ASYNCHRONOUS, are particularly ugly, especially since it's not immediately obvious why those tests assert on the thread being CANCELED, whereas others do not.
No functional change intended.
--- .../selftests/kvm/include/kvm_syscalls.h | 24 +++++++++++++++++++ tools/testing/selftests/kvm/x86/hyperv_ipi.c | 15 ++---------- .../selftests/kvm/x86/hyperv_tlb_flush.c | 15 ++---------- .../selftests/kvm/x86/recalc_apic_map_test.c | 3 +-- .../selftests/kvm/x86/sync_regs_test.c | 3 +-- .../selftests/kvm/x86/xapic_ipi_test.c | 15 ++---------- .../selftests/kvm/x86/xen_shinfo_test.c | 3 +-- 7 files changed, 33 insertions(+), 45 deletions(-)
tools/testing/selftests/kvm/include/kvm_syscalls.h +24 -0
@@ -103,6 +103,30 @@ __KVM_SYSCALL_DEFINE(pthread_create, 4, pthread_t *, thread, __KVM_SYSCALL_DEFINE(pthread_join, 2, pthread_t, thread, void **, thread_return);__KVM_SYSCALL_DEFINE (pthread_cancel,1 , pthread_t, thread); +static inline void __kvm_cancel_join_pthread (pthread_t thread,void **r) +{ +kvm_pthread_cancel (thread); +kvm_pthread_join (thread, r); +} + +static inline void kvm_cancel_join_pthread (pthread_t thread) +{ +__kvm_cancel_join_pthread (thread,NULL ); +} + +/* +* Cancel+Join a pthread that was configured with PTHREAD_CANCEL_ASYNCHRONOUS +* and is expected to exit only in response to cancellation. +*/ +static inline void kvm_cancel_join_pthread_async (pthread_t thread) +{ +void *r; + +__kvm_cancel_join_pthread (thread, &r); +TEST_ASSERT (r ==PTHREAD_CANCELED , +"expected retval=%p, got %p" ,PTHREAD_CANCELED , r); +} +#define kvm_free_fd (fd) \ do { \ kvm_close(fd); \
tools/testing/selftests/kvm/x86/hyperv_ipi.c +2 -13
@@ -222,17 +222,6 @@ static void *vcpu_thread(void *arg)return NULL ; } -static void cancel_join_vcpu_thread (pthread_t thread,struct kvm_vcpu *vcpu) -{ -void *retval; - -kvm_pthread_cancel (thread); -kvm_pthread_join (thread, &retval); -TEST_ASSERT (retval ==PTHREAD_CANCELED , -"expected retval=%p, got %p" ,PTHREAD_CANCELED , - retval); -} -int main (int argc,char *argv[]) {struct kvm_vm *vm;
@@ -293,8 +282,8 @@ int main(int argc, char *argv[]) } done: -cancel_join_vcpu_thread (threads[0 ], vcpu[1 ]); -cancel_join_vcpu_thread (threads[1 ], vcpu[2 ]); +kvm_cancel_join_pthread_async (threads[0 ]); +kvm_cancel_join_pthread_async (threads[1 ]);kvm_vm_free (vm);return 0 ;
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +2 -13
@@ -548,17 +548,6 @@ static void *vcpu_thread(void *arg)return NULL ; } -static void cancel_join_vcpu_thread (pthread_t thread,struct kvm_vcpu *vcpu) -{ -void *retval; - -kvm_pthread_cancel (thread); -kvm_pthread_join (thread, &retval); -TEST_ASSERT (retval ==PTHREAD_CANCELED , -"expected retval=%p, got %p" ,PTHREAD_CANCELED , - retval); -} -int main (int argc,char *argv[]) {struct kvm_vm *vm;
@@ -652,8 +641,8 @@ int main(int argc, char *argv[]) } done: -cancel_join_vcpu_thread (threads[0 ], vcpu[1 ]); -cancel_join_vcpu_thread (threads[1 ], vcpu[2 ]); +kvm_cancel_join_pthread_async (threads[0 ]); +kvm_cancel_join_pthread_async (threads[1 ]);kvm_vm_free (vm);return 0 ;
tools/testing/selftests/kvm/x86/recalc_apic_map_test.c +1 -2
@@ -65,8 +65,7 @@ int main(void)vcpu_set_msr (vcpuN,MSR_IA32_APICBASE ,LAPIC_DISABLED ); } -kvm_pthread_cancel (thread); -kvm_pthread_join (thread,NULL ); +kvm_cancel_join_pthread (thread);kvm_vm_free (vm);
tools/testing/selftests/kvm/x86/sync_regs_test.c +1 -2
@@ -199,8 +199,7 @@ static void race_sync_regs(struct kvm_vcpu *vcpu, void *racer) } } -kvm_pthread_cancel (thread); -kvm_pthread_join (thread,NULL ); +kvm_cancel_join_pthread (thread);kvm_x86_state_cleanup (state); }
tools/testing/selftests/kvm/x86/xapic_ipi_test.c +2 -13
@@ -228,17 +228,6 @@ static void *vcpu_thread(void *arg)return NULL ; } -static void cancel_join_vcpu_thread (pthread_t thread,struct kvm_vcpu *vcpu) -{ -void *retval; - -kvm_pthread_cancel (thread); -kvm_pthread_join (thread, &retval); -TEST_ASSERT (retval ==PTHREAD_CANCELED , -"expected retval=%p, got %p" ,PTHREAD_CANCELED , - retval); -} -void do_migrations (struct test_data_page *data,int run_secs,int delay_usecs,u64 *pipis_rcvd) {
@@ -450,8 +439,8 @@ int main(int argc, char *argv[])/* * Cancel threads and wait for them to stop. */ -cancel_join_vcpu_thread (threads[0 ], params[0 ].vcpu); -cancel_join_vcpu_thread (threads[1 ], params[1 ].vcpu); +kvm_cancel_join_pthread_async (threads[0 ]); +kvm_cancel_join_pthread_async (threads[1 ]);/* * If the host support Idle HLT, i.e. KVM *might* be using Idle HLT,
tools/testing/selftests/kvm/x86/xen_shinfo_test.c +1 -2
@@ -947,8 +947,7 @@ int main(int argc, char *argv[])TEST_ASSERT (!evtchn_irq_expected,"Expected event channel IRQ but it didn't happen" ); -kvm_pthread_cancel (thread); -kvm_pthread_join (thread,0 ); +kvm_cancel_join_pthread (thread); goto done;case TEST_GUEST_SAW_IRQ :
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH 8/9 KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np()
In reply to Sean ChristophersonAdd and use KVM wrappers for pthread_{g,s}etaffinity_np() so that selftests don't need to manually assert that the syscalls succeeded, and so that they don't need to manually pass in sizeof(cpu_set_t) for the size.
Note, the steal time test didn't assert success, but the TEST_ASSERT() one line later makes it quite clear the test relies on pthread_getaffinity_np() succeeding.
--- .../selftests/kvm/hardware_disable_test.c | 12 ++---------- .../testing/selftests/kvm/include/kvm_syscalls.h | 16 ++++++++++++++++ tools/testing/selftests/kvm/steal_time.c | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +2 -10
@@ -54,14 +54,6 @@ static void *sleeping_thread(void *arg)TEST_FAIL ("%s: exited" , __func__); } -static inline void check_set_affinity (pthread_t thread,cpu_set_t *cpu_set) -{ -int r; - - r =pthread_setaffinity_np (thread,sizeof (cpu_set_t), cpu_set); -TEST_ASSERT (r ==0 ,"%s: failed set affinity" , __func__); -} -static void run_test (u32 run) {struct kvm_vcpu *vcpu;
@@ -76,11 +68,11 @@ static void run_test(u32 run) vcpu =vm_vcpu_add (vm, i, guest_code);kvm_pthread_create (&thread,NULL , run_vcpu, vcpu); -check_set_affinity (thread, &child_cpu_set); +kvm_pthread_setaffinity (thread, &child_cpu_set);for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) {kvm_pthread_create (&thread,NULL , sleeping_thread, (void *)NULL ); -check_set_affinity (thread, &child_cpu_set); +kvm_pthread_setaffinity (thread, &child_cpu_set); } }pr_debug ("%s: [%d] all threads launched\n" , __func__, run);
tools/testing/selftests/kvm/include/kvm_syscalls.h +16 -0
@@ -97,6 +97,22 @@ __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice); __KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask); +__KVM_SYSCALL_DEFINE (pthread_getaffinity_np,3 , pthread_t, thread, + size_t, cpusetsize, cpu_set_t *, cpuset); +__KVM_SYSCALL_DEFINE (pthread_setaffinity_np,3 , pthread_t, thread, + size_t, cpusetsize, const cpu_set_t *, cpuset); + +static inline void kvm_pthread_getaffinity (pthread_t thread,cpu_set_t *cpuset) +{ +kvm_pthread_getaffinity_np (thread,sizeof (cpu_set_t), cpuset); +} + +static inline void kvm_pthread_setaffinity (pthread_t thread, +const cpu_set_t *cpuset) +{ +kvm_pthread_setaffinity_np (thread,sizeof (cpu_set_t), cpuset); +} +typedef void *(*pthread_fn_t )(void *); __KVM_SYSCALL_DEFINE(pthread_create, 4, pthread_t *, thread, const pthread_attr_t *, attr, pthread_fn_t, fn, void *, arg);
tools/testing/selftests/kvm/steal_time.c +1 -1
@@ -549,7 +549,7 @@ 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 ();kvm_pthread_create (&thread,NULL , do_steal_time,NULL ); -pthread_getaffinity_np (thread,sizeof (cpuset), &cpuset); +kvm_pthread_getaffinity (thread, &cpuset);TEST_ASSERT (CPU_COUNT (&cpuset) ==1 &&CPU_ISSET (cpu, &cpuset),"Worker failed to inherit parent's CPU affinity" );
Signature · 1 line
2.55.0.508.g3f0d502094-goog
[email protected]
AI reviewRe: [PATCH 8/9] KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM syscall wrappers for pthread functions produce misleading error messages on failure. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM syscall wrappers for pthread functions produce misleading error messages on failure. --
--- Patch [8]: [PATCH 8/9] KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np() --- commit 9fc7623d339b95135fb5ecb9685356bc8dff9829
Author: Sean Christopherson <[email protected]>
KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np()
This commit adds KVM wrappers for pthread_getaffinity_np() and pthread_setaffinity_np() to simplify selftest code. It removes the need to manually check for success or supply the size of cpu_set_t in the tests.
[ ... ]
tools/testing/selftests/kvm/include/kvm_syscalls.h __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); · line 103
Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +4
-0@@ -97,6 +97,22 @@ __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice); __KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask); +__KVM_SYSCALL_DEFINE (pthread_getaffinity_np,3 , pthread_t, thread, + size_t, cpusetsize, cpu_set_t *, cpuset); +__KVM_SYSCALL_DEFINE (pthread_setaffinity_np,3 , pthread_t, thread, + size_t, cpusetsize, const cpu_set_t *, cpuset);
[Severity: Medium]
Will these pthread wrappers print misleading error messages on failure?
The pthread_* functions return positive error codes directly on failure rather than returning -1 and setting errno. If __KVM_SYSCALL_DEFINE formats its failure messages assuming errno is set, failing to set affinity could result in printing a stale or unrelated error string (such as "Success") alongside the return code, which might impede debugging.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Sean Christopherson
maintainerRe: [PATCH 8/9] KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np()
Quoted 32 lines · On Thu, Jul 30, 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM syscall wrappers for pthread functions produce misleading error messages on failure. ----- Patch [8]: [PATCH 8/9] KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np() --- commit
9fc7623d339b95135fb5ecb9685356bc8dff9829
Author: Sean Christopherson <[email protected]>KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np()
This commit adds KVM wrappers for
pthread_getaffinity_np()andpthread_setaffinity_np()to simplify selftest code. It removes the need to manually check for success or supply the size ofcpu_set_tin the tests.[ ... ]
tools/testing/selftests/kvm/include/kvm_syscalls.h __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); · line 103
Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +4
-0@@ -97,6 +97,22 @@ __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length); __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice); __KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask); +__KVM_SYSCALL_DEFINE (pthread_getaffinity_np,3 , pthread_t, thread, + size_t, cpusetsize, cpu_set_t *, cpuset); +__KVM_SYSCALL_DEFINE (pthread_setaffinity_np,3 , pthread_t, thread, + size_t, cpusetsize, const cpu_set_t *, cpuset);[Severity: Medium]
Will these pthread wrappers print misleading error messages on failure?
Quoted 5 lines
The pthread_* functions return positive error codes directly on failure rather than returning -1 and setting errno. If
__KVM_SYSCALL_DEFINEformats its failure messages assuming errno is set, failing to set affinity could result in printing a stale or unrelated error string (such as "Success") alongside the return code, which might impede debugging.
Empirically, no. Just because they _also_ return an exact error code doesn't make errno invalid.
==== Test Assertion Failure ====
include/kvm_syscalls.h:117: !r
pid=155681 tid=155681 errno=11 - Resource temporarily unavailable pthread_create failed, rc: 11 errno: 11 (Resource temporarily unavailable)
Sean Christopherson
maintainerPATCH 9/9 KVM: selftests: Clean up global constants in hardware disable test
In reply to Sean ChristophersonRename the global constants in the hardware disable test to better capture what they control, and open code the literal number of sleeping tasks and iterations instead of using power-of-2 math to express values that are 100% arbitrary.
No functional change intended.
--- .../selftests/kvm/hardware_disable_test.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +9 -9
@@ -17,10 +17,10 @@#include "kvm_util.h" #include "ucall_common.h" -#define VCPU_NUM 4 -#define SLEEPING_THREAD_NUM (1 << 4) -#define FORK_NUM (1ULL << 9) -#define DELAY_US_MAX 2000 +#define NR_VCPUS 4 +#define NR_SLEEPERS_PER_VCPU 16 +#define NR_ITERATIONS 512 +#define DELAY_US_MAX 2000sem_t *sem;static cpu_set_t child_cpu_set;
@@ -61,16 +61,16 @@ static void run_test(u32 run)pthread_t thread;u32 i, j; - vm =vm_create (VCPU_NUM ); + vm =vm_create (NR_VCPUS );pr_debug ("%s: [%d] start vcpus\n" , __func__, run); -for (i =0 ; i <VCPU_NUM ; ++i) { +for (i =0 ; i <NR_VCPUS ; ++i) { vcpu =vm_vcpu_add (vm, i, guest_code);kvm_pthread_create (&thread,NULL , run_vcpu, vcpu);kvm_pthread_setaffinity (thread, &child_cpu_set); -for (j =0 ; j <SLEEPING_THREAD_NUM ; ++j) { +for (j =0 ; j <NR_SLEEPERS_PER_VCPU ; ++j) {kvm_pthread_create (&thread,NULL , sleeping_thread, (void *)NULL );kvm_pthread_setaffinity (thread, &child_cpu_set); }
@@ -124,7 +124,7 @@ static void setup_child_cpu_set(void) cpu =pin_task_to_random_cpu (pthread_self (), &child_cpu_set);CPU_CLR (cpu, &child_cpu_set); -while (CPU_COUNT (&child_cpu_set) >VCPU_NUM ) +while (CPU_COUNT (&child_cpu_set) >NR_VCPUS )CPU_CLR (kvm_pick_random_cpu (&child_cpu_set), &child_cpu_set); }
@@ -139,7 +139,7 @@ int main(int argc, char **argv) sem =sem_open ("vm_sem" ,O_CREAT |O_EXCL ,0644 ,0 );sem_unlink ("vm_sem" ); -for (i =0 ; i <FORK_NUM ; ++i) { +for (i =0 ; i <NR_ITERATIONS ; ++i) { pid =fork ();TEST_ASSERT (pid >=0 ,"%s: unable to fork" , __func__);if (pid ==0 )
Signature · 1 line
2.55.0.508.g3f0d502094-goog