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=1796142107; b=osoGYB7apkKlC/0rLCxl9X1+qG1kn1K27agOxQlmDwSZRpLLghBOQCalfzVeqkP0ZcC8 qPqnVoaG0qdXZpRCAg5zSqsuJEbWlUAxuTCZweoyHlzgdmUOpEw1HGOppaT9kkHy1CBWf NgH39Ec3N2R2t459olViChjNY5ZCEM9sz4NaVCvL/Rw19Ty+1uhcMG6KbRXebp5EQnbxI F6IQGWSrgHIZZ2t1+ys30O4uQyQxcOWfaD4bJ8y3MghNNqLLgToi/VR2XzvsgIzPxATNS vcO8zxvwa7L3f4HDeWdZB0BpMRGoQ1ENg7IL/OfwAL9vugl2jwuDBxfvma1Rc+71fXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796142107; 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=aX/BX2GxTDVEDyEM1H077V1uEPwm7u386G8RO5Q/FCE=; b=2BG5YnkBpcRWG/h9iQbH15rsVYkg+T6J8KuX5EeSSRHOw2s+VJyPAwhFIpljT/z0SoHE aEzOdtbhvaCAY03CngS1z6vQqUe5bkSFVz0hwFJTI+/LuNseAQJOxOTyYFITmRwJS6i+9 njmztnmClwwMltPb470anT3yvcqC/eg4xXAX3OpfdD387UX15NByTlD6jHocfZKmWdURm EEXaSXa8RpdHGFDoB1JDIjDSOVZkAh0PnlHJkeGnK47LbcnYAjxwhKFQaRQp7VQVguj6F 1wue180KybSub/nkvMlf9qu4n66Nd7m5TIzov8WkOpBy09wt0jPpFR5DUNqU2x4KpUQ== 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="T/7X6mbX"; 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 4DA89606ABC3 for ; Tue, 1 Dec 2026 16:21:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4DA89606ABC3 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) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=T/7X6mbX 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 AA5151516; Tue, 1 Dec 2026 08:21:42 -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 977633F86F; Tue, 1 Dec 2026 08:21:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796142105; bh=Rz5j0GnZ+arCh5LiUoPvs93mauhoewMec4cRtpNd3vA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=T/7X6mbX0KodK4pZ9GPST2rUtPSbvZxVUj5hC0fztgwJFwgKdErqO+Y8NtRjm7awr aPW8mwfWdEd3Sc0rt+Fu+ciApIFM5XubDkDwrQdUico39YqQPvd9DGo9i7+PA0yRQv kqhJO7SFc9bxRLHcnm77/U/0EYYwbwGxoQL0qt6c= Date: Tue, 1 Dec 2026 16:21:38 +0000 From: Alexandru Elisei To: Mika Tammi Cc: Will Deacon , Julien Thierry , kvm@vger.kernel.org Subject: Re: [PATCH kvmtool v2 1/2] Set the VCPU affinity with sched_setaffinity() (for musl compatibility) Message-ID: References: <20261130220543.70217-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: <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 Looks correct to me: Reviewed-by: Alexandru Elisei Tested on an arm64 machine, with the host kernel compiled with CONFIG_NR_CPUS=16 and CONFIG_NR_CPUS=4096, checked /proc//task//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 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) >