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