Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * cgroup_freezer.c - control group freezer subsystem |
| 3 | * |
| 4 | * Copyright IBM Corporation, 2007 |
| 5 | * |
| 6 | * Author : Cedric Le Goater <clg@fr.ibm.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of version 2.1 of the GNU Lesser General Public License |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it would be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | */ |
| 16 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 19 | #include <linux/cgroup.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/freezer.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 25 | /* |
| 26 | * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is |
| 27 | * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared |
| 28 | * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING |
| 29 | * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of |
| 30 | * its ancestors has FREEZING_SELF set. |
| 31 | */ |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 32 | enum freezer_state_flags { |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 33 | CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */ |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 34 | CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */ |
| 35 | CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */ |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 36 | CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */ |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 37 | |
| 38 | /* mask for all FREEZING flags */ |
| 39 | CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct freezer { |
Tejun Heo | bcd66c8 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 43 | struct cgroup_subsys_state css; |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 44 | unsigned int state; |
Tejun Heo | bcd66c8 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 45 | spinlock_t lock; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 48 | static inline struct freezer *css_freezer(struct cgroup_subsys_state *css) |
| 49 | { |
| 50 | return css ? container_of(css, struct freezer, css) : NULL; |
| 51 | } |
| 52 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 53 | static inline struct freezer *task_freezer(struct task_struct *task) |
| 54 | { |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 55 | return css_freezer(task_css(task, freezer_subsys_id)); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 58 | static struct freezer *parent_freezer(struct freezer *freezer) |
| 59 | { |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 60 | return css_freezer(css_parent(&freezer->css)); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 63 | bool cgroup_freezing(struct task_struct *task) |
Tomasz Buchert | d5de4dd | 2010-10-27 15:33:32 -0700 | [diff] [blame] | 64 | { |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 65 | bool ret; |
Tomasz Buchert | d5de4dd | 2010-10-27 15:33:32 -0700 | [diff] [blame] | 66 | |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 67 | rcu_read_lock(); |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 68 | ret = task_freezer(task)->state & CGROUP_FREEZING; |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 69 | rcu_read_unlock(); |
| 70 | |
| 71 | return ret; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
| 75 | * cgroups_write_string() limits the size of freezer state strings to |
| 76 | * CGROUP_LOCAL_BUFFER_SIZE |
| 77 | */ |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 78 | static const char *freezer_state_strs(unsigned int state) |
| 79 | { |
| 80 | if (state & CGROUP_FROZEN) |
| 81 | return "FROZEN"; |
| 82 | if (state & CGROUP_FREEZING) |
| 83 | return "FREEZING"; |
| 84 | return "THAWED"; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 87 | struct cgroup_subsys freezer_subsys; |
| 88 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 89 | static struct cgroup_subsys_state * |
| 90 | freezer_css_alloc(struct cgroup_subsys_state *parent_css) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 91 | { |
| 92 | struct freezer *freezer; |
| 93 | |
| 94 | freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL); |
| 95 | if (!freezer) |
| 96 | return ERR_PTR(-ENOMEM); |
| 97 | |
| 98 | spin_lock_init(&freezer->lock); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 99 | return &freezer->css; |
| 100 | } |
| 101 | |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 102 | /** |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 103 | * freezer_css_online - commit creation of a freezer css |
| 104 | * @css: css being created |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 105 | * |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 106 | * We're committing to creation of @css. Mark it online and inherit |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 107 | * parent's freezing state while holding both parent's and our |
| 108 | * freezer->lock. |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 109 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 110 | static int freezer_css_online(struct cgroup_subsys_state *css) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 111 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 112 | struct freezer *freezer = css_freezer(css); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 113 | struct freezer *parent = parent_freezer(freezer); |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 114 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 115 | /* |
| 116 | * The following double locking and freezing state inheritance |
| 117 | * guarantee that @cgroup can never escape ancestors' freezing |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 118 | * states. See css_for_each_descendant_pre() for details. |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 119 | */ |
| 120 | if (parent) |
| 121 | spin_lock_irq(&parent->lock); |
| 122 | spin_lock_nested(&freezer->lock, SINGLE_DEPTH_NESTING); |
| 123 | |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 124 | freezer->state |= CGROUP_FREEZER_ONLINE; |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 125 | |
| 126 | if (parent && (parent->state & CGROUP_FREEZING)) { |
| 127 | freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN; |
| 128 | atomic_inc(&system_freezing_cnt); |
| 129 | } |
| 130 | |
| 131 | spin_unlock(&freezer->lock); |
| 132 | if (parent) |
| 133 | spin_unlock_irq(&parent->lock); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 134 | |
| 135 | return 0; |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 139 | * freezer_css_offline - initiate destruction of a freezer css |
| 140 | * @css: css being destroyed |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 141 | * |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 142 | * @css is going away. Mark it dead and decrement system_freezing_count if |
| 143 | * it was holding one. |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 144 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 145 | static void freezer_css_offline(struct cgroup_subsys_state *css) |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 146 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 147 | struct freezer *freezer = css_freezer(css); |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 148 | |
| 149 | spin_lock_irq(&freezer->lock); |
| 150 | |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 151 | if (freezer->state & CGROUP_FREEZING) |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 152 | atomic_dec(&system_freezing_cnt); |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 153 | |
| 154 | freezer->state = 0; |
| 155 | |
| 156 | spin_unlock_irq(&freezer->lock); |
| 157 | } |
| 158 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 159 | static void freezer_css_free(struct cgroup_subsys_state *css) |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 160 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 161 | kfree(css_freezer(css)); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Matt Helsley | 957a4ee | 2008-10-18 20:27:22 -0700 | [diff] [blame] | 164 | /* |
Tejun Heo | ead5c47 | 2012-10-16 15:03:15 -0700 | [diff] [blame] | 165 | * Tasks can be migrated into a different freezer anytime regardless of its |
| 166 | * current state. freezer_attach() is responsible for making new tasks |
| 167 | * conform to the current state. |
| 168 | * |
| 169 | * Freezer state changes and task migration are synchronized via |
| 170 | * @freezer->lock. freezer_attach() makes the new tasks conform to the |
| 171 | * current state and all following state changes can see the new tasks. |
Matt Helsley | 957a4ee | 2008-10-18 20:27:22 -0700 | [diff] [blame] | 172 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 173 | static void freezer_attach(struct cgroup_subsys_state *new_css, |
| 174 | struct cgroup_taskset *tset) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 175 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 176 | struct freezer *freezer = css_freezer(new_css); |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 177 | struct task_struct *task; |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 178 | bool clear_frozen = false; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 179 | |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 180 | spin_lock_irq(&freezer->lock); |
| 181 | |
Li Zefan | 80a6a2c | 2008-10-29 14:00:52 -0700 | [diff] [blame] | 182 | /* |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 183 | * Make the new tasks conform to the current state of @new_css. |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 184 | * For simplicity, when migrating any task to a FROZEN cgroup, we |
| 185 | * revert it to FREEZING and let update_if_frozen() determine the |
| 186 | * correct state later. |
| 187 | * |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 188 | * Tasks in @tset are on @new_css but may not conform to its |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 189 | * current state before executing the following - !frozen tasks may |
| 190 | * be visible in a FROZEN cgroup and frozen tasks in a THAWED one. |
Li Zefan | 80a6a2c | 2008-10-29 14:00:52 -0700 | [diff] [blame] | 191 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 192 | cgroup_taskset_for_each(task, new_css->cgroup, tset) { |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 193 | if (!(freezer->state & CGROUP_FREEZING)) { |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 194 | __thaw_task(task); |
| 195 | } else { |
| 196 | freeze_task(task); |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 197 | freezer->state &= ~CGROUP_FROZEN; |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 198 | clear_frozen = true; |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
Matt Helsley | 957a4ee | 2008-10-18 20:27:22 -0700 | [diff] [blame] | 201 | |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 202 | spin_unlock_irq(&freezer->lock); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Propagate FROZEN clearing upwards. We may race with |
| 206 | * update_if_frozen(), but as long as both work bottom-up, either |
| 207 | * update_if_frozen() sees child's FROZEN cleared or we clear the |
| 208 | * parent's FROZEN later. No parent w/ !FROZEN children can be |
| 209 | * left FROZEN. |
| 210 | */ |
| 211 | while (clear_frozen && (freezer = parent_freezer(freezer))) { |
| 212 | spin_lock_irq(&freezer->lock); |
| 213 | freezer->state &= ~CGROUP_FROZEN; |
| 214 | clear_frozen = freezer->state & CGROUP_FREEZING; |
| 215 | spin_unlock_irq(&freezer->lock); |
| 216 | } |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 219 | static void freezer_fork(struct task_struct *task) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 220 | { |
| 221 | struct freezer *freezer; |
| 222 | |
Paul E. McKenney | 8b46f88 | 2010-04-21 13:02:08 -0700 | [diff] [blame] | 223 | rcu_read_lock(); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 224 | freezer = task_freezer(task); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 225 | |
Li Zefan | 3b1b3f6 | 2008-11-12 13:26:50 -0800 | [diff] [blame] | 226 | /* |
| 227 | * The root cgroup is non-freezable, so we can skip the |
| 228 | * following check. |
| 229 | */ |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 230 | if (!parent_freezer(freezer)) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 231 | goto out; |
Li Zefan | 3b1b3f6 | 2008-11-12 13:26:50 -0800 | [diff] [blame] | 232 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 233 | spin_lock_irq(&freezer->lock); |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 234 | if (freezer->state & CGROUP_FREEZING) |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 235 | freeze_task(task); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 236 | spin_unlock_irq(&freezer->lock); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 237 | out: |
| 238 | rcu_read_unlock(); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 241 | /** |
| 242 | * update_if_frozen - update whether a cgroup finished freezing |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 243 | * @css: css of interest |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 244 | * |
| 245 | * Once FREEZING is initiated, transition to FROZEN is lazily updated by |
| 246 | * calling this function. If the current state is FREEZING but not FROZEN, |
| 247 | * this function checks whether all tasks of this cgroup and the descendant |
| 248 | * cgroups finished freezing and, if so, sets FROZEN. |
| 249 | * |
| 250 | * The caller is responsible for grabbing RCU read lock and calling |
| 251 | * update_if_frozen() on all descendants prior to invoking this function. |
Tejun Heo | b4d1831 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 252 | * |
| 253 | * Task states and freezer state might disagree while tasks are being |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 254 | * migrated into or out of @css, so we can't verify task states against |
Tejun Heo | ead5c47 | 2012-10-16 15:03:15 -0700 | [diff] [blame] | 255 | * @freezer state here. See freezer_attach() for details. |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 256 | */ |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 257 | static void update_if_frozen(struct cgroup_subsys_state *css) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 258 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 259 | struct freezer *freezer = css_freezer(css); |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 260 | struct cgroup_subsys_state *pos; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 261 | struct css_task_iter it; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 262 | struct task_struct *task; |
Tejun Heo | b4d1831 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 263 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 264 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 265 | |
| 266 | spin_lock_irq(&freezer->lock); |
| 267 | |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 268 | if (!(freezer->state & CGROUP_FREEZING) || |
| 269 | (freezer->state & CGROUP_FROZEN)) |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 270 | goto out_unlock; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 271 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 272 | /* are all (live) children frozen? */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 273 | css_for_each_child(pos, css) { |
| 274 | struct freezer *child = css_freezer(pos); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 275 | |
| 276 | if ((child->state & CGROUP_FREEZER_ONLINE) && |
| 277 | !(child->state & CGROUP_FROZEN)) |
| 278 | goto out_unlock; |
| 279 | } |
| 280 | |
| 281 | /* are all tasks frozen? */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 282 | css_task_iter_start(css, &it); |
Tejun Heo | b4d1831 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 283 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 284 | while ((task = css_task_iter_next(&it))) { |
Tejun Heo | 3c426d5 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 285 | if (freezing(task)) { |
Tejun Heo | 3c426d5 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 286 | /* |
| 287 | * freezer_should_skip() indicates that the task |
| 288 | * should be skipped when determining freezing |
| 289 | * completion. Consider it frozen in addition to |
| 290 | * the usual frozen condition. |
| 291 | */ |
Oleg Nesterov | 5d8f72b | 2012-10-26 19:46:06 +0200 | [diff] [blame] | 292 | if (!frozen(task) && !freezer_should_skip(task)) |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 293 | goto out_iter_end; |
Tejun Heo | 3c426d5 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 294 | } |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 297 | freezer->state |= CGROUP_FROZEN; |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 298 | out_iter_end: |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 299 | css_task_iter_end(&it); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 300 | out_unlock: |
| 301 | spin_unlock_irq(&freezer->lock); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 304 | static int freezer_read(struct cgroup_subsys_state *css, struct cftype *cft, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 305 | struct seq_file *m) |
| 306 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 307 | struct cgroup_subsys_state *pos; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 308 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 309 | rcu_read_lock(); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 310 | |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 311 | /* update states bottom-up */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 312 | css_for_each_descendant_post(pos, css) |
| 313 | update_if_frozen(pos); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 314 | update_if_frozen(css); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 315 | |
| 316 | rcu_read_unlock(); |
| 317 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 318 | seq_puts(m, freezer_state_strs(css_freezer(css)->state)); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 319 | seq_putc(m, '\n'); |
| 320 | return 0; |
| 321 | } |
| 322 | |
Tejun Heo | bcd66c8 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 323 | static void freeze_cgroup(struct freezer *freezer) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 324 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 325 | struct css_task_iter it; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 326 | struct task_struct *task; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 327 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 328 | css_task_iter_start(&freezer->css, &it); |
| 329 | while ((task = css_task_iter_next(&it))) |
Tejun Heo | 51f246e | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 330 | freeze_task(task); |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 331 | css_task_iter_end(&it); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Tejun Heo | bcd66c8 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 334 | static void unfreeze_cgroup(struct freezer *freezer) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 335 | { |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 336 | struct css_task_iter it; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 337 | struct task_struct *task; |
| 338 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 339 | css_task_iter_start(&freezer->css, &it); |
| 340 | while ((task = css_task_iter_next(&it))) |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 341 | __thaw_task(task); |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 342 | css_task_iter_end(&it); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 345 | /** |
| 346 | * freezer_apply_state - apply state change to a single cgroup_freezer |
| 347 | * @freezer: freezer to apply state change to |
| 348 | * @freeze: whether to freeze or unfreeze |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 349 | * @state: CGROUP_FREEZING_* flag to set or clear |
| 350 | * |
| 351 | * Set or clear @state on @cgroup according to @freeze, and perform |
| 352 | * freezing or thawing as necessary. |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 353 | */ |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 354 | static void freezer_apply_state(struct freezer *freezer, bool freeze, |
| 355 | unsigned int state) |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 356 | { |
Tejun Heo | ead5c47 | 2012-10-16 15:03:15 -0700 | [diff] [blame] | 357 | /* also synchronizes against task migration, see freezer_attach() */ |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 358 | lockdep_assert_held(&freezer->lock); |
Li Zefan | 51308ee | 2008-10-29 14:00:54 -0700 | [diff] [blame] | 359 | |
Tejun Heo | 5300a9b | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 360 | if (!(freezer->state & CGROUP_FREEZER_ONLINE)) |
| 361 | return; |
| 362 | |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 363 | if (freeze) { |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 364 | if (!(freezer->state & CGROUP_FREEZING)) |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 365 | atomic_inc(&system_freezing_cnt); |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 366 | freezer->state |= state; |
Tejun Heo | bcd66c8 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 367 | freeze_cgroup(freezer); |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 368 | } else { |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 369 | bool was_freezing = freezer->state & CGROUP_FREEZING; |
| 370 | |
| 371 | freezer->state &= ~state; |
| 372 | |
| 373 | if (!(freezer->state & CGROUP_FREEZING)) { |
| 374 | if (was_freezing) |
| 375 | atomic_dec(&system_freezing_cnt); |
| 376 | freezer->state &= ~CGROUP_FROZEN; |
| 377 | unfreeze_cgroup(freezer); |
| 378 | } |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 379 | } |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 380 | } |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 381 | |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 382 | /** |
| 383 | * freezer_change_state - change the freezing state of a cgroup_freezer |
| 384 | * @freezer: freezer of interest |
| 385 | * @freeze: whether to freeze or thaw |
| 386 | * |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 387 | * Freeze or thaw @freezer according to @freeze. The operations are |
| 388 | * recursive - all descendants of @freezer will be affected. |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 389 | */ |
| 390 | static void freezer_change_state(struct freezer *freezer, bool freeze) |
| 391 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 392 | struct cgroup_subsys_state *pos; |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 393 | |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 394 | /* update @freezer */ |
| 395 | spin_lock_irq(&freezer->lock); |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 396 | freezer_apply_state(freezer, freeze, CGROUP_FREEZING_SELF); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 397 | spin_unlock_irq(&freezer->lock); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Update all its descendants in pre-order traversal. Each |
| 401 | * descendant will try to inherit its parent's FREEZING state as |
| 402 | * CGROUP_FREEZING_PARENT. |
| 403 | */ |
| 404 | rcu_read_lock(); |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 405 | css_for_each_descendant_pre(pos, &freezer->css) { |
| 406 | struct freezer *pos_f = css_freezer(pos); |
Tejun Heo | ef9fe98 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 407 | struct freezer *parent = parent_freezer(pos_f); |
| 408 | |
| 409 | /* |
| 410 | * Our update to @parent->state is already visible which is |
| 411 | * all we need. No need to lock @parent. For more info on |
| 412 | * synchronization, see freezer_post_create(). |
| 413 | */ |
| 414 | spin_lock_irq(&pos_f->lock); |
| 415 | freezer_apply_state(pos_f, parent->state & CGROUP_FREEZING, |
| 416 | CGROUP_FREEZING_PARENT); |
| 417 | spin_unlock_irq(&pos_f->lock); |
| 418 | } |
| 419 | rcu_read_unlock(); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 422 | static int freezer_write(struct cgroup_subsys_state *css, struct cftype *cft, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 423 | const char *buffer) |
| 424 | { |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 425 | bool freeze; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 426 | |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 427 | if (strcmp(buffer, freezer_state_strs(0)) == 0) |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 428 | freeze = false; |
Tejun Heo | d6a2fe1 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 429 | else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0) |
Tejun Heo | 04a4ec3 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 430 | freeze = true; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 431 | else |
Li Zefan | 3b1b3f6 | 2008-11-12 13:26:50 -0800 | [diff] [blame] | 432 | return -EINVAL; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 433 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 434 | freezer_change_state(css_freezer(css), freeze); |
Tejun Heo | 51f246e | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 435 | return 0; |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 438 | static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css, |
| 439 | struct cftype *cft) |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 440 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 441 | struct freezer *freezer = css_freezer(css); |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 442 | |
| 443 | return (bool)(freezer->state & CGROUP_FREEZING_SELF); |
| 444 | } |
| 445 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 446 | static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css, |
| 447 | struct cftype *cft) |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 448 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 449 | struct freezer *freezer = css_freezer(css); |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 450 | |
| 451 | return (bool)(freezer->state & CGROUP_FREEZING_PARENT); |
| 452 | } |
| 453 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 454 | static struct cftype files[] = { |
| 455 | { |
| 456 | .name = "state", |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 457 | .flags = CFTYPE_NOT_ON_ROOT, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 458 | .read_seq_string = freezer_read, |
| 459 | .write_string = freezer_write, |
| 460 | }, |
Tejun Heo | a225218 | 2012-11-09 09:12:30 -0800 | [diff] [blame] | 461 | { |
| 462 | .name = "self_freezing", |
| 463 | .flags = CFTYPE_NOT_ON_ROOT, |
| 464 | .read_u64 = freezer_self_freezing_read, |
| 465 | }, |
| 466 | { |
| 467 | .name = "parent_freezing", |
| 468 | .flags = CFTYPE_NOT_ON_ROOT, |
| 469 | .read_u64 = freezer_parent_freezing_read, |
| 470 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 471 | { } /* terminate */ |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 474 | struct cgroup_subsys freezer_subsys = { |
| 475 | .name = "freezer", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 476 | .css_alloc = freezer_css_alloc, |
| 477 | .css_online = freezer_css_online, |
| 478 | .css_offline = freezer_css_offline, |
| 479 | .css_free = freezer_css_free, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 480 | .subsys_id = freezer_subsys_id, |
Tejun Heo | 8755ade | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 481 | .attach = freezer_attach, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 482 | .fork = freezer_fork, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 483 | .base_cftypes = files, |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 484 | }; |