]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - kernel/power/process.c
Freezer: return int from freeze_processes
[linux-2.6-omap-h63xx.git] / kernel / power / process.c
index 99eeb119b06db1d350d04de044fdb624f4627a19..00cdbe5f518ff8bf755f33bc83f5755adc9d19d1 100644 (file)
@@ -8,7 +8,6 @@
 
 #undef DEBUG
 
-#include <linux/smp_lock.h>
 #include <linux/interrupt.h>
 #include <linux/suspend.h>
 #include <linux/module.h>
 
 static inline int freezeable(struct task_struct * p)
 {
-       if ((p == current) || 
+       if ((p == current) ||
            (p->flags & PF_NOFREEZE) ||
-           (p->exit_state == EXIT_ZOMBIE) ||
-           (p->exit_state == EXIT_DEAD) ||
-           (p->state == TASK_STOPPED))
+           (p->exit_state != 0))
                return 0;
        return 1;
 }
 
+/*
+ * freezing is complete, mark current process as frozen
+ */
+static inline void frozen_process(void)
+{
+       if (!unlikely(current->flags & PF_NOFREEZE)) {
+               current->flags |= PF_FROZEN;
+               wmb();
+       }
+       clear_freeze_flag(current);
+}
+
 /* Refrigerator is place where frozen processes are stored :-). */
 void refrigerator(void)
 {
        /* Hmm, should we be allowed to suspend when there are realtime
           processes around? */
        long save;
+
+       task_lock(current);
+       if (freezing(current)) {
+               frozen_process();
+               task_unlock(current);
+       } else {
+               task_unlock(current);
+               return;
+       }
        save = current->state;
        pr_debug("%s entered refrigerator\n", current->comm);
 
-       frozen_process(current);
        spin_lock_irq(&current->sighand->siglock);
        recalc_sigpending(); /* We sent fake signal, clean it up */
        spin_unlock_irq(&current->sighand->siglock);
 
-       while (frozen(current)) {
-               current->state = TASK_UNINTERRUPTIBLE;
+       for (;;) {
+               set_current_state(TASK_UNINTERRUPTIBLE);
+               if (!frozen(current))
+                       break;
                schedule();
        }
        pr_debug("%s left refrigerator\n", current->comm);
-       current->state = save;
+       __set_current_state(save);
 }
 
-static inline void freeze_process(struct task_struct *p)
+static void freeze_task(struct task_struct *p)
 {
        unsigned long flags;
 
        if (!freezing(p)) {
-               freeze(p);
-               spin_lock_irqsave(&p->sighand->siglock, flags);
-               signal_wake_up(p, 0);
-               spin_unlock_irqrestore(&p->sighand->siglock, flags);
+               rmb();
+               if (!frozen(p)) {
+                       set_freeze_flag(p);
+                       if (p->state == TASK_STOPPED)
+                               force_sig_specific(SIGSTOP, p);
+                       spin_lock_irqsave(&p->sighand->siglock, flags);
+                       signal_wake_up(p, p->state == TASK_STOPPED);
+                       spin_unlock_irqrestore(&p->sighand->siglock, flags);
+               }
        }
 }
 
@@ -74,19 +98,14 @@ static void cancel_freezing(struct task_struct *p)
 
        if (freezing(p)) {
                pr_debug("  clean up: %s\n", p->comm);
-               do_not_freeze(p);
+               clear_freeze_flag(p);
                spin_lock_irqsave(&p->sighand->siglock, flags);
-               recalc_sigpending_tsk(p);
+               recalc_sigpending_and_wake(p);
                spin_unlock_irqrestore(&p->sighand->siglock, flags);
        }
 }
 
-static inline int is_user_space(struct task_struct *p)
-{
-       return p->mm && !(p->flags & PF_BORROWED_MM);
-}
-
-static unsigned int try_to_freeze_tasks(int freeze_user_space)
+static int try_to_freeze_tasks(int freeze_user_space)
 {
        struct task_struct *g, *p;
        unsigned long end_time;
@@ -97,34 +116,36 @@ static unsigned int try_to_freeze_tasks(int freeze_user_space)
                todo = 0;
                read_lock(&tasklist_lock);
                do_each_thread(g, p) {
-                       if (!freezeable(p))
-                               continue;
-
-                       if (frozen(p))
+                       if (frozen(p) || !freezeable(p))
                                continue;
 
-                       if (p->state == TASK_TRACED &&
-                           (frozen(p->parent) ||
-                            p->parent->state == TASK_STOPPED)) {
-                               cancel_freezing(p);
-                               continue;
-                       }
-                       if (is_user_space(p)) {
-                               if (!freeze_user_space)
+                       if (freeze_user_space) {
+                               if (p->state == TASK_TRACED &&
+                                   frozen(p->parent)) {
+                                       cancel_freezing(p);
                                        continue;
-
-                               /* Freeze the task unless there is a vfork
-                                * completion pending
+                               }
+                               /*
+                                * Kernel threads should not have TIF_FREEZE set
+                                * at this point, so we must ensure that either
+                                * p->mm is not NULL *and* PF_BORROWED_MM is
+                                * unset, or TIF_FRREZE is left unset.
+                                * The task_lock() is necessary to prevent races
+                                * with exit_mm() or use_mm()/unuse_mm() from
+                                * occuring.
                                 */
-                               if (!p->vfork_done)
-                                       freeze_process(p);
-                       } else {
-                               if (freeze_user_space)
+                               task_lock(p);
+                               if (!p->mm || (p->flags & PF_BORROWED_MM)) {
+                                       task_unlock(p);
                                        continue;
-
-                               freeze_process(p);
+                               }
+                               freeze_task(p);
+                               task_unlock(p);
+                       } else {
+                               freeze_task(p);
                        }
-                       todo++;
+                       if (!freezer_should_skip(p))
+                               todo++;
                } while_each_thread(g, p);
                read_unlock(&tasklist_lock);
                yield();                        /* Yield is okay here */
@@ -139,46 +160,41 @@ static unsigned int try_to_freeze_tasks(int freeze_user_space)
                 * but it cleans up leftover PF_FREEZE requests.
                 */
                printk("\n");
-               printk(KERN_ERR "Stopping %s timed out after %d seconds "
+               printk(KERN_ERR "Freezing of %s timed out after %d seconds "
                                "(%d tasks refusing to freeze):\n",
-                               freeze_user_space ? "user space processes" :
-                                       "kernel threads",
+                               freeze_user_space ? "user space " : "tasks ",
                                TIMEOUT / HZ, todo);
+               show_state();
                read_lock(&tasklist_lock);
                do_each_thread(g, p) {
-                       if (is_user_space(p) == !freeze_user_space)
-                               continue;
-
-                       if (freezeable(p) && !frozen(p))
+                       task_lock(p);
+                       if (freezing(p) && !freezer_should_skip(p))
                                printk(KERN_ERR " %s\n", p->comm);
-
                        cancel_freezing(p);
+                       task_unlock(p);
                } while_each_thread(g, p);
                read_unlock(&tasklist_lock);
        }
 
-       return todo;
+       return todo ? -EBUSY : 0;
 }
 
 /**
  *     freeze_processes - tell processes to enter the refrigerator
- *
- *     Returns 0 on success, or the number of processes that didn't freeze,
- *     although they were told to.
  */
 int freeze_processes(void)
 {
-       unsigned int nr_unfrozen;
+       int error;
 
        printk("Stopping tasks ... ");
-       nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
-       if (nr_unfrozen)
-               return nr_unfrozen;
+       error = try_to_freeze_tasks(FREEZER_USER_SPACE);
+       if (error)
+               return error;
 
        sys_sync();
-       nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
-       if (nr_unfrozen)
-               return nr_unfrozen;
+       error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
+       if (error)
+               return error;
 
        printk("done.\n");
        BUG_ON(in_atomic());
@@ -194,12 +210,10 @@ static void thaw_tasks(int thaw_user_space)
                if (!freezeable(p))
                        continue;
 
-               if (is_user_space(p) == !thaw_user_space)
+               if (!p->mm == thaw_user_space)
                        continue;
 
-               if (!thaw_process(p))
-                       printk(KERN_WARNING " Strange, %s not stopped\n",
-                               p->comm );
+               thaw_process(p);
        } while_each_thread(g, p);
        read_unlock(&tasklist_lock);
 }