From: Sean Christopherson <seanjc@google.com>
To: Josh Hilke <jrhilke@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
kvm@vger.kernel.org, David Matlack <dmatlack@google.com>,
Alex Williamson <alex@shazbot.org>
Subject: Re: [PATCH v2 11/14] KVM: selftests: Add support for vCPU pinning
Date: Wed, 1 Apr 2026 13:55:47 -0700 [thread overview]
Message-ID: <ac2GU_u3tzyTIcls@google.com> (raw)
In-Reply-To: <20260331194033.3890309-12-jrhilke@google.com>
On Tue, Mar 31, 2026, Josh Hilke wrote:
> From: David Matlack <dmatlack@google.com>
>
> Add the '-p' flag to vfio_pci_irq_test to pin vCPU threads to random
> physical CPUs throughout the test. This stresses the interaction between
> vCPU migration and interrupt delivery, ensuring that interrupts find
> their target even as the vCPU moves across the host's pCPUs.
This isn't "pinning" in the normal sense of the word in KVM-land. Pinning in
KVM usually means affining a vCPU to _one_ pCPU and never changing that
affinity. What you're doing here is *very* different; you're delibrately
mucking with affinity to force pCPU migrations. That isn't bad by any means,
but calling it "pinning" is super confusing, and I would argue the use of '-p'
is flat out wrong. I would suggest '-m' for migrate if it's available.
> Signed-off-by: David Matlack <dmatlack@google.com>
> Signed-off-by: Josh Hilke <jrhilke@google.com>
> Co-developed-by: Josh Hilke <jrhilke@google.com>
> ---
> .../selftests/kvm/include/kvm_syscalls.h | 7 ++
> .../testing/selftests/kvm/vfio_pci_irq_test.c | 79 ++++++++++++++++++-
> 2 files changed, 83 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_syscalls.h b/tools/testing/selftests/kvm/include/kvm_syscalls.h
> index d4e613162bba..5f839f735640 100644
> --- a/tools/testing/selftests/kvm/include/kvm_syscalls.h
> +++ b/tools/testing/selftests/kvm/include/kvm_syscalls.h
> @@ -73,9 +73,16 @@ static inline int kvm_dup(int fd)
> return new_fd;
> }
>
> +static inline int gettid(void)
> +{
> + return syscall(__NR_gettid);
> +}
> +
> __KVM_SYSCALL_DEFINE(munmap, 2, void *, mem, size_t, size);
> __KVM_SYSCALL_DEFINE(close, 1, int, fd);
> __KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, loff_t, len);
> __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length);
> +__KVM_SYSCALL_DEFINE(sched_setaffinity, 3, pid_t, pid, size_t, cpusetsize, const cpu_set_t *, mask);
> +__KVM_SYSCALL_DEFINE(sched_getaffinity, 3, pid_t, pid, size_t, cpusetsize, cpu_set_t *, mask);
Please add these separately and use then throughout, e.g. in rseq_test.c (which
doesn't very similar migration shenanigans).
> #endif /* SELFTEST_KVM_SYSCALLS_H */
> diff --git a/tools/testing/selftests/kvm/vfio_pci_irq_test.c b/tools/testing/selftests/kvm/vfio_pci_irq_test.c
> index fa77ce348251..c2f48c6710dd 100644
> --- a/tools/testing/selftests/kvm/vfio_pci_irq_test.c
> +++ b/tools/testing/selftests/kvm/vfio_pci_irq_test.c
> @@ -22,6 +22,8 @@ static bool guest_ready_for_irqs[KVM_MAX_VCPUS];
> static bool guest_received_irq[KVM_MAX_VCPUS];
> static bool guest_received_nmi[KVM_MAX_VCPUS];
>
> +static pid_t vcpu_tids[KVM_MAX_VCPUS];
> +
> #define TIMEOUT_NS (2ULL * 1000 * 1000 * 1000)
>
> static u32 guest_get_vcpu_id(void)
> @@ -64,12 +66,64 @@ static void *vcpu_thread_main(void *arg)
> struct kvm_vcpu *vcpu = arg;
> struct ucall uc;
>
> + WRITE_ONCE(vcpu_tids[vcpu->id], syscall(__NR_gettid));
Heh, what's the point of adding gettid() if you don't use it?
> +
> vcpu_run(vcpu);
> TEST_ASSERT_EQ(UCALL_DONE, get_ucall(vcpu, &uc));
>
> return NULL;
> }
>
> +static int get_cpu(struct kvm_vcpu *vcpu)
> +{
> + pid_t tid = vcpu_tids[vcpu->id];
> + cpu_set_t cpus;
> + int cpu = -1;
> + int i;
> +
> + kvm_sched_getaffinity(tid, sizeof(cpus), &cpus);
> +
> + for (i = 0; i < get_nprocs(); i++) {
> + if (!CPU_ISSET(i, &cpus))
> + continue;
> +
> + if (cpu != -1) {
> + cpu = i;
> + } else {
> + /* vCPU is pinned to multiple CPUs */
> + return -1;
Given that this is for debugging failures, printing -1 isn't as helpful as it
could be. Ideally the test would print something similar to the output of
`taskset -c -p <pid>`.
> + }
> + }
> +
> + return cpu;
> +}
> +
> +static void pin_vcpu_threads(int nr_vcpus, int start_cpu, cpu_set_t *available_cpus)
> +{
> + const size_t size = sizeof(cpu_set_t);
> + int nr_cpus, cpu, vcpu_index = 0;
> + cpu_set_t target_cpu;
> +
> + nr_cpus = get_nprocs();
> + CPU_ZERO(&target_cpu);
> +
> + for (cpu = start_cpu;; cpu = (cpu + 1) % nr_cpus) {
Why round-robin the pCPUs? If anything, that _reduces_ coverage because it
creates a single pattern for how vCPUs are affined. And vCPUs will never be
affined to the same pCPU.
Why not fully randomize the affinity? Again, this isn't pinning, it's forced
task migration.
> + if (vcpu_index == nr_vcpus)
> + break;
> +
> + if (!CPU_ISSET(cpu, available_cpus))
> + continue;
> +
> + CPU_SET(cpu, &target_cpu);
> +
> + kvm_sched_setaffinity(vcpu_tids[vcpu_index], size, &target_cpu);
If you're affining to _one_ pCPU, just use pin_task_to_cpu(). Actually, all
of this belongs in a helper, e.g. pin_task_to_random_cpu().
> + CPU_CLR(cpu, &target_cpu);
> +
> + vcpu_index++;
> + }
> +}
next prev parent reply other threads:[~2026-04-01 20:55 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-31 19:40 [PATCH v2 00/14] KVM: selftests: Link with VFIO selftests lib and test device interrupts Josh Hilke
2026-03-31 19:40 ` [PATCH v2 01/14] KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests Josh Hilke
2026-04-01 18:17 ` Sean Christopherson
2026-04-01 23:49 ` Josh Hilke
2026-03-31 19:40 ` [PATCH v2 02/14] KVM: selftests: Add helper functions for IRQ testing Josh Hilke
2026-04-01 18:26 ` Sean Christopherson
2026-04-01 23:54 ` Josh Hilke
2026-03-31 19:40 ` [PATCH v2 03/14] KVM: selftests: Add vfio_pci_irq_test Josh Hilke
2026-04-01 19:58 ` Sean Christopherson
2026-04-02 0:13 ` Josh Hilke
2026-04-02 17:52 ` Sean Christopherson
2026-03-31 19:40 ` [PATCH v2 04/14] KVM: selftests: Reproduce tests that rely on randomization Josh Hilke
2026-03-31 19:40 ` [PATCH v2 05/14] KVM: selftests: Add support for random host IRQ affinity Josh Hilke
2026-04-01 20:01 ` Sean Christopherson
2026-04-02 1:16 ` Josh Hilke
2026-03-31 19:40 ` [PATCH v2 06/14] KVM: selftests: Allow blocking vCPUs via HLT Josh Hilke
2026-04-01 20:03 ` Sean Christopherson
2026-03-31 19:40 ` [PATCH v2 07/14] KVM: selftests: Add support for physical device MSI triggers Josh Hilke
2026-04-01 20:24 ` Sean Christopherson
2026-04-02 3:23 ` Josh Hilke
2026-03-31 19:40 ` [PATCH v2 08/14] KVM: selftests: Add option to clear GSI routes Josh Hilke
2026-03-31 19:40 ` [PATCH v2 09/14] KVM: selftests: Make test IRQ count configurable Josh Hilke
2026-03-31 19:40 ` [PATCH v2 10/14] KVM: selftests: Add support for NMI delivery Josh Hilke
2026-04-01 20:29 ` Sean Christopherson
2026-04-02 5:27 ` Josh Hilke
2026-03-31 19:40 ` [PATCH v2 11/14] KVM: selftests: Add support for vCPU pinning Josh Hilke
2026-04-01 20:55 ` Sean Christopherson [this message]
2026-03-31 19:40 ` [PATCH v2 12/14] KVM: selftests: Support testing with multiple vCPUs Josh Hilke
2026-03-31 19:40 ` [PATCH v2 13/14] KVM: selftests: Add xAPIC mode support Josh Hilke
2026-03-31 19:40 ` [PATCH v2 14/14] KVM: selftests: Make vfio_pci_irq_test timeout configurable Josh Hilke
2026-04-01 21:00 ` Sean Christopherson
2026-04-01 18:17 ` [PATCH v2 00/14] KVM: selftests: Link with VFIO selftests lib and test device interrupts Sean Christopherson
2026-04-01 18:52 ` David Matlack
2026-04-01 19:07 ` Sean Christopherson
2026-04-01 20:12 ` David Matlack
2026-04-01 23:41 ` Josh Hilke
2026-04-01 23:58 ` David Matlack
2026-04-02 0:38 ` Josh Hilke
2026-04-02 1:49 ` Josh Hilke
2026-04-02 17:35 ` Sean Christopherson
2026-04-02 17:56 ` David Matlack
2026-04-02 18:07 ` Josh Hilke
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ac2GU_u3tzyTIcls@google.com \
--to=seanjc@google.com \
--cc=alex@shazbot.org \
--cc=dmatlack@google.com \
--cc=jrhilke@google.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox