Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

freezer: make freezing() test freeze conditions in effect instead of TIF_FREEZE

Using TIF_FREEZE for freezing worked when there was only single
freezing condition (the PM one); however, now there is also the
cgroup_freezer and single bit flag is getting clumsy.
thaw_processes() is already testing whether cgroup freezing in in
effect to avoid thawing tasks which were frozen by both PM and cgroup
freezers.

This is racy (nothing prevents race against cgroup freezing) and
fragile. A much simpler way is to test actual freeze conditions from
freezing() - ie. directly test whether PM or cgroup freezing is in
effect.

This patch adds variables to indicate whether and what type of
freezing conditions are in effect and reimplements freezing() such
that it directly tests whether any of the two freezing conditions is
active and the task should freeze. On fast path, freezing() is still
very cheap - it only tests system_freezing_cnt.

This makes the clumsy dancing aroung TIF_FREEZE unnecessary and
freeze/thaw operations more usual - updating state variables for the
new state and nudging target tasks so that they notice the new state
and comply. As long as the nudging happens after state update, it's
race-free.

* This allows use of freezing() in freeze_task(). Replace the open
coded tests with freezing().

* p != current test is added to warning printing conditions in
try_to_freeze_tasks() failure path. This is necessary as freezing()
is now true for the task which initiated freezing too.

-v2: Oleg pointed out that re-freezing FROZEN cgroup could increment
system_freezing_cnt. Fixed.

Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Paul Menage <paul@paulmenage.org> (for the cgroup portions)

Tejun Heo a3201227 22b4e111

+72 -49
+12 -21
include/linux/freezer.h
··· 5 5 6 6 #include <linux/sched.h> 7 7 #include <linux/wait.h> 8 + #include <linux/atomic.h> 8 9 9 10 #ifdef CONFIG_FREEZER 11 + extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */ 12 + extern bool pm_freezing; /* PM freezing in effect */ 13 + extern bool pm_nosig_freezing; /* PM nosig freezing in effect */ 14 + 10 15 /* 11 16 * Check if a process has been frozen 12 17 */ ··· 20 15 return p->flags & PF_FROZEN; 21 16 } 22 17 18 + extern bool freezing_slow_path(struct task_struct *p); 19 + 23 20 /* 24 21 * Check if there is a request to freeze a process 25 22 */ 26 - static inline int freezing(struct task_struct *p) 23 + static inline bool freezing(struct task_struct *p) 27 24 { 28 - return test_tsk_thread_flag(p, TIF_FREEZE); 29 - } 30 - 31 - /* 32 - * Request that a process be frozen 33 - */ 34 - static inline void set_freeze_flag(struct task_struct *p) 35 - { 36 - set_tsk_thread_flag(p, TIF_FREEZE); 37 - } 38 - 39 - /* 40 - * Sometimes we may need to cancel the previous 'freeze' request 41 - */ 42 - static inline void clear_freeze_flag(struct task_struct *p) 43 - { 44 - clear_tsk_thread_flag(p, TIF_FREEZE); 25 + if (likely(!atomic_read(&system_freezing_cnt))) 26 + return false; 27 + return freezing_slow_path(p); 45 28 } 46 29 47 30 static inline bool should_send_signal(struct task_struct *p) ··· 167 174 }) 168 175 #else /* !CONFIG_FREEZER */ 169 176 static inline int frozen(struct task_struct *p) { return 0; } 170 - static inline int freezing(struct task_struct *p) { return 0; } 171 - static inline void set_freeze_flag(struct task_struct *p) {} 172 - static inline void clear_freeze_flag(struct task_struct *p) {} 177 + static inline bool freezing(struct task_struct *p) { return false; } 173 178 174 179 static inline bool __refrigerator(bool check_kthr_stop) { return false; } 175 180 static inline int freeze_processes(void) { return -ENOSYS; }
+9 -1
kernel/cgroup_freezer.c
··· 145 145 static void freezer_destroy(struct cgroup_subsys *ss, 146 146 struct cgroup *cgroup) 147 147 { 148 - kfree(cgroup_freezer(cgroup)); 148 + struct freezer *freezer = cgroup_freezer(cgroup); 149 + 150 + if (freezer->state != CGROUP_THAWED) 151 + atomic_dec(&system_freezing_cnt); 152 + kfree(freezer); 149 153 } 150 154 151 155 /* ··· 311 307 312 308 switch (goal_state) { 313 309 case CGROUP_THAWED: 310 + if (freezer->state != CGROUP_THAWED) 311 + atomic_dec(&system_freezing_cnt); 314 312 freezer->state = CGROUP_THAWED; 315 313 unfreeze_cgroup(cgroup, freezer); 316 314 break; 317 315 case CGROUP_FROZEN: 316 + if (freezer->state == CGROUP_THAWED) 317 + atomic_inc(&system_freezing_cnt); 318 318 freezer->state = CGROUP_FREEZING; 319 319 retval = try_to_freeze_cgroup(cgroup, freezer); 320 320 break;
-1
kernel/fork.c
··· 997 997 new_flags |= PF_FORKNOEXEC; 998 998 new_flags |= PF_STARTING; 999 999 p->flags = new_flags; 1000 - clear_freeze_flag(p); 1001 1000 } 1002 1001 1003 1002 SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
+40 -22
kernel/freezer.c
··· 11 11 #include <linux/freezer.h> 12 12 #include <linux/kthread.h> 13 13 14 + /* total number of freezing conditions in effect */ 15 + atomic_t system_freezing_cnt = ATOMIC_INIT(0); 16 + EXPORT_SYMBOL(system_freezing_cnt); 17 + 18 + /* indicate whether PM freezing is in effect, protected by pm_mutex */ 19 + bool pm_freezing; 20 + bool pm_nosig_freezing; 21 + 14 22 /* protects freezing and frozen transitions */ 15 23 static DEFINE_SPINLOCK(freezer_lock); 24 + 25 + /** 26 + * freezing_slow_path - slow path for testing whether a task needs to be frozen 27 + * @p: task to be tested 28 + * 29 + * This function is called by freezing() if system_freezing_cnt isn't zero 30 + * and tests whether @p needs to enter and stay in frozen state. Can be 31 + * called under any context. The freezers are responsible for ensuring the 32 + * target tasks see the updated state. 33 + */ 34 + bool freezing_slow_path(struct task_struct *p) 35 + { 36 + if (p->flags & PF_NOFREEZE) 37 + return false; 38 + 39 + if (pm_nosig_freezing || cgroup_freezing(p)) 40 + return true; 41 + 42 + if (pm_freezing && !(p->flags & PF_FREEZER_NOSIG)) 43 + return true; 44 + 45 + return false; 46 + } 47 + EXPORT_SYMBOL(freezing_slow_path); 16 48 17 49 /* Refrigerator is place where frozen processes are stored :-). */ 18 50 bool __refrigerator(bool check_kthr_stop) ··· 55 23 long save; 56 24 57 25 /* 58 - * Enter FROZEN. If NOFREEZE, schedule immediate thawing by 59 - * clearing freezing. 26 + * No point in checking freezing() again - the caller already did. 27 + * Proceed to enter FROZEN. 60 28 */ 61 29 spin_lock_irq(&freezer_lock); 62 30 repeat: 63 - if (!freezing(current)) { 64 - spin_unlock_irq(&freezer_lock); 65 - return was_frozen; 66 - } 67 - if (current->flags & PF_NOFREEZE) 68 - clear_freeze_flag(current); 69 31 current->flags |= PF_FROZEN; 70 32 spin_unlock_irq(&freezer_lock); 71 33 ··· 125 99 bool freeze_task(struct task_struct *p, bool sig_only) 126 100 { 127 101 unsigned long flags; 128 - bool ret = false; 129 102 130 103 spin_lock_irqsave(&freezer_lock, flags); 131 - 132 - if ((p->flags & PF_NOFREEZE) || 133 - (sig_only && !should_send_signal(p))) 134 - goto out_unlock; 135 - 136 - if (frozen(p)) 137 - goto out_unlock; 138 - 139 - set_freeze_flag(p); 104 + if (!freezing(p) || frozen(p)) { 105 + spin_unlock_irqrestore(&freezer_lock, flags); 106 + return false; 107 + } 140 108 141 109 if (should_send_signal(p)) { 142 110 fake_signal_wake_up(p); ··· 143 123 } else { 144 124 wake_up_state(p, TASK_INTERRUPTIBLE); 145 125 } 146 - ret = true; 147 - out_unlock: 126 + 148 127 spin_unlock_irqrestore(&freezer_lock, flags); 149 - return ret; 128 + return true; 150 129 } 151 130 152 131 void __thaw_task(struct task_struct *p) ··· 162 143 * avoid leaving dangling TIF_SIGPENDING behind. 163 144 */ 164 145 spin_lock_irqsave(&freezer_lock, flags); 165 - clear_freeze_flag(p); 166 146 if (frozen(p)) { 167 147 wake_up_process(p); 168 148 } else {
+11 -4
kernel/power/process.c
··· 101 101 read_lock(&tasklist_lock); 102 102 do_each_thread(g, p) { 103 103 if (!wakeup && !freezer_should_skip(p) && 104 - freezing(p) && !frozen(p)) 104 + p != current && freezing(p) && !frozen(p)) 105 105 sched_show_task(p); 106 106 } while_each_thread(g, p); 107 107 read_unlock(&tasklist_lock); ··· 122 122 { 123 123 int error; 124 124 125 + if (!pm_freezing) 126 + atomic_inc(&system_freezing_cnt); 127 + 125 128 printk("Freezing user space processes ... "); 129 + pm_freezing = true; 126 130 error = try_to_freeze_tasks(true); 127 131 if (!error) { 128 132 printk("done."); ··· 150 146 int error; 151 147 152 148 printk("Freezing remaining freezable tasks ... "); 149 + pm_nosig_freezing = true; 153 150 error = try_to_freeze_tasks(false); 154 151 if (!error) 155 152 printk("done."); ··· 167 162 { 168 163 struct task_struct *g, *p; 169 164 165 + if (pm_freezing) 166 + atomic_dec(&system_freezing_cnt); 167 + pm_freezing = false; 168 + pm_nosig_freezing = false; 169 + 170 170 oom_killer_enable(); 171 171 172 172 printk("Restarting tasks ... "); ··· 180 170 181 171 read_lock(&tasklist_lock); 182 172 do_each_thread(g, p) { 183 - if (cgroup_freezing(p)) 184 - continue; 185 - 186 173 __thaw_task(p); 187 174 } while_each_thread(g, p); 188 175 read_unlock(&tasklist_lock);