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

* [PATCH kvmtool v2 2/2] Remove kvm_create_vcpu_thread()
  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 ` 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
  1 sibling, 1 reply; 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

The VCPU threads now set their own affinity, so
kvm_create_vcpu_thread() only wraps pthread_create(). Remove it, and
have kvm_cmd_run_work() call pthread_create() itself, as it did before
commit 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU
threads"). pthread_create() doesn't set errno, so a failure is reported
with die() again, instead of die_perror().

Suggested-by: Alexandru Elisei <alexandru.elisei@arm.com>
Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Mika Tammi <mikatammi@gmail.com>
---
New in v2. With both patches, lkvm builds for the same targets as with
patch 1 alone, and boots guests the same way: on x86_64 and arm64 with
and without --vcpu-affinity, with glibc and musl, and on the QEMU rv32
and rv64 KVM hosts with --vcpu-affinity 0.

 builtin-run.c      |  4 ++--
 include/kvm/util.h |  4 ----
 util/util.c        | 12 ------------
 3 files changed, 2 insertions(+), 18 deletions(-)

diff --git a/builtin-run.c b/builtin-run.c
index ac779a4..264bf8f 100644
--- a/builtin-run.c
+++ b/builtin-run.c
@@ -876,8 +876,8 @@ static int kvm_cmd_run_work(struct kvm *kvm)
 	int i;
 
 	for (i = 0; i < kvm->nrcpus; i++) {
-		if (kvm_create_vcpu_thread(kvm, &kvm->cpus[i]->thread, kvm_cpu_thread, kvm->cpus[i]))
-			die_perror("unable to create KVM VCPU thread");
+		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
+			die("unable to create KVM VCPU thread");
 	}
 
 	/* Only VCPU #0 is going to exit by itself when shutting down */
diff --git a/include/kvm/util.h b/include/kvm/util.h
index dc09720..b767b77 100644
--- a/include/kvm/util.h
+++ b/include/kvm/util.h
@@ -19,7 +19,6 @@
 #include <signal.h>
 #include <errno.h>
 #include <limits.h>
-#include <pthread.h>
 #include <sys/param.h>
 #include <sys/types.h>
 #include <linux/types.h>
@@ -151,9 +150,6 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
 void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
 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);
-
 unsigned long host_page_size(void);
 
 #ifndef PAGE_SIZE
diff --git a/util/util.c b/util/util.c
index 59855d6..9baef5d 100644
--- a/util/util.c
+++ b/util/util.c
@@ -207,18 +207,6 @@ void *mmap_guest_memfd(struct kvm *kvm, u64 size)
 	return addr;
 }
 
-int kvm_create_vcpu_thread(struct kvm *kvm, pthread_t *thread,
-			   void *(*start_routine)(void *), void *arg)
-{
-	int ret;
-
-	ret = pthread_create(thread, NULL, start_routine, arg);
-	if (ret)
-		errno = ret;
-
-	return -ret;
-}
-
 unsigned long host_page_size(void)
 {
 	static long page_size;
-- 
2.54.0 (Apple Git-157)


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

* Re: [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility)
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Alexandru Elisei @ 2026-12-01 16:21 UTC (permalink / raw)
  To: Mika Tammi; +Cc: Will Deacon, Julien Thierry, kvm

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)
> 

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

* Re: [PATCH kvmtool v2 2/2] Remove kvm_create_vcpu_thread()
  2026-11-30 22:05 ` [PATCH kvmtool v2 2/2] Remove kvm_create_vcpu_thread() Mika Tammi
@ 2026-12-01 16:21   ` Alexandru Elisei
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandru Elisei @ 2026-12-01 16:21 UTC (permalink / raw)
  To: Mika Tammi; +Cc: Will Deacon, Julien Thierry, kvm

Hi Mika,

On Mon, Nov 30, 2026 at 11:05:43PM +0100, Mika Tammi wrote:
> The VCPU threads now set their own affinity, so
> kvm_create_vcpu_thread() only wraps pthread_create(). Remove it, and
> have kvm_cmd_run_work() call pthread_create() itself, as it did before
> commit 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU
> threads"). pthread_create() doesn't set errno, so a failure is reported
> with die() again, instead of die_perror().
> 
> Suggested-by: Alexandru Elisei <alexandru.elisei@arm.com>
> 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>

Also tested on an arm64 machine with CONFIG_NR_CPUS=16, checked that the VCPU
threads are scheduled on the --vcpu-affinity cpulist:
Tested-by: Alexandru Elisei <alexandru.elisei@arm.com>

> ---
> New in v2. With both patches, lkvm builds for the same targets as with
> patch 1 alone, and boots guests the same way: on x86_64 and arm64 with
> and without --vcpu-affinity, with glibc and musl, and on the QEMU rv32
> and rv64 KVM hosts with --vcpu-affinity 0.
> 
>  builtin-run.c      |  4 ++--
>  include/kvm/util.h |  4 ----
>  util/util.c        | 12 ------------
>  3 files changed, 2 insertions(+), 18 deletions(-)
> 
> diff --git a/builtin-run.c b/builtin-run.c
> index ac779a4..264bf8f 100644
> --- a/builtin-run.c
> +++ b/builtin-run.c
> @@ -876,8 +876,8 @@ static int kvm_cmd_run_work(struct kvm *kvm)
>  	int i;
>  
>  	for (i = 0; i < kvm->nrcpus; i++) {
> -		if (kvm_create_vcpu_thread(kvm, &kvm->cpus[i]->thread, kvm_cpu_thread, kvm->cpus[i]))
> -			die_perror("unable to create KVM VCPU thread");
> +		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
> +			die("unable to create KVM VCPU thread");
>  	}
>  
>  	/* Only VCPU #0 is going to exit by itself when shutting down */
> diff --git a/include/kvm/util.h b/include/kvm/util.h
> index dc09720..b767b77 100644
> --- a/include/kvm/util.h
> +++ b/include/kvm/util.h
> @@ -19,7 +19,6 @@
>  #include <signal.h>
>  #include <errno.h>
>  #include <limits.h>
> -#include <pthread.h>
>  #include <sys/param.h>
>  #include <sys/types.h>
>  #include <linux/types.h>
> @@ -151,9 +150,6 @@ void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size);
>  void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size);
>  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);
> -
>  unsigned long host_page_size(void);
>  
>  #ifndef PAGE_SIZE
> diff --git a/util/util.c b/util/util.c
> index 59855d6..9baef5d 100644
> --- a/util/util.c
> +++ b/util/util.c
> @@ -207,18 +207,6 @@ void *mmap_guest_memfd(struct kvm *kvm, u64 size)
>  	return addr;
>  }
>  
> -int kvm_create_vcpu_thread(struct kvm *kvm, pthread_t *thread,
> -			   void *(*start_routine)(void *), void *arg)
> -{
> -	int ret;
> -
> -	ret = pthread_create(thread, NULL, start_routine, arg);
> -	if (ret)
> -		errno = ret;
> -
> -	return -ret;
> -}
> -
>  unsigned long host_page_size(void)
>  {
>  	static long page_size;
> -- 
> 2.54.0 (Apple Git-157)
> 

^ permalink raw reply	[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.