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