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