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