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