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
25 #include <linux/cpu.h>
26 #include <linux/cpumask.h>
27 #include <linux/cpuset.h>
28 #include <linux/err.h>
29 #include <linux/errno.h>
30 #include <linux/file.h>
31 #include <linux/fs.h>
32 #include <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <linux/kmod.h>
36 #include <linux/list.h>
37 #include <linux/mempolicy.h>
38 #include <linux/mm.h>
39 #include <linux/memory.h>
40 #include <linux/export.h>
41 #include <linux/mount.h>
42 #include <linux/fs_context.h>
43 #include <linux/namei.h>
44 #include <linux/pagemap.h>
45 #include <linux/proc_fs.h>
46 #include <linux/rcupdate.h>
47 #include <linux/sched.h>
48 #include <linux/sched/deadline.h>
49 #include <linux/sched/mm.h>
50 #include <linux/sched/task.h>
51 #include <linux/seq_file.h>
52 #include <linux/security.h>
53 #include <linux/slab.h>
54 #include <linux/spinlock.h>
55 #include <linux/stat.h>
56 #include <linux/string.h>
57 #include <linux/time.h>
58 #include <linux/time64.h>
59 #include <linux/backing-dev.h>
60 #include <linux/sort.h>
61 #include <linux/oom.h>
62 #include <linux/sched/isolation.h>
63 #include <linux/uaccess.h>
64 #include <linux/atomic.h>
65 #include <linux/mutex.h>
66 #include <linux/cgroup.h>
67 #include <linux/wait.h>
68
69 DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key);
70 DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key);
71
72 /* See "Frequency meter" comments, below. */
73
74 struct fmeter {
75 int cnt; /* unprocessed events count */
76 int val; /* most recent output value */
77 time64_t time; /* clock (secs) when val computed */
78 spinlock_t lock; /* guards read or write of above */
79 };
80
81 struct cpuset {
82 struct cgroup_subsys_state css;
83
84 unsigned long flags; /* "unsigned long" so bitops work */
85
86 /*
87 * On default hierarchy:
88 *
89 * The user-configured masks can only be changed by writing to
90 * cpuset.cpus and cpuset.mems, and won't be limited by the
91 * parent masks.
92 *
93 * The effective masks is the real masks that apply to the tasks
94 * in the cpuset. They may be changed if the configured masks are
95 * changed or hotplug happens.
96 *
97 * effective_mask == configured_mask & parent's effective_mask,
98 * and if it ends up empty, it will inherit the parent's mask.
99 *
100 *
101 * On legacy hierachy:
102 *
103 * The user-configured masks are always the same with effective masks.
104 */
105
106 /* user-configured CPUs and Memory Nodes allow to tasks */
107 cpumask_var_t cpus_allowed;
108 cpumask_var_t cpus_requested;
109 nodemask_t mems_allowed;
110
111 /* effective CPUs and Memory Nodes allow to tasks */
112 cpumask_var_t effective_cpus;
113 nodemask_t effective_mems;
114
115 /*
116 * CPUs allocated to child sub-partitions (default hierarchy only)
117 * - CPUs granted by the parent = effective_cpus U subparts_cpus
118 * - effective_cpus and subparts_cpus are mutually exclusive.
119 *
120 * effective_cpus contains only onlined CPUs, but subparts_cpus
121 * may have offlined ones.
122 */
123 cpumask_var_t subparts_cpus;
124
125 /*
126 * This is old Memory Nodes tasks took on.
127 *
128 * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
129 * - A new cpuset's old_mems_allowed is initialized when some
130 * task is moved into it.
131 * - old_mems_allowed is used in cpuset_migrate_mm() when we change
132 * cpuset.mems_allowed and have tasks' nodemask updated, and
133 * then old_mems_allowed is updated to mems_allowed.
134 */
135 nodemask_t old_mems_allowed;
136
137 struct fmeter fmeter; /* memory_pressure filter */
138
139 /*
140 * Tasks are being attached to this cpuset. Used to prevent
141 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
142 */
143 int attach_in_progress;
144
145 /* partition number for rebuild_sched_domains() */
146 int pn;
147
148 /* for custom sched domain */
149 int relax_domain_level;
150
151 /* number of CPUs in subparts_cpus */
152 int nr_subparts_cpus;
153
154 /* partition root state */
155 int partition_root_state;
156
157 /*
158 * Default hierarchy only:
159 * use_parent_ecpus - set if using parent's effective_cpus
160 * child_ecpus_count - # of children with use_parent_ecpus set
161 */
162 int use_parent_ecpus;
163 int child_ecpus_count;
164 };
165
166 /*
167 * Partition root states:
168 *
169 * 0 - not a partition root
170 *
171 * 1 - partition root
172 *
173 * -1 - invalid partition root
174 * None of the cpus in cpus_allowed can be put into the parent's
175 * subparts_cpus. In this case, the cpuset is not a real partition
176 * root anymore. However, the CPU_EXCLUSIVE bit will still be set
177 * and the cpuset can be restored back to a partition root if the
178 * parent cpuset can give more CPUs back to this child cpuset.
179 */
180 #define PRS_DISABLED 0
181 #define PRS_ENABLED 1
182 #define PRS_ERROR -1
183
184 /*
185 * Temporary cpumasks for working with partitions that are passed among
186 * functions to avoid memory allocation in inner functions.
187 */
188 struct tmpmasks {
189 cpumask_var_t addmask, delmask; /* For partition root */
190 cpumask_var_t new_cpus; /* For update_cpumasks_hier() */
191 };
192
css_cs(struct cgroup_subsys_state * css)193 static inline struct cpuset *css_cs(struct cgroup_subsys_state *css)
194 {
195 return css ? container_of(css, struct cpuset, css) : NULL;
196 }
197
198 /* Retrieve the cpuset for a task */
task_cs(struct task_struct * task)199 static inline struct cpuset *task_cs(struct task_struct *task)
200 {
201 return css_cs(task_css(task, cpuset_cgrp_id));
202 }
203
parent_cs(struct cpuset * cs)204 static inline struct cpuset *parent_cs(struct cpuset *cs)
205 {
206 return css_cs(cs->css.parent);
207 }
208
209 /* bits in struct cpuset flags field */
210 typedef enum {
211 CS_ONLINE,
212 CS_CPU_EXCLUSIVE,
213 CS_MEM_EXCLUSIVE,
214 CS_MEM_HARDWALL,
215 CS_MEMORY_MIGRATE,
216 CS_SCHED_LOAD_BALANCE,
217 CS_SPREAD_PAGE,
218 CS_SPREAD_SLAB,
219 } cpuset_flagbits_t;
220
221 /* convenient tests for these bits */
is_cpuset_online(struct cpuset * cs)222 static inline bool is_cpuset_online(struct cpuset *cs)
223 {
224 return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css);
225 }
226
is_cpu_exclusive(const struct cpuset * cs)227 static inline int is_cpu_exclusive(const struct cpuset *cs)
228 {
229 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
230 }
231
is_mem_exclusive(const struct cpuset * cs)232 static inline int is_mem_exclusive(const struct cpuset *cs)
233 {
234 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
235 }
236
is_mem_hardwall(const struct cpuset * cs)237 static inline int is_mem_hardwall(const struct cpuset *cs)
238 {
239 return test_bit(CS_MEM_HARDWALL, &cs->flags);
240 }
241
is_sched_load_balance(const struct cpuset * cs)242 static inline int is_sched_load_balance(const struct cpuset *cs)
243 {
244 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
245 }
246
is_memory_migrate(const struct cpuset * cs)247 static inline int is_memory_migrate(const struct cpuset *cs)
248 {
249 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
250 }
251
is_spread_page(const struct cpuset * cs)252 static inline int is_spread_page(const struct cpuset *cs)
253 {
254 return test_bit(CS_SPREAD_PAGE, &cs->flags);
255 }
256
is_spread_slab(const struct cpuset * cs)257 static inline int is_spread_slab(const struct cpuset *cs)
258 {
259 return test_bit(CS_SPREAD_SLAB, &cs->flags);
260 }
261
is_partition_root(const struct cpuset * cs)262 static inline int is_partition_root(const struct cpuset *cs)
263 {
264 return cs->partition_root_state > 0;
265 }
266
267 static struct cpuset top_cpuset = {
268 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) |
269 (1 << CS_MEM_EXCLUSIVE)),
270 .partition_root_state = PRS_ENABLED,
271 };
272
273 /**
274 * cpuset_for_each_child - traverse online children of a cpuset
275 * @child_cs: loop cursor pointing to the current child
276 * @pos_css: used for iteration
277 * @parent_cs: target cpuset to walk children of
278 *
279 * Walk @child_cs through the online children of @parent_cs. Must be used
280 * with RCU read locked.
281 */
282 #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \
283 css_for_each_child((pos_css), &(parent_cs)->css) \
284 if (is_cpuset_online(((child_cs) = css_cs((pos_css)))))
285
286 /**
287 * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
288 * @des_cs: loop cursor pointing to the current descendant
289 * @pos_css: used for iteration
290 * @root_cs: target cpuset to walk ancestor of
291 *
292 * Walk @des_cs through the online descendants of @root_cs. Must be used
293 * with RCU read locked. The caller may modify @pos_css by calling
294 * css_rightmost_descendant() to skip subtree. @root_cs is included in the
295 * iteration and the first node to be visited.
296 */
297 #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \
298 css_for_each_descendant_pre((pos_css), &(root_cs)->css) \
299 if (is_cpuset_online(((des_cs) = css_cs((pos_css)))))
300
301 /*
302 * There are two global locks guarding cpuset structures - cpuset_mutex and
303 * callback_lock. We also require taking task_lock() when dereferencing a
304 * task's cpuset pointer. See "The task_lock() exception", at the end of this
305 * comment.
306 *
307 * A task must hold both locks to modify cpusets. If a task holds
308 * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it
309 * is the only task able to also acquire callback_lock and be able to
310 * modify cpusets. It can perform various checks on the cpuset structure
311 * first, knowing nothing will change. It can also allocate memory while
312 * just holding cpuset_mutex. While it is performing these checks, various
313 * callback routines can briefly acquire callback_lock to query cpusets.
314 * Once it is ready to make the changes, it takes callback_lock, blocking
315 * everyone else.
316 *
317 * Calls to the kernel memory allocator can not be made while holding
318 * callback_lock, as that would risk double tripping on callback_lock
319 * from one of the callbacks into the cpuset code from within
320 * __alloc_pages().
321 *
322 * If a task is only holding callback_lock, then it has read-only
323 * access to cpusets.
324 *
325 * Now, the task_struct fields mems_allowed and mempolicy may be changed
326 * by other task, we use alloc_lock in the task_struct fields to protect
327 * them.
328 *
329 * The cpuset_common_file_read() handlers only hold callback_lock across
330 * small pieces of code, such as when reading out possibly multi-word
331 * cpumasks and nodemasks.
332 *
333 * Accessing a task's cpuset should be done in accordance with the
334 * guidelines for accessing subsystem state in kernel/cgroup.c
335 */
336
337 DEFINE_STATIC_PERCPU_RWSEM(cpuset_rwsem);
338
cpuset_read_lock(void)339 void cpuset_read_lock(void)
340 {
341 percpu_down_read(&cpuset_rwsem);
342 }
343
cpuset_read_unlock(void)344 void cpuset_read_unlock(void)
345 {
346 percpu_up_read(&cpuset_rwsem);
347 }
348
349 static DEFINE_SPINLOCK(callback_lock);
350
351 static struct workqueue_struct *cpuset_migrate_mm_wq;
352
353 /*
354 * CPU / memory hotplug is handled asynchronously.
355 */
356 static void cpuset_hotplug_workfn(struct work_struct *work);
357 static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn);
358
359 static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq);
360
361 /*
362 * Cgroup v2 behavior is used on the "cpus" and "mems" control files when
363 * on default hierarchy or when the cpuset_v2_mode flag is set by mounting
364 * the v1 cpuset cgroup filesystem with the "cpuset_v2_mode" mount option.
365 * With v2 behavior, "cpus" and "mems" are always what the users have
366 * requested and won't be changed by hotplug events. Only the effective
367 * cpus or mems will be affected.
368 */
is_in_v2_mode(void)369 static inline bool is_in_v2_mode(void)
370 {
371 return cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
372 (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
373 }
374
375 /*
376 * Return in pmask the portion of a cpusets's cpus_allowed that
377 * are online. If none are online, walk up the cpuset hierarchy
378 * until we find one that does have some online cpus.
379 *
380 * One way or another, we guarantee to return some non-empty subset
381 * of cpu_online_mask.
382 *
383 * Call with callback_lock or cpuset_mutex held.
384 */
guarantee_online_cpus(struct cpuset * cs,struct cpumask * pmask)385 static void guarantee_online_cpus(struct cpuset *cs, struct cpumask *pmask)
386 {
387 while (!cpumask_intersects(cs->effective_cpus, cpu_online_mask)) {
388 cs = parent_cs(cs);
389 if (unlikely(!cs)) {
390 /*
391 * The top cpuset doesn't have any online cpu as a
392 * consequence of a race between cpuset_hotplug_work
393 * and cpu hotplug notifier. But we know the top
394 * cpuset's effective_cpus is on its way to be
395 * identical to cpu_online_mask.
396 */
397 cpumask_copy(pmask, cpu_online_mask);
398 return;
399 }
400 }
401 cpumask_and(pmask, cs->effective_cpus, cpu_online_mask);
402 }
403
404 /*
405 * Return in *pmask the portion of a cpusets's mems_allowed that
406 * are online, with memory. If none are online with memory, walk
407 * up the cpuset hierarchy until we find one that does have some
408 * online mems. The top cpuset always has some mems online.
409 *
410 * One way or another, we guarantee to return some non-empty subset
411 * of node_states[N_MEMORY].
412 *
413 * Call with callback_lock or cpuset_mutex held.
414 */
guarantee_online_mems(struct cpuset * cs,nodemask_t * pmask)415 static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
416 {
417 while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
418 cs = parent_cs(cs);
419 nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
420 }
421
422 /*
423 * update task's spread flag if cpuset's page/slab spread flag is set
424 *
425 * Call with callback_lock or cpuset_mutex held.
426 */
cpuset_update_task_spread_flag(struct cpuset * cs,struct task_struct * tsk)427 static void cpuset_update_task_spread_flag(struct cpuset *cs,
428 struct task_struct *tsk)
429 {
430 if (is_spread_page(cs))
431 task_set_spread_page(tsk);
432 else
433 task_clear_spread_page(tsk);
434
435 if (is_spread_slab(cs))
436 task_set_spread_slab(tsk);
437 else
438 task_clear_spread_slab(tsk);
439 }
440
441 /*
442 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
443 *
444 * One cpuset is a subset of another if all its allowed CPUs and
445 * Memory Nodes are a subset of the other, and its exclusive flags
446 * are only set if the other's are set. Call holding cpuset_mutex.
447 */
448
is_cpuset_subset(const struct cpuset * p,const struct cpuset * q)449 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
450 {
451 return cpumask_subset(p->cpus_requested, q->cpus_requested) &&
452 nodes_subset(p->mems_allowed, q->mems_allowed) &&
453 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
454 is_mem_exclusive(p) <= is_mem_exclusive(q);
455 }
456
457 /**
458 * alloc_cpumasks - allocate three cpumasks for cpuset
459 * @cs: the cpuset that have cpumasks to be allocated.
460 * @tmp: the tmpmasks structure pointer
461 * Return: 0 if successful, -ENOMEM otherwise.
462 *
463 * Only one of the two input arguments should be non-NULL.
464 */
alloc_cpumasks(struct cpuset * cs,struct tmpmasks * tmp)465 static inline int alloc_cpumasks(struct cpuset *cs, struct tmpmasks *tmp)
466 {
467 cpumask_var_t *pmask1, *pmask2, *pmask3, *pmask4;
468
469 if (cs) {
470 pmask1 = &cs->cpus_allowed;
471 pmask2 = &cs->effective_cpus;
472 pmask3 = &cs->subparts_cpus;
473 pmask4 = &cs->cpus_requested;
474 } else {
475 pmask1 = &tmp->new_cpus;
476 pmask2 = &tmp->addmask;
477 pmask3 = &tmp->delmask;
478 }
479
480 if (!zalloc_cpumask_var(pmask1, GFP_KERNEL))
481 return -ENOMEM;
482
483 if (!zalloc_cpumask_var(pmask2, GFP_KERNEL))
484 goto free_one;
485
486 if (!zalloc_cpumask_var(pmask3, GFP_KERNEL))
487 goto free_two;
488
489 if (cs && !zalloc_cpumask_var(pmask4, GFP_KERNEL))
490 goto free_three;
491
492 return 0;
493
494 free_three:
495 free_cpumask_var(*pmask3);
496 free_two:
497 free_cpumask_var(*pmask2);
498 free_one:
499 free_cpumask_var(*pmask1);
500 return -ENOMEM;
501 }
502
503 /**
504 * free_cpumasks - free cpumasks in a tmpmasks structure
505 * @cs: the cpuset that have cpumasks to be free.
506 * @tmp: the tmpmasks structure pointer
507 */
free_cpumasks(struct cpuset * cs,struct tmpmasks * tmp)508 static inline void free_cpumasks(struct cpuset *cs, struct tmpmasks *tmp)
509 {
510 if (cs) {
511 free_cpumask_var(cs->cpus_allowed);
512 free_cpumask_var(cs->cpus_requested);
513 free_cpumask_var(cs->effective_cpus);
514 free_cpumask_var(cs->subparts_cpus);
515 }
516 if (tmp) {
517 free_cpumask_var(tmp->new_cpus);
518 free_cpumask_var(tmp->addmask);
519 free_cpumask_var(tmp->delmask);
520 }
521 }
522
523 /**
524 * alloc_trial_cpuset - allocate a trial cpuset
525 * @cs: the cpuset that the trial cpuset duplicates
526 */
alloc_trial_cpuset(struct cpuset * cs)527 static struct cpuset *alloc_trial_cpuset(struct cpuset *cs)
528 {
529 struct cpuset *trial;
530
531 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
532 if (!trial)
533 return NULL;
534
535 if (alloc_cpumasks(trial, NULL)) {
536 kfree(trial);
537 return NULL;
538 }
539
540 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
541 cpumask_copy(trial->cpus_requested, cs->cpus_requested);
542 cpumask_copy(trial->effective_cpus, cs->effective_cpus);
543 return trial;
544 }
545
546 /**
547 * free_cpuset - free the cpuset
548 * @cs: the cpuset to be freed
549 */
free_cpuset(struct cpuset * cs)550 static inline void free_cpuset(struct cpuset *cs)
551 {
552 free_cpumasks(cs, NULL);
553 kfree(cs);
554 }
555
556 /*
557 * validate_change() - Used to validate that any proposed cpuset change
558 * follows the structural rules for cpusets.
559 *
560 * If we replaced the flag and mask values of the current cpuset
561 * (cur) with those values in the trial cpuset (trial), would
562 * our various subset and exclusive rules still be valid? Presumes
563 * cpuset_mutex held.
564 *
565 * 'cur' is the address of an actual, in-use cpuset. Operations
566 * such as list traversal that depend on the actual address of the
567 * cpuset in the list must use cur below, not trial.
568 *
569 * 'trial' is the address of bulk structure copy of cur, with
570 * perhaps one or more of the fields cpus_allowed, mems_allowed,
571 * or flags changed to new, trial values.
572 *
573 * Return 0 if valid, -errno if not.
574 */
575
validate_change(struct cpuset * cur,struct cpuset * trial)576 static int validate_change(struct cpuset *cur, struct cpuset *trial)
577 {
578 struct cgroup_subsys_state *css;
579 struct cpuset *c, *par;
580 int ret;
581
582 rcu_read_lock();
583
584 /* Each of our child cpusets must be a subset of us */
585 ret = -EBUSY;
586 cpuset_for_each_child(c, css, cur)
587 if (!is_cpuset_subset(c, trial))
588 goto out;
589
590 /* Remaining checks don't apply to root cpuset */
591 ret = 0;
592 if (cur == &top_cpuset)
593 goto out;
594
595 par = parent_cs(cur);
596
597 /* On legacy hiearchy, we must be a subset of our parent cpuset. */
598 ret = -EACCES;
599 if (!is_in_v2_mode() && !is_cpuset_subset(trial, par))
600 goto out;
601
602 /*
603 * If either I or some sibling (!= me) is exclusive, we can't
604 * overlap
605 */
606 ret = -EINVAL;
607 cpuset_for_each_child(c, css, par) {
608 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
609 c != cur &&
610 cpumask_intersects(trial->cpus_requested,
611 c->cpus_requested))
612 goto out;
613 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
614 c != cur &&
615 nodes_intersects(trial->mems_allowed, c->mems_allowed))
616 goto out;
617 }
618
619 /*
620 * Cpusets with tasks - existing or newly being attached - can't
621 * be changed to have empty cpus_allowed or mems_allowed.
622 */
623 ret = -ENOSPC;
624 if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) {
625 if (!cpumask_empty(cur->cpus_allowed) &&
626 cpumask_empty(trial->cpus_allowed))
627 goto out;
628 if (!nodes_empty(cur->mems_allowed) &&
629 nodes_empty(trial->mems_allowed))
630 goto out;
631 }
632
633 /*
634 * We can't shrink if we won't have enough room for SCHED_DEADLINE
635 * tasks.
636 */
637 ret = -EBUSY;
638 if (is_cpu_exclusive(cur) &&
639 !cpuset_cpumask_can_shrink(cur->cpus_allowed,
640 trial->cpus_allowed))
641 goto out;
642
643 ret = 0;
644 out:
645 rcu_read_unlock();
646 return ret;
647 }
648
649 #ifdef CONFIG_SMP
650 /*
651 * Helper routine for generate_sched_domains().
652 * Do cpusets a, b have overlapping effective cpus_allowed masks?
653 */
cpusets_overlap(struct cpuset * a,struct cpuset * b)654 static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
655 {
656 return cpumask_intersects(a->effective_cpus, b->effective_cpus);
657 }
658
659 static void
update_domain_attr(struct sched_domain_attr * dattr,struct cpuset * c)660 update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
661 {
662 if (dattr->relax_domain_level < c->relax_domain_level)
663 dattr->relax_domain_level = c->relax_domain_level;
664 return;
665 }
666
update_domain_attr_tree(struct sched_domain_attr * dattr,struct cpuset * root_cs)667 static void update_domain_attr_tree(struct sched_domain_attr *dattr,
668 struct cpuset *root_cs)
669 {
670 struct cpuset *cp;
671 struct cgroup_subsys_state *pos_css;
672
673 rcu_read_lock();
674 cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
675 /* skip the whole subtree if @cp doesn't have any CPU */
676 if (cpumask_empty(cp->cpus_allowed)) {
677 pos_css = css_rightmost_descendant(pos_css);
678 continue;
679 }
680
681 if (is_sched_load_balance(cp))
682 update_domain_attr(dattr, cp);
683 }
684 rcu_read_unlock();
685 }
686
687 /* Must be called with cpuset_mutex held. */
nr_cpusets(void)688 static inline int nr_cpusets(void)
689 {
690 /* jump label reference count + the top-level cpuset */
691 return static_key_count(&cpusets_enabled_key.key) + 1;
692 }
693
694 /*
695 * generate_sched_domains()
696 *
697 * This function builds a partial partition of the systems CPUs
698 * A 'partial partition' is a set of non-overlapping subsets whose
699 * union is a subset of that set.
700 * The output of this function needs to be passed to kernel/sched/core.c
701 * partition_sched_domains() routine, which will rebuild the scheduler's
702 * load balancing domains (sched domains) as specified by that partial
703 * partition.
704 *
705 * See "What is sched_load_balance" in Documentation/admin-guide/cgroup-v1/cpusets.rst
706 * for a background explanation of this.
707 *
708 * Does not return errors, on the theory that the callers of this
709 * routine would rather not worry about failures to rebuild sched
710 * domains when operating in the severe memory shortage situations
711 * that could cause allocation failures below.
712 *
713 * Must be called with cpuset_mutex held.
714 *
715 * The three key local variables below are:
716 * cp - cpuset pointer, used (together with pos_css) to perform a
717 * top-down scan of all cpusets. For our purposes, rebuilding
718 * the schedulers sched domains, we can ignore !is_sched_load_
719 * balance cpusets.
720 * csa - (for CpuSet Array) Array of pointers to all the cpusets
721 * that need to be load balanced, for convenient iterative
722 * access by the subsequent code that finds the best partition,
723 * i.e the set of domains (subsets) of CPUs such that the
724 * cpus_allowed of every cpuset marked is_sched_load_balance
725 * is a subset of one of these domains, while there are as
726 * many such domains as possible, each as small as possible.
727 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
728 * the kernel/sched/core.c routine partition_sched_domains() in a
729 * convenient format, that can be easily compared to the prior
730 * value to determine what partition elements (sched domains)
731 * were changed (added or removed.)
732 *
733 * Finding the best partition (set of domains):
734 * The triple nested loops below over i, j, k scan over the
735 * load balanced cpusets (using the array of cpuset pointers in
736 * csa[]) looking for pairs of cpusets that have overlapping
737 * cpus_allowed, but which don't have the same 'pn' partition
738 * number and gives them in the same partition number. It keeps
739 * looping on the 'restart' label until it can no longer find
740 * any such pairs.
741 *
742 * The union of the cpus_allowed masks from the set of
743 * all cpusets having the same 'pn' value then form the one
744 * element of the partition (one sched domain) to be passed to
745 * partition_sched_domains().
746 */
generate_sched_domains(cpumask_var_t ** domains,struct sched_domain_attr ** attributes)747 static int generate_sched_domains(cpumask_var_t **domains,
748 struct sched_domain_attr **attributes)
749 {
750 struct cpuset *cp; /* top-down scan of cpusets */
751 struct cpuset **csa; /* array of all cpuset ptrs */
752 int csn; /* how many cpuset ptrs in csa so far */
753 int i, j, k; /* indices for partition finding loops */
754 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
755 struct sched_domain_attr *dattr; /* attributes for custom domains */
756 int ndoms = 0; /* number of sched domains in result */
757 int nslot; /* next empty doms[] struct cpumask slot */
758 struct cgroup_subsys_state *pos_css;
759 bool root_load_balance = is_sched_load_balance(&top_cpuset);
760
761 doms = NULL;
762 dattr = NULL;
763 csa = NULL;
764
765 /* Special case for the 99% of systems with one, full, sched domain */
766 if (root_load_balance && !top_cpuset.nr_subparts_cpus) {
767 ndoms = 1;
768 doms = alloc_sched_domains(ndoms);
769 if (!doms)
770 goto done;
771
772 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
773 if (dattr) {
774 *dattr = SD_ATTR_INIT;
775 update_domain_attr_tree(dattr, &top_cpuset);
776 }
777 cpumask_and(doms[0], top_cpuset.effective_cpus,
778 housekeeping_cpumask(HK_FLAG_DOMAIN));
779
780 goto done;
781 }
782
783 csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL);
784 if (!csa)
785 goto done;
786 csn = 0;
787
788 rcu_read_lock();
789 if (root_load_balance)
790 csa[csn++] = &top_cpuset;
791 cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
792 if (cp == &top_cpuset)
793 continue;
794 /*
795 * Continue traversing beyond @cp iff @cp has some CPUs and
796 * isn't load balancing. The former is obvious. The
797 * latter: All child cpusets contain a subset of the
798 * parent's cpus, so just skip them, and then we call
799 * update_domain_attr_tree() to calc relax_domain_level of
800 * the corresponding sched domain.
801 *
802 * If root is load-balancing, we can skip @cp if it
803 * is a subset of the root's effective_cpus.
804 */
805 if (!cpumask_empty(cp->cpus_allowed) &&
806 !(is_sched_load_balance(cp) &&
807 cpumask_intersects(cp->cpus_allowed,
808 housekeeping_cpumask(HK_FLAG_DOMAIN))))
809 continue;
810
811 if (root_load_balance &&
812 cpumask_subset(cp->cpus_allowed, top_cpuset.effective_cpus))
813 continue;
814
815 if (is_sched_load_balance(cp) &&
816 !cpumask_empty(cp->effective_cpus))
817 csa[csn++] = cp;
818
819 /* skip @cp's subtree if not a partition root */
820 if (!is_partition_root(cp))
821 pos_css = css_rightmost_descendant(pos_css);
822 }
823 rcu_read_unlock();
824
825 for (i = 0; i < csn; i++)
826 csa[i]->pn = i;
827 ndoms = csn;
828
829 restart:
830 /* Find the best partition (set of sched domains) */
831 for (i = 0; i < csn; i++) {
832 struct cpuset *a = csa[i];
833 int apn = a->pn;
834
835 for (j = 0; j < csn; j++) {
836 struct cpuset *b = csa[j];
837 int bpn = b->pn;
838
839 if (apn != bpn && cpusets_overlap(a, b)) {
840 for (k = 0; k < csn; k++) {
841 struct cpuset *c = csa[k];
842
843 if (c->pn == bpn)
844 c->pn = apn;
845 }
846 ndoms--; /* one less element */
847 goto restart;
848 }
849 }
850 }
851
852 /*
853 * Now we know how many domains to create.
854 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
855 */
856 doms = alloc_sched_domains(ndoms);
857 if (!doms)
858 goto done;
859
860 /*
861 * The rest of the code, including the scheduler, can deal with
862 * dattr==NULL case. No need to abort if alloc fails.
863 */
864 dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr),
865 GFP_KERNEL);
866
867 for (nslot = 0, i = 0; i < csn; i++) {
868 struct cpuset *a = csa[i];
869 struct cpumask *dp;
870 int apn = a->pn;
871
872 if (apn < 0) {
873 /* Skip completed partitions */
874 continue;
875 }
876
877 dp = doms[nslot];
878
879 if (nslot == ndoms) {
880 static int warnings = 10;
881 if (warnings) {
882 pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n",
883 nslot, ndoms, csn, i, apn);
884 warnings--;
885 }
886 continue;
887 }
888
889 cpumask_clear(dp);
890 if (dattr)
891 *(dattr + nslot) = SD_ATTR_INIT;
892 for (j = i; j < csn; j++) {
893 struct cpuset *b = csa[j];
894
895 if (apn == b->pn) {
896 cpumask_or(dp, dp, b->effective_cpus);
897 cpumask_and(dp, dp, housekeeping_cpumask(HK_FLAG_DOMAIN));
898 if (dattr)
899 update_domain_attr_tree(dattr + nslot, b);
900
901 /* Done with this partition */
902 b->pn = -1;
903 }
904 }
905 nslot++;
906 }
907 BUG_ON(nslot != ndoms);
908
909 done:
910 kfree(csa);
911
912 /*
913 * Fallback to the default domain if kmalloc() failed.
914 * See comments in partition_sched_domains().
915 */
916 if (doms == NULL)
917 ndoms = 1;
918
919 *domains = doms;
920 *attributes = dattr;
921 return ndoms;
922 }
923
update_tasks_root_domain(struct cpuset * cs)924 static void update_tasks_root_domain(struct cpuset *cs)
925 {
926 struct css_task_iter it;
927 struct task_struct *task;
928
929 css_task_iter_start(&cs->css, 0, &it);
930
931 while ((task = css_task_iter_next(&it)))
932 dl_add_task_root_domain(task);
933
934 css_task_iter_end(&it);
935 }
936
rebuild_root_domains(void)937 static void rebuild_root_domains(void)
938 {
939 struct cpuset *cs = NULL;
940 struct cgroup_subsys_state *pos_css;
941
942 percpu_rwsem_assert_held(&cpuset_rwsem);
943 lockdep_assert_cpus_held();
944 lockdep_assert_held(&sched_domains_mutex);
945
946 rcu_read_lock();
947
948 /*
949 * Clear default root domain DL accounting, it will be computed again
950 * if a task belongs to it.
951 */
952 dl_clear_root_domain(&def_root_domain);
953
954 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
955
956 if (cpumask_empty(cs->effective_cpus)) {
957 pos_css = css_rightmost_descendant(pos_css);
958 continue;
959 }
960
961 css_get(&cs->css);
962
963 rcu_read_unlock();
964
965 update_tasks_root_domain(cs);
966
967 rcu_read_lock();
968 css_put(&cs->css);
969 }
970 rcu_read_unlock();
971 }
972
973 static void
partition_and_rebuild_sched_domains(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)974 partition_and_rebuild_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
975 struct sched_domain_attr *dattr_new)
976 {
977 mutex_lock(&sched_domains_mutex);
978 partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
979 rebuild_root_domains();
980 mutex_unlock(&sched_domains_mutex);
981 }
982
983 /*
984 * Rebuild scheduler domains.
985 *
986 * If the flag 'sched_load_balance' of any cpuset with non-empty
987 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
988 * which has that flag enabled, or if any cpuset with a non-empty
989 * 'cpus' is removed, then call this routine to rebuild the
990 * scheduler's dynamic sched domains.
991 *
992 * Call with cpuset_mutex held. Takes get_online_cpus().
993 */
rebuild_sched_domains_locked(void)994 static void rebuild_sched_domains_locked(void)
995 {
996 struct cgroup_subsys_state *pos_css;
997 struct sched_domain_attr *attr;
998 cpumask_var_t *doms;
999 struct cpuset *cs;
1000 int ndoms;
1001
1002 lockdep_assert_cpus_held();
1003 percpu_rwsem_assert_held(&cpuset_rwsem);
1004
1005 /*
1006 * If we have raced with CPU hotplug, return early to avoid
1007 * passing doms with offlined cpu to partition_sched_domains().
1008 * Anyways, cpuset_hotplug_workfn() will rebuild sched domains.
1009 *
1010 * With no CPUs in any subpartitions, top_cpuset's effective CPUs
1011 * should be the same as the active CPUs, so checking only top_cpuset
1012 * is enough to detect racing CPU offlines.
1013 */
1014 if (!top_cpuset.nr_subparts_cpus &&
1015 !cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask))
1016 return;
1017
1018 /*
1019 * With subpartition CPUs, however, the effective CPUs of a partition
1020 * root should be only a subset of the active CPUs. Since a CPU in any
1021 * partition root could be offlined, all must be checked.
1022 */
1023 if (top_cpuset.nr_subparts_cpus) {
1024 rcu_read_lock();
1025 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
1026 if (!is_partition_root(cs)) {
1027 pos_css = css_rightmost_descendant(pos_css);
1028 continue;
1029 }
1030 if (!cpumask_subset(cs->effective_cpus,
1031 cpu_active_mask)) {
1032 rcu_read_unlock();
1033 return;
1034 }
1035 }
1036 rcu_read_unlock();
1037 }
1038
1039 /* Generate domain masks and attrs */
1040 ndoms = generate_sched_domains(&doms, &attr);
1041
1042 /* Have scheduler rebuild the domains */
1043 partition_and_rebuild_sched_domains(ndoms, doms, attr);
1044 }
1045 #else /* !CONFIG_SMP */
rebuild_sched_domains_locked(void)1046 static void rebuild_sched_domains_locked(void)
1047 {
1048 }
1049 #endif /* CONFIG_SMP */
1050
rebuild_sched_domains(void)1051 void rebuild_sched_domains(void)
1052 {
1053 get_online_cpus();
1054 percpu_down_write(&cpuset_rwsem);
1055 rebuild_sched_domains_locked();
1056 percpu_up_write(&cpuset_rwsem);
1057 put_online_cpus();
1058 }
1059
1060 /**
1061 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
1062 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
1063 *
1064 * Iterate through each task of @cs updating its cpus_allowed to the
1065 * effective cpuset's. As this function is called with cpuset_mutex held,
1066 * cpuset membership stays stable.
1067 */
update_tasks_cpumask(struct cpuset * cs)1068 static void update_tasks_cpumask(struct cpuset *cs)
1069 {
1070 struct css_task_iter it;
1071 struct task_struct *task;
1072
1073 css_task_iter_start(&cs->css, 0, &it);
1074 while ((task = css_task_iter_next(&it)))
1075 set_cpus_allowed_ptr(task, cs->effective_cpus);
1076 css_task_iter_end(&it);
1077 }
1078
1079 /**
1080 * compute_effective_cpumask - Compute the effective cpumask of the cpuset
1081 * @new_cpus: the temp variable for the new effective_cpus mask
1082 * @cs: the cpuset the need to recompute the new effective_cpus mask
1083 * @parent: the parent cpuset
1084 *
1085 * If the parent has subpartition CPUs, include them in the list of
1086 * allowable CPUs in computing the new effective_cpus mask. Since offlined
1087 * CPUs are not removed from subparts_cpus, we have to use cpu_active_mask
1088 * to mask those out.
1089 */
compute_effective_cpumask(struct cpumask * new_cpus,struct cpuset * cs,struct cpuset * parent)1090 static void compute_effective_cpumask(struct cpumask *new_cpus,
1091 struct cpuset *cs, struct cpuset *parent)
1092 {
1093 if (parent->nr_subparts_cpus) {
1094 cpumask_or(new_cpus, parent->effective_cpus,
1095 parent->subparts_cpus);
1096 cpumask_and(new_cpus, new_cpus, cs->cpus_requested);
1097 cpumask_and(new_cpus, new_cpus, cpu_active_mask);
1098 } else {
1099 cpumask_and(new_cpus, cs->cpus_requested,
1100 parent->effective_cpus);
1101 }
1102 }
1103
1104 /*
1105 * Commands for update_parent_subparts_cpumask
1106 */
1107 enum subparts_cmd {
1108 partcmd_enable, /* Enable partition root */
1109 partcmd_disable, /* Disable partition root */
1110 partcmd_update, /* Update parent's subparts_cpus */
1111 };
1112
1113 /**
1114 * update_parent_subparts_cpumask - update subparts_cpus mask of parent cpuset
1115 * @cpuset: The cpuset that requests change in partition root state
1116 * @cmd: Partition root state change command
1117 * @newmask: Optional new cpumask for partcmd_update
1118 * @tmp: Temporary addmask and delmask
1119 * Return: 0, 1 or an error code
1120 *
1121 * For partcmd_enable, the cpuset is being transformed from a non-partition
1122 * root to a partition root. The cpus_allowed mask of the given cpuset will
1123 * be put into parent's subparts_cpus and taken away from parent's
1124 * effective_cpus. The function will return 0 if all the CPUs listed in
1125 * cpus_allowed can be granted or an error code will be returned.
1126 *
1127 * For partcmd_disable, the cpuset is being transofrmed from a partition
1128 * root back to a non-partition root. Any CPUs in cpus_allowed that are in
1129 * parent's subparts_cpus will be taken away from that cpumask and put back
1130 * into parent's effective_cpus. 0 should always be returned.
1131 *
1132 * For partcmd_update, if the optional newmask is specified, the cpu
1133 * list is to be changed from cpus_allowed to newmask. Otherwise,
1134 * cpus_allowed is assumed to remain the same. The cpuset should either
1135 * be a partition root or an invalid partition root. The partition root
1136 * state may change if newmask is NULL and none of the requested CPUs can
1137 * be granted by the parent. The function will return 1 if changes to
1138 * parent's subparts_cpus and effective_cpus happen or 0 otherwise.
1139 * Error code should only be returned when newmask is non-NULL.
1140 *
1141 * The partcmd_enable and partcmd_disable commands are used by
1142 * update_prstate(). The partcmd_update command is used by
1143 * update_cpumasks_hier() with newmask NULL and update_cpumask() with
1144 * newmask set.
1145 *
1146 * The checking is more strict when enabling partition root than the
1147 * other two commands.
1148 *
1149 * Because of the implicit cpu exclusive nature of a partition root,
1150 * cpumask changes that violates the cpu exclusivity rule will not be
1151 * permitted when checked by validate_change(). The validate_change()
1152 * function will also prevent any changes to the cpu list if it is not
1153 * a superset of children's cpu lists.
1154 */
update_parent_subparts_cpumask(struct cpuset * cpuset,int cmd,struct cpumask * newmask,struct tmpmasks * tmp)1155 static int update_parent_subparts_cpumask(struct cpuset *cpuset, int cmd,
1156 struct cpumask *newmask,
1157 struct tmpmasks *tmp)
1158 {
1159 struct cpuset *parent = parent_cs(cpuset);
1160 int adding; /* Moving cpus from effective_cpus to subparts_cpus */
1161 int deleting; /* Moving cpus from subparts_cpus to effective_cpus */
1162 int new_prs;
1163 bool part_error = false; /* Partition error? */
1164
1165 percpu_rwsem_assert_held(&cpuset_rwsem);
1166
1167 /*
1168 * The parent must be a partition root.
1169 * The new cpumask, if present, or the current cpus_allowed must
1170 * not be empty.
1171 */
1172 if (!is_partition_root(parent) ||
1173 (newmask && cpumask_empty(newmask)) ||
1174 (!newmask && cpumask_empty(cpuset->cpus_allowed)))
1175 return -EINVAL;
1176
1177 /*
1178 * Enabling/disabling partition root is not allowed if there are
1179 * online children.
1180 */
1181 if ((cmd != partcmd_update) && css_has_online_children(&cpuset->css))
1182 return -EBUSY;
1183
1184 /*
1185 * Enabling partition root is not allowed if not all the CPUs
1186 * can be granted from parent's effective_cpus or at least one
1187 * CPU will be left after that.
1188 */
1189 if ((cmd == partcmd_enable) &&
1190 (!cpumask_subset(cpuset->cpus_allowed, parent->effective_cpus) ||
1191 cpumask_equal(cpuset->cpus_allowed, parent->effective_cpus)))
1192 return -EINVAL;
1193
1194 /*
1195 * A cpumask update cannot make parent's effective_cpus become empty.
1196 */
1197 adding = deleting = false;
1198 new_prs = cpuset->partition_root_state;
1199 if (cmd == partcmd_enable) {
1200 cpumask_copy(tmp->addmask, cpuset->cpus_allowed);
1201 adding = true;
1202 } else if (cmd == partcmd_disable) {
1203 deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed,
1204 parent->subparts_cpus);
1205 } else if (newmask) {
1206 /*
1207 * partcmd_update with newmask:
1208 *
1209 * delmask = cpus_allowed & ~newmask & parent->subparts_cpus
1210 * addmask = newmask & parent->effective_cpus
1211 * & ~parent->subparts_cpus
1212 */
1213 cpumask_andnot(tmp->delmask, cpuset->cpus_allowed, newmask);
1214 deleting = cpumask_and(tmp->delmask, tmp->delmask,
1215 parent->subparts_cpus);
1216
1217 cpumask_and(tmp->addmask, newmask, parent->effective_cpus);
1218 adding = cpumask_andnot(tmp->addmask, tmp->addmask,
1219 parent->subparts_cpus);
1220 /*
1221 * Return error if the new effective_cpus could become empty.
1222 */
1223 if (adding &&
1224 cpumask_equal(parent->effective_cpus, tmp->addmask)) {
1225 if (!deleting)
1226 return -EINVAL;
1227 /*
1228 * As some of the CPUs in subparts_cpus might have
1229 * been offlined, we need to compute the real delmask
1230 * to confirm that.
1231 */
1232 if (!cpumask_and(tmp->addmask, tmp->delmask,
1233 cpu_active_mask))
1234 return -EINVAL;
1235 cpumask_copy(tmp->addmask, parent->effective_cpus);
1236 }
1237 } else {
1238 /*
1239 * partcmd_update w/o newmask:
1240 *
1241 * addmask = cpus_allowed & parent->effective_cpus
1242 *
1243 * Note that parent's subparts_cpus may have been
1244 * pre-shrunk in case there is a change in the cpu list.
1245 * So no deletion is needed.
1246 */
1247 adding = cpumask_and(tmp->addmask, cpuset->cpus_allowed,
1248 parent->effective_cpus);
1249 part_error = cpumask_equal(tmp->addmask,
1250 parent->effective_cpus);
1251 }
1252
1253 if (cmd == partcmd_update) {
1254 int prev_prs = cpuset->partition_root_state;
1255
1256 /*
1257 * Check for possible transition between PRS_ENABLED
1258 * and PRS_ERROR.
1259 */
1260 switch (cpuset->partition_root_state) {
1261 case PRS_ENABLED:
1262 if (part_error)
1263 new_prs = PRS_ERROR;
1264 break;
1265 case PRS_ERROR:
1266 if (!part_error)
1267 new_prs = PRS_ENABLED;
1268 break;
1269 }
1270 /*
1271 * Set part_error if previously in invalid state.
1272 */
1273 part_error = (prev_prs == PRS_ERROR);
1274 }
1275
1276 if (!part_error && (new_prs == PRS_ERROR))
1277 return 0; /* Nothing need to be done */
1278
1279 if (new_prs == PRS_ERROR) {
1280 /*
1281 * Remove all its cpus from parent's subparts_cpus.
1282 */
1283 adding = false;
1284 deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed,
1285 parent->subparts_cpus);
1286 }
1287
1288 if (!adding && !deleting && (new_prs == cpuset->partition_root_state))
1289 return 0;
1290
1291 /*
1292 * Change the parent's subparts_cpus.
1293 * Newly added CPUs will be removed from effective_cpus and
1294 * newly deleted ones will be added back to effective_cpus.
1295 */
1296 spin_lock_irq(&callback_lock);
1297 if (adding) {
1298 cpumask_or(parent->subparts_cpus,
1299 parent->subparts_cpus, tmp->addmask);
1300 cpumask_andnot(parent->effective_cpus,
1301 parent->effective_cpus, tmp->addmask);
1302 }
1303 if (deleting) {
1304 cpumask_andnot(parent->subparts_cpus,
1305 parent->subparts_cpus, tmp->delmask);
1306 /*
1307 * Some of the CPUs in subparts_cpus might have been offlined.
1308 */
1309 cpumask_and(tmp->delmask, tmp->delmask, cpu_active_mask);
1310 cpumask_or(parent->effective_cpus,
1311 parent->effective_cpus, tmp->delmask);
1312 }
1313
1314 parent->nr_subparts_cpus = cpumask_weight(parent->subparts_cpus);
1315
1316 if (cpuset->partition_root_state != new_prs)
1317 cpuset->partition_root_state = new_prs;
1318 spin_unlock_irq(&callback_lock);
1319
1320 return cmd == partcmd_update;
1321 }
1322
1323 /*
1324 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
1325 * @cs: the cpuset to consider
1326 * @tmp: temp variables for calculating effective_cpus & partition setup
1327 *
1328 * When congifured cpumask is changed, the effective cpumasks of this cpuset
1329 * and all its descendants need to be updated.
1330 *
1331 * On legacy hierachy, effective_cpus will be the same with cpu_allowed.
1332 *
1333 * Called with cpuset_mutex held
1334 */
update_cpumasks_hier(struct cpuset * cs,struct tmpmasks * tmp)1335 static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp)
1336 {
1337 struct cpuset *cp;
1338 struct cgroup_subsys_state *pos_css;
1339 bool need_rebuild_sched_domains = false;
1340 int new_prs;
1341
1342 rcu_read_lock();
1343 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1344 struct cpuset *parent = parent_cs(cp);
1345
1346 compute_effective_cpumask(tmp->new_cpus, cp, parent);
1347
1348 /*
1349 * If it becomes empty, inherit the effective mask of the
1350 * parent, which is guaranteed to have some CPUs.
1351 */
1352 if (is_in_v2_mode() && cpumask_empty(tmp->new_cpus)) {
1353 cpumask_copy(tmp->new_cpus, parent->effective_cpus);
1354 if (!cp->use_parent_ecpus) {
1355 cp->use_parent_ecpus = true;
1356 parent->child_ecpus_count++;
1357 }
1358 } else if (cp->use_parent_ecpus) {
1359 cp->use_parent_ecpus = false;
1360 WARN_ON_ONCE(!parent->child_ecpus_count);
1361 parent->child_ecpus_count--;
1362 }
1363
1364 /*
1365 * Skip the whole subtree if the cpumask remains the same
1366 * and has no partition root state.
1367 */
1368 if (!cp->partition_root_state &&
1369 cpumask_equal(tmp->new_cpus, cp->effective_cpus)) {
1370 pos_css = css_rightmost_descendant(pos_css);
1371 continue;
1372 }
1373
1374 /*
1375 * update_parent_subparts_cpumask() should have been called
1376 * for cs already in update_cpumask(). We should also call
1377 * update_tasks_cpumask() again for tasks in the parent
1378 * cpuset if the parent's subparts_cpus changes.
1379 */
1380 new_prs = cp->partition_root_state;
1381 if ((cp != cs) && new_prs) {
1382 switch (parent->partition_root_state) {
1383 case PRS_DISABLED:
1384 /*
1385 * If parent is not a partition root or an
1386 * invalid partition root, clear its state
1387 * and its CS_CPU_EXCLUSIVE flag.
1388 */
1389 WARN_ON_ONCE(cp->partition_root_state
1390 != PRS_ERROR);
1391 new_prs = PRS_DISABLED;
1392
1393 /*
1394 * clear_bit() is an atomic operation and
1395 * readers aren't interested in the state
1396 * of CS_CPU_EXCLUSIVE anyway. So we can
1397 * just update the flag without holding
1398 * the callback_lock.
1399 */
1400 clear_bit(CS_CPU_EXCLUSIVE, &cp->flags);
1401 break;
1402
1403 case PRS_ENABLED:
1404 if (update_parent_subparts_cpumask(cp, partcmd_update, NULL, tmp))
1405 update_tasks_cpumask(parent);
1406 break;
1407
1408 case PRS_ERROR:
1409 /*
1410 * When parent is invalid, it has to be too.
1411 */
1412 new_prs = PRS_ERROR;
1413 break;
1414 }
1415 }
1416
1417 if (!css_tryget_online(&cp->css))
1418 continue;
1419 rcu_read_unlock();
1420
1421 spin_lock_irq(&callback_lock);
1422
1423 cpumask_copy(cp->effective_cpus, tmp->new_cpus);
1424 if (cp->nr_subparts_cpus && (new_prs != PRS_ENABLED)) {
1425 cp->nr_subparts_cpus = 0;
1426 cpumask_clear(cp->subparts_cpus);
1427 } else if (cp->nr_subparts_cpus) {
1428 /*
1429 * Make sure that effective_cpus & subparts_cpus
1430 * are mutually exclusive.
1431 *
1432 * In the unlikely event that effective_cpus
1433 * becomes empty. we clear cp->nr_subparts_cpus and
1434 * let its child partition roots to compete for
1435 * CPUs again.
1436 */
1437 cpumask_andnot(cp->effective_cpus, cp->effective_cpus,
1438 cp->subparts_cpus);
1439 if (cpumask_empty(cp->effective_cpus)) {
1440 cpumask_copy(cp->effective_cpus, tmp->new_cpus);
1441 cpumask_clear(cp->subparts_cpus);
1442 cp->nr_subparts_cpus = 0;
1443 } else if (!cpumask_subset(cp->subparts_cpus,
1444 tmp->new_cpus)) {
1445 cpumask_andnot(cp->subparts_cpus,
1446 cp->subparts_cpus, tmp->new_cpus);
1447 cp->nr_subparts_cpus
1448 = cpumask_weight(cp->subparts_cpus);
1449 }
1450 }
1451
1452 if (new_prs != cp->partition_root_state)
1453 cp->partition_root_state = new_prs;
1454
1455 spin_unlock_irq(&callback_lock);
1456
1457 WARN_ON(!is_in_v2_mode() &&
1458 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
1459
1460 update_tasks_cpumask(cp);
1461
1462 /*
1463 * On legacy hierarchy, if the effective cpumask of any non-
1464 * empty cpuset is changed, we need to rebuild sched domains.
1465 * On default hierarchy, the cpuset needs to be a partition
1466 * root as well.
1467 */
1468 if (!cpumask_empty(cp->cpus_allowed) &&
1469 is_sched_load_balance(cp) &&
1470 (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
1471 is_partition_root(cp)))
1472 need_rebuild_sched_domains = true;
1473
1474 rcu_read_lock();
1475 css_put(&cp->css);
1476 }
1477 rcu_read_unlock();
1478
1479 if (need_rebuild_sched_domains)
1480 rebuild_sched_domains_locked();
1481 }
1482
1483 /**
1484 * update_sibling_cpumasks - Update siblings cpumasks
1485 * @parent: Parent cpuset
1486 * @cs: Current cpuset
1487 * @tmp: Temp variables
1488 */
update_sibling_cpumasks(struct cpuset * parent,struct cpuset * cs,struct tmpmasks * tmp)1489 static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
1490 struct tmpmasks *tmp)
1491 {
1492 struct cpuset *sibling;
1493 struct cgroup_subsys_state *pos_css;
1494
1495 /*
1496 * Check all its siblings and call update_cpumasks_hier()
1497 * if their use_parent_ecpus flag is set in order for them
1498 * to use the right effective_cpus value.
1499 */
1500 rcu_read_lock();
1501 cpuset_for_each_child(sibling, pos_css, parent) {
1502 if (sibling == cs)
1503 continue;
1504 if (!sibling->use_parent_ecpus)
1505 continue;
1506
1507 update_cpumasks_hier(sibling, tmp);
1508 }
1509 rcu_read_unlock();
1510 }
1511
1512 /**
1513 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
1514 * @cs: the cpuset to consider
1515 * @trialcs: trial cpuset
1516 * @buf: buffer of cpu numbers written to this cpuset
1517 */
update_cpumask(struct cpuset * cs,struct cpuset * trialcs,const char * buf)1518 static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
1519 const char *buf)
1520 {
1521 int retval;
1522 struct tmpmasks tmp;
1523
1524 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
1525 if (cs == &top_cpuset)
1526 return -EACCES;
1527
1528 /*
1529 * An empty cpus_requested is ok only if the cpuset has no tasks.
1530 * Since cpulist_parse() fails on an empty mask, we special case
1531 * that parsing. The validate_change() call ensures that cpusets
1532 * with tasks have cpus.
1533 */
1534 if (!*buf) {
1535 cpumask_clear(trialcs->cpus_requested);
1536 } else {
1537 retval = cpulist_parse(buf, trialcs->cpus_requested);
1538 if (retval < 0)
1539 return retval;
1540 }
1541
1542 if (!cpumask_subset(trialcs->cpus_requested, cpu_present_mask))
1543 return -EINVAL;
1544
1545 cpumask_and(trialcs->cpus_allowed, trialcs->cpus_requested,
1546 cpu_active_mask);
1547
1548 /* Nothing to do if the cpus didn't change */
1549 if (cpumask_equal(cs->cpus_requested, trialcs->cpus_requested))
1550 return 0;
1551
1552 retval = validate_change(cs, trialcs);
1553 if (retval < 0)
1554 return retval;
1555
1556 #ifdef CONFIG_CPUMASK_OFFSTACK
1557 /*
1558 * Use the cpumasks in trialcs for tmpmasks when they are pointers
1559 * to allocated cpumasks.
1560 */
1561 tmp.addmask = trialcs->subparts_cpus;
1562 tmp.delmask = trialcs->effective_cpus;
1563 tmp.new_cpus = trialcs->cpus_allowed;
1564 #endif
1565
1566 if (cs->partition_root_state) {
1567 /* Cpumask of a partition root cannot be empty */
1568 if (cpumask_empty(trialcs->cpus_allowed))
1569 return -EINVAL;
1570 if (update_parent_subparts_cpumask(cs, partcmd_update,
1571 trialcs->cpus_allowed, &tmp) < 0)
1572 return -EINVAL;
1573 }
1574
1575 spin_lock_irq(&callback_lock);
1576 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
1577 cpumask_copy(cs->cpus_requested, trialcs->cpus_requested);
1578
1579 /*
1580 * Make sure that subparts_cpus is a subset of cpus_allowed.
1581 */
1582 if (cs->nr_subparts_cpus) {
1583 cpumask_and(cs->subparts_cpus, cs->subparts_cpus, cs->cpus_allowed);
1584 cs->nr_subparts_cpus = cpumask_weight(cs->subparts_cpus);
1585 }
1586 spin_unlock_irq(&callback_lock);
1587
1588 update_cpumasks_hier(cs, &tmp);
1589
1590 if (cs->partition_root_state) {
1591 struct cpuset *parent = parent_cs(cs);
1592
1593 /*
1594 * For partition root, update the cpumasks of sibling
1595 * cpusets if they use parent's effective_cpus.
1596 */
1597 if (parent->child_ecpus_count)
1598 update_sibling_cpumasks(parent, cs, &tmp);
1599 }
1600 return 0;
1601 }
1602
1603 /*
1604 * Migrate memory region from one set of nodes to another. This is
1605 * performed asynchronously as it can be called from process migration path
1606 * holding locks involved in process management. All mm migrations are
1607 * performed in the queued order and can be waited for by flushing
1608 * cpuset_migrate_mm_wq.
1609 */
1610
1611 struct cpuset_migrate_mm_work {
1612 struct work_struct work;
1613 struct mm_struct *mm;
1614 nodemask_t from;
1615 nodemask_t to;
1616 };
1617
cpuset_migrate_mm_workfn(struct work_struct * work)1618 static void cpuset_migrate_mm_workfn(struct work_struct *work)
1619 {
1620 struct cpuset_migrate_mm_work *mwork =
1621 container_of(work, struct cpuset_migrate_mm_work, work);
1622
1623 /* on a wq worker, no need to worry about %current's mems_allowed */
1624 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
1625 mmput(mwork->mm);
1626 kfree(mwork);
1627 }
1628
cpuset_migrate_mm(struct mm_struct * mm,const nodemask_t * from,const nodemask_t * to)1629 static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1630 const nodemask_t *to)
1631 {
1632 struct cpuset_migrate_mm_work *mwork;
1633
1634 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL);
1635 if (mwork) {
1636 mwork->mm = mm;
1637 mwork->from = *from;
1638 mwork->to = *to;
1639 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn);
1640 queue_work(cpuset_migrate_mm_wq, &mwork->work);
1641 } else {
1642 mmput(mm);
1643 }
1644 }
1645
cpuset_post_attach(void)1646 static void cpuset_post_attach(void)
1647 {
1648 flush_workqueue(cpuset_migrate_mm_wq);
1649 }
1650
1651 /*
1652 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
1653 * @tsk: the task to change
1654 * @newmems: new nodes that the task will be set
1655 *
1656 * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed
1657 * and rebind an eventual tasks' mempolicy. If the task is allocating in
1658 * parallel, it might temporarily see an empty intersection, which results in
1659 * a seqlock check and retry before OOM or allocation failure.
1660 */
cpuset_change_task_nodemask(struct task_struct * tsk,nodemask_t * newmems)1661 static void cpuset_change_task_nodemask(struct task_struct *tsk,
1662 nodemask_t *newmems)
1663 {
1664 task_lock(tsk);
1665
1666 local_irq_disable();
1667 write_seqcount_begin(&tsk->mems_allowed_seq);
1668
1669 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
1670 mpol_rebind_task(tsk, newmems);
1671 tsk->mems_allowed = *newmems;
1672
1673 write_seqcount_end(&tsk->mems_allowed_seq);
1674 local_irq_enable();
1675
1676 task_unlock(tsk);
1677 }
1678
1679 static void *cpuset_being_rebound;
1680
1681 /**
1682 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1683 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1684 *
1685 * Iterate through each task of @cs updating its mems_allowed to the
1686 * effective cpuset's. As this function is called with cpuset_mutex held,
1687 * cpuset membership stays stable.
1688 */
update_tasks_nodemask(struct cpuset * cs)1689 static void update_tasks_nodemask(struct cpuset *cs)
1690 {
1691 static nodemask_t newmems; /* protected by cpuset_mutex */
1692 struct css_task_iter it;
1693 struct task_struct *task;
1694
1695 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
1696
1697 guarantee_online_mems(cs, &newmems);
1698
1699 /*
1700 * The mpol_rebind_mm() call takes mmap_lock, which we couldn't
1701 * take while holding tasklist_lock. Forks can happen - the
1702 * mpol_dup() cpuset_being_rebound check will catch such forks,
1703 * and rebind their vma mempolicies too. Because we still hold
1704 * the global cpuset_mutex, we know that no other rebind effort
1705 * will be contending for the global variable cpuset_being_rebound.
1706 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
1707 * is idempotent. Also migrate pages in each mm to new nodes.
1708 */
1709 css_task_iter_start(&cs->css, 0, &it);
1710 while ((task = css_task_iter_next(&it))) {
1711 struct mm_struct *mm;
1712 bool migrate;
1713
1714 cpuset_change_task_nodemask(task, &newmems);
1715
1716 mm = get_task_mm(task);
1717 if (!mm)
1718 continue;
1719
1720 migrate = is_memory_migrate(cs);
1721
1722 mpol_rebind_mm(mm, &cs->mems_allowed);
1723 if (migrate)
1724 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
1725 else
1726 mmput(mm);
1727 }
1728 css_task_iter_end(&it);
1729
1730 /*
1731 * All the tasks' nodemasks have been updated, update
1732 * cs->old_mems_allowed.
1733 */
1734 cs->old_mems_allowed = newmems;
1735
1736 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
1737 cpuset_being_rebound = NULL;
1738 }
1739
1740 /*
1741 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1742 * @cs: the cpuset to consider
1743 * @new_mems: a temp variable for calculating new effective_mems
1744 *
1745 * When configured nodemask is changed, the effective nodemasks of this cpuset
1746 * and all its descendants need to be updated.
1747 *
1748 * On legacy hiearchy, effective_mems will be the same with mems_allowed.
1749 *
1750 * Called with cpuset_mutex held
1751 */
update_nodemasks_hier(struct cpuset * cs,nodemask_t * new_mems)1752 static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
1753 {
1754 struct cpuset *cp;
1755 struct cgroup_subsys_state *pos_css;
1756
1757 rcu_read_lock();
1758 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1759 struct cpuset *parent = parent_cs(cp);
1760
1761 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
1762
1763 /*
1764 * If it becomes empty, inherit the effective mask of the
1765 * parent, which is guaranteed to have some MEMs.
1766 */
1767 if (is_in_v2_mode() && nodes_empty(*new_mems))
1768 *new_mems = parent->effective_mems;
1769
1770 /* Skip the whole subtree if the nodemask remains the same. */
1771 if (nodes_equal(*new_mems, cp->effective_mems)) {
1772 pos_css = css_rightmost_descendant(pos_css);
1773 continue;
1774 }
1775
1776 if (!css_tryget_online(&cp->css))
1777 continue;
1778 rcu_read_unlock();
1779
1780 spin_lock_irq(&callback_lock);
1781 cp->effective_mems = *new_mems;
1782 spin_unlock_irq(&callback_lock);
1783
1784 WARN_ON(!is_in_v2_mode() &&
1785 !nodes_equal(cp->mems_allowed, cp->effective_mems));
1786
1787 update_tasks_nodemask(cp);
1788
1789 rcu_read_lock();
1790 css_put(&cp->css);
1791 }
1792 rcu_read_unlock();
1793 }
1794
1795 /*
1796 * Handle user request to change the 'mems' memory placement
1797 * of a cpuset. Needs to validate the request, update the
1798 * cpusets mems_allowed, and for each task in the cpuset,
1799 * update mems_allowed and rebind task's mempolicy and any vma
1800 * mempolicies and if the cpuset is marked 'memory_migrate',
1801 * migrate the tasks pages to the new memory.
1802 *
1803 * Call with cpuset_mutex held. May take callback_lock during call.
1804 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1805 * lock each such tasks mm->mmap_lock, scan its vma's and rebind
1806 * their mempolicies to the cpusets new mems_allowed.
1807 */
update_nodemask(struct cpuset * cs,struct cpuset * trialcs,const char * buf)1808 static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1809 const char *buf)
1810 {
1811 int retval;
1812
1813 /*
1814 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
1815 * it's read-only
1816 */
1817 if (cs == &top_cpuset) {
1818 retval = -EACCES;
1819 goto done;
1820 }
1821
1822 /*
1823 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1824 * Since nodelist_parse() fails on an empty mask, we special case
1825 * that parsing. The validate_change() call ensures that cpusets
1826 * with tasks have memory.
1827 */
1828 if (!*buf) {
1829 nodes_clear(trialcs->mems_allowed);
1830 } else {
1831 retval = nodelist_parse(buf, trialcs->mems_allowed);
1832 if (retval < 0)
1833 goto done;
1834
1835 if (!nodes_subset(trialcs->mems_allowed,
1836 top_cpuset.mems_allowed)) {
1837 retval = -EINVAL;
1838 goto done;
1839 }
1840 }
1841
1842 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) {
1843 retval = 0; /* Too easy - nothing to do */
1844 goto done;
1845 }
1846 retval = validate_change(cs, trialcs);
1847 if (retval < 0)
1848 goto done;
1849
1850 spin_lock_irq(&callback_lock);
1851 cs->mems_allowed = trialcs->mems_allowed;
1852 spin_unlock_irq(&callback_lock);
1853
1854 /* use trialcs->mems_allowed as a temp variable */
1855 update_nodemasks_hier(cs, &trialcs->mems_allowed);
1856 done:
1857 return retval;
1858 }
1859
current_cpuset_is_being_rebound(void)1860 bool current_cpuset_is_being_rebound(void)
1861 {
1862 bool ret;
1863
1864 rcu_read_lock();
1865 ret = task_cs(current) == cpuset_being_rebound;
1866 rcu_read_unlock();
1867
1868 return ret;
1869 }
1870
update_relax_domain_level(struct cpuset * cs,s64 val)1871 static int update_relax_domain_level(struct cpuset *cs, s64 val)
1872 {
1873 #ifdef CONFIG_SMP
1874 if (val < -1 || val >= sched_domain_level_max)
1875 return -EINVAL;
1876 #endif
1877
1878 if (val != cs->relax_domain_level) {
1879 cs->relax_domain_level = val;
1880 if (!cpumask_empty(cs->cpus_allowed) &&
1881 is_sched_load_balance(cs))
1882 rebuild_sched_domains_locked();
1883 }
1884
1885 return 0;
1886 }
1887
1888 /**
1889 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1890 * @cs: the cpuset in which each task's spread flags needs to be changed
1891 *
1892 * Iterate through each task of @cs updating its spread flags. As this
1893 * function is called with cpuset_mutex held, cpuset membership stays
1894 * stable.
1895 */
update_tasks_flags(struct cpuset * cs)1896 static void update_tasks_flags(struct cpuset *cs)
1897 {
1898 struct css_task_iter it;
1899 struct task_struct *task;
1900
1901 css_task_iter_start(&cs->css, 0, &it);
1902 while ((task = css_task_iter_next(&it)))
1903 cpuset_update_task_spread_flag(cs, task);
1904 css_task_iter_end(&it);
1905 }
1906
1907 /*
1908 * update_flag - read a 0 or a 1 in a file and update associated flag
1909 * bit: the bit to update (see cpuset_flagbits_t)
1910 * cs: the cpuset to update
1911 * turning_on: whether the flag is being set or cleared
1912 *
1913 * Call with cpuset_mutex held.
1914 */
1915
update_flag(cpuset_flagbits_t bit,struct cpuset * cs,int turning_on)1916 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1917 int turning_on)
1918 {
1919 struct cpuset *trialcs;
1920 int balance_flag_changed;
1921 int spread_flag_changed;
1922 int err;
1923
1924 trialcs = alloc_trial_cpuset(cs);
1925 if (!trialcs)
1926 return -ENOMEM;
1927
1928 if (turning_on)
1929 set_bit(bit, &trialcs->flags);
1930 else
1931 clear_bit(bit, &trialcs->flags);
1932
1933 err = validate_change(cs, trialcs);
1934 if (err < 0)
1935 goto out;
1936
1937 balance_flag_changed = (is_sched_load_balance(cs) !=
1938 is_sched_load_balance(trialcs));
1939
1940 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1941 || (is_spread_page(cs) != is_spread_page(trialcs)));
1942
1943 spin_lock_irq(&callback_lock);
1944 cs->flags = trialcs->flags;
1945 spin_unlock_irq(&callback_lock);
1946
1947 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
1948 rebuild_sched_domains_locked();
1949
1950 if (spread_flag_changed)
1951 update_tasks_flags(cs);
1952 out:
1953 free_cpuset(trialcs);
1954 return err;
1955 }
1956
1957 /*
1958 * update_prstate - update partititon_root_state
1959 * cs: the cpuset to update
1960 * new_prs: new partition root state
1961 *
1962 * Call with cpuset_mutex held.
1963 */
update_prstate(struct cpuset * cs,int new_prs)1964 static int update_prstate(struct cpuset *cs, int new_prs)
1965 {
1966 int err, old_prs = cs->partition_root_state;
1967 struct cpuset *parent = parent_cs(cs);
1968 struct tmpmasks tmpmask;
1969
1970 if (old_prs == new_prs)
1971 return 0;
1972
1973 /*
1974 * Cannot force a partial or invalid partition root to a full
1975 * partition root.
1976 */
1977 if (new_prs && (old_prs == PRS_ERROR))
1978 return -EINVAL;
1979
1980 if (alloc_cpumasks(NULL, &tmpmask))
1981 return -ENOMEM;
1982
1983 err = -EINVAL;
1984 if (!old_prs) {
1985 /*
1986 * Turning on partition root requires setting the
1987 * CS_CPU_EXCLUSIVE bit implicitly as well and cpus_allowed
1988 * cannot be NULL.
1989 */
1990 if (cpumask_empty(cs->cpus_allowed))
1991 goto out;
1992
1993 err = update_flag(CS_CPU_EXCLUSIVE, cs, 1);
1994 if (err)
1995 goto out;
1996
1997 err = update_parent_subparts_cpumask(cs, partcmd_enable,
1998 NULL, &tmpmask);
1999 if (err) {
2000 update_flag(CS_CPU_EXCLUSIVE, cs, 0);
2001 goto out;
2002 }
2003 } else {
2004 /*
2005 * Turning off partition root will clear the
2006 * CS_CPU_EXCLUSIVE bit.
2007 */
2008 if (old_prs == PRS_ERROR) {
2009 update_flag(CS_CPU_EXCLUSIVE, cs, 0);
2010 err = 0;
2011 goto out;
2012 }
2013
2014 err = update_parent_subparts_cpumask(cs, partcmd_disable,
2015 NULL, &tmpmask);
2016 if (err)
2017 goto out;
2018
2019 /* Turning off CS_CPU_EXCLUSIVE will not return error */
2020 update_flag(CS_CPU_EXCLUSIVE, cs, 0);
2021 }
2022
2023 /*
2024 * Update cpumask of parent's tasks except when it is the top
2025 * cpuset as some system daemons cannot be mapped to other CPUs.
2026 */
2027 if (parent != &top_cpuset)
2028 update_tasks_cpumask(parent);
2029
2030 if (parent->child_ecpus_count)
2031 update_sibling_cpumasks(parent, cs, &tmpmask);
2032
2033 rebuild_sched_domains_locked();
2034 out:
2035 if (!err) {
2036 spin_lock_irq(&callback_lock);
2037 cs->partition_root_state = new_prs;
2038 spin_unlock_irq(&callback_lock);
2039 }
2040
2041 free_cpumasks(NULL, &tmpmask);
2042 return err;
2043 }
2044
2045 /*
2046 * Frequency meter - How fast is some event occurring?
2047 *
2048 * These routines manage a digitally filtered, constant time based,
2049 * event frequency meter. There are four routines:
2050 * fmeter_init() - initialize a frequency meter.
2051 * fmeter_markevent() - called each time the event happens.
2052 * fmeter_getrate() - returns the recent rate of such events.
2053 * fmeter_update() - internal routine used to update fmeter.
2054 *
2055 * A common data structure is passed to each of these routines,
2056 * which is used to keep track of the state required to manage the
2057 * frequency meter and its digital filter.
2058 *
2059 * The filter works on the number of events marked per unit time.
2060 * The filter is single-pole low-pass recursive (IIR). The time unit
2061 * is 1 second. Arithmetic is done using 32-bit integers scaled to
2062 * simulate 3 decimal digits of precision (multiplied by 1000).
2063 *
2064 * With an FM_COEF of 933, and a time base of 1 second, the filter
2065 * has a half-life of 10 seconds, meaning that if the events quit
2066 * happening, then the rate returned from the fmeter_getrate()
2067 * will be cut in half each 10 seconds, until it converges to zero.
2068 *
2069 * It is not worth doing a real infinitely recursive filter. If more
2070 * than FM_MAXTICKS ticks have elapsed since the last filter event,
2071 * just compute FM_MAXTICKS ticks worth, by which point the level
2072 * will be stable.
2073 *
2074 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
2075 * arithmetic overflow in the fmeter_update() routine.
2076 *
2077 * Given the simple 32 bit integer arithmetic used, this meter works
2078 * best for reporting rates between one per millisecond (msec) and
2079 * one per 32 (approx) seconds. At constant rates faster than one
2080 * per msec it maxes out at values just under 1,000,000. At constant
2081 * rates between one per msec, and one per second it will stabilize
2082 * to a value N*1000, where N is the rate of events per second.
2083 * At constant rates between one per second and one per 32 seconds,
2084 * it will be choppy, moving up on the seconds that have an event,
2085 * and then decaying until the next event. At rates slower than
2086 * about one in 32 seconds, it decays all the way back to zero between
2087 * each event.
2088 */
2089
2090 #define FM_COEF 933 /* coefficient for half-life of 10 secs */
2091 #define FM_MAXTICKS ((u32)99) /* useless computing more ticks than this */
2092 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
2093 #define FM_SCALE 1000 /* faux fixed point scale */
2094
2095 /* Initialize a frequency meter */
fmeter_init(struct fmeter * fmp)2096 static void fmeter_init(struct fmeter *fmp)
2097 {
2098 fmp->cnt = 0;
2099 fmp->val = 0;
2100 fmp->time = 0;
2101 spin_lock_init(&fmp->lock);
2102 }
2103
2104 /* Internal meter update - process cnt events and update value */
fmeter_update(struct fmeter * fmp)2105 static void fmeter_update(struct fmeter *fmp)
2106 {
2107 time64_t now;
2108 u32 ticks;
2109
2110 now = ktime_get_seconds();
2111 ticks = now - fmp->time;
2112
2113 if (ticks == 0)
2114 return;
2115
2116 ticks = min(FM_MAXTICKS, ticks);
2117 while (ticks-- > 0)
2118 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
2119 fmp->time = now;
2120
2121 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
2122 fmp->cnt = 0;
2123 }
2124
2125 /* Process any previous ticks, then bump cnt by one (times scale). */
fmeter_markevent(struct fmeter * fmp)2126 static void fmeter_markevent(struct fmeter *fmp)
2127 {
2128 spin_lock(&fmp->lock);
2129 fmeter_update(fmp);
2130 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
2131 spin_unlock(&fmp->lock);
2132 }
2133
2134 /* Process any previous ticks, then return current value. */
fmeter_getrate(struct fmeter * fmp)2135 static int fmeter_getrate(struct fmeter *fmp)
2136 {
2137 int val;
2138
2139 spin_lock(&fmp->lock);
2140 fmeter_update(fmp);
2141 val = fmp->val;
2142 spin_unlock(&fmp->lock);
2143 return val;
2144 }
2145
2146 static struct cpuset *cpuset_attach_old_cs;
2147
2148 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
cpuset_can_attach(struct cgroup_taskset * tset)2149 static int cpuset_can_attach(struct cgroup_taskset *tset)
2150 {
2151 struct cgroup_subsys_state *css;
2152 struct cpuset *cs;
2153 struct task_struct *task;
2154 int ret;
2155
2156 /* used later by cpuset_attach() */
2157 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
2158 cs = css_cs(css);
2159
2160 percpu_down_write(&cpuset_rwsem);
2161
2162 /* allow moving tasks into an empty cpuset if on default hierarchy */
2163 ret = -ENOSPC;
2164 if (!is_in_v2_mode() &&
2165 (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)))
2166 goto out_unlock;
2167
2168 cgroup_taskset_for_each(task, css, tset) {
2169 ret = task_can_attach(task, cs->cpus_allowed);
2170 if (ret)
2171 goto out_unlock;
2172 ret = security_task_setscheduler(task);
2173 if (ret)
2174 goto out_unlock;
2175 }
2176
2177 /*
2178 * Mark attach is in progress. This makes validate_change() fail
2179 * changes which zero cpus/mems_allowed.
2180 */
2181 cs->attach_in_progress++;
2182 ret = 0;
2183 out_unlock:
2184 percpu_up_write(&cpuset_rwsem);
2185 return ret;
2186 }
2187
cpuset_cancel_attach(struct cgroup_taskset * tset)2188 static void cpuset_cancel_attach(struct cgroup_taskset *tset)
2189 {
2190 struct cgroup_subsys_state *css;
2191
2192 cgroup_taskset_first(tset, &css);
2193
2194 percpu_down_write(&cpuset_rwsem);
2195 css_cs(css)->attach_in_progress--;
2196 percpu_up_write(&cpuset_rwsem);
2197 }
2198
2199 /*
2200 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
2201 * but we can't allocate it dynamically there. Define it global and
2202 * allocate from cpuset_init().
2203 */
2204 static cpumask_var_t cpus_attach;
2205
cpuset_attach(struct cgroup_taskset * tset)2206 static void cpuset_attach(struct cgroup_taskset *tset)
2207 {
2208 /* static buf protected by cpuset_mutex */
2209 static nodemask_t cpuset_attach_nodemask_to;
2210 struct task_struct *task;
2211 struct task_struct *leader;
2212 struct cgroup_subsys_state *css;
2213 struct cpuset *cs;
2214 struct cpuset *oldcs = cpuset_attach_old_cs;
2215
2216 cgroup_taskset_first(tset, &css);
2217 cs = css_cs(css);
2218
2219 percpu_down_write(&cpuset_rwsem);
2220
2221 /* prepare for attach */
2222 if (cs == &top_cpuset)
2223 cpumask_copy(cpus_attach, cpu_possible_mask);
2224 else
2225 guarantee_online_cpus(cs, cpus_attach);
2226
2227 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
2228
2229 cgroup_taskset_for_each(task, css, tset) {
2230 /*
2231 * can_attach beforehand should guarantee that this doesn't
2232 * fail. TODO: have a better way to handle failure here
2233 */
2234 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
2235
2236 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
2237 cpuset_update_task_spread_flag(cs, task);
2238 }
2239
2240 /*
2241 * Change mm for all threadgroup leaders. This is expensive and may
2242 * sleep and should be moved outside migration path proper.
2243 */
2244 cpuset_attach_nodemask_to = cs->effective_mems;
2245 cgroup_taskset_for_each_leader(leader, css, tset) {
2246 struct mm_struct *mm = get_task_mm(leader);
2247
2248 if (mm) {
2249 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
2250
2251 /*
2252 * old_mems_allowed is the same with mems_allowed
2253 * here, except if this task is being moved
2254 * automatically due to hotplug. In that case
2255 * @mems_allowed has been updated and is empty, so
2256 * @old_mems_allowed is the right nodesets that we
2257 * migrate mm from.
2258 */
2259 if (is_memory_migrate(cs))
2260 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed,
2261 &cpuset_attach_nodemask_to);
2262 else
2263 mmput(mm);
2264 }
2265 }
2266
2267 cs->old_mems_allowed = cpuset_attach_nodemask_to;
2268
2269 cs->attach_in_progress--;
2270 if (!cs->attach_in_progress)
2271 wake_up(&cpuset_attach_wq);
2272
2273 percpu_up_write(&cpuset_rwsem);
2274 }
2275
2276 /* The various types of files and directories in a cpuset file system */
2277
2278 typedef enum {
2279 FILE_MEMORY_MIGRATE,
2280 FILE_CPULIST,
2281 FILE_MEMLIST,
2282 FILE_EFFECTIVE_CPULIST,
2283 FILE_EFFECTIVE_MEMLIST,
2284 FILE_SUBPARTS_CPULIST,
2285 FILE_CPU_EXCLUSIVE,
2286 FILE_MEM_EXCLUSIVE,
2287 FILE_MEM_HARDWALL,
2288 FILE_SCHED_LOAD_BALANCE,
2289 FILE_PARTITION_ROOT,
2290 FILE_SCHED_RELAX_DOMAIN_LEVEL,
2291 FILE_MEMORY_PRESSURE_ENABLED,
2292 FILE_MEMORY_PRESSURE,
2293 FILE_SPREAD_PAGE,
2294 FILE_SPREAD_SLAB,
2295 } cpuset_filetype_t;
2296
cpuset_write_u64(struct cgroup_subsys_state * css,struct cftype * cft,u64 val)2297 static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
2298 u64 val)
2299 {
2300 struct cpuset *cs = css_cs(css);
2301 cpuset_filetype_t type = cft->private;
2302 int retval = 0;
2303
2304 get_online_cpus();
2305 percpu_down_write(&cpuset_rwsem);
2306 if (!is_cpuset_online(cs)) {
2307 retval = -ENODEV;
2308 goto out_unlock;
2309 }
2310
2311 switch (type) {
2312 case FILE_CPU_EXCLUSIVE:
2313 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
2314 break;
2315 case FILE_MEM_EXCLUSIVE:
2316 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
2317 break;
2318 case FILE_MEM_HARDWALL:
2319 retval = update_flag(CS_MEM_HARDWALL, cs, val);
2320 break;
2321 case FILE_SCHED_LOAD_BALANCE:
2322 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
2323 break;
2324 case FILE_MEMORY_MIGRATE:
2325 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
2326 break;
2327 case FILE_MEMORY_PRESSURE_ENABLED:
2328 cpuset_memory_pressure_enabled = !!val;
2329 break;
2330 case FILE_SPREAD_PAGE:
2331 retval = update_flag(CS_SPREAD_PAGE, cs, val);
2332 break;
2333 case FILE_SPREAD_SLAB:
2334 retval = update_flag(CS_SPREAD_SLAB, cs, val);
2335 break;
2336 default:
2337 retval = -EINVAL;
2338 break;
2339 }
2340 out_unlock:
2341 percpu_up_write(&cpuset_rwsem);
2342 put_online_cpus();
2343 return retval;
2344 }
2345
cpuset_write_s64(struct cgroup_subsys_state * css,struct cftype * cft,s64 val)2346 static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
2347 s64 val)
2348 {
2349 struct cpuset *cs = css_cs(css);
2350 cpuset_filetype_t type = cft->private;
2351 int retval = -ENODEV;
2352
2353 get_online_cpus();
2354 percpu_down_write(&cpuset_rwsem);
2355 if (!is_cpuset_online(cs))
2356 goto out_unlock;
2357
2358 switch (type) {
2359 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
2360 retval = update_relax_domain_level(cs, val);
2361 break;
2362 default:
2363 retval = -EINVAL;
2364 break;
2365 }
2366 out_unlock:
2367 percpu_up_write(&cpuset_rwsem);
2368 put_online_cpus();
2369 return retval;
2370 }
2371
2372 /*
2373 * Common handling for a write to a "cpus" or "mems" file.
2374 */
cpuset_write_resmask(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)2375 static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
2376 char *buf, size_t nbytes, loff_t off)
2377 {
2378 struct cpuset *cs = css_cs(of_css(of));
2379 struct cpuset *trialcs;
2380 int retval = -ENODEV;
2381
2382 buf = strstrip(buf);
2383
2384 /*
2385 * CPU or memory hotunplug may leave @cs w/o any execution
2386 * resources, in which case the hotplug code asynchronously updates
2387 * configuration and transfers all tasks to the nearest ancestor
2388 * which can execute.
2389 *
2390 * As writes to "cpus" or "mems" may restore @cs's execution
2391 * resources, wait for the previously scheduled operations before
2392 * proceeding, so that we don't end up keep removing tasks added
2393 * after execution capability is restored.
2394 *
2395 * cpuset_hotplug_work calls back into cgroup core via
2396 * cgroup_transfer_tasks() and waiting for it from a cgroupfs
2397 * operation like this one can lead to a deadlock through kernfs
2398 * active_ref protection. Let's break the protection. Losing the
2399 * protection is okay as we check whether @cs is online after
2400 * grabbing cpuset_mutex anyway. This only happens on the legacy
2401 * hierarchies.
2402 */
2403 css_get(&cs->css);
2404 kernfs_break_active_protection(of->kn);
2405 flush_work(&cpuset_hotplug_work);
2406
2407 get_online_cpus();
2408 percpu_down_write(&cpuset_rwsem);
2409 if (!is_cpuset_online(cs))
2410 goto out_unlock;
2411
2412 trialcs = alloc_trial_cpuset(cs);
2413 if (!trialcs) {
2414 retval = -ENOMEM;
2415 goto out_unlock;
2416 }
2417
2418 switch (of_cft(of)->private) {
2419 case FILE_CPULIST:
2420 retval = update_cpumask(cs, trialcs, buf);
2421 break;
2422 case FILE_MEMLIST:
2423 retval = update_nodemask(cs, trialcs, buf);
2424 break;
2425 default:
2426 retval = -EINVAL;
2427 break;
2428 }
2429
2430 free_cpuset(trialcs);
2431 out_unlock:
2432 percpu_up_write(&cpuset_rwsem);
2433 put_online_cpus();
2434 kernfs_unbreak_active_protection(of->kn);
2435 css_put(&cs->css);
2436 flush_workqueue(cpuset_migrate_mm_wq);
2437 return retval ?: nbytes;
2438 }
2439
2440 /*
2441 * These ascii lists should be read in a single call, by using a user
2442 * buffer large enough to hold the entire map. If read in smaller
2443 * chunks, there is no guarantee of atomicity. Since the display format
2444 * used, list of ranges of sequential numbers, is variable length,
2445 * and since these maps can change value dynamically, one could read
2446 * gibberish by doing partial reads while a list was changing.
2447 */
cpuset_common_seq_show(struct seq_file * sf,void * v)2448 static int cpuset_common_seq_show(struct seq_file *sf, void *v)
2449 {
2450 struct cpuset *cs = css_cs(seq_css(sf));
2451 cpuset_filetype_t type = seq_cft(sf)->private;
2452 int ret = 0;
2453
2454 spin_lock_irq(&callback_lock);
2455
2456 switch (type) {
2457 case FILE_CPULIST:
2458 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_requested));
2459 break;
2460 case FILE_MEMLIST:
2461 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed));
2462 break;
2463 case FILE_EFFECTIVE_CPULIST:
2464 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus));
2465 break;
2466 case FILE_EFFECTIVE_MEMLIST:
2467 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems));
2468 break;
2469 case FILE_SUBPARTS_CPULIST:
2470 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->subparts_cpus));
2471 break;
2472 default:
2473 ret = -EINVAL;
2474 }
2475
2476 spin_unlock_irq(&callback_lock);
2477 return ret;
2478 }
2479
cpuset_read_u64(struct cgroup_subsys_state * css,struct cftype * cft)2480 static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft)
2481 {
2482 struct cpuset *cs = css_cs(css);
2483 cpuset_filetype_t type = cft->private;
2484 switch (type) {
2485 case FILE_CPU_EXCLUSIVE:
2486 return is_cpu_exclusive(cs);
2487 case FILE_MEM_EXCLUSIVE:
2488 return is_mem_exclusive(cs);
2489 case FILE_MEM_HARDWALL:
2490 return is_mem_hardwall(cs);
2491 case FILE_SCHED_LOAD_BALANCE:
2492 return is_sched_load_balance(cs);
2493 case FILE_MEMORY_MIGRATE:
2494 return is_memory_migrate(cs);
2495 case FILE_MEMORY_PRESSURE_ENABLED:
2496 return cpuset_memory_pressure_enabled;
2497 case FILE_MEMORY_PRESSURE:
2498 return fmeter_getrate(&cs->fmeter);
2499 case FILE_SPREAD_PAGE:
2500 return is_spread_page(cs);
2501 case FILE_SPREAD_SLAB:
2502 return is_spread_slab(cs);
2503 default:
2504 BUG();
2505 }
2506
2507 /* Unreachable but makes gcc happy */
2508 return 0;
2509 }
2510
cpuset_read_s64(struct cgroup_subsys_state * css,struct cftype * cft)2511 static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft)
2512 {
2513 struct cpuset *cs = css_cs(css);
2514 cpuset_filetype_t type = cft->private;
2515 switch (type) {
2516 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
2517 return cs->relax_domain_level;
2518 default:
2519 BUG();
2520 }
2521
2522 /* Unrechable but makes gcc happy */
2523 return 0;
2524 }
2525
sched_partition_show(struct seq_file * seq,void * v)2526 static int sched_partition_show(struct seq_file *seq, void *v)
2527 {
2528 struct cpuset *cs = css_cs(seq_css(seq));
2529
2530 switch (cs->partition_root_state) {
2531 case PRS_ENABLED:
2532 seq_puts(seq, "root\n");
2533 break;
2534 case PRS_DISABLED:
2535 seq_puts(seq, "member\n");
2536 break;
2537 case PRS_ERROR:
2538 seq_puts(seq, "root invalid\n");
2539 break;
2540 }
2541 return 0;
2542 }
2543
sched_partition_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)2544 static ssize_t sched_partition_write(struct kernfs_open_file *of, char *buf,
2545 size_t nbytes, loff_t off)
2546 {
2547 struct cpuset *cs = css_cs(of_css(of));
2548 int val;
2549 int retval = -ENODEV;
2550
2551 buf = strstrip(buf);
2552
2553 /*
2554 * Convert "root" to ENABLED, and convert "member" to DISABLED.
2555 */
2556 if (!strcmp(buf, "root"))
2557 val = PRS_ENABLED;
2558 else if (!strcmp(buf, "member"))
2559 val = PRS_DISABLED;
2560 else
2561 return -EINVAL;
2562
2563 css_get(&cs->css);
2564 get_online_cpus();
2565 percpu_down_write(&cpuset_rwsem);
2566 if (!is_cpuset_online(cs))
2567 goto out_unlock;
2568
2569 retval = update_prstate(cs, val);
2570 out_unlock:
2571 percpu_up_write(&cpuset_rwsem);
2572 put_online_cpus();
2573 css_put(&cs->css);
2574 return retval ?: nbytes;
2575 }
2576
2577 /*
2578 * for the common functions, 'private' gives the type of file
2579 */
2580
2581 static struct cftype legacy_files[] = {
2582 {
2583 .name = "cpus",
2584 .seq_show = cpuset_common_seq_show,
2585 .write = cpuset_write_resmask,
2586 .max_write_len = (100U + 6 * NR_CPUS),
2587 .private = FILE_CPULIST,
2588 },
2589
2590 {
2591 .name = "mems",
2592 .seq_show = cpuset_common_seq_show,
2593 .write = cpuset_write_resmask,
2594 .max_write_len = (100U + 6 * MAX_NUMNODES),
2595 .private = FILE_MEMLIST,
2596 },
2597
2598 {
2599 .name = "effective_cpus",
2600 .seq_show = cpuset_common_seq_show,
2601 .private = FILE_EFFECTIVE_CPULIST,
2602 },
2603
2604 {
2605 .name = "effective_mems",
2606 .seq_show = cpuset_common_seq_show,
2607 .private = FILE_EFFECTIVE_MEMLIST,
2608 },
2609
2610 {
2611 .name = "cpu_exclusive",
2612 .read_u64 = cpuset_read_u64,
2613 .write_u64 = cpuset_write_u64,
2614 .private = FILE_CPU_EXCLUSIVE,
2615 },
2616
2617 {
2618 .name = "mem_exclusive",
2619 .read_u64 = cpuset_read_u64,
2620 .write_u64 = cpuset_write_u64,
2621 .private = FILE_MEM_EXCLUSIVE,
2622 },
2623
2624 {
2625 .name = "mem_hardwall",
2626 .read_u64 = cpuset_read_u64,
2627 .write_u64 = cpuset_write_u64,
2628 .private = FILE_MEM_HARDWALL,
2629 },
2630
2631 {
2632 .name = "sched_load_balance",
2633 .read_u64 = cpuset_read_u64,
2634 .write_u64 = cpuset_write_u64,
2635 .private = FILE_SCHED_LOAD_BALANCE,
2636 },
2637
2638 {
2639 .name = "sched_relax_domain_level",
2640 .read_s64 = cpuset_read_s64,
2641 .write_s64 = cpuset_write_s64,
2642 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
2643 },
2644
2645 {
2646 .name = "memory_migrate",
2647 .read_u64 = cpuset_read_u64,
2648 .write_u64 = cpuset_write_u64,
2649 .private = FILE_MEMORY_MIGRATE,
2650 },
2651
2652 {
2653 .name = "memory_pressure",
2654 .read_u64 = cpuset_read_u64,
2655 .private = FILE_MEMORY_PRESSURE,
2656 },
2657
2658 {
2659 .name = "memory_spread_page",
2660 .read_u64 = cpuset_read_u64,
2661 .write_u64 = cpuset_write_u64,
2662 .private = FILE_SPREAD_PAGE,
2663 },
2664
2665 {
2666 .name = "memory_spread_slab",
2667 .read_u64 = cpuset_read_u64,
2668 .write_u64 = cpuset_write_u64,
2669 .private = FILE_SPREAD_SLAB,
2670 },
2671
2672 {
2673 .name = "memory_pressure_enabled",
2674 .flags = CFTYPE_ONLY_ON_ROOT,
2675 .read_u64 = cpuset_read_u64,
2676 .write_u64 = cpuset_write_u64,
2677 .private = FILE_MEMORY_PRESSURE_ENABLED,
2678 },
2679
2680 { } /* terminate */
2681 };
2682
2683 /*
2684 * This is currently a minimal set for the default hierarchy. It can be
2685 * expanded later on by migrating more features and control files from v1.
2686 */
2687 static struct cftype dfl_files[] = {
2688 {
2689 .name = "cpus",
2690 .seq_show = cpuset_common_seq_show,
2691 .write = cpuset_write_resmask,
2692 .max_write_len = (100U + 6 * NR_CPUS),
2693 .private = FILE_CPULIST,
2694 .flags = CFTYPE_NOT_ON_ROOT,
2695 },
2696
2697 {
2698 .name = "mems",
2699 .seq_show = cpuset_common_seq_show,
2700 .write = cpuset_write_resmask,
2701 .max_write_len = (100U + 6 * MAX_NUMNODES),
2702 .private = FILE_MEMLIST,
2703 .flags = CFTYPE_NOT_ON_ROOT,
2704 },
2705
2706 {
2707 .name = "cpus.effective",
2708 .seq_show = cpuset_common_seq_show,
2709 .private = FILE_EFFECTIVE_CPULIST,
2710 },
2711
2712 {
2713 .name = "mems.effective",
2714 .seq_show = cpuset_common_seq_show,
2715 .private = FILE_EFFECTIVE_MEMLIST,
2716 },
2717
2718 {
2719 .name = "cpus.partition",
2720 .seq_show = sched_partition_show,
2721 .write = sched_partition_write,
2722 .private = FILE_PARTITION_ROOT,
2723 .flags = CFTYPE_NOT_ON_ROOT,
2724 },
2725
2726 {
2727 .name = "cpus.subpartitions",
2728 .seq_show = cpuset_common_seq_show,
2729 .private = FILE_SUBPARTS_CPULIST,
2730 .flags = CFTYPE_DEBUG,
2731 },
2732
2733 { } /* terminate */
2734 };
2735
2736
2737 /*
2738 * cpuset_css_alloc - allocate a cpuset css
2739 * cgrp: control group that the new cpuset will be part of
2740 */
2741
2742 static struct cgroup_subsys_state *
cpuset_css_alloc(struct cgroup_subsys_state * parent_css)2743 cpuset_css_alloc(struct cgroup_subsys_state *parent_css)
2744 {
2745 struct cpuset *cs;
2746
2747 if (!parent_css)
2748 return &top_cpuset.css;
2749
2750 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
2751 if (!cs)
2752 return ERR_PTR(-ENOMEM);
2753
2754 if (alloc_cpumasks(cs, NULL)) {
2755 kfree(cs);
2756 return ERR_PTR(-ENOMEM);
2757 }
2758
2759 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
2760 nodes_clear(cs->mems_allowed);
2761 nodes_clear(cs->effective_mems);
2762 fmeter_init(&cs->fmeter);
2763 cs->relax_domain_level = -1;
2764
2765 return &cs->css;
2766 }
2767
cpuset_css_online(struct cgroup_subsys_state * css)2768 static int cpuset_css_online(struct cgroup_subsys_state *css)
2769 {
2770 struct cpuset *cs = css_cs(css);
2771 struct cpuset *parent = parent_cs(cs);
2772 struct cpuset *tmp_cs;
2773 struct cgroup_subsys_state *pos_css;
2774
2775 if (!parent)
2776 return 0;
2777
2778 get_online_cpus();
2779 percpu_down_write(&cpuset_rwsem);
2780
2781 set_bit(CS_ONLINE, &cs->flags);
2782 if (is_spread_page(parent))
2783 set_bit(CS_SPREAD_PAGE, &cs->flags);
2784 if (is_spread_slab(parent))
2785 set_bit(CS_SPREAD_SLAB, &cs->flags);
2786
2787 cpuset_inc();
2788
2789 spin_lock_irq(&callback_lock);
2790 if (is_in_v2_mode()) {
2791 cpumask_copy(cs->effective_cpus, parent->effective_cpus);
2792 cs->effective_mems = parent->effective_mems;
2793 cs->use_parent_ecpus = true;
2794 parent->child_ecpus_count++;
2795 }
2796 spin_unlock_irq(&callback_lock);
2797
2798 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
2799 goto out_unlock;
2800
2801 /*
2802 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
2803 * set. This flag handling is implemented in cgroup core for
2804 * histrical reasons - the flag may be specified during mount.
2805 *
2806 * Currently, if any sibling cpusets have exclusive cpus or mem, we
2807 * refuse to clone the configuration - thereby refusing the task to
2808 * be entered, and as a result refusing the sys_unshare() or
2809 * clone() which initiated it. If this becomes a problem for some
2810 * users who wish to allow that scenario, then this could be
2811 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2812 * (and likewise for mems) to the new cgroup.
2813 */
2814 rcu_read_lock();
2815 cpuset_for_each_child(tmp_cs, pos_css, parent) {
2816 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
2817 rcu_read_unlock();
2818 goto out_unlock;
2819 }
2820 }
2821 rcu_read_unlock();
2822
2823 spin_lock_irq(&callback_lock);
2824 cs->mems_allowed = parent->mems_allowed;
2825 cs->effective_mems = parent->mems_allowed;
2826 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
2827 cpumask_copy(cs->cpus_requested, parent->cpus_requested);
2828 cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
2829 spin_unlock_irq(&callback_lock);
2830 out_unlock:
2831 percpu_up_write(&cpuset_rwsem);
2832 put_online_cpus();
2833 return 0;
2834 }
2835
2836 /*
2837 * If the cpuset being removed has its flag 'sched_load_balance'
2838 * enabled, then simulate turning sched_load_balance off, which
2839 * will call rebuild_sched_domains_locked(). That is not needed
2840 * in the default hierarchy where only changes in partition
2841 * will cause repartitioning.
2842 *
2843 * If the cpuset has the 'sched.partition' flag enabled, simulate
2844 * turning 'sched.partition" off.
2845 */
2846
cpuset_css_offline(struct cgroup_subsys_state * css)2847 static void cpuset_css_offline(struct cgroup_subsys_state *css)
2848 {
2849 struct cpuset *cs = css_cs(css);
2850
2851 get_online_cpus();
2852 percpu_down_write(&cpuset_rwsem);
2853
2854 if (is_partition_root(cs))
2855 update_prstate(cs, 0);
2856
2857 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
2858 is_sched_load_balance(cs))
2859 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
2860
2861 if (cs->use_parent_ecpus) {
2862 struct cpuset *parent = parent_cs(cs);
2863
2864 cs->use_parent_ecpus = false;
2865 parent->child_ecpus_count--;
2866 }
2867
2868 cpuset_dec();
2869 clear_bit(CS_ONLINE, &cs->flags);
2870
2871 percpu_up_write(&cpuset_rwsem);
2872 put_online_cpus();
2873 }
2874
cpuset_css_free(struct cgroup_subsys_state * css)2875 static void cpuset_css_free(struct cgroup_subsys_state *css)
2876 {
2877 struct cpuset *cs = css_cs(css);
2878
2879 free_cpuset(cs);
2880 }
2881
cpuset_bind(struct cgroup_subsys_state * root_css)2882 static void cpuset_bind(struct cgroup_subsys_state *root_css)
2883 {
2884 percpu_down_write(&cpuset_rwsem);
2885 spin_lock_irq(&callback_lock);
2886
2887 if (is_in_v2_mode()) {
2888 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
2889 top_cpuset.mems_allowed = node_possible_map;
2890 } else {
2891 cpumask_copy(top_cpuset.cpus_allowed,
2892 top_cpuset.effective_cpus);
2893 top_cpuset.mems_allowed = top_cpuset.effective_mems;
2894 }
2895
2896 spin_unlock_irq(&callback_lock);
2897 percpu_up_write(&cpuset_rwsem);
2898 }
2899
2900 /*
2901 * Make sure the new task conform to the current state of its parent,
2902 * which could have been changed by cpuset just after it inherits the
2903 * state from the parent and before it sits on the cgroup's task list.
2904 */
cpuset_fork(struct task_struct * task)2905 static void cpuset_fork(struct task_struct *task)
2906 {
2907 if (task_css_is_root(task, cpuset_cgrp_id))
2908 return;
2909
2910 set_cpus_allowed_ptr(task, current->cpus_ptr);
2911 task->mems_allowed = current->mems_allowed;
2912 }
2913
2914 struct cgroup_subsys cpuset_cgrp_subsys = {
2915 .css_alloc = cpuset_css_alloc,
2916 .css_online = cpuset_css_online,
2917 .css_offline = cpuset_css_offline,
2918 .css_free = cpuset_css_free,
2919 .can_attach = cpuset_can_attach,
2920 .cancel_attach = cpuset_cancel_attach,
2921 .attach = cpuset_attach,
2922 .post_attach = cpuset_post_attach,
2923 .bind = cpuset_bind,
2924 .fork = cpuset_fork,
2925 .legacy_cftypes = legacy_files,
2926 .dfl_cftypes = dfl_files,
2927 .early_init = true,
2928 .threaded = true,
2929 };
2930
2931 /**
2932 * cpuset_init - initialize cpusets at system boot
2933 *
2934 * Description: Initialize top_cpuset
2935 **/
2936
cpuset_init(void)2937 int __init cpuset_init(void)
2938 {
2939 BUG_ON(percpu_init_rwsem(&cpuset_rwsem));
2940
2941 BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL));
2942 BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_requested, GFP_KERNEL));
2943 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL));
2944 BUG_ON(!zalloc_cpumask_var(&top_cpuset.subparts_cpus, GFP_KERNEL));
2945
2946 cpumask_setall(top_cpuset.cpus_allowed);
2947 cpumask_setall(top_cpuset.cpus_requested);
2948 nodes_setall(top_cpuset.mems_allowed);
2949 cpumask_setall(top_cpuset.effective_cpus);
2950 nodes_setall(top_cpuset.effective_mems);
2951
2952 fmeter_init(&top_cpuset.fmeter);
2953 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
2954 top_cpuset.relax_domain_level = -1;
2955
2956 BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL));
2957
2958 return 0;
2959 }
2960
2961 /*
2962 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
2963 * or memory nodes, we need to walk over the cpuset hierarchy,
2964 * removing that CPU or node from all cpusets. If this removes the
2965 * last CPU or node from a cpuset, then move the tasks in the empty
2966 * cpuset to its next-highest non-empty parent.
2967 */
remove_tasks_in_empty_cpuset(struct cpuset * cs)2968 static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
2969 {
2970 struct cpuset *parent;
2971
2972 /*
2973 * Find its next-highest non-empty parent, (top cpuset
2974 * has online cpus, so can't be empty).
2975 */
2976 parent = parent_cs(cs);
2977 while (cpumask_empty(parent->cpus_allowed) ||
2978 nodes_empty(parent->mems_allowed))
2979 parent = parent_cs(parent);
2980
2981 if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) {
2982 pr_err("cpuset: failed to transfer tasks out of empty cpuset ");
2983 pr_cont_cgroup_name(cs->css.cgroup);
2984 pr_cont("\n");
2985 }
2986 }
2987
2988 static void
hotplug_update_tasks_legacy(struct cpuset * cs,struct cpumask * new_cpus,nodemask_t * new_mems,bool cpus_updated,bool mems_updated)2989 hotplug_update_tasks_legacy(struct cpuset *cs,
2990 struct cpumask *new_cpus, nodemask_t *new_mems,
2991 bool cpus_updated, bool mems_updated)
2992 {
2993 bool is_empty;
2994
2995 spin_lock_irq(&callback_lock);
2996 cpumask_copy(cs->cpus_allowed, new_cpus);
2997 cpumask_copy(cs->effective_cpus, new_cpus);
2998 cs->mems_allowed = *new_mems;
2999 cs->effective_mems = *new_mems;
3000 spin_unlock_irq(&callback_lock);
3001
3002 /*
3003 * Don't call update_tasks_cpumask() if the cpuset becomes empty,
3004 * as the tasks will be migratecd to an ancestor.
3005 */
3006 if (cpus_updated && !cpumask_empty(cs->cpus_allowed))
3007 update_tasks_cpumask(cs);
3008 if (mems_updated && !nodes_empty(cs->mems_allowed))
3009 update_tasks_nodemask(cs);
3010
3011 is_empty = cpumask_empty(cs->cpus_allowed) ||
3012 nodes_empty(cs->mems_allowed);
3013
3014 percpu_up_write(&cpuset_rwsem);
3015
3016 /*
3017 * Move tasks to the nearest ancestor with execution resources,
3018 * This is full cgroup operation which will also call back into
3019 * cpuset. Should be done outside any lock.
3020 */
3021 if (is_empty)
3022 remove_tasks_in_empty_cpuset(cs);
3023
3024 percpu_down_write(&cpuset_rwsem);
3025 }
3026
3027 static void
hotplug_update_tasks(struct cpuset * cs,struct cpumask * new_cpus,nodemask_t * new_mems,bool cpus_updated,bool mems_updated)3028 hotplug_update_tasks(struct cpuset *cs,
3029 struct cpumask *new_cpus, nodemask_t *new_mems,
3030 bool cpus_updated, bool mems_updated)
3031 {
3032 if (cpumask_empty(new_cpus))
3033 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus);
3034 if (nodes_empty(*new_mems))
3035 *new_mems = parent_cs(cs)->effective_mems;
3036
3037 spin_lock_irq(&callback_lock);
3038 cpumask_copy(cs->effective_cpus, new_cpus);
3039 cs->effective_mems = *new_mems;
3040 spin_unlock_irq(&callback_lock);
3041
3042 if (cpus_updated)
3043 update_tasks_cpumask(cs);
3044 if (mems_updated)
3045 update_tasks_nodemask(cs);
3046 }
3047
3048 static bool force_rebuild;
3049
cpuset_force_rebuild(void)3050 void cpuset_force_rebuild(void)
3051 {
3052 force_rebuild = true;
3053 }
3054
3055 /**
3056 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
3057 * @cs: cpuset in interest
3058 * @tmp: the tmpmasks structure pointer
3059 *
3060 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
3061 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
3062 * all its tasks are moved to the nearest ancestor with both resources.
3063 */
cpuset_hotplug_update_tasks(struct cpuset * cs,struct tmpmasks * tmp)3064 static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
3065 {
3066 static cpumask_t new_cpus;
3067 static nodemask_t new_mems;
3068 bool cpus_updated;
3069 bool mems_updated;
3070 struct cpuset *parent;
3071 retry:
3072 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
3073
3074 percpu_down_write(&cpuset_rwsem);
3075
3076 /*
3077 * We have raced with task attaching. We wait until attaching
3078 * is finished, so we won't attach a task to an empty cpuset.
3079 */
3080 if (cs->attach_in_progress) {
3081 percpu_up_write(&cpuset_rwsem);
3082 goto retry;
3083 }
3084
3085 parent = parent_cs(cs);
3086 compute_effective_cpumask(&new_cpus, cs, parent);
3087 nodes_and(new_mems, cs->mems_allowed, parent->effective_mems);
3088
3089 if (cs->nr_subparts_cpus)
3090 /*
3091 * Make sure that CPUs allocated to child partitions
3092 * do not show up in effective_cpus.
3093 */
3094 cpumask_andnot(&new_cpus, &new_cpus, cs->subparts_cpus);
3095
3096 if (!tmp || !cs->partition_root_state)
3097 goto update_tasks;
3098
3099 /*
3100 * In the unlikely event that a partition root has empty
3101 * effective_cpus or its parent becomes erroneous, we have to
3102 * transition it to the erroneous state.
3103 */
3104 if (is_partition_root(cs) && (cpumask_empty(&new_cpus) ||
3105 (parent->partition_root_state == PRS_ERROR))) {
3106 if (cs->nr_subparts_cpus) {
3107 spin_lock_irq(&callback_lock);
3108 cs->nr_subparts_cpus = 0;
3109 cpumask_clear(cs->subparts_cpus);
3110 spin_unlock_irq(&callback_lock);
3111 compute_effective_cpumask(&new_cpus, cs, parent);
3112 }
3113
3114 /*
3115 * If the effective_cpus is empty because the child
3116 * partitions take away all the CPUs, we can keep
3117 * the current partition and let the child partitions
3118 * fight for available CPUs.
3119 */
3120 if ((parent->partition_root_state == PRS_ERROR) ||
3121 cpumask_empty(&new_cpus)) {
3122 update_parent_subparts_cpumask(cs, partcmd_disable,
3123 NULL, tmp);
3124 spin_lock_irq(&callback_lock);
3125 cs->partition_root_state = PRS_ERROR;
3126 spin_unlock_irq(&callback_lock);
3127 }
3128 cpuset_force_rebuild();
3129 }
3130
3131 /*
3132 * On the other hand, an erroneous partition root may be transitioned
3133 * back to a regular one or a partition root with no CPU allocated
3134 * from the parent may change to erroneous.
3135 */
3136 if (is_partition_root(parent) &&
3137 ((cs->partition_root_state == PRS_ERROR) ||
3138 !cpumask_intersects(&new_cpus, parent->subparts_cpus)) &&
3139 update_parent_subparts_cpumask(cs, partcmd_update, NULL, tmp))
3140 cpuset_force_rebuild();
3141
3142 update_tasks:
3143 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
3144 mems_updated = !nodes_equal(new_mems, cs->effective_mems);
3145
3146 if (is_in_v2_mode())
3147 hotplug_update_tasks(cs, &new_cpus, &new_mems,
3148 cpus_updated, mems_updated);
3149 else
3150 hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems,
3151 cpus_updated, mems_updated);
3152
3153 percpu_up_write(&cpuset_rwsem);
3154 }
3155
3156 /**
3157 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
3158 *
3159 * This function is called after either CPU or memory configuration has
3160 * changed and updates cpuset accordingly. The top_cpuset is always
3161 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
3162 * order to make cpusets transparent (of no affect) on systems that are
3163 * actively using CPU hotplug but making no active use of cpusets.
3164 *
3165 * Non-root cpusets are only affected by offlining. If any CPUs or memory
3166 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
3167 * all descendants.
3168 *
3169 * Note that CPU offlining during suspend is ignored. We don't modify
3170 * cpusets across suspend/resume cycles at all.
3171 */
cpuset_hotplug_workfn(struct work_struct * work)3172 static void cpuset_hotplug_workfn(struct work_struct *work)
3173 {
3174 static cpumask_t new_cpus;
3175 static nodemask_t new_mems;
3176 bool cpus_updated, mems_updated;
3177 bool on_dfl = is_in_v2_mode();
3178 struct tmpmasks tmp, *ptmp = NULL;
3179
3180 if (on_dfl && !alloc_cpumasks(NULL, &tmp))
3181 ptmp = &tmp;
3182
3183 percpu_down_write(&cpuset_rwsem);
3184
3185 /* fetch the available cpus/mems and find out which changed how */
3186 cpumask_copy(&new_cpus, cpu_active_mask);
3187 new_mems = node_states[N_MEMORY];
3188
3189 /*
3190 * If subparts_cpus is populated, it is likely that the check below
3191 * will produce a false positive on cpus_updated when the cpu list
3192 * isn't changed. It is extra work, but it is better to be safe.
3193 */
3194 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus);
3195 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems);
3196
3197 /*
3198 * In the rare case that hotplug removes all the cpus in subparts_cpus,
3199 * we assumed that cpus are updated.
3200 */
3201 if (!cpus_updated && top_cpuset.nr_subparts_cpus)
3202 cpus_updated = true;
3203
3204 /* synchronize cpus_allowed to cpu_active_mask */
3205 if (cpus_updated) {
3206 spin_lock_irq(&callback_lock);
3207 if (!on_dfl)
3208 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
3209 /*
3210 * Make sure that CPUs allocated to child partitions
3211 * do not show up in effective_cpus. If no CPU is left,
3212 * we clear the subparts_cpus & let the child partitions
3213 * fight for the CPUs again.
3214 */
3215 if (top_cpuset.nr_subparts_cpus) {
3216 if (cpumask_subset(&new_cpus,
3217 top_cpuset.subparts_cpus)) {
3218 top_cpuset.nr_subparts_cpus = 0;
3219 cpumask_clear(top_cpuset.subparts_cpus);
3220 } else {
3221 cpumask_andnot(&new_cpus, &new_cpus,
3222 top_cpuset.subparts_cpus);
3223 }
3224 }
3225 cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
3226 spin_unlock_irq(&callback_lock);
3227 /* we don't mess with cpumasks of tasks in top_cpuset */
3228 }
3229
3230 /* synchronize mems_allowed to N_MEMORY */
3231 if (mems_updated) {
3232 spin_lock_irq(&callback_lock);
3233 if (!on_dfl)
3234 top_cpuset.mems_allowed = new_mems;
3235 top_cpuset.effective_mems = new_mems;
3236 spin_unlock_irq(&callback_lock);
3237 update_tasks_nodemask(&top_cpuset);
3238 }
3239
3240 percpu_up_write(&cpuset_rwsem);
3241
3242 /* if cpus or mems changed, we need to propagate to descendants */
3243 if (cpus_updated || mems_updated) {
3244 struct cpuset *cs;
3245 struct cgroup_subsys_state *pos_css;
3246
3247 rcu_read_lock();
3248 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
3249 if (cs == &top_cpuset || !css_tryget_online(&cs->css))
3250 continue;
3251 rcu_read_unlock();
3252
3253 cpuset_hotplug_update_tasks(cs, ptmp);
3254
3255 rcu_read_lock();
3256 css_put(&cs->css);
3257 }
3258 rcu_read_unlock();
3259 }
3260
3261 /* rebuild sched domains if cpus_allowed has changed */
3262 if (cpus_updated || force_rebuild) {
3263 force_rebuild = false;
3264 rebuild_sched_domains();
3265 }
3266
3267 free_cpumasks(NULL, ptmp);
3268 }
3269
cpuset_update_active_cpus(void)3270 void cpuset_update_active_cpus(void)
3271 {
3272 /*
3273 * We're inside cpu hotplug critical region which usually nests
3274 * inside cgroup synchronization. Bounce actual hotplug processing
3275 * to a work item to avoid reverse locking order.
3276 */
3277 schedule_work(&cpuset_hotplug_work);
3278 }
3279
cpuset_wait_for_hotplug(void)3280 void cpuset_wait_for_hotplug(void)
3281 {
3282 flush_work(&cpuset_hotplug_work);
3283 }
3284
3285 /*
3286 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
3287 * Call this routine anytime after node_states[N_MEMORY] changes.
3288 * See cpuset_update_active_cpus() for CPU hotplug handling.
3289 */
cpuset_track_online_nodes(struct notifier_block * self,unsigned long action,void * arg)3290 static int cpuset_track_online_nodes(struct notifier_block *self,
3291 unsigned long action, void *arg)
3292 {
3293 schedule_work(&cpuset_hotplug_work);
3294 return NOTIFY_OK;
3295 }
3296
3297 static struct notifier_block cpuset_track_online_nodes_nb = {
3298 .notifier_call = cpuset_track_online_nodes,
3299 .priority = 10, /* ??! */
3300 };
3301
3302 /**
3303 * cpuset_init_smp - initialize cpus_allowed
3304 *
3305 * Description: Finish top cpuset after cpu, node maps are initialized
3306 */
cpuset_init_smp(void)3307 void __init cpuset_init_smp(void)
3308 {
3309 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
3310 top_cpuset.mems_allowed = node_states[N_MEMORY];
3311 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed;
3312
3313 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask);
3314 top_cpuset.effective_mems = node_states[N_MEMORY];
3315
3316 register_hotmemory_notifier(&cpuset_track_online_nodes_nb);
3317
3318 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0);
3319 BUG_ON(!cpuset_migrate_mm_wq);
3320 }
3321
3322 /**
3323 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
3324 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
3325 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
3326 *
3327 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
3328 * attached to the specified @tsk. Guaranteed to return some non-empty
3329 * subset of cpu_online_mask, even if this means going outside the
3330 * tasks cpuset.
3331 **/
3332
cpuset_cpus_allowed(struct task_struct * tsk,struct cpumask * pmask)3333 void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
3334 {
3335 unsigned long flags;
3336
3337 spin_lock_irqsave(&callback_lock, flags);
3338 rcu_read_lock();
3339 guarantee_online_cpus(task_cs(tsk), pmask);
3340 rcu_read_unlock();
3341 spin_unlock_irqrestore(&callback_lock, flags);
3342 }
3343
3344 /**
3345 * cpuset_cpus_allowed_fallback - final fallback before complete catastrophe.
3346 * @tsk: pointer to task_struct with which the scheduler is struggling
3347 *
3348 * Description: In the case that the scheduler cannot find an allowed cpu in
3349 * tsk->cpus_allowed, we fall back to task_cs(tsk)->cpus_allowed. In legacy
3350 * mode however, this value is the same as task_cs(tsk)->effective_cpus,
3351 * which will not contain a sane cpumask during cases such as cpu hotplugging.
3352 * This is the absolute last resort for the scheduler and it is only used if
3353 * _every_ other avenue has been traveled.
3354 **/
3355
cpuset_cpus_allowed_fallback(struct task_struct * tsk)3356 void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
3357 {
3358 rcu_read_lock();
3359 do_set_cpus_allowed(tsk, is_in_v2_mode() ?
3360 task_cs(tsk)->cpus_allowed : cpu_possible_mask);
3361 rcu_read_unlock();
3362
3363 /*
3364 * We own tsk->cpus_allowed, nobody can change it under us.
3365 *
3366 * But we used cs && cs->cpus_allowed lockless and thus can
3367 * race with cgroup_attach_task() or update_cpumask() and get
3368 * the wrong tsk->cpus_allowed. However, both cases imply the
3369 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
3370 * which takes task_rq_lock().
3371 *
3372 * If we are called after it dropped the lock we must see all
3373 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
3374 * set any mask even if it is not right from task_cs() pov,
3375 * the pending set_cpus_allowed_ptr() will fix things.
3376 *
3377 * select_fallback_rq() will fix things ups and set cpu_possible_mask
3378 * if required.
3379 */
3380 }
3381
cpuset_init_current_mems_allowed(void)3382 void __init cpuset_init_current_mems_allowed(void)
3383 {
3384 nodes_setall(current->mems_allowed);
3385 }
3386
3387 /**
3388 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
3389 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
3390 *
3391 * Description: Returns the nodemask_t mems_allowed of the cpuset
3392 * attached to the specified @tsk. Guaranteed to return some non-empty
3393 * subset of node_states[N_MEMORY], even if this means going outside the
3394 * tasks cpuset.
3395 **/
3396
cpuset_mems_allowed(struct task_struct * tsk)3397 nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
3398 {
3399 nodemask_t mask;
3400 unsigned long flags;
3401
3402 spin_lock_irqsave(&callback_lock, flags);
3403 rcu_read_lock();
3404 guarantee_online_mems(task_cs(tsk), &mask);
3405 rcu_read_unlock();
3406 spin_unlock_irqrestore(&callback_lock, flags);
3407
3408 return mask;
3409 }
3410
3411 /**
3412 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
3413 * @nodemask: the nodemask to be checked
3414 *
3415 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
3416 */
cpuset_nodemask_valid_mems_allowed(nodemask_t * nodemask)3417 int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
3418 {
3419 return nodes_intersects(*nodemask, current->mems_allowed);
3420 }
3421
3422 /*
3423 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
3424 * mem_hardwall ancestor to the specified cpuset. Call holding
3425 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
3426 * (an unusual configuration), then returns the root cpuset.
3427 */
nearest_hardwall_ancestor(struct cpuset * cs)3428 static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs)
3429 {
3430 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs))
3431 cs = parent_cs(cs);
3432 return cs;
3433 }
3434
3435 /**
3436 * cpuset_node_allowed - Can we allocate on a memory node?
3437 * @node: is this an allowed node?
3438 * @gfp_mask: memory allocation flags
3439 *
3440 * If we're in interrupt, yes, we can always allocate. If @node is set in
3441 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
3442 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
3443 * yes. If current has access to memory reserves as an oom victim, yes.
3444 * Otherwise, no.
3445 *
3446 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
3447 * and do not allow allocations outside the current tasks cpuset
3448 * unless the task has been OOM killed.
3449 * GFP_KERNEL allocations are not so marked, so can escape to the
3450 * nearest enclosing hardwalled ancestor cpuset.
3451 *
3452 * Scanning up parent cpusets requires callback_lock. The
3453 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
3454 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
3455 * current tasks mems_allowed came up empty on the first pass over
3456 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
3457 * cpuset are short of memory, might require taking the callback_lock.
3458 *
3459 * The first call here from mm/page_alloc:get_page_from_freelist()
3460 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
3461 * so no allocation on a node outside the cpuset is allowed (unless
3462 * in interrupt, of course).
3463 *
3464 * The second pass through get_page_from_freelist() doesn't even call
3465 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
3466 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
3467 * in alloc_flags. That logic and the checks below have the combined
3468 * affect that:
3469 * in_interrupt - any node ok (current task context irrelevant)
3470 * GFP_ATOMIC - any node ok
3471 * tsk_is_oom_victim - any node ok
3472 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
3473 * GFP_USER - only nodes in current tasks mems allowed ok.
3474 */
__cpuset_node_allowed(int node,gfp_t gfp_mask)3475 bool __cpuset_node_allowed(int node, gfp_t gfp_mask)
3476 {
3477 struct cpuset *cs; /* current cpuset ancestors */
3478 int allowed; /* is allocation in zone z allowed? */
3479 unsigned long flags;
3480
3481 if (in_interrupt())
3482 return true;
3483 if (node_isset(node, current->mems_allowed))
3484 return true;
3485 /*
3486 * Allow tasks that have access to memory reserves because they have
3487 * been OOM killed to get memory anywhere.
3488 */
3489 if (unlikely(tsk_is_oom_victim(current)))
3490 return true;
3491 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
3492 return false;
3493
3494 if (current->flags & PF_EXITING) /* Let dying task have memory */
3495 return true;
3496
3497 /* Not hardwall and node outside mems_allowed: scan up cpusets */
3498 spin_lock_irqsave(&callback_lock, flags);
3499
3500 rcu_read_lock();
3501 cs = nearest_hardwall_ancestor(task_cs(current));
3502 allowed = node_isset(node, cs->mems_allowed);
3503 rcu_read_unlock();
3504
3505 spin_unlock_irqrestore(&callback_lock, flags);
3506 return allowed;
3507 }
3508
3509 /**
3510 * cpuset_mem_spread_node() - On which node to begin search for a file page
3511 * cpuset_slab_spread_node() - On which node to begin search for a slab page
3512 *
3513 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
3514 * tasks in a cpuset with is_spread_page or is_spread_slab set),
3515 * and if the memory allocation used cpuset_mem_spread_node()
3516 * to determine on which node to start looking, as it will for
3517 * certain page cache or slab cache pages such as used for file
3518 * system buffers and inode caches, then instead of starting on the
3519 * local node to look for a free page, rather spread the starting
3520 * node around the tasks mems_allowed nodes.
3521 *
3522 * We don't have to worry about the returned node being offline
3523 * because "it can't happen", and even if it did, it would be ok.
3524 *
3525 * The routines calling guarantee_online_mems() are careful to
3526 * only set nodes in task->mems_allowed that are online. So it
3527 * should not be possible for the following code to return an
3528 * offline node. But if it did, that would be ok, as this routine
3529 * is not returning the node where the allocation must be, only
3530 * the node where the search should start. The zonelist passed to
3531 * __alloc_pages() will include all nodes. If the slab allocator
3532 * is passed an offline node, it will fall back to the local node.
3533 * See kmem_cache_alloc_node().
3534 */
3535
cpuset_spread_node(int * rotor)3536 static int cpuset_spread_node(int *rotor)
3537 {
3538 return *rotor = next_node_in(*rotor, current->mems_allowed);
3539 }
3540
cpuset_mem_spread_node(void)3541 int cpuset_mem_spread_node(void)
3542 {
3543 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
3544 current->cpuset_mem_spread_rotor =
3545 node_random(¤t->mems_allowed);
3546
3547 return cpuset_spread_node(¤t->cpuset_mem_spread_rotor);
3548 }
3549
cpuset_slab_spread_node(void)3550 int cpuset_slab_spread_node(void)
3551 {
3552 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
3553 current->cpuset_slab_spread_rotor =
3554 node_random(¤t->mems_allowed);
3555
3556 return cpuset_spread_node(¤t->cpuset_slab_spread_rotor);
3557 }
3558
3559 EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
3560
3561 /**
3562 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
3563 * @tsk1: pointer to task_struct of some task.
3564 * @tsk2: pointer to task_struct of some other task.
3565 *
3566 * Description: Return true if @tsk1's mems_allowed intersects the
3567 * mems_allowed of @tsk2. Used by the OOM killer to determine if
3568 * one of the task's memory usage might impact the memory available
3569 * to the other.
3570 **/
3571
cpuset_mems_allowed_intersects(const struct task_struct * tsk1,const struct task_struct * tsk2)3572 int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
3573 const struct task_struct *tsk2)
3574 {
3575 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
3576 }
3577
3578 /**
3579 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
3580 *
3581 * Description: Prints current's name, cpuset name, and cached copy of its
3582 * mems_allowed to the kernel log.
3583 */
cpuset_print_current_mems_allowed(void)3584 void cpuset_print_current_mems_allowed(void)
3585 {
3586 struct cgroup *cgrp;
3587
3588 rcu_read_lock();
3589
3590 cgrp = task_cs(current)->css.cgroup;
3591 pr_cont(",cpuset=");
3592 pr_cont_cgroup_name(cgrp);
3593 pr_cont(",mems_allowed=%*pbl",
3594 nodemask_pr_args(¤t->mems_allowed));
3595
3596 rcu_read_unlock();
3597 }
3598
3599 /*
3600 * Collection of memory_pressure is suppressed unless
3601 * this flag is enabled by writing "1" to the special
3602 * cpuset file 'memory_pressure_enabled' in the root cpuset.
3603 */
3604
3605 int cpuset_memory_pressure_enabled __read_mostly;
3606
3607 /**
3608 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
3609 *
3610 * Keep a running average of the rate of synchronous (direct)
3611 * page reclaim efforts initiated by tasks in each cpuset.
3612 *
3613 * This represents the rate at which some task in the cpuset
3614 * ran low on memory on all nodes it was allowed to use, and
3615 * had to enter the kernels page reclaim code in an effort to
3616 * create more free memory by tossing clean pages or swapping
3617 * or writing dirty pages.
3618 *
3619 * Display to user space in the per-cpuset read-only file
3620 * "memory_pressure". Value displayed is an integer
3621 * representing the recent rate of entry into the synchronous
3622 * (direct) page reclaim by any task attached to the cpuset.
3623 **/
3624
__cpuset_memory_pressure_bump(void)3625 void __cpuset_memory_pressure_bump(void)
3626 {
3627 rcu_read_lock();
3628 fmeter_markevent(&task_cs(current)->fmeter);
3629 rcu_read_unlock();
3630 }
3631
3632 #ifdef CONFIG_PROC_PID_CPUSET
3633 /*
3634 * proc_cpuset_show()
3635 * - Print tasks cpuset path into seq_file.
3636 * - Used for /proc/<pid>/cpuset.
3637 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
3638 * doesn't really matter if tsk->cpuset changes after we read it,
3639 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
3640 * anyway.
3641 */
proc_cpuset_show(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * tsk)3642 int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
3643 struct pid *pid, struct task_struct *tsk)
3644 {
3645 char *buf;
3646 struct cgroup_subsys_state *css;
3647 int retval;
3648
3649 retval = -ENOMEM;
3650 buf = kmalloc(PATH_MAX, GFP_KERNEL);
3651 if (!buf)
3652 goto out;
3653
3654 css = task_get_css(tsk, cpuset_cgrp_id);
3655 retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX,
3656 current->nsproxy->cgroup_ns);
3657 css_put(css);
3658 if (retval >= PATH_MAX)
3659 retval = -ENAMETOOLONG;
3660 if (retval < 0)
3661 goto out_free;
3662 seq_puts(m, buf);
3663 seq_putc(m, '\n');
3664 retval = 0;
3665 out_free:
3666 kfree(buf);
3667 out:
3668 return retval;
3669 }
3670 #endif /* CONFIG_PROC_PID_CPUSET */
3671
3672 /* Display task mems_allowed in /proc/<pid>/status file. */
cpuset_task_status_allowed(struct seq_file * m,struct task_struct * task)3673 void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
3674 {
3675 seq_printf(m, "Mems_allowed:\t%*pb\n",
3676 nodemask_pr_args(&task->mems_allowed));
3677 seq_printf(m, "Mems_allowed_list:\t%*pbl\n",
3678 nodemask_pr_args(&task->mems_allowed));
3679 }
3680