cpusets: update tasks' page/slab spread flags in time
[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>
1da177e4
LT
40#include <linux/module.h>
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>
58#include <asm/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
LT
99
100 /*
101 * Copy of global cpuset_mems_generation as of the most
102 * recent time this cpuset changed its mems_allowed.
103 */
3e0d98b9
PJ
104 int mems_generation;
105
106 struct fmeter fmeter; /* memory_pressure filter */
029190c5
PJ
107
108 /* partition number for rebuild_sched_domains() */
109 int pn;
956db3ca 110
1d3504fc
HS
111 /* for custom sched domain */
112 int relax_domain_level;
113
956db3ca
CW
114 /* used for walking a cpuset heirarchy */
115 struct list_head stack_list;
1da177e4
LT
116};
117
8793d854
PM
118/* Retrieve the cpuset for a cgroup */
119static inline struct cpuset *cgroup_cs(struct cgroup *cont)
120{
121 return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
122 struct cpuset, css);
123}
124
125/* Retrieve the cpuset for a task */
126static inline struct cpuset *task_cs(struct task_struct *task)
127{
128 return container_of(task_subsys_state(task, cpuset_subsys_id),
129 struct cpuset, css);
130}
8793d854 131
1da177e4
LT
132/* bits in struct cpuset flags field */
133typedef enum {
134 CS_CPU_EXCLUSIVE,
135 CS_MEM_EXCLUSIVE,
78608366 136 CS_MEM_HARDWALL,
45b07ef3 137 CS_MEMORY_MIGRATE,
029190c5 138 CS_SCHED_LOAD_BALANCE,
825a46af
PJ
139 CS_SPREAD_PAGE,
140 CS_SPREAD_SLAB,
1da177e4
LT
141} cpuset_flagbits_t;
142
143/* convenient tests for these bits */
144static inline int is_cpu_exclusive(const struct cpuset *cs)
145{
7b5b9ef0 146 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
1da177e4
LT
147}
148
149static inline int is_mem_exclusive(const struct cpuset *cs)
150{
7b5b9ef0 151 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
1da177e4
LT
152}
153
78608366
PM
154static inline int is_mem_hardwall(const struct cpuset *cs)
155{
156 return test_bit(CS_MEM_HARDWALL, &cs->flags);
157}
158
029190c5
PJ
159static inline int is_sched_load_balance(const struct cpuset *cs)
160{
161 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
162}
163
45b07ef3
PJ
164static inline int is_memory_migrate(const struct cpuset *cs)
165{
7b5b9ef0 166 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
45b07ef3
PJ
167}
168
825a46af
PJ
169static inline int is_spread_page(const struct cpuset *cs)
170{
171 return test_bit(CS_SPREAD_PAGE, &cs->flags);
172}
173
174static inline int is_spread_slab(const struct cpuset *cs)
175{
176 return test_bit(CS_SPREAD_SLAB, &cs->flags);
177}
178
1da177e4 179/*
151a4420 180 * Increment this integer everytime any cpuset changes its
1da177e4
LT
181 * mems_allowed value. Users of cpusets can track this generation
182 * number, and avoid having to lock and reload mems_allowed unless
183 * the cpuset they're using changes generation.
184 *
2df167a3 185 * A single, global generation is needed because cpuset_attach_task() could
1da177e4
LT
186 * reattach a task to a different cpuset, which must not have its
187 * generation numbers aliased with those of that tasks previous cpuset.
188 *
189 * Generations are needed for mems_allowed because one task cannot
2df167a3 190 * modify another's memory placement. So we must enable every task,
1da177e4
LT
191 * on every visit to __alloc_pages(), to efficiently check whether
192 * its current->cpuset->mems_allowed has changed, requiring an update
193 * of its current->mems_allowed.
151a4420 194 *
2df167a3 195 * Since writes to cpuset_mems_generation are guarded by the cgroup lock
151a4420 196 * there is no need to mark it atomic.
1da177e4 197 */
151a4420 198static int cpuset_mems_generation;
1da177e4
LT
199
200static struct cpuset top_cpuset = {
201 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
1da177e4
LT
202};
203
1da177e4 204/*
2df167a3
PM
205 * There are two global mutexes guarding cpuset structures. The first
206 * is the main control groups cgroup_mutex, accessed via
207 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
208 * callback_mutex, below. They can nest. It is ok to first take
209 * cgroup_mutex, then nest callback_mutex. We also require taking
210 * task_lock() when dereferencing a task's cpuset pointer. See "The
211 * task_lock() exception", at the end of this comment.
053199ed 212 *
3d3f26a7 213 * A task must hold both mutexes to modify cpusets. If a task
2df167a3 214 * holds cgroup_mutex, then it blocks others wanting that mutex,
3d3f26a7 215 * ensuring that it is the only task able to also acquire callback_mutex
053199ed
PJ
216 * and be able to modify cpusets. It can perform various checks on
217 * the cpuset structure first, knowing nothing will change. It can
2df167a3 218 * also allocate memory while just holding cgroup_mutex. While it is
053199ed 219 * performing these checks, various callback routines can briefly
3d3f26a7
IM
220 * acquire callback_mutex to query cpusets. Once it is ready to make
221 * the changes, it takes callback_mutex, blocking everyone else.
053199ed
PJ
222 *
223 * Calls to the kernel memory allocator can not be made while holding
3d3f26a7 224 * callback_mutex, as that would risk double tripping on callback_mutex
053199ed
PJ
225 * from one of the callbacks into the cpuset code from within
226 * __alloc_pages().
227 *
3d3f26a7 228 * If a task is only holding callback_mutex, then it has read-only
053199ed
PJ
229 * access to cpusets.
230 *
231 * The task_struct fields mems_allowed and mems_generation may only
232 * be accessed in the context of that task, so require no locks.
233 *
3d3f26a7 234 * The cpuset_common_file_read() handlers only hold callback_mutex across
053199ed
PJ
235 * small pieces of code, such as when reading out possibly multi-word
236 * cpumasks and nodemasks.
237 *
2df167a3
PM
238 * Accessing a task's cpuset should be done in accordance with the
239 * guidelines for accessing subsystem state in kernel/cgroup.c
1da177e4
LT
240 */
241
3d3f26a7 242static DEFINE_MUTEX(callback_mutex);
4247bdc6 243
75aa1994
DR
244/*
245 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
246 * buffers. They are statically allocated to prevent using excess stack
247 * when calling cpuset_print_task_mems_allowed().
248 */
249#define CPUSET_NAME_LEN (128)
250#define CPUSET_NODELIST_LEN (256)
251static char cpuset_name[CPUSET_NAME_LEN];
252static char cpuset_nodelist[CPUSET_NODELIST_LEN];
253static DEFINE_SPINLOCK(cpuset_buffer_lock);
254
cf417141
MK
255/*
256 * This is ugly, but preserves the userspace API for existing cpuset
8793d854 257 * users. If someone tries to mount the "cpuset" filesystem, we
cf417141
MK
258 * silently switch it to mount "cgroup" instead
259 */
454e2398
DH
260static int cpuset_get_sb(struct file_system_type *fs_type,
261 int flags, const char *unused_dev_name,
262 void *data, struct vfsmount *mnt)
1da177e4 263{
8793d854
PM
264 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
265 int ret = -ENODEV;
266 if (cgroup_fs) {
267 char mountopts[] =
268 "cpuset,noprefix,"
269 "release_agent=/sbin/cpuset_release_agent";
270 ret = cgroup_fs->get_sb(cgroup_fs, flags,
271 unused_dev_name, mountopts, mnt);
272 put_filesystem(cgroup_fs);
273 }
274 return ret;
1da177e4
LT
275}
276
277static struct file_system_type cpuset_fs_type = {
278 .name = "cpuset",
279 .get_sb = cpuset_get_sb,
1da177e4
LT
280};
281
1da177e4 282/*
300ed6cb 283 * Return in pmask the portion of a cpusets's cpus_allowed that
1da177e4
LT
284 * are online. If none are online, walk up the cpuset hierarchy
285 * until we find one that does have some online cpus. If we get
286 * all the way to the top and still haven't found any online cpus,
287 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
288 * task, return cpu_online_map.
289 *
290 * One way or another, we guarantee to return some non-empty subset
291 * of cpu_online_map.
292 *
3d3f26a7 293 * Call with callback_mutex held.
1da177e4
LT
294 */
295
6af866af
LZ
296static void guarantee_online_cpus(const struct cpuset *cs,
297 struct cpumask *pmask)
1da177e4 298{
300ed6cb 299 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
1da177e4
LT
300 cs = cs->parent;
301 if (cs)
300ed6cb 302 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
1da177e4 303 else
300ed6cb
LZ
304 cpumask_copy(pmask, cpu_online_mask);
305 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
1da177e4
LT
306}
307
308/*
309 * Return in *pmask the portion of a cpusets's mems_allowed that
0e1e7c7a
CL
310 * are online, with memory. If none are online with memory, walk
311 * up the cpuset hierarchy until we find one that does have some
312 * online mems. If we get all the way to the top and still haven't
313 * found any online mems, return node_states[N_HIGH_MEMORY].
1da177e4
LT
314 *
315 * One way or another, we guarantee to return some non-empty subset
0e1e7c7a 316 * of node_states[N_HIGH_MEMORY].
1da177e4 317 *
3d3f26a7 318 * Call with callback_mutex held.
1da177e4
LT
319 */
320
321static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
322{
0e1e7c7a
CL
323 while (cs && !nodes_intersects(cs->mems_allowed,
324 node_states[N_HIGH_MEMORY]))
1da177e4
LT
325 cs = cs->parent;
326 if (cs)
0e1e7c7a
CL
327 nodes_and(*pmask, cs->mems_allowed,
328 node_states[N_HIGH_MEMORY]);
1da177e4 329 else
0e1e7c7a
CL
330 *pmask = node_states[N_HIGH_MEMORY];
331 BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
1da177e4
LT
332}
333
f3b39d47
MX
334/*
335 * update task's spread flag if cpuset's page/slab spread flag is set
336 *
337 * Called with callback_mutex/cgroup_mutex held
338 */
339static void cpuset_update_task_spread_flag(struct cpuset *cs,
340 struct task_struct *tsk)
341{
342 if (is_spread_page(cs))
343 tsk->flags |= PF_SPREAD_PAGE;
344 else
345 tsk->flags &= ~PF_SPREAD_PAGE;
346 if (is_spread_slab(cs))
347 tsk->flags |= PF_SPREAD_SLAB;
348 else
349 tsk->flags &= ~PF_SPREAD_SLAB;
350}
351
cf2a473c
PJ
352/**
353 * cpuset_update_task_memory_state - update task memory placement
354 *
355 * If the current tasks cpusets mems_allowed changed behind our
356 * backs, update current->mems_allowed, mems_generation and task NUMA
357 * mempolicy to the new value.
053199ed 358 *
cf2a473c
PJ
359 * Task mempolicy is updated by rebinding it relative to the
360 * current->cpuset if a task has its memory placement changed.
361 * Do not call this routine if in_interrupt().
362 *
4a01c8d5 363 * Call without callback_mutex or task_lock() held. May be
2df167a3
PM
364 * called with or without cgroup_mutex held. Thanks in part to
365 * 'the_top_cpuset_hack', the task's cpuset pointer will never
41f7f60d
DR
366 * be NULL. This routine also might acquire callback_mutex during
367 * call.
053199ed 368 *
6b9c2603
PJ
369 * Reading current->cpuset->mems_generation doesn't need task_lock
370 * to guard the current->cpuset derefence, because it is guarded
2df167a3 371 * from concurrent freeing of current->cpuset using RCU.
6b9c2603
PJ
372 *
373 * The rcu_dereference() is technically probably not needed,
374 * as I don't actually mind if I see a new cpuset pointer but
375 * an old value of mems_generation. However this really only
376 * matters on alpha systems using cpusets heavily. If I dropped
377 * that rcu_dereference(), it would save them a memory barrier.
378 * For all other arch's, rcu_dereference is a no-op anyway, and for
379 * alpha systems not using cpusets, another planned optimization,
380 * avoiding the rcu critical section for tasks in the root cpuset
381 * which is statically allocated, so can't vanish, will make this
382 * irrelevant. Better to use RCU as intended, than to engage in
383 * some cute trick to save a memory barrier that is impossible to
384 * test, for alpha systems using cpusets heavily, which might not
385 * even exist.
053199ed
PJ
386 *
387 * This routine is needed to update the per-task mems_allowed data,
388 * within the tasks context, when it is trying to allocate memory
389 * (in various mm/mempolicy.c routines) and notices that some other
390 * task has been modifying its cpuset.
1da177e4
LT
391 */
392
fe85a998 393void cpuset_update_task_memory_state(void)
1da177e4 394{
053199ed 395 int my_cpusets_mem_gen;
cf2a473c 396 struct task_struct *tsk = current;
6b9c2603 397 struct cpuset *cs;
053199ed 398
13337714
LJ
399 rcu_read_lock();
400 my_cpusets_mem_gen = task_cs(tsk)->mems_generation;
401 rcu_read_unlock();
1da177e4 402
cf2a473c 403 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
3d3f26a7 404 mutex_lock(&callback_mutex);
cf2a473c 405 task_lock(tsk);
8793d854 406 cs = task_cs(tsk); /* Maybe changed when task not locked */
cf2a473c
PJ
407 guarantee_online_mems(cs, &tsk->mems_allowed);
408 tsk->cpuset_mems_generation = cs->mems_generation;
409 task_unlock(tsk);
3d3f26a7 410 mutex_unlock(&callback_mutex);
74cb2155 411 mpol_rebind_task(tsk, &tsk->mems_allowed);
1da177e4
LT
412 }
413}
414
415/*
416 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
417 *
418 * One cpuset is a subset of another if all its allowed CPUs and
419 * Memory Nodes are a subset of the other, and its exclusive flags
2df167a3 420 * are only set if the other's are set. Call holding cgroup_mutex.
1da177e4
LT
421 */
422
423static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
424{
300ed6cb 425 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
1da177e4
LT
426 nodes_subset(p->mems_allowed, q->mems_allowed) &&
427 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
428 is_mem_exclusive(p) <= is_mem_exclusive(q);
429}
430
645fcc9d
LZ
431/**
432 * alloc_trial_cpuset - allocate a trial cpuset
433 * @cs: the cpuset that the trial cpuset duplicates
434 */
435static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
436{
300ed6cb
LZ
437 struct cpuset *trial;
438
439 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
440 if (!trial)
441 return NULL;
442
443 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
444 kfree(trial);
445 return NULL;
446 }
447 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
448
449 return trial;
645fcc9d
LZ
450}
451
452/**
453 * free_trial_cpuset - free the trial cpuset
454 * @trial: the trial cpuset to be freed
455 */
456static void free_trial_cpuset(struct cpuset *trial)
457{
300ed6cb 458 free_cpumask_var(trial->cpus_allowed);
645fcc9d
LZ
459 kfree(trial);
460}
461
1da177e4
LT
462/*
463 * validate_change() - Used to validate that any proposed cpuset change
464 * follows the structural rules for cpusets.
465 *
466 * If we replaced the flag and mask values of the current cpuset
467 * (cur) with those values in the trial cpuset (trial), would
468 * our various subset and exclusive rules still be valid? Presumes
2df167a3 469 * cgroup_mutex held.
1da177e4
LT
470 *
471 * 'cur' is the address of an actual, in-use cpuset. Operations
472 * such as list traversal that depend on the actual address of the
473 * cpuset in the list must use cur below, not trial.
474 *
475 * 'trial' is the address of bulk structure copy of cur, with
476 * perhaps one or more of the fields cpus_allowed, mems_allowed,
477 * or flags changed to new, trial values.
478 *
479 * Return 0 if valid, -errno if not.
480 */
481
482static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
483{
8793d854 484 struct cgroup *cont;
1da177e4
LT
485 struct cpuset *c, *par;
486
487 /* Each of our child cpusets must be a subset of us */
8793d854
PM
488 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
489 if (!is_cpuset_subset(cgroup_cs(cont), trial))
1da177e4
LT
490 return -EBUSY;
491 }
492
493 /* Remaining checks don't apply to root cpuset */
69604067 494 if (cur == &top_cpuset)
1da177e4
LT
495 return 0;
496
69604067
PJ
497 par = cur->parent;
498
1da177e4
LT
499 /* We must be a subset of our parent cpuset */
500 if (!is_cpuset_subset(trial, par))
501 return -EACCES;
502
2df167a3
PM
503 /*
504 * If either I or some sibling (!= me) is exclusive, we can't
505 * overlap
506 */
8793d854
PM
507 list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
508 c = cgroup_cs(cont);
1da177e4
LT
509 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
510 c != cur &&
300ed6cb 511 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
1da177e4
LT
512 return -EINVAL;
513 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
514 c != cur &&
515 nodes_intersects(trial->mems_allowed, c->mems_allowed))
516 return -EINVAL;
517 }
518
020958b6
PJ
519 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
520 if (cgroup_task_count(cur->css.cgroup)) {
300ed6cb 521 if (cpumask_empty(trial->cpus_allowed) ||
020958b6
PJ
522 nodes_empty(trial->mems_allowed)) {
523 return -ENOSPC;
524 }
525 }
526
1da177e4
LT
527 return 0;
528}
529
db7f47cf 530#ifdef CONFIG_SMP
029190c5 531/*
cf417141 532 * Helper routine for generate_sched_domains().
029190c5
PJ
533 * Do cpusets a, b have overlapping cpus_allowed masks?
534 */
029190c5
PJ
535static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
536{
300ed6cb 537 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
029190c5
PJ
538}
539
1d3504fc
HS
540static void
541update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
542{
1d3504fc
HS
543 if (dattr->relax_domain_level < c->relax_domain_level)
544 dattr->relax_domain_level = c->relax_domain_level;
545 return;
546}
547
f5393693
LJ
548static void
549update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
550{
551 LIST_HEAD(q);
552
553 list_add(&c->stack_list, &q);
554 while (!list_empty(&q)) {
555 struct cpuset *cp;
556 struct cgroup *cont;
557 struct cpuset *child;
558
559 cp = list_first_entry(&q, struct cpuset, stack_list);
560 list_del(q.next);
561
300ed6cb 562 if (cpumask_empty(cp->cpus_allowed))
f5393693
LJ
563 continue;
564
565 if (is_sched_load_balance(cp))
566 update_domain_attr(dattr, cp);
567
568 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
569 child = cgroup_cs(cont);
570 list_add_tail(&child->stack_list, &q);
571 }
572 }
573}
574
029190c5 575/*
cf417141
MK
576 * generate_sched_domains()
577 *
578 * This function builds a partial partition of the systems CPUs
579 * A 'partial partition' is a set of non-overlapping subsets whose
580 * union is a subset of that set.
581 * The output of this function needs to be passed to kernel/sched.c
582 * partition_sched_domains() routine, which will rebuild the scheduler's
583 * load balancing domains (sched domains) as specified by that partial
584 * partition.
029190c5 585 *
45ce80fb 586 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
029190c5
PJ
587 * for a background explanation of this.
588 *
589 * Does not return errors, on the theory that the callers of this
590 * routine would rather not worry about failures to rebuild sched
591 * domains when operating in the severe memory shortage situations
592 * that could cause allocation failures below.
593 *
cf417141 594 * Must be called with cgroup_lock held.
029190c5
PJ
595 *
596 * The three key local variables below are:
aeed6824 597 * q - a linked-list queue of cpuset pointers, used to implement a
029190c5
PJ
598 * top-down scan of all cpusets. This scan loads a pointer
599 * to each cpuset marked is_sched_load_balance into the
600 * array 'csa'. For our purposes, rebuilding the schedulers
601 * sched domains, we can ignore !is_sched_load_balance cpusets.
602 * csa - (for CpuSet Array) Array of pointers to all the cpusets
603 * that need to be load balanced, for convenient iterative
604 * access by the subsequent code that finds the best partition,
605 * i.e the set of domains (subsets) of CPUs such that the
606 * cpus_allowed of every cpuset marked is_sched_load_balance
607 * is a subset of one of these domains, while there are as
608 * many such domains as possible, each as small as possible.
609 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
610 * the kernel/sched.c routine partition_sched_domains() in a
611 * convenient format, that can be easily compared to the prior
612 * value to determine what partition elements (sched domains)
613 * were changed (added or removed.)
614 *
615 * Finding the best partition (set of domains):
616 * The triple nested loops below over i, j, k scan over the
617 * load balanced cpusets (using the array of cpuset pointers in
618 * csa[]) looking for pairs of cpusets that have overlapping
619 * cpus_allowed, but which don't have the same 'pn' partition
620 * number and gives them in the same partition number. It keeps
621 * looping on the 'restart' label until it can no longer find
622 * any such pairs.
623 *
624 * The union of the cpus_allowed masks from the set of
625 * all cpusets having the same 'pn' value then form the one
626 * element of the partition (one sched domain) to be passed to
627 * partition_sched_domains().
628 */
6af866af
LZ
629/* FIXME: see the FIXME in partition_sched_domains() */
630static int generate_sched_domains(struct cpumask **domains,
cf417141 631 struct sched_domain_attr **attributes)
029190c5 632{
cf417141 633 LIST_HEAD(q); /* queue of cpusets to be scanned */
029190c5
PJ
634 struct cpuset *cp; /* scans q */
635 struct cpuset **csa; /* array of all cpuset ptrs */
636 int csn; /* how many cpuset ptrs in csa so far */
637 int i, j, k; /* indices for partition finding loops */
6af866af 638 struct cpumask *doms; /* resulting partition; i.e. sched domains */
1d3504fc 639 struct sched_domain_attr *dattr; /* attributes for custom domains */
1583715d 640 int ndoms = 0; /* number of sched domains in result */
6af866af 641 int nslot; /* next empty doms[] struct cpumask slot */
029190c5 642
029190c5 643 doms = NULL;
1d3504fc 644 dattr = NULL;
cf417141 645 csa = NULL;
029190c5
PJ
646
647 /* Special case for the 99% of systems with one, full, sched domain */
648 if (is_sched_load_balance(&top_cpuset)) {
6af866af 649 doms = kmalloc(cpumask_size(), GFP_KERNEL);
029190c5 650 if (!doms)
cf417141
MK
651 goto done;
652
1d3504fc
HS
653 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
654 if (dattr) {
655 *dattr = SD_ATTR_INIT;
93a65575 656 update_domain_attr_tree(dattr, &top_cpuset);
1d3504fc 657 }
300ed6cb 658 cpumask_copy(doms, top_cpuset.cpus_allowed);
cf417141
MK
659
660 ndoms = 1;
661 goto done;
029190c5
PJ
662 }
663
029190c5
PJ
664 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
665 if (!csa)
666 goto done;
667 csn = 0;
668
aeed6824
LZ
669 list_add(&top_cpuset.stack_list, &q);
670 while (!list_empty(&q)) {
029190c5
PJ
671 struct cgroup *cont;
672 struct cpuset *child; /* scans child cpusets of cp */
489a5393 673
aeed6824
LZ
674 cp = list_first_entry(&q, struct cpuset, stack_list);
675 list_del(q.next);
676
300ed6cb 677 if (cpumask_empty(cp->cpus_allowed))
489a5393
LJ
678 continue;
679
f5393693
LJ
680 /*
681 * All child cpusets contain a subset of the parent's cpus, so
682 * just skip them, and then we call update_domain_attr_tree()
683 * to calc relax_domain_level of the corresponding sched
684 * domain.
685 */
686 if (is_sched_load_balance(cp)) {
029190c5 687 csa[csn++] = cp;
f5393693
LJ
688 continue;
689 }
489a5393 690
029190c5
PJ
691 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
692 child = cgroup_cs(cont);
aeed6824 693 list_add_tail(&child->stack_list, &q);
029190c5
PJ
694 }
695 }
696
697 for (i = 0; i < csn; i++)
698 csa[i]->pn = i;
699 ndoms = csn;
700
701restart:
702 /* Find the best partition (set of sched domains) */
703 for (i = 0; i < csn; i++) {
704 struct cpuset *a = csa[i];
705 int apn = a->pn;
706
707 for (j = 0; j < csn; j++) {
708 struct cpuset *b = csa[j];
709 int bpn = b->pn;
710
711 if (apn != bpn && cpusets_overlap(a, b)) {
712 for (k = 0; k < csn; k++) {
713 struct cpuset *c = csa[k];
714
715 if (c->pn == bpn)
716 c->pn = apn;
717 }
718 ndoms--; /* one less element */
719 goto restart;
720 }
721 }
722 }
723
cf417141
MK
724 /*
725 * Now we know how many domains to create.
726 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
727 */
6af866af 728 doms = kmalloc(ndoms * cpumask_size(), GFP_KERNEL);
700018e0 729 if (!doms)
cf417141 730 goto done;
cf417141
MK
731
732 /*
733 * The rest of the code, including the scheduler, can deal with
734 * dattr==NULL case. No need to abort if alloc fails.
735 */
1d3504fc 736 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
029190c5
PJ
737
738 for (nslot = 0, i = 0; i < csn; i++) {
739 struct cpuset *a = csa[i];
6af866af 740 struct cpumask *dp;
029190c5
PJ
741 int apn = a->pn;
742
cf417141
MK
743 if (apn < 0) {
744 /* Skip completed partitions */
745 continue;
746 }
747
748 dp = doms + nslot;
749
750 if (nslot == ndoms) {
751 static int warnings = 10;
752 if (warnings) {
753 printk(KERN_WARNING
754 "rebuild_sched_domains confused:"
755 " nslot %d, ndoms %d, csn %d, i %d,"
756 " apn %d\n",
757 nslot, ndoms, csn, i, apn);
758 warnings--;
029190c5 759 }
cf417141
MK
760 continue;
761 }
029190c5 762
6af866af 763 cpumask_clear(dp);
cf417141
MK
764 if (dattr)
765 *(dattr + nslot) = SD_ATTR_INIT;
766 for (j = i; j < csn; j++) {
767 struct cpuset *b = csa[j];
768
769 if (apn == b->pn) {
300ed6cb 770 cpumask_or(dp, dp, b->cpus_allowed);
cf417141
MK
771 if (dattr)
772 update_domain_attr_tree(dattr + nslot, b);
773
774 /* Done with this partition */
775 b->pn = -1;
029190c5 776 }
029190c5 777 }
cf417141 778 nslot++;
029190c5
PJ
779 }
780 BUG_ON(nslot != ndoms);
781
cf417141
MK
782done:
783 kfree(csa);
784
700018e0
LZ
785 /*
786 * Fallback to the default domain if kmalloc() failed.
787 * See comments in partition_sched_domains().
788 */
789 if (doms == NULL)
790 ndoms = 1;
791
cf417141
MK
792 *domains = doms;
793 *attributes = dattr;
794 return ndoms;
795}
796
797/*
798 * Rebuild scheduler domains.
799 *
800 * Call with neither cgroup_mutex held nor within get_online_cpus().
801 * Takes both cgroup_mutex and get_online_cpus().
802 *
803 * Cannot be directly called from cpuset code handling changes
804 * to the cpuset pseudo-filesystem, because it cannot be called
805 * from code that already holds cgroup_mutex.
806 */
807static void do_rebuild_sched_domains(struct work_struct *unused)
808{
809 struct sched_domain_attr *attr;
6af866af 810 struct cpumask *doms;
cf417141
MK
811 int ndoms;
812
86ef5c9a 813 get_online_cpus();
cf417141
MK
814
815 /* Generate domain masks and attrs */
816 cgroup_lock();
817 ndoms = generate_sched_domains(&doms, &attr);
818 cgroup_unlock();
819
820 /* Have scheduler rebuild the domains */
821 partition_sched_domains(ndoms, doms, attr);
822
86ef5c9a 823 put_online_cpus();
cf417141 824}
db7f47cf
PM
825#else /* !CONFIG_SMP */
826static void do_rebuild_sched_domains(struct work_struct *unused)
827{
828}
829
830static int generate_sched_domains(struct cpumask **domains,
831 struct sched_domain_attr **attributes)
832{
833 *domains = NULL;
834 return 1;
835}
836#endif /* CONFIG_SMP */
029190c5 837
cf417141
MK
838static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
839
840/*
841 * Rebuild scheduler domains, asynchronously via workqueue.
842 *
843 * If the flag 'sched_load_balance' of any cpuset with non-empty
844 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
845 * which has that flag enabled, or if any cpuset with a non-empty
846 * 'cpus' is removed, then call this routine to rebuild the
847 * scheduler's dynamic sched domains.
848 *
849 * The rebuild_sched_domains() and partition_sched_domains()
850 * routines must nest cgroup_lock() inside get_online_cpus(),
851 * but such cpuset changes as these must nest that locking the
852 * other way, holding cgroup_lock() for much of the code.
853 *
854 * So in order to avoid an ABBA deadlock, the cpuset code handling
855 * these user changes delegates the actual sched domain rebuilding
856 * to a separate workqueue thread, which ends up processing the
857 * above do_rebuild_sched_domains() function.
858 */
859static void async_rebuild_sched_domains(void)
860{
f90d4118 861 queue_work(cpuset_wq, &rebuild_sched_domains_work);
cf417141
MK
862}
863
864/*
865 * Accomplishes the same scheduler domain rebuild as the above
866 * async_rebuild_sched_domains(), however it directly calls the
867 * rebuild routine synchronously rather than calling it via an
868 * asynchronous work thread.
869 *
870 * This can only be called from code that is not holding
871 * cgroup_mutex (not nested in a cgroup_lock() call.)
872 */
873void rebuild_sched_domains(void)
874{
875 do_rebuild_sched_domains(NULL);
029190c5
PJ
876}
877
58f4790b
CW
878/**
879 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
880 * @tsk: task to test
881 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
882 *
2df167a3 883 * Call with cgroup_mutex held. May take callback_mutex during call.
58f4790b
CW
884 * Called for each task in a cgroup by cgroup_scan_tasks().
885 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
886 * words, if its mask is not equal to its cpuset's mask).
053199ed 887 */
9e0c914c
AB
888static int cpuset_test_cpumask(struct task_struct *tsk,
889 struct cgroup_scanner *scan)
58f4790b 890{
300ed6cb 891 return !cpumask_equal(&tsk->cpus_allowed,
58f4790b
CW
892 (cgroup_cs(scan->cg))->cpus_allowed);
893}
053199ed 894
58f4790b
CW
895/**
896 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
897 * @tsk: task to test
898 * @scan: struct cgroup_scanner containing the cgroup of the task
899 *
900 * Called by cgroup_scan_tasks() for each task in a cgroup whose
901 * cpus_allowed mask needs to be changed.
902 *
903 * We don't need to re-check for the cgroup/cpuset membership, since we're
904 * holding cgroup_lock() at this point.
905 */
9e0c914c
AB
906static void cpuset_change_cpumask(struct task_struct *tsk,
907 struct cgroup_scanner *scan)
58f4790b 908{
300ed6cb 909 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
58f4790b
CW
910}
911
0b2f630a
MX
912/**
913 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
914 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
4e74339a 915 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
0b2f630a
MX
916 *
917 * Called with cgroup_mutex held
918 *
919 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
920 * calling callback functions for each.
921 *
4e74339a
LZ
922 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
923 * if @heap != NULL.
0b2f630a 924 */
4e74339a 925static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
0b2f630a
MX
926{
927 struct cgroup_scanner scan;
0b2f630a
MX
928
929 scan.cg = cs->css.cgroup;
930 scan.test_task = cpuset_test_cpumask;
931 scan.process_task = cpuset_change_cpumask;
4e74339a
LZ
932 scan.heap = heap;
933 cgroup_scan_tasks(&scan);
0b2f630a
MX
934}
935
58f4790b
CW
936/**
937 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
938 * @cs: the cpuset to consider
939 * @buf: buffer of cpu numbers written to this cpuset
940 */
645fcc9d
LZ
941static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
942 const char *buf)
1da177e4 943{
4e74339a 944 struct ptr_heap heap;
58f4790b
CW
945 int retval;
946 int is_load_balanced;
1da177e4 947
4c4d50f7
PJ
948 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
949 if (cs == &top_cpuset)
950 return -EACCES;
951
6f7f02e7 952 /*
c8d9c90c 953 * An empty cpus_allowed is ok only if the cpuset has no tasks.
020958b6
PJ
954 * Since cpulist_parse() fails on an empty mask, we special case
955 * that parsing. The validate_change() call ensures that cpusets
956 * with tasks have cpus.
6f7f02e7 957 */
020958b6 958 if (!*buf) {
300ed6cb 959 cpumask_clear(trialcs->cpus_allowed);
6f7f02e7 960 } else {
300ed6cb 961 retval = cpulist_parse(buf, trialcs->cpus_allowed);
6f7f02e7
DR
962 if (retval < 0)
963 return retval;
37340746 964
300ed6cb 965 if (!cpumask_subset(trialcs->cpus_allowed, cpu_online_mask))
37340746 966 return -EINVAL;
6f7f02e7 967 }
645fcc9d 968 retval = validate_change(cs, trialcs);
85d7b949
DG
969 if (retval < 0)
970 return retval;
029190c5 971
8707d8b8 972 /* Nothing to do if the cpus didn't change */
300ed6cb 973 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
8707d8b8 974 return 0;
58f4790b 975
4e74339a
LZ
976 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
977 if (retval)
978 return retval;
979
645fcc9d 980 is_load_balanced = is_sched_load_balance(trialcs);
029190c5 981
3d3f26a7 982 mutex_lock(&callback_mutex);
300ed6cb 983 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
3d3f26a7 984 mutex_unlock(&callback_mutex);
029190c5 985
8707d8b8
PM
986 /*
987 * Scan tasks in the cpuset, and update the cpumasks of any
58f4790b 988 * that need an update.
8707d8b8 989 */
4e74339a
LZ
990 update_tasks_cpumask(cs, &heap);
991
992 heap_free(&heap);
58f4790b 993
8707d8b8 994 if (is_load_balanced)
cf417141 995 async_rebuild_sched_domains();
85d7b949 996 return 0;
1da177e4
LT
997}
998
e4e364e8
PJ
999/*
1000 * cpuset_migrate_mm
1001 *
1002 * Migrate memory region from one set of nodes to another.
1003 *
1004 * Temporarilly set tasks mems_allowed to target nodes of migration,
1005 * so that the migration code can allocate pages on these nodes.
1006 *
2df167a3 1007 * Call holding cgroup_mutex, so current's cpuset won't change
c8d9c90c 1008 * during this call, as manage_mutex holds off any cpuset_attach()
e4e364e8
PJ
1009 * calls. Therefore we don't need to take task_lock around the
1010 * call to guarantee_online_mems(), as we know no one is changing
2df167a3 1011 * our task's cpuset.
e4e364e8
PJ
1012 *
1013 * Hold callback_mutex around the two modifications of our tasks
1014 * mems_allowed to synchronize with cpuset_mems_allowed().
1015 *
1016 * While the mm_struct we are migrating is typically from some
1017 * other task, the task_struct mems_allowed that we are hacking
1018 * is for our current task, which must allocate new pages for that
1019 * migrating memory region.
1020 *
1021 * We call cpuset_update_task_memory_state() before hacking
1022 * our tasks mems_allowed, so that we are assured of being in
1023 * sync with our tasks cpuset, and in particular, callbacks to
1024 * cpuset_update_task_memory_state() from nested page allocations
1025 * won't see any mismatch of our cpuset and task mems_generation
1026 * values, so won't overwrite our hacked tasks mems_allowed
1027 * nodemask.
1028 */
1029
1030static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1031 const nodemask_t *to)
1032{
1033 struct task_struct *tsk = current;
1034
1035 cpuset_update_task_memory_state();
1036
1037 mutex_lock(&callback_mutex);
1038 tsk->mems_allowed = *to;
1039 mutex_unlock(&callback_mutex);
1040
1041 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
1042
1043 mutex_lock(&callback_mutex);
8793d854 1044 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
e4e364e8
PJ
1045 mutex_unlock(&callback_mutex);
1046}
1047
3b6766fe
LZ
1048/*
1049 * Rebind task's vmas to cpuset's new mems_allowed, and migrate pages to new
1050 * nodes if memory_migrate flag is set. Called with cgroup_mutex held.
1051 */
1052static void cpuset_change_nodemask(struct task_struct *p,
1053 struct cgroup_scanner *scan)
1054{
1055 struct mm_struct *mm;
1056 struct cpuset *cs;
1057 int migrate;
1058 const nodemask_t *oldmem = scan->data;
1059
1060 mm = get_task_mm(p);
1061 if (!mm)
1062 return;
1063
1064 cs = cgroup_cs(scan->cg);
1065 migrate = is_memory_migrate(cs);
1066
1067 mpol_rebind_mm(mm, &cs->mems_allowed);
1068 if (migrate)
1069 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1070 mmput(mm);
1071}
1072
8793d854
PM
1073static void *cpuset_being_rebound;
1074
0b2f630a
MX
1075/**
1076 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1077 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1078 * @oldmem: old mems_allowed of cpuset cs
010cfac4 1079 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
0b2f630a
MX
1080 *
1081 * Called with cgroup_mutex held
010cfac4
LZ
1082 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1083 * if @heap != NULL.
0b2f630a 1084 */
010cfac4
LZ
1085static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1086 struct ptr_heap *heap)
1da177e4 1087{
3b6766fe 1088 struct cgroup_scanner scan;
59dac16f 1089
846a16bf 1090 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
4225399a 1091
3b6766fe
LZ
1092 scan.cg = cs->css.cgroup;
1093 scan.test_task = NULL;
1094 scan.process_task = cpuset_change_nodemask;
010cfac4 1095 scan.heap = heap;
3b6766fe 1096 scan.data = (nodemask_t *)oldmem;
4225399a
PJ
1097
1098 /*
3b6766fe
LZ
1099 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1100 * take while holding tasklist_lock. Forks can happen - the
1101 * mpol_dup() cpuset_being_rebound check will catch such forks,
1102 * and rebind their vma mempolicies too. Because we still hold
1103 * the global cgroup_mutex, we know that no other rebind effort
1104 * will be contending for the global variable cpuset_being_rebound.
4225399a 1105 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
04c19fa6 1106 * is idempotent. Also migrate pages in each mm to new nodes.
4225399a 1107 */
010cfac4 1108 cgroup_scan_tasks(&scan);
4225399a 1109
2df167a3 1110 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
8793d854 1111 cpuset_being_rebound = NULL;
1da177e4
LT
1112}
1113
0b2f630a
MX
1114/*
1115 * Handle user request to change the 'mems' memory placement
1116 * of a cpuset. Needs to validate the request, update the
1117 * cpusets mems_allowed and mems_generation, and for each
1118 * task in the cpuset, rebind any vma mempolicies and if
1119 * the cpuset is marked 'memory_migrate', migrate the tasks
1120 * pages to the new memory.
1121 *
1122 * Call with cgroup_mutex held. May take callback_mutex during call.
1123 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1124 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1125 * their mempolicies to the cpusets new mems_allowed.
1126 */
645fcc9d
LZ
1127static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1128 const char *buf)
0b2f630a 1129{
0b2f630a
MX
1130 nodemask_t oldmem;
1131 int retval;
010cfac4 1132 struct ptr_heap heap;
0b2f630a
MX
1133
1134 /*
1135 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
1136 * it's read-only
1137 */
1138 if (cs == &top_cpuset)
1139 return -EACCES;
1140
0b2f630a
MX
1141 /*
1142 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1143 * Since nodelist_parse() fails on an empty mask, we special case
1144 * that parsing. The validate_change() call ensures that cpusets
1145 * with tasks have memory.
1146 */
1147 if (!*buf) {
645fcc9d 1148 nodes_clear(trialcs->mems_allowed);
0b2f630a 1149 } else {
645fcc9d 1150 retval = nodelist_parse(buf, trialcs->mems_allowed);
0b2f630a
MX
1151 if (retval < 0)
1152 goto done;
1153
645fcc9d 1154 if (!nodes_subset(trialcs->mems_allowed,
0b2f630a
MX
1155 node_states[N_HIGH_MEMORY]))
1156 return -EINVAL;
1157 }
1158 oldmem = cs->mems_allowed;
645fcc9d 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 cs->mems_generation = cpuset_mems_generation++;
1174 mutex_unlock(&callback_mutex);
1175
010cfac4
LZ
1176 update_tasks_nodemask(cs, &oldmem, &heap);
1177
1178 heap_free(&heap);
0b2f630a
MX
1179done:
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
30e0e178
LZ
1191 if (val < -1 || val >= SD_LV_MAX)
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
2341d1b6
LZ
1399/* Protected by cgroup_lock */
1400static cpumask_var_t cpus_attach;
1401
2df167a3 1402/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
8793d854
PM
1403static int cpuset_can_attach(struct cgroup_subsys *ss,
1404 struct cgroup *cont, struct task_struct *tsk)
1da177e4 1405{
8793d854 1406 struct cpuset *cs = cgroup_cs(cont);
1da177e4 1407
300ed6cb 1408 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1da177e4 1409 return -ENOSPC;
9985b0ba 1410
6d7b2f5f
DR
1411 /*
1412 * Kthreads bound to specific cpus cannot be moved to a new cpuset; we
1413 * cannot change their cpu affinity and isolating such threads by their
1414 * set of allowed nodes is unnecessary. Thus, cpusets are not
1415 * applicable for such threads. This prevents checking for success of
1416 * set_cpus_allowed_ptr() on all attached tasks before cpus_allowed may
1417 * be changed.
1418 */
1419 if (tsk->flags & PF_THREAD_BOUND)
1420 return -EINVAL;
1da177e4 1421
6d7b2f5f 1422 return security_task_setscheduler(tsk, 0, NULL);
8793d854 1423}
1da177e4 1424
8793d854
PM
1425static void cpuset_attach(struct cgroup_subsys *ss,
1426 struct cgroup *cont, struct cgroup *oldcont,
1427 struct task_struct *tsk)
1428{
8793d854
PM
1429 nodemask_t from, to;
1430 struct mm_struct *mm;
1431 struct cpuset *cs = cgroup_cs(cont);
1432 struct cpuset *oldcs = cgroup_cs(oldcont);
9985b0ba 1433 int err;
22fb52dd 1434
f5813d94 1435 if (cs == &top_cpuset) {
2341d1b6 1436 cpumask_copy(cpus_attach, cpu_possible_mask);
f5813d94
MX
1437 } else {
1438 mutex_lock(&callback_mutex);
2341d1b6 1439 guarantee_online_cpus(cs, cpus_attach);
f5813d94
MX
1440 mutex_unlock(&callback_mutex);
1441 }
2341d1b6 1442 err = set_cpus_allowed_ptr(tsk, cpus_attach);
9985b0ba
DR
1443 if (err)
1444 return;
1da177e4 1445
950592f7
MX
1446 cpuset_update_task_spread_flag(cs, tsk);
1447
45b07ef3
PJ
1448 from = oldcs->mems_allowed;
1449 to = cs->mems_allowed;
4225399a
PJ
1450 mm = get_task_mm(tsk);
1451 if (mm) {
1452 mpol_rebind_mm(mm, &to);
2741a559 1453 if (is_memory_migrate(cs))
e4e364e8 1454 cpuset_migrate_mm(mm, &from, &to);
4225399a
PJ
1455 mmput(mm);
1456 }
1da177e4
LT
1457}
1458
1459/* The various types of files and directories in a cpuset file system */
1460
1461typedef enum {
45b07ef3 1462 FILE_MEMORY_MIGRATE,
1da177e4
LT
1463 FILE_CPULIST,
1464 FILE_MEMLIST,
1465 FILE_CPU_EXCLUSIVE,
1466 FILE_MEM_EXCLUSIVE,
78608366 1467 FILE_MEM_HARDWALL,
029190c5 1468 FILE_SCHED_LOAD_BALANCE,
1d3504fc 1469 FILE_SCHED_RELAX_DOMAIN_LEVEL,
3e0d98b9
PJ
1470 FILE_MEMORY_PRESSURE_ENABLED,
1471 FILE_MEMORY_PRESSURE,
825a46af
PJ
1472 FILE_SPREAD_PAGE,
1473 FILE_SPREAD_SLAB,
1da177e4
LT
1474} cpuset_filetype_t;
1475
700fe1ab
PM
1476static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1477{
1478 int retval = 0;
1479 struct cpuset *cs = cgroup_cs(cgrp);
1480 cpuset_filetype_t type = cft->private;
1481
e3712395 1482 if (!cgroup_lock_live_group(cgrp))
700fe1ab 1483 return -ENODEV;
700fe1ab
PM
1484
1485 switch (type) {
1da177e4 1486 case FILE_CPU_EXCLUSIVE:
700fe1ab 1487 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1da177e4
LT
1488 break;
1489 case FILE_MEM_EXCLUSIVE:
700fe1ab 1490 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1da177e4 1491 break;
78608366
PM
1492 case FILE_MEM_HARDWALL:
1493 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1494 break;
029190c5 1495 case FILE_SCHED_LOAD_BALANCE:
700fe1ab 1496 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1d3504fc 1497 break;
45b07ef3 1498 case FILE_MEMORY_MIGRATE:
700fe1ab 1499 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
45b07ef3 1500 break;
3e0d98b9 1501 case FILE_MEMORY_PRESSURE_ENABLED:
700fe1ab 1502 cpuset_memory_pressure_enabled = !!val;
3e0d98b9
PJ
1503 break;
1504 case FILE_MEMORY_PRESSURE:
1505 retval = -EACCES;
1506 break;
825a46af 1507 case FILE_SPREAD_PAGE:
700fe1ab 1508 retval = update_flag(CS_SPREAD_PAGE, cs, val);
825a46af
PJ
1509 break;
1510 case FILE_SPREAD_SLAB:
700fe1ab 1511 retval = update_flag(CS_SPREAD_SLAB, cs, val);
825a46af 1512 break;
1da177e4
LT
1513 default:
1514 retval = -EINVAL;
700fe1ab 1515 break;
1da177e4 1516 }
8793d854 1517 cgroup_unlock();
1da177e4
LT
1518 return retval;
1519}
1520
5be7a479
PM
1521static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1522{
1523 int retval = 0;
1524 struct cpuset *cs = cgroup_cs(cgrp);
1525 cpuset_filetype_t type = cft->private;
1526
e3712395 1527 if (!cgroup_lock_live_group(cgrp))
5be7a479 1528 return -ENODEV;
e3712395 1529
5be7a479
PM
1530 switch (type) {
1531 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1532 retval = update_relax_domain_level(cs, val);
1533 break;
1534 default:
1535 retval = -EINVAL;
1536 break;
1537 }
1538 cgroup_unlock();
1539 return retval;
1540}
1541
e3712395
PM
1542/*
1543 * Common handling for a write to a "cpus" or "mems" file.
1544 */
1545static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1546 const char *buf)
1547{
1548 int retval = 0;
645fcc9d
LZ
1549 struct cpuset *cs = cgroup_cs(cgrp);
1550 struct cpuset *trialcs;
e3712395
PM
1551
1552 if (!cgroup_lock_live_group(cgrp))
1553 return -ENODEV;
1554
645fcc9d
LZ
1555 trialcs = alloc_trial_cpuset(cs);
1556 if (!trialcs)
1557 return -ENOMEM;
1558
e3712395
PM
1559 switch (cft->private) {
1560 case FILE_CPULIST:
645fcc9d 1561 retval = update_cpumask(cs, trialcs, buf);
e3712395
PM
1562 break;
1563 case FILE_MEMLIST:
645fcc9d 1564 retval = update_nodemask(cs, trialcs, buf);
e3712395
PM
1565 break;
1566 default:
1567 retval = -EINVAL;
1568 break;
1569 }
645fcc9d
LZ
1570
1571 free_trial_cpuset(trialcs);
e3712395
PM
1572 cgroup_unlock();
1573 return retval;
1574}
1575
1da177e4
LT
1576/*
1577 * These ascii lists should be read in a single call, by using a user
1578 * buffer large enough to hold the entire map. If read in smaller
1579 * chunks, there is no guarantee of atomicity. Since the display format
1580 * used, list of ranges of sequential numbers, is variable length,
1581 * and since these maps can change value dynamically, one could read
1582 * gibberish by doing partial reads while a list was changing.
1583 * A single large read to a buffer that crosses a page boundary is
1584 * ok, because the result being copied to user land is not recomputed
1585 * across a page fault.
1586 */
1587
1588static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1589{
5a7625df 1590 int ret;
1da177e4 1591
3d3f26a7 1592 mutex_lock(&callback_mutex);
300ed6cb 1593 ret = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
3d3f26a7 1594 mutex_unlock(&callback_mutex);
1da177e4 1595
5a7625df 1596 return ret;
1da177e4
LT
1597}
1598
1599static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1600{
1601 nodemask_t mask;
1602
3d3f26a7 1603 mutex_lock(&callback_mutex);
1da177e4 1604 mask = cs->mems_allowed;
3d3f26a7 1605 mutex_unlock(&callback_mutex);
1da177e4
LT
1606
1607 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1608}
1609
8793d854
PM
1610static ssize_t cpuset_common_file_read(struct cgroup *cont,
1611 struct cftype *cft,
1612 struct file *file,
1613 char __user *buf,
1614 size_t nbytes, loff_t *ppos)
1da177e4 1615{
8793d854 1616 struct cpuset *cs = cgroup_cs(cont);
1da177e4
LT
1617 cpuset_filetype_t type = cft->private;
1618 char *page;
1619 ssize_t retval = 0;
1620 char *s;
1da177e4 1621
e12ba74d 1622 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
1da177e4
LT
1623 return -ENOMEM;
1624
1625 s = page;
1626
1627 switch (type) {
1628 case FILE_CPULIST:
1629 s += cpuset_sprintf_cpulist(s, cs);
1630 break;
1631 case FILE_MEMLIST:
1632 s += cpuset_sprintf_memlist(s, cs);
1633 break;
1da177e4
LT
1634 default:
1635 retval = -EINVAL;
1636 goto out;
1637 }
1638 *s++ = '\n';
1da177e4 1639
eacaa1f5 1640 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1da177e4
LT
1641out:
1642 free_page((unsigned long)page);
1643 return retval;
1644}
1645
700fe1ab
PM
1646static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1647{
1648 struct cpuset *cs = cgroup_cs(cont);
1649 cpuset_filetype_t type = cft->private;
1650 switch (type) {
1651 case FILE_CPU_EXCLUSIVE:
1652 return is_cpu_exclusive(cs);
1653 case FILE_MEM_EXCLUSIVE:
1654 return is_mem_exclusive(cs);
78608366
PM
1655 case FILE_MEM_HARDWALL:
1656 return is_mem_hardwall(cs);
700fe1ab
PM
1657 case FILE_SCHED_LOAD_BALANCE:
1658 return is_sched_load_balance(cs);
1659 case FILE_MEMORY_MIGRATE:
1660 return is_memory_migrate(cs);
1661 case FILE_MEMORY_PRESSURE_ENABLED:
1662 return cpuset_memory_pressure_enabled;
1663 case FILE_MEMORY_PRESSURE:
1664 return fmeter_getrate(&cs->fmeter);
1665 case FILE_SPREAD_PAGE:
1666 return is_spread_page(cs);
1667 case FILE_SPREAD_SLAB:
1668 return is_spread_slab(cs);
1669 default:
1670 BUG();
1671 }
cf417141
MK
1672
1673 /* Unreachable but makes gcc happy */
1674 return 0;
700fe1ab 1675}
1da177e4 1676
5be7a479
PM
1677static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1678{
1679 struct cpuset *cs = cgroup_cs(cont);
1680 cpuset_filetype_t type = cft->private;
1681 switch (type) {
1682 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1683 return cs->relax_domain_level;
1684 default:
1685 BUG();
1686 }
cf417141
MK
1687
1688 /* Unrechable but makes gcc happy */
1689 return 0;
5be7a479
PM
1690}
1691
1da177e4
LT
1692
1693/*
1694 * for the common functions, 'private' gives the type of file
1695 */
1696
addf2c73
PM
1697static struct cftype files[] = {
1698 {
1699 .name = "cpus",
1700 .read = cpuset_common_file_read,
e3712395
PM
1701 .write_string = cpuset_write_resmask,
1702 .max_write_len = (100U + 6 * NR_CPUS),
addf2c73
PM
1703 .private = FILE_CPULIST,
1704 },
1705
1706 {
1707 .name = "mems",
1708 .read = cpuset_common_file_read,
e3712395
PM
1709 .write_string = cpuset_write_resmask,
1710 .max_write_len = (100U + 6 * MAX_NUMNODES),
addf2c73
PM
1711 .private = FILE_MEMLIST,
1712 },
1713
1714 {
1715 .name = "cpu_exclusive",
1716 .read_u64 = cpuset_read_u64,
1717 .write_u64 = cpuset_write_u64,
1718 .private = FILE_CPU_EXCLUSIVE,
1719 },
1720
1721 {
1722 .name = "mem_exclusive",
1723 .read_u64 = cpuset_read_u64,
1724 .write_u64 = cpuset_write_u64,
1725 .private = FILE_MEM_EXCLUSIVE,
1726 },
1727
78608366
PM
1728 {
1729 .name = "mem_hardwall",
1730 .read_u64 = cpuset_read_u64,
1731 .write_u64 = cpuset_write_u64,
1732 .private = FILE_MEM_HARDWALL,
1733 },
1734
addf2c73
PM
1735 {
1736 .name = "sched_load_balance",
1737 .read_u64 = cpuset_read_u64,
1738 .write_u64 = cpuset_write_u64,
1739 .private = FILE_SCHED_LOAD_BALANCE,
1740 },
1741
1742 {
1743 .name = "sched_relax_domain_level",
5be7a479
PM
1744 .read_s64 = cpuset_read_s64,
1745 .write_s64 = cpuset_write_s64,
addf2c73
PM
1746 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1747 },
1748
1749 {
1750 .name = "memory_migrate",
1751 .read_u64 = cpuset_read_u64,
1752 .write_u64 = cpuset_write_u64,
1753 .private = FILE_MEMORY_MIGRATE,
1754 },
1755
1756 {
1757 .name = "memory_pressure",
1758 .read_u64 = cpuset_read_u64,
1759 .write_u64 = cpuset_write_u64,
1760 .private = FILE_MEMORY_PRESSURE,
099fca32 1761 .mode = S_IRUGO,
addf2c73
PM
1762 },
1763
1764 {
1765 .name = "memory_spread_page",
1766 .read_u64 = cpuset_read_u64,
1767 .write_u64 = cpuset_write_u64,
1768 .private = FILE_SPREAD_PAGE,
1769 },
1770
1771 {
1772 .name = "memory_spread_slab",
1773 .read_u64 = cpuset_read_u64,
1774 .write_u64 = cpuset_write_u64,
1775 .private = FILE_SPREAD_SLAB,
1776 },
45b07ef3
PJ
1777};
1778
3e0d98b9
PJ
1779static struct cftype cft_memory_pressure_enabled = {
1780 .name = "memory_pressure_enabled",
700fe1ab
PM
1781 .read_u64 = cpuset_read_u64,
1782 .write_u64 = cpuset_write_u64,
3e0d98b9
PJ
1783 .private = FILE_MEMORY_PRESSURE_ENABLED,
1784};
1785
8793d854 1786static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
1da177e4
LT
1787{
1788 int err;
1789
addf2c73
PM
1790 err = cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
1791 if (err)
1da177e4 1792 return err;
8793d854 1793 /* memory_pressure_enabled is in root cpuset only */
addf2c73 1794 if (!cont->parent)
8793d854 1795 err = cgroup_add_file(cont, ss,
addf2c73
PM
1796 &cft_memory_pressure_enabled);
1797 return err;
1da177e4
LT
1798}
1799
8793d854
PM
1800/*
1801 * post_clone() is called at the end of cgroup_clone().
1802 * 'cgroup' was just created automatically as a result of
1803 * a cgroup_clone(), and the current task is about to
1804 * be moved into 'cgroup'.
1805 *
1806 * Currently we refuse to set up the cgroup - thereby
1807 * refusing the task to be entered, and as a result refusing
1808 * the sys_unshare() or clone() which initiated it - if any
1809 * sibling cpusets have exclusive cpus or mem.
1810 *
1811 * If this becomes a problem for some users who wish to
1812 * allow that scenario, then cpuset_post_clone() could be
1813 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2df167a3
PM
1814 * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
1815 * held.
8793d854
PM
1816 */
1817static void cpuset_post_clone(struct cgroup_subsys *ss,
1818 struct cgroup *cgroup)
1819{
1820 struct cgroup *parent, *child;
1821 struct cpuset *cs, *parent_cs;
1822
1823 parent = cgroup->parent;
1824 list_for_each_entry(child, &parent->children, sibling) {
1825 cs = cgroup_cs(child);
1826 if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
1827 return;
1828 }
1829 cs = cgroup_cs(cgroup);
1830 parent_cs = cgroup_cs(parent);
1831
1832 cs->mems_allowed = parent_cs->mems_allowed;
300ed6cb 1833 cpumask_copy(cs->cpus_allowed, parent_cs->cpus_allowed);
8793d854
PM
1834 return;
1835}
1836
1da177e4
LT
1837/*
1838 * cpuset_create - create a cpuset
2df167a3
PM
1839 * ss: cpuset cgroup subsystem
1840 * cont: control group that the new cpuset will be part of
1da177e4
LT
1841 */
1842
8793d854
PM
1843static struct cgroup_subsys_state *cpuset_create(
1844 struct cgroup_subsys *ss,
1845 struct cgroup *cont)
1da177e4
LT
1846{
1847 struct cpuset *cs;
8793d854 1848 struct cpuset *parent;
1da177e4 1849
8793d854
PM
1850 if (!cont->parent) {
1851 /* This is early initialization for the top cgroup */
1852 top_cpuset.mems_generation = cpuset_mems_generation++;
1853 return &top_cpuset.css;
1854 }
1855 parent = cgroup_cs(cont->parent);
1da177e4
LT
1856 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1857 if (!cs)
8793d854 1858 return ERR_PTR(-ENOMEM);
300ed6cb
LZ
1859 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1860 kfree(cs);
1861 return ERR_PTR(-ENOMEM);
1862 }
1da177e4 1863
cf2a473c 1864 cpuset_update_task_memory_state();
1da177e4 1865 cs->flags = 0;
825a46af
PJ
1866 if (is_spread_page(parent))
1867 set_bit(CS_SPREAD_PAGE, &cs->flags);
1868 if (is_spread_slab(parent))
1869 set_bit(CS_SPREAD_SLAB, &cs->flags);
029190c5 1870 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
300ed6cb 1871 cpumask_clear(cs->cpus_allowed);
f9a86fcb 1872 nodes_clear(cs->mems_allowed);
151a4420 1873 cs->mems_generation = cpuset_mems_generation++;
3e0d98b9 1874 fmeter_init(&cs->fmeter);
1d3504fc 1875 cs->relax_domain_level = -1;
1da177e4
LT
1876
1877 cs->parent = parent;
202f72d5 1878 number_of_cpusets++;
8793d854 1879 return &cs->css ;
1da177e4
LT
1880}
1881
029190c5 1882/*
029190c5
PJ
1883 * If the cpuset being removed has its flag 'sched_load_balance'
1884 * enabled, then simulate turning sched_load_balance off, which
cf417141 1885 * will call async_rebuild_sched_domains().
029190c5
PJ
1886 */
1887
8793d854 1888static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
1da177e4 1889{
8793d854 1890 struct cpuset *cs = cgroup_cs(cont);
1da177e4 1891
cf2a473c 1892 cpuset_update_task_memory_state();
029190c5
PJ
1893
1894 if (is_sched_load_balance(cs))
700fe1ab 1895 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
029190c5 1896
202f72d5 1897 number_of_cpusets--;
300ed6cb 1898 free_cpumask_var(cs->cpus_allowed);
8793d854 1899 kfree(cs);
1da177e4
LT
1900}
1901
8793d854
PM
1902struct cgroup_subsys cpuset_subsys = {
1903 .name = "cpuset",
1904 .create = cpuset_create,
cf417141 1905 .destroy = cpuset_destroy,
8793d854
PM
1906 .can_attach = cpuset_can_attach,
1907 .attach = cpuset_attach,
1908 .populate = cpuset_populate,
1909 .post_clone = cpuset_post_clone,
1910 .subsys_id = cpuset_subsys_id,
1911 .early_init = 1,
1912};
1913
c417f024
PJ
1914/*
1915 * cpuset_init_early - just enough so that the calls to
1916 * cpuset_update_task_memory_state() in early init code
1917 * are harmless.
1918 */
1919
1920int __init cpuset_init_early(void)
1921{
38c7fed2 1922 alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_NOWAIT);
300ed6cb 1923
8793d854 1924 top_cpuset.mems_generation = cpuset_mems_generation++;
c417f024
PJ
1925 return 0;
1926}
1927
8793d854 1928
1da177e4
LT
1929/**
1930 * cpuset_init - initialize cpusets at system boot
1931 *
1932 * Description: Initialize top_cpuset and the cpuset internal file system,
1933 **/
1934
1935int __init cpuset_init(void)
1936{
8793d854 1937 int err = 0;
1da177e4 1938
300ed6cb 1939 cpumask_setall(top_cpuset.cpus_allowed);
f9a86fcb 1940 nodes_setall(top_cpuset.mems_allowed);
1da177e4 1941
3e0d98b9 1942 fmeter_init(&top_cpuset.fmeter);
151a4420 1943 top_cpuset.mems_generation = cpuset_mems_generation++;
029190c5 1944 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
1d3504fc 1945 top_cpuset.relax_domain_level = -1;
1da177e4 1946
1da177e4
LT
1947 err = register_filesystem(&cpuset_fs_type);
1948 if (err < 0)
8793d854
PM
1949 return err;
1950
2341d1b6
LZ
1951 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1952 BUG();
1953
202f72d5 1954 number_of_cpusets = 1;
8793d854 1955 return 0;
1da177e4
LT
1956}
1957
956db3ca
CW
1958/**
1959 * cpuset_do_move_task - move a given task to another cpuset
1960 * @tsk: pointer to task_struct the task to move
1961 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1962 *
1963 * Called by cgroup_scan_tasks() for each task in a cgroup.
1964 * Return nonzero to stop the walk through the tasks.
1965 */
9e0c914c
AB
1966static void cpuset_do_move_task(struct task_struct *tsk,
1967 struct cgroup_scanner *scan)
956db3ca 1968{
7f81b1ae 1969 struct cgroup *new_cgroup = scan->data;
956db3ca 1970
7f81b1ae 1971 cgroup_attach_task(new_cgroup, tsk);
956db3ca
CW
1972}
1973
1974/**
1975 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
1976 * @from: cpuset in which the tasks currently reside
1977 * @to: cpuset to which the tasks will be moved
1978 *
c8d9c90c
PJ
1979 * Called with cgroup_mutex held
1980 * callback_mutex must not be held, as cpuset_attach() will take it.
956db3ca
CW
1981 *
1982 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1983 * calling callback functions for each.
1984 */
1985static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
1986{
7f81b1ae 1987 struct cgroup_scanner scan;
956db3ca 1988
7f81b1ae
LZ
1989 scan.cg = from->css.cgroup;
1990 scan.test_task = NULL; /* select all tasks in cgroup */
1991 scan.process_task = cpuset_do_move_task;
1992 scan.heap = NULL;
1993 scan.data = to->css.cgroup;
956db3ca 1994
7f81b1ae 1995 if (cgroup_scan_tasks(&scan))
956db3ca
CW
1996 printk(KERN_ERR "move_member_tasks_to_cpuset: "
1997 "cgroup_scan_tasks failed\n");
1998}
1999
b1aac8bb 2000/*
cf417141 2001 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
b1aac8bb
PJ
2002 * or memory nodes, we need to walk over the cpuset hierarchy,
2003 * removing that CPU or node from all cpusets. If this removes the
956db3ca
CW
2004 * last CPU or node from a cpuset, then move the tasks in the empty
2005 * cpuset to its next-highest non-empty parent.
b1aac8bb 2006 *
c8d9c90c
PJ
2007 * Called with cgroup_mutex held
2008 * callback_mutex must not be held, as cpuset_attach() will take it.
b1aac8bb 2009 */
956db3ca
CW
2010static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
2011{
2012 struct cpuset *parent;
2013
c8d9c90c
PJ
2014 /*
2015 * The cgroup's css_sets list is in use if there are tasks
2016 * in the cpuset; the list is empty if there are none;
2017 * the cs->css.refcnt seems always 0.
2018 */
956db3ca
CW
2019 if (list_empty(&cs->css.cgroup->css_sets))
2020 return;
b1aac8bb 2021
956db3ca
CW
2022 /*
2023 * Find its next-highest non-empty parent, (top cpuset
2024 * has online cpus, so can't be empty).
2025 */
2026 parent = cs->parent;
300ed6cb 2027 while (cpumask_empty(parent->cpus_allowed) ||
b4501295 2028 nodes_empty(parent->mems_allowed))
956db3ca 2029 parent = parent->parent;
956db3ca
CW
2030
2031 move_member_tasks_to_cpuset(cs, parent);
2032}
2033
2034/*
2035 * Walk the specified cpuset subtree and look for empty cpusets.
2036 * The tasks of such cpuset must be moved to a parent cpuset.
2037 *
2df167a3 2038 * Called with cgroup_mutex held. We take callback_mutex to modify
956db3ca
CW
2039 * cpus_allowed and mems_allowed.
2040 *
2041 * This walk processes the tree from top to bottom, completing one layer
2042 * before dropping down to the next. It always processes a node before
2043 * any of its children.
2044 *
2045 * For now, since we lack memory hot unplug, we'll never see a cpuset
2046 * that has tasks along with an empty 'mems'. But if we did see such
2047 * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
2048 */
d294eb83 2049static void scan_for_empty_cpusets(struct cpuset *root)
b1aac8bb 2050{
8d1e6266 2051 LIST_HEAD(queue);
956db3ca
CW
2052 struct cpuset *cp; /* scans cpusets being updated */
2053 struct cpuset *child; /* scans child cpusets of cp */
8793d854 2054 struct cgroup *cont;
f9b4fb8d 2055 nodemask_t oldmems;
b1aac8bb 2056
956db3ca
CW
2057 list_add_tail((struct list_head *)&root->stack_list, &queue);
2058
956db3ca 2059 while (!list_empty(&queue)) {
8d1e6266 2060 cp = list_first_entry(&queue, struct cpuset, stack_list);
956db3ca
CW
2061 list_del(queue.next);
2062 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
2063 child = cgroup_cs(cont);
2064 list_add_tail(&child->stack_list, &queue);
2065 }
b4501295
PJ
2066
2067 /* Continue past cpusets with all cpus, mems online */
300ed6cb 2068 if (cpumask_subset(cp->cpus_allowed, cpu_online_mask) &&
b4501295
PJ
2069 nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
2070 continue;
2071
f9b4fb8d
MX
2072 oldmems = cp->mems_allowed;
2073
956db3ca 2074 /* Remove offline cpus and mems from this cpuset. */
b4501295 2075 mutex_lock(&callback_mutex);
300ed6cb
LZ
2076 cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
2077 cpu_online_mask);
956db3ca
CW
2078 nodes_and(cp->mems_allowed, cp->mems_allowed,
2079 node_states[N_HIGH_MEMORY]);
b4501295
PJ
2080 mutex_unlock(&callback_mutex);
2081
2082 /* Move tasks from the empty cpuset to a parent */
300ed6cb 2083 if (cpumask_empty(cp->cpus_allowed) ||
b4501295 2084 nodes_empty(cp->mems_allowed))
956db3ca 2085 remove_tasks_in_empty_cpuset(cp);
f9b4fb8d 2086 else {
4e74339a 2087 update_tasks_cpumask(cp, NULL);
010cfac4 2088 update_tasks_nodemask(cp, &oldmems, NULL);
f9b4fb8d 2089 }
b1aac8bb
PJ
2090 }
2091}
2092
4c4d50f7
PJ
2093/*
2094 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2095 * period. This is necessary in order to make cpusets transparent
2096 * (of no affect) on systems that are actively using CPU hotplug
2097 * but making no active use of cpusets.
2098 *
38837fc7
PJ
2099 * This routine ensures that top_cpuset.cpus_allowed tracks
2100 * cpu_online_map on each CPU hotplug (cpuhp) event.
cf417141
MK
2101 *
2102 * Called within get_online_cpus(). Needs to call cgroup_lock()
2103 * before calling generate_sched_domains().
4c4d50f7 2104 */
cf417141 2105static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
029190c5 2106 unsigned long phase, void *unused_cpu)
4c4d50f7 2107{
cf417141 2108 struct sched_domain_attr *attr;
6af866af 2109 struct cpumask *doms;
cf417141
MK
2110 int ndoms;
2111
3e84050c 2112 switch (phase) {
3e84050c
DA
2113 case CPU_ONLINE:
2114 case CPU_ONLINE_FROZEN:
2115 case CPU_DEAD:
2116 case CPU_DEAD_FROZEN:
3e84050c 2117 break;
cf417141 2118
3e84050c 2119 default:
ac076758 2120 return NOTIFY_DONE;
3e84050c 2121 }
ac076758 2122
cf417141 2123 cgroup_lock();
0b4217b3 2124 mutex_lock(&callback_mutex);
300ed6cb 2125 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
0b4217b3 2126 mutex_unlock(&callback_mutex);
cf417141
MK
2127 scan_for_empty_cpusets(&top_cpuset);
2128 ndoms = generate_sched_domains(&doms, &attr);
2129 cgroup_unlock();
2130
2131 /* Have scheduler rebuild the domains */
2132 partition_sched_domains(ndoms, doms, attr);
2133
3e84050c 2134 return NOTIFY_OK;
4c4d50f7 2135}
4c4d50f7 2136
b1aac8bb 2137#ifdef CONFIG_MEMORY_HOTPLUG
38837fc7 2138/*
0e1e7c7a 2139 * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
cf417141
MK
2140 * Call this routine anytime after node_states[N_HIGH_MEMORY] changes.
2141 * See also the previous routine cpuset_track_online_cpus().
38837fc7 2142 */
f481891f
MX
2143static int cpuset_track_online_nodes(struct notifier_block *self,
2144 unsigned long action, void *arg)
38837fc7 2145{
cf417141 2146 cgroup_lock();
f481891f
MX
2147 switch (action) {
2148 case MEM_ONLINE:
f481891f 2149 case MEM_OFFLINE:
0b4217b3 2150 mutex_lock(&callback_mutex);
f481891f 2151 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
0b4217b3
LZ
2152 mutex_unlock(&callback_mutex);
2153 if (action == MEM_OFFLINE)
2154 scan_for_empty_cpusets(&top_cpuset);
f481891f
MX
2155 break;
2156 default:
2157 break;
2158 }
cf417141 2159 cgroup_unlock();
f481891f 2160 return NOTIFY_OK;
38837fc7
PJ
2161}
2162#endif
2163
1da177e4
LT
2164/**
2165 * cpuset_init_smp - initialize cpus_allowed
2166 *
2167 * Description: Finish top cpuset after cpu, node maps are initialized
2168 **/
2169
2170void __init cpuset_init_smp(void)
2171{
300ed6cb 2172 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
0e1e7c7a 2173 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
4c4d50f7 2174
cf417141 2175 hotcpu_notifier(cpuset_track_online_cpus, 0);
f481891f 2176 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
f90d4118
MX
2177
2178 cpuset_wq = create_singlethread_workqueue("cpuset");
2179 BUG_ON(!cpuset_wq);
1da177e4
LT
2180}
2181
2182/**
1da177e4
LT
2183 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2184 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
6af866af 2185 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
1da177e4 2186 *
300ed6cb 2187 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
1da177e4
LT
2188 * attached to the specified @tsk. Guaranteed to return some non-empty
2189 * subset of cpu_online_map, even if this means going outside the
2190 * tasks cpuset.
2191 **/
2192
6af866af 2193void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
1da177e4 2194{
3d3f26a7 2195 mutex_lock(&callback_mutex);
f9a86fcb 2196 cpuset_cpus_allowed_locked(tsk, pmask);
470fd646 2197 mutex_unlock(&callback_mutex);
470fd646
CW
2198}
2199
2200/**
2201 * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
2df167a3 2202 * Must be called with callback_mutex held.
470fd646 2203 **/
6af866af 2204void cpuset_cpus_allowed_locked(struct task_struct *tsk, struct cpumask *pmask)
470fd646 2205{
909d75a3 2206 task_lock(tsk);
f9a86fcb 2207 guarantee_online_cpus(task_cs(tsk), pmask);
909d75a3 2208 task_unlock(tsk);
1da177e4
LT
2209}
2210
2211void cpuset_init_current_mems_allowed(void)
2212{
f9a86fcb 2213 nodes_setall(current->mems_allowed);
1da177e4
LT
2214}
2215
909d75a3
PJ
2216/**
2217 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2218 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2219 *
2220 * Description: Returns the nodemask_t mems_allowed of the cpuset
2221 * attached to the specified @tsk. Guaranteed to return some non-empty
0e1e7c7a 2222 * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
909d75a3
PJ
2223 * tasks cpuset.
2224 **/
2225
2226nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2227{
2228 nodemask_t mask;
2229
3d3f26a7 2230 mutex_lock(&callback_mutex);
909d75a3 2231 task_lock(tsk);
8793d854 2232 guarantee_online_mems(task_cs(tsk), &mask);
909d75a3 2233 task_unlock(tsk);
3d3f26a7 2234 mutex_unlock(&callback_mutex);
909d75a3
PJ
2235
2236 return mask;
2237}
2238
d9fd8a6d 2239/**
19770b32
MG
2240 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2241 * @nodemask: the nodemask to be checked
d9fd8a6d 2242 *
19770b32 2243 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
1da177e4 2244 */
19770b32 2245int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
1da177e4 2246{
19770b32 2247 return nodes_intersects(*nodemask, current->mems_allowed);
1da177e4
LT
2248}
2249
9bf2229f 2250/*
78608366
PM
2251 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2252 * mem_hardwall ancestor to the specified cpuset. Call holding
2253 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2254 * (an unusual configuration), then returns the root cpuset.
9bf2229f 2255 */
78608366 2256static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
9bf2229f 2257{
78608366 2258 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
9bf2229f
PJ
2259 cs = cs->parent;
2260 return cs;
2261}
2262
d9fd8a6d 2263/**
a1bc5a4e
DR
2264 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2265 * @node: is this an allowed node?
02a0e53d 2266 * @gfp_mask: memory allocation flags
d9fd8a6d 2267 *
a1bc5a4e
DR
2268 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2269 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2270 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2271 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2272 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2273 * flag, yes.
9bf2229f
PJ
2274 * Otherwise, no.
2275 *
a1bc5a4e
DR
2276 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2277 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2278 * might sleep, and might allow a node from an enclosing cpuset.
02a0e53d 2279 *
a1bc5a4e
DR
2280 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2281 * cpusets, and never sleeps.
02a0e53d
PJ
2282 *
2283 * The __GFP_THISNODE placement logic is really handled elsewhere,
2284 * by forcibly using a zonelist starting at a specified node, and by
2285 * (in get_page_from_freelist()) refusing to consider the zones for
2286 * any node on the zonelist except the first. By the time any such
2287 * calls get to this routine, we should just shut up and say 'yes'.
2288 *
9bf2229f 2289 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
c596d9f3
DR
2290 * and do not allow allocations outside the current tasks cpuset
2291 * unless the task has been OOM killed as is marked TIF_MEMDIE.
9bf2229f 2292 * GFP_KERNEL allocations are not so marked, so can escape to the
78608366 2293 * nearest enclosing hardwalled ancestor cpuset.
9bf2229f 2294 *
02a0e53d
PJ
2295 * Scanning up parent cpusets requires callback_mutex. The
2296 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2297 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2298 * current tasks mems_allowed came up empty on the first pass over
2299 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2300 * cpuset are short of memory, might require taking the callback_mutex
2301 * mutex.
9bf2229f 2302 *
36be57ff 2303 * The first call here from mm/page_alloc:get_page_from_freelist()
02a0e53d
PJ
2304 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2305 * so no allocation on a node outside the cpuset is allowed (unless
2306 * in interrupt, of course).
36be57ff
PJ
2307 *
2308 * The second pass through get_page_from_freelist() doesn't even call
2309 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2310 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2311 * in alloc_flags. That logic and the checks below have the combined
2312 * affect that:
9bf2229f
PJ
2313 * in_interrupt - any node ok (current task context irrelevant)
2314 * GFP_ATOMIC - any node ok
c596d9f3 2315 * TIF_MEMDIE - any node ok
78608366 2316 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
9bf2229f 2317 * GFP_USER - only nodes in current tasks mems allowed ok.
36be57ff
PJ
2318 *
2319 * Rule:
a1bc5a4e 2320 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
36be57ff
PJ
2321 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2322 * the code that might scan up ancestor cpusets and sleep.
02a0e53d 2323 */
a1bc5a4e 2324int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
1da177e4 2325{
9bf2229f 2326 const struct cpuset *cs; /* current cpuset ancestors */
29afd49b 2327 int allowed; /* is allocation in zone z allowed? */
9bf2229f 2328
9b819d20 2329 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
9bf2229f 2330 return 1;
92d1dbd2 2331 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
9bf2229f
PJ
2332 if (node_isset(node, current->mems_allowed))
2333 return 1;
c596d9f3
DR
2334 /*
2335 * Allow tasks that have access to memory reserves because they have
2336 * been OOM killed to get memory anywhere.
2337 */
2338 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2339 return 1;
9bf2229f
PJ
2340 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2341 return 0;
2342
5563e770
BP
2343 if (current->flags & PF_EXITING) /* Let dying task have memory */
2344 return 1;
2345
9bf2229f 2346 /* Not hardwall and node outside mems_allowed: scan up cpusets */
3d3f26a7 2347 mutex_lock(&callback_mutex);
053199ed 2348
053199ed 2349 task_lock(current);
78608366 2350 cs = nearest_hardwall_ancestor(task_cs(current));
053199ed
PJ
2351 task_unlock(current);
2352
9bf2229f 2353 allowed = node_isset(node, cs->mems_allowed);
3d3f26a7 2354 mutex_unlock(&callback_mutex);
9bf2229f 2355 return allowed;
1da177e4
LT
2356}
2357
02a0e53d 2358/*
a1bc5a4e
DR
2359 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2360 * @node: is this an allowed node?
02a0e53d
PJ
2361 * @gfp_mask: memory allocation flags
2362 *
a1bc5a4e
DR
2363 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2364 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2365 * yes. If the task has been OOM killed and has access to memory reserves as
2366 * specified by the TIF_MEMDIE flag, yes.
2367 * Otherwise, no.
02a0e53d
PJ
2368 *
2369 * The __GFP_THISNODE placement logic is really handled elsewhere,
2370 * by forcibly using a zonelist starting at a specified node, and by
2371 * (in get_page_from_freelist()) refusing to consider the zones for
2372 * any node on the zonelist except the first. By the time any such
2373 * calls get to this routine, we should just shut up and say 'yes'.
2374 *
a1bc5a4e
DR
2375 * Unlike the cpuset_node_allowed_softwall() variant, above,
2376 * this variant requires that the node be in the current task's
02a0e53d
PJ
2377 * mems_allowed or that we're in interrupt. It does not scan up the
2378 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2379 * It never sleeps.
2380 */
a1bc5a4e 2381int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
02a0e53d 2382{
02a0e53d
PJ
2383 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2384 return 1;
02a0e53d
PJ
2385 if (node_isset(node, current->mems_allowed))
2386 return 1;
dedf8b79
DW
2387 /*
2388 * Allow tasks that have access to memory reserves because they have
2389 * been OOM killed to get memory anywhere.
2390 */
2391 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2392 return 1;
02a0e53d
PJ
2393 return 0;
2394}
2395
505970b9
PJ
2396/**
2397 * cpuset_lock - lock out any changes to cpuset structures
2398 *
3d3f26a7 2399 * The out of memory (oom) code needs to mutex_lock cpusets
505970b9 2400 * from being changed while it scans the tasklist looking for a
3d3f26a7 2401 * task in an overlapping cpuset. Expose callback_mutex via this
505970b9
PJ
2402 * cpuset_lock() routine, so the oom code can lock it, before
2403 * locking the task list. The tasklist_lock is a spinlock, so
3d3f26a7 2404 * must be taken inside callback_mutex.
505970b9
PJ
2405 */
2406
2407void cpuset_lock(void)
2408{
3d3f26a7 2409 mutex_lock(&callback_mutex);
505970b9
PJ
2410}
2411
2412/**
2413 * cpuset_unlock - release lock on cpuset changes
2414 *
2415 * Undo the lock taken in a previous cpuset_lock() call.
2416 */
2417
2418void cpuset_unlock(void)
2419{
3d3f26a7 2420 mutex_unlock(&callback_mutex);
505970b9
PJ
2421}
2422
825a46af
PJ
2423/**
2424 * cpuset_mem_spread_node() - On which node to begin search for a page
2425 *
2426 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2427 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2428 * and if the memory allocation used cpuset_mem_spread_node()
2429 * to determine on which node to start looking, as it will for
2430 * certain page cache or slab cache pages such as used for file
2431 * system buffers and inode caches, then instead of starting on the
2432 * local node to look for a free page, rather spread the starting
2433 * node around the tasks mems_allowed nodes.
2434 *
2435 * We don't have to worry about the returned node being offline
2436 * because "it can't happen", and even if it did, it would be ok.
2437 *
2438 * The routines calling guarantee_online_mems() are careful to
2439 * only set nodes in task->mems_allowed that are online. So it
2440 * should not be possible for the following code to return an
2441 * offline node. But if it did, that would be ok, as this routine
2442 * is not returning the node where the allocation must be, only
2443 * the node where the search should start. The zonelist passed to
2444 * __alloc_pages() will include all nodes. If the slab allocator
2445 * is passed an offline node, it will fall back to the local node.
2446 * See kmem_cache_alloc_node().
2447 */
2448
2449int cpuset_mem_spread_node(void)
2450{
2451 int node;
2452
2453 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2454 if (node == MAX_NUMNODES)
2455 node = first_node(current->mems_allowed);
2456 current->cpuset_mem_spread_rotor = node;
2457 return node;
2458}
2459EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2460
ef08e3b4 2461/**
bbe373f2
DR
2462 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2463 * @tsk1: pointer to task_struct of some task.
2464 * @tsk2: pointer to task_struct of some other task.
2465 *
2466 * Description: Return true if @tsk1's mems_allowed intersects the
2467 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2468 * one of the task's memory usage might impact the memory available
2469 * to the other.
ef08e3b4
PJ
2470 **/
2471
bbe373f2
DR
2472int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2473 const struct task_struct *tsk2)
ef08e3b4 2474{
bbe373f2 2475 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
ef08e3b4
PJ
2476}
2477
75aa1994
DR
2478/**
2479 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2480 * @task: pointer to task_struct of some task.
2481 *
2482 * Description: Prints @task's name, cpuset name, and cached copy of its
2483 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2484 * dereferencing task_cs(task).
2485 */
2486void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2487{
2488 struct dentry *dentry;
2489
2490 dentry = task_cs(tsk)->css.cgroup->dentry;
2491 spin_lock(&cpuset_buffer_lock);
2492 snprintf(cpuset_name, CPUSET_NAME_LEN,
2493 dentry ? (const char *)dentry->d_name.name : "/");
2494 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2495 tsk->mems_allowed);
2496 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2497 tsk->comm, cpuset_name, cpuset_nodelist);
2498 spin_unlock(&cpuset_buffer_lock);
2499}
2500
3e0d98b9
PJ
2501/*
2502 * Collection of memory_pressure is suppressed unless
2503 * this flag is enabled by writing "1" to the special
2504 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2505 */
2506
c5b2aff8 2507int cpuset_memory_pressure_enabled __read_mostly;
3e0d98b9
PJ
2508
2509/**
2510 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2511 *
2512 * Keep a running average of the rate of synchronous (direct)
2513 * page reclaim efforts initiated by tasks in each cpuset.
2514 *
2515 * This represents the rate at which some task in the cpuset
2516 * ran low on memory on all nodes it was allowed to use, and
2517 * had to enter the kernels page reclaim code in an effort to
2518 * create more free memory by tossing clean pages or swapping
2519 * or writing dirty pages.
2520 *
2521 * Display to user space in the per-cpuset read-only file
2522 * "memory_pressure". Value displayed is an integer
2523 * representing the recent rate of entry into the synchronous
2524 * (direct) page reclaim by any task attached to the cpuset.
2525 **/
2526
2527void __cpuset_memory_pressure_bump(void)
2528{
3e0d98b9 2529 task_lock(current);
8793d854 2530 fmeter_markevent(&task_cs(current)->fmeter);
3e0d98b9
PJ
2531 task_unlock(current);
2532}
2533
8793d854 2534#ifdef CONFIG_PROC_PID_CPUSET
1da177e4
LT
2535/*
2536 * proc_cpuset_show()
2537 * - Print tasks cpuset path into seq_file.
2538 * - Used for /proc/<pid>/cpuset.
053199ed
PJ
2539 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2540 * doesn't really matter if tsk->cpuset changes after we read it,
c8d9c90c 2541 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
2df167a3 2542 * anyway.
1da177e4 2543 */
029190c5 2544static int proc_cpuset_show(struct seq_file *m, void *unused_v)
1da177e4 2545{
13b41b09 2546 struct pid *pid;
1da177e4
LT
2547 struct task_struct *tsk;
2548 char *buf;
8793d854 2549 struct cgroup_subsys_state *css;
99f89551 2550 int retval;
1da177e4 2551
99f89551 2552 retval = -ENOMEM;
1da177e4
LT
2553 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2554 if (!buf)
99f89551
EB
2555 goto out;
2556
2557 retval = -ESRCH;
13b41b09
EB
2558 pid = m->private;
2559 tsk = get_pid_task(pid, PIDTYPE_PID);
99f89551
EB
2560 if (!tsk)
2561 goto out_free;
1da177e4 2562
99f89551 2563 retval = -EINVAL;
8793d854
PM
2564 cgroup_lock();
2565 css = task_subsys_state(tsk, cpuset_subsys_id);
2566 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
1da177e4 2567 if (retval < 0)
99f89551 2568 goto out_unlock;
1da177e4
LT
2569 seq_puts(m, buf);
2570 seq_putc(m, '\n');
99f89551 2571out_unlock:
8793d854 2572 cgroup_unlock();
99f89551
EB
2573 put_task_struct(tsk);
2574out_free:
1da177e4 2575 kfree(buf);
99f89551 2576out:
1da177e4
LT
2577 return retval;
2578}
2579
2580static int cpuset_open(struct inode *inode, struct file *file)
2581{
13b41b09
EB
2582 struct pid *pid = PROC_I(inode)->pid;
2583 return single_open(file, proc_cpuset_show, pid);
1da177e4
LT
2584}
2585
9a32144e 2586const struct file_operations proc_cpuset_operations = {
1da177e4
LT
2587 .open = cpuset_open,
2588 .read = seq_read,
2589 .llseek = seq_lseek,
2590 .release = single_release,
2591};
8793d854 2592#endif /* CONFIG_PROC_PID_CPUSET */
1da177e4
LT
2593
2594/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
df5f8314
EB
2595void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
2596{
2597 seq_printf(m, "Cpus_allowed:\t");
30e8e136 2598 seq_cpumask(m, &task->cpus_allowed);
df5f8314 2599 seq_printf(m, "\n");
39106dcf 2600 seq_printf(m, "Cpus_allowed_list:\t");
30e8e136 2601 seq_cpumask_list(m, &task->cpus_allowed);
39106dcf 2602 seq_printf(m, "\n");
df5f8314 2603 seq_printf(m, "Mems_allowed:\t");
30e8e136 2604 seq_nodemask(m, &task->mems_allowed);
df5f8314 2605 seq_printf(m, "\n");
39106dcf 2606 seq_printf(m, "Mems_allowed_list:\t");
30e8e136 2607 seq_nodemask_list(m, &task->mems_allowed);
39106dcf 2608 seq_printf(m, "\n");
1da177e4 2609}