Python iteration to look for possible sysfs deadlocks (resend)
It is possible to deadlock the kernel if the same lock is used on module exit and syfs ops. I've proposed a way to fix this [0], however it not clear how wide spread this issue is, and so it would be best to avoid the fix so to avoid the complexity the fix requires.
[0] https://lkml.kernel.org/r/[email protected]
A full hunt would involve using any lock, and iteration on module exit and also on the sysfs ops, as each could have indirect calls which do use a lock.
An attempt to just look for the deadlock candidates with 2 level of indirection follows:
@ finds_module_exit @
identifier exit_fn;
declarer name module_exit;
@@
module_exit(exit_fn);
@ exit_fn_locks @
identifier finds_module_exit.exit_fn;
identifier mutex, spinlock;
@@
exit_fn(...)
{
...
(
mutex_lock(&mutex);
|
spin_lock(&spinlock);
)
...
}@ exit_indirect_call_level_1 @
identifier finds_module_exit.exit_fn;
identifier indirect_fn;
@@
exit_fn(...)
{
...
indirect_fn(...)
...
}@ ind_call_level_1 @
identifier exit_indirect_call_level_1.indirect_fn; identifier ind_mutex, ind_spinlock;
@@
indirect_fn(...)
{
...
(
mutex_lock(&ind_mutex);
|
spin_lock(&ind_spinlock);
)
...
}@ indirect_call_level_2 @
identifier exit_indirect_call_level_1.indirect_fn; identifier indirect_fn_2;
@@
indirect_fn(...)
{
...
indirect_fn_2(...)
...
}@ ind_call_level_2 @
identifier indirect_call_level_2.indirect_fn_2;
identifier ind_mutex_2, ind_spinlock_2;
@@
indirect_fn_2(...)
{
...
(
mutex_lock(&ind_mutex_2);
|
spin_lock(&ind_spinlock_2);
)
...
}@ any_store @
identifier fn_store;
identifier exit_fn_locks.mutex, exit_fn_locks.spinlock; identifier dev, attr, buf, len;
identifier ind_call_level_1.ind_mutex, ind_call_level_1.ind_spinlock; identifier ind_call_level_2.ind_mutex_2, ind_call_level_2.ind_spinlock_2; @@
ssize_t fn_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
{
...(
- mutex_lock(&mutex);
+ mutex_lock_deadlock(&mutex);
|
- mutex_lock(&ind_mutex);
+ mutex_lock_deadlock(&ind_mutex);
|
- spin_lock(&spinlock);
+ spin_lock_deadlock(&spinlock);
|
- spin_lock(&ind_spinlock);
+ spin_lock_deadlock(&ind_spinlock);
|
- mutex_lock(&ind_mutex_2);
+ mutex_lock_deadlock(&ind_mutex_2);
|
- spin_lock(&ind_spinlock_2);
+ spin_lock_deadlock(&ind_spinlock_2);
)
... }
@ any_show @
identifier fn_show;
identifier exit_fn_locks.mutex, exit_fn_locks.spinlock; identifier dev, attr, buf;
identifier ind_call_level_1.ind_mutex, ind_call_level_1.ind_spinlock; identifier ind_call_level_2.ind_mutex_2, ind_call_level_2.ind_spinlock_2; @@
ssize_t fn_show(struct device *dev, struct device_attribute *attr, const char *buf)
{
...(
- mutex_lock(&mutex);
+ mutex_lock_deadlock(&mutex);
|
- mutex_lock(&ind_mutex);
+ mutex_lock_deadlock(&ind_mutex);
|
- spin_lock(&spinlock);
+ spin_lock_deadlock(&spinlock);
|
- spin_lock(&ind_spinlock);
+ spin_lock_deadlock(&ind_spinlock);
|
- mutex_lock(&ind_mutex_2);
+ mutex_lock_deadlock(&ind_mutex_2);
|
- spin_lock(&ind_spinlock_2);
+ spin_lock_deadlock(&ind_spinlock_2);
)
... }
And so obviously this can go on forever. It doesn't address indirect uses of a lock on the sysfs ops either. An attempt to use iteration follows, however this seems to fall short of what I want, any recommendations?
virtual after_start
@initialize:python@
@@
seen = set()
@ finds_module_exit @
identifier exit_fn;
declarer name module_exit;
@@
module_exit(exit_fn);
@ exit_fn_locks @
identifier finds_module_exit.exit_fn;
identifier mutex, spinlock;
@@
exit_fn(...)
{
...
(
mutex_lock(&mutex);
|
spin_lock(&spinlock);
)
...
}@ exit_indirect_fn depends on finds_module_exit && after_start exists @ identifier finds_module_exit.exit_fn;
identifier virtual.fn;
position p;
@@
exit_fn(...)
{
...
fn@p(...)
...
}@ ind_call depends on finds_module_exit @
identifier indirect_fn;
identifier ind_mutex, ind_spinlock;
position p;
@@
indirect_fn@p(...)
{
...
(
mutex_lock(&ind_mutex);
|
spin_lock(&ind_spinlock);
)
...
}@ any_store depends on finds_module_exit && (exit_fn_locks || ind_call) @ identifier fn_store;
identifier exit_fn_locks.mutex, exit_fn_locks.spinlock; identifier dev, attr, buf, len;
identifier ind_call.ind_mutex, ind_call.ind_spinlock;
@@
ssize_t fn_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
{
...(
- mutex_lock(&mutex);
+ mutex_lock_deadlock(&mutex);
|
- mutex_lock(&ind_mutex);
+ mutex_lock_deadlock(&ind_mutex);
|
- spin_lock(&spinlock);
+ spin_lock_deadlock(&spinlock);
|
- spin_lock(&ind_spinlock);
+ spin_lock_deadlock(&ind_spinlock);
)
... }
@ any_show depends on finds_module_exit && (exit_fn_locks || ind_call) @ identifier fn_show;
identifier exit_fn_locks.mutex, exit_fn_locks.spinlock; identifier dev, attr, buf;
identifier ind_call.ind_mutex, ind_call.ind_spinlock;
@@
ssize_t fn_show(struct device *dev, struct device_attribute *attr, const char *buf)
{
...(
- mutex_lock(&mutex);
+ mutex_lock_deadlock(&mutex);
|
- mutex_lock(&ind_mutex);
+ mutex_lock_deadlock(&ind_mutex);
|
- spin_lock(&spinlock);
+ spin_lock_deadlock(&spinlock);
|
- spin_lock(&ind_spinlock);
+ spin_lock_deadlock(&ind_spinlock);
)
... }
@script:python depends on exit_indirect_fn @indirect_fn << ind_call.indirect_fn;
p << ind_call.p;
@@
if fn not in seen:
seen.add(indirect_fn)
it = Iteration()
it.set_files([p[0].file])
it.add_virtual_rule(after_start)
it.add_virtual_identifier(fn, indirect_fn)
it.register()
@script:python@
p << exit_indirect_fn.p;
@@
cocci.print_main("indirect call shares lock with sysfs store or show", p)