1 /* 2 * drivers/power/process.c - Functions for starting/stopping processes on 3 * suspend transitions. 4 * 5 * Originally from swsusp. 6 */ 7 8 9 #undef DEBUG 10 11 #include <linux/interrupt.h> 12 #include <linux/oom.h> 13 #include <linux/suspend.h> 14 #include <linux/module.h> 15 #include <linux/syscalls.h> 16 #include <linux/freezer.h> 17 #include <linux/delay.h> 18 #include <linux/workqueue.h> 19 20 /* 21 * Timeout for stopping processes 22 */ 23 #define TIMEOUT (20 * HZ) 24 25 static inline int freezeable(struct task_struct * p) 26 { 27 if ((p == current) || 28 (p->flags & PF_NOFREEZE) || 29 (p->exit_state != 0)) 30 return 0; 31 return 1; 32 } 33 34 static int try_to_freeze_tasks(bool sig_only) 35 { 36 struct task_struct *g, *p; 37 unsigned long end_time; 38 unsigned int todo; 39 bool wq_busy = false; 40 struct timeval start, end; 41 u64 elapsed_csecs64; 42 unsigned int elapsed_csecs; 43 bool wakeup = false; 44 45 do_gettimeofday(&start); 46 47 end_time = jiffies + TIMEOUT; 48 49 if (!sig_only) 50 freeze_workqueues_begin(); 51 52 while (true) { 53 todo = 0; 54 read_lock(&tasklist_lock); 55 do_each_thread(g, p) { 56 if (frozen(p) || !freezeable(p)) 57 continue; 58 59 if (!freeze_task(p, sig_only)) 60 continue; 61 62 /* 63 * Now that we've done set_freeze_flag, don't 64 * perturb a task in TASK_STOPPED or TASK_TRACED. 65 * It is "frozen enough". If the task does wake 66 * up, it will immediately call try_to_freeze. 67 */ 68 if (!task_is_stopped_or_traced(p) && 69 !freezer_should_skip(p)) 70 todo++; 71 } while_each_thread(g, p); 72 read_unlock(&tasklist_lock); 73 74 if (!sig_only) { 75 wq_busy = freeze_workqueues_busy(); 76 todo += wq_busy; 77 } 78 79 if (!todo || time_after(jiffies, end_time)) 80 break; 81 82 if (!pm_check_wakeup_events()) { 83 wakeup = true; 84 break; 85 } 86 87 /* 88 * We need to retry, but first give the freezing tasks some 89 * time to enter the regrigerator. 90 */ 91 msleep(10); 92 } 93 94 do_gettimeofday(&end); 95 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start); 96 do_div(elapsed_csecs64, NSEC_PER_SEC / 100); 97 elapsed_csecs = elapsed_csecs64; 98 99 if (todo) { 100 /* This does not unfreeze processes that are already frozen 101 * (we have slightly ugly calling convention in that respect, 102 * and caller must call thaw_processes() if something fails), 103 * but it cleans up leftover PF_FREEZE requests. 104 */ 105 printk("\n"); 106 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds " 107 "(%d tasks refusing to freeze, wq_busy=%d):\n", 108 wakeup ? "aborted" : "failed", 109 elapsed_csecs / 100, elapsed_csecs % 100, 110 todo - wq_busy, wq_busy); 111 112 thaw_workqueues(); 113 114 read_lock(&tasklist_lock); 115 do_each_thread(g, p) { 116 task_lock(p); 117 if (!wakeup && freezing(p) && !freezer_should_skip(p)) 118 sched_show_task(p); 119 cancel_freezing(p); 120 task_unlock(p); 121 } while_each_thread(g, p); 122 read_unlock(&tasklist_lock); 123 } else { 124 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100, 125 elapsed_csecs % 100); 126 } 127 128 return todo ? -EBUSY : 0; 129 } 130 131 /** 132 * freeze_processes - tell processes to enter the refrigerator 133 */ 134 int freeze_processes(void) 135 { 136 int error; 137 138 printk("Freezing user space processes ... "); 139 error = try_to_freeze_tasks(true); 140 if (error) 141 goto Exit; 142 printk("done.\n"); 143 144 printk("Freezing remaining freezable tasks ... "); 145 error = try_to_freeze_tasks(false); 146 if (error) 147 goto Exit; 148 printk("done."); 149 150 oom_killer_disable(); 151 Exit: 152 BUG_ON(in_atomic()); 153 printk("\n"); 154 155 return error; 156 } 157 158 static void thaw_tasks(bool nosig_only) 159 { 160 struct task_struct *g, *p; 161 162 read_lock(&tasklist_lock); 163 do_each_thread(g, p) { 164 if (!freezeable(p)) 165 continue; 166 167 if (nosig_only && should_send_signal(p)) 168 continue; 169 170 if (cgroup_freezing_or_frozen(p)) 171 continue; 172 173 thaw_process(p); 174 } while_each_thread(g, p); 175 read_unlock(&tasklist_lock); 176 } 177 178 void thaw_processes(void) 179 { 180 oom_killer_enable(); 181 182 printk("Restarting tasks ... "); 183 thaw_workqueues(); 184 thaw_tasks(true); 185 thaw_tasks(false); 186 schedule(); 187 printk("done.\n"); 188 } 189 190