在 Linux 中暂停 kthread

Pausing a kthread in Linux

此处https://blog.packagecloud.io/eng/2017/02/06/monitoring-tuning-linux-networking-stack-sending-data/#queuing-disciplines 它是这样写的:

As you’ll see from the previous post, the NET_TX_SOFTIRQ softirq has the function net_tx_action registered to it. This means that there is a kernel thread executing net_tx_action. That thread is occasionally paused and raise_softirq_irqoff resumes it. Let’s take a look at what net_tx_action does so we can understand how the kernel processes transmit requests.

据说kthread偶尔暂停。当 kthread 暂停时,为什么?

kthread 如何知道要执行的工作?它轮询队列吗?

我认为关于暂停线程的说法更像是一种修辞手法。在这种情况下,暂停的不是 kthread,线程工作正常。

与 softirq 相关的工作主体在 __do_softirq() 函数中。

有多种软中断类型,每种软中断类型都由位掩码中的一位表示。每当有特定类型的 softirq 工作时,相应的位就会在位掩码中产生。 __do_softirq() 从最低有效位开始一点一点地处理这个位掩码,并为每个设置了位的软中断类型工作。因此 softirq 类型按优​​先级顺序处理,第 0 位代表最高优先级。事实上,如果您查看代码,您会发现位掩码已保存(复制),然后在处理开始之前被清除,并且处理的是副本。

每次向内核网络堆栈提交新的 skb 以发送数据时,NET_TX_SOFTIRQ 的位都会升高。这导致 __do_softirq() 调用 net_tx_action() 以获取传出数据。如果没有数据发送出去,则该位不会升高。本质上,这就是导致内核 softirq 线程 "pause" 的原因,这只是一种外行的说法,它没有工作,所以 net_tx_action() 没有被调用。一旦有更多数据,当数据被提交到内核网络堆栈时,该位会再次升高。 __do_softirq() 看到并再次调用 net_tx_action()

每个 CPU 上都有一个软中断线程。当至少有一个挂起的软中断类型时,线程为 运行。线程在 softirq_threads 结构中定义并在 spawn_softirqd() 函数中启动。