]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - include/linux/freezer.h
freezer: fix PF_NOFREEZE vs freezeable race
[karo-tx-linux.git] / include / linux / freezer.h
1 /* Freezer declarations */
2
3 #include <linux/sched.h>
4
5 #ifdef CONFIG_PM
6 /*
7  * Check if a process has been frozen
8  */
9 static inline int frozen(struct task_struct *p)
10 {
11         return p->flags & PF_FROZEN;
12 }
13
14 /*
15  * Check if there is a request to freeze a process
16  */
17 static inline int freezing(struct task_struct *p)
18 {
19         return test_tsk_thread_flag(p, TIF_FREEZE);
20 }
21
22 /*
23  * Request that a process be frozen
24  */
25 static inline void freeze(struct task_struct *p)
26 {
27         set_tsk_thread_flag(p, TIF_FREEZE);
28 }
29
30 /*
31  * Sometimes we may need to cancel the previous 'freeze' request
32  */
33 static inline void do_not_freeze(struct task_struct *p)
34 {
35         clear_tsk_thread_flag(p, TIF_FREEZE);
36 }
37
38 /*
39  * Wake up a frozen process
40  *
41  * task_lock() is taken to prevent the race with refrigerator() which may
42  * occur if the freezing of tasks fails.  Namely, without the lock, if the
43  * freezing of tasks failed, thaw_tasks() might have run before a task in
44  * refrigerator() could call frozen_process(), in which case the task would be
45  * frozen and no one would thaw it.
46  */
47 static inline int thaw_process(struct task_struct *p)
48 {
49         task_lock(p);
50         if (frozen(p)) {
51                 p->flags &= ~PF_FROZEN;
52                 task_unlock(p);
53                 wake_up_process(p);
54                 return 1;
55         }
56         clear_tsk_thread_flag(p, TIF_FREEZE);
57         task_unlock(p);
58         return 0;
59 }
60
61 /*
62  * freezing is complete, mark process as frozen
63  */
64 static inline void frozen_process(struct task_struct *p)
65 {
66         if (!unlikely(p->flags & PF_NOFREEZE)) {
67                 p->flags |= PF_FROZEN;
68                 wmb();
69         }
70         clear_tsk_thread_flag(p, TIF_FREEZE);
71 }
72
73 extern void refrigerator(void);
74 extern int freeze_processes(void);
75 extern void thaw_processes(void);
76
77 static inline int try_to_freeze(void)
78 {
79         if (freezing(current)) {
80                 refrigerator();
81                 return 1;
82         } else
83                 return 0;
84 }
85
86 /*
87  * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
88  * calls wait_for_completion(&vfork) and reset right after it returns from this
89  * function.  Next, the parent should call try_to_freeze() to freeze itself
90  * appropriately in case the child has exited before the freezing of tasks is
91  * complete.  However, we don't want kernel threads to be frozen in unexpected
92  * places, so we allow them to block freeze_processes() instead or to set
93  * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
94  * parents.  Fortunately, in the ____call_usermodehelper() case the parent won't
95  * really block freeze_processes(), since ____call_usermodehelper() (the child)
96  * does a little before exec/exit and it can't be frozen before waking up the
97  * parent.
98  */
99
100 /*
101  * If the current task is a user space one, tell the freezer not to count it as
102  * freezable.
103  */
104 static inline void freezer_do_not_count(void)
105 {
106         if (current->mm)
107                 current->flags |= PF_FREEZER_SKIP;
108 }
109
110 /*
111  * If the current task is a user space one, tell the freezer to count it as
112  * freezable again and try to freeze it.
113  */
114 static inline void freezer_count(void)
115 {
116         if (current->mm) {
117                 current->flags &= ~PF_FREEZER_SKIP;
118                 try_to_freeze();
119         }
120 }
121
122 /*
123  * Check if the task should be counted as freezeable by the freezer
124  */
125 static inline int freezer_should_skip(struct task_struct *p)
126 {
127         return !!(p->flags & PF_FREEZER_SKIP);
128 }
129
130 #else
131 static inline int frozen(struct task_struct *p) { return 0; }
132 static inline int freezing(struct task_struct *p) { return 0; }
133 static inline void freeze(struct task_struct *p) { BUG(); }
134 static inline int thaw_process(struct task_struct *p) { return 1; }
135 static inline void frozen_process(struct task_struct *p) { BUG(); }
136
137 static inline void refrigerator(void) {}
138 static inline int freeze_processes(void) { BUG(); return 0; }
139 static inline void thaw_processes(void) {}
140
141 static inline int try_to_freeze(void) { return 0; }
142
143 static inline void freezer_do_not_count(void) {}
144 static inline void freezer_count(void) {}
145 static inline int freezer_should_skip(struct task_struct *p) { return 0; }
146 #endif