]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
freezer: fix set_freezable[_with_signal]() race
authorTejun Heo <tj@kernel.org>
Mon, 21 Nov 2011 20:32:25 +0000 (12:32 -0800)
committerTejun Heo <tj@kernel.org>
Mon, 21 Nov 2011 20:32:25 +0000 (12:32 -0800)
A kthread doing set_freezable*() may race with on-going PM freeze and
the freezer might think all tasks are frozen while the new freezable
kthread is merrily proceeding to execute code paths which aren't
supposed to be executing during PM freeze.

Reimplement set_freezable[_with_signal]() using __set_freezable() such
that freezable PF flags are modified under freezer_lock and
try_to_freeze() is called afterwards.  This eliminates race condition
against freezing.

Note: Separated out from larger patch to resolve fix order dependency
      Oleg pointed out.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
include/linux/freezer.h
kernel/freezer.c

index 3d50913d39d07605180b1de3dbedda8252dd5c07..a0f1b3a3604f6e061bfc51803fa52c60fcae4f46 100644 (file)
@@ -49,6 +49,7 @@ static inline bool try_to_freeze(void)
 }
 
 extern bool freeze_task(struct task_struct *p, bool sig_only);
+extern bool __set_freezable(bool with_signal);
 
 #ifdef CONFIG_CGROUP_FREEZER
 extern bool cgroup_freezing(struct task_struct *task);
@@ -106,18 +107,18 @@ static inline int freezer_should_skip(struct task_struct *p)
 /*
  * Tell the freezer that the current task should be frozen by it
  */
-static inline void set_freezable(void)
+static inline bool set_freezable(void)
 {
-       current->flags &= ~PF_NOFREEZE;
+       return __set_freezable(false);
 }
 
 /*
  * Tell the freezer that the current task should be frozen by it and that it
  * should send a fake signal to the task to freeze it.
  */
-static inline void set_freezable_with_signal(void)
+static inline bool set_freezable_with_signal(void)
 {
-       current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
+       return __set_freezable(true);
 }
 
 /*
index 95a123844241781337d816c355f83d905934d9df..b1e7a7b3d2cd60d02587b7e31f2c233edf791978 100644 (file)
@@ -171,3 +171,28 @@ void __thaw_task(struct task_struct *p)
        }
        spin_unlock_irqrestore(&freezer_lock, flags);
 }
+
+/**
+ * __set_freezable - make %current freezable
+ * @with_signal: do we want %TIF_SIGPENDING for notification too?
+ *
+ * Mark %current freezable and enter refrigerator if necessary.
+ */
+bool __set_freezable(bool with_signal)
+{
+       might_sleep();
+
+       /*
+        * Modify flags while holding freezer_lock.  This ensures the
+        * freezer notices that we aren't frozen yet or the freezing
+        * condition is visible to try_to_freeze() below.
+        */
+       spin_lock_irq(&freezer_lock);
+       current->flags &= ~PF_NOFREEZE;
+       if (with_signal)
+               current->flags &= ~PF_FREEZER_NOSIG;
+       spin_unlock_irq(&freezer_lock);
+
+       return try_to_freeze();
+}
+EXPORT_SYMBOL(__set_freezable);