博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINUX内核之普通自旋锁
阅读量:4641 次
发布时间:2019-06-09

本文共 11453 字,大约阅读时间需要 38 分钟。

@CopyLeft by ICANTHI Can do ANy THing that I CAN THink!~

Author:WenHui,WuHan University,2012-6-15

 

PDF版阅读地址

 

普通自旋锁

自旋锁最常见的使用场景是创建一段临界区 :

static DEFINE_SPINLOCK(xxx_lock);

unsigned long flags;

spin_lock_irqsave(&xxx_lock, flags);

... critical section here ..

spin_unlock_irqrestore(&xxx_lock, flags);

自旋锁使用时值得注意的是:对于采用使用自旋锁以保证共享变量的存取安全时,仅当系统中所有涉及到存取该共享变量的程序部分都采用成对的spin_lock、和spin_unlock来进行操作才能保证其安全性。

NOTE! The spin-lock is safe only when you _also_ use the lock itself to do locking across CPU's, which implies that EVERYTHING that touches a shared variable has to agree about the spinlock they want to use.

在Linux2.6.15.5中,自旋体数据结构如下:

当配置CONFIG_SMP时,raw_spinlock_t才是一个含有slock变量的结构,该slock字段标识自旋锁是否空闲状态,用以处理多CPU处理器并发申请锁的情况;当未配置CONFIG_SMP时,对于单CPU而言,不会发生发申请自旋锁,故raw_lock为空结构体。

当配置CONFIG_SMP和CONFIG_PREEMPT时,spinlock_t才会有break_lock字段,break_lock字段用于标记自旋锁竞争状态,当break_lock = 0时表示没有多于两个的执行路径,当break_lock = 1时表示没有其它进程在忙等待该锁。当在SMP多CPU体系架构下有可能出现申请不到自旋锁、空等的情况,但LINUX内核必须保证在spin_lock的原子性,故在配置CONFIG_PREEMPT时必须禁止内核抢占。

字段
描述
spin_lock_init(lock)
一个自旋锁时,可使用接口函数将其初始化为锁定状态
spin_lock(lock)
用于锁定自旋锁,如果成功则返回;否则循环等待自旋锁变为空闲
spin_unlock(lock)
释放自旋锁lock,重新设置自旋锁为锁定状态
spin_is_locked(lock)
判断当前自旋锁是否处于锁定状态
spin_unlock_wait(lock)
循环等待、直到自旋锁lock变为可用状态
spin_trylock(lock)
尝试锁定自旋锁lock,如不成功则返回0;否则锁定,并返回1
spin_can_lock(lock)
判断自旋锁lock是否处于空闲状态

spin_lock和spin_unlock的关系如下:

可见,在UP体系架构中,由于没有必要有实际的锁以防止多CPU抢占,spin操作仅仅是禁止和开启内核抢占。

LINUX 2.6.35版本,将spin lock实现更改为 ticket lock。spin_lock数据结构除了用于内核调试之外,字段为:

ticket spinlock将rlock字段分解为如下两部分:

Next是下一个票号,而Owner是允许使用自旋锁的票号。加锁时CPU取Next,并将rlock.Next + 1。将Next与Owner相比较,若相同,则加锁成功;否则循环等待、直到Next = rlock.Owner为止。解锁则直接将Owner + 1即可。

spin_lock和spin_unlock的调用关系如下:

 

普通自旋锁源码分析

源程序文件目录关系图

在/include/linux/spinlock.h中通过是否配置CONFIG_SMP项判断导入哪种自旋锁定义及操作:

 
/*
  * include/linux/spinlock.h - generic spinlock/rwlock declarations
  * here's the role of the various spinlock/rwlock related include files:
  * on SMP builds:
  *  asm/spinlock_types.h: contains the arch_spinlock_t/arch_rwlock_t and the
  *                        initializers
  *  linux/spinlock_types.h:
  *                        defines the generic type and initializers
  *  asm/spinlock.h:       contains the arch_spin_*()/etc. lowlevel
  *                        implementations, mostly inline assembly code
  *  linux/spinlock_api_smp.h:
  *                        contains the prototypes for the _spin_*() APIs.
  *  linux/spinlock.h:     builds the final spin_*() APIs.
  * on UP builds:
  *  linux/spinlock_type_up.h:
  *                        contains the generic, simplified UP spinlock type.
  *                        (which is an empty structure on non-debug builds)
  *  linux/spinlock_types.h:
  *                        defines the generic type and initializers
  *  linux/spinlock_up.h:
  *                        contains the arch_spin_*()/etc. version of UP
  *                        builds. (which are NOPs on non-debug, non-preempt
  *                        builds)
  *   (included on UP-non-debug builds:)
  *  linux/spinlock_api_up.h:
  *                        builds the _spin_*() APIs.
  *  linux/spinlock.h:     builds the final spin_*() APIs.
  */
 
/*
  * Pull the arch_spin*() functions/declarations (UP-nondebug doesnt need them):
  */
#ifdef CONFIG_SMP
# include 
#else
# include <>
#endif
typedef struct  {
         union {
                 struct  ;
         };
} ;
static inline void ( *)
{
         (&->);
}
#define ()     ()
 
 
static inline void ( *)
{
         (&->);
}
#define ()           ()
 

UP体系架构

 

spin_lock函数在UP体系架构中最终实现方式为:

/include/linux/spinlock_api_up.h

#define ()                    ()
/*
  * In the UP-nondebug case there's no real locking going on, so the
  * only thing we have to do is to keep the preempt counts and irq
  * flags straight, to suppress compiler warnings of unused lock
  * variables, and to add the proper checker annotations:
  */
#define () \
   do { (); (); (void)(); } while (0)
#define ()                    ()
 
preempt_disable在未配置CONFIG_PREEMPT时为空函数,否则禁止内核抢占。而__acquire()用于内核编译过程中静态检查。(void)(lock)则是为避免编译器产生lock未被使用的警告。
 
spin_unlock函数在UP体系架构中最终实现方式为:
#define () \
   do { (); (); (void)(); } while (0)
 

SMP体系架构-Tickect Spin Lock的实现方式

在Linux2.6.24中,自旋锁由一个整数表示,当为1时表示锁是空闲的,spin_lock()每次减少1,故 <=0时则表示有多个锁在忙等待,但这将导致不公平性。自linux2.6.25开始,自旋锁将整数拆为一个16位数,结构如下:

该实现机制称为“Ticket spinlocks”,Next字节表示下一次请求锁给其分配的票号,而Owner表示当前可以取得锁的票号,Next和Owner初始化为0。当lock.Next = lock.Owner时,表示该锁处于空闲状态spin_lock执行如下过程:

1、my_ticket = slock.next

2、slock.next++

3、wait until my_ticket = slock.owner

spin_unlock执行如下过程:

1、slock.owner++

但该锁将导致一个问题:8个bit将只能最多表示255个CPU来竞争该锁。故系统通过的方式,将实现两个tickect_spin_lock和ticket_spin_unclock的版本:
#if ( < 256)
#define  8
#else
#define  16
 

SMP体系架构-SPIN LOCK (ticket_shif 8)

#ifdef CONFIG_INLINE_SPIN_LOCK
#define () ()
#endif

/include/linux/spinlock_api_smp.h:

static inline void ( *)
{
         ();
         (&->, 0, 0, );
         (, , );
}

在__raw_spin_lock中,首先禁止内核抢占,调用LOCK_CONTENED宏

#define (, try, )                        \
do {                                                            \
         if (!try()) {                                      \
                 (&()->, );    \
                 ();                                    \
         }                                                       \
         (&()->, );                     \
} while (0)

其中即在_raw_spin_lock中,即为首先调用do_raw_spin_trylock尝试加锁,若失败则继续调用do_raw_spin_lock进行加锁。而do_raw_spin_xxx具体实现与平台有关。

/include/linux/spinlock.h

static inline void ( *) ()
{
         ();
         (&->);
}
 
static inline int ( *)
{
         return (&()->);
}

在X86平台下,和实现为两个函数:

/arch/x86/include/asm/spinlock.h
static  void ( *)
{
         ();
}
 
static  int ( *)
{
         return ();
}
#if ( < 256)
#define  8
static  void ( *)
{
         short  = 0x0100;
 
         asm volatile (
                  "xaddw %w0, %1\n"
                 "1:\t"
                 "cmpb %h0, %b0\n\t"
                 "je 2f\n\t"
                 "rep ; nop\n\t"
                 "movb %1, %b0\n\t"
                 /* don't need lfence here, because loads are in-order */
                 "jmp 1b\n"
                 "2:"
                 : "+Q" (), "+m" (->)
                 :
                 : "memory", "cc");
}

066:LOCK_PREFIX在UP上为空定义,而在SMP上为Lock,用以保证从066行~074行为原子操作,强制所有CPU缓存失效。xaddw指令用法如下:

xaddw src, dsc ==

tmp = dsc

desc = dsc + src

src = tmp

XADDW语法验证实验:

xaddw使%0和%1按1个word长度交换相加,即:%0: inc → slock, %1: slock → slock + 0x0100。%1此时高字节Next + 1。xaddw使%0和%1内容改变如下:

068:比较inc中自己的Next是否与Owner中ticket相等,若相等则获取自旋锁使用权、结束循环。

070行 ~ 073行:如果Owner不属于自己,则执行空语句,并重新读取slock中的Owner,跳回至068行进行判断。

为什么要用LOCK_PREFIX宏来代替直接使用lock指令的方式呢?解释如下:为了避免在配置了CONFIG_SMP项编译产生的SMP内核、实际却运行在UP系统上时系统执行lock命令所带来的开销,系统创建在.smp_locks一张SMP alternatives table用以保存系统中所有lock指令的指针。当实际运行时,若从SMP→UP时,可以根据.smp_locks lock 指针表通过热补丁的方式将lock指令替换成nop指令。当然也可以实现系统运行时将锁由UP→SMP的切换。具体应用可参见参考资料《Linux 内核 LOCK_PREFIX 的含义》。

 

/*
  * Alternative inline assembly for SMP.
  *
  * The LOCK_PREFIX macro defined here replaces the LOCK and
  * LOCK_PREFIX macros used everywhere in the source tree.
  *
  * SMP alternatives use the same data structures as the other
  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  * UP system running a SMP kernel.  The existing apply_alternatives()
  * works fine for patching a SMP kernel for UP.
  *
  * The SMP alternative tables can be kept after boot and contain both
  * UP and SMP versions of the instructions to allow switching back to
  * SMP at runtime, when hotplugging in a new CPU, which is especially
  * useful in virtualized environments.
  *
  * The very common lock prefix is handled as special case in a
  * separate table which is a pure address list without replacement ptr
  * and size information.  That keeps the table sizes small.
  */
 
#ifdef CONFIG_SMP
#define  \
                 ".section .smp_locks,\"a\"\n"   \
                 ".balign 4\n"                   \
                 ".long 671f - .\n" /* offset */ \
                 ".previous\n"                   \
                 "671:"
 
#define   "\n\tlock; "
 
#else /* ! CONFIG_SMP */
#define  ""
#define  ""
#endif

032“.section .smp_locks, a”,表示以下代码生成在.smp_locks段中,而“a”代表——allocatable。

033行~034行 “.balign 4 .long 571f”,表示以4字节对齐、将671标签的地址置于.smp_locks段中,而标签671的地址即为:代码段lock指令的地址。(其实就是lock指令的指针啦~~~)

033行~034行 “.previous”伪指令,表示恢复以前section,即代码段。故在038行将导致在代码段生成lock指令。

时首先尝试使用__ticket_spin_trylock对lock进行加锁,若失败则继续使用__ticket_spin_lock进行加锁。不直接调用__ticket_spin_lock而使用__ticket_spin_trylock的原因是:

trylock首先不会修改lock.slock的ticket,它只是通过再次检查,1)将slock读出,并判断slock是否处于空闲状态;2)调用LOCK执行原子操作,判断当前slock的Next是否已经被其它CPU修改,若未被修改则获得该锁,并将lock.slock.Next + 1。

spin_lock,无论如何,首先调用LOCK执行原子性操作、声明ticket;而trylock则首先进行slock.Next == slock.Owner的判断,降低第二次比较调用LOCK的概率。

 

static  int ( *)
{
         int , new;
 
         asm volatile("movzwl %2, %0\n\t"
                      "cmpb %h0,%b0\n\t"
                      "leal 0x100(%"  "0), %1\n\t"
                      "jne 1f\n\t"
                       "cmpxchgw %w1,%2\n\t"
                      "1:"
                      "sete %b1\n\t"
                      "movzbl %b1,%0\n\t"
                      : "=&a" (), "=&q" (new), "+m" (->)
                      :
                      : "memory", "cc");
 
         return ;
}

084将lock.slock的值赋给tmp。

085比较tmp.next == tmp.owner,判断当前自旋锁是否空闲。

086leal指令(Load effective address),实际上是movl的变形,“leal 0x10 (%eax, %eax, 3), %edx” → “%edx = 0x10 + %eax + %eax * 3”,但leal却不像movl那样从内存取值、而直接读取寄存器。086行语句,根据REG_PTR_MODE不同配置,在X86平台下为:“leal 0x100(%k0), %1”,而在其它平台为:“leal 0x100(%q0), %1”,忽略占位符修饰“k”或“q”,则该行语句等价于:

“movl (%0 + 0x100),%1”,此时new = { tmp.Next + 1, tmp.Owner }。

087若tmp.next != tmp.owner,即自旋锁不空闲,则跳到089行将0赋值给tmp并返回。

088原子性地执行操作cmpxchgw,用以检测当前自旋锁是否已被其它CPU修改lock.slock的Next域,若有竞争者则失败、否则获得该锁并将Next + 1,这一系列操作是原子性的!cmpxchgw操作解释如下:

the accumulator (8-32 bits) with "dest". If equal the "dest" is loaded with "src", otherwise the accumulator is loaded with "dest".(在IA32下,%EAX即为累加器。)

所以,“cmpxchgw %w1, %2”等效于:

“tmp.Next == lock.slock.Next ? lock.slock = new : tmp = lock.slock”

若Next未发生变化,则将lock.slock更新为new,实质上是将slock的Next+1

090执行sete指令,若cmpxchgw或cmpb成功则将new的最低字节%b1赋值为1,否则赋值为0. sete的解释为:

Sets the byte in the operand to 1 if the Zero Flag is set, otherwise sets the operand to 0.

091movzbl(movz from byte to long)指令将%b1赋值给tmp最低字节,且其它位补0.即将tmp置为0或1.

 

SMP体系架构-SPIN UNLOCK (ticket_shif 8)

/include/linux/spinlock_api_smp.h

#ifdef CONFIG_INLINE_SPIN_LOCK
#define () ()
#endif
static inline void ( *)
{
         (&->, 1, );
         ();
         ();
}

spin_unlock即最终调用do_raw_spin_unlock对自旋锁进行释放操作。

/include/linux/spinlock.h

static inline void ( *) ()
{
         ();
         (&->);
}

对于x86的IA32平台,arch_spin_lock实现如下:

/arch/x86/include/asm/spinlock.h

static  void ( *)
{
         ();
}
#if ( < 256)
#define  8
static  void ( *)
{
         asm volatile( "incb %0"
                      : "+m" (->)
                      :
                      : "memory", "cc");
}

101将lock->slock的Owner + 1,表示可以让下一个拥有牌号的CPU加锁。

#if (CONFIG_X86_32) && \
         ((CONFIG_X86_OOSTORE) || (CONFIG_X86_PPRO_FENCE))
/*
  * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
  * (PPro errata 66, 92)
  */
#
#else
#
#endif
 

参考资料

自旋锁

《spinlocks.txt》,/Documentation/spinlocks.txt

《Ticket spinlocks》,

《Linux x86 spinlock实现之分析》,

《Linux 内核 LOCK_PREFIX 的含义》,

《The Intel 8086 / 8088/ 80186 / 80286 / 80386 / 80486 Instruction Set》:

posted on
2012-06-15 11:12 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/icanth/archive/2012/06/15/2550379.html

你可能感兴趣的文章
禁用缓存
查看>>
显示和编辑注解
查看>>
ArcGIS AddIN之工具不可用
查看>>
配置错误,无法识别的配置节 system.serviceModel 解决方案
查看>>
oc学习
查看>>
Java生鲜电商平台-Linux服务器常用升级的基础包
查看>>
聊天室(C++客户端+Pyhton服务器)2.基本功能添加
查看>>
swift中try
查看>>
缓存框架Ehcache相关
查看>>
用Instant client批量安装Oracle客户端-安装配置
查看>>
Fireworks为枝繁叶茂的树木图片抠底
查看>>
iphone 开发学习整理
查看>>
自我介绍
查看>>
Prof. Dr. Ligang Liu (刘利刚) 中国科技大学
查看>>
centos 升级openssl
查看>>
Ubuntu下安装 Mono(整理)
查看>>
Python Tkinter 学习成果:点歌软件music
查看>>
虚方法(virtual)和抽象方法(abstract)的区别
查看>>
Guid.NewGuid().ToString()生成唯一码js
查看>>
python 中feedParser
查看>>