linux 使用spinlock的配对关系问题
大家使用spinlock的時(shí)候,一般是這么配對(duì):
spin_lock---------------------spin_unlock------------------最輕 spin_lock_bh----------------spin_unlock_bh-----------------較輕 spin_lock_irq----------------spin_unlock_irq---------------較重 spin_lock_irqsave---------spin_lock_irqrestore--------------最重那么,假設(shè)我使用?spin_lock_irqsave 來關(guān)中斷和關(guān)搶占,并保存之前的中斷狀態(tài),那么,我能不能使用spin_unlock來解鎖呢?
static inline int __mod_timer(struct timer_list *timer, unsigned long expires,bool pending_only, int pinned) {struct tvec_base *base, *new_base;unsigned long flags;int ret = 0 , cpu;timer_stats_timer_set_start_info(timer);BUG_ON(!timer->function);base = lock_timer_base(timer, &flags);ret = detach_if_pending(timer, base, false);if (!ret && pending_only)goto out_unlock;debug_activate(timer, expires);cpu = smp_processor_id();#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)if (!pinned && get_sysctl_timer_migration())cpu = get_nohz_timer_target(); #endifnew_base = per_cpu(tvec_bases, cpu);if (base != new_base) {/** We are trying to schedule the timer on the local CPU.* However we can't change timer's base while it is running,* otherwise del_timer_sync() can't detect that the timer's* handler yet has not finished. This also guarantees that* the timer is serialized wrt itself.*/if (likely(base->running_timer != timer)) {/* See the comment in lock_timer_base() */timer_set_base(timer, NULL);spin_unlock(&base->lock);base = new_base;spin_lock(&base->lock);timer_set_base(timer, base);}}timer->expires = expires;internal_add_timer(base, timer);out_unlock:spin_unlock_irqrestore(&base->lock, flags);-----------收尾很重要return ret; }答案就在這個(gè)函數(shù)中,看完這個(gè)函數(shù),你就不會(huì)這么淺顯地理解配對(duì)了,可能會(huì)更深地去理解spinlock的使用。
其實(shí)spinlock這種忙等的方式,使用場景一般要求,臨界區(qū)不能太大,且多路徑訪問的概率不那么高。這里的多路徑,就是指進(jìn)程上下文和中斷上下文,其中中斷上下文又分為硬中斷和軟中斷。
進(jìn)程上下文包括:用戶進(jìn)程,內(nèi)核線程,從調(diào)度角度來說,都?xì)w屬進(jìn)程上下文,可以睡眠。
中斷上下文包括:HW interrupt context(中斷handler)、軟中斷上下文(soft irq,當(dāng)然由于各種原因,該softirq被推遲到softirqd的內(nèi)核線程中執(zhí)行的時(shí)候就不屬于這個(gè)場景了,屬于進(jìn)程上下文那個(gè)分類了)、timer的callback函數(shù)(本質(zhì)上也是softirq)、tasklet(本質(zhì)上也是softirq)。
不同的控制路徑,使用不同的spinlock,比如只有進(jìn)程上下文訪問臨界區(qū),則使用spin_lock就夠了,如果有軟中斷和進(jìn)程上下文訪問臨界區(qū),則需要用spin_lock_bh,如果有硬中斷訪問和進(jìn)程上下文訪問,則用spin_lock_irq。
假設(shè)只有軟中斷訪問臨界區(qū),則只要關(guān)軟中斷就行,甚至都不需要spin-lock,
假設(shè)只有硬中斷訪問臨界區(qū),則只要關(guān)硬中斷就行,也不需要spin-lock
?如果有興趣完全弄懂,可以參考?蝸窩科技? http://www.wowotech.net/kernel_synchronization/spinlock.html
轉(zhuǎn)載于:https://www.cnblogs.com/10087622blog/p/9560979.html
總結(jié)
以上是生活随笔為你收集整理的linux 使用spinlock的配对关系问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 强如 Disruptor 也发生内存溢出
- 下一篇: VMware安装MikroTik Rou