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