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