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