freezer: clean up freeze_processes() failure path
[firefly-linux-kernel-4.4.55.git] / kernel / freezer.c
index 7be56c53439723bb1f95ff43b6cb620ef1524193..11e32d419dec625a760eeba149546265eb51013b 100644 (file)
@@ -9,34 +9,34 @@
 #include <linux/export.h>
 #include <linux/syscalls.h>
 #include <linux/freezer.h>
+#include <linux/kthread.h>
 
-/*
- * freezing is complete, mark current process as frozen
- */
-static inline void frozen_process(void)
-{
-       if (!unlikely(current->flags & PF_NOFREEZE)) {
-               current->flags |= PF_FROZEN;
-               smp_wmb();
-       }
-       clear_freeze_flag(current);
-}
+/* protects freezing and frozen transitions */
+static DEFINE_SPINLOCK(freezer_lock);
 
 /* Refrigerator is place where frozen processes are stored :-). */
-void refrigerator(void)
+bool __refrigerator(bool check_kthr_stop)
 {
        /* Hmm, should we be allowed to suspend when there are realtime
           processes around? */
+       bool was_frozen = false;
        long save;
 
-       task_lock(current);
-       if (freezing(current)) {
-               frozen_process();
-               task_unlock(current);
-       } else {
-               task_unlock(current);
-               return;
+       /*
+        * Enter FROZEN.  If NOFREEZE, schedule immediate thawing by
+        * clearing freezing.
+        */
+       spin_lock_irq(&freezer_lock);
+repeat:
+       if (!freezing(current)) {
+               spin_unlock_irq(&freezer_lock);
+               return was_frozen;
        }
+       if (current->flags & PF_NOFREEZE)
+               clear_freeze_flag(current);
+       current->flags |= PF_FROZEN;
+       spin_unlock_irq(&freezer_lock);
+
        save = current->state;
        pr_debug("%s entered refrigerator\n", current->comm);
 
@@ -44,23 +44,34 @@ void refrigerator(void)
        recalc_sigpending(); /* We sent fake signal, clean it up */
        spin_unlock_irq(&current->sighand->siglock);
 
-       /* prevent accounting of that task to load */
-       current->flags |= PF_FREEZING;
-
        for (;;) {
                set_current_state(TASK_UNINTERRUPTIBLE);
-               if (!frozen(current))
+               if (!freezing(current) ||
+                   (check_kthr_stop && kthread_should_stop()))
                        break;
+               was_frozen = true;
                schedule();
        }
 
-       /* Remove the accounting blocker */
-       current->flags &= ~PF_FREEZING;
+       /* leave FROZEN */
+       spin_lock_irq(&freezer_lock);
+       if (freezing(current))
+               goto repeat;
+       current->flags &= ~PF_FROZEN;
+       spin_unlock_irq(&freezer_lock);
 
        pr_debug("%s left refrigerator\n", current->comm);
-       __set_current_state(save);
+
+       /*
+        * Restore saved task state before returning.  The mb'd version
+        * needs to be used; otherwise, it might silently break
+        * synchronization which depends on ordered task state change.
+        */
+       set_current_state(save);
+
+       return was_frozen;
 }
-EXPORT_SYMBOL(refrigerator);
+EXPORT_SYMBOL(__refrigerator);
 
 static void fake_signal_wake_up(struct task_struct *p)
 {
@@ -87,21 +98,19 @@ static void fake_signal_wake_up(struct task_struct *p)
  */
 bool freeze_task(struct task_struct *p, bool sig_only)
 {
-       /*
-        * We first check if the task is freezing and next if it has already
-        * been frozen to avoid the race with frozen_process() which first marks
-        * the task as frozen and next clears its TIF_FREEZE.
-        */
-       if (!freezing(p)) {
-               smp_rmb();
-               if (frozen(p))
-                       return false;
-
-               if (!sig_only || should_send_signal(p))
-                       set_freeze_flag(p);
-               else
-                       return false;
-       }
+       unsigned long flags;
+       bool ret = false;
+
+       spin_lock_irqsave(&freezer_lock, flags);
+
+       if ((p->flags & PF_NOFREEZE) ||
+           (sig_only && !should_send_signal(p)))
+               goto out_unlock;
+
+       if (frozen(p))
+               goto out_unlock;
+
+       set_freeze_flag(p);
 
        if (should_send_signal(p)) {
                fake_signal_wake_up(p);
@@ -111,56 +120,36 @@ bool freeze_task(struct task_struct *p, bool sig_only)
                 * TASK_RUNNING transition can't race with task state
                 * testing in try_to_freeze_tasks().
                 */
-       } else if (sig_only) {
-               return false;
        } else {
                wake_up_state(p, TASK_INTERRUPTIBLE);
        }
-
-       return true;
+       ret = true;
+out_unlock:
+       spin_unlock_irqrestore(&freezer_lock, flags);
+       return ret;
 }
 
-void cancel_freezing(struct task_struct *p)
+void __thaw_task(struct task_struct *p)
 {
        unsigned long flags;
 
-       if (freezing(p)) {
-               pr_debug("  clean up: %s\n", p->comm);
-               clear_freeze_flag(p);
-               spin_lock_irqsave(&p->sighand->siglock, flags);
-               recalc_sigpending_and_wake(p);
-               spin_unlock_irqrestore(&p->sighand->siglock, flags);
-       }
-}
-
-static int __thaw_process(struct task_struct *p)
-{
-       if (frozen(p)) {
-               p->flags &= ~PF_FROZEN;
-               return 1;
-       }
+       /*
+        * Clear freezing and kick @p if FROZEN.  Clearing is guaranteed to
+        * be visible to @p as waking up implies wmb.  Waking up inside
+        * freezer_lock also prevents wakeups from leaking outside
+        * refrigerator.
+        *
+        * If !FROZEN, @p hasn't reached refrigerator, recalc sigpending to
+        * avoid leaving dangling TIF_SIGPENDING behind.
+        */
+       spin_lock_irqsave(&freezer_lock, flags);
        clear_freeze_flag(p);
-       return 0;
-}
-
-/*
- * Wake up a frozen process
- *
- * task_lock() is needed to prevent the race with refrigerator() which may
- * occur if the freezing of tasks fails.  Namely, without the lock, if the
- * freezing of tasks failed, thaw_tasks() might have run before a task in
- * refrigerator() could call frozen_process(), in which case the task would be
- * frozen and no one would thaw it.
- */
-int thaw_process(struct task_struct *p)
-{
-       task_lock(p);
-       if (__thaw_process(p) == 1) {
-               task_unlock(p);
+       if (frozen(p)) {
                wake_up_process(p);
-               return 1;
+       } else {
+               spin_lock(&p->sighand->siglock);
+               recalc_sigpending_and_wake(p);
+               spin_unlock(&p->sighand->siglock);
        }
-       task_unlock(p);
-       return 0;
+       spin_unlock_irqrestore(&freezer_lock, flags);
 }
-EXPORT_SYMBOL(thaw_process);