cpuset: schedule hotplug propagation from cpuset_attach() if the cpuset is empty
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / cpuset.c
CommitLineData
1da177e4
LT
1/*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
029190c5 7 * Copyright (C) 2004-2007 Silicon Graphics, Inc.
8793d854 8 * Copyright (C) 2006 Google, Inc
1da177e4
LT
9 *
10 * Portions derived from Patrick Mochel's sysfs code.
11 * sysfs is Copyright (c) 2001-3 Patrick Mochel
1da177e4 12 *
825a46af 13 * 2003-10-10 Written by Simon Derr.
1da177e4 14 * 2003-10-22 Updates by Stephen Hemminger.
825a46af 15 * 2004 May-July Rework by Paul Jackson.
8793d854 16 * 2006 Rework by Paul Menage to use generic cgroups
cf417141
MK
17 * 2008 Rework of the scheduler domains and CPU hotplug handling
18 * by Max Krasnyansky
1da177e4
LT
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
1da177e4
LT
25#include <linux/cpu.h>
26#include <linux/cpumask.h>
27#include <linux/cpuset.h>
28#include <linux/err.h>
29#include <linux/errno.h>
30#include <linux/file.h>
31#include <linux/fs.h>
32#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/kernel.h>
35#include <linux/kmod.h>
36#include <linux/list.h>
68860ec1 37#include <linux/mempolicy.h>
1da177e4 38#include <linux/mm.h>
f481891f 39#include <linux/memory.h>
9984de1a 40#include <linux/export.h>
1da177e4
LT
41#include <linux/mount.h>
42#include <linux/namei.h>
43#include <linux/pagemap.h>
44#include <linux/proc_fs.h>
6b9c2603 45#include <linux/rcupdate.h>
1da177e4
LT
46#include <linux/sched.h>
47#include <linux/seq_file.h>
22fb52dd 48#include <linux/security.h>
1da177e4 49#include <linux/slab.h>
1da177e4
LT
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>
56
57#include <asm/uaccess.h>
60063497 58#include <linux/atomic.h>
3d3f26a7 59#include <linux/mutex.h>
956db3ca
CW
60#include <linux/workqueue.h>
61#include <linux/cgroup.h>
1da177e4 62
202f72d5
PJ
63/*
64 * Tracks how many cpusets are currently defined in system.
65 * When there is only one cpuset (the root cpuset) we can
66 * short circuit some hooks.
67 */
7edc5962 68int number_of_cpusets __read_mostly;
202f72d5 69
2df167a3 70/* Forward declare cgroup structures */
8793d854
PM
71struct cgroup_subsys cpuset_subsys;
72struct cpuset;
73
3e0d98b9
PJ
74/* See "Frequency meter" comments, below. */
75
76struct fmeter {
77 int cnt; /* unprocessed events count */
78 int val; /* most recent output value */
79 time_t time; /* clock (secs) when val computed */
80 spinlock_t lock; /* guards read or write of above */
81};
82
1da177e4 83struct cpuset {
8793d854
PM
84 struct cgroup_subsys_state css;
85
1da177e4 86 unsigned long flags; /* "unsigned long" so bitops work */
300ed6cb 87 cpumask_var_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
1da177e4
LT
88 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
89
1da177e4 90 struct cpuset *parent; /* my parent */
1da177e4 91
3e0d98b9 92 struct fmeter fmeter; /* memory_pressure filter */
029190c5 93
452477fa
TH
94 /*
95 * Tasks are being attached to this cpuset. Used to prevent
96 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
97 */
98 int attach_in_progress;
99
029190c5
PJ
100 /* partition number for rebuild_sched_domains() */
101 int pn;
956db3ca 102
1d3504fc
HS
103 /* for custom sched domain */
104 int relax_domain_level;
105
732bee7a 106 /* used for walking a cpuset hierarchy */
956db3ca 107 struct list_head stack_list;
8d033948
TH
108
109 struct work_struct hotplug_work;
1da177e4
LT
110};
111
8793d854
PM
112/* Retrieve the cpuset for a cgroup */
113static inline struct cpuset *cgroup_cs(struct cgroup *cont)
114{
115 return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
116 struct cpuset, css);
117}
118
119/* Retrieve the cpuset for a task */
120static inline struct cpuset *task_cs(struct task_struct *task)
121{
122 return container_of(task_subsys_state(task, cpuset_subsys_id),
123 struct cpuset, css);
124}
8793d854 125
b246272e
DR
126#ifdef CONFIG_NUMA
127static inline bool task_has_mempolicy(struct task_struct *task)
128{
129 return task->mempolicy;
130}
131#else
132static inline bool task_has_mempolicy(struct task_struct *task)
133{
134 return false;
135}
136#endif
137
138
1da177e4
LT
139/* bits in struct cpuset flags field */
140typedef enum {
efeb77b2 141 CS_ONLINE,
1da177e4
LT
142 CS_CPU_EXCLUSIVE,
143 CS_MEM_EXCLUSIVE,
78608366 144 CS_MEM_HARDWALL,
45b07ef3 145 CS_MEMORY_MIGRATE,
029190c5 146 CS_SCHED_LOAD_BALANCE,
825a46af
PJ
147 CS_SPREAD_PAGE,
148 CS_SPREAD_SLAB,
1da177e4
LT
149} cpuset_flagbits_t;
150
151/* convenient tests for these bits */
efeb77b2
TH
152static inline bool is_cpuset_online(const struct cpuset *cs)
153{
154 return test_bit(CS_ONLINE, &cs->flags);
155}
156
1da177e4
LT
157static inline int is_cpu_exclusive(const struct cpuset *cs)
158{
7b5b9ef0 159 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
1da177e4
LT
160}
161
162static inline int is_mem_exclusive(const struct cpuset *cs)
163{
7b5b9ef0 164 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
1da177e4
LT
165}
166
78608366
PM
167static inline int is_mem_hardwall(const struct cpuset *cs)
168{
169 return test_bit(CS_MEM_HARDWALL, &cs->flags);
170}
171
029190c5
PJ
172static inline int is_sched_load_balance(const struct cpuset *cs)
173{
174 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
175}
176
45b07ef3
PJ
177static inline int is_memory_migrate(const struct cpuset *cs)
178{
7b5b9ef0 179 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
45b07ef3
PJ
180}
181
825a46af
PJ
182static inline int is_spread_page(const struct cpuset *cs)
183{
184 return test_bit(CS_SPREAD_PAGE, &cs->flags);
185}
186
187static inline int is_spread_slab(const struct cpuset *cs)
188{
189 return test_bit(CS_SPREAD_SLAB, &cs->flags);
190}
191
1da177e4 192static struct cpuset top_cpuset = {
efeb77b2
TH
193 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) |
194 (1 << CS_MEM_EXCLUSIVE)),
1da177e4
LT
195};
196
ae8086ce
TH
197/**
198 * cpuset_for_each_child - traverse online children of a cpuset
199 * @child_cs: loop cursor pointing to the current child
200 * @pos_cgrp: used for iteration
201 * @parent_cs: target cpuset to walk children of
202 *
203 * Walk @child_cs through the online children of @parent_cs. Must be used
204 * with RCU read locked.
205 */
206#define cpuset_for_each_child(child_cs, pos_cgrp, parent_cs) \
207 cgroup_for_each_child((pos_cgrp), (parent_cs)->css.cgroup) \
208 if (is_cpuset_online(((child_cs) = cgroup_cs((pos_cgrp)))))
209
1da177e4 210/*
2df167a3
PM
211 * There are two global mutexes guarding cpuset structures. The first
212 * is the main control groups cgroup_mutex, accessed via
213 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
214 * callback_mutex, below. They can nest. It is ok to first take
215 * cgroup_mutex, then nest callback_mutex. We also require taking
216 * task_lock() when dereferencing a task's cpuset pointer. See "The
217 * task_lock() exception", at the end of this comment.
053199ed 218 *
3d3f26a7 219 * A task must hold both mutexes to modify cpusets. If a task
2df167a3 220 * holds cgroup_mutex, then it blocks others wanting that mutex,
3d3f26a7 221 * ensuring that it is the only task able to also acquire callback_mutex
053199ed
PJ
222 * and be able to modify cpusets. It can perform various checks on
223 * the cpuset structure first, knowing nothing will change. It can
2df167a3 224 * also allocate memory while just holding cgroup_mutex. While it is
053199ed 225 * performing these checks, various callback routines can briefly
3d3f26a7
IM
226 * acquire callback_mutex to query cpusets. Once it is ready to make
227 * the changes, it takes callback_mutex, blocking everyone else.
053199ed
PJ
228 *
229 * Calls to the kernel memory allocator can not be made while holding
3d3f26a7 230 * callback_mutex, as that would risk double tripping on callback_mutex
053199ed
PJ
231 * from one of the callbacks into the cpuset code from within
232 * __alloc_pages().
233 *
3d3f26a7 234 * If a task is only holding callback_mutex, then it has read-only
053199ed
PJ
235 * access to cpusets.
236 *
58568d2a
MX
237 * Now, the task_struct fields mems_allowed and mempolicy may be changed
238 * by other task, we use alloc_lock in the task_struct fields to protect
239 * them.
053199ed 240 *
3d3f26a7 241 * The cpuset_common_file_read() handlers only hold callback_mutex across
053199ed
PJ
242 * small pieces of code, such as when reading out possibly multi-word
243 * cpumasks and nodemasks.
244 *
2df167a3
PM
245 * Accessing a task's cpuset should be done in accordance with the
246 * guidelines for accessing subsystem state in kernel/cgroup.c
1da177e4
LT
247 */
248
3d3f26a7 249static DEFINE_MUTEX(callback_mutex);
4247bdc6 250
75aa1994
DR
251/*
252 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
253 * buffers. They are statically allocated to prevent using excess stack
254 * when calling cpuset_print_task_mems_allowed().
255 */
256#define CPUSET_NAME_LEN (128)
257#define CPUSET_NODELIST_LEN (256)
258static char cpuset_name[CPUSET_NAME_LEN];
259static char cpuset_nodelist[CPUSET_NODELIST_LEN];
260static DEFINE_SPINLOCK(cpuset_buffer_lock);
261
3a5a6d0c
TH
262/*
263 * CPU / memory hotplug is handled asynchronously.
264 */
8d033948
TH
265static struct workqueue_struct *cpuset_propagate_hotplug_wq;
266
3a5a6d0c 267static void cpuset_hotplug_workfn(struct work_struct *work);
8d033948 268static void cpuset_propagate_hotplug_workfn(struct work_struct *work);
02bb5863 269static void schedule_cpuset_propagate_hotplug(struct cpuset *cs);
3a5a6d0c
TH
270
271static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn);
272
cf417141
MK
273/*
274 * This is ugly, but preserves the userspace API for existing cpuset
8793d854 275 * users. If someone tries to mount the "cpuset" filesystem, we
cf417141
MK
276 * silently switch it to mount "cgroup" instead
277 */
f7e83571
AV
278static struct dentry *cpuset_mount(struct file_system_type *fs_type,
279 int flags, const char *unused_dev_name, void *data)
1da177e4 280{
8793d854 281 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
f7e83571 282 struct dentry *ret = ERR_PTR(-ENODEV);
8793d854
PM
283 if (cgroup_fs) {
284 char mountopts[] =
285 "cpuset,noprefix,"
286 "release_agent=/sbin/cpuset_release_agent";
f7e83571
AV
287 ret = cgroup_fs->mount(cgroup_fs, flags,
288 unused_dev_name, mountopts);
8793d854
PM
289 put_filesystem(cgroup_fs);
290 }
291 return ret;
1da177e4
LT
292}
293
294static struct file_system_type cpuset_fs_type = {
295 .name = "cpuset",
f7e83571 296 .mount = cpuset_mount,
1da177e4
LT
297};
298
1da177e4 299/*
300ed6cb 300 * Return in pmask the portion of a cpusets's cpus_allowed that
1da177e4
LT
301 * are online. If none are online, walk up the cpuset hierarchy
302 * until we find one that does have some online cpus. If we get
303 * all the way to the top and still haven't found any online cpus,
5f054e31
RR
304 * return cpu_online_mask. Or if passed a NULL cs from an exit'ing
305 * task, return cpu_online_mask.
1da177e4
LT
306 *
307 * One way or another, we guarantee to return some non-empty subset
5f054e31 308 * of cpu_online_mask.
1da177e4 309 *
3d3f26a7 310 * Call with callback_mutex held.
1da177e4
LT
311 */
312
6af866af
LZ
313static void guarantee_online_cpus(const struct cpuset *cs,
314 struct cpumask *pmask)
1da177e4 315{
300ed6cb 316 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
1da177e4
LT
317 cs = cs->parent;
318 if (cs)
300ed6cb 319 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
1da177e4 320 else
300ed6cb
LZ
321 cpumask_copy(pmask, cpu_online_mask);
322 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
1da177e4
LT
323}
324
325/*
326 * Return in *pmask the portion of a cpusets's mems_allowed that
0e1e7c7a
CL
327 * are online, with memory. If none are online with memory, walk
328 * up the cpuset hierarchy until we find one that does have some
329 * online mems. If we get all the way to the top and still haven't
38d7bee9 330 * found any online mems, return node_states[N_MEMORY].
1da177e4
LT
331 *
332 * One way or another, we guarantee to return some non-empty subset
38d7bee9 333 * of node_states[N_MEMORY].
1da177e4 334 *
3d3f26a7 335 * Call with callback_mutex held.
1da177e4
LT
336 */
337
338static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
339{
0e1e7c7a 340 while (cs && !nodes_intersects(cs->mems_allowed,
38d7bee9 341 node_states[N_MEMORY]))
1da177e4
LT
342 cs = cs->parent;
343 if (cs)
0e1e7c7a 344 nodes_and(*pmask, cs->mems_allowed,
38d7bee9 345 node_states[N_MEMORY]);
1da177e4 346 else
38d7bee9
LJ
347 *pmask = node_states[N_MEMORY];
348 BUG_ON(!nodes_intersects(*pmask, node_states[N_MEMORY]));
1da177e4
LT
349}
350
f3b39d47
MX
351/*
352 * update task's spread flag if cpuset's page/slab spread flag is set
353 *
354 * Called with callback_mutex/cgroup_mutex held
355 */
356static void cpuset_update_task_spread_flag(struct cpuset *cs,
357 struct task_struct *tsk)
358{
359 if (is_spread_page(cs))
360 tsk->flags |= PF_SPREAD_PAGE;
361 else
362 tsk->flags &= ~PF_SPREAD_PAGE;
363 if (is_spread_slab(cs))
364 tsk->flags |= PF_SPREAD_SLAB;
365 else
366 tsk->flags &= ~PF_SPREAD_SLAB;
367}
368
1da177e4
LT
369/*
370 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
371 *
372 * One cpuset is a subset of another if all its allowed CPUs and
373 * Memory Nodes are a subset of the other, and its exclusive flags
2df167a3 374 * are only set if the other's are set. Call holding cgroup_mutex.
1da177e4
LT
375 */
376
377static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
378{
300ed6cb 379 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
1da177e4
LT
380 nodes_subset(p->mems_allowed, q->mems_allowed) &&
381 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
382 is_mem_exclusive(p) <= is_mem_exclusive(q);
383}
384
645fcc9d
LZ
385/**
386 * alloc_trial_cpuset - allocate a trial cpuset
387 * @cs: the cpuset that the trial cpuset duplicates
388 */
389static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
390{
300ed6cb
LZ
391 struct cpuset *trial;
392
393 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
394 if (!trial)
395 return NULL;
396
397 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
398 kfree(trial);
399 return NULL;
400 }
401 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
402
403 return trial;
645fcc9d
LZ
404}
405
406/**
407 * free_trial_cpuset - free the trial cpuset
408 * @trial: the trial cpuset to be freed
409 */
410static void free_trial_cpuset(struct cpuset *trial)
411{
300ed6cb 412 free_cpumask_var(trial->cpus_allowed);
645fcc9d
LZ
413 kfree(trial);
414}
415
1da177e4
LT
416/*
417 * validate_change() - Used to validate that any proposed cpuset change
418 * follows the structural rules for cpusets.
419 *
420 * If we replaced the flag and mask values of the current cpuset
421 * (cur) with those values in the trial cpuset (trial), would
422 * our various subset and exclusive rules still be valid? Presumes
2df167a3 423 * cgroup_mutex held.
1da177e4
LT
424 *
425 * 'cur' is the address of an actual, in-use cpuset. Operations
426 * such as list traversal that depend on the actual address of the
427 * cpuset in the list must use cur below, not trial.
428 *
429 * 'trial' is the address of bulk structure copy of cur, with
430 * perhaps one or more of the fields cpus_allowed, mems_allowed,
431 * or flags changed to new, trial values.
432 *
433 * Return 0 if valid, -errno if not.
434 */
435
436static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
437{
8793d854 438 struct cgroup *cont;
1da177e4 439 struct cpuset *c, *par;
ae8086ce
TH
440 int ret;
441
442 rcu_read_lock();
1da177e4
LT
443
444 /* Each of our child cpusets must be a subset of us */
ae8086ce
TH
445 ret = -EBUSY;
446 cpuset_for_each_child(c, cont, cur)
447 if (!is_cpuset_subset(c, trial))
448 goto out;
1da177e4
LT
449
450 /* Remaining checks don't apply to root cpuset */
ae8086ce 451 ret = 0;
69604067 452 if (cur == &top_cpuset)
ae8086ce 453 goto out;
1da177e4 454
69604067
PJ
455 par = cur->parent;
456
1da177e4 457 /* We must be a subset of our parent cpuset */
ae8086ce 458 ret = -EACCES;
1da177e4 459 if (!is_cpuset_subset(trial, par))
ae8086ce 460 goto out;
1da177e4 461
2df167a3
PM
462 /*
463 * If either I or some sibling (!= me) is exclusive, we can't
464 * overlap
465 */
ae8086ce
TH
466 ret = -EINVAL;
467 cpuset_for_each_child(c, cont, par) {
1da177e4
LT
468 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
469 c != cur &&
300ed6cb 470 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
ae8086ce 471 goto out;
1da177e4
LT
472 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
473 c != cur &&
474 nodes_intersects(trial->mems_allowed, c->mems_allowed))
ae8086ce 475 goto out;
1da177e4
LT
476 }
477
452477fa
TH
478 /*
479 * Cpusets with tasks - existing or newly being attached - can't
480 * have empty cpus_allowed or mems_allowed.
481 */
ae8086ce 482 ret = -ENOSPC;
452477fa 483 if ((cgroup_task_count(cur->css.cgroup) || cur->attach_in_progress) &&
ae8086ce
TH
484 (cpumask_empty(trial->cpus_allowed) ||
485 nodes_empty(trial->mems_allowed)))
486 goto out;
020958b6 487
ae8086ce
TH
488 ret = 0;
489out:
490 rcu_read_unlock();
491 return ret;
1da177e4
LT
492}
493
db7f47cf 494#ifdef CONFIG_SMP
029190c5 495/*
cf417141 496 * Helper routine for generate_sched_domains().
029190c5
PJ
497 * Do cpusets a, b have overlapping cpus_allowed masks?
498 */
029190c5
PJ
499static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
500{
300ed6cb 501 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
029190c5
PJ
502}
503
1d3504fc
HS
504static void
505update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
506{
1d3504fc
HS
507 if (dattr->relax_domain_level < c->relax_domain_level)
508 dattr->relax_domain_level = c->relax_domain_level;
509 return;
510}
511
f5393693
LJ
512static void
513update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
514{
515 LIST_HEAD(q);
516
517 list_add(&c->stack_list, &q);
518 while (!list_empty(&q)) {
519 struct cpuset *cp;
520 struct cgroup *cont;
521 struct cpuset *child;
522
523 cp = list_first_entry(&q, struct cpuset, stack_list);
524 list_del(q.next);
525
300ed6cb 526 if (cpumask_empty(cp->cpus_allowed))
f5393693
LJ
527 continue;
528
529 if (is_sched_load_balance(cp))
530 update_domain_attr(dattr, cp);
531
ae8086ce
TH
532 rcu_read_lock();
533 cpuset_for_each_child(child, cont, cp)
f5393693 534 list_add_tail(&child->stack_list, &q);
ae8086ce 535 rcu_read_unlock();
f5393693
LJ
536 }
537}
538
029190c5 539/*
cf417141
MK
540 * generate_sched_domains()
541 *
542 * This function builds a partial partition of the systems CPUs
543 * A 'partial partition' is a set of non-overlapping subsets whose
544 * union is a subset of that set.
545 * The output of this function needs to be passed to kernel/sched.c
546 * partition_sched_domains() routine, which will rebuild the scheduler's
547 * load balancing domains (sched domains) as specified by that partial
548 * partition.
029190c5 549 *
45ce80fb 550 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
029190c5
PJ
551 * for a background explanation of this.
552 *
553 * Does not return errors, on the theory that the callers of this
554 * routine would rather not worry about failures to rebuild sched
555 * domains when operating in the severe memory shortage situations
556 * that could cause allocation failures below.
557 *
cf417141 558 * Must be called with cgroup_lock held.
029190c5
PJ
559 *
560 * The three key local variables below are:
aeed6824 561 * q - a linked-list queue of cpuset pointers, used to implement a
029190c5
PJ
562 * top-down scan of all cpusets. This scan loads a pointer
563 * to each cpuset marked is_sched_load_balance into the
564 * array 'csa'. For our purposes, rebuilding the schedulers
565 * sched domains, we can ignore !is_sched_load_balance cpusets.
566 * csa - (for CpuSet Array) Array of pointers to all the cpusets
567 * that need to be load balanced, for convenient iterative
568 * access by the subsequent code that finds the best partition,
569 * i.e the set of domains (subsets) of CPUs such that the
570 * cpus_allowed of every cpuset marked is_sched_load_balance
571 * is a subset of one of these domains, while there are as
572 * many such domains as possible, each as small as possible.
573 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
574 * the kernel/sched.c routine partition_sched_domains() in a
575 * convenient format, that can be easily compared to the prior
576 * value to determine what partition elements (sched domains)
577 * were changed (added or removed.)
578 *
579 * Finding the best partition (set of domains):
580 * The triple nested loops below over i, j, k scan over the
581 * load balanced cpusets (using the array of cpuset pointers in
582 * csa[]) looking for pairs of cpusets that have overlapping
583 * cpus_allowed, but which don't have the same 'pn' partition
584 * number and gives them in the same partition number. It keeps
585 * looping on the 'restart' label until it can no longer find
586 * any such pairs.
587 *
588 * The union of the cpus_allowed masks from the set of
589 * all cpusets having the same 'pn' value then form the one
590 * element of the partition (one sched domain) to be passed to
591 * partition_sched_domains().
592 */
acc3f5d7 593static int generate_sched_domains(cpumask_var_t **domains,
cf417141 594 struct sched_domain_attr **attributes)
029190c5 595{
cf417141 596 LIST_HEAD(q); /* queue of cpusets to be scanned */
029190c5
PJ
597 struct cpuset *cp; /* scans q */
598 struct cpuset **csa; /* array of all cpuset ptrs */
599 int csn; /* how many cpuset ptrs in csa so far */
600 int i, j, k; /* indices for partition finding loops */
acc3f5d7 601 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
1d3504fc 602 struct sched_domain_attr *dattr; /* attributes for custom domains */
1583715d 603 int ndoms = 0; /* number of sched domains in result */
6af866af 604 int nslot; /* next empty doms[] struct cpumask slot */
029190c5 605
029190c5 606 doms = NULL;
1d3504fc 607 dattr = NULL;
cf417141 608 csa = NULL;
029190c5
PJ
609
610 /* Special case for the 99% of systems with one, full, sched domain */
611 if (is_sched_load_balance(&top_cpuset)) {
acc3f5d7
RR
612 ndoms = 1;
613 doms = alloc_sched_domains(ndoms);
029190c5 614 if (!doms)
cf417141
MK
615 goto done;
616
1d3504fc
HS
617 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
618 if (dattr) {
619 *dattr = SD_ATTR_INIT;
93a65575 620 update_domain_attr_tree(dattr, &top_cpuset);
1d3504fc 621 }
acc3f5d7 622 cpumask_copy(doms[0], top_cpuset.cpus_allowed);
cf417141 623
cf417141 624 goto done;
029190c5
PJ
625 }
626
029190c5
PJ
627 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
628 if (!csa)
629 goto done;
630 csn = 0;
631
aeed6824
LZ
632 list_add(&top_cpuset.stack_list, &q);
633 while (!list_empty(&q)) {
029190c5
PJ
634 struct cgroup *cont;
635 struct cpuset *child; /* scans child cpusets of cp */
489a5393 636
aeed6824
LZ
637 cp = list_first_entry(&q, struct cpuset, stack_list);
638 list_del(q.next);
639
300ed6cb 640 if (cpumask_empty(cp->cpus_allowed))
489a5393
LJ
641 continue;
642
f5393693
LJ
643 /*
644 * All child cpusets contain a subset of the parent's cpus, so
645 * just skip them, and then we call update_domain_attr_tree()
646 * to calc relax_domain_level of the corresponding sched
647 * domain.
648 */
649 if (is_sched_load_balance(cp)) {
029190c5 650 csa[csn++] = cp;
f5393693
LJ
651 continue;
652 }
489a5393 653
ae8086ce
TH
654 rcu_read_lock();
655 cpuset_for_each_child(child, cont, cp)
aeed6824 656 list_add_tail(&child->stack_list, &q);
ae8086ce 657 rcu_read_unlock();
029190c5
PJ
658 }
659
660 for (i = 0; i < csn; i++)
661 csa[i]->pn = i;
662 ndoms = csn;
663
664restart:
665 /* Find the best partition (set of sched domains) */
666 for (i = 0; i < csn; i++) {
667 struct cpuset *a = csa[i];
668 int apn = a->pn;
669
670 for (j = 0; j < csn; j++) {
671 struct cpuset *b = csa[j];
672 int bpn = b->pn;
673
674 if (apn != bpn && cpusets_overlap(a, b)) {
675 for (k = 0; k < csn; k++) {
676 struct cpuset *c = csa[k];
677
678 if (c->pn == bpn)
679 c->pn = apn;
680 }
681 ndoms--; /* one less element */
682 goto restart;
683 }
684 }
685 }
686
cf417141
MK
687 /*
688 * Now we know how many domains to create.
689 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
690 */
acc3f5d7 691 doms = alloc_sched_domains(ndoms);
700018e0 692 if (!doms)
cf417141 693 goto done;
cf417141
MK
694
695 /*
696 * The rest of the code, including the scheduler, can deal with
697 * dattr==NULL case. No need to abort if alloc fails.
698 */
1d3504fc 699 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
029190c5
PJ
700
701 for (nslot = 0, i = 0; i < csn; i++) {
702 struct cpuset *a = csa[i];
6af866af 703 struct cpumask *dp;
029190c5
PJ
704 int apn = a->pn;
705
cf417141
MK
706 if (apn < 0) {
707 /* Skip completed partitions */
708 continue;
709 }
710
acc3f5d7 711 dp = doms[nslot];
cf417141
MK
712
713 if (nslot == ndoms) {
714 static int warnings = 10;
715 if (warnings) {
716 printk(KERN_WARNING
717 "rebuild_sched_domains confused:"
718 " nslot %d, ndoms %d, csn %d, i %d,"
719 " apn %d\n",
720 nslot, ndoms, csn, i, apn);
721 warnings--;
029190c5 722 }
cf417141
MK
723 continue;
724 }
029190c5 725
6af866af 726 cpumask_clear(dp);
cf417141
MK
727 if (dattr)
728 *(dattr + nslot) = SD_ATTR_INIT;
729 for (j = i; j < csn; j++) {
730 struct cpuset *b = csa[j];
731
732 if (apn == b->pn) {
300ed6cb 733 cpumask_or(dp, dp, b->cpus_allowed);
cf417141
MK
734 if (dattr)
735 update_domain_attr_tree(dattr + nslot, b);
736
737 /* Done with this partition */
738 b->pn = -1;
029190c5 739 }
029190c5 740 }
cf417141 741 nslot++;
029190c5
PJ
742 }
743 BUG_ON(nslot != ndoms);
744
cf417141
MK
745done:
746 kfree(csa);
747
700018e0
LZ
748 /*
749 * Fallback to the default domain if kmalloc() failed.
750 * See comments in partition_sched_domains().
751 */
752 if (doms == NULL)
753 ndoms = 1;
754
cf417141
MK
755 *domains = doms;
756 *attributes = dattr;
757 return ndoms;
758}
759
760/*
761 * Rebuild scheduler domains.
762 *
699140ba
TH
763 * If the flag 'sched_load_balance' of any cpuset with non-empty
764 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
765 * which has that flag enabled, or if any cpuset with a non-empty
766 * 'cpus' is removed, then call this routine to rebuild the
767 * scheduler's dynamic sched domains.
cf417141 768 *
699140ba 769 * Call with cgroup_mutex held. Takes get_online_cpus().
cf417141 770 */
699140ba 771static void rebuild_sched_domains_locked(void)
cf417141
MK
772{
773 struct sched_domain_attr *attr;
acc3f5d7 774 cpumask_var_t *doms;
cf417141
MK
775 int ndoms;
776
699140ba 777 WARN_ON_ONCE(!cgroup_lock_is_held());
86ef5c9a 778 get_online_cpus();
cf417141
MK
779
780 /* Generate domain masks and attrs */
cf417141 781 ndoms = generate_sched_domains(&doms, &attr);
cf417141
MK
782
783 /* Have scheduler rebuild the domains */
784 partition_sched_domains(ndoms, doms, attr);
785
86ef5c9a 786 put_online_cpus();
cf417141 787}
db7f47cf 788#else /* !CONFIG_SMP */
699140ba 789static void rebuild_sched_domains_locked(void)
db7f47cf
PM
790{
791}
792
e1b8090b 793static int generate_sched_domains(cpumask_var_t **domains,
db7f47cf
PM
794 struct sched_domain_attr **attributes)
795{
796 *domains = NULL;
797 return 1;
798}
799#endif /* CONFIG_SMP */
029190c5 800
cf417141
MK
801void rebuild_sched_domains(void)
802{
699140ba
TH
803 cgroup_lock();
804 rebuild_sched_domains_locked();
805 cgroup_unlock();
029190c5
PJ
806}
807
58f4790b
CW
808/**
809 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
810 * @tsk: task to test
811 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
812 *
2df167a3 813 * Call with cgroup_mutex held. May take callback_mutex during call.
58f4790b
CW
814 * Called for each task in a cgroup by cgroup_scan_tasks().
815 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
816 * words, if its mask is not equal to its cpuset's mask).
053199ed 817 */
9e0c914c
AB
818static int cpuset_test_cpumask(struct task_struct *tsk,
819 struct cgroup_scanner *scan)
58f4790b 820{
300ed6cb 821 return !cpumask_equal(&tsk->cpus_allowed,
58f4790b
CW
822 (cgroup_cs(scan->cg))->cpus_allowed);
823}
053199ed 824
58f4790b
CW
825/**
826 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
827 * @tsk: task to test
828 * @scan: struct cgroup_scanner containing the cgroup of the task
829 *
830 * Called by cgroup_scan_tasks() for each task in a cgroup whose
831 * cpus_allowed mask needs to be changed.
832 *
833 * We don't need to re-check for the cgroup/cpuset membership, since we're
834 * holding cgroup_lock() at this point.
835 */
9e0c914c
AB
836static void cpuset_change_cpumask(struct task_struct *tsk,
837 struct cgroup_scanner *scan)
58f4790b 838{
300ed6cb 839 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
58f4790b
CW
840}
841
0b2f630a
MX
842/**
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
4e74339a 845 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
0b2f630a
MX
846 *
847 * Called with cgroup_mutex held
848 *
849 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
850 * calling callback functions for each.
851 *
4e74339a
LZ
852 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
853 * if @heap != NULL.
0b2f630a 854 */
4e74339a 855static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
0b2f630a
MX
856{
857 struct cgroup_scanner scan;
0b2f630a
MX
858
859 scan.cg = cs->css.cgroup;
860 scan.test_task = cpuset_test_cpumask;
861 scan.process_task = cpuset_change_cpumask;
4e74339a
LZ
862 scan.heap = heap;
863 cgroup_scan_tasks(&scan);
0b2f630a
MX
864}
865
58f4790b
CW
866/**
867 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
868 * @cs: the cpuset to consider
869 * @buf: buffer of cpu numbers written to this cpuset
870 */
645fcc9d
LZ
871static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
872 const char *buf)
1da177e4 873{
4e74339a 874 struct ptr_heap heap;
58f4790b
CW
875 int retval;
876 int is_load_balanced;
1da177e4 877
5f054e31 878 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
4c4d50f7
PJ
879 if (cs == &top_cpuset)
880 return -EACCES;
881
6f7f02e7 882 /*
c8d9c90c 883 * An empty cpus_allowed is ok only if the cpuset has no tasks.
020958b6
PJ
884 * Since cpulist_parse() fails on an empty mask, we special case
885 * that parsing. The validate_change() call ensures that cpusets
886 * with tasks have cpus.
6f7f02e7 887 */
020958b6 888 if (!*buf) {
300ed6cb 889 cpumask_clear(trialcs->cpus_allowed);
6f7f02e7 890 } else {
300ed6cb 891 retval = cpulist_parse(buf, trialcs->cpus_allowed);
6f7f02e7
DR
892 if (retval < 0)
893 return retval;
37340746 894
6ad4c188 895 if (!cpumask_subset(trialcs->cpus_allowed, cpu_active_mask))
37340746 896 return -EINVAL;
6f7f02e7 897 }
645fcc9d 898 retval = validate_change(cs, trialcs);
85d7b949
DG
899 if (retval < 0)
900 return retval;
029190c5 901
8707d8b8 902 /* Nothing to do if the cpus didn't change */
300ed6cb 903 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
8707d8b8 904 return 0;
58f4790b 905
4e74339a
LZ
906 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
907 if (retval)
908 return retval;
909
645fcc9d 910 is_load_balanced = is_sched_load_balance(trialcs);
029190c5 911
3d3f26a7 912 mutex_lock(&callback_mutex);
300ed6cb 913 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
3d3f26a7 914 mutex_unlock(&callback_mutex);
029190c5 915
8707d8b8
PM
916 /*
917 * Scan tasks in the cpuset, and update the cpumasks of any
58f4790b 918 * that need an update.
8707d8b8 919 */
4e74339a
LZ
920 update_tasks_cpumask(cs, &heap);
921
922 heap_free(&heap);
58f4790b 923
8707d8b8 924 if (is_load_balanced)
699140ba 925 rebuild_sched_domains_locked();
85d7b949 926 return 0;
1da177e4
LT
927}
928
e4e364e8
PJ
929/*
930 * cpuset_migrate_mm
931 *
932 * Migrate memory region from one set of nodes to another.
933 *
934 * Temporarilly set tasks mems_allowed to target nodes of migration,
935 * so that the migration code can allocate pages on these nodes.
936 *
2df167a3 937 * Call holding cgroup_mutex, so current's cpuset won't change
c8d9c90c 938 * during this call, as manage_mutex holds off any cpuset_attach()
e4e364e8
PJ
939 * calls. Therefore we don't need to take task_lock around the
940 * call to guarantee_online_mems(), as we know no one is changing
2df167a3 941 * our task's cpuset.
e4e364e8 942 *
e4e364e8
PJ
943 * While the mm_struct we are migrating is typically from some
944 * other task, the task_struct mems_allowed that we are hacking
945 * is for our current task, which must allocate new pages for that
946 * migrating memory region.
e4e364e8
PJ
947 */
948
949static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
950 const nodemask_t *to)
951{
952 struct task_struct *tsk = current;
953
e4e364e8 954 tsk->mems_allowed = *to;
e4e364e8
PJ
955
956 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
957
8793d854 958 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
e4e364e8
PJ
959}
960
3b6766fe 961/*
58568d2a
MX
962 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
963 * @tsk: the task to change
964 * @newmems: new nodes that the task will be set
965 *
966 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
967 * we structure updates as setting all new allowed nodes, then clearing newly
968 * disallowed ones.
58568d2a
MX
969 */
970static void cpuset_change_task_nodemask(struct task_struct *tsk,
971 nodemask_t *newmems)
972{
b246272e 973 bool need_loop;
89e8a244 974
c0ff7453
MX
975 /*
976 * Allow tasks that have access to memory reserves because they have
977 * been OOM killed to get memory anywhere.
978 */
979 if (unlikely(test_thread_flag(TIF_MEMDIE)))
980 return;
981 if (current->flags & PF_EXITING) /* Let dying task have memory */
982 return;
983
984 task_lock(tsk);
b246272e
DR
985 /*
986 * Determine if a loop is necessary if another thread is doing
987 * get_mems_allowed(). If at least one node remains unchanged and
988 * tsk does not have a mempolicy, then an empty nodemask will not be
989 * possible when mems_allowed is larger than a word.
990 */
991 need_loop = task_has_mempolicy(tsk) ||
992 !nodes_intersects(*newmems, tsk->mems_allowed);
c0ff7453 993
cc9a6c87
MG
994 if (need_loop)
995 write_seqcount_begin(&tsk->mems_allowed_seq);
c0ff7453 996
cc9a6c87
MG
997 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
998 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
c0ff7453
MX
999
1000 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
58568d2a 1001 tsk->mems_allowed = *newmems;
cc9a6c87
MG
1002
1003 if (need_loop)
1004 write_seqcount_end(&tsk->mems_allowed_seq);
1005
c0ff7453 1006 task_unlock(tsk);
58568d2a
MX
1007}
1008
1009/*
1010 * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy
1011 * of it to cpuset's new mems_allowed, and migrate pages to new nodes if
1012 * memory_migrate flag is set. Called with cgroup_mutex held.
3b6766fe
LZ
1013 */
1014static void cpuset_change_nodemask(struct task_struct *p,
1015 struct cgroup_scanner *scan)
1016{
1017 struct mm_struct *mm;
1018 struct cpuset *cs;
1019 int migrate;
1020 const nodemask_t *oldmem = scan->data;
ee24d379 1021 static nodemask_t newmems; /* protected by cgroup_mutex */
58568d2a
MX
1022
1023 cs = cgroup_cs(scan->cg);
ee24d379 1024 guarantee_online_mems(cs, &newmems);
58568d2a 1025
ee24d379 1026 cpuset_change_task_nodemask(p, &newmems);
53feb297 1027
3b6766fe
LZ
1028 mm = get_task_mm(p);
1029 if (!mm)
1030 return;
1031
3b6766fe
LZ
1032 migrate = is_memory_migrate(cs);
1033
1034 mpol_rebind_mm(mm, &cs->mems_allowed);
1035 if (migrate)
1036 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1037 mmput(mm);
1038}
1039
8793d854
PM
1040static void *cpuset_being_rebound;
1041
0b2f630a
MX
1042/**
1043 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1044 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1045 * @oldmem: old mems_allowed of cpuset cs
010cfac4 1046 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
0b2f630a
MX
1047 *
1048 * Called with cgroup_mutex held
010cfac4
LZ
1049 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1050 * if @heap != NULL.
0b2f630a 1051 */
010cfac4
LZ
1052static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1053 struct ptr_heap *heap)
1da177e4 1054{
3b6766fe 1055 struct cgroup_scanner scan;
59dac16f 1056
846a16bf 1057 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
4225399a 1058
3b6766fe
LZ
1059 scan.cg = cs->css.cgroup;
1060 scan.test_task = NULL;
1061 scan.process_task = cpuset_change_nodemask;
010cfac4 1062 scan.heap = heap;
3b6766fe 1063 scan.data = (nodemask_t *)oldmem;
4225399a
PJ
1064
1065 /*
3b6766fe
LZ
1066 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1067 * take while holding tasklist_lock. Forks can happen - the
1068 * mpol_dup() cpuset_being_rebound check will catch such forks,
1069 * and rebind their vma mempolicies too. Because we still hold
1070 * the global cgroup_mutex, we know that no other rebind effort
1071 * will be contending for the global variable cpuset_being_rebound.
4225399a 1072 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
04c19fa6 1073 * is idempotent. Also migrate pages in each mm to new nodes.
4225399a 1074 */
010cfac4 1075 cgroup_scan_tasks(&scan);
4225399a 1076
2df167a3 1077 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
8793d854 1078 cpuset_being_rebound = NULL;
1da177e4
LT
1079}
1080
0b2f630a
MX
1081/*
1082 * Handle user request to change the 'mems' memory placement
1083 * of a cpuset. Needs to validate the request, update the
58568d2a
MX
1084 * cpusets mems_allowed, and for each task in the cpuset,
1085 * update mems_allowed and rebind task's mempolicy and any vma
1086 * mempolicies and if the cpuset is marked 'memory_migrate',
1087 * migrate the tasks pages to the new memory.
0b2f630a
MX
1088 *
1089 * Call with cgroup_mutex held. May take callback_mutex during call.
1090 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1091 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1092 * their mempolicies to the cpusets new mems_allowed.
1093 */
645fcc9d
LZ
1094static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1095 const char *buf)
0b2f630a 1096{
53feb297 1097 NODEMASK_ALLOC(nodemask_t, oldmem, GFP_KERNEL);
0b2f630a 1098 int retval;
010cfac4 1099 struct ptr_heap heap;
0b2f630a 1100
53feb297
MX
1101 if (!oldmem)
1102 return -ENOMEM;
1103
0b2f630a 1104 /*
38d7bee9 1105 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
0b2f630a
MX
1106 * it's read-only
1107 */
53feb297
MX
1108 if (cs == &top_cpuset) {
1109 retval = -EACCES;
1110 goto done;
1111 }
0b2f630a 1112
0b2f630a
MX
1113 /*
1114 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1115 * Since nodelist_parse() fails on an empty mask, we special case
1116 * that parsing. The validate_change() call ensures that cpusets
1117 * with tasks have memory.
1118 */
1119 if (!*buf) {
645fcc9d 1120 nodes_clear(trialcs->mems_allowed);
0b2f630a 1121 } else {
645fcc9d 1122 retval = nodelist_parse(buf, trialcs->mems_allowed);
0b2f630a
MX
1123 if (retval < 0)
1124 goto done;
1125
645fcc9d 1126 if (!nodes_subset(trialcs->mems_allowed,
38d7bee9 1127 node_states[N_MEMORY])) {
53feb297
MX
1128 retval = -EINVAL;
1129 goto done;
1130 }
0b2f630a 1131 }
53feb297
MX
1132 *oldmem = cs->mems_allowed;
1133 if (nodes_equal(*oldmem, trialcs->mems_allowed)) {
0b2f630a
MX
1134 retval = 0; /* Too easy - nothing to do */
1135 goto done;
1136 }
645fcc9d 1137 retval = validate_change(cs, trialcs);
0b2f630a
MX
1138 if (retval < 0)
1139 goto done;
1140
010cfac4
LZ
1141 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1142 if (retval < 0)
1143 goto done;
1144
0b2f630a 1145 mutex_lock(&callback_mutex);
645fcc9d 1146 cs->mems_allowed = trialcs->mems_allowed;
0b2f630a
MX
1147 mutex_unlock(&callback_mutex);
1148
53feb297 1149 update_tasks_nodemask(cs, oldmem, &heap);
010cfac4
LZ
1150
1151 heap_free(&heap);
0b2f630a 1152done:
53feb297 1153 NODEMASK_FREE(oldmem);
0b2f630a
MX
1154 return retval;
1155}
1156
8793d854
PM
1157int current_cpuset_is_being_rebound(void)
1158{
1159 return task_cs(current) == cpuset_being_rebound;
1160}
1161
5be7a479 1162static int update_relax_domain_level(struct cpuset *cs, s64 val)
1d3504fc 1163{
db7f47cf 1164#ifdef CONFIG_SMP
60495e77 1165 if (val < -1 || val >= sched_domain_level_max)
30e0e178 1166 return -EINVAL;
db7f47cf 1167#endif
1d3504fc
HS
1168
1169 if (val != cs->relax_domain_level) {
1170 cs->relax_domain_level = val;
300ed6cb
LZ
1171 if (!cpumask_empty(cs->cpus_allowed) &&
1172 is_sched_load_balance(cs))
699140ba 1173 rebuild_sched_domains_locked();
1d3504fc
HS
1174 }
1175
1176 return 0;
1177}
1178
950592f7
MX
1179/*
1180 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1181 * @tsk: task to be updated
1182 * @scan: struct cgroup_scanner containing the cgroup of the task
1183 *
1184 * Called by cgroup_scan_tasks() for each task in a cgroup.
1185 *
1186 * We don't need to re-check for the cgroup/cpuset membership, since we're
1187 * holding cgroup_lock() at this point.
1188 */
1189static void cpuset_change_flag(struct task_struct *tsk,
1190 struct cgroup_scanner *scan)
1191{
1192 cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk);
1193}
1194
1195/*
1196 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1197 * @cs: the cpuset in which each task's spread flags needs to be changed
1198 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
1199 *
1200 * Called with cgroup_mutex held
1201 *
1202 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1203 * calling callback functions for each.
1204 *
1205 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1206 * if @heap != NULL.
1207 */
1208static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap)
1209{
1210 struct cgroup_scanner scan;
1211
1212 scan.cg = cs->css.cgroup;
1213 scan.test_task = NULL;
1214 scan.process_task = cpuset_change_flag;
1215 scan.heap = heap;
1216 cgroup_scan_tasks(&scan);
1217}
1218
1da177e4
LT
1219/*
1220 * update_flag - read a 0 or a 1 in a file and update associated flag
78608366
PM
1221 * bit: the bit to update (see cpuset_flagbits_t)
1222 * cs: the cpuset to update
1223 * turning_on: whether the flag is being set or cleared
053199ed 1224 *
2df167a3 1225 * Call with cgroup_mutex held.
1da177e4
LT
1226 */
1227
700fe1ab
PM
1228static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1229 int turning_on)
1da177e4 1230{
645fcc9d 1231 struct cpuset *trialcs;
40b6a762 1232 int balance_flag_changed;
950592f7
MX
1233 int spread_flag_changed;
1234 struct ptr_heap heap;
1235 int err;
1da177e4 1236
645fcc9d
LZ
1237 trialcs = alloc_trial_cpuset(cs);
1238 if (!trialcs)
1239 return -ENOMEM;
1240
1da177e4 1241 if (turning_on)
645fcc9d 1242 set_bit(bit, &trialcs->flags);
1da177e4 1243 else
645fcc9d 1244 clear_bit(bit, &trialcs->flags);
1da177e4 1245
645fcc9d 1246 err = validate_change(cs, trialcs);
85d7b949 1247 if (err < 0)
645fcc9d 1248 goto out;
029190c5 1249
950592f7
MX
1250 err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1251 if (err < 0)
1252 goto out;
1253
029190c5 1254 balance_flag_changed = (is_sched_load_balance(cs) !=
645fcc9d 1255 is_sched_load_balance(trialcs));
029190c5 1256
950592f7
MX
1257 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1258 || (is_spread_page(cs) != is_spread_page(trialcs)));
1259
3d3f26a7 1260 mutex_lock(&callback_mutex);
645fcc9d 1261 cs->flags = trialcs->flags;
3d3f26a7 1262 mutex_unlock(&callback_mutex);
85d7b949 1263
300ed6cb 1264 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
699140ba 1265 rebuild_sched_domains_locked();
029190c5 1266
950592f7
MX
1267 if (spread_flag_changed)
1268 update_tasks_flags(cs, &heap);
1269 heap_free(&heap);
645fcc9d
LZ
1270out:
1271 free_trial_cpuset(trialcs);
1272 return err;
1da177e4
LT
1273}
1274
3e0d98b9 1275/*
80f7228b 1276 * Frequency meter - How fast is some event occurring?
3e0d98b9
PJ
1277 *
1278 * These routines manage a digitally filtered, constant time based,
1279 * event frequency meter. There are four routines:
1280 * fmeter_init() - initialize a frequency meter.
1281 * fmeter_markevent() - called each time the event happens.
1282 * fmeter_getrate() - returns the recent rate of such events.
1283 * fmeter_update() - internal routine used to update fmeter.
1284 *
1285 * A common data structure is passed to each of these routines,
1286 * which is used to keep track of the state required to manage the
1287 * frequency meter and its digital filter.
1288 *
1289 * The filter works on the number of events marked per unit time.
1290 * The filter is single-pole low-pass recursive (IIR). The time unit
1291 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1292 * simulate 3 decimal digits of precision (multiplied by 1000).
1293 *
1294 * With an FM_COEF of 933, and a time base of 1 second, the filter
1295 * has a half-life of 10 seconds, meaning that if the events quit
1296 * happening, then the rate returned from the fmeter_getrate()
1297 * will be cut in half each 10 seconds, until it converges to zero.
1298 *
1299 * It is not worth doing a real infinitely recursive filter. If more
1300 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1301 * just compute FM_MAXTICKS ticks worth, by which point the level
1302 * will be stable.
1303 *
1304 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1305 * arithmetic overflow in the fmeter_update() routine.
1306 *
1307 * Given the simple 32 bit integer arithmetic used, this meter works
1308 * best for reporting rates between one per millisecond (msec) and
1309 * one per 32 (approx) seconds. At constant rates faster than one
1310 * per msec it maxes out at values just under 1,000,000. At constant
1311 * rates between one per msec, and one per second it will stabilize
1312 * to a value N*1000, where N is the rate of events per second.
1313 * At constant rates between one per second and one per 32 seconds,
1314 * it will be choppy, moving up on the seconds that have an event,
1315 * and then decaying until the next event. At rates slower than
1316 * about one in 32 seconds, it decays all the way back to zero between
1317 * each event.
1318 */
1319
1320#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1321#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1322#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1323#define FM_SCALE 1000 /* faux fixed point scale */
1324
1325/* Initialize a frequency meter */
1326static void fmeter_init(struct fmeter *fmp)
1327{
1328 fmp->cnt = 0;
1329 fmp->val = 0;
1330 fmp->time = 0;
1331 spin_lock_init(&fmp->lock);
1332}
1333
1334/* Internal meter update - process cnt events and update value */
1335static void fmeter_update(struct fmeter *fmp)
1336{
1337 time_t now = get_seconds();
1338 time_t ticks = now - fmp->time;
1339
1340 if (ticks == 0)
1341 return;
1342
1343 ticks = min(FM_MAXTICKS, ticks);
1344 while (ticks-- > 0)
1345 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1346 fmp->time = now;
1347
1348 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1349 fmp->cnt = 0;
1350}
1351
1352/* Process any previous ticks, then bump cnt by one (times scale). */
1353static void fmeter_markevent(struct fmeter *fmp)
1354{
1355 spin_lock(&fmp->lock);
1356 fmeter_update(fmp);
1357 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1358 spin_unlock(&fmp->lock);
1359}
1360
1361/* Process any previous ticks, then return current value. */
1362static int fmeter_getrate(struct fmeter *fmp)
1363{
1364 int val;
1365
1366 spin_lock(&fmp->lock);
1367 fmeter_update(fmp);
1368 val = fmp->val;
1369 spin_unlock(&fmp->lock);
1370 return val;
1371}
1372
2df167a3 1373/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
761b3ef5 1374static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
f780bdb7 1375{
2f7ee569 1376 struct cpuset *cs = cgroup_cs(cgrp);
bb9d97b6
TH
1377 struct task_struct *task;
1378 int ret;
1da177e4 1379
300ed6cb 1380 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1da177e4 1381 return -ENOSPC;
9985b0ba 1382
bb9d97b6
TH
1383 cgroup_taskset_for_each(task, cgrp, tset) {
1384 /*
1385 * Kthreads bound to specific cpus cannot be moved to a new
1386 * cpuset; we cannot change their cpu affinity and
1387 * isolating such threads by their set of allowed nodes is
1388 * unnecessary. Thus, cpusets are not applicable for such
1389 * threads. This prevents checking for success of
1390 * set_cpus_allowed_ptr() on all attached tasks before
1391 * cpus_allowed may be changed.
1392 */
1393 if (task->flags & PF_THREAD_BOUND)
1394 return -EINVAL;
1395 if ((ret = security_task_setscheduler(task)))
1396 return ret;
1397 }
f780bdb7 1398
452477fa
TH
1399 /*
1400 * Mark attach is in progress. This makes validate_change() fail
1401 * changes which zero cpus/mems_allowed.
1402 */
1403 cs->attach_in_progress++;
1404
94196f51 1405 return 0;
8793d854 1406}
1da177e4 1407
452477fa
TH
1408static void cpuset_cancel_attach(struct cgroup *cgrp,
1409 struct cgroup_taskset *tset)
1410{
1411 cgroup_cs(cgrp)->attach_in_progress--;
1412}
1413
4e4c9a14
TH
1414/*
1415 * Protected by cgroup_mutex. cpus_attach is used only by cpuset_attach()
1416 * but we can't allocate it dynamically there. Define it global and
1417 * allocate from cpuset_init().
1418 */
1419static cpumask_var_t cpus_attach;
1420
761b3ef5 1421static void cpuset_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
8793d854 1422{
4e4c9a14
TH
1423 /* static bufs protected by cgroup_mutex */
1424 static nodemask_t cpuset_attach_nodemask_from;
1425 static nodemask_t cpuset_attach_nodemask_to;
8793d854 1426 struct mm_struct *mm;
bb9d97b6
TH
1427 struct task_struct *task;
1428 struct task_struct *leader = cgroup_taskset_first(tset);
2f7ee569
TH
1429 struct cgroup *oldcgrp = cgroup_taskset_cur_cgroup(tset);
1430 struct cpuset *cs = cgroup_cs(cgrp);
1431 struct cpuset *oldcs = cgroup_cs(oldcgrp);
22fb52dd 1432
4e4c9a14
TH
1433 /* prepare for attach */
1434 if (cs == &top_cpuset)
1435 cpumask_copy(cpus_attach, cpu_possible_mask);
1436 else
1437 guarantee_online_cpus(cs, cpus_attach);
1438
1439 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
1440
bb9d97b6
TH
1441 cgroup_taskset_for_each(task, cgrp, tset) {
1442 /*
1443 * can_attach beforehand should guarantee that this doesn't
1444 * fail. TODO: have a better way to handle failure here
1445 */
1446 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1447
1448 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1449 cpuset_update_task_spread_flag(cs, task);
1450 }
22fb52dd 1451
f780bdb7
BB
1452 /*
1453 * Change mm, possibly for multiple threads in a threadgroup. This is
1454 * expensive and may sleep.
1455 */
1456 cpuset_attach_nodemask_from = oldcs->mems_allowed;
1457 cpuset_attach_nodemask_to = cs->mems_allowed;
bb9d97b6 1458 mm = get_task_mm(leader);
4225399a 1459 if (mm) {
f780bdb7 1460 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
2741a559 1461 if (is_memory_migrate(cs))
f780bdb7
BB
1462 cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from,
1463 &cpuset_attach_nodemask_to);
4225399a
PJ
1464 mmput(mm);
1465 }
452477fa
TH
1466
1467 cs->attach_in_progress--;
02bb5863
TH
1468
1469 /*
1470 * We may have raced with CPU/memory hotunplug. Trigger hotplug
1471 * propagation if @cs doesn't have any CPU or memory. It will move
1472 * the newly added tasks to the nearest parent which can execute.
1473 */
1474 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1475 schedule_cpuset_propagate_hotplug(cs);
1da177e4
LT
1476}
1477
1478/* The various types of files and directories in a cpuset file system */
1479
1480typedef enum {
45b07ef3 1481 FILE_MEMORY_MIGRATE,
1da177e4
LT
1482 FILE_CPULIST,
1483 FILE_MEMLIST,
1484 FILE_CPU_EXCLUSIVE,
1485 FILE_MEM_EXCLUSIVE,
78608366 1486 FILE_MEM_HARDWALL,
029190c5 1487 FILE_SCHED_LOAD_BALANCE,
1d3504fc 1488 FILE_SCHED_RELAX_DOMAIN_LEVEL,
3e0d98b9
PJ
1489 FILE_MEMORY_PRESSURE_ENABLED,
1490 FILE_MEMORY_PRESSURE,
825a46af
PJ
1491 FILE_SPREAD_PAGE,
1492 FILE_SPREAD_SLAB,
1da177e4
LT
1493} cpuset_filetype_t;
1494
700fe1ab
PM
1495static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1496{
1497 int retval = 0;
1498 struct cpuset *cs = cgroup_cs(cgrp);
1499 cpuset_filetype_t type = cft->private;
1500
e3712395 1501 if (!cgroup_lock_live_group(cgrp))
700fe1ab 1502 return -ENODEV;
700fe1ab
PM
1503
1504 switch (type) {
1da177e4 1505 case FILE_CPU_EXCLUSIVE:
700fe1ab 1506 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1da177e4
LT
1507 break;
1508 case FILE_MEM_EXCLUSIVE:
700fe1ab 1509 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1da177e4 1510 break;
78608366
PM
1511 case FILE_MEM_HARDWALL:
1512 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1513 break;
029190c5 1514 case FILE_SCHED_LOAD_BALANCE:
700fe1ab 1515 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1d3504fc 1516 break;
45b07ef3 1517 case FILE_MEMORY_MIGRATE:
700fe1ab 1518 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
45b07ef3 1519 break;
3e0d98b9 1520 case FILE_MEMORY_PRESSURE_ENABLED:
700fe1ab 1521 cpuset_memory_pressure_enabled = !!val;
3e0d98b9
PJ
1522 break;
1523 case FILE_MEMORY_PRESSURE:
1524 retval = -EACCES;
1525 break;
825a46af 1526 case FILE_SPREAD_PAGE:
700fe1ab 1527 retval = update_flag(CS_SPREAD_PAGE, cs, val);
825a46af
PJ
1528 break;
1529 case FILE_SPREAD_SLAB:
700fe1ab 1530 retval = update_flag(CS_SPREAD_SLAB, cs, val);
825a46af 1531 break;
1da177e4
LT
1532 default:
1533 retval = -EINVAL;
700fe1ab 1534 break;
1da177e4 1535 }
8793d854 1536 cgroup_unlock();
1da177e4
LT
1537 return retval;
1538}
1539
5be7a479
PM
1540static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1541{
1542 int retval = 0;
1543 struct cpuset *cs = cgroup_cs(cgrp);
1544 cpuset_filetype_t type = cft->private;
1545
e3712395 1546 if (!cgroup_lock_live_group(cgrp))
5be7a479 1547 return -ENODEV;
e3712395 1548
5be7a479
PM
1549 switch (type) {
1550 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1551 retval = update_relax_domain_level(cs, val);
1552 break;
1553 default:
1554 retval = -EINVAL;
1555 break;
1556 }
1557 cgroup_unlock();
1558 return retval;
1559}
1560
e3712395
PM
1561/*
1562 * Common handling for a write to a "cpus" or "mems" file.
1563 */
1564static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1565 const char *buf)
1566{
1567 int retval = 0;
645fcc9d
LZ
1568 struct cpuset *cs = cgroup_cs(cgrp);
1569 struct cpuset *trialcs;
e3712395 1570
3a5a6d0c
TH
1571 /*
1572 * CPU or memory hotunplug may leave @cs w/o any execution
1573 * resources, in which case the hotplug code asynchronously updates
1574 * configuration and transfers all tasks to the nearest ancestor
1575 * which can execute.
1576 *
1577 * As writes to "cpus" or "mems" may restore @cs's execution
1578 * resources, wait for the previously scheduled operations before
1579 * proceeding, so that we don't end up keep removing tasks added
1580 * after execution capability is restored.
02bb5863
TH
1581 *
1582 * Flushing cpuset_hotplug_work is enough to synchronize against
1583 * hotplug hanlding; however, cpuset_attach() may schedule
1584 * propagation work directly. Flush the workqueue too.
3a5a6d0c
TH
1585 */
1586 flush_work(&cpuset_hotplug_work);
02bb5863 1587 flush_workqueue(cpuset_propagate_hotplug_wq);
3a5a6d0c 1588
e3712395
PM
1589 if (!cgroup_lock_live_group(cgrp))
1590 return -ENODEV;
1591
645fcc9d 1592 trialcs = alloc_trial_cpuset(cs);
b75f38d6
LZ
1593 if (!trialcs) {
1594 retval = -ENOMEM;
1595 goto out;
1596 }
645fcc9d 1597
e3712395
PM
1598 switch (cft->private) {
1599 case FILE_CPULIST:
645fcc9d 1600 retval = update_cpumask(cs, trialcs, buf);
e3712395
PM
1601 break;
1602 case FILE_MEMLIST:
645fcc9d 1603 retval = update_nodemask(cs, trialcs, buf);
e3712395
PM
1604 break;
1605 default:
1606 retval = -EINVAL;
1607 break;
1608 }
645fcc9d
LZ
1609
1610 free_trial_cpuset(trialcs);
b75f38d6 1611out:
e3712395
PM
1612 cgroup_unlock();
1613 return retval;
1614}
1615
1da177e4
LT
1616/*
1617 * These ascii lists should be read in a single call, by using a user
1618 * buffer large enough to hold the entire map. If read in smaller
1619 * chunks, there is no guarantee of atomicity. Since the display format
1620 * used, list of ranges of sequential numbers, is variable length,
1621 * and since these maps can change value dynamically, one could read
1622 * gibberish by doing partial reads while a list was changing.
1623 * A single large read to a buffer that crosses a page boundary is
1624 * ok, because the result being copied to user land is not recomputed
1625 * across a page fault.
1626 */
1627
9303e0c4 1628static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1da177e4 1629{
9303e0c4 1630 size_t count;
1da177e4 1631
3d3f26a7 1632 mutex_lock(&callback_mutex);
9303e0c4 1633 count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
3d3f26a7 1634 mutex_unlock(&callback_mutex);
1da177e4 1635
9303e0c4 1636 return count;
1da177e4
LT
1637}
1638
9303e0c4 1639static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1da177e4 1640{
9303e0c4 1641 size_t count;
1da177e4 1642
3d3f26a7 1643 mutex_lock(&callback_mutex);
9303e0c4 1644 count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed);
3d3f26a7 1645 mutex_unlock(&callback_mutex);
1da177e4 1646
9303e0c4 1647 return count;
1da177e4
LT
1648}
1649
8793d854
PM
1650static ssize_t cpuset_common_file_read(struct cgroup *cont,
1651 struct cftype *cft,
1652 struct file *file,
1653 char __user *buf,
1654 size_t nbytes, loff_t *ppos)
1da177e4 1655{
8793d854 1656 struct cpuset *cs = cgroup_cs(cont);
1da177e4
LT
1657 cpuset_filetype_t type = cft->private;
1658 char *page;
1659 ssize_t retval = 0;
1660 char *s;
1da177e4 1661
e12ba74d 1662 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
1da177e4
LT
1663 return -ENOMEM;
1664
1665 s = page;
1666
1667 switch (type) {
1668 case FILE_CPULIST:
1669 s += cpuset_sprintf_cpulist(s, cs);
1670 break;
1671 case FILE_MEMLIST:
1672 s += cpuset_sprintf_memlist(s, cs);
1673 break;
1da177e4
LT
1674 default:
1675 retval = -EINVAL;
1676 goto out;
1677 }
1678 *s++ = '\n';
1da177e4 1679
eacaa1f5 1680 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1da177e4
LT
1681out:
1682 free_page((unsigned long)page);
1683 return retval;
1684}
1685
700fe1ab
PM
1686static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1687{
1688 struct cpuset *cs = cgroup_cs(cont);
1689 cpuset_filetype_t type = cft->private;
1690 switch (type) {
1691 case FILE_CPU_EXCLUSIVE:
1692 return is_cpu_exclusive(cs);
1693 case FILE_MEM_EXCLUSIVE:
1694 return is_mem_exclusive(cs);
78608366
PM
1695 case FILE_MEM_HARDWALL:
1696 return is_mem_hardwall(cs);
700fe1ab
PM
1697 case FILE_SCHED_LOAD_BALANCE:
1698 return is_sched_load_balance(cs);
1699 case FILE_MEMORY_MIGRATE:
1700 return is_memory_migrate(cs);
1701 case FILE_MEMORY_PRESSURE_ENABLED:
1702 return cpuset_memory_pressure_enabled;
1703 case FILE_MEMORY_PRESSURE:
1704 return fmeter_getrate(&cs->fmeter);
1705 case FILE_SPREAD_PAGE:
1706 return is_spread_page(cs);
1707 case FILE_SPREAD_SLAB:
1708 return is_spread_slab(cs);
1709 default:
1710 BUG();
1711 }
cf417141
MK
1712
1713 /* Unreachable but makes gcc happy */
1714 return 0;
700fe1ab 1715}
1da177e4 1716
5be7a479
PM
1717static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1718{
1719 struct cpuset *cs = cgroup_cs(cont);
1720 cpuset_filetype_t type = cft->private;
1721 switch (type) {
1722 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1723 return cs->relax_domain_level;
1724 default:
1725 BUG();
1726 }
cf417141
MK
1727
1728 /* Unrechable but makes gcc happy */
1729 return 0;
5be7a479
PM
1730}
1731
1da177e4
LT
1732
1733/*
1734 * for the common functions, 'private' gives the type of file
1735 */
1736
addf2c73
PM
1737static struct cftype files[] = {
1738 {
1739 .name = "cpus",
1740 .read = cpuset_common_file_read,
e3712395
PM
1741 .write_string = cpuset_write_resmask,
1742 .max_write_len = (100U + 6 * NR_CPUS),
addf2c73
PM
1743 .private = FILE_CPULIST,
1744 },
1745
1746 {
1747 .name = "mems",
1748 .read = cpuset_common_file_read,
e3712395
PM
1749 .write_string = cpuset_write_resmask,
1750 .max_write_len = (100U + 6 * MAX_NUMNODES),
addf2c73
PM
1751 .private = FILE_MEMLIST,
1752 },
1753
1754 {
1755 .name = "cpu_exclusive",
1756 .read_u64 = cpuset_read_u64,
1757 .write_u64 = cpuset_write_u64,
1758 .private = FILE_CPU_EXCLUSIVE,
1759 },
1760
1761 {
1762 .name = "mem_exclusive",
1763 .read_u64 = cpuset_read_u64,
1764 .write_u64 = cpuset_write_u64,
1765 .private = FILE_MEM_EXCLUSIVE,
1766 },
1767
78608366
PM
1768 {
1769 .name = "mem_hardwall",
1770 .read_u64 = cpuset_read_u64,
1771 .write_u64 = cpuset_write_u64,
1772 .private = FILE_MEM_HARDWALL,
1773 },
1774
addf2c73
PM
1775 {
1776 .name = "sched_load_balance",
1777 .read_u64 = cpuset_read_u64,
1778 .write_u64 = cpuset_write_u64,
1779 .private = FILE_SCHED_LOAD_BALANCE,
1780 },
1781
1782 {
1783 .name = "sched_relax_domain_level",
5be7a479
PM
1784 .read_s64 = cpuset_read_s64,
1785 .write_s64 = cpuset_write_s64,
addf2c73
PM
1786 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1787 },
1788
1789 {
1790 .name = "memory_migrate",
1791 .read_u64 = cpuset_read_u64,
1792 .write_u64 = cpuset_write_u64,
1793 .private = FILE_MEMORY_MIGRATE,
1794 },
1795
1796 {
1797 .name = "memory_pressure",
1798 .read_u64 = cpuset_read_u64,
1799 .write_u64 = cpuset_write_u64,
1800 .private = FILE_MEMORY_PRESSURE,
099fca32 1801 .mode = S_IRUGO,
addf2c73
PM
1802 },
1803
1804 {
1805 .name = "memory_spread_page",
1806 .read_u64 = cpuset_read_u64,
1807 .write_u64 = cpuset_write_u64,
1808 .private = FILE_SPREAD_PAGE,
1809 },
1810
1811 {
1812 .name = "memory_spread_slab",
1813 .read_u64 = cpuset_read_u64,
1814 .write_u64 = cpuset_write_u64,
1815 .private = FILE_SPREAD_SLAB,
1816 },
3e0d98b9 1817
4baf6e33
TH
1818 {
1819 .name = "memory_pressure_enabled",
1820 .flags = CFTYPE_ONLY_ON_ROOT,
1821 .read_u64 = cpuset_read_u64,
1822 .write_u64 = cpuset_write_u64,
1823 .private = FILE_MEMORY_PRESSURE_ENABLED,
1824 },
1da177e4 1825
4baf6e33
TH
1826 { } /* terminate */
1827};
1da177e4
LT
1828
1829/*
92fb9748 1830 * cpuset_css_alloc - allocate a cpuset css
2df167a3 1831 * cont: control group that the new cpuset will be part of
1da177e4
LT
1832 */
1833
92fb9748 1834static struct cgroup_subsys_state *cpuset_css_alloc(struct cgroup *cont)
1da177e4 1835{
c8f699bb 1836 struct cpuset *cs;
1da177e4 1837
c8f699bb 1838 if (!cont->parent)
8793d854 1839 return &top_cpuset.css;
033fa1c5 1840
c8f699bb 1841 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
1da177e4 1842 if (!cs)
8793d854 1843 return ERR_PTR(-ENOMEM);
300ed6cb
LZ
1844 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1845 kfree(cs);
1846 return ERR_PTR(-ENOMEM);
1847 }
1da177e4 1848
029190c5 1849 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
300ed6cb 1850 cpumask_clear(cs->cpus_allowed);
f9a86fcb 1851 nodes_clear(cs->mems_allowed);
3e0d98b9 1852 fmeter_init(&cs->fmeter);
8d033948 1853 INIT_WORK(&cs->hotplug_work, cpuset_propagate_hotplug_workfn);
1d3504fc 1854 cs->relax_domain_level = -1;
c8f699bb
TH
1855 cs->parent = cgroup_cs(cont->parent);
1856
1857 return &cs->css;
1858}
1859
1860static int cpuset_css_online(struct cgroup *cgrp)
1861{
1862 struct cpuset *cs = cgroup_cs(cgrp);
1863 struct cpuset *parent = cs->parent;
ae8086ce
TH
1864 struct cpuset *tmp_cs;
1865 struct cgroup *pos_cg;
c8f699bb
TH
1866
1867 if (!parent)
1868 return 0;
1869
efeb77b2 1870 set_bit(CS_ONLINE, &cs->flags);
c8f699bb
TH
1871 if (is_spread_page(parent))
1872 set_bit(CS_SPREAD_PAGE, &cs->flags);
1873 if (is_spread_slab(parent))
1874 set_bit(CS_SPREAD_SLAB, &cs->flags);
1da177e4 1875
202f72d5 1876 number_of_cpusets++;
033fa1c5 1877
c8f699bb
TH
1878 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags))
1879 return 0;
033fa1c5
TH
1880
1881 /*
1882 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
1883 * set. This flag handling is implemented in cgroup core for
1884 * histrical reasons - the flag may be specified during mount.
1885 *
1886 * Currently, if any sibling cpusets have exclusive cpus or mem, we
1887 * refuse to clone the configuration - thereby refusing the task to
1888 * be entered, and as a result refusing the sys_unshare() or
1889 * clone() which initiated it. If this becomes a problem for some
1890 * users who wish to allow that scenario, then this could be
1891 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
1892 * (and likewise for mems) to the new cgroup.
1893 */
ae8086ce
TH
1894 rcu_read_lock();
1895 cpuset_for_each_child(tmp_cs, pos_cg, parent) {
1896 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
1897 rcu_read_unlock();
c8f699bb 1898 return 0;
ae8086ce 1899 }
033fa1c5 1900 }
ae8086ce 1901 rcu_read_unlock();
033fa1c5
TH
1902
1903 mutex_lock(&callback_mutex);
1904 cs->mems_allowed = parent->mems_allowed;
1905 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
1906 mutex_unlock(&callback_mutex);
c8f699bb
TH
1907
1908 return 0;
1909}
1910
1911static void cpuset_css_offline(struct cgroup *cgrp)
1912{
1913 struct cpuset *cs = cgroup_cs(cgrp);
1914
1915 /* css_offline is called w/o cgroup_mutex, grab it */
1916 cgroup_lock();
1917
1918 if (is_sched_load_balance(cs))
1919 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
1920
1921 number_of_cpusets--;
efeb77b2 1922 clear_bit(CS_ONLINE, &cs->flags);
c8f699bb
TH
1923
1924 cgroup_unlock();
1da177e4
LT
1925}
1926
029190c5 1927/*
029190c5
PJ
1928 * If the cpuset being removed has its flag 'sched_load_balance'
1929 * enabled, then simulate turning sched_load_balance off, which
699140ba 1930 * will call rebuild_sched_domains_locked().
029190c5
PJ
1931 */
1932
92fb9748 1933static void cpuset_css_free(struct cgroup *cont)
1da177e4 1934{
8793d854 1935 struct cpuset *cs = cgroup_cs(cont);
1da177e4 1936
300ed6cb 1937 free_cpumask_var(cs->cpus_allowed);
8793d854 1938 kfree(cs);
1da177e4
LT
1939}
1940
8793d854
PM
1941struct cgroup_subsys cpuset_subsys = {
1942 .name = "cpuset",
92fb9748 1943 .css_alloc = cpuset_css_alloc,
c8f699bb
TH
1944 .css_online = cpuset_css_online,
1945 .css_offline = cpuset_css_offline,
92fb9748 1946 .css_free = cpuset_css_free,
8793d854 1947 .can_attach = cpuset_can_attach,
452477fa 1948 .cancel_attach = cpuset_cancel_attach,
8793d854 1949 .attach = cpuset_attach,
8793d854 1950 .subsys_id = cpuset_subsys_id,
4baf6e33 1951 .base_cftypes = files,
8793d854
PM
1952 .early_init = 1,
1953};
1954
1da177e4
LT
1955/**
1956 * cpuset_init - initialize cpusets at system boot
1957 *
1958 * Description: Initialize top_cpuset and the cpuset internal file system,
1959 **/
1960
1961int __init cpuset_init(void)
1962{
8793d854 1963 int err = 0;
1da177e4 1964
58568d2a
MX
1965 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
1966 BUG();
1967
300ed6cb 1968 cpumask_setall(top_cpuset.cpus_allowed);
f9a86fcb 1969 nodes_setall(top_cpuset.mems_allowed);
1da177e4 1970
3e0d98b9 1971 fmeter_init(&top_cpuset.fmeter);
029190c5 1972 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
1d3504fc 1973 top_cpuset.relax_domain_level = -1;
1da177e4 1974
1da177e4
LT
1975 err = register_filesystem(&cpuset_fs_type);
1976 if (err < 0)
8793d854
PM
1977 return err;
1978
2341d1b6
LZ
1979 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1980 BUG();
1981
202f72d5 1982 number_of_cpusets = 1;
8793d854 1983 return 0;
1da177e4
LT
1984}
1985
956db3ca
CW
1986/**
1987 * cpuset_do_move_task - move a given task to another cpuset
1988 * @tsk: pointer to task_struct the task to move
1989 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1990 *
1991 * Called by cgroup_scan_tasks() for each task in a cgroup.
1992 * Return nonzero to stop the walk through the tasks.
1993 */
9e0c914c
AB
1994static void cpuset_do_move_task(struct task_struct *tsk,
1995 struct cgroup_scanner *scan)
956db3ca 1996{
7f81b1ae 1997 struct cgroup *new_cgroup = scan->data;
956db3ca 1998
7f81b1ae 1999 cgroup_attach_task(new_cgroup, tsk);
956db3ca
CW
2000}
2001
2002/**
2003 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
2004 * @from: cpuset in which the tasks currently reside
2005 * @to: cpuset to which the tasks will be moved
2006 *
c8d9c90c
PJ
2007 * Called with cgroup_mutex held
2008 * callback_mutex must not be held, as cpuset_attach() will take it.
956db3ca
CW
2009 *
2010 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
2011 * calling callback functions for each.
2012 */
2013static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
2014{
7f81b1ae 2015 struct cgroup_scanner scan;
956db3ca 2016
7f81b1ae
LZ
2017 scan.cg = from->css.cgroup;
2018 scan.test_task = NULL; /* select all tasks in cgroup */
2019 scan.process_task = cpuset_do_move_task;
2020 scan.heap = NULL;
2021 scan.data = to->css.cgroup;
956db3ca 2022
7f81b1ae 2023 if (cgroup_scan_tasks(&scan))
956db3ca
CW
2024 printk(KERN_ERR "move_member_tasks_to_cpuset: "
2025 "cgroup_scan_tasks failed\n");
2026}
2027
b1aac8bb 2028/*
cf417141 2029 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
b1aac8bb
PJ
2030 * or memory nodes, we need to walk over the cpuset hierarchy,
2031 * removing that CPU or node from all cpusets. If this removes the
956db3ca
CW
2032 * last CPU or node from a cpuset, then move the tasks in the empty
2033 * cpuset to its next-highest non-empty parent.
b1aac8bb 2034 *
c8d9c90c
PJ
2035 * Called with cgroup_mutex held
2036 * callback_mutex must not be held, as cpuset_attach() will take it.
b1aac8bb 2037 */
956db3ca
CW
2038static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
2039{
2040 struct cpuset *parent;
2041
956db3ca
CW
2042 /*
2043 * Find its next-highest non-empty parent, (top cpuset
2044 * has online cpus, so can't be empty).
2045 */
2046 parent = cs->parent;
300ed6cb 2047 while (cpumask_empty(parent->cpus_allowed) ||
b4501295 2048 nodes_empty(parent->mems_allowed))
956db3ca 2049 parent = parent->parent;
956db3ca
CW
2050
2051 move_member_tasks_to_cpuset(cs, parent);
2052}
2053
80d1fa64
SB
2054/*
2055 * Helper function to traverse cpusets.
2056 * It can be used to walk the cpuset tree from top to bottom, completing
2057 * one layer before dropping down to the next (thus always processing a
2058 * node before any of its children).
2059 */
2060static struct cpuset *cpuset_next(struct list_head *queue)
2061{
2062 struct cpuset *cp;
2063 struct cpuset *child; /* scans child cpusets of cp */
2064 struct cgroup *cont;
2065
2066 if (list_empty(queue))
2067 return NULL;
2068
2069 cp = list_first_entry(queue, struct cpuset, stack_list);
2070 list_del(queue->next);
ae8086ce
TH
2071 rcu_read_lock();
2072 cpuset_for_each_child(child, cont, cp)
80d1fa64 2073 list_add_tail(&child->stack_list, queue);
ae8086ce 2074 rcu_read_unlock();
80d1fa64
SB
2075
2076 return cp;
2077}
2078
deb7aa30 2079/**
8d033948 2080 * cpuset_propagate_hotplug_workfn - propagate CPU/memory hotplug to a cpuset
deb7aa30 2081 * @cs: cpuset in interest
956db3ca 2082 *
deb7aa30
TH
2083 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2084 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2085 * all its tasks are moved to the nearest ancestor with both resources.
956db3ca 2086 */
8d033948 2087static void cpuset_propagate_hotplug_workfn(struct work_struct *work)
b1aac8bb 2088{
deb7aa30
TH
2089 static cpumask_t off_cpus;
2090 static nodemask_t off_mems, tmp_mems;
8d033948 2091 struct cpuset *cs = container_of(work, struct cpuset, hotplug_work);
7ddf96b0 2092
8d033948 2093 cgroup_lock();
7ddf96b0 2094
deb7aa30
TH
2095 cpumask_andnot(&off_cpus, cs->cpus_allowed, top_cpuset.cpus_allowed);
2096 nodes_andnot(off_mems, cs->mems_allowed, top_cpuset.mems_allowed);
7ddf96b0 2097
deb7aa30
TH
2098 /* remove offline cpus from @cs */
2099 if (!cpumask_empty(&off_cpus)) {
2100 mutex_lock(&callback_mutex);
2101 cpumask_andnot(cs->cpus_allowed, cs->cpus_allowed, &off_cpus);
2102 mutex_unlock(&callback_mutex);
2103 update_tasks_cpumask(cs, NULL);
2104 }
b4501295 2105
deb7aa30
TH
2106 /* remove offline mems from @cs */
2107 if (!nodes_empty(off_mems)) {
2108 tmp_mems = cs->mems_allowed;
2109 mutex_lock(&callback_mutex);
2110 nodes_andnot(cs->mems_allowed, cs->mems_allowed, off_mems);
2111 mutex_unlock(&callback_mutex);
2112 update_tasks_nodemask(cs, &tmp_mems, NULL);
b1aac8bb 2113 }
deb7aa30
TH
2114
2115 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
2116 remove_tasks_in_empty_cpuset(cs);
8d033948
TH
2117
2118 cgroup_unlock();
2119
2120 /* the following may free @cs, should be the last operation */
2121 css_put(&cs->css);
2122}
2123
2124/**
2125 * schedule_cpuset_propagate_hotplug - schedule hotplug propagation to a cpuset
2126 * @cs: cpuset of interest
2127 *
2128 * Schedule cpuset_propagate_hotplug_workfn() which will update CPU and
2129 * memory masks according to top_cpuset.
2130 */
2131static void schedule_cpuset_propagate_hotplug(struct cpuset *cs)
2132{
2133 /*
2134 * Pin @cs. The refcnt will be released when the work item
2135 * finishes executing.
2136 */
2137 if (!css_tryget(&cs->css))
2138 return;
2139
2140 /*
2141 * Queue @cs->hotplug_work. If already pending, lose the css ref.
2142 * cpuset_propagate_hotplug_wq is ordered and propagation will
2143 * happen in the order this function is called.
2144 */
2145 if (!queue_work(cpuset_propagate_hotplug_wq, &cs->hotplug_work))
2146 css_put(&cs->css);
b1aac8bb
PJ
2147}
2148
deb7aa30 2149/**
3a5a6d0c 2150 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
d35be8ba 2151 *
deb7aa30
TH
2152 * This function is called after either CPU or memory configuration has
2153 * changed and updates cpuset accordingly. The top_cpuset is always
2154 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2155 * order to make cpusets transparent (of no affect) on systems that are
2156 * actively using CPU hotplug but making no active use of cpusets.
cf417141 2157 *
deb7aa30
TH
2158 * Non-root cpusets are only affected by offlining. If any CPUs or memory
2159 * nodes have been taken down, cpuset_propagate_hotplug() is invoked on all
2160 * descendants.
7ddf96b0 2161 *
deb7aa30
TH
2162 * Note that CPU offlining during suspend is ignored. We don't modify
2163 * cpusets across suspend/resume cycles at all.
4c4d50f7 2164 */
3a5a6d0c 2165static void cpuset_hotplug_workfn(struct work_struct *work)
4c4d50f7 2166{
deb7aa30
TH
2167 static cpumask_t new_cpus, tmp_cpus;
2168 static nodemask_t new_mems, tmp_mems;
2169 bool cpus_updated, mems_updated;
2170 bool cpus_offlined, mems_offlined;
cf417141 2171
cf417141 2172 cgroup_lock();
7ddf96b0 2173
deb7aa30
TH
2174 /* fetch the available cpus/mems and find out which changed how */
2175 cpumask_copy(&new_cpus, cpu_active_mask);
2176 new_mems = node_states[N_MEMORY];
2177
2178 cpus_updated = !cpumask_equal(top_cpuset.cpus_allowed, &new_cpus);
2179 cpus_offlined = cpumask_andnot(&tmp_cpus, top_cpuset.cpus_allowed,
2180 &new_cpus);
2181
2182 mems_updated = !nodes_equal(top_cpuset.mems_allowed, new_mems);
2183 nodes_andnot(tmp_mems, top_cpuset.mems_allowed, new_mems);
2184 mems_offlined = !nodes_empty(tmp_mems);
2185
2186 /* synchronize cpus_allowed to cpu_active_mask */
2187 if (cpus_updated) {
2188 mutex_lock(&callback_mutex);
2189 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
2190 mutex_unlock(&callback_mutex);
2191 /* we don't mess with cpumasks of tasks in top_cpuset */
2192 }
2193
2194 /* synchronize mems_allowed to N_MEMORY */
2195 if (mems_updated) {
2196 tmp_mems = top_cpuset.mems_allowed;
2197 mutex_lock(&callback_mutex);
2198 top_cpuset.mems_allowed = new_mems;
2199 mutex_unlock(&callback_mutex);
2200 update_tasks_nodemask(&top_cpuset, &tmp_mems, NULL);
2201 }
2202
2203 /* if cpus or mems went down, we need to propagate to descendants */
2204 if (cpus_offlined || mems_offlined) {
2205 struct cpuset *cs;
2206 LIST_HEAD(queue);
2207
2208 list_add_tail(&top_cpuset.stack_list, &queue);
2209 while ((cs = cpuset_next(&queue)))
2210 if (cs != &top_cpuset)
8d033948 2211 schedule_cpuset_propagate_hotplug(cs);
deb7aa30 2212 }
7ddf96b0 2213
cf417141
MK
2214 cgroup_unlock();
2215
8d033948
TH
2216 /* wait for propagations to finish */
2217 flush_workqueue(cpuset_propagate_hotplug_wq);
2218
deb7aa30
TH
2219 /* rebuild sched domains if cpus_allowed has changed */
2220 if (cpus_updated) {
2221 struct sched_domain_attr *attr;
2222 cpumask_var_t *doms;
2223 int ndoms;
2224
2225 cgroup_lock();
2226 ndoms = generate_sched_domains(&doms, &attr);
2227 cgroup_unlock();
2228
2229 partition_sched_domains(ndoms, doms, attr);
2230 }
2231}
2232
2233void cpuset_update_active_cpus(bool cpu_online)
2234{
3a5a6d0c
TH
2235 /*
2236 * We're inside cpu hotplug critical region which usually nests
2237 * inside cgroup synchronization. Bounce actual hotplug processing
2238 * to a work item to avoid reverse locking order.
2239 *
2240 * We still need to do partition_sched_domains() synchronously;
2241 * otherwise, the scheduler will get confused and put tasks to the
2242 * dead CPU. Fall back to the default single domain.
2243 * cpuset_hotplug_workfn() will rebuild it as necessary.
2244 */
2245 partition_sched_domains(1, NULL, NULL);
2246 schedule_work(&cpuset_hotplug_work);
4c4d50f7 2247}
4c4d50f7 2248
b1aac8bb 2249#ifdef CONFIG_MEMORY_HOTPLUG
38837fc7 2250/*
38d7bee9
LJ
2251 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2252 * Call this routine anytime after node_states[N_MEMORY] changes.
a1cd2b13 2253 * See cpuset_update_active_cpus() for CPU hotplug handling.
38837fc7 2254 */
f481891f
MX
2255static int cpuset_track_online_nodes(struct notifier_block *self,
2256 unsigned long action, void *arg)
38837fc7 2257{
3a5a6d0c 2258 schedule_work(&cpuset_hotplug_work);
f481891f 2259 return NOTIFY_OK;
38837fc7
PJ
2260}
2261#endif
2262
1da177e4
LT
2263/**
2264 * cpuset_init_smp - initialize cpus_allowed
2265 *
2266 * Description: Finish top cpuset after cpu, node maps are initialized
2267 **/
2268
2269void __init cpuset_init_smp(void)
2270{
6ad4c188 2271 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
38d7bee9 2272 top_cpuset.mems_allowed = node_states[N_MEMORY];
4c4d50f7 2273
f481891f 2274 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
8d033948
TH
2275
2276 cpuset_propagate_hotplug_wq =
2277 alloc_ordered_workqueue("cpuset_hotplug", 0);
2278 BUG_ON(!cpuset_propagate_hotplug_wq);
1da177e4
LT
2279}
2280
2281/**
1da177e4
LT
2282 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2283 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
6af866af 2284 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
1da177e4 2285 *
300ed6cb 2286 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
1da177e4 2287 * attached to the specified @tsk. Guaranteed to return some non-empty
5f054e31 2288 * subset of cpu_online_mask, even if this means going outside the
1da177e4
LT
2289 * tasks cpuset.
2290 **/
2291
6af866af 2292void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
1da177e4 2293{
3d3f26a7 2294 mutex_lock(&callback_mutex);
909d75a3 2295 task_lock(tsk);
f9a86fcb 2296 guarantee_online_cpus(task_cs(tsk), pmask);
909d75a3 2297 task_unlock(tsk);
897f0b3c 2298 mutex_unlock(&callback_mutex);
1da177e4
LT
2299}
2300
2baab4e9 2301void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
9084bb82
ON
2302{
2303 const struct cpuset *cs;
9084bb82
ON
2304
2305 rcu_read_lock();
2306 cs = task_cs(tsk);
2307 if (cs)
1e1b6c51 2308 do_set_cpus_allowed(tsk, cs->cpus_allowed);
9084bb82
ON
2309 rcu_read_unlock();
2310
2311 /*
2312 * We own tsk->cpus_allowed, nobody can change it under us.
2313 *
2314 * But we used cs && cs->cpus_allowed lockless and thus can
2315 * race with cgroup_attach_task() or update_cpumask() and get
2316 * the wrong tsk->cpus_allowed. However, both cases imply the
2317 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2318 * which takes task_rq_lock().
2319 *
2320 * If we are called after it dropped the lock we must see all
2321 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2322 * set any mask even if it is not right from task_cs() pov,
2323 * the pending set_cpus_allowed_ptr() will fix things.
2baab4e9
PZ
2324 *
2325 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2326 * if required.
9084bb82 2327 */
9084bb82
ON
2328}
2329
1da177e4
LT
2330void cpuset_init_current_mems_allowed(void)
2331{
f9a86fcb 2332 nodes_setall(current->mems_allowed);
1da177e4
LT
2333}
2334
909d75a3
PJ
2335/**
2336 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2337 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2338 *
2339 * Description: Returns the nodemask_t mems_allowed of the cpuset
2340 * attached to the specified @tsk. Guaranteed to return some non-empty
38d7bee9 2341 * subset of node_states[N_MEMORY], even if this means going outside the
909d75a3
PJ
2342 * tasks cpuset.
2343 **/
2344
2345nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2346{
2347 nodemask_t mask;
2348
3d3f26a7 2349 mutex_lock(&callback_mutex);
909d75a3 2350 task_lock(tsk);
8793d854 2351 guarantee_online_mems(task_cs(tsk), &mask);
909d75a3 2352 task_unlock(tsk);
3d3f26a7 2353 mutex_unlock(&callback_mutex);
909d75a3
PJ
2354
2355 return mask;
2356}
2357
d9fd8a6d 2358/**
19770b32
MG
2359 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2360 * @nodemask: the nodemask to be checked
d9fd8a6d 2361 *
19770b32 2362 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
1da177e4 2363 */
19770b32 2364int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
1da177e4 2365{
19770b32 2366 return nodes_intersects(*nodemask, current->mems_allowed);
1da177e4
LT
2367}
2368
9bf2229f 2369/*
78608366
PM
2370 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2371 * mem_hardwall ancestor to the specified cpuset. Call holding
2372 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2373 * (an unusual configuration), then returns the root cpuset.
9bf2229f 2374 */
78608366 2375static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
9bf2229f 2376{
78608366 2377 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
9bf2229f
PJ
2378 cs = cs->parent;
2379 return cs;
2380}
2381
d9fd8a6d 2382/**
a1bc5a4e
DR
2383 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2384 * @node: is this an allowed node?
02a0e53d 2385 * @gfp_mask: memory allocation flags
d9fd8a6d 2386 *
a1bc5a4e
DR
2387 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2388 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2389 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2390 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2391 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2392 * flag, yes.
9bf2229f
PJ
2393 * Otherwise, no.
2394 *
a1bc5a4e
DR
2395 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2396 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2397 * might sleep, and might allow a node from an enclosing cpuset.
02a0e53d 2398 *
a1bc5a4e
DR
2399 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2400 * cpusets, and never sleeps.
02a0e53d
PJ
2401 *
2402 * The __GFP_THISNODE placement logic is really handled elsewhere,
2403 * by forcibly using a zonelist starting at a specified node, and by
2404 * (in get_page_from_freelist()) refusing to consider the zones for
2405 * any node on the zonelist except the first. By the time any such
2406 * calls get to this routine, we should just shut up and say 'yes'.
2407 *
9bf2229f 2408 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
c596d9f3
DR
2409 * and do not allow allocations outside the current tasks cpuset
2410 * unless the task has been OOM killed as is marked TIF_MEMDIE.
9bf2229f 2411 * GFP_KERNEL allocations are not so marked, so can escape to the
78608366 2412 * nearest enclosing hardwalled ancestor cpuset.
9bf2229f 2413 *
02a0e53d
PJ
2414 * Scanning up parent cpusets requires callback_mutex. The
2415 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2416 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2417 * current tasks mems_allowed came up empty on the first pass over
2418 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2419 * cpuset are short of memory, might require taking the callback_mutex
2420 * mutex.
9bf2229f 2421 *
36be57ff 2422 * The first call here from mm/page_alloc:get_page_from_freelist()
02a0e53d
PJ
2423 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2424 * so no allocation on a node outside the cpuset is allowed (unless
2425 * in interrupt, of course).
36be57ff
PJ
2426 *
2427 * The second pass through get_page_from_freelist() doesn't even call
2428 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2429 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2430 * in alloc_flags. That logic and the checks below have the combined
2431 * affect that:
9bf2229f
PJ
2432 * in_interrupt - any node ok (current task context irrelevant)
2433 * GFP_ATOMIC - any node ok
c596d9f3 2434 * TIF_MEMDIE - any node ok
78608366 2435 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
9bf2229f 2436 * GFP_USER - only nodes in current tasks mems allowed ok.
36be57ff
PJ
2437 *
2438 * Rule:
a1bc5a4e 2439 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
36be57ff
PJ
2440 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2441 * the code that might scan up ancestor cpusets and sleep.
02a0e53d 2442 */
a1bc5a4e 2443int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
1da177e4 2444{
9bf2229f 2445 const struct cpuset *cs; /* current cpuset ancestors */
29afd49b 2446 int allowed; /* is allocation in zone z allowed? */
9bf2229f 2447
9b819d20 2448 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
9bf2229f 2449 return 1;
92d1dbd2 2450 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
9bf2229f
PJ
2451 if (node_isset(node, current->mems_allowed))
2452 return 1;
c596d9f3
DR
2453 /*
2454 * Allow tasks that have access to memory reserves because they have
2455 * been OOM killed to get memory anywhere.
2456 */
2457 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2458 return 1;
9bf2229f
PJ
2459 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2460 return 0;
2461
5563e770
BP
2462 if (current->flags & PF_EXITING) /* Let dying task have memory */
2463 return 1;
2464
9bf2229f 2465 /* Not hardwall and node outside mems_allowed: scan up cpusets */
3d3f26a7 2466 mutex_lock(&callback_mutex);
053199ed 2467
053199ed 2468 task_lock(current);
78608366 2469 cs = nearest_hardwall_ancestor(task_cs(current));
053199ed
PJ
2470 task_unlock(current);
2471
9bf2229f 2472 allowed = node_isset(node, cs->mems_allowed);
3d3f26a7 2473 mutex_unlock(&callback_mutex);
9bf2229f 2474 return allowed;
1da177e4
LT
2475}
2476
02a0e53d 2477/*
a1bc5a4e
DR
2478 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2479 * @node: is this an allowed node?
02a0e53d
PJ
2480 * @gfp_mask: memory allocation flags
2481 *
a1bc5a4e
DR
2482 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2483 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2484 * yes. If the task has been OOM killed and has access to memory reserves as
2485 * specified by the TIF_MEMDIE flag, yes.
2486 * Otherwise, no.
02a0e53d
PJ
2487 *
2488 * The __GFP_THISNODE placement logic is really handled elsewhere,
2489 * by forcibly using a zonelist starting at a specified node, and by
2490 * (in get_page_from_freelist()) refusing to consider the zones for
2491 * any node on the zonelist except the first. By the time any such
2492 * calls get to this routine, we should just shut up and say 'yes'.
2493 *
a1bc5a4e
DR
2494 * Unlike the cpuset_node_allowed_softwall() variant, above,
2495 * this variant requires that the node be in the current task's
02a0e53d
PJ
2496 * mems_allowed or that we're in interrupt. It does not scan up the
2497 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2498 * It never sleeps.
2499 */
a1bc5a4e 2500int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
02a0e53d 2501{
02a0e53d
PJ
2502 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2503 return 1;
02a0e53d
PJ
2504 if (node_isset(node, current->mems_allowed))
2505 return 1;
dedf8b79
DW
2506 /*
2507 * Allow tasks that have access to memory reserves because they have
2508 * been OOM killed to get memory anywhere.
2509 */
2510 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2511 return 1;
02a0e53d
PJ
2512 return 0;
2513}
2514
825a46af 2515/**
6adef3eb
JS
2516 * cpuset_mem_spread_node() - On which node to begin search for a file page
2517 * cpuset_slab_spread_node() - On which node to begin search for a slab page
825a46af
PJ
2518 *
2519 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2520 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2521 * and if the memory allocation used cpuset_mem_spread_node()
2522 * to determine on which node to start looking, as it will for
2523 * certain page cache or slab cache pages such as used for file
2524 * system buffers and inode caches, then instead of starting on the
2525 * local node to look for a free page, rather spread the starting
2526 * node around the tasks mems_allowed nodes.
2527 *
2528 * We don't have to worry about the returned node being offline
2529 * because "it can't happen", and even if it did, it would be ok.
2530 *
2531 * The routines calling guarantee_online_mems() are careful to
2532 * only set nodes in task->mems_allowed that are online. So it
2533 * should not be possible for the following code to return an
2534 * offline node. But if it did, that would be ok, as this routine
2535 * is not returning the node where the allocation must be, only
2536 * the node where the search should start. The zonelist passed to
2537 * __alloc_pages() will include all nodes. If the slab allocator
2538 * is passed an offline node, it will fall back to the local node.
2539 * See kmem_cache_alloc_node().
2540 */
2541
6adef3eb 2542static int cpuset_spread_node(int *rotor)
825a46af
PJ
2543{
2544 int node;
2545
6adef3eb 2546 node = next_node(*rotor, current->mems_allowed);
825a46af
PJ
2547 if (node == MAX_NUMNODES)
2548 node = first_node(current->mems_allowed);
6adef3eb 2549 *rotor = node;
825a46af
PJ
2550 return node;
2551}
6adef3eb
JS
2552
2553int cpuset_mem_spread_node(void)
2554{
778d3b0f
MH
2555 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2556 current->cpuset_mem_spread_rotor =
2557 node_random(&current->mems_allowed);
2558
6adef3eb
JS
2559 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2560}
2561
2562int cpuset_slab_spread_node(void)
2563{
778d3b0f
MH
2564 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2565 current->cpuset_slab_spread_rotor =
2566 node_random(&current->mems_allowed);
2567
6adef3eb
JS
2568 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2569}
2570
825a46af
PJ
2571EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2572
ef08e3b4 2573/**
bbe373f2
DR
2574 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2575 * @tsk1: pointer to task_struct of some task.
2576 * @tsk2: pointer to task_struct of some other task.
2577 *
2578 * Description: Return true if @tsk1's mems_allowed intersects the
2579 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2580 * one of the task's memory usage might impact the memory available
2581 * to the other.
ef08e3b4
PJ
2582 **/
2583
bbe373f2
DR
2584int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2585 const struct task_struct *tsk2)
ef08e3b4 2586{
bbe373f2 2587 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
ef08e3b4
PJ
2588}
2589
75aa1994
DR
2590/**
2591 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2592 * @task: pointer to task_struct of some task.
2593 *
2594 * Description: Prints @task's name, cpuset name, and cached copy of its
2595 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2596 * dereferencing task_cs(task).
2597 */
2598void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2599{
2600 struct dentry *dentry;
2601
2602 dentry = task_cs(tsk)->css.cgroup->dentry;
2603 spin_lock(&cpuset_buffer_lock);
2604 snprintf(cpuset_name, CPUSET_NAME_LEN,
2605 dentry ? (const char *)dentry->d_name.name : "/");
2606 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2607 tsk->mems_allowed);
2608 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2609 tsk->comm, cpuset_name, cpuset_nodelist);
2610 spin_unlock(&cpuset_buffer_lock);
2611}
2612
3e0d98b9
PJ
2613/*
2614 * Collection of memory_pressure is suppressed unless
2615 * this flag is enabled by writing "1" to the special
2616 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2617 */
2618
c5b2aff8 2619int cpuset_memory_pressure_enabled __read_mostly;
3e0d98b9
PJ
2620
2621/**
2622 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2623 *
2624 * Keep a running average of the rate of synchronous (direct)
2625 * page reclaim efforts initiated by tasks in each cpuset.
2626 *
2627 * This represents the rate at which some task in the cpuset
2628 * ran low on memory on all nodes it was allowed to use, and
2629 * had to enter the kernels page reclaim code in an effort to
2630 * create more free memory by tossing clean pages or swapping
2631 * or writing dirty pages.
2632 *
2633 * Display to user space in the per-cpuset read-only file
2634 * "memory_pressure". Value displayed is an integer
2635 * representing the recent rate of entry into the synchronous
2636 * (direct) page reclaim by any task attached to the cpuset.
2637 **/
2638
2639void __cpuset_memory_pressure_bump(void)
2640{
3e0d98b9 2641 task_lock(current);
8793d854 2642 fmeter_markevent(&task_cs(current)->fmeter);
3e0d98b9
PJ
2643 task_unlock(current);
2644}
2645
8793d854 2646#ifdef CONFIG_PROC_PID_CPUSET
1da177e4
LT
2647/*
2648 * proc_cpuset_show()
2649 * - Print tasks cpuset path into seq_file.
2650 * - Used for /proc/<pid>/cpuset.
053199ed
PJ
2651 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2652 * doesn't really matter if tsk->cpuset changes after we read it,
c8d9c90c 2653 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
2df167a3 2654 * anyway.
1da177e4 2655 */
029190c5 2656static int proc_cpuset_show(struct seq_file *m, void *unused_v)
1da177e4 2657{
13b41b09 2658 struct pid *pid;
1da177e4
LT
2659 struct task_struct *tsk;
2660 char *buf;
8793d854 2661 struct cgroup_subsys_state *css;
99f89551 2662 int retval;
1da177e4 2663
99f89551 2664 retval = -ENOMEM;
1da177e4
LT
2665 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2666 if (!buf)
99f89551
EB
2667 goto out;
2668
2669 retval = -ESRCH;
13b41b09
EB
2670 pid = m->private;
2671 tsk = get_pid_task(pid, PIDTYPE_PID);
99f89551
EB
2672 if (!tsk)
2673 goto out_free;
1da177e4 2674
99f89551 2675 retval = -EINVAL;
8793d854
PM
2676 cgroup_lock();
2677 css = task_subsys_state(tsk, cpuset_subsys_id);
2678 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
1da177e4 2679 if (retval < 0)
99f89551 2680 goto out_unlock;
1da177e4
LT
2681 seq_puts(m, buf);
2682 seq_putc(m, '\n');
99f89551 2683out_unlock:
8793d854 2684 cgroup_unlock();
99f89551
EB
2685 put_task_struct(tsk);
2686out_free:
1da177e4 2687 kfree(buf);
99f89551 2688out:
1da177e4
LT
2689 return retval;
2690}
2691
2692static int cpuset_open(struct inode *inode, struct file *file)
2693{
13b41b09
EB
2694 struct pid *pid = PROC_I(inode)->pid;
2695 return single_open(file, proc_cpuset_show, pid);
1da177e4
LT
2696}
2697
9a32144e 2698const struct file_operations proc_cpuset_operations = {
1da177e4
LT
2699 .open = cpuset_open,
2700 .read = seq_read,
2701 .llseek = seq_lseek,
2702 .release = single_release,
2703};
8793d854 2704#endif /* CONFIG_PROC_PID_CPUSET */
1da177e4 2705
d01d4827 2706/* Display task mems_allowed in /proc/<pid>/status file. */
df5f8314
EB
2707void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
2708{
df5f8314 2709 seq_printf(m, "Mems_allowed:\t");
30e8e136 2710 seq_nodemask(m, &task->mems_allowed);
df5f8314 2711 seq_printf(m, "\n");
39106dcf 2712 seq_printf(m, "Mems_allowed_list:\t");
30e8e136 2713 seq_nodemask_list(m, &task->mems_allowed);
39106dcf 2714 seq_printf(m, "\n");
1da177e4 2715}