From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047070; b=moM/qhd6DriZy7M1SVajt3fDDbE+0KsxAUfTe/mqhj4IWXPZwO5JI5zgOzramJGjCM9I GmuPev28oFKrb4GZLRdwonIuvBsM+mnTuR7yHsnb4VV5i3dRwNRR4IT01Av0Vd1MWaej4 6mLqeR9rj2fHtFwESbfC8BRXcOp//LGVLOa1bC6mDCcZpVL00Grz5WHOst1Or+XJ6ulj0 sG2J7305UytiM76Fb8TSFLD9o+/iGN5JYdpghLp7vzpHmTCyd9MG/MTjkHJvvLeEV3RbA 0c19JKcj/1lfmhHWv+25sYdU0zpCYuUa/O0/f5mdY9JSDdlziQ6tcYEWuiQoWfwXUKA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047070; h=DMARC-Filter:Received:Received:DKIM-Signature:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=Z7erEBDBuIuuSKcGOCtHnbHI4E17iKy+bjP4Apwwg7Y=; b=tza+xDk0O+dsBvkaSB8RQy3PbXBDhJf4oSlVhNK9i7JrrsoIlxlqk3tjk0ZTVTj3Bn3M SsY7wCwMBp6c4MSBbhku+b/T85b6MPRPE0EtE2TbxSjY+GUiVfaRuJJhtQUPScmLgSfG9 K034syyjKCwRSeblhexaGe5kJUKZCWJLAV9uRX648xfKFBun/R+Hi4cAERW8Q/5OR1yg/ I96vj3JhsnGg+GEbol3x1bKZx4te8D5dXVcpHL65nmM75Y84ExAP+hii21NTMn0vAHYMJ JTIlGG+55/tZOaR/qsDhaTfAYVXqgTLWO/35iKWJR1jxqHqyw9v2C18gHB3AyPwgHwQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Db0IjGTc; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B9493606A33B for ; Mon, 30 Nov 2026 13:57:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B9493606A33B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Db0IjGTc Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id ECE961575; Mon, 30 Nov 2026 05:57:44 -0800 (PST) Received: from raptor (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0E2853F86F; Mon, 30 Nov 2026 05:57:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796047067; bh=F2cGHLoq2fIEUT5j/ucsbmN7HcGRoVrcMTNdm42RsME=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Db0IjGTcr8X90h7GeylfJqLagDhohppQwCLzHjh4CHkDL0Xq3iGVwNDWb/1noNNKO ebJuiZ+cCA/jojSgmiEvLmv1kURCYGeKRcfEYVquMyZUftjxtRlhc+hSVJRZmrDbVG hv8HqkYSGpNiyDa4GaTcZh7HNBEQn+GWmGpOSQAs= Date: Mon, 30 Nov 2026 13:57:44 +0000 From: Alexandru Elisei To: Mika Tammi Cc: Will Deacon , Julien Thierry , kvm@vger.kernel.org Subject: Re: [PATCH kvmtool] Set the VCPU affinity with sched_setaffinity() (for musl compatibility) Message-ID: References: <20261128061232.64870-1-mikatammi@gmail.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > --- > 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) >