当前位置:   article > 正文

Linux 4.19 内核中 spinlock 概览

spinlock

Linux内核中 spinlock相关数据结构和代码实现涉及的文件还是挺多的,这篇博客尝试从文件的角度来梳理一下 spinlock的相关数据结构和代码实现,适合想大概了解 Linux内核中 spinlock从上层 API到底层实现间的调用路径和传参变化,尤其适合了解 spinlockqspinlock之间的关系。

这里只以 spinlock加锁函数 为切入点,梳理该函数从上层 API到底层实现涉及的文件。

1. 关注 spin_lock()

spinlock的加锁函数是 spin_lock()
spin_lock()的位置:include/linux/spinlock.h
spin_lock()的内容:

static __always_inline void spin_lock(spinlock_t *lock)
{
    raw_spin_lock(&lock->rlock);
}
  • 1
  • 2
  • 3
  • 4

看到这里,需要关注该函数的入参类型:spinlock_t
spinlock_t的位置: include/linux/spinlock_types.h
spinlock_t的内容:

typedef struct spinlock {
    union {
        struct raw_spinlock rlock;

#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
        struct {
            u8 __padding[LOCK_PADSIZE];
            struct lockdep_map dep_map;
        };
#endif
    };
} spinlock_t;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2. 关注 __raw_spin_lock()

从上面可以看到,spin_lock()调用了 raw_spin_lock(), 而 raw_spin_lock()是一个宏,将它展开后得到:__raw_spin_lock(),同时假定 CONFIG_INLINE_SPIN_LOCK配置项打开,则
__raw_spin_lock()的位置:include/linux/spinlock_api_smp.h
__raw_spin_lock()的内容:

static inline void __raw_spin_lock(raw_spinlock_t *lock)                                       
{
    preempt_disable();
    spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
    LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

__raw_spin_lock()的参数类型是:raw_spinlock_t,它和 struct raw_spinlock是同一个类型,对应 spinlock_trlock成员变量的类型
raw_spinlock_t的位置:include/linux/spinlock_types.h
raw_spinloct_t的内容:

typedef struct raw_spinlock {
    arch_spinlock_t raw_lock;
#ifdef CONFIG_DEBUG_SPINLOCK          
    unsigned int magic, owner_cpu;
    void *owner;                
#endif                                
#ifdef CONFIG_DEBUG_LOCK_ALLOC        
    struct lockdep_map dep_map;
#endif                                
} raw_spinlock_t;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3. 关注 do_raw_spin_lock()

从上面可以看到, __raw_spin_lock()调用了 do_raw_spin_lock()
do_raw_spin_lock()的位置:include/linux/spinlock.h
do_raw_spin_lock()的内容:

static inline void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock)
{
    __acquire(lock);
    arch_spin_lock(&lock->raw_lock);
}
  • 1
  • 2
  • 3
  • 4
  • 5

4. 关注 arch_spin_lock() 和 arch_spinlock_t

从上面可以看到,do_raw_spin_lock()调用了 arch_spin_lock()
arch_spin_lock()和它的入参 arch_spinlock_t是与体系结构相关的,这里面基本能分成两派,
一派是自己实现 arch_spin_lock()arch_spinlock_t
一派是使用通用的 qspinlock实现,也即 #define arch_spin_lock(l) queued_spin_lock(l)typedef struct qspinlock {...} arch_spinlock_t

体系结构自己实现的架构包括:alpha, arc, arm, hexagon, ia64, parisc, powerpc, riscv, s390, sh, sparc-32, xtensa
具体列举其中的 5 个:
alpha架构
arch_spin_lock()的位置:arch/alpha/include/asm/spinlock.h
arch_spinlock_t的位置:arch/alpha/include/asm/spinlock_types.h

arc架构
arch_spin_lock()的位置:arch/arc/include/asm/spinlock.h
arch_spinlock_t的位置:arch/arc/include/asm/spinlock_types.h

arm架构
arch_spin_lock()的位置:arch/arm/include/asm/spinlock.h
arch_spinlock_t的位置:arch/arm/include/asm/spinlock_types.h

hexagon架构
arch_spin_lock()的位置:arch/hexagon/include/asm/spinlock.h
arch_spinlock_t的位置:arch/hexagon/include/asm/spinlock_types.h

ia64架构
arch_spin_lock()的位置:arch/ia64/include/asm/spinlock.h
arch_spinlock_t的位置:arch/ia64/include/asm/spinlock_types.h

使用 qspinlock实现的架构包括:mips, sparc-64, x86, arm64, openrisc
queued_spin_lock()的位置:include/asm-generic/qspinlock.h
queued_spin_lock()的内容是:

static __always_inline void queued_spin_lock(struct qspinlock *lock)
{
    u32 val;
 
    val = atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL);
    if (likely(val == 0)) 
        return;
    queued_spin_lock_slowpath(lock, val);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

struct qspinlock的位置:include/asm-generic/qspinlock_types.h
struct qspinlock的内容是:

typedef struct qspinlock {
    union {
        atomic_t val;

        /*
         * By using the whole 2nd least significant byte for the
         * pending bit, we can allow better optimization of the lock
         * acquisition for the pending bit holder.
         */
#ifdef __LITTLE_ENDIAN
        struct {
            u8  locked;
            u8  pending;
        };
        struct {
            u16 locked_pending;
            u16 tail;
        };
#else
        struct {
            u16 tail;
            u16 locked_pending;
        };
        struct {
            u8  reserved[2];
            u8  pending;
            u8  locked;
        };
#endif
    };
} arch_spinlock_t;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/214559
推荐阅读
相关标签
  

闽ICP备14008679号