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 * if the last task using a cpuset exits, and the cpuset was marked
170 * notify_on_release. In that case, the cpuset_sem is taken, the
171 * path to the released cpuset calculated, and a usermode call made
172 * to /sbin/cpuset_release_agent with the name of the cpuset (path
173 * relative to the root of cpuset file system) as the argument.
175 * A cpuset can only be deleted if both its 'count' of using tasks is
176 * zero, and its list of 'children' cpusets is empty. Since all tasks
177 * in the system use _some_ cpuset, and since there is always at least
178 * one task in the system (init, pid == 1), therefore, top_cpuset
179 * always has either children cpusets and/or using tasks. So no need
180 * for any special hack to ensure that top_cpuset cannot be deleted.
183 static DECLARE_MUTEX(cpuset_sem
);
186 * A couple of forward declarations required, due to cyclic reference loop:
187 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
188 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
191 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
192 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
194 static struct backing_dev_info cpuset_backing_dev_info
= {
195 .ra_pages
= 0, /* No readahead */
196 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
199 static struct inode
*cpuset_new_inode(mode_t mode
)
201 struct inode
*inode
= new_inode(cpuset_sb
);
204 inode
->i_mode
= mode
;
205 inode
->i_uid
= current
->fsuid
;
206 inode
->i_gid
= current
->fsgid
;
207 inode
->i_blksize
= PAGE_CACHE_SIZE
;
209 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
210 inode
->i_mapping
->backing_dev_info
= &cpuset_backing_dev_info
;
215 static void cpuset_diput(struct dentry
*dentry
, struct inode
*inode
)
217 /* is dentry a directory ? if so, kfree() associated cpuset */
218 if (S_ISDIR(inode
->i_mode
)) {
219 struct cpuset
*cs
= dentry
->d_fsdata
;
220 BUG_ON(!(is_removed(cs
)));
226 static struct dentry_operations cpuset_dops
= {
227 .d_iput
= cpuset_diput
,
230 static struct dentry
*cpuset_get_dentry(struct dentry
*parent
, const char *name
)
236 qstr
.len
= strlen(name
);
237 qstr
.hash
= full_name_hash(name
, qstr
.len
);
238 d
= lookup_hash(&qstr
, parent
);
240 d
->d_op
= &cpuset_dops
;
244 static void remove_dir(struct dentry
*d
)
246 struct dentry
*parent
= dget(d
->d_parent
);
249 simple_rmdir(parent
->d_inode
, d
);
254 * NOTE : the dentry must have been dget()'ed
256 static void cpuset_d_remove_dir(struct dentry
*dentry
)
258 struct list_head
*node
;
260 spin_lock(&dcache_lock
);
261 node
= dentry
->d_subdirs
.next
;
262 while (node
!= &dentry
->d_subdirs
) {
263 struct dentry
*d
= list_entry(node
, struct dentry
, d_child
);
267 spin_unlock(&dcache_lock
);
269 simple_unlink(dentry
->d_inode
, d
);
271 spin_lock(&dcache_lock
);
273 node
= dentry
->d_subdirs
.next
;
275 list_del_init(&dentry
->d_child
);
276 spin_unlock(&dcache_lock
);
280 static struct super_operations cpuset_ops
= {
281 .statfs
= simple_statfs
,
282 .drop_inode
= generic_delete_inode
,
285 static int cpuset_fill_super(struct super_block
*sb
, void *unused_data
,
291 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
292 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
293 sb
->s_magic
= CPUSET_SUPER_MAGIC
;
294 sb
->s_op
= &cpuset_ops
;
297 inode
= cpuset_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
);
299 inode
->i_op
= &simple_dir_inode_operations
;
300 inode
->i_fop
= &simple_dir_operations
;
301 /* directories start off with i_nlink == 2 (for "." entry) */
307 root
= d_alloc_root(inode
);
316 static struct super_block
*cpuset_get_sb(struct file_system_type
*fs_type
,
317 int flags
, const char *unused_dev_name
,
320 return get_sb_single(fs_type
, flags
, data
, cpuset_fill_super
);
323 static struct file_system_type cpuset_fs_type
= {
325 .get_sb
= cpuset_get_sb
,
326 .kill_sb
= kill_litter_super
,
331 * The files in the cpuset filesystem mostly have a very simple read/write
332 * handling, some common function will take care of it. Nevertheless some cases
333 * (read tasks) are special and therefore I define this structure for every
337 * When reading/writing to a file:
338 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
339 * - the 'cftype' of the file is file->f_dentry->d_fsdata
345 int (*open
) (struct inode
*inode
, struct file
*file
);
346 ssize_t (*read
) (struct file
*file
, char __user
*buf
, size_t nbytes
,
348 int (*write
) (struct file
*file
, const char __user
*buf
, size_t nbytes
,
350 int (*release
) (struct inode
*inode
, struct file
*file
);
353 static inline struct cpuset
*__d_cs(struct dentry
*dentry
)
355 return dentry
->d_fsdata
;
358 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
360 return dentry
->d_fsdata
;
364 * Call with cpuset_sem held. Writes path of cpuset into buf.
365 * Returns 0 on success, -errno on error.
368 static int cpuset_path(const struct cpuset
*cs
, char *buf
, int buflen
)
372 start
= buf
+ buflen
;
376 int len
= cs
->dentry
->d_name
.len
;
377 if ((start
-= len
) < buf
)
378 return -ENAMETOOLONG
;
379 memcpy(start
, cs
->dentry
->d_name
.name
, len
);
386 return -ENAMETOOLONG
;
389 memmove(buf
, start
, buf
+ buflen
- start
);
394 * Notify userspace when a cpuset is released, by running
395 * /sbin/cpuset_release_agent with the name of the cpuset (path
396 * relative to the root of cpuset file system) as the argument.
398 * Most likely, this user command will try to rmdir this cpuset.
400 * This races with the possibility that some other task will be
401 * attached to this cpuset before it is removed, or that some other
402 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
403 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
404 * unused, and this cpuset will be reprieved from its death sentence,
405 * to continue to serve a useful existence. Next time it's released,
406 * we will get notified again, if it still has 'notify_on_release' set.
408 * Note final arg to call_usermodehelper() is 0 - that means
409 * don't wait. Since we are holding the global cpuset_sem here,
410 * and we are asking another thread (started from keventd) to rmdir a
411 * cpuset, we can't wait - or we'd deadlock with the removing thread
415 static int cpuset_release_agent(char *cpuset_str
)
417 char *argv
[3], *envp
[3];
421 argv
[i
++] = "/sbin/cpuset_release_agent";
422 argv
[i
++] = cpuset_str
;
426 /* minimal command environment */
427 envp
[i
++] = "HOME=/";
428 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
431 return call_usermodehelper(argv
[0], argv
, envp
, 0);
435 * Either cs->count of using tasks transitioned to zero, or the
436 * cs->children list of child cpusets just became empty. If this
437 * cs is notify_on_release() and now both the user count is zero and
438 * the list of children is empty, send notice to user land.
441 static void check_for_release(struct cpuset
*cs
)
443 if (notify_on_release(cs
) && atomic_read(&cs
->count
) == 0 &&
444 list_empty(&cs
->children
)) {
447 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
450 if (cpuset_path(cs
, buf
, PAGE_SIZE
) < 0)
452 cpuset_release_agent(buf
);
459 * Return in *pmask the portion of a cpusets's cpus_allowed that
460 * are online. If none are online, walk up the cpuset hierarchy
461 * until we find one that does have some online cpus. If we get
462 * all the way to the top and still haven't found any online cpus,
463 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
464 * task, return cpu_online_map.
466 * One way or another, we guarantee to return some non-empty subset
469 * Call with cpuset_sem held.
472 static void guarantee_online_cpus(const struct cpuset
*cs
, cpumask_t
*pmask
)
474 while (cs
&& !cpus_intersects(cs
->cpus_allowed
, cpu_online_map
))
477 cpus_and(*pmask
, cs
->cpus_allowed
, cpu_online_map
);
479 *pmask
= cpu_online_map
;
480 BUG_ON(!cpus_intersects(*pmask
, cpu_online_map
));
484 * Return in *pmask the portion of a cpusets's mems_allowed that
485 * are online. If none are online, walk up the cpuset hierarchy
486 * until we find one that does have some online mems. If we get
487 * all the way to the top and still haven't found any online mems,
488 * return node_online_map.
490 * One way or another, we guarantee to return some non-empty subset
491 * of node_online_map.
493 * Call with cpuset_sem held.
496 static void guarantee_online_mems(const struct cpuset
*cs
, nodemask_t
*pmask
)
498 while (cs
&& !nodes_intersects(cs
->mems_allowed
, node_online_map
))
501 nodes_and(*pmask
, cs
->mems_allowed
, node_online_map
);
503 *pmask
= node_online_map
;
504 BUG_ON(!nodes_intersects(*pmask
, node_online_map
));
508 * Refresh current tasks mems_allowed and mems_generation from
509 * current tasks cpuset. Call with cpuset_sem held.
511 * Be sure to call refresh_mems() on any cpuset operation which
512 * (1) holds cpuset_sem, and (2) might possibly alloc memory.
513 * Call after obtaining cpuset_sem lock, before any possible
514 * allocation. Otherwise one risks trying to allocate memory
515 * while the task cpuset_mems_generation is not the same as
516 * the mems_generation in its cpuset, which would deadlock on
517 * cpuset_sem in cpuset_update_current_mems_allowed().
519 * Since we hold cpuset_sem, once refresh_mems() is called, the
520 * test (current->cpuset_mems_generation != cs->mems_generation)
521 * in cpuset_update_current_mems_allowed() will remain false,
522 * until we drop cpuset_sem. Anyone else who would change our
523 * cpusets mems_generation needs to lock cpuset_sem first.
526 static void refresh_mems(void)
528 struct cpuset
*cs
= current
->cpuset
;
530 if (current
->cpuset_mems_generation
!= cs
->mems_generation
) {
531 guarantee_online_mems(cs
, ¤t
->mems_allowed
);
532 current
->cpuset_mems_generation
= cs
->mems_generation
;
537 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
539 * One cpuset is a subset of another if all its allowed CPUs and
540 * Memory Nodes are a subset of the other, and its exclusive flags
541 * are only set if the other's are set.
544 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
546 return cpus_subset(p
->cpus_allowed
, q
->cpus_allowed
) &&
547 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
548 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
549 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
553 * validate_change() - Used to validate that any proposed cpuset change
554 * follows the structural rules for cpusets.
556 * If we replaced the flag and mask values of the current cpuset
557 * (cur) with those values in the trial cpuset (trial), would
558 * our various subset and exclusive rules still be valid? Presumes
561 * 'cur' is the address of an actual, in-use cpuset. Operations
562 * such as list traversal that depend on the actual address of the
563 * cpuset in the list must use cur below, not trial.
565 * 'trial' is the address of bulk structure copy of cur, with
566 * perhaps one or more of the fields cpus_allowed, mems_allowed,
567 * or flags changed to new, trial values.
569 * Return 0 if valid, -errno if not.
572 static int validate_change(const struct cpuset
*cur
, const struct cpuset
*trial
)
574 struct cpuset
*c
, *par
;
576 /* Each of our child cpusets must be a subset of us */
577 list_for_each_entry(c
, &cur
->children
, sibling
) {
578 if (!is_cpuset_subset(c
, trial
))
582 /* Remaining checks don't apply to root cpuset */
583 if ((par
= cur
->parent
) == NULL
)
586 /* We must be a subset of our parent cpuset */
587 if (!is_cpuset_subset(trial
, par
))
590 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
591 list_for_each_entry(c
, &par
->children
, sibling
) {
592 if ((is_cpu_exclusive(trial
) || is_cpu_exclusive(c
)) &&
594 cpus_intersects(trial
->cpus_allowed
, c
->cpus_allowed
))
596 if ((is_mem_exclusive(trial
) || is_mem_exclusive(c
)) &&
598 nodes_intersects(trial
->mems_allowed
, c
->mems_allowed
))
605 static int update_cpumask(struct cpuset
*cs
, char *buf
)
607 struct cpuset trialcs
;
611 retval
= cpulist_parse(buf
, trialcs
.cpus_allowed
);
614 cpus_and(trialcs
.cpus_allowed
, trialcs
.cpus_allowed
, cpu_online_map
);
615 if (cpus_empty(trialcs
.cpus_allowed
))
617 retval
= validate_change(cs
, &trialcs
);
619 cs
->cpus_allowed
= trialcs
.cpus_allowed
;
623 static int update_nodemask(struct cpuset
*cs
, char *buf
)
625 struct cpuset trialcs
;
629 retval
= nodelist_parse(buf
, trialcs
.mems_allowed
);
632 nodes_and(trialcs
.mems_allowed
, trialcs
.mems_allowed
, node_online_map
);
633 if (nodes_empty(trialcs
.mems_allowed
))
635 retval
= validate_change(cs
, &trialcs
);
637 cs
->mems_allowed
= trialcs
.mems_allowed
;
638 atomic_inc(&cpuset_mems_generation
);
639 cs
->mems_generation
= atomic_read(&cpuset_mems_generation
);
645 * update_flag - read a 0 or a 1 in a file and update associated flag
646 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
647 * CS_NOTIFY_ON_RELEASE)
648 * cs: the cpuset to update
649 * buf: the buffer where we read the 0 or 1
652 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
, char *buf
)
655 struct cpuset trialcs
;
658 turning_on
= (simple_strtoul(buf
, NULL
, 10) != 0);
662 set_bit(bit
, &trialcs
.flags
);
664 clear_bit(bit
, &trialcs
.flags
);
666 err
= validate_change(cs
, &trialcs
);
669 set_bit(bit
, &cs
->flags
);
671 clear_bit(bit
, &cs
->flags
);
676 static int attach_task(struct cpuset
*cs
, char *buf
)
679 struct task_struct
*tsk
;
680 struct cpuset
*oldcs
;
683 if (sscanf(buf
, "%d", &pid
) != 1)
685 if (cpus_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
))
689 read_lock(&tasklist_lock
);
691 tsk
= find_task_by_pid(pid
);
693 read_unlock(&tasklist_lock
);
697 get_task_struct(tsk
);
698 read_unlock(&tasklist_lock
);
700 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
701 && (current
->euid
!= tsk
->suid
)) {
702 put_task_struct(tsk
);
707 get_task_struct(tsk
);
714 put_task_struct(tsk
);
717 atomic_inc(&cs
->count
);
721 guarantee_online_cpus(cs
, &cpus
);
722 set_cpus_allowed(tsk
, cpus
);
724 put_task_struct(tsk
);
725 if (atomic_dec_and_test(&oldcs
->count
))
726 check_for_release(oldcs
);
730 /* The various types of files and directories in a cpuset file system */
739 FILE_NOTIFY_ON_RELEASE
,
743 static ssize_t
cpuset_common_file_write(struct file
*file
, const char __user
*userbuf
,
744 size_t nbytes
, loff_t
*unused_ppos
)
746 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
747 struct cftype
*cft
= __d_cft(file
->f_dentry
);
748 cpuset_filetype_t type
= cft
->private;
752 /* Crude upper limit on largest legitimate cpulist user might write. */
753 if (nbytes
> 100 + 6 * NR_CPUS
)
756 /* +1 for nul-terminator */
757 if ((buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
)) == 0)
760 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
764 buffer
[nbytes
] = 0; /* nul-terminate */
768 if (is_removed(cs
)) {
775 retval
= update_cpumask(cs
, buffer
);
778 retval
= update_nodemask(cs
, buffer
);
780 case FILE_CPU_EXCLUSIVE
:
781 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, buffer
);
783 case FILE_MEM_EXCLUSIVE
:
784 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, buffer
);
786 case FILE_NOTIFY_ON_RELEASE
:
787 retval
= update_flag(CS_NOTIFY_ON_RELEASE
, cs
, buffer
);
790 retval
= attach_task(cs
, buffer
);
806 static ssize_t
cpuset_file_write(struct file
*file
, const char __user
*buf
,
807 size_t nbytes
, loff_t
*ppos
)
810 struct cftype
*cft
= __d_cft(file
->f_dentry
);
814 /* special function ? */
816 retval
= cft
->write(file
, buf
, nbytes
, ppos
);
818 retval
= cpuset_common_file_write(file
, buf
, nbytes
, ppos
);
824 * These ascii lists should be read in a single call, by using a user
825 * buffer large enough to hold the entire map. If read in smaller
826 * chunks, there is no guarantee of atomicity. Since the display format
827 * used, list of ranges of sequential numbers, is variable length,
828 * and since these maps can change value dynamically, one could read
829 * gibberish by doing partial reads while a list was changing.
830 * A single large read to a buffer that crosses a page boundary is
831 * ok, because the result being copied to user land is not recomputed
832 * across a page fault.
835 static int cpuset_sprintf_cpulist(char *page
, struct cpuset
*cs
)
840 mask
= cs
->cpus_allowed
;
843 return cpulist_scnprintf(page
, PAGE_SIZE
, mask
);
846 static int cpuset_sprintf_memlist(char *page
, struct cpuset
*cs
)
851 mask
= cs
->mems_allowed
;
854 return nodelist_scnprintf(page
, PAGE_SIZE
, mask
);
857 static ssize_t
cpuset_common_file_read(struct file
*file
, char __user
*buf
,
858 size_t nbytes
, loff_t
*ppos
)
860 struct cftype
*cft
= __d_cft(file
->f_dentry
);
861 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
862 cpuset_filetype_t type
= cft
->private;
869 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
876 s
+= cpuset_sprintf_cpulist(s
, cs
);
879 s
+= cpuset_sprintf_memlist(s
, cs
);
881 case FILE_CPU_EXCLUSIVE
:
882 *s
++ = is_cpu_exclusive(cs
) ? '1' : '0';
884 case FILE_MEM_EXCLUSIVE
:
885 *s
++ = is_mem_exclusive(cs
) ? '1' : '0';
887 case FILE_NOTIFY_ON_RELEASE
:
888 *s
++ = notify_on_release(cs
) ? '1' : '0';
897 start
= page
+ *ppos
;
899 retval
= n
- copy_to_user(buf
, start
, min(n
, nbytes
));
902 free_page((unsigned long)page
);
906 static ssize_t
cpuset_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
910 struct cftype
*cft
= __d_cft(file
->f_dentry
);
914 /* special function ? */
916 retval
= cft
->read(file
, buf
, nbytes
, ppos
);
918 retval
= cpuset_common_file_read(file
, buf
, nbytes
, ppos
);
923 static int cpuset_file_open(struct inode
*inode
, struct file
*file
)
928 err
= generic_file_open(inode
, file
);
932 cft
= __d_cft(file
->f_dentry
);
936 err
= cft
->open(inode
, file
);
943 static int cpuset_file_release(struct inode
*inode
, struct file
*file
)
945 struct cftype
*cft
= __d_cft(file
->f_dentry
);
947 return cft
->release(inode
, file
);
951 static struct file_operations cpuset_file_operations
= {
952 .read
= cpuset_file_read
,
953 .write
= cpuset_file_write
,
954 .llseek
= generic_file_llseek
,
955 .open
= cpuset_file_open
,
956 .release
= cpuset_file_release
,
959 static struct inode_operations cpuset_dir_inode_operations
= {
960 .lookup
= simple_lookup
,
961 .mkdir
= cpuset_mkdir
,
962 .rmdir
= cpuset_rmdir
,
965 static int cpuset_create_file(struct dentry
*dentry
, int mode
)
974 inode
= cpuset_new_inode(mode
);
979 inode
->i_op
= &cpuset_dir_inode_operations
;
980 inode
->i_fop
= &simple_dir_operations
;
982 /* start off with i_nlink == 2 (for "." entry) */
984 } else if (S_ISREG(mode
)) {
986 inode
->i_fop
= &cpuset_file_operations
;
989 d_instantiate(dentry
, inode
);
990 dget(dentry
); /* Extra count - pin the dentry in core */
995 * cpuset_create_dir - create a directory for an object.
996 * cs: the cpuset we create the directory for.
997 * It must have a valid ->parent field
998 * And we are going to fill its ->dentry field.
999 * name: The name to give to the cpuset directory. Will be copied.
1000 * mode: mode to set on new directory.
1003 static int cpuset_create_dir(struct cpuset
*cs
, const char *name
, int mode
)
1005 struct dentry
*dentry
= NULL
;
1006 struct dentry
*parent
;
1009 parent
= cs
->parent
->dentry
;
1010 dentry
= cpuset_get_dentry(parent
, name
);
1012 return PTR_ERR(dentry
);
1013 error
= cpuset_create_file(dentry
, S_IFDIR
| mode
);
1015 dentry
->d_fsdata
= cs
;
1016 parent
->d_inode
->i_nlink
++;
1017 cs
->dentry
= dentry
;
1024 static int cpuset_add_file(struct dentry
*dir
, const struct cftype
*cft
)
1026 struct dentry
*dentry
;
1029 down(&dir
->d_inode
->i_sem
);
1030 dentry
= cpuset_get_dentry(dir
, cft
->name
);
1031 if (!IS_ERR(dentry
)) {
1032 error
= cpuset_create_file(dentry
, 0644 | S_IFREG
);
1034 dentry
->d_fsdata
= (void *)cft
;
1037 error
= PTR_ERR(dentry
);
1038 up(&dir
->d_inode
->i_sem
);
1043 * Stuff for reading the 'tasks' file.
1045 * Reading this file can return large amounts of data if a cpuset has
1046 * *lots* of attached tasks. So it may need several calls to read(),
1047 * but we cannot guarantee that the information we produce is correct
1048 * unless we produce it entirely atomically.
1050 * Upon tasks file open(), a struct ctr_struct is allocated, that
1051 * will have a pointer to an array (also allocated here). The struct
1052 * ctr_struct * is stored in file->private_data. Its resources will
1053 * be freed by release() when the file is closed. The array is used
1054 * to sprintf the PIDs and then used by read().
1057 /* cpusets_tasks_read array */
1065 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1066 * Return actual number of pids loaded.
1068 static inline int pid_array_load(pid_t
*pidarray
, int npids
, struct cpuset
*cs
)
1071 struct task_struct
*g
, *p
;
1073 read_lock(&tasklist_lock
);
1075 do_each_thread(g
, p
) {
1076 if (p
->cpuset
== cs
) {
1077 pidarray
[n
++] = p
->pid
;
1078 if (unlikely(n
== npids
))
1081 } while_each_thread(g
, p
);
1084 read_unlock(&tasklist_lock
);
1088 static int cmppid(const void *a
, const void *b
)
1090 return *(pid_t
*)a
- *(pid_t
*)b
;
1094 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1095 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1096 * count 'cnt' of how many chars would be written if buf were large enough.
1098 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1103 for (i
= 0; i
< npids
; i
++)
1104 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1108 static int cpuset_tasks_open(struct inode
*unused
, struct file
*file
)
1110 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1111 struct ctr_struct
*ctr
;
1116 if (!(file
->f_mode
& FMODE_READ
))
1119 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1124 * If cpuset gets more users after we read count, we won't have
1125 * enough space - tough. This race is indistinguishable to the
1126 * caller from the case that the additional cpuset users didn't
1127 * show up until sometime later on.
1129 npids
= atomic_read(&cs
->count
);
1130 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1134 npids
= pid_array_load(pidarray
, npids
, cs
);
1135 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1137 /* Call pid_array_to_buf() twice, first just to get bufsz */
1138 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1139 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1142 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1145 file
->private_data
= ctr
;
1156 static ssize_t
cpuset_tasks_read(struct file
*file
, char __user
*buf
,
1157 size_t nbytes
, loff_t
*ppos
)
1159 struct ctr_struct
*ctr
= file
->private_data
;
1161 if (*ppos
+ nbytes
> ctr
->bufsz
)
1162 nbytes
= ctr
->bufsz
- *ppos
;
1163 if (copy_to_user(buf
, ctr
->buf
+ *ppos
, nbytes
))
1169 static int cpuset_tasks_release(struct inode
*unused_inode
, struct file
*file
)
1171 struct ctr_struct
*ctr
;
1173 if (file
->f_mode
& FMODE_READ
) {
1174 ctr
= file
->private_data
;
1182 * for the common functions, 'private' gives the type of file
1185 static struct cftype cft_tasks
= {
1187 .open
= cpuset_tasks_open
,
1188 .read
= cpuset_tasks_read
,
1189 .release
= cpuset_tasks_release
,
1190 .private = FILE_TASKLIST
,
1193 static struct cftype cft_cpus
= {
1195 .private = FILE_CPULIST
,
1198 static struct cftype cft_mems
= {
1200 .private = FILE_MEMLIST
,
1203 static struct cftype cft_cpu_exclusive
= {
1204 .name
= "cpu_exclusive",
1205 .private = FILE_CPU_EXCLUSIVE
,
1208 static struct cftype cft_mem_exclusive
= {
1209 .name
= "mem_exclusive",
1210 .private = FILE_MEM_EXCLUSIVE
,
1213 static struct cftype cft_notify_on_release
= {
1214 .name
= "notify_on_release",
1215 .private = FILE_NOTIFY_ON_RELEASE
,
1218 static int cpuset_populate_dir(struct dentry
*cs_dentry
)
1222 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpus
)) < 0)
1224 if ((err
= cpuset_add_file(cs_dentry
, &cft_mems
)) < 0)
1226 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpu_exclusive
)) < 0)
1228 if ((err
= cpuset_add_file(cs_dentry
, &cft_mem_exclusive
)) < 0)
1230 if ((err
= cpuset_add_file(cs_dentry
, &cft_notify_on_release
)) < 0)
1232 if ((err
= cpuset_add_file(cs_dentry
, &cft_tasks
)) < 0)
1238 * cpuset_create - create a cpuset
1239 * parent: cpuset that will be parent of the new cpuset.
1240 * name: name of the new cpuset. Will be strcpy'ed.
1241 * mode: mode to set on new inode
1243 * Must be called with the semaphore on the parent inode held
1246 static long cpuset_create(struct cpuset
*parent
, const char *name
, int mode
)
1251 cs
= kmalloc(sizeof(*cs
), GFP_KERNEL
);
1258 if (notify_on_release(parent
))
1259 set_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
1260 cs
->cpus_allowed
= CPU_MASK_NONE
;
1261 cs
->mems_allowed
= NODE_MASK_NONE
;
1262 atomic_set(&cs
->count
, 0);
1263 INIT_LIST_HEAD(&cs
->sibling
);
1264 INIT_LIST_HEAD(&cs
->children
);
1265 atomic_inc(&cpuset_mems_generation
);
1266 cs
->mems_generation
= atomic_read(&cpuset_mems_generation
);
1268 cs
->parent
= parent
;
1270 list_add(&cs
->sibling
, &cs
->parent
->children
);
1272 err
= cpuset_create_dir(cs
, name
, mode
);
1277 * Release cpuset_sem before cpuset_populate_dir() because it
1278 * will down() this new directory's i_sem and if we race with
1279 * another mkdir, we might deadlock.
1283 err
= cpuset_populate_dir(cs
->dentry
);
1284 /* If err < 0, we have a half-filled directory - oh well ;) */
1287 list_del(&cs
->sibling
);
1293 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1295 struct cpuset
*c_parent
= dentry
->d_parent
->d_fsdata
;
1297 /* the vfs holds inode->i_sem already */
1298 return cpuset_create(c_parent
, dentry
->d_name
.name
, mode
| S_IFDIR
);
1301 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
1303 struct cpuset
*cs
= dentry
->d_fsdata
;
1305 struct cpuset
*parent
;
1307 /* the vfs holds both inode->i_sem already */
1311 if (atomic_read(&cs
->count
) > 0) {
1315 if (!list_empty(&cs
->children
)) {
1319 spin_lock(&cs
->dentry
->d_lock
);
1320 parent
= cs
->parent
;
1321 set_bit(CS_REMOVED
, &cs
->flags
);
1322 list_del(&cs
->sibling
); /* delete my sibling from parent->children */
1323 if (list_empty(&parent
->children
))
1324 check_for_release(parent
);
1325 d
= dget(cs
->dentry
);
1327 spin_unlock(&d
->d_lock
);
1328 cpuset_d_remove_dir(d
);
1335 * cpuset_init - initialize cpusets at system boot
1337 * Description: Initialize top_cpuset and the cpuset internal file system,
1340 int __init
cpuset_init(void)
1342 struct dentry
*root
;
1345 top_cpuset
.cpus_allowed
= CPU_MASK_ALL
;
1346 top_cpuset
.mems_allowed
= NODE_MASK_ALL
;
1348 atomic_inc(&cpuset_mems_generation
);
1349 top_cpuset
.mems_generation
= atomic_read(&cpuset_mems_generation
);
1351 init_task
.cpuset
= &top_cpuset
;
1353 err
= register_filesystem(&cpuset_fs_type
);
1356 cpuset_mount
= kern_mount(&cpuset_fs_type
);
1357 if (IS_ERR(cpuset_mount
)) {
1358 printk(KERN_ERR
"cpuset: could not mount!\n");
1359 err
= PTR_ERR(cpuset_mount
);
1360 cpuset_mount
= NULL
;
1363 root
= cpuset_mount
->mnt_sb
->s_root
;
1364 root
->d_fsdata
= &top_cpuset
;
1365 root
->d_inode
->i_nlink
++;
1366 top_cpuset
.dentry
= root
;
1367 root
->d_inode
->i_op
= &cpuset_dir_inode_operations
;
1368 err
= cpuset_populate_dir(root
);
1374 * cpuset_init_smp - initialize cpus_allowed
1376 * Description: Finish top cpuset after cpu, node maps are initialized
1379 void __init
cpuset_init_smp(void)
1381 top_cpuset
.cpus_allowed
= cpu_online_map
;
1382 top_cpuset
.mems_allowed
= node_online_map
;
1386 * cpuset_fork - attach newly forked task to its parents cpuset.
1387 * @p: pointer to task_struct of forking parent process.
1389 * Description: By default, on fork, a task inherits its
1390 * parents cpuset. The pointer to the shared cpuset is
1391 * automatically copied in fork.c by dup_task_struct().
1392 * This cpuset_fork() routine need only increment the usage
1393 * counter in that cpuset.
1396 void cpuset_fork(struct task_struct
*tsk
)
1398 atomic_inc(&tsk
->cpuset
->count
);
1402 * cpuset_exit - detach cpuset from exiting task
1403 * @tsk: pointer to task_struct of exiting process
1405 * Description: Detach cpuset from @tsk and release it.
1409 void cpuset_exit(struct task_struct
*tsk
)
1418 if (atomic_dec_and_test(&cs
->count
)) {
1420 check_for_release(cs
);
1426 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1427 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
1429 * Description: Returns the cpumask_t cpus_allowed of the cpuset
1430 * attached to the specified @tsk. Guaranteed to return some non-empty
1431 * subset of cpu_online_map, even if this means going outside the
1435 cpumask_t
cpuset_cpus_allowed(const struct task_struct
*tsk
)
1440 task_lock((struct task_struct
*)tsk
);
1441 guarantee_online_cpus(tsk
->cpuset
, &mask
);
1442 task_unlock((struct task_struct
*)tsk
);
1448 void cpuset_init_current_mems_allowed(void)
1450 current
->mems_allowed
= NODE_MASK_ALL
;
1454 * If the current tasks cpusets mems_allowed changed behind our backs,
1455 * update current->mems_allowed and mems_generation to the new value.
1456 * Do not call this routine if in_interrupt().
1459 void cpuset_update_current_mems_allowed(void)
1461 struct cpuset
*cs
= current
->cpuset
;
1464 return; /* task is exiting */
1465 if (current
->cpuset_mems_generation
!= cs
->mems_generation
) {
1472 void cpuset_restrict_to_mems_allowed(unsigned long *nodes
)
1474 bitmap_and(nodes
, nodes
, nodes_addr(current
->mems_allowed
),
1479 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1481 int cpuset_zonelist_valid_mems_allowed(struct zonelist
*zl
)
1485 for (i
= 0; zl
->zones
[i
]; i
++) {
1486 int nid
= zl
->zones
[i
]->zone_pgdat
->node_id
;
1488 if (node_isset(nid
, current
->mems_allowed
))
1495 * Is 'current' valid, and is zone z allowed in current->mems_allowed?
1497 int cpuset_zone_allowed(struct zone
*z
)
1499 return in_interrupt() ||
1500 node_isset(z
->zone_pgdat
->node_id
, current
->mems_allowed
);
1504 * proc_cpuset_show()
1505 * - Print tasks cpuset path into seq_file.
1506 * - Used for /proc/<pid>/cpuset.
1509 static int proc_cpuset_show(struct seq_file
*m
, void *v
)
1512 struct task_struct
*tsk
;
1516 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1530 retval
= cpuset_path(cs
, buf
, PAGE_SIZE
);
1541 static int cpuset_open(struct inode
*inode
, struct file
*file
)
1543 struct task_struct
*tsk
= PROC_I(inode
)->task
;
1544 return single_open(file
, proc_cpuset_show
, tsk
);
1547 struct file_operations proc_cpuset_operations
= {
1548 .open
= cpuset_open
,
1550 .llseek
= seq_lseek
,
1551 .release
= single_release
,
1554 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
1555 char *cpuset_task_status_allowed(struct task_struct
*task
, char *buffer
)
1557 buffer
+= sprintf(buffer
, "Cpus_allowed:\t");
1558 buffer
+= cpumask_scnprintf(buffer
, PAGE_SIZE
, task
->cpus_allowed
);
1559 buffer
+= sprintf(buffer
, "\n");
1560 buffer
+= sprintf(buffer
, "Mems_allowed:\t");
1561 buffer
+= nodemask_scnprintf(buffer
, PAGE_SIZE
, task
->mems_allowed
);
1562 buffer
+= sprintf(buffer
, "\n");