如何使用linux内核中的lockdep特性进行死锁检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20892822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:45:02  来源:igfitidea点击:

How to use lockdep feature in linux kernel for deadlock detection

clinuxlinux-kernelkerneldeadlock

提问by brokenfoot

I have a linux kernel driver and a user app that interacts with it. The kernel driver has a deadlock in it. I came accross this feature in the linux kernel called "lockdep". I was able to configure it and recompile my kernel (and I do see the lockdep folders in /proc). But I don't know how to infer the output of this tool or how to go about debugging the driver using this tool for that matter. Any help will much appreciated. Thanks!

我有一个 linux 内核驱动程序和一个与之交互的用户应用程序。内核驱动程序中存在死锁。我在名为“ lockdep”的 linux 内核中遇到了这个功能。我能够配置它并重新编译我的内核(我确实在 /proc 中看到了 lockdep 文件夹)。但我不知道如何推断此工具的输出或如何使用此工具调试驱动程序。任何帮助将不胜感激。谢谢!

采纳答案by brokenfoot

To enable lockdep feature, edit .config file through menuconfig:

要启用 lockdep 功能,请通过 menuconfig 编辑 .config 文件:

make menuconfig

And enable following in Hacking Options:

并在 Hacking Options 中启用以下内容:

 1. [*] Detect Hard and Soft Lockups
 2. [*] Detect Hung Tasks
 3. [*] RT Mutex debugging, deadlock detection
 4. -*- Spinlock and rw-lock debugging: basic checks
 5. -*- Mutex debugging: basic checks
 6. -*- Lock debugging: detect incorrect freeing of live locks
 7. [*] Lock debugging: prove locking correctness
 8. [*] Lock usage statistics

Recompile the kernel:

重新编译内核:

make ARCH=i386 -j4 //whatever your arch is

Now, boot the new kernel image, under /proc you should see the following new folders:

现在,启动新的内核映像,在 /proc 下,您应该会看到以下新文件夹:

/proc/lockdep
/proc/lockdep_chains
/proc/lockdep_stat
/proc/locks
/proc/lock_stats

Now, insert the module you think that is causing the error, and access it with your user application (or whatever way you use to run your driver module). If the app deadlocks(hangs), do a:

现在,插入您认为导致错误的模块,并使用您的用户应用程序(或您用来运行驱动程序模块的任何方式)访问它。如果应用程序死锁(挂起),请执行以下操作:

ps -aux | grep <app_name>

you should see a +D (uninterruptible sleep) state for your app, do a:

您应该会看到应用的 +D(不间断睡眠)状态,请执行以下操作:

dmesg

The log it prints will include the function/file causing the deadlock.

它打印的日志将包括导致死锁的函数/文件。

That's it!

就是这样!

回答by Andreas Bombe

There is not much to it – the lockdep code will simply print a description of the situation and a stack backtrace to the kernel log when it encounters a locking sequence that potentially deadlocks. You just have to watch your kernel output (via dmesgor serial line or whatever you use).

它没有什么——当遇到可能死锁的锁定序列时,lockdep 代码将简单地将情况描述和堆栈回溯打印到内核日志。你只需要观察你的内核输出(通过dmesg或串行线或你使用的任何东西)。

The lockdep code debugs only locks, it can not warn you about deadlocks that arise from something else.

lockdep 代码只调试锁,它不能警告你由其他原因引起的死锁。