]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
freezer: make freezing indicate freeze condition in effect
authorTejun Heo <tj@kernel.org>
Sat, 20 Aug 2011 09:31:40 +0000 (11:31 +0200)
committerTejun Heo <tj@kernel.org>
Sat, 20 Aug 2011 09:31:40 +0000 (11:31 +0200)
Currently freezing (TIF_FREEZE) and frozen (PF_FROZEN) states are
interlocked - freezing is set to request freeze and when the task
actually freezes, it clears freezing and sets frozen.

This interlocking makes things more complex than necessary - freezing
doesn't mean there's freezing condition in effect and frozen doesn't
match the task actually entering and leaving frozen state (it's
cleared by the thawing task).

This patch makes freezing indicate that freeze condition is in effect.
A task enters and stays frozen if freezing.  This makes PF_FROZEN
manipulation done only by the task itself and prevents wakeup from
__thaw_task() leaking outside of refrigerator.

The only place which needs to tell freezing && !frozen is
try_to_freeze_task() to whine about tasks which don't enter frozen.
It's updated to test the condition explicitly.

Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/freezer.c
kernel/power/process.c

index c60082db2cdcbb57d417e20f923964916d7cc76f..d6165cdf43fd8d7460e86dd48186ddc9272c8580 100644 (file)
@@ -22,14 +22,18 @@ bool __refrigerator(bool check_kthr_stop)
        bool was_frozen = false;
        long save;
 
+       /*
+        * Enter FROZEN.  If NOFREEZE, schedule immediate thawing by
+        * clearing freezing.
+        */
        spin_lock_irq(&freezer_lock);
        if (!freezing(current)) {
                spin_unlock_irq(&freezer_lock);
                return was_frozen;
        }
-       if (!(current->flags & PF_NOFREEZE))
-               current->flags |= PF_FROZEN;
-       clear_freeze_flag(current);
+       if (current->flags & PF_NOFREEZE)
+               clear_freeze_flag(current);
+       current->flags |= PF_FROZEN;
        spin_unlock_irq(&freezer_lock);
 
        save = current->state;
@@ -44,7 +48,7 @@ bool __refrigerator(bool check_kthr_stop)
 
        for (;;) {
                set_current_state(TASK_UNINTERRUPTIBLE);
-               if (!frozen(current) ||
+               if (!freezing(current) ||
                    (check_kthr_stop && kthread_should_stop()))
                        break;
                was_frozen = true;
@@ -54,6 +58,11 @@ bool __refrigerator(bool check_kthr_stop)
        /* Remove the accounting blocker */
        current->flags &= ~PF_FREEZING;
 
+       /* leave FROZEN */
+       spin_lock_irq(&freezer_lock);
+       current->flags &= ~PF_FROZEN;
+       spin_unlock_irq(&freezer_lock);
+
        pr_debug("%s left refrigerator\n", current->comm);
 
        /*
@@ -137,24 +146,19 @@ void cancel_freezing(struct task_struct *p)
        spin_unlock_irqrestore(&freezer_lock, flags);
 }
 
-/*
- * Wake up a frozen task
- *
- * 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.
- */
 void __thaw_task(struct task_struct *p)
 {
        unsigned long flags;
 
+       /*
+        * 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.
+        */
        spin_lock_irqsave(&freezer_lock, flags);
-       if (frozen(p)) {
-               p->flags &= ~PF_FROZEN;
+       clear_freeze_flag(p);
+       if (frozen(p))
                wake_up_process(p);
-       } else
-               clear_freeze_flag(p);
        spin_unlock_irqrestore(&freezer_lock, flags);
 }
index 96d138e314841210688ee883594d16dded5e7b01..f075c2f1b36d4a8fe051811e8d050233d541a407 100644 (file)
@@ -118,7 +118,8 @@ static int try_to_freeze_tasks(bool sig_only)
 
                read_lock(&tasklist_lock);
                do_each_thread(g, p) {
-                       if (!wakeup && freezing(p) && !freezer_should_skip(p))
+                       if (!wakeup && !freezer_should_skip(p) &&
+                           freezing(p) && !frozen(p))
                                sched_show_task(p);
                        cancel_freezing(p);
                } while_each_thread(g, p);