oom: suppress extraneous stack and memory dump
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / cpuset.c
CommitLineData
1da177e4
LT
1/*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
825a46af 7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
1da177e4
LT
8 *
9 * Portions derived from Patrick Mochel's sysfs code.
10 * sysfs is Copyright (c) 2001-3 Patrick Mochel
1da177e4 11 *
825a46af 12 * 2003-10-10 Written by Simon Derr.
1da177e4 13 * 2003-10-22 Updates by Stephen Hemminger.
825a46af 14 * 2004 May-July Rework by Paul Jackson.
1da177e4
LT
15 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file COPYING in the main directory of the Linux
18 * distribution for more details.
19 */
20
1da177e4
LT
21#include <linux/cpu.h>
22#include <linux/cpumask.h>
23#include <linux/cpuset.h>
24#include <linux/err.h>
25#include <linux/errno.h>
26#include <linux/file.h>
27#include <linux/fs.h>
28#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/kernel.h>
31#include <linux/kmod.h>
32#include <linux/list.h>
68860ec1 33#include <linux/mempolicy.h>
1da177e4
LT
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/mount.h>
37#include <linux/namei.h>
38#include <linux/pagemap.h>
39#include <linux/proc_fs.h>
6b9c2603 40#include <linux/rcupdate.h>
1da177e4
LT
41#include <linux/sched.h>
42#include <linux/seq_file.h>
22fb52dd 43#include <linux/security.h>
1da177e4 44#include <linux/slab.h>
1da177e4
LT
45#include <linux/spinlock.h>
46#include <linux/stat.h>
47#include <linux/string.h>
48#include <linux/time.h>
49#include <linux/backing-dev.h>
50#include <linux/sort.h>
51
52#include <asm/uaccess.h>
53#include <asm/atomic.h>
3d3f26a7 54#include <linux/mutex.h>
1da177e4 55
c5b2aff8 56#define CPUSET_SUPER_MAGIC 0x27e0eb
1da177e4 57
202f72d5
PJ
58/*
59 * Tracks how many cpusets are currently defined in system.
60 * When there is only one cpuset (the root cpuset) we can
61 * short circuit some hooks.
62 */
7edc5962 63int number_of_cpusets __read_mostly;
202f72d5 64
3e0d98b9
PJ
65/* See "Frequency meter" comments, below. */
66
67struct fmeter {
68 int cnt; /* unprocessed events count */
69 int val; /* most recent output value */
70 time_t time; /* clock (secs) when val computed */
71 spinlock_t lock; /* guards read or write of above */
72};
73
1da177e4
LT
74struct cpuset {
75 unsigned long flags; /* "unsigned long" so bitops work */
76 cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
77 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
78
053199ed
PJ
79 /*
80 * Count is atomic so can incr (fork) or decr (exit) without a lock.
81 */
1da177e4
LT
82 atomic_t count; /* count tasks using this cpuset */
83
84 /*
85 * We link our 'sibling' struct into our parents 'children'.
86 * Our children link their 'sibling' into our 'children'.
87 */
88 struct list_head sibling; /* my parents children */
89 struct list_head children; /* my children */
90
91 struct cpuset *parent; /* my parent */
92 struct dentry *dentry; /* cpuset fs entry */
93
94 /*
95 * Copy of global cpuset_mems_generation as of the most
96 * recent time this cpuset changed its mems_allowed.
97 */
3e0d98b9
PJ
98 int mems_generation;
99
100 struct fmeter fmeter; /* memory_pressure filter */
1da177e4
LT
101};
102
103/* bits in struct cpuset flags field */
104typedef enum {
105 CS_CPU_EXCLUSIVE,
106 CS_MEM_EXCLUSIVE,
45b07ef3 107 CS_MEMORY_MIGRATE,
1da177e4 108 CS_REMOVED,
825a46af
PJ
109 CS_NOTIFY_ON_RELEASE,
110 CS_SPREAD_PAGE,
111 CS_SPREAD_SLAB,
1da177e4
LT
112} cpuset_flagbits_t;
113
114/* convenient tests for these bits */
115static inline int is_cpu_exclusive(const struct cpuset *cs)
116{
7b5b9ef0 117 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
1da177e4
LT
118}
119
120static inline int is_mem_exclusive(const struct cpuset *cs)
121{
7b5b9ef0 122 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
1da177e4
LT
123}
124
125static inline int is_removed(const struct cpuset *cs)
126{
7b5b9ef0 127 return test_bit(CS_REMOVED, &cs->flags);
1da177e4
LT
128}
129
130static inline int notify_on_release(const struct cpuset *cs)
131{
7b5b9ef0 132 return test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
1da177e4
LT
133}
134
45b07ef3
PJ
135static inline int is_memory_migrate(const struct cpuset *cs)
136{
7b5b9ef0 137 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
45b07ef3
PJ
138}
139
825a46af
PJ
140static inline int is_spread_page(const struct cpuset *cs)
141{
142 return test_bit(CS_SPREAD_PAGE, &cs->flags);
143}
144
145static inline int is_spread_slab(const struct cpuset *cs)
146{
147 return test_bit(CS_SPREAD_SLAB, &cs->flags);
148}
149
1da177e4 150/*
151a4420 151 * Increment this integer everytime any cpuset changes its
1da177e4
LT
152 * mems_allowed value. Users of cpusets can track this generation
153 * number, and avoid having to lock and reload mems_allowed unless
154 * the cpuset they're using changes generation.
155 *
156 * A single, global generation is needed because attach_task() could
157 * reattach a task to a different cpuset, which must not have its
158 * generation numbers aliased with those of that tasks previous cpuset.
159 *
160 * Generations are needed for mems_allowed because one task cannot
161 * modify anothers memory placement. So we must enable every task,
162 * on every visit to __alloc_pages(), to efficiently check whether
163 * its current->cpuset->mems_allowed has changed, requiring an update
164 * of its current->mems_allowed.
151a4420
PJ
165 *
166 * Since cpuset_mems_generation is guarded by manage_mutex,
167 * there is no need to mark it atomic.
1da177e4 168 */
151a4420 169static int cpuset_mems_generation;
1da177e4
LT
170
171static struct cpuset top_cpuset = {
172 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
173 .cpus_allowed = CPU_MASK_ALL,
174 .mems_allowed = NODE_MASK_ALL,
175 .count = ATOMIC_INIT(0),
176 .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
177 .children = LIST_HEAD_INIT(top_cpuset.children),
1da177e4
LT
178};
179
180static struct vfsmount *cpuset_mount;
3e0d98b9 181static struct super_block *cpuset_sb;
1da177e4
LT
182
183/*
3d3f26a7
IM
184 * We have two global cpuset mutexes below. They can nest.
185 * It is ok to first take manage_mutex, then nest callback_mutex. We also
053199ed
PJ
186 * require taking task_lock() when dereferencing a tasks cpuset pointer.
187 * See "The task_lock() exception", at the end of this comment.
188 *
3d3f26a7
IM
189 * A task must hold both mutexes to modify cpusets. If a task
190 * holds manage_mutex, then it blocks others wanting that mutex,
191 * ensuring that it is the only task able to also acquire callback_mutex
053199ed
PJ
192 * and be able to modify cpusets. It can perform various checks on
193 * the cpuset structure first, knowing nothing will change. It can
3d3f26a7 194 * also allocate memory while just holding manage_mutex. While it is
053199ed 195 * performing these checks, various callback routines can briefly
3d3f26a7
IM
196 * acquire callback_mutex to query cpusets. Once it is ready to make
197 * the changes, it takes callback_mutex, blocking everyone else.
053199ed
PJ
198 *
199 * Calls to the kernel memory allocator can not be made while holding
3d3f26a7 200 * callback_mutex, as that would risk double tripping on callback_mutex
053199ed
PJ
201 * from one of the callbacks into the cpuset code from within
202 * __alloc_pages().
203 *
3d3f26a7 204 * If a task is only holding callback_mutex, then it has read-only
053199ed
PJ
205 * access to cpusets.
206 *
207 * The task_struct fields mems_allowed and mems_generation may only
208 * be accessed in the context of that task, so require no locks.
209 *
210 * Any task can increment and decrement the count field without lock.
3d3f26a7 211 * So in general, code holding manage_mutex or callback_mutex can't rely
053199ed 212 * on the count field not changing. However, if the count goes to
3d3f26a7 213 * zero, then only attach_task(), which holds both mutexes, can
053199ed
PJ
214 * increment it again. Because a count of zero means that no tasks
215 * are currently attached, therefore there is no way a task attached
216 * to that cpuset can fork (the other way to increment the count).
3d3f26a7 217 * So code holding manage_mutex or callback_mutex can safely assume that
053199ed 218 * if the count is zero, it will stay zero. Similarly, if a task
3d3f26a7 219 * holds manage_mutex or callback_mutex on a cpuset with zero count, it
053199ed 220 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
3d3f26a7 221 * both of those mutexes.
053199ed
PJ
222 *
223 * The cpuset_common_file_write handler for operations that modify
3d3f26a7 224 * the cpuset hierarchy holds manage_mutex across the entire operation,
053199ed
PJ
225 * single threading all such cpuset modifications across the system.
226 *
3d3f26a7 227 * The cpuset_common_file_read() handlers only hold callback_mutex across
053199ed
PJ
228 * small pieces of code, such as when reading out possibly multi-word
229 * cpumasks and nodemasks.
230 *
231 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
3d3f26a7 232 * (usually) take either mutex. These are the two most performance
053199ed 233 * critical pieces of code here. The exception occurs on cpuset_exit(),
3d3f26a7 234 * when a task in a notify_on_release cpuset exits. Then manage_mutex
2efe86b8 235 * is taken, and if the cpuset count is zero, a usermode call made
1da177e4
LT
236 * to /sbin/cpuset_release_agent with the name of the cpuset (path
237 * relative to the root of cpuset file system) as the argument.
238 *
053199ed
PJ
239 * A cpuset can only be deleted if both its 'count' of using tasks
240 * is zero, and its list of 'children' cpusets is empty. Since all
241 * tasks in the system use _some_ cpuset, and since there is always at
f400e198 242 * least one task in the system (init), therefore, top_cpuset
053199ed
PJ
243 * always has either children cpusets and/or using tasks. So we don't
244 * need a special hack to ensure that top_cpuset cannot be deleted.
245 *
246 * The above "Tale of Two Semaphores" would be complete, but for:
247 *
248 * The task_lock() exception
249 *
250 * The need for this exception arises from the action of attach_task(),
251 * which overwrites one tasks cpuset pointer with another. It does
3d3f26a7 252 * so using both mutexes, however there are several performance
053199ed 253 * critical places that need to reference task->cpuset without the
3d3f26a7 254 * expense of grabbing a system global mutex. Therefore except as
053199ed
PJ
255 * noted below, when dereferencing or, as in attach_task(), modifying
256 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
257 * (task->alloc_lock) already in the task_struct routinely used for
258 * such matters.
6b9c2603
PJ
259 *
260 * P.S. One more locking exception. RCU is used to guard the
261 * update of a tasks cpuset pointer by attach_task() and the
262 * access of task->cpuset->mems_generation via that pointer in
263 * the routine cpuset_update_task_memory_state().
1da177e4
LT
264 */
265
3d3f26a7
IM
266static DEFINE_MUTEX(manage_mutex);
267static DEFINE_MUTEX(callback_mutex);
4247bdc6 268
1da177e4
LT
269/*
270 * A couple of forward declarations required, due to cyclic reference loop:
271 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
272 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
273 */
274
275static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
276static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
277
278static struct backing_dev_info cpuset_backing_dev_info = {
279 .ra_pages = 0, /* No readahead */
280 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
281};
282
283static struct inode *cpuset_new_inode(mode_t mode)
284{
285 struct inode *inode = new_inode(cpuset_sb);
286
287 if (inode) {
288 inode->i_mode = mode;
289 inode->i_uid = current->fsuid;
290 inode->i_gid = current->fsgid;
1da177e4
LT
291 inode->i_blocks = 0;
292 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
293 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
294 }
295 return inode;
296}
297
298static void cpuset_diput(struct dentry *dentry, struct inode *inode)
299{
300 /* is dentry a directory ? if so, kfree() associated cpuset */
301 if (S_ISDIR(inode->i_mode)) {
302 struct cpuset *cs = dentry->d_fsdata;
303 BUG_ON(!(is_removed(cs)));
304 kfree(cs);
305 }
306 iput(inode);
307}
308
309static struct dentry_operations cpuset_dops = {
310 .d_iput = cpuset_diput,
311};
312
313static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
314{
5f45f1a7 315 struct dentry *d = lookup_one_len(name, parent, strlen(name));
1da177e4
LT
316 if (!IS_ERR(d))
317 d->d_op = &cpuset_dops;
318 return d;
319}
320
321static void remove_dir(struct dentry *d)
322{
323 struct dentry *parent = dget(d->d_parent);
324
325 d_delete(d);
326 simple_rmdir(parent->d_inode, d);
327 dput(parent);
328}
329
330/*
331 * NOTE : the dentry must have been dget()'ed
332 */
333static void cpuset_d_remove_dir(struct dentry *dentry)
334{
335 struct list_head *node;
336
337 spin_lock(&dcache_lock);
338 node = dentry->d_subdirs.next;
339 while (node != &dentry->d_subdirs) {
5160ee6f 340 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
1da177e4
LT
341 list_del_init(node);
342 if (d->d_inode) {
343 d = dget_locked(d);
344 spin_unlock(&dcache_lock);
345 d_delete(d);
346 simple_unlink(dentry->d_inode, d);
347 dput(d);
348 spin_lock(&dcache_lock);
349 }
350 node = dentry->d_subdirs.next;
351 }
5160ee6f 352 list_del_init(&dentry->d_u.d_child);
1da177e4
LT
353 spin_unlock(&dcache_lock);
354 remove_dir(dentry);
355}
356
357static struct super_operations cpuset_ops = {
358 .statfs = simple_statfs,
359 .drop_inode = generic_delete_inode,
360};
361
362static int cpuset_fill_super(struct super_block *sb, void *unused_data,
363 int unused_silent)
364{
365 struct inode *inode;
366 struct dentry *root;
367
368 sb->s_blocksize = PAGE_CACHE_SIZE;
369 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
370 sb->s_magic = CPUSET_SUPER_MAGIC;
371 sb->s_op = &cpuset_ops;
372 cpuset_sb = sb;
373
374 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
375 if (inode) {
376 inode->i_op = &simple_dir_inode_operations;
377 inode->i_fop = &simple_dir_operations;
378 /* directories start off with i_nlink == 2 (for "." entry) */
d8c76e6f 379 inc_nlink(inode);
1da177e4
LT
380 } else {
381 return -ENOMEM;
382 }
383
384 root = d_alloc_root(inode);
385 if (!root) {
386 iput(inode);
387 return -ENOMEM;
388 }
389 sb->s_root = root;
390 return 0;
391}
392
454e2398
DH
393static int cpuset_get_sb(struct file_system_type *fs_type,
394 int flags, const char *unused_dev_name,
395 void *data, struct vfsmount *mnt)
1da177e4 396{
454e2398 397 return get_sb_single(fs_type, flags, data, cpuset_fill_super, mnt);
1da177e4
LT
398}
399
400static struct file_system_type cpuset_fs_type = {
401 .name = "cpuset",
402 .get_sb = cpuset_get_sb,
403 .kill_sb = kill_litter_super,
404};
405
406/* struct cftype:
407 *
408 * The files in the cpuset filesystem mostly have a very simple read/write
409 * handling, some common function will take care of it. Nevertheless some cases
410 * (read tasks) are special and therefore I define this structure for every
411 * kind of file.
412 *
413 *
414 * When reading/writing to a file:
a7a005fd
JS
415 * - the cpuset to use in file->f_path.dentry->d_parent->d_fsdata
416 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
1da177e4
LT
417 */
418
419struct cftype {
420 char *name;
421 int private;
422 int (*open) (struct inode *inode, struct file *file);
423 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
424 loff_t *ppos);
425 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
426 loff_t *ppos);
427 int (*release) (struct inode *inode, struct file *file);
428};
429
430static inline struct cpuset *__d_cs(struct dentry *dentry)
431{
432 return dentry->d_fsdata;
433}
434
435static inline struct cftype *__d_cft(struct dentry *dentry)
436{
437 return dentry->d_fsdata;
438}
439
440/*
3d3f26a7 441 * Call with manage_mutex held. Writes path of cpuset into buf.
1da177e4
LT
442 * Returns 0 on success, -errno on error.
443 */
444
445static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
446{
447 char *start;
448
449 start = buf + buflen;
450
451 *--start = '\0';
452 for (;;) {
453 int len = cs->dentry->d_name.len;
454 if ((start -= len) < buf)
455 return -ENAMETOOLONG;
456 memcpy(start, cs->dentry->d_name.name, len);
457 cs = cs->parent;
458 if (!cs)
459 break;
460 if (!cs->parent)
461 continue;
462 if (--start < buf)
463 return -ENAMETOOLONG;
464 *start = '/';
465 }
466 memmove(buf, start, buf + buflen - start);
467 return 0;
468}
469
470/*
471 * Notify userspace when a cpuset is released, by running
472 * /sbin/cpuset_release_agent with the name of the cpuset (path
473 * relative to the root of cpuset file system) as the argument.
474 *
475 * Most likely, this user command will try to rmdir this cpuset.
476 *
477 * This races with the possibility that some other task will be
478 * attached to this cpuset before it is removed, or that some other
479 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
480 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
481 * unused, and this cpuset will be reprieved from its death sentence,
482 * to continue to serve a useful existence. Next time it's released,
483 * we will get notified again, if it still has 'notify_on_release' set.
484 *
3077a260
PJ
485 * The final arg to call_usermodehelper() is 0, which means don't
486 * wait. The separate /sbin/cpuset_release_agent task is forked by
487 * call_usermodehelper(), then control in this thread returns here,
488 * without waiting for the release agent task. We don't bother to
489 * wait because the caller of this routine has no use for the exit
490 * status of the /sbin/cpuset_release_agent task, so no sense holding
491 * our caller up for that.
492 *
3d3f26a7 493 * When we had only one cpuset mutex, we had to call this
053199ed
PJ
494 * without holding it, to avoid deadlock when call_usermodehelper()
495 * allocated memory. With two locks, we could now call this while
3d3f26a7
IM
496 * holding manage_mutex, but we still don't, so as to minimize
497 * the time manage_mutex is held.
1da177e4
LT
498 */
499
3077a260 500static void cpuset_release_agent(const char *pathbuf)
1da177e4
LT
501{
502 char *argv[3], *envp[3];
503 int i;
504
3077a260
PJ
505 if (!pathbuf)
506 return;
507
1da177e4
LT
508 i = 0;
509 argv[i++] = "/sbin/cpuset_release_agent";
3077a260 510 argv[i++] = (char *)pathbuf;
1da177e4
LT
511 argv[i] = NULL;
512
513 i = 0;
514 /* minimal command environment */
515 envp[i++] = "HOME=/";
516 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
517 envp[i] = NULL;
518
86313c48 519 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3077a260 520 kfree(pathbuf);
1da177e4
LT
521}
522
523/*
524 * Either cs->count of using tasks transitioned to zero, or the
525 * cs->children list of child cpusets just became empty. If this
526 * cs is notify_on_release() and now both the user count is zero and
3077a260
PJ
527 * the list of children is empty, prepare cpuset path in a kmalloc'd
528 * buffer, to be returned via ppathbuf, so that the caller can invoke
3d3f26a7
IM
529 * cpuset_release_agent() with it later on, once manage_mutex is dropped.
530 * Call here with manage_mutex held.
3077a260
PJ
531 *
532 * This check_for_release() routine is responsible for kmalloc'ing
533 * pathbuf. The above cpuset_release_agent() is responsible for
534 * kfree'ing pathbuf. The caller of these routines is responsible
535 * for providing a pathbuf pointer, initialized to NULL, then
3d3f26a7
IM
536 * calling check_for_release() with manage_mutex held and the address
537 * of the pathbuf pointer, then dropping manage_mutex, then calling
3077a260 538 * cpuset_release_agent() with pathbuf, as set by check_for_release().
1da177e4
LT
539 */
540
3077a260 541static void check_for_release(struct cpuset *cs, char **ppathbuf)
1da177e4
LT
542{
543 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
544 list_empty(&cs->children)) {
545 char *buf;
546
547 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
548 if (!buf)
549 return;
550 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
3077a260
PJ
551 kfree(buf);
552 else
553 *ppathbuf = buf;
1da177e4
LT
554 }
555}
556
557/*
558 * Return in *pmask the portion of a cpusets's cpus_allowed that
559 * are online. If none are online, walk up the cpuset hierarchy
560 * until we find one that does have some online cpus. If we get
561 * all the way to the top and still haven't found any online cpus,
562 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
563 * task, return cpu_online_map.
564 *
565 * One way or another, we guarantee to return some non-empty subset
566 * of cpu_online_map.
567 *
3d3f26a7 568 * Call with callback_mutex held.
1da177e4
LT
569 */
570
571static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
572{
573 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
574 cs = cs->parent;
575 if (cs)
576 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
577 else
578 *pmask = cpu_online_map;
579 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
580}
581
582/*
583 * Return in *pmask the portion of a cpusets's mems_allowed that
0e1e7c7a
CL
584 * are online, with memory. If none are online with memory, walk
585 * up the cpuset hierarchy until we find one that does have some
586 * online mems. If we get all the way to the top and still haven't
587 * found any online mems, return node_states[N_HIGH_MEMORY].
1da177e4
LT
588 *
589 * One way or another, we guarantee to return some non-empty subset
0e1e7c7a 590 * of node_states[N_HIGH_MEMORY].
1da177e4 591 *
3d3f26a7 592 * Call with callback_mutex held.
1da177e4
LT
593 */
594
595static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
596{
0e1e7c7a
CL
597 while (cs && !nodes_intersects(cs->mems_allowed,
598 node_states[N_HIGH_MEMORY]))
1da177e4
LT
599 cs = cs->parent;
600 if (cs)
0e1e7c7a
CL
601 nodes_and(*pmask, cs->mems_allowed,
602 node_states[N_HIGH_MEMORY]);
1da177e4 603 else
0e1e7c7a
CL
604 *pmask = node_states[N_HIGH_MEMORY];
605 BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
1da177e4
LT
606}
607
cf2a473c
PJ
608/**
609 * cpuset_update_task_memory_state - update task memory placement
610 *
611 * If the current tasks cpusets mems_allowed changed behind our
612 * backs, update current->mems_allowed, mems_generation and task NUMA
613 * mempolicy to the new value.
053199ed 614 *
cf2a473c
PJ
615 * Task mempolicy is updated by rebinding it relative to the
616 * current->cpuset if a task has its memory placement changed.
617 * Do not call this routine if in_interrupt().
618 *
4a01c8d5
PJ
619 * Call without callback_mutex or task_lock() held. May be
620 * called with or without manage_mutex held. Thanks in part to
621 * 'the_top_cpuset_hack', the tasks cpuset pointer will never
622 * be NULL. This routine also might acquire callback_mutex and
cf2a473c 623 * current->mm->mmap_sem during call.
053199ed 624 *
6b9c2603
PJ
625 * Reading current->cpuset->mems_generation doesn't need task_lock
626 * to guard the current->cpuset derefence, because it is guarded
627 * from concurrent freeing of current->cpuset by attach_task(),
628 * using RCU.
629 *
630 * The rcu_dereference() is technically probably not needed,
631 * as I don't actually mind if I see a new cpuset pointer but
632 * an old value of mems_generation. However this really only
633 * matters on alpha systems using cpusets heavily. If I dropped
634 * that rcu_dereference(), it would save them a memory barrier.
635 * For all other arch's, rcu_dereference is a no-op anyway, and for
636 * alpha systems not using cpusets, another planned optimization,
637 * avoiding the rcu critical section for tasks in the root cpuset
638 * which is statically allocated, so can't vanish, will make this
639 * irrelevant. Better to use RCU as intended, than to engage in
640 * some cute trick to save a memory barrier that is impossible to
641 * test, for alpha systems using cpusets heavily, which might not
642 * even exist.
053199ed
PJ
643 *
644 * This routine is needed to update the per-task mems_allowed data,
645 * within the tasks context, when it is trying to allocate memory
646 * (in various mm/mempolicy.c routines) and notices that some other
647 * task has been modifying its cpuset.
1da177e4
LT
648 */
649
fe85a998 650void cpuset_update_task_memory_state(void)
1da177e4 651{
053199ed 652 int my_cpusets_mem_gen;
cf2a473c 653 struct task_struct *tsk = current;
6b9c2603 654 struct cpuset *cs;
053199ed 655
03a285f5
PJ
656 if (tsk->cpuset == &top_cpuset) {
657 /* Don't need rcu for top_cpuset. It's never freed. */
658 my_cpusets_mem_gen = top_cpuset.mems_generation;
659 } else {
660 rcu_read_lock();
661 cs = rcu_dereference(tsk->cpuset);
662 my_cpusets_mem_gen = cs->mems_generation;
663 rcu_read_unlock();
664 }
1da177e4 665
cf2a473c 666 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
3d3f26a7 667 mutex_lock(&callback_mutex);
cf2a473c
PJ
668 task_lock(tsk);
669 cs = tsk->cpuset; /* Maybe changed when task not locked */
cf2a473c
PJ
670 guarantee_online_mems(cs, &tsk->mems_allowed);
671 tsk->cpuset_mems_generation = cs->mems_generation;
825a46af
PJ
672 if (is_spread_page(cs))
673 tsk->flags |= PF_SPREAD_PAGE;
674 else
675 tsk->flags &= ~PF_SPREAD_PAGE;
676 if (is_spread_slab(cs))
677 tsk->flags |= PF_SPREAD_SLAB;
678 else
679 tsk->flags &= ~PF_SPREAD_SLAB;
cf2a473c 680 task_unlock(tsk);
3d3f26a7 681 mutex_unlock(&callback_mutex);
74cb2155 682 mpol_rebind_task(tsk, &tsk->mems_allowed);
1da177e4
LT
683 }
684}
685
686/*
687 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
688 *
689 * One cpuset is a subset of another if all its allowed CPUs and
690 * Memory Nodes are a subset of the other, and its exclusive flags
3d3f26a7 691 * are only set if the other's are set. Call holding manage_mutex.
1da177e4
LT
692 */
693
694static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
695{
696 return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
697 nodes_subset(p->mems_allowed, q->mems_allowed) &&
698 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
699 is_mem_exclusive(p) <= is_mem_exclusive(q);
700}
701
702/*
703 * validate_change() - Used to validate that any proposed cpuset change
704 * follows the structural rules for cpusets.
705 *
706 * If we replaced the flag and mask values of the current cpuset
707 * (cur) with those values in the trial cpuset (trial), would
708 * our various subset and exclusive rules still be valid? Presumes
3d3f26a7 709 * manage_mutex held.
1da177e4
LT
710 *
711 * 'cur' is the address of an actual, in-use cpuset. Operations
712 * such as list traversal that depend on the actual address of the
713 * cpuset in the list must use cur below, not trial.
714 *
715 * 'trial' is the address of bulk structure copy of cur, with
716 * perhaps one or more of the fields cpus_allowed, mems_allowed,
717 * or flags changed to new, trial values.
718 *
719 * Return 0 if valid, -errno if not.
720 */
721
722static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
723{
724 struct cpuset *c, *par;
725
726 /* Each of our child cpusets must be a subset of us */
727 list_for_each_entry(c, &cur->children, sibling) {
728 if (!is_cpuset_subset(c, trial))
729 return -EBUSY;
730 }
731
732 /* Remaining checks don't apply to root cpuset */
69604067 733 if (cur == &top_cpuset)
1da177e4
LT
734 return 0;
735
69604067
PJ
736 par = cur->parent;
737
1da177e4
LT
738 /* We must be a subset of our parent cpuset */
739 if (!is_cpuset_subset(trial, par))
740 return -EACCES;
741
742 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
743 list_for_each_entry(c, &par->children, sibling) {
744 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
745 c != cur &&
746 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
747 return -EINVAL;
748 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
749 c != cur &&
750 nodes_intersects(trial->mems_allowed, c->mems_allowed))
751 return -EINVAL;
752 }
753
754 return 0;
755}
756
053199ed 757/*
3d3f26a7 758 * Call with manage_mutex held. May take callback_mutex during call.
053199ed
PJ
759 */
760
1da177e4
LT
761static int update_cpumask(struct cpuset *cs, char *buf)
762{
763 struct cpuset trialcs;
607717a6 764 int retval;
1da177e4 765
4c4d50f7
PJ
766 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
767 if (cs == &top_cpuset)
768 return -EACCES;
769
1da177e4 770 trialcs = *cs;
6f7f02e7
DR
771
772 /*
773 * We allow a cpuset's cpus_allowed to be empty; if it has attached
774 * tasks, we'll catch it later when we validate the change and return
775 * -ENOSPC.
776 */
777 if (!buf[0] || (buf[0] == '\n' && !buf[1])) {
778 cpus_clear(trialcs.cpus_allowed);
779 } else {
780 retval = cpulist_parse(buf, trialcs.cpus_allowed);
781 if (retval < 0)
782 return retval;
783 }
1da177e4 784 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
6f7f02e7
DR
785 /* cpus_allowed cannot be empty for a cpuset with attached tasks. */
786 if (atomic_read(&cs->count) && cpus_empty(trialcs.cpus_allowed))
1da177e4
LT
787 return -ENOSPC;
788 retval = validate_change(cs, &trialcs);
85d7b949
DG
789 if (retval < 0)
790 return retval;
3d3f26a7 791 mutex_lock(&callback_mutex);
85d7b949 792 cs->cpus_allowed = trialcs.cpus_allowed;
3d3f26a7 793 mutex_unlock(&callback_mutex);
85d7b949 794 return 0;
1da177e4
LT
795}
796
e4e364e8
PJ
797/*
798 * cpuset_migrate_mm
799 *
800 * Migrate memory region from one set of nodes to another.
801 *
802 * Temporarilly set tasks mems_allowed to target nodes of migration,
803 * so that the migration code can allocate pages on these nodes.
804 *
805 * Call holding manage_mutex, so our current->cpuset won't change
806 * during this call, as manage_mutex holds off any attach_task()
807 * calls. Therefore we don't need to take task_lock around the
808 * call to guarantee_online_mems(), as we know no one is changing
809 * our tasks cpuset.
810 *
811 * Hold callback_mutex around the two modifications of our tasks
812 * mems_allowed to synchronize with cpuset_mems_allowed().
813 *
814 * While the mm_struct we are migrating is typically from some
815 * other task, the task_struct mems_allowed that we are hacking
816 * is for our current task, which must allocate new pages for that
817 * migrating memory region.
818 *
819 * We call cpuset_update_task_memory_state() before hacking
820 * our tasks mems_allowed, so that we are assured of being in
821 * sync with our tasks cpuset, and in particular, callbacks to
822 * cpuset_update_task_memory_state() from nested page allocations
823 * won't see any mismatch of our cpuset and task mems_generation
824 * values, so won't overwrite our hacked tasks mems_allowed
825 * nodemask.
826 */
827
828static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
829 const nodemask_t *to)
830{
831 struct task_struct *tsk = current;
832
833 cpuset_update_task_memory_state();
834
835 mutex_lock(&callback_mutex);
836 tsk->mems_allowed = *to;
837 mutex_unlock(&callback_mutex);
838
839 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
840
841 mutex_lock(&callback_mutex);
842 guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed);
843 mutex_unlock(&callback_mutex);
844}
845
053199ed 846/*
4225399a
PJ
847 * Handle user request to change the 'mems' memory placement
848 * of a cpuset. Needs to validate the request, update the
849 * cpusets mems_allowed and mems_generation, and for each
04c19fa6
PJ
850 * task in the cpuset, rebind any vma mempolicies and if
851 * the cpuset is marked 'memory_migrate', migrate the tasks
852 * pages to the new memory.
4225399a 853 *
3d3f26a7 854 * Call with manage_mutex held. May take callback_mutex during call.
4225399a
PJ
855 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
856 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
857 * their mempolicies to the cpusets new mems_allowed.
053199ed
PJ
858 */
859
1da177e4
LT
860static int update_nodemask(struct cpuset *cs, char *buf)
861{
862 struct cpuset trialcs;
04c19fa6 863 nodemask_t oldmem;
4225399a
PJ
864 struct task_struct *g, *p;
865 struct mm_struct **mmarray;
866 int i, n, ntasks;
04c19fa6 867 int migrate;
4225399a 868 int fudge;
1da177e4
LT
869 int retval;
870
0e1e7c7a
CL
871 /*
872 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
873 * it's read-only
874 */
38837fc7
PJ
875 if (cs == &top_cpuset)
876 return -EACCES;
877
1da177e4 878 trialcs = *cs;
6f7f02e7
DR
879
880 /*
881 * We allow a cpuset's mems_allowed to be empty; if it has attached
882 * tasks, we'll catch it later when we validate the change and return
883 * -ENOSPC.
884 */
885 if (!buf[0] || (buf[0] == '\n' && !buf[1])) {
886 nodes_clear(trialcs.mems_allowed);
887 } else {
888 retval = nodelist_parse(buf, trialcs.mems_allowed);
889 if (retval < 0)
890 goto done;
0e1e7c7a
CL
891 if (!nodes_intersects(trialcs.mems_allowed,
892 node_states[N_HIGH_MEMORY])) {
893 /*
894 * error if only memoryless nodes specified.
895 */
896 retval = -ENOSPC;
897 goto done;
898 }
6f7f02e7 899 }
0e1e7c7a
CL
900 /*
901 * Exclude memoryless nodes. We know that trialcs.mems_allowed
902 * contains at least one node with memory.
903 */
904 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed,
905 node_states[N_HIGH_MEMORY]);
04c19fa6
PJ
906 oldmem = cs->mems_allowed;
907 if (nodes_equal(oldmem, trialcs.mems_allowed)) {
908 retval = 0; /* Too easy - nothing to do */
909 goto done;
910 }
6f7f02e7
DR
911 /* mems_allowed cannot be empty for a cpuset with attached tasks. */
912 if (atomic_read(&cs->count) && nodes_empty(trialcs.mems_allowed)) {
59dac16f
PJ
913 retval = -ENOSPC;
914 goto done;
1da177e4 915 }
59dac16f
PJ
916 retval = validate_change(cs, &trialcs);
917 if (retval < 0)
918 goto done;
919
3d3f26a7 920 mutex_lock(&callback_mutex);
59dac16f 921 cs->mems_allowed = trialcs.mems_allowed;
151a4420 922 cs->mems_generation = cpuset_mems_generation++;
3d3f26a7 923 mutex_unlock(&callback_mutex);
59dac16f 924
4225399a
PJ
925 set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
926
927 fudge = 10; /* spare mmarray[] slots */
928 fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
929 retval = -ENOMEM;
930
931 /*
932 * Allocate mmarray[] to hold mm reference for each task
933 * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
934 * tasklist_lock. We could use GFP_ATOMIC, but with a
935 * few more lines of code, we can retry until we get a big
936 * enough mmarray[] w/o using GFP_ATOMIC.
937 */
938 while (1) {
939 ntasks = atomic_read(&cs->count); /* guess */
940 ntasks += fudge;
941 mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
942 if (!mmarray)
943 goto done;
c2aef333 944 read_lock(&tasklist_lock); /* block fork */
4225399a
PJ
945 if (atomic_read(&cs->count) <= ntasks)
946 break; /* got enough */
c2aef333 947 read_unlock(&tasklist_lock); /* try again */
4225399a
PJ
948 kfree(mmarray);
949 }
950
951 n = 0;
952
953 /* Load up mmarray[] with mm reference for each task in cpuset. */
954 do_each_thread(g, p) {
955 struct mm_struct *mm;
956
957 if (n >= ntasks) {
958 printk(KERN_WARNING
959 "Cpuset mempolicy rebind incomplete.\n");
960 continue;
961 }
962 if (p->cpuset != cs)
963 continue;
964 mm = get_task_mm(p);
965 if (!mm)
966 continue;
967 mmarray[n++] = mm;
968 } while_each_thread(g, p);
c2aef333 969 read_unlock(&tasklist_lock);
4225399a
PJ
970
971 /*
972 * Now that we've dropped the tasklist spinlock, we can
973 * rebind the vma mempolicies of each mm in mmarray[] to their
974 * new cpuset, and release that mm. The mpol_rebind_mm()
975 * call takes mmap_sem, which we couldn't take while holding
976 * tasklist_lock. Forks can happen again now - the mpol_copy()
977 * cpuset_being_rebound check will catch such forks, and rebind
978 * their vma mempolicies too. Because we still hold the global
3d3f26a7 979 * cpuset manage_mutex, we know that no other rebind effort will
4225399a
PJ
980 * be contending for the global variable cpuset_being_rebound.
981 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
04c19fa6 982 * is idempotent. Also migrate pages in each mm to new nodes.
4225399a 983 */
04c19fa6 984 migrate = is_memory_migrate(cs);
4225399a
PJ
985 for (i = 0; i < n; i++) {
986 struct mm_struct *mm = mmarray[i];
987
988 mpol_rebind_mm(mm, &cs->mems_allowed);
e4e364e8
PJ
989 if (migrate)
990 cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
4225399a
PJ
991 mmput(mm);
992 }
993
994 /* We're done rebinding vma's to this cpusets new mems_allowed. */
995 kfree(mmarray);
996 set_cpuset_being_rebound(NULL);
997 retval = 0;
59dac16f 998done:
1da177e4
LT
999 return retval;
1000}
1001
3e0d98b9 1002/*
3d3f26a7 1003 * Call with manage_mutex held.
3e0d98b9
PJ
1004 */
1005
1006static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
1007{
1008 if (simple_strtoul(buf, NULL, 10) != 0)
1009 cpuset_memory_pressure_enabled = 1;
1010 else
1011 cpuset_memory_pressure_enabled = 0;
1012 return 0;
1013}
1014
1da177e4
LT
1015/*
1016 * update_flag - read a 0 or a 1 in a file and update associated flag
1017 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
825a46af
PJ
1018 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
1019 * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
1da177e4
LT
1020 * cs: the cpuset to update
1021 * buf: the buffer where we read the 0 or 1
053199ed 1022 *
3d3f26a7 1023 * Call with manage_mutex held.
1da177e4
LT
1024 */
1025
1026static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
1027{
1028 int turning_on;
1029 struct cpuset trialcs;
607717a6 1030 int err;
1da177e4
LT
1031
1032 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
1033
1034 trialcs = *cs;
1035 if (turning_on)
1036 set_bit(bit, &trialcs.flags);
1037 else
1038 clear_bit(bit, &trialcs.flags);
1039
1040 err = validate_change(cs, &trialcs);
85d7b949
DG
1041 if (err < 0)
1042 return err;
3d3f26a7 1043 mutex_lock(&callback_mutex);
69604067 1044 cs->flags = trialcs.flags;
3d3f26a7 1045 mutex_unlock(&callback_mutex);
85d7b949 1046
85d7b949 1047 return 0;
1da177e4
LT
1048}
1049
3e0d98b9 1050/*
80f7228b 1051 * Frequency meter - How fast is some event occurring?
3e0d98b9
PJ
1052 *
1053 * These routines manage a digitally filtered, constant time based,
1054 * event frequency meter. There are four routines:
1055 * fmeter_init() - initialize a frequency meter.
1056 * fmeter_markevent() - called each time the event happens.
1057 * fmeter_getrate() - returns the recent rate of such events.
1058 * fmeter_update() - internal routine used to update fmeter.
1059 *
1060 * A common data structure is passed to each of these routines,
1061 * which is used to keep track of the state required to manage the
1062 * frequency meter and its digital filter.
1063 *
1064 * The filter works on the number of events marked per unit time.
1065 * The filter is single-pole low-pass recursive (IIR). The time unit
1066 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1067 * simulate 3 decimal digits of precision (multiplied by 1000).
1068 *
1069 * With an FM_COEF of 933, and a time base of 1 second, the filter
1070 * has a half-life of 10 seconds, meaning that if the events quit
1071 * happening, then the rate returned from the fmeter_getrate()
1072 * will be cut in half each 10 seconds, until it converges to zero.
1073 *
1074 * It is not worth doing a real infinitely recursive filter. If more
1075 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1076 * just compute FM_MAXTICKS ticks worth, by which point the level
1077 * will be stable.
1078 *
1079 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1080 * arithmetic overflow in the fmeter_update() routine.
1081 *
1082 * Given the simple 32 bit integer arithmetic used, this meter works
1083 * best for reporting rates between one per millisecond (msec) and
1084 * one per 32 (approx) seconds. At constant rates faster than one
1085 * per msec it maxes out at values just under 1,000,000. At constant
1086 * rates between one per msec, and one per second it will stabilize
1087 * to a value N*1000, where N is the rate of events per second.
1088 * At constant rates between one per second and one per 32 seconds,
1089 * it will be choppy, moving up on the seconds that have an event,
1090 * and then decaying until the next event. At rates slower than
1091 * about one in 32 seconds, it decays all the way back to zero between
1092 * each event.
1093 */
1094
1095#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1096#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1097#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1098#define FM_SCALE 1000 /* faux fixed point scale */
1099
1100/* Initialize a frequency meter */
1101static void fmeter_init(struct fmeter *fmp)
1102{
1103 fmp->cnt = 0;
1104 fmp->val = 0;
1105 fmp->time = 0;
1106 spin_lock_init(&fmp->lock);
1107}
1108
1109/* Internal meter update - process cnt events and update value */
1110static void fmeter_update(struct fmeter *fmp)
1111{
1112 time_t now = get_seconds();
1113 time_t ticks = now - fmp->time;
1114
1115 if (ticks == 0)
1116 return;
1117
1118 ticks = min(FM_MAXTICKS, ticks);
1119 while (ticks-- > 0)
1120 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1121 fmp->time = now;
1122
1123 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1124 fmp->cnt = 0;
1125}
1126
1127/* Process any previous ticks, then bump cnt by one (times scale). */
1128static void fmeter_markevent(struct fmeter *fmp)
1129{
1130 spin_lock(&fmp->lock);
1131 fmeter_update(fmp);
1132 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1133 spin_unlock(&fmp->lock);
1134}
1135
1136/* Process any previous ticks, then return current value. */
1137static int fmeter_getrate(struct fmeter *fmp)
1138{
1139 int val;
1140
1141 spin_lock(&fmp->lock);
1142 fmeter_update(fmp);
1143 val = fmp->val;
1144 spin_unlock(&fmp->lock);
1145 return val;
1146}
1147
053199ed
PJ
1148/*
1149 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
1150 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1151 * notified on release.
1152 *
3d3f26a7 1153 * Call holding manage_mutex. May take callback_mutex and task_lock of
053199ed
PJ
1154 * the task 'pid' during call.
1155 */
1156
3077a260 1157static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
1da177e4
LT
1158{
1159 pid_t pid;
1160 struct task_struct *tsk;
1161 struct cpuset *oldcs;
1162 cpumask_t cpus;
45b07ef3 1163 nodemask_t from, to;
4225399a 1164 struct mm_struct *mm;
22fb52dd 1165 int retval;
1da177e4 1166
3077a260 1167 if (sscanf(pidbuf, "%d", &pid) != 1)
1da177e4
LT
1168 return -EIO;
1169 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1170 return -ENOSPC;
1171
1172 if (pid) {
1173 read_lock(&tasklist_lock);
1174
1175 tsk = find_task_by_pid(pid);
053199ed 1176 if (!tsk || tsk->flags & PF_EXITING) {
1da177e4
LT
1177 read_unlock(&tasklist_lock);
1178 return -ESRCH;
1179 }
1180
1181 get_task_struct(tsk);
1182 read_unlock(&tasklist_lock);
1183
1184 if ((current->euid) && (current->euid != tsk->uid)
1185 && (current->euid != tsk->suid)) {
1186 put_task_struct(tsk);
1187 return -EACCES;
1188 }
1189 } else {
1190 tsk = current;
1191 get_task_struct(tsk);
1192 }
1193
22fb52dd
DQ
1194 retval = security_task_setscheduler(tsk, 0, NULL);
1195 if (retval) {
1196 put_task_struct(tsk);
1197 return retval;
1198 }
1199
3d3f26a7 1200 mutex_lock(&callback_mutex);
053199ed 1201
1da177e4
LT
1202 task_lock(tsk);
1203 oldcs = tsk->cpuset;
181b6480
PJ
1204 /*
1205 * After getting 'oldcs' cpuset ptr, be sure still not exiting.
1206 * If 'oldcs' might be the top_cpuset due to the_top_cpuset_hack
1207 * then fail this attach_task(), to avoid breaking top_cpuset.count.
1208 */
1209 if (tsk->flags & PF_EXITING) {
1da177e4 1210 task_unlock(tsk);
3d3f26a7 1211 mutex_unlock(&callback_mutex);
1da177e4
LT
1212 put_task_struct(tsk);
1213 return -ESRCH;
1214 }
1215 atomic_inc(&cs->count);
6b9c2603 1216 rcu_assign_pointer(tsk->cpuset, cs);
1da177e4
LT
1217 task_unlock(tsk);
1218
1219 guarantee_online_cpus(cs, &cpus);
1220 set_cpus_allowed(tsk, cpus);
1221
45b07ef3
PJ
1222 from = oldcs->mems_allowed;
1223 to = cs->mems_allowed;
1224
3d3f26a7 1225 mutex_unlock(&callback_mutex);
4225399a
PJ
1226
1227 mm = get_task_mm(tsk);
1228 if (mm) {
1229 mpol_rebind_mm(mm, &to);
2741a559 1230 if (is_memory_migrate(cs))
e4e364e8 1231 cpuset_migrate_mm(mm, &from, &to);
4225399a
PJ
1232 mmput(mm);
1233 }
1234
1da177e4 1235 put_task_struct(tsk);
6b9c2603 1236 synchronize_rcu();
1da177e4 1237 if (atomic_dec_and_test(&oldcs->count))
3077a260 1238 check_for_release(oldcs, ppathbuf);
1da177e4
LT
1239 return 0;
1240}
1241
1242/* The various types of files and directories in a cpuset file system */
1243
1244typedef enum {
1245 FILE_ROOT,
1246 FILE_DIR,
45b07ef3 1247 FILE_MEMORY_MIGRATE,
1da177e4
LT
1248 FILE_CPULIST,
1249 FILE_MEMLIST,
1250 FILE_CPU_EXCLUSIVE,
1251 FILE_MEM_EXCLUSIVE,
1252 FILE_NOTIFY_ON_RELEASE,
3e0d98b9
PJ
1253 FILE_MEMORY_PRESSURE_ENABLED,
1254 FILE_MEMORY_PRESSURE,
825a46af
PJ
1255 FILE_SPREAD_PAGE,
1256 FILE_SPREAD_SLAB,
1da177e4
LT
1257 FILE_TASKLIST,
1258} cpuset_filetype_t;
1259
d3ed11c3
PM
1260static ssize_t cpuset_common_file_write(struct file *file,
1261 const char __user *userbuf,
1da177e4
LT
1262 size_t nbytes, loff_t *unused_ppos)
1263{
a7a005fd
JS
1264 struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
1265 struct cftype *cft = __d_cft(file->f_path.dentry);
1da177e4
LT
1266 cpuset_filetype_t type = cft->private;
1267 char *buffer;
3077a260 1268 char *pathbuf = NULL;
1da177e4
LT
1269 int retval = 0;
1270
1271 /* Crude upper limit on largest legitimate cpulist user might write. */
d3ed11c3 1272 if (nbytes > 100 + 6 * max(NR_CPUS, MAX_NUMNODES))
1da177e4
LT
1273 return -E2BIG;
1274
1275 /* +1 for nul-terminator */
1276 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
1277 return -ENOMEM;
1278
1279 if (copy_from_user(buffer, userbuf, nbytes)) {
1280 retval = -EFAULT;
1281 goto out1;
1282 }
1283 buffer[nbytes] = 0; /* nul-terminate */
1284
3d3f26a7 1285 mutex_lock(&manage_mutex);
1da177e4
LT
1286
1287 if (is_removed(cs)) {
1288 retval = -ENODEV;
1289 goto out2;
1290 }
1291
1292 switch (type) {
1293 case FILE_CPULIST:
1294 retval = update_cpumask(cs, buffer);
1295 break;
1296 case FILE_MEMLIST:
1297 retval = update_nodemask(cs, buffer);
1298 break;
1299 case FILE_CPU_EXCLUSIVE:
1300 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
1301 break;
1302 case FILE_MEM_EXCLUSIVE:
1303 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
1304 break;
1305 case FILE_NOTIFY_ON_RELEASE:
1306 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
1307 break;
45b07ef3
PJ
1308 case FILE_MEMORY_MIGRATE:
1309 retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
1310 break;
3e0d98b9
PJ
1311 case FILE_MEMORY_PRESSURE_ENABLED:
1312 retval = update_memory_pressure_enabled(cs, buffer);
1313 break;
1314 case FILE_MEMORY_PRESSURE:
1315 retval = -EACCES;
1316 break;
825a46af
PJ
1317 case FILE_SPREAD_PAGE:
1318 retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
151a4420 1319 cs->mems_generation = cpuset_mems_generation++;
825a46af
PJ
1320 break;
1321 case FILE_SPREAD_SLAB:
1322 retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
151a4420 1323 cs->mems_generation = cpuset_mems_generation++;
825a46af 1324 break;
1da177e4 1325 case FILE_TASKLIST:
3077a260 1326 retval = attach_task(cs, buffer, &pathbuf);
1da177e4
LT
1327 break;
1328 default:
1329 retval = -EINVAL;
1330 goto out2;
1331 }
1332
1333 if (retval == 0)
1334 retval = nbytes;
1335out2:
3d3f26a7 1336 mutex_unlock(&manage_mutex);
3077a260 1337 cpuset_release_agent(pathbuf);
1da177e4
LT
1338out1:
1339 kfree(buffer);
1340 return retval;
1341}
1342
1343static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
1344 size_t nbytes, loff_t *ppos)
1345{
1346 ssize_t retval = 0;
a7a005fd 1347 struct cftype *cft = __d_cft(file->f_path.dentry);
1da177e4
LT
1348 if (!cft)
1349 return -ENODEV;
1350
1351 /* special function ? */
1352 if (cft->write)
1353 retval = cft->write(file, buf, nbytes, ppos);
1354 else
1355 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
1356
1357 return retval;
1358}
1359
1360/*
1361 * These ascii lists should be read in a single call, by using a user
1362 * buffer large enough to hold the entire map. If read in smaller
1363 * chunks, there is no guarantee of atomicity. Since the display format
1364 * used, list of ranges of sequential numbers, is variable length,
1365 * and since these maps can change value dynamically, one could read
1366 * gibberish by doing partial reads while a list was changing.
1367 * A single large read to a buffer that crosses a page boundary is
1368 * ok, because the result being copied to user land is not recomputed
1369 * across a page fault.
1370 */
1371
1372static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1373{
1374 cpumask_t mask;
1375
3d3f26a7 1376 mutex_lock(&callback_mutex);
1da177e4 1377 mask = cs->cpus_allowed;
3d3f26a7 1378 mutex_unlock(&callback_mutex);
1da177e4
LT
1379
1380 return cpulist_scnprintf(page, PAGE_SIZE, mask);
1381}
1382
1383static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1384{
1385 nodemask_t mask;
1386
3d3f26a7 1387 mutex_lock(&callback_mutex);
1da177e4 1388 mask = cs->mems_allowed;
3d3f26a7 1389 mutex_unlock(&callback_mutex);
1da177e4
LT
1390
1391 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1392}
1393
1394static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
1395 size_t nbytes, loff_t *ppos)
1396{
a7a005fd
JS
1397 struct cftype *cft = __d_cft(file->f_path.dentry);
1398 struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
1da177e4
LT
1399 cpuset_filetype_t type = cft->private;
1400 char *page;
1401 ssize_t retval = 0;
1402 char *s;
1da177e4 1403
e12ba74d 1404 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
1da177e4
LT
1405 return -ENOMEM;
1406
1407 s = page;
1408
1409 switch (type) {
1410 case FILE_CPULIST:
1411 s += cpuset_sprintf_cpulist(s, cs);
1412 break;
1413 case FILE_MEMLIST:
1414 s += cpuset_sprintf_memlist(s, cs);
1415 break;
1416 case FILE_CPU_EXCLUSIVE:
1417 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
1418 break;
1419 case FILE_MEM_EXCLUSIVE:
1420 *s++ = is_mem_exclusive(cs) ? '1' : '0';
1421 break;
1422 case FILE_NOTIFY_ON_RELEASE:
1423 *s++ = notify_on_release(cs) ? '1' : '0';
1424 break;
45b07ef3
PJ
1425 case FILE_MEMORY_MIGRATE:
1426 *s++ = is_memory_migrate(cs) ? '1' : '0';
1427 break;
3e0d98b9
PJ
1428 case FILE_MEMORY_PRESSURE_ENABLED:
1429 *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
1430 break;
1431 case FILE_MEMORY_PRESSURE:
1432 s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
1433 break;
825a46af
PJ
1434 case FILE_SPREAD_PAGE:
1435 *s++ = is_spread_page(cs) ? '1' : '0';
1436 break;
1437 case FILE_SPREAD_SLAB:
1438 *s++ = is_spread_slab(cs) ? '1' : '0';
1439 break;
1da177e4
LT
1440 default:
1441 retval = -EINVAL;
1442 goto out;
1443 }
1444 *s++ = '\n';
1da177e4 1445
eacaa1f5 1446 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1da177e4
LT
1447out:
1448 free_page((unsigned long)page);
1449 return retval;
1450}
1451
1452static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1453 loff_t *ppos)
1454{
1455 ssize_t retval = 0;
a7a005fd 1456 struct cftype *cft = __d_cft(file->f_path.dentry);
1da177e4
LT
1457 if (!cft)
1458 return -ENODEV;
1459
1460 /* special function ? */
1461 if (cft->read)
1462 retval = cft->read(file, buf, nbytes, ppos);
1463 else
1464 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1465
1466 return retval;
1467}
1468
1469static int cpuset_file_open(struct inode *inode, struct file *file)
1470{
1471 int err;
1472 struct cftype *cft;
1473
1474 err = generic_file_open(inode, file);
1475 if (err)
1476 return err;
1477
a7a005fd 1478 cft = __d_cft(file->f_path.dentry);
1da177e4
LT
1479 if (!cft)
1480 return -ENODEV;
1481 if (cft->open)
1482 err = cft->open(inode, file);
1483 else
1484 err = 0;
1485
1486 return err;
1487}
1488
1489static int cpuset_file_release(struct inode *inode, struct file *file)
1490{
a7a005fd 1491 struct cftype *cft = __d_cft(file->f_path.dentry);
1da177e4
LT
1492 if (cft->release)
1493 return cft->release(inode, file);
1494 return 0;
1495}
1496
18a19cb3
PJ
1497/*
1498 * cpuset_rename - Only allow simple rename of directories in place.
1499 */
1500static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
1501 struct inode *new_dir, struct dentry *new_dentry)
1502{
1503 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1504 return -ENOTDIR;
1505 if (new_dentry->d_inode)
1506 return -EEXIST;
1507 if (old_dir != new_dir)
1508 return -EIO;
1509 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1510}
1511
15ad7cdc 1512static const struct file_operations cpuset_file_operations = {
1da177e4
LT
1513 .read = cpuset_file_read,
1514 .write = cpuset_file_write,
1515 .llseek = generic_file_llseek,
1516 .open = cpuset_file_open,
1517 .release = cpuset_file_release,
1518};
1519
92e1d5be 1520static const struct inode_operations cpuset_dir_inode_operations = {
1da177e4
LT
1521 .lookup = simple_lookup,
1522 .mkdir = cpuset_mkdir,
1523 .rmdir = cpuset_rmdir,
18a19cb3 1524 .rename = cpuset_rename,
1da177e4
LT
1525};
1526
1527static int cpuset_create_file(struct dentry *dentry, int mode)
1528{
1529 struct inode *inode;
1530
1531 if (!dentry)
1532 return -ENOENT;
1533 if (dentry->d_inode)
1534 return -EEXIST;
1535
1536 inode = cpuset_new_inode(mode);
1537 if (!inode)
1538 return -ENOMEM;
1539
1540 if (S_ISDIR(mode)) {
1541 inode->i_op = &cpuset_dir_inode_operations;
1542 inode->i_fop = &simple_dir_operations;
1543
1544 /* start off with i_nlink == 2 (for "." entry) */
d8c76e6f 1545 inc_nlink(inode);
1da177e4
LT
1546 } else if (S_ISREG(mode)) {
1547 inode->i_size = 0;
1548 inode->i_fop = &cpuset_file_operations;
1549 }
1550
1551 d_instantiate(dentry, inode);
1552 dget(dentry); /* Extra count - pin the dentry in core */
1553 return 0;
1554}
1555
1556/*
1557 * cpuset_create_dir - create a directory for an object.
c5b2aff8 1558 * cs: the cpuset we create the directory for.
1da177e4
LT
1559 * It must have a valid ->parent field
1560 * And we are going to fill its ->dentry field.
1561 * name: The name to give to the cpuset directory. Will be copied.
1562 * mode: mode to set on new directory.
1563 */
1564
1565static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1566{
1567 struct dentry *dentry = NULL;
1568 struct dentry *parent;
1569 int error = 0;
1570
1571 parent = cs->parent->dentry;
1572 dentry = cpuset_get_dentry(parent, name);
1573 if (IS_ERR(dentry))
1574 return PTR_ERR(dentry);
1575 error = cpuset_create_file(dentry, S_IFDIR | mode);
1576 if (!error) {
1577 dentry->d_fsdata = cs;
d8c76e6f 1578 inc_nlink(parent->d_inode);
1da177e4
LT
1579 cs->dentry = dentry;
1580 }
1581 dput(dentry);
1582
1583 return error;
1584}
1585
1586static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1587{
1588 struct dentry *dentry;
1589 int error;
1590
1b1dcc1b 1591 mutex_lock(&dir->d_inode->i_mutex);
1da177e4
LT
1592 dentry = cpuset_get_dentry(dir, cft->name);
1593 if (!IS_ERR(dentry)) {
1594 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1595 if (!error)
1596 dentry->d_fsdata = (void *)cft;
1597 dput(dentry);
1598 } else
1599 error = PTR_ERR(dentry);
1b1dcc1b 1600 mutex_unlock(&dir->d_inode->i_mutex);
1da177e4
LT
1601 return error;
1602}
1603
1604/*
1605 * Stuff for reading the 'tasks' file.
1606 *
1607 * Reading this file can return large amounts of data if a cpuset has
1608 * *lots* of attached tasks. So it may need several calls to read(),
1609 * but we cannot guarantee that the information we produce is correct
1610 * unless we produce it entirely atomically.
1611 *
1612 * Upon tasks file open(), a struct ctr_struct is allocated, that
1613 * will have a pointer to an array (also allocated here). The struct
1614 * ctr_struct * is stored in file->private_data. Its resources will
1615 * be freed by release() when the file is closed. The array is used
1616 * to sprintf the PIDs and then used by read().
1617 */
1618
1619/* cpusets_tasks_read array */
1620
1621struct ctr_struct {
1622 char *buf;
1623 int bufsz;
1624};
1625
1626/*
1627 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
053199ed
PJ
1628 * Return actual number of pids loaded. No need to task_lock(p)
1629 * when reading out p->cpuset, as we don't really care if it changes
1630 * on the next cycle, and we are not going to try to dereference it.
1da177e4 1631 */
858119e1 1632static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1da177e4
LT
1633{
1634 int n = 0;
1635 struct task_struct *g, *p;
1636
1637 read_lock(&tasklist_lock);
1638
1639 do_each_thread(g, p) {
1640 if (p->cpuset == cs) {
1da177e4
LT
1641 if (unlikely(n == npids))
1642 goto array_full;
3e903e7b 1643 pidarray[n++] = p->pid;
1da177e4
LT
1644 }
1645 } while_each_thread(g, p);
1646
1647array_full:
1648 read_unlock(&tasklist_lock);
1649 return n;
1650}
1651
1652static int cmppid(const void *a, const void *b)
1653{
1654 return *(pid_t *)a - *(pid_t *)b;
1655}
1656
1657/*
1658 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1659 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1660 * count 'cnt' of how many chars would be written if buf were large enough.
1661 */
1662static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1663{
1664 int cnt = 0;
1665 int i;
1666
1667 for (i = 0; i < npids; i++)
1668 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1669 return cnt;
1670}
1671
053199ed
PJ
1672/*
1673 * Handle an open on 'tasks' file. Prepare a buffer listing the
1674 * process id's of tasks currently attached to the cpuset being opened.
1675 *
3d3f26a7 1676 * Does not require any specific cpuset mutexes, and does not take any.
053199ed 1677 */
1da177e4
LT
1678static int cpuset_tasks_open(struct inode *unused, struct file *file)
1679{
a7a005fd 1680 struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
1da177e4
LT
1681 struct ctr_struct *ctr;
1682 pid_t *pidarray;
1683 int npids;
1684 char c;
1685
1686 if (!(file->f_mode & FMODE_READ))
1687 return 0;
1688
1689 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1690 if (!ctr)
1691 goto err0;
1692
1693 /*
1694 * If cpuset gets more users after we read count, we won't have
1695 * enough space - tough. This race is indistinguishable to the
1696 * caller from the case that the additional cpuset users didn't
1697 * show up until sometime later on.
1698 */
1699 npids = atomic_read(&cs->count);
1700 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1701 if (!pidarray)
1702 goto err1;
1703
1704 npids = pid_array_load(pidarray, npids, cs);
1705 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1706
1707 /* Call pid_array_to_buf() twice, first just to get bufsz */
1708 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1709 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1710 if (!ctr->buf)
1711 goto err2;
1712 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1713
1714 kfree(pidarray);
1715 file->private_data = ctr;
1716 return 0;
1717
1718err2:
1719 kfree(pidarray);
1720err1:
1721 kfree(ctr);
1722err0:
1723 return -ENOMEM;
1724}
1725
1726static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1727 size_t nbytes, loff_t *ppos)
1728{
1729 struct ctr_struct *ctr = file->private_data;
1730
85badbdf 1731 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
1da177e4
LT
1732}
1733
1734static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1735{
1736 struct ctr_struct *ctr;
1737
1738 if (file->f_mode & FMODE_READ) {
1739 ctr = file->private_data;
1740 kfree(ctr->buf);
1741 kfree(ctr);
1742 }
1743 return 0;
1744}
1745
1746/*
1747 * for the common functions, 'private' gives the type of file
1748 */
1749
1750static struct cftype cft_tasks = {
1751 .name = "tasks",
1752 .open = cpuset_tasks_open,
1753 .read = cpuset_tasks_read,
1754 .release = cpuset_tasks_release,
1755 .private = FILE_TASKLIST,
1756};
1757
1758static struct cftype cft_cpus = {
1759 .name = "cpus",
1760 .private = FILE_CPULIST,
1761};
1762
1763static struct cftype cft_mems = {
1764 .name = "mems",
1765 .private = FILE_MEMLIST,
1766};
1767
1768static struct cftype cft_cpu_exclusive = {
1769 .name = "cpu_exclusive",
1770 .private = FILE_CPU_EXCLUSIVE,
1771};
1772
1773static struct cftype cft_mem_exclusive = {
1774 .name = "mem_exclusive",
1775 .private = FILE_MEM_EXCLUSIVE,
1776};
1777
1778static struct cftype cft_notify_on_release = {
1779 .name = "notify_on_release",
1780 .private = FILE_NOTIFY_ON_RELEASE,
1781};
1782
45b07ef3
PJ
1783static struct cftype cft_memory_migrate = {
1784 .name = "memory_migrate",
1785 .private = FILE_MEMORY_MIGRATE,
1786};
1787
3e0d98b9
PJ
1788static struct cftype cft_memory_pressure_enabled = {
1789 .name = "memory_pressure_enabled",
1790 .private = FILE_MEMORY_PRESSURE_ENABLED,
1791};
1792
1793static struct cftype cft_memory_pressure = {
1794 .name = "memory_pressure",
1795 .private = FILE_MEMORY_PRESSURE,
1796};
1797
825a46af
PJ
1798static struct cftype cft_spread_page = {
1799 .name = "memory_spread_page",
1800 .private = FILE_SPREAD_PAGE,
1801};
1802
1803static struct cftype cft_spread_slab = {
1804 .name = "memory_spread_slab",
1805 .private = FILE_SPREAD_SLAB,
1806};
1807
1da177e4
LT
1808static int cpuset_populate_dir(struct dentry *cs_dentry)
1809{
1810 int err;
1811
1812 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1813 return err;
1814 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1815 return err;
1816 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1817 return err;
1818 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1819 return err;
1820 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1821 return err;
45b07ef3
PJ
1822 if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
1823 return err;
3e0d98b9
PJ
1824 if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
1825 return err;
825a46af
PJ
1826 if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0)
1827 return err;
1828 if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0)
1829 return err;
1da177e4
LT
1830 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1831 return err;
1832 return 0;
1833}
1834
1835/*
1836 * cpuset_create - create a cpuset
1837 * parent: cpuset that will be parent of the new cpuset.
1838 * name: name of the new cpuset. Will be strcpy'ed.
1839 * mode: mode to set on new inode
1840 *
3d3f26a7 1841 * Must be called with the mutex on the parent inode held
1da177e4
LT
1842 */
1843
1844static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1845{
1846 struct cpuset *cs;
1847 int err;
1848
1849 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1850 if (!cs)
1851 return -ENOMEM;
1852
3d3f26a7 1853 mutex_lock(&manage_mutex);
cf2a473c 1854 cpuset_update_task_memory_state();
1da177e4
LT
1855 cs->flags = 0;
1856 if (notify_on_release(parent))
1857 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
825a46af
PJ
1858 if (is_spread_page(parent))
1859 set_bit(CS_SPREAD_PAGE, &cs->flags);
1860 if (is_spread_slab(parent))
1861 set_bit(CS_SPREAD_SLAB, &cs->flags);
1da177e4
LT
1862 cs->cpus_allowed = CPU_MASK_NONE;
1863 cs->mems_allowed = NODE_MASK_NONE;
1864 atomic_set(&cs->count, 0);
1865 INIT_LIST_HEAD(&cs->sibling);
1866 INIT_LIST_HEAD(&cs->children);
151a4420 1867 cs->mems_generation = cpuset_mems_generation++;
3e0d98b9 1868 fmeter_init(&cs->fmeter);
1da177e4
LT
1869
1870 cs->parent = parent;
1871
3d3f26a7 1872 mutex_lock(&callback_mutex);
1da177e4 1873 list_add(&cs->sibling, &cs->parent->children);
202f72d5 1874 number_of_cpusets++;
3d3f26a7 1875 mutex_unlock(&callback_mutex);
1da177e4
LT
1876
1877 err = cpuset_create_dir(cs, name, mode);
1878 if (err < 0)
1879 goto err;
1880
1881 /*
3d3f26a7 1882 * Release manage_mutex before cpuset_populate_dir() because it
1b1dcc1b 1883 * will down() this new directory's i_mutex and if we race with
1da177e4
LT
1884 * another mkdir, we might deadlock.
1885 */
3d3f26a7 1886 mutex_unlock(&manage_mutex);
1da177e4
LT
1887
1888 err = cpuset_populate_dir(cs->dentry);
1889 /* If err < 0, we have a half-filled directory - oh well ;) */
1890 return 0;
1891err:
1892 list_del(&cs->sibling);
3d3f26a7 1893 mutex_unlock(&manage_mutex);
1da177e4
LT
1894 kfree(cs);
1895 return err;
1896}
1897
1898static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1899{
1900 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1901
1b1dcc1b 1902 /* the vfs holds inode->i_mutex already */
1da177e4
LT
1903 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1904}
1905
1906static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1907{
1908 struct cpuset *cs = dentry->d_fsdata;
1909 struct dentry *d;
1910 struct cpuset *parent;
3077a260 1911 char *pathbuf = NULL;
1da177e4 1912
1b1dcc1b 1913 /* the vfs holds both inode->i_mutex already */
1da177e4 1914
3d3f26a7 1915 mutex_lock(&manage_mutex);
cf2a473c 1916 cpuset_update_task_memory_state();
1da177e4 1917 if (atomic_read(&cs->count) > 0) {
3d3f26a7 1918 mutex_unlock(&manage_mutex);
1da177e4
LT
1919 return -EBUSY;
1920 }
1921 if (!list_empty(&cs->children)) {
3d3f26a7 1922 mutex_unlock(&manage_mutex);
1da177e4
LT
1923 return -EBUSY;
1924 }
1da177e4 1925 parent = cs->parent;
3d3f26a7 1926 mutex_lock(&callback_mutex);
1da177e4
LT
1927 set_bit(CS_REMOVED, &cs->flags);
1928 list_del(&cs->sibling); /* delete my sibling from parent->children */
85d7b949 1929 spin_lock(&cs->dentry->d_lock);
1da177e4
LT
1930 d = dget(cs->dentry);
1931 cs->dentry = NULL;
1932 spin_unlock(&d->d_lock);
1933 cpuset_d_remove_dir(d);
1934 dput(d);
202f72d5 1935 number_of_cpusets--;
3d3f26a7 1936 mutex_unlock(&callback_mutex);
053199ed
PJ
1937 if (list_empty(&parent->children))
1938 check_for_release(parent, &pathbuf);
3d3f26a7 1939 mutex_unlock(&manage_mutex);
3077a260 1940 cpuset_release_agent(pathbuf);
1da177e4
LT
1941 return 0;
1942}
1943
c417f024
PJ
1944/*
1945 * cpuset_init_early - just enough so that the calls to
1946 * cpuset_update_task_memory_state() in early init code
1947 * are harmless.
1948 */
1949
1950int __init cpuset_init_early(void)
1951{
1952 struct task_struct *tsk = current;
1953
1954 tsk->cpuset = &top_cpuset;
151a4420 1955 tsk->cpuset->mems_generation = cpuset_mems_generation++;
c417f024
PJ
1956 return 0;
1957}
1958
1da177e4
LT
1959/**
1960 * cpuset_init - initialize cpusets at system boot
1961 *
1962 * Description: Initialize top_cpuset and the cpuset internal file system,
1963 **/
1964
1965int __init cpuset_init(void)
1966{
1967 struct dentry *root;
1968 int err;
1969
1970 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1971 top_cpuset.mems_allowed = NODE_MASK_ALL;
1972
3e0d98b9 1973 fmeter_init(&top_cpuset.fmeter);
151a4420 1974 top_cpuset.mems_generation = cpuset_mems_generation++;
1da177e4
LT
1975
1976 init_task.cpuset = &top_cpuset;
1977
1978 err = register_filesystem(&cpuset_fs_type);
1979 if (err < 0)
1980 goto out;
1981 cpuset_mount = kern_mount(&cpuset_fs_type);
1982 if (IS_ERR(cpuset_mount)) {
1983 printk(KERN_ERR "cpuset: could not mount!\n");
1984 err = PTR_ERR(cpuset_mount);
1985 cpuset_mount = NULL;
1986 goto out;
1987 }
1988 root = cpuset_mount->mnt_sb->s_root;
1989 root->d_fsdata = &top_cpuset;
d8c76e6f 1990 inc_nlink(root->d_inode);
1da177e4
LT
1991 top_cpuset.dentry = root;
1992 root->d_inode->i_op = &cpuset_dir_inode_operations;
202f72d5 1993 number_of_cpusets = 1;
1da177e4 1994 err = cpuset_populate_dir(root);
3e0d98b9
PJ
1995 /* memory_pressure_enabled is in root cpuset only */
1996 if (err == 0)
1997 err = cpuset_add_file(root, &cft_memory_pressure_enabled);
1da177e4
LT
1998out:
1999 return err;
2000}
2001
b1aac8bb
PJ
2002/*
2003 * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs
2004 * or memory nodes, we need to walk over the cpuset hierarchy,
2005 * removing that CPU or node from all cpusets. If this removes the
2006 * last CPU or node from a cpuset, then the guarantee_online_cpus()
2007 * or guarantee_online_mems() code will use that emptied cpusets
2008 * parent online CPUs or nodes. Cpusets that were already empty of
2009 * CPUs or nodes are left empty.
2010 *
2011 * This routine is intentionally inefficient in a couple of regards.
2012 * It will check all cpusets in a subtree even if the top cpuset of
2013 * the subtree has no offline CPUs or nodes. It checks both CPUs and
2014 * nodes, even though the caller could have been coded to know that
2015 * only one of CPUs or nodes needed to be checked on a given call.
2016 * This was done to minimize text size rather than cpu cycles.
2017 *
2018 * Call with both manage_mutex and callback_mutex held.
2019 *
2020 * Recursive, on depth of cpuset subtree.
2021 */
2022
2023static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
2024{
2025 struct cpuset *c;
2026
2027 /* Each of our child cpusets mems must be online */
2028 list_for_each_entry(c, &cur->children, sibling) {
2029 guarantee_online_cpus_mems_in_subtree(c);
2030 if (!cpus_empty(c->cpus_allowed))
2031 guarantee_online_cpus(c, &c->cpus_allowed);
2032 if (!nodes_empty(c->mems_allowed))
2033 guarantee_online_mems(c, &c->mems_allowed);
2034 }
2035}
2036
2037/*
2038 * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track
0e1e7c7a
CL
2039 * cpu_online_map and node_states[N_HIGH_MEMORY]. Force the top cpuset to
2040 * track what's online after any CPU or memory node hotplug or unplug
2041 * event.
b1aac8bb
PJ
2042 *
2043 * To ensure that we don't remove a CPU or node from the top cpuset
2044 * that is currently in use by a child cpuset (which would violate
2045 * the rule that cpusets must be subsets of their parent), we first
2046 * call the recursive routine guarantee_online_cpus_mems_in_subtree().
2047 *
2048 * Since there are two callers of this routine, one for CPU hotplug
2049 * events and one for memory node hotplug events, we could have coded
2050 * two separate routines here. We code it as a single common routine
2051 * in order to minimize text size.
2052 */
2053
2054static void common_cpu_mem_hotplug_unplug(void)
2055{
2056 mutex_lock(&manage_mutex);
2057 mutex_lock(&callback_mutex);
2058
2059 guarantee_online_cpus_mems_in_subtree(&top_cpuset);
2060 top_cpuset.cpus_allowed = cpu_online_map;
0e1e7c7a 2061 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
b1aac8bb
PJ
2062
2063 mutex_unlock(&callback_mutex);
2064 mutex_unlock(&manage_mutex);
2065}
b1aac8bb 2066
4c4d50f7
PJ
2067/*
2068 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2069 * period. This is necessary in order to make cpusets transparent
2070 * (of no affect) on systems that are actively using CPU hotplug
2071 * but making no active use of cpusets.
2072 *
38837fc7
PJ
2073 * This routine ensures that top_cpuset.cpus_allowed tracks
2074 * cpu_online_map on each CPU hotplug (cpuhp) event.
4c4d50f7
PJ
2075 */
2076
4c4d50f7
PJ
2077static int cpuset_handle_cpuhp(struct notifier_block *nb,
2078 unsigned long phase, void *cpu)
2079{
ac076758
AK
2080 if (phase == CPU_DYING || phase == CPU_DYING_FROZEN)
2081 return NOTIFY_DONE;
2082
b1aac8bb 2083 common_cpu_mem_hotplug_unplug();
4c4d50f7
PJ
2084 return 0;
2085}
4c4d50f7 2086
b1aac8bb 2087#ifdef CONFIG_MEMORY_HOTPLUG
38837fc7 2088/*
0e1e7c7a
CL
2089 * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
2090 * Call this routine anytime after you change
2091 * node_states[N_HIGH_MEMORY].
38837fc7
PJ
2092 * See also the previous routine cpuset_handle_cpuhp().
2093 */
2094
1af98928 2095void cpuset_track_online_nodes(void)
38837fc7 2096{
b1aac8bb 2097 common_cpu_mem_hotplug_unplug();
38837fc7
PJ
2098}
2099#endif
2100
1da177e4
LT
2101/**
2102 * cpuset_init_smp - initialize cpus_allowed
2103 *
2104 * Description: Finish top cpuset after cpu, node maps are initialized
2105 **/
2106
2107void __init cpuset_init_smp(void)
2108{
2109 top_cpuset.cpus_allowed = cpu_online_map;
0e1e7c7a 2110 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
4c4d50f7
PJ
2111
2112 hotcpu_notifier(cpuset_handle_cpuhp, 0);
1da177e4
LT
2113}
2114
2115/**
2116 * cpuset_fork - attach newly forked task to its parents cpuset.
d9fd8a6d 2117 * @tsk: pointer to task_struct of forking parent process.
1da177e4 2118 *
053199ed
PJ
2119 * Description: A task inherits its parent's cpuset at fork().
2120 *
2121 * A pointer to the shared cpuset was automatically copied in fork.c
2122 * by dup_task_struct(). However, we ignore that copy, since it was
2123 * not made under the protection of task_lock(), so might no longer be
2124 * a valid cpuset pointer. attach_task() might have already changed
2125 * current->cpuset, allowing the previously referenced cpuset to
2126 * be removed and freed. Instead, we task_lock(current) and copy
2127 * its present value of current->cpuset for our freshly forked child.
2128 *
2129 * At the point that cpuset_fork() is called, 'current' is the parent
2130 * task, and the passed argument 'child' points to the child task.
1da177e4
LT
2131 **/
2132
053199ed 2133void cpuset_fork(struct task_struct *child)
1da177e4 2134{
053199ed
PJ
2135 task_lock(current);
2136 child->cpuset = current->cpuset;
2137 atomic_inc(&child->cpuset->count);
2138 task_unlock(current);
1da177e4
LT
2139}
2140
2141/**
2142 * cpuset_exit - detach cpuset from exiting task
2143 * @tsk: pointer to task_struct of exiting process
2144 *
2145 * Description: Detach cpuset from @tsk and release it.
2146 *
053199ed 2147 * Note that cpusets marked notify_on_release force every task in
3d3f26a7 2148 * them to take the global manage_mutex mutex when exiting.
053199ed
PJ
2149 * This could impact scaling on very large systems. Be reluctant to
2150 * use notify_on_release cpusets where very high task exit scaling
2151 * is required on large systems.
2152 *
2153 * Don't even think about derefencing 'cs' after the cpuset use count
3d3f26a7
IM
2154 * goes to zero, except inside a critical section guarded by manage_mutex
2155 * or callback_mutex. Otherwise a zero cpuset use count is a license to
053199ed
PJ
2156 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
2157 *
3d3f26a7
IM
2158 * This routine has to take manage_mutex, not callback_mutex, because
2159 * it is holding that mutex while calling check_for_release(),
2160 * which calls kmalloc(), so can't be called holding callback_mutex().
053199ed 2161 *
8488bc35 2162 * the_top_cpuset_hack:
06fed338
PJ
2163 *
2164 * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
2165 *
2166 * Don't leave a task unable to allocate memory, as that is an
2167 * accident waiting to happen should someone add a callout in
2168 * do_exit() after the cpuset_exit() call that might allocate.
2169 * If a task tries to allocate memory with an invalid cpuset,
2170 * it will oops in cpuset_update_task_memory_state().
2171 *
2172 * We call cpuset_exit() while the task is still competent to
2173 * handle notify_on_release(), then leave the task attached to
2174 * the root cpuset (top_cpuset) for the remainder of its exit.
2175 *
2176 * To do this properly, we would increment the reference count on
2177 * top_cpuset, and near the very end of the kernel/exit.c do_exit()
2178 * code we would add a second cpuset function call, to drop that
2179 * reference. This would just create an unnecessary hot spot on
2180 * the top_cpuset reference count, to no avail.
2181 *
2182 * Normally, holding a reference to a cpuset without bumping its
2183 * count is unsafe. The cpuset could go away, or someone could
2184 * attach us to a different cpuset, decrementing the count on
2185 * the first cpuset that we never incremented. But in this case,
2186 * top_cpuset isn't going away, and either task has PF_EXITING set,
2187 * which wards off any attach_task() attempts, or task is a failed
2188 * fork, never visible to attach_task.
2189 *
2190 * Another way to do this would be to set the cpuset pointer
2191 * to NULL here, and check in cpuset_update_task_memory_state()
2192 * for a NULL pointer. This hack avoids that NULL check, for no
2193 * cost (other than this way too long comment ;).
1da177e4
LT
2194 **/
2195
2196void cpuset_exit(struct task_struct *tsk)
2197{
2198 struct cpuset *cs;
2199
dd9037a2 2200 task_lock(current);
1da177e4 2201 cs = tsk->cpuset;
8488bc35 2202 tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
dd9037a2 2203 task_unlock(current);
1da177e4 2204
2efe86b8 2205 if (notify_on_release(cs)) {
3077a260
PJ
2206 char *pathbuf = NULL;
2207
3d3f26a7 2208 mutex_lock(&manage_mutex);
2efe86b8 2209 if (atomic_dec_and_test(&cs->count))
3077a260 2210 check_for_release(cs, &pathbuf);
3d3f26a7 2211 mutex_unlock(&manage_mutex);
3077a260 2212 cpuset_release_agent(pathbuf);
2efe86b8
PJ
2213 } else {
2214 atomic_dec(&cs->count);
1da177e4
LT
2215 }
2216}
2217
2218/**
2219 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2220 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2221 *
2222 * Description: Returns the cpumask_t cpus_allowed of the cpuset
2223 * attached to the specified @tsk. Guaranteed to return some non-empty
2224 * subset of cpu_online_map, even if this means going outside the
2225 * tasks cpuset.
2226 **/
2227
909d75a3 2228cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
1da177e4
LT
2229{
2230 cpumask_t mask;
2231
3d3f26a7 2232 mutex_lock(&callback_mutex);
909d75a3 2233 task_lock(tsk);
1da177e4 2234 guarantee_online_cpus(tsk->cpuset, &mask);
909d75a3 2235 task_unlock(tsk);
3d3f26a7 2236 mutex_unlock(&callback_mutex);
1da177e4
LT
2237
2238 return mask;
2239}
2240
2241void cpuset_init_current_mems_allowed(void)
2242{
2243 current->mems_allowed = NODE_MASK_ALL;
2244}
2245
909d75a3
PJ
2246/**
2247 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2248 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2249 *
2250 * Description: Returns the nodemask_t mems_allowed of the cpuset
2251 * attached to the specified @tsk. Guaranteed to return some non-empty
0e1e7c7a 2252 * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
909d75a3
PJ
2253 * tasks cpuset.
2254 **/
2255
2256nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2257{
2258 nodemask_t mask;
2259
3d3f26a7 2260 mutex_lock(&callback_mutex);
909d75a3
PJ
2261 task_lock(tsk);
2262 guarantee_online_mems(tsk->cpuset, &mask);
2263 task_unlock(tsk);
3d3f26a7 2264 mutex_unlock(&callback_mutex);
909d75a3
PJ
2265
2266 return mask;
2267}
2268
d9fd8a6d
RD
2269/**
2270 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2271 * @zl: the zonelist to be checked
2272 *
1da177e4
LT
2273 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2274 */
2275int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
2276{
2277 int i;
2278
2279 for (i = 0; zl->zones[i]; i++) {
89fa3024 2280 int nid = zone_to_nid(zl->zones[i]);
1da177e4
LT
2281
2282 if (node_isset(nid, current->mems_allowed))
2283 return 1;
2284 }
2285 return 0;
2286}
2287
9bf2229f
PJ
2288/*
2289 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
3d3f26a7 2290 * ancestor to the specified cpuset. Call holding callback_mutex.
9bf2229f
PJ
2291 * If no ancestor is mem_exclusive (an unusual configuration), then
2292 * returns the root cpuset.
2293 */
2294static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
2295{
2296 while (!is_mem_exclusive(cs) && cs->parent)
2297 cs = cs->parent;
2298 return cs;
2299}
2300
d9fd8a6d 2301/**
02a0e53d 2302 * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node?
9bf2229f 2303 * @z: is this zone on an allowed node?
02a0e53d 2304 * @gfp_mask: memory allocation flags
d9fd8a6d 2305 *
02a0e53d
PJ
2306 * If we're in interrupt, yes, we can always allocate. If
2307 * __GFP_THISNODE is set, yes, we can always allocate. If zone
9bf2229f
PJ
2308 * z's node is in our tasks mems_allowed, yes. If it's not a
2309 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2310 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
c596d9f3
DR
2311 * If the task has been OOM killed and has access to memory reserves
2312 * as specified by the TIF_MEMDIE flag, yes.
9bf2229f
PJ
2313 * Otherwise, no.
2314 *
02a0e53d
PJ
2315 * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall()
2316 * reduces to cpuset_zone_allowed_hardwall(). Otherwise,
2317 * cpuset_zone_allowed_softwall() might sleep, and might allow a zone
2318 * from an enclosing cpuset.
2319 *
2320 * cpuset_zone_allowed_hardwall() only handles the simpler case of
2321 * hardwall cpusets, and never sleeps.
2322 *
2323 * The __GFP_THISNODE placement logic is really handled elsewhere,
2324 * by forcibly using a zonelist starting at a specified node, and by
2325 * (in get_page_from_freelist()) refusing to consider the zones for
2326 * any node on the zonelist except the first. By the time any such
2327 * calls get to this routine, we should just shut up and say 'yes'.
2328 *
9bf2229f 2329 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
c596d9f3
DR
2330 * and do not allow allocations outside the current tasks cpuset
2331 * unless the task has been OOM killed as is marked TIF_MEMDIE.
9bf2229f 2332 * GFP_KERNEL allocations are not so marked, so can escape to the
02a0e53d 2333 * nearest enclosing mem_exclusive ancestor cpuset.
9bf2229f 2334 *
02a0e53d
PJ
2335 * Scanning up parent cpusets requires callback_mutex. The
2336 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2337 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2338 * current tasks mems_allowed came up empty on the first pass over
2339 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2340 * cpuset are short of memory, might require taking the callback_mutex
2341 * mutex.
9bf2229f 2342 *
36be57ff 2343 * The first call here from mm/page_alloc:get_page_from_freelist()
02a0e53d
PJ
2344 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2345 * so no allocation on a node outside the cpuset is allowed (unless
2346 * in interrupt, of course).
36be57ff
PJ
2347 *
2348 * The second pass through get_page_from_freelist() doesn't even call
2349 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2350 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2351 * in alloc_flags. That logic and the checks below have the combined
2352 * affect that:
9bf2229f
PJ
2353 * in_interrupt - any node ok (current task context irrelevant)
2354 * GFP_ATOMIC - any node ok
c596d9f3 2355 * TIF_MEMDIE - any node ok
9bf2229f
PJ
2356 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2357 * GFP_USER - only nodes in current tasks mems allowed ok.
36be57ff
PJ
2358 *
2359 * Rule:
02a0e53d 2360 * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you
36be57ff
PJ
2361 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2362 * the code that might scan up ancestor cpusets and sleep.
02a0e53d 2363 */
9bf2229f 2364
02a0e53d 2365int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask)
1da177e4 2366{
9bf2229f
PJ
2367 int node; /* node that zone z is on */
2368 const struct cpuset *cs; /* current cpuset ancestors */
29afd49b 2369 int allowed; /* is allocation in zone z allowed? */
9bf2229f 2370
9b819d20 2371 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
9bf2229f 2372 return 1;
89fa3024 2373 node = zone_to_nid(z);
92d1dbd2 2374 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
9bf2229f
PJ
2375 if (node_isset(node, current->mems_allowed))
2376 return 1;
c596d9f3
DR
2377 /*
2378 * Allow tasks that have access to memory reserves because they have
2379 * been OOM killed to get memory anywhere.
2380 */
2381 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2382 return 1;
9bf2229f
PJ
2383 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2384 return 0;
2385
5563e770
BP
2386 if (current->flags & PF_EXITING) /* Let dying task have memory */
2387 return 1;
2388
9bf2229f 2389 /* Not hardwall and node outside mems_allowed: scan up cpusets */
3d3f26a7 2390 mutex_lock(&callback_mutex);
053199ed 2391
053199ed
PJ
2392 task_lock(current);
2393 cs = nearest_exclusive_ancestor(current->cpuset);
2394 task_unlock(current);
2395
9bf2229f 2396 allowed = node_isset(node, cs->mems_allowed);
3d3f26a7 2397 mutex_unlock(&callback_mutex);
9bf2229f 2398 return allowed;
1da177e4
LT
2399}
2400
02a0e53d
PJ
2401/*
2402 * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node?
2403 * @z: is this zone on an allowed node?
2404 * @gfp_mask: memory allocation flags
2405 *
2406 * If we're in interrupt, yes, we can always allocate.
2407 * If __GFP_THISNODE is set, yes, we can always allocate. If zone
c596d9f3
DR
2408 * z's node is in our tasks mems_allowed, yes. If the task has been
2409 * OOM killed and has access to memory reserves as specified by the
2410 * TIF_MEMDIE flag, yes. Otherwise, no.
02a0e53d
PJ
2411 *
2412 * The __GFP_THISNODE placement logic is really handled elsewhere,
2413 * by forcibly using a zonelist starting at a specified node, and by
2414 * (in get_page_from_freelist()) refusing to consider the zones for
2415 * any node on the zonelist except the first. By the time any such
2416 * calls get to this routine, we should just shut up and say 'yes'.
2417 *
2418 * Unlike the cpuset_zone_allowed_softwall() variant, above,
2419 * this variant requires that the zone be in the current tasks
2420 * mems_allowed or that we're in interrupt. It does not scan up the
2421 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2422 * It never sleeps.
2423 */
2424
2425int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask)
2426{
2427 int node; /* node that zone z is on */
2428
2429 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2430 return 1;
2431 node = zone_to_nid(z);
2432 if (node_isset(node, current->mems_allowed))
2433 return 1;
c596d9f3
DR
2434 /*
2435 * Allow tasks that have access to memory reserves because they have
2436 * been OOM killed to get memory anywhere.
2437 */
2438 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2439 return 1;
02a0e53d
PJ
2440 return 0;
2441}
2442
505970b9
PJ
2443/**
2444 * cpuset_lock - lock out any changes to cpuset structures
2445 *
3d3f26a7 2446 * The out of memory (oom) code needs to mutex_lock cpusets
505970b9 2447 * from being changed while it scans the tasklist looking for a
3d3f26a7 2448 * task in an overlapping cpuset. Expose callback_mutex via this
505970b9
PJ
2449 * cpuset_lock() routine, so the oom code can lock it, before
2450 * locking the task list. The tasklist_lock is a spinlock, so
3d3f26a7 2451 * must be taken inside callback_mutex.
505970b9
PJ
2452 */
2453
2454void cpuset_lock(void)
2455{
3d3f26a7 2456 mutex_lock(&callback_mutex);
505970b9
PJ
2457}
2458
2459/**
2460 * cpuset_unlock - release lock on cpuset changes
2461 *
2462 * Undo the lock taken in a previous cpuset_lock() call.
2463 */
2464
2465void cpuset_unlock(void)
2466{
3d3f26a7 2467 mutex_unlock(&callback_mutex);
505970b9
PJ
2468}
2469
825a46af
PJ
2470/**
2471 * cpuset_mem_spread_node() - On which node to begin search for a page
2472 *
2473 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2474 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2475 * and if the memory allocation used cpuset_mem_spread_node()
2476 * to determine on which node to start looking, as it will for
2477 * certain page cache or slab cache pages such as used for file
2478 * system buffers and inode caches, then instead of starting on the
2479 * local node to look for a free page, rather spread the starting
2480 * node around the tasks mems_allowed nodes.
2481 *
2482 * We don't have to worry about the returned node being offline
2483 * because "it can't happen", and even if it did, it would be ok.
2484 *
2485 * The routines calling guarantee_online_mems() are careful to
2486 * only set nodes in task->mems_allowed that are online. So it
2487 * should not be possible for the following code to return an
2488 * offline node. But if it did, that would be ok, as this routine
2489 * is not returning the node where the allocation must be, only
2490 * the node where the search should start. The zonelist passed to
2491 * __alloc_pages() will include all nodes. If the slab allocator
2492 * is passed an offline node, it will fall back to the local node.
2493 * See kmem_cache_alloc_node().
2494 */
2495
2496int cpuset_mem_spread_node(void)
2497{
2498 int node;
2499
2500 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2501 if (node == MAX_NUMNODES)
2502 node = first_node(current->mems_allowed);
2503 current->cpuset_mem_spread_rotor = node;
2504 return node;
2505}
2506EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2507
ef08e3b4
PJ
2508/**
2509 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2510 * @p: pointer to task_struct of some other task.
2511 *
2512 * Description: Return true if the nearest mem_exclusive ancestor
2513 * cpusets of tasks @p and current overlap. Used by oom killer to
2514 * determine if task @p's memory usage might impact the memory
2515 * available to the current task.
2516 *
3d3f26a7 2517 * Call while holding callback_mutex.
ef08e3b4
PJ
2518 **/
2519
2520int cpuset_excl_nodes_overlap(const struct task_struct *p)
2521{
2522 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
0d673a5a 2523 int overlap = 1; /* do cpusets overlap? */
ef08e3b4 2524
053199ed
PJ
2525 task_lock(current);
2526 if (current->flags & PF_EXITING) {
2527 task_unlock(current);
2528 goto done;
2529 }
2530 cs1 = nearest_exclusive_ancestor(current->cpuset);
2531 task_unlock(current);
2532
2533 task_lock((struct task_struct *)p);
2534 if (p->flags & PF_EXITING) {
2535 task_unlock((struct task_struct *)p);
2536 goto done;
2537 }
2538 cs2 = nearest_exclusive_ancestor(p->cpuset);
2539 task_unlock((struct task_struct *)p);
2540
ef08e3b4
PJ
2541 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
2542done:
ef08e3b4
PJ
2543 return overlap;
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{
2574 struct cpuset *cs;
2575
2576 task_lock(current);
2577 cs = current->cpuset;
2578 fmeter_markevent(&cs->fmeter);
2579 task_unlock(current);
2580}
2581
1da177e4
LT
2582/*
2583 * proc_cpuset_show()
2584 * - Print tasks cpuset path into seq_file.
2585 * - Used for /proc/<pid>/cpuset.
053199ed
PJ
2586 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2587 * doesn't really matter if tsk->cpuset changes after we read it,
3d3f26a7 2588 * and we take manage_mutex, keeping attach_task() from changing it
8488bc35
PJ
2589 * anyway. No need to check that tsk->cpuset != NULL, thanks to
2590 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
2591 * cpuset to top_cpuset.
1da177e4 2592 */
1da177e4
LT
2593static int proc_cpuset_show(struct seq_file *m, void *v)
2594{
13b41b09 2595 struct pid *pid;
1da177e4
LT
2596 struct task_struct *tsk;
2597 char *buf;
99f89551 2598 int retval;
1da177e4 2599
99f89551 2600 retval = -ENOMEM;
1da177e4
LT
2601 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2602 if (!buf)
99f89551
EB
2603 goto out;
2604
2605 retval = -ESRCH;
13b41b09
EB
2606 pid = m->private;
2607 tsk = get_pid_task(pid, PIDTYPE_PID);
99f89551
EB
2608 if (!tsk)
2609 goto out_free;
1da177e4 2610
99f89551 2611 retval = -EINVAL;
3d3f26a7 2612 mutex_lock(&manage_mutex);
99f89551 2613
8488bc35 2614 retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE);
1da177e4 2615 if (retval < 0)
99f89551 2616 goto out_unlock;
1da177e4
LT
2617 seq_puts(m, buf);
2618 seq_putc(m, '\n');
99f89551 2619out_unlock:
3d3f26a7 2620 mutex_unlock(&manage_mutex);
99f89551
EB
2621 put_task_struct(tsk);
2622out_free:
1da177e4 2623 kfree(buf);
99f89551 2624out:
1da177e4
LT
2625 return retval;
2626}
2627
2628static int cpuset_open(struct inode *inode, struct file *file)
2629{
13b41b09
EB
2630 struct pid *pid = PROC_I(inode)->pid;
2631 return single_open(file, proc_cpuset_show, pid);
1da177e4
LT
2632}
2633
9a32144e 2634const struct file_operations proc_cpuset_operations = {
1da177e4
LT
2635 .open = cpuset_open,
2636 .read = seq_read,
2637 .llseek = seq_lseek,
2638 .release = single_release,
2639};
2640
2641/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2642char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
2643{
2644 buffer += sprintf(buffer, "Cpus_allowed:\t");
2645 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
2646 buffer += sprintf(buffer, "\n");
2647 buffer += sprintf(buffer, "Mems_allowed:\t");
2648 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
2649 buffer += sprintf(buffer, "\n");
2650 return buffer;
2651}