Linux内核:Spinlock SMP:spin_lock_irq SMP版本中为什么会有preempt_disable()? [英] Linux Kernel: Spinlock SMP: Why there is a preempt_disable() in spin_lock_irq SMP version?

查看:189
本文介绍了Linux内核:Spinlock SMP:spin_lock_irq SMP版本中为什么会有preempt_disable()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux内核中的原始代码为:

static inline void __raw_spin_lock_irq(raw_spinlock_t *lock)
{
    local_irq_disable();
    preempt_disable();
    spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
    LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
}

我认为在禁用本地IRQ之后,没有执行路径可以抢占当前路径.

因为所有常见的硬IRQ均已禁用,所以不应出现softirq,也不应打勾以踢进度表.我认为目前的道路是安全的.那么为什么会有preempt_disable()?

据我所知,Dave Miller在2002年12月4日将preempt_disable()调用添加到包括spin_lock_irq在内的相当多的锁定原语中. ,并在2.5.51中发布.提交消息没有帮助;它只是说"[SPINLOCK]:修复非SMP触发自旋/rwlock宏."

我相信在可抢占内核下正确锁定文档对此进行了充分的解释.标题为使用中断禁用进行预防"的最后一节开始,

It is possible to prevent a preemption event using local_irq_disable and
local_irq_save.  Note, when doing so, you must be very careful ...

The original code in Linux kernel is:

static inline void __raw_spin_lock_irq(raw_spinlock_t *lock)
{
    local_irq_disable();
    preempt_disable();
    spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
    LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
}

I think there is no execution path can preempt current path after local IRQ is disabled.

Because all common hard IRQs are disabled, there should be no softirq occur and also no tick to kick schedule wheel. I think current path is safe. So why there is a preempt_disable()?

解决方案

As far as I can tell, preempt_disable() calls were added to quite a few locking primitives, including spin_lock_irq, by Dave Miller on December 4th, 2002, and released in 2.5.51. The commit message isn't helpful; it just says "[SPINLOCK]: Fix non-SMP nopping spin/rwlock macros."

I believe the Proper Locking Under a Preemptible Kernel documentation explains this well enough. The final section titled "PREVENTING PREEMPTION USING INTERRUPT DISABLING" begins,

It is possible to prevent a preemption event using local_irq_disable and
local_irq_save.  Note, when doing so, you must be very careful ...

这篇关于Linux内核:Spinlock SMP:spin_lock_irq SMP版本中为什么会有preempt_disable()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆