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