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