mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-09-05 08:44:14 +02:00
Currently the nested interrupt disabling and enabling is represented by
_irqsave() and _irqrestore() APIs, which are relatively unsafe, for
example:
<interrupts are enabled as beginning>
spin_lock_irqsave(l1, flag1);
spin_lock_irqsave(l2, flag2);
spin_unlock_irqrestore(l1, flags1);
<l2 is still held but interrupts are enabled>
// accesses to interrupt-disable protected data will cause races
This is even easier to trigger with guard facilities:
unsigned long flag2;
scoped_guard(spin_lock_irqsave, l1) {
spin_lock_irqsave(l2, flag2);
}
// l2 locked but interrupts are enabled.
spin_unlock_irqrestore(l2, flag2);
(Hand-to-hand locking critical sections are not uncommon for a
fine-grained lock design)
And because of this unsafety, Rust cannot easily wrap the
interrupt-disabling locks in a safe API, which complicates the design.
To resolve this, introduce a new set of interrupt disabling APIs:
* local_interrupt_disable();
* local_interrupt_enable();
They work like local_irq_save() and local_irq_restore() except that 1)
the outermost local_interrupt_disable() call saves the interrupt state
into a per-CPU variable, so that the outermost local_interrupt_enable()
can restore the state, and 2) a per-CPU counter is added to record the
nest level of these calls, so that interrupts are not accidentally
enabled inside the outermost critical section.
Also add the corresponding spin_lock primitives: spin_lock_irq_disable()
and spin_unlock_irq_enable(), as a result, code as follows:
spin_lock_irq_disable(l1);
spin_lock_irq_disable(l2);
spin_unlock_irq_enable(l1);
// Interrupts are still disabled.
spin_unlock_irq_enable(l2);
doesn't have the issue that interrupts are accidentally enabled.
This also makes the wrapper of interrupt-disabling locks on Rust easier
to design.
[boqun: Apply Peter's feedback and fix spell errors reported by Ingo]
[boqun: Address the duplicate spin_acquire() spotted by sashiko]
Co-developed-by: Lyude Paul <lyude@redhat.com>
Signed-off-by: Lyude Paul <lyude@redhat.com>
Signed-off-by: Boqun Feng <boqun@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260804182657.87716-1-boqun@kernel.org
258 lines
7.2 KiB
C
258 lines
7.2 KiB
C
#ifndef __LINUX_SPINLOCK_API_SMP_H
|
|
#define __LINUX_SPINLOCK_API_SMP_H
|
|
|
|
#ifndef __LINUX_INSIDE_SPINLOCK_H
|
|
# error "Please do not include this file directly."
|
|
#endif
|
|
|
|
/*
|
|
* include/linux/spinlock_api_smp.h
|
|
*
|
|
* spinlock API declarations on SMP (and debug)
|
|
* (implemented in kernel/spinlock.c)
|
|
*
|
|
* portions Copyright 2005, Red Hat, Inc., Ingo Molnar
|
|
* Released under the General Public License (GPL).
|
|
*/
|
|
|
|
int in_lock_functions(unsigned long addr);
|
|
|
|
#define assert_raw_spin_locked(x) BUG_ON(!raw_spin_is_locked(x))
|
|
|
|
void __lockfunc _raw_spin_lock(raw_spinlock_t *lock) __acquires(lock);
|
|
void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
|
|
__acquires(lock);
|
|
void __lockfunc
|
|
_raw_spin_lock_nest_lock(raw_spinlock_t *lock, struct lockdep_map *map)
|
|
__acquires(lock);
|
|
void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) __acquires(lock);
|
|
void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
|
|
__acquires(lock);
|
|
void __lockfunc _raw_spin_lock_irq_disable(raw_spinlock_t *lock)
|
|
__acquires(lock);
|
|
|
|
unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock)
|
|
__acquires(lock);
|
|
unsigned long __lockfunc
|
|
_raw_spin_lock_irqsave_nested(raw_spinlock_t *lock, int subclass)
|
|
__acquires(lock);
|
|
int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock) __cond_acquires(true, lock);
|
|
int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock) __cond_acquires(true, lock);
|
|
void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock) __releases(lock);
|
|
void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) __releases(lock);
|
|
void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock) __releases(lock);
|
|
void __lockfunc _raw_spin_unlock_irq_enable(raw_spinlock_t *lock) __releases(lock);
|
|
void __lockfunc
|
|
_raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
|
|
__releases(lock);
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_LOCK
|
|
#define _raw_spin_lock(lock) __raw_spin_lock(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_LOCK_BH
|
|
#define _raw_spin_lock_bh(lock) __raw_spin_lock_bh(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_LOCK_IRQ
|
|
#define _raw_spin_lock_irq(lock) __raw_spin_lock_irq(lock)
|
|
#endif
|
|
|
|
/* Use the same config as spin_lock_irq() temporarily. */
|
|
#ifdef CONFIG_INLINE_SPIN_LOCK_IRQ
|
|
#define _raw_spin_lock_irq_disable(lock) __raw_spin_lock_irq_disable(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
|
|
#define _raw_spin_lock_irqsave(lock) __raw_spin_lock_irqsave(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_TRYLOCK
|
|
#define _raw_spin_trylock(lock) __raw_spin_trylock(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_TRYLOCK_BH
|
|
#define _raw_spin_trylock_bh(lock) __raw_spin_trylock_bh(lock)
|
|
#endif
|
|
|
|
#ifndef CONFIG_UNINLINE_SPIN_UNLOCK
|
|
#define _raw_spin_unlock(lock) __raw_spin_unlock(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_UNLOCK_BH
|
|
#define _raw_spin_unlock_bh(lock) __raw_spin_unlock_bh(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQ
|
|
#define _raw_spin_unlock_irq(lock) __raw_spin_unlock_irq(lock)
|
|
#endif
|
|
|
|
/* Use the same config as spin_unlock_irq() temporarily. */
|
|
#ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQ
|
|
#define _raw_spin_unlock_irq_enable(lock) __raw_spin_unlock_irq_enable(lock)
|
|
#endif
|
|
|
|
#ifdef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
|
|
#define _raw_spin_unlock_irqrestore(lock, flags) __raw_spin_unlock_irqrestore(lock, flags)
|
|
#endif
|
|
|
|
static inline int __raw_spin_trylock(raw_spinlock_t *lock)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
preempt_disable();
|
|
if (do_raw_spin_trylock(lock)) {
|
|
spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
|
|
return 1;
|
|
}
|
|
preempt_enable();
|
|
return 0;
|
|
}
|
|
|
|
static __always_inline bool _raw_spin_trylock_irq(raw_spinlock_t *lock)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
local_irq_disable();
|
|
if (_raw_spin_trylock(lock))
|
|
return true;
|
|
local_irq_enable();
|
|
return false;
|
|
}
|
|
|
|
static __always_inline bool _raw_spin_trylock_irq_disable(raw_spinlock_t *lock)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
local_interrupt_disable();
|
|
if (_raw_spin_trylock(lock))
|
|
return true;
|
|
local_interrupt_enable();
|
|
return false;
|
|
}
|
|
|
|
static __always_inline bool _raw_spin_trylock_irqsave(raw_spinlock_t *lock, unsigned long *flags)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
local_irq_save(*flags);
|
|
if (_raw_spin_trylock(lock))
|
|
return true;
|
|
local_irq_restore(*flags);
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* If lockdep is enabled then we use the non-preemption spin-ops
|
|
* even on CONFIG_PREEMPTION, because lockdep assumes that interrupts are
|
|
* not re-enabled during lock-acquire (which the preempt-spin-ops do):
|
|
*/
|
|
#if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC)
|
|
|
|
static inline unsigned long __raw_spin_lock_irqsave(raw_spinlock_t *lock)
|
|
__acquires(lock) __no_context_analysis
|
|
{
|
|
unsigned long flags;
|
|
|
|
local_irq_save(flags);
|
|
preempt_disable();
|
|
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
|
|
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
|
|
return flags;
|
|
}
|
|
|
|
static inline void __raw_spin_lock_irq(raw_spinlock_t *lock)
|
|
__acquires(lock) __no_context_analysis
|
|
{
|
|
local_irq_disable();
|
|
preempt_disable();
|
|
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
|
|
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
|
|
}
|
|
|
|
static inline void __raw_spin_lock_irq_disable(raw_spinlock_t *lock)
|
|
__acquires(lock) __no_context_analysis
|
|
{
|
|
local_interrupt_disable();
|
|
preempt_disable();
|
|
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
|
|
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
|
|
}
|
|
|
|
static inline void __raw_spin_lock_bh(raw_spinlock_t *lock)
|
|
__acquires(lock) __no_context_analysis
|
|
{
|
|
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
|
|
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
|
|
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
|
|
}
|
|
|
|
static inline void __raw_spin_lock(raw_spinlock_t *lock)
|
|
__acquires(lock) __no_context_analysis
|
|
{
|
|
preempt_disable();
|
|
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
|
|
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
|
|
}
|
|
|
|
#endif /* !CONFIG_GENERIC_LOCKBREAK || CONFIG_DEBUG_LOCK_ALLOC */
|
|
|
|
static inline void __raw_spin_unlock(raw_spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
spin_release(&lock->dep_map, _RET_IP_);
|
|
do_raw_spin_unlock(lock);
|
|
preempt_enable();
|
|
}
|
|
|
|
static inline void __raw_spin_unlock_irqrestore(raw_spinlock_t *lock,
|
|
unsigned long flags)
|
|
__releases(lock)
|
|
{
|
|
spin_release(&lock->dep_map, _RET_IP_);
|
|
do_raw_spin_unlock(lock);
|
|
local_irq_restore(flags);
|
|
preempt_enable();
|
|
}
|
|
|
|
static inline void __raw_spin_unlock_irq(raw_spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
spin_release(&lock->dep_map, _RET_IP_);
|
|
do_raw_spin_unlock(lock);
|
|
local_irq_enable();
|
|
preempt_enable();
|
|
}
|
|
|
|
static inline void __raw_spin_unlock_irq_enable(raw_spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
spin_release(&lock->dep_map, _RET_IP_);
|
|
do_raw_spin_unlock(lock);
|
|
local_interrupt_enable();
|
|
preempt_enable();
|
|
}
|
|
|
|
static inline void __raw_spin_unlock_bh(raw_spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
spin_release(&lock->dep_map, _RET_IP_);
|
|
do_raw_spin_unlock(lock);
|
|
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
|
|
}
|
|
|
|
static inline int __raw_spin_trylock_bh(raw_spinlock_t *lock)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
|
|
if (do_raw_spin_trylock(lock)) {
|
|
spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
|
|
return 1;
|
|
}
|
|
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
|
|
return 0;
|
|
}
|
|
|
|
/* PREEMPT_RT has its own rwlock implementation */
|
|
#ifndef CONFIG_PREEMPT_RT
|
|
#include <linux/rwlock_api_smp.h>
|
|
#endif
|
|
|
|
#endif /* __LINUX_SPINLOCK_API_SMP_H */
|