Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Maxim Levitsky <mlevitsk@redhat.com>
To: kvm@vger.kernel.org
Cc: "Andrew Jones" <drjones@redhat.com>,
	"Alexandru Elisei" <alexandru.elisei@arm.com>,
	"Maxim Levitsky" <mlevitsk@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Claudio Imbrenda" <imbrenda@linux.ibm.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Nico Boehr" <nrb@linux.ibm.com>,
	"Cathy Avery" <cavery@redhat.com>,
	"Janosch Frank" <frankja@linux.ibm.com>
Subject: [kvm-unit-tests PATCH v3 12/27] x86: add IPI stress test
Date: Tue, 22 Nov 2022 18:11:37 +0200	[thread overview]
Message-ID: <20221122161152.293072-13-mlevitsk@redhat.com> (raw)
In-Reply-To: <20221122161152.293072-1-mlevitsk@redhat.com>

Adds a test that sends IPIs between vCPUs and detects missing IPIs

Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
 x86/Makefile.common |   3 +-
 x86/ipi_stress.c    | 167 ++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |  10 +++
 3 files changed, 179 insertions(+), 1 deletion(-)
 create mode 100644 x86/ipi_stress.c

diff --git a/x86/Makefile.common b/x86/Makefile.common
index fa0a50e6..08cc036b 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -89,7 +89,8 @@ tests-common = $(TEST_DIR)/vmexit.$(exe) $(TEST_DIR)/tsc.$(exe) \
                $(TEST_DIR)/eventinj.$(exe) \
                $(TEST_DIR)/smap.$(exe) \
                $(TEST_DIR)/umip.$(exe) \
-               $(TEST_DIR)/smm_int_window.$(exe)
+               $(TEST_DIR)/smm_int_window.$(exe) \
+               $(TEST_DIR)/ipi_stress.$(exe)
 
 # The following test cases are disabled when building EFI tests because they
 # use absolute addresses in their inline assembly code, which cannot compile
diff --git a/x86/ipi_stress.c b/x86/ipi_stress.c
new file mode 100644
index 00000000..dea3e605
--- /dev/null
+++ b/x86/ipi_stress.c
@@ -0,0 +1,167 @@
+#include "libcflat.h"
+#include "smp.h"
+#include "alloc.h"
+#include "apic.h"
+#include "processor.h"
+#include "isr.h"
+#include "asm/barrier.h"
+#include "delay.h"
+#include "desc.h"
+#include "msr.h"
+#include "vm.h"
+#include "types.h"
+#include "alloc_page.h"
+#include "vmalloc.h"
+#include "random.h"
+
+u64 num_iterations = 100000;
+float hlt_prob = 0.1;
+volatile bool end_test;
+
+#define APIC_TIMER_PERIOD (1000*1000*1000)
+
+struct cpu_test_state {
+	volatile u64 isr_count;
+	u64 last_isr_count;
+	struct random_state random;
+	int smp_id;
+} *cpu_states;
+
+
+static void ipi_interrupt_handler(isr_regs_t *r)
+{
+	cpu_states[smp_id()].isr_count++;
+	eoi();
+}
+
+static void local_timer_interrupt(isr_regs_t *r)
+{
+	struct cpu_test_state *state = &cpu_states[smp_id()];
+
+	u64 isr_count = state->isr_count;
+	unsigned long diff =  isr_count - state->last_isr_count;
+
+	if (!diff) {
+		printf("\n");
+		printf("hang detected!!\n");
+		end_test = true;
+		goto out;
+	}
+
+	printf("made %ld IPIs\n", diff * cpu_count());
+	state->last_isr_count = state->isr_count;
+out:
+	eoi();
+}
+
+static void wait_for_ipi(struct cpu_test_state *state)
+{
+	u64 old_count = state->isr_count;
+	bool use_halt = random_decision(&state->random, hlt_prob);
+
+	do {
+		if (use_halt) {
+			safe_halt();
+			cli();
+		} else
+			sti_nop_cli();
+
+	} while (old_count == state->isr_count);
+
+	assert(state->isr_count == old_count + 1);
+}
+
+
+static void vcpu_init(void *)
+{
+	struct cpu_test_state *state = &cpu_states[smp_id()];
+
+	memset(state, 0, sizeof(*state));
+
+	/* To make it easier to see iteration number in the trace */
+	handle_irq(0x40, ipi_interrupt_handler);
+	handle_irq(0x50, ipi_interrupt_handler);
+
+	state->random = get_prng();
+	state->isr_count = 0;
+	state->smp_id = smp_id();
+}
+
+static void vcpu_code(void *)
+{
+	struct cpu_test_state *state = &cpu_states[smp_id()];
+	int ncpus = cpu_count();
+	u64 i;
+	u8 target_smp_id;
+
+	if (state->smp_id > 0)
+		wait_for_ipi(state);
+
+	target_smp_id = state->smp_id == ncpus - 1 ? 0 : state->smp_id  + 1;
+
+	for (i = 0; i < num_iterations && !end_test; i++) {
+		// send IPI to a next vCPU in a circular fashion
+		apic_icr_write(APIC_INT_ASSERT |
+				APIC_DEST_PHYSICAL |
+				APIC_DM_FIXED |
+				(i % 2 ? 0x40 : 0x50),
+				target_smp_id);
+
+		if (i == (num_iterations - 1) && state->smp_id > 0)
+			break;
+
+		// wait for the IPI interrupt chain to come back to us
+		wait_for_ipi(state);
+	}
+}
+
+int main(int argc, void **argv)
+{
+	int cpu, ncpus = cpu_count();
+
+	handle_irq(0xF0, local_timer_interrupt);
+	apic_setup_timer(0xF0, APIC_LVT_TIMER_PERIODIC);
+
+	if (argc > 1) {
+		int hlt_param = atol(argv[1]);
+
+		if (hlt_param == 1)
+			hlt_prob = 100;
+		else if (hlt_param == 0)
+			hlt_prob = 0;
+	}
+
+	if (argc > 2)
+		num_iterations = atol(argv[2]);
+
+	setup_vm();
+	init_prng();
+
+	cpu_states = calloc(ncpus, sizeof(cpu_states[0]));
+
+	printf("found %d cpus\n", ncpus);
+	printf("running for %lld iterations\n",
+		(unsigned long long)num_iterations);
+
+	on_cpus(vcpu_init, NULL);
+
+	apic_start_timer(1000*1000*1000);
+
+	printf("test started, waiting to end...\n");
+
+	on_cpus(vcpu_code, NULL);
+
+	apic_stop_timer();
+	apic_cleanup_timer();
+
+	for (cpu = 0; cpu < ncpus; ++cpu) {
+		u64 result = cpu_states[cpu].isr_count;
+
+		report(result == num_iterations,
+				"Number of IPIs match (%lld)",
+				(unsigned long long)result);
+	}
+
+	free((void *)cpu_states);
+	return report_summary();
+}
diff --git a/x86/unittests.cfg b/x86/unittests.cfg
index df248dff..b0fd92fb 100644
--- a/x86/unittests.cfg
+++ b/x86/unittests.cfg
@@ -74,6 +74,16 @@ smp = 2
 file = smptest.flat
 smp = 3
 
+[ipi_stress]
+file = ipi_stress.flat
+extra_params = -cpu host,-x2apic -global kvm-pit.lost_tick_policy=discard -machine kernel-irqchip=on
+smp = 4
+
+[ipi_stress_x2apic]
+file = ipi_stress.flat
+extra_params = -cpu host,+x2apic -global kvm-pit.lost_tick_policy=discard -machine kernel-irqchip=on
+smp = 4
+
 [vmexit_cpuid]
 file = vmexit.flat
 extra_params = -append 'cpuid'
-- 
2.34.3


  parent reply	other threads:[~2022-11-22 16:15 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-22 16:11 [kvm-unit-tests PATCH v3 00/27] kvm-unit-tests: set of fixes and new tests Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 01/27] x86: replace irq_{enable|disable}() with sti()/cli() Maxim Levitsky
2022-12-01 13:46   ` Emanuele Giuseppe Esposito
2022-12-06 13:55     ` Maxim Levitsky
2022-12-06 14:15       ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 02/27] x86: introduce sti_nop() and sti_nop_cli() Maxim Levitsky
2022-12-01 13:46   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 03/27] x86: add few helper functions for apic local timer Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 04/27] svm: remove nop after stgi/clgi Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 05/27] svm: make svm_intr_intercept_mix_if/gif test a bit more robust Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 06/27] svm: use apic_start_timer/apic_stop_timer instead of open coding it Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 07/27] x86: Add test for #SMI during interrupt window Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 08/27] x86: Add a simple test for SYSENTER instruction Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 09/27] svm: add simple nested shutdown test Maxim Levitsky
2022-12-01 13:46   ` Emanuele Giuseppe Esposito
2022-12-06 13:56     ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 10/27] SVM: add two tests for exitintinto on exception Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 11/27] lib: Add random number generator Maxim Levitsky
2022-11-23  9:28   ` Claudio Imbrenda
2022-11-23 12:54     ` Andrew Jones
2022-12-06 13:57       ` Maxim Levitsky
2022-12-06 14:07     ` Maxim Levitsky
2022-12-14 10:33       ` Claudio Imbrenda
2022-11-22 16:11 ` Maxim Levitsky [this message]
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 13/27] svm: remove get_npt_pte extern Maxim Levitsky
2022-12-01 13:46   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 14/27] svm: move svm spec definitions to lib/x86/svm.h Maxim Levitsky
2022-12-01 13:54   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 15/27] svm: move some svm support functions into lib/x86/svm_lib.h Maxim Levitsky
2022-12-01 13:59   ` Emanuele Giuseppe Esposito
2022-12-06 14:10     ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 16/27] svm: move setup_svm() to svm_lib.c Maxim Levitsky
2022-12-01 16:14   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 17/27] svm: correctly skip if NPT not supported Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 18/27] svm: move vmcb_ident to svm_lib.c Maxim Levitsky
2022-12-01 16:18   ` Emanuele Giuseppe Esposito
2022-12-06 14:11     ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 19/27] svm: rewerite vm entry macros Maxim Levitsky
2022-12-02 10:14   ` Emanuele Giuseppe Esposito
2022-12-06 13:56     ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 20/27] svm: move v2 tests run into test_run Maxim Levitsky
2022-12-02  9:53   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 21/27] svm: cleanup the default_prepare Maxim Levitsky
2022-12-02  9:45   ` Emanuele Giuseppe Esposito
2022-12-06 13:56     ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 22/27] svm: introduce svm_vcpu Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 23/27] svm: introduce struct svm_test_context Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 24/27] svm: use svm_test_context in v2 tests Maxim Levitsky
2022-12-02 10:27   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 25/27] svm: move nested vcpu to test context Maxim Levitsky
2022-12-02 10:22   ` Emanuele Giuseppe Esposito
2022-12-06 14:29     ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 26/27] svm: move test_guest_func " Maxim Levitsky
2022-12-02 10:28   ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 27/27] x86: ipi_stress: add optional SVM support Maxim Levitsky
2023-06-07 23:25 ` [kvm-unit-tests PATCH v3 00/27] kvm-unit-tests: set of fixes and new tests Sean Christopherson

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=20221122161152.293072-13-mlevitsk@redhat.com \
    --to=mlevitsk@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=alexandru.elisei@arm.com \
    --cc=cavery@redhat.com \
    --cc=drjones@redhat.com \
    --cc=frankja@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=nrb@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=thuth@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