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 #include "cpuset-internal.h" 25 26 #include <linux/init.h> 27 #include <linux/interrupt.h> 28 #include <linux/kernel.h> 29 #include <linux/mempolicy.h> 30 #include <linux/mm.h> 31 #include <linux/memory.h> 32 #include <linux/export.h> 33 #include <linux/rcupdate.h> 34 #include <linux/sched.h> 35 #include <linux/sched/deadline.h> 36 #include <linux/sched/mm.h> 37 #include <linux/sched/task.h> 38 #include <linux/security.h> 39 #include <linux/oom.h> 40 #include <linux/sched/isolation.h> 41 #include <linux/wait.h> 42 #include <linux/workqueue.h> 43 44 DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key); 45 DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key); 46 47 /* 48 * There could be abnormal cpuset configurations for cpu or memory 49 * node binding, add this key to provide a quick low-cost judgment 50 * of the situation. 51 */ 52 DEFINE_STATIC_KEY_FALSE(cpusets_insane_config_key); 53 54 static const char * const perr_strings[] = { 55 [PERR_INVCPUS] = "Invalid cpu list in cpuset.cpus.exclusive", 56 [PERR_INVPARENT] = "Parent is an invalid partition root", 57 [PERR_NOTPART] = "Parent is not a partition root", 58 [PERR_NOTEXCL] = "Cpu list in cpuset.cpus not exclusive", 59 [PERR_NOCPUS] = "Parent unable to distribute cpu downstream", 60 [PERR_HOTPLUG] = "No cpu available due to hotplug", 61 [PERR_CPUSEMPTY] = "cpuset.cpus and cpuset.cpus.exclusive are empty", 62 [PERR_HKEEPING] = "partition config conflicts with housekeeping setup", 63 [PERR_ACCESS] = "Enable partition not permitted", 64 [PERR_REMOTE] = "Have remote partition underneath", 65 }; 66 67 /* 68 * For local partitions, update to subpartitions_cpus & isolated_cpus is done 69 * in update_parent_effective_cpumask(). For remote partitions, it is done in 70 * the remote_partition_*() and remote_cpus_update() helpers. 71 */ 72 /* 73 * Exclusive CPUs distributed out to local or remote sub-partitions of 74 * top_cpuset 75 */ 76 static cpumask_var_t subpartitions_cpus; 77 78 /* 79 * Exclusive CPUs in isolated partitions 80 */ 81 static cpumask_var_t isolated_cpus; 82 83 /* 84 * Housekeeping (HK_TYPE_DOMAIN) CPUs at boot 85 */ 86 static cpumask_var_t boot_hk_cpus; 87 static bool have_boot_isolcpus; 88 89 /* List of remote partition root children */ 90 static struct list_head remote_children; 91 92 /* 93 * A flag to force sched domain rebuild at the end of an operation. 94 * It can be set in 95 * - update_partition_sd_lb() 96 * - update_cpumasks_hier() 97 * - cpuset_update_flag() 98 * - cpuset_hotplug_update_tasks() 99 * - cpuset_handle_hotplug() 100 * 101 * Protected by cpuset_mutex (with cpus_read_lock held) or cpus_write_lock. 102 * 103 * Note that update_relax_domain_level() in cpuset-v1.c can still call 104 * rebuild_sched_domains_locked() directly without using this flag. 105 */ 106 static bool force_sd_rebuild; 107 108 /* 109 * Partition root states: 110 * 111 * 0 - member (not a partition root) 112 * 1 - partition root 113 * 2 - partition root without load balancing (isolated) 114 * -1 - invalid partition root 115 * -2 - invalid isolated partition root 116 * 117 * There are 2 types of partitions - local or remote. Local partitions are 118 * those whose parents are partition root themselves. Setting of 119 * cpuset.cpus.exclusive are optional in setting up local partitions. 120 * Remote partitions are those whose parents are not partition roots. Passing 121 * down exclusive CPUs by setting cpuset.cpus.exclusive along its ancestor 122 * nodes are mandatory in creating a remote partition. 123 * 124 * For simplicity, a local partition can be created under a local or remote 125 * partition but a remote partition cannot have any partition root in its 126 * ancestor chain except the cgroup root. 127 */ 128 #define PRS_MEMBER 0 129 #define PRS_ROOT 1 130 #define PRS_ISOLATED 2 131 #define PRS_INVALID_ROOT -1 132 #define PRS_INVALID_ISOLATED -2 133 134 static inline bool is_prs_invalid(int prs_state) 135 { 136 return prs_state < 0; 137 } 138 139 /* 140 * Temporary cpumasks for working with partitions that are passed among 141 * functions to avoid memory allocation in inner functions. 142 */ 143 struct tmpmasks { 144 cpumask_var_t addmask, delmask; /* For partition root */ 145 cpumask_var_t new_cpus; /* For update_cpumasks_hier() */ 146 }; 147 148 void inc_dl_tasks_cs(struct task_struct *p) 149 { 150 struct cpuset *cs = task_cs(p); 151 152 cs->nr_deadline_tasks++; 153 } 154 155 void dec_dl_tasks_cs(struct task_struct *p) 156 { 157 struct cpuset *cs = task_cs(p); 158 159 cs->nr_deadline_tasks--; 160 } 161 162 static inline int is_partition_valid(const struct cpuset *cs) 163 { 164 return cs->partition_root_state > 0; 165 } 166 167 static inline int is_partition_invalid(const struct cpuset *cs) 168 { 169 return cs->partition_root_state < 0; 170 } 171 172 /* 173 * Callers should hold callback_lock to modify partition_root_state. 174 */ 175 static inline void make_partition_invalid(struct cpuset *cs) 176 { 177 if (cs->partition_root_state > 0) 178 cs->partition_root_state = -cs->partition_root_state; 179 } 180 181 /* 182 * Send notification event of whenever partition_root_state changes. 183 */ 184 static inline void notify_partition_change(struct cpuset *cs, int old_prs) 185 { 186 if (old_prs == cs->partition_root_state) 187 return; 188 cgroup_file_notify(&cs->partition_file); 189 190 /* Reset prs_err if not invalid */ 191 if (is_partition_valid(cs)) 192 WRITE_ONCE(cs->prs_err, PERR_NONE); 193 } 194 195 /* 196 * The top_cpuset is always synchronized to cpu_active_mask and we should avoid 197 * using cpu_online_mask as much as possible. An active CPU is always an online 198 * CPU, but not vice versa. cpu_active_mask and cpu_online_mask can differ 199 * during hotplug operations. A CPU is marked active at the last stage of CPU 200 * bringup (CPUHP_AP_ACTIVE). It is also the stage where cpuset hotplug code 201 * will be called to update the sched domains so that the scheduler can move 202 * a normal task to a newly active CPU or remove tasks away from a newly 203 * inactivated CPU. The online bit is set much earlier in the CPU bringup 204 * process and cleared much later in CPU teardown. 205 * 206 * If cpu_online_mask is used while a hotunplug operation is happening in 207 * parallel, we may leave an offline CPU in cpu_allowed or some other masks. 208 */ 209 static struct cpuset top_cpuset = { 210 .flags = BIT(CS_CPU_EXCLUSIVE) | 211 BIT(CS_MEM_EXCLUSIVE) | BIT(CS_SCHED_LOAD_BALANCE), 212 .partition_root_state = PRS_ROOT, 213 .relax_domain_level = -1, 214 .remote_sibling = LIST_HEAD_INIT(top_cpuset.remote_sibling), 215 }; 216 217 /* 218 * There are two global locks guarding cpuset structures - cpuset_mutex and 219 * callback_lock. The cpuset code uses only cpuset_mutex. Other kernel 220 * subsystems can use cpuset_lock()/cpuset_unlock() to prevent change to cpuset 221 * structures. Note that cpuset_mutex needs to be a mutex as it is used in 222 * paths that rely on priority inheritance (e.g. scheduler - on RT) for 223 * correctness. 224 * 225 * A task must hold both locks to modify cpusets. If a task holds 226 * cpuset_mutex, it blocks others, ensuring that it is the only task able to 227 * also acquire callback_lock and be able to modify cpusets. It can perform 228 * various checks on the cpuset structure first, knowing nothing will change. 229 * It can also allocate memory while just holding cpuset_mutex. While it is 230 * performing these checks, various callback routines can briefly acquire 231 * callback_lock to query cpusets. Once it is ready to make the changes, it 232 * takes callback_lock, blocking everyone else. 233 * 234 * Calls to the kernel memory allocator can not be made while holding 235 * callback_lock, as that would risk double tripping on callback_lock 236 * from one of the callbacks into the cpuset code from within 237 * __alloc_pages(). 238 * 239 * If a task is only holding callback_lock, then it has read-only 240 * access to cpusets. 241 * 242 * Now, the task_struct fields mems_allowed and mempolicy may be changed 243 * by other task, we use alloc_lock in the task_struct fields to protect 244 * them. 245 * 246 * The cpuset_common_seq_show() handlers only hold callback_lock across 247 * small pieces of code, such as when reading out possibly multi-word 248 * cpumasks and nodemasks. 249 */ 250 251 static DEFINE_MUTEX(cpuset_mutex); 252 253 /** 254 * cpuset_lock - Acquire the global cpuset mutex 255 * 256 * This locks the global cpuset mutex to prevent modifications to cpuset 257 * hierarchy and configurations. This helper is not enough to make modification. 258 */ 259 void cpuset_lock(void) 260 { 261 mutex_lock(&cpuset_mutex); 262 } 263 264 void cpuset_unlock(void) 265 { 266 mutex_unlock(&cpuset_mutex); 267 } 268 269 /** 270 * cpuset_full_lock - Acquire full protection for cpuset modification 271 * 272 * Takes both CPU hotplug read lock (cpus_read_lock()) and cpuset mutex 273 * to safely modify cpuset data. 274 */ 275 void cpuset_full_lock(void) 276 { 277 cpus_read_lock(); 278 mutex_lock(&cpuset_mutex); 279 } 280 281 void cpuset_full_unlock(void) 282 { 283 mutex_unlock(&cpuset_mutex); 284 cpus_read_unlock(); 285 } 286 287 static DEFINE_SPINLOCK(callback_lock); 288 289 void cpuset_callback_lock_irq(void) 290 { 291 spin_lock_irq(&callback_lock); 292 } 293 294 void cpuset_callback_unlock_irq(void) 295 { 296 spin_unlock_irq(&callback_lock); 297 } 298 299 static struct workqueue_struct *cpuset_migrate_mm_wq; 300 301 static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq); 302 303 static inline void check_insane_mems_config(nodemask_t *nodes) 304 { 305 if (!cpusets_insane_config() && 306 movable_only_nodes(nodes)) { 307 static_branch_enable_cpuslocked(&cpusets_insane_config_key); 308 pr_info("Unsupported (movable nodes only) cpuset configuration detected (nmask=%*pbl)!\n" 309 "Cpuset allocations might fail even with a lot of memory available.\n", 310 nodemask_pr_args(nodes)); 311 } 312 } 313 314 /* 315 * decrease cs->attach_in_progress. 316 * wake_up cpuset_attach_wq if cs->attach_in_progress==0. 317 */ 318 static inline void dec_attach_in_progress_locked(struct cpuset *cs) 319 { 320 lockdep_assert_held(&cpuset_mutex); 321 322 cs->attach_in_progress--; 323 if (!cs->attach_in_progress) 324 wake_up(&cpuset_attach_wq); 325 } 326 327 static inline void dec_attach_in_progress(struct cpuset *cs) 328 { 329 mutex_lock(&cpuset_mutex); 330 dec_attach_in_progress_locked(cs); 331 mutex_unlock(&cpuset_mutex); 332 } 333 334 static inline bool cpuset_v2(void) 335 { 336 return !IS_ENABLED(CONFIG_CPUSETS_V1) || 337 cgroup_subsys_on_dfl(cpuset_cgrp_subsys); 338 } 339 340 /* 341 * Cgroup v2 behavior is used on the "cpus" and "mems" control files when 342 * on default hierarchy or when the cpuset_v2_mode flag is set by mounting 343 * the v1 cpuset cgroup filesystem with the "cpuset_v2_mode" mount option. 344 * With v2 behavior, "cpus" and "mems" are always what the users have 345 * requested and won't be changed by hotplug events. Only the effective 346 * cpus or mems will be affected. 347 */ 348 static inline bool is_in_v2_mode(void) 349 { 350 return cpuset_v2() || 351 (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE); 352 } 353 354 /** 355 * partition_is_populated - check if partition has tasks 356 * @cs: partition root to be checked 357 * @excluded_child: a child cpuset to be excluded in task checking 358 * Return: true if there are tasks, false otherwise 359 * 360 * It is assumed that @cs is a valid partition root. @excluded_child should 361 * be non-NULL when this cpuset is going to become a partition itself. 362 */ 363 static inline bool partition_is_populated(struct cpuset *cs, 364 struct cpuset *excluded_child) 365 { 366 struct cgroup_subsys_state *css; 367 struct cpuset *child; 368 369 if (cs->css.cgroup->nr_populated_csets) 370 return true; 371 if (!excluded_child && !cs->nr_subparts) 372 return cgroup_is_populated(cs->css.cgroup); 373 374 rcu_read_lock(); 375 cpuset_for_each_child(child, css, cs) { 376 if (child == excluded_child) 377 continue; 378 if (is_partition_valid(child)) 379 continue; 380 if (cgroup_is_populated(child->css.cgroup)) { 381 rcu_read_unlock(); 382 return true; 383 } 384 } 385 rcu_read_unlock(); 386 return false; 387 } 388 389 /* 390 * Return in pmask the portion of a task's cpusets's cpus_allowed that 391 * are online and are capable of running the task. If none are found, 392 * walk up the cpuset hierarchy until we find one that does have some 393 * appropriate cpus. 394 * 395 * One way or another, we guarantee to return some non-empty subset 396 * of cpu_active_mask. 397 * 398 * Call with callback_lock or cpuset_mutex held. 399 */ 400 static void guarantee_active_cpus(struct task_struct *tsk, 401 struct cpumask *pmask) 402 { 403 const struct cpumask *possible_mask = task_cpu_possible_mask(tsk); 404 struct cpuset *cs; 405 406 if (WARN_ON(!cpumask_and(pmask, possible_mask, cpu_active_mask))) 407 cpumask_copy(pmask, cpu_active_mask); 408 409 rcu_read_lock(); 410 cs = task_cs(tsk); 411 412 while (!cpumask_intersects(cs->effective_cpus, pmask)) 413 cs = parent_cs(cs); 414 415 cpumask_and(pmask, pmask, cs->effective_cpus); 416 rcu_read_unlock(); 417 } 418 419 /* 420 * Return in *pmask the portion of a cpusets's mems_allowed that 421 * are online, with memory. If none are online with memory, walk 422 * up the cpuset hierarchy until we find one that does have some 423 * online mems. The top cpuset always has some mems online. 424 * 425 * One way or another, we guarantee to return some non-empty subset 426 * of node_states[N_MEMORY]. 427 * 428 * Call with callback_lock or cpuset_mutex held. 429 */ 430 static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask) 431 { 432 while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY])) 433 cs = parent_cs(cs); 434 nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]); 435 } 436 437 /** 438 * alloc_cpumasks - Allocate an array of cpumask variables 439 * @pmasks: Pointer to array of cpumask_var_t pointers 440 * @size: Number of cpumasks to allocate 441 * Return: 0 if successful, -ENOMEM otherwise. 442 * 443 * Allocates @size cpumasks and initializes them to empty. Returns 0 on 444 * success, -ENOMEM on allocation failure. On failure, any previously 445 * allocated cpumasks are freed. 446 */ 447 static inline int alloc_cpumasks(cpumask_var_t *pmasks[], u32 size) 448 { 449 int i; 450 451 for (i = 0; i < size; i++) { 452 if (!zalloc_cpumask_var(pmasks[i], GFP_KERNEL)) { 453 while (--i >= 0) 454 free_cpumask_var(*pmasks[i]); 455 return -ENOMEM; 456 } 457 } 458 return 0; 459 } 460 461 /** 462 * alloc_tmpmasks - Allocate temporary cpumasks for cpuset operations. 463 * @tmp: Pointer to tmpmasks structure to populate 464 * Return: 0 on success, -ENOMEM on allocation failure 465 */ 466 static inline int alloc_tmpmasks(struct tmpmasks *tmp) 467 { 468 /* 469 * Array of pointers to the three cpumask_var_t fields in tmpmasks. 470 * Note: Array size must match actual number of masks (3) 471 */ 472 cpumask_var_t *pmask[3] = { 473 &tmp->new_cpus, 474 &tmp->addmask, 475 &tmp->delmask 476 }; 477 478 return alloc_cpumasks(pmask, ARRAY_SIZE(pmask)); 479 } 480 481 /** 482 * free_tmpmasks - free cpumasks in a tmpmasks structure 483 * @tmp: the tmpmasks structure pointer 484 */ 485 static inline void free_tmpmasks(struct tmpmasks *tmp) 486 { 487 free_cpumask_var(tmp->new_cpus); 488 free_cpumask_var(tmp->addmask); 489 free_cpumask_var(tmp->delmask); 490 } 491 492 /** 493 * dup_or_alloc_cpuset - Duplicate or allocate a new cpuset 494 * @cs: Source cpuset to duplicate (NULL for a fresh allocation) 495 * 496 * Creates a new cpuset by either: 497 * 1. Duplicating an existing cpuset (if @cs is non-NULL), or 498 * 2. Allocating a fresh cpuset with zero-initialized masks (if @cs is NULL) 499 * 500 * Return: Pointer to newly allocated cpuset on success, NULL on failure 501 */ 502 static struct cpuset *dup_or_alloc_cpuset(struct cpuset *cs) 503 { 504 struct cpuset *trial; 505 506 /* Allocate base structure */ 507 trial = cs ? kmemdup(cs, sizeof(*cs), GFP_KERNEL) : 508 kzalloc(sizeof(*cs), GFP_KERNEL); 509 if (!trial) 510 return NULL; 511 512 /* Setup cpumask pointer array */ 513 cpumask_var_t *pmask[4] = { 514 &trial->cpus_allowed, 515 &trial->effective_cpus, 516 &trial->effective_xcpus, 517 &trial->exclusive_cpus 518 }; 519 520 if (alloc_cpumasks(pmask, ARRAY_SIZE(pmask))) { 521 kfree(trial); 522 return NULL; 523 } 524 525 /* Copy masks if duplicating */ 526 if (cs) { 527 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed); 528 cpumask_copy(trial->effective_cpus, cs->effective_cpus); 529 cpumask_copy(trial->effective_xcpus, cs->effective_xcpus); 530 cpumask_copy(trial->exclusive_cpus, cs->exclusive_cpus); 531 } 532 533 return trial; 534 } 535 536 /** 537 * free_cpuset - free the cpuset 538 * @cs: the cpuset to be freed 539 */ 540 static inline void free_cpuset(struct cpuset *cs) 541 { 542 free_cpumask_var(cs->cpus_allowed); 543 free_cpumask_var(cs->effective_cpus); 544 free_cpumask_var(cs->effective_xcpus); 545 free_cpumask_var(cs->exclusive_cpus); 546 kfree(cs); 547 } 548 549 /* Return user specified exclusive CPUs */ 550 static inline struct cpumask *user_xcpus(struct cpuset *cs) 551 { 552 return cpumask_empty(cs->exclusive_cpus) ? cs->cpus_allowed 553 : cs->exclusive_cpus; 554 } 555 556 static inline bool xcpus_empty(struct cpuset *cs) 557 { 558 return cpumask_empty(cs->cpus_allowed) && 559 cpumask_empty(cs->exclusive_cpus); 560 } 561 562 /* 563 * cpusets_are_exclusive() - check if two cpusets are exclusive 564 * 565 * Return true if exclusive, false if not 566 */ 567 static inline bool cpusets_are_exclusive(struct cpuset *cs1, struct cpuset *cs2) 568 { 569 struct cpumask *xcpus1 = user_xcpus(cs1); 570 struct cpumask *xcpus2 = user_xcpus(cs2); 571 572 if (cpumask_intersects(xcpus1, xcpus2)) 573 return false; 574 return true; 575 } 576 577 /* 578 * validate_change() - Used to validate that any proposed cpuset change 579 * follows the structural rules for cpusets. 580 * 581 * If we replaced the flag and mask values of the current cpuset 582 * (cur) with those values in the trial cpuset (trial), would 583 * our various subset and exclusive rules still be valid? Presumes 584 * cpuset_mutex held. 585 * 586 * 'cur' is the address of an actual, in-use cpuset. Operations 587 * such as list traversal that depend on the actual address of the 588 * cpuset in the list must use cur below, not trial. 589 * 590 * 'trial' is the address of bulk structure copy of cur, with 591 * perhaps one or more of the fields cpus_allowed, mems_allowed, 592 * or flags changed to new, trial values. 593 * 594 * Return 0 if valid, -errno if not. 595 */ 596 597 static int validate_change(struct cpuset *cur, struct cpuset *trial) 598 { 599 struct cgroup_subsys_state *css; 600 struct cpuset *c, *par; 601 int ret = 0; 602 603 rcu_read_lock(); 604 605 if (!is_in_v2_mode()) 606 ret = cpuset1_validate_change(cur, trial); 607 if (ret) 608 goto out; 609 610 /* Remaining checks don't apply to root cpuset */ 611 if (cur == &top_cpuset) 612 goto out; 613 614 par = parent_cs(cur); 615 616 /* 617 * Cpusets with tasks - existing or newly being attached - can't 618 * be changed to have empty cpus_allowed or mems_allowed. 619 */ 620 ret = -ENOSPC; 621 if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) { 622 if (!cpumask_empty(cur->cpus_allowed) && 623 cpumask_empty(trial->cpus_allowed)) 624 goto out; 625 if (!nodes_empty(cur->mems_allowed) && 626 nodes_empty(trial->mems_allowed)) 627 goto out; 628 } 629 630 /* 631 * We can't shrink if we won't have enough room for SCHED_DEADLINE 632 * tasks. This check is not done when scheduling is disabled as the 633 * users should know what they are doing. 634 * 635 * For v1, effective_cpus == cpus_allowed & user_xcpus() returns 636 * cpus_allowed. 637 * 638 * For v2, is_cpu_exclusive() & is_sched_load_balance() are true only 639 * for non-isolated partition root. At this point, the target 640 * effective_cpus isn't computed yet. user_xcpus() is the best 641 * approximation. 642 * 643 * TBD: May need to precompute the real effective_cpus here in case 644 * incorrect scheduling of SCHED_DEADLINE tasks in a partition 645 * becomes an issue. 646 */ 647 ret = -EBUSY; 648 if (is_cpu_exclusive(cur) && is_sched_load_balance(cur) && 649 !cpuset_cpumask_can_shrink(cur->effective_cpus, user_xcpus(trial))) 650 goto out; 651 652 /* 653 * If either I or some sibling (!= me) is exclusive, we can't 654 * overlap. exclusive_cpus cannot overlap with each other if set. 655 */ 656 ret = -EINVAL; 657 cpuset_for_each_child(c, css, par) { 658 bool txset, cxset; /* Are exclusive_cpus set? */ 659 660 if (c == cur) 661 continue; 662 663 txset = !cpumask_empty(trial->exclusive_cpus); 664 cxset = !cpumask_empty(c->exclusive_cpus); 665 if (is_cpu_exclusive(trial) || is_cpu_exclusive(c) || 666 (txset && cxset)) { 667 if (!cpusets_are_exclusive(trial, c)) 668 goto out; 669 } else if (txset || cxset) { 670 struct cpumask *xcpus, *acpus; 671 672 /* 673 * When just one of the exclusive_cpus's is set, 674 * cpus_allowed of the other cpuset, if set, cannot be 675 * a subset of it or none of those CPUs will be 676 * available if these exclusive CPUs are activated. 677 */ 678 if (txset) { 679 xcpus = trial->exclusive_cpus; 680 acpus = c->cpus_allowed; 681 } else { 682 xcpus = c->exclusive_cpus; 683 acpus = trial->cpus_allowed; 684 } 685 if (!cpumask_empty(acpus) && cpumask_subset(acpus, xcpus)) 686 goto out; 687 } 688 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && 689 nodes_intersects(trial->mems_allowed, c->mems_allowed)) 690 goto out; 691 } 692 693 ret = 0; 694 out: 695 rcu_read_unlock(); 696 return ret; 697 } 698 699 #ifdef CONFIG_SMP 700 /* 701 * Helper routine for generate_sched_domains(). 702 * Do cpusets a, b have overlapping effective cpus_allowed masks? 703 */ 704 static int cpusets_overlap(struct cpuset *a, struct cpuset *b) 705 { 706 return cpumask_intersects(a->effective_cpus, b->effective_cpus); 707 } 708 709 static void 710 update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c) 711 { 712 if (dattr->relax_domain_level < c->relax_domain_level) 713 dattr->relax_domain_level = c->relax_domain_level; 714 return; 715 } 716 717 static void update_domain_attr_tree(struct sched_domain_attr *dattr, 718 struct cpuset *root_cs) 719 { 720 struct cpuset *cp; 721 struct cgroup_subsys_state *pos_css; 722 723 rcu_read_lock(); 724 cpuset_for_each_descendant_pre(cp, pos_css, root_cs) { 725 /* skip the whole subtree if @cp doesn't have any CPU */ 726 if (cpumask_empty(cp->cpus_allowed)) { 727 pos_css = css_rightmost_descendant(pos_css); 728 continue; 729 } 730 731 if (is_sched_load_balance(cp)) 732 update_domain_attr(dattr, cp); 733 } 734 rcu_read_unlock(); 735 } 736 737 /* Must be called with cpuset_mutex held. */ 738 static inline int nr_cpusets(void) 739 { 740 /* jump label reference count + the top-level cpuset */ 741 return static_key_count(&cpusets_enabled_key.key) + 1; 742 } 743 744 /* 745 * generate_sched_domains() 746 * 747 * This function builds a partial partition of the systems CPUs 748 * A 'partial partition' is a set of non-overlapping subsets whose 749 * union is a subset of that set. 750 * The output of this function needs to be passed to kernel/sched/core.c 751 * partition_sched_domains() routine, which will rebuild the scheduler's 752 * load balancing domains (sched domains) as specified by that partial 753 * partition. 754 * 755 * See "What is sched_load_balance" in Documentation/admin-guide/cgroup-v1/cpusets.rst 756 * for a background explanation of this. 757 * 758 * Does not return errors, on the theory that the callers of this 759 * routine would rather not worry about failures to rebuild sched 760 * domains when operating in the severe memory shortage situations 761 * that could cause allocation failures below. 762 * 763 * Must be called with cpuset_mutex held. 764 * 765 * The three key local variables below are: 766 * cp - cpuset pointer, used (together with pos_css) to perform a 767 * top-down scan of all cpusets. For our purposes, rebuilding 768 * the schedulers sched domains, we can ignore !is_sched_load_ 769 * balance cpusets. 770 * csa - (for CpuSet Array) Array of pointers to all the cpusets 771 * that need to be load balanced, for convenient iterative 772 * access by the subsequent code that finds the best partition, 773 * i.e the set of domains (subsets) of CPUs such that the 774 * cpus_allowed of every cpuset marked is_sched_load_balance 775 * is a subset of one of these domains, while there are as 776 * many such domains as possible, each as small as possible. 777 * doms - Conversion of 'csa' to an array of cpumasks, for passing to 778 * the kernel/sched/core.c routine partition_sched_domains() in a 779 * convenient format, that can be easily compared to the prior 780 * value to determine what partition elements (sched domains) 781 * were changed (added or removed.) 782 * 783 * Finding the best partition (set of domains): 784 * The double nested loops below over i, j scan over the load 785 * balanced cpusets (using the array of cpuset pointers in csa[]) 786 * looking for pairs of cpusets that have overlapping cpus_allowed 787 * and merging them using a union-find algorithm. 788 * 789 * The union of the cpus_allowed masks from the set of all cpusets 790 * having the same root then form the one element of the partition 791 * (one sched domain) to be passed to partition_sched_domains(). 792 * 793 */ 794 static int generate_sched_domains(cpumask_var_t **domains, 795 struct sched_domain_attr **attributes) 796 { 797 struct cpuset *cp; /* top-down scan of cpusets */ 798 struct cpuset **csa; /* array of all cpuset ptrs */ 799 int csn; /* how many cpuset ptrs in csa so far */ 800 int i, j; /* indices for partition finding loops */ 801 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */ 802 struct sched_domain_attr *dattr; /* attributes for custom domains */ 803 int ndoms = 0; /* number of sched domains in result */ 804 int nslot; /* next empty doms[] struct cpumask slot */ 805 struct cgroup_subsys_state *pos_css; 806 bool root_load_balance = is_sched_load_balance(&top_cpuset); 807 bool cgrpv2 = cpuset_v2(); 808 int nslot_update; 809 810 doms = NULL; 811 dattr = NULL; 812 csa = NULL; 813 814 /* Special case for the 99% of systems with one, full, sched domain */ 815 if (root_load_balance && cpumask_empty(subpartitions_cpus)) { 816 single_root_domain: 817 ndoms = 1; 818 doms = alloc_sched_domains(ndoms); 819 if (!doms) 820 goto done; 821 822 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL); 823 if (dattr) { 824 *dattr = SD_ATTR_INIT; 825 update_domain_attr_tree(dattr, &top_cpuset); 826 } 827 cpumask_and(doms[0], top_cpuset.effective_cpus, 828 housekeeping_cpumask(HK_TYPE_DOMAIN)); 829 830 goto done; 831 } 832 833 csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL); 834 if (!csa) 835 goto done; 836 csn = 0; 837 838 rcu_read_lock(); 839 if (root_load_balance) 840 csa[csn++] = &top_cpuset; 841 cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) { 842 if (cp == &top_cpuset) 843 continue; 844 845 if (cgrpv2) 846 goto v2; 847 848 /* 849 * v1: 850 * Continue traversing beyond @cp iff @cp has some CPUs and 851 * isn't load balancing. The former is obvious. The 852 * latter: All child cpusets contain a subset of the 853 * parent's cpus, so just skip them, and then we call 854 * update_domain_attr_tree() to calc relax_domain_level of 855 * the corresponding sched domain. 856 */ 857 if (!cpumask_empty(cp->cpus_allowed) && 858 !(is_sched_load_balance(cp) && 859 cpumask_intersects(cp->cpus_allowed, 860 housekeeping_cpumask(HK_TYPE_DOMAIN)))) 861 continue; 862 863 if (is_sched_load_balance(cp) && 864 !cpumask_empty(cp->effective_cpus)) 865 csa[csn++] = cp; 866 867 /* skip @cp's subtree */ 868 pos_css = css_rightmost_descendant(pos_css); 869 continue; 870 871 v2: 872 /* 873 * Only valid partition roots that are not isolated and with 874 * non-empty effective_cpus will be saved into csn[]. 875 */ 876 if ((cp->partition_root_state == PRS_ROOT) && 877 !cpumask_empty(cp->effective_cpus)) 878 csa[csn++] = cp; 879 880 /* 881 * Skip @cp's subtree if not a partition root and has no 882 * exclusive CPUs to be granted to child cpusets. 883 */ 884 if (!is_partition_valid(cp) && cpumask_empty(cp->exclusive_cpus)) 885 pos_css = css_rightmost_descendant(pos_css); 886 } 887 rcu_read_unlock(); 888 889 /* 890 * If there are only isolated partitions underneath the cgroup root, 891 * we can optimize out unneeded sched domains scanning. 892 */ 893 if (root_load_balance && (csn == 1)) 894 goto single_root_domain; 895 896 for (i = 0; i < csn; i++) 897 uf_node_init(&csa[i]->node); 898 899 /* Merge overlapping cpusets */ 900 for (i = 0; i < csn; i++) { 901 for (j = i + 1; j < csn; j++) { 902 if (cpusets_overlap(csa[i], csa[j])) { 903 /* 904 * Cgroup v2 shouldn't pass down overlapping 905 * partition root cpusets. 906 */ 907 WARN_ON_ONCE(cgrpv2); 908 uf_union(&csa[i]->node, &csa[j]->node); 909 } 910 } 911 } 912 913 /* Count the total number of domains */ 914 for (i = 0; i < csn; i++) { 915 if (uf_find(&csa[i]->node) == &csa[i]->node) 916 ndoms++; 917 } 918 919 /* 920 * Now we know how many domains to create. 921 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks. 922 */ 923 doms = alloc_sched_domains(ndoms); 924 if (!doms) 925 goto done; 926 927 /* 928 * The rest of the code, including the scheduler, can deal with 929 * dattr==NULL case. No need to abort if alloc fails. 930 */ 931 dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr), 932 GFP_KERNEL); 933 934 /* 935 * Cgroup v2 doesn't support domain attributes, just set all of them 936 * to SD_ATTR_INIT. Also non-isolating partition root CPUs are a 937 * subset of HK_TYPE_DOMAIN housekeeping CPUs. 938 */ 939 if (cgrpv2) { 940 for (i = 0; i < ndoms; i++) { 941 /* 942 * The top cpuset may contain some boot time isolated 943 * CPUs that need to be excluded from the sched domain. 944 */ 945 if (csa[i] == &top_cpuset) 946 cpumask_and(doms[i], csa[i]->effective_cpus, 947 housekeeping_cpumask(HK_TYPE_DOMAIN)); 948 else 949 cpumask_copy(doms[i], csa[i]->effective_cpus); 950 if (dattr) 951 dattr[i] = SD_ATTR_INIT; 952 } 953 goto done; 954 } 955 956 for (nslot = 0, i = 0; i < csn; i++) { 957 nslot_update = 0; 958 for (j = i; j < csn; j++) { 959 if (uf_find(&csa[j]->node) == &csa[i]->node) { 960 struct cpumask *dp = doms[nslot]; 961 962 if (i == j) { 963 nslot_update = 1; 964 cpumask_clear(dp); 965 if (dattr) 966 *(dattr + nslot) = SD_ATTR_INIT; 967 } 968 cpumask_or(dp, dp, csa[j]->effective_cpus); 969 cpumask_and(dp, dp, housekeeping_cpumask(HK_TYPE_DOMAIN)); 970 if (dattr) 971 update_domain_attr_tree(dattr + nslot, csa[j]); 972 } 973 } 974 if (nslot_update) 975 nslot++; 976 } 977 BUG_ON(nslot != ndoms); 978 979 done: 980 kfree(csa); 981 982 /* 983 * Fallback to the default domain if kmalloc() failed. 984 * See comments in partition_sched_domains(). 985 */ 986 if (doms == NULL) 987 ndoms = 1; 988 989 *domains = doms; 990 *attributes = dattr; 991 return ndoms; 992 } 993 994 static void dl_update_tasks_root_domain(struct cpuset *cs) 995 { 996 struct css_task_iter it; 997 struct task_struct *task; 998 999 if (cs->nr_deadline_tasks == 0) 1000 return; 1001 1002 css_task_iter_start(&cs->css, 0, &it); 1003 1004 while ((task = css_task_iter_next(&it))) 1005 dl_add_task_root_domain(task); 1006 1007 css_task_iter_end(&it); 1008 } 1009 1010 void dl_rebuild_rd_accounting(void) 1011 { 1012 struct cpuset *cs = NULL; 1013 struct cgroup_subsys_state *pos_css; 1014 int cpu; 1015 u64 cookie = ++dl_cookie; 1016 1017 lockdep_assert_held(&cpuset_mutex); 1018 lockdep_assert_cpus_held(); 1019 lockdep_assert_held(&sched_domains_mutex); 1020 1021 rcu_read_lock(); 1022 1023 for_each_possible_cpu(cpu) { 1024 if (dl_bw_visited(cpu, cookie)) 1025 continue; 1026 1027 dl_clear_root_domain_cpu(cpu); 1028 } 1029 1030 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { 1031 1032 if (cpumask_empty(cs->effective_cpus)) { 1033 pos_css = css_rightmost_descendant(pos_css); 1034 continue; 1035 } 1036 1037 css_get(&cs->css); 1038 1039 rcu_read_unlock(); 1040 1041 dl_update_tasks_root_domain(cs); 1042 1043 rcu_read_lock(); 1044 css_put(&cs->css); 1045 } 1046 rcu_read_unlock(); 1047 } 1048 1049 /* 1050 * Rebuild scheduler domains. 1051 * 1052 * If the flag 'sched_load_balance' of any cpuset with non-empty 1053 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset 1054 * which has that flag enabled, or if any cpuset with a non-empty 1055 * 'cpus' is removed, then call this routine to rebuild the 1056 * scheduler's dynamic sched domains. 1057 * 1058 * Call with cpuset_mutex held. Takes cpus_read_lock(). 1059 */ 1060 void rebuild_sched_domains_locked(void) 1061 { 1062 struct cgroup_subsys_state *pos_css; 1063 struct sched_domain_attr *attr; 1064 cpumask_var_t *doms; 1065 struct cpuset *cs; 1066 int ndoms; 1067 1068 lockdep_assert_cpus_held(); 1069 lockdep_assert_held(&cpuset_mutex); 1070 force_sd_rebuild = false; 1071 1072 /* 1073 * If we have raced with CPU hotplug, return early to avoid 1074 * passing doms with offlined cpu to partition_sched_domains(). 1075 * Anyways, cpuset_handle_hotplug() will rebuild sched domains. 1076 * 1077 * With no CPUs in any subpartitions, top_cpuset's effective CPUs 1078 * should be the same as the active CPUs, so checking only top_cpuset 1079 * is enough to detect racing CPU offlines. 1080 */ 1081 if (cpumask_empty(subpartitions_cpus) && 1082 !cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask)) 1083 return; 1084 1085 /* 1086 * With subpartition CPUs, however, the effective CPUs of a partition 1087 * root should be only a subset of the active CPUs. Since a CPU in any 1088 * partition root could be offlined, all must be checked. 1089 */ 1090 if (!cpumask_empty(subpartitions_cpus)) { 1091 rcu_read_lock(); 1092 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { 1093 if (!is_partition_valid(cs)) { 1094 pos_css = css_rightmost_descendant(pos_css); 1095 continue; 1096 } 1097 if (!cpumask_subset(cs->effective_cpus, 1098 cpu_active_mask)) { 1099 rcu_read_unlock(); 1100 return; 1101 } 1102 } 1103 rcu_read_unlock(); 1104 } 1105 1106 /* Generate domain masks and attrs */ 1107 ndoms = generate_sched_domains(&doms, &attr); 1108 1109 /* Have scheduler rebuild the domains */ 1110 partition_sched_domains(ndoms, doms, attr); 1111 } 1112 #else /* !CONFIG_SMP */ 1113 void rebuild_sched_domains_locked(void) 1114 { 1115 } 1116 #endif /* CONFIG_SMP */ 1117 1118 static void rebuild_sched_domains_cpuslocked(void) 1119 { 1120 mutex_lock(&cpuset_mutex); 1121 rebuild_sched_domains_locked(); 1122 mutex_unlock(&cpuset_mutex); 1123 } 1124 1125 void rebuild_sched_domains(void) 1126 { 1127 cpus_read_lock(); 1128 rebuild_sched_domains_cpuslocked(); 1129 cpus_read_unlock(); 1130 } 1131 1132 void cpuset_reset_sched_domains(void) 1133 { 1134 mutex_lock(&cpuset_mutex); 1135 partition_sched_domains(1, NULL, NULL); 1136 mutex_unlock(&cpuset_mutex); 1137 } 1138 1139 /** 1140 * cpuset_update_tasks_cpumask - Update the cpumasks of tasks in the cpuset. 1141 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed 1142 * @new_cpus: the temp variable for the new effective_cpus mask 1143 * 1144 * Iterate through each task of @cs updating its cpus_allowed to the 1145 * effective cpuset's. As this function is called with cpuset_mutex held, 1146 * cpuset membership stays stable. 1147 * 1148 * For top_cpuset, task_cpu_possible_mask() is used instead of effective_cpus 1149 * to make sure all offline CPUs are also included as hotplug code won't 1150 * update cpumasks for tasks in top_cpuset. 1151 * 1152 * As task_cpu_possible_mask() can be task dependent in arm64, we have to 1153 * do cpu masking per task instead of doing it once for all. 1154 */ 1155 void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus) 1156 { 1157 struct css_task_iter it; 1158 struct task_struct *task; 1159 bool top_cs = cs == &top_cpuset; 1160 1161 css_task_iter_start(&cs->css, 0, &it); 1162 while ((task = css_task_iter_next(&it))) { 1163 const struct cpumask *possible_mask = task_cpu_possible_mask(task); 1164 1165 if (top_cs) { 1166 /* 1167 * PF_NO_SETAFFINITY tasks are ignored. 1168 * All per cpu kthreads should have PF_NO_SETAFFINITY 1169 * flag set, see kthread_set_per_cpu(). 1170 */ 1171 if (task->flags & PF_NO_SETAFFINITY) 1172 continue; 1173 cpumask_andnot(new_cpus, possible_mask, subpartitions_cpus); 1174 } else { 1175 cpumask_and(new_cpus, possible_mask, cs->effective_cpus); 1176 } 1177 set_cpus_allowed_ptr(task, new_cpus); 1178 } 1179 css_task_iter_end(&it); 1180 } 1181 1182 /** 1183 * compute_effective_cpumask - Compute the effective cpumask of the cpuset 1184 * @new_cpus: the temp variable for the new effective_cpus mask 1185 * @cs: the cpuset the need to recompute the new effective_cpus mask 1186 * @parent: the parent cpuset 1187 * 1188 * The result is valid only if the given cpuset isn't a partition root. 1189 */ 1190 static void compute_effective_cpumask(struct cpumask *new_cpus, 1191 struct cpuset *cs, struct cpuset *parent) 1192 { 1193 cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus); 1194 } 1195 1196 /* 1197 * Commands for update_parent_effective_cpumask 1198 */ 1199 enum partition_cmd { 1200 partcmd_enable, /* Enable partition root */ 1201 partcmd_enablei, /* Enable isolated partition root */ 1202 partcmd_disable, /* Disable partition root */ 1203 partcmd_update, /* Update parent's effective_cpus */ 1204 partcmd_invalidate, /* Make partition invalid */ 1205 }; 1206 1207 static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs, 1208 struct tmpmasks *tmp); 1209 1210 /* 1211 * Update partition exclusive flag 1212 * 1213 * Return: 0 if successful, an error code otherwise 1214 */ 1215 static int update_partition_exclusive_flag(struct cpuset *cs, int new_prs) 1216 { 1217 bool exclusive = (new_prs > PRS_MEMBER); 1218 1219 if (exclusive && !is_cpu_exclusive(cs)) { 1220 if (cpuset_update_flag(CS_CPU_EXCLUSIVE, cs, 1)) 1221 return PERR_NOTEXCL; 1222 } else if (!exclusive && is_cpu_exclusive(cs)) { 1223 /* Turning off CS_CPU_EXCLUSIVE will not return error */ 1224 cpuset_update_flag(CS_CPU_EXCLUSIVE, cs, 0); 1225 } 1226 return 0; 1227 } 1228 1229 /* 1230 * Update partition load balance flag and/or rebuild sched domain 1231 * 1232 * Changing load balance flag will automatically call 1233 * rebuild_sched_domains_locked(). 1234 * This function is for cgroup v2 only. 1235 */ 1236 static void update_partition_sd_lb(struct cpuset *cs, int old_prs) 1237 { 1238 int new_prs = cs->partition_root_state; 1239 bool rebuild_domains = (new_prs > 0) || (old_prs > 0); 1240 bool new_lb; 1241 1242 /* 1243 * If cs is not a valid partition root, the load balance state 1244 * will follow its parent. 1245 */ 1246 if (new_prs > 0) { 1247 new_lb = (new_prs != PRS_ISOLATED); 1248 } else { 1249 new_lb = is_sched_load_balance(parent_cs(cs)); 1250 } 1251 if (new_lb != !!is_sched_load_balance(cs)) { 1252 rebuild_domains = true; 1253 if (new_lb) 1254 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 1255 else 1256 clear_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 1257 } 1258 1259 if (rebuild_domains) 1260 cpuset_force_rebuild(); 1261 } 1262 1263 /* 1264 * tasks_nocpu_error - Return true if tasks will have no effective_cpus 1265 */ 1266 static bool tasks_nocpu_error(struct cpuset *parent, struct cpuset *cs, 1267 struct cpumask *xcpus) 1268 { 1269 /* 1270 * A populated partition (cs or parent) can't have empty effective_cpus 1271 */ 1272 return (cpumask_subset(parent->effective_cpus, xcpus) && 1273 partition_is_populated(parent, cs)) || 1274 (!cpumask_intersects(xcpus, cpu_active_mask) && 1275 partition_is_populated(cs, NULL)); 1276 } 1277 1278 static void reset_partition_data(struct cpuset *cs) 1279 { 1280 struct cpuset *parent = parent_cs(cs); 1281 1282 if (!cpuset_v2()) 1283 return; 1284 1285 lockdep_assert_held(&callback_lock); 1286 1287 cs->nr_subparts = 0; 1288 if (cpumask_empty(cs->exclusive_cpus)) { 1289 cpumask_clear(cs->effective_xcpus); 1290 if (is_cpu_exclusive(cs)) 1291 clear_bit(CS_CPU_EXCLUSIVE, &cs->flags); 1292 } 1293 if (!cpumask_and(cs->effective_cpus, parent->effective_cpus, cs->cpus_allowed)) 1294 cpumask_copy(cs->effective_cpus, parent->effective_cpus); 1295 } 1296 1297 /* 1298 * isolated_cpus_update - Update the isolated_cpus mask 1299 * @old_prs: old partition_root_state 1300 * @new_prs: new partition_root_state 1301 * @xcpus: exclusive CPUs with state change 1302 */ 1303 static void isolated_cpus_update(int old_prs, int new_prs, struct cpumask *xcpus) 1304 { 1305 WARN_ON_ONCE(old_prs == new_prs); 1306 if (new_prs == PRS_ISOLATED) 1307 cpumask_or(isolated_cpus, isolated_cpus, xcpus); 1308 else 1309 cpumask_andnot(isolated_cpus, isolated_cpus, xcpus); 1310 } 1311 1312 /* 1313 * partition_xcpus_add - Add new exclusive CPUs to partition 1314 * @new_prs: new partition_root_state 1315 * @parent: parent cpuset 1316 * @xcpus: exclusive CPUs to be added 1317 * Return: true if isolated_cpus modified, false otherwise 1318 * 1319 * Remote partition if parent == NULL 1320 */ 1321 static bool partition_xcpus_add(int new_prs, struct cpuset *parent, 1322 struct cpumask *xcpus) 1323 { 1324 bool isolcpus_updated; 1325 1326 WARN_ON_ONCE(new_prs < 0); 1327 lockdep_assert_held(&callback_lock); 1328 if (!parent) 1329 parent = &top_cpuset; 1330 1331 1332 if (parent == &top_cpuset) 1333 cpumask_or(subpartitions_cpus, subpartitions_cpus, xcpus); 1334 1335 isolcpus_updated = (new_prs != parent->partition_root_state); 1336 if (isolcpus_updated) 1337 isolated_cpus_update(parent->partition_root_state, new_prs, 1338 xcpus); 1339 1340 cpumask_andnot(parent->effective_cpus, parent->effective_cpus, xcpus); 1341 return isolcpus_updated; 1342 } 1343 1344 /* 1345 * partition_xcpus_del - Remove exclusive CPUs from partition 1346 * @old_prs: old partition_root_state 1347 * @parent: parent cpuset 1348 * @xcpus: exclusive CPUs to be removed 1349 * Return: true if isolated_cpus modified, false otherwise 1350 * 1351 * Remote partition if parent == NULL 1352 */ 1353 static bool partition_xcpus_del(int old_prs, struct cpuset *parent, 1354 struct cpumask *xcpus) 1355 { 1356 bool isolcpus_updated; 1357 1358 WARN_ON_ONCE(old_prs < 0); 1359 lockdep_assert_held(&callback_lock); 1360 if (!parent) 1361 parent = &top_cpuset; 1362 1363 if (parent == &top_cpuset) 1364 cpumask_andnot(subpartitions_cpus, subpartitions_cpus, xcpus); 1365 1366 isolcpus_updated = (old_prs != parent->partition_root_state); 1367 if (isolcpus_updated) 1368 isolated_cpus_update(old_prs, parent->partition_root_state, 1369 xcpus); 1370 1371 cpumask_and(xcpus, xcpus, cpu_active_mask); 1372 cpumask_or(parent->effective_cpus, parent->effective_cpus, xcpus); 1373 return isolcpus_updated; 1374 } 1375 1376 static void update_unbound_workqueue_cpumask(bool isolcpus_updated) 1377 { 1378 int ret; 1379 1380 lockdep_assert_cpus_held(); 1381 1382 if (!isolcpus_updated) 1383 return; 1384 1385 ret = workqueue_unbound_exclude_cpumask(isolated_cpus); 1386 WARN_ON_ONCE(ret < 0); 1387 } 1388 1389 /** 1390 * cpuset_cpu_is_isolated - Check if the given CPU is isolated 1391 * @cpu: the CPU number to be checked 1392 * Return: true if CPU is used in an isolated partition, false otherwise 1393 */ 1394 bool cpuset_cpu_is_isolated(int cpu) 1395 { 1396 return cpumask_test_cpu(cpu, isolated_cpus); 1397 } 1398 EXPORT_SYMBOL_GPL(cpuset_cpu_is_isolated); 1399 1400 /* 1401 * compute_effective_exclusive_cpumask - compute effective exclusive CPUs 1402 * @cs: cpuset 1403 * @xcpus: effective exclusive CPUs value to be set 1404 * @real_cs: the real cpuset (can be NULL) 1405 * Return: 0 if there is no sibling conflict, > 0 otherwise 1406 * 1407 * If exclusive_cpus isn't explicitly set or a real_cs is provided, we have to 1408 * scan the sibling cpusets and exclude their exclusive_cpus or effective_xcpus 1409 * as well. The provision of real_cs means that a cpumask is being changed and 1410 * the given cs is a trial one. 1411 */ 1412 static int compute_effective_exclusive_cpumask(struct cpuset *cs, 1413 struct cpumask *xcpus, 1414 struct cpuset *real_cs) 1415 { 1416 struct cgroup_subsys_state *css; 1417 struct cpuset *parent = parent_cs(cs); 1418 struct cpuset *sibling; 1419 int retval = 0; 1420 1421 if (!xcpus) 1422 xcpus = cs->effective_xcpus; 1423 1424 cpumask_and(xcpus, user_xcpus(cs), parent->effective_xcpus); 1425 1426 if (!real_cs) { 1427 if (!cpumask_empty(cs->exclusive_cpus)) 1428 return 0; 1429 } else { 1430 cs = real_cs; 1431 } 1432 1433 /* 1434 * Exclude exclusive CPUs from siblings 1435 */ 1436 rcu_read_lock(); 1437 cpuset_for_each_child(sibling, css, parent) { 1438 if (sibling == cs) 1439 continue; 1440 1441 if (cpumask_intersects(xcpus, sibling->exclusive_cpus)) { 1442 cpumask_andnot(xcpus, xcpus, sibling->exclusive_cpus); 1443 retval++; 1444 continue; 1445 } 1446 if (cpumask_intersects(xcpus, sibling->effective_xcpus)) { 1447 cpumask_andnot(xcpus, xcpus, sibling->effective_xcpus); 1448 retval++; 1449 } 1450 } 1451 rcu_read_unlock(); 1452 return retval; 1453 } 1454 1455 static inline bool is_remote_partition(struct cpuset *cs) 1456 { 1457 return !list_empty(&cs->remote_sibling); 1458 } 1459 1460 static inline bool is_local_partition(struct cpuset *cs) 1461 { 1462 return is_partition_valid(cs) && !is_remote_partition(cs); 1463 } 1464 1465 /* 1466 * remote_partition_enable - Enable current cpuset as a remote partition root 1467 * @cs: the cpuset to update 1468 * @new_prs: new partition_root_state 1469 * @tmp: temporary masks 1470 * Return: 0 if successful, errcode if error 1471 * 1472 * Enable the current cpuset to become a remote partition root taking CPUs 1473 * directly from the top cpuset. cpuset_mutex must be held by the caller. 1474 */ 1475 static int remote_partition_enable(struct cpuset *cs, int new_prs, 1476 struct tmpmasks *tmp) 1477 { 1478 bool isolcpus_updated; 1479 1480 /* 1481 * The user must have sysadmin privilege. 1482 */ 1483 if (!capable(CAP_SYS_ADMIN)) 1484 return PERR_ACCESS; 1485 1486 /* 1487 * The requested exclusive_cpus must not be allocated to other 1488 * partitions and it can't use up all the root's effective_cpus. 1489 * 1490 * The effective_xcpus mask can contain offline CPUs, but there must 1491 * be at least one or more online CPUs present before it can be enabled. 1492 * 1493 * Note that creating a remote partition with any local partition root 1494 * above it or remote partition root underneath it is not allowed. 1495 */ 1496 compute_effective_exclusive_cpumask(cs, tmp->new_cpus, NULL); 1497 WARN_ON_ONCE(cpumask_intersects(tmp->new_cpus, subpartitions_cpus)); 1498 if (!cpumask_intersects(tmp->new_cpus, cpu_active_mask) || 1499 cpumask_subset(top_cpuset.effective_cpus, tmp->new_cpus)) 1500 return PERR_INVCPUS; 1501 1502 spin_lock_irq(&callback_lock); 1503 isolcpus_updated = partition_xcpus_add(new_prs, NULL, tmp->new_cpus); 1504 list_add(&cs->remote_sibling, &remote_children); 1505 cpumask_copy(cs->effective_xcpus, tmp->new_cpus); 1506 spin_unlock_irq(&callback_lock); 1507 update_unbound_workqueue_cpumask(isolcpus_updated); 1508 cpuset_force_rebuild(); 1509 cs->prs_err = 0; 1510 1511 /* 1512 * Propagate changes in top_cpuset's effective_cpus down the hierarchy. 1513 */ 1514 cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus); 1515 update_sibling_cpumasks(&top_cpuset, NULL, tmp); 1516 return 0; 1517 } 1518 1519 /* 1520 * remote_partition_disable - Remove current cpuset from remote partition list 1521 * @cs: the cpuset to update 1522 * @tmp: temporary masks 1523 * 1524 * The effective_cpus is also updated. 1525 * 1526 * cpuset_mutex must be held by the caller. 1527 */ 1528 static void remote_partition_disable(struct cpuset *cs, struct tmpmasks *tmp) 1529 { 1530 bool isolcpus_updated; 1531 1532 WARN_ON_ONCE(!is_remote_partition(cs)); 1533 WARN_ON_ONCE(!cpumask_subset(cs->effective_xcpus, subpartitions_cpus)); 1534 1535 spin_lock_irq(&callback_lock); 1536 list_del_init(&cs->remote_sibling); 1537 isolcpus_updated = partition_xcpus_del(cs->partition_root_state, 1538 NULL, cs->effective_xcpus); 1539 if (cs->prs_err) 1540 cs->partition_root_state = -cs->partition_root_state; 1541 else 1542 cs->partition_root_state = PRS_MEMBER; 1543 1544 /* effective_xcpus may need to be changed */ 1545 compute_effective_exclusive_cpumask(cs, NULL, NULL); 1546 reset_partition_data(cs); 1547 spin_unlock_irq(&callback_lock); 1548 update_unbound_workqueue_cpumask(isolcpus_updated); 1549 cpuset_force_rebuild(); 1550 1551 /* 1552 * Propagate changes in top_cpuset's effective_cpus down the hierarchy. 1553 */ 1554 cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus); 1555 update_sibling_cpumasks(&top_cpuset, NULL, tmp); 1556 } 1557 1558 /* 1559 * remote_cpus_update - cpus_exclusive change of remote partition 1560 * @cs: the cpuset to be updated 1561 * @xcpus: the new exclusive_cpus mask, if non-NULL 1562 * @excpus: the new effective_xcpus mask 1563 * @tmp: temporary masks 1564 * 1565 * top_cpuset and subpartitions_cpus will be updated or partition can be 1566 * invalidated. 1567 */ 1568 static void remote_cpus_update(struct cpuset *cs, struct cpumask *xcpus, 1569 struct cpumask *excpus, struct tmpmasks *tmp) 1570 { 1571 bool adding, deleting; 1572 int prs = cs->partition_root_state; 1573 int isolcpus_updated = 0; 1574 1575 if (WARN_ON_ONCE(!is_remote_partition(cs))) 1576 return; 1577 1578 WARN_ON_ONCE(!cpumask_subset(cs->effective_xcpus, subpartitions_cpus)); 1579 1580 if (cpumask_empty(excpus)) { 1581 cs->prs_err = PERR_CPUSEMPTY; 1582 goto invalidate; 1583 } 1584 1585 adding = cpumask_andnot(tmp->addmask, excpus, cs->effective_xcpus); 1586 deleting = cpumask_andnot(tmp->delmask, cs->effective_xcpus, excpus); 1587 1588 /* 1589 * Additions of remote CPUs is only allowed if those CPUs are 1590 * not allocated to other partitions and there are effective_cpus 1591 * left in the top cpuset. 1592 */ 1593 if (adding) { 1594 WARN_ON_ONCE(cpumask_intersects(tmp->addmask, subpartitions_cpus)); 1595 if (!capable(CAP_SYS_ADMIN)) 1596 cs->prs_err = PERR_ACCESS; 1597 else if (cpumask_intersects(tmp->addmask, subpartitions_cpus) || 1598 cpumask_subset(top_cpuset.effective_cpus, tmp->addmask)) 1599 cs->prs_err = PERR_NOCPUS; 1600 if (cs->prs_err) 1601 goto invalidate; 1602 } 1603 1604 spin_lock_irq(&callback_lock); 1605 if (adding) 1606 isolcpus_updated += partition_xcpus_add(prs, NULL, tmp->addmask); 1607 if (deleting) 1608 isolcpus_updated += partition_xcpus_del(prs, NULL, tmp->delmask); 1609 /* 1610 * Need to update effective_xcpus and exclusive_cpus now as 1611 * update_sibling_cpumasks() below may iterate back to the same cs. 1612 */ 1613 cpumask_copy(cs->effective_xcpus, excpus); 1614 if (xcpus) 1615 cpumask_copy(cs->exclusive_cpus, xcpus); 1616 spin_unlock_irq(&callback_lock); 1617 update_unbound_workqueue_cpumask(isolcpus_updated); 1618 if (adding || deleting) 1619 cpuset_force_rebuild(); 1620 1621 /* 1622 * Propagate changes in top_cpuset's effective_cpus down the hierarchy. 1623 */ 1624 cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus); 1625 update_sibling_cpumasks(&top_cpuset, NULL, tmp); 1626 return; 1627 1628 invalidate: 1629 remote_partition_disable(cs, tmp); 1630 } 1631 1632 /* 1633 * prstate_housekeeping_conflict - check for partition & housekeeping conflicts 1634 * @prstate: partition root state to be checked 1635 * @new_cpus: cpu mask 1636 * Return: true if there is conflict, false otherwise 1637 * 1638 * CPUs outside of boot_hk_cpus, if defined, can only be used in an 1639 * isolated partition. 1640 */ 1641 static bool prstate_housekeeping_conflict(int prstate, struct cpumask *new_cpus) 1642 { 1643 if (!have_boot_isolcpus) 1644 return false; 1645 1646 if ((prstate != PRS_ISOLATED) && !cpumask_subset(new_cpus, boot_hk_cpus)) 1647 return true; 1648 1649 return false; 1650 } 1651 1652 /** 1653 * update_parent_effective_cpumask - update effective_cpus mask of parent cpuset 1654 * @cs: The cpuset that requests change in partition root state 1655 * @cmd: Partition root state change command 1656 * @newmask: Optional new cpumask for partcmd_update 1657 * @tmp: Temporary addmask and delmask 1658 * Return: 0 or a partition root state error code 1659 * 1660 * For partcmd_enable*, the cpuset is being transformed from a non-partition 1661 * root to a partition root. The effective_xcpus (cpus_allowed if 1662 * effective_xcpus not set) mask of the given cpuset will be taken away from 1663 * parent's effective_cpus. The function will return 0 if all the CPUs listed 1664 * in effective_xcpus can be granted or an error code will be returned. 1665 * 1666 * For partcmd_disable, the cpuset is being transformed from a partition 1667 * root back to a non-partition root. Any CPUs in effective_xcpus will be 1668 * given back to parent's effective_cpus. 0 will always be returned. 1669 * 1670 * For partcmd_update, if the optional newmask is specified, the cpu list is 1671 * to be changed from effective_xcpus to newmask. Otherwise, effective_xcpus is 1672 * assumed to remain the same. The cpuset should either be a valid or invalid 1673 * partition root. The partition root state may change from valid to invalid 1674 * or vice versa. An error code will be returned if transitioning from 1675 * invalid to valid violates the exclusivity rule. 1676 * 1677 * For partcmd_invalidate, the current partition will be made invalid. 1678 * 1679 * The partcmd_enable* and partcmd_disable commands are used by 1680 * update_prstate(). An error code may be returned and the caller will check 1681 * for error. 1682 * 1683 * The partcmd_update command is used by update_cpumasks_hier() with newmask 1684 * NULL and update_cpumask() with newmask set. The partcmd_invalidate is used 1685 * by update_cpumask() with NULL newmask. In both cases, the callers won't 1686 * check for error and so partition_root_state and prs_err will be updated 1687 * directly. 1688 */ 1689 static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, 1690 struct cpumask *newmask, 1691 struct tmpmasks *tmp) 1692 { 1693 struct cpuset *parent = parent_cs(cs); 1694 int adding; /* Adding cpus to parent's effective_cpus */ 1695 int deleting; /* Deleting cpus from parent's effective_cpus */ 1696 int old_prs, new_prs; 1697 int part_error = PERR_NONE; /* Partition error? */ 1698 int subparts_delta = 0; 1699 int isolcpus_updated = 0; 1700 struct cpumask *xcpus = user_xcpus(cs); 1701 bool nocpu; 1702 1703 lockdep_assert_held(&cpuset_mutex); 1704 WARN_ON_ONCE(is_remote_partition(cs)); /* For local partition only */ 1705 1706 /* 1707 * new_prs will only be changed for the partcmd_update and 1708 * partcmd_invalidate commands. 1709 */ 1710 adding = deleting = false; 1711 old_prs = new_prs = cs->partition_root_state; 1712 1713 if (cmd == partcmd_invalidate) { 1714 if (is_prs_invalid(old_prs)) 1715 return 0; 1716 1717 /* 1718 * Make the current partition invalid. 1719 */ 1720 if (is_partition_valid(parent)) 1721 adding = cpumask_and(tmp->addmask, 1722 xcpus, parent->effective_xcpus); 1723 if (old_prs > 0) { 1724 new_prs = -old_prs; 1725 subparts_delta--; 1726 } 1727 goto write_error; 1728 } 1729 1730 /* 1731 * The parent must be a partition root. 1732 * The new cpumask, if present, or the current cpus_allowed must 1733 * not be empty. 1734 */ 1735 if (!is_partition_valid(parent)) { 1736 return is_partition_invalid(parent) 1737 ? PERR_INVPARENT : PERR_NOTPART; 1738 } 1739 if (!newmask && xcpus_empty(cs)) 1740 return PERR_CPUSEMPTY; 1741 1742 nocpu = tasks_nocpu_error(parent, cs, xcpus); 1743 1744 if ((cmd == partcmd_enable) || (cmd == partcmd_enablei)) { 1745 /* 1746 * Need to call compute_effective_exclusive_cpumask() in case 1747 * exclusive_cpus not set. Sibling conflict should only happen 1748 * if exclusive_cpus isn't set. 1749 */ 1750 xcpus = tmp->delmask; 1751 if (compute_effective_exclusive_cpumask(cs, xcpus, NULL)) 1752 WARN_ON_ONCE(!cpumask_empty(cs->exclusive_cpus)); 1753 1754 /* 1755 * Enabling partition root is not allowed if its 1756 * effective_xcpus is empty. 1757 */ 1758 if (cpumask_empty(xcpus)) 1759 return PERR_INVCPUS; 1760 1761 if (prstate_housekeeping_conflict(new_prs, xcpus)) 1762 return PERR_HKEEPING; 1763 1764 /* 1765 * A parent can be left with no CPU as long as there is no 1766 * task directly associated with the parent partition. 1767 */ 1768 if (nocpu) 1769 return PERR_NOCPUS; 1770 1771 /* 1772 * This function will only be called when all the preliminary 1773 * checks have passed. At this point, the following condition 1774 * should hold. 1775 * 1776 * (cs->effective_xcpus & cpu_active_mask) ⊆ parent->effective_cpus 1777 * 1778 * Warn if it is not the case. 1779 */ 1780 cpumask_and(tmp->new_cpus, xcpus, cpu_active_mask); 1781 WARN_ON_ONCE(!cpumask_subset(tmp->new_cpus, parent->effective_cpus)); 1782 1783 deleting = true; 1784 subparts_delta++; 1785 new_prs = (cmd == partcmd_enable) ? PRS_ROOT : PRS_ISOLATED; 1786 } else if (cmd == partcmd_disable) { 1787 /* 1788 * May need to add cpus back to parent's effective_cpus 1789 * (and maybe removed from subpartitions_cpus/isolated_cpus) 1790 * for valid partition root. xcpus may contain CPUs that 1791 * shouldn't be removed from the two global cpumasks. 1792 */ 1793 if (is_partition_valid(cs)) { 1794 cpumask_copy(tmp->addmask, cs->effective_xcpus); 1795 adding = true; 1796 subparts_delta--; 1797 } 1798 new_prs = PRS_MEMBER; 1799 } else if (newmask) { 1800 /* 1801 * Empty cpumask is not allowed 1802 */ 1803 if (cpumask_empty(newmask)) { 1804 part_error = PERR_CPUSEMPTY; 1805 goto write_error; 1806 } 1807 1808 /* Check newmask again, whether cpus are available for parent/cs */ 1809 nocpu |= tasks_nocpu_error(parent, cs, newmask); 1810 1811 /* 1812 * partcmd_update with newmask: 1813 * 1814 * Compute add/delete mask to/from effective_cpus 1815 * 1816 * For valid partition: 1817 * addmask = exclusive_cpus & ~newmask 1818 * & parent->effective_xcpus 1819 * delmask = newmask & ~exclusive_cpus 1820 * & parent->effective_xcpus 1821 * 1822 * For invalid partition: 1823 * delmask = newmask & parent->effective_xcpus 1824 */ 1825 if (is_prs_invalid(old_prs)) { 1826 adding = false; 1827 deleting = cpumask_and(tmp->delmask, 1828 newmask, parent->effective_xcpus); 1829 } else { 1830 cpumask_andnot(tmp->addmask, xcpus, newmask); 1831 adding = cpumask_and(tmp->addmask, tmp->addmask, 1832 parent->effective_xcpus); 1833 1834 cpumask_andnot(tmp->delmask, newmask, xcpus); 1835 deleting = cpumask_and(tmp->delmask, tmp->delmask, 1836 parent->effective_xcpus); 1837 } 1838 /* 1839 * The new CPUs to be removed from parent's effective CPUs 1840 * must be present. 1841 */ 1842 if (deleting) { 1843 cpumask_and(tmp->new_cpus, tmp->delmask, cpu_active_mask); 1844 WARN_ON_ONCE(!cpumask_subset(tmp->new_cpus, parent->effective_cpus)); 1845 } 1846 1847 /* 1848 * Make partition invalid if parent's effective_cpus could 1849 * become empty and there are tasks in the parent. 1850 */ 1851 if (nocpu && (!adding || 1852 !cpumask_intersects(tmp->addmask, cpu_active_mask))) { 1853 part_error = PERR_NOCPUS; 1854 deleting = false; 1855 adding = cpumask_and(tmp->addmask, 1856 xcpus, parent->effective_xcpus); 1857 } 1858 } else { 1859 /* 1860 * partcmd_update w/o newmask 1861 * 1862 * delmask = effective_xcpus & parent->effective_cpus 1863 * 1864 * This can be called from: 1865 * 1) update_cpumasks_hier() 1866 * 2) cpuset_hotplug_update_tasks() 1867 * 1868 * Check to see if it can be transitioned from valid to 1869 * invalid partition or vice versa. 1870 * 1871 * A partition error happens when parent has tasks and all 1872 * its effective CPUs will have to be distributed out. 1873 */ 1874 WARN_ON_ONCE(!is_partition_valid(parent)); 1875 if (nocpu) { 1876 part_error = PERR_NOCPUS; 1877 if (is_partition_valid(cs)) 1878 adding = cpumask_and(tmp->addmask, 1879 xcpus, parent->effective_xcpus); 1880 } else if (is_partition_invalid(cs) && !cpumask_empty(xcpus) && 1881 cpumask_subset(xcpus, parent->effective_xcpus)) { 1882 struct cgroup_subsys_state *css; 1883 struct cpuset *child; 1884 bool exclusive = true; 1885 1886 /* 1887 * Convert invalid partition to valid has to 1888 * pass the cpu exclusivity test. 1889 */ 1890 rcu_read_lock(); 1891 cpuset_for_each_child(child, css, parent) { 1892 if (child == cs) 1893 continue; 1894 if (!cpusets_are_exclusive(cs, child)) { 1895 exclusive = false; 1896 break; 1897 } 1898 } 1899 rcu_read_unlock(); 1900 if (exclusive) 1901 deleting = cpumask_and(tmp->delmask, 1902 xcpus, parent->effective_cpus); 1903 else 1904 part_error = PERR_NOTEXCL; 1905 } 1906 } 1907 1908 write_error: 1909 if (part_error) 1910 WRITE_ONCE(cs->prs_err, part_error); 1911 1912 if (cmd == partcmd_update) { 1913 /* 1914 * Check for possible transition between valid and invalid 1915 * partition root. 1916 */ 1917 switch (cs->partition_root_state) { 1918 case PRS_ROOT: 1919 case PRS_ISOLATED: 1920 if (part_error) { 1921 new_prs = -old_prs; 1922 subparts_delta--; 1923 } 1924 break; 1925 case PRS_INVALID_ROOT: 1926 case PRS_INVALID_ISOLATED: 1927 if (!part_error) { 1928 new_prs = -old_prs; 1929 subparts_delta++; 1930 } 1931 break; 1932 } 1933 } 1934 1935 if (!adding && !deleting && (new_prs == old_prs)) 1936 return 0; 1937 1938 /* 1939 * Transitioning between invalid to valid or vice versa may require 1940 * changing CS_CPU_EXCLUSIVE. In the case of partcmd_update, 1941 * validate_change() has already been successfully called and 1942 * CPU lists in cs haven't been updated yet. So defer it to later. 1943 */ 1944 if ((old_prs != new_prs) && (cmd != partcmd_update)) { 1945 int err = update_partition_exclusive_flag(cs, new_prs); 1946 1947 if (err) 1948 return err; 1949 } 1950 1951 /* 1952 * Change the parent's effective_cpus & effective_xcpus (top cpuset 1953 * only). 1954 * 1955 * Newly added CPUs will be removed from effective_cpus and 1956 * newly deleted ones will be added back to effective_cpus. 1957 */ 1958 spin_lock_irq(&callback_lock); 1959 if (old_prs != new_prs) { 1960 cs->partition_root_state = new_prs; 1961 if (new_prs <= 0) 1962 cs->nr_subparts = 0; 1963 } 1964 /* 1965 * Adding to parent's effective_cpus means deletion CPUs from cs 1966 * and vice versa. 1967 */ 1968 if (adding) 1969 isolcpus_updated += partition_xcpus_del(old_prs, parent, 1970 tmp->addmask); 1971 if (deleting) 1972 isolcpus_updated += partition_xcpus_add(new_prs, parent, 1973 tmp->delmask); 1974 1975 if (is_partition_valid(parent)) { 1976 parent->nr_subparts += subparts_delta; 1977 WARN_ON_ONCE(parent->nr_subparts < 0); 1978 } 1979 spin_unlock_irq(&callback_lock); 1980 update_unbound_workqueue_cpumask(isolcpus_updated); 1981 1982 if ((old_prs != new_prs) && (cmd == partcmd_update)) 1983 update_partition_exclusive_flag(cs, new_prs); 1984 1985 if (adding || deleting) { 1986 cpuset_update_tasks_cpumask(parent, tmp->addmask); 1987 update_sibling_cpumasks(parent, cs, tmp); 1988 } 1989 1990 /* 1991 * For partcmd_update without newmask, it is being called from 1992 * cpuset_handle_hotplug(). Update the load balance flag and 1993 * scheduling domain accordingly. 1994 */ 1995 if ((cmd == partcmd_update) && !newmask) 1996 update_partition_sd_lb(cs, old_prs); 1997 1998 notify_partition_change(cs, old_prs); 1999 return 0; 2000 } 2001 2002 /** 2003 * compute_partition_effective_cpumask - compute effective_cpus for partition 2004 * @cs: partition root cpuset 2005 * @new_ecpus: previously computed effective_cpus to be updated 2006 * 2007 * Compute the effective_cpus of a partition root by scanning effective_xcpus 2008 * of child partition roots and excluding their effective_xcpus. 2009 * 2010 * This has the side effect of invalidating valid child partition roots, 2011 * if necessary. Since it is called from either cpuset_hotplug_update_tasks() 2012 * or update_cpumasks_hier() where parent and children are modified 2013 * successively, we don't need to call update_parent_effective_cpumask() 2014 * and the child's effective_cpus will be updated in later iterations. 2015 * 2016 * Note that rcu_read_lock() is assumed to be held. 2017 */ 2018 static void compute_partition_effective_cpumask(struct cpuset *cs, 2019 struct cpumask *new_ecpus) 2020 { 2021 struct cgroup_subsys_state *css; 2022 struct cpuset *child; 2023 bool populated = partition_is_populated(cs, NULL); 2024 2025 /* 2026 * Check child partition roots to see if they should be 2027 * invalidated when 2028 * 1) child effective_xcpus not a subset of new 2029 * excluisve_cpus 2030 * 2) All the effective_cpus will be used up and cp 2031 * has tasks 2032 */ 2033 compute_effective_exclusive_cpumask(cs, new_ecpus, NULL); 2034 cpumask_and(new_ecpus, new_ecpus, cpu_active_mask); 2035 2036 rcu_read_lock(); 2037 cpuset_for_each_child(child, css, cs) { 2038 if (!is_partition_valid(child)) 2039 continue; 2040 2041 /* 2042 * There shouldn't be a remote partition underneath another 2043 * partition root. 2044 */ 2045 WARN_ON_ONCE(is_remote_partition(child)); 2046 child->prs_err = 0; 2047 if (!cpumask_subset(child->effective_xcpus, 2048 cs->effective_xcpus)) 2049 child->prs_err = PERR_INVCPUS; 2050 else if (populated && 2051 cpumask_subset(new_ecpus, child->effective_xcpus)) 2052 child->prs_err = PERR_NOCPUS; 2053 2054 if (child->prs_err) { 2055 int old_prs = child->partition_root_state; 2056 2057 /* 2058 * Invalidate child partition 2059 */ 2060 spin_lock_irq(&callback_lock); 2061 make_partition_invalid(child); 2062 cs->nr_subparts--; 2063 child->nr_subparts = 0; 2064 spin_unlock_irq(&callback_lock); 2065 notify_partition_change(child, old_prs); 2066 continue; 2067 } 2068 cpumask_andnot(new_ecpus, new_ecpus, 2069 child->effective_xcpus); 2070 } 2071 rcu_read_unlock(); 2072 } 2073 2074 /* 2075 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree 2076 * @cs: the cpuset to consider 2077 * @tmp: temp variables for calculating effective_cpus & partition setup 2078 * @force: don't skip any descendant cpusets if set 2079 * 2080 * When configured cpumask is changed, the effective cpumasks of this cpuset 2081 * and all its descendants need to be updated. 2082 * 2083 * On legacy hierarchy, effective_cpus will be the same with cpu_allowed. 2084 * 2085 * Called with cpuset_mutex held 2086 */ 2087 static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp, 2088 bool force) 2089 { 2090 struct cpuset *cp; 2091 struct cgroup_subsys_state *pos_css; 2092 bool need_rebuild_sched_domains = false; 2093 int old_prs, new_prs; 2094 2095 rcu_read_lock(); 2096 cpuset_for_each_descendant_pre(cp, pos_css, cs) { 2097 struct cpuset *parent = parent_cs(cp); 2098 bool remote = is_remote_partition(cp); 2099 bool update_parent = false; 2100 2101 old_prs = new_prs = cp->partition_root_state; 2102 2103 /* 2104 * For child remote partition root (!= cs), we need to call 2105 * remote_cpus_update() if effective_xcpus will be changed. 2106 * Otherwise, we can skip the whole subtree. 2107 * 2108 * remote_cpus_update() will reuse tmp->new_cpus only after 2109 * its value is being processed. 2110 */ 2111 if (remote && (cp != cs)) { 2112 compute_effective_exclusive_cpumask(cp, tmp->new_cpus, NULL); 2113 if (cpumask_equal(cp->effective_xcpus, tmp->new_cpus)) { 2114 pos_css = css_rightmost_descendant(pos_css); 2115 continue; 2116 } 2117 rcu_read_unlock(); 2118 remote_cpus_update(cp, NULL, tmp->new_cpus, tmp); 2119 rcu_read_lock(); 2120 2121 /* Remote partition may be invalidated */ 2122 new_prs = cp->partition_root_state; 2123 remote = (new_prs == old_prs); 2124 } 2125 2126 if (remote || (is_partition_valid(parent) && is_partition_valid(cp))) 2127 compute_partition_effective_cpumask(cp, tmp->new_cpus); 2128 else 2129 compute_effective_cpumask(tmp->new_cpus, cp, parent); 2130 2131 if (remote) 2132 goto get_css; /* Ready to update cpuset data */ 2133 2134 /* 2135 * A partition with no effective_cpus is allowed as long as 2136 * there is no task associated with it. Call 2137 * update_parent_effective_cpumask() to check it. 2138 */ 2139 if (is_partition_valid(cp) && cpumask_empty(tmp->new_cpus)) { 2140 update_parent = true; 2141 goto update_parent_effective; 2142 } 2143 2144 /* 2145 * If it becomes empty, inherit the effective mask of the 2146 * parent, which is guaranteed to have some CPUs unless 2147 * it is a partition root that has explicitly distributed 2148 * out all its CPUs. 2149 */ 2150 if (is_in_v2_mode() && !remote && cpumask_empty(tmp->new_cpus)) 2151 cpumask_copy(tmp->new_cpus, parent->effective_cpus); 2152 2153 /* 2154 * Skip the whole subtree if 2155 * 1) the cpumask remains the same, 2156 * 2) has no partition root state, 2157 * 3) force flag not set, and 2158 * 4) for v2 load balance state same as its parent. 2159 */ 2160 if (!cp->partition_root_state && !force && 2161 cpumask_equal(tmp->new_cpus, cp->effective_cpus) && 2162 (!cpuset_v2() || 2163 (is_sched_load_balance(parent) == is_sched_load_balance(cp)))) { 2164 pos_css = css_rightmost_descendant(pos_css); 2165 continue; 2166 } 2167 2168 update_parent_effective: 2169 /* 2170 * update_parent_effective_cpumask() should have been called 2171 * for cs already in update_cpumask(). We should also call 2172 * cpuset_update_tasks_cpumask() again for tasks in the parent 2173 * cpuset if the parent's effective_cpus changes. 2174 */ 2175 if ((cp != cs) && old_prs) { 2176 switch (parent->partition_root_state) { 2177 case PRS_ROOT: 2178 case PRS_ISOLATED: 2179 update_parent = true; 2180 break; 2181 2182 default: 2183 /* 2184 * When parent is not a partition root or is 2185 * invalid, child partition roots become 2186 * invalid too. 2187 */ 2188 if (is_partition_valid(cp)) 2189 new_prs = -cp->partition_root_state; 2190 WRITE_ONCE(cp->prs_err, 2191 is_partition_invalid(parent) 2192 ? PERR_INVPARENT : PERR_NOTPART); 2193 break; 2194 } 2195 } 2196 get_css: 2197 if (!css_tryget_online(&cp->css)) 2198 continue; 2199 rcu_read_unlock(); 2200 2201 if (update_parent) { 2202 update_parent_effective_cpumask(cp, partcmd_update, NULL, tmp); 2203 /* 2204 * The cpuset partition_root_state may become 2205 * invalid. Capture it. 2206 */ 2207 new_prs = cp->partition_root_state; 2208 } 2209 2210 spin_lock_irq(&callback_lock); 2211 cpumask_copy(cp->effective_cpus, tmp->new_cpus); 2212 cp->partition_root_state = new_prs; 2213 if (!cpumask_empty(cp->exclusive_cpus) && (cp != cs)) 2214 compute_effective_exclusive_cpumask(cp, NULL, NULL); 2215 2216 /* 2217 * Make sure effective_xcpus is properly set for a valid 2218 * partition root. 2219 */ 2220 if ((new_prs > 0) && cpumask_empty(cp->exclusive_cpus)) 2221 cpumask_and(cp->effective_xcpus, 2222 cp->cpus_allowed, parent->effective_xcpus); 2223 else if (new_prs < 0) 2224 reset_partition_data(cp); 2225 spin_unlock_irq(&callback_lock); 2226 2227 notify_partition_change(cp, old_prs); 2228 2229 WARN_ON(!is_in_v2_mode() && 2230 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus)); 2231 2232 cpuset_update_tasks_cpumask(cp, cp->effective_cpus); 2233 2234 /* 2235 * On default hierarchy, inherit the CS_SCHED_LOAD_BALANCE 2236 * from parent if current cpuset isn't a valid partition root 2237 * and their load balance states differ. 2238 */ 2239 if (cpuset_v2() && !is_partition_valid(cp) && 2240 (is_sched_load_balance(parent) != is_sched_load_balance(cp))) { 2241 if (is_sched_load_balance(parent)) 2242 set_bit(CS_SCHED_LOAD_BALANCE, &cp->flags); 2243 else 2244 clear_bit(CS_SCHED_LOAD_BALANCE, &cp->flags); 2245 } 2246 2247 /* 2248 * On legacy hierarchy, if the effective cpumask of any non- 2249 * empty cpuset is changed, we need to rebuild sched domains. 2250 * On default hierarchy, the cpuset needs to be a partition 2251 * root as well. 2252 */ 2253 if (!cpumask_empty(cp->cpus_allowed) && 2254 is_sched_load_balance(cp) && 2255 (!cpuset_v2() || is_partition_valid(cp))) 2256 need_rebuild_sched_domains = true; 2257 2258 rcu_read_lock(); 2259 css_put(&cp->css); 2260 } 2261 rcu_read_unlock(); 2262 2263 if (need_rebuild_sched_domains) 2264 cpuset_force_rebuild(); 2265 } 2266 2267 /** 2268 * update_sibling_cpumasks - Update siblings cpumasks 2269 * @parent: Parent cpuset 2270 * @cs: Current cpuset 2271 * @tmp: Temp variables 2272 */ 2273 static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs, 2274 struct tmpmasks *tmp) 2275 { 2276 struct cpuset *sibling; 2277 struct cgroup_subsys_state *pos_css; 2278 2279 lockdep_assert_held(&cpuset_mutex); 2280 2281 /* 2282 * Check all its siblings and call update_cpumasks_hier() 2283 * if their effective_cpus will need to be changed. 2284 * 2285 * It is possible a change in parent's effective_cpus 2286 * due to a change in a child partition's effective_xcpus will impact 2287 * its siblings even if they do not inherit parent's effective_cpus 2288 * directly. 2289 * 2290 * The update_cpumasks_hier() function may sleep. So we have to 2291 * release the RCU read lock before calling it. 2292 */ 2293 rcu_read_lock(); 2294 cpuset_for_each_child(sibling, pos_css, parent) { 2295 if (sibling == cs) 2296 continue; 2297 if (!is_partition_valid(sibling)) { 2298 compute_effective_cpumask(tmp->new_cpus, sibling, 2299 parent); 2300 if (cpumask_equal(tmp->new_cpus, sibling->effective_cpus)) 2301 continue; 2302 } else if (is_remote_partition(sibling)) { 2303 /* 2304 * Change in a sibling cpuset won't affect a remote 2305 * partition root. 2306 */ 2307 continue; 2308 } 2309 2310 if (!css_tryget_online(&sibling->css)) 2311 continue; 2312 2313 rcu_read_unlock(); 2314 update_cpumasks_hier(sibling, tmp, false); 2315 rcu_read_lock(); 2316 css_put(&sibling->css); 2317 } 2318 rcu_read_unlock(); 2319 } 2320 2321 /** 2322 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it 2323 * @cs: the cpuset to consider 2324 * @trialcs: trial cpuset 2325 * @buf: buffer of cpu numbers written to this cpuset 2326 */ 2327 static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, 2328 const char *buf) 2329 { 2330 int retval; 2331 struct tmpmasks tmp; 2332 struct cpuset *parent = parent_cs(cs); 2333 bool invalidate = false; 2334 bool force = false; 2335 int old_prs = cs->partition_root_state; 2336 2337 /* top_cpuset.cpus_allowed tracks cpu_active_mask; it's read-only */ 2338 if (cs == &top_cpuset) 2339 return -EACCES; 2340 2341 /* 2342 * An empty cpus_allowed is ok only if the cpuset has no tasks. 2343 * Since cpulist_parse() fails on an empty mask, we special case 2344 * that parsing. The validate_change() call ensures that cpusets 2345 * with tasks have cpus. 2346 */ 2347 if (!*buf) { 2348 cpumask_clear(trialcs->cpus_allowed); 2349 if (cpumask_empty(trialcs->exclusive_cpus)) 2350 cpumask_clear(trialcs->effective_xcpus); 2351 } else { 2352 retval = cpulist_parse(buf, trialcs->cpus_allowed); 2353 if (retval < 0) 2354 return retval; 2355 2356 if (!cpumask_subset(trialcs->cpus_allowed, 2357 top_cpuset.cpus_allowed)) 2358 return -EINVAL; 2359 2360 /* 2361 * When exclusive_cpus isn't explicitly set, it is constrained 2362 * by cpus_allowed and parent's effective_xcpus. Otherwise, 2363 * trialcs->effective_xcpus is used as a temporary cpumask 2364 * for checking validity of the partition root. 2365 */ 2366 trialcs->partition_root_state = PRS_MEMBER; 2367 if (!cpumask_empty(trialcs->exclusive_cpus) || is_partition_valid(cs)) 2368 compute_effective_exclusive_cpumask(trialcs, NULL, cs); 2369 } 2370 2371 /* Nothing to do if the cpus didn't change */ 2372 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed)) 2373 return 0; 2374 2375 if (alloc_tmpmasks(&tmp)) 2376 return -ENOMEM; 2377 2378 if (old_prs) { 2379 if (is_partition_valid(cs) && 2380 cpumask_empty(trialcs->effective_xcpus)) { 2381 invalidate = true; 2382 cs->prs_err = PERR_INVCPUS; 2383 } else if (prstate_housekeeping_conflict(old_prs, trialcs->effective_xcpus)) { 2384 invalidate = true; 2385 cs->prs_err = PERR_HKEEPING; 2386 } else if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus)) { 2387 invalidate = true; 2388 cs->prs_err = PERR_NOCPUS; 2389 } 2390 } 2391 2392 /* 2393 * Check all the descendants in update_cpumasks_hier() if 2394 * effective_xcpus is to be changed. 2395 */ 2396 force = !cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus); 2397 2398 retval = validate_change(cs, trialcs); 2399 2400 if ((retval == -EINVAL) && cpuset_v2()) { 2401 struct cgroup_subsys_state *css; 2402 struct cpuset *cp; 2403 2404 /* 2405 * The -EINVAL error code indicates that partition sibling 2406 * CPU exclusivity rule has been violated. We still allow 2407 * the cpumask change to proceed while invalidating the 2408 * partition. However, any conflicting sibling partitions 2409 * have to be marked as invalid too. 2410 */ 2411 invalidate = true; 2412 rcu_read_lock(); 2413 cpuset_for_each_child(cp, css, parent) { 2414 struct cpumask *xcpus = user_xcpus(trialcs); 2415 2416 if (is_partition_valid(cp) && 2417 cpumask_intersects(xcpus, cp->effective_xcpus)) { 2418 rcu_read_unlock(); 2419 update_parent_effective_cpumask(cp, partcmd_invalidate, NULL, &tmp); 2420 rcu_read_lock(); 2421 } 2422 } 2423 rcu_read_unlock(); 2424 retval = 0; 2425 } 2426 2427 if (retval < 0) 2428 goto out_free; 2429 2430 if (is_partition_valid(cs) || 2431 (is_partition_invalid(cs) && !invalidate)) { 2432 struct cpumask *xcpus = trialcs->effective_xcpus; 2433 2434 if (cpumask_empty(xcpus) && is_partition_invalid(cs)) 2435 xcpus = trialcs->cpus_allowed; 2436 2437 /* 2438 * Call remote_cpus_update() to handle valid remote partition 2439 */ 2440 if (is_remote_partition(cs)) 2441 remote_cpus_update(cs, NULL, xcpus, &tmp); 2442 else if (invalidate) 2443 update_parent_effective_cpumask(cs, partcmd_invalidate, 2444 NULL, &tmp); 2445 else 2446 update_parent_effective_cpumask(cs, partcmd_update, 2447 xcpus, &tmp); 2448 } 2449 2450 spin_lock_irq(&callback_lock); 2451 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); 2452 cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus); 2453 if ((old_prs > 0) && !is_partition_valid(cs)) 2454 reset_partition_data(cs); 2455 spin_unlock_irq(&callback_lock); 2456 2457 /* effective_cpus/effective_xcpus will be updated here */ 2458 update_cpumasks_hier(cs, &tmp, force); 2459 2460 /* Update CS_SCHED_LOAD_BALANCE and/or sched_domains, if necessary */ 2461 if (cs->partition_root_state) 2462 update_partition_sd_lb(cs, old_prs); 2463 out_free: 2464 free_tmpmasks(&tmp); 2465 return retval; 2466 } 2467 2468 /** 2469 * update_exclusive_cpumask - update the exclusive_cpus mask of a cpuset 2470 * @cs: the cpuset to consider 2471 * @trialcs: trial cpuset 2472 * @buf: buffer of cpu numbers written to this cpuset 2473 * 2474 * The tasks' cpumask will be updated if cs is a valid partition root. 2475 */ 2476 static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs, 2477 const char *buf) 2478 { 2479 int retval; 2480 struct tmpmasks tmp; 2481 struct cpuset *parent = parent_cs(cs); 2482 bool invalidate = false; 2483 bool force = false; 2484 int old_prs = cs->partition_root_state; 2485 2486 if (!*buf) { 2487 cpumask_clear(trialcs->exclusive_cpus); 2488 cpumask_clear(trialcs->effective_xcpus); 2489 } else { 2490 retval = cpulist_parse(buf, trialcs->exclusive_cpus); 2491 if (retval < 0) 2492 return retval; 2493 } 2494 2495 /* Nothing to do if the CPUs didn't change */ 2496 if (cpumask_equal(cs->exclusive_cpus, trialcs->exclusive_cpus)) 2497 return 0; 2498 2499 if (*buf) { 2500 trialcs->partition_root_state = PRS_MEMBER; 2501 /* 2502 * Reject the change if there is exclusive CPUs conflict with 2503 * the siblings. 2504 */ 2505 if (compute_effective_exclusive_cpumask(trialcs, NULL, cs)) 2506 return -EINVAL; 2507 } 2508 2509 /* 2510 * Check all the descendants in update_cpumasks_hier() if 2511 * effective_xcpus is to be changed. 2512 */ 2513 force = !cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus); 2514 2515 retval = validate_change(cs, trialcs); 2516 if (retval) 2517 return retval; 2518 2519 if (alloc_tmpmasks(&tmp)) 2520 return -ENOMEM; 2521 2522 if (old_prs) { 2523 if (cpumask_empty(trialcs->effective_xcpus)) { 2524 invalidate = true; 2525 cs->prs_err = PERR_INVCPUS; 2526 } else if (prstate_housekeeping_conflict(old_prs, trialcs->effective_xcpus)) { 2527 invalidate = true; 2528 cs->prs_err = PERR_HKEEPING; 2529 } else if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus)) { 2530 invalidate = true; 2531 cs->prs_err = PERR_NOCPUS; 2532 } 2533 2534 if (is_remote_partition(cs)) { 2535 if (invalidate) 2536 remote_partition_disable(cs, &tmp); 2537 else 2538 remote_cpus_update(cs, trialcs->exclusive_cpus, 2539 trialcs->effective_xcpus, &tmp); 2540 } else if (invalidate) { 2541 update_parent_effective_cpumask(cs, partcmd_invalidate, 2542 NULL, &tmp); 2543 } else { 2544 update_parent_effective_cpumask(cs, partcmd_update, 2545 trialcs->effective_xcpus, &tmp); 2546 } 2547 } 2548 spin_lock_irq(&callback_lock); 2549 cpumask_copy(cs->exclusive_cpus, trialcs->exclusive_cpus); 2550 cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus); 2551 if ((old_prs > 0) && !is_partition_valid(cs)) 2552 reset_partition_data(cs); 2553 spin_unlock_irq(&callback_lock); 2554 2555 /* 2556 * Call update_cpumasks_hier() to update effective_cpus/effective_xcpus 2557 * of the subtree when it is a valid partition root or effective_xcpus 2558 * is updated. 2559 */ 2560 if (is_partition_valid(cs) || force) 2561 update_cpumasks_hier(cs, &tmp, force); 2562 2563 /* Update CS_SCHED_LOAD_BALANCE and/or sched_domains, if necessary */ 2564 if (cs->partition_root_state) 2565 update_partition_sd_lb(cs, old_prs); 2566 2567 free_tmpmasks(&tmp); 2568 return 0; 2569 } 2570 2571 /* 2572 * Migrate memory region from one set of nodes to another. This is 2573 * performed asynchronously as it can be called from process migration path 2574 * holding locks involved in process management. All mm migrations are 2575 * performed in the queued order and can be waited for by flushing 2576 * cpuset_migrate_mm_wq. 2577 */ 2578 2579 struct cpuset_migrate_mm_work { 2580 struct work_struct work; 2581 struct mm_struct *mm; 2582 nodemask_t from; 2583 nodemask_t to; 2584 }; 2585 2586 static void cpuset_migrate_mm_workfn(struct work_struct *work) 2587 { 2588 struct cpuset_migrate_mm_work *mwork = 2589 container_of(work, struct cpuset_migrate_mm_work, work); 2590 2591 /* on a wq worker, no need to worry about %current's mems_allowed */ 2592 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL); 2593 mmput(mwork->mm); 2594 kfree(mwork); 2595 } 2596 2597 static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, 2598 const nodemask_t *to) 2599 { 2600 struct cpuset_migrate_mm_work *mwork; 2601 2602 if (nodes_equal(*from, *to)) { 2603 mmput(mm); 2604 return; 2605 } 2606 2607 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL); 2608 if (mwork) { 2609 mwork->mm = mm; 2610 mwork->from = *from; 2611 mwork->to = *to; 2612 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn); 2613 queue_work(cpuset_migrate_mm_wq, &mwork->work); 2614 } else { 2615 mmput(mm); 2616 } 2617 } 2618 2619 static void cpuset_post_attach(void) 2620 { 2621 flush_workqueue(cpuset_migrate_mm_wq); 2622 } 2623 2624 /* 2625 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy 2626 * @tsk: the task to change 2627 * @newmems: new nodes that the task will be set 2628 * 2629 * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed 2630 * and rebind an eventual tasks' mempolicy. If the task is allocating in 2631 * parallel, it might temporarily see an empty intersection, which results in 2632 * a seqlock check and retry before OOM or allocation failure. 2633 */ 2634 static void cpuset_change_task_nodemask(struct task_struct *tsk, 2635 nodemask_t *newmems) 2636 { 2637 task_lock(tsk); 2638 2639 local_irq_disable(); 2640 write_seqcount_begin(&tsk->mems_allowed_seq); 2641 2642 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); 2643 mpol_rebind_task(tsk, newmems); 2644 tsk->mems_allowed = *newmems; 2645 2646 write_seqcount_end(&tsk->mems_allowed_seq); 2647 local_irq_enable(); 2648 2649 task_unlock(tsk); 2650 } 2651 2652 static void *cpuset_being_rebound; 2653 2654 /** 2655 * cpuset_update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. 2656 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed 2657 * 2658 * Iterate through each task of @cs updating its mems_allowed to the 2659 * effective cpuset's. As this function is called with cpuset_mutex held, 2660 * cpuset membership stays stable. 2661 */ 2662 void cpuset_update_tasks_nodemask(struct cpuset *cs) 2663 { 2664 static nodemask_t newmems; /* protected by cpuset_mutex */ 2665 struct css_task_iter it; 2666 struct task_struct *task; 2667 2668 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ 2669 2670 guarantee_online_mems(cs, &newmems); 2671 2672 /* 2673 * The mpol_rebind_mm() call takes mmap_lock, which we couldn't 2674 * take while holding tasklist_lock. Forks can happen - the 2675 * mpol_dup() cpuset_being_rebound check will catch such forks, 2676 * and rebind their vma mempolicies too. Because we still hold 2677 * the global cpuset_mutex, we know that no other rebind effort 2678 * will be contending for the global variable cpuset_being_rebound. 2679 * It's ok if we rebind the same mm twice; mpol_rebind_mm() 2680 * is idempotent. Also migrate pages in each mm to new nodes. 2681 */ 2682 css_task_iter_start(&cs->css, 0, &it); 2683 while ((task = css_task_iter_next(&it))) { 2684 struct mm_struct *mm; 2685 bool migrate; 2686 2687 cpuset_change_task_nodemask(task, &newmems); 2688 2689 mm = get_task_mm(task); 2690 if (!mm) 2691 continue; 2692 2693 migrate = is_memory_migrate(cs); 2694 2695 mpol_rebind_mm(mm, &cs->mems_allowed); 2696 if (migrate) 2697 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems); 2698 else 2699 mmput(mm); 2700 } 2701 css_task_iter_end(&it); 2702 2703 /* 2704 * All the tasks' nodemasks have been updated, update 2705 * cs->old_mems_allowed. 2706 */ 2707 cs->old_mems_allowed = newmems; 2708 2709 /* We're done rebinding vmas to this cpuset's new mems_allowed. */ 2710 cpuset_being_rebound = NULL; 2711 } 2712 2713 /* 2714 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree 2715 * @cs: the cpuset to consider 2716 * @new_mems: a temp variable for calculating new effective_mems 2717 * 2718 * When configured nodemask is changed, the effective nodemasks of this cpuset 2719 * and all its descendants need to be updated. 2720 * 2721 * On legacy hierarchy, effective_mems will be the same with mems_allowed. 2722 * 2723 * Called with cpuset_mutex held 2724 */ 2725 static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) 2726 { 2727 struct cpuset *cp; 2728 struct cgroup_subsys_state *pos_css; 2729 2730 rcu_read_lock(); 2731 cpuset_for_each_descendant_pre(cp, pos_css, cs) { 2732 struct cpuset *parent = parent_cs(cp); 2733 2734 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems); 2735 2736 /* 2737 * If it becomes empty, inherit the effective mask of the 2738 * parent, which is guaranteed to have some MEMs. 2739 */ 2740 if (is_in_v2_mode() && nodes_empty(*new_mems)) 2741 *new_mems = parent->effective_mems; 2742 2743 /* Skip the whole subtree if the nodemask remains the same. */ 2744 if (nodes_equal(*new_mems, cp->effective_mems)) { 2745 pos_css = css_rightmost_descendant(pos_css); 2746 continue; 2747 } 2748 2749 if (!css_tryget_online(&cp->css)) 2750 continue; 2751 rcu_read_unlock(); 2752 2753 spin_lock_irq(&callback_lock); 2754 cp->effective_mems = *new_mems; 2755 spin_unlock_irq(&callback_lock); 2756 2757 WARN_ON(!is_in_v2_mode() && 2758 !nodes_equal(cp->mems_allowed, cp->effective_mems)); 2759 2760 cpuset_update_tasks_nodemask(cp); 2761 2762 rcu_read_lock(); 2763 css_put(&cp->css); 2764 } 2765 rcu_read_unlock(); 2766 } 2767 2768 /* 2769 * Handle user request to change the 'mems' memory placement 2770 * of a cpuset. Needs to validate the request, update the 2771 * cpusets mems_allowed, and for each task in the cpuset, 2772 * update mems_allowed and rebind task's mempolicy and any vma 2773 * mempolicies and if the cpuset is marked 'memory_migrate', 2774 * migrate the tasks pages to the new memory. 2775 * 2776 * Call with cpuset_mutex held. May take callback_lock during call. 2777 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, 2778 * lock each such tasks mm->mmap_lock, scan its vma's and rebind 2779 * their mempolicies to the cpusets new mems_allowed. 2780 */ 2781 static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, 2782 const char *buf) 2783 { 2784 int retval; 2785 2786 /* 2787 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY]; 2788 * it's read-only 2789 */ 2790 if (cs == &top_cpuset) { 2791 retval = -EACCES; 2792 goto done; 2793 } 2794 2795 /* 2796 * An empty mems_allowed is ok iff there are no tasks in the cpuset. 2797 * Since nodelist_parse() fails on an empty mask, we special case 2798 * that parsing. The validate_change() call ensures that cpusets 2799 * with tasks have memory. 2800 */ 2801 if (!*buf) { 2802 nodes_clear(trialcs->mems_allowed); 2803 } else { 2804 retval = nodelist_parse(buf, trialcs->mems_allowed); 2805 if (retval < 0) 2806 goto done; 2807 2808 if (!nodes_subset(trialcs->mems_allowed, 2809 top_cpuset.mems_allowed)) { 2810 retval = -EINVAL; 2811 goto done; 2812 } 2813 } 2814 2815 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) { 2816 retval = 0; /* Too easy - nothing to do */ 2817 goto done; 2818 } 2819 retval = validate_change(cs, trialcs); 2820 if (retval < 0) 2821 goto done; 2822 2823 check_insane_mems_config(&trialcs->mems_allowed); 2824 2825 spin_lock_irq(&callback_lock); 2826 cs->mems_allowed = trialcs->mems_allowed; 2827 spin_unlock_irq(&callback_lock); 2828 2829 /* use trialcs->mems_allowed as a temp variable */ 2830 update_nodemasks_hier(cs, &trialcs->mems_allowed); 2831 done: 2832 return retval; 2833 } 2834 2835 bool current_cpuset_is_being_rebound(void) 2836 { 2837 bool ret; 2838 2839 rcu_read_lock(); 2840 ret = task_cs(current) == cpuset_being_rebound; 2841 rcu_read_unlock(); 2842 2843 return ret; 2844 } 2845 2846 /* 2847 * cpuset_update_flag - read a 0 or a 1 in a file and update associated flag 2848 * bit: the bit to update (see cpuset_flagbits_t) 2849 * cs: the cpuset to update 2850 * turning_on: whether the flag is being set or cleared 2851 * 2852 * Call with cpuset_mutex held. 2853 */ 2854 2855 int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, 2856 int turning_on) 2857 { 2858 struct cpuset *trialcs; 2859 int balance_flag_changed; 2860 int spread_flag_changed; 2861 int err; 2862 2863 trialcs = dup_or_alloc_cpuset(cs); 2864 if (!trialcs) 2865 return -ENOMEM; 2866 2867 if (turning_on) 2868 set_bit(bit, &trialcs->flags); 2869 else 2870 clear_bit(bit, &trialcs->flags); 2871 2872 err = validate_change(cs, trialcs); 2873 if (err < 0) 2874 goto out; 2875 2876 balance_flag_changed = (is_sched_load_balance(cs) != 2877 is_sched_load_balance(trialcs)); 2878 2879 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs)) 2880 || (is_spread_page(cs) != is_spread_page(trialcs))); 2881 2882 spin_lock_irq(&callback_lock); 2883 cs->flags = trialcs->flags; 2884 spin_unlock_irq(&callback_lock); 2885 2886 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) { 2887 if (cpuset_v2()) 2888 cpuset_force_rebuild(); 2889 else 2890 rebuild_sched_domains_locked(); 2891 } 2892 2893 if (spread_flag_changed) 2894 cpuset1_update_tasks_flags(cs); 2895 out: 2896 free_cpuset(trialcs); 2897 return err; 2898 } 2899 2900 /** 2901 * update_prstate - update partition_root_state 2902 * @cs: the cpuset to update 2903 * @new_prs: new partition root state 2904 * Return: 0 if successful, != 0 if error 2905 * 2906 * Call with cpuset_mutex held. 2907 */ 2908 static int update_prstate(struct cpuset *cs, int new_prs) 2909 { 2910 int err = PERR_NONE, old_prs = cs->partition_root_state; 2911 struct cpuset *parent = parent_cs(cs); 2912 struct tmpmasks tmpmask; 2913 bool isolcpus_updated = false; 2914 2915 if (old_prs == new_prs) 2916 return 0; 2917 2918 /* 2919 * Treat a previously invalid partition root as if it is a "member". 2920 */ 2921 if (new_prs && is_prs_invalid(old_prs)) 2922 old_prs = PRS_MEMBER; 2923 2924 if (alloc_tmpmasks(&tmpmask)) 2925 return -ENOMEM; 2926 2927 err = update_partition_exclusive_flag(cs, new_prs); 2928 if (err) 2929 goto out; 2930 2931 if (!old_prs) { 2932 /* 2933 * cpus_allowed and exclusive_cpus cannot be both empty. 2934 */ 2935 if (xcpus_empty(cs)) { 2936 err = PERR_CPUSEMPTY; 2937 goto out; 2938 } 2939 2940 /* 2941 * We don't support the creation of a new local partition with 2942 * a remote partition underneath it. This unsupported 2943 * setting can happen only if parent is the top_cpuset because 2944 * a remote partition cannot be created underneath an existing 2945 * local or remote partition. 2946 */ 2947 if ((parent == &top_cpuset) && 2948 cpumask_intersects(cs->exclusive_cpus, subpartitions_cpus)) { 2949 err = PERR_REMOTE; 2950 goto out; 2951 } 2952 2953 /* 2954 * If parent is valid partition, enable local partiion. 2955 * Otherwise, enable a remote partition. 2956 */ 2957 if (is_partition_valid(parent)) { 2958 enum partition_cmd cmd = (new_prs == PRS_ROOT) 2959 ? partcmd_enable : partcmd_enablei; 2960 2961 err = update_parent_effective_cpumask(cs, cmd, NULL, &tmpmask); 2962 } else { 2963 err = remote_partition_enable(cs, new_prs, &tmpmask); 2964 } 2965 } else if (old_prs && new_prs) { 2966 /* 2967 * A change in load balance state only, no change in cpumasks. 2968 * Need to update isolated_cpus. 2969 */ 2970 isolcpus_updated = true; 2971 } else { 2972 /* 2973 * Switching back to member is always allowed even if it 2974 * disables child partitions. 2975 */ 2976 if (is_remote_partition(cs)) 2977 remote_partition_disable(cs, &tmpmask); 2978 else 2979 update_parent_effective_cpumask(cs, partcmd_disable, 2980 NULL, &tmpmask); 2981 2982 /* 2983 * Invalidation of child partitions will be done in 2984 * update_cpumasks_hier(). 2985 */ 2986 } 2987 out: 2988 /* 2989 * Make partition invalid & disable CS_CPU_EXCLUSIVE if an error 2990 * happens. 2991 */ 2992 if (err) { 2993 new_prs = -new_prs; 2994 update_partition_exclusive_flag(cs, new_prs); 2995 } 2996 2997 spin_lock_irq(&callback_lock); 2998 cs->partition_root_state = new_prs; 2999 WRITE_ONCE(cs->prs_err, err); 3000 if (!is_partition_valid(cs)) 3001 reset_partition_data(cs); 3002 else if (isolcpus_updated) 3003 isolated_cpus_update(old_prs, new_prs, cs->effective_xcpus); 3004 spin_unlock_irq(&callback_lock); 3005 update_unbound_workqueue_cpumask(isolcpus_updated); 3006 3007 /* Force update if switching back to member & update effective_xcpus */ 3008 update_cpumasks_hier(cs, &tmpmask, !new_prs); 3009 3010 /* A newly created partition must have effective_xcpus set */ 3011 WARN_ON_ONCE(!old_prs && (new_prs > 0) 3012 && cpumask_empty(cs->effective_xcpus)); 3013 3014 /* Update sched domains and load balance flag */ 3015 update_partition_sd_lb(cs, old_prs); 3016 3017 notify_partition_change(cs, old_prs); 3018 if (force_sd_rebuild) 3019 rebuild_sched_domains_locked(); 3020 free_tmpmasks(&tmpmask); 3021 return 0; 3022 } 3023 3024 static struct cpuset *cpuset_attach_old_cs; 3025 3026 /* 3027 * Check to see if a cpuset can accept a new task 3028 * For v1, cpus_allowed and mems_allowed can't be empty. 3029 * For v2, effective_cpus can't be empty. 3030 * Note that in v1, effective_cpus = cpus_allowed. 3031 */ 3032 static int cpuset_can_attach_check(struct cpuset *cs) 3033 { 3034 if (cpumask_empty(cs->effective_cpus) || 3035 (!is_in_v2_mode() && nodes_empty(cs->mems_allowed))) 3036 return -ENOSPC; 3037 return 0; 3038 } 3039 3040 static void reset_migrate_dl_data(struct cpuset *cs) 3041 { 3042 cs->nr_migrate_dl_tasks = 0; 3043 cs->sum_migrate_dl_bw = 0; 3044 } 3045 3046 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */ 3047 static int cpuset_can_attach(struct cgroup_taskset *tset) 3048 { 3049 struct cgroup_subsys_state *css; 3050 struct cpuset *cs, *oldcs; 3051 struct task_struct *task; 3052 bool cpus_updated, mems_updated; 3053 int ret; 3054 3055 /* used later by cpuset_attach() */ 3056 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css)); 3057 oldcs = cpuset_attach_old_cs; 3058 cs = css_cs(css); 3059 3060 mutex_lock(&cpuset_mutex); 3061 3062 /* Check to see if task is allowed in the cpuset */ 3063 ret = cpuset_can_attach_check(cs); 3064 if (ret) 3065 goto out_unlock; 3066 3067 cpus_updated = !cpumask_equal(cs->effective_cpus, oldcs->effective_cpus); 3068 mems_updated = !nodes_equal(cs->effective_mems, oldcs->effective_mems); 3069 3070 cgroup_taskset_for_each(task, css, tset) { 3071 ret = task_can_attach(task); 3072 if (ret) 3073 goto out_unlock; 3074 3075 /* 3076 * Skip rights over task check in v2 when nothing changes, 3077 * migration permission derives from hierarchy ownership in 3078 * cgroup_procs_write_permission()). 3079 */ 3080 if (!cpuset_v2() || (cpus_updated || mems_updated)) { 3081 ret = security_task_setscheduler(task); 3082 if (ret) 3083 goto out_unlock; 3084 } 3085 3086 if (dl_task(task)) { 3087 cs->nr_migrate_dl_tasks++; 3088 cs->sum_migrate_dl_bw += task->dl.dl_bw; 3089 } 3090 } 3091 3092 if (!cs->nr_migrate_dl_tasks) 3093 goto out_success; 3094 3095 if (!cpumask_intersects(oldcs->effective_cpus, cs->effective_cpus)) { 3096 int cpu = cpumask_any_and(cpu_active_mask, cs->effective_cpus); 3097 3098 if (unlikely(cpu >= nr_cpu_ids)) { 3099 reset_migrate_dl_data(cs); 3100 ret = -EINVAL; 3101 goto out_unlock; 3102 } 3103 3104 ret = dl_bw_alloc(cpu, cs->sum_migrate_dl_bw); 3105 if (ret) { 3106 reset_migrate_dl_data(cs); 3107 goto out_unlock; 3108 } 3109 } 3110 3111 out_success: 3112 /* 3113 * Mark attach is in progress. This makes validate_change() fail 3114 * changes which zero cpus/mems_allowed. 3115 */ 3116 cs->attach_in_progress++; 3117 out_unlock: 3118 mutex_unlock(&cpuset_mutex); 3119 return ret; 3120 } 3121 3122 static void cpuset_cancel_attach(struct cgroup_taskset *tset) 3123 { 3124 struct cgroup_subsys_state *css; 3125 struct cpuset *cs; 3126 3127 cgroup_taskset_first(tset, &css); 3128 cs = css_cs(css); 3129 3130 mutex_lock(&cpuset_mutex); 3131 dec_attach_in_progress_locked(cs); 3132 3133 if (cs->nr_migrate_dl_tasks) { 3134 int cpu = cpumask_any(cs->effective_cpus); 3135 3136 dl_bw_free(cpu, cs->sum_migrate_dl_bw); 3137 reset_migrate_dl_data(cs); 3138 } 3139 3140 mutex_unlock(&cpuset_mutex); 3141 } 3142 3143 /* 3144 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach_task() 3145 * but we can't allocate it dynamically there. Define it global and 3146 * allocate from cpuset_init(). 3147 */ 3148 static cpumask_var_t cpus_attach; 3149 static nodemask_t cpuset_attach_nodemask_to; 3150 3151 static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task) 3152 { 3153 lockdep_assert_held(&cpuset_mutex); 3154 3155 if (cs != &top_cpuset) 3156 guarantee_active_cpus(task, cpus_attach); 3157 else 3158 cpumask_andnot(cpus_attach, task_cpu_possible_mask(task), 3159 subpartitions_cpus); 3160 /* 3161 * can_attach beforehand should guarantee that this doesn't 3162 * fail. TODO: have a better way to handle failure here 3163 */ 3164 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); 3165 3166 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); 3167 cpuset1_update_task_spread_flags(cs, task); 3168 } 3169 3170 static void cpuset_attach(struct cgroup_taskset *tset) 3171 { 3172 struct task_struct *task; 3173 struct task_struct *leader; 3174 struct cgroup_subsys_state *css; 3175 struct cpuset *cs; 3176 struct cpuset *oldcs = cpuset_attach_old_cs; 3177 bool cpus_updated, mems_updated; 3178 3179 cgroup_taskset_first(tset, &css); 3180 cs = css_cs(css); 3181 3182 lockdep_assert_cpus_held(); /* see cgroup_attach_lock() */ 3183 mutex_lock(&cpuset_mutex); 3184 cpus_updated = !cpumask_equal(cs->effective_cpus, 3185 oldcs->effective_cpus); 3186 mems_updated = !nodes_equal(cs->effective_mems, oldcs->effective_mems); 3187 3188 /* 3189 * In the default hierarchy, enabling cpuset in the child cgroups 3190 * will trigger a number of cpuset_attach() calls with no change 3191 * in effective cpus and mems. In that case, we can optimize out 3192 * by skipping the task iteration and update. 3193 */ 3194 if (cpuset_v2() && !cpus_updated && !mems_updated) { 3195 cpuset_attach_nodemask_to = cs->effective_mems; 3196 goto out; 3197 } 3198 3199 guarantee_online_mems(cs, &cpuset_attach_nodemask_to); 3200 3201 cgroup_taskset_for_each(task, css, tset) 3202 cpuset_attach_task(cs, task); 3203 3204 /* 3205 * Change mm for all threadgroup leaders. This is expensive and may 3206 * sleep and should be moved outside migration path proper. Skip it 3207 * if there is no change in effective_mems and CS_MEMORY_MIGRATE is 3208 * not set. 3209 */ 3210 cpuset_attach_nodemask_to = cs->effective_mems; 3211 if (!is_memory_migrate(cs) && !mems_updated) 3212 goto out; 3213 3214 cgroup_taskset_for_each_leader(leader, css, tset) { 3215 struct mm_struct *mm = get_task_mm(leader); 3216 3217 if (mm) { 3218 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to); 3219 3220 /* 3221 * old_mems_allowed is the same with mems_allowed 3222 * here, except if this task is being moved 3223 * automatically due to hotplug. In that case 3224 * @mems_allowed has been updated and is empty, so 3225 * @old_mems_allowed is the right nodesets that we 3226 * migrate mm from. 3227 */ 3228 if (is_memory_migrate(cs)) 3229 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed, 3230 &cpuset_attach_nodemask_to); 3231 else 3232 mmput(mm); 3233 } 3234 } 3235 3236 out: 3237 cs->old_mems_allowed = cpuset_attach_nodemask_to; 3238 3239 if (cs->nr_migrate_dl_tasks) { 3240 cs->nr_deadline_tasks += cs->nr_migrate_dl_tasks; 3241 oldcs->nr_deadline_tasks -= cs->nr_migrate_dl_tasks; 3242 reset_migrate_dl_data(cs); 3243 } 3244 3245 dec_attach_in_progress_locked(cs); 3246 3247 mutex_unlock(&cpuset_mutex); 3248 } 3249 3250 /* 3251 * Common handling for a write to a "cpus" or "mems" file. 3252 */ 3253 ssize_t cpuset_write_resmask(struct kernfs_open_file *of, 3254 char *buf, size_t nbytes, loff_t off) 3255 { 3256 struct cpuset *cs = css_cs(of_css(of)); 3257 struct cpuset *trialcs; 3258 int retval = -ENODEV; 3259 3260 buf = strstrip(buf); 3261 cpuset_full_lock(); 3262 if (!is_cpuset_online(cs)) 3263 goto out_unlock; 3264 3265 trialcs = dup_or_alloc_cpuset(cs); 3266 if (!trialcs) { 3267 retval = -ENOMEM; 3268 goto out_unlock; 3269 } 3270 3271 switch (of_cft(of)->private) { 3272 case FILE_CPULIST: 3273 retval = update_cpumask(cs, trialcs, buf); 3274 break; 3275 case FILE_EXCLUSIVE_CPULIST: 3276 retval = update_exclusive_cpumask(cs, trialcs, buf); 3277 break; 3278 case FILE_MEMLIST: 3279 retval = update_nodemask(cs, trialcs, buf); 3280 break; 3281 default: 3282 retval = -EINVAL; 3283 break; 3284 } 3285 3286 free_cpuset(trialcs); 3287 if (force_sd_rebuild) 3288 rebuild_sched_domains_locked(); 3289 out_unlock: 3290 cpuset_full_unlock(); 3291 flush_workqueue(cpuset_migrate_mm_wq); 3292 return retval ?: nbytes; 3293 } 3294 3295 /* 3296 * These ascii lists should be read in a single call, by using a user 3297 * buffer large enough to hold the entire map. If read in smaller 3298 * chunks, there is no guarantee of atomicity. Since the display format 3299 * used, list of ranges of sequential numbers, is variable length, 3300 * and since these maps can change value dynamically, one could read 3301 * gibberish by doing partial reads while a list was changing. 3302 */ 3303 int cpuset_common_seq_show(struct seq_file *sf, void *v) 3304 { 3305 struct cpuset *cs = css_cs(seq_css(sf)); 3306 cpuset_filetype_t type = seq_cft(sf)->private; 3307 int ret = 0; 3308 3309 spin_lock_irq(&callback_lock); 3310 3311 switch (type) { 3312 case FILE_CPULIST: 3313 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed)); 3314 break; 3315 case FILE_MEMLIST: 3316 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed)); 3317 break; 3318 case FILE_EFFECTIVE_CPULIST: 3319 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus)); 3320 break; 3321 case FILE_EFFECTIVE_MEMLIST: 3322 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems)); 3323 break; 3324 case FILE_EXCLUSIVE_CPULIST: 3325 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->exclusive_cpus)); 3326 break; 3327 case FILE_EFFECTIVE_XCPULIST: 3328 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_xcpus)); 3329 break; 3330 case FILE_SUBPARTS_CPULIST: 3331 seq_printf(sf, "%*pbl\n", cpumask_pr_args(subpartitions_cpus)); 3332 break; 3333 case FILE_ISOLATED_CPULIST: 3334 seq_printf(sf, "%*pbl\n", cpumask_pr_args(isolated_cpus)); 3335 break; 3336 default: 3337 ret = -EINVAL; 3338 } 3339 3340 spin_unlock_irq(&callback_lock); 3341 return ret; 3342 } 3343 3344 static int cpuset_partition_show(struct seq_file *seq, void *v) 3345 { 3346 struct cpuset *cs = css_cs(seq_css(seq)); 3347 const char *err, *type = NULL; 3348 3349 switch (cs->partition_root_state) { 3350 case PRS_ROOT: 3351 seq_puts(seq, "root\n"); 3352 break; 3353 case PRS_ISOLATED: 3354 seq_puts(seq, "isolated\n"); 3355 break; 3356 case PRS_MEMBER: 3357 seq_puts(seq, "member\n"); 3358 break; 3359 case PRS_INVALID_ROOT: 3360 type = "root"; 3361 fallthrough; 3362 case PRS_INVALID_ISOLATED: 3363 if (!type) 3364 type = "isolated"; 3365 err = perr_strings[READ_ONCE(cs->prs_err)]; 3366 if (err) 3367 seq_printf(seq, "%s invalid (%s)\n", type, err); 3368 else 3369 seq_printf(seq, "%s invalid\n", type); 3370 break; 3371 } 3372 return 0; 3373 } 3374 3375 static ssize_t cpuset_partition_write(struct kernfs_open_file *of, char *buf, 3376 size_t nbytes, loff_t off) 3377 { 3378 struct cpuset *cs = css_cs(of_css(of)); 3379 int val; 3380 int retval = -ENODEV; 3381 3382 buf = strstrip(buf); 3383 3384 if (!strcmp(buf, "root")) 3385 val = PRS_ROOT; 3386 else if (!strcmp(buf, "member")) 3387 val = PRS_MEMBER; 3388 else if (!strcmp(buf, "isolated")) 3389 val = PRS_ISOLATED; 3390 else 3391 return -EINVAL; 3392 3393 cpuset_full_lock(); 3394 if (is_cpuset_online(cs)) 3395 retval = update_prstate(cs, val); 3396 cpuset_full_unlock(); 3397 return retval ?: nbytes; 3398 } 3399 3400 /* 3401 * This is currently a minimal set for the default hierarchy. It can be 3402 * expanded later on by migrating more features and control files from v1. 3403 */ 3404 static struct cftype dfl_files[] = { 3405 { 3406 .name = "cpus", 3407 .seq_show = cpuset_common_seq_show, 3408 .write = cpuset_write_resmask, 3409 .max_write_len = (100U + 6 * NR_CPUS), 3410 .private = FILE_CPULIST, 3411 .flags = CFTYPE_NOT_ON_ROOT, 3412 }, 3413 3414 { 3415 .name = "mems", 3416 .seq_show = cpuset_common_seq_show, 3417 .write = cpuset_write_resmask, 3418 .max_write_len = (100U + 6 * MAX_NUMNODES), 3419 .private = FILE_MEMLIST, 3420 .flags = CFTYPE_NOT_ON_ROOT, 3421 }, 3422 3423 { 3424 .name = "cpus.effective", 3425 .seq_show = cpuset_common_seq_show, 3426 .private = FILE_EFFECTIVE_CPULIST, 3427 }, 3428 3429 { 3430 .name = "mems.effective", 3431 .seq_show = cpuset_common_seq_show, 3432 .private = FILE_EFFECTIVE_MEMLIST, 3433 }, 3434 3435 { 3436 .name = "cpus.partition", 3437 .seq_show = cpuset_partition_show, 3438 .write = cpuset_partition_write, 3439 .private = FILE_PARTITION_ROOT, 3440 .flags = CFTYPE_NOT_ON_ROOT, 3441 .file_offset = offsetof(struct cpuset, partition_file), 3442 }, 3443 3444 { 3445 .name = "cpus.exclusive", 3446 .seq_show = cpuset_common_seq_show, 3447 .write = cpuset_write_resmask, 3448 .max_write_len = (100U + 6 * NR_CPUS), 3449 .private = FILE_EXCLUSIVE_CPULIST, 3450 .flags = CFTYPE_NOT_ON_ROOT, 3451 }, 3452 3453 { 3454 .name = "cpus.exclusive.effective", 3455 .seq_show = cpuset_common_seq_show, 3456 .private = FILE_EFFECTIVE_XCPULIST, 3457 .flags = CFTYPE_NOT_ON_ROOT, 3458 }, 3459 3460 { 3461 .name = "cpus.subpartitions", 3462 .seq_show = cpuset_common_seq_show, 3463 .private = FILE_SUBPARTS_CPULIST, 3464 .flags = CFTYPE_ONLY_ON_ROOT | CFTYPE_DEBUG, 3465 }, 3466 3467 { 3468 .name = "cpus.isolated", 3469 .seq_show = cpuset_common_seq_show, 3470 .private = FILE_ISOLATED_CPULIST, 3471 .flags = CFTYPE_ONLY_ON_ROOT, 3472 }, 3473 3474 { } /* terminate */ 3475 }; 3476 3477 3478 /** 3479 * cpuset_css_alloc - Allocate a cpuset css 3480 * @parent_css: Parent css of the control group that the new cpuset will be 3481 * part of 3482 * Return: cpuset css on success, -ENOMEM on failure. 3483 * 3484 * Allocate and initialize a new cpuset css, for non-NULL @parent_css, return 3485 * top cpuset css otherwise. 3486 */ 3487 static struct cgroup_subsys_state * 3488 cpuset_css_alloc(struct cgroup_subsys_state *parent_css) 3489 { 3490 struct cpuset *cs; 3491 3492 if (!parent_css) 3493 return &top_cpuset.css; 3494 3495 cs = dup_or_alloc_cpuset(NULL); 3496 if (!cs) 3497 return ERR_PTR(-ENOMEM); 3498 3499 __set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 3500 fmeter_init(&cs->fmeter); 3501 cs->relax_domain_level = -1; 3502 INIT_LIST_HEAD(&cs->remote_sibling); 3503 3504 /* Set CS_MEMORY_MIGRATE for default hierarchy */ 3505 if (cpuset_v2()) 3506 __set_bit(CS_MEMORY_MIGRATE, &cs->flags); 3507 3508 return &cs->css; 3509 } 3510 3511 static int cpuset_css_online(struct cgroup_subsys_state *css) 3512 { 3513 struct cpuset *cs = css_cs(css); 3514 struct cpuset *parent = parent_cs(cs); 3515 struct cpuset *tmp_cs; 3516 struct cgroup_subsys_state *pos_css; 3517 3518 if (!parent) 3519 return 0; 3520 3521 cpuset_full_lock(); 3522 if (is_spread_page(parent)) 3523 set_bit(CS_SPREAD_PAGE, &cs->flags); 3524 if (is_spread_slab(parent)) 3525 set_bit(CS_SPREAD_SLAB, &cs->flags); 3526 /* 3527 * For v2, clear CS_SCHED_LOAD_BALANCE if parent is isolated 3528 */ 3529 if (cpuset_v2() && !is_sched_load_balance(parent)) 3530 clear_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 3531 3532 cpuset_inc(); 3533 3534 spin_lock_irq(&callback_lock); 3535 if (is_in_v2_mode()) { 3536 cpumask_copy(cs->effective_cpus, parent->effective_cpus); 3537 cs->effective_mems = parent->effective_mems; 3538 } 3539 spin_unlock_irq(&callback_lock); 3540 3541 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags)) 3542 goto out_unlock; 3543 3544 /* 3545 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is 3546 * set. This flag handling is implemented in cgroup core for 3547 * historical reasons - the flag may be specified during mount. 3548 * 3549 * Currently, if any sibling cpusets have exclusive cpus or mem, we 3550 * refuse to clone the configuration - thereby refusing the task to 3551 * be entered, and as a result refusing the sys_unshare() or 3552 * clone() which initiated it. If this becomes a problem for some 3553 * users who wish to allow that scenario, then this could be 3554 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive 3555 * (and likewise for mems) to the new cgroup. 3556 */ 3557 rcu_read_lock(); 3558 cpuset_for_each_child(tmp_cs, pos_css, parent) { 3559 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) { 3560 rcu_read_unlock(); 3561 goto out_unlock; 3562 } 3563 } 3564 rcu_read_unlock(); 3565 3566 spin_lock_irq(&callback_lock); 3567 cs->mems_allowed = parent->mems_allowed; 3568 cs->effective_mems = parent->mems_allowed; 3569 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed); 3570 cpumask_copy(cs->effective_cpus, parent->cpus_allowed); 3571 spin_unlock_irq(&callback_lock); 3572 out_unlock: 3573 cpuset_full_unlock(); 3574 return 0; 3575 } 3576 3577 /* 3578 * If the cpuset being removed has its flag 'sched_load_balance' 3579 * enabled, then simulate turning sched_load_balance off, which 3580 * will call rebuild_sched_domains_locked(). That is not needed 3581 * in the default hierarchy where only changes in partition 3582 * will cause repartitioning. 3583 */ 3584 static void cpuset_css_offline(struct cgroup_subsys_state *css) 3585 { 3586 struct cpuset *cs = css_cs(css); 3587 3588 cpuset_full_lock(); 3589 if (!cpuset_v2() && is_sched_load_balance(cs)) 3590 cpuset_update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); 3591 3592 cpuset_dec(); 3593 cpuset_full_unlock(); 3594 } 3595 3596 /* 3597 * If a dying cpuset has the 'cpus.partition' enabled, turn it off by 3598 * changing it back to member to free its exclusive CPUs back to the pool to 3599 * be used by other online cpusets. 3600 */ 3601 static void cpuset_css_killed(struct cgroup_subsys_state *css) 3602 { 3603 struct cpuset *cs = css_cs(css); 3604 3605 cpuset_full_lock(); 3606 /* Reset valid partition back to member */ 3607 if (is_partition_valid(cs)) 3608 update_prstate(cs, PRS_MEMBER); 3609 cpuset_full_unlock(); 3610 } 3611 3612 static void cpuset_css_free(struct cgroup_subsys_state *css) 3613 { 3614 struct cpuset *cs = css_cs(css); 3615 3616 free_cpuset(cs); 3617 } 3618 3619 static void cpuset_bind(struct cgroup_subsys_state *root_css) 3620 { 3621 mutex_lock(&cpuset_mutex); 3622 spin_lock_irq(&callback_lock); 3623 3624 if (is_in_v2_mode()) { 3625 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask); 3626 cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask); 3627 top_cpuset.mems_allowed = node_possible_map; 3628 } else { 3629 cpumask_copy(top_cpuset.cpus_allowed, 3630 top_cpuset.effective_cpus); 3631 top_cpuset.mems_allowed = top_cpuset.effective_mems; 3632 } 3633 3634 spin_unlock_irq(&callback_lock); 3635 mutex_unlock(&cpuset_mutex); 3636 } 3637 3638 /* 3639 * In case the child is cloned into a cpuset different from its parent, 3640 * additional checks are done to see if the move is allowed. 3641 */ 3642 static int cpuset_can_fork(struct task_struct *task, struct css_set *cset) 3643 { 3644 struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]); 3645 bool same_cs; 3646 int ret; 3647 3648 rcu_read_lock(); 3649 same_cs = (cs == task_cs(current)); 3650 rcu_read_unlock(); 3651 3652 if (same_cs) 3653 return 0; 3654 3655 lockdep_assert_held(&cgroup_mutex); 3656 mutex_lock(&cpuset_mutex); 3657 3658 /* Check to see if task is allowed in the cpuset */ 3659 ret = cpuset_can_attach_check(cs); 3660 if (ret) 3661 goto out_unlock; 3662 3663 ret = task_can_attach(task); 3664 if (ret) 3665 goto out_unlock; 3666 3667 ret = security_task_setscheduler(task); 3668 if (ret) 3669 goto out_unlock; 3670 3671 /* 3672 * Mark attach is in progress. This makes validate_change() fail 3673 * changes which zero cpus/mems_allowed. 3674 */ 3675 cs->attach_in_progress++; 3676 out_unlock: 3677 mutex_unlock(&cpuset_mutex); 3678 return ret; 3679 } 3680 3681 static void cpuset_cancel_fork(struct task_struct *task, struct css_set *cset) 3682 { 3683 struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]); 3684 bool same_cs; 3685 3686 rcu_read_lock(); 3687 same_cs = (cs == task_cs(current)); 3688 rcu_read_unlock(); 3689 3690 if (same_cs) 3691 return; 3692 3693 dec_attach_in_progress(cs); 3694 } 3695 3696 /* 3697 * Make sure the new task conform to the current state of its parent, 3698 * which could have been changed by cpuset just after it inherits the 3699 * state from the parent and before it sits on the cgroup's task list. 3700 */ 3701 static void cpuset_fork(struct task_struct *task) 3702 { 3703 struct cpuset *cs; 3704 bool same_cs; 3705 3706 rcu_read_lock(); 3707 cs = task_cs(task); 3708 same_cs = (cs == task_cs(current)); 3709 rcu_read_unlock(); 3710 3711 if (same_cs) { 3712 if (cs == &top_cpuset) 3713 return; 3714 3715 set_cpus_allowed_ptr(task, current->cpus_ptr); 3716 task->mems_allowed = current->mems_allowed; 3717 return; 3718 } 3719 3720 /* CLONE_INTO_CGROUP */ 3721 mutex_lock(&cpuset_mutex); 3722 guarantee_online_mems(cs, &cpuset_attach_nodemask_to); 3723 cpuset_attach_task(cs, task); 3724 3725 dec_attach_in_progress_locked(cs); 3726 mutex_unlock(&cpuset_mutex); 3727 } 3728 3729 struct cgroup_subsys cpuset_cgrp_subsys = { 3730 .css_alloc = cpuset_css_alloc, 3731 .css_online = cpuset_css_online, 3732 .css_offline = cpuset_css_offline, 3733 .css_killed = cpuset_css_killed, 3734 .css_free = cpuset_css_free, 3735 .can_attach = cpuset_can_attach, 3736 .cancel_attach = cpuset_cancel_attach, 3737 .attach = cpuset_attach, 3738 .post_attach = cpuset_post_attach, 3739 .bind = cpuset_bind, 3740 .can_fork = cpuset_can_fork, 3741 .cancel_fork = cpuset_cancel_fork, 3742 .fork = cpuset_fork, 3743 #ifdef CONFIG_CPUSETS_V1 3744 .legacy_cftypes = cpuset1_files, 3745 #endif 3746 .dfl_cftypes = dfl_files, 3747 .early_init = true, 3748 .threaded = true, 3749 }; 3750 3751 /** 3752 * cpuset_init - initialize cpusets at system boot 3753 * 3754 * Description: Initialize top_cpuset 3755 **/ 3756 3757 int __init cpuset_init(void) 3758 { 3759 BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)); 3760 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL)); 3761 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_xcpus, GFP_KERNEL)); 3762 BUG_ON(!alloc_cpumask_var(&top_cpuset.exclusive_cpus, GFP_KERNEL)); 3763 BUG_ON(!zalloc_cpumask_var(&subpartitions_cpus, GFP_KERNEL)); 3764 BUG_ON(!zalloc_cpumask_var(&isolated_cpus, GFP_KERNEL)); 3765 3766 cpumask_setall(top_cpuset.cpus_allowed); 3767 nodes_setall(top_cpuset.mems_allowed); 3768 cpumask_setall(top_cpuset.effective_cpus); 3769 cpumask_setall(top_cpuset.effective_xcpus); 3770 cpumask_setall(top_cpuset.exclusive_cpus); 3771 nodes_setall(top_cpuset.effective_mems); 3772 3773 fmeter_init(&top_cpuset.fmeter); 3774 INIT_LIST_HEAD(&remote_children); 3775 3776 BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)); 3777 3778 have_boot_isolcpus = housekeeping_enabled(HK_TYPE_DOMAIN); 3779 if (have_boot_isolcpus) { 3780 BUG_ON(!alloc_cpumask_var(&boot_hk_cpus, GFP_KERNEL)); 3781 cpumask_copy(boot_hk_cpus, housekeeping_cpumask(HK_TYPE_DOMAIN)); 3782 cpumask_andnot(isolated_cpus, cpu_possible_mask, boot_hk_cpus); 3783 } 3784 3785 return 0; 3786 } 3787 3788 static void 3789 hotplug_update_tasks(struct cpuset *cs, 3790 struct cpumask *new_cpus, nodemask_t *new_mems, 3791 bool cpus_updated, bool mems_updated) 3792 { 3793 /* A partition root is allowed to have empty effective cpus */ 3794 if (cpumask_empty(new_cpus) && !is_partition_valid(cs)) 3795 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus); 3796 if (nodes_empty(*new_mems)) 3797 *new_mems = parent_cs(cs)->effective_mems; 3798 3799 spin_lock_irq(&callback_lock); 3800 cpumask_copy(cs->effective_cpus, new_cpus); 3801 cs->effective_mems = *new_mems; 3802 spin_unlock_irq(&callback_lock); 3803 3804 if (cpus_updated) 3805 cpuset_update_tasks_cpumask(cs, new_cpus); 3806 if (mems_updated) 3807 cpuset_update_tasks_nodemask(cs); 3808 } 3809 3810 void cpuset_force_rebuild(void) 3811 { 3812 force_sd_rebuild = true; 3813 } 3814 3815 /** 3816 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug 3817 * @cs: cpuset in interest 3818 * @tmp: the tmpmasks structure pointer 3819 * 3820 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone 3821 * offline, update @cs accordingly. If @cs ends up with no CPU or memory, 3822 * all its tasks are moved to the nearest ancestor with both resources. 3823 */ 3824 static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) 3825 { 3826 static cpumask_t new_cpus; 3827 static nodemask_t new_mems; 3828 bool cpus_updated; 3829 bool mems_updated; 3830 bool remote; 3831 int partcmd = -1; 3832 struct cpuset *parent; 3833 retry: 3834 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0); 3835 3836 mutex_lock(&cpuset_mutex); 3837 3838 /* 3839 * We have raced with task attaching. We wait until attaching 3840 * is finished, so we won't attach a task to an empty cpuset. 3841 */ 3842 if (cs->attach_in_progress) { 3843 mutex_unlock(&cpuset_mutex); 3844 goto retry; 3845 } 3846 3847 parent = parent_cs(cs); 3848 compute_effective_cpumask(&new_cpus, cs, parent); 3849 nodes_and(new_mems, cs->mems_allowed, parent->effective_mems); 3850 3851 if (!tmp || !cs->partition_root_state) 3852 goto update_tasks; 3853 3854 /* 3855 * Compute effective_cpus for valid partition root, may invalidate 3856 * child partition roots if necessary. 3857 */ 3858 remote = is_remote_partition(cs); 3859 if (remote || (is_partition_valid(cs) && is_partition_valid(parent))) 3860 compute_partition_effective_cpumask(cs, &new_cpus); 3861 3862 if (remote && cpumask_empty(&new_cpus) && 3863 partition_is_populated(cs, NULL)) { 3864 cs->prs_err = PERR_HOTPLUG; 3865 remote_partition_disable(cs, tmp); 3866 compute_effective_cpumask(&new_cpus, cs, parent); 3867 remote = false; 3868 } 3869 3870 /* 3871 * Force the partition to become invalid if either one of 3872 * the following conditions hold: 3873 * 1) empty effective cpus but not valid empty partition. 3874 * 2) parent is invalid or doesn't grant any cpus to child 3875 * partitions. 3876 */ 3877 if (is_local_partition(cs) && (!is_partition_valid(parent) || 3878 tasks_nocpu_error(parent, cs, &new_cpus))) 3879 partcmd = partcmd_invalidate; 3880 /* 3881 * On the other hand, an invalid partition root may be transitioned 3882 * back to a regular one with a non-empty effective xcpus. 3883 */ 3884 else if (is_partition_valid(parent) && is_partition_invalid(cs) && 3885 !cpumask_empty(cs->effective_xcpus)) 3886 partcmd = partcmd_update; 3887 3888 if (partcmd >= 0) { 3889 update_parent_effective_cpumask(cs, partcmd, NULL, tmp); 3890 if ((partcmd == partcmd_invalidate) || is_partition_valid(cs)) { 3891 compute_partition_effective_cpumask(cs, &new_cpus); 3892 cpuset_force_rebuild(); 3893 } 3894 } 3895 3896 update_tasks: 3897 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus); 3898 mems_updated = !nodes_equal(new_mems, cs->effective_mems); 3899 if (!cpus_updated && !mems_updated) 3900 goto unlock; /* Hotplug doesn't affect this cpuset */ 3901 3902 if (mems_updated) 3903 check_insane_mems_config(&new_mems); 3904 3905 if (is_in_v2_mode()) 3906 hotplug_update_tasks(cs, &new_cpus, &new_mems, 3907 cpus_updated, mems_updated); 3908 else 3909 cpuset1_hotplug_update_tasks(cs, &new_cpus, &new_mems, 3910 cpus_updated, mems_updated); 3911 3912 unlock: 3913 mutex_unlock(&cpuset_mutex); 3914 } 3915 3916 /** 3917 * cpuset_handle_hotplug - handle CPU/memory hot{,un}plug for a cpuset 3918 * 3919 * This function is called after either CPU or memory configuration has 3920 * changed and updates cpuset accordingly. The top_cpuset is always 3921 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in 3922 * order to make cpusets transparent (of no affect) on systems that are 3923 * actively using CPU hotplug but making no active use of cpusets. 3924 * 3925 * Non-root cpusets are only affected by offlining. If any CPUs or memory 3926 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on 3927 * all descendants. 3928 * 3929 * Note that CPU offlining during suspend is ignored. We don't modify 3930 * cpusets across suspend/resume cycles at all. 3931 * 3932 * CPU / memory hotplug is handled synchronously. 3933 */ 3934 static void cpuset_handle_hotplug(void) 3935 { 3936 static cpumask_t new_cpus; 3937 static nodemask_t new_mems; 3938 bool cpus_updated, mems_updated; 3939 bool on_dfl = is_in_v2_mode(); 3940 struct tmpmasks tmp, *ptmp = NULL; 3941 3942 if (on_dfl && !alloc_tmpmasks(&tmp)) 3943 ptmp = &tmp; 3944 3945 lockdep_assert_cpus_held(); 3946 mutex_lock(&cpuset_mutex); 3947 3948 /* fetch the available cpus/mems and find out which changed how */ 3949 cpumask_copy(&new_cpus, cpu_active_mask); 3950 new_mems = node_states[N_MEMORY]; 3951 3952 /* 3953 * If subpartitions_cpus is populated, it is likely that the check 3954 * below will produce a false positive on cpus_updated when the cpu 3955 * list isn't changed. It is extra work, but it is better to be safe. 3956 */ 3957 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus) || 3958 !cpumask_empty(subpartitions_cpus); 3959 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems); 3960 3961 /* For v1, synchronize cpus_allowed to cpu_active_mask */ 3962 if (cpus_updated) { 3963 cpuset_force_rebuild(); 3964 spin_lock_irq(&callback_lock); 3965 if (!on_dfl) 3966 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); 3967 /* 3968 * Make sure that CPUs allocated to child partitions 3969 * do not show up in effective_cpus. If no CPU is left, 3970 * we clear the subpartitions_cpus & let the child partitions 3971 * fight for the CPUs again. 3972 */ 3973 if (!cpumask_empty(subpartitions_cpus)) { 3974 if (cpumask_subset(&new_cpus, subpartitions_cpus)) { 3975 top_cpuset.nr_subparts = 0; 3976 cpumask_clear(subpartitions_cpus); 3977 } else { 3978 cpumask_andnot(&new_cpus, &new_cpus, 3979 subpartitions_cpus); 3980 } 3981 } 3982 cpumask_copy(top_cpuset.effective_cpus, &new_cpus); 3983 spin_unlock_irq(&callback_lock); 3984 /* we don't mess with cpumasks of tasks in top_cpuset */ 3985 } 3986 3987 /* synchronize mems_allowed to N_MEMORY */ 3988 if (mems_updated) { 3989 spin_lock_irq(&callback_lock); 3990 if (!on_dfl) 3991 top_cpuset.mems_allowed = new_mems; 3992 top_cpuset.effective_mems = new_mems; 3993 spin_unlock_irq(&callback_lock); 3994 cpuset_update_tasks_nodemask(&top_cpuset); 3995 } 3996 3997 mutex_unlock(&cpuset_mutex); 3998 3999 /* if cpus or mems changed, we need to propagate to descendants */ 4000 if (cpus_updated || mems_updated) { 4001 struct cpuset *cs; 4002 struct cgroup_subsys_state *pos_css; 4003 4004 rcu_read_lock(); 4005 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { 4006 if (cs == &top_cpuset || !css_tryget_online(&cs->css)) 4007 continue; 4008 rcu_read_unlock(); 4009 4010 cpuset_hotplug_update_tasks(cs, ptmp); 4011 4012 rcu_read_lock(); 4013 css_put(&cs->css); 4014 } 4015 rcu_read_unlock(); 4016 } 4017 4018 /* rebuild sched domains if necessary */ 4019 if (force_sd_rebuild) 4020 rebuild_sched_domains_cpuslocked(); 4021 4022 free_tmpmasks(ptmp); 4023 } 4024 4025 void cpuset_update_active_cpus(void) 4026 { 4027 /* 4028 * We're inside cpu hotplug critical region which usually nests 4029 * inside cgroup synchronization. Bounce actual hotplug processing 4030 * to a work item to avoid reverse locking order. 4031 */ 4032 cpuset_handle_hotplug(); 4033 } 4034 4035 /* 4036 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. 4037 * Call this routine anytime after node_states[N_MEMORY] changes. 4038 * See cpuset_update_active_cpus() for CPU hotplug handling. 4039 */ 4040 static int cpuset_track_online_nodes(struct notifier_block *self, 4041 unsigned long action, void *arg) 4042 { 4043 cpuset_handle_hotplug(); 4044 return NOTIFY_OK; 4045 } 4046 4047 /** 4048 * cpuset_init_smp - initialize cpus_allowed 4049 * 4050 * Description: Finish top cpuset after cpu, node maps are initialized 4051 */ 4052 void __init cpuset_init_smp(void) 4053 { 4054 /* 4055 * cpus_allowd/mems_allowed set to v2 values in the initial 4056 * cpuset_bind() call will be reset to v1 values in another 4057 * cpuset_bind() call when v1 cpuset is mounted. 4058 */ 4059 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed; 4060 4061 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask); 4062 top_cpuset.effective_mems = node_states[N_MEMORY]; 4063 4064 hotplug_node_notifier(cpuset_track_online_nodes, CPUSET_CALLBACK_PRI); 4065 4066 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0); 4067 BUG_ON(!cpuset_migrate_mm_wq); 4068 } 4069 4070 /** 4071 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. 4072 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. 4073 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. 4074 * 4075 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset 4076 * attached to the specified @tsk. Guaranteed to return some non-empty 4077 * subset of cpu_active_mask, even if this means going outside the 4078 * tasks cpuset, except when the task is in the top cpuset. 4079 **/ 4080 4081 void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) 4082 { 4083 unsigned long flags; 4084 struct cpuset *cs; 4085 4086 spin_lock_irqsave(&callback_lock, flags); 4087 rcu_read_lock(); 4088 4089 cs = task_cs(tsk); 4090 if (cs != &top_cpuset) 4091 guarantee_active_cpus(tsk, pmask); 4092 /* 4093 * Tasks in the top cpuset won't get update to their cpumasks 4094 * when a hotplug online/offline event happens. So we include all 4095 * offline cpus in the allowed cpu list. 4096 */ 4097 if ((cs == &top_cpuset) || cpumask_empty(pmask)) { 4098 const struct cpumask *possible_mask = task_cpu_possible_mask(tsk); 4099 4100 /* 4101 * We first exclude cpus allocated to partitions. If there is no 4102 * allowable online cpu left, we fall back to all possible cpus. 4103 */ 4104 cpumask_andnot(pmask, possible_mask, subpartitions_cpus); 4105 if (!cpumask_intersects(pmask, cpu_active_mask)) 4106 cpumask_copy(pmask, possible_mask); 4107 } 4108 4109 rcu_read_unlock(); 4110 spin_unlock_irqrestore(&callback_lock, flags); 4111 } 4112 4113 /** 4114 * cpuset_cpus_allowed_fallback - final fallback before complete catastrophe. 4115 * @tsk: pointer to task_struct with which the scheduler is struggling 4116 * 4117 * Description: In the case that the scheduler cannot find an allowed cpu in 4118 * tsk->cpus_allowed, we fall back to task_cs(tsk)->cpus_allowed. In legacy 4119 * mode however, this value is the same as task_cs(tsk)->effective_cpus, 4120 * which will not contain a sane cpumask during cases such as cpu hotplugging. 4121 * This is the absolute last resort for the scheduler and it is only used if 4122 * _every_ other avenue has been traveled. 4123 * 4124 * Returns true if the affinity of @tsk was changed, false otherwise. 4125 **/ 4126 4127 bool cpuset_cpus_allowed_fallback(struct task_struct *tsk) 4128 { 4129 const struct cpumask *possible_mask = task_cpu_possible_mask(tsk); 4130 const struct cpumask *cs_mask; 4131 bool changed = false; 4132 4133 rcu_read_lock(); 4134 cs_mask = task_cs(tsk)->cpus_allowed; 4135 if (is_in_v2_mode() && cpumask_subset(cs_mask, possible_mask)) { 4136 do_set_cpus_allowed(tsk, cs_mask); 4137 changed = true; 4138 } 4139 rcu_read_unlock(); 4140 4141 /* 4142 * We own tsk->cpus_allowed, nobody can change it under us. 4143 * 4144 * But we used cs && cs->cpus_allowed lockless and thus can 4145 * race with cgroup_attach_task() or update_cpumask() and get 4146 * the wrong tsk->cpus_allowed. However, both cases imply the 4147 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() 4148 * which takes task_rq_lock(). 4149 * 4150 * If we are called after it dropped the lock we must see all 4151 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary 4152 * set any mask even if it is not right from task_cs() pov, 4153 * the pending set_cpus_allowed_ptr() will fix things. 4154 * 4155 * select_fallback_rq() will fix things ups and set cpu_possible_mask 4156 * if required. 4157 */ 4158 return changed; 4159 } 4160 4161 void __init cpuset_init_current_mems_allowed(void) 4162 { 4163 nodes_setall(current->mems_allowed); 4164 } 4165 4166 /** 4167 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. 4168 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. 4169 * 4170 * Description: Returns the nodemask_t mems_allowed of the cpuset 4171 * attached to the specified @tsk. Guaranteed to return some non-empty 4172 * subset of node_states[N_MEMORY], even if this means going outside the 4173 * tasks cpuset. 4174 **/ 4175 4176 nodemask_t cpuset_mems_allowed(struct task_struct *tsk) 4177 { 4178 nodemask_t mask; 4179 unsigned long flags; 4180 4181 spin_lock_irqsave(&callback_lock, flags); 4182 rcu_read_lock(); 4183 guarantee_online_mems(task_cs(tsk), &mask); 4184 rcu_read_unlock(); 4185 spin_unlock_irqrestore(&callback_lock, flags); 4186 4187 return mask; 4188 } 4189 4190 /** 4191 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. current mems_allowed 4192 * @nodemask: the nodemask to be checked 4193 * 4194 * Are any of the nodes in the nodemask allowed in current->mems_allowed? 4195 */ 4196 int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask) 4197 { 4198 return nodes_intersects(*nodemask, current->mems_allowed); 4199 } 4200 4201 /* 4202 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or 4203 * mem_hardwall ancestor to the specified cpuset. Call holding 4204 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall 4205 * (an unusual configuration), then returns the root cpuset. 4206 */ 4207 static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs) 4208 { 4209 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs)) 4210 cs = parent_cs(cs); 4211 return cs; 4212 } 4213 4214 /* 4215 * cpuset_current_node_allowed - Can current task allocate on a memory node? 4216 * @node: is this an allowed node? 4217 * @gfp_mask: memory allocation flags 4218 * 4219 * If we're in interrupt, yes, we can always allocate. If @node is set in 4220 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this 4221 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset, 4222 * yes. If current has access to memory reserves as an oom victim, yes. 4223 * Otherwise, no. 4224 * 4225 * GFP_USER allocations are marked with the __GFP_HARDWALL bit, 4226 * and do not allow allocations outside the current tasks cpuset 4227 * unless the task has been OOM killed. 4228 * GFP_KERNEL allocations are not so marked, so can escape to the 4229 * nearest enclosing hardwalled ancestor cpuset. 4230 * 4231 * Scanning up parent cpusets requires callback_lock. The 4232 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit 4233 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the 4234 * current tasks mems_allowed came up empty on the first pass over 4235 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the 4236 * cpuset are short of memory, might require taking the callback_lock. 4237 * 4238 * The first call here from mm/page_alloc:get_page_from_freelist() 4239 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, 4240 * so no allocation on a node outside the cpuset is allowed (unless 4241 * in interrupt, of course). 4242 * 4243 * The second pass through get_page_from_freelist() doesn't even call 4244 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() 4245 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set 4246 * in alloc_flags. That logic and the checks below have the combined 4247 * affect that: 4248 * in_interrupt - any node ok (current task context irrelevant) 4249 * GFP_ATOMIC - any node ok 4250 * tsk_is_oom_victim - any node ok 4251 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok 4252 * GFP_USER - only nodes in current tasks mems allowed ok. 4253 */ 4254 bool cpuset_current_node_allowed(int node, gfp_t gfp_mask) 4255 { 4256 struct cpuset *cs; /* current cpuset ancestors */ 4257 bool allowed; /* is allocation in zone z allowed? */ 4258 unsigned long flags; 4259 4260 if (in_interrupt()) 4261 return true; 4262 if (node_isset(node, current->mems_allowed)) 4263 return true; 4264 /* 4265 * Allow tasks that have access to memory reserves because they have 4266 * been OOM killed to get memory anywhere. 4267 */ 4268 if (unlikely(tsk_is_oom_victim(current))) 4269 return true; 4270 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ 4271 return false; 4272 4273 if (current->flags & PF_EXITING) /* Let dying task have memory */ 4274 return true; 4275 4276 /* Not hardwall and node outside mems_allowed: scan up cpusets */ 4277 spin_lock_irqsave(&callback_lock, flags); 4278 4279 rcu_read_lock(); 4280 cs = nearest_hardwall_ancestor(task_cs(current)); 4281 allowed = node_isset(node, cs->mems_allowed); 4282 rcu_read_unlock(); 4283 4284 spin_unlock_irqrestore(&callback_lock, flags); 4285 return allowed; 4286 } 4287 4288 bool cpuset_node_allowed(struct cgroup *cgroup, int nid) 4289 { 4290 struct cgroup_subsys_state *css; 4291 struct cpuset *cs; 4292 bool allowed; 4293 4294 /* 4295 * In v1, mem_cgroup and cpuset are unlikely in the same hierarchy 4296 * and mems_allowed is likely to be empty even if we could get to it, 4297 * so return true to avoid taking a global lock on the empty check. 4298 */ 4299 if (!cpuset_v2()) 4300 return true; 4301 4302 css = cgroup_get_e_css(cgroup, &cpuset_cgrp_subsys); 4303 if (!css) 4304 return true; 4305 4306 /* 4307 * Normally, accessing effective_mems would require the cpuset_mutex 4308 * or callback_lock - but node_isset is atomic and the reference 4309 * taken via cgroup_get_e_css is sufficient to protect css. 4310 * 4311 * Since this interface is intended for use by migration paths, we 4312 * relax locking here to avoid taking global locks - while accepting 4313 * there may be rare scenarios where the result may be innaccurate. 4314 * 4315 * Reclaim and migration are subject to these same race conditions, and 4316 * cannot make strong isolation guarantees, so this is acceptable. 4317 */ 4318 cs = container_of(css, struct cpuset, css); 4319 allowed = node_isset(nid, cs->effective_mems); 4320 css_put(css); 4321 return allowed; 4322 } 4323 4324 /** 4325 * cpuset_spread_node() - On which node to begin search for a page 4326 * @rotor: round robin rotor 4327 * 4328 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for 4329 * tasks in a cpuset with is_spread_page or is_spread_slab set), 4330 * and if the memory allocation used cpuset_mem_spread_node() 4331 * to determine on which node to start looking, as it will for 4332 * certain page cache or slab cache pages such as used for file 4333 * system buffers and inode caches, then instead of starting on the 4334 * local node to look for a free page, rather spread the starting 4335 * node around the tasks mems_allowed nodes. 4336 * 4337 * We don't have to worry about the returned node being offline 4338 * because "it can't happen", and even if it did, it would be ok. 4339 * 4340 * The routines calling guarantee_online_mems() are careful to 4341 * only set nodes in task->mems_allowed that are online. So it 4342 * should not be possible for the following code to return an 4343 * offline node. But if it did, that would be ok, as this routine 4344 * is not returning the node where the allocation must be, only 4345 * the node where the search should start. The zonelist passed to 4346 * __alloc_pages() will include all nodes. If the slab allocator 4347 * is passed an offline node, it will fall back to the local node. 4348 * See kmem_cache_alloc_node(). 4349 */ 4350 static int cpuset_spread_node(int *rotor) 4351 { 4352 return *rotor = next_node_in(*rotor, current->mems_allowed); 4353 } 4354 4355 /** 4356 * cpuset_mem_spread_node() - On which node to begin search for a file page 4357 */ 4358 int cpuset_mem_spread_node(void) 4359 { 4360 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) 4361 current->cpuset_mem_spread_rotor = 4362 node_random(¤t->mems_allowed); 4363 4364 return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); 4365 } 4366 4367 /** 4368 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? 4369 * @tsk1: pointer to task_struct of some task. 4370 * @tsk2: pointer to task_struct of some other task. 4371 * 4372 * Description: Return true if @tsk1's mems_allowed intersects the 4373 * mems_allowed of @tsk2. Used by the OOM killer to determine if 4374 * one of the task's memory usage might impact the memory available 4375 * to the other. 4376 **/ 4377 4378 int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, 4379 const struct task_struct *tsk2) 4380 { 4381 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); 4382 } 4383 4384 /** 4385 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed 4386 * 4387 * Description: Prints current's name, cpuset name, and cached copy of its 4388 * mems_allowed to the kernel log. 4389 */ 4390 void cpuset_print_current_mems_allowed(void) 4391 { 4392 struct cgroup *cgrp; 4393 4394 rcu_read_lock(); 4395 4396 cgrp = task_cs(current)->css.cgroup; 4397 pr_cont(",cpuset="); 4398 pr_cont_cgroup_name(cgrp); 4399 pr_cont(",mems_allowed=%*pbl", 4400 nodemask_pr_args(¤t->mems_allowed)); 4401 4402 rcu_read_unlock(); 4403 } 4404 4405 /* Display task mems_allowed in /proc/<pid>/status file. */ 4406 void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) 4407 { 4408 seq_printf(m, "Mems_allowed:\t%*pb\n", 4409 nodemask_pr_args(&task->mems_allowed)); 4410 seq_printf(m, "Mems_allowed_list:\t%*pbl\n", 4411 nodemask_pr_args(&task->mems_allowed)); 4412 } 4413