mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds
@ 2026-07-07 15:07 Sean Christopherson
  2026-07-07 15:07 ` [PATCH v2 1/2] KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np() Sean Christopherson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sean Christopherson @ 2026-07-07 15:07 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, linux-kernel, Hisam Mehboob, Aqib Faruqui, Sean Christopherson

Remove unnecessary usage of pthread_attr_setaffinity_np() in the steal time
test so that it can build against non-glibc libraries, and opportunistically
expand the test coverage by pinning the test threads to a random pCPU instead
of hardcoding the pin to pCPU0.

v2:
 - Rely on CPU affinity inheritance. [Sashiko]
 - Add a patch to in to a random CPU instead of CPU0.

v1: https://lore.kernel.org/all/20260706163649.155548-1-hisamshar@gmail.com


Sean Christopherson (2):
  KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np()
  KVM: selftests: Randomize pCPU in steal time test

 tools/testing/selftests/kvm/steal_time.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)


base-commit: a204badd8432f93b7e862e7dac6db0fe3d65f370
-- 
2.55.0.rc2.803.g1fd1e6609c-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np()
  2026-07-07 15:07 [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson
@ 2026-07-07 15:07 ` Sean Christopherson
  2026-07-07 15:07 ` [PATCH v2 2/2] KVM: selftests: Randomize pCPU in steal time test Sean Christopherson
  2026-07-14 18:41 ` [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2026-07-07 15:07 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, linux-kernel, Hisam Mehboob, Aqib Faruqui, Sean Christopherson

In the steal time test, don't explicitly set the CPU affinity mask of the
worker child and instead rely on the child inheriting the affinity of the
main thread.  Per the pthread_create()[1] and pthread_setaffinity_np()[2]
documentation, new threads inherit the parent's affinity mask.

   Linux-specific details
       The new thread inherits copies of the calling thread's capability
       sets (see capabilities(7)) and CPU affinity mask (see
       sched_setaffinity(2)).

Out of an abundance of caution, assert that the child did indeed inherit
the CPU affinity mask, as the test will hang indefinitely if the system is
under light load.

Dropping use of pthread_attr_setaffinity_np() allows building the steal
time test against non-glibc C libraries that don't implement that GNU
extension.

Link: https://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html [1]
Link: https://man7.org/linux/man-pages/man3/pthread_create.3.html [1]
Cc: Hisam Mehboob <hisamshar@gmail.com>
Reported-by: Aqib Faruqui <aqibaf@amazon.com>
Closes: https://lore.kernel.org/all/20250829142556.72577-4-aqibaf@amazon.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 tools/testing/selftests/kvm/steal_time.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c
index 76fcdd1fd3cb..a244bf9f701f 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -508,7 +508,6 @@ int main(int ac, char **av)
 {
 	struct kvm_vcpu *vcpus[NR_VCPUS];
 	struct kvm_vm *vm;
-	pthread_attr_t attr;
 	pthread_t thread;
 	cpu_set_t cpuset;
 	unsigned int gpages;
@@ -522,8 +521,6 @@ int main(int ac, char **av)
 	/* Set CPU affinity so we can force preemption of the VCPU */
 	CPU_ZERO(&cpuset);
 	CPU_SET(0, &cpuset);
-	pthread_attr_init(&attr);
-	pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
 	pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
 
 	/* Create a VM and an identity mapped memslot for the steal time structure */
@@ -558,7 +555,11 @@ int main(int ac, char **av)
 
 		/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */
 		run_delay = get_run_delay();
-		pthread_create(&thread, &attr, do_steal_time, NULL);
+		pthread_create(&thread, NULL, do_steal_time, NULL);
+		pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
+		TEST_ASSERT(CPU_COUNT(&cpuset) == 1 && CPU_ISSET(0, &cpuset),
+			    "Worker failed to inherit parent's CPU affinity");
+
 		do
 			sched_yield();
 		while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS);
-- 
2.55.0.rc2.803.g1fd1e6609c-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] KVM: selftests: Randomize pCPU in steal time test
  2026-07-07 15:07 [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson
  2026-07-07 15:07 ` [PATCH v2 1/2] KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np() Sean Christopherson
@ 2026-07-07 15:07 ` Sean Christopherson
  2026-07-14 18:41 ` [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2026-07-07 15:07 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kvm, linux-kernel, Hisam Mehboob, Aqib Faruqui, Sean Christopherson

Pin the steal time test's tasks to a random pCPU in the system instead of
hardcoding the test to always run on pCPU0 as a cheap way of increasing
test coverage, and to do the "right thing" if the parent task of the test
doesn't have pCPU0 in its CPU affinity mask.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 tools/testing/selftests/kvm/steal_time.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c
index a244bf9f701f..92e7ffcd68b7 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -514,14 +514,12 @@ int main(int ac, char **av)
 	long stolen_time;
 	long run_delay;
 	bool verbose;
-	int i;
+	int i, cpu;
 
 	verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10));
 
 	/* Set CPU affinity so we can force preemption of the VCPU */
-	CPU_ZERO(&cpuset);
-	CPU_SET(0, &cpuset);
-	pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+	cpu = pin_self_to_any_cpu();
 
 	/* Create a VM and an identity mapped memslot for the steal time structure */
 	vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
@@ -557,7 +555,7 @@ int main(int ac, char **av)
 		run_delay = get_run_delay();
 		pthread_create(&thread, NULL, do_steal_time, NULL);
 		pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
-		TEST_ASSERT(CPU_COUNT(&cpuset) == 1 && CPU_ISSET(0, &cpuset),
+		TEST_ASSERT(CPU_COUNT(&cpuset) == 1 && CPU_ISSET(cpu, &cpuset),
 			    "Worker failed to inherit parent's CPU affinity");
 
 		do
-- 
2.55.0.rc2.803.g1fd1e6609c-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds
  2026-07-07 15:07 [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson
  2026-07-07 15:07 ` [PATCH v2 1/2] KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np() Sean Christopherson
  2026-07-07 15:07 ` [PATCH v2 2/2] KVM: selftests: Randomize pCPU in steal time test Sean Christopherson
@ 2026-07-14 18:41 ` Sean Christopherson
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2026-07-14 18:41 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Hisam Mehboob, Aqib Faruqui

On Tue, 07 Jul 2026 08:07:04 -0700, Sean Christopherson wrote:
> Remove unnecessary usage of pthread_attr_setaffinity_np() in the steal time
> test so that it can build against non-glibc libraries, and opportunistically
> expand the test coverage by pinning the test threads to a random pCPU instead
> of hardcoding the pin to pCPU0.
> 
> v2:
>  - Rely on CPU affinity inheritance. [Sashiko]
>  - Add a patch to in to a random CPU instead of CPU0.
> 
> [...]

Applied to kvm-x86 selftests, thanks!

[1/2] KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np()
      https://github.com/kvm-x86/linux/commit/3f339b70bb59
[2/2] KVM: selftests: Randomize pCPU in steal time test
      https://github.com/kvm-x86/linux/commit/2351e814e1e1

--
https://github.com/kvm-x86/linux/tree/next

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-14 19:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-07 15:07 [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson
2026-07-07 15:07 ` [PATCH v2 1/2] KVM: selftests: Drop superfluous use of pthread_attr_setaffinity_np() Sean Christopherson
2026-07-07 15:07 ` [PATCH v2 2/2] KVM: selftests: Randomize pCPU in steal time test Sean Christopherson
2026-07-14 18:41 ` [PATCH v2 0/2] KVM: selftest: Fix steal time w/ non-glibc builds Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®