blob: bd1e89c4c96ae80a6db4d46a5764e010df331eae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
Paul Jackson825a46a2006-03-24 03:16:03 -08007 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Portions derived from Patrick Mochel's sysfs code.
10 * sysfs is Copyright (c) 2001-3 Patrick Mochel
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Paul Jackson825a46a2006-03-24 03:16:03 -080012 * 2003-10-10 Written by Simon Derr.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 2003-10-22 Updates by Stephen Hemminger.
Paul Jackson825a46a2006-03-24 03:16:03 -080014 * 2004 May-July Rework by Paul Jackson.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
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.
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
27#include <linux/fs.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>
Paul Jackson68860ec2005-10-30 15:02:36 -080033#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/mm.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>
Paul Jackson6b9c2602006-01-08 01:02:02 -080040#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sched.h>
42#include <linux/seq_file.h>
David Quigley22fb52d2006-06-23 02:04:00 -070043#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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>
52
53#include <asm/uaccess.h>
54#include <asm/atomic.h>
Ingo Molnar3d3f26a2006-03-23 03:00:18 -080055#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Paul Jacksonc5b2aff82006-01-08 01:01:51 -080057#define CPUSET_SUPER_MAGIC 0x27e0eb
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Paul Jackson202f72d2006-01-08 01:01:57 -080059/*
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.
63 */
Paul Jackson7edc5962006-01-08 01:02:03 -080064int number_of_cpusets __read_mostly;
Paul Jackson202f72d2006-01-08 01:01:57 -080065
Paul Jackson3e0d98b2006-01-08 01:01:49 -080066/* See "Frequency meter" comments, below. */
67
68struct fmeter {
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 */
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct cpuset {
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 */
79
Paul Jackson053199e2005-10-30 15:02:30 -080080 /*
81 * Count is atomic so can incr (fork) or decr (exit) without a lock.
82 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 atomic_t count; /* count tasks using this cpuset */
84
85 /*
86 * We link our 'sibling' struct into our parents 'children'.
87 * Our children link their 'sibling' into our 'children'.
88 */
89 struct list_head sibling; /* my parents children */
90 struct list_head children; /* my children */
91
92 struct cpuset *parent; /* my parent */
93 struct dentry *dentry; /* cpuset fs entry */
94
95 /*
96 * Copy of global cpuset_mems_generation as of the most
97 * recent time this cpuset changed its mems_allowed.
98 */
Paul Jackson3e0d98b2006-01-08 01:01:49 -080099 int mems_generation;
100
101 struct fmeter fmeter; /* memory_pressure filter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104/* bits in struct cpuset flags field */
105typedef enum {
106 CS_CPU_EXCLUSIVE,
107 CS_MEM_EXCLUSIVE,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800108 CS_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 CS_REMOVED,
Paul Jackson825a46a2006-03-24 03:16:03 -0800110 CS_NOTIFY_ON_RELEASE,
111 CS_SPREAD_PAGE,
112 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113} cpuset_flagbits_t;
114
115/* convenient tests for these bits */
116static inline int is_cpu_exclusive(const struct cpuset *cs)
117{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800118 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static inline int is_mem_exclusive(const struct cpuset *cs)
122{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800123 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static inline int is_removed(const struct cpuset *cs)
127{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800128 return test_bit(CS_REMOVED, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static inline int notify_on_release(const struct cpuset *cs)
132{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800133 return test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Paul Jackson45b07ef2006-01-08 01:00:56 -0800136static inline int is_memory_migrate(const struct cpuset *cs)
137{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800138 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800139}
140
Paul Jackson825a46a2006-03-24 03:16:03 -0800141static inline int is_spread_page(const struct cpuset *cs)
142{
143 return test_bit(CS_SPREAD_PAGE, &cs->flags);
144}
145
146static inline int is_spread_slab(const struct cpuset *cs)
147{
148 return test_bit(CS_SPREAD_SLAB, &cs->flags);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Paul Jackson151a4422006-03-24 03:16:11 -0800152 * Increment this integer everytime any cpuset changes its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * 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.
156 *
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.
160 *
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.
Paul Jackson151a4422006-03-24 03:16:11 -0800166 *
167 * Since cpuset_mems_generation is guarded by manage_mutex,
168 * there is no need to mark it atomic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Paul Jackson151a4422006-03-24 03:16:11 -0800170static int cpuset_mems_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static 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),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181static struct vfsmount *cpuset_mount;
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800182static struct super_block *cpuset_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800185 * 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
Paul Jackson053199e2005-10-30 15:02:30 -0800187 * require taking task_lock() when dereferencing a tasks cpuset pointer.
188 * See "The task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800190 * 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
Paul Jackson053199e2005-10-30 15:02:30 -0800193 * and be able to modify cpusets. It can perform various checks on
194 * the cpuset structure first, knowing nothing will change. It can
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800195 * also allocate memory while just holding manage_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800196 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800197 * acquire callback_mutex to query cpusets. Once it is ready to make
198 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
Paul Jackson053199e2005-10-30 15:02:30 -0800200 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800201 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800202 * from one of the callbacks into the cpuset code from within
203 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800205 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800206 * access to cpusets.
207 *
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.
210 *
211 * Any task can increment and decrement the count field without lock.
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800212 * So in general, code holding manage_mutex or callback_mutex can't rely
Paul Jackson053199e2005-10-30 15:02:30 -0800213 * on the count field not changing. However, if the count goes to
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800214 * zero, then only attach_task(), which holds both mutexes, can
Paul Jackson053199e2005-10-30 15:02:30 -0800215 * 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).
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800218 * So code holding manage_mutex or callback_mutex can safely assume that
Paul Jackson053199e2005-10-30 15:02:30 -0800219 * if the count is zero, it will stay zero. Similarly, if a task
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800220 * holds manage_mutex or callback_mutex on a cpuset with zero count, it
Paul Jackson053199e2005-10-30 15:02:30 -0800221 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800222 * both of those mutexes.
Paul Jackson053199e2005-10-30 15:02:30 -0800223 *
224 * The cpuset_common_file_write handler for operations that modify
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800225 * the cpuset hierarchy holds manage_mutex across the entire operation,
Paul Jackson053199e2005-10-30 15:02:30 -0800226 * single threading all such cpuset modifications across the system.
227 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800228 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800229 * small pieces of code, such as when reading out possibly multi-word
230 * cpumasks and nodemasks.
231 *
232 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800233 * (usually) take either mutex. These are the two most performance
Paul Jackson053199e2005-10-30 15:02:30 -0800234 * critical pieces of code here. The exception occurs on cpuset_exit(),
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800235 * when a task in a notify_on_release cpuset exits. Then manage_mutex
Paul Jackson2efe86b2005-05-27 02:02:43 -0700236 * is taken, and if the cpuset count is zero, a usermode call made
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * to /sbin/cpuset_release_agent with the name of the cpuset (path
238 * relative to the root of cpuset file system) as the argument.
239 *
Paul Jackson053199e2005-10-30 15:02:30 -0800240 * 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
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700243 * least one task in the system (init), therefore, top_cpuset
Paul Jackson053199e2005-10-30 15:02:30 -0800244 * 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.
246 *
247 * The above "Tale of Two Semaphores" would be complete, but for:
248 *
249 * The task_lock() exception
250 *
251 * The need for this exception arises from the action of attach_task(),
252 * which overwrites one tasks cpuset pointer with another. It does
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800253 * so using both mutexes, however there are several performance
Paul Jackson053199e2005-10-30 15:02:30 -0800254 * critical places that need to reference task->cpuset without the
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800255 * expense of grabbing a system global mutex. Therefore except as
Paul Jackson053199e2005-10-30 15:02:30 -0800256 * 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
259 * such matters.
Paul Jackson6b9c2602006-01-08 01:02:02 -0800260 *
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().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800267static DEFINE_MUTEX(manage_mutex);
268static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700269
270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * 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.
274 */
275
276static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
277static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
278
279static 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,
282};
283
284static struct inode *cpuset_new_inode(mode_t mode)
285{
286 struct inode *inode = new_inode(cpuset_sb);
287
288 if (inode) {
289 inode->i_mode = mode;
290 inode->i_uid = current->fsuid;
291 inode->i_gid = current->fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 inode->i_blocks = 0;
293 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
294 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
295 }
296 return inode;
297}
298
299static void cpuset_diput(struct dentry *dentry, struct inode *inode)
300{
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)));
305 kfree(cs);
306 }
307 iput(inode);
308}
309
310static struct dentry_operations cpuset_dops = {
311 .d_iput = cpuset_diput,
312};
313
314static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
315{
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700316 struct dentry *d = lookup_one_len(name, parent, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!IS_ERR(d))
318 d->d_op = &cpuset_dops;
319 return d;
320}
321
322static void remove_dir(struct dentry *d)
323{
324 struct dentry *parent = dget(d->d_parent);
325
326 d_delete(d);
327 simple_rmdir(parent->d_inode, d);
328 dput(parent);
329}
330
331/*
332 * NOTE : the dentry must have been dget()'ed
333 */
334static void cpuset_d_remove_dir(struct dentry *dentry)
335{
336 struct list_head *node;
337
338 spin_lock(&dcache_lock);
339 node = dentry->d_subdirs.next;
340 while (node != &dentry->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800341 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 list_del_init(node);
343 if (d->d_inode) {
344 d = dget_locked(d);
345 spin_unlock(&dcache_lock);
346 d_delete(d);
347 simple_unlink(dentry->d_inode, d);
348 dput(d);
349 spin_lock(&dcache_lock);
350 }
351 node = dentry->d_subdirs.next;
352 }
Eric Dumazet5160ee62006-01-08 01:03:32 -0800353 list_del_init(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 spin_unlock(&dcache_lock);
355 remove_dir(dentry);
356}
357
358static struct super_operations cpuset_ops = {
359 .statfs = simple_statfs,
360 .drop_inode = generic_delete_inode,
361};
362
363static int cpuset_fill_super(struct super_block *sb, void *unused_data,
364 int unused_silent)
365{
366 struct inode *inode;
367 struct dentry *root;
368
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;
373 cpuset_sb = sb;
374
375 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
376 if (inode) {
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) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700380 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
382 return -ENOMEM;
383 }
384
385 root = d_alloc_root(inode);
386 if (!root) {
387 iput(inode);
388 return -ENOMEM;
389 }
390 sb->s_root = root;
391 return 0;
392}
393
David Howells454e2392006-06-23 02:02:57 -0700394static int cpuset_get_sb(struct file_system_type *fs_type,
395 int flags, const char *unused_dev_name,
396 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
David Howells454e2392006-06-23 02:02:57 -0700398 return get_sb_single(fs_type, flags, data, cpuset_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static struct file_system_type cpuset_fs_type = {
402 .name = "cpuset",
403 .get_sb = cpuset_get_sb,
404 .kill_sb = kill_litter_super,
405};
406
407/* struct cftype:
408 *
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
412 * kind of file.
413 *
414 *
415 * When reading/writing to a file:
416 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
417 * - the 'cftype' of the file is file->f_dentry->d_fsdata
418 */
419
420struct cftype {
421 char *name;
422 int private;
423 int (*open) (struct inode *inode, struct file *file);
424 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
425 loff_t *ppos);
426 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
427 loff_t *ppos);
428 int (*release) (struct inode *inode, struct file *file);
429};
430
431static inline struct cpuset *__d_cs(struct dentry *dentry)
432{
433 return dentry->d_fsdata;
434}
435
436static inline struct cftype *__d_cft(struct dentry *dentry)
437{
438 return dentry->d_fsdata;
439}
440
441/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800442 * Call with manage_mutex held. Writes path of cpuset into buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * Returns 0 on success, -errno on error.
444 */
445
446static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
447{
448 char *start;
449
450 start = buf + buflen;
451
452 *--start = '\0';
453 for (;;) {
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);
458 cs = cs->parent;
459 if (!cs)
460 break;
461 if (!cs->parent)
462 continue;
463 if (--start < buf)
464 return -ENAMETOOLONG;
465 *start = '/';
466 }
467 memmove(buf, start, buf + buflen - start);
468 return 0;
469}
470
471/*
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.
475 *
476 * Most likely, this user command will try to rmdir this cpuset.
477 *
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.
485 *
Paul Jackson3077a262005-08-09 10:07:59 -0700486 * 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.
493 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800494 * When we had only one cpuset mutex, we had to call this
Paul Jackson053199e2005-10-30 15:02:30 -0800495 * without holding it, to avoid deadlock when call_usermodehelper()
496 * allocated memory. With two locks, we could now call this while
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800497 * holding manage_mutex, but we still don't, so as to minimize
498 * the time manage_mutex is held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
500
Paul Jackson3077a262005-08-09 10:07:59 -0700501static void cpuset_release_agent(const char *pathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 char *argv[3], *envp[3];
504 int i;
505
Paul Jackson3077a262005-08-09 10:07:59 -0700506 if (!pathbuf)
507 return;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 i = 0;
510 argv[i++] = "/sbin/cpuset_release_agent";
Paul Jackson3077a262005-08-09 10:07:59 -0700511 argv[i++] = (char *)pathbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 argv[i] = NULL;
513
514 i = 0;
515 /* minimal command environment */
516 envp[i++] = "HOME=/";
517 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
518 envp[i] = NULL;
519
Paul Jackson3077a262005-08-09 10:07:59 -0700520 call_usermodehelper(argv[0], argv, envp, 0);
521 kfree(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/*
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
Paul Jackson3077a262005-08-09 10:07:59 -0700528 * 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
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800530 * cpuset_release_agent() with it later on, once manage_mutex is dropped.
531 * Call here with manage_mutex held.
Paul Jackson3077a262005-08-09 10:07:59 -0700532 *
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
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800537 * calling check_for_release() with manage_mutex held and the address
538 * of the pathbuf pointer, then dropping manage_mutex, then calling
Paul Jackson3077a262005-08-09 10:07:59 -0700539 * cpuset_release_agent() with pathbuf, as set by check_for_release().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
541
Paul Jackson3077a262005-08-09 10:07:59 -0700542static void check_for_release(struct cpuset *cs, char **ppathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
545 list_empty(&cs->children)) {
546 char *buf;
547
548 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
549 if (!buf)
550 return;
551 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
Paul Jackson3077a262005-08-09 10:07:59 -0700552 kfree(buf);
553 else
554 *ppathbuf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556}
557
558/*
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.
565 *
566 * One way or another, we guarantee to return some non-empty subset
567 * of cpu_online_map.
568 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800569 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
571
572static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
573{
574 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
575 cs = cs->parent;
576 if (cs)
577 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
578 else
579 *pmask = cpu_online_map;
580 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
581}
582
583/*
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.
589 *
590 * One way or another, we guarantee to return some non-empty subset
591 * of node_online_map.
592 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800593 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595
596static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
597{
598 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
599 cs = cs->parent;
600 if (cs)
601 nodes_and(*pmask, cs->mems_allowed, node_online_map);
602 else
603 *pmask = node_online_map;
604 BUG_ON(!nodes_intersects(*pmask, node_online_map));
605}
606
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800607/**
608 * cpuset_update_task_memory_state - update task memory placement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800610 * 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.
613 *
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().
617 *
Paul Jackson4a01c8d2006-03-31 02:30:50 -0800618 * 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
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800622 * current->mm->mmap_sem during call.
Paul Jackson5aa15b52005-10-30 15:02:28 -0800623 *
Paul Jackson6b9c2602006-01-08 01:02:02 -0800624 * 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(),
627 * using RCU.
628 *
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
641 * even exist.
Paul Jackson053199e2005-10-30 15:02:30 -0800642 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648
Randy Dunlapfe85a992006-02-03 03:04:23 -0800649void cpuset_update_task_memory_state(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Paul Jackson053199e2005-10-30 15:02:30 -0800651 int my_cpusets_mem_gen;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800652 struct task_struct *tsk = current;
Paul Jackson6b9c2602006-01-08 01:02:02 -0800653 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Paul Jackson03a285f2006-01-08 01:02:04 -0800655 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;
658 } else {
659 rcu_read_lock();
660 cs = rcu_dereference(tsk->cpuset);
661 my_cpusets_mem_gen = cs->mems_generation;
662 rcu_read_unlock();
663 }
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800664
665 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800666 mutex_lock(&callback_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800667 task_lock(tsk);
668 cs = tsk->cpuset; /* Maybe changed when task not locked */
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800669 guarantee_online_mems(cs, &tsk->mems_allowed);
670 tsk->cpuset_mems_generation = cs->mems_generation;
Paul Jackson825a46a2006-03-24 03:16:03 -0800671 if (is_spread_page(cs))
672 tsk->flags |= PF_SPREAD_PAGE;
673 else
674 tsk->flags &= ~PF_SPREAD_PAGE;
675 if (is_spread_slab(cs))
676 tsk->flags |= PF_SPREAD_SLAB;
677 else
678 tsk->flags &= ~PF_SPREAD_SLAB;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800679 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800680 mutex_unlock(&callback_mutex);
Paul Jackson74cb2152006-01-08 01:01:56 -0800681 mpol_rebind_task(tsk, &tsk->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683}
684
685/*
686 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
687 *
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
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800690 * are only set if the other's are set. Call holding manage_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
692
693static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
694{
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);
699}
700
701/*
702 * validate_change() - Used to validate that any proposed cpuset change
703 * follows the structural rules for cpusets.
704 *
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
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800708 * manage_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
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.
713 *
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.
717 *
718 * Return 0 if valid, -errno if not.
719 */
720
721static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
722{
723 struct cpuset *c, *par;
724
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))
728 return -EBUSY;
729 }
730
731 /* Remaining checks don't apply to root cpuset */
Paul Jackson69604062006-12-06 20:36:15 -0800732 if (cur == &top_cpuset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734
Paul Jackson69604062006-12-06 20:36:15 -0800735 par = cur->parent;
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* We must be a subset of our parent cpuset */
738 if (!is_cpuset_subset(trial, par))
739 return -EACCES;
740
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)) &&
744 c != cur &&
745 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
746 return -EINVAL;
747 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
748 c != cur &&
749 nodes_intersects(trial->mems_allowed, c->mems_allowed))
750 return -EINVAL;
751 }
752
753 return 0;
754}
755
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700756/*
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
763 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800764 * Call with manage_mutex held. May nest a call to the
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700765 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
Paul Jacksonabb5a5c2006-07-23 11:36:08 -0700766 * Must not be called holding callback_mutex, because we must
767 * not call lock_cpu_hotplug() while holding callback_mutex.
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700768 */
Paul Jackson212d6d22005-08-25 12:47:56 -0700769
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700770static void update_cpu_domains(struct cpuset *cur)
771{
772 struct cpuset *c, *par = cur->parent;
773 cpumask_t pspan, cspan;
774
775 if (par == NULL || cpus_empty(cur->cpus_allowed))
776 return;
777
778 /*
779 * Get all cpus from parent's cpus_allowed not part of exclusive
780 * children
781 */
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);
786 }
Paul Jacksonabb5a5c2006-07-23 11:36:08 -0700787 if (!is_cpu_exclusive(cur)) {
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700788 cpus_or(pspan, pspan, cur->cpus_allowed);
789 if (cpus_equal(pspan, cur->cpus_allowed))
790 return;
791 cspan = CPU_MASK_NONE;
792 } else {
793 if (cpus_empty(pspan))
794 return;
795 cspan = cur->cpus_allowed;
796 /*
797 * Get all cpus from current cpuset's cpus_allowed not part
798 * of exclusive children
799 */
800 list_for_each_entry(c, &cur->children, sibling) {
801 if (is_cpu_exclusive(c))
802 cpus_andnot(cspan, cspan, c->cpus_allowed);
803 }
804 }
805
806 lock_cpu_hotplug();
807 partition_sched_domains(&pspan, &cspan);
808 unlock_cpu_hotplug();
809}
810
Paul Jackson053199e2005-10-30 15:02:30 -0800811/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800812 * Call with manage_mutex held. May take callback_mutex during call.
Paul Jackson053199e2005-10-30 15:02:30 -0800813 */
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static int update_cpumask(struct cpuset *cs, char *buf)
816{
817 struct cpuset trialcs;
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700818 int retval, cpus_unchanged;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700820 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
821 if (cs == &top_cpuset)
822 return -EACCES;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 trialcs = *cs;
825 retval = cpulist_parse(buf, trialcs.cpus_allowed);
826 if (retval < 0)
827 return retval;
828 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
829 if (cpus_empty(trialcs.cpus_allowed))
830 return -ENOSPC;
831 retval = validate_change(cs, &trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700832 if (retval < 0)
833 return retval;
834 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800835 mutex_lock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700836 cs->cpus_allowed = trialcs.cpus_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800837 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700838 if (is_cpu_exclusive(cs) && !cpus_unchanged)
839 update_cpu_domains(cs);
840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Paul Jackson053199e2005-10-30 15:02:30 -0800843/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800844 * cpuset_migrate_mm
845 *
846 * Migrate memory region from one set of nodes to another.
847 *
848 * Temporarilly set tasks mems_allowed to target nodes of migration,
849 * so that the migration code can allocate pages on these nodes.
850 *
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
855 * our tasks cpuset.
856 *
857 * Hold callback_mutex around the two modifications of our tasks
858 * mems_allowed to synchronize with cpuset_mems_allowed().
859 *
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.
864 *
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
871 * nodemask.
872 */
873
874static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
875 const nodemask_t *to)
876{
877 struct task_struct *tsk = current;
878
879 cpuset_update_task_memory_state();
880
881 mutex_lock(&callback_mutex);
882 tsk->mems_allowed = *to;
883 mutex_unlock(&callback_mutex);
884
885 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
886
887 mutex_lock(&callback_mutex);
888 guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed);
889 mutex_unlock(&callback_mutex);
890}
891
892/*
Paul Jackson42253992006-01-08 01:01:59 -0800893 * 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
Paul Jackson04c19fa2006-01-08 01:02:00 -0800896 * 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.
Paul Jackson42253992006-01-08 01:01:59 -0800899 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800900 * Call with manage_mutex held. May take callback_mutex during call.
Paul Jackson42253992006-01-08 01:01:59 -0800901 * 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.
Paul Jackson053199e2005-10-30 15:02:30 -0800904 */
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906static int update_nodemask(struct cpuset *cs, char *buf)
907{
908 struct cpuset trialcs;
Paul Jackson04c19fa2006-01-08 01:02:00 -0800909 nodemask_t oldmem;
Paul Jackson42253992006-01-08 01:01:59 -0800910 struct task_struct *g, *p;
911 struct mm_struct **mmarray;
912 int i, n, ntasks;
Paul Jackson04c19fa2006-01-08 01:02:00 -0800913 int migrate;
Paul Jackson42253992006-01-08 01:01:59 -0800914 int fudge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int retval;
916
Paul Jackson38837fc2006-09-29 02:01:16 -0700917 /* top_cpuset.mems_allowed tracks node_online_map; it's read-only */
918 if (cs == &top_cpuset)
919 return -EACCES;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 trialcs = *cs;
922 retval = nodelist_parse(buf, trialcs.mems_allowed);
923 if (retval < 0)
Paul Jackson59dac162006-01-08 01:01:52 -0800924 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
Paul Jackson04c19fa2006-01-08 01:02:00 -0800926 oldmem = cs->mems_allowed;
927 if (nodes_equal(oldmem, trialcs.mems_allowed)) {
928 retval = 0; /* Too easy - nothing to do */
929 goto done;
930 }
Paul Jackson59dac162006-01-08 01:01:52 -0800931 if (nodes_empty(trialcs.mems_allowed)) {
932 retval = -ENOSPC;
933 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Paul Jackson59dac162006-01-08 01:01:52 -0800935 retval = validate_change(cs, &trialcs);
936 if (retval < 0)
937 goto done;
938
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800939 mutex_lock(&callback_mutex);
Paul Jackson59dac162006-01-08 01:01:52 -0800940 cs->mems_allowed = trialcs.mems_allowed;
Paul Jackson151a4422006-03-24 03:16:11 -0800941 cs->mems_generation = cpuset_mems_generation++;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800942 mutex_unlock(&callback_mutex);
Paul Jackson59dac162006-01-08 01:01:52 -0800943
Paul Jackson42253992006-01-08 01:01:59 -0800944 set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
945
946 fudge = 10; /* spare mmarray[] slots */
947 fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
948 retval = -ENOMEM;
949
950 /*
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.
956 */
957 while (1) {
958 ntasks = atomic_read(&cs->count); /* guess */
959 ntasks += fudge;
960 mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
961 if (!mmarray)
962 goto done;
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 */
967 kfree(mmarray);
968 }
969
970 n = 0;
971
972 /* Load up mmarray[] with mm reference for each task in cpuset. */
973 do_each_thread(g, p) {
974 struct mm_struct *mm;
975
976 if (n >= ntasks) {
977 printk(KERN_WARNING
978 "Cpuset mempolicy rebind incomplete.\n");
979 continue;
980 }
981 if (p->cpuset != cs)
982 continue;
983 mm = get_task_mm(p);
984 if (!mm)
985 continue;
986 mmarray[n++] = mm;
987 } while_each_thread(g, p);
988 write_unlock_irq(&tasklist_lock);
989
990 /*
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
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800998 * cpuset manage_mutex, we know that no other rebind effort will
Paul Jackson42253992006-01-08 01:01:59 -0800999 * be contending for the global variable cpuset_being_rebound.
1000 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001001 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001002 */
Paul Jackson04c19fa2006-01-08 01:02:00 -08001003 migrate = is_memory_migrate(cs);
Paul Jackson42253992006-01-08 01:01:59 -08001004 for (i = 0; i < n; i++) {
1005 struct mm_struct *mm = mmarray[i];
1006
1007 mpol_rebind_mm(mm, &cs->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001008 if (migrate)
1009 cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
Paul Jackson42253992006-01-08 01:01:59 -08001010 mmput(mm);
1011 }
1012
1013 /* We're done rebinding vma's to this cpusets new mems_allowed. */
1014 kfree(mmarray);
1015 set_cpuset_being_rebound(NULL);
1016 retval = 0;
Paul Jackson59dac162006-01-08 01:01:52 -08001017done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return retval;
1019}
1020
1021/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001022 * Call with manage_mutex held.
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001023 */
1024
1025static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
1026{
1027 if (simple_strtoul(buf, NULL, 10) != 0)
1028 cpuset_memory_pressure_enabled = 1;
1029 else
1030 cpuset_memory_pressure_enabled = 0;
1031 return 0;
1032}
1033
1034/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * 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,
Paul Jackson825a46a2006-03-24 03:16:03 -08001037 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
1038 * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * cs: the cpuset to update
1040 * buf: the buffer where we read the 0 or 1
Paul Jackson053199e2005-10-30 15:02:30 -08001041 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001042 * Call with manage_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044
1045static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
1046{
1047 int turning_on;
1048 struct cpuset trialcs;
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001049 int err, cpu_exclusive_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
1052
1053 trialcs = *cs;
1054 if (turning_on)
1055 set_bit(bit, &trialcs.flags);
1056 else
1057 clear_bit(bit, &trialcs.flags);
1058
1059 err = validate_change(cs, &trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001060 if (err < 0)
1061 return err;
1062 cpu_exclusive_changed =
1063 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001064 mutex_lock(&callback_mutex);
Paul Jackson69604062006-12-06 20:36:15 -08001065 cs->flags = trialcs.flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001066 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001067
1068 if (cpu_exclusive_changed)
1069 update_cpu_domains(cs);
1070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Paul Jackson053199e2005-10-30 15:02:30 -08001073/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001074 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001075 *
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.
1082 *
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.
1086 *
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).
1091 *
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.
1096 *
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
1100 * will be stable.
1101 *
1102 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1103 * arithmetic overflow in the fmeter_update() routine.
1104 *
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
1115 * each event.
1116 */
1117
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 */
1122
1123/* Initialize a frequency meter */
1124static void fmeter_init(struct fmeter *fmp)
1125{
1126 fmp->cnt = 0;
1127 fmp->val = 0;
1128 fmp->time = 0;
1129 spin_lock_init(&fmp->lock);
1130}
1131
1132/* Internal meter update - process cnt events and update value */
1133static void fmeter_update(struct fmeter *fmp)
1134{
1135 time_t now = get_seconds();
1136 time_t ticks = now - fmp->time;
1137
1138 if (ticks == 0)
1139 return;
1140
1141 ticks = min(FM_MAXTICKS, ticks);
1142 while (ticks-- > 0)
1143 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1144 fmp->time = now;
1145
1146 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1147 fmp->cnt = 0;
1148}
1149
1150/* Process any previous ticks, then bump cnt by one (times scale). */
1151static void fmeter_markevent(struct fmeter *fmp)
1152{
1153 spin_lock(&fmp->lock);
1154 fmeter_update(fmp);
1155 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1156 spin_unlock(&fmp->lock);
1157}
1158
1159/* Process any previous ticks, then return current value. */
1160static int fmeter_getrate(struct fmeter *fmp)
1161{
1162 int val;
1163
1164 spin_lock(&fmp->lock);
1165 fmeter_update(fmp);
1166 val = fmp->val;
1167 spin_unlock(&fmp->lock);
1168 return val;
1169}
1170
1171/*
Paul Jackson053199e2005-10-30 15:02:30 -08001172 * 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.
1175 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001176 * Call holding manage_mutex. May take callback_mutex and task_lock of
Paul Jackson053199e2005-10-30 15:02:30 -08001177 * the task 'pid' during call.
1178 */
1179
Paul Jackson3077a262005-08-09 10:07:59 -07001180static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 pid_t pid;
1183 struct task_struct *tsk;
1184 struct cpuset *oldcs;
1185 cpumask_t cpus;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001186 nodemask_t from, to;
Paul Jackson42253992006-01-08 01:01:59 -08001187 struct mm_struct *mm;
David Quigley22fb52d2006-06-23 02:04:00 -07001188 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Paul Jackson3077a262005-08-09 10:07:59 -07001190 if (sscanf(pidbuf, "%d", &pid) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -EIO;
1192 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1193 return -ENOSPC;
1194
1195 if (pid) {
1196 read_lock(&tasklist_lock);
1197
1198 tsk = find_task_by_pid(pid);
Paul Jackson053199e2005-10-30 15:02:30 -08001199 if (!tsk || tsk->flags & PF_EXITING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 read_unlock(&tasklist_lock);
1201 return -ESRCH;
1202 }
1203
1204 get_task_struct(tsk);
1205 read_unlock(&tasklist_lock);
1206
1207 if ((current->euid) && (current->euid != tsk->uid)
1208 && (current->euid != tsk->suid)) {
1209 put_task_struct(tsk);
1210 return -EACCES;
1211 }
1212 } else {
1213 tsk = current;
1214 get_task_struct(tsk);
1215 }
1216
David Quigley22fb52d2006-06-23 02:04:00 -07001217 retval = security_task_setscheduler(tsk, 0, NULL);
1218 if (retval) {
1219 put_task_struct(tsk);
1220 return retval;
1221 }
1222
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001223 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 task_lock(tsk);
1226 oldcs = tsk->cpuset;
Paul Jackson181b6482006-09-29 02:01:48 -07001227 /*
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.
1231 */
1232 if (tsk->flags & PF_EXITING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001234 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 put_task_struct(tsk);
1236 return -ESRCH;
1237 }
1238 atomic_inc(&cs->count);
Paul Jackson6b9c2602006-01-08 01:02:02 -08001239 rcu_assign_pointer(tsk->cpuset, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 task_unlock(tsk);
1241
1242 guarantee_online_cpus(cs, &cpus);
1243 set_cpus_allowed(tsk, cpus);
1244
Paul Jackson45b07ef2006-01-08 01:00:56 -08001245 from = oldcs->mems_allowed;
1246 to = cs->mems_allowed;
1247
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001248 mutex_unlock(&callback_mutex);
Paul Jackson42253992006-01-08 01:01:59 -08001249
1250 mm = get_task_mm(tsk);
1251 if (mm) {
1252 mpol_rebind_mm(mm, &to);
Paul Jackson2741a552006-03-31 02:30:51 -08001253 if (is_memory_migrate(cs))
Paul Jacksone4e364e2006-03-31 02:30:52 -08001254 cpuset_migrate_mm(mm, &from, &to);
Paul Jackson42253992006-01-08 01:01:59 -08001255 mmput(mm);
1256 }
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 put_task_struct(tsk);
Paul Jackson6b9c2602006-01-08 01:02:02 -08001259 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (atomic_dec_and_test(&oldcs->count))
Paul Jackson3077a262005-08-09 10:07:59 -07001261 check_for_release(oldcs, ppathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return 0;
1263}
1264
1265/* The various types of files and directories in a cpuset file system */
1266
1267typedef enum {
1268 FILE_ROOT,
1269 FILE_DIR,
Paul Jackson45b07ef2006-01-08 01:00:56 -08001270 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 FILE_CPULIST,
1272 FILE_MEMLIST,
1273 FILE_CPU_EXCLUSIVE,
1274 FILE_MEM_EXCLUSIVE,
1275 FILE_NOTIFY_ON_RELEASE,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001276 FILE_MEMORY_PRESSURE_ENABLED,
1277 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001278 FILE_SPREAD_PAGE,
1279 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 FILE_TASKLIST,
1281} cpuset_filetype_t;
1282
1283static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
1284 size_t nbytes, loff_t *unused_ppos)
1285{
1286 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1287 struct cftype *cft = __d_cft(file->f_dentry);
1288 cpuset_filetype_t type = cft->private;
1289 char *buffer;
Paul Jackson3077a262005-08-09 10:07:59 -07001290 char *pathbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int retval = 0;
1292
1293 /* Crude upper limit on largest legitimate cpulist user might write. */
1294 if (nbytes > 100 + 6 * NR_CPUS)
1295 return -E2BIG;
1296
1297 /* +1 for nul-terminator */
1298 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
1299 return -ENOMEM;
1300
1301 if (copy_from_user(buffer, userbuf, nbytes)) {
1302 retval = -EFAULT;
1303 goto out1;
1304 }
1305 buffer[nbytes] = 0; /* nul-terminate */
1306
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001307 mutex_lock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (is_removed(cs)) {
1310 retval = -ENODEV;
1311 goto out2;
1312 }
1313
1314 switch (type) {
1315 case FILE_CPULIST:
1316 retval = update_cpumask(cs, buffer);
1317 break;
1318 case FILE_MEMLIST:
1319 retval = update_nodemask(cs, buffer);
1320 break;
1321 case FILE_CPU_EXCLUSIVE:
1322 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
1323 break;
1324 case FILE_MEM_EXCLUSIVE:
1325 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
1326 break;
1327 case FILE_NOTIFY_ON_RELEASE:
1328 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
1329 break;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001330 case FILE_MEMORY_MIGRATE:
1331 retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
1332 break;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001333 case FILE_MEMORY_PRESSURE_ENABLED:
1334 retval = update_memory_pressure_enabled(cs, buffer);
1335 break;
1336 case FILE_MEMORY_PRESSURE:
1337 retval = -EACCES;
1338 break;
Paul Jackson825a46a2006-03-24 03:16:03 -08001339 case FILE_SPREAD_PAGE:
1340 retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
Paul Jackson151a4422006-03-24 03:16:11 -08001341 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson825a46a2006-03-24 03:16:03 -08001342 break;
1343 case FILE_SPREAD_SLAB:
1344 retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
Paul Jackson151a4422006-03-24 03:16:11 -08001345 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson825a46a2006-03-24 03:16:03 -08001346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 case FILE_TASKLIST:
Paul Jackson3077a262005-08-09 10:07:59 -07001348 retval = attach_task(cs, buffer, &pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 break;
1350 default:
1351 retval = -EINVAL;
1352 goto out2;
1353 }
1354
1355 if (retval == 0)
1356 retval = nbytes;
1357out2:
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001358 mutex_unlock(&manage_mutex);
Paul Jackson3077a262005-08-09 10:07:59 -07001359 cpuset_release_agent(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360out1:
1361 kfree(buffer);
1362 return retval;
1363}
1364
1365static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
1366 size_t nbytes, loff_t *ppos)
1367{
1368 ssize_t retval = 0;
1369 struct cftype *cft = __d_cft(file->f_dentry);
1370 if (!cft)
1371 return -ENODEV;
1372
1373 /* special function ? */
1374 if (cft->write)
1375 retval = cft->write(file, buf, nbytes, ppos);
1376 else
1377 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
1378
1379 return retval;
1380}
1381
1382/*
1383 * These ascii lists should be read in a single call, by using a user
1384 * buffer large enough to hold the entire map. If read in smaller
1385 * chunks, there is no guarantee of atomicity. Since the display format
1386 * used, list of ranges of sequential numbers, is variable length,
1387 * and since these maps can change value dynamically, one could read
1388 * gibberish by doing partial reads while a list was changing.
1389 * A single large read to a buffer that crosses a page boundary is
1390 * ok, because the result being copied to user land is not recomputed
1391 * across a page fault.
1392 */
1393
1394static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1395{
1396 cpumask_t mask;
1397
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001398 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 mask = cs->cpus_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001400 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return cpulist_scnprintf(page, PAGE_SIZE, mask);
1403}
1404
1405static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1406{
1407 nodemask_t mask;
1408
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001409 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 mask = cs->mems_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001411 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1414}
1415
1416static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
1417 size_t nbytes, loff_t *ppos)
1418{
1419 struct cftype *cft = __d_cft(file->f_dentry);
1420 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1421 cpuset_filetype_t type = cft->private;
1422 char *page;
1423 ssize_t retval = 0;
1424 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1427 return -ENOMEM;
1428
1429 s = page;
1430
1431 switch (type) {
1432 case FILE_CPULIST:
1433 s += cpuset_sprintf_cpulist(s, cs);
1434 break;
1435 case FILE_MEMLIST:
1436 s += cpuset_sprintf_memlist(s, cs);
1437 break;
1438 case FILE_CPU_EXCLUSIVE:
1439 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
1440 break;
1441 case FILE_MEM_EXCLUSIVE:
1442 *s++ = is_mem_exclusive(cs) ? '1' : '0';
1443 break;
1444 case FILE_NOTIFY_ON_RELEASE:
1445 *s++ = notify_on_release(cs) ? '1' : '0';
1446 break;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001447 case FILE_MEMORY_MIGRATE:
1448 *s++ = is_memory_migrate(cs) ? '1' : '0';
1449 break;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001450 case FILE_MEMORY_PRESSURE_ENABLED:
1451 *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
1452 break;
1453 case FILE_MEMORY_PRESSURE:
1454 s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
1455 break;
Paul Jackson825a46a2006-03-24 03:16:03 -08001456 case FILE_SPREAD_PAGE:
1457 *s++ = is_spread_page(cs) ? '1' : '0';
1458 break;
1459 case FILE_SPREAD_SLAB:
1460 *s++ = is_spread_slab(cs) ? '1' : '0';
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 default:
1463 retval = -EINVAL;
1464 goto out;
1465 }
1466 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Al Viroeacaa1f2005-09-30 03:26:43 +01001468 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469out:
1470 free_page((unsigned long)page);
1471 return retval;
1472}
1473
1474static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1475 loff_t *ppos)
1476{
1477 ssize_t retval = 0;
1478 struct cftype *cft = __d_cft(file->f_dentry);
1479 if (!cft)
1480 return -ENODEV;
1481
1482 /* special function ? */
1483 if (cft->read)
1484 retval = cft->read(file, buf, nbytes, ppos);
1485 else
1486 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1487
1488 return retval;
1489}
1490
1491static int cpuset_file_open(struct inode *inode, struct file *file)
1492{
1493 int err;
1494 struct cftype *cft;
1495
1496 err = generic_file_open(inode, file);
1497 if (err)
1498 return err;
1499
1500 cft = __d_cft(file->f_dentry);
1501 if (!cft)
1502 return -ENODEV;
1503 if (cft->open)
1504 err = cft->open(inode, file);
1505 else
1506 err = 0;
1507
1508 return err;
1509}
1510
1511static int cpuset_file_release(struct inode *inode, struct file *file)
1512{
1513 struct cftype *cft = __d_cft(file->f_dentry);
1514 if (cft->release)
1515 return cft->release(inode, file);
1516 return 0;
1517}
1518
Paul Jackson18a19cb2005-10-30 15:02:31 -08001519/*
1520 * cpuset_rename - Only allow simple rename of directories in place.
1521 */
1522static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
1523 struct inode *new_dir, struct dentry *new_dentry)
1524{
1525 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1526 return -ENOTDIR;
1527 if (new_dentry->d_inode)
1528 return -EEXIST;
1529 if (old_dir != new_dir)
1530 return -EIO;
1531 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534static struct file_operations cpuset_file_operations = {
1535 .read = cpuset_file_read,
1536 .write = cpuset_file_write,
1537 .llseek = generic_file_llseek,
1538 .open = cpuset_file_open,
1539 .release = cpuset_file_release,
1540};
1541
1542static struct inode_operations cpuset_dir_inode_operations = {
1543 .lookup = simple_lookup,
1544 .mkdir = cpuset_mkdir,
1545 .rmdir = cpuset_rmdir,
Paul Jackson18a19cb2005-10-30 15:02:31 -08001546 .rename = cpuset_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547};
1548
1549static int cpuset_create_file(struct dentry *dentry, int mode)
1550{
1551 struct inode *inode;
1552
1553 if (!dentry)
1554 return -ENOENT;
1555 if (dentry->d_inode)
1556 return -EEXIST;
1557
1558 inode = cpuset_new_inode(mode);
1559 if (!inode)
1560 return -ENOMEM;
1561
1562 if (S_ISDIR(mode)) {
1563 inode->i_op = &cpuset_dir_inode_operations;
1564 inode->i_fop = &simple_dir_operations;
1565
1566 /* start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -07001567 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 } else if (S_ISREG(mode)) {
1569 inode->i_size = 0;
1570 inode->i_fop = &cpuset_file_operations;
1571 }
1572
1573 d_instantiate(dentry, inode);
1574 dget(dentry); /* Extra count - pin the dentry in core */
1575 return 0;
1576}
1577
1578/*
1579 * cpuset_create_dir - create a directory for an object.
Paul Jacksonc5b2aff82006-01-08 01:01:51 -08001580 * cs: the cpuset we create the directory for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * It must have a valid ->parent field
1582 * And we are going to fill its ->dentry field.
1583 * name: The name to give to the cpuset directory. Will be copied.
1584 * mode: mode to set on new directory.
1585 */
1586
1587static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1588{
1589 struct dentry *dentry = NULL;
1590 struct dentry *parent;
1591 int error = 0;
1592
1593 parent = cs->parent->dentry;
1594 dentry = cpuset_get_dentry(parent, name);
1595 if (IS_ERR(dentry))
1596 return PTR_ERR(dentry);
1597 error = cpuset_create_file(dentry, S_IFDIR | mode);
1598 if (!error) {
1599 dentry->d_fsdata = cs;
Dave Hansend8c76e62006-09-30 23:29:04 -07001600 inc_nlink(parent->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 cs->dentry = dentry;
1602 }
1603 dput(dentry);
1604
1605 return error;
1606}
1607
1608static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1609{
1610 struct dentry *dentry;
1611 int error;
1612
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001613 mutex_lock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 dentry = cpuset_get_dentry(dir, cft->name);
1615 if (!IS_ERR(dentry)) {
1616 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1617 if (!error)
1618 dentry->d_fsdata = (void *)cft;
1619 dput(dentry);
1620 } else
1621 error = PTR_ERR(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001622 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return error;
1624}
1625
1626/*
1627 * Stuff for reading the 'tasks' file.
1628 *
1629 * Reading this file can return large amounts of data if a cpuset has
1630 * *lots* of attached tasks. So it may need several calls to read(),
1631 * but we cannot guarantee that the information we produce is correct
1632 * unless we produce it entirely atomically.
1633 *
1634 * Upon tasks file open(), a struct ctr_struct is allocated, that
1635 * will have a pointer to an array (also allocated here). The struct
1636 * ctr_struct * is stored in file->private_data. Its resources will
1637 * be freed by release() when the file is closed. The array is used
1638 * to sprintf the PIDs and then used by read().
1639 */
1640
1641/* cpusets_tasks_read array */
1642
1643struct ctr_struct {
1644 char *buf;
1645 int bufsz;
1646};
1647
1648/*
1649 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
Paul Jackson053199e2005-10-30 15:02:30 -08001650 * Return actual number of pids loaded. No need to task_lock(p)
1651 * when reading out p->cpuset, as we don't really care if it changes
1652 * on the next cycle, and we are not going to try to dereference it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001654static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 int n = 0;
1657 struct task_struct *g, *p;
1658
1659 read_lock(&tasklist_lock);
1660
1661 do_each_thread(g, p) {
1662 if (p->cpuset == cs) {
1663 pidarray[n++] = p->pid;
1664 if (unlikely(n == npids))
1665 goto array_full;
1666 }
1667 } while_each_thread(g, p);
1668
1669array_full:
1670 read_unlock(&tasklist_lock);
1671 return n;
1672}
1673
1674static int cmppid(const void *a, const void *b)
1675{
1676 return *(pid_t *)a - *(pid_t *)b;
1677}
1678
1679/*
1680 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1681 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1682 * count 'cnt' of how many chars would be written if buf were large enough.
1683 */
1684static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1685{
1686 int cnt = 0;
1687 int i;
1688
1689 for (i = 0; i < npids; i++)
1690 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1691 return cnt;
1692}
1693
Paul Jackson053199e2005-10-30 15:02:30 -08001694/*
1695 * Handle an open on 'tasks' file. Prepare a buffer listing the
1696 * process id's of tasks currently attached to the cpuset being opened.
1697 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001698 * Does not require any specific cpuset mutexes, and does not take any.
Paul Jackson053199e2005-10-30 15:02:30 -08001699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700static int cpuset_tasks_open(struct inode *unused, struct file *file)
1701{
1702 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1703 struct ctr_struct *ctr;
1704 pid_t *pidarray;
1705 int npids;
1706 char c;
1707
1708 if (!(file->f_mode & FMODE_READ))
1709 return 0;
1710
1711 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1712 if (!ctr)
1713 goto err0;
1714
1715 /*
1716 * If cpuset gets more users after we read count, we won't have
1717 * enough space - tough. This race is indistinguishable to the
1718 * caller from the case that the additional cpuset users didn't
1719 * show up until sometime later on.
1720 */
1721 npids = atomic_read(&cs->count);
1722 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1723 if (!pidarray)
1724 goto err1;
1725
1726 npids = pid_array_load(pidarray, npids, cs);
1727 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1728
1729 /* Call pid_array_to_buf() twice, first just to get bufsz */
1730 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1731 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1732 if (!ctr->buf)
1733 goto err2;
1734 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1735
1736 kfree(pidarray);
1737 file->private_data = ctr;
1738 return 0;
1739
1740err2:
1741 kfree(pidarray);
1742err1:
1743 kfree(ctr);
1744err0:
1745 return -ENOMEM;
1746}
1747
1748static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1749 size_t nbytes, loff_t *ppos)
1750{
1751 struct ctr_struct *ctr = file->private_data;
1752
1753 if (*ppos + nbytes > ctr->bufsz)
1754 nbytes = ctr->bufsz - *ppos;
1755 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1756 return -EFAULT;
1757 *ppos += nbytes;
1758 return nbytes;
1759}
1760
1761static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1762{
1763 struct ctr_struct *ctr;
1764
1765 if (file->f_mode & FMODE_READ) {
1766 ctr = file->private_data;
1767 kfree(ctr->buf);
1768 kfree(ctr);
1769 }
1770 return 0;
1771}
1772
1773/*
1774 * for the common functions, 'private' gives the type of file
1775 */
1776
1777static struct cftype cft_tasks = {
1778 .name = "tasks",
1779 .open = cpuset_tasks_open,
1780 .read = cpuset_tasks_read,
1781 .release = cpuset_tasks_release,
1782 .private = FILE_TASKLIST,
1783};
1784
1785static struct cftype cft_cpus = {
1786 .name = "cpus",
1787 .private = FILE_CPULIST,
1788};
1789
1790static struct cftype cft_mems = {
1791 .name = "mems",
1792 .private = FILE_MEMLIST,
1793};
1794
1795static struct cftype cft_cpu_exclusive = {
1796 .name = "cpu_exclusive",
1797 .private = FILE_CPU_EXCLUSIVE,
1798};
1799
1800static struct cftype cft_mem_exclusive = {
1801 .name = "mem_exclusive",
1802 .private = FILE_MEM_EXCLUSIVE,
1803};
1804
1805static struct cftype cft_notify_on_release = {
1806 .name = "notify_on_release",
1807 .private = FILE_NOTIFY_ON_RELEASE,
1808};
1809
Paul Jackson45b07ef2006-01-08 01:00:56 -08001810static struct cftype cft_memory_migrate = {
1811 .name = "memory_migrate",
1812 .private = FILE_MEMORY_MIGRATE,
1813};
1814
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001815static struct cftype cft_memory_pressure_enabled = {
1816 .name = "memory_pressure_enabled",
1817 .private = FILE_MEMORY_PRESSURE_ENABLED,
1818};
1819
1820static struct cftype cft_memory_pressure = {
1821 .name = "memory_pressure",
1822 .private = FILE_MEMORY_PRESSURE,
1823};
1824
Paul Jackson825a46a2006-03-24 03:16:03 -08001825static struct cftype cft_spread_page = {
1826 .name = "memory_spread_page",
1827 .private = FILE_SPREAD_PAGE,
1828};
1829
1830static struct cftype cft_spread_slab = {
1831 .name = "memory_spread_slab",
1832 .private = FILE_SPREAD_SLAB,
1833};
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static int cpuset_populate_dir(struct dentry *cs_dentry)
1836{
1837 int err;
1838
1839 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1840 return err;
1841 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1842 return err;
1843 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1844 return err;
1845 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1846 return err;
1847 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1848 return err;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001849 if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
1850 return err;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001851 if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
1852 return err;
Paul Jackson825a46a2006-03-24 03:16:03 -08001853 if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0)
1854 return err;
1855 if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0)
1856 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1858 return err;
1859 return 0;
1860}
1861
1862/*
1863 * cpuset_create - create a cpuset
1864 * parent: cpuset that will be parent of the new cpuset.
1865 * name: name of the new cpuset. Will be strcpy'ed.
1866 * mode: mode to set on new inode
1867 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001868 * Must be called with the mutex on the parent inode held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 */
1870
1871static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1872{
1873 struct cpuset *cs;
1874 int err;
1875
1876 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1877 if (!cs)
1878 return -ENOMEM;
1879
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001880 mutex_lock(&manage_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001881 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 cs->flags = 0;
1883 if (notify_on_release(parent))
1884 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
Paul Jackson825a46a2006-03-24 03:16:03 -08001885 if (is_spread_page(parent))
1886 set_bit(CS_SPREAD_PAGE, &cs->flags);
1887 if (is_spread_slab(parent))
1888 set_bit(CS_SPREAD_SLAB, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 cs->cpus_allowed = CPU_MASK_NONE;
1890 cs->mems_allowed = NODE_MASK_NONE;
1891 atomic_set(&cs->count, 0);
1892 INIT_LIST_HEAD(&cs->sibling);
1893 INIT_LIST_HEAD(&cs->children);
Paul Jackson151a4422006-03-24 03:16:11 -08001894 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001895 fmeter_init(&cs->fmeter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 cs->parent = parent;
1898
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001899 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 list_add(&cs->sibling, &cs->parent->children);
Paul Jackson202f72d2006-01-08 01:01:57 -08001901 number_of_cpusets++;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001902 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 err = cpuset_create_dir(cs, name, mode);
1905 if (err < 0)
1906 goto err;
1907
1908 /*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001909 * Release manage_mutex before cpuset_populate_dir() because it
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001910 * will down() this new directory's i_mutex and if we race with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 * another mkdir, we might deadlock.
1912 */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001913 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 err = cpuset_populate_dir(cs->dentry);
1916 /* If err < 0, we have a half-filled directory - oh well ;) */
1917 return 0;
1918err:
1919 list_del(&cs->sibling);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001920 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 kfree(cs);
1922 return err;
1923}
1924
1925static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1926{
1927 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1928
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001929 /* the vfs holds inode->i_mutex already */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1931}
1932
Paul Jacksonabb5a5c2006-07-23 11:36:08 -07001933/*
1934 * Locking note on the strange update_flag() call below:
1935 *
1936 * If the cpuset being removed is marked cpu_exclusive, then simulate
1937 * turning cpu_exclusive off, which will call update_cpu_domains().
1938 * The lock_cpu_hotplug() call in update_cpu_domains() must not be
1939 * made while holding callback_mutex. Elsewhere the kernel nests
1940 * callback_mutex inside lock_cpu_hotplug() calls. So the reverse
1941 * nesting would risk an ABBA deadlock.
1942 */
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1945{
1946 struct cpuset *cs = dentry->d_fsdata;
1947 struct dentry *d;
1948 struct cpuset *parent;
Paul Jackson3077a262005-08-09 10:07:59 -07001949 char *pathbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001951 /* the vfs holds both inode->i_mutex already */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001953 mutex_lock(&manage_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001954 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (atomic_read(&cs->count) > 0) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001956 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -EBUSY;
1958 }
1959 if (!list_empty(&cs->children)) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001960 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -EBUSY;
1962 }
Paul Jacksonabb5a5c2006-07-23 11:36:08 -07001963 if (is_cpu_exclusive(cs)) {
1964 int retval = update_flag(CS_CPU_EXCLUSIVE, cs, "0");
1965 if (retval < 0) {
1966 mutex_unlock(&manage_mutex);
1967 return retval;
1968 }
1969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 parent = cs->parent;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001971 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 set_bit(CS_REMOVED, &cs->flags);
1973 list_del(&cs->sibling); /* delete my sibling from parent->children */
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001974 spin_lock(&cs->dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 d = dget(cs->dentry);
1976 cs->dentry = NULL;
1977 spin_unlock(&d->d_lock);
1978 cpuset_d_remove_dir(d);
1979 dput(d);
Paul Jackson202f72d2006-01-08 01:01:57 -08001980 number_of_cpusets--;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001981 mutex_unlock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08001982 if (list_empty(&parent->children))
1983 check_for_release(parent, &pathbuf);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001984 mutex_unlock(&manage_mutex);
Paul Jackson3077a262005-08-09 10:07:59 -07001985 cpuset_release_agent(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
1987}
1988
Paul Jacksonc417f022006-01-08 01:02:01 -08001989/*
1990 * cpuset_init_early - just enough so that the calls to
1991 * cpuset_update_task_memory_state() in early init code
1992 * are harmless.
1993 */
1994
1995int __init cpuset_init_early(void)
1996{
1997 struct task_struct *tsk = current;
1998
1999 tsk->cpuset = &top_cpuset;
Paul Jackson151a4422006-03-24 03:16:11 -08002000 tsk->cpuset->mems_generation = cpuset_mems_generation++;
Paul Jacksonc417f022006-01-08 01:02:01 -08002001 return 0;
2002}
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/**
2005 * cpuset_init - initialize cpusets at system boot
2006 *
2007 * Description: Initialize top_cpuset and the cpuset internal file system,
2008 **/
2009
2010int __init cpuset_init(void)
2011{
2012 struct dentry *root;
2013 int err;
2014
2015 top_cpuset.cpus_allowed = CPU_MASK_ALL;
2016 top_cpuset.mems_allowed = NODE_MASK_ALL;
2017
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002018 fmeter_init(&top_cpuset.fmeter);
Paul Jackson151a4422006-03-24 03:16:11 -08002019 top_cpuset.mems_generation = cpuset_mems_generation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 init_task.cpuset = &top_cpuset;
2022
2023 err = register_filesystem(&cpuset_fs_type);
2024 if (err < 0)
2025 goto out;
2026 cpuset_mount = kern_mount(&cpuset_fs_type);
2027 if (IS_ERR(cpuset_mount)) {
2028 printk(KERN_ERR "cpuset: could not mount!\n");
2029 err = PTR_ERR(cpuset_mount);
2030 cpuset_mount = NULL;
2031 goto out;
2032 }
2033 root = cpuset_mount->mnt_sb->s_root;
2034 root->d_fsdata = &top_cpuset;
Dave Hansend8c76e62006-09-30 23:29:04 -07002035 inc_nlink(root->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 top_cpuset.dentry = root;
2037 root->d_inode->i_op = &cpuset_dir_inode_operations;
Paul Jackson202f72d2006-01-08 01:01:57 -08002038 number_of_cpusets = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 err = cpuset_populate_dir(root);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002040 /* memory_pressure_enabled is in root cpuset only */
2041 if (err == 0)
2042 err = cpuset_add_file(root, &cft_memory_pressure_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043out:
2044 return err;
2045}
2046
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002047#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_MEMORY_HOTPLUG)
2048/*
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.
2056 *
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.
2063 *
2064 * Call with both manage_mutex and callback_mutex held.
2065 *
2066 * Recursive, on depth of cpuset subtree.
2067 */
2068
2069static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
2070{
2071 struct cpuset *c;
2072
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);
2080 }
2081}
2082
2083/*
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.
2087 *
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().
2092 *
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.
2097 */
2098
2099static void common_cpu_mem_hotplug_unplug(void)
2100{
2101 mutex_lock(&manage_mutex);
2102 mutex_lock(&callback_mutex);
2103
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;
2107
2108 mutex_unlock(&callback_mutex);
2109 mutex_unlock(&manage_mutex);
2110}
2111#endif
2112
2113#ifdef CONFIG_HOTPLUG_CPU
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002114/*
2115 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2116 * period. This is necessary in order to make cpusets transparent
2117 * (of no affect) on systems that are actively using CPU hotplug
2118 * but making no active use of cpusets.
2119 *
Paul Jackson38837fc2006-09-29 02:01:16 -07002120 * This routine ensures that top_cpuset.cpus_allowed tracks
2121 * cpu_online_map on each CPU hotplug (cpuhp) event.
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002122 */
2123
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002124static int cpuset_handle_cpuhp(struct notifier_block *nb,
2125 unsigned long phase, void *cpu)
2126{
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002127 common_cpu_mem_hotplug_unplug();
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002128 return 0;
2129}
2130#endif
2131
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002132#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002133/*
2134 * Keep top_cpuset.mems_allowed tracking node_online_map.
2135 * Call this routine anytime after you change node_online_map.
2136 * See also the previous routine cpuset_handle_cpuhp().
2137 */
2138
Al Viro1af98922006-10-10 22:48:57 +01002139void cpuset_track_online_nodes(void)
Paul Jackson38837fc2006-09-29 02:01:16 -07002140{
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002141 common_cpu_mem_hotplug_unplug();
Paul Jackson38837fc2006-09-29 02:01:16 -07002142}
2143#endif
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/**
2146 * cpuset_init_smp - initialize cpus_allowed
2147 *
2148 * Description: Finish top cpuset after cpu, node maps are initialized
2149 **/
2150
2151void __init cpuset_init_smp(void)
2152{
2153 top_cpuset.cpus_allowed = cpu_online_map;
2154 top_cpuset.mems_allowed = node_online_map;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002155
2156 hotcpu_notifier(cpuset_handle_cpuhp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159/**
2160 * cpuset_fork - attach newly forked task to its parents cpuset.
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002161 * @tsk: pointer to task_struct of forking parent process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 *
Paul Jackson053199e2005-10-30 15:02:30 -08002163 * Description: A task inherits its parent's cpuset at fork().
2164 *
2165 * A pointer to the shared cpuset was automatically copied in fork.c
2166 * by dup_task_struct(). However, we ignore that copy, since it was
2167 * not made under the protection of task_lock(), so might no longer be
2168 * a valid cpuset pointer. attach_task() might have already changed
2169 * current->cpuset, allowing the previously referenced cpuset to
2170 * be removed and freed. Instead, we task_lock(current) and copy
2171 * its present value of current->cpuset for our freshly forked child.
2172 *
2173 * At the point that cpuset_fork() is called, 'current' is the parent
2174 * task, and the passed argument 'child' points to the child task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 **/
2176
Paul Jackson053199e2005-10-30 15:02:30 -08002177void cpuset_fork(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Paul Jackson053199e2005-10-30 15:02:30 -08002179 task_lock(current);
2180 child->cpuset = current->cpuset;
2181 atomic_inc(&child->cpuset->count);
2182 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185/**
2186 * cpuset_exit - detach cpuset from exiting task
2187 * @tsk: pointer to task_struct of exiting process
2188 *
2189 * Description: Detach cpuset from @tsk and release it.
2190 *
Paul Jackson053199e2005-10-30 15:02:30 -08002191 * Note that cpusets marked notify_on_release force every task in
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002192 * them to take the global manage_mutex mutex when exiting.
Paul Jackson053199e2005-10-30 15:02:30 -08002193 * This could impact scaling on very large systems. Be reluctant to
2194 * use notify_on_release cpusets where very high task exit scaling
2195 * is required on large systems.
Paul Jackson2efe86b2005-05-27 02:02:43 -07002196 *
Paul Jackson053199e2005-10-30 15:02:30 -08002197 * Don't even think about derefencing 'cs' after the cpuset use count
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002198 * goes to zero, except inside a critical section guarded by manage_mutex
2199 * or callback_mutex. Otherwise a zero cpuset use count is a license to
Paul Jackson053199e2005-10-30 15:02:30 -08002200 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
2201 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002202 * This routine has to take manage_mutex, not callback_mutex, because
2203 * it is holding that mutex while calling check_for_release(),
2204 * which calls kmalloc(), so can't be called holding callback_mutex().
Paul Jackson053199e2005-10-30 15:02:30 -08002205 *
2206 * We don't need to task_lock() this reference to tsk->cpuset,
2207 * because tsk is already marked PF_EXITING, so attach_task() won't
Paul Jacksonb4b26412006-01-08 01:01:53 -08002208 * mess with it, or task is a failed fork, never visible to attach_task.
Paul Jackson06fed332006-02-15 15:17:38 -08002209 *
Paul Jackson8488bc32006-03-24 03:16:10 -08002210 * the_top_cpuset_hack:
Paul Jackson06fed332006-02-15 15:17:38 -08002211 *
2212 * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
2213 *
2214 * Don't leave a task unable to allocate memory, as that is an
2215 * accident waiting to happen should someone add a callout in
2216 * do_exit() after the cpuset_exit() call that might allocate.
2217 * If a task tries to allocate memory with an invalid cpuset,
2218 * it will oops in cpuset_update_task_memory_state().
2219 *
2220 * We call cpuset_exit() while the task is still competent to
2221 * handle notify_on_release(), then leave the task attached to
2222 * the root cpuset (top_cpuset) for the remainder of its exit.
2223 *
2224 * To do this properly, we would increment the reference count on
2225 * top_cpuset, and near the very end of the kernel/exit.c do_exit()
2226 * code we would add a second cpuset function call, to drop that
2227 * reference. This would just create an unnecessary hot spot on
2228 * the top_cpuset reference count, to no avail.
2229 *
2230 * Normally, holding a reference to a cpuset without bumping its
2231 * count is unsafe. The cpuset could go away, or someone could
2232 * attach us to a different cpuset, decrementing the count on
2233 * the first cpuset that we never incremented. But in this case,
2234 * top_cpuset isn't going away, and either task has PF_EXITING set,
2235 * which wards off any attach_task() attempts, or task is a failed
2236 * fork, never visible to attach_task.
2237 *
2238 * Another way to do this would be to set the cpuset pointer
2239 * to NULL here, and check in cpuset_update_task_memory_state()
2240 * for a NULL pointer. This hack avoids that NULL check, for no
2241 * cost (other than this way too long comment ;).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 **/
2243
2244void cpuset_exit(struct task_struct *tsk)
2245{
2246 struct cpuset *cs;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 cs = tsk->cpuset;
Paul Jackson8488bc32006-03-24 03:16:10 -08002249 tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Paul Jackson2efe86b2005-05-27 02:02:43 -07002251 if (notify_on_release(cs)) {
Paul Jackson3077a262005-08-09 10:07:59 -07002252 char *pathbuf = NULL;
2253
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002254 mutex_lock(&manage_mutex);
Paul Jackson2efe86b2005-05-27 02:02:43 -07002255 if (atomic_dec_and_test(&cs->count))
Paul Jackson3077a262005-08-09 10:07:59 -07002256 check_for_release(cs, &pathbuf);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002257 mutex_unlock(&manage_mutex);
Paul Jackson3077a262005-08-09 10:07:59 -07002258 cpuset_release_agent(pathbuf);
Paul Jackson2efe86b2005-05-27 02:02:43 -07002259 } else {
2260 atomic_dec(&cs->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262}
2263
2264/**
2265 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2266 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2267 *
2268 * Description: Returns the cpumask_t cpus_allowed of the cpuset
2269 * attached to the specified @tsk. Guaranteed to return some non-empty
2270 * subset of cpu_online_map, even if this means going outside the
2271 * tasks cpuset.
2272 **/
2273
Paul Jackson909d75a2006-01-08 01:01:55 -08002274cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 cpumask_t mask;
2277
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002278 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002279 task_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 guarantee_online_cpus(tsk->cpuset, &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002281 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002282 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 return mask;
2285}
2286
2287void cpuset_init_current_mems_allowed(void)
2288{
2289 current->mems_allowed = NODE_MASK_ALL;
2290}
2291
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002292/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002293 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2294 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2295 *
2296 * Description: Returns the nodemask_t mems_allowed of the cpuset
2297 * attached to the specified @tsk. Guaranteed to return some non-empty
2298 * subset of node_online_map, even if this means going outside the
2299 * tasks cpuset.
2300 **/
2301
2302nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2303{
2304 nodemask_t mask;
2305
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002306 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002307 task_lock(tsk);
2308 guarantee_online_mems(tsk->cpuset, &mask);
2309 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002310 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002311
2312 return mask;
2313}
2314
2315/**
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002316 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2317 * @zl: the zonelist to be checked
2318 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2320 */
2321int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
2322{
2323 int i;
2324
2325 for (i = 0; zl->zones[i]; i++) {
Christoph Lameter89fa3022006-09-25 23:31:55 -07002326 int nid = zone_to_nid(zl->zones[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (node_isset(nid, current->mems_allowed))
2329 return 1;
2330 }
2331 return 0;
2332}
2333
Paul Jackson9bf22292005-09-06 15:18:12 -07002334/*
2335 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002336 * ancestor to the specified cpuset. Call holding callback_mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002337 * If no ancestor is mem_exclusive (an unusual configuration), then
2338 * returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 */
Paul Jackson9bf22292005-09-06 15:18:12 -07002340static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Paul Jackson9bf22292005-09-06 15:18:12 -07002342 while (!is_mem_exclusive(cs) && cs->parent)
2343 cs = cs->parent;
2344 return cs;
2345}
2346
2347/**
2348 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
2349 * @z: is this zone on an allowed node?
2350 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
2351 *
2352 * If we're in interrupt, yes, we can always allocate. If zone
2353 * z's node is in our tasks mems_allowed, yes. If it's not a
2354 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2355 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
2356 * Otherwise, no.
2357 *
2358 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2359 * and do not allow allocations outside the current tasks cpuset.
2360 * GFP_KERNEL allocations are not so marked, so can escape to the
2361 * nearest mem_exclusive ancestor cpuset.
2362 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002363 * Scanning up parent cpusets requires callback_mutex. The __alloc_pages()
Paul Jackson9bf22292005-09-06 15:18:12 -07002364 * routine only calls here with __GFP_HARDWALL bit _not_ set if
2365 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
2366 * mems_allowed came up empty on the first pass over the zonelist.
2367 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002368 * short of memory, might require taking the callback_mutex mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002369 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002370 * The first call here from mm/page_alloc:get_page_from_freelist()
2371 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, so
2372 * no allocation on a node outside the cpuset is allowed (unless in
2373 * interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002374 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002375 * The second pass through get_page_from_freelist() doesn't even call
2376 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2377 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2378 * in alloc_flags. That logic and the checks below have the combined
2379 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002380 * in_interrupt - any node ok (current task context irrelevant)
2381 * GFP_ATOMIC - any node ok
2382 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2383 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002384 *
2385 * Rule:
2386 * Don't call cpuset_zone_allowed() if you can't sleep, unless you
2387 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2388 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson9bf22292005-09-06 15:18:12 -07002389 **/
2390
Paul Jackson202f72d2006-01-08 01:01:57 -08002391int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002392{
2393 int node; /* node that zone z is on */
2394 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002395 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002396
Christoph Lameter9b819d22006-09-25 23:31:40 -07002397 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002398 return 1;
Christoph Lameter89fa3022006-09-25 23:31:55 -07002399 node = zone_to_nid(z);
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002400 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002401 if (node_isset(node, current->mems_allowed))
2402 return 1;
2403 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2404 return 0;
2405
Bob Picco5563e772005-11-13 16:06:35 -08002406 if (current->flags & PF_EXITING) /* Let dying task have memory */
2407 return 1;
2408
Paul Jackson9bf22292005-09-06 15:18:12 -07002409 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002410 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002411
Paul Jackson053199e2005-10-30 15:02:30 -08002412 task_lock(current);
2413 cs = nearest_exclusive_ancestor(current->cpuset);
2414 task_unlock(current);
2415
Paul Jackson9bf22292005-09-06 15:18:12 -07002416 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002417 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002418 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002421/**
Paul Jackson505970b2006-01-14 13:21:06 -08002422 * cpuset_lock - lock out any changes to cpuset structures
2423 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002424 * The out of memory (oom) code needs to mutex_lock cpusets
Paul Jackson505970b2006-01-14 13:21:06 -08002425 * from being changed while it scans the tasklist looking for a
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002426 * task in an overlapping cpuset. Expose callback_mutex via this
Paul Jackson505970b2006-01-14 13:21:06 -08002427 * cpuset_lock() routine, so the oom code can lock it, before
2428 * locking the task list. The tasklist_lock is a spinlock, so
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002429 * must be taken inside callback_mutex.
Paul Jackson505970b2006-01-14 13:21:06 -08002430 */
2431
2432void cpuset_lock(void)
2433{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002434 mutex_lock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002435}
2436
2437/**
2438 * cpuset_unlock - release lock on cpuset changes
2439 *
2440 * Undo the lock taken in a previous cpuset_lock() call.
2441 */
2442
2443void cpuset_unlock(void)
2444{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002445 mutex_unlock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002446}
2447
2448/**
Paul Jackson825a46a2006-03-24 03:16:03 -08002449 * cpuset_mem_spread_node() - On which node to begin search for a page
2450 *
2451 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2452 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2453 * and if the memory allocation used cpuset_mem_spread_node()
2454 * to determine on which node to start looking, as it will for
2455 * certain page cache or slab cache pages such as used for file
2456 * system buffers and inode caches, then instead of starting on the
2457 * local node to look for a free page, rather spread the starting
2458 * node around the tasks mems_allowed nodes.
2459 *
2460 * We don't have to worry about the returned node being offline
2461 * because "it can't happen", and even if it did, it would be ok.
2462 *
2463 * The routines calling guarantee_online_mems() are careful to
2464 * only set nodes in task->mems_allowed that are online. So it
2465 * should not be possible for the following code to return an
2466 * offline node. But if it did, that would be ok, as this routine
2467 * is not returning the node where the allocation must be, only
2468 * the node where the search should start. The zonelist passed to
2469 * __alloc_pages() will include all nodes. If the slab allocator
2470 * is passed an offline node, it will fall back to the local node.
2471 * See kmem_cache_alloc_node().
2472 */
2473
2474int cpuset_mem_spread_node(void)
2475{
2476 int node;
2477
2478 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2479 if (node == MAX_NUMNODES)
2480 node = first_node(current->mems_allowed);
2481 current->cpuset_mem_spread_rotor = node;
2482 return node;
2483}
2484EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2485
2486/**
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002487 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2488 * @p: pointer to task_struct of some other task.
2489 *
2490 * Description: Return true if the nearest mem_exclusive ancestor
2491 * cpusets of tasks @p and current overlap. Used by oom killer to
2492 * determine if task @p's memory usage might impact the memory
2493 * available to the current task.
2494 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002495 * Call while holding callback_mutex.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002496 **/
2497
2498int cpuset_excl_nodes_overlap(const struct task_struct *p)
2499{
2500 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
Nick Piggin0d673a52006-08-27 01:23:54 -07002501 int overlap = 1; /* do cpusets overlap? */
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002502
Paul Jackson053199e2005-10-30 15:02:30 -08002503 task_lock(current);
2504 if (current->flags & PF_EXITING) {
2505 task_unlock(current);
2506 goto done;
2507 }
2508 cs1 = nearest_exclusive_ancestor(current->cpuset);
2509 task_unlock(current);
2510
2511 task_lock((struct task_struct *)p);
2512 if (p->flags & PF_EXITING) {
2513 task_unlock((struct task_struct *)p);
2514 goto done;
2515 }
2516 cs2 = nearest_exclusive_ancestor(p->cpuset);
2517 task_unlock((struct task_struct *)p);
2518
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002519 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
2520done:
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002521 return overlap;
2522}
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002525 * Collection of memory_pressure is suppressed unless
2526 * this flag is enabled by writing "1" to the special
2527 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2528 */
2529
Paul Jacksonc5b2aff82006-01-08 01:01:51 -08002530int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002531
2532/**
2533 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2534 *
2535 * Keep a running average of the rate of synchronous (direct)
2536 * page reclaim efforts initiated by tasks in each cpuset.
2537 *
2538 * This represents the rate at which some task in the cpuset
2539 * ran low on memory on all nodes it was allowed to use, and
2540 * had to enter the kernels page reclaim code in an effort to
2541 * create more free memory by tossing clean pages or swapping
2542 * or writing dirty pages.
2543 *
2544 * Display to user space in the per-cpuset read-only file
2545 * "memory_pressure". Value displayed is an integer
2546 * representing the recent rate of entry into the synchronous
2547 * (direct) page reclaim by any task attached to the cpuset.
2548 **/
2549
2550void __cpuset_memory_pressure_bump(void)
2551{
2552 struct cpuset *cs;
2553
2554 task_lock(current);
2555 cs = current->cpuset;
2556 fmeter_markevent(&cs->fmeter);
2557 task_unlock(current);
2558}
2559
2560/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 * proc_cpuset_show()
2562 * - Print tasks cpuset path into seq_file.
2563 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002564 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2565 * doesn't really matter if tsk->cpuset changes after we read it,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002566 * and we take manage_mutex, keeping attach_task() from changing it
Paul Jackson8488bc32006-03-24 03:16:10 -08002567 * anyway. No need to check that tsk->cpuset != NULL, thanks to
2568 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
2569 * cpuset to top_cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571static int proc_cpuset_show(struct seq_file *m, void *v)
2572{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002573 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct task_struct *tsk;
2575 char *buf;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002576 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Eric W. Biederman99f89552006-06-26 00:25:55 -07002578 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2580 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Eric W. Biederman99f89552006-06-26 00:25:55 -07002583 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002584 pid = m->private;
2585 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002586 if (!tsk)
2587 goto out_free;
2588
2589 retval = -EINVAL;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002590 mutex_lock(&manage_mutex);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002591
Paul Jackson8488bc32006-03-24 03:16:10 -08002592 retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002594 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 seq_puts(m, buf);
2596 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002597out_unlock:
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002598 mutex_unlock(&manage_mutex);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002599 put_task_struct(tsk);
2600out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002602out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return retval;
2604}
2605
2606static int cpuset_open(struct inode *inode, struct file *file)
2607{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002608 struct pid *pid = PROC_I(inode)->pid;
2609 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
2612struct file_operations proc_cpuset_operations = {
2613 .open = cpuset_open,
2614 .read = seq_read,
2615 .llseek = seq_lseek,
2616 .release = single_release,
2617};
2618
2619/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2620char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
2621{
2622 buffer += sprintf(buffer, "Cpus_allowed:\t");
2623 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
2624 buffer += sprintf(buffer, "\n");
2625 buffer += sprintf(buffer, "Mems_allowed:\t");
2626 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
2627 buffer += sprintf(buffer, "\n");
2628 return buffer;
2629}