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 * Tracks how many cpusets are currently defined in system.
61 * When there is only one cpuset (the root cpuset) we can
62 * short circuit some hooks.
64 int number_of_cpusets
;
66 /* See "Frequency meter" comments, below. */
69 int cnt
; /* unprocessed events count */
70 int val
; /* most recent output value */
71 time_t time
; /* clock (secs) when val computed */
72 spinlock_t lock
; /* guards read or write of above */
76 unsigned long flags
; /* "unsigned long" so bitops work */
77 cpumask_t cpus_allowed
; /* CPUs allowed to tasks in cpuset */
78 nodemask_t mems_allowed
; /* Memory Nodes allowed to tasks */
81 * Count is atomic so can incr (fork) or decr (exit) without a lock.
83 atomic_t count
; /* count tasks using this cpuset */
86 * We link our 'sibling' struct into our parents 'children'.
87 * Our children link their 'sibling' into our 'children'.
89 struct list_head sibling
; /* my parents children */
90 struct list_head children
; /* my children */
92 struct cpuset
*parent
; /* my parent */
93 struct dentry
*dentry
; /* cpuset fs entry */
96 * Copy of global cpuset_mems_generation as of the most
97 * recent time this cpuset changed its mems_allowed.
101 struct fmeter fmeter
; /* memory_pressure filter */
104 /* bits in struct cpuset flags field */
113 /* convenient tests for these bits */
114 static inline int is_cpu_exclusive(const struct cpuset
*cs
)
116 return !!test_bit(CS_CPU_EXCLUSIVE
, &cs
->flags
);
119 static inline int is_mem_exclusive(const struct cpuset
*cs
)
121 return !!test_bit(CS_MEM_EXCLUSIVE
, &cs
->flags
);
124 static inline int is_removed(const struct cpuset
*cs
)
126 return !!test_bit(CS_REMOVED
, &cs
->flags
);
129 static inline int notify_on_release(const struct cpuset
*cs
)
131 return !!test_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
134 static inline int is_memory_migrate(const struct cpuset
*cs
)
136 return !!test_bit(CS_MEMORY_MIGRATE
, &cs
->flags
);
140 * Increment this atomic integer everytime any cpuset changes its
141 * mems_allowed value. Users of cpusets can track this generation
142 * number, and avoid having to lock and reload mems_allowed unless
143 * the cpuset they're using changes generation.
145 * A single, global generation is needed because attach_task() could
146 * reattach a task to a different cpuset, which must not have its
147 * generation numbers aliased with those of that tasks previous cpuset.
149 * Generations are needed for mems_allowed because one task cannot
150 * modify anothers memory placement. So we must enable every task,
151 * on every visit to __alloc_pages(), to efficiently check whether
152 * its current->cpuset->mems_allowed has changed, requiring an update
153 * of its current->mems_allowed.
155 static atomic_t cpuset_mems_generation
= ATOMIC_INIT(1);
157 static struct cpuset top_cpuset
= {
158 .flags
= ((1 << CS_CPU_EXCLUSIVE
) | (1 << CS_MEM_EXCLUSIVE
)),
159 .cpus_allowed
= CPU_MASK_ALL
,
160 .mems_allowed
= NODE_MASK_ALL
,
161 .count
= ATOMIC_INIT(0),
162 .sibling
= LIST_HEAD_INIT(top_cpuset
.sibling
),
163 .children
= LIST_HEAD_INIT(top_cpuset
.children
),
166 static struct vfsmount
*cpuset_mount
;
167 static struct super_block
*cpuset_sb
;
170 * We have two global cpuset semaphores below. They can nest.
171 * It is ok to first take manage_sem, then nest callback_sem. We also
172 * require taking task_lock() when dereferencing a tasks cpuset pointer.
173 * See "The task_lock() exception", at the end of this comment.
175 * A task must hold both semaphores to modify cpusets. If a task
176 * holds manage_sem, then it blocks others wanting that semaphore,
177 * ensuring that it is the only task able to also acquire callback_sem
178 * and be able to modify cpusets. It can perform various checks on
179 * the cpuset structure first, knowing nothing will change. It can
180 * also allocate memory while just holding manage_sem. While it is
181 * performing these checks, various callback routines can briefly
182 * acquire callback_sem to query cpusets. Once it is ready to make
183 * the changes, it takes callback_sem, blocking everyone else.
185 * Calls to the kernel memory allocator can not be made while holding
186 * callback_sem, as that would risk double tripping on callback_sem
187 * from one of the callbacks into the cpuset code from within
190 * If a task is only holding callback_sem, then it has read-only
193 * The task_struct fields mems_allowed and mems_generation may only
194 * be accessed in the context of that task, so require no locks.
196 * Any task can increment and decrement the count field without lock.
197 * So in general, code holding manage_sem or callback_sem can't rely
198 * on the count field not changing. However, if the count goes to
199 * zero, then only attach_task(), which holds both semaphores, can
200 * increment it again. Because a count of zero means that no tasks
201 * are currently attached, therefore there is no way a task attached
202 * to that cpuset can fork (the other way to increment the count).
203 * So code holding manage_sem or callback_sem can safely assume that
204 * if the count is zero, it will stay zero. Similarly, if a task
205 * holds manage_sem or callback_sem on a cpuset with zero count, it
206 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
207 * both of those semaphores.
209 * A possible optimization to improve parallelism would be to make
210 * callback_sem a R/W semaphore (rwsem), allowing the callback routines
211 * to proceed in parallel, with read access, until the holder of
212 * manage_sem needed to take this rwsem for exclusive write access
213 * and modify some cpusets.
215 * The cpuset_common_file_write handler for operations that modify
216 * the cpuset hierarchy holds manage_sem across the entire operation,
217 * single threading all such cpuset modifications across the system.
219 * The cpuset_common_file_read() handlers only hold callback_sem across
220 * small pieces of code, such as when reading out possibly multi-word
221 * cpumasks and nodemasks.
223 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
224 * (usually) take either semaphore. These are the two most performance
225 * critical pieces of code here. The exception occurs on cpuset_exit(),
226 * when a task in a notify_on_release cpuset exits. Then manage_sem
227 * is taken, and if the cpuset count is zero, a usermode call made
228 * to /sbin/cpuset_release_agent with the name of the cpuset (path
229 * relative to the root of cpuset file system) as the argument.
231 * A cpuset can only be deleted if both its 'count' of using tasks
232 * is zero, and its list of 'children' cpusets is empty. Since all
233 * tasks in the system use _some_ cpuset, and since there is always at
234 * least one task in the system (init, pid == 1), therefore, top_cpuset
235 * always has either children cpusets and/or using tasks. So we don't
236 * need a special hack to ensure that top_cpuset cannot be deleted.
238 * The above "Tale of Two Semaphores" would be complete, but for:
240 * The task_lock() exception
242 * The need for this exception arises from the action of attach_task(),
243 * which overwrites one tasks cpuset pointer with another. It does
244 * so using both semaphores, however there are several performance
245 * critical places that need to reference task->cpuset without the
246 * expense of grabbing a system global semaphore. Therefore except as
247 * noted below, when dereferencing or, as in attach_task(), modifying
248 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
249 * (task->alloc_lock) already in the task_struct routinely used for
253 static DECLARE_MUTEX(manage_sem
);
254 static DECLARE_MUTEX(callback_sem
);
257 * A couple of forward declarations required, due to cyclic reference loop:
258 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
259 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
262 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
263 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
265 static struct backing_dev_info cpuset_backing_dev_info
= {
266 .ra_pages
= 0, /* No readahead */
267 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
270 static struct inode
*cpuset_new_inode(mode_t mode
)
272 struct inode
*inode
= new_inode(cpuset_sb
);
275 inode
->i_mode
= mode
;
276 inode
->i_uid
= current
->fsuid
;
277 inode
->i_gid
= current
->fsgid
;
278 inode
->i_blksize
= PAGE_CACHE_SIZE
;
280 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
281 inode
->i_mapping
->backing_dev_info
= &cpuset_backing_dev_info
;
286 static void cpuset_diput(struct dentry
*dentry
, struct inode
*inode
)
288 /* is dentry a directory ? if so, kfree() associated cpuset */
289 if (S_ISDIR(inode
->i_mode
)) {
290 struct cpuset
*cs
= dentry
->d_fsdata
;
291 BUG_ON(!(is_removed(cs
)));
297 static struct dentry_operations cpuset_dops
= {
298 .d_iput
= cpuset_diput
,
301 static struct dentry
*cpuset_get_dentry(struct dentry
*parent
, const char *name
)
303 struct dentry
*d
= lookup_one_len(name
, parent
, strlen(name
));
305 d
->d_op
= &cpuset_dops
;
309 static void remove_dir(struct dentry
*d
)
311 struct dentry
*parent
= dget(d
->d_parent
);
314 simple_rmdir(parent
->d_inode
, d
);
319 * NOTE : the dentry must have been dget()'ed
321 static void cpuset_d_remove_dir(struct dentry
*dentry
)
323 struct list_head
*node
;
325 spin_lock(&dcache_lock
);
326 node
= dentry
->d_subdirs
.next
;
327 while (node
!= &dentry
->d_subdirs
) {
328 struct dentry
*d
= list_entry(node
, struct dentry
, d_child
);
332 spin_unlock(&dcache_lock
);
334 simple_unlink(dentry
->d_inode
, d
);
336 spin_lock(&dcache_lock
);
338 node
= dentry
->d_subdirs
.next
;
340 list_del_init(&dentry
->d_child
);
341 spin_unlock(&dcache_lock
);
345 static struct super_operations cpuset_ops
= {
346 .statfs
= simple_statfs
,
347 .drop_inode
= generic_delete_inode
,
350 static int cpuset_fill_super(struct super_block
*sb
, void *unused_data
,
356 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
357 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
358 sb
->s_magic
= CPUSET_SUPER_MAGIC
;
359 sb
->s_op
= &cpuset_ops
;
362 inode
= cpuset_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
);
364 inode
->i_op
= &simple_dir_inode_operations
;
365 inode
->i_fop
= &simple_dir_operations
;
366 /* directories start off with i_nlink == 2 (for "." entry) */
372 root
= d_alloc_root(inode
);
381 static struct super_block
*cpuset_get_sb(struct file_system_type
*fs_type
,
382 int flags
, const char *unused_dev_name
,
385 return get_sb_single(fs_type
, flags
, data
, cpuset_fill_super
);
388 static struct file_system_type cpuset_fs_type
= {
390 .get_sb
= cpuset_get_sb
,
391 .kill_sb
= kill_litter_super
,
396 * The files in the cpuset filesystem mostly have a very simple read/write
397 * handling, some common function will take care of it. Nevertheless some cases
398 * (read tasks) are special and therefore I define this structure for every
402 * When reading/writing to a file:
403 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
404 * - the 'cftype' of the file is file->f_dentry->d_fsdata
410 int (*open
) (struct inode
*inode
, struct file
*file
);
411 ssize_t (*read
) (struct file
*file
, char __user
*buf
, size_t nbytes
,
413 int (*write
) (struct file
*file
, const char __user
*buf
, size_t nbytes
,
415 int (*release
) (struct inode
*inode
, struct file
*file
);
418 static inline struct cpuset
*__d_cs(struct dentry
*dentry
)
420 return dentry
->d_fsdata
;
423 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
425 return dentry
->d_fsdata
;
429 * Call with manage_sem held. Writes path of cpuset into buf.
430 * Returns 0 on success, -errno on error.
433 static int cpuset_path(const struct cpuset
*cs
, char *buf
, int buflen
)
437 start
= buf
+ buflen
;
441 int len
= cs
->dentry
->d_name
.len
;
442 if ((start
-= len
) < buf
)
443 return -ENAMETOOLONG
;
444 memcpy(start
, cs
->dentry
->d_name
.name
, len
);
451 return -ENAMETOOLONG
;
454 memmove(buf
, start
, buf
+ buflen
- start
);
459 * Notify userspace when a cpuset is released, by running
460 * /sbin/cpuset_release_agent with the name of the cpuset (path
461 * relative to the root of cpuset file system) as the argument.
463 * Most likely, this user command will try to rmdir this cpuset.
465 * This races with the possibility that some other task will be
466 * attached to this cpuset before it is removed, or that some other
467 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
468 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
469 * unused, and this cpuset will be reprieved from its death sentence,
470 * to continue to serve a useful existence. Next time it's released,
471 * we will get notified again, if it still has 'notify_on_release' set.
473 * The final arg to call_usermodehelper() is 0, which means don't
474 * wait. The separate /sbin/cpuset_release_agent task is forked by
475 * call_usermodehelper(), then control in this thread returns here,
476 * without waiting for the release agent task. We don't bother to
477 * wait because the caller of this routine has no use for the exit
478 * status of the /sbin/cpuset_release_agent task, so no sense holding
479 * our caller up for that.
481 * When we had only one cpuset semaphore, we had to call this
482 * without holding it, to avoid deadlock when call_usermodehelper()
483 * allocated memory. With two locks, we could now call this while
484 * holding manage_sem, but we still don't, so as to minimize
485 * the time manage_sem is held.
488 static void cpuset_release_agent(const char *pathbuf
)
490 char *argv
[3], *envp
[3];
497 argv
[i
++] = "/sbin/cpuset_release_agent";
498 argv
[i
++] = (char *)pathbuf
;
502 /* minimal command environment */
503 envp
[i
++] = "HOME=/";
504 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
507 call_usermodehelper(argv
[0], argv
, envp
, 0);
512 * Either cs->count of using tasks transitioned to zero, or the
513 * cs->children list of child cpusets just became empty. If this
514 * cs is notify_on_release() and now both the user count is zero and
515 * the list of children is empty, prepare cpuset path in a kmalloc'd
516 * buffer, to be returned via ppathbuf, so that the caller can invoke
517 * cpuset_release_agent() with it later on, once manage_sem is dropped.
518 * Call here with manage_sem held.
520 * This check_for_release() routine is responsible for kmalloc'ing
521 * pathbuf. The above cpuset_release_agent() is responsible for
522 * kfree'ing pathbuf. The caller of these routines is responsible
523 * for providing a pathbuf pointer, initialized to NULL, then
524 * calling check_for_release() with manage_sem held and the address
525 * of the pathbuf pointer, then dropping manage_sem, then calling
526 * cpuset_release_agent() with pathbuf, as set by check_for_release().
529 static void check_for_release(struct cpuset
*cs
, char **ppathbuf
)
531 if (notify_on_release(cs
) && atomic_read(&cs
->count
) == 0 &&
532 list_empty(&cs
->children
)) {
535 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
538 if (cpuset_path(cs
, buf
, PAGE_SIZE
) < 0)
546 * Return in *pmask the portion of a cpusets's cpus_allowed that
547 * are online. If none are online, walk up the cpuset hierarchy
548 * until we find one that does have some online cpus. If we get
549 * all the way to the top and still haven't found any online cpus,
550 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
551 * task, return cpu_online_map.
553 * One way or another, we guarantee to return some non-empty subset
556 * Call with callback_sem held.
559 static void guarantee_online_cpus(const struct cpuset
*cs
, cpumask_t
*pmask
)
561 while (cs
&& !cpus_intersects(cs
->cpus_allowed
, cpu_online_map
))
564 cpus_and(*pmask
, cs
->cpus_allowed
, cpu_online_map
);
566 *pmask
= cpu_online_map
;
567 BUG_ON(!cpus_intersects(*pmask
, cpu_online_map
));
571 * Return in *pmask the portion of a cpusets's mems_allowed that
572 * are online. If none are online, walk up the cpuset hierarchy
573 * until we find one that does have some online mems. If we get
574 * all the way to the top and still haven't found any online mems,
575 * return node_online_map.
577 * One way or another, we guarantee to return some non-empty subset
578 * of node_online_map.
580 * Call with callback_sem held.
583 static void guarantee_online_mems(const struct cpuset
*cs
, nodemask_t
*pmask
)
585 while (cs
&& !nodes_intersects(cs
->mems_allowed
, node_online_map
))
588 nodes_and(*pmask
, cs
->mems_allowed
, node_online_map
);
590 *pmask
= node_online_map
;
591 BUG_ON(!nodes_intersects(*pmask
, node_online_map
));
595 * cpuset_update_task_memory_state - update task memory placement
597 * If the current tasks cpusets mems_allowed changed behind our
598 * backs, update current->mems_allowed, mems_generation and task NUMA
599 * mempolicy to the new value.
601 * Task mempolicy is updated by rebinding it relative to the
602 * current->cpuset if a task has its memory placement changed.
603 * Do not call this routine if in_interrupt().
605 * Call without callback_sem or task_lock() held. May be called
606 * with or without manage_sem held. Except in early boot or
607 * an exiting task, when tsk->cpuset is NULL, this routine will
608 * acquire task_lock(). We don't need to use task_lock to guard
609 * against another task changing a non-NULL cpuset pointer to NULL,
610 * as that is only done by a task on itself, and if the current task
611 * is here, it is not simultaneously in the exit code NULL'ing its
612 * cpuset pointer. This routine also might acquire callback_sem and
613 * current->mm->mmap_sem during call.
615 * The task_lock() is required to dereference current->cpuset safely.
616 * Without it, we could pick up the pointer value of current->cpuset
617 * in one instruction, and then attach_task could give us a different
618 * cpuset, and then the cpuset we had could be removed and freed,
619 * and then on our next instruction, we could dereference a no longer
620 * valid cpuset pointer to get its mems_generation field.
622 * This routine is needed to update the per-task mems_allowed data,
623 * within the tasks context, when it is trying to allocate memory
624 * (in various mm/mempolicy.c routines) and notices that some other
625 * task has been modifying its cpuset.
628 void cpuset_update_task_memory_state()
630 int my_cpusets_mem_gen
;
631 struct task_struct
*tsk
= current
;
632 struct cpuset
*cs
= tsk
->cpuset
;
638 my_cpusets_mem_gen
= cs
->mems_generation
;
641 if (my_cpusets_mem_gen
!= tsk
->cpuset_mems_generation
) {
644 cs
= tsk
->cpuset
; /* Maybe changed when task not locked */
645 guarantee_online_mems(cs
, &tsk
->mems_allowed
);
646 tsk
->cpuset_mems_generation
= cs
->mems_generation
;
649 mpol_rebind_task(tsk
, &tsk
->mems_allowed
);
654 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
656 * One cpuset is a subset of another if all its allowed CPUs and
657 * Memory Nodes are a subset of the other, and its exclusive flags
658 * are only set if the other's are set. Call holding manage_sem.
661 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
663 return cpus_subset(p
->cpus_allowed
, q
->cpus_allowed
) &&
664 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
665 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
666 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
670 * validate_change() - Used to validate that any proposed cpuset change
671 * follows the structural rules for cpusets.
673 * If we replaced the flag and mask values of the current cpuset
674 * (cur) with those values in the trial cpuset (trial), would
675 * our various subset and exclusive rules still be valid? Presumes
678 * 'cur' is the address of an actual, in-use cpuset. Operations
679 * such as list traversal that depend on the actual address of the
680 * cpuset in the list must use cur below, not trial.
682 * 'trial' is the address of bulk structure copy of cur, with
683 * perhaps one or more of the fields cpus_allowed, mems_allowed,
684 * or flags changed to new, trial values.
686 * Return 0 if valid, -errno if not.
689 static int validate_change(const struct cpuset
*cur
, const struct cpuset
*trial
)
691 struct cpuset
*c
, *par
;
693 /* Each of our child cpusets must be a subset of us */
694 list_for_each_entry(c
, &cur
->children
, sibling
) {
695 if (!is_cpuset_subset(c
, trial
))
699 /* Remaining checks don't apply to root cpuset */
700 if ((par
= cur
->parent
) == NULL
)
703 /* We must be a subset of our parent cpuset */
704 if (!is_cpuset_subset(trial
, par
))
707 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
708 list_for_each_entry(c
, &par
->children
, sibling
) {
709 if ((is_cpu_exclusive(trial
) || is_cpu_exclusive(c
)) &&
711 cpus_intersects(trial
->cpus_allowed
, c
->cpus_allowed
))
713 if ((is_mem_exclusive(trial
) || is_mem_exclusive(c
)) &&
715 nodes_intersects(trial
->mems_allowed
, c
->mems_allowed
))
723 * For a given cpuset cur, partition the system as follows
724 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
725 * exclusive child cpusets
726 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
727 * exclusive child cpusets
728 * Build these two partitions by calling partition_sched_domains
730 * Call with manage_sem held. May nest a call to the
731 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
734 static void update_cpu_domains(struct cpuset
*cur
)
736 struct cpuset
*c
, *par
= cur
->parent
;
737 cpumask_t pspan
, cspan
;
739 if (par
== NULL
|| cpus_empty(cur
->cpus_allowed
))
743 * Get all cpus from parent's cpus_allowed not part of exclusive
746 pspan
= par
->cpus_allowed
;
747 list_for_each_entry(c
, &par
->children
, sibling
) {
748 if (is_cpu_exclusive(c
))
749 cpus_andnot(pspan
, pspan
, c
->cpus_allowed
);
751 if (is_removed(cur
) || !is_cpu_exclusive(cur
)) {
752 cpus_or(pspan
, pspan
, cur
->cpus_allowed
);
753 if (cpus_equal(pspan
, cur
->cpus_allowed
))
755 cspan
= CPU_MASK_NONE
;
757 if (cpus_empty(pspan
))
759 cspan
= cur
->cpus_allowed
;
761 * Get all cpus from current cpuset's cpus_allowed not part
762 * of exclusive children
764 list_for_each_entry(c
, &cur
->children
, sibling
) {
765 if (is_cpu_exclusive(c
))
766 cpus_andnot(cspan
, cspan
, c
->cpus_allowed
);
771 partition_sched_domains(&pspan
, &cspan
);
772 unlock_cpu_hotplug();
776 * Call with manage_sem held. May take callback_sem during call.
779 static int update_cpumask(struct cpuset
*cs
, char *buf
)
781 struct cpuset trialcs
;
782 int retval
, cpus_unchanged
;
785 retval
= cpulist_parse(buf
, trialcs
.cpus_allowed
);
788 cpus_and(trialcs
.cpus_allowed
, trialcs
.cpus_allowed
, cpu_online_map
);
789 if (cpus_empty(trialcs
.cpus_allowed
))
791 retval
= validate_change(cs
, &trialcs
);
794 cpus_unchanged
= cpus_equal(cs
->cpus_allowed
, trialcs
.cpus_allowed
);
796 cs
->cpus_allowed
= trialcs
.cpus_allowed
;
798 if (is_cpu_exclusive(cs
) && !cpus_unchanged
)
799 update_cpu_domains(cs
);
804 * Handle user request to change the 'mems' memory placement
805 * of a cpuset. Needs to validate the request, update the
806 * cpusets mems_allowed and mems_generation, and for each
807 * task in the cpuset, rebind any vma mempolicies and if
808 * the cpuset is marked 'memory_migrate', migrate the tasks
809 * pages to the new memory.
811 * Call with manage_sem held. May take callback_sem during call.
812 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
813 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
814 * their mempolicies to the cpusets new mems_allowed.
817 static int update_nodemask(struct cpuset
*cs
, char *buf
)
819 struct cpuset trialcs
;
821 struct task_struct
*g
, *p
;
822 struct mm_struct
**mmarray
;
829 retval
= nodelist_parse(buf
, trialcs
.mems_allowed
);
832 nodes_and(trialcs
.mems_allowed
, trialcs
.mems_allowed
, node_online_map
);
833 oldmem
= cs
->mems_allowed
;
834 if (nodes_equal(oldmem
, trialcs
.mems_allowed
)) {
835 retval
= 0; /* Too easy - nothing to do */
838 if (nodes_empty(trialcs
.mems_allowed
)) {
842 retval
= validate_change(cs
, &trialcs
);
847 cs
->mems_allowed
= trialcs
.mems_allowed
;
848 atomic_inc(&cpuset_mems_generation
);
849 cs
->mems_generation
= atomic_read(&cpuset_mems_generation
);
852 set_cpuset_being_rebound(cs
); /* causes mpol_copy() rebind */
854 fudge
= 10; /* spare mmarray[] slots */
855 fudge
+= cpus_weight(cs
->cpus_allowed
); /* imagine one fork-bomb/cpu */
859 * Allocate mmarray[] to hold mm reference for each task
860 * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
861 * tasklist_lock. We could use GFP_ATOMIC, but with a
862 * few more lines of code, we can retry until we get a big
863 * enough mmarray[] w/o using GFP_ATOMIC.
866 ntasks
= atomic_read(&cs
->count
); /* guess */
868 mmarray
= kmalloc(ntasks
* sizeof(*mmarray
), GFP_KERNEL
);
871 write_lock_irq(&tasklist_lock
); /* block fork */
872 if (atomic_read(&cs
->count
) <= ntasks
)
873 break; /* got enough */
874 write_unlock_irq(&tasklist_lock
); /* try again */
880 /* Load up mmarray[] with mm reference for each task in cpuset. */
881 do_each_thread(g
, p
) {
882 struct mm_struct
*mm
;
886 "Cpuset mempolicy rebind incomplete.\n");
895 } while_each_thread(g
, p
);
896 write_unlock_irq(&tasklist_lock
);
899 * Now that we've dropped the tasklist spinlock, we can
900 * rebind the vma mempolicies of each mm in mmarray[] to their
901 * new cpuset, and release that mm. The mpol_rebind_mm()
902 * call takes mmap_sem, which we couldn't take while holding
903 * tasklist_lock. Forks can happen again now - the mpol_copy()
904 * cpuset_being_rebound check will catch such forks, and rebind
905 * their vma mempolicies too. Because we still hold the global
906 * cpuset manage_sem, we know that no other rebind effort will
907 * be contending for the global variable cpuset_being_rebound.
908 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
909 * is idempotent. Also migrate pages in each mm to new nodes.
911 migrate
= is_memory_migrate(cs
);
912 for (i
= 0; i
< n
; i
++) {
913 struct mm_struct
*mm
= mmarray
[i
];
915 mpol_rebind_mm(mm
, &cs
->mems_allowed
);
917 do_migrate_pages(mm
, &oldmem
, &cs
->mems_allowed
,
923 /* We're done rebinding vma's to this cpusets new mems_allowed. */
925 set_cpuset_being_rebound(NULL
);
932 * Call with manage_sem held.
935 static int update_memory_pressure_enabled(struct cpuset
*cs
, char *buf
)
937 if (simple_strtoul(buf
, NULL
, 10) != 0)
938 cpuset_memory_pressure_enabled
= 1;
940 cpuset_memory_pressure_enabled
= 0;
945 * update_flag - read a 0 or a 1 in a file and update associated flag
946 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
947 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE)
948 * cs: the cpuset to update
949 * buf: the buffer where we read the 0 or 1
951 * Call with manage_sem held.
954 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
, char *buf
)
957 struct cpuset trialcs
;
958 int err
, cpu_exclusive_changed
;
960 turning_on
= (simple_strtoul(buf
, NULL
, 10) != 0);
964 set_bit(bit
, &trialcs
.flags
);
966 clear_bit(bit
, &trialcs
.flags
);
968 err
= validate_change(cs
, &trialcs
);
971 cpu_exclusive_changed
=
972 (is_cpu_exclusive(cs
) != is_cpu_exclusive(&trialcs
));
975 set_bit(bit
, &cs
->flags
);
977 clear_bit(bit
, &cs
->flags
);
980 if (cpu_exclusive_changed
)
981 update_cpu_domains(cs
);
986 * Frequency meter - How fast is some event occuring?
988 * These routines manage a digitally filtered, constant time based,
989 * event frequency meter. There are four routines:
990 * fmeter_init() - initialize a frequency meter.
991 * fmeter_markevent() - called each time the event happens.
992 * fmeter_getrate() - returns the recent rate of such events.
993 * fmeter_update() - internal routine used to update fmeter.
995 * A common data structure is passed to each of these routines,
996 * which is used to keep track of the state required to manage the
997 * frequency meter and its digital filter.
999 * The filter works on the number of events marked per unit time.
1000 * The filter is single-pole low-pass recursive (IIR). The time unit
1001 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1002 * simulate 3 decimal digits of precision (multiplied by 1000).
1004 * With an FM_COEF of 933, and a time base of 1 second, the filter
1005 * has a half-life of 10 seconds, meaning that if the events quit
1006 * happening, then the rate returned from the fmeter_getrate()
1007 * will be cut in half each 10 seconds, until it converges to zero.
1009 * It is not worth doing a real infinitely recursive filter. If more
1010 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1011 * just compute FM_MAXTICKS ticks worth, by which point the level
1014 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1015 * arithmetic overflow in the fmeter_update() routine.
1017 * Given the simple 32 bit integer arithmetic used, this meter works
1018 * best for reporting rates between one per millisecond (msec) and
1019 * one per 32 (approx) seconds. At constant rates faster than one
1020 * per msec it maxes out at values just under 1,000,000. At constant
1021 * rates between one per msec, and one per second it will stabilize
1022 * to a value N*1000, where N is the rate of events per second.
1023 * At constant rates between one per second and one per 32 seconds,
1024 * it will be choppy, moving up on the seconds that have an event,
1025 * and then decaying until the next event. At rates slower than
1026 * about one in 32 seconds, it decays all the way back to zero between
1030 #define FM_COEF 933 /* coefficient for half-life of 10 secs */
1031 #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1032 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1033 #define FM_SCALE 1000 /* faux fixed point scale */
1035 /* Initialize a frequency meter */
1036 static void fmeter_init(struct fmeter
*fmp
)
1041 spin_lock_init(&fmp
->lock
);
1044 /* Internal meter update - process cnt events and update value */
1045 static void fmeter_update(struct fmeter
*fmp
)
1047 time_t now
= get_seconds();
1048 time_t ticks
= now
- fmp
->time
;
1053 ticks
= min(FM_MAXTICKS
, ticks
);
1055 fmp
->val
= (FM_COEF
* fmp
->val
) / FM_SCALE
;
1058 fmp
->val
+= ((FM_SCALE
- FM_COEF
) * fmp
->cnt
) / FM_SCALE
;
1062 /* Process any previous ticks, then bump cnt by one (times scale). */
1063 static void fmeter_markevent(struct fmeter
*fmp
)
1065 spin_lock(&fmp
->lock
);
1067 fmp
->cnt
= min(FM_MAXCNT
, fmp
->cnt
+ FM_SCALE
);
1068 spin_unlock(&fmp
->lock
);
1071 /* Process any previous ticks, then return current value. */
1072 static int fmeter_getrate(struct fmeter
*fmp
)
1076 spin_lock(&fmp
->lock
);
1079 spin_unlock(&fmp
->lock
);
1084 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
1085 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1086 * notified on release.
1088 * Call holding manage_sem. May take callback_sem and task_lock of
1089 * the task 'pid' during call.
1092 static int attach_task(struct cpuset
*cs
, char *pidbuf
, char **ppathbuf
)
1095 struct task_struct
*tsk
;
1096 struct cpuset
*oldcs
;
1098 nodemask_t from
, to
;
1099 struct mm_struct
*mm
;
1101 if (sscanf(pidbuf
, "%d", &pid
) != 1)
1103 if (cpus_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
))
1107 read_lock(&tasklist_lock
);
1109 tsk
= find_task_by_pid(pid
);
1110 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1111 read_unlock(&tasklist_lock
);
1115 get_task_struct(tsk
);
1116 read_unlock(&tasklist_lock
);
1118 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
1119 && (current
->euid
!= tsk
->suid
)) {
1120 put_task_struct(tsk
);
1125 get_task_struct(tsk
);
1128 down(&callback_sem
);
1131 oldcs
= tsk
->cpuset
;
1135 put_task_struct(tsk
);
1138 atomic_inc(&cs
->count
);
1142 guarantee_online_cpus(cs
, &cpus
);
1143 set_cpus_allowed(tsk
, cpus
);
1145 from
= oldcs
->mems_allowed
;
1146 to
= cs
->mems_allowed
;
1150 mm
= get_task_mm(tsk
);
1152 mpol_rebind_mm(mm
, &to
);
1156 if (is_memory_migrate(cs
))
1157 do_migrate_pages(tsk
->mm
, &from
, &to
, MPOL_MF_MOVE_ALL
);
1158 put_task_struct(tsk
);
1159 if (atomic_dec_and_test(&oldcs
->count
))
1160 check_for_release(oldcs
, ppathbuf
);
1164 /* The various types of files and directories in a cpuset file system */
1169 FILE_MEMORY_MIGRATE
,
1174 FILE_NOTIFY_ON_RELEASE
,
1175 FILE_MEMORY_PRESSURE_ENABLED
,
1176 FILE_MEMORY_PRESSURE
,
1178 } cpuset_filetype_t
;
1180 static ssize_t
cpuset_common_file_write(struct file
*file
, const char __user
*userbuf
,
1181 size_t nbytes
, loff_t
*unused_ppos
)
1183 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1184 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1185 cpuset_filetype_t type
= cft
->private;
1187 char *pathbuf
= NULL
;
1190 /* Crude upper limit on largest legitimate cpulist user might write. */
1191 if (nbytes
> 100 + 6 * NR_CPUS
)
1194 /* +1 for nul-terminator */
1195 if ((buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
)) == 0)
1198 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
1202 buffer
[nbytes
] = 0; /* nul-terminate */
1206 if (is_removed(cs
)) {
1213 retval
= update_cpumask(cs
, buffer
);
1216 retval
= update_nodemask(cs
, buffer
);
1218 case FILE_CPU_EXCLUSIVE
:
1219 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, buffer
);
1221 case FILE_MEM_EXCLUSIVE
:
1222 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, buffer
);
1224 case FILE_NOTIFY_ON_RELEASE
:
1225 retval
= update_flag(CS_NOTIFY_ON_RELEASE
, cs
, buffer
);
1227 case FILE_MEMORY_MIGRATE
:
1228 retval
= update_flag(CS_MEMORY_MIGRATE
, cs
, buffer
);
1230 case FILE_MEMORY_PRESSURE_ENABLED
:
1231 retval
= update_memory_pressure_enabled(cs
, buffer
);
1233 case FILE_MEMORY_PRESSURE
:
1237 retval
= attach_task(cs
, buffer
, &pathbuf
);
1248 cpuset_release_agent(pathbuf
);
1254 static ssize_t
cpuset_file_write(struct file
*file
, const char __user
*buf
,
1255 size_t nbytes
, loff_t
*ppos
)
1258 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1262 /* special function ? */
1264 retval
= cft
->write(file
, buf
, nbytes
, ppos
);
1266 retval
= cpuset_common_file_write(file
, buf
, nbytes
, ppos
);
1272 * These ascii lists should be read in a single call, by using a user
1273 * buffer large enough to hold the entire map. If read in smaller
1274 * chunks, there is no guarantee of atomicity. Since the display format
1275 * used, list of ranges of sequential numbers, is variable length,
1276 * and since these maps can change value dynamically, one could read
1277 * gibberish by doing partial reads while a list was changing.
1278 * A single large read to a buffer that crosses a page boundary is
1279 * ok, because the result being copied to user land is not recomputed
1280 * across a page fault.
1283 static int cpuset_sprintf_cpulist(char *page
, struct cpuset
*cs
)
1287 down(&callback_sem
);
1288 mask
= cs
->cpus_allowed
;
1291 return cpulist_scnprintf(page
, PAGE_SIZE
, mask
);
1294 static int cpuset_sprintf_memlist(char *page
, struct cpuset
*cs
)
1298 down(&callback_sem
);
1299 mask
= cs
->mems_allowed
;
1302 return nodelist_scnprintf(page
, PAGE_SIZE
, mask
);
1305 static ssize_t
cpuset_common_file_read(struct file
*file
, char __user
*buf
,
1306 size_t nbytes
, loff_t
*ppos
)
1308 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1309 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1310 cpuset_filetype_t type
= cft
->private;
1315 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
1322 s
+= cpuset_sprintf_cpulist(s
, cs
);
1325 s
+= cpuset_sprintf_memlist(s
, cs
);
1327 case FILE_CPU_EXCLUSIVE
:
1328 *s
++ = is_cpu_exclusive(cs
) ? '1' : '0';
1330 case FILE_MEM_EXCLUSIVE
:
1331 *s
++ = is_mem_exclusive(cs
) ? '1' : '0';
1333 case FILE_NOTIFY_ON_RELEASE
:
1334 *s
++ = notify_on_release(cs
) ? '1' : '0';
1336 case FILE_MEMORY_MIGRATE
:
1337 *s
++ = is_memory_migrate(cs
) ? '1' : '0';
1339 case FILE_MEMORY_PRESSURE_ENABLED
:
1340 *s
++ = cpuset_memory_pressure_enabled
? '1' : '0';
1342 case FILE_MEMORY_PRESSURE
:
1343 s
+= sprintf(s
, "%d", fmeter_getrate(&cs
->fmeter
));
1351 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1353 free_page((unsigned long)page
);
1357 static ssize_t
cpuset_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1361 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1365 /* special function ? */
1367 retval
= cft
->read(file
, buf
, nbytes
, ppos
);
1369 retval
= cpuset_common_file_read(file
, buf
, nbytes
, ppos
);
1374 static int cpuset_file_open(struct inode
*inode
, struct file
*file
)
1379 err
= generic_file_open(inode
, file
);
1383 cft
= __d_cft(file
->f_dentry
);
1387 err
= cft
->open(inode
, file
);
1394 static int cpuset_file_release(struct inode
*inode
, struct file
*file
)
1396 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1398 return cft
->release(inode
, file
);
1403 * cpuset_rename - Only allow simple rename of directories in place.
1405 static int cpuset_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1406 struct inode
*new_dir
, struct dentry
*new_dentry
)
1408 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1410 if (new_dentry
->d_inode
)
1412 if (old_dir
!= new_dir
)
1414 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1417 static struct file_operations cpuset_file_operations
= {
1418 .read
= cpuset_file_read
,
1419 .write
= cpuset_file_write
,
1420 .llseek
= generic_file_llseek
,
1421 .open
= cpuset_file_open
,
1422 .release
= cpuset_file_release
,
1425 static struct inode_operations cpuset_dir_inode_operations
= {
1426 .lookup
= simple_lookup
,
1427 .mkdir
= cpuset_mkdir
,
1428 .rmdir
= cpuset_rmdir
,
1429 .rename
= cpuset_rename
,
1432 static int cpuset_create_file(struct dentry
*dentry
, int mode
)
1434 struct inode
*inode
;
1438 if (dentry
->d_inode
)
1441 inode
= cpuset_new_inode(mode
);
1445 if (S_ISDIR(mode
)) {
1446 inode
->i_op
= &cpuset_dir_inode_operations
;
1447 inode
->i_fop
= &simple_dir_operations
;
1449 /* start off with i_nlink == 2 (for "." entry) */
1451 } else if (S_ISREG(mode
)) {
1453 inode
->i_fop
= &cpuset_file_operations
;
1456 d_instantiate(dentry
, inode
);
1457 dget(dentry
); /* Extra count - pin the dentry in core */
1462 * cpuset_create_dir - create a directory for an object.
1463 * cs: the cpuset we create the directory for.
1464 * It must have a valid ->parent field
1465 * And we are going to fill its ->dentry field.
1466 * name: The name to give to the cpuset directory. Will be copied.
1467 * mode: mode to set on new directory.
1470 static int cpuset_create_dir(struct cpuset
*cs
, const char *name
, int mode
)
1472 struct dentry
*dentry
= NULL
;
1473 struct dentry
*parent
;
1476 parent
= cs
->parent
->dentry
;
1477 dentry
= cpuset_get_dentry(parent
, name
);
1479 return PTR_ERR(dentry
);
1480 error
= cpuset_create_file(dentry
, S_IFDIR
| mode
);
1482 dentry
->d_fsdata
= cs
;
1483 parent
->d_inode
->i_nlink
++;
1484 cs
->dentry
= dentry
;
1491 static int cpuset_add_file(struct dentry
*dir
, const struct cftype
*cft
)
1493 struct dentry
*dentry
;
1496 down(&dir
->d_inode
->i_sem
);
1497 dentry
= cpuset_get_dentry(dir
, cft
->name
);
1498 if (!IS_ERR(dentry
)) {
1499 error
= cpuset_create_file(dentry
, 0644 | S_IFREG
);
1501 dentry
->d_fsdata
= (void *)cft
;
1504 error
= PTR_ERR(dentry
);
1505 up(&dir
->d_inode
->i_sem
);
1510 * Stuff for reading the 'tasks' file.
1512 * Reading this file can return large amounts of data if a cpuset has
1513 * *lots* of attached tasks. So it may need several calls to read(),
1514 * but we cannot guarantee that the information we produce is correct
1515 * unless we produce it entirely atomically.
1517 * Upon tasks file open(), a struct ctr_struct is allocated, that
1518 * will have a pointer to an array (also allocated here). The struct
1519 * ctr_struct * is stored in file->private_data. Its resources will
1520 * be freed by release() when the file is closed. The array is used
1521 * to sprintf the PIDs and then used by read().
1524 /* cpusets_tasks_read array */
1532 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1533 * Return actual number of pids loaded. No need to task_lock(p)
1534 * when reading out p->cpuset, as we don't really care if it changes
1535 * on the next cycle, and we are not going to try to dereference it.
1537 static inline int pid_array_load(pid_t
*pidarray
, int npids
, struct cpuset
*cs
)
1540 struct task_struct
*g
, *p
;
1542 read_lock(&tasklist_lock
);
1544 do_each_thread(g
, p
) {
1545 if (p
->cpuset
== cs
) {
1546 pidarray
[n
++] = p
->pid
;
1547 if (unlikely(n
== npids
))
1550 } while_each_thread(g
, p
);
1553 read_unlock(&tasklist_lock
);
1557 static int cmppid(const void *a
, const void *b
)
1559 return *(pid_t
*)a
- *(pid_t
*)b
;
1563 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1564 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1565 * count 'cnt' of how many chars would be written if buf were large enough.
1567 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1572 for (i
= 0; i
< npids
; i
++)
1573 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1578 * Handle an open on 'tasks' file. Prepare a buffer listing the
1579 * process id's of tasks currently attached to the cpuset being opened.
1581 * Does not require any specific cpuset semaphores, and does not take any.
1583 static int cpuset_tasks_open(struct inode
*unused
, struct file
*file
)
1585 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1586 struct ctr_struct
*ctr
;
1591 if (!(file
->f_mode
& FMODE_READ
))
1594 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1599 * If cpuset gets more users after we read count, we won't have
1600 * enough space - tough. This race is indistinguishable to the
1601 * caller from the case that the additional cpuset users didn't
1602 * show up until sometime later on.
1604 npids
= atomic_read(&cs
->count
);
1605 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1609 npids
= pid_array_load(pidarray
, npids
, cs
);
1610 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1612 /* Call pid_array_to_buf() twice, first just to get bufsz */
1613 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1614 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1617 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1620 file
->private_data
= ctr
;
1631 static ssize_t
cpuset_tasks_read(struct file
*file
, char __user
*buf
,
1632 size_t nbytes
, loff_t
*ppos
)
1634 struct ctr_struct
*ctr
= file
->private_data
;
1636 if (*ppos
+ nbytes
> ctr
->bufsz
)
1637 nbytes
= ctr
->bufsz
- *ppos
;
1638 if (copy_to_user(buf
, ctr
->buf
+ *ppos
, nbytes
))
1644 static int cpuset_tasks_release(struct inode
*unused_inode
, struct file
*file
)
1646 struct ctr_struct
*ctr
;
1648 if (file
->f_mode
& FMODE_READ
) {
1649 ctr
= file
->private_data
;
1657 * for the common functions, 'private' gives the type of file
1660 static struct cftype cft_tasks
= {
1662 .open
= cpuset_tasks_open
,
1663 .read
= cpuset_tasks_read
,
1664 .release
= cpuset_tasks_release
,
1665 .private = FILE_TASKLIST
,
1668 static struct cftype cft_cpus
= {
1670 .private = FILE_CPULIST
,
1673 static struct cftype cft_mems
= {
1675 .private = FILE_MEMLIST
,
1678 static struct cftype cft_cpu_exclusive
= {
1679 .name
= "cpu_exclusive",
1680 .private = FILE_CPU_EXCLUSIVE
,
1683 static struct cftype cft_mem_exclusive
= {
1684 .name
= "mem_exclusive",
1685 .private = FILE_MEM_EXCLUSIVE
,
1688 static struct cftype cft_notify_on_release
= {
1689 .name
= "notify_on_release",
1690 .private = FILE_NOTIFY_ON_RELEASE
,
1693 static struct cftype cft_memory_migrate
= {
1694 .name
= "memory_migrate",
1695 .private = FILE_MEMORY_MIGRATE
,
1698 static struct cftype cft_memory_pressure_enabled
= {
1699 .name
= "memory_pressure_enabled",
1700 .private = FILE_MEMORY_PRESSURE_ENABLED
,
1703 static struct cftype cft_memory_pressure
= {
1704 .name
= "memory_pressure",
1705 .private = FILE_MEMORY_PRESSURE
,
1708 static int cpuset_populate_dir(struct dentry
*cs_dentry
)
1712 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpus
)) < 0)
1714 if ((err
= cpuset_add_file(cs_dentry
, &cft_mems
)) < 0)
1716 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpu_exclusive
)) < 0)
1718 if ((err
= cpuset_add_file(cs_dentry
, &cft_mem_exclusive
)) < 0)
1720 if ((err
= cpuset_add_file(cs_dentry
, &cft_notify_on_release
)) < 0)
1722 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_migrate
)) < 0)
1724 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_pressure
)) < 0)
1726 if ((err
= cpuset_add_file(cs_dentry
, &cft_tasks
)) < 0)
1732 * cpuset_create - create a cpuset
1733 * parent: cpuset that will be parent of the new cpuset.
1734 * name: name of the new cpuset. Will be strcpy'ed.
1735 * mode: mode to set on new inode
1737 * Must be called with the semaphore on the parent inode held
1740 static long cpuset_create(struct cpuset
*parent
, const char *name
, int mode
)
1745 cs
= kmalloc(sizeof(*cs
), GFP_KERNEL
);
1750 cpuset_update_task_memory_state();
1752 if (notify_on_release(parent
))
1753 set_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
1754 cs
->cpus_allowed
= CPU_MASK_NONE
;
1755 cs
->mems_allowed
= NODE_MASK_NONE
;
1756 atomic_set(&cs
->count
, 0);
1757 INIT_LIST_HEAD(&cs
->sibling
);
1758 INIT_LIST_HEAD(&cs
->children
);
1759 atomic_inc(&cpuset_mems_generation
);
1760 cs
->mems_generation
= atomic_read(&cpuset_mems_generation
);
1761 fmeter_init(&cs
->fmeter
);
1763 cs
->parent
= parent
;
1765 down(&callback_sem
);
1766 list_add(&cs
->sibling
, &cs
->parent
->children
);
1767 number_of_cpusets
++;
1770 err
= cpuset_create_dir(cs
, name
, mode
);
1775 * Release manage_sem before cpuset_populate_dir() because it
1776 * will down() this new directory's i_sem and if we race with
1777 * another mkdir, we might deadlock.
1781 err
= cpuset_populate_dir(cs
->dentry
);
1782 /* If err < 0, we have a half-filled directory - oh well ;) */
1785 list_del(&cs
->sibling
);
1791 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1793 struct cpuset
*c_parent
= dentry
->d_parent
->d_fsdata
;
1795 /* the vfs holds inode->i_sem already */
1796 return cpuset_create(c_parent
, dentry
->d_name
.name
, mode
| S_IFDIR
);
1799 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
1801 struct cpuset
*cs
= dentry
->d_fsdata
;
1803 struct cpuset
*parent
;
1804 char *pathbuf
= NULL
;
1806 /* the vfs holds both inode->i_sem already */
1809 cpuset_update_task_memory_state();
1810 if (atomic_read(&cs
->count
) > 0) {
1814 if (!list_empty(&cs
->children
)) {
1818 parent
= cs
->parent
;
1819 down(&callback_sem
);
1820 set_bit(CS_REMOVED
, &cs
->flags
);
1821 if (is_cpu_exclusive(cs
))
1822 update_cpu_domains(cs
);
1823 list_del(&cs
->sibling
); /* delete my sibling from parent->children */
1824 spin_lock(&cs
->dentry
->d_lock
);
1825 d
= dget(cs
->dentry
);
1827 spin_unlock(&d
->d_lock
);
1828 cpuset_d_remove_dir(d
);
1830 number_of_cpusets
--;
1832 if (list_empty(&parent
->children
))
1833 check_for_release(parent
, &pathbuf
);
1835 cpuset_release_agent(pathbuf
);
1840 * cpuset_init - initialize cpusets at system boot
1842 * Description: Initialize top_cpuset and the cpuset internal file system,
1845 int __init
cpuset_init(void)
1847 struct dentry
*root
;
1850 top_cpuset
.cpus_allowed
= CPU_MASK_ALL
;
1851 top_cpuset
.mems_allowed
= NODE_MASK_ALL
;
1853 fmeter_init(&top_cpuset
.fmeter
);
1854 atomic_inc(&cpuset_mems_generation
);
1855 top_cpuset
.mems_generation
= atomic_read(&cpuset_mems_generation
);
1857 init_task
.cpuset
= &top_cpuset
;
1859 err
= register_filesystem(&cpuset_fs_type
);
1862 cpuset_mount
= kern_mount(&cpuset_fs_type
);
1863 if (IS_ERR(cpuset_mount
)) {
1864 printk(KERN_ERR
"cpuset: could not mount!\n");
1865 err
= PTR_ERR(cpuset_mount
);
1866 cpuset_mount
= NULL
;
1869 root
= cpuset_mount
->mnt_sb
->s_root
;
1870 root
->d_fsdata
= &top_cpuset
;
1871 root
->d_inode
->i_nlink
++;
1872 top_cpuset
.dentry
= root
;
1873 root
->d_inode
->i_op
= &cpuset_dir_inode_operations
;
1874 number_of_cpusets
= 1;
1875 err
= cpuset_populate_dir(root
);
1876 /* memory_pressure_enabled is in root cpuset only */
1878 err
= cpuset_add_file(root
, &cft_memory_pressure_enabled
);
1884 * cpuset_init_smp - initialize cpus_allowed
1886 * Description: Finish top cpuset after cpu, node maps are initialized
1889 void __init
cpuset_init_smp(void)
1891 top_cpuset
.cpus_allowed
= cpu_online_map
;
1892 top_cpuset
.mems_allowed
= node_online_map
;
1896 * cpuset_fork - attach newly forked task to its parents cpuset.
1897 * @tsk: pointer to task_struct of forking parent process.
1899 * Description: A task inherits its parent's cpuset at fork().
1901 * A pointer to the shared cpuset was automatically copied in fork.c
1902 * by dup_task_struct(). However, we ignore that copy, since it was
1903 * not made under the protection of task_lock(), so might no longer be
1904 * a valid cpuset pointer. attach_task() might have already changed
1905 * current->cpuset, allowing the previously referenced cpuset to
1906 * be removed and freed. Instead, we task_lock(current) and copy
1907 * its present value of current->cpuset for our freshly forked child.
1909 * At the point that cpuset_fork() is called, 'current' is the parent
1910 * task, and the passed argument 'child' points to the child task.
1913 void cpuset_fork(struct task_struct
*child
)
1916 child
->cpuset
= current
->cpuset
;
1917 atomic_inc(&child
->cpuset
->count
);
1918 task_unlock(current
);
1922 * cpuset_exit - detach cpuset from exiting task
1923 * @tsk: pointer to task_struct of exiting process
1925 * Description: Detach cpuset from @tsk and release it.
1927 * Note that cpusets marked notify_on_release force every task in
1928 * them to take the global manage_sem semaphore when exiting.
1929 * This could impact scaling on very large systems. Be reluctant to
1930 * use notify_on_release cpusets where very high task exit scaling
1931 * is required on large systems.
1933 * Don't even think about derefencing 'cs' after the cpuset use count
1934 * goes to zero, except inside a critical section guarded by manage_sem
1935 * or callback_sem. Otherwise a zero cpuset use count is a license to
1936 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
1938 * This routine has to take manage_sem, not callback_sem, because
1939 * it is holding that semaphore while calling check_for_release(),
1940 * which calls kmalloc(), so can't be called holding callback__sem().
1942 * We don't need to task_lock() this reference to tsk->cpuset,
1943 * because tsk is already marked PF_EXITING, so attach_task() won't
1944 * mess with it, or task is a failed fork, never visible to attach_task.
1947 void cpuset_exit(struct task_struct
*tsk
)
1954 if (notify_on_release(cs
)) {
1955 char *pathbuf
= NULL
;
1958 if (atomic_dec_and_test(&cs
->count
))
1959 check_for_release(cs
, &pathbuf
);
1961 cpuset_release_agent(pathbuf
);
1963 atomic_dec(&cs
->count
);
1968 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1969 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
1971 * Description: Returns the cpumask_t cpus_allowed of the cpuset
1972 * attached to the specified @tsk. Guaranteed to return some non-empty
1973 * subset of cpu_online_map, even if this means going outside the
1977 cpumask_t
cpuset_cpus_allowed(struct task_struct
*tsk
)
1981 down(&callback_sem
);
1983 guarantee_online_cpus(tsk
->cpuset
, &mask
);
1990 void cpuset_init_current_mems_allowed(void)
1992 current
->mems_allowed
= NODE_MASK_ALL
;
1996 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
1997 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
1999 * Description: Returns the nodemask_t mems_allowed of the cpuset
2000 * attached to the specified @tsk. Guaranteed to return some non-empty
2001 * subset of node_online_map, even if this means going outside the
2005 nodemask_t
cpuset_mems_allowed(struct task_struct
*tsk
)
2009 down(&callback_sem
);
2011 guarantee_online_mems(tsk
->cpuset
, &mask
);
2019 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2020 * @zl: the zonelist to be checked
2022 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2024 int cpuset_zonelist_valid_mems_allowed(struct zonelist
*zl
)
2028 for (i
= 0; zl
->zones
[i
]; i
++) {
2029 int nid
= zl
->zones
[i
]->zone_pgdat
->node_id
;
2031 if (node_isset(nid
, current
->mems_allowed
))
2038 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
2039 * ancestor to the specified cpuset. Call holding callback_sem.
2040 * If no ancestor is mem_exclusive (an unusual configuration), then
2041 * returns the root cpuset.
2043 static const struct cpuset
*nearest_exclusive_ancestor(const struct cpuset
*cs
)
2045 while (!is_mem_exclusive(cs
) && cs
->parent
)
2051 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
2052 * @z: is this zone on an allowed node?
2053 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
2055 * If we're in interrupt, yes, we can always allocate. If zone
2056 * z's node is in our tasks mems_allowed, yes. If it's not a
2057 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2058 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
2061 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2062 * and do not allow allocations outside the current tasks cpuset.
2063 * GFP_KERNEL allocations are not so marked, so can escape to the
2064 * nearest mem_exclusive ancestor cpuset.
2066 * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
2067 * routine only calls here with __GFP_HARDWALL bit _not_ set if
2068 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
2069 * mems_allowed came up empty on the first pass over the zonelist.
2070 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
2071 * short of memory, might require taking the callback_sem semaphore.
2073 * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
2074 * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
2075 * hardwall cpusets - no allocation on a node outside the cpuset is
2076 * allowed (unless in interrupt, of course).
2078 * The second loop doesn't even call here for GFP_ATOMIC requests
2079 * (if the __alloc_pages() local variable 'wait' is set). That check
2080 * and the checks below have the combined affect in the second loop of
2081 * the __alloc_pages() routine that:
2082 * in_interrupt - any node ok (current task context irrelevant)
2083 * GFP_ATOMIC - any node ok
2084 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2085 * GFP_USER - only nodes in current tasks mems allowed ok.
2088 int __cpuset_zone_allowed(struct zone
*z
, gfp_t gfp_mask
)
2090 int node
; /* node that zone z is on */
2091 const struct cpuset
*cs
; /* current cpuset ancestors */
2092 int allowed
= 1; /* is allocation in zone z allowed? */
2096 node
= z
->zone_pgdat
->node_id
;
2097 if (node_isset(node
, current
->mems_allowed
))
2099 if (gfp_mask
& __GFP_HARDWALL
) /* If hardwall request, stop here */
2102 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
2105 /* Not hardwall and node outside mems_allowed: scan up cpusets */
2106 down(&callback_sem
);
2109 cs
= nearest_exclusive_ancestor(current
->cpuset
);
2110 task_unlock(current
);
2112 allowed
= node_isset(node
, cs
->mems_allowed
);
2118 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2119 * @p: pointer to task_struct of some other task.
2121 * Description: Return true if the nearest mem_exclusive ancestor
2122 * cpusets of tasks @p and current overlap. Used by oom killer to
2123 * determine if task @p's memory usage might impact the memory
2124 * available to the current task.
2126 * Acquires callback_sem - not suitable for calling from a fast path.
2129 int cpuset_excl_nodes_overlap(const struct task_struct
*p
)
2131 const struct cpuset
*cs1
, *cs2
; /* my and p's cpuset ancestors */
2132 int overlap
= 0; /* do cpusets overlap? */
2134 down(&callback_sem
);
2137 if (current
->flags
& PF_EXITING
) {
2138 task_unlock(current
);
2141 cs1
= nearest_exclusive_ancestor(current
->cpuset
);
2142 task_unlock(current
);
2144 task_lock((struct task_struct
*)p
);
2145 if (p
->flags
& PF_EXITING
) {
2146 task_unlock((struct task_struct
*)p
);
2149 cs2
= nearest_exclusive_ancestor(p
->cpuset
);
2150 task_unlock((struct task_struct
*)p
);
2152 overlap
= nodes_intersects(cs1
->mems_allowed
, cs2
->mems_allowed
);
2160 * Collection of memory_pressure is suppressed unless
2161 * this flag is enabled by writing "1" to the special
2162 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2165 int cpuset_memory_pressure_enabled __read_mostly
;
2168 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2170 * Keep a running average of the rate of synchronous (direct)
2171 * page reclaim efforts initiated by tasks in each cpuset.
2173 * This represents the rate at which some task in the cpuset
2174 * ran low on memory on all nodes it was allowed to use, and
2175 * had to enter the kernels page reclaim code in an effort to
2176 * create more free memory by tossing clean pages or swapping
2177 * or writing dirty pages.
2179 * Display to user space in the per-cpuset read-only file
2180 * "memory_pressure". Value displayed is an integer
2181 * representing the recent rate of entry into the synchronous
2182 * (direct) page reclaim by any task attached to the cpuset.
2185 void __cpuset_memory_pressure_bump(void)
2190 cs
= current
->cpuset
;
2191 fmeter_markevent(&cs
->fmeter
);
2192 task_unlock(current
);
2196 * proc_cpuset_show()
2197 * - Print tasks cpuset path into seq_file.
2198 * - Used for /proc/<pid>/cpuset.
2199 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2200 * doesn't really matter if tsk->cpuset changes after we read it,
2201 * and we take manage_sem, keeping attach_task() from changing it
2205 static int proc_cpuset_show(struct seq_file
*m
, void *v
)
2208 struct task_struct
*tsk
;
2212 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2224 retval
= cpuset_path(cs
, buf
, PAGE_SIZE
);
2235 static int cpuset_open(struct inode
*inode
, struct file
*file
)
2237 struct task_struct
*tsk
= PROC_I(inode
)->task
;
2238 return single_open(file
, proc_cpuset_show
, tsk
);
2241 struct file_operations proc_cpuset_operations
= {
2242 .open
= cpuset_open
,
2244 .llseek
= seq_lseek
,
2245 .release
= single_release
,
2248 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2249 char *cpuset_task_status_allowed(struct task_struct
*task
, char *buffer
)
2251 buffer
+= sprintf(buffer
, "Cpus_allowed:\t");
2252 buffer
+= cpumask_scnprintf(buffer
, PAGE_SIZE
, task
->cpus_allowed
);
2253 buffer
+= sprintf(buffer
, "\n");
2254 buffer
+= sprintf(buffer
, "Mems_allowed:\t");
2255 buffer
+= nodemask_scnprintf(buffer
, PAGE_SIZE
, task
->mems_allowed
);
2256 buffer
+= sprintf(buffer
, "\n");