Skip to contentSkip to conversation

riscv: sbi: Add IPI tests

PATCH In review Andrew Jones 1 participant 6 messages Nov 6, 2024 – Nov 11, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover riscv: sbi: Add IPI tests

Repost Cade's IPI test patch[1] with all the changes pointed out in the last review and more.

Based-on: https://gitlab.com/jones-drew/kvm-unit-tests/-/commits/riscv/sbi

[1] https://lore.kernel.org/all/20240826065106.20281-1-cade.richard at berkeley.edu/

v2:
- Rebased on latest riscv/sbi
- Improved ipi done checking by introducing cpumask_equal
- Added a patch for another IPI test case

Andrew Jones (2):
riscv: Add sbi_send_ipi_broadcast
riscv: sbi: Add two in hart_mask IPI test

Cade Richard (1):
riscv: sbi: Add IPI extension tests

 lib/cpumask.h       |  13 ++++
 lib/riscv/asm/sbi.h |   1 +
 lib/riscv/sbi.c     |   7 +-
 riscv/sbi.c         | 184 +++++++++++++++++++++++++++++++++++++++++++-
 riscv/unittests.cfg |   1 +
 5 files changed, 203 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH v2 1/3 riscv: Add sbi_send_ipi_broadcast

Coming SBI IPI tests will use this, but as it could be useful for other tests too, add it to the library.

---
 lib/riscv/asm/sbi.h | 1 +
 lib/riscv/sbi.c     | 7 ++++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
lib/riscv/asm/sbi.h +1 -0
index 1319439b7118..102486c00de3 100644
@@ -87,6 +87,7 @@ struct sbiret sbi_hart_get_status(unsigned long hartid);
 struct sbiret sbi_send_ipi(unsigned long hart_mask, unsigned long hart_mask_base);
 struct sbiret sbi_send_ipi_cpu(int cpu);
 struct sbiret sbi_send_ipi_cpumask(const cpumask_t *mask);
+struct sbiret sbi_send_ipi_broadcast(void);
 struct sbiret sbi_set_timer(unsigned long stime_value);
 long sbi_probe(int ext);
 
lib/riscv/sbi.c +6 -1
index f25bde169490..02dd338c1915 100644
@@ -62,13 +62,18 @@ struct sbiret sbi_send_ipi_cpu(int cpu)
 	return sbi_send_ipi(1UL, cpus[cpu].hartid);
 }
 
+struct sbiret sbi_send_ipi_broadcast(void)
+{
+	return sbi_send_ipi(0, -1UL);
+}
+
 struct sbiret sbi_send_ipi_cpumask(const cpumask_t *mask)
 {
 	struct sbiret ret;
 	cpumask_t tmp;
 
 	if (cpumask_full(mask))
-		return sbi_send_ipi(0, -1UL);
+		return sbi_send_ipi_broadcast();
 
 	cpumask_copy(&tmp, mask);
 
Signature · 1 line
2.47.0
#2 lore raw

PATCH v2 2/3 riscv: sbi: Add IPI extension tests

In reply to Andrew Jones

From: Cade Richard <[email protected]>

Ensure IPIs directed at single harts are received and also that all harts receive IPIs on broadcast. Also check for invalid param errors when the params result in hartids greater than the max.

---
 lib/cpumask.h       |  13 ++++
 riscv/sbi.c         | 147 +++++++++++++++++++++++++++++++++++++++++++-
 riscv/unittests.cfg |   1 +
 3 files changed, 159 insertions(+), 2 deletions(-)
lib/cpumask.h +13 -0
index 37d360786573..b4cd83a66a56 100644
@@ -72,6 +72,19 @@ static inline bool cpumask_subset(const struct cpumask *src1, const struct cpuma
 	return !lastmask || !((cpumask_bits(src1)[i] & ~cpumask_bits(src2)[i]) & lastmask);
 }
 
+static inline bool cpumask_equal(const struct cpumask *src1, const struct cpumask *src2)
+{
+	unsigned long lastmask = BIT_MASK(nr_cpus) - 1;
+	int i;
+
+	for (i = 0; i < BIT_WORD(nr_cpus); ++i) {
+		if (cpumask_bits(src1)[i] != cpumask_bits(src2)[i])
+			return false;
+	}
+
+	return !lastmask || (cpumask_bits(src1)[i] & lastmask) == (cpumask_bits(src2)[i] & lastmask);
+}
+
 static inline bool cpumask_empty(const cpumask_t *mask)
 {
 	unsigned long lastmask = BIT_MASK(nr_cpus) - 1;
riscv/sbi.c +145 -2

174 lines not on this page · raw message

riscv/unittests.cfg +1 -0
index cbd36bf63e14..2eb760eca24e 100644
@@ -16,4 +16,5 @@ groups = selftest
 # Set $FIRMWARE_OVERRIDE to /path/to/firmware to select the SBI implementation.
 [sbi]
 file = sbi.flat
+smp = $MAX_SMP
 groups = sbi
Signature · 1 line
2.47.0
#3 lore raw

PATCH v2 3/3 riscv: sbi: Add two in hart_mask IPI test

In reply to Andrew Jones

We should ensure that when hart_mask has more than one hartid that both harts get IPIs with a single call of the IPI function.

---
 riscv/sbi.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
riscv/sbi.c +38 -1
index cdf8d13cc9cf..8ccdf42f902a 100644
@@ -364,10 +364,11 @@ static void check_ipi(void)
 	int nr_cpus_present = cpumask_weight(&cpu_present_mask);
 	int me = smp_processor_id();
 	unsigned long max_hartid = 0;
+	unsigned long hartid1, hartid2;
 	cpumask_t ipi_receivers;
 	static prng_state ps;
 	struct sbiret ret;
-	int cpu;
+	int cpu, cpu2;
 
 	ps = prng_init(0xDEADBEEF);
 
@@ -398,6 +399,42 @@ static void check_ipi(void)
 	ipi_hart_check(&ipi_receivers);
 	report_prefix_pop();
 
+	report_prefix_push("two in hart_mask");
+
+	if (nr_cpus_present < 3) {
+		report_skip("3 cpus required");
+		goto end_two;
+	}
+
+	cpu = rand_online_cpu(&ps);
+	hartid1 = cpus[cpu].hartid;
+	hartid2 = 0;
+	for_each_present_cpu(cpu2) {
+		if (cpu2 == cpu || cpu2 == me)
+			continue;
+		hartid2 = cpus[cpu2].hartid;
+		if (__builtin_labs(hartid2 - hartid1) < BITS_PER_LONG)
+			break;
+	}
+	if (cpu2 == nr_cpus) {
+		report_skip("hartids are too sparse");
+		goto end_two;
+	}
+
+	cpumask_clear(&ipi_done);
+	cpumask_clear(&ipi_receivers);
+	cpumask_set_cpu(cpu, &ipi_receivers);
+	cpumask_set_cpu(cpu2, &ipi_receivers);
+	on_cpu_async(cpu, ipi_hart_wait, (void *)d);
+	on_cpu_async(cpu2, ipi_hart_wait, (void *)d);
+	ret = sbi_send_ipi((1UL << __builtin_labs(hartid2 - hartid1)) | 1UL, hartid1 < hartid2 ? hartid1 : hartid2);
+	report(ret.error == SBI_SUCCESS, "ipi returned success");
+	while (!cpumask_equal(&ipi_done, &ipi_receivers))
+		cpu_relax();
+	ipi_hart_check(&ipi_receivers);
+end_two:
+	report_prefix_pop();
+
 	report_prefix_push("broadcast");
 	cpumask_clear(&ipi_done);
 	cpumask_copy(&ipi_receivers, &cpu_present_mask);
Signature · 1 line
2.47.0
#4 lore raw

Re: [kvm-unit-tests PATCH v2 3/3] riscv: sbi: Add two in hart_mask IPI test

Quoted 34 lines · Andrew Jones

We should ensure that when hart_mask has more than one hartid that both harts get IPIs with a single call of the IPI function.

---
 riscv/sbi.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
riscv/sbi.c +7 -1
index cdf8d13cc9cf..8ccdf42f902a 100644
@@ -364,10 +364,11 @@ static void check_ipi(void)
 	int nr_cpus_present = cpumask_weight(&cpu_present_mask);
 	int me = smp_processor_id();
 	unsigned long max_hartid = 0;
+	unsigned long hartid1, hartid2;
 	cpumask_t ipi_receivers;
 	static prng_state ps;
 	struct sbiret ret;
-	int cpu;
+	int cpu, cpu2;
 
 	ps = prng_init(0xDEADBEEF);
 
@@ -398,6 +399,42 @@ static void check_ipi(void)
 	ipi_hart_check(&ipi_receivers);
 	report_prefix_pop();
 
+	report_prefix_push("two in hart_mask");
+
+	if (nr_cpus_present < 3) {
+		report_skip("3 cpus required");
+		goto end_two;

riscv/sbi.c static void check_ipi(void) · line 416

Quoted 10 lines
riscv/sbi.c +10 -0
+	}
+
+	cpu = rand_online_cpu(&ps);
+	hartid1 = cpus[cpu].hartid;
+	hartid2 = 0;
+	for_each_present_cpu(cpu2) {
+		if (cpu2 == cpu || cpu2 == me)
+			continue;
+		hartid2 = cpus[cpu2].hartid;
+		if (__builtin_labs(hartid2 - hartid1) < BITS_PER_LONG)

clang is complaining about these __builtin_labs calls not taking signed input. I'll add this wrapper

static long __labs(long a)

 {
    return __builtin_labs@;

}

Quoted 27 lines
riscv/sbi.c +21 -0
+			break;
+	}
+	if (cpu2 == nr_cpus) {
+		report_skip("hartids are too sparse");
+		goto end_two;
+	}
+
+	cpumask_clear(&ipi_done);
+	cpumask_clear(&ipi_receivers);
+	cpumask_set_cpu(cpu, &ipi_receivers);
+	cpumask_set_cpu(cpu2, &ipi_receivers);
+	on_cpu_async(cpu, ipi_hart_wait, (void *)d);
+	on_cpu_async(cpu2, ipi_hart_wait, (void *)d);
+	ret = sbi_send_ipi((1UL << __builtin_labs(hartid2 - hartid1)) | 1UL, hartid1 < hartid2 ? hartid1 : hartid2);
+	report(ret.error == SBI_SUCCESS, "ipi returned success");
+	while (!cpumask_equal(&ipi_done, &ipi_receivers))
+		cpu_relax();
+	ipi_hart_check(&ipi_receivers);
+end_two:
+	report_prefix_pop();
+
 	report_prefix_push("broadcast");
 	cpumask_clear(&ipi_done);
 	cpumask_copy(&ipi_receivers, &cpu_present_mask);
Signature · 1 line
2.47.0
#5 lore raw
In reply to Andrew Jones
Quoted 20 lines · Andrew Jones

Repost Cade's IPI test patch[1] with all the changes pointed out in the last review and more.

Based-on: https://gitlab.com/jones-drew/kvm-unit-tests/-/commits/riscv/sbi

[1] https://lore.kernel.org/all/20240826065106.20281-1-cade.richard at berkeley.edu/

v2:
- Rebased on latest riscv/sbi
- Improved ipi done checking by introducing cpumask_equal
- Added a patch for another IPI test case

Andrew Jones (2):
riscv: Add sbi_send_ipi_broadcast
riscv: sbi: Add two in hart_mask IPI test

Cade Richard (1):
riscv: sbi: Add IPI extension tests

Quoted 10 lines
 lib/cpumask.h       |  13 ++++
 lib/riscv/asm/sbi.h |   1 +
 lib/riscv/sbi.c     |   7 +-
 riscv/sbi.c         | 184 +++++++++++++++++++++++++++++++++++++++++++-
 riscv/unittests.cfg |   1 +
 5 files changed, 203 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.0

Merged through riscv/sbi.

Thanks,
drew