All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
@ 2026-11-30 22:05 Mika Tammi
  2026-11-30 22:05 ` [PATCH kvmtool v2 2/2] Remove kvm_create_vcpu_thread() Mika Tammi
  2026-12-01 16:21 ` [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility) Alexandru Elisei
  0 siblings, 2 replies; 4+ messages in thread
From: Mika Tammi @ 2026-11-30 22:05 UTC (permalink / raw)
  To: Will Deacon, Julien Thierry; +Cc: kvm, Alexandru Elisei, Mika Tammi

Commit 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU
threads") creates the VCPU threads with pthread_attr_setaffinity_np().
That is a glibc extension, which musl doesn't have, so kvmtool no longer
builds with musl:

  util/util.c:221:23: error: implicit declaration of function 'pthread_attr_setaffinity_np'; did you mean 'pthread_setaffinity_np'? [-Wimplicit-function-declaration]

musl can only set the affinity of a thread that already exists. If the
thread that creates a VCPU thread did that, the VCPU could run before
its affinity is in place, and on arm64 KVM_RUN fails on a CPU that the
VCPU's PMU doesn't support. So have each VCPU thread set its own
affinity as the first thing it does, with sched_setaffinity(), which
kvmtool also used before that commit. As that commit intended, only the
VCPU threads get the affinity.

Give sched_setaffinity() the size that the CPU set is allocated with,
CPU_ALLOC_SIZE(NR_CPUS). The size passed until now, sizeof(cpu_set_t),
covers only CPUs 0-1023, and is larger than the set on riscv and mips.

Fixes: 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU threads")
Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Mika Tammi <mikatammi@gmail.com>
---
v2:
- Give sched_setaffinity() CPU_ALLOC_SIZE(NR_CPUS), the size of the CPU
  set, instead of sizeof(cpu_set_t) (Alexandru)
- Patch 2 is new: it removes kvm_create_vcpu_thread() (Alexandru)

v1: https://lore.kernel.org/kvm/20261128061232.64870-1-mikatammi@gmail.com/

Tested with nixpkgs' kvmtool package at kvmtool master (4b4aceb1dae7).
The x86_64 builds with musl, dynamic and static, fail as above and
build with this patch, as do x86_64 and arm64 with glibc, arm64 with
static musl, and cross builds for riscv32, riscv64, powerpc64,
powerpc64le and mips64el. On 8-CPU x86_64 and arm64 hosts with
-c 2 --vcpu-affinity 2-3, the VCPU threads may run only on CPUs 2-3 and
the other threads that lkvm creates on all eight, with glibc and musl
alike and as with master's glibc build, and the guest boots. A static
musl lkvm on QEMU rv32 and rv64 KVM hosts boots a guest with
--vcpu-affinity 0.

With CPUs that can't be used, each VCPU thread now fails with
"sched_setaffinity: Invalid argument", where pthread_create() failed
before.

 builtin-run.c | 14 ++++++++++++++
 util/util.c   | 19 +------------------
 2 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/builtin-run.c b/builtin-run.c
index f06231e..ac779a4 100644
--- a/builtin-run.c
+++ b/builtin-run.c
@@ -323,10 +323,24 @@ out_free:
 
 static void *kvm_cpu_thread(void *arg)
 {
+	cpu_set_t *affinity;
 	char name[16];
+	int ret;
 
 	current_kvm_cpu = arg;
 
+	/*
+	 * The thread sets its own affinity, before it runs the VCPU.
+	 * pthread_attr_setaffinity_np() could create the thread with it,
+	 * but is a glibc extension, which musl doesn't have.
+	 */
+	affinity = current_kvm_cpu->kvm->vcpu_affinity;
+	if (affinity) {
+		ret = sched_setaffinity(0, CPU_ALLOC_SIZE(NR_CPUS), affinity);
+		if (ret == -1)
+			die_perror("sched_setaffinity");
+	}
+
 	sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id);
 	kvm__set_thread_name(name);
 
diff --git a/util/util.c b/util/util.c
index c4ee033..59855d6 100644
--- a/util/util.c
+++ b/util/util.c
@@ -210,29 +210,12 @@ void *mmap_guest_memfd(struct kvm *kvm, u64 size)
 int kvm_create_vcpu_thread(struct kvm *kvm, pthread_t *thread,
 			   void *(*start_routine)(void *), void *arg)
 {
-	pthread_attr_t attr;
-	cpu_set_t *affinity;
 	int ret;
 
-	pthread_attr_init(&attr);
-
-	affinity = kvm->vcpu_affinity;
-	if (affinity) {
-		ret = pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), affinity);
-		if (ret) {
-			/* Set errno so the caller can call die_perror(). */
-			errno = ret;
-			/* kvmtool treats a negative return value as an error. */
-			return -ret;
-		}
-	}
-
-	ret = pthread_create(thread, &attr, start_routine, arg);
+	ret = pthread_create(thread, NULL, start_routine, arg);
 	if (ret)
 		errno = ret;
 
-	pthread_attr_destroy(&attr);
-
 	return -ret;
 }
 
-- 
2.54.0 (Apple Git-157)


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

end of thread, other threads:[~2026-12-01 16:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 22:05 [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility) Mika Tammi
2026-11-30 22:05 ` [PATCH kvmtool v2 2/2] Remove kvm_create_vcpu_thread() Mika Tammi
2026-12-01 16:21   ` Alexandru Elisei
2026-12-01 16:21 ` [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility) Alexandru Elisei

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.