内核hung检测机制(3)
实践
编写ko在内核线程上下文中触发D状态死锁
代码
hungtask.c
#include <linux/init.h>
#include <linux/sched.h> //current macro
#include <linux/module.h>
void hung_task_work_fn(struct work_struct *work);
DEFINE_MUTEX(ckw_hung_task_mutex);
DECLARE_WORK(ckw_work_item,hung_task_work_fn);
static int __init hung_task_init(void){
pr_err("init \r\n");
pr_err("now we schedule a workitem,will hung in kthread \r\n");
schedule_work(&ckw_work_item);
return 0;
}
void hung_task_work_fn(struct work_struct *work){
pr_err("comm(%s) pid(%d)\r\n",current->comm,current->pid);
mutex_lock(&ckw_hung_task_mutex);
pr_err("now we try to get mutex again, thus will triger hung task check\r\n");
mutex_lock(&ckw_hung_task_mutex);
pr_err("we should never get here\r\n");
}
static void __exit hung_task_exit(void){
pr_err("exit \r\n");
}
module_init(hung_task_init);
module_exit(hung_task_exit);
MODULE_LICENSE("GPL");
Makefile
ifneq ($(KERNELRELEASE),)
obj-m += hung_task.o
else
KBUILD_DIR:=/lib/modules/$(shell uname -r)/build
PWD:= $(shell pwd)
all:
make -C $(KBUILD_DIR) M=$(PWD) modules
clean:
make -C $(KBUILD_DIR) M=$(PWD) modules clean
endif
现象
如图,通过工作队列,在内核进程上下文中触发D状态死锁,内核最终检测到进程hung住。
不相关的一些疑问:ko中注册了workitem,如果workitem正在被worker内核线程执行,那么rmmod ko能成功吗?