blob: b602f73fb38d021133f0c8d3f7f0516dc78e61a7 [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
21#include <linux/config.h>
22#include <linux/cpu.h>
23#include <linux/cpumask.h>
24#include <linux/cpuset.h>
25#include <linux/err.h>
26#include <linux/errno.h>
27#include <linux/file.h>
28#include <linux/fs.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/kernel.h>
32#include <linux/kmod.h>
33#include <linux/list.h>
Paul Jackson68860ec2005-10-30 15:02:36 -080034#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/mm.h>
36#include <linux/module.h>
37#include <linux/mount.h>
38#include <linux/namei.h>
39#include <linux/pagemap.h>
40#include <linux/proc_fs.h>
Paul Jackson6b9c2602006-01-08 01:02:02 -080041#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/sched.h>
43#include <linux/seq_file.h>
David Quigley22fb52d2006-06-23 02:04:00 -070044#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/slab.h>
46#include <linux/smp_lock.h>
47#include <linux/spinlock.h>
48#include <linux/stat.h>
49#include <linux/string.h>
50#include <linux/time.h>
51#include <linux/backing-dev.h>
52#include <linux/sort.h>
53
54#include <asm/uaccess.h>
55#include <asm/atomic.h>
Ingo Molnar3d3f26a2006-03-23 03:00:18 -080056#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Paul Jacksonc5b2aff2006-01-08 01:01:51 -080058#define CPUSET_SUPER_MAGIC 0x27e0eb
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paul Jackson202f72d2006-01-08 01:01:57 -080060/*
61 * Tracks how many cpusets are currently defined in system.
62 * When there is only one cpuset (the root cpuset) we can
63 * short circuit some hooks.
64 */
Paul Jackson7edc5962006-01-08 01:02:03 -080065int number_of_cpusets __read_mostly;
Paul Jackson202f72d2006-01-08 01:01:57 -080066
Paul Jackson3e0d98b2006-01-08 01:01:49 -080067/* See "Frequency meter" comments, below. */
68
69struct fmeter {
70 int cnt; /* unprocessed events count */
71 int val; /* most recent output value */
72 time_t time; /* clock (secs) when val computed */
73 spinlock_t lock; /* guards read or write of above */
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct cpuset {
77 unsigned long flags; /* "unsigned long" so bitops work */
78 cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
79 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
80
Paul Jackson053199e2005-10-30 15:02:30 -080081 /*
82 * Count is atomic so can incr (fork) or decr (exit) without a lock.
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 atomic_t count; /* count tasks using this cpuset */
85
86 /*
87 * We link our 'sibling' struct into our parents 'children'.
88 * Our children link their 'sibling' into our 'children'.
89 */
90 struct list_head sibling; /* my parents children */
91 struct list_head children; /* my children */
92
93 struct cpuset *parent; /* my parent */
94 struct dentry *dentry; /* cpuset fs entry */
95
96 /*
97 * Copy of global cpuset_mems_generation as of the most
98 * recent time this cpuset changed its mems_allowed.
99 */
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800100 int mems_generation;
101
102 struct fmeter fmeter; /* memory_pressure filter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105/* bits in struct cpuset flags field */
106typedef enum {
107 CS_CPU_EXCLUSIVE,
108 CS_MEM_EXCLUSIVE,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800109 CS_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 CS_REMOVED,
Paul Jackson825a46a2006-03-24 03:16:03 -0800111 CS_NOTIFY_ON_RELEASE,
112 CS_SPREAD_PAGE,
113 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114} cpuset_flagbits_t;
115
116/* convenient tests for these bits */
117static inline int is_cpu_exclusive(const struct cpuset *cs)
118{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800119 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static inline int is_mem_exclusive(const struct cpuset *cs)
123{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800124 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static inline int is_removed(const struct cpuset *cs)
128{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800129 return test_bit(CS_REMOVED, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static inline int notify_on_release(const struct cpuset *cs)
133{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800134 return test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Paul Jackson45b07ef2006-01-08 01:00:56 -0800137static inline int is_memory_migrate(const struct cpuset *cs)
138{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800139 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800140}
141
Paul Jackson825a46a2006-03-24 03:16:03 -0800142static inline int is_spread_page(const struct cpuset *cs)
143{
144 return test_bit(CS_SPREAD_PAGE, &cs->flags);
145}
146
147static inline int is_spread_slab(const struct cpuset *cs)
148{
149 return test_bit(CS_SPREAD_SLAB, &cs->flags);
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Paul Jackson151a4422006-03-24 03:16:11 -0800153 * Increment this integer everytime any cpuset changes its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * mems_allowed value. Users of cpusets can track this generation
155 * number, and avoid having to lock and reload mems_allowed unless
156 * the cpuset they're using changes generation.
157 *
158 * A single, global generation is needed because attach_task() could
159 * reattach a task to a different cpuset, which must not have its
160 * generation numbers aliased with those of that tasks previous cpuset.
161 *
162 * Generations are needed for mems_allowed because one task cannot
163 * modify anothers memory placement. So we must enable every task,
164 * on every visit to __alloc_pages(), to efficiently check whether
165 * its current->cpuset->mems_allowed has changed, requiring an update
166 * of its current->mems_allowed.
Paul Jackson151a4422006-03-24 03:16:11 -0800167 *
168 * Since cpuset_mems_generation is guarded by manage_mutex,
169 * there is no need to mark it atomic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Paul Jackson151a4422006-03-24 03:16:11 -0800171static int cpuset_mems_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static struct cpuset top_cpuset = {
174 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
175 .cpus_allowed = CPU_MASK_ALL,
176 .mems_allowed = NODE_MASK_ALL,
177 .count = ATOMIC_INIT(0),
178 .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
179 .children = LIST_HEAD_INIT(top_cpuset.children),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
182static struct vfsmount *cpuset_mount;
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800183static struct super_block *cpuset_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800186 * We have two global cpuset mutexes below. They can nest.
187 * It is ok to first take manage_mutex, then nest callback_mutex. We also
Paul Jackson053199e2005-10-30 15:02:30 -0800188 * require taking task_lock() when dereferencing a tasks cpuset pointer.
189 * See "The task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800191 * A task must hold both mutexes to modify cpusets. If a task
192 * holds manage_mutex, then it blocks others wanting that mutex,
193 * ensuring that it is the only task able to also acquire callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800194 * and be able to modify cpusets. It can perform various checks on
195 * the cpuset structure first, knowing nothing will change. It can
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800196 * also allocate memory while just holding manage_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800197 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800198 * acquire callback_mutex to query cpusets. Once it is ready to make
199 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Paul Jackson053199e2005-10-30 15:02:30 -0800201 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800202 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800203 * from one of the callbacks into the cpuset code from within
204 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800206 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800207 * access to cpusets.
208 *
209 * The task_struct fields mems_allowed and mems_generation may only
210 * be accessed in the context of that task, so require no locks.
211 *
212 * Any task can increment and decrement the count field without lock.
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800213 * So in general, code holding manage_mutex or callback_mutex can't rely
Paul Jackson053199e2005-10-30 15:02:30 -0800214 * on the count field not changing. However, if the count goes to
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800215 * zero, then only attach_task(), which holds both mutexes, can
Paul Jackson053199e2005-10-30 15:02:30 -0800216 * increment it again. Because a count of zero means that no tasks
217 * are currently attached, therefore there is no way a task attached
218 * to that cpuset can fork (the other way to increment the count).
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800219 * So code holding manage_mutex or callback_mutex can safely assume that
Paul Jackson053199e2005-10-30 15:02:30 -0800220 * if the count is zero, it will stay zero. Similarly, if a task
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800221 * holds manage_mutex or callback_mutex on a cpuset with zero count, it
Paul Jackson053199e2005-10-30 15:02:30 -0800222 * knows that the cpuset won't be removed, as cpuset_rmdir() needs
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800223 * both of those mutexes.
Paul Jackson053199e2005-10-30 15:02:30 -0800224 *
225 * The cpuset_common_file_write handler for operations that modify
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800226 * the cpuset hierarchy holds manage_mutex across the entire operation,
Paul Jackson053199e2005-10-30 15:02:30 -0800227 * single threading all such cpuset modifications across the system.
228 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800229 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800230 * small pieces of code, such as when reading out possibly multi-word
231 * cpumasks and nodemasks.
232 *
233 * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800234 * (usually) take either mutex. These are the two most performance
Paul Jackson053199e2005-10-30 15:02:30 -0800235 * critical pieces of code here. The exception occurs on cpuset_exit(),
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800236 * when a task in a notify_on_release cpuset exits. Then manage_mutex
Paul Jackson2efe86b2005-05-27 02:02:43 -0700237 * is taken, and if the cpuset count is zero, a usermode call made
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * to /sbin/cpuset_release_agent with the name of the cpuset (path
239 * relative to the root of cpuset file system) as the argument.
240 *
Paul Jackson053199e2005-10-30 15:02:30 -0800241 * A cpuset can only be deleted if both its 'count' of using tasks
242 * is zero, and its list of 'children' cpusets is empty. Since all
243 * tasks in the system use _some_ cpuset, and since there is always at
244 * least one task in the system (init, pid == 1), therefore, top_cpuset
245 * always has either children cpusets and/or using tasks. So we don't
246 * need a special hack to ensure that top_cpuset cannot be deleted.
247 *
248 * The above "Tale of Two Semaphores" would be complete, but for:
249 *
250 * The task_lock() exception
251 *
252 * The need for this exception arises from the action of attach_task(),
253 * which overwrites one tasks cpuset pointer with another. It does
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800254 * so using both mutexes, however there are several performance
Paul Jackson053199e2005-10-30 15:02:30 -0800255 * critical places that need to reference task->cpuset without the
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800256 * expense of grabbing a system global mutex. Therefore except as
Paul Jackson053199e2005-10-30 15:02:30 -0800257 * noted below, when dereferencing or, as in attach_task(), modifying
258 * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
259 * (task->alloc_lock) already in the task_struct routinely used for
260 * such matters.
Paul Jackson6b9c2602006-01-08 01:02:02 -0800261 *
262 * P.S. One more locking exception. RCU is used to guard the
263 * update of a tasks cpuset pointer by attach_task() and the
264 * access of task->cpuset->mems_generation via that pointer in
265 * the routine cpuset_update_task_memory_state().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
267
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800268static DEFINE_MUTEX(manage_mutex);
269static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700270
271/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * A couple of forward declarations required, due to cyclic reference loop:
273 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
274 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
275 */
276
277static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
278static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
279
280static struct backing_dev_info cpuset_backing_dev_info = {
281 .ra_pages = 0, /* No readahead */
282 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
283};
284
285static struct inode *cpuset_new_inode(mode_t mode)
286{
287 struct inode *inode = new_inode(cpuset_sb);
288
289 if (inode) {
290 inode->i_mode = mode;
291 inode->i_uid = current->fsuid;
292 inode->i_gid = current->fsgid;
293 inode->i_blksize = PAGE_CACHE_SIZE;
294 inode->i_blocks = 0;
295 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
296 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
297 }
298 return inode;
299}
300
301static void cpuset_diput(struct dentry *dentry, struct inode *inode)
302{
303 /* is dentry a directory ? if so, kfree() associated cpuset */
304 if (S_ISDIR(inode->i_mode)) {
305 struct cpuset *cs = dentry->d_fsdata;
306 BUG_ON(!(is_removed(cs)));
307 kfree(cs);
308 }
309 iput(inode);
310}
311
312static struct dentry_operations cpuset_dops = {
313 .d_iput = cpuset_diput,
314};
315
316static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
317{
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -0700318 struct dentry *d = lookup_one_len(name, parent, strlen(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!IS_ERR(d))
320 d->d_op = &cpuset_dops;
321 return d;
322}
323
324static void remove_dir(struct dentry *d)
325{
326 struct dentry *parent = dget(d->d_parent);
327
328 d_delete(d);
329 simple_rmdir(parent->d_inode, d);
330 dput(parent);
331}
332
333/*
334 * NOTE : the dentry must have been dget()'ed
335 */
336static void cpuset_d_remove_dir(struct dentry *dentry)
337{
338 struct list_head *node;
339
340 spin_lock(&dcache_lock);
341 node = dentry->d_subdirs.next;
342 while (node != &dentry->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800343 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 list_del_init(node);
345 if (d->d_inode) {
346 d = dget_locked(d);
347 spin_unlock(&dcache_lock);
348 d_delete(d);
349 simple_unlink(dentry->d_inode, d);
350 dput(d);
351 spin_lock(&dcache_lock);
352 }
353 node = dentry->d_subdirs.next;
354 }
Eric Dumazet5160ee62006-01-08 01:03:32 -0800355 list_del_init(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_unlock(&dcache_lock);
357 remove_dir(dentry);
358}
359
360static struct super_operations cpuset_ops = {
361 .statfs = simple_statfs,
362 .drop_inode = generic_delete_inode,
363};
364
365static int cpuset_fill_super(struct super_block *sb, void *unused_data,
366 int unused_silent)
367{
368 struct inode *inode;
369 struct dentry *root;
370
371 sb->s_blocksize = PAGE_CACHE_SIZE;
372 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
373 sb->s_magic = CPUSET_SUPER_MAGIC;
374 sb->s_op = &cpuset_ops;
375 cpuset_sb = sb;
376
377 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
378 if (inode) {
379 inode->i_op = &simple_dir_inode_operations;
380 inode->i_fop = &simple_dir_operations;
381 /* directories start off with i_nlink == 2 (for "." entry) */
382 inode->i_nlink++;
383 } else {
384 return -ENOMEM;
385 }
386
387 root = d_alloc_root(inode);
388 if (!root) {
389 iput(inode);
390 return -ENOMEM;
391 }
392 sb->s_root = root;
393 return 0;
394}
395
David Howells454e2392006-06-23 02:02:57 -0700396static int cpuset_get_sb(struct file_system_type *fs_type,
397 int flags, const char *unused_dev_name,
398 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
David Howells454e2392006-06-23 02:02:57 -0700400 return get_sb_single(fs_type, flags, data, cpuset_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403static struct file_system_type cpuset_fs_type = {
404 .name = "cpuset",
405 .get_sb = cpuset_get_sb,
406 .kill_sb = kill_litter_super,
407};
408
409/* struct cftype:
410 *
411 * The files in the cpuset filesystem mostly have a very simple read/write
412 * handling, some common function will take care of it. Nevertheless some cases
413 * (read tasks) are special and therefore I define this structure for every
414 * kind of file.
415 *
416 *
417 * When reading/writing to a file:
418 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
419 * - the 'cftype' of the file is file->f_dentry->d_fsdata
420 */
421
422struct cftype {
423 char *name;
424 int private;
425 int (*open) (struct inode *inode, struct file *file);
426 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
427 loff_t *ppos);
428 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
429 loff_t *ppos);
430 int (*release) (struct inode *inode, struct file *file);
431};
432
433static inline struct cpuset *__d_cs(struct dentry *dentry)
434{
435 return dentry->d_fsdata;
436}
437
438static inline struct cftype *__d_cft(struct dentry *dentry)
439{
440 return dentry->d_fsdata;
441}
442
443/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800444 * Call with manage_mutex held. Writes path of cpuset into buf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * Returns 0 on success, -errno on error.
446 */
447
448static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
449{
450 char *start;
451
452 start = buf + buflen;
453
454 *--start = '\0';
455 for (;;) {
456 int len = cs->dentry->d_name.len;
457 if ((start -= len) < buf)
458 return -ENAMETOOLONG;
459 memcpy(start, cs->dentry->d_name.name, len);
460 cs = cs->parent;
461 if (!cs)
462 break;
463 if (!cs->parent)
464 continue;
465 if (--start < buf)
466 return -ENAMETOOLONG;
467 *start = '/';
468 }
469 memmove(buf, start, buf + buflen - start);
470 return 0;
471}
472
473/*
474 * Notify userspace when a cpuset is released, by running
475 * /sbin/cpuset_release_agent with the name of the cpuset (path
476 * relative to the root of cpuset file system) as the argument.
477 *
478 * Most likely, this user command will try to rmdir this cpuset.
479 *
480 * This races with the possibility that some other task will be
481 * attached to this cpuset before it is removed, or that some other
482 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
483 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
484 * unused, and this cpuset will be reprieved from its death sentence,
485 * to continue to serve a useful existence. Next time it's released,
486 * we will get notified again, if it still has 'notify_on_release' set.
487 *
Paul Jackson3077a262005-08-09 10:07:59 -0700488 * The final arg to call_usermodehelper() is 0, which means don't
489 * wait. The separate /sbin/cpuset_release_agent task is forked by
490 * call_usermodehelper(), then control in this thread returns here,
491 * without waiting for the release agent task. We don't bother to
492 * wait because the caller of this routine has no use for the exit
493 * status of the /sbin/cpuset_release_agent task, so no sense holding
494 * our caller up for that.
495 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800496 * When we had only one cpuset mutex, we had to call this
Paul Jackson053199e2005-10-30 15:02:30 -0800497 * without holding it, to avoid deadlock when call_usermodehelper()
498 * allocated memory. With two locks, we could now call this while
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800499 * holding manage_mutex, but we still don't, so as to minimize
500 * the time manage_mutex is held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
502
Paul Jackson3077a262005-08-09 10:07:59 -0700503static void cpuset_release_agent(const char *pathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 char *argv[3], *envp[3];
506 int i;
507
Paul Jackson3077a262005-08-09 10:07:59 -0700508 if (!pathbuf)
509 return;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 i = 0;
512 argv[i++] = "/sbin/cpuset_release_agent";
Paul Jackson3077a262005-08-09 10:07:59 -0700513 argv[i++] = (char *)pathbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 argv[i] = NULL;
515
516 i = 0;
517 /* minimal command environment */
518 envp[i++] = "HOME=/";
519 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
520 envp[i] = NULL;
521
Paul Jackson3077a262005-08-09 10:07:59 -0700522 call_usermodehelper(argv[0], argv, envp, 0);
523 kfree(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/*
527 * Either cs->count of using tasks transitioned to zero, or the
528 * cs->children list of child cpusets just became empty. If this
529 * cs is notify_on_release() and now both the user count is zero and
Paul Jackson3077a262005-08-09 10:07:59 -0700530 * the list of children is empty, prepare cpuset path in a kmalloc'd
531 * buffer, to be returned via ppathbuf, so that the caller can invoke
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800532 * cpuset_release_agent() with it later on, once manage_mutex is dropped.
533 * Call here with manage_mutex held.
Paul Jackson3077a262005-08-09 10:07:59 -0700534 *
535 * This check_for_release() routine is responsible for kmalloc'ing
536 * pathbuf. The above cpuset_release_agent() is responsible for
537 * kfree'ing pathbuf. The caller of these routines is responsible
538 * for providing a pathbuf pointer, initialized to NULL, then
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800539 * calling check_for_release() with manage_mutex held and the address
540 * of the pathbuf pointer, then dropping manage_mutex, then calling
Paul Jackson3077a262005-08-09 10:07:59 -0700541 * cpuset_release_agent() with pathbuf, as set by check_for_release().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543
Paul Jackson3077a262005-08-09 10:07:59 -0700544static void check_for_release(struct cpuset *cs, char **ppathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
547 list_empty(&cs->children)) {
548 char *buf;
549
550 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
551 if (!buf)
552 return;
553 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
Paul Jackson3077a262005-08-09 10:07:59 -0700554 kfree(buf);
555 else
556 *ppathbuf = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558}
559
560/*
561 * Return in *pmask the portion of a cpusets's cpus_allowed that
562 * are online. If none are online, walk up the cpuset hierarchy
563 * until we find one that does have some online cpus. If we get
564 * all the way to the top and still haven't found any online cpus,
565 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
566 * task, return cpu_online_map.
567 *
568 * One way or another, we guarantee to return some non-empty subset
569 * of cpu_online_map.
570 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800571 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
573
574static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
575{
576 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
577 cs = cs->parent;
578 if (cs)
579 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
580 else
581 *pmask = cpu_online_map;
582 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
583}
584
585/*
586 * Return in *pmask the portion of a cpusets's mems_allowed that
587 * are online. If none are online, walk up the cpuset hierarchy
588 * until we find one that does have some online mems. If we get
589 * all the way to the top and still haven't found any online mems,
590 * return node_online_map.
591 *
592 * One way or another, we guarantee to return some non-empty subset
593 * of node_online_map.
594 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800595 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
597
598static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
599{
600 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
601 cs = cs->parent;
602 if (cs)
603 nodes_and(*pmask, cs->mems_allowed, node_online_map);
604 else
605 *pmask = node_online_map;
606 BUG_ON(!nodes_intersects(*pmask, node_online_map));
607}
608
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800609/**
610 * cpuset_update_task_memory_state - update task memory placement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800612 * If the current tasks cpusets mems_allowed changed behind our
613 * backs, update current->mems_allowed, mems_generation and task NUMA
614 * mempolicy to the new value.
615 *
616 * Task mempolicy is updated by rebinding it relative to the
617 * current->cpuset if a task has its memory placement changed.
618 * Do not call this routine if in_interrupt().
619 *
Paul Jackson4a01c8d2006-03-31 02:30:50 -0800620 * Call without callback_mutex or task_lock() held. May be
621 * called with or without manage_mutex held. Thanks in part to
622 * 'the_top_cpuset_hack', the tasks cpuset pointer will never
623 * be NULL. This routine also might acquire callback_mutex and
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800624 * current->mm->mmap_sem during call.
Paul Jackson5aa15b52005-10-30 15:02:28 -0800625 *
Paul Jackson6b9c2602006-01-08 01:02:02 -0800626 * Reading current->cpuset->mems_generation doesn't need task_lock
627 * to guard the current->cpuset derefence, because it is guarded
628 * from concurrent freeing of current->cpuset by attach_task(),
629 * using RCU.
630 *
631 * The rcu_dereference() is technically probably not needed,
632 * as I don't actually mind if I see a new cpuset pointer but
633 * an old value of mems_generation. However this really only
634 * matters on alpha systems using cpusets heavily. If I dropped
635 * that rcu_dereference(), it would save them a memory barrier.
636 * For all other arch's, rcu_dereference is a no-op anyway, and for
637 * alpha systems not using cpusets, another planned optimization,
638 * avoiding the rcu critical section for tasks in the root cpuset
639 * which is statically allocated, so can't vanish, will make this
640 * irrelevant. Better to use RCU as intended, than to engage in
641 * some cute trick to save a memory barrier that is impossible to
642 * test, for alpha systems using cpusets heavily, which might not
643 * even exist.
Paul Jackson053199e2005-10-30 15:02:30 -0800644 *
645 * This routine is needed to update the per-task mems_allowed data,
646 * within the tasks context, when it is trying to allocate memory
647 * (in various mm/mempolicy.c routines) and notices that some other
648 * task has been modifying its cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
650
Randy Dunlapfe85a992006-02-03 03:04:23 -0800651void cpuset_update_task_memory_state(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Paul Jackson053199e2005-10-30 15:02:30 -0800653 int my_cpusets_mem_gen;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800654 struct task_struct *tsk = current;
Paul Jackson6b9c2602006-01-08 01:02:02 -0800655 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Paul Jackson03a285f2006-01-08 01:02:04 -0800657 if (tsk->cpuset == &top_cpuset) {
658 /* Don't need rcu for top_cpuset. It's never freed. */
659 my_cpusets_mem_gen = top_cpuset.mems_generation;
660 } else {
661 rcu_read_lock();
662 cs = rcu_dereference(tsk->cpuset);
663 my_cpusets_mem_gen = cs->mems_generation;
664 rcu_read_unlock();
665 }
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800666
667 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800668 mutex_lock(&callback_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800669 task_lock(tsk);
670 cs = tsk->cpuset; /* Maybe changed when task not locked */
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800671 guarantee_online_mems(cs, &tsk->mems_allowed);
672 tsk->cpuset_mems_generation = cs->mems_generation;
Paul Jackson825a46a2006-03-24 03:16:03 -0800673 if (is_spread_page(cs))
674 tsk->flags |= PF_SPREAD_PAGE;
675 else
676 tsk->flags &= ~PF_SPREAD_PAGE;
677 if (is_spread_slab(cs))
678 tsk->flags |= PF_SPREAD_SLAB;
679 else
680 tsk->flags &= ~PF_SPREAD_SLAB;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800681 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800682 mutex_unlock(&callback_mutex);
Paul Jackson74cb2152006-01-08 01:01:56 -0800683 mpol_rebind_task(tsk, &tsk->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685}
686
687/*
688 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
689 *
690 * One cpuset is a subset of another if all its allowed CPUs and
691 * Memory Nodes are a subset of the other, and its exclusive flags
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800692 * are only set if the other's are set. Call holding manage_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694
695static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
696{
697 return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
698 nodes_subset(p->mems_allowed, q->mems_allowed) &&
699 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
700 is_mem_exclusive(p) <= is_mem_exclusive(q);
701}
702
703/*
704 * validate_change() - Used to validate that any proposed cpuset change
705 * follows the structural rules for cpusets.
706 *
707 * If we replaced the flag and mask values of the current cpuset
708 * (cur) with those values in the trial cpuset (trial), would
709 * our various subset and exclusive rules still be valid? Presumes
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800710 * manage_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
712 * 'cur' is the address of an actual, in-use cpuset. Operations
713 * such as list traversal that depend on the actual address of the
714 * cpuset in the list must use cur below, not trial.
715 *
716 * 'trial' is the address of bulk structure copy of cur, with
717 * perhaps one or more of the fields cpus_allowed, mems_allowed,
718 * or flags changed to new, trial values.
719 *
720 * Return 0 if valid, -errno if not.
721 */
722
723static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
724{
725 struct cpuset *c, *par;
726
727 /* Each of our child cpusets must be a subset of us */
728 list_for_each_entry(c, &cur->children, sibling) {
729 if (!is_cpuset_subset(c, trial))
730 return -EBUSY;
731 }
732
733 /* Remaining checks don't apply to root cpuset */
734 if ((par = cur->parent) == NULL)
735 return 0;
736
737 /* 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.
766 */
Paul Jackson212d6d22005-08-25 12:47:56 -0700767
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700768static void update_cpu_domains(struct cpuset *cur)
769{
770 struct cpuset *c, *par = cur->parent;
771 cpumask_t pspan, cspan;
772
773 if (par == NULL || cpus_empty(cur->cpus_allowed))
774 return;
775
776 /*
777 * Get all cpus from parent's cpus_allowed not part of exclusive
778 * children
779 */
780 pspan = par->cpus_allowed;
781 list_for_each_entry(c, &par->children, sibling) {
782 if (is_cpu_exclusive(c))
783 cpus_andnot(pspan, pspan, c->cpus_allowed);
784 }
785 if (is_removed(cur) || !is_cpu_exclusive(cur)) {
786 cpus_or(pspan, pspan, cur->cpus_allowed);
787 if (cpus_equal(pspan, cur->cpus_allowed))
788 return;
789 cspan = CPU_MASK_NONE;
790 } else {
791 if (cpus_empty(pspan))
792 return;
793 cspan = cur->cpus_allowed;
794 /*
795 * Get all cpus from current cpuset's cpus_allowed not part
796 * of exclusive children
797 */
798 list_for_each_entry(c, &cur->children, sibling) {
799 if (is_cpu_exclusive(c))
800 cpus_andnot(cspan, cspan, c->cpus_allowed);
801 }
802 }
803
804 lock_cpu_hotplug();
805 partition_sched_domains(&pspan, &cspan);
806 unlock_cpu_hotplug();
807}
808
Paul Jackson053199e2005-10-30 15:02:30 -0800809/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800810 * Call with manage_mutex held. May take callback_mutex during call.
Paul Jackson053199e2005-10-30 15:02:30 -0800811 */
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int update_cpumask(struct cpuset *cs, char *buf)
814{
815 struct cpuset trialcs;
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700816 int retval, cpus_unchanged;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 trialcs = *cs;
819 retval = cpulist_parse(buf, trialcs.cpus_allowed);
820 if (retval < 0)
821 return retval;
822 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
823 if (cpus_empty(trialcs.cpus_allowed))
824 return -ENOSPC;
825 retval = validate_change(cs, &trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700826 if (retval < 0)
827 return retval;
828 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800829 mutex_lock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700830 cs->cpus_allowed = trialcs.cpus_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800831 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700832 if (is_cpu_exclusive(cs) && !cpus_unchanged)
833 update_cpu_domains(cs);
834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Paul Jackson053199e2005-10-30 15:02:30 -0800837/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800838 * cpuset_migrate_mm
839 *
840 * Migrate memory region from one set of nodes to another.
841 *
842 * Temporarilly set tasks mems_allowed to target nodes of migration,
843 * so that the migration code can allocate pages on these nodes.
844 *
845 * Call holding manage_mutex, so our current->cpuset won't change
846 * during this call, as manage_mutex holds off any attach_task()
847 * calls. Therefore we don't need to take task_lock around the
848 * call to guarantee_online_mems(), as we know no one is changing
849 * our tasks cpuset.
850 *
851 * Hold callback_mutex around the two modifications of our tasks
852 * mems_allowed to synchronize with cpuset_mems_allowed().
853 *
854 * While the mm_struct we are migrating is typically from some
855 * other task, the task_struct mems_allowed that we are hacking
856 * is for our current task, which must allocate new pages for that
857 * migrating memory region.
858 *
859 * We call cpuset_update_task_memory_state() before hacking
860 * our tasks mems_allowed, so that we are assured of being in
861 * sync with our tasks cpuset, and in particular, callbacks to
862 * cpuset_update_task_memory_state() from nested page allocations
863 * won't see any mismatch of our cpuset and task mems_generation
864 * values, so won't overwrite our hacked tasks mems_allowed
865 * nodemask.
866 */
867
868static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
869 const nodemask_t *to)
870{
871 struct task_struct *tsk = current;
872
873 cpuset_update_task_memory_state();
874
875 mutex_lock(&callback_mutex);
876 tsk->mems_allowed = *to;
877 mutex_unlock(&callback_mutex);
878
879 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
880
881 mutex_lock(&callback_mutex);
882 guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed);
883 mutex_unlock(&callback_mutex);
884}
885
886/*
Paul Jackson42253992006-01-08 01:01:59 -0800887 * Handle user request to change the 'mems' memory placement
888 * of a cpuset. Needs to validate the request, update the
889 * cpusets mems_allowed and mems_generation, and for each
Paul Jackson04c19fa2006-01-08 01:02:00 -0800890 * task in the cpuset, rebind any vma mempolicies and if
891 * the cpuset is marked 'memory_migrate', migrate the tasks
892 * pages to the new memory.
Paul Jackson42253992006-01-08 01:01:59 -0800893 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800894 * Call with manage_mutex held. May take callback_mutex during call.
Paul Jackson42253992006-01-08 01:01:59 -0800895 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
896 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
897 * their mempolicies to the cpusets new mems_allowed.
Paul Jackson053199e2005-10-30 15:02:30 -0800898 */
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static int update_nodemask(struct cpuset *cs, char *buf)
901{
902 struct cpuset trialcs;
Paul Jackson04c19fa2006-01-08 01:02:00 -0800903 nodemask_t oldmem;
Paul Jackson42253992006-01-08 01:01:59 -0800904 struct task_struct *g, *p;
905 struct mm_struct **mmarray;
906 int i, n, ntasks;
Paul Jackson04c19fa2006-01-08 01:02:00 -0800907 int migrate;
Paul Jackson42253992006-01-08 01:01:59 -0800908 int fudge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int retval;
910
911 trialcs = *cs;
912 retval = nodelist_parse(buf, trialcs.mems_allowed);
913 if (retval < 0)
Paul Jackson59dac162006-01-08 01:01:52 -0800914 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
Paul Jackson04c19fa2006-01-08 01:02:00 -0800916 oldmem = cs->mems_allowed;
917 if (nodes_equal(oldmem, trialcs.mems_allowed)) {
918 retval = 0; /* Too easy - nothing to do */
919 goto done;
920 }
Paul Jackson59dac162006-01-08 01:01:52 -0800921 if (nodes_empty(trialcs.mems_allowed)) {
922 retval = -ENOSPC;
923 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Paul Jackson59dac162006-01-08 01:01:52 -0800925 retval = validate_change(cs, &trialcs);
926 if (retval < 0)
927 goto done;
928
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800929 mutex_lock(&callback_mutex);
Paul Jackson59dac162006-01-08 01:01:52 -0800930 cs->mems_allowed = trialcs.mems_allowed;
Paul Jackson151a4422006-03-24 03:16:11 -0800931 cs->mems_generation = cpuset_mems_generation++;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800932 mutex_unlock(&callback_mutex);
Paul Jackson59dac162006-01-08 01:01:52 -0800933
Paul Jackson42253992006-01-08 01:01:59 -0800934 set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
935
936 fudge = 10; /* spare mmarray[] slots */
937 fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
938 retval = -ENOMEM;
939
940 /*
941 * Allocate mmarray[] to hold mm reference for each task
942 * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
943 * tasklist_lock. We could use GFP_ATOMIC, but with a
944 * few more lines of code, we can retry until we get a big
945 * enough mmarray[] w/o using GFP_ATOMIC.
946 */
947 while (1) {
948 ntasks = atomic_read(&cs->count); /* guess */
949 ntasks += fudge;
950 mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
951 if (!mmarray)
952 goto done;
953 write_lock_irq(&tasklist_lock); /* block fork */
954 if (atomic_read(&cs->count) <= ntasks)
955 break; /* got enough */
956 write_unlock_irq(&tasklist_lock); /* try again */
957 kfree(mmarray);
958 }
959
960 n = 0;
961
962 /* Load up mmarray[] with mm reference for each task in cpuset. */
963 do_each_thread(g, p) {
964 struct mm_struct *mm;
965
966 if (n >= ntasks) {
967 printk(KERN_WARNING
968 "Cpuset mempolicy rebind incomplete.\n");
969 continue;
970 }
971 if (p->cpuset != cs)
972 continue;
973 mm = get_task_mm(p);
974 if (!mm)
975 continue;
976 mmarray[n++] = mm;
977 } while_each_thread(g, p);
978 write_unlock_irq(&tasklist_lock);
979
980 /*
981 * Now that we've dropped the tasklist spinlock, we can
982 * rebind the vma mempolicies of each mm in mmarray[] to their
983 * new cpuset, and release that mm. The mpol_rebind_mm()
984 * call takes mmap_sem, which we couldn't take while holding
985 * tasklist_lock. Forks can happen again now - the mpol_copy()
986 * cpuset_being_rebound check will catch such forks, and rebind
987 * their vma mempolicies too. Because we still hold the global
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800988 * cpuset manage_mutex, we know that no other rebind effort will
Paul Jackson42253992006-01-08 01:01:59 -0800989 * be contending for the global variable cpuset_being_rebound.
990 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -0800991 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -0800992 */
Paul Jackson04c19fa2006-01-08 01:02:00 -0800993 migrate = is_memory_migrate(cs);
Paul Jackson42253992006-01-08 01:01:59 -0800994 for (i = 0; i < n; i++) {
995 struct mm_struct *mm = mmarray[i];
996
997 mpol_rebind_mm(mm, &cs->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -0800998 if (migrate)
999 cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
Paul Jackson42253992006-01-08 01:01:59 -08001000 mmput(mm);
1001 }
1002
1003 /* We're done rebinding vma's to this cpusets new mems_allowed. */
1004 kfree(mmarray);
1005 set_cpuset_being_rebound(NULL);
1006 retval = 0;
Paul Jackson59dac162006-01-08 01:01:52 -08001007done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return retval;
1009}
1010
1011/*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001012 * Call with manage_mutex held.
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001013 */
1014
1015static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
1016{
1017 if (simple_strtoul(buf, NULL, 10) != 0)
1018 cpuset_memory_pressure_enabled = 1;
1019 else
1020 cpuset_memory_pressure_enabled = 0;
1021 return 0;
1022}
1023
1024/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * update_flag - read a 0 or a 1 in a file and update associated flag
1026 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001027 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
1028 * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * cs: the cpuset to update
1030 * buf: the buffer where we read the 0 or 1
Paul Jackson053199e2005-10-30 15:02:30 -08001031 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001032 * Call with manage_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034
1035static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
1036{
1037 int turning_on;
1038 struct cpuset trialcs;
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001039 int err, cpu_exclusive_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
1042
1043 trialcs = *cs;
1044 if (turning_on)
1045 set_bit(bit, &trialcs.flags);
1046 else
1047 clear_bit(bit, &trialcs.flags);
1048
1049 err = validate_change(cs, &trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001050 if (err < 0)
1051 return err;
1052 cpu_exclusive_changed =
1053 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001054 mutex_lock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001055 if (turning_on)
1056 set_bit(bit, &cs->flags);
1057 else
1058 clear_bit(bit, &cs->flags);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001059 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001060
1061 if (cpu_exclusive_changed)
1062 update_cpu_domains(cs);
1063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Paul Jackson053199e2005-10-30 15:02:30 -08001066/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001067 * Frequency meter - How fast is some event occuring?
1068 *
1069 * These routines manage a digitally filtered, constant time based,
1070 * event frequency meter. There are four routines:
1071 * fmeter_init() - initialize a frequency meter.
1072 * fmeter_markevent() - called each time the event happens.
1073 * fmeter_getrate() - returns the recent rate of such events.
1074 * fmeter_update() - internal routine used to update fmeter.
1075 *
1076 * A common data structure is passed to each of these routines,
1077 * which is used to keep track of the state required to manage the
1078 * frequency meter and its digital filter.
1079 *
1080 * The filter works on the number of events marked per unit time.
1081 * The filter is single-pole low-pass recursive (IIR). The time unit
1082 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1083 * simulate 3 decimal digits of precision (multiplied by 1000).
1084 *
1085 * With an FM_COEF of 933, and a time base of 1 second, the filter
1086 * has a half-life of 10 seconds, meaning that if the events quit
1087 * happening, then the rate returned from the fmeter_getrate()
1088 * will be cut in half each 10 seconds, until it converges to zero.
1089 *
1090 * It is not worth doing a real infinitely recursive filter. If more
1091 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1092 * just compute FM_MAXTICKS ticks worth, by which point the level
1093 * will be stable.
1094 *
1095 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1096 * arithmetic overflow in the fmeter_update() routine.
1097 *
1098 * Given the simple 32 bit integer arithmetic used, this meter works
1099 * best for reporting rates between one per millisecond (msec) and
1100 * one per 32 (approx) seconds. At constant rates faster than one
1101 * per msec it maxes out at values just under 1,000,000. At constant
1102 * rates between one per msec, and one per second it will stabilize
1103 * to a value N*1000, where N is the rate of events per second.
1104 * At constant rates between one per second and one per 32 seconds,
1105 * it will be choppy, moving up on the seconds that have an event,
1106 * and then decaying until the next event. At rates slower than
1107 * about one in 32 seconds, it decays all the way back to zero between
1108 * each event.
1109 */
1110
1111#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1112#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1113#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1114#define FM_SCALE 1000 /* faux fixed point scale */
1115
1116/* Initialize a frequency meter */
1117static void fmeter_init(struct fmeter *fmp)
1118{
1119 fmp->cnt = 0;
1120 fmp->val = 0;
1121 fmp->time = 0;
1122 spin_lock_init(&fmp->lock);
1123}
1124
1125/* Internal meter update - process cnt events and update value */
1126static void fmeter_update(struct fmeter *fmp)
1127{
1128 time_t now = get_seconds();
1129 time_t ticks = now - fmp->time;
1130
1131 if (ticks == 0)
1132 return;
1133
1134 ticks = min(FM_MAXTICKS, ticks);
1135 while (ticks-- > 0)
1136 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1137 fmp->time = now;
1138
1139 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1140 fmp->cnt = 0;
1141}
1142
1143/* Process any previous ticks, then bump cnt by one (times scale). */
1144static void fmeter_markevent(struct fmeter *fmp)
1145{
1146 spin_lock(&fmp->lock);
1147 fmeter_update(fmp);
1148 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1149 spin_unlock(&fmp->lock);
1150}
1151
1152/* Process any previous ticks, then return current value. */
1153static int fmeter_getrate(struct fmeter *fmp)
1154{
1155 int val;
1156
1157 spin_lock(&fmp->lock);
1158 fmeter_update(fmp);
1159 val = fmp->val;
1160 spin_unlock(&fmp->lock);
1161 return val;
1162}
1163
1164/*
Paul Jackson053199e2005-10-30 15:02:30 -08001165 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
1166 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1167 * notified on release.
1168 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001169 * Call holding manage_mutex. May take callback_mutex and task_lock of
Paul Jackson053199e2005-10-30 15:02:30 -08001170 * the task 'pid' during call.
1171 */
1172
Paul Jackson3077a262005-08-09 10:07:59 -07001173static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 pid_t pid;
1176 struct task_struct *tsk;
1177 struct cpuset *oldcs;
1178 cpumask_t cpus;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001179 nodemask_t from, to;
Paul Jackson42253992006-01-08 01:01:59 -08001180 struct mm_struct *mm;
David Quigley22fb52d2006-06-23 02:04:00 -07001181 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Paul Jackson3077a262005-08-09 10:07:59 -07001183 if (sscanf(pidbuf, "%d", &pid) != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -EIO;
1185 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1186 return -ENOSPC;
1187
1188 if (pid) {
1189 read_lock(&tasklist_lock);
1190
1191 tsk = find_task_by_pid(pid);
Paul Jackson053199e2005-10-30 15:02:30 -08001192 if (!tsk || tsk->flags & PF_EXITING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 read_unlock(&tasklist_lock);
1194 return -ESRCH;
1195 }
1196
1197 get_task_struct(tsk);
1198 read_unlock(&tasklist_lock);
1199
1200 if ((current->euid) && (current->euid != tsk->uid)
1201 && (current->euid != tsk->suid)) {
1202 put_task_struct(tsk);
1203 return -EACCES;
1204 }
1205 } else {
1206 tsk = current;
1207 get_task_struct(tsk);
1208 }
1209
David Quigley22fb52d2006-06-23 02:04:00 -07001210 retval = security_task_setscheduler(tsk, 0, NULL);
1211 if (retval) {
1212 put_task_struct(tsk);
1213 return retval;
1214 }
1215
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001216 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 task_lock(tsk);
1219 oldcs = tsk->cpuset;
1220 if (!oldcs) {
1221 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001222 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 put_task_struct(tsk);
1224 return -ESRCH;
1225 }
1226 atomic_inc(&cs->count);
Paul Jackson6b9c2602006-01-08 01:02:02 -08001227 rcu_assign_pointer(tsk->cpuset, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 task_unlock(tsk);
1229
1230 guarantee_online_cpus(cs, &cpus);
1231 set_cpus_allowed(tsk, cpus);
1232
Paul Jackson45b07ef2006-01-08 01:00:56 -08001233 from = oldcs->mems_allowed;
1234 to = cs->mems_allowed;
1235
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001236 mutex_unlock(&callback_mutex);
Paul Jackson42253992006-01-08 01:01:59 -08001237
1238 mm = get_task_mm(tsk);
1239 if (mm) {
1240 mpol_rebind_mm(mm, &to);
Paul Jackson2741a552006-03-31 02:30:51 -08001241 if (is_memory_migrate(cs))
Paul Jacksone4e364e2006-03-31 02:30:52 -08001242 cpuset_migrate_mm(mm, &from, &to);
Paul Jackson42253992006-01-08 01:01:59 -08001243 mmput(mm);
1244 }
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 put_task_struct(tsk);
Paul Jackson6b9c2602006-01-08 01:02:02 -08001247 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (atomic_dec_and_test(&oldcs->count))
Paul Jackson3077a262005-08-09 10:07:59 -07001249 check_for_release(oldcs, ppathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252
1253/* The various types of files and directories in a cpuset file system */
1254
1255typedef enum {
1256 FILE_ROOT,
1257 FILE_DIR,
Paul Jackson45b07ef2006-01-08 01:00:56 -08001258 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 FILE_CPULIST,
1260 FILE_MEMLIST,
1261 FILE_CPU_EXCLUSIVE,
1262 FILE_MEM_EXCLUSIVE,
1263 FILE_NOTIFY_ON_RELEASE,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001264 FILE_MEMORY_PRESSURE_ENABLED,
1265 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001266 FILE_SPREAD_PAGE,
1267 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 FILE_TASKLIST,
1269} cpuset_filetype_t;
1270
1271static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
1272 size_t nbytes, loff_t *unused_ppos)
1273{
1274 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1275 struct cftype *cft = __d_cft(file->f_dentry);
1276 cpuset_filetype_t type = cft->private;
1277 char *buffer;
Paul Jackson3077a262005-08-09 10:07:59 -07001278 char *pathbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int retval = 0;
1280
1281 /* Crude upper limit on largest legitimate cpulist user might write. */
1282 if (nbytes > 100 + 6 * NR_CPUS)
1283 return -E2BIG;
1284
1285 /* +1 for nul-terminator */
1286 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
1287 return -ENOMEM;
1288
1289 if (copy_from_user(buffer, userbuf, nbytes)) {
1290 retval = -EFAULT;
1291 goto out1;
1292 }
1293 buffer[nbytes] = 0; /* nul-terminate */
1294
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001295 mutex_lock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (is_removed(cs)) {
1298 retval = -ENODEV;
1299 goto out2;
1300 }
1301
1302 switch (type) {
1303 case FILE_CPULIST:
1304 retval = update_cpumask(cs, buffer);
1305 break;
1306 case FILE_MEMLIST:
1307 retval = update_nodemask(cs, buffer);
1308 break;
1309 case FILE_CPU_EXCLUSIVE:
1310 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
1311 break;
1312 case FILE_MEM_EXCLUSIVE:
1313 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
1314 break;
1315 case FILE_NOTIFY_ON_RELEASE:
1316 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
1317 break;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001318 case FILE_MEMORY_MIGRATE:
1319 retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
1320 break;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001321 case FILE_MEMORY_PRESSURE_ENABLED:
1322 retval = update_memory_pressure_enabled(cs, buffer);
1323 break;
1324 case FILE_MEMORY_PRESSURE:
1325 retval = -EACCES;
1326 break;
Paul Jackson825a46a2006-03-24 03:16:03 -08001327 case FILE_SPREAD_PAGE:
1328 retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
Paul Jackson151a4422006-03-24 03:16:11 -08001329 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson825a46a2006-03-24 03:16:03 -08001330 break;
1331 case FILE_SPREAD_SLAB:
1332 retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
Paul Jackson151a4422006-03-24 03:16:11 -08001333 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson825a46a2006-03-24 03:16:03 -08001334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case FILE_TASKLIST:
Paul Jackson3077a262005-08-09 10:07:59 -07001336 retval = attach_task(cs, buffer, &pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
1338 default:
1339 retval = -EINVAL;
1340 goto out2;
1341 }
1342
1343 if (retval == 0)
1344 retval = nbytes;
1345out2:
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001346 mutex_unlock(&manage_mutex);
Paul Jackson3077a262005-08-09 10:07:59 -07001347 cpuset_release_agent(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348out1:
1349 kfree(buffer);
1350 return retval;
1351}
1352
1353static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
1354 size_t nbytes, loff_t *ppos)
1355{
1356 ssize_t retval = 0;
1357 struct cftype *cft = __d_cft(file->f_dentry);
1358 if (!cft)
1359 return -ENODEV;
1360
1361 /* special function ? */
1362 if (cft->write)
1363 retval = cft->write(file, buf, nbytes, ppos);
1364 else
1365 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
1366
1367 return retval;
1368}
1369
1370/*
1371 * These ascii lists should be read in a single call, by using a user
1372 * buffer large enough to hold the entire map. If read in smaller
1373 * chunks, there is no guarantee of atomicity. Since the display format
1374 * used, list of ranges of sequential numbers, is variable length,
1375 * and since these maps can change value dynamically, one could read
1376 * gibberish by doing partial reads while a list was changing.
1377 * A single large read to a buffer that crosses a page boundary is
1378 * ok, because the result being copied to user land is not recomputed
1379 * across a page fault.
1380 */
1381
1382static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1383{
1384 cpumask_t mask;
1385
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001386 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 mask = cs->cpus_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001388 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 return cpulist_scnprintf(page, PAGE_SIZE, mask);
1391}
1392
1393static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1394{
1395 nodemask_t mask;
1396
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001397 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 mask = cs->mems_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001399 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1402}
1403
1404static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
1405 size_t nbytes, loff_t *ppos)
1406{
1407 struct cftype *cft = __d_cft(file->f_dentry);
1408 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1409 cpuset_filetype_t type = cft->private;
1410 char *page;
1411 ssize_t retval = 0;
1412 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1415 return -ENOMEM;
1416
1417 s = page;
1418
1419 switch (type) {
1420 case FILE_CPULIST:
1421 s += cpuset_sprintf_cpulist(s, cs);
1422 break;
1423 case FILE_MEMLIST:
1424 s += cpuset_sprintf_memlist(s, cs);
1425 break;
1426 case FILE_CPU_EXCLUSIVE:
1427 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
1428 break;
1429 case FILE_MEM_EXCLUSIVE:
1430 *s++ = is_mem_exclusive(cs) ? '1' : '0';
1431 break;
1432 case FILE_NOTIFY_ON_RELEASE:
1433 *s++ = notify_on_release(cs) ? '1' : '0';
1434 break;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001435 case FILE_MEMORY_MIGRATE:
1436 *s++ = is_memory_migrate(cs) ? '1' : '0';
1437 break;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001438 case FILE_MEMORY_PRESSURE_ENABLED:
1439 *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
1440 break;
1441 case FILE_MEMORY_PRESSURE:
1442 s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
1443 break;
Paul Jackson825a46a2006-03-24 03:16:03 -08001444 case FILE_SPREAD_PAGE:
1445 *s++ = is_spread_page(cs) ? '1' : '0';
1446 break;
1447 case FILE_SPREAD_SLAB:
1448 *s++ = is_spread_slab(cs) ? '1' : '0';
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 default:
1451 retval = -EINVAL;
1452 goto out;
1453 }
1454 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Al Viroeacaa1f2005-09-30 03:26:43 +01001456 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457out:
1458 free_page((unsigned long)page);
1459 return retval;
1460}
1461
1462static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1463 loff_t *ppos)
1464{
1465 ssize_t retval = 0;
1466 struct cftype *cft = __d_cft(file->f_dentry);
1467 if (!cft)
1468 return -ENODEV;
1469
1470 /* special function ? */
1471 if (cft->read)
1472 retval = cft->read(file, buf, nbytes, ppos);
1473 else
1474 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1475
1476 return retval;
1477}
1478
1479static int cpuset_file_open(struct inode *inode, struct file *file)
1480{
1481 int err;
1482 struct cftype *cft;
1483
1484 err = generic_file_open(inode, file);
1485 if (err)
1486 return err;
1487
1488 cft = __d_cft(file->f_dentry);
1489 if (!cft)
1490 return -ENODEV;
1491 if (cft->open)
1492 err = cft->open(inode, file);
1493 else
1494 err = 0;
1495
1496 return err;
1497}
1498
1499static int cpuset_file_release(struct inode *inode, struct file *file)
1500{
1501 struct cftype *cft = __d_cft(file->f_dentry);
1502 if (cft->release)
1503 return cft->release(inode, file);
1504 return 0;
1505}
1506
Paul Jackson18a19cb2005-10-30 15:02:31 -08001507/*
1508 * cpuset_rename - Only allow simple rename of directories in place.
1509 */
1510static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
1511 struct inode *new_dir, struct dentry *new_dentry)
1512{
1513 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1514 return -ENOTDIR;
1515 if (new_dentry->d_inode)
1516 return -EEXIST;
1517 if (old_dir != new_dir)
1518 return -EIO;
1519 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static struct file_operations cpuset_file_operations = {
1523 .read = cpuset_file_read,
1524 .write = cpuset_file_write,
1525 .llseek = generic_file_llseek,
1526 .open = cpuset_file_open,
1527 .release = cpuset_file_release,
1528};
1529
1530static struct inode_operations cpuset_dir_inode_operations = {
1531 .lookup = simple_lookup,
1532 .mkdir = cpuset_mkdir,
1533 .rmdir = cpuset_rmdir,
Paul Jackson18a19cb2005-10-30 15:02:31 -08001534 .rename = cpuset_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535};
1536
1537static int cpuset_create_file(struct dentry *dentry, int mode)
1538{
1539 struct inode *inode;
1540
1541 if (!dentry)
1542 return -ENOENT;
1543 if (dentry->d_inode)
1544 return -EEXIST;
1545
1546 inode = cpuset_new_inode(mode);
1547 if (!inode)
1548 return -ENOMEM;
1549
1550 if (S_ISDIR(mode)) {
1551 inode->i_op = &cpuset_dir_inode_operations;
1552 inode->i_fop = &simple_dir_operations;
1553
1554 /* start off with i_nlink == 2 (for "." entry) */
1555 inode->i_nlink++;
1556 } else if (S_ISREG(mode)) {
1557 inode->i_size = 0;
1558 inode->i_fop = &cpuset_file_operations;
1559 }
1560
1561 d_instantiate(dentry, inode);
1562 dget(dentry); /* Extra count - pin the dentry in core */
1563 return 0;
1564}
1565
1566/*
1567 * cpuset_create_dir - create a directory for an object.
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08001568 * cs: the cpuset we create the directory for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * It must have a valid ->parent field
1570 * And we are going to fill its ->dentry field.
1571 * name: The name to give to the cpuset directory. Will be copied.
1572 * mode: mode to set on new directory.
1573 */
1574
1575static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1576{
1577 struct dentry *dentry = NULL;
1578 struct dentry *parent;
1579 int error = 0;
1580
1581 parent = cs->parent->dentry;
1582 dentry = cpuset_get_dentry(parent, name);
1583 if (IS_ERR(dentry))
1584 return PTR_ERR(dentry);
1585 error = cpuset_create_file(dentry, S_IFDIR | mode);
1586 if (!error) {
1587 dentry->d_fsdata = cs;
1588 parent->d_inode->i_nlink++;
1589 cs->dentry = dentry;
1590 }
1591 dput(dentry);
1592
1593 return error;
1594}
1595
1596static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1597{
1598 struct dentry *dentry;
1599 int error;
1600
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001601 mutex_lock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 dentry = cpuset_get_dentry(dir, cft->name);
1603 if (!IS_ERR(dentry)) {
1604 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1605 if (!error)
1606 dentry->d_fsdata = (void *)cft;
1607 dput(dentry);
1608 } else
1609 error = PTR_ERR(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001610 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return error;
1612}
1613
1614/*
1615 * Stuff for reading the 'tasks' file.
1616 *
1617 * Reading this file can return large amounts of data if a cpuset has
1618 * *lots* of attached tasks. So it may need several calls to read(),
1619 * but we cannot guarantee that the information we produce is correct
1620 * unless we produce it entirely atomically.
1621 *
1622 * Upon tasks file open(), a struct ctr_struct is allocated, that
1623 * will have a pointer to an array (also allocated here). The struct
1624 * ctr_struct * is stored in file->private_data. Its resources will
1625 * be freed by release() when the file is closed. The array is used
1626 * to sprintf the PIDs and then used by read().
1627 */
1628
1629/* cpusets_tasks_read array */
1630
1631struct ctr_struct {
1632 char *buf;
1633 int bufsz;
1634};
1635
1636/*
1637 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
Paul Jackson053199e2005-10-30 15:02:30 -08001638 * Return actual number of pids loaded. No need to task_lock(p)
1639 * when reading out p->cpuset, as we don't really care if it changes
1640 * on the next cycle, and we are not going to try to dereference it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001642static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 int n = 0;
1645 struct task_struct *g, *p;
1646
1647 read_lock(&tasklist_lock);
1648
1649 do_each_thread(g, p) {
1650 if (p->cpuset == cs) {
1651 pidarray[n++] = p->pid;
1652 if (unlikely(n == npids))
1653 goto array_full;
1654 }
1655 } while_each_thread(g, p);
1656
1657array_full:
1658 read_unlock(&tasklist_lock);
1659 return n;
1660}
1661
1662static int cmppid(const void *a, const void *b)
1663{
1664 return *(pid_t *)a - *(pid_t *)b;
1665}
1666
1667/*
1668 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1669 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1670 * count 'cnt' of how many chars would be written if buf were large enough.
1671 */
1672static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1673{
1674 int cnt = 0;
1675 int i;
1676
1677 for (i = 0; i < npids; i++)
1678 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1679 return cnt;
1680}
1681
Paul Jackson053199e2005-10-30 15:02:30 -08001682/*
1683 * Handle an open on 'tasks' file. Prepare a buffer listing the
1684 * process id's of tasks currently attached to the cpuset being opened.
1685 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001686 * Does not require any specific cpuset mutexes, and does not take any.
Paul Jackson053199e2005-10-30 15:02:30 -08001687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688static int cpuset_tasks_open(struct inode *unused, struct file *file)
1689{
1690 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1691 struct ctr_struct *ctr;
1692 pid_t *pidarray;
1693 int npids;
1694 char c;
1695
1696 if (!(file->f_mode & FMODE_READ))
1697 return 0;
1698
1699 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1700 if (!ctr)
1701 goto err0;
1702
1703 /*
1704 * If cpuset gets more users after we read count, we won't have
1705 * enough space - tough. This race is indistinguishable to the
1706 * caller from the case that the additional cpuset users didn't
1707 * show up until sometime later on.
1708 */
1709 npids = atomic_read(&cs->count);
1710 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1711 if (!pidarray)
1712 goto err1;
1713
1714 npids = pid_array_load(pidarray, npids, cs);
1715 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1716
1717 /* Call pid_array_to_buf() twice, first just to get bufsz */
1718 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1719 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1720 if (!ctr->buf)
1721 goto err2;
1722 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1723
1724 kfree(pidarray);
1725 file->private_data = ctr;
1726 return 0;
1727
1728err2:
1729 kfree(pidarray);
1730err1:
1731 kfree(ctr);
1732err0:
1733 return -ENOMEM;
1734}
1735
1736static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1737 size_t nbytes, loff_t *ppos)
1738{
1739 struct ctr_struct *ctr = file->private_data;
1740
1741 if (*ppos + nbytes > ctr->bufsz)
1742 nbytes = ctr->bufsz - *ppos;
1743 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1744 return -EFAULT;
1745 *ppos += nbytes;
1746 return nbytes;
1747}
1748
1749static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1750{
1751 struct ctr_struct *ctr;
1752
1753 if (file->f_mode & FMODE_READ) {
1754 ctr = file->private_data;
1755 kfree(ctr->buf);
1756 kfree(ctr);
1757 }
1758 return 0;
1759}
1760
1761/*
1762 * for the common functions, 'private' gives the type of file
1763 */
1764
1765static struct cftype cft_tasks = {
1766 .name = "tasks",
1767 .open = cpuset_tasks_open,
1768 .read = cpuset_tasks_read,
1769 .release = cpuset_tasks_release,
1770 .private = FILE_TASKLIST,
1771};
1772
1773static struct cftype cft_cpus = {
1774 .name = "cpus",
1775 .private = FILE_CPULIST,
1776};
1777
1778static struct cftype cft_mems = {
1779 .name = "mems",
1780 .private = FILE_MEMLIST,
1781};
1782
1783static struct cftype cft_cpu_exclusive = {
1784 .name = "cpu_exclusive",
1785 .private = FILE_CPU_EXCLUSIVE,
1786};
1787
1788static struct cftype cft_mem_exclusive = {
1789 .name = "mem_exclusive",
1790 .private = FILE_MEM_EXCLUSIVE,
1791};
1792
1793static struct cftype cft_notify_on_release = {
1794 .name = "notify_on_release",
1795 .private = FILE_NOTIFY_ON_RELEASE,
1796};
1797
Paul Jackson45b07ef2006-01-08 01:00:56 -08001798static struct cftype cft_memory_migrate = {
1799 .name = "memory_migrate",
1800 .private = FILE_MEMORY_MIGRATE,
1801};
1802
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001803static struct cftype cft_memory_pressure_enabled = {
1804 .name = "memory_pressure_enabled",
1805 .private = FILE_MEMORY_PRESSURE_ENABLED,
1806};
1807
1808static struct cftype cft_memory_pressure = {
1809 .name = "memory_pressure",
1810 .private = FILE_MEMORY_PRESSURE,
1811};
1812
Paul Jackson825a46a2006-03-24 03:16:03 -08001813static struct cftype cft_spread_page = {
1814 .name = "memory_spread_page",
1815 .private = FILE_SPREAD_PAGE,
1816};
1817
1818static struct cftype cft_spread_slab = {
1819 .name = "memory_spread_slab",
1820 .private = FILE_SPREAD_SLAB,
1821};
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823static int cpuset_populate_dir(struct dentry *cs_dentry)
1824{
1825 int err;
1826
1827 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1828 return err;
1829 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1830 return err;
1831 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1832 return err;
1833 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1834 return err;
1835 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1836 return err;
Paul Jackson45b07ef2006-01-08 01:00:56 -08001837 if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
1838 return err;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001839 if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
1840 return err;
Paul Jackson825a46a2006-03-24 03:16:03 -08001841 if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0)
1842 return err;
1843 if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0)
1844 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1846 return err;
1847 return 0;
1848}
1849
1850/*
1851 * cpuset_create - create a cpuset
1852 * parent: cpuset that will be parent of the new cpuset.
1853 * name: name of the new cpuset. Will be strcpy'ed.
1854 * mode: mode to set on new inode
1855 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001856 * Must be called with the mutex on the parent inode held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
1858
1859static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1860{
1861 struct cpuset *cs;
1862 int err;
1863
1864 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1865 if (!cs)
1866 return -ENOMEM;
1867
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001868 mutex_lock(&manage_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001869 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 cs->flags = 0;
1871 if (notify_on_release(parent))
1872 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
Paul Jackson825a46a2006-03-24 03:16:03 -08001873 if (is_spread_page(parent))
1874 set_bit(CS_SPREAD_PAGE, &cs->flags);
1875 if (is_spread_slab(parent))
1876 set_bit(CS_SPREAD_SLAB, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 cs->cpus_allowed = CPU_MASK_NONE;
1878 cs->mems_allowed = NODE_MASK_NONE;
1879 atomic_set(&cs->count, 0);
1880 INIT_LIST_HEAD(&cs->sibling);
1881 INIT_LIST_HEAD(&cs->children);
Paul Jackson151a4422006-03-24 03:16:11 -08001882 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001883 fmeter_init(&cs->fmeter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 cs->parent = parent;
1886
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001887 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 list_add(&cs->sibling, &cs->parent->children);
Paul Jackson202f72d2006-01-08 01:01:57 -08001889 number_of_cpusets++;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001890 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 err = cpuset_create_dir(cs, name, mode);
1893 if (err < 0)
1894 goto err;
1895
1896 /*
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001897 * Release manage_mutex before cpuset_populate_dir() because it
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001898 * will down() this new directory's i_mutex and if we race with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * another mkdir, we might deadlock.
1900 */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001901 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 err = cpuset_populate_dir(cs->dentry);
1904 /* If err < 0, we have a half-filled directory - oh well ;) */
1905 return 0;
1906err:
1907 list_del(&cs->sibling);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001908 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 kfree(cs);
1910 return err;
1911}
1912
1913static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1914{
1915 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1916
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001917 /* the vfs holds inode->i_mutex already */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1919}
1920
1921static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1922{
1923 struct cpuset *cs = dentry->d_fsdata;
1924 struct dentry *d;
1925 struct cpuset *parent;
Paul Jackson3077a262005-08-09 10:07:59 -07001926 char *pathbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001928 /* the vfs holds both inode->i_mutex already */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001930 mutex_lock(&manage_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001931 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (atomic_read(&cs->count) > 0) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001933 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EBUSY;
1935 }
1936 if (!list_empty(&cs->children)) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001937 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return -EBUSY;
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 parent = cs->parent;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001941 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 set_bit(CS_REMOVED, &cs->flags);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001943 if (is_cpu_exclusive(cs))
1944 update_cpu_domains(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 list_del(&cs->sibling); /* delete my sibling from parent->children */
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001946 spin_lock(&cs->dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 d = dget(cs->dentry);
1948 cs->dentry = NULL;
1949 spin_unlock(&d->d_lock);
1950 cpuset_d_remove_dir(d);
1951 dput(d);
Paul Jackson202f72d2006-01-08 01:01:57 -08001952 number_of_cpusets--;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001953 mutex_unlock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08001954 if (list_empty(&parent->children))
1955 check_for_release(parent, &pathbuf);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001956 mutex_unlock(&manage_mutex);
Paul Jackson3077a262005-08-09 10:07:59 -07001957 cpuset_release_agent(pathbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959}
1960
Paul Jacksonc417f022006-01-08 01:02:01 -08001961/*
1962 * cpuset_init_early - just enough so that the calls to
1963 * cpuset_update_task_memory_state() in early init code
1964 * are harmless.
1965 */
1966
1967int __init cpuset_init_early(void)
1968{
1969 struct task_struct *tsk = current;
1970
1971 tsk->cpuset = &top_cpuset;
Paul Jackson151a4422006-03-24 03:16:11 -08001972 tsk->cpuset->mems_generation = cpuset_mems_generation++;
Paul Jacksonc417f022006-01-08 01:02:01 -08001973 return 0;
1974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976/**
1977 * cpuset_init - initialize cpusets at system boot
1978 *
1979 * Description: Initialize top_cpuset and the cpuset internal file system,
1980 **/
1981
1982int __init cpuset_init(void)
1983{
1984 struct dentry *root;
1985 int err;
1986
1987 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1988 top_cpuset.mems_allowed = NODE_MASK_ALL;
1989
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001990 fmeter_init(&top_cpuset.fmeter);
Paul Jackson151a4422006-03-24 03:16:11 -08001991 top_cpuset.mems_generation = cpuset_mems_generation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 init_task.cpuset = &top_cpuset;
1994
1995 err = register_filesystem(&cpuset_fs_type);
1996 if (err < 0)
1997 goto out;
1998 cpuset_mount = kern_mount(&cpuset_fs_type);
1999 if (IS_ERR(cpuset_mount)) {
2000 printk(KERN_ERR "cpuset: could not mount!\n");
2001 err = PTR_ERR(cpuset_mount);
2002 cpuset_mount = NULL;
2003 goto out;
2004 }
2005 root = cpuset_mount->mnt_sb->s_root;
2006 root->d_fsdata = &top_cpuset;
2007 root->d_inode->i_nlink++;
2008 top_cpuset.dentry = root;
2009 root->d_inode->i_op = &cpuset_dir_inode_operations;
Paul Jackson202f72d2006-01-08 01:01:57 -08002010 number_of_cpusets = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 err = cpuset_populate_dir(root);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002012 /* memory_pressure_enabled is in root cpuset only */
2013 if (err == 0)
2014 err = cpuset_add_file(root, &cft_memory_pressure_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015out:
2016 return err;
2017}
2018
2019/**
2020 * cpuset_init_smp - initialize cpus_allowed
2021 *
2022 * Description: Finish top cpuset after cpu, node maps are initialized
2023 **/
2024
2025void __init cpuset_init_smp(void)
2026{
2027 top_cpuset.cpus_allowed = cpu_online_map;
2028 top_cpuset.mems_allowed = node_online_map;
2029}
2030
2031/**
2032 * cpuset_fork - attach newly forked task to its parents cpuset.
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002033 * @tsk: pointer to task_struct of forking parent process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *
Paul Jackson053199e2005-10-30 15:02:30 -08002035 * Description: A task inherits its parent's cpuset at fork().
2036 *
2037 * A pointer to the shared cpuset was automatically copied in fork.c
2038 * by dup_task_struct(). However, we ignore that copy, since it was
2039 * not made under the protection of task_lock(), so might no longer be
2040 * a valid cpuset pointer. attach_task() might have already changed
2041 * current->cpuset, allowing the previously referenced cpuset to
2042 * be removed and freed. Instead, we task_lock(current) and copy
2043 * its present value of current->cpuset for our freshly forked child.
2044 *
2045 * At the point that cpuset_fork() is called, 'current' is the parent
2046 * task, and the passed argument 'child' points to the child task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 **/
2048
Paul Jackson053199e2005-10-30 15:02:30 -08002049void cpuset_fork(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Paul Jackson053199e2005-10-30 15:02:30 -08002051 task_lock(current);
2052 child->cpuset = current->cpuset;
2053 atomic_inc(&child->cpuset->count);
2054 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057/**
2058 * cpuset_exit - detach cpuset from exiting task
2059 * @tsk: pointer to task_struct of exiting process
2060 *
2061 * Description: Detach cpuset from @tsk and release it.
2062 *
Paul Jackson053199e2005-10-30 15:02:30 -08002063 * Note that cpusets marked notify_on_release force every task in
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002064 * them to take the global manage_mutex mutex when exiting.
Paul Jackson053199e2005-10-30 15:02:30 -08002065 * This could impact scaling on very large systems. Be reluctant to
2066 * use notify_on_release cpusets where very high task exit scaling
2067 * is required on large systems.
Paul Jackson2efe86b2005-05-27 02:02:43 -07002068 *
Paul Jackson053199e2005-10-30 15:02:30 -08002069 * Don't even think about derefencing 'cs' after the cpuset use count
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002070 * goes to zero, except inside a critical section guarded by manage_mutex
2071 * or callback_mutex. Otherwise a zero cpuset use count is a license to
Paul Jackson053199e2005-10-30 15:02:30 -08002072 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
2073 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002074 * This routine has to take manage_mutex, not callback_mutex, because
2075 * it is holding that mutex while calling check_for_release(),
2076 * which calls kmalloc(), so can't be called holding callback_mutex().
Paul Jackson053199e2005-10-30 15:02:30 -08002077 *
2078 * We don't need to task_lock() this reference to tsk->cpuset,
2079 * because tsk is already marked PF_EXITING, so attach_task() won't
Paul Jacksonb4b26412006-01-08 01:01:53 -08002080 * mess with it, or task is a failed fork, never visible to attach_task.
Paul Jackson06fed332006-02-15 15:17:38 -08002081 *
Paul Jackson8488bc32006-03-24 03:16:10 -08002082 * the_top_cpuset_hack:
Paul Jackson06fed332006-02-15 15:17:38 -08002083 *
2084 * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
2085 *
2086 * Don't leave a task unable to allocate memory, as that is an
2087 * accident waiting to happen should someone add a callout in
2088 * do_exit() after the cpuset_exit() call that might allocate.
2089 * If a task tries to allocate memory with an invalid cpuset,
2090 * it will oops in cpuset_update_task_memory_state().
2091 *
2092 * We call cpuset_exit() while the task is still competent to
2093 * handle notify_on_release(), then leave the task attached to
2094 * the root cpuset (top_cpuset) for the remainder of its exit.
2095 *
2096 * To do this properly, we would increment the reference count on
2097 * top_cpuset, and near the very end of the kernel/exit.c do_exit()
2098 * code we would add a second cpuset function call, to drop that
2099 * reference. This would just create an unnecessary hot spot on
2100 * the top_cpuset reference count, to no avail.
2101 *
2102 * Normally, holding a reference to a cpuset without bumping its
2103 * count is unsafe. The cpuset could go away, or someone could
2104 * attach us to a different cpuset, decrementing the count on
2105 * the first cpuset that we never incremented. But in this case,
2106 * top_cpuset isn't going away, and either task has PF_EXITING set,
2107 * which wards off any attach_task() attempts, or task is a failed
2108 * fork, never visible to attach_task.
2109 *
2110 * Another way to do this would be to set the cpuset pointer
2111 * to NULL here, and check in cpuset_update_task_memory_state()
2112 * for a NULL pointer. This hack avoids that NULL check, for no
2113 * cost (other than this way too long comment ;).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 **/
2115
2116void cpuset_exit(struct task_struct *tsk)
2117{
2118 struct cpuset *cs;
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 cs = tsk->cpuset;
Paul Jackson8488bc32006-03-24 03:16:10 -08002121 tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Paul Jackson2efe86b2005-05-27 02:02:43 -07002123 if (notify_on_release(cs)) {
Paul Jackson3077a262005-08-09 10:07:59 -07002124 char *pathbuf = NULL;
2125
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002126 mutex_lock(&manage_mutex);
Paul Jackson2efe86b2005-05-27 02:02:43 -07002127 if (atomic_dec_and_test(&cs->count))
Paul Jackson3077a262005-08-09 10:07:59 -07002128 check_for_release(cs, &pathbuf);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002129 mutex_unlock(&manage_mutex);
Paul Jackson3077a262005-08-09 10:07:59 -07002130 cpuset_release_agent(pathbuf);
Paul Jackson2efe86b2005-05-27 02:02:43 -07002131 } else {
2132 atomic_dec(&cs->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134}
2135
2136/**
2137 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2138 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2139 *
2140 * Description: Returns the cpumask_t cpus_allowed of the cpuset
2141 * attached to the specified @tsk. Guaranteed to return some non-empty
2142 * subset of cpu_online_map, even if this means going outside the
2143 * tasks cpuset.
2144 **/
2145
Paul Jackson909d75a2006-01-08 01:01:55 -08002146cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 cpumask_t mask;
2149
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002150 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002151 task_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 guarantee_online_cpus(tsk->cpuset, &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002153 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002154 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 return mask;
2157}
2158
2159void cpuset_init_current_mems_allowed(void)
2160{
2161 current->mems_allowed = NODE_MASK_ALL;
2162}
2163
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002164/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002165 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2166 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2167 *
2168 * Description: Returns the nodemask_t mems_allowed of the cpuset
2169 * attached to the specified @tsk. Guaranteed to return some non-empty
2170 * subset of node_online_map, even if this means going outside the
2171 * tasks cpuset.
2172 **/
2173
2174nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2175{
2176 nodemask_t mask;
2177
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002178 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002179 task_lock(tsk);
2180 guarantee_online_mems(tsk->cpuset, &mask);
2181 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002182 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002183
2184 return mask;
2185}
2186
2187/**
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002188 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
2189 * @zl: the zonelist to be checked
2190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
2192 */
2193int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
2194{
2195 int i;
2196
2197 for (i = 0; zl->zones[i]; i++) {
2198 int nid = zl->zones[i]->zone_pgdat->node_id;
2199
2200 if (node_isset(nid, current->mems_allowed))
2201 return 1;
2202 }
2203 return 0;
2204}
2205
Paul Jackson9bf22292005-09-06 15:18:12 -07002206/*
2207 * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002208 * ancestor to the specified cpuset. Call holding callback_mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002209 * If no ancestor is mem_exclusive (an unusual configuration), then
2210 * returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 */
Paul Jackson9bf22292005-09-06 15:18:12 -07002212static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Paul Jackson9bf22292005-09-06 15:18:12 -07002214 while (!is_mem_exclusive(cs) && cs->parent)
2215 cs = cs->parent;
2216 return cs;
2217}
2218
2219/**
2220 * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
2221 * @z: is this zone on an allowed node?
2222 * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
2223 *
2224 * If we're in interrupt, yes, we can always allocate. If zone
2225 * z's node is in our tasks mems_allowed, yes. If it's not a
2226 * __GFP_HARDWALL request and this zone's nodes is in the nearest
2227 * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
2228 * Otherwise, no.
2229 *
2230 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
2231 * and do not allow allocations outside the current tasks cpuset.
2232 * GFP_KERNEL allocations are not so marked, so can escape to the
2233 * nearest mem_exclusive ancestor cpuset.
2234 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002235 * Scanning up parent cpusets requires callback_mutex. The __alloc_pages()
Paul Jackson9bf22292005-09-06 15:18:12 -07002236 * routine only calls here with __GFP_HARDWALL bit _not_ set if
2237 * it's a GFP_KERNEL allocation, and all nodes in the current tasks
2238 * mems_allowed came up empty on the first pass over the zonelist.
2239 * So only GFP_KERNEL allocations, if all nodes in the cpuset are
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002240 * short of memory, might require taking the callback_mutex mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002241 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002242 * The first call here from mm/page_alloc:get_page_from_freelist()
2243 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, so
2244 * no allocation on a node outside the cpuset is allowed (unless in
2245 * interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002246 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002247 * The second pass through get_page_from_freelist() doesn't even call
2248 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2249 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2250 * in alloc_flags. That logic and the checks below have the combined
2251 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002252 * in_interrupt - any node ok (current task context irrelevant)
2253 * GFP_ATOMIC - any node ok
2254 * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
2255 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002256 *
2257 * Rule:
2258 * Don't call cpuset_zone_allowed() if you can't sleep, unless you
2259 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2260 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson9bf22292005-09-06 15:18:12 -07002261 **/
2262
Paul Jackson202f72d2006-01-08 01:01:57 -08002263int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002264{
2265 int node; /* node that zone z is on */
2266 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002267 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002268
2269 if (in_interrupt())
2270 return 1;
2271 node = z->zone_pgdat->node_id;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002272 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002273 if (node_isset(node, current->mems_allowed))
2274 return 1;
2275 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2276 return 0;
2277
Bob Picco5563e772005-11-13 16:06:35 -08002278 if (current->flags & PF_EXITING) /* Let dying task have memory */
2279 return 1;
2280
Paul Jackson9bf22292005-09-06 15:18:12 -07002281 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002282 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002283
Paul Jackson053199e2005-10-30 15:02:30 -08002284 task_lock(current);
2285 cs = nearest_exclusive_ancestor(current->cpuset);
2286 task_unlock(current);
2287
Paul Jackson9bf22292005-09-06 15:18:12 -07002288 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002289 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002290 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002293/**
Paul Jackson505970b2006-01-14 13:21:06 -08002294 * cpuset_lock - lock out any changes to cpuset structures
2295 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002296 * The out of memory (oom) code needs to mutex_lock cpusets
Paul Jackson505970b2006-01-14 13:21:06 -08002297 * from being changed while it scans the tasklist looking for a
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002298 * task in an overlapping cpuset. Expose callback_mutex via this
Paul Jackson505970b2006-01-14 13:21:06 -08002299 * cpuset_lock() routine, so the oom code can lock it, before
2300 * locking the task list. The tasklist_lock is a spinlock, so
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002301 * must be taken inside callback_mutex.
Paul Jackson505970b2006-01-14 13:21:06 -08002302 */
2303
2304void cpuset_lock(void)
2305{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002306 mutex_lock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002307}
2308
2309/**
2310 * cpuset_unlock - release lock on cpuset changes
2311 *
2312 * Undo the lock taken in a previous cpuset_lock() call.
2313 */
2314
2315void cpuset_unlock(void)
2316{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002317 mutex_unlock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002318}
2319
2320/**
Paul Jackson825a46a2006-03-24 03:16:03 -08002321 * cpuset_mem_spread_node() - On which node to begin search for a page
2322 *
2323 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2324 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2325 * and if the memory allocation used cpuset_mem_spread_node()
2326 * to determine on which node to start looking, as it will for
2327 * certain page cache or slab cache pages such as used for file
2328 * system buffers and inode caches, then instead of starting on the
2329 * local node to look for a free page, rather spread the starting
2330 * node around the tasks mems_allowed nodes.
2331 *
2332 * We don't have to worry about the returned node being offline
2333 * because "it can't happen", and even if it did, it would be ok.
2334 *
2335 * The routines calling guarantee_online_mems() are careful to
2336 * only set nodes in task->mems_allowed that are online. So it
2337 * should not be possible for the following code to return an
2338 * offline node. But if it did, that would be ok, as this routine
2339 * is not returning the node where the allocation must be, only
2340 * the node where the search should start. The zonelist passed to
2341 * __alloc_pages() will include all nodes. If the slab allocator
2342 * is passed an offline node, it will fall back to the local node.
2343 * See kmem_cache_alloc_node().
2344 */
2345
2346int cpuset_mem_spread_node(void)
2347{
2348 int node;
2349
2350 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2351 if (node == MAX_NUMNODES)
2352 node = first_node(current->mems_allowed);
2353 current->cpuset_mem_spread_rotor = node;
2354 return node;
2355}
2356EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2357
2358/**
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002359 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
2360 * @p: pointer to task_struct of some other task.
2361 *
2362 * Description: Return true if the nearest mem_exclusive ancestor
2363 * cpusets of tasks @p and current overlap. Used by oom killer to
2364 * determine if task @p's memory usage might impact the memory
2365 * available to the current task.
2366 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002367 * Call while holding callback_mutex.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002368 **/
2369
2370int cpuset_excl_nodes_overlap(const struct task_struct *p)
2371{
2372 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
2373 int overlap = 0; /* do cpusets overlap? */
2374
Paul Jackson053199e2005-10-30 15:02:30 -08002375 task_lock(current);
2376 if (current->flags & PF_EXITING) {
2377 task_unlock(current);
2378 goto done;
2379 }
2380 cs1 = nearest_exclusive_ancestor(current->cpuset);
2381 task_unlock(current);
2382
2383 task_lock((struct task_struct *)p);
2384 if (p->flags & PF_EXITING) {
2385 task_unlock((struct task_struct *)p);
2386 goto done;
2387 }
2388 cs2 = nearest_exclusive_ancestor(p->cpuset);
2389 task_unlock((struct task_struct *)p);
2390
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002391 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
2392done:
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002393 return overlap;
2394}
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002397 * Collection of memory_pressure is suppressed unless
2398 * this flag is enabled by writing "1" to the special
2399 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2400 */
2401
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002402int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002403
2404/**
2405 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2406 *
2407 * Keep a running average of the rate of synchronous (direct)
2408 * page reclaim efforts initiated by tasks in each cpuset.
2409 *
2410 * This represents the rate at which some task in the cpuset
2411 * ran low on memory on all nodes it was allowed to use, and
2412 * had to enter the kernels page reclaim code in an effort to
2413 * create more free memory by tossing clean pages or swapping
2414 * or writing dirty pages.
2415 *
2416 * Display to user space in the per-cpuset read-only file
2417 * "memory_pressure". Value displayed is an integer
2418 * representing the recent rate of entry into the synchronous
2419 * (direct) page reclaim by any task attached to the cpuset.
2420 **/
2421
2422void __cpuset_memory_pressure_bump(void)
2423{
2424 struct cpuset *cs;
2425
2426 task_lock(current);
2427 cs = current->cpuset;
2428 fmeter_markevent(&cs->fmeter);
2429 task_unlock(current);
2430}
2431
2432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * proc_cpuset_show()
2434 * - Print tasks cpuset path into seq_file.
2435 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002436 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2437 * doesn't really matter if tsk->cpuset changes after we read it,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002438 * and we take manage_mutex, keeping attach_task() from changing it
Paul Jackson8488bc32006-03-24 03:16:10 -08002439 * anyway. No need to check that tsk->cpuset != NULL, thanks to
2440 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
2441 * cpuset to top_cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443static int proc_cpuset_show(struct seq_file *m, void *v)
2444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct task_struct *tsk;
2446 char *buf;
2447 int retval = 0;
2448
2449 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2450 if (!buf)
2451 return -ENOMEM;
2452
2453 tsk = m->private;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002454 mutex_lock(&manage_mutex);
Paul Jackson8488bc32006-03-24 03:16:10 -08002455 retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (retval < 0)
2457 goto out;
2458 seq_puts(m, buf);
2459 seq_putc(m, '\n');
2460out:
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002461 mutex_unlock(&manage_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 kfree(buf);
2463 return retval;
2464}
2465
2466static int cpuset_open(struct inode *inode, struct file *file)
2467{
2468 struct task_struct *tsk = PROC_I(inode)->task;
2469 return single_open(file, proc_cpuset_show, tsk);
2470}
2471
2472struct file_operations proc_cpuset_operations = {
2473 .open = cpuset_open,
2474 .read = seq_read,
2475 .llseek = seq_lseek,
2476 .release = single_release,
2477};
2478
2479/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2480char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
2481{
2482 buffer += sprintf(buffer, "Cpus_allowed:\t");
2483 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
2484 buffer += sprintf(buffer, "\n");
2485 buffer += sprintf(buffer, "Mems_allowed:\t");
2486 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
2487 buffer += sprintf(buffer, "\n");
2488 return buffer;
2489}