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 */
64 * Count is atomic so can incr (fork) or decr (exit) without a lock.
66 atomic_t count; /* count tasks using this cpuset */
69 * We link our 'sibling' struct into our parents 'children'.
70 * Our children link their 'sibling' into our 'children'.
72 struct list_head sibling; /* my parents children */
73 struct list_head children; /* my children */
75 struct cpuset *parent; /* my parent */
76 struct dentry *dentry; /* cpuset fs entry */
79 * Copy of global cpuset_mems_generation as of the most
80 * recent time this cpuset changed its mems_allowed.
85 /* bits in struct cpuset flags field */
93 /* convenient tests for these bits */
94 static inline int is_cpu_exclusive(const struct cpuset *cs)
96 return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
99 static inline int is_mem_exclusive(const struct cpuset *cs)
101 return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
104 static inline int is_removed(const struct cpuset *cs)
106 return !!test_bit(CS_REMOVED, &cs->flags);
109 static inline int notify_on_release(const struct cpuset *cs)
111 return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
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.
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.
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.
130 static atomic_t cpuset_mems_generation = ATOMIC_INIT(1);
132 static 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),
141 .mems_generation = 0,
144 static struct vfsmount *cpuset_mount;
145 static struct super_block *cpuset_sb = NULL;
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.
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.
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
168 * If a task is only holding callback_sem, then it has read-only
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.
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.
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.
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.
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.
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
205 * is taken, and if the cpuset count is zero, a usermode call made
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.
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.
216 * The above "Tale of Two Semaphores" would be complete, but for:
218 * The task_lock() exception
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
231 static DECLARE_MUTEX(manage_sem);
232 static DECLARE_MUTEX(callback_sem);
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.
240 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
241 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
243 static 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,
248 static struct inode *cpuset_new_inode(mode_t mode)
250 struct inode *inode = new_inode(cpuset_sb);
253 inode->i_mode = mode;
254 inode->i_uid = current->fsuid;
255 inode->i_gid = current->fsgid;
256 inode->i_blksize = PAGE_CACHE_SIZE;
258 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
259 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
264 static void cpuset_diput(struct dentry *dentry, struct inode *inode)
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)));
275 static struct dentry_operations cpuset_dops = {
276 .d_iput = cpuset_diput,
279 static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
281 struct dentry *d = lookup_one_len(name, parent, strlen(name));
283 d->d_op = &cpuset_dops;
287 static void remove_dir(struct dentry *d)
289 struct dentry *parent = dget(d->d_parent);
292 simple_rmdir(parent->d_inode, d);
297 * NOTE : the dentry must have been dget()'ed
299 static void cpuset_d_remove_dir(struct dentry *dentry)
301 struct list_head *node;
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);
310 spin_unlock(&dcache_lock);
312 simple_unlink(dentry->d_inode, d);
314 spin_lock(&dcache_lock);
316 node = dentry->d_subdirs.next;
318 list_del_init(&dentry->d_child);
319 spin_unlock(&dcache_lock);
323 static struct super_operations cpuset_ops = {
324 .statfs = simple_statfs,
325 .drop_inode = generic_delete_inode,
328 static int cpuset_fill_super(struct super_block *sb, void *unused_data,
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;
340 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
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) */
350 root = d_alloc_root(inode);
359 static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
360 int flags, const char *unused_dev_name,
363 return get_sb_single(fs_type, flags, data, cpuset_fill_super);
366 static struct file_system_type cpuset_fs_type = {
368 .get_sb = cpuset_get_sb,
369 .kill_sb = kill_litter_super,
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
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
388 int (*open) (struct inode *inode, struct file *file);
389 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
391 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
393 int (*release) (struct inode *inode, struct file *file);
396 static inline struct cpuset *__d_cs(struct dentry *dentry)
398 return dentry->d_fsdata;
401 static inline struct cftype *__d_cft(struct dentry *dentry)
403 return dentry->d_fsdata;
407 * Call with manage_sem held. Writes path of cpuset into buf.
408 * Returns 0 on success, -errno on error.
411 static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
415 start = buf + buflen;
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);
429 return -ENAMETOOLONG;
432 memmove(buf, start, buf + buflen - start);
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.
441 * Most likely, this user command will try to rmdir this cpuset.
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.
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.
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.
466 static void cpuset_release_agent(const char *pathbuf)
468 char *argv[3], *envp[3];
475 argv[i++] = "/sbin/cpuset_release_agent";
476 argv[i++] = (char *)pathbuf;
480 /* minimal command environment */
481 envp[i++] = "HOME=/";
482 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
485 call_usermodehelper(argv[0], argv, envp, 0);
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
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
495 * cpuset_release_agent() with it later on, once manage_sem is dropped.
496 * Call here with manage_sem held.
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
502 * calling check_for_release() with manage_sem held and the address
503 * of the pathbuf pointer, then dropping manage_sem, then calling
504 * cpuset_release_agent() with pathbuf, as set by check_for_release().
507 static void check_for_release(struct cpuset *cs, char **ppathbuf)
509 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
510 list_empty(&cs->children)) {
513 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
516 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
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.
531 * One way or another, we guarantee to return some non-empty subset
534 * Call with callback_sem held.
537 static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
539 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
542 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
544 *pmask = cpu_online_map;
545 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
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.
555 * One way or another, we guarantee to return some non-empty subset
556 * of node_online_map.
558 * Call with callback_sem held.
561 static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
563 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
566 nodes_and(*pmask, cs->mems_allowed, node_online_map);
568 *pmask = node_online_map;
569 BUG_ON(!nodes_intersects(*pmask, node_online_map));
573 * Refresh current tasks mems_allowed and mems_generation from current
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.
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.
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.
593 static void refresh_mems(void)
595 int my_cpusets_mem_gen;
598 my_cpusets_mem_gen = current->cpuset->mems_generation;
599 task_unlock(current);
601 if (current->cpuset_mems_generation != my_cpusets_mem_gen) {
606 cs = current->cpuset;
607 guarantee_online_mems(cs, ¤t->mems_allowed);
608 current->cpuset_mems_generation = cs->mems_generation;
609 task_unlock(current);
615 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
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
619 * are only set if the other's are set. Call holding manage_sem.
622 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
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);
631 * validate_change() - Used to validate that any proposed cpuset change
632 * follows the structural rules for cpusets.
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
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.
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.
647 * Return 0 if valid, -errno if not.
650 static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
652 struct cpuset *c, *par;
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))
660 /* Remaining checks don't apply to root cpuset */
661 if ((par = cur->parent) == NULL)
664 /* We must be a subset of our parent cpuset */
665 if (!is_cpuset_subset(trial, par))
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)) &&
672 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
674 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
676 nodes_intersects(trial->mems_allowed, c->mems_allowed))
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
691 * Call with manage_sem held. May nest a call to the
692 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
695 static void update_cpu_domains(struct cpuset *cur)
697 struct cpuset *c, *par = cur->parent;
698 cpumask_t pspan, cspan;
700 if (par == NULL || cpus_empty(cur->cpus_allowed))
704 * Get all cpus from parent's cpus_allowed not part of exclusive
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);
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))
716 cspan = CPU_MASK_NONE;
718 if (cpus_empty(pspan))
720 cspan = cur->cpus_allowed;
722 * Get all cpus from current cpuset's cpus_allowed not part
723 * of exclusive children
725 list_for_each_entry(c, &cur->children, sibling) {
726 if (is_cpu_exclusive(c))
727 cpus_andnot(cspan, cspan, c->cpus_allowed);
732 partition_sched_domains(&pspan, &cspan);
733 unlock_cpu_hotplug();
737 * Call with manage_sem held. May take callback_sem during call.
740 static int update_cpumask(struct cpuset *cs, char *buf)
742 struct cpuset trialcs;
743 int retval, cpus_unchanged;
746 retval = cpulist_parse(buf, trialcs.cpus_allowed);
749 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
750 if (cpus_empty(trialcs.cpus_allowed))
752 retval = validate_change(cs, &trialcs);
755 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
757 cs->cpus_allowed = trialcs.cpus_allowed;
759 if (is_cpu_exclusive(cs) && !cpus_unchanged)
760 update_cpu_domains(cs);
765 * Call with manage_sem held. May take callback_sem during call.
768 static int update_nodemask(struct cpuset *cs, char *buf)
770 struct cpuset trialcs;
774 retval = nodelist_parse(buf, trialcs.mems_allowed);
777 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
778 if (nodes_empty(trialcs.mems_allowed))
780 retval = validate_change(cs, &trialcs);
783 cs->mems_allowed = trialcs.mems_allowed;
784 atomic_inc(&cpuset_mems_generation);
785 cs->mems_generation = atomic_read(&cpuset_mems_generation);
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
798 * Call with manage_sem held.
801 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
804 struct cpuset trialcs;
805 int err, cpu_exclusive_changed;
807 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
811 set_bit(bit, &trialcs.flags);
813 clear_bit(bit, &trialcs.flags);
815 err = validate_change(cs, &trialcs);
818 cpu_exclusive_changed =
819 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
822 set_bit(bit, &cs->flags);
824 clear_bit(bit, &cs->flags);
827 if (cpu_exclusive_changed)
828 update_cpu_domains(cs);
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.
837 * Call holding manage_sem. May take callback_sem and task_lock of
838 * the task 'pid' during call.
841 static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
844 struct task_struct *tsk;
845 struct cpuset *oldcs;
848 if (sscanf(pidbuf, "%d", &pid) != 1)
850 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
854 read_lock(&tasklist_lock);
856 tsk = find_task_by_pid(pid);
857 if (!tsk || tsk->flags & PF_EXITING) {
858 read_unlock(&tasklist_lock);
862 get_task_struct(tsk);
863 read_unlock(&tasklist_lock);
865 if ((current->euid) && (current->euid != tsk->uid)
866 && (current->euid != tsk->suid)) {
867 put_task_struct(tsk);
872 get_task_struct(tsk);
882 put_task_struct(tsk);
885 atomic_inc(&cs->count);
889 guarantee_online_cpus(cs, &cpus);
890 set_cpus_allowed(tsk, cpus);
893 put_task_struct(tsk);
894 if (atomic_dec_and_test(&oldcs->count))
895 check_for_release(oldcs, ppathbuf);
899 /* The various types of files and directories in a cpuset file system */
908 FILE_NOTIFY_ON_RELEASE,
912 static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
913 size_t nbytes, loff_t *unused_ppos)
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;
919 char *pathbuf = NULL;
922 /* Crude upper limit on largest legitimate cpulist user might write. */
923 if (nbytes > 100 + 6 * NR_CPUS)
926 /* +1 for nul-terminator */
927 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
930 if (copy_from_user(buffer, userbuf, nbytes)) {
934 buffer[nbytes] = 0; /* nul-terminate */
938 if (is_removed(cs)) {
945 retval = update_cpumask(cs, buffer);
948 retval = update_nodemask(cs, buffer);
950 case FILE_CPU_EXCLUSIVE:
951 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
953 case FILE_MEM_EXCLUSIVE:
954 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
956 case FILE_NOTIFY_ON_RELEASE:
957 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
960 retval = attach_task(cs, buffer, &pathbuf);
971 cpuset_release_agent(pathbuf);
977 static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
978 size_t nbytes, loff_t *ppos)
981 struct cftype *cft = __d_cft(file->f_dentry);
985 /* special function ? */
987 retval = cft->write(file, buf, nbytes, ppos);
989 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
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.
1006 static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1010 down(&callback_sem);
1011 mask = cs->cpus_allowed;
1014 return cpulist_scnprintf(page, PAGE_SIZE, mask);
1017 static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1021 down(&callback_sem);
1022 mask = cs->mems_allowed;
1025 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1028 static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
1029 size_t nbytes, loff_t *ppos)
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;
1038 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1045 s += cpuset_sprintf_cpulist(s, cs);
1048 s += cpuset_sprintf_memlist(s, cs);
1050 case FILE_CPU_EXCLUSIVE:
1051 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
1053 case FILE_MEM_EXCLUSIVE:
1054 *s++ = is_mem_exclusive(cs) ? '1' : '0';
1056 case FILE_NOTIFY_ON_RELEASE:
1057 *s++ = notify_on_release(cs) ? '1' : '0';
1065 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1067 free_page((unsigned long)page);
1071 static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1075 struct cftype *cft = __d_cft(file->f_dentry);
1079 /* special function ? */
1081 retval = cft->read(file, buf, nbytes, ppos);
1083 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1088 static int cpuset_file_open(struct inode *inode, struct file *file)
1093 err = generic_file_open(inode, file);
1097 cft = __d_cft(file->f_dentry);
1101 err = cft->open(inode, file);
1108 static int cpuset_file_release(struct inode *inode, struct file *file)
1110 struct cftype *cft = __d_cft(file->f_dentry);
1112 return cft->release(inode, file);
1116 static 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,
1124 static struct inode_operations cpuset_dir_inode_operations = {
1125 .lookup = simple_lookup,
1126 .mkdir = cpuset_mkdir,
1127 .rmdir = cpuset_rmdir,
1130 static int cpuset_create_file(struct dentry *dentry, int mode)
1132 struct inode *inode;
1136 if (dentry->d_inode)
1139 inode = cpuset_new_inode(mode);
1143 if (S_ISDIR(mode)) {
1144 inode->i_op = &cpuset_dir_inode_operations;
1145 inode->i_fop = &simple_dir_operations;
1147 /* start off with i_nlink == 2 (for "." entry) */
1149 } else if (S_ISREG(mode)) {
1151 inode->i_fop = &cpuset_file_operations;
1154 d_instantiate(dentry, inode);
1155 dget(dentry); /* Extra count - pin the dentry in core */
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.
1168 static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1170 struct dentry *dentry = NULL;
1171 struct dentry *parent;
1174 parent = cs->parent->dentry;
1175 dentry = cpuset_get_dentry(parent, name);
1177 return PTR_ERR(dentry);
1178 error = cpuset_create_file(dentry, S_IFDIR | mode);
1180 dentry->d_fsdata = cs;
1181 parent->d_inode->i_nlink++;
1182 cs->dentry = dentry;
1189 static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1191 struct dentry *dentry;
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);
1199 dentry->d_fsdata = (void *)cft;
1202 error = PTR_ERR(dentry);
1203 up(&dir->d_inode->i_sem);
1208 * Stuff for reading the 'tasks' file.
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.
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().
1222 /* cpusets_tasks_read array */
1230 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
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.
1235 static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1238 struct task_struct *g, *p;
1240 read_lock(&tasklist_lock);
1242 do_each_thread(g, p) {
1243 if (p->cpuset == cs) {
1244 pidarray[n++] = p->pid;
1245 if (unlikely(n == npids))
1248 } while_each_thread(g, p);
1251 read_unlock(&tasklist_lock);
1255 static int cmppid(const void *a, const void *b)
1257 return *(pid_t *)a - *(pid_t *)b;
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.
1265 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1270 for (i = 0; i < npids; i++)
1271 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
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.
1279 * Does not require any specific cpuset semaphores, and does not take any.
1281 static int cpuset_tasks_open(struct inode *unused, struct file *file)
1283 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1284 struct ctr_struct *ctr;
1289 if (!(file->f_mode & FMODE_READ))
1292 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
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.
1302 npids = atomic_read(&cs->count);
1303 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1307 npids = pid_array_load(pidarray, npids, cs);
1308 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
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);
1315 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1318 file->private_data = ctr;
1329 static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1330 size_t nbytes, loff_t *ppos)
1332 struct ctr_struct *ctr = file->private_data;
1334 if (*ppos + nbytes > ctr->bufsz)
1335 nbytes = ctr->bufsz - *ppos;
1336 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1342 static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1344 struct ctr_struct *ctr;
1346 if (file->f_mode & FMODE_READ) {
1347 ctr = file->private_data;
1355 * for the common functions, 'private' gives the type of file
1358 static struct cftype cft_tasks = {
1360 .open = cpuset_tasks_open,
1361 .read = cpuset_tasks_read,
1362 .release = cpuset_tasks_release,
1363 .private = FILE_TASKLIST,
1366 static struct cftype cft_cpus = {
1368 .private = FILE_CPULIST,
1371 static struct cftype cft_mems = {
1373 .private = FILE_MEMLIST,
1376 static struct cftype cft_cpu_exclusive = {
1377 .name = "cpu_exclusive",
1378 .private = FILE_CPU_EXCLUSIVE,
1381 static struct cftype cft_mem_exclusive = {
1382 .name = "mem_exclusive",
1383 .private = FILE_MEM_EXCLUSIVE,
1386 static struct cftype cft_notify_on_release = {
1387 .name = "notify_on_release",
1388 .private = FILE_NOTIFY_ON_RELEASE,
1391 static int cpuset_populate_dir(struct dentry *cs_dentry)
1395 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1397 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1399 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1401 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1403 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1405 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
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
1416 * Must be called with the semaphore on the parent inode held
1419 static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1424 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
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);
1441 cs->parent = parent;
1443 down(&callback_sem);
1444 list_add(&cs->sibling, &cs->parent->children);
1447 err = cpuset_create_dir(cs, name, mode);
1452 * Release manage_sem before cpuset_populate_dir() because it
1453 * will down() this new directory's i_sem and if we race with
1454 * another mkdir, we might deadlock.
1458 err = cpuset_populate_dir(cs->dentry);
1459 /* If err < 0, we have a half-filled directory - oh well ;) */
1462 list_del(&cs->sibling);
1468 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1470 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1472 /* the vfs holds inode->i_sem already */
1473 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1476 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1478 struct cpuset *cs = dentry->d_fsdata;
1480 struct cpuset *parent;
1481 char *pathbuf = NULL;
1483 /* the vfs holds both inode->i_sem already */
1487 if (atomic_read(&cs->count) > 0) {
1491 if (!list_empty(&cs->children)) {
1495 parent = cs->parent;
1496 down(&callback_sem);
1497 set_bit(CS_REMOVED, &cs->flags);
1498 if (is_cpu_exclusive(cs))
1499 update_cpu_domains(cs);
1500 list_del(&cs->sibling); /* delete my sibling from parent->children */
1501 spin_lock(&cs->dentry->d_lock);
1502 d = dget(cs->dentry);
1504 spin_unlock(&d->d_lock);
1505 cpuset_d_remove_dir(d);
1508 if (list_empty(&parent->children))
1509 check_for_release(parent, &pathbuf);
1511 cpuset_release_agent(pathbuf);
1516 * cpuset_init - initialize cpusets at system boot
1518 * Description: Initialize top_cpuset and the cpuset internal file system,
1521 int __init cpuset_init(void)
1523 struct dentry *root;
1526 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1527 top_cpuset.mems_allowed = NODE_MASK_ALL;
1529 atomic_inc(&cpuset_mems_generation);
1530 top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
1532 init_task.cpuset = &top_cpuset;
1534 err = register_filesystem(&cpuset_fs_type);
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;
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);
1555 * cpuset_init_smp - initialize cpus_allowed
1557 * Description: Finish top cpuset after cpu, node maps are initialized
1560 void __init cpuset_init_smp(void)
1562 top_cpuset.cpus_allowed = cpu_online_map;
1563 top_cpuset.mems_allowed = node_online_map;
1567 * cpuset_fork - attach newly forked task to its parents cpuset.
1568 * @tsk: pointer to task_struct of forking parent process.
1570 * Description: A task inherits its parent's cpuset at fork().
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.
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.
1584 void cpuset_fork(struct task_struct *child)
1587 child->cpuset = current->cpuset;
1588 atomic_inc(&child->cpuset->count);
1589 task_unlock(current);
1593 * cpuset_exit - detach cpuset from exiting task
1594 * @tsk: pointer to task_struct of exiting process
1596 * Description: Detach cpuset from @tsk and release it.
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.
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().
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().
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
1618 void cpuset_exit(struct task_struct *tsk)
1622 BUG_ON(!(tsk->flags & PF_EXITING));
1627 if (notify_on_release(cs)) {
1628 char *pathbuf = NULL;
1631 if (atomic_dec_and_test(&cs->count))
1632 check_for_release(cs, &pathbuf);
1634 cpuset_release_agent(pathbuf);
1636 atomic_dec(&cs->count);
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.
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
1650 cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
1654 down(&callback_sem);
1655 task_lock((struct task_struct *)tsk);
1656 guarantee_online_cpus(tsk->cpuset, &mask);
1657 task_unlock((struct task_struct *)tsk);
1663 void cpuset_init_current_mems_allowed(void)
1665 current->mems_allowed = NODE_MASK_ALL;
1669 * cpuset_update_current_mems_allowed - update mems parameters to new values
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().
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
1681 void cpuset_update_current_mems_allowed(void)
1684 int need_to_refresh = 0;
1687 cs = current->cpuset;
1690 if (current->cpuset_mems_generation != cs->mems_generation)
1691 need_to_refresh = 1;
1693 task_unlock(current);
1694 if (need_to_refresh)
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
1702 void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
1704 bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
1709 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
1710 * @zl: the zonelist to be checked
1712 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1714 int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
1718 for (i = 0; zl->zones[i]; i++) {
1719 int nid = zl->zones[i]->zone_pgdat->node_id;
1721 if (node_isset(nid, current->mems_allowed))
1728 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
1729 * ancestor to the specified cpuset. Call holding callback_sem.
1730 * If no ancestor is mem_exclusive (an unusual configuration), then
1731 * returns the root cpuset.
1733 static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
1735 while (!is_mem_exclusive(cs) && cs->parent)
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)
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.
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.
1756 * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
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
1761 * short of memory, might require taking the callback_sem semaphore.
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).
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.
1778 int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
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? */
1786 node = z->zone_pgdat->node_id;
1787 if (node_isset(node, current->mems_allowed))
1789 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
1792 /* Not hardwall and node outside mems_allowed: scan up cpusets */
1793 down(&callback_sem);
1795 if (current->flags & PF_EXITING) /* Let dying task have memory */
1798 cs = nearest_exclusive_ancestor(current->cpuset);
1799 task_unlock(current);
1801 allowed = node_isset(node, cs->mems_allowed);
1807 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
1808 * @p: pointer to task_struct of some other task.
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.
1815 * Acquires callback_sem - not suitable for calling from a fast path.
1818 int cpuset_excl_nodes_overlap(const struct task_struct *p)
1820 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
1821 int overlap = 0; /* do cpusets overlap? */
1823 down(&callback_sem);
1826 if (current->flags & PF_EXITING) {
1827 task_unlock(current);
1830 cs1 = nearest_exclusive_ancestor(current->cpuset);
1831 task_unlock(current);
1833 task_lock((struct task_struct *)p);
1834 if (p->flags & PF_EXITING) {
1835 task_unlock((struct task_struct *)p);
1838 cs2 = nearest_exclusive_ancestor(p->cpuset);
1839 task_unlock((struct task_struct *)p);
1841 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
1849 * proc_cpuset_show()
1850 * - Print tasks cpuset path into seq_file.
1851 * - Used for /proc/<pid>/cpuset.
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
1858 static int proc_cpuset_show(struct seq_file *m, void *v)
1861 struct task_struct *tsk;
1865 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1877 retval = cpuset_path(cs, buf, PAGE_SIZE);
1888 static int cpuset_open(struct inode *inode, struct file *file)
1890 struct task_struct *tsk = PROC_I(inode)->task;
1891 return single_open(file, proc_cpuset_show, tsk);
1894 struct file_operations proc_cpuset_operations = {
1895 .open = cpuset_open,
1897 .llseek = seq_lseek,
1898 .release = single_release,
1901 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
1902 char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
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");