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