1 /* 2 * kernel/cpuset.c 3 * 4 * Processor and Memory placement constraints for sets of tasks. 5 * 6 * Copyright (C) 2003 BULL SA. 7 * Copyright (C) 2004-2007 Silicon Graphics, Inc. 8 * Copyright (C) 2006 Google, Inc 9 * 10 * Portions derived from Patrick Mochel's sysfs code. 11 * sysfs is Copyright (c) 2001-3 Patrick Mochel 12 * 13 * 2003-10-10 Written by Simon Derr. 14 * 2003-10-22 Updates by Stephen Hemminger. 15 * 2004 May-July Rework by Paul Jackson. 16 * 2006 Rework by Paul Menage to use generic cgroups 17 * 2008 Rework of the scheduler domains and CPU hotplug handling 18 * by Max Krasnyansky 19 * 20 * This file is subject to the terms and conditions of the GNU General Public 21 * License. See the file COPYING in the main directory of the Linux 22 * distribution for more details. 23 */ 24 25 #include <linux/cpu.h> 26 #include <linux/cpumask.h> 27 #include <linux/cpuset.h> 28 #include <linux/err.h> 29 #include <linux/errno.h> 30 #include <linux/file.h> 31 #include <linux/fs.h> 32 #include <linux/init.h> 33 #include <linux/interrupt.h> 34 #include <linux/kernel.h> 35 #include <linux/kmod.h> 36 #include <linux/list.h> 37 #include <linux/mempolicy.h> 38 #include <linux/mm.h> 39 #include <linux/memory.h> 40 #include <linux/export.h> 41 #include <linux/mount.h> 42 #include <linux/namei.h> 43 #include <linux/pagemap.h> 44 #include <linux/proc_fs.h> 45 #include <linux/rcupdate.h> 46 #include <linux/sched.h> 47 #include <linux/sched/mm.h> 48 #include <linux/sched/task.h> 49 #include <linux/seq_file.h> 50 #include <linux/security.h> 51 #include <linux/slab.h> 52 #include <linux/spinlock.h> 53 #include <linux/stat.h> 54 #include <linux/string.h> 55 #include <linux/time.h> 56 #include <linux/time64.h> 57 #include <linux/backing-dev.h> 58 #include <linux/sort.h> 59 #include <linux/oom.h> 60 #include <linux/sched/isolation.h> 61 #include <linux/uaccess.h> 62 #include <linux/atomic.h> 63 #include <linux/mutex.h> 64 #include <linux/cgroup.h> 65 #include <linux/wait.h> 66 67 DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key); 68 DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key); 69 70 /* See "Frequency meter" comments, below. */ 71 72 struct fmeter { 73 int cnt; /* unprocessed events count */ 74 int val; /* most recent output value */ 75 time64_t time; /* clock (secs) when val computed */ 76 spinlock_t lock; /* guards read or write of above */ 77 }; 78 79 struct cpuset { 80 struct cgroup_subsys_state css; 81 82 unsigned long flags; /* "unsigned long" so bitops work */ 83 84 /* 85 * On default hierarchy: 86 * 87 * The user-configured masks can only be changed by writing to 88 * cpuset.cpus and cpuset.mems, and won't be limited by the 89 * parent masks. 90 * 91 * The effective masks is the real masks that apply to the tasks 92 * in the cpuset. They may be changed if the configured masks are 93 * changed or hotplug happens. 94 * 95 * effective_mask == configured_mask & parent's effective_mask, 96 * and if it ends up empty, it will inherit the parent's mask. 97 * 98 * 99 * On legacy hierachy: 100 * 101 * The user-configured masks are always the same with effective masks. 102 */ 103 104 /* user-configured CPUs and Memory Nodes allow to tasks */ 105 cpumask_var_t cpus_allowed; 106 nodemask_t mems_allowed; 107 108 /* effective CPUs and Memory Nodes allow to tasks */ 109 cpumask_var_t effective_cpus; 110 nodemask_t effective_mems; 111 112 /* 113 * CPUs allocated to child sub-partitions (default hierarchy only) 114 * - CPUs granted by the parent = effective_cpus U subparts_cpus 115 * - effective_cpus and subparts_cpus are mutually exclusive. 116 * 117 * effective_cpus contains only onlined CPUs, but subparts_cpus 118 * may have offlined ones. 119 */ 120 cpumask_var_t subparts_cpus; 121 122 /* 123 * This is old Memory Nodes tasks took on. 124 * 125 * - top_cpuset.old_mems_allowed is initialized to mems_allowed. 126 * - A new cpuset's old_mems_allowed is initialized when some 127 * task is moved into it. 128 * - old_mems_allowed is used in cpuset_migrate_mm() when we change 129 * cpuset.mems_allowed and have tasks' nodemask updated, and 130 * then old_mems_allowed is updated to mems_allowed. 131 */ 132 nodemask_t old_mems_allowed; 133 134 struct fmeter fmeter; /* memory_pressure filter */ 135 136 /* 137 * Tasks are being attached to this cpuset. Used to prevent 138 * zeroing cpus/mems_allowed between ->can_attach() and ->attach(). 139 */ 140 int attach_in_progress; 141 142 /* partition number for rebuild_sched_domains() */ 143 int pn; 144 145 /* for custom sched domain */ 146 int relax_domain_level; 147 148 /* number of CPUs in subparts_cpus */ 149 int nr_subparts_cpus; 150 151 /* partition root state */ 152 int partition_root_state; 153 154 /* 155 * Default hierarchy only: 156 * use_parent_ecpus - set if using parent's effective_cpus 157 * child_ecpus_count - # of children with use_parent_ecpus set 158 */ 159 int use_parent_ecpus; 160 int child_ecpus_count; 161 }; 162 163 /* 164 * Partition root states: 165 * 166 * 0 - not a partition root 167 * 168 * 1 - partition root 169 * 170 * -1 - invalid partition root 171 * None of the cpus in cpus_allowed can be put into the parent's 172 * subparts_cpus. In this case, the cpuset is not a real partition 173 * root anymore. However, the CPU_EXCLUSIVE bit will still be set 174 * and the cpuset can be restored back to a partition root if the 175 * parent cpuset can give more CPUs back to this child cpuset. 176 */ 177 #define PRS_DISABLED 0 178 #define PRS_ENABLED 1 179 #define PRS_ERROR -1 180 181 /* 182 * Temporary cpumasks for working with partitions that are passed among 183 * functions to avoid memory allocation in inner functions. 184 */ 185 struct tmpmasks { 186 cpumask_var_t addmask, delmask; /* For partition root */ 187 cpumask_var_t new_cpus; /* For update_cpumasks_hier() */ 188 }; 189 190 static inline struct cpuset *css_cs(struct cgroup_subsys_state *css) 191 { 192 return css ? container_of(css, struct cpuset, css) : NULL; 193 } 194 195 /* Retrieve the cpuset for a task */ 196 static inline struct cpuset *task_cs(struct task_struct *task) 197 { 198 return css_cs(task_css(task, cpuset_cgrp_id)); 199 } 200 201 static inline struct cpuset *parent_cs(struct cpuset *cs) 202 { 203 return css_cs(cs->css.parent); 204 } 205 206 /* bits in struct cpuset flags field */ 207 typedef enum { 208 CS_ONLINE, 209 CS_CPU_EXCLUSIVE, 210 CS_MEM_EXCLUSIVE, 211 CS_MEM_HARDWALL, 212 CS_MEMORY_MIGRATE, 213 CS_SCHED_LOAD_BALANCE, 214 CS_SPREAD_PAGE, 215 CS_SPREAD_SLAB, 216 } cpuset_flagbits_t; 217 218 /* convenient tests for these bits */ 219 static inline bool is_cpuset_online(struct cpuset *cs) 220 { 221 return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css); 222 } 223 224 static inline int is_cpu_exclusive(const struct cpuset *cs) 225 { 226 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); 227 } 228 229 static inline int is_mem_exclusive(const struct cpuset *cs) 230 { 231 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); 232 } 233 234 static inline int is_mem_hardwall(const struct cpuset *cs) 235 { 236 return test_bit(CS_MEM_HARDWALL, &cs->flags); 237 } 238 239 static inline int is_sched_load_balance(const struct cpuset *cs) 240 { 241 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 242 } 243 244 static inline int is_memory_migrate(const struct cpuset *cs) 245 { 246 return test_bit(CS_MEMORY_MIGRATE, &cs->flags); 247 } 248 249 static inline int is_spread_page(const struct cpuset *cs) 250 { 251 return test_bit(CS_SPREAD_PAGE, &cs->flags); 252 } 253 254 static inline int is_spread_slab(const struct cpuset *cs) 255 { 256 return test_bit(CS_SPREAD_SLAB, &cs->flags); 257 } 258 259 static inline int is_partition_root(const struct cpuset *cs) 260 { 261 return cs->partition_root_state > 0; 262 } 263 264 static struct cpuset top_cpuset = { 265 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) | 266 (1 << CS_MEM_EXCLUSIVE)), 267 .partition_root_state = PRS_ENABLED, 268 }; 269 270 /** 271 * cpuset_for_each_child - traverse online children of a cpuset 272 * @child_cs: loop cursor pointing to the current child 273 * @pos_css: used for iteration 274 * @parent_cs: target cpuset to walk children of 275 * 276 * Walk @child_cs through the online children of @parent_cs. Must be used 277 * with RCU read locked. 278 */ 279 #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \ 280 css_for_each_child((pos_css), &(parent_cs)->css) \ 281 if (is_cpuset_online(((child_cs) = css_cs((pos_css))))) 282 283 /** 284 * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants 285 * @des_cs: loop cursor pointing to the current descendant 286 * @pos_css: used for iteration 287 * @root_cs: target cpuset to walk ancestor of 288 * 289 * Walk @des_cs through the online descendants of @root_cs. Must be used 290 * with RCU read locked. The caller may modify @pos_css by calling 291 * css_rightmost_descendant() to skip subtree. @root_cs is included in the 292 * iteration and the first node to be visited. 293 */ 294 #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \ 295 css_for_each_descendant_pre((pos_css), &(root_cs)->css) \ 296 if (is_cpuset_online(((des_cs) = css_cs((pos_css))))) 297 298 /* 299 * There are two global locks guarding cpuset structures - cpuset_mutex and 300 * callback_lock. We also require taking task_lock() when dereferencing a 301 * task's cpuset pointer. See "The task_lock() exception", at the end of this 302 * comment. 303 * 304 * A task must hold both locks to modify cpusets. If a task holds 305 * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it 306 * is the only task able to also acquire callback_lock and be able to 307 * modify cpusets. It can perform various checks on the cpuset structure 308 * first, knowing nothing will change. It can also allocate memory while 309 * just holding cpuset_mutex. While it is performing these checks, various 310 * callback routines can briefly acquire callback_lock to query cpusets. 311 * Once it is ready to make the changes, it takes callback_lock, blocking 312 * everyone else. 313 * 314 * Calls to the kernel memory allocator can not be made while holding 315 * callback_lock, as that would risk double tripping on callback_lock 316 * from one of the callbacks into the cpuset code from within 317 * __alloc_pages(). 318 * 319 * If a task is only holding callback_lock, then it has read-only 320 * access to cpusets. 321 * 322 * Now, the task_struct fields mems_allowed and mempolicy may be changed 323 * by other task, we use alloc_lock in the task_struct fields to protect 324 * them. 325 * 326 * The cpuset_common_file_read() handlers only hold callback_lock across 327 * small pieces of code, such as when reading out possibly multi-word 328 * cpumasks and nodemasks. 329 * 330 * Accessing a task's cpuset should be done in accordance with the 331 * guidelines for accessing subsystem state in kernel/cgroup.c 332 */ 333 334 static DEFINE_MUTEX(cpuset_mutex); 335 static DEFINE_SPINLOCK(callback_lock); 336 337 static struct workqueue_struct *cpuset_migrate_mm_wq; 338 339 /* 340 * CPU / memory hotplug is handled asynchronously. 341 */ 342 static void cpuset_hotplug_workfn(struct work_struct *work); 343 static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn); 344 345 static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq); 346 347 /* 348 * Cgroup v2 behavior is used when on default hierarchy or the 349 * cgroup_v2_mode flag is set. 350 */ 351 static inline bool is_in_v2_mode(void) 352 { 353 return cgroup_subsys_on_dfl(cpuset_cgrp_subsys) || 354 (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE); 355 } 356 357 /* 358 * This is ugly, but preserves the userspace API for existing cpuset 359 * users. If someone tries to mount the "cpuset" filesystem, we 360 * silently switch it to mount "cgroup" instead 361 */ 362 static struct dentry *cpuset_mount(struct file_system_type *fs_type, 363 int flags, const char *unused_dev_name, void *data) 364 { 365 struct file_system_type *cgroup_fs = get_fs_type("cgroup"); 366 struct dentry *ret = ERR_PTR(-ENODEV); 367 if (cgroup_fs) { 368 char mountopts[] = 369 "cpuset,noprefix," 370 "release_agent=/sbin/cpuset_release_agent"; 371 ret = cgroup_fs->mount(cgroup_fs, flags, 372 unused_dev_name, mountopts); 373 put_filesystem(cgroup_fs); 374 } 375 return ret; 376 } 377 378 static struct file_system_type cpuset_fs_type = { 379 .name = "cpuset", 380 .mount = cpuset_mount, 381 }; 382 383 /* 384 * Return in pmask the portion of a cpusets's cpus_allowed that 385 * are online. If none are online, walk up the cpuset hierarchy 386 * until we find one that does have some online cpus. 387 * 388 * One way or another, we guarantee to return some non-empty subset 389 * of cpu_online_mask. 390 * 391 * Call with callback_lock or cpuset_mutex held. 392 */ 393 static void guarantee_online_cpus(struct cpuset *cs, struct cpumask *pmask) 394 { 395 while (!cpumask_intersects(cs->effective_cpus, cpu_online_mask)) { 396 cs = parent_cs(cs); 397 if (unlikely(!cs)) { 398 /* 399 * The top cpuset doesn't have any online cpu as a 400 * consequence of a race between cpuset_hotplug_work 401 * and cpu hotplug notifier. But we know the top 402 * cpuset's effective_cpus is on its way to to be 403 * identical to cpu_online_mask. 404 */ 405 cpumask_copy(pmask, cpu_online_mask); 406 return; 407 } 408 } 409 cpumask_and(pmask, cs->effective_cpus, cpu_online_mask); 410 } 411 412 /* 413 * Return in *pmask the portion of a cpusets's mems_allowed that 414 * are online, with memory. If none are online with memory, walk 415 * up the cpuset hierarchy until we find one that does have some 416 * online mems. The top cpuset always has some mems online. 417 * 418 * One way or another, we guarantee to return some non-empty subset 419 * of node_states[N_MEMORY]. 420 * 421 * Call with callback_lock or cpuset_mutex held. 422 */ 423 static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask) 424 { 425 while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY])) 426 cs = parent_cs(cs); 427 nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]); 428 } 429 430 /* 431 * update task's spread flag if cpuset's page/slab spread flag is set 432 * 433 * Call with callback_lock or cpuset_mutex held. 434 */ 435 static void cpuset_update_task_spread_flag(struct cpuset *cs, 436 struct task_struct *tsk) 437 { 438 if (is_spread_page(cs)) 439 task_set_spread_page(tsk); 440 else 441 task_clear_spread_page(tsk); 442 443 if (is_spread_slab(cs)) 444 task_set_spread_slab(tsk); 445 else 446 task_clear_spread_slab(tsk); 447 } 448 449 /* 450 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? 451 * 452 * One cpuset is a subset of another if all its allowed CPUs and 453 * Memory Nodes are a subset of the other, and its exclusive flags 454 * are only set if the other's are set. Call holding cpuset_mutex. 455 */ 456 457 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) 458 { 459 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) && 460 nodes_subset(p->mems_allowed, q->mems_allowed) && 461 is_cpu_exclusive(p) <= is_cpu_exclusive(q) && 462 is_mem_exclusive(p) <= is_mem_exclusive(q); 463 } 464 465 /** 466 * alloc_cpumasks - allocate three cpumasks for cpuset 467 * @cs: the cpuset that have cpumasks to be allocated. 468 * @tmp: the tmpmasks structure pointer 469 * Return: 0 if successful, -ENOMEM otherwise. 470 * 471 * Only one of the two input arguments should be non-NULL. 472 */ 473 static inline int alloc_cpumasks(struct cpuset *cs, struct tmpmasks *tmp) 474 { 475 cpumask_var_t *pmask1, *pmask2, *pmask3; 476 477 if (cs) { 478 pmask1 = &cs->cpus_allowed; 479 pmask2 = &cs->effective_cpus; 480 pmask3 = &cs->subparts_cpus; 481 } else { 482 pmask1 = &tmp->new_cpus; 483 pmask2 = &tmp->addmask; 484 pmask3 = &tmp->delmask; 485 } 486 487 if (!zalloc_cpumask_var(pmask1, GFP_KERNEL)) 488 return -ENOMEM; 489 490 if (!zalloc_cpumask_var(pmask2, GFP_KERNEL)) 491 goto free_one; 492 493 if (!zalloc_cpumask_var(pmask3, GFP_KERNEL)) 494 goto free_two; 495 496 return 0; 497 498 free_two: 499 free_cpumask_var(*pmask2); 500 free_one: 501 free_cpumask_var(*pmask1); 502 return -ENOMEM; 503 } 504 505 /** 506 * free_cpumasks - free cpumasks in a tmpmasks structure 507 * @cs: the cpuset that have cpumasks to be free. 508 * @tmp: the tmpmasks structure pointer 509 */ 510 static inline void free_cpumasks(struct cpuset *cs, struct tmpmasks *tmp) 511 { 512 if (cs) { 513 free_cpumask_var(cs->cpus_allowed); 514 free_cpumask_var(cs->effective_cpus); 515 free_cpumask_var(cs->subparts_cpus); 516 } 517 if (tmp) { 518 free_cpumask_var(tmp->new_cpus); 519 free_cpumask_var(tmp->addmask); 520 free_cpumask_var(tmp->delmask); 521 } 522 } 523 524 /** 525 * alloc_trial_cpuset - allocate a trial cpuset 526 * @cs: the cpuset that the trial cpuset duplicates 527 */ 528 static struct cpuset *alloc_trial_cpuset(struct cpuset *cs) 529 { 530 struct cpuset *trial; 531 532 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL); 533 if (!trial) 534 return NULL; 535 536 if (alloc_cpumasks(trial, NULL)) { 537 kfree(trial); 538 return NULL; 539 } 540 541 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed); 542 cpumask_copy(trial->effective_cpus, cs->effective_cpus); 543 return trial; 544 } 545 546 /** 547 * free_cpuset - free the cpuset 548 * @cs: the cpuset to be freed 549 */ 550 static inline void free_cpuset(struct cpuset *cs) 551 { 552 free_cpumasks(cs, NULL); 553 kfree(cs); 554 } 555 556 /* 557 * validate_change() - Used to validate that any proposed cpuset change 558 * follows the structural rules for cpusets. 559 * 560 * If we replaced the flag and mask values of the current cpuset 561 * (cur) with those values in the trial cpuset (trial), would 562 * our various subset and exclusive rules still be valid? Presumes 563 * cpuset_mutex held. 564 * 565 * 'cur' is the address of an actual, in-use cpuset. Operations 566 * such as list traversal that depend on the actual address of the 567 * cpuset in the list must use cur below, not trial. 568 * 569 * 'trial' is the address of bulk structure copy of cur, with 570 * perhaps one or more of the fields cpus_allowed, mems_allowed, 571 * or flags changed to new, trial values. 572 * 573 * Return 0 if valid, -errno if not. 574 */ 575 576 static int validate_change(struct cpuset *cur, struct cpuset *trial) 577 { 578 struct cgroup_subsys_state *css; 579 struct cpuset *c, *par; 580 int ret; 581 582 rcu_read_lock(); 583 584 /* Each of our child cpusets must be a subset of us */ 585 ret = -EBUSY; 586 cpuset_for_each_child(c, css, cur) 587 if (!is_cpuset_subset(c, trial)) 588 goto out; 589 590 /* Remaining checks don't apply to root cpuset */ 591 ret = 0; 592 if (cur == &top_cpuset) 593 goto out; 594 595 par = parent_cs(cur); 596 597 /* On legacy hiearchy, we must be a subset of our parent cpuset. */ 598 ret = -EACCES; 599 if (!is_in_v2_mode() && !is_cpuset_subset(trial, par)) 600 goto out; 601 602 /* 603 * If either I or some sibling (!= me) is exclusive, we can't 604 * overlap 605 */ 606 ret = -EINVAL; 607 cpuset_for_each_child(c, css, par) { 608 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && 609 c != cur && 610 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed)) 611 goto out; 612 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && 613 c != cur && 614 nodes_intersects(trial->mems_allowed, c->mems_allowed)) 615 goto out; 616 } 617 618 /* 619 * Cpusets with tasks - existing or newly being attached - can't 620 * be changed to have empty cpus_allowed or mems_allowed. 621 */ 622 ret = -ENOSPC; 623 if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) { 624 if (!cpumask_empty(cur->cpus_allowed) && 625 cpumask_empty(trial->cpus_allowed)) 626 goto out; 627 if (!nodes_empty(cur->mems_allowed) && 628 nodes_empty(trial->mems_allowed)) 629 goto out; 630 } 631 632 /* 633 * We can't shrink if we won't have enough room for SCHED_DEADLINE 634 * tasks. 635 */ 636 ret = -EBUSY; 637 if (is_cpu_exclusive(cur) && 638 !cpuset_cpumask_can_shrink(cur->cpus_allowed, 639 trial->cpus_allowed)) 640 goto out; 641 642 ret = 0; 643 out: 644 rcu_read_unlock(); 645 return ret; 646 } 647 648 #ifdef CONFIG_SMP 649 /* 650 * Helper routine for generate_sched_domains(). 651 * Do cpusets a, b have overlapping effective cpus_allowed masks? 652 */ 653 static int cpusets_overlap(struct cpuset *a, struct cpuset *b) 654 { 655 return cpumask_intersects(a->effective_cpus, b->effective_cpus); 656 } 657 658 static void 659 update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c) 660 { 661 if (dattr->relax_domain_level < c->relax_domain_level) 662 dattr->relax_domain_level = c->relax_domain_level; 663 return; 664 } 665 666 static void update_domain_attr_tree(struct sched_domain_attr *dattr, 667 struct cpuset *root_cs) 668 { 669 struct cpuset *cp; 670 struct cgroup_subsys_state *pos_css; 671 672 rcu_read_lock(); 673 cpuset_for_each_descendant_pre(cp, pos_css, root_cs) { 674 /* skip the whole subtree if @cp doesn't have any CPU */ 675 if (cpumask_empty(cp->cpus_allowed)) { 676 pos_css = css_rightmost_descendant(pos_css); 677 continue; 678 } 679 680 if (is_sched_load_balance(cp)) 681 update_domain_attr(dattr, cp); 682 } 683 rcu_read_unlock(); 684 } 685 686 /* Must be called with cpuset_mutex held. */ 687 static inline int nr_cpusets(void) 688 { 689 /* jump label reference count + the top-level cpuset */ 690 return static_key_count(&cpusets_enabled_key.key) + 1; 691 } 692 693 /* 694 * generate_sched_domains() 695 * 696 * This function builds a partial partition of the systems CPUs 697 * A 'partial partition' is a set of non-overlapping subsets whose 698 * union is a subset of that set. 699 * The output of this function needs to be passed to kernel/sched/core.c 700 * partition_sched_domains() routine, which will rebuild the scheduler's 701 * load balancing domains (sched domains) as specified by that partial 702 * partition. 703 * 704 * See "What is sched_load_balance" in Documentation/cgroup-v1/cpusets.txt 705 * for a background explanation of this. 706 * 707 * Does not return errors, on the theory that the callers of this 708 * routine would rather not worry about failures to rebuild sched 709 * domains when operating in the severe memory shortage situations 710 * that could cause allocation failures below. 711 * 712 * Must be called with cpuset_mutex held. 713 * 714 * The three key local variables below are: 715 * q - a linked-list queue of cpuset pointers, used to implement a 716 * top-down scan of all cpusets. This scan loads a pointer 717 * to each cpuset marked is_sched_load_balance into the 718 * array 'csa'. For our purposes, rebuilding the schedulers 719 * sched domains, we can ignore !is_sched_load_balance cpusets. 720 * csa - (for CpuSet Array) Array of pointers to all the cpusets 721 * that need to be load balanced, for convenient iterative 722 * access by the subsequent code that finds the best partition, 723 * i.e the set of domains (subsets) of CPUs such that the 724 * cpus_allowed of every cpuset marked is_sched_load_balance 725 * is a subset of one of these domains, while there are as 726 * many such domains as possible, each as small as possible. 727 * doms - Conversion of 'csa' to an array of cpumasks, for passing to 728 * the kernel/sched/core.c routine partition_sched_domains() in a 729 * convenient format, that can be easily compared to the prior 730 * value to determine what partition elements (sched domains) 731 * were changed (added or removed.) 732 * 733 * Finding the best partition (set of domains): 734 * The triple nested loops below over i, j, k scan over the 735 * load balanced cpusets (using the array of cpuset pointers in 736 * csa[]) looking for pairs of cpusets that have overlapping 737 * cpus_allowed, but which don't have the same 'pn' partition 738 * number and gives them in the same partition number. It keeps 739 * looping on the 'restart' label until it can no longer find 740 * any such pairs. 741 * 742 * The union of the cpus_allowed masks from the set of 743 * all cpusets having the same 'pn' value then form the one 744 * element of the partition (one sched domain) to be passed to 745 * partition_sched_domains(). 746 */ 747 static int generate_sched_domains(cpumask_var_t **domains, 748 struct sched_domain_attr **attributes) 749 { 750 struct cpuset *cp; /* scans q */ 751 struct cpuset **csa; /* array of all cpuset ptrs */ 752 int csn; /* how many cpuset ptrs in csa so far */ 753 int i, j, k; /* indices for partition finding loops */ 754 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */ 755 struct sched_domain_attr *dattr; /* attributes for custom domains */ 756 int ndoms = 0; /* number of sched domains in result */ 757 int nslot; /* next empty doms[] struct cpumask slot */ 758 struct cgroup_subsys_state *pos_css; 759 bool root_load_balance = is_sched_load_balance(&top_cpuset); 760 761 doms = NULL; 762 dattr = NULL; 763 csa = NULL; 764 765 /* Special case for the 99% of systems with one, full, sched domain */ 766 if (root_load_balance && !top_cpuset.nr_subparts_cpus) { 767 ndoms = 1; 768 doms = alloc_sched_domains(ndoms); 769 if (!doms) 770 goto done; 771 772 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL); 773 if (dattr) { 774 *dattr = SD_ATTR_INIT; 775 update_domain_attr_tree(dattr, &top_cpuset); 776 } 777 cpumask_and(doms[0], top_cpuset.effective_cpus, 778 housekeeping_cpumask(HK_FLAG_DOMAIN)); 779 780 goto done; 781 } 782 783 csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL); 784 if (!csa) 785 goto done; 786 csn = 0; 787 788 rcu_read_lock(); 789 if (root_load_balance) 790 csa[csn++] = &top_cpuset; 791 cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) { 792 if (cp == &top_cpuset) 793 continue; 794 /* 795 * Continue traversing beyond @cp iff @cp has some CPUs and 796 * isn't load balancing. The former is obvious. The 797 * latter: All child cpusets contain a subset of the 798 * parent's cpus, so just skip them, and then we call 799 * update_domain_attr_tree() to calc relax_domain_level of 800 * the corresponding sched domain. 801 * 802 * If root is load-balancing, we can skip @cp if it 803 * is a subset of the root's effective_cpus. 804 */ 805 if (!cpumask_empty(cp->cpus_allowed) && 806 !(is_sched_load_balance(cp) && 807 cpumask_intersects(cp->cpus_allowed, 808 housekeeping_cpumask(HK_FLAG_DOMAIN)))) 809 continue; 810 811 if (root_load_balance && 812 cpumask_subset(cp->cpus_allowed, top_cpuset.effective_cpus)) 813 continue; 814 815 if (is_sched_load_balance(cp)) 816 csa[csn++] = cp; 817 818 /* skip @cp's subtree if not a partition root */ 819 if (!is_partition_root(cp)) 820 pos_css = css_rightmost_descendant(pos_css); 821 } 822 rcu_read_unlock(); 823 824 for (i = 0; i < csn; i++) 825 csa[i]->pn = i; 826 ndoms = csn; 827 828 restart: 829 /* Find the best partition (set of sched domains) */ 830 for (i = 0; i < csn; i++) { 831 struct cpuset *a = csa[i]; 832 int apn = a->pn; 833 834 for (j = 0; j < csn; j++) { 835 struct cpuset *b = csa[j]; 836 int bpn = b->pn; 837 838 if (apn != bpn && cpusets_overlap(a, b)) { 839 for (k = 0; k < csn; k++) { 840 struct cpuset *c = csa[k]; 841 842 if (c->pn == bpn) 843 c->pn = apn; 844 } 845 ndoms--; /* one less element */ 846 goto restart; 847 } 848 } 849 } 850 851 /* 852 * Now we know how many domains to create. 853 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks. 854 */ 855 doms = alloc_sched_domains(ndoms); 856 if (!doms) 857 goto done; 858 859 /* 860 * The rest of the code, including the scheduler, can deal with 861 * dattr==NULL case. No need to abort if alloc fails. 862 */ 863 dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr), 864 GFP_KERNEL); 865 866 for (nslot = 0, i = 0; i < csn; i++) { 867 struct cpuset *a = csa[i]; 868 struct cpumask *dp; 869 int apn = a->pn; 870 871 if (apn < 0) { 872 /* Skip completed partitions */ 873 continue; 874 } 875 876 dp = doms[nslot]; 877 878 if (nslot == ndoms) { 879 static int warnings = 10; 880 if (warnings) { 881 pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n", 882 nslot, ndoms, csn, i, apn); 883 warnings--; 884 } 885 continue; 886 } 887 888 cpumask_clear(dp); 889 if (dattr) 890 *(dattr + nslot) = SD_ATTR_INIT; 891 for (j = i; j < csn; j++) { 892 struct cpuset *b = csa[j]; 893 894 if (apn == b->pn) { 895 cpumask_or(dp, dp, b->effective_cpus); 896 cpumask_and(dp, dp, housekeeping_cpumask(HK_FLAG_DOMAIN)); 897 if (dattr) 898 update_domain_attr_tree(dattr + nslot, b); 899 900 /* Done with this partition */ 901 b->pn = -1; 902 } 903 } 904 nslot++; 905 } 906 BUG_ON(nslot != ndoms); 907 908 done: 909 kfree(csa); 910 911 /* 912 * Fallback to the default domain if kmalloc() failed. 913 * See comments in partition_sched_domains(). 914 */ 915 if (doms == NULL) 916 ndoms = 1; 917 918 *domains = doms; 919 *attributes = dattr; 920 return ndoms; 921 } 922 923 /* 924 * Rebuild scheduler domains. 925 * 926 * If the flag 'sched_load_balance' of any cpuset with non-empty 927 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset 928 * which has that flag enabled, or if any cpuset with a non-empty 929 * 'cpus' is removed, then call this routine to rebuild the 930 * scheduler's dynamic sched domains. 931 * 932 * Call with cpuset_mutex held. Takes get_online_cpus(). 933 */ 934 static void rebuild_sched_domains_locked(void) 935 { 936 struct sched_domain_attr *attr; 937 cpumask_var_t *doms; 938 int ndoms; 939 940 lockdep_assert_held(&cpuset_mutex); 941 get_online_cpus(); 942 943 /* 944 * We have raced with CPU hotplug. Don't do anything to avoid 945 * passing doms with offlined cpu to partition_sched_domains(). 946 * Anyways, hotplug work item will rebuild sched domains. 947 */ 948 if (!top_cpuset.nr_subparts_cpus && 949 !cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask)) 950 goto out; 951 952 if (top_cpuset.nr_subparts_cpus && 953 !cpumask_subset(top_cpuset.effective_cpus, cpu_active_mask)) 954 goto out; 955 956 /* Generate domain masks and attrs */ 957 ndoms = generate_sched_domains(&doms, &attr); 958 959 /* Have scheduler rebuild the domains */ 960 partition_sched_domains(ndoms, doms, attr); 961 out: 962 put_online_cpus(); 963 } 964 #else /* !CONFIG_SMP */ 965 static void rebuild_sched_domains_locked(void) 966 { 967 } 968 #endif /* CONFIG_SMP */ 969 970 void rebuild_sched_domains(void) 971 { 972 mutex_lock(&cpuset_mutex); 973 rebuild_sched_domains_locked(); 974 mutex_unlock(&cpuset_mutex); 975 } 976 977 /** 978 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset. 979 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed 980 * 981 * Iterate through each task of @cs updating its cpus_allowed to the 982 * effective cpuset's. As this function is called with cpuset_mutex held, 983 * cpuset membership stays stable. 984 */ 985 static void update_tasks_cpumask(struct cpuset *cs) 986 { 987 struct css_task_iter it; 988 struct task_struct *task; 989 990 css_task_iter_start(&cs->css, 0, &it); 991 while ((task = css_task_iter_next(&it))) 992 set_cpus_allowed_ptr(task, cs->effective_cpus); 993 css_task_iter_end(&it); 994 } 995 996 /** 997 * compute_effective_cpumask - Compute the effective cpumask of the cpuset 998 * @new_cpus: the temp variable for the new effective_cpus mask 999 * @cs: the cpuset the need to recompute the new effective_cpus mask 1000 * @parent: the parent cpuset 1001 * 1002 * If the parent has subpartition CPUs, include them in the list of 1003 * allowable CPUs in computing the new effective_cpus mask. Since offlined 1004 * CPUs are not removed from subparts_cpus, we have to use cpu_active_mask 1005 * to mask those out. 1006 */ 1007 static void compute_effective_cpumask(struct cpumask *new_cpus, 1008 struct cpuset *cs, struct cpuset *parent) 1009 { 1010 if (parent->nr_subparts_cpus) { 1011 cpumask_or(new_cpus, parent->effective_cpus, 1012 parent->subparts_cpus); 1013 cpumask_and(new_cpus, new_cpus, cs->cpus_allowed); 1014 cpumask_and(new_cpus, new_cpus, cpu_active_mask); 1015 } else { 1016 cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus); 1017 } 1018 } 1019 1020 /* 1021 * Commands for update_parent_subparts_cpumask 1022 */ 1023 enum subparts_cmd { 1024 partcmd_enable, /* Enable partition root */ 1025 partcmd_disable, /* Disable partition root */ 1026 partcmd_update, /* Update parent's subparts_cpus */ 1027 }; 1028 1029 /** 1030 * update_parent_subparts_cpumask - update subparts_cpus mask of parent cpuset 1031 * @cpuset: The cpuset that requests change in partition root state 1032 * @cmd: Partition root state change command 1033 * @newmask: Optional new cpumask for partcmd_update 1034 * @tmp: Temporary addmask and delmask 1035 * Return: 0, 1 or an error code 1036 * 1037 * For partcmd_enable, the cpuset is being transformed from a non-partition 1038 * root to a partition root. The cpus_allowed mask of the given cpuset will 1039 * be put into parent's subparts_cpus and taken away from parent's 1040 * effective_cpus. The function will return 0 if all the CPUs listed in 1041 * cpus_allowed can be granted or an error code will be returned. 1042 * 1043 * For partcmd_disable, the cpuset is being transofrmed from a partition 1044 * root back to a non-partition root. any CPUs in cpus_allowed that are in 1045 * parent's subparts_cpus will be taken away from that cpumask and put back 1046 * into parent's effective_cpus. 0 should always be returned. 1047 * 1048 * For partcmd_update, if the optional newmask is specified, the cpu 1049 * list is to be changed from cpus_allowed to newmask. Otherwise, 1050 * cpus_allowed is assumed to remain the same. The cpuset should either 1051 * be a partition root or an invalid partition root. The partition root 1052 * state may change if newmask is NULL and none of the requested CPUs can 1053 * be granted by the parent. The function will return 1 if changes to 1054 * parent's subparts_cpus and effective_cpus happen or 0 otherwise. 1055 * Error code should only be returned when newmask is non-NULL. 1056 * 1057 * The partcmd_enable and partcmd_disable commands are used by 1058 * update_prstate(). The partcmd_update command is used by 1059 * update_cpumasks_hier() with newmask NULL and update_cpumask() with 1060 * newmask set. 1061 * 1062 * The checking is more strict when enabling partition root than the 1063 * other two commands. 1064 * 1065 * Because of the implicit cpu exclusive nature of a partition root, 1066 * cpumask changes that violates the cpu exclusivity rule will not be 1067 * permitted when checked by validate_change(). The validate_change() 1068 * function will also prevent any changes to the cpu list if it is not 1069 * a superset of children's cpu lists. 1070 */ 1071 static int update_parent_subparts_cpumask(struct cpuset *cpuset, int cmd, 1072 struct cpumask *newmask, 1073 struct tmpmasks *tmp) 1074 { 1075 struct cpuset *parent = parent_cs(cpuset); 1076 int adding; /* Moving cpus from effective_cpus to subparts_cpus */ 1077 int deleting; /* Moving cpus from subparts_cpus to effective_cpus */ 1078 bool part_error = false; /* Partition error? */ 1079 1080 lockdep_assert_held(&cpuset_mutex); 1081 1082 /* 1083 * The parent must be a partition root. 1084 * The new cpumask, if present, or the current cpus_allowed must 1085 * not be empty. 1086 */ 1087 if (!is_partition_root(parent) || 1088 (newmask && cpumask_empty(newmask)) || 1089 (!newmask && cpumask_empty(cpuset->cpus_allowed))) 1090 return -EINVAL; 1091 1092 /* 1093 * Enabling/disabling partition root is not allowed if there are 1094 * online children. 1095 */ 1096 if ((cmd != partcmd_update) && css_has_online_children(&cpuset->css)) 1097 return -EBUSY; 1098 1099 /* 1100 * Enabling partition root is not allowed if not all the CPUs 1101 * can be granted from parent's effective_cpus or at least one 1102 * CPU will be left after that. 1103 */ 1104 if ((cmd == partcmd_enable) && 1105 (!cpumask_subset(cpuset->cpus_allowed, parent->effective_cpus) || 1106 cpumask_equal(cpuset->cpus_allowed, parent->effective_cpus))) 1107 return -EINVAL; 1108 1109 /* 1110 * A cpumask update cannot make parent's effective_cpus become empty. 1111 */ 1112 adding = deleting = false; 1113 if (cmd == partcmd_enable) { 1114 cpumask_copy(tmp->addmask, cpuset->cpus_allowed); 1115 adding = true; 1116 } else if (cmd == partcmd_disable) { 1117 deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed, 1118 parent->subparts_cpus); 1119 } else if (newmask) { 1120 /* 1121 * partcmd_update with newmask: 1122 * 1123 * delmask = cpus_allowed & ~newmask & parent->subparts_cpus 1124 * addmask = newmask & parent->effective_cpus 1125 * & ~parent->subparts_cpus 1126 */ 1127 cpumask_andnot(tmp->delmask, cpuset->cpus_allowed, newmask); 1128 deleting = cpumask_and(tmp->delmask, tmp->delmask, 1129 parent->subparts_cpus); 1130 1131 cpumask_and(tmp->addmask, newmask, parent->effective_cpus); 1132 adding = cpumask_andnot(tmp->addmask, tmp->addmask, 1133 parent->subparts_cpus); 1134 /* 1135 * Return error if the new effective_cpus could become empty. 1136 */ 1137 if (adding && 1138 cpumask_equal(parent->effective_cpus, tmp->addmask)) { 1139 if (!deleting) 1140 return -EINVAL; 1141 /* 1142 * As some of the CPUs in subparts_cpus might have 1143 * been offlined, we need to compute the real delmask 1144 * to confirm that. 1145 */ 1146 if (!cpumask_and(tmp->addmask, tmp->delmask, 1147 cpu_active_mask)) 1148 return -EINVAL; 1149 cpumask_copy(tmp->addmask, parent->effective_cpus); 1150 } 1151 } else { 1152 /* 1153 * partcmd_update w/o newmask: 1154 * 1155 * addmask = cpus_allowed & parent->effectiveb_cpus 1156 * 1157 * Note that parent's subparts_cpus may have been 1158 * pre-shrunk in case there is a change in the cpu list. 1159 * So no deletion is needed. 1160 */ 1161 adding = cpumask_and(tmp->addmask, cpuset->cpus_allowed, 1162 parent->effective_cpus); 1163 part_error = cpumask_equal(tmp->addmask, 1164 parent->effective_cpus); 1165 } 1166 1167 if (cmd == partcmd_update) { 1168 int prev_prs = cpuset->partition_root_state; 1169 1170 /* 1171 * Check for possible transition between PRS_ENABLED 1172 * and PRS_ERROR. 1173 */ 1174 switch (cpuset->partition_root_state) { 1175 case PRS_ENABLED: 1176 if (part_error) 1177 cpuset->partition_root_state = PRS_ERROR; 1178 break; 1179 case PRS_ERROR: 1180 if (!part_error) 1181 cpuset->partition_root_state = PRS_ENABLED; 1182 break; 1183 } 1184 /* 1185 * Set part_error if previously in invalid state. 1186 */ 1187 part_error = (prev_prs == PRS_ERROR); 1188 } 1189 1190 if (!part_error && (cpuset->partition_root_state == PRS_ERROR)) 1191 return 0; /* Nothing need to be done */ 1192 1193 if (cpuset->partition_root_state == PRS_ERROR) { 1194 /* 1195 * Remove all its cpus from parent's subparts_cpus. 1196 */ 1197 adding = false; 1198 deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed, 1199 parent->subparts_cpus); 1200 } 1201 1202 if (!adding && !deleting) 1203 return 0; 1204 1205 /* 1206 * Change the parent's subparts_cpus. 1207 * Newly added CPUs will be removed from effective_cpus and 1208 * newly deleted ones will be added back to effective_cpus. 1209 */ 1210 spin_lock_irq(&callback_lock); 1211 if (adding) { 1212 cpumask_or(parent->subparts_cpus, 1213 parent->subparts_cpus, tmp->addmask); 1214 cpumask_andnot(parent->effective_cpus, 1215 parent->effective_cpus, tmp->addmask); 1216 } 1217 if (deleting) { 1218 cpumask_andnot(parent->subparts_cpus, 1219 parent->subparts_cpus, tmp->delmask); 1220 /* 1221 * Some of the CPUs in subparts_cpus might have been offlined. 1222 */ 1223 cpumask_and(tmp->delmask, tmp->delmask, cpu_active_mask); 1224 cpumask_or(parent->effective_cpus, 1225 parent->effective_cpus, tmp->delmask); 1226 } 1227 1228 parent->nr_subparts_cpus = cpumask_weight(parent->subparts_cpus); 1229 spin_unlock_irq(&callback_lock); 1230 1231 return cmd == partcmd_update; 1232 } 1233 1234 /* 1235 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree 1236 * @cs: the cpuset to consider 1237 * @tmp: temp variables for calculating effective_cpus & partition setup 1238 * 1239 * When congifured cpumask is changed, the effective cpumasks of this cpuset 1240 * and all its descendants need to be updated. 1241 * 1242 * On legacy hierachy, effective_cpus will be the same with cpu_allowed. 1243 * 1244 * Called with cpuset_mutex held 1245 */ 1246 static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp) 1247 { 1248 struct cpuset *cp; 1249 struct cgroup_subsys_state *pos_css; 1250 bool need_rebuild_sched_domains = false; 1251 1252 rcu_read_lock(); 1253 cpuset_for_each_descendant_pre(cp, pos_css, cs) { 1254 struct cpuset *parent = parent_cs(cp); 1255 1256 compute_effective_cpumask(tmp->new_cpus, cp, parent); 1257 1258 /* 1259 * If it becomes empty, inherit the effective mask of the 1260 * parent, which is guaranteed to have some CPUs. 1261 */ 1262 if (is_in_v2_mode() && cpumask_empty(tmp->new_cpus)) { 1263 cpumask_copy(tmp->new_cpus, parent->effective_cpus); 1264 if (!cp->use_parent_ecpus) { 1265 cp->use_parent_ecpus = true; 1266 parent->child_ecpus_count++; 1267 } 1268 } else if (cp->use_parent_ecpus) { 1269 cp->use_parent_ecpus = false; 1270 WARN_ON_ONCE(!parent->child_ecpus_count); 1271 parent->child_ecpus_count--; 1272 } 1273 1274 /* 1275 * Skip the whole subtree if the cpumask remains the same 1276 * and has no partition root state. 1277 */ 1278 if (!cp->partition_root_state && 1279 cpumask_equal(tmp->new_cpus, cp->effective_cpus)) { 1280 pos_css = css_rightmost_descendant(pos_css); 1281 continue; 1282 } 1283 1284 /* 1285 * update_parent_subparts_cpumask() should have been called 1286 * for cs already in update_cpumask(). We should also call 1287 * update_tasks_cpumask() again for tasks in the parent 1288 * cpuset if the parent's subparts_cpus changes. 1289 */ 1290 if ((cp != cs) && cp->partition_root_state) { 1291 switch (parent->partition_root_state) { 1292 case PRS_DISABLED: 1293 /* 1294 * If parent is not a partition root or an 1295 * invalid partition root, clear the state 1296 * state and the CS_CPU_EXCLUSIVE flag. 1297 */ 1298 WARN_ON_ONCE(cp->partition_root_state 1299 != PRS_ERROR); 1300 cp->partition_root_state = 0; 1301 1302 /* 1303 * clear_bit() is an atomic operation and 1304 * readers aren't interested in the state 1305 * of CS_CPU_EXCLUSIVE anyway. So we can 1306 * just update the flag without holding 1307 * the callback_lock. 1308 */ 1309 clear_bit(CS_CPU_EXCLUSIVE, &cp->flags); 1310 break; 1311 1312 case PRS_ENABLED: 1313 if (update_parent_subparts_cpumask(cp, partcmd_update, NULL, tmp)) 1314 update_tasks_cpumask(parent); 1315 break; 1316 1317 case PRS_ERROR: 1318 /* 1319 * When parent is invalid, it has to be too. 1320 */ 1321 cp->partition_root_state = PRS_ERROR; 1322 if (cp->nr_subparts_cpus) { 1323 cp->nr_subparts_cpus = 0; 1324 cpumask_clear(cp->subparts_cpus); 1325 } 1326 break; 1327 } 1328 } 1329 1330 if (!css_tryget_online(&cp->css)) 1331 continue; 1332 rcu_read_unlock(); 1333 1334 spin_lock_irq(&callback_lock); 1335 1336 cpumask_copy(cp->effective_cpus, tmp->new_cpus); 1337 if (cp->nr_subparts_cpus && 1338 (cp->partition_root_state != PRS_ENABLED)) { 1339 cp->nr_subparts_cpus = 0; 1340 cpumask_clear(cp->subparts_cpus); 1341 } else if (cp->nr_subparts_cpus) { 1342 /* 1343 * Make sure that effective_cpus & subparts_cpus 1344 * are mutually exclusive. 1345 * 1346 * In the unlikely event that effective_cpus 1347 * becomes empty. we clear cp->nr_subparts_cpus and 1348 * let its child partition roots to compete for 1349 * CPUs again. 1350 */ 1351 cpumask_andnot(cp->effective_cpus, cp->effective_cpus, 1352 cp->subparts_cpus); 1353 if (cpumask_empty(cp->effective_cpus)) { 1354 cpumask_copy(cp->effective_cpus, tmp->new_cpus); 1355 cpumask_clear(cp->subparts_cpus); 1356 cp->nr_subparts_cpus = 0; 1357 } else if (!cpumask_subset(cp->subparts_cpus, 1358 tmp->new_cpus)) { 1359 cpumask_andnot(cp->subparts_cpus, 1360 cp->subparts_cpus, tmp->new_cpus); 1361 cp->nr_subparts_cpus 1362 = cpumask_weight(cp->subparts_cpus); 1363 } 1364 } 1365 spin_unlock_irq(&callback_lock); 1366 1367 WARN_ON(!is_in_v2_mode() && 1368 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus)); 1369 1370 update_tasks_cpumask(cp); 1371 1372 /* 1373 * On legacy hierarchy, if the effective cpumask of any non- 1374 * empty cpuset is changed, we need to rebuild sched domains. 1375 * On default hierarchy, the cpuset needs to be a partition 1376 * root as well. 1377 */ 1378 if (!cpumask_empty(cp->cpus_allowed) && 1379 is_sched_load_balance(cp) && 1380 (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) || 1381 is_partition_root(cp))) 1382 need_rebuild_sched_domains = true; 1383 1384 rcu_read_lock(); 1385 css_put(&cp->css); 1386 } 1387 rcu_read_unlock(); 1388 1389 if (need_rebuild_sched_domains) 1390 rebuild_sched_domains_locked(); 1391 } 1392 1393 /** 1394 * update_sibling_cpumasks - Update siblings cpumasks 1395 * @parent: Parent cpuset 1396 * @cs: Current cpuset 1397 * @tmp: Temp variables 1398 */ 1399 static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs, 1400 struct tmpmasks *tmp) 1401 { 1402 struct cpuset *sibling; 1403 struct cgroup_subsys_state *pos_css; 1404 1405 /* 1406 * Check all its siblings and call update_cpumasks_hier() 1407 * if their use_parent_ecpus flag is set in order for them 1408 * to use the right effective_cpus value. 1409 */ 1410 rcu_read_lock(); 1411 cpuset_for_each_child(sibling, pos_css, parent) { 1412 if (sibling == cs) 1413 continue; 1414 if (!sibling->use_parent_ecpus) 1415 continue; 1416 1417 update_cpumasks_hier(sibling, tmp); 1418 } 1419 rcu_read_unlock(); 1420 } 1421 1422 /** 1423 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it 1424 * @cs: the cpuset to consider 1425 * @trialcs: trial cpuset 1426 * @buf: buffer of cpu numbers written to this cpuset 1427 */ 1428 static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, 1429 const char *buf) 1430 { 1431 int retval; 1432 struct tmpmasks tmp; 1433 1434 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */ 1435 if (cs == &top_cpuset) 1436 return -EACCES; 1437 1438 /* 1439 * An empty cpus_allowed is ok only if the cpuset has no tasks. 1440 * Since cpulist_parse() fails on an empty mask, we special case 1441 * that parsing. The validate_change() call ensures that cpusets 1442 * with tasks have cpus. 1443 */ 1444 if (!*buf) { 1445 cpumask_clear(trialcs->cpus_allowed); 1446 } else { 1447 retval = cpulist_parse(buf, trialcs->cpus_allowed); 1448 if (retval < 0) 1449 return retval; 1450 1451 if (!cpumask_subset(trialcs->cpus_allowed, 1452 top_cpuset.cpus_allowed)) 1453 return -EINVAL; 1454 } 1455 1456 /* Nothing to do if the cpus didn't change */ 1457 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed)) 1458 return 0; 1459 1460 retval = validate_change(cs, trialcs); 1461 if (retval < 0) 1462 return retval; 1463 1464 #ifdef CONFIG_CPUMASK_OFFSTACK 1465 /* 1466 * Use the cpumasks in trialcs for tmpmasks when they are pointers 1467 * to allocated cpumasks. 1468 */ 1469 tmp.addmask = trialcs->subparts_cpus; 1470 tmp.delmask = trialcs->effective_cpus; 1471 tmp.new_cpus = trialcs->cpus_allowed; 1472 #endif 1473 1474 if (cs->partition_root_state) { 1475 /* Cpumask of a partition root cannot be empty */ 1476 if (cpumask_empty(trialcs->cpus_allowed)) 1477 return -EINVAL; 1478 if (update_parent_subparts_cpumask(cs, partcmd_update, 1479 trialcs->cpus_allowed, &tmp) < 0) 1480 return -EINVAL; 1481 } 1482 1483 spin_lock_irq(&callback_lock); 1484 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); 1485 1486 /* 1487 * Make sure that subparts_cpus is a subset of cpus_allowed. 1488 */ 1489 if (cs->nr_subparts_cpus) { 1490 cpumask_andnot(cs->subparts_cpus, cs->subparts_cpus, 1491 cs->cpus_allowed); 1492 cs->nr_subparts_cpus = cpumask_weight(cs->subparts_cpus); 1493 } 1494 spin_unlock_irq(&callback_lock); 1495 1496 update_cpumasks_hier(cs, &tmp); 1497 1498 if (cs->partition_root_state) { 1499 struct cpuset *parent = parent_cs(cs); 1500 1501 /* 1502 * For partition root, update the cpumasks of sibling 1503 * cpusets if they use parent's effective_cpus. 1504 */ 1505 if (parent->child_ecpus_count) 1506 update_sibling_cpumasks(parent, cs, &tmp); 1507 } 1508 return 0; 1509 } 1510 1511 /* 1512 * Migrate memory region from one set of nodes to another. This is 1513 * performed asynchronously as it can be called from process migration path 1514 * holding locks involved in process management. All mm migrations are 1515 * performed in the queued order and can be waited for by flushing 1516 * cpuset_migrate_mm_wq. 1517 */ 1518 1519 struct cpuset_migrate_mm_work { 1520 struct work_struct work; 1521 struct mm_struct *mm; 1522 nodemask_t from; 1523 nodemask_t to; 1524 }; 1525 1526 static void cpuset_migrate_mm_workfn(struct work_struct *work) 1527 { 1528 struct cpuset_migrate_mm_work *mwork = 1529 container_of(work, struct cpuset_migrate_mm_work, work); 1530 1531 /* on a wq worker, no need to worry about %current's mems_allowed */ 1532 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL); 1533 mmput(mwork->mm); 1534 kfree(mwork); 1535 } 1536 1537 static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, 1538 const nodemask_t *to) 1539 { 1540 struct cpuset_migrate_mm_work *mwork; 1541 1542 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL); 1543 if (mwork) { 1544 mwork->mm = mm; 1545 mwork->from = *from; 1546 mwork->to = *to; 1547 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn); 1548 queue_work(cpuset_migrate_mm_wq, &mwork->work); 1549 } else { 1550 mmput(mm); 1551 } 1552 } 1553 1554 static void cpuset_post_attach(void) 1555 { 1556 flush_workqueue(cpuset_migrate_mm_wq); 1557 } 1558 1559 /* 1560 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy 1561 * @tsk: the task to change 1562 * @newmems: new nodes that the task will be set 1563 * 1564 * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed 1565 * and rebind an eventual tasks' mempolicy. If the task is allocating in 1566 * parallel, it might temporarily see an empty intersection, which results in 1567 * a seqlock check and retry before OOM or allocation failure. 1568 */ 1569 static void cpuset_change_task_nodemask(struct task_struct *tsk, 1570 nodemask_t *newmems) 1571 { 1572 task_lock(tsk); 1573 1574 local_irq_disable(); 1575 write_seqcount_begin(&tsk->mems_allowed_seq); 1576 1577 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); 1578 mpol_rebind_task(tsk, newmems); 1579 tsk->mems_allowed = *newmems; 1580 1581 write_seqcount_end(&tsk->mems_allowed_seq); 1582 local_irq_enable(); 1583 1584 task_unlock(tsk); 1585 } 1586 1587 static void *cpuset_being_rebound; 1588 1589 /** 1590 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. 1591 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed 1592 * 1593 * Iterate through each task of @cs updating its mems_allowed to the 1594 * effective cpuset's. As this function is called with cpuset_mutex held, 1595 * cpuset membership stays stable. 1596 */ 1597 static void update_tasks_nodemask(struct cpuset *cs) 1598 { 1599 static nodemask_t newmems; /* protected by cpuset_mutex */ 1600 struct css_task_iter it; 1601 struct task_struct *task; 1602 1603 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ 1604 1605 guarantee_online_mems(cs, &newmems); 1606 1607 /* 1608 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't 1609 * take while holding tasklist_lock. Forks can happen - the 1610 * mpol_dup() cpuset_being_rebound check will catch such forks, 1611 * and rebind their vma mempolicies too. Because we still hold 1612 * the global cpuset_mutex, we know that no other rebind effort 1613 * will be contending for the global variable cpuset_being_rebound. 1614 * It's ok if we rebind the same mm twice; mpol_rebind_mm() 1615 * is idempotent. Also migrate pages in each mm to new nodes. 1616 */ 1617 css_task_iter_start(&cs->css, 0, &it); 1618 while ((task = css_task_iter_next(&it))) { 1619 struct mm_struct *mm; 1620 bool migrate; 1621 1622 cpuset_change_task_nodemask(task, &newmems); 1623 1624 mm = get_task_mm(task); 1625 if (!mm) 1626 continue; 1627 1628 migrate = is_memory_migrate(cs); 1629 1630 mpol_rebind_mm(mm, &cs->mems_allowed); 1631 if (migrate) 1632 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems); 1633 else 1634 mmput(mm); 1635 } 1636 css_task_iter_end(&it); 1637 1638 /* 1639 * All the tasks' nodemasks have been updated, update 1640 * cs->old_mems_allowed. 1641 */ 1642 cs->old_mems_allowed = newmems; 1643 1644 /* We're done rebinding vmas to this cpuset's new mems_allowed. */ 1645 cpuset_being_rebound = NULL; 1646 } 1647 1648 /* 1649 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree 1650 * @cs: the cpuset to consider 1651 * @new_mems: a temp variable for calculating new effective_mems 1652 * 1653 * When configured nodemask is changed, the effective nodemasks of this cpuset 1654 * and all its descendants need to be updated. 1655 * 1656 * On legacy hiearchy, effective_mems will be the same with mems_allowed. 1657 * 1658 * Called with cpuset_mutex held 1659 */ 1660 static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) 1661 { 1662 struct cpuset *cp; 1663 struct cgroup_subsys_state *pos_css; 1664 1665 rcu_read_lock(); 1666 cpuset_for_each_descendant_pre(cp, pos_css, cs) { 1667 struct cpuset *parent = parent_cs(cp); 1668 1669 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems); 1670 1671 /* 1672 * If it becomes empty, inherit the effective mask of the 1673 * parent, which is guaranteed to have some MEMs. 1674 */ 1675 if (is_in_v2_mode() && nodes_empty(*new_mems)) 1676 *new_mems = parent->effective_mems; 1677 1678 /* Skip the whole subtree if the nodemask remains the same. */ 1679 if (nodes_equal(*new_mems, cp->effective_mems)) { 1680 pos_css = css_rightmost_descendant(pos_css); 1681 continue; 1682 } 1683 1684 if (!css_tryget_online(&cp->css)) 1685 continue; 1686 rcu_read_unlock(); 1687 1688 spin_lock_irq(&callback_lock); 1689 cp->effective_mems = *new_mems; 1690 spin_unlock_irq(&callback_lock); 1691 1692 WARN_ON(!is_in_v2_mode() && 1693 !nodes_equal(cp->mems_allowed, cp->effective_mems)); 1694 1695 update_tasks_nodemask(cp); 1696 1697 rcu_read_lock(); 1698 css_put(&cp->css); 1699 } 1700 rcu_read_unlock(); 1701 } 1702 1703 /* 1704 * Handle user request to change the 'mems' memory placement 1705 * of a cpuset. Needs to validate the request, update the 1706 * cpusets mems_allowed, and for each task in the cpuset, 1707 * update mems_allowed and rebind task's mempolicy and any vma 1708 * mempolicies and if the cpuset is marked 'memory_migrate', 1709 * migrate the tasks pages to the new memory. 1710 * 1711 * Call with cpuset_mutex held. May take callback_lock during call. 1712 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, 1713 * lock each such tasks mm->mmap_sem, scan its vma's and rebind 1714 * their mempolicies to the cpusets new mems_allowed. 1715 */ 1716 static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, 1717 const char *buf) 1718 { 1719 int retval; 1720 1721 /* 1722 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY]; 1723 * it's read-only 1724 */ 1725 if (cs == &top_cpuset) { 1726 retval = -EACCES; 1727 goto done; 1728 } 1729 1730 /* 1731 * An empty mems_allowed is ok iff there are no tasks in the cpuset. 1732 * Since nodelist_parse() fails on an empty mask, we special case 1733 * that parsing. The validate_change() call ensures that cpusets 1734 * with tasks have memory. 1735 */ 1736 if (!*buf) { 1737 nodes_clear(trialcs->mems_allowed); 1738 } else { 1739 retval = nodelist_parse(buf, trialcs->mems_allowed); 1740 if (retval < 0) 1741 goto done; 1742 1743 if (!nodes_subset(trialcs->mems_allowed, 1744 top_cpuset.mems_allowed)) { 1745 retval = -EINVAL; 1746 goto done; 1747 } 1748 } 1749 1750 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) { 1751 retval = 0; /* Too easy - nothing to do */ 1752 goto done; 1753 } 1754 retval = validate_change(cs, trialcs); 1755 if (retval < 0) 1756 goto done; 1757 1758 spin_lock_irq(&callback_lock); 1759 cs->mems_allowed = trialcs->mems_allowed; 1760 spin_unlock_irq(&callback_lock); 1761 1762 /* use trialcs->mems_allowed as a temp variable */ 1763 update_nodemasks_hier(cs, &trialcs->mems_allowed); 1764 done: 1765 return retval; 1766 } 1767 1768 bool current_cpuset_is_being_rebound(void) 1769 { 1770 bool ret; 1771 1772 rcu_read_lock(); 1773 ret = task_cs(current) == cpuset_being_rebound; 1774 rcu_read_unlock(); 1775 1776 return ret; 1777 } 1778 1779 static int update_relax_domain_level(struct cpuset *cs, s64 val) 1780 { 1781 #ifdef CONFIG_SMP 1782 if (val < -1 || val >= sched_domain_level_max) 1783 return -EINVAL; 1784 #endif 1785 1786 if (val != cs->relax_domain_level) { 1787 cs->relax_domain_level = val; 1788 if (!cpumask_empty(cs->cpus_allowed) && 1789 is_sched_load_balance(cs)) 1790 rebuild_sched_domains_locked(); 1791 } 1792 1793 return 0; 1794 } 1795 1796 /** 1797 * update_tasks_flags - update the spread flags of tasks in the cpuset. 1798 * @cs: the cpuset in which each task's spread flags needs to be changed 1799 * 1800 * Iterate through each task of @cs updating its spread flags. As this 1801 * function is called with cpuset_mutex held, cpuset membership stays 1802 * stable. 1803 */ 1804 static void update_tasks_flags(struct cpuset *cs) 1805 { 1806 struct css_task_iter it; 1807 struct task_struct *task; 1808 1809 css_task_iter_start(&cs->css, 0, &it); 1810 while ((task = css_task_iter_next(&it))) 1811 cpuset_update_task_spread_flag(cs, task); 1812 css_task_iter_end(&it); 1813 } 1814 1815 /* 1816 * update_flag - read a 0 or a 1 in a file and update associated flag 1817 * bit: the bit to update (see cpuset_flagbits_t) 1818 * cs: the cpuset to update 1819 * turning_on: whether the flag is being set or cleared 1820 * 1821 * Call with cpuset_mutex held. 1822 */ 1823 1824 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, 1825 int turning_on) 1826 { 1827 struct cpuset *trialcs; 1828 int balance_flag_changed; 1829 int spread_flag_changed; 1830 int err; 1831 1832 trialcs = alloc_trial_cpuset(cs); 1833 if (!trialcs) 1834 return -ENOMEM; 1835 1836 if (turning_on) 1837 set_bit(bit, &trialcs->flags); 1838 else 1839 clear_bit(bit, &trialcs->flags); 1840 1841 err = validate_change(cs, trialcs); 1842 if (err < 0) 1843 goto out; 1844 1845 balance_flag_changed = (is_sched_load_balance(cs) != 1846 is_sched_load_balance(trialcs)); 1847 1848 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs)) 1849 || (is_spread_page(cs) != is_spread_page(trialcs))); 1850 1851 spin_lock_irq(&callback_lock); 1852 cs->flags = trialcs->flags; 1853 spin_unlock_irq(&callback_lock); 1854 1855 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) 1856 rebuild_sched_domains_locked(); 1857 1858 if (spread_flag_changed) 1859 update_tasks_flags(cs); 1860 out: 1861 free_cpuset(trialcs); 1862 return err; 1863 } 1864 1865 /* 1866 * update_prstate - update partititon_root_state 1867 * cs: the cpuset to update 1868 * val: 0 - disabled, 1 - enabled 1869 * 1870 * Call with cpuset_mutex held. 1871 */ 1872 static int update_prstate(struct cpuset *cs, int val) 1873 { 1874 int err; 1875 struct cpuset *parent = parent_cs(cs); 1876 struct tmpmasks tmp; 1877 1878 if ((val != 0) && (val != 1)) 1879 return -EINVAL; 1880 if (val == cs->partition_root_state) 1881 return 0; 1882 1883 /* 1884 * Cannot force a partial or invalid partition root to a full 1885 * partition root. 1886 */ 1887 if (val && cs->partition_root_state) 1888 return -EINVAL; 1889 1890 if (alloc_cpumasks(NULL, &tmp)) 1891 return -ENOMEM; 1892 1893 err = -EINVAL; 1894 if (!cs->partition_root_state) { 1895 /* 1896 * Turning on partition root requires setting the 1897 * CS_CPU_EXCLUSIVE bit implicitly as well and cpus_allowed 1898 * cannot be NULL. 1899 */ 1900 if (cpumask_empty(cs->cpus_allowed)) 1901 goto out; 1902 1903 err = update_flag(CS_CPU_EXCLUSIVE, cs, 1); 1904 if (err) 1905 goto out; 1906 1907 err = update_parent_subparts_cpumask(cs, partcmd_enable, 1908 NULL, &tmp); 1909 if (err) { 1910 update_flag(CS_CPU_EXCLUSIVE, cs, 0); 1911 goto out; 1912 } 1913 cs->partition_root_state = PRS_ENABLED; 1914 } else { 1915 /* 1916 * Turning off partition root will clear the 1917 * CS_CPU_EXCLUSIVE bit. 1918 */ 1919 if (cs->partition_root_state == PRS_ERROR) { 1920 cs->partition_root_state = 0; 1921 update_flag(CS_CPU_EXCLUSIVE, cs, 0); 1922 err = 0; 1923 goto out; 1924 } 1925 1926 err = update_parent_subparts_cpumask(cs, partcmd_disable, 1927 NULL, &tmp); 1928 if (err) 1929 goto out; 1930 1931 cs->partition_root_state = 0; 1932 1933 /* Turning off CS_CPU_EXCLUSIVE will not return error */ 1934 update_flag(CS_CPU_EXCLUSIVE, cs, 0); 1935 } 1936 1937 /* 1938 * Update cpumask of parent's tasks except when it is the top 1939 * cpuset as some system daemons cannot be mapped to other CPUs. 1940 */ 1941 if (parent != &top_cpuset) 1942 update_tasks_cpumask(parent); 1943 1944 if (parent->child_ecpus_count) 1945 update_sibling_cpumasks(parent, cs, &tmp); 1946 1947 rebuild_sched_domains_locked(); 1948 out: 1949 free_cpumasks(NULL, &tmp); 1950 return err; 1951 } 1952 1953 /* 1954 * Frequency meter - How fast is some event occurring? 1955 * 1956 * These routines manage a digitally filtered, constant time based, 1957 * event frequency meter. There are four routines: 1958 * fmeter_init() - initialize a frequency meter. 1959 * fmeter_markevent() - called each time the event happens. 1960 * fmeter_getrate() - returns the recent rate of such events. 1961 * fmeter_update() - internal routine used to update fmeter. 1962 * 1963 * A common data structure is passed to each of these routines, 1964 * which is used to keep track of the state required to manage the 1965 * frequency meter and its digital filter. 1966 * 1967 * The filter works on the number of events marked per unit time. 1968 * The filter is single-pole low-pass recursive (IIR). The time unit 1969 * is 1 second. Arithmetic is done using 32-bit integers scaled to 1970 * simulate 3 decimal digits of precision (multiplied by 1000). 1971 * 1972 * With an FM_COEF of 933, and a time base of 1 second, the filter 1973 * has a half-life of 10 seconds, meaning that if the events quit 1974 * happening, then the rate returned from the fmeter_getrate() 1975 * will be cut in half each 10 seconds, until it converges to zero. 1976 * 1977 * It is not worth doing a real infinitely recursive filter. If more 1978 * than FM_MAXTICKS ticks have elapsed since the last filter event, 1979 * just compute FM_MAXTICKS ticks worth, by which point the level 1980 * will be stable. 1981 * 1982 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid 1983 * arithmetic overflow in the fmeter_update() routine. 1984 * 1985 * Given the simple 32 bit integer arithmetic used, this meter works 1986 * best for reporting rates between one per millisecond (msec) and 1987 * one per 32 (approx) seconds. At constant rates faster than one 1988 * per msec it maxes out at values just under 1,000,000. At constant 1989 * rates between one per msec, and one per second it will stabilize 1990 * to a value N*1000, where N is the rate of events per second. 1991 * At constant rates between one per second and one per 32 seconds, 1992 * it will be choppy, moving up on the seconds that have an event, 1993 * and then decaying until the next event. At rates slower than 1994 * about one in 32 seconds, it decays all the way back to zero between 1995 * each event. 1996 */ 1997 1998 #define FM_COEF 933 /* coefficient for half-life of 10 secs */ 1999 #define FM_MAXTICKS ((u32)99) /* useless computing more ticks than this */ 2000 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ 2001 #define FM_SCALE 1000 /* faux fixed point scale */ 2002 2003 /* Initialize a frequency meter */ 2004 static void fmeter_init(struct fmeter *fmp) 2005 { 2006 fmp->cnt = 0; 2007 fmp->val = 0; 2008 fmp->time = 0; 2009 spin_lock_init(&fmp->lock); 2010 } 2011 2012 /* Internal meter update - process cnt events and update value */ 2013 static void fmeter_update(struct fmeter *fmp) 2014 { 2015 time64_t now; 2016 u32 ticks; 2017 2018 now = ktime_get_seconds(); 2019 ticks = now - fmp->time; 2020 2021 if (ticks == 0) 2022 return; 2023 2024 ticks = min(FM_MAXTICKS, ticks); 2025 while (ticks-- > 0) 2026 fmp->val = (FM_COEF * fmp->val) / FM_SCALE; 2027 fmp->time = now; 2028 2029 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; 2030 fmp->cnt = 0; 2031 } 2032 2033 /* Process any previous ticks, then bump cnt by one (times scale). */ 2034 static void fmeter_markevent(struct fmeter *fmp) 2035 { 2036 spin_lock(&fmp->lock); 2037 fmeter_update(fmp); 2038 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); 2039 spin_unlock(&fmp->lock); 2040 } 2041 2042 /* Process any previous ticks, then return current value. */ 2043 static int fmeter_getrate(struct fmeter *fmp) 2044 { 2045 int val; 2046 2047 spin_lock(&fmp->lock); 2048 fmeter_update(fmp); 2049 val = fmp->val; 2050 spin_unlock(&fmp->lock); 2051 return val; 2052 } 2053 2054 static struct cpuset *cpuset_attach_old_cs; 2055 2056 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */ 2057 static int cpuset_can_attach(struct cgroup_taskset *tset) 2058 { 2059 struct cgroup_subsys_state *css; 2060 struct cpuset *cs; 2061 struct task_struct *task; 2062 int ret; 2063 2064 /* used later by cpuset_attach() */ 2065 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css)); 2066 cs = css_cs(css); 2067 2068 mutex_lock(&cpuset_mutex); 2069 2070 /* allow moving tasks into an empty cpuset if on default hierarchy */ 2071 ret = -ENOSPC; 2072 if (!is_in_v2_mode() && 2073 (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))) 2074 goto out_unlock; 2075 2076 cgroup_taskset_for_each(task, css, tset) { 2077 ret = task_can_attach(task, cs->cpus_allowed); 2078 if (ret) 2079 goto out_unlock; 2080 ret = security_task_setscheduler(task); 2081 if (ret) 2082 goto out_unlock; 2083 } 2084 2085 /* 2086 * Mark attach is in progress. This makes validate_change() fail 2087 * changes which zero cpus/mems_allowed. 2088 */ 2089 cs->attach_in_progress++; 2090 ret = 0; 2091 out_unlock: 2092 mutex_unlock(&cpuset_mutex); 2093 return ret; 2094 } 2095 2096 static void cpuset_cancel_attach(struct cgroup_taskset *tset) 2097 { 2098 struct cgroup_subsys_state *css; 2099 2100 cgroup_taskset_first(tset, &css); 2101 2102 mutex_lock(&cpuset_mutex); 2103 css_cs(css)->attach_in_progress--; 2104 mutex_unlock(&cpuset_mutex); 2105 } 2106 2107 /* 2108 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach() 2109 * but we can't allocate it dynamically there. Define it global and 2110 * allocate from cpuset_init(). 2111 */ 2112 static cpumask_var_t cpus_attach; 2113 2114 static void cpuset_attach(struct cgroup_taskset *tset) 2115 { 2116 /* static buf protected by cpuset_mutex */ 2117 static nodemask_t cpuset_attach_nodemask_to; 2118 struct task_struct *task; 2119 struct task_struct *leader; 2120 struct cgroup_subsys_state *css; 2121 struct cpuset *cs; 2122 struct cpuset *oldcs = cpuset_attach_old_cs; 2123 2124 cgroup_taskset_first(tset, &css); 2125 cs = css_cs(css); 2126 2127 mutex_lock(&cpuset_mutex); 2128 2129 /* prepare for attach */ 2130 if (cs == &top_cpuset) 2131 cpumask_copy(cpus_attach, cpu_possible_mask); 2132 else 2133 guarantee_online_cpus(cs, cpus_attach); 2134 2135 guarantee_online_mems(cs, &cpuset_attach_nodemask_to); 2136 2137 cgroup_taskset_for_each(task, css, tset) { 2138 /* 2139 * can_attach beforehand should guarantee that this doesn't 2140 * fail. TODO: have a better way to handle failure here 2141 */ 2142 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); 2143 2144 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); 2145 cpuset_update_task_spread_flag(cs, task); 2146 } 2147 2148 /* 2149 * Change mm for all threadgroup leaders. This is expensive and may 2150 * sleep and should be moved outside migration path proper. 2151 */ 2152 cpuset_attach_nodemask_to = cs->effective_mems; 2153 cgroup_taskset_for_each_leader(leader, css, tset) { 2154 struct mm_struct *mm = get_task_mm(leader); 2155 2156 if (mm) { 2157 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to); 2158 2159 /* 2160 * old_mems_allowed is the same with mems_allowed 2161 * here, except if this task is being moved 2162 * automatically due to hotplug. In that case 2163 * @mems_allowed has been updated and is empty, so 2164 * @old_mems_allowed is the right nodesets that we 2165 * migrate mm from. 2166 */ 2167 if (is_memory_migrate(cs)) 2168 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed, 2169 &cpuset_attach_nodemask_to); 2170 else 2171 mmput(mm); 2172 } 2173 } 2174 2175 cs->old_mems_allowed = cpuset_attach_nodemask_to; 2176 2177 cs->attach_in_progress--; 2178 if (!cs->attach_in_progress) 2179 wake_up(&cpuset_attach_wq); 2180 2181 mutex_unlock(&cpuset_mutex); 2182 } 2183 2184 /* The various types of files and directories in a cpuset file system */ 2185 2186 typedef enum { 2187 FILE_MEMORY_MIGRATE, 2188 FILE_CPULIST, 2189 FILE_MEMLIST, 2190 FILE_EFFECTIVE_CPULIST, 2191 FILE_EFFECTIVE_MEMLIST, 2192 FILE_SUBPARTS_CPULIST, 2193 FILE_CPU_EXCLUSIVE, 2194 FILE_MEM_EXCLUSIVE, 2195 FILE_MEM_HARDWALL, 2196 FILE_SCHED_LOAD_BALANCE, 2197 FILE_PARTITION_ROOT, 2198 FILE_SCHED_RELAX_DOMAIN_LEVEL, 2199 FILE_MEMORY_PRESSURE_ENABLED, 2200 FILE_MEMORY_PRESSURE, 2201 FILE_SPREAD_PAGE, 2202 FILE_SPREAD_SLAB, 2203 } cpuset_filetype_t; 2204 2205 static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft, 2206 u64 val) 2207 { 2208 struct cpuset *cs = css_cs(css); 2209 cpuset_filetype_t type = cft->private; 2210 int retval = 0; 2211 2212 mutex_lock(&cpuset_mutex); 2213 if (!is_cpuset_online(cs)) { 2214 retval = -ENODEV; 2215 goto out_unlock; 2216 } 2217 2218 switch (type) { 2219 case FILE_CPU_EXCLUSIVE: 2220 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val); 2221 break; 2222 case FILE_MEM_EXCLUSIVE: 2223 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val); 2224 break; 2225 case FILE_MEM_HARDWALL: 2226 retval = update_flag(CS_MEM_HARDWALL, cs, val); 2227 break; 2228 case FILE_SCHED_LOAD_BALANCE: 2229 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val); 2230 break; 2231 case FILE_MEMORY_MIGRATE: 2232 retval = update_flag(CS_MEMORY_MIGRATE, cs, val); 2233 break; 2234 case FILE_MEMORY_PRESSURE_ENABLED: 2235 cpuset_memory_pressure_enabled = !!val; 2236 break; 2237 case FILE_SPREAD_PAGE: 2238 retval = update_flag(CS_SPREAD_PAGE, cs, val); 2239 break; 2240 case FILE_SPREAD_SLAB: 2241 retval = update_flag(CS_SPREAD_SLAB, cs, val); 2242 break; 2243 default: 2244 retval = -EINVAL; 2245 break; 2246 } 2247 out_unlock: 2248 mutex_unlock(&cpuset_mutex); 2249 return retval; 2250 } 2251 2252 static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft, 2253 s64 val) 2254 { 2255 struct cpuset *cs = css_cs(css); 2256 cpuset_filetype_t type = cft->private; 2257 int retval = -ENODEV; 2258 2259 mutex_lock(&cpuset_mutex); 2260 if (!is_cpuset_online(cs)) 2261 goto out_unlock; 2262 2263 switch (type) { 2264 case FILE_SCHED_RELAX_DOMAIN_LEVEL: 2265 retval = update_relax_domain_level(cs, val); 2266 break; 2267 default: 2268 retval = -EINVAL; 2269 break; 2270 } 2271 out_unlock: 2272 mutex_unlock(&cpuset_mutex); 2273 return retval; 2274 } 2275 2276 /* 2277 * Common handling for a write to a "cpus" or "mems" file. 2278 */ 2279 static ssize_t cpuset_write_resmask(struct kernfs_open_file *of, 2280 char *buf, size_t nbytes, loff_t off) 2281 { 2282 struct cpuset *cs = css_cs(of_css(of)); 2283 struct cpuset *trialcs; 2284 int retval = -ENODEV; 2285 2286 buf = strstrip(buf); 2287 2288 /* 2289 * CPU or memory hotunplug may leave @cs w/o any execution 2290 * resources, in which case the hotplug code asynchronously updates 2291 * configuration and transfers all tasks to the nearest ancestor 2292 * which can execute. 2293 * 2294 * As writes to "cpus" or "mems" may restore @cs's execution 2295 * resources, wait for the previously scheduled operations before 2296 * proceeding, so that we don't end up keep removing tasks added 2297 * after execution capability is restored. 2298 * 2299 * cpuset_hotplug_work calls back into cgroup core via 2300 * cgroup_transfer_tasks() and waiting for it from a cgroupfs 2301 * operation like this one can lead to a deadlock through kernfs 2302 * active_ref protection. Let's break the protection. Losing the 2303 * protection is okay as we check whether @cs is online after 2304 * grabbing cpuset_mutex anyway. This only happens on the legacy 2305 * hierarchies. 2306 */ 2307 css_get(&cs->css); 2308 kernfs_break_active_protection(of->kn); 2309 flush_work(&cpuset_hotplug_work); 2310 2311 mutex_lock(&cpuset_mutex); 2312 if (!is_cpuset_online(cs)) 2313 goto out_unlock; 2314 2315 trialcs = alloc_trial_cpuset(cs); 2316 if (!trialcs) { 2317 retval = -ENOMEM; 2318 goto out_unlock; 2319 } 2320 2321 switch (of_cft(of)->private) { 2322 case FILE_CPULIST: 2323 retval = update_cpumask(cs, trialcs, buf); 2324 break; 2325 case FILE_MEMLIST: 2326 retval = update_nodemask(cs, trialcs, buf); 2327 break; 2328 default: 2329 retval = -EINVAL; 2330 break; 2331 } 2332 2333 free_cpuset(trialcs); 2334 out_unlock: 2335 mutex_unlock(&cpuset_mutex); 2336 kernfs_unbreak_active_protection(of->kn); 2337 css_put(&cs->css); 2338 flush_workqueue(cpuset_migrate_mm_wq); 2339 return retval ?: nbytes; 2340 } 2341 2342 /* 2343 * These ascii lists should be read in a single call, by using a user 2344 * buffer large enough to hold the entire map. If read in smaller 2345 * chunks, there is no guarantee of atomicity. Since the display format 2346 * used, list of ranges of sequential numbers, is variable length, 2347 * and since these maps can change value dynamically, one could read 2348 * gibberish by doing partial reads while a list was changing. 2349 */ 2350 static int cpuset_common_seq_show(struct seq_file *sf, void *v) 2351 { 2352 struct cpuset *cs = css_cs(seq_css(sf)); 2353 cpuset_filetype_t type = seq_cft(sf)->private; 2354 int ret = 0; 2355 2356 spin_lock_irq(&callback_lock); 2357 2358 switch (type) { 2359 case FILE_CPULIST: 2360 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed)); 2361 break; 2362 case FILE_MEMLIST: 2363 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed)); 2364 break; 2365 case FILE_EFFECTIVE_CPULIST: 2366 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus)); 2367 break; 2368 case FILE_EFFECTIVE_MEMLIST: 2369 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems)); 2370 break; 2371 case FILE_SUBPARTS_CPULIST: 2372 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->subparts_cpus)); 2373 break; 2374 default: 2375 ret = -EINVAL; 2376 } 2377 2378 spin_unlock_irq(&callback_lock); 2379 return ret; 2380 } 2381 2382 static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft) 2383 { 2384 struct cpuset *cs = css_cs(css); 2385 cpuset_filetype_t type = cft->private; 2386 switch (type) { 2387 case FILE_CPU_EXCLUSIVE: 2388 return is_cpu_exclusive(cs); 2389 case FILE_MEM_EXCLUSIVE: 2390 return is_mem_exclusive(cs); 2391 case FILE_MEM_HARDWALL: 2392 return is_mem_hardwall(cs); 2393 case FILE_SCHED_LOAD_BALANCE: 2394 return is_sched_load_balance(cs); 2395 case FILE_MEMORY_MIGRATE: 2396 return is_memory_migrate(cs); 2397 case FILE_MEMORY_PRESSURE_ENABLED: 2398 return cpuset_memory_pressure_enabled; 2399 case FILE_MEMORY_PRESSURE: 2400 return fmeter_getrate(&cs->fmeter); 2401 case FILE_SPREAD_PAGE: 2402 return is_spread_page(cs); 2403 case FILE_SPREAD_SLAB: 2404 return is_spread_slab(cs); 2405 default: 2406 BUG(); 2407 } 2408 2409 /* Unreachable but makes gcc happy */ 2410 return 0; 2411 } 2412 2413 static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft) 2414 { 2415 struct cpuset *cs = css_cs(css); 2416 cpuset_filetype_t type = cft->private; 2417 switch (type) { 2418 case FILE_SCHED_RELAX_DOMAIN_LEVEL: 2419 return cs->relax_domain_level; 2420 default: 2421 BUG(); 2422 } 2423 2424 /* Unrechable but makes gcc happy */ 2425 return 0; 2426 } 2427 2428 static int sched_partition_show(struct seq_file *seq, void *v) 2429 { 2430 struct cpuset *cs = css_cs(seq_css(seq)); 2431 2432 switch (cs->partition_root_state) { 2433 case PRS_ENABLED: 2434 seq_puts(seq, "root\n"); 2435 break; 2436 case PRS_DISABLED: 2437 seq_puts(seq, "member\n"); 2438 break; 2439 case PRS_ERROR: 2440 seq_puts(seq, "root invalid\n"); 2441 break; 2442 } 2443 return 0; 2444 } 2445 2446 static ssize_t sched_partition_write(struct kernfs_open_file *of, char *buf, 2447 size_t nbytes, loff_t off) 2448 { 2449 struct cpuset *cs = css_cs(of_css(of)); 2450 int val; 2451 int retval = -ENODEV; 2452 2453 buf = strstrip(buf); 2454 2455 /* 2456 * Convert "root" to ENABLED, and convert "member" to DISABLED. 2457 */ 2458 if (!strcmp(buf, "root")) 2459 val = PRS_ENABLED; 2460 else if (!strcmp(buf, "member")) 2461 val = PRS_DISABLED; 2462 else 2463 return -EINVAL; 2464 2465 css_get(&cs->css); 2466 mutex_lock(&cpuset_mutex); 2467 if (!is_cpuset_online(cs)) 2468 goto out_unlock; 2469 2470 retval = update_prstate(cs, val); 2471 out_unlock: 2472 mutex_unlock(&cpuset_mutex); 2473 css_put(&cs->css); 2474 return retval ?: nbytes; 2475 } 2476 2477 /* 2478 * for the common functions, 'private' gives the type of file 2479 */ 2480 2481 static struct cftype legacy_files[] = { 2482 { 2483 .name = "cpus", 2484 .seq_show = cpuset_common_seq_show, 2485 .write = cpuset_write_resmask, 2486 .max_write_len = (100U + 6 * NR_CPUS), 2487 .private = FILE_CPULIST, 2488 }, 2489 2490 { 2491 .name = "mems", 2492 .seq_show = cpuset_common_seq_show, 2493 .write = cpuset_write_resmask, 2494 .max_write_len = (100U + 6 * MAX_NUMNODES), 2495 .private = FILE_MEMLIST, 2496 }, 2497 2498 { 2499 .name = "effective_cpus", 2500 .seq_show = cpuset_common_seq_show, 2501 .private = FILE_EFFECTIVE_CPULIST, 2502 }, 2503 2504 { 2505 .name = "effective_mems", 2506 .seq_show = cpuset_common_seq_show, 2507 .private = FILE_EFFECTIVE_MEMLIST, 2508 }, 2509 2510 { 2511 .name = "cpu_exclusive", 2512 .read_u64 = cpuset_read_u64, 2513 .write_u64 = cpuset_write_u64, 2514 .private = FILE_CPU_EXCLUSIVE, 2515 }, 2516 2517 { 2518 .name = "mem_exclusive", 2519 .read_u64 = cpuset_read_u64, 2520 .write_u64 = cpuset_write_u64, 2521 .private = FILE_MEM_EXCLUSIVE, 2522 }, 2523 2524 { 2525 .name = "mem_hardwall", 2526 .read_u64 = cpuset_read_u64, 2527 .write_u64 = cpuset_write_u64, 2528 .private = FILE_MEM_HARDWALL, 2529 }, 2530 2531 { 2532 .name = "sched_load_balance", 2533 .read_u64 = cpuset_read_u64, 2534 .write_u64 = cpuset_write_u64, 2535 .private = FILE_SCHED_LOAD_BALANCE, 2536 }, 2537 2538 { 2539 .name = "sched_relax_domain_level", 2540 .read_s64 = cpuset_read_s64, 2541 .write_s64 = cpuset_write_s64, 2542 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL, 2543 }, 2544 2545 { 2546 .name = "memory_migrate", 2547 .read_u64 = cpuset_read_u64, 2548 .write_u64 = cpuset_write_u64, 2549 .private = FILE_MEMORY_MIGRATE, 2550 }, 2551 2552 { 2553 .name = "memory_pressure", 2554 .read_u64 = cpuset_read_u64, 2555 .private = FILE_MEMORY_PRESSURE, 2556 }, 2557 2558 { 2559 .name = "memory_spread_page", 2560 .read_u64 = cpuset_read_u64, 2561 .write_u64 = cpuset_write_u64, 2562 .private = FILE_SPREAD_PAGE, 2563 }, 2564 2565 { 2566 .name = "memory_spread_slab", 2567 .read_u64 = cpuset_read_u64, 2568 .write_u64 = cpuset_write_u64, 2569 .private = FILE_SPREAD_SLAB, 2570 }, 2571 2572 { 2573 .name = "memory_pressure_enabled", 2574 .flags = CFTYPE_ONLY_ON_ROOT, 2575 .read_u64 = cpuset_read_u64, 2576 .write_u64 = cpuset_write_u64, 2577 .private = FILE_MEMORY_PRESSURE_ENABLED, 2578 }, 2579 2580 { } /* terminate */ 2581 }; 2582 2583 /* 2584 * This is currently a minimal set for the default hierarchy. It can be 2585 * expanded later on by migrating more features and control files from v1. 2586 */ 2587 static struct cftype dfl_files[] = { 2588 { 2589 .name = "cpus", 2590 .seq_show = cpuset_common_seq_show, 2591 .write = cpuset_write_resmask, 2592 .max_write_len = (100U + 6 * NR_CPUS), 2593 .private = FILE_CPULIST, 2594 .flags = CFTYPE_NOT_ON_ROOT, 2595 }, 2596 2597 { 2598 .name = "mems", 2599 .seq_show = cpuset_common_seq_show, 2600 .write = cpuset_write_resmask, 2601 .max_write_len = (100U + 6 * MAX_NUMNODES), 2602 .private = FILE_MEMLIST, 2603 .flags = CFTYPE_NOT_ON_ROOT, 2604 }, 2605 2606 { 2607 .name = "cpus.effective", 2608 .seq_show = cpuset_common_seq_show, 2609 .private = FILE_EFFECTIVE_CPULIST, 2610 }, 2611 2612 { 2613 .name = "mems.effective", 2614 .seq_show = cpuset_common_seq_show, 2615 .private = FILE_EFFECTIVE_MEMLIST, 2616 }, 2617 2618 { 2619 .name = "cpus.partition", 2620 .seq_show = sched_partition_show, 2621 .write = sched_partition_write, 2622 .private = FILE_PARTITION_ROOT, 2623 .flags = CFTYPE_NOT_ON_ROOT, 2624 }, 2625 2626 { 2627 .name = "cpus.subpartitions", 2628 .seq_show = cpuset_common_seq_show, 2629 .private = FILE_SUBPARTS_CPULIST, 2630 .flags = CFTYPE_DEBUG, 2631 }, 2632 2633 { } /* terminate */ 2634 }; 2635 2636 2637 /* 2638 * cpuset_css_alloc - allocate a cpuset css 2639 * cgrp: control group that the new cpuset will be part of 2640 */ 2641 2642 static struct cgroup_subsys_state * 2643 cpuset_css_alloc(struct cgroup_subsys_state *parent_css) 2644 { 2645 struct cpuset *cs; 2646 2647 if (!parent_css) 2648 return &top_cpuset.css; 2649 2650 cs = kzalloc(sizeof(*cs), GFP_KERNEL); 2651 if (!cs) 2652 return ERR_PTR(-ENOMEM); 2653 2654 if (alloc_cpumasks(cs, NULL)) { 2655 kfree(cs); 2656 return ERR_PTR(-ENOMEM); 2657 } 2658 2659 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 2660 nodes_clear(cs->mems_allowed); 2661 nodes_clear(cs->effective_mems); 2662 fmeter_init(&cs->fmeter); 2663 cs->relax_domain_level = -1; 2664 2665 return &cs->css; 2666 } 2667 2668 static int cpuset_css_online(struct cgroup_subsys_state *css) 2669 { 2670 struct cpuset *cs = css_cs(css); 2671 struct cpuset *parent = parent_cs(cs); 2672 struct cpuset *tmp_cs; 2673 struct cgroup_subsys_state *pos_css; 2674 2675 if (!parent) 2676 return 0; 2677 2678 mutex_lock(&cpuset_mutex); 2679 2680 set_bit(CS_ONLINE, &cs->flags); 2681 if (is_spread_page(parent)) 2682 set_bit(CS_SPREAD_PAGE, &cs->flags); 2683 if (is_spread_slab(parent)) 2684 set_bit(CS_SPREAD_SLAB, &cs->flags); 2685 2686 cpuset_inc(); 2687 2688 spin_lock_irq(&callback_lock); 2689 if (is_in_v2_mode()) { 2690 cpumask_copy(cs->effective_cpus, parent->effective_cpus); 2691 cs->effective_mems = parent->effective_mems; 2692 cs->use_parent_ecpus = true; 2693 parent->child_ecpus_count++; 2694 } 2695 spin_unlock_irq(&callback_lock); 2696 2697 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags)) 2698 goto out_unlock; 2699 2700 /* 2701 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is 2702 * set. This flag handling is implemented in cgroup core for 2703 * histrical reasons - the flag may be specified during mount. 2704 * 2705 * Currently, if any sibling cpusets have exclusive cpus or mem, we 2706 * refuse to clone the configuration - thereby refusing the task to 2707 * be entered, and as a result refusing the sys_unshare() or 2708 * clone() which initiated it. If this becomes a problem for some 2709 * users who wish to allow that scenario, then this could be 2710 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive 2711 * (and likewise for mems) to the new cgroup. 2712 */ 2713 rcu_read_lock(); 2714 cpuset_for_each_child(tmp_cs, pos_css, parent) { 2715 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) { 2716 rcu_read_unlock(); 2717 goto out_unlock; 2718 } 2719 } 2720 rcu_read_unlock(); 2721 2722 spin_lock_irq(&callback_lock); 2723 cs->mems_allowed = parent->mems_allowed; 2724 cs->effective_mems = parent->mems_allowed; 2725 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed); 2726 cpumask_copy(cs->effective_cpus, parent->cpus_allowed); 2727 spin_unlock_irq(&callback_lock); 2728 out_unlock: 2729 mutex_unlock(&cpuset_mutex); 2730 return 0; 2731 } 2732 2733 /* 2734 * If the cpuset being removed has its flag 'sched_load_balance' 2735 * enabled, then simulate turning sched_load_balance off, which 2736 * will call rebuild_sched_domains_locked(). That is not needed 2737 * in the default hierarchy where only changes in partition 2738 * will cause repartitioning. 2739 * 2740 * If the cpuset has the 'sched.partition' flag enabled, simulate 2741 * turning 'sched.partition" off. 2742 */ 2743 2744 static void cpuset_css_offline(struct cgroup_subsys_state *css) 2745 { 2746 struct cpuset *cs = css_cs(css); 2747 2748 mutex_lock(&cpuset_mutex); 2749 2750 if (is_partition_root(cs)) 2751 update_prstate(cs, 0); 2752 2753 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && 2754 is_sched_load_balance(cs)) 2755 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); 2756 2757 if (cs->use_parent_ecpus) { 2758 struct cpuset *parent = parent_cs(cs); 2759 2760 cs->use_parent_ecpus = false; 2761 parent->child_ecpus_count--; 2762 } 2763 2764 cpuset_dec(); 2765 clear_bit(CS_ONLINE, &cs->flags); 2766 2767 mutex_unlock(&cpuset_mutex); 2768 } 2769 2770 static void cpuset_css_free(struct cgroup_subsys_state *css) 2771 { 2772 struct cpuset *cs = css_cs(css); 2773 2774 free_cpuset(cs); 2775 } 2776 2777 static void cpuset_bind(struct cgroup_subsys_state *root_css) 2778 { 2779 mutex_lock(&cpuset_mutex); 2780 spin_lock_irq(&callback_lock); 2781 2782 if (is_in_v2_mode()) { 2783 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask); 2784 top_cpuset.mems_allowed = node_possible_map; 2785 } else { 2786 cpumask_copy(top_cpuset.cpus_allowed, 2787 top_cpuset.effective_cpus); 2788 top_cpuset.mems_allowed = top_cpuset.effective_mems; 2789 } 2790 2791 spin_unlock_irq(&callback_lock); 2792 mutex_unlock(&cpuset_mutex); 2793 } 2794 2795 /* 2796 * Make sure the new task conform to the current state of its parent, 2797 * which could have been changed by cpuset just after it inherits the 2798 * state from the parent and before it sits on the cgroup's task list. 2799 */ 2800 static void cpuset_fork(struct task_struct *task) 2801 { 2802 if (task_css_is_root(task, cpuset_cgrp_id)) 2803 return; 2804 2805 set_cpus_allowed_ptr(task, ¤t->cpus_allowed); 2806 task->mems_allowed = current->mems_allowed; 2807 } 2808 2809 struct cgroup_subsys cpuset_cgrp_subsys = { 2810 .css_alloc = cpuset_css_alloc, 2811 .css_online = cpuset_css_online, 2812 .css_offline = cpuset_css_offline, 2813 .css_free = cpuset_css_free, 2814 .can_attach = cpuset_can_attach, 2815 .cancel_attach = cpuset_cancel_attach, 2816 .attach = cpuset_attach, 2817 .post_attach = cpuset_post_attach, 2818 .bind = cpuset_bind, 2819 .fork = cpuset_fork, 2820 .legacy_cftypes = legacy_files, 2821 .dfl_cftypes = dfl_files, 2822 .early_init = true, 2823 .threaded = true, 2824 }; 2825 2826 /** 2827 * cpuset_init - initialize cpusets at system boot 2828 * 2829 * Description: Initialize top_cpuset and the cpuset internal file system, 2830 **/ 2831 2832 int __init cpuset_init(void) 2833 { 2834 int err = 0; 2835 2836 BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)); 2837 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL)); 2838 BUG_ON(!zalloc_cpumask_var(&top_cpuset.subparts_cpus, GFP_KERNEL)); 2839 2840 cpumask_setall(top_cpuset.cpus_allowed); 2841 nodes_setall(top_cpuset.mems_allowed); 2842 cpumask_setall(top_cpuset.effective_cpus); 2843 nodes_setall(top_cpuset.effective_mems); 2844 2845 fmeter_init(&top_cpuset.fmeter); 2846 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); 2847 top_cpuset.relax_domain_level = -1; 2848 2849 err = register_filesystem(&cpuset_fs_type); 2850 if (err < 0) 2851 return err; 2852 2853 BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)); 2854 2855 return 0; 2856 } 2857 2858 /* 2859 * If CPU and/or memory hotplug handlers, below, unplug any CPUs 2860 * or memory nodes, we need to walk over the cpuset hierarchy, 2861 * removing that CPU or node from all cpusets. If this removes the 2862 * last CPU or node from a cpuset, then move the tasks in the empty 2863 * cpuset to its next-highest non-empty parent. 2864 */ 2865 static void remove_tasks_in_empty_cpuset(struct cpuset *cs) 2866 { 2867 struct cpuset *parent; 2868 2869 /* 2870 * Find its next-highest non-empty parent, (top cpuset 2871 * has online cpus, so can't be empty). 2872 */ 2873 parent = parent_cs(cs); 2874 while (cpumask_empty(parent->cpus_allowed) || 2875 nodes_empty(parent->mems_allowed)) 2876 parent = parent_cs(parent); 2877 2878 if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) { 2879 pr_err("cpuset: failed to transfer tasks out of empty cpuset "); 2880 pr_cont_cgroup_name(cs->css.cgroup); 2881 pr_cont("\n"); 2882 } 2883 } 2884 2885 static void 2886 hotplug_update_tasks_legacy(struct cpuset *cs, 2887 struct cpumask *new_cpus, nodemask_t *new_mems, 2888 bool cpus_updated, bool mems_updated) 2889 { 2890 bool is_empty; 2891 2892 spin_lock_irq(&callback_lock); 2893 cpumask_copy(cs->cpus_allowed, new_cpus); 2894 cpumask_copy(cs->effective_cpus, new_cpus); 2895 cs->mems_allowed = *new_mems; 2896 cs->effective_mems = *new_mems; 2897 spin_unlock_irq(&callback_lock); 2898 2899 /* 2900 * Don't call update_tasks_cpumask() if the cpuset becomes empty, 2901 * as the tasks will be migratecd to an ancestor. 2902 */ 2903 if (cpus_updated && !cpumask_empty(cs->cpus_allowed)) 2904 update_tasks_cpumask(cs); 2905 if (mems_updated && !nodes_empty(cs->mems_allowed)) 2906 update_tasks_nodemask(cs); 2907 2908 is_empty = cpumask_empty(cs->cpus_allowed) || 2909 nodes_empty(cs->mems_allowed); 2910 2911 mutex_unlock(&cpuset_mutex); 2912 2913 /* 2914 * Move tasks to the nearest ancestor with execution resources, 2915 * This is full cgroup operation which will also call back into 2916 * cpuset. Should be done outside any lock. 2917 */ 2918 if (is_empty) 2919 remove_tasks_in_empty_cpuset(cs); 2920 2921 mutex_lock(&cpuset_mutex); 2922 } 2923 2924 static void 2925 hotplug_update_tasks(struct cpuset *cs, 2926 struct cpumask *new_cpus, nodemask_t *new_mems, 2927 bool cpus_updated, bool mems_updated) 2928 { 2929 if (cpumask_empty(new_cpus)) 2930 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus); 2931 if (nodes_empty(*new_mems)) 2932 *new_mems = parent_cs(cs)->effective_mems; 2933 2934 spin_lock_irq(&callback_lock); 2935 cpumask_copy(cs->effective_cpus, new_cpus); 2936 cs->effective_mems = *new_mems; 2937 spin_unlock_irq(&callback_lock); 2938 2939 if (cpus_updated) 2940 update_tasks_cpumask(cs); 2941 if (mems_updated) 2942 update_tasks_nodemask(cs); 2943 } 2944 2945 static bool force_rebuild; 2946 2947 void cpuset_force_rebuild(void) 2948 { 2949 force_rebuild = true; 2950 } 2951 2952 /** 2953 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug 2954 * @cs: cpuset in interest 2955 * @tmp: the tmpmasks structure pointer 2956 * 2957 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone 2958 * offline, update @cs accordingly. If @cs ends up with no CPU or memory, 2959 * all its tasks are moved to the nearest ancestor with both resources. 2960 */ 2961 static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) 2962 { 2963 static cpumask_t new_cpus; 2964 static nodemask_t new_mems; 2965 bool cpus_updated; 2966 bool mems_updated; 2967 struct cpuset *parent; 2968 retry: 2969 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0); 2970 2971 mutex_lock(&cpuset_mutex); 2972 2973 /* 2974 * We have raced with task attaching. We wait until attaching 2975 * is finished, so we won't attach a task to an empty cpuset. 2976 */ 2977 if (cs->attach_in_progress) { 2978 mutex_unlock(&cpuset_mutex); 2979 goto retry; 2980 } 2981 2982 parent = parent_cs(cs); 2983 compute_effective_cpumask(&new_cpus, cs, parent); 2984 nodes_and(new_mems, cs->mems_allowed, parent->effective_mems); 2985 2986 if (cs->nr_subparts_cpus) 2987 /* 2988 * Make sure that CPUs allocated to child partitions 2989 * do not show up in effective_cpus. 2990 */ 2991 cpumask_andnot(&new_cpus, &new_cpus, cs->subparts_cpus); 2992 2993 if (!tmp || !cs->partition_root_state) 2994 goto update_tasks; 2995 2996 /* 2997 * In the unlikely event that a partition root has empty 2998 * effective_cpus or its parent becomes erroneous, we have to 2999 * transition it to the erroneous state. 3000 */ 3001 if (is_partition_root(cs) && (cpumask_empty(&new_cpus) || 3002 (parent->partition_root_state == PRS_ERROR))) { 3003 if (cs->nr_subparts_cpus) { 3004 cs->nr_subparts_cpus = 0; 3005 cpumask_clear(cs->subparts_cpus); 3006 compute_effective_cpumask(&new_cpus, cs, parent); 3007 } 3008 3009 /* 3010 * If the effective_cpus is empty because the child 3011 * partitions take away all the CPUs, we can keep 3012 * the current partition and let the child partitions 3013 * fight for available CPUs. 3014 */ 3015 if ((parent->partition_root_state == PRS_ERROR) || 3016 cpumask_empty(&new_cpus)) { 3017 update_parent_subparts_cpumask(cs, partcmd_disable, 3018 NULL, tmp); 3019 cs->partition_root_state = PRS_ERROR; 3020 } 3021 cpuset_force_rebuild(); 3022 } 3023 3024 /* 3025 * On the other hand, an erroneous partition root may be transitioned 3026 * back to a regular one or a partition root with no CPU allocated 3027 * from the parent may change to erroneous. 3028 */ 3029 if (is_partition_root(parent) && 3030 ((cs->partition_root_state == PRS_ERROR) || 3031 !cpumask_intersects(&new_cpus, parent->subparts_cpus)) && 3032 update_parent_subparts_cpumask(cs, partcmd_update, NULL, tmp)) 3033 cpuset_force_rebuild(); 3034 3035 update_tasks: 3036 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus); 3037 mems_updated = !nodes_equal(new_mems, cs->effective_mems); 3038 3039 if (is_in_v2_mode()) 3040 hotplug_update_tasks(cs, &new_cpus, &new_mems, 3041 cpus_updated, mems_updated); 3042 else 3043 hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems, 3044 cpus_updated, mems_updated); 3045 3046 mutex_unlock(&cpuset_mutex); 3047 } 3048 3049 /** 3050 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset 3051 * 3052 * This function is called after either CPU or memory configuration has 3053 * changed and updates cpuset accordingly. The top_cpuset is always 3054 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in 3055 * order to make cpusets transparent (of no affect) on systems that are 3056 * actively using CPU hotplug but making no active use of cpusets. 3057 * 3058 * Non-root cpusets are only affected by offlining. If any CPUs or memory 3059 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on 3060 * all descendants. 3061 * 3062 * Note that CPU offlining during suspend is ignored. We don't modify 3063 * cpusets across suspend/resume cycles at all. 3064 */ 3065 static void cpuset_hotplug_workfn(struct work_struct *work) 3066 { 3067 static cpumask_t new_cpus; 3068 static nodemask_t new_mems; 3069 bool cpus_updated, mems_updated; 3070 bool on_dfl = is_in_v2_mode(); 3071 struct tmpmasks tmp, *ptmp = NULL; 3072 3073 if (on_dfl && !alloc_cpumasks(NULL, &tmp)) 3074 ptmp = &tmp; 3075 3076 mutex_lock(&cpuset_mutex); 3077 3078 /* fetch the available cpus/mems and find out which changed how */ 3079 cpumask_copy(&new_cpus, cpu_active_mask); 3080 new_mems = node_states[N_MEMORY]; 3081 3082 /* 3083 * If subparts_cpus is populated, it is likely that the check below 3084 * will produce a false positive on cpus_updated when the cpu list 3085 * isn't changed. It is extra work, but it is better to be safe. 3086 */ 3087 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus); 3088 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems); 3089 3090 /* synchronize cpus_allowed to cpu_active_mask */ 3091 if (cpus_updated) { 3092 spin_lock_irq(&callback_lock); 3093 if (!on_dfl) 3094 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); 3095 /* 3096 * Make sure that CPUs allocated to child partitions 3097 * do not show up in effective_cpus. If no CPU is left, 3098 * we clear the subparts_cpus & let the child partitions 3099 * fight for the CPUs again. 3100 */ 3101 if (top_cpuset.nr_subparts_cpus) { 3102 if (cpumask_subset(&new_cpus, 3103 top_cpuset.subparts_cpus)) { 3104 top_cpuset.nr_subparts_cpus = 0; 3105 cpumask_clear(top_cpuset.subparts_cpus); 3106 } else { 3107 cpumask_andnot(&new_cpus, &new_cpus, 3108 top_cpuset.subparts_cpus); 3109 } 3110 } 3111 cpumask_copy(top_cpuset.effective_cpus, &new_cpus); 3112 spin_unlock_irq(&callback_lock); 3113 /* we don't mess with cpumasks of tasks in top_cpuset */ 3114 } 3115 3116 /* synchronize mems_allowed to N_MEMORY */ 3117 if (mems_updated) { 3118 spin_lock_irq(&callback_lock); 3119 if (!on_dfl) 3120 top_cpuset.mems_allowed = new_mems; 3121 top_cpuset.effective_mems = new_mems; 3122 spin_unlock_irq(&callback_lock); 3123 update_tasks_nodemask(&top_cpuset); 3124 } 3125 3126 mutex_unlock(&cpuset_mutex); 3127 3128 /* if cpus or mems changed, we need to propagate to descendants */ 3129 if (cpus_updated || mems_updated) { 3130 struct cpuset *cs; 3131 struct cgroup_subsys_state *pos_css; 3132 3133 rcu_read_lock(); 3134 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { 3135 if (cs == &top_cpuset || !css_tryget_online(&cs->css)) 3136 continue; 3137 rcu_read_unlock(); 3138 3139 cpuset_hotplug_update_tasks(cs, ptmp); 3140 3141 rcu_read_lock(); 3142 css_put(&cs->css); 3143 } 3144 rcu_read_unlock(); 3145 } 3146 3147 /* rebuild sched domains if cpus_allowed has changed */ 3148 if (cpus_updated || force_rebuild) { 3149 force_rebuild = false; 3150 rebuild_sched_domains(); 3151 } 3152 3153 free_cpumasks(NULL, ptmp); 3154 } 3155 3156 void cpuset_update_active_cpus(void) 3157 { 3158 /* 3159 * We're inside cpu hotplug critical region which usually nests 3160 * inside cgroup synchronization. Bounce actual hotplug processing 3161 * to a work item to avoid reverse locking order. 3162 */ 3163 schedule_work(&cpuset_hotplug_work); 3164 } 3165 3166 void cpuset_wait_for_hotplug(void) 3167 { 3168 flush_work(&cpuset_hotplug_work); 3169 } 3170 3171 /* 3172 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. 3173 * Call this routine anytime after node_states[N_MEMORY] changes. 3174 * See cpuset_update_active_cpus() for CPU hotplug handling. 3175 */ 3176 static int cpuset_track_online_nodes(struct notifier_block *self, 3177 unsigned long action, void *arg) 3178 { 3179 schedule_work(&cpuset_hotplug_work); 3180 return NOTIFY_OK; 3181 } 3182 3183 static struct notifier_block cpuset_track_online_nodes_nb = { 3184 .notifier_call = cpuset_track_online_nodes, 3185 .priority = 10, /* ??! */ 3186 }; 3187 3188 /** 3189 * cpuset_init_smp - initialize cpus_allowed 3190 * 3191 * Description: Finish top cpuset after cpu, node maps are initialized 3192 */ 3193 void __init cpuset_init_smp(void) 3194 { 3195 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask); 3196 top_cpuset.mems_allowed = node_states[N_MEMORY]; 3197 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed; 3198 3199 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask); 3200 top_cpuset.effective_mems = node_states[N_MEMORY]; 3201 3202 register_hotmemory_notifier(&cpuset_track_online_nodes_nb); 3203 3204 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0); 3205 BUG_ON(!cpuset_migrate_mm_wq); 3206 } 3207 3208 /** 3209 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. 3210 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. 3211 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. 3212 * 3213 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset 3214 * attached to the specified @tsk. Guaranteed to return some non-empty 3215 * subset of cpu_online_mask, even if this means going outside the 3216 * tasks cpuset. 3217 **/ 3218 3219 void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) 3220 { 3221 unsigned long flags; 3222 3223 spin_lock_irqsave(&callback_lock, flags); 3224 rcu_read_lock(); 3225 guarantee_online_cpus(task_cs(tsk), pmask); 3226 rcu_read_unlock(); 3227 spin_unlock_irqrestore(&callback_lock, flags); 3228 } 3229 3230 void cpuset_cpus_allowed_fallback(struct task_struct *tsk) 3231 { 3232 rcu_read_lock(); 3233 do_set_cpus_allowed(tsk, task_cs(tsk)->effective_cpus); 3234 rcu_read_unlock(); 3235 3236 /* 3237 * We own tsk->cpus_allowed, nobody can change it under us. 3238 * 3239 * But we used cs && cs->cpus_allowed lockless and thus can 3240 * race with cgroup_attach_task() or update_cpumask() and get 3241 * the wrong tsk->cpus_allowed. However, both cases imply the 3242 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() 3243 * which takes task_rq_lock(). 3244 * 3245 * If we are called after it dropped the lock we must see all 3246 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary 3247 * set any mask even if it is not right from task_cs() pov, 3248 * the pending set_cpus_allowed_ptr() will fix things. 3249 * 3250 * select_fallback_rq() will fix things ups and set cpu_possible_mask 3251 * if required. 3252 */ 3253 } 3254 3255 void __init cpuset_init_current_mems_allowed(void) 3256 { 3257 nodes_setall(current->mems_allowed); 3258 } 3259 3260 /** 3261 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. 3262 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. 3263 * 3264 * Description: Returns the nodemask_t mems_allowed of the cpuset 3265 * attached to the specified @tsk. Guaranteed to return some non-empty 3266 * subset of node_states[N_MEMORY], even if this means going outside the 3267 * tasks cpuset. 3268 **/ 3269 3270 nodemask_t cpuset_mems_allowed(struct task_struct *tsk) 3271 { 3272 nodemask_t mask; 3273 unsigned long flags; 3274 3275 spin_lock_irqsave(&callback_lock, flags); 3276 rcu_read_lock(); 3277 guarantee_online_mems(task_cs(tsk), &mask); 3278 rcu_read_unlock(); 3279 spin_unlock_irqrestore(&callback_lock, flags); 3280 3281 return mask; 3282 } 3283 3284 /** 3285 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed 3286 * @nodemask: the nodemask to be checked 3287 * 3288 * Are any of the nodes in the nodemask allowed in current->mems_allowed? 3289 */ 3290 int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask) 3291 { 3292 return nodes_intersects(*nodemask, current->mems_allowed); 3293 } 3294 3295 /* 3296 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or 3297 * mem_hardwall ancestor to the specified cpuset. Call holding 3298 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall 3299 * (an unusual configuration), then returns the root cpuset. 3300 */ 3301 static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs) 3302 { 3303 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs)) 3304 cs = parent_cs(cs); 3305 return cs; 3306 } 3307 3308 /** 3309 * cpuset_node_allowed - Can we allocate on a memory node? 3310 * @node: is this an allowed node? 3311 * @gfp_mask: memory allocation flags 3312 * 3313 * If we're in interrupt, yes, we can always allocate. If @node is set in 3314 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this 3315 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset, 3316 * yes. If current has access to memory reserves as an oom victim, yes. 3317 * Otherwise, no. 3318 * 3319 * GFP_USER allocations are marked with the __GFP_HARDWALL bit, 3320 * and do not allow allocations outside the current tasks cpuset 3321 * unless the task has been OOM killed. 3322 * GFP_KERNEL allocations are not so marked, so can escape to the 3323 * nearest enclosing hardwalled ancestor cpuset. 3324 * 3325 * Scanning up parent cpusets requires callback_lock. The 3326 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit 3327 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the 3328 * current tasks mems_allowed came up empty on the first pass over 3329 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the 3330 * cpuset are short of memory, might require taking the callback_lock. 3331 * 3332 * The first call here from mm/page_alloc:get_page_from_freelist() 3333 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, 3334 * so no allocation on a node outside the cpuset is allowed (unless 3335 * in interrupt, of course). 3336 * 3337 * The second pass through get_page_from_freelist() doesn't even call 3338 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() 3339 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set 3340 * in alloc_flags. That logic and the checks below have the combined 3341 * affect that: 3342 * in_interrupt - any node ok (current task context irrelevant) 3343 * GFP_ATOMIC - any node ok 3344 * tsk_is_oom_victim - any node ok 3345 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok 3346 * GFP_USER - only nodes in current tasks mems allowed ok. 3347 */ 3348 bool __cpuset_node_allowed(int node, gfp_t gfp_mask) 3349 { 3350 struct cpuset *cs; /* current cpuset ancestors */ 3351 int allowed; /* is allocation in zone z allowed? */ 3352 unsigned long flags; 3353 3354 if (in_interrupt()) 3355 return true; 3356 if (node_isset(node, current->mems_allowed)) 3357 return true; 3358 /* 3359 * Allow tasks that have access to memory reserves because they have 3360 * been OOM killed to get memory anywhere. 3361 */ 3362 if (unlikely(tsk_is_oom_victim(current))) 3363 return true; 3364 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ 3365 return false; 3366 3367 if (current->flags & PF_EXITING) /* Let dying task have memory */ 3368 return true; 3369 3370 /* Not hardwall and node outside mems_allowed: scan up cpusets */ 3371 spin_lock_irqsave(&callback_lock, flags); 3372 3373 rcu_read_lock(); 3374 cs = nearest_hardwall_ancestor(task_cs(current)); 3375 allowed = node_isset(node, cs->mems_allowed); 3376 rcu_read_unlock(); 3377 3378 spin_unlock_irqrestore(&callback_lock, flags); 3379 return allowed; 3380 } 3381 3382 /** 3383 * cpuset_mem_spread_node() - On which node to begin search for a file page 3384 * cpuset_slab_spread_node() - On which node to begin search for a slab page 3385 * 3386 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for 3387 * tasks in a cpuset with is_spread_page or is_spread_slab set), 3388 * and if the memory allocation used cpuset_mem_spread_node() 3389 * to determine on which node to start looking, as it will for 3390 * certain page cache or slab cache pages such as used for file 3391 * system buffers and inode caches, then instead of starting on the 3392 * local node to look for a free page, rather spread the starting 3393 * node around the tasks mems_allowed nodes. 3394 * 3395 * We don't have to worry about the returned node being offline 3396 * because "it can't happen", and even if it did, it would be ok. 3397 * 3398 * The routines calling guarantee_online_mems() are careful to 3399 * only set nodes in task->mems_allowed that are online. So it 3400 * should not be possible for the following code to return an 3401 * offline node. But if it did, that would be ok, as this routine 3402 * is not returning the node where the allocation must be, only 3403 * the node where the search should start. The zonelist passed to 3404 * __alloc_pages() will include all nodes. If the slab allocator 3405 * is passed an offline node, it will fall back to the local node. 3406 * See kmem_cache_alloc_node(). 3407 */ 3408 3409 static int cpuset_spread_node(int *rotor) 3410 { 3411 return *rotor = next_node_in(*rotor, current->mems_allowed); 3412 } 3413 3414 int cpuset_mem_spread_node(void) 3415 { 3416 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) 3417 current->cpuset_mem_spread_rotor = 3418 node_random(¤t->mems_allowed); 3419 3420 return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); 3421 } 3422 3423 int cpuset_slab_spread_node(void) 3424 { 3425 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE) 3426 current->cpuset_slab_spread_rotor = 3427 node_random(¤t->mems_allowed); 3428 3429 return cpuset_spread_node(¤t->cpuset_slab_spread_rotor); 3430 } 3431 3432 EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); 3433 3434 /** 3435 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? 3436 * @tsk1: pointer to task_struct of some task. 3437 * @tsk2: pointer to task_struct of some other task. 3438 * 3439 * Description: Return true if @tsk1's mems_allowed intersects the 3440 * mems_allowed of @tsk2. Used by the OOM killer to determine if 3441 * one of the task's memory usage might impact the memory available 3442 * to the other. 3443 **/ 3444 3445 int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, 3446 const struct task_struct *tsk2) 3447 { 3448 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); 3449 } 3450 3451 /** 3452 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed 3453 * 3454 * Description: Prints current's name, cpuset name, and cached copy of its 3455 * mems_allowed to the kernel log. 3456 */ 3457 void cpuset_print_current_mems_allowed(void) 3458 { 3459 struct cgroup *cgrp; 3460 3461 rcu_read_lock(); 3462 3463 cgrp = task_cs(current)->css.cgroup; 3464 pr_cont(",cpuset="); 3465 pr_cont_cgroup_name(cgrp); 3466 pr_cont(",mems_allowed=%*pbl", 3467 nodemask_pr_args(¤t->mems_allowed)); 3468 3469 rcu_read_unlock(); 3470 } 3471 3472 /* 3473 * Collection of memory_pressure is suppressed unless 3474 * this flag is enabled by writing "1" to the special 3475 * cpuset file 'memory_pressure_enabled' in the root cpuset. 3476 */ 3477 3478 int cpuset_memory_pressure_enabled __read_mostly; 3479 3480 /** 3481 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. 3482 * 3483 * Keep a running average of the rate of synchronous (direct) 3484 * page reclaim efforts initiated by tasks in each cpuset. 3485 * 3486 * This represents the rate at which some task in the cpuset 3487 * ran low on memory on all nodes it was allowed to use, and 3488 * had to enter the kernels page reclaim code in an effort to 3489 * create more free memory by tossing clean pages or swapping 3490 * or writing dirty pages. 3491 * 3492 * Display to user space in the per-cpuset read-only file 3493 * "memory_pressure". Value displayed is an integer 3494 * representing the recent rate of entry into the synchronous 3495 * (direct) page reclaim by any task attached to the cpuset. 3496 **/ 3497 3498 void __cpuset_memory_pressure_bump(void) 3499 { 3500 rcu_read_lock(); 3501 fmeter_markevent(&task_cs(current)->fmeter); 3502 rcu_read_unlock(); 3503 } 3504 3505 #ifdef CONFIG_PROC_PID_CPUSET 3506 /* 3507 * proc_cpuset_show() 3508 * - Print tasks cpuset path into seq_file. 3509 * - Used for /proc/<pid>/cpuset. 3510 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it 3511 * doesn't really matter if tsk->cpuset changes after we read it, 3512 * and we take cpuset_mutex, keeping cpuset_attach() from changing it 3513 * anyway. 3514 */ 3515 int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns, 3516 struct pid *pid, struct task_struct *tsk) 3517 { 3518 char *buf; 3519 struct cgroup_subsys_state *css; 3520 int retval; 3521 3522 retval = -ENOMEM; 3523 buf = kmalloc(PATH_MAX, GFP_KERNEL); 3524 if (!buf) 3525 goto out; 3526 3527 css = task_get_css(tsk, cpuset_cgrp_id); 3528 retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX, 3529 current->nsproxy->cgroup_ns); 3530 css_put(css); 3531 if (retval >= PATH_MAX) 3532 retval = -ENAMETOOLONG; 3533 if (retval < 0) 3534 goto out_free; 3535 seq_puts(m, buf); 3536 seq_putc(m, '\n'); 3537 retval = 0; 3538 out_free: 3539 kfree(buf); 3540 out: 3541 return retval; 3542 } 3543 #endif /* CONFIG_PROC_PID_CPUSET */ 3544 3545 /* Display task mems_allowed in /proc/<pid>/status file. */ 3546 void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) 3547 { 3548 seq_printf(m, "Mems_allowed:\t%*pb\n", 3549 nodemask_pr_args(&task->mems_allowed)); 3550 seq_printf(m, "Mems_allowed_list:\t%*pbl\n", 3551 nodemask_pr_args(&task->mems_allowed)); 3552 } 3553