簡單的AB-BA死鎖案例
下面舉一個簡單的AB-BA死鎖的例子:
#include < linux/module.h >
#include < linux/init.h >
#include < linux/kernel.h >
static DEFINE_SPINLOCK(hack_spinA);
static DEFINE_SPINLOCK(hack_spinB);
void hack_spinAB(void)
{
printk("hack_lockdep:A- >Bn");
spin_lock(&hack_spinA);
spin_lock(&hack_spinB);
}
void hack_spinBA(void)
{
printk("hack_lockdep:B- >An");
spin_lock(&hack_spinB);
}
static int __init lockdep_test_init(void)
{
printk("figo:my lockdep module initn");
hack_spinAB();
hack_spinBA();
return 0;
}
static void __exit lockdep_test_exit(void)
{
printk("goodbyen");
}
module_init(lockdep_test_init);
module_exit(lockdep_test_exit);
MODULE_LICENSE("GPL");
上述代碼初始化了兩個自旋鎖,其中hack_spinAB()函數(shù)分別申請了hack_spinA
鎖和hack_spinB
鎖,hack_spinBA()函數(shù)要申請hack_spinB
鎖。因?yàn)閯偛沛ihack_spinB
已經(jīng)被成功獲取且還沒有釋放,所以它會一直等待,而且它也被鎖在hack_spinA
的臨界區(qū)里。
現(xiàn)象:
[root@imx6ull:~]# insmod lockdep_test.ko
[ 437.981262] figo:my lockdep module init
[ 437.985145] hack_lockdep:A- >B
[ 437.989054] hack_lockdep:B- >A
[ 437.992304]
[ 437.993819] =============================================
[ 437.999229] [ INFO: possible recursive locking detected ]
[ 438.004641] 4.9.88 #2 Tainted: G O
[ 438.009180] ---------------------------------------------
[ 438.014589] insmod/367 is trying to acquire lock:
[ 438.019303] (hack_spinB){+.+...}, at: [< 7f00a030 >] lockdep_test_init+0x30/0x3c [lockdep_test]
[ 438.028006] but task is already holding lock:
[ 438.032547] (hack_spinB){+.+...}, at: [< 7f008038 >] hack_spinAB+0x38/0x3c [lockdep_test]
[ 438.040715] other info that might help us debug this:
[ 438.045950] Possible unsafe locking scenario:
[ 438.045950]
[ 438.051883] CPU0
[ 438.054337] ----
[ 438.056790] lock(hack_spinB);
[ 438.059975] lock(hack_spinB);
[ 438.063160]
[ 438.063160] *** DEADLOCK ***
[ 438.063160]
[ 438.069094] May be due to missing lock nesting notation
[ 438.069094]
[ 438.075896] 2 locks held by insmod/367:
[ 438.079740] #0: (hack_spinA){+.+...}, at: [< 7f008030 >] hack_spinAB+0x30/0x3c [lockdep_test]
[ 438.088358] #1: (hack_spinB){+.+...}, at: [< 7f008038 >] hack_spinAB+0x38/0x3c [lockdep_test]
[ 438.096977]
[ 438.096977] stack backtrace:
[ 438.101352] CPU: 0 PID: 367 Comm: insmod Tainted: G O 4.9.88 #2
[ 438.108410] Hardware name: Freescale i.MX6 UltraLite (Device Tree)
[ 438.114628] [< 801136cc >] (unwind_backtrace) from [< 8010e78c >] (show_stack+0x20/0x24)
[ 438.122396] [< 8010e78c >] (show_stack) from [< 804ccc34 >] (dump_stack+0xa0/0xcc)
[ 438.129646] [< 804ccc34 >] (dump_stack) from [< 8018f020 >] (__lock_acquire+0x8bc/0x1d4c)
[ 438.137502] [< 8018f020 >] (__lock_acquire) from [< 80190b78 >] (lock_acquire+0xf4/0x2f8)
[ 438.145358] [< 80190b78 >] (lock_acquire) from [< 80c94a0c >] (_raw_spin_lock+0x4c/0x84)
[ 438.153129] [< 80c94a0c >] (_raw_spin_lock) from [< 7f00a030 >] (lockdep_test_init+0x30/0x3c [lockdep_test])
[ 438.162638] [< 7f00a030 >] (lockdep_test_init [lockdep_test]) from [< 80102004 >] (do_one_initcall+0x54/0x184)
[ 438.172315] [< 80102004 >] (do_one_initcall) from [< 80229624 >] (do_init_module+0x74/0x1f8)
[ 438.180431] [< 80229624 >] (do_init_module) from [< 801dac54 >] (load_module+0x201c/0x279c)
[ 438.188461] [< 801dac54 >] (load_module) from [< 801db648 >] (SyS_finit_module+0xc4/0xfc)
[ 438.196317] [< 801db648 >] (SyS_finit_module) from [< 80109680 >] (ret_fast_syscall+0x0/0x1c)
提示信息顯示:嘗試獲取hack_spinB鎖,但是該鎖已經(jīng)在函數(shù)hack_spinAB中被鎖定 :
lockdep已經(jīng)很清晰地顯示了死鎖發(fā)生的路徑和發(fā)生時函數(shù)調(diào)用的棧信息,根據(jù)這些信息可以很快速地定位問題和解決問題。
-
內(nèi)核
+關(guān)注
關(guān)注
3文章
1372瀏覽量
40282 -
Linux
+關(guān)注
關(guān)注
87文章
11295瀏覽量
209348 -
死鎖
+關(guān)注
關(guān)注
0文章
25瀏覽量
8077
發(fā)布評論請先 登錄
相關(guān)推薦
評論