asm-generic/mmiowb: Mark accesses to fix KCSAN warnings
Subsystems (2)
PATCH 1/3 asm-generic/mmiowb: Mark accesses to fix KCSAN warnings
Prior to this patch, data races are detectable by KCSAN of the following forms:
[1] Asynchronous calls to mmiowb_set_pending() from an interrupt context or otherwise outside of a critical section
[2] Interrupted critical sections, where the interrupt will itself acquire a lock
In case [1], calling context does not need an mmiowb() call to be issued, otherwise it would do so itself. Such calls to mmiowb_set_pending() are either idempotent or no-ops.
In case [2], irrespective of when the interrupt occurs, the interrupt will acquire and release its locks prior to its return, nesting_count will continue balanced. In the worst case, the interrupted critical section during a mmiowb_spin_unlock() call observes an mmiowb to be pending and afterward is interrupted, leading to an extraneous call to mmiowb(). This data race is clearly innocuous.
Resolve KCSAN warnings of type [1] by means of READ_ONCE, WRITE_ONCE. As increments and decrements to nesting_count are balanced by interrupt contexts, resolve type [2] warnings by simply revoking instrumentation, with data_race() rather than READ_ONCE() and WRITE_ONCE(), the memory consistency semantics of plain-accesses will still lead to correct behaviour.
---
Previously discussed here:
https://lore.kernel.org/linuxppc-dev/[email protected]/
But pushed back due to affecting other architectures. Reissuing, to linuxppc-dev, as it does not enact a functional change.
--- include/asm-generic/mmiowb.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
include/asm-generic/mmiowb.h +9 -6
@@ -37,25 +37,28 @@ static inline void mmiowb_set_pending(void)struct mmiowb_state *ms =__mmiowb_state ();if (likely (ms->nesting_count)) - ms->mmiowb_pending = ms->nesting_count; + WRITE_ONCE(ms->mmiowb_pending, ms->nesting_count); }static inline void mmiowb_spin_lock (void ) {struct mmiowb_state *ms =__mmiowb_state (); - ms->nesting_count++; + +/* Increment need not be atomic. Nestedness is balanced over interrupts. */ +data_race (ms->nesting_count++); }static inline void mmiowb_spin_unlock (void ) {struct mmiowb_state *ms =__mmiowb_state (); +u16 pending =READ_ONCE (ms->mmiowb_pending); -if (unlikely (ms->mmiowb_pending)) { - ms->mmiowb_pending =0 ; +WRITE_ONCE (ms->mmiowb_pending,0 ); +if (unlikely (pending))mmiowb (); - } - ms->nesting_count--; +/* Decrement need not be atomic. Nestedness is balanced over interrupts. */ +data_race (ms->nesting_count--); }#else #define mmiowb_set_pending () do { } while (0)
Signature · 1 line
2.43.0
PATCH 2/3 powerpc: Apply __always_inline to interrupt_{enter,exit}_prepare()
In keeping with the advice given by Documentation/core-api/entry.rst, entry and exit handlers for interrupts should not be instrumented. Guarantee that the interrupt_{enter,exit}_prepare() routines are inlined so that they will inheret instrumentation from their caller.
KCSAN kernels were observed to compile without inlining these routines, which would lead to grief on NMI handlers.
--- arch/powerpc/include/asm/interrupt.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/interrupt.h +6 -6
@@ -150,7 +150,7 @@ static inline void booke_restore_dbcr0(void)#endif } -static inline void interrupt_enter_prepare (struct pt_regs *regs) +static __always_inline void interrupt_enter_prepare(struct pt_regs *regs) {#ifdef CONFIG_PPC64 irq_soft_mask_set (IRQS_ALL_DISABLED );
@@ -215,11 +215,11 @@ static inline void interrupt_enter_prepare(struct pt_regs *regs)* However interrupt_nmi_exit_prepare does return directly to regs, because * NMIs do not do "exit work" or replay soft-masked interrupts. */ -static inline void interrupt_exit_prepare (struct pt_regs *regs) +static __always_inline void interrupt_exit_prepare(struct pt_regs *regs) { } -static inline void interrupt_async_enter_prepare (struct pt_regs *regs) +static __always_inline void interrupt_async_enter_prepare(struct pt_regs *regs) {#ifdef CONFIG_PPC64 /* Ensure interrupt_enter_prepare does not enable MSR[EE] */
@@ -238,7 +238,7 @@ static inline void interrupt_async_enter_prepare(struct pt_regs *regs)irq_enter (); } -static inline void interrupt_async_exit_prepare (struct pt_regs *regs) +static __always_inline void interrupt_async_exit_prepare(struct pt_regs *regs) {/* * Adjust at exit so the main handler sees the true NIA. This must
@@ -278,7 +278,7 @@ static inline bool nmi_disables_ftrace(struct pt_regs *regs)return true; } -static inline void interrupt_nmi_enter_prepare (struct pt_regs *regs,struct interrupt_nmi_state *state) +static __always_inline void interrupt_nmi_enter_prepare(struct pt_regs *regs, struct interrupt_nmi_state *state) {#ifdef CONFIG_PPC64 state->irq_soft_mask = local_paca->irq_soft_mask;
@@ -340,7 +340,7 @@ static inline void interrupt_nmi_enter_prepare(struct pt_regs *regs, struct intenmi_enter (); } -static inline void interrupt_nmi_exit_prepare (struct pt_regs *regs,struct interrupt_nmi_state *state) +static __always_inline void interrupt_nmi_exit_prepare(struct pt_regs *regs, struct interrupt_nmi_state *state) {if (mfmsr () &MSR_DR ) {// nmi_exit if relocations are on
Signature · 1 line
2.43.0
PATCH 3/3 powerpc/64: Only warn for kuap locked when KCSAN not present
In reply to Rohan McLureArbitrary instrumented locations, including syscall handlers, can call arch_local_irq_restore() transitively when KCSAN is enabled, and in turn also replay_soft_interrupts_irqrestore(). The precondition on entry to this routine that is checked is that KUAP is enabled (user access prohibited). Failure to meet this condition only triggers a warning however, and afterwards KUAP is enabled anyway. That is, KUAP being disabled on entry is in fact permissable, but not possible on an uninstrumented kernel.
Disable this assertion only when KCSAN is enabled.
--- arch/powerpc/kernel/irq_64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/kernel/irq_64.c +2 -1
@@ -189,7 +189,8 @@ static inline __no_kcsan void replay_soft_interrupts_irqrestore(void)* and re-locking AMR but we shouldn't get here in the first place, * hence the warning. */ -kuap_assert_locked (); +if (!IS_ENABLED (CONFIG_KCSAN )) +kuap_assert_locked ();if (kuap_state !=AMR_KUAP_BLOCKED )set_kuap (AMR_KUAP_BLOCKED );
Signature · 1 line
2.43.0