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