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] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
Date: Mon, 30 Nov 2026 13:57:44 +0000 [thread overview]
Message-ID: <aw2A2LFXowPvS4yV@raptor> (raw)
In-Reply-To: <20261128061232.64870-1-mikatammi@gmail.com>
Hi Mika,
Sorry for this, didn't realize that the 'np' in pthread_attr_setaffinity_np
stands for 'nonportable'.
On Sat, Nov 28, 2026 at 07:12:32AM +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(), 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 <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 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.
To be honest, now that kvm_create_vcpu_thread() doesn't set the affinity,
it can be removed entirely and kvm_cmd_run_work() reverted to calling
pthread_create() directly, as before commit 4c90ccf4c593. I don't feel
strongly about removing it or leaving it as it is.
>
> 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);
That's not the size of kvm->vcpu_affinity though:
kvm->vcpu_affinity = CPU_ALLOC(NR_CPUS);
From man 2 sched_setaffinity:
'The underlying system calls (which represent CPU masks as bit masks of type
unsigned long *) impose no restriction on the size of the CPU mask. However,
the cpu_set_t data type used by glibc has a fixed size of 128 bytes, meaning
that the maximum CPU number that can be represented is 1023. If the kernel CPU
affinity mask is larger than 1024, then calls of the form:
sched_getaffinity(pid, sizeof(cpu_set_t), &mask);
fail with the error EINVAL, the error produced by the underlying system call for
the case where the mask size specified in cpusetsize is smaller than the size of
the affinity mask used by the kernel.'
Grepping for NR_CPUS results in:
$ ag '^#define NR_CPUS'
powerpc/include/asm/kernel.h
6:#define NR_CPUS 2048
x86/include/asm/kernel.h
6:#define NR_CPUS 8196
arm64/include/asm/kernel.h
6:#define NR_CPUS 4096
riscv/include/asm/kernel.h
6:#define NR_CPUS 512
mips/include/asm/kernel.h
6:#define NR_CPUS 256
which represents the maximum host kernel value for CONFIG_NR_CPUS (checked
for arm64 and x86).
The man 2 sched_setaffinity also says this:
'On success, the raw sched_getaffinity() system call returns the number
of bytes placed copied into the mask buffer; this will be the minimum of
cpusetsize and the size (in bytes) of the cpumask_t data type that is used
internally by the kernel to represent the CPU set bit mask.'
and since in my own tests, on a kernel with CONFIG_NR_CPUS=512, when using
cpusetsize=128 (1024 CPUs) no error happened, that suggests to me that a
cpusetsize value larger than what the kernel implements is not considered
an error.
Since the --vcpu-affinity parser allows values up to NR_CPUS - 1, can you
check that using cpusetsize=CPU_ALLOC_SIZE(NR_CPUS) works as intended? For
musl too?
Thanks,
Alex
> + 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)
>
next prev parent reply other threads:[~2026-11-30 13:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 6:12 [PATCH kvmtool] Set the VCPU affinity with sched_setaffinity() (for musl compatibility) Mika Tammi
2026-11-30 13:57 ` Alexandru Elisei [this message]
2026-11-30 22:01 ` Mika Tammi
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=aw2A2LFXowPvS4yV@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 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.