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