Sunday, October 4, 2015

Just discovered Dont know wat dis

http://www.crazypirate.me/futex/chapter4.html
http://blog.nativeflow.com/the-futex-vulnerability


https://events.linuxfoundation.org/sites/events/files/slides/linuxcon-2014-locking-final.pdf

Revisited :atomic

 

Atomic operations
http://simpleopencl.blogspot.in/2013/05/atomic-operations-and-floats-in-opencl.html
Several different atomic operations are supported,
almost all only for integers:
addition (integers and 32-bit floats)
minimum / maximum
increment / decrement
exchange / compare-and-swap
bitwise AND OR XOR
These are
quite fast for data in local memory
slower for data in global memory
(better on new Kepler hardware)
Lecture 3 – p. 17
Atomic operations
Compare-and-swap:
int atomic_cmpxchg(volatile __local int
*p,
int cmp, int val);
if compare equals old value stored at address then
val is stored instead
in either case, routine returns the value of old
seems a bizarre routine at first sight, but can be very
useful for atomic locks
also can be used to implement 64-bit floating point
atomic addition
Lecture 3 – p. 18
Global atomic lock
// global variable: 0 unlocked, 1 locked
__global volatile int lock=0;
__kernel void kernel(...) {
...
if (get_local_id(0)==0) {
// set lock
do {} while(atomic_cmpxchg(&lock, 0, 1));
}
...
// free lock
lock = 0;
} Lecture 3 – p. 19Global atomic lock
Problem: when a work-item writes data to global memory
the order of completion is not guaranteed, so global writes
may not have completed by the time the lock is unlocked
__kernel void kernel(...) {
...
if (get_local_id(0)==0) {
do {} while(atomic_cmpxchg(&lock,0,1));
...
mem_fence(CLK_GLOBAL_MEM_FENCE); // order writes
// free lock
lock = 0;
}
} Lecture 3 – p. 20mem_fence
mem fence();
order all preceding global or local (or both) reads and
writes
means all loads/stores committed to memory before
any following loads/stores
mem fence write();
same as above, but only for stores
mem fence read();
same as above, but only for loads
Different to barrier() – non-blocking
Lecture 3 – p. 21Summary
lots of esoteric capabilities – don’t worry about most of
them
essential to understand work-item divergence – can
have a very big impact on performance
barrier() is vital – will see another use of it in next
lecture
the rest can be ignored until you have a critical need
– then read the documentation carefully and look for
examples in the SDK
http://blog.csdn.net/angle_birds/article/details/7891174
所谓原子操作,就是该操作绝不会在执行完毕前被任何其他任务或事件打断,也就说,它的最小的执行单位,不可能有比它更小的执行单位。因此这里的原子实际是使用了物理学里的物质微粒的概念。
原子操作需要硬件的支持,因此是架构相关的,其API和原子类型的定义都定义在内核源码树的include/asm/atomic.h文件中,它们都使用汇编语言实现,因为C语言并不能实现这样的操作。
原子操作主要用于实现资源计数,很多引用计数(refcnt)就是通过原子操作实现的。原子类型定义如下:
typedef struct 

     volatile int counter; 

atomic_t;
volatile修饰字段告诉gcc不要对该类型的数据做优化处理,对它的访问都是对内存的访问,而不是对寄存器的访问。

Linux中的基本原子操作
宏或者函数
说明
Atomic_read
返回原子变量的值
Atomic_set
设置原子变量的值。
Atomic_add
原子的递增计数的值。
Atomic_sub
原子的递减计数的值。
atomic_cmpxchg
原子比较并交换计数值。
atomic_clear_mask
原子的清除掩码。

除此以外,还有一组操作64位原子变量的变体,以及一些位操作宏及函数。这里不再罗列。
/**
 返回原子变量的值。
 这里强制将counter转换为volatile int并取其值。目的就是为了避免编译优化。
 */
#define atomic_read(v)   (*(volatile int *)&(v)->counter)
/**
 设置原子变量的值。
 */
#define atomic_set(v,i)    (((v)->counter) = (i))

原子递增的实现比较精妙,理解它的关键是需要明白ldrexstrex这一对指令的含义。
/**
 原子的递增计数的值。
 */
static inline void atomic_add(int i, atomic_t *v)
{
         unsigned long tmp;
         int result;

         /**
          * __volatile__是为了防止编译器乱序。与"#define atomic_read(v)          (*(volatile int *)&(v)->counter)"中的volatile类似。
          */
         __asm__ __volatile__("@ atomic_add\n"
         /**
          * ldrexarm为了支持多核引入的新指令,表示"排它性"加载。与mipsll指令一样的效果。
          它与"排它性"存储配对使用。
          */
"1:    ldrex         %0, [%3]\n"
         /**
          原子变量的值已经加载到寄存器中,这里对寄存器中的值减去指定的值。
          */
"       add  %0, %0, %4\n"
         /**
          * strex"排它性"的存储寄存器的值到内存中。类似于mipssc指令。
          */
"       strex         %1, %0, [%3]\n"
         /**
          关键代码是这里的判断。如果在ldrexstrex之间,其他核没有对原子变量变量进行加载存储操作,
          那么寄存器中值就是0,否则非0.
          */
"       teq   %1, #0\n"
         /**
          如果其他核与本核冲突,那么寄存器值为非0,这里跳转到标号1处,重新加载内存的值并递增其值。
          */
"       bne  1b"
         : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
         : "r" (&v->counter), "Ir" (i)
         : "cc");
}

atomic_add_return递增原子变量的值,并返回它的新值。它与atomic_add的最大不同,在于在原子递增前后各增加了一句:smp_mb();
这是由linux原子操作函数的语义规定的:所有对原子变量的操作,如果需要向调用者返回结果,那么就需要增加多核内存屏障的语义。通俗的说,就是其他核看到本核对原子变量的操作结果时,本核在原子变量前的操作对其他核也是可见的。

理解了atomic_add,其他原子变量的实现也就容易理解了。这里不再详述。

atomic_cmpxchg()函数实现了一个比较+交换的原子操作(原子就是说cpu要不就不
做,要做就一定要做完某些操作才能干别的事情,对应这里就是比较和交换要一次过做完).
atomic_cmpxchg()比较kgdb_active->count的值是否等用-1,如果是则把cpu的值赋
给kgdb_active->count,否则不修改它的值,atomic_cmpxchg返回
kgdb_active->count赋值前的值.
kgdb_active是一个全局原子变量,定义在kernel/kgdb.c中,用来记录当前正在执行
kgdb代码的cpu号,它起到一个锁的作用,因为同一时间只能有一个cpu执行kgdb的代
码,这是可以想象得到的,如果两个cpu在两个不同断点被触发,那究竟是谁和远端gdb通
信呢?前一条命令被 cpu1拿了,后一条却去了cpu2那里,那还得了。
kgdb_active的初始值为-1,-1表示当前kgdb的处理函数并没有被触发,相反如果
kgdb已经在运行,那么kgdb_active就有它自己的值,这些处理都是针对多cpu的,如
果只有一个cpu,这个世界就简单多了。这里是防止多个kgdb的实例在不同cpu被触发
引起互相干扰。考虑这种情况,在cpu1上有一个断点让kgdb起来,这时,kgdb_active
还是-1,cpu1很顺利就给kgdb_active赋值然后进入后面的操作.这时cpu2中kgdb也被触发.
它也想进入后面的操作,但是这时候kgdb_active已经不再是-1,cpu2只能不断地比较
kgdb_active的值和执行cpu_relax(),宏cpu_relax()可以简化为一条pause汇编,通过引
入一个很短的延迟,加快了紧跟在锁后面的代码的执行并减少能源的消耗,实际上就是
让cpu2等。当cpu1在退出kgdb_handle_exception()前会把 kgdb_active赋回-1, 这样
cpu2就可以进行后面的操作了。kgdb使用大量的原子操作来完成锁的功能,后面还会
看到. atomic操作加上cpu_relax()跟一个自旋锁很相似。

No comments:

Post a Comment