Skip to contentSkip to conversation

KVM: selftests: Add more syscall wrappers, fix hardware_disable_test

PATCH Superseded Sean Christopherson 2 participants 18 messages Jul 31

Subsystems (5)
KVM Kselftest KVM x86 KVM arm64 +1 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v2 cover 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 when running a preemptible kernels on a large NUMA system. Turns out my analysis/guess in v1 was wrong; the problem isn't direct CPU contention, it's that letting the workers do any amount of "real" work on any CPUs leads to pthread_create() slowdowns from the perspective of the caller.

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

As with v1, I'm planning on taking this through kvm-x86.

v2:
- pause() the child after creating workers so as not to busy loop, and to maintain equivalent functionality. [Sashiko]
- Don't bother pinning the parent of immediate child, and instead focus on affining workers to their CPUs asap.

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (12):
KVM: selftests: Add a KVM syscall wrapper for sched_setaffinity()
KVM: selftests: Set threads CPU affinity before doing work in hardware
disable test
KVM: selftests: Pre-set threads affinity in hardware disable test when
possible
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 threads to random CPUs 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     | 103 ++++++++----------
 .../selftests/kvm/include/kvm_syscalls.h      |  48 ++++++++
 .../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       |  12 +-
 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, 183 insertions(+), 230 deletions(-)

base-commit: 3c7d7f908d574277a845423ec32250a8d8df44c8

Signature · 1 line
2.55.0.508.g3f0d502094-goog
author maintainer
#1 lore raw

PATCH v2 1/12 KVM: selftests: Add a KVM syscall wrapper for sched_setaffinity()

Add and use a KVM wrapper for sched_setaffinity() so that selftests don't need to manually assert that the syscall succeeded.

No functional change intended.

---
 tools/testing/selftests/kvm/include/kvm_syscalls.h | 1 +
 tools/testing/selftests/kvm/rseq_test.c            | 6 ++----
 2 files changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/kvm_syscalls.h +1 -0
index 5dae6143ddb0..01dca99009c2 100644
@@ -95,6 +95,7 @@ __KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, loff_t, l
 __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(sched_setaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask);
 
 #define kvm_free_fd(fd)		\
 do {				\
tools/testing/selftests/kvm/rseq_test.c +2 -4
index 557e393c223b..edcaf0fbe20c 100644
@@ -75,7 +75,7 @@ static void *migration_worker(void *__rseq_tid)
 {
 	pid_t rseq_tid = (pid_t)(unsigned long)__rseq_tid;
 	cpu_set_t allowed_mask;
-	int r, i, cpu;
+	int i, cpu;
 
 	CPU_ZERO(&allowed_mask);
 
@@ -96,9 +96,7 @@ static void *migration_worker(void *__rseq_tid)
 		 * stable, i.e. while changing affinity is in-progress.
 		 */
 		smp_wmb();
-		r = sched_setaffinity(rseq_tid, sizeof(allowed_mask), &allowed_mask);
-		TEST_ASSERT(!r, "sched_setaffinity failed, errno = %d (%s)",
-			    errno, strerror(errno));
+		kvm_sched_setaffinity(rseq_tid, sizeof(allowed_mask), &allowed_mask);
 		smp_wmb();
 		atomic_inc(&seq_cnt);
 
Signature · 1 line
2.55.0.508.g3f0d502094-goog
author maintainer
#2 lore raw

PATCH v2 2/12 KVM: selftests: Set threads CPU affinity before doing work in hardware disable test

In reply to Sean Christopherson

Affine worker threads to their target pCPU set *before* doing any work in the hardware disable test. When running a preemptible kernel on a NUMA system with a large number of CPUs (100+ of CPUs), waiting to set the affinity of the grandchildren until after the grandchild is fully spun up results in total test runtimes of over a minute, presumably due to cross-node NUMA penalties and scheduler lock contention.

E.g. from the perspective of the caller, when waiting to affine the tasks, pthread_create() takes ~100x longer.

---
 .../selftests/kvm/hardware_disable_test.c     | 23 +++++++------------
 1 file changed, 8 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +8 -15
index 3147f5c97e94..f088b4af3ccc 100644
@@ -21,6 +21,7 @@
 #define FORK_NUM (1ULL << 9)
 #define DELAY_US_MAX 2000
 
+static cpu_set_t threads_cpu_set;
 sem_t *sem;
 
 static void guest_code(void)
@@ -35,6 +36,8 @@ static void *run_vcpu(void *arg)
 	struct kvm_vcpu *vcpu = arg;
 	struct kvm_run *run = vcpu->run;
 
+	kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
+
 	vcpu_run(vcpu);
 
 	TEST_ASSERT(false, "%s: exited with reason %d: %s",
@@ -47,6 +50,8 @@ static void *sleeping_thread(void *arg)
 {
 	int fd;
 
+	kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
+
 	while (true) {
 		fd = open("/dev/null", O_RDWR);
 		close(fd);
@@ -64,14 +69,6 @@ static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
 	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;
-
-	r = pthread_setaffinity_np(thread, sizeof(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;
@@ -84,16 +81,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 +93,10 @@ 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);
 
 		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);
 		}
 	}
 	pr_debug("%s: [%d] all threads launched\n", __func__, run);
@@ -153,6 +143,9 @@ int main(int argc, char **argv)
 	int s, r;
 	pid_t pid;
 
+	for (i = 0; i < VCPU_NUM; i++)
+		CPU_SET(i, &threads_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
author maintainer
#3 lore raw

PATCH v2 3/12 KVM: selftests: Pre-set threads affinity in hardware disable test when possible

In reply to Sean Christopherson

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.

---
 .../testing/selftests/kvm/hardware_disable_test.c  | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +12 -2
index f088b4af3ccc..d8288147b0a6 100644
@@ -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);
 		}
 	}
Signature · 1 line
2.55.0.508.g3f0d502094-goog
#4 lore raw

Re: [PATCH v2 03/12] KVM: selftests: Pre-set threads affinity in hardware disable test when possible

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The return value of `pthread_attr_setaffinity_np()` is ignored, leading to silent failures and…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The return value of pthread_attr_setaffinity_np() is ignored, leading to silent failures and divergent error-handling between libcs.
- [Low] The test incorrectly uses the internal glibc macro __USE_GNU to detect GNU extension availability.
--

--- Patch [3]: [PATCH v2 03/12] KVM: selftests: Pre-set threads affinity in hardware disable test when possible --- commit e87ff4fe7aad8c6f99e1f03d8e09fe9b5ceed69d
Author: Sean Christopherson <[email protected]>

KVM: selftests: Pre-set threads affinity in hardware disable test when possible

This commit attempts to reduce the perceived latency of pthread_create() on large NUMA systems by pre-setting the CPU affinity for grandchildren threads using pthread_attr_setaffinity_np() when building against a compatible libc.

tools/testing/selftests/kvm/hardware_disable_test.c static void *run_vcpu(void *arg) · line 39

Quoted 9 lines
tools/testing/selftests/kvm/hardware_disable_test.c +1 -0
index f088b4af3ccc7..d8288147b0a69 100644
@@ -36,7 +36,9 @@ static void *run_vcpu(void *arg)
 	struct kvm_vcpu *vcpu = arg;
 	struct kvm_run *run = vcpu->run;
 
+#ifndef __USE_GNU

[Severity: Low]
Does checking __USE_GNU correctly detect the availability of this API across different libc implementations?

__USE_GNU is an internal glibc macro. Other implementations like musl provide pthread_attr_setaffinity_np() and support _GNU_SOURCE, but do not define __USE_GNU, which would unnecessarily disable this optimization for them.

Should this code check for _GNU_SOURCE or rely on a standard feature test macro instead?

Quoted 5 lines
tools/testing/selftests/kvm/hardware_disable_test.c +2 -0
+	kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
+#endif
 
 	vcpu_run(vcpu);
 

[ ... ]

Quoted 4 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0 -0
@@ -80,22 +84,28 @@ static inline void check_join(pthread_t thread, void **retval)
 static void run_test(u32 run)
 {
 	struct kvm_vcpu *vcpu;

tools/testing/selftests/kvm/hardware_disable_test.c static inline void check_join(pthread_t thread, void **retval) · line 96

Quoted 10 lines
tools/testing/selftests/kvm/hardware_disable_test.c +4 -0
+	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);

[Severity: Medium]
Is it intentional to ignore the return value of pthread_attr_setaffinity_np() here?

If this fails (for example, due to cgroup CPU mask restrictions), it silently leaves the attribute unmodified and the optimization is missed. Meanwhile, the fallback path in run_vcpu() using kvm_sched_setaffinity() explicitly asserts and fails the test on error.

Should the return value be checked to maintain consistent test behavior and error handling between the two paths?

Quoted 3 lines
tools/testing/selftests/kvm/hardware_disable_test.c +2 -0
+#endif
+
 	vm = vm_create(VCPU_NUM);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#5 lore raw

PATCH v2 4/12 KVM: selftests: Return the target CPU from pin_task_to_random_cpu()

In reply to Sean Christopherson

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
index b39e713c30a4..61d72e1e7601 100644
@@ -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
index 3794575d2ca0..af2bad0553f9 100644
@@ -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
author maintainer
#6 lore raw

PATCH v2 5/12 KVM: selftests: Extract picking of random CPU from cpu_set_t to separate API

In reply to Sean Christopherson

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.

---
 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
index 61d72e1e7601..5a7a455b5387 100644
@@ -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
index af2bad0553f9..9ddc047d5c27 100644
@@ -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
author maintainer
#7 lore raw

PATCH v2 6/12 KVM: selftests: Affine threads to random CPUs in hardware disable test

In reply to Sean Christopherson

Affine the worker threads to random CPUs in the hardware disable test, and honor the starting CPU set in the process. Hardcoding to CPUs 0-3 is both wrong and less interesting than running on random CPUs.

---
 tools/testing/selftests/kvm/hardware_disable_test.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +5 -2
index d8288147b0a6..bd23c1300370 100644
@@ -149,12 +149,15 @@ void wait_for_child_setup(pid_t pid)
 
 int main(int argc, char **argv)
 {
+	cpu_set_t allowed_cpu_set;
 	u32 i;
 	int s, r;
 	pid_t pid;
 
-	for (i = 0; i < VCPU_NUM; i++)
-		CPU_SET(i, &threads_cpu_set);
+	kvm_sched_getaffinity(0, sizeof(cpu_set_t), &allowed_cpu_set);
+
+	for (i = 0; i < VCPU_NUM && CPU_COUNT(&allowed_cpu_set); i++)
+		CPU_SET(kvm_pick_random_cpu(&allowed_cpu_set), &threads_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
#8 lore raw

Re: [PATCH v2 06/12] KVM: selftests: Affine threads to random CPUs in hardware disable test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Randomly selected CPUs are not removed from `allowed_cpu_set`, leading to duplicate selections…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Randomly selected CPUs are not removed from allowed_cpu_set, leading to duplicate selections and artificially reduced test concurrency. --

--- Patch [6]: [PATCH v2 06/12] KVM: selftests: Affine threads to random CPUs in hardware disable test --- commit 093b65b96e15e7cdd5b611bd7d8556fb88cae6e6
Author: Sean Christopherson <[email protected]>

KVM: selftests: Affine threads to random CPUs in hardware disable test

This patch modifies the hardware disable test to affine worker threads to random CPUs instead of hardcoding them to CPUs 0-3. It also honors the starting CPU set during this process.

Quoted 9 lines
tools/testing/selftests/kvm/hardware_disable_test.c +1 -0
index d8288147b0a69..bd23c13003707 100644
@@ -149,12 +149,15 @@ void wait_for_child_setup(pid_t pid)
 
 int main(int argc, char **argv)
 {
+	cpu_set_t allowed_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 +4 -2
 	u32 i;
 	int s, r;
 	pid_t pid;
 
-	for (i = 0; i < VCPU_NUM; i++)
-		CPU_SET(i, &threads_cpu_set);
+	kvm_sched_getaffinity(0, sizeof(cpu_set_t), &allowed_cpu_set);
+
+	for (i = 0; i < VCPU_NUM && CPU_COUNT(&allowed_cpu_set); i++)
+		CPU_SET(kvm_pick_random_cpu(&allowed_cpu_set), &threads_cpu_set);

[Severity: Medium]
Since kvm_pick_random_cpu() doesn't clear the selected CPU from allowed_cpu_set, can the same CPU be selected multiple times here?

Because the selected CPU is not cleared (for example, with CPU_CLR), threads_cpu_set could end up with fewer than VCPU_NUM unique CPUs. This might artificially reduce the test's concurrency.

Additionally, the loop condition checks CPU_COUNT(&allowed_cpu_set), but since allowed_cpu_set is never modified inside the loop, this count will remain constant. Was there an intention to remove the chosen CPU from allowed_cpu_set in each iteration?

Quoted 2 lines
tools/testing/selftests/kvm/hardware_disable_test.c +0 -0
 	sem = sem_open("vm_sem", O_CREAT | O_EXCL, 0644, 0);
 	sem_unlink("vm_sem");
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author maintainer
#9 lore raw

Re: [PATCH v2 06/12] KVM: selftests: Affine threads to random CPUs in hardware disable test

Quoted 27 lines · On Fri, Jul 31, 2026, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Randomly selected CPUs are not removed from allowed_cpu_set, leading to duplicate selections and artificially reduced test concurrency. --

--- Patch [6]: [PATCH v2 06/12] KVM: selftests: Affine threads to random CPUs in hardware disable test --- commit 093b65b96e15e7cdd5b611bd7d8556fb88cae6e6
Author: Sean Christopherson <[email protected]>

KVM: selftests: Affine threads to random CPUs in hardware disable test

This patch modifies the hardware disable test to affine worker threads to random CPUs instead of hardcoding them to CPUs 0-3. It also honors the starting CPU set during this process.

Quoted 13 lines
tools/testing/selftests/kvm/hardware_disable_test.c +1 -0
index d8288147b0a69..bd23c13003707 100644
@@ -149,12 +149,15 @@ void wait_for_child_setup(pid_t pid)
 
 int main(int argc, char **argv)
 {
+	cpu_set_t allowed_cpu_set;
 	u32 i;
 	int s, r;
 	pid_t pid;
 
Quoted 10 lines

tools/testing/selftests/kvm/hardware_disable_test.c void wait_for_child_setup(pid_t pid) · line 160

Quoted 6 lines
tools/testing/selftests/kvm/hardware_disable_test.c +4 -2
-	for (i = 0; i < VCPU_NUM; i++)
-		CPU_SET(i, &threads_cpu_set);
+	kvm_sched_getaffinity(0, sizeof(cpu_set_t), &allowed_cpu_set);
+
+	for (i = 0; i < VCPU_NUM && CPU_COUNT(&allowed_cpu_set); i++)
+		CPU_SET(kvm_pick_random_cpu(&allowed_cpu_set), &threads_cpu_set);

[Severity: Medium]
Since kvm_pick_random_cpu() doesn't clear the selected CPU from allowed_cpu_set, can the same CPU be selected multiple times here?

/facepalm

Remembered to CPU_CLR() the selected cpu in all my fancy version, then forgot to do so for the simple version.

author maintainer
#10 lore raw

PATCH v2 7/12 KVM: selftests: Drop unreachable, dead code from hardware disable test

In reply to Sean Christopherson

Drop 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     | 41 +++++++------------
 1 file changed, 14 insertions(+), 27 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +14 -27
index bd23c1300370..b8115b2dcae3 100644
@@ -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)
@@ -42,10 +43,8 @@ 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)
@@ -56,12 +55,11 @@ static void *sleeping_thread(void *arg)
 	kvm_sched_setaffinity(0, sizeof(cpu_set_t), &threads_cpu_set);
 #endif
 
-	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,22 +71,12 @@ static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr,
 	TEST_ASSERT(r == 0, "%s: failed to create thread", __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;
 	pthread_attr_t attr;
 	struct kvm_vm *vm;
-	pthread_t threads[VCPU_NUM];
-	pthread_t throw_away;
-	void *b;
+	pthread_t thread;
 	u32 i, j;
 
 	pthread_attr_init(&attr);
@@ -102,19 +90,18 @@ 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], &attr, run_vcpu, vcpu);
+		check_create_thread(&thread, &attr, run_vcpu, vcpu);
 
-		for (j = 0; j < SLEEPING_THREAD_NUM; ++j) {
-			check_create_thread(&throw_away, &attr, sleeping_thread,
+		for (j = 0; j < SLEEPING_THREAD_NUM; ++j)
+			check_create_thread(&thread, &attr, sleeping_thread,
 					    (void *)NULL);
-		}
 	}
 	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)
+		pause();
 }
 
 void wait_for_child_setup(pid_t pid)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
author maintainer
#11 lore raw

PATCH v2 8/12 KVM: selftests: Add KVM syscall wrapper for pthread_create()

In reply to Sean Christopherson

Add 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 +++-------
 .../selftests/kvm/arm64/vgic_lpi_stress.c        |  4 ++--
 tools/testing/selftests/kvm/dirty_log_test.c     |  2 +-
 .../selftests/kvm/hardware_disable_test.c        | 16 +++-------------
 .../testing/selftests/kvm/include/kvm_syscalls.h |  5 +++++
 tools/testing/selftests/kvm/irq_test.c           |  2 +-
 tools/testing/selftests/kvm/irqfd_test.c         |  2 +-
 .../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 +-
 .../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 +-
 .../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 ++------
 .../testing/selftests/kvm/x86/xen_shinfo_test.c  |  3 +--
 28 files changed, 50 insertions(+), 77 deletions(-)
tools/testing/selftests/kvm/arch_timer.c +4 -10
index f8b02597897b..a6a66b5b7ea9 100644
@@ -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
index 5e231998617e..2d6f20d5c117 100644
@@ -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
index d64d434d3f06..549e0547d905 100644
@@ -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
index e8419d7da1ea..ca22e5f09ac2 100644
@@ -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 -13
index b8115b2dcae3..694d064e63f1 100644
@@ -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"
 
@@ -62,15 +62,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 void run_test(u32 run)
 {
 	struct kvm_vcpu *vcpu;
@@ -90,11 +81,10 @@ 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, &attr, run_vcpu, vcpu);
+		kvm_pthread_create(&thread, &attr, run_vcpu, vcpu);
 
 		for (j = 0; j < SLEEPING_THREAD_NUM; ++j)
-			check_create_thread(&thread, &attr, sleeping_thread,
-					    (void *)NULL);
+			kvm_pthread_create(&thread, &attr, sleeping_thread, (void *)NULL);
 	}
 	pr_debug("%s: [%d] all threads launched\n", __func__, run);
 	sem_post(sem);
tools/testing/selftests/kvm/include/kvm_syscalls.h +5 -0
index 01dca99009c2..e75be8378779 100644
@@ -12,6 +12,7 @@
 #include <sys/mman.h>
 #include <sys/syscall.h>
 
+#include <pthread.h>
 #include <sched.h>
 #include <test_util.h>
 
@@ -97,6 +98,10 @@ __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(sched_setaffinity, 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
index 240f6f0fdbe4..7da5f8d75e85 100644
@@ -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
index 5d7590d01868..8b39144a2834 100644
@@ -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
index a910e3abb8c7..8222bdb63e49 100644
@@ -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
index 3599b75d97c9..0bfa623ce9b5 100644
@@ -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
index ef8d76f71f83..4b3e3158ed87 100644
@@ -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
index 4d9ad6104a6e..eb49305db404 100644
@@ -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
index 3d5f33a63b2b..9448498849ff 100644
@@ -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
index a0fcae3cb7a8..88287ccb9093 100644
@@ -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
index edcaf0fbe20c..0921b8d90d4a 100644
@@ -239,8 +239,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
index 0244848621b3..96b32bd74009 100644
@@ -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
index a152ab65c657..12f5022bb164 100644
@@ -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
index 0b88c743ee7d..f5f708be5aa0 100644
@@ -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
index 771535f9aad3..3cf451f3153e 100644
@@ -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
index b4be9a175379..87fac1968a47 100644
@@ -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
index 1d2f5d4fd45d..e419b08f1fa6 100644
@@ -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
index 10db9fe6d906..e26524fb4b50 100644
@@ -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
index cbc92a862ea9..13abb65620d5 100644
@@ -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
index d59abb198d86..66c7ce49620a 100644
@@ -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
index 5b0c2359bbb4..f7228af7d594 100644
@@ -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
index 59da8d4da607..54b4b8d06d3b 100644
@@ -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
index 3a326c5e74ca..271ec09c629e 100644
@@ -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, &params[0]);
-	TEST_ASSERT(r == 0,
-		    "pthread_create halter failed errno=%d", errno);
+	kvm_pthread_create(&threads[0], NULL, vcpu_thread, &params[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, &params[1]);
-	TEST_ASSERT(r == 0, "pthread_create sender failed errno=%d", errno);
+	kvm_pthread_create(&threads[1], NULL, vcpu_thread, &params[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
index 5076f6a75455..20f518fe2450 100644
@@ -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
#12 lore raw

Re: [PATCH v2 08/12] 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] Using `__KVM_SYSCALL_DEFINE` to wrap `pthread_create` results in misleading error messages on…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Using __KVM_SYSCALL_DEFINE to wrap pthread_create results in misleading error messages on failure, as it logs errno instead of the returned error code. --

--- Patch [8]: [PATCH v2 08/12] KVM: selftests: Add KVM syscall wrapper for pthread_create() --- commit dca849bcf7bfa248030e1f22bebefccde9e01db3
Author: Sean Christopherson <[email protected]>

KVM: selftests: Add KVM syscall wrapper for pthread_create()

This commit adds a wrapper for the pthread_create() syscall to the KVM selftests. It replaces manual error checking across the selftests with the new wrapper which automatically asserts success.

[ ... ]

Quoted 4 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +0 -0
index 01dca99009c2f..e75be83787798 100644

[ ... ]

tools/testing/selftests/kvm/include/kvm_syscalls.h __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice); · line 104

Quoted 8 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +4 -0
@@ -97,6 +98,10 @@ __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(sched_setaffinity, 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 wrapper format the error message correctly on failure?

The __KVM_SYSCALL_DEFINE macro uses __KVM_SYSCALL_ERROR to format the error string, which reads errno and uses strerror(errno).

However, POSIX specifies that pthread_create returns the error code directly and does not set errno.

If pthread_create fails, wouldn't this print a stale errno, or print "Success" if errno happened to be zero, rather than reporting the actual returned error code?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author maintainer
#13 lore raw

PATCH v2 9/12 KVM: selftests: Add KVM syscall wrappers for pthread_{cancel,join}()

In reply to Sean Christopherson

Add 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
index a6a66b5b7ea9..03160ad666f5 100644
@@ -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
index 2d6f20d5c117..3cb85932ee85 100644
@@ -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
index 549e0547d905..9a69c439a0f8 100644
@@ -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
index ca22e5f09ac2..af5eb0334a74 100644
@@ -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
index e75be8378779..d6c18e13b9c8 100644
@@ -101,6 +101,8 @@ __KVM_SYSCALL_DEFINE(sched_setaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_s
 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);
+__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
index 7da5f8d75e85..168c52734fef 100644
@@ -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
index 8b39144a2834..ebb2478878de 100644
@@ -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
index 8222bdb63e49..974813ea8953 100644
@@ -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
index 0bfa623ce9b5..2864562f57b4 100644
@@ -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
index 4b3e3158ed87..f7ce5a6ddcc2 100644
@@ -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
index eb49305db404..e1299611be32 100644
@@ -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
index 9448498849ff..741e343962ed 100644
@@ -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
index 88287ccb9093..c57631aab3d3 100644
@@ -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
index 0921b8d90d4a..b386aece68fd 100644
@@ -312,7 +312,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
index 96b32bd74009..ce8db96ccf7e 100644
@@ -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
index 12f5022bb164..dffe9654f45b 100644
@@ -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
index f5f708be5aa0..6cb1eb4c40c9 100644
@@ -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
index 3cf451f3153e..d2efb1043534 100644
@@ -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
index 87fac1968a47..e4fc9bb6f329 100644
@@ -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
index e419b08f1fa6..3aa0673f79f3 100644
@@ -418,7 +418,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, u32 nr_v
 	WRITE_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
index e26524fb4b50..e6d16e9d6120 100644
@@ -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
index 13abb65620d5..e3e397f32bff 100644
@@ -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
index 66c7ce49620a..de006e1c861c 100644
@@ -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
index f7228af7d594..99a5bbeffc5d 100644
@@ -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
index 54b4b8d06d3b..2a3a9bd8b338 100644
@@ -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
index 271ec09c629e..33e78b284c67 100644
@@ -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
index 20f518fe2450..3ec5e67b21ef 100644
@@ -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
#14 lore raw

Re: [PATCH v2 09/12] KVM: selftests: Add KVM syscall wrappers for pthread_{cancel,join}()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `kvm_pthread_join` and `kvm_pthread_cancel` wrappers are defined using the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The kvm_pthread_join and kvm_pthread_cancel wrappers are defined using the __KVM_SYSCALL_DEFINE macro, which incorrectly assumes the wrapped function sets the thread-local errno variable on failure. --

--- Patch [9]: [PATCH v2 09/12] KVM: selftests: Add KVM syscall wrappers for pthread_{cancel,join}() --- commit b9978e67ef4ca95fc0d3b87b0d80bdb48e623a21
Author: Sean Christopherson <[email protected]>

KVM: selftests: Add KVM syscall wrappers for pthread_{cancel,join}()

This commit adds kvm_pthread_join() and kvm_pthread_cancel() wrappers to KVM selftests. These wrappers automatically assert that the underlying pthread functions succeed, removing the need for manual error checking across multiple tests.

tools/testing/selftests/kvm/include/kvm_syscalls.h __KVM_SYSCALL_DEFINE(sched_setaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_s · line 105

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_syscalls.h +2 -0
index e75be83787798..d6c18e13b9c8b 100644
@@ -101,6 +101,8 @@ __KVM_SYSCALL_DEFINE(sched_setaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_s
 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);
+__KVM_SYSCALL_DEFINE(pthread_join, 2, pthread_t, thread, void **, thread_return);
+__KVM_SYSCALL_DEFINE(pthread_cancel, 1, pthread_t, thread);

[Severity: Medium]
Will this produce misleading error messages on test failures?

Pthread functions like pthread_join and pthread_cancel return their error codes directly and do not set the thread-local errno variable.

Since these wrappers use the __KVM_SYSCALL_DEFINE macro, if the underlying pthread function fails, the __KVM_SYSCALL_ERROR macro will format the error message using the stale errno instead of the returned error code. This could result in test assertions printing an incorrect errno (e.g., "Success") rather than the actual error string.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author maintainer
#15 lore raw

PATCH v2 10/12 KVM: selftests: Add helper APIs to cancel+join pthreads

In reply to Sean Christopherson

Deduplicate 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
index d6c18e13b9c8..c0657e54aee5 100644
@@ -104,6 +104,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_pthread_cancel_join(pthread_t thread, void **r)
+{
+	kvm_pthread_cancel(thread);
+	kvm_pthread_join(thread, r);
+}
+
+static inline void kvm_pthread_cancel_join(pthread_t thread)
+{
+	__kvm_pthread_cancel_join(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_pthread_cancel_join_async(pthread_t thread)
+{
+	void *r;
+
+	__kvm_pthread_cancel_join(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
index d2efb1043534..a2fc0bca8011 100644
@@ -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_pthread_cancel_join_async(threads[0]);
+	kvm_pthread_cancel_join_async(threads[1]);
 	kvm_vm_free(vm);
 
 	return 0;
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +2 -13
index e4fc9bb6f329..dc9d40830a7f 100644
@@ -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_pthread_cancel_join_async(threads[0]);
+	kvm_pthread_cancel_join_async(threads[1]);
 	kvm_vm_free(vm);
 
 	return 0;
tools/testing/selftests/kvm/x86/recalc_apic_map_test.c +1 -2
index e3e397f32bff..6252a96320ee 100644
@@ -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_pthread_cancel_join(thread);
 
 	kvm_vm_free(vm);
 
tools/testing/selftests/kvm/x86/sync_regs_test.c +1 -2
index 99a5bbeffc5d..ed0c21b427c9 100644
@@ -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_pthread_cancel_join(thread);
 
 	kvm_x86_state_cleanup(state);
 }
tools/testing/selftests/kvm/x86/xapic_ipi_test.c +2 -13
index 33e78b284c67..469e3ab16460 100644
@@ -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_pthread_cancel_join_async(threads[0]);
+	kvm_pthread_cancel_join_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
index 3ec5e67b21ef..aaa414c677ad 100644
@@ -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_pthread_cancel_join(thread);
 				goto done;
 
 			case TEST_GUEST_SAW_IRQ:
Signature · 1 line
2.55.0.508.g3f0d502094-goog
author maintainer
#16 lore raw

PATCH v2 11/12 KVM: selftests: Add KVM syscall wrappers for pthread_{g,s}etaffinity_np()

In reply to Sean Christopherson

Add 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.

---
 .../testing/selftests/kvm/include/kvm_syscalls.h | 16 ++++++++++++++++
 tools/testing/selftests/kvm/steal_time.c         |  2 +-
 2 files changed, 17 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/kvm_syscalls.h +16 -0
index c0657e54aee5..dc7461894aef 100644
@@ -98,6 +98,22 @@ __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(sched_setaffinity, 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
index 6cb1eb4c40c9..bc3c62b72c58 100644
@@ -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
author maintainer
#17 lore raw

PATCH v2 12/12 KVM: selftests: Clean up global constants in hardware disable test

In reply to Sean Christopherson

Rename 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.

Opportunistically tag the global semaphore with "static".

No functional change intended.

---
 .../selftests/kvm/hardware_disable_test.c     | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/hardware_disable_test.c +10 -10
index 694d064e63f1..cf9306bbe970 100644
@@ -17,13 +17,13 @@
 #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		2000
 
 static cpu_set_t threads_cpu_set;
-sem_t *sem;
+static sem_t *sem;
 
 static void guest_code(void)
 {
@@ -75,15 +75,15 @@ static void run_test(u32 run)
 	pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &threads_cpu_set);
 #endif
 
-	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, &attr, run_vcpu, vcpu);
 
-		for (j = 0; j < SLEEPING_THREAD_NUM; ++j)
+		for (j = 0; j < NR_SLEEPERS_PER_VCPU; ++j)
 			kvm_pthread_create(&thread, &attr, sleeping_thread, (void *)NULL);
 	}
 	pr_debug("%s: [%d] all threads launched\n", __func__, run);
@@ -133,13 +133,13 @@ int main(int argc, char **argv)
 
 	kvm_sched_getaffinity(0, sizeof(cpu_set_t), &allowed_cpu_set);
 
-	for (i = 0; i < VCPU_NUM && CPU_COUNT(&allowed_cpu_set); i++)
+	for (i = 0; i < NR_VCPUS && CPU_COUNT(&allowed_cpu_set); i++)
 		CPU_SET(kvm_pick_random_cpu(&allowed_cpu_set), &threads_cpu_set);
 
 	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