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