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>
35 #include <linux/mempolicy.h>
37 #include <linux/module.h>
38 #include <linux/mount.h>
39 #include <linux/namei.h>
40 #include <linux/pagemap.h>
41 #include <linux/proc_fs.h>
42 #include <linux/sched.h>
43 #include <linux/seq_file.h>
44 #include <linux/slab.h>
45 #include <linux/smp_lock.h>
46 #include <linux/spinlock.h>
47 #include <linux/stat.h>
48 #include <linux/string.h>
49 #include <linux/time.h>
50 #include <linux/backing-dev.h>
51 #include <linux/sort.h>
53 #include <asm/uaccess.h>
54 #include <asm/atomic.h>
55 #include <asm/semaphore.h>
57 #define CPUSET_SUPER_MAGIC 0x27e0eb
60 unsigned long flags
; /* "unsigned long" so bitops work */
61 cpumask_t cpus_allowed
; /* CPUs allowed to tasks in cpuset */
62 nodemask_t mems_allowed
; /* Memory Nodes allowed to tasks */
65 * Count is atomic so can incr (fork) or decr (exit) without a lock.
67 atomic_t count
; /* count tasks using this cpuset */
70 * We link our 'sibling' struct into our parents 'children'.
71 * Our children link their 'sibling' into our 'children'.
73 struct list_head sibling
; /* my parents children */
74 struct list_head children
; /* my children */
76 struct cpuset
*parent
; /* my parent */
77 struct dentry
*dentry
; /* cpuset fs entry */
80 * Copy of global cpuset_mems_generation as of the most
81 * recent time this cpuset changed its mems_allowed.
86 /* bits in struct cpuset flags field */
95 /* convenient tests for these bits */
96 static inline int is_cpu_exclusive(const struct cpuset
*cs
)
98 return !!test_bit(CS_CPU_EXCLUSIVE
, &cs
->flags
);
101 static inline int is_mem_exclusive(const struct cpuset
*cs
)
103 return !!test_bit(CS_MEM_EXCLUSIVE
, &cs
->flags
);
106 static inline int is_removed(const struct cpuset
*cs
)
108 return !!test_bit(CS_REMOVED
, &cs
->flags
);
111 static inline int notify_on_release(const struct cpuset
*cs
)
113 return !!test_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
116 static inline int is_memory_migrate(const struct cpuset
*cs
)
118 return !!test_bit(CS_MEMORY_MIGRATE
, &cs
->flags
);
122 * Increment this atomic integer everytime any cpuset changes its
123 * mems_allowed value. Users of cpusets can track this generation
124 * number, and avoid having to lock and reload mems_allowed unless
125 * the cpuset they're using changes generation.
127 * A single, global generation is needed because attach_task() could
128 * reattach a task to a different cpuset, which must not have its
129 * generation numbers aliased with those of that tasks previous cpuset.
131 * Generations are needed for mems_allowed because one task cannot
132 * modify anothers memory placement. So we must enable every task,
133 * on every visit to __alloc_pages(), to efficiently check whether
134 * its current->cpuset->mems_allowed has changed, requiring an update
135 * of its current->mems_allowed.
137 static atomic_t cpuset_mems_generation
= ATOMIC_INIT(1);
139 static struct cpuset top_cpuset
= {
140 .flags
= ((1 << CS_CPU_EXCLUSIVE
) | (1 << CS_MEM_EXCLUSIVE
)),
141 .cpus_allowed
= CPU_MASK_ALL
,
142 .mems_allowed
= NODE_MASK_ALL
,
143 .count
= ATOMIC_INIT(0),
144 .sibling
= LIST_HEAD_INIT(top_cpuset
.sibling
),
145 .children
= LIST_HEAD_INIT(top_cpuset
.children
),
148 .mems_generation
= 0,
151 static struct vfsmount
*cpuset_mount
;
152 static struct super_block
*cpuset_sb
= NULL
;
155 * We have two global cpuset semaphores below. They can nest.
156 * It is ok to first take manage_sem, then nest callback_sem. We also
157 * require taking task_lock() when dereferencing a tasks cpuset pointer.
158 * See "The task_lock() exception", at the end of this comment.
160 * A task must hold both semaphores to modify cpusets. If a task
161 * holds manage_sem, then it blocks others wanting that semaphore,
162 * ensuring that it is the only task able to also acquire callback_sem
163 * and be able to modify cpusets. It can perform various checks on
164 * the cpuset structure first, knowing nothing will change. It can
165 * also allocate memory while just holding manage_sem. While it is
166 * performing these checks, various callback routines can briefly
167 * acquire callback_sem to query cpusets. Once it is ready to make
168 * the changes, it takes callback_sem, blocking everyone else.
170 * Calls to the kernel memory allocator can not be made while holding
171 * callback_sem, as that would risk double tripping on callback_sem
172 * from one of the callbacks into the cpuset code from within
175 * If a task is only holding callback_sem, then it has read-only
178 * The task_struct fields mems_allowed and mems_generation may only
179 * be accessed in the context of that task, so require no locks.
181 * Any task can increment and decrement the count field without lock.
182 * So in general, code holding manage_sem or callback_sem can't rely
183 * on the count field not changing. However, if the count goes to
184 * zero, then only attach_task(), which holds both semaphores, can
185 * increment it again. Because a count of zero means that no tasks
186 * are currently attached, therefore there is no way a task attached
187 * to that cpuset can fork (the other way to increment the count).
188 * So code holding manage_sem or callback_sem can safely assume that
189 * if the count is zero, it will stay zero. Similarly, if a task
190 * holds manage_sem or callback_sem on a cpuset with zero count, it
191 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
192 * both of those semaphores.
194 * A possible optimization to improve parallelism would be to make
195 * callback_sem a R/W semaphore (rwsem), allowing the callback routines
196 * to proceed in parallel, with read access, until the holder of
197 * manage_sem needed to take this rwsem for exclusive write access
198 * and modify some cpusets.
200 * The cpuset_common_file_write handler for operations that modify
201 * the cpuset hierarchy holds manage_sem across the entire operation,
202 * single threading all such cpuset modifications across the system.
204 * The cpuset_common_file_read() handlers only hold callback_sem across
205 * small pieces of code, such as when reading out possibly multi-word
206 * cpumasks and nodemasks.
208 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
209 * (usually) take either semaphore. These are the two most performance
210 * critical pieces of code here. The exception occurs on cpuset_exit(),
211 * when a task in a notify_on_release cpuset exits. Then manage_sem
212 * is taken, and if the cpuset count is zero, a usermode call made
213 * to /sbin/cpuset_release_agent with the name of the cpuset (path
214 * relative to the root of cpuset file system) as the argument.
216 * A cpuset can only be deleted if both its 'count' of using tasks
217 * is zero, and its list of 'children' cpusets is empty. Since all
218 * tasks in the system use _some_ cpuset, and since there is always at
219 * least one task in the system (init, pid == 1), therefore, top_cpuset
220 * always has either children cpusets and/or using tasks. So we don't
221 * need a special hack to ensure that top_cpuset cannot be deleted.
223 * The above "Tale of Two Semaphores" would be complete, but for:
225 * The task_lock() exception
227 * The need for this exception arises from the action of attach_task(),
228 * which overwrites one tasks cpuset pointer with another. It does
229 * so using both semaphores, however there are several performance
230 * critical places that need to reference task->cpuset without the
231 * expense of grabbing a system global semaphore. Therefore except as
232 * noted below, when dereferencing or, as in attach_task(), modifying
233 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
234 * (task->alloc_lock) already in the task_struct routinely used for
238 static DECLARE_MUTEX(manage_sem
);
239 static DECLARE_MUTEX(callback_sem
);
242 * A couple of forward declarations required, due to cyclic reference loop:
243 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
244 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
247 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
248 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
250 static struct backing_dev_info cpuset_backing_dev_info
= {
251 .ra_pages
= 0, /* No readahead */
252 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
255 static struct inode
*cpuset_new_inode(mode_t mode
)
257 struct inode
*inode
= new_inode(cpuset_sb
);
260 inode
->i_mode
= mode
;
261 inode
->i_uid
= current
->fsuid
;
262 inode
->i_gid
= current
->fsgid
;
263 inode
->i_blksize
= PAGE_CACHE_SIZE
;
265 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
266 inode
->i_mapping
->backing_dev_info
= &cpuset_backing_dev_info
;
271 static void cpuset_diput(struct dentry
*dentry
, struct inode
*inode
)
273 /* is dentry a directory ? if so, kfree() associated cpuset */
274 if (S_ISDIR(inode
->i_mode
)) {
275 struct cpuset
*cs
= dentry
->d_fsdata
;
276 BUG_ON(!(is_removed(cs
)));
282 static struct dentry_operations cpuset_dops
= {
283 .d_iput
= cpuset_diput
,
286 static struct dentry
*cpuset_get_dentry(struct dentry
*parent
, const char *name
)
288 struct dentry
*d
= lookup_one_len(name
, parent
, strlen(name
));
290 d
->d_op
= &cpuset_dops
;
294 static void remove_dir(struct dentry
*d
)
296 struct dentry
*parent
= dget(d
->d_parent
);
299 simple_rmdir(parent
->d_inode
, d
);
304 * NOTE : the dentry must have been dget()'ed
306 static void cpuset_d_remove_dir(struct dentry
*dentry
)
308 struct list_head
*node
;
310 spin_lock(&dcache_lock
);
311 node
= dentry
->d_subdirs
.next
;
312 while (node
!= &dentry
->d_subdirs
) {
313 struct dentry
*d
= list_entry(node
, struct dentry
, d_child
);
317 spin_unlock(&dcache_lock
);
319 simple_unlink(dentry
->d_inode
, d
);
321 spin_lock(&dcache_lock
);
323 node
= dentry
->d_subdirs
.next
;
325 list_del_init(&dentry
->d_child
);
326 spin_unlock(&dcache_lock
);
330 static struct super_operations cpuset_ops
= {
331 .statfs
= simple_statfs
,
332 .drop_inode
= generic_delete_inode
,
335 static int cpuset_fill_super(struct super_block
*sb
, void *unused_data
,
341 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
342 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
343 sb
->s_magic
= CPUSET_SUPER_MAGIC
;
344 sb
->s_op
= &cpuset_ops
;
347 inode
= cpuset_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
);
349 inode
->i_op
= &simple_dir_inode_operations
;
350 inode
->i_fop
= &simple_dir_operations
;
351 /* directories start off with i_nlink == 2 (for "." entry) */
357 root
= d_alloc_root(inode
);
366 static struct super_block
*cpuset_get_sb(struct file_system_type
*fs_type
,
367 int flags
, const char *unused_dev_name
,
370 return get_sb_single(fs_type
, flags
, data
, cpuset_fill_super
);
373 static struct file_system_type cpuset_fs_type
= {
375 .get_sb
= cpuset_get_sb
,
376 .kill_sb
= kill_litter_super
,
381 * The files in the cpuset filesystem mostly have a very simple read/write
382 * handling, some common function will take care of it. Nevertheless some cases
383 * (read tasks) are special and therefore I define this structure for every
387 * When reading/writing to a file:
388 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
389 * - the 'cftype' of the file is file->f_dentry->d_fsdata
395 int (*open
) (struct inode
*inode
, struct file
*file
);
396 ssize_t (*read
) (struct file
*file
, char __user
*buf
, size_t nbytes
,
398 int (*write
) (struct file
*file
, const char __user
*buf
, size_t nbytes
,
400 int (*release
) (struct inode
*inode
, struct file
*file
);
403 static inline struct cpuset
*__d_cs(struct dentry
*dentry
)
405 return dentry
->d_fsdata
;
408 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
410 return dentry
->d_fsdata
;
414 * Call with manage_sem held. Writes path of cpuset into buf.
415 * Returns 0 on success, -errno on error.
418 static int cpuset_path(const struct cpuset
*cs
, char *buf
, int buflen
)
422 start
= buf
+ buflen
;
426 int len
= cs
->dentry
->d_name
.len
;
427 if ((start
-= len
) < buf
)
428 return -ENAMETOOLONG
;
429 memcpy(start
, cs
->dentry
->d_name
.name
, len
);
436 return -ENAMETOOLONG
;
439 memmove(buf
, start
, buf
+ buflen
- start
);
444 * Notify userspace when a cpuset is released, by running
445 * /sbin/cpuset_release_agent with the name of the cpuset (path
446 * relative to the root of cpuset file system) as the argument.
448 * Most likely, this user command will try to rmdir this cpuset.
450 * This races with the possibility that some other task will be
451 * attached to this cpuset before it is removed, or that some other
452 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
453 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
454 * unused, and this cpuset will be reprieved from its death sentence,
455 * to continue to serve a useful existence. Next time it's released,
456 * we will get notified again, if it still has 'notify_on_release' set.
458 * The final arg to call_usermodehelper() is 0, which means don't
459 * wait. The separate /sbin/cpuset_release_agent task is forked by
460 * call_usermodehelper(), then control in this thread returns here,
461 * without waiting for the release agent task. We don't bother to
462 * wait because the caller of this routine has no use for the exit
463 * status of the /sbin/cpuset_release_agent task, so no sense holding
464 * our caller up for that.
466 * When we had only one cpuset semaphore, we had to call this
467 * without holding it, to avoid deadlock when call_usermodehelper()
468 * allocated memory. With two locks, we could now call this while
469 * holding manage_sem, but we still don't, so as to minimize
470 * the time manage_sem is held.
473 static void cpuset_release_agent(const char *pathbuf
)
475 char *argv
[3], *envp
[3];
482 argv
[i
++] = "/sbin/cpuset_release_agent";
483 argv
[i
++] = (char *)pathbuf
;
487 /* minimal command environment */
488 envp
[i
++] = "HOME=/";
489 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
492 call_usermodehelper(argv
[0], argv
, envp
, 0);
497 * Either cs->count of using tasks transitioned to zero, or the
498 * cs->children list of child cpusets just became empty. If this
499 * cs is notify_on_release() and now both the user count is zero and
500 * the list of children is empty, prepare cpuset path in a kmalloc'd
501 * buffer, to be returned via ppathbuf, so that the caller can invoke
502 * cpuset_release_agent() with it later on, once manage_sem is dropped.
503 * Call here with manage_sem held.
505 * This check_for_release() routine is responsible for kmalloc'ing
506 * pathbuf. The above cpuset_release_agent() is responsible for
507 * kfree'ing pathbuf. The caller of these routines is responsible
508 * for providing a pathbuf pointer, initialized to NULL, then
509 * calling check_for_release() with manage_sem held and the address
510 * of the pathbuf pointer, then dropping manage_sem, then calling
511 * cpuset_release_agent() with pathbuf, as set by check_for_release().
514 static void check_for_release(struct cpuset
*cs
, char **ppathbuf
)
516 if (notify_on_release(cs
) && atomic_read(&cs
->count
) == 0 &&
517 list_empty(&cs
->children
)) {
520 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
523 if (cpuset_path(cs
, buf
, PAGE_SIZE
) < 0)
531 * Return in *pmask the portion of a cpusets's cpus_allowed that
532 * are online. If none are online, walk up the cpuset hierarchy
533 * until we find one that does have some online cpus. If we get
534 * all the way to the top and still haven't found any online cpus,
535 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
536 * task, return cpu_online_map.
538 * One way or another, we guarantee to return some non-empty subset
541 * Call with callback_sem held.
544 static void guarantee_online_cpus(const struct cpuset
*cs
, cpumask_t
*pmask
)
546 while (cs
&& !cpus_intersects(cs
->cpus_allowed
, cpu_online_map
))
549 cpus_and(*pmask
, cs
->cpus_allowed
, cpu_online_map
);
551 *pmask
= cpu_online_map
;
552 BUG_ON(!cpus_intersects(*pmask
, cpu_online_map
));
556 * Return in *pmask the portion of a cpusets's mems_allowed that
557 * are online. If none are online, walk up the cpuset hierarchy
558 * until we find one that does have some online mems. If we get
559 * all the way to the top and still haven't found any online mems,
560 * return node_online_map.
562 * One way or another, we guarantee to return some non-empty subset
563 * of node_online_map.
565 * Call with callback_sem held.
568 static void guarantee_online_mems(const struct cpuset
*cs
, nodemask_t
*pmask
)
570 while (cs
&& !nodes_intersects(cs
->mems_allowed
, node_online_map
))
573 nodes_and(*pmask
, cs
->mems_allowed
, node_online_map
);
575 *pmask
= node_online_map
;
576 BUG_ON(!nodes_intersects(*pmask
, node_online_map
));
580 * Refresh current tasks mems_allowed and mems_generation from current
583 * Call without callback_sem or task_lock() held. May be called with
584 * or without manage_sem held. Will acquire task_lock() and might
585 * acquire callback_sem during call.
587 * The task_lock() is required to dereference current->cpuset safely.
588 * Without it, we could pick up the pointer value of current->cpuset
589 * in one instruction, and then attach_task could give us a different
590 * cpuset, and then the cpuset we had could be removed and freed,
591 * and then on our next instruction, we could dereference a no longer
592 * valid cpuset pointer to get its mems_generation field.
594 * This routine is needed to update the per-task mems_allowed data,
595 * within the tasks context, when it is trying to allocate memory
596 * (in various mm/mempolicy.c routines) and notices that some other
597 * task has been modifying its cpuset.
600 static void refresh_mems(void)
602 int my_cpusets_mem_gen
;
605 my_cpusets_mem_gen
= current
->cpuset
->mems_generation
;
606 task_unlock(current
);
608 if (current
->cpuset_mems_generation
!= my_cpusets_mem_gen
) {
610 nodemask_t oldmem
= current
->mems_allowed
;
615 cs
= current
->cpuset
;
616 migrate
= is_memory_migrate(cs
);
617 guarantee_online_mems(cs
, ¤t
->mems_allowed
);
618 current
->cpuset_mems_generation
= cs
->mems_generation
;
619 task_unlock(current
);
621 if (!nodes_equal(oldmem
, current
->mems_allowed
)) {
622 numa_policy_rebind(&oldmem
, ¤t
->mems_allowed
);
624 do_migrate_pages(current
->mm
, &oldmem
,
625 ¤t
->mems_allowed
,
633 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
635 * One cpuset is a subset of another if all its allowed CPUs and
636 * Memory Nodes are a subset of the other, and its exclusive flags
637 * are only set if the other's are set. Call holding manage_sem.
640 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
642 return cpus_subset(p
->cpus_allowed
, q
->cpus_allowed
) &&
643 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
644 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
645 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
649 * validate_change() - Used to validate that any proposed cpuset change
650 * follows the structural rules for cpusets.
652 * If we replaced the flag and mask values of the current cpuset
653 * (cur) with those values in the trial cpuset (trial), would
654 * our various subset and exclusive rules still be valid? Presumes
657 * 'cur' is the address of an actual, in-use cpuset. Operations
658 * such as list traversal that depend on the actual address of the
659 * cpuset in the list must use cur below, not trial.
661 * 'trial' is the address of bulk structure copy of cur, with
662 * perhaps one or more of the fields cpus_allowed, mems_allowed,
663 * or flags changed to new, trial values.
665 * Return 0 if valid, -errno if not.
668 static int validate_change(const struct cpuset
*cur
, const struct cpuset
*trial
)
670 struct cpuset
*c
, *par
;
672 /* Each of our child cpusets must be a subset of us */
673 list_for_each_entry(c
, &cur
->children
, sibling
) {
674 if (!is_cpuset_subset(c
, trial
))
678 /* Remaining checks don't apply to root cpuset */
679 if ((par
= cur
->parent
) == NULL
)
682 /* We must be a subset of our parent cpuset */
683 if (!is_cpuset_subset(trial
, par
))
686 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
687 list_for_each_entry(c
, &par
->children
, sibling
) {
688 if ((is_cpu_exclusive(trial
) || is_cpu_exclusive(c
)) &&
690 cpus_intersects(trial
->cpus_allowed
, c
->cpus_allowed
))
692 if ((is_mem_exclusive(trial
) || is_mem_exclusive(c
)) &&
694 nodes_intersects(trial
->mems_allowed
, c
->mems_allowed
))
702 * For a given cpuset cur, partition the system as follows
703 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
704 * exclusive child cpusets
705 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
706 * exclusive child cpusets
707 * Build these two partitions by calling partition_sched_domains
709 * Call with manage_sem held. May nest a call to the
710 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
713 static void update_cpu_domains(struct cpuset
*cur
)
715 struct cpuset
*c
, *par
= cur
->parent
;
716 cpumask_t pspan
, cspan
;
718 if (par
== NULL
|| cpus_empty(cur
->cpus_allowed
))
722 * Get all cpus from parent's cpus_allowed not part of exclusive
725 pspan
= par
->cpus_allowed
;
726 list_for_each_entry(c
, &par
->children
, sibling
) {
727 if (is_cpu_exclusive(c
))
728 cpus_andnot(pspan
, pspan
, c
->cpus_allowed
);
730 if (is_removed(cur
) || !is_cpu_exclusive(cur
)) {
731 cpus_or(pspan
, pspan
, cur
->cpus_allowed
);
732 if (cpus_equal(pspan
, cur
->cpus_allowed
))
734 cspan
= CPU_MASK_NONE
;
736 if (cpus_empty(pspan
))
738 cspan
= cur
->cpus_allowed
;
740 * Get all cpus from current cpuset's cpus_allowed not part
741 * of exclusive children
743 list_for_each_entry(c
, &cur
->children
, sibling
) {
744 if (is_cpu_exclusive(c
))
745 cpus_andnot(cspan
, cspan
, c
->cpus_allowed
);
750 partition_sched_domains(&pspan
, &cspan
);
751 unlock_cpu_hotplug();
755 * Call with manage_sem held. May take callback_sem during call.
758 static int update_cpumask(struct cpuset
*cs
, char *buf
)
760 struct cpuset trialcs
;
761 int retval
, cpus_unchanged
;
764 retval
= cpulist_parse(buf
, trialcs
.cpus_allowed
);
767 cpus_and(trialcs
.cpus_allowed
, trialcs
.cpus_allowed
, cpu_online_map
);
768 if (cpus_empty(trialcs
.cpus_allowed
))
770 retval
= validate_change(cs
, &trialcs
);
773 cpus_unchanged
= cpus_equal(cs
->cpus_allowed
, trialcs
.cpus_allowed
);
775 cs
->cpus_allowed
= trialcs
.cpus_allowed
;
777 if (is_cpu_exclusive(cs
) && !cpus_unchanged
)
778 update_cpu_domains(cs
);
783 * Call with manage_sem held. May take callback_sem during call.
786 static int update_nodemask(struct cpuset
*cs
, char *buf
)
788 struct cpuset trialcs
;
792 retval
= nodelist_parse(buf
, trialcs
.mems_allowed
);
795 nodes_and(trialcs
.mems_allowed
, trialcs
.mems_allowed
, node_online_map
);
796 if (nodes_empty(trialcs
.mems_allowed
))
798 retval
= validate_change(cs
, &trialcs
);
801 cs
->mems_allowed
= trialcs
.mems_allowed
;
802 atomic_inc(&cpuset_mems_generation
);
803 cs
->mems_generation
= atomic_read(&cpuset_mems_generation
);
810 * update_flag - read a 0 or a 1 in a file and update associated flag
811 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
812 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE)
813 * cs: the cpuset to update
814 * buf: the buffer where we read the 0 or 1
816 * Call with manage_sem held.
819 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
, char *buf
)
822 struct cpuset trialcs
;
823 int err
, cpu_exclusive_changed
;
825 turning_on
= (simple_strtoul(buf
, NULL
, 10) != 0);
829 set_bit(bit
, &trialcs
.flags
);
831 clear_bit(bit
, &trialcs
.flags
);
833 err
= validate_change(cs
, &trialcs
);
836 cpu_exclusive_changed
=
837 (is_cpu_exclusive(cs
) != is_cpu_exclusive(&trialcs
));
840 set_bit(bit
, &cs
->flags
);
842 clear_bit(bit
, &cs
->flags
);
845 if (cpu_exclusive_changed
)
846 update_cpu_domains(cs
);
851 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
852 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
853 * notified on release.
855 * Call holding manage_sem. May take callback_sem and task_lock of
856 * the task 'pid' during call.
859 static int attach_task(struct cpuset
*cs
, char *pidbuf
, char **ppathbuf
)
862 struct task_struct
*tsk
;
863 struct cpuset
*oldcs
;
867 if (sscanf(pidbuf
, "%d", &pid
) != 1)
869 if (cpus_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
))
873 read_lock(&tasklist_lock
);
875 tsk
= find_task_by_pid(pid
);
876 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
877 read_unlock(&tasklist_lock
);
881 get_task_struct(tsk
);
882 read_unlock(&tasklist_lock
);
884 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
885 && (current
->euid
!= tsk
->suid
)) {
886 put_task_struct(tsk
);
891 get_task_struct(tsk
);
901 put_task_struct(tsk
);
904 atomic_inc(&cs
->count
);
908 guarantee_online_cpus(cs
, &cpus
);
909 set_cpus_allowed(tsk
, cpus
);
911 from
= oldcs
->mems_allowed
;
912 to
= cs
->mems_allowed
;
915 if (is_memory_migrate(cs
))
916 do_migrate_pages(tsk
->mm
, &from
, &to
, MPOL_MF_MOVE_ALL
);
917 put_task_struct(tsk
);
918 if (atomic_dec_and_test(&oldcs
->count
))
919 check_for_release(oldcs
, ppathbuf
);
923 /* The various types of files and directories in a cpuset file system */
933 FILE_NOTIFY_ON_RELEASE
,
937 static ssize_t
cpuset_common_file_write(struct file
*file
, const char __user
*userbuf
,
938 size_t nbytes
, loff_t
*unused_ppos
)
940 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
941 struct cftype
*cft
= __d_cft(file
->f_dentry
);
942 cpuset_filetype_t type
= cft
->private;
944 char *pathbuf
= NULL
;
947 /* Crude upper limit on largest legitimate cpulist user might write. */
948 if (nbytes
> 100 + 6 * NR_CPUS
)
951 /* +1 for nul-terminator */
952 if ((buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
)) == 0)
955 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
959 buffer
[nbytes
] = 0; /* nul-terminate */
963 if (is_removed(cs
)) {
970 retval
= update_cpumask(cs
, buffer
);
973 retval
= update_nodemask(cs
, buffer
);
975 case FILE_CPU_EXCLUSIVE
:
976 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, buffer
);
978 case FILE_MEM_EXCLUSIVE
:
979 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, buffer
);
981 case FILE_NOTIFY_ON_RELEASE
:
982 retval
= update_flag(CS_NOTIFY_ON_RELEASE
, cs
, buffer
);
984 case FILE_MEMORY_MIGRATE
:
985 retval
= update_flag(CS_MEMORY_MIGRATE
, cs
, buffer
);
988 retval
= attach_task(cs
, buffer
, &pathbuf
);
999 cpuset_release_agent(pathbuf
);
1005 static ssize_t
cpuset_file_write(struct file
*file
, const char __user
*buf
,
1006 size_t nbytes
, loff_t
*ppos
)
1009 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1013 /* special function ? */
1015 retval
= cft
->write(file
, buf
, nbytes
, ppos
);
1017 retval
= cpuset_common_file_write(file
, buf
, nbytes
, ppos
);
1023 * These ascii lists should be read in a single call, by using a user
1024 * buffer large enough to hold the entire map. If read in smaller
1025 * chunks, there is no guarantee of atomicity. Since the display format
1026 * used, list of ranges of sequential numbers, is variable length,
1027 * and since these maps can change value dynamically, one could read
1028 * gibberish by doing partial reads while a list was changing.
1029 * A single large read to a buffer that crosses a page boundary is
1030 * ok, because the result being copied to user land is not recomputed
1031 * across a page fault.
1034 static int cpuset_sprintf_cpulist(char *page
, struct cpuset
*cs
)
1038 down(&callback_sem
);
1039 mask
= cs
->cpus_allowed
;
1042 return cpulist_scnprintf(page
, PAGE_SIZE
, mask
);
1045 static int cpuset_sprintf_memlist(char *page
, struct cpuset
*cs
)
1049 down(&callback_sem
);
1050 mask
= cs
->mems_allowed
;
1053 return nodelist_scnprintf(page
, PAGE_SIZE
, mask
);
1056 static ssize_t
cpuset_common_file_read(struct file
*file
, char __user
*buf
,
1057 size_t nbytes
, loff_t
*ppos
)
1059 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1060 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1061 cpuset_filetype_t type
= cft
->private;
1066 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
1073 s
+= cpuset_sprintf_cpulist(s
, cs
);
1076 s
+= cpuset_sprintf_memlist(s
, cs
);
1078 case FILE_CPU_EXCLUSIVE
:
1079 *s
++ = is_cpu_exclusive(cs
) ? '1' : '0';
1081 case FILE_MEM_EXCLUSIVE
:
1082 *s
++ = is_mem_exclusive(cs
) ? '1' : '0';
1084 case FILE_NOTIFY_ON_RELEASE
:
1085 *s
++ = notify_on_release(cs
) ? '1' : '0';
1087 case FILE_MEMORY_MIGRATE
:
1088 *s
++ = is_memory_migrate(cs
) ? '1' : '0';
1096 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1098 free_page((unsigned long)page
);
1102 static ssize_t
cpuset_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1106 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1110 /* special function ? */
1112 retval
= cft
->read(file
, buf
, nbytes
, ppos
);
1114 retval
= cpuset_common_file_read(file
, buf
, nbytes
, ppos
);
1119 static int cpuset_file_open(struct inode
*inode
, struct file
*file
)
1124 err
= generic_file_open(inode
, file
);
1128 cft
= __d_cft(file
->f_dentry
);
1132 err
= cft
->open(inode
, file
);
1139 static int cpuset_file_release(struct inode
*inode
, struct file
*file
)
1141 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1143 return cft
->release(inode
, file
);
1148 * cpuset_rename - Only allow simple rename of directories in place.
1150 static int cpuset_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1151 struct inode
*new_dir
, struct dentry
*new_dentry
)
1153 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1155 if (new_dentry
->d_inode
)
1157 if (old_dir
!= new_dir
)
1159 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1162 static struct file_operations cpuset_file_operations
= {
1163 .read
= cpuset_file_read
,
1164 .write
= cpuset_file_write
,
1165 .llseek
= generic_file_llseek
,
1166 .open
= cpuset_file_open
,
1167 .release
= cpuset_file_release
,
1170 static struct inode_operations cpuset_dir_inode_operations
= {
1171 .lookup
= simple_lookup
,
1172 .mkdir
= cpuset_mkdir
,
1173 .rmdir
= cpuset_rmdir
,
1174 .rename
= cpuset_rename
,
1177 static int cpuset_create_file(struct dentry
*dentry
, int mode
)
1179 struct inode
*inode
;
1183 if (dentry
->d_inode
)
1186 inode
= cpuset_new_inode(mode
);
1190 if (S_ISDIR(mode
)) {
1191 inode
->i_op
= &cpuset_dir_inode_operations
;
1192 inode
->i_fop
= &simple_dir_operations
;
1194 /* start off with i_nlink == 2 (for "." entry) */
1196 } else if (S_ISREG(mode
)) {
1198 inode
->i_fop
= &cpuset_file_operations
;
1201 d_instantiate(dentry
, inode
);
1202 dget(dentry
); /* Extra count - pin the dentry in core */
1207 * cpuset_create_dir - create a directory for an object.
1208 * cs: the cpuset we create the directory for.
1209 * It must have a valid ->parent field
1210 * And we are going to fill its ->dentry field.
1211 * name: The name to give to the cpuset directory. Will be copied.
1212 * mode: mode to set on new directory.
1215 static int cpuset_create_dir(struct cpuset
*cs
, const char *name
, int mode
)
1217 struct dentry
*dentry
= NULL
;
1218 struct dentry
*parent
;
1221 parent
= cs
->parent
->dentry
;
1222 dentry
= cpuset_get_dentry(parent
, name
);
1224 return PTR_ERR(dentry
);
1225 error
= cpuset_create_file(dentry
, S_IFDIR
| mode
);
1227 dentry
->d_fsdata
= cs
;
1228 parent
->d_inode
->i_nlink
++;
1229 cs
->dentry
= dentry
;
1236 static int cpuset_add_file(struct dentry
*dir
, const struct cftype
*cft
)
1238 struct dentry
*dentry
;
1241 down(&dir
->d_inode
->i_sem
);
1242 dentry
= cpuset_get_dentry(dir
, cft
->name
);
1243 if (!IS_ERR(dentry
)) {
1244 error
= cpuset_create_file(dentry
, 0644 | S_IFREG
);
1246 dentry
->d_fsdata
= (void *)cft
;
1249 error
= PTR_ERR(dentry
);
1250 up(&dir
->d_inode
->i_sem
);
1255 * Stuff for reading the 'tasks' file.
1257 * Reading this file can return large amounts of data if a cpuset has
1258 * *lots* of attached tasks. So it may need several calls to read(),
1259 * but we cannot guarantee that the information we produce is correct
1260 * unless we produce it entirely atomically.
1262 * Upon tasks file open(), a struct ctr_struct is allocated, that
1263 * will have a pointer to an array (also allocated here). The struct
1264 * ctr_struct * is stored in file->private_data. Its resources will
1265 * be freed by release() when the file is closed. The array is used
1266 * to sprintf the PIDs and then used by read().
1269 /* cpusets_tasks_read array */
1277 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1278 * Return actual number of pids loaded. No need to task_lock(p)
1279 * when reading out p->cpuset, as we don't really care if it changes
1280 * on the next cycle, and we are not going to try to dereference it.
1282 static inline int pid_array_load(pid_t
*pidarray
, int npids
, struct cpuset
*cs
)
1285 struct task_struct
*g
, *p
;
1287 read_lock(&tasklist_lock
);
1289 do_each_thread(g
, p
) {
1290 if (p
->cpuset
== cs
) {
1291 pidarray
[n
++] = p
->pid
;
1292 if (unlikely(n
== npids
))
1295 } while_each_thread(g
, p
);
1298 read_unlock(&tasklist_lock
);
1302 static int cmppid(const void *a
, const void *b
)
1304 return *(pid_t
*)a
- *(pid_t
*)b
;
1308 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1309 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1310 * count 'cnt' of how many chars would be written if buf were large enough.
1312 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1317 for (i
= 0; i
< npids
; i
++)
1318 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1323 * Handle an open on 'tasks' file. Prepare a buffer listing the
1324 * process id's of tasks currently attached to the cpuset being opened.
1326 * Does not require any specific cpuset semaphores, and does not take any.
1328 static int cpuset_tasks_open(struct inode
*unused
, struct file
*file
)
1330 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1331 struct ctr_struct
*ctr
;
1336 if (!(file
->f_mode
& FMODE_READ
))
1339 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1344 * If cpuset gets more users after we read count, we won't have
1345 * enough space - tough. This race is indistinguishable to the
1346 * caller from the case that the additional cpuset users didn't
1347 * show up until sometime later on.
1349 npids
= atomic_read(&cs
->count
);
1350 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1354 npids
= pid_array_load(pidarray
, npids
, cs
);
1355 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1357 /* Call pid_array_to_buf() twice, first just to get bufsz */
1358 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1359 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1362 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1365 file
->private_data
= ctr
;
1376 static ssize_t
cpuset_tasks_read(struct file
*file
, char __user
*buf
,
1377 size_t nbytes
, loff_t
*ppos
)
1379 struct ctr_struct
*ctr
= file
->private_data
;
1381 if (*ppos
+ nbytes
> ctr
->bufsz
)
1382 nbytes
= ctr
->bufsz
- *ppos
;
1383 if (copy_to_user(buf
, ctr
->buf
+ *ppos
, nbytes
))
1389 static int cpuset_tasks_release(struct inode
*unused_inode
, struct file
*file
)
1391 struct ctr_struct
*ctr
;
1393 if (file
->f_mode
& FMODE_READ
) {
1394 ctr
= file
->private_data
;
1402 * for the common functions, 'private' gives the type of file
1405 static struct cftype cft_tasks
= {
1407 .open
= cpuset_tasks_open
,
1408 .read
= cpuset_tasks_read
,
1409 .release
= cpuset_tasks_release
,
1410 .private = FILE_TASKLIST
,
1413 static struct cftype cft_cpus
= {
1415 .private = FILE_CPULIST
,
1418 static struct cftype cft_mems
= {
1420 .private = FILE_MEMLIST
,
1423 static struct cftype cft_cpu_exclusive
= {
1424 .name
= "cpu_exclusive",
1425 .private = FILE_CPU_EXCLUSIVE
,
1428 static struct cftype cft_mem_exclusive
= {
1429 .name
= "mem_exclusive",
1430 .private = FILE_MEM_EXCLUSIVE
,
1433 static struct cftype cft_notify_on_release
= {
1434 .name
= "notify_on_release",
1435 .private = FILE_NOTIFY_ON_RELEASE
,
1438 static struct cftype cft_memory_migrate
= {
1439 .name
= "memory_migrate",
1440 .private = FILE_MEMORY_MIGRATE
,
1443 static int cpuset_populate_dir(struct dentry
*cs_dentry
)
1447 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpus
)) < 0)
1449 if ((err
= cpuset_add_file(cs_dentry
, &cft_mems
)) < 0)
1451 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpu_exclusive
)) < 0)
1453 if ((err
= cpuset_add_file(cs_dentry
, &cft_mem_exclusive
)) < 0)
1455 if ((err
= cpuset_add_file(cs_dentry
, &cft_notify_on_release
)) < 0)
1457 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_migrate
)) < 0)
1459 if ((err
= cpuset_add_file(cs_dentry
, &cft_tasks
)) < 0)
1465 * cpuset_create - create a cpuset
1466 * parent: cpuset that will be parent of the new cpuset.
1467 * name: name of the new cpuset. Will be strcpy'ed.
1468 * mode: mode to set on new inode
1470 * Must be called with the semaphore on the parent inode held
1473 static long cpuset_create(struct cpuset
*parent
, const char *name
, int mode
)
1478 cs
= kmalloc(sizeof(*cs
), GFP_KERNEL
);
1485 if (notify_on_release(parent
))
1486 set_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
1487 cs
->cpus_allowed
= CPU_MASK_NONE
;
1488 cs
->mems_allowed
= NODE_MASK_NONE
;
1489 atomic_set(&cs
->count
, 0);
1490 INIT_LIST_HEAD(&cs
->sibling
);
1491 INIT_LIST_HEAD(&cs
->children
);
1492 atomic_inc(&cpuset_mems_generation
);
1493 cs
->mems_generation
= atomic_read(&cpuset_mems_generation
);
1495 cs
->parent
= parent
;
1497 down(&callback_sem
);
1498 list_add(&cs
->sibling
, &cs
->parent
->children
);
1501 err
= cpuset_create_dir(cs
, name
, mode
);
1506 * Release manage_sem before cpuset_populate_dir() because it
1507 * will down() this new directory's i_sem and if we race with
1508 * another mkdir, we might deadlock.
1512 err
= cpuset_populate_dir(cs
->dentry
);
1513 /* If err < 0, we have a half-filled directory - oh well ;) */
1516 list_del(&cs
->sibling
);
1522 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1524 struct cpuset
*c_parent
= dentry
->d_parent
->d_fsdata
;
1526 /* the vfs holds inode->i_sem already */
1527 return cpuset_create(c_parent
, dentry
->d_name
.name
, mode
| S_IFDIR
);
1530 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
1532 struct cpuset
*cs
= dentry
->d_fsdata
;
1534 struct cpuset
*parent
;
1535 char *pathbuf
= NULL
;
1537 /* the vfs holds both inode->i_sem already */
1541 if (atomic_read(&cs
->count
) > 0) {
1545 if (!list_empty(&cs
->children
)) {
1549 parent
= cs
->parent
;
1550 down(&callback_sem
);
1551 set_bit(CS_REMOVED
, &cs
->flags
);
1552 if (is_cpu_exclusive(cs
))
1553 update_cpu_domains(cs
);
1554 list_del(&cs
->sibling
); /* delete my sibling from parent->children */
1555 spin_lock(&cs
->dentry
->d_lock
);
1556 d
= dget(cs
->dentry
);
1558 spin_unlock(&d
->d_lock
);
1559 cpuset_d_remove_dir(d
);
1562 if (list_empty(&parent
->children
))
1563 check_for_release(parent
, &pathbuf
);
1565 cpuset_release_agent(pathbuf
);
1570 * cpuset_init - initialize cpusets at system boot
1572 * Description: Initialize top_cpuset and the cpuset internal file system,
1575 int __init
cpuset_init(void)
1577 struct dentry
*root
;
1580 top_cpuset
.cpus_allowed
= CPU_MASK_ALL
;
1581 top_cpuset
.mems_allowed
= NODE_MASK_ALL
;
1583 atomic_inc(&cpuset_mems_generation
);
1584 top_cpuset
.mems_generation
= atomic_read(&cpuset_mems_generation
);
1586 init_task
.cpuset
= &top_cpuset
;
1588 err
= register_filesystem(&cpuset_fs_type
);
1591 cpuset_mount
= kern_mount(&cpuset_fs_type
);
1592 if (IS_ERR(cpuset_mount
)) {
1593 printk(KERN_ERR
"cpuset: could not mount!\n");
1594 err
= PTR_ERR(cpuset_mount
);
1595 cpuset_mount
= NULL
;
1598 root
= cpuset_mount
->mnt_sb
->s_root
;
1599 root
->d_fsdata
= &top_cpuset
;
1600 root
->d_inode
->i_nlink
++;
1601 top_cpuset
.dentry
= root
;
1602 root
->d_inode
->i_op
= &cpuset_dir_inode_operations
;
1603 err
= cpuset_populate_dir(root
);
1609 * cpuset_init_smp - initialize cpus_allowed
1611 * Description: Finish top cpuset after cpu, node maps are initialized
1614 void __init
cpuset_init_smp(void)
1616 top_cpuset
.cpus_allowed
= cpu_online_map
;
1617 top_cpuset
.mems_allowed
= node_online_map
;
1621 * cpuset_fork - attach newly forked task to its parents cpuset.
1622 * @tsk: pointer to task_struct of forking parent process.
1624 * Description: A task inherits its parent's cpuset at fork().
1626 * A pointer to the shared cpuset was automatically copied in fork.c
1627 * by dup_task_struct(). However, we ignore that copy, since it was
1628 * not made under the protection of task_lock(), so might no longer be
1629 * a valid cpuset pointer. attach_task() might have already changed
1630 * current->cpuset, allowing the previously referenced cpuset to
1631 * be removed and freed. Instead, we task_lock(current) and copy
1632 * its present value of current->cpuset for our freshly forked child.
1634 * At the point that cpuset_fork() is called, 'current' is the parent
1635 * task, and the passed argument 'child' points to the child task.
1638 void cpuset_fork(struct task_struct
*child
)
1641 child
->cpuset
= current
->cpuset
;
1642 atomic_inc(&child
->cpuset
->count
);
1643 task_unlock(current
);
1647 * cpuset_exit - detach cpuset from exiting task
1648 * @tsk: pointer to task_struct of exiting process
1650 * Description: Detach cpuset from @tsk and release it.
1652 * Note that cpusets marked notify_on_release force every task in
1653 * them to take the global manage_sem semaphore when exiting.
1654 * This could impact scaling on very large systems. Be reluctant to
1655 * use notify_on_release cpusets where very high task exit scaling
1656 * is required on large systems.
1658 * Don't even think about derefencing 'cs' after the cpuset use count
1659 * goes to zero, except inside a critical section guarded by manage_sem
1660 * or callback_sem. Otherwise a zero cpuset use count is a license to
1661 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
1663 * This routine has to take manage_sem, not callback_sem, because
1664 * it is holding that semaphore while calling check_for_release(),
1665 * which calls kmalloc(), so can't be called holding callback__sem().
1667 * We don't need to task_lock() this reference to tsk->cpuset,
1668 * because tsk is already marked PF_EXITING, so attach_task() won't
1672 void cpuset_exit(struct task_struct
*tsk
)
1676 BUG_ON(!(tsk
->flags
& PF_EXITING
));
1681 if (notify_on_release(cs
)) {
1682 char *pathbuf
= NULL
;
1685 if (atomic_dec_and_test(&cs
->count
))
1686 check_for_release(cs
, &pathbuf
);
1688 cpuset_release_agent(pathbuf
);
1690 atomic_dec(&cs
->count
);
1695 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1696 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
1698 * Description: Returns the cpumask_t cpus_allowed of the cpuset
1699 * attached to the specified @tsk. Guaranteed to return some non-empty
1700 * subset of cpu_online_map, even if this means going outside the
1704 cpumask_t
cpuset_cpus_allowed(const struct task_struct
*tsk
)
1708 down(&callback_sem
);
1709 task_lock((struct task_struct
*)tsk
);
1710 guarantee_online_cpus(tsk
->cpuset
, &mask
);
1711 task_unlock((struct task_struct
*)tsk
);
1717 void cpuset_init_current_mems_allowed(void)
1719 current
->mems_allowed
= NODE_MASK_ALL
;
1723 * cpuset_update_current_mems_allowed - update mems parameters to new values
1725 * If the current tasks cpusets mems_allowed changed behind our backs,
1726 * update current->mems_allowed and mems_generation to the new value.
1727 * Do not call this routine if in_interrupt().
1729 * Call without callback_sem or task_lock() held. May be called
1730 * with or without manage_sem held. Unless exiting, it will acquire
1731 * task_lock(). Also might acquire callback_sem during call to
1735 void cpuset_update_current_mems_allowed(void)
1738 int need_to_refresh
= 0;
1741 cs
= current
->cpuset
;
1744 if (current
->cpuset_mems_generation
!= cs
->mems_generation
)
1745 need_to_refresh
= 1;
1747 task_unlock(current
);
1748 if (need_to_refresh
)
1753 * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
1754 * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
1756 void cpuset_restrict_to_mems_allowed(unsigned long *nodes
)
1758 bitmap_and(nodes
, nodes
, nodes_addr(current
->mems_allowed
),
1763 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
1764 * @zl: the zonelist to be checked
1766 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1768 int cpuset_zonelist_valid_mems_allowed(struct zonelist
*zl
)
1772 for (i
= 0; zl
->zones
[i
]; i
++) {
1773 int nid
= zl
->zones
[i
]->zone_pgdat
->node_id
;
1775 if (node_isset(nid
, current
->mems_allowed
))
1782 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
1783 * ancestor to the specified cpuset. Call holding callback_sem.
1784 * If no ancestor is mem_exclusive (an unusual configuration), then
1785 * returns the root cpuset.
1787 static const struct cpuset
*nearest_exclusive_ancestor(const struct cpuset
*cs
)
1789 while (!is_mem_exclusive(cs
) && cs
->parent
)
1795 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
1796 * @z: is this zone on an allowed node?
1797 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
1799 * If we're in interrupt, yes, we can always allocate. If zone
1800 * z's node is in our tasks mems_allowed, yes. If it's not a
1801 * __GFP_HARDWALL request and this zone's nodes is in the nearest
1802 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
1805 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
1806 * and do not allow allocations outside the current tasks cpuset.
1807 * GFP_KERNEL allocations are not so marked, so can escape to the
1808 * nearest mem_exclusive ancestor cpuset.
1810 * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
1811 * routine only calls here with __GFP_HARDWALL bit _not_ set if
1812 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
1813 * mems_allowed came up empty on the first pass over the zonelist.
1814 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
1815 * short of memory, might require taking the callback_sem semaphore.
1817 * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
1818 * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
1819 * hardwall cpusets - no allocation on a node outside the cpuset is
1820 * allowed (unless in interrupt, of course).
1822 * The second loop doesn't even call here for GFP_ATOMIC requests
1823 * (if the __alloc_pages() local variable 'wait' is set). That check
1824 * and the checks below have the combined affect in the second loop of
1825 * the __alloc_pages() routine that:
1826 * in_interrupt - any node ok (current task context irrelevant)
1827 * GFP_ATOMIC - any node ok
1828 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
1829 * GFP_USER - only nodes in current tasks mems allowed ok.
1832 int cpuset_zone_allowed(struct zone
*z
, gfp_t gfp_mask
)
1834 int node
; /* node that zone z is on */
1835 const struct cpuset
*cs
; /* current cpuset ancestors */
1836 int allowed
= 1; /* is allocation in zone z allowed? */
1840 node
= z
->zone_pgdat
->node_id
;
1841 if (node_isset(node
, current
->mems_allowed
))
1843 if (gfp_mask
& __GFP_HARDWALL
) /* If hardwall request, stop here */
1846 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
1849 /* Not hardwall and node outside mems_allowed: scan up cpusets */
1850 down(&callback_sem
);
1853 cs
= nearest_exclusive_ancestor(current
->cpuset
);
1854 task_unlock(current
);
1856 allowed
= node_isset(node
, cs
->mems_allowed
);
1862 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
1863 * @p: pointer to task_struct of some other task.
1865 * Description: Return true if the nearest mem_exclusive ancestor
1866 * cpusets of tasks @p and current overlap. Used by oom killer to
1867 * determine if task @p's memory usage might impact the memory
1868 * available to the current task.
1870 * Acquires callback_sem - not suitable for calling from a fast path.
1873 int cpuset_excl_nodes_overlap(const struct task_struct
*p
)
1875 const struct cpuset
*cs1
, *cs2
; /* my and p's cpuset ancestors */
1876 int overlap
= 0; /* do cpusets overlap? */
1878 down(&callback_sem
);
1881 if (current
->flags
& PF_EXITING
) {
1882 task_unlock(current
);
1885 cs1
= nearest_exclusive_ancestor(current
->cpuset
);
1886 task_unlock(current
);
1888 task_lock((struct task_struct
*)p
);
1889 if (p
->flags
& PF_EXITING
) {
1890 task_unlock((struct task_struct
*)p
);
1893 cs2
= nearest_exclusive_ancestor(p
->cpuset
);
1894 task_unlock((struct task_struct
*)p
);
1896 overlap
= nodes_intersects(cs1
->mems_allowed
, cs2
->mems_allowed
);
1904 * proc_cpuset_show()
1905 * - Print tasks cpuset path into seq_file.
1906 * - Used for /proc/<pid>/cpuset.
1907 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
1908 * doesn't really matter if tsk->cpuset changes after we read it,
1909 * and we take manage_sem, keeping attach_task() from changing it
1913 static int proc_cpuset_show(struct seq_file
*m
, void *v
)
1916 struct task_struct
*tsk
;
1920 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1932 retval
= cpuset_path(cs
, buf
, PAGE_SIZE
);
1943 static int cpuset_open(struct inode
*inode
, struct file
*file
)
1945 struct task_struct
*tsk
= PROC_I(inode
)->task
;
1946 return single_open(file
, proc_cpuset_show
, tsk
);
1949 struct file_operations proc_cpuset_operations
= {
1950 .open
= cpuset_open
,
1952 .llseek
= seq_lseek
,
1953 .release
= single_release
,
1956 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
1957 char *cpuset_task_status_allowed(struct task_struct
*task
, char *buffer
)
1959 buffer
+= sprintf(buffer
, "Cpus_allowed:\t");
1960 buffer
+= cpumask_scnprintf(buffer
, PAGE_SIZE
, task
->cpus_allowed
);
1961 buffer
+= sprintf(buffer
, "\n");
1962 buffer
+= sprintf(buffer
, "Mems_allowed:\t");
1963 buffer
+= nodemask_scnprintf(buffer
, PAGE_SIZE
, task
->mems_allowed
);
1964 buffer
+= sprintf(buffer
, "\n");