| 1 |
|
| 2 | #ifndef _LINUX_CGROUP_H
|
| 3 | #define _LINUX_CGROUP_H
|
| 4 | |
| 5 | * cgroup interface
|
| 6 | *
|
| 7 | * Copyright (C) 2003 BULL SA
|
| 8 | * Copyright (C) 2004-2006 Silicon Graphics, Inc.
|
| 9 | *
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/sched.h>
|
| 13 | #include <linux/nodemask.h>
|
| 14 | #include <linux/list.h>
|
| 15 | #include <linux/rculist.h>
|
| 16 | #include <linux/cgroupstats.h>
|
| 17 | #include <linux/fs.h>
|
| 18 | #include <linux/seq_file.h>
|
| 19 | #include <linux/kernfs.h>
|
| 20 | #include <linux/jump_label.h>
|
| 21 | #include <linux/types.h>
|
| 22 | #include <linux/notifier.h>
|
| 23 | #include <linux/ns_common.h>
|
| 24 | #include <linux/nsproxy.h>
|
| 25 | #include <linux/user_namespace.h>
|
| 26 | #include <linux/refcount.h>
|
| 27 | #include <linux/kernel_stat.h>
|
| 28 |
|
| 29 | #include <linux/cgroup-defs.h>
|
| 30 | #include <linux/cgroup_namespace.h>
|
| 31 |
|
| 32 | struct kernel_clone_args;
|
| 33 |
|
| 34 | |
| 35 | * All weight knobs on the default hierarchy should use the following min,
|
| 36 | * default and max values. The default value is the logarithmic center of
|
| 37 | * MIN and MAX and allows 100x to be expressed in both directions.
|
| 38 |
|
| 39 | #define CGROUP_WEIGHT_MIN 1
|
| 40 | #define CGROUP_WEIGHT_DFL 100
|
| 41 | #define CGROUP_WEIGHT_MAX 10000
|
| 42 |
|
| 43 | #ifdef CONFIG_CGROUPS
|
| 44 |
|
| 45 | enum css_task_iter_flags {
|
| 46 | CSS_TASK_ITER_PROCS = (1U << 0),
|
| 47 | CSS_TASK_ITER_THREADED = (1U << 1),
|
| 48 | CSS_TASK_ITER_SKIPPED = (1U << 16),
|
| 49 | };
|
| 50 |
|
| 51 |
|
| 52 | struct css_task_iter {
|
| 53 | struct cgroup_subsys *ss;
|
| 54 | unsigned int flags;
|
| 55 |
|
| 56 | struct list_head *cset_pos;
|
| 57 | struct list_head *cset_head;
|
| 58 |
|
| 59 | struct list_head *tcset_pos;
|
| 60 | struct list_head *tcset_head;
|
| 61 |
|
| 62 | struct list_head *task_pos;
|
| 63 |
|
| 64 | struct list_head *cur_tasks_head;
|
| 65 | struct css_set *cur_cset;
|
| 66 | struct css_set *cur_dcset;
|
| 67 | struct task_struct *cur_task;
|
| 68 | struct list_head iters_node;
|
| 69 | };
|
| 70 |
|
| 71 | enum cgroup_lifetime_events {
|
| 72 | CGROUP_LIFETIME_ONLINE,
|
| 73 | CGROUP_LIFETIME_OFFLINE,
|
| 74 | };
|
| 75 |
|
| 76 | extern struct file_system_type cgroup_fs_type;
|
| 77 | extern struct cgroup_root cgrp_dfl_root;
|
| 78 | extern struct css_set init_css_set;
|
| 79 | extern spinlock_t css_set_lock;
|
| 80 | extern struct blocking_notifier_head cgroup_lifetime_notifier;
|
| 81 |
|
| 82 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
|
| 83 | #include <linux/cgroup_subsys.h>
|
| 84 | #undef SUBSYS
|
| 85 |
|
| 86 | #define SUBSYS(_x) \
|
| 87 | extern struct static_key_true _x ## _cgrp_subsys_enabled_key; \
|
| 88 | extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
|
| 89 | #include <linux/cgroup_subsys.h>
|
| 90 | #undef SUBSYS
|
| 91 |
|
| 92 | |
| 93 | * cgroup_subsys_enabled - fast test on whether a subsys is enabled
|
| 94 | * @ss: subsystem in question
|
| 95 |
|
| 96 | #define cgroup_subsys_enabled(ss) \
|
| 97 | static_branch_likely(&ss ## _enabled_key)
|
| 98 |
|
| 99 | |
| 100 | * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
|
| 101 | * @ss: subsystem in question
|
| 102 |
|
| 103 | #define cgroup_subsys_on_dfl(ss) \
|
| 104 | static_branch_likely(&ss ## _on_dfl_key)
|
| 105 |
|
| 106 | bool css_has_online_children(struct cgroup_subsys_state *css);
|
| 107 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
|
| 108 | struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
|
| 109 | struct cgroup_subsys *ss);
|
| 110 | struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
|
| 111 | struct cgroup_subsys *ss);
|
| 112 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
|
| 113 | struct cgroup_subsys *ss);
|
| 114 |
|
| 115 | struct cgroup *cgroup_get_from_path(const char *path);
|
| 116 | struct cgroup *cgroup_get_from_fd(int fd);
|
| 117 | struct cgroup *cgroup_v1v2_get_from_fd(int fd);
|
| 118 |
|
| 119 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
|
| 120 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
|
| 121 |
|
| 122 | int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
|
| 123 | int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
|
| 124 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
|
| 125 | int cgroup_rm_cftypes(struct cftype *cfts);
|
| 126 | void cgroup_file_notify(struct cgroup_file *cfile);
|
| 127 | void cgroup_file_show(struct cgroup_file *cfile, bool show);
|
| 128 |
|
| 129 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
|
| 130 | int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
|
| 131 | struct pid *pid, struct task_struct *tsk);
|
| 132 |
|
| 133 | void cgroup_fork(struct task_struct *p);
|
| 134 | extern int cgroup_can_fork(struct task_struct *p,
|
| 135 | struct kernel_clone_args *kargs);
|
| 136 | extern void cgroup_cancel_fork(struct task_struct *p,
|
| 137 | struct kernel_clone_args *kargs);
|
| 138 | extern void cgroup_post_fork(struct task_struct *p,
|
| 139 | struct kernel_clone_args *kargs);
|
| 140 | void cgroup_exit(struct task_struct *p);
|
| 141 | void cgroup_release(struct task_struct *p);
|
| 142 | void cgroup_free(struct task_struct *p);
|
| 143 |
|
| 144 | int cgroup_init_early(void);
|
| 145 | int cgroup_init(void);
|
| 146 |
|
| 147 | int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
|
| 148 |
|
| 149 | |
| 150 | * Iteration helpers and macros.
|
| 151 |
|
| 152 |
|
| 153 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
|
| 154 | struct cgroup_subsys_state *parent);
|
| 155 | struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
|
| 156 | struct cgroup_subsys_state *css);
|
| 157 | struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
|
| 158 | struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
|
| 159 | struct cgroup_subsys_state *css);
|
| 160 |
|
| 161 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
|
| 162 | struct cgroup_subsys_state **dst_cssp);
|
| 163 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
|
| 164 | struct cgroup_subsys_state **dst_cssp);
|
| 165 |
|
| 166 | void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
|
| 167 | struct css_task_iter *it);
|
| 168 | struct task_struct *css_task_iter_next(struct css_task_iter *it);
|
| 169 | void css_task_iter_end(struct css_task_iter *it);
|
| 170 |
|
| 171 | |
| 172 | * css_for_each_child - iterate through children of a css
|
| 173 | * @pos: the css * to use as the loop cursor
|
| 174 | * @parent: css whose children to walk
|
| 175 | *
|
| 176 | * Walk @parent's children. Must be called under rcu_read_lock().
|
| 177 | *
|
| 178 | * If a subsystem synchronizes ->css_online() and the start of iteration, a
|
| 179 | * css which finished ->css_online() is guaranteed to be visible in the
|
| 180 | * future iterations and will stay visible until the last reference is put.
|
| 181 | * A css which hasn't finished ->css_online() or already finished
|
| 182 | * ->css_offline() may show up during traversal. It's each subsystem's
|
| 183 | * responsibility to synchronize against on/offlining.
|
| 184 | *
|
| 185 | * It is allowed to temporarily drop RCU read lock during iteration. The
|
| 186 | * caller is responsible for ensuring that @pos remains accessible until
|
| 187 | * the start of the next iteration by, for example, bumping the css refcnt.
|
| 188 |
|
| 189 | #define css_for_each_child(pos, parent) \
|
| 190 | for ((pos) = css_next_child(NULL, (parent)); (pos); \
|
| 191 | (pos) = css_next_child((pos), (parent)))
|
| 192 |
|
| 193 | |
| 194 | * css_for_each_descendant_pre - pre-order walk of a css's descendants
|
| 195 | * @pos: the css * to use as the loop cursor
|
| 196 | * @root: css whose descendants to walk
|
| 197 | *
|
| 198 | * Walk @root's descendants. @root is included in the iteration and the
|
| 199 | * first node to be visited. Must be called under rcu_read_lock().
|
| 200 | *
|
| 201 | * If a subsystem synchronizes ->css_online() and the start of iteration, a
|
| 202 | * css which finished ->css_online() is guaranteed to be visible in the
|
| 203 | * future iterations and will stay visible until the last reference is put.
|
| 204 | * A css which hasn't finished ->css_online() or already finished
|
| 205 | * ->css_offline() may show up during traversal. It's each subsystem's
|
| 206 | * responsibility to synchronize against on/offlining.
|
| 207 | *
|
| 208 | * For example, the following guarantees that a descendant can't escape
|
| 209 | * state updates of its ancestors.
|
| 210 | *
|
| 211 | * my_online(@css)
|
| 212 | * {
|
| 213 | * Lock @css's parent and @css;
|
| 214 | * Inherit state from the parent;
|
| 215 | * Unlock both.
|
| 216 | * }
|
| 217 | *
|
| 218 | * my_update_state(@css)
|
| 219 | * {
|
| 220 | * css_for_each_descendant_pre(@pos, @css) {
|
| 221 | * Lock @pos;
|
| 222 | * if (@pos == @css)
|
| 223 | * Update @css's state;
|
| 224 | * else
|
| 225 | * Verify @pos is alive and inherit state from its parent;
|
| 226 | * Unlock @pos;
|
| 227 | * }
|
| 228 | * }
|
| 229 | *
|
| 230 | * As long as the inheriting step, including checking the parent state, is
|
| 231 | * enclosed inside @pos locking, double-locking the parent isn't necessary
|
| 232 | * while inheriting. The state update to the parent is guaranteed to be
|
| 233 | * visible by walking order and, as long as inheriting operations to the
|
| 234 | * same @pos are atomic to each other, multiple updates racing each other
|
| 235 | * still result in the correct state. It's guaranateed that at least one
|
| 236 | * inheritance happens for any css after the latest update to its parent.
|
| 237 | *
|
| 238 | * If checking parent's state requires locking the parent, each inheriting
|
| 239 | * iteration should lock and unlock both @pos->parent and @pos.
|
| 240 | *
|
| 241 | * Alternatively, a subsystem may choose to use a single global lock to
|
| 242 | * synchronize ->css_online() and ->css_offline() against tree-walking
|
| 243 | * operations.
|
| 244 | *
|
| 245 | * It is allowed to temporarily drop RCU read lock during iteration. The
|
| 246 | * caller is responsible for ensuring that @pos remains accessible until
|
| 247 | * the start of the next iteration by, for example, bumping the css refcnt.
|
| 248 |
|
| 249 | #define css_for_each_descendant_pre(pos, css) \
|
| 250 | for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
|
| 251 | (pos) = css_next_descendant_pre((pos), (css)))
|
| 252 |
|
| 253 | |
| 254 | * css_for_each_descendant_post - post-order walk of a css's descendants
|
| 255 | * @pos: the css * to use as the loop cursor
|
| 256 | * @css: css whose descendants to walk
|
| 257 | *
|
| 258 | * Similar to css_for_each_descendant_pre() but performs post-order
|
| 259 | * traversal instead. @root is included in the iteration and the last
|
| 260 | * node to be visited.
|
| 261 | *
|
| 262 | * If a subsystem synchronizes ->css_online() and the start of iteration, a
|
| 263 | * css which finished ->css_online() is guaranteed to be visible in the
|
| 264 | * future iterations and will stay visible until the last reference is put.
|
| 265 | * A css which hasn't finished ->css_online() or already finished
|
| 266 | * ->css_offline() may show up during traversal. It's each subsystem's
|
| 267 | * responsibility to synchronize against on/offlining.
|
| 268 | *
|
| 269 | * Note that the walk visibility guarantee example described in pre-order
|
| 270 | * walk doesn't apply the same to post-order walks.
|
| 271 |
|
| 272 | #define css_for_each_descendant_post(pos, css) \
|
| 273 | for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
|
| 274 | (pos) = css_next_descendant_post((pos), (css)))
|
| 275 |
|
| 276 | |
| 277 | * cgroup_taskset_for_each - iterate cgroup_taskset
|
| 278 | * @task: the loop cursor
|
| 279 | * @dst_css: the destination css
|
| 280 | * @tset: taskset to iterate
|
| 281 | *
|
| 282 | * @tset may contain multiple tasks and they may belong to multiple
|
| 283 | * processes.
|
| 284 | *
|
| 285 | * On the v2 hierarchy, there may be tasks from multiple processes and they
|
| 286 | * may not share the source or destination csses.
|
| 287 | *
|
| 288 | * On traditional hierarchies, when there are multiple tasks in @tset, if a
|
| 289 | * task of a process is in @tset, all tasks of the process are in @tset.
|
| 290 | * Also, all are guaranteed to share the same source and destination csses.
|
| 291 | *
|
| 292 | * Iteration is not in any specific order.
|
| 293 |
|
| 294 | #define cgroup_taskset_for_each(task, dst_css, tset) \
|
| 295 | for ((task) = cgroup_taskset_first((tset), &(dst_css)); \
|
| 296 | (task); \
|
| 297 | (task) = cgroup_taskset_next((tset), &(dst_css)))
|
| 298 |
|
| 299 | |
| 300 | * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
|
| 301 | * @leader: the loop cursor
|
| 302 | * @dst_css: the destination css
|
| 303 | * @tset: taskset to iterate
|
| 304 | *
|
| 305 | * Iterate threadgroup leaders of @tset. For single-task migrations, @tset
|
| 306 | * may not contain any.
|
| 307 |
|
| 308 | #define cgroup_taskset_for_each_leader(leader, dst_css, tset) \
|
| 309 | for ((leader) = cgroup_taskset_first((tset), &(dst_css)); \
|
| 310 | (leader); \
|
| 311 | (leader) = cgroup_taskset_next((tset), &(dst_css))) \
|
| 312 | if ((leader) != (leader)->group_leader) \
|
| 313 | ; \
|
| 314 | else
|
| 315 |
|
| 316 | |
| 317 | * Inline functions.
|
| 318 |
|
| 319 |
|
| 320 | #ifdef CONFIG_DEBUG_CGROUP_REF
|
| 321 | void css_get(struct cgroup_subsys_state *css);
|
| 322 | void css_get_many(struct cgroup_subsys_state *css, unsigned int n);
|
| 323 | bool css_tryget(struct cgroup_subsys_state *css);
|
| 324 | bool css_tryget_online(struct cgroup_subsys_state *css);
|
| 325 | void css_put(struct cgroup_subsys_state *css);
|
| 326 | void css_put_many(struct cgroup_subsys_state *css, unsigned int n);
|
| 327 | #else
|
| 328 | #define CGROUP_REF_FN_ATTRS static inline
|
| 329 | #define CGROUP_REF_EXPORT(fn)
|
| 330 | #include <linux/cgroup_refcnt.h>
|
| 331 | #endif
|
| 332 |
|
| 333 | static inline u64 cgroup_id(const struct cgroup *cgrp)
|
| 334 | {
|
| 335 | return cgrp->kn->id;
|
| 336 | }
|
| 337 |
|
| 338 | |
| 339 | * css_is_dying - test whether the specified css is dying
|
| 340 | * @css: target css
|
| 341 | *
|
| 342 | * Test whether @css is in the process of offlining or already offline. In
|
| 343 | * most cases, ->css_online() and ->css_offline() callbacks should be
|
| 344 | * enough; however, the actual offline operations are RCU delayed and this
|
| 345 | * test returns %true also when @css is scheduled to be offlined.
|
| 346 | *
|
| 347 | * This is useful, for example, when the use case requires synchronous
|
| 348 | * behavior with respect to cgroup removal. cgroup removal schedules css
|
| 349 | * offlining but the css can seem alive while the operation is being
|
| 350 | * delayed. If the delay affects user visible semantics, this test can be
|
| 351 | * used to resolve the situation.
|
| 352 |
|
| 353 | static inline bool css_is_dying(struct cgroup_subsys_state *css)
|
| 354 | {
|
| 355 | return css->flags & CSS_DYING;
|
| 356 | }
|
| 357 |
|
| 358 | static inline bool css_is_online(struct cgroup_subsys_state *css)
|
| 359 | {
|
| 360 | return css->flags & CSS_ONLINE;
|
| 361 | }
|
| 362 |
|
| 363 | static inline bool css_is_self(struct cgroup_subsys_state *css)
|
| 364 | {
|
| 365 | if (css == &css->cgroup->self) {
|
| 366 |
|
| 367 | WARN_ON(css->ss != NULL);
|
| 368 | return true;
|
| 369 | }
|
| 370 |
|
| 371 | return false;
|
| 372 | }
|
| 373 |
|
| 374 | static inline void cgroup_get(struct cgroup *cgrp)
|
| 375 | {
|
| 376 | css_get(&cgrp->self);
|
| 377 | }
|
| 378 |
|
| 379 | static inline bool cgroup_tryget(struct cgroup *cgrp)
|
| 380 | {
|
| 381 | return css_tryget(&cgrp->self);
|
| 382 | }
|
| 383 |
|
| 384 | static inline void cgroup_put(struct cgroup *cgrp)
|
| 385 | {
|
| 386 | css_put(&cgrp->self);
|
| 387 | }
|
| 388 |
|
| 389 | extern struct mutex cgroup_mutex;
|
| 390 |
|
| 391 | static inline void cgroup_lock(void)
|
| 392 | {
|
| 393 | mutex_lock(&cgroup_mutex);
|
| 394 | }
|
| 395 |
|
| 396 | static inline void cgroup_unlock(void)
|
| 397 | {
|
| 398 | mutex_unlock(&cgroup_mutex);
|
| 399 | }
|
| 400 |
|
| 401 | |
| 402 | * task_css_set_check - obtain a task's css_set with extra access conditions
|
| 403 | * @task: the task to obtain css_set for
|
| 404 | * @__c: extra condition expression to be passed to rcu_dereference_check()
|
| 405 | *
|
| 406 | * A task's css_set is RCU protected, initialized and exited while holding
|
| 407 | * task_lock(), and can only be modified while holding both cgroup_mutex
|
| 408 | * and task_lock() while the task is alive. This macro verifies that the
|
| 409 | * caller is inside proper critical section and returns @task's css_set.
|
| 410 | *
|
| 411 | * The caller can also specify additional allowed conditions via @__c, such
|
| 412 | * as locks used during the cgroup_subsys::attach() methods.
|
| 413 |
|
| 414 | #ifdef CONFIG_PROVE_RCU
|
| 415 | #define task_css_set_check(task, __c) \
|
| 416 | rcu_dereference_check((task)->cgroups, \
|
| 417 | rcu_read_lock_sched_held() || \
|
| 418 | lockdep_is_held(&cgroup_mutex) || \
|
| 419 | lockdep_is_held(&css_set_lock) || \
|
| 420 | ((task)->flags & PF_EXITING) || (__c))
|
| 421 | #else
|
| 422 | #define task_css_set_check(task, __c) \
|
| 423 | rcu_dereference((task)->cgroups)
|
| 424 | #endif
|
| 425 |
|
| 426 | |
| 427 | * task_css_check - obtain css for (task, subsys) w/ extra access conds
|
| 428 | * @task: the target task
|
| 429 | * @subsys_id: the target subsystem ID
|
| 430 | * @__c: extra condition expression to be passed to rcu_dereference_check()
|
| 431 | *
|
| 432 | * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
|
| 433 | * synchronization rules are the same as task_css_set_check().
|
| 434 |
|
| 435 | #define task_css_check(task, subsys_id, __c) \
|
| 436 | task_css_set_check((task), (__c))->subsys[(subsys_id)]
|
| 437 |
|
| 438 | |
| 439 | * task_css_set - obtain a task's css_set
|
| 440 | * @task: the task to obtain css_set for
|
| 441 | *
|
| 442 | * See task_css_set_check().
|
| 443 |
|
| 444 | static inline struct css_set *task_css_set(struct task_struct *task)
|
| 445 | {
|
| 446 | return task_css_set_check(task, false);
|
| 447 | }
|
| 448 |
|
| 449 | |
| 450 | * task_css - obtain css for (task, subsys)
|
| 451 | * @task: the target task
|
| 452 | * @subsys_id: the target subsystem ID
|
| 453 | *
|
| 454 | * See task_css_check().
|
| 455 |
|
| 456 | static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
|
| 457 | int subsys_id)
|
| 458 | {
|
| 459 | return task_css_check(task, subsys_id, false);
|
| 460 | }
|
| 461 |
|
| 462 | |
| 463 | * task_get_css - find and get the css for (task, subsys)
|
| 464 | * @task: the target task
|
| 465 | * @subsys_id: the target subsystem ID
|
| 466 | *
|
| 467 | * Find the css for the (@task, @subsys_id) combination, increment a
|
| 468 | * reference on and return it. This function is guaranteed to return a
|
| 469 | * valid css. The returned css may already have been offlined.
|
| 470 |
|
| 471 | static inline struct cgroup_subsys_state *
|
| 472 | task_get_css(struct task_struct *task, int subsys_id)
|
| 473 | {
|
| 474 | struct cgroup_subsys_state *css;
|
| 475 |
|
| 476 | rcu_read_lock();
|
| 477 | while (true) {
|
| 478 | css = task_css(task, subsys_id);
|
| 479 | |
| 480 | * Can't use css_tryget_online() here. A task which has
|
| 481 | * PF_EXITING set may stay associated with an offline css.
|
| 482 | * If such task calls this function, css_tryget_online()
|
| 483 | * will keep failing.
|
| 484 |
|
| 485 | if (likely(css_tryget(css)))
|
| 486 | break;
|
| 487 | cpu_relax();
|
| 488 | }
|
| 489 | rcu_read_unlock();
|
| 490 | return css;
|
| 491 | }
|
| 492 |
|
| 493 | |
| 494 | * task_css_is_root - test whether a task belongs to the root css
|
| 495 | * @task: the target task
|
| 496 | * @subsys_id: the target subsystem ID
|
| 497 | *
|
| 498 | * Test whether @task belongs to the root css on the specified subsystem.
|
| 499 | * May be invoked in any context.
|
| 500 |
|
| 501 | static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
|
| 502 | {
|
| 503 | return task_css_check(task, subsys_id, true) ==
|
| 504 | init_css_set.subsys[subsys_id];
|
| 505 | }
|
| 506 |
|
| 507 | static inline struct cgroup *task_cgroup(struct task_struct *task,
|
| 508 | int subsys_id)
|
| 509 | {
|
| 510 | return task_css(task, subsys_id)->cgroup;
|
| 511 | }
|
| 512 |
|
| 513 | static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
|
| 514 | {
|
| 515 | return task_css_set(task)->dfl_cgrp;
|
| 516 | }
|
| 517 |
|
| 518 | static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
|
| 519 | {
|
| 520 | struct cgroup_subsys_state *parent_css = cgrp->self.parent;
|
| 521 |
|
| 522 | if (parent_css)
|
| 523 | return container_of(parent_css, struct cgroup, self);
|
| 524 | return NULL;
|
| 525 | }
|
| 526 |
|
| 527 | |
| 528 | * cgroup_is_descendant - test ancestry
|
| 529 | * @cgrp: the cgroup to be tested
|
| 530 | * @ancestor: possible ancestor of @cgrp
|
| 531 | *
|
| 532 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true
|
| 533 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
|
| 534 | * and @ancestor are accessible.
|
| 535 |
|
| 536 | static inline bool cgroup_is_descendant(struct cgroup *cgrp,
|
| 537 | struct cgroup *ancestor)
|
| 538 | {
|
| 539 | if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
|
| 540 | return false;
|
| 541 | return cgrp->ancestors[ancestor->level] == ancestor;
|
| 542 | }
|
| 543 |
|
| 544 | |
| 545 | * cgroup_ancestor - find ancestor of cgroup
|
| 546 | * @cgrp: cgroup to find ancestor of
|
| 547 | * @ancestor_level: level of ancestor to find starting from root
|
| 548 | *
|
| 549 | * Find ancestor of cgroup at specified level starting from root if it exists
|
| 550 | * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
|
| 551 | * @ancestor_level.
|
| 552 | *
|
| 553 | * This function is safe to call as long as @cgrp is accessible.
|
| 554 |
|
| 555 | static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
|
| 556 | int ancestor_level)
|
| 557 | {
|
| 558 | if (ancestor_level < 0 || ancestor_level > cgrp->level)
|
| 559 | return NULL;
|
| 560 | return cgrp->ancestors[ancestor_level];
|
| 561 | }
|
| 562 |
|
| 563 | |
| 564 | * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
|
| 565 | * @task: the task to be tested
|
| 566 | * @ancestor: possible ancestor of @task's cgroup
|
| 567 | *
|
| 568 | * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
|
| 569 | * It follows all the same rules as cgroup_is_descendant, and only applies
|
| 570 | * to the default hierarchy.
|
| 571 |
|
| 572 | static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
|
| 573 | struct cgroup *ancestor)
|
| 574 | {
|
| 575 | struct css_set *cset = task_css_set(task);
|
| 576 |
|
| 577 | return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
|
| 578 | }
|
| 579 |
|
| 580 |
|
| 581 | static inline bool cgroup_is_populated(struct cgroup *cgrp)
|
| 582 | {
|
| 583 | return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
|
| 584 | cgrp->nr_populated_threaded_children;
|
| 585 | }
|
| 586 |
|
| 587 |
|
| 588 | static inline ino_t cgroup_ino(struct cgroup *cgrp)
|
| 589 | {
|
| 590 | return kernfs_ino(cgrp->kn);
|
| 591 | }
|
| 592 |
|
| 593 |
|
| 594 | static inline struct cftype *of_cft(struct kernfs_open_file *of)
|
| 595 | {
|
| 596 | return of->kn->priv;
|
| 597 | }
|
| 598 |
|
| 599 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
|
| 600 |
|
| 601 |
|
| 602 | static inline struct cftype *seq_cft(struct seq_file *seq)
|
| 603 | {
|
| 604 | return of_cft(seq->private);
|
| 605 | }
|
| 606 |
|
| 607 | static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
|
| 608 | {
|
| 609 | return of_css(seq->private);
|
| 610 | }
|
| 611 |
|
| 612 | |
| 613 | * Name / path handling functions. All are thin wrappers around the kernfs
|
| 614 | * counterparts and can be called under any context.
|
| 615 |
|
| 616 |
|
| 617 | static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
|
| 618 | {
|
| 619 | return kernfs_name(cgrp->kn, buf, buflen);
|
| 620 | }
|
| 621 |
|
| 622 | static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
|
| 623 | {
|
| 624 | return kernfs_path(cgrp->kn, buf, buflen);
|
| 625 | }
|
| 626 |
|
| 627 | static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
|
| 628 | {
|
| 629 | pr_cont_kernfs_name(cgrp->kn);
|
| 630 | }
|
| 631 |
|
| 632 | static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
|
| 633 | {
|
| 634 | pr_cont_kernfs_path(cgrp->kn);
|
| 635 | }
|
| 636 |
|
| 637 | bool cgroup_psi_enabled(void);
|
| 638 |
|
| 639 | static inline void cgroup_init_kthreadd(void)
|
| 640 | {
|
| 641 | |
| 642 | * kthreadd is inherited by all kthreads, keep it in the root so
|
| 643 | * that the new kthreads are guaranteed to stay in the root until
|
| 644 | * initialization is finished.
|
| 645 |
|
| 646 | current->no_cgroup_migration = 1;
|
| 647 | }
|
| 648 |
|
| 649 | static inline void cgroup_kthread_ready(void)
|
| 650 | {
|
| 651 | |
| 652 | * This kthread finished initialization. The creator should have
|
| 653 | * set PF_NO_SETAFFINITY if this kthread should stay in the root.
|
| 654 |
|
| 655 | current->no_cgroup_migration = 0;
|
| 656 | }
|
| 657 |
|
| 658 | void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
|
| 659 | struct cgroup *__cgroup_get_from_id(u64 id);
|
| 660 | struct cgroup *cgroup_get_from_id(u64 id);
|
| 661 | #else
|
| 662 |
|
| 663 | struct cgroup_subsys_state;
|
| 664 | struct cgroup;
|
| 665 |
|
| 666 | static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
|
| 667 | static inline void css_get(struct cgroup_subsys_state *css) {}
|
| 668 | static inline void css_put(struct cgroup_subsys_state *css) {}
|
| 669 | static inline void cgroup_lock(void) {}
|
| 670 | static inline void cgroup_unlock(void) {}
|
| 671 | static inline int cgroup_attach_task_all(struct task_struct *from,
|
| 672 | struct task_struct *t) { return 0; }
|
| 673 | static inline int cgroupstats_build(struct cgroupstats *stats,
|
| 674 | struct dentry *dentry) { return -EINVAL; }
|
| 675 |
|
| 676 | static inline void cgroup_fork(struct task_struct *p) {}
|
| 677 | static inline int cgroup_can_fork(struct task_struct *p,
|
| 678 | struct kernel_clone_args *kargs) { return 0; }
|
| 679 | static inline void cgroup_cancel_fork(struct task_struct *p,
|
| 680 | struct kernel_clone_args *kargs) {}
|
| 681 | static inline void cgroup_post_fork(struct task_struct *p,
|
| 682 | struct kernel_clone_args *kargs) {}
|
| 683 | static inline void cgroup_exit(struct task_struct *p) {}
|
| 684 | static inline void cgroup_release(struct task_struct *p) {}
|
| 685 | static inline void cgroup_free(struct task_struct *p) {}
|
| 686 |
|
| 687 | static inline int cgroup_init_early(void) { return 0; }
|
| 688 | static inline int cgroup_init(void) { return 0; }
|
| 689 | static inline void cgroup_init_kthreadd(void) {}
|
| 690 | static inline void cgroup_kthread_ready(void) {}
|
| 691 |
|
| 692 | static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
|
| 693 | {
|
| 694 | return NULL;
|
| 695 | }
|
| 696 |
|
| 697 | static inline bool cgroup_psi_enabled(void)
|
| 698 | {
|
| 699 | return false;
|
| 700 | }
|
| 701 |
|
| 702 | static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
|
| 703 | struct cgroup *ancestor)
|
| 704 | {
|
| 705 | return true;
|
| 706 | }
|
| 707 |
|
| 708 | static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
|
| 709 | {}
|
| 710 | #endif
|
| 711 |
|
| 712 | #ifdef CONFIG_CGROUPS
|
| 713 | |
| 714 | * cgroup scalable recursive statistics.
|
| 715 |
|
| 716 | void css_rstat_updated(struct cgroup_subsys_state *css, int cpu);
|
| 717 | void css_rstat_flush(struct cgroup_subsys_state *css);
|
| 718 |
|
| 719 | |
| 720 | * Basic resource stats.
|
| 721 |
|
| 722 | #ifdef CONFIG_CGROUP_CPUACCT
|
| 723 | void cpuacct_charge(struct task_struct *tsk, u64 cputime);
|
| 724 | void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
|
| 725 | #else
|
| 726 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
|
| 727 | static inline void cpuacct_account_field(struct task_struct *tsk, int index,
|
| 728 | u64 val) {}
|
| 729 | #endif
|
| 730 |
|
| 731 | void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
|
| 732 | void __cgroup_account_cputime_field(struct cgroup *cgrp,
|
| 733 | enum cpu_usage_stat index, u64 delta_exec);
|
| 734 |
|
| 735 | static inline void cgroup_account_cputime(struct task_struct *task,
|
| 736 | u64 delta_exec)
|
| 737 | {
|
| 738 | struct cgroup *cgrp;
|
| 739 |
|
| 740 | cpuacct_charge(task, delta_exec);
|
| 741 |
|
| 742 | cgrp = task_dfl_cgroup(task);
|
| 743 | if (cgroup_parent(cgrp))
|
| 744 | __cgroup_account_cputime(cgrp, delta_exec);
|
| 745 | }
|
| 746 |
|
| 747 | static inline void cgroup_account_cputime_field(struct task_struct *task,
|
| 748 | enum cpu_usage_stat index,
|
| 749 | u64 delta_exec)
|
| 750 | {
|
| 751 | struct cgroup *cgrp;
|
| 752 |
|
| 753 | cpuacct_account_field(task, index, delta_exec);
|
| 754 |
|
| 755 | cgrp = task_dfl_cgroup(task);
|
| 756 | if (cgroup_parent(cgrp))
|
| 757 | __cgroup_account_cputime_field(cgrp, index, delta_exec);
|
| 758 | }
|
| 759 |
|
| 760 | #else
|
| 761 |
|
| 762 | static inline void cgroup_account_cputime(struct task_struct *task,
|
| 763 | u64 delta_exec) {}
|
| 764 | static inline void cgroup_account_cputime_field(struct task_struct *task,
|
| 765 | enum cpu_usage_stat index,
|
| 766 | u64 delta_exec) {}
|
| 767 |
|
| 768 | #endif
|
| 769 |
|
| 770 | |
| 771 | * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
|
| 772 | * definition in cgroup-defs.h.
|
| 773 |
|
| 774 | #ifdef CONFIG_SOCK_CGROUP_DATA
|
| 775 |
|
| 776 | void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
|
| 777 | void cgroup_sk_clone(struct sock_cgroup_data *skcd);
|
| 778 | void cgroup_sk_free(struct sock_cgroup_data *skcd);
|
| 779 |
|
| 780 | static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
|
| 781 | {
|
| 782 | return skcd->cgroup;
|
| 783 | }
|
| 784 |
|
| 785 | #else
|
| 786 |
|
| 787 | static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
|
| 788 | static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
|
| 789 | static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
|
| 790 |
|
| 791 | #endif
|
| 792 |
|
| 793 | #ifdef CONFIG_CGROUPS
|
| 794 |
|
| 795 | void cgroup_enter_frozen(void);
|
| 796 | void cgroup_leave_frozen(bool always_leave);
|
| 797 | void cgroup_update_frozen(struct cgroup *cgrp);
|
| 798 | void cgroup_freeze(struct cgroup *cgrp, bool freeze);
|
| 799 | void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
|
| 800 | struct cgroup *dst);
|
| 801 |
|
| 802 | static inline bool cgroup_task_frozen(struct task_struct *task)
|
| 803 | {
|
| 804 | return task->frozen;
|
| 805 | }
|
| 806 |
|
| 807 | #else
|
| 808 |
|
| 809 | static inline void cgroup_enter_frozen(void) { }
|
| 810 | static inline void cgroup_leave_frozen(bool always_leave) { }
|
| 811 | static inline bool cgroup_task_frozen(struct task_struct *task)
|
| 812 | {
|
| 813 | return false;
|
| 814 | }
|
| 815 |
|
| 816 | #endif
|
| 817 |
|
| 818 | #ifdef CONFIG_CGROUP_BPF
|
| 819 | static inline void cgroup_bpf_get(struct cgroup *cgrp)
|
| 820 | {
|
| 821 | percpu_ref_get(&cgrp->bpf.refcnt);
|
| 822 | }
|
| 823 |
|
| 824 | static inline void cgroup_bpf_put(struct cgroup *cgrp)
|
| 825 | {
|
| 826 | percpu_ref_put(&cgrp->bpf.refcnt);
|
| 827 | }
|
| 828 |
|
| 829 | #else
|
| 830 |
|
| 831 | static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
|
| 832 | static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
|
| 833 |
|
| 834 | #endif
|
| 835 |
|
| 836 | struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id);
|
| 837 |
|
| 838 | struct cgroup_of_peak *of_peak(struct kernfs_open_file *of);
|
| 839 |
|
| 840 | #endif
|