4 * Processor and Memory placement constraints for sets of tasks.
6 * Copyright (C) 2003 BULL SA.
7 * Copyright (C) 2004-2007 Silicon Graphics, Inc.
8 * Copyright (C) 2006 Google, Inc
10 * Portions derived from Patrick Mochel's sysfs code.
11 * sysfs is Copyright (c) 2001-3 Patrick Mochel
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
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.
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>
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>
39 #include <linux/memory.h>
40 #include <linux/export.h>
41 #include <linux/mount.h>
42 #include <linux/namei.h>
43 #include <linux/pagemap.h>
44 #include <linux/proc_fs.h>
45 #include <linux/rcupdate.h>
46 #include <linux/sched.h>
47 #include <linux/seq_file.h>
48 #include <linux/security.h>
49 #include <linux/slab.h>
50 #include <linux/spinlock.h>
51 #include <linux/stat.h>
52 #include <linux/string.h>
53 #include <linux/time.h>
54 #include <linux/backing-dev.h>
55 #include <linux/sort.h>
57 #include <asm/uaccess.h>
58 #include <linux/atomic.h>
59 #include <linux/mutex.h>
60 #include <linux/cgroup.h>
61 #include <linux/wait.h>
63 struct static_key cpusets_pre_enable_key __read_mostly
= STATIC_KEY_INIT_FALSE
;
64 struct static_key cpusets_enabled_key __read_mostly
= STATIC_KEY_INIT_FALSE
;
66 /* See "Frequency meter" comments, below. */
69 int cnt
; /* unprocessed events count */
70 int val
; /* most recent output value */
71 time_t time
; /* clock (secs) when val computed */
72 spinlock_t lock
; /* guards read or write of above */
76 struct cgroup_subsys_state css
;
78 unsigned long flags
; /* "unsigned long" so bitops work */
81 * On default hierarchy:
83 * The user-configured masks can only be changed by writing to
84 * cpuset.cpus and cpuset.mems, and won't be limited by the
87 * The effective masks is the real masks that apply to the tasks
88 * in the cpuset. They may be changed if the configured masks are
89 * changed or hotplug happens.
91 * effective_mask == configured_mask & parent's effective_mask,
92 * and if it ends up empty, it will inherit the parent's mask.
97 * The user-configured masks are always the same with effective masks.
100 /* user-configured CPUs and Memory Nodes allow to tasks */
101 cpumask_var_t cpus_allowed
;
102 cpumask_var_t cpus_requested
;
103 nodemask_t mems_allowed
;
105 /* effective CPUs and Memory Nodes allow to tasks */
106 cpumask_var_t effective_cpus
;
107 nodemask_t effective_mems
;
110 * This is old Memory Nodes tasks took on.
112 * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
113 * - A new cpuset's old_mems_allowed is initialized when some
114 * task is moved into it.
115 * - old_mems_allowed is used in cpuset_migrate_mm() when we change
116 * cpuset.mems_allowed and have tasks' nodemask updated, and
117 * then old_mems_allowed is updated to mems_allowed.
119 nodemask_t old_mems_allowed
;
121 struct fmeter fmeter
; /* memory_pressure filter */
124 * Tasks are being attached to this cpuset. Used to prevent
125 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
127 int attach_in_progress
;
129 /* partition number for rebuild_sched_domains() */
132 /* for custom sched domain */
133 int relax_domain_level
;
136 static inline struct cpuset
*css_cs(struct cgroup_subsys_state
*css
)
138 return css
? container_of(css
, struct cpuset
, css
) : NULL
;
141 /* Retrieve the cpuset for a task */
142 static inline struct cpuset
*task_cs(struct task_struct
*task
)
144 return css_cs(task_css(task
, cpuset_cgrp_id
));
147 static inline struct cpuset
*parent_cs(struct cpuset
*cs
)
149 return css_cs(cs
->css
.parent
);
153 static inline bool task_has_mempolicy(struct task_struct
*task
)
155 return task
->mempolicy
;
158 static inline bool task_has_mempolicy(struct task_struct
*task
)
165 /* bits in struct cpuset flags field */
172 CS_SCHED_LOAD_BALANCE
,
178 /* convenient tests for these bits */
179 static inline bool is_cpuset_online(struct cpuset
*cs
)
181 return test_bit(CS_ONLINE
, &cs
->flags
) && !css_is_dying(&cs
->css
);
184 static inline int is_cpu_exclusive(const struct cpuset
*cs
)
186 return test_bit(CS_CPU_EXCLUSIVE
, &cs
->flags
);
189 static inline int is_mem_exclusive(const struct cpuset
*cs
)
191 return test_bit(CS_MEM_EXCLUSIVE
, &cs
->flags
);
194 static inline int is_mem_hardwall(const struct cpuset
*cs
)
196 return test_bit(CS_MEM_HARDWALL
, &cs
->flags
);
199 static inline int is_sched_load_balance(const struct cpuset
*cs
)
201 return test_bit(CS_SCHED_LOAD_BALANCE
, &cs
->flags
);
204 static inline int is_memory_migrate(const struct cpuset
*cs
)
206 return test_bit(CS_MEMORY_MIGRATE
, &cs
->flags
);
209 static inline int is_spread_page(const struct cpuset
*cs
)
211 return test_bit(CS_SPREAD_PAGE
, &cs
->flags
);
214 static inline int is_spread_slab(const struct cpuset
*cs
)
216 return test_bit(CS_SPREAD_SLAB
, &cs
->flags
);
219 static struct cpuset top_cpuset
= {
220 .flags
= ((1 << CS_ONLINE
) | (1 << CS_CPU_EXCLUSIVE
) |
221 (1 << CS_MEM_EXCLUSIVE
)),
224 static inline int is_family_boost_enabled(const struct cpuset
*cs
)
226 return test_bit(CS_FAMILY_BOOST
, &cs
->flags
);
230 * cpuset_for_each_child - traverse online children of a cpuset
231 * @child_cs: loop cursor pointing to the current child
232 * @pos_css: used for iteration
233 * @parent_cs: target cpuset to walk children of
235 * Walk @child_cs through the online children of @parent_cs. Must be used
236 * with RCU read locked.
238 #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \
239 css_for_each_child((pos_css), &(parent_cs)->css) \
240 if (is_cpuset_online(((child_cs) = css_cs((pos_css)))))
243 * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
244 * @des_cs: loop cursor pointing to the current descendant
245 * @pos_css: used for iteration
246 * @root_cs: target cpuset to walk ancestor of
248 * Walk @des_cs through the online descendants of @root_cs. Must be used
249 * with RCU read locked. The caller may modify @pos_css by calling
250 * css_rightmost_descendant() to skip subtree. @root_cs is included in the
251 * iteration and the first node to be visited.
253 #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \
254 css_for_each_descendant_pre((pos_css), &(root_cs)->css) \
255 if (is_cpuset_online(((des_cs) = css_cs((pos_css)))))
258 * There are two global locks guarding cpuset structures - cpuset_mutex and
259 * callback_lock. We also require taking task_lock() when dereferencing a
260 * task's cpuset pointer. See "The task_lock() exception", at the end of this
263 * A task must hold both locks to modify cpusets. If a task holds
264 * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it
265 * is the only task able to also acquire callback_lock and be able to
266 * modify cpusets. It can perform various checks on the cpuset structure
267 * first, knowing nothing will change. It can also allocate memory while
268 * just holding cpuset_mutex. While it is performing these checks, various
269 * callback routines can briefly acquire callback_lock to query cpusets.
270 * Once it is ready to make the changes, it takes callback_lock, blocking
273 * Calls to the kernel memory allocator can not be made while holding
274 * callback_lock, as that would risk double tripping on callback_lock
275 * from one of the callbacks into the cpuset code from within
278 * If a task is only holding callback_lock, then it has read-only
281 * Now, the task_struct fields mems_allowed and mempolicy may be changed
282 * by other task, we use alloc_lock in the task_struct fields to protect
285 * The cpuset_common_file_read() handlers only hold callback_lock across
286 * small pieces of code, such as when reading out possibly multi-word
287 * cpumasks and nodemasks.
289 * Accessing a task's cpuset should be done in accordance with the
290 * guidelines for accessing subsystem state in kernel/cgroup.c
293 static DEFINE_MUTEX(cpuset_mutex
);
294 static DEFINE_SPINLOCK(callback_lock
);
296 static struct workqueue_struct
*cpuset_migrate_mm_wq
;
299 * CPU / memory hotplug is handled asynchronously.
301 static void cpuset_hotplug_workfn(struct work_struct
*work
);
302 static DECLARE_WORK(cpuset_hotplug_work
, cpuset_hotplug_workfn
);
304 static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq
);
307 * This is ugly, but preserves the userspace API for existing cpuset
308 * users. If someone tries to mount the "cpuset" filesystem, we
309 * silently switch it to mount "cgroup" instead
311 static struct dentry
*cpuset_mount(struct file_system_type
*fs_type
,
312 int flags
, const char *unused_dev_name
, void *data
)
314 struct file_system_type
*cgroup_fs
= get_fs_type("cgroup");
315 struct dentry
*ret
= ERR_PTR(-ENODEV
);
319 "release_agent=/sbin/cpuset_release_agent";
320 ret
= cgroup_fs
->mount(cgroup_fs
, flags
,
321 unused_dev_name
, mountopts
);
322 put_filesystem(cgroup_fs
);
327 static struct file_system_type cpuset_fs_type
= {
329 .mount
= cpuset_mount
,
332 int is_top_task(struct task_struct
*p
)
334 struct cpuset
*cpuset_for_task
;
338 cpuset_for_task
= task_cs(p
);
339 ret
= is_family_boost_enabled(cpuset_for_task
);
344 EXPORT_SYMBOL(is_top_task
);
347 * Return in pmask the portion of a cpusets's cpus_allowed that
348 * are online. If none are online, walk up the cpuset hierarchy
349 * until we find one that does have some online cpus.
351 * One way or another, we guarantee to return some non-empty subset
352 * of cpu_online_mask.
354 * Call with callback_lock or cpuset_mutex held.
356 static void guarantee_online_cpus(struct cpuset
*cs
, struct cpumask
*pmask
)
358 while (!cpumask_intersects(cs
->effective_cpus
, cpu_online_mask
)) {
362 * The top cpuset doesn't have any online cpu as a
363 * consequence of a race between cpuset_hotplug_work
364 * and cpu hotplug notifier. But we know the top
365 * cpuset's effective_cpus is on its way to to be
366 * identical to cpu_online_mask.
368 cpumask_copy(pmask
, cpu_online_mask
);
372 cpumask_and(pmask
, cs
->effective_cpus
, cpu_online_mask
);
376 * Return in *pmask the portion of a cpusets's mems_allowed that
377 * are online, with memory. If none are online with memory, walk
378 * up the cpuset hierarchy until we find one that does have some
379 * online mems. The top cpuset always has some mems online.
381 * One way or another, we guarantee to return some non-empty subset
382 * of node_states[N_MEMORY].
384 * Call with callback_lock or cpuset_mutex held.
386 static void guarantee_online_mems(struct cpuset
*cs
, nodemask_t
*pmask
)
388 while (!nodes_intersects(cs
->effective_mems
, node_states
[N_MEMORY
]))
390 nodes_and(*pmask
, cs
->effective_mems
, node_states
[N_MEMORY
]);
394 * update task's spread flag if cpuset's page/slab spread flag is set
396 * Call with callback_lock or cpuset_mutex held.
398 static void cpuset_update_task_spread_flag(struct cpuset
*cs
,
399 struct task_struct
*tsk
)
401 if (is_spread_page(cs
))
402 task_set_spread_page(tsk
);
404 task_clear_spread_page(tsk
);
406 if (is_spread_slab(cs
))
407 task_set_spread_slab(tsk
);
409 task_clear_spread_slab(tsk
);
413 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
415 * One cpuset is a subset of another if all its allowed CPUs and
416 * Memory Nodes are a subset of the other, and its exclusive flags
417 * are only set if the other's are set. Call holding cpuset_mutex.
420 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
422 return cpumask_subset(p
->cpus_requested
, q
->cpus_requested
) &&
423 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
424 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
425 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
429 * alloc_trial_cpuset - allocate a trial cpuset
430 * @cs: the cpuset that the trial cpuset duplicates
432 static struct cpuset
*alloc_trial_cpuset(struct cpuset
*cs
)
434 struct cpuset
*trial
;
436 trial
= kmemdup(cs
, sizeof(*cs
), GFP_KERNEL
);
440 if (!alloc_cpumask_var(&trial
->cpus_allowed
, GFP_KERNEL
))
442 if (!alloc_cpumask_var(&trial
->effective_cpus
, GFP_KERNEL
))
445 cpumask_copy(trial
->cpus_allowed
, cs
->cpus_allowed
);
446 cpumask_copy(trial
->effective_cpus
, cs
->effective_cpus
);
450 free_cpumask_var(trial
->cpus_allowed
);
457 * free_trial_cpuset - free the trial cpuset
458 * @trial: the trial cpuset to be freed
460 static void free_trial_cpuset(struct cpuset
*trial
)
462 free_cpumask_var(trial
->effective_cpus
);
463 free_cpumask_var(trial
->cpus_allowed
);
468 * validate_change() - Used to validate that any proposed cpuset change
469 * follows the structural rules for cpusets.
471 * If we replaced the flag and mask values of the current cpuset
472 * (cur) with those values in the trial cpuset (trial), would
473 * our various subset and exclusive rules still be valid? Presumes
476 * 'cur' is the address of an actual, in-use cpuset. Operations
477 * such as list traversal that depend on the actual address of the
478 * cpuset in the list must use cur below, not trial.
480 * 'trial' is the address of bulk structure copy of cur, with
481 * perhaps one or more of the fields cpus_allowed, mems_allowed,
482 * or flags changed to new, trial values.
484 * Return 0 if valid, -errno if not.
487 static int validate_change(struct cpuset
*cur
, struct cpuset
*trial
)
489 struct cgroup_subsys_state
*css
;
490 struct cpuset
*c
, *par
;
495 /* Each of our child cpusets must be a subset of us */
497 cpuset_for_each_child(c
, css
, cur
)
498 if (!is_cpuset_subset(c
, trial
))
501 /* Remaining checks don't apply to root cpuset */
503 if (cur
== &top_cpuset
)
506 par
= parent_cs(cur
);
508 /* On legacy hiearchy, we must be a subset of our parent cpuset. */
510 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys
) &&
511 !is_cpuset_subset(trial
, par
))
515 * If either I or some sibling (!= me) is exclusive, we can't
519 cpuset_for_each_child(c
, css
, par
) {
520 if ((is_cpu_exclusive(trial
) || is_cpu_exclusive(c
)) &&
522 cpumask_intersects(trial
->cpus_requested
, c
->cpus_requested
))
524 if ((is_mem_exclusive(trial
) || is_mem_exclusive(c
)) &&
526 nodes_intersects(trial
->mems_allowed
, c
->mems_allowed
))
531 * Cpusets with tasks - existing or newly being attached - can't
532 * be changed to have empty cpus_allowed or mems_allowed.
535 if ((cgroup_is_populated(cur
->css
.cgroup
) || cur
->attach_in_progress
)) {
536 if (!cpumask_empty(cur
->cpus_allowed
) &&
537 cpumask_empty(trial
->cpus_allowed
))
539 if (!nodes_empty(cur
->mems_allowed
) &&
540 nodes_empty(trial
->mems_allowed
))
545 * We can't shrink if we won't have enough room for SCHED_DEADLINE
549 if (is_cpu_exclusive(cur
) &&
550 !cpuset_cpumask_can_shrink(cur
->cpus_allowed
,
551 trial
->cpus_allowed
))
562 * Helper routine for generate_sched_domains().
563 * Do cpusets a, b have overlapping effective cpus_allowed masks?
565 static int cpusets_overlap(struct cpuset
*a
, struct cpuset
*b
)
567 return cpumask_intersects(a
->effective_cpus
, b
->effective_cpus
);
571 update_domain_attr(struct sched_domain_attr
*dattr
, struct cpuset
*c
)
573 if (dattr
->relax_domain_level
< c
->relax_domain_level
)
574 dattr
->relax_domain_level
= c
->relax_domain_level
;
578 static void update_domain_attr_tree(struct sched_domain_attr
*dattr
,
579 struct cpuset
*root_cs
)
582 struct cgroup_subsys_state
*pos_css
;
585 cpuset_for_each_descendant_pre(cp
, pos_css
, root_cs
) {
586 /* skip the whole subtree if @cp doesn't have any CPU */
587 if (cpumask_empty(cp
->cpus_allowed
)) {
588 pos_css
= css_rightmost_descendant(pos_css
);
592 if (is_sched_load_balance(cp
))
593 update_domain_attr(dattr
, cp
);
599 * generate_sched_domains()
601 * This function builds a partial partition of the systems CPUs
602 * A 'partial partition' is a set of non-overlapping subsets whose
603 * union is a subset of that set.
604 * The output of this function needs to be passed to kernel/sched/core.c
605 * partition_sched_domains() routine, which will rebuild the scheduler's
606 * load balancing domains (sched domains) as specified by that partial
609 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
610 * for a background explanation of this.
612 * Does not return errors, on the theory that the callers of this
613 * routine would rather not worry about failures to rebuild sched
614 * domains when operating in the severe memory shortage situations
615 * that could cause allocation failures below.
617 * Must be called with cpuset_mutex held.
619 * The three key local variables below are:
620 * q - a linked-list queue of cpuset pointers, used to implement a
621 * top-down scan of all cpusets. This scan loads a pointer
622 * to each cpuset marked is_sched_load_balance into the
623 * array 'csa'. For our purposes, rebuilding the schedulers
624 * sched domains, we can ignore !is_sched_load_balance cpusets.
625 * csa - (for CpuSet Array) Array of pointers to all the cpusets
626 * that need to be load balanced, for convenient iterative
627 * access by the subsequent code that finds the best partition,
628 * i.e the set of domains (subsets) of CPUs such that the
629 * cpus_allowed of every cpuset marked is_sched_load_balance
630 * is a subset of one of these domains, while there are as
631 * many such domains as possible, each as small as possible.
632 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
633 * the kernel/sched/core.c routine partition_sched_domains() in a
634 * convenient format, that can be easily compared to the prior
635 * value to determine what partition elements (sched domains)
636 * were changed (added or removed.)
638 * Finding the best partition (set of domains):
639 * The triple nested loops below over i, j, k scan over the
640 * load balanced cpusets (using the array of cpuset pointers in
641 * csa[]) looking for pairs of cpusets that have overlapping
642 * cpus_allowed, but which don't have the same 'pn' partition
643 * number and gives them in the same partition number. It keeps
644 * looping on the 'restart' label until it can no longer find
647 * The union of the cpus_allowed masks from the set of
648 * all cpusets having the same 'pn' value then form the one
649 * element of the partition (one sched domain) to be passed to
650 * partition_sched_domains().
652 static int generate_sched_domains(cpumask_var_t
**domains
,
653 struct sched_domain_attr
**attributes
)
655 struct cpuset
*cp
; /* scans q */
656 struct cpuset
**csa
; /* array of all cpuset ptrs */
657 int csn
; /* how many cpuset ptrs in csa so far */
658 int i
, j
, k
; /* indices for partition finding loops */
659 cpumask_var_t
*doms
; /* resulting partition; i.e. sched domains */
660 cpumask_var_t non_isolated_cpus
; /* load balanced CPUs */
661 struct sched_domain_attr
*dattr
; /* attributes for custom domains */
662 int ndoms
= 0; /* number of sched domains in result */
663 int nslot
; /* next empty doms[] struct cpumask slot */
664 struct cgroup_subsys_state
*pos_css
;
670 if (!alloc_cpumask_var(&non_isolated_cpus
, GFP_KERNEL
))
672 cpumask_andnot(non_isolated_cpus
, cpu_possible_mask
, cpu_isolated_map
);
674 /* Special case for the 99% of systems with one, full, sched domain */
675 if (is_sched_load_balance(&top_cpuset
)) {
677 doms
= alloc_sched_domains(ndoms
);
681 dattr
= kmalloc(sizeof(struct sched_domain_attr
), GFP_KERNEL
);
683 *dattr
= SD_ATTR_INIT
;
684 update_domain_attr_tree(dattr
, &top_cpuset
);
686 cpumask_and(doms
[0], top_cpuset
.effective_cpus
,
692 csa
= kmalloc(nr_cpusets() * sizeof(cp
), GFP_KERNEL
);
698 cpuset_for_each_descendant_pre(cp
, pos_css
, &top_cpuset
) {
699 if (cp
== &top_cpuset
)
702 * Continue traversing beyond @cp iff @cp has some CPUs and
703 * isn't load balancing. The former is obvious. The
704 * latter: All child cpusets contain a subset of the
705 * parent's cpus, so just skip them, and then we call
706 * update_domain_attr_tree() to calc relax_domain_level of
707 * the corresponding sched domain.
709 if (!cpumask_empty(cp
->cpus_allowed
) &&
710 !(is_sched_load_balance(cp
) &&
711 cpumask_intersects(cp
->cpus_allowed
, non_isolated_cpus
)))
714 if (is_sched_load_balance(cp
))
717 /* skip @cp's subtree */
718 pos_css
= css_rightmost_descendant(pos_css
);
722 for (i
= 0; i
< csn
; i
++)
727 /* Find the best partition (set of sched domains) */
728 for (i
= 0; i
< csn
; i
++) {
729 struct cpuset
*a
= csa
[i
];
732 for (j
= 0; j
< csn
; j
++) {
733 struct cpuset
*b
= csa
[j
];
736 if (apn
!= bpn
&& cpusets_overlap(a
, b
)) {
737 for (k
= 0; k
< csn
; k
++) {
738 struct cpuset
*c
= csa
[k
];
743 ndoms
--; /* one less element */
750 * Now we know how many domains to create.
751 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
753 doms
= alloc_sched_domains(ndoms
);
758 * The rest of the code, including the scheduler, can deal with
759 * dattr==NULL case. No need to abort if alloc fails.
761 dattr
= kmalloc(ndoms
* sizeof(struct sched_domain_attr
), GFP_KERNEL
);
763 for (nslot
= 0, i
= 0; i
< csn
; i
++) {
764 struct cpuset
*a
= csa
[i
];
769 /* Skip completed partitions */
775 if (nslot
== ndoms
) {
776 static int warnings
= 10;
778 pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n",
779 nslot
, ndoms
, csn
, i
, apn
);
787 *(dattr
+ nslot
) = SD_ATTR_INIT
;
788 for (j
= i
; j
< csn
; j
++) {
789 struct cpuset
*b
= csa
[j
];
792 cpumask_or(dp
, dp
, b
->effective_cpus
);
793 cpumask_and(dp
, dp
, non_isolated_cpus
);
795 update_domain_attr_tree(dattr
+ nslot
, b
);
797 /* Done with this partition */
803 BUG_ON(nslot
!= ndoms
);
806 free_cpumask_var(non_isolated_cpus
);
810 * Fallback to the default domain if kmalloc() failed.
811 * See comments in partition_sched_domains().
822 * Rebuild scheduler domains.
824 * If the flag 'sched_load_balance' of any cpuset with non-empty
825 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
826 * which has that flag enabled, or if any cpuset with a non-empty
827 * 'cpus' is removed, then call this routine to rebuild the
828 * scheduler's dynamic sched domains.
830 * Call with cpuset_mutex held. Takes get_online_cpus().
832 static void rebuild_sched_domains_locked(void)
834 struct sched_domain_attr
*attr
;
838 lockdep_assert_held(&cpuset_mutex
);
842 * We have raced with CPU hotplug. Don't do anything to avoid
843 * passing doms with offlined cpu to partition_sched_domains().
844 * Anyways, hotplug work item will rebuild sched domains.
846 if (!cpumask_equal(top_cpuset
.effective_cpus
, cpu_active_mask
))
849 /* Generate domain masks and attrs */
850 ndoms
= generate_sched_domains(&doms
, &attr
);
852 /* Have scheduler rebuild the domains */
853 partition_sched_domains(ndoms
, doms
, attr
);
857 #else /* !CONFIG_SMP */
858 static void rebuild_sched_domains_locked(void)
861 #endif /* CONFIG_SMP */
863 void rebuild_sched_domains(void)
865 mutex_lock(&cpuset_mutex
);
866 rebuild_sched_domains_locked();
867 mutex_unlock(&cpuset_mutex
);
871 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
872 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
874 * Iterate through each task of @cs updating its cpus_allowed to the
875 * effective cpuset's. As this function is called with cpuset_mutex held,
876 * cpuset membership stays stable.
878 static void update_tasks_cpumask(struct cpuset
*cs
)
880 struct css_task_iter it
;
881 struct task_struct
*task
;
883 css_task_iter_start(&cs
->css
, &it
);
884 while ((task
= css_task_iter_next(&it
)))
885 set_cpus_allowed_ptr(task
, cs
->effective_cpus
);
886 css_task_iter_end(&it
);
890 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
891 * @cs: the cpuset to consider
892 * @new_cpus: temp variable for calculating new effective_cpus
894 * When congifured cpumask is changed, the effective cpumasks of this cpuset
895 * and all its descendants need to be updated.
897 * On legacy hierachy, effective_cpus will be the same with cpu_allowed.
899 * Called with cpuset_mutex held
901 static void update_cpumasks_hier(struct cpuset
*cs
, struct cpumask
*new_cpus
)
904 struct cgroup_subsys_state
*pos_css
;
905 bool need_rebuild_sched_domains
= false;
908 cpuset_for_each_descendant_pre(cp
, pos_css
, cs
) {
909 struct cpuset
*parent
= parent_cs(cp
);
911 cpumask_and(new_cpus
, cp
->cpus_allowed
, parent
->effective_cpus
);
914 * If it becomes empty, inherit the effective mask of the
915 * parent, which is guaranteed to have some CPUs.
917 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys
) &&
918 cpumask_empty(new_cpus
))
919 cpumask_copy(new_cpus
, parent
->effective_cpus
);
921 /* Skip the whole subtree if the cpumask remains the same. */
922 if (cpumask_equal(new_cpus
, cp
->effective_cpus
)) {
923 pos_css
= css_rightmost_descendant(pos_css
);
927 if (!css_tryget_online(&cp
->css
))
931 spin_lock_irq(&callback_lock
);
932 cpumask_copy(cp
->effective_cpus
, new_cpus
);
933 spin_unlock_irq(&callback_lock
);
935 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys
) &&
936 !cpumask_equal(cp
->cpus_allowed
, cp
->effective_cpus
));
938 update_tasks_cpumask(cp
);
941 * If the effective cpumask of any non-empty cpuset is changed,
942 * we need to rebuild sched domains.
944 if (!cpumask_empty(cp
->cpus_allowed
) &&
945 is_sched_load_balance(cp
))
946 need_rebuild_sched_domains
= true;
953 if (need_rebuild_sched_domains
)
954 rebuild_sched_domains_locked();
958 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
959 * @cs: the cpuset to consider
960 * @trialcs: trial cpuset
961 * @buf: buffer of cpu numbers written to this cpuset
963 static int update_cpumask(struct cpuset
*cs
, struct cpuset
*trialcs
,
968 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
969 if (cs
== &top_cpuset
)
973 * An empty cpus_allowed is ok only if the cpuset has no tasks.
974 * Since cpulist_parse() fails on an empty mask, we special case
975 * that parsing. The validate_change() call ensures that cpusets
976 * with tasks have cpus.
979 cpumask_clear(trialcs
->cpus_allowed
);
981 retval
= cpulist_parse(buf
, trialcs
->cpus_requested
);
985 if (!cpumask_subset(trialcs
->cpus_requested
, cpu_present_mask
))
988 cpumask_and(trialcs
->cpus_allowed
, trialcs
->cpus_requested
, cpu_active_mask
);
991 /* Nothing to do if the cpus didn't change */
992 if (cpumask_equal(cs
->cpus_requested
, trialcs
->cpus_requested
))
995 retval
= validate_change(cs
, trialcs
);
999 spin_lock_irq(&callback_lock
);
1000 cpumask_copy(cs
->cpus_allowed
, trialcs
->cpus_allowed
);
1001 cpumask_copy(cs
->cpus_requested
, trialcs
->cpus_requested
);
1002 spin_unlock_irq(&callback_lock
);
1004 /* use trialcs->cpus_allowed as a temp variable */
1005 update_cpumasks_hier(cs
, trialcs
->cpus_allowed
);
1010 * Migrate memory region from one set of nodes to another. This is
1011 * performed asynchronously as it can be called from process migration path
1012 * holding locks involved in process management. All mm migrations are
1013 * performed in the queued order and can be waited for by flushing
1014 * cpuset_migrate_mm_wq.
1017 struct cpuset_migrate_mm_work
{
1018 struct work_struct work
;
1019 struct mm_struct
*mm
;
1024 static void cpuset_migrate_mm_workfn(struct work_struct
*work
)
1026 struct cpuset_migrate_mm_work
*mwork
=
1027 container_of(work
, struct cpuset_migrate_mm_work
, work
);
1029 /* on a wq worker, no need to worry about %current's mems_allowed */
1030 do_migrate_pages(mwork
->mm
, &mwork
->from
, &mwork
->to
, MPOL_MF_MOVE_ALL
);
1035 static void cpuset_migrate_mm(struct mm_struct
*mm
, const nodemask_t
*from
,
1036 const nodemask_t
*to
)
1038 struct cpuset_migrate_mm_work
*mwork
;
1040 mwork
= kzalloc(sizeof(*mwork
), GFP_KERNEL
);
1043 mwork
->from
= *from
;
1045 INIT_WORK(&mwork
->work
, cpuset_migrate_mm_workfn
);
1046 queue_work(cpuset_migrate_mm_wq
, &mwork
->work
);
1052 static void cpuset_post_attach(void)
1054 flush_workqueue(cpuset_migrate_mm_wq
);
1058 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
1059 * @tsk: the task to change
1060 * @newmems: new nodes that the task will be set
1062 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
1063 * we structure updates as setting all new allowed nodes, then clearing newly
1066 static void cpuset_change_task_nodemask(struct task_struct
*tsk
,
1067 nodemask_t
*newmems
)
1072 * Allow tasks that have access to memory reserves because they have
1073 * been OOM killed to get memory anywhere.
1075 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
1077 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
1082 * Determine if a loop is necessary if another thread is doing
1083 * read_mems_allowed_begin(). If at least one node remains unchanged and
1084 * tsk does not have a mempolicy, then an empty nodemask will not be
1085 * possible when mems_allowed is larger than a word.
1087 need_loop
= task_has_mempolicy(tsk
) ||
1088 !nodes_intersects(*newmems
, tsk
->mems_allowed
);
1091 local_irq_disable();
1092 write_seqcount_begin(&tsk
->mems_allowed_seq
);
1095 nodes_or(tsk
->mems_allowed
, tsk
->mems_allowed
, *newmems
);
1096 mpol_rebind_task(tsk
, newmems
, MPOL_REBIND_STEP1
);
1098 mpol_rebind_task(tsk
, newmems
, MPOL_REBIND_STEP2
);
1099 tsk
->mems_allowed
= *newmems
;
1102 write_seqcount_end(&tsk
->mems_allowed_seq
);
1109 static void *cpuset_being_rebound
;
1112 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1113 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1115 * Iterate through each task of @cs updating its mems_allowed to the
1116 * effective cpuset's. As this function is called with cpuset_mutex held,
1117 * cpuset membership stays stable.
1119 static void update_tasks_nodemask(struct cpuset
*cs
)
1121 static nodemask_t newmems
; /* protected by cpuset_mutex */
1122 struct css_task_iter it
;
1123 struct task_struct
*task
;
1125 cpuset_being_rebound
= cs
; /* causes mpol_dup() rebind */
1127 guarantee_online_mems(cs
, &newmems
);
1130 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1131 * take while holding tasklist_lock. Forks can happen - the
1132 * mpol_dup() cpuset_being_rebound check will catch such forks,
1133 * and rebind their vma mempolicies too. Because we still hold
1134 * the global cpuset_mutex, we know that no other rebind effort
1135 * will be contending for the global variable cpuset_being_rebound.
1136 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
1137 * is idempotent. Also migrate pages in each mm to new nodes.
1139 css_task_iter_start(&cs
->css
, &it
);
1140 while ((task
= css_task_iter_next(&it
))) {
1141 struct mm_struct
*mm
;
1144 cpuset_change_task_nodemask(task
, &newmems
);
1146 mm
= get_task_mm(task
);
1150 migrate
= is_memory_migrate(cs
);
1152 mpol_rebind_mm(mm
, &cs
->mems_allowed
);
1154 cpuset_migrate_mm(mm
, &cs
->old_mems_allowed
, &newmems
);
1158 css_task_iter_end(&it
);
1161 * All the tasks' nodemasks have been updated, update
1162 * cs->old_mems_allowed.
1164 cs
->old_mems_allowed
= newmems
;
1166 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
1167 cpuset_being_rebound
= NULL
;
1171 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1172 * @cs: the cpuset to consider
1173 * @new_mems: a temp variable for calculating new effective_mems
1175 * When configured nodemask is changed, the effective nodemasks of this cpuset
1176 * and all its descendants need to be updated.
1178 * On legacy hiearchy, effective_mems will be the same with mems_allowed.
1180 * Called with cpuset_mutex held
1182 static void update_nodemasks_hier(struct cpuset
*cs
, nodemask_t
*new_mems
)
1185 struct cgroup_subsys_state
*pos_css
;
1188 cpuset_for_each_descendant_pre(cp
, pos_css
, cs
) {
1189 struct cpuset
*parent
= parent_cs(cp
);
1191 nodes_and(*new_mems
, cp
->mems_allowed
, parent
->effective_mems
);
1194 * If it becomes empty, inherit the effective mask of the
1195 * parent, which is guaranteed to have some MEMs.
1197 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys
) &&
1198 nodes_empty(*new_mems
))
1199 *new_mems
= parent
->effective_mems
;
1201 /* Skip the whole subtree if the nodemask remains the same. */
1202 if (nodes_equal(*new_mems
, cp
->effective_mems
)) {
1203 pos_css
= css_rightmost_descendant(pos_css
);
1207 if (!css_tryget_online(&cp
->css
))
1211 spin_lock_irq(&callback_lock
);
1212 cp
->effective_mems
= *new_mems
;
1213 spin_unlock_irq(&callback_lock
);
1215 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys
) &&
1216 !nodes_equal(cp
->mems_allowed
, cp
->effective_mems
));
1218 update_tasks_nodemask(cp
);
1227 * Handle user request to change the 'mems' memory placement
1228 * of a cpuset. Needs to validate the request, update the
1229 * cpusets mems_allowed, and for each task in the cpuset,
1230 * update mems_allowed and rebind task's mempolicy and any vma
1231 * mempolicies and if the cpuset is marked 'memory_migrate',
1232 * migrate the tasks pages to the new memory.
1234 * Call with cpuset_mutex held. May take callback_lock during call.
1235 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1236 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1237 * their mempolicies to the cpusets new mems_allowed.
1239 static int update_nodemask(struct cpuset
*cs
, struct cpuset
*trialcs
,
1245 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
1248 if (cs
== &top_cpuset
) {
1254 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1255 * Since nodelist_parse() fails on an empty mask, we special case
1256 * that parsing. The validate_change() call ensures that cpusets
1257 * with tasks have memory.
1260 nodes_clear(trialcs
->mems_allowed
);
1262 retval
= nodelist_parse(buf
, trialcs
->mems_allowed
);
1266 if (!nodes_subset(trialcs
->mems_allowed
,
1267 top_cpuset
.mems_allowed
)) {
1273 if (nodes_equal(cs
->mems_allowed
, trialcs
->mems_allowed
)) {
1274 retval
= 0; /* Too easy - nothing to do */
1277 retval
= validate_change(cs
, trialcs
);
1281 spin_lock_irq(&callback_lock
);
1282 cs
->mems_allowed
= trialcs
->mems_allowed
;
1283 spin_unlock_irq(&callback_lock
);
1285 /* use trialcs->mems_allowed as a temp variable */
1286 update_nodemasks_hier(cs
, &trialcs
->mems_allowed
);
1291 int current_cpuset_is_being_rebound(void)
1296 ret
= task_cs(current
) == cpuset_being_rebound
;
1302 static int update_relax_domain_level(struct cpuset
*cs
, s64 val
)
1305 if (val
< -1 || val
>= sched_domain_level_max
)
1309 if (val
!= cs
->relax_domain_level
) {
1310 cs
->relax_domain_level
= val
;
1311 if (!cpumask_empty(cs
->cpus_allowed
) &&
1312 is_sched_load_balance(cs
))
1313 rebuild_sched_domains_locked();
1320 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1321 * @cs: the cpuset in which each task's spread flags needs to be changed
1323 * Iterate through each task of @cs updating its spread flags. As this
1324 * function is called with cpuset_mutex held, cpuset membership stays
1327 static void update_tasks_flags(struct cpuset
*cs
)
1329 struct css_task_iter it
;
1330 struct task_struct
*task
;
1332 css_task_iter_start(&cs
->css
, &it
);
1333 while ((task
= css_task_iter_next(&it
)))
1334 cpuset_update_task_spread_flag(cs
, task
);
1335 css_task_iter_end(&it
);
1339 * update_flag - read a 0 or a 1 in a file and update associated flag
1340 * bit: the bit to update (see cpuset_flagbits_t)
1341 * cs: the cpuset to update
1342 * turning_on: whether the flag is being set or cleared
1344 * Call with cpuset_mutex held.
1347 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
,
1350 struct cpuset
*trialcs
;
1351 int balance_flag_changed
;
1352 int spread_flag_changed
;
1355 trialcs
= alloc_trial_cpuset(cs
);
1360 set_bit(bit
, &trialcs
->flags
);
1362 clear_bit(bit
, &trialcs
->flags
);
1364 err
= validate_change(cs
, trialcs
);
1368 balance_flag_changed
= (is_sched_load_balance(cs
) !=
1369 is_sched_load_balance(trialcs
));
1371 spread_flag_changed
= ((is_spread_slab(cs
) != is_spread_slab(trialcs
))
1372 || (is_spread_page(cs
) != is_spread_page(trialcs
)));
1374 spin_lock_irq(&callback_lock
);
1375 cs
->flags
= trialcs
->flags
;
1376 spin_unlock_irq(&callback_lock
);
1378 if (!cpumask_empty(trialcs
->cpus_allowed
) && balance_flag_changed
)
1379 rebuild_sched_domains_locked();
1381 if (spread_flag_changed
)
1382 update_tasks_flags(cs
);
1384 free_trial_cpuset(trialcs
);
1389 * Frequency meter - How fast is some event occurring?
1391 * These routines manage a digitally filtered, constant time based,
1392 * event frequency meter. There are four routines:
1393 * fmeter_init() - initialize a frequency meter.
1394 * fmeter_markevent() - called each time the event happens.
1395 * fmeter_getrate() - returns the recent rate of such events.
1396 * fmeter_update() - internal routine used to update fmeter.
1398 * A common data structure is passed to each of these routines,
1399 * which is used to keep track of the state required to manage the
1400 * frequency meter and its digital filter.
1402 * The filter works on the number of events marked per unit time.
1403 * The filter is single-pole low-pass recursive (IIR). The time unit
1404 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1405 * simulate 3 decimal digits of precision (multiplied by 1000).
1407 * With an FM_COEF of 933, and a time base of 1 second, the filter
1408 * has a half-life of 10 seconds, meaning that if the events quit
1409 * happening, then the rate returned from the fmeter_getrate()
1410 * will be cut in half each 10 seconds, until it converges to zero.
1412 * It is not worth doing a real infinitely recursive filter. If more
1413 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1414 * just compute FM_MAXTICKS ticks worth, by which point the level
1417 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1418 * arithmetic overflow in the fmeter_update() routine.
1420 * Given the simple 32 bit integer arithmetic used, this meter works
1421 * best for reporting rates between one per millisecond (msec) and
1422 * one per 32 (approx) seconds. At constant rates faster than one
1423 * per msec it maxes out at values just under 1,000,000. At constant
1424 * rates between one per msec, and one per second it will stabilize
1425 * to a value N*1000, where N is the rate of events per second.
1426 * At constant rates between one per second and one per 32 seconds,
1427 * it will be choppy, moving up on the seconds that have an event,
1428 * and then decaying until the next event. At rates slower than
1429 * about one in 32 seconds, it decays all the way back to zero between
1433 #define FM_COEF 933 /* coefficient for half-life of 10 secs */
1434 #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1435 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1436 #define FM_SCALE 1000 /* faux fixed point scale */
1438 /* Initialize a frequency meter */
1439 static void fmeter_init(struct fmeter
*fmp
)
1444 spin_lock_init(&fmp
->lock
);
1447 /* Internal meter update - process cnt events and update value */
1448 static void fmeter_update(struct fmeter
*fmp
)
1450 time_t now
= get_seconds();
1451 time_t ticks
= now
- fmp
->time
;
1456 ticks
= min(FM_MAXTICKS
, ticks
);
1458 fmp
->val
= (FM_COEF
* fmp
->val
) / FM_SCALE
;
1461 fmp
->val
+= ((FM_SCALE
- FM_COEF
) * fmp
->cnt
) / FM_SCALE
;
1465 /* Process any previous ticks, then bump cnt by one (times scale). */
1466 static void fmeter_markevent(struct fmeter
*fmp
)
1468 spin_lock(&fmp
->lock
);
1470 fmp
->cnt
= min(FM_MAXCNT
, fmp
->cnt
+ FM_SCALE
);
1471 spin_unlock(&fmp
->lock
);
1474 /* Process any previous ticks, then return current value. */
1475 static int fmeter_getrate(struct fmeter
*fmp
)
1479 spin_lock(&fmp
->lock
);
1482 spin_unlock(&fmp
->lock
);
1486 static struct cpuset
*cpuset_attach_old_cs
;
1488 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
1489 static int cpuset_can_attach(struct cgroup_taskset
*tset
)
1491 struct cgroup_subsys_state
*css
;
1493 struct task_struct
*task
;
1496 /* used later by cpuset_attach() */
1497 cpuset_attach_old_cs
= task_cs(cgroup_taskset_first(tset
, &css
));
1500 mutex_lock(&cpuset_mutex
);
1502 /* allow moving tasks into an empty cpuset if on default hierarchy */
1504 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys
) &&
1505 (cpumask_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
)))
1508 cgroup_taskset_for_each(task
, css
, tset
) {
1509 ret
= task_can_attach(task
, cs
->cpus_allowed
);
1512 ret
= security_task_setscheduler(task
);
1518 * Mark attach is in progress. This makes validate_change() fail
1519 * changes which zero cpus/mems_allowed.
1521 cs
->attach_in_progress
++;
1524 mutex_unlock(&cpuset_mutex
);
1528 static void cpuset_cancel_attach(struct cgroup_taskset
*tset
)
1530 struct cgroup_subsys_state
*css
;
1533 cgroup_taskset_first(tset
, &css
);
1536 mutex_lock(&cpuset_mutex
);
1537 css_cs(css
)->attach_in_progress
--;
1538 mutex_unlock(&cpuset_mutex
);
1542 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
1543 * but we can't allocate it dynamically there. Define it global and
1544 * allocate from cpuset_init().
1546 static cpumask_var_t cpus_attach
;
1548 static void cpuset_attach(struct cgroup_taskset
*tset
)
1550 /* static buf protected by cpuset_mutex */
1551 static nodemask_t cpuset_attach_nodemask_to
;
1552 struct task_struct
*task
;
1553 struct task_struct
*leader
;
1554 struct cgroup_subsys_state
*css
;
1556 struct cpuset
*oldcs
= cpuset_attach_old_cs
;
1558 cgroup_taskset_first(tset
, &css
);
1561 mutex_lock(&cpuset_mutex
);
1563 /* prepare for attach */
1564 if (cs
== &top_cpuset
)
1565 cpumask_copy(cpus_attach
, cpu_possible_mask
);
1567 guarantee_online_cpus(cs
, cpus_attach
);
1569 guarantee_online_mems(cs
, &cpuset_attach_nodemask_to
);
1571 cgroup_taskset_for_each(task
, css
, tset
) {
1573 * can_attach beforehand should guarantee that this doesn't
1574 * fail. TODO: have a better way to handle failure here
1576 WARN_ON_ONCE(set_cpus_allowed_ptr(task
, cpus_attach
));
1578 cpuset_change_task_nodemask(task
, &cpuset_attach_nodemask_to
);
1579 cpuset_update_task_spread_flag(cs
, task
);
1583 * Change mm for all threadgroup leaders. This is expensive and may
1584 * sleep and should be moved outside migration path proper.
1586 cpuset_attach_nodemask_to
= cs
->effective_mems
;
1587 cgroup_taskset_for_each_leader(leader
, css
, tset
) {
1588 struct mm_struct
*mm
= get_task_mm(leader
);
1591 mpol_rebind_mm(mm
, &cpuset_attach_nodemask_to
);
1594 * old_mems_allowed is the same with mems_allowed
1595 * here, except if this task is being moved
1596 * automatically due to hotplug. In that case
1597 * @mems_allowed has been updated and is empty, so
1598 * @old_mems_allowed is the right nodesets that we
1601 if (is_memory_migrate(cs
))
1602 cpuset_migrate_mm(mm
, &oldcs
->old_mems_allowed
,
1603 &cpuset_attach_nodemask_to
);
1609 cs
->old_mems_allowed
= cpuset_attach_nodemask_to
;
1611 cs
->attach_in_progress
--;
1612 if (!cs
->attach_in_progress
)
1613 wake_up(&cpuset_attach_wq
);
1615 mutex_unlock(&cpuset_mutex
);
1618 /* The various types of files and directories in a cpuset file system */
1621 FILE_MEMORY_MIGRATE
,
1624 FILE_EFFECTIVE_CPULIST
,
1625 FILE_EFFECTIVE_MEMLIST
,
1629 FILE_SCHED_LOAD_BALANCE
,
1630 FILE_SCHED_RELAX_DOMAIN_LEVEL
,
1631 FILE_MEMORY_PRESSURE_ENABLED
,
1632 FILE_MEMORY_PRESSURE
,
1636 } cpuset_filetype_t
;
1638 static int cpuset_write_u64(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
1641 struct cpuset
*cs
= css_cs(css
);
1642 cpuset_filetype_t type
= cft
->private;
1645 mutex_lock(&cpuset_mutex
);
1646 if (!is_cpuset_online(cs
)) {
1652 case FILE_CPU_EXCLUSIVE
:
1653 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, val
);
1655 case FILE_MEM_EXCLUSIVE
:
1656 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, val
);
1658 case FILE_MEM_HARDWALL
:
1659 retval
= update_flag(CS_MEM_HARDWALL
, cs
, val
);
1661 case FILE_SCHED_LOAD_BALANCE
:
1662 retval
= update_flag(CS_SCHED_LOAD_BALANCE
, cs
, val
);
1664 case FILE_MEMORY_MIGRATE
:
1665 retval
= update_flag(CS_MEMORY_MIGRATE
, cs
, val
);
1667 case FILE_MEMORY_PRESSURE_ENABLED
:
1668 cpuset_memory_pressure_enabled
= !!val
;
1670 case FILE_SPREAD_PAGE
:
1671 retval
= update_flag(CS_SPREAD_PAGE
, cs
, val
);
1673 case FILE_SPREAD_SLAB
:
1674 retval
= update_flag(CS_SPREAD_SLAB
, cs
, val
);
1676 case FILE_FAMILY_BOOST
:
1677 retval
= update_flag(CS_FAMILY_BOOST
, cs
, val
);
1684 mutex_unlock(&cpuset_mutex
);
1688 static int cpuset_write_s64(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
1691 struct cpuset
*cs
= css_cs(css
);
1692 cpuset_filetype_t type
= cft
->private;
1693 int retval
= -ENODEV
;
1695 mutex_lock(&cpuset_mutex
);
1696 if (!is_cpuset_online(cs
))
1700 case FILE_SCHED_RELAX_DOMAIN_LEVEL
:
1701 retval
= update_relax_domain_level(cs
, val
);
1708 mutex_unlock(&cpuset_mutex
);
1713 * Common handling for a write to a "cpus" or "mems" file.
1715 static ssize_t
cpuset_write_resmask(struct kernfs_open_file
*of
,
1716 char *buf
, size_t nbytes
, loff_t off
)
1718 struct cpuset
*cs
= css_cs(of_css(of
));
1719 struct cpuset
*trialcs
;
1720 int retval
= -ENODEV
;
1722 buf
= strstrip(buf
);
1725 * CPU or memory hotunplug may leave @cs w/o any execution
1726 * resources, in which case the hotplug code asynchronously updates
1727 * configuration and transfers all tasks to the nearest ancestor
1728 * which can execute.
1730 * As writes to "cpus" or "mems" may restore @cs's execution
1731 * resources, wait for the previously scheduled operations before
1732 * proceeding, so that we don't end up keep removing tasks added
1733 * after execution capability is restored.
1735 * cpuset_hotplug_work calls back into cgroup core via
1736 * cgroup_transfer_tasks() and waiting for it from a cgroupfs
1737 * operation like this one can lead to a deadlock through kernfs
1738 * active_ref protection. Let's break the protection. Losing the
1739 * protection is okay as we check whether @cs is online after
1740 * grabbing cpuset_mutex anyway. This only happens on the legacy
1744 kernfs_break_active_protection(of
->kn
);
1745 flush_work(&cpuset_hotplug_work
);
1747 mutex_lock(&cpuset_mutex
);
1748 if (!is_cpuset_online(cs
))
1751 trialcs
= alloc_trial_cpuset(cs
);
1757 switch (of_cft(of
)->private) {
1759 retval
= update_cpumask(cs
, trialcs
, buf
);
1762 retval
= update_nodemask(cs
, trialcs
, buf
);
1769 free_trial_cpuset(trialcs
);
1771 mutex_unlock(&cpuset_mutex
);
1772 kernfs_unbreak_active_protection(of
->kn
);
1774 flush_workqueue(cpuset_migrate_mm_wq
);
1775 return retval
?: nbytes
;
1779 * These ascii lists should be read in a single call, by using a user
1780 * buffer large enough to hold the entire map. If read in smaller
1781 * chunks, there is no guarantee of atomicity. Since the display format
1782 * used, list of ranges of sequential numbers, is variable length,
1783 * and since these maps can change value dynamically, one could read
1784 * gibberish by doing partial reads while a list was changing.
1786 static int cpuset_common_seq_show(struct seq_file
*sf
, void *v
)
1788 struct cpuset
*cs
= css_cs(seq_css(sf
));
1789 cpuset_filetype_t type
= seq_cft(sf
)->private;
1792 spin_lock_irq(&callback_lock
);
1796 seq_printf(sf
, "%*pbl\n", cpumask_pr_args(cs
->cpus_requested
));
1799 seq_printf(sf
, "%*pbl\n", nodemask_pr_args(&cs
->mems_allowed
));
1801 case FILE_EFFECTIVE_CPULIST
:
1802 seq_printf(sf
, "%*pbl\n", cpumask_pr_args(cs
->effective_cpus
));
1804 case FILE_EFFECTIVE_MEMLIST
:
1805 seq_printf(sf
, "%*pbl\n", nodemask_pr_args(&cs
->effective_mems
));
1811 spin_unlock_irq(&callback_lock
);
1815 static u64
cpuset_read_u64(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
1817 struct cpuset
*cs
= css_cs(css
);
1818 cpuset_filetype_t type
= cft
->private;
1820 case FILE_CPU_EXCLUSIVE
:
1821 return is_cpu_exclusive(cs
);
1822 case FILE_MEM_EXCLUSIVE
:
1823 return is_mem_exclusive(cs
);
1824 case FILE_MEM_HARDWALL
:
1825 return is_mem_hardwall(cs
);
1826 case FILE_SCHED_LOAD_BALANCE
:
1827 return is_sched_load_balance(cs
);
1828 case FILE_MEMORY_MIGRATE
:
1829 return is_memory_migrate(cs
);
1830 case FILE_MEMORY_PRESSURE_ENABLED
:
1831 return cpuset_memory_pressure_enabled
;
1832 case FILE_MEMORY_PRESSURE
:
1833 return fmeter_getrate(&cs
->fmeter
);
1834 case FILE_SPREAD_PAGE
:
1835 return is_spread_page(cs
);
1836 case FILE_SPREAD_SLAB
:
1837 return is_spread_slab(cs
);
1838 case FILE_FAMILY_BOOST
:
1839 return is_family_boost_enabled(cs
);
1844 /* Unreachable but makes gcc happy */
1848 static s64
cpuset_read_s64(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
1850 struct cpuset
*cs
= css_cs(css
);
1851 cpuset_filetype_t type
= cft
->private;
1853 case FILE_SCHED_RELAX_DOMAIN_LEVEL
:
1854 return cs
->relax_domain_level
;
1859 /* Unrechable but makes gcc happy */
1865 * for the common functions, 'private' gives the type of file
1868 static struct cftype files
[] = {
1871 .seq_show
= cpuset_common_seq_show
,
1872 .write
= cpuset_write_resmask
,
1873 .max_write_len
= (100U + 6 * NR_CPUS
),
1874 .private = FILE_CPULIST
,
1879 .seq_show
= cpuset_common_seq_show
,
1880 .write
= cpuset_write_resmask
,
1881 .max_write_len
= (100U + 6 * MAX_NUMNODES
),
1882 .private = FILE_MEMLIST
,
1886 .name
= "effective_cpus",
1887 .seq_show
= cpuset_common_seq_show
,
1888 .private = FILE_EFFECTIVE_CPULIST
,
1892 .name
= "effective_mems",
1893 .seq_show
= cpuset_common_seq_show
,
1894 .private = FILE_EFFECTIVE_MEMLIST
,
1898 .name
= "cpu_exclusive",
1899 .read_u64
= cpuset_read_u64
,
1900 .write_u64
= cpuset_write_u64
,
1901 .private = FILE_CPU_EXCLUSIVE
,
1905 .name
= "mem_exclusive",
1906 .read_u64
= cpuset_read_u64
,
1907 .write_u64
= cpuset_write_u64
,
1908 .private = FILE_MEM_EXCLUSIVE
,
1912 .name
= "mem_hardwall",
1913 .read_u64
= cpuset_read_u64
,
1914 .write_u64
= cpuset_write_u64
,
1915 .private = FILE_MEM_HARDWALL
,
1919 .name
= "sched_load_balance",
1920 .read_u64
= cpuset_read_u64
,
1921 .write_u64
= cpuset_write_u64
,
1922 .private = FILE_SCHED_LOAD_BALANCE
,
1926 .name
= "sched_relax_domain_level",
1927 .read_s64
= cpuset_read_s64
,
1928 .write_s64
= cpuset_write_s64
,
1929 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL
,
1933 .name
= "memory_migrate",
1934 .read_u64
= cpuset_read_u64
,
1935 .write_u64
= cpuset_write_u64
,
1936 .private = FILE_MEMORY_MIGRATE
,
1940 .name
= "memory_pressure",
1941 .read_u64
= cpuset_read_u64
,
1942 .private = FILE_MEMORY_PRESSURE
,
1946 .name
= "memory_spread_page",
1947 .read_u64
= cpuset_read_u64
,
1948 .write_u64
= cpuset_write_u64
,
1949 .private = FILE_SPREAD_PAGE
,
1953 .name
= "memory_spread_slab",
1954 .read_u64
= cpuset_read_u64
,
1955 .write_u64
= cpuset_write_u64
,
1956 .private = FILE_SPREAD_SLAB
,
1960 .name
= "memory_pressure_enabled",
1961 .flags
= CFTYPE_ONLY_ON_ROOT
,
1962 .read_u64
= cpuset_read_u64
,
1963 .write_u64
= cpuset_write_u64
,
1964 .private = FILE_MEMORY_PRESSURE_ENABLED
,
1968 .name
= "family_boost",
1969 .read_u64
= cpuset_read_u64
,
1970 .write_u64
= cpuset_write_u64
,
1971 .private = FILE_FAMILY_BOOST
,
1979 * cpuset_css_alloc - allocate a cpuset css
1980 * cgrp: control group that the new cpuset will be part of
1983 static struct cgroup_subsys_state
*
1984 cpuset_css_alloc(struct cgroup_subsys_state
*parent_css
)
1989 return &top_cpuset
.css
;
1991 cs
= kzalloc(sizeof(*cs
), GFP_KERNEL
);
1993 return ERR_PTR(-ENOMEM
);
1994 if (!alloc_cpumask_var(&cs
->cpus_allowed
, GFP_KERNEL
))
1996 if (!alloc_cpumask_var(&cs
->cpus_requested
, GFP_KERNEL
))
1998 if (!alloc_cpumask_var(&cs
->effective_cpus
, GFP_KERNEL
))
1999 goto free_requested
;
2001 set_bit(CS_SCHED_LOAD_BALANCE
, &cs
->flags
);
2002 cpumask_clear(cs
->cpus_allowed
);
2003 cpumask_clear(cs
->cpus_requested
);
2004 nodes_clear(cs
->mems_allowed
);
2005 cpumask_clear(cs
->effective_cpus
);
2006 nodes_clear(cs
->effective_mems
);
2007 fmeter_init(&cs
->fmeter
);
2008 cs
->relax_domain_level
= -1;
2013 free_cpumask_var(cs
->cpus_requested
);
2015 free_cpumask_var(cs
->cpus_allowed
);
2018 return ERR_PTR(-ENOMEM
);
2021 static int cpuset_css_online(struct cgroup_subsys_state
*css
)
2023 struct cpuset
*cs
= css_cs(css
);
2024 struct cpuset
*parent
= parent_cs(cs
);
2025 struct cpuset
*tmp_cs
;
2026 struct cgroup_subsys_state
*pos_css
;
2031 mutex_lock(&cpuset_mutex
);
2033 set_bit(CS_ONLINE
, &cs
->flags
);
2034 if (is_spread_page(parent
))
2035 set_bit(CS_SPREAD_PAGE
, &cs
->flags
);
2036 if (is_spread_slab(parent
))
2037 set_bit(CS_SPREAD_SLAB
, &cs
->flags
);
2041 spin_lock_irq(&callback_lock
);
2042 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys
)) {
2043 cpumask_copy(cs
->effective_cpus
, parent
->effective_cpus
);
2044 cs
->effective_mems
= parent
->effective_mems
;
2046 spin_unlock_irq(&callback_lock
);
2048 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
))
2052 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
2053 * set. This flag handling is implemented in cgroup core for
2054 * histrical reasons - the flag may be specified during mount.
2056 * Currently, if any sibling cpusets have exclusive cpus or mem, we
2057 * refuse to clone the configuration - thereby refusing the task to
2058 * be entered, and as a result refusing the sys_unshare() or
2059 * clone() which initiated it. If this becomes a problem for some
2060 * users who wish to allow that scenario, then this could be
2061 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2062 * (and likewise for mems) to the new cgroup.
2065 cpuset_for_each_child(tmp_cs
, pos_css
, parent
) {
2066 if (is_mem_exclusive(tmp_cs
) || is_cpu_exclusive(tmp_cs
)) {
2073 spin_lock_irq(&callback_lock
);
2074 cs
->mems_allowed
= parent
->mems_allowed
;
2075 cs
->effective_mems
= parent
->mems_allowed
;
2076 cpumask_copy(cs
->cpus_allowed
, parent
->cpus_allowed
);
2077 cpumask_copy(cs
->cpus_requested
, parent
->cpus_requested
);
2078 cpumask_copy(cs
->effective_cpus
, parent
->cpus_allowed
);
2079 spin_unlock_irq(&callback_lock
);
2081 mutex_unlock(&cpuset_mutex
);
2086 * If the cpuset being removed has its flag 'sched_load_balance'
2087 * enabled, then simulate turning sched_load_balance off, which
2088 * will call rebuild_sched_domains_locked().
2091 static void cpuset_css_offline(struct cgroup_subsys_state
*css
)
2093 struct cpuset
*cs
= css_cs(css
);
2095 mutex_lock(&cpuset_mutex
);
2097 if (is_sched_load_balance(cs
))
2098 update_flag(CS_SCHED_LOAD_BALANCE
, cs
, 0);
2101 clear_bit(CS_ONLINE
, &cs
->flags
);
2103 mutex_unlock(&cpuset_mutex
);
2106 static void cpuset_css_free(struct cgroup_subsys_state
*css
)
2108 struct cpuset
*cs
= css_cs(css
);
2110 free_cpumask_var(cs
->effective_cpus
);
2111 free_cpumask_var(cs
->cpus_allowed
);
2112 free_cpumask_var(cs
->cpus_requested
);
2116 static void cpuset_bind(struct cgroup_subsys_state
*root_css
)
2118 mutex_lock(&cpuset_mutex
);
2119 spin_lock_irq(&callback_lock
);
2121 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys
)) {
2122 cpumask_copy(top_cpuset
.cpus_allowed
, cpu_possible_mask
);
2123 top_cpuset
.mems_allowed
= node_possible_map
;
2125 cpumask_copy(top_cpuset
.cpus_allowed
,
2126 top_cpuset
.effective_cpus
);
2127 top_cpuset
.mems_allowed
= top_cpuset
.effective_mems
;
2130 spin_unlock_irq(&callback_lock
);
2131 mutex_unlock(&cpuset_mutex
);
2135 * Make sure the new task conform to the current state of its parent,
2136 * which could have been changed by cpuset just after it inherits the
2137 * state from the parent and before it sits on the cgroup's task list.
2139 void cpuset_fork(struct task_struct
*task
, void *priv
)
2141 if (task_css_is_root(task
, cpuset_cgrp_id
))
2144 set_cpus_allowed_ptr(task
, ¤t
->cpus_allowed
);
2145 task
->mems_allowed
= current
->mems_allowed
;
2148 static int cpuset_allow_attach(struct cgroup_taskset
*tset
)
2150 const struct cred
*cred
= current_cred(), *tcred
;
2151 struct task_struct
*task
;
2152 struct cgroup_subsys_state
*css
;
2154 cgroup_taskset_for_each(task
, css
, tset
) {
2155 tcred
= __task_cred(task
);
2157 if ((current
!= task
) && !capable(CAP_SYS_ADMIN
) &&
2158 cred
->euid
.val
!= tcred
->uid
.val
&& cred
->euid
.val
!= tcred
->suid
.val
)
2165 struct cgroup_subsys cpuset_cgrp_subsys
= {
2166 .css_alloc
= cpuset_css_alloc
,
2167 .css_online
= cpuset_css_online
,
2168 .css_offline
= cpuset_css_offline
,
2169 .css_free
= cpuset_css_free
,
2170 .can_attach
= cpuset_can_attach
,
2171 .allow_attach
= cpuset_allow_attach
,
2172 .cancel_attach
= cpuset_cancel_attach
,
2173 .attach
= cpuset_attach
,
2174 .post_attach
= cpuset_post_attach
,
2175 .bind
= cpuset_bind
,
2176 .fork
= cpuset_fork
,
2177 .legacy_cftypes
= files
,
2182 * cpuset_init - initialize cpusets at system boot
2184 * Description: Initialize top_cpuset and the cpuset internal file system,
2187 int __init
cpuset_init(void)
2191 if (!alloc_cpumask_var(&top_cpuset
.cpus_allowed
, GFP_KERNEL
))
2193 if (!alloc_cpumask_var(&top_cpuset
.effective_cpus
, GFP_KERNEL
))
2195 if (!alloc_cpumask_var(&top_cpuset
.cpus_requested
, GFP_KERNEL
))
2198 cpumask_setall(top_cpuset
.cpus_allowed
);
2199 cpumask_setall(top_cpuset
.cpus_requested
);
2200 nodes_setall(top_cpuset
.mems_allowed
);
2201 cpumask_setall(top_cpuset
.effective_cpus
);
2202 nodes_setall(top_cpuset
.effective_mems
);
2204 fmeter_init(&top_cpuset
.fmeter
);
2205 set_bit(CS_SCHED_LOAD_BALANCE
, &top_cpuset
.flags
);
2206 top_cpuset
.relax_domain_level
= -1;
2208 err
= register_filesystem(&cpuset_fs_type
);
2212 if (!alloc_cpumask_var(&cpus_attach
, GFP_KERNEL
))
2219 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
2220 * or memory nodes, we need to walk over the cpuset hierarchy,
2221 * removing that CPU or node from all cpusets. If this removes the
2222 * last CPU or node from a cpuset, then move the tasks in the empty
2223 * cpuset to its next-highest non-empty parent.
2225 static void remove_tasks_in_empty_cpuset(struct cpuset
*cs
)
2227 struct cpuset
*parent
;
2230 * Find its next-highest non-empty parent, (top cpuset
2231 * has online cpus, so can't be empty).
2233 parent
= parent_cs(cs
);
2234 while (cpumask_empty(parent
->cpus_allowed
) ||
2235 nodes_empty(parent
->mems_allowed
))
2236 parent
= parent_cs(parent
);
2238 if (cgroup_transfer_tasks(parent
->css
.cgroup
, cs
->css
.cgroup
)) {
2239 pr_err("cpuset: failed to transfer tasks out of empty cpuset ");
2240 pr_cont_cgroup_name(cs
->css
.cgroup
);
2246 hotplug_update_tasks_legacy(struct cpuset
*cs
,
2247 struct cpumask
*new_cpus
, nodemask_t
*new_mems
,
2248 bool cpus_updated
, bool mems_updated
)
2252 spin_lock_irq(&callback_lock
);
2253 cpumask_copy(cs
->cpus_allowed
, new_cpus
);
2254 cpumask_copy(cs
->effective_cpus
, new_cpus
);
2255 cs
->mems_allowed
= *new_mems
;
2256 cs
->effective_mems
= *new_mems
;
2257 spin_unlock_irq(&callback_lock
);
2260 * Don't call update_tasks_cpumask() if the cpuset becomes empty,
2261 * as the tasks will be migratecd to an ancestor.
2263 if (cpus_updated
&& !cpumask_empty(cs
->cpus_allowed
))
2264 update_tasks_cpumask(cs
);
2265 if (mems_updated
&& !nodes_empty(cs
->mems_allowed
))
2266 update_tasks_nodemask(cs
);
2268 is_empty
= cpumask_empty(cs
->cpus_allowed
) ||
2269 nodes_empty(cs
->mems_allowed
);
2271 mutex_unlock(&cpuset_mutex
);
2274 * Move tasks to the nearest ancestor with execution resources,
2275 * This is full cgroup operation which will also call back into
2276 * cpuset. Should be done outside any lock.
2279 remove_tasks_in_empty_cpuset(cs
);
2281 mutex_lock(&cpuset_mutex
);
2285 hotplug_update_tasks(struct cpuset
*cs
,
2286 struct cpumask
*new_cpus
, nodemask_t
*new_mems
,
2287 bool cpus_updated
, bool mems_updated
)
2289 if (cpumask_empty(new_cpus
))
2290 cpumask_copy(new_cpus
, parent_cs(cs
)->effective_cpus
);
2291 if (nodes_empty(*new_mems
))
2292 *new_mems
= parent_cs(cs
)->effective_mems
;
2294 spin_lock_irq(&callback_lock
);
2295 cpumask_copy(cs
->effective_cpus
, new_cpus
);
2296 cs
->effective_mems
= *new_mems
;
2297 spin_unlock_irq(&callback_lock
);
2300 update_tasks_cpumask(cs
);
2302 update_tasks_nodemask(cs
);
2306 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
2307 * @cs: cpuset in interest
2309 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2310 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2311 * all its tasks are moved to the nearest ancestor with both resources.
2313 static void cpuset_hotplug_update_tasks(struct cpuset
*cs
)
2315 static cpumask_t new_cpus
;
2316 static nodemask_t new_mems
;
2320 wait_event(cpuset_attach_wq
, cs
->attach_in_progress
== 0);
2322 mutex_lock(&cpuset_mutex
);
2325 * We have raced with task attaching. We wait until attaching
2326 * is finished, so we won't attach a task to an empty cpuset.
2328 if (cs
->attach_in_progress
) {
2329 mutex_unlock(&cpuset_mutex
);
2333 cpumask_and(&new_cpus
, cs
->cpus_requested
, parent_cs(cs
)->effective_cpus
);
2334 nodes_and(new_mems
, cs
->mems_allowed
, parent_cs(cs
)->effective_mems
);
2336 cpus_updated
= !cpumask_equal(&new_cpus
, cs
->effective_cpus
);
2337 mems_updated
= !nodes_equal(new_mems
, cs
->effective_mems
);
2339 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys
))
2340 hotplug_update_tasks(cs
, &new_cpus
, &new_mems
,
2341 cpus_updated
, mems_updated
);
2343 hotplug_update_tasks_legacy(cs
, &new_cpus
, &new_mems
,
2344 cpus_updated
, mems_updated
);
2346 mutex_unlock(&cpuset_mutex
);
2349 static bool force_rebuild
;
2351 void cpuset_force_rebuild(void)
2353 force_rebuild
= true;
2357 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
2359 * This function is called after either CPU or memory configuration has
2360 * changed and updates cpuset accordingly. The top_cpuset is always
2361 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2362 * order to make cpusets transparent (of no affect) on systems that are
2363 * actively using CPU hotplug but making no active use of cpusets.
2365 * Non-root cpusets are only affected by offlining. If any CPUs or memory
2366 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
2369 * Note that CPU offlining during suspend is ignored. We don't modify
2370 * cpusets across suspend/resume cycles at all.
2372 static void cpuset_hotplug_workfn(struct work_struct
*work
)
2374 static cpumask_t new_cpus
;
2375 static nodemask_t new_mems
;
2376 bool cpus_updated
, mems_updated
;
2377 bool on_dfl
= cgroup_subsys_on_dfl(cpuset_cgrp_subsys
);
2379 mutex_lock(&cpuset_mutex
);
2381 /* fetch the available cpus/mems and find out which changed how */
2382 cpumask_copy(&new_cpus
, cpu_active_mask
);
2383 new_mems
= node_states
[N_MEMORY
];
2385 cpus_updated
= !cpumask_equal(top_cpuset
.effective_cpus
, &new_cpus
);
2386 mems_updated
= !nodes_equal(top_cpuset
.effective_mems
, new_mems
);
2388 /* synchronize cpus_allowed to cpu_active_mask */
2390 spin_lock_irq(&callback_lock
);
2392 cpumask_copy(top_cpuset
.cpus_allowed
, &new_cpus
);
2393 cpumask_copy(top_cpuset
.effective_cpus
, &new_cpus
);
2394 spin_unlock_irq(&callback_lock
);
2395 /* we don't mess with cpumasks of tasks in top_cpuset */
2398 /* synchronize mems_allowed to N_MEMORY */
2400 spin_lock_irq(&callback_lock
);
2402 top_cpuset
.mems_allowed
= new_mems
;
2403 top_cpuset
.effective_mems
= new_mems
;
2404 spin_unlock_irq(&callback_lock
);
2405 update_tasks_nodemask(&top_cpuset
);
2408 mutex_unlock(&cpuset_mutex
);
2410 /* if cpus or mems changed, we need to propagate to descendants */
2411 if (cpus_updated
|| mems_updated
) {
2413 struct cgroup_subsys_state
*pos_css
;
2416 cpuset_for_each_descendant_pre(cs
, pos_css
, &top_cpuset
) {
2417 if (cs
== &top_cpuset
|| !css_tryget_online(&cs
->css
))
2421 cpuset_hotplug_update_tasks(cs
);
2429 /* rebuild sched domains if cpus_allowed has changed */
2430 if (cpus_updated
|| force_rebuild
) {
2431 force_rebuild
= false;
2432 rebuild_sched_domains();
2436 void cpuset_update_active_cpus(bool cpu_online
)
2439 * We're inside cpu hotplug critical region which usually nests
2440 * inside cgroup synchronization. Bounce actual hotplug processing
2441 * to a work item to avoid reverse locking order.
2443 * We still need to do partition_sched_domains() synchronously;
2444 * otherwise, the scheduler will get confused and put tasks to the
2445 * dead CPU. Fall back to the default single domain.
2446 * cpuset_hotplug_workfn() will rebuild it as necessary.
2448 partition_sched_domains(1, NULL
, NULL
);
2449 schedule_work(&cpuset_hotplug_work
);
2452 void cpuset_wait_for_hotplug(void)
2454 flush_work(&cpuset_hotplug_work
);
2458 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2459 * Call this routine anytime after node_states[N_MEMORY] changes.
2460 * See cpuset_update_active_cpus() for CPU hotplug handling.
2462 static int cpuset_track_online_nodes(struct notifier_block
*self
,
2463 unsigned long action
, void *arg
)
2465 schedule_work(&cpuset_hotplug_work
);
2469 static struct notifier_block cpuset_track_online_nodes_nb
= {
2470 .notifier_call
= cpuset_track_online_nodes
,
2471 .priority
= 10, /* ??! */
2475 * cpuset_init_smp - initialize cpus_allowed
2477 * Description: Finish top cpuset after cpu, node maps are initialized
2479 void __init
cpuset_init_smp(void)
2481 cpumask_copy(top_cpuset
.cpus_allowed
, cpu_active_mask
);
2482 top_cpuset
.mems_allowed
= node_states
[N_MEMORY
];
2483 top_cpuset
.old_mems_allowed
= top_cpuset
.mems_allowed
;
2485 cpumask_copy(top_cpuset
.effective_cpus
, cpu_active_mask
);
2486 top_cpuset
.effective_mems
= node_states
[N_MEMORY
];
2488 register_hotmemory_notifier(&cpuset_track_online_nodes_nb
);
2490 cpuset_migrate_mm_wq
= alloc_ordered_workqueue("cpuset_migrate_mm", 0);
2491 BUG_ON(!cpuset_migrate_mm_wq
);
2495 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2496 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2497 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
2499 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
2500 * attached to the specified @tsk. Guaranteed to return some non-empty
2501 * subset of cpu_online_mask, even if this means going outside the
2505 void cpuset_cpus_allowed(struct task_struct
*tsk
, struct cpumask
*pmask
)
2507 unsigned long flags
;
2509 spin_lock_irqsave(&callback_lock
, flags
);
2511 guarantee_online_cpus(task_cs(tsk
), pmask
);
2513 spin_unlock_irqrestore(&callback_lock
, flags
);
2516 void cpuset_cpus_allowed_fallback(struct task_struct
*tsk
)
2519 do_set_cpus_allowed(tsk
, task_cs(tsk
)->effective_cpus
);
2523 * We own tsk->cpus_allowed, nobody can change it under us.
2525 * But we used cs && cs->cpus_allowed lockless and thus can
2526 * race with cgroup_attach_task() or update_cpumask() and get
2527 * the wrong tsk->cpus_allowed. However, both cases imply the
2528 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2529 * which takes task_rq_lock().
2531 * If we are called after it dropped the lock we must see all
2532 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2533 * set any mask even if it is not right from task_cs() pov,
2534 * the pending set_cpus_allowed_ptr() will fix things.
2536 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2541 void __init
cpuset_init_current_mems_allowed(void)
2543 nodes_setall(current
->mems_allowed
);
2547 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2548 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2550 * Description: Returns the nodemask_t mems_allowed of the cpuset
2551 * attached to the specified @tsk. Guaranteed to return some non-empty
2552 * subset of node_states[N_MEMORY], even if this means going outside the
2556 nodemask_t
cpuset_mems_allowed(struct task_struct
*tsk
)
2559 unsigned long flags
;
2561 spin_lock_irqsave(&callback_lock
, flags
);
2563 guarantee_online_mems(task_cs(tsk
), &mask
);
2565 spin_unlock_irqrestore(&callback_lock
, flags
);
2571 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2572 * @nodemask: the nodemask to be checked
2574 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
2576 int cpuset_nodemask_valid_mems_allowed(nodemask_t
*nodemask
)
2578 return nodes_intersects(*nodemask
, current
->mems_allowed
);
2582 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2583 * mem_hardwall ancestor to the specified cpuset. Call holding
2584 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
2585 * (an unusual configuration), then returns the root cpuset.
2587 static struct cpuset
*nearest_hardwall_ancestor(struct cpuset
*cs
)
2589 while (!(is_mem_exclusive(cs
) || is_mem_hardwall(cs
)) && parent_cs(cs
))
2595 * cpuset_node_allowed - Can we allocate on a memory node?
2596 * @node: is this an allowed node?
2597 * @gfp_mask: memory allocation flags
2599 * If we're in interrupt, yes, we can always allocate. If @node is set in
2600 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
2601 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
2602 * yes. If current has access to memory reserves due to TIF_MEMDIE, yes.
2605 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2606 * and do not allow allocations outside the current tasks cpuset
2607 * unless the task has been OOM killed as is marked TIF_MEMDIE.
2608 * GFP_KERNEL allocations are not so marked, so can escape to the
2609 * nearest enclosing hardwalled ancestor cpuset.
2611 * Scanning up parent cpusets requires callback_lock. The
2612 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2613 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2614 * current tasks mems_allowed came up empty on the first pass over
2615 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2616 * cpuset are short of memory, might require taking the callback_lock.
2618 * The first call here from mm/page_alloc:get_page_from_freelist()
2619 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2620 * so no allocation on a node outside the cpuset is allowed (unless
2621 * in interrupt, of course).
2623 * The second pass through get_page_from_freelist() doesn't even call
2624 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2625 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2626 * in alloc_flags. That logic and the checks below have the combined
2628 * in_interrupt - any node ok (current task context irrelevant)
2629 * GFP_ATOMIC - any node ok
2630 * TIF_MEMDIE - any node ok
2631 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
2632 * GFP_USER - only nodes in current tasks mems allowed ok.
2634 int __cpuset_node_allowed(int node
, gfp_t gfp_mask
)
2636 struct cpuset
*cs
; /* current cpuset ancestors */
2637 int allowed
; /* is allocation in zone z allowed? */
2638 unsigned long flags
;
2642 if (node_isset(node
, current
->mems_allowed
))
2645 * Allow tasks that have access to memory reserves because they have
2646 * been OOM killed to get memory anywhere.
2648 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
2650 if (gfp_mask
& __GFP_HARDWALL
) /* If hardwall request, stop here */
2653 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
2656 /* Not hardwall and node outside mems_allowed: scan up cpusets */
2657 spin_lock_irqsave(&callback_lock
, flags
);
2660 cs
= nearest_hardwall_ancestor(task_cs(current
));
2661 allowed
= node_isset(node
, cs
->mems_allowed
);
2664 spin_unlock_irqrestore(&callback_lock
, flags
);
2669 * cpuset_mem_spread_node() - On which node to begin search for a file page
2670 * cpuset_slab_spread_node() - On which node to begin search for a slab page
2672 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2673 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2674 * and if the memory allocation used cpuset_mem_spread_node()
2675 * to determine on which node to start looking, as it will for
2676 * certain page cache or slab cache pages such as used for file
2677 * system buffers and inode caches, then instead of starting on the
2678 * local node to look for a free page, rather spread the starting
2679 * node around the tasks mems_allowed nodes.
2681 * We don't have to worry about the returned node being offline
2682 * because "it can't happen", and even if it did, it would be ok.
2684 * The routines calling guarantee_online_mems() are careful to
2685 * only set nodes in task->mems_allowed that are online. So it
2686 * should not be possible for the following code to return an
2687 * offline node. But if it did, that would be ok, as this routine
2688 * is not returning the node where the allocation must be, only
2689 * the node where the search should start. The zonelist passed to
2690 * __alloc_pages() will include all nodes. If the slab allocator
2691 * is passed an offline node, it will fall back to the local node.
2692 * See kmem_cache_alloc_node().
2695 static int cpuset_spread_node(int *rotor
)
2699 node
= next_node(*rotor
, current
->mems_allowed
);
2700 if (node
== MAX_NUMNODES
)
2701 node
= first_node(current
->mems_allowed
);
2706 int cpuset_mem_spread_node(void)
2708 if (current
->cpuset_mem_spread_rotor
== NUMA_NO_NODE
)
2709 current
->cpuset_mem_spread_rotor
=
2710 node_random(¤t
->mems_allowed
);
2712 return cpuset_spread_node(¤t
->cpuset_mem_spread_rotor
);
2715 int cpuset_slab_spread_node(void)
2717 if (current
->cpuset_slab_spread_rotor
== NUMA_NO_NODE
)
2718 current
->cpuset_slab_spread_rotor
=
2719 node_random(¤t
->mems_allowed
);
2721 return cpuset_spread_node(¤t
->cpuset_slab_spread_rotor
);
2724 EXPORT_SYMBOL_GPL(cpuset_mem_spread_node
);
2727 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2728 * @tsk1: pointer to task_struct of some task.
2729 * @tsk2: pointer to task_struct of some other task.
2731 * Description: Return true if @tsk1's mems_allowed intersects the
2732 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2733 * one of the task's memory usage might impact the memory available
2737 int cpuset_mems_allowed_intersects(const struct task_struct
*tsk1
,
2738 const struct task_struct
*tsk2
)
2740 return nodes_intersects(tsk1
->mems_allowed
, tsk2
->mems_allowed
);
2744 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
2746 * Description: Prints current's name, cpuset name, and cached copy of its
2747 * mems_allowed to the kernel log.
2749 void cpuset_print_current_mems_allowed(void)
2751 struct cgroup
*cgrp
;
2755 cgrp
= task_cs(current
)->css
.cgroup
;
2756 pr_info("%s cpuset=", current
->comm
);
2757 pr_cont_cgroup_name(cgrp
);
2758 pr_cont(" mems_allowed=%*pbl\n",
2759 nodemask_pr_args(¤t
->mems_allowed
));
2765 * Collection of memory_pressure is suppressed unless
2766 * this flag is enabled by writing "1" to the special
2767 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2770 int cpuset_memory_pressure_enabled __read_mostly
;
2773 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2775 * Keep a running average of the rate of synchronous (direct)
2776 * page reclaim efforts initiated by tasks in each cpuset.
2778 * This represents the rate at which some task in the cpuset
2779 * ran low on memory on all nodes it was allowed to use, and
2780 * had to enter the kernels page reclaim code in an effort to
2781 * create more free memory by tossing clean pages or swapping
2782 * or writing dirty pages.
2784 * Display to user space in the per-cpuset read-only file
2785 * "memory_pressure". Value displayed is an integer
2786 * representing the recent rate of entry into the synchronous
2787 * (direct) page reclaim by any task attached to the cpuset.
2790 void __cpuset_memory_pressure_bump(void)
2793 fmeter_markevent(&task_cs(current
)->fmeter
);
2797 #ifdef CONFIG_PROC_PID_CPUSET
2799 * proc_cpuset_show()
2800 * - Print tasks cpuset path into seq_file.
2801 * - Used for /proc/<pid>/cpuset.
2802 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2803 * doesn't really matter if tsk->cpuset changes after we read it,
2804 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
2807 int proc_cpuset_show(struct seq_file
*m
, struct pid_namespace
*ns
,
2808 struct pid
*pid
, struct task_struct
*tsk
)
2811 struct cgroup_subsys_state
*css
;
2815 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
2819 retval
= -ENAMETOOLONG
;
2821 css
= task_css(tsk
, cpuset_cgrp_id
);
2822 p
= cgroup_path(css
->cgroup
, buf
, PATH_MAX
);
2834 #endif /* CONFIG_PROC_PID_CPUSET */
2836 /* Display task mems_allowed in /proc/<pid>/status file. */
2837 void cpuset_task_status_allowed(struct seq_file
*m
, struct task_struct
*task
)
2839 seq_printf(m
, "Mems_allowed:\t%*pb\n",
2840 nodemask_pr_args(&task
->mems_allowed
));
2841 seq_printf(m
, "Mems_allowed_list:\t%*pbl\n",
2842 nodemask_pr_args(&task
->mems_allowed
));