1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * cgroup_freezer.c - control group freezer subsystem 4 * 5 * Copyright IBM Corporation, 2007 6 * 7 * Author : Cedric Le Goater <clg@fr.ibm.com> 8 */ 9 10 #include <linux/export.h> 11 #include <linux/slab.h> 12 #include <linux/cgroup.h> 13 #include <linux/fs.h> 14 #include <linux/uaccess.h> 15 #include <linux/freezer.h> 16 #include <linux/seq_file.h> 17 #include <linux/mutex.h> 18 #include <linux/cpu.h> 19 20 /* 21 * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is 22 * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared 23 * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING 24 * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of 25 * its ancestors has FREEZING_SELF set. 26 */ 27 enum freezer_state_flags { 28 CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */ 29 CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */ 30 CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */ 31 CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */ 32 33 /* mask for all FREEZING flags */ 34 CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT, 35 }; 36 37 struct freezer { 38 struct cgroup_subsys_state css; 39 unsigned int state; 40 }; 41 42 static DEFINE_MUTEX(freezer_mutex); 43 44 static inline struct freezer *css_freezer(struct cgroup_subsys_state *css) 45 { 46 return css ? container_of(css, struct freezer, css) : NULL; 47 } 48 49 static inline struct freezer *task_freezer(struct task_struct *task) 50 { 51 return css_freezer(task_css(task, freezer_cgrp_id)); 52 } 53 54 static struct freezer *parent_freezer(struct freezer *freezer) 55 { 56 return css_freezer(freezer->css.parent); 57 } 58 59 bool cgroup1_freezing(struct task_struct *task) 60 { 61 bool ret; 62 63 rcu_read_lock(); 64 ret = task_freezer(task)->state & CGROUP_FREEZING; 65 rcu_read_unlock(); 66 67 return ret; 68 } 69 70 static const char *freezer_state_strs(unsigned int state) 71 { 72 if (state & CGROUP_FROZEN) 73 return "FROZEN"; 74 if (state & CGROUP_FREEZING) 75 return "FREEZING"; 76 return "THAWED"; 77 }; 78 79 static struct cgroup_subsys_state * 80 freezer_css_alloc(struct cgroup_subsys_state *parent_css) 81 { 82 struct freezer *freezer; 83 84 freezer = kzalloc_obj(struct freezer); 85 if (!freezer) 86 return ERR_PTR(-ENOMEM); 87 88 return &freezer->css; 89 } 90 91 /** 92 * freezer_css_online - commit creation of a freezer css 93 * @css: css being created 94 * 95 * We're committing to creation of @css. Mark it online and inherit 96 * parent's freezing state while holding cpus read lock and freezer_mutex. 97 */ 98 static int freezer_css_online(struct cgroup_subsys_state *css) 99 { 100 struct freezer *freezer = css_freezer(css); 101 struct freezer *parent = parent_freezer(freezer); 102 103 cpus_read_lock(); 104 mutex_lock(&freezer_mutex); 105 106 freezer->state |= CGROUP_FREEZER_ONLINE; 107 108 if (parent && (parent->state & CGROUP_FREEZING)) { 109 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN; 110 static_branch_inc_cpuslocked(&freezer_active); 111 } 112 113 mutex_unlock(&freezer_mutex); 114 cpus_read_unlock(); 115 return 0; 116 } 117 118 /** 119 * freezer_css_offline - initiate destruction of a freezer css 120 * @css: css being destroyed 121 * 122 * @css is going away. Mark it dead and decrement freezer_active if 123 * it was holding one. 124 */ 125 static void freezer_css_offline(struct cgroup_subsys_state *css) 126 { 127 struct freezer *freezer = css_freezer(css); 128 129 cpus_read_lock(); 130 mutex_lock(&freezer_mutex); 131 132 if (freezer->state & CGROUP_FREEZING) 133 static_branch_dec_cpuslocked(&freezer_active); 134 135 freezer->state = 0; 136 137 mutex_unlock(&freezer_mutex); 138 cpus_read_unlock(); 139 } 140 141 static void freezer_css_free(struct cgroup_subsys_state *css) 142 { 143 kfree(css_freezer(css)); 144 } 145 146 /* 147 * Tasks can be migrated into a different freezer anytime regardless of its 148 * current state. freezer_attach() is responsible for making new tasks 149 * conform to the current state. 150 * 151 * Freezer state changes and task migration are synchronized via 152 * @freezer->lock. freezer_attach() makes the new tasks conform to the 153 * current state and all following state changes can see the new tasks. 154 */ 155 static void freezer_attach(struct cgroup_taskset *tset) 156 { 157 struct task_struct *task; 158 struct cgroup_subsys_state *new_css; 159 160 mutex_lock(&freezer_mutex); 161 162 /* 163 * Make the new tasks conform to the current state of @new_css. 164 * For simplicity, when migrating any task to a FROZEN cgroup, we 165 * revert it to FREEZING and let update_if_frozen() determine the 166 * correct state later. 167 * 168 * Tasks in @tset are on @new_css but may not conform to its 169 * current state before executing the following - !frozen tasks may 170 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one. 171 */ 172 cgroup_taskset_for_each(task, new_css, tset) { 173 struct freezer *freezer = css_freezer(new_css); 174 175 if (!(freezer->state & CGROUP_FREEZING)) { 176 __thaw_task(task); 177 } else { 178 /* clear FROZEN and propagate upwards */ 179 while (freezer && (freezer->state & CGROUP_FROZEN)) { 180 freezer->state &= ~CGROUP_FROZEN; 181 freezer = parent_freezer(freezer); 182 } 183 freeze_task(task); 184 } 185 } 186 187 mutex_unlock(&freezer_mutex); 188 } 189 190 /** 191 * freezer_fork - cgroup post fork callback 192 * @task: a task which has just been forked 193 * 194 * @task has just been created and should conform to the current state of 195 * the cgroup_freezer it belongs to. This function may race against 196 * freezer_attach(). Losing to freezer_attach() means that we don't have 197 * to do anything as freezer_attach() will put @task into the appropriate 198 * state. 199 */ 200 static void freezer_fork(struct task_struct *task) 201 { 202 struct freezer *freezer; 203 204 /* 205 * The root cgroup is non-freezable, so we can skip locking the 206 * freezer. This is safe regardless of race with task migration. 207 * If we didn't race or won, skipping is obviously the right thing 208 * to do. If we lost and root is the new cgroup, noop is still the 209 * right thing to do. 210 */ 211 if (task_css_is_root(task, freezer_cgrp_id)) 212 return; 213 214 mutex_lock(&freezer_mutex); 215 rcu_read_lock(); 216 217 freezer = task_freezer(task); 218 if (freezer->state & CGROUP_FREEZING) 219 freeze_task(task); 220 221 rcu_read_unlock(); 222 mutex_unlock(&freezer_mutex); 223 } 224 225 /** 226 * update_if_frozen - update whether a cgroup finished freezing 227 * @css: css of interest 228 * 229 * Once FREEZING is initiated, transition to FROZEN is lazily updated by 230 * calling this function. If the current state is FREEZING but not FROZEN, 231 * this function checks whether all tasks of this cgroup and the descendant 232 * cgroups finished freezing and, if so, sets FROZEN. 233 * 234 * The caller is responsible for grabbing RCU read lock and calling 235 * update_if_frozen() on all descendants prior to invoking this function. 236 * 237 * Task states and freezer state might disagree while tasks are being 238 * migrated into or out of @css, so we can't verify task states against 239 * @freezer state here. See freezer_attach() for details. 240 */ 241 static void update_if_frozen(struct cgroup_subsys_state *css) 242 { 243 struct freezer *freezer = css_freezer(css); 244 struct cgroup_subsys_state *pos; 245 struct css_task_iter it; 246 struct task_struct *task; 247 248 lockdep_assert_held(&freezer_mutex); 249 250 if (!(freezer->state & CGROUP_FREEZING) || 251 (freezer->state & CGROUP_FROZEN)) 252 return; 253 254 /* are all (live) children frozen? */ 255 rcu_read_lock(); 256 css_for_each_child(pos, css) { 257 struct freezer *child = css_freezer(pos); 258 259 if ((child->state & CGROUP_FREEZER_ONLINE) && 260 !(child->state & CGROUP_FROZEN)) { 261 rcu_read_unlock(); 262 return; 263 } 264 } 265 rcu_read_unlock(); 266 267 /* are all tasks frozen? */ 268 css_task_iter_start(css, CSS_TASK_ITER_WITH_DEAD, &it); 269 270 while ((task = css_task_iter_next(&it))) { 271 if (freezing(task) && !frozen(task)) 272 goto out_iter_end; 273 } 274 275 freezer->state |= CGROUP_FROZEN; 276 out_iter_end: 277 css_task_iter_end(&it); 278 } 279 280 static int freezer_read(struct seq_file *m, void *v) 281 { 282 struct cgroup_subsys_state *css = seq_css(m), *pos; 283 284 mutex_lock(&freezer_mutex); 285 rcu_read_lock(); 286 287 /* update states bottom-up */ 288 css_for_each_descendant_post(pos, css) { 289 if (!css_tryget_online(pos)) 290 continue; 291 rcu_read_unlock(); 292 293 update_if_frozen(pos); 294 295 rcu_read_lock(); 296 css_put(pos); 297 } 298 299 rcu_read_unlock(); 300 mutex_unlock(&freezer_mutex); 301 302 seq_puts(m, freezer_state_strs(css_freezer(css)->state)); 303 seq_putc(m, '\n'); 304 return 0; 305 } 306 307 static void freeze_cgroup(struct freezer *freezer) 308 { 309 struct css_task_iter it; 310 struct task_struct *task; 311 312 css_task_iter_start(&freezer->css, CSS_TASK_ITER_WITH_DEAD, &it); 313 while ((task = css_task_iter_next(&it))) 314 freeze_task(task); 315 css_task_iter_end(&it); 316 } 317 318 static void unfreeze_cgroup(struct freezer *freezer) 319 { 320 struct css_task_iter it; 321 struct task_struct *task; 322 323 css_task_iter_start(&freezer->css, CSS_TASK_ITER_WITH_DEAD, &it); 324 while ((task = css_task_iter_next(&it))) 325 __thaw_task(task); 326 css_task_iter_end(&it); 327 } 328 329 /** 330 * freezer_apply_state - apply state change to a single cgroup_freezer 331 * @freezer: freezer to apply state change to 332 * @freeze: whether to freeze or unfreeze 333 * @state: CGROUP_FREEZING_* flag to set or clear 334 * 335 * Set or clear @state on @cgroup according to @freeze, and perform 336 * freezing or thawing as necessary. 337 */ 338 static void freezer_apply_state(struct freezer *freezer, bool freeze, 339 unsigned int state) 340 { 341 /* also synchronizes against task migration, see freezer_attach() */ 342 lockdep_assert_held(&freezer_mutex); 343 344 if (!(freezer->state & CGROUP_FREEZER_ONLINE)) 345 return; 346 347 if (freeze) { 348 if (!(freezer->state & CGROUP_FREEZING)) 349 static_branch_inc_cpuslocked(&freezer_active); 350 freezer->state |= state; 351 freeze_cgroup(freezer); 352 } else { 353 bool was_freezing = freezer->state & CGROUP_FREEZING; 354 355 freezer->state &= ~state; 356 357 if (!(freezer->state & CGROUP_FREEZING)) { 358 freezer->state &= ~CGROUP_FROZEN; 359 if (was_freezing) 360 static_branch_dec_cpuslocked(&freezer_active); 361 unfreeze_cgroup(freezer); 362 } 363 } 364 } 365 366 /** 367 * freezer_change_state - change the freezing state of a cgroup_freezer 368 * @freezer: freezer of interest 369 * @freeze: whether to freeze or thaw 370 * 371 * Freeze or thaw @freezer according to @freeze. The operations are 372 * recursive - all descendants of @freezer will be affected. 373 */ 374 static void freezer_change_state(struct freezer *freezer, bool freeze) 375 { 376 struct cgroup_subsys_state *pos; 377 378 cpus_read_lock(); 379 /* 380 * Update all its descendants in pre-order traversal. Each 381 * descendant will try to inherit its parent's FREEZING state as 382 * CGROUP_FREEZING_PARENT. 383 */ 384 mutex_lock(&freezer_mutex); 385 386 /* 387 * A dead cgroup must not be frozen. 388 * 389 * rmdir thaws the cgroup from freezer_css_killed(), but freezer.state 390 * stays writable until rmdir removes it a moment later. A FROZEN write 391 * in that gap would freeze the cgroup again, and nothing could thaw it 392 * after that. 393 * 394 * A freezer.state write doesn't take cgroup_mutex, but testing 395 * cgroup_is_dead() without it is safe. rmdir marks the cgroup dead 396 * before the thaw, so a write that takes freezer_mutex after the thaw 397 * sees it dead. One that takes it before the thaw may still see the 398 * cgroup alive, but the thaw then undoes its freeze. 399 */ 400 if (freeze && data_race(cgroup_is_dead(freezer->css.cgroup))) 401 goto out_unlock; 402 403 rcu_read_lock(); 404 css_for_each_descendant_pre(pos, &freezer->css) { 405 struct freezer *pos_f = css_freezer(pos); 406 struct freezer *parent = parent_freezer(pos_f); 407 408 if (!css_tryget_online(pos)) 409 continue; 410 rcu_read_unlock(); 411 412 if (pos_f == freezer) 413 freezer_apply_state(pos_f, freeze, 414 CGROUP_FREEZING_SELF); 415 else 416 freezer_apply_state(pos_f, 417 parent->state & CGROUP_FREEZING, 418 CGROUP_FREEZING_PARENT); 419 420 rcu_read_lock(); 421 css_put(pos); 422 } 423 rcu_read_unlock(); 424 out_unlock: 425 mutex_unlock(&freezer_mutex); 426 cpus_read_unlock(); 427 } 428 429 /** 430 * freezer_css_killed - Thaw a freezer cgroup being removed 431 * @css: css being killed 432 * 433 * Exiting tasks hidden from cgroup.procs can still be in the cgroup when it is 434 * removed, and a frozen one has nothing left to thaw it once freezer.state is 435 * gone. Thaw the cgroup as a THAWED write to it would. A frozen ancestor keeps 436 * the tasks frozen through CGROUP_FREEZING_PARENT until the ancestor is thawed. 437 */ 438 static void freezer_css_killed(struct cgroup_subsys_state *css) 439 { 440 freezer_change_state(css_freezer(css), false); 441 } 442 443 static ssize_t freezer_write(struct kernfs_open_file *of, 444 char *buf, size_t nbytes, loff_t off) 445 { 446 bool freeze; 447 448 buf = strstrip(buf); 449 450 if (strcmp(buf, freezer_state_strs(0)) == 0) 451 freeze = false; 452 else if (strcmp(buf, freezer_state_strs(CGROUP_FROZEN)) == 0) { 453 pr_info_once("Freezing with imperfect legacy cgroup freezer. " 454 "See cgroup.freeze of cgroup v2\n"); 455 freeze = true; 456 } else 457 return -EINVAL; 458 459 freezer_change_state(css_freezer(of_css(of)), freeze); 460 return nbytes; 461 } 462 463 static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css, 464 struct cftype *cft) 465 { 466 struct freezer *freezer = css_freezer(css); 467 468 return (bool)(freezer->state & CGROUP_FREEZING_SELF); 469 } 470 471 static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css, 472 struct cftype *cft) 473 { 474 struct freezer *freezer = css_freezer(css); 475 476 return (bool)(freezer->state & CGROUP_FREEZING_PARENT); 477 } 478 479 static struct cftype files[] = { 480 { 481 .name = "state", 482 .flags = CFTYPE_NOT_ON_ROOT, 483 .seq_show = freezer_read, 484 .write = freezer_write, 485 }, 486 { 487 .name = "self_freezing", 488 .flags = CFTYPE_NOT_ON_ROOT, 489 .read_u64 = freezer_self_freezing_read, 490 }, 491 { 492 .name = "parent_freezing", 493 .flags = CFTYPE_NOT_ON_ROOT, 494 .read_u64 = freezer_parent_freezing_read, 495 }, 496 { } /* terminate */ 497 }; 498 499 struct cgroup_subsys freezer_cgrp_subsys = { 500 .css_alloc = freezer_css_alloc, 501 .css_online = freezer_css_online, 502 .css_killed = freezer_css_killed, 503 .css_offline = freezer_css_offline, 504 .css_free = freezer_css_free, 505 .attach = freezer_attach, 506 .fork = freezer_fork, 507 .legacy_cftypes = files, 508 }; 509