[SCSI] aic79xx: Remove busyq
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / cpuset.c
1 /*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
7 * Copyright (C) 2004 Silicon Graphics, Inc.
8 *
9 * Portions derived from Patrick Mochel's sysfs code.
10 * sysfs is Copyright (c) 2001-3 Patrick Mochel
11 * Portions Copyright (c) 2004 Silicon Graphics, Inc.
12 *
13 * 2003-10-10 Written by Simon Derr <simon.derr@bull.net>
14 * 2003-10-22 Updates by Stephen Hemminger.
15 * 2004 May-July Rework by Paul Jackson <pj@sgi.com>
16 *
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file COPYING in the main directory of the Linux
19 * distribution for more details.
20 */
21
22 #include <linux/config.h>
23 #include <linux/cpu.h>
24 #include <linux/cpumask.h>
25 #include <linux/cpuset.h>
26 #include <linux/err.h>
27 #include <linux/errno.h>
28 #include <linux/file.h>
29 #include <linux/fs.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
32 #include <linux/kernel.h>
33 #include <linux/kmod.h>
34 #include <linux/list.h>
35 #include <linux/mm.h>
36 #include <linux/module.h>
37 #include <linux/mount.h>
38 #include <linux/namei.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/sched.h>
42 #include <linux/seq_file.h>
43 #include <linux/slab.h>
44 #include <linux/smp_lock.h>
45 #include <linux/spinlock.h>
46 #include <linux/stat.h>
47 #include <linux/string.h>
48 #include <linux/time.h>
49 #include <linux/backing-dev.h>
50 #include <linux/sort.h>
51
52 #include <asm/uaccess.h>
53 #include <asm/atomic.h>
54 #include <asm/semaphore.h>
55
56 #define CPUSET_SUPER_MAGIC 0x27e0eb
57
58 struct cpuset {
59 unsigned long flags; /* "unsigned long" so bitops work */
60 cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
61 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
62
63 atomic_t count; /* count tasks using this cpuset */
64
65 /*
66 * We link our 'sibling' struct into our parents 'children'.
67 * Our children link their 'sibling' into our 'children'.
68 */
69 struct list_head sibling; /* my parents children */
70 struct list_head children; /* my children */
71
72 struct cpuset *parent; /* my parent */
73 struct dentry *dentry; /* cpuset fs entry */
74
75 /*
76 * Copy of global cpuset_mems_generation as of the most
77 * recent time this cpuset changed its mems_allowed.
78 */
79 int mems_generation;
80 };
81
82 /* bits in struct cpuset flags field */
83 typedef enum {
84 CS_CPU_EXCLUSIVE,
85 CS_MEM_EXCLUSIVE,
86 CS_REMOVED,
87 CS_NOTIFY_ON_RELEASE
88 } cpuset_flagbits_t;
89
90 /* convenient tests for these bits */
91 static inline int is_cpu_exclusive(const struct cpuset *cs)
92 {
93 return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
94 }
95
96 static inline int is_mem_exclusive(const struct cpuset *cs)
97 {
98 return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
99 }
100
101 static inline int is_removed(const struct cpuset *cs)
102 {
103 return !!test_bit(CS_REMOVED, &cs->flags);
104 }
105
106 static inline int notify_on_release(const struct cpuset *cs)
107 {
108 return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
109 }
110
111 /*
112 * Increment this atomic integer everytime any cpuset changes its
113 * mems_allowed value. Users of cpusets can track this generation
114 * number, and avoid having to lock and reload mems_allowed unless
115 * the cpuset they're using changes generation.
116 *
117 * A single, global generation is needed because attach_task() could
118 * reattach a task to a different cpuset, which must not have its
119 * generation numbers aliased with those of that tasks previous cpuset.
120 *
121 * Generations are needed for mems_allowed because one task cannot
122 * modify anothers memory placement. So we must enable every task,
123 * on every visit to __alloc_pages(), to efficiently check whether
124 * its current->cpuset->mems_allowed has changed, requiring an update
125 * of its current->mems_allowed.
126 */
127 static atomic_t cpuset_mems_generation = ATOMIC_INIT(1);
128
129 static struct cpuset top_cpuset = {
130 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
131 .cpus_allowed = CPU_MASK_ALL,
132 .mems_allowed = NODE_MASK_ALL,
133 .count = ATOMIC_INIT(0),
134 .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
135 .children = LIST_HEAD_INIT(top_cpuset.children),
136 .parent = NULL,
137 .dentry = NULL,
138 .mems_generation = 0,
139 };
140
141 static struct vfsmount *cpuset_mount;
142 static struct super_block *cpuset_sb = NULL;
143
144 /*
145 * cpuset_sem should be held by anyone who is depending on the children
146 * or sibling lists of any cpuset, or performing non-atomic operations
147 * on the flags or *_allowed values of a cpuset, such as raising the
148 * CS_REMOVED flag bit iff it is not already raised, or reading and
149 * conditionally modifying the *_allowed values. One kernel global
150 * cpuset semaphore should be sufficient - these things don't change
151 * that much.
152 *
153 * The code that modifies cpusets holds cpuset_sem across the entire
154 * operation, from cpuset_common_file_write() down, single threading
155 * all cpuset modifications (except for counter manipulations from
156 * fork and exit) across the system. This presumes that cpuset
157 * modifications are rare - better kept simple and safe, even if slow.
158 *
159 * The code that reads cpusets, such as in cpuset_common_file_read()
160 * and below, only holds cpuset_sem across small pieces of code, such
161 * as when reading out possibly multi-word cpumasks and nodemasks, as
162 * the risks are less, and the desire for performance a little greater.
163 * The proc_cpuset_show() routine needs to hold cpuset_sem to insure
164 * that no cs->dentry is NULL, as it walks up the cpuset tree to root.
165 *
166 * The hooks from fork and exit, cpuset_fork() and cpuset_exit(), don't
167 * (usually) grab cpuset_sem. These are the two most performance
168 * critical pieces of code here. The exception occurs on exit(),
169 * when a task in a notify_on_release cpuset exits. Then cpuset_sem
170 * is taken, and if the cpuset count is zero, a usermode call made
171 * to /sbin/cpuset_release_agent with the name of the cpuset (path
172 * relative to the root of cpuset file system) as the argument.
173 *
174 * A cpuset can only be deleted if both its 'count' of using tasks is
175 * zero, and its list of 'children' cpusets is empty. Since all tasks
176 * in the system use _some_ cpuset, and since there is always at least
177 * one task in the system (init, pid == 1), therefore, top_cpuset
178 * always has either children cpusets and/or using tasks. So no need
179 * for any special hack to ensure that top_cpuset cannot be deleted.
180 */
181
182 static DECLARE_MUTEX(cpuset_sem);
183
184 /*
185 * A couple of forward declarations required, due to cyclic reference loop:
186 * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
187 * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
188 */
189
190 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
191 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
192
193 static struct backing_dev_info cpuset_backing_dev_info = {
194 .ra_pages = 0, /* No readahead */
195 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
196 };
197
198 static struct inode *cpuset_new_inode(mode_t mode)
199 {
200 struct inode *inode = new_inode(cpuset_sb);
201
202 if (inode) {
203 inode->i_mode = mode;
204 inode->i_uid = current->fsuid;
205 inode->i_gid = current->fsgid;
206 inode->i_blksize = PAGE_CACHE_SIZE;
207 inode->i_blocks = 0;
208 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
209 inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
210 }
211 return inode;
212 }
213
214 static void cpuset_diput(struct dentry *dentry, struct inode *inode)
215 {
216 /* is dentry a directory ? if so, kfree() associated cpuset */
217 if (S_ISDIR(inode->i_mode)) {
218 struct cpuset *cs = dentry->d_fsdata;
219 BUG_ON(!(is_removed(cs)));
220 kfree(cs);
221 }
222 iput(inode);
223 }
224
225 static struct dentry_operations cpuset_dops = {
226 .d_iput = cpuset_diput,
227 };
228
229 static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
230 {
231 struct dentry *d = lookup_one_len(name, parent, strlen(name));
232 if (!IS_ERR(d))
233 d->d_op = &cpuset_dops;
234 return d;
235 }
236
237 static void remove_dir(struct dentry *d)
238 {
239 struct dentry *parent = dget(d->d_parent);
240
241 d_delete(d);
242 simple_rmdir(parent->d_inode, d);
243 dput(parent);
244 }
245
246 /*
247 * NOTE : the dentry must have been dget()'ed
248 */
249 static void cpuset_d_remove_dir(struct dentry *dentry)
250 {
251 struct list_head *node;
252
253 spin_lock(&dcache_lock);
254 node = dentry->d_subdirs.next;
255 while (node != &dentry->d_subdirs) {
256 struct dentry *d = list_entry(node, struct dentry, d_child);
257 list_del_init(node);
258 if (d->d_inode) {
259 d = dget_locked(d);
260 spin_unlock(&dcache_lock);
261 d_delete(d);
262 simple_unlink(dentry->d_inode, d);
263 dput(d);
264 spin_lock(&dcache_lock);
265 }
266 node = dentry->d_subdirs.next;
267 }
268 list_del_init(&dentry->d_child);
269 spin_unlock(&dcache_lock);
270 remove_dir(dentry);
271 }
272
273 static struct super_operations cpuset_ops = {
274 .statfs = simple_statfs,
275 .drop_inode = generic_delete_inode,
276 };
277
278 static int cpuset_fill_super(struct super_block *sb, void *unused_data,
279 int unused_silent)
280 {
281 struct inode *inode;
282 struct dentry *root;
283
284 sb->s_blocksize = PAGE_CACHE_SIZE;
285 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
286 sb->s_magic = CPUSET_SUPER_MAGIC;
287 sb->s_op = &cpuset_ops;
288 cpuset_sb = sb;
289
290 inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
291 if (inode) {
292 inode->i_op = &simple_dir_inode_operations;
293 inode->i_fop = &simple_dir_operations;
294 /* directories start off with i_nlink == 2 (for "." entry) */
295 inode->i_nlink++;
296 } else {
297 return -ENOMEM;
298 }
299
300 root = d_alloc_root(inode);
301 if (!root) {
302 iput(inode);
303 return -ENOMEM;
304 }
305 sb->s_root = root;
306 return 0;
307 }
308
309 static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
310 int flags, const char *unused_dev_name,
311 void *data)
312 {
313 return get_sb_single(fs_type, flags, data, cpuset_fill_super);
314 }
315
316 static struct file_system_type cpuset_fs_type = {
317 .name = "cpuset",
318 .get_sb = cpuset_get_sb,
319 .kill_sb = kill_litter_super,
320 };
321
322 /* struct cftype:
323 *
324 * The files in the cpuset filesystem mostly have a very simple read/write
325 * handling, some common function will take care of it. Nevertheless some cases
326 * (read tasks) are special and therefore I define this structure for every
327 * kind of file.
328 *
329 *
330 * When reading/writing to a file:
331 * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
332 * - the 'cftype' of the file is file->f_dentry->d_fsdata
333 */
334
335 struct cftype {
336 char *name;
337 int private;
338 int (*open) (struct inode *inode, struct file *file);
339 ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
340 loff_t *ppos);
341 int (*write) (struct file *file, const char __user *buf, size_t nbytes,
342 loff_t *ppos);
343 int (*release) (struct inode *inode, struct file *file);
344 };
345
346 static inline struct cpuset *__d_cs(struct dentry *dentry)
347 {
348 return dentry->d_fsdata;
349 }
350
351 static inline struct cftype *__d_cft(struct dentry *dentry)
352 {
353 return dentry->d_fsdata;
354 }
355
356 /*
357 * Call with cpuset_sem held. Writes path of cpuset into buf.
358 * Returns 0 on success, -errno on error.
359 */
360
361 static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
362 {
363 char *start;
364
365 start = buf + buflen;
366
367 *--start = '\0';
368 for (;;) {
369 int len = cs->dentry->d_name.len;
370 if ((start -= len) < buf)
371 return -ENAMETOOLONG;
372 memcpy(start, cs->dentry->d_name.name, len);
373 cs = cs->parent;
374 if (!cs)
375 break;
376 if (!cs->parent)
377 continue;
378 if (--start < buf)
379 return -ENAMETOOLONG;
380 *start = '/';
381 }
382 memmove(buf, start, buf + buflen - start);
383 return 0;
384 }
385
386 /*
387 * Notify userspace when a cpuset is released, by running
388 * /sbin/cpuset_release_agent with the name of the cpuset (path
389 * relative to the root of cpuset file system) as the argument.
390 *
391 * Most likely, this user command will try to rmdir this cpuset.
392 *
393 * This races with the possibility that some other task will be
394 * attached to this cpuset before it is removed, or that some other
395 * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
396 * The presumed 'rmdir' will fail quietly if this cpuset is no longer
397 * unused, and this cpuset will be reprieved from its death sentence,
398 * to continue to serve a useful existence. Next time it's released,
399 * we will get notified again, if it still has 'notify_on_release' set.
400 *
401 * Note final arg to call_usermodehelper() is 0 - that means
402 * don't wait. Since we are holding the global cpuset_sem here,
403 * and we are asking another thread (started from keventd) to rmdir a
404 * cpuset, we can't wait - or we'd deadlock with the removing thread
405 * on cpuset_sem.
406 */
407
408 static int cpuset_release_agent(char *cpuset_str)
409 {
410 char *argv[3], *envp[3];
411 int i;
412
413 i = 0;
414 argv[i++] = "/sbin/cpuset_release_agent";
415 argv[i++] = cpuset_str;
416 argv[i] = NULL;
417
418 i = 0;
419 /* minimal command environment */
420 envp[i++] = "HOME=/";
421 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
422 envp[i] = NULL;
423
424 return call_usermodehelper(argv[0], argv, envp, 0);
425 }
426
427 /*
428 * Either cs->count of using tasks transitioned to zero, or the
429 * cs->children list of child cpusets just became empty. If this
430 * cs is notify_on_release() and now both the user count is zero and
431 * the list of children is empty, send notice to user land.
432 */
433
434 static void check_for_release(struct cpuset *cs)
435 {
436 if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
437 list_empty(&cs->children)) {
438 char *buf;
439
440 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
441 if (!buf)
442 return;
443 if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
444 goto out;
445 cpuset_release_agent(buf);
446 out:
447 kfree(buf);
448 }
449 }
450
451 /*
452 * Return in *pmask the portion of a cpusets's cpus_allowed that
453 * are online. If none are online, walk up the cpuset hierarchy
454 * until we find one that does have some online cpus. If we get
455 * all the way to the top and still haven't found any online cpus,
456 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
457 * task, return cpu_online_map.
458 *
459 * One way or another, we guarantee to return some non-empty subset
460 * of cpu_online_map.
461 *
462 * Call with cpuset_sem held.
463 */
464
465 static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
466 {
467 while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
468 cs = cs->parent;
469 if (cs)
470 cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
471 else
472 *pmask = cpu_online_map;
473 BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
474 }
475
476 /*
477 * Return in *pmask the portion of a cpusets's mems_allowed that
478 * are online. If none are online, walk up the cpuset hierarchy
479 * until we find one that does have some online mems. If we get
480 * all the way to the top and still haven't found any online mems,
481 * return node_online_map.
482 *
483 * One way or another, we guarantee to return some non-empty subset
484 * of node_online_map.
485 *
486 * Call with cpuset_sem held.
487 */
488
489 static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
490 {
491 while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
492 cs = cs->parent;
493 if (cs)
494 nodes_and(*pmask, cs->mems_allowed, node_online_map);
495 else
496 *pmask = node_online_map;
497 BUG_ON(!nodes_intersects(*pmask, node_online_map));
498 }
499
500 /*
501 * Refresh current tasks mems_allowed and mems_generation from
502 * current tasks cpuset. Call with cpuset_sem held.
503 *
504 * Be sure to call refresh_mems() on any cpuset operation which
505 * (1) holds cpuset_sem, and (2) might possibly alloc memory.
506 * Call after obtaining cpuset_sem lock, before any possible
507 * allocation. Otherwise one risks trying to allocate memory
508 * while the task cpuset_mems_generation is not the same as
509 * the mems_generation in its cpuset, which would deadlock on
510 * cpuset_sem in cpuset_update_current_mems_allowed().
511 *
512 * Since we hold cpuset_sem, once refresh_mems() is called, the
513 * test (current->cpuset_mems_generation != cs->mems_generation)
514 * in cpuset_update_current_mems_allowed() will remain false,
515 * until we drop cpuset_sem. Anyone else who would change our
516 * cpusets mems_generation needs to lock cpuset_sem first.
517 */
518
519 static void refresh_mems(void)
520 {
521 struct cpuset *cs = current->cpuset;
522
523 if (current->cpuset_mems_generation != cs->mems_generation) {
524 guarantee_online_mems(cs, &current->mems_allowed);
525 current->cpuset_mems_generation = cs->mems_generation;
526 }
527 }
528
529 /*
530 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
531 *
532 * One cpuset is a subset of another if all its allowed CPUs and
533 * Memory Nodes are a subset of the other, and its exclusive flags
534 * are only set if the other's are set.
535 */
536
537 static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
538 {
539 return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
540 nodes_subset(p->mems_allowed, q->mems_allowed) &&
541 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
542 is_mem_exclusive(p) <= is_mem_exclusive(q);
543 }
544
545 /*
546 * validate_change() - Used to validate that any proposed cpuset change
547 * follows the structural rules for cpusets.
548 *
549 * If we replaced the flag and mask values of the current cpuset
550 * (cur) with those values in the trial cpuset (trial), would
551 * our various subset and exclusive rules still be valid? Presumes
552 * cpuset_sem held.
553 *
554 * 'cur' is the address of an actual, in-use cpuset. Operations
555 * such as list traversal that depend on the actual address of the
556 * cpuset in the list must use cur below, not trial.
557 *
558 * 'trial' is the address of bulk structure copy of cur, with
559 * perhaps one or more of the fields cpus_allowed, mems_allowed,
560 * or flags changed to new, trial values.
561 *
562 * Return 0 if valid, -errno if not.
563 */
564
565 static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
566 {
567 struct cpuset *c, *par;
568
569 /* Each of our child cpusets must be a subset of us */
570 list_for_each_entry(c, &cur->children, sibling) {
571 if (!is_cpuset_subset(c, trial))
572 return -EBUSY;
573 }
574
575 /* Remaining checks don't apply to root cpuset */
576 if ((par = cur->parent) == NULL)
577 return 0;
578
579 /* We must be a subset of our parent cpuset */
580 if (!is_cpuset_subset(trial, par))
581 return -EACCES;
582
583 /* If either I or some sibling (!= me) is exclusive, we can't overlap */
584 list_for_each_entry(c, &par->children, sibling) {
585 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
586 c != cur &&
587 cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
588 return -EINVAL;
589 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
590 c != cur &&
591 nodes_intersects(trial->mems_allowed, c->mems_allowed))
592 return -EINVAL;
593 }
594
595 return 0;
596 }
597
598 /*
599 * For a given cpuset cur, partition the system as follows
600 * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
601 * exclusive child cpusets
602 * b. All cpus in the current cpuset's cpus_allowed that are not part of any
603 * exclusive child cpusets
604 * Build these two partitions by calling partition_sched_domains
605 *
606 * Call with cpuset_sem held. May nest a call to the
607 * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
608 */
609 static void update_cpu_domains(struct cpuset *cur)
610 {
611 struct cpuset *c, *par = cur->parent;
612 cpumask_t pspan, cspan;
613
614 if (par == NULL || cpus_empty(cur->cpus_allowed))
615 return;
616
617 /*
618 * Get all cpus from parent's cpus_allowed not part of exclusive
619 * children
620 */
621 pspan = par->cpus_allowed;
622 list_for_each_entry(c, &par->children, sibling) {
623 if (is_cpu_exclusive(c))
624 cpus_andnot(pspan, pspan, c->cpus_allowed);
625 }
626 if (is_removed(cur) || !is_cpu_exclusive(cur)) {
627 cpus_or(pspan, pspan, cur->cpus_allowed);
628 if (cpus_equal(pspan, cur->cpus_allowed))
629 return;
630 cspan = CPU_MASK_NONE;
631 } else {
632 if (cpus_empty(pspan))
633 return;
634 cspan = cur->cpus_allowed;
635 /*
636 * Get all cpus from current cpuset's cpus_allowed not part
637 * of exclusive children
638 */
639 list_for_each_entry(c, &cur->children, sibling) {
640 if (is_cpu_exclusive(c))
641 cpus_andnot(cspan, cspan, c->cpus_allowed);
642 }
643 }
644
645 lock_cpu_hotplug();
646 partition_sched_domains(&pspan, &cspan);
647 unlock_cpu_hotplug();
648 }
649
650 static int update_cpumask(struct cpuset *cs, char *buf)
651 {
652 struct cpuset trialcs;
653 int retval, cpus_unchanged;
654
655 trialcs = *cs;
656 retval = cpulist_parse(buf, trialcs.cpus_allowed);
657 if (retval < 0)
658 return retval;
659 cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
660 if (cpus_empty(trialcs.cpus_allowed))
661 return -ENOSPC;
662 retval = validate_change(cs, &trialcs);
663 if (retval < 0)
664 return retval;
665 cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
666 cs->cpus_allowed = trialcs.cpus_allowed;
667 if (is_cpu_exclusive(cs) && !cpus_unchanged)
668 update_cpu_domains(cs);
669 return 0;
670 }
671
672 static int update_nodemask(struct cpuset *cs, char *buf)
673 {
674 struct cpuset trialcs;
675 int retval;
676
677 trialcs = *cs;
678 retval = nodelist_parse(buf, trialcs.mems_allowed);
679 if (retval < 0)
680 return retval;
681 nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
682 if (nodes_empty(trialcs.mems_allowed))
683 return -ENOSPC;
684 retval = validate_change(cs, &trialcs);
685 if (retval == 0) {
686 cs->mems_allowed = trialcs.mems_allowed;
687 atomic_inc(&cpuset_mems_generation);
688 cs->mems_generation = atomic_read(&cpuset_mems_generation);
689 }
690 return retval;
691 }
692
693 /*
694 * update_flag - read a 0 or a 1 in a file and update associated flag
695 * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
696 * CS_NOTIFY_ON_RELEASE)
697 * cs: the cpuset to update
698 * buf: the buffer where we read the 0 or 1
699 */
700
701 static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
702 {
703 int turning_on;
704 struct cpuset trialcs;
705 int err, cpu_exclusive_changed;
706
707 turning_on = (simple_strtoul(buf, NULL, 10) != 0);
708
709 trialcs = *cs;
710 if (turning_on)
711 set_bit(bit, &trialcs.flags);
712 else
713 clear_bit(bit, &trialcs.flags);
714
715 err = validate_change(cs, &trialcs);
716 if (err < 0)
717 return err;
718 cpu_exclusive_changed =
719 (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
720 if (turning_on)
721 set_bit(bit, &cs->flags);
722 else
723 clear_bit(bit, &cs->flags);
724
725 if (cpu_exclusive_changed)
726 update_cpu_domains(cs);
727 return 0;
728 }
729
730 static int attach_task(struct cpuset *cs, char *buf)
731 {
732 pid_t pid;
733 struct task_struct *tsk;
734 struct cpuset *oldcs;
735 cpumask_t cpus;
736
737 if (sscanf(buf, "%d", &pid) != 1)
738 return -EIO;
739 if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
740 return -ENOSPC;
741
742 if (pid) {
743 read_lock(&tasklist_lock);
744
745 tsk = find_task_by_pid(pid);
746 if (!tsk) {
747 read_unlock(&tasklist_lock);
748 return -ESRCH;
749 }
750
751 get_task_struct(tsk);
752 read_unlock(&tasklist_lock);
753
754 if ((current->euid) && (current->euid != tsk->uid)
755 && (current->euid != tsk->suid)) {
756 put_task_struct(tsk);
757 return -EACCES;
758 }
759 } else {
760 tsk = current;
761 get_task_struct(tsk);
762 }
763
764 task_lock(tsk);
765 oldcs = tsk->cpuset;
766 if (!oldcs) {
767 task_unlock(tsk);
768 put_task_struct(tsk);
769 return -ESRCH;
770 }
771 atomic_inc(&cs->count);
772 tsk->cpuset = cs;
773 task_unlock(tsk);
774
775 guarantee_online_cpus(cs, &cpus);
776 set_cpus_allowed(tsk, cpus);
777
778 put_task_struct(tsk);
779 if (atomic_dec_and_test(&oldcs->count))
780 check_for_release(oldcs);
781 return 0;
782 }
783
784 /* The various types of files and directories in a cpuset file system */
785
786 typedef enum {
787 FILE_ROOT,
788 FILE_DIR,
789 FILE_CPULIST,
790 FILE_MEMLIST,
791 FILE_CPU_EXCLUSIVE,
792 FILE_MEM_EXCLUSIVE,
793 FILE_NOTIFY_ON_RELEASE,
794 FILE_TASKLIST,
795 } cpuset_filetype_t;
796
797 static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
798 size_t nbytes, loff_t *unused_ppos)
799 {
800 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
801 struct cftype *cft = __d_cft(file->f_dentry);
802 cpuset_filetype_t type = cft->private;
803 char *buffer;
804 int retval = 0;
805
806 /* Crude upper limit on largest legitimate cpulist user might write. */
807 if (nbytes > 100 + 6 * NR_CPUS)
808 return -E2BIG;
809
810 /* +1 for nul-terminator */
811 if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
812 return -ENOMEM;
813
814 if (copy_from_user(buffer, userbuf, nbytes)) {
815 retval = -EFAULT;
816 goto out1;
817 }
818 buffer[nbytes] = 0; /* nul-terminate */
819
820 down(&cpuset_sem);
821
822 if (is_removed(cs)) {
823 retval = -ENODEV;
824 goto out2;
825 }
826
827 switch (type) {
828 case FILE_CPULIST:
829 retval = update_cpumask(cs, buffer);
830 break;
831 case FILE_MEMLIST:
832 retval = update_nodemask(cs, buffer);
833 break;
834 case FILE_CPU_EXCLUSIVE:
835 retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
836 break;
837 case FILE_MEM_EXCLUSIVE:
838 retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
839 break;
840 case FILE_NOTIFY_ON_RELEASE:
841 retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
842 break;
843 case FILE_TASKLIST:
844 retval = attach_task(cs, buffer);
845 break;
846 default:
847 retval = -EINVAL;
848 goto out2;
849 }
850
851 if (retval == 0)
852 retval = nbytes;
853 out2:
854 up(&cpuset_sem);
855 out1:
856 kfree(buffer);
857 return retval;
858 }
859
860 static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
861 size_t nbytes, loff_t *ppos)
862 {
863 ssize_t retval = 0;
864 struct cftype *cft = __d_cft(file->f_dentry);
865 if (!cft)
866 return -ENODEV;
867
868 /* special function ? */
869 if (cft->write)
870 retval = cft->write(file, buf, nbytes, ppos);
871 else
872 retval = cpuset_common_file_write(file, buf, nbytes, ppos);
873
874 return retval;
875 }
876
877 /*
878 * These ascii lists should be read in a single call, by using a user
879 * buffer large enough to hold the entire map. If read in smaller
880 * chunks, there is no guarantee of atomicity. Since the display format
881 * used, list of ranges of sequential numbers, is variable length,
882 * and since these maps can change value dynamically, one could read
883 * gibberish by doing partial reads while a list was changing.
884 * A single large read to a buffer that crosses a page boundary is
885 * ok, because the result being copied to user land is not recomputed
886 * across a page fault.
887 */
888
889 static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
890 {
891 cpumask_t mask;
892
893 down(&cpuset_sem);
894 mask = cs->cpus_allowed;
895 up(&cpuset_sem);
896
897 return cpulist_scnprintf(page, PAGE_SIZE, mask);
898 }
899
900 static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
901 {
902 nodemask_t mask;
903
904 down(&cpuset_sem);
905 mask = cs->mems_allowed;
906 up(&cpuset_sem);
907
908 return nodelist_scnprintf(page, PAGE_SIZE, mask);
909 }
910
911 static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
912 size_t nbytes, loff_t *ppos)
913 {
914 struct cftype *cft = __d_cft(file->f_dentry);
915 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
916 cpuset_filetype_t type = cft->private;
917 char *page;
918 ssize_t retval = 0;
919 char *s;
920 char *start;
921 size_t n;
922
923 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
924 return -ENOMEM;
925
926 s = page;
927
928 switch (type) {
929 case FILE_CPULIST:
930 s += cpuset_sprintf_cpulist(s, cs);
931 break;
932 case FILE_MEMLIST:
933 s += cpuset_sprintf_memlist(s, cs);
934 break;
935 case FILE_CPU_EXCLUSIVE:
936 *s++ = is_cpu_exclusive(cs) ? '1' : '0';
937 break;
938 case FILE_MEM_EXCLUSIVE:
939 *s++ = is_mem_exclusive(cs) ? '1' : '0';
940 break;
941 case FILE_NOTIFY_ON_RELEASE:
942 *s++ = notify_on_release(cs) ? '1' : '0';
943 break;
944 default:
945 retval = -EINVAL;
946 goto out;
947 }
948 *s++ = '\n';
949 *s = '\0';
950
951 start = page + *ppos;
952 n = s - start;
953 retval = n - copy_to_user(buf, start, min(n, nbytes));
954 *ppos += retval;
955 out:
956 free_page((unsigned long)page);
957 return retval;
958 }
959
960 static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
961 loff_t *ppos)
962 {
963 ssize_t retval = 0;
964 struct cftype *cft = __d_cft(file->f_dentry);
965 if (!cft)
966 return -ENODEV;
967
968 /* special function ? */
969 if (cft->read)
970 retval = cft->read(file, buf, nbytes, ppos);
971 else
972 retval = cpuset_common_file_read(file, buf, nbytes, ppos);
973
974 return retval;
975 }
976
977 static int cpuset_file_open(struct inode *inode, struct file *file)
978 {
979 int err;
980 struct cftype *cft;
981
982 err = generic_file_open(inode, file);
983 if (err)
984 return err;
985
986 cft = __d_cft(file->f_dentry);
987 if (!cft)
988 return -ENODEV;
989 if (cft->open)
990 err = cft->open(inode, file);
991 else
992 err = 0;
993
994 return err;
995 }
996
997 static int cpuset_file_release(struct inode *inode, struct file *file)
998 {
999 struct cftype *cft = __d_cft(file->f_dentry);
1000 if (cft->release)
1001 return cft->release(inode, file);
1002 return 0;
1003 }
1004
1005 static struct file_operations cpuset_file_operations = {
1006 .read = cpuset_file_read,
1007 .write = cpuset_file_write,
1008 .llseek = generic_file_llseek,
1009 .open = cpuset_file_open,
1010 .release = cpuset_file_release,
1011 };
1012
1013 static struct inode_operations cpuset_dir_inode_operations = {
1014 .lookup = simple_lookup,
1015 .mkdir = cpuset_mkdir,
1016 .rmdir = cpuset_rmdir,
1017 };
1018
1019 static int cpuset_create_file(struct dentry *dentry, int mode)
1020 {
1021 struct inode *inode;
1022
1023 if (!dentry)
1024 return -ENOENT;
1025 if (dentry->d_inode)
1026 return -EEXIST;
1027
1028 inode = cpuset_new_inode(mode);
1029 if (!inode)
1030 return -ENOMEM;
1031
1032 if (S_ISDIR(mode)) {
1033 inode->i_op = &cpuset_dir_inode_operations;
1034 inode->i_fop = &simple_dir_operations;
1035
1036 /* start off with i_nlink == 2 (for "." entry) */
1037 inode->i_nlink++;
1038 } else if (S_ISREG(mode)) {
1039 inode->i_size = 0;
1040 inode->i_fop = &cpuset_file_operations;
1041 }
1042
1043 d_instantiate(dentry, inode);
1044 dget(dentry); /* Extra count - pin the dentry in core */
1045 return 0;
1046 }
1047
1048 /*
1049 * cpuset_create_dir - create a directory for an object.
1050 * cs: the cpuset we create the directory for.
1051 * It must have a valid ->parent field
1052 * And we are going to fill its ->dentry field.
1053 * name: The name to give to the cpuset directory. Will be copied.
1054 * mode: mode to set on new directory.
1055 */
1056
1057 static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
1058 {
1059 struct dentry *dentry = NULL;
1060 struct dentry *parent;
1061 int error = 0;
1062
1063 parent = cs->parent->dentry;
1064 dentry = cpuset_get_dentry(parent, name);
1065 if (IS_ERR(dentry))
1066 return PTR_ERR(dentry);
1067 error = cpuset_create_file(dentry, S_IFDIR | mode);
1068 if (!error) {
1069 dentry->d_fsdata = cs;
1070 parent->d_inode->i_nlink++;
1071 cs->dentry = dentry;
1072 }
1073 dput(dentry);
1074
1075 return error;
1076 }
1077
1078 static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
1079 {
1080 struct dentry *dentry;
1081 int error;
1082
1083 down(&dir->d_inode->i_sem);
1084 dentry = cpuset_get_dentry(dir, cft->name);
1085 if (!IS_ERR(dentry)) {
1086 error = cpuset_create_file(dentry, 0644 | S_IFREG);
1087 if (!error)
1088 dentry->d_fsdata = (void *)cft;
1089 dput(dentry);
1090 } else
1091 error = PTR_ERR(dentry);
1092 up(&dir->d_inode->i_sem);
1093 return error;
1094 }
1095
1096 /*
1097 * Stuff for reading the 'tasks' file.
1098 *
1099 * Reading this file can return large amounts of data if a cpuset has
1100 * *lots* of attached tasks. So it may need several calls to read(),
1101 * but we cannot guarantee that the information we produce is correct
1102 * unless we produce it entirely atomically.
1103 *
1104 * Upon tasks file open(), a struct ctr_struct is allocated, that
1105 * will have a pointer to an array (also allocated here). The struct
1106 * ctr_struct * is stored in file->private_data. Its resources will
1107 * be freed by release() when the file is closed. The array is used
1108 * to sprintf the PIDs and then used by read().
1109 */
1110
1111 /* cpusets_tasks_read array */
1112
1113 struct ctr_struct {
1114 char *buf;
1115 int bufsz;
1116 };
1117
1118 /*
1119 * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
1120 * Return actual number of pids loaded.
1121 */
1122 static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
1123 {
1124 int n = 0;
1125 struct task_struct *g, *p;
1126
1127 read_lock(&tasklist_lock);
1128
1129 do_each_thread(g, p) {
1130 if (p->cpuset == cs) {
1131 pidarray[n++] = p->pid;
1132 if (unlikely(n == npids))
1133 goto array_full;
1134 }
1135 } while_each_thread(g, p);
1136
1137 array_full:
1138 read_unlock(&tasklist_lock);
1139 return n;
1140 }
1141
1142 static int cmppid(const void *a, const void *b)
1143 {
1144 return *(pid_t *)a - *(pid_t *)b;
1145 }
1146
1147 /*
1148 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1149 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1150 * count 'cnt' of how many chars would be written if buf were large enough.
1151 */
1152 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1153 {
1154 int cnt = 0;
1155 int i;
1156
1157 for (i = 0; i < npids; i++)
1158 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1159 return cnt;
1160 }
1161
1162 static int cpuset_tasks_open(struct inode *unused, struct file *file)
1163 {
1164 struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
1165 struct ctr_struct *ctr;
1166 pid_t *pidarray;
1167 int npids;
1168 char c;
1169
1170 if (!(file->f_mode & FMODE_READ))
1171 return 0;
1172
1173 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1174 if (!ctr)
1175 goto err0;
1176
1177 /*
1178 * If cpuset gets more users after we read count, we won't have
1179 * enough space - tough. This race is indistinguishable to the
1180 * caller from the case that the additional cpuset users didn't
1181 * show up until sometime later on.
1182 */
1183 npids = atomic_read(&cs->count);
1184 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1185 if (!pidarray)
1186 goto err1;
1187
1188 npids = pid_array_load(pidarray, npids, cs);
1189 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1190
1191 /* Call pid_array_to_buf() twice, first just to get bufsz */
1192 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1193 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1194 if (!ctr->buf)
1195 goto err2;
1196 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1197
1198 kfree(pidarray);
1199 file->private_data = ctr;
1200 return 0;
1201
1202 err2:
1203 kfree(pidarray);
1204 err1:
1205 kfree(ctr);
1206 err0:
1207 return -ENOMEM;
1208 }
1209
1210 static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
1211 size_t nbytes, loff_t *ppos)
1212 {
1213 struct ctr_struct *ctr = file->private_data;
1214
1215 if (*ppos + nbytes > ctr->bufsz)
1216 nbytes = ctr->bufsz - *ppos;
1217 if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
1218 return -EFAULT;
1219 *ppos += nbytes;
1220 return nbytes;
1221 }
1222
1223 static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
1224 {
1225 struct ctr_struct *ctr;
1226
1227 if (file->f_mode & FMODE_READ) {
1228 ctr = file->private_data;
1229 kfree(ctr->buf);
1230 kfree(ctr);
1231 }
1232 return 0;
1233 }
1234
1235 /*
1236 * for the common functions, 'private' gives the type of file
1237 */
1238
1239 static struct cftype cft_tasks = {
1240 .name = "tasks",
1241 .open = cpuset_tasks_open,
1242 .read = cpuset_tasks_read,
1243 .release = cpuset_tasks_release,
1244 .private = FILE_TASKLIST,
1245 };
1246
1247 static struct cftype cft_cpus = {
1248 .name = "cpus",
1249 .private = FILE_CPULIST,
1250 };
1251
1252 static struct cftype cft_mems = {
1253 .name = "mems",
1254 .private = FILE_MEMLIST,
1255 };
1256
1257 static struct cftype cft_cpu_exclusive = {
1258 .name = "cpu_exclusive",
1259 .private = FILE_CPU_EXCLUSIVE,
1260 };
1261
1262 static struct cftype cft_mem_exclusive = {
1263 .name = "mem_exclusive",
1264 .private = FILE_MEM_EXCLUSIVE,
1265 };
1266
1267 static struct cftype cft_notify_on_release = {
1268 .name = "notify_on_release",
1269 .private = FILE_NOTIFY_ON_RELEASE,
1270 };
1271
1272 static int cpuset_populate_dir(struct dentry *cs_dentry)
1273 {
1274 int err;
1275
1276 if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
1277 return err;
1278 if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
1279 return err;
1280 if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
1281 return err;
1282 if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
1283 return err;
1284 if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
1285 return err;
1286 if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
1287 return err;
1288 return 0;
1289 }
1290
1291 /*
1292 * cpuset_create - create a cpuset
1293 * parent: cpuset that will be parent of the new cpuset.
1294 * name: name of the new cpuset. Will be strcpy'ed.
1295 * mode: mode to set on new inode
1296 *
1297 * Must be called with the semaphore on the parent inode held
1298 */
1299
1300 static long cpuset_create(struct cpuset *parent, const char *name, int mode)
1301 {
1302 struct cpuset *cs;
1303 int err;
1304
1305 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1306 if (!cs)
1307 return -ENOMEM;
1308
1309 down(&cpuset_sem);
1310 refresh_mems();
1311 cs->flags = 0;
1312 if (notify_on_release(parent))
1313 set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
1314 cs->cpus_allowed = CPU_MASK_NONE;
1315 cs->mems_allowed = NODE_MASK_NONE;
1316 atomic_set(&cs->count, 0);
1317 INIT_LIST_HEAD(&cs->sibling);
1318 INIT_LIST_HEAD(&cs->children);
1319 atomic_inc(&cpuset_mems_generation);
1320 cs->mems_generation = atomic_read(&cpuset_mems_generation);
1321
1322 cs->parent = parent;
1323
1324 list_add(&cs->sibling, &cs->parent->children);
1325
1326 err = cpuset_create_dir(cs, name, mode);
1327 if (err < 0)
1328 goto err;
1329
1330 /*
1331 * Release cpuset_sem before cpuset_populate_dir() because it
1332 * will down() this new directory's i_sem and if we race with
1333 * another mkdir, we might deadlock.
1334 */
1335 up(&cpuset_sem);
1336
1337 err = cpuset_populate_dir(cs->dentry);
1338 /* If err < 0, we have a half-filled directory - oh well ;) */
1339 return 0;
1340 err:
1341 list_del(&cs->sibling);
1342 up(&cpuset_sem);
1343 kfree(cs);
1344 return err;
1345 }
1346
1347 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1348 {
1349 struct cpuset *c_parent = dentry->d_parent->d_fsdata;
1350
1351 /* the vfs holds inode->i_sem already */
1352 return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
1353 }
1354
1355 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
1356 {
1357 struct cpuset *cs = dentry->d_fsdata;
1358 struct dentry *d;
1359 struct cpuset *parent;
1360
1361 /* the vfs holds both inode->i_sem already */
1362
1363 down(&cpuset_sem);
1364 refresh_mems();
1365 if (atomic_read(&cs->count) > 0) {
1366 up(&cpuset_sem);
1367 return -EBUSY;
1368 }
1369 if (!list_empty(&cs->children)) {
1370 up(&cpuset_sem);
1371 return -EBUSY;
1372 }
1373 parent = cs->parent;
1374 set_bit(CS_REMOVED, &cs->flags);
1375 if (is_cpu_exclusive(cs))
1376 update_cpu_domains(cs);
1377 list_del(&cs->sibling); /* delete my sibling from parent->children */
1378 if (list_empty(&parent->children))
1379 check_for_release(parent);
1380 spin_lock(&cs->dentry->d_lock);
1381 d = dget(cs->dentry);
1382 cs->dentry = NULL;
1383 spin_unlock(&d->d_lock);
1384 cpuset_d_remove_dir(d);
1385 dput(d);
1386 up(&cpuset_sem);
1387 return 0;
1388 }
1389
1390 /**
1391 * cpuset_init - initialize cpusets at system boot
1392 *
1393 * Description: Initialize top_cpuset and the cpuset internal file system,
1394 **/
1395
1396 int __init cpuset_init(void)
1397 {
1398 struct dentry *root;
1399 int err;
1400
1401 top_cpuset.cpus_allowed = CPU_MASK_ALL;
1402 top_cpuset.mems_allowed = NODE_MASK_ALL;
1403
1404 atomic_inc(&cpuset_mems_generation);
1405 top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
1406
1407 init_task.cpuset = &top_cpuset;
1408
1409 err = register_filesystem(&cpuset_fs_type);
1410 if (err < 0)
1411 goto out;
1412 cpuset_mount = kern_mount(&cpuset_fs_type);
1413 if (IS_ERR(cpuset_mount)) {
1414 printk(KERN_ERR "cpuset: could not mount!\n");
1415 err = PTR_ERR(cpuset_mount);
1416 cpuset_mount = NULL;
1417 goto out;
1418 }
1419 root = cpuset_mount->mnt_sb->s_root;
1420 root->d_fsdata = &top_cpuset;
1421 root->d_inode->i_nlink++;
1422 top_cpuset.dentry = root;
1423 root->d_inode->i_op = &cpuset_dir_inode_operations;
1424 err = cpuset_populate_dir(root);
1425 out:
1426 return err;
1427 }
1428
1429 /**
1430 * cpuset_init_smp - initialize cpus_allowed
1431 *
1432 * Description: Finish top cpuset after cpu, node maps are initialized
1433 **/
1434
1435 void __init cpuset_init_smp(void)
1436 {
1437 top_cpuset.cpus_allowed = cpu_online_map;
1438 top_cpuset.mems_allowed = node_online_map;
1439 }
1440
1441 /**
1442 * cpuset_fork - attach newly forked task to its parents cpuset.
1443 * @tsk: pointer to task_struct of forking parent process.
1444 *
1445 * Description: By default, on fork, a task inherits its
1446 * parent's cpuset. The pointer to the shared cpuset is
1447 * automatically copied in fork.c by dup_task_struct().
1448 * This cpuset_fork() routine need only increment the usage
1449 * counter in that cpuset.
1450 **/
1451
1452 void cpuset_fork(struct task_struct *tsk)
1453 {
1454 atomic_inc(&tsk->cpuset->count);
1455 }
1456
1457 /**
1458 * cpuset_exit - detach cpuset from exiting task
1459 * @tsk: pointer to task_struct of exiting process
1460 *
1461 * Description: Detach cpuset from @tsk and release it.
1462 *
1463 * Note that cpusets marked notify_on_release force every task
1464 * in them to take the global cpuset_sem semaphore when exiting.
1465 * This could impact scaling on very large systems. Be reluctant
1466 * to use notify_on_release cpusets where very high task exit
1467 * scaling is required on large systems.
1468 *
1469 * Don't even think about derefencing 'cs' after the cpuset use
1470 * count goes to zero, except inside a critical section guarded
1471 * by the cpuset_sem semaphore. If you don't hold cpuset_sem,
1472 * then a zero cpuset use count is a license to any other task to
1473 * nuke the cpuset immediately.
1474 **/
1475
1476 void cpuset_exit(struct task_struct *tsk)
1477 {
1478 struct cpuset *cs;
1479
1480 task_lock(tsk);
1481 cs = tsk->cpuset;
1482 tsk->cpuset = NULL;
1483 task_unlock(tsk);
1484
1485 if (notify_on_release(cs)) {
1486 down(&cpuset_sem);
1487 if (atomic_dec_and_test(&cs->count))
1488 check_for_release(cs);
1489 up(&cpuset_sem);
1490 } else {
1491 atomic_dec(&cs->count);
1492 }
1493 }
1494
1495 /**
1496 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
1497 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
1498 *
1499 * Description: Returns the cpumask_t cpus_allowed of the cpuset
1500 * attached to the specified @tsk. Guaranteed to return some non-empty
1501 * subset of cpu_online_map, even if this means going outside the
1502 * tasks cpuset.
1503 **/
1504
1505 cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
1506 {
1507 cpumask_t mask;
1508
1509 down(&cpuset_sem);
1510 task_lock((struct task_struct *)tsk);
1511 guarantee_online_cpus(tsk->cpuset, &mask);
1512 task_unlock((struct task_struct *)tsk);
1513 up(&cpuset_sem);
1514
1515 return mask;
1516 }
1517
1518 void cpuset_init_current_mems_allowed(void)
1519 {
1520 current->mems_allowed = NODE_MASK_ALL;
1521 }
1522
1523 /**
1524 * cpuset_update_current_mems_allowed - update mems parameters to new values
1525 *
1526 * If the current tasks cpusets mems_allowed changed behind our backs,
1527 * update current->mems_allowed and mems_generation to the new value.
1528 * Do not call this routine if in_interrupt().
1529 */
1530
1531 void cpuset_update_current_mems_allowed(void)
1532 {
1533 struct cpuset *cs = current->cpuset;
1534
1535 if (!cs)
1536 return; /* task is exiting */
1537 if (current->cpuset_mems_generation != cs->mems_generation) {
1538 down(&cpuset_sem);
1539 refresh_mems();
1540 up(&cpuset_sem);
1541 }
1542 }
1543
1544 /**
1545 * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
1546 * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
1547 */
1548 void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
1549 {
1550 bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
1551 MAX_NUMNODES);
1552 }
1553
1554 /**
1555 * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
1556 * @zl: the zonelist to be checked
1557 *
1558 * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
1559 */
1560 int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
1561 {
1562 int i;
1563
1564 for (i = 0; zl->zones[i]; i++) {
1565 int nid = zl->zones[i]->zone_pgdat->node_id;
1566
1567 if (node_isset(nid, current->mems_allowed))
1568 return 1;
1569 }
1570 return 0;
1571 }
1572
1573 /**
1574 * cpuset_zone_allowed - is zone z allowed in current->mems_allowed
1575 * @z: zone in question
1576 *
1577 * Is zone z allowed in current->mems_allowed, or is
1578 * the CPU in interrupt context? (zone is always allowed in this case)
1579 */
1580 int cpuset_zone_allowed(struct zone *z)
1581 {
1582 return in_interrupt() ||
1583 node_isset(z->zone_pgdat->node_id, current->mems_allowed);
1584 }
1585
1586 /*
1587 * proc_cpuset_show()
1588 * - Print tasks cpuset path into seq_file.
1589 * - Used for /proc/<pid>/cpuset.
1590 */
1591
1592 static int proc_cpuset_show(struct seq_file *m, void *v)
1593 {
1594 struct cpuset *cs;
1595 struct task_struct *tsk;
1596 char *buf;
1597 int retval = 0;
1598
1599 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1600 if (!buf)
1601 return -ENOMEM;
1602
1603 tsk = m->private;
1604 down(&cpuset_sem);
1605 task_lock(tsk);
1606 cs = tsk->cpuset;
1607 task_unlock(tsk);
1608 if (!cs) {
1609 retval = -EINVAL;
1610 goto out;
1611 }
1612
1613 retval = cpuset_path(cs, buf, PAGE_SIZE);
1614 if (retval < 0)
1615 goto out;
1616 seq_puts(m, buf);
1617 seq_putc(m, '\n');
1618 out:
1619 up(&cpuset_sem);
1620 kfree(buf);
1621 return retval;
1622 }
1623
1624 static int cpuset_open(struct inode *inode, struct file *file)
1625 {
1626 struct task_struct *tsk = PROC_I(inode)->task;
1627 return single_open(file, proc_cpuset_show, tsk);
1628 }
1629
1630 struct file_operations proc_cpuset_operations = {
1631 .open = cpuset_open,
1632 .read = seq_read,
1633 .llseek = seq_lseek,
1634 .release = single_release,
1635 };
1636
1637 /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
1638 char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
1639 {
1640 buffer += sprintf(buffer, "Cpus_allowed:\t");
1641 buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
1642 buffer += sprintf(buffer, "\n");
1643 buffer += sprintf(buffer, "Mems_allowed:\t");
1644 buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
1645 buffer += sprintf(buffer, "\n");
1646 return buffer;
1647 }