]>
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. | |
029190c5 | 7 | * Copyright (C) 2004-2007 Silicon Graphics, Inc. |
8793d854 | 8 | * Copyright (C) 2006 Google, Inc |
1da177e4 LT |
9 | * |
10 | * Portions derived from Patrick Mochel's sysfs code. | |
11 | * sysfs is Copyright (c) 2001-3 Patrick Mochel | |
1da177e4 | 12 | * |
825a46af | 13 | * 2003-10-10 Written by Simon Derr. |
1da177e4 | 14 | * 2003-10-22 Updates by Stephen Hemminger. |
825a46af | 15 | * 2004 May-July Rework by Paul Jackson. |
8793d854 | 16 | * 2006 Rework by Paul Menage to use generic cgroups |
1da177e4 LT |
17 | * |
18 | * This file is subject to the terms and conditions of the GNU General Public | |
19 | * License. See the file COPYING in the main directory of the Linux | |
20 | * distribution for more details. | |
21 | */ | |
22 | ||
1da177e4 LT |
23 | #include <linux/cpu.h> |
24 | #include <linux/cpumask.h> | |
25 | #include <linux/cpuset.h> | |
26 | #include <linux/err.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/file.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/kernel.h> | |
33 | #include <linux/kmod.h> | |
34 | #include <linux/list.h> | |
68860ec1 | 35 | #include <linux/mempolicy.h> |
1da177e4 LT |
36 | #include <linux/mm.h> |
37 | #include <linux/module.h> | |
38 | #include <linux/mount.h> | |
39 | #include <linux/namei.h> | |
40 | #include <linux/pagemap.h> | |
41 | #include <linux/proc_fs.h> | |
6b9c2603 | 42 | #include <linux/rcupdate.h> |
1da177e4 LT |
43 | #include <linux/sched.h> |
44 | #include <linux/seq_file.h> | |
22fb52dd | 45 | #include <linux/security.h> |
1da177e4 | 46 | #include <linux/slab.h> |
1da177e4 LT |
47 | #include <linux/spinlock.h> |
48 | #include <linux/stat.h> | |
49 | #include <linux/string.h> | |
50 | #include <linux/time.h> | |
51 | #include <linux/backing-dev.h> | |
52 | #include <linux/sort.h> | |
53 | ||
54 | #include <asm/uaccess.h> | |
55 | #include <asm/atomic.h> | |
3d3f26a7 | 56 | #include <linux/mutex.h> |
029190c5 | 57 | #include <linux/kfifo.h> |
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 | |
8793d854 PM |
66 | /* Retrieve the cpuset from a cgroup */ |
67 | struct cgroup_subsys cpuset_subsys; | |
68 | struct cpuset; | |
69 | ||
3e0d98b9 PJ |
70 | /* See "Frequency meter" comments, below. */ |
71 | ||
72 | struct fmeter { | |
73 | int cnt; /* unprocessed events count */ | |
74 | int val; /* most recent output value */ | |
75 | time_t time; /* clock (secs) when val computed */ | |
76 | spinlock_t lock; /* guards read or write of above */ | |
77 | }; | |
78 | ||
1da177e4 | 79 | struct cpuset { |
8793d854 PM |
80 | struct cgroup_subsys_state css; |
81 | ||
1da177e4 LT |
82 | unsigned long flags; /* "unsigned long" so bitops work */ |
83 | cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ | |
84 | nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ | |
85 | ||
1da177e4 | 86 | struct cpuset *parent; /* my parent */ |
1da177e4 LT |
87 | |
88 | /* | |
89 | * Copy of global cpuset_mems_generation as of the most | |
90 | * recent time this cpuset changed its mems_allowed. | |
91 | */ | |
3e0d98b9 PJ |
92 | int mems_generation; |
93 | ||
94 | struct fmeter fmeter; /* memory_pressure filter */ | |
029190c5 PJ |
95 | |
96 | /* partition number for rebuild_sched_domains() */ | |
97 | int pn; | |
1da177e4 LT |
98 | }; |
99 | ||
8793d854 PM |
100 | /* Retrieve the cpuset for a cgroup */ |
101 | static inline struct cpuset *cgroup_cs(struct cgroup *cont) | |
102 | { | |
103 | return container_of(cgroup_subsys_state(cont, cpuset_subsys_id), | |
104 | struct cpuset, css); | |
105 | } | |
106 | ||
107 | /* Retrieve the cpuset for a task */ | |
108 | static inline struct cpuset *task_cs(struct task_struct *task) | |
109 | { | |
110 | return container_of(task_subsys_state(task, cpuset_subsys_id), | |
111 | struct cpuset, css); | |
112 | } | |
113 | ||
114 | ||
1da177e4 LT |
115 | /* bits in struct cpuset flags field */ |
116 | typedef enum { | |
117 | CS_CPU_EXCLUSIVE, | |
118 | CS_MEM_EXCLUSIVE, | |
45b07ef3 | 119 | CS_MEMORY_MIGRATE, |
029190c5 | 120 | CS_SCHED_LOAD_BALANCE, |
825a46af PJ |
121 | CS_SPREAD_PAGE, |
122 | CS_SPREAD_SLAB, | |
1da177e4 LT |
123 | } cpuset_flagbits_t; |
124 | ||
125 | /* convenient tests for these bits */ | |
126 | static inline int is_cpu_exclusive(const struct cpuset *cs) | |
127 | { | |
7b5b9ef0 | 128 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
1da177e4 LT |
129 | } |
130 | ||
131 | static inline int is_mem_exclusive(const struct cpuset *cs) | |
132 | { | |
7b5b9ef0 | 133 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
1da177e4 LT |
134 | } |
135 | ||
029190c5 PJ |
136 | static inline int is_sched_load_balance(const struct cpuset *cs) |
137 | { | |
138 | return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); | |
139 | } | |
140 | ||
45b07ef3 PJ |
141 | static inline int is_memory_migrate(const struct cpuset *cs) |
142 | { | |
7b5b9ef0 | 143 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
45b07ef3 PJ |
144 | } |
145 | ||
825a46af PJ |
146 | static inline int is_spread_page(const struct cpuset *cs) |
147 | { | |
148 | return test_bit(CS_SPREAD_PAGE, &cs->flags); | |
149 | } | |
150 | ||
151 | static inline int is_spread_slab(const struct cpuset *cs) | |
152 | { | |
153 | return test_bit(CS_SPREAD_SLAB, &cs->flags); | |
154 | } | |
155 | ||
1da177e4 | 156 | /* |
151a4420 | 157 | * Increment this integer everytime any cpuset changes its |
1da177e4 LT |
158 | * mems_allowed value. Users of cpusets can track this generation |
159 | * number, and avoid having to lock and reload mems_allowed unless | |
160 | * the cpuset they're using changes generation. | |
161 | * | |
162 | * A single, global generation is needed because attach_task() could | |
163 | * reattach a task to a different cpuset, which must not have its | |
164 | * generation numbers aliased with those of that tasks previous cpuset. | |
165 | * | |
166 | * Generations are needed for mems_allowed because one task cannot | |
167 | * modify anothers memory placement. So we must enable every task, | |
168 | * on every visit to __alloc_pages(), to efficiently check whether | |
169 | * its current->cpuset->mems_allowed has changed, requiring an update | |
170 | * of its current->mems_allowed. | |
151a4420 PJ |
171 | * |
172 | * Since cpuset_mems_generation is guarded by manage_mutex, | |
173 | * there is no need to mark it atomic. | |
1da177e4 | 174 | */ |
151a4420 | 175 | static int cpuset_mems_generation; |
1da177e4 LT |
176 | |
177 | static struct cpuset top_cpuset = { | |
178 | .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)), | |
179 | .cpus_allowed = CPU_MASK_ALL, | |
180 | .mems_allowed = NODE_MASK_ALL, | |
1da177e4 LT |
181 | }; |
182 | ||
1da177e4 | 183 | /* |
3d3f26a7 IM |
184 | * We have two global cpuset mutexes below. They can nest. |
185 | * It is ok to first take manage_mutex, then nest callback_mutex. We also | |
053199ed PJ |
186 | * require taking task_lock() when dereferencing a tasks cpuset pointer. |
187 | * See "The task_lock() exception", at the end of this comment. | |
188 | * | |
3d3f26a7 IM |
189 | * A task must hold both mutexes to modify cpusets. If a task |
190 | * holds manage_mutex, then it blocks others wanting that mutex, | |
191 | * ensuring that it is the only task able to also acquire callback_mutex | |
053199ed PJ |
192 | * and be able to modify cpusets. It can perform various checks on |
193 | * the cpuset structure first, knowing nothing will change. It can | |
3d3f26a7 | 194 | * also allocate memory while just holding manage_mutex. While it is |
053199ed | 195 | * performing these checks, various callback routines can briefly |
3d3f26a7 IM |
196 | * acquire callback_mutex to query cpusets. Once it is ready to make |
197 | * the changes, it takes callback_mutex, blocking everyone else. | |
053199ed PJ |
198 | * |
199 | * Calls to the kernel memory allocator can not be made while holding | |
3d3f26a7 | 200 | * callback_mutex, as that would risk double tripping on callback_mutex |
053199ed PJ |
201 | * from one of the callbacks into the cpuset code from within |
202 | * __alloc_pages(). | |
203 | * | |
3d3f26a7 | 204 | * If a task is only holding callback_mutex, then it has read-only |
053199ed PJ |
205 | * access to cpusets. |
206 | * | |
207 | * The task_struct fields mems_allowed and mems_generation may only | |
208 | * be accessed in the context of that task, so require no locks. | |
209 | * | |
210 | * Any task can increment and decrement the count field without lock. | |
3d3f26a7 | 211 | * So in general, code holding manage_mutex or callback_mutex can't rely |
053199ed | 212 | * on the count field not changing. However, if the count goes to |
3d3f26a7 | 213 | * zero, then only attach_task(), which holds both mutexes, can |
053199ed PJ |
214 | * increment it again. Because a count of zero means that no tasks |
215 | * are currently attached, therefore there is no way a task attached | |
216 | * to that cpuset can fork (the other way to increment the count). | |
3d3f26a7 | 217 | * So code holding manage_mutex or callback_mutex can safely assume that |
053199ed | 218 | * if the count is zero, it will stay zero. Similarly, if a task |
3d3f26a7 | 219 | * holds manage_mutex or callback_mutex on a cpuset with zero count, it |
053199ed | 220 | * knows that the cpuset won't be removed, as cpuset_rmdir() needs |
3d3f26a7 | 221 | * both of those mutexes. |
053199ed PJ |
222 | * |
223 | * The cpuset_common_file_write handler for operations that modify | |
3d3f26a7 | 224 | * the cpuset hierarchy holds manage_mutex across the entire operation, |
053199ed PJ |
225 | * single threading all such cpuset modifications across the system. |
226 | * | |
3d3f26a7 | 227 | * The cpuset_common_file_read() handlers only hold callback_mutex across |
053199ed PJ |
228 | * small pieces of code, such as when reading out possibly multi-word |
229 | * cpumasks and nodemasks. | |
230 | * | |
231 | * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't | |
3d3f26a7 | 232 | * (usually) take either mutex. These are the two most performance |
053199ed | 233 | * critical pieces of code here. The exception occurs on cpuset_exit(), |
3d3f26a7 | 234 | * when a task in a notify_on_release cpuset exits. Then manage_mutex |
2efe86b8 | 235 | * is taken, and if the cpuset count is zero, a usermode call made |
1da177e4 LT |
236 | * to /sbin/cpuset_release_agent with the name of the cpuset (path |
237 | * relative to the root of cpuset file system) as the argument. | |
238 | * | |
053199ed PJ |
239 | * A cpuset can only be deleted if both its 'count' of using tasks |
240 | * is zero, and its list of 'children' cpusets is empty. Since all | |
241 | * tasks in the system use _some_ cpuset, and since there is always at | |
f400e198 | 242 | * least one task in the system (init), therefore, top_cpuset |
053199ed PJ |
243 | * always has either children cpusets and/or using tasks. So we don't |
244 | * need a special hack to ensure that top_cpuset cannot be deleted. | |
245 | * | |
246 | * The above "Tale of Two Semaphores" would be complete, but for: | |
247 | * | |
248 | * The task_lock() exception | |
249 | * | |
250 | * The need for this exception arises from the action of attach_task(), | |
251 | * which overwrites one tasks cpuset pointer with another. It does | |
3d3f26a7 | 252 | * so using both mutexes, however there are several performance |
053199ed | 253 | * critical places that need to reference task->cpuset without the |
3d3f26a7 | 254 | * expense of grabbing a system global mutex. Therefore except as |
053199ed PJ |
255 | * noted below, when dereferencing or, as in attach_task(), modifying |
256 | * a tasks cpuset pointer we use task_lock(), which acts on a spinlock | |
257 | * (task->alloc_lock) already in the task_struct routinely used for | |
258 | * such matters. | |
6b9c2603 PJ |
259 | * |
260 | * P.S. One more locking exception. RCU is used to guard the | |
261 | * update of a tasks cpuset pointer by attach_task() and the | |
262 | * access of task->cpuset->mems_generation via that pointer in | |
263 | * the routine cpuset_update_task_memory_state(). | |
1da177e4 LT |
264 | */ |
265 | ||
3d3f26a7 | 266 | static DEFINE_MUTEX(callback_mutex); |
4247bdc6 | 267 | |
8793d854 PM |
268 | /* This is ugly, but preserves the userspace API for existing cpuset |
269 | * users. If someone tries to mount the "cpuset" filesystem, we | |
270 | * silently switch it to mount "cgroup" instead */ | |
454e2398 DH |
271 | static int cpuset_get_sb(struct file_system_type *fs_type, |
272 | int flags, const char *unused_dev_name, | |
273 | void *data, struct vfsmount *mnt) | |
1da177e4 | 274 | { |
8793d854 PM |
275 | struct file_system_type *cgroup_fs = get_fs_type("cgroup"); |
276 | int ret = -ENODEV; | |
277 | if (cgroup_fs) { | |
278 | char mountopts[] = | |
279 | "cpuset,noprefix," | |
280 | "release_agent=/sbin/cpuset_release_agent"; | |
281 | ret = cgroup_fs->get_sb(cgroup_fs, flags, | |
282 | unused_dev_name, mountopts, mnt); | |
283 | put_filesystem(cgroup_fs); | |
284 | } | |
285 | return ret; | |
1da177e4 LT |
286 | } |
287 | ||
288 | static struct file_system_type cpuset_fs_type = { | |
289 | .name = "cpuset", | |
290 | .get_sb = cpuset_get_sb, | |
1da177e4 LT |
291 | }; |
292 | ||
1da177e4 LT |
293 | /* |
294 | * Return in *pmask the portion of a cpusets's cpus_allowed that | |
295 | * are online. If none are online, walk up the cpuset hierarchy | |
296 | * until we find one that does have some online cpus. If we get | |
297 | * all the way to the top and still haven't found any online cpus, | |
298 | * return cpu_online_map. Or if passed a NULL cs from an exit'ing | |
299 | * task, return cpu_online_map. | |
300 | * | |
301 | * One way or another, we guarantee to return some non-empty subset | |
302 | * of cpu_online_map. | |
303 | * | |
3d3f26a7 | 304 | * Call with callback_mutex held. |
1da177e4 LT |
305 | */ |
306 | ||
307 | static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask) | |
308 | { | |
309 | while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map)) | |
310 | cs = cs->parent; | |
311 | if (cs) | |
312 | cpus_and(*pmask, cs->cpus_allowed, cpu_online_map); | |
313 | else | |
314 | *pmask = cpu_online_map; | |
315 | BUG_ON(!cpus_intersects(*pmask, cpu_online_map)); | |
316 | } | |
317 | ||
318 | /* | |
319 | * Return in *pmask the portion of a cpusets's mems_allowed that | |
0e1e7c7a CL |
320 | * are online, with memory. If none are online with memory, walk |
321 | * up the cpuset hierarchy until we find one that does have some | |
322 | * online mems. If we get all the way to the top and still haven't | |
323 | * found any online mems, return node_states[N_HIGH_MEMORY]. | |
1da177e4 LT |
324 | * |
325 | * One way or another, we guarantee to return some non-empty subset | |
0e1e7c7a | 326 | * of node_states[N_HIGH_MEMORY]. |
1da177e4 | 327 | * |
3d3f26a7 | 328 | * Call with callback_mutex held. |
1da177e4 LT |
329 | */ |
330 | ||
331 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) | |
332 | { | |
0e1e7c7a CL |
333 | while (cs && !nodes_intersects(cs->mems_allowed, |
334 | node_states[N_HIGH_MEMORY])) | |
1da177e4 LT |
335 | cs = cs->parent; |
336 | if (cs) | |
0e1e7c7a CL |
337 | nodes_and(*pmask, cs->mems_allowed, |
338 | node_states[N_HIGH_MEMORY]); | |
1da177e4 | 339 | else |
0e1e7c7a CL |
340 | *pmask = node_states[N_HIGH_MEMORY]; |
341 | BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY])); | |
1da177e4 LT |
342 | } |
343 | ||
cf2a473c PJ |
344 | /** |
345 | * cpuset_update_task_memory_state - update task memory placement | |
346 | * | |
347 | * If the current tasks cpusets mems_allowed changed behind our | |
348 | * backs, update current->mems_allowed, mems_generation and task NUMA | |
349 | * mempolicy to the new value. | |
053199ed | 350 | * |
cf2a473c PJ |
351 | * Task mempolicy is updated by rebinding it relative to the |
352 | * current->cpuset if a task has its memory placement changed. | |
353 | * Do not call this routine if in_interrupt(). | |
354 | * | |
4a01c8d5 PJ |
355 | * Call without callback_mutex or task_lock() held. May be |
356 | * called with or without manage_mutex held. Thanks in part to | |
357 | * 'the_top_cpuset_hack', the tasks cpuset pointer will never | |
358 | * be NULL. This routine also might acquire callback_mutex and | |
cf2a473c | 359 | * current->mm->mmap_sem during call. |
053199ed | 360 | * |
6b9c2603 PJ |
361 | * Reading current->cpuset->mems_generation doesn't need task_lock |
362 | * to guard the current->cpuset derefence, because it is guarded | |
363 | * from concurrent freeing of current->cpuset by attach_task(), | |
364 | * using RCU. | |
365 | * | |
366 | * The rcu_dereference() is technically probably not needed, | |
367 | * as I don't actually mind if I see a new cpuset pointer but | |
368 | * an old value of mems_generation. However this really only | |
369 | * matters on alpha systems using cpusets heavily. If I dropped | |
370 | * that rcu_dereference(), it would save them a memory barrier. | |
371 | * For all other arch's, rcu_dereference is a no-op anyway, and for | |
372 | * alpha systems not using cpusets, another planned optimization, | |
373 | * avoiding the rcu critical section for tasks in the root cpuset | |
374 | * which is statically allocated, so can't vanish, will make this | |
375 | * irrelevant. Better to use RCU as intended, than to engage in | |
376 | * some cute trick to save a memory barrier that is impossible to | |
377 | * test, for alpha systems using cpusets heavily, which might not | |
378 | * even exist. | |
053199ed PJ |
379 | * |
380 | * This routine is needed to update the per-task mems_allowed data, | |
381 | * within the tasks context, when it is trying to allocate memory | |
382 | * (in various mm/mempolicy.c routines) and notices that some other | |
383 | * task has been modifying its cpuset. | |
1da177e4 LT |
384 | */ |
385 | ||
fe85a998 | 386 | void cpuset_update_task_memory_state(void) |
1da177e4 | 387 | { |
053199ed | 388 | int my_cpusets_mem_gen; |
cf2a473c | 389 | struct task_struct *tsk = current; |
6b9c2603 | 390 | struct cpuset *cs; |
053199ed | 391 | |
8793d854 | 392 | if (task_cs(tsk) == &top_cpuset) { |
03a285f5 PJ |
393 | /* Don't need rcu for top_cpuset. It's never freed. */ |
394 | my_cpusets_mem_gen = top_cpuset.mems_generation; | |
395 | } else { | |
396 | rcu_read_lock(); | |
8793d854 | 397 | my_cpusets_mem_gen = task_cs(current)->mems_generation; |
03a285f5 PJ |
398 | rcu_read_unlock(); |
399 | } | |
1da177e4 | 400 | |
cf2a473c | 401 | if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) { |
3d3f26a7 | 402 | mutex_lock(&callback_mutex); |
cf2a473c | 403 | task_lock(tsk); |
8793d854 | 404 | cs = task_cs(tsk); /* Maybe changed when task not locked */ |
cf2a473c PJ |
405 | guarantee_online_mems(cs, &tsk->mems_allowed); |
406 | tsk->cpuset_mems_generation = cs->mems_generation; | |
825a46af PJ |
407 | if (is_spread_page(cs)) |
408 | tsk->flags |= PF_SPREAD_PAGE; | |
409 | else | |
410 | tsk->flags &= ~PF_SPREAD_PAGE; | |
411 | if (is_spread_slab(cs)) | |
412 | tsk->flags |= PF_SPREAD_SLAB; | |
413 | else | |
414 | tsk->flags &= ~PF_SPREAD_SLAB; | |
cf2a473c | 415 | task_unlock(tsk); |
3d3f26a7 | 416 | mutex_unlock(&callback_mutex); |
74cb2155 | 417 | mpol_rebind_task(tsk, &tsk->mems_allowed); |
1da177e4 LT |
418 | } |
419 | } | |
420 | ||
421 | /* | |
422 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? | |
423 | * | |
424 | * One cpuset is a subset of another if all its allowed CPUs and | |
425 | * Memory Nodes are a subset of the other, and its exclusive flags | |
3d3f26a7 | 426 | * are only set if the other's are set. Call holding manage_mutex. |
1da177e4 LT |
427 | */ |
428 | ||
429 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) | |
430 | { | |
431 | return cpus_subset(p->cpus_allowed, q->cpus_allowed) && | |
432 | nodes_subset(p->mems_allowed, q->mems_allowed) && | |
433 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && | |
434 | is_mem_exclusive(p) <= is_mem_exclusive(q); | |
435 | } | |
436 | ||
437 | /* | |
438 | * validate_change() - Used to validate that any proposed cpuset change | |
439 | * follows the structural rules for cpusets. | |
440 | * | |
441 | * If we replaced the flag and mask values of the current cpuset | |
442 | * (cur) with those values in the trial cpuset (trial), would | |
443 | * our various subset and exclusive rules still be valid? Presumes | |
3d3f26a7 | 444 | * manage_mutex held. |
1da177e4 LT |
445 | * |
446 | * 'cur' is the address of an actual, in-use cpuset. Operations | |
447 | * such as list traversal that depend on the actual address of the | |
448 | * cpuset in the list must use cur below, not trial. | |
449 | * | |
450 | * 'trial' is the address of bulk structure copy of cur, with | |
451 | * perhaps one or more of the fields cpus_allowed, mems_allowed, | |
452 | * or flags changed to new, trial values. | |
453 | * | |
454 | * Return 0 if valid, -errno if not. | |
455 | */ | |
456 | ||
457 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) | |
458 | { | |
8793d854 | 459 | struct cgroup *cont; |
1da177e4 LT |
460 | struct cpuset *c, *par; |
461 | ||
462 | /* Each of our child cpusets must be a subset of us */ | |
8793d854 PM |
463 | list_for_each_entry(cont, &cur->css.cgroup->children, sibling) { |
464 | if (!is_cpuset_subset(cgroup_cs(cont), trial)) | |
1da177e4 LT |
465 | return -EBUSY; |
466 | } | |
467 | ||
468 | /* Remaining checks don't apply to root cpuset */ | |
69604067 | 469 | if (cur == &top_cpuset) |
1da177e4 LT |
470 | return 0; |
471 | ||
69604067 PJ |
472 | par = cur->parent; |
473 | ||
1da177e4 LT |
474 | /* We must be a subset of our parent cpuset */ |
475 | if (!is_cpuset_subset(trial, par)) | |
476 | return -EACCES; | |
477 | ||
478 | /* If either I or some sibling (!= me) is exclusive, we can't overlap */ | |
8793d854 PM |
479 | list_for_each_entry(cont, &par->css.cgroup->children, sibling) { |
480 | c = cgroup_cs(cont); | |
1da177e4 LT |
481 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
482 | c != cur && | |
483 | cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) | |
484 | return -EINVAL; | |
485 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && | |
486 | c != cur && | |
487 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) | |
488 | return -EINVAL; | |
489 | } | |
490 | ||
020958b6 PJ |
491 | /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */ |
492 | if (cgroup_task_count(cur->css.cgroup)) { | |
493 | if (cpus_empty(trial->cpus_allowed) || | |
494 | nodes_empty(trial->mems_allowed)) { | |
495 | return -ENOSPC; | |
496 | } | |
497 | } | |
498 | ||
1da177e4 LT |
499 | return 0; |
500 | } | |
501 | ||
029190c5 PJ |
502 | /* |
503 | * Helper routine for rebuild_sched_domains(). | |
504 | * Do cpusets a, b have overlapping cpus_allowed masks? | |
505 | */ | |
506 | ||
507 | static int cpusets_overlap(struct cpuset *a, struct cpuset *b) | |
508 | { | |
509 | return cpus_intersects(a->cpus_allowed, b->cpus_allowed); | |
510 | } | |
511 | ||
512 | /* | |
513 | * rebuild_sched_domains() | |
514 | * | |
515 | * If the flag 'sched_load_balance' of any cpuset with non-empty | |
516 | * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset | |
517 | * which has that flag enabled, or if any cpuset with a non-empty | |
518 | * 'cpus' is removed, then call this routine to rebuild the | |
519 | * scheduler's dynamic sched domains. | |
520 | * | |
521 | * This routine builds a partial partition of the systems CPUs | |
522 | * (the set of non-overlappping cpumask_t's in the array 'part' | |
523 | * below), and passes that partial partition to the kernel/sched.c | |
524 | * partition_sched_domains() routine, which will rebuild the | |
525 | * schedulers load balancing domains (sched domains) as specified | |
526 | * by that partial partition. A 'partial partition' is a set of | |
527 | * non-overlapping subsets whose union is a subset of that set. | |
528 | * | |
529 | * See "What is sched_load_balance" in Documentation/cpusets.txt | |
530 | * for a background explanation of this. | |
531 | * | |
532 | * Does not return errors, on the theory that the callers of this | |
533 | * routine would rather not worry about failures to rebuild sched | |
534 | * domains when operating in the severe memory shortage situations | |
535 | * that could cause allocation failures below. | |
536 | * | |
537 | * Call with cgroup_mutex held. May take callback_mutex during | |
538 | * call due to the kfifo_alloc() and kmalloc() calls. May nest | |
539 | * a call to the lock_cpu_hotplug()/unlock_cpu_hotplug() pair. | |
540 | * Must not be called holding callback_mutex, because we must not | |
541 | * call lock_cpu_hotplug() while holding callback_mutex. Elsewhere | |
542 | * the kernel nests callback_mutex inside lock_cpu_hotplug() calls. | |
543 | * So the reverse nesting would risk an ABBA deadlock. | |
544 | * | |
545 | * The three key local variables below are: | |
546 | * q - a kfifo queue of cpuset pointers, used to implement a | |
547 | * top-down scan of all cpusets. This scan loads a pointer | |
548 | * to each cpuset marked is_sched_load_balance into the | |
549 | * array 'csa'. For our purposes, rebuilding the schedulers | |
550 | * sched domains, we can ignore !is_sched_load_balance cpusets. | |
551 | * csa - (for CpuSet Array) Array of pointers to all the cpusets | |
552 | * that need to be load balanced, for convenient iterative | |
553 | * access by the subsequent code that finds the best partition, | |
554 | * i.e the set of domains (subsets) of CPUs such that the | |
555 | * cpus_allowed of every cpuset marked is_sched_load_balance | |
556 | * is a subset of one of these domains, while there are as | |
557 | * many such domains as possible, each as small as possible. | |
558 | * doms - Conversion of 'csa' to an array of cpumasks, for passing to | |
559 | * the kernel/sched.c routine partition_sched_domains() in a | |
560 | * convenient format, that can be easily compared to the prior | |
561 | * value to determine what partition elements (sched domains) | |
562 | * were changed (added or removed.) | |
563 | * | |
564 | * Finding the best partition (set of domains): | |
565 | * The triple nested loops below over i, j, k scan over the | |
566 | * load balanced cpusets (using the array of cpuset pointers in | |
567 | * csa[]) looking for pairs of cpusets that have overlapping | |
568 | * cpus_allowed, but which don't have the same 'pn' partition | |
569 | * number and gives them in the same partition number. It keeps | |
570 | * looping on the 'restart' label until it can no longer find | |
571 | * any such pairs. | |
572 | * | |
573 | * The union of the cpus_allowed masks from the set of | |
574 | * all cpusets having the same 'pn' value then form the one | |
575 | * element of the partition (one sched domain) to be passed to | |
576 | * partition_sched_domains(). | |
577 | */ | |
578 | ||
579 | static void rebuild_sched_domains(void) | |
580 | { | |
581 | struct kfifo *q; /* queue of cpusets to be scanned */ | |
582 | struct cpuset *cp; /* scans q */ | |
583 | struct cpuset **csa; /* array of all cpuset ptrs */ | |
584 | int csn; /* how many cpuset ptrs in csa so far */ | |
585 | int i, j, k; /* indices for partition finding loops */ | |
586 | cpumask_t *doms; /* resulting partition; i.e. sched domains */ | |
587 | int ndoms; /* number of sched domains in result */ | |
588 | int nslot; /* next empty doms[] cpumask_t slot */ | |
589 | ||
590 | q = NULL; | |
591 | csa = NULL; | |
592 | doms = NULL; | |
593 | ||
594 | /* Special case for the 99% of systems with one, full, sched domain */ | |
595 | if (is_sched_load_balance(&top_cpuset)) { | |
596 | ndoms = 1; | |
597 | doms = kmalloc(sizeof(cpumask_t), GFP_KERNEL); | |
598 | if (!doms) | |
599 | goto rebuild; | |
600 | *doms = top_cpuset.cpus_allowed; | |
601 | goto rebuild; | |
602 | } | |
603 | ||
604 | q = kfifo_alloc(number_of_cpusets * sizeof(cp), GFP_KERNEL, NULL); | |
605 | if (IS_ERR(q)) | |
606 | goto done; | |
607 | csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL); | |
608 | if (!csa) | |
609 | goto done; | |
610 | csn = 0; | |
611 | ||
612 | cp = &top_cpuset; | |
613 | __kfifo_put(q, (void *)&cp, sizeof(cp)); | |
614 | while (__kfifo_get(q, (void *)&cp, sizeof(cp))) { | |
615 | struct cgroup *cont; | |
616 | struct cpuset *child; /* scans child cpusets of cp */ | |
617 | if (is_sched_load_balance(cp)) | |
618 | csa[csn++] = cp; | |
619 | list_for_each_entry(cont, &cp->css.cgroup->children, sibling) { | |
620 | child = cgroup_cs(cont); | |
621 | __kfifo_put(q, (void *)&child, sizeof(cp)); | |
622 | } | |
623 | } | |
624 | ||
625 | for (i = 0; i < csn; i++) | |
626 | csa[i]->pn = i; | |
627 | ndoms = csn; | |
628 | ||
629 | restart: | |
630 | /* Find the best partition (set of sched domains) */ | |
631 | for (i = 0; i < csn; i++) { | |
632 | struct cpuset *a = csa[i]; | |
633 | int apn = a->pn; | |
634 | ||
635 | for (j = 0; j < csn; j++) { | |
636 | struct cpuset *b = csa[j]; | |
637 | int bpn = b->pn; | |
638 | ||
639 | if (apn != bpn && cpusets_overlap(a, b)) { | |
640 | for (k = 0; k < csn; k++) { | |
641 | struct cpuset *c = csa[k]; | |
642 | ||
643 | if (c->pn == bpn) | |
644 | c->pn = apn; | |
645 | } | |
646 | ndoms--; /* one less element */ | |
647 | goto restart; | |
648 | } | |
649 | } | |
650 | } | |
651 | ||
652 | /* Convert <csn, csa> to <ndoms, doms> */ | |
653 | doms = kmalloc(ndoms * sizeof(cpumask_t), GFP_KERNEL); | |
654 | if (!doms) | |
655 | goto rebuild; | |
656 | ||
657 | for (nslot = 0, i = 0; i < csn; i++) { | |
658 | struct cpuset *a = csa[i]; | |
659 | int apn = a->pn; | |
660 | ||
661 | if (apn >= 0) { | |
662 | cpumask_t *dp = doms + nslot; | |
663 | ||
664 | if (nslot == ndoms) { | |
665 | static int warnings = 10; | |
666 | if (warnings) { | |
667 | printk(KERN_WARNING | |
668 | "rebuild_sched_domains confused:" | |
669 | " nslot %d, ndoms %d, csn %d, i %d," | |
670 | " apn %d\n", | |
671 | nslot, ndoms, csn, i, apn); | |
672 | warnings--; | |
673 | } | |
674 | continue; | |
675 | } | |
676 | ||
677 | cpus_clear(*dp); | |
678 | for (j = i; j < csn; j++) { | |
679 | struct cpuset *b = csa[j]; | |
680 | ||
681 | if (apn == b->pn) { | |
682 | cpus_or(*dp, *dp, b->cpus_allowed); | |
683 | b->pn = -1; | |
684 | } | |
685 | } | |
686 | nslot++; | |
687 | } | |
688 | } | |
689 | BUG_ON(nslot != ndoms); | |
690 | ||
691 | rebuild: | |
692 | /* Have scheduler rebuild sched domains */ | |
693 | lock_cpu_hotplug(); | |
694 | partition_sched_domains(ndoms, doms); | |
695 | unlock_cpu_hotplug(); | |
696 | ||
697 | done: | |
698 | if (q && !IS_ERR(q)) | |
699 | kfifo_free(q); | |
700 | kfree(csa); | |
701 | /* Don't kfree(doms) -- partition_sched_domains() does that. */ | |
702 | } | |
703 | ||
053199ed | 704 | /* |
3d3f26a7 | 705 | * Call with manage_mutex held. May take callback_mutex during call. |
053199ed PJ |
706 | */ |
707 | ||
1da177e4 LT |
708 | static int update_cpumask(struct cpuset *cs, char *buf) |
709 | { | |
710 | struct cpuset trialcs; | |
607717a6 | 711 | int retval; |
029190c5 | 712 | int cpus_changed, is_load_balanced; |
1da177e4 | 713 | |
4c4d50f7 PJ |
714 | /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */ |
715 | if (cs == &top_cpuset) | |
716 | return -EACCES; | |
717 | ||
1da177e4 | 718 | trialcs = *cs; |
6f7f02e7 DR |
719 | |
720 | /* | |
020958b6 PJ |
721 | * An empty cpus_allowed is ok iff there are no tasks in the cpuset. |
722 | * Since cpulist_parse() fails on an empty mask, we special case | |
723 | * that parsing. The validate_change() call ensures that cpusets | |
724 | * with tasks have cpus. | |
6f7f02e7 | 725 | */ |
020958b6 PJ |
726 | buf = strstrip(buf); |
727 | if (!*buf) { | |
6f7f02e7 DR |
728 | cpus_clear(trialcs.cpus_allowed); |
729 | } else { | |
730 | retval = cpulist_parse(buf, trialcs.cpus_allowed); | |
731 | if (retval < 0) | |
732 | return retval; | |
733 | } | |
1da177e4 | 734 | cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); |
1da177e4 | 735 | retval = validate_change(cs, &trialcs); |
85d7b949 DG |
736 | if (retval < 0) |
737 | return retval; | |
029190c5 PJ |
738 | |
739 | cpus_changed = !cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); | |
740 | is_load_balanced = is_sched_load_balance(&trialcs); | |
741 | ||
3d3f26a7 | 742 | mutex_lock(&callback_mutex); |
85d7b949 | 743 | cs->cpus_allowed = trialcs.cpus_allowed; |
3d3f26a7 | 744 | mutex_unlock(&callback_mutex); |
029190c5 PJ |
745 | |
746 | if (cpus_changed && is_load_balanced) | |
747 | rebuild_sched_domains(); | |
748 | ||
85d7b949 | 749 | return 0; |
1da177e4 LT |
750 | } |
751 | ||
e4e364e8 PJ |
752 | /* |
753 | * cpuset_migrate_mm | |
754 | * | |
755 | * Migrate memory region from one set of nodes to another. | |
756 | * | |
757 | * Temporarilly set tasks mems_allowed to target nodes of migration, | |
758 | * so that the migration code can allocate pages on these nodes. | |
759 | * | |
760 | * Call holding manage_mutex, so our current->cpuset won't change | |
761 | * during this call, as manage_mutex holds off any attach_task() | |
762 | * calls. Therefore we don't need to take task_lock around the | |
763 | * call to guarantee_online_mems(), as we know no one is changing | |
764 | * our tasks cpuset. | |
765 | * | |
766 | * Hold callback_mutex around the two modifications of our tasks | |
767 | * mems_allowed to synchronize with cpuset_mems_allowed(). | |
768 | * | |
769 | * While the mm_struct we are migrating is typically from some | |
770 | * other task, the task_struct mems_allowed that we are hacking | |
771 | * is for our current task, which must allocate new pages for that | |
772 | * migrating memory region. | |
773 | * | |
774 | * We call cpuset_update_task_memory_state() before hacking | |
775 | * our tasks mems_allowed, so that we are assured of being in | |
776 | * sync with our tasks cpuset, and in particular, callbacks to | |
777 | * cpuset_update_task_memory_state() from nested page allocations | |
778 | * won't see any mismatch of our cpuset and task mems_generation | |
779 | * values, so won't overwrite our hacked tasks mems_allowed | |
780 | * nodemask. | |
781 | */ | |
782 | ||
783 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, | |
784 | const nodemask_t *to) | |
785 | { | |
786 | struct task_struct *tsk = current; | |
787 | ||
788 | cpuset_update_task_memory_state(); | |
789 | ||
790 | mutex_lock(&callback_mutex); | |
791 | tsk->mems_allowed = *to; | |
792 | mutex_unlock(&callback_mutex); | |
793 | ||
794 | do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL); | |
795 | ||
796 | mutex_lock(&callback_mutex); | |
8793d854 | 797 | guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed); |
e4e364e8 PJ |
798 | mutex_unlock(&callback_mutex); |
799 | } | |
800 | ||
053199ed | 801 | /* |
4225399a PJ |
802 | * Handle user request to change the 'mems' memory placement |
803 | * of a cpuset. Needs to validate the request, update the | |
804 | * cpusets mems_allowed and mems_generation, and for each | |
04c19fa6 PJ |
805 | * task in the cpuset, rebind any vma mempolicies and if |
806 | * the cpuset is marked 'memory_migrate', migrate the tasks | |
807 | * pages to the new memory. | |
4225399a | 808 | * |
3d3f26a7 | 809 | * Call with manage_mutex held. May take callback_mutex during call. |
4225399a PJ |
810 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
811 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind | |
812 | * their mempolicies to the cpusets new mems_allowed. | |
053199ed PJ |
813 | */ |
814 | ||
8793d854 PM |
815 | static void *cpuset_being_rebound; |
816 | ||
1da177e4 LT |
817 | static int update_nodemask(struct cpuset *cs, char *buf) |
818 | { | |
819 | struct cpuset trialcs; | |
04c19fa6 | 820 | nodemask_t oldmem; |
8793d854 | 821 | struct task_struct *p; |
4225399a PJ |
822 | struct mm_struct **mmarray; |
823 | int i, n, ntasks; | |
04c19fa6 | 824 | int migrate; |
4225399a | 825 | int fudge; |
1da177e4 | 826 | int retval; |
8793d854 | 827 | struct cgroup_iter it; |
1da177e4 | 828 | |
0e1e7c7a CL |
829 | /* |
830 | * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY]; | |
831 | * it's read-only | |
832 | */ | |
38837fc7 PJ |
833 | if (cs == &top_cpuset) |
834 | return -EACCES; | |
835 | ||
1da177e4 | 836 | trialcs = *cs; |
6f7f02e7 DR |
837 | |
838 | /* | |
020958b6 PJ |
839 | * An empty mems_allowed is ok iff there are no tasks in the cpuset. |
840 | * Since nodelist_parse() fails on an empty mask, we special case | |
841 | * that parsing. The validate_change() call ensures that cpusets | |
842 | * with tasks have memory. | |
6f7f02e7 | 843 | */ |
020958b6 PJ |
844 | buf = strstrip(buf); |
845 | if (!*buf) { | |
6f7f02e7 DR |
846 | nodes_clear(trialcs.mems_allowed); |
847 | } else { | |
848 | retval = nodelist_parse(buf, trialcs.mems_allowed); | |
849 | if (retval < 0) | |
850 | goto done; | |
851 | } | |
0e1e7c7a CL |
852 | nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, |
853 | node_states[N_HIGH_MEMORY]); | |
04c19fa6 PJ |
854 | oldmem = cs->mems_allowed; |
855 | if (nodes_equal(oldmem, trialcs.mems_allowed)) { | |
856 | retval = 0; /* Too easy - nothing to do */ | |
857 | goto done; | |
858 | } | |
59dac16f PJ |
859 | retval = validate_change(cs, &trialcs); |
860 | if (retval < 0) | |
861 | goto done; | |
862 | ||
3d3f26a7 | 863 | mutex_lock(&callback_mutex); |
59dac16f | 864 | cs->mems_allowed = trialcs.mems_allowed; |
151a4420 | 865 | cs->mems_generation = cpuset_mems_generation++; |
3d3f26a7 | 866 | mutex_unlock(&callback_mutex); |
59dac16f | 867 | |
8793d854 | 868 | cpuset_being_rebound = cs; /* causes mpol_copy() rebind */ |
4225399a PJ |
869 | |
870 | fudge = 10; /* spare mmarray[] slots */ | |
871 | fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */ | |
872 | retval = -ENOMEM; | |
873 | ||
874 | /* | |
875 | * Allocate mmarray[] to hold mm reference for each task | |
876 | * in cpuset cs. Can't kmalloc GFP_KERNEL while holding | |
877 | * tasklist_lock. We could use GFP_ATOMIC, but with a | |
878 | * few more lines of code, we can retry until we get a big | |
879 | * enough mmarray[] w/o using GFP_ATOMIC. | |
880 | */ | |
881 | while (1) { | |
8793d854 | 882 | ntasks = cgroup_task_count(cs->css.cgroup); /* guess */ |
4225399a PJ |
883 | ntasks += fudge; |
884 | mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL); | |
885 | if (!mmarray) | |
886 | goto done; | |
c2aef333 | 887 | read_lock(&tasklist_lock); /* block fork */ |
8793d854 | 888 | if (cgroup_task_count(cs->css.cgroup) <= ntasks) |
4225399a | 889 | break; /* got enough */ |
c2aef333 | 890 | read_unlock(&tasklist_lock); /* try again */ |
4225399a PJ |
891 | kfree(mmarray); |
892 | } | |
893 | ||
894 | n = 0; | |
895 | ||
896 | /* Load up mmarray[] with mm reference for each task in cpuset. */ | |
8793d854 PM |
897 | cgroup_iter_start(cs->css.cgroup, &it); |
898 | while ((p = cgroup_iter_next(cs->css.cgroup, &it))) { | |
4225399a PJ |
899 | struct mm_struct *mm; |
900 | ||
901 | if (n >= ntasks) { | |
902 | printk(KERN_WARNING | |
903 | "Cpuset mempolicy rebind incomplete.\n"); | |
8793d854 | 904 | break; |
4225399a | 905 | } |
4225399a PJ |
906 | mm = get_task_mm(p); |
907 | if (!mm) | |
908 | continue; | |
909 | mmarray[n++] = mm; | |
8793d854 PM |
910 | } |
911 | cgroup_iter_end(cs->css.cgroup, &it); | |
c2aef333 | 912 | read_unlock(&tasklist_lock); |
4225399a PJ |
913 | |
914 | /* | |
915 | * Now that we've dropped the tasklist spinlock, we can | |
916 | * rebind the vma mempolicies of each mm in mmarray[] to their | |
917 | * new cpuset, and release that mm. The mpol_rebind_mm() | |
918 | * call takes mmap_sem, which we couldn't take while holding | |
919 | * tasklist_lock. Forks can happen again now - the mpol_copy() | |
920 | * cpuset_being_rebound check will catch such forks, and rebind | |
921 | * their vma mempolicies too. Because we still hold the global | |
3d3f26a7 | 922 | * cpuset manage_mutex, we know that no other rebind effort will |
4225399a PJ |
923 | * be contending for the global variable cpuset_being_rebound. |
924 | * It's ok if we rebind the same mm twice; mpol_rebind_mm() | |
04c19fa6 | 925 | * is idempotent. Also migrate pages in each mm to new nodes. |
4225399a | 926 | */ |
04c19fa6 | 927 | migrate = is_memory_migrate(cs); |
4225399a PJ |
928 | for (i = 0; i < n; i++) { |
929 | struct mm_struct *mm = mmarray[i]; | |
930 | ||
931 | mpol_rebind_mm(mm, &cs->mems_allowed); | |
e4e364e8 PJ |
932 | if (migrate) |
933 | cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed); | |
4225399a PJ |
934 | mmput(mm); |
935 | } | |
936 | ||
937 | /* We're done rebinding vma's to this cpusets new mems_allowed. */ | |
938 | kfree(mmarray); | |
8793d854 | 939 | cpuset_being_rebound = NULL; |
4225399a | 940 | retval = 0; |
59dac16f | 941 | done: |
1da177e4 LT |
942 | return retval; |
943 | } | |
944 | ||
8793d854 PM |
945 | int current_cpuset_is_being_rebound(void) |
946 | { | |
947 | return task_cs(current) == cpuset_being_rebound; | |
948 | } | |
949 | ||
3e0d98b9 | 950 | /* |
3d3f26a7 | 951 | * Call with manage_mutex held. |
3e0d98b9 PJ |
952 | */ |
953 | ||
954 | static int update_memory_pressure_enabled(struct cpuset *cs, char *buf) | |
955 | { | |
956 | if (simple_strtoul(buf, NULL, 10) != 0) | |
957 | cpuset_memory_pressure_enabled = 1; | |
958 | else | |
959 | cpuset_memory_pressure_enabled = 0; | |
960 | return 0; | |
961 | } | |
962 | ||
1da177e4 LT |
963 | /* |
964 | * update_flag - read a 0 or a 1 in a file and update associated flag | |
965 | * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, | |
029190c5 | 966 | * CS_SCHED_LOAD_BALANCE, |
825a46af PJ |
967 | * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE, |
968 | * CS_SPREAD_PAGE, CS_SPREAD_SLAB) | |
1da177e4 LT |
969 | * cs: the cpuset to update |
970 | * buf: the buffer where we read the 0 or 1 | |
053199ed | 971 | * |
3d3f26a7 | 972 | * Call with manage_mutex held. |
1da177e4 LT |
973 | */ |
974 | ||
975 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf) | |
976 | { | |
977 | int turning_on; | |
978 | struct cpuset trialcs; | |
607717a6 | 979 | int err; |
029190c5 | 980 | int cpus_nonempty, balance_flag_changed; |
1da177e4 LT |
981 | |
982 | turning_on = (simple_strtoul(buf, NULL, 10) != 0); | |
983 | ||
984 | trialcs = *cs; | |
985 | if (turning_on) | |
986 | set_bit(bit, &trialcs.flags); | |
987 | else | |
988 | clear_bit(bit, &trialcs.flags); | |
989 | ||
990 | err = validate_change(cs, &trialcs); | |
85d7b949 DG |
991 | if (err < 0) |
992 | return err; | |
029190c5 PJ |
993 | |
994 | cpus_nonempty = !cpus_empty(trialcs.cpus_allowed); | |
995 | balance_flag_changed = (is_sched_load_balance(cs) != | |
996 | is_sched_load_balance(&trialcs)); | |
997 | ||
3d3f26a7 | 998 | mutex_lock(&callback_mutex); |
69604067 | 999 | cs->flags = trialcs.flags; |
3d3f26a7 | 1000 | mutex_unlock(&callback_mutex); |
85d7b949 | 1001 | |
029190c5 PJ |
1002 | if (cpus_nonempty && balance_flag_changed) |
1003 | rebuild_sched_domains(); | |
1004 | ||
85d7b949 | 1005 | return 0; |
1da177e4 LT |
1006 | } |
1007 | ||
3e0d98b9 | 1008 | /* |
80f7228b | 1009 | * Frequency meter - How fast is some event occurring? |
3e0d98b9 PJ |
1010 | * |
1011 | * These routines manage a digitally filtered, constant time based, | |
1012 | * event frequency meter. There are four routines: | |
1013 | * fmeter_init() - initialize a frequency meter. | |
1014 | * fmeter_markevent() - called each time the event happens. | |
1015 | * fmeter_getrate() - returns the recent rate of such events. | |
1016 | * fmeter_update() - internal routine used to update fmeter. | |
1017 | * | |
1018 | * A common data structure is passed to each of these routines, | |
1019 | * which is used to keep track of the state required to manage the | |
1020 | * frequency meter and its digital filter. | |
1021 | * | |
1022 | * The filter works on the number of events marked per unit time. | |
1023 | * The filter is single-pole low-pass recursive (IIR). The time unit | |
1024 | * is 1 second. Arithmetic is done using 32-bit integers scaled to | |
1025 | * simulate 3 decimal digits of precision (multiplied by 1000). | |
1026 | * | |
1027 | * With an FM_COEF of 933, and a time base of 1 second, the filter | |
1028 | * has a half-life of 10 seconds, meaning that if the events quit | |
1029 | * happening, then the rate returned from the fmeter_getrate() | |
1030 | * will be cut in half each 10 seconds, until it converges to zero. | |
1031 | * | |
1032 | * It is not worth doing a real infinitely recursive filter. If more | |
1033 | * than FM_MAXTICKS ticks have elapsed since the last filter event, | |
1034 | * just compute FM_MAXTICKS ticks worth, by which point the level | |
1035 | * will be stable. | |
1036 | * | |
1037 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid | |
1038 | * arithmetic overflow in the fmeter_update() routine. | |
1039 | * | |
1040 | * Given the simple 32 bit integer arithmetic used, this meter works | |
1041 | * best for reporting rates between one per millisecond (msec) and | |
1042 | * one per 32 (approx) seconds. At constant rates faster than one | |
1043 | * per msec it maxes out at values just under 1,000,000. At constant | |
1044 | * rates between one per msec, and one per second it will stabilize | |
1045 | * to a value N*1000, where N is the rate of events per second. | |
1046 | * At constant rates between one per second and one per 32 seconds, | |
1047 | * it will be choppy, moving up on the seconds that have an event, | |
1048 | * and then decaying until the next event. At rates slower than | |
1049 | * about one in 32 seconds, it decays all the way back to zero between | |
1050 | * each event. | |
1051 | */ | |
1052 | ||
1053 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ | |
1054 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ | |
1055 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ | |
1056 | #define FM_SCALE 1000 /* faux fixed point scale */ | |
1057 | ||
1058 | /* Initialize a frequency meter */ | |
1059 | static void fmeter_init(struct fmeter *fmp) | |
1060 | { | |
1061 | fmp->cnt = 0; | |
1062 | fmp->val = 0; | |
1063 | fmp->time = 0; | |
1064 | spin_lock_init(&fmp->lock); | |
1065 | } | |
1066 | ||
1067 | /* Internal meter update - process cnt events and update value */ | |
1068 | static void fmeter_update(struct fmeter *fmp) | |
1069 | { | |
1070 | time_t now = get_seconds(); | |
1071 | time_t ticks = now - fmp->time; | |
1072 | ||
1073 | if (ticks == 0) | |
1074 | return; | |
1075 | ||
1076 | ticks = min(FM_MAXTICKS, ticks); | |
1077 | while (ticks-- > 0) | |
1078 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; | |
1079 | fmp->time = now; | |
1080 | ||
1081 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; | |
1082 | fmp->cnt = 0; | |
1083 | } | |
1084 | ||
1085 | /* Process any previous ticks, then bump cnt by one (times scale). */ | |
1086 | static void fmeter_markevent(struct fmeter *fmp) | |
1087 | { | |
1088 | spin_lock(&fmp->lock); | |
1089 | fmeter_update(fmp); | |
1090 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); | |
1091 | spin_unlock(&fmp->lock); | |
1092 | } | |
1093 | ||
1094 | /* Process any previous ticks, then return current value. */ | |
1095 | static int fmeter_getrate(struct fmeter *fmp) | |
1096 | { | |
1097 | int val; | |
1098 | ||
1099 | spin_lock(&fmp->lock); | |
1100 | fmeter_update(fmp); | |
1101 | val = fmp->val; | |
1102 | spin_unlock(&fmp->lock); | |
1103 | return val; | |
1104 | } | |
1105 | ||
8793d854 PM |
1106 | static int cpuset_can_attach(struct cgroup_subsys *ss, |
1107 | struct cgroup *cont, struct task_struct *tsk) | |
1da177e4 | 1108 | { |
8793d854 | 1109 | struct cpuset *cs = cgroup_cs(cont); |
1da177e4 | 1110 | |
1da177e4 LT |
1111 | if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
1112 | return -ENOSPC; | |
1113 | ||
8793d854 PM |
1114 | return security_task_setscheduler(tsk, 0, NULL); |
1115 | } | |
1da177e4 | 1116 | |
8793d854 PM |
1117 | static void cpuset_attach(struct cgroup_subsys *ss, |
1118 | struct cgroup *cont, struct cgroup *oldcont, | |
1119 | struct task_struct *tsk) | |
1120 | { | |
1121 | cpumask_t cpus; | |
1122 | nodemask_t from, to; | |
1123 | struct mm_struct *mm; | |
1124 | struct cpuset *cs = cgroup_cs(cont); | |
1125 | struct cpuset *oldcs = cgroup_cs(oldcont); | |
22fb52dd | 1126 | |
3d3f26a7 | 1127 | mutex_lock(&callback_mutex); |
1da177e4 LT |
1128 | guarantee_online_cpus(cs, &cpus); |
1129 | set_cpus_allowed(tsk, cpus); | |
8793d854 | 1130 | mutex_unlock(&callback_mutex); |
1da177e4 | 1131 | |
45b07ef3 PJ |
1132 | from = oldcs->mems_allowed; |
1133 | to = cs->mems_allowed; | |
4225399a PJ |
1134 | mm = get_task_mm(tsk); |
1135 | if (mm) { | |
1136 | mpol_rebind_mm(mm, &to); | |
2741a559 | 1137 | if (is_memory_migrate(cs)) |
e4e364e8 | 1138 | cpuset_migrate_mm(mm, &from, &to); |
4225399a PJ |
1139 | mmput(mm); |
1140 | } | |
1141 | ||
1da177e4 LT |
1142 | } |
1143 | ||
1144 | /* The various types of files and directories in a cpuset file system */ | |
1145 | ||
1146 | typedef enum { | |
45b07ef3 | 1147 | FILE_MEMORY_MIGRATE, |
1da177e4 LT |
1148 | FILE_CPULIST, |
1149 | FILE_MEMLIST, | |
1150 | FILE_CPU_EXCLUSIVE, | |
1151 | FILE_MEM_EXCLUSIVE, | |
029190c5 | 1152 | FILE_SCHED_LOAD_BALANCE, |
3e0d98b9 PJ |
1153 | FILE_MEMORY_PRESSURE_ENABLED, |
1154 | FILE_MEMORY_PRESSURE, | |
825a46af PJ |
1155 | FILE_SPREAD_PAGE, |
1156 | FILE_SPREAD_SLAB, | |
1da177e4 LT |
1157 | } cpuset_filetype_t; |
1158 | ||
8793d854 PM |
1159 | static ssize_t cpuset_common_file_write(struct cgroup *cont, |
1160 | struct cftype *cft, | |
1161 | struct file *file, | |
d3ed11c3 | 1162 | const char __user *userbuf, |
1da177e4 LT |
1163 | size_t nbytes, loff_t *unused_ppos) |
1164 | { | |
8793d854 | 1165 | struct cpuset *cs = cgroup_cs(cont); |
1da177e4 LT |
1166 | cpuset_filetype_t type = cft->private; |
1167 | char *buffer; | |
1168 | int retval = 0; | |
1169 | ||
1170 | /* Crude upper limit on largest legitimate cpulist user might write. */ | |
029190c5 | 1171 | if (nbytes > 100U + 6 * max(NR_CPUS, MAX_NUMNODES)) |
1da177e4 LT |
1172 | return -E2BIG; |
1173 | ||
1174 | /* +1 for nul-terminator */ | |
1175 | if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0) | |
1176 | return -ENOMEM; | |
1177 | ||
1178 | if (copy_from_user(buffer, userbuf, nbytes)) { | |
1179 | retval = -EFAULT; | |
1180 | goto out1; | |
1181 | } | |
1182 | buffer[nbytes] = 0; /* nul-terminate */ | |
1183 | ||
8793d854 | 1184 | cgroup_lock(); |
1da177e4 | 1185 | |
8793d854 | 1186 | if (cgroup_is_removed(cont)) { |
1da177e4 LT |
1187 | retval = -ENODEV; |
1188 | goto out2; | |
1189 | } | |
1190 | ||
1191 | switch (type) { | |
1192 | case FILE_CPULIST: | |
1193 | retval = update_cpumask(cs, buffer); | |
1194 | break; | |
1195 | case FILE_MEMLIST: | |
1196 | retval = update_nodemask(cs, buffer); | |
1197 | break; | |
1198 | case FILE_CPU_EXCLUSIVE: | |
1199 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer); | |
1200 | break; | |
1201 | case FILE_MEM_EXCLUSIVE: | |
1202 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); | |
1203 | break; | |
029190c5 PJ |
1204 | case FILE_SCHED_LOAD_BALANCE: |
1205 | retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, buffer); | |
1206 | break; | |
45b07ef3 PJ |
1207 | case FILE_MEMORY_MIGRATE: |
1208 | retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer); | |
1209 | break; | |
3e0d98b9 PJ |
1210 | case FILE_MEMORY_PRESSURE_ENABLED: |
1211 | retval = update_memory_pressure_enabled(cs, buffer); | |
1212 | break; | |
1213 | case FILE_MEMORY_PRESSURE: | |
1214 | retval = -EACCES; | |
1215 | break; | |
825a46af PJ |
1216 | case FILE_SPREAD_PAGE: |
1217 | retval = update_flag(CS_SPREAD_PAGE, cs, buffer); | |
151a4420 | 1218 | cs->mems_generation = cpuset_mems_generation++; |
825a46af PJ |
1219 | break; |
1220 | case FILE_SPREAD_SLAB: | |
1221 | retval = update_flag(CS_SPREAD_SLAB, cs, buffer); | |
151a4420 | 1222 | cs->mems_generation = cpuset_mems_generation++; |
825a46af | 1223 | break; |
1da177e4 LT |
1224 | default: |
1225 | retval = -EINVAL; | |
1226 | goto out2; | |
1227 | } | |
1228 | ||
1229 | if (retval == 0) | |
1230 | retval = nbytes; | |
1231 | out2: | |
8793d854 | 1232 | cgroup_unlock(); |
1da177e4 LT |
1233 | out1: |
1234 | kfree(buffer); | |
1235 | return retval; | |
1236 | } | |
1237 | ||
1da177e4 LT |
1238 | /* |
1239 | * These ascii lists should be read in a single call, by using a user | |
1240 | * buffer large enough to hold the entire map. If read in smaller | |
1241 | * chunks, there is no guarantee of atomicity. Since the display format | |
1242 | * used, list of ranges of sequential numbers, is variable length, | |
1243 | * and since these maps can change value dynamically, one could read | |
1244 | * gibberish by doing partial reads while a list was changing. | |
1245 | * A single large read to a buffer that crosses a page boundary is | |
1246 | * ok, because the result being copied to user land is not recomputed | |
1247 | * across a page fault. | |
1248 | */ | |
1249 | ||
1250 | static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs) | |
1251 | { | |
1252 | cpumask_t mask; | |
1253 | ||
3d3f26a7 | 1254 | mutex_lock(&callback_mutex); |
1da177e4 | 1255 | mask = cs->cpus_allowed; |
3d3f26a7 | 1256 | mutex_unlock(&callback_mutex); |
1da177e4 LT |
1257 | |
1258 | return cpulist_scnprintf(page, PAGE_SIZE, mask); | |
1259 | } | |
1260 | ||
1261 | static int cpuset_sprintf_memlist(char *page, struct cpuset *cs) | |
1262 | { | |
1263 | nodemask_t mask; | |
1264 | ||
3d3f26a7 | 1265 | mutex_lock(&callback_mutex); |
1da177e4 | 1266 | mask = cs->mems_allowed; |
3d3f26a7 | 1267 | mutex_unlock(&callback_mutex); |
1da177e4 LT |
1268 | |
1269 | return nodelist_scnprintf(page, PAGE_SIZE, mask); | |
1270 | } | |
1271 | ||
8793d854 PM |
1272 | static ssize_t cpuset_common_file_read(struct cgroup *cont, |
1273 | struct cftype *cft, | |
1274 | struct file *file, | |
1275 | char __user *buf, | |
1276 | size_t nbytes, loff_t *ppos) | |
1da177e4 | 1277 | { |
8793d854 | 1278 | struct cpuset *cs = cgroup_cs(cont); |
1da177e4 LT |
1279 | cpuset_filetype_t type = cft->private; |
1280 | char *page; | |
1281 | ssize_t retval = 0; | |
1282 | char *s; | |
1da177e4 | 1283 | |
e12ba74d | 1284 | if (!(page = (char *)__get_free_page(GFP_TEMPORARY))) |
1da177e4 LT |
1285 | return -ENOMEM; |
1286 | ||
1287 | s = page; | |
1288 | ||
1289 | switch (type) { | |
1290 | case FILE_CPULIST: | |
1291 | s += cpuset_sprintf_cpulist(s, cs); | |
1292 | break; | |
1293 | case FILE_MEMLIST: | |
1294 | s += cpuset_sprintf_memlist(s, cs); | |
1295 | break; | |
1296 | case FILE_CPU_EXCLUSIVE: | |
1297 | *s++ = is_cpu_exclusive(cs) ? '1' : '0'; | |
1298 | break; | |
1299 | case FILE_MEM_EXCLUSIVE: | |
1300 | *s++ = is_mem_exclusive(cs) ? '1' : '0'; | |
1301 | break; | |
029190c5 PJ |
1302 | case FILE_SCHED_LOAD_BALANCE: |
1303 | *s++ = is_sched_load_balance(cs) ? '1' : '0'; | |
1304 | break; | |
45b07ef3 PJ |
1305 | case FILE_MEMORY_MIGRATE: |
1306 | *s++ = is_memory_migrate(cs) ? '1' : '0'; | |
1307 | break; | |
3e0d98b9 PJ |
1308 | case FILE_MEMORY_PRESSURE_ENABLED: |
1309 | *s++ = cpuset_memory_pressure_enabled ? '1' : '0'; | |
1310 | break; | |
1311 | case FILE_MEMORY_PRESSURE: | |
1312 | s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter)); | |
1313 | break; | |
825a46af PJ |
1314 | case FILE_SPREAD_PAGE: |
1315 | *s++ = is_spread_page(cs) ? '1' : '0'; | |
1316 | break; | |
1317 | case FILE_SPREAD_SLAB: | |
1318 | *s++ = is_spread_slab(cs) ? '1' : '0'; | |
1319 | break; | |
1da177e4 LT |
1320 | default: |
1321 | retval = -EINVAL; | |
1322 | goto out; | |
1323 | } | |
1324 | *s++ = '\n'; | |
1da177e4 | 1325 | |
eacaa1f5 | 1326 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
1da177e4 LT |
1327 | out: |
1328 | free_page((unsigned long)page); | |
1329 | return retval; | |
1330 | } | |
1331 | ||
1da177e4 | 1332 | |
1da177e4 | 1333 | |
1da177e4 | 1334 | |
1da177e4 LT |
1335 | |
1336 | /* | |
1337 | * for the common functions, 'private' gives the type of file | |
1338 | */ | |
1339 | ||
1da177e4 LT |
1340 | static struct cftype cft_cpus = { |
1341 | .name = "cpus", | |
8793d854 PM |
1342 | .read = cpuset_common_file_read, |
1343 | .write = cpuset_common_file_write, | |
1da177e4 LT |
1344 | .private = FILE_CPULIST, |
1345 | }; | |
1346 | ||
1347 | static struct cftype cft_mems = { | |
1348 | .name = "mems", | |
8793d854 PM |
1349 | .read = cpuset_common_file_read, |
1350 | .write = cpuset_common_file_write, | |
1da177e4 LT |
1351 | .private = FILE_MEMLIST, |
1352 | }; | |
1353 | ||
1354 | static struct cftype cft_cpu_exclusive = { | |
1355 | .name = "cpu_exclusive", | |
8793d854 PM |
1356 | .read = cpuset_common_file_read, |
1357 | .write = cpuset_common_file_write, | |
1da177e4 LT |
1358 | .private = FILE_CPU_EXCLUSIVE, |
1359 | }; | |
1360 | ||
1361 | static struct cftype cft_mem_exclusive = { | |
1362 | .name = "mem_exclusive", | |
8793d854 PM |
1363 | .read = cpuset_common_file_read, |
1364 | .write = cpuset_common_file_write, | |
1da177e4 LT |
1365 | .private = FILE_MEM_EXCLUSIVE, |
1366 | }; | |
1367 | ||
029190c5 PJ |
1368 | static struct cftype cft_sched_load_balance = { |
1369 | .name = "sched_load_balance", | |
1370 | .read = cpuset_common_file_read, | |
1371 | .write = cpuset_common_file_write, | |
1372 | .private = FILE_SCHED_LOAD_BALANCE, | |
1373 | }; | |
1374 | ||
45b07ef3 PJ |
1375 | static struct cftype cft_memory_migrate = { |
1376 | .name = "memory_migrate", | |
8793d854 PM |
1377 | .read = cpuset_common_file_read, |
1378 | .write = cpuset_common_file_write, | |
45b07ef3 PJ |
1379 | .private = FILE_MEMORY_MIGRATE, |
1380 | }; | |
1381 | ||
3e0d98b9 PJ |
1382 | static struct cftype cft_memory_pressure_enabled = { |
1383 | .name = "memory_pressure_enabled", | |
8793d854 PM |
1384 | .read = cpuset_common_file_read, |
1385 | .write = cpuset_common_file_write, | |
3e0d98b9 PJ |
1386 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
1387 | }; | |
1388 | ||
1389 | static struct cftype cft_memory_pressure = { | |
1390 | .name = "memory_pressure", | |
8793d854 PM |
1391 | .read = cpuset_common_file_read, |
1392 | .write = cpuset_common_file_write, | |
3e0d98b9 PJ |
1393 | .private = FILE_MEMORY_PRESSURE, |
1394 | }; | |
1395 | ||
825a46af PJ |
1396 | static struct cftype cft_spread_page = { |
1397 | .name = "memory_spread_page", | |
8793d854 PM |
1398 | .read = cpuset_common_file_read, |
1399 | .write = cpuset_common_file_write, | |
825a46af PJ |
1400 | .private = FILE_SPREAD_PAGE, |
1401 | }; | |
1402 | ||
1403 | static struct cftype cft_spread_slab = { | |
1404 | .name = "memory_spread_slab", | |
8793d854 PM |
1405 | .read = cpuset_common_file_read, |
1406 | .write = cpuset_common_file_write, | |
825a46af PJ |
1407 | .private = FILE_SPREAD_SLAB, |
1408 | }; | |
1409 | ||
8793d854 | 1410 | static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
1da177e4 LT |
1411 | { |
1412 | int err; | |
1413 | ||
8793d854 | 1414 | if ((err = cgroup_add_file(cont, ss, &cft_cpus)) < 0) |
1da177e4 | 1415 | return err; |
8793d854 | 1416 | if ((err = cgroup_add_file(cont, ss, &cft_mems)) < 0) |
1da177e4 | 1417 | return err; |
8793d854 | 1418 | if ((err = cgroup_add_file(cont, ss, &cft_cpu_exclusive)) < 0) |
1da177e4 | 1419 | return err; |
8793d854 | 1420 | if ((err = cgroup_add_file(cont, ss, &cft_mem_exclusive)) < 0) |
1da177e4 | 1421 | return err; |
8793d854 | 1422 | if ((err = cgroup_add_file(cont, ss, &cft_memory_migrate)) < 0) |
1da177e4 | 1423 | return err; |
029190c5 PJ |
1424 | if ((err = cgroup_add_file(cont, ss, &cft_sched_load_balance)) < 0) |
1425 | return err; | |
8793d854 | 1426 | if ((err = cgroup_add_file(cont, ss, &cft_memory_pressure)) < 0) |
45b07ef3 | 1427 | return err; |
8793d854 | 1428 | if ((err = cgroup_add_file(cont, ss, &cft_spread_page)) < 0) |
3e0d98b9 | 1429 | return err; |
8793d854 | 1430 | if ((err = cgroup_add_file(cont, ss, &cft_spread_slab)) < 0) |
1da177e4 | 1431 | return err; |
8793d854 PM |
1432 | /* memory_pressure_enabled is in root cpuset only */ |
1433 | if (err == 0 && !cont->parent) | |
1434 | err = cgroup_add_file(cont, ss, | |
1435 | &cft_memory_pressure_enabled); | |
1da177e4 LT |
1436 | return 0; |
1437 | } | |
1438 | ||
8793d854 PM |
1439 | /* |
1440 | * post_clone() is called at the end of cgroup_clone(). | |
1441 | * 'cgroup' was just created automatically as a result of | |
1442 | * a cgroup_clone(), and the current task is about to | |
1443 | * be moved into 'cgroup'. | |
1444 | * | |
1445 | * Currently we refuse to set up the cgroup - thereby | |
1446 | * refusing the task to be entered, and as a result refusing | |
1447 | * the sys_unshare() or clone() which initiated it - if any | |
1448 | * sibling cpusets have exclusive cpus or mem. | |
1449 | * | |
1450 | * If this becomes a problem for some users who wish to | |
1451 | * allow that scenario, then cpuset_post_clone() could be | |
1452 | * changed to grant parent->cpus_allowed-sibling_cpus_exclusive | |
1453 | * (and likewise for mems) to the new cgroup. | |
1454 | */ | |
1455 | static void cpuset_post_clone(struct cgroup_subsys *ss, | |
1456 | struct cgroup *cgroup) | |
1457 | { | |
1458 | struct cgroup *parent, *child; | |
1459 | struct cpuset *cs, *parent_cs; | |
1460 | ||
1461 | parent = cgroup->parent; | |
1462 | list_for_each_entry(child, &parent->children, sibling) { | |
1463 | cs = cgroup_cs(child); | |
1464 | if (is_mem_exclusive(cs) || is_cpu_exclusive(cs)) | |
1465 | return; | |
1466 | } | |
1467 | cs = cgroup_cs(cgroup); | |
1468 | parent_cs = cgroup_cs(parent); | |
1469 | ||
1470 | cs->mems_allowed = parent_cs->mems_allowed; | |
1471 | cs->cpus_allowed = parent_cs->cpus_allowed; | |
1472 | return; | |
1473 | } | |
1474 | ||
1da177e4 LT |
1475 | /* |
1476 | * cpuset_create - create a cpuset | |
1477 | * parent: cpuset that will be parent of the new cpuset. | |
1478 | * name: name of the new cpuset. Will be strcpy'ed. | |
1479 | * mode: mode to set on new inode | |
1480 | * | |
3d3f26a7 | 1481 | * Must be called with the mutex on the parent inode held |
1da177e4 LT |
1482 | */ |
1483 | ||
8793d854 PM |
1484 | static struct cgroup_subsys_state *cpuset_create( |
1485 | struct cgroup_subsys *ss, | |
1486 | struct cgroup *cont) | |
1da177e4 LT |
1487 | { |
1488 | struct cpuset *cs; | |
8793d854 | 1489 | struct cpuset *parent; |
1da177e4 | 1490 | |
8793d854 PM |
1491 | if (!cont->parent) { |
1492 | /* This is early initialization for the top cgroup */ | |
1493 | top_cpuset.mems_generation = cpuset_mems_generation++; | |
1494 | return &top_cpuset.css; | |
1495 | } | |
1496 | parent = cgroup_cs(cont->parent); | |
1da177e4 LT |
1497 | cs = kmalloc(sizeof(*cs), GFP_KERNEL); |
1498 | if (!cs) | |
8793d854 | 1499 | return ERR_PTR(-ENOMEM); |
1da177e4 | 1500 | |
cf2a473c | 1501 | cpuset_update_task_memory_state(); |
1da177e4 | 1502 | cs->flags = 0; |
825a46af PJ |
1503 | if (is_spread_page(parent)) |
1504 | set_bit(CS_SPREAD_PAGE, &cs->flags); | |
1505 | if (is_spread_slab(parent)) | |
1506 | set_bit(CS_SPREAD_SLAB, &cs->flags); | |
029190c5 | 1507 | set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
1da177e4 LT |
1508 | cs->cpus_allowed = CPU_MASK_NONE; |
1509 | cs->mems_allowed = NODE_MASK_NONE; | |
151a4420 | 1510 | cs->mems_generation = cpuset_mems_generation++; |
3e0d98b9 | 1511 | fmeter_init(&cs->fmeter); |
1da177e4 LT |
1512 | |
1513 | cs->parent = parent; | |
202f72d5 | 1514 | number_of_cpusets++; |
8793d854 | 1515 | return &cs->css ; |
1da177e4 LT |
1516 | } |
1517 | ||
029190c5 PJ |
1518 | /* |
1519 | * Locking note on the strange update_flag() call below: | |
1520 | * | |
1521 | * If the cpuset being removed has its flag 'sched_load_balance' | |
1522 | * enabled, then simulate turning sched_load_balance off, which | |
1523 | * will call rebuild_sched_domains(). The lock_cpu_hotplug() | |
1524 | * call in rebuild_sched_domains() must not be made while holding | |
1525 | * callback_mutex. Elsewhere the kernel nests callback_mutex inside | |
1526 | * lock_cpu_hotplug() calls. So the reverse nesting would risk an | |
1527 | * ABBA deadlock. | |
1528 | */ | |
1529 | ||
8793d854 | 1530 | static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont) |
1da177e4 | 1531 | { |
8793d854 | 1532 | struct cpuset *cs = cgroup_cs(cont); |
1da177e4 | 1533 | |
cf2a473c | 1534 | cpuset_update_task_memory_state(); |
029190c5 PJ |
1535 | |
1536 | if (is_sched_load_balance(cs)) | |
1537 | update_flag(CS_SCHED_LOAD_BALANCE, cs, "0"); | |
1538 | ||
202f72d5 | 1539 | number_of_cpusets--; |
8793d854 | 1540 | kfree(cs); |
1da177e4 LT |
1541 | } |
1542 | ||
8793d854 PM |
1543 | struct cgroup_subsys cpuset_subsys = { |
1544 | .name = "cpuset", | |
1545 | .create = cpuset_create, | |
1546 | .destroy = cpuset_destroy, | |
1547 | .can_attach = cpuset_can_attach, | |
1548 | .attach = cpuset_attach, | |
1549 | .populate = cpuset_populate, | |
1550 | .post_clone = cpuset_post_clone, | |
1551 | .subsys_id = cpuset_subsys_id, | |
1552 | .early_init = 1, | |
1553 | }; | |
1554 | ||
c417f024 PJ |
1555 | /* |
1556 | * cpuset_init_early - just enough so that the calls to | |
1557 | * cpuset_update_task_memory_state() in early init code | |
1558 | * are harmless. | |
1559 | */ | |
1560 | ||
1561 | int __init cpuset_init_early(void) | |
1562 | { | |
8793d854 | 1563 | top_cpuset.mems_generation = cpuset_mems_generation++; |
c417f024 PJ |
1564 | return 0; |
1565 | } | |
1566 | ||
8793d854 | 1567 | |
1da177e4 LT |
1568 | /** |
1569 | * cpuset_init - initialize cpusets at system boot | |
1570 | * | |
1571 | * Description: Initialize top_cpuset and the cpuset internal file system, | |
1572 | **/ | |
1573 | ||
1574 | int __init cpuset_init(void) | |
1575 | { | |
8793d854 | 1576 | int err = 0; |
1da177e4 LT |
1577 | |
1578 | top_cpuset.cpus_allowed = CPU_MASK_ALL; | |
1579 | top_cpuset.mems_allowed = NODE_MASK_ALL; | |
1580 | ||
3e0d98b9 | 1581 | fmeter_init(&top_cpuset.fmeter); |
151a4420 | 1582 | top_cpuset.mems_generation = cpuset_mems_generation++; |
029190c5 | 1583 | set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); |
1da177e4 | 1584 | |
1da177e4 LT |
1585 | err = register_filesystem(&cpuset_fs_type); |
1586 | if (err < 0) | |
8793d854 PM |
1587 | return err; |
1588 | ||
202f72d5 | 1589 | number_of_cpusets = 1; |
8793d854 | 1590 | return 0; |
1da177e4 LT |
1591 | } |
1592 | ||
b1aac8bb PJ |
1593 | /* |
1594 | * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs | |
1595 | * or memory nodes, we need to walk over the cpuset hierarchy, | |
1596 | * removing that CPU or node from all cpusets. If this removes the | |
1597 | * last CPU or node from a cpuset, then the guarantee_online_cpus() | |
1598 | * or guarantee_online_mems() code will use that emptied cpusets | |
1599 | * parent online CPUs or nodes. Cpusets that were already empty of | |
1600 | * CPUs or nodes are left empty. | |
1601 | * | |
1602 | * This routine is intentionally inefficient in a couple of regards. | |
1603 | * It will check all cpusets in a subtree even if the top cpuset of | |
1604 | * the subtree has no offline CPUs or nodes. It checks both CPUs and | |
1605 | * nodes, even though the caller could have been coded to know that | |
1606 | * only one of CPUs or nodes needed to be checked on a given call. | |
1607 | * This was done to minimize text size rather than cpu cycles. | |
1608 | * | |
1609 | * Call with both manage_mutex and callback_mutex held. | |
1610 | * | |
1611 | * Recursive, on depth of cpuset subtree. | |
1612 | */ | |
1613 | ||
1614 | static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur) | |
1615 | { | |
8793d854 | 1616 | struct cgroup *cont; |
b1aac8bb PJ |
1617 | struct cpuset *c; |
1618 | ||
1619 | /* Each of our child cpusets mems must be online */ | |
8793d854 PM |
1620 | list_for_each_entry(cont, &cur->css.cgroup->children, sibling) { |
1621 | c = cgroup_cs(cont); | |
b1aac8bb PJ |
1622 | guarantee_online_cpus_mems_in_subtree(c); |
1623 | if (!cpus_empty(c->cpus_allowed)) | |
1624 | guarantee_online_cpus(c, &c->cpus_allowed); | |
1625 | if (!nodes_empty(c->mems_allowed)) | |
1626 | guarantee_online_mems(c, &c->mems_allowed); | |
1627 | } | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track | |
0e1e7c7a CL |
1632 | * cpu_online_map and node_states[N_HIGH_MEMORY]. Force the top cpuset to |
1633 | * track what's online after any CPU or memory node hotplug or unplug | |
1634 | * event. | |
b1aac8bb PJ |
1635 | * |
1636 | * To ensure that we don't remove a CPU or node from the top cpuset | |
1637 | * that is currently in use by a child cpuset (which would violate | |
1638 | * the rule that cpusets must be subsets of their parent), we first | |
1639 | * call the recursive routine guarantee_online_cpus_mems_in_subtree(). | |
1640 | * | |
1641 | * Since there are two callers of this routine, one for CPU hotplug | |
1642 | * events and one for memory node hotplug events, we could have coded | |
1643 | * two separate routines here. We code it as a single common routine | |
1644 | * in order to minimize text size. | |
1645 | */ | |
1646 | ||
1647 | static void common_cpu_mem_hotplug_unplug(void) | |
1648 | { | |
8793d854 | 1649 | cgroup_lock(); |
b1aac8bb PJ |
1650 | mutex_lock(&callback_mutex); |
1651 | ||
1652 | guarantee_online_cpus_mems_in_subtree(&top_cpuset); | |
1653 | top_cpuset.cpus_allowed = cpu_online_map; | |
0e1e7c7a | 1654 | top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY]; |
b1aac8bb PJ |
1655 | |
1656 | mutex_unlock(&callback_mutex); | |
8793d854 | 1657 | cgroup_unlock(); |
b1aac8bb | 1658 | } |
b1aac8bb | 1659 | |
4c4d50f7 PJ |
1660 | /* |
1661 | * The top_cpuset tracks what CPUs and Memory Nodes are online, | |
1662 | * period. This is necessary in order to make cpusets transparent | |
1663 | * (of no affect) on systems that are actively using CPU hotplug | |
1664 | * but making no active use of cpusets. | |
1665 | * | |
38837fc7 PJ |
1666 | * This routine ensures that top_cpuset.cpus_allowed tracks |
1667 | * cpu_online_map on each CPU hotplug (cpuhp) event. | |
4c4d50f7 PJ |
1668 | */ |
1669 | ||
029190c5 PJ |
1670 | static int cpuset_handle_cpuhp(struct notifier_block *unused_nb, |
1671 | unsigned long phase, void *unused_cpu) | |
4c4d50f7 | 1672 | { |
ac076758 AK |
1673 | if (phase == CPU_DYING || phase == CPU_DYING_FROZEN) |
1674 | return NOTIFY_DONE; | |
1675 | ||
b1aac8bb | 1676 | common_cpu_mem_hotplug_unplug(); |
4c4d50f7 PJ |
1677 | return 0; |
1678 | } | |
4c4d50f7 | 1679 | |
b1aac8bb | 1680 | #ifdef CONFIG_MEMORY_HOTPLUG |
38837fc7 | 1681 | /* |
0e1e7c7a CL |
1682 | * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY]. |
1683 | * Call this routine anytime after you change | |
1684 | * node_states[N_HIGH_MEMORY]. | |
38837fc7 PJ |
1685 | * See also the previous routine cpuset_handle_cpuhp(). |
1686 | */ | |
1687 | ||
1af98928 | 1688 | void cpuset_track_online_nodes(void) |
38837fc7 | 1689 | { |
b1aac8bb | 1690 | common_cpu_mem_hotplug_unplug(); |
38837fc7 PJ |
1691 | } |
1692 | #endif | |
1693 | ||
1da177e4 LT |
1694 | /** |
1695 | * cpuset_init_smp - initialize cpus_allowed | |
1696 | * | |
1697 | * Description: Finish top cpuset after cpu, node maps are initialized | |
1698 | **/ | |
1699 | ||
1700 | void __init cpuset_init_smp(void) | |
1701 | { | |
1702 | top_cpuset.cpus_allowed = cpu_online_map; | |
0e1e7c7a | 1703 | top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY]; |
4c4d50f7 PJ |
1704 | |
1705 | hotcpu_notifier(cpuset_handle_cpuhp, 0); | |
1da177e4 LT |
1706 | } |
1707 | ||
1708 | /** | |
3077a260 | 1709 | |
1da177e4 LT |
1710 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
1711 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. | |
1712 | * | |
1713 | * Description: Returns the cpumask_t cpus_allowed of the cpuset | |
1714 | * attached to the specified @tsk. Guaranteed to return some non-empty | |
1715 | * subset of cpu_online_map, even if this means going outside the | |
1716 | * tasks cpuset. | |
1717 | **/ | |
1718 | ||
909d75a3 | 1719 | cpumask_t cpuset_cpus_allowed(struct task_struct *tsk) |
1da177e4 LT |
1720 | { |
1721 | cpumask_t mask; | |
1722 | ||
3d3f26a7 | 1723 | mutex_lock(&callback_mutex); |
909d75a3 | 1724 | task_lock(tsk); |
8793d854 | 1725 | guarantee_online_cpus(task_cs(tsk), &mask); |
909d75a3 | 1726 | task_unlock(tsk); |
3d3f26a7 | 1727 | mutex_unlock(&callback_mutex); |
1da177e4 LT |
1728 | |
1729 | return mask; | |
1730 | } | |
1731 | ||
1732 | void cpuset_init_current_mems_allowed(void) | |
1733 | { | |
1734 | current->mems_allowed = NODE_MASK_ALL; | |
1735 | } | |
1736 | ||
909d75a3 PJ |
1737 | /** |
1738 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. | |
1739 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. | |
1740 | * | |
1741 | * Description: Returns the nodemask_t mems_allowed of the cpuset | |
1742 | * attached to the specified @tsk. Guaranteed to return some non-empty | |
0e1e7c7a | 1743 | * subset of node_states[N_HIGH_MEMORY], even if this means going outside the |
909d75a3 PJ |
1744 | * tasks cpuset. |
1745 | **/ | |
1746 | ||
1747 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) | |
1748 | { | |
1749 | nodemask_t mask; | |
1750 | ||
3d3f26a7 | 1751 | mutex_lock(&callback_mutex); |
909d75a3 | 1752 | task_lock(tsk); |
8793d854 | 1753 | guarantee_online_mems(task_cs(tsk), &mask); |
909d75a3 | 1754 | task_unlock(tsk); |
3d3f26a7 | 1755 | mutex_unlock(&callback_mutex); |
909d75a3 PJ |
1756 | |
1757 | return mask; | |
1758 | } | |
1759 | ||
d9fd8a6d RD |
1760 | /** |
1761 | * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed | |
1762 | * @zl: the zonelist to be checked | |
1763 | * | |
1da177e4 LT |
1764 | * Are any of the nodes on zonelist zl allowed in current->mems_allowed? |
1765 | */ | |
1766 | int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl) | |
1767 | { | |
1768 | int i; | |
1769 | ||
1770 | for (i = 0; zl->zones[i]; i++) { | |
89fa3024 | 1771 | int nid = zone_to_nid(zl->zones[i]); |
1da177e4 LT |
1772 | |
1773 | if (node_isset(nid, current->mems_allowed)) | |
1774 | return 1; | |
1775 | } | |
1776 | return 0; | |
1777 | } | |
1778 | ||
9bf2229f PJ |
1779 | /* |
1780 | * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive | |
3d3f26a7 | 1781 | * ancestor to the specified cpuset. Call holding callback_mutex. |
9bf2229f PJ |
1782 | * If no ancestor is mem_exclusive (an unusual configuration), then |
1783 | * returns the root cpuset. | |
1784 | */ | |
1785 | static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs) | |
1786 | { | |
1787 | while (!is_mem_exclusive(cs) && cs->parent) | |
1788 | cs = cs->parent; | |
1789 | return cs; | |
1790 | } | |
1791 | ||
d9fd8a6d | 1792 | /** |
02a0e53d | 1793 | * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node? |
9bf2229f | 1794 | * @z: is this zone on an allowed node? |
02a0e53d | 1795 | * @gfp_mask: memory allocation flags |
d9fd8a6d | 1796 | * |
02a0e53d PJ |
1797 | * If we're in interrupt, yes, we can always allocate. If |
1798 | * __GFP_THISNODE is set, yes, we can always allocate. If zone | |
9bf2229f PJ |
1799 | * z's node is in our tasks mems_allowed, yes. If it's not a |
1800 | * __GFP_HARDWALL request and this zone's nodes is in the nearest | |
1801 | * mem_exclusive cpuset ancestor to this tasks cpuset, yes. | |
c596d9f3 DR |
1802 | * If the task has been OOM killed and has access to memory reserves |
1803 | * as specified by the TIF_MEMDIE flag, yes. | |
9bf2229f PJ |
1804 | * Otherwise, no. |
1805 | * | |
02a0e53d PJ |
1806 | * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall() |
1807 | * reduces to cpuset_zone_allowed_hardwall(). Otherwise, | |
1808 | * cpuset_zone_allowed_softwall() might sleep, and might allow a zone | |
1809 | * from an enclosing cpuset. | |
1810 | * | |
1811 | * cpuset_zone_allowed_hardwall() only handles the simpler case of | |
1812 | * hardwall cpusets, and never sleeps. | |
1813 | * | |
1814 | * The __GFP_THISNODE placement logic is really handled elsewhere, | |
1815 | * by forcibly using a zonelist starting at a specified node, and by | |
1816 | * (in get_page_from_freelist()) refusing to consider the zones for | |
1817 | * any node on the zonelist except the first. By the time any such | |
1818 | * calls get to this routine, we should just shut up and say 'yes'. | |
1819 | * | |
9bf2229f | 1820 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
c596d9f3 DR |
1821 | * and do not allow allocations outside the current tasks cpuset |
1822 | * unless the task has been OOM killed as is marked TIF_MEMDIE. | |
9bf2229f | 1823 | * GFP_KERNEL allocations are not so marked, so can escape to the |
02a0e53d | 1824 | * nearest enclosing mem_exclusive ancestor cpuset. |
9bf2229f | 1825 | * |
02a0e53d PJ |
1826 | * Scanning up parent cpusets requires callback_mutex. The |
1827 | * __alloc_pages() routine only calls here with __GFP_HARDWALL bit | |
1828 | * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the | |
1829 | * current tasks mems_allowed came up empty on the first pass over | |
1830 | * the zonelist. So only GFP_KERNEL allocations, if all nodes in the | |
1831 | * cpuset are short of memory, might require taking the callback_mutex | |
1832 | * mutex. | |
9bf2229f | 1833 | * |
36be57ff | 1834 | * The first call here from mm/page_alloc:get_page_from_freelist() |
02a0e53d PJ |
1835 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, |
1836 | * so no allocation on a node outside the cpuset is allowed (unless | |
1837 | * in interrupt, of course). | |
36be57ff PJ |
1838 | * |
1839 | * The second pass through get_page_from_freelist() doesn't even call | |
1840 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() | |
1841 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set | |
1842 | * in alloc_flags. That logic and the checks below have the combined | |
1843 | * affect that: | |
9bf2229f PJ |
1844 | * in_interrupt - any node ok (current task context irrelevant) |
1845 | * GFP_ATOMIC - any node ok | |
c596d9f3 | 1846 | * TIF_MEMDIE - any node ok |
9bf2229f PJ |
1847 | * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok |
1848 | * GFP_USER - only nodes in current tasks mems allowed ok. | |
36be57ff PJ |
1849 | * |
1850 | * Rule: | |
02a0e53d | 1851 | * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you |
36be57ff PJ |
1852 | * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables |
1853 | * the code that might scan up ancestor cpusets and sleep. | |
02a0e53d | 1854 | */ |
9bf2229f | 1855 | |
02a0e53d | 1856 | int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask) |
1da177e4 | 1857 | { |
9bf2229f PJ |
1858 | int node; /* node that zone z is on */ |
1859 | const struct cpuset *cs; /* current cpuset ancestors */ | |
29afd49b | 1860 | int allowed; /* is allocation in zone z allowed? */ |
9bf2229f | 1861 | |
9b819d20 | 1862 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
9bf2229f | 1863 | return 1; |
89fa3024 | 1864 | node = zone_to_nid(z); |
92d1dbd2 | 1865 | might_sleep_if(!(gfp_mask & __GFP_HARDWALL)); |
9bf2229f PJ |
1866 | if (node_isset(node, current->mems_allowed)) |
1867 | return 1; | |
c596d9f3 DR |
1868 | /* |
1869 | * Allow tasks that have access to memory reserves because they have | |
1870 | * been OOM killed to get memory anywhere. | |
1871 | */ | |
1872 | if (unlikely(test_thread_flag(TIF_MEMDIE))) | |
1873 | return 1; | |
9bf2229f PJ |
1874 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
1875 | return 0; | |
1876 | ||
5563e770 BP |
1877 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
1878 | return 1; | |
1879 | ||
9bf2229f | 1880 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
3d3f26a7 | 1881 | mutex_lock(&callback_mutex); |
053199ed | 1882 | |
053199ed | 1883 | task_lock(current); |
8793d854 | 1884 | cs = nearest_exclusive_ancestor(task_cs(current)); |
053199ed PJ |
1885 | task_unlock(current); |
1886 | ||
9bf2229f | 1887 | allowed = node_isset(node, cs->mems_allowed); |
3d3f26a7 | 1888 | mutex_unlock(&callback_mutex); |
9bf2229f | 1889 | return allowed; |
1da177e4 LT |
1890 | } |
1891 | ||
02a0e53d PJ |
1892 | /* |
1893 | * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node? | |
1894 | * @z: is this zone on an allowed node? | |
1895 | * @gfp_mask: memory allocation flags | |
1896 | * | |
1897 | * If we're in interrupt, yes, we can always allocate. | |
1898 | * If __GFP_THISNODE is set, yes, we can always allocate. If zone | |
c596d9f3 DR |
1899 | * z's node is in our tasks mems_allowed, yes. If the task has been |
1900 | * OOM killed and has access to memory reserves as specified by the | |
1901 | * TIF_MEMDIE flag, yes. Otherwise, no. | |
02a0e53d PJ |
1902 | * |
1903 | * The __GFP_THISNODE placement logic is really handled elsewhere, | |
1904 | * by forcibly using a zonelist starting at a specified node, and by | |
1905 | * (in get_page_from_freelist()) refusing to consider the zones for | |
1906 | * any node on the zonelist except the first. By the time any such | |
1907 | * calls get to this routine, we should just shut up and say 'yes'. | |
1908 | * | |
1909 | * Unlike the cpuset_zone_allowed_softwall() variant, above, | |
1910 | * this variant requires that the zone be in the current tasks | |
1911 | * mems_allowed or that we're in interrupt. It does not scan up the | |
1912 | * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset. | |
1913 | * It never sleeps. | |
1914 | */ | |
1915 | ||
1916 | int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask) | |
1917 | { | |
1918 | int node; /* node that zone z is on */ | |
1919 | ||
1920 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) | |
1921 | return 1; | |
1922 | node = zone_to_nid(z); | |
1923 | if (node_isset(node, current->mems_allowed)) | |
1924 | return 1; | |
dedf8b79 DW |
1925 | /* |
1926 | * Allow tasks that have access to memory reserves because they have | |
1927 | * been OOM killed to get memory anywhere. | |
1928 | */ | |
1929 | if (unlikely(test_thread_flag(TIF_MEMDIE))) | |
1930 | return 1; | |
02a0e53d PJ |
1931 | return 0; |
1932 | } | |
1933 | ||
505970b9 PJ |
1934 | /** |
1935 | * cpuset_lock - lock out any changes to cpuset structures | |
1936 | * | |
3d3f26a7 | 1937 | * The out of memory (oom) code needs to mutex_lock cpusets |
505970b9 | 1938 | * from being changed while it scans the tasklist looking for a |
3d3f26a7 | 1939 | * task in an overlapping cpuset. Expose callback_mutex via this |
505970b9 PJ |
1940 | * cpuset_lock() routine, so the oom code can lock it, before |
1941 | * locking the task list. The tasklist_lock is a spinlock, so | |
3d3f26a7 | 1942 | * must be taken inside callback_mutex. |
505970b9 PJ |
1943 | */ |
1944 | ||
1945 | void cpuset_lock(void) | |
1946 | { | |
3d3f26a7 | 1947 | mutex_lock(&callback_mutex); |
505970b9 PJ |
1948 | } |
1949 | ||
1950 | /** | |
1951 | * cpuset_unlock - release lock on cpuset changes | |
1952 | * | |
1953 | * Undo the lock taken in a previous cpuset_lock() call. | |
1954 | */ | |
1955 | ||
1956 | void cpuset_unlock(void) | |
1957 | { | |
3d3f26a7 | 1958 | mutex_unlock(&callback_mutex); |
505970b9 PJ |
1959 | } |
1960 | ||
825a46af PJ |
1961 | /** |
1962 | * cpuset_mem_spread_node() - On which node to begin search for a page | |
1963 | * | |
1964 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for | |
1965 | * tasks in a cpuset with is_spread_page or is_spread_slab set), | |
1966 | * and if the memory allocation used cpuset_mem_spread_node() | |
1967 | * to determine on which node to start looking, as it will for | |
1968 | * certain page cache or slab cache pages such as used for file | |
1969 | * system buffers and inode caches, then instead of starting on the | |
1970 | * local node to look for a free page, rather spread the starting | |
1971 | * node around the tasks mems_allowed nodes. | |
1972 | * | |
1973 | * We don't have to worry about the returned node being offline | |
1974 | * because "it can't happen", and even if it did, it would be ok. | |
1975 | * | |
1976 | * The routines calling guarantee_online_mems() are careful to | |
1977 | * only set nodes in task->mems_allowed that are online. So it | |
1978 | * should not be possible for the following code to return an | |
1979 | * offline node. But if it did, that would be ok, as this routine | |
1980 | * is not returning the node where the allocation must be, only | |
1981 | * the node where the search should start. The zonelist passed to | |
1982 | * __alloc_pages() will include all nodes. If the slab allocator | |
1983 | * is passed an offline node, it will fall back to the local node. | |
1984 | * See kmem_cache_alloc_node(). | |
1985 | */ | |
1986 | ||
1987 | int cpuset_mem_spread_node(void) | |
1988 | { | |
1989 | int node; | |
1990 | ||
1991 | node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed); | |
1992 | if (node == MAX_NUMNODES) | |
1993 | node = first_node(current->mems_allowed); | |
1994 | current->cpuset_mem_spread_rotor = node; | |
1995 | return node; | |
1996 | } | |
1997 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); | |
1998 | ||
ef08e3b4 | 1999 | /** |
bbe373f2 DR |
2000 | * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? |
2001 | * @tsk1: pointer to task_struct of some task. | |
2002 | * @tsk2: pointer to task_struct of some other task. | |
2003 | * | |
2004 | * Description: Return true if @tsk1's mems_allowed intersects the | |
2005 | * mems_allowed of @tsk2. Used by the OOM killer to determine if | |
2006 | * one of the task's memory usage might impact the memory available | |
2007 | * to the other. | |
ef08e3b4 PJ |
2008 | **/ |
2009 | ||
bbe373f2 DR |
2010 | int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, |
2011 | const struct task_struct *tsk2) | |
ef08e3b4 | 2012 | { |
bbe373f2 | 2013 | return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); |
ef08e3b4 PJ |
2014 | } |
2015 | ||
3e0d98b9 PJ |
2016 | /* |
2017 | * Collection of memory_pressure is suppressed unless | |
2018 | * this flag is enabled by writing "1" to the special | |
2019 | * cpuset file 'memory_pressure_enabled' in the root cpuset. | |
2020 | */ | |
2021 | ||
c5b2aff8 | 2022 | int cpuset_memory_pressure_enabled __read_mostly; |
3e0d98b9 PJ |
2023 | |
2024 | /** | |
2025 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. | |
2026 | * | |
2027 | * Keep a running average of the rate of synchronous (direct) | |
2028 | * page reclaim efforts initiated by tasks in each cpuset. | |
2029 | * | |
2030 | * This represents the rate at which some task in the cpuset | |
2031 | * ran low on memory on all nodes it was allowed to use, and | |
2032 | * had to enter the kernels page reclaim code in an effort to | |
2033 | * create more free memory by tossing clean pages or swapping | |
2034 | * or writing dirty pages. | |
2035 | * | |
2036 | * Display to user space in the per-cpuset read-only file | |
2037 | * "memory_pressure". Value displayed is an integer | |
2038 | * representing the recent rate of entry into the synchronous | |
2039 | * (direct) page reclaim by any task attached to the cpuset. | |
2040 | **/ | |
2041 | ||
2042 | void __cpuset_memory_pressure_bump(void) | |
2043 | { | |
3e0d98b9 | 2044 | task_lock(current); |
8793d854 | 2045 | fmeter_markevent(&task_cs(current)->fmeter); |
3e0d98b9 PJ |
2046 | task_unlock(current); |
2047 | } | |
2048 | ||
8793d854 | 2049 | #ifdef CONFIG_PROC_PID_CPUSET |
1da177e4 LT |
2050 | /* |
2051 | * proc_cpuset_show() | |
2052 | * - Print tasks cpuset path into seq_file. | |
2053 | * - Used for /proc/<pid>/cpuset. | |
053199ed PJ |
2054 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
2055 | * doesn't really matter if tsk->cpuset changes after we read it, | |
3d3f26a7 | 2056 | * and we take manage_mutex, keeping attach_task() from changing it |
8488bc35 PJ |
2057 | * anyway. No need to check that tsk->cpuset != NULL, thanks to |
2058 | * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks | |
2059 | * cpuset to top_cpuset. | |
1da177e4 | 2060 | */ |
029190c5 | 2061 | static int proc_cpuset_show(struct seq_file *m, void *unused_v) |
1da177e4 | 2062 | { |
13b41b09 | 2063 | struct pid *pid; |
1da177e4 LT |
2064 | struct task_struct *tsk; |
2065 | char *buf; | |
8793d854 | 2066 | struct cgroup_subsys_state *css; |
99f89551 | 2067 | int retval; |
1da177e4 | 2068 | |
99f89551 | 2069 | retval = -ENOMEM; |
1da177e4 LT |
2070 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
2071 | if (!buf) | |
99f89551 EB |
2072 | goto out; |
2073 | ||
2074 | retval = -ESRCH; | |
13b41b09 EB |
2075 | pid = m->private; |
2076 | tsk = get_pid_task(pid, PIDTYPE_PID); | |
99f89551 EB |
2077 | if (!tsk) |
2078 | goto out_free; | |
1da177e4 | 2079 | |
99f89551 | 2080 | retval = -EINVAL; |
8793d854 PM |
2081 | cgroup_lock(); |
2082 | css = task_subsys_state(tsk, cpuset_subsys_id); | |
2083 | retval = cgroup_path(css->cgroup, buf, PAGE_SIZE); | |
1da177e4 | 2084 | if (retval < 0) |
99f89551 | 2085 | goto out_unlock; |
1da177e4 LT |
2086 | seq_puts(m, buf); |
2087 | seq_putc(m, '\n'); | |
99f89551 | 2088 | out_unlock: |
8793d854 | 2089 | cgroup_unlock(); |
99f89551 EB |
2090 | put_task_struct(tsk); |
2091 | out_free: | |
1da177e4 | 2092 | kfree(buf); |
99f89551 | 2093 | out: |
1da177e4 LT |
2094 | return retval; |
2095 | } | |
2096 | ||
2097 | static int cpuset_open(struct inode *inode, struct file *file) | |
2098 | { | |
13b41b09 EB |
2099 | struct pid *pid = PROC_I(inode)->pid; |
2100 | return single_open(file, proc_cpuset_show, pid); | |
1da177e4 LT |
2101 | } |
2102 | ||
9a32144e | 2103 | const struct file_operations proc_cpuset_operations = { |
1da177e4 LT |
2104 | .open = cpuset_open, |
2105 | .read = seq_read, | |
2106 | .llseek = seq_lseek, | |
2107 | .release = single_release, | |
2108 | }; | |
8793d854 | 2109 | #endif /* CONFIG_PROC_PID_CPUSET */ |
1da177e4 LT |
2110 | |
2111 | /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ | |
2112 | char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) | |
2113 | { | |
2114 | buffer += sprintf(buffer, "Cpus_allowed:\t"); | |
2115 | buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed); | |
2116 | buffer += sprintf(buffer, "\n"); | |
2117 | buffer += sprintf(buffer, "Mems_allowed:\t"); | |
2118 | buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed); | |
2119 | buffer += sprintf(buffer, "\n"); | |
2120 | return buffer; | |
2121 | } |