4 * Processor and Memory placement constraints for sets of tasks.
6 * Copyright (C) 2003 BULL SA.
7 * Copyright (C) 2004 Silicon Graphics, Inc.
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.
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>
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.
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>
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>
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>
52 #include <asm/uaccess.h>
53 #include <asm/atomic.h>
54 #include <asm/semaphore.h>
56 #define CPUSET_SUPER_MAGIC 0x27e0eb
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 */
63 atomic_t count; /* count tasks using this cpuset */
66 * We link our 'sibling' struct into our parents 'children'.
67 * Our children link their 'sibling' into our 'children'.
69 struct list_head sibling; /* my parents children */
70 struct list_head children; /* my children */
72 struct cpuset *parent; /* my parent */
73 struct dentry *dentry; /* cpuset fs entry */
76 * Copy of global cpuset_mems_generation as of the most
77 * recent time this cpuset changed its mems_allowed.
82 /* bits in struct cpuset flags field */
90 /* convenient tests for these bits */
91 static inline int is_cpu_exclusive(const struct cpuset *cs)
93 return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
96 static inline int is_mem_exclusive(const struct cpuset *cs)
98 return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
101 static inline int is_removed(const struct cpuset *cs)
103 return !!test_bit(CS_REMOVED, &cs->flags);
106 static inline int notify_on_release(const struct cpuset *cs)
108 return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
112 * Increment this atomic integer everytime any cpuset changes its
113 * mems_allowed value. Users of cpusets can track this generation
114 * number, and avoid having to lock and reload mems_allowed unless
115 * the cpuset they're using changes generation.
117 * A single, global generation is needed because attach_task() could
118 * reattach a task to a different cpuset, which must not have its
119 * generation numbers aliased with those of that tasks previous cpuset.
121 * Generations are needed for mems_allowed because one task cannot
122 * modify anothers memory placement. So we must enable every task,
123 * on every visit to __alloc_pages(), to efficiently check whether
124 * its current->cpuset->mems_allowed has changed, requiring an update
125 * of its current->mems_allowed.
127 static atomic_t cpuset_mems_generation = ATOMIC_INIT(1);
129 static struct cpuset top_cpuset = {
130 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
131 .cpus_allowed = CPU_MASK_ALL,
132 .mems_allowed = NODE_MASK_ALL,
133 .count = ATOMIC_INIT(0),
134 .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
135 .children = LIST_HEAD_INIT(top_cpuset.children),
138 .mems_generation = 0,
141 static struct vfsmount *cpuset_mount;
142 static struct super_block *cpuset_sb = NULL;
145 * cpuset_sem should be held by anyone who is depending on the children
146 * or sibling lists of any cpuset, or performing non-atomic operations
147 * on the flags or *_allowed values of a cpuset, such as raising the
148 * CS_REMOVED flag bit iff it is not already raised, or reading and
149 * conditionally modifying the *_allowed values. One kernel global
150 * cpuset semaphore should be sufficient - these things don't change
153 * The code that modifies cpusets holds cpuset_sem across the entire
154 * operation, from cpuset_common_file_write() down, single threading
155 * all cpuset modifications (except for counter manipulations from
156 * fork and exit) across the system. This presumes that cpuset
157 * modifications are rare - better kept simple and safe, even if slow.
159 * The code that reads cpusets, such as in cpuset_common_file_read()
160 * and below, only holds cpuset_sem across small pieces of code, such
161 * as when reading out possibly multi-word cpumasks and nodemasks, as
162 * the risks are less, and the desire for performance a little greater.
163 * The proc_cpuset_show() routine needs to hold cpuset_sem to insure
164 * that no cs->dentry is NULL, as it walks up the cpuset tree to root.
166 * The hooks from fork and exit, cpuset_fork() and cpuset_exit(), don't
167 * (usually) grab cpuset_sem. These are the two most performance
168 * critical pieces of code here. The exception occurs on exit(),
169 * when a task in a notify_on_release cpuset exits. Then cpuset_sem
170 * is taken, and if the cpuset count is zero, a usermode call made
171 * to /sbin/cpuset_release_agent with the name of the cpuset (path
172 * relative to the root of cpuset file system) as the argument.
174 * A cpuset can only be deleted if both its 'count' of using tasks is
175 * zero, and its list of 'children' cpusets is empty. Since all tasks
176 * in the system use _some_ cpuset, and since there is always at least
177 * one task in the system (init, pid == 1), therefore, top_cpuset
178 * always has either children cpusets and/or using tasks. So no need
179 * for any special hack to ensure that top_cpuset cannot be deleted.
182 static DECLARE_MUTEX(cpuset_sem);
185 * A couple of forward declarations required, due to cyclic reference loop:
186 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
187 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
190 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
191 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
193 static struct backing_dev_info cpuset_backing_dev_info = {
194 .ra_pages = 0, /* No readahead */
195 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
198 static struct inode *cpuset_new_inode(mode_t mode)
200 struct inode *inode = new_inode(cpuset_sb);
203 inode->i_mode = mode;
204 inode->i_uid = current->fsuid;
205 inode->i_gid = current->fsgid;
206 inode->i_blksize = PAGE_CACHE_SIZE;
208 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
209 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
214 static void cpuset_diput(struct dentry *dentry, struct inode *inode)
216 /* is dentry a directory ? if so, kfree() associated cpuset */
217 if (S_ISDIR(inode->i_mode)) {
218 struct cpuset *cs = dentry->d_fsdata;
219 BUG_ON(!(is_removed(cs)));
225 static struct dentry_operations cpuset_dops = {
226 .d_iput = cpuset_diput,
229 static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
231 struct dentry *d = lookup_one_len(name, parent, strlen(name));
233 d->d_op = &cpuset_dops;
237 static void remove_dir(struct dentry *d)
239 struct dentry *parent = dget(d->d_parent);
242 simple_rmdir(parent->d_inode, d);
247 * NOTE : the dentry must have been dget()'ed
249 static void cpuset_d_remove_dir(struct dentry *dentry)
251 struct list_head *node;
253 spin_lock(&dcache_lock);
254 node = dentry->d_subdirs.next;
255 while (node != &dentry->d_subdirs) {
256 struct dentry *d = list_entry(node, struct dentry, d_child);
260 spin_unlock(&dcache_lock);
262 simple_unlink(dentry->d_inode, d);
264 spin_lock(&dcache_lock);
266 node = dentry->d_subdirs.next;
268 list_del_init(&dentry->d_child);
269 spin_unlock(&dcache_lock);
273 static struct super_operations cpuset_ops = {
274 .statfs = simple_statfs,
275 .drop_inode = generic_delete_inode,
278 static int cpuset_fill_super(struct super_block *sb, void *unused_data,
284 sb->s_blocksize = PAGE_CACHE_SIZE;
285 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
286 sb->s_magic = CPUSET_SUPER_MAGIC;
287 sb->s_op = &cpuset_ops;
290 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
292 inode->i_op = &simple_dir_inode_operations;
293 inode->i_fop = &simple_dir_operations;
294 /* directories start off with i_nlink == 2 (for "." entry) */
300 root = d_alloc_root(inode);
309 static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
310 int flags, const char *unused_dev_name,
313 return get_sb_single(fs_type, flags, data, cpuset_fill_super);
316 static struct file_system_type cpuset_fs_type = {
318 .get_sb = cpuset_get_sb,
319 .kill_sb = kill_litter_super,
324 * The files in the cpuset filesystem mostly have a very simple read/write
325 * handling, some common function will take care of it. Nevertheless some cases
326 * (read tasks) are special and therefore I define this structure for every
330 * When reading/writing to a file:
331 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
332 * - the 'cftype' of the file is file->f_dentry->d_fsdata
338 int (*open) (struct inode *inode, struct file *file);
339 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
341 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
343 int (*release) (struct inode *inode, struct file *file);
346 static inline struct cpuset *__d_cs(struct dentry *dentry)
348 return dentry->d_fsdata;
351 static inline struct cftype *__d_cft(struct dentry *dentry)
353 return dentry->d_fsdata;
357 * Call with cpuset_sem held. Writes path of cpuset into buf.
358 * Returns 0 on success, -errno on error.
361 static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
365 start = buf + buflen;
369 int len = cs->dentry->d_name.len;
370 if ((start -= len) < buf)
371 return -ENAMETOOLONG;
372 memcpy(start, cs->dentry->d_name.name, len);
379 return -ENAMETOOLONG;
382 memmove(buf, start, buf + buflen - start);
387 * Notify userspace when a cpuset is released, by running
388 * /sbin/cpuset_release_agent with the name of the cpuset (path
389 * relative to the root of cpuset file system) as the argument.
391 * Most likely, this user command will try to rmdir this cpuset.
393 * This races with the possibility that some other task will be
394 * attached to this cpuset before it is removed, or that some other
395 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
396 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
397 * unused, and this cpuset will be reprieved from its death sentence,
398 * to continue to serve a useful existence. Next time it's released,
399 * we will get notified again, if it still has 'notify_on_release' set.
401 * The final arg to call_usermodehelper() is 0, which means don't
402 * wait. The separate /sbin/cpuset_release_agent task is forked by
403 * call_usermodehelper(), then control in this thread returns here,
404 * without waiting for the release agent task. We don't bother to
405 * wait because the caller of this routine has no use for the exit
406 * status of the /sbin/cpuset_release_agent task, so no sense holding
407 * our caller up for that.
409 * The simple act of forking that task might require more memory,
410 * which might need cpuset_sem. So this routine must be called while
411 * cpuset_sem is not held, to avoid a possible deadlock. See also
412 * comments for check_for_release(), below.
415 static void cpuset_release_agent(const char *pathbuf)
417 char *argv[3], *envp[3];
424 argv[i++] = "/sbin/cpuset_release_agent";
425 argv[i++] = (char *)pathbuf;
429 /* minimal command environment */
430 envp[i++] = "HOME=/";
431 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
434 call_usermodehelper(argv[0], argv, envp, 0);
439 * Either cs->count of using tasks transitioned to zero, or the
440 * cs->children list of child cpusets just became empty. If this
441 * cs is notify_on_release() and now both the user count is zero and
442 * the list of children is empty, prepare cpuset path in a kmalloc'd
443 * buffer, to be returned via ppathbuf, so that the caller can invoke
444 * cpuset_release_agent() with it later on, once cpuset_sem is dropped.
445 * Call here with cpuset_sem held.
447 * This check_for_release() routine is responsible for kmalloc'ing
448 * pathbuf. The above cpuset_release_agent() is responsible for
449 * kfree'ing pathbuf. The caller of these routines is responsible
450 * for providing a pathbuf pointer, initialized to NULL, then
451 * calling check_for_release() with cpuset_sem held and the address
452 * of the pathbuf pointer, then dropping cpuset_sem, then calling
453 * cpuset_release_agent() with pathbuf, as set by check_for_release().
456 static void check_for_release(struct cpuset *cs, char **ppathbuf)
458 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
459 list_empty(&cs->children)) {
462 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
465 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
473 * Return in *pmask the portion of a cpusets's cpus_allowed that
474 * are online. If none are online, walk up the cpuset hierarchy
475 * until we find one that does have some online cpus. If we get
476 * all the way to the top and still haven't found any online cpus,
477 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
478 * task, return cpu_online_map.
480 * One way or another, we guarantee to return some non-empty subset
483 * Call with cpuset_sem held.
486 static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
488 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
491 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
493 *pmask = cpu_online_map;
494 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
498 * Return in *pmask the portion of a cpusets's mems_allowed that
499 * are online. If none are online, walk up the cpuset hierarchy
500 * until we find one that does have some online mems. If we get
501 * all the way to the top and still haven't found any online mems,
502 * return node_online_map.
504 * One way or another, we guarantee to return some non-empty subset
505 * of node_online_map.
507 * Call with cpuset_sem held.
510 static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
512 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
515 nodes_and(*pmask, cs->mems_allowed, node_online_map);
517 *pmask = node_online_map;
518 BUG_ON(!nodes_intersects(*pmask, node_online_map));
522 * Refresh current tasks mems_allowed and mems_generation from
523 * current tasks cpuset. Call with cpuset_sem held.
525 * Be sure to call refresh_mems() on any cpuset operation which
526 * (1) holds cpuset_sem, and (2) might possibly alloc memory.
527 * Call after obtaining cpuset_sem lock, before any possible
528 * allocation. Otherwise one risks trying to allocate memory
529 * while the task cpuset_mems_generation is not the same as
530 * the mems_generation in its cpuset, which would deadlock on
531 * cpuset_sem in cpuset_update_current_mems_allowed().
533 * Since we hold cpuset_sem, once refresh_mems() is called, the
534 * test (current->cpuset_mems_generation != cs->mems_generation)
535 * in cpuset_update_current_mems_allowed() will remain false,
536 * until we drop cpuset_sem. Anyone else who would change our
537 * cpusets mems_generation needs to lock cpuset_sem first.
540 static void refresh_mems(void)
542 struct cpuset *cs = current->cpuset;
544 if (current->cpuset_mems_generation != cs->mems_generation) {
545 guarantee_online_mems(cs, ¤t->mems_allowed);
546 current->cpuset_mems_generation = cs->mems_generation;
551 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
553 * One cpuset is a subset of another if all its allowed CPUs and
554 * Memory Nodes are a subset of the other, and its exclusive flags
555 * are only set if the other's are set.
558 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
560 return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
561 nodes_subset(p->mems_allowed, q->mems_allowed) &&
562 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
563 is_mem_exclusive(p) <= is_mem_exclusive(q);
567 * validate_change() - Used to validate that any proposed cpuset change
568 * follows the structural rules for cpusets.
570 * If we replaced the flag and mask values of the current cpuset
571 * (cur) with those values in the trial cpuset (trial), would
572 * our various subset and exclusive rules still be valid? Presumes
575 * 'cur' is the address of an actual, in-use cpuset. Operations
576 * such as list traversal that depend on the actual address of the
577 * cpuset in the list must use cur below, not trial.
579 * 'trial' is the address of bulk structure copy of cur, with
580 * perhaps one or more of the fields cpus_allowed, mems_allowed,
581 * or flags changed to new, trial values.
583 * Return 0 if valid, -errno if not.
586 static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
588 struct cpuset *c, *par;
590 /* Each of our child cpusets must be a subset of us */
591 list_for_each_entry(c, &cur->children, sibling) {
592 if (!is_cpuset_subset(c, trial))
596 /* Remaining checks don't apply to root cpuset */
597 if ((par = cur->parent) == NULL)
600 /* We must be a subset of our parent cpuset */
601 if (!is_cpuset_subset(trial, par))
604 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
605 list_for_each_entry(c, &par->children, sibling) {
606 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
608 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
610 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
612 nodes_intersects(trial->mems_allowed, c->mems_allowed))
620 * For a given cpuset cur, partition the system as follows
621 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
622 * exclusive child cpusets
623 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
624 * exclusive child cpusets
625 * Build these two partitions by calling partition_sched_domains
627 * Call with cpuset_sem held. May nest a call to the
628 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
630 static void update_cpu_domains(struct cpuset *cur)
632 struct cpuset *c, *par = cur->parent;
633 cpumask_t pspan, cspan;
635 if (par == NULL || cpus_empty(cur->cpus_allowed))
639 * Hack to avoid 2.6.13 partial node dynamic sched domain bug.
640 * Require the 'cpu_exclusive' cpuset to include all (or none)
641 * of the CPUs on each node, or return w/o changing sched domains.
642 * Remove this hack when dynamic sched domains fixed.
647 for_each_cpu_mask(i, cur->cpus_allowed) {
648 cpumask_t mask = node_to_cpumask(cpu_to_node(i));
650 for_each_cpu_mask(j, mask) {
651 if (!cpu_isset(j, cur->cpus_allowed))
658 * Get all cpus from parent's cpus_allowed not part of exclusive
661 pspan = par->cpus_allowed;
662 list_for_each_entry(c, &par->children, sibling) {
663 if (is_cpu_exclusive(c))
664 cpus_andnot(pspan, pspan, c->cpus_allowed);
666 if (is_removed(cur) || !is_cpu_exclusive(cur)) {
667 cpus_or(pspan, pspan, cur->cpus_allowed);
668 if (cpus_equal(pspan, cur->cpus_allowed))
670 cspan = CPU_MASK_NONE;
672 if (cpus_empty(pspan))
674 cspan = cur->cpus_allowed;
676 * Get all cpus from current cpuset's cpus_allowed not part
677 * of exclusive children
679 list_for_each_entry(c, &cur->children, sibling) {
680 if (is_cpu_exclusive(c))
681 cpus_andnot(cspan, cspan, c->cpus_allowed);
686 partition_sched_domains(&pspan, &cspan);
687 unlock_cpu_hotplug();
690 static int update_cpumask(struct cpuset *cs, char *buf)
692 struct cpuset trialcs;
693 int retval, cpus_unchanged;
696 retval = cpulist_parse(buf, trialcs.cpus_allowed);
699 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
700 if (cpus_empty(trialcs.cpus_allowed))
702 retval = validate_change(cs, &trialcs);
705 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
706 cs->cpus_allowed = trialcs.cpus_allowed;
707 if (is_cpu_exclusive(cs) && !cpus_unchanged)
708 update_cpu_domains(cs);
712 static int update_nodemask(struct cpuset *cs, char *buf)
714 struct cpuset trialcs;
718 retval = nodelist_parse(buf, trialcs.mems_allowed);
721 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
722 if (nodes_empty(trialcs.mems_allowed))
724 retval = validate_change(cs, &trialcs);
726 cs->mems_allowed = trialcs.mems_allowed;
727 atomic_inc(&cpuset_mems_generation);
728 cs->mems_generation = atomic_read(&cpuset_mems_generation);
734 * update_flag - read a 0 or a 1 in a file and update associated flag
735 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
736 * CS_NOTIFY_ON_RELEASE)
737 * cs: the cpuset to update
738 * buf: the buffer where we read the 0 or 1
741 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
744 struct cpuset trialcs;
745 int err, cpu_exclusive_changed;
747 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
751 set_bit(bit, &trialcs.flags);
753 clear_bit(bit, &trialcs.flags);
755 err = validate_change(cs, &trialcs);
758 cpu_exclusive_changed =
759 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
761 set_bit(bit, &cs->flags);
763 clear_bit(bit, &cs->flags);
765 if (cpu_exclusive_changed)
766 update_cpu_domains(cs);
770 static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
773 struct task_struct *tsk;
774 struct cpuset *oldcs;
777 if (sscanf(pidbuf, "%d", &pid) != 1)
779 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
783 read_lock(&tasklist_lock);
785 tsk = find_task_by_pid(pid);
787 read_unlock(&tasklist_lock);
791 get_task_struct(tsk);
792 read_unlock(&tasklist_lock);
794 if ((current->euid) && (current->euid != tsk->uid)
795 && (current->euid != tsk->suid)) {
796 put_task_struct(tsk);
801 get_task_struct(tsk);
808 put_task_struct(tsk);
811 atomic_inc(&cs->count);
815 guarantee_online_cpus(cs, &cpus);
816 set_cpus_allowed(tsk, cpus);
818 put_task_struct(tsk);
819 if (atomic_dec_and_test(&oldcs->count))
820 check_for_release(oldcs, ppathbuf);
824 /* The various types of files and directories in a cpuset file system */
833 FILE_NOTIFY_ON_RELEASE,
837 static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
838 size_t nbytes, loff_t *unused_ppos)
840 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
841 struct cftype *cft = __d_cft(file->f_dentry);
842 cpuset_filetype_t type = cft->private;
844 char *pathbuf = NULL;
847 /* Crude upper limit on largest legitimate cpulist user might write. */
848 if (nbytes > 100 + 6 * NR_CPUS)
851 /* +1 for nul-terminator */
852 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
855 if (copy_from_user(buffer, userbuf, nbytes)) {
859 buffer[nbytes] = 0; /* nul-terminate */
863 if (is_removed(cs)) {
870 retval = update_cpumask(cs, buffer);
873 retval = update_nodemask(cs, buffer);
875 case FILE_CPU_EXCLUSIVE:
876 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
878 case FILE_MEM_EXCLUSIVE:
879 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
881 case FILE_NOTIFY_ON_RELEASE:
882 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
885 retval = attach_task(cs, buffer, &pathbuf);
896 cpuset_release_agent(pathbuf);
902 static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
903 size_t nbytes, loff_t *ppos)
906 struct cftype *cft = __d_cft(file->f_dentry);
910 /* special function ? */
912 retval = cft->write(file, buf, nbytes, ppos);
914 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
920 * These ascii lists should be read in a single call, by using a user
921 * buffer large enough to hold the entire map. If read in smaller
922 * chunks, there is no guarantee of atomicity. Since the display format
923 * used, list of ranges of sequential numbers, is variable length,
924 * and since these maps can change value dynamically, one could read
925 * gibberish by doing partial reads while a list was changing.
926 * A single large read to a buffer that crosses a page boundary is
927 * ok, because the result being copied to user land is not recomputed
928 * across a page fault.
931 static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
936 mask = cs->cpus_allowed;
939 return cpulist_scnprintf(page, PAGE_SIZE, mask);
942 static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
947 mask = cs->mems_allowed;
950 return nodelist_scnprintf(page, PAGE_SIZE, mask);
953 static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
954 size_t nbytes, loff_t *ppos)
956 struct cftype *cft = __d_cft(file->f_dentry);
957 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
958 cpuset_filetype_t type = cft->private;
965 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
972 s += cpuset_sprintf_cpulist(s, cs);
975 s += cpuset_sprintf_memlist(s, cs);
977 case FILE_CPU_EXCLUSIVE:
978 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
980 case FILE_MEM_EXCLUSIVE:
981 *s++ = is_mem_exclusive(cs) ? '1' : '0';
983 case FILE_NOTIFY_ON_RELEASE:
984 *s++ = notify_on_release(cs) ? '1' : '0';
993 start = page + *ppos;
995 retval = n - copy_to_user(buf, start, min(n, nbytes));
998 free_page((unsigned long)page);
1002 static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1006 struct cftype *cft = __d_cft(file->f_dentry);
1010 /* special function ? */
1012 retval = cft->read(file, buf, nbytes, ppos);
1014 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1019 static int cpuset_file_open(struct inode *inode, struct file *file)
1024 err = generic_file_open(inode, file);
1028 cft = __d_cft(file->f_dentry);
1032 err = cft->open(inode, file);
1039 static int cpuset_file_release(struct inode *inode, struct file *file)
1041 struct cftype *cft = __d_cft(file->f_dentry);
1043 return cft->release(inode, file);
1047 static struct file_operations cpuset_file_operations = {
1048 .read = cpuset_file_read,
1049 .write = cpuset_file_write,
1050 .llseek = generic_file_llseek,
1051 .open = cpuset_file_open,
1052 .release = cpuset_file_release,
1055 static struct inode_operations cpuset_dir_inode_operations = {
1056 .lookup = simple_lookup,
1057 .mkdir = cpuset_mkdir,
1058 .rmdir = cpuset_rmdir,
1061 static int cpuset_create_file(struct dentry *dentry, int mode)
1063 struct inode *inode;
1067 if (dentry->d_inode)
1070 inode = cpuset_new_inode(mode);
1074 if (S_ISDIR(mode)) {
1075 inode->i_op = &cpuset_dir_inode_operations;
1076 inode->i_fop = &simple_dir_operations;
1078 /* start off with i_nlink == 2 (for "." entry) */
1080 } else if (S_ISREG(mode)) {
1082 inode->i_fop = &cpuset_file_operations;
1085 d_instantiate(dentry, inode);
1086 dget(dentry); /* Extra count - pin the dentry in core */
1091 * cpuset_create_dir - create a directory for an object.
1092 * cs: the cpuset we create the directory for.
1093 * It must have a valid ->parent field
1094 * And we are going to fill its ->dentry field.
1095 * name: The name to give to the cpuset directory. Will be copied.
1096 * mode: mode to set on new directory.
1099 static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1101 struct dentry *dentry = NULL;
1102 struct dentry *parent;
1105 parent = cs->parent->dentry;
1106 dentry = cpuset_get_dentry(parent, name);
1108 return PTR_ERR(dentry);
1109 error = cpuset_create_file(dentry, S_IFDIR | mode);
1111 dentry->d_fsdata = cs;
1112 parent->d_inode->i_nlink++;
1113 cs->dentry = dentry;
1120 static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1122 struct dentry *dentry;
1125 down(&dir->d_inode->i_sem);
1126 dentry = cpuset_get_dentry(dir, cft->name);
1127 if (!IS_ERR(dentry)) {
1128 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1130 dentry->d_fsdata = (void *)cft;
1133 error = PTR_ERR(dentry);
1134 up(&dir->d_inode->i_sem);
1139 * Stuff for reading the 'tasks' file.
1141 * Reading this file can return large amounts of data if a cpuset has
1142 * *lots* of attached tasks. So it may need several calls to read(),
1143 * but we cannot guarantee that the information we produce is correct
1144 * unless we produce it entirely atomically.
1146 * Upon tasks file open(), a struct ctr_struct is allocated, that
1147 * will have a pointer to an array (also allocated here). The struct
1148 * ctr_struct * is stored in file->private_data. Its resources will
1149 * be freed by release() when the file is closed. The array is used
1150 * to sprintf the PIDs and then used by read().
1153 /* cpusets_tasks_read array */
1161 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1162 * Return actual number of pids loaded.
1164 static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1167 struct task_struct *g, *p;
1169 read_lock(&tasklist_lock);
1171 do_each_thread(g, p) {
1172 if (p->cpuset == cs) {
1173 pidarray[n++] = p->pid;
1174 if (unlikely(n == npids))
1177 } while_each_thread(g, p);
1180 read_unlock(&tasklist_lock);
1184 static int cmppid(const void *a, const void *b)
1186 return *(pid_t *)a - *(pid_t *)b;
1190 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1191 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1192 * count 'cnt' of how many chars would be written if buf were large enough.
1194 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1199 for (i = 0; i < npids; i++)
1200 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1204 static int cpuset_tasks_open(struct inode *unused, struct file *file)
1206 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1207 struct ctr_struct *ctr;
1212 if (!(file->f_mode & FMODE_READ))
1215 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1220 * If cpuset gets more users after we read count, we won't have
1221 * enough space - tough. This race is indistinguishable to the
1222 * caller from the case that the additional cpuset users didn't
1223 * show up until sometime later on.
1225 npids = atomic_read(&cs->count);
1226 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1230 npids = pid_array_load(pidarray, npids, cs);
1231 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1233 /* Call pid_array_to_buf() twice, first just to get bufsz */
1234 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1235 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1238 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1241 file->private_data = ctr;
1252 static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1253 size_t nbytes, loff_t *ppos)
1255 struct ctr_struct *ctr = file->private_data;
1257 if (*ppos + nbytes > ctr->bufsz)
1258 nbytes = ctr->bufsz - *ppos;
1259 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1265 static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1267 struct ctr_struct *ctr;
1269 if (file->f_mode & FMODE_READ) {
1270 ctr = file->private_data;
1278 * for the common functions, 'private' gives the type of file
1281 static struct cftype cft_tasks = {
1283 .open = cpuset_tasks_open,
1284 .read = cpuset_tasks_read,
1285 .release = cpuset_tasks_release,
1286 .private = FILE_TASKLIST,
1289 static struct cftype cft_cpus = {
1291 .private = FILE_CPULIST,
1294 static struct cftype cft_mems = {
1296 .private = FILE_MEMLIST,
1299 static struct cftype cft_cpu_exclusive = {
1300 .name = "cpu_exclusive",
1301 .private = FILE_CPU_EXCLUSIVE,
1304 static struct cftype cft_mem_exclusive = {
1305 .name = "mem_exclusive",
1306 .private = FILE_MEM_EXCLUSIVE,
1309 static struct cftype cft_notify_on_release = {
1310 .name = "notify_on_release",
1311 .private = FILE_NOTIFY_ON_RELEASE,
1314 static int cpuset_populate_dir(struct dentry *cs_dentry)
1318 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1320 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1322 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1324 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1326 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1328 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1334 * cpuset_create - create a cpuset
1335 * parent: cpuset that will be parent of the new cpuset.
1336 * name: name of the new cpuset. Will be strcpy'ed.
1337 * mode: mode to set on new inode
1339 * Must be called with the semaphore on the parent inode held
1342 static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1347 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1354 if (notify_on_release(parent))
1355 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
1356 cs->cpus_allowed = CPU_MASK_NONE;
1357 cs->mems_allowed = NODE_MASK_NONE;
1358 atomic_set(&cs->count, 0);
1359 INIT_LIST_HEAD(&cs->sibling);
1360 INIT_LIST_HEAD(&cs->children);
1361 atomic_inc(&cpuset_mems_generation);
1362 cs->mems_generation = atomic_read(&cpuset_mems_generation);
1364 cs->parent = parent;
1366 list_add(&cs->sibling, &cs->parent->children);
1368 err = cpuset_create_dir(cs, name, mode);
1373 * Release cpuset_sem before cpuset_populate_dir() because it
1374 * will down() this new directory's i_sem and if we race with
1375 * another mkdir, we might deadlock.
1379 err = cpuset_populate_dir(cs->dentry);
1380 /* If err < 0, we have a half-filled directory - oh well ;) */
1383 list_del(&cs->sibling);
1389 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1391 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1393 /* the vfs holds inode->i_sem already */
1394 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1397 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1399 struct cpuset *cs = dentry->d_fsdata;
1401 struct cpuset *parent;
1402 char *pathbuf = NULL;
1404 /* the vfs holds both inode->i_sem already */
1408 if (atomic_read(&cs->count) > 0) {
1412 if (!list_empty(&cs->children)) {
1416 parent = cs->parent;
1417 set_bit(CS_REMOVED, &cs->flags);
1418 if (is_cpu_exclusive(cs))
1419 update_cpu_domains(cs);
1420 list_del(&cs->sibling); /* delete my sibling from parent->children */
1421 if (list_empty(&parent->children))
1422 check_for_release(parent, &pathbuf);
1423 spin_lock(&cs->dentry->d_lock);
1424 d = dget(cs->dentry);
1426 spin_unlock(&d->d_lock);
1427 cpuset_d_remove_dir(d);
1430 cpuset_release_agent(pathbuf);
1435 * cpuset_init - initialize cpusets at system boot
1437 * Description: Initialize top_cpuset and the cpuset internal file system,
1440 int __init cpuset_init(void)
1442 struct dentry *root;
1445 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1446 top_cpuset.mems_allowed = NODE_MASK_ALL;
1448 atomic_inc(&cpuset_mems_generation);
1449 top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
1451 init_task.cpuset = &top_cpuset;
1453 err = register_filesystem(&cpuset_fs_type);
1456 cpuset_mount = kern_mount(&cpuset_fs_type);
1457 if (IS_ERR(cpuset_mount)) {
1458 printk(KERN_ERR "cpuset: could not mount!\n");
1459 err = PTR_ERR(cpuset_mount);
1460 cpuset_mount = NULL;
1463 root = cpuset_mount->mnt_sb->s_root;
1464 root->d_fsdata = &top_cpuset;
1465 root->d_inode->i_nlink++;
1466 top_cpuset.dentry = root;
1467 root->d_inode->i_op = &cpuset_dir_inode_operations;
1468 err = cpuset_populate_dir(root);
1474 * cpuset_init_smp - initialize cpus_allowed
1476 * Description: Finish top cpuset after cpu, node maps are initialized
1479 void __init cpuset_init_smp(void)
1481 top_cpuset.cpus_allowed = cpu_online_map;
1482 top_cpuset.mems_allowed = node_online_map;
1486 * cpuset_fork - attach newly forked task to its parents cpuset.
1487 * @tsk: pointer to task_struct of forking parent process.
1489 * Description: By default, on fork, a task inherits its
1490 * parent's cpuset. The pointer to the shared cpuset is
1491 * automatically copied in fork.c by dup_task_struct().
1492 * This cpuset_fork() routine need only increment the usage
1493 * counter in that cpuset.
1496 void cpuset_fork(struct task_struct *tsk)
1498 atomic_inc(&tsk->cpuset->count);
1502 * cpuset_exit - detach cpuset from exiting task
1503 * @tsk: pointer to task_struct of exiting process
1505 * Description: Detach cpuset from @tsk and release it.
1507 * Note that cpusets marked notify_on_release force every task
1508 * in them to take the global cpuset_sem semaphore when exiting.
1509 * This could impact scaling on very large systems. Be reluctant
1510 * to use notify_on_release cpusets where very high task exit
1511 * scaling is required on large systems.
1513 * Don't even think about derefencing 'cs' after the cpuset use
1514 * count goes to zero, except inside a critical section guarded
1515 * by the cpuset_sem semaphore. If you don't hold cpuset_sem,
1516 * then a zero cpuset use count is a license to any other task to
1517 * nuke the cpuset immediately.
1520 void cpuset_exit(struct task_struct *tsk)
1529 if (notify_on_release(cs)) {
1530 char *pathbuf = NULL;
1533 if (atomic_dec_and_test(&cs->count))
1534 check_for_release(cs, &pathbuf);
1536 cpuset_release_agent(pathbuf);
1538 atomic_dec(&cs->count);
1543 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1544 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
1546 * Description: Returns the cpumask_t cpus_allowed of the cpuset
1547 * attached to the specified @tsk. Guaranteed to return some non-empty
1548 * subset of cpu_online_map, even if this means going outside the
1552 cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
1557 task_lock((struct task_struct *)tsk);
1558 guarantee_online_cpus(tsk->cpuset, &mask);
1559 task_unlock((struct task_struct *)tsk);
1565 void cpuset_init_current_mems_allowed(void)
1567 current->mems_allowed = NODE_MASK_ALL;
1571 * cpuset_update_current_mems_allowed - update mems parameters to new values
1573 * If the current tasks cpusets mems_allowed changed behind our backs,
1574 * update current->mems_allowed and mems_generation to the new value.
1575 * Do not call this routine if in_interrupt().
1578 void cpuset_update_current_mems_allowed(void)
1580 struct cpuset *cs = current->cpuset;
1583 return; /* task is exiting */
1584 if (current->cpuset_mems_generation != cs->mems_generation) {
1592 * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
1593 * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
1595 void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
1597 bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
1602 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
1603 * @zl: the zonelist to be checked
1605 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1607 int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
1611 for (i = 0; zl->zones[i]; i++) {
1612 int nid = zl->zones[i]->zone_pgdat->node_id;
1614 if (node_isset(nid, current->mems_allowed))
1621 * cpuset_zone_allowed - is zone z allowed in current->mems_allowed
1622 * @z: zone in question
1624 * Is zone z allowed in current->mems_allowed, or is
1625 * the CPU in interrupt context? (zone is always allowed in this case)
1627 int cpuset_zone_allowed(struct zone *z)
1629 return in_interrupt() ||
1630 node_isset(z->zone_pgdat->node_id, current->mems_allowed);
1634 * proc_cpuset_show()
1635 * - Print tasks cpuset path into seq_file.
1636 * - Used for /proc/<pid>/cpuset.
1639 static int proc_cpuset_show(struct seq_file *m, void *v)
1642 struct task_struct *tsk;
1646 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1660 retval = cpuset_path(cs, buf, PAGE_SIZE);
1671 static int cpuset_open(struct inode *inode, struct file *file)
1673 struct task_struct *tsk = PROC_I(inode)->task;
1674 return single_open(file, proc_cpuset_show, tsk);
1677 struct file_operations proc_cpuset_operations = {
1678 .open = cpuset_open,
1680 .llseek = seq_lseek,
1681 .release = single_release,
1684 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
1685 char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
1687 buffer += sprintf(buffer, "Cpus_allowed:\t");
1688 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
1689 buffer += sprintf(buffer, "\n");
1690 buffer += sprintf(buffer, "Mems_allowed:\t");
1691 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
1692 buffer += sprintf(buffer, "\n");