mutex_lockdep_test

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <linux/mutex.h>
#include <linux/delay.h>


//static DEFINE_SPINLOCK(spinlock_a);    //用spinlock就有效果了,会引起softlockup,导致黑屏。但是系统可能没死,adb能连接,power键有反应。

static DEFINE_MUTEX(mutex_a);              //用这个睡眠锁,只会增加两个D状态task(此处worker和thread),不影响系统。
static struct delayed_work delay_task;
static void lockdep_timefunc(unsigned long);
static DEFINE_TIMER(lockdep_timer,lockdep_timefunc,0,0);

static void lockdep_timefunc(unsigned long dummy)
{
    schedule_delayed_work(&delay_task,10);
    mod_timer(&lockdep_timer,jiffies + msecs_to_jiffies(100));
}

static void lockdep_test_worker(struct work_struct *work){

    mutex_lock(&mutex_a);
    mdelay(300);
    mutex_unlock(&mutex_a);
}

static int lockdep_thread(void *nothing){
    set_freezable();
    set_user_nice(current,0);
    while(!kthread_should_stop()){
        mdelay(500);
        mutex_lock(&mutex_a);
        cancel_delayed_work_sync(&delay_task);
        mutex_unlock(&mutex_a);
    }
    return 0;
}

static int __init lockdep_test_init(void){
    struct task_struct *lock_thread;
    printk("figo: my lockdep module init\n");
    lock_thread = kthread_run(lockdep_thread,NULL,"lockdep_test");

        INIT_DELAYED_WORK(&delay_task,lockdep_test_worker);
    
    lockdep_timer.expires = jiffies + msecs_to_jiffies(500);
    add_timer(&lockdep_timer);
    return 0;
}

static void __exit lockdep_test_exit(void){
    printk("goodbye\n");
}

MODULE_LICENSE("GPL");
module_init(lockdep_test_init);
module_exit(lockdep_test_exit);


可以造成死锁,但开启下面宏,mutex 死锁没detect到,spinlock就detect到了。

CONFIG_LOCK_STAT=y

CONFIG_PROVE_LOCKING=y

CONFIG_DEBUG_LOCKDEP=y


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值