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);
183 static struct task_struct *cpuset_sem_owner;
184 static int cpuset_sem_depth;
187 * The global cpuset semaphore cpuset_sem can be needed by the
188 * memory allocator to update a tasks mems_allowed (see the calls
189 * to cpuset_update_current_mems_allowed()) or to walk up the
190 * cpuset hierarchy to find a mem_exclusive cpuset see the calls
191 * to cpuset_excl_nodes_overlap()).
193 * But if the memory allocation is being done by cpuset.c code, it
194 * usually already holds cpuset_sem. Double tripping on a kernel
195 * semaphore deadlocks the current task, and any other task that
196 * subsequently tries to obtain the lock.
198 * Run all up's and down's on cpuset_sem through the following
199 * wrappers, which will detect this nested locking, and avoid
203 static inline void cpuset_down(struct semaphore *psem)
205 if (cpuset_sem_owner != current) {
207 cpuset_sem_owner = current;
212 static inline void cpuset_up(struct semaphore *psem)
214 if (--cpuset_sem_depth == 0) {
215 cpuset_sem_owner = NULL;
221 * A couple of forward declarations required, due to cyclic reference loop:
222 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
223 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
226 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
227 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
229 static struct backing_dev_info cpuset_backing_dev_info = {
230 .ra_pages = 0, /* No readahead */
231 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
234 static struct inode *cpuset_new_inode(mode_t mode)
236 struct inode *inode = new_inode(cpuset_sb);
239 inode->i_mode = mode;
240 inode->i_uid = current->fsuid;
241 inode->i_gid = current->fsgid;
242 inode->i_blksize = PAGE_CACHE_SIZE;
244 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
245 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
250 static void cpuset_diput(struct dentry *dentry, struct inode *inode)
252 /* is dentry a directory ? if so, kfree() associated cpuset */
253 if (S_ISDIR(inode->i_mode)) {
254 struct cpuset *cs = dentry->d_fsdata;
255 BUG_ON(!(is_removed(cs)));
261 static struct dentry_operations cpuset_dops = {
262 .d_iput = cpuset_diput,
265 static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
267 struct dentry *d = lookup_one_len(name, parent, strlen(name));
269 d->d_op = &cpuset_dops;
273 static void remove_dir(struct dentry *d)
275 struct dentry *parent = dget(d->d_parent);
278 simple_rmdir(parent->d_inode, d);
283 * NOTE : the dentry must have been dget()'ed
285 static void cpuset_d_remove_dir(struct dentry *dentry)
287 struct list_head *node;
289 spin_lock(&dcache_lock);
290 node = dentry->d_subdirs.next;
291 while (node != &dentry->d_subdirs) {
292 struct dentry *d = list_entry(node, struct dentry, d_child);
296 spin_unlock(&dcache_lock);
298 simple_unlink(dentry->d_inode, d);
300 spin_lock(&dcache_lock);
302 node = dentry->d_subdirs.next;
304 list_del_init(&dentry->d_child);
305 spin_unlock(&dcache_lock);
309 static struct super_operations cpuset_ops = {
310 .statfs = simple_statfs,
311 .drop_inode = generic_delete_inode,
314 static int cpuset_fill_super(struct super_block *sb, void *unused_data,
320 sb->s_blocksize = PAGE_CACHE_SIZE;
321 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
322 sb->s_magic = CPUSET_SUPER_MAGIC;
323 sb->s_op = &cpuset_ops;
326 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
328 inode->i_op = &simple_dir_inode_operations;
329 inode->i_fop = &simple_dir_operations;
330 /* directories start off with i_nlink == 2 (for "." entry) */
336 root = d_alloc_root(inode);
345 static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
346 int flags, const char *unused_dev_name,
349 return get_sb_single(fs_type, flags, data, cpuset_fill_super);
352 static struct file_system_type cpuset_fs_type = {
354 .get_sb = cpuset_get_sb,
355 .kill_sb = kill_litter_super,
360 * The files in the cpuset filesystem mostly have a very simple read/write
361 * handling, some common function will take care of it. Nevertheless some cases
362 * (read tasks) are special and therefore I define this structure for every
366 * When reading/writing to a file:
367 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
368 * - the 'cftype' of the file is file->f_dentry->d_fsdata
374 int (*open) (struct inode *inode, struct file *file);
375 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
377 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
379 int (*release) (struct inode *inode, struct file *file);
382 static inline struct cpuset *__d_cs(struct dentry *dentry)
384 return dentry->d_fsdata;
387 static inline struct cftype *__d_cft(struct dentry *dentry)
389 return dentry->d_fsdata;
393 * Call with cpuset_sem held. Writes path of cpuset into buf.
394 * Returns 0 on success, -errno on error.
397 static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
401 start = buf + buflen;
405 int len = cs->dentry->d_name.len;
406 if ((start -= len) < buf)
407 return -ENAMETOOLONG;
408 memcpy(start, cs->dentry->d_name.name, len);
415 return -ENAMETOOLONG;
418 memmove(buf, start, buf + buflen - start);
423 * Notify userspace when a cpuset is released, by running
424 * /sbin/cpuset_release_agent with the name of the cpuset (path
425 * relative to the root of cpuset file system) as the argument.
427 * Most likely, this user command will try to rmdir this cpuset.
429 * This races with the possibility that some other task will be
430 * attached to this cpuset before it is removed, or that some other
431 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
432 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
433 * unused, and this cpuset will be reprieved from its death sentence,
434 * to continue to serve a useful existence. Next time it's released,
435 * we will get notified again, if it still has 'notify_on_release' set.
437 * The final arg to call_usermodehelper() is 0, which means don't
438 * wait. The separate /sbin/cpuset_release_agent task is forked by
439 * call_usermodehelper(), then control in this thread returns here,
440 * without waiting for the release agent task. We don't bother to
441 * wait because the caller of this routine has no use for the exit
442 * status of the /sbin/cpuset_release_agent task, so no sense holding
443 * our caller up for that.
445 * The simple act of forking that task might require more memory,
446 * which might need cpuset_sem. So this routine must be called while
447 * cpuset_sem is not held, to avoid a possible deadlock. See also
448 * comments for check_for_release(), below.
451 static void cpuset_release_agent(const char *pathbuf)
453 char *argv[3], *envp[3];
460 argv[i++] = "/sbin/cpuset_release_agent";
461 argv[i++] = (char *)pathbuf;
465 /* minimal command environment */
466 envp[i++] = "HOME=/";
467 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
470 call_usermodehelper(argv[0], argv, envp, 0);
475 * Either cs->count of using tasks transitioned to zero, or the
476 * cs->children list of child cpusets just became empty. If this
477 * cs is notify_on_release() and now both the user count is zero and
478 * the list of children is empty, prepare cpuset path in a kmalloc'd
479 * buffer, to be returned via ppathbuf, so that the caller can invoke
480 * cpuset_release_agent() with it later on, once cpuset_sem is dropped.
481 * Call here with cpuset_sem held.
483 * This check_for_release() routine is responsible for kmalloc'ing
484 * pathbuf. The above cpuset_release_agent() is responsible for
485 * kfree'ing pathbuf. The caller of these routines is responsible
486 * for providing a pathbuf pointer, initialized to NULL, then
487 * calling check_for_release() with cpuset_sem held and the address
488 * of the pathbuf pointer, then dropping cpuset_sem, then calling
489 * cpuset_release_agent() with pathbuf, as set by check_for_release().
492 static void check_for_release(struct cpuset *cs, char **ppathbuf)
494 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
495 list_empty(&cs->children)) {
498 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
501 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
509 * Return in *pmask the portion of a cpusets's cpus_allowed that
510 * are online. If none are online, walk up the cpuset hierarchy
511 * until we find one that does have some online cpus. If we get
512 * all the way to the top and still haven't found any online cpus,
513 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
514 * task, return cpu_online_map.
516 * One way or another, we guarantee to return some non-empty subset
519 * Call with cpuset_sem held.
522 static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
524 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
527 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
529 *pmask = cpu_online_map;
530 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
534 * Return in *pmask the portion of a cpusets's mems_allowed that
535 * are online. If none are online, walk up the cpuset hierarchy
536 * until we find one that does have some online mems. If we get
537 * all the way to the top and still haven't found any online mems,
538 * return node_online_map.
540 * One way or another, we guarantee to return some non-empty subset
541 * of node_online_map.
543 * Call with cpuset_sem held.
546 static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
548 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
551 nodes_and(*pmask, cs->mems_allowed, node_online_map);
553 *pmask = node_online_map;
554 BUG_ON(!nodes_intersects(*pmask, node_online_map));
558 * Refresh current tasks mems_allowed and mems_generation from
559 * current tasks cpuset. Call with cpuset_sem held.
561 * This routine is needed to update the per-task mems_allowed
562 * data, within the tasks context, when it is trying to allocate
563 * memory (in various mm/mempolicy.c routines) and notices
564 * that some other task has been modifying its cpuset.
567 static void refresh_mems(void)
569 struct cpuset *cs = current->cpuset;
571 if (current->cpuset_mems_generation != cs->mems_generation) {
572 guarantee_online_mems(cs, ¤t->mems_allowed);
573 current->cpuset_mems_generation = cs->mems_generation;
578 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
580 * One cpuset is a subset of another if all its allowed CPUs and
581 * Memory Nodes are a subset of the other, and its exclusive flags
582 * are only set if the other's are set.
585 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
587 return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
588 nodes_subset(p->mems_allowed, q->mems_allowed) &&
589 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
590 is_mem_exclusive(p) <= is_mem_exclusive(q);
594 * validate_change() - Used to validate that any proposed cpuset change
595 * follows the structural rules for cpusets.
597 * If we replaced the flag and mask values of the current cpuset
598 * (cur) with those values in the trial cpuset (trial), would
599 * our various subset and exclusive rules still be valid? Presumes
602 * 'cur' is the address of an actual, in-use cpuset. Operations
603 * such as list traversal that depend on the actual address of the
604 * cpuset in the list must use cur below, not trial.
606 * 'trial' is the address of bulk structure copy of cur, with
607 * perhaps one or more of the fields cpus_allowed, mems_allowed,
608 * or flags changed to new, trial values.
610 * Return 0 if valid, -errno if not.
613 static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
615 struct cpuset *c, *par;
617 /* Each of our child cpusets must be a subset of us */
618 list_for_each_entry(c, &cur->children, sibling) {
619 if (!is_cpuset_subset(c, trial))
623 /* Remaining checks don't apply to root cpuset */
624 if ((par = cur->parent) == NULL)
627 /* We must be a subset of our parent cpuset */
628 if (!is_cpuset_subset(trial, par))
631 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
632 list_for_each_entry(c, &par->children, sibling) {
633 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
635 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
637 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
639 nodes_intersects(trial->mems_allowed, c->mems_allowed))
647 * For a given cpuset cur, partition the system as follows
648 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
649 * exclusive child cpusets
650 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
651 * exclusive child cpusets
652 * Build these two partitions by calling partition_sched_domains
654 * Call with cpuset_sem held. May nest a call to the
655 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
658 static void update_cpu_domains(struct cpuset *cur)
660 struct cpuset *c, *par = cur->parent;
661 cpumask_t pspan, cspan;
663 if (par == NULL || cpus_empty(cur->cpus_allowed))
667 * Get all cpus from parent's cpus_allowed not part of exclusive
670 pspan = par->cpus_allowed;
671 list_for_each_entry(c, &par->children, sibling) {
672 if (is_cpu_exclusive(c))
673 cpus_andnot(pspan, pspan, c->cpus_allowed);
675 if (is_removed(cur) || !is_cpu_exclusive(cur)) {
676 cpus_or(pspan, pspan, cur->cpus_allowed);
677 if (cpus_equal(pspan, cur->cpus_allowed))
679 cspan = CPU_MASK_NONE;
681 if (cpus_empty(pspan))
683 cspan = cur->cpus_allowed;
685 * Get all cpus from current cpuset's cpus_allowed not part
686 * of exclusive children
688 list_for_each_entry(c, &cur->children, sibling) {
689 if (is_cpu_exclusive(c))
690 cpus_andnot(cspan, cspan, c->cpus_allowed);
695 partition_sched_domains(&pspan, &cspan);
696 unlock_cpu_hotplug();
699 static int update_cpumask(struct cpuset *cs, char *buf)
701 struct cpuset trialcs;
702 int retval, cpus_unchanged;
705 retval = cpulist_parse(buf, trialcs.cpus_allowed);
708 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
709 if (cpus_empty(trialcs.cpus_allowed))
711 retval = validate_change(cs, &trialcs);
714 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
715 cs->cpus_allowed = trialcs.cpus_allowed;
716 if (is_cpu_exclusive(cs) && !cpus_unchanged)
717 update_cpu_domains(cs);
721 static int update_nodemask(struct cpuset *cs, char *buf)
723 struct cpuset trialcs;
727 retval = nodelist_parse(buf, trialcs.mems_allowed);
730 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
731 if (nodes_empty(trialcs.mems_allowed))
733 retval = validate_change(cs, &trialcs);
735 cs->mems_allowed = trialcs.mems_allowed;
736 atomic_inc(&cpuset_mems_generation);
737 cs->mems_generation = atomic_read(&cpuset_mems_generation);
743 * update_flag - read a 0 or a 1 in a file and update associated flag
744 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
745 * CS_NOTIFY_ON_RELEASE)
746 * cs: the cpuset to update
747 * buf: the buffer where we read the 0 or 1
750 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
753 struct cpuset trialcs;
754 int err, cpu_exclusive_changed;
756 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
760 set_bit(bit, &trialcs.flags);
762 clear_bit(bit, &trialcs.flags);
764 err = validate_change(cs, &trialcs);
767 cpu_exclusive_changed =
768 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
770 set_bit(bit, &cs->flags);
772 clear_bit(bit, &cs->flags);
774 if (cpu_exclusive_changed)
775 update_cpu_domains(cs);
779 static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
782 struct task_struct *tsk;
783 struct cpuset *oldcs;
786 if (sscanf(pidbuf, "%d", &pid) != 1)
788 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
792 read_lock(&tasklist_lock);
794 tsk = find_task_by_pid(pid);
796 read_unlock(&tasklist_lock);
800 get_task_struct(tsk);
801 read_unlock(&tasklist_lock);
803 if ((current->euid) && (current->euid != tsk->uid)
804 && (current->euid != tsk->suid)) {
805 put_task_struct(tsk);
810 get_task_struct(tsk);
817 put_task_struct(tsk);
820 atomic_inc(&cs->count);
824 guarantee_online_cpus(cs, &cpus);
825 set_cpus_allowed(tsk, cpus);
827 put_task_struct(tsk);
828 if (atomic_dec_and_test(&oldcs->count))
829 check_for_release(oldcs, ppathbuf);
833 /* The various types of files and directories in a cpuset file system */
842 FILE_NOTIFY_ON_RELEASE,
846 static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
847 size_t nbytes, loff_t *unused_ppos)
849 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
850 struct cftype *cft = __d_cft(file->f_dentry);
851 cpuset_filetype_t type = cft->private;
853 char *pathbuf = NULL;
856 /* Crude upper limit on largest legitimate cpulist user might write. */
857 if (nbytes > 100 + 6 * NR_CPUS)
860 /* +1 for nul-terminator */
861 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
864 if (copy_from_user(buffer, userbuf, nbytes)) {
868 buffer[nbytes] = 0; /* nul-terminate */
870 cpuset_down(&cpuset_sem);
872 if (is_removed(cs)) {
879 retval = update_cpumask(cs, buffer);
882 retval = update_nodemask(cs, buffer);
884 case FILE_CPU_EXCLUSIVE:
885 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
887 case FILE_MEM_EXCLUSIVE:
888 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
890 case FILE_NOTIFY_ON_RELEASE:
891 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
894 retval = attach_task(cs, buffer, &pathbuf);
904 cpuset_up(&cpuset_sem);
905 cpuset_release_agent(pathbuf);
911 static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
912 size_t nbytes, loff_t *ppos)
915 struct cftype *cft = __d_cft(file->f_dentry);
919 /* special function ? */
921 retval = cft->write(file, buf, nbytes, ppos);
923 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
929 * These ascii lists should be read in a single call, by using a user
930 * buffer large enough to hold the entire map. If read in smaller
931 * chunks, there is no guarantee of atomicity. Since the display format
932 * used, list of ranges of sequential numbers, is variable length,
933 * and since these maps can change value dynamically, one could read
934 * gibberish by doing partial reads while a list was changing.
935 * A single large read to a buffer that crosses a page boundary is
936 * ok, because the result being copied to user land is not recomputed
937 * across a page fault.
940 static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
944 cpuset_down(&cpuset_sem);
945 mask = cs->cpus_allowed;
946 cpuset_up(&cpuset_sem);
948 return cpulist_scnprintf(page, PAGE_SIZE, mask);
951 static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
955 cpuset_down(&cpuset_sem);
956 mask = cs->mems_allowed;
957 cpuset_up(&cpuset_sem);
959 return nodelist_scnprintf(page, PAGE_SIZE, mask);
962 static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
963 size_t nbytes, loff_t *ppos)
965 struct cftype *cft = __d_cft(file->f_dentry);
966 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
967 cpuset_filetype_t type = cft->private;
972 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
979 s += cpuset_sprintf_cpulist(s, cs);
982 s += cpuset_sprintf_memlist(s, cs);
984 case FILE_CPU_EXCLUSIVE:
985 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
987 case FILE_MEM_EXCLUSIVE:
988 *s++ = is_mem_exclusive(cs) ? '1' : '0';
990 case FILE_NOTIFY_ON_RELEASE:
991 *s++ = notify_on_release(cs) ? '1' : '0';
1000 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1002 free_page((unsigned long)page);
1006 static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1010 struct cftype *cft = __d_cft(file->f_dentry);
1014 /* special function ? */
1016 retval = cft->read(file, buf, nbytes, ppos);
1018 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1023 static int cpuset_file_open(struct inode *inode, struct file *file)
1028 err = generic_file_open(inode, file);
1032 cft = __d_cft(file->f_dentry);
1036 err = cft->open(inode, file);
1043 static int cpuset_file_release(struct inode *inode, struct file *file)
1045 struct cftype *cft = __d_cft(file->f_dentry);
1047 return cft->release(inode, file);
1051 static struct file_operations cpuset_file_operations = {
1052 .read = cpuset_file_read,
1053 .write = cpuset_file_write,
1054 .llseek = generic_file_llseek,
1055 .open = cpuset_file_open,
1056 .release = cpuset_file_release,
1059 static struct inode_operations cpuset_dir_inode_operations = {
1060 .lookup = simple_lookup,
1061 .mkdir = cpuset_mkdir,
1062 .rmdir = cpuset_rmdir,
1065 static int cpuset_create_file(struct dentry *dentry, int mode)
1067 struct inode *inode;
1071 if (dentry->d_inode)
1074 inode = cpuset_new_inode(mode);
1078 if (S_ISDIR(mode)) {
1079 inode->i_op = &cpuset_dir_inode_operations;
1080 inode->i_fop = &simple_dir_operations;
1082 /* start off with i_nlink == 2 (for "." entry) */
1084 } else if (S_ISREG(mode)) {
1086 inode->i_fop = &cpuset_file_operations;
1089 d_instantiate(dentry, inode);
1090 dget(dentry); /* Extra count - pin the dentry in core */
1095 * cpuset_create_dir - create a directory for an object.
1096 * cs: the cpuset we create the directory for.
1097 * It must have a valid ->parent field
1098 * And we are going to fill its ->dentry field.
1099 * name: The name to give to the cpuset directory. Will be copied.
1100 * mode: mode to set on new directory.
1103 static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1105 struct dentry *dentry = NULL;
1106 struct dentry *parent;
1109 parent = cs->parent->dentry;
1110 dentry = cpuset_get_dentry(parent, name);
1112 return PTR_ERR(dentry);
1113 error = cpuset_create_file(dentry, S_IFDIR | mode);
1115 dentry->d_fsdata = cs;
1116 parent->d_inode->i_nlink++;
1117 cs->dentry = dentry;
1124 static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1126 struct dentry *dentry;
1129 down(&dir->d_inode->i_sem);
1130 dentry = cpuset_get_dentry(dir, cft->name);
1131 if (!IS_ERR(dentry)) {
1132 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1134 dentry->d_fsdata = (void *)cft;
1137 error = PTR_ERR(dentry);
1138 up(&dir->d_inode->i_sem);
1143 * Stuff for reading the 'tasks' file.
1145 * Reading this file can return large amounts of data if a cpuset has
1146 * *lots* of attached tasks. So it may need several calls to read(),
1147 * but we cannot guarantee that the information we produce is correct
1148 * unless we produce it entirely atomically.
1150 * Upon tasks file open(), a struct ctr_struct is allocated, that
1151 * will have a pointer to an array (also allocated here). The struct
1152 * ctr_struct * is stored in file->private_data. Its resources will
1153 * be freed by release() when the file is closed. The array is used
1154 * to sprintf the PIDs and then used by read().
1157 /* cpusets_tasks_read array */
1165 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1166 * Return actual number of pids loaded.
1168 static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1171 struct task_struct *g, *p;
1173 read_lock(&tasklist_lock);
1175 do_each_thread(g, p) {
1176 if (p->cpuset == cs) {
1177 pidarray[n++] = p->pid;
1178 if (unlikely(n == npids))
1181 } while_each_thread(g, p);
1184 read_unlock(&tasklist_lock);
1188 static int cmppid(const void *a, const void *b)
1190 return *(pid_t *)a - *(pid_t *)b;
1194 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1195 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1196 * count 'cnt' of how many chars would be written if buf were large enough.
1198 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1203 for (i = 0; i < npids; i++)
1204 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1208 static int cpuset_tasks_open(struct inode *unused, struct file *file)
1210 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1211 struct ctr_struct *ctr;
1216 if (!(file->f_mode & FMODE_READ))
1219 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1224 * If cpuset gets more users after we read count, we won't have
1225 * enough space - tough. This race is indistinguishable to the
1226 * caller from the case that the additional cpuset users didn't
1227 * show up until sometime later on.
1229 npids = atomic_read(&cs->count);
1230 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1234 npids = pid_array_load(pidarray, npids, cs);
1235 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1237 /* Call pid_array_to_buf() twice, first just to get bufsz */
1238 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1239 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1242 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1245 file->private_data = ctr;
1256 static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1257 size_t nbytes, loff_t *ppos)
1259 struct ctr_struct *ctr = file->private_data;
1261 if (*ppos + nbytes > ctr->bufsz)
1262 nbytes = ctr->bufsz - *ppos;
1263 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1269 static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1271 struct ctr_struct *ctr;
1273 if (file->f_mode & FMODE_READ) {
1274 ctr = file->private_data;
1282 * for the common functions, 'private' gives the type of file
1285 static struct cftype cft_tasks = {
1287 .open = cpuset_tasks_open,
1288 .read = cpuset_tasks_read,
1289 .release = cpuset_tasks_release,
1290 .private = FILE_TASKLIST,
1293 static struct cftype cft_cpus = {
1295 .private = FILE_CPULIST,
1298 static struct cftype cft_mems = {
1300 .private = FILE_MEMLIST,
1303 static struct cftype cft_cpu_exclusive = {
1304 .name = "cpu_exclusive",
1305 .private = FILE_CPU_EXCLUSIVE,
1308 static struct cftype cft_mem_exclusive = {
1309 .name = "mem_exclusive",
1310 .private = FILE_MEM_EXCLUSIVE,
1313 static struct cftype cft_notify_on_release = {
1314 .name = "notify_on_release",
1315 .private = FILE_NOTIFY_ON_RELEASE,
1318 static int cpuset_populate_dir(struct dentry *cs_dentry)
1322 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1324 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1326 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1328 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1330 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1332 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1338 * cpuset_create - create a cpuset
1339 * parent: cpuset that will be parent of the new cpuset.
1340 * name: name of the new cpuset. Will be strcpy'ed.
1341 * mode: mode to set on new inode
1343 * Must be called with the semaphore on the parent inode held
1346 static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1351 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1355 cpuset_down(&cpuset_sem);
1357 if (notify_on_release(parent))
1358 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
1359 cs->cpus_allowed = CPU_MASK_NONE;
1360 cs->mems_allowed = NODE_MASK_NONE;
1361 atomic_set(&cs->count, 0);
1362 INIT_LIST_HEAD(&cs->sibling);
1363 INIT_LIST_HEAD(&cs->children);
1364 atomic_inc(&cpuset_mems_generation);
1365 cs->mems_generation = atomic_read(&cpuset_mems_generation);
1367 cs->parent = parent;
1369 list_add(&cs->sibling, &cs->parent->children);
1371 err = cpuset_create_dir(cs, name, mode);
1376 * Release cpuset_sem before cpuset_populate_dir() because it
1377 * will down() this new directory's i_sem and if we race with
1378 * another mkdir, we might deadlock.
1380 cpuset_up(&cpuset_sem);
1382 err = cpuset_populate_dir(cs->dentry);
1383 /* If err < 0, we have a half-filled directory - oh well ;) */
1386 list_del(&cs->sibling);
1387 cpuset_up(&cpuset_sem);
1392 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1394 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1396 /* the vfs holds inode->i_sem already */
1397 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1400 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1402 struct cpuset *cs = dentry->d_fsdata;
1404 struct cpuset *parent;
1405 char *pathbuf = NULL;
1407 /* the vfs holds both inode->i_sem already */
1409 cpuset_down(&cpuset_sem);
1410 if (atomic_read(&cs->count) > 0) {
1411 cpuset_up(&cpuset_sem);
1414 if (!list_empty(&cs->children)) {
1415 cpuset_up(&cpuset_sem);
1418 parent = cs->parent;
1419 set_bit(CS_REMOVED, &cs->flags);
1420 if (is_cpu_exclusive(cs))
1421 update_cpu_domains(cs);
1422 list_del(&cs->sibling); /* delete my sibling from parent->children */
1423 if (list_empty(&parent->children))
1424 check_for_release(parent, &pathbuf);
1425 spin_lock(&cs->dentry->d_lock);
1426 d = dget(cs->dentry);
1428 spin_unlock(&d->d_lock);
1429 cpuset_d_remove_dir(d);
1431 cpuset_up(&cpuset_sem);
1432 cpuset_release_agent(pathbuf);
1437 * cpuset_init - initialize cpusets at system boot
1439 * Description: Initialize top_cpuset and the cpuset internal file system,
1442 int __init cpuset_init(void)
1444 struct dentry *root;
1447 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1448 top_cpuset.mems_allowed = NODE_MASK_ALL;
1450 atomic_inc(&cpuset_mems_generation);
1451 top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
1453 init_task.cpuset = &top_cpuset;
1455 err = register_filesystem(&cpuset_fs_type);
1458 cpuset_mount = kern_mount(&cpuset_fs_type);
1459 if (IS_ERR(cpuset_mount)) {
1460 printk(KERN_ERR "cpuset: could not mount!\n");
1461 err = PTR_ERR(cpuset_mount);
1462 cpuset_mount = NULL;
1465 root = cpuset_mount->mnt_sb->s_root;
1466 root->d_fsdata = &top_cpuset;
1467 root->d_inode->i_nlink++;
1468 top_cpuset.dentry = root;
1469 root->d_inode->i_op = &cpuset_dir_inode_operations;
1470 err = cpuset_populate_dir(root);
1476 * cpuset_init_smp - initialize cpus_allowed
1478 * Description: Finish top cpuset after cpu, node maps are initialized
1481 void __init cpuset_init_smp(void)
1483 top_cpuset.cpus_allowed = cpu_online_map;
1484 top_cpuset.mems_allowed = node_online_map;
1488 * cpuset_fork - attach newly forked task to its parents cpuset.
1489 * @tsk: pointer to task_struct of forking parent process.
1491 * Description: By default, on fork, a task inherits its
1492 * parent's cpuset. The pointer to the shared cpuset is
1493 * automatically copied in fork.c by dup_task_struct().
1494 * This cpuset_fork() routine need only increment the usage
1495 * counter in that cpuset.
1498 void cpuset_fork(struct task_struct *tsk)
1500 atomic_inc(&tsk->cpuset->count);
1504 * cpuset_exit - detach cpuset from exiting task
1505 * @tsk: pointer to task_struct of exiting process
1507 * Description: Detach cpuset from @tsk and release it.
1509 * Note that cpusets marked notify_on_release force every task
1510 * in them to take the global cpuset_sem semaphore when exiting.
1511 * This could impact scaling on very large systems. Be reluctant
1512 * to use notify_on_release cpusets where very high task exit
1513 * scaling is required on large systems.
1515 * Don't even think about derefencing 'cs' after the cpuset use
1516 * count goes to zero, except inside a critical section guarded
1517 * by the cpuset_sem semaphore. If you don't hold cpuset_sem,
1518 * then a zero cpuset use count is a license to any other task to
1519 * nuke the cpuset immediately.
1522 void cpuset_exit(struct task_struct *tsk)
1531 if (notify_on_release(cs)) {
1532 char *pathbuf = NULL;
1534 cpuset_down(&cpuset_sem);
1535 if (atomic_dec_and_test(&cs->count))
1536 check_for_release(cs, &pathbuf);
1537 cpuset_up(&cpuset_sem);
1538 cpuset_release_agent(pathbuf);
1540 atomic_dec(&cs->count);
1545 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1546 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
1548 * Description: Returns the cpumask_t cpus_allowed of the cpuset
1549 * attached to the specified @tsk. Guaranteed to return some non-empty
1550 * subset of cpu_online_map, even if this means going outside the
1554 cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
1558 cpuset_down(&cpuset_sem);
1559 task_lock((struct task_struct *)tsk);
1560 guarantee_online_cpus(tsk->cpuset, &mask);
1561 task_unlock((struct task_struct *)tsk);
1562 cpuset_up(&cpuset_sem);
1567 void cpuset_init_current_mems_allowed(void)
1569 current->mems_allowed = NODE_MASK_ALL;
1573 * cpuset_update_current_mems_allowed - update mems parameters to new values
1575 * If the current tasks cpusets mems_allowed changed behind our backs,
1576 * update current->mems_allowed and mems_generation to the new value.
1577 * Do not call this routine if in_interrupt().
1580 void cpuset_update_current_mems_allowed(void)
1582 struct cpuset *cs = current->cpuset;
1585 return; /* task is exiting */
1586 if (current->cpuset_mems_generation != cs->mems_generation) {
1587 cpuset_down(&cpuset_sem);
1589 cpuset_up(&cpuset_sem);
1594 * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
1595 * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
1597 void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
1599 bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
1604 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
1605 * @zl: the zonelist to be checked
1607 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1609 int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
1613 for (i = 0; zl->zones[i]; i++) {
1614 int nid = zl->zones[i]->zone_pgdat->node_id;
1616 if (node_isset(nid, current->mems_allowed))
1623 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
1624 * ancestor to the specified cpuset. Call while holding cpuset_sem.
1625 * If no ancestor is mem_exclusive (an unusual configuration), then
1626 * returns the root cpuset.
1628 static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
1630 while (!is_mem_exclusive(cs) && cs->parent)
1636 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
1637 * @z: is this zone on an allowed node?
1638 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
1640 * If we're in interrupt, yes, we can always allocate. If zone
1641 * z's node is in our tasks mems_allowed, yes. If it's not a
1642 * __GFP_HARDWALL request and this zone's nodes is in the nearest
1643 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
1646 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
1647 * and do not allow allocations outside the current tasks cpuset.
1648 * GFP_KERNEL allocations are not so marked, so can escape to the
1649 * nearest mem_exclusive ancestor cpuset.
1651 * Scanning up parent cpusets requires cpuset_sem. The __alloc_pages()
1652 * routine only calls here with __GFP_HARDWALL bit _not_ set if
1653 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
1654 * mems_allowed came up empty on the first pass over the zonelist.
1655 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
1656 * short of memory, might require taking the cpuset_sem semaphore.
1658 * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
1659 * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
1660 * hardwall cpusets - no allocation on a node outside the cpuset is
1661 * allowed (unless in interrupt, of course).
1663 * The second loop doesn't even call here for GFP_ATOMIC requests
1664 * (if the __alloc_pages() local variable 'wait' is set). That check
1665 * and the checks below have the combined affect in the second loop of
1666 * the __alloc_pages() routine that:
1667 * in_interrupt - any node ok (current task context irrelevant)
1668 * GFP_ATOMIC - any node ok
1669 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
1670 * GFP_USER - only nodes in current tasks mems allowed ok.
1673 int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
1675 int node; /* node that zone z is on */
1676 const struct cpuset *cs; /* current cpuset ancestors */
1677 int allowed = 1; /* is allocation in zone z allowed? */
1681 node = z->zone_pgdat->node_id;
1682 if (node_isset(node, current->mems_allowed))
1684 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
1687 /* Not hardwall and node outside mems_allowed: scan up cpusets */
1688 cpuset_down(&cpuset_sem);
1689 cs = current->cpuset;
1691 goto done; /* current task exiting */
1692 cs = nearest_exclusive_ancestor(cs);
1693 allowed = node_isset(node, cs->mems_allowed);
1695 cpuset_up(&cpuset_sem);
1700 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
1701 * @p: pointer to task_struct of some other task.
1703 * Description: Return true if the nearest mem_exclusive ancestor
1704 * cpusets of tasks @p and current overlap. Used by oom killer to
1705 * determine if task @p's memory usage might impact the memory
1706 * available to the current task.
1708 * Acquires cpuset_sem - not suitable for calling from a fast path.
1711 int cpuset_excl_nodes_overlap(const struct task_struct *p)
1713 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
1714 int overlap = 0; /* do cpusets overlap? */
1716 cpuset_down(&cpuset_sem);
1717 cs1 = current->cpuset;
1719 goto done; /* current task exiting */
1722 goto done; /* task p is exiting */
1723 cs1 = nearest_exclusive_ancestor(cs1);
1724 cs2 = nearest_exclusive_ancestor(cs2);
1725 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
1727 cpuset_up(&cpuset_sem);
1733 * proc_cpuset_show()
1734 * - Print tasks cpuset path into seq_file.
1735 * - Used for /proc/<pid>/cpuset.
1738 static int proc_cpuset_show(struct seq_file *m, void *v)
1741 struct task_struct *tsk;
1745 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1750 cpuset_down(&cpuset_sem);
1759 retval = cpuset_path(cs, buf, PAGE_SIZE);
1765 cpuset_up(&cpuset_sem);
1770 static int cpuset_open(struct inode *inode, struct file *file)
1772 struct task_struct *tsk = PROC_I(inode)->task;
1773 return single_open(file, proc_cpuset_show, tsk);
1776 struct file_operations proc_cpuset_operations = {
1777 .open = cpuset_open,
1779 .llseek = seq_lseek,
1780 .release = single_release,
1783 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
1784 char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
1786 buffer += sprintf(buffer, "Cpus_allowed:\t");
1787 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
1788 buffer += sprintf(buffer, "\n");
1789 buffer += sprintf(buffer, "Mems_allowed:\t");
1790 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
1791 buffer += sprintf(buffer, "\n");