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 /* Return true if isolated_cpus changes. */ 1263 static bool isolated_cpu_update(int new_prs, int cpu) 1264 { 1265 lockdep_assert_held(&callback_lock); 1266 lockdep_assert_held(&cpuset_mutex); 1267 1268 if (new_prs == PRS_ISOLATED) { 1269 if (cpumask_test_cpu(cpu, isolated_cpus)) 1270 return false; 1271 cpumask_set_cpu(cpu, isolated_cpus); 1272 return true; 1273 } 1274 1275 /* CPUs isolated at boot must remain isolated. */ 1276 if (!cpumask_test_cpu(cpu, 1277 housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)) || 1278 !cpumask_test_cpu(cpu, isolated_cpus)) 1279 return false; 1280 cpumask_clear_cpu(cpu, isolated_cpus); 1281 return true; 1282 } 1283 1284 /* 1285 * isolated_cpus_update - Update the isolated_cpus mask 1286 * @old_prs: old partition_root_state 1287 * @new_prs: new partition_root_state 1288 * @xcpus: exclusive CPUs with state change 1289 */ 1290 static void isolated_cpus_update(int old_prs, int new_prs, struct cpumask *xcpus) 1291 { 1292 bool updated = false; 1293 int cpu; 1294 1295 WARN_ON_ONCE(old_prs == new_prs); 1296 lockdep_assert_held(&callback_lock); 1297 lockdep_assert_held(&cpuset_mutex); 1298 for_each_cpu(cpu, xcpus) 1299 updated |= isolated_cpu_update(new_prs, cpu); 1300 if (updated) 1301 update_housekeeping = true; 1302 } 1303 1304 /* 1305 * partition_xcpus_add - Add new exclusive CPUs to partition 1306 * @new_prs: new partition_root_state 1307 * @parent: parent cpuset 1308 * @xcpus: exclusive CPUs to be added 1309 * 1310 * Remote partition if parent == NULL 1311 */ 1312 static void partition_xcpus_add(int new_prs, struct cpuset *parent, 1313 struct cpumask *xcpus) 1314 { 1315 WARN_ON_ONCE(new_prs < 0); 1316 lockdep_assert_held(&callback_lock); 1317 if (!parent) 1318 parent = &top_cpuset; 1319 1320 1321 if (parent == &top_cpuset) 1322 cpumask_or(subpartitions_cpus, subpartitions_cpus, xcpus); 1323 1324 if (new_prs != parent->partition_root_state) 1325 isolated_cpus_update(parent->partition_root_state, new_prs, 1326 xcpus); 1327 1328 cpumask_andnot(parent->effective_cpus, parent->effective_cpus, xcpus); 1329 } 1330 1331 /* 1332 * partition_xcpus_del - Remove exclusive CPUs from partition 1333 * @old_prs: old partition_root_state 1334 * @parent: parent cpuset 1335 * @xcpus: exclusive CPUs to be removed 1336 * 1337 * Remote partition if parent == NULL 1338 */ 1339 static void partition_xcpus_del(int old_prs, struct cpuset *parent, 1340 struct cpumask *xcpus) 1341 { 1342 WARN_ON_ONCE(old_prs < 0); 1343 lockdep_assert_held(&callback_lock); 1344 if (!parent) 1345 parent = &top_cpuset; 1346 1347 if (parent == &top_cpuset) 1348 cpumask_andnot(subpartitions_cpus, subpartitions_cpus, xcpus); 1349 1350 if (old_prs != parent->partition_root_state) 1351 isolated_cpus_update(old_prs, parent->partition_root_state, 1352 xcpus); 1353 1354 cpumask_or(parent->effective_cpus, parent->effective_cpus, xcpus); 1355 cpumask_and(parent->effective_cpus, parent->effective_cpus, cpu_active_mask); 1356 } 1357 1358 /* 1359 * isolated_cpus_can_update - check for isolated & nohz_full conflicts 1360 * @add_cpus: cpu mask for cpus that are going to be isolated 1361 * @del_cpus: cpu mask for cpus that are no longer isolated, can be NULL 1362 * Return: false if there is conflict, true otherwise 1363 * 1364 * If nohz_full is enabled and we have isolated CPUs, their combination must 1365 * still leave housekeeping CPUs. 1366 * 1367 * TBD: Should consider merging this function into 1368 * prstate_housekeeping_conflict(). 1369 */ 1370 static bool isolated_cpus_can_update(struct cpumask *add_cpus, 1371 struct cpumask *del_cpus) 1372 { 1373 cpumask_var_t full_hk_cpus; 1374 int res = true; 1375 1376 if (!housekeeping_enabled(HK_TYPE_KERNEL_NOISE)) 1377 return true; 1378 1379 if (del_cpus && cpumask_weight_and(del_cpus, 1380 housekeeping_cpumask(HK_TYPE_KERNEL_NOISE))) 1381 return true; 1382 1383 if (!alloc_cpumask_var(&full_hk_cpus, GFP_KERNEL)) 1384 return false; 1385 1386 cpumask_and(full_hk_cpus, housekeeping_cpumask(HK_TYPE_KERNEL_NOISE), 1387 housekeeping_cpumask(HK_TYPE_DOMAIN)); 1388 cpumask_andnot(full_hk_cpus, full_hk_cpus, isolated_cpus); 1389 cpumask_and(full_hk_cpus, full_hk_cpus, cpu_active_mask); 1390 if (!cpumask_weight_andnot(full_hk_cpus, add_cpus)) 1391 res = false; 1392 1393 free_cpumask_var(full_hk_cpus); 1394 return res; 1395 } 1396 1397 /* 1398 * prstate_housekeeping_conflict - check for partition & housekeeping conflicts 1399 * @prstate: partition root state to be checked 1400 * @new_cpus: cpu mask 1401 * Return: true if there is conflict, false otherwise 1402 * 1403 * CPUs outside of HK_TYPE_DOMAIN_BOOT, if defined, can only be used in an 1404 * isolated partition. 1405 */ 1406 static bool prstate_housekeeping_conflict(int prstate, struct cpumask *new_cpus) 1407 { 1408 if (!housekeeping_enabled(HK_TYPE_DOMAIN_BOOT)) 1409 return false; 1410 1411 if ((prstate != PRS_ISOLATED) && 1412 !cpumask_subset(new_cpus, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT))) 1413 return true; 1414 1415 return false; 1416 } 1417 1418 /* 1419 * cpuset_update_sd_hk_unlock - Rebuild sched domains, update HK & unlock 1420 * 1421 * Update housekeeping cpumasks and rebuild sched domains if necessary and 1422 * then do a cpuset_full_unlock(). 1423 * This should be called at the end of cpuset operation. 1424 */ 1425 static void cpuset_update_sd_hk_unlock(void) 1426 __releases(&cpuset_mutex) 1427 __releases(&cpuset_top_mutex) 1428 { 1429 /* force_sd_rebuild will be cleared in rebuild_sched_domains_locked() */ 1430 if (force_sd_rebuild) 1431 rebuild_sched_domains_locked(); 1432 1433 if (update_housekeeping) { 1434 update_housekeeping = false; 1435 cpumask_copy(isolated_hk_cpus, isolated_cpus); 1436 1437 /* 1438 * housekeeping_update() is now called without holding 1439 * cpus_read_lock and cpuset_mutex. Only cpuset_top_mutex 1440 * is still being held for mutual exclusion. 1441 */ 1442 mutex_unlock(&cpuset_mutex); 1443 cpus_read_unlock(); 1444 WARN_ON_ONCE(housekeeping_update(isolated_hk_cpus)); 1445 mutex_unlock(&cpuset_top_mutex); 1446 } else { 1447 cpuset_full_unlock(); 1448 } 1449 } 1450 1451 /* 1452 * Work function to invoke cpuset_update_sd_hk_unlock() 1453 */ 1454 static void hk_sd_workfn(struct work_struct *work) 1455 { 1456 cpuset_full_lock(); 1457 cpuset_update_sd_hk_unlock(); 1458 } 1459 1460 /** 1461 * rm_siblings_excl_cpus - Remove exclusive CPUs that are used by sibling cpusets 1462 * @parent: Parent cpuset containing all siblings 1463 * @cs: Current cpuset (will be skipped) 1464 * @excpus: exclusive effective CPU mask to modify 1465 * 1466 * This function ensures the given @excpus mask doesn't include any CPUs that 1467 * are exclusively allocated to sibling cpusets. It walks through all siblings 1468 * of @cs under @parent and removes their exclusive CPUs from @excpus. 1469 */ 1470 static int rm_siblings_excl_cpus(struct cpuset *parent, struct cpuset *cs, 1471 struct cpumask *excpus) 1472 { 1473 struct cgroup_subsys_state *css; 1474 struct cpuset *sibling; 1475 int retval = 0; 1476 1477 if (cpumask_empty(excpus)) 1478 return 0; 1479 1480 /* 1481 * Remove exclusive CPUs from siblings 1482 */ 1483 rcu_read_lock(); 1484 cpuset_for_each_child(sibling, css, parent) { 1485 struct cpumask *sibling_xcpus; 1486 1487 if (sibling == cs) 1488 continue; 1489 1490 /* 1491 * If exclusive_cpus is defined, effective_xcpus will always 1492 * be a subset. Otherwise, effective_xcpus will only be set 1493 * in a valid partition root. 1494 */ 1495 sibling_xcpus = cpumask_empty(sibling->exclusive_cpus) 1496 ? sibling->effective_xcpus 1497 : sibling->exclusive_cpus; 1498 1499 if (cpumask_intersects(excpus, sibling_xcpus)) { 1500 cpumask_andnot(excpus, excpus, sibling_xcpus); 1501 retval++; 1502 } 1503 } 1504 rcu_read_unlock(); 1505 1506 return retval; 1507 } 1508 1509 /* 1510 * compute_excpus - compute effective exclusive CPUs 1511 * @cs: cpuset 1512 * @xcpus: effective exclusive CPUs value to be set 1513 * Return: 0 if there is no sibling conflict, > 0 otherwise 1514 * 1515 * If exclusive_cpus isn't explicitly set , we have to scan the sibling cpusets 1516 * and exclude their exclusive_cpus or effective_xcpus as well. 1517 */ 1518 static int compute_excpus(struct cpuset *cs, struct cpumask *excpus) 1519 { 1520 struct cpuset *parent = parent_cs(cs); 1521 1522 cpumask_and(excpus, user_xcpus(cs), parent->effective_xcpus); 1523 1524 if (!cpumask_empty(cs->exclusive_cpus)) 1525 return 0; 1526 1527 return rm_siblings_excl_cpus(parent, cs, excpus); 1528 } 1529 1530 /* 1531 * compute_trialcs_excpus - Compute effective exclusive CPUs for a trial cpuset 1532 * @trialcs: The trial cpuset containing the proposed new configuration 1533 * @cs: The original cpuset that the trial configuration is based on 1534 * Return: 0 if successful with no sibling conflict, >0 if a conflict is found 1535 * 1536 * Computes the effective_xcpus for a trial configuration. @cs is provided to represent 1537 * the real cs. 1538 */ 1539 static int compute_trialcs_excpus(struct cpuset *trialcs, struct cpuset *cs) 1540 { 1541 struct cpuset *parent = parent_cs(trialcs); 1542 struct cpumask *excpus = trialcs->effective_xcpus; 1543 1544 /* trialcs is member, cpuset.cpus has no impact to excpus */ 1545 if (cs_is_member(cs)) 1546 cpumask_and(excpus, trialcs->exclusive_cpus, 1547 parent->effective_xcpus); 1548 else 1549 cpumask_and(excpus, user_xcpus(trialcs), parent->effective_xcpus); 1550 1551 return rm_siblings_excl_cpus(parent, cs, excpus); 1552 } 1553 1554 static inline bool is_remote_partition(struct cpuset *cs) 1555 { 1556 return cs->remote_partition; 1557 } 1558 1559 static inline bool is_local_partition(struct cpuset *cs) 1560 { 1561 return is_partition_valid(cs) && !is_remote_partition(cs); 1562 } 1563 1564 /* 1565 * remote_partition_enable - Enable current cpuset as a remote partition root 1566 * @cs: the cpuset to update 1567 * @new_prs: new partition_root_state 1568 * @tmp: temporary masks 1569 * Return: 0 if successful, errcode if error 1570 * 1571 * Enable the current cpuset to become a remote partition root taking CPUs 1572 * directly from the top cpuset. cpuset_mutex must be held by the caller. 1573 */ 1574 static int remote_partition_enable(struct cpuset *cs, int new_prs, 1575 struct tmpmasks *tmp) 1576 { 1577 /* 1578 * The user must have sysadmin privilege. 1579 */ 1580 if (!capable(CAP_SYS_ADMIN)) 1581 return PERR_ACCESS; 1582 1583 /* 1584 * The requested exclusive_cpus must not be allocated to other 1585 * partitions and it can't use up all the root's effective_cpus. 1586 * 1587 * The effective_xcpus mask can contain offline CPUs, but there must 1588 * be at least one or more online CPUs present before it can be enabled. 1589 * 1590 * Note that creating a remote partition with any local partition root 1591 * above it or remote partition root underneath it is not allowed. 1592 */ 1593 compute_excpus(cs, tmp->new_cpus); 1594 if (!cpumask_intersects(tmp->new_cpus, cpu_active_mask) || 1595 cpumask_subset(top_cpuset.effective_cpus, tmp->new_cpus)) 1596 return PERR_INVCPUS; 1597 if (cpumask_intersects(tmp->new_cpus, subpartitions_cpus)) 1598 return PERR_NOCPUS; 1599 if (((new_prs == PRS_ISOLATED) && 1600 !isolated_cpus_can_update(tmp->new_cpus, NULL)) || 1601 prstate_housekeeping_conflict(new_prs, tmp->new_cpus)) 1602 return PERR_HKEEPING; 1603 1604 spin_lock_irq(&callback_lock); 1605 partition_xcpus_add(new_prs, NULL, tmp->new_cpus); 1606 cs->remote_partition = true; 1607 cpumask_copy(cs->effective_xcpus, tmp->new_cpus); 1608 spin_unlock_irq(&callback_lock); 1609 cpuset_force_rebuild(); 1610 WRITE_ONCE(cs->prs_err, 0); 1611 1612 /* 1613 * Propagate changes in top_cpuset's effective_cpus down the hierarchy. 1614 */ 1615 cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus); 1616 update_sibling_cpumasks(&top_cpuset, NULL, tmp); 1617 return 0; 1618 } 1619 1620 /* 1621 * remote_partition_disable - Remove current cpuset from remote partition list 1622 * @cs: the cpuset to update 1623 * @tmp: temporary masks 1624 * 1625 * The effective_cpus is also updated. 1626 * 1627 * cpuset_mutex must be held by the caller. 1628 */ 1629 static void remote_partition_disable(struct cpuset *cs, struct tmpmasks *tmp) 1630 { 1631 WARN_ON_ONCE(!is_remote_partition(cs)); 1632 /* 1633 * When a CPU is offlined, top_cpuset may end up with no available CPUs, 1634 * which should clear subpartitions_cpus. We should not emit a warning for this 1635 * scenario: the hierarchy is updated from top to bottom, so subpartitions_cpus 1636 * may already be cleared when disabling the partition. 1637 */ 1638 WARN_ON_ONCE(!cpumask_subset(cs->effective_xcpus, subpartitions_cpus) && 1639 !cpumask_empty(subpartitions_cpus)); 1640 1641 spin_lock_irq(&callback_lock); 1642 cs->remote_partition = false; 1643 partition_xcpus_del(cs->partition_root_state, NULL, cs->effective_xcpus); 1644 if (cs->prs_err) 1645 cs->partition_root_state = -cs->partition_root_state; 1646 else 1647 cs->partition_root_state = PRS_MEMBER; 1648 1649 /* effective_xcpus may need to be changed */ 1650 compute_excpus(cs, cs->effective_xcpus); 1651 reset_partition_data(cs); 1652 spin_unlock_irq(&callback_lock); 1653 cpuset_force_rebuild(); 1654 1655 /* 1656 * Propagate changes in top_cpuset's effective_cpus down the hierarchy. 1657 */ 1658 cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus); 1659 update_sibling_cpumasks(&top_cpuset, NULL, tmp); 1660 } 1661 1662 /* 1663 * remote_cpus_update - cpus_exclusive change of remote partition 1664 * @cs: the cpuset to be updated 1665 * @xcpus: the new exclusive_cpus mask, if non-NULL 1666 * @excpus: the new effective_xcpus mask 1667 * @tmp: temporary masks 1668 * 1669 * top_cpuset and subpartitions_cpus will be updated or partition can be 1670 * invalidated. 1671 */ 1672 static void remote_cpus_update(struct cpuset *cs, struct cpumask *xcpus, 1673 struct cpumask *excpus, struct tmpmasks *tmp) 1674 { 1675 bool adding, deleting; 1676 int prs = cs->partition_root_state; 1677 1678 if (WARN_ON_ONCE(!is_remote_partition(cs))) 1679 return; 1680 1681 WARN_ON_ONCE(!cpumask_subset(cs->effective_xcpus, subpartitions_cpus)); 1682 1683 if (cpumask_empty(excpus)) { 1684 WRITE_ONCE(cs->prs_err, PERR_CPUSEMPTY); 1685 goto invalidate; 1686 } 1687 1688 adding = cpumask_andnot(tmp->addmask, excpus, cs->effective_xcpus); 1689 deleting = cpumask_andnot(tmp->delmask, cs->effective_xcpus, excpus); 1690 1691 /* 1692 * Additions of remote CPUs is only allowed if those CPUs are 1693 * not allocated to other partitions and there are effective_cpus 1694 * left in the top cpuset. 1695 */ 1696 if (adding) { 1697 WARN_ON_ONCE(cpumask_intersects(tmp->addmask, subpartitions_cpus)); 1698 if (!capable(CAP_SYS_ADMIN)) 1699 WRITE_ONCE(cs->prs_err, PERR_ACCESS); 1700 else if (cpumask_intersects(tmp->addmask, subpartitions_cpus) || 1701 cpumask_subset(top_cpuset.effective_cpus, tmp->addmask)) 1702 WRITE_ONCE(cs->prs_err, PERR_NOCPUS); 1703 else if ((prs == PRS_ISOLATED) && 1704 !isolated_cpus_can_update(tmp->addmask, tmp->delmask)) 1705 WRITE_ONCE(cs->prs_err, PERR_HKEEPING); 1706 if (cs->prs_err) 1707 goto invalidate; 1708 } 1709 1710 spin_lock_irq(&callback_lock); 1711 if (adding) 1712 partition_xcpus_add(prs, NULL, tmp->addmask); 1713 if (deleting) 1714 partition_xcpus_del(prs, NULL, tmp->delmask); 1715 /* 1716 * Need to update effective_xcpus and exclusive_cpus now as 1717 * update_sibling_cpumasks() below may iterate back to the same cs. 1718 */ 1719 cpumask_copy(cs->effective_xcpus, excpus); 1720 if (xcpus) 1721 cpumask_copy(cs->exclusive_cpus, xcpus); 1722 spin_unlock_irq(&callback_lock); 1723 if (adding || deleting) 1724 cpuset_force_rebuild(); 1725 1726 /* 1727 * Propagate changes in top_cpuset's effective_cpus down the hierarchy. 1728 */ 1729 cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus); 1730 update_sibling_cpumasks(&top_cpuset, NULL, tmp); 1731 return; 1732 1733 invalidate: 1734 remote_partition_disable(cs, tmp); 1735 } 1736 1737 /** 1738 * update_parent_effective_cpumask - update effective_cpus mask of parent cpuset 1739 * @cs: The cpuset that requests change in partition root state 1740 * @cmd: Partition root state change command 1741 * @newmask: Optional new cpumask for partcmd_update 1742 * @tmp: Temporary addmask and delmask 1743 * Return: 0 or a partition root state error code 1744 * 1745 * For partcmd_enable*, the cpuset is being transformed from a non-partition 1746 * root to a partition root. The effective_xcpus (cpus_allowed if 1747 * effective_xcpus not set) mask of the given cpuset will be taken away from 1748 * parent's effective_cpus. The function will return 0 if all the CPUs listed 1749 * in effective_xcpus can be granted or an error code will be returned. 1750 * 1751 * For partcmd_disable, the cpuset is being transformed from a partition 1752 * root back to a non-partition root. Any CPUs in effective_xcpus will be 1753 * given back to parent's effective_cpus. 0 will always be returned. 1754 * 1755 * For partcmd_update, if the optional newmask is specified, the cpu list is 1756 * to be changed from effective_xcpus to newmask. Otherwise, effective_xcpus is 1757 * assumed to remain the same. The cpuset should either be a valid or invalid 1758 * partition root. The partition root state may change from valid to invalid 1759 * or vice versa. An error code will be returned if transitioning from 1760 * invalid to valid violates the exclusivity rule. 1761 * 1762 * For partcmd_invalidate, the current partition will be made invalid. 1763 * 1764 * The partcmd_enable* and partcmd_disable commands are used by 1765 * update_prstate(). An error code may be returned and the caller will check 1766 * for error. 1767 * 1768 * The partcmd_update command is used by update_cpumasks_hier() with newmask 1769 * NULL and update_cpumask() with newmask set. The partcmd_invalidate is used 1770 * by update_cpumask() with NULL newmask. In both cases, the callers won't 1771 * check for error and so partition_root_state and prs_err will be updated 1772 * directly. 1773 */ 1774 static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, 1775 struct cpumask *newmask, 1776 struct tmpmasks *tmp) 1777 { 1778 struct cpuset *parent = parent_cs(cs); 1779 int adding; /* Adding cpus to parent's effective_cpus */ 1780 int deleting; /* Deleting cpus from parent's effective_cpus */ 1781 int old_prs, new_prs; 1782 int part_error = PERR_NONE; /* Partition error? */ 1783 struct cpumask *xcpus = user_xcpus(cs); 1784 int parent_prs = parent->partition_root_state; 1785 bool nocpu; 1786 1787 lockdep_assert_cpuset_lock_held(); 1788 WARN_ON_ONCE(is_remote_partition(cs)); /* For local partition only */ 1789 1790 /* 1791 * new_prs will only be changed for the partcmd_update and 1792 * partcmd_invalidate commands. 1793 */ 1794 adding = deleting = false; 1795 old_prs = new_prs = cs->partition_root_state; 1796 1797 if (cmd == partcmd_invalidate) { 1798 if (is_partition_invalid(cs)) 1799 return 0; 1800 1801 /* 1802 * Make the current partition invalid. 1803 */ 1804 if (is_partition_valid(parent)) 1805 adding = cpumask_and(tmp->addmask, 1806 cs->effective_xcpus, 1807 parent->effective_xcpus); 1808 if (old_prs > 0) 1809 new_prs = -old_prs; 1810 1811 goto write_error; 1812 } 1813 1814 /* 1815 * The parent must be a partition root. 1816 * The new cpumask, if present, or the current cpus_allowed must 1817 * not be empty. 1818 */ 1819 if (!is_partition_valid(parent)) { 1820 return is_partition_invalid(parent) 1821 ? PERR_INVPARENT : PERR_NOTPART; 1822 } 1823 if (!newmask && xcpus_empty(cs)) 1824 return PERR_CPUSEMPTY; 1825 1826 nocpu = tasks_nocpu_error(parent, cs, xcpus); 1827 1828 if ((cmd == partcmd_enable) || (cmd == partcmd_enablei)) { 1829 /* 1830 * Need to call compute_excpus() in case 1831 * exclusive_cpus not set. Sibling conflict should only happen 1832 * if exclusive_cpus isn't set. 1833 */ 1834 xcpus = tmp->delmask; 1835 if (compute_excpus(cs, xcpus)) 1836 WARN_ON_ONCE(!cpumask_empty(cs->exclusive_cpus)); 1837 new_prs = (cmd == partcmd_enable) ? PRS_ROOT : PRS_ISOLATED; 1838 1839 /* 1840 * Enabling partition root is not allowed if its 1841 * effective_xcpus is empty. 1842 */ 1843 if (cpumask_empty(xcpus)) 1844 return PERR_INVCPUS; 1845 1846 if (prstate_housekeeping_conflict(new_prs, xcpus)) 1847 return PERR_HKEEPING; 1848 1849 if ((new_prs == PRS_ISOLATED) && (new_prs != parent_prs) && 1850 !isolated_cpus_can_update(xcpus, NULL)) 1851 return PERR_HKEEPING; 1852 1853 if (tasks_nocpu_error(parent, cs, xcpus)) 1854 return PERR_NOCPUS; 1855 1856 /* 1857 * This function will only be called when all the preliminary 1858 * checks have passed. At this point, the following condition 1859 * should hold. 1860 * 1861 * (cs->effective_xcpus & cpu_active_mask) ⊆ parent->effective_cpus 1862 * 1863 * Warn if it is not the case. 1864 */ 1865 cpumask_and(tmp->new_cpus, xcpus, cpu_active_mask); 1866 WARN_ON_ONCE(!cpumask_subset(tmp->new_cpus, parent->effective_cpus)); 1867 1868 deleting = true; 1869 } else if (cmd == partcmd_disable) { 1870 /* 1871 * May need to add cpus back to parent's effective_cpus 1872 * (and maybe removed from subpartitions_cpus/isolated_cpus) 1873 * for valid partition root. xcpus may contain CPUs that 1874 * shouldn't be removed from the two global cpumasks. 1875 */ 1876 if (is_partition_valid(cs)) { 1877 cpumask_copy(tmp->addmask, cs->effective_xcpus); 1878 adding = true; 1879 } 1880 new_prs = PRS_MEMBER; 1881 } else if (newmask) { 1882 /* 1883 * Empty cpumask is not allowed 1884 */ 1885 if (cpumask_empty(newmask)) { 1886 part_error = PERR_CPUSEMPTY; 1887 goto write_error; 1888 } 1889 1890 /* Check newmask again, whether cpus are available for parent/cs */ 1891 nocpu |= tasks_nocpu_error(parent, cs, newmask); 1892 1893 /* 1894 * partcmd_update with newmask: 1895 * 1896 * Compute add/delete mask to/from effective_cpus 1897 * 1898 * For valid partition: 1899 * addmask = effective_xcpus & ~newmask 1900 * & parent->effective_xcpus 1901 * delmask = newmask & ~effective_xcpus 1902 * & parent->effective_xcpus 1903 * 1904 * For invalid partition: 1905 * delmask = newmask & parent->effective_xcpus 1906 * The partition may become valid soon. 1907 */ 1908 if (is_partition_invalid(cs)) { 1909 adding = false; 1910 deleting = cpumask_and(tmp->delmask, 1911 newmask, parent->effective_xcpus); 1912 } else { 1913 cpumask_andnot(tmp->addmask, cs->effective_xcpus, newmask); 1914 adding = cpumask_and(tmp->addmask, tmp->addmask, 1915 parent->effective_xcpus); 1916 1917 cpumask_andnot(tmp->delmask, newmask, cs->effective_xcpus); 1918 deleting = cpumask_and(tmp->delmask, tmp->delmask, 1919 parent->effective_xcpus); 1920 } 1921 1922 /* 1923 * TBD: Invalidate a currently valid child root partition may 1924 * still break isolated_cpus_can_update() rule if parent is an 1925 * isolated partition. 1926 */ 1927 if (is_partition_valid(cs) && (old_prs != parent_prs)) { 1928 if ((parent_prs == PRS_ROOT) && 1929 /* Adding to parent means removing isolated CPUs */ 1930 !isolated_cpus_can_update(tmp->delmask, tmp->addmask)) 1931 part_error = PERR_HKEEPING; 1932 if ((parent_prs == PRS_ISOLATED) && 1933 /* Adding to parent means adding isolated CPUs */ 1934 !isolated_cpus_can_update(tmp->addmask, tmp->delmask)) 1935 part_error = PERR_HKEEPING; 1936 } 1937 1938 /* 1939 * The new CPUs to be removed from parent's effective CPUs 1940 * must be present. 1941 */ 1942 if (deleting) { 1943 cpumask_and(tmp->new_cpus, tmp->delmask, cpu_active_mask); 1944 WARN_ON_ONCE(!cpumask_subset(tmp->new_cpus, parent->effective_cpus)); 1945 } 1946 1947 /* 1948 * Make partition invalid if parent's effective_cpus could 1949 * become empty and there are tasks in the parent. 1950 */ 1951 if (nocpu && (!adding || 1952 !cpumask_intersects(tmp->addmask, cpu_active_mask))) { 1953 part_error = PERR_NOCPUS; 1954 deleting = false; 1955 adding = cpumask_and(tmp->addmask, 1956 cs->effective_xcpus, parent->effective_xcpus); 1957 } 1958 } else { 1959 /* 1960 * partcmd_update w/o newmask 1961 * 1962 * delmask = effective_xcpus & parent->effective_cpus 1963 * 1964 * This can be called from: 1965 * 1) update_cpumasks_hier() 1966 * 2) cpuset_hotplug_update_tasks() 1967 * 1968 * Check to see if it can be transitioned from valid to 1969 * invalid partition or vice versa. 1970 * 1971 * A partition error happens when parent has tasks and all 1972 * its effective CPUs will have to be distributed out. 1973 */ 1974 if (nocpu) { 1975 part_error = PERR_NOCPUS; 1976 if (is_partition_valid(cs)) 1977 adding = cpumask_and(tmp->addmask, 1978 cs->effective_xcpus, 1979 parent->effective_xcpus); 1980 } else if (is_partition_invalid(cs) && !cpumask_empty(xcpus) && 1981 cpumask_subset(xcpus, parent->effective_xcpus)) { 1982 struct cgroup_subsys_state *css; 1983 struct cpuset *child; 1984 bool exclusive = true; 1985 1986 /* 1987 * Convert invalid partition to valid has to 1988 * pass the cpu exclusivity test. 1989 */ 1990 rcu_read_lock(); 1991 cpuset_for_each_child(child, css, parent) { 1992 if (child == cs) 1993 continue; 1994 if (!cpusets_are_exclusive(cs, child)) { 1995 exclusive = false; 1996 break; 1997 } 1998 } 1999 rcu_read_unlock(); 2000 if (exclusive) 2001 deleting = cpumask_and(tmp->delmask, 2002 xcpus, parent->effective_cpus); 2003 else 2004 part_error = PERR_NOTEXCL; 2005 } 2006 } 2007 2008 write_error: 2009 if (part_error) 2010 WRITE_ONCE(cs->prs_err, part_error); 2011 2012 if (cmd == partcmd_update) { 2013 /* 2014 * Check for possible transition between valid and invalid 2015 * partition root. 2016 */ 2017 switch (cs->partition_root_state) { 2018 case PRS_ROOT: 2019 case PRS_ISOLATED: 2020 if (part_error) 2021 new_prs = -old_prs; 2022 break; 2023 case PRS_INVALID_ROOT: 2024 case PRS_INVALID_ISOLATED: 2025 if (!part_error) 2026 new_prs = -old_prs; 2027 break; 2028 } 2029 } 2030 2031 if (!adding && !deleting && (new_prs == old_prs)) 2032 return 0; 2033 2034 /* 2035 * Transitioning between invalid to valid or vice versa may require 2036 * changing CS_CPU_EXCLUSIVE. In the case of partcmd_update, 2037 * validate_change() has already been successfully called and 2038 * CPU lists in cs haven't been updated yet. So defer it to later. 2039 */ 2040 if ((old_prs != new_prs) && (cmd != partcmd_update)) { 2041 int err = update_partition_exclusive_flag(cs, new_prs); 2042 2043 if (err) 2044 return err; 2045 } 2046 2047 /* 2048 * Change the parent's effective_cpus & effective_xcpus (top cpuset 2049 * only). 2050 * 2051 * Newly added CPUs will be removed from effective_cpus and 2052 * newly deleted ones will be added back to effective_cpus. 2053 */ 2054 spin_lock_irq(&callback_lock); 2055 if (old_prs != new_prs) 2056 cs->partition_root_state = new_prs; 2057 2058 /* 2059 * Adding to parent's effective_cpus means deletion CPUs from cs 2060 * and vice versa. 2061 */ 2062 if (adding) 2063 partition_xcpus_del(old_prs, parent, tmp->addmask); 2064 if (deleting) 2065 partition_xcpus_add(new_prs, parent, tmp->delmask); 2066 2067 spin_unlock_irq(&callback_lock); 2068 2069 if ((old_prs != new_prs) && (cmd == partcmd_update)) 2070 update_partition_exclusive_flag(cs, new_prs); 2071 2072 if (adding || deleting) { 2073 cpuset_update_tasks_cpumask(parent, tmp->addmask); 2074 update_sibling_cpumasks(parent, cs, tmp); 2075 } 2076 2077 /* 2078 * For partcmd_update without newmask, it is being called from 2079 * cpuset_handle_hotplug(). Update the load balance flag and 2080 * scheduling domain accordingly. 2081 */ 2082 if ((cmd == partcmd_update) && !newmask) 2083 update_partition_sd_lb(cs, old_prs); 2084 2085 notify_partition_change(cs, old_prs); 2086 return 0; 2087 } 2088 2089 /** 2090 * compute_partition_effective_cpumask - compute effective_cpus for partition 2091 * @cs: partition root cpuset 2092 * @new_ecpus: previously computed effective_cpus to be updated 2093 * 2094 * Compute the effective_cpus of a partition root by scanning effective_xcpus 2095 * of child partition roots and excluding their effective_xcpus. 2096 * 2097 * This has the side effect of invalidating valid child partition roots, 2098 * if necessary. Since it is called from either cpuset_hotplug_update_tasks() 2099 * or update_cpumasks_hier() where parent and children are modified 2100 * successively, we don't need to call update_parent_effective_cpumask() 2101 * and the child's effective_cpus will be updated in later iterations. 2102 * 2103 * Note that rcu_read_lock() is assumed to be held. 2104 */ 2105 static void compute_partition_effective_cpumask(struct cpuset *cs, 2106 struct cpumask *new_ecpus) 2107 { 2108 struct cgroup_subsys_state *css; 2109 struct cpuset *child; 2110 bool populated = partition_is_populated(cs, NULL); 2111 2112 /* 2113 * Check child partition roots to see if they should be 2114 * invalidated when 2115 * 1) child effective_xcpus not a subset of new 2116 * excluisve_cpus 2117 * 2) All the effective_cpus will be used up and cp 2118 * has tasks 2119 */ 2120 compute_excpus(cs, new_ecpus); 2121 cpumask_and(new_ecpus, new_ecpus, cpu_active_mask); 2122 2123 rcu_read_lock(); 2124 cpuset_for_each_child(child, css, cs) { 2125 if (!is_partition_valid(child)) 2126 continue; 2127 2128 /* 2129 * There shouldn't be a remote partition underneath another 2130 * partition root. 2131 */ 2132 WARN_ON_ONCE(is_remote_partition(child)); 2133 WRITE_ONCE(child->prs_err, 0); 2134 if (!cpumask_subset(child->effective_xcpus, 2135 cs->effective_xcpus)) 2136 WRITE_ONCE(child->prs_err, PERR_INVCPUS); 2137 else if (populated && 2138 cpumask_subset(new_ecpus, child->effective_xcpus)) 2139 WRITE_ONCE(child->prs_err, PERR_NOCPUS); 2140 2141 if (child->prs_err) { 2142 int old_prs = child->partition_root_state; 2143 2144 /* 2145 * Invalidate child partition 2146 */ 2147 spin_lock_irq(&callback_lock); 2148 make_partition_invalid(child); 2149 spin_unlock_irq(&callback_lock); 2150 notify_partition_change(child, old_prs); 2151 continue; 2152 } 2153 cpumask_andnot(new_ecpus, new_ecpus, 2154 child->effective_xcpus); 2155 } 2156 rcu_read_unlock(); 2157 } 2158 2159 /* 2160 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree 2161 * @cs: the cpuset to consider 2162 * @tmp: temp variables for calculating effective_cpus & partition setup 2163 * @force: don't skip any descendant cpusets if set 2164 * 2165 * When configured cpumask is changed, the effective cpumasks of this cpuset 2166 * and all its descendants need to be updated. 2167 * 2168 * On legacy hierarchy, effective_cpus will be the same with cpu_allowed. 2169 * 2170 * Called with cpuset_mutex held 2171 */ 2172 static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp, 2173 bool force) 2174 { 2175 struct cpuset *cp; 2176 struct cgroup_subsys_state *pos_css; 2177 int old_prs, new_prs; 2178 2179 rcu_read_lock(); 2180 cpuset_for_each_descendant_pre(cp, pos_css, cs) { 2181 struct cpuset *parent = parent_cs(cp); 2182 bool remote = is_remote_partition(cp); 2183 bool update_parent = false; 2184 2185 old_prs = new_prs = cp->partition_root_state; 2186 2187 /* 2188 * For child remote partition root (!= cs), we need to call 2189 * remote_cpus_update() if effective_xcpus will be changed. 2190 * Otherwise, we can skip the whole subtree. 2191 * 2192 * remote_cpus_update() will reuse tmp->new_cpus only after 2193 * its value is being processed. 2194 */ 2195 if (remote && (cp != cs)) { 2196 compute_excpus(cp, tmp->new_cpus); 2197 if (cpumask_equal(cp->effective_xcpus, tmp->new_cpus)) { 2198 pos_css = css_rightmost_descendant(pos_css); 2199 continue; 2200 } 2201 rcu_read_unlock(); 2202 remote_cpus_update(cp, NULL, tmp->new_cpus, tmp); 2203 rcu_read_lock(); 2204 2205 /* Remote partition may be invalidated */ 2206 new_prs = cp->partition_root_state; 2207 remote = (new_prs == old_prs); 2208 } 2209 2210 if (remote || (is_partition_valid(parent) && is_partition_valid(cp))) 2211 compute_partition_effective_cpumask(cp, tmp->new_cpus); 2212 else 2213 compute_effective_cpumask(tmp->new_cpus, cp, parent); 2214 2215 if (remote) 2216 goto get_css; /* Ready to update cpuset data */ 2217 2218 /* 2219 * A partition with no effective_cpus is allowed as long as 2220 * there is no task associated with it. Call 2221 * update_parent_effective_cpumask() to check it. 2222 */ 2223 if (is_partition_valid(cp) && cpumask_empty(tmp->new_cpus)) { 2224 update_parent = true; 2225 goto update_parent_effective; 2226 } 2227 2228 /* 2229 * Skip the whole subtree if 2230 * 1) the cpumask remains the same, 2231 * 2) has no partition root state, 2232 * 3) force flag not set, and 2233 * 4) for v2 load balance state same as its parent. 2234 */ 2235 if (!cp->partition_root_state && !force && 2236 cpumask_equal(tmp->new_cpus, cp->effective_cpus) && 2237 (!cpuset_v2() || 2238 (is_sched_load_balance(parent) == is_sched_load_balance(cp)))) { 2239 pos_css = css_rightmost_descendant(pos_css); 2240 continue; 2241 } 2242 2243 update_parent_effective: 2244 /* 2245 * update_parent_effective_cpumask() should have been called 2246 * for cs already in update_cpumask(). We should also call 2247 * cpuset_update_tasks_cpumask() again for tasks in the parent 2248 * cpuset if the parent's effective_cpus changes. 2249 */ 2250 if ((cp != cs) && old_prs) { 2251 switch (parent->partition_root_state) { 2252 case PRS_ROOT: 2253 case PRS_ISOLATED: 2254 update_parent = true; 2255 break; 2256 2257 default: 2258 /* 2259 * When parent is not a partition root or is 2260 * invalid, child partition roots become 2261 * invalid too. 2262 */ 2263 if (is_partition_valid(cp)) 2264 new_prs = -cp->partition_root_state; 2265 WRITE_ONCE(cp->prs_err, 2266 is_partition_invalid(parent) 2267 ? PERR_INVPARENT : PERR_NOTPART); 2268 break; 2269 } 2270 } 2271 get_css: 2272 if (!css_tryget_online(&cp->css)) 2273 continue; 2274 rcu_read_unlock(); 2275 2276 if (update_parent) { 2277 update_parent_effective_cpumask(cp, partcmd_update, NULL, tmp); 2278 /* 2279 * The cpuset partition_root_state may become 2280 * invalid. Capture it. 2281 */ 2282 new_prs = cp->partition_root_state; 2283 } 2284 2285 spin_lock_irq(&callback_lock); 2286 cpumask_copy(cp->effective_cpus, tmp->new_cpus); 2287 cp->partition_root_state = new_prs; 2288 /* 2289 * Need to compute effective_xcpus if either exclusive_cpus 2290 * is non-empty or it is a valid partition root. 2291 */ 2292 if ((new_prs > 0) || !cpumask_empty(cp->exclusive_cpus)) 2293 compute_excpus(cp, cp->effective_xcpus); 2294 if (new_prs <= 0) 2295 reset_partition_data(cp); 2296 spin_unlock_irq(&callback_lock); 2297 2298 notify_partition_change(cp, old_prs); 2299 2300 WARN_ON(!is_in_v2_mode() && 2301 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus)); 2302 2303 cpuset_update_tasks_cpumask(cp, tmp->new_cpus); 2304 2305 /* 2306 * On default hierarchy, inherit the CS_SCHED_LOAD_BALANCE 2307 * from parent if current cpuset isn't a valid partition root 2308 * and their load balance states differ. 2309 */ 2310 if (cpuset_v2() && !is_partition_valid(cp) && 2311 (is_sched_load_balance(parent) != is_sched_load_balance(cp))) { 2312 if (is_sched_load_balance(parent)) 2313 set_bit(CS_SCHED_LOAD_BALANCE, &cp->flags); 2314 else 2315 clear_bit(CS_SCHED_LOAD_BALANCE, &cp->flags); 2316 } 2317 2318 /* 2319 * On legacy hierarchy, if the effective cpumask of any non- 2320 * empty cpuset is changed, we need to rebuild sched domains. 2321 * On default hierarchy, the cpuset needs to be a partition 2322 * root as well. 2323 */ 2324 if (!cpumask_empty(cp->cpus_allowed) && 2325 is_sched_load_balance(cp) && 2326 (!cpuset_v2() || is_partition_valid(cp))) 2327 cpuset_force_rebuild(); 2328 2329 rcu_read_lock(); 2330 css_put(&cp->css); 2331 } 2332 rcu_read_unlock(); 2333 } 2334 2335 /** 2336 * update_sibling_cpumasks - Update siblings cpumasks 2337 * @parent: Parent cpuset 2338 * @cs: Current cpuset 2339 * @tmp: Temp variables 2340 */ 2341 static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs, 2342 struct tmpmasks *tmp) 2343 { 2344 struct cpuset *sibling; 2345 struct cgroup_subsys_state *pos_css; 2346 2347 lockdep_assert_cpuset_lock_held(); 2348 2349 /* 2350 * Check all its siblings and call update_cpumasks_hier() 2351 * if their effective_cpus will need to be changed. 2352 * 2353 * It is possible a change in parent's effective_cpus 2354 * due to a change in a child partition's effective_xcpus will impact 2355 * its siblings even if they do not inherit parent's effective_cpus 2356 * directly. It should not impact valid partition. 2357 * 2358 * The update_cpumasks_hier() function may sleep. So we have to 2359 * release the RCU read lock before calling it. 2360 */ 2361 rcu_read_lock(); 2362 cpuset_for_each_child(sibling, pos_css, parent) { 2363 if (sibling == cs || is_partition_valid(sibling)) 2364 continue; 2365 2366 compute_effective_cpumask(tmp->new_cpus, sibling, 2367 parent); 2368 if (cpumask_equal(tmp->new_cpus, sibling->effective_cpus)) 2369 continue; 2370 2371 if (!css_tryget_online(&sibling->css)) 2372 continue; 2373 2374 rcu_read_unlock(); 2375 update_cpumasks_hier(sibling, tmp, false); 2376 rcu_read_lock(); 2377 css_put(&sibling->css); 2378 } 2379 rcu_read_unlock(); 2380 } 2381 2382 static int parse_cpuset_cpulist(const char *buf, struct cpumask *out_mask) 2383 { 2384 int retval; 2385 2386 retval = cpulist_parse(buf, out_mask); 2387 if (retval < 0) 2388 return retval; 2389 if (!cpumask_subset(out_mask, top_cpuset.cpus_allowed)) 2390 return -EINVAL; 2391 2392 return 0; 2393 } 2394 2395 /** 2396 * validate_partition - Validate a cpuset partition configuration 2397 * @cs: The cpuset to validate 2398 * @trialcs: The trial cpuset containing proposed configuration changes 2399 * 2400 * If any validation check fails, the appropriate error code is set in the 2401 * cpuset's prs_err field. 2402 * 2403 * Return: PRS error code (0 if valid, non-zero error code if invalid) 2404 */ 2405 static enum prs_errcode validate_partition(struct cpuset *cs, struct cpuset *trialcs) 2406 { 2407 struct cpuset *parent = parent_cs(cs); 2408 2409 if (cs_is_member(trialcs)) 2410 return PERR_NONE; 2411 2412 if (cpumask_empty(trialcs->effective_xcpus)) 2413 return PERR_INVCPUS; 2414 2415 if (prstate_housekeeping_conflict(trialcs->partition_root_state, 2416 trialcs->effective_xcpus)) 2417 return PERR_HKEEPING; 2418 2419 if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus)) 2420 return PERR_NOCPUS; 2421 2422 return PERR_NONE; 2423 } 2424 2425 /** 2426 * partition_cpus_change - Handle partition state changes due to CPU mask updates 2427 * @cs: The target cpuset being modified 2428 * @trialcs: The trial cpuset containing proposed configuration changes 2429 * @tmp: Temporary masks for intermediate calculations 2430 * 2431 * This function handles partition state transitions triggered by CPU mask changes. 2432 * CPU modifications may cause a partition to be disabled or require state updates. 2433 */ 2434 static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs, 2435 struct tmpmasks *tmp) 2436 { 2437 enum prs_errcode prs_err; 2438 2439 if (cs_is_member(cs)) 2440 return; 2441 2442 prs_err = validate_partition(cs, trialcs); 2443 if (prs_err) { 2444 WRITE_ONCE(cs->prs_err, prs_err); 2445 trialcs->prs_err = prs_err; 2446 } 2447 2448 if (is_remote_partition(cs)) { 2449 if (trialcs->prs_err) 2450 remote_partition_disable(cs, tmp); 2451 else 2452 remote_cpus_update(cs, trialcs->exclusive_cpus, 2453 trialcs->effective_xcpus, tmp); 2454 } else { 2455 if (trialcs->prs_err) 2456 update_parent_effective_cpumask(cs, partcmd_invalidate, 2457 NULL, tmp); 2458 else 2459 update_parent_effective_cpumask(cs, partcmd_update, 2460 trialcs->effective_xcpus, tmp); 2461 } 2462 } 2463 2464 /** 2465 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it 2466 * @cs: the cpuset to consider 2467 * @trialcs: trial cpuset 2468 * @buf: buffer of cpu numbers written to this cpuset 2469 */ 2470 static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, 2471 const char *buf) 2472 { 2473 int retval; 2474 struct tmpmasks tmp; 2475 bool force = false; 2476 int old_prs = cs->partition_root_state; 2477 2478 retval = parse_cpuset_cpulist(buf, trialcs->cpus_allowed); 2479 if (retval < 0) 2480 return retval; 2481 2482 /* Nothing to do if the cpus didn't change */ 2483 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed)) 2484 return 0; 2485 2486 compute_trialcs_excpus(trialcs, cs); 2487 trialcs->prs_err = PERR_NONE; 2488 2489 retval = validate_change(cs, trialcs); 2490 if (retval < 0) 2491 return retval; 2492 2493 if (alloc_tmpmasks(&tmp)) 2494 return -ENOMEM; 2495 2496 /* 2497 * Check all the descendants in update_cpumasks_hier() if 2498 * effective_xcpus is to be changed. 2499 */ 2500 force = !cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus); 2501 2502 partition_cpus_change(cs, trialcs, &tmp); 2503 2504 spin_lock_irq(&callback_lock); 2505 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); 2506 cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus); 2507 if ((old_prs > 0) && !is_partition_valid(cs)) 2508 reset_partition_data(cs); 2509 spin_unlock_irq(&callback_lock); 2510 2511 /* effective_cpus/effective_xcpus will be updated here */ 2512 update_cpumasks_hier(cs, &tmp, force); 2513 2514 /* Update CS_SCHED_LOAD_BALANCE and/or sched_domains, if necessary */ 2515 if (cs->partition_root_state) 2516 update_partition_sd_lb(cs, old_prs); 2517 2518 free_tmpmasks(&tmp); 2519 return retval; 2520 } 2521 2522 /** 2523 * update_exclusive_cpumask - update the exclusive_cpus mask of a cpuset 2524 * @cs: the cpuset to consider 2525 * @trialcs: trial cpuset 2526 * @buf: buffer of cpu numbers written to this cpuset 2527 * 2528 * The tasks' cpumask will be updated if cs is a valid partition root. 2529 */ 2530 static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs, 2531 const char *buf) 2532 { 2533 int retval; 2534 struct tmpmasks tmp; 2535 bool force = false; 2536 int old_prs = cs->partition_root_state; 2537 2538 retval = parse_cpuset_cpulist(buf, trialcs->exclusive_cpus); 2539 if (retval < 0) 2540 return retval; 2541 2542 /* Nothing to do if the CPUs didn't change */ 2543 if (cpumask_equal(cs->exclusive_cpus, trialcs->exclusive_cpus)) 2544 return 0; 2545 2546 /* 2547 * Reject the change if there is exclusive CPUs conflict with 2548 * the siblings. 2549 */ 2550 if (compute_trialcs_excpus(trialcs, cs)) 2551 return -EINVAL; 2552 2553 /* 2554 * Check all the descendants in update_cpumasks_hier() if 2555 * effective_xcpus is to be changed. 2556 */ 2557 force = !cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus); 2558 2559 retval = validate_change(cs, trialcs); 2560 if (retval) 2561 return retval; 2562 2563 if (alloc_tmpmasks(&tmp)) 2564 return -ENOMEM; 2565 2566 trialcs->prs_err = PERR_NONE; 2567 partition_cpus_change(cs, trialcs, &tmp); 2568 2569 spin_lock_irq(&callback_lock); 2570 cpumask_copy(cs->exclusive_cpus, trialcs->exclusive_cpus); 2571 cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus); 2572 if ((old_prs > 0) && !is_partition_valid(cs)) 2573 reset_partition_data(cs); 2574 spin_unlock_irq(&callback_lock); 2575 2576 /* 2577 * Call update_cpumasks_hier() to update effective_cpus/effective_xcpus 2578 * of the subtree when it is a valid partition root or effective_xcpus 2579 * is updated. 2580 */ 2581 if (is_partition_valid(cs) || force) 2582 update_cpumasks_hier(cs, &tmp, force); 2583 2584 /* Update CS_SCHED_LOAD_BALANCE and/or sched_domains, if necessary */ 2585 if (cs->partition_root_state) 2586 update_partition_sd_lb(cs, old_prs); 2587 2588 free_tmpmasks(&tmp); 2589 return 0; 2590 } 2591 2592 /* 2593 * Migrate memory region from one set of nodes to another. This is 2594 * performed asynchronously as it can be called from process migration path 2595 * holding locks involved in process management. All mm migrations are 2596 * performed in the queued order and can be waited for by flushing 2597 * cpuset_migrate_mm_wq. 2598 */ 2599 2600 struct cpuset_migrate_mm_work { 2601 struct work_struct work; 2602 struct mm_struct *mm; 2603 nodemask_t from; 2604 nodemask_t to; 2605 }; 2606 2607 static void cpuset_migrate_mm_workfn(struct work_struct *work) 2608 { 2609 struct cpuset_migrate_mm_work *mwork = 2610 container_of(work, struct cpuset_migrate_mm_work, work); 2611 2612 /* on a wq worker, no need to worry about %current's mems_allowed */ 2613 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL); 2614 mmput(mwork->mm); 2615 kfree(mwork); 2616 } 2617 2618 static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, 2619 const nodemask_t *to) 2620 { 2621 struct cpuset_migrate_mm_work *mwork; 2622 2623 if (nodes_equal(*from, *to)) { 2624 mmput(mm); 2625 return; 2626 } 2627 2628 mwork = kzalloc_obj(*mwork); 2629 if (mwork) { 2630 mwork->mm = mm; 2631 mwork->from = *from; 2632 mwork->to = *to; 2633 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn); 2634 queue_work(cpuset_migrate_mm_wq, &mwork->work); 2635 } else { 2636 mmput(mm); 2637 } 2638 } 2639 2640 static void flush_migrate_mm_task_workfn(struct callback_head *head) 2641 { 2642 flush_workqueue(cpuset_migrate_mm_wq); 2643 kfree(head); 2644 } 2645 2646 static void schedule_flush_migrate_mm(void) 2647 { 2648 struct callback_head *flush_cb; 2649 2650 flush_cb = kzalloc_obj(struct callback_head); 2651 if (!flush_cb) 2652 return; 2653 2654 init_task_work(flush_cb, flush_migrate_mm_task_workfn); 2655 2656 if (task_work_add(current, flush_cb, TWA_RESUME)) 2657 kfree(flush_cb); 2658 } 2659 2660 /* 2661 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy 2662 * @tsk: the task to change 2663 * @newmems: new nodes that the task will be set 2664 * 2665 * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed 2666 * and rebind an eventual tasks' mempolicy. If the task is allocating in 2667 * parallel, it might temporarily see an empty intersection, which results in 2668 * a seqlock check and retry before OOM or allocation failure. 2669 */ 2670 static void cpuset_change_task_nodemask(struct task_struct *tsk, 2671 nodemask_t *newmems) 2672 { 2673 task_lock(tsk); 2674 2675 local_irq_disable(); 2676 write_seqcount_begin(&tsk->mems_allowed_seq); 2677 2678 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); 2679 mpol_rebind_task(tsk, newmems); 2680 tsk->mems_allowed = *newmems; 2681 2682 write_seqcount_end(&tsk->mems_allowed_seq); 2683 local_irq_enable(); 2684 2685 task_unlock(tsk); 2686 } 2687 2688 static void *cpuset_being_rebound; 2689 2690 /** 2691 * cpuset_update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. 2692 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed 2693 * 2694 * Iterate through each task of @cs updating its mems_allowed to the 2695 * effective cpuset's. As this function is called with cpuset_mutex held, 2696 * cpuset membership stays stable. 2697 * 2698 * - cpuset_change_task_nodemask(): guarantee_online_mems() 2699 * - mpol_rebind_mm(): effective_mems 2700 * - cpuset_migrate_mm(): guarantee_online_mems() 2701 * - old_mems_allowed: guarantee_online_mems() 2702 * 2703 * For v2, guarantee_online_mems() should return a node mask that is the same 2704 * as the effective_mems of current cpuset. 2705 */ 2706 void cpuset_update_tasks_nodemask(struct cpuset *cs) 2707 { 2708 static nodemask_t newmems; /* protected by cpuset_mutex */ 2709 struct css_task_iter it; 2710 struct task_struct *task; 2711 2712 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ 2713 guarantee_online_mems(cs, &newmems); 2714 2715 /* 2716 * The mpol_rebind_mm() call takes mmap_lock, which we couldn't 2717 * take while holding tasklist_lock. Forks can happen - the 2718 * mpol_dup() cpuset_being_rebound check will catch such forks, 2719 * and rebind their vma mempolicies too. Because we still hold 2720 * the global cpuset_mutex, we know that no other rebind effort 2721 * will be contending for the global variable cpuset_being_rebound. 2722 * It's ok if we rebind the same mm twice; mpol_rebind_mm() 2723 * is idempotent. Also migrate pages in each mm to new nodes. 2724 */ 2725 css_task_iter_start(&cs->css, 0, &it); 2726 while ((task = css_task_iter_next(&it))) { 2727 struct mm_struct *mm; 2728 bool migrate; 2729 2730 cpuset_change_task_nodemask(task, &newmems); 2731 2732 /* Rebind and migrate mm only for thread group leader */ 2733 if (!thread_group_leader(task)) 2734 continue; 2735 2736 mm = get_task_mm(task); 2737 if (!mm) 2738 continue; 2739 2740 migrate = is_memory_migrate(cs); 2741 2742 /* 2743 * For v1 we can have empty effective_mems, but we cannot 2744 * attach any tasks (see cpuset_can_attach_check()). For v2, 2745 * effective_mems is guaranteed to not be empty. 2746 */ 2747 mpol_rebind_mm(mm, &cs->effective_mems); 2748 if (migrate) 2749 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems); 2750 else 2751 mmput(mm); 2752 } 2753 css_task_iter_end(&it); 2754 2755 /* 2756 * All the tasks' nodemasks have been updated, update 2757 * cs->old_mems_allowed. 2758 */ 2759 cs->old_mems_allowed = newmems; 2760 2761 /* We're done rebinding vmas to this cpuset's new mems_allowed. */ 2762 cpuset_being_rebound = NULL; 2763 } 2764 2765 /* 2766 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree 2767 * @cs: the cpuset to consider 2768 * @new_mems: a temp variable for calculating new effective_mems 2769 * 2770 * When configured nodemask is changed, the effective nodemasks of this cpuset 2771 * and all its descendants need to be updated. 2772 * 2773 * On legacy hierarchy, effective_mems will be the same with mems_allowed. 2774 * 2775 * Called with cpuset_mutex held 2776 */ 2777 static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) 2778 { 2779 struct cpuset *cp; 2780 struct cgroup_subsys_state *pos_css; 2781 2782 rcu_read_lock(); 2783 cpuset_for_each_descendant_pre(cp, pos_css, cs) { 2784 struct cpuset *parent = parent_cs(cp); 2785 2786 compute_effective_nodemask(new_mems, cp, parent); 2787 2788 /* Skip the whole subtree if the nodemask remains the same. */ 2789 if (nodes_equal(*new_mems, cp->effective_mems)) { 2790 pos_css = css_rightmost_descendant(pos_css); 2791 continue; 2792 } 2793 2794 if (!css_tryget_online(&cp->css)) 2795 continue; 2796 rcu_read_unlock(); 2797 2798 spin_lock_irq(&callback_lock); 2799 cp->effective_mems = *new_mems; 2800 spin_unlock_irq(&callback_lock); 2801 2802 WARN_ON(!is_in_v2_mode() && 2803 !nodes_equal(cp->mems_allowed, cp->effective_mems)); 2804 2805 cpuset_update_tasks_nodemask(cp); 2806 2807 rcu_read_lock(); 2808 css_put(&cp->css); 2809 } 2810 rcu_read_unlock(); 2811 } 2812 2813 /* 2814 * Handle user request to change the 'mems' memory placement 2815 * of a cpuset. Needs to validate the request, update the 2816 * cpusets mems_allowed, and for each task in the cpuset, 2817 * update mems_allowed and rebind task's mempolicy and any vma 2818 * mempolicies and if the cpuset is marked 'memory_migrate', 2819 * migrate the tasks pages to the new memory. 2820 * 2821 * Call with cpuset_mutex held. May take callback_lock during call. 2822 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, 2823 * lock each such tasks mm->mmap_lock, scan its vma's and rebind 2824 * their mempolicies to the cpusets new mems_allowed. 2825 */ 2826 static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, 2827 const char *buf) 2828 { 2829 int retval; 2830 2831 /* 2832 * An empty mems_allowed is ok iff there are no tasks in the cpuset. 2833 * The validate_change() call ensures that cpusets with tasks have memory. 2834 */ 2835 retval = nodelist_parse(buf, trialcs->mems_allowed); 2836 if (retval < 0) 2837 return retval; 2838 2839 if (!nodes_subset(trialcs->mems_allowed, 2840 top_cpuset.mems_allowed)) 2841 return -EINVAL; 2842 2843 /* No change? nothing to do */ 2844 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) 2845 return 0; 2846 2847 retval = validate_change(cs, trialcs); 2848 if (retval < 0) 2849 return retval; 2850 2851 check_insane_mems_config(&trialcs->mems_allowed); 2852 2853 spin_lock_irq(&callback_lock); 2854 cs->mems_allowed = trialcs->mems_allowed; 2855 spin_unlock_irq(&callback_lock); 2856 2857 /* use trialcs->mems_allowed as a temp variable */ 2858 update_nodemasks_hier(cs, &trialcs->mems_allowed); 2859 return 0; 2860 } 2861 2862 bool current_cpuset_is_being_rebound(void) 2863 { 2864 bool ret; 2865 2866 rcu_read_lock(); 2867 ret = task_cs(current) == cpuset_being_rebound; 2868 rcu_read_unlock(); 2869 2870 return ret; 2871 } 2872 2873 /* 2874 * cpuset_update_flag - read a 0 or a 1 in a file and update associated flag 2875 * bit: the bit to update (see cpuset_flagbits_t) 2876 * cs: the cpuset to update 2877 * turning_on: whether the flag is being set or cleared 2878 * 2879 * Call with cpuset_mutex held. 2880 */ 2881 2882 int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, 2883 int turning_on) 2884 { 2885 struct cpuset *trialcs; 2886 int balance_flag_changed; 2887 int spread_page_changed; 2888 int err; 2889 2890 trialcs = dup_or_alloc_cpuset(cs); 2891 if (!trialcs) 2892 return -ENOMEM; 2893 2894 if (turning_on) 2895 set_bit(bit, &trialcs->flags); 2896 else 2897 clear_bit(bit, &trialcs->flags); 2898 2899 err = validate_change(cs, trialcs); 2900 if (err < 0) 2901 goto out; 2902 2903 balance_flag_changed = (is_sched_load_balance(cs) != 2904 is_sched_load_balance(trialcs)); 2905 2906 spread_page_changed = is_spread_page(cs) != is_spread_page(trialcs); 2907 2908 spin_lock_irq(&callback_lock); 2909 cs->flags = trialcs->flags; 2910 spin_unlock_irq(&callback_lock); 2911 2912 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) { 2913 if (cpuset_v2()) 2914 cpuset_force_rebuild(); 2915 else 2916 rebuild_sched_domains_locked(); 2917 } 2918 2919 if (spread_page_changed) 2920 cpuset1_update_tasks_flags(cs); 2921 out: 2922 free_cpuset(trialcs); 2923 return err; 2924 } 2925 2926 /** 2927 * update_prstate - update partition_root_state 2928 * @cs: the cpuset to update 2929 * @new_prs: new partition root state 2930 * Return: 0 if successful, != 0 if error 2931 * 2932 * Call with cpuset_mutex held. 2933 */ 2934 static int update_prstate(struct cpuset *cs, int new_prs) 2935 { 2936 int err = PERR_NONE, old_prs = cs->partition_root_state; 2937 struct cpuset *parent = parent_cs(cs); 2938 struct tmpmasks tmpmask; 2939 bool isolcpus_updated = false; 2940 2941 if (old_prs == new_prs) 2942 return 0; 2943 2944 /* 2945 * Treat a previously invalid partition root as if it is a "member". 2946 */ 2947 if (new_prs && is_partition_invalid(cs)) 2948 old_prs = PRS_MEMBER; 2949 2950 if (alloc_tmpmasks(&tmpmask)) 2951 return -ENOMEM; 2952 2953 err = update_partition_exclusive_flag(cs, new_prs); 2954 if (err) 2955 goto out; 2956 2957 if (!old_prs) { 2958 /* 2959 * cpus_allowed and exclusive_cpus cannot be both empty. 2960 */ 2961 if (xcpus_empty(cs)) { 2962 err = PERR_CPUSEMPTY; 2963 goto out; 2964 } 2965 2966 /* 2967 * We don't support the creation of a new local partition with 2968 * a remote partition underneath it. This unsupported 2969 * setting can happen only if parent is the top_cpuset because 2970 * a remote partition cannot be created underneath an existing 2971 * local or remote partition. 2972 */ 2973 if ((parent == &top_cpuset) && 2974 cpumask_intersects(cs->exclusive_cpus, subpartitions_cpus)) { 2975 err = PERR_REMOTE; 2976 goto out; 2977 } 2978 2979 /* 2980 * If parent is valid partition, enable local partiion. 2981 * Otherwise, enable a remote partition. 2982 */ 2983 if (is_partition_valid(parent)) { 2984 enum partition_cmd cmd = (new_prs == PRS_ROOT) 2985 ? partcmd_enable : partcmd_enablei; 2986 2987 err = update_parent_effective_cpumask(cs, cmd, NULL, &tmpmask); 2988 } else { 2989 err = remote_partition_enable(cs, new_prs, &tmpmask); 2990 } 2991 } else if (old_prs && new_prs) { 2992 /* 2993 * A change in load balance state only, no change in cpumasks. 2994 * Need to update isolated_cpus. 2995 */ 2996 if (((new_prs == PRS_ISOLATED) && 2997 !isolated_cpus_can_update(cs->effective_xcpus, NULL)) || 2998 prstate_housekeeping_conflict(new_prs, cs->effective_xcpus)) 2999 err = PERR_HKEEPING; 3000 else 3001 isolcpus_updated = true; 3002 } else { 3003 /* 3004 * Switching back to member is always allowed even if it 3005 * disables child partitions. 3006 */ 3007 if (is_remote_partition(cs)) 3008 remote_partition_disable(cs, &tmpmask); 3009 else 3010 update_parent_effective_cpumask(cs, partcmd_disable, 3011 NULL, &tmpmask); 3012 3013 /* 3014 * Invalidation of child partitions will be done in 3015 * update_cpumasks_hier(). 3016 */ 3017 } 3018 out: 3019 /* 3020 * Make partition invalid & disable CS_CPU_EXCLUSIVE if an error 3021 * happens. 3022 */ 3023 if (err) { 3024 new_prs = -new_prs; 3025 update_partition_exclusive_flag(cs, new_prs); 3026 } 3027 3028 spin_lock_irq(&callback_lock); 3029 cs->partition_root_state = new_prs; 3030 WRITE_ONCE(cs->prs_err, err); 3031 if (!is_partition_valid(cs)) 3032 reset_partition_data(cs); 3033 else if (isolcpus_updated) 3034 isolated_cpus_update(old_prs, new_prs, cs->effective_xcpus); 3035 spin_unlock_irq(&callback_lock); 3036 3037 /* Force update if switching back to member & update effective_xcpus */ 3038 update_cpumasks_hier(cs, &tmpmask, !new_prs); 3039 3040 /* A newly created partition must have effective_xcpus set */ 3041 WARN_ON_ONCE(!old_prs && (new_prs > 0) 3042 && cpumask_empty(cs->effective_xcpus)); 3043 3044 /* Update sched domains and load balance flag */ 3045 update_partition_sd_lb(cs, old_prs); 3046 3047 notify_partition_change(cs, old_prs); 3048 if (force_sd_rebuild) 3049 rebuild_sched_domains_locked(); 3050 free_tmpmasks(&tmpmask); 3051 return 0; 3052 } 3053 3054 /* 3055 * Check to see if a cpuset can accept a new task 3056 * For v1, cpus_allowed and mems_allowed can't be empty. 3057 * For v2, effective_cpus can't be empty. 3058 * Note that in v1, effective_cpus = cpus_allowed. 3059 * 3060 * Also set the boolean flag passed in by @psetsched depending on if 3061 * security_task_setscheduler() call is needed and @oldcs is not NULL. 3062 */ 3063 static int cpuset_can_attach_check(struct cpuset *cs, struct cpuset *oldcs, 3064 bool *psetsched) 3065 { 3066 bool cpus_updated, mems_updated; 3067 3068 if (cpumask_empty(cs->effective_cpus) || 3069 (!is_in_v2_mode() && nodes_empty(cs->mems_allowed))) 3070 return -ENOSPC; 3071 3072 if (!oldcs) 3073 return 0; 3074 3075 if (!llist_on_list(&oldcs->attach_node)) 3076 llist_add(&oldcs->attach_node, &src_cs_head); 3077 3078 if (!llist_on_list(&cs->attach_node)) 3079 llist_add(&cs->attach_node, &dst_cs_head); 3080 3081 cpus_updated = !cpumask_equal(cs->effective_cpus, oldcs->effective_cpus); 3082 mems_updated = !nodes_equal(cs->effective_mems, oldcs->effective_mems); 3083 3084 if (cpus_updated) 3085 attach_ctx.cpus_updated = true; 3086 if (mems_updated) 3087 attach_ctx.mems_updated = true; 3088 3089 /* 3090 * Skip rights over task setsched check in v2 when nothing changes for 3091 * the current oldcs/cs pair, migration permission derives from 3092 * hierarchy ownership in cgroup_procs_write_permission()). 3093 */ 3094 *psetsched = !cpuset_v2() || cpus_updated || mems_updated; 3095 3096 /* 3097 * A v1 cpuset with tasks will have no CPU left only when CPU hotplug 3098 * brings the last online CPU offline as users are not allowed to empty 3099 * cpuset.cpus when there are active tasks inside. When that happens, 3100 * we should allow tasks to migrate out without security check to make 3101 * sure they will be able to run after migration. 3102 */ 3103 if (!is_in_v2_mode() && cpumask_empty(oldcs->effective_cpus)) 3104 *psetsched = false; 3105 3106 return 0; 3107 } 3108 3109 static int cpuset_reserve_dl_bw(void) 3110 { 3111 struct cpuset *cs; 3112 int cpu, ret; 3113 3114 llist_for_each_entry(cs, dst_cs_head.first, attach_node) { 3115 if (!cs->sum_migrate_dl_bw) 3116 continue; 3117 3118 cpu = cpumask_any_and(cpu_active_mask, cs->effective_cpus); 3119 if (unlikely(cpu >= nr_cpu_ids)) 3120 return -EINVAL; 3121 3122 ret = dl_bw_alloc(cpu, cs->sum_migrate_dl_bw); 3123 if (ret) 3124 return ret; 3125 3126 cs->dl_bw_cpu = cpu; 3127 } 3128 return 0; 3129 } 3130 3131 /* 3132 * Clear and optionally apply (@cancel is false) the attach related data in the 3133 * source or destination cpuset. 3134 */ 3135 static void clear_attach_data(struct llist_head *head, bool cancel) 3136 { 3137 struct cpuset *cs, *next; 3138 struct llist_node *lnode = __llist_del_all(head); 3139 3140 llist_for_each_entry_safe(cs, next, lnode, attach_node) { 3141 init_llist_node(&cs->attach_node); 3142 if (cs->nr_migrate_dl_tasks) { 3143 if (!cancel) 3144 atomic_add(cs->nr_migrate_dl_tasks, &cs->nr_deadline_tasks); 3145 else if (cs->dl_bw_cpu >= 0) /* && cancel */ 3146 dl_bw_free(cs->dl_bw_cpu, cs->sum_migrate_dl_bw); 3147 cs->nr_migrate_dl_tasks = 0; 3148 cs->sum_migrate_dl_bw = 0; 3149 cs->dl_bw_cpu = -1; 3150 } 3151 } 3152 } 3153 3154 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */ 3155 static int cpuset_can_attach(struct cgroup_taskset *tset) 3156 { 3157 struct cgroup_subsys_state *css; 3158 struct cpuset *cs, *oldcs; 3159 struct task_struct *task; 3160 bool setsched_check; 3161 int ret; 3162 3163 cs = oldcs = NULL; 3164 mutex_lock(&cpuset_mutex); 3165 attach_ctx.old_cs = NULL; /* Used later in cpuset_attach_task() */ 3166 attach_ctx.cpus_updated = false; 3167 attach_ctx.mems_updated = false; 3168 attach_ctx.many_dest_cs = false; 3169 3170 /* 3171 * The attach_ctx.old_cs is used mainly by cpuset_migrate_mm() to get 3172 * the old_mems_allowed value. There are two ways that many-to-one 3173 * cpuset migration can happen: 3174 * 1) A multithread application with threads in different cpusets is 3175 * wholely migrated to a new cpuset. 3176 * 2) Disabling v2 cpuset controller will move all the tasks in child 3177 * cpusets to the parent cpuset. 3178 * 3179 * In the former case, it is the mm setting of the group leader that 3180 * really matters. So attach_ctx.old_cs should track the oldcs of the 3181 * group leader. It falls back to the oldcs of the first task if there 3182 * is no group leader in the taskset. In the latter case, effective_mems 3183 * of child cpusets must always be a subset of the parent. So no real 3184 * page migration will be necessary no matter which child cpuset is 3185 * selected as attach_ctx.old_cs. 3186 * 3187 * For a v2 threaded subtree where cpuset isn't enabled in some of the 3188 * cgroups, it is possible that oldcs == cs for some of the tasks. 3189 * In this case, we can skip checking on those tasks as there is no 3190 * actual migration wrt cpuset. 3191 */ 3192 cgroup_taskset_for_each(task, css, tset) { 3193 struct cpuset *new_cs = css_cs(css); 3194 struct cpuset *new_oldcs = task_cs(task); 3195 3196 if ((new_oldcs != oldcs) || (new_cs != cs)) { 3197 if (cs && (new_cs != cs)) 3198 attach_ctx.many_dest_cs = true; 3199 cs = new_cs; 3200 oldcs = new_oldcs; 3201 if (oldcs == cs) 3202 continue; 3203 if (!attach_ctx.old_cs) 3204 attach_ctx.old_cs = oldcs; 3205 ret = cpuset_can_attach_check(cs, oldcs, &setsched_check); 3206 if (ret) 3207 goto out_unlock; 3208 } 3209 3210 if (oldcs == cs) 3211 continue; 3212 3213 ret = task_can_attach(task); 3214 if (ret) 3215 goto out_unlock; 3216 3217 /* Update attach_ctx.old_cs to the latest group leader */ 3218 if (task == task->group_leader) 3219 attach_ctx.old_cs = task_cs(task); 3220 3221 if (setsched_check) { 3222 ret = security_task_setscheduler(task); 3223 if (ret) 3224 goto out_unlock; 3225 } 3226 3227 if (dl_task(task)) { 3228 /* 3229 * Count all migrating DL tasks for cpuset task accounting. 3230 * Only tasks that need a root-domain bandwidth move 3231 * contribute to sum_migrate_dl_bw. 3232 */ 3233 cs->nr_migrate_dl_tasks++; 3234 oldcs->nr_migrate_dl_tasks--; 3235 if (dl_task_needs_bw_move(task, cs->effective_cpus)) 3236 cs->sum_migrate_dl_bw += task->dl.dl_bw; 3237 } 3238 } 3239 3240 /* 3241 * The only case where there are multiple destination cpusets for 3242 * task migration is when enabling a v2 cpuset controllers where 3243 * tasks will be migrated to multiple child cpusets from a parent 3244 * cpuset with the same effective CPUs and memory nodes. IOW, 3245 * both attach_cpus_updated and attach_mems_updated should be false. 3246 * If not, it is a condition that the current code cannot handle. 3247 * Print a warning and abort the attach operation as further code 3248 * change may be needed. 3249 */ 3250 if (WARN_ON_ONCE(attach_ctx.many_dest_cs && (!cpuset_v2() || 3251 attach_ctx.cpus_updated || attach_ctx.mems_updated))) { 3252 ret = -EINVAL; 3253 goto out_unlock; 3254 } 3255 3256 ret = cpuset_reserve_dl_bw(); 3257 3258 out_unlock: 3259 if (ret) { 3260 clear_attach_data(&src_cs_head, true); 3261 clear_attach_data(&dst_cs_head, true); 3262 } else { 3263 attach_ctx.in_progress++; 3264 } 3265 3266 mutex_unlock(&cpuset_mutex); 3267 return ret; 3268 } 3269 3270 static void cpuset_cancel_attach(struct cgroup_taskset *tset) 3271 { 3272 mutex_lock(&cpuset_mutex); 3273 dec_attach_in_progress_locked(); 3274 clear_attach_data(&src_cs_head, true); 3275 clear_attach_data(&dst_cs_head, true); 3276 mutex_unlock(&cpuset_mutex); 3277 } 3278 3279 /* 3280 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach_task() 3281 * but we can't allocate it dynamically there. Define it global and 3282 * allocate from cpuset_init(). 3283 */ 3284 static cpumask_var_t cpus_attach; 3285 3286 static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task) 3287 { 3288 struct mm_struct *mm; 3289 3290 lockdep_assert_cpuset_lock_held(); 3291 3292 if (cs != &top_cpuset) 3293 guarantee_active_cpus(task, cpus_attach); 3294 else 3295 cpumask_andnot(cpus_attach, task_cpu_possible_mask(task), 3296 subpartitions_cpus); 3297 /* 3298 * can_attach beforehand should guarantee that this doesn't 3299 * fail. TODO: have a better way to handle failure here 3300 */ 3301 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); 3302 3303 if (cpuset_v2() && !attach_ctx.mems_updated) 3304 return; 3305 3306 cpuset_change_task_nodemask(task, &attach_ctx.nodemask_to); 3307 cpuset1_update_task_spread_flags(cs, task); 3308 3309 if ((task != task->group_leader) || !attach_ctx.mems_updated) 3310 return; 3311 3312 /* 3313 * Change mm for threadgroup leader. This is expensive and may 3314 * sleep and should be moved outside migration path proper. 3315 */ 3316 mm = get_task_mm(task); 3317 if (mm) { 3318 struct cpuset *oldcs = attach_ctx.old_cs; 3319 3320 mpol_rebind_mm(mm, &cs->effective_mems); 3321 3322 /* 3323 * old_mems_allowed is the same with mems_allowed 3324 * here, except if this task is being moved 3325 * automatically due to hotplug. In that case 3326 * @mems_allowed has been updated and is empty, so 3327 * @old_mems_allowed is the right nodesets that we 3328 * migrate mm from. 3329 */ 3330 if (is_memory_migrate(cs)) { 3331 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed, 3332 &attach_ctx.nodemask_to); 3333 attach_ctx.task_work_queued = true; 3334 } else { 3335 mmput(mm); 3336 } 3337 } 3338 } 3339 3340 static void cpuset_attach(struct cgroup_taskset *tset) 3341 { 3342 struct task_struct *task; 3343 struct cgroup_subsys_state *css; 3344 struct cpuset *cs; 3345 3346 cgroup_taskset_first(tset, &css); 3347 cs = css_cs(css); 3348 3349 lockdep_assert_cpus_held(); /* see cgroup_attach_lock() */ 3350 mutex_lock(&cpuset_mutex); 3351 attach_ctx.task_work_queued = false; 3352 guarantee_online_mems(cs, &attach_ctx.nodemask_to); 3353 3354 /* 3355 * attach_ctx.old_cs can only be NULL if no task is actually migrating. 3356 * This is highly unlikely. If it happens at all, we can skip task 3357 * iteration and setting old_mems_allowed. 3358 */ 3359 if (unlikely(!attach_ctx.old_cs)) 3360 goto out; 3361 3362 /* 3363 * In the default hierarchy, enabling cpuset in the child cgroups 3364 * will trigger a cpuset_attach() call with no change in effective cpus 3365 * and mems. In that case, we can optimize out by skipping the task 3366 * iteration and the destination cpuset list is iterated to set 3367 * old_mems_allowed. 3368 */ 3369 if (cpuset_v2() && !attach_ctx.cpus_updated && !attach_ctx.mems_updated) { 3370 llist_for_each_entry(cs, dst_cs_head.first, attach_node) 3371 cs->old_mems_allowed = attach_ctx.nodemask_to; 3372 goto out; 3373 } 3374 3375 cgroup_taskset_for_each(task, css, tset) 3376 cpuset_attach_task(cs, task); 3377 3378 if (attach_ctx.task_work_queued) 3379 schedule_flush_migrate_mm(); 3380 cs->old_mems_allowed = attach_ctx.nodemask_to; 3381 out: 3382 clear_attach_data(&src_cs_head, false); 3383 clear_attach_data(&dst_cs_head, false); 3384 dec_attach_in_progress_locked(); 3385 3386 mutex_unlock(&cpuset_mutex); 3387 } 3388 3389 /* 3390 * Common handling for a write to a "cpus" or "mems" file. 3391 */ 3392 ssize_t cpuset_write_resmask(struct kernfs_open_file *of, 3393 char *buf, size_t nbytes, loff_t off) 3394 { 3395 struct cpuset *cs = css_cs(of_css(of)); 3396 struct cpuset *trialcs; 3397 int retval = -ENODEV; 3398 3399 /* root is read-only */ 3400 if (cs == &top_cpuset) 3401 return -EACCES; 3402 3403 buf = strstrip(buf); 3404 3405 /* cpuset_mutex acquired in wait_attach_done_lock() */ 3406 mutex_lock(&cpuset_top_mutex); 3407 cpus_read_lock(); 3408 wait_attach_done_lock(); 3409 3410 if (!is_cpuset_online(cs)) 3411 goto out_unlock; 3412 3413 trialcs = dup_or_alloc_cpuset(cs); 3414 if (!trialcs) { 3415 retval = -ENOMEM; 3416 goto out_unlock; 3417 } 3418 3419 switch (of_cft(of)->private) { 3420 case FILE_CPULIST: 3421 retval = update_cpumask(cs, trialcs, buf); 3422 break; 3423 case FILE_EXCLUSIVE_CPULIST: 3424 retval = update_exclusive_cpumask(cs, trialcs, buf); 3425 break; 3426 case FILE_MEMLIST: 3427 retval = update_nodemask(cs, trialcs, buf); 3428 break; 3429 default: 3430 retval = -EINVAL; 3431 break; 3432 } 3433 3434 free_cpuset(trialcs); 3435 out_unlock: 3436 cpuset_update_sd_hk_unlock(); 3437 if (of_cft(of)->private == FILE_MEMLIST) 3438 schedule_flush_migrate_mm(); 3439 return retval ?: nbytes; 3440 } 3441 3442 /* 3443 * These ascii lists should be read in a single call, by using a user 3444 * buffer large enough to hold the entire map. If read in smaller 3445 * chunks, there is no guarantee of atomicity. Since the display format 3446 * used, list of ranges of sequential numbers, is variable length, 3447 * and since these maps can change value dynamically, one could read 3448 * gibberish by doing partial reads while a list was changing. 3449 */ 3450 int cpuset_common_seq_show(struct seq_file *sf, void *v) 3451 { 3452 struct cpuset *cs = css_cs(seq_css(sf)); 3453 cpuset_filetype_t type = seq_cft(sf)->private; 3454 int ret = 0; 3455 3456 spin_lock_irq(&callback_lock); 3457 3458 switch (type) { 3459 case FILE_CPULIST: 3460 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed)); 3461 break; 3462 case FILE_MEMLIST: 3463 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed)); 3464 break; 3465 case FILE_EFFECTIVE_CPULIST: 3466 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus)); 3467 break; 3468 case FILE_EFFECTIVE_MEMLIST: 3469 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems)); 3470 break; 3471 case FILE_EXCLUSIVE_CPULIST: 3472 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->exclusive_cpus)); 3473 break; 3474 case FILE_EFFECTIVE_XCPULIST: 3475 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_xcpus)); 3476 break; 3477 case FILE_SUBPARTS_CPULIST: 3478 seq_printf(sf, "%*pbl\n", cpumask_pr_args(subpartitions_cpus)); 3479 break; 3480 case FILE_ISOLATED_CPULIST: 3481 seq_printf(sf, "%*pbl\n", cpumask_pr_args(isolated_cpus)); 3482 break; 3483 default: 3484 ret = -EINVAL; 3485 } 3486 3487 spin_unlock_irq(&callback_lock); 3488 return ret; 3489 } 3490 3491 static int cpuset_partition_show(struct seq_file *seq, void *v) 3492 { 3493 struct cpuset *cs = css_cs(seq_css(seq)); 3494 const char *err, *type = NULL; 3495 3496 switch (cs->partition_root_state) { 3497 case PRS_ROOT: 3498 seq_puts(seq, "root\n"); 3499 break; 3500 case PRS_ISOLATED: 3501 seq_puts(seq, "isolated\n"); 3502 break; 3503 case PRS_MEMBER: 3504 seq_puts(seq, "member\n"); 3505 break; 3506 case PRS_INVALID_ROOT: 3507 type = "root"; 3508 fallthrough; 3509 case PRS_INVALID_ISOLATED: 3510 if (!type) 3511 type = "isolated"; 3512 err = perr_strings[READ_ONCE(cs->prs_err)]; 3513 if (err) 3514 seq_printf(seq, "%s invalid (%s)\n", type, err); 3515 else 3516 seq_printf(seq, "%s invalid\n", type); 3517 break; 3518 } 3519 return 0; 3520 } 3521 3522 static ssize_t cpuset_partition_write(struct kernfs_open_file *of, char *buf, 3523 size_t nbytes, loff_t off) 3524 { 3525 struct cpuset *cs = css_cs(of_css(of)); 3526 int val; 3527 int retval = -ENODEV; 3528 3529 buf = strstrip(buf); 3530 3531 if (!strcmp(buf, "root")) 3532 val = PRS_ROOT; 3533 else if (!strcmp(buf, "member")) 3534 val = PRS_MEMBER; 3535 else if (!strcmp(buf, "isolated")) 3536 val = PRS_ISOLATED; 3537 else 3538 return -EINVAL; 3539 3540 mutex_lock(&cpuset_top_mutex); 3541 cpus_read_lock(); 3542 wait_attach_done_lock(); 3543 3544 if (is_cpuset_online(cs)) 3545 retval = update_prstate(cs, val); 3546 cpuset_update_sd_hk_unlock(); 3547 return retval ?: nbytes; 3548 } 3549 3550 /* 3551 * This is currently a minimal set for the default hierarchy. It can be 3552 * expanded later on by migrating more features and control files from v1. 3553 */ 3554 static struct cftype dfl_files[] = { 3555 { 3556 .name = "cpus", 3557 .seq_show = cpuset_common_seq_show, 3558 .write = cpuset_write_resmask, 3559 .max_write_len = (100U + 6 * NR_CPUS), 3560 .private = FILE_CPULIST, 3561 .flags = CFTYPE_NOT_ON_ROOT, 3562 }, 3563 3564 { 3565 .name = "mems", 3566 .seq_show = cpuset_common_seq_show, 3567 .write = cpuset_write_resmask, 3568 .max_write_len = (100U + 6 * MAX_NUMNODES), 3569 .private = FILE_MEMLIST, 3570 .flags = CFTYPE_NOT_ON_ROOT, 3571 }, 3572 3573 { 3574 .name = "cpus.effective", 3575 .seq_show = cpuset_common_seq_show, 3576 .private = FILE_EFFECTIVE_CPULIST, 3577 }, 3578 3579 { 3580 .name = "mems.effective", 3581 .seq_show = cpuset_common_seq_show, 3582 .private = FILE_EFFECTIVE_MEMLIST, 3583 }, 3584 3585 { 3586 .name = "cpus.partition", 3587 .seq_show = cpuset_partition_show, 3588 .write = cpuset_partition_write, 3589 .private = FILE_PARTITION_ROOT, 3590 .flags = CFTYPE_NOT_ON_ROOT, 3591 .file_offset = offsetof(struct cpuset, partition_file), 3592 }, 3593 3594 { 3595 .name = "cpus.exclusive", 3596 .seq_show = cpuset_common_seq_show, 3597 .write = cpuset_write_resmask, 3598 .max_write_len = (100U + 6 * NR_CPUS), 3599 .private = FILE_EXCLUSIVE_CPULIST, 3600 .flags = CFTYPE_NOT_ON_ROOT, 3601 }, 3602 3603 { 3604 .name = "cpus.exclusive.effective", 3605 .seq_show = cpuset_common_seq_show, 3606 .private = FILE_EFFECTIVE_XCPULIST, 3607 .flags = CFTYPE_NOT_ON_ROOT, 3608 }, 3609 3610 { 3611 .name = "cpus.subpartitions", 3612 .seq_show = cpuset_common_seq_show, 3613 .private = FILE_SUBPARTS_CPULIST, 3614 .flags = CFTYPE_ONLY_ON_ROOT | CFTYPE_DEBUG, 3615 }, 3616 3617 { 3618 .name = "cpus.isolated", 3619 .seq_show = cpuset_common_seq_show, 3620 .private = FILE_ISOLATED_CPULIST, 3621 .flags = CFTYPE_ONLY_ON_ROOT, 3622 }, 3623 3624 { } /* terminate */ 3625 }; 3626 3627 3628 /** 3629 * cpuset_css_alloc - Allocate a cpuset css 3630 * @parent_css: Parent css of the control group that the new cpuset will be 3631 * part of 3632 * Return: cpuset css on success, -ENOMEM on failure. 3633 * 3634 * Allocate and initialize a new cpuset css, for non-NULL @parent_css, return 3635 * top cpuset css otherwise. 3636 */ 3637 static struct cgroup_subsys_state * 3638 cpuset_css_alloc(struct cgroup_subsys_state *parent_css) 3639 { 3640 struct cpuset *cs; 3641 3642 if (!parent_css) 3643 return &top_cpuset.css; 3644 3645 cs = dup_or_alloc_cpuset(NULL); 3646 if (!cs) 3647 return ERR_PTR(-ENOMEM); 3648 3649 __set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 3650 cpuset1_init(cs); 3651 3652 /* Set CS_MEMORY_MIGRATE for default hierarchy */ 3653 if (cpuset_v2()) 3654 __set_bit(CS_MEMORY_MIGRATE, &cs->flags); 3655 3656 return &cs->css; 3657 } 3658 3659 static int cpuset_css_online(struct cgroup_subsys_state *css) 3660 { 3661 struct cpuset *cs = css_cs(css); 3662 struct cpuset *parent = parent_cs(cs); 3663 3664 if (!parent) 3665 return 0; 3666 3667 cpuset_full_lock(); 3668 /* 3669 * For v2, clear CS_SCHED_LOAD_BALANCE if parent is isolated 3670 */ 3671 if (cpuset_v2() && !is_sched_load_balance(parent)) 3672 clear_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); 3673 3674 cpuset_inc(); 3675 3676 spin_lock_irq(&callback_lock); 3677 if (is_in_v2_mode()) { 3678 cpumask_copy(cs->effective_cpus, parent->effective_cpus); 3679 cs->effective_mems = parent->effective_mems; 3680 } 3681 spin_unlock_irq(&callback_lock); 3682 cpuset1_online_css(css); 3683 3684 cpuset_full_unlock(); 3685 return 0; 3686 } 3687 3688 /* 3689 * If the cpuset being removed has its flag 'sched_load_balance' 3690 * enabled, then simulate turning sched_load_balance off, which 3691 * will call rebuild_sched_domains_locked(). That is not needed 3692 * in the default hierarchy where only changes in partition 3693 * will cause repartitioning. 3694 */ 3695 static void cpuset_css_offline(struct cgroup_subsys_state *css) 3696 { 3697 struct cpuset *cs = css_cs(css); 3698 3699 cpuset_full_lock(); 3700 if (!cpuset_v2() && is_sched_load_balance(cs)) 3701 cpuset_update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); 3702 3703 cpuset_dec(); 3704 cpuset_full_unlock(); 3705 } 3706 3707 /* 3708 * If a dying cpuset has the 'cpus.partition' enabled, turn it off by 3709 * changing it back to member to free its exclusive CPUs back to the pool to 3710 * be used by other online cpusets. 3711 */ 3712 static void cpuset_css_killed(struct cgroup_subsys_state *css) 3713 { 3714 struct cpuset *cs = css_cs(css); 3715 3716 cpuset_full_lock(); 3717 /* Reset valid partition back to member */ 3718 if (is_partition_valid(cs)) 3719 update_prstate(cs, PRS_MEMBER); 3720 cpuset_update_sd_hk_unlock(); 3721 } 3722 3723 static void cpuset_css_free(struct cgroup_subsys_state *css) 3724 { 3725 struct cpuset *cs = css_cs(css); 3726 3727 free_cpuset(cs); 3728 } 3729 3730 static void cpuset_bind(struct cgroup_subsys_state *root_css) 3731 { 3732 mutex_lock(&cpuset_mutex); 3733 spin_lock_irq(&callback_lock); 3734 3735 if (is_in_v2_mode()) { 3736 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask); 3737 cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask); 3738 top_cpuset.mems_allowed = node_possible_map; 3739 } else { 3740 cpumask_copy(top_cpuset.cpus_allowed, 3741 top_cpuset.effective_cpus); 3742 top_cpuset.mems_allowed = top_cpuset.effective_mems; 3743 } 3744 3745 spin_unlock_irq(&callback_lock); 3746 mutex_unlock(&cpuset_mutex); 3747 } 3748 3749 /* 3750 * In case the child is cloned into a cpuset different from its parent, 3751 * additional checks are done to see if the move is allowed. 3752 */ 3753 static int cpuset_can_fork(struct task_struct *task, struct css_set *cset) 3754 { 3755 struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]); 3756 bool same_cs; 3757 int ret; 3758 3759 rcu_read_lock(); 3760 same_cs = (cs == task_cs(current)); 3761 rcu_read_unlock(); 3762 3763 if (same_cs) 3764 return 0; 3765 3766 lockdep_assert_held(&cgroup_mutex); 3767 mutex_lock(&cpuset_mutex); 3768 3769 /* Check to see if task is allowed in the cpuset */ 3770 ret = cpuset_can_attach_check(cs, NULL, NULL); 3771 if (ret) 3772 goto out_unlock; 3773 3774 ret = task_can_attach(task); 3775 if (ret) 3776 goto out_unlock; 3777 3778 ret = security_task_setscheduler(task); 3779 if (ret) 3780 goto out_unlock; 3781 3782 attach_ctx.in_progress++; 3783 out_unlock: 3784 mutex_unlock(&cpuset_mutex); 3785 return ret; 3786 } 3787 3788 static void cpuset_cancel_fork(struct task_struct *task, struct css_set *cset) 3789 { 3790 struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]); 3791 bool same_cs; 3792 3793 rcu_read_lock(); 3794 same_cs = (cs == task_cs(current)); 3795 rcu_read_unlock(); 3796 3797 if (same_cs) 3798 return; 3799 3800 dec_attach_in_progress(); 3801 } 3802 3803 /* 3804 * Make sure the new task conform to the current state of its parent, 3805 * which could have been changed by cpuset just after it inherits the 3806 * state from the parent and before it sits on the cgroup's task list. 3807 */ 3808 static void cpuset_fork(struct task_struct *task) 3809 { 3810 struct cpuset *cs, *oldcs; 3811 3812 rcu_read_lock(); 3813 cs = task_cs(task); 3814 oldcs = task_cs(current); 3815 rcu_read_unlock(); 3816 3817 if (cs == oldcs) { 3818 if (cs == &top_cpuset) 3819 return; 3820 3821 set_cpus_allowed_ptr(task, current->cpus_ptr); 3822 task->mems_allowed = current->mems_allowed; 3823 return; 3824 } 3825 3826 /* CLONE_INTO_CGROUP */ 3827 mutex_lock(&cpuset_mutex); 3828 guarantee_online_mems(cs, &attach_ctx.nodemask_to); 3829 cs->old_mems_allowed = attach_ctx.nodemask_to; 3830 3831 /* 3832 * Assume CPUs and memory nodes are updated 3833 * A CLONE_INTO_CGROUP operation should have taken the cgroup mutex 3834 * and so there shouldn't be a competing cpuset_attach() operation. 3835 */ 3836 attach_ctx.cpus_updated = attach_ctx.mems_updated = true; 3837 attach_ctx.task_work_queued = false; 3838 attach_ctx.old_cs = oldcs; 3839 cpuset_attach_task(cs, task); 3840 if (attach_ctx.task_work_queued) 3841 schedule_flush_migrate_mm(); 3842 3843 dec_attach_in_progress_locked(); 3844 mutex_unlock(&cpuset_mutex); 3845 } 3846 3847 struct cgroup_subsys cpuset_cgrp_subsys = { 3848 .css_alloc = cpuset_css_alloc, 3849 .css_online = cpuset_css_online, 3850 .css_offline = cpuset_css_offline, 3851 .css_killed = cpuset_css_killed, 3852 .css_free = cpuset_css_free, 3853 .can_attach = cpuset_can_attach, 3854 .cancel_attach = cpuset_cancel_attach, 3855 .attach = cpuset_attach, 3856 .bind = cpuset_bind, 3857 .can_fork = cpuset_can_fork, 3858 .cancel_fork = cpuset_cancel_fork, 3859 .fork = cpuset_fork, 3860 #ifdef CONFIG_CPUSETS_V1 3861 .legacy_cftypes = cpuset1_files, 3862 #endif 3863 .dfl_cftypes = dfl_files, 3864 .early_init = true, 3865 .threaded = true, 3866 }; 3867 3868 /** 3869 * cpuset_init - initialize cpusets at system boot 3870 * 3871 * Description: Initialize top_cpuset 3872 **/ 3873 3874 int __init cpuset_init(void) 3875 { 3876 BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)); 3877 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL)); 3878 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_xcpus, GFP_KERNEL)); 3879 BUG_ON(!alloc_cpumask_var(&top_cpuset.exclusive_cpus, GFP_KERNEL)); 3880 BUG_ON(!zalloc_cpumask_var(&subpartitions_cpus, GFP_KERNEL)); 3881 BUG_ON(!zalloc_cpumask_var(&isolated_cpus, GFP_KERNEL)); 3882 BUG_ON(!zalloc_cpumask_var(&isolated_hk_cpus, GFP_KERNEL)); 3883 3884 cpumask_setall(top_cpuset.cpus_allowed); 3885 nodes_setall(top_cpuset.mems_allowed); 3886 cpumask_setall(top_cpuset.effective_cpus); 3887 cpumask_setall(top_cpuset.effective_xcpus); 3888 cpumask_setall(top_cpuset.exclusive_cpus); 3889 nodes_setall(top_cpuset.effective_mems); 3890 init_llist_node(&top_cpuset.attach_node); 3891 3892 cpuset1_init(&top_cpuset); 3893 3894 BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)); 3895 3896 if (housekeeping_enabled(HK_TYPE_DOMAIN_BOOT)) 3897 cpumask_andnot(isolated_cpus, cpu_possible_mask, 3898 housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)); 3899 3900 return 0; 3901 } 3902 3903 static void 3904 hotplug_update_tasks(struct cpuset *cs, 3905 struct cpumask *new_cpus, nodemask_t *new_mems, 3906 bool cpus_updated, bool mems_updated) 3907 { 3908 /* A partition root is allowed to have empty effective cpus */ 3909 if (cpumask_empty(new_cpus) && !is_partition_valid(cs)) 3910 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus); 3911 if (nodes_empty(*new_mems)) 3912 *new_mems = parent_cs(cs)->effective_mems; 3913 3914 spin_lock_irq(&callback_lock); 3915 cpumask_copy(cs->effective_cpus, new_cpus); 3916 cs->effective_mems = *new_mems; 3917 spin_unlock_irq(&callback_lock); 3918 3919 if (cpus_updated) 3920 cpuset_update_tasks_cpumask(cs, new_cpus); 3921 if (mems_updated) 3922 cpuset_update_tasks_nodemask(cs); 3923 } 3924 3925 void cpuset_force_rebuild(void) 3926 { 3927 force_sd_rebuild = true; 3928 } 3929 3930 /** 3931 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug 3932 * @cs: cpuset in interest 3933 * @tmp: the tmpmasks structure pointer 3934 * 3935 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone 3936 * offline, update @cs accordingly. If @cs ends up with no CPU or memory, 3937 * all its tasks are moved to the nearest ancestor with both resources. 3938 */ 3939 static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) 3940 { 3941 static cpumask_t new_cpus; 3942 static nodemask_t new_mems; 3943 bool cpus_updated; 3944 bool mems_updated; 3945 bool remote; 3946 int partcmd = -1; 3947 struct cpuset *parent; 3948 3949 wait_attach_done_lock(); 3950 parent = parent_cs(cs); 3951 compute_effective_cpumask(&new_cpus, cs, parent); 3952 compute_effective_nodemask(&new_mems, cs, parent); 3953 3954 if (!tmp || !cs->partition_root_state) 3955 goto update_tasks; 3956 3957 /* 3958 * Compute effective_cpus for valid partition root, may invalidate 3959 * child partition roots if necessary. 3960 */ 3961 remote = is_remote_partition(cs); 3962 if (remote || (is_partition_valid(cs) && is_partition_valid(parent))) 3963 compute_partition_effective_cpumask(cs, &new_cpus); 3964 3965 if (remote && (cpumask_empty(subpartitions_cpus) || 3966 (cpumask_empty(&new_cpus) && 3967 partition_is_populated(cs, NULL)))) { 3968 WRITE_ONCE(cs->prs_err, PERR_HOTPLUG); 3969 remote_partition_disable(cs, tmp); 3970 compute_effective_cpumask(&new_cpus, cs, parent); 3971 remote = false; 3972 } 3973 3974 /* 3975 * Force the partition to become invalid if either one of 3976 * the following conditions hold: 3977 * 1) empty effective cpus but not valid empty partition. 3978 * 2) parent is invalid or doesn't grant any cpus to child 3979 * partitions. 3980 * 3) subpartitions_cpus is empty. 3981 */ 3982 if (is_local_partition(cs) && 3983 (!is_partition_valid(parent) || 3984 tasks_nocpu_error(parent, cs, &new_cpus) || 3985 cpumask_empty(subpartitions_cpus))) 3986 partcmd = partcmd_invalidate; 3987 /* 3988 * On the other hand, an invalid partition root may be transitioned 3989 * back to a regular one with a non-empty effective xcpus. 3990 */ 3991 else if (is_partition_valid(parent) && is_partition_invalid(cs) && 3992 !cpumask_empty(cs->effective_xcpus)) 3993 partcmd = partcmd_update; 3994 3995 if (partcmd >= 0) { 3996 update_parent_effective_cpumask(cs, partcmd, NULL, tmp); 3997 if ((partcmd == partcmd_invalidate) || is_partition_valid(cs)) { 3998 compute_partition_effective_cpumask(cs, &new_cpus); 3999 cpuset_force_rebuild(); 4000 } 4001 } 4002 4003 update_tasks: 4004 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus); 4005 mems_updated = !nodes_equal(new_mems, cs->effective_mems); 4006 if (!cpus_updated && !mems_updated) 4007 goto unlock; /* Hotplug doesn't affect this cpuset */ 4008 4009 if (mems_updated) 4010 check_insane_mems_config(&new_mems); 4011 4012 if (is_in_v2_mode()) 4013 hotplug_update_tasks(cs, &new_cpus, &new_mems, 4014 cpus_updated, mems_updated); 4015 else 4016 cpuset1_hotplug_update_tasks(cs, &new_cpus, &new_mems, 4017 cpus_updated, mems_updated); 4018 4019 unlock: 4020 mutex_unlock(&cpuset_mutex); 4021 } 4022 4023 /** 4024 * cpuset_handle_hotplug - handle CPU/memory hot{,un}plug for a cpuset 4025 * 4026 * This function is called after either CPU or memory configuration has 4027 * changed and updates cpuset accordingly. The top_cpuset is always 4028 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in 4029 * order to make cpusets transparent (of no affect) on systems that are 4030 * actively using CPU hotplug but making no active use of cpusets. 4031 * 4032 * Non-root cpusets are only affected by offlining. If any CPUs or memory 4033 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on 4034 * all descendants. 4035 * 4036 * Note that CPU offlining during suspend is ignored. We don't modify 4037 * cpusets across suspend/resume cycles at all. 4038 * 4039 * CPU / memory hotplug is handled synchronously. 4040 */ 4041 static void cpuset_handle_hotplug(void) 4042 { 4043 static DECLARE_WORK(hk_sd_work, hk_sd_workfn); 4044 static cpumask_t new_cpus; 4045 static nodemask_t new_mems; 4046 bool cpus_updated, mems_updated; 4047 bool on_dfl = is_in_v2_mode(); 4048 struct tmpmasks tmp, *ptmp = NULL; 4049 4050 if (on_dfl && !alloc_tmpmasks(&tmp)) 4051 ptmp = &tmp; 4052 4053 lockdep_assert_cpus_held(); 4054 mutex_lock(&cpuset_mutex); 4055 4056 /* fetch the available cpus/mems and find out which changed how */ 4057 cpumask_copy(&new_cpus, cpu_active_mask); 4058 new_mems = node_states[N_MEMORY]; 4059 4060 /* 4061 * If subpartitions_cpus is populated, it is likely that the check 4062 * below will produce a false positive on cpus_updated when the cpu 4063 * list isn't changed. It is extra work, but it is better to be safe. 4064 */ 4065 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus) || 4066 !cpumask_empty(subpartitions_cpus); 4067 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems); 4068 4069 /* For v1, synchronize cpus_allowed to cpu_active_mask */ 4070 if (cpus_updated) { 4071 cpuset_force_rebuild(); 4072 spin_lock_irq(&callback_lock); 4073 if (!on_dfl) 4074 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); 4075 /* 4076 * Make sure that CPUs allocated to child partitions 4077 * do not show up in effective_cpus. If no CPU is left, 4078 * we clear the subpartitions_cpus & let the child partitions 4079 * fight for the CPUs again. 4080 */ 4081 if (!cpumask_empty(subpartitions_cpus)) { 4082 if (cpumask_subset(&new_cpus, subpartitions_cpus)) { 4083 cpumask_clear(subpartitions_cpus); 4084 } else { 4085 cpumask_andnot(&new_cpus, &new_cpus, 4086 subpartitions_cpus); 4087 } 4088 } 4089 cpumask_copy(top_cpuset.effective_cpus, &new_cpus); 4090 spin_unlock_irq(&callback_lock); 4091 /* we don't mess with cpumasks of tasks in top_cpuset */ 4092 } 4093 4094 /* synchronize mems_allowed to N_MEMORY */ 4095 if (mems_updated) { 4096 spin_lock_irq(&callback_lock); 4097 if (!on_dfl) 4098 top_cpuset.mems_allowed = new_mems; 4099 top_cpuset.effective_mems = new_mems; 4100 spin_unlock_irq(&callback_lock); 4101 cpuset_update_tasks_nodemask(&top_cpuset); 4102 } 4103 4104 mutex_unlock(&cpuset_mutex); 4105 4106 /* if cpus or mems changed, we need to propagate to descendants */ 4107 if (cpus_updated || mems_updated) { 4108 struct cpuset *cs; 4109 struct cgroup_subsys_state *pos_css; 4110 4111 rcu_read_lock(); 4112 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { 4113 if (cs == &top_cpuset || !css_tryget_online(&cs->css)) 4114 continue; 4115 rcu_read_unlock(); 4116 4117 cpuset_hotplug_update_tasks(cs, ptmp); 4118 4119 rcu_read_lock(); 4120 css_put(&cs->css); 4121 } 4122 rcu_read_unlock(); 4123 } 4124 4125 /* 4126 * rebuild_sched_domains() will always be called directly if needed 4127 * to make sure that newly added or removed CPU will be reflected in 4128 * the sched domains. However, if isolated partition invalidation 4129 * or recreation is being done (update_housekeeping set), a work item 4130 * will be queued to call housekeeping_update() to update the 4131 * corresponding housekeeping cpumasks after some slight delay. 4132 * 4133 * We rely on WORK_STRUCT_PENDING_BIT to not requeue a work item that 4134 * is still pending. Before the pending bit is cleared, the work data 4135 * is copied out and work item dequeued. So it is possible to queue 4136 * the work again before the hk_sd_workfn() is invoked to process the 4137 * previously queued work. Since hk_sd_workfn() doesn't use the work 4138 * item at all, this is not a problem. 4139 */ 4140 if (force_sd_rebuild) 4141 rebuild_sched_domains_cpuslocked(); 4142 if (update_housekeeping) 4143 queue_work(system_dfl_wq, &hk_sd_work); 4144 4145 free_tmpmasks(ptmp); 4146 } 4147 4148 void cpuset_update_active_cpus(void) 4149 { 4150 /* 4151 * We're inside cpu hotplug critical region which usually nests 4152 * inside cgroup synchronization. Bounce actual hotplug processing 4153 * to a work item to avoid reverse locking order. 4154 */ 4155 cpuset_handle_hotplug(); 4156 } 4157 4158 /* 4159 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. 4160 * Call this routine anytime after node_states[N_MEMORY] changes. 4161 * See cpuset_update_active_cpus() for CPU hotplug handling. 4162 */ 4163 static int cpuset_track_online_nodes(struct notifier_block *self, 4164 unsigned long action, void *arg) 4165 { 4166 cpuset_handle_hotplug(); 4167 return NOTIFY_OK; 4168 } 4169 4170 /** 4171 * cpuset_init_smp - initialize cpus_allowed 4172 * 4173 * Description: Finish top cpuset after cpu, node maps are initialized 4174 */ 4175 void __init cpuset_init_smp(void) 4176 { 4177 /* 4178 * cpus_allowd/mems_allowed set to v2 values in the initial 4179 * cpuset_bind() call will be reset to v1 values in another 4180 * cpuset_bind() call when v1 cpuset is mounted. 4181 */ 4182 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed; 4183 4184 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask); 4185 top_cpuset.effective_mems = node_states[N_MEMORY]; 4186 4187 hotplug_node_notifier(cpuset_track_online_nodes, CPUSET_CALLBACK_PRI); 4188 4189 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0); 4190 BUG_ON(!cpuset_migrate_mm_wq); 4191 } 4192 4193 /* 4194 * Return cpus_allowed mask from a task's cpuset. 4195 */ 4196 static void __cpuset_cpus_allowed_locked(struct task_struct *tsk, struct cpumask *pmask) 4197 { 4198 struct cpuset *cs; 4199 4200 cs = task_cs(tsk); 4201 if (cs != &top_cpuset) 4202 guarantee_active_cpus(tsk, pmask); 4203 /* 4204 * Tasks in the top cpuset won't get update to their cpumasks 4205 * when a hotplug online/offline event happens. So we include all 4206 * offline cpus in the allowed cpu list. 4207 */ 4208 if ((cs == &top_cpuset) || cpumask_empty(pmask)) { 4209 const struct cpumask *possible_mask = task_cpu_possible_mask(tsk); 4210 4211 /* 4212 * We first exclude cpus allocated to partitions. If there is no 4213 * allowable online cpu left, we fall back to all possible cpus. 4214 */ 4215 cpumask_andnot(pmask, possible_mask, subpartitions_cpus); 4216 if (!cpumask_intersects(pmask, cpu_active_mask)) 4217 cpumask_copy(pmask, possible_mask); 4218 } 4219 } 4220 4221 /** 4222 * cpuset_cpus_allowed_locked - return cpus_allowed mask from a task's cpuset. 4223 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. 4224 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. 4225 * 4226 * Similir to cpuset_cpus_allowed() except that the caller must have acquired 4227 * cpuset_mutex. 4228 */ 4229 void cpuset_cpus_allowed_locked(struct task_struct *tsk, struct cpumask *pmask) 4230 { 4231 lockdep_assert_cpuset_lock_held(); 4232 __cpuset_cpus_allowed_locked(tsk, pmask); 4233 } 4234 4235 /** 4236 * cpuset_cpus_allowed - return cpus_allowed mask from a task's cpuset. 4237 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. 4238 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. 4239 * 4240 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset 4241 * attached to the specified @tsk. Guaranteed to return some non-empty 4242 * subset of cpu_active_mask, even if this means going outside the 4243 * tasks cpuset, except when the task is in the top cpuset. 4244 **/ 4245 4246 void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) 4247 { 4248 unsigned long flags; 4249 4250 spin_lock_irqsave(&callback_lock, flags); 4251 __cpuset_cpus_allowed_locked(tsk, pmask); 4252 spin_unlock_irqrestore(&callback_lock, flags); 4253 } 4254 4255 /** 4256 * cpuset_cpus_allowed_fallback - final fallback before complete catastrophe. 4257 * @tsk: pointer to task_struct with which the scheduler is struggling 4258 * 4259 * Description: In the case that the scheduler cannot find an allowed cpu in 4260 * tsk->cpus_allowed, we fall back to task_cs(tsk)->cpus_allowed. In legacy 4261 * mode however, this value is the same as task_cs(tsk)->effective_cpus, 4262 * which will not contain a sane cpumask during cases such as cpu hotplugging. 4263 * This is the absolute last resort for the scheduler and it is only used if 4264 * _every_ other avenue has been traveled. 4265 * 4266 * Returns true if the affinity of @tsk was changed, false otherwise. 4267 **/ 4268 4269 bool cpuset_cpus_allowed_fallback(struct task_struct *tsk) 4270 { 4271 const struct cpumask *possible_mask = task_cpu_possible_mask(tsk); 4272 const struct cpumask *cs_mask; 4273 bool changed = false; 4274 4275 rcu_read_lock(); 4276 cs_mask = task_cs(tsk)->cpus_allowed; 4277 if (is_in_v2_mode() && cpumask_subset(cs_mask, possible_mask)) { 4278 set_cpus_allowed_force(tsk, cs_mask); 4279 changed = true; 4280 } 4281 rcu_read_unlock(); 4282 4283 /* 4284 * We own tsk->cpus_allowed, nobody can change it under us. 4285 * 4286 * But we used cs && cs->cpus_allowed lockless and thus can 4287 * race with cgroup_attach_task() or update_cpumask() and get 4288 * the wrong tsk->cpus_allowed. However, both cases imply the 4289 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() 4290 * which takes task_rq_lock(). 4291 * 4292 * If we are called after it dropped the lock we must see all 4293 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary 4294 * set any mask even if it is not right from task_cs() pov, 4295 * the pending set_cpus_allowed_ptr() will fix things. 4296 * 4297 * select_fallback_rq() will fix things ups and set cpu_possible_mask 4298 * if required. 4299 */ 4300 return changed; 4301 } 4302 4303 /* 4304 * Returns the number of CPUs available for this cgroup. 4305 * 4306 * This only really works for cgroup-v2 where all the controllers are mounted 4307 * in the same hierarchy. If not cgroup-v2 or no cpuset controller is 4308 * configured it reverts to num_online_cpus(). 4309 */ 4310 int cpuset_num_cpus(struct cgroup *cgrp) 4311 { 4312 int nr = num_online_cpus(); 4313 struct cpuset *cs; 4314 4315 if (is_in_v2_mode()) { 4316 guard(rcu)(); 4317 cs = css_cs(cgroup_e_css(cgrp, &cpuset_cgrp_subsys)); 4318 if (cs) 4319 nr = cpumask_weight(cs->effective_cpus); 4320 } 4321 4322 return nr; 4323 } 4324 4325 void __init cpuset_init_current_mems_allowed(void) 4326 { 4327 nodes_setall(current->mems_allowed); 4328 } 4329 4330 /** 4331 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. 4332 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. 4333 * 4334 * Description: Returns the nodemask_t mems_allowed of the cpuset 4335 * attached to the specified @tsk. Guaranteed to return some non-empty 4336 * subset of node_states[N_MEMORY], even if this means going outside the 4337 * tasks cpuset. 4338 **/ 4339 4340 nodemask_t cpuset_mems_allowed(struct task_struct *tsk) 4341 { 4342 nodemask_t mask; 4343 unsigned long flags; 4344 4345 spin_lock_irqsave(&callback_lock, flags); 4346 guarantee_online_mems(task_cs(tsk), &mask); 4347 spin_unlock_irqrestore(&callback_lock, flags); 4348 4349 return mask; 4350 } 4351 4352 /** 4353 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. current mems_allowed 4354 * @nodemask: the nodemask to be checked 4355 * 4356 * Are any of the nodes in the nodemask allowed in current->mems_allowed? 4357 */ 4358 int cpuset_nodemask_valid_mems_allowed(const nodemask_t *nodemask) 4359 { 4360 return nodes_intersects(*nodemask, current->mems_allowed); 4361 } 4362 4363 /* 4364 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or 4365 * mem_hardwall ancestor to the specified cpuset. Call holding 4366 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall 4367 * (an unusual configuration), then returns the root cpuset. 4368 */ 4369 static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs) 4370 { 4371 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs)) 4372 cs = parent_cs(cs); 4373 return cs; 4374 } 4375 4376 /* 4377 * cpuset_current_node_allowed - Can current task allocate on a memory node? 4378 * @node: is this an allowed node? 4379 * @gfp_mask: memory allocation flags 4380 * 4381 * If we're in interrupt, yes, we can always allocate. If @node is set in 4382 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this 4383 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset, 4384 * yes. If current has access to memory reserves as an oom victim, yes. 4385 * If the current task is PF_EXITING, yes. Otherwise, no. 4386 * 4387 * GFP_USER allocations are marked with the __GFP_HARDWALL bit, 4388 * and do not allow allocations outside the current tasks cpuset 4389 * unless the task has been OOM killed or is exiting. 4390 * GFP_KERNEL allocations are not so marked, so can escape to the 4391 * nearest enclosing hardwalled ancestor cpuset. 4392 * 4393 * Scanning up parent cpusets requires callback_lock. The 4394 * page allocator only calls here with __GFP_HARDWALL bit 4395 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the 4396 * current tasks mems_allowed came up empty on the first pass over 4397 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the 4398 * cpuset are short of memory, might require taking the callback_lock. 4399 * 4400 * The first call here from mm/page_alloc:get_page_from_freelist() 4401 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, 4402 * so no allocation on a node outside the cpuset is allowed (unless 4403 * in interrupt, of course). The PF_EXITING check must therefore 4404 * come before the __GFP_HARDWALL check, otherwise a dying task 4405 * would be blocked on the fast path. 4406 * 4407 * The second pass through get_page_from_freelist() doesn't even call here for 4408 * GFP_ATOMIC calls. That, and the checks below have the combined affect that: 4409 * in_interrupt - any node ok (current task context irrelevant) 4410 * GFP_ATOMIC - any node ok 4411 * tsk_is_oom_victim - any node ok 4412 * PF_EXITING - any node ok (let dying task exit quickly) 4413 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok 4414 * GFP_USER - only nodes in current tasks mems allowed ok. 4415 */ 4416 bool cpuset_current_node_allowed(int node, gfp_t gfp_mask) 4417 { 4418 struct cpuset *cs; /* current cpuset ancestors */ 4419 bool allowed; /* is allocation in zone z allowed? */ 4420 unsigned long flags; 4421 4422 if (in_interrupt()) 4423 return true; 4424 if (node_isset(node, current->mems_allowed)) 4425 return true; 4426 /* 4427 * Allow tasks that have access to memory reserves because they have 4428 * been OOM killed to get memory anywhere. 4429 */ 4430 if (unlikely(tsk_is_oom_victim(current))) 4431 return true; 4432 if (current->flags & PF_EXITING) /* Let dying task have memory */ 4433 return true; 4434 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ 4435 return false; 4436 4437 if (cpuset_v2()) 4438 return true; 4439 4440 /* Not hardwall and node outside mems_allowed: scan up cpusets */ 4441 spin_lock_irqsave(&callback_lock, flags); 4442 4443 cs = nearest_hardwall_ancestor(task_cs(current)); 4444 allowed = node_isset(node, cs->mems_allowed); 4445 4446 spin_unlock_irqrestore(&callback_lock, flags); 4447 return allowed; 4448 } 4449 4450 /** 4451 * cpuset_nodes_allowed - return effective_mems mask from a cgroup cpuset. 4452 * @cgroup: pointer to struct cgroup. 4453 * @mask: pointer to struct nodemask_t to be returned. 4454 * 4455 * Returns effective_mems mask from a cgroup cpuset if it is cgroup v2 and 4456 * has cpuset subsys. Otherwise, returns node_states[N_MEMORY]. 4457 * 4458 * This function intentionally avoids taking the cpuset_mutex or callback_lock 4459 * when accessing effective_mems. This is because the obtained effective_mems 4460 * is stale immediately after the query anyway (e.g., effective_mems is updated 4461 * immediately after releasing the lock but before returning). 4462 * 4463 * As a result, returned @mask may be empty because cs->effective_mems can be 4464 * rebound during this call. Besides, nodes in @mask are not guaranteed to be 4465 * online due to hot plugins. Callers should check the mask for validity on 4466 * return based on its subsequent use. 4467 **/ 4468 void cpuset_nodes_allowed(struct cgroup *cgroup, nodemask_t *mask) 4469 { 4470 struct cgroup_subsys_state *css; 4471 struct cpuset *cs; 4472 4473 /* 4474 * In v1, mem_cgroup and cpuset are unlikely in the same hierarchy 4475 * and mems_allowed is likely to be empty even if we could get to it, 4476 * so return directly to avoid taking a global lock on the empty check. 4477 */ 4478 if (!cgroup || !cpuset_v2()) { 4479 nodes_copy(*mask, node_states[N_MEMORY]); 4480 return; 4481 } 4482 4483 css = cgroup_get_e_css(cgroup, &cpuset_cgrp_subsys); 4484 if (!css) { 4485 nodes_copy(*mask, node_states[N_MEMORY]); 4486 return; 4487 } 4488 4489 /* 4490 * The reference taken via cgroup_get_e_css is sufficient to 4491 * protect css, but it does not imply safe accesses to effective_mems. 4492 * 4493 * Normally, accessing effective_mems would require the cpuset_mutex 4494 * or callback_lock - but the correctness of this information is stale 4495 * immediately after the query anyway. We do not acquire the lock 4496 * during this process to save lock contention in exchange for racing 4497 * against mems_allowed rebinds. 4498 */ 4499 cs = container_of(css, struct cpuset, css); 4500 nodes_copy(*mask, cs->effective_mems); 4501 css_put(css); 4502 } 4503 4504 /** 4505 * cpuset_spread_node() - On which node to begin search for a page 4506 * @rotor: round robin rotor 4507 * 4508 * If a task is marked PFA_SPREAD_PAGE and a page cache allocation uses 4509 * cpuset_mem_spread_node() to determine where to start looking, spread the 4510 * starting node around the task's mems_allowed nodes instead of starting on 4511 * the local node. 4512 * 4513 * We don't have to worry about the returned node being offline 4514 * because "it can't happen", and even if it did, it would be ok. 4515 * 4516 * The routines calling guarantee_online_mems() are careful to 4517 * only set nodes in task->mems_allowed that are online. So it 4518 * should not be possible for the following code to return an 4519 * offline node. But if it did, that would be ok, as this routine 4520 * is not returning the node where the allocation must be, only 4521 * the node where the search should start. The zonelist used by 4522 * the allocator will include all nodes. If the slab allocator 4523 * is passed an offline node, it will fall back to the local node. 4524 * See kmem_cache_alloc_node(). 4525 */ 4526 static int cpuset_spread_node(int *rotor) 4527 { 4528 return *rotor = next_node_in(*rotor, current->mems_allowed); 4529 } 4530 4531 /** 4532 * cpuset_mem_spread_node() - On which node to begin search for a file page 4533 */ 4534 int cpuset_mem_spread_node(void) 4535 { 4536 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) 4537 current->cpuset_mem_spread_rotor = 4538 node_random(¤t->mems_allowed); 4539 4540 return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); 4541 } 4542 4543 /** 4544 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? 4545 * @tsk1: pointer to task_struct of some task. 4546 * @tsk2: pointer to task_struct of some other task. 4547 * 4548 * Description: Return true if @tsk1's mems_allowed intersects the 4549 * mems_allowed of @tsk2. Used by the OOM killer to determine if 4550 * one of the task's memory usage might impact the memory available 4551 * to the other. 4552 **/ 4553 4554 int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, 4555 const struct task_struct *tsk2) 4556 { 4557 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); 4558 } 4559 4560 /** 4561 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed 4562 * 4563 * Description: Prints current's name, cpuset name, and cached copy of its 4564 * mems_allowed to the kernel log. 4565 */ 4566 void cpuset_print_current_mems_allowed(void) 4567 { 4568 struct cgroup *cgrp; 4569 4570 rcu_read_lock(); 4571 4572 cgrp = task_cs(current)->css.cgroup; 4573 pr_cont(",cpuset="); 4574 pr_cont_cgroup_name(cgrp); 4575 pr_cont(",mems_allowed=%*pbl", 4576 nodemask_pr_args(¤t->mems_allowed)); 4577 4578 rcu_read_unlock(); 4579 } 4580 4581 /* Display task mems_allowed in /proc/<pid>/status file. */ 4582 void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) 4583 { 4584 seq_printf(m, "Mems_allowed:\t%*pb\n", 4585 nodemask_pr_args(&task->mems_allowed)); 4586 seq_printf(m, "Mems_allowed_list:\t%*pbl\n", 4587 nodemask_pr_args(&task->mems_allowed)); 4588 } 4589