我想使用基于互斥锁的死锁手动创建内核崩溃。 我在一个模块中创建了2个内核线程,并进行了递归互斥锁。因此,线程之一再次获取锁定并陷入死锁情况。我了解使用互斥锁,系统进入了睡眠状态,因此设备不会崩溃。是否有任何机制可以在由于互斥锁导致死锁时引发恐慌?
int foo1(void *data)
{
while(!kthread_should_stop()) {
mutex_lock(&my_mutex);
global_variable++;
mutex_unlock(&my_mutex);
msleep(1000);
}
return 0;
}
int foo2(void *data)
{
while(!kthread_should_stop()) {
mutex_lock(&my_mutex);
global_variable++;
mutex_lock(&my_mutex); // Recursive lock
mutex_unlock(&my_mutex);
msleep(1000);
}
return 0;
}
static int __init driver_init(void)
{
if((alloc_chrdev_region(&dev, 0, 1, "my_Dev")) <0){
printk(KERN_INFO "Cannot allocate major number\n");
return -1;
}
cdev_init(&my_cdev,&fops);
if((cdev_add(&my_cdev,dev,1)) < 0){
printk(KERN_INFO "Cannot add the device to the system\n");
}
if((device_create(dev_class,NULL,dev,NULL,"my_device")) == NULL){
printk(KERN_INFO "Cannot create the Device\n");
class_destroy(dev_class);
}
mutex_init(&my_mutex);
my_thread1 = kthread_run(foo1,NULL,"my Thread1");
if(my_thread1) {
printk(KERN_ERR "Kthread1 Created Successfully...\n");
} else {
printk(KERN_ERR "Cannot create kthread1\n");
}
my_thread2 = kthread_run(foo2,NULL,"my Thread2");
if(my_thread2) {
printk(KERN_ERR "Kthread2 Created Successfully...\n");
} else {
printk(KERN_ERR "Cannot create kthread2\n");
}
return 0;
}
使用上面的代码,我可以创建死锁。但是我正在寻找内核恐慌,以便可以对此错误进行调试。