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