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