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