Commit 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU threads") moved vcpu_affinity_parser() from arm64 to builtin-run.c. There it returns 0 when cpulist_parse() fails, where it returned the error before. So lkvm now ignores a CPU list that it can't parse, such as "foo" or a CPU number of NR_CPUS or more, and runs the guest without the affinity. cpulist_parse() also accepts a list without any CPUs, such as "" or ",", and vcpu_affinity_parser() has taken such a list since --vcpu-affinity was added. lkvm then only fails later, when it can't give the VCPU threads an empty affinity. Return the error again, and reject a list without CPUs too, so that lkvm says what is wrong with the list, prints its usage and exits. Fixes: 4c90ccf4c593 ("Correctly apply --vcpu-affinity to the VCPU threads") Suggested-by: Alexandru Elisei Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Mika Tammi --- v2: - Also reject a list without CPUs, such as "" or "," (Alexandru) - Say what is wrong with the list (Alexandru) - New subject, as the patch now rejects both kinds of invalid list v1: https://lore.kernel.org/kvm/20261130222608.90696-1-mikatammi@gmail.com/ Unlike the check suggested in the review of v1, this one counts the CPUs after the list is parsed, so it also rejects lists such as "," or " ", which cpulist_parse() takes as no CPUs too. Tested with nixpkgs' kvmtool package at kvmtool master (4b4aceb1dae7) on x86_64 and arm64 hosts. With "foo" or a CPU number of NR_CPUS or more, master runs the guest with the VCPU threads on every CPU, and with "", "," or " " it fails when it creates the VCPU threads ("unable to create KVM VCPU thread: Invalid argument"). With this patch lkvm prints Error: Malformed or invalid VCPU affinity cpulist 'foo' or Error: Empty VCPU affinity cpulist and its usage, and exits with status 129. --no-vcpu-affinity, which calls the parser without a list and failed like an empty list, now gets the second message. Lists that can be parsed, such as 2-3 or 1,,2, set the affinity as before. A static musl lkvm, with the affinity fix [1] that master needs to build with musl, behaves the same on x86_64. Cross builds for riscv32 and riscv64 build. [1] https://lore.kernel.org/kvm/20261130220543.70217-1-mikatammi@gmail.com/ builtin-run.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index f06231e..d0e5a87 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -183,8 +183,10 @@ static int vcpu_affinity_parser(const struct option *opt, const char *arg, int u die_perror("calloc"); ret = cpulist_parse(cpulist, cpumask); - if (ret) + if (ret) { + pr_err("Malformed or invalid VCPU affinity cpulist '%s'", cpulist); goto out_free; + } kvm->vcpu_affinity = CPU_ALLOC(NR_CPUS); if (!kvm->vcpu_affinity) @@ -195,9 +197,14 @@ static int vcpu_affinity_parser(const struct option *opt, const char *arg, int u for_each_cpu(cpu, cpumask) CPU_SET_S(cpu, setsize, kvm->vcpu_affinity); + if (!CPU_COUNT_S(setsize, kvm->vcpu_affinity)) { + pr_err("Empty VCPU affinity cpulist"); + ret = -EINVAL; + } + out_free: free(cpumask); - return 0; + return ret; } #ifndef OPT_ARCH_RUN -- 2.54.0 (Apple Git-157)