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