Subject: [PATCH v3 04/16] lockdep: check that no locks held at freeze time

From: Colin Cross <[email-masked]>
Date: Mon, 6 May 2013 16:50:09 -0700

From: Mandeep Singh Baines <[email-masked]>

We shouldn't try_to_freeze if locks are held.  Holding a lock can cause a
deadlock if the lock is later acquired in the suspend or hibernate path
(e.g.  by dpm).  Holding a lock can also cause a deadlock in the case of
cgroup_freezer if a lock is held inside a frozen cgroup that is later
acquired by a process outside that group.

History:
This patch was originally applied as 6aa9707099c and reverted in
dbf520a9d7d4 because NFS was freezing with locks held.  It was
deemed better to keep the bad freeze point in NFS to allow laptops
to suspend consistently.  The previous patch in this series converts
NFS to call _unsafe versions of the freezable helpers so that
lockdep doesn't complain about them until a more correct fix
can be applied.

[[email-masked]: export debug_check_no_locks_held]
Signed-off-by: Mandeep Singh Baines <[email-masked]>
Cc: Ben Chan <[email-masked]>
Cc: Oleg Nesterov <[email-masked]>
Cc: Tejun Heo <[email-masked]>
Cc: Rafael J. Wysocki <[email-masked]>
Cc: Ingo Molnar <[email-masked]>
Signed-off-by: Andrew Morton <[email-masked]>
Signed-off-by: Linus Torvalds <[email-masked]>
[[email-masked]: don't warn if try_to_freeze_unsafe is called]
Signed-off-by: Colin Cross <[email-masked]>
---
 include/linux/freezer.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/linux/freezer.h b/include/linux/freezer.h
index d3c038e..bcf9e65 100644
--- a/include/linux/freezer.h
+++ b/include/linux/freezer.h
@@ -3,6 +3,7 @@
 #ifndef FREEZER_H_INCLUDED
 #define FREEZER_H_INCLUDED
 
+#include <linux/debug_locks.h>
 #include <linux/sched.h>
 #include <linux/wait.h>
 #include <linux/atomic.h>
@@ -60,6 +61,8 @@ static inline bool try_to_freeze_unsafe(void)
 
 static inline bool try_to_freeze(void)
 {
+	if (!(current->flags & PF_NOFREEZE))
+		debug_check_no_locks_held();
 	return try_to_freeze_unsafe();
 }
 
-- 
1.8.2.1