Like debugging the lock in userspace, it mainly involves:
1. Locate the lock deadlock and print out the call stack.
2. Find out who is using this mutex.
For example, in kernel config, enable is associated with lock/mutex, such as config_debug_mutex and config_detect_hung_tasks. In this way, when a deadlock occurs, wait for a period of time (120 seconds by default). The kernel detects the deadlock and prints the call stack. In kernel hacking, there are a large number of config files that are conducive to debugging. You can read them one by one. There is always one suitable for you.
For example, if config_debug_mutex is enabled, add the following code:
Show_stack (<lock>. Owner, null );
<Lock> is your mutex variable name ,. the owner is a struct task_struct *. By using the show_stack function, you can print out who occupies the mutex and the call stack when it occupies the mutex. Very cool.
In addition, you can take a look at the definition of struct mutex. There are some practical things, such as the mutex name. Struct task_struct also has some practical things, such as PID.
To sum up, the following can be printed in the kernel:
Dump_stack: prints the current backtrace
Show_stack: prints the backtrace of the specified task_struct.