blob: 6fe28d6f282bea26174b59723ba9c8cf4a3279a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
7 * Copyright (C) 2004 Silicon Graphics, Inc.
8 *
9 * Portions derived from Patrick Mochel's sysfs code.
10 * sysfs is Copyright (c) 2001-3 Patrick Mochel
11 * Portions Copyright (c) 2004 Silicon Graphics, Inc.
12 *
13 * 2003-10-10 Written by Simon Derr <simon.derr@bull.net>
14 * 2003-10-22 Updates by Stephen Hemminger.
15 * 2004 May-July Rework by Paul Jackson <pj@sgi.com>
16 *
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file COPYING in the main directory of the Linux
19 * distribution for more details.
20 */
21
22#include <linux/config.h>
23#include <linux/cpu.h>
24#include <linux/cpumask.h>
25#include <linux/cpuset.h>
26#include <linux/err.h>
27#include <linux/errno.h>
28#include <linux/file.h>
29#include <linux/fs.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/kernel.h>
33#include <linux/kmod.h>
34#include <linux/list.h>
Paul Jackson68860ec2005-10-30 15:02:36 -080035#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mm.h>
37#include <linux/module.h>
38#include <linux/mount.h>
39#include <linux/namei.h>
40#include <linux/pagemap.h>
41#include <linux/proc_fs.h>
Paul Jackson6b9c2602006-01-08 01:02:02 -080042#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sched.h>
44#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/smp_lock.h>
47#include <linux/spinlock.h>
48#include <linux/stat.h>
49#include <linux/string.h>
50#include <linux/time.h>
51#include <linux/backing-dev.h>
52#include <linux/sort.h>
53
54#include <asm/uaccess.h>
55#include <asm/atomic.h>
56#include <asm/semaphore.h>
57
Paul Jacksonc5b2aff2006-01-08 01:01:51 -080058#define CPUSET_SUPER_MAGIC 0x27e0eb
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paul Jackson202f72d2006-01-08 01:01:57 -080060/*
61 * Tracks how many cpusets are currently defined in system.
62 * When there is only one cpuset (the root cpuset) we can
63 * short circuit some hooks.
64 */
65int number_of_cpusets;
66
Paul Jackson3e0d98b2006-01-08 01:01:49 -080067/* See "Frequency meter" comments, below. */
68
69struct fmeter {
70 int cnt; /* unprocessed events count */
71 int val; /* most recent output value */
72 time_t time; /* clock (secs) when val computed */
73 spinlock_t lock; /* guards read or write of above */
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct cpuset {
77 unsigned long flags; /* "unsigned long" so bitops work */
78 cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
79 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
80
Paul Jackson053199e2005-10-30 15:02:30 -080081 /*
82 * Count is atomic so can incr (fork) or decr (exit) without a lock.
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 atomic_t count; /* count tasks using this cpuset */
85
86 /*
87 * We link our 'sibling' struct into our parents 'children'.
88 * Our children link their 'sibling' into our 'children'.
89 */
90 struct list_head sibling; /* my parents children */
91 struct list_head children; /* my children */
92
93 struct cpuset *parent; /* my parent */
94 struct dentry *dentry; /* cpuset fs entry */
95
96 /*
97 * Copy of global cpuset_mems_generation as of the most
98 * recent time this cpuset changed its mems_allowed.
99 */
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800100 int mems_generation;
101
102 struct fmeter fmeter; /* memory_pressure filter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105/* bits in struct cpuset flags field */
106typedef enum {
107 CS_CPU_EXCLUSIVE,
108 CS_MEM_EXCLUSIVE,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800109 CS_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 CS_REMOVED,
111 CS_NOTIFY_ON_RELEASE
112} cpuset_flagbits_t;
113
114/* convenient tests for these bits */
115static inline int is_cpu_exclusive(const struct cpuset *cs)
116{
117 return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
118}
119
120static inline int is_mem_exclusive(const struct cpuset *cs)
121{
122 return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
123}
124
125static inline int is_removed(const struct cpuset *cs)
126{
127 return !!test_bit(CS_REMOVED, &cs->flags);
128}
129
130static inline int notify_on_release(const struct cpuset *cs)
131{
132 return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
133}
134
Paul Jackson45b07ef2006-01-08 01:00:56 -0800135static inline int is_memory_migrate(const struct cpuset *cs)
136{
137 return !!test_bit(CS_MEMORY_MIGRATE, &cs->flags);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Increment this atomic integer everytime any cpuset changes its
142 * mems_allowed value. Users of cpusets can track this generation
143 * number, and avoid having to lock and reload mems_allowed unless
144 * the cpuset they're using changes generation.
145 *
146 * A single, global generation is needed because attach_task() could
147 * reattach a task to a different cpuset, which must not have its
148 * generation numbers aliased with those of that tasks previous cpuset.
149 *
150 * Generations are needed for mems_allowed because one task cannot
151 * modify anothers memory placement. So we must enable every task,
152 * on every visit to __alloc_pages(), to efficiently check whether
153 * its current->cpuset->mems_allowed has changed, requiring an update
154 * of its current->mems_allowed.
155 */
156static atomic_t cpuset_mems_generation = ATOMIC_INIT(1);
157
158static struct cpuset top_cpuset = {
159 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
160 .cpus_allowed = CPU_MASK_ALL,
161 .mems_allowed = NODE_MASK_ALL,
162 .count = ATOMIC_INIT(0),
163 .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
164 .children = LIST_HEAD_INIT(top_cpuset.children),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167static struct vfsmount *cpuset_mount;
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800168static struct super_block *cpuset_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/*
Paul Jackson053199e2005-10-30 15:02:30 -0800171 * We have two global cpuset semaphores below. They can nest.
172 * It is ok to first take manage_sem, then nest callback_sem. We also
173 * require taking task_lock() when dereferencing a tasks cpuset pointer.
174 * See "The task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
Paul Jackson053199e2005-10-30 15:02:30 -0800176 * A task must hold both semaphores to modify cpusets. If a task
177 * holds manage_sem, then it blocks others wanting that semaphore,
178 * ensuring that it is the only task able to also acquire callback_sem
179 * and be able to modify cpusets. It can perform various checks on
180 * the cpuset structure first, knowing nothing will change. It can
181 * also allocate memory while just holding manage_sem. While it is
182 * performing these checks, various callback routines can briefly
183 * acquire callback_sem to query cpusets. Once it is ready to make
184 * the changes, it takes callback_sem, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
Paul Jackson053199e2005-10-30 15:02:30 -0800186 * Calls to the kernel memory allocator can not be made while holding
187 * callback_sem, as that would risk double tripping on callback_sem
188 * from one of the callbacks into the cpuset code from within
189 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
Paul Jackson053199e2005-10-30 15:02:30 -0800191 * If a task is only holding callback_sem, then it has read-only
192 * access to cpusets.
193 *
194 * The task_struct fields mems_allowed and mems_generation may only
195 * be accessed in the context of that task, so require no locks.
196 *
197 * Any task can increment and decrement the count field without lock.
198 * So in general, code holding manage_sem or callback_sem can't rely
199 * on the count field not changing. However, if the count goes to
200 * zero, then only attach_task(), which holds both semaphores, can
201 * increment it again. Because a count of zero means that no tasks
202 * are currently attached, therefore there is no way a task attached
203 * to that cpuset can fork (the other way to increment the count).
204 * So code holding manage_sem or callback_sem can safely assume that
205 * if the count is zero, it will stay zero. Similarly, if a task
206 * holds manage_sem or callback_sem on a cpuset with zero count, it
207 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
208 * both of those semaphores.
209 *
210 * A possible optimization to improve parallelism would be to make
211 * callback_sem a R/W semaphore (rwsem), allowing the callback routines
212 * to proceed in parallel, with read access, until the holder of
213 * manage_sem needed to take this rwsem for exclusive write access
214 * and modify some cpusets.
215 *
216 * The cpuset_common_file_write handler for operations that modify
217 * the cpuset hierarchy holds manage_sem across the entire operation,
218 * single threading all such cpuset modifications across the system.
219 *
220 * The cpuset_common_file_read() handlers only hold callback_sem across
221 * small pieces of code, such as when reading out possibly multi-word
222 * cpumasks and nodemasks.
223 *
224 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
225 * (usually) take either semaphore. These are the two most performance
226 * critical pieces of code here. The exception occurs on cpuset_exit(),
227 * when a task in a notify_on_release cpuset exits. Then manage_sem
Paul Jackson2efe86b2005-05-27 02:02:43 -0700228 * is taken, and if the cpuset count is zero, a usermode call made
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * to /sbin/cpuset_release_agent with the name of the cpuset (path
230 * relative to the root of cpuset file system) as the argument.
231 *
Paul Jackson053199e2005-10-30 15:02:30 -0800232 * A cpuset can only be deleted if both its 'count' of using tasks
233 * is zero, and its list of 'children' cpusets is empty. Since all
234 * tasks in the system use _some_ cpuset, and since there is always at
235 * least one task in the system (init, pid == 1), therefore, top_cpuset
236 * always has either children cpusets and/or using tasks. So we don't
237 * need a special hack to ensure that top_cpuset cannot be deleted.
238 *
239 * The above "Tale of Two Semaphores" would be complete, but for:
240 *
241 * The task_lock() exception
242 *
243 * The need for this exception arises from the action of attach_task(),
244 * which overwrites one tasks cpuset pointer with another. It does
245 * so using both semaphores, however there are several performance
246 * critical places that need to reference task->cpuset without the
247 * expense of grabbing a system global semaphore. Therefore except as
248 * noted below, when dereferencing or, as in attach_task(), modifying
249 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
250 * (task->alloc_lock) already in the task_struct routinely used for
251 * such matters.
Paul Jackson6b9c2602006-01-08 01:02:02 -0800252 *
253 * P.S. One more locking exception. RCU is used to guard the
254 * update of a tasks cpuset pointer by attach_task() and the
255 * access of task->cpuset->mems_generation via that pointer in
256 * the routine cpuset_update_task_memory_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258
Paul Jackson053199e2005-10-30 15:02:30 -0800259static DECLARE_MUTEX(manage_sem);
260static DECLARE_MUTEX(callback_sem);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700261
262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * A couple of forward declarations required, due to cyclic reference loop:
264 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
265 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
266 */
267
268static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
269static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
270
271static struct backing_dev_info cpuset_backing_dev_info = {
272 .ra_pages = 0, /* No readahead */
273 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
274};
275
276static struct inode *cpuset_new_inode(mode_t mode)
277{
278 struct inode *inode = new_inode(cpuset_sb);
279
280 if (inode) {
281 inode->i_mode = mode;
282 inode->i_uid = current->fsuid;
283 inode->i_gid = current->fsgid;
284 inode->i_blksize = PAGE_CACHE_SIZE;
285 inode->i_blocks = 0;
286 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
287 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
288 }
289 return inode;
290}
291
292static void cpuset_diput(struct dentry *dentry, struct inode *inode)
293{
294 /* is dentry a directory ? if so, kfree() associated cpuset */
295 if (S_ISDIR(inode->i_mode)) {
296 struct cpuset *cs = dentry->d_fsdata;
297 BUG_ON(!(is_removed(cs)));
298 kfree(cs);
299 }
300 iput(inode);
301}
302
303static struct dentry_operations cpuset_dops = {
304 .d_iput = cpuset_diput,
305};
306
307static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
308{
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700309 struct dentry *d = lookup_one_len(name, parent, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!IS_ERR(d))
311 d->d_op = &cpuset_dops;
312 return d;
313}
314
315static void remove_dir(struct dentry *d)
316{
317 struct dentry *parent = dget(d->d_parent);
318
319 d_delete(d);
320 simple_rmdir(parent->d_inode, d);
321 dput(parent);
322}
323
324/*
325 * NOTE : the dentry must have been dget()'ed
326 */
327static void cpuset_d_remove_dir(struct dentry *dentry)
328{
329 struct list_head *node;
330
331 spin_lock(&dcache_lock);
332 node = dentry->d_subdirs.next;
333 while (node != &dentry->d_subdirs) {
334 struct dentry *d = list_entry(node, struct dentry, d_child);
335 list_del_init(node);
336 if (d->d_inode) {
337 d = dget_locked(d);
338 spin_unlock(&dcache_lock);
339 d_delete(d);
340 simple_unlink(dentry->d_inode, d);
341 dput(d);
342 spin_lock(&dcache_lock);
343 }
344 node = dentry->d_subdirs.next;
345 }
346 list_del_init(&dentry->d_child);
347 spin_unlock(&dcache_lock);
348 remove_dir(dentry);
349}
350
351static struct super_operations cpuset_ops = {
352 .statfs = simple_statfs,
353 .drop_inode = generic_delete_inode,
354};
355
356static int cpuset_fill_super(struct super_block *sb, void *unused_data,
357 int unused_silent)
358{
359 struct inode *inode;
360 struct dentry *root;
361
362 sb->s_blocksize = PAGE_CACHE_SIZE;
363 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
364 sb->s_magic = CPUSET_SUPER_MAGIC;
365 sb->s_op = &cpuset_ops;
366 cpuset_sb = sb;
367
368 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
369 if (inode) {
370 inode->i_op = &simple_dir_inode_operations;
371 inode->i_fop = &simple_dir_operations;
372 /* directories start off with i_nlink == 2 (for "." entry) */
373 inode->i_nlink++;
374 } else {
375 return -ENOMEM;
376 }
377
378 root = d_alloc_root(inode);
379 if (!root) {
380 iput(inode);
381 return -ENOMEM;
382 }
383 sb->s_root = root;
384 return 0;
385}
386
387static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
388 int flags, const char *unused_dev_name,
389 void *data)
390{
391 return get_sb_single(fs_type, flags, data, cpuset_fill_super);
392}
393
394static struct file_system_type cpuset_fs_type = {
395 .name = "cpuset",
396 .get_sb = cpuset_get_sb,
397 .kill_sb = kill_litter_super,
398};
399
400/* struct cftype:
401 *
402 * The files in the cpuset filesystem mostly have a very simple read/write
403 * handling, some common function will take care of it. Nevertheless some cases
404 * (read tasks) are special and therefore I define this structure for every
405 * kind of file.
406 *
407 *
408 * When reading/writing to a file:
409 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
410 * - the 'cftype' of the file is file->f_dentry->d_fsdata
411 */
412
413struct cftype {
414 char *name;
415 int private;
416 int (*open) (struct inode *inode, struct file *file);
417 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
418 loff_t *ppos);
419 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
420 loff_t *ppos);
421 int (*release) (struct inode *inode, struct file *file);
422};
423
424static inline struct cpuset *__d_cs(struct dentry *dentry)
425{
426 return dentry->d_fsdata;
427}
428
429static inline struct cftype *__d_cft(struct dentry *dentry)
430{
431 return dentry->d_fsdata;
432}
433
434/*
Paul Jackson053199e2005-10-30 15:02:30 -0800435 * Call with manage_sem held. Writes path of cpuset into buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * Returns 0 on success, -errno on error.
437 */
438
439static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
440{
441 char *start;
442
443 start = buf + buflen;
444
445 *--start = '\0';
446 for (;;) {
447 int len = cs->dentry->d_name.len;
448 if ((start -= len) < buf)
449 return -ENAMETOOLONG;
450 memcpy(start, cs->dentry->d_name.name, len);
451 cs = cs->parent;
452 if (!cs)
453 break;
454 if (!cs->parent)
455 continue;
456 if (--start < buf)
457 return -ENAMETOOLONG;
458 *start = '/';
459 }
460 memmove(buf, start, buf + buflen - start);
461 return 0;
462}
463
464/*
465 * Notify userspace when a cpuset is released, by running
466 * /sbin/cpuset_release_agent with the name of the cpuset (path
467 * relative to the root of cpuset file system) as the argument.
468 *
469 * Most likely, this user command will try to rmdir this cpuset.
470 *
471 * This races with the possibility that some other task will be
472 * attached to this cpuset before it is removed, or that some other
473 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
474 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
475 * unused, and this cpuset will be reprieved from its death sentence,
476 * to continue to serve a useful existence. Next time it's released,
477 * we will get notified again, if it still has 'notify_on_release' set.
478 *
Paul Jackson3077a262005-08-09 10:07:59 -0700479 * The final arg to call_usermodehelper() is 0, which means don't
480 * wait. The separate /sbin/cpuset_release_agent task is forked by
481 * call_usermodehelper(), then control in this thread returns here,
482 * without waiting for the release agent task. We don't bother to
483 * wait because the caller of this routine has no use for the exit
484 * status of the /sbin/cpuset_release_agent task, so no sense holding
485 * our caller up for that.
486 *
Paul Jackson053199e2005-10-30 15:02:30 -0800487 * When we had only one cpuset semaphore, we had to call this
488 * without holding it, to avoid deadlock when call_usermodehelper()
489 * allocated memory. With two locks, we could now call this while
490 * holding manage_sem, but we still don't, so as to minimize
491 * the time manage_sem is held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
493
Paul Jackson3077a262005-08-09 10:07:59 -0700494static void cpuset_release_agent(const char *pathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 char *argv[3], *envp[3];
497 int i;
498
Paul Jackson3077a262005-08-09 10:07:59 -0700499 if (!pathbuf)
500 return;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 i = 0;
503 argv[i++] = "/sbin/cpuset_release_agent";
Paul Jackson3077a262005-08-09 10:07:59 -0700504 argv[i++] = (char *)pathbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 argv[i] = NULL;
506
507 i = 0;
508 /* minimal command environment */
509 envp[i++] = "HOME=/";
510 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
511 envp[i] = NULL;
512
Paul Jackson3077a262005-08-09 10:07:59 -0700513 call_usermodehelper(argv[0], argv, envp, 0);
514 kfree(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517/*
518 * Either cs->count of using tasks transitioned to zero, or the
519 * cs->children list of child cpusets just became empty. If this
520 * cs is notify_on_release() and now both the user count is zero and
Paul Jackson3077a262005-08-09 10:07:59 -0700521 * the list of children is empty, prepare cpuset path in a kmalloc'd
522 * buffer, to be returned via ppathbuf, so that the caller can invoke
Paul Jackson053199e2005-10-30 15:02:30 -0800523 * cpuset_release_agent() with it later on, once manage_sem is dropped.
524 * Call here with manage_sem held.
Paul Jackson3077a262005-08-09 10:07:59 -0700525 *
526 * This check_for_release() routine is responsible for kmalloc'ing
527 * pathbuf. The above cpuset_release_agent() is responsible for
528 * kfree'ing pathbuf. The caller of these routines is responsible
529 * for providing a pathbuf pointer, initialized to NULL, then
Paul Jackson053199e2005-10-30 15:02:30 -0800530 * calling check_for_release() with manage_sem held and the address
531 * of the pathbuf pointer, then dropping manage_sem, then calling
Paul Jackson3077a262005-08-09 10:07:59 -0700532 * cpuset_release_agent() with pathbuf, as set by check_for_release().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
534
Paul Jackson3077a262005-08-09 10:07:59 -0700535static void check_for_release(struct cpuset *cs, char **ppathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
538 list_empty(&cs->children)) {
539 char *buf;
540
541 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
542 if (!buf)
543 return;
544 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
Paul Jackson3077a262005-08-09 10:07:59 -0700545 kfree(buf);
546 else
547 *ppathbuf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549}
550
551/*
552 * Return in *pmask the portion of a cpusets's cpus_allowed that
553 * are online. If none are online, walk up the cpuset hierarchy
554 * until we find one that does have some online cpus. If we get
555 * all the way to the top and still haven't found any online cpus,
556 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
557 * task, return cpu_online_map.
558 *
559 * One way or another, we guarantee to return some non-empty subset
560 * of cpu_online_map.
561 *
Paul Jackson053199e2005-10-30 15:02:30 -0800562 * Call with callback_sem held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
564
565static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
566{
567 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
568 cs = cs->parent;
569 if (cs)
570 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
571 else
572 *pmask = cpu_online_map;
573 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
574}
575
576/*
577 * Return in *pmask the portion of a cpusets's mems_allowed that
578 * are online. If none are online, walk up the cpuset hierarchy
579 * until we find one that does have some online mems. If we get
580 * all the way to the top and still haven't found any online mems,
581 * return node_online_map.
582 *
583 * One way or another, we guarantee to return some non-empty subset
584 * of node_online_map.
585 *
Paul Jackson053199e2005-10-30 15:02:30 -0800586 * Call with callback_sem held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588
589static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
590{
591 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
592 cs = cs->parent;
593 if (cs)
594 nodes_and(*pmask, cs->mems_allowed, node_online_map);
595 else
596 *pmask = node_online_map;
597 BUG_ON(!nodes_intersects(*pmask, node_online_map));
598}
599
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800600/**
601 * cpuset_update_task_memory_state - update task memory placement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800603 * If the current tasks cpusets mems_allowed changed behind our
604 * backs, update current->mems_allowed, mems_generation and task NUMA
605 * mempolicy to the new value.
606 *
607 * Task mempolicy is updated by rebinding it relative to the
608 * current->cpuset if a task has its memory placement changed.
609 * Do not call this routine if in_interrupt().
610 *
611 * Call without callback_sem or task_lock() held. May be called
Paul Jacksonc417f022006-01-08 01:02:01 -0800612 * with or without manage_sem held. Doesn't need task_lock to guard
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800613 * against another task changing a non-NULL cpuset pointer to NULL,
614 * as that is only done by a task on itself, and if the current task
615 * is here, it is not simultaneously in the exit code NULL'ing its
616 * cpuset pointer. This routine also might acquire callback_sem and
617 * current->mm->mmap_sem during call.
Paul Jackson5aa15b52005-10-30 15:02:28 -0800618 *
Paul Jackson6b9c2602006-01-08 01:02:02 -0800619 * Reading current->cpuset->mems_generation doesn't need task_lock
620 * to guard the current->cpuset derefence, because it is guarded
621 * from concurrent freeing of current->cpuset by attach_task(),
622 * using RCU.
623 *
624 * The rcu_dereference() is technically probably not needed,
625 * as I don't actually mind if I see a new cpuset pointer but
626 * an old value of mems_generation. However this really only
627 * matters on alpha systems using cpusets heavily. If I dropped
628 * that rcu_dereference(), it would save them a memory barrier.
629 * For all other arch's, rcu_dereference is a no-op anyway, and for
630 * alpha systems not using cpusets, another planned optimization,
631 * avoiding the rcu critical section for tasks in the root cpuset
632 * which is statically allocated, so can't vanish, will make this
633 * irrelevant. Better to use RCU as intended, than to engage in
634 * some cute trick to save a memory barrier that is impossible to
635 * test, for alpha systems using cpusets heavily, which might not
636 * even exist.
Paul Jackson053199e2005-10-30 15:02:30 -0800637 *
638 * This routine is needed to update the per-task mems_allowed data,
639 * within the tasks context, when it is trying to allocate memory
640 * (in various mm/mempolicy.c routines) and notices that some other
641 * task has been modifying its cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800644void cpuset_update_task_memory_state()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Paul Jackson053199e2005-10-30 15:02:30 -0800646 int my_cpusets_mem_gen;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800647 struct task_struct *tsk = current;
Paul Jackson6b9c2602006-01-08 01:02:02 -0800648 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Paul Jackson6b9c2602006-01-08 01:02:02 -0800650 rcu_read_lock();
651 cs = rcu_dereference(tsk->cpuset);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800652 my_cpusets_mem_gen = cs->mems_generation;
Paul Jackson6b9c2602006-01-08 01:02:02 -0800653 rcu_read_unlock();
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800654
655 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
Paul Jackson053199e2005-10-30 15:02:30 -0800656 down(&callback_sem);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800657 task_lock(tsk);
658 cs = tsk->cpuset; /* Maybe changed when task not locked */
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800659 guarantee_online_mems(cs, &tsk->mems_allowed);
660 tsk->cpuset_mems_generation = cs->mems_generation;
661 task_unlock(tsk);
Paul Jackson053199e2005-10-30 15:02:30 -0800662 up(&callback_sem);
Paul Jackson74cb2152006-01-08 01:01:56 -0800663 mpol_rebind_task(tsk, &tsk->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665}
666
667/*
668 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
669 *
670 * One cpuset is a subset of another if all its allowed CPUs and
671 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Jackson053199e2005-10-30 15:02:30 -0800672 * are only set if the other's are set. Call holding manage_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
674
675static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
676{
677 return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
678 nodes_subset(p->mems_allowed, q->mems_allowed) &&
679 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
680 is_mem_exclusive(p) <= is_mem_exclusive(q);
681}
682
683/*
684 * validate_change() - Used to validate that any proposed cpuset change
685 * follows the structural rules for cpusets.
686 *
687 * If we replaced the flag and mask values of the current cpuset
688 * (cur) with those values in the trial cpuset (trial), would
689 * our various subset and exclusive rules still be valid? Presumes
Paul Jackson053199e2005-10-30 15:02:30 -0800690 * manage_sem held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 *
692 * 'cur' is the address of an actual, in-use cpuset. Operations
693 * such as list traversal that depend on the actual address of the
694 * cpuset in the list must use cur below, not trial.
695 *
696 * 'trial' is the address of bulk structure copy of cur, with
697 * perhaps one or more of the fields cpus_allowed, mems_allowed,
698 * or flags changed to new, trial values.
699 *
700 * Return 0 if valid, -errno if not.
701 */
702
703static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
704{
705 struct cpuset *c, *par;
706
707 /* Each of our child cpusets must be a subset of us */
708 list_for_each_entry(c, &cur->children, sibling) {
709 if (!is_cpuset_subset(c, trial))
710 return -EBUSY;
711 }
712
713 /* Remaining checks don't apply to root cpuset */
714 if ((par = cur->parent) == NULL)
715 return 0;
716
717 /* We must be a subset of our parent cpuset */
718 if (!is_cpuset_subset(trial, par))
719 return -EACCES;
720
721 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
722 list_for_each_entry(c, &par->children, sibling) {
723 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
724 c != cur &&
725 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
726 return -EINVAL;
727 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
728 c != cur &&
729 nodes_intersects(trial->mems_allowed, c->mems_allowed))
730 return -EINVAL;
731 }
732
733 return 0;
734}
735
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700736/*
737 * For a given cpuset cur, partition the system as follows
738 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
739 * exclusive child cpusets
740 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
741 * exclusive child cpusets
742 * Build these two partitions by calling partition_sched_domains
743 *
Paul Jackson053199e2005-10-30 15:02:30 -0800744 * Call with manage_sem held. May nest a call to the
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700745 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
746 */
Paul Jackson212d6d22005-08-25 12:47:56 -0700747
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700748static void update_cpu_domains(struct cpuset *cur)
749{
750 struct cpuset *c, *par = cur->parent;
751 cpumask_t pspan, cspan;
752
753 if (par == NULL || cpus_empty(cur->cpus_allowed))
754 return;
755
756 /*
757 * Get all cpus from parent's cpus_allowed not part of exclusive
758 * children
759 */
760 pspan = par->cpus_allowed;
761 list_for_each_entry(c, &par->children, sibling) {
762 if (is_cpu_exclusive(c))
763 cpus_andnot(pspan, pspan, c->cpus_allowed);
764 }
765 if (is_removed(cur) || !is_cpu_exclusive(cur)) {
766 cpus_or(pspan, pspan, cur->cpus_allowed);
767 if (cpus_equal(pspan, cur->cpus_allowed))
768 return;
769 cspan = CPU_MASK_NONE;
770 } else {
771 if (cpus_empty(pspan))
772 return;
773 cspan = cur->cpus_allowed;
774 /*
775 * Get all cpus from current cpuset's cpus_allowed not part
776 * of exclusive children
777 */
778 list_for_each_entry(c, &cur->children, sibling) {
779 if (is_cpu_exclusive(c))
780 cpus_andnot(cspan, cspan, c->cpus_allowed);
781 }
782 }
783
784 lock_cpu_hotplug();
785 partition_sched_domains(&pspan, &cspan);
786 unlock_cpu_hotplug();
787}
788
Paul Jackson053199e2005-10-30 15:02:30 -0800789/*
790 * Call with manage_sem held. May take callback_sem during call.
791 */
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793static int update_cpumask(struct cpuset *cs, char *buf)
794{
795 struct cpuset trialcs;
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700796 int retval, cpus_unchanged;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 trialcs = *cs;
799 retval = cpulist_parse(buf, trialcs.cpus_allowed);
800 if (retval < 0)
801 return retval;
802 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
803 if (cpus_empty(trialcs.cpus_allowed))
804 return -ENOSPC;
805 retval = validate_change(cs, &trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700806 if (retval < 0)
807 return retval;
808 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
Paul Jackson053199e2005-10-30 15:02:30 -0800809 down(&callback_sem);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700810 cs->cpus_allowed = trialcs.cpus_allowed;
Paul Jackson053199e2005-10-30 15:02:30 -0800811 up(&callback_sem);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700812 if (is_cpu_exclusive(cs) && !cpus_unchanged)
813 update_cpu_domains(cs);
814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Paul Jackson053199e2005-10-30 15:02:30 -0800817/*
Paul Jackson42253992006-01-08 01:01:59 -0800818 * Handle user request to change the 'mems' memory placement
819 * of a cpuset. Needs to validate the request, update the
820 * cpusets mems_allowed and mems_generation, and for each
Paul Jackson04c19fa2006-01-08 01:02:00 -0800821 * task in the cpuset, rebind any vma mempolicies and if
822 * the cpuset is marked 'memory_migrate', migrate the tasks
823 * pages to the new memory.
Paul Jackson42253992006-01-08 01:01:59 -0800824 *
Paul Jackson053199e2005-10-30 15:02:30 -0800825 * Call with manage_sem held. May take callback_sem during call.
Paul Jackson42253992006-01-08 01:01:59 -0800826 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
827 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
828 * their mempolicies to the cpusets new mems_allowed.
Paul Jackson053199e2005-10-30 15:02:30 -0800829 */
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static int update_nodemask(struct cpuset *cs, char *buf)
832{
833 struct cpuset trialcs;
Paul Jackson04c19fa2006-01-08 01:02:00 -0800834 nodemask_t oldmem;
Paul Jackson42253992006-01-08 01:01:59 -0800835 struct task_struct *g, *p;
836 struct mm_struct **mmarray;
837 int i, n, ntasks;
Paul Jackson04c19fa2006-01-08 01:02:00 -0800838 int migrate;
Paul Jackson42253992006-01-08 01:01:59 -0800839 int fudge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int retval;
841
842 trialcs = *cs;
843 retval = nodelist_parse(buf, trialcs.mems_allowed);
844 if (retval < 0)
Paul Jackson59dac162006-01-08 01:01:52 -0800845 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
Paul Jackson04c19fa2006-01-08 01:02:00 -0800847 oldmem = cs->mems_allowed;
848 if (nodes_equal(oldmem, trialcs.mems_allowed)) {
849 retval = 0; /* Too easy - nothing to do */
850 goto done;
851 }
Paul Jackson59dac162006-01-08 01:01:52 -0800852 if (nodes_empty(trialcs.mems_allowed)) {
853 retval = -ENOSPC;
854 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Paul Jackson59dac162006-01-08 01:01:52 -0800856 retval = validate_change(cs, &trialcs);
857 if (retval < 0)
858 goto done;
859
860 down(&callback_sem);
861 cs->mems_allowed = trialcs.mems_allowed;
862 atomic_inc(&cpuset_mems_generation);
863 cs->mems_generation = atomic_read(&cpuset_mems_generation);
864 up(&callback_sem);
865
Paul Jackson42253992006-01-08 01:01:59 -0800866 set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
867
868 fudge = 10; /* spare mmarray[] slots */
869 fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
870 retval = -ENOMEM;
871
872 /*
873 * Allocate mmarray[] to hold mm reference for each task
874 * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
875 * tasklist_lock. We could use GFP_ATOMIC, but with a
876 * few more lines of code, we can retry until we get a big
877 * enough mmarray[] w/o using GFP_ATOMIC.
878 */
879 while (1) {
880 ntasks = atomic_read(&cs->count); /* guess */
881 ntasks += fudge;
882 mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
883 if (!mmarray)
884 goto done;
885 write_lock_irq(&tasklist_lock); /* block fork */
886 if (atomic_read(&cs->count) <= ntasks)
887 break; /* got enough */
888 write_unlock_irq(&tasklist_lock); /* try again */
889 kfree(mmarray);
890 }
891
892 n = 0;
893
894 /* Load up mmarray[] with mm reference for each task in cpuset. */
895 do_each_thread(g, p) {
896 struct mm_struct *mm;
897
898 if (n >= ntasks) {
899 printk(KERN_WARNING
900 "Cpuset mempolicy rebind incomplete.\n");
901 continue;
902 }
903 if (p->cpuset != cs)
904 continue;
905 mm = get_task_mm(p);
906 if (!mm)
907 continue;
908 mmarray[n++] = mm;
909 } while_each_thread(g, p);
910 write_unlock_irq(&tasklist_lock);
911
912 /*
913 * Now that we've dropped the tasklist spinlock, we can
914 * rebind the vma mempolicies of each mm in mmarray[] to their
915 * new cpuset, and release that mm. The mpol_rebind_mm()
916 * call takes mmap_sem, which we couldn't take while holding
917 * tasklist_lock. Forks can happen again now - the mpol_copy()
918 * cpuset_being_rebound check will catch such forks, and rebind
919 * their vma mempolicies too. Because we still hold the global
920 * cpuset manage_sem, we know that no other rebind effort will
921 * be contending for the global variable cpuset_being_rebound.
922 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -0800923 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -0800924 */
Paul Jackson04c19fa2006-01-08 01:02:00 -0800925 migrate = is_memory_migrate(cs);
Paul Jackson42253992006-01-08 01:01:59 -0800926 for (i = 0; i < n; i++) {
927 struct mm_struct *mm = mmarray[i];
928
929 mpol_rebind_mm(mm, &cs->mems_allowed);
Paul Jackson04c19fa2006-01-08 01:02:00 -0800930 if (migrate) {
931 do_migrate_pages(mm, &oldmem, &cs->mems_allowed,
932 MPOL_MF_MOVE_ALL);
933 }
Paul Jackson42253992006-01-08 01:01:59 -0800934 mmput(mm);
935 }
936
937 /* We're done rebinding vma's to this cpusets new mems_allowed. */
938 kfree(mmarray);
939 set_cpuset_being_rebound(NULL);
940 retval = 0;
Paul Jackson59dac162006-01-08 01:01:52 -0800941done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return retval;
943}
944
945/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800946 * Call with manage_sem held.
947 */
948
949static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
950{
951 if (simple_strtoul(buf, NULL, 10) != 0)
952 cpuset_memory_pressure_enabled = 1;
953 else
954 cpuset_memory_pressure_enabled = 0;
955 return 0;
956}
957
958/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * update_flag - read a 0 or a 1 in a file and update associated flag
960 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800961 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * cs: the cpuset to update
963 * buf: the buffer where we read the 0 or 1
Paul Jackson053199e2005-10-30 15:02:30 -0800964 *
965 * Call with manage_sem held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
967
968static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
969{
970 int turning_on;
971 struct cpuset trialcs;
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700972 int err, cpu_exclusive_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
975
976 trialcs = *cs;
977 if (turning_on)
978 set_bit(bit, &trialcs.flags);
979 else
980 clear_bit(bit, &trialcs.flags);
981
982 err = validate_change(cs, &trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700983 if (err < 0)
984 return err;
985 cpu_exclusive_changed =
986 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
Paul Jackson053199e2005-10-30 15:02:30 -0800987 down(&callback_sem);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700988 if (turning_on)
989 set_bit(bit, &cs->flags);
990 else
991 clear_bit(bit, &cs->flags);
Paul Jackson053199e2005-10-30 15:02:30 -0800992 up(&callback_sem);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700993
994 if (cpu_exclusive_changed)
995 update_cpu_domains(cs);
996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Paul Jackson053199e2005-10-30 15:02:30 -0800999/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001000 * Frequency meter - How fast is some event occuring?
1001 *
1002 * These routines manage a digitally filtered, constant time based,
1003 * event frequency meter. There are four routines:
1004 * fmeter_init() - initialize a frequency meter.
1005 * fmeter_markevent() - called each time the event happens.
1006 * fmeter_getrate() - returns the recent rate of such events.
1007 * fmeter_update() - internal routine used to update fmeter.
1008 *
1009 * A common data structure is passed to each of these routines,
1010 * which is used to keep track of the state required to manage the
1011 * frequency meter and its digital filter.
1012 *
1013 * The filter works on the number of events marked per unit time.
1014 * The filter is single-pole low-pass recursive (IIR). The time unit
1015 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1016 * simulate 3 decimal digits of precision (multiplied by 1000).
1017 *
1018 * With an FM_COEF of 933, and a time base of 1 second, the filter
1019 * has a half-life of 10 seconds, meaning that if the events quit
1020 * happening, then the rate returned from the fmeter_getrate()
1021 * will be cut in half each 10 seconds, until it converges to zero.
1022 *
1023 * It is not worth doing a real infinitely recursive filter. If more
1024 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1025 * just compute FM_MAXTICKS ticks worth, by which point the level
1026 * will be stable.
1027 *
1028 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1029 * arithmetic overflow in the fmeter_update() routine.
1030 *
1031 * Given the simple 32 bit integer arithmetic used, this meter works
1032 * best for reporting rates between one per millisecond (msec) and
1033 * one per 32 (approx) seconds. At constant rates faster than one
1034 * per msec it maxes out at values just under 1,000,000. At constant
1035 * rates between one per msec, and one per second it will stabilize
1036 * to a value N*1000, where N is the rate of events per second.
1037 * At constant rates between one per second and one per 32 seconds,
1038 * it will be choppy, moving up on the seconds that have an event,
1039 * and then decaying until the next event. At rates slower than
1040 * about one in 32 seconds, it decays all the way back to zero between
1041 * each event.
1042 */
1043
1044#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1045#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1046#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1047#define FM_SCALE 1000 /* faux fixed point scale */
1048
1049/* Initialize a frequency meter */
1050static void fmeter_init(struct fmeter *fmp)
1051{
1052 fmp->cnt = 0;
1053 fmp->val = 0;
1054 fmp->time = 0;
1055 spin_lock_init(&fmp->lock);
1056}
1057
1058/* Internal meter update - process cnt events and update value */
1059static void fmeter_update(struct fmeter *fmp)
1060{
1061 time_t now = get_seconds();
1062 time_t ticks = now - fmp->time;
1063
1064 if (ticks == 0)
1065 return;
1066
1067 ticks = min(FM_MAXTICKS, ticks);
1068 while (ticks-- > 0)
1069 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1070 fmp->time = now;
1071
1072 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1073 fmp->cnt = 0;
1074}
1075
1076/* Process any previous ticks, then bump cnt by one (times scale). */
1077static void fmeter_markevent(struct fmeter *fmp)
1078{
1079 spin_lock(&fmp->lock);
1080 fmeter_update(fmp);
1081 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1082 spin_unlock(&fmp->lock);
1083}
1084
1085/* Process any previous ticks, then return current value. */
1086static int fmeter_getrate(struct fmeter *fmp)
1087{
1088 int val;
1089
1090 spin_lock(&fmp->lock);
1091 fmeter_update(fmp);
1092 val = fmp->val;
1093 spin_unlock(&fmp->lock);
1094 return val;
1095}
1096
1097/*
Paul Jackson053199e2005-10-30 15:02:30 -08001098 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
1099 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1100 * notified on release.
1101 *
1102 * Call holding manage_sem. May take callback_sem and task_lock of
1103 * the task 'pid' during call.
1104 */
1105
Paul Jackson3077a262005-08-09 10:07:59 -07001106static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 pid_t pid;
1109 struct task_struct *tsk;
1110 struct cpuset *oldcs;
1111 cpumask_t cpus;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001112 nodemask_t from, to;
Paul Jackson42253992006-01-08 01:01:59 -08001113 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Paul Jackson3077a262005-08-09 10:07:59 -07001115 if (sscanf(pidbuf, "%d", &pid) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return -EIO;
1117 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1118 return -ENOSPC;
1119
1120 if (pid) {
1121 read_lock(&tasklist_lock);
1122
1123 tsk = find_task_by_pid(pid);
Paul Jackson053199e2005-10-30 15:02:30 -08001124 if (!tsk || tsk->flags & PF_EXITING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 read_unlock(&tasklist_lock);
1126 return -ESRCH;
1127 }
1128
1129 get_task_struct(tsk);
1130 read_unlock(&tasklist_lock);
1131
1132 if ((current->euid) && (current->euid != tsk->uid)
1133 && (current->euid != tsk->suid)) {
1134 put_task_struct(tsk);
1135 return -EACCES;
1136 }
1137 } else {
1138 tsk = current;
1139 get_task_struct(tsk);
1140 }
1141
Paul Jackson053199e2005-10-30 15:02:30 -08001142 down(&callback_sem);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 task_lock(tsk);
1145 oldcs = tsk->cpuset;
1146 if (!oldcs) {
1147 task_unlock(tsk);
Paul Jackson053199e2005-10-30 15:02:30 -08001148 up(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 put_task_struct(tsk);
1150 return -ESRCH;
1151 }
1152 atomic_inc(&cs->count);
Paul Jackson6b9c2602006-01-08 01:02:02 -08001153 rcu_assign_pointer(tsk->cpuset, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 task_unlock(tsk);
1155
1156 guarantee_online_cpus(cs, &cpus);
1157 set_cpus_allowed(tsk, cpus);
1158
Paul Jackson45b07ef2006-01-08 01:00:56 -08001159 from = oldcs->mems_allowed;
1160 to = cs->mems_allowed;
1161
Paul Jackson053199e2005-10-30 15:02:30 -08001162 up(&callback_sem);
Paul Jackson42253992006-01-08 01:01:59 -08001163
1164 mm = get_task_mm(tsk);
1165 if (mm) {
1166 mpol_rebind_mm(mm, &to);
1167 mmput(mm);
1168 }
1169
Paul Jackson45b07ef2006-01-08 01:00:56 -08001170 if (is_memory_migrate(cs))
1171 do_migrate_pages(tsk->mm, &from, &to, MPOL_MF_MOVE_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 put_task_struct(tsk);
Paul Jackson6b9c2602006-01-08 01:02:02 -08001173 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (atomic_dec_and_test(&oldcs->count))
Paul Jackson3077a262005-08-09 10:07:59 -07001175 check_for_release(oldcs, ppathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177}
1178
1179/* The various types of files and directories in a cpuset file system */
1180
1181typedef enum {
1182 FILE_ROOT,
1183 FILE_DIR,
Paul Jackson45b07ef2006-01-08 01:00:56 -08001184 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 FILE_CPULIST,
1186 FILE_MEMLIST,
1187 FILE_CPU_EXCLUSIVE,
1188 FILE_MEM_EXCLUSIVE,
1189 FILE_NOTIFY_ON_RELEASE,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001190 FILE_MEMORY_PRESSURE_ENABLED,
1191 FILE_MEMORY_PRESSURE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 FILE_TASKLIST,
1193} cpuset_filetype_t;
1194
1195static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
1196 size_t nbytes, loff_t *unused_ppos)
1197{
1198 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1199 struct cftype *cft = __d_cft(file->f_dentry);
1200 cpuset_filetype_t type = cft->private;
1201 char *buffer;
Paul Jackson3077a262005-08-09 10:07:59 -07001202 char *pathbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int retval = 0;
1204
1205 /* Crude upper limit on largest legitimate cpulist user might write. */
1206 if (nbytes > 100 + 6 * NR_CPUS)
1207 return -E2BIG;
1208
1209 /* +1 for nul-terminator */
1210 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
1211 return -ENOMEM;
1212
1213 if (copy_from_user(buffer, userbuf, nbytes)) {
1214 retval = -EFAULT;
1215 goto out1;
1216 }
1217 buffer[nbytes] = 0; /* nul-terminate */
1218
Paul Jackson053199e2005-10-30 15:02:30 -08001219 down(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 if (is_removed(cs)) {
1222 retval = -ENODEV;
1223 goto out2;
1224 }
1225
1226 switch (type) {
1227 case FILE_CPULIST:
1228 retval = update_cpumask(cs, buffer);
1229 break;
1230 case FILE_MEMLIST:
1231 retval = update_nodemask(cs, buffer);
1232 break;
1233 case FILE_CPU_EXCLUSIVE:
1234 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
1235 break;
1236 case FILE_MEM_EXCLUSIVE:
1237 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
1238 break;
1239 case FILE_NOTIFY_ON_RELEASE:
1240 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
1241 break;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001242 case FILE_MEMORY_MIGRATE:
1243 retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
1244 break;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001245 case FILE_MEMORY_PRESSURE_ENABLED:
1246 retval = update_memory_pressure_enabled(cs, buffer);
1247 break;
1248 case FILE_MEMORY_PRESSURE:
1249 retval = -EACCES;
1250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 case FILE_TASKLIST:
Paul Jackson3077a262005-08-09 10:07:59 -07001252 retval = attach_task(cs, buffer, &pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 default:
1255 retval = -EINVAL;
1256 goto out2;
1257 }
1258
1259 if (retval == 0)
1260 retval = nbytes;
1261out2:
Paul Jackson053199e2005-10-30 15:02:30 -08001262 up(&manage_sem);
Paul Jackson3077a262005-08-09 10:07:59 -07001263 cpuset_release_agent(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264out1:
1265 kfree(buffer);
1266 return retval;
1267}
1268
1269static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
1270 size_t nbytes, loff_t *ppos)
1271{
1272 ssize_t retval = 0;
1273 struct cftype *cft = __d_cft(file->f_dentry);
1274 if (!cft)
1275 return -ENODEV;
1276
1277 /* special function ? */
1278 if (cft->write)
1279 retval = cft->write(file, buf, nbytes, ppos);
1280 else
1281 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
1282
1283 return retval;
1284}
1285
1286/*
1287 * These ascii lists should be read in a single call, by using a user
1288 * buffer large enough to hold the entire map. If read in smaller
1289 * chunks, there is no guarantee of atomicity. Since the display format
1290 * used, list of ranges of sequential numbers, is variable length,
1291 * and since these maps can change value dynamically, one could read
1292 * gibberish by doing partial reads while a list was changing.
1293 * A single large read to a buffer that crosses a page boundary is
1294 * ok, because the result being copied to user land is not recomputed
1295 * across a page fault.
1296 */
1297
1298static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1299{
1300 cpumask_t mask;
1301
Paul Jackson053199e2005-10-30 15:02:30 -08001302 down(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 mask = cs->cpus_allowed;
Paul Jackson053199e2005-10-30 15:02:30 -08001304 up(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 return cpulist_scnprintf(page, PAGE_SIZE, mask);
1307}
1308
1309static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1310{
1311 nodemask_t mask;
1312
Paul Jackson053199e2005-10-30 15:02:30 -08001313 down(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 mask = cs->mems_allowed;
Paul Jackson053199e2005-10-30 15:02:30 -08001315 up(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1318}
1319
1320static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
1321 size_t nbytes, loff_t *ppos)
1322{
1323 struct cftype *cft = __d_cft(file->f_dentry);
1324 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1325 cpuset_filetype_t type = cft->private;
1326 char *page;
1327 ssize_t retval = 0;
1328 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1331 return -ENOMEM;
1332
1333 s = page;
1334
1335 switch (type) {
1336 case FILE_CPULIST:
1337 s += cpuset_sprintf_cpulist(s, cs);
1338 break;
1339 case FILE_MEMLIST:
1340 s += cpuset_sprintf_memlist(s, cs);
1341 break;
1342 case FILE_CPU_EXCLUSIVE:
1343 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
1344 break;
1345 case FILE_MEM_EXCLUSIVE:
1346 *s++ = is_mem_exclusive(cs) ? '1' : '0';
1347 break;
1348 case FILE_NOTIFY_ON_RELEASE:
1349 *s++ = notify_on_release(cs) ? '1' : '0';
1350 break;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001351 case FILE_MEMORY_MIGRATE:
1352 *s++ = is_memory_migrate(cs) ? '1' : '0';
1353 break;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001354 case FILE_MEMORY_PRESSURE_ENABLED:
1355 *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
1356 break;
1357 case FILE_MEMORY_PRESSURE:
1358 s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
1359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 default:
1361 retval = -EINVAL;
1362 goto out;
1363 }
1364 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Al Viroeacaa1f2005-09-30 03:26:43 +01001366 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367out:
1368 free_page((unsigned long)page);
1369 return retval;
1370}
1371
1372static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1373 loff_t *ppos)
1374{
1375 ssize_t retval = 0;
1376 struct cftype *cft = __d_cft(file->f_dentry);
1377 if (!cft)
1378 return -ENODEV;
1379
1380 /* special function ? */
1381 if (cft->read)
1382 retval = cft->read(file, buf, nbytes, ppos);
1383 else
1384 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1385
1386 return retval;
1387}
1388
1389static int cpuset_file_open(struct inode *inode, struct file *file)
1390{
1391 int err;
1392 struct cftype *cft;
1393
1394 err = generic_file_open(inode, file);
1395 if (err)
1396 return err;
1397
1398 cft = __d_cft(file->f_dentry);
1399 if (!cft)
1400 return -ENODEV;
1401 if (cft->open)
1402 err = cft->open(inode, file);
1403 else
1404 err = 0;
1405
1406 return err;
1407}
1408
1409static int cpuset_file_release(struct inode *inode, struct file *file)
1410{
1411 struct cftype *cft = __d_cft(file->f_dentry);
1412 if (cft->release)
1413 return cft->release(inode, file);
1414 return 0;
1415}
1416
Paul Jackson18a19cb2005-10-30 15:02:31 -08001417/*
1418 * cpuset_rename - Only allow simple rename of directories in place.
1419 */
1420static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
1421 struct inode *new_dir, struct dentry *new_dentry)
1422{
1423 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1424 return -ENOTDIR;
1425 if (new_dentry->d_inode)
1426 return -EEXIST;
1427 if (old_dir != new_dir)
1428 return -EIO;
1429 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static struct file_operations cpuset_file_operations = {
1433 .read = cpuset_file_read,
1434 .write = cpuset_file_write,
1435 .llseek = generic_file_llseek,
1436 .open = cpuset_file_open,
1437 .release = cpuset_file_release,
1438};
1439
1440static struct inode_operations cpuset_dir_inode_operations = {
1441 .lookup = simple_lookup,
1442 .mkdir = cpuset_mkdir,
1443 .rmdir = cpuset_rmdir,
Paul Jackson18a19cb2005-10-30 15:02:31 -08001444 .rename = cpuset_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445};
1446
1447static int cpuset_create_file(struct dentry *dentry, int mode)
1448{
1449 struct inode *inode;
1450
1451 if (!dentry)
1452 return -ENOENT;
1453 if (dentry->d_inode)
1454 return -EEXIST;
1455
1456 inode = cpuset_new_inode(mode);
1457 if (!inode)
1458 return -ENOMEM;
1459
1460 if (S_ISDIR(mode)) {
1461 inode->i_op = &cpuset_dir_inode_operations;
1462 inode->i_fop = &simple_dir_operations;
1463
1464 /* start off with i_nlink == 2 (for "." entry) */
1465 inode->i_nlink++;
1466 } else if (S_ISREG(mode)) {
1467 inode->i_size = 0;
1468 inode->i_fop = &cpuset_file_operations;
1469 }
1470
1471 d_instantiate(dentry, inode);
1472 dget(dentry); /* Extra count - pin the dentry in core */
1473 return 0;
1474}
1475
1476/*
1477 * cpuset_create_dir - create a directory for an object.
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08001478 * cs: the cpuset we create the directory for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * It must have a valid ->parent field
1480 * And we are going to fill its ->dentry field.
1481 * name: The name to give to the cpuset directory. Will be copied.
1482 * mode: mode to set on new directory.
1483 */
1484
1485static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1486{
1487 struct dentry *dentry = NULL;
1488 struct dentry *parent;
1489 int error = 0;
1490
1491 parent = cs->parent->dentry;
1492 dentry = cpuset_get_dentry(parent, name);
1493 if (IS_ERR(dentry))
1494 return PTR_ERR(dentry);
1495 error = cpuset_create_file(dentry, S_IFDIR | mode);
1496 if (!error) {
1497 dentry->d_fsdata = cs;
1498 parent->d_inode->i_nlink++;
1499 cs->dentry = dentry;
1500 }
1501 dput(dentry);
1502
1503 return error;
1504}
1505
1506static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1507{
1508 struct dentry *dentry;
1509 int error;
1510
1511 down(&dir->d_inode->i_sem);
1512 dentry = cpuset_get_dentry(dir, cft->name);
1513 if (!IS_ERR(dentry)) {
1514 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1515 if (!error)
1516 dentry->d_fsdata = (void *)cft;
1517 dput(dentry);
1518 } else
1519 error = PTR_ERR(dentry);
1520 up(&dir->d_inode->i_sem);
1521 return error;
1522}
1523
1524/*
1525 * Stuff for reading the 'tasks' file.
1526 *
1527 * Reading this file can return large amounts of data if a cpuset has
1528 * *lots* of attached tasks. So it may need several calls to read(),
1529 * but we cannot guarantee that the information we produce is correct
1530 * unless we produce it entirely atomically.
1531 *
1532 * Upon tasks file open(), a struct ctr_struct is allocated, that
1533 * will have a pointer to an array (also allocated here). The struct
1534 * ctr_struct * is stored in file->private_data. Its resources will
1535 * be freed by release() when the file is closed. The array is used
1536 * to sprintf the PIDs and then used by read().
1537 */
1538
1539/* cpusets_tasks_read array */
1540
1541struct ctr_struct {
1542 char *buf;
1543 int bufsz;
1544};
1545
1546/*
1547 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
Paul Jackson053199e2005-10-30 15:02:30 -08001548 * Return actual number of pids loaded. No need to task_lock(p)
1549 * when reading out p->cpuset, as we don't really care if it changes
1550 * on the next cycle, and we are not going to try to dereference it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
1552static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1553{
1554 int n = 0;
1555 struct task_struct *g, *p;
1556
1557 read_lock(&tasklist_lock);
1558
1559 do_each_thread(g, p) {
1560 if (p->cpuset == cs) {
1561 pidarray[n++] = p->pid;
1562 if (unlikely(n == npids))
1563 goto array_full;
1564 }
1565 } while_each_thread(g, p);
1566
1567array_full:
1568 read_unlock(&tasklist_lock);
1569 return n;
1570}
1571
1572static int cmppid(const void *a, const void *b)
1573{
1574 return *(pid_t *)a - *(pid_t *)b;
1575}
1576
1577/*
1578 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1579 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1580 * count 'cnt' of how many chars would be written if buf were large enough.
1581 */
1582static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1583{
1584 int cnt = 0;
1585 int i;
1586
1587 for (i = 0; i < npids; i++)
1588 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1589 return cnt;
1590}
1591
Paul Jackson053199e2005-10-30 15:02:30 -08001592/*
1593 * Handle an open on 'tasks' file. Prepare a buffer listing the
1594 * process id's of tasks currently attached to the cpuset being opened.
1595 *
1596 * Does not require any specific cpuset semaphores, and does not take any.
1597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598static int cpuset_tasks_open(struct inode *unused, struct file *file)
1599{
1600 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1601 struct ctr_struct *ctr;
1602 pid_t *pidarray;
1603 int npids;
1604 char c;
1605
1606 if (!(file->f_mode & FMODE_READ))
1607 return 0;
1608
1609 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1610 if (!ctr)
1611 goto err0;
1612
1613 /*
1614 * If cpuset gets more users after we read count, we won't have
1615 * enough space - tough. This race is indistinguishable to the
1616 * caller from the case that the additional cpuset users didn't
1617 * show up until sometime later on.
1618 */
1619 npids = atomic_read(&cs->count);
1620 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1621 if (!pidarray)
1622 goto err1;
1623
1624 npids = pid_array_load(pidarray, npids, cs);
1625 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1626
1627 /* Call pid_array_to_buf() twice, first just to get bufsz */
1628 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1629 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1630 if (!ctr->buf)
1631 goto err2;
1632 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1633
1634 kfree(pidarray);
1635 file->private_data = ctr;
1636 return 0;
1637
1638err2:
1639 kfree(pidarray);
1640err1:
1641 kfree(ctr);
1642err0:
1643 return -ENOMEM;
1644}
1645
1646static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1647 size_t nbytes, loff_t *ppos)
1648{
1649 struct ctr_struct *ctr = file->private_data;
1650
1651 if (*ppos + nbytes > ctr->bufsz)
1652 nbytes = ctr->bufsz - *ppos;
1653 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1654 return -EFAULT;
1655 *ppos += nbytes;
1656 return nbytes;
1657}
1658
1659static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1660{
1661 struct ctr_struct *ctr;
1662
1663 if (file->f_mode & FMODE_READ) {
1664 ctr = file->private_data;
1665 kfree(ctr->buf);
1666 kfree(ctr);
1667 }
1668 return 0;
1669}
1670
1671/*
1672 * for the common functions, 'private' gives the type of file
1673 */
1674
1675static struct cftype cft_tasks = {
1676 .name = "tasks",
1677 .open = cpuset_tasks_open,
1678 .read = cpuset_tasks_read,
1679 .release = cpuset_tasks_release,
1680 .private = FILE_TASKLIST,
1681};
1682
1683static struct cftype cft_cpus = {
1684 .name = "cpus",
1685 .private = FILE_CPULIST,
1686};
1687
1688static struct cftype cft_mems = {
1689 .name = "mems",
1690 .private = FILE_MEMLIST,
1691};
1692
1693static struct cftype cft_cpu_exclusive = {
1694 .name = "cpu_exclusive",
1695 .private = FILE_CPU_EXCLUSIVE,
1696};
1697
1698static struct cftype cft_mem_exclusive = {
1699 .name = "mem_exclusive",
1700 .private = FILE_MEM_EXCLUSIVE,
1701};
1702
1703static struct cftype cft_notify_on_release = {
1704 .name = "notify_on_release",
1705 .private = FILE_NOTIFY_ON_RELEASE,
1706};
1707
Paul Jackson45b07ef2006-01-08 01:00:56 -08001708static struct cftype cft_memory_migrate = {
1709 .name = "memory_migrate",
1710 .private = FILE_MEMORY_MIGRATE,
1711};
1712
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001713static struct cftype cft_memory_pressure_enabled = {
1714 .name = "memory_pressure_enabled",
1715 .private = FILE_MEMORY_PRESSURE_ENABLED,
1716};
1717
1718static struct cftype cft_memory_pressure = {
1719 .name = "memory_pressure",
1720 .private = FILE_MEMORY_PRESSURE,
1721};
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723static int cpuset_populate_dir(struct dentry *cs_dentry)
1724{
1725 int err;
1726
1727 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1728 return err;
1729 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1730 return err;
1731 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1732 return err;
1733 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1734 return err;
1735 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1736 return err;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001737 if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
1738 return err;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001739 if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
1740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1742 return err;
1743 return 0;
1744}
1745
1746/*
1747 * cpuset_create - create a cpuset
1748 * parent: cpuset that will be parent of the new cpuset.
1749 * name: name of the new cpuset. Will be strcpy'ed.
1750 * mode: mode to set on new inode
1751 *
1752 * Must be called with the semaphore on the parent inode held
1753 */
1754
1755static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1756{
1757 struct cpuset *cs;
1758 int err;
1759
1760 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1761 if (!cs)
1762 return -ENOMEM;
1763
Paul Jackson053199e2005-10-30 15:02:30 -08001764 down(&manage_sem);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001765 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 cs->flags = 0;
1767 if (notify_on_release(parent))
1768 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
1769 cs->cpus_allowed = CPU_MASK_NONE;
1770 cs->mems_allowed = NODE_MASK_NONE;
1771 atomic_set(&cs->count, 0);
1772 INIT_LIST_HEAD(&cs->sibling);
1773 INIT_LIST_HEAD(&cs->children);
1774 atomic_inc(&cpuset_mems_generation);
1775 cs->mems_generation = atomic_read(&cpuset_mems_generation);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001776 fmeter_init(&cs->fmeter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 cs->parent = parent;
1779
Paul Jackson053199e2005-10-30 15:02:30 -08001780 down(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 list_add(&cs->sibling, &cs->parent->children);
Paul Jackson202f72d2006-01-08 01:01:57 -08001782 number_of_cpusets++;
Paul Jackson053199e2005-10-30 15:02:30 -08001783 up(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 err = cpuset_create_dir(cs, name, mode);
1786 if (err < 0)
1787 goto err;
1788
1789 /*
Paul Jackson053199e2005-10-30 15:02:30 -08001790 * Release manage_sem before cpuset_populate_dir() because it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * will down() this new directory's i_sem and if we race with
1792 * another mkdir, we might deadlock.
1793 */
Paul Jackson053199e2005-10-30 15:02:30 -08001794 up(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 err = cpuset_populate_dir(cs->dentry);
1797 /* If err < 0, we have a half-filled directory - oh well ;) */
1798 return 0;
1799err:
1800 list_del(&cs->sibling);
Paul Jackson053199e2005-10-30 15:02:30 -08001801 up(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 kfree(cs);
1803 return err;
1804}
1805
1806static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1807{
1808 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1809
1810 /* the vfs holds inode->i_sem already */
1811 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1812}
1813
1814static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1815{
1816 struct cpuset *cs = dentry->d_fsdata;
1817 struct dentry *d;
1818 struct cpuset *parent;
Paul Jackson3077a262005-08-09 10:07:59 -07001819 char *pathbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /* the vfs holds both inode->i_sem already */
1822
Paul Jackson053199e2005-10-30 15:02:30 -08001823 down(&manage_sem);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001824 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (atomic_read(&cs->count) > 0) {
Paul Jackson053199e2005-10-30 15:02:30 -08001826 up(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -EBUSY;
1828 }
1829 if (!list_empty(&cs->children)) {
Paul Jackson053199e2005-10-30 15:02:30 -08001830 up(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -EBUSY;
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 parent = cs->parent;
Paul Jackson053199e2005-10-30 15:02:30 -08001834 down(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 set_bit(CS_REMOVED, &cs->flags);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001836 if (is_cpu_exclusive(cs))
1837 update_cpu_domains(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 list_del(&cs->sibling); /* delete my sibling from parent->children */
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001839 spin_lock(&cs->dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 d = dget(cs->dentry);
1841 cs->dentry = NULL;
1842 spin_unlock(&d->d_lock);
1843 cpuset_d_remove_dir(d);
1844 dput(d);
Paul Jackson202f72d2006-01-08 01:01:57 -08001845 number_of_cpusets--;
Paul Jackson053199e2005-10-30 15:02:30 -08001846 up(&callback_sem);
1847 if (list_empty(&parent->children))
1848 check_for_release(parent, &pathbuf);
1849 up(&manage_sem);
Paul Jackson3077a262005-08-09 10:07:59 -07001850 cpuset_release_agent(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return 0;
1852}
1853
Paul Jacksonc417f022006-01-08 01:02:01 -08001854/*
1855 * cpuset_init_early - just enough so that the calls to
1856 * cpuset_update_task_memory_state() in early init code
1857 * are harmless.
1858 */
1859
1860int __init cpuset_init_early(void)
1861{
1862 struct task_struct *tsk = current;
1863
1864 tsk->cpuset = &top_cpuset;
1865 tsk->cpuset->mems_generation = atomic_read(&cpuset_mems_generation);
1866 return 0;
1867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/**
1870 * cpuset_init - initialize cpusets at system boot
1871 *
1872 * Description: Initialize top_cpuset and the cpuset internal file system,
1873 **/
1874
1875int __init cpuset_init(void)
1876{
1877 struct dentry *root;
1878 int err;
1879
1880 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1881 top_cpuset.mems_allowed = NODE_MASK_ALL;
1882
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001883 fmeter_init(&top_cpuset.fmeter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 atomic_inc(&cpuset_mems_generation);
1885 top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
1886
1887 init_task.cpuset = &top_cpuset;
1888
1889 err = register_filesystem(&cpuset_fs_type);
1890 if (err < 0)
1891 goto out;
1892 cpuset_mount = kern_mount(&cpuset_fs_type);
1893 if (IS_ERR(cpuset_mount)) {
1894 printk(KERN_ERR "cpuset: could not mount!\n");
1895 err = PTR_ERR(cpuset_mount);
1896 cpuset_mount = NULL;
1897 goto out;
1898 }
1899 root = cpuset_mount->mnt_sb->s_root;
1900 root->d_fsdata = &top_cpuset;
1901 root->d_inode->i_nlink++;
1902 top_cpuset.dentry = root;
1903 root->d_inode->i_op = &cpuset_dir_inode_operations;
Paul Jackson202f72d2006-01-08 01:01:57 -08001904 number_of_cpusets = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 err = cpuset_populate_dir(root);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001906 /* memory_pressure_enabled is in root cpuset only */
1907 if (err == 0)
1908 err = cpuset_add_file(root, &cft_memory_pressure_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909out:
1910 return err;
1911}
1912
1913/**
1914 * cpuset_init_smp - initialize cpus_allowed
1915 *
1916 * Description: Finish top cpuset after cpu, node maps are initialized
1917 **/
1918
1919void __init cpuset_init_smp(void)
1920{
1921 top_cpuset.cpus_allowed = cpu_online_map;
1922 top_cpuset.mems_allowed = node_online_map;
1923}
1924
1925/**
1926 * cpuset_fork - attach newly forked task to its parents cpuset.
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07001927 * @tsk: pointer to task_struct of forking parent process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 *
Paul Jackson053199e2005-10-30 15:02:30 -08001929 * Description: A task inherits its parent's cpuset at fork().
1930 *
1931 * A pointer to the shared cpuset was automatically copied in fork.c
1932 * by dup_task_struct(). However, we ignore that copy, since it was
1933 * not made under the protection of task_lock(), so might no longer be
1934 * a valid cpuset pointer. attach_task() might have already changed
1935 * current->cpuset, allowing the previously referenced cpuset to
1936 * be removed and freed. Instead, we task_lock(current) and copy
1937 * its present value of current->cpuset for our freshly forked child.
1938 *
1939 * At the point that cpuset_fork() is called, 'current' is the parent
1940 * task, and the passed argument 'child' points to the child task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 **/
1942
Paul Jackson053199e2005-10-30 15:02:30 -08001943void cpuset_fork(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Paul Jackson053199e2005-10-30 15:02:30 -08001945 task_lock(current);
1946 child->cpuset = current->cpuset;
1947 atomic_inc(&child->cpuset->count);
1948 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951/**
1952 * cpuset_exit - detach cpuset from exiting task
1953 * @tsk: pointer to task_struct of exiting process
1954 *
1955 * Description: Detach cpuset from @tsk and release it.
1956 *
Paul Jackson053199e2005-10-30 15:02:30 -08001957 * Note that cpusets marked notify_on_release force every task in
1958 * them to take the global manage_sem semaphore when exiting.
1959 * This could impact scaling on very large systems. Be reluctant to
1960 * use notify_on_release cpusets where very high task exit scaling
1961 * is required on large systems.
Paul Jackson2efe86b2005-05-27 02:02:43 -07001962 *
Paul Jackson053199e2005-10-30 15:02:30 -08001963 * Don't even think about derefencing 'cs' after the cpuset use count
1964 * goes to zero, except inside a critical section guarded by manage_sem
1965 * or callback_sem. Otherwise a zero cpuset use count is a license to
1966 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
1967 *
1968 * This routine has to take manage_sem, not callback_sem, because
1969 * it is holding that semaphore while calling check_for_release(),
1970 * which calls kmalloc(), so can't be called holding callback__sem().
1971 *
1972 * We don't need to task_lock() this reference to tsk->cpuset,
1973 * because tsk is already marked PF_EXITING, so attach_task() won't
Paul Jacksonb4b26412006-01-08 01:01:53 -08001974 * mess with it, or task is a failed fork, never visible to attach_task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 **/
1976
1977void cpuset_exit(struct task_struct *tsk)
1978{
1979 struct cpuset *cs;
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 cs = tsk->cpuset;
1982 tsk->cpuset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Paul Jackson2efe86b2005-05-27 02:02:43 -07001984 if (notify_on_release(cs)) {
Paul Jackson3077a262005-08-09 10:07:59 -07001985 char *pathbuf = NULL;
1986
Paul Jackson053199e2005-10-30 15:02:30 -08001987 down(&manage_sem);
Paul Jackson2efe86b2005-05-27 02:02:43 -07001988 if (atomic_dec_and_test(&cs->count))
Paul Jackson3077a262005-08-09 10:07:59 -07001989 check_for_release(cs, &pathbuf);
Paul Jackson053199e2005-10-30 15:02:30 -08001990 up(&manage_sem);
Paul Jackson3077a262005-08-09 10:07:59 -07001991 cpuset_release_agent(pathbuf);
Paul Jackson2efe86b2005-05-27 02:02:43 -07001992 } else {
1993 atomic_dec(&cs->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995}
1996
1997/**
1998 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1999 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2000 *
2001 * Description: Returns the cpumask_t cpus_allowed of the cpuset
2002 * attached to the specified @tsk. Guaranteed to return some non-empty
2003 * subset of cpu_online_map, even if this means going outside the
2004 * tasks cpuset.
2005 **/
2006
Paul Jackson909d75a2006-01-08 01:01:55 -08002007cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 cpumask_t mask;
2010
Paul Jackson053199e2005-10-30 15:02:30 -08002011 down(&callback_sem);
Paul Jackson909d75a2006-01-08 01:01:55 -08002012 task_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 guarantee_online_cpus(tsk->cpuset, &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002014 task_unlock(tsk);
Paul Jackson053199e2005-10-30 15:02:30 -08002015 up(&callback_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 return mask;
2018}
2019
2020void cpuset_init_current_mems_allowed(void)
2021{
2022 current->mems_allowed = NODE_MASK_ALL;
2023}
2024
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002025/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002026 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2027 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2028 *
2029 * Description: Returns the nodemask_t mems_allowed of the cpuset
2030 * attached to the specified @tsk. Guaranteed to return some non-empty
2031 * subset of node_online_map, even if this means going outside the
2032 * tasks cpuset.
2033 **/
2034
2035nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2036{
2037 nodemask_t mask;
2038
2039 down(&callback_sem);
2040 task_lock(tsk);
2041 guarantee_online_mems(tsk->cpuset, &mask);
2042 task_unlock(tsk);
2043 up(&callback_sem);
2044
2045 return mask;
2046}
2047
2048/**
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002049 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2050 * @zl: the zonelist to be checked
2051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2053 */
2054int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
2055{
2056 int i;
2057
2058 for (i = 0; zl->zones[i]; i++) {
2059 int nid = zl->zones[i]->zone_pgdat->node_id;
2060
2061 if (node_isset(nid, current->mems_allowed))
2062 return 1;
2063 }
2064 return 0;
2065}
2066
Paul Jackson9bf22292005-09-06 15:18:12 -07002067/*
2068 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
Paul Jackson053199e2005-10-30 15:02:30 -08002069 * ancestor to the specified cpuset. Call holding callback_sem.
Paul Jackson9bf22292005-09-06 15:18:12 -07002070 * If no ancestor is mem_exclusive (an unusual configuration), then
2071 * returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 */
Paul Jackson9bf22292005-09-06 15:18:12 -07002073static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Paul Jackson9bf22292005-09-06 15:18:12 -07002075 while (!is_mem_exclusive(cs) && cs->parent)
2076 cs = cs->parent;
2077 return cs;
2078}
2079
2080/**
2081 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
2082 * @z: is this zone on an allowed node?
2083 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
2084 *
2085 * If we're in interrupt, yes, we can always allocate. If zone
2086 * z's node is in our tasks mems_allowed, yes. If it's not a
2087 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2088 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
2089 * Otherwise, no.
2090 *
2091 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2092 * and do not allow allocations outside the current tasks cpuset.
2093 * GFP_KERNEL allocations are not so marked, so can escape to the
2094 * nearest mem_exclusive ancestor cpuset.
2095 *
Paul Jackson053199e2005-10-30 15:02:30 -08002096 * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
Paul Jackson9bf22292005-09-06 15:18:12 -07002097 * routine only calls here with __GFP_HARDWALL bit _not_ set if
2098 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
2099 * mems_allowed came up empty on the first pass over the zonelist.
2100 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
Paul Jackson053199e2005-10-30 15:02:30 -08002101 * short of memory, might require taking the callback_sem semaphore.
Paul Jackson9bf22292005-09-06 15:18:12 -07002102 *
2103 * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
2104 * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
2105 * hardwall cpusets - no allocation on a node outside the cpuset is
2106 * allowed (unless in interrupt, of course).
2107 *
2108 * The second loop doesn't even call here for GFP_ATOMIC requests
2109 * (if the __alloc_pages() local variable 'wait' is set). That check
2110 * and the checks below have the combined affect in the second loop of
2111 * the __alloc_pages() routine that:
2112 * in_interrupt - any node ok (current task context irrelevant)
2113 * GFP_ATOMIC - any node ok
2114 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2115 * GFP_USER - only nodes in current tasks mems allowed ok.
2116 **/
2117
Paul Jackson202f72d2006-01-08 01:01:57 -08002118int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002119{
2120 int node; /* node that zone z is on */
2121 const struct cpuset *cs; /* current cpuset ancestors */
2122 int allowed = 1; /* is allocation in zone z allowed? */
2123
2124 if (in_interrupt())
2125 return 1;
2126 node = z->zone_pgdat->node_id;
2127 if (node_isset(node, current->mems_allowed))
2128 return 1;
2129 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2130 return 0;
2131
Bob Picco5563e772005-11-13 16:06:35 -08002132 if (current->flags & PF_EXITING) /* Let dying task have memory */
2133 return 1;
2134
Paul Jackson9bf22292005-09-06 15:18:12 -07002135 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Paul Jackson053199e2005-10-30 15:02:30 -08002136 down(&callback_sem);
2137
Paul Jackson053199e2005-10-30 15:02:30 -08002138 task_lock(current);
2139 cs = nearest_exclusive_ancestor(current->cpuset);
2140 task_unlock(current);
2141
Paul Jackson9bf22292005-09-06 15:18:12 -07002142 allowed = node_isset(node, cs->mems_allowed);
Paul Jackson053199e2005-10-30 15:02:30 -08002143 up(&callback_sem);
Paul Jackson9bf22292005-09-06 15:18:12 -07002144 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002147/**
2148 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2149 * @p: pointer to task_struct of some other task.
2150 *
2151 * Description: Return true if the nearest mem_exclusive ancestor
2152 * cpusets of tasks @p and current overlap. Used by oom killer to
2153 * determine if task @p's memory usage might impact the memory
2154 * available to the current task.
2155 *
Paul Jackson053199e2005-10-30 15:02:30 -08002156 * Acquires callback_sem - not suitable for calling from a fast path.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002157 **/
2158
2159int cpuset_excl_nodes_overlap(const struct task_struct *p)
2160{
2161 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
2162 int overlap = 0; /* do cpusets overlap? */
2163
Paul Jackson053199e2005-10-30 15:02:30 -08002164 down(&callback_sem);
2165
2166 task_lock(current);
2167 if (current->flags & PF_EXITING) {
2168 task_unlock(current);
2169 goto done;
2170 }
2171 cs1 = nearest_exclusive_ancestor(current->cpuset);
2172 task_unlock(current);
2173
2174 task_lock((struct task_struct *)p);
2175 if (p->flags & PF_EXITING) {
2176 task_unlock((struct task_struct *)p);
2177 goto done;
2178 }
2179 cs2 = nearest_exclusive_ancestor(p->cpuset);
2180 task_unlock((struct task_struct *)p);
2181
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002182 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
2183done:
Paul Jackson053199e2005-10-30 15:02:30 -08002184 up(&callback_sem);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002185
2186 return overlap;
2187}
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002190 * Collection of memory_pressure is suppressed unless
2191 * this flag is enabled by writing "1" to the special
2192 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2193 */
2194
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002195int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002196
2197/**
2198 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2199 *
2200 * Keep a running average of the rate of synchronous (direct)
2201 * page reclaim efforts initiated by tasks in each cpuset.
2202 *
2203 * This represents the rate at which some task in the cpuset
2204 * ran low on memory on all nodes it was allowed to use, and
2205 * had to enter the kernels page reclaim code in an effort to
2206 * create more free memory by tossing clean pages or swapping
2207 * or writing dirty pages.
2208 *
2209 * Display to user space in the per-cpuset read-only file
2210 * "memory_pressure". Value displayed is an integer
2211 * representing the recent rate of entry into the synchronous
2212 * (direct) page reclaim by any task attached to the cpuset.
2213 **/
2214
2215void __cpuset_memory_pressure_bump(void)
2216{
2217 struct cpuset *cs;
2218
2219 task_lock(current);
2220 cs = current->cpuset;
2221 fmeter_markevent(&cs->fmeter);
2222 task_unlock(current);
2223}
2224
2225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 * proc_cpuset_show()
2227 * - Print tasks cpuset path into seq_file.
2228 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002229 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2230 * doesn't really matter if tsk->cpuset changes after we read it,
2231 * and we take manage_sem, keeping attach_task() from changing it
2232 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 */
2234
2235static int proc_cpuset_show(struct seq_file *m, void *v)
2236{
2237 struct cpuset *cs;
2238 struct task_struct *tsk;
2239 char *buf;
2240 int retval = 0;
2241
2242 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2243 if (!buf)
2244 return -ENOMEM;
2245
2246 tsk = m->private;
Paul Jackson053199e2005-10-30 15:02:30 -08002247 down(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 cs = tsk->cpuset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (!cs) {
2250 retval = -EINVAL;
2251 goto out;
2252 }
2253
2254 retval = cpuset_path(cs, buf, PAGE_SIZE);
2255 if (retval < 0)
2256 goto out;
2257 seq_puts(m, buf);
2258 seq_putc(m, '\n');
2259out:
Paul Jackson053199e2005-10-30 15:02:30 -08002260 up(&manage_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 kfree(buf);
2262 return retval;
2263}
2264
2265static int cpuset_open(struct inode *inode, struct file *file)
2266{
2267 struct task_struct *tsk = PROC_I(inode)->task;
2268 return single_open(file, proc_cpuset_show, tsk);
2269}
2270
2271struct file_operations proc_cpuset_operations = {
2272 .open = cpuset_open,
2273 .read = seq_read,
2274 .llseek = seq_lseek,
2275 .release = single_release,
2276};
2277
2278/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2279char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
2280{
2281 buffer += sprintf(buffer, "Cpus_allowed:\t");
2282 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
2283 buffer += sprintf(buffer, "\n");
2284 buffer += sprintf(buffer, "Mems_allowed:\t");
2285 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
2286 buffer += sprintf(buffer, "\n");
2287 return buffer;
2288}