當前位置:首頁 > 嵌入式 > 嵌入式硬件
[導讀]在我修改linux源代碼的過程中曾被大量的內(nèi)核互斥現(xiàn)象所困擾,這需要利用內(nèi)核鎖去解決,雖然最后大部分解決,但我覺得應該留下些什么,也沒時間寫了,偶爾看見這位兄弟的文章,覺得正是我想整理的,所以拿出來給大家分享,關(guān)于bottom_half和中斷的問題,在tcp/ip半底中絕對不能對文件讀寫操作,不然就panic,恰恰我在linux中的增強功能就有這個操作,使我郁悶了很久,歡迎大家討論

/*e4gle:在我修改linux源代碼的過程中曾被大量的內(nèi)核互斥現(xiàn)象所困擾,這需要利用內(nèi)核鎖去解決,雖然最后大部分解決,但我覺得應該留下些什么,也沒時間寫了,偶爾看見這位兄弟的文章,覺得正是我想整理的,所以拿出來給大家分享,關(guān)于bottom_half和中斷的問題,在tcp/ip半底中絕對不能對文件讀寫操作,不然就panic,恰恰我在linux中的增強功能就有這個操作,使我郁悶了很久,歡迎大家討論
*/
內(nèi)核中的互斥之我見
by wheelz

看了前面各位的討論,我也有些想法,與大家商榷。
需要澄清的是,互斥手段的選擇,不是根據(jù)臨界區(qū)的大小,而是根據(jù)臨界區(qū)的性質(zhì),以及 有哪些部分的代碼,即哪些內(nèi)核執(zhí)行路徑來爭奪。
從嚴格意義上說,semaphore和spinlock_XXX屬于不同層次的互斥手段,前者的 實現(xiàn)有賴于后者,這有點象HTTP和TCP的關(guān)系,都是協(xié)議,但層次是不同的。
先說semaphore,它是進程級的,用于多個進程之間對資源的互斥,雖然也是在 內(nèi)核中,但是該內(nèi)核執(zhí)行路徑是以進程的身份,代表進程來爭奪資源的。如果 競爭不上,會有context switch,進程可以去sleep,但CPU不會停,會接著運行 其他的執(zhí)行路徑。從概念上說,這和單CPU或多CPU沒有直接的關(guān)系,只是在 semaphore本身的實現(xiàn)上,為了保證semaphore結(jié)構(gòu)存取的原子性,在多CPU中需要spinlock來互斥。
在內(nèi)核中,更多的是要保持內(nèi)核各個執(zhí)行路徑之間的數(shù)據(jù)訪問互斥,這是最基本的互斥問題,即保持數(shù)據(jù)修改的原子性。semaphore的實現(xiàn),也要依賴這個。在單CPU中,主要是中斷和bottom_half的問題,因此,開關(guān)中斷就可以了。在多CPU中,又加上了其他CPU的干擾,因此需要spinlock來幫助。這兩個部分結(jié)合起來,就形成了spinlock_XXX。它的特點是,一旦CPU進入了spinlock_XXX,它就不會干別的,而是一直空轉(zhuǎn),直到鎖定成功為止。因此,這就決定了被spinlock_XXX鎖住的臨界區(qū)不能停,更不能context switch,要存取完數(shù)據(jù)后趕快出來,以便其他的在空轉(zhuǎn)的執(zhí)行路徑能夠獲得spinlock。這也是spinlock的原則所在。如果當前執(zhí)行路徑一定要進行context switch,那就要在schedule()之前釋放spinlock,否則,容易死鎖。因為在中斷和bh中,沒有context,無法進行context switch,只能空轉(zhuǎn)等待spinlock,你context switch走了,誰知道猴年馬月才能回來。
因為spinlock的原意和目的就是保證數(shù)據(jù)修改的原子性,因此也沒有理由在spinlock 鎖住的臨界區(qū)中停留。
spinlock_XXX有很多形式,有
spin_lock()/spin_unlock(),
spin_lock_irq()/spin_unlock_irq(),
spin_lock_irqsave/spin_unlock_irqrestore()
spin_lock_bh()/spin_unlock_bh()
local_irq_disable/local_irq_enable
local_bh_disable/local_bh_enable
那么,在什么情況下具體用哪個呢?這要看是在什么內(nèi)核執(zhí)行路徑中,以及要與哪些內(nèi)核執(zhí)行路徑相互斥。我們知道,內(nèi)核中的執(zhí)行路徑主要有:
1 用戶進程的內(nèi)核態(tài),此時有進程context,主要是代表進程在執(zhí)行系統(tǒng)調(diào)用 等。
2 中斷或者異?;蛘咦韵莸龋瑥母拍钌险f,此時沒有進程context,不能進行
context switch。
3 bottom_half,從概念上說,此時也沒有進程context。
4 同時,相同的執(zhí)行路徑還可能在其他的CPU上運行。
這樣,考慮這四個方面的因素,通過判斷我們要互斥的數(shù)據(jù)會被這四個因素中
的哪幾個來存取,就可以決定具體使用哪種形式的spinlock。如果只要和其他CPU互斥,就要用spin_lock/spin_unlock,如果要和irq及其他CPU互斥,就要用
spin_lock_irq/spin_unlock_irq,如果既要和irq及其他CPU互斥,又要保存EFLAG的狀態(tài),就要用spin_lock_irqsave/spin_unlock_irqrestore,如果要和bh及其他CPU互斥,就要用spin_lock_bh/spin_unlock_bh,如果不需要和其他CPU互斥,只要和irq互斥,則用local_irq_disable/local_irq_enable,
如果不需要和其他CPU互斥,只要和bh互斥,則用local_bh_disable/local_bh_enable,
等等。值得指出的是,對同一個數(shù)據(jù)的互斥,在不同的內(nèi)核執(zhí)行路徑中,
所用的形式有可能不同(見下面的例子)。
舉一個例子。在中斷部分中有一個irq_desc_t類型的結(jié)構(gòu)數(shù)組變量irq_desc[],
該數(shù)組每個成員對應一個irq的描述結(jié)構(gòu),里面有該irq的響應函數(shù)等。
在irq_desc_t結(jié)構(gòu)中有一個spinlock,用來保證存取(修改)的互斥。
對于具體一個irq成員,irq_desc[irq],對其存取的內(nèi)核執(zhí)行路徑有兩個,一是
在設(shè)置該irq的響應函數(shù)時(setup_irq),這通常發(fā)生在module的初始化階段,或
系統(tǒng)的初始化階段;二是在中斷響應函數(shù)中(do_IRQ)。代碼如下:
int setup_irq(unsigned int irq, struct irqaction * new)
{
int shared = 0;
unsigned long flags;
struct irqaction *old, **p;
irq_desc_t *desc = irq_desc + irq;
/*
* Some drivers like serial.c use request_irq() heavily,
* so we have to be careful not to interfere with a
* running system.
*/
if (new->flags & SA_SAMPLE_RANDOM) {
/*
* This function might sleep, we want to call it first,
* outside of the atomic block.
* Yes, this might clear the entropy pool if the wrong
* driver is attempted to be loaded, without actually
* installing a new handler, but is this really a problem,
* only the sysadmin is able to do this.
*/
rand_initialize_irq(irq);
}
/*
* The following block of code has to be executed atomically
*/
[1] spin_lock_irqsave(&desc->lock,flags);
p = &desc->action;
if ((old = *p) != NULL) {
/* Can't share interrupts unless both agree to */
if (!(old->flags & new->flags & SA_SHIRQ)) {
[2] spin_unlock_irqrestore(&desc->lock,flags);
return -EBUSY;
}
/* add new interrupt at end of irq queue */
do {
p = &old->next;
old = *p;
} while (old);
shared = 1;
}
*p = new;
if (!shared) {
desc->depth = 0;
desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
desc->handler->startup(irq);
}
[3] spin_unlock_irqrestore(&desc->lock,flags);
register_irq_proc(irq);
return 0;
}
asmlinkage unsigned int do_IRQ(struct pt_regs regs)
{
/*
* We ack quickly, we don't want the irq controller
* thinking we're snobs just because some other CPU has
* disabled global interrupts (we have already done the
* INT_ACK cycles, it's too late to try to pretend to the
* controller that we aren't taking the interrupt).
*
* 0 return value means that this irq is already being
* handled by some other CPU. (or is disabled)
*/
int irq = regs.orig_eax & 0xff; /* high bits used in ret_from_ code */
int cpu = smp_processor_id();
irq_desc_t *desc = irq_desc + irq;
struct irqaction * action;
unsigned int status;
kstat.irqs[cpu][irq]++;
[4] spin_lock(&desc->lock);
desc->handler->ack(irq);
/*
REPLAY is when Linux resends an IRQ that was dropped earlier
WAITING is used by probe to mark irqs that are being tested
*/
status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
status |= IRQ_PENDING; /* we _want_ to handle it */
/*
* If the IRQ is disabled for whatever reason, we cannot
* use the action we have.
*/
action = NULL;
if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
action = desc->action;
status &= ~IRQ_PENDING; /* we commit to handling */
status |= IRQ_INPROGRESS; /* we are handling it */
}
desc->status = status;
/*
* If there is no IRQ handler or it was disabled, exit early.
Since we set PENDING, if another processor is handling
a different instance of this same irq, the other processor
will take care of it.
*/
if (!action)
goto out;
/*
* Edge triggered interrupts need to remember
* pending events.
* This applies to any hw interrupts that allow a second
* instance of the same irq to arrive while we are in do_IRQ
* or in the handler. But the code here only handles the _second_
* instance of the irq, not the third or fourth. So it is mostly
* useful for irq hardware that does not mask cleanly in an
* SMP environment.
*/
for (;;) {
[5] spin_unlock(&desc->lock);
handle_IRQ_event(irq, ®s, action);
[6] spin_lock(&desc->lock)

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉