Fork me on GitHub

Linux 内核学习笔记(四)——中断下页 bottom halves and deferring work

bottom halves and deferring work

重要的需要立即处理的工作在上半页(interrupt handler)进行处理,不是太紧急的部分就在下半页进行处理。The job of bottom halves is to perform any interrupt-related work not performed by the interrupt handler. 上下半页工作的界定通常有如下建议:

  • 时间敏感的放在interrupt handler;
  • 牵涉到hardware的,放在interrupt handler;
  • 确保另一个中断(特别是同一个中断)不会打断当前的中断它;放在interrupt handler;
  • 其他的都放在下半页 bottom halves;

为什么要使用下半页? 因为中断本身会把同一个interrupt lines其他中断都禁用,甚至可能会禁用所有的中断。这样其他的中断即使来了,也不能进入处理数据。为了保证整个系统的实时反应能力,把interrupt handler的执行内容少一点,其他的部分放在bottom half。通常bottom half是中断句柄结束之后才运行,核心理念就是not now。它可以在之后任何一个合适的时间点运行,尽管通常都是interrupt handler 结束之后紧跟着就运行,好处就是把interrupt lines上的其他中断释放了,不再禁用了。

最开始的BH,就是有一个静态的32位的bottom halves list,排着队挨次执行,不能并发进行。后来以链表的形式,在BH的基础上发展了task queues。后来发展出了可以并行的名字很难记的softirqs,在softirqs的基础上形成了动态创造的tasklet。目前常用的下页:softirqs(很少使用)、tasklet(常用),work queues。具体见下图:

softirqs

A softirq never preempts another softirq.The only event that can preempt a softirq is
an interrupt handler.Another softirq—even the same one—can run on another processor,
however.

Pending softirqs are checked for and executed in the following places:

  • In the return from hardware interrupt code path
  • In the ksoftirqd kernel thread
  • In any code that explicitly checks for and executes pending softirqs, such as the net-
    working subsystem

tasklet

tasklet是在softirqs的基础上实现的,但是应用起来更简单。比起softirqs,大多数时候都是使用tasklet。非常简单,只要调用相应的函数即可。

两个tasklet不能同时concurrently;和softirqs一样,tasklet不能sleep。在其他中断运行的时候(因为是下页,其他中断当然可以运行)如果当前的下页和中断有share data,需要考虑同步的问题。

值得注意的是,softirqs是可以自我启动的,为了解决陷入自我循环中,浪费大量的系统资源。一种方式是不去处理这个softirqs的reactivated,直到它的interrupt handler再次出现才执行。另一种方式是,使用一个叫ksoftirqs的kernel thread去处理这些自启动,但是设定其优先级较低,使其在资源不紧张时调用。

work queues

work queue 可以sleep。Work queues defer work into a kernel thread—this bottom half always runs in process context. work queue与前面的机制很不一样。如果这样下半页的工作需要sleep,就使用work queue,否则使用softirqs或者tasklet即可。 work queue使用起来是最简单的。

参考文献

  1. Robert Love. "Linux Kernel Development." (2008).

本文作者:LT_Rock

本文链接https://www.cnblogs.com/LT-Rock/p/13581499.html

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!


posted @ 2020-08-29 11:32  LT_Rock  阅读(438)  评论(0)    收藏  举报