#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 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);
#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