History

workqueue: Catch more locking problems with flush_work()

If a workqueue is flushed with flush_work() lockdep checking can be circumvented. For example: static DEFINE_MUTEX(mutex); static void my_work(struct work_struct *w) { mutex_lock(&mutex); mutex_unlock(&mutex); } static DECLARE_WORK(work, my_work); static int __init start_test_module(void) { schedule_work(&work); return 0; } module_init(start_test_module); static void __exit stop_test_module(void) { mutex_lock(&mutex); flush_work(&work); mutex_unlock(&mutex); } module_exit(stop_test_module); would not always print a warning when flush_work() was called. In this trivial example nothing could go wrong since we are guaranteed module_init() and module_exit() don't run concurrently, but if the work item is schedule asynchronously we could have a scenario where the work item is running just at the time flush_work() is called resulting in a classic ABBA locking problem. Add a lockdep hint by acquiring and releasing the work item lockdep_map in flush_work() so that we always catch this potential deadlock scenario.

Stephen Boyd committed 14 years ago

Commit 0976dfc1 parent f5b25521 file · +3 −0Browse files

Showing the changes to kernel/workqueue.c (1 of 1 file). Show all

Unified · Split

kernel/workqueue.c+3−0
@@ -2509,6 +2509,9 @@ bool flush_work(struct work_struct *work) · 2508 unmodified lines
25092509 {
25102510 struct wq_barrier barr;
25112511
2512+ lock_map_acquire(&work->lockdep_map);
2513+ lock_map_release(&work->lockdep_map);
2514+
25122515 if (start_flush_work(work, &barr, true)) {
25132516 wait_for_completion(&barr.done);
25142517 destroy_work_on_stack(&barr.work);