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