aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/cpuset.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/cpuset.c')
-rw-r--r--kernel/cpuset.c1736
1 files changed, 628 insertions, 1108 deletions
diff --git a/kernel/cpuset.c b/kernel/cpuset.c
index 57e6448b171e..50f5dc463688 100644
--- a/kernel/cpuset.c
+++ b/kernel/cpuset.c
@@ -4,7 +4,8 @@
4 * Processor and Memory placement constraints for sets of tasks. 4 * Processor and Memory placement constraints for sets of tasks.
5 * 5 *
6 * Copyright (C) 2003 BULL SA. 6 * Copyright (C) 2003 BULL SA.
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc. 7 * Copyright (C) 2004-2007 Silicon Graphics, Inc.
8 * Copyright (C) 2006 Google, Inc
8 * 9 *
9 * Portions derived from Patrick Mochel's sysfs code. 10 * Portions derived from Patrick Mochel's sysfs code.
10 * sysfs is Copyright (c) 2001-3 Patrick Mochel 11 * sysfs is Copyright (c) 2001-3 Patrick Mochel
@@ -12,6 +13,7 @@
12 * 2003-10-10 Written by Simon Derr. 13 * 2003-10-10 Written by Simon Derr.
13 * 2003-10-22 Updates by Stephen Hemminger. 14 * 2003-10-22 Updates by Stephen Hemminger.
14 * 2004 May-July Rework by Paul Jackson. 15 * 2004 May-July Rework by Paul Jackson.
16 * 2006 Rework by Paul Menage to use generic cgroups
15 * 17 *
16 * This file is subject to the terms and conditions of the GNU General Public 18 * 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 19 * License. See the file COPYING in the main directory of the Linux
@@ -36,6 +38,7 @@
36#include <linux/mount.h> 38#include <linux/mount.h>
37#include <linux/namei.h> 39#include <linux/namei.h>
38#include <linux/pagemap.h> 40#include <linux/pagemap.h>
41#include <linux/prio_heap.h>
39#include <linux/proc_fs.h> 42#include <linux/proc_fs.h>
40#include <linux/rcupdate.h> 43#include <linux/rcupdate.h>
41#include <linux/sched.h> 44#include <linux/sched.h>
@@ -52,8 +55,7 @@
52#include <asm/uaccess.h> 55#include <asm/uaccess.h>
53#include <asm/atomic.h> 56#include <asm/atomic.h>
54#include <linux/mutex.h> 57#include <linux/mutex.h>
55 58#include <linux/kfifo.h>
56#define CPUSET_SUPER_MAGIC 0x27e0eb
57 59
58/* 60/*
59 * Tracks how many cpusets are currently defined in system. 61 * Tracks how many cpusets are currently defined in system.
@@ -62,6 +64,10 @@
62 */ 64 */
63int number_of_cpusets __read_mostly; 65int number_of_cpusets __read_mostly;
64 66
67/* Retrieve the cpuset from a cgroup */
68struct cgroup_subsys cpuset_subsys;
69struct cpuset;
70
65/* See "Frequency meter" comments, below. */ 71/* See "Frequency meter" comments, below. */
66 72
67struct fmeter { 73struct fmeter {
@@ -72,24 +78,13 @@ struct fmeter {
72}; 78};
73 79
74struct cpuset { 80struct cpuset {
81 struct cgroup_subsys_state css;
82
75 unsigned long flags; /* "unsigned long" so bitops work */ 83 unsigned long flags; /* "unsigned long" so bitops work */
76 cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ 84 cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
77 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ 85 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
78 86
79 /*
80 * Count is atomic so can incr (fork) or decr (exit) without a lock.
81 */
82 atomic_t count; /* count tasks using this cpuset */
83
84 /*
85 * We link our 'sibling' struct into our parents 'children'.
86 * Our children link their 'sibling' into our 'children'.
87 */
88 struct list_head sibling; /* my parents children */
89 struct list_head children; /* my children */
90
91 struct cpuset *parent; /* my parent */ 87 struct cpuset *parent; /* my parent */
92 struct dentry *dentry; /* cpuset fs entry */
93 88
94 /* 89 /*
95 * Copy of global cpuset_mems_generation as of the most 90 * Copy of global cpuset_mems_generation as of the most
@@ -98,15 +93,32 @@ struct cpuset {
98 int mems_generation; 93 int mems_generation;
99 94
100 struct fmeter fmeter; /* memory_pressure filter */ 95 struct fmeter fmeter; /* memory_pressure filter */
96
97 /* partition number for rebuild_sched_domains() */
98 int pn;
101}; 99};
102 100
101/* Retrieve the cpuset for a cgroup */
102static inline struct cpuset *cgroup_cs(struct cgroup *cont)
103{
104 return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
105 struct cpuset, css);
106}
107
108/* Retrieve the cpuset for a task */
109static inline struct cpuset *task_cs(struct task_struct *task)
110{
111 return container_of(task_subsys_state(task, cpuset_subsys_id),
112 struct cpuset, css);
113}
114
115
103/* bits in struct cpuset flags field */ 116/* bits in struct cpuset flags field */
104typedef enum { 117typedef enum {
105 CS_CPU_EXCLUSIVE, 118 CS_CPU_EXCLUSIVE,
106 CS_MEM_EXCLUSIVE, 119 CS_MEM_EXCLUSIVE,
107 CS_MEMORY_MIGRATE, 120 CS_MEMORY_MIGRATE,
108 CS_REMOVED, 121 CS_SCHED_LOAD_BALANCE,
109 CS_NOTIFY_ON_RELEASE,
110 CS_SPREAD_PAGE, 122 CS_SPREAD_PAGE,
111 CS_SPREAD_SLAB, 123 CS_SPREAD_SLAB,
112} cpuset_flagbits_t; 124} cpuset_flagbits_t;
@@ -122,14 +134,9 @@ static inline int is_mem_exclusive(const struct cpuset *cs)
122 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); 134 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
123} 135}
124 136
125static inline int is_removed(const struct cpuset *cs) 137static inline int is_sched_load_balance(const struct cpuset *cs)
126{ 138{
127 return test_bit(CS_REMOVED, &cs->flags); 139 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
128}
129
130static inline int notify_on_release(const struct cpuset *cs)
131{
132 return test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
133} 140}
134 141
135static inline int is_memory_migrate(const struct cpuset *cs) 142static inline int is_memory_migrate(const struct cpuset *cs)
@@ -172,14 +179,8 @@ static struct cpuset top_cpuset = {
172 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)), 179 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
173 .cpus_allowed = CPU_MASK_ALL, 180 .cpus_allowed = CPU_MASK_ALL,
174 .mems_allowed = NODE_MASK_ALL, 181 .mems_allowed = NODE_MASK_ALL,
175 .count = ATOMIC_INIT(0),
176 .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
177 .children = LIST_HEAD_INIT(top_cpuset.children),
178}; 182};
179 183
180static struct vfsmount *cpuset_mount;
181static struct super_block *cpuset_sb;
182
183/* 184/*
184 * We have two global cpuset mutexes below. They can nest. 185 * We have two global cpuset mutexes below. They can nest.
185 * It is ok to first take manage_mutex, then nest callback_mutex. We also 186 * It is ok to first take manage_mutex, then nest callback_mutex. We also
@@ -263,297 +264,33 @@ static struct super_block *cpuset_sb;
263 * the routine cpuset_update_task_memory_state(). 264 * the routine cpuset_update_task_memory_state().
264 */ 265 */
265 266
266static DEFINE_MUTEX(manage_mutex);
267static DEFINE_MUTEX(callback_mutex); 267static DEFINE_MUTEX(callback_mutex);
268 268
269/* 269/* This is ugly, but preserves the userspace API for existing cpuset
270 * A couple of forward declarations required, due to cyclic reference loop: 270 * users. If someone tries to mount the "cpuset" filesystem, we
271 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file 271 * silently switch it to mount "cgroup" instead */
272 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
273 */
274
275static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
276static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
277
278static struct backing_dev_info cpuset_backing_dev_info = {
279 .ra_pages = 0, /* No readahead */
280 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
281};
282
283static struct inode *cpuset_new_inode(mode_t mode)
284{
285 struct inode *inode = new_inode(cpuset_sb);
286
287 if (inode) {
288 inode->i_mode = mode;
289 inode->i_uid = current->fsuid;
290 inode->i_gid = current->fsgid;
291 inode->i_blocks = 0;
292 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
293 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
294 }
295 return inode;
296}
297
298static void cpuset_diput(struct dentry *dentry, struct inode *inode)
299{
300 /* is dentry a directory ? if so, kfree() associated cpuset */
301 if (S_ISDIR(inode->i_mode)) {
302 struct cpuset *cs = dentry->d_fsdata;
303 BUG_ON(!(is_removed(cs)));
304 kfree(cs);
305 }
306 iput(inode);
307}
308
309static struct dentry_operations cpuset_dops = {
310 .d_iput = cpuset_diput,
311};
312
313static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
314{
315 struct dentry *d = lookup_one_len(name, parent, strlen(name));
316 if (!IS_ERR(d))
317 d->d_op = &cpuset_dops;
318 return d;
319}
320
321static void remove_dir(struct dentry *d)
322{
323 struct dentry *parent = dget(d->d_parent);
324
325 d_delete(d);
326 simple_rmdir(parent->d_inode, d);
327 dput(parent);
328}
329
330/*
331 * NOTE : the dentry must have been dget()'ed
332 */
333static void cpuset_d_remove_dir(struct dentry *dentry)
334{
335 struct list_head *node;
336
337 spin_lock(&dcache_lock);
338 node = dentry->d_subdirs.next;
339 while (node != &dentry->d_subdirs) {
340 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
341 list_del_init(node);
342 if (d->d_inode) {
343 d = dget_locked(d);
344 spin_unlock(&dcache_lock);
345 d_delete(d);
346 simple_unlink(dentry->d_inode, d);
347 dput(d);
348 spin_lock(&dcache_lock);
349 }
350 node = dentry->d_subdirs.next;
351 }
352 list_del_init(&dentry->d_u.d_child);
353 spin_unlock(&dcache_lock);
354 remove_dir(dentry);
355}
356
357static struct super_operations cpuset_ops = {
358 .statfs = simple_statfs,
359 .drop_inode = generic_delete_inode,
360};
361
362static int cpuset_fill_super(struct super_block *sb, void *unused_data,
363 int unused_silent)
364{
365 struct inode *inode;
366 struct dentry *root;
367
368 sb->s_blocksize = PAGE_CACHE_SIZE;
369 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
370 sb->s_magic = CPUSET_SUPER_MAGIC;
371 sb->s_op = &cpuset_ops;
372 cpuset_sb = sb;
373
374 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
375 if (inode) {
376 inode->i_op = &simple_dir_inode_operations;
377 inode->i_fop = &simple_dir_operations;
378 /* directories start off with i_nlink == 2 (for "." entry) */
379 inc_nlink(inode);
380 } else {
381 return -ENOMEM;
382 }
383
384 root = d_alloc_root(inode);
385 if (!root) {
386 iput(inode);
387 return -ENOMEM;
388 }
389 sb->s_root = root;
390 return 0;
391}
392
393static int cpuset_get_sb(struct file_system_type *fs_type, 272static int cpuset_get_sb(struct file_system_type *fs_type,
394 int flags, const char *unused_dev_name, 273 int flags, const char *unused_dev_name,
395 void *data, struct vfsmount *mnt) 274 void *data, struct vfsmount *mnt)
396{ 275{
397 return get_sb_single(fs_type, flags, data, cpuset_fill_super, mnt); 276 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
277 int ret = -ENODEV;
278 if (cgroup_fs) {
279 char mountopts[] =
280 "cpuset,noprefix,"
281 "release_agent=/sbin/cpuset_release_agent";
282 ret = cgroup_fs->get_sb(cgroup_fs, flags,
283 unused_dev_name, mountopts, mnt);
284 put_filesystem(cgroup_fs);
285 }
286 return ret;
398} 287}
399 288
400static struct file_system_type cpuset_fs_type = { 289static struct file_system_type cpuset_fs_type = {
401 .name = "cpuset", 290 .name = "cpuset",
402 .get_sb = cpuset_get_sb, 291 .get_sb = cpuset_get_sb,
403 .kill_sb = kill_litter_super,
404};
405
406/* struct cftype:
407 *
408 * The files in the cpuset filesystem mostly have a very simple read/write
409 * handling, some common function will take care of it. Nevertheless some cases
410 * (read tasks) are special and therefore I define this structure for every
411 * kind of file.
412 *
413 *
414 * When reading/writing to a file:
415 * - the cpuset to use in file->f_path.dentry->d_parent->d_fsdata
416 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
417 */
418
419struct cftype {
420 char *name;
421 int private;
422 int (*open) (struct inode *inode, struct file *file);
423 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
424 loff_t *ppos);
425 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
426 loff_t *ppos);
427 int (*release) (struct inode *inode, struct file *file);
428}; 292};
429 293
430static inline struct cpuset *__d_cs(struct dentry *dentry)
431{
432 return dentry->d_fsdata;
433}
434
435static inline struct cftype *__d_cft(struct dentry *dentry)
436{
437 return dentry->d_fsdata;
438}
439
440/*
441 * Call with manage_mutex held. Writes path of cpuset into buf.
442 * Returns 0 on success, -errno on error.
443 */
444
445static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
446{
447 char *start;
448
449 start = buf + buflen;
450
451 *--start = '\0';
452 for (;;) {
453 int len = cs->dentry->d_name.len;
454 if ((start -= len) < buf)
455 return -ENAMETOOLONG;
456 memcpy(start, cs->dentry->d_name.name, len);
457 cs = cs->parent;
458 if (!cs)
459 break;
460 if (!cs->parent)
461 continue;
462 if (--start < buf)
463 return -ENAMETOOLONG;
464 *start = '/';
465 }
466 memmove(buf, start, buf + buflen - start);
467 return 0;
468}
469
470/*
471 * Notify userspace when a cpuset is released, by running
472 * /sbin/cpuset_release_agent with the name of the cpuset (path
473 * relative to the root of cpuset file system) as the argument.
474 *
475 * Most likely, this user command will try to rmdir this cpuset.
476 *
477 * This races with the possibility that some other task will be
478 * attached to this cpuset before it is removed, or that some other
479 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
480 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
481 * unused, and this cpuset will be reprieved from its death sentence,
482 * to continue to serve a useful existence. Next time it's released,
483 * we will get notified again, if it still has 'notify_on_release' set.
484 *
485 * The final arg to call_usermodehelper() is 0, which means don't
486 * wait. The separate /sbin/cpuset_release_agent task is forked by
487 * call_usermodehelper(), then control in this thread returns here,
488 * without waiting for the release agent task. We don't bother to
489 * wait because the caller of this routine has no use for the exit
490 * status of the /sbin/cpuset_release_agent task, so no sense holding
491 * our caller up for that.
492 *
493 * When we had only one cpuset mutex, we had to call this
494 * without holding it, to avoid deadlock when call_usermodehelper()
495 * allocated memory. With two locks, we could now call this while
496 * holding manage_mutex, but we still don't, so as to minimize
497 * the time manage_mutex is held.
498 */
499
500static void cpuset_release_agent(const char *pathbuf)
501{
502 char *argv[3], *envp[3];
503 int i;
504
505 if (!pathbuf)
506 return;
507
508 i = 0;
509 argv[i++] = "/sbin/cpuset_release_agent";
510 argv[i++] = (char *)pathbuf;
511 argv[i] = NULL;
512
513 i = 0;
514 /* minimal command environment */
515 envp[i++] = "HOME=/";
516 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
517 envp[i] = NULL;
518
519 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
520 kfree(pathbuf);
521}
522
523/*
524 * Either cs->count of using tasks transitioned to zero, or the
525 * cs->children list of child cpusets just became empty. If this
526 * cs is notify_on_release() and now both the user count is zero and
527 * the list of children is empty, prepare cpuset path in a kmalloc'd
528 * buffer, to be returned via ppathbuf, so that the caller can invoke
529 * cpuset_release_agent() with it later on, once manage_mutex is dropped.
530 * Call here with manage_mutex held.
531 *
532 * This check_for_release() routine is responsible for kmalloc'ing
533 * pathbuf. The above cpuset_release_agent() is responsible for
534 * kfree'ing pathbuf. The caller of these routines is responsible
535 * for providing a pathbuf pointer, initialized to NULL, then
536 * calling check_for_release() with manage_mutex held and the address
537 * of the pathbuf pointer, then dropping manage_mutex, then calling
538 * cpuset_release_agent() with pathbuf, as set by check_for_release().
539 */
540
541static void check_for_release(struct cpuset *cs, char **ppathbuf)
542{
543 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
544 list_empty(&cs->children)) {
545 char *buf;
546
547 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
548 if (!buf)
549 return;
550 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
551 kfree(buf);
552 else
553 *ppathbuf = buf;
554 }
555}
556
557/* 294/*
558 * Return in *pmask the portion of a cpusets's cpus_allowed that 295 * Return in *pmask the portion of a cpusets's cpus_allowed that
559 * are online. If none are online, walk up the cpuset hierarchy 296 * are online. If none are online, walk up the cpuset hierarchy
@@ -581,26 +318,28 @@ static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
581 318
582/* 319/*
583 * Return in *pmask the portion of a cpusets's mems_allowed that 320 * Return in *pmask the portion of a cpusets's mems_allowed that
584 * are online. If none are online, walk up the cpuset hierarchy 321 * are online, with memory. If none are online with memory, walk
585 * until we find one that does have some online mems. If we get 322 * up the cpuset hierarchy until we find one that does have some
586 * all the way to the top and still haven't found any online mems, 323 * online mems. If we get all the way to the top and still haven't
587 * return node_online_map. 324 * found any online mems, return node_states[N_HIGH_MEMORY].
588 * 325 *
589 * One way or another, we guarantee to return some non-empty subset 326 * One way or another, we guarantee to return some non-empty subset
590 * of node_online_map. 327 * of node_states[N_HIGH_MEMORY].
591 * 328 *
592 * Call with callback_mutex held. 329 * Call with callback_mutex held.
593 */ 330 */
594 331
595static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) 332static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
596{ 333{
597 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map)) 334 while (cs && !nodes_intersects(cs->mems_allowed,
335 node_states[N_HIGH_MEMORY]))
598 cs = cs->parent; 336 cs = cs->parent;
599 if (cs) 337 if (cs)
600 nodes_and(*pmask, cs->mems_allowed, node_online_map); 338 nodes_and(*pmask, cs->mems_allowed,
339 node_states[N_HIGH_MEMORY]);
601 else 340 else
602 *pmask = node_online_map; 341 *pmask = node_states[N_HIGH_MEMORY];
603 BUG_ON(!nodes_intersects(*pmask, node_online_map)); 342 BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
604} 343}
605 344
606/** 345/**
@@ -651,20 +390,19 @@ void cpuset_update_task_memory_state(void)
651 struct task_struct *tsk = current; 390 struct task_struct *tsk = current;
652 struct cpuset *cs; 391 struct cpuset *cs;
653 392
654 if (tsk->cpuset == &top_cpuset) { 393 if (task_cs(tsk) == &top_cpuset) {
655 /* Don't need rcu for top_cpuset. It's never freed. */ 394 /* Don't need rcu for top_cpuset. It's never freed. */
656 my_cpusets_mem_gen = top_cpuset.mems_generation; 395 my_cpusets_mem_gen = top_cpuset.mems_generation;
657 } else { 396 } else {
658 rcu_read_lock(); 397 rcu_read_lock();
659 cs = rcu_dereference(tsk->cpuset); 398 my_cpusets_mem_gen = task_cs(current)->mems_generation;
660 my_cpusets_mem_gen = cs->mems_generation;
661 rcu_read_unlock(); 399 rcu_read_unlock();
662 } 400 }
663 401
664 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) { 402 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
665 mutex_lock(&callback_mutex); 403 mutex_lock(&callback_mutex);
666 task_lock(tsk); 404 task_lock(tsk);
667 cs = tsk->cpuset; /* Maybe changed when task not locked */ 405 cs = task_cs(tsk); /* Maybe changed when task not locked */
668 guarantee_online_mems(cs, &tsk->mems_allowed); 406 guarantee_online_mems(cs, &tsk->mems_allowed);
669 tsk->cpuset_mems_generation = cs->mems_generation; 407 tsk->cpuset_mems_generation = cs->mems_generation;
670 if (is_spread_page(cs)) 408 if (is_spread_page(cs))
@@ -719,11 +457,12 @@ static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
719 457
720static int validate_change(const struct cpuset *cur, const struct cpuset *trial) 458static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
721{ 459{
460 struct cgroup *cont;
722 struct cpuset *c, *par; 461 struct cpuset *c, *par;
723 462
724 /* Each of our child cpusets must be a subset of us */ 463 /* Each of our child cpusets must be a subset of us */
725 list_for_each_entry(c, &cur->children, sibling) { 464 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
726 if (!is_cpuset_subset(c, trial)) 465 if (!is_cpuset_subset(cgroup_cs(cont), trial))
727 return -EBUSY; 466 return -EBUSY;
728 } 467 }
729 468
@@ -738,7 +477,8 @@ static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
738 return -EACCES; 477 return -EACCES;
739 478
740 /* If either I or some sibling (!= me) is exclusive, we can't overlap */ 479 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
741 list_for_each_entry(c, &par->children, sibling) { 480 list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
481 c = cgroup_cs(cont);
742 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && 482 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
743 c != cur && 483 c != cur &&
744 cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) 484 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
@@ -749,62 +489,247 @@ static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
749 return -EINVAL; 489 return -EINVAL;
750 } 490 }
751 491
492 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
493 if (cgroup_task_count(cur->css.cgroup)) {
494 if (cpus_empty(trial->cpus_allowed) ||
495 nodes_empty(trial->mems_allowed)) {
496 return -ENOSPC;
497 }
498 }
499
752 return 0; 500 return 0;
753} 501}
754 502
755/* 503/*
756 * For a given cpuset cur, partition the system as follows 504 * Helper routine for rebuild_sched_domains().
757 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any 505 * Do cpusets a, b have overlapping cpus_allowed masks?
758 * exclusive child cpusets
759 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
760 * exclusive child cpusets
761 * Build these two partitions by calling partition_sched_domains
762 *
763 * Call with manage_mutex held. May nest a call to the
764 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
765 * Must not be called holding callback_mutex, because we must
766 * not call lock_cpu_hotplug() while holding callback_mutex.
767 */ 506 */
768 507
769static void update_cpu_domains(struct cpuset *cur) 508static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
770{ 509{
771 struct cpuset *c, *par = cur->parent; 510 return cpus_intersects(a->cpus_allowed, b->cpus_allowed);
772 cpumask_t pspan, cspan; 511}
773 512
774 if (par == NULL || cpus_empty(cur->cpus_allowed)) 513/*
775 return; 514 * rebuild_sched_domains()
515 *
516 * If the flag 'sched_load_balance' of any cpuset with non-empty
517 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
518 * which has that flag enabled, or if any cpuset with a non-empty
519 * 'cpus' is removed, then call this routine to rebuild the
520 * scheduler's dynamic sched domains.
521 *
522 * This routine builds a partial partition of the systems CPUs
523 * (the set of non-overlappping cpumask_t's in the array 'part'
524 * below), and passes that partial partition to the kernel/sched.c
525 * partition_sched_domains() routine, which will rebuild the
526 * schedulers load balancing domains (sched domains) as specified
527 * by that partial partition. A 'partial partition' is a set of
528 * non-overlapping subsets whose union is a subset of that set.
529 *
530 * See "What is sched_load_balance" in Documentation/cpusets.txt
531 * for a background explanation of this.
532 *
533 * Does not return errors, on the theory that the callers of this
534 * routine would rather not worry about failures to rebuild sched
535 * domains when operating in the severe memory shortage situations
536 * that could cause allocation failures below.
537 *
538 * Call with cgroup_mutex held. May take callback_mutex during
539 * call due to the kfifo_alloc() and kmalloc() calls. May nest
540 * a call to the lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
541 * Must not be called holding callback_mutex, because we must not
542 * call lock_cpu_hotplug() while holding callback_mutex. Elsewhere
543 * the kernel nests callback_mutex inside lock_cpu_hotplug() calls.
544 * So the reverse nesting would risk an ABBA deadlock.
545 *
546 * The three key local variables below are:
547 * q - a kfifo queue of cpuset pointers, used to implement a
548 * top-down scan of all cpusets. This scan loads a pointer
549 * to each cpuset marked is_sched_load_balance into the
550 * array 'csa'. For our purposes, rebuilding the schedulers
551 * sched domains, we can ignore !is_sched_load_balance cpusets.
552 * csa - (for CpuSet Array) Array of pointers to all the cpusets
553 * that need to be load balanced, for convenient iterative
554 * access by the subsequent code that finds the best partition,
555 * i.e the set of domains (subsets) of CPUs such that the
556 * cpus_allowed of every cpuset marked is_sched_load_balance
557 * is a subset of one of these domains, while there are as
558 * many such domains as possible, each as small as possible.
559 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
560 * the kernel/sched.c routine partition_sched_domains() in a
561 * convenient format, that can be easily compared to the prior
562 * value to determine what partition elements (sched domains)
563 * were changed (added or removed.)
564 *
565 * Finding the best partition (set of domains):
566 * The triple nested loops below over i, j, k scan over the
567 * load balanced cpusets (using the array of cpuset pointers in
568 * csa[]) looking for pairs of cpusets that have overlapping
569 * cpus_allowed, but which don't have the same 'pn' partition
570 * number and gives them in the same partition number. It keeps
571 * looping on the 'restart' label until it can no longer find
572 * any such pairs.
573 *
574 * The union of the cpus_allowed masks from the set of
575 * all cpusets having the same 'pn' value then form the one
576 * element of the partition (one sched domain) to be passed to
577 * partition_sched_domains().
578 */
776 579
777 /* 580static void rebuild_sched_domains(void)
778 * Get all cpus from parent's cpus_allowed not part of exclusive 581{
779 * children 582 struct kfifo *q; /* queue of cpusets to be scanned */
780 */ 583 struct cpuset *cp; /* scans q */
781 pspan = par->cpus_allowed; 584 struct cpuset **csa; /* array of all cpuset ptrs */
782 list_for_each_entry(c, &par->children, sibling) { 585 int csn; /* how many cpuset ptrs in csa so far */
783 if (is_cpu_exclusive(c)) 586 int i, j, k; /* indices for partition finding loops */
784 cpus_andnot(pspan, pspan, c->cpus_allowed); 587 cpumask_t *doms; /* resulting partition; i.e. sched domains */
588 int ndoms; /* number of sched domains in result */
589 int nslot; /* next empty doms[] cpumask_t slot */
590
591 q = NULL;
592 csa = NULL;
593 doms = NULL;
594
595 /* Special case for the 99% of systems with one, full, sched domain */
596 if (is_sched_load_balance(&top_cpuset)) {
597 ndoms = 1;
598 doms = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
599 if (!doms)
600 goto rebuild;
601 *doms = top_cpuset.cpus_allowed;
602 goto rebuild;
785 } 603 }
786 if (!is_cpu_exclusive(cur)) { 604
787 cpus_or(pspan, pspan, cur->cpus_allowed); 605 q = kfifo_alloc(number_of_cpusets * sizeof(cp), GFP_KERNEL, NULL);
788 if (cpus_equal(pspan, cur->cpus_allowed)) 606 if (IS_ERR(q))
789 return; 607 goto done;
790 cspan = CPU_MASK_NONE; 608 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
791 } else { 609 if (!csa)
792 if (cpus_empty(pspan)) 610 goto done;
793 return; 611 csn = 0;
794 cspan = cur->cpus_allowed; 612
795 /* 613 cp = &top_cpuset;
796 * Get all cpus from current cpuset's cpus_allowed not part 614 __kfifo_put(q, (void *)&cp, sizeof(cp));
797 * of exclusive children 615 while (__kfifo_get(q, (void *)&cp, sizeof(cp))) {
798 */ 616 struct cgroup *cont;
799 list_for_each_entry(c, &cur->children, sibling) { 617 struct cpuset *child; /* scans child cpusets of cp */
800 if (is_cpu_exclusive(c)) 618 if (is_sched_load_balance(cp))
801 cpus_andnot(cspan, cspan, c->cpus_allowed); 619 csa[csn++] = cp;
620 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
621 child = cgroup_cs(cont);
622 __kfifo_put(q, (void *)&child, sizeof(cp));
623 }
624 }
625
626 for (i = 0; i < csn; i++)
627 csa[i]->pn = i;
628 ndoms = csn;
629
630restart:
631 /* Find the best partition (set of sched domains) */
632 for (i = 0; i < csn; i++) {
633 struct cpuset *a = csa[i];
634 int apn = a->pn;
635
636 for (j = 0; j < csn; j++) {
637 struct cpuset *b = csa[j];
638 int bpn = b->pn;
639
640 if (apn != bpn && cpusets_overlap(a, b)) {
641 for (k = 0; k < csn; k++) {
642 struct cpuset *c = csa[k];
643
644 if (c->pn == bpn)
645 c->pn = apn;
646 }
647 ndoms--; /* one less element */
648 goto restart;
649 }
650 }
651 }
652
653 /* Convert <csn, csa> to <ndoms, doms> */
654 doms = kmalloc(ndoms * sizeof(cpumask_t), GFP_KERNEL);
655 if (!doms)
656 goto rebuild;
657
658 for (nslot = 0, i = 0; i < csn; i++) {
659 struct cpuset *a = csa[i];
660 int apn = a->pn;
661
662 if (apn >= 0) {
663 cpumask_t *dp = doms + nslot;
664
665 if (nslot == ndoms) {
666 static int warnings = 10;
667 if (warnings) {
668 printk(KERN_WARNING
669 "rebuild_sched_domains confused:"
670 " nslot %d, ndoms %d, csn %d, i %d,"
671 " apn %d\n",
672 nslot, ndoms, csn, i, apn);
673 warnings--;
674 }
675 continue;
676 }
677
678 cpus_clear(*dp);
679 for (j = i; j < csn; j++) {
680 struct cpuset *b = csa[j];
681
682 if (apn == b->pn) {
683 cpus_or(*dp, *dp, b->cpus_allowed);
684 b->pn = -1;
685 }
686 }
687 nslot++;
802 } 688 }
803 } 689 }
690 BUG_ON(nslot != ndoms);
804 691
692rebuild:
693 /* Have scheduler rebuild sched domains */
805 lock_cpu_hotplug(); 694 lock_cpu_hotplug();
806 partition_sched_domains(&pspan, &cspan); 695 partition_sched_domains(ndoms, doms);
807 unlock_cpu_hotplug(); 696 unlock_cpu_hotplug();
697
698done:
699 if (q && !IS_ERR(q))
700 kfifo_free(q);
701 kfree(csa);
702 /* Don't kfree(doms) -- partition_sched_domains() does that. */
703}
704
705static inline int started_after_time(struct task_struct *t1,
706 struct timespec *time,
707 struct task_struct *t2)
708{
709 int start_diff = timespec_compare(&t1->start_time, time);
710 if (start_diff > 0) {
711 return 1;
712 } else if (start_diff < 0) {
713 return 0;
714 } else {
715 /*
716 * Arbitrarily, if two processes started at the same
717 * time, we'll say that the lower pointer value
718 * started first. Note that t2 may have exited by now
719 * so this may not be a valid pointer any longer, but
720 * that's fine - it still serves to distinguish
721 * between two tasks started (effectively)
722 * simultaneously.
723 */
724 return t1 > t2;
725 }
726}
727
728static inline int started_after(void *p1, void *p2)
729{
730 struct task_struct *t1 = p1;
731 struct task_struct *t2 = p2;
732 return started_after_time(t1, &t2->start_time, t2);
808} 733}
809 734
810/* 735/*
@@ -814,7 +739,15 @@ static void update_cpu_domains(struct cpuset *cur)
814static int update_cpumask(struct cpuset *cs, char *buf) 739static int update_cpumask(struct cpuset *cs, char *buf)
815{ 740{
816 struct cpuset trialcs; 741 struct cpuset trialcs;
817 int retval, cpus_unchanged; 742 int retval, i;
743 int is_load_balanced;
744 struct cgroup_iter it;
745 struct cgroup *cgrp = cs->css.cgroup;
746 struct task_struct *p, *dropped;
747 /* Never dereference latest_task, since it's not refcounted */
748 struct task_struct *latest_task = NULL;
749 struct ptr_heap heap;
750 struct timespec latest_time = { 0, 0 };
818 751
819 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */ 752 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
820 if (cs == &top_cpuset) 753 if (cs == &top_cpuset)
@@ -823,11 +756,13 @@ static int update_cpumask(struct cpuset *cs, char *buf)
823 trialcs = *cs; 756 trialcs = *cs;
824 757
825 /* 758 /*
826 * We allow a cpuset's cpus_allowed to be empty; if it has attached 759 * An empty cpus_allowed is ok iff there are no tasks in the cpuset.
827 * tasks, we'll catch it later when we validate the change and return 760 * Since cpulist_parse() fails on an empty mask, we special case
828 * -ENOSPC. 761 * that parsing. The validate_change() call ensures that cpusets
762 * with tasks have cpus.
829 */ 763 */
830 if (!buf[0] || (buf[0] == '\n' && !buf[1])) { 764 buf = strstrip(buf);
765 if (!*buf) {
831 cpus_clear(trialcs.cpus_allowed); 766 cpus_clear(trialcs.cpus_allowed);
832 } else { 767 } else {
833 retval = cpulist_parse(buf, trialcs.cpus_allowed); 768 retval = cpulist_parse(buf, trialcs.cpus_allowed);
@@ -835,18 +770,79 @@ static int update_cpumask(struct cpuset *cs, char *buf)
835 return retval; 770 return retval;
836 } 771 }
837 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); 772 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
838 /* cpus_allowed cannot be empty for a cpuset with attached tasks. */
839 if (atomic_read(&cs->count) && cpus_empty(trialcs.cpus_allowed))
840 return -ENOSPC;
841 retval = validate_change(cs, &trialcs); 773 retval = validate_change(cs, &trialcs);
842 if (retval < 0) 774 if (retval < 0)
843 return retval; 775 return retval;
844 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); 776
777 /* Nothing to do if the cpus didn't change */
778 if (cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed))
779 return 0;
780 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, &started_after);
781 if (retval)
782 return retval;
783
784 is_load_balanced = is_sched_load_balance(&trialcs);
785
845 mutex_lock(&callback_mutex); 786 mutex_lock(&callback_mutex);
846 cs->cpus_allowed = trialcs.cpus_allowed; 787 cs->cpus_allowed = trialcs.cpus_allowed;
847 mutex_unlock(&callback_mutex); 788 mutex_unlock(&callback_mutex);
848 if (is_cpu_exclusive(cs) && !cpus_unchanged) 789
849 update_cpu_domains(cs); 790 again:
791 /*
792 * Scan tasks in the cpuset, and update the cpumasks of any
793 * that need an update. Since we can't call set_cpus_allowed()
794 * while holding tasklist_lock, gather tasks to be processed
795 * in a heap structure. If the statically-sized heap fills up,
796 * overflow tasks that started later, and in future iterations
797 * only consider tasks that started after the latest task in
798 * the previous pass. This guarantees forward progress and
799 * that we don't miss any tasks
800 */
801 heap.size = 0;
802 cgroup_iter_start(cgrp, &it);
803 while ((p = cgroup_iter_next(cgrp, &it))) {
804 /* Only affect tasks that don't have the right cpus_allowed */
805 if (cpus_equal(p->cpus_allowed, cs->cpus_allowed))
806 continue;
807 /*
808 * Only process tasks that started after the last task
809 * we processed
810 */
811 if (!started_after_time(p, &latest_time, latest_task))
812 continue;
813 dropped = heap_insert(&heap, p);
814 if (dropped == NULL) {
815 get_task_struct(p);
816 } else if (dropped != p) {
817 get_task_struct(p);
818 put_task_struct(dropped);
819 }
820 }
821 cgroup_iter_end(cgrp, &it);
822 if (heap.size) {
823 for (i = 0; i < heap.size; i++) {
824 struct task_struct *p = heap.ptrs[i];
825 if (i == 0) {
826 latest_time = p->start_time;
827 latest_task = p;
828 }
829 set_cpus_allowed(p, cs->cpus_allowed);
830 put_task_struct(p);
831 }
832 /*
833 * If we had to process any tasks at all, scan again
834 * in case some of them were in the middle of forking
835 * children that didn't notice the new cpumask
836 * restriction. Not the most efficient way to do it,
837 * but it avoids having to take callback_mutex in the
838 * fork path
839 */
840 goto again;
841 }
842 heap_free(&heap);
843 if (is_load_balanced)
844 rebuild_sched_domains();
845
850 return 0; 846 return 0;
851} 847}
852 848
@@ -895,7 +891,7 @@ static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
895 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL); 891 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
896 892
897 mutex_lock(&callback_mutex); 893 mutex_lock(&callback_mutex);
898 guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed); 894 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
899 mutex_unlock(&callback_mutex); 895 mutex_unlock(&callback_mutex);
900} 896}
901 897
@@ -913,46 +909,50 @@ static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
913 * their mempolicies to the cpusets new mems_allowed. 909 * their mempolicies to the cpusets new mems_allowed.
914 */ 910 */
915 911
912static void *cpuset_being_rebound;
913
916static int update_nodemask(struct cpuset *cs, char *buf) 914static int update_nodemask(struct cpuset *cs, char *buf)
917{ 915{
918 struct cpuset trialcs; 916 struct cpuset trialcs;
919 nodemask_t oldmem; 917 nodemask_t oldmem;
920 struct task_struct *g, *p; 918 struct task_struct *p;
921 struct mm_struct **mmarray; 919 struct mm_struct **mmarray;
922 int i, n, ntasks; 920 int i, n, ntasks;
923 int migrate; 921 int migrate;
924 int fudge; 922 int fudge;
925 int retval; 923 int retval;
924 struct cgroup_iter it;
926 925
927 /* top_cpuset.mems_allowed tracks node_online_map; it's read-only */ 926 /*
927 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
928 * it's read-only
929 */
928 if (cs == &top_cpuset) 930 if (cs == &top_cpuset)
929 return -EACCES; 931 return -EACCES;
930 932
931 trialcs = *cs; 933 trialcs = *cs;
932 934
933 /* 935 /*
934 * We allow a cpuset's mems_allowed to be empty; if it has attached 936 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
935 * tasks, we'll catch it later when we validate the change and return 937 * Since nodelist_parse() fails on an empty mask, we special case
936 * -ENOSPC. 938 * that parsing. The validate_change() call ensures that cpusets
939 * with tasks have memory.
937 */ 940 */
938 if (!buf[0] || (buf[0] == '\n' && !buf[1])) { 941 buf = strstrip(buf);
942 if (!*buf) {
939 nodes_clear(trialcs.mems_allowed); 943 nodes_clear(trialcs.mems_allowed);
940 } else { 944 } else {
941 retval = nodelist_parse(buf, trialcs.mems_allowed); 945 retval = nodelist_parse(buf, trialcs.mems_allowed);
942 if (retval < 0) 946 if (retval < 0)
943 goto done; 947 goto done;
944 } 948 }
945 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map); 949 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed,
950 node_states[N_HIGH_MEMORY]);
946 oldmem = cs->mems_allowed; 951 oldmem = cs->mems_allowed;
947 if (nodes_equal(oldmem, trialcs.mems_allowed)) { 952 if (nodes_equal(oldmem, trialcs.mems_allowed)) {
948 retval = 0; /* Too easy - nothing to do */ 953 retval = 0; /* Too easy - nothing to do */
949 goto done; 954 goto done;
950 } 955 }
951 /* mems_allowed cannot be empty for a cpuset with attached tasks. */
952 if (atomic_read(&cs->count) && nodes_empty(trialcs.mems_allowed)) {
953 retval = -ENOSPC;
954 goto done;
955 }
956 retval = validate_change(cs, &trialcs); 956 retval = validate_change(cs, &trialcs);
957 if (retval < 0) 957 if (retval < 0)
958 goto done; 958 goto done;
@@ -962,7 +962,7 @@ static int update_nodemask(struct cpuset *cs, char *buf)
962 cs->mems_generation = cpuset_mems_generation++; 962 cs->mems_generation = cpuset_mems_generation++;
963 mutex_unlock(&callback_mutex); 963 mutex_unlock(&callback_mutex);
964 964
965 set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */ 965 cpuset_being_rebound = cs; /* causes mpol_copy() rebind */
966 966
967 fudge = 10; /* spare mmarray[] slots */ 967 fudge = 10; /* spare mmarray[] slots */
968 fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */ 968 fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
@@ -976,13 +976,13 @@ static int update_nodemask(struct cpuset *cs, char *buf)
976 * enough mmarray[] w/o using GFP_ATOMIC. 976 * enough mmarray[] w/o using GFP_ATOMIC.
977 */ 977 */
978 while (1) { 978 while (1) {
979 ntasks = atomic_read(&cs->count); /* guess */ 979 ntasks = cgroup_task_count(cs->css.cgroup); /* guess */
980 ntasks += fudge; 980 ntasks += fudge;
981 mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL); 981 mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
982 if (!mmarray) 982 if (!mmarray)
983 goto done; 983 goto done;
984 read_lock(&tasklist_lock); /* block fork */ 984 read_lock(&tasklist_lock); /* block fork */
985 if (atomic_read(&cs->count) <= ntasks) 985 if (cgroup_task_count(cs->css.cgroup) <= ntasks)
986 break; /* got enough */ 986 break; /* got enough */
987 read_unlock(&tasklist_lock); /* try again */ 987 read_unlock(&tasklist_lock); /* try again */
988 kfree(mmarray); 988 kfree(mmarray);
@@ -991,21 +991,21 @@ static int update_nodemask(struct cpuset *cs, char *buf)
991 n = 0; 991 n = 0;
992 992
993 /* Load up mmarray[] with mm reference for each task in cpuset. */ 993 /* Load up mmarray[] with mm reference for each task in cpuset. */
994 do_each_thread(g, p) { 994 cgroup_iter_start(cs->css.cgroup, &it);
995 while ((p = cgroup_iter_next(cs->css.cgroup, &it))) {
995 struct mm_struct *mm; 996 struct mm_struct *mm;
996 997
997 if (n >= ntasks) { 998 if (n >= ntasks) {
998 printk(KERN_WARNING 999 printk(KERN_WARNING
999 "Cpuset mempolicy rebind incomplete.\n"); 1000 "Cpuset mempolicy rebind incomplete.\n");
1000 continue; 1001 break;
1001 } 1002 }
1002 if (p->cpuset != cs)
1003 continue;
1004 mm = get_task_mm(p); 1003 mm = get_task_mm(p);
1005 if (!mm) 1004 if (!mm)
1006 continue; 1005 continue;
1007 mmarray[n++] = mm; 1006 mmarray[n++] = mm;
1008 } while_each_thread(g, p); 1007 }
1008 cgroup_iter_end(cs->css.cgroup, &it);
1009 read_unlock(&tasklist_lock); 1009 read_unlock(&tasklist_lock);
1010 1010
1011 /* 1011 /*
@@ -1033,12 +1033,17 @@ static int update_nodemask(struct cpuset *cs, char *buf)
1033 1033
1034 /* We're done rebinding vma's to this cpusets new mems_allowed. */ 1034 /* We're done rebinding vma's to this cpusets new mems_allowed. */
1035 kfree(mmarray); 1035 kfree(mmarray);
1036 set_cpuset_being_rebound(NULL); 1036 cpuset_being_rebound = NULL;
1037 retval = 0; 1037 retval = 0;
1038done: 1038done:
1039 return retval; 1039 return retval;
1040} 1040}
1041 1041
1042int current_cpuset_is_being_rebound(void)
1043{
1044 return task_cs(current) == cpuset_being_rebound;
1045}
1046
1042/* 1047/*
1043 * Call with manage_mutex held. 1048 * Call with manage_mutex held.
1044 */ 1049 */
@@ -1055,6 +1060,7 @@ static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
1055/* 1060/*
1056 * update_flag - read a 0 or a 1 in a file and update associated flag 1061 * update_flag - read a 0 or a 1 in a file and update associated flag
1057 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, 1062 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
1063 * CS_SCHED_LOAD_BALANCE,
1058 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE, 1064 * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
1059 * CS_SPREAD_PAGE, CS_SPREAD_SLAB) 1065 * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
1060 * cs: the cpuset to update 1066 * cs: the cpuset to update
@@ -1067,7 +1073,8 @@ static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
1067{ 1073{
1068 int turning_on; 1074 int turning_on;
1069 struct cpuset trialcs; 1075 struct cpuset trialcs;
1070 int err, cpu_exclusive_changed; 1076 int err;
1077 int cpus_nonempty, balance_flag_changed;
1071 1078
1072 turning_on = (simple_strtoul(buf, NULL, 10) != 0); 1079 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
1073 1080
@@ -1080,14 +1087,18 @@ static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
1080 err = validate_change(cs, &trialcs); 1087 err = validate_change(cs, &trialcs);
1081 if (err < 0) 1088 if (err < 0)
1082 return err; 1089 return err;
1083 cpu_exclusive_changed = 1090
1084 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs)); 1091 cpus_nonempty = !cpus_empty(trialcs.cpus_allowed);
1092 balance_flag_changed = (is_sched_load_balance(cs) !=
1093 is_sched_load_balance(&trialcs));
1094
1085 mutex_lock(&callback_mutex); 1095 mutex_lock(&callback_mutex);
1086 cs->flags = trialcs.flags; 1096 cs->flags = trialcs.flags;
1087 mutex_unlock(&callback_mutex); 1097 mutex_unlock(&callback_mutex);
1088 1098
1089 if (cpu_exclusive_changed) 1099 if (cpus_nonempty && balance_flag_changed)
1090 update_cpu_domains(cs); 1100 rebuild_sched_domains();
1101
1091 return 0; 1102 return 0;
1092} 1103}
1093 1104
@@ -1189,85 +1200,34 @@ static int fmeter_getrate(struct fmeter *fmp)
1189 return val; 1200 return val;
1190} 1201}
1191 1202
1192/* 1203static int cpuset_can_attach(struct cgroup_subsys *ss,
1193 * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly 1204 struct cgroup *cont, struct task_struct *tsk)
1194 * writing the path of the old cpuset in 'ppathbuf' if it needs to be
1195 * notified on release.
1196 *
1197 * Call holding manage_mutex. May take callback_mutex and task_lock of
1198 * the task 'pid' during call.
1199 */
1200
1201static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
1202{ 1205{
1203 pid_t pid; 1206 struct cpuset *cs = cgroup_cs(cont);
1204 struct task_struct *tsk;
1205 struct cpuset *oldcs;
1206 cpumask_t cpus;
1207 nodemask_t from, to;
1208 struct mm_struct *mm;
1209 int retval;
1210 1207
1211 if (sscanf(pidbuf, "%d", &pid) != 1)
1212 return -EIO;
1213 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) 1208 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1214 return -ENOSPC; 1209 return -ENOSPC;
1215 1210
1216 if (pid) { 1211 return security_task_setscheduler(tsk, 0, NULL);
1217 read_lock(&tasklist_lock); 1212}
1218
1219 tsk = find_task_by_pid(pid);
1220 if (!tsk || tsk->flags & PF_EXITING) {
1221 read_unlock(&tasklist_lock);
1222 return -ESRCH;
1223 }
1224
1225 get_task_struct(tsk);
1226 read_unlock(&tasklist_lock);
1227
1228 if ((current->euid) && (current->euid != tsk->uid)
1229 && (current->euid != tsk->suid)) {
1230 put_task_struct(tsk);
1231 return -EACCES;
1232 }
1233 } else {
1234 tsk = current;
1235 get_task_struct(tsk);
1236 }
1237 1213
1238 retval = security_task_setscheduler(tsk, 0, NULL); 1214static void cpuset_attach(struct cgroup_subsys *ss,
1239 if (retval) { 1215 struct cgroup *cont, struct cgroup *oldcont,
1240 put_task_struct(tsk); 1216 struct task_struct *tsk)
1241 return retval; 1217{
1242 } 1218 cpumask_t cpus;
1219 nodemask_t from, to;
1220 struct mm_struct *mm;
1221 struct cpuset *cs = cgroup_cs(cont);
1222 struct cpuset *oldcs = cgroup_cs(oldcont);
1243 1223
1244 mutex_lock(&callback_mutex); 1224 mutex_lock(&callback_mutex);
1245
1246 task_lock(tsk);
1247 oldcs = tsk->cpuset;
1248 /*
1249 * After getting 'oldcs' cpuset ptr, be sure still not exiting.
1250 * If 'oldcs' might be the top_cpuset due to the_top_cpuset_hack
1251 * then fail this attach_task(), to avoid breaking top_cpuset.count.
1252 */
1253 if (tsk->flags & PF_EXITING) {
1254 task_unlock(tsk);
1255 mutex_unlock(&callback_mutex);
1256 put_task_struct(tsk);
1257 return -ESRCH;
1258 }
1259 atomic_inc(&cs->count);
1260 rcu_assign_pointer(tsk->cpuset, cs);
1261 task_unlock(tsk);
1262
1263 guarantee_online_cpus(cs, &cpus); 1225 guarantee_online_cpus(cs, &cpus);
1264 set_cpus_allowed(tsk, cpus); 1226 set_cpus_allowed(tsk, cpus);
1227 mutex_unlock(&callback_mutex);
1265 1228
1266 from = oldcs->mems_allowed; 1229 from = oldcs->mems_allowed;
1267 to = cs->mems_allowed; 1230 to = cs->mems_allowed;
1268
1269 mutex_unlock(&callback_mutex);
1270
1271 mm = get_task_mm(tsk); 1231 mm = get_task_mm(tsk);
1272 if (mm) { 1232 if (mm) {
1273 mpol_rebind_mm(mm, &to); 1233 mpol_rebind_mm(mm, &to);
@@ -1276,44 +1236,36 @@ static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
1276 mmput(mm); 1236 mmput(mm);
1277 } 1237 }
1278 1238
1279 put_task_struct(tsk);
1280 synchronize_rcu();
1281 if (atomic_dec_and_test(&oldcs->count))
1282 check_for_release(oldcs, ppathbuf);
1283 return 0;
1284} 1239}
1285 1240
1286/* The various types of files and directories in a cpuset file system */ 1241/* The various types of files and directories in a cpuset file system */
1287 1242
1288typedef enum { 1243typedef enum {
1289 FILE_ROOT,
1290 FILE_DIR,
1291 FILE_MEMORY_MIGRATE, 1244 FILE_MEMORY_MIGRATE,
1292 FILE_CPULIST, 1245 FILE_CPULIST,
1293 FILE_MEMLIST, 1246 FILE_MEMLIST,
1294 FILE_CPU_EXCLUSIVE, 1247 FILE_CPU_EXCLUSIVE,
1295 FILE_MEM_EXCLUSIVE, 1248 FILE_MEM_EXCLUSIVE,
1296 FILE_NOTIFY_ON_RELEASE, 1249 FILE_SCHED_LOAD_BALANCE,
1297 FILE_MEMORY_PRESSURE_ENABLED, 1250 FILE_MEMORY_PRESSURE_ENABLED,
1298 FILE_MEMORY_PRESSURE, 1251 FILE_MEMORY_PRESSURE,
1299 FILE_SPREAD_PAGE, 1252 FILE_SPREAD_PAGE,
1300 FILE_SPREAD_SLAB, 1253 FILE_SPREAD_SLAB,
1301 FILE_TASKLIST,
1302} cpuset_filetype_t; 1254} cpuset_filetype_t;
1303 1255
1304static ssize_t cpuset_common_file_write(struct file *file, 1256static ssize_t cpuset_common_file_write(struct cgroup *cont,
1257 struct cftype *cft,
1258 struct file *file,
1305 const char __user *userbuf, 1259 const char __user *userbuf,
1306 size_t nbytes, loff_t *unused_ppos) 1260 size_t nbytes, loff_t *unused_ppos)
1307{ 1261{
1308 struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent); 1262 struct cpuset *cs = cgroup_cs(cont);
1309 struct cftype *cft = __d_cft(file->f_path.dentry);
1310 cpuset_filetype_t type = cft->private; 1263 cpuset_filetype_t type = cft->private;
1311 char *buffer; 1264 char *buffer;
1312 char *pathbuf = NULL;
1313 int retval = 0; 1265 int retval = 0;
1314 1266
1315 /* Crude upper limit on largest legitimate cpulist user might write. */ 1267 /* Crude upper limit on largest legitimate cpulist user might write. */
1316 if (nbytes > 100 + 6 * max(NR_CPUS, MAX_NUMNODES)) 1268 if (nbytes > 100U + 6 * max(NR_CPUS, MAX_NUMNODES))
1317 return -E2BIG; 1269 return -E2BIG;
1318 1270
1319 /* +1 for nul-terminator */ 1271 /* +1 for nul-terminator */
@@ -1326,9 +1278,9 @@ static ssize_t cpuset_common_file_write(struct file *file,
1326 } 1278 }
1327 buffer[nbytes] = 0; /* nul-terminate */ 1279 buffer[nbytes] = 0; /* nul-terminate */
1328 1280
1329 mutex_lock(&manage_mutex); 1281 cgroup_lock();
1330 1282
1331 if (is_removed(cs)) { 1283 if (cgroup_is_removed(cont)) {
1332 retval = -ENODEV; 1284 retval = -ENODEV;
1333 goto out2; 1285 goto out2;
1334 } 1286 }
@@ -1346,8 +1298,8 @@ static ssize_t cpuset_common_file_write(struct file *file,
1346 case FILE_MEM_EXCLUSIVE: 1298 case FILE_MEM_EXCLUSIVE:
1347 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); 1299 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
1348 break; 1300 break;
1349 case FILE_NOTIFY_ON_RELEASE: 1301 case FILE_SCHED_LOAD_BALANCE:
1350 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer); 1302 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, buffer);
1351 break; 1303 break;
1352 case FILE_MEMORY_MIGRATE: 1304 case FILE_MEMORY_MIGRATE:
1353 retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer); 1305 retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
@@ -1366,9 +1318,6 @@ static ssize_t cpuset_common_file_write(struct file *file,
1366 retval = update_flag(CS_SPREAD_SLAB, cs, buffer); 1318 retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
1367 cs->mems_generation = cpuset_mems_generation++; 1319 cs->mems_generation = cpuset_mems_generation++;
1368 break; 1320 break;
1369 case FILE_TASKLIST:
1370 retval = attach_task(cs, buffer, &pathbuf);
1371 break;
1372 default: 1321 default:
1373 retval = -EINVAL; 1322 retval = -EINVAL;
1374 goto out2; 1323 goto out2;
@@ -1377,30 +1326,12 @@ static ssize_t cpuset_common_file_write(struct file *file,
1377 if (retval == 0) 1326 if (retval == 0)
1378 retval = nbytes; 1327 retval = nbytes;
1379out2: 1328out2:
1380 mutex_unlock(&manage_mutex); 1329 cgroup_unlock();
1381 cpuset_release_agent(pathbuf);
1382out1: 1330out1:
1383 kfree(buffer); 1331 kfree(buffer);
1384 return retval; 1332 return retval;
1385} 1333}
1386 1334
1387static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
1388 size_t nbytes, loff_t *ppos)
1389{
1390 ssize_t retval = 0;
1391 struct cftype *cft = __d_cft(file->f_path.dentry);
1392 if (!cft)
1393 return -ENODEV;
1394
1395 /* special function ? */
1396 if (cft->write)
1397 retval = cft->write(file, buf, nbytes, ppos);
1398 else
1399 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
1400
1401 return retval;
1402}
1403
1404/* 1335/*
1405 * These ascii lists should be read in a single call, by using a user 1336 * These ascii lists should be read in a single call, by using a user
1406 * buffer large enough to hold the entire map. If read in smaller 1337 * buffer large enough to hold the entire map. If read in smaller
@@ -1435,17 +1366,19 @@ static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1435 return nodelist_scnprintf(page, PAGE_SIZE, mask); 1366 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1436} 1367}
1437 1368
1438static ssize_t cpuset_common_file_read(struct file *file, char __user *buf, 1369static ssize_t cpuset_common_file_read(struct cgroup *cont,
1439 size_t nbytes, loff_t *ppos) 1370 struct cftype *cft,
1371 struct file *file,
1372 char __user *buf,
1373 size_t nbytes, loff_t *ppos)
1440{ 1374{
1441 struct cftype *cft = __d_cft(file->f_path.dentry); 1375 struct cpuset *cs = cgroup_cs(cont);
1442 struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
1443 cpuset_filetype_t type = cft->private; 1376 cpuset_filetype_t type = cft->private;
1444 char *page; 1377 char *page;
1445 ssize_t retval = 0; 1378 ssize_t retval = 0;
1446 char *s; 1379 char *s;
1447 1380
1448 if (!(page = (char *)__get_free_page(GFP_KERNEL))) 1381 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
1449 return -ENOMEM; 1382 return -ENOMEM;
1450 1383
1451 s = page; 1384 s = page;
@@ -1463,8 +1396,8 @@ static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
1463 case FILE_MEM_EXCLUSIVE: 1396 case FILE_MEM_EXCLUSIVE:
1464 *s++ = is_mem_exclusive(cs) ? '1' : '0'; 1397 *s++ = is_mem_exclusive(cs) ? '1' : '0';
1465 break; 1398 break;
1466 case FILE_NOTIFY_ON_RELEASE: 1399 case FILE_SCHED_LOAD_BALANCE:
1467 *s++ = notify_on_release(cs) ? '1' : '0'; 1400 *s++ = is_sched_load_balance(cs) ? '1' : '0';
1468 break; 1401 break;
1469 case FILE_MEMORY_MIGRATE: 1402 case FILE_MEMORY_MIGRATE:
1470 *s++ = is_memory_migrate(cs) ? '1' : '0'; 1403 *s++ = is_memory_migrate(cs) ? '1' : '0';
@@ -1493,390 +1426,150 @@ out:
1493 return retval; 1426 return retval;
1494} 1427}
1495 1428
1496static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
1497 loff_t *ppos)
1498{
1499 ssize_t retval = 0;
1500 struct cftype *cft = __d_cft(file->f_path.dentry);
1501 if (!cft)
1502 return -ENODEV;
1503 1429
1504 /* special function ? */
1505 if (cft->read)
1506 retval = cft->read(file, buf, nbytes, ppos);
1507 else
1508 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
1509 1430
1510 return retval;
1511}
1512 1431
1513static int cpuset_file_open(struct inode *inode, struct file *file)
1514{
1515 int err;
1516 struct cftype *cft;
1517
1518 err = generic_file_open(inode, file);
1519 if (err)
1520 return err;
1521
1522 cft = __d_cft(file->f_path.dentry);
1523 if (!cft)
1524 return -ENODEV;
1525 if (cft->open)
1526 err = cft->open(inode, file);
1527 else
1528 err = 0;
1529
1530 return err;
1531}
1532
1533static int cpuset_file_release(struct inode *inode, struct file *file)
1534{
1535 struct cftype *cft = __d_cft(file->f_path.dentry);
1536 if (cft->release)
1537 return cft->release(inode, file);
1538 return 0;
1539}
1540
1541/*
1542 * cpuset_rename - Only allow simple rename of directories in place.
1543 */
1544static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
1545 struct inode *new_dir, struct dentry *new_dentry)
1546{
1547 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1548 return -ENOTDIR;
1549 if (new_dentry->d_inode)
1550 return -EEXIST;
1551 if (old_dir != new_dir)
1552 return -EIO;
1553 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1554}
1555
1556static const struct file_operations cpuset_file_operations = {
1557 .read = cpuset_file_read,
1558 .write = cpuset_file_write,
1559 .llseek = generic_file_llseek,
1560 .open = cpuset_file_open,
1561 .release = cpuset_file_release,
1562};
1563
1564static const struct inode_operations cpuset_dir_inode_operations = {
1565 .lookup = simple_lookup,
1566 .mkdir = cpuset_mkdir,
1567 .rmdir = cpuset_rmdir,
1568 .rename = cpuset_rename,
1569};
1570
1571static int cpuset_create_file(struct dentry *dentry, int mode)
1572{
1573 struct inode *inode;
1574
1575 if (!dentry)
1576 return -ENOENT;
1577 if (dentry->d_inode)
1578 return -EEXIST;
1579
1580 inode = cpuset_new_inode(mode);
1581 if (!inode)
1582 return -ENOMEM;
1583
1584 if (S_ISDIR(mode)) {
1585 inode->i_op = &cpuset_dir_inode_operations;
1586 inode->i_fop = &simple_dir_operations;
1587
1588 /* start off with i_nlink == 2 (for "." entry) */
1589 inc_nlink(inode);
1590 } else if (S_ISREG(mode)) {
1591 inode->i_size = 0;
1592 inode->i_fop = &cpuset_file_operations;
1593 }
1594
1595 d_instantiate(dentry, inode);
1596 dget(dentry); /* Extra count - pin the dentry in core */
1597 return 0;
1598}
1599
1600/*
1601 * cpuset_create_dir - create a directory for an object.
1602 * cs: the cpuset we create the directory for.
1603 * It must have a valid ->parent field
1604 * And we are going to fill its ->dentry field.
1605 * name: The name to give to the cpuset directory. Will be copied.
1606 * mode: mode to set on new directory.
1607 */
1608
1609static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1610{
1611 struct dentry *dentry = NULL;
1612 struct dentry *parent;
1613 int error = 0;
1614
1615 parent = cs->parent->dentry;
1616 dentry = cpuset_get_dentry(parent, name);
1617 if (IS_ERR(dentry))
1618 return PTR_ERR(dentry);
1619 error = cpuset_create_file(dentry, S_IFDIR | mode);
1620 if (!error) {
1621 dentry->d_fsdata = cs;
1622 inc_nlink(parent->d_inode);
1623 cs->dentry = dentry;
1624 }
1625 dput(dentry);
1626
1627 return error;
1628}
1629
1630static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1631{
1632 struct dentry *dentry;
1633 int error;
1634
1635 mutex_lock(&dir->d_inode->i_mutex);
1636 dentry = cpuset_get_dentry(dir, cft->name);
1637 if (!IS_ERR(dentry)) {
1638 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1639 if (!error)
1640 dentry->d_fsdata = (void *)cft;
1641 dput(dentry);
1642 } else
1643 error = PTR_ERR(dentry);
1644 mutex_unlock(&dir->d_inode->i_mutex);
1645 return error;
1646}
1647
1648/*
1649 * Stuff for reading the 'tasks' file.
1650 *
1651 * Reading this file can return large amounts of data if a cpuset has
1652 * *lots* of attached tasks. So it may need several calls to read(),
1653 * but we cannot guarantee that the information we produce is correct
1654 * unless we produce it entirely atomically.
1655 *
1656 * Upon tasks file open(), a struct ctr_struct is allocated, that
1657 * will have a pointer to an array (also allocated here). The struct
1658 * ctr_struct * is stored in file->private_data. Its resources will
1659 * be freed by release() when the file is closed. The array is used
1660 * to sprintf the PIDs and then used by read().
1661 */
1662
1663/* cpusets_tasks_read array */
1664
1665struct ctr_struct {
1666 char *buf;
1667 int bufsz;
1668};
1669
1670/*
1671 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1672 * Return actual number of pids loaded. No need to task_lock(p)
1673 * when reading out p->cpuset, as we don't really care if it changes
1674 * on the next cycle, and we are not going to try to dereference it.
1675 */
1676static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1677{
1678 int n = 0;
1679 struct task_struct *g, *p;
1680
1681 read_lock(&tasklist_lock);
1682
1683 do_each_thread(g, p) {
1684 if (p->cpuset == cs) {
1685 if (unlikely(n == npids))
1686 goto array_full;
1687 pidarray[n++] = p->pid;
1688 }
1689 } while_each_thread(g, p);
1690
1691array_full:
1692 read_unlock(&tasklist_lock);
1693 return n;
1694}
1695
1696static int cmppid(const void *a, const void *b)
1697{
1698 return *(pid_t *)a - *(pid_t *)b;
1699}
1700
1701/*
1702 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1703 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1704 * count 'cnt' of how many chars would be written if buf were large enough.
1705 */
1706static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1707{
1708 int cnt = 0;
1709 int i;
1710
1711 for (i = 0; i < npids; i++)
1712 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1713 return cnt;
1714}
1715
1716/*
1717 * Handle an open on 'tasks' file. Prepare a buffer listing the
1718 * process id's of tasks currently attached to the cpuset being opened.
1719 *
1720 * Does not require any specific cpuset mutexes, and does not take any.
1721 */
1722static int cpuset_tasks_open(struct inode *unused, struct file *file)
1723{
1724 struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
1725 struct ctr_struct *ctr;
1726 pid_t *pidarray;
1727 int npids;
1728 char c;
1729
1730 if (!(file->f_mode & FMODE_READ))
1731 return 0;
1732
1733 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1734 if (!ctr)
1735 goto err0;
1736
1737 /*
1738 * If cpuset gets more users after we read count, we won't have
1739 * enough space - tough. This race is indistinguishable to the
1740 * caller from the case that the additional cpuset users didn't
1741 * show up until sometime later on.
1742 */
1743 npids = atomic_read(&cs->count);
1744 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1745 if (!pidarray)
1746 goto err1;
1747
1748 npids = pid_array_load(pidarray, npids, cs);
1749 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1750
1751 /* Call pid_array_to_buf() twice, first just to get bufsz */
1752 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1753 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1754 if (!ctr->buf)
1755 goto err2;
1756 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1757
1758 kfree(pidarray);
1759 file->private_data = ctr;
1760 return 0;
1761
1762err2:
1763 kfree(pidarray);
1764err1:
1765 kfree(ctr);
1766err0:
1767 return -ENOMEM;
1768}
1769
1770static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1771 size_t nbytes, loff_t *ppos)
1772{
1773 struct ctr_struct *ctr = file->private_data;
1774
1775 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
1776}
1777
1778static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1779{
1780 struct ctr_struct *ctr;
1781
1782 if (file->f_mode & FMODE_READ) {
1783 ctr = file->private_data;
1784 kfree(ctr->buf);
1785 kfree(ctr);
1786 }
1787 return 0;
1788}
1789 1432
1790/* 1433/*
1791 * for the common functions, 'private' gives the type of file 1434 * for the common functions, 'private' gives the type of file
1792 */ 1435 */
1793 1436
1794static struct cftype cft_tasks = {
1795 .name = "tasks",
1796 .open = cpuset_tasks_open,
1797 .read = cpuset_tasks_read,
1798 .release = cpuset_tasks_release,
1799 .private = FILE_TASKLIST,
1800};
1801
1802static struct cftype cft_cpus = { 1437static struct cftype cft_cpus = {
1803 .name = "cpus", 1438 .name = "cpus",
1439 .read = cpuset_common_file_read,
1440 .write = cpuset_common_file_write,
1804 .private = FILE_CPULIST, 1441 .private = FILE_CPULIST,
1805}; 1442};
1806 1443
1807static struct cftype cft_mems = { 1444static struct cftype cft_mems = {
1808 .name = "mems", 1445 .name = "mems",
1446 .read = cpuset_common_file_read,
1447 .write = cpuset_common_file_write,
1809 .private = FILE_MEMLIST, 1448 .private = FILE_MEMLIST,
1810}; 1449};
1811 1450
1812static struct cftype cft_cpu_exclusive = { 1451static struct cftype cft_cpu_exclusive = {
1813 .name = "cpu_exclusive", 1452 .name = "cpu_exclusive",
1453 .read = cpuset_common_file_read,
1454 .write = cpuset_common_file_write,
1814 .private = FILE_CPU_EXCLUSIVE, 1455 .private = FILE_CPU_EXCLUSIVE,
1815}; 1456};
1816 1457
1817static struct cftype cft_mem_exclusive = { 1458static struct cftype cft_mem_exclusive = {
1818 .name = "mem_exclusive", 1459 .name = "mem_exclusive",
1460 .read = cpuset_common_file_read,
1461 .write = cpuset_common_file_write,
1819 .private = FILE_MEM_EXCLUSIVE, 1462 .private = FILE_MEM_EXCLUSIVE,
1820}; 1463};
1821 1464
1822static struct cftype cft_notify_on_release = { 1465static struct cftype cft_sched_load_balance = {
1823 .name = "notify_on_release", 1466 .name = "sched_load_balance",
1824 .private = FILE_NOTIFY_ON_RELEASE, 1467 .read = cpuset_common_file_read,
1468 .write = cpuset_common_file_write,
1469 .private = FILE_SCHED_LOAD_BALANCE,
1825}; 1470};
1826 1471
1827static struct cftype cft_memory_migrate = { 1472static struct cftype cft_memory_migrate = {
1828 .name = "memory_migrate", 1473 .name = "memory_migrate",
1474 .read = cpuset_common_file_read,
1475 .write = cpuset_common_file_write,
1829 .private = FILE_MEMORY_MIGRATE, 1476 .private = FILE_MEMORY_MIGRATE,
1830}; 1477};
1831 1478
1832static struct cftype cft_memory_pressure_enabled = { 1479static struct cftype cft_memory_pressure_enabled = {
1833 .name = "memory_pressure_enabled", 1480 .name = "memory_pressure_enabled",
1481 .read = cpuset_common_file_read,
1482 .write = cpuset_common_file_write,
1834 .private = FILE_MEMORY_PRESSURE_ENABLED, 1483 .private = FILE_MEMORY_PRESSURE_ENABLED,
1835}; 1484};
1836 1485
1837static struct cftype cft_memory_pressure = { 1486static struct cftype cft_memory_pressure = {
1838 .name = "memory_pressure", 1487 .name = "memory_pressure",
1488 .read = cpuset_common_file_read,
1489 .write = cpuset_common_file_write,
1839 .private = FILE_MEMORY_PRESSURE, 1490 .private = FILE_MEMORY_PRESSURE,
1840}; 1491};
1841 1492
1842static struct cftype cft_spread_page = { 1493static struct cftype cft_spread_page = {
1843 .name = "memory_spread_page", 1494 .name = "memory_spread_page",
1495 .read = cpuset_common_file_read,
1496 .write = cpuset_common_file_write,
1844 .private = FILE_SPREAD_PAGE, 1497 .private = FILE_SPREAD_PAGE,
1845}; 1498};
1846 1499
1847static struct cftype cft_spread_slab = { 1500static struct cftype cft_spread_slab = {
1848 .name = "memory_spread_slab", 1501 .name = "memory_spread_slab",
1502 .read = cpuset_common_file_read,
1503 .write = cpuset_common_file_write,
1849 .private = FILE_SPREAD_SLAB, 1504 .private = FILE_SPREAD_SLAB,
1850}; 1505};
1851 1506
1852static int cpuset_populate_dir(struct dentry *cs_dentry) 1507static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
1853{ 1508{
1854 int err; 1509 int err;
1855 1510
1856 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0) 1511 if ((err = cgroup_add_file(cont, ss, &cft_cpus)) < 0)
1857 return err;
1858 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1859 return err; 1512 return err;
1860 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0) 1513 if ((err = cgroup_add_file(cont, ss, &cft_mems)) < 0)
1861 return err; 1514 return err;
1862 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0) 1515 if ((err = cgroup_add_file(cont, ss, &cft_cpu_exclusive)) < 0)
1863 return err; 1516 return err;
1864 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0) 1517 if ((err = cgroup_add_file(cont, ss, &cft_mem_exclusive)) < 0)
1865 return err; 1518 return err;
1866 if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0) 1519 if ((err = cgroup_add_file(cont, ss, &cft_memory_migrate)) < 0)
1867 return err; 1520 return err;
1868 if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0) 1521 if ((err = cgroup_add_file(cont, ss, &cft_sched_load_balance)) < 0)
1869 return err; 1522 return err;
1870 if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0) 1523 if ((err = cgroup_add_file(cont, ss, &cft_memory_pressure)) < 0)
1871 return err; 1524 return err;
1872 if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0) 1525 if ((err = cgroup_add_file(cont, ss, &cft_spread_page)) < 0)
1873 return err; 1526 return err;
1874 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0) 1527 if ((err = cgroup_add_file(cont, ss, &cft_spread_slab)) < 0)
1875 return err; 1528 return err;
1529 /* memory_pressure_enabled is in root cpuset only */
1530 if (err == 0 && !cont->parent)
1531 err = cgroup_add_file(cont, ss,
1532 &cft_memory_pressure_enabled);
1876 return 0; 1533 return 0;
1877} 1534}
1878 1535
1879/* 1536/*
1537 * post_clone() is called at the end of cgroup_clone().
1538 * 'cgroup' was just created automatically as a result of
1539 * a cgroup_clone(), and the current task is about to
1540 * be moved into 'cgroup'.
1541 *
1542 * Currently we refuse to set up the cgroup - thereby
1543 * refusing the task to be entered, and as a result refusing
1544 * the sys_unshare() or clone() which initiated it - if any
1545 * sibling cpusets have exclusive cpus or mem.
1546 *
1547 * If this becomes a problem for some users who wish to
1548 * allow that scenario, then cpuset_post_clone() could be
1549 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
1550 * (and likewise for mems) to the new cgroup.
1551 */
1552static void cpuset_post_clone(struct cgroup_subsys *ss,
1553 struct cgroup *cgroup)
1554{
1555 struct cgroup *parent, *child;
1556 struct cpuset *cs, *parent_cs;
1557
1558 parent = cgroup->parent;
1559 list_for_each_entry(child, &parent->children, sibling) {
1560 cs = cgroup_cs(child);
1561 if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
1562 return;
1563 }
1564 cs = cgroup_cs(cgroup);
1565 parent_cs = cgroup_cs(parent);
1566
1567 cs->mems_allowed = parent_cs->mems_allowed;
1568 cs->cpus_allowed = parent_cs->cpus_allowed;
1569 return;
1570}
1571
1572/*
1880 * cpuset_create - create a cpuset 1573 * cpuset_create - create a cpuset
1881 * parent: cpuset that will be parent of the new cpuset. 1574 * parent: cpuset that will be parent of the new cpuset.
1882 * name: name of the new cpuset. Will be strcpy'ed. 1575 * name: name of the new cpuset. Will be strcpy'ed.
@@ -1885,124 +1578,77 @@ static int cpuset_populate_dir(struct dentry *cs_dentry)
1885 * Must be called with the mutex on the parent inode held 1578 * Must be called with the mutex on the parent inode held
1886 */ 1579 */
1887 1580
1888static long cpuset_create(struct cpuset *parent, const char *name, int mode) 1581static struct cgroup_subsys_state *cpuset_create(
1582 struct cgroup_subsys *ss,
1583 struct cgroup *cont)
1889{ 1584{
1890 struct cpuset *cs; 1585 struct cpuset *cs;
1891 int err; 1586 struct cpuset *parent;
1892 1587
1588 if (!cont->parent) {
1589 /* This is early initialization for the top cgroup */
1590 top_cpuset.mems_generation = cpuset_mems_generation++;
1591 return &top_cpuset.css;
1592 }
1593 parent = cgroup_cs(cont->parent);
1893 cs = kmalloc(sizeof(*cs), GFP_KERNEL); 1594 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1894 if (!cs) 1595 if (!cs)
1895 return -ENOMEM; 1596 return ERR_PTR(-ENOMEM);
1896 1597
1897 mutex_lock(&manage_mutex);
1898 cpuset_update_task_memory_state(); 1598 cpuset_update_task_memory_state();
1899 cs->flags = 0; 1599 cs->flags = 0;
1900 if (notify_on_release(parent))
1901 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
1902 if (is_spread_page(parent)) 1600 if (is_spread_page(parent))
1903 set_bit(CS_SPREAD_PAGE, &cs->flags); 1601 set_bit(CS_SPREAD_PAGE, &cs->flags);
1904 if (is_spread_slab(parent)) 1602 if (is_spread_slab(parent))
1905 set_bit(CS_SPREAD_SLAB, &cs->flags); 1603 set_bit(CS_SPREAD_SLAB, &cs->flags);
1604 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
1906 cs->cpus_allowed = CPU_MASK_NONE; 1605 cs->cpus_allowed = CPU_MASK_NONE;
1907 cs->mems_allowed = NODE_MASK_NONE; 1606 cs->mems_allowed = NODE_MASK_NONE;
1908 atomic_set(&cs->count, 0);
1909 INIT_LIST_HEAD(&cs->sibling);
1910 INIT_LIST_HEAD(&cs->children);
1911 cs->mems_generation = cpuset_mems_generation++; 1607 cs->mems_generation = cpuset_mems_generation++;
1912 fmeter_init(&cs->fmeter); 1608 fmeter_init(&cs->fmeter);
1913 1609
1914 cs->parent = parent; 1610 cs->parent = parent;
1915
1916 mutex_lock(&callback_mutex);
1917 list_add(&cs->sibling, &cs->parent->children);
1918 number_of_cpusets++; 1611 number_of_cpusets++;
1919 mutex_unlock(&callback_mutex); 1612 return &cs->css ;
1920
1921 err = cpuset_create_dir(cs, name, mode);
1922 if (err < 0)
1923 goto err;
1924
1925 /*
1926 * Release manage_mutex before cpuset_populate_dir() because it
1927 * will down() this new directory's i_mutex and if we race with
1928 * another mkdir, we might deadlock.
1929 */
1930 mutex_unlock(&manage_mutex);
1931
1932 err = cpuset_populate_dir(cs->dentry);
1933 /* If err < 0, we have a half-filled directory - oh well ;) */
1934 return 0;
1935err:
1936 list_del(&cs->sibling);
1937 mutex_unlock(&manage_mutex);
1938 kfree(cs);
1939 return err;
1940}
1941
1942static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1943{
1944 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1945
1946 /* the vfs holds inode->i_mutex already */
1947 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1948} 1613}
1949 1614
1950/* 1615/*
1951 * Locking note on the strange update_flag() call below: 1616 * Locking note on the strange update_flag() call below:
1952 * 1617 *
1953 * If the cpuset being removed is marked cpu_exclusive, then simulate 1618 * If the cpuset being removed has its flag 'sched_load_balance'
1954 * turning cpu_exclusive off, which will call update_cpu_domains(). 1619 * enabled, then simulate turning sched_load_balance off, which
1955 * The lock_cpu_hotplug() call in update_cpu_domains() must not be 1620 * will call rebuild_sched_domains(). The lock_cpu_hotplug()
1956 * made while holding callback_mutex. Elsewhere the kernel nests 1621 * call in rebuild_sched_domains() must not be made while holding
1957 * callback_mutex inside lock_cpu_hotplug() calls. So the reverse 1622 * callback_mutex. Elsewhere the kernel nests callback_mutex inside
1958 * nesting would risk an ABBA deadlock. 1623 * lock_cpu_hotplug() calls. So the reverse nesting would risk an
1624 * ABBA deadlock.
1959 */ 1625 */
1960 1626
1961static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry) 1627static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
1962{ 1628{
1963 struct cpuset *cs = dentry->d_fsdata; 1629 struct cpuset *cs = cgroup_cs(cont);
1964 struct dentry *d;
1965 struct cpuset *parent;
1966 char *pathbuf = NULL;
1967
1968 /* the vfs holds both inode->i_mutex already */
1969 1630
1970 mutex_lock(&manage_mutex);
1971 cpuset_update_task_memory_state(); 1631 cpuset_update_task_memory_state();
1972 if (atomic_read(&cs->count) > 0) { 1632
1973 mutex_unlock(&manage_mutex); 1633 if (is_sched_load_balance(cs))
1974 return -EBUSY; 1634 update_flag(CS_SCHED_LOAD_BALANCE, cs, "0");
1975 } 1635
1976 if (!list_empty(&cs->children)) {
1977 mutex_unlock(&manage_mutex);
1978 return -EBUSY;
1979 }
1980 if (is_cpu_exclusive(cs)) {
1981 int retval = update_flag(CS_CPU_EXCLUSIVE, cs, "0");
1982 if (retval < 0) {
1983 mutex_unlock(&manage_mutex);
1984 return retval;
1985 }
1986 }
1987 parent = cs->parent;
1988 mutex_lock(&callback_mutex);
1989 set_bit(CS_REMOVED, &cs->flags);
1990 list_del(&cs->sibling); /* delete my sibling from parent->children */
1991 spin_lock(&cs->dentry->d_lock);
1992 d = dget(cs->dentry);
1993 cs->dentry = NULL;
1994 spin_unlock(&d->d_lock);
1995 cpuset_d_remove_dir(d);
1996 dput(d);
1997 number_of_cpusets--; 1636 number_of_cpusets--;
1998 mutex_unlock(&callback_mutex); 1637 kfree(cs);
1999 if (list_empty(&parent->children))
2000 check_for_release(parent, &pathbuf);
2001 mutex_unlock(&manage_mutex);
2002 cpuset_release_agent(pathbuf);
2003 return 0;
2004} 1638}
2005 1639
1640struct cgroup_subsys cpuset_subsys = {
1641 .name = "cpuset",
1642 .create = cpuset_create,
1643 .destroy = cpuset_destroy,
1644 .can_attach = cpuset_can_attach,
1645 .attach = cpuset_attach,
1646 .populate = cpuset_populate,
1647 .post_clone = cpuset_post_clone,
1648 .subsys_id = cpuset_subsys_id,
1649 .early_init = 1,
1650};
1651
2006/* 1652/*
2007 * cpuset_init_early - just enough so that the calls to 1653 * cpuset_init_early - just enough so that the calls to
2008 * cpuset_update_task_memory_state() in early init code 1654 * cpuset_update_task_memory_state() in early init code
@@ -2011,13 +1657,11 @@ static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
2011 1657
2012int __init cpuset_init_early(void) 1658int __init cpuset_init_early(void)
2013{ 1659{
2014 struct task_struct *tsk = current; 1660 top_cpuset.mems_generation = cpuset_mems_generation++;
2015
2016 tsk->cpuset = &top_cpuset;
2017 tsk->cpuset->mems_generation = cpuset_mems_generation++;
2018 return 0; 1661 return 0;
2019} 1662}
2020 1663
1664
2021/** 1665/**
2022 * cpuset_init - initialize cpusets at system boot 1666 * cpuset_init - initialize cpusets at system boot
2023 * 1667 *
@@ -2026,39 +1670,21 @@ int __init cpuset_init_early(void)
2026 1670
2027int __init cpuset_init(void) 1671int __init cpuset_init(void)
2028{ 1672{
2029 struct dentry *root; 1673 int err = 0;
2030 int err;
2031 1674
2032 top_cpuset.cpus_allowed = CPU_MASK_ALL; 1675 top_cpuset.cpus_allowed = CPU_MASK_ALL;
2033 top_cpuset.mems_allowed = NODE_MASK_ALL; 1676 top_cpuset.mems_allowed = NODE_MASK_ALL;
2034 1677
2035 fmeter_init(&top_cpuset.fmeter); 1678 fmeter_init(&top_cpuset.fmeter);
2036 top_cpuset.mems_generation = cpuset_mems_generation++; 1679 top_cpuset.mems_generation = cpuset_mems_generation++;
2037 1680 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
2038 init_task.cpuset = &top_cpuset;
2039 1681
2040 err = register_filesystem(&cpuset_fs_type); 1682 err = register_filesystem(&cpuset_fs_type);
2041 if (err < 0) 1683 if (err < 0)
2042 goto out; 1684 return err;
2043 cpuset_mount = kern_mount(&cpuset_fs_type); 1685
2044 if (IS_ERR(cpuset_mount)) {
2045 printk(KERN_ERR "cpuset: could not mount!\n");
2046 err = PTR_ERR(cpuset_mount);
2047 cpuset_mount = NULL;
2048 goto out;
2049 }
2050 root = cpuset_mount->mnt_sb->s_root;
2051 root->d_fsdata = &top_cpuset;
2052 inc_nlink(root->d_inode);
2053 top_cpuset.dentry = root;
2054 root->d_inode->i_op = &cpuset_dir_inode_operations;
2055 number_of_cpusets = 1; 1686 number_of_cpusets = 1;
2056 err = cpuset_populate_dir(root); 1687 return 0;
2057 /* memory_pressure_enabled is in root cpuset only */
2058 if (err == 0)
2059 err = cpuset_add_file(root, &cft_memory_pressure_enabled);
2060out:
2061 return err;
2062} 1688}
2063 1689
2064/* 1690/*
@@ -2084,10 +1710,12 @@ out:
2084 1710
2085static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur) 1711static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
2086{ 1712{
1713 struct cgroup *cont;
2087 struct cpuset *c; 1714 struct cpuset *c;
2088 1715
2089 /* Each of our child cpusets mems must be online */ 1716 /* Each of our child cpusets mems must be online */
2090 list_for_each_entry(c, &cur->children, sibling) { 1717 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
1718 c = cgroup_cs(cont);
2091 guarantee_online_cpus_mems_in_subtree(c); 1719 guarantee_online_cpus_mems_in_subtree(c);
2092 if (!cpus_empty(c->cpus_allowed)) 1720 if (!cpus_empty(c->cpus_allowed))
2093 guarantee_online_cpus(c, &c->cpus_allowed); 1721 guarantee_online_cpus(c, &c->cpus_allowed);
@@ -2098,8 +1726,9 @@ static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
2098 1726
2099/* 1727/*
2100 * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track 1728 * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track
2101 * cpu_online_map and node_online_map. Force the top cpuset to track 1729 * cpu_online_map and node_states[N_HIGH_MEMORY]. Force the top cpuset to
2102 * whats online after any CPU or memory node hotplug or unplug event. 1730 * track what's online after any CPU or memory node hotplug or unplug
1731 * event.
2103 * 1732 *
2104 * To ensure that we don't remove a CPU or node from the top cpuset 1733 * To ensure that we don't remove a CPU or node from the top cpuset
2105 * that is currently in use by a child cpuset (which would violate 1734 * that is currently in use by a child cpuset (which would violate
@@ -2114,15 +1743,15 @@ static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
2114 1743
2115static void common_cpu_mem_hotplug_unplug(void) 1744static void common_cpu_mem_hotplug_unplug(void)
2116{ 1745{
2117 mutex_lock(&manage_mutex); 1746 cgroup_lock();
2118 mutex_lock(&callback_mutex); 1747 mutex_lock(&callback_mutex);
2119 1748
2120 guarantee_online_cpus_mems_in_subtree(&top_cpuset); 1749 guarantee_online_cpus_mems_in_subtree(&top_cpuset);
2121 top_cpuset.cpus_allowed = cpu_online_map; 1750 top_cpuset.cpus_allowed = cpu_online_map;
2122 top_cpuset.mems_allowed = node_online_map; 1751 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
2123 1752
2124 mutex_unlock(&callback_mutex); 1753 mutex_unlock(&callback_mutex);
2125 mutex_unlock(&manage_mutex); 1754 cgroup_unlock();
2126} 1755}
2127 1756
2128/* 1757/*
@@ -2135,8 +1764,8 @@ static void common_cpu_mem_hotplug_unplug(void)
2135 * cpu_online_map on each CPU hotplug (cpuhp) event. 1764 * cpu_online_map on each CPU hotplug (cpuhp) event.
2136 */ 1765 */
2137 1766
2138static int cpuset_handle_cpuhp(struct notifier_block *nb, 1767static int cpuset_handle_cpuhp(struct notifier_block *unused_nb,
2139 unsigned long phase, void *cpu) 1768 unsigned long phase, void *unused_cpu)
2140{ 1769{
2141 if (phase == CPU_DYING || phase == CPU_DYING_FROZEN) 1770 if (phase == CPU_DYING || phase == CPU_DYING_FROZEN)
2142 return NOTIFY_DONE; 1771 return NOTIFY_DONE;
@@ -2147,8 +1776,9 @@ static int cpuset_handle_cpuhp(struct notifier_block *nb,
2147 1776
2148#ifdef CONFIG_MEMORY_HOTPLUG 1777#ifdef CONFIG_MEMORY_HOTPLUG
2149/* 1778/*
2150 * Keep top_cpuset.mems_allowed tracking node_online_map. 1779 * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
2151 * Call this routine anytime after you change node_online_map. 1780 * Call this routine anytime after you change
1781 * node_states[N_HIGH_MEMORY].
2152 * See also the previous routine cpuset_handle_cpuhp(). 1782 * See also the previous routine cpuset_handle_cpuhp().
2153 */ 1783 */
2154 1784
@@ -2167,115 +1797,13 @@ void cpuset_track_online_nodes(void)
2167void __init cpuset_init_smp(void) 1797void __init cpuset_init_smp(void)
2168{ 1798{
2169 top_cpuset.cpus_allowed = cpu_online_map; 1799 top_cpuset.cpus_allowed = cpu_online_map;
2170 top_cpuset.mems_allowed = node_online_map; 1800 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
2171 1801
2172 hotcpu_notifier(cpuset_handle_cpuhp, 0); 1802 hotcpu_notifier(cpuset_handle_cpuhp, 0);
2173} 1803}
2174 1804
2175/** 1805/**
2176 * cpuset_fork - attach newly forked task to its parents cpuset.
2177 * @tsk: pointer to task_struct of forking parent process.
2178 *
2179 * Description: A task inherits its parent's cpuset at fork().
2180 *
2181 * A pointer to the shared cpuset was automatically copied in fork.c
2182 * by dup_task_struct(). However, we ignore that copy, since it was
2183 * not made under the protection of task_lock(), so might no longer be
2184 * a valid cpuset pointer. attach_task() might have already changed
2185 * current->cpuset, allowing the previously referenced cpuset to
2186 * be removed and freed. Instead, we task_lock(current) and copy
2187 * its present value of current->cpuset for our freshly forked child.
2188 *
2189 * At the point that cpuset_fork() is called, 'current' is the parent
2190 * task, and the passed argument 'child' points to the child task.
2191 **/
2192
2193void cpuset_fork(struct task_struct *child)
2194{
2195 task_lock(current);
2196 child->cpuset = current->cpuset;
2197 atomic_inc(&child->cpuset->count);
2198 task_unlock(current);
2199}
2200
2201/**
2202 * cpuset_exit - detach cpuset from exiting task
2203 * @tsk: pointer to task_struct of exiting process
2204 *
2205 * Description: Detach cpuset from @tsk and release it.
2206 *
2207 * Note that cpusets marked notify_on_release force every task in
2208 * them to take the global manage_mutex mutex when exiting.
2209 * This could impact scaling on very large systems. Be reluctant to
2210 * use notify_on_release cpusets where very high task exit scaling
2211 * is required on large systems.
2212 *
2213 * Don't even think about derefencing 'cs' after the cpuset use count
2214 * goes to zero, except inside a critical section guarded by manage_mutex
2215 * or callback_mutex. Otherwise a zero cpuset use count is a license to
2216 * any other task to nuke the cpuset immediately, via cpuset_rmdir().
2217 *
2218 * This routine has to take manage_mutex, not callback_mutex, because
2219 * it is holding that mutex while calling check_for_release(),
2220 * which calls kmalloc(), so can't be called holding callback_mutex().
2221 *
2222 * the_top_cpuset_hack:
2223 *
2224 * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
2225 *
2226 * Don't leave a task unable to allocate memory, as that is an
2227 * accident waiting to happen should someone add a callout in
2228 * do_exit() after the cpuset_exit() call that might allocate.
2229 * If a task tries to allocate memory with an invalid cpuset,
2230 * it will oops in cpuset_update_task_memory_state().
2231 *
2232 * We call cpuset_exit() while the task is still competent to
2233 * handle notify_on_release(), then leave the task attached to
2234 * the root cpuset (top_cpuset) for the remainder of its exit.
2235 *
2236 * To do this properly, we would increment the reference count on
2237 * top_cpuset, and near the very end of the kernel/exit.c do_exit()
2238 * code we would add a second cpuset function call, to drop that
2239 * reference. This would just create an unnecessary hot spot on
2240 * the top_cpuset reference count, to no avail.
2241 *
2242 * Normally, holding a reference to a cpuset without bumping its
2243 * count is unsafe. The cpuset could go away, or someone could
2244 * attach us to a different cpuset, decrementing the count on
2245 * the first cpuset that we never incremented. But in this case,
2246 * top_cpuset isn't going away, and either task has PF_EXITING set,
2247 * which wards off any attach_task() attempts, or task is a failed
2248 * fork, never visible to attach_task.
2249 *
2250 * Another way to do this would be to set the cpuset pointer
2251 * to NULL here, and check in cpuset_update_task_memory_state()
2252 * for a NULL pointer. This hack avoids that NULL check, for no
2253 * cost (other than this way too long comment ;).
2254 **/
2255
2256void cpuset_exit(struct task_struct *tsk)
2257{
2258 struct cpuset *cs;
2259
2260 task_lock(current);
2261 cs = tsk->cpuset;
2262 tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
2263 task_unlock(current);
2264
2265 if (notify_on_release(cs)) {
2266 char *pathbuf = NULL;
2267
2268 mutex_lock(&manage_mutex);
2269 if (atomic_dec_and_test(&cs->count))
2270 check_for_release(cs, &pathbuf);
2271 mutex_unlock(&manage_mutex);
2272 cpuset_release_agent(pathbuf);
2273 } else {
2274 atomic_dec(&cs->count);
2275 }
2276}
2277 1806
2278/**
2279 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. 1807 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2280 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. 1808 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
2281 * 1809 *
@@ -2290,10 +1818,23 @@ cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
2290 cpumask_t mask; 1818 cpumask_t mask;
2291 1819
2292 mutex_lock(&callback_mutex); 1820 mutex_lock(&callback_mutex);
1821 mask = cpuset_cpus_allowed_locked(tsk);
1822 mutex_unlock(&callback_mutex);
1823
1824 return mask;
1825}
1826
1827/**
1828 * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
1829 * Must be called with callback_mutex held.
1830 **/
1831cpumask_t cpuset_cpus_allowed_locked(struct task_struct *tsk)
1832{
1833 cpumask_t mask;
1834
2293 task_lock(tsk); 1835 task_lock(tsk);
2294 guarantee_online_cpus(tsk->cpuset, &mask); 1836 guarantee_online_cpus(task_cs(tsk), &mask);
2295 task_unlock(tsk); 1837 task_unlock(tsk);
2296 mutex_unlock(&callback_mutex);
2297 1838
2298 return mask; 1839 return mask;
2299} 1840}
@@ -2309,7 +1850,7 @@ void cpuset_init_current_mems_allowed(void)
2309 * 1850 *
2310 * Description: Returns the nodemask_t mems_allowed of the cpuset 1851 * Description: Returns the nodemask_t mems_allowed of the cpuset
2311 * attached to the specified @tsk. Guaranteed to return some non-empty 1852 * attached to the specified @tsk. Guaranteed to return some non-empty
2312 * subset of node_online_map, even if this means going outside the 1853 * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
2313 * tasks cpuset. 1854 * tasks cpuset.
2314 **/ 1855 **/
2315 1856
@@ -2319,7 +1860,7 @@ nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2319 1860
2320 mutex_lock(&callback_mutex); 1861 mutex_lock(&callback_mutex);
2321 task_lock(tsk); 1862 task_lock(tsk);
2322 guarantee_online_mems(tsk->cpuset, &mask); 1863 guarantee_online_mems(task_cs(tsk), &mask);
2323 task_unlock(tsk); 1864 task_unlock(tsk);
2324 mutex_unlock(&callback_mutex); 1865 mutex_unlock(&callback_mutex);
2325 1866
@@ -2450,7 +1991,7 @@ int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask)
2450 mutex_lock(&callback_mutex); 1991 mutex_lock(&callback_mutex);
2451 1992
2452 task_lock(current); 1993 task_lock(current);
2453 cs = nearest_exclusive_ancestor(current->cpuset); 1994 cs = nearest_exclusive_ancestor(task_cs(current));
2454 task_unlock(current); 1995 task_unlock(current);
2455 1996
2456 allowed = node_isset(node, cs->mems_allowed); 1997 allowed = node_isset(node, cs->mems_allowed);
@@ -2491,12 +2032,12 @@ int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask)
2491 node = zone_to_nid(z); 2032 node = zone_to_nid(z);
2492 if (node_isset(node, current->mems_allowed)) 2033 if (node_isset(node, current->mems_allowed))
2493 return 1; 2034 return 1;
2494 /* 2035 /*
2495 * Allow tasks that have access to memory reserves because they have 2036 * Allow tasks that have access to memory reserves because they have
2496 * been OOM killed to get memory anywhere. 2037 * been OOM killed to get memory anywhere.
2497 */ 2038 */
2498 if (unlikely(test_thread_flag(TIF_MEMDIE))) 2039 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2499 return 1; 2040 return 1;
2500 return 0; 2041 return 0;
2501} 2042}
2502 2043
@@ -2566,41 +2107,20 @@ int cpuset_mem_spread_node(void)
2566EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); 2107EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2567 2108
2568/** 2109/**
2569 * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors? 2110 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2570 * @p: pointer to task_struct of some other task. 2111 * @tsk1: pointer to task_struct of some task.
2571 * 2112 * @tsk2: pointer to task_struct of some other task.
2572 * Description: Return true if the nearest mem_exclusive ancestor 2113 *
2573 * cpusets of tasks @p and current overlap. Used by oom killer to 2114 * Description: Return true if @tsk1's mems_allowed intersects the
2574 * determine if task @p's memory usage might impact the memory 2115 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2575 * available to the current task. 2116 * one of the task's memory usage might impact the memory available
2576 * 2117 * to the other.
2577 * Call while holding callback_mutex.
2578 **/ 2118 **/
2579 2119
2580int cpuset_excl_nodes_overlap(const struct task_struct *p) 2120int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2121 const struct task_struct *tsk2)
2581{ 2122{
2582 const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */ 2123 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
2583 int overlap = 1; /* do cpusets overlap? */
2584
2585 task_lock(current);
2586 if (current->flags & PF_EXITING) {
2587 task_unlock(current);
2588 goto done;
2589 }
2590 cs1 = nearest_exclusive_ancestor(current->cpuset);
2591 task_unlock(current);
2592
2593 task_lock((struct task_struct *)p);
2594 if (p->flags & PF_EXITING) {
2595 task_unlock((struct task_struct *)p);
2596 goto done;
2597 }
2598 cs2 = nearest_exclusive_ancestor(p->cpuset);
2599 task_unlock((struct task_struct *)p);
2600
2601 overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
2602done:
2603 return overlap;
2604} 2124}
2605 2125
2606/* 2126/*
@@ -2631,14 +2151,12 @@ int cpuset_memory_pressure_enabled __read_mostly;
2631 2151
2632void __cpuset_memory_pressure_bump(void) 2152void __cpuset_memory_pressure_bump(void)
2633{ 2153{
2634 struct cpuset *cs;
2635
2636 task_lock(current); 2154 task_lock(current);
2637 cs = current->cpuset; 2155 fmeter_markevent(&task_cs(current)->fmeter);
2638 fmeter_markevent(&cs->fmeter);
2639 task_unlock(current); 2156 task_unlock(current);
2640} 2157}
2641 2158
2159#ifdef CONFIG_PROC_PID_CPUSET
2642/* 2160/*
2643 * proc_cpuset_show() 2161 * proc_cpuset_show()
2644 * - Print tasks cpuset path into seq_file. 2162 * - Print tasks cpuset path into seq_file.
@@ -2650,11 +2168,12 @@ void __cpuset_memory_pressure_bump(void)
2650 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks 2168 * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
2651 * cpuset to top_cpuset. 2169 * cpuset to top_cpuset.
2652 */ 2170 */
2653static int proc_cpuset_show(struct seq_file *m, void *v) 2171static int proc_cpuset_show(struct seq_file *m, void *unused_v)
2654{ 2172{
2655 struct pid *pid; 2173 struct pid *pid;
2656 struct task_struct *tsk; 2174 struct task_struct *tsk;
2657 char *buf; 2175 char *buf;
2176 struct cgroup_subsys_state *css;
2658 int retval; 2177 int retval;
2659 2178
2660 retval = -ENOMEM; 2179 retval = -ENOMEM;
@@ -2669,15 +2188,15 @@ static int proc_cpuset_show(struct seq_file *m, void *v)
2669 goto out_free; 2188 goto out_free;
2670 2189
2671 retval = -EINVAL; 2190 retval = -EINVAL;
2672 mutex_lock(&manage_mutex); 2191 cgroup_lock();
2673 2192 css = task_subsys_state(tsk, cpuset_subsys_id);
2674 retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE); 2193 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
2675 if (retval < 0) 2194 if (retval < 0)
2676 goto out_unlock; 2195 goto out_unlock;
2677 seq_puts(m, buf); 2196 seq_puts(m, buf);
2678 seq_putc(m, '\n'); 2197 seq_putc(m, '\n');
2679out_unlock: 2198out_unlock:
2680 mutex_unlock(&manage_mutex); 2199 cgroup_unlock();
2681 put_task_struct(tsk); 2200 put_task_struct(tsk);
2682out_free: 2201out_free:
2683 kfree(buf); 2202 kfree(buf);
@@ -2697,6 +2216,7 @@ const struct file_operations proc_cpuset_operations = {
2697 .llseek = seq_lseek, 2216 .llseek = seq_lseek,
2698 .release = single_release, 2217 .release = single_release,
2699}; 2218};
2219#endif /* CONFIG_PROC_PID_CPUSET */
2700 2220
2701/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ 2221/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
2702char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) 2222char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)