天天看點

linux核心架構-中斷處理機制

本文對中斷系統進行了全面的分析與探讨,主要包括中斷控制器、中斷分類、中斷親和力、中斷線程化與 SMP 中的中斷遷徙等。首先對中斷工作原理進行了簡要分析,接着詳細探讨了中斷親和力的實作原理,最後對中斷線程化與非線程化中斷之間的實作機理進行了對比分析。在每一部分都對linux3.2.0核心中arm架構的中斷機理進行了研究對比。

1 什麼是中斷?

Linux 核心需要對連接配接到計算機上的所有硬體裝置進行管理,毫無疑問這是它的份内事。如果要管理這些裝置,首先得和它們互相通信才行,一般有兩種方案可實作這種功能:

輪詢(polling) 讓核心定期對裝置的狀态進行查詢,然後做出相應的處理;

中斷(interrupt) 讓硬體在需要的時候向核心發出信号(變核心主動為硬體主動)。

輪詢因其周期性的執行,影響效率,故而采用中斷方式管理所有外設。

硬體中斷(hardware interrupt):由系統自身和與之連接配接的外設自動産生。它們用于實作更高效的實作裝置驅動程式,也用于引起處理器自身對異常和錯誤的關注,這些是需要與核心代碼進行互動的。

軟中斷(SoftIRQ):用于有效實作核心中的延期操作。

中斷工作流程:

從實體學的角度看,中斷是一種電信号,由硬體裝置産生,并直接送入中斷控制器(如 8259A)的輸入引腳上,然後再由中斷控制器向處理器發送相應的信号。處理器一經檢測到該信号,便中斷自己目前正在處理的工作,轉而去進行中斷。此後,處理器會通知 OS 已經産生中斷。這樣,OS 就可以對這個中斷進行适當的處理。不同的裝置對應的中斷不同,而每個中斷都通過一個唯一的數字辨別,這些值通常被稱為中斷請求線。

2 APIC vs 8259A

X86計算機的 CPU 為中斷隻提供了兩條外接引腳:NMI 和 INTR。其中 NMI 是不可屏蔽中斷,它通常用于電源掉電和實體存儲器奇偶校驗;INTR是可屏蔽中斷,可以通過設定中斷屏蔽位來進行中斷屏蔽,它主要用于接受外部硬體的中斷信号,這些信号由中斷控制器傳遞給 CPU。

常見的中斷控制器有兩種:

2.1 可程式設計中斷控制器8259A

傳統的 PIC(Programmable Interrupt Controller)是由兩片 8259A 風格的外部晶片以“級聯”的方式連接配接在一起。每個晶片可處理多達 8 個不同的 IRQ。因為從 PIC 的 INT 輸出線連接配接到主 PIC 的 IRQ2 引腳,是以可用 IRQ 線的個數達到 15 個,如圖 1 所示。

linux核心架構-中斷處理機制

2.2 進階可程式設計中斷控制器(APIC)

8259A 隻适合單 CPU 的情況,為了充分挖掘 SMP 體系結構的并行性,能夠把中斷傳遞給系統中的每個 CPU 至關重要。基于此理由,Intel 引入了一種名為 I/O 進階可程式設計控制器的新元件,來替代老式的 8259A 可程式設計中斷控制器。該元件包含兩大組成部分:一是“本地 APIC”,主要負責傳遞中斷信号到指定的處理器;舉例來說,一台具有三個處理器的機器,則它必須相對的要有三個本地 APIC。另外一個重要的部分是 I/O APIC,主要是收集來自 I/O 裝置的 Interrupt 信号且在當那些裝置需要中斷時發送信号到本地 APIC,系統中最多可擁有 8 個 I/O APIC。

每個本地 APIC 都有 32 位的寄存器,一個内部時鐘,一個本地定時裝置以及為本地中斷保留的兩條額外的 IRQ 線 LINT0 和 LINT1。所有本地 APIC 都連接配接到 I/O APIC,形成一個多級 APIC 系統,如圖 2 所示。

linux核心架構-中斷處理機制

目前大部分單處理器系統都包含一個 I/O APIC 晶片,可以通過以下兩種方式來對這種晶片進行配置:

1) 作為一種标準的 8259A 工作方式。本地 APIC 被禁止,外部 I/O APIC 連接配接到 CPU,兩條 LINT0 和 LINT1 分别連接配接到 INTR 和 NMI 引腳。

2) 作為一種标準外部 I/O APIC。本地 APIC 被激活,且所有的外部中斷都通過 I/O APIC 接收。

辨識一個系統是否正在使用 I/O APIC,可以在指令行輸入如下指令:

[email protected]:~$ cat /proc/interrupts 
           CPU0       CPU1       
  0:         44          0   IO-APIC-edge      timer
  1:          3          0   IO-APIC-edge      i8042
  6:       1528       2111   IO-APIC-edge      floppy
  7:          0          0   IO-APIC-edge      parport0
  8:          0          1   IO-APIC-edge      rtc0
  9:          2          1   IO-APIC-fasteoi   acpi
 12:          1          3   IO-APIC-edge      i8042
 14:          0          0   IO-APIC-edge      ata_piix
 15:          0          0   IO-APIC-edge      ata_piix
 ......
           

果輸出結果中列出了 IO-APIC,說明您的系統正在使用 APIC。如果看到 XT-PIC,意味着您的系統正在使用 8259A 晶片。

2.3 ARM Cortex-A8中斷研究

ARM Cortex-A8處理器是第一款基于ARMv7架構的産品。Sitara家族AM335x系列 是基于ARM ® Cortex-A8 微處理器。中斷控制器能夠控制多達 128個中斷請求。微處理器的ARM中斷控制器(AINTC)負責優化系統外設發送的服務請求,産生nIRQ 或nFIQ,發送給cpu。中斷類型和中斷輸入優先級是可程式設計的。AINTC 和 ARM 微處理器通過AXI2OCP橋相連,遵循AXI協定,運作速率是微處理器的一半。

AINTC的通用特點:

• 多達128個中斷請求

• 每個中斷輸入都可定制優先級

• 每個中斷輸入能被指定為nFIQ 或 nIRQ

• nFIQ 和 nIRQ具有獨立的優先級排序

注意:AXI(Advanced eXtensible Interface)是一種總線協定,該協定是ARM公司提出的AMBA(Advanced Microcontroller Bus Architecture)3.0協定中最重要的部分,是一種面向高性能、高帶寬、低延遲的片内總線。它的位址/控制和資料相位是分離的,支援不對齊的資料傳輸,同時在突發傳輸中,隻需要首位址,同時分離的讀寫資料通道、并支援Outstanding傳輸通路和亂序通路,并更加容易進行時序收斂。AXI 是AMBA 中一個新的高性能協定。AXI 技術豐富了現有的AMBA 标準内容,滿足超高性能和複雜的片上系統(SoC)設計的需求。

2.4 ARM Cortex-A8 和 x86架構的中斷硬體比較

ARM Cortex-A8一般使用ARM中斷控制器(AINTC)與MPU相連,連接配接方式與X86使用8259A的方式相似,隻是遵循的協定不同。

3 中斷分類

中斷可分為同步(synchronous)中斷和異步(asynchronous)中斷:

3.1. 同步中斷是當指令執行時由 CPU 控制單元産生,之是以稱為同步,是因為隻有在一條指令執行完畢後 CPU 才會發出中斷,而不是發生在代碼指令執行期間,比如系統調用。

3.2. 異步中斷是指由其他硬體裝置依照 CPU 時鐘信号随機産生,即意味着中斷能夠在指令之間發生,例如鍵盤中斷。

根據 Intel 官方資料,同步中斷稱為異常(exception),異步中斷被稱為中斷(interrupt)。

中斷可分為可屏蔽中斷(Maskable interrupt)和非屏蔽中斷(Nomaskable interrupt)。異常可分為故障(fault)、陷阱(trap)、終止(abort)三類。

從廣義上講,中斷可分為四類:中斷、故障、陷阱、終止。這些類别之間的異同點請參看 表 1。

表 1:中斷類别及其行為

類别 原因 異步/同步 傳回行為
中斷 來自I/O裝置的信号 異步 總是傳回到下一條指令
陷阱 有意的異常 同步 總是傳回到下一條指令
故障 潛在可恢複的錯誤 同步 傳回到目前指令
終止 不可恢複的錯誤 同步 不會傳回

X86 體系結構的每個中斷都被賦予一個唯一的編号或者向量(8 位無符号整數)。非屏蔽中斷和異常向量是固定的,而可屏蔽中斷向量可以通過對中斷控制器的程式設計來改變。

4 Linux 2.6 中斷處理原理簡介

中斷描述符表(Interrupt Descriptor Table,IDT)是一個系統表,它與每一個中斷或異常向量相聯系,每一個向量在表中存放的是相應的中斷或異常處理程式的入口位址。核心在允許中斷發生前,也就是在系統初始化時,必須把 IDT 表的初始化位址裝載到 idtr 寄存器中,初始化表中的每一項。

當處于實模式下時,IDT 被初始化并由 BIOS 程式所使用。然而,一旦 Linux 開始接管,IDT 就被移到 ARM 的另一個區域,并進行第二次初始化,因為 Linux 不使用任何 BIOS 程式,而使用自己專門的中斷服務程式(例程)(interrupt service routine,ISR)。中斷和異常處理程式很像正常的 C 函數,有三個主要的資料結構包含了與 IRQ 相關的所有資訊:

hw_interrupt_type

irq_desc_t

irqaction

,圖3 解釋了它們之間是如何關聯的。

linux核心架構-中斷處理機制

在 X86 系統中,對于 8259A 和 I/O APIC 這兩種不同類型的中斷控制器,

hw_interrupt_type

結構體被賦予不同的值,具體差別參見表 2。

表 2:8259A 和 I/O APIC PIC 的差別

8259A I/O APIC
static struct hw_interrupt_type i8259A_irq_type = { 
"XT-PIC", 
startup_8259A_irq, 
shutdown_8259A_irq, 
enable_8259A_irq, 
disable_8259A_irq, 
mask_and_ack_8259A, 
end_8259A_irq, 
NULL }; 
           
static struct hw_interrupt_type ioapic_edge_type ={
.typename = "IO-APIC-edge",
.startup = startup_edge_ioapic,
.shutdown = shutdown_edge_ioapic,
.enable = enable_edge_ioapic,
.disable = disable_edge_ioapic,
.ack = ack_edge_ioapic,
.end = end_edge_ioapic,
.set_affinity = set_ioapic_affinity,};
static struct hw_interrupt_type ioapic_level_type = {
.typename = "IO-APIC-level",
.startup = startup_level_ioapic,
.shutdown = shutdown_level_ioapic,
.enable = enable_level_ioapic,
.disable = disable_level_ioapic,
.ack = mask_and_ack_level_ioapic,
.end = end_level_ioapic,
.set_affinity = set_ioapic_affinity,};
           

在中斷初始化階段,調用

hw_interrupt_type

類型的變量初始化

irq_desc_t

結構中的

handle

成員。在早期的系統中使用級聯的8259A,是以将用

i8259A_irq_type

來進行初始化,而對于SMP系統來說,要麼以

ioapic_edge_type

,或以

ioapic_level_type

來初始化

handle

變量。

對于每一個外設,要麼以靜态(聲明為

static

類型的全局變量)或動态(調用

request_irq

函數)的方式向 Linux 核心注冊中斷處理程式。不管以何種方式注冊,都會聲明或配置設定一塊

irqaction

結構(其中

handler

指向中斷服務程式),然後調用

setup_irq()

函數,将

irq_desc_t

irqaction

聯系起來。

當中斷發生時,通過中斷描述符表 IDT 擷取中斷服務程式入口位址,對于

32≤ i ≤255(i≠128)

之間的中斷向量,将會執行

push $i-256,jmp common_interrupt

指令。随之将調用

do_IRQ()

函數,以中斷向量為

irq_desc[]

結構的下标,擷取

action

的指針,然後調用

handler

所指向的中斷服務程式。

從以上描述,我們不難看出整個中斷的流程,如圖 4 所示:

linux核心架構-中斷處理機制

4.2 linux-3.2.0-m3352/arch/arm/include/asm$

4.2.1 IRQ控制器抽象定義

/**
 * struct irq_desc - interrupt descriptor
 * @irq_data:		per irq and chip data passed down to chip functions
 * @timer_rand_state:	pointer to timer rand state struct
 * @kstat_irqs:		irq stats per cpu
 * @handle_irq:		highlevel irq-events handler
 * @preflow_handler:	handler called before the flow handler (currently used by sparc)
 * @action:		the irq action chain
 * @status:		status information
 * @core_internal_state__do_not_mess_with_it: core internal status information
 * @depth:		disable-depth, for nested irq_disable() calls
 * @wake_depth:		enable depth, for multiple irq_set_irq_wake() callers
 * @irq_count:		stats field to detect stalled irqs
 * @last_unhandled:	aging timer for unhandled count
 * @irqs_unhandled:	stats field for spurious unhandled interrupts
 * @lock:		locking for SMP
 * @affinity_hint:	hint to user space for preferred irq affinity
 * @affinity_notify:	context for notification of affinity changes
 * @pending_mask:	pending rebalanced interrupts
 * @threads_oneshot:	bitfield to handle shared oneshot threads
 * @threads_active:	number of irqaction threads currently running
 * @wait_for_threads:	wait queue for sync_irq to wait for threaded handlers
 * @dir:		/proc/irq/ procfs entry
 * @name:		flow handler name for /proc/interrupts output
 */
struct irq_desc {
	struct irq_data		irq_data;
	struct timer_rand_state *timer_rand_state;
	unsigned int __percpu	*kstat_irqs;
	irq_flow_handler_t	handle_irq;
#ifdef CONFIG_IRQ_PREFLOW_FASTEOI
	irq_preflow_handler_t	preflow_handler;
#endif
	struct irqaction	*action;	/* IRQ action list */
	unsigned int		status_use_accessors;
	unsigned int		core_internal_state__do_not_mess_with_it;
	unsigned int		depth;		/* nested irq disables */
	unsigned int		wake_depth;	/* nested wake enables */
	unsigned int		irq_count;	/* For detecting broken IRQs */
	unsigned long		last_unhandled;	/* Aging timer for unhandled count */
	unsigned int		irqs_unhandled;
	raw_spinlock_t		lock;
	struct cpumask		*percpu_enabled;
#ifdef CONFIG_SMP
	const struct cpumask	*affinity_hint;
	struct irq_affinity_notify *affinity_notify;
#ifdef CONFIG_GENERIC_PENDING_IRQ
	cpumask_var_t		pending_mask;
#endif
#endif
	unsigned long		threads_oneshot;
	atomic_t		threads_active;
	wait_queue_head_t       wait_for_threads;
#ifdef CONFIG_PROC_FS
	struct proc_dir_entry	*dir;
#endif
	struct module		*owner;
	const char		*name;
} ____cacheline_internodealigned_in_smp;
           
/**
 * struct irq_data - per irq and irq chip data passed down to chip functions
 * @irq:        中斷号
 * @hwirq:        硬體中斷号, local to the interrupt domain
 * @node:        node index useful for balancing
 * @state_use_accessors: status information for irq chip functions.
 *            Use accessor functions to deal with it
 * @chip:        low level interrupt hardware access
 * @domain:        Interrupt translation domain; responsible for mapping
 *            between hwirq number and linux irq number.
 * @handler_data:    per-IRQ data for the irq_chip methods
 * @chip_data:        platform-specific per-chip private data for the chip
 *            methods, to allow shared chip implementations
 * @msi_desc:        MSI descriptor
 * @affinity:        IRQ affinity on SMP
 *
 * The fields here need to overlay the ones in irq_desc until we
 * cleaned up the direct references and switched everything over to
 * irq_data.
 */
           
struct irq_data {
	unsigned int		irq;
	unsigned long		hwirq;
	unsigned int		node;
	unsigned int		state_use_accessors;
	struct irq_chip		*chip;
	struct irq_domain	*domain;
	void			*handler_data;
	void			*chip_data;
	struct msi_desc		*msi_desc;
#ifdef CONFIG_SMP
	cpumask_var_t		affinity;
#endif
};
           
/**
 * struct irq_chip - hardware interrupt chip descriptor
 *
 * @name:     在/proc/interrupts中顯示的名稱,用于辨別硬體控制器。在IA-32系統上可能值是“XTPIC”和“IO-APIC”,在AMD64系統上大多數時候也會使用後者。具體依據系統。
 * @irq_startup:    指向一個函數,用于第一次初始化一個IRQ。初始化工作僅限于啟用該IRQ。因而,startup函數實際上就是将工作轉給enable(預設情況下為enable)
 * @irq_shutdown:    關閉中斷 (defaults to ->disable if NULL)
 * @irq_enable:        激活一個IRQ。也就是IRQ從禁用狀态到啟動狀态的轉換 (defaults to chip->unmask if NULL)
 * @irq_disable:    與enable相對
 * @irq_ack:        start of a new interrupt
 * @irq_mask:        mask an interrupt source
 * @irq_mask_ack:    ack and mask an interrupt source
 * @irq_unmask:        unmask an interrupt source
 * @irq_eoi:                   end of interrupt
 * @irq_set_affinity:    set the CPU affinity on SMP machines
 * @irq_retrigger:    resend an IRQ to the CPU
 * @irq_set_type:    set the flow type (IRQ_TYPE_LEVEL/etc.) of an IRQ
 * @irq_set_wake:    enable/disable power-management wake-on of an IRQ
 * @irq_bus_lock:    function to lock access to slow bus (i2c) chips
 * @irq_bus_sync_unlock:function to sync and unlock slow bus (i2c) chips
 * @irq_cpu_online:    configure an interrupt source for a secondary CPU
 * @irq_cpu_offline:    un-configure an interrupt source for a secondary CPU
 * @irq_suspend:                    function called from core code on suspend once per chip
 * @irq_resume:                     function called from core code on resume once per chip
 * @irq_pm_shutdown:         function called from core code on shutdown once per chip
 * @irq_print_chip:                optional to print special chip info in show_interrupts
 * @flags:                                 chip specific flags
 *
 * @release:        release function solely used by UML
 */
struct irq_chip {
	const char	*name;
	unsigned int	(*irq_startup)(struct irq_data *data);
	void		(*irq_shutdown)(struct irq_data *data);
	void		(*irq_enable)(struct irq_data *data);
	void		(*irq_disable)(struct irq_data *data);

	void		(*irq_ack)(struct irq_data *data);
	void		(*irq_mask)(struct irq_data *data);
	void		(*irq_mask_ack)(struct irq_data *data);
	void		(*irq_unmask)(struct irq_data *data);
	void		(*irq_eoi)(struct irq_data *data);

	int		(*irq_set_affinity)(struct irq_data *data, const struct cpumask *dest, bool force);
	int		(*irq_retrigger)(struct irq_data *data);
	int		(*irq_set_type)(struct irq_data *data, unsigned int flow_type);
	int		(*irq_set_wake)(struct irq_data *data, unsigned int on);

	void		(*irq_bus_lock)(struct irq_data *data);
	void		(*irq_bus_sync_unlock)(struct irq_data *data);

	void		(*irq_cpu_online)(struct irq_data *data);
	void		(*irq_cpu_offline)(struct irq_data *data);

	void		(*irq_suspend)(struct irq_data *data);
	void		(*irq_resume)(struct irq_data *data);
	void		(*irq_pm_shutdown)(struct irq_data *data);

	void		(*irq_print_chip)(struct irq_data *data, struct seq_file *p);

	unsigned long	flags;

	/* Currently used only by UML, might disappear one day.*/
#ifdef CONFIG_IRQ_RELEASE_METHOD
	void		(*release)(unsigned int irq, void *dev_id);
#endif
};
           

中斷控制器晶片實作的一個特定例子,就是AMD64系統上的IO-APIC(代碼位置linux/arch/x86/kernel/apic/io_apic.c)。有下列定義給出:

static struct irq_chip ioapic_chip __read_mostly = {
	.name			= "IO-APIC",
	.irq_startup		= startup_ioapic_irq,
	.irq_mask		= mask_ioapic_irq,
	.irq_unmask		= unmask_ioapic_irq,
	.irq_ack		= ack_apic_edge,
	.irq_eoi		= ack_apic_level,
#ifdef CONFIG_SMP
	.irq_set_affinity	= ioapic_set_affinity,
#endif
	.irq_retrigger		= ioapic_retrigger_irq,
};
           

基于AXI的ARM中斷控制器的資料結構的執行個體化。

static struct irq_chip gic_chip = {
	.name			= "GIC",
	.irq_mask		= gic_mask_irq,
	.irq_unmask		= gic_unmask_irq,
	.irq_eoi		= gic_eoi_irq,
	.irq_set_type		= gic_set_type,
	.irq_retrigger		= gic_retrigger,
#ifdef CONFIG_SMP
	.irq_set_affinity	= gic_set_affinity,
#endif
	.irq_set_wake		= gic_set_wake,
};
           

處理程式函數用到的資料結構體如下所示,在<linux/include/linux/interrupt.h>中定義:

/**
 * struct irqaction - per interrupt action descriptor
 * @handler:	interrupt handler function
 * @flags:	flags (see IRQF_* above)
 * @name:	name of the device
 * @dev_id:	cookie to identify the device
 * @percpu_dev_id:	cookie to identify the device
 * @next:	pointer to the next irqaction for shared interrupts
 * @irq:	interrupt number
 * @dir:	pointer to the proc/irq/NN/name entry
 * @thread_fn:	interrupt handler function for threaded interrupts
 * @thread:	thread pointer for threaded interrupts
 * @thread_flags:	flags related to @thread
 * @thread_mask:	bitmask for keeping track of @thread activity
 */
struct irqaction {
	irq_handler_t		<span style="background-color: rgb(255, 255, 102);"><span style="color:#FF0000;">handler;</span></span>
	unsigned long		flags;
	void			*dev_id;
	void __percpu		*percpu_dev_id;
	struct irqaction	*next;
	int			irq;
	irq_handler_t		thread_fn;
	struct task_struct	*thread;
	unsigned long		thread_flags;
	unsigned long		thread_mask;
	const char		*name;
	struct proc_dir_entry	*dir;
} ____cacheline_internodealigned_in_smp;
           

該結構中最重要的成員便是處理程式函數本身,即handler成員,這是一個函數指針,位于結構的起始處。在裝置請求一個系統中斷,而中斷控制器通過引發中斷将該請求轉發到處理器的時候,将調用該處理程式函數。

name和dev_id唯一地标示一個指針,指向在所有核心資料結構中唯一表示了該裝置的資料結構執行個體,例如網卡的net_device執行個體。如果幾個裝置共享一個IRQ,那麼IRQ編号自身不能辨別該裝置,此時,在删除處理程式函數時,将需要上述資訊。

flags是一個标志變量,通過位圖描述了IRQ(和相關的中斷)的一些特性,位圖中的各個标志位照例可以通過預定義的常數通路。<interrupt.h>

5 中斷綁定——中斷親和力(IRQ Affinity)

在 SMP 體系結構中,我們可以通過調用系統調用和一組相關的宏來設定 CPU 親和力(CPU affinity),将一個或多個程序綁定到一個或多個處理器上運作。中斷在這方面也毫不示弱,也具有相同的特性。中斷親和力是指将一個或多個中斷源綁定到特定的 CPU 上運作。中斷親和力最初由 Ingo Molnar 設計并實作。

/proc/irq

目錄中,對于已經注冊中斷處理程式的硬體裝置,都會在該目錄下存在一個以該中斷号命名的目錄

IRQ#

IRQ#

目錄下有一個

smp_affinity

檔案(SMP 體系結構才有該檔案),它是一個 CPU 的位掩碼,可以用來設定該中斷的親和力, 預設值為

0xffffffff

,表明把中斷發送到所有的 CPU 上去處理。如果中斷控制器不支援

IRQ affinity

,不能改變此預設值,同時也不能關閉所有的 CPU 位掩碼,即不能設定成

0x0

我們以網卡(eth1,中斷号 44 )為例,在具有 8 個 CPU 的伺服器上來設定網卡中斷的親和力(以下資料出自核心源碼

Documentation\IRQ-affinity.txt

):

[[email protected] 44]# cat smp_affinity
ffffffff
[[email protected] 44]# echo 0f > smp_affinity
[[email protected] 44]# cat smp_affinity
0000000f
[[email protected] 44]# ping -f h
PING hell (195.4.7.3): 56 data bytes
...
--- hell ping statistics ---
6029 packets transmitted, 6027 packets received, 0% packet loss
round-trip min/avg/max = 0.1/0.1/0.4 ms
[[email protected] 44]# cat /proc/interrupts | grep 44:
 44:   0   1785   1785   1783   1783   1   1   0   IO-APIC-level   eth1
[[email protected] 44]# echo f0 > smp_affinity
[[email protected] 44]# ping -f h
PING hell (195.4.7.3): 56 data bytes
..
--- hell ping statistics ---
2779 packets transmitted, 2777 packets received, 0% packet loss
round-trip min/avg/max = 0.1/0.5/585.4 ms
[[email protected] 44]# cat /proc/interrupts | grep 44:
 44:  1068  1785  1785  1784   1784   1069   1070   1069   IO-APIC-level  eth1
[[email protected] 44]#      

在上例中,我們首先隻允許在 CPU0~3 上處理網卡中斷,接着運作 ping 程式,不難發現在 CPU4~7 上并沒有對網卡中斷進行處理。然後隻在 CPU4~7 上對網卡中斷進行處理, CPU0~3 不對網卡中斷進行任何處理,運作 ping 程式之後,再次檢視

/proc/interrupts

檔案時,不難發現 CPU4~7 上的中斷次數明顯增加,而 CPU0~3 上的中斷次數沒有太大的變化。

在探讨中斷親和力的實作原理之前,我們首先來了解 I/O APIC 中的組成。

I/O APIC 由一組 24 條 IRQ 線,一張 24 項的中斷重定向表(Interrupt Redirection Table),可程式設計寄存器,以及通過 APIC 總線發送和接收 APIC 資訊的一個資訊單元組成。其中與中斷親和力息息相關的是中斷重定向表,中斷重定向表表中的每一項都可以被單獨程式設計以指明中斷向量和優先級、目标處理器及選擇處理器的方式。

通過表 2,不難發現 8259A 和 APIC 中斷控制器最大不同點在于

hw_interrupt_type

類型變量的最後一項。對于 8259A 類型,

set_affinity

被置為

NULL

,而對于 SMP 的 APIC 類型,

set_affinity

被指派為

set_ioapic_affinity

在系統初始化期間,對于 SMP 體系結構,将會調用

setup_IO_APIC_irqs()

函數來初始化 I/O APIC 晶片,晶片中的中斷重定向表的 24 項被填充。在系統啟動期間,所有的 CPU 都執行

setup_local_APIC()

函數,完成本地的 APIC 初始化。當有中斷被觸發時,将相應的中斷重定向表中的值轉換成一條消息,然後,通過 APIC 總線把消息發送給一個或多個本地 APIC 單元,這樣,中斷就能立即被傳遞給一個特定的 CPU,或一組 CPU,或所有的 CPU,進而來實作中斷親和力。

當我們通過 cat 指令将 CPU 掩碼寫進

smp_affinity

檔案時,此時的調用路線圖為:

write()

->

sys_write()

->

vfs_write()

->

proc_file_write()

->

irq_affinity_write_proc()

->

set_affinity()

->

set_ioapic_affinity()

->

set_ioapic_affinity_irq()

->

io_apic_write()

;其中在調用

set_ioapic_affinity_irq()

函數時,以中斷号和 CPU 掩碼作為參數,接着繼續調用

io_apic_write()

,修改相應的中斷重定向中的值,來完成中斷親和力的設定。當執行 ping 指令時,網卡中斷被觸發,産生了一個中斷信号,多 APIC 系統根據中斷重定向表中的值,依照仲裁機制,選擇 CPU0~3 中的某一個 CPU,并将該信号傳遞給相應的本地 APIC,本地 APIC 又中斷它的 CPU,整個事件不通報給其他所有的 CPU。

6 新特性展望——中斷線程化(Interrupt Threads)

在嵌入式領域,業界對 Linux 實時性的呼聲越來越高,對中斷進行改造勢在必行。在 Linux 中,中斷具有最高的優先級。不論在任何時刻,隻要産生中斷事件,核心将立即執行相應的中斷處理程式,等到所有挂起的中斷和軟中斷處理完畢後才能執行正常的任務,是以有可能造成實時任務得不到及時的處理。中斷線程化之後,中斷将作為核心線程運作而且被賦予不同的實時優先級,實時任務可以有比中斷線程更高的優先級。這樣,具有最高優先級的實時任務就能得到優先處理,即使在嚴重負載下仍有實時性保證。

目前較新的 Linux 2.6.17 還不支援中斷線程化。但由 Ingo Molnar 設計并實作的實時更新檔,實作了中斷線程化。最新的下載下傳位址為:

http://people.redhat.com/~mingo/realtime-preempt/patch-2.6.17-rt9

下面将對中斷線程化進行簡要分析。

在初始化階段,中斷線程化的中斷初始化與正常中斷初始化大體上相同,在

start_kernel()

函數中都調用了

trap_init()

init_IRQ()

兩個函數來初始化

irq_desc_t

結構體,不同點主要展現在核心初始化建立

init

線程時,中斷線程化的中斷在

init()

函數中還将調用

init_hardirqs(kernel/irq/manage.c

(已經打過上文提到的更新檔)),來為每一個 IRQ 建立一個核心線程,最高實時優先級為 50,依次類推直到 25,是以任何 IRQ 線程的最低實時優先級為 25。

void __init init_hardirqs(void)
{
……
	for (i = 0; i < NR_IRQS; i++) {
		irq_desc_t *desc = irq_desc + i;
		if (desc->action && !(desc->status & IRQ_NODELAY))
			desc->thread = kthread_create(do_irqd, desc, "IRQ %d", irq);
    ……
	}
}
static int do_irqd(void * __desc)
{
    ……
	/*
	 * Scale irq thread priorities from prio 50 to prio 25
	 */
	param.sched_priority = curr_irq_prio;
	if (param.sched_priority > 25)
		curr_irq_prio = param.sched_priority - 1;
   ……
}      

如果某個中斷号狀态位中的 IRQ_NODELAY 被置位,那麼該中斷不能被線程化。

在中斷處理階段,兩者之間的異同點主要展現在:兩者相同的部分是當發生中斷時,CPU 将調用

do_IRQ()

函數來處理相應的中斷,

do_IRQ()

在做了必要的相關處理之後調用

__do_IRQ()

。兩者最大的不同點展現在

__do_IRQ()

函數中,在該函數中,将判斷該中斷是否已經被線程化(如果中斷描述符的狀态字段不包含

IRQ_NODELAY

标志,則說明該中斷被線程化了),對于沒有線程化的中斷,将直接調用

handle_IRQ_event()

函數來處理。

fastcall notrace unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs)
{
……
	if (redirect_hardirq(desc))
		goto out_no_end;
……
action_ret = handle_IRQ_event(irq, regs, action);
……
}
int redirect_hardirq(struct irq_desc *desc)
{
……
	if (!hardirq_preemption || (desc->status & IRQ_NODELAY) || !desc->thread)
		return 0;
……
	if (desc->thread && desc->thread->state != TASK_RUNNING)
		wake_up_process(desc->thread);
……
}      

對于已經線程化的情況,調用

wake_up_process()

函數喚醒中斷處理線程,并開始運作,核心線程将調用

do_hardirq()

來處理相應的中斷,該函數将判斷是否有中斷需要被處理,如果有就調用

handle_IRQ_event()

來處理。

handle_IRQ_event()

将直接調用相應的中斷處理函數來完成中斷處理。

不難看出,不管是線程化還是非線程化的中斷,最終都會執行

handle_IRQ_event()

函數來調用相應的中斷處理函數,隻是線程化的中斷處理函數是在核心線程中執行的。

并不是所有的中斷都可以被線程化,比如時鐘中斷,主要用來維護系統時間以及定時器等,其中定時器是作業系統的脈搏,一旦被線程化,就有可能被挂起,這樣後果将不堪設想,是以不應當被線程化。如果某個中斷需要被實時處理,它可以像時鐘中斷那樣,用

SA_NODELAY

标志來聲明自己非線程化,例如:

static struct irqaction irq0 = {
	timer_interrupt, SA_INTERRUPT | SA_NODELAY, CPU_MASK_NONE, "timer", NULL, NULL
};      

其中,

SA_NODELAY

IRQ_NODELAY

之間的轉換,是在

setup_irq()

函數中完成的。

7 中斷負載均衡—SMP體系結構下的中斷

中斷負載均衡的實作主要封裝在

arch\ arch\i386\kernel\io-apic.c

檔案中。如果在編譯核心時配置了

CONFIG_IRQBALANCE

選項,那麼 SMP 體系結構中的中斷負載均衡将以子產品的形式存在于核心中。

late_initcall(balanced_irq_init);
#define late_initcall(fn)		module_init(fn)  //include\linux\init.h      

balanced_irq_init()

函數中,将建立一個核心線程來負責中斷負載均衡:

static int __init balanced_irq_init(void)
{   ……
	printk(KERN_INFO "Starting balanced_irq\n");
	if (kernel_thread(balanced_irq, NULL, CLONE_KERNEL) >= 0) 
		return 0;
	else 
		printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
    ……
}      

balanced_irq()

函數中,每隔 5HZ=5s 的時間,将調用一次

do_irq_balance()

函數,進行中斷的遷徙。将重負載 CPU 上的中斷遷移到較空閑的CPU上進行處理。

8 總結

随着中斷親和力和中斷線程化的相繼實作,Linux 核心在 SMP 和實時性能方面的表現越來越讓人滿意,完全有理由相信,在不久的将來,中斷線程化将被合并到基線版本中。本文對中斷線程化的分析隻是起一個抛磚引玉的作用,當新特性釋出時,不至于讓人感到迷茫。

  • 注釋 1:輪詢也不是毫無用處,比如NAPI,就是輪詢與中斷相結合的經典案例。

9 參考資料:

http://www.ibm.com/developerworks/cn/linux/l-cn-linuxkernelint/

繼續閱讀