4 * Processor and Memory placement constraints for sets of tasks.
6 * Copyright (C) 2003 BULL SA.
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
9 * Portions derived from Patrick Mochel's sysfs code.
10 * sysfs is Copyright (c) 2001-3 Patrick Mochel
12 * 2003-10-10 Written by Simon Derr.
13 * 2003-10-22 Updates by Stephen Hemminger.
14 * 2004 May-July Rework by Paul Jackson.
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file COPYING in the main directory of the Linux
18 * distribution for more details.
21 #include <linux/config.h>
22 #include <linux/cpu.h>
23 #include <linux/cpumask.h>
24 #include <linux/cpuset.h>
25 #include <linux/err.h>
26 #include <linux/errno.h>
27 #include <linux/file.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/kmod.h>
33 #include <linux/list.h>
34 #include <linux/mempolicy.h>
36 #include <linux/module.h>
37 #include <linux/mount.h>
38 #include <linux/namei.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/seq_file.h>
44 #include <linux/security.h>
45 #include <linux/slab.h>
46 #include <linux/smp_lock.h>
47 #include <linux/spinlock.h>
48 #include <linux/stat.h>
49 #include <linux/string.h>
50 #include <linux/time.h>
51 #include <linux/backing-dev.h>
52 #include <linux/sort.h>
54 #include <asm/uaccess.h>
55 #include <asm/atomic.h>
56 #include <linux/mutex.h>
58 #define CPUSET_SUPER_MAGIC 0x27e0eb
61 * Tracks how many cpusets are currently defined in system.
62 * When there is only one cpuset (the root cpuset) we can
63 * short circuit some hooks.
65 int number_of_cpusets __read_mostly
;
67 /* See "Frequency meter" comments, below. */
70 int cnt
; /* unprocessed events count */
71 int val
; /* most recent output value */
72 time_t time
; /* clock (secs) when val computed */
73 spinlock_t lock
; /* guards read or write of above */
77 unsigned long flags
; /* "unsigned long" so bitops work */
78 cpumask_t cpus_allowed
; /* CPUs allowed to tasks in cpuset */
79 nodemask_t mems_allowed
; /* Memory Nodes allowed to tasks */
82 * Count is atomic so can incr (fork) or decr (exit) without a lock.
84 atomic_t count
; /* count tasks using this cpuset */
87 * We link our 'sibling' struct into our parents 'children'.
88 * Our children link their 'sibling' into our 'children'.
90 struct list_head sibling
; /* my parents children */
91 struct list_head children
; /* my children */
93 struct cpuset
*parent
; /* my parent */
94 struct dentry
*dentry
; /* cpuset fs entry */
97 * Copy of global cpuset_mems_generation as of the most
98 * recent time this cpuset changed its mems_allowed.
102 struct fmeter fmeter
; /* memory_pressure filter */
105 /* bits in struct cpuset flags field */
111 CS_NOTIFY_ON_RELEASE
,
116 /* convenient tests for these bits */
117 static inline int is_cpu_exclusive(const struct cpuset
*cs
)
119 return test_bit(CS_CPU_EXCLUSIVE
, &cs
->flags
);
122 static inline int is_mem_exclusive(const struct cpuset
*cs
)
124 return test_bit(CS_MEM_EXCLUSIVE
, &cs
->flags
);
127 static inline int is_removed(const struct cpuset
*cs
)
129 return test_bit(CS_REMOVED
, &cs
->flags
);
132 static inline int notify_on_release(const struct cpuset
*cs
)
134 return test_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
137 static inline int is_memory_migrate(const struct cpuset
*cs
)
139 return test_bit(CS_MEMORY_MIGRATE
, &cs
->flags
);
142 static inline int is_spread_page(const struct cpuset
*cs
)
144 return test_bit(CS_SPREAD_PAGE
, &cs
->flags
);
147 static inline int is_spread_slab(const struct cpuset
*cs
)
149 return test_bit(CS_SPREAD_SLAB
, &cs
->flags
);
153 * Increment this integer everytime any cpuset changes its
154 * mems_allowed value. Users of cpusets can track this generation
155 * number, and avoid having to lock and reload mems_allowed unless
156 * the cpuset they're using changes generation.
158 * A single, global generation is needed because attach_task() could
159 * reattach a task to a different cpuset, which must not have its
160 * generation numbers aliased with those of that tasks previous cpuset.
162 * Generations are needed for mems_allowed because one task cannot
163 * modify anothers memory placement. So we must enable every task,
164 * on every visit to __alloc_pages(), to efficiently check whether
165 * its current->cpuset->mems_allowed has changed, requiring an update
166 * of its current->mems_allowed.
168 * Since cpuset_mems_generation is guarded by manage_mutex,
169 * there is no need to mark it atomic.
171 static int cpuset_mems_generation
;
173 static struct cpuset top_cpuset
= {
174 .flags
= ((1 << CS_CPU_EXCLUSIVE
) | (1 << CS_MEM_EXCLUSIVE
)),
175 .cpus_allowed
= CPU_MASK_ALL
,
176 .mems_allowed
= NODE_MASK_ALL
,
177 .count
= ATOMIC_INIT(0),
178 .sibling
= LIST_HEAD_INIT(top_cpuset
.sibling
),
179 .children
= LIST_HEAD_INIT(top_cpuset
.children
),
182 static struct vfsmount
*cpuset_mount
;
183 static struct super_block
*cpuset_sb
;
186 * We have two global cpuset mutexes below. They can nest.
187 * It is ok to first take manage_mutex, then nest callback_mutex. We also
188 * require taking task_lock() when dereferencing a tasks cpuset pointer.
189 * See "The task_lock() exception", at the end of this comment.
191 * A task must hold both mutexes to modify cpusets. If a task
192 * holds manage_mutex, then it blocks others wanting that mutex,
193 * ensuring that it is the only task able to also acquire callback_mutex
194 * and be able to modify cpusets. It can perform various checks on
195 * the cpuset structure first, knowing nothing will change. It can
196 * also allocate memory while just holding manage_mutex. While it is
197 * performing these checks, various callback routines can briefly
198 * acquire callback_mutex to query cpusets. Once it is ready to make
199 * the changes, it takes callback_mutex, blocking everyone else.
201 * Calls to the kernel memory allocator can not be made while holding
202 * callback_mutex, as that would risk double tripping on callback_mutex
203 * from one of the callbacks into the cpuset code from within
206 * If a task is only holding callback_mutex, then it has read-only
209 * The task_struct fields mems_allowed and mems_generation may only
210 * be accessed in the context of that task, so require no locks.
212 * Any task can increment and decrement the count field without lock.
213 * So in general, code holding manage_mutex or callback_mutex can't rely
214 * on the count field not changing. However, if the count goes to
215 * zero, then only attach_task(), which holds both mutexes, can
216 * increment it again. Because a count of zero means that no tasks
217 * are currently attached, therefore there is no way a task attached
218 * to that cpuset can fork (the other way to increment the count).
219 * So code holding manage_mutex or callback_mutex can safely assume that
220 * if the count is zero, it will stay zero. Similarly, if a task
221 * holds manage_mutex or callback_mutex on a cpuset with zero count, it
222 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
223 * both of those mutexes.
225 * The cpuset_common_file_write handler for operations that modify
226 * the cpuset hierarchy holds manage_mutex across the entire operation,
227 * single threading all such cpuset modifications across the system.
229 * The cpuset_common_file_read() handlers only hold callback_mutex across
230 * small pieces of code, such as when reading out possibly multi-word
231 * cpumasks and nodemasks.
233 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
234 * (usually) take either mutex. These are the two most performance
235 * critical pieces of code here. The exception occurs on cpuset_exit(),
236 * when a task in a notify_on_release cpuset exits. Then manage_mutex
237 * is taken, and if the cpuset count is zero, a usermode call made
238 * to /sbin/cpuset_release_agent with the name of the cpuset (path
239 * relative to the root of cpuset file system) as the argument.
241 * A cpuset can only be deleted if both its 'count' of using tasks
242 * is zero, and its list of 'children' cpusets is empty. Since all
243 * tasks in the system use _some_ cpuset, and since there is always at
244 * least one task in the system (init, pid == 1), therefore, top_cpuset
245 * always has either children cpusets and/or using tasks. So we don't
246 * need a special hack to ensure that top_cpuset cannot be deleted.
248 * The above "Tale of Two Semaphores" would be complete, but for:
250 * The task_lock() exception
252 * The need for this exception arises from the action of attach_task(),
253 * which overwrites one tasks cpuset pointer with another. It does
254 * so using both mutexes, however there are several performance
255 * critical places that need to reference task->cpuset without the
256 * expense of grabbing a system global mutex. Therefore except as
257 * noted below, when dereferencing or, as in attach_task(), modifying
258 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
259 * (task->alloc_lock) already in the task_struct routinely used for
262 * P.S. One more locking exception. RCU is used to guard the
263 * update of a tasks cpuset pointer by attach_task() and the
264 * access of task->cpuset->mems_generation via that pointer in
265 * the routine cpuset_update_task_memory_state().
268 static DEFINE_MUTEX(manage_mutex
);
269 static DEFINE_MUTEX(callback_mutex
);
272 * A couple of forward declarations required, due to cyclic reference loop:
273 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
274 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
277 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
278 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
280 static struct backing_dev_info cpuset_backing_dev_info
= {
281 .ra_pages
= 0, /* No readahead */
282 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
285 static struct inode
*cpuset_new_inode(mode_t mode
)
287 struct inode
*inode
= new_inode(cpuset_sb
);
290 inode
->i_mode
= mode
;
291 inode
->i_uid
= current
->fsuid
;
292 inode
->i_gid
= current
->fsgid
;
293 inode
->i_blksize
= PAGE_CACHE_SIZE
;
295 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
296 inode
->i_mapping
->backing_dev_info
= &cpuset_backing_dev_info
;
301 static void cpuset_diput(struct dentry
*dentry
, struct inode
*inode
)
303 /* is dentry a directory ? if so, kfree() associated cpuset */
304 if (S_ISDIR(inode
->i_mode
)) {
305 struct cpuset
*cs
= dentry
->d_fsdata
;
306 BUG_ON(!(is_removed(cs
)));
312 static struct dentry_operations cpuset_dops
= {
313 .d_iput
= cpuset_diput
,
316 static struct dentry
*cpuset_get_dentry(struct dentry
*parent
, const char *name
)
318 struct dentry
*d
= lookup_one_len(name
, parent
, strlen(name
));
320 d
->d_op
= &cpuset_dops
;
324 static void remove_dir(struct dentry
*d
)
326 struct dentry
*parent
= dget(d
->d_parent
);
329 simple_rmdir(parent
->d_inode
, d
);
334 * NOTE : the dentry must have been dget()'ed
336 static void cpuset_d_remove_dir(struct dentry
*dentry
)
338 struct list_head
*node
;
340 spin_lock(&dcache_lock
);
341 node
= dentry
->d_subdirs
.next
;
342 while (node
!= &dentry
->d_subdirs
) {
343 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
347 spin_unlock(&dcache_lock
);
349 simple_unlink(dentry
->d_inode
, d
);
351 spin_lock(&dcache_lock
);
353 node
= dentry
->d_subdirs
.next
;
355 list_del_init(&dentry
->d_u
.d_child
);
356 spin_unlock(&dcache_lock
);
360 static struct super_operations cpuset_ops
= {
361 .statfs
= simple_statfs
,
362 .drop_inode
= generic_delete_inode
,
365 static int cpuset_fill_super(struct super_block
*sb
, void *unused_data
,
371 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
372 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
373 sb
->s_magic
= CPUSET_SUPER_MAGIC
;
374 sb
->s_op
= &cpuset_ops
;
377 inode
= cpuset_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
);
379 inode
->i_op
= &simple_dir_inode_operations
;
380 inode
->i_fop
= &simple_dir_operations
;
381 /* directories start off with i_nlink == 2 (for "." entry) */
387 root
= d_alloc_root(inode
);
396 static int cpuset_get_sb(struct file_system_type
*fs_type
,
397 int flags
, const char *unused_dev_name
,
398 void *data
, struct vfsmount
*mnt
)
400 return get_sb_single(fs_type
, flags
, data
, cpuset_fill_super
, mnt
);
403 static struct file_system_type cpuset_fs_type
= {
405 .get_sb
= cpuset_get_sb
,
406 .kill_sb
= kill_litter_super
,
411 * The files in the cpuset filesystem mostly have a very simple read/write
412 * handling, some common function will take care of it. Nevertheless some cases
413 * (read tasks) are special and therefore I define this structure for every
417 * When reading/writing to a file:
418 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
419 * - the 'cftype' of the file is file->f_dentry->d_fsdata
425 int (*open
) (struct inode
*inode
, struct file
*file
);
426 ssize_t (*read
) (struct file
*file
, char __user
*buf
, size_t nbytes
,
428 int (*write
) (struct file
*file
, const char __user
*buf
, size_t nbytes
,
430 int (*release
) (struct inode
*inode
, struct file
*file
);
433 static inline struct cpuset
*__d_cs(struct dentry
*dentry
)
435 return dentry
->d_fsdata
;
438 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
440 return dentry
->d_fsdata
;
444 * Call with manage_mutex held. Writes path of cpuset into buf.
445 * Returns 0 on success, -errno on error.
448 static int cpuset_path(const struct cpuset
*cs
, char *buf
, int buflen
)
452 start
= buf
+ buflen
;
456 int len
= cs
->dentry
->d_name
.len
;
457 if ((start
-= len
) < buf
)
458 return -ENAMETOOLONG
;
459 memcpy(start
, cs
->dentry
->d_name
.name
, len
);
466 return -ENAMETOOLONG
;
469 memmove(buf
, start
, buf
+ buflen
- start
);
474 * Notify userspace when a cpuset is released, by running
475 * /sbin/cpuset_release_agent with the name of the cpuset (path
476 * relative to the root of cpuset file system) as the argument.
478 * Most likely, this user command will try to rmdir this cpuset.
480 * This races with the possibility that some other task will be
481 * attached to this cpuset before it is removed, or that some other
482 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
483 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
484 * unused, and this cpuset will be reprieved from its death sentence,
485 * to continue to serve a useful existence. Next time it's released,
486 * we will get notified again, if it still has 'notify_on_release' set.
488 * The final arg to call_usermodehelper() is 0, which means don't
489 * wait. The separate /sbin/cpuset_release_agent task is forked by
490 * call_usermodehelper(), then control in this thread returns here,
491 * without waiting for the release agent task. We don't bother to
492 * wait because the caller of this routine has no use for the exit
493 * status of the /sbin/cpuset_release_agent task, so no sense holding
494 * our caller up for that.
496 * When we had only one cpuset mutex, we had to call this
497 * without holding it, to avoid deadlock when call_usermodehelper()
498 * allocated memory. With two locks, we could now call this while
499 * holding manage_mutex, but we still don't, so as to minimize
500 * the time manage_mutex is held.
503 static void cpuset_release_agent(const char *pathbuf
)
505 char *argv
[3], *envp
[3];
512 argv
[i
++] = "/sbin/cpuset_release_agent";
513 argv
[i
++] = (char *)pathbuf
;
517 /* minimal command environment */
518 envp
[i
++] = "HOME=/";
519 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
522 call_usermodehelper(argv
[0], argv
, envp
, 0);
527 * Either cs->count of using tasks transitioned to zero, or the
528 * cs->children list of child cpusets just became empty. If this
529 * cs is notify_on_release() and now both the user count is zero and
530 * the list of children is empty, prepare cpuset path in a kmalloc'd
531 * buffer, to be returned via ppathbuf, so that the caller can invoke
532 * cpuset_release_agent() with it later on, once manage_mutex is dropped.
533 * Call here with manage_mutex held.
535 * This check_for_release() routine is responsible for kmalloc'ing
536 * pathbuf. The above cpuset_release_agent() is responsible for
537 * kfree'ing pathbuf. The caller of these routines is responsible
538 * for providing a pathbuf pointer, initialized to NULL, then
539 * calling check_for_release() with manage_mutex held and the address
540 * of the pathbuf pointer, then dropping manage_mutex, then calling
541 * cpuset_release_agent() with pathbuf, as set by check_for_release().
544 static void check_for_release(struct cpuset
*cs
, char **ppathbuf
)
546 if (notify_on_release(cs
) && atomic_read(&cs
->count
) == 0 &&
547 list_empty(&cs
->children
)) {
550 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
553 if (cpuset_path(cs
, buf
, PAGE_SIZE
) < 0)
561 * Return in *pmask the portion of a cpusets's cpus_allowed that
562 * are online. If none are online, walk up the cpuset hierarchy
563 * until we find one that does have some online cpus. If we get
564 * all the way to the top and still haven't found any online cpus,
565 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
566 * task, return cpu_online_map.
568 * One way or another, we guarantee to return some non-empty subset
571 * Call with callback_mutex held.
574 static void guarantee_online_cpus(const struct cpuset
*cs
, cpumask_t
*pmask
)
576 while (cs
&& !cpus_intersects(cs
->cpus_allowed
, cpu_online_map
))
579 cpus_and(*pmask
, cs
->cpus_allowed
, cpu_online_map
);
581 *pmask
= cpu_online_map
;
582 BUG_ON(!cpus_intersects(*pmask
, cpu_online_map
));
586 * Return in *pmask the portion of a cpusets's mems_allowed that
587 * are online. If none are online, walk up the cpuset hierarchy
588 * until we find one that does have some online mems. If we get
589 * all the way to the top and still haven't found any online mems,
590 * return node_online_map.
592 * One way or another, we guarantee to return some non-empty subset
593 * of node_online_map.
595 * Call with callback_mutex held.
598 static void guarantee_online_mems(const struct cpuset
*cs
, nodemask_t
*pmask
)
600 while (cs
&& !nodes_intersects(cs
->mems_allowed
, node_online_map
))
603 nodes_and(*pmask
, cs
->mems_allowed
, node_online_map
);
605 *pmask
= node_online_map
;
606 BUG_ON(!nodes_intersects(*pmask
, node_online_map
));
610 * cpuset_update_task_memory_state - update task memory placement
612 * If the current tasks cpusets mems_allowed changed behind our
613 * backs, update current->mems_allowed, mems_generation and task NUMA
614 * mempolicy to the new value.
616 * Task mempolicy is updated by rebinding it relative to the
617 * current->cpuset if a task has its memory placement changed.
618 * Do not call this routine if in_interrupt().
620 * Call without callback_mutex or task_lock() held. May be
621 * called with or without manage_mutex held. Thanks in part to
622 * 'the_top_cpuset_hack', the tasks cpuset pointer will never
623 * be NULL. This routine also might acquire callback_mutex and
624 * current->mm->mmap_sem during call.
626 * Reading current->cpuset->mems_generation doesn't need task_lock
627 * to guard the current->cpuset derefence, because it is guarded
628 * from concurrent freeing of current->cpuset by attach_task(),
631 * The rcu_dereference() is technically probably not needed,
632 * as I don't actually mind if I see a new cpuset pointer but
633 * an old value of mems_generation. However this really only
634 * matters on alpha systems using cpusets heavily. If I dropped
635 * that rcu_dereference(), it would save them a memory barrier.
636 * For all other arch's, rcu_dereference is a no-op anyway, and for
637 * alpha systems not using cpusets, another planned optimization,
638 * avoiding the rcu critical section for tasks in the root cpuset
639 * which is statically allocated, so can't vanish, will make this
640 * irrelevant. Better to use RCU as intended, than to engage in
641 * some cute trick to save a memory barrier that is impossible to
642 * test, for alpha systems using cpusets heavily, which might not
645 * This routine is needed to update the per-task mems_allowed data,
646 * within the tasks context, when it is trying to allocate memory
647 * (in various mm/mempolicy.c routines) and notices that some other
648 * task has been modifying its cpuset.
651 void cpuset_update_task_memory_state(void)
653 int my_cpusets_mem_gen
;
654 struct task_struct
*tsk
= current
;
657 if (tsk
->cpuset
== &top_cpuset
) {
658 /* Don't need rcu for top_cpuset. It's never freed. */
659 my_cpusets_mem_gen
= top_cpuset
.mems_generation
;
662 cs
= rcu_dereference(tsk
->cpuset
);
663 my_cpusets_mem_gen
= cs
->mems_generation
;
667 if (my_cpusets_mem_gen
!= tsk
->cpuset_mems_generation
) {
668 mutex_lock(&callback_mutex
);
670 cs
= tsk
->cpuset
; /* Maybe changed when task not locked */
671 guarantee_online_mems(cs
, &tsk
->mems_allowed
);
672 tsk
->cpuset_mems_generation
= cs
->mems_generation
;
673 if (is_spread_page(cs
))
674 tsk
->flags
|= PF_SPREAD_PAGE
;
676 tsk
->flags
&= ~PF_SPREAD_PAGE
;
677 if (is_spread_slab(cs
))
678 tsk
->flags
|= PF_SPREAD_SLAB
;
680 tsk
->flags
&= ~PF_SPREAD_SLAB
;
682 mutex_unlock(&callback_mutex
);
683 mpol_rebind_task(tsk
, &tsk
->mems_allowed
);
688 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
690 * One cpuset is a subset of another if all its allowed CPUs and
691 * Memory Nodes are a subset of the other, and its exclusive flags
692 * are only set if the other's are set. Call holding manage_mutex.
695 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
697 return cpus_subset(p
->cpus_allowed
, q
->cpus_allowed
) &&
698 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
699 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
700 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
704 * validate_change() - Used to validate that any proposed cpuset change
705 * follows the structural rules for cpusets.
707 * If we replaced the flag and mask values of the current cpuset
708 * (cur) with those values in the trial cpuset (trial), would
709 * our various subset and exclusive rules still be valid? Presumes
712 * 'cur' is the address of an actual, in-use cpuset. Operations
713 * such as list traversal that depend on the actual address of the
714 * cpuset in the list must use cur below, not trial.
716 * 'trial' is the address of bulk structure copy of cur, with
717 * perhaps one or more of the fields cpus_allowed, mems_allowed,
718 * or flags changed to new, trial values.
720 * Return 0 if valid, -errno if not.
723 static int validate_change(const struct cpuset
*cur
, const struct cpuset
*trial
)
725 struct cpuset
*c
, *par
;
727 /* Each of our child cpusets must be a subset of us */
728 list_for_each_entry(c
, &cur
->children
, sibling
) {
729 if (!is_cpuset_subset(c
, trial
))
733 /* Remaining checks don't apply to root cpuset */
734 if ((par
= cur
->parent
) == NULL
)
737 /* We must be a subset of our parent cpuset */
738 if (!is_cpuset_subset(trial
, par
))
741 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
742 list_for_each_entry(c
, &par
->children
, sibling
) {
743 if ((is_cpu_exclusive(trial
) || is_cpu_exclusive(c
)) &&
745 cpus_intersects(trial
->cpus_allowed
, c
->cpus_allowed
))
747 if ((is_mem_exclusive(trial
) || is_mem_exclusive(c
)) &&
749 nodes_intersects(trial
->mems_allowed
, c
->mems_allowed
))
757 * For a given cpuset cur, partition the system as follows
758 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
759 * exclusive child cpusets
760 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
761 * exclusive child cpusets
762 * Build these two partitions by calling partition_sched_domains
764 * Call with manage_mutex held. May nest a call to the
765 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
768 static void update_cpu_domains(struct cpuset
*cur
)
770 struct cpuset
*c
, *par
= cur
->parent
;
771 cpumask_t pspan
, cspan
;
773 if (par
== NULL
|| cpus_empty(cur
->cpus_allowed
))
777 * Get all cpus from parent's cpus_allowed not part of exclusive
780 pspan
= par
->cpus_allowed
;
781 list_for_each_entry(c
, &par
->children
, sibling
) {
782 if (is_cpu_exclusive(c
))
783 cpus_andnot(pspan
, pspan
, c
->cpus_allowed
);
785 if (is_removed(cur
) || !is_cpu_exclusive(cur
)) {
786 cpus_or(pspan
, pspan
, cur
->cpus_allowed
);
787 if (cpus_equal(pspan
, cur
->cpus_allowed
))
789 cspan
= CPU_MASK_NONE
;
791 if (cpus_empty(pspan
))
793 cspan
= cur
->cpus_allowed
;
795 * Get all cpus from current cpuset's cpus_allowed not part
796 * of exclusive children
798 list_for_each_entry(c
, &cur
->children
, sibling
) {
799 if (is_cpu_exclusive(c
))
800 cpus_andnot(cspan
, cspan
, c
->cpus_allowed
);
805 partition_sched_domains(&pspan
, &cspan
);
806 unlock_cpu_hotplug();
810 * Call with manage_mutex held. May take callback_mutex during call.
813 static int update_cpumask(struct cpuset
*cs
, char *buf
)
815 struct cpuset trialcs
;
816 int retval
, cpus_unchanged
;
819 retval
= cpulist_parse(buf
, trialcs
.cpus_allowed
);
822 cpus_and(trialcs
.cpus_allowed
, trialcs
.cpus_allowed
, cpu_online_map
);
823 if (cpus_empty(trialcs
.cpus_allowed
))
825 retval
= validate_change(cs
, &trialcs
);
828 cpus_unchanged
= cpus_equal(cs
->cpus_allowed
, trialcs
.cpus_allowed
);
829 mutex_lock(&callback_mutex
);
830 cs
->cpus_allowed
= trialcs
.cpus_allowed
;
831 mutex_unlock(&callback_mutex
);
832 if (is_cpu_exclusive(cs
) && !cpus_unchanged
)
833 update_cpu_domains(cs
);
840 * Migrate memory region from one set of nodes to another.
842 * Temporarilly set tasks mems_allowed to target nodes of migration,
843 * so that the migration code can allocate pages on these nodes.
845 * Call holding manage_mutex, so our current->cpuset won't change
846 * during this call, as manage_mutex holds off any attach_task()
847 * calls. Therefore we don't need to take task_lock around the
848 * call to guarantee_online_mems(), as we know no one is changing
851 * Hold callback_mutex around the two modifications of our tasks
852 * mems_allowed to synchronize with cpuset_mems_allowed().
854 * While the mm_struct we are migrating is typically from some
855 * other task, the task_struct mems_allowed that we are hacking
856 * is for our current task, which must allocate new pages for that
857 * migrating memory region.
859 * We call cpuset_update_task_memory_state() before hacking
860 * our tasks mems_allowed, so that we are assured of being in
861 * sync with our tasks cpuset, and in particular, callbacks to
862 * cpuset_update_task_memory_state() from nested page allocations
863 * won't see any mismatch of our cpuset and task mems_generation
864 * values, so won't overwrite our hacked tasks mems_allowed
868 static void cpuset_migrate_mm(struct mm_struct
*mm
, const nodemask_t
*from
,
869 const nodemask_t
*to
)
871 struct task_struct
*tsk
= current
;
873 cpuset_update_task_memory_state();
875 mutex_lock(&callback_mutex
);
876 tsk
->mems_allowed
= *to
;
877 mutex_unlock(&callback_mutex
);
879 do_migrate_pages(mm
, from
, to
, MPOL_MF_MOVE_ALL
);
881 mutex_lock(&callback_mutex
);
882 guarantee_online_mems(tsk
->cpuset
, &tsk
->mems_allowed
);
883 mutex_unlock(&callback_mutex
);
887 * Handle user request to change the 'mems' memory placement
888 * of a cpuset. Needs to validate the request, update the
889 * cpusets mems_allowed and mems_generation, and for each
890 * task in the cpuset, rebind any vma mempolicies and if
891 * the cpuset is marked 'memory_migrate', migrate the tasks
892 * pages to the new memory.
894 * Call with manage_mutex held. May take callback_mutex during call.
895 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
896 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
897 * their mempolicies to the cpusets new mems_allowed.
900 static int update_nodemask(struct cpuset
*cs
, char *buf
)
902 struct cpuset trialcs
;
904 struct task_struct
*g
, *p
;
905 struct mm_struct
**mmarray
;
912 retval
= nodelist_parse(buf
, trialcs
.mems_allowed
);
915 nodes_and(trialcs
.mems_allowed
, trialcs
.mems_allowed
, node_online_map
);
916 oldmem
= cs
->mems_allowed
;
917 if (nodes_equal(oldmem
, trialcs
.mems_allowed
)) {
918 retval
= 0; /* Too easy - nothing to do */
921 if (nodes_empty(trialcs
.mems_allowed
)) {
925 retval
= validate_change(cs
, &trialcs
);
929 mutex_lock(&callback_mutex
);
930 cs
->mems_allowed
= trialcs
.mems_allowed
;
931 cs
->mems_generation
= cpuset_mems_generation
++;
932 mutex_unlock(&callback_mutex
);
934 set_cpuset_being_rebound(cs
); /* causes mpol_copy() rebind */
936 fudge
= 10; /* spare mmarray[] slots */
937 fudge
+= cpus_weight(cs
->cpus_allowed
); /* imagine one fork-bomb/cpu */
941 * Allocate mmarray[] to hold mm reference for each task
942 * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
943 * tasklist_lock. We could use GFP_ATOMIC, but with a
944 * few more lines of code, we can retry until we get a big
945 * enough mmarray[] w/o using GFP_ATOMIC.
948 ntasks
= atomic_read(&cs
->count
); /* guess */
950 mmarray
= kmalloc(ntasks
* sizeof(*mmarray
), GFP_KERNEL
);
953 write_lock_irq(&tasklist_lock
); /* block fork */
954 if (atomic_read(&cs
->count
) <= ntasks
)
955 break; /* got enough */
956 write_unlock_irq(&tasklist_lock
); /* try again */
962 /* Load up mmarray[] with mm reference for each task in cpuset. */
963 do_each_thread(g
, p
) {
964 struct mm_struct
*mm
;
968 "Cpuset mempolicy rebind incomplete.\n");
977 } while_each_thread(g
, p
);
978 write_unlock_irq(&tasklist_lock
);
981 * Now that we've dropped the tasklist spinlock, we can
982 * rebind the vma mempolicies of each mm in mmarray[] to their
983 * new cpuset, and release that mm. The mpol_rebind_mm()
984 * call takes mmap_sem, which we couldn't take while holding
985 * tasklist_lock. Forks can happen again now - the mpol_copy()
986 * cpuset_being_rebound check will catch such forks, and rebind
987 * their vma mempolicies too. Because we still hold the global
988 * cpuset manage_mutex, we know that no other rebind effort will
989 * be contending for the global variable cpuset_being_rebound.
990 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
991 * is idempotent. Also migrate pages in each mm to new nodes.
993 migrate
= is_memory_migrate(cs
);
994 for (i
= 0; i
< n
; i
++) {
995 struct mm_struct
*mm
= mmarray
[i
];
997 mpol_rebind_mm(mm
, &cs
->mems_allowed
);
999 cpuset_migrate_mm(mm
, &oldmem
, &cs
->mems_allowed
);
1003 /* We're done rebinding vma's to this cpusets new mems_allowed. */
1005 set_cpuset_being_rebound(NULL
);
1012 * Call with manage_mutex held.
1015 static int update_memory_pressure_enabled(struct cpuset
*cs
, char *buf
)
1017 if (simple_strtoul(buf
, NULL
, 10) != 0)
1018 cpuset_memory_pressure_enabled
= 1;
1020 cpuset_memory_pressure_enabled
= 0;
1025 * update_flag - read a 0 or a 1 in a file and update associated flag
1026 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
1027 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
1028 * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
1029 * cs: the cpuset to update
1030 * buf: the buffer where we read the 0 or 1
1032 * Call with manage_mutex held.
1035 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
, char *buf
)
1038 struct cpuset trialcs
;
1039 int err
, cpu_exclusive_changed
;
1041 turning_on
= (simple_strtoul(buf
, NULL
, 10) != 0);
1045 set_bit(bit
, &trialcs
.flags
);
1047 clear_bit(bit
, &trialcs
.flags
);
1049 err
= validate_change(cs
, &trialcs
);
1052 cpu_exclusive_changed
=
1053 (is_cpu_exclusive(cs
) != is_cpu_exclusive(&trialcs
));
1054 mutex_lock(&callback_mutex
);
1056 set_bit(bit
, &cs
->flags
);
1058 clear_bit(bit
, &cs
->flags
);
1059 mutex_unlock(&callback_mutex
);
1061 if (cpu_exclusive_changed
)
1062 update_cpu_domains(cs
);
1067 * Frequency meter - How fast is some event occuring?
1069 * These routines manage a digitally filtered, constant time based,
1070 * event frequency meter. There are four routines:
1071 * fmeter_init() - initialize a frequency meter.
1072 * fmeter_markevent() - called each time the event happens.
1073 * fmeter_getrate() - returns the recent rate of such events.
1074 * fmeter_update() - internal routine used to update fmeter.
1076 * A common data structure is passed to each of these routines,
1077 * which is used to keep track of the state required to manage the
1078 * frequency meter and its digital filter.
1080 * The filter works on the number of events marked per unit time.
1081 * The filter is single-pole low-pass recursive (IIR). The time unit
1082 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1083 * simulate 3 decimal digits of precision (multiplied by 1000).
1085 * With an FM_COEF of 933, and a time base of 1 second, the filter
1086 * has a half-life of 10 seconds, meaning that if the events quit
1087 * happening, then the rate returned from the fmeter_getrate()
1088 * will be cut in half each 10 seconds, until it converges to zero.
1090 * It is not worth doing a real infinitely recursive filter. If more
1091 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1092 * just compute FM_MAXTICKS ticks worth, by which point the level
1095 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1096 * arithmetic overflow in the fmeter_update() routine.
1098 * Given the simple 32 bit integer arithmetic used, this meter works
1099 * best for reporting rates between one per millisecond (msec) and
1100 * one per 32 (approx) seconds. At constant rates faster than one
1101 * per msec it maxes out at values just under 1,000,000. At constant
1102 * rates between one per msec, and one per second it will stabilize
1103 * to a value N*1000, where N is the rate of events per second.
1104 * At constant rates between one per second and one per 32 seconds,
1105 * it will be choppy, moving up on the seconds that have an event,
1106 * and then decaying until the next event. At rates slower than
1107 * about one in 32 seconds, it decays all the way back to zero between
1111 #define FM_COEF 933 /* coefficient for half-life of 10 secs */
1112 #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1113 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1114 #define FM_SCALE 1000 /* faux fixed point scale */
1116 /* Initialize a frequency meter */
1117 static void fmeter_init(struct fmeter
*fmp
)
1122 spin_lock_init(&fmp
->lock
);
1125 /* Internal meter update - process cnt events and update value */
1126 static void fmeter_update(struct fmeter
*fmp
)
1128 time_t now
= get_seconds();
1129 time_t ticks
= now
- fmp
->time
;
1134 ticks
= min(FM_MAXTICKS
, ticks
);
1136 fmp
->val
= (FM_COEF
* fmp
->val
) / FM_SCALE
;
1139 fmp
->val
+= ((FM_SCALE
- FM_COEF
) * fmp
->cnt
) / FM_SCALE
;
1143 /* Process any previous ticks, then bump cnt by one (times scale). */
1144 static void fmeter_markevent(struct fmeter
*fmp
)
1146 spin_lock(&fmp
->lock
);
1148 fmp
->cnt
= min(FM_MAXCNT
, fmp
->cnt
+ FM_SCALE
);
1149 spin_unlock(&fmp
->lock
);
1152 /* Process any previous ticks, then return current value. */
1153 static int fmeter_getrate(struct fmeter
*fmp
)
1157 spin_lock(&fmp
->lock
);
1160 spin_unlock(&fmp
->lock
);
1165 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
1166 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1167 * notified on release.
1169 * Call holding manage_mutex. May take callback_mutex and task_lock of
1170 * the task 'pid' during call.
1173 static int attach_task(struct cpuset
*cs
, char *pidbuf
, char **ppathbuf
)
1176 struct task_struct
*tsk
;
1177 struct cpuset
*oldcs
;
1179 nodemask_t from
, to
;
1180 struct mm_struct
*mm
;
1183 if (sscanf(pidbuf
, "%d", &pid
) != 1)
1185 if (cpus_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
))
1189 read_lock(&tasklist_lock
);
1191 tsk
= find_task_by_pid(pid
);
1192 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1193 read_unlock(&tasklist_lock
);
1197 get_task_struct(tsk
);
1198 read_unlock(&tasklist_lock
);
1200 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
1201 && (current
->euid
!= tsk
->suid
)) {
1202 put_task_struct(tsk
);
1207 get_task_struct(tsk
);
1210 retval
= security_task_setscheduler(tsk
, 0, NULL
);
1212 put_task_struct(tsk
);
1216 mutex_lock(&callback_mutex
);
1219 oldcs
= tsk
->cpuset
;
1222 mutex_unlock(&callback_mutex
);
1223 put_task_struct(tsk
);
1226 atomic_inc(&cs
->count
);
1227 rcu_assign_pointer(tsk
->cpuset
, cs
);
1230 guarantee_online_cpus(cs
, &cpus
);
1231 set_cpus_allowed(tsk
, cpus
);
1233 from
= oldcs
->mems_allowed
;
1234 to
= cs
->mems_allowed
;
1236 mutex_unlock(&callback_mutex
);
1238 mm
= get_task_mm(tsk
);
1240 mpol_rebind_mm(mm
, &to
);
1241 if (is_memory_migrate(cs
))
1242 cpuset_migrate_mm(mm
, &from
, &to
);
1246 put_task_struct(tsk
);
1248 if (atomic_dec_and_test(&oldcs
->count
))
1249 check_for_release(oldcs
, ppathbuf
);
1253 /* The various types of files and directories in a cpuset file system */
1258 FILE_MEMORY_MIGRATE
,
1263 FILE_NOTIFY_ON_RELEASE
,
1264 FILE_MEMORY_PRESSURE_ENABLED
,
1265 FILE_MEMORY_PRESSURE
,
1269 } cpuset_filetype_t
;
1271 static ssize_t
cpuset_common_file_write(struct file
*file
, const char __user
*userbuf
,
1272 size_t nbytes
, loff_t
*unused_ppos
)
1274 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1275 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1276 cpuset_filetype_t type
= cft
->private;
1278 char *pathbuf
= NULL
;
1281 /* Crude upper limit on largest legitimate cpulist user might write. */
1282 if (nbytes
> 100 + 6 * NR_CPUS
)
1285 /* +1 for nul-terminator */
1286 if ((buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
)) == 0)
1289 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
1293 buffer
[nbytes
] = 0; /* nul-terminate */
1295 mutex_lock(&manage_mutex
);
1297 if (is_removed(cs
)) {
1304 retval
= update_cpumask(cs
, buffer
);
1307 retval
= update_nodemask(cs
, buffer
);
1309 case FILE_CPU_EXCLUSIVE
:
1310 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, buffer
);
1312 case FILE_MEM_EXCLUSIVE
:
1313 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, buffer
);
1315 case FILE_NOTIFY_ON_RELEASE
:
1316 retval
= update_flag(CS_NOTIFY_ON_RELEASE
, cs
, buffer
);
1318 case FILE_MEMORY_MIGRATE
:
1319 retval
= update_flag(CS_MEMORY_MIGRATE
, cs
, buffer
);
1321 case FILE_MEMORY_PRESSURE_ENABLED
:
1322 retval
= update_memory_pressure_enabled(cs
, buffer
);
1324 case FILE_MEMORY_PRESSURE
:
1327 case FILE_SPREAD_PAGE
:
1328 retval
= update_flag(CS_SPREAD_PAGE
, cs
, buffer
);
1329 cs
->mems_generation
= cpuset_mems_generation
++;
1331 case FILE_SPREAD_SLAB
:
1332 retval
= update_flag(CS_SPREAD_SLAB
, cs
, buffer
);
1333 cs
->mems_generation
= cpuset_mems_generation
++;
1336 retval
= attach_task(cs
, buffer
, &pathbuf
);
1346 mutex_unlock(&manage_mutex
);
1347 cpuset_release_agent(pathbuf
);
1353 static ssize_t
cpuset_file_write(struct file
*file
, const char __user
*buf
,
1354 size_t nbytes
, loff_t
*ppos
)
1357 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1361 /* special function ? */
1363 retval
= cft
->write(file
, buf
, nbytes
, ppos
);
1365 retval
= cpuset_common_file_write(file
, buf
, nbytes
, ppos
);
1371 * These ascii lists should be read in a single call, by using a user
1372 * buffer large enough to hold the entire map. If read in smaller
1373 * chunks, there is no guarantee of atomicity. Since the display format
1374 * used, list of ranges of sequential numbers, is variable length,
1375 * and since these maps can change value dynamically, one could read
1376 * gibberish by doing partial reads while a list was changing.
1377 * A single large read to a buffer that crosses a page boundary is
1378 * ok, because the result being copied to user land is not recomputed
1379 * across a page fault.
1382 static int cpuset_sprintf_cpulist(char *page
, struct cpuset
*cs
)
1386 mutex_lock(&callback_mutex
);
1387 mask
= cs
->cpus_allowed
;
1388 mutex_unlock(&callback_mutex
);
1390 return cpulist_scnprintf(page
, PAGE_SIZE
, mask
);
1393 static int cpuset_sprintf_memlist(char *page
, struct cpuset
*cs
)
1397 mutex_lock(&callback_mutex
);
1398 mask
= cs
->mems_allowed
;
1399 mutex_unlock(&callback_mutex
);
1401 return nodelist_scnprintf(page
, PAGE_SIZE
, mask
);
1404 static ssize_t
cpuset_common_file_read(struct file
*file
, char __user
*buf
,
1405 size_t nbytes
, loff_t
*ppos
)
1407 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1408 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1409 cpuset_filetype_t type
= cft
->private;
1414 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
1421 s
+= cpuset_sprintf_cpulist(s
, cs
);
1424 s
+= cpuset_sprintf_memlist(s
, cs
);
1426 case FILE_CPU_EXCLUSIVE
:
1427 *s
++ = is_cpu_exclusive(cs
) ? '1' : '0';
1429 case FILE_MEM_EXCLUSIVE
:
1430 *s
++ = is_mem_exclusive(cs
) ? '1' : '0';
1432 case FILE_NOTIFY_ON_RELEASE
:
1433 *s
++ = notify_on_release(cs
) ? '1' : '0';
1435 case FILE_MEMORY_MIGRATE
:
1436 *s
++ = is_memory_migrate(cs
) ? '1' : '0';
1438 case FILE_MEMORY_PRESSURE_ENABLED
:
1439 *s
++ = cpuset_memory_pressure_enabled
? '1' : '0';
1441 case FILE_MEMORY_PRESSURE
:
1442 s
+= sprintf(s
, "%d", fmeter_getrate(&cs
->fmeter
));
1444 case FILE_SPREAD_PAGE
:
1445 *s
++ = is_spread_page(cs
) ? '1' : '0';
1447 case FILE_SPREAD_SLAB
:
1448 *s
++ = is_spread_slab(cs
) ? '1' : '0';
1456 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1458 free_page((unsigned long)page
);
1462 static ssize_t
cpuset_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1466 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1470 /* special function ? */
1472 retval
= cft
->read(file
, buf
, nbytes
, ppos
);
1474 retval
= cpuset_common_file_read(file
, buf
, nbytes
, ppos
);
1479 static int cpuset_file_open(struct inode
*inode
, struct file
*file
)
1484 err
= generic_file_open(inode
, file
);
1488 cft
= __d_cft(file
->f_dentry
);
1492 err
= cft
->open(inode
, file
);
1499 static int cpuset_file_release(struct inode
*inode
, struct file
*file
)
1501 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1503 return cft
->release(inode
, file
);
1508 * cpuset_rename - Only allow simple rename of directories in place.
1510 static int cpuset_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1511 struct inode
*new_dir
, struct dentry
*new_dentry
)
1513 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1515 if (new_dentry
->d_inode
)
1517 if (old_dir
!= new_dir
)
1519 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1522 static struct file_operations cpuset_file_operations
= {
1523 .read
= cpuset_file_read
,
1524 .write
= cpuset_file_write
,
1525 .llseek
= generic_file_llseek
,
1526 .open
= cpuset_file_open
,
1527 .release
= cpuset_file_release
,
1530 static struct inode_operations cpuset_dir_inode_operations
= {
1531 .lookup
= simple_lookup
,
1532 .mkdir
= cpuset_mkdir
,
1533 .rmdir
= cpuset_rmdir
,
1534 .rename
= cpuset_rename
,
1537 static int cpuset_create_file(struct dentry
*dentry
, int mode
)
1539 struct inode
*inode
;
1543 if (dentry
->d_inode
)
1546 inode
= cpuset_new_inode(mode
);
1550 if (S_ISDIR(mode
)) {
1551 inode
->i_op
= &cpuset_dir_inode_operations
;
1552 inode
->i_fop
= &simple_dir_operations
;
1554 /* start off with i_nlink == 2 (for "." entry) */
1556 } else if (S_ISREG(mode
)) {
1558 inode
->i_fop
= &cpuset_file_operations
;
1561 d_instantiate(dentry
, inode
);
1562 dget(dentry
); /* Extra count - pin the dentry in core */
1567 * cpuset_create_dir - create a directory for an object.
1568 * cs: the cpuset we create the directory for.
1569 * It must have a valid ->parent field
1570 * And we are going to fill its ->dentry field.
1571 * name: The name to give to the cpuset directory. Will be copied.
1572 * mode: mode to set on new directory.
1575 static int cpuset_create_dir(struct cpuset
*cs
, const char *name
, int mode
)
1577 struct dentry
*dentry
= NULL
;
1578 struct dentry
*parent
;
1581 parent
= cs
->parent
->dentry
;
1582 dentry
= cpuset_get_dentry(parent
, name
);
1584 return PTR_ERR(dentry
);
1585 error
= cpuset_create_file(dentry
, S_IFDIR
| mode
);
1587 dentry
->d_fsdata
= cs
;
1588 parent
->d_inode
->i_nlink
++;
1589 cs
->dentry
= dentry
;
1596 static int cpuset_add_file(struct dentry
*dir
, const struct cftype
*cft
)
1598 struct dentry
*dentry
;
1601 mutex_lock(&dir
->d_inode
->i_mutex
);
1602 dentry
= cpuset_get_dentry(dir
, cft
->name
);
1603 if (!IS_ERR(dentry
)) {
1604 error
= cpuset_create_file(dentry
, 0644 | S_IFREG
);
1606 dentry
->d_fsdata
= (void *)cft
;
1609 error
= PTR_ERR(dentry
);
1610 mutex_unlock(&dir
->d_inode
->i_mutex
);
1615 * Stuff for reading the 'tasks' file.
1617 * Reading this file can return large amounts of data if a cpuset has
1618 * *lots* of attached tasks. So it may need several calls to read(),
1619 * but we cannot guarantee that the information we produce is correct
1620 * unless we produce it entirely atomically.
1622 * Upon tasks file open(), a struct ctr_struct is allocated, that
1623 * will have a pointer to an array (also allocated here). The struct
1624 * ctr_struct * is stored in file->private_data. Its resources will
1625 * be freed by release() when the file is closed. The array is used
1626 * to sprintf the PIDs and then used by read().
1629 /* cpusets_tasks_read array */
1637 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1638 * Return actual number of pids loaded. No need to task_lock(p)
1639 * when reading out p->cpuset, as we don't really care if it changes
1640 * on the next cycle, and we are not going to try to dereference it.
1642 static int pid_array_load(pid_t
*pidarray
, int npids
, struct cpuset
*cs
)
1645 struct task_struct
*g
, *p
;
1647 read_lock(&tasklist_lock
);
1649 do_each_thread(g
, p
) {
1650 if (p
->cpuset
== cs
) {
1651 pidarray
[n
++] = p
->pid
;
1652 if (unlikely(n
== npids
))
1655 } while_each_thread(g
, p
);
1658 read_unlock(&tasklist_lock
);
1662 static int cmppid(const void *a
, const void *b
)
1664 return *(pid_t
*)a
- *(pid_t
*)b
;
1668 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1669 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1670 * count 'cnt' of how many chars would be written if buf were large enough.
1672 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1677 for (i
= 0; i
< npids
; i
++)
1678 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1683 * Handle an open on 'tasks' file. Prepare a buffer listing the
1684 * process id's of tasks currently attached to the cpuset being opened.
1686 * Does not require any specific cpuset mutexes, and does not take any.
1688 static int cpuset_tasks_open(struct inode
*unused
, struct file
*file
)
1690 struct cpuset
*cs
= __d_cs(file
->f_dentry
->d_parent
);
1691 struct ctr_struct
*ctr
;
1696 if (!(file
->f_mode
& FMODE_READ
))
1699 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1704 * If cpuset gets more users after we read count, we won't have
1705 * enough space - tough. This race is indistinguishable to the
1706 * caller from the case that the additional cpuset users didn't
1707 * show up until sometime later on.
1709 npids
= atomic_read(&cs
->count
);
1710 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1714 npids
= pid_array_load(pidarray
, npids
, cs
);
1715 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1717 /* Call pid_array_to_buf() twice, first just to get bufsz */
1718 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1719 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1722 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1725 file
->private_data
= ctr
;
1736 static ssize_t
cpuset_tasks_read(struct file
*file
, char __user
*buf
,
1737 size_t nbytes
, loff_t
*ppos
)
1739 struct ctr_struct
*ctr
= file
->private_data
;
1741 if (*ppos
+ nbytes
> ctr
->bufsz
)
1742 nbytes
= ctr
->bufsz
- *ppos
;
1743 if (copy_to_user(buf
, ctr
->buf
+ *ppos
, nbytes
))
1749 static int cpuset_tasks_release(struct inode
*unused_inode
, struct file
*file
)
1751 struct ctr_struct
*ctr
;
1753 if (file
->f_mode
& FMODE_READ
) {
1754 ctr
= file
->private_data
;
1762 * for the common functions, 'private' gives the type of file
1765 static struct cftype cft_tasks
= {
1767 .open
= cpuset_tasks_open
,
1768 .read
= cpuset_tasks_read
,
1769 .release
= cpuset_tasks_release
,
1770 .private = FILE_TASKLIST
,
1773 static struct cftype cft_cpus
= {
1775 .private = FILE_CPULIST
,
1778 static struct cftype cft_mems
= {
1780 .private = FILE_MEMLIST
,
1783 static struct cftype cft_cpu_exclusive
= {
1784 .name
= "cpu_exclusive",
1785 .private = FILE_CPU_EXCLUSIVE
,
1788 static struct cftype cft_mem_exclusive
= {
1789 .name
= "mem_exclusive",
1790 .private = FILE_MEM_EXCLUSIVE
,
1793 static struct cftype cft_notify_on_release
= {
1794 .name
= "notify_on_release",
1795 .private = FILE_NOTIFY_ON_RELEASE
,
1798 static struct cftype cft_memory_migrate
= {
1799 .name
= "memory_migrate",
1800 .private = FILE_MEMORY_MIGRATE
,
1803 static struct cftype cft_memory_pressure_enabled
= {
1804 .name
= "memory_pressure_enabled",
1805 .private = FILE_MEMORY_PRESSURE_ENABLED
,
1808 static struct cftype cft_memory_pressure
= {
1809 .name
= "memory_pressure",
1810 .private = FILE_MEMORY_PRESSURE
,
1813 static struct cftype cft_spread_page
= {
1814 .name
= "memory_spread_page",
1815 .private = FILE_SPREAD_PAGE
,
1818 static struct cftype cft_spread_slab
= {
1819 .name
= "memory_spread_slab",
1820 .private = FILE_SPREAD_SLAB
,
1823 static int cpuset_populate_dir(struct dentry
*cs_dentry
)
1827 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpus
)) < 0)
1829 if ((err
= cpuset_add_file(cs_dentry
, &cft_mems
)) < 0)
1831 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpu_exclusive
)) < 0)
1833 if ((err
= cpuset_add_file(cs_dentry
, &cft_mem_exclusive
)) < 0)
1835 if ((err
= cpuset_add_file(cs_dentry
, &cft_notify_on_release
)) < 0)
1837 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_migrate
)) < 0)
1839 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_pressure
)) < 0)
1841 if ((err
= cpuset_add_file(cs_dentry
, &cft_spread_page
)) < 0)
1843 if ((err
= cpuset_add_file(cs_dentry
, &cft_spread_slab
)) < 0)
1845 if ((err
= cpuset_add_file(cs_dentry
, &cft_tasks
)) < 0)
1851 * cpuset_create - create a cpuset
1852 * parent: cpuset that will be parent of the new cpuset.
1853 * name: name of the new cpuset. Will be strcpy'ed.
1854 * mode: mode to set on new inode
1856 * Must be called with the mutex on the parent inode held
1859 static long cpuset_create(struct cpuset
*parent
, const char *name
, int mode
)
1864 cs
= kmalloc(sizeof(*cs
), GFP_KERNEL
);
1868 mutex_lock(&manage_mutex
);
1869 cpuset_update_task_memory_state();
1871 if (notify_on_release(parent
))
1872 set_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
1873 if (is_spread_page(parent
))
1874 set_bit(CS_SPREAD_PAGE
, &cs
->flags
);
1875 if (is_spread_slab(parent
))
1876 set_bit(CS_SPREAD_SLAB
, &cs
->flags
);
1877 cs
->cpus_allowed
= CPU_MASK_NONE
;
1878 cs
->mems_allowed
= NODE_MASK_NONE
;
1879 atomic_set(&cs
->count
, 0);
1880 INIT_LIST_HEAD(&cs
->sibling
);
1881 INIT_LIST_HEAD(&cs
->children
);
1882 cs
->mems_generation
= cpuset_mems_generation
++;
1883 fmeter_init(&cs
->fmeter
);
1885 cs
->parent
= parent
;
1887 mutex_lock(&callback_mutex
);
1888 list_add(&cs
->sibling
, &cs
->parent
->children
);
1889 number_of_cpusets
++;
1890 mutex_unlock(&callback_mutex
);
1892 err
= cpuset_create_dir(cs
, name
, mode
);
1897 * Release manage_mutex before cpuset_populate_dir() because it
1898 * will down() this new directory's i_mutex and if we race with
1899 * another mkdir, we might deadlock.
1901 mutex_unlock(&manage_mutex
);
1903 err
= cpuset_populate_dir(cs
->dentry
);
1904 /* If err < 0, we have a half-filled directory - oh well ;) */
1907 list_del(&cs
->sibling
);
1908 mutex_unlock(&manage_mutex
);
1913 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1915 struct cpuset
*c_parent
= dentry
->d_parent
->d_fsdata
;
1917 /* the vfs holds inode->i_mutex already */
1918 return cpuset_create(c_parent
, dentry
->d_name
.name
, mode
| S_IFDIR
);
1921 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
1923 struct cpuset
*cs
= dentry
->d_fsdata
;
1925 struct cpuset
*parent
;
1926 char *pathbuf
= NULL
;
1928 /* the vfs holds both inode->i_mutex already */
1930 mutex_lock(&manage_mutex
);
1931 cpuset_update_task_memory_state();
1932 if (atomic_read(&cs
->count
) > 0) {
1933 mutex_unlock(&manage_mutex
);
1936 if (!list_empty(&cs
->children
)) {
1937 mutex_unlock(&manage_mutex
);
1940 parent
= cs
->parent
;
1941 mutex_lock(&callback_mutex
);
1942 set_bit(CS_REMOVED
, &cs
->flags
);
1943 if (is_cpu_exclusive(cs
))
1944 update_cpu_domains(cs
);
1945 list_del(&cs
->sibling
); /* delete my sibling from parent->children */
1946 spin_lock(&cs
->dentry
->d_lock
);
1947 d
= dget(cs
->dentry
);
1949 spin_unlock(&d
->d_lock
);
1950 cpuset_d_remove_dir(d
);
1952 number_of_cpusets
--;
1953 mutex_unlock(&callback_mutex
);
1954 if (list_empty(&parent
->children
))
1955 check_for_release(parent
, &pathbuf
);
1956 mutex_unlock(&manage_mutex
);
1957 cpuset_release_agent(pathbuf
);
1962 * cpuset_init_early - just enough so that the calls to
1963 * cpuset_update_task_memory_state() in early init code
1967 int __init
cpuset_init_early(void)
1969 struct task_struct
*tsk
= current
;
1971 tsk
->cpuset
= &top_cpuset
;
1972 tsk
->cpuset
->mems_generation
= cpuset_mems_generation
++;
1977 * cpuset_init - initialize cpusets at system boot
1979 * Description: Initialize top_cpuset and the cpuset internal file system,
1982 int __init
cpuset_init(void)
1984 struct dentry
*root
;
1987 top_cpuset
.cpus_allowed
= CPU_MASK_ALL
;
1988 top_cpuset
.mems_allowed
= NODE_MASK_ALL
;
1990 fmeter_init(&top_cpuset
.fmeter
);
1991 top_cpuset
.mems_generation
= cpuset_mems_generation
++;
1993 init_task
.cpuset
= &top_cpuset
;
1995 err
= register_filesystem(&cpuset_fs_type
);
1998 cpuset_mount
= kern_mount(&cpuset_fs_type
);
1999 if (IS_ERR(cpuset_mount
)) {
2000 printk(KERN_ERR
"cpuset: could not mount!\n");
2001 err
= PTR_ERR(cpuset_mount
);
2002 cpuset_mount
= NULL
;
2005 root
= cpuset_mount
->mnt_sb
->s_root
;
2006 root
->d_fsdata
= &top_cpuset
;
2007 root
->d_inode
->i_nlink
++;
2008 top_cpuset
.dentry
= root
;
2009 root
->d_inode
->i_op
= &cpuset_dir_inode_operations
;
2010 number_of_cpusets
= 1;
2011 err
= cpuset_populate_dir(root
);
2012 /* memory_pressure_enabled is in root cpuset only */
2014 err
= cpuset_add_file(root
, &cft_memory_pressure_enabled
);
2020 * cpuset_init_smp - initialize cpus_allowed
2022 * Description: Finish top cpuset after cpu, node maps are initialized
2025 void __init
cpuset_init_smp(void)
2027 top_cpuset
.cpus_allowed
= cpu_online_map
;
2028 top_cpuset
.mems_allowed
= node_online_map
;
2032 * cpuset_fork - attach newly forked task to its parents cpuset.
2033 * @tsk: pointer to task_struct of forking parent process.
2035 * Description: A task inherits its parent's cpuset at fork().
2037 * A pointer to the shared cpuset was automatically copied in fork.c
2038 * by dup_task_struct(). However, we ignore that copy, since it was
2039 * not made under the protection of task_lock(), so might no longer be
2040 * a valid cpuset pointer. attach_task() might have already changed
2041 * current->cpuset, allowing the previously referenced cpuset to
2042 * be removed and freed. Instead, we task_lock(current) and copy
2043 * its present value of current->cpuset for our freshly forked child.
2045 * At the point that cpuset_fork() is called, 'current' is the parent
2046 * task, and the passed argument 'child' points to the child task.
2049 void cpuset_fork(struct task_struct
*child
)
2052 child
->cpuset
= current
->cpuset
;
2053 atomic_inc(&child
->cpuset
->count
);
2054 task_unlock(current
);
2058 * cpuset_exit - detach cpuset from exiting task
2059 * @tsk: pointer to task_struct of exiting process
2061 * Description: Detach cpuset from @tsk and release it.
2063 * Note that cpusets marked notify_on_release force every task in
2064 * them to take the global manage_mutex mutex when exiting.
2065 * This could impact scaling on very large systems. Be reluctant to
2066 * use notify_on_release cpusets where very high task exit scaling
2067 * is required on large systems.
2069 * Don't even think about derefencing 'cs' after the cpuset use count
2070 * goes to zero, except inside a critical section guarded by manage_mutex
2071 * or callback_mutex. Otherwise a zero cpuset use count is a license to
2072 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
2074 * This routine has to take manage_mutex, not callback_mutex, because
2075 * it is holding that mutex while calling check_for_release(),
2076 * which calls kmalloc(), so can't be called holding callback_mutex().
2078 * We don't need to task_lock() this reference to tsk->cpuset,
2079 * because tsk is already marked PF_EXITING, so attach_task() won't
2080 * mess with it, or task is a failed fork, never visible to attach_task.
2082 * the_top_cpuset_hack:
2084 * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
2086 * Don't leave a task unable to allocate memory, as that is an
2087 * accident waiting to happen should someone add a callout in
2088 * do_exit() after the cpuset_exit() call that might allocate.
2089 * If a task tries to allocate memory with an invalid cpuset,
2090 * it will oops in cpuset_update_task_memory_state().
2092 * We call cpuset_exit() while the task is still competent to
2093 * handle notify_on_release(), then leave the task attached to
2094 * the root cpuset (top_cpuset) for the remainder of its exit.
2096 * To do this properly, we would increment the reference count on
2097 * top_cpuset, and near the very end of the kernel/exit.c do_exit()
2098 * code we would add a second cpuset function call, to drop that
2099 * reference. This would just create an unnecessary hot spot on
2100 * the top_cpuset reference count, to no avail.
2102 * Normally, holding a reference to a cpuset without bumping its
2103 * count is unsafe. The cpuset could go away, or someone could
2104 * attach us to a different cpuset, decrementing the count on
2105 * the first cpuset that we never incremented. But in this case,
2106 * top_cpuset isn't going away, and either task has PF_EXITING set,
2107 * which wards off any attach_task() attempts, or task is a failed
2108 * fork, never visible to attach_task.
2110 * Another way to do this would be to set the cpuset pointer
2111 * to NULL here, and check in cpuset_update_task_memory_state()
2112 * for a NULL pointer. This hack avoids that NULL check, for no
2113 * cost (other than this way too long comment ;).
2116 void cpuset_exit(struct task_struct
*tsk
)
2121 tsk
->cpuset
= &top_cpuset
; /* the_top_cpuset_hack - see above */
2123 if (notify_on_release(cs
)) {
2124 char *pathbuf
= NULL
;
2126 mutex_lock(&manage_mutex
);
2127 if (atomic_dec_and_test(&cs
->count
))
2128 check_for_release(cs
, &pathbuf
);
2129 mutex_unlock(&manage_mutex
);
2130 cpuset_release_agent(pathbuf
);
2132 atomic_dec(&cs
->count
);
2137 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2138 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2140 * Description: Returns the cpumask_t cpus_allowed of the cpuset
2141 * attached to the specified @tsk. Guaranteed to return some non-empty
2142 * subset of cpu_online_map, even if this means going outside the
2146 cpumask_t
cpuset_cpus_allowed(struct task_struct
*tsk
)
2150 mutex_lock(&callback_mutex
);
2152 guarantee_online_cpus(tsk
->cpuset
, &mask
);
2154 mutex_unlock(&callback_mutex
);
2159 void cpuset_init_current_mems_allowed(void)
2161 current
->mems_allowed
= NODE_MASK_ALL
;
2165 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2166 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2168 * Description: Returns the nodemask_t mems_allowed of the cpuset
2169 * attached to the specified @tsk. Guaranteed to return some non-empty
2170 * subset of node_online_map, even if this means going outside the
2174 nodemask_t
cpuset_mems_allowed(struct task_struct
*tsk
)
2178 mutex_lock(&callback_mutex
);
2180 guarantee_online_mems(tsk
->cpuset
, &mask
);
2182 mutex_unlock(&callback_mutex
);
2188 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2189 * @zl: the zonelist to be checked
2191 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2193 int cpuset_zonelist_valid_mems_allowed(struct zonelist
*zl
)
2197 for (i
= 0; zl
->zones
[i
]; i
++) {
2198 int nid
= zl
->zones
[i
]->zone_pgdat
->node_id
;
2200 if (node_isset(nid
, current
->mems_allowed
))
2207 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
2208 * ancestor to the specified cpuset. Call holding callback_mutex.
2209 * If no ancestor is mem_exclusive (an unusual configuration), then
2210 * returns the root cpuset.
2212 static const struct cpuset
*nearest_exclusive_ancestor(const struct cpuset
*cs
)
2214 while (!is_mem_exclusive(cs
) && cs
->parent
)
2220 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
2221 * @z: is this zone on an allowed node?
2222 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
2224 * If we're in interrupt, yes, we can always allocate. If zone
2225 * z's node is in our tasks mems_allowed, yes. If it's not a
2226 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2227 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
2230 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2231 * and do not allow allocations outside the current tasks cpuset.
2232 * GFP_KERNEL allocations are not so marked, so can escape to the
2233 * nearest mem_exclusive ancestor cpuset.
2235 * Scanning up parent cpusets requires callback_mutex. The __alloc_pages()
2236 * routine only calls here with __GFP_HARDWALL bit _not_ set if
2237 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
2238 * mems_allowed came up empty on the first pass over the zonelist.
2239 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
2240 * short of memory, might require taking the callback_mutex mutex.
2242 * The first call here from mm/page_alloc:get_page_from_freelist()
2243 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, so
2244 * no allocation on a node outside the cpuset is allowed (unless in
2245 * interrupt, of course).
2247 * The second pass through get_page_from_freelist() doesn't even call
2248 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2249 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2250 * in alloc_flags. That logic and the checks below have the combined
2252 * in_interrupt - any node ok (current task context irrelevant)
2253 * GFP_ATOMIC - any node ok
2254 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2255 * GFP_USER - only nodes in current tasks mems allowed ok.
2258 * Don't call cpuset_zone_allowed() if you can't sleep, unless you
2259 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2260 * the code that might scan up ancestor cpusets and sleep.
2263 int __cpuset_zone_allowed(struct zone
*z
, gfp_t gfp_mask
)
2265 int node
; /* node that zone z is on */
2266 const struct cpuset
*cs
; /* current cpuset ancestors */
2267 int allowed
; /* is allocation in zone z allowed? */
2271 node
= z
->zone_pgdat
->node_id
;
2272 might_sleep_if(!(gfp_mask
& __GFP_HARDWALL
));
2273 if (node_isset(node
, current
->mems_allowed
))
2275 if (gfp_mask
& __GFP_HARDWALL
) /* If hardwall request, stop here */
2278 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
2281 /* Not hardwall and node outside mems_allowed: scan up cpusets */
2282 mutex_lock(&callback_mutex
);
2285 cs
= nearest_exclusive_ancestor(current
->cpuset
);
2286 task_unlock(current
);
2288 allowed
= node_isset(node
, cs
->mems_allowed
);
2289 mutex_unlock(&callback_mutex
);
2294 * cpuset_lock - lock out any changes to cpuset structures
2296 * The out of memory (oom) code needs to mutex_lock cpusets
2297 * from being changed while it scans the tasklist looking for a
2298 * task in an overlapping cpuset. Expose callback_mutex via this
2299 * cpuset_lock() routine, so the oom code can lock it, before
2300 * locking the task list. The tasklist_lock is a spinlock, so
2301 * must be taken inside callback_mutex.
2304 void cpuset_lock(void)
2306 mutex_lock(&callback_mutex
);
2310 * cpuset_unlock - release lock on cpuset changes
2312 * Undo the lock taken in a previous cpuset_lock() call.
2315 void cpuset_unlock(void)
2317 mutex_unlock(&callback_mutex
);
2321 * cpuset_mem_spread_node() - On which node to begin search for a page
2323 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2324 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2325 * and if the memory allocation used cpuset_mem_spread_node()
2326 * to determine on which node to start looking, as it will for
2327 * certain page cache or slab cache pages such as used for file
2328 * system buffers and inode caches, then instead of starting on the
2329 * local node to look for a free page, rather spread the starting
2330 * node around the tasks mems_allowed nodes.
2332 * We don't have to worry about the returned node being offline
2333 * because "it can't happen", and even if it did, it would be ok.
2335 * The routines calling guarantee_online_mems() are careful to
2336 * only set nodes in task->mems_allowed that are online. So it
2337 * should not be possible for the following code to return an
2338 * offline node. But if it did, that would be ok, as this routine
2339 * is not returning the node where the allocation must be, only
2340 * the node where the search should start. The zonelist passed to
2341 * __alloc_pages() will include all nodes. If the slab allocator
2342 * is passed an offline node, it will fall back to the local node.
2343 * See kmem_cache_alloc_node().
2346 int cpuset_mem_spread_node(void)
2350 node
= next_node(current
->cpuset_mem_spread_rotor
, current
->mems_allowed
);
2351 if (node
== MAX_NUMNODES
)
2352 node
= first_node(current
->mems_allowed
);
2353 current
->cpuset_mem_spread_rotor
= node
;
2356 EXPORT_SYMBOL_GPL(cpuset_mem_spread_node
);
2359 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2360 * @p: pointer to task_struct of some other task.
2362 * Description: Return true if the nearest mem_exclusive ancestor
2363 * cpusets of tasks @p and current overlap. Used by oom killer to
2364 * determine if task @p's memory usage might impact the memory
2365 * available to the current task.
2367 * Call while holding callback_mutex.
2370 int cpuset_excl_nodes_overlap(const struct task_struct
*p
)
2372 const struct cpuset
*cs1
, *cs2
; /* my and p's cpuset ancestors */
2373 int overlap
= 0; /* do cpusets overlap? */
2376 if (current
->flags
& PF_EXITING
) {
2377 task_unlock(current
);
2380 cs1
= nearest_exclusive_ancestor(current
->cpuset
);
2381 task_unlock(current
);
2383 task_lock((struct task_struct
*)p
);
2384 if (p
->flags
& PF_EXITING
) {
2385 task_unlock((struct task_struct
*)p
);
2388 cs2
= nearest_exclusive_ancestor(p
->cpuset
);
2389 task_unlock((struct task_struct
*)p
);
2391 overlap
= nodes_intersects(cs1
->mems_allowed
, cs2
->mems_allowed
);
2397 * Collection of memory_pressure is suppressed unless
2398 * this flag is enabled by writing "1" to the special
2399 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2402 int cpuset_memory_pressure_enabled __read_mostly
;
2405 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2407 * Keep a running average of the rate of synchronous (direct)
2408 * page reclaim efforts initiated by tasks in each cpuset.
2410 * This represents the rate at which some task in the cpuset
2411 * ran low on memory on all nodes it was allowed to use, and
2412 * had to enter the kernels page reclaim code in an effort to
2413 * create more free memory by tossing clean pages or swapping
2414 * or writing dirty pages.
2416 * Display to user space in the per-cpuset read-only file
2417 * "memory_pressure". Value displayed is an integer
2418 * representing the recent rate of entry into the synchronous
2419 * (direct) page reclaim by any task attached to the cpuset.
2422 void __cpuset_memory_pressure_bump(void)
2427 cs
= current
->cpuset
;
2428 fmeter_markevent(&cs
->fmeter
);
2429 task_unlock(current
);
2433 * proc_cpuset_show()
2434 * - Print tasks cpuset path into seq_file.
2435 * - Used for /proc/<pid>/cpuset.
2436 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2437 * doesn't really matter if tsk->cpuset changes after we read it,
2438 * and we take manage_mutex, keeping attach_task() from changing it
2439 * anyway. No need to check that tsk->cpuset != NULL, thanks to
2440 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
2441 * cpuset to top_cpuset.
2443 static int proc_cpuset_show(struct seq_file
*m
, void *v
)
2446 struct task_struct
*tsk
;
2451 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2457 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
2462 mutex_lock(&manage_mutex
);
2464 retval
= cpuset_path(tsk
->cpuset
, buf
, PAGE_SIZE
);
2470 mutex_unlock(&manage_mutex
);
2471 put_task_struct(tsk
);
2478 static int cpuset_open(struct inode
*inode
, struct file
*file
)
2480 struct pid
*pid
= PROC_I(inode
)->pid
;
2481 return single_open(file
, proc_cpuset_show
, pid
);
2484 struct file_operations proc_cpuset_operations
= {
2485 .open
= cpuset_open
,
2487 .llseek
= seq_lseek
,
2488 .release
= single_release
,
2491 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2492 char *cpuset_task_status_allowed(struct task_struct
*task
, char *buffer
)
2494 buffer
+= sprintf(buffer
, "Cpus_allowed:\t");
2495 buffer
+= cpumask_scnprintf(buffer
, PAGE_SIZE
, task
->cpus_allowed
);
2496 buffer
+= sprintf(buffer
, "\n");
2497 buffer
+= sprintf(buffer
, "Mems_allowed:\t");
2498 buffer
+= nodemask_scnprintf(buffer
, PAGE_SIZE
, task
->mems_allowed
);
2499 buffer
+= sprintf(buffer
, "\n");