发新话题
打印

自旋锁(Spin locks)

自旋锁(Spin locks)



   研究的视角:多处理机系统中内核中的数据结构并发访问控制
在多处理机系统中,信号并不总是解决同步问题的最好方案,
在多个CPU通过内核控制路径并发访问内核时,一些内核中
的数据结构应保护起来,免受破坏,在这种情况下,如果需
要更新数据结构的时间很短暂,信号的处理方案并不是很有
效,为了检查一个信号,内核必须在信号列表中插入一个进
程,然后使其处于挂起状态,相对来讲,这样操作性能的开
销比较大,有可能在这个时间内,其他的内核控制路上已经
释放了信号,时机控制不利。所以采用了自旋锁,自旋锁与
信号很相似,但是它没有形成进程列表,当一个进程发现由
另一个进程关闭的锁时,它反复探测,执行一个轻量级循环
指令直到这个锁被开放。当然啦,自旋锁在单处机环境中是
无用的,如果用了,这时一个内核控制路径试图访问一个被
锁的数据结构,它一定进入无限循环状态,因此,没有机会
更新被保护的数据结构,也不存在释放的问题,
最终的结果将被系统挂起。
进一步研究请参考ULKIII第五章 内核同步技术

对这句话不太同意:
----------------
当然啦,自旋锁在单处机环境中是 无用的,如果用了,这时一个内核控制路径试图访问一个被
锁的数据结构,它一定进入无限循环状态,因此,没有机会更新被保护的数据结构,也不存在释放的问题,最终的结果将被系统挂起。
----------------
在ULK_3第五章中关于spin_lock有这样的描述.
As a general rule, kernel preemption is disabled in every critical region protected by spin locks. In the case of a uniprocessor system, the locks themselves are useless, and the spin lock primitives just disable or enable the kernel preemption. Please notice that kernel preemption is still enabled during the busy wait phase, thus a process waiting for a spin lock to be released could be replaced by a higher priority process.

也就是说在单机系统中使用 spin lock也不会有什么样的问题的.(对2.6 preemption enable kernel来说),而且可以用来enable 或 disable preemptive.
我译的这段内容并没有考虑单与多的一致性问题。
如果用,产生了问题,如何克服,下面就是解决的方法:
2。6 ----arbitrarily interleave
"lease notice that kernel preemption is still enabled during the busy wait phase, thus a process waiting for a spin lock to be released could

be replaced by a higher priority process."
ULK_3
1.6.5.4. Spin locks
In multiprocessor systems, semaphores are not always the best solution to the synchronization problems. Some kernel data structures should be protected from being concurrently accessed by kernel control paths that run on different CPUs. In this case, if the time required to update the data structure is short, a semaphore could be very inefficient. To check a semaphore, the kernel must insert a process in the semaphore list and then suspend it. Because both operations are relatively expensive, in the time it takes to complete them, the other kernel control path could have already released the semaphore.
In these cases, multiprocessor operating systems use spin locks . A spin lock is very similar to a semaphore, but it has no process list; when a process finds the lock closed by another process, it "spins" around repeatedly, executing a tight instruction loop until the lock becomes open.
Of course, spin locks are useless in a uniprocessor environment. When a kernel control path tries to access a locked data structure, it starts an endless loop. Therefore, the kernel control path that is updating the protected data structure would not have a chance to continue the execution and release the spin lock. The final result would be that the system hangs.
Preemptive kernel
When compiled with the "reemptible Kernel" option, Linux 2.6 can arbitrarily interleave execution flows while they are in privileged mode. Besides Linux 2.6, a few other conventional, general-purpose Unix systems, such as Solaris and Mach 3.0 , are fully preemptive kernels. SVR4.2/MP introduces some fixed preemption points as a method to get limited preemption capability.
Preemptible Kernel-----》译成“抢占式内核”(个人认为)
All process switches are performed by the switch_to macro. In both preemptive and nonpreemptive kernels, a process switch occurs when a process has finished some thread of kernel activity and the scheduler is invoked. However,

in nonpreemptive kernels, the current process cannot be replaced unless it is about to switch to User Mode
我觉得 在LDDE_3中
5.5. Spinlocks
Spinlocks are, by their nature, intended for use on multiprocessor systems, although a uniprocessor workstation running a preemptive kernel behaves like SMP, as far as concurrency is concerned. If a nonpreemptive uniprocessor system ever went into a spin on a lock, it would spin forever; no other thread would ever be able to obtain the CPU to release the lock. For this reason, spinlock operations on uniprocessor systems without preemption enabled are optimized to do nothing, with the exception of the ones that change the IRQ masking status. Because of preemption, even if you never expect your code to run on an SMP system, you still need to implement proper locking.

更要准确一些。
实质上是一致的,由于章节的关系显得有些分散,同意你说的在LDDE_3安排的更好一些。
----Spinlocks的引入有利也有弊, 权衡之策,不同内核引入的方法也不同。
当然啦,自旋锁在单处机环境中是
无用的,如果用了,这时一个内核控制路径试图访问一个被
锁的数据结构,它一定进入无限循环状态,因此,没有机会
更新被保护的数据结构,也不存在释放的问题,
最终的结果将被系统挂起。

Of course, spin locks are useless in a uniprocessor environment. When a kernel control path tries to access a locked data structure, it starts an endless loop. Therefore, the kernel control path that is updating the protected data structure would not have a chance to continue the execution and release the spin lock. The final result would be that the system hangs.

个人以为这一段这样翻译比较确切:

当然,自旋锁在单处机环境中是
无用的。假设一个内核控制路径试图访问一个被
锁定的数据结构,它一定会进入无限循环状态(因为此时它不能被抢占)。
而正在更新被保护数据结构的内核控制路径就
没有机会更新被保护的数据结构以及释放自旋锁,
最终的结果将导致系统挂起。
呵呵这个在"深入理解LINUX内核"第一版中,绪论中就有说明的。
发新话题