当前位置: 代码网 > 服务器>服务器>Linux > linux中的内核死锁调试

linux中的内核死锁调试

2024年05月18日 Linux 我要评论
linux内核死锁调试使用内核的kernel hacking功能打开以下配置config_prove_locking=yconfig_lock_stat=yconfig_debug_lockdep=y

linux内核死锁调试

使用内核的kernel hacking功能

打开以下配置

config_prove_locking=y
config_lock_stat=y
config_debug_lockdep=y

重新编译内核后,

proc目录下会有lockdep lockdep_stats lockdep_chains说明lockdep模块已经生效

测试code

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>


static define_spinlock(hack_spina);
static define_spinlock(hack_spinb);
#ifdef memory_test
static char* buf;
static void create_slue_err(void){
    buf = kmalloc(32,gfp_kernel);
    if(buf)  {
        /*memset(buf,0x00,33);*/
    /*  kfree(buf);
        printk("%s\n","free buf" );*/
        /*kfree(buf);*/
        memset(buf,0x00,33);
    }

    return;
}
#endif
void hack_spinba(void) {
    printk("%s\n","hack_spin:b=>a\n" );
    spin_lock(&hack_spina);
    spin_lock(&hack_spinb);
}

void hack_spinab(void) {
    printk("%s\n","hack_spin:a=>b\n" );
    spin_lock(&hack_spinb);
}

static int __init my_test_init(void) {
    printk("init %s\n", "my_test_init 1");
#ifdef memory_test 
    create_slue_err();
#endif
    hack_spinba();
    hack_spinab();
    printk("init %s\n", "my_test_init 2");
    return 0;
}

static void __exit my_test_exit(void) {
    printk("%s\n","my_test_exit" );
    return ;
}
module_license("gpl");
module_init(my_test_init);
module_exit(my_test_exit);

测试打印:

[  188.596504@1] init my_test_init 1
[  188.596548@1] hack_spin:b=>a
[  188.596548@1] 
[  188.598679@1] hack_spin:a=>b
[  188.598679@1] 
[  189.920389@1] bug: spinlock lockup suspected on cpu#1, insmod/6999
[  189.920943@1]  lock: hack_spinb+0x0/0xfffffffffffffef4 [slub_test], .magic: dead4ead, .owner: insmod/6999, .owner_cpu: 1
[  189.931758@1] cpu: 1 pid: 6999 comm: insmod tainted: g           o 3.14.29 #6
[  189.938784@1] call trace:
[  189.941406@1] [<ffffffc0010893f8>] dump_backtrace+0x0/0x144
[  189.946896@1] [<ffffffc001089558>] show_stack+0x1c/0x28
[  189.952120@1] [<ffffffc001b5b480>] dump_stack+0x74/0xb8
[  189.957247@1] [<ffffffc0010fb6d4>] spin_dump+0x78/0x98
[  189.962369@1] [<ffffffc0010fb904>] do_raw_spin_lock+0x170/0x1a8
[  189.968199@1] [<ffffffc001b670a8>] _raw_spin_lock+0x68/0x88
[  189.973764@1] [<ffffffbffc00507c>] hack_spinab+0x30/0x3c [slub_test]
[  189.980015@1] [<ffffffbffc00d028>] $x+0x28/0x4c [slub_test]
[  189.985566@1] [<ffffffc0010816d4>] do_one_initcall+0xd4/0x13c
[  189.991257@1] [<ffffffc00112c2f8>] load_module+0x16d8/0x1e08
[  189.996834@1] [<ffffffc00112cba0>] sys_finit_module+0x80/0x90

[  211.641019@1] info: rcu_sched detected stalls on cpus/tasks: { 1} (detected by 2, t=2104 jiffies, g=18446744073709551337, c=18446744073709551336, q=4)
[  211.648804@2] task dump for cpu 1:
[  211.652167@2] insmod          r  running task        0  6999   5821 0x0000000a
[  211.659325@2] call trace:
[  211.661920@2] [<ffffffc001085f10>] __switch_to+0x74/0x8c

很明显可以看出死锁的发生路径与调用栈

hack_spinab在申请spin_lock(&hack_spinb)时死锁了

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com