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/cpu.h>
22 #include <linux/cpumask.h>
23 #include <linux/cpuset.h>
24 #include <linux/err.h>
25 #include <linux/errno.h>
26 #include <linux/file.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/kmod.h>
32 #include <linux/list.h>
33 #include <linux/mempolicy.h>
35 #include <linux/module.h>
36 #include <linux/mount.h>
37 #include <linux/namei.h>
38 #include <linux/pagemap.h>
39 #include <linux/proc_fs.h>
40 #include <linux/rcupdate.h>
41 #include <linux/sched.h>
42 #include <linux/seq_file.h>
43 #include <linux/security.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 <linux/mutex.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 __read_mostly
;
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 */
110 CS_NOTIFY_ON_RELEASE
,
115 /* convenient tests for these bits */
116 static inline int is_cpu_exclusive(const struct cpuset
*cs
)
118 return test_bit(CS_CPU_EXCLUSIVE
, &cs
->flags
);
121 static inline int is_mem_exclusive(const struct cpuset
*cs
)
123 return test_bit(CS_MEM_EXCLUSIVE
, &cs
->flags
);
126 static inline int is_removed(const struct cpuset
*cs
)
128 return test_bit(CS_REMOVED
, &cs
->flags
);
131 static inline int notify_on_release(const struct cpuset
*cs
)
133 return test_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
136 static inline int is_memory_migrate(const struct cpuset
*cs
)
138 return test_bit(CS_MEMORY_MIGRATE
, &cs
->flags
);
141 static inline int is_spread_page(const struct cpuset
*cs
)
143 return test_bit(CS_SPREAD_PAGE
, &cs
->flags
);
146 static inline int is_spread_slab(const struct cpuset
*cs
)
148 return test_bit(CS_SPREAD_SLAB
, &cs
->flags
);
152 * Increment this integer everytime any cpuset changes its
153 * mems_allowed value. Users of cpusets can track this generation
154 * number, and avoid having to lock and reload mems_allowed unless
155 * the cpuset they're using changes generation.
157 * A single, global generation is needed because attach_task() could
158 * reattach a task to a different cpuset, which must not have its
159 * generation numbers aliased with those of that tasks previous cpuset.
161 * Generations are needed for mems_allowed because one task cannot
162 * modify anothers memory placement. So we must enable every task,
163 * on every visit to __alloc_pages(), to efficiently check whether
164 * its current->cpuset->mems_allowed has changed, requiring an update
165 * of its current->mems_allowed.
167 * Since cpuset_mems_generation is guarded by manage_mutex,
168 * there is no need to mark it atomic.
170 static int cpuset_mems_generation
;
172 static struct cpuset top_cpuset
= {
173 .flags
= ((1 << CS_CPU_EXCLUSIVE
) | (1 << CS_MEM_EXCLUSIVE
)),
174 .cpus_allowed
= CPU_MASK_ALL
,
175 .mems_allowed
= NODE_MASK_ALL
,
176 .count
= ATOMIC_INIT(0),
177 .sibling
= LIST_HEAD_INIT(top_cpuset
.sibling
),
178 .children
= LIST_HEAD_INIT(top_cpuset
.children
),
181 static struct vfsmount
*cpuset_mount
;
182 static struct super_block
*cpuset_sb
;
185 * We have two global cpuset mutexes below. They can nest.
186 * It is ok to first take manage_mutex, then nest callback_mutex. We also
187 * require taking task_lock() when dereferencing a tasks cpuset pointer.
188 * See "The task_lock() exception", at the end of this comment.
190 * A task must hold both mutexes to modify cpusets. If a task
191 * holds manage_mutex, then it blocks others wanting that mutex,
192 * ensuring that it is the only task able to also acquire callback_mutex
193 * and be able to modify cpusets. It can perform various checks on
194 * the cpuset structure first, knowing nothing will change. It can
195 * also allocate memory while just holding manage_mutex. While it is
196 * performing these checks, various callback routines can briefly
197 * acquire callback_mutex to query cpusets. Once it is ready to make
198 * the changes, it takes callback_mutex, blocking everyone else.
200 * Calls to the kernel memory allocator can not be made while holding
201 * callback_mutex, as that would risk double tripping on callback_mutex
202 * from one of the callbacks into the cpuset code from within
205 * If a task is only holding callback_mutex, then it has read-only
208 * The task_struct fields mems_allowed and mems_generation may only
209 * be accessed in the context of that task, so require no locks.
211 * Any task can increment and decrement the count field without lock.
212 * So in general, code holding manage_mutex or callback_mutex can't rely
213 * on the count field not changing. However, if the count goes to
214 * zero, then only attach_task(), which holds both mutexes, can
215 * increment it again. Because a count of zero means that no tasks
216 * are currently attached, therefore there is no way a task attached
217 * to that cpuset can fork (the other way to increment the count).
218 * So code holding manage_mutex or callback_mutex can safely assume that
219 * if the count is zero, it will stay zero. Similarly, if a task
220 * holds manage_mutex or callback_mutex on a cpuset with zero count, it
221 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
222 * both of those mutexes.
224 * The cpuset_common_file_write handler for operations that modify
225 * the cpuset hierarchy holds manage_mutex across the entire operation,
226 * single threading all such cpuset modifications across the system.
228 * The cpuset_common_file_read() handlers only hold callback_mutex across
229 * small pieces of code, such as when reading out possibly multi-word
230 * cpumasks and nodemasks.
232 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
233 * (usually) take either mutex. These are the two most performance
234 * critical pieces of code here. The exception occurs on cpuset_exit(),
235 * when a task in a notify_on_release cpuset exits. Then manage_mutex
236 * is taken, and if the cpuset count is zero, a usermode call made
237 * to /sbin/cpuset_release_agent with the name of the cpuset (path
238 * relative to the root of cpuset file system) as the argument.
240 * A cpuset can only be deleted if both its 'count' of using tasks
241 * is zero, and its list of 'children' cpusets is empty. Since all
242 * tasks in the system use _some_ cpuset, and since there is always at
243 * least one task in the system (init), therefore, top_cpuset
244 * always has either children cpusets and/or using tasks. So we don't
245 * need a special hack to ensure that top_cpuset cannot be deleted.
247 * The above "Tale of Two Semaphores" would be complete, but for:
249 * The task_lock() exception
251 * The need for this exception arises from the action of attach_task(),
252 * which overwrites one tasks cpuset pointer with another. It does
253 * so using both mutexes, however there are several performance
254 * critical places that need to reference task->cpuset without the
255 * expense of grabbing a system global mutex. Therefore except as
256 * noted below, when dereferencing or, as in attach_task(), modifying
257 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
258 * (task->alloc_lock) already in the task_struct routinely used for
261 * P.S. One more locking exception. RCU is used to guard the
262 * update of a tasks cpuset pointer by attach_task() and the
263 * access of task->cpuset->mems_generation via that pointer in
264 * the routine cpuset_update_task_memory_state().
267 static DEFINE_MUTEX(manage_mutex
);
268 static DEFINE_MUTEX(callback_mutex
);
271 * A couple of forward declarations required, due to cyclic reference loop:
272 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
273 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
276 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
277 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
279 static struct backing_dev_info cpuset_backing_dev_info
= {
280 .ra_pages
= 0, /* No readahead */
281 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
284 static struct inode
*cpuset_new_inode(mode_t mode
)
286 struct inode
*inode
= new_inode(cpuset_sb
);
289 inode
->i_mode
= mode
;
290 inode
->i_uid
= current
->fsuid
;
291 inode
->i_gid
= current
->fsgid
;
293 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
294 inode
->i_mapping
->backing_dev_info
= &cpuset_backing_dev_info
;
299 static void cpuset_diput(struct dentry
*dentry
, struct inode
*inode
)
301 /* is dentry a directory ? if so, kfree() associated cpuset */
302 if (S_ISDIR(inode
->i_mode
)) {
303 struct cpuset
*cs
= dentry
->d_fsdata
;
304 BUG_ON(!(is_removed(cs
)));
310 static struct dentry_operations cpuset_dops
= {
311 .d_iput
= cpuset_diput
,
314 static struct dentry
*cpuset_get_dentry(struct dentry
*parent
, const char *name
)
316 struct dentry
*d
= lookup_one_len(name
, parent
, strlen(name
));
318 d
->d_op
= &cpuset_dops
;
322 static void remove_dir(struct dentry
*d
)
324 struct dentry
*parent
= dget(d
->d_parent
);
327 simple_rmdir(parent
->d_inode
, d
);
332 * NOTE : the dentry must have been dget()'ed
334 static void cpuset_d_remove_dir(struct dentry
*dentry
)
336 struct list_head
*node
;
338 spin_lock(&dcache_lock
);
339 node
= dentry
->d_subdirs
.next
;
340 while (node
!= &dentry
->d_subdirs
) {
341 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
345 spin_unlock(&dcache_lock
);
347 simple_unlink(dentry
->d_inode
, d
);
349 spin_lock(&dcache_lock
);
351 node
= dentry
->d_subdirs
.next
;
353 list_del_init(&dentry
->d_u
.d_child
);
354 spin_unlock(&dcache_lock
);
358 static struct super_operations cpuset_ops
= {
359 .statfs
= simple_statfs
,
360 .drop_inode
= generic_delete_inode
,
363 static int cpuset_fill_super(struct super_block
*sb
, void *unused_data
,
369 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
370 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
371 sb
->s_magic
= CPUSET_SUPER_MAGIC
;
372 sb
->s_op
= &cpuset_ops
;
375 inode
= cpuset_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
);
377 inode
->i_op
= &simple_dir_inode_operations
;
378 inode
->i_fop
= &simple_dir_operations
;
379 /* directories start off with i_nlink == 2 (for "." entry) */
385 root
= d_alloc_root(inode
);
394 static int cpuset_get_sb(struct file_system_type
*fs_type
,
395 int flags
, const char *unused_dev_name
,
396 void *data
, struct vfsmount
*mnt
)
398 return get_sb_single(fs_type
, flags
, data
, cpuset_fill_super
, mnt
);
401 static struct file_system_type cpuset_fs_type
= {
403 .get_sb
= cpuset_get_sb
,
404 .kill_sb
= kill_litter_super
,
409 * The files in the cpuset filesystem mostly have a very simple read/write
410 * handling, some common function will take care of it. Nevertheless some cases
411 * (read tasks) are special and therefore I define this structure for every
415 * When reading/writing to a file:
416 * - the cpuset to use in file->f_path.dentry->d_parent->d_fsdata
417 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
423 int (*open
) (struct inode
*inode
, struct file
*file
);
424 ssize_t (*read
) (struct file
*file
, char __user
*buf
, size_t nbytes
,
426 int (*write
) (struct file
*file
, const char __user
*buf
, size_t nbytes
,
428 int (*release
) (struct inode
*inode
, struct file
*file
);
431 static inline struct cpuset
*__d_cs(struct dentry
*dentry
)
433 return dentry
->d_fsdata
;
436 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
438 return dentry
->d_fsdata
;
442 * Call with manage_mutex held. Writes path of cpuset into buf.
443 * Returns 0 on success, -errno on error.
446 static int cpuset_path(const struct cpuset
*cs
, char *buf
, int buflen
)
450 start
= buf
+ buflen
;
454 int len
= cs
->dentry
->d_name
.len
;
455 if ((start
-= len
) < buf
)
456 return -ENAMETOOLONG
;
457 memcpy(start
, cs
->dentry
->d_name
.name
, len
);
464 return -ENAMETOOLONG
;
467 memmove(buf
, start
, buf
+ buflen
- start
);
472 * Notify userspace when a cpuset is released, by running
473 * /sbin/cpuset_release_agent with the name of the cpuset (path
474 * relative to the root of cpuset file system) as the argument.
476 * Most likely, this user command will try to rmdir this cpuset.
478 * This races with the possibility that some other task will be
479 * attached to this cpuset before it is removed, or that some other
480 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
481 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
482 * unused, and this cpuset will be reprieved from its death sentence,
483 * to continue to serve a useful existence. Next time it's released,
484 * we will get notified again, if it still has 'notify_on_release' set.
486 * The final arg to call_usermodehelper() is 0, which means don't
487 * wait. The separate /sbin/cpuset_release_agent task is forked by
488 * call_usermodehelper(), then control in this thread returns here,
489 * without waiting for the release agent task. We don't bother to
490 * wait because the caller of this routine has no use for the exit
491 * status of the /sbin/cpuset_release_agent task, so no sense holding
492 * our caller up for that.
494 * When we had only one cpuset mutex, we had to call this
495 * without holding it, to avoid deadlock when call_usermodehelper()
496 * allocated memory. With two locks, we could now call this while
497 * holding manage_mutex, but we still don't, so as to minimize
498 * the time manage_mutex is held.
501 static void cpuset_release_agent(const char *pathbuf
)
503 char *argv
[3], *envp
[3];
510 argv
[i
++] = "/sbin/cpuset_release_agent";
511 argv
[i
++] = (char *)pathbuf
;
515 /* minimal command environment */
516 envp
[i
++] = "HOME=/";
517 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
520 call_usermodehelper(argv
[0], argv
, envp
, 0);
525 * Either cs->count of using tasks transitioned to zero, or the
526 * cs->children list of child cpusets just became empty. If this
527 * cs is notify_on_release() and now both the user count is zero and
528 * the list of children is empty, prepare cpuset path in a kmalloc'd
529 * buffer, to be returned via ppathbuf, so that the caller can invoke
530 * cpuset_release_agent() with it later on, once manage_mutex is dropped.
531 * Call here with manage_mutex held.
533 * This check_for_release() routine is responsible for kmalloc'ing
534 * pathbuf. The above cpuset_release_agent() is responsible for
535 * kfree'ing pathbuf. The caller of these routines is responsible
536 * for providing a pathbuf pointer, initialized to NULL, then
537 * calling check_for_release() with manage_mutex held and the address
538 * of the pathbuf pointer, then dropping manage_mutex, then calling
539 * cpuset_release_agent() with pathbuf, as set by check_for_release().
542 static void check_for_release(struct cpuset
*cs
, char **ppathbuf
)
544 if (notify_on_release(cs
) && atomic_read(&cs
->count
) == 0 &&
545 list_empty(&cs
->children
)) {
548 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
551 if (cpuset_path(cs
, buf
, PAGE_SIZE
) < 0)
559 * Return in *pmask the portion of a cpusets's cpus_allowed that
560 * are online. If none are online, walk up the cpuset hierarchy
561 * until we find one that does have some online cpus. If we get
562 * all the way to the top and still haven't found any online cpus,
563 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
564 * task, return cpu_online_map.
566 * One way or another, we guarantee to return some non-empty subset
569 * Call with callback_mutex held.
572 static void guarantee_online_cpus(const struct cpuset
*cs
, cpumask_t
*pmask
)
574 while (cs
&& !cpus_intersects(cs
->cpus_allowed
, cpu_online_map
))
577 cpus_and(*pmask
, cs
->cpus_allowed
, cpu_online_map
);
579 *pmask
= cpu_online_map
;
580 BUG_ON(!cpus_intersects(*pmask
, cpu_online_map
));
584 * Return in *pmask the portion of a cpusets's mems_allowed that
585 * are online. If none are online, walk up the cpuset hierarchy
586 * until we find one that does have some online mems. If we get
587 * all the way to the top and still haven't found any online mems,
588 * return node_online_map.
590 * One way or another, we guarantee to return some non-empty subset
591 * of node_online_map.
593 * Call with callback_mutex held.
596 static void guarantee_online_mems(const struct cpuset
*cs
, nodemask_t
*pmask
)
598 while (cs
&& !nodes_intersects(cs
->mems_allowed
, node_online_map
))
601 nodes_and(*pmask
, cs
->mems_allowed
, node_online_map
);
603 *pmask
= node_online_map
;
604 BUG_ON(!nodes_intersects(*pmask
, node_online_map
));
608 * cpuset_update_task_memory_state - update task memory placement
610 * If the current tasks cpusets mems_allowed changed behind our
611 * backs, update current->mems_allowed, mems_generation and task NUMA
612 * mempolicy to the new value.
614 * Task mempolicy is updated by rebinding it relative to the
615 * current->cpuset if a task has its memory placement changed.
616 * Do not call this routine if in_interrupt().
618 * Call without callback_mutex or task_lock() held. May be
619 * called with or without manage_mutex held. Thanks in part to
620 * 'the_top_cpuset_hack', the tasks cpuset pointer will never
621 * be NULL. This routine also might acquire callback_mutex and
622 * current->mm->mmap_sem during call.
624 * Reading current->cpuset->mems_generation doesn't need task_lock
625 * to guard the current->cpuset derefence, because it is guarded
626 * from concurrent freeing of current->cpuset by attach_task(),
629 * The rcu_dereference() is technically probably not needed,
630 * as I don't actually mind if I see a new cpuset pointer but
631 * an old value of mems_generation. However this really only
632 * matters on alpha systems using cpusets heavily. If I dropped
633 * that rcu_dereference(), it would save them a memory barrier.
634 * For all other arch's, rcu_dereference is a no-op anyway, and for
635 * alpha systems not using cpusets, another planned optimization,
636 * avoiding the rcu critical section for tasks in the root cpuset
637 * which is statically allocated, so can't vanish, will make this
638 * irrelevant. Better to use RCU as intended, than to engage in
639 * some cute trick to save a memory barrier that is impossible to
640 * test, for alpha systems using cpusets heavily, which might not
643 * This routine is needed to update the per-task mems_allowed data,
644 * within the tasks context, when it is trying to allocate memory
645 * (in various mm/mempolicy.c routines) and notices that some other
646 * task has been modifying its cpuset.
649 void cpuset_update_task_memory_state(void)
651 int my_cpusets_mem_gen
;
652 struct task_struct
*tsk
= current
;
655 if (tsk
->cpuset
== &top_cpuset
) {
656 /* Don't need rcu for top_cpuset. It's never freed. */
657 my_cpusets_mem_gen
= top_cpuset
.mems_generation
;
660 cs
= rcu_dereference(tsk
->cpuset
);
661 my_cpusets_mem_gen
= cs
->mems_generation
;
665 if (my_cpusets_mem_gen
!= tsk
->cpuset_mems_generation
) {
666 mutex_lock(&callback_mutex
);
668 cs
= tsk
->cpuset
; /* Maybe changed when task not locked */
669 guarantee_online_mems(cs
, &tsk
->mems_allowed
);
670 tsk
->cpuset_mems_generation
= cs
->mems_generation
;
671 if (is_spread_page(cs
))
672 tsk
->flags
|= PF_SPREAD_PAGE
;
674 tsk
->flags
&= ~PF_SPREAD_PAGE
;
675 if (is_spread_slab(cs
))
676 tsk
->flags
|= PF_SPREAD_SLAB
;
678 tsk
->flags
&= ~PF_SPREAD_SLAB
;
680 mutex_unlock(&callback_mutex
);
681 mpol_rebind_task(tsk
, &tsk
->mems_allowed
);
686 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
688 * One cpuset is a subset of another if all its allowed CPUs and
689 * Memory Nodes are a subset of the other, and its exclusive flags
690 * are only set if the other's are set. Call holding manage_mutex.
693 static int is_cpuset_subset(const struct cpuset
*p
, const struct cpuset
*q
)
695 return cpus_subset(p
->cpus_allowed
, q
->cpus_allowed
) &&
696 nodes_subset(p
->mems_allowed
, q
->mems_allowed
) &&
697 is_cpu_exclusive(p
) <= is_cpu_exclusive(q
) &&
698 is_mem_exclusive(p
) <= is_mem_exclusive(q
);
702 * validate_change() - Used to validate that any proposed cpuset change
703 * follows the structural rules for cpusets.
705 * If we replaced the flag and mask values of the current cpuset
706 * (cur) with those values in the trial cpuset (trial), would
707 * our various subset and exclusive rules still be valid? Presumes
710 * 'cur' is the address of an actual, in-use cpuset. Operations
711 * such as list traversal that depend on the actual address of the
712 * cpuset in the list must use cur below, not trial.
714 * 'trial' is the address of bulk structure copy of cur, with
715 * perhaps one or more of the fields cpus_allowed, mems_allowed,
716 * or flags changed to new, trial values.
718 * Return 0 if valid, -errno if not.
721 static int validate_change(const struct cpuset
*cur
, const struct cpuset
*trial
)
723 struct cpuset
*c
, *par
;
725 /* Each of our child cpusets must be a subset of us */
726 list_for_each_entry(c
, &cur
->children
, sibling
) {
727 if (!is_cpuset_subset(c
, trial
))
731 /* Remaining checks don't apply to root cpuset */
732 if (cur
== &top_cpuset
)
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.
766 * Must not be called holding callback_mutex, because we must
767 * not call lock_cpu_hotplug() while holding callback_mutex.
770 static void update_cpu_domains(struct cpuset
*cur
)
772 struct cpuset
*c
, *par
= cur
->parent
;
773 cpumask_t pspan
, cspan
;
775 if (par
== NULL
|| cpus_empty(cur
->cpus_allowed
))
779 * Get all cpus from parent's cpus_allowed not part of exclusive
782 pspan
= par
->cpus_allowed
;
783 list_for_each_entry(c
, &par
->children
, sibling
) {
784 if (is_cpu_exclusive(c
))
785 cpus_andnot(pspan
, pspan
, c
->cpus_allowed
);
787 if (!is_cpu_exclusive(cur
)) {
788 cpus_or(pspan
, pspan
, cur
->cpus_allowed
);
789 if (cpus_equal(pspan
, cur
->cpus_allowed
))
791 cspan
= CPU_MASK_NONE
;
793 if (cpus_empty(pspan
))
795 cspan
= cur
->cpus_allowed
;
797 * Get all cpus from current cpuset's cpus_allowed not part
798 * of exclusive children
800 list_for_each_entry(c
, &cur
->children
, sibling
) {
801 if (is_cpu_exclusive(c
))
802 cpus_andnot(cspan
, cspan
, c
->cpus_allowed
);
807 partition_sched_domains(&pspan
, &cspan
);
808 unlock_cpu_hotplug();
812 * Call with manage_mutex held. May take callback_mutex during call.
815 static int update_cpumask(struct cpuset
*cs
, char *buf
)
817 struct cpuset trialcs
;
818 int retval
, cpus_unchanged
;
820 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
821 if (cs
== &top_cpuset
)
825 retval
= cpulist_parse(buf
, trialcs
.cpus_allowed
);
828 cpus_and(trialcs
.cpus_allowed
, trialcs
.cpus_allowed
, cpu_online_map
);
829 if (cpus_empty(trialcs
.cpus_allowed
))
831 retval
= validate_change(cs
, &trialcs
);
834 cpus_unchanged
= cpus_equal(cs
->cpus_allowed
, trialcs
.cpus_allowed
);
835 mutex_lock(&callback_mutex
);
836 cs
->cpus_allowed
= trialcs
.cpus_allowed
;
837 mutex_unlock(&callback_mutex
);
838 if (is_cpu_exclusive(cs
) && !cpus_unchanged
)
839 update_cpu_domains(cs
);
846 * Migrate memory region from one set of nodes to another.
848 * Temporarilly set tasks mems_allowed to target nodes of migration,
849 * so that the migration code can allocate pages on these nodes.
851 * Call holding manage_mutex, so our current->cpuset won't change
852 * during this call, as manage_mutex holds off any attach_task()
853 * calls. Therefore we don't need to take task_lock around the
854 * call to guarantee_online_mems(), as we know no one is changing
857 * Hold callback_mutex around the two modifications of our tasks
858 * mems_allowed to synchronize with cpuset_mems_allowed().
860 * While the mm_struct we are migrating is typically from some
861 * other task, the task_struct mems_allowed that we are hacking
862 * is for our current task, which must allocate new pages for that
863 * migrating memory region.
865 * We call cpuset_update_task_memory_state() before hacking
866 * our tasks mems_allowed, so that we are assured of being in
867 * sync with our tasks cpuset, and in particular, callbacks to
868 * cpuset_update_task_memory_state() from nested page allocations
869 * won't see any mismatch of our cpuset and task mems_generation
870 * values, so won't overwrite our hacked tasks mems_allowed
874 static void cpuset_migrate_mm(struct mm_struct
*mm
, const nodemask_t
*from
,
875 const nodemask_t
*to
)
877 struct task_struct
*tsk
= current
;
879 cpuset_update_task_memory_state();
881 mutex_lock(&callback_mutex
);
882 tsk
->mems_allowed
= *to
;
883 mutex_unlock(&callback_mutex
);
885 do_migrate_pages(mm
, from
, to
, MPOL_MF_MOVE_ALL
);
887 mutex_lock(&callback_mutex
);
888 guarantee_online_mems(tsk
->cpuset
, &tsk
->mems_allowed
);
889 mutex_unlock(&callback_mutex
);
893 * Handle user request to change the 'mems' memory placement
894 * of a cpuset. Needs to validate the request, update the
895 * cpusets mems_allowed and mems_generation, and for each
896 * task in the cpuset, rebind any vma mempolicies and if
897 * the cpuset is marked 'memory_migrate', migrate the tasks
898 * pages to the new memory.
900 * Call with manage_mutex held. May take callback_mutex during call.
901 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
902 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
903 * their mempolicies to the cpusets new mems_allowed.
906 static int update_nodemask(struct cpuset
*cs
, char *buf
)
908 struct cpuset trialcs
;
910 struct task_struct
*g
, *p
;
911 struct mm_struct
**mmarray
;
917 /* top_cpuset.mems_allowed tracks node_online_map; it's read-only */
918 if (cs
== &top_cpuset
)
922 retval
= nodelist_parse(buf
, trialcs
.mems_allowed
);
925 nodes_and(trialcs
.mems_allowed
, trialcs
.mems_allowed
, node_online_map
);
926 oldmem
= cs
->mems_allowed
;
927 if (nodes_equal(oldmem
, trialcs
.mems_allowed
)) {
928 retval
= 0; /* Too easy - nothing to do */
931 if (nodes_empty(trialcs
.mems_allowed
)) {
935 retval
= validate_change(cs
, &trialcs
);
939 mutex_lock(&callback_mutex
);
940 cs
->mems_allowed
= trialcs
.mems_allowed
;
941 cs
->mems_generation
= cpuset_mems_generation
++;
942 mutex_unlock(&callback_mutex
);
944 set_cpuset_being_rebound(cs
); /* causes mpol_copy() rebind */
946 fudge
= 10; /* spare mmarray[] slots */
947 fudge
+= cpus_weight(cs
->cpus_allowed
); /* imagine one fork-bomb/cpu */
951 * Allocate mmarray[] to hold mm reference for each task
952 * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
953 * tasklist_lock. We could use GFP_ATOMIC, but with a
954 * few more lines of code, we can retry until we get a big
955 * enough mmarray[] w/o using GFP_ATOMIC.
958 ntasks
= atomic_read(&cs
->count
); /* guess */
960 mmarray
= kmalloc(ntasks
* sizeof(*mmarray
), GFP_KERNEL
);
963 write_lock_irq(&tasklist_lock
); /* block fork */
964 if (atomic_read(&cs
->count
) <= ntasks
)
965 break; /* got enough */
966 write_unlock_irq(&tasklist_lock
); /* try again */
972 /* Load up mmarray[] with mm reference for each task in cpuset. */
973 do_each_thread(g
, p
) {
974 struct mm_struct
*mm
;
978 "Cpuset mempolicy rebind incomplete.\n");
987 } while_each_thread(g
, p
);
988 write_unlock_irq(&tasklist_lock
);
991 * Now that we've dropped the tasklist spinlock, we can
992 * rebind the vma mempolicies of each mm in mmarray[] to their
993 * new cpuset, and release that mm. The mpol_rebind_mm()
994 * call takes mmap_sem, which we couldn't take while holding
995 * tasklist_lock. Forks can happen again now - the mpol_copy()
996 * cpuset_being_rebound check will catch such forks, and rebind
997 * their vma mempolicies too. Because we still hold the global
998 * cpuset manage_mutex, we know that no other rebind effort will
999 * be contending for the global variable cpuset_being_rebound.
1000 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
1001 * is idempotent. Also migrate pages in each mm to new nodes.
1003 migrate
= is_memory_migrate(cs
);
1004 for (i
= 0; i
< n
; i
++) {
1005 struct mm_struct
*mm
= mmarray
[i
];
1007 mpol_rebind_mm(mm
, &cs
->mems_allowed
);
1009 cpuset_migrate_mm(mm
, &oldmem
, &cs
->mems_allowed
);
1013 /* We're done rebinding vma's to this cpusets new mems_allowed. */
1015 set_cpuset_being_rebound(NULL
);
1022 * Call with manage_mutex held.
1025 static int update_memory_pressure_enabled(struct cpuset
*cs
, char *buf
)
1027 if (simple_strtoul(buf
, NULL
, 10) != 0)
1028 cpuset_memory_pressure_enabled
= 1;
1030 cpuset_memory_pressure_enabled
= 0;
1035 * update_flag - read a 0 or a 1 in a file and update associated flag
1036 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
1037 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
1038 * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
1039 * cs: the cpuset to update
1040 * buf: the buffer where we read the 0 or 1
1042 * Call with manage_mutex held.
1045 static int update_flag(cpuset_flagbits_t bit
, struct cpuset
*cs
, char *buf
)
1048 struct cpuset trialcs
;
1049 int err
, cpu_exclusive_changed
;
1051 turning_on
= (simple_strtoul(buf
, NULL
, 10) != 0);
1055 set_bit(bit
, &trialcs
.flags
);
1057 clear_bit(bit
, &trialcs
.flags
);
1059 err
= validate_change(cs
, &trialcs
);
1062 cpu_exclusive_changed
=
1063 (is_cpu_exclusive(cs
) != is_cpu_exclusive(&trialcs
));
1064 mutex_lock(&callback_mutex
);
1065 cs
->flags
= trialcs
.flags
;
1066 mutex_unlock(&callback_mutex
);
1068 if (cpu_exclusive_changed
)
1069 update_cpu_domains(cs
);
1074 * Frequency meter - How fast is some event occurring?
1076 * These routines manage a digitally filtered, constant time based,
1077 * event frequency meter. There are four routines:
1078 * fmeter_init() - initialize a frequency meter.
1079 * fmeter_markevent() - called each time the event happens.
1080 * fmeter_getrate() - returns the recent rate of such events.
1081 * fmeter_update() - internal routine used to update fmeter.
1083 * A common data structure is passed to each of these routines,
1084 * which is used to keep track of the state required to manage the
1085 * frequency meter and its digital filter.
1087 * The filter works on the number of events marked per unit time.
1088 * The filter is single-pole low-pass recursive (IIR). The time unit
1089 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1090 * simulate 3 decimal digits of precision (multiplied by 1000).
1092 * With an FM_COEF of 933, and a time base of 1 second, the filter
1093 * has a half-life of 10 seconds, meaning that if the events quit
1094 * happening, then the rate returned from the fmeter_getrate()
1095 * will be cut in half each 10 seconds, until it converges to zero.
1097 * It is not worth doing a real infinitely recursive filter. If more
1098 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1099 * just compute FM_MAXTICKS ticks worth, by which point the level
1102 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1103 * arithmetic overflow in the fmeter_update() routine.
1105 * Given the simple 32 bit integer arithmetic used, this meter works
1106 * best for reporting rates between one per millisecond (msec) and
1107 * one per 32 (approx) seconds. At constant rates faster than one
1108 * per msec it maxes out at values just under 1,000,000. At constant
1109 * rates between one per msec, and one per second it will stabilize
1110 * to a value N*1000, where N is the rate of events per second.
1111 * At constant rates between one per second and one per 32 seconds,
1112 * it will be choppy, moving up on the seconds that have an event,
1113 * and then decaying until the next event. At rates slower than
1114 * about one in 32 seconds, it decays all the way back to zero between
1118 #define FM_COEF 933 /* coefficient for half-life of 10 secs */
1119 #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1120 #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1121 #define FM_SCALE 1000 /* faux fixed point scale */
1123 /* Initialize a frequency meter */
1124 static void fmeter_init(struct fmeter
*fmp
)
1129 spin_lock_init(&fmp
->lock
);
1132 /* Internal meter update - process cnt events and update value */
1133 static void fmeter_update(struct fmeter
*fmp
)
1135 time_t now
= get_seconds();
1136 time_t ticks
= now
- fmp
->time
;
1141 ticks
= min(FM_MAXTICKS
, ticks
);
1143 fmp
->val
= (FM_COEF
* fmp
->val
) / FM_SCALE
;
1146 fmp
->val
+= ((FM_SCALE
- FM_COEF
) * fmp
->cnt
) / FM_SCALE
;
1150 /* Process any previous ticks, then bump cnt by one (times scale). */
1151 static void fmeter_markevent(struct fmeter
*fmp
)
1153 spin_lock(&fmp
->lock
);
1155 fmp
->cnt
= min(FM_MAXCNT
, fmp
->cnt
+ FM_SCALE
);
1156 spin_unlock(&fmp
->lock
);
1159 /* Process any previous ticks, then return current value. */
1160 static int fmeter_getrate(struct fmeter
*fmp
)
1164 spin_lock(&fmp
->lock
);
1167 spin_unlock(&fmp
->lock
);
1172 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
1173 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1174 * notified on release.
1176 * Call holding manage_mutex. May take callback_mutex and task_lock of
1177 * the task 'pid' during call.
1180 static int attach_task(struct cpuset
*cs
, char *pidbuf
, char **ppathbuf
)
1183 struct task_struct
*tsk
;
1184 struct cpuset
*oldcs
;
1186 nodemask_t from
, to
;
1187 struct mm_struct
*mm
;
1190 if (sscanf(pidbuf
, "%d", &pid
) != 1)
1192 if (cpus_empty(cs
->cpus_allowed
) || nodes_empty(cs
->mems_allowed
))
1196 read_lock(&tasklist_lock
);
1198 tsk
= find_task_by_pid(pid
);
1199 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1200 read_unlock(&tasklist_lock
);
1204 get_task_struct(tsk
);
1205 read_unlock(&tasklist_lock
);
1207 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
1208 && (current
->euid
!= tsk
->suid
)) {
1209 put_task_struct(tsk
);
1214 get_task_struct(tsk
);
1217 retval
= security_task_setscheduler(tsk
, 0, NULL
);
1219 put_task_struct(tsk
);
1223 mutex_lock(&callback_mutex
);
1226 oldcs
= tsk
->cpuset
;
1228 * After getting 'oldcs' cpuset ptr, be sure still not exiting.
1229 * If 'oldcs' might be the top_cpuset due to the_top_cpuset_hack
1230 * then fail this attach_task(), to avoid breaking top_cpuset.count.
1232 if (tsk
->flags
& PF_EXITING
) {
1234 mutex_unlock(&callback_mutex
);
1235 put_task_struct(tsk
);
1238 atomic_inc(&cs
->count
);
1239 rcu_assign_pointer(tsk
->cpuset
, cs
);
1242 guarantee_online_cpus(cs
, &cpus
);
1243 set_cpus_allowed(tsk
, cpus
);
1245 from
= oldcs
->mems_allowed
;
1246 to
= cs
->mems_allowed
;
1248 mutex_unlock(&callback_mutex
);
1250 mm
= get_task_mm(tsk
);
1252 mpol_rebind_mm(mm
, &to
);
1253 if (is_memory_migrate(cs
))
1254 cpuset_migrate_mm(mm
, &from
, &to
);
1258 put_task_struct(tsk
);
1260 if (atomic_dec_and_test(&oldcs
->count
))
1261 check_for_release(oldcs
, ppathbuf
);
1265 /* The various types of files and directories in a cpuset file system */
1270 FILE_MEMORY_MIGRATE
,
1275 FILE_NOTIFY_ON_RELEASE
,
1276 FILE_MEMORY_PRESSURE_ENABLED
,
1277 FILE_MEMORY_PRESSURE
,
1281 } cpuset_filetype_t
;
1283 static ssize_t
cpuset_common_file_write(struct file
*file
,
1284 const char __user
*userbuf
,
1285 size_t nbytes
, loff_t
*unused_ppos
)
1287 struct cpuset
*cs
= __d_cs(file
->f_path
.dentry
->d_parent
);
1288 struct cftype
*cft
= __d_cft(file
->f_path
.dentry
);
1289 cpuset_filetype_t type
= cft
->private;
1291 char *pathbuf
= NULL
;
1294 /* Crude upper limit on largest legitimate cpulist user might write. */
1295 if (nbytes
> 100 + 6 * max(NR_CPUS
, MAX_NUMNODES
))
1298 /* +1 for nul-terminator */
1299 if ((buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
)) == 0)
1302 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
1306 buffer
[nbytes
] = 0; /* nul-terminate */
1308 mutex_lock(&manage_mutex
);
1310 if (is_removed(cs
)) {
1317 retval
= update_cpumask(cs
, buffer
);
1320 retval
= update_nodemask(cs
, buffer
);
1322 case FILE_CPU_EXCLUSIVE
:
1323 retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, buffer
);
1325 case FILE_MEM_EXCLUSIVE
:
1326 retval
= update_flag(CS_MEM_EXCLUSIVE
, cs
, buffer
);
1328 case FILE_NOTIFY_ON_RELEASE
:
1329 retval
= update_flag(CS_NOTIFY_ON_RELEASE
, cs
, buffer
);
1331 case FILE_MEMORY_MIGRATE
:
1332 retval
= update_flag(CS_MEMORY_MIGRATE
, cs
, buffer
);
1334 case FILE_MEMORY_PRESSURE_ENABLED
:
1335 retval
= update_memory_pressure_enabled(cs
, buffer
);
1337 case FILE_MEMORY_PRESSURE
:
1340 case FILE_SPREAD_PAGE
:
1341 retval
= update_flag(CS_SPREAD_PAGE
, cs
, buffer
);
1342 cs
->mems_generation
= cpuset_mems_generation
++;
1344 case FILE_SPREAD_SLAB
:
1345 retval
= update_flag(CS_SPREAD_SLAB
, cs
, buffer
);
1346 cs
->mems_generation
= cpuset_mems_generation
++;
1349 retval
= attach_task(cs
, buffer
, &pathbuf
);
1359 mutex_unlock(&manage_mutex
);
1360 cpuset_release_agent(pathbuf
);
1366 static ssize_t
cpuset_file_write(struct file
*file
, const char __user
*buf
,
1367 size_t nbytes
, loff_t
*ppos
)
1370 struct cftype
*cft
= __d_cft(file
->f_path
.dentry
);
1374 /* special function ? */
1376 retval
= cft
->write(file
, buf
, nbytes
, ppos
);
1378 retval
= cpuset_common_file_write(file
, buf
, nbytes
, ppos
);
1384 * These ascii lists should be read in a single call, by using a user
1385 * buffer large enough to hold the entire map. If read in smaller
1386 * chunks, there is no guarantee of atomicity. Since the display format
1387 * used, list of ranges of sequential numbers, is variable length,
1388 * and since these maps can change value dynamically, one could read
1389 * gibberish by doing partial reads while a list was changing.
1390 * A single large read to a buffer that crosses a page boundary is
1391 * ok, because the result being copied to user land is not recomputed
1392 * across a page fault.
1395 static int cpuset_sprintf_cpulist(char *page
, struct cpuset
*cs
)
1399 mutex_lock(&callback_mutex
);
1400 mask
= cs
->cpus_allowed
;
1401 mutex_unlock(&callback_mutex
);
1403 return cpulist_scnprintf(page
, PAGE_SIZE
, mask
);
1406 static int cpuset_sprintf_memlist(char *page
, struct cpuset
*cs
)
1410 mutex_lock(&callback_mutex
);
1411 mask
= cs
->mems_allowed
;
1412 mutex_unlock(&callback_mutex
);
1414 return nodelist_scnprintf(page
, PAGE_SIZE
, mask
);
1417 static ssize_t
cpuset_common_file_read(struct file
*file
, char __user
*buf
,
1418 size_t nbytes
, loff_t
*ppos
)
1420 struct cftype
*cft
= __d_cft(file
->f_path
.dentry
);
1421 struct cpuset
*cs
= __d_cs(file
->f_path
.dentry
->d_parent
);
1422 cpuset_filetype_t type
= cft
->private;
1427 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
1434 s
+= cpuset_sprintf_cpulist(s
, cs
);
1437 s
+= cpuset_sprintf_memlist(s
, cs
);
1439 case FILE_CPU_EXCLUSIVE
:
1440 *s
++ = is_cpu_exclusive(cs
) ? '1' : '0';
1442 case FILE_MEM_EXCLUSIVE
:
1443 *s
++ = is_mem_exclusive(cs
) ? '1' : '0';
1445 case FILE_NOTIFY_ON_RELEASE
:
1446 *s
++ = notify_on_release(cs
) ? '1' : '0';
1448 case FILE_MEMORY_MIGRATE
:
1449 *s
++ = is_memory_migrate(cs
) ? '1' : '0';
1451 case FILE_MEMORY_PRESSURE_ENABLED
:
1452 *s
++ = cpuset_memory_pressure_enabled
? '1' : '0';
1454 case FILE_MEMORY_PRESSURE
:
1455 s
+= sprintf(s
, "%d", fmeter_getrate(&cs
->fmeter
));
1457 case FILE_SPREAD_PAGE
:
1458 *s
++ = is_spread_page(cs
) ? '1' : '0';
1460 case FILE_SPREAD_SLAB
:
1461 *s
++ = is_spread_slab(cs
) ? '1' : '0';
1469 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1471 free_page((unsigned long)page
);
1475 static ssize_t
cpuset_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1479 struct cftype
*cft
= __d_cft(file
->f_path
.dentry
);
1483 /* special function ? */
1485 retval
= cft
->read(file
, buf
, nbytes
, ppos
);
1487 retval
= cpuset_common_file_read(file
, buf
, nbytes
, ppos
);
1492 static int cpuset_file_open(struct inode
*inode
, struct file
*file
)
1497 err
= generic_file_open(inode
, file
);
1501 cft
= __d_cft(file
->f_path
.dentry
);
1505 err
= cft
->open(inode
, file
);
1512 static int cpuset_file_release(struct inode
*inode
, struct file
*file
)
1514 struct cftype
*cft
= __d_cft(file
->f_path
.dentry
);
1516 return cft
->release(inode
, file
);
1521 * cpuset_rename - Only allow simple rename of directories in place.
1523 static int cpuset_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1524 struct inode
*new_dir
, struct dentry
*new_dentry
)
1526 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1528 if (new_dentry
->d_inode
)
1530 if (old_dir
!= new_dir
)
1532 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1535 static const struct file_operations cpuset_file_operations
= {
1536 .read
= cpuset_file_read
,
1537 .write
= cpuset_file_write
,
1538 .llseek
= generic_file_llseek
,
1539 .open
= cpuset_file_open
,
1540 .release
= cpuset_file_release
,
1543 static const struct inode_operations cpuset_dir_inode_operations
= {
1544 .lookup
= simple_lookup
,
1545 .mkdir
= cpuset_mkdir
,
1546 .rmdir
= cpuset_rmdir
,
1547 .rename
= cpuset_rename
,
1550 static int cpuset_create_file(struct dentry
*dentry
, int mode
)
1552 struct inode
*inode
;
1556 if (dentry
->d_inode
)
1559 inode
= cpuset_new_inode(mode
);
1563 if (S_ISDIR(mode
)) {
1564 inode
->i_op
= &cpuset_dir_inode_operations
;
1565 inode
->i_fop
= &simple_dir_operations
;
1567 /* start off with i_nlink == 2 (for "." entry) */
1569 } else if (S_ISREG(mode
)) {
1571 inode
->i_fop
= &cpuset_file_operations
;
1574 d_instantiate(dentry
, inode
);
1575 dget(dentry
); /* Extra count - pin the dentry in core */
1580 * cpuset_create_dir - create a directory for an object.
1581 * cs: the cpuset we create the directory for.
1582 * It must have a valid ->parent field
1583 * And we are going to fill its ->dentry field.
1584 * name: The name to give to the cpuset directory. Will be copied.
1585 * mode: mode to set on new directory.
1588 static int cpuset_create_dir(struct cpuset
*cs
, const char *name
, int mode
)
1590 struct dentry
*dentry
= NULL
;
1591 struct dentry
*parent
;
1594 parent
= cs
->parent
->dentry
;
1595 dentry
= cpuset_get_dentry(parent
, name
);
1597 return PTR_ERR(dentry
);
1598 error
= cpuset_create_file(dentry
, S_IFDIR
| mode
);
1600 dentry
->d_fsdata
= cs
;
1601 inc_nlink(parent
->d_inode
);
1602 cs
->dentry
= dentry
;
1609 static int cpuset_add_file(struct dentry
*dir
, const struct cftype
*cft
)
1611 struct dentry
*dentry
;
1614 mutex_lock(&dir
->d_inode
->i_mutex
);
1615 dentry
= cpuset_get_dentry(dir
, cft
->name
);
1616 if (!IS_ERR(dentry
)) {
1617 error
= cpuset_create_file(dentry
, 0644 | S_IFREG
);
1619 dentry
->d_fsdata
= (void *)cft
;
1622 error
= PTR_ERR(dentry
);
1623 mutex_unlock(&dir
->d_inode
->i_mutex
);
1628 * Stuff for reading the 'tasks' file.
1630 * Reading this file can return large amounts of data if a cpuset has
1631 * *lots* of attached tasks. So it may need several calls to read(),
1632 * but we cannot guarantee that the information we produce is correct
1633 * unless we produce it entirely atomically.
1635 * Upon tasks file open(), a struct ctr_struct is allocated, that
1636 * will have a pointer to an array (also allocated here). The struct
1637 * ctr_struct * is stored in file->private_data. Its resources will
1638 * be freed by release() when the file is closed. The array is used
1639 * to sprintf the PIDs and then used by read().
1642 /* cpusets_tasks_read array */
1650 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1651 * Return actual number of pids loaded. No need to task_lock(p)
1652 * when reading out p->cpuset, as we don't really care if it changes
1653 * on the next cycle, and we are not going to try to dereference it.
1655 static int pid_array_load(pid_t
*pidarray
, int npids
, struct cpuset
*cs
)
1658 struct task_struct
*g
, *p
;
1660 read_lock(&tasklist_lock
);
1662 do_each_thread(g
, p
) {
1663 if (p
->cpuset
== cs
) {
1664 pidarray
[n
++] = p
->pid
;
1665 if (unlikely(n
== npids
))
1668 } while_each_thread(g
, p
);
1671 read_unlock(&tasklist_lock
);
1675 static int cmppid(const void *a
, const void *b
)
1677 return *(pid_t
*)a
- *(pid_t
*)b
;
1681 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1682 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1683 * count 'cnt' of how many chars would be written if buf were large enough.
1685 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1690 for (i
= 0; i
< npids
; i
++)
1691 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1696 * Handle an open on 'tasks' file. Prepare a buffer listing the
1697 * process id's of tasks currently attached to the cpuset being opened.
1699 * Does not require any specific cpuset mutexes, and does not take any.
1701 static int cpuset_tasks_open(struct inode
*unused
, struct file
*file
)
1703 struct cpuset
*cs
= __d_cs(file
->f_path
.dentry
->d_parent
);
1704 struct ctr_struct
*ctr
;
1709 if (!(file
->f_mode
& FMODE_READ
))
1712 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1717 * If cpuset gets more users after we read count, we won't have
1718 * enough space - tough. This race is indistinguishable to the
1719 * caller from the case that the additional cpuset users didn't
1720 * show up until sometime later on.
1722 npids
= atomic_read(&cs
->count
);
1723 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1727 npids
= pid_array_load(pidarray
, npids
, cs
);
1728 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1730 /* Call pid_array_to_buf() twice, first just to get bufsz */
1731 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1732 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1735 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1738 file
->private_data
= ctr
;
1749 static ssize_t
cpuset_tasks_read(struct file
*file
, char __user
*buf
,
1750 size_t nbytes
, loff_t
*ppos
)
1752 struct ctr_struct
*ctr
= file
->private_data
;
1754 if (*ppos
+ nbytes
> ctr
->bufsz
)
1755 nbytes
= ctr
->bufsz
- *ppos
;
1756 if (copy_to_user(buf
, ctr
->buf
+ *ppos
, nbytes
))
1762 static int cpuset_tasks_release(struct inode
*unused_inode
, struct file
*file
)
1764 struct ctr_struct
*ctr
;
1766 if (file
->f_mode
& FMODE_READ
) {
1767 ctr
= file
->private_data
;
1775 * for the common functions, 'private' gives the type of file
1778 static struct cftype cft_tasks
= {
1780 .open
= cpuset_tasks_open
,
1781 .read
= cpuset_tasks_read
,
1782 .release
= cpuset_tasks_release
,
1783 .private = FILE_TASKLIST
,
1786 static struct cftype cft_cpus
= {
1788 .private = FILE_CPULIST
,
1791 static struct cftype cft_mems
= {
1793 .private = FILE_MEMLIST
,
1796 static struct cftype cft_cpu_exclusive
= {
1797 .name
= "cpu_exclusive",
1798 .private = FILE_CPU_EXCLUSIVE
,
1801 static struct cftype cft_mem_exclusive
= {
1802 .name
= "mem_exclusive",
1803 .private = FILE_MEM_EXCLUSIVE
,
1806 static struct cftype cft_notify_on_release
= {
1807 .name
= "notify_on_release",
1808 .private = FILE_NOTIFY_ON_RELEASE
,
1811 static struct cftype cft_memory_migrate
= {
1812 .name
= "memory_migrate",
1813 .private = FILE_MEMORY_MIGRATE
,
1816 static struct cftype cft_memory_pressure_enabled
= {
1817 .name
= "memory_pressure_enabled",
1818 .private = FILE_MEMORY_PRESSURE_ENABLED
,
1821 static struct cftype cft_memory_pressure
= {
1822 .name
= "memory_pressure",
1823 .private = FILE_MEMORY_PRESSURE
,
1826 static struct cftype cft_spread_page
= {
1827 .name
= "memory_spread_page",
1828 .private = FILE_SPREAD_PAGE
,
1831 static struct cftype cft_spread_slab
= {
1832 .name
= "memory_spread_slab",
1833 .private = FILE_SPREAD_SLAB
,
1836 static int cpuset_populate_dir(struct dentry
*cs_dentry
)
1840 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpus
)) < 0)
1842 if ((err
= cpuset_add_file(cs_dentry
, &cft_mems
)) < 0)
1844 if ((err
= cpuset_add_file(cs_dentry
, &cft_cpu_exclusive
)) < 0)
1846 if ((err
= cpuset_add_file(cs_dentry
, &cft_mem_exclusive
)) < 0)
1848 if ((err
= cpuset_add_file(cs_dentry
, &cft_notify_on_release
)) < 0)
1850 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_migrate
)) < 0)
1852 if ((err
= cpuset_add_file(cs_dentry
, &cft_memory_pressure
)) < 0)
1854 if ((err
= cpuset_add_file(cs_dentry
, &cft_spread_page
)) < 0)
1856 if ((err
= cpuset_add_file(cs_dentry
, &cft_spread_slab
)) < 0)
1858 if ((err
= cpuset_add_file(cs_dentry
, &cft_tasks
)) < 0)
1864 * cpuset_create - create a cpuset
1865 * parent: cpuset that will be parent of the new cpuset.
1866 * name: name of the new cpuset. Will be strcpy'ed.
1867 * mode: mode to set on new inode
1869 * Must be called with the mutex on the parent inode held
1872 static long cpuset_create(struct cpuset
*parent
, const char *name
, int mode
)
1877 cs
= kmalloc(sizeof(*cs
), GFP_KERNEL
);
1881 mutex_lock(&manage_mutex
);
1882 cpuset_update_task_memory_state();
1884 if (notify_on_release(parent
))
1885 set_bit(CS_NOTIFY_ON_RELEASE
, &cs
->flags
);
1886 if (is_spread_page(parent
))
1887 set_bit(CS_SPREAD_PAGE
, &cs
->flags
);
1888 if (is_spread_slab(parent
))
1889 set_bit(CS_SPREAD_SLAB
, &cs
->flags
);
1890 cs
->cpus_allowed
= CPU_MASK_NONE
;
1891 cs
->mems_allowed
= NODE_MASK_NONE
;
1892 atomic_set(&cs
->count
, 0);
1893 INIT_LIST_HEAD(&cs
->sibling
);
1894 INIT_LIST_HEAD(&cs
->children
);
1895 cs
->mems_generation
= cpuset_mems_generation
++;
1896 fmeter_init(&cs
->fmeter
);
1898 cs
->parent
= parent
;
1900 mutex_lock(&callback_mutex
);
1901 list_add(&cs
->sibling
, &cs
->parent
->children
);
1902 number_of_cpusets
++;
1903 mutex_unlock(&callback_mutex
);
1905 err
= cpuset_create_dir(cs
, name
, mode
);
1910 * Release manage_mutex before cpuset_populate_dir() because it
1911 * will down() this new directory's i_mutex and if we race with
1912 * another mkdir, we might deadlock.
1914 mutex_unlock(&manage_mutex
);
1916 err
= cpuset_populate_dir(cs
->dentry
);
1917 /* If err < 0, we have a half-filled directory - oh well ;) */
1920 list_del(&cs
->sibling
);
1921 mutex_unlock(&manage_mutex
);
1926 static int cpuset_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1928 struct cpuset
*c_parent
= dentry
->d_parent
->d_fsdata
;
1930 /* the vfs holds inode->i_mutex already */
1931 return cpuset_create(c_parent
, dentry
->d_name
.name
, mode
| S_IFDIR
);
1935 * Locking note on the strange update_flag() call below:
1937 * If the cpuset being removed is marked cpu_exclusive, then simulate
1938 * turning cpu_exclusive off, which will call update_cpu_domains().
1939 * The lock_cpu_hotplug() call in update_cpu_domains() must not be
1940 * made while holding callback_mutex. Elsewhere the kernel nests
1941 * callback_mutex inside lock_cpu_hotplug() calls. So the reverse
1942 * nesting would risk an ABBA deadlock.
1945 static int cpuset_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
1947 struct cpuset
*cs
= dentry
->d_fsdata
;
1949 struct cpuset
*parent
;
1950 char *pathbuf
= NULL
;
1952 /* the vfs holds both inode->i_mutex already */
1954 mutex_lock(&manage_mutex
);
1955 cpuset_update_task_memory_state();
1956 if (atomic_read(&cs
->count
) > 0) {
1957 mutex_unlock(&manage_mutex
);
1960 if (!list_empty(&cs
->children
)) {
1961 mutex_unlock(&manage_mutex
);
1964 if (is_cpu_exclusive(cs
)) {
1965 int retval
= update_flag(CS_CPU_EXCLUSIVE
, cs
, "0");
1967 mutex_unlock(&manage_mutex
);
1971 parent
= cs
->parent
;
1972 mutex_lock(&callback_mutex
);
1973 set_bit(CS_REMOVED
, &cs
->flags
);
1974 list_del(&cs
->sibling
); /* delete my sibling from parent->children */
1975 spin_lock(&cs
->dentry
->d_lock
);
1976 d
= dget(cs
->dentry
);
1978 spin_unlock(&d
->d_lock
);
1979 cpuset_d_remove_dir(d
);
1981 number_of_cpusets
--;
1982 mutex_unlock(&callback_mutex
);
1983 if (list_empty(&parent
->children
))
1984 check_for_release(parent
, &pathbuf
);
1985 mutex_unlock(&manage_mutex
);
1986 cpuset_release_agent(pathbuf
);
1991 * cpuset_init_early - just enough so that the calls to
1992 * cpuset_update_task_memory_state() in early init code
1996 int __init
cpuset_init_early(void)
1998 struct task_struct
*tsk
= current
;
2000 tsk
->cpuset
= &top_cpuset
;
2001 tsk
->cpuset
->mems_generation
= cpuset_mems_generation
++;
2006 * cpuset_init - initialize cpusets at system boot
2008 * Description: Initialize top_cpuset and the cpuset internal file system,
2011 int __init
cpuset_init(void)
2013 struct dentry
*root
;
2016 top_cpuset
.cpus_allowed
= CPU_MASK_ALL
;
2017 top_cpuset
.mems_allowed
= NODE_MASK_ALL
;
2019 fmeter_init(&top_cpuset
.fmeter
);
2020 top_cpuset
.mems_generation
= cpuset_mems_generation
++;
2022 init_task
.cpuset
= &top_cpuset
;
2024 err
= register_filesystem(&cpuset_fs_type
);
2027 cpuset_mount
= kern_mount(&cpuset_fs_type
);
2028 if (IS_ERR(cpuset_mount
)) {
2029 printk(KERN_ERR
"cpuset: could not mount!\n");
2030 err
= PTR_ERR(cpuset_mount
);
2031 cpuset_mount
= NULL
;
2034 root
= cpuset_mount
->mnt_sb
->s_root
;
2035 root
->d_fsdata
= &top_cpuset
;
2036 inc_nlink(root
->d_inode
);
2037 top_cpuset
.dentry
= root
;
2038 root
->d_inode
->i_op
= &cpuset_dir_inode_operations
;
2039 number_of_cpusets
= 1;
2040 err
= cpuset_populate_dir(root
);
2041 /* memory_pressure_enabled is in root cpuset only */
2043 err
= cpuset_add_file(root
, &cft_memory_pressure_enabled
);
2049 * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs
2050 * or memory nodes, we need to walk over the cpuset hierarchy,
2051 * removing that CPU or node from all cpusets. If this removes the
2052 * last CPU or node from a cpuset, then the guarantee_online_cpus()
2053 * or guarantee_online_mems() code will use that emptied cpusets
2054 * parent online CPUs or nodes. Cpusets that were already empty of
2055 * CPUs or nodes are left empty.
2057 * This routine is intentionally inefficient in a couple of regards.
2058 * It will check all cpusets in a subtree even if the top cpuset of
2059 * the subtree has no offline CPUs or nodes. It checks both CPUs and
2060 * nodes, even though the caller could have been coded to know that
2061 * only one of CPUs or nodes needed to be checked on a given call.
2062 * This was done to minimize text size rather than cpu cycles.
2064 * Call with both manage_mutex and callback_mutex held.
2066 * Recursive, on depth of cpuset subtree.
2069 static void guarantee_online_cpus_mems_in_subtree(const struct cpuset
*cur
)
2073 /* Each of our child cpusets mems must be online */
2074 list_for_each_entry(c
, &cur
->children
, sibling
) {
2075 guarantee_online_cpus_mems_in_subtree(c
);
2076 if (!cpus_empty(c
->cpus_allowed
))
2077 guarantee_online_cpus(c
, &c
->cpus_allowed
);
2078 if (!nodes_empty(c
->mems_allowed
))
2079 guarantee_online_mems(c
, &c
->mems_allowed
);
2084 * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track
2085 * cpu_online_map and node_online_map. Force the top cpuset to track
2086 * whats online after any CPU or memory node hotplug or unplug event.
2088 * To ensure that we don't remove a CPU or node from the top cpuset
2089 * that is currently in use by a child cpuset (which would violate
2090 * the rule that cpusets must be subsets of their parent), we first
2091 * call the recursive routine guarantee_online_cpus_mems_in_subtree().
2093 * Since there are two callers of this routine, one for CPU hotplug
2094 * events and one for memory node hotplug events, we could have coded
2095 * two separate routines here. We code it as a single common routine
2096 * in order to minimize text size.
2099 static void common_cpu_mem_hotplug_unplug(void)
2101 mutex_lock(&manage_mutex
);
2102 mutex_lock(&callback_mutex
);
2104 guarantee_online_cpus_mems_in_subtree(&top_cpuset
);
2105 top_cpuset
.cpus_allowed
= cpu_online_map
;
2106 top_cpuset
.mems_allowed
= node_online_map
;
2108 mutex_unlock(&callback_mutex
);
2109 mutex_unlock(&manage_mutex
);
2113 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2114 * period. This is necessary in order to make cpusets transparent
2115 * (of no affect) on systems that are actively using CPU hotplug
2116 * but making no active use of cpusets.
2118 * This routine ensures that top_cpuset.cpus_allowed tracks
2119 * cpu_online_map on each CPU hotplug (cpuhp) event.
2122 static int cpuset_handle_cpuhp(struct notifier_block
*nb
,
2123 unsigned long phase
, void *cpu
)
2125 common_cpu_mem_hotplug_unplug();
2129 #ifdef CONFIG_MEMORY_HOTPLUG
2131 * Keep top_cpuset.mems_allowed tracking node_online_map.
2132 * Call this routine anytime after you change node_online_map.
2133 * See also the previous routine cpuset_handle_cpuhp().
2136 void cpuset_track_online_nodes(void)
2138 common_cpu_mem_hotplug_unplug();
2143 * cpuset_init_smp - initialize cpus_allowed
2145 * Description: Finish top cpuset after cpu, node maps are initialized
2148 void __init
cpuset_init_smp(void)
2150 top_cpuset
.cpus_allowed
= cpu_online_map
;
2151 top_cpuset
.mems_allowed
= node_online_map
;
2153 hotcpu_notifier(cpuset_handle_cpuhp
, 0);
2157 * cpuset_fork - attach newly forked task to its parents cpuset.
2158 * @tsk: pointer to task_struct of forking parent process.
2160 * Description: A task inherits its parent's cpuset at fork().
2162 * A pointer to the shared cpuset was automatically copied in fork.c
2163 * by dup_task_struct(). However, we ignore that copy, since it was
2164 * not made under the protection of task_lock(), so might no longer be
2165 * a valid cpuset pointer. attach_task() might have already changed
2166 * current->cpuset, allowing the previously referenced cpuset to
2167 * be removed and freed. Instead, we task_lock(current) and copy
2168 * its present value of current->cpuset for our freshly forked child.
2170 * At the point that cpuset_fork() is called, 'current' is the parent
2171 * task, and the passed argument 'child' points to the child task.
2174 void cpuset_fork(struct task_struct
*child
)
2177 child
->cpuset
= current
->cpuset
;
2178 atomic_inc(&child
->cpuset
->count
);
2179 task_unlock(current
);
2183 * cpuset_exit - detach cpuset from exiting task
2184 * @tsk: pointer to task_struct of exiting process
2186 * Description: Detach cpuset from @tsk and release it.
2188 * Note that cpusets marked notify_on_release force every task in
2189 * them to take the global manage_mutex mutex when exiting.
2190 * This could impact scaling on very large systems. Be reluctant to
2191 * use notify_on_release cpusets where very high task exit scaling
2192 * is required on large systems.
2194 * Don't even think about derefencing 'cs' after the cpuset use count
2195 * goes to zero, except inside a critical section guarded by manage_mutex
2196 * or callback_mutex. Otherwise a zero cpuset use count is a license to
2197 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
2199 * This routine has to take manage_mutex, not callback_mutex, because
2200 * it is holding that mutex while calling check_for_release(),
2201 * which calls kmalloc(), so can't be called holding callback_mutex().
2203 * the_top_cpuset_hack:
2205 * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
2207 * Don't leave a task unable to allocate memory, as that is an
2208 * accident waiting to happen should someone add a callout in
2209 * do_exit() after the cpuset_exit() call that might allocate.
2210 * If a task tries to allocate memory with an invalid cpuset,
2211 * it will oops in cpuset_update_task_memory_state().
2213 * We call cpuset_exit() while the task is still competent to
2214 * handle notify_on_release(), then leave the task attached to
2215 * the root cpuset (top_cpuset) for the remainder of its exit.
2217 * To do this properly, we would increment the reference count on
2218 * top_cpuset, and near the very end of the kernel/exit.c do_exit()
2219 * code we would add a second cpuset function call, to drop that
2220 * reference. This would just create an unnecessary hot spot on
2221 * the top_cpuset reference count, to no avail.
2223 * Normally, holding a reference to a cpuset without bumping its
2224 * count is unsafe. The cpuset could go away, or someone could
2225 * attach us to a different cpuset, decrementing the count on
2226 * the first cpuset that we never incremented. But in this case,
2227 * top_cpuset isn't going away, and either task has PF_EXITING set,
2228 * which wards off any attach_task() attempts, or task is a failed
2229 * fork, never visible to attach_task.
2231 * Another way to do this would be to set the cpuset pointer
2232 * to NULL here, and check in cpuset_update_task_memory_state()
2233 * for a NULL pointer. This hack avoids that NULL check, for no
2234 * cost (other than this way too long comment ;).
2237 void cpuset_exit(struct task_struct
*tsk
)
2243 tsk
->cpuset
= &top_cpuset
; /* the_top_cpuset_hack - see above */
2244 task_unlock(current
);
2246 if (notify_on_release(cs
)) {
2247 char *pathbuf
= NULL
;
2249 mutex_lock(&manage_mutex
);
2250 if (atomic_dec_and_test(&cs
->count
))
2251 check_for_release(cs
, &pathbuf
);
2252 mutex_unlock(&manage_mutex
);
2253 cpuset_release_agent(pathbuf
);
2255 atomic_dec(&cs
->count
);
2260 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2261 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2263 * Description: Returns the cpumask_t cpus_allowed of the cpuset
2264 * attached to the specified @tsk. Guaranteed to return some non-empty
2265 * subset of cpu_online_map, even if this means going outside the
2269 cpumask_t
cpuset_cpus_allowed(struct task_struct
*tsk
)
2273 mutex_lock(&callback_mutex
);
2275 guarantee_online_cpus(tsk
->cpuset
, &mask
);
2277 mutex_unlock(&callback_mutex
);
2282 void cpuset_init_current_mems_allowed(void)
2284 current
->mems_allowed
= NODE_MASK_ALL
;
2288 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2289 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2291 * Description: Returns the nodemask_t mems_allowed of the cpuset
2292 * attached to the specified @tsk. Guaranteed to return some non-empty
2293 * subset of node_online_map, even if this means going outside the
2297 nodemask_t
cpuset_mems_allowed(struct task_struct
*tsk
)
2301 mutex_lock(&callback_mutex
);
2303 guarantee_online_mems(tsk
->cpuset
, &mask
);
2305 mutex_unlock(&callback_mutex
);
2311 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2312 * @zl: the zonelist to be checked
2314 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2316 int cpuset_zonelist_valid_mems_allowed(struct zonelist
*zl
)
2320 for (i
= 0; zl
->zones
[i
]; i
++) {
2321 int nid
= zone_to_nid(zl
->zones
[i
]);
2323 if (node_isset(nid
, current
->mems_allowed
))
2330 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
2331 * ancestor to the specified cpuset. Call holding callback_mutex.
2332 * If no ancestor is mem_exclusive (an unusual configuration), then
2333 * returns the root cpuset.
2335 static const struct cpuset
*nearest_exclusive_ancestor(const struct cpuset
*cs
)
2337 while (!is_mem_exclusive(cs
) && cs
->parent
)
2343 * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node?
2344 * @z: is this zone on an allowed node?
2345 * @gfp_mask: memory allocation flags
2347 * If we're in interrupt, yes, we can always allocate. If
2348 * __GFP_THISNODE is set, yes, we can always allocate. If zone
2349 * z's node is in our tasks mems_allowed, yes. If it's not a
2350 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2351 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
2352 * If the task has been OOM killed and has access to memory reserves
2353 * as specified by the TIF_MEMDIE flag, yes.
2356 * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall()
2357 * reduces to cpuset_zone_allowed_hardwall(). Otherwise,
2358 * cpuset_zone_allowed_softwall() might sleep, and might allow a zone
2359 * from an enclosing cpuset.
2361 * cpuset_zone_allowed_hardwall() only handles the simpler case of
2362 * hardwall cpusets, and never sleeps.
2364 * The __GFP_THISNODE placement logic is really handled elsewhere,
2365 * by forcibly using a zonelist starting at a specified node, and by
2366 * (in get_page_from_freelist()) refusing to consider the zones for
2367 * any node on the zonelist except the first. By the time any such
2368 * calls get to this routine, we should just shut up and say 'yes'.
2370 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2371 * and do not allow allocations outside the current tasks cpuset
2372 * unless the task has been OOM killed as is marked TIF_MEMDIE.
2373 * GFP_KERNEL allocations are not so marked, so can escape to the
2374 * nearest enclosing mem_exclusive ancestor cpuset.
2376 * Scanning up parent cpusets requires callback_mutex. The
2377 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2378 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2379 * current tasks mems_allowed came up empty on the first pass over
2380 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2381 * cpuset are short of memory, might require taking the callback_mutex
2384 * The first call here from mm/page_alloc:get_page_from_freelist()
2385 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2386 * so no allocation on a node outside the cpuset is allowed (unless
2387 * in interrupt, of course).
2389 * The second pass through get_page_from_freelist() doesn't even call
2390 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2391 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2392 * in alloc_flags. That logic and the checks below have the combined
2394 * in_interrupt - any node ok (current task context irrelevant)
2395 * GFP_ATOMIC - any node ok
2396 * TIF_MEMDIE - any node ok
2397 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2398 * GFP_USER - only nodes in current tasks mems allowed ok.
2401 * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you
2402 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2403 * the code that might scan up ancestor cpusets and sleep.
2406 int __cpuset_zone_allowed_softwall(struct zone
*z
, gfp_t gfp_mask
)
2408 int node
; /* node that zone z is on */
2409 const struct cpuset
*cs
; /* current cpuset ancestors */
2410 int allowed
; /* is allocation in zone z allowed? */
2412 if (in_interrupt() || (gfp_mask
& __GFP_THISNODE
))
2414 node
= zone_to_nid(z
);
2415 might_sleep_if(!(gfp_mask
& __GFP_HARDWALL
));
2416 if (node_isset(node
, current
->mems_allowed
))
2419 * Allow tasks that have access to memory reserves because they have
2420 * been OOM killed to get memory anywhere.
2422 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
2424 if (gfp_mask
& __GFP_HARDWALL
) /* If hardwall request, stop here */
2427 if (current
->flags
& PF_EXITING
) /* Let dying task have memory */
2430 /* Not hardwall and node outside mems_allowed: scan up cpusets */
2431 mutex_lock(&callback_mutex
);
2434 cs
= nearest_exclusive_ancestor(current
->cpuset
);
2435 task_unlock(current
);
2437 allowed
= node_isset(node
, cs
->mems_allowed
);
2438 mutex_unlock(&callback_mutex
);
2443 * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node?
2444 * @z: is this zone on an allowed node?
2445 * @gfp_mask: memory allocation flags
2447 * If we're in interrupt, yes, we can always allocate.
2448 * If __GFP_THISNODE is set, yes, we can always allocate. If zone
2449 * z's node is in our tasks mems_allowed, yes. If the task has been
2450 * OOM killed and has access to memory reserves as specified by the
2451 * TIF_MEMDIE flag, yes. Otherwise, no.
2453 * The __GFP_THISNODE placement logic is really handled elsewhere,
2454 * by forcibly using a zonelist starting at a specified node, and by
2455 * (in get_page_from_freelist()) refusing to consider the zones for
2456 * any node on the zonelist except the first. By the time any such
2457 * calls get to this routine, we should just shut up and say 'yes'.
2459 * Unlike the cpuset_zone_allowed_softwall() variant, above,
2460 * this variant requires that the zone be in the current tasks
2461 * mems_allowed or that we're in interrupt. It does not scan up the
2462 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2466 int __cpuset_zone_allowed_hardwall(struct zone
*z
, gfp_t gfp_mask
)
2468 int node
; /* node that zone z is on */
2470 if (in_interrupt() || (gfp_mask
& __GFP_THISNODE
))
2472 node
= zone_to_nid(z
);
2473 if (node_isset(node
, current
->mems_allowed
))
2476 * Allow tasks that have access to memory reserves because they have
2477 * been OOM killed to get memory anywhere.
2479 if (unlikely(test_thread_flag(TIF_MEMDIE
)))
2485 * cpuset_lock - lock out any changes to cpuset structures
2487 * The out of memory (oom) code needs to mutex_lock cpusets
2488 * from being changed while it scans the tasklist looking for a
2489 * task in an overlapping cpuset. Expose callback_mutex via this
2490 * cpuset_lock() routine, so the oom code can lock it, before
2491 * locking the task list. The tasklist_lock is a spinlock, so
2492 * must be taken inside callback_mutex.
2495 void cpuset_lock(void)
2497 mutex_lock(&callback_mutex
);
2501 * cpuset_unlock - release lock on cpuset changes
2503 * Undo the lock taken in a previous cpuset_lock() call.
2506 void cpuset_unlock(void)
2508 mutex_unlock(&callback_mutex
);
2512 * cpuset_mem_spread_node() - On which node to begin search for a page
2514 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2515 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2516 * and if the memory allocation used cpuset_mem_spread_node()
2517 * to determine on which node to start looking, as it will for
2518 * certain page cache or slab cache pages such as used for file
2519 * system buffers and inode caches, then instead of starting on the
2520 * local node to look for a free page, rather spread the starting
2521 * node around the tasks mems_allowed nodes.
2523 * We don't have to worry about the returned node being offline
2524 * because "it can't happen", and even if it did, it would be ok.
2526 * The routines calling guarantee_online_mems() are careful to
2527 * only set nodes in task->mems_allowed that are online. So it
2528 * should not be possible for the following code to return an
2529 * offline node. But if it did, that would be ok, as this routine
2530 * is not returning the node where the allocation must be, only
2531 * the node where the search should start. The zonelist passed to
2532 * __alloc_pages() will include all nodes. If the slab allocator
2533 * is passed an offline node, it will fall back to the local node.
2534 * See kmem_cache_alloc_node().
2537 int cpuset_mem_spread_node(void)
2541 node
= next_node(current
->cpuset_mem_spread_rotor
, current
->mems_allowed
);
2542 if (node
== MAX_NUMNODES
)
2543 node
= first_node(current
->mems_allowed
);
2544 current
->cpuset_mem_spread_rotor
= node
;
2547 EXPORT_SYMBOL_GPL(cpuset_mem_spread_node
);
2550 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2551 * @p: pointer to task_struct of some other task.
2553 * Description: Return true if the nearest mem_exclusive ancestor
2554 * cpusets of tasks @p and current overlap. Used by oom killer to
2555 * determine if task @p's memory usage might impact the memory
2556 * available to the current task.
2558 * Call while holding callback_mutex.
2561 int cpuset_excl_nodes_overlap(const struct task_struct
*p
)
2563 const struct cpuset
*cs1
, *cs2
; /* my and p's cpuset ancestors */
2564 int overlap
= 1; /* do cpusets overlap? */
2567 if (current
->flags
& PF_EXITING
) {
2568 task_unlock(current
);
2571 cs1
= nearest_exclusive_ancestor(current
->cpuset
);
2572 task_unlock(current
);
2574 task_lock((struct task_struct
*)p
);
2575 if (p
->flags
& PF_EXITING
) {
2576 task_unlock((struct task_struct
*)p
);
2579 cs2
= nearest_exclusive_ancestor(p
->cpuset
);
2580 task_unlock((struct task_struct
*)p
);
2582 overlap
= nodes_intersects(cs1
->mems_allowed
, cs2
->mems_allowed
);
2588 * Collection of memory_pressure is suppressed unless
2589 * this flag is enabled by writing "1" to the special
2590 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2593 int cpuset_memory_pressure_enabled __read_mostly
;
2596 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2598 * Keep a running average of the rate of synchronous (direct)
2599 * page reclaim efforts initiated by tasks in each cpuset.
2601 * This represents the rate at which some task in the cpuset
2602 * ran low on memory on all nodes it was allowed to use, and
2603 * had to enter the kernels page reclaim code in an effort to
2604 * create more free memory by tossing clean pages or swapping
2605 * or writing dirty pages.
2607 * Display to user space in the per-cpuset read-only file
2608 * "memory_pressure". Value displayed is an integer
2609 * representing the recent rate of entry into the synchronous
2610 * (direct) page reclaim by any task attached to the cpuset.
2613 void __cpuset_memory_pressure_bump(void)
2618 cs
= current
->cpuset
;
2619 fmeter_markevent(&cs
->fmeter
);
2620 task_unlock(current
);
2624 * proc_cpuset_show()
2625 * - Print tasks cpuset path into seq_file.
2626 * - Used for /proc/<pid>/cpuset.
2627 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2628 * doesn't really matter if tsk->cpuset changes after we read it,
2629 * and we take manage_mutex, keeping attach_task() from changing it
2630 * anyway. No need to check that tsk->cpuset != NULL, thanks to
2631 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
2632 * cpuset to top_cpuset.
2634 static int proc_cpuset_show(struct seq_file
*m
, void *v
)
2637 struct task_struct
*tsk
;
2642 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2648 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
2653 mutex_lock(&manage_mutex
);
2655 retval
= cpuset_path(tsk
->cpuset
, buf
, PAGE_SIZE
);
2661 mutex_unlock(&manage_mutex
);
2662 put_task_struct(tsk
);
2669 static int cpuset_open(struct inode
*inode
, struct file
*file
)
2671 struct pid
*pid
= PROC_I(inode
)->pid
;
2672 return single_open(file
, proc_cpuset_show
, pid
);
2675 const struct file_operations proc_cpuset_operations
= {
2676 .open
= cpuset_open
,
2678 .llseek
= seq_lseek
,
2679 .release
= single_release
,
2682 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2683 char *cpuset_task_status_allowed(struct task_struct
*task
, char *buffer
)
2685 buffer
+= sprintf(buffer
, "Cpus_allowed:\t");
2686 buffer
+= cpumask_scnprintf(buffer
, PAGE_SIZE
, task
->cpus_allowed
);
2687 buffer
+= sprintf(buffer
, "\n");
2688 buffer
+= sprintf(buffer
, "Mems_allowed:\t");
2689 buffer
+= nodemask_scnprintf(buffer
, PAGE_SIZE
, task
->mems_allowed
);
2690 buffer
+= sprintf(buffer
, "\n");