From: Sean Christopherson <seanjc@google.com>
To: Aqib Faruqui <aqibaf@amazon.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Shuah Khan <shuah@kernel.org>,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, nh-open-source@amazon.com
Subject: Re: [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback
Date: Fri, 29 Aug 2025 15:37:23 -0700 [thread overview]
Message-ID: <aLIroyJSWQ26wnY4@google.com> (raw)
In-Reply-To: <20250829142556.72577-4-aqibaf@amazon.com>
On Fri, Aug 29, 2025, Aqib Faruqui wrote:
> The pthread_attr_setaffinity_np function is a GNU extension that may not
> be available in non-glibc C libraries. Some KVM selftests use this
> function for CPU affinity control.
>
> Add a function declaration and weak stub implementation for non-glibc
> builds. This allows tests to build, with the affinity setting being a
> no-op and errno set for the caller when the actual function is not available.
Except this isn't a fallback, for all intents and purposes it silently breaks
the test. A "fallback" is generally something that provides roughly equivalent
functionality. This particular test will still pass because forced preemption
isn't strictly necessary, but this is still gross.
Luckily, KVM selftests already provides APIs to pin tasks, just use those and
the problem naturally goes away.
--
From: Sean Christopherson <seanjc@google.com>
Date: Fri, 29 Aug 2025 15:31:44 -0700
Subject: [PATCH] KVM: selftests: Use KVM's task pinning APIs in steal_time
time
Use pin_self_to_cpu() and pin_task_to_cpu() to pin the vCPU thread and
the stealer thread to pCPU0 in the steal_time. Eliminating the usage of
pthread_attr_setaffinity_np() in particular allows building the test
again non-glibc C libraries.
Reported-by: Aqib Faruqui <aqibaf@amazon.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/steal_time.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c
index cce2520af720..663c99c81703 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -341,9 +341,7 @@ int main(int ac, char **av)
{
struct kvm_vcpu *vcpus[NR_VCPUS];
struct kvm_vm *vm;
- pthread_attr_t attr;
pthread_t thread;
- cpu_set_t cpuset;
unsigned int gpages;
long stolen_time;
long run_delay;
@@ -353,11 +351,7 @@ int main(int ac, char **av)
verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10));
/* Set CPU affinity so we can force preemption of the VCPU */
- CPU_ZERO(&cpuset);
- CPU_SET(0, &cpuset);
- pthread_attr_init(&attr);
- pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
- pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+ pin_self_to_cpu(0);
/* Create a VM and an identity mapped memslot for the steal time structure */
vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
@@ -389,7 +383,9 @@ int main(int ac, char **av)
/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */
run_delay = get_run_delay();
- pthread_create(&thread, &attr, do_steal_time, NULL);
+ pthread_create(&thread, NULL, do_steal_time, NULL);
+ pin_task_to_cpu(thread, 0);
+
do
sched_yield();
while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS);
base-commit: ecbcc2461839e848970468b44db32282e5059925
--
next prev parent reply other threads:[~2025-08-29 22:37 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
2025-08-29 14:25 ` [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback Aqib Faruqui
2025-08-29 22:52 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 2/9] KVM: selftests: Add __packed attribute fallback Aqib Faruqui
2025-08-29 22:46 ` Sean Christopherson
2025-09-01 15:08 ` Faruqui, Aqib
2025-08-29 14:25 ` [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback Aqib Faruqui
2025-08-29 22:37 ` Sean Christopherson [this message]
2025-08-29 14:25 ` [PATCH 4/9] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
2025-08-29 14:25 ` [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86 Aqib Faruqui
2025-08-29 20:38 ` Sean Christopherson
[not found] ` <33701547-13AA-467D-AC41-A1A05963B1DD@amazon.com>
2025-09-05 8:39 ` Sean Christopherson
2025-09-05 13:59 ` Aqib Faruqui
2025-09-08 18:22 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 6/9] KVM: selftests: Add backtrace fallback Aqib Faruqui
2025-08-29 14:25 ` [PATCH 7/9] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
2025-08-29 14:25 ` [PATCH 8/9] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
2025-08-29 14:25 ` [PATCH 9/9] selftests: kselftest: Add ulong typedef for non-glibc compatibility Aqib Faruqui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aLIroyJSWQ26wnY4@google.com \
--to=seanjc@google.com \
--cc=aqibaf@amazon.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=nh-open-source@amazon.com \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.