Skip to contentSkip to conversation

RISC-V: Probe misaligned access speed in parallel

PATCH Applied Evan Green 4 participants 5 messages Nov 6, 2023 – Nov 8, 2023 RISC-V

lore
Share & tools
lore

Evan Green

author
#0 lore raw

PATCH v3 1/1 RISC-V: Probe misaligned access speed in parallel

Probing for misaligned access speed takes about 0.06 seconds. On a system with 64 cores, doing this in smp_callin() means it's done serially, extending boot time by 3.8 seconds. That's a lot of boot time.

Instead of measuring each CPU serially, let's do the measurements on all CPUs in parallel. If we disable preemption on all CPUs, the jiffies stop ticking, so we can do this in stages of 1) everybody except core 0, then 2) core 0. The allocations are all done outside of on_each_cpu() to avoid calling alloc_pages() with interrupts disabled.

For hotplugged CPUs that come in after the boot time measurement, register CPU hotplug callbacks, and do the measurement there. Interrupts are enabled in those callbacks, so they're fine to do alloc_pages() in.

---

Changes in v3:
- Avoid alloc_pages() with interrupts disabled (Sebastien)
- Use cpuhp callbacks instead of hooking into smp_callin() (Sebastien).
- Move cached answer check in check_unaligned_access() out to the hotplug callback, both to save the work of a useless allocation, and since check_unaligned_access_emulated() resets the answer to unknown.

Changes in v2:
- Removed new global, used system_state == SYSTEM_RUNNING instead (Jisheng)
- Added tags

 arch/riscv/include/asm/cpufeature.h |  1 -
 arch/riscv/kernel/cpufeature.c      | 96 +++++++++++++++++++++++------
 arch/riscv/kernel/smpboot.c         |  1 -
 3 files changed, 77 insertions(+), 21 deletions(-)
arch/riscv/include/asm/cpufeature.h +0 -1
index 7f1e46a9d445..69f2cae96f0b 100644
@@ -30,7 +30,6 @@ DECLARE_PER_CPU(long, misaligned_access_speed);
 /* Per-cpu ISA extensions. */
 extern struct riscv_isainfo hart_isa[NR_CPUS];
 
-void check_unaligned_access(int cpu);
 void riscv_user_isa_enable(void);
 
 #ifdef CONFIG_RISCV_MISALIGNED
arch/riscv/kernel/cpufeature.c +77 -19

145 lines not on this page · raw message

arch/riscv/kernel/smpboot.c +0 -1
index d69c628c24f4..d162bf339beb 100644
@@ -247,7 +247,6 @@ asmlinkage __visible void smp_callin(void)
 	riscv_ipi_enable();
 
 	numa_add_cpu(curr_cpuid);
-	check_unaligned_access(curr_cpuid);
 	set_cpu_online(curr_cpuid, 1);
 
 	if (has_vector()) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · Evan Green

Probing for misaligned access speed takes about 0.06 seconds. On a system with 64 cores, doing this in smp_callin() means it's done serially, extending boot time by 3.8 seconds. That's a lot of boot time.

Instead of measuring each CPU serially, let's do the measurements on all CPUs in parallel. If we disable preemption on all CPUs, the jiffies stop ticking, so we can do this in stages of 1) everybody except core 0, then 2) core 0. The allocations are all done outside of on_each_cpu() to avoid calling alloc_pages() with interrupts disabled.

Quoted 3 lines

For hotplugged CPUs that come in after the boot time measurement, register CPU hotplug callbacks, and do the measurement there. Interrupts are enabled in those callbacks, so they're fine to do alloc_pages() in.

I think this is dragged out of proportion. I would do this (if needed can can't be identified by CPU-ID or so) on boot CPU only. If there is evidence/ proof/ blessing from the high RiscV council that different types of CPU cores are mixed together then this could be extended. You brought Big-Little up in the other thread. This is actually known. Same as with hyper-threads on x86, you know which CPU is the core and which hyper thread (CPU) belongs to it.
So in terms of BigLittle you _could_ limit this to one Big and one Little core instead running it on all.

But this is just my few on this. From PREEMPT_RT's point of view, the way you restructured the memory allocation should work now.

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +0 -0
index 6a01ded615cd..fe59e18dbd5b 100644

…

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 681

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +6 -2
-static int __init check_unaligned_access_boot_cpu(void)
+/* Measure unaligned access on all CPUs present at boot in parallel. */
+static int check_unaligned_access_all_cpus(void)
 {
-	check_unaligned_access(0);
+	unsigned int cpu;
+	unsigned int cpu_count = num_possible_cpus();
+	struct page **bufs = kzalloc(cpu_count * sizeof(struct page *),
+				     GFP_KERNEL);

kcalloc(). For beauty reasons you could try a reverse xmas tree.

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+
+	if (!bufs) {
+		pr_warn("Allocation failure, not measuring misaligned performance\n");
+		return 0;
+	}
+
+	/*
+	 * Allocate separate buffers for each CPU so there's no fighting over
+	 * cache lines.
+	 */

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 701

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+	for_each_cpu(cpu, cpu_online_mask) {
+		bufs[cpu] = alloc_pages(GFP_KERNEL, MISALIGNED_BUFFER_ORDER);
+		if (!bufs[cpu]) {
+			pr_warn("Allocation failure, not measuring misaligned performance\n");
+			goto out;
+		}
+	}
+
+	/* Check everybody except 0, who stays behind to tend jiffies. */
+	on_each_cpu(check_unaligned_access_nonboot_cpu, bufs, 1);

comments! _HOW_ do you ensure that CPU0 is left out? You don't. CPU0 does this and the leaves which is a waste. Using on_each_cpu_cond() could deal with this. And you have the check within the wrapper (check_unaligned_access_nonboot_cpu()) anyway.

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 704

Quoted 2 lines
arch/riscv/kernel/cpufeature.c +2 -0
+	/* Check core 0. */
+	smp_call_on_cpu(0, check_unaligned_access, bufs[0], true);

Now that comment is obvious. If you want to add a comment, why not state why CPU0 has to be done last?

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 708

Quoted 4 lines
arch/riscv/kernel/cpufeature.c +4 -0
+
+	/* Setup hotplug callback for any new CPUs that come online. */
+	cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "riscv:online",
+				  riscv_online_cpu, NULL);

Instead riscv:online you could use riscv:unaliged_check or something that pin points the callback to something obvious. This is exported via sysfs.

Again, comment is obvious. For that to make sense would require RiscV to support physical-hotplug. For KVM like environment (where you can plug in CPUs later) this probably doesn't make sense at all. Why not? Because

- without explicit CPU pinning your slow/ fast CPU mapping (host <-> guest) could change if the scheduler on the host moves the threads around.

- without explicit task offload and resource partitioning on the host your guest thread might get interrupt during the measurement. This is done during boot so chances are high that it runs 100% of its time slice and will be preempted once other tasks on the host ask for CPU run time.

Both points mean, that the results may not be accurate over time. Maybe a KVM-hint if this is so important (along with other restrictions).

Quoted 7 lines
arch/riscv/kernel/cpufeature.c +6 -0
+
+out:
 	unaligned_emulation_finish();
+	for_each_cpu(cpu, cpu_online_mask) {
+		if (bufs[cpu])
+			__free_pages(bufs[cpu], MISALIGNED_BUFFER_ORDER);
+	}

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 724

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +3 -1
+
+	kfree(bufs);
 	return 0;
 }
 
-arch_initcall(check_unaligned_access_boot_cpu);
+arch_initcall(check_unaligned_access_all_cpus);
 
 void riscv_user_isa_enable(void)
 {

Sebastian

author
#2 lore raw
Quoted 16 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Evan Green

Probing for misaligned access speed takes about 0.06 seconds. On a system with 64 cores, doing this in smp_callin() means it's done serially, extending boot time by 3.8 seconds. That's a lot of boot time.

Instead of measuring each CPU serially, let's do the measurements on all CPUs in parallel. If we disable preemption on all CPUs, the jiffies stop ticking, so we can do this in stages of 1) everybody except core 0, then 2) core 0. The allocations are all done outside of on_each_cpu() to avoid calling alloc_pages() with interrupts disabled.

Quoted 3 lines

For hotplugged CPUs that come in after the boot time measurement, register CPU hotplug callbacks, and do the measurement there. Interrupts are enabled in those callbacks, so they're fine to do alloc_pages() in.

Quoted 9 lines

I think this is dragged out of proportion. I would do this (if needed can can't be identified by CPU-ID or so) on boot CPU only. If there is evidence/ proof/ blessing from the high RiscV council that different types of CPU cores are mixed together then this could be extended. You brought Big-Little up in the other thread. This is actually known. Same as with hyper-threads on x86, you know which CPU is the core and which hyper thread (CPU) belongs to it.
So in terms of BigLittle you _could_ limit this to one Big and one Little core instead running it on all.

Doing it on one per cluster might also happen to work, but I still see nothing that prevents variety within a cluster, so I'm not comfortable with that assumption. It also doesn't buy much. I'm not sure what kind of guidance RVI is providing on integrating multiple CPUs into a system. I haven't seen any myself, but am happy to reassess if there's documentation banning the scenarios I'm imagining.

Quoted 3 lines

But this is just my few on this. From PREEMPT_RT's point of view, the way you restructured the memory allocation should work now.

Thanks!

Quoted 65 lines
Quoted 11 lines
arch/riscv/kernel/cpufeature.c +0 -0
index 6a01ded615cd..fe59e18dbd5b 100644

…

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 681

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +6 -2
-static int __init check_unaligned_access_boot_cpu(void)
+/* Measure unaligned access on all CPUs present at boot in parallel. */
+static int check_unaligned_access_all_cpus(void)
 {
-     check_unaligned_access(0);
+     unsigned int cpu;
+     unsigned int cpu_count = num_possible_cpus();
+     struct page **bufs = kzalloc(cpu_count * sizeof(struct page *),
+                                  GFP_KERNEL);

kcalloc(). For beauty reasons you could try a reverse xmas tree.

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+
+     if (!bufs) {
+             pr_warn("Allocation failure, not measuring misaligned performance\n");
+             return 0;
+     }
+
+     /*
+      * Allocate separate buffers for each CPU so there's no fighting over
+      * cache lines.
+      */

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 701

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+     for_each_cpu(cpu, cpu_online_mask) {
+             bufs[cpu] = alloc_pages(GFP_KERNEL, MISALIGNED_BUFFER_ORDER);
+             if (!bufs[cpu]) {
+                     pr_warn("Allocation failure, not measuring misaligned performance\n");
+                     goto out;
+             }
+     }
+
+     /* Check everybody except 0, who stays behind to tend jiffies. */
+     on_each_cpu(check_unaligned_access_nonboot_cpu, bufs, 1);

comments! _HOW_ do you ensure that CPU0 is left out? You don't. CPU0 does this and the leaves which is a waste. Using on_each_cpu_cond() could deal with this. And you have the check within the wrapper (check_unaligned_access_nonboot_cpu()) anyway.

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 704

Quoted 2 lines
arch/riscv/kernel/cpufeature.c +2 -0
+     /* Check core 0. */
+     smp_call_on_cpu(0, check_unaligned_access, bufs[0], true);

Now that comment is obvious. If you want to add a comment, why not state why CPU0 has to be done last?

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 708

Quoted 4 lines
arch/riscv/kernel/cpufeature.c +4 -0
+
+     /* Setup hotplug callback for any new CPUs that come online. */
+     cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "riscv:online",
+                               riscv_online_cpu, NULL);

Instead riscv:online you could use riscv:unaliged_check or something that pin points the callback to something obvious. This is exported via sysfs.

Quoted 7 lines

Again, comment is obvious. For that to make sense would require RiscV to support physical-hotplug. For KVM like environment (where you can plug in CPUs later) this probably doesn't make sense at all. Why not? Because

- without explicit CPU pinning your slow/ fast CPU mapping (host <-> guest) could change if the scheduler on the host moves the threads around.

Taking a system with non-identical cores and allowing vcpus to bounce between them sounds like a hypervisor configuration issue to me, regardless of this patch.

Quoted 6 lines

- without explicit task offload and resource partitioning on the host your guest thread might get interrupt during the measurement. This is done during boot so chances are high that it runs 100% of its time slice and will be preempted once other tasks on the host ask for CPU run time.

The measurement takes the best (lowest time) iteration. So unless every iteration gets interrupted, I should get a good read in there somewhere.
-Evan

maintainer
#3 lore raw
Quoted 28 lines · Evan Green
Quoted 16 lines · Sebastian Andrzej Siewior
Quoted 10 lines · Evan Green

Probing for misaligned access speed takes about 0.06 seconds. On a system with 64 cores, doing this in smp_callin() means it's done serially, extending boot time by 3.8 seconds. That's a lot of boot time.

Instead of measuring each CPU serially, let's do the measurements on all CPUs in parallel. If we disable preemption on all CPUs, the jiffies stop ticking, so we can do this in stages of 1) everybody except core 0, then 2) core 0. The allocations are all done outside of on_each_cpu() to avoid calling alloc_pages() with interrupts disabled.

Quoted 3 lines

For hotplugged CPUs that come in after the boot time measurement, register CPU hotplug callbacks, and do the measurement there. Interrupts are enabled in those callbacks, so they're fine to do alloc_pages() in.

Quoted 9 lines

I think this is dragged out of proportion. I would do this (if needed can can't be identified by CPU-ID or so) on boot CPU only. If there is evidence/ proof/ blessing from the high RiscV council that different types of CPU cores are mixed together then this could be extended. You brought Big-Little up in the other thread. This is actually known. Same as with hyper-threads on x86, you know which CPU is the core and which hyper thread (CPU) belongs to it.
So in terms of BigLittle you _could_ limit this to one Big and one Little core instead running it on all.

Quoted 6 lines

Doing it on one per cluster might also happen to work, but I still see nothing that prevents variety within a cluster, so I'm not comfortable with that assumption. It also doesn't buy much. I'm not sure what kind of guidance RVI is providing on integrating multiple CPUs into a system. I haven't seen any myself, but am happy to reassess if there's documentation banning the scenarios I'm imagining.

IIUC there's pretty much no rules here, and vendors are already building wacky systems (the K230 just showed up with heterogenous-ISA cores, we've got a handful now). I guess we could write up some guidance in Documentation/riscv describing what sort of systems we generally test on, but given how RISC-V generally goes vendors are just going to build the crazy stuff anyway and we'll have to deal with it.

Quoted 91 lines
Quoted 4 lines

But this is just my few on this. From PREEMPT_RT's point of view, the way you restructured the memory allocation should work now.

Thanks!

Quoted 65 lines
Quoted 11 lines
arch/riscv/kernel/cpufeature.c +0 -0
index 6a01ded615cd..fe59e18dbd5b 100644

…

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 681

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +6 -2
-static int __init check_unaligned_access_boot_cpu(void)
+/* Measure unaligned access on all CPUs present at boot in parallel. */
+static int check_unaligned_access_all_cpus(void)
 {
-     check_unaligned_access(0);
+     unsigned int cpu;
+     unsigned int cpu_count = num_possible_cpus();
+     struct page **bufs = kzalloc(cpu_count * sizeof(struct page *),
+                                  GFP_KERNEL);

kcalloc(). For beauty reasons you could try a reverse xmas tree.

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+
+     if (!bufs) {
+             pr_warn("Allocation failure, not measuring misaligned performance\n");
+             return 0;
+     }
+
+     /*
+      * Allocate separate buffers for each CPU so there's no fighting over
+      * cache lines.
+      */

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 701

Quoted 10 lines
arch/riscv/kernel/cpufeature.c +10 -0
+     for_each_cpu(cpu, cpu_online_mask) {
+             bufs[cpu] = alloc_pages(GFP_KERNEL, MISALIGNED_BUFFER_ORDER);
+             if (!bufs[cpu]) {
+                     pr_warn("Allocation failure, not measuring misaligned performance\n");
+                     goto out;
+             }
+     }
+
+     /* Check everybody except 0, who stays behind to tend jiffies. */
+     on_each_cpu(check_unaligned_access_nonboot_cpu, bufs, 1);

comments! _HOW_ do you ensure that CPU0 is left out? You don't. CPU0 does this and the leaves which is a waste. Using on_each_cpu_cond() could deal with this. And you have the check within the wrapper (check_unaligned_access_nonboot_cpu()) anyway.

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 704

Quoted 2 lines
arch/riscv/kernel/cpufeature.c +2 -0
+     /* Check core 0. */
+     smp_call_on_cpu(0, check_unaligned_access, bufs[0], true);

Now that comment is obvious. If you want to add a comment, why not state why CPU0 has to be done last?

arch/riscv/kernel/cpufeature.c void check_unaligned_access(int cpu) · line 708

Quoted 4 lines
arch/riscv/kernel/cpufeature.c +4 -0
+
+     /* Setup hotplug callback for any new CPUs that come online. */
+     cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "riscv:online",
+                               riscv_online_cpu, NULL);

Instead riscv:online you could use riscv:unaliged_check or something that pin points the callback to something obvious. This is exported via sysfs.

Quoted 7 lines

Again, comment is obvious. For that to make sense would require RiscV to support physical-hotplug. For KVM like environment (where you can plug in CPUs later) this probably doesn't make sense at all. Why not? Because

- without explicit CPU pinning your slow/ fast CPU mapping (host <-> guest) could change if the scheduler on the host moves the threads around.

Taking a system with non-identical cores and allowing vcpus to bounce between them sounds like a hypervisor configuration issue to me, regardless of this patch.

Quoted 6 lines

- without explicit task offload and resource partitioning on the host your guest thread might get interrupt during the measurement. This is done during boot so chances are high that it runs 100% of its time slice and will be preempted once other tasks on the host ask for CPU run time.

Quoted 4 lines

The measurement takes the best (lowest time) iteration. So unless every iteration gets interrupted, I should get a good read in there somewhere.
-Evan

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#4 lore raw
In reply to Evan Green
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 11 lines · On Mon, 6 Nov 2023 14:58:55 -0800 you wrote:

Probing for misaligned access speed takes about 0.06 seconds. On a system with 64 cores, doing this in smp_callin() means it's done serially, extending boot time by 3.8 seconds. That's a lot of boot time.

Instead of measuring each CPU serially, let's do the measurements on all CPUs in parallel. If we disable preemption on all CPUs, the jiffies stop ticking, so we can do this in stages of 1) everybody except core 0, then 2) core 0. The allocations are all done outside of on_each_cpu() to avoid calling alloc_pages() with interrupts disabled.

[...]

Here is the summary with links:
- [v3] RISC-V: Probe misaligned access speed in parallel

    https://git.kernel.org/riscv/c/55e0bf49a0d0

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html