]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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(), | |
2efe86b8 PJ |
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 | |
1da177e4 LT |
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); | |
4247bdc6 | 183 | |
1da177e4 LT |
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 | { | |
5f45f1a7 | 231 | struct dentry *d = lookup_one_len(name, parent, strlen(name)); |
1da177e4 LT |
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 | * | |
3077a260 PJ |
401 | * The final arg to call_usermodehelper() is 0, which means don't |
402 | * wait. The separate /sbin/cpuset_release_agent task is forked by | |
403 | * call_usermodehelper(), then control in this thread returns here, | |
404 | * without waiting for the release agent task. We don't bother to | |
405 | * wait because the caller of this routine has no use for the exit | |
406 | * status of the /sbin/cpuset_release_agent task, so no sense holding | |
407 | * our caller up for that. | |
408 | * | |
409 | * The simple act of forking that task might require more memory, | |
410 | * which might need cpuset_sem. So this routine must be called while | |
411 | * cpuset_sem is not held, to avoid a possible deadlock. See also | |
412 | * comments for check_for_release(), below. | |
1da177e4 LT |
413 | */ |
414 | ||
3077a260 | 415 | static void cpuset_release_agent(const char *pathbuf) |
1da177e4 LT |
416 | { |
417 | char *argv[3], *envp[3]; | |
418 | int i; | |
419 | ||
3077a260 PJ |
420 | if (!pathbuf) |
421 | return; | |
422 | ||
1da177e4 LT |
423 | i = 0; |
424 | argv[i++] = "/sbin/cpuset_release_agent"; | |
3077a260 | 425 | argv[i++] = (char *)pathbuf; |
1da177e4 LT |
426 | argv[i] = NULL; |
427 | ||
428 | i = 0; | |
429 | /* minimal command environment */ | |
430 | envp[i++] = "HOME=/"; | |
431 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; | |
432 | envp[i] = NULL; | |
433 | ||
3077a260 PJ |
434 | call_usermodehelper(argv[0], argv, envp, 0); |
435 | kfree(pathbuf); | |
1da177e4 LT |
436 | } |
437 | ||
438 | /* | |
439 | * Either cs->count of using tasks transitioned to zero, or the | |
440 | * cs->children list of child cpusets just became empty. If this | |
441 | * cs is notify_on_release() and now both the user count is zero and | |
3077a260 PJ |
442 | * the list of children is empty, prepare cpuset path in a kmalloc'd |
443 | * buffer, to be returned via ppathbuf, so that the caller can invoke | |
444 | * cpuset_release_agent() with it later on, once cpuset_sem is dropped. | |
445 | * Call here with cpuset_sem held. | |
446 | * | |
447 | * This check_for_release() routine is responsible for kmalloc'ing | |
448 | * pathbuf. The above cpuset_release_agent() is responsible for | |
449 | * kfree'ing pathbuf. The caller of these routines is responsible | |
450 | * for providing a pathbuf pointer, initialized to NULL, then | |
451 | * calling check_for_release() with cpuset_sem held and the address | |
452 | * of the pathbuf pointer, then dropping cpuset_sem, then calling | |
453 | * cpuset_release_agent() with pathbuf, as set by check_for_release(). | |
1da177e4 LT |
454 | */ |
455 | ||
3077a260 | 456 | static void check_for_release(struct cpuset *cs, char **ppathbuf) |
1da177e4 LT |
457 | { |
458 | if (notify_on_release(cs) && atomic_read(&cs->count) == 0 && | |
459 | list_empty(&cs->children)) { | |
460 | char *buf; | |
461 | ||
462 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
463 | if (!buf) | |
464 | return; | |
465 | if (cpuset_path(cs, buf, PAGE_SIZE) < 0) | |
3077a260 PJ |
466 | kfree(buf); |
467 | else | |
468 | *ppathbuf = buf; | |
1da177e4 LT |
469 | } |
470 | } | |
471 | ||
472 | /* | |
473 | * Return in *pmask the portion of a cpusets's cpus_allowed that | |
474 | * are online. If none are online, walk up the cpuset hierarchy | |
475 | * until we find one that does have some online cpus. If we get | |
476 | * all the way to the top and still haven't found any online cpus, | |
477 | * return cpu_online_map. Or if passed a NULL cs from an exit'ing | |
478 | * task, return cpu_online_map. | |
479 | * | |
480 | * One way or another, we guarantee to return some non-empty subset | |
481 | * of cpu_online_map. | |
482 | * | |
483 | * Call with cpuset_sem held. | |
484 | */ | |
485 | ||
486 | static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask) | |
487 | { | |
488 | while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map)) | |
489 | cs = cs->parent; | |
490 | if (cs) | |
491 | cpus_and(*pmask, cs->cpus_allowed, cpu_online_map); | |
492 | else | |
493 | *pmask = cpu_online_map; | |
494 | BUG_ON(!cpus_intersects(*pmask, cpu_online_map)); | |
495 | } | |
496 | ||
497 | /* | |
498 | * Return in *pmask the portion of a cpusets's mems_allowed that | |
499 | * are online. If none are online, walk up the cpuset hierarchy | |
500 | * until we find one that does have some online mems. If we get | |
501 | * all the way to the top and still haven't found any online mems, | |
502 | * return node_online_map. | |
503 | * | |
504 | * One way or another, we guarantee to return some non-empty subset | |
505 | * of node_online_map. | |
506 | * | |
507 | * Call with cpuset_sem held. | |
508 | */ | |
509 | ||
510 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) | |
511 | { | |
512 | while (cs && !nodes_intersects(cs->mems_allowed, node_online_map)) | |
513 | cs = cs->parent; | |
514 | if (cs) | |
515 | nodes_and(*pmask, cs->mems_allowed, node_online_map); | |
516 | else | |
517 | *pmask = node_online_map; | |
518 | BUG_ON(!nodes_intersects(*pmask, node_online_map)); | |
519 | } | |
520 | ||
521 | /* | |
522 | * Refresh current tasks mems_allowed and mems_generation from | |
523 | * current tasks cpuset. Call with cpuset_sem held. | |
524 | * | |
5aa15b5f PJ |
525 | * Be sure to call refresh_mems() on any cpuset operation which |
526 | * (1) holds cpuset_sem, and (2) might possibly alloc memory. | |
527 | * Call after obtaining cpuset_sem lock, before any possible | |
528 | * allocation. Otherwise one risks trying to allocate memory | |
529 | * while the task cpuset_mems_generation is not the same as | |
530 | * the mems_generation in its cpuset, which would deadlock on | |
531 | * cpuset_sem in cpuset_update_current_mems_allowed(). | |
532 | * | |
533 | * Since we hold cpuset_sem, once refresh_mems() is called, the | |
534 | * test (current->cpuset_mems_generation != cs->mems_generation) | |
535 | * in cpuset_update_current_mems_allowed() will remain false, | |
536 | * until we drop cpuset_sem. Anyone else who would change our | |
537 | * cpusets mems_generation needs to lock cpuset_sem first. | |
1da177e4 LT |
538 | */ |
539 | ||
540 | static void refresh_mems(void) | |
541 | { | |
542 | struct cpuset *cs = current->cpuset; | |
543 | ||
544 | if (current->cpuset_mems_generation != cs->mems_generation) { | |
545 | guarantee_online_mems(cs, ¤t->mems_allowed); | |
546 | current->cpuset_mems_generation = cs->mems_generation; | |
547 | } | |
548 | } | |
549 | ||
550 | /* | |
551 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? | |
552 | * | |
553 | * One cpuset is a subset of another if all its allowed CPUs and | |
554 | * Memory Nodes are a subset of the other, and its exclusive flags | |
555 | * are only set if the other's are set. | |
556 | */ | |
557 | ||
558 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) | |
559 | { | |
560 | return cpus_subset(p->cpus_allowed, q->cpus_allowed) && | |
561 | nodes_subset(p->mems_allowed, q->mems_allowed) && | |
562 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && | |
563 | is_mem_exclusive(p) <= is_mem_exclusive(q); | |
564 | } | |
565 | ||
566 | /* | |
567 | * validate_change() - Used to validate that any proposed cpuset change | |
568 | * follows the structural rules for cpusets. | |
569 | * | |
570 | * If we replaced the flag and mask values of the current cpuset | |
571 | * (cur) with those values in the trial cpuset (trial), would | |
572 | * our various subset and exclusive rules still be valid? Presumes | |
573 | * cpuset_sem held. | |
574 | * | |
575 | * 'cur' is the address of an actual, in-use cpuset. Operations | |
576 | * such as list traversal that depend on the actual address of the | |
577 | * cpuset in the list must use cur below, not trial. | |
578 | * | |
579 | * 'trial' is the address of bulk structure copy of cur, with | |
580 | * perhaps one or more of the fields cpus_allowed, mems_allowed, | |
581 | * or flags changed to new, trial values. | |
582 | * | |
583 | * Return 0 if valid, -errno if not. | |
584 | */ | |
585 | ||
586 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) | |
587 | { | |
588 | struct cpuset *c, *par; | |
589 | ||
590 | /* Each of our child cpusets must be a subset of us */ | |
591 | list_for_each_entry(c, &cur->children, sibling) { | |
592 | if (!is_cpuset_subset(c, trial)) | |
593 | return -EBUSY; | |
594 | } | |
595 | ||
596 | /* Remaining checks don't apply to root cpuset */ | |
597 | if ((par = cur->parent) == NULL) | |
598 | return 0; | |
599 | ||
600 | /* We must be a subset of our parent cpuset */ | |
601 | if (!is_cpuset_subset(trial, par)) | |
602 | return -EACCES; | |
603 | ||
604 | /* If either I or some sibling (!= me) is exclusive, we can't overlap */ | |
605 | list_for_each_entry(c, &par->children, sibling) { | |
606 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && | |
607 | c != cur && | |
608 | cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) | |
609 | return -EINVAL; | |
610 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && | |
611 | c != cur && | |
612 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) | |
613 | return -EINVAL; | |
614 | } | |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
85d7b949 DG |
619 | /* |
620 | * For a given cpuset cur, partition the system as follows | |
621 | * a. All cpus in the parent cpuset's cpus_allowed that are not part of any | |
622 | * exclusive child cpusets | |
623 | * b. All cpus in the current cpuset's cpus_allowed that are not part of any | |
624 | * exclusive child cpusets | |
625 | * Build these two partitions by calling partition_sched_domains | |
626 | * | |
627 | * Call with cpuset_sem held. May nest a call to the | |
628 | * lock_cpu_hotplug()/unlock_cpu_hotplug() pair. | |
629 | */ | |
212d6d22 | 630 | |
85d7b949 DG |
631 | static void update_cpu_domains(struct cpuset *cur) |
632 | { | |
633 | struct cpuset *c, *par = cur->parent; | |
634 | cpumask_t pspan, cspan; | |
635 | ||
636 | if (par == NULL || cpus_empty(cur->cpus_allowed)) | |
637 | return; | |
638 | ||
639 | /* | |
640 | * Get all cpus from parent's cpus_allowed not part of exclusive | |
641 | * children | |
642 | */ | |
643 | pspan = par->cpus_allowed; | |
644 | list_for_each_entry(c, &par->children, sibling) { | |
645 | if (is_cpu_exclusive(c)) | |
646 | cpus_andnot(pspan, pspan, c->cpus_allowed); | |
647 | } | |
648 | if (is_removed(cur) || !is_cpu_exclusive(cur)) { | |
649 | cpus_or(pspan, pspan, cur->cpus_allowed); | |
650 | if (cpus_equal(pspan, cur->cpus_allowed)) | |
651 | return; | |
652 | cspan = CPU_MASK_NONE; | |
653 | } else { | |
654 | if (cpus_empty(pspan)) | |
655 | return; | |
656 | cspan = cur->cpus_allowed; | |
657 | /* | |
658 | * Get all cpus from current cpuset's cpus_allowed not part | |
659 | * of exclusive children | |
660 | */ | |
661 | list_for_each_entry(c, &cur->children, sibling) { | |
662 | if (is_cpu_exclusive(c)) | |
663 | cpus_andnot(cspan, cspan, c->cpus_allowed); | |
664 | } | |
665 | } | |
666 | ||
667 | lock_cpu_hotplug(); | |
668 | partition_sched_domains(&pspan, &cspan); | |
669 | unlock_cpu_hotplug(); | |
670 | } | |
671 | ||
1da177e4 LT |
672 | static int update_cpumask(struct cpuset *cs, char *buf) |
673 | { | |
674 | struct cpuset trialcs; | |
85d7b949 | 675 | int retval, cpus_unchanged; |
1da177e4 LT |
676 | |
677 | trialcs = *cs; | |
678 | retval = cpulist_parse(buf, trialcs.cpus_allowed); | |
679 | if (retval < 0) | |
680 | return retval; | |
681 | cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); | |
682 | if (cpus_empty(trialcs.cpus_allowed)) | |
683 | return -ENOSPC; | |
684 | retval = validate_change(cs, &trialcs); | |
85d7b949 DG |
685 | if (retval < 0) |
686 | return retval; | |
687 | cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); | |
688 | cs->cpus_allowed = trialcs.cpus_allowed; | |
689 | if (is_cpu_exclusive(cs) && !cpus_unchanged) | |
690 | update_cpu_domains(cs); | |
691 | return 0; | |
1da177e4 LT |
692 | } |
693 | ||
694 | static int update_nodemask(struct cpuset *cs, char *buf) | |
695 | { | |
696 | struct cpuset trialcs; | |
697 | int retval; | |
698 | ||
699 | trialcs = *cs; | |
700 | retval = nodelist_parse(buf, trialcs.mems_allowed); | |
701 | if (retval < 0) | |
702 | return retval; | |
703 | nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map); | |
704 | if (nodes_empty(trialcs.mems_allowed)) | |
705 | return -ENOSPC; | |
706 | retval = validate_change(cs, &trialcs); | |
707 | if (retval == 0) { | |
708 | cs->mems_allowed = trialcs.mems_allowed; | |
709 | atomic_inc(&cpuset_mems_generation); | |
710 | cs->mems_generation = atomic_read(&cpuset_mems_generation); | |
711 | } | |
712 | return retval; | |
713 | } | |
714 | ||
715 | /* | |
716 | * update_flag - read a 0 or a 1 in a file and update associated flag | |
717 | * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, | |
718 | * CS_NOTIFY_ON_RELEASE) | |
719 | * cs: the cpuset to update | |
720 | * buf: the buffer where we read the 0 or 1 | |
721 | */ | |
722 | ||
723 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf) | |
724 | { | |
725 | int turning_on; | |
726 | struct cpuset trialcs; | |
85d7b949 | 727 | int err, cpu_exclusive_changed; |
1da177e4 LT |
728 | |
729 | turning_on = (simple_strtoul(buf, NULL, 10) != 0); | |
730 | ||
731 | trialcs = *cs; | |
732 | if (turning_on) | |
733 | set_bit(bit, &trialcs.flags); | |
734 | else | |
735 | clear_bit(bit, &trialcs.flags); | |
736 | ||
737 | err = validate_change(cs, &trialcs); | |
85d7b949 DG |
738 | if (err < 0) |
739 | return err; | |
740 | cpu_exclusive_changed = | |
741 | (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs)); | |
742 | if (turning_on) | |
743 | set_bit(bit, &cs->flags); | |
744 | else | |
745 | clear_bit(bit, &cs->flags); | |
746 | ||
747 | if (cpu_exclusive_changed) | |
748 | update_cpu_domains(cs); | |
749 | return 0; | |
1da177e4 LT |
750 | } |
751 | ||
3077a260 | 752 | static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf) |
1da177e4 LT |
753 | { |
754 | pid_t pid; | |
755 | struct task_struct *tsk; | |
756 | struct cpuset *oldcs; | |
757 | cpumask_t cpus; | |
758 | ||
3077a260 | 759 | if (sscanf(pidbuf, "%d", &pid) != 1) |
1da177e4 LT |
760 | return -EIO; |
761 | if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) | |
762 | return -ENOSPC; | |
763 | ||
764 | if (pid) { | |
765 | read_lock(&tasklist_lock); | |
766 | ||
767 | tsk = find_task_by_pid(pid); | |
768 | if (!tsk) { | |
769 | read_unlock(&tasklist_lock); | |
770 | return -ESRCH; | |
771 | } | |
772 | ||
773 | get_task_struct(tsk); | |
774 | read_unlock(&tasklist_lock); | |
775 | ||
776 | if ((current->euid) && (current->euid != tsk->uid) | |
777 | && (current->euid != tsk->suid)) { | |
778 | put_task_struct(tsk); | |
779 | return -EACCES; | |
780 | } | |
781 | } else { | |
782 | tsk = current; | |
783 | get_task_struct(tsk); | |
784 | } | |
785 | ||
786 | task_lock(tsk); | |
787 | oldcs = tsk->cpuset; | |
788 | if (!oldcs) { | |
789 | task_unlock(tsk); | |
790 | put_task_struct(tsk); | |
791 | return -ESRCH; | |
792 | } | |
793 | atomic_inc(&cs->count); | |
794 | tsk->cpuset = cs; | |
795 | task_unlock(tsk); | |
796 | ||
797 | guarantee_online_cpus(cs, &cpus); | |
798 | set_cpus_allowed(tsk, cpus); | |
799 | ||
800 | put_task_struct(tsk); | |
801 | if (atomic_dec_and_test(&oldcs->count)) | |
3077a260 | 802 | check_for_release(oldcs, ppathbuf); |
1da177e4 LT |
803 | return 0; |
804 | } | |
805 | ||
806 | /* The various types of files and directories in a cpuset file system */ | |
807 | ||
808 | typedef enum { | |
809 | FILE_ROOT, | |
810 | FILE_DIR, | |
811 | FILE_CPULIST, | |
812 | FILE_MEMLIST, | |
813 | FILE_CPU_EXCLUSIVE, | |
814 | FILE_MEM_EXCLUSIVE, | |
815 | FILE_NOTIFY_ON_RELEASE, | |
816 | FILE_TASKLIST, | |
817 | } cpuset_filetype_t; | |
818 | ||
819 | static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf, | |
820 | size_t nbytes, loff_t *unused_ppos) | |
821 | { | |
822 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); | |
823 | struct cftype *cft = __d_cft(file->f_dentry); | |
824 | cpuset_filetype_t type = cft->private; | |
825 | char *buffer; | |
3077a260 | 826 | char *pathbuf = NULL; |
1da177e4 LT |
827 | int retval = 0; |
828 | ||
829 | /* Crude upper limit on largest legitimate cpulist user might write. */ | |
830 | if (nbytes > 100 + 6 * NR_CPUS) | |
831 | return -E2BIG; | |
832 | ||
833 | /* +1 for nul-terminator */ | |
834 | if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0) | |
835 | return -ENOMEM; | |
836 | ||
837 | if (copy_from_user(buffer, userbuf, nbytes)) { | |
838 | retval = -EFAULT; | |
839 | goto out1; | |
840 | } | |
841 | buffer[nbytes] = 0; /* nul-terminate */ | |
842 | ||
5aa15b5f | 843 | down(&cpuset_sem); |
1da177e4 LT |
844 | |
845 | if (is_removed(cs)) { | |
846 | retval = -ENODEV; | |
847 | goto out2; | |
848 | } | |
849 | ||
850 | switch (type) { | |
851 | case FILE_CPULIST: | |
852 | retval = update_cpumask(cs, buffer); | |
853 | break; | |
854 | case FILE_MEMLIST: | |
855 | retval = update_nodemask(cs, buffer); | |
856 | break; | |
857 | case FILE_CPU_EXCLUSIVE: | |
858 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer); | |
859 | break; | |
860 | case FILE_MEM_EXCLUSIVE: | |
861 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); | |
862 | break; | |
863 | case FILE_NOTIFY_ON_RELEASE: | |
864 | retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer); | |
865 | break; | |
866 | case FILE_TASKLIST: | |
3077a260 | 867 | retval = attach_task(cs, buffer, &pathbuf); |
1da177e4 LT |
868 | break; |
869 | default: | |
870 | retval = -EINVAL; | |
871 | goto out2; | |
872 | } | |
873 | ||
874 | if (retval == 0) | |
875 | retval = nbytes; | |
876 | out2: | |
5aa15b5f | 877 | up(&cpuset_sem); |
3077a260 | 878 | cpuset_release_agent(pathbuf); |
1da177e4 LT |
879 | out1: |
880 | kfree(buffer); | |
881 | return retval; | |
882 | } | |
883 | ||
884 | static ssize_t cpuset_file_write(struct file *file, const char __user *buf, | |
885 | size_t nbytes, loff_t *ppos) | |
886 | { | |
887 | ssize_t retval = 0; | |
888 | struct cftype *cft = __d_cft(file->f_dentry); | |
889 | if (!cft) | |
890 | return -ENODEV; | |
891 | ||
892 | /* special function ? */ | |
893 | if (cft->write) | |
894 | retval = cft->write(file, buf, nbytes, ppos); | |
895 | else | |
896 | retval = cpuset_common_file_write(file, buf, nbytes, ppos); | |
897 | ||
898 | return retval; | |
899 | } | |
900 | ||
901 | /* | |
902 | * These ascii lists should be read in a single call, by using a user | |
903 | * buffer large enough to hold the entire map. If read in smaller | |
904 | * chunks, there is no guarantee of atomicity. Since the display format | |
905 | * used, list of ranges of sequential numbers, is variable length, | |
906 | * and since these maps can change value dynamically, one could read | |
907 | * gibberish by doing partial reads while a list was changing. | |
908 | * A single large read to a buffer that crosses a page boundary is | |
909 | * ok, because the result being copied to user land is not recomputed | |
910 | * across a page fault. | |
911 | */ | |
912 | ||
913 | static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs) | |
914 | { | |
915 | cpumask_t mask; | |
916 | ||
5aa15b5f | 917 | down(&cpuset_sem); |
1da177e4 | 918 | mask = cs->cpus_allowed; |
5aa15b5f | 919 | up(&cpuset_sem); |
1da177e4 LT |
920 | |
921 | return cpulist_scnprintf(page, PAGE_SIZE, mask); | |
922 | } | |
923 | ||
924 | static int cpuset_sprintf_memlist(char *page, struct cpuset *cs) | |
925 | { | |
926 | nodemask_t mask; | |
927 | ||
5aa15b5f | 928 | down(&cpuset_sem); |
1da177e4 | 929 | mask = cs->mems_allowed; |
5aa15b5f | 930 | up(&cpuset_sem); |
1da177e4 LT |
931 | |
932 | return nodelist_scnprintf(page, PAGE_SIZE, mask); | |
933 | } | |
934 | ||
935 | static ssize_t cpuset_common_file_read(struct file *file, char __user *buf, | |
936 | size_t nbytes, loff_t *ppos) | |
937 | { | |
938 | struct cftype *cft = __d_cft(file->f_dentry); | |
939 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); | |
940 | cpuset_filetype_t type = cft->private; | |
941 | char *page; | |
942 | ssize_t retval = 0; | |
943 | char *s; | |
1da177e4 LT |
944 | |
945 | if (!(page = (char *)__get_free_page(GFP_KERNEL))) | |
946 | return -ENOMEM; | |
947 | ||
948 | s = page; | |
949 | ||
950 | switch (type) { | |
951 | case FILE_CPULIST: | |
952 | s += cpuset_sprintf_cpulist(s, cs); | |
953 | break; | |
954 | case FILE_MEMLIST: | |
955 | s += cpuset_sprintf_memlist(s, cs); | |
956 | break; | |
957 | case FILE_CPU_EXCLUSIVE: | |
958 | *s++ = is_cpu_exclusive(cs) ? '1' : '0'; | |
959 | break; | |
960 | case FILE_MEM_EXCLUSIVE: | |
961 | *s++ = is_mem_exclusive(cs) ? '1' : '0'; | |
962 | break; | |
963 | case FILE_NOTIFY_ON_RELEASE: | |
964 | *s++ = notify_on_release(cs) ? '1' : '0'; | |
965 | break; | |
966 | default: | |
967 | retval = -EINVAL; | |
968 | goto out; | |
969 | } | |
970 | *s++ = '\n'; | |
1da177e4 | 971 | |
eacaa1f5 | 972 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
1da177e4 LT |
973 | out: |
974 | free_page((unsigned long)page); | |
975 | return retval; | |
976 | } | |
977 | ||
978 | static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes, | |
979 | loff_t *ppos) | |
980 | { | |
981 | ssize_t retval = 0; | |
982 | struct cftype *cft = __d_cft(file->f_dentry); | |
983 | if (!cft) | |
984 | return -ENODEV; | |
985 | ||
986 | /* special function ? */ | |
987 | if (cft->read) | |
988 | retval = cft->read(file, buf, nbytes, ppos); | |
989 | else | |
990 | retval = cpuset_common_file_read(file, buf, nbytes, ppos); | |
991 | ||
992 | return retval; | |
993 | } | |
994 | ||
995 | static int cpuset_file_open(struct inode *inode, struct file *file) | |
996 | { | |
997 | int err; | |
998 | struct cftype *cft; | |
999 | ||
1000 | err = generic_file_open(inode, file); | |
1001 | if (err) | |
1002 | return err; | |
1003 | ||
1004 | cft = __d_cft(file->f_dentry); | |
1005 | if (!cft) | |
1006 | return -ENODEV; | |
1007 | if (cft->open) | |
1008 | err = cft->open(inode, file); | |
1009 | else | |
1010 | err = 0; | |
1011 | ||
1012 | return err; | |
1013 | } | |
1014 | ||
1015 | static int cpuset_file_release(struct inode *inode, struct file *file) | |
1016 | { | |
1017 | struct cftype *cft = __d_cft(file->f_dentry); | |
1018 | if (cft->release) | |
1019 | return cft->release(inode, file); | |
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | static struct file_operations cpuset_file_operations = { | |
1024 | .read = cpuset_file_read, | |
1025 | .write = cpuset_file_write, | |
1026 | .llseek = generic_file_llseek, | |
1027 | .open = cpuset_file_open, | |
1028 | .release = cpuset_file_release, | |
1029 | }; | |
1030 | ||
1031 | static struct inode_operations cpuset_dir_inode_operations = { | |
1032 | .lookup = simple_lookup, | |
1033 | .mkdir = cpuset_mkdir, | |
1034 | .rmdir = cpuset_rmdir, | |
1035 | }; | |
1036 | ||
1037 | static int cpuset_create_file(struct dentry *dentry, int mode) | |
1038 | { | |
1039 | struct inode *inode; | |
1040 | ||
1041 | if (!dentry) | |
1042 | return -ENOENT; | |
1043 | if (dentry->d_inode) | |
1044 | return -EEXIST; | |
1045 | ||
1046 | inode = cpuset_new_inode(mode); | |
1047 | if (!inode) | |
1048 | return -ENOMEM; | |
1049 | ||
1050 | if (S_ISDIR(mode)) { | |
1051 | inode->i_op = &cpuset_dir_inode_operations; | |
1052 | inode->i_fop = &simple_dir_operations; | |
1053 | ||
1054 | /* start off with i_nlink == 2 (for "." entry) */ | |
1055 | inode->i_nlink++; | |
1056 | } else if (S_ISREG(mode)) { | |
1057 | inode->i_size = 0; | |
1058 | inode->i_fop = &cpuset_file_operations; | |
1059 | } | |
1060 | ||
1061 | d_instantiate(dentry, inode); | |
1062 | dget(dentry); /* Extra count - pin the dentry in core */ | |
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | /* | |
1067 | * cpuset_create_dir - create a directory for an object. | |
1068 | * cs: the cpuset we create the directory for. | |
1069 | * It must have a valid ->parent field | |
1070 | * And we are going to fill its ->dentry field. | |
1071 | * name: The name to give to the cpuset directory. Will be copied. | |
1072 | * mode: mode to set on new directory. | |
1073 | */ | |
1074 | ||
1075 | static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode) | |
1076 | { | |
1077 | struct dentry *dentry = NULL; | |
1078 | struct dentry *parent; | |
1079 | int error = 0; | |
1080 | ||
1081 | parent = cs->parent->dentry; | |
1082 | dentry = cpuset_get_dentry(parent, name); | |
1083 | if (IS_ERR(dentry)) | |
1084 | return PTR_ERR(dentry); | |
1085 | error = cpuset_create_file(dentry, S_IFDIR | mode); | |
1086 | if (!error) { | |
1087 | dentry->d_fsdata = cs; | |
1088 | parent->d_inode->i_nlink++; | |
1089 | cs->dentry = dentry; | |
1090 | } | |
1091 | dput(dentry); | |
1092 | ||
1093 | return error; | |
1094 | } | |
1095 | ||
1096 | static int cpuset_add_file(struct dentry *dir, const struct cftype *cft) | |
1097 | { | |
1098 | struct dentry *dentry; | |
1099 | int error; | |
1100 | ||
1101 | down(&dir->d_inode->i_sem); | |
1102 | dentry = cpuset_get_dentry(dir, cft->name); | |
1103 | if (!IS_ERR(dentry)) { | |
1104 | error = cpuset_create_file(dentry, 0644 | S_IFREG); | |
1105 | if (!error) | |
1106 | dentry->d_fsdata = (void *)cft; | |
1107 | dput(dentry); | |
1108 | } else | |
1109 | error = PTR_ERR(dentry); | |
1110 | up(&dir->d_inode->i_sem); | |
1111 | return error; | |
1112 | } | |
1113 | ||
1114 | /* | |
1115 | * Stuff for reading the 'tasks' file. | |
1116 | * | |
1117 | * Reading this file can return large amounts of data if a cpuset has | |
1118 | * *lots* of attached tasks. So it may need several calls to read(), | |
1119 | * but we cannot guarantee that the information we produce is correct | |
1120 | * unless we produce it entirely atomically. | |
1121 | * | |
1122 | * Upon tasks file open(), a struct ctr_struct is allocated, that | |
1123 | * will have a pointer to an array (also allocated here). The struct | |
1124 | * ctr_struct * is stored in file->private_data. Its resources will | |
1125 | * be freed by release() when the file is closed. The array is used | |
1126 | * to sprintf the PIDs and then used by read(). | |
1127 | */ | |
1128 | ||
1129 | /* cpusets_tasks_read array */ | |
1130 | ||
1131 | struct ctr_struct { | |
1132 | char *buf; | |
1133 | int bufsz; | |
1134 | }; | |
1135 | ||
1136 | /* | |
1137 | * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'. | |
1138 | * Return actual number of pids loaded. | |
1139 | */ | |
1140 | static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs) | |
1141 | { | |
1142 | int n = 0; | |
1143 | struct task_struct *g, *p; | |
1144 | ||
1145 | read_lock(&tasklist_lock); | |
1146 | ||
1147 | do_each_thread(g, p) { | |
1148 | if (p->cpuset == cs) { | |
1149 | pidarray[n++] = p->pid; | |
1150 | if (unlikely(n == npids)) | |
1151 | goto array_full; | |
1152 | } | |
1153 | } while_each_thread(g, p); | |
1154 | ||
1155 | array_full: | |
1156 | read_unlock(&tasklist_lock); | |
1157 | return n; | |
1158 | } | |
1159 | ||
1160 | static int cmppid(const void *a, const void *b) | |
1161 | { | |
1162 | return *(pid_t *)a - *(pid_t *)b; | |
1163 | } | |
1164 | ||
1165 | /* | |
1166 | * Convert array 'a' of 'npids' pid_t's to a string of newline separated | |
1167 | * decimal pids in 'buf'. Don't write more than 'sz' chars, but return | |
1168 | * count 'cnt' of how many chars would be written if buf were large enough. | |
1169 | */ | |
1170 | static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids) | |
1171 | { | |
1172 | int cnt = 0; | |
1173 | int i; | |
1174 | ||
1175 | for (i = 0; i < npids; i++) | |
1176 | cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]); | |
1177 | return cnt; | |
1178 | } | |
1179 | ||
1180 | static int cpuset_tasks_open(struct inode *unused, struct file *file) | |
1181 | { | |
1182 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); | |
1183 | struct ctr_struct *ctr; | |
1184 | pid_t *pidarray; | |
1185 | int npids; | |
1186 | char c; | |
1187 | ||
1188 | if (!(file->f_mode & FMODE_READ)) | |
1189 | return 0; | |
1190 | ||
1191 | ctr = kmalloc(sizeof(*ctr), GFP_KERNEL); | |
1192 | if (!ctr) | |
1193 | goto err0; | |
1194 | ||
1195 | /* | |
1196 | * If cpuset gets more users after we read count, we won't have | |
1197 | * enough space - tough. This race is indistinguishable to the | |
1198 | * caller from the case that the additional cpuset users didn't | |
1199 | * show up until sometime later on. | |
1200 | */ | |
1201 | npids = atomic_read(&cs->count); | |
1202 | pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL); | |
1203 | if (!pidarray) | |
1204 | goto err1; | |
1205 | ||
1206 | npids = pid_array_load(pidarray, npids, cs); | |
1207 | sort(pidarray, npids, sizeof(pid_t), cmppid, NULL); | |
1208 | ||
1209 | /* Call pid_array_to_buf() twice, first just to get bufsz */ | |
1210 | ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1; | |
1211 | ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL); | |
1212 | if (!ctr->buf) | |
1213 | goto err2; | |
1214 | ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids); | |
1215 | ||
1216 | kfree(pidarray); | |
1217 | file->private_data = ctr; | |
1218 | return 0; | |
1219 | ||
1220 | err2: | |
1221 | kfree(pidarray); | |
1222 | err1: | |
1223 | kfree(ctr); | |
1224 | err0: | |
1225 | return -ENOMEM; | |
1226 | } | |
1227 | ||
1228 | static ssize_t cpuset_tasks_read(struct file *file, char __user *buf, | |
1229 | size_t nbytes, loff_t *ppos) | |
1230 | { | |
1231 | struct ctr_struct *ctr = file->private_data; | |
1232 | ||
1233 | if (*ppos + nbytes > ctr->bufsz) | |
1234 | nbytes = ctr->bufsz - *ppos; | |
1235 | if (copy_to_user(buf, ctr->buf + *ppos, nbytes)) | |
1236 | return -EFAULT; | |
1237 | *ppos += nbytes; | |
1238 | return nbytes; | |
1239 | } | |
1240 | ||
1241 | static int cpuset_tasks_release(struct inode *unused_inode, struct file *file) | |
1242 | { | |
1243 | struct ctr_struct *ctr; | |
1244 | ||
1245 | if (file->f_mode & FMODE_READ) { | |
1246 | ctr = file->private_data; | |
1247 | kfree(ctr->buf); | |
1248 | kfree(ctr); | |
1249 | } | |
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * for the common functions, 'private' gives the type of file | |
1255 | */ | |
1256 | ||
1257 | static struct cftype cft_tasks = { | |
1258 | .name = "tasks", | |
1259 | .open = cpuset_tasks_open, | |
1260 | .read = cpuset_tasks_read, | |
1261 | .release = cpuset_tasks_release, | |
1262 | .private = FILE_TASKLIST, | |
1263 | }; | |
1264 | ||
1265 | static struct cftype cft_cpus = { | |
1266 | .name = "cpus", | |
1267 | .private = FILE_CPULIST, | |
1268 | }; | |
1269 | ||
1270 | static struct cftype cft_mems = { | |
1271 | .name = "mems", | |
1272 | .private = FILE_MEMLIST, | |
1273 | }; | |
1274 | ||
1275 | static struct cftype cft_cpu_exclusive = { | |
1276 | .name = "cpu_exclusive", | |
1277 | .private = FILE_CPU_EXCLUSIVE, | |
1278 | }; | |
1279 | ||
1280 | static struct cftype cft_mem_exclusive = { | |
1281 | .name = "mem_exclusive", | |
1282 | .private = FILE_MEM_EXCLUSIVE, | |
1283 | }; | |
1284 | ||
1285 | static struct cftype cft_notify_on_release = { | |
1286 | .name = "notify_on_release", | |
1287 | .private = FILE_NOTIFY_ON_RELEASE, | |
1288 | }; | |
1289 | ||
1290 | static int cpuset_populate_dir(struct dentry *cs_dentry) | |
1291 | { | |
1292 | int err; | |
1293 | ||
1294 | if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0) | |
1295 | return err; | |
1296 | if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0) | |
1297 | return err; | |
1298 | if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0) | |
1299 | return err; | |
1300 | if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0) | |
1301 | return err; | |
1302 | if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0) | |
1303 | return err; | |
1304 | if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0) | |
1305 | return err; | |
1306 | return 0; | |
1307 | } | |
1308 | ||
1309 | /* | |
1310 | * cpuset_create - create a cpuset | |
1311 | * parent: cpuset that will be parent of the new cpuset. | |
1312 | * name: name of the new cpuset. Will be strcpy'ed. | |
1313 | * mode: mode to set on new inode | |
1314 | * | |
1315 | * Must be called with the semaphore on the parent inode held | |
1316 | */ | |
1317 | ||
1318 | static long cpuset_create(struct cpuset *parent, const char *name, int mode) | |
1319 | { | |
1320 | struct cpuset *cs; | |
1321 | int err; | |
1322 | ||
1323 | cs = kmalloc(sizeof(*cs), GFP_KERNEL); | |
1324 | if (!cs) | |
1325 | return -ENOMEM; | |
1326 | ||
5aa15b5f PJ |
1327 | down(&cpuset_sem); |
1328 | refresh_mems(); | |
1da177e4 LT |
1329 | cs->flags = 0; |
1330 | if (notify_on_release(parent)) | |
1331 | set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); | |
1332 | cs->cpus_allowed = CPU_MASK_NONE; | |
1333 | cs->mems_allowed = NODE_MASK_NONE; | |
1334 | atomic_set(&cs->count, 0); | |
1335 | INIT_LIST_HEAD(&cs->sibling); | |
1336 | INIT_LIST_HEAD(&cs->children); | |
1337 | atomic_inc(&cpuset_mems_generation); | |
1338 | cs->mems_generation = atomic_read(&cpuset_mems_generation); | |
1339 | ||
1340 | cs->parent = parent; | |
1341 | ||
1342 | list_add(&cs->sibling, &cs->parent->children); | |
1343 | ||
1344 | err = cpuset_create_dir(cs, name, mode); | |
1345 | if (err < 0) | |
1346 | goto err; | |
1347 | ||
1348 | /* | |
1349 | * Release cpuset_sem before cpuset_populate_dir() because it | |
1350 | * will down() this new directory's i_sem and if we race with | |
1351 | * another mkdir, we might deadlock. | |
1352 | */ | |
5aa15b5f | 1353 | up(&cpuset_sem); |
1da177e4 LT |
1354 | |
1355 | err = cpuset_populate_dir(cs->dentry); | |
1356 | /* If err < 0, we have a half-filled directory - oh well ;) */ | |
1357 | return 0; | |
1358 | err: | |
1359 | list_del(&cs->sibling); | |
5aa15b5f | 1360 | up(&cpuset_sem); |
1da177e4 LT |
1361 | kfree(cs); |
1362 | return err; | |
1363 | } | |
1364 | ||
1365 | static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1366 | { | |
1367 | struct cpuset *c_parent = dentry->d_parent->d_fsdata; | |
1368 | ||
1369 | /* the vfs holds inode->i_sem already */ | |
1370 | return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR); | |
1371 | } | |
1372 | ||
1373 | static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry) | |
1374 | { | |
1375 | struct cpuset *cs = dentry->d_fsdata; | |
1376 | struct dentry *d; | |
1377 | struct cpuset *parent; | |
3077a260 | 1378 | char *pathbuf = NULL; |
1da177e4 LT |
1379 | |
1380 | /* the vfs holds both inode->i_sem already */ | |
1381 | ||
5aa15b5f PJ |
1382 | down(&cpuset_sem); |
1383 | refresh_mems(); | |
1da177e4 | 1384 | if (atomic_read(&cs->count) > 0) { |
5aa15b5f | 1385 | up(&cpuset_sem); |
1da177e4 LT |
1386 | return -EBUSY; |
1387 | } | |
1388 | if (!list_empty(&cs->children)) { | |
5aa15b5f | 1389 | up(&cpuset_sem); |
1da177e4 LT |
1390 | return -EBUSY; |
1391 | } | |
1da177e4 LT |
1392 | parent = cs->parent; |
1393 | set_bit(CS_REMOVED, &cs->flags); | |
85d7b949 DG |
1394 | if (is_cpu_exclusive(cs)) |
1395 | update_cpu_domains(cs); | |
1da177e4 LT |
1396 | list_del(&cs->sibling); /* delete my sibling from parent->children */ |
1397 | if (list_empty(&parent->children)) | |
3077a260 | 1398 | check_for_release(parent, &pathbuf); |
85d7b949 | 1399 | spin_lock(&cs->dentry->d_lock); |
1da177e4 LT |
1400 | d = dget(cs->dentry); |
1401 | cs->dentry = NULL; | |
1402 | spin_unlock(&d->d_lock); | |
1403 | cpuset_d_remove_dir(d); | |
1404 | dput(d); | |
5aa15b5f | 1405 | up(&cpuset_sem); |
3077a260 | 1406 | cpuset_release_agent(pathbuf); |
1da177e4 LT |
1407 | return 0; |
1408 | } | |
1409 | ||
1410 | /** | |
1411 | * cpuset_init - initialize cpusets at system boot | |
1412 | * | |
1413 | * Description: Initialize top_cpuset and the cpuset internal file system, | |
1414 | **/ | |
1415 | ||
1416 | int __init cpuset_init(void) | |
1417 | { | |
1418 | struct dentry *root; | |
1419 | int err; | |
1420 | ||
1421 | top_cpuset.cpus_allowed = CPU_MASK_ALL; | |
1422 | top_cpuset.mems_allowed = NODE_MASK_ALL; | |
1423 | ||
1424 | atomic_inc(&cpuset_mems_generation); | |
1425 | top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation); | |
1426 | ||
1427 | init_task.cpuset = &top_cpuset; | |
1428 | ||
1429 | err = register_filesystem(&cpuset_fs_type); | |
1430 | if (err < 0) | |
1431 | goto out; | |
1432 | cpuset_mount = kern_mount(&cpuset_fs_type); | |
1433 | if (IS_ERR(cpuset_mount)) { | |
1434 | printk(KERN_ERR "cpuset: could not mount!\n"); | |
1435 | err = PTR_ERR(cpuset_mount); | |
1436 | cpuset_mount = NULL; | |
1437 | goto out; | |
1438 | } | |
1439 | root = cpuset_mount->mnt_sb->s_root; | |
1440 | root->d_fsdata = &top_cpuset; | |
1441 | root->d_inode->i_nlink++; | |
1442 | top_cpuset.dentry = root; | |
1443 | root->d_inode->i_op = &cpuset_dir_inode_operations; | |
1444 | err = cpuset_populate_dir(root); | |
1445 | out: | |
1446 | return err; | |
1447 | } | |
1448 | ||
1449 | /** | |
1450 | * cpuset_init_smp - initialize cpus_allowed | |
1451 | * | |
1452 | * Description: Finish top cpuset after cpu, node maps are initialized | |
1453 | **/ | |
1454 | ||
1455 | void __init cpuset_init_smp(void) | |
1456 | { | |
1457 | top_cpuset.cpus_allowed = cpu_online_map; | |
1458 | top_cpuset.mems_allowed = node_online_map; | |
1459 | } | |
1460 | ||
1461 | /** | |
1462 | * cpuset_fork - attach newly forked task to its parents cpuset. | |
d9fd8a6d | 1463 | * @tsk: pointer to task_struct of forking parent process. |
1da177e4 LT |
1464 | * |
1465 | * Description: By default, on fork, a task inherits its | |
d9fd8a6d | 1466 | * parent's cpuset. The pointer to the shared cpuset is |
1da177e4 LT |
1467 | * automatically copied in fork.c by dup_task_struct(). |
1468 | * This cpuset_fork() routine need only increment the usage | |
1469 | * counter in that cpuset. | |
1470 | **/ | |
1471 | ||
1472 | void cpuset_fork(struct task_struct *tsk) | |
1473 | { | |
1474 | atomic_inc(&tsk->cpuset->count); | |
1475 | } | |
1476 | ||
1477 | /** | |
1478 | * cpuset_exit - detach cpuset from exiting task | |
1479 | * @tsk: pointer to task_struct of exiting process | |
1480 | * | |
1481 | * Description: Detach cpuset from @tsk and release it. | |
1482 | * | |
2efe86b8 PJ |
1483 | * Note that cpusets marked notify_on_release force every task |
1484 | * in them to take the global cpuset_sem semaphore when exiting. | |
1485 | * This could impact scaling on very large systems. Be reluctant | |
1486 | * to use notify_on_release cpusets where very high task exit | |
1487 | * scaling is required on large systems. | |
1488 | * | |
1489 | * Don't even think about derefencing 'cs' after the cpuset use | |
1490 | * count goes to zero, except inside a critical section guarded | |
1491 | * by the cpuset_sem semaphore. If you don't hold cpuset_sem, | |
1492 | * then a zero cpuset use count is a license to any other task to | |
1493 | * nuke the cpuset immediately. | |
1da177e4 LT |
1494 | **/ |
1495 | ||
1496 | void cpuset_exit(struct task_struct *tsk) | |
1497 | { | |
1498 | struct cpuset *cs; | |
1499 | ||
1500 | task_lock(tsk); | |
1501 | cs = tsk->cpuset; | |
1502 | tsk->cpuset = NULL; | |
1503 | task_unlock(tsk); | |
1504 | ||
2efe86b8 | 1505 | if (notify_on_release(cs)) { |
3077a260 PJ |
1506 | char *pathbuf = NULL; |
1507 | ||
5aa15b5f | 1508 | down(&cpuset_sem); |
2efe86b8 | 1509 | if (atomic_dec_and_test(&cs->count)) |
3077a260 | 1510 | check_for_release(cs, &pathbuf); |
5aa15b5f | 1511 | up(&cpuset_sem); |
3077a260 | 1512 | cpuset_release_agent(pathbuf); |
2efe86b8 PJ |
1513 | } else { |
1514 | atomic_dec(&cs->count); | |
1da177e4 LT |
1515 | } |
1516 | } | |
1517 | ||
1518 | /** | |
1519 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. | |
1520 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. | |
1521 | * | |
1522 | * Description: Returns the cpumask_t cpus_allowed of the cpuset | |
1523 | * attached to the specified @tsk. Guaranteed to return some non-empty | |
1524 | * subset of cpu_online_map, even if this means going outside the | |
1525 | * tasks cpuset. | |
1526 | **/ | |
1527 | ||
9a848896 | 1528 | cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk) |
1da177e4 LT |
1529 | { |
1530 | cpumask_t mask; | |
1531 | ||
5aa15b5f | 1532 | down(&cpuset_sem); |
1da177e4 LT |
1533 | task_lock((struct task_struct *)tsk); |
1534 | guarantee_online_cpus(tsk->cpuset, &mask); | |
1535 | task_unlock((struct task_struct *)tsk); | |
5aa15b5f | 1536 | up(&cpuset_sem); |
1da177e4 LT |
1537 | |
1538 | return mask; | |
1539 | } | |
1540 | ||
1541 | void cpuset_init_current_mems_allowed(void) | |
1542 | { | |
1543 | current->mems_allowed = NODE_MASK_ALL; | |
1544 | } | |
1545 | ||
d9fd8a6d RD |
1546 | /** |
1547 | * cpuset_update_current_mems_allowed - update mems parameters to new values | |
1548 | * | |
1da177e4 LT |
1549 | * If the current tasks cpusets mems_allowed changed behind our backs, |
1550 | * update current->mems_allowed and mems_generation to the new value. | |
1551 | * Do not call this routine if in_interrupt(). | |
1552 | */ | |
1553 | ||
1554 | void cpuset_update_current_mems_allowed(void) | |
1555 | { | |
1556 | struct cpuset *cs = current->cpuset; | |
1557 | ||
1558 | if (!cs) | |
1559 | return; /* task is exiting */ | |
1560 | if (current->cpuset_mems_generation != cs->mems_generation) { | |
5aa15b5f | 1561 | down(&cpuset_sem); |
1da177e4 | 1562 | refresh_mems(); |
5aa15b5f | 1563 | up(&cpuset_sem); |
1da177e4 LT |
1564 | } |
1565 | } | |
1566 | ||
d9fd8a6d RD |
1567 | /** |
1568 | * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed | |
1569 | * @nodes: pointer to a node bitmap that is and-ed with mems_allowed | |
1570 | */ | |
1da177e4 LT |
1571 | void cpuset_restrict_to_mems_allowed(unsigned long *nodes) |
1572 | { | |
1573 | bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed), | |
1574 | MAX_NUMNODES); | |
1575 | } | |
1576 | ||
d9fd8a6d RD |
1577 | /** |
1578 | * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed | |
1579 | * @zl: the zonelist to be checked | |
1580 | * | |
1da177e4 LT |
1581 | * Are any of the nodes on zonelist zl allowed in current->mems_allowed? |
1582 | */ | |
1583 | int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl) | |
1584 | { | |
1585 | int i; | |
1586 | ||
1587 | for (i = 0; zl->zones[i]; i++) { | |
1588 | int nid = zl->zones[i]->zone_pgdat->node_id; | |
1589 | ||
1590 | if (node_isset(nid, current->mems_allowed)) | |
1591 | return 1; | |
1592 | } | |
1593 | return 0; | |
1594 | } | |
1595 | ||
9bf2229f PJ |
1596 | /* |
1597 | * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive | |
1598 | * ancestor to the specified cpuset. Call while holding cpuset_sem. | |
1599 | * If no ancestor is mem_exclusive (an unusual configuration), then | |
1600 | * returns the root cpuset. | |
1601 | */ | |
1602 | static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs) | |
1603 | { | |
1604 | while (!is_mem_exclusive(cs) && cs->parent) | |
1605 | cs = cs->parent; | |
1606 | return cs; | |
1607 | } | |
1608 | ||
d9fd8a6d | 1609 | /** |
9bf2229f PJ |
1610 | * cpuset_zone_allowed - Can we allocate memory on zone z's memory node? |
1611 | * @z: is this zone on an allowed node? | |
1612 | * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL) | |
d9fd8a6d | 1613 | * |
9bf2229f PJ |
1614 | * If we're in interrupt, yes, we can always allocate. If zone |
1615 | * z's node is in our tasks mems_allowed, yes. If it's not a | |
1616 | * __GFP_HARDWALL request and this zone's nodes is in the nearest | |
1617 | * mem_exclusive cpuset ancestor to this tasks cpuset, yes. | |
1618 | * Otherwise, no. | |
1619 | * | |
1620 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, | |
1621 | * and do not allow allocations outside the current tasks cpuset. | |
1622 | * GFP_KERNEL allocations are not so marked, so can escape to the | |
1623 | * nearest mem_exclusive ancestor cpuset. | |
1624 | * | |
1625 | * Scanning up parent cpusets requires cpuset_sem. The __alloc_pages() | |
1626 | * routine only calls here with __GFP_HARDWALL bit _not_ set if | |
1627 | * it's a GFP_KERNEL allocation, and all nodes in the current tasks | |
1628 | * mems_allowed came up empty on the first pass over the zonelist. | |
1629 | * So only GFP_KERNEL allocations, if all nodes in the cpuset are | |
1630 | * short of memory, might require taking the cpuset_sem semaphore. | |
1631 | * | |
1632 | * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages() | |
1633 | * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing | |
1634 | * hardwall cpusets - no allocation on a node outside the cpuset is | |
1635 | * allowed (unless in interrupt, of course). | |
1636 | * | |
1637 | * The second loop doesn't even call here for GFP_ATOMIC requests | |
1638 | * (if the __alloc_pages() local variable 'wait' is set). That check | |
1639 | * and the checks below have the combined affect in the second loop of | |
1640 | * the __alloc_pages() routine that: | |
1641 | * in_interrupt - any node ok (current task context irrelevant) | |
1642 | * GFP_ATOMIC - any node ok | |
1643 | * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok | |
1644 | * GFP_USER - only nodes in current tasks mems allowed ok. | |
1645 | **/ | |
1646 | ||
dd0fc66f | 1647 | int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask) |
1da177e4 | 1648 | { |
9bf2229f PJ |
1649 | int node; /* node that zone z is on */ |
1650 | const struct cpuset *cs; /* current cpuset ancestors */ | |
1651 | int allowed = 1; /* is allocation in zone z allowed? */ | |
1652 | ||
1653 | if (in_interrupt()) | |
1654 | return 1; | |
1655 | node = z->zone_pgdat->node_id; | |
1656 | if (node_isset(node, current->mems_allowed)) | |
1657 | return 1; | |
1658 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ | |
1659 | return 0; | |
1660 | ||
1661 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ | |
5aa15b5f | 1662 | down(&cpuset_sem); |
9bf2229f PJ |
1663 | cs = current->cpuset; |
1664 | if (!cs) | |
1665 | goto done; /* current task exiting */ | |
1666 | cs = nearest_exclusive_ancestor(cs); | |
1667 | allowed = node_isset(node, cs->mems_allowed); | |
1668 | done: | |
5aa15b5f | 1669 | up(&cpuset_sem); |
9bf2229f | 1670 | return allowed; |
1da177e4 LT |
1671 | } |
1672 | ||
ef08e3b4 PJ |
1673 | /** |
1674 | * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors? | |
1675 | * @p: pointer to task_struct of some other task. | |
1676 | * | |
1677 | * Description: Return true if the nearest mem_exclusive ancestor | |
1678 | * cpusets of tasks @p and current overlap. Used by oom killer to | |
1679 | * determine if task @p's memory usage might impact the memory | |
1680 | * available to the current task. | |
1681 | * | |
1682 | * Acquires cpuset_sem - not suitable for calling from a fast path. | |
1683 | **/ | |
1684 | ||
1685 | int cpuset_excl_nodes_overlap(const struct task_struct *p) | |
1686 | { | |
1687 | const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */ | |
1688 | int overlap = 0; /* do cpusets overlap? */ | |
1689 | ||
5aa15b5f | 1690 | down(&cpuset_sem); |
ef08e3b4 PJ |
1691 | cs1 = current->cpuset; |
1692 | if (!cs1) | |
1693 | goto done; /* current task exiting */ | |
1694 | cs2 = p->cpuset; | |
1695 | if (!cs2) | |
1696 | goto done; /* task p is exiting */ | |
1697 | cs1 = nearest_exclusive_ancestor(cs1); | |
1698 | cs2 = nearest_exclusive_ancestor(cs2); | |
1699 | overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed); | |
1700 | done: | |
5aa15b5f | 1701 | up(&cpuset_sem); |
ef08e3b4 PJ |
1702 | |
1703 | return overlap; | |
1704 | } | |
1705 | ||
1da177e4 LT |
1706 | /* |
1707 | * proc_cpuset_show() | |
1708 | * - Print tasks cpuset path into seq_file. | |
1709 | * - Used for /proc/<pid>/cpuset. | |
1710 | */ | |
1711 | ||
1712 | static int proc_cpuset_show(struct seq_file *m, void *v) | |
1713 | { | |
1714 | struct cpuset *cs; | |
1715 | struct task_struct *tsk; | |
1716 | char *buf; | |
1717 | int retval = 0; | |
1718 | ||
1719 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1720 | if (!buf) | |
1721 | return -ENOMEM; | |
1722 | ||
1723 | tsk = m->private; | |
5aa15b5f | 1724 | down(&cpuset_sem); |
1da177e4 LT |
1725 | task_lock(tsk); |
1726 | cs = tsk->cpuset; | |
1727 | task_unlock(tsk); | |
1728 | if (!cs) { | |
1729 | retval = -EINVAL; | |
1730 | goto out; | |
1731 | } | |
1732 | ||
1733 | retval = cpuset_path(cs, buf, PAGE_SIZE); | |
1734 | if (retval < 0) | |
1735 | goto out; | |
1736 | seq_puts(m, buf); | |
1737 | seq_putc(m, '\n'); | |
1738 | out: | |
5aa15b5f | 1739 | up(&cpuset_sem); |
1da177e4 LT |
1740 | kfree(buf); |
1741 | return retval; | |
1742 | } | |
1743 | ||
1744 | static int cpuset_open(struct inode *inode, struct file *file) | |
1745 | { | |
1746 | struct task_struct *tsk = PROC_I(inode)->task; | |
1747 | return single_open(file, proc_cpuset_show, tsk); | |
1748 | } | |
1749 | ||
1750 | struct file_operations proc_cpuset_operations = { | |
1751 | .open = cpuset_open, | |
1752 | .read = seq_read, | |
1753 | .llseek = seq_lseek, | |
1754 | .release = single_release, | |
1755 | }; | |
1756 | ||
1757 | /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ | |
1758 | char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) | |
1759 | { | |
1760 | buffer += sprintf(buffer, "Cpus_allowed:\t"); | |
1761 | buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed); | |
1762 | buffer += sprintf(buffer, "\n"); | |
1763 | buffer += sprintf(buffer, "Mems_allowed:\t"); | |
1764 | buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed); | |
1765 | buffer += sprintf(buffer, "\n"); | |
1766 | return buffer; | |
1767 | } |