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