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/workqueue.h>
61 #include <linux/cgroup.h>
62 #include <linux/wait.h>
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 */
79 cpumask_var_t cpus_allowed
; /* CPUs allowed to tasks in cpuset */
80 nodemask_t mems_allowed
; /* Memory Nodes allowed to tasks */
83 * This is old Memory Nodes tasks took on.
85 * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
86 * - A new cpuset's old_mems_allowed is initialized when some
87 * task is moved into it.
88 * - old_mems_allowed is used in cpuset_migrate_mm() when we change
89 * cpuset.mems_allowed and have tasks' nodemask updated, and
90 * then old_mems_allowed is updated to mems_allowed.
92 nodemask_t old_mems_allowed
;
94 struct fmeter fmeter
; /* memory_pressure filter */
97 * Tasks are being attached to this cpuset. Used to prevent
98 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
100 int attach_in_progress
;
102 /* partition number for rebuild_sched_domains() */
105 /* for custom sched domain */
106 int relax_domain_level
;
109 static inline struct cpuset
*css_cs(struct cgroup_subsys_state
*css
)
111 return css
? container_of(css
, struct cpuset
, css
) : NULL
;
114 /* Retrieve the cpuset for a task */
115 static inline struct cpuset
*task_cs(struct task_struct
*task
)
117 return css_cs(task_css(task
, cpuset_subsys_id
));
120 static inline struct cpuset
*parent_cs(struct cpuset
*cs
)
122 return css_cs(css_parent(&cs
->css
));
126 static inline bool task_has_mempolicy(struct task_struct
*task
)
128 return task
->mempolicy
;
131 static inline bool task_has_mempolicy(struct task_struct
*task
)
138 /* bits in struct cpuset flags field */
145 CS_SCHED_LOAD_BALANCE
,
150 /* convenient tests for these bits */
151 static inline bool is_cpuset_online(const struct cpuset
*cs
)
153 return test_bit(CS_ONLINE
, &cs
->flags
);
156 static inline int is_cpu_exclusive(const struct cpuset
*cs
)
158 return test_bit(CS_CPU_EXCLUSIVE
, &cs
->flags
);
161 static inline int is_mem_exclusive(const struct cpuset
*cs
)
163 return test_bit(CS_MEM_EXCLUSIVE
, &cs
->flags
);
166 static inline int is_mem_hardwall(const struct cpuset
*cs
)
168 return test_bit(CS_MEM_HARDWALL
, &cs
->flags
);
171 static inline int is_sched_load_balance(const struct cpuset
*cs
)
173 return test_bit(CS_SCHED_LOAD_BALANCE
, &cs
->flags
);
176 static inline int is_memory_migrate(const struct cpuset
*cs
)
178 return test_bit(CS_MEMORY_MIGRATE
, &cs
->flags
);
181 static inline int is_spread_page(const struct cpuset
*cs
)
183 return test_bit(CS_SPREAD_PAGE
, &cs
->flags
);
186 static inline int is_spread_slab(const struct cpuset
*cs
)
188 return test_bit(CS_SPREAD_SLAB
, &cs
->flags
);
191 static struct cpuset top_cpuset
= {
192 .flags
= ((1 << CS_ONLINE
) | (1 << CS_CPU_EXCLUSIVE
) |
193 (1 << CS_MEM_EXCLUSIVE
)),
197 * cpuset_for_each_child - traverse online children of a cpuset
198 * @child_cs: loop cursor pointing to the current child
199 * @pos_css: used for iteration
200 * @parent_cs: target cpuset to walk children of
202 * Walk @child_cs through the online children of @parent_cs. Must be used
203 * with RCU read locked.
205 #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \
206 css_for_each_child((pos_css), &(parent_cs)->css) \
207 if (is_cpuset_online(((child_cs) = css_cs((pos_css)))))
210 * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
211 * @des_cs: loop cursor pointing to the current descendant
212 * @pos_css: used for iteration
213 * @root_cs: target cpuset to walk ancestor of
215 * Walk @des_cs through the online descendants of @root_cs. Must be used
216 * with RCU read locked. The caller may modify @pos_css by calling
217 * css_rightmost_descendant() to skip subtree. @root_cs is included in the
218 * iteration and the first node to be visited.
220 #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \
221 css_for_each_descendant_pre((pos_css), &(root_cs)->css) \
222 if (is_cpuset_online(((des_cs) = css_cs((pos_css)))))
225 * There are two global mutexes guarding cpuset structures - cpuset_mutex
226 * and callback_mutex. The latter may nest inside the former. We also
227 * require taking task_lock() when dereferencing a task's cpuset pointer.
228 * See "The task_lock() exception", at the end of this comment.
230 * A task must hold both mutexes to modify cpusets. If a task holds
231 * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it
232 * is the only task able to also acquire callback_mutex and be able to
233 * modify cpusets. It can perform various checks on the cpuset structure
234 * first, knowing nothing will change. It can also allocate memory while
235 * just holding cpuset_mutex. While it is performing these checks, various
236 * callback routines can briefly acquire callback_mutex to query cpusets.
237 * Once it is ready to make the changes, it takes callback_mutex, blocking
240 * Calls to the kernel memory allocator can not be made while holding
241 * callback_mutex, as that would risk double tripping on callback_mutex
242 * from one of the callbacks into the cpuset code from within
245 * If a task is only holding callback_mutex, then it has read-only
248 * Now, the task_struct fields mems_allowed and mempolicy may be changed
249 * by other task, we use alloc_lock in the task_struct fields to protect
252 * The cpuset_common_file_read() handlers only hold callback_mutex across
253 * small pieces of code, such as when reading out possibly multi-word
254 * cpumasks and nodemasks.
256 * Accessing a task's cpuset should be done in accordance with the
257 * guidelines for accessing subsystem state in kernel/cgroup.c
260 static DEFINE_MUTEX(cpuset_mutex
);
261 static DEFINE_MUTEX(callback_mutex
);
264 * CPU / memory hotplug is handled asynchronously.
266 static void cpuset_hotplug_workfn(struct work_struct
*work
);
267 static DECLARE_WORK(cpuset_hotplug_work
, cpuset_hotplug_workfn
);
269 static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq
);
272 * This is ugly, but preserves the userspace API for existing cpuset
273 * users. If someone tries to mount the "cpuset" filesystem, we
274 * silently switch it to mount "cgroup" instead
276 static struct dentry
*cpuset_mount(struct file_system_type
*fs_type
,
277 int flags
, const char *unused_dev_name
, void *data
)
279 struct file_system_type
*cgroup_fs
= get_fs_type("cgroup");
280 struct dentry
*ret
= ERR_PTR(-ENODEV
);
284 "release_agent=/sbin/cpuset_release_agent";
285 ret
= cgroup_fs
->mount(cgroup_fs
, flags
,
286 unused_dev_name
, mountopts
);
287 put_filesystem(cgroup_fs
);
292 static struct file_system_type cpuset_fs_type
= {
294 .mount
= cpuset_mount
,
298 * Return in pmask the portion of a cpusets's cpus_allowed that
299 * are online. If none are online, walk up the cpuset hierarchy
300 * until we find one that does have some online cpus. The top
301 * cpuset always has some cpus online.
303 * One way or another, we guarantee to return some non-empty subset
304 * of cpu_online_mask.
306 * Call with callback_mutex held.
308 static void guarantee_online_cpus(struct cpuset
*cs
, struct cpumask
*pmask
)
310 while (!cpumask_intersects(cs
->cpus_allowed
, cpu_online_mask
))
312 cpumask_and(pmask
, cs
->cpus_allowed
, cpu_online_mask
);
316 * Return in *pmask the portion of a cpusets's mems_allowed that
317 * are online, with memory. If none are online with memory, walk
318 * up the cpuset hierarchy until we find one that does have some
319 * online mems. The top cpuset always has some mems online.
321 * One way or another, we guarantee to return some non-empty subset
322 * of node_states[N_MEMORY].
324 * Call with callback_mutex held.
326 static void guarantee_online_mems(struct cpuset
*cs
, nodemask_t
*pmask
)
328 while (!nodes_intersects(cs
->mems_allowed
, node_states
[N_MEMORY
]))
330 nodes_and(*pmask
, cs
->mems_allowed
, node_states
[N_MEMORY
]);
334 * update task's spread flag if cpuset's page/slab spread flag is set
336 * Called with callback_mutex/cpuset_mutex held
338 static void cpuset_update_task_spread_flag(struct cpuset
*cs
,
339 struct task_struct
*tsk
)
341 if (is_spread_page(cs
))
342 tsk
->flags
|= PF_SPREAD_PAGE
;
344 tsk
->flags
&= ~PF_SPREAD_PAGE
;
345 if (is_spread_slab(cs
))
346 tsk
->flags
|= PF_SPREAD_SLAB
;
348 tsk
->flags
&= ~PF_SPREAD_SLAB
;
352 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
354 * One cpuset is a subset of another if all its allowed CPUs and
355 * Memory Nodes are a subset of the other, and its exclusive flags
356 * are only set if the other's are set. Call holding cpuset_mutex.
359 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
361 return cpumask_subset(p
->cpus_allowed
, q
->cpus_allowed
) &&
362 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
363 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
364 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
368 * alloc_trial_cpuset - allocate a trial cpuset
369 * @cs: the cpuset that the trial cpuset duplicates
371 static struct cpuset
*alloc_trial_cpuset(struct cpuset
*cs
)
373 struct cpuset
*trial
;
375 trial
= kmemdup(cs
, sizeof(*cs
), GFP_KERNEL
);
379 if (!alloc_cpumask_var(&trial
->cpus_allowed
, GFP_KERNEL
)) {
383 cpumask_copy(trial
->cpus_allowed
, cs
->cpus_allowed
);
389 * free_trial_cpuset - free the trial cpuset
390 * @trial: the trial cpuset to be freed
392 static void free_trial_cpuset(struct cpuset
*trial
)
394 free_cpumask_var(trial
->cpus_allowed
);
399 * validate_change() - Used to validate that any proposed cpuset change
400 * follows the structural rules for cpusets.
402 * If we replaced the flag and mask values of the current cpuset
403 * (cur) with those values in the trial cpuset (trial), would
404 * our various subset and exclusive rules still be valid? Presumes
407 * 'cur' is the address of an actual, in-use cpuset. Operations
408 * such as list traversal that depend on the actual address of the
409 * cpuset in the list must use cur below, not trial.
411 * 'trial' is the address of bulk structure copy of cur, with
412 * perhaps one or more of the fields cpus_allowed, mems_allowed,
413 * or flags changed to new, trial values.
415 * Return 0 if valid, -errno if not.
418 static int validate_change(struct cpuset
*cur
, struct cpuset
*trial
)
420 struct cgroup_subsys_state
*css
;
421 struct cpuset
*c
, *par
;
426 /* Each of our child cpusets must be a subset of us */
428 cpuset_for_each_child(c
, css
, cur
)
429 if (!is_cpuset_subset(c
, trial
))
432 /* Remaining checks don't apply to root cpuset */
434 if (cur
== &top_cpuset
)
437 par
= parent_cs(cur
);
439 /* We must be a subset of our parent cpuset */
441 if (!is_cpuset_subset(trial
, par
))
445 * If either I or some sibling (!= me) is exclusive, we can't
449 cpuset_for_each_child(c
, css
, par
) {
450 if ((is_cpu_exclusive(trial
) || is_cpu_exclusive(c
)) &&
452 cpumask_intersects(trial
->cpus_allowed
, c
->cpus_allowed
))
454 if ((is_mem_exclusive(trial
) || is_mem_exclusive(c
)) &&
456 nodes_intersects(trial
->mems_allowed
, c
->mems_allowed
))
461 * Cpusets with tasks - existing or newly being attached - can't
462 * be changed to have empty cpus_allowed or mems_allowed.
465 if ((cgroup_task_count(cur
->css
.cgroup
) || cur
->attach_in_progress
)) {
466 if (!cpumask_empty(cur
->cpus_allowed
) &&
467 cpumask_empty(trial
->cpus_allowed
))
469 if (!nodes_empty(cur
->mems_allowed
) &&
470 nodes_empty(trial
->mems_allowed
))
482 * Helper routine for generate_sched_domains().
483 * Do cpusets a, b have overlapping cpus_allowed masks?
485 static int cpusets_overlap(struct cpuset
*a
, struct cpuset
*b
)
487 return cpumask_intersects(a
->cpus_allowed
, b
->cpus_allowed
);
491 update_domain_attr(struct sched_domain_attr
*dattr
, struct cpuset
*c
)
493 if (dattr
->relax_domain_level
< c
->relax_domain_level
)
494 dattr
->relax_domain_level
= c
->relax_domain_level
;
498 static void update_domain_attr_tree(struct sched_domain_attr
*dattr
,
499 struct cpuset
*root_cs
)
502 struct cgroup_subsys_state
*pos_css
;
505 cpuset_for_each_descendant_pre(cp
, pos_css
, root_cs
) {
506 /* skip the whole subtree if @cp doesn't have any CPU */
507 if (cpumask_empty(cp
->cpus_allowed
)) {
508 pos_css
= css_rightmost_descendant(pos_css
);
512 if (is_sched_load_balance(cp
))
513 update_domain_attr(dattr
, cp
);
519 * generate_sched_domains()
521 * This function builds a partial partition of the systems CPUs
522 * A 'partial partition' is a set of non-overlapping subsets whose
523 * union is a subset of that set.
524 * The output of this function needs to be passed to kernel/sched/core.c
525 * partition_sched_domains() routine, which will rebuild the scheduler's
526 * load balancing domains (sched domains) as specified by that partial
529 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
530 * for a background explanation of this.
532 * Does not return errors, on the theory that the callers of this
533 * routine would rather not worry about failures to rebuild sched
534 * domains when operating in the severe memory shortage situations
535 * that could cause allocation failures below.
537 * Must be called with cpuset_mutex held.
539 * The three key local variables below are:
540 * q - a linked-list queue of cpuset pointers, used to implement a
541 * top-down scan of all cpusets. This scan loads a pointer
542 * to each cpuset marked is_sched_load_balance into the
543 * array 'csa'. For our purposes, rebuilding the schedulers
544 * sched domains, we can ignore !is_sched_load_balance cpusets.
545 * csa - (for CpuSet Array) Array of pointers to all the cpusets
546 * that need to be load balanced, for convenient iterative
547 * access by the subsequent code that finds the best partition,
548 * i.e the set of domains (subsets) of CPUs such that the
549 * cpus_allowed of every cpuset marked is_sched_load_balance
550 * is a subset of one of these domains, while there are as
551 * many such domains as possible, each as small as possible.
552 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
553 * the kernel/sched/core.c routine partition_sched_domains() in a
554 * convenient format, that can be easily compared to the prior
555 * value to determine what partition elements (sched domains)
556 * were changed (added or removed.)
558 * Finding the best partition (set of domains):
559 * The triple nested loops below over i, j, k scan over the
560 * load balanced cpusets (using the array of cpuset pointers in
561 * csa[]) looking for pairs of cpusets that have overlapping
562 * cpus_allowed, but which don't have the same 'pn' partition
563 * number and gives them in the same partition number. It keeps
564 * looping on the 'restart' label until it can no longer find
567 * The union of the cpus_allowed masks from the set of
568 * all cpusets having the same 'pn' value then form the one
569 * element of the partition (one sched domain) to be passed to
570 * partition_sched_domains().
572 static int generate_sched_domains(cpumask_var_t
**domains
,
573 struct sched_domain_attr
**attributes
)
575 struct cpuset
*cp
; /* scans q */
576 struct cpuset
**csa
; /* array of all cpuset ptrs */
577 int csn
; /* how many cpuset ptrs in csa so far */
578 int i
, j
, k
; /* indices for partition finding loops */
579 cpumask_var_t
*doms
; /* resulting partition; i.e. sched domains */
580 struct sched_domain_attr
*dattr
; /* attributes for custom domains */
581 int ndoms
= 0; /* number of sched domains in result */
582 int nslot
; /* next empty doms[] struct cpumask slot */
583 struct cgroup_subsys_state
*pos_css
;
589 /* Special case for the 99% of systems with one, full, sched domain */
590 if (is_sched_load_balance(&top_cpuset
)) {
592 doms
= alloc_sched_domains(ndoms
);
596 dattr
= kmalloc(sizeof(struct sched_domain_attr
), GFP_KERNEL
);
598 *dattr
= SD_ATTR_INIT
;
599 update_domain_attr_tree(dattr
, &top_cpuset
);
601 cpumask_copy(doms
[0], top_cpuset
.cpus_allowed
);
606 csa
= kmalloc(nr_cpusets() * sizeof(cp
), GFP_KERNEL
);
612 cpuset_for_each_descendant_pre(cp
, pos_css
, &top_cpuset
) {
613 if (cp
== &top_cpuset
)
616 * Continue traversing beyond @cp iff @cp has some CPUs and
617 * isn't load balancing. The former is obvious. The
618 * latter: All child cpusets contain a subset of the
619 * parent's cpus, so just skip them, and then we call
620 * update_domain_attr_tree() to calc relax_domain_level of
621 * the corresponding sched domain.
623 if (!cpumask_empty(cp
->cpus_allowed
) &&
624 !is_sched_load_balance(cp
))
627 if (is_sched_load_balance(cp
))
630 /* skip @cp's subtree */
631 pos_css
= css_rightmost_descendant(pos_css
);
635 for (i
= 0; i
< csn
; i
++)
640 /* Find the best partition (set of sched domains) */
641 for (i
= 0; i
< csn
; i
++) {
642 struct cpuset
*a
= csa
[i
];
645 for (j
= 0; j
< csn
; j
++) {
646 struct cpuset
*b
= csa
[j
];
649 if (apn
!= bpn
&& cpusets_overlap(a
, b
)) {
650 for (k
= 0; k
< csn
; k
++) {
651 struct cpuset
*c
= csa
[k
];
656 ndoms
--; /* one less element */
663 * Now we know how many domains to create.
664 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
666 doms
= alloc_sched_domains(ndoms
);
671 * The rest of the code, including the scheduler, can deal with
672 * dattr==NULL case. No need to abort if alloc fails.
674 dattr
= kmalloc(ndoms
* sizeof(struct sched_domain_attr
), GFP_KERNEL
);
676 for (nslot
= 0, i
= 0; i
< csn
; i
++) {
677 struct cpuset
*a
= csa
[i
];
682 /* Skip completed partitions */
688 if (nslot
== ndoms
) {
689 static int warnings
= 10;
692 "rebuild_sched_domains confused:"
693 " nslot %d, ndoms %d, csn %d, i %d,"
695 nslot
, ndoms
, csn
, i
, apn
);
703 *(dattr
+ nslot
) = SD_ATTR_INIT
;
704 for (j
= i
; j
< csn
; j
++) {
705 struct cpuset
*b
= csa
[j
];
708 cpumask_or(dp
, dp
, b
->cpus_allowed
);
710 update_domain_attr_tree(dattr
+ nslot
, b
);
712 /* Done with this partition */
718 BUG_ON(nslot
!= ndoms
);
724 * Fallback to the default domain if kmalloc() failed.
725 * See comments in partition_sched_domains().
736 * Rebuild scheduler domains.
738 * If the flag 'sched_load_balance' of any cpuset with non-empty
739 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
740 * which has that flag enabled, or if any cpuset with a non-empty
741 * 'cpus' is removed, then call this routine to rebuild the
742 * scheduler's dynamic sched domains.
744 * Call with cpuset_mutex held. Takes get_online_cpus().
746 static void rebuild_sched_domains_locked(void)
748 struct sched_domain_attr
*attr
;
752 lockdep_assert_held(&cpuset_mutex
);
756 * We have raced with CPU hotplug. Don't do anything to avoid
757 * passing doms with offlined cpu to partition_sched_domains().
758 * Anyways, hotplug work item will rebuild sched domains.
760 if (!cpumask_equal(top_cpuset
.cpus_allowed
, cpu_active_mask
))
763 /* Generate domain masks and attrs */
764 ndoms
= generate_sched_domains(&doms
, &attr
);
766 /* Have scheduler rebuild the domains */
767 partition_sched_domains(ndoms
, doms
, attr
);
771 #else /* !CONFIG_SMP */
772 static void rebuild_sched_domains_locked(void)
775 #endif /* CONFIG_SMP */
777 void rebuild_sched_domains(void)
779 mutex_lock(&cpuset_mutex
);
780 rebuild_sched_domains_locked();
781 mutex_unlock(&cpuset_mutex
);
785 * effective_cpumask_cpuset - return nearest ancestor with non-empty cpus
786 * @cs: the cpuset in interest
788 * A cpuset's effective cpumask is the cpumask of the nearest ancestor
789 * with non-empty cpus. We use effective cpumask whenever:
790 * - we update tasks' cpus_allowed. (they take on the ancestor's cpumask
791 * if the cpuset they reside in has no cpus)
792 * - we want to retrieve task_cs(tsk)'s cpus_allowed.
794 * Called with cpuset_mutex held. cpuset_cpus_allowed_fallback() is an
795 * exception. See comments there.
797 static struct cpuset
*effective_cpumask_cpuset(struct cpuset
*cs
)
799 while (cpumask_empty(cs
->cpus_allowed
))
805 * effective_nodemask_cpuset - return nearest ancestor with non-empty mems
806 * @cs: the cpuset in interest
808 * A cpuset's effective nodemask is the nodemask of the nearest ancestor
809 * with non-empty memss. We use effective nodemask whenever:
810 * - we update tasks' mems_allowed. (they take on the ancestor's nodemask
811 * if the cpuset they reside in has no mems)
812 * - we want to retrieve task_cs(tsk)'s mems_allowed.
814 * Called with cpuset_mutex held.
816 static struct cpuset
*effective_nodemask_cpuset(struct cpuset
*cs
)
818 while (nodes_empty(cs
->mems_allowed
))
824 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
826 * @data: cpuset to @tsk belongs to
828 * Called by css_scan_tasks() for each task in a cgroup whose cpus_allowed
829 * mask needs to be changed.
831 * We don't need to re-check for the cgroup/cpuset membership, since we're
832 * holding cpuset_mutex at this point.
834 static void cpuset_change_cpumask(struct task_struct
*tsk
, void *data
)
836 struct cpuset
*cs
= data
;
837 struct cpuset
*cpus_cs
= effective_cpumask_cpuset(cs
);
839 set_cpus_allowed_ptr(tsk
, cpus_cs
->cpus_allowed
);
843 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
844 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
845 * @heap: if NULL, defer allocating heap memory to css_scan_tasks()
847 * Called with cpuset_mutex held
849 * The css_scan_tasks() function will scan all the tasks in a cgroup,
850 * calling callback functions for each.
852 * No return value. It's guaranteed that css_scan_tasks() always returns 0
855 static void update_tasks_cpumask(struct cpuset
*cs
, struct ptr_heap
*heap
)
857 css_scan_tasks(&cs
->css
, NULL
, cpuset_change_cpumask
, cs
, heap
);
861 * update_tasks_cpumask_hier - Update the cpumasks of tasks in the hierarchy.
862 * @root_cs: the root cpuset of the hierarchy
863 * @update_root: update root cpuset or not?
864 * @heap: the heap used by css_scan_tasks()
866 * This will update cpumasks of tasks in @root_cs and all other empty cpusets
867 * which take on cpumask of @root_cs.
869 * Called with cpuset_mutex held
871 static void update_tasks_cpumask_hier(struct cpuset
*root_cs
,
872 bool update_root
, struct ptr_heap
*heap
)
875 struct cgroup_subsys_state
*pos_css
;
878 cpuset_for_each_descendant_pre(cp
, pos_css
, root_cs
) {
883 /* skip the whole subtree if @cp have some CPU */
884 if (!cpumask_empty(cp
->cpus_allowed
)) {
885 pos_css
= css_rightmost_descendant(pos_css
);
889 if (!css_tryget(&cp
->css
))
893 update_tasks_cpumask(cp
, heap
);
902 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
903 * @cs: the cpuset to consider
904 * @buf: buffer of cpu numbers written to this cpuset
906 static int update_cpumask(struct cpuset
*cs
, struct cpuset
*trialcs
,
909 struct ptr_heap heap
;
911 int is_load_balanced
;
913 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
914 if (cs
== &top_cpuset
)
918 * An empty cpus_allowed is ok only if the cpuset has no tasks.
919 * Since cpulist_parse() fails on an empty mask, we special case
920 * that parsing. The validate_change() call ensures that cpusets
921 * with tasks have cpus.
924 cpumask_clear(trialcs
->cpus_allowed
);
926 retval
= cpulist_parse(buf
, trialcs
->cpus_allowed
);
930 if (!cpumask_subset(trialcs
->cpus_allowed
, cpu_active_mask
))
934 /* Nothing to do if the cpus didn't change */
935 if (cpumask_equal(cs
->cpus_allowed
, trialcs
->cpus_allowed
))
938 retval
= validate_change(cs
, trialcs
);
942 retval
= heap_init(&heap
, PAGE_SIZE
, GFP_KERNEL
, NULL
);
946 is_load_balanced
= is_sched_load_balance(trialcs
);
948 mutex_lock(&callback_mutex
);
949 cpumask_copy(cs
->cpus_allowed
, trialcs
->cpus_allowed
);
950 mutex_unlock(&callback_mutex
);
952 update_tasks_cpumask_hier(cs
, true, &heap
);
956 if (is_load_balanced
)
957 rebuild_sched_domains_locked();
964 * Migrate memory region from one set of nodes to another.
966 * Temporarilly set tasks mems_allowed to target nodes of migration,
967 * so that the migration code can allocate pages on these nodes.
969 * While the mm_struct we are migrating is typically from some
970 * other task, the task_struct mems_allowed that we are hacking
971 * is for our current task, which must allocate new pages for that
972 * migrating memory region.
975 static void cpuset_migrate_mm(struct mm_struct
*mm
, const nodemask_t
*from
,
976 const nodemask_t
*to
)
978 struct task_struct
*tsk
= current
;
979 struct cpuset
*mems_cs
;
981 tsk
->mems_allowed
= *to
;
983 do_migrate_pages(mm
, from
, to
, MPOL_MF_MOVE_ALL
);
986 mems_cs
= effective_nodemask_cpuset(task_cs(tsk
));
987 guarantee_online_mems(mems_cs
, &tsk
->mems_allowed
);
992 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
993 * @tsk: the task to change
994 * @newmems: new nodes that the task will be set
996 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
997 * we structure updates as setting all new allowed nodes, then clearing newly
1000 static void cpuset_change_task_nodemask(struct task_struct
*tsk
,
1001 nodemask_t
*newmems
)
1006 * Allow tasks that have access to memory reserves because they have
1007 * been OOM killed to get memory anywhere.
1009 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
1011 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
1016 * Determine if a loop is necessary if another thread is doing
1017 * read_mems_allowed_begin(). If at least one node remains unchanged and
1018 * tsk does not have a mempolicy, then an empty nodemask will not be
1019 * possible when mems_allowed is larger than a word.
1021 need_loop
= task_has_mempolicy(tsk
) ||
1022 !nodes_intersects(*newmems
, tsk
->mems_allowed
);
1025 local_irq_disable();
1026 write_seqcount_begin(&tsk
->mems_allowed_seq
);
1029 nodes_or(tsk
->mems_allowed
, tsk
->mems_allowed
, *newmems
);
1030 mpol_rebind_task(tsk
, newmems
, MPOL_REBIND_STEP1
);
1032 mpol_rebind_task(tsk
, newmems
, MPOL_REBIND_STEP2
);
1033 tsk
->mems_allowed
= *newmems
;
1036 write_seqcount_end(&tsk
->mems_allowed_seq
);
1043 struct cpuset_change_nodemask_arg
{
1045 nodemask_t
*newmems
;
1049 * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy
1050 * of it to cpuset's new mems_allowed, and migrate pages to new nodes if
1051 * memory_migrate flag is set. Called with cpuset_mutex held.
1053 static void cpuset_change_nodemask(struct task_struct
*p
, void *data
)
1055 struct cpuset_change_nodemask_arg
*arg
= data
;
1056 struct cpuset
*cs
= arg
->cs
;
1057 struct mm_struct
*mm
;
1060 cpuset_change_task_nodemask(p
, arg
->newmems
);
1062 mm
= get_task_mm(p
);
1066 migrate
= is_memory_migrate(cs
);
1068 mpol_rebind_mm(mm
, &cs
->mems_allowed
);
1070 cpuset_migrate_mm(mm
, &cs
->old_mems_allowed
, arg
->newmems
);
1074 static void *cpuset_being_rebound
;
1077 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1078 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1079 * @heap: if NULL, defer allocating heap memory to css_scan_tasks()
1081 * Called with cpuset_mutex held. No return value. It's guaranteed that
1082 * css_scan_tasks() always returns 0 if @heap != NULL.
1084 static void update_tasks_nodemask(struct cpuset
*cs
, struct ptr_heap
*heap
)
1086 static nodemask_t newmems
; /* protected by cpuset_mutex */
1087 struct cpuset
*mems_cs
= effective_nodemask_cpuset(cs
);
1088 struct cpuset_change_nodemask_arg arg
= { .cs
= cs
,
1089 .newmems
= &newmems
};
1091 cpuset_being_rebound
= cs
; /* causes mpol_dup() rebind */
1093 guarantee_online_mems(mems_cs
, &newmems
);
1096 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1097 * take while holding tasklist_lock. Forks can happen - the
1098 * mpol_dup() cpuset_being_rebound check will catch such forks,
1099 * and rebind their vma mempolicies too. Because we still hold
1100 * the global cpuset_mutex, we know that no other rebind effort
1101 * will be contending for the global variable cpuset_being_rebound.
1102 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
1103 * is idempotent. Also migrate pages in each mm to new nodes.
1105 css_scan_tasks(&cs
->css
, NULL
, cpuset_change_nodemask
, &arg
, heap
);
1108 * All the tasks' nodemasks have been updated, update
1109 * cs->old_mems_allowed.
1111 cs
->old_mems_allowed
= newmems
;
1113 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
1114 cpuset_being_rebound
= NULL
;
1118 * update_tasks_nodemask_hier - Update the nodemasks of tasks in the hierarchy.
1119 * @cs: the root cpuset of the hierarchy
1120 * @update_root: update the root cpuset or not?
1121 * @heap: the heap used by css_scan_tasks()
1123 * This will update nodemasks of tasks in @root_cs and all other empty cpusets
1124 * which take on nodemask of @root_cs.
1126 * Called with cpuset_mutex held
1128 static void update_tasks_nodemask_hier(struct cpuset
*root_cs
,
1129 bool update_root
, struct ptr_heap
*heap
)
1132 struct cgroup_subsys_state
*pos_css
;
1135 cpuset_for_each_descendant_pre(cp
, pos_css
, root_cs
) {
1136 if (cp
== root_cs
) {
1140 /* skip the whole subtree if @cp have some CPU */
1141 if (!nodes_empty(cp
->mems_allowed
)) {
1142 pos_css
= css_rightmost_descendant(pos_css
);
1146 if (!css_tryget(&cp
->css
))
1150 update_tasks_nodemask(cp
, heap
);
1159 * Handle user request to change the 'mems' memory placement
1160 * of a cpuset. Needs to validate the request, update the
1161 * cpusets mems_allowed, and for each task in the cpuset,
1162 * update mems_allowed and rebind task's mempolicy and any vma
1163 * mempolicies and if the cpuset is marked 'memory_migrate',
1164 * migrate the tasks pages to the new memory.
1166 * Call with cpuset_mutex held. May take callback_mutex during call.
1167 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1168 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1169 * their mempolicies to the cpusets new mems_allowed.
1171 static int update_nodemask(struct cpuset
*cs
, struct cpuset
*trialcs
,
1175 struct ptr_heap heap
;
1178 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
1181 if (cs
== &top_cpuset
) {
1187 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1188 * Since nodelist_parse() fails on an empty mask, we special case
1189 * that parsing. The validate_change() call ensures that cpusets
1190 * with tasks have memory.
1193 nodes_clear(trialcs
->mems_allowed
);
1195 retval
= nodelist_parse(buf
, trialcs
->mems_allowed
);
1199 if (!nodes_subset(trialcs
->mems_allowed
,
1200 node_states
[N_MEMORY
])) {
1206 if (nodes_equal(cs
->mems_allowed
, trialcs
->mems_allowed
)) {
1207 retval
= 0; /* Too easy - nothing to do */
1210 retval
= validate_change(cs
, trialcs
);
1214 retval
= heap_init(&heap
, PAGE_SIZE
, GFP_KERNEL
, NULL
);
1218 mutex_lock(&callback_mutex
);
1219 cs
->mems_allowed
= trialcs
->mems_allowed
;
1220 mutex_unlock(&callback_mutex
);
1222 update_tasks_nodemask_hier(cs
, true, &heap
);
1229 int current_cpuset_is_being_rebound(void)
1234 ret
= task_cs(current
) == cpuset_being_rebound
;
1240 static int update_relax_domain_level(struct cpuset
*cs
, s64 val
)
1243 if (val
< -1 || val
>= sched_domain_level_max
)
1247 if (val
!= cs
->relax_domain_level
) {
1248 cs
->relax_domain_level
= val
;
1249 if (!cpumask_empty(cs
->cpus_allowed
) &&
1250 is_sched_load_balance(cs
))
1251 rebuild_sched_domains_locked();
1258 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1259 * @tsk: task to be updated
1260 * @data: cpuset to @tsk belongs to
1262 * Called by css_scan_tasks() for each task in a cgroup.
1264 * We don't need to re-check for the cgroup/cpuset membership, since we're
1265 * holding cpuset_mutex at this point.
1267 static void cpuset_change_flag(struct task_struct
*tsk
, void *data
)
1269 struct cpuset
*cs
= data
;
1271 cpuset_update_task_spread_flag(cs
, tsk
);
1275 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1276 * @cs: the cpuset in which each task's spread flags needs to be changed
1277 * @heap: if NULL, defer allocating heap memory to css_scan_tasks()
1279 * Called with cpuset_mutex held
1281 * The css_scan_tasks() function will scan all the tasks in a cgroup,
1282 * calling callback functions for each.
1284 * No return value. It's guaranteed that css_scan_tasks() always returns 0
1287 static void update_tasks_flags(struct cpuset
*cs
, struct ptr_heap
*heap
)
1289 css_scan_tasks(&cs
->css
, NULL
, cpuset_change_flag
, cs
, heap
);
1293 * update_flag - read a 0 or a 1 in a file and update associated flag
1294 * bit: the bit to update (see cpuset_flagbits_t)
1295 * cs: the cpuset to update
1296 * turning_on: whether the flag is being set or cleared
1298 * Call with cpuset_mutex held.
1301 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
,
1304 struct cpuset
*trialcs
;
1305 int balance_flag_changed
;
1306 int spread_flag_changed
;
1307 struct ptr_heap heap
;
1310 trialcs
= alloc_trial_cpuset(cs
);
1315 set_bit(bit
, &trialcs
->flags
);
1317 clear_bit(bit
, &trialcs
->flags
);
1319 err
= validate_change(cs
, trialcs
);
1323 err
= heap_init(&heap
, PAGE_SIZE
, GFP_KERNEL
, NULL
);
1327 balance_flag_changed
= (is_sched_load_balance(cs
) !=
1328 is_sched_load_balance(trialcs
));
1330 spread_flag_changed
= ((is_spread_slab(cs
) != is_spread_slab(trialcs
))
1331 || (is_spread_page(cs
) != is_spread_page(trialcs
)));
1333 mutex_lock(&callback_mutex
);
1334 cs
->flags
= trialcs
->flags
;
1335 mutex_unlock(&callback_mutex
);
1337 if (!cpumask_empty(trialcs
->cpus_allowed
) && balance_flag_changed
)
1338 rebuild_sched_domains_locked();
1340 if (spread_flag_changed
)
1341 update_tasks_flags(cs
, &heap
);
1344 free_trial_cpuset(trialcs
);
1349 * Frequency meter - How fast is some event occurring?
1351 * These routines manage a digitally filtered, constant time based,
1352 * event frequency meter. There are four routines:
1353 * fmeter_init() - initialize a frequency meter.
1354 * fmeter_markevent() - called each time the event happens.
1355 * fmeter_getrate() - returns the recent rate of such events.
1356 * fmeter_update() - internal routine used to update fmeter.
1358 * A common data structure is passed to each of these routines,
1359 * which is used to keep track of the state required to manage the
1360 * frequency meter and its digital filter.
1362 * The filter works on the number of events marked per unit time.
1363 * The filter is single-pole low-pass recursive (IIR). The time unit
1364 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1365 * simulate 3 decimal digits of precision (multiplied by 1000).
1367 * With an FM_COEF of 933, and a time base of 1 second, the filter
1368 * has a half-life of 10 seconds, meaning that if the events quit
1369 * happening, then the rate returned from the fmeter_getrate()
1370 * will be cut in half each 10 seconds, until it converges to zero.
1372 * It is not worth doing a real infinitely recursive filter. If more
1373 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1374 * just compute FM_MAXTICKS ticks worth, by which point the level
1377 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1378 * arithmetic overflow in the fmeter_update() routine.
1380 * Given the simple 32 bit integer arithmetic used, this meter works
1381 * best for reporting rates between one per millisecond (msec) and
1382 * one per 32 (approx) seconds. At constant rates faster than one
1383 * per msec it maxes out at values just under 1,000,000. At constant
1384 * rates between one per msec, and one per second it will stabilize
1385 * to a value N*1000, where N is the rate of events per second.
1386 * At constant rates between one per second and one per 32 seconds,
1387 * it will be choppy, moving up on the seconds that have an event,
1388 * and then decaying until the next event. At rates slower than
1389 * about one in 32 seconds, it decays all the way back to zero between
1393 #define FM_COEF 933 /* coefficient for half-life of 10 secs */
1394 #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1395 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1396 #define FM_SCALE 1000 /* faux fixed point scale */
1398 /* Initialize a frequency meter */
1399 static void fmeter_init(struct fmeter
*fmp
)
1404 spin_lock_init(&fmp
->lock
);
1407 /* Internal meter update - process cnt events and update value */
1408 static void fmeter_update(struct fmeter
*fmp
)
1410 time_t now
= get_seconds();
1411 time_t ticks
= now
- fmp
->time
;
1416 ticks
= min(FM_MAXTICKS
, ticks
);
1418 fmp
->val
= (FM_COEF
* fmp
->val
) / FM_SCALE
;
1421 fmp
->val
+= ((FM_SCALE
- FM_COEF
) * fmp
->cnt
) / FM_SCALE
;
1425 /* Process any previous ticks, then bump cnt by one (times scale). */
1426 static void fmeter_markevent(struct fmeter
*fmp
)
1428 spin_lock(&fmp
->lock
);
1430 fmp
->cnt
= min(FM_MAXCNT
, fmp
->cnt
+ FM_SCALE
);
1431 spin_unlock(&fmp
->lock
);
1434 /* Process any previous ticks, then return current value. */
1435 static int fmeter_getrate(struct fmeter
*fmp
)
1439 spin_lock(&fmp
->lock
);
1442 spin_unlock(&fmp
->lock
);
1446 /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
1447 static int cpuset_can_attach(struct cgroup_subsys_state
*css
,
1448 struct cgroup_taskset
*tset
)
1450 struct cpuset
*cs
= css_cs(css
);
1451 struct task_struct
*task
;
1454 mutex_lock(&cpuset_mutex
);
1457 * We allow to move tasks into an empty cpuset if sane_behavior
1461 if (!cgroup_sane_behavior(css
->cgroup
) &&
1462 (cpumask_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
)))
1465 cgroup_taskset_for_each(task
, css
, tset
) {
1467 * Kthreads which disallow setaffinity shouldn't be moved
1468 * to a new cpuset; we don't want to change their cpu
1469 * affinity and isolating such threads by their set of
1470 * allowed nodes is unnecessary. Thus, cpusets are not
1471 * applicable for such threads. This prevents checking for
1472 * success of set_cpus_allowed_ptr() on all attached tasks
1473 * before cpus_allowed may be changed.
1476 if (task
->flags
& PF_NO_SETAFFINITY
)
1478 ret
= security_task_setscheduler(task
);
1484 * Mark attach is in progress. This makes validate_change() fail
1485 * changes which zero cpus/mems_allowed.
1487 cs
->attach_in_progress
++;
1490 mutex_unlock(&cpuset_mutex
);
1494 static void cpuset_cancel_attach(struct cgroup_subsys_state
*css
,
1495 struct cgroup_taskset
*tset
)
1497 mutex_lock(&cpuset_mutex
);
1498 css_cs(css
)->attach_in_progress
--;
1499 mutex_unlock(&cpuset_mutex
);
1503 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
1504 * but we can't allocate it dynamically there. Define it global and
1505 * allocate from cpuset_init().
1507 static cpumask_var_t cpus_attach
;
1509 static void cpuset_attach(struct cgroup_subsys_state
*css
,
1510 struct cgroup_taskset
*tset
)
1512 /* static buf protected by cpuset_mutex */
1513 static nodemask_t cpuset_attach_nodemask_to
;
1514 struct mm_struct
*mm
;
1515 struct task_struct
*task
;
1516 struct task_struct
*leader
= cgroup_taskset_first(tset
);
1517 struct cgroup_subsys_state
*oldcss
= cgroup_taskset_cur_css(tset
,
1519 struct cpuset
*cs
= css_cs(css
);
1520 struct cpuset
*oldcs
= css_cs(oldcss
);
1521 struct cpuset
*cpus_cs
= effective_cpumask_cpuset(cs
);
1522 struct cpuset
*mems_cs
= effective_nodemask_cpuset(cs
);
1524 mutex_lock(&cpuset_mutex
);
1526 /* prepare for attach */
1527 if (cs
== &top_cpuset
)
1528 cpumask_copy(cpus_attach
, cpu_possible_mask
);
1530 guarantee_online_cpus(cpus_cs
, cpus_attach
);
1532 guarantee_online_mems(mems_cs
, &cpuset_attach_nodemask_to
);
1534 cgroup_taskset_for_each(task
, css
, tset
) {
1536 * can_attach beforehand should guarantee that this doesn't
1537 * fail. TODO: have a better way to handle failure here
1539 WARN_ON_ONCE(set_cpus_allowed_ptr(task
, cpus_attach
));
1541 cpuset_change_task_nodemask(task
, &cpuset_attach_nodemask_to
);
1542 cpuset_update_task_spread_flag(cs
, task
);
1546 * Change mm, possibly for multiple threads in a threadgroup. This is
1547 * expensive and may sleep.
1549 cpuset_attach_nodemask_to
= cs
->mems_allowed
;
1550 mm
= get_task_mm(leader
);
1552 struct cpuset
*mems_oldcs
= effective_nodemask_cpuset(oldcs
);
1554 mpol_rebind_mm(mm
, &cpuset_attach_nodemask_to
);
1557 * old_mems_allowed is the same with mems_allowed here, except
1558 * if this task is being moved automatically due to hotplug.
1559 * In that case @mems_allowed has been updated and is empty,
1560 * so @old_mems_allowed is the right nodesets that we migrate
1563 if (is_memory_migrate(cs
)) {
1564 cpuset_migrate_mm(mm
, &mems_oldcs
->old_mems_allowed
,
1565 &cpuset_attach_nodemask_to
);
1570 cs
->old_mems_allowed
= cpuset_attach_nodemask_to
;
1572 cs
->attach_in_progress
--;
1573 if (!cs
->attach_in_progress
)
1574 wake_up(&cpuset_attach_wq
);
1576 mutex_unlock(&cpuset_mutex
);
1579 /* The various types of files and directories in a cpuset file system */
1582 FILE_MEMORY_MIGRATE
,
1588 FILE_SCHED_LOAD_BALANCE
,
1589 FILE_SCHED_RELAX_DOMAIN_LEVEL
,
1590 FILE_MEMORY_PRESSURE_ENABLED
,
1591 FILE_MEMORY_PRESSURE
,
1594 } cpuset_filetype_t
;
1596 static int cpuset_write_u64(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
1599 struct cpuset
*cs
= css_cs(css
);
1600 cpuset_filetype_t type
= cft
->private;
1603 mutex_lock(&cpuset_mutex
);
1604 if (!is_cpuset_online(cs
)) {
1610 case FILE_CPU_EXCLUSIVE
:
1611 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, val
);
1613 case FILE_MEM_EXCLUSIVE
:
1614 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, val
);
1616 case FILE_MEM_HARDWALL
:
1617 retval
= update_flag(CS_MEM_HARDWALL
, cs
, val
);
1619 case FILE_SCHED_LOAD_BALANCE
:
1620 retval
= update_flag(CS_SCHED_LOAD_BALANCE
, cs
, val
);
1622 case FILE_MEMORY_MIGRATE
:
1623 retval
= update_flag(CS_MEMORY_MIGRATE
, cs
, val
);
1625 case FILE_MEMORY_PRESSURE_ENABLED
:
1626 cpuset_memory_pressure_enabled
= !!val
;
1628 case FILE_MEMORY_PRESSURE
:
1631 case FILE_SPREAD_PAGE
:
1632 retval
= update_flag(CS_SPREAD_PAGE
, cs
, val
);
1634 case FILE_SPREAD_SLAB
:
1635 retval
= update_flag(CS_SPREAD_SLAB
, cs
, val
);
1642 mutex_unlock(&cpuset_mutex
);
1646 static int cpuset_write_s64(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
1649 struct cpuset
*cs
= css_cs(css
);
1650 cpuset_filetype_t type
= cft
->private;
1651 int retval
= -ENODEV
;
1653 mutex_lock(&cpuset_mutex
);
1654 if (!is_cpuset_online(cs
))
1658 case FILE_SCHED_RELAX_DOMAIN_LEVEL
:
1659 retval
= update_relax_domain_level(cs
, val
);
1666 mutex_unlock(&cpuset_mutex
);
1671 * Common handling for a write to a "cpus" or "mems" file.
1673 static int cpuset_write_resmask(struct cgroup_subsys_state
*css
,
1674 struct cftype
*cft
, const char *buf
)
1676 struct cpuset
*cs
= css_cs(css
);
1677 struct cpuset
*trialcs
;
1678 int retval
= -ENODEV
;
1681 * CPU or memory hotunplug may leave @cs w/o any execution
1682 * resources, in which case the hotplug code asynchronously updates
1683 * configuration and transfers all tasks to the nearest ancestor
1684 * which can execute.
1686 * As writes to "cpus" or "mems" may restore @cs's execution
1687 * resources, wait for the previously scheduled operations before
1688 * proceeding, so that we don't end up keep removing tasks added
1689 * after execution capability is restored.
1691 flush_work(&cpuset_hotplug_work
);
1693 mutex_lock(&cpuset_mutex
);
1694 if (!is_cpuset_online(cs
))
1697 trialcs
= alloc_trial_cpuset(cs
);
1703 switch (cft
->private) {
1705 retval
= update_cpumask(cs
, trialcs
, buf
);
1708 retval
= update_nodemask(cs
, trialcs
, buf
);
1715 free_trial_cpuset(trialcs
);
1717 mutex_unlock(&cpuset_mutex
);
1722 * These ascii lists should be read in a single call, by using a user
1723 * buffer large enough to hold the entire map. If read in smaller
1724 * chunks, there is no guarantee of atomicity. Since the display format
1725 * used, list of ranges of sequential numbers, is variable length,
1726 * and since these maps can change value dynamically, one could read
1727 * gibberish by doing partial reads while a list was changing.
1728 * A single large read to a buffer that crosses a page boundary is
1729 * ok, because the result being copied to user land is not recomputed
1730 * across a page fault.
1733 static size_t cpuset_sprintf_cpulist(char *page
, struct cpuset
*cs
)
1737 mutex_lock(&callback_mutex
);
1738 count
= cpulist_scnprintf(page
, PAGE_SIZE
, cs
->cpus_allowed
);
1739 mutex_unlock(&callback_mutex
);
1744 static size_t cpuset_sprintf_memlist(char *page
, struct cpuset
*cs
)
1748 mutex_lock(&callback_mutex
);
1749 count
= nodelist_scnprintf(page
, PAGE_SIZE
, cs
->mems_allowed
);
1750 mutex_unlock(&callback_mutex
);
1755 static ssize_t
cpuset_common_file_read(struct cgroup_subsys_state
*css
,
1756 struct cftype
*cft
, struct file
*file
,
1757 char __user
*buf
, size_t nbytes
,
1760 struct cpuset
*cs
= css_cs(css
);
1761 cpuset_filetype_t type
= cft
->private;
1766 if (!(page
= (char *)__get_free_page(GFP_TEMPORARY
)))
1773 s
+= cpuset_sprintf_cpulist(s
, cs
);
1776 s
+= cpuset_sprintf_memlist(s
, cs
);
1784 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1786 free_page((unsigned long)page
);
1790 static u64
cpuset_read_u64(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
1792 struct cpuset
*cs
= css_cs(css
);
1793 cpuset_filetype_t type
= cft
->private;
1795 case FILE_CPU_EXCLUSIVE
:
1796 return is_cpu_exclusive(cs
);
1797 case FILE_MEM_EXCLUSIVE
:
1798 return is_mem_exclusive(cs
);
1799 case FILE_MEM_HARDWALL
:
1800 return is_mem_hardwall(cs
);
1801 case FILE_SCHED_LOAD_BALANCE
:
1802 return is_sched_load_balance(cs
);
1803 case FILE_MEMORY_MIGRATE
:
1804 return is_memory_migrate(cs
);
1805 case FILE_MEMORY_PRESSURE_ENABLED
:
1806 return cpuset_memory_pressure_enabled
;
1807 case FILE_MEMORY_PRESSURE
:
1808 return fmeter_getrate(&cs
->fmeter
);
1809 case FILE_SPREAD_PAGE
:
1810 return is_spread_page(cs
);
1811 case FILE_SPREAD_SLAB
:
1812 return is_spread_slab(cs
);
1817 /* Unreachable but makes gcc happy */
1821 static s64
cpuset_read_s64(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
1823 struct cpuset
*cs
= css_cs(css
);
1824 cpuset_filetype_t type
= cft
->private;
1826 case FILE_SCHED_RELAX_DOMAIN_LEVEL
:
1827 return cs
->relax_domain_level
;
1832 /* Unrechable but makes gcc happy */
1838 * for the common functions, 'private' gives the type of file
1841 static struct cftype files
[] = {
1844 .read
= cpuset_common_file_read
,
1845 .write_string
= cpuset_write_resmask
,
1846 .max_write_len
= (100U + 6 * NR_CPUS
),
1847 .private = FILE_CPULIST
,
1852 .read
= cpuset_common_file_read
,
1853 .write_string
= cpuset_write_resmask
,
1854 .max_write_len
= (100U + 6 * MAX_NUMNODES
),
1855 .private = FILE_MEMLIST
,
1859 .name
= "cpu_exclusive",
1860 .read_u64
= cpuset_read_u64
,
1861 .write_u64
= cpuset_write_u64
,
1862 .private = FILE_CPU_EXCLUSIVE
,
1866 .name
= "mem_exclusive",
1867 .read_u64
= cpuset_read_u64
,
1868 .write_u64
= cpuset_write_u64
,
1869 .private = FILE_MEM_EXCLUSIVE
,
1873 .name
= "mem_hardwall",
1874 .read_u64
= cpuset_read_u64
,
1875 .write_u64
= cpuset_write_u64
,
1876 .private = FILE_MEM_HARDWALL
,
1880 .name
= "sched_load_balance",
1881 .read_u64
= cpuset_read_u64
,
1882 .write_u64
= cpuset_write_u64
,
1883 .private = FILE_SCHED_LOAD_BALANCE
,
1887 .name
= "sched_relax_domain_level",
1888 .read_s64
= cpuset_read_s64
,
1889 .write_s64
= cpuset_write_s64
,
1890 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL
,
1894 .name
= "memory_migrate",
1895 .read_u64
= cpuset_read_u64
,
1896 .write_u64
= cpuset_write_u64
,
1897 .private = FILE_MEMORY_MIGRATE
,
1901 .name
= "memory_pressure",
1902 .read_u64
= cpuset_read_u64
,
1903 .write_u64
= cpuset_write_u64
,
1904 .private = FILE_MEMORY_PRESSURE
,
1909 .name
= "memory_spread_page",
1910 .read_u64
= cpuset_read_u64
,
1911 .write_u64
= cpuset_write_u64
,
1912 .private = FILE_SPREAD_PAGE
,
1916 .name
= "memory_spread_slab",
1917 .read_u64
= cpuset_read_u64
,
1918 .write_u64
= cpuset_write_u64
,
1919 .private = FILE_SPREAD_SLAB
,
1923 .name
= "memory_pressure_enabled",
1924 .flags
= CFTYPE_ONLY_ON_ROOT
,
1925 .read_u64
= cpuset_read_u64
,
1926 .write_u64
= cpuset_write_u64
,
1927 .private = FILE_MEMORY_PRESSURE_ENABLED
,
1934 * cpuset_css_alloc - allocate a cpuset css
1935 * cgrp: control group that the new cpuset will be part of
1938 static struct cgroup_subsys_state
*
1939 cpuset_css_alloc(struct cgroup_subsys_state
*parent_css
)
1944 return &top_cpuset
.css
;
1946 cs
= kzalloc(sizeof(*cs
), GFP_KERNEL
);
1948 return ERR_PTR(-ENOMEM
);
1949 if (!alloc_cpumask_var(&cs
->cpus_allowed
, GFP_KERNEL
)) {
1951 return ERR_PTR(-ENOMEM
);
1954 set_bit(CS_SCHED_LOAD_BALANCE
, &cs
->flags
);
1955 cpumask_clear(cs
->cpus_allowed
);
1956 nodes_clear(cs
->mems_allowed
);
1957 fmeter_init(&cs
->fmeter
);
1958 cs
->relax_domain_level
= -1;
1963 static int cpuset_css_online(struct cgroup_subsys_state
*css
)
1965 struct cpuset
*cs
= css_cs(css
);
1966 struct cpuset
*parent
= parent_cs(cs
);
1967 struct cpuset
*tmp_cs
;
1968 struct cgroup_subsys_state
*pos_css
;
1973 mutex_lock(&cpuset_mutex
);
1975 set_bit(CS_ONLINE
, &cs
->flags
);
1976 if (is_spread_page(parent
))
1977 set_bit(CS_SPREAD_PAGE
, &cs
->flags
);
1978 if (is_spread_slab(parent
))
1979 set_bit(CS_SPREAD_SLAB
, &cs
->flags
);
1983 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
))
1987 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
1988 * set. This flag handling is implemented in cgroup core for
1989 * histrical reasons - the flag may be specified during mount.
1991 * Currently, if any sibling cpusets have exclusive cpus or mem, we
1992 * refuse to clone the configuration - thereby refusing the task to
1993 * be entered, and as a result refusing the sys_unshare() or
1994 * clone() which initiated it. If this becomes a problem for some
1995 * users who wish to allow that scenario, then this could be
1996 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
1997 * (and likewise for mems) to the new cgroup.
2000 cpuset_for_each_child(tmp_cs
, pos_css
, parent
) {
2001 if (is_mem_exclusive(tmp_cs
) || is_cpu_exclusive(tmp_cs
)) {
2008 mutex_lock(&callback_mutex
);
2009 cs
->mems_allowed
= parent
->mems_allowed
;
2010 cpumask_copy(cs
->cpus_allowed
, parent
->cpus_allowed
);
2011 mutex_unlock(&callback_mutex
);
2013 mutex_unlock(&cpuset_mutex
);
2018 * If the cpuset being removed has its flag 'sched_load_balance'
2019 * enabled, then simulate turning sched_load_balance off, which
2020 * will call rebuild_sched_domains_locked().
2023 static void cpuset_css_offline(struct cgroup_subsys_state
*css
)
2025 struct cpuset
*cs
= css_cs(css
);
2027 mutex_lock(&cpuset_mutex
);
2029 if (is_sched_load_balance(cs
))
2030 update_flag(CS_SCHED_LOAD_BALANCE
, cs
, 0);
2033 clear_bit(CS_ONLINE
, &cs
->flags
);
2035 mutex_unlock(&cpuset_mutex
);
2038 static void cpuset_css_free(struct cgroup_subsys_state
*css
)
2040 struct cpuset
*cs
= css_cs(css
);
2042 free_cpumask_var(cs
->cpus_allowed
);
2046 struct cgroup_subsys cpuset_subsys
= {
2048 .css_alloc
= cpuset_css_alloc
,
2049 .css_online
= cpuset_css_online
,
2050 .css_offline
= cpuset_css_offline
,
2051 .css_free
= cpuset_css_free
,
2052 .can_attach
= cpuset_can_attach
,
2053 .cancel_attach
= cpuset_cancel_attach
,
2054 .attach
= cpuset_attach
,
2055 .subsys_id
= cpuset_subsys_id
,
2056 .base_cftypes
= files
,
2061 * cpuset_init - initialize cpusets at system boot
2063 * Description: Initialize top_cpuset and the cpuset internal file system,
2066 int __init
cpuset_init(void)
2070 if (!alloc_cpumask_var(&top_cpuset
.cpus_allowed
, GFP_KERNEL
))
2073 cpumask_setall(top_cpuset
.cpus_allowed
);
2074 nodes_setall(top_cpuset
.mems_allowed
);
2076 fmeter_init(&top_cpuset
.fmeter
);
2077 set_bit(CS_SCHED_LOAD_BALANCE
, &top_cpuset
.flags
);
2078 top_cpuset
.relax_domain_level
= -1;
2080 err
= register_filesystem(&cpuset_fs_type
);
2084 if (!alloc_cpumask_var(&cpus_attach
, GFP_KERNEL
))
2091 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
2092 * or memory nodes, we need to walk over the cpuset hierarchy,
2093 * removing that CPU or node from all cpusets. If this removes the
2094 * last CPU or node from a cpuset, then move the tasks in the empty
2095 * cpuset to its next-highest non-empty parent.
2097 static void remove_tasks_in_empty_cpuset(struct cpuset
*cs
)
2099 struct cpuset
*parent
;
2102 * Find its next-highest non-empty parent, (top cpuset
2103 * has online cpus, so can't be empty).
2105 parent
= parent_cs(cs
);
2106 while (cpumask_empty(parent
->cpus_allowed
) ||
2107 nodes_empty(parent
->mems_allowed
))
2108 parent
= parent_cs(parent
);
2110 if (cgroup_transfer_tasks(parent
->css
.cgroup
, cs
->css
.cgroup
)) {
2112 printk(KERN_ERR
"cpuset: failed to transfer tasks out of empty cpuset %s\n",
2113 cgroup_name(cs
->css
.cgroup
));
2119 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
2120 * @cs: cpuset in interest
2122 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2123 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2124 * all its tasks are moved to the nearest ancestor with both resources.
2126 static void cpuset_hotplug_update_tasks(struct cpuset
*cs
)
2128 static cpumask_t off_cpus
;
2129 static nodemask_t off_mems
;
2131 bool sane
= cgroup_sane_behavior(cs
->css
.cgroup
);
2134 wait_event(cpuset_attach_wq
, cs
->attach_in_progress
== 0);
2136 mutex_lock(&cpuset_mutex
);
2139 * We have raced with task attaching. We wait until attaching
2140 * is finished, so we won't attach a task to an empty cpuset.
2142 if (cs
->attach_in_progress
) {
2143 mutex_unlock(&cpuset_mutex
);
2147 cpumask_andnot(&off_cpus
, cs
->cpus_allowed
, top_cpuset
.cpus_allowed
);
2148 nodes_andnot(off_mems
, cs
->mems_allowed
, top_cpuset
.mems_allowed
);
2150 mutex_lock(&callback_mutex
);
2151 cpumask_andnot(cs
->cpus_allowed
, cs
->cpus_allowed
, &off_cpus
);
2152 mutex_unlock(&callback_mutex
);
2155 * If sane_behavior flag is set, we need to update tasks' cpumask
2156 * for empty cpuset to take on ancestor's cpumask. Otherwise, don't
2157 * call update_tasks_cpumask() if the cpuset becomes empty, as
2158 * the tasks in it will be migrated to an ancestor.
2160 if ((sane
&& cpumask_empty(cs
->cpus_allowed
)) ||
2161 (!cpumask_empty(&off_cpus
) && !cpumask_empty(cs
->cpus_allowed
)))
2162 update_tasks_cpumask(cs
, NULL
);
2164 mutex_lock(&callback_mutex
);
2165 nodes_andnot(cs
->mems_allowed
, cs
->mems_allowed
, off_mems
);
2166 mutex_unlock(&callback_mutex
);
2169 * If sane_behavior flag is set, we need to update tasks' nodemask
2170 * for empty cpuset to take on ancestor's nodemask. Otherwise, don't
2171 * call update_tasks_nodemask() if the cpuset becomes empty, as
2172 * the tasks in it will be migratd to an ancestor.
2174 if ((sane
&& nodes_empty(cs
->mems_allowed
)) ||
2175 (!nodes_empty(off_mems
) && !nodes_empty(cs
->mems_allowed
)))
2176 update_tasks_nodemask(cs
, NULL
);
2178 is_empty
= cpumask_empty(cs
->cpus_allowed
) ||
2179 nodes_empty(cs
->mems_allowed
);
2181 mutex_unlock(&cpuset_mutex
);
2184 * If sane_behavior flag is set, we'll keep tasks in empty cpusets.
2186 * Otherwise move tasks to the nearest ancestor with execution
2187 * resources. This is full cgroup operation which will
2188 * also call back into cpuset. Should be done outside any lock.
2190 if (!sane
&& is_empty
)
2191 remove_tasks_in_empty_cpuset(cs
);
2195 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
2197 * This function is called after either CPU or memory configuration has
2198 * changed and updates cpuset accordingly. The top_cpuset is always
2199 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2200 * order to make cpusets transparent (of no affect) on systems that are
2201 * actively using CPU hotplug but making no active use of cpusets.
2203 * Non-root cpusets are only affected by offlining. If any CPUs or memory
2204 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
2207 * Note that CPU offlining during suspend is ignored. We don't modify
2208 * cpusets across suspend/resume cycles at all.
2210 static void cpuset_hotplug_workfn(struct work_struct
*work
)
2212 static cpumask_t new_cpus
;
2213 static nodemask_t new_mems
;
2214 bool cpus_updated
, mems_updated
;
2216 mutex_lock(&cpuset_mutex
);
2218 /* fetch the available cpus/mems and find out which changed how */
2219 cpumask_copy(&new_cpus
, cpu_active_mask
);
2220 new_mems
= node_states
[N_MEMORY
];
2222 cpus_updated
= !cpumask_equal(top_cpuset
.cpus_allowed
, &new_cpus
);
2223 mems_updated
= !nodes_equal(top_cpuset
.mems_allowed
, new_mems
);
2225 /* synchronize cpus_allowed to cpu_active_mask */
2227 mutex_lock(&callback_mutex
);
2228 cpumask_copy(top_cpuset
.cpus_allowed
, &new_cpus
);
2229 mutex_unlock(&callback_mutex
);
2230 /* we don't mess with cpumasks of tasks in top_cpuset */
2233 /* synchronize mems_allowed to N_MEMORY */
2235 mutex_lock(&callback_mutex
);
2236 top_cpuset
.mems_allowed
= new_mems
;
2237 mutex_unlock(&callback_mutex
);
2238 update_tasks_nodemask(&top_cpuset
, NULL
);
2241 mutex_unlock(&cpuset_mutex
);
2243 /* if cpus or mems changed, we need to propagate to descendants */
2244 if (cpus_updated
|| mems_updated
) {
2246 struct cgroup_subsys_state
*pos_css
;
2249 cpuset_for_each_descendant_pre(cs
, pos_css
, &top_cpuset
) {
2250 if (cs
== &top_cpuset
|| !css_tryget(&cs
->css
))
2254 cpuset_hotplug_update_tasks(cs
);
2262 /* rebuild sched domains if cpus_allowed has changed */
2264 rebuild_sched_domains();
2267 void cpuset_update_active_cpus(bool cpu_online
)
2270 * We're inside cpu hotplug critical region which usually nests
2271 * inside cgroup synchronization. Bounce actual hotplug processing
2272 * to a work item to avoid reverse locking order.
2274 * We still need to do partition_sched_domains() synchronously;
2275 * otherwise, the scheduler will get confused and put tasks to the
2276 * dead CPU. Fall back to the default single domain.
2277 * cpuset_hotplug_workfn() will rebuild it as necessary.
2279 partition_sched_domains(1, NULL
, NULL
);
2280 schedule_work(&cpuset_hotplug_work
);
2284 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2285 * Call this routine anytime after node_states[N_MEMORY] changes.
2286 * See cpuset_update_active_cpus() for CPU hotplug handling.
2288 static int cpuset_track_online_nodes(struct notifier_block
*self
,
2289 unsigned long action
, void *arg
)
2291 schedule_work(&cpuset_hotplug_work
);
2295 static struct notifier_block cpuset_track_online_nodes_nb
= {
2296 .notifier_call
= cpuset_track_online_nodes
,
2297 .priority
= 10, /* ??! */
2301 * cpuset_init_smp - initialize cpus_allowed
2303 * Description: Finish top cpuset after cpu, node maps are initialized
2305 void __init
cpuset_init_smp(void)
2307 cpumask_copy(top_cpuset
.cpus_allowed
, cpu_active_mask
);
2308 top_cpuset
.mems_allowed
= node_states
[N_MEMORY
];
2309 top_cpuset
.old_mems_allowed
= top_cpuset
.mems_allowed
;
2311 register_hotmemory_notifier(&cpuset_track_online_nodes_nb
);
2315 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2316 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2317 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
2319 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
2320 * attached to the specified @tsk. Guaranteed to return some non-empty
2321 * subset of cpu_online_mask, even if this means going outside the
2325 void cpuset_cpus_allowed(struct task_struct
*tsk
, struct cpumask
*pmask
)
2327 struct cpuset
*cpus_cs
;
2329 mutex_lock(&callback_mutex
);
2331 cpus_cs
= effective_cpumask_cpuset(task_cs(tsk
));
2332 guarantee_online_cpus(cpus_cs
, pmask
);
2334 mutex_unlock(&callback_mutex
);
2337 void cpuset_cpus_allowed_fallback(struct task_struct
*tsk
)
2339 struct cpuset
*cpus_cs
;
2342 cpus_cs
= effective_cpumask_cpuset(task_cs(tsk
));
2343 do_set_cpus_allowed(tsk
, cpus_cs
->cpus_allowed
);
2347 * We own tsk->cpus_allowed, nobody can change it under us.
2349 * But we used cs && cs->cpus_allowed lockless and thus can
2350 * race with cgroup_attach_task() or update_cpumask() and get
2351 * the wrong tsk->cpus_allowed. However, both cases imply the
2352 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2353 * which takes task_rq_lock().
2355 * If we are called after it dropped the lock we must see all
2356 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2357 * set any mask even if it is not right from task_cs() pov,
2358 * the pending set_cpus_allowed_ptr() will fix things.
2360 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2365 void cpuset_init_current_mems_allowed(void)
2367 nodes_setall(current
->mems_allowed
);
2371 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2372 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2374 * Description: Returns the nodemask_t mems_allowed of the cpuset
2375 * attached to the specified @tsk. Guaranteed to return some non-empty
2376 * subset of node_states[N_MEMORY], even if this means going outside the
2380 nodemask_t
cpuset_mems_allowed(struct task_struct
*tsk
)
2382 struct cpuset
*mems_cs
;
2385 mutex_lock(&callback_mutex
);
2387 mems_cs
= effective_nodemask_cpuset(task_cs(tsk
));
2388 guarantee_online_mems(mems_cs
, &mask
);
2390 mutex_unlock(&callback_mutex
);
2396 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2397 * @nodemask: the nodemask to be checked
2399 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
2401 int cpuset_nodemask_valid_mems_allowed(nodemask_t
*nodemask
)
2403 return nodes_intersects(*nodemask
, current
->mems_allowed
);
2407 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2408 * mem_hardwall ancestor to the specified cpuset. Call holding
2409 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2410 * (an unusual configuration), then returns the root cpuset.
2412 static struct cpuset
*nearest_hardwall_ancestor(struct cpuset
*cs
)
2414 while (!(is_mem_exclusive(cs
) || is_mem_hardwall(cs
)) && parent_cs(cs
))
2420 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2421 * @node: is this an allowed node?
2422 * @gfp_mask: memory allocation flags
2424 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2425 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2426 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2427 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2428 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2432 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2433 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2434 * might sleep, and might allow a node from an enclosing cpuset.
2436 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2437 * cpusets, and never sleeps.
2439 * The __GFP_THISNODE placement logic is really handled elsewhere,
2440 * by forcibly using a zonelist starting at a specified node, and by
2441 * (in get_page_from_freelist()) refusing to consider the zones for
2442 * any node on the zonelist except the first. By the time any such
2443 * calls get to this routine, we should just shut up and say 'yes'.
2445 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2446 * and do not allow allocations outside the current tasks cpuset
2447 * unless the task has been OOM killed as is marked TIF_MEMDIE.
2448 * GFP_KERNEL allocations are not so marked, so can escape to the
2449 * nearest enclosing hardwalled ancestor cpuset.
2451 * Scanning up parent cpusets requires callback_mutex. The
2452 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2453 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2454 * current tasks mems_allowed came up empty on the first pass over
2455 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2456 * cpuset are short of memory, might require taking the callback_mutex
2459 * The first call here from mm/page_alloc:get_page_from_freelist()
2460 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2461 * so no allocation on a node outside the cpuset is allowed (unless
2462 * in interrupt, of course).
2464 * The second pass through get_page_from_freelist() doesn't even call
2465 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2466 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2467 * in alloc_flags. That logic and the checks below have the combined
2469 * in_interrupt - any node ok (current task context irrelevant)
2470 * GFP_ATOMIC - any node ok
2471 * TIF_MEMDIE - any node ok
2472 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
2473 * GFP_USER - only nodes in current tasks mems allowed ok.
2476 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
2477 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2478 * the code that might scan up ancestor cpusets and sleep.
2480 int __cpuset_node_allowed_softwall(int node
, gfp_t gfp_mask
)
2482 struct cpuset
*cs
; /* current cpuset ancestors */
2483 int allowed
; /* is allocation in zone z allowed? */
2485 if (in_interrupt() || (gfp_mask
& __GFP_THISNODE
))
2487 might_sleep_if(!(gfp_mask
& __GFP_HARDWALL
));
2488 if (node_isset(node
, current
->mems_allowed
))
2491 * Allow tasks that have access to memory reserves because they have
2492 * been OOM killed to get memory anywhere.
2494 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
2496 if (gfp_mask
& __GFP_HARDWALL
) /* If hardwall request, stop here */
2499 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
2502 /* Not hardwall and node outside mems_allowed: scan up cpusets */
2503 mutex_lock(&callback_mutex
);
2506 cs
= nearest_hardwall_ancestor(task_cs(current
));
2507 allowed
= node_isset(node
, cs
->mems_allowed
);
2508 task_unlock(current
);
2510 mutex_unlock(&callback_mutex
);
2515 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2516 * @node: is this an allowed node?
2517 * @gfp_mask: memory allocation flags
2519 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2520 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2521 * yes. If the task has been OOM killed and has access to memory reserves as
2522 * specified by the TIF_MEMDIE flag, yes.
2525 * The __GFP_THISNODE placement logic is really handled elsewhere,
2526 * by forcibly using a zonelist starting at a specified node, and by
2527 * (in get_page_from_freelist()) refusing to consider the zones for
2528 * any node on the zonelist except the first. By the time any such
2529 * calls get to this routine, we should just shut up and say 'yes'.
2531 * Unlike the cpuset_node_allowed_softwall() variant, above,
2532 * this variant requires that the node be in the current task's
2533 * mems_allowed or that we're in interrupt. It does not scan up the
2534 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2537 int __cpuset_node_allowed_hardwall(int node
, gfp_t gfp_mask
)
2539 if (in_interrupt() || (gfp_mask
& __GFP_THISNODE
))
2541 if (node_isset(node
, current
->mems_allowed
))
2544 * Allow tasks that have access to memory reserves because they have
2545 * been OOM killed to get memory anywhere.
2547 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
2553 * cpuset_mem_spread_node() - On which node to begin search for a file page
2554 * cpuset_slab_spread_node() - On which node to begin search for a slab page
2556 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2557 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2558 * and if the memory allocation used cpuset_mem_spread_node()
2559 * to determine on which node to start looking, as it will for
2560 * certain page cache or slab cache pages such as used for file
2561 * system buffers and inode caches, then instead of starting on the
2562 * local node to look for a free page, rather spread the starting
2563 * node around the tasks mems_allowed nodes.
2565 * We don't have to worry about the returned node being offline
2566 * because "it can't happen", and even if it did, it would be ok.
2568 * The routines calling guarantee_online_mems() are careful to
2569 * only set nodes in task->mems_allowed that are online. So it
2570 * should not be possible for the following code to return an
2571 * offline node. But if it did, that would be ok, as this routine
2572 * is not returning the node where the allocation must be, only
2573 * the node where the search should start. The zonelist passed to
2574 * __alloc_pages() will include all nodes. If the slab allocator
2575 * is passed an offline node, it will fall back to the local node.
2576 * See kmem_cache_alloc_node().
2579 static int cpuset_spread_node(int *rotor
)
2583 node
= next_node(*rotor
, current
->mems_allowed
);
2584 if (node
== MAX_NUMNODES
)
2585 node
= first_node(current
->mems_allowed
);
2590 int cpuset_mem_spread_node(void)
2592 if (current
->cpuset_mem_spread_rotor
== NUMA_NO_NODE
)
2593 current
->cpuset_mem_spread_rotor
=
2594 node_random(¤t
->mems_allowed
);
2596 return cpuset_spread_node(¤t
->cpuset_mem_spread_rotor
);
2599 int cpuset_slab_spread_node(void)
2601 if (current
->cpuset_slab_spread_rotor
== NUMA_NO_NODE
)
2602 current
->cpuset_slab_spread_rotor
=
2603 node_random(¤t
->mems_allowed
);
2605 return cpuset_spread_node(¤t
->cpuset_slab_spread_rotor
);
2608 EXPORT_SYMBOL_GPL(cpuset_mem_spread_node
);
2611 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2612 * @tsk1: pointer to task_struct of some task.
2613 * @tsk2: pointer to task_struct of some other task.
2615 * Description: Return true if @tsk1's mems_allowed intersects the
2616 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2617 * one of the task's memory usage might impact the memory available
2621 int cpuset_mems_allowed_intersects(const struct task_struct
*tsk1
,
2622 const struct task_struct
*tsk2
)
2624 return nodes_intersects(tsk1
->mems_allowed
, tsk2
->mems_allowed
);
2627 #define CPUSET_NODELIST_LEN (256)
2630 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2631 * @task: pointer to task_struct of some task.
2633 * Description: Prints @task's name, cpuset name, and cached copy of its
2634 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2635 * dereferencing task_cs(task).
2637 void cpuset_print_task_mems_allowed(struct task_struct
*tsk
)
2639 /* Statically allocated to prevent using excess stack. */
2640 static char cpuset_nodelist
[CPUSET_NODELIST_LEN
];
2641 static DEFINE_SPINLOCK(cpuset_buffer_lock
);
2643 struct cgroup
*cgrp
= task_cs(tsk
)->css
.cgroup
;
2646 spin_lock(&cpuset_buffer_lock
);
2648 nodelist_scnprintf(cpuset_nodelist
, CPUSET_NODELIST_LEN
,
2650 printk(KERN_INFO
"%s cpuset=%s mems_allowed=%s\n",
2651 tsk
->comm
, cgroup_name(cgrp
), cpuset_nodelist
);
2653 spin_unlock(&cpuset_buffer_lock
);
2658 * Collection of memory_pressure is suppressed unless
2659 * this flag is enabled by writing "1" to the special
2660 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2663 int cpuset_memory_pressure_enabled __read_mostly
;
2666 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2668 * Keep a running average of the rate of synchronous (direct)
2669 * page reclaim efforts initiated by tasks in each cpuset.
2671 * This represents the rate at which some task in the cpuset
2672 * ran low on memory on all nodes it was allowed to use, and
2673 * had to enter the kernels page reclaim code in an effort to
2674 * create more free memory by tossing clean pages or swapping
2675 * or writing dirty pages.
2677 * Display to user space in the per-cpuset read-only file
2678 * "memory_pressure". Value displayed is an integer
2679 * representing the recent rate of entry into the synchronous
2680 * (direct) page reclaim by any task attached to the cpuset.
2683 void __cpuset_memory_pressure_bump(void)
2686 fmeter_markevent(&task_cs(current
)->fmeter
);
2687 task_unlock(current
);
2690 #ifdef CONFIG_PROC_PID_CPUSET
2692 * proc_cpuset_show()
2693 * - Print tasks cpuset path into seq_file.
2694 * - Used for /proc/<pid>/cpuset.
2695 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2696 * doesn't really matter if tsk->cpuset changes after we read it,
2697 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
2700 int proc_cpuset_show(struct seq_file
*m
, void *unused_v
)
2703 struct task_struct
*tsk
;
2705 struct cgroup_subsys_state
*css
;
2709 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2715 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
2720 css
= task_css(tsk
, cpuset_subsys_id
);
2721 retval
= cgroup_path(css
->cgroup
, buf
, PAGE_SIZE
);
2728 put_task_struct(tsk
);
2734 #endif /* CONFIG_PROC_PID_CPUSET */
2736 /* Display task mems_allowed in /proc/<pid>/status file. */
2737 void cpuset_task_status_allowed(struct seq_file
*m
, struct task_struct
*task
)
2739 seq_printf(m
, "Mems_allowed:\t");
2740 seq_nodemask(m
, &task
->mems_allowed
);
2741 seq_printf(m
, "\n");
2742 seq_printf(m
, "Mems_allowed_list:\t");
2743 seq_nodemask_list(m
, &task
->mems_allowed
);
2744 seq_printf(m
, "\n");