: powerpc: Misc fixes for debug kernel splats
Subsystems (2)
Ritesh Harjani (IBM)
reviewerv2 cover : powerpc: Misc fixes for debug kernel splats
Enabling debug options like PROVE_LOCKING, DEBUG_PREEMPT and DEBUG_ATOMIC_SLEEP on powerpc64 showed these warnings.
Main motivation for me to look into this was - many folks internally reported that after generic entry/exit feature, they aren't able to use lockdep because of the warning splat it generates during boot, after which lockdep gets disabled.
Therefore this series is an attempt to fix the warnings which are generated
- when we enable these various debug config options.
v1 -> v2:
========
1. Patch-4 fixes a pre-exiting issue pointed out by Sashiko.
2. Patch-2 and patch-3 from previous revision is re-arranged.
3. Patch-2 change smp_processor_id() to raw_smp_processor_id() since preemption is already off - (From Shrikanth)
(Sorry about a quick v2 re-spin. The main reason for this was, a new patch-4 which Sashiko pointed out as a pre-existing issue).
Ritesh Harjani (IBM) (4):
powerpc/64: Leave lockdep hardirq context around replayed interrupts powerpc/watchdog: Disable preemption in arch_touch_nmi_watchdog() powerpc/kvm-hv-pmu: Don't allocate gsm/gsb GFP_KERNEL allocations under spinlock powerpc/kvm-hv-pmu: Free hostwide stats buffers if perf_pmu_register() fails
arch/powerpc/kernel/irq_64.c | 11 +++--- arch/powerpc/kernel/watchdog.c | 8 +++- arch/powerpc/perf/kvm-hv-pmu.c | 69 ++++++++++++++++------------------ 3 files changed, 44 insertions(+), 44 deletions(-)
--
2.39.5
Ritesh Harjani (IBM)
reviewerPATCH v2 1/4 powerpc/64: Leave lockdep hardirq context around replayed interrupts
With PROVE_LOCKING, boot warns once and lockdep turns itself off:
DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context())
WARNING: kernel/locking/lockdep.c:4448 at lockdep_hardirqs_on_prepare+0x22c/0x2d4, CPU#0: swapper/0/0
CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 7.4.0-rc2-00027-g074831f06e18-dirty #123 PREEMPT
NIP: c0000000002b2f2c LR: c0000000002b2f28 CTR: c000000002426f90
REGS: c000000004f075c0 TRAP: 0700 Not tainted (7.4.0-rc2-00027-g074831f06e18-dirty)
MSR: 8000000002021033 <SF,VEC,ME,IR,DR,RI,LE> CR: 48022221 XER: 20040000
NIP [c0000000002b2f2c] lockdep_hardirqs_on_prepare+0x22c/0x2d4
LR [c0000000002b2f28] lockdep_hardirqs_on_prepare+0x228/0x2d4
Call Trace:
lockdep_hardirqs_on_prepare+0x228/0x2d4 (unreliable)
trace_hardirqs_on+0x98/0x11c
irqentry_exit+0x64c/0x1064
timer_interrupt+0x274/0x6bc
__replay_soft_interrupts+0x158/0x380
arch_local_irq_restore+0x1cc/0x244
console_flush_one_record+0x438/0x654
console_unlock+0xb8/0x1e0
vprintk_emit+0x300/0x56c
vprintk+0x20/0x34
_printk+0x40/0x54
lockdep_init+0xdc/0x104
start_kernel+0xa24/0xc68
start_here_common+0x1c/0x20Since commit bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature") a replayed handler always returns via irqentry_exit(), which traces hardirqs on while the replay is still inside irq_enter():
arch_local_irq_restore()
irq_enter() hardirq_context = 1
__replay_soft_interrupts()
timer_interrupt(®s)
irq_enter_rcu() hardirq_context = 2
irq_exit_rcu() hardirq_context = 1
irqentry_exit()
trace_hardirqs_on() hardirq_context != 0 -> WARN
next_interrupt()
lockdep_hardirq_exit() too late
Leave the hardirq context before each replayed handler instead of after it:
arch_local_irq_restore()
irq_enter() hardirq_context = 1
__replay_soft_interrupts()
lockdep_hardirq_exit() hardirq_context = 0 // fix
timer_interrupt(®s)
irq_enter_rcu() hardirq_context = 1
irq_exit_rcu() hardirq_context = 0
irqentry_exit()
trace_hardirqs_on() ok
next_interrupt()
trace_hardirqs_off()
lockdep_hardirq_enter() hardirq_context = 1
irq_exit() hardirq_context = 0
--- arch/powerpc/kernel/irq_64.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
arch/powerpc/kernel/irq_64.c +5 -6
@@ -75,12 +75,6 @@ static inline void next_interrupt(struct pt_regs *regs)WARN_ON (irq_soft_mask_return () !=IRQS_ALL_DISABLED ); } -/* -* We are responding to the next interrupt, so interrupt-off -* latencies should be reset here. -*/ -lockdep_hardirq_exit (); -trace_hardirqs_on ();trace_hardirqs_off ();lockdep_hardirq_enter (); }
@@ -137,18 +131,21 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_BOOK3S ) &&irq_happened_test_and_clear (PACA_IRQ_HMI )) { regs.trap =INTERRUPT_HMI ; +lockdep_hardirq_exit ();handle_hmi_exception (®s);next_interrupt (®s); }if (irq_happened_test_and_clear (PACA_IRQ_DEC )) { regs.trap =INTERRUPT_DECREMENTER ; +lockdep_hardirq_exit ();timer_interrupt (®s);next_interrupt (®s); }if (irq_happened_test_and_clear (PACA_IRQ_EE )) { regs.trap =INTERRUPT_EXTERNAL ; +lockdep_hardirq_exit ();do_IRQ (®s);next_interrupt (®s); }
@@ -156,6 +153,7 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_DOORBELL ) &&irq_happened_test_and_clear (PACA_IRQ_DBELL )) { regs.trap =INTERRUPT_DOORBELL ; +lockdep_hardirq_exit ();doorbell_exception (®s);next_interrupt (®s); }
@@ -164,6 +162,7 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_BOOK3S ) &&irq_happened_test_and_clear (PACA_IRQ_PMI )) { regs.trap =INTERRUPT_PERFMON ; +lockdep_hardirq_exit ();performance_monitor_exception (®s);next_interrupt (®s); }
--
2.39.5
Ritesh Harjani (IBM)
reviewerPATCH v2 2/4 powerpc/watchdog: Disable preemption in arch_touch_nmi_watchdog()
In reply to Ritesh Harjani (IBM)touch_nmi_watchdog() can be called from preemptible context, e.g. sysrq-w via /proc/sysrq-trigger. The powerpc implementation uses smp_processor_id(), so with DEBUG_PREEMPT we get:
root@buildroot:/# echo w > /proc/sysrq-trigger sysrq: Show Blocked State BUG: using smp_processor_id() in preemptible [00000000] code: bash/238 caller is arch_touch_nmi_watchdog+0x3c/0xd4 CPU: 0 UID: 0 PID: 238 Comm: bash Not tainted 7.4.0-rc2-00029-g7664547149ae-dirty #126 PREEMPT Call Trace: dump_stack_lvl+0x138/0x150 (unreliable) check_preemption_disabled+0x120/0x158 arch_touch_nmi_watchdog+0x3c/0xd4 show_state_filter+0xf0/0x338 sysrq_handle_showstate_blocked+0x24/0x38 __handle_sysrq+0x134/0x3a8 write_sysrq_trigger+0x178/0x22c proc_reg_write+0xf4/0x150 vfs_write+0xfc/0x71c ksys_write+0x8c/0x150 system_call_exception+0x310/0x590 system_call_vectored_common+0x15c/0x2ec ---- interrupt: 3000 at 0x7ffd01755b98
Unlike the generic version, which only sets a flag, we update the CPU's timebase stamp and its SMP watchdog pending bit, so a migration in between touches the state of a CPU we are no longer on.
Disable preemption across the update. While at it, let's also mark the function as notrace (since we might be within a tracer when this function gets called).
--- arch/powerpc/kernel/watchdog.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/powerpc/kernel/watchdog.c +6 -2
@@ -456,12 +456,16 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)return HRTIMER_RESTART ; } -void arch_touch_nmi_watchdog (void ) +notrace void arch_touch_nmi_watchdog(void) {unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms *1000 ; -int cpu =smp_processor_id (); +int cpu;u64 tb; +/* Callers may be preemptible. Stay on this CPU for the update. */ +guard (preempt_notrace)(); + cpu =raw_smp_processor_id (); +if (!cpumask_test_cpu (cpu, &watchdog_cpumask))return ;
--
2.39.5
Ritesh Harjani (IBM)
reviewerPATCH v2 3/4 powerpc/kvm-hv-pmu: Don't allocate gsm/gsb GFP_KERNEL allocations under spinlock
In reply to Ritesh Harjani (IBM)kvmppc_init_hostwide() takes lock_l0_stats with spin_lock_irqsave() and then allocates the guest state message and buffer with GFP_KERNEL. On a nested API v2 host with DEBUG_ATOMIC_SLEEP this warns at boot:
BUG: sleeping function called from invalid context at include/linux/sched/mm.h:322
in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 1, name: swapper/0
preempt_count: 1, expected: 0
RCU nest depth: 0, expected: 0
locks held by swapper/0/1: 1, on CPU#0:
#0: c000000004ad95b8 (lock_l0_stats){....}-{3:3}, at: kvmppc_register_pmu+0x50/0x270
CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.4.0-rc2-00028-ge5a8bf3a3ee9-dirty #124 PREEMPT
Call Trace:
dump_stack_lvl+0xe8/0x150 (unreliable)
__might_resched+0x190/0x30c
__kmalloc_cache_noprof+0x4dc/0x760
kvmppc_gsm_new+0x68/0xcc
kvmppc_register_pmu+0xd4/0x270
do_one_initcall+0xac/0x48c
kernel_init_freeable+0x3cc/0x7f0
kernel_init+0x3c/0x274
ret_from_kernel_user_thread+0x14/0x1cThe lock is not needed for the allocation of gsm and gsb buffers. Allocate and fill the message and buffer without the lock and only take it to publish the two global pointers.
--- arch/powerpc/perf/kvm-hv-pmu.c | 65 ++++++++++++++++------------------ 1 file changed, 30 insertions(+), 35 deletions(-)
arch/powerpc/perf/kvm-hv-pmu.c +30 -35
@@ -314,53 +314,48 @@ static struct kvmppc_gs_msg_ops gsb_ops_l0_stats = {static int kvmppc_init_hostwide (void ) { -int rc =0 ; +struct kvmppc_gs_msg *gsm; +struct kvmppc_gs_buff *gsb;unsigned long flags; +int rc; -spin_lock_irqsave (&lock_l0_stats, flags); - -/* already registered ? */ -if (gsm_l0_stats) { - rc =0 ; - goto out; - } - -/* setup the Guest state message/buffer to talk to L0 */ - gsm_l0_stats =kvmppc_gsm_new (&gsb_ops_l0_stats, &l0_stats, -GSM_SEND ,GFP_KERNEL ); -if (!gsm_l0_stats) { - rc = -ENOMEM ; - goto out; - } +/* allocate the Guest state message/buffer to talk to L0 */ + gsm =kvmppc_gsm_new (&gsb_ops_l0_stats, &l0_stats,GSM_SEND , +GFP_KERNEL ); +if (!gsm) +return -ENOMEM ;/* Populate the Idents */ -kvmppc_gsm_include (gsm_l0_stats,KVMPPC_GSID_L0_GUEST_HEAP ); -kvmppc_gsm_include (gsm_l0_stats,KVMPPC_GSID_L0_GUEST_HEAP_MAX ); -kvmppc_gsm_include (gsm_l0_stats,KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE ); -kvmppc_gsm_include (gsm_l0_stats,KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE_MAX ); -kvmppc_gsm_include (gsm_l0_stats,KVMPPC_GSID_L0_GUEST_PGTABLE_RECLAIM ); +kvmppc_gsm_include (gsm,KVMPPC_GSID_L0_GUEST_HEAP ); +kvmppc_gsm_include (gsm,KVMPPC_GSID_L0_GUEST_HEAP_MAX ); +kvmppc_gsm_include (gsm,KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE ); +kvmppc_gsm_include (gsm,KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE_MAX ); +kvmppc_gsm_include (gsm,KVMPPC_GSID_L0_GUEST_PGTABLE_RECLAIM );/* allocate GSB. Guest/Vcpu Id is ignored */ - gsb_l0_stats =kvmppc_gsb_new (kvmppc_gsm_size (gsm_l0_stats),0 ,0 , -GFP_KERNEL ); -if (!gsb_l0_stats) { + gsb =kvmppc_gsb_new (kvmppc_gsm_size (gsm),0 ,0 ,GFP_KERNEL ); +if (!gsb) { rc = -ENOMEM ; - goto out; + goto free_gsm; }/* ask the ops to fill in the info */ - rc =kvmppc_gsm_fill_info (gsm_l0_stats, gsb_l0_stats); + rc =kvmppc_gsm_fill_info (gsm, gsb); +if (rc) + goto free_gsb; -out: -if (rc) { -if (gsm_l0_stats) -kvmppc_gsm_free (gsm_l0_stats); -if (gsb_l0_stats) -kvmppc_gsb_free (gsb_l0_stats); - gsm_l0_stats =NULL ; - gsb_l0_stats =NULL ; - } +/* setup the Guest state message/buffer to talk to L0 */ +spin_lock_irqsave (&lock_l0_stats, flags); + gsm_l0_stats = gsm; + gsb_l0_stats = gsb;spin_unlock_irqrestore (&lock_l0_stats, flags); + +return 0 ; + +free_gsb: +kvmppc_gsb_free (gsb); +free_gsm: +kvmppc_gsm_free (gsm);return rc; }
Signature · 1 line
2.39.5
Ritesh Harjani (IBM)
reviewerPATCH v2 4/4 powerpc/kvm-hv-pmu: Free hostwide stats buffers if perf_pmu_register() fails
In reply to Ritesh Harjani (IBM)kvmppc_register_pmu() calls kvmppc_init_hostwide(), which allocates gsm_l0_stats and gsb_l0_stats. It then registers the pmu. If perf_pmu_register() fails we return without freeing them.
Call kvmppc_cleanup_hostwide() on that error path.
--- arch/powerpc/perf/kvm-hv-pmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/perf/kvm-hv-pmu.c +3 -1
@@ -402,8 +402,10 @@ static int __init kvmppc_register_pmu(void)/* Register the pmu */ rc =perf_pmu_register (&kvmppc_pmu, kvmppc_pmu.name,-1 ); -if (rc) +if (rc) { +kvmppc_cleanup_hostwide (); goto out; + }pr_info ("Registered kvm-hv pmu" ); }
--
2.39.5
Re: [PATCHv2 2/4] powerpc/watchdog: Disable preemption in arch_touch_nmi_watchdog()
In reply to Ritesh Harjani (IBM)Quoted 28 lines · Ritesh Harjani (IBM)
touch_nmi_watchdog()can be called from preemptible context, e.g. sysrq-w via/proc/sysrq-trigger. The powerpc implementation usessmp_processor_id(), so withDEBUG_PREEMPTwe get:root@buildroot:/# echo w > /proc/sysrq-trigger sysrq: Show Blocked State BUG: using smp_processor_id() in preemptible [00000000] code: bash/238 caller is arch_touch_nmi_watchdog+0x3c/0xd4 CPU: 0 UID: 0 PID: 238 Comm: bash Not tainted 7.4.0-rc2-00029-g7664547149ae-dirty #126 PREEMPT Call Trace: dump_stack_lvl+0x138/0x150 (unreliable) check_preemption_disabled+0x120/0x158 arch_touch_nmi_watchdog+0x3c/0xd4 show_state_filter+0xf0/0x338 sysrq_handle_showstate_blocked+0x24/0x38 __handle_sysrq+0x134/0x3a8 write_sysrq_trigger+0x178/0x22c proc_reg_write+0xf4/0x150 vfs_write+0xfc/0x71c ksys_write+0x8c/0x150 system_call_exception+0x310/0x590 system_call_vectored_common+0x15c/0x2ec ---- interrupt: 3000 at 0x7ffd01755b98Unlike the generic version, which only sets a flag, we update the CPU's timebase stamp and its SMP watchdog pending bit, so a migration in between touches the state of a CPU we are no longer on.
Quoted 7 lines
Disable preemption across the update. While at it, let's also mark the function as notrace (since we might be within a tracer when this function gets called).
Fixes 2104180a5369 ("powerpc/64s: implement arch-specific hardlockup watchdog")Signed-off-by Ritesh Harjani (IBM) <[email protected]>
Quoted 30 lines
--- arch/powerpc/kernel/watchdog.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/powerpc/kernel/watchdog.c +6
-3@@ -456,12 +456,16 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)return HRTIMER_RESTART ; } -void arch_touch_nmi_watchdog (void ) +notrace void arch_touch_nmi_watchdog(void) {unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms *1000 ; -int cpu =smp_processor_id (); +int cpu;u64 tb; +/* Callers may be preemptible. Stay on this CPU for the update. */ +guard (preempt_notrace)(); + cpu =raw_smp_processor_id (); +if (!cpumask_test_cpu (cpu, &watchdog_cpumask))return ; --2.39.5
Christophe Leroy (CS GROUP)
reviewerRe: [PATCHv2 1/4] powerpc/64: Leave lockdep hardirq context around replayed interrupts
In reply to Ritesh Harjani (IBM)Le 17/11/2026 à 15:25, Ritesh Harjani (IBM) a écrit :
Quoted 80 lines
With
PROVE_LOCKING, boot warns once and lockdep turns itself off:DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()) WARNING: kernel/locking/lockdep.c:4448 at lockdep_hardirqs_on_prepare+0x22c/0x2d4, CPU#0: swapper/0/0 CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 7.4.0-rc2-00027-g074831f06e18-dirty #123 PREEMPT NIP: c0000000002b2f2c LR: c0000000002b2f28 CTR: c000000002426f90 REGS: c000000004f075c0 TRAP: 0700 Not tainted (7.4.0-rc2-00027-g074831f06e18-dirty) MSR: 8000000002021033 <SF,VEC,ME,IR,DR,RI,LE> CR: 48022221 XER: 20040000 NIP [c0000000002b2f2c] lockdep_hardirqs_on_prepare+0x22c/0x2d4 LR [c0000000002b2f28] lockdep_hardirqs_on_prepare+0x228/0x2d4 Call Trace: lockdep_hardirqs_on_prepare+0x228/0x2d4 (unreliable) trace_hardirqs_on+0x98/0x11c irqentry_exit+0x64c/0x1064 timer_interrupt+0x274/0x6bc __replay_soft_interrupts+0x158/0x380 arch_local_irq_restore+0x1cc/0x244 console_flush_one_record+0x438/0x654 console_unlock+0xb8/0x1e0 vprintk_emit+0x300/0x56c vprintk+0x20/0x34 _printk+0x40/0x54 lockdep_init+0xdc/0x104 start_kernel+0xa24/0xc68 start_here_common+0x1c/0x20Since commit
bee25f97ad24("powerpc: EnableGENERIC_ENTRYfeature") a replayed handler always returns viairqentry_exit(), which traces hardirqs on while the replay is still insideirq_enter():
arch_local_irq_restore()
irq_enter()hardirq_context= 1
__replay_soft_interrupts()
timer_interrupt(®s)
irq_enter_rcu()hardirq_context= 2
irq_exit_rcu()hardirq_context= 1
irqentry_exit()
trace_hardirqs_on()hardirq_context!= 0 -> WARN
next_interrupt()
lockdep_hardirq_exit()too lateLeave the hardirq context before each replayed handler instead of after it:
arch_local_irq_restore()
irq_enter()hardirq_context= 1
__replay_soft_interrupts()
lockdep_hardirq_exit()hardirq_context= 0 // fix
timer_interrupt(®s)
irq_enter_rcu()hardirq_context= 1
irq_exit_rcu()hardirq_context= 0
irqentry_exit()
trace_hardirqs_on()ok
next_interrupt()
trace_hardirqs_off()
lockdep_hardirq_enter()hardirq_context= 1
irq_exit()hardirq_context= 0Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")Signed-off-by Ritesh Harjani (IBM) <[email protected]>--- arch/powerpc/kernel/irq_64.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)arch/powerpc/kernel/irq_64.c +0
-6@@ -75,12 +75,6 @@ static inline void next_interrupt(struct pt_regs *regs)WARN_ON (irq_soft_mask_return () !=IRQS_ALL_DISABLED ); } -/* -* We are responding to the next interrupt, so interrupt-off -* latencies should be reset here. -*/ -lockdep_hardirq_exit (); -trace_hardirqs_on ();trace_hardirqs_off ();lockdep_hardirq_enter (); }
arch/powerpc/kernel/irq_64.c static __no_kcsan void __replay_soft_interrupts(void) · line 134
Quoted 4 lines
arch/powerpc/kernel/irq_64.c +1
-0@@ -137,18 +131,21 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_BOOK3S ) &&irq_happened_test_and_clear (PACA_IRQ_HMI )) { regs.trap =INTERRUPT_HMI ; +lockdep_hardirq_exit ();
Can we include that in irq_happened_test_and_clear() instead of duplicating it 5 times ?
Quoted 36 lines
arch/powerpc/kernel/irq_64.c +4
-1handle_hmi_exception (®s);next_interrupt (®s); }if (irq_happened_test_and_clear (PACA_IRQ_DEC )) { regs.trap =INTERRUPT_DECREMENTER ; +lockdep_hardirq_exit ();timer_interrupt (®s);next_interrupt (®s); }if (irq_happened_test_and_clear (PACA_IRQ_EE )) { regs.trap =INTERRUPT_EXTERNAL ; +lockdep_hardirq_exit ();do_IRQ (®s);next_interrupt (®s); }@@ -156,6 +153,7 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_DOORBELL ) &&irq_happened_test_and_clear (PACA_IRQ_DBELL )) { regs.trap =INTERRUPT_DOORBELL ; +lockdep_hardirq_exit ();doorbell_exception (®s);next_interrupt (®s); }@@ -164,6 +162,7 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_BOOK3S ) &&irq_happened_test_and_clear (PACA_IRQ_PMI )) { regs.trap =INTERRUPT_PERFMON ; +lockdep_hardirq_exit ();performance_monitor_exception (®s);next_interrupt (®s); } --2.39.5
Ritesh Harjani (IBM)
reviewerRe: [PATCHv2 1/4] powerpc/64: Leave lockdep hardirq context around replayed interrupts
Quoted 80 lines · Christophe Leroy (CS GROUP)
Le 17/11/2026 à 15:25, Ritesh Harjani (IBM) a écrit :
Quoted 79 lines
With
PROVE_LOCKING, boot warns once and lockdep turns itself off:DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()) WARNING: kernel/locking/lockdep.c:4448 at lockdep_hardirqs_on_prepare+0x22c/0x2d4, CPU#0: swapper/0/0 CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 7.4.0-rc2-00027-g074831f06e18-dirty #123 PREEMPT NIP: c0000000002b2f2c LR: c0000000002b2f28 CTR: c000000002426f90 REGS: c000000004f075c0 TRAP: 0700 Not tainted (7.4.0-rc2-00027-g074831f06e18-dirty) MSR: 8000000002021033 <SF,VEC,ME,IR,DR,RI,LE> CR: 48022221 XER: 20040000 NIP [c0000000002b2f2c] lockdep_hardirqs_on_prepare+0x22c/0x2d4 LR [c0000000002b2f28] lockdep_hardirqs_on_prepare+0x228/0x2d4 Call Trace: lockdep_hardirqs_on_prepare+0x228/0x2d4 (unreliable) trace_hardirqs_on+0x98/0x11c irqentry_exit+0x64c/0x1064 timer_interrupt+0x274/0x6bc __replay_soft_interrupts+0x158/0x380 arch_local_irq_restore+0x1cc/0x244 console_flush_one_record+0x438/0x654 console_unlock+0xb8/0x1e0 vprintk_emit+0x300/0x56c vprintk+0x20/0x34 _printk+0x40/0x54 lockdep_init+0xdc/0x104 start_kernel+0xa24/0xc68 start_here_common+0x1c/0x20Since commit
bee25f97ad24("powerpc: EnableGENERIC_ENTRYfeature") a replayed handler always returns viairqentry_exit(), which traces hardirqs on while the replay is still insideirq_enter():
arch_local_irq_restore()
irq_enter()hardirq_context= 1
__replay_soft_interrupts()
timer_interrupt(®s)
irq_enter_rcu()hardirq_context= 2
irq_exit_rcu()hardirq_context= 1
irqentry_exit()
trace_hardirqs_on()hardirq_context!= 0 -> WARN
next_interrupt()
lockdep_hardirq_exit()too lateLeave the hardirq context before each replayed handler instead of after it:
arch_local_irq_restore()
irq_enter()hardirq_context= 1
__replay_soft_interrupts()
lockdep_hardirq_exit()hardirq_context= 0 // fix
timer_interrupt(®s)
irq_enter_rcu()hardirq_context= 1
irq_exit_rcu()hardirq_context= 0
irqentry_exit()
trace_hardirqs_on()ok
next_interrupt()
trace_hardirqs_off()
lockdep_hardirq_enter()hardirq_context= 1
irq_exit()hardirq_context= 0Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")Signed-off-by Ritesh Harjani (IBM) <[email protected]>--- arch/powerpc/kernel/irq_64.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)arch/powerpc/kernel/irq_64.c +0
-6@@ -75,12 +75,6 @@ static inline void next_interrupt(struct pt_regs *regs)WARN_ON (irq_soft_mask_return () !=IRQS_ALL_DISABLED ); } -/* -* We are responding to the next interrupt, so interrupt-off -* latencies should be reset here. -*/ -lockdep_hardirq_exit (); -trace_hardirqs_on ();trace_hardirqs_off ();lockdep_hardirq_enter (); }
Quoted 9 lines
arch/powerpc/kernel/irq_64.c static __no_kcsan void __replay_soft_interrupts(void) · line 134
Quoted 5 lines
arch/powerpc/kernel/irq_64.c +1
-0@@ -137,18 +131,21 @@ static __no_kcsan void __replay_soft_interrupts(void)if (IS_ENABLED (CONFIG_PPC_BOOK3S ) &&irq_happened_test_and_clear (PACA_IRQ_HMI )) { regs.trap =INTERRUPT_HMI ; +lockdep_hardirq_exit ();Can we include that in
irq_happened_test_and_clear()instead of duplicating it 5 times ?
Thanks for the suggestion. We can actually simplify this if we move this into the caller itself i.e. __replay_soft_interrupts(). I think we could change it to this diff then:
arch/powerpc/kernel/irq_64.c +14 -5
@@ -76,13 +76,11 @@ static inline void next_interrupt(struct pt_regs *regs) }/* -* We are responding to the next interrupt, so interrupt-off -* latencies should be reset here. +* We are responding to the next interrupt. The irqentry_exit() of the +* replayed handler has traced hardirqs on, which also reset the +* interrupt-off latencies. Trace them off again. */ -lockdep_hardirq_exit (); -trace_hardirqs_on ();trace_hardirqs_off (); -lockdep_hardirq_enter (); }static inline bool irq_happened_test_and_clear (u8 irq)
@@ -116,6 +114,15 @@ static __no_kcsan void __replay_soft_interrupts(void)*/ local_paca->irq_happened |=PACA_IRQ_REPLAYING ; +/* +* The irq_enter() of our callers is there to hold off softirqs. Each +* replayed handler enters and leaves the hardirq context itself and +* returns through irqentry_exit(), which again traces hardirqs on. +* Since lockdep only allows that outside of hardirq context, so leave +* it for the duration of the replay. +*/ +lockdep_hardirq_exit (); +ppc_save_regs (®s); regs.softe =IRQS_ENABLED ; regs.msr |=MSR_EE ;
@@ -168,6 +175,8 @@ static __no_kcsan void __replay_soft_interrupts(void)next_interrupt (®s); } +lockdep_hardirq_enter (); + local_paca->irq_happened &= ~PACA_IRQ_REPLAYING ; }
The above simplifies it a lot, and the new state machine with hardirq_context and hardirqs_enabled which lockdep cares about, will look like this:
hardirq_context hardirqs_enabledarch_local_irq_restore()
trace_hardirqs_off() 0 1 -> 0
irq_enter() 0 -> 1 0 (softirqs held off)
__replay_soft_interrupts()
lockdep_hardirq_exit() 1 -> 0 0 patch: the loop is not a handler
timer_interrupt(®s)
irq_enter_rcu() 0 -> 1 0 handler starts
irq_exit_rcu() 1 -> 0 0 handler done
irqentry_exit()
trace_hardirqs_on() 0 0 -> 1 legal: context is 0
next_interrupt()
trace_hardirqs_off() 0 1 -> 0 still replaying: back to off
... same for each pending interrupt
lockdep_hardirq_enter() 0 -> 1 0 patch: restore for the caller
irq_exit() 1 -> 0 0 softirqs may run now
trace_hardirqs_on() 0 0 -> 1 IRQs really enabledLet me know if this looks good, then I can re-spin the next revision with the above changes.
-ritesh