From: Josh Hilke <jrhilke@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, David Matlack <dmatlack@google.com>,
Alex Williamson <alex@shazbot.org>,
Josh Hilke <jrhilke@google.com>
Subject: [PATCH v2 02/14] KVM: selftests: Add helper functions for IRQ testing
Date: Tue, 31 Mar 2026 19:40:21 +0000 [thread overview]
Message-ID: <20260331194033.3890309-3-jrhilke@google.com> (raw)
In-Reply-To: <20260331194033.3890309-1-jrhilke@google.com>
Introduce a test library to get IRQ numbers and counts. Add utility
functions to synchronize global variables between the host and guest to
track interrupt reception and readiness.
Signed-off-by: David Matlack <dmatlack@google.com>
Signed-off-by: Josh Hilke <jrhilke@google.com>
Co-developed-by: Josh Hilke <jrhilke@google.com>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../testing/selftests/kvm/include/irq_util.h | 11 +++
.../testing/selftests/kvm/include/kvm_util.h | 11 +++
tools/testing/selftests/kvm/lib/irq_util.c | 84 +++++++++++++++++++
4 files changed, 107 insertions(+)
create mode 100644 tools/testing/selftests/kvm/include/irq_util.h
create mode 100644 tools/testing/selftests/kvm/lib/irq_util.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index b3e223f45575..d8f77e181e8e 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -11,6 +11,7 @@ LIBKVM += lib/kvm_util.c
LIBKVM += lib/lru_gen_util.c
LIBKVM += lib/memstress.c
LIBKVM += lib/guest_sprintf.c
+LIBKVM += lib/irq_util.c
LIBKVM += lib/rbtree.c
LIBKVM += lib/sparsebit.c
LIBKVM += lib/test_util.c
diff --git a/tools/testing/selftests/kvm/include/irq_util.h b/tools/testing/selftests/kvm/include/irq_util.h
new file mode 100644
index 000000000000..a5d6b63b30da
--- /dev/null
+++ b/tools/testing/selftests/kvm/include/irq_util.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef SELFTEST_KVM_PROC_UTIL_H
+#define SELFTEST_KVM_PROC_UTIL_H
+
+#include <stdint.h>
+
+int get_irq_number(const char *device_bdf, int msi);
+uint64_t get_irq_count(int irq);
+uint64_t get_irq_count_by_name(const char *name);
+
+#endif /* SELFTEST_KVM_PROC_UTIL_H */
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 8b39cb919f4f..8be02e7980eb 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -1144,6 +1144,17 @@ vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
memcpy(&(g), _p, sizeof(g)); \
})
+#define READ_FROM_GUEST(_vm, _variable) ({ \
+ sync_global_from_guest(_vm, _variable); \
+ READ_ONCE(_variable); \
+})
+
+#define WRITE_TO_GUEST(_vm, _variable, _value) do { \
+ WRITE_ONCE(_variable, _value); \
+ sync_global_to_guest(_vm, _variable); \
+} while (0)
+
+
/*
* Write a global value, but only in the VM's (guest's) domain. Primarily used
* for "globals" that hold per-VM values (VMs always duplicate code and global
diff --git a/tools/testing/selftests/kvm/lib/irq_util.c b/tools/testing/selftests/kvm/lib/irq_util.c
new file mode 100644
index 000000000000..062f6cda23b9
--- /dev/null
+++ b/tools/testing/selftests/kvm/lib/irq_util.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "kvm_util.h"
+#include "test_util.h"
+#include "irq_util.h"
+
+#include <ctype.h>
+
+static FILE *open_proc_interrupts(void)
+{
+ FILE *fp;
+
+ fp = fopen("/proc/interrupts", "r");
+ TEST_ASSERT(fp, "fopen(/proc/interrupts) failed");
+
+ return fp;
+}
+
+int get_irq_number(const char *device_bdf, int msi)
+{
+ char search_string[64];
+ char line[4096];
+ int irq = -1;
+ FILE *fp;
+
+ fp = open_proc_interrupts();
+
+ snprintf(search_string, sizeof(search_string), "vfio-msix[%d]", msi);
+
+ while (fgets(line, sizeof(line), fp)) {
+ if (strstr(line, device_bdf) && strstr(line, search_string)) {
+ TEST_ASSERT_EQ(1, sscanf(line, "%d:", &irq));
+ break;
+ }
+ }
+
+ fclose(fp);
+
+ TEST_ASSERT(irq != -1, "Failed to locate IRQ for %s %s", device_bdf, search_string);
+ return irq;
+}
+
+static int parse_interrupt_count(char *token)
+{
+ char *c;
+
+ for (c = token; *c; c++) {
+ if (!isdigit(*c))
+ return 0;
+ }
+
+ return atoi_non_negative("interrupt count", token);
+}
+
+uint64_t get_irq_count_by_name(const char *name)
+{
+ uint64_t total_count = 0;
+ char line[4096];
+ FILE *fp;
+
+ fp = open_proc_interrupts();
+
+ while (fgets(line, sizeof(line), fp)) {
+ char *token = strtok(line, " ");
+
+ if (strcmp(token, name))
+ continue;
+
+ while ((token = strtok(NULL, " ")))
+ total_count += parse_interrupt_count(token);
+
+ break;
+ }
+
+ fclose(fp);
+ return total_count;
+}
+
+uint64_t get_irq_count(int irq)
+{
+ char search_string[32];
+
+ snprintf(search_string, sizeof(search_string), "%d:", irq);
+ return get_irq_count_by_name(search_string);
+}
--
2.53.0.1118.gaef5881109-goog
next prev parent reply other threads:[~2026-03-31 19:41 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 ` Josh Hilke [this message]
2026-04-01 18:26 ` [PATCH v2 02/14] KVM: selftests: Add helper functions for IRQ testing 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
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=20260331194033.3890309-3-jrhilke@google.com \
--to=jrhilke@google.com \
--cc=alex@shazbot.org \
--cc=dmatlack@google.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.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