一文完全读懂 Linux 中断处理

系统 Linux
中断 是为了解决外部设备完成某些工作后通知CPU的一种机制(譬如硬盘完成读写操作后通过中断告知CPU已经完成)。

[[432723]]

什么是中断

中断 是为了解决外部设备完成某些工作后通知CPU的一种机制(譬如硬盘完成读写操作后通过中断告知CPU已经完成)。早期没有中断机制的计算机就不得不通过轮询来查询外部设备的状态,由于轮询是试探查询的(也就是说设备不一定是就绪状态),所以往往要做很多无用的查询,从而导致效率非常低下。由于中断是由外部设备主动通知CPU的,所以不需要CPU进行轮询去查询,效率大大提升。

从物理学的角度看,中断是一种电信号,由硬件设备产生,并直接送入中断控制器(如 8259A)的输入引脚上,然后再由中断控制器向处理器发送相应的信号。处理器一经检测到该信号,便中断自己当前正在处理的工作,转而去处理中断。此后,处理器会通知 OS 已经产生中断。这样,OS 就可以对这个中断进行适当的处理。不同的设备对应的中断不同,而每个中断都通过一个唯一的数字标识,这些值通常被称为中断请求线。

中断控制器

X86计算机的 CPU 为中断只提供了两条外接引脚:NMI 和 INTR。其中 NMI 是不可屏蔽中断,它通常用于电源掉电和物理存储器奇偶校验;INTR是可屏蔽中断,可以通过设置中断屏蔽位来进行中断屏蔽,它主要用于接受外部硬件的中断信号,这些信号由中断控制器传递给 CPU。

常见的中断控制器有两种:

可编程中断控制器8259A

传统的 PIC(Programmable Interrupt Controller,可编程中断控制器)是由两片 8259A 风格的外部芯片以“级联”的方式连接在一起。每个芯片可处理多达 8 个不同的 IRQ。因为从 PIC 的 INT 输出线连接到主 PIC 的 IRQ2 引脚,所以可用 IRQ 线的个数达到 15 个,如图下所示。

8259A

高级可编程中断控制器(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 系统,如图下所示。

APIC

目前大部分单处理器系统都包含一个 I/O APIC 芯片,可以通过以下两种方式来对这种芯片进行配置:

  • 作为一种标准的 8259A 工作方式。本地 APIC 被禁止,外部 I/O APIC 连接到 CPU,两条 LINT0 和 LINT1 分别连接到 INTR 和 NMI 引脚。
  • 作为一种标准外部 I/O APIC。本地 APIC 被激活,且所有的外部中断都通过 I/O APIC 接收。

辨别一个系统是否正在使用 I/O APIC,可以在命令行输入如下命令:

# cat /proc/interrupts 
           CPU0        
  0:      90504    IO-APIC-edge  timer 
  1:        131    IO-APIC-edge  i8042 
  8:          4    IO-APIC-edge  rtc 
  9:          0    IO-APIC-level  acpi 
 12:        111    IO-APIC-edge  i8042 
 14:       1862    IO-APIC-edge  ide0 
 15:         28    IO-APIC-edge  ide1 
177:          9    IO-APIC-level  eth0 
185:          0    IO-APIC-level  via82cxxx 
... 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

如果输出结果中列出了 IO-APIC,说明您的系统正在使用 APIC。如果看到 XT-PIC,意味着您的系统正在使用 8259A 芯片。

中断分类

中断可分为同步(synchronous)中断和异步(asynchronous)中断:

  • 同步中断是当指令执行时由 CPU 控制单元产生,之所以称为同步,是因为只有在一条指令执行完毕后 CPU 才会发出中断,而不是发生在代码指令执行期间,比如系统调用。
  • 异步中断是指由其他硬件设备依照 CPU 时钟信号随机产生,即意味着中断能够在指令之间发生,例如键盘中断。

根据 Intel 官方资料,同步中断称为异常(exception),异步中断被称为中断(interrupt)。

中断可分为 可屏蔽中断(Maskable interrupt)和 非屏蔽中断(Nomaskable interrupt)。异常可分为 故障(fault)、陷阱(trap)、终止(abort)三类。

从广义上讲,中断可分为四类:中断、故障、陷阱、终止。这些类别之间的异同点请参看 表。

表:中断类别及其行为

类别 原因 异步/同步 返回行为
中断 来自I/O设备的信号 异步 总是返回到下一条指令
陷阱 有意的异常 同步 总是返回到下一条指令
故障 潜在可恢复的错误 同步 返回到当前指令
终止 不可恢复的错误 同步 不会返回
 

X86 体系结构的每个中断都被赋予一个唯一的编号或者向量(8 位无符号整数)。非屏蔽中断和异常向量是固定的,而可屏蔽中断向量可以通过对中断控制器的编程来改变。

中断处理 - 上半部(硬中断)

由于 APIC中断控制器 有点小复杂,所以本文主要通过 8259A中断控制器 来介绍Linux对中断的处理过程。

中断处理相关结构

前面说过,8259A中断控制器 由两片 8259A 风格的外部芯片以 级联 的方式连接在一起,每个芯片可处理多达 8 个不同的 IRQ(中断请求),所以可用 IRQ 线的个数达到 15 个。如下图:

8259A

在内核中每条IRQ线由结构体 irq_desc_t 来描述,irq_desc_t 定义如下:

typedef struct { 
    unsigned int status;        /* IRQ status */ 
    hw_irq_controller *handler; 
    struct irqaction *action;   /* IRQ action list */ 
    unsigned int depth;         /* nested irq disables */ 
    spinlock_t lock; 
} irq_desc_t; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

下面介绍一下 irq_desc_t 结构各个字段的作用:

  • status: IRQ线的状态。
  • handler: 类型为 hw_interrupt_type 结构,表示IRQ线对应的硬件相关处理函数,比如 8259A中断控制器 接收到一个中断信号时,需要发送一个确认信号才会继续接收中断信号的,发送确认信号的函数就是 hw_interrupt_type 中的 ack 函数。
  • action: 类型为 irqaction 结构,中断信号的处理入口。由于一条IRQ线可以被多个硬件共享,所以 action 是一个链表,每个 action 代表一个硬件的中断处理入口。
  • depth: 防止多次开启和关闭IRQ线。
  • lock: 防止多核CPU同时对IRQ进行操作的自旋锁。

hw_interrupt_type 这个结构与硬件相关,这里就不作介绍了,我们来看看 irqaction 这个结构:

struct irqaction { 
    void (*handler)(int, void *, struct pt_regs *); 
    unsigned long flags; 
    unsigned long mask; 
    const char *name
    void *dev_id; 
    struct irqaction *next
}; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

下面说说 irqaction 结构各个字段的作用:

  • handler: 中断处理的入口函数,handler 的第一个参数是中断号,第二个参数是设备对应的ID,第三个参数是中断发生时由内核保存的各个寄存器的值。
  • flags: 标志位,用于表示 irqaction 的一些行为,例如是否能够与其他硬件共享IRQ线。
  • name: 用于保存中断处理的名字。
  • dev_id: 设备ID。
  • next: 每个硬件的中断处理入口对应一个 irqaction 结构,由于多个硬件可以共享同一条IRQ线,所以这里通过 next 字段来连接不同的硬件中断处理入口。

irq_desc_t 结构关系如下图:

irq_desc_t

注册中断处理入口

在内核中,可以通过 setup_irq() 函数来注册一个中断处理入口。setup_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; 
    ... 
    spin_lock_irqsave(&desc->lock,flags); 
    p = &desc->action
    if ((old = *p) != NULL) { 
        if (!(old->flags & new->flags & SA_SHIRQ)) { 
            spin_unlock_irqrestore(&desc->lock,flags); 
            return -EBUSY; 
        } 
 
        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); 
    } 
    spin_unlock_irqrestore(&desc->lock,flags); 
 
    register_irq_proc(irq); // 注册proc文件系统 
    return 0; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

setup_irq() 函数比较简单,就是通过 irq 号来查找对应的 irq_desc_t 结构,并把新的 irqaction 连接到 irq_desc_t 结构的 action 链表中。要注意的是,如果设备不支持共享IRQ线(也即是 flags 字段没有设置 SA_SHIRQ 标志),那么就返回 EBUSY 错误。

我们看看 时钟中断处理入口 的注册实例:

static struct irqaction irq0  = { timer_interrupt, SA_INTERRUPT, 0, "timer"NULLNULL}; 
 
void __init time_init(void) 

    ... 
    setup_irq(0, &irq0); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

可以看到,时钟中断处理入口的IRQ号为0,处理函数为 timer_interrupt(),并且不支持共享IRQ线(flags 字段没有设置 SA_SHIRQ 标志)。

处理中断请求

当一个中断发生时,中断控制层会发送信号给CPU,CPU收到信号会中断当前的执行,转而执行中断处理过程。中断处理过程首先会保存寄存器的值到栈中,然后调用 do_IRQ() 函数进行进一步的处理,do_IRQ() 函数代码如下:

asmlinkage unsigned int do_IRQ(struct pt_regs regs) 

    int irq = regs.orig_eax & 0xff; /* 获取IRQ号  */ 
    int cpu = smp_processor_id(); 
    irq_desc_t *desc = irq_desc + irq; 
    struct irqaction * action
    unsigned int status; 
 
    kstat.irqs[cpu][irq]++; 
    spin_lock(&desc->lock); 
    desc->handler->ack(irq); 
 
    status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING); 
    status |= IRQ_PENDING; /* we _want_ to handle it */ 
 
    action = NULL
    if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) { // 当前IRQ不在处理中 
        action = desc->action;    // 获取 action 链表 
        status &= ~IRQ_PENDING;   // 去除IRQ_PENDING标志, 这个标志用于记录是否在处理IRQ请求的时候又发生了中断 
        status |= IRQ_INPROGRESS; // 设置IRQ_INPROGRESS标志, 表示正在处理IRQ 
    } 
    desc->status = status; 
 
    if (!action)  // 如果上一次IRQ还没完成, 直接退出 
        goto out
 
    for (;;) { 
        spin_unlock(&desc->lock); 
        handle_IRQ_event(irq, &regs, action); // 处理IRQ请求 
        spin_lock(&desc->lock); 
         
        if (!(desc->status & IRQ_PENDING)) // 如果在处理IRQ请求的时候又发生了中断, 继续处理IRQ请求 
            break; 
        desc->status &= ~IRQ_PENDING; 
    } 
    desc->status &= ~IRQ_INPROGRESS; 
out
 
    desc->handler->end(irq); 
    spin_unlock(&desc->lock); 
 
    if (softirq_active(cpu) & softirq_mask(cpu)) 
        do_softirq(); // 中断下半部处理 
    return 1; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

do_IRQ() 函数首先通过IRQ号获取到其对应的 irq_desc_t 结构,注意的是同一个中断有可能发生多次,所以要判断当前IRQ是否正在被处理当中(判断 irq_desc_t 结构的 status 字段是否设置了 IRQ_INPROGRESS 标志),如果不是处理当前,那么就获取到 action 链表,然后通过调用 handle_IRQ_event() 函数来执行 action 链表中的中断处理函数。

如果在处理中断的过程中又发生了相同的中断(irq_desc_t 结构的 status 字段被设置了 IRQ_INPROGRESS 标志),那么就继续对中断进行处理。处理完中断后,调用 do_softirq() 函数来对中断下半部进行处理(下面会说)。

接下来看看 handle_IRQ_event() 函数的实现:

int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action

    int status; 
    int cpu = smp_processor_id(); 
 
    irq_enter(cpu, irq); 
 
    status = 1; /* Force the "do bottom halves" bit */ 
 
    if (!(action->flags & SA_INTERRUPT)) // 如果中断处理能够在打开中断的情况下执行, 那么就打开中断 
        __sti(); 
 
    do { 
        status |= action->flags; 
        action->handler(irq, action->dev_id, regs); 
        action = action->next
    } while (action); 
    if (status & SA_SAMPLE_RANDOM) 
        add_interrupt_randomness(irq); 
    __cli(); 
 
    irq_exit(cpu, irq); 
 
    return status; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

handle_IRQ_event() 函数非常简单,就是遍历 action 链表并且执行其中的处理函数,比如对于 时钟中断 就是调用 timer_interrupt() 函数。这里要注意的是,如果中断处理过程能够开启中断的,那么就把中断打开(因为CPU接收到中断信号时会关闭中断)。

中断处理 - 下半部(软中断)

由于中断处理一般在关闭中断的情况下执行,所以中断处理不能太耗时,否则后续发生的中断就不能实时地被处理。鉴于这个原因,Linux把中断处理分为两个部分,上半部 和 下半部,上半部 在前面已经介绍过,接下来就介绍一下 下半部 的执行。

一般中断 上半部 只会做一些最基础的操作(比如从网卡中复制数据到缓存中),然后对要执行的中断 下半部 进行标识,标识完调用 do_softirq() 函数进行处理。

softirq机制

中断下半部 由 softirq(软中断) 机制来实现的,在Linux内核中,有一个名为 softirq_vec 的数组,如下:

static struct softirq_action softirq_vec[32]; 
  • 1.

其类型为 softirq_action 结构,定义如下:

struct softirq_action 

    void    (*action)(struct softirq_action *); 
    void    *data; 
}; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

softirq_vec 数组是 softirq 机制的核心,softirq_vec 数组每个元素代表一种软中断。但在Linux中只定义了四种软中断,如下:

enum 

    HI_SOFTIRQ=0, 
    NET_TX_SOFTIRQ, 
    NET_RX_SOFTIRQ, 
    TASKLET_SOFTIRQ 
}; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

HI_SOFTIRQ 是高优先级tasklet,而 TASKLET_SOFTIRQ 是普通tasklet,tasklet是基于softirq机制的一种任务队列(下面会介绍)。NET_TX_SOFTIRQ 和 NET_RX_SOFTIRQ 特定用于网络子模块的软中断(不作介绍)。

注册softirq处理函数

要注册一个softirq处理函数,可以通过 open_softirq() 函数来进行,代码如下:

void open_softirq(int nr, void (*action)(struct softirq_action*), void *data) 

    unsigned long flags; 
    int i; 
 
    spin_lock_irqsave(&softirq_mask_lock, flags); 
    softirq_vec[nr].data = data; 
    softirq_vec[nr].action = action
 
    for (i=0; i<NR_CPUS; i++) 
        softirq_mask(i) |= (1<<nr); 
    spin_unlock_irqrestore(&softirq_mask_lock, flags); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

open_softirq() 函数的主要工作就是向 softirq_vec 数组添加一个softirq处理函数。

Linux在系统初始化时注册了两种softirq处理函数,分别为 TASKLET_SOFTIRQ 和 HI_SOFTIRQ:

void __init softirq_init() 

    ... 
    open_softirq(TASKLET_SOFTIRQ, tasklet_action, NULL); 
    open_softirq(HI_SOFTIRQ, tasklet_hi_action, NULL); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

处理softirq

处理softirq是通过 do_softirq() 函数实现,代码如下:

asmlinkage void do_softirq() 

    int cpu = smp_processor_id(); 
    __u32 active, mask; 
 
    if (in_interrupt()) 
        return
 
    local_bh_disable(); 
 
    local_irq_disable(); 
    mask = softirq_mask(cpu); 
    active = softirq_active(cpu) & mask; 
 
    if (active) { 
        struct softirq_action *h; 
 
restart: 
        softirq_active(cpu) &= ~active; 
 
        local_irq_enable(); 
 
        h = softirq_vec; 
        mask &= ~active; 
 
        do { 
            if (active & 1) 
                h->action(h); 
            h++; 
            active >>= 1; 
        } while (active); 
 
        local_irq_disable(); 
 
        active = softirq_active(cpu); 
        if ((active &= mask) != 0) 
            goto retry; 
    } 
 
    local_bh_enable(); 
 
    return
 
retry: 
    goto restart; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

前面说了 softirq_vec 数组有32个元素,每个元素对应一种类型的softirq,那么Linux怎么知道哪种softirq需要被执行呢?在Linux中,每个CPU都有一个类型为 irq_cpustat_t 结构的变量,irq_cpustat_t 结构定义如下:

typedef struct { 
    unsigned int __softirq_active; 
    unsigned int __softirq_mask; 
    ... 
} irq_cpustat_t; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

其中 __softirq_active 字段表示有哪种softirq触发了(int类型有32个位,每一个位代表一种softirq),而 __softirq_mask 字段表示哪种softirq被屏蔽了。Linux通过 __softirq_active 这个字段得知哪种softirq需要执行(只需要把对应位设置为1)。

所以,do_softirq() 函数首先通过 softirq_mask(cpu) 来获取当前CPU对应被屏蔽的softirq,而 softirq_active(cpu) & mask 就是获取需要执行的softirq,然后就通过对比 __softirq_active 字段的各个位来判断是否要执行该类型的softirq。

tasklet机制

前面说了,tasklet机制是基于softirq机制的,tasklet机制其实就是一个任务队列,然后通过softirq执行。在Linux内核中有两种tasklet,一种是高优先级tasklet,一种是普通tasklet。这两种tasklet的实现基本一致,唯一不同的就是执行的优先级,高优先级tasklet会先于普通tasklet执行。

tasklet本质是一个队列,通过结构体 tasklet_head 存储,并且每个CPU有一个这样的队列,我们来看看结构体 tasklet_head 的定义:

struct tasklet_head 

    struct tasklet_struct *list; 
}; 
 
struct tasklet_struct 

    struct tasklet_struct *next
    unsigned long state; 
    atomic_t count
    void (*func)(unsigned long); 
    unsigned long data; 
}; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

从 tasklet_head 的定义可以知道,tasklet_head 结构是 tasklet_struct 结构队列的头部,而 tasklet_struct 结构的 func 字段正式任务要执行的函数指针。Linux定义了两种的tasklet队列,分别为 tasklet_vec 和 tasklet_hi_vec,定义如下:

struct tasklet_head tasklet_vec[NR_CPUS]; 
 
struct tasklet_head tasklet_hi_vec[NR_CPUS]; 
  • 1.
  • 2.
  • 3.

可以看出,tasklet_vec 和 tasklet_hi_vec 都是数组,数组的元素个数为CPU的核心数,也就是每个CPU核心都有一个高优先级tasklet队列和一个普通tasklet队列。

调度tasklet

如果我们有一个tasklet需要执行,那么高优先级tasklet可以通过 tasklet_hi_schedule() 函数调度,而普通tasklet可以通过 tasklet_schedule() 调度。这两个函数基本一样,所以我们只分析其中一个:

static inline void tasklet_hi_schedule(struct tasklet_struct *t) 

    if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) { 
        int cpu = smp_processor_id(); 
        unsigned long flags; 
 
        local_irq_save(flags); 
        t->next = tasklet_hi_vec[cpu].list; 
        tasklet_hi_vec[cpu].list = t; 
        __cpu_raise_softirq(cpu, HI_SOFTIRQ); 
        local_irq_restore(flags); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

函数参数的类型是 tasklet_struct 结构的指针,表示需要执行的tasklet结构。tasklet_hi_schedule() 函数首先判断这个tasklet是否已经被添加到队列中,如果不是就添加到 tasklet_hi_vec 队列中,并且通过调用 __cpu_raise_softirq(cpu, HI_SOFTIRQ) 来告诉softirq需要执行 HI_SOFTIRQ 类型的softirq,我们来看看 __cpu_raise_softirq() 函数的实现:

static inline void __cpu_raise_softirq(int cpu, int nr) 

    softirq_active(cpu) |= (1<<nr); 

  • 1.
  • 2.
  • 3.
  • 4.

可以看出,__cpu_raise_softirq() 函数就是把 irq_cpustat_t 结构的 __softirq_active 字段的 nr位 设置为1。对于 tasklet_hi_schedule() 函数就是把 HI_SOFTIRQ 位(0位)设置为1。

前面我们也介绍过,Linux在初始化时会注册两种softirq,TASKLET_SOFTIRQ 和 HI_SOFTIRQ:

void __init softirq_init() 

    ... 
    open_softirq(TASKLET_SOFTIRQ, tasklet_action, NULL); 
    open_softirq(HI_SOFTIRQ, tasklet_hi_action, NULL); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

所以当把 irq_cpustat_t 结构的 __softirq_active 字段的 HI_SOFTIRQ 位(0位)设置为1时,softirq机制就会执行 tasklet_hi_action() 函数,我们来看看 tasklet_hi_action() 函数的实现:

static void tasklet_hi_action(struct softirq_action *a) 

    int cpu = smp_processor_id(); 
    struct tasklet_struct *list; 
 
    local_irq_disable(); 
    list = tasklet_hi_vec[cpu].list; 
    tasklet_hi_vec[cpu].list = NULL
    local_irq_enable(); 
 
    while (list != NULL) { 
        struct tasklet_struct *t = list; 
 
        list = list->next
 
        if (tasklet_trylock(t)) { 
            if (atomic_read(&t->count) == 0) { 
                clear_bit(TASKLET_STATE_SCHED, &t->state); 
 
                t->func(t->data);  // 调用tasklet处理函数 
                tasklet_unlock(t); 
                continue
            } 
            tasklet_unlock(t); 
        } 
        ... 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

 

tasklet_hi_action() 函数非常简单,就是遍历 tasklet_hi_vec 队列并且执行其中tasklet的处理函数。

 

责任编辑:武晓燕 来源: Linux内核那些事
相关推荐

2023-12-22 19:59:15

2021-08-04 16:06:45

DataOps智领云

2021-08-11 10:10:26

Linux定时器数组

2018-09-28 14:06:25

前端缓存后端

2022-11-06 21:14:02

数据驱动架构数据

2022-09-22 09:00:46

CSS单位

2021-10-20 07:18:51

Linux延时队列

2023-11-27 17:35:48

ComponentWeb外层

2023-05-20 17:58:31

低代码软件

2022-07-05 06:30:54

云网络网络云原生

2022-07-26 00:00:03

语言模型人工智能

2022-12-01 17:23:45

2022-10-20 08:01:23

2021-12-29 18:00:19

无损网络网络通信网络

2021-04-24 09:02:36

Linux 内存分配

2024-12-27 14:45:59

2023-12-02 19:42:29

2020-12-30 09:05:24

架构微内核系统

2021-02-05 05:26:33

字节ASCII控制

2018-10-30 11:10:05

Flink数据集计算
点赞
收藏

51CTO技术栈公众号