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
176 lines
4.6 KiB
C
176 lines
4.6 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
#ifndef __LINUX_SPINLOCK_RT_H
|
|
#define __LINUX_SPINLOCK_RT_H
|
|
|
|
#ifndef __LINUX_INSIDE_SPINLOCK_H
|
|
#error Do not include directly. Use spinlock.h
|
|
#endif
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
extern void __rt_spin_lock_init(spinlock_t *lock, const char *name,
|
|
struct lock_class_key *key, bool percpu);
|
|
#else
|
|
static inline void __rt_spin_lock_init(spinlock_t *lock, const char *name,
|
|
struct lock_class_key *key, bool percpu)
|
|
{
|
|
}
|
|
#endif
|
|
|
|
#define __spin_lock_init(slock, name, key, percpu) \
|
|
do { \
|
|
rt_mutex_base_init(&(slock)->lock); \
|
|
__rt_spin_lock_init(slock, name, key, percpu); \
|
|
} while (0)
|
|
|
|
#define _spin_lock_init(slock, percpu) \
|
|
do { \
|
|
static struct lock_class_key __key; \
|
|
__spin_lock_init(slock, #slock, &__key, percpu); \
|
|
} while (0)
|
|
|
|
#define spin_lock_init(slock) _spin_lock_init(slock, false)
|
|
#define local_spin_lock_init(slock) _spin_lock_init(slock, true)
|
|
|
|
extern void rt_spin_lock(spinlock_t *lock) __acquires(lock);
|
|
extern void rt_spin_lock_nested(spinlock_t *lock, int subclass) __acquires(lock);
|
|
extern void rt_spin_lock_nest_lock(spinlock_t *lock, struct lockdep_map *nest_lock) __acquires(lock);
|
|
extern void rt_spin_unlock(spinlock_t *lock) __releases(lock);
|
|
extern void rt_spin_lock_unlock(spinlock_t *lock);
|
|
extern int rt_spin_trylock_bh(spinlock_t *lock) __cond_acquires(true, lock);
|
|
extern int rt_spin_trylock(spinlock_t *lock) __cond_acquires(true, lock);
|
|
|
|
static __always_inline void spin_lock(spinlock_t *lock)
|
|
__acquires(lock)
|
|
{
|
|
rt_spin_lock(lock);
|
|
}
|
|
|
|
#ifdef CONFIG_LOCKDEP
|
|
# define __spin_lock_nested(lock, subclass) \
|
|
rt_spin_lock_nested(lock, subclass)
|
|
|
|
# define __spin_lock_nest_lock(lock, nest_lock) \
|
|
do { \
|
|
typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
|
|
rt_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
|
|
} while (0)
|
|
# define __spin_lock_irqsave_nested(lock, flags, subclass) \
|
|
do { \
|
|
typecheck(unsigned long, flags); \
|
|
flags = 0; \
|
|
__spin_lock_nested(lock, subclass); \
|
|
} while (0)
|
|
|
|
#else
|
|
/*
|
|
* Always evaluate the 'subclass' argument to avoid that the compiler
|
|
* warns about set-but-not-used variables when building with
|
|
* CONFIG_DEBUG_LOCK_ALLOC=n and with W=1.
|
|
*/
|
|
# define __spin_lock_nested(lock, subclass) spin_lock(((void)(subclass), (lock)))
|
|
# define __spin_lock_nest_lock(lock, subclass) spin_lock(((void)(subclass), (lock)))
|
|
# define __spin_lock_irqsave_nested(lock, flags, subclass) \
|
|
spin_lock_irqsave(((void)(subclass), (lock)), flags)
|
|
#endif
|
|
|
|
#define spin_lock_nested(lock, subclass) \
|
|
__spin_lock_nested(lock, subclass)
|
|
|
|
#define spin_lock_nest_lock(lock, nest_lock) \
|
|
__spin_lock_nest_lock(lock, nest_lock)
|
|
|
|
#define spin_lock_irqsave_nested(lock, flags, subclass) \
|
|
__spin_lock_irqsave_nested(lock, flags, subclass)
|
|
|
|
static __always_inline void spin_lock_bh(spinlock_t *lock)
|
|
__acquires(lock)
|
|
{
|
|
/* Investigate: Drop bh when blocking ? */
|
|
local_bh_disable();
|
|
rt_spin_lock(lock);
|
|
}
|
|
|
|
static __always_inline void spin_lock_irq(spinlock_t *lock)
|
|
__acquires(lock)
|
|
{
|
|
rt_spin_lock(lock);
|
|
}
|
|
|
|
static __always_inline void spin_lock_irq_disable(spinlock_t *lock)
|
|
__acquires(lock)
|
|
{
|
|
rt_spin_lock(lock);
|
|
}
|
|
|
|
#define spin_lock_irqsave(lock, flags) \
|
|
do { \
|
|
typecheck(unsigned long, flags); \
|
|
flags = 0; \
|
|
spin_lock(lock); \
|
|
} while (0)
|
|
|
|
static __always_inline void spin_unlock(spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
rt_spin_unlock(lock);
|
|
}
|
|
|
|
static __always_inline void spin_unlock_bh(spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
rt_spin_unlock(lock);
|
|
local_bh_enable();
|
|
}
|
|
|
|
static __always_inline void spin_unlock_irq(spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
rt_spin_unlock(lock);
|
|
}
|
|
|
|
static __always_inline void spin_unlock_irq_enable(spinlock_t *lock)
|
|
__releases(lock)
|
|
{
|
|
rt_spin_unlock(lock);
|
|
}
|
|
|
|
static __always_inline void spin_unlock_irqrestore(spinlock_t *lock,
|
|
unsigned long flags)
|
|
__releases(lock)
|
|
{
|
|
rt_spin_unlock(lock);
|
|
}
|
|
|
|
#define spin_trylock(lock) rt_spin_trylock(lock)
|
|
|
|
static __always_inline int spin_trylock_irq_disable(spinlock_t *lock)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
return rt_spin_trylock(lock);
|
|
}
|
|
|
|
#define spin_trylock_bh(lock) rt_spin_trylock_bh(lock)
|
|
|
|
#define spin_trylock_irq(lock) rt_spin_trylock(lock)
|
|
|
|
static __always_inline bool _spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
|
|
__cond_acquires(true, lock)
|
|
{
|
|
*flags = 0;
|
|
return rt_spin_trylock(lock);
|
|
}
|
|
#define spin_trylock_irqsave(lock, flags) _spin_trylock_irqsave(lock, &(flags))
|
|
|
|
#define spin_is_contended(lock) (((void)(lock), 0))
|
|
|
|
static inline int spin_is_locked(spinlock_t *lock)
|
|
{
|
|
return rt_mutex_base_is_locked(&lock->lock);
|
|
}
|
|
|
|
#define assert_spin_locked(lock) BUG_ON(!spin_is_locked(lock))
|
|
|
|
#include <linux/rwlock_rt.h>
|
|
|
|
#endif
|