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