標籤:linux核心 中斷處理 非強制中斷 中斷上下文
參考:
http://bbs.chinaunix.net/thread-2333484-1-1.html
http://liu1227787871.blog.163.com/blog/static/20536319720129210112658/
1、非強制中斷
一般來說,一次中斷服務的過程通常可以分為兩個部分。開頭的 部分往往必須在關中斷的條件下執行,這樣才能在不受幹擾的條件下“原子”地完成一些關鍵性操作,同時這部分操作的時間性又往往很強,必須在插斷要求發生後立即或至少在一定時間限制中執行,而且相繼的多次插斷要求也不能合并在一起來處理。而後半部分,通常可以而且應該在開中斷的條件下執行,這樣才不至於因中斷關閉過久而造成其他中斷的丟失,同時,這些操作常常允許延時到稍後才來執行,而且有可能多次中斷的相關部分合并在一起處理。這些不同的性質常常使中斷服務的前後兩半明顯地區分開來,可以而且應該分別加以不同的實現。這裡的後半部分就稱為"bottom half",在核心代碼中往往寫成bf ,而bf的這部分就可以通過軟體中斷來實現。因為軟體中斷的啟用是通過代碼來實現的,而不是硬體,所以就可以自己或由系統來決定啟用的時機!
1.1 註冊
還是以我最熟悉的兩個老朋友做為開篇:
open_softirq(NET_TX_SOFTIRQ, net_tx_action);
open_softirq(NET_RX_SOFTIRQ, net_rx_action);
open_softirq向核心註冊一個非強制中斷,其實質是設定非強制中斷向量表相應槽位,註冊其處理函數:
void open_softirq(int nr, void (*action)(struct softirq_action *)){ softirq_vec[nr].action = action;}
softirq_vec是整個非強制中斷的向量表:
void open_softirq(int nr, void (*action)(struct softirq_action*), void *data) { softirq_vec[nr].data = data; softirq_vec[nr].action = action;};static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
NR_SOFTIRQS是最大非強制中斷向量數,核心支援的所有非強制中斷如下:
enum{ HI_SOFTIRQ=0, TIMER_SOFTIRQ, NET_TX_SOFTIRQ, NET_RX_SOFTIRQ, BLOCK_SOFTIRQ, TASKLET_SOFTIRQ, SCHED_SOFTIRQ, HRTIMER_SOFTIRQ, RCU_SOFTIRQ, /* Preferable RCU should always be the last softirq */ NR_SOFTIRQS};
好像後為為RPS新增了一個,不過這我的核心版本偏低。
1.2 啟用
當需要調用非強制中斷時,需要調用raise_softirq函數啟用非強制中斷,這裡使用術語“啟用”而非“調用”,
是因為在很多情況下不能直接調用非強制中斷。所以只能快速地將其標誌為“可執行”,等待未來某一時刻調用。
為什麼“在很多情況下不能直接調用非強制中斷”?試想一下下半部引入的理念,就是為了讓上半部更快地執行。
如果在中斷程式碼中直接調用非強制中斷函數,那麼就失去了上半部與下半部的區別,也就是失去了其存在的意義。
核心使用一個名為__softirq_pending的位元影像來描述非強制中斷,每一個位對應一個非強制中斷,位元影像包含在結構irq_stat中:
typedef struct { unsigned int __softirq_pending; ……} ____cacheline_aligned irq_cpustat_t;DECLARE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
宏or_softirq_pending用於設定相應的位(位或操作):
#define or_softirq_pending(x) percpu_or(irq_stat.__softirq_pending, (x))
local_softirq_pending用於取得整個位元影像(而非某一位):
#define local_softirq_pending() percpu_read(irq_stat.__softirq_pending)
宏__raise_softirq_irqoff是or_softirq_pending的包裹:
#define __raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
raise_softirq_irqoff通過調用__raise_softirq_irqoff實現啟用非強制中斷,它的參數nr即位非強制中斷對應的位元影像槽位:
/** This function must run with irqs disabled!*/inline void raise_softirq_irqoff(unsigned int nr){ //置位元影像,即標記為可執行狀態 __raise_softirq_irqoff(nr); /* * If we're in an interrupt or softirq, we're done * (this also catches softirq-disabled code). We will * actually run the softirq once we return from * the irq or softirq. * * Otherwise we wake up ksoftirqd to make sure we * schedule the softirq soon. */ //設定了位元影像後,可以判斷是否已經沒有在中斷上下文中了,如果沒有,則是一個立即調用非強制中斷的好時機。 //in_interrupt另一個作用是判斷非強制中斷是否被禁用。 //wakeup_softirqd喚醒非強制中斷的守護進程ksoftirq。 if (!in_interrupt()) wakeup_softirqd();}複製代碼現在可以來看"啟用"非強制中斷的所有含義了,raise_softirq函數完成這一操作:void raise_softirq(unsigned int nr){ unsigned long flags; //所有操作,應該關閉中斷,避免嵌套調用 local_irq_save(flags); raise_softirq_irqoff(nr); local_irq_restore(flags);}
可見,啟用的操作,主要是兩點:
<1>、最重要的,就是置相應的位元影像,等待將來被處理;
<2>、如果此時已經沒有在中斷上下文中,則立即調用(其實是核心線程的喚醒操作),現在就是將來;
2、調度時機
是的,除了raise_softirq在,可能會(嗯,重要的是“可能”)通過wakeup_softirqd喚醒ksoftirqd外,還得明白非強制中斷的其它調用時機。
A、當do_IRQ完成了I/O中斷時調用irq_exit:
#ifdef __ARCH_IRQ_EXIT_IRQS_DISABLED# define invoke_softirq() __do_softirq()#else# define invoke_softirq() do_softirq()#endifvoid irq_exit(void){ account_system_vtime(current); trace_hardirq_exit(); sub_preempt_count(IRQ_EXIT_OFFSET); if (!in_interrupt() && local_softirq_pending()) invoke_softirq(); //調用非強制中斷
B、如果系統使用I/O APIC,在處理完本地時鐘中斷時:
void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs){ …… irq_exit(); ……}
C、local_bh_enable
local_bh_enable就是開啟下半部,當然重中之中就是非強制中斷了:void local_bh_enable(void){ _local_bh_enable_ip((unsigned long)__builtin_return_address(0));}static inline void _local_bh_enable_ip(unsigned long ip){ …… if (unlikely(!in_interrupt() && local_softirq_pending())) do_softirq(); ……}
D、在SMP中,當CPU處理完被CALL_FUNCTION_VECTOR處理器間中斷所觸發的函數時:
唔,對多核中CPU的之間的通訊不熟,不太清楚這個機制……
3、do_softirq
不論是哪種調用方式,最終都會觸發到非強制中斷的核心處理函數do_softirq,它處理當前CPU上的所有非強制中斷。
核心將非強制中斷設計盡量與平台無關,但是在某些情況下,它們還是會有差異,先來看一個x86 32位的do_softirq版本:
asmlinkage void do_softirq(void){ unsigned long flags; struct thread_info *curctx; union irq_ctx *irqctx; u32 *isp; //非強制中斷不能在中斷上下文內嵌套調用。中斷處理常式或下半部採用的是"啟用"方式。 if (in_interrupt()) return; //禁止中斷,儲存中斷標誌 local_irq_save(flags); //核心使用一個CPU位元影像,確實幾個非強制中斷可以同時在不同的CPU上運行,包括相同的非強制中斷。例如, //NET_RX_SOFTIRQ可以同時跑在多個處理器上。 //local_softirq_pending用於確定當前CPU的所有位元影像是否被設定。即是否有非強制中斷等待處理。 //回想一下經常發生的網卡接收資料處理:當網卡中斷落在哪一個CPU上時,與之相應的非強制中斷函數就會在其上執行。 //從這裡來看,實質就是哪個網卡中斷落在相應的CPU上,CPU置其非強制中斷位元影像,這裡做相應的檢測(這裡local_softirq_pending只 //是一個總的判斷,後面還有按位的判斷),檢測到有相應的位,執行之 if (local_softirq_pending()) { //取得線程描述符 curctx = current_thread_info(); //構造中斷上下文結構,softirq_ctx是每個CPU的非強制中斷上下文 //static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx); //這裡先取得當前CPU的非強制中斷上下文,然後為其賦初始值——儲存當前進程和棧指標 irqctx = __get_cpu_var(softirq_ctx); irqctx->tinfo.task = curctx->task; irqctx->tinfo.previous_esp = current_stack_pointer; /* build the stack frame on the softirq stack */ //構造中斷棧幀 isp = (u32 *) ((char *)irqctx + sizeof(*irqctx)); //call_on_stack切換核心棧,並在中斷上下文上執行函數__do_softirq call_on_stack(__do_softirq, isp); /* * Shouldnt happen, we returned above if in_interrupt(): */ WARN_ON_ONCE(softirq_count()); } //恢複之 local_irq_restore(flags);}
當配置了CONFIG_4KSTACKS,每個進程的thread_union只有4K,而非8K。發生中斷時,核心棧將不使用進程的核心棧,而使用每個 cpu的插斷要求棧。
核心棧將使用每個 cpu的插斷要求棧,而非進程的核心棧來執行非強制中斷函數:
static void call_on_stack(void *func, void *stack){ asm volatile("xchgl %%ebx,%%esp \n" //交換棧指標,中斷棧幀的指標stack做為傳入參數(%ebx),交換後esp是irq_ctx的棧頂,ebx是進程核心棧的棧 "call *%%edi \n" //調用非強制中斷函數 "movl %%ebx,%%esp \n" //恢複之,直接使用movl,而非xchgl是因為函數執行完畢,中斷的棧幀指標已經沒有用處了 : "=b" (stack) : "0" (stack), "D"(func) : "memory", "cc", "edx", "ecx", "eax");}
PS:所有的這些執行,應該都是在定義4K棧的基礎上的:
#ifdef CONFIG_4KSTACKS/** per-CPU IRQ handling contexts (thread information and stack)*/union irq_ctx { struct thread_info tinfo; u32 stack[THREAD_SIZE/sizeof(u32)];} __attribute__((aligned(PAGE_SIZE)));static DEFINE_PER_CPU(union irq_ctx *, hardirq_ctx);static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx);……static void call_on_stack(void *func, void *stack)……
是的,這個版本相對複雜,但是如果看了複雜的,再來看簡單的,就容易多了,當平台沒有定義do_softirq函數時(__ARCH_HAS_DO_SOFTIRQ),
核心提供了一個通用的:
#ifndef __ARCH_HAS_DO_SOFTIRQasmlinkage void do_softirq(void){ __u32 pending; unsigned long flags; if (in_interrupt()) return; local_irq_save(flags); pending = local_softirq_pending(); if (pending) __do_softirq(); local_irq_restore(flags);}#endif
無需更多的解釋,它非常的簡潔。
不論是哪個版本,都將調用__do_softirq函數:
asmlinkage void __do_softirq(void){ struct softirq_action *h; __u32 pending; int max_restart = MAX_SOFTIRQ_RESTART; int cpu; //儲存位元影像 pending = local_softirq_pending(); //處理序計量 account_system_vtime(current); //關閉本地CPU下半部。為了保證同一個CPU上的非強制中斷以串列方式執行。 __local_bh_disable((unsigned long)__builtin_return_address(0)); lockdep_softirq_enter(); //擷取本地CPU cpu = smp_processor_id();restart: /* Reset the pending bitmask before enabling irqs */ //清除位元影像 set_softirq_pending(0); //鎖中斷,只是為了保持位元影像的互斥,位元影像處理完畢。後面的代碼可以直接使用儲存的pending, //而中斷處理常式在啟用的時候,也可以放心地使用irq_stat.__softirq_pending。 //所以,可以開中斷了 local_irq_enable(); //取得非強制中斷向量 h = softirq_vec; //迴圈處理所有的非強制中斷 do { //逐步取位元影像的每一位,判斷該位上是否有非強制中斷被設定。若有,處理之 if (pending & 1) { //儲存搶佔計數器 int prev_count = preempt_count(); kstat_incr_softirqs_this_cpu(h - softirq_vec); trace_softirq_entry(h, softirq_vec); //調用非強制中斷 h->action(h); trace_softirq_exit(h, softirq_vec); //判斷非強制中斷是否被搶佔,如果是,則輸出一段錯誤資訊 if (unlikely(prev_count != preempt_count())) { printk(KERN_ERR "huh, entered softirq %td %s %p" "with preempt_count %08x," " exited with %08x?\n", h - softirq_vec, softirq_to_name[h - softirq_vec], h->action, prev_count, preempt_count()); preempt_count() = prev_count; } //??qsctr,這個是啥東東 rcu_bh_qsctr_inc(cpu); } //指向下一個非強制中斷槽位 h++; //移位,取下一個非強制中斷位 pending >>= 1; } while (pending); //當非強制中斷處理完畢後,因為前面已經開了中斷了,所以有可能新的非強制中斷已經又被設定, //非強制中斷發送器會嘗試重新非強制中斷,其最大重啟次數由max_restart決定。 //所以,這裡必須再次關閉中斷,再來一次…… local_irq_disable(); //取位元影像 pending = local_softirq_pending(); //有非強制中斷被設定,且沒有超過最大重啟次數,再來一次先 if (pending && --max_restart) goto restart; //超過最大重啟次數,還有非強制中斷待處理,調用wakeup_softirqd。其任處是喚醒非強制中斷守護進程ksoftirqd。 if (pending) wakeup_softirqd(); lockdep_softirq_exit(); account_system_vtime(current); //恢複下半部 _local_bh_enable();}
中斷跟蹤
如果中斷跟蹤CONFIG_TRACE_IRQFLAGS被定義,lockdep_softirq_enter/lockdep_softirq_exit用於遞增/遞減當前進程的非強制中斷上下文計數器softirq_context:
# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
複製代碼
trace_softirq_entry與trace_softirq_exit配合使用,可以用於判斷非強制中斷的延遲。
好像非強制中斷不太難,沒有更多的內容了。歡迎大家回貼補充。
淺析Linux的非強制中斷的實現