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