[PATCH] KVM: s390: selftests: Add IRQ injection selftests

Janosch Frank <[email protected]>
Newsgroups org.kernel.vger.linux-s390,org.kernel.vger.kvm
Message-ID <[email protected]>
From: Gabriella Seifert <[email protected]>

The IRQ injection selftests haven't been added to the selftests yet so
let's do that now. We start off with a couple simple error checks for
invalid data.

Signed-off-by: Gabriella Seifert <[email protected]>
[[email protected]: Improved formatting and wording]
Signed-off-by: Janosch Frank <[email protected]>
---

Written by Gabriela in her internship as a side project. Unfortunately
she wasn't able to send it out herself due to a bit of time crunch.
She did prepare additional code that's more in-depth with guest side
checks but didn't have the time to fully test and clean it up. We'll
see if and when that code will be posted.

I have her permission to fix up review comments and she's in bcc.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/s390/irq_injection.c        | 74 +++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/irq_injection.c

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 6fc34e9bf8e1..423c0cb6f17d 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -215,6 +215,7 @@ TEST_GEN_PROGS_s390 += rseq_test
 TEST_GEN_PROGS_s390 += s390/irq_routing
 TEST_GEN_PROGS_s390 += mmu_stress_test
 TEST_GEN_PROGS_s390 += pre_fault_memory_test
+TEST_GEN_PROGS_s390 += s390/irq_injection
 
 TEST_GEN_PROGS_riscv = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test
diff --git a/tools/testing/selftests/kvm/s390/irq_injection.c b/tools/testing/selftests/kvm/s390/irq_injection.c
new file mode 100644
index 000000000000..75e0528f9b01
--- /dev/null
+++ b/tools/testing/selftests/kvm/s390/irq_injection.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test for KVM_S390_IRQ - s390x IRQ injection API
+ *
+ * Verifies that VM-level interrupt types are correctly rejected when
+ * injected via the vCPU file descriptor, and that invalid arguments
+ * to vCPU-level interrupts are also rejected.
+ *
+ * Copyright IBM Corp. 2026
+ * Author: Gabriella Seifert <[email protected]>
+ */
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include "kvm_util.h"
+#include "test_util.h"
+#include <linux/kvm.h>
+
+static void test_vm_irq_on_vcpu_fails(struct kvm_vcpu *vcpu, __u64 type)
+{
+	struct kvm_s390_irq irq = { .type = type };
+	int rc;
+
+	rc = __vcpu_ioctl(vcpu, KVM_S390_IRQ, &irq);
+	ksft_test_result(rc == -1 && errno == EINVAL,
+			 "VM on VCPU IRQ type 0x%llx: expected %d, got %d\n",
+			 type, EINVAL, errno);
+}
+
+static void test_vcpu_irq_bad_param_fails(struct kvm_vcpu *vcpu,
+					  struct kvm_s390_irq *irq)
+{
+	int rc;
+
+	rc = __vcpu_ioctl(vcpu, KVM_S390_IRQ, irq);
+	ksft_test_result(rc == -1 && errno == EINVAL,
+			 "VCPU IRQ bad param type 0x%llx: expected %d got %d\n",
+			 irq->type, EINVAL, errno);
+}
+
+int main(void)
+{
+	struct kvm_s390_irq irq;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	ksft_print_header();
+	ksft_set_plan(6);
+	vm = vm_create_with_one_vcpu(&vcpu, NULL);
+
+	test_vm_irq_on_vcpu_fails(vcpu, KVM_S390_INT_VIRTIO);
+	test_vm_irq_on_vcpu_fails(vcpu, KVM_S390_INT_SERVICE);
+	test_vm_irq_on_vcpu_fails(vcpu, KVM_S390_INT_IO_MIN);
+
+	memset(&irq, 0, sizeof(irq));
+	irq.type = KVM_S390_INT_EMERGENCY;
+	irq.u.emerg.code = 0xffff;
+	test_vcpu_irq_bad_param_fails(vcpu, &irq);
+
+	memset(&irq, 0, sizeof(irq));
+	irq.type = KVM_S390_INT_EXTERNAL_CALL;
+	irq.u.extcall.code = 0xffff;
+	test_vcpu_irq_bad_param_fails(vcpu, &irq);
+
+	memset(&irq, 0, sizeof(irq));
+	irq.type = 0xdeadbeef;
+	test_vcpu_irq_bad_param_fails(vcpu, &irq);
+
+	kvm_vm_free(vm);
+	ksft_finished();
+}
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.