Input: ads7846 - SPI_CPHA mode bugfix
[firefly-linux-kernel-4.4.55.git] / kernel / power / process.c
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/smp_lock.h>
12 #include <linux/interrupt.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
17
18 /* 
19  * Timeout for stopping processes
20  */
21 #define TIMEOUT (20 * HZ)
22
23 #define FREEZER_KERNEL_THREADS 0
24 #define FREEZER_USER_SPACE 1
25
26 static inline int freezeable(struct task_struct * p)
27 {
28         if ((p == current) || 
29             (p->flags & PF_NOFREEZE) ||
30             (p->exit_state == EXIT_ZOMBIE) ||
31             (p->exit_state == EXIT_DEAD))
32                 return 0;
33         return 1;
34 }
35
36 /* Refrigerator is place where frozen processes are stored :-). */
37 void refrigerator(void)
38 {
39         /* Hmm, should we be allowed to suspend when there are realtime
40            processes around? */
41         long save;
42         save = current->state;
43         pr_debug("%s entered refrigerator\n", current->comm);
44
45         frozen_process(current);
46         spin_lock_irq(&current->sighand->siglock);
47         recalc_sigpending(); /* We sent fake signal, clean it up */
48         spin_unlock_irq(&current->sighand->siglock);
49
50         for (;;) {
51                 set_current_state(TASK_UNINTERRUPTIBLE);
52                 if (!frozen(current))
53                         break;
54                 schedule();
55         }
56         pr_debug("%s left refrigerator\n", current->comm);
57         current->state = save;
58 }
59
60 static inline void freeze_process(struct task_struct *p)
61 {
62         unsigned long flags;
63
64         if (!freezing(p)) {
65                 rmb();
66                 if (!frozen(p)) {
67                         if (p->state == TASK_STOPPED)
68                                 force_sig_specific(SIGSTOP, p);
69
70                         freeze(p);
71                         spin_lock_irqsave(&p->sighand->siglock, flags);
72                         signal_wake_up(p, p->state == TASK_STOPPED);
73                         spin_unlock_irqrestore(&p->sighand->siglock, flags);
74                 }
75         }
76 }
77
78 static void cancel_freezing(struct task_struct *p)
79 {
80         unsigned long flags;
81
82         if (freezing(p)) {
83                 pr_debug("  clean up: %s\n", p->comm);
84                 do_not_freeze(p);
85                 spin_lock_irqsave(&p->sighand->siglock, flags);
86                 recalc_sigpending_tsk(p);
87                 spin_unlock_irqrestore(&p->sighand->siglock, flags);
88         }
89 }
90
91 static inline int is_user_space(struct task_struct *p)
92 {
93         return p->mm && !(p->flags & PF_BORROWED_MM);
94 }
95
96 static unsigned int try_to_freeze_tasks(int freeze_user_space)
97 {
98         struct task_struct *g, *p;
99         unsigned long end_time;
100         unsigned int todo;
101
102         end_time = jiffies + TIMEOUT;
103         do {
104                 todo = 0;
105                 read_lock(&tasklist_lock);
106                 do_each_thread(g, p) {
107                         if (!freezeable(p))
108                                 continue;
109
110                         if (frozen(p))
111                                 continue;
112
113                         if (p->state == TASK_TRACED && frozen(p->parent)) {
114                                 cancel_freezing(p);
115                                 continue;
116                         }
117                         if (is_user_space(p)) {
118                                 if (!freeze_user_space)
119                                         continue;
120
121                                 /* Freeze the task unless there is a vfork
122                                  * completion pending
123                                  */
124                                 if (!p->vfork_done)
125                                         freeze_process(p);
126                         } else {
127                                 if (freeze_user_space)
128                                         continue;
129
130                                 freeze_process(p);
131                         }
132                         todo++;
133                 } while_each_thread(g, p);
134                 read_unlock(&tasklist_lock);
135                 yield();                        /* Yield is okay here */
136                 if (todo && time_after(jiffies, end_time))
137                         break;
138         } while (todo);
139
140         if (todo) {
141                 /* This does not unfreeze processes that are already frozen
142                  * (we have slightly ugly calling convention in that respect,
143                  * and caller must call thaw_processes() if something fails),
144                  * but it cleans up leftover PF_FREEZE requests.
145                  */
146                 printk("\n");
147                 printk(KERN_ERR "Stopping %s timed out after %d seconds "
148                                 "(%d tasks refusing to freeze):\n",
149                                 freeze_user_space ? "user space processes" :
150                                         "kernel threads",
151                                 TIMEOUT / HZ, todo);
152                 read_lock(&tasklist_lock);
153                 do_each_thread(g, p) {
154                         if (is_user_space(p) == !freeze_user_space)
155                                 continue;
156
157                         if (freezeable(p) && !frozen(p))
158                                 printk(KERN_ERR " %s\n", p->comm);
159
160                         cancel_freezing(p);
161                 } while_each_thread(g, p);
162                 read_unlock(&tasklist_lock);
163         }
164
165         return todo;
166 }
167
168 /**
169  *      freeze_processes - tell processes to enter the refrigerator
170  *
171  *      Returns 0 on success, or the number of processes that didn't freeze,
172  *      although they were told to.
173  */
174 int freeze_processes(void)
175 {
176         unsigned int nr_unfrozen;
177
178         printk("Stopping tasks ... ");
179         nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
180         if (nr_unfrozen)
181                 return nr_unfrozen;
182
183         sys_sync();
184         nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
185         if (nr_unfrozen)
186                 return nr_unfrozen;
187
188         printk("done.\n");
189         BUG_ON(in_atomic());
190         return 0;
191 }
192
193 static void thaw_tasks(int thaw_user_space)
194 {
195         struct task_struct *g, *p;
196
197         read_lock(&tasklist_lock);
198         do_each_thread(g, p) {
199                 if (!freezeable(p))
200                         continue;
201
202                 if (is_user_space(p) == !thaw_user_space)
203                         continue;
204
205                 if (!thaw_process(p))
206                         printk(KERN_WARNING " Strange, %s not stopped\n",
207                                 p->comm );
208         } while_each_thread(g, p);
209         read_unlock(&tasklist_lock);
210 }
211
212 void thaw_processes(void)
213 {
214         printk("Restarting tasks ... ");
215         thaw_tasks(FREEZER_KERNEL_THREADS);
216         thaw_tasks(FREEZER_USER_SPACE);
217         schedule();
218         printk("done.\n");
219 }
220
221 EXPORT_SYMBOL(refrigerator);