Skip to contentSkip to conversation

powerpc64: Preempt count optimizations

RFC In review Shrikanth Hegde 2 participants 6 messages Oct 2 – Nov 20 PowerPC

lore
Share & tools
lore

Shrikanth Hegde

author reviewer
#0 lore raw

cover powerpc64: Preempt count optimizations

This is an RFC and initial implementation of the idea which was discussed in few threads. [1][2][3]

It is lightly tested, but given changes are involving the core api, it definitely needs more testing. Also I need to investigate the patches in more detail and likely changelogs needs update too.

Please go ahead and try to see if works/breaks for you.

Ideas in brief:

1. Move preempt count to PACA.
preempt count in PACA is better as it make it direct access from r13 instead of obtaining from thread info.

2. Fold need_resched into preempt count.
Also it was noted that adopting similar style of folding need_resched into preempt count as ARM64 is better as it helps load-store archs and helps to avoid accessing thread info in fast paths.

3. Enable HAS_SEPARATE_PREEMPT_RESCHED_BITS.
While there enable the HAS_SEPARATE_PREEMPT_RESCHED_BITS which is a recent trick to fold the NMI bits into preempt count(29-32bits) instead of using separate per_cpu count.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/#t
[3] https://lore.kernel.org/all/[email protected]/

Below are hackbench results.

Test Baseline Patched Gain
=============================================================

Hackbench process 10 groups         1.000x    0.991x   +0.88%
Hackbench process 20 groups         1.000x    0.972x   +2.81%
Hackbench process 30 groups         1.000x    0.976x   +2.37%
Hackbench process 40 groups         1.000x    0.979x   +2.11%
Hackbench process 50 groups         1.000x    0.980x   +2.05%
Hackbench process 60 groups         1.000x    0.982x   +1.79%
Hackbench thread 10  groups         1.000x    0.978x   +2.23%
Hackbench thread 20  groups         1.000x    0.971x   +2.93%
Hackbench process pipe 10 groups    1.000x    0.957x   +4.35%
Hackbench process pipe 20 groups    1.000x    0.977x   +2.32%
Hackbench process pipe 30 groups    1.000x    0.981x   +1.95%
Hackbench process pipe 40 groups    1.000x    0.981x   +1.93%
Hackbench process pipe 50 groups    1.000x    0.975x   +2.54%
Hackbench process pipe 60 groups    1.000x    1.024x   -2.37%
Hackbench thread pipe 10 groups     1.000x    0.970x   +3.04%
Hackbench thread pipe 20 groups     1.000x    0.981x   +1.89%

Things tested(no warnings/crash):
- Build and boot.
- hackbench run.
- daytrader run.
- kexec.

Shrikanth Hegde (3):
powerpc64: move preempt count to paca powerpc64/preempt: Add need_resched bit inside preempt_count powerpc64/Kconfig: Enable HAS_SEPARATE_PREEMPT_RESCHED_BITS

 arch/powerpc/Kconfig                   |   1 +
 arch/powerpc/include/asm/paca.h        |  12 +++
 arch/powerpc/include/asm/preempt.h     | 113 +++++++++++++++++++++++++
 arch/powerpc/include/asm/thread_info.h |  14 ++-
 arch/powerpc/kernel/paca.c             |   1 +
 arch/powerpc/kexec/core_64.c           |   2 +-
 6 files changed, 139 insertions(+), 4 deletions(-)
 create mode 100644 arch/powerpc/include/asm/preempt.h
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

RFC 1/3 powerpc64: move preempt count to paca

PowerPC uses generic implementation of preempt as of now. That means preempt count in embedded in thread_info.

preempt_count = current_thread_info->preempt_count.

Current is first obtained from PACA. So this involves multiple cachelines/pointers access. But preempt count is a per cpu storage attribute indicating whether preemption can happen on this CPU. per cpu storage has been used in other archs such x86 already.

Powerpc64 architecture using PACA instead seems to be a more natural fit. r13 is local paca pointer and it is always cacheline hot.

So getting preempt_count from paca means avoiding two deferrences. It is worth doing that since preempt count is very high frequency operation involved in various kernel primitives.

Given that after 7.0 powerpc has only preemptible kernels, makes it even more relevant.

No functional change is intended.

---
 arch/powerpc/include/asm/paca.h        |   1 +
 arch/powerpc/include/asm/preempt.h     | 103 +++++++++++++++++++++++++
 arch/powerpc/include/asm/thread_info.h |  14 +++-
 arch/powerpc/kernel/paca.c             |   1 +
 arch/powerpc/kexec/core_64.c           |   2 +-
 5 files changed, 117 insertions(+), 4 deletions(-)
 create mode 100644 arch/powerpc/include/asm/preempt.h
arch/powerpc/include/asm/paca.h +1 -0
index 1d58da946739..0aa729c2faf5 100644
@@ -164,6 +164,7 @@ struct paca_struct {
 	u64 saved_r1;			/* r1 save for RTAS calls or PM or EE=0 */
 	u64 saved_msr;			/* MSR saved here by enter_rtas */
 	u64 exit_save_r1;		/* Syscall/interrupt R1 save */
+	int preempt_count;
 #ifdef CONFIG_PPC_BOOK3E_64
 	u16 trap_save;			/* Used when bad stack is encountered */
 #endif
arch/powerpc/include/asm/preempt.h new file +103 -0

103 lines not on this page · raw message

arch/powerpc/include/asm/thread_info.h +11 -3
index 1e069a2e7ce8..89fd63442f7a 100644
@@ -52,8 +52,9 @@
  * low level task data.
  */
 struct thread_info {
-	int		preempt_count;		/* 0 => preemptable,
-						   <0 => BUG */
+#ifndef CONFIG_PPC64
+	int		preempt_count;
+#endif
 #ifdef CONFIG_SMP
 	unsigned int	cpu;
 #endif
@@ -74,12 +75,19 @@ struct thread_info {
 	unsigned long	flags ____cacheline_aligned_in_smp;
 };
 
+#ifdef CONFIG_PPC64
+#define INIT_THREAD_INFO_PREEMPT_COUNT
+#else
+#define INIT_THREAD_INFO_PREEMPT_COUNT \
+	.preempt_count = INIT_PREEMPT_COUNT,
+#endif
+
 /*
  * macros/functions for gaining access to the thread information structure
  */
 #define INIT_THREAD_INFO(tsk)			\
 {						\
-	.preempt_count = INIT_PREEMPT_COUNT,	\
+	INIT_THREAD_INFO_PREEMPT_COUNT		\
 	.flags =	0,			\
 }
 
arch/powerpc/kernel/paca.c +1 -0
index 7502066c3c53..f19d057c37b3 100644
@@ -200,6 +200,7 @@ void __init initialise_paca(struct paca_struct *new_paca, int cpu)
 	new_paca->hw_cpu_id = 0xffff;
 	new_paca->kexec_state = KEXEC_STATE_NONE;
 	new_paca->__current = &init_task;
+	new_paca->preempt_count = PREEMPT_DISABLED;
 	new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
 #ifdef CONFIG_PPC_64S_HASH_MMU
 	new_paca->slb_shadow_ptr = NULL;
arch/powerpc/kexec/core_64.c +1 -1
index 58c13a59b93b..f3954695e0b5 100644
@@ -352,7 +352,7 @@ void default_machine_kexec(struct kimage *image)
 	 * XXX: the task struct will likely be invalid once we do the copy!
 	 */
 	current_thread_info()->flags = 0;
-	current_thread_info()->preempt_count = HARDIRQ_OFFSET;
+	WRITE_ONCE(local_paca->preempt_count, HARDIRQ_OFFSET);
 
 	/* We need a static PACA, too; copy this CPU's PACA over and switch to
 	 * it. Also poison per_cpu_offset and NULL lppaca to catch anyone using
Signature · 1 line
2.52.0
author reviewer
#2 lore raw

RFC 2/3 powerpc64/preempt: Add need_resched bit inside preempt_count

In reply to Shrikanth Hegde

Current implementation doesn't make use of the PREEMPT_NEED_RESCHED flag, since this can interact badly with load/store architectures which rely on the preempt_count word being unchanged across an interrupt.

On PPC64 which is 64 bit and the preempt count is only 32 bits wide, one can simply pack it next to the resched flag and load the whole thing in one go, so that a dec-and-test operation doesn't need to check tif.

[Adopted from arm64 implementation, including changelog bits]
---
 arch/powerpc/include/asm/paca.h    | 13 +++++++-
 arch/powerpc/include/asm/preempt.h | 52 ++++++++++++++++++------------
 arch/powerpc/kexec/core_64.c       |  2 +-
 3 files changed, 44 insertions(+), 23 deletions(-)
arch/powerpc/include/asm/paca.h +12 -1
index 0aa729c2faf5..a73e214bd6f3 100644
@@ -164,7 +164,18 @@ struct paca_struct {
 	u64 saved_r1;			/* r1 save for RTAS calls or PM or EE=0 */
 	u64 saved_msr;			/* MSR saved here by enter_rtas */
 	u64 exit_save_r1;		/* Syscall/interrupt R1 save */
-	int preempt_count;
+	union {
+		u64		preempt_count;
+		struct {
+#ifdef __BIG_ENDIAN__
+			u32	need_resched;
+			u32	count;
+#else
+			u32	count;
+			u32	need_resched;
+#endif
+		} preempt;
+	};
 #ifdef CONFIG_PPC_BOOK3E_64
 	u16 trap_save;			/* Used when bad stack is encountered */
 #endif
arch/powerpc/include/asm/preempt.h +31 -21

114 lines not on this page · raw message

arch/powerpc/kexec/core_64.c +1 -1
index f3954695e0b5..0f17c75cee28 100644
@@ -352,7 +352,7 @@ void default_machine_kexec(struct kimage *image)
 	 * XXX: the task struct will likely be invalid once we do the copy!
 	 */
 	current_thread_info()->flags = 0;
-	WRITE_ONCE(local_paca->preempt_count, HARDIRQ_OFFSET);
+	WRITE_ONCE(local_paca->preempt.count, HARDIRQ_OFFSET);
 
 	/* We need a static PACA, too; copy this CPU's PACA over and switch to
 	 * it. Also poison per_cpu_offset and NULL lppaca to catch anyone using
Signature · 1 line
2.52.0
author reviewer
#3 lore raw

RFC 3/3 powerpc64/Kconfig: Enable HAS_SEPARATE_PREEMPT_RESCHED_BITS

In reply to Shrikanth Hegde

Now there is separate need resched bit, enable the generic config of HAS_SEPARATE_PREEMPT_RESCHED_BITS. This would make use of higher bits of preempt count(29-32bits) for NMI bits.

---
 arch/powerpc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index 143381d267ce..001bb2f3eec1 100644
@@ -215,6 +215,7 @@ config PPC
 	select GENERIC_PCI_IOMAP		if PCI
 	select GENERIC_SMP_IDLE_THREAD
 	select HAS_IOPORT			if PCI
+	select HAS_SEPARATE_PREEMPT_RESCHED_BITS	if PPC64
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_ARCH_HUGE_VMALLOC		if HAVE_ARCH_HUGE_VMAP
 	select HAVE_ARCH_HUGE_VMAP		if PPC_RADIX_MMU || PPC_8xx
Signature · 1 line
2.52.0
author reviewer
#4 lore raw
In reply to Shrikanth Hegde

venkat, Samir,

Could you run CI suite, or LTP test suite and your standard set of tests to see if it holding good functionally?

Quoted 70 lines · Shrikanth Hegde

This is an RFC and initial implementation of the idea which was discussed in few threads. [1][2][3]

It is lightly tested, but given changes are involving the core api, it definitely needs more testing. Also I need to investigate the patches in more detail and likely changelogs needs update too.

Please go ahead and try to see if works/breaks for you.

Ideas in brief:

1. Move preempt count to PACA.
preempt count in PACA is better as it make it direct access from r13 instead of obtaining from thread info.

2. Fold need_resched into preempt count.
Also it was noted that adopting similar style of folding need_resched into preempt count as ARM64 is better as it helps load-store archs and helps to avoid accessing thread info in fast paths.

3. Enable HAS_SEPARATE_PREEMPT_RESCHED_BITS.
While there enable the HAS_SEPARATE_PREEMPT_RESCHED_BITS which is a recent trick to fold the NMI bits into preempt count(29-32bits) instead of using separate per_cpu count.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/#t
[3] https://lore.kernel.org/all/[email protected]/

Below are hackbench results.

Test Baseline Patched Gain
=============================================================

Hackbench process 10 groups         1.000x    0.991x   +0.88%
Hackbench process 20 groups         1.000x    0.972x   +2.81%
Hackbench process 30 groups         1.000x    0.976x   +2.37%
Hackbench process 40 groups         1.000x    0.979x   +2.11%
Hackbench process 50 groups         1.000x    0.980x   +2.05%
Hackbench process 60 groups         1.000x    0.982x   +1.79%
Hackbench thread 10  groups         1.000x    0.978x   +2.23%
Hackbench thread 20  groups         1.000x    0.971x   +2.93%
Hackbench process pipe 10 groups    1.000x    0.957x   +4.35%
Hackbench process pipe 20 groups    1.000x    0.977x   +2.32%
Hackbench process pipe 30 groups    1.000x    0.981x   +1.95%
Hackbench process pipe 40 groups    1.000x    0.981x   +1.93%
Hackbench process pipe 50 groups    1.000x    0.975x   +2.54%
Hackbench process pipe 60 groups    1.000x    1.024x   -2.37%
Hackbench thread pipe 10 groups     1.000x    0.970x   +3.04%
Hackbench thread pipe 20 groups     1.000x    0.981x   +1.89%

Things tested(no warnings/crash):
- Build and boot.
- hackbench run.
- daytrader run.
- kexec.

Shrikanth Hegde (3):
powerpc64: move preempt count to paca powerpc64/preempt: Add need_resched bit inside preempt_count powerpc64/Kconfig: Enable HAS_SEPARATE_PREEMPT_RESCHED_BITS

  arch/powerpc/Kconfig                   |   1 +
  arch/powerpc/include/asm/paca.h        |  12 +++
  arch/powerpc/include/asm/preempt.h     | 113 +++++++++++++++++++++++++
  arch/powerpc/include/asm/thread_info.h |  14 ++-
  arch/powerpc/kernel/paca.c             |   1 +
  arch/powerpc/kexec/core_64.c           |   2 +-
  6 files changed, 139 insertions(+), 4 deletions(-)

create mode 100644 arch/powerpc/include/asm/preempt.h

#5 lore raw

Hi Shrikanth,

Thanks for the RFC and for the pointer. We ran it on our POWER10 box.

Short summary: functionally clean, but we could not reproduce the performance gains, and the stress-ng kill regression from the ppc64le dynamic-preemption discussion [1] does not improve.

## Setup

Same-tree A/B on ibm-p10-01-lp13 (IBM 9080-HEX, POWER10, pHyp LPAR, 1 core SMT-8, 64 GiB, ppc64le):

- baseline: v7.4-rc3
- patched: v7.4-rc3 + this series (git am, applies cleanly)
- config: seeded from our ELN ppc64le config:
CONFIG_PREEMPT_DYNAMIC=y,
CONFIG_PREEMPT_RCU=y; dynamic preempt default resolves to "lazy" on boot
- SELinux enforcing, tuned virtual-guest, stress-ng 0.21.03
- interleaved rounds, full reboots between all measurements (n=27/kernel for stress-ng, n=20/config for hackbench)

## Functional: clean

Both kernels boot and run without any dmesg findings. No scheduling-while-atomic, preempt-count imbalance, lockdep, RCU or NMI splats. The NMI-folding (HAS_SEPARATE_PREEMPT_RESCHED_BITS) and PACA preempt-count paths work under load (stress-ng, hackbench, perf).

## stress-ng --kill 1 -t 23 (the RHEL-216657 worst case)

    baseline   58,722 bogo-ops/s
    patched    57,378 bogo-ops/s   (-2.3%, 95% CI [-5.0%, +0.4%], n.s.)

No recovery of the kill regression. (Noise floor on this LPAR is CV ~7-8%, single-threaded on a shared frame. Effects below ~4% are not resolvable here.)

## hackbench (20 groups, 1000 loops)

                        baseline   patched    delta
    process, socket      3.601s     3.653s    +1.4%  (p=0.09)
    thread,  socket       3.860s     3.895s    +0.9%  (p=0.014)
    process, pipe         1.619s     1.608s    -0.7%  (p=0.60)
    thread,  pipe          1.778s     1.771s    -0.4%  (p=0.40)

We could not reproduce the 2-3% gains from your cover letter; our socket configs point the other way slightly.

## perf profile observation

Paired perf-record runs of the kill stressor show __rcu_read_lock + __rcu_read_unlock at 5.5% of cycles on baseline vs 4.5% on the patched kernel, so the RCU read-side component did get cheaper, but avc_lookup (22-24%) dominates this path and total throughput does not move. That matches our earlier root-cause analysis of the dynamic-preemption regression [1]: on this path the cost is dominated by the weak-ordering barriers rather than by the preempt_count address computation this series optimizes.

## preempt=full follow-up

Since our config defaults to "Dynamic Preempt: lazy", we also reran the full matrix with preempt=full on both kernels (same tree, same methodology, n=15/kernel kill, n=10/config hackbench):

stress-ng --kill: baseline 53,995 vs patched 53,901 bogo-ops/s
(-0.2%, flat)

hackbench: all four configs within +/-0.8%, none significant
(process/thread x socket/pipe)

So under full preemption as well, the series shows neither the gains from the cover letter nor any regression on our box. For completeness: the mode itself matters more than the patch on this workload. Kill drops from ~58.7k (lazy) to ~54.0k (full) bogo-ops/s on both kernels alike.

Thanks again! I'm happy to share raw data or rerun with different parameters.

[1] https://lore.kernel.org/all/[email protected]/

Signature · 2 lines
Jirka Hladky
Red Hat