Re: [PATCH v3 13/16] KVM: selftests: Verify vCPU migration during IRQ delivery

From: Sean Christopherson

Date: Tue May 26 2026 - 22:23:24 EST


On Tue, Apr 21, 2026, Josh Hilke wrote:
> From: David Matlack <dmatlack@xxxxxxxxxx>
>
> Add the '-m' flag to irq_test to migrate vCPU threads across random
> physical CPUs during the test. This validates KVM's ability to handle
> vCPUs changing physical CPUs while interrupts are actively being
> injected.
>
> To support this, add wrappers for gettid() and sched_getaffinity(), and
> introduce pin_task_to_random_cpu() in lib/kvm_util.c.
>
> Co-developed-by: Josh Hilke <jrhilke@xxxxxxxxxx>
> Signed-off-by: Josh Hilke <jrhilke@xxxxxxxxxx>
> Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
> ---
> .../selftests/kvm/include/kvm_syscalls.h | 6 ++++
> .../testing/selftests/kvm/include/kvm_util.h | 2 ++
> tools/testing/selftests/kvm/irq_test.c | 34 +++++++++++++++++--
> tools/testing/selftests/kvm/lib/kvm_util.c | 19 +++++++++++
> 4 files changed, 59 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_syscalls.h b/tools/testing/selftests/kvm/include/kvm_syscalls.h
> index d4e613162bba..d73f45c5df92 100644
> --- a/tools/testing/selftests/kvm/include/kvm_syscalls.h
> +++ b/tools/testing/selftests/kvm/include/kvm_syscalls.h
> @@ -73,9 +73,15 @@ static inline int kvm_dup(int fd)
> return new_fd;
> }
>
> +static inline int gettid(void)

gettid() already exists in at least some of my environments:

include/kvm_syscalls.h:87:19: error: static declaration of ‘gettid’ follows non-static declaration
87 | static inline int gettid(void)
| ^~~~~~
In file included from /usr/include/unistd.h:1221,
from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
from /usr/include/signal.h:328,
from include/test_util.h:12,
from lib/kvm_util.c:7:
/usr/include/x86_64-linux-gnu/bits/unistd_ext.h:34:16: note: previous declaration of ‘gettid’ with type ‘__pid_t(void)’ {aka ‘int(void)’}
34 | extern __pid_t gettid (void) __THROW;
| ^~~~~~

> +void pin_task_to_random_cpu(pthread_t task, cpu_set_t *possible_cpus)
> +{
> + int i = 0, nr_cpus = CPU_COUNT(possible_cpus);
> + int cpu, target_idx;
> +
> + target_idx = kvm_random_u64(&kvm_rng) % nr_cpus;
> +
> + for (cpu = 0; i < nr_cpus; cpu++) {

Shouldn't this be bounded on "cpu < nr_cpus"? If 'i' gets to nr_cpus, it means
"i == target_idx" was never taken. And at that point, the loop will hang
indefinitely because cpu will likely be waaaay out of bounds of possible_cpus,
i.e. the loop will always continue.

> + if (!CPU_ISSET(cpu, possible_cpus))
> + continue;
> +
> + if (i == target_idx) {
> + pin_task_to_cpu(task, cpu);
> + return;
> + }
> + i++;
> + }

This should TEST_FAIL(). Or maybe "break" on the match, and the assert that
i == target_idx?

> +}
> +
> void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
> int nr_vcpus)
> {
> --
> 2.54.0.rc2.533.g4f5dca5207-goog
>