Back to home page

LXR

 
 

    


0001 /*
0002  * kernel/freezer.c - Function to freeze a process
0003  *
0004  * Originally from kernel/power/process.c
0005  */
0006 
0007 #include <linux/interrupt.h>
0008 #include <linux/suspend.h>
0009 #include <linux/export.h>
0010 #include <linux/syscalls.h>
0011 #include <linux/freezer.h>
0012 #include <linux/kthread.h>
0013 
0014 /* total number of freezing conditions in effect */
0015 atomic_t system_freezing_cnt = ATOMIC_INIT(0);
0016 EXPORT_SYMBOL(system_freezing_cnt);
0017 
0018 /* indicate whether PM freezing is in effect, protected by pm_mutex */
0019 bool pm_freezing;
0020 bool pm_nosig_freezing;
0021 
0022 /*
0023  * Temporary export for the deadlock workaround in ata_scsi_hotplug().
0024  * Remove once the hack becomes unnecessary.
0025  */
0026 EXPORT_SYMBOL_GPL(pm_freezing);
0027 
0028 /* protects freezing and frozen transitions */
0029 static DEFINE_SPINLOCK(freezer_lock);
0030 
0031 /**
0032  * freezing_slow_path - slow path for testing whether a task needs to be frozen
0033  * @p: task to be tested
0034  *
0035  * This function is called by freezing() if system_freezing_cnt isn't zero
0036  * and tests whether @p needs to enter and stay in frozen state.  Can be
0037  * called under any context.  The freezers are responsible for ensuring the
0038  * target tasks see the updated state.
0039  */
0040 bool freezing_slow_path(struct task_struct *p)
0041 {
0042     if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
0043         return false;
0044 
0045     if (test_tsk_thread_flag(p, TIF_MEMDIE))
0046         return false;
0047 
0048     if (pm_nosig_freezing || cgroup_freezing(p))
0049         return true;
0050 
0051     if (pm_freezing && !(p->flags & PF_KTHREAD))
0052         return true;
0053 
0054     return false;
0055 }
0056 EXPORT_SYMBOL(freezing_slow_path);
0057 
0058 /* Refrigerator is place where frozen processes are stored :-). */
0059 bool __refrigerator(bool check_kthr_stop)
0060 {
0061     /* Hmm, should we be allowed to suspend when there are realtime
0062        processes around? */
0063     bool was_frozen = false;
0064     long save = current->state;
0065 
0066     pr_debug("%s entered refrigerator\n", current->comm);
0067 
0068     for (;;) {
0069         set_current_state(TASK_UNINTERRUPTIBLE);
0070 
0071         spin_lock_irq(&freezer_lock);
0072         current->flags |= PF_FROZEN;
0073         if (!freezing(current) ||
0074             (check_kthr_stop && kthread_should_stop()))
0075             current->flags &= ~PF_FROZEN;
0076         spin_unlock_irq(&freezer_lock);
0077 
0078         if (!(current->flags & PF_FROZEN))
0079             break;
0080         was_frozen = true;
0081         schedule();
0082     }
0083 
0084     pr_debug("%s left refrigerator\n", current->comm);
0085 
0086     /*
0087      * Restore saved task state before returning.  The mb'd version
0088      * needs to be used; otherwise, it might silently break
0089      * synchronization which depends on ordered task state change.
0090      */
0091     set_current_state(save);
0092 
0093     return was_frozen;
0094 }
0095 EXPORT_SYMBOL(__refrigerator);
0096 
0097 static void fake_signal_wake_up(struct task_struct *p)
0098 {
0099     unsigned long flags;
0100 
0101     if (lock_task_sighand(p, &flags)) {
0102         signal_wake_up(p, 0);
0103         unlock_task_sighand(p, &flags);
0104     }
0105 }
0106 
0107 /**
0108  * freeze_task - send a freeze request to given task
0109  * @p: task to send the request to
0110  *
0111  * If @p is freezing, the freeze request is sent either by sending a fake
0112  * signal (if it's not a kernel thread) or waking it up (if it's a kernel
0113  * thread).
0114  *
0115  * RETURNS:
0116  * %false, if @p is not freezing or already frozen; %true, otherwise
0117  */
0118 bool freeze_task(struct task_struct *p)
0119 {
0120     unsigned long flags;
0121 
0122     /*
0123      * This check can race with freezer_do_not_count, but worst case that
0124      * will result in an extra wakeup being sent to the task.  It does not
0125      * race with freezer_count(), the barriers in freezer_count() and
0126      * freezer_should_skip() ensure that either freezer_count() sees
0127      * freezing == true in try_to_freeze() and freezes, or
0128      * freezer_should_skip() sees !PF_FREEZE_SKIP and freezes the task
0129      * normally.
0130      */
0131     if (freezer_should_skip(p))
0132         return false;
0133 
0134     spin_lock_irqsave(&freezer_lock, flags);
0135     if (!freezing(p) || frozen(p)) {
0136         spin_unlock_irqrestore(&freezer_lock, flags);
0137         return false;
0138     }
0139 
0140     if (!(p->flags & PF_KTHREAD))
0141         fake_signal_wake_up(p);
0142     else
0143         wake_up_state(p, TASK_INTERRUPTIBLE);
0144 
0145     spin_unlock_irqrestore(&freezer_lock, flags);
0146     return true;
0147 }
0148 
0149 void __thaw_task(struct task_struct *p)
0150 {
0151     unsigned long flags;
0152 
0153     spin_lock_irqsave(&freezer_lock, flags);
0154     if (frozen(p))
0155         wake_up_process(p);
0156     spin_unlock_irqrestore(&freezer_lock, flags);
0157 }
0158 
0159 /**
0160  * set_freezable - make %current freezable
0161  *
0162  * Mark %current freezable and enter refrigerator if necessary.
0163  */
0164 bool set_freezable(void)
0165 {
0166     might_sleep();
0167 
0168     /*
0169      * Modify flags while holding freezer_lock.  This ensures the
0170      * freezer notices that we aren't frozen yet or the freezing
0171      * condition is visible to try_to_freeze() below.
0172      */
0173     spin_lock_irq(&freezer_lock);
0174     current->flags &= ~PF_NOFREEZE;
0175     spin_unlock_irq(&freezer_lock);
0176 
0177     return try_to_freeze();
0178 }
0179 EXPORT_SYMBOL(set_freezable);