From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Mika Tammi <mikatammi@gmail.com>
Cc: Will Deacon <will@kernel.org>,
Julien Thierry <julien.thierry.kdev@gmail.com>,
kvm@vger.kernel.org
Subject: Re: [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
Date: Tue, 1 Dec 2026 16:21:38 +0000 [thread overview]
Message-ID: <aw70EiGL3-GC_SyH@raptor> (raw)
In-Reply-To: <20261130220543.70217-1-mikatammi@gmail.com>
Hi Mika,
On Mon, Nov 30, 2026 at 11:05:42PM +0100, Mika Tammi wrote:
> 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>
Looks correct to me:
Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>
Tested on an arm64 machine, with the host kernel compiled with CONFIG_NR_CPUS=16
and CONFIG_NR_CPUS=4096, checked /proc/<kvmtool pid>/task/<task id>/status and
the VCPUs (and the virtio threads spawned from the VCPU threads) run on the
physical CPUs specified by --vcpu-affinity, while the other threads run on the
physical CPUs specified with taskset:
Tested-by: Alexandru Elisei <alexandru.elisei@arm.com>
Thanks,
Alex
> ---
> 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)
>
prev parent reply other threads:[~2026-12-01 16:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Alexandru Elisei [this message]
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=aw70EiGL3-GC_SyH@raptor \
--to=alexandru.elisei@arm.com \
--cc=julien.thierry.kdev@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=mikatammi@gmail.com \
--cc=will@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox