* [PATCH kvmtool] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
@ 2026-11-28 6:12 Mika Tammi
2026-11-30 13:57 ` Alexandru Elisei
0 siblings, 1 reply; 3+ messages in thread
From: Mika Tammi @ 2026-11-28 6:12 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(), 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.
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)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH kvmtool] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
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
2026-11-30 22:01 ` Mika Tammi
0 siblings, 1 reply; 3+ messages in thread
From: Alexandru Elisei @ 2026-11-30 13:57 UTC (permalink / raw)
To: Mika Tammi; +Cc: Will Deacon, Julien Thierry, kvm
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)
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH kvmtool] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
2026-11-30 13:57 ` Alexandru Elisei
@ 2026-11-30 22:01 ` Mika Tammi
0 siblings, 0 replies; 3+ messages in thread
From: Mika Tammi @ 2026-11-30 22:01 UTC (permalink / raw)
To: Alexandru Elisei; +Cc: Will Deacon, Julien Thierry, kvm
Hi Alexandru,
Thanks for looking at this.
On Mon, Nov 30, 2026 at 01:57:44PM +0000, Alexandru Elisei wrote:
> 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.
I'll remove it in a second patch on top in v2, so that the fix itself
stays small.
> > + 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);
Right. I took sizeof(cpu_set_t) over from the sched_setaffinity() call
that was there before 4c90ccf4c593; the pthread_attr_setaffinity_np()
call has it too. v2 passes CPU_ALLOC_SIZE(NR_CPUS).
> 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?
It does, with glibc and with musl. As you thought, a cpusetsize larger
than the kernel's mask isn't an error for sched_setaffinity():
get_user_cpu_mask() in kernel/sched/syscalls.c cuts the length down to
cpumask_size(), so the kernel ignores the bits beyond its own mask. The
EINVAL that the man page describes is from sched_getaffinity(), when
the mask is too small. glibc (2.44) and musl (1.2.6) both hand
cpusetsize to the system call unchanged.
I ran lkvm with CPU_ALLOC_SIZE(NR_CPUS) on 8-CPU hosts running Linux
6.18: on x86_64 (1032 bytes) with glibc, musl and static musl, and on
arm64 (512 bytes) with glibc and static musl. All of them behave the
same:
-c 2 --vcpu-affinity 2-3 the VCPU threads may run on CPUs 2-3, the
other threads that lkvm creates on 0-7
--vcpu-affinity 3,2000 the VCPU threads may run on CPU 3
--vcpu-affinity 100, 2000 "sched_setaffinity: Invalid argument"
or NR_CPUS - 1
Master's glibc build gives the same CPUs, and "unable to create KVM
VCPU thread: Invalid argument" for the last three. A static musl lkvm
on QEMU rv32 and rv64 KVM hosts (64 bytes) boots a guest with
--vcpu-affinity 0.
I don't have a host with more than 1024 CPUs, so for the CPUs that
sizeof(cpu_set_t) left out I can only go by the kernel code.
Thanks,
Mika Tammi
> On 30. Nov 2026, at 14.57, Alexandru Elisei <alexandru.elisei@arm.com> wrote:
>
> 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)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 22:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-30 22:01 ` Mika Tammi
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.