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(), as kvmtool called it before that commit. As that commit intended, only the VCPU threads get the affinity. Fixes: 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU threads") Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Mika Tammi --- 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 with glibc and cross builds for arm64, riscv32 and riscv64. On an 8-CPU x86_64 host 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. For arm64 I could only compile it. With CPUs that can't be used, each VCPU thread now fails with "sched_setaffinity: Invalid argument", where pthread_create() failed before. kvm_create_vcpu_thread() no longer uses its kvm argument. I left it alone to keep this fix small, and can send a cleanup on top if you would like one. 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..e366ce9 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, sizeof(cpu_set_t), 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)