我正在尝试以将 I/O 请求移交给单独的工作线程的方式编写块设备驱动程序。工作线程等待获取请求,然后执行实际的 I/O。
我正在使用完整的结构在工作线程和传输请求函数之间进行同步。工作线程在处理请求之前执行 wait_for_completion 并在传输请求得到请求时被唤醒。
但是,当我尝试执行 insmod 时,我的系统会挂起。这是回溯在每个 CPU 上的样子
[ 152.036031] BUG: soft lockup - CPU#0 stuck for 22s! [mount:1752]
[ 152.036046] CPU 0
[ 152.036046] Call Trace:
[ 152.036046] [<ffffffff8108d3e3>] smp_call_function+0x33/0x60
[ 152.036046] [<ffffffff8108d443>] on_each_cpu+0x33/0xa0
[ 152.036046] [<ffffffff81189e95>] __blkdev_put+0x185/0x1f0
[ 152.036046] [<ffffffff8115570a>] __fput+0xaa/0x200
[ 152.036046] [<ffffffff81151f3f>] filp_close+0x5f/0x90
[ 152.036046] [<ffffffff8115201d>] sys_close+0xad/0x120
[ 152.036046] [<ffffffff815a7312>] system_call_fastpath+0x16/0x1b
[ 158.233026] INFO: rcu_preempt_state detected stalls on CPUs/tasks: { 2} (detected by 0, t=60002 jiffies)
[ 158.233031] sending NMI to all CPUs:
[ 158.233031] NMI backtrace for cpu 0
[ 158.233031] CPU 0
[ 158.233031] Call Trace:
[ 158.233031] [<ffffffff8102018f>] arch_trigger_all_cpu_backtrace+0x4f/0x90
[ 158.233031] [<ffffffff810ca429>] print_other_cpu_stall+0x159/0x1c0
[ 158.233031] [<ffffffff810cb5e1>] __rcu_pending+0x31/0x180
[ 158.233031] [<ffffffff810cbb4a>] rcu_check_callbacks+0x11a/0x190
[ 158.233031] [<ffffffff8106505f>] update_process_times+0x3f/0x80
[ 158.233031] [<ffffffff8108772b>] tick_sched_timer+0x5b/0xc0
[ 158.233031] [<ffffffff8107a2ce>] __run_hrtimer+0x6e/0x240
[ 158.233031] [<ffffffff8107acf5>] hrtimer_interrupt+0xe5/0x200
[ 158.233031] [<ffffffff8101efb3>] smp_apic_timer_interrupt+0x63/0xa0
[ 158.233031] [<ffffffff815a7e5e>] apic_timer_interrupt+0x6e/0x80
[ 158.233031] [<ffffffff8108d30e>] smp_call_function_many+0x1fe/0x2a0
[ 158.233031] [<ffffffff8108d3e3>] smp_call_function+0x33/0x60
[ 158.233031] [<ffffffff8108d443>] on_each_cpu+0x33/0xa0
[ 158.233031] [<ffffffff81189e95>] __blkdev_put+0x185/0x1f0
[ 158.233031] [<ffffffff8115570a>] __fput+0xaa/0x200
[ 158.233031] [<ffffffff81151f3f>] filp_close+0x5f/0x90
[ 158.233031] [<ffffffff8115201d>] sys_close+0xad/0x120
[ 158.233031] [<ffffffff815a7312>] system_call_fastpath+0x16/0x1b
[ 158.233291] CPU 3 and CPU 1
[ 158.233347] Call Trace:
[ 158.233352] [<ffffffff8101eb68>] lapic_next_event+0x18/0x20
[ 158.233356] [<ffffffff81087288>] tick_dev_program_event+0x38/0x100
[ 158.233360] [<ffffffff8107ad2d>] hrtimer_interrupt+0x11d/0x200
[ 158.233363] [<ffffffff8101efb3>] smp_apic_timer_interrupt+0x63/0xa0
[ 158.233366] [<ffffffff815a7e5e>] apic_timer_interrupt+0x6e/0x80
[ 158.233371] [<ffffffff81029aa2>] native_safe_halt+0x2/0x10
[ 158.233374] [<ffffffff8100a67d>] default_idle+0x4d/0x2a0
[ 158.233378] [<ffffffff810011a6>] cpu_idle+0x86/0xd0
[ 137.645911] CPU 2
[ 137.645911] Call Trace:
[ 137.645911] [<ffffffff8159d766>] wait_for_common+0x26/0x150
[ 137.645911] [<ffffffffa01674e2>] tsdd_worker_thread+0x72/0x1b0 [tsdd]
[ 137.645911] [<ffffffff81075eee>] kthread+0x7e/0x90
[ 137.645911] [<ffffffff815a94f4>] kernel_thread_helper+0x4/0x10`
...最终系统挂起。
我隐约明白这里似乎发生了什么。(tsdd) 工作线程在 CPU 2 上运行,等待 wait_for_completion()。在 CPU0 上有一个关闭系统调用。它似乎在等待所有其他 CPU 的响应。它得到除 CPU2 之外的所有响应。它等待的时间太长(BUG:软锁定 - CPU#0 卡住了 22 秒!)并且有一个定时器中断。
现在这个中断被广播到所有 CPU。wait_for_completion() 函数使 CPU2 上的线程等待直到 completion_done。即使发生中断,它也不会唤醒线程。当在这种情况下出现定时器中断时,中断被发送到所有 CPU,包括 CPU2,我们的线程在 wait_for_completion() 处卡住。它将无法为中断提供服务,系统最终会挂起。
这个观察是正确的,还是有更多的事情发生?
下面是一个简短的伪代码:
static struct request *sch_req = NULL; //global
static struct complete *comp = NULL; // initialized in module_init
void transfer_req(req_queue) {
req = blk_fetch_request(req_queue);
served = 0;
while (!served) {
if (completion_done(comp))
continue;
sch_req = req;
sch_queue = req_queue;
complete(comp);
served = 1;
}
}
void worker_thread() {
while (!kthread_should_stop()) {
if (wait_for_completion(comp))
continue;
while (sch_req) {
perform_IO(sch_req);
sch_req = blk_fetch_req(sch_queue);
}
init_completion(comp);
}
}
有人可以帮忙弄清楚这里出了什么问题吗?我也想了解如何解决这个问题。我什至尝试使用 wait_for_completion_interruptible,但这似乎并不能解决问题。
谢谢
PS - 很抱歉发了这么长的帖子(无法附加日志文件)