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