]>
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 | ||
053199ed PJ |
63 | /* |
64 | * Count is atomic so can incr (fork) or decr (exit) without a lock. | |
65 | */ | |
1da177e4 LT |
66 | atomic_t count; /* count tasks using this cpuset */ |
67 | ||
68 | /* | |
69 | * We link our 'sibling' struct into our parents 'children'. | |
70 | * Our children link their 'sibling' into our 'children'. | |
71 | */ | |
72 | struct list_head sibling; /* my parents children */ | |
73 | struct list_head children; /* my children */ | |
74 | ||
75 | struct cpuset *parent; /* my parent */ | |
76 | struct dentry *dentry; /* cpuset fs entry */ | |
77 | ||
78 | /* | |
79 | * Copy of global cpuset_mems_generation as of the most | |
80 | * recent time this cpuset changed its mems_allowed. | |
81 | */ | |
82 | int mems_generation; | |
83 | }; | |
84 | ||
85 | /* bits in struct cpuset flags field */ | |
86 | typedef enum { | |
87 | CS_CPU_EXCLUSIVE, | |
88 | CS_MEM_EXCLUSIVE, | |
89 | CS_REMOVED, | |
90 | CS_NOTIFY_ON_RELEASE | |
91 | } cpuset_flagbits_t; | |
92 | ||
93 | /* convenient tests for these bits */ | |
94 | static inline int is_cpu_exclusive(const struct cpuset *cs) | |
95 | { | |
96 | return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags); | |
97 | } | |
98 | ||
99 | static inline int is_mem_exclusive(const struct cpuset *cs) | |
100 | { | |
101 | return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags); | |
102 | } | |
103 | ||
104 | static inline int is_removed(const struct cpuset *cs) | |
105 | { | |
106 | return !!test_bit(CS_REMOVED, &cs->flags); | |
107 | } | |
108 | ||
109 | static inline int notify_on_release(const struct cpuset *cs) | |
110 | { | |
111 | return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); | |
112 | } | |
113 | ||
114 | /* | |
115 | * Increment this atomic integer everytime any cpuset changes its | |
116 | * mems_allowed value. Users of cpusets can track this generation | |
117 | * number, and avoid having to lock and reload mems_allowed unless | |
118 | * the cpuset they're using changes generation. | |
119 | * | |
120 | * A single, global generation is needed because attach_task() could | |
121 | * reattach a task to a different cpuset, which must not have its | |
122 | * generation numbers aliased with those of that tasks previous cpuset. | |
123 | * | |
124 | * Generations are needed for mems_allowed because one task cannot | |
125 | * modify anothers memory placement. So we must enable every task, | |
126 | * on every visit to __alloc_pages(), to efficiently check whether | |
127 | * its current->cpuset->mems_allowed has changed, requiring an update | |
128 | * of its current->mems_allowed. | |
129 | */ | |
130 | static atomic_t cpuset_mems_generation = ATOMIC_INIT(1); | |
131 | ||
132 | static struct cpuset top_cpuset = { | |
133 | .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)), | |
134 | .cpus_allowed = CPU_MASK_ALL, | |
135 | .mems_allowed = NODE_MASK_ALL, | |
136 | .count = ATOMIC_INIT(0), | |
137 | .sibling = LIST_HEAD_INIT(top_cpuset.sibling), | |
138 | .children = LIST_HEAD_INIT(top_cpuset.children), | |
139 | .parent = NULL, | |
140 | .dentry = NULL, | |
141 | .mems_generation = 0, | |
142 | }; | |
143 | ||
144 | static struct vfsmount *cpuset_mount; | |
145 | static struct super_block *cpuset_sb = NULL; | |
146 | ||
147 | /* | |
053199ed PJ |
148 | * We have two global cpuset semaphores below. They can nest. |
149 | * It is ok to first take manage_sem, then nest callback_sem. We also | |
150 | * require taking task_lock() when dereferencing a tasks cpuset pointer. | |
151 | * See "The task_lock() exception", at the end of this comment. | |
152 | * | |
153 | * A task must hold both semaphores to modify cpusets. If a task | |
154 | * holds manage_sem, then it blocks others wanting that semaphore, | |
155 | * ensuring that it is the only task able to also acquire callback_sem | |
156 | * and be able to modify cpusets. It can perform various checks on | |
157 | * the cpuset structure first, knowing nothing will change. It can | |
158 | * also allocate memory while just holding manage_sem. While it is | |
159 | * performing these checks, various callback routines can briefly | |
160 | * acquire callback_sem to query cpusets. Once it is ready to make | |
161 | * the changes, it takes callback_sem, blocking everyone else. | |
162 | * | |
163 | * Calls to the kernel memory allocator can not be made while holding | |
164 | * callback_sem, as that would risk double tripping on callback_sem | |
165 | * from one of the callbacks into the cpuset code from within | |
166 | * __alloc_pages(). | |
167 | * | |
168 | * If a task is only holding callback_sem, then it has read-only | |
169 | * access to cpusets. | |
170 | * | |
171 | * The task_struct fields mems_allowed and mems_generation may only | |
172 | * be accessed in the context of that task, so require no locks. | |
173 | * | |
174 | * Any task can increment and decrement the count field without lock. | |
175 | * So in general, code holding manage_sem or callback_sem can't rely | |
176 | * on the count field not changing. However, if the count goes to | |
177 | * zero, then only attach_task(), which holds both semaphores, can | |
178 | * increment it again. Because a count of zero means that no tasks | |
179 | * are currently attached, therefore there is no way a task attached | |
180 | * to that cpuset can fork (the other way to increment the count). | |
181 | * So code holding manage_sem or callback_sem can safely assume that | |
182 | * if the count is zero, it will stay zero. Similarly, if a task | |
183 | * holds manage_sem or callback_sem on a cpuset with zero count, it | |
184 | * knows that the cpuset won't be removed, as cpuset_rmdir() needs | |
185 | * both of those semaphores. | |
186 | * | |
187 | * A possible optimization to improve parallelism would be to make | |
188 | * callback_sem a R/W semaphore (rwsem), allowing the callback routines | |
189 | * to proceed in parallel, with read access, until the holder of | |
190 | * manage_sem needed to take this rwsem for exclusive write access | |
191 | * and modify some cpusets. | |
192 | * | |
193 | * The cpuset_common_file_write handler for operations that modify | |
194 | * the cpuset hierarchy holds manage_sem across the entire operation, | |
195 | * single threading all such cpuset modifications across the system. | |
196 | * | |
197 | * The cpuset_common_file_read() handlers only hold callback_sem across | |
198 | * small pieces of code, such as when reading out possibly multi-word | |
199 | * cpumasks and nodemasks. | |
200 | * | |
201 | * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't | |
202 | * (usually) take either semaphore. These are the two most performance | |
203 | * critical pieces of code here. The exception occurs on cpuset_exit(), | |
204 | * when a task in a notify_on_release cpuset exits. Then manage_sem | |
2efe86b8 | 205 | * is taken, and if the cpuset count is zero, a usermode call made |
1da177e4 LT |
206 | * to /sbin/cpuset_release_agent with the name of the cpuset (path |
207 | * relative to the root of cpuset file system) as the argument. | |
208 | * | |
053199ed PJ |
209 | * A cpuset can only be deleted if both its 'count' of using tasks |
210 | * is zero, and its list of 'children' cpusets is empty. Since all | |
211 | * tasks in the system use _some_ cpuset, and since there is always at | |
212 | * least one task in the system (init, pid == 1), therefore, top_cpuset | |
213 | * always has either children cpusets and/or using tasks. So we don't | |
214 | * need a special hack to ensure that top_cpuset cannot be deleted. | |
215 | * | |
216 | * The above "Tale of Two Semaphores" would be complete, but for: | |
217 | * | |
218 | * The task_lock() exception | |
219 | * | |
220 | * The need for this exception arises from the action of attach_task(), | |
221 | * which overwrites one tasks cpuset pointer with another. It does | |
222 | * so using both semaphores, however there are several performance | |
223 | * critical places that need to reference task->cpuset without the | |
224 | * expense of grabbing a system global semaphore. Therefore except as | |
225 | * noted below, when dereferencing or, as in attach_task(), modifying | |
226 | * a tasks cpuset pointer we use task_lock(), which acts on a spinlock | |
227 | * (task->alloc_lock) already in the task_struct routinely used for | |
228 | * such matters. | |
1da177e4 LT |
229 | */ |
230 | ||
053199ed PJ |
231 | static DECLARE_MUTEX(manage_sem); |
232 | static DECLARE_MUTEX(callback_sem); | |
4247bdc6 | 233 | |
1da177e4 LT |
234 | /* |
235 | * A couple of forward declarations required, due to cyclic reference loop: | |
236 | * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file | |
237 | * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir. | |
238 | */ | |
239 | ||
240 | static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode); | |
241 | static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry); | |
242 | ||
243 | static struct backing_dev_info cpuset_backing_dev_info = { | |
244 | .ra_pages = 0, /* No readahead */ | |
245 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, | |
246 | }; | |
247 | ||
248 | static struct inode *cpuset_new_inode(mode_t mode) | |
249 | { | |
250 | struct inode *inode = new_inode(cpuset_sb); | |
251 | ||
252 | if (inode) { | |
253 | inode->i_mode = mode; | |
254 | inode->i_uid = current->fsuid; | |
255 | inode->i_gid = current->fsgid; | |
256 | inode->i_blksize = PAGE_CACHE_SIZE; | |
257 | inode->i_blocks = 0; | |
258 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
259 | inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info; | |
260 | } | |
261 | return inode; | |
262 | } | |
263 | ||
264 | static void cpuset_diput(struct dentry *dentry, struct inode *inode) | |
265 | { | |
266 | /* is dentry a directory ? if so, kfree() associated cpuset */ | |
267 | if (S_ISDIR(inode->i_mode)) { | |
268 | struct cpuset *cs = dentry->d_fsdata; | |
269 | BUG_ON(!(is_removed(cs))); | |
270 | kfree(cs); | |
271 | } | |
272 | iput(inode); | |
273 | } | |
274 | ||
275 | static struct dentry_operations cpuset_dops = { | |
276 | .d_iput = cpuset_diput, | |
277 | }; | |
278 | ||
279 | static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name) | |
280 | { | |
5f45f1a7 | 281 | struct dentry *d = lookup_one_len(name, parent, strlen(name)); |
1da177e4 LT |
282 | if (!IS_ERR(d)) |
283 | d->d_op = &cpuset_dops; | |
284 | return d; | |
285 | } | |
286 | ||
287 | static void remove_dir(struct dentry *d) | |
288 | { | |
289 | struct dentry *parent = dget(d->d_parent); | |
290 | ||
291 | d_delete(d); | |
292 | simple_rmdir(parent->d_inode, d); | |
293 | dput(parent); | |
294 | } | |
295 | ||
296 | /* | |
297 | * NOTE : the dentry must have been dget()'ed | |
298 | */ | |
299 | static void cpuset_d_remove_dir(struct dentry *dentry) | |
300 | { | |
301 | struct list_head *node; | |
302 | ||
303 | spin_lock(&dcache_lock); | |
304 | node = dentry->d_subdirs.next; | |
305 | while (node != &dentry->d_subdirs) { | |
306 | struct dentry *d = list_entry(node, struct dentry, d_child); | |
307 | list_del_init(node); | |
308 | if (d->d_inode) { | |
309 | d = dget_locked(d); | |
310 | spin_unlock(&dcache_lock); | |
311 | d_delete(d); | |
312 | simple_unlink(dentry->d_inode, d); | |
313 | dput(d); | |
314 | spin_lock(&dcache_lock); | |
315 | } | |
316 | node = dentry->d_subdirs.next; | |
317 | } | |
318 | list_del_init(&dentry->d_child); | |
319 | spin_unlock(&dcache_lock); | |
320 | remove_dir(dentry); | |
321 | } | |
322 | ||
323 | static struct super_operations cpuset_ops = { | |
324 | .statfs = simple_statfs, | |
325 | .drop_inode = generic_delete_inode, | |
326 | }; | |
327 | ||
328 | static int cpuset_fill_super(struct super_block *sb, void *unused_data, | |
329 | int unused_silent) | |
330 | { | |
331 | struct inode *inode; | |
332 | struct dentry *root; | |
333 | ||
334 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
335 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
336 | sb->s_magic = CPUSET_SUPER_MAGIC; | |
337 | sb->s_op = &cpuset_ops; | |
338 | cpuset_sb = sb; | |
339 | ||
340 | inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR); | |
341 | if (inode) { | |
342 | inode->i_op = &simple_dir_inode_operations; | |
343 | inode->i_fop = &simple_dir_operations; | |
344 | /* directories start off with i_nlink == 2 (for "." entry) */ | |
345 | inode->i_nlink++; | |
346 | } else { | |
347 | return -ENOMEM; | |
348 | } | |
349 | ||
350 | root = d_alloc_root(inode); | |
351 | if (!root) { | |
352 | iput(inode); | |
353 | return -ENOMEM; | |
354 | } | |
355 | sb->s_root = root; | |
356 | return 0; | |
357 | } | |
358 | ||
359 | static struct super_block *cpuset_get_sb(struct file_system_type *fs_type, | |
360 | int flags, const char *unused_dev_name, | |
361 | void *data) | |
362 | { | |
363 | return get_sb_single(fs_type, flags, data, cpuset_fill_super); | |
364 | } | |
365 | ||
366 | static struct file_system_type cpuset_fs_type = { | |
367 | .name = "cpuset", | |
368 | .get_sb = cpuset_get_sb, | |
369 | .kill_sb = kill_litter_super, | |
370 | }; | |
371 | ||
372 | /* struct cftype: | |
373 | * | |
374 | * The files in the cpuset filesystem mostly have a very simple read/write | |
375 | * handling, some common function will take care of it. Nevertheless some cases | |
376 | * (read tasks) are special and therefore I define this structure for every | |
377 | * kind of file. | |
378 | * | |
379 | * | |
380 | * When reading/writing to a file: | |
381 | * - the cpuset to use in file->f_dentry->d_parent->d_fsdata | |
382 | * - the 'cftype' of the file is file->f_dentry->d_fsdata | |
383 | */ | |
384 | ||
385 | struct cftype { | |
386 | char *name; | |
387 | int private; | |
388 | int (*open) (struct inode *inode, struct file *file); | |
389 | ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes, | |
390 | loff_t *ppos); | |
391 | int (*write) (struct file *file, const char __user *buf, size_t nbytes, | |
392 | loff_t *ppos); | |
393 | int (*release) (struct inode *inode, struct file *file); | |
394 | }; | |
395 | ||
396 | static inline struct cpuset *__d_cs(struct dentry *dentry) | |
397 | { | |
398 | return dentry->d_fsdata; | |
399 | } | |
400 | ||
401 | static inline struct cftype *__d_cft(struct dentry *dentry) | |
402 | { | |
403 | return dentry->d_fsdata; | |
404 | } | |
405 | ||
406 | /* | |
053199ed | 407 | * Call with manage_sem held. Writes path of cpuset into buf. |
1da177e4 LT |
408 | * Returns 0 on success, -errno on error. |
409 | */ | |
410 | ||
411 | static int cpuset_path(const struct cpuset *cs, char *buf, int buflen) | |
412 | { | |
413 | char *start; | |
414 | ||
415 | start = buf + buflen; | |
416 | ||
417 | *--start = '\0'; | |
418 | for (;;) { | |
419 | int len = cs->dentry->d_name.len; | |
420 | if ((start -= len) < buf) | |
421 | return -ENAMETOOLONG; | |
422 | memcpy(start, cs->dentry->d_name.name, len); | |
423 | cs = cs->parent; | |
424 | if (!cs) | |
425 | break; | |
426 | if (!cs->parent) | |
427 | continue; | |
428 | if (--start < buf) | |
429 | return -ENAMETOOLONG; | |
430 | *start = '/'; | |
431 | } | |
432 | memmove(buf, start, buf + buflen - start); | |
433 | return 0; | |
434 | } | |
435 | ||
436 | /* | |
437 | * Notify userspace when a cpuset is released, by running | |
438 | * /sbin/cpuset_release_agent with the name of the cpuset (path | |
439 | * relative to the root of cpuset file system) as the argument. | |
440 | * | |
441 | * Most likely, this user command will try to rmdir this cpuset. | |
442 | * | |
443 | * This races with the possibility that some other task will be | |
444 | * attached to this cpuset before it is removed, or that some other | |
445 | * user task will 'mkdir' a child cpuset of this cpuset. That's ok. | |
446 | * The presumed 'rmdir' will fail quietly if this cpuset is no longer | |
447 | * unused, and this cpuset will be reprieved from its death sentence, | |
448 | * to continue to serve a useful existence. Next time it's released, | |
449 | * we will get notified again, if it still has 'notify_on_release' set. | |
450 | * | |
3077a260 PJ |
451 | * The final arg to call_usermodehelper() is 0, which means don't |
452 | * wait. The separate /sbin/cpuset_release_agent task is forked by | |
453 | * call_usermodehelper(), then control in this thread returns here, | |
454 | * without waiting for the release agent task. We don't bother to | |
455 | * wait because the caller of this routine has no use for the exit | |
456 | * status of the /sbin/cpuset_release_agent task, so no sense holding | |
457 | * our caller up for that. | |
458 | * | |
053199ed PJ |
459 | * When we had only one cpuset semaphore, we had to call this |
460 | * without holding it, to avoid deadlock when call_usermodehelper() | |
461 | * allocated memory. With two locks, we could now call this while | |
462 | * holding manage_sem, but we still don't, so as to minimize | |
463 | * the time manage_sem is held. | |
1da177e4 LT |
464 | */ |
465 | ||
3077a260 | 466 | static void cpuset_release_agent(const char *pathbuf) |
1da177e4 LT |
467 | { |
468 | char *argv[3], *envp[3]; | |
469 | int i; | |
470 | ||
3077a260 PJ |
471 | if (!pathbuf) |
472 | return; | |
473 | ||
1da177e4 LT |
474 | i = 0; |
475 | argv[i++] = "/sbin/cpuset_release_agent"; | |
3077a260 | 476 | argv[i++] = (char *)pathbuf; |
1da177e4 LT |
477 | argv[i] = NULL; |
478 | ||
479 | i = 0; | |
480 | /* minimal command environment */ | |
481 | envp[i++] = "HOME=/"; | |
482 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; | |
483 | envp[i] = NULL; | |
484 | ||
3077a260 PJ |
485 | call_usermodehelper(argv[0], argv, envp, 0); |
486 | kfree(pathbuf); | |
1da177e4 LT |
487 | } |
488 | ||
489 | /* | |
490 | * Either cs->count of using tasks transitioned to zero, or the | |
491 | * cs->children list of child cpusets just became empty. If this | |
492 | * cs is notify_on_release() and now both the user count is zero and | |
3077a260 PJ |
493 | * the list of children is empty, prepare cpuset path in a kmalloc'd |
494 | * buffer, to be returned via ppathbuf, so that the caller can invoke | |
053199ed PJ |
495 | * cpuset_release_agent() with it later on, once manage_sem is dropped. |
496 | * Call here with manage_sem held. | |
3077a260 PJ |
497 | * |
498 | * This check_for_release() routine is responsible for kmalloc'ing | |
499 | * pathbuf. The above cpuset_release_agent() is responsible for | |
500 | * kfree'ing pathbuf. The caller of these routines is responsible | |
501 | * for providing a pathbuf pointer, initialized to NULL, then | |
053199ed PJ |
502 | * calling check_for_release() with manage_sem held and the address |
503 | * of the pathbuf pointer, then dropping manage_sem, then calling | |
3077a260 | 504 | * cpuset_release_agent() with pathbuf, as set by check_for_release(). |
1da177e4 LT |
505 | */ |
506 | ||
3077a260 | 507 | static void check_for_release(struct cpuset *cs, char **ppathbuf) |
1da177e4 LT |
508 | { |
509 | if (notify_on_release(cs) && atomic_read(&cs->count) == 0 && | |
510 | list_empty(&cs->children)) { | |
511 | char *buf; | |
512 | ||
513 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
514 | if (!buf) | |
515 | return; | |
516 | if (cpuset_path(cs, buf, PAGE_SIZE) < 0) | |
3077a260 PJ |
517 | kfree(buf); |
518 | else | |
519 | *ppathbuf = buf; | |
1da177e4 LT |
520 | } |
521 | } | |
522 | ||
523 | /* | |
524 | * Return in *pmask the portion of a cpusets's cpus_allowed that | |
525 | * are online. If none are online, walk up the cpuset hierarchy | |
526 | * until we find one that does have some online cpus. If we get | |
527 | * all the way to the top and still haven't found any online cpus, | |
528 | * return cpu_online_map. Or if passed a NULL cs from an exit'ing | |
529 | * task, return cpu_online_map. | |
530 | * | |
531 | * One way or another, we guarantee to return some non-empty subset | |
532 | * of cpu_online_map. | |
533 | * | |
053199ed | 534 | * Call with callback_sem held. |
1da177e4 LT |
535 | */ |
536 | ||
537 | static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask) | |
538 | { | |
539 | while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map)) | |
540 | cs = cs->parent; | |
541 | if (cs) | |
542 | cpus_and(*pmask, cs->cpus_allowed, cpu_online_map); | |
543 | else | |
544 | *pmask = cpu_online_map; | |
545 | BUG_ON(!cpus_intersects(*pmask, cpu_online_map)); | |
546 | } | |
547 | ||
548 | /* | |
549 | * Return in *pmask the portion of a cpusets's mems_allowed that | |
550 | * are online. If none are online, walk up the cpuset hierarchy | |
551 | * until we find one that does have some online mems. If we get | |
552 | * all the way to the top and still haven't found any online mems, | |
553 | * return node_online_map. | |
554 | * | |
555 | * One way or another, we guarantee to return some non-empty subset | |
556 | * of node_online_map. | |
557 | * | |
053199ed | 558 | * Call with callback_sem held. |
1da177e4 LT |
559 | */ |
560 | ||
561 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) | |
562 | { | |
563 | while (cs && !nodes_intersects(cs->mems_allowed, node_online_map)) | |
564 | cs = cs->parent; | |
565 | if (cs) | |
566 | nodes_and(*pmask, cs->mems_allowed, node_online_map); | |
567 | else | |
568 | *pmask = node_online_map; | |
569 | BUG_ON(!nodes_intersects(*pmask, node_online_map)); | |
570 | } | |
571 | ||
572 | /* | |
053199ed PJ |
573 | * Refresh current tasks mems_allowed and mems_generation from current |
574 | * tasks cpuset. | |
575 | * | |
576 | * Call without callback_sem or task_lock() held. May be called with | |
577 | * or without manage_sem held. Will acquire task_lock() and might | |
578 | * acquire callback_sem during call. | |
579 | * | |
580 | * The task_lock() is required to dereference current->cpuset safely. | |
581 | * Without it, we could pick up the pointer value of current->cpuset | |
582 | * in one instruction, and then attach_task could give us a different | |
583 | * cpuset, and then the cpuset we had could be removed and freed, | |
584 | * and then on our next instruction, we could dereference a no longer | |
585 | * valid cpuset pointer to get its mems_generation field. | |
586 | * | |
587 | * This routine is needed to update the per-task mems_allowed data, | |
588 | * within the tasks context, when it is trying to allocate memory | |
589 | * (in various mm/mempolicy.c routines) and notices that some other | |
590 | * task has been modifying its cpuset. | |
1da177e4 LT |
591 | */ |
592 | ||
593 | static void refresh_mems(void) | |
594 | { | |
053199ed PJ |
595 | int my_cpusets_mem_gen; |
596 | ||
597 | task_lock(current); | |
598 | my_cpusets_mem_gen = current->cpuset->mems_generation; | |
599 | task_unlock(current); | |
1da177e4 | 600 | |
053199ed PJ |
601 | if (current->cpuset_mems_generation != my_cpusets_mem_gen) { |
602 | struct cpuset *cs; | |
603 | ||
604 | down(&callback_sem); | |
605 | task_lock(current); | |
606 | cs = current->cpuset; | |
1da177e4 LT |
607 | guarantee_online_mems(cs, ¤t->mems_allowed); |
608 | current->cpuset_mems_generation = cs->mems_generation; | |
053199ed PJ |
609 | task_unlock(current); |
610 | up(&callback_sem); | |
1da177e4 LT |
611 | } |
612 | } | |
613 | ||
614 | /* | |
615 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? | |
616 | * | |
617 | * One cpuset is a subset of another if all its allowed CPUs and | |
618 | * Memory Nodes are a subset of the other, and its exclusive flags | |
053199ed | 619 | * are only set if the other's are set. Call holding manage_sem. |
1da177e4 LT |
620 | */ |
621 | ||
622 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) | |
623 | { | |
624 | return cpus_subset(p->cpus_allowed, q->cpus_allowed) && | |
625 | nodes_subset(p->mems_allowed, q->mems_allowed) && | |
626 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && | |
627 | is_mem_exclusive(p) <= is_mem_exclusive(q); | |
628 | } | |
629 | ||
630 | /* | |
631 | * validate_change() - Used to validate that any proposed cpuset change | |
632 | * follows the structural rules for cpusets. | |
633 | * | |
634 | * If we replaced the flag and mask values of the current cpuset | |
635 | * (cur) with those values in the trial cpuset (trial), would | |
636 | * our various subset and exclusive rules still be valid? Presumes | |
053199ed | 637 | * manage_sem held. |
1da177e4 LT |
638 | * |
639 | * 'cur' is the address of an actual, in-use cpuset. Operations | |
640 | * such as list traversal that depend on the actual address of the | |
641 | * cpuset in the list must use cur below, not trial. | |
642 | * | |
643 | * 'trial' is the address of bulk structure copy of cur, with | |
644 | * perhaps one or more of the fields cpus_allowed, mems_allowed, | |
645 | * or flags changed to new, trial values. | |
646 | * | |
647 | * Return 0 if valid, -errno if not. | |
648 | */ | |
649 | ||
650 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) | |
651 | { | |
652 | struct cpuset *c, *par; | |
653 | ||
654 | /* Each of our child cpusets must be a subset of us */ | |
655 | list_for_each_entry(c, &cur->children, sibling) { | |
656 | if (!is_cpuset_subset(c, trial)) | |
657 | return -EBUSY; | |
658 | } | |
659 | ||
660 | /* Remaining checks don't apply to root cpuset */ | |
661 | if ((par = cur->parent) == NULL) | |
662 | return 0; | |
663 | ||
664 | /* We must be a subset of our parent cpuset */ | |
665 | if (!is_cpuset_subset(trial, par)) | |
666 | return -EACCES; | |
667 | ||
668 | /* If either I or some sibling (!= me) is exclusive, we can't overlap */ | |
669 | list_for_each_entry(c, &par->children, sibling) { | |
670 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && | |
671 | c != cur && | |
672 | cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) | |
673 | return -EINVAL; | |
674 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && | |
675 | c != cur && | |
676 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) | |
677 | return -EINVAL; | |
678 | } | |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
85d7b949 DG |
683 | /* |
684 | * For a given cpuset cur, partition the system as follows | |
685 | * a. All cpus in the parent cpuset's cpus_allowed that are not part of any | |
686 | * exclusive child cpusets | |
687 | * b. All cpus in the current cpuset's cpus_allowed that are not part of any | |
688 | * exclusive child cpusets | |
689 | * Build these two partitions by calling partition_sched_domains | |
690 | * | |
053199ed | 691 | * Call with manage_sem held. May nest a call to the |
85d7b949 DG |
692 | * lock_cpu_hotplug()/unlock_cpu_hotplug() pair. |
693 | */ | |
212d6d22 | 694 | |
85d7b949 DG |
695 | static void update_cpu_domains(struct cpuset *cur) |
696 | { | |
697 | struct cpuset *c, *par = cur->parent; | |
698 | cpumask_t pspan, cspan; | |
699 | ||
700 | if (par == NULL || cpus_empty(cur->cpus_allowed)) | |
701 | return; | |
702 | ||
703 | /* | |
704 | * Get all cpus from parent's cpus_allowed not part of exclusive | |
705 | * children | |
706 | */ | |
707 | pspan = par->cpus_allowed; | |
708 | list_for_each_entry(c, &par->children, sibling) { | |
709 | if (is_cpu_exclusive(c)) | |
710 | cpus_andnot(pspan, pspan, c->cpus_allowed); | |
711 | } | |
712 | if (is_removed(cur) || !is_cpu_exclusive(cur)) { | |
713 | cpus_or(pspan, pspan, cur->cpus_allowed); | |
714 | if (cpus_equal(pspan, cur->cpus_allowed)) | |
715 | return; | |
716 | cspan = CPU_MASK_NONE; | |
717 | } else { | |
718 | if (cpus_empty(pspan)) | |
719 | return; | |
720 | cspan = cur->cpus_allowed; | |
721 | /* | |
722 | * Get all cpus from current cpuset's cpus_allowed not part | |
723 | * of exclusive children | |
724 | */ | |
725 | list_for_each_entry(c, &cur->children, sibling) { | |
726 | if (is_cpu_exclusive(c)) | |
727 | cpus_andnot(cspan, cspan, c->cpus_allowed); | |
728 | } | |
729 | } | |
730 | ||
731 | lock_cpu_hotplug(); | |
732 | partition_sched_domains(&pspan, &cspan); | |
733 | unlock_cpu_hotplug(); | |
734 | } | |
735 | ||
053199ed PJ |
736 | /* |
737 | * Call with manage_sem held. May take callback_sem during call. | |
738 | */ | |
739 | ||
1da177e4 LT |
740 | static int update_cpumask(struct cpuset *cs, char *buf) |
741 | { | |
742 | struct cpuset trialcs; | |
85d7b949 | 743 | int retval, cpus_unchanged; |
1da177e4 LT |
744 | |
745 | trialcs = *cs; | |
746 | retval = cpulist_parse(buf, trialcs.cpus_allowed); | |
747 | if (retval < 0) | |
748 | return retval; | |
749 | cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); | |
750 | if (cpus_empty(trialcs.cpus_allowed)) | |
751 | return -ENOSPC; | |
752 | retval = validate_change(cs, &trialcs); | |
85d7b949 DG |
753 | if (retval < 0) |
754 | return retval; | |
755 | cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); | |
053199ed | 756 | down(&callback_sem); |
85d7b949 | 757 | cs->cpus_allowed = trialcs.cpus_allowed; |
053199ed | 758 | up(&callback_sem); |
85d7b949 DG |
759 | if (is_cpu_exclusive(cs) && !cpus_unchanged) |
760 | update_cpu_domains(cs); | |
761 | return 0; | |
1da177e4 LT |
762 | } |
763 | ||
053199ed PJ |
764 | /* |
765 | * Call with manage_sem held. May take callback_sem during call. | |
766 | */ | |
767 | ||
1da177e4 LT |
768 | static int update_nodemask(struct cpuset *cs, char *buf) |
769 | { | |
770 | struct cpuset trialcs; | |
771 | int retval; | |
772 | ||
773 | trialcs = *cs; | |
774 | retval = nodelist_parse(buf, trialcs.mems_allowed); | |
775 | if (retval < 0) | |
776 | return retval; | |
777 | nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map); | |
778 | if (nodes_empty(trialcs.mems_allowed)) | |
779 | return -ENOSPC; | |
780 | retval = validate_change(cs, &trialcs); | |
781 | if (retval == 0) { | |
053199ed | 782 | down(&callback_sem); |
1da177e4 LT |
783 | cs->mems_allowed = trialcs.mems_allowed; |
784 | atomic_inc(&cpuset_mems_generation); | |
785 | cs->mems_generation = atomic_read(&cpuset_mems_generation); | |
053199ed | 786 | up(&callback_sem); |
1da177e4 LT |
787 | } |
788 | return retval; | |
789 | } | |
790 | ||
791 | /* | |
792 | * update_flag - read a 0 or a 1 in a file and update associated flag | |
793 | * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, | |
794 | * CS_NOTIFY_ON_RELEASE) | |
795 | * cs: the cpuset to update | |
796 | * buf: the buffer where we read the 0 or 1 | |
053199ed PJ |
797 | * |
798 | * Call with manage_sem held. | |
1da177e4 LT |
799 | */ |
800 | ||
801 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf) | |
802 | { | |
803 | int turning_on; | |
804 | struct cpuset trialcs; | |
85d7b949 | 805 | int err, cpu_exclusive_changed; |
1da177e4 LT |
806 | |
807 | turning_on = (simple_strtoul(buf, NULL, 10) != 0); | |
808 | ||
809 | trialcs = *cs; | |
810 | if (turning_on) | |
811 | set_bit(bit, &trialcs.flags); | |
812 | else | |
813 | clear_bit(bit, &trialcs.flags); | |
814 | ||
815 | err = validate_change(cs, &trialcs); | |
85d7b949 DG |
816 | if (err < 0) |
817 | return err; | |
818 | cpu_exclusive_changed = | |
819 | (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs)); | |
053199ed | 820 | down(&callback_sem); |
85d7b949 DG |
821 | if (turning_on) |
822 | set_bit(bit, &cs->flags); | |
823 | else | |
824 | clear_bit(bit, &cs->flags); | |
053199ed | 825 | up(&callback_sem); |
85d7b949 DG |
826 | |
827 | if (cpu_exclusive_changed) | |
828 | update_cpu_domains(cs); | |
829 | return 0; | |
1da177e4 LT |
830 | } |
831 | ||
053199ed PJ |
832 | /* |
833 | * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly | |
834 | * writing the path of the old cpuset in 'ppathbuf' if it needs to be | |
835 | * notified on release. | |
836 | * | |
837 | * Call holding manage_sem. May take callback_sem and task_lock of | |
838 | * the task 'pid' during call. | |
839 | */ | |
840 | ||
3077a260 | 841 | static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf) |
1da177e4 LT |
842 | { |
843 | pid_t pid; | |
844 | struct task_struct *tsk; | |
845 | struct cpuset *oldcs; | |
846 | cpumask_t cpus; | |
847 | ||
3077a260 | 848 | if (sscanf(pidbuf, "%d", &pid) != 1) |
1da177e4 LT |
849 | return -EIO; |
850 | if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) | |
851 | return -ENOSPC; | |
852 | ||
853 | if (pid) { | |
854 | read_lock(&tasklist_lock); | |
855 | ||
856 | tsk = find_task_by_pid(pid); | |
053199ed | 857 | if (!tsk || tsk->flags & PF_EXITING) { |
1da177e4 LT |
858 | read_unlock(&tasklist_lock); |
859 | return -ESRCH; | |
860 | } | |
861 | ||
862 | get_task_struct(tsk); | |
863 | read_unlock(&tasklist_lock); | |
864 | ||
865 | if ((current->euid) && (current->euid != tsk->uid) | |
866 | && (current->euid != tsk->suid)) { | |
867 | put_task_struct(tsk); | |
868 | return -EACCES; | |
869 | } | |
870 | } else { | |
871 | tsk = current; | |
872 | get_task_struct(tsk); | |
873 | } | |
874 | ||
053199ed PJ |
875 | down(&callback_sem); |
876 | ||
1da177e4 LT |
877 | task_lock(tsk); |
878 | oldcs = tsk->cpuset; | |
879 | if (!oldcs) { | |
880 | task_unlock(tsk); | |
053199ed | 881 | up(&callback_sem); |
1da177e4 LT |
882 | put_task_struct(tsk); |
883 | return -ESRCH; | |
884 | } | |
885 | atomic_inc(&cs->count); | |
886 | tsk->cpuset = cs; | |
887 | task_unlock(tsk); | |
888 | ||
889 | guarantee_online_cpus(cs, &cpus); | |
890 | set_cpus_allowed(tsk, cpus); | |
891 | ||
053199ed | 892 | up(&callback_sem); |
1da177e4 LT |
893 | put_task_struct(tsk); |
894 | if (atomic_dec_and_test(&oldcs->count)) | |
3077a260 | 895 | check_for_release(oldcs, ppathbuf); |
1da177e4 LT |
896 | return 0; |
897 | } | |
898 | ||
899 | /* The various types of files and directories in a cpuset file system */ | |
900 | ||
901 | typedef enum { | |
902 | FILE_ROOT, | |
903 | FILE_DIR, | |
904 | FILE_CPULIST, | |
905 | FILE_MEMLIST, | |
906 | FILE_CPU_EXCLUSIVE, | |
907 | FILE_MEM_EXCLUSIVE, | |
908 | FILE_NOTIFY_ON_RELEASE, | |
909 | FILE_TASKLIST, | |
910 | } cpuset_filetype_t; | |
911 | ||
912 | static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf, | |
913 | size_t nbytes, loff_t *unused_ppos) | |
914 | { | |
915 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); | |
916 | struct cftype *cft = __d_cft(file->f_dentry); | |
917 | cpuset_filetype_t type = cft->private; | |
918 | char *buffer; | |
3077a260 | 919 | char *pathbuf = NULL; |
1da177e4 LT |
920 | int retval = 0; |
921 | ||
922 | /* Crude upper limit on largest legitimate cpulist user might write. */ | |
923 | if (nbytes > 100 + 6 * NR_CPUS) | |
924 | return -E2BIG; | |
925 | ||
926 | /* +1 for nul-terminator */ | |
927 | if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0) | |
928 | return -ENOMEM; | |
929 | ||
930 | if (copy_from_user(buffer, userbuf, nbytes)) { | |
931 | retval = -EFAULT; | |
932 | goto out1; | |
933 | } | |
934 | buffer[nbytes] = 0; /* nul-terminate */ | |
935 | ||
053199ed | 936 | down(&manage_sem); |
1da177e4 LT |
937 | |
938 | if (is_removed(cs)) { | |
939 | retval = -ENODEV; | |
940 | goto out2; | |
941 | } | |
942 | ||
943 | switch (type) { | |
944 | case FILE_CPULIST: | |
945 | retval = update_cpumask(cs, buffer); | |
946 | break; | |
947 | case FILE_MEMLIST: | |
948 | retval = update_nodemask(cs, buffer); | |
949 | break; | |
950 | case FILE_CPU_EXCLUSIVE: | |
951 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer); | |
952 | break; | |
953 | case FILE_MEM_EXCLUSIVE: | |
954 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); | |
955 | break; | |
956 | case FILE_NOTIFY_ON_RELEASE: | |
957 | retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer); | |
958 | break; | |
959 | case FILE_TASKLIST: | |
3077a260 | 960 | retval = attach_task(cs, buffer, &pathbuf); |
1da177e4 LT |
961 | break; |
962 | default: | |
963 | retval = -EINVAL; | |
964 | goto out2; | |
965 | } | |
966 | ||
967 | if (retval == 0) | |
968 | retval = nbytes; | |
969 | out2: | |
053199ed | 970 | up(&manage_sem); |
3077a260 | 971 | cpuset_release_agent(pathbuf); |
1da177e4 LT |
972 | out1: |
973 | kfree(buffer); | |
974 | return retval; | |
975 | } | |
976 | ||
977 | static ssize_t cpuset_file_write(struct file *file, const char __user *buf, | |
978 | size_t nbytes, loff_t *ppos) | |
979 | { | |
980 | ssize_t retval = 0; | |
981 | struct cftype *cft = __d_cft(file->f_dentry); | |
982 | if (!cft) | |
983 | return -ENODEV; | |
984 | ||
985 | /* special function ? */ | |
986 | if (cft->write) | |
987 | retval = cft->write(file, buf, nbytes, ppos); | |
988 | else | |
989 | retval = cpuset_common_file_write(file, buf, nbytes, ppos); | |
990 | ||
991 | return retval; | |
992 | } | |
993 | ||
994 | /* | |
995 | * These ascii lists should be read in a single call, by using a user | |
996 | * buffer large enough to hold the entire map. If read in smaller | |
997 | * chunks, there is no guarantee of atomicity. Since the display format | |
998 | * used, list of ranges of sequential numbers, is variable length, | |
999 | * and since these maps can change value dynamically, one could read | |
1000 | * gibberish by doing partial reads while a list was changing. | |
1001 | * A single large read to a buffer that crosses a page boundary is | |
1002 | * ok, because the result being copied to user land is not recomputed | |
1003 | * across a page fault. | |
1004 | */ | |
1005 | ||
1006 | static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs) | |
1007 | { | |
1008 | cpumask_t mask; | |
1009 | ||
053199ed | 1010 | down(&callback_sem); |
1da177e4 | 1011 | mask = cs->cpus_allowed; |
053199ed | 1012 | up(&callback_sem); |
1da177e4 LT |
1013 | |
1014 | return cpulist_scnprintf(page, PAGE_SIZE, mask); | |
1015 | } | |
1016 | ||
1017 | static int cpuset_sprintf_memlist(char *page, struct cpuset *cs) | |
1018 | { | |
1019 | nodemask_t mask; | |
1020 | ||
053199ed | 1021 | down(&callback_sem); |
1da177e4 | 1022 | mask = cs->mems_allowed; |
053199ed | 1023 | up(&callback_sem); |
1da177e4 LT |
1024 | |
1025 | return nodelist_scnprintf(page, PAGE_SIZE, mask); | |
1026 | } | |
1027 | ||
1028 | static ssize_t cpuset_common_file_read(struct file *file, char __user *buf, | |
1029 | size_t nbytes, loff_t *ppos) | |
1030 | { | |
1031 | struct cftype *cft = __d_cft(file->f_dentry); | |
1032 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); | |
1033 | cpuset_filetype_t type = cft->private; | |
1034 | char *page; | |
1035 | ssize_t retval = 0; | |
1036 | char *s; | |
1da177e4 LT |
1037 | |
1038 | if (!(page = (char *)__get_free_page(GFP_KERNEL))) | |
1039 | return -ENOMEM; | |
1040 | ||
1041 | s = page; | |
1042 | ||
1043 | switch (type) { | |
1044 | case FILE_CPULIST: | |
1045 | s += cpuset_sprintf_cpulist(s, cs); | |
1046 | break; | |
1047 | case FILE_MEMLIST: | |
1048 | s += cpuset_sprintf_memlist(s, cs); | |
1049 | break; | |
1050 | case FILE_CPU_EXCLUSIVE: | |
1051 | *s++ = is_cpu_exclusive(cs) ? '1' : '0'; | |
1052 | break; | |
1053 | case FILE_MEM_EXCLUSIVE: | |
1054 | *s++ = is_mem_exclusive(cs) ? '1' : '0'; | |
1055 | break; | |
1056 | case FILE_NOTIFY_ON_RELEASE: | |
1057 | *s++ = notify_on_release(cs) ? '1' : '0'; | |
1058 | break; | |
1059 | default: | |
1060 | retval = -EINVAL; | |
1061 | goto out; | |
1062 | } | |
1063 | *s++ = '\n'; | |
1da177e4 | 1064 | |
eacaa1f5 | 1065 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
1da177e4 LT |
1066 | out: |
1067 | free_page((unsigned long)page); | |
1068 | return retval; | |
1069 | } | |
1070 | ||
1071 | static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes, | |
1072 | loff_t *ppos) | |
1073 | { | |
1074 | ssize_t retval = 0; | |
1075 | struct cftype *cft = __d_cft(file->f_dentry); | |
1076 | if (!cft) | |
1077 | return -ENODEV; | |
1078 | ||
1079 | /* special function ? */ | |
1080 | if (cft->read) | |
1081 | retval = cft->read(file, buf, nbytes, ppos); | |
1082 | else | |
1083 | retval = cpuset_common_file_read(file, buf, nbytes, ppos); | |
1084 | ||
1085 | return retval; | |
1086 | } | |
1087 | ||
1088 | static int cpuset_file_open(struct inode *inode, struct file *file) | |
1089 | { | |
1090 | int err; | |
1091 | struct cftype *cft; | |
1092 | ||
1093 | err = generic_file_open(inode, file); | |
1094 | if (err) | |
1095 | return err; | |
1096 | ||
1097 | cft = __d_cft(file->f_dentry); | |
1098 | if (!cft) | |
1099 | return -ENODEV; | |
1100 | if (cft->open) | |
1101 | err = cft->open(inode, file); | |
1102 | else | |
1103 | err = 0; | |
1104 | ||
1105 | return err; | |
1106 | } | |
1107 | ||
1108 | static int cpuset_file_release(struct inode *inode, struct file *file) | |
1109 | { | |
1110 | struct cftype *cft = __d_cft(file->f_dentry); | |
1111 | if (cft->release) | |
1112 | return cft->release(inode, file); | |
1113 | return 0; | |
1114 | } | |
1115 | ||
18a19cb3 PJ |
1116 | /* |
1117 | * cpuset_rename - Only allow simple rename of directories in place. | |
1118 | */ | |
1119 | static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry, | |
1120 | struct inode *new_dir, struct dentry *new_dentry) | |
1121 | { | |
1122 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) | |
1123 | return -ENOTDIR; | |
1124 | if (new_dentry->d_inode) | |
1125 | return -EEXIST; | |
1126 | if (old_dir != new_dir) | |
1127 | return -EIO; | |
1128 | return simple_rename(old_dir, old_dentry, new_dir, new_dentry); | |
1129 | } | |
1130 | ||
1da177e4 LT |
1131 | static struct file_operations cpuset_file_operations = { |
1132 | .read = cpuset_file_read, | |
1133 | .write = cpuset_file_write, | |
1134 | .llseek = generic_file_llseek, | |
1135 | .open = cpuset_file_open, | |
1136 | .release = cpuset_file_release, | |
1137 | }; | |
1138 | ||
1139 | static struct inode_operations cpuset_dir_inode_operations = { | |
1140 | .lookup = simple_lookup, | |
1141 | .mkdir = cpuset_mkdir, | |
1142 | .rmdir = cpuset_rmdir, | |
18a19cb3 | 1143 | .rename = cpuset_rename, |
1da177e4 LT |
1144 | }; |
1145 | ||
1146 | static int cpuset_create_file(struct dentry *dentry, int mode) | |
1147 | { | |
1148 | struct inode *inode; | |
1149 | ||
1150 | if (!dentry) | |
1151 | return -ENOENT; | |
1152 | if (dentry->d_inode) | |
1153 | return -EEXIST; | |
1154 | ||
1155 | inode = cpuset_new_inode(mode); | |
1156 | if (!inode) | |
1157 | return -ENOMEM; | |
1158 | ||
1159 | if (S_ISDIR(mode)) { | |
1160 | inode->i_op = &cpuset_dir_inode_operations; | |
1161 | inode->i_fop = &simple_dir_operations; | |
1162 | ||
1163 | /* start off with i_nlink == 2 (for "." entry) */ | |
1164 | inode->i_nlink++; | |
1165 | } else if (S_ISREG(mode)) { | |
1166 | inode->i_size = 0; | |
1167 | inode->i_fop = &cpuset_file_operations; | |
1168 | } | |
1169 | ||
1170 | d_instantiate(dentry, inode); | |
1171 | dget(dentry); /* Extra count - pin the dentry in core */ | |
1172 | return 0; | |
1173 | } | |
1174 | ||
1175 | /* | |
1176 | * cpuset_create_dir - create a directory for an object. | |
1177 | * cs: the cpuset we create the directory for. | |
1178 | * It must have a valid ->parent field | |
1179 | * And we are going to fill its ->dentry field. | |
1180 | * name: The name to give to the cpuset directory. Will be copied. | |
1181 | * mode: mode to set on new directory. | |
1182 | */ | |
1183 | ||
1184 | static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode) | |
1185 | { | |
1186 | struct dentry *dentry = NULL; | |
1187 | struct dentry *parent; | |
1188 | int error = 0; | |
1189 | ||
1190 | parent = cs->parent->dentry; | |
1191 | dentry = cpuset_get_dentry(parent, name); | |
1192 | if (IS_ERR(dentry)) | |
1193 | return PTR_ERR(dentry); | |
1194 | error = cpuset_create_file(dentry, S_IFDIR | mode); | |
1195 | if (!error) { | |
1196 | dentry->d_fsdata = cs; | |
1197 | parent->d_inode->i_nlink++; | |
1198 | cs->dentry = dentry; | |
1199 | } | |
1200 | dput(dentry); | |
1201 | ||
1202 | return error; | |
1203 | } | |
1204 | ||
1205 | static int cpuset_add_file(struct dentry *dir, const struct cftype *cft) | |
1206 | { | |
1207 | struct dentry *dentry; | |
1208 | int error; | |
1209 | ||
1210 | down(&dir->d_inode->i_sem); | |
1211 | dentry = cpuset_get_dentry(dir, cft->name); | |
1212 | if (!IS_ERR(dentry)) { | |
1213 | error = cpuset_create_file(dentry, 0644 | S_IFREG); | |
1214 | if (!error) | |
1215 | dentry->d_fsdata = (void *)cft; | |
1216 | dput(dentry); | |
1217 | } else | |
1218 | error = PTR_ERR(dentry); | |
1219 | up(&dir->d_inode->i_sem); | |
1220 | return error; | |
1221 | } | |
1222 | ||
1223 | /* | |
1224 | * Stuff for reading the 'tasks' file. | |
1225 | * | |
1226 | * Reading this file can return large amounts of data if a cpuset has | |
1227 | * *lots* of attached tasks. So it may need several calls to read(), | |
1228 | * but we cannot guarantee that the information we produce is correct | |
1229 | * unless we produce it entirely atomically. | |
1230 | * | |
1231 | * Upon tasks file open(), a struct ctr_struct is allocated, that | |
1232 | * will have a pointer to an array (also allocated here). The struct | |
1233 | * ctr_struct * is stored in file->private_data. Its resources will | |
1234 | * be freed by release() when the file is closed. The array is used | |
1235 | * to sprintf the PIDs and then used by read(). | |
1236 | */ | |
1237 | ||
1238 | /* cpusets_tasks_read array */ | |
1239 | ||
1240 | struct ctr_struct { | |
1241 | char *buf; | |
1242 | int bufsz; | |
1243 | }; | |
1244 | ||
1245 | /* | |
1246 | * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'. | |
053199ed PJ |
1247 | * Return actual number of pids loaded. No need to task_lock(p) |
1248 | * when reading out p->cpuset, as we don't really care if it changes | |
1249 | * on the next cycle, and we are not going to try to dereference it. | |
1da177e4 LT |
1250 | */ |
1251 | static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs) | |
1252 | { | |
1253 | int n = 0; | |
1254 | struct task_struct *g, *p; | |
1255 | ||
1256 | read_lock(&tasklist_lock); | |
1257 | ||
1258 | do_each_thread(g, p) { | |
1259 | if (p->cpuset == cs) { | |
1260 | pidarray[n++] = p->pid; | |
1261 | if (unlikely(n == npids)) | |
1262 | goto array_full; | |
1263 | } | |
1264 | } while_each_thread(g, p); | |
1265 | ||
1266 | array_full: | |
1267 | read_unlock(&tasklist_lock); | |
1268 | return n; | |
1269 | } | |
1270 | ||
1271 | static int cmppid(const void *a, const void *b) | |
1272 | { | |
1273 | return *(pid_t *)a - *(pid_t *)b; | |
1274 | } | |
1275 | ||
1276 | /* | |
1277 | * Convert array 'a' of 'npids' pid_t's to a string of newline separated | |
1278 | * decimal pids in 'buf'. Don't write more than 'sz' chars, but return | |
1279 | * count 'cnt' of how many chars would be written if buf were large enough. | |
1280 | */ | |
1281 | static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids) | |
1282 | { | |
1283 | int cnt = 0; | |
1284 | int i; | |
1285 | ||
1286 | for (i = 0; i < npids; i++) | |
1287 | cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]); | |
1288 | return cnt; | |
1289 | } | |
1290 | ||
053199ed PJ |
1291 | /* |
1292 | * Handle an open on 'tasks' file. Prepare a buffer listing the | |
1293 | * process id's of tasks currently attached to the cpuset being opened. | |
1294 | * | |
1295 | * Does not require any specific cpuset semaphores, and does not take any. | |
1296 | */ | |
1da177e4 LT |
1297 | static int cpuset_tasks_open(struct inode *unused, struct file *file) |
1298 | { | |
1299 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); | |
1300 | struct ctr_struct *ctr; | |
1301 | pid_t *pidarray; | |
1302 | int npids; | |
1303 | char c; | |
1304 | ||
1305 | if (!(file->f_mode & FMODE_READ)) | |
1306 | return 0; | |
1307 | ||
1308 | ctr = kmalloc(sizeof(*ctr), GFP_KERNEL); | |
1309 | if (!ctr) | |
1310 | goto err0; | |
1311 | ||
1312 | /* | |
1313 | * If cpuset gets more users after we read count, we won't have | |
1314 | * enough space - tough. This race is indistinguishable to the | |
1315 | * caller from the case that the additional cpuset users didn't | |
1316 | * show up until sometime later on. | |
1317 | */ | |
1318 | npids = atomic_read(&cs->count); | |
1319 | pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL); | |
1320 | if (!pidarray) | |
1321 | goto err1; | |
1322 | ||
1323 | npids = pid_array_load(pidarray, npids, cs); | |
1324 | sort(pidarray, npids, sizeof(pid_t), cmppid, NULL); | |
1325 | ||
1326 | /* Call pid_array_to_buf() twice, first just to get bufsz */ | |
1327 | ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1; | |
1328 | ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL); | |
1329 | if (!ctr->buf) | |
1330 | goto err2; | |
1331 | ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids); | |
1332 | ||
1333 | kfree(pidarray); | |
1334 | file->private_data = ctr; | |
1335 | return 0; | |
1336 | ||
1337 | err2: | |
1338 | kfree(pidarray); | |
1339 | err1: | |
1340 | kfree(ctr); | |
1341 | err0: | |
1342 | return -ENOMEM; | |
1343 | } | |
1344 | ||
1345 | static ssize_t cpuset_tasks_read(struct file *file, char __user *buf, | |
1346 | size_t nbytes, loff_t *ppos) | |
1347 | { | |
1348 | struct ctr_struct *ctr = file->private_data; | |
1349 | ||
1350 | if (*ppos + nbytes > ctr->bufsz) | |
1351 | nbytes = ctr->bufsz - *ppos; | |
1352 | if (copy_to_user(buf, ctr->buf + *ppos, nbytes)) | |
1353 | return -EFAULT; | |
1354 | *ppos += nbytes; | |
1355 | return nbytes; | |
1356 | } | |
1357 | ||
1358 | static int cpuset_tasks_release(struct inode *unused_inode, struct file *file) | |
1359 | { | |
1360 | struct ctr_struct *ctr; | |
1361 | ||
1362 | if (file->f_mode & FMODE_READ) { | |
1363 | ctr = file->private_data; | |
1364 | kfree(ctr->buf); | |
1365 | kfree(ctr); | |
1366 | } | |
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | /* | |
1371 | * for the common functions, 'private' gives the type of file | |
1372 | */ | |
1373 | ||
1374 | static struct cftype cft_tasks = { | |
1375 | .name = "tasks", | |
1376 | .open = cpuset_tasks_open, | |
1377 | .read = cpuset_tasks_read, | |
1378 | .release = cpuset_tasks_release, | |
1379 | .private = FILE_TASKLIST, | |
1380 | }; | |
1381 | ||
1382 | static struct cftype cft_cpus = { | |
1383 | .name = "cpus", | |
1384 | .private = FILE_CPULIST, | |
1385 | }; | |
1386 | ||
1387 | static struct cftype cft_mems = { | |
1388 | .name = "mems", | |
1389 | .private = FILE_MEMLIST, | |
1390 | }; | |
1391 | ||
1392 | static struct cftype cft_cpu_exclusive = { | |
1393 | .name = "cpu_exclusive", | |
1394 | .private = FILE_CPU_EXCLUSIVE, | |
1395 | }; | |
1396 | ||
1397 | static struct cftype cft_mem_exclusive = { | |
1398 | .name = "mem_exclusive", | |
1399 | .private = FILE_MEM_EXCLUSIVE, | |
1400 | }; | |
1401 | ||
1402 | static struct cftype cft_notify_on_release = { | |
1403 | .name = "notify_on_release", | |
1404 | .private = FILE_NOTIFY_ON_RELEASE, | |
1405 | }; | |
1406 | ||
1407 | static int cpuset_populate_dir(struct dentry *cs_dentry) | |
1408 | { | |
1409 | int err; | |
1410 | ||
1411 | if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0) | |
1412 | return err; | |
1413 | if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0) | |
1414 | return err; | |
1415 | if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0) | |
1416 | return err; | |
1417 | if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0) | |
1418 | return err; | |
1419 | if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0) | |
1420 | return err; | |
1421 | if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0) | |
1422 | return err; | |
1423 | return 0; | |
1424 | } | |
1425 | ||
1426 | /* | |
1427 | * cpuset_create - create a cpuset | |
1428 | * parent: cpuset that will be parent of the new cpuset. | |
1429 | * name: name of the new cpuset. Will be strcpy'ed. | |
1430 | * mode: mode to set on new inode | |
1431 | * | |
1432 | * Must be called with the semaphore on the parent inode held | |
1433 | */ | |
1434 | ||
1435 | static long cpuset_create(struct cpuset *parent, const char *name, int mode) | |
1436 | { | |
1437 | struct cpuset *cs; | |
1438 | int err; | |
1439 | ||
1440 | cs = kmalloc(sizeof(*cs), GFP_KERNEL); | |
1441 | if (!cs) | |
1442 | return -ENOMEM; | |
1443 | ||
053199ed | 1444 | down(&manage_sem); |
5aa15b5f | 1445 | refresh_mems(); |
1da177e4 LT |
1446 | cs->flags = 0; |
1447 | if (notify_on_release(parent)) | |
1448 | set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); | |
1449 | cs->cpus_allowed = CPU_MASK_NONE; | |
1450 | cs->mems_allowed = NODE_MASK_NONE; | |
1451 | atomic_set(&cs->count, 0); | |
1452 | INIT_LIST_HEAD(&cs->sibling); | |
1453 | INIT_LIST_HEAD(&cs->children); | |
1454 | atomic_inc(&cpuset_mems_generation); | |
1455 | cs->mems_generation = atomic_read(&cpuset_mems_generation); | |
1456 | ||
1457 | cs->parent = parent; | |
1458 | ||
053199ed | 1459 | down(&callback_sem); |
1da177e4 | 1460 | list_add(&cs->sibling, &cs->parent->children); |
053199ed | 1461 | up(&callback_sem); |
1da177e4 LT |
1462 | |
1463 | err = cpuset_create_dir(cs, name, mode); | |
1464 | if (err < 0) | |
1465 | goto err; | |
1466 | ||
1467 | /* | |
053199ed | 1468 | * Release manage_sem before cpuset_populate_dir() because it |
1da177e4 LT |
1469 | * will down() this new directory's i_sem and if we race with |
1470 | * another mkdir, we might deadlock. | |
1471 | */ | |
053199ed | 1472 | up(&manage_sem); |
1da177e4 LT |
1473 | |
1474 | err = cpuset_populate_dir(cs->dentry); | |
1475 | /* If err < 0, we have a half-filled directory - oh well ;) */ | |
1476 | return 0; | |
1477 | err: | |
1478 | list_del(&cs->sibling); | |
053199ed | 1479 | up(&manage_sem); |
1da177e4 LT |
1480 | kfree(cs); |
1481 | return err; | |
1482 | } | |
1483 | ||
1484 | static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1485 | { | |
1486 | struct cpuset *c_parent = dentry->d_parent->d_fsdata; | |
1487 | ||
1488 | /* the vfs holds inode->i_sem already */ | |
1489 | return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR); | |
1490 | } | |
1491 | ||
1492 | static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry) | |
1493 | { | |
1494 | struct cpuset *cs = dentry->d_fsdata; | |
1495 | struct dentry *d; | |
1496 | struct cpuset *parent; | |
3077a260 | 1497 | char *pathbuf = NULL; |
1da177e4 LT |
1498 | |
1499 | /* the vfs holds both inode->i_sem already */ | |
1500 | ||
053199ed | 1501 | down(&manage_sem); |
5aa15b5f | 1502 | refresh_mems(); |
1da177e4 | 1503 | if (atomic_read(&cs->count) > 0) { |
053199ed | 1504 | up(&manage_sem); |
1da177e4 LT |
1505 | return -EBUSY; |
1506 | } | |
1507 | if (!list_empty(&cs->children)) { | |
053199ed | 1508 | up(&manage_sem); |
1da177e4 LT |
1509 | return -EBUSY; |
1510 | } | |
1da177e4 | 1511 | parent = cs->parent; |
053199ed | 1512 | down(&callback_sem); |
1da177e4 | 1513 | set_bit(CS_REMOVED, &cs->flags); |
85d7b949 DG |
1514 | if (is_cpu_exclusive(cs)) |
1515 | update_cpu_domains(cs); | |
1da177e4 | 1516 | list_del(&cs->sibling); /* delete my sibling from parent->children */ |
85d7b949 | 1517 | spin_lock(&cs->dentry->d_lock); |
1da177e4 LT |
1518 | d = dget(cs->dentry); |
1519 | cs->dentry = NULL; | |
1520 | spin_unlock(&d->d_lock); | |
1521 | cpuset_d_remove_dir(d); | |
1522 | dput(d); | |
053199ed PJ |
1523 | up(&callback_sem); |
1524 | if (list_empty(&parent->children)) | |
1525 | check_for_release(parent, &pathbuf); | |
1526 | up(&manage_sem); | |
3077a260 | 1527 | cpuset_release_agent(pathbuf); |
1da177e4 LT |
1528 | return 0; |
1529 | } | |
1530 | ||
1531 | /** | |
1532 | * cpuset_init - initialize cpusets at system boot | |
1533 | * | |
1534 | * Description: Initialize top_cpuset and the cpuset internal file system, | |
1535 | **/ | |
1536 | ||
1537 | int __init cpuset_init(void) | |
1538 | { | |
1539 | struct dentry *root; | |
1540 | int err; | |
1541 | ||
1542 | top_cpuset.cpus_allowed = CPU_MASK_ALL; | |
1543 | top_cpuset.mems_allowed = NODE_MASK_ALL; | |
1544 | ||
1545 | atomic_inc(&cpuset_mems_generation); | |
1546 | top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation); | |
1547 | ||
1548 | init_task.cpuset = &top_cpuset; | |
1549 | ||
1550 | err = register_filesystem(&cpuset_fs_type); | |
1551 | if (err < 0) | |
1552 | goto out; | |
1553 | cpuset_mount = kern_mount(&cpuset_fs_type); | |
1554 | if (IS_ERR(cpuset_mount)) { | |
1555 | printk(KERN_ERR "cpuset: could not mount!\n"); | |
1556 | err = PTR_ERR(cpuset_mount); | |
1557 | cpuset_mount = NULL; | |
1558 | goto out; | |
1559 | } | |
1560 | root = cpuset_mount->mnt_sb->s_root; | |
1561 | root->d_fsdata = &top_cpuset; | |
1562 | root->d_inode->i_nlink++; | |
1563 | top_cpuset.dentry = root; | |
1564 | root->d_inode->i_op = &cpuset_dir_inode_operations; | |
1565 | err = cpuset_populate_dir(root); | |
1566 | out: | |
1567 | return err; | |
1568 | } | |
1569 | ||
1570 | /** | |
1571 | * cpuset_init_smp - initialize cpus_allowed | |
1572 | * | |
1573 | * Description: Finish top cpuset after cpu, node maps are initialized | |
1574 | **/ | |
1575 | ||
1576 | void __init cpuset_init_smp(void) | |
1577 | { | |
1578 | top_cpuset.cpus_allowed = cpu_online_map; | |
1579 | top_cpuset.mems_allowed = node_online_map; | |
1580 | } | |
1581 | ||
1582 | /** | |
1583 | * cpuset_fork - attach newly forked task to its parents cpuset. | |
d9fd8a6d | 1584 | * @tsk: pointer to task_struct of forking parent process. |
1da177e4 | 1585 | * |
053199ed PJ |
1586 | * Description: A task inherits its parent's cpuset at fork(). |
1587 | * | |
1588 | * A pointer to the shared cpuset was automatically copied in fork.c | |
1589 | * by dup_task_struct(). However, we ignore that copy, since it was | |
1590 | * not made under the protection of task_lock(), so might no longer be | |
1591 | * a valid cpuset pointer. attach_task() might have already changed | |
1592 | * current->cpuset, allowing the previously referenced cpuset to | |
1593 | * be removed and freed. Instead, we task_lock(current) and copy | |
1594 | * its present value of current->cpuset for our freshly forked child. | |
1595 | * | |
1596 | * At the point that cpuset_fork() is called, 'current' is the parent | |
1597 | * task, and the passed argument 'child' points to the child task. | |
1da177e4 LT |
1598 | **/ |
1599 | ||
053199ed | 1600 | void cpuset_fork(struct task_struct *child) |
1da177e4 | 1601 | { |
053199ed PJ |
1602 | task_lock(current); |
1603 | child->cpuset = current->cpuset; | |
1604 | atomic_inc(&child->cpuset->count); | |
1605 | task_unlock(current); | |
1da177e4 LT |
1606 | } |
1607 | ||
1608 | /** | |
1609 | * cpuset_exit - detach cpuset from exiting task | |
1610 | * @tsk: pointer to task_struct of exiting process | |
1611 | * | |
1612 | * Description: Detach cpuset from @tsk and release it. | |
1613 | * | |
053199ed PJ |
1614 | * Note that cpusets marked notify_on_release force every task in |
1615 | * them to take the global manage_sem semaphore when exiting. | |
1616 | * This could impact scaling on very large systems. Be reluctant to | |
1617 | * use notify_on_release cpusets where very high task exit scaling | |
1618 | * is required on large systems. | |
1619 | * | |
1620 | * Don't even think about derefencing 'cs' after the cpuset use count | |
1621 | * goes to zero, except inside a critical section guarded by manage_sem | |
1622 | * or callback_sem. Otherwise a zero cpuset use count is a license to | |
1623 | * any other task to nuke the cpuset immediately, via cpuset_rmdir(). | |
1624 | * | |
1625 | * This routine has to take manage_sem, not callback_sem, because | |
1626 | * it is holding that semaphore while calling check_for_release(), | |
1627 | * which calls kmalloc(), so can't be called holding callback__sem(). | |
1628 | * | |
1629 | * We don't need to task_lock() this reference to tsk->cpuset, | |
1630 | * because tsk is already marked PF_EXITING, so attach_task() won't | |
1631 | * mess with it. | |
1da177e4 LT |
1632 | **/ |
1633 | ||
1634 | void cpuset_exit(struct task_struct *tsk) | |
1635 | { | |
1636 | struct cpuset *cs; | |
1637 | ||
053199ed PJ |
1638 | BUG_ON(!(tsk->flags & PF_EXITING)); |
1639 | ||
1da177e4 LT |
1640 | cs = tsk->cpuset; |
1641 | tsk->cpuset = NULL; | |
1da177e4 | 1642 | |
2efe86b8 | 1643 | if (notify_on_release(cs)) { |
3077a260 PJ |
1644 | char *pathbuf = NULL; |
1645 | ||
053199ed | 1646 | down(&manage_sem); |
2efe86b8 | 1647 | if (atomic_dec_and_test(&cs->count)) |
3077a260 | 1648 | check_for_release(cs, &pathbuf); |
053199ed | 1649 | up(&manage_sem); |
3077a260 | 1650 | cpuset_release_agent(pathbuf); |
2efe86b8 PJ |
1651 | } else { |
1652 | atomic_dec(&cs->count); | |
1da177e4 LT |
1653 | } |
1654 | } | |
1655 | ||
1656 | /** | |
1657 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. | |
1658 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. | |
1659 | * | |
1660 | * Description: Returns the cpumask_t cpus_allowed of the cpuset | |
1661 | * attached to the specified @tsk. Guaranteed to return some non-empty | |
1662 | * subset of cpu_online_map, even if this means going outside the | |
1663 | * tasks cpuset. | |
1664 | **/ | |
1665 | ||
9a848896 | 1666 | cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk) |
1da177e4 LT |
1667 | { |
1668 | cpumask_t mask; | |
1669 | ||
053199ed | 1670 | down(&callback_sem); |
1da177e4 LT |
1671 | task_lock((struct task_struct *)tsk); |
1672 | guarantee_online_cpus(tsk->cpuset, &mask); | |
1673 | task_unlock((struct task_struct *)tsk); | |
053199ed | 1674 | up(&callback_sem); |
1da177e4 LT |
1675 | |
1676 | return mask; | |
1677 | } | |
1678 | ||
1679 | void cpuset_init_current_mems_allowed(void) | |
1680 | { | |
1681 | current->mems_allowed = NODE_MASK_ALL; | |
1682 | } | |
1683 | ||
d9fd8a6d RD |
1684 | /** |
1685 | * cpuset_update_current_mems_allowed - update mems parameters to new values | |
1686 | * | |
1da177e4 LT |
1687 | * If the current tasks cpusets mems_allowed changed behind our backs, |
1688 | * update current->mems_allowed and mems_generation to the new value. | |
1689 | * Do not call this routine if in_interrupt(). | |
053199ed PJ |
1690 | * |
1691 | * Call without callback_sem or task_lock() held. May be called | |
1692 | * with or without manage_sem held. Unless exiting, it will acquire | |
1693 | * task_lock(). Also might acquire callback_sem during call to | |
1694 | * refresh_mems(). | |
1da177e4 LT |
1695 | */ |
1696 | ||
1697 | void cpuset_update_current_mems_allowed(void) | |
1698 | { | |
053199ed PJ |
1699 | struct cpuset *cs; |
1700 | int need_to_refresh = 0; | |
1da177e4 | 1701 | |
053199ed PJ |
1702 | task_lock(current); |
1703 | cs = current->cpuset; | |
1da177e4 | 1704 | if (!cs) |
053199ed PJ |
1705 | goto done; |
1706 | if (current->cpuset_mems_generation != cs->mems_generation) | |
1707 | need_to_refresh = 1; | |
1708 | done: | |
1709 | task_unlock(current); | |
1710 | if (need_to_refresh) | |
1da177e4 | 1711 | refresh_mems(); |
1da177e4 LT |
1712 | } |
1713 | ||
d9fd8a6d RD |
1714 | /** |
1715 | * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed | |
1716 | * @nodes: pointer to a node bitmap that is and-ed with mems_allowed | |
1717 | */ | |
1da177e4 LT |
1718 | void cpuset_restrict_to_mems_allowed(unsigned long *nodes) |
1719 | { | |
1720 | bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed), | |
1721 | MAX_NUMNODES); | |
1722 | } | |
1723 | ||
d9fd8a6d RD |
1724 | /** |
1725 | * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed | |
1726 | * @zl: the zonelist to be checked | |
1727 | * | |
1da177e4 LT |
1728 | * Are any of the nodes on zonelist zl allowed in current->mems_allowed? |
1729 | */ | |
1730 | int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl) | |
1731 | { | |
1732 | int i; | |
1733 | ||
1734 | for (i = 0; zl->zones[i]; i++) { | |
1735 | int nid = zl->zones[i]->zone_pgdat->node_id; | |
1736 | ||
1737 | if (node_isset(nid, current->mems_allowed)) | |
1738 | return 1; | |
1739 | } | |
1740 | return 0; | |
1741 | } | |
1742 | ||
9bf2229f PJ |
1743 | /* |
1744 | * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive | |
053199ed | 1745 | * ancestor to the specified cpuset. Call holding callback_sem. |
9bf2229f PJ |
1746 | * If no ancestor is mem_exclusive (an unusual configuration), then |
1747 | * returns the root cpuset. | |
1748 | */ | |
1749 | static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs) | |
1750 | { | |
1751 | while (!is_mem_exclusive(cs) && cs->parent) | |
1752 | cs = cs->parent; | |
1753 | return cs; | |
1754 | } | |
1755 | ||
d9fd8a6d | 1756 | /** |
9bf2229f PJ |
1757 | * cpuset_zone_allowed - Can we allocate memory on zone z's memory node? |
1758 | * @z: is this zone on an allowed node? | |
1759 | * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL) | |
d9fd8a6d | 1760 | * |
9bf2229f PJ |
1761 | * If we're in interrupt, yes, we can always allocate. If zone |
1762 | * z's node is in our tasks mems_allowed, yes. If it's not a | |
1763 | * __GFP_HARDWALL request and this zone's nodes is in the nearest | |
1764 | * mem_exclusive cpuset ancestor to this tasks cpuset, yes. | |
1765 | * Otherwise, no. | |
1766 | * | |
1767 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, | |
1768 | * and do not allow allocations outside the current tasks cpuset. | |
1769 | * GFP_KERNEL allocations are not so marked, so can escape to the | |
1770 | * nearest mem_exclusive ancestor cpuset. | |
1771 | * | |
053199ed | 1772 | * Scanning up parent cpusets requires callback_sem. The __alloc_pages() |
9bf2229f PJ |
1773 | * routine only calls here with __GFP_HARDWALL bit _not_ set if |
1774 | * it's a GFP_KERNEL allocation, and all nodes in the current tasks | |
1775 | * mems_allowed came up empty on the first pass over the zonelist. | |
1776 | * So only GFP_KERNEL allocations, if all nodes in the cpuset are | |
053199ed | 1777 | * short of memory, might require taking the callback_sem semaphore. |
9bf2229f PJ |
1778 | * |
1779 | * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages() | |
1780 | * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing | |
1781 | * hardwall cpusets - no allocation on a node outside the cpuset is | |
1782 | * allowed (unless in interrupt, of course). | |
1783 | * | |
1784 | * The second loop doesn't even call here for GFP_ATOMIC requests | |
1785 | * (if the __alloc_pages() local variable 'wait' is set). That check | |
1786 | * and the checks below have the combined affect in the second loop of | |
1787 | * the __alloc_pages() routine that: | |
1788 | * in_interrupt - any node ok (current task context irrelevant) | |
1789 | * GFP_ATOMIC - any node ok | |
1790 | * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok | |
1791 | * GFP_USER - only nodes in current tasks mems allowed ok. | |
1792 | **/ | |
1793 | ||
dd0fc66f | 1794 | int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask) |
1da177e4 | 1795 | { |
9bf2229f PJ |
1796 | int node; /* node that zone z is on */ |
1797 | const struct cpuset *cs; /* current cpuset ancestors */ | |
1798 | int allowed = 1; /* is allocation in zone z allowed? */ | |
1799 | ||
1800 | if (in_interrupt()) | |
1801 | return 1; | |
1802 | node = z->zone_pgdat->node_id; | |
1803 | if (node_isset(node, current->mems_allowed)) | |
1804 | return 1; | |
1805 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ | |
1806 | return 0; | |
1807 | ||
1808 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ | |
053199ed PJ |
1809 | down(&callback_sem); |
1810 | ||
1811 | if (current->flags & PF_EXITING) /* Let dying task have memory */ | |
1812 | return 1; | |
1813 | task_lock(current); | |
1814 | cs = nearest_exclusive_ancestor(current->cpuset); | |
1815 | task_unlock(current); | |
1816 | ||
9bf2229f | 1817 | allowed = node_isset(node, cs->mems_allowed); |
053199ed | 1818 | up(&callback_sem); |
9bf2229f | 1819 | return allowed; |
1da177e4 LT |
1820 | } |
1821 | ||
ef08e3b4 PJ |
1822 | /** |
1823 | * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors? | |
1824 | * @p: pointer to task_struct of some other task. | |
1825 | * | |
1826 | * Description: Return true if the nearest mem_exclusive ancestor | |
1827 | * cpusets of tasks @p and current overlap. Used by oom killer to | |
1828 | * determine if task @p's memory usage might impact the memory | |
1829 | * available to the current task. | |
1830 | * | |
053199ed | 1831 | * Acquires callback_sem - not suitable for calling from a fast path. |
ef08e3b4 PJ |
1832 | **/ |
1833 | ||
1834 | int cpuset_excl_nodes_overlap(const struct task_struct *p) | |
1835 | { | |
1836 | const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */ | |
1837 | int overlap = 0; /* do cpusets overlap? */ | |
1838 | ||
053199ed PJ |
1839 | down(&callback_sem); |
1840 | ||
1841 | task_lock(current); | |
1842 | if (current->flags & PF_EXITING) { | |
1843 | task_unlock(current); | |
1844 | goto done; | |
1845 | } | |
1846 | cs1 = nearest_exclusive_ancestor(current->cpuset); | |
1847 | task_unlock(current); | |
1848 | ||
1849 | task_lock((struct task_struct *)p); | |
1850 | if (p->flags & PF_EXITING) { | |
1851 | task_unlock((struct task_struct *)p); | |
1852 | goto done; | |
1853 | } | |
1854 | cs2 = nearest_exclusive_ancestor(p->cpuset); | |
1855 | task_unlock((struct task_struct *)p); | |
1856 | ||
ef08e3b4 PJ |
1857 | overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed); |
1858 | done: | |
053199ed | 1859 | up(&callback_sem); |
ef08e3b4 PJ |
1860 | |
1861 | return overlap; | |
1862 | } | |
1863 | ||
1da177e4 LT |
1864 | /* |
1865 | * proc_cpuset_show() | |
1866 | * - Print tasks cpuset path into seq_file. | |
1867 | * - Used for /proc/<pid>/cpuset. | |
053199ed PJ |
1868 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
1869 | * doesn't really matter if tsk->cpuset changes after we read it, | |
1870 | * and we take manage_sem, keeping attach_task() from changing it | |
1871 | * anyway. | |
1da177e4 LT |
1872 | */ |
1873 | ||
1874 | static int proc_cpuset_show(struct seq_file *m, void *v) | |
1875 | { | |
1876 | struct cpuset *cs; | |
1877 | struct task_struct *tsk; | |
1878 | char *buf; | |
1879 | int retval = 0; | |
1880 | ||
1881 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1882 | if (!buf) | |
1883 | return -ENOMEM; | |
1884 | ||
1885 | tsk = m->private; | |
053199ed | 1886 | down(&manage_sem); |
1da177e4 | 1887 | cs = tsk->cpuset; |
1da177e4 LT |
1888 | if (!cs) { |
1889 | retval = -EINVAL; | |
1890 | goto out; | |
1891 | } | |
1892 | ||
1893 | retval = cpuset_path(cs, buf, PAGE_SIZE); | |
1894 | if (retval < 0) | |
1895 | goto out; | |
1896 | seq_puts(m, buf); | |
1897 | seq_putc(m, '\n'); | |
1898 | out: | |
053199ed | 1899 | up(&manage_sem); |
1da177e4 LT |
1900 | kfree(buf); |
1901 | return retval; | |
1902 | } | |
1903 | ||
1904 | static int cpuset_open(struct inode *inode, struct file *file) | |
1905 | { | |
1906 | struct task_struct *tsk = PROC_I(inode)->task; | |
1907 | return single_open(file, proc_cpuset_show, tsk); | |
1908 | } | |
1909 | ||
1910 | struct file_operations proc_cpuset_operations = { | |
1911 | .open = cpuset_open, | |
1912 | .read = seq_read, | |
1913 | .llseek = seq_lseek, | |
1914 | .release = single_release, | |
1915 | }; | |
1916 | ||
1917 | /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ | |
1918 | char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) | |
1919 | { | |
1920 | buffer += sprintf(buffer, "Cpus_allowed:\t"); | |
1921 | buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed); | |
1922 | buffer += sprintf(buffer, "\n"); | |
1923 | buffer += sprintf(buffer, "Mems_allowed:\t"); | |
1924 | buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed); | |
1925 | buffer += sprintf(buffer, "\n"); | |
1926 | return buffer; | |
1927 | } |