]>
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 |
cf417141 MK |
17 | * 2008 Rework of the scheduler domains and CPU hotplug handling |
18 | * by Max Krasnyansky | |
1da177e4 LT |
19 | * |
20 | * This file is subject to the terms and conditions of the GNU General Public | |
21 | * License. See the file COPYING in the main directory of the Linux | |
22 | * distribution for more details. | |
23 | */ | |
24 | ||
1da177e4 LT |
25 | #include <linux/cpu.h> |
26 | #include <linux/cpumask.h> | |
27 | #include <linux/cpuset.h> | |
28 | #include <linux/err.h> | |
29 | #include <linux/errno.h> | |
30 | #include <linux/file.h> | |
31 | #include <linux/fs.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/interrupt.h> | |
34 | #include <linux/kernel.h> | |
35 | #include <linux/kmod.h> | |
36 | #include <linux/list.h> | |
68860ec1 | 37 | #include <linux/mempolicy.h> |
1da177e4 | 38 | #include <linux/mm.h> |
f481891f | 39 | #include <linux/memory.h> |
9984de1a | 40 | #include <linux/export.h> |
1da177e4 LT |
41 | #include <linux/mount.h> |
42 | #include <linux/namei.h> | |
43 | #include <linux/pagemap.h> | |
44 | #include <linux/proc_fs.h> | |
6b9c2603 | 45 | #include <linux/rcupdate.h> |
1da177e4 LT |
46 | #include <linux/sched.h> |
47 | #include <linux/seq_file.h> | |
22fb52dd | 48 | #include <linux/security.h> |
1da177e4 | 49 | #include <linux/slab.h> |
1da177e4 LT |
50 | #include <linux/spinlock.h> |
51 | #include <linux/stat.h> | |
52 | #include <linux/string.h> | |
53 | #include <linux/time.h> | |
54 | #include <linux/backing-dev.h> | |
55 | #include <linux/sort.h> | |
56 | ||
57 | #include <asm/uaccess.h> | |
60063497 | 58 | #include <linux/atomic.h> |
3d3f26a7 | 59 | #include <linux/mutex.h> |
956db3ca CW |
60 | #include <linux/workqueue.h> |
61 | #include <linux/cgroup.h> | |
1da177e4 | 62 | |
202f72d5 PJ |
63 | /* |
64 | * Tracks how many cpusets are currently defined in system. | |
65 | * When there is only one cpuset (the root cpuset) we can | |
66 | * short circuit some hooks. | |
67 | */ | |
7edc5962 | 68 | int number_of_cpusets __read_mostly; |
202f72d5 | 69 | |
2df167a3 | 70 | /* Forward declare cgroup structures */ |
8793d854 PM |
71 | struct cgroup_subsys cpuset_subsys; |
72 | struct cpuset; | |
73 | ||
3e0d98b9 PJ |
74 | /* See "Frequency meter" comments, below. */ |
75 | ||
76 | struct fmeter { | |
77 | int cnt; /* unprocessed events count */ | |
78 | int val; /* most recent output value */ | |
79 | time_t time; /* clock (secs) when val computed */ | |
80 | spinlock_t lock; /* guards read or write of above */ | |
81 | }; | |
82 | ||
1da177e4 | 83 | struct cpuset { |
8793d854 PM |
84 | struct cgroup_subsys_state css; |
85 | ||
1da177e4 | 86 | unsigned long flags; /* "unsigned long" so bitops work */ |
300ed6cb | 87 | cpumask_var_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ |
1da177e4 LT |
88 | nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ |
89 | ||
1da177e4 | 90 | struct cpuset *parent; /* my parent */ |
1da177e4 | 91 | |
3e0d98b9 | 92 | struct fmeter fmeter; /* memory_pressure filter */ |
029190c5 | 93 | |
452477fa TH |
94 | /* |
95 | * Tasks are being attached to this cpuset. Used to prevent | |
96 | * zeroing cpus/mems_allowed between ->can_attach() and ->attach(). | |
97 | */ | |
98 | int attach_in_progress; | |
99 | ||
029190c5 PJ |
100 | /* partition number for rebuild_sched_domains() */ |
101 | int pn; | |
956db3ca | 102 | |
1d3504fc HS |
103 | /* for custom sched domain */ |
104 | int relax_domain_level; | |
105 | ||
8d033948 | 106 | struct work_struct hotplug_work; |
1da177e4 LT |
107 | }; |
108 | ||
8793d854 PM |
109 | /* Retrieve the cpuset for a cgroup */ |
110 | static inline struct cpuset *cgroup_cs(struct cgroup *cont) | |
111 | { | |
112 | return container_of(cgroup_subsys_state(cont, cpuset_subsys_id), | |
113 | struct cpuset, css); | |
114 | } | |
115 | ||
116 | /* Retrieve the cpuset for a task */ | |
117 | static inline struct cpuset *task_cs(struct task_struct *task) | |
118 | { | |
119 | return container_of(task_subsys_state(task, cpuset_subsys_id), | |
120 | struct cpuset, css); | |
121 | } | |
8793d854 | 122 | |
b246272e DR |
123 | #ifdef CONFIG_NUMA |
124 | static inline bool task_has_mempolicy(struct task_struct *task) | |
125 | { | |
126 | return task->mempolicy; | |
127 | } | |
128 | #else | |
129 | static inline bool task_has_mempolicy(struct task_struct *task) | |
130 | { | |
131 | return false; | |
132 | } | |
133 | #endif | |
134 | ||
135 | ||
1da177e4 LT |
136 | /* bits in struct cpuset flags field */ |
137 | typedef enum { | |
efeb77b2 | 138 | CS_ONLINE, |
1da177e4 LT |
139 | CS_CPU_EXCLUSIVE, |
140 | CS_MEM_EXCLUSIVE, | |
78608366 | 141 | CS_MEM_HARDWALL, |
45b07ef3 | 142 | CS_MEMORY_MIGRATE, |
029190c5 | 143 | CS_SCHED_LOAD_BALANCE, |
825a46af PJ |
144 | CS_SPREAD_PAGE, |
145 | CS_SPREAD_SLAB, | |
1da177e4 LT |
146 | } cpuset_flagbits_t; |
147 | ||
148 | /* convenient tests for these bits */ | |
efeb77b2 TH |
149 | static inline bool is_cpuset_online(const struct cpuset *cs) |
150 | { | |
151 | return test_bit(CS_ONLINE, &cs->flags); | |
152 | } | |
153 | ||
1da177e4 LT |
154 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
155 | { | |
7b5b9ef0 | 156 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
1da177e4 LT |
157 | } |
158 | ||
159 | static inline int is_mem_exclusive(const struct cpuset *cs) | |
160 | { | |
7b5b9ef0 | 161 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
1da177e4 LT |
162 | } |
163 | ||
78608366 PM |
164 | static inline int is_mem_hardwall(const struct cpuset *cs) |
165 | { | |
166 | return test_bit(CS_MEM_HARDWALL, &cs->flags); | |
167 | } | |
168 | ||
029190c5 PJ |
169 | static inline int is_sched_load_balance(const struct cpuset *cs) |
170 | { | |
171 | return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); | |
172 | } | |
173 | ||
45b07ef3 PJ |
174 | static inline int is_memory_migrate(const struct cpuset *cs) |
175 | { | |
7b5b9ef0 | 176 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
45b07ef3 PJ |
177 | } |
178 | ||
825a46af PJ |
179 | static inline int is_spread_page(const struct cpuset *cs) |
180 | { | |
181 | return test_bit(CS_SPREAD_PAGE, &cs->flags); | |
182 | } | |
183 | ||
184 | static inline int is_spread_slab(const struct cpuset *cs) | |
185 | { | |
186 | return test_bit(CS_SPREAD_SLAB, &cs->flags); | |
187 | } | |
188 | ||
1da177e4 | 189 | static struct cpuset top_cpuset = { |
efeb77b2 TH |
190 | .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) | |
191 | (1 << CS_MEM_EXCLUSIVE)), | |
1da177e4 LT |
192 | }; |
193 | ||
ae8086ce TH |
194 | /** |
195 | * cpuset_for_each_child - traverse online children of a cpuset | |
196 | * @child_cs: loop cursor pointing to the current child | |
197 | * @pos_cgrp: used for iteration | |
198 | * @parent_cs: target cpuset to walk children of | |
199 | * | |
200 | * Walk @child_cs through the online children of @parent_cs. Must be used | |
201 | * with RCU read locked. | |
202 | */ | |
203 | #define cpuset_for_each_child(child_cs, pos_cgrp, parent_cs) \ | |
204 | cgroup_for_each_child((pos_cgrp), (parent_cs)->css.cgroup) \ | |
205 | if (is_cpuset_online(((child_cs) = cgroup_cs((pos_cgrp))))) | |
206 | ||
fc560a26 TH |
207 | /** |
208 | * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants | |
209 | * @des_cs: loop cursor pointing to the current descendant | |
210 | * @pos_cgrp: used for iteration | |
211 | * @root_cs: target cpuset to walk ancestor of | |
212 | * | |
213 | * Walk @des_cs through the online descendants of @root_cs. Must be used | |
214 | * with RCU read locked. The caller may modify @pos_cgrp by calling | |
215 | * cgroup_rightmost_descendant() to skip subtree. | |
216 | */ | |
217 | #define cpuset_for_each_descendant_pre(des_cs, pos_cgrp, root_cs) \ | |
218 | cgroup_for_each_descendant_pre((pos_cgrp), (root_cs)->css.cgroup) \ | |
219 | if (is_cpuset_online(((des_cs) = cgroup_cs((pos_cgrp))))) | |
220 | ||
1da177e4 | 221 | /* |
5d21cc2d TH |
222 | * There are two global mutexes guarding cpuset structures - cpuset_mutex |
223 | * and callback_mutex. The latter may nest inside the former. We also | |
224 | * require taking task_lock() when dereferencing a task's cpuset pointer. | |
225 | * See "The task_lock() exception", at the end of this comment. | |
226 | * | |
227 | * A task must hold both mutexes to modify cpusets. If a task holds | |
228 | * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it | |
229 | * is the only task able to also acquire callback_mutex and be able to | |
230 | * modify cpusets. It can perform various checks on the cpuset structure | |
231 | * first, knowing nothing will change. It can also allocate memory while | |
232 | * just holding cpuset_mutex. While it is performing these checks, various | |
233 | * callback routines can briefly acquire callback_mutex to query cpusets. | |
234 | * Once it is ready to make the changes, it takes callback_mutex, blocking | |
235 | * everyone else. | |
053199ed PJ |
236 | * |
237 | * Calls to the kernel memory allocator can not be made while holding | |
3d3f26a7 | 238 | * callback_mutex, as that would risk double tripping on callback_mutex |
053199ed PJ |
239 | * from one of the callbacks into the cpuset code from within |
240 | * __alloc_pages(). | |
241 | * | |
3d3f26a7 | 242 | * If a task is only holding callback_mutex, then it has read-only |
053199ed PJ |
243 | * access to cpusets. |
244 | * | |
58568d2a MX |
245 | * Now, the task_struct fields mems_allowed and mempolicy may be changed |
246 | * by other task, we use alloc_lock in the task_struct fields to protect | |
247 | * them. | |
053199ed | 248 | * |
3d3f26a7 | 249 | * The cpuset_common_file_read() handlers only hold callback_mutex across |
053199ed PJ |
250 | * small pieces of code, such as when reading out possibly multi-word |
251 | * cpumasks and nodemasks. | |
252 | * | |
2df167a3 PM |
253 | * Accessing a task's cpuset should be done in accordance with the |
254 | * guidelines for accessing subsystem state in kernel/cgroup.c | |
1da177e4 LT |
255 | */ |
256 | ||
5d21cc2d | 257 | static DEFINE_MUTEX(cpuset_mutex); |
3d3f26a7 | 258 | static DEFINE_MUTEX(callback_mutex); |
4247bdc6 | 259 | |
75aa1994 DR |
260 | /* |
261 | * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist | |
262 | * buffers. They are statically allocated to prevent using excess stack | |
263 | * when calling cpuset_print_task_mems_allowed(). | |
264 | */ | |
265 | #define CPUSET_NAME_LEN (128) | |
266 | #define CPUSET_NODELIST_LEN (256) | |
267 | static char cpuset_name[CPUSET_NAME_LEN]; | |
268 | static char cpuset_nodelist[CPUSET_NODELIST_LEN]; | |
269 | static DEFINE_SPINLOCK(cpuset_buffer_lock); | |
270 | ||
3a5a6d0c TH |
271 | /* |
272 | * CPU / memory hotplug is handled asynchronously. | |
273 | */ | |
8d033948 TH |
274 | static struct workqueue_struct *cpuset_propagate_hotplug_wq; |
275 | ||
3a5a6d0c | 276 | static void cpuset_hotplug_workfn(struct work_struct *work); |
8d033948 | 277 | static void cpuset_propagate_hotplug_workfn(struct work_struct *work); |
02bb5863 | 278 | static void schedule_cpuset_propagate_hotplug(struct cpuset *cs); |
3a5a6d0c TH |
279 | |
280 | static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn); | |
281 | ||
cf417141 MK |
282 | /* |
283 | * This is ugly, but preserves the userspace API for existing cpuset | |
8793d854 | 284 | * users. If someone tries to mount the "cpuset" filesystem, we |
cf417141 MK |
285 | * silently switch it to mount "cgroup" instead |
286 | */ | |
f7e83571 AV |
287 | static struct dentry *cpuset_mount(struct file_system_type *fs_type, |
288 | int flags, const char *unused_dev_name, void *data) | |
1da177e4 | 289 | { |
8793d854 | 290 | struct file_system_type *cgroup_fs = get_fs_type("cgroup"); |
f7e83571 | 291 | struct dentry *ret = ERR_PTR(-ENODEV); |
8793d854 PM |
292 | if (cgroup_fs) { |
293 | char mountopts[] = | |
294 | "cpuset,noprefix," | |
295 | "release_agent=/sbin/cpuset_release_agent"; | |
f7e83571 AV |
296 | ret = cgroup_fs->mount(cgroup_fs, flags, |
297 | unused_dev_name, mountopts); | |
8793d854 PM |
298 | put_filesystem(cgroup_fs); |
299 | } | |
300 | return ret; | |
1da177e4 LT |
301 | } |
302 | ||
303 | static struct file_system_type cpuset_fs_type = { | |
304 | .name = "cpuset", | |
f7e83571 | 305 | .mount = cpuset_mount, |
1da177e4 LT |
306 | }; |
307 | ||
1da177e4 | 308 | /* |
300ed6cb | 309 | * Return in pmask the portion of a cpusets's cpus_allowed that |
1da177e4 LT |
310 | * are online. If none are online, walk up the cpuset hierarchy |
311 | * until we find one that does have some online cpus. If we get | |
312 | * all the way to the top and still haven't found any online cpus, | |
5f054e31 RR |
313 | * return cpu_online_mask. Or if passed a NULL cs from an exit'ing |
314 | * task, return cpu_online_mask. | |
1da177e4 LT |
315 | * |
316 | * One way or another, we guarantee to return some non-empty subset | |
5f054e31 | 317 | * of cpu_online_mask. |
1da177e4 | 318 | * |
3d3f26a7 | 319 | * Call with callback_mutex held. |
1da177e4 LT |
320 | */ |
321 | ||
6af866af LZ |
322 | static void guarantee_online_cpus(const struct cpuset *cs, |
323 | struct cpumask *pmask) | |
1da177e4 | 324 | { |
300ed6cb | 325 | while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask)) |
1da177e4 LT |
326 | cs = cs->parent; |
327 | if (cs) | |
300ed6cb | 328 | cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask); |
1da177e4 | 329 | else |
300ed6cb LZ |
330 | cpumask_copy(pmask, cpu_online_mask); |
331 | BUG_ON(!cpumask_intersects(pmask, cpu_online_mask)); | |
1da177e4 LT |
332 | } |
333 | ||
334 | /* | |
335 | * Return in *pmask the portion of a cpusets's mems_allowed that | |
0e1e7c7a CL |
336 | * are online, with memory. If none are online with memory, walk |
337 | * up the cpuset hierarchy until we find one that does have some | |
338 | * online mems. If we get all the way to the top and still haven't | |
38d7bee9 | 339 | * found any online mems, return node_states[N_MEMORY]. |
1da177e4 LT |
340 | * |
341 | * One way or another, we guarantee to return some non-empty subset | |
38d7bee9 | 342 | * of node_states[N_MEMORY]. |
1da177e4 | 343 | * |
3d3f26a7 | 344 | * Call with callback_mutex held. |
1da177e4 LT |
345 | */ |
346 | ||
347 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) | |
348 | { | |
0e1e7c7a | 349 | while (cs && !nodes_intersects(cs->mems_allowed, |
38d7bee9 | 350 | node_states[N_MEMORY])) |
1da177e4 LT |
351 | cs = cs->parent; |
352 | if (cs) | |
0e1e7c7a | 353 | nodes_and(*pmask, cs->mems_allowed, |
38d7bee9 | 354 | node_states[N_MEMORY]); |
1da177e4 | 355 | else |
38d7bee9 LJ |
356 | *pmask = node_states[N_MEMORY]; |
357 | BUG_ON(!nodes_intersects(*pmask, node_states[N_MEMORY])); | |
1da177e4 LT |
358 | } |
359 | ||
f3b39d47 MX |
360 | /* |
361 | * update task's spread flag if cpuset's page/slab spread flag is set | |
362 | * | |
5d21cc2d | 363 | * Called with callback_mutex/cpuset_mutex held |
f3b39d47 MX |
364 | */ |
365 | static void cpuset_update_task_spread_flag(struct cpuset *cs, | |
366 | struct task_struct *tsk) | |
367 | { | |
368 | if (is_spread_page(cs)) | |
369 | tsk->flags |= PF_SPREAD_PAGE; | |
370 | else | |
371 | tsk->flags &= ~PF_SPREAD_PAGE; | |
372 | if (is_spread_slab(cs)) | |
373 | tsk->flags |= PF_SPREAD_SLAB; | |
374 | else | |
375 | tsk->flags &= ~PF_SPREAD_SLAB; | |
376 | } | |
377 | ||
1da177e4 LT |
378 | /* |
379 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? | |
380 | * | |
381 | * One cpuset is a subset of another if all its allowed CPUs and | |
382 | * Memory Nodes are a subset of the other, and its exclusive flags | |
5d21cc2d | 383 | * are only set if the other's are set. Call holding cpuset_mutex. |
1da177e4 LT |
384 | */ |
385 | ||
386 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) | |
387 | { | |
300ed6cb | 388 | return cpumask_subset(p->cpus_allowed, q->cpus_allowed) && |
1da177e4 LT |
389 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
390 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && | |
391 | is_mem_exclusive(p) <= is_mem_exclusive(q); | |
392 | } | |
393 | ||
645fcc9d LZ |
394 | /** |
395 | * alloc_trial_cpuset - allocate a trial cpuset | |
396 | * @cs: the cpuset that the trial cpuset duplicates | |
397 | */ | |
398 | static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs) | |
399 | { | |
300ed6cb LZ |
400 | struct cpuset *trial; |
401 | ||
402 | trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL); | |
403 | if (!trial) | |
404 | return NULL; | |
405 | ||
406 | if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) { | |
407 | kfree(trial); | |
408 | return NULL; | |
409 | } | |
410 | cpumask_copy(trial->cpus_allowed, cs->cpus_allowed); | |
411 | ||
412 | return trial; | |
645fcc9d LZ |
413 | } |
414 | ||
415 | /** | |
416 | * free_trial_cpuset - free the trial cpuset | |
417 | * @trial: the trial cpuset to be freed | |
418 | */ | |
419 | static void free_trial_cpuset(struct cpuset *trial) | |
420 | { | |
300ed6cb | 421 | free_cpumask_var(trial->cpus_allowed); |
645fcc9d LZ |
422 | kfree(trial); |
423 | } | |
424 | ||
1da177e4 LT |
425 | /* |
426 | * validate_change() - Used to validate that any proposed cpuset change | |
427 | * follows the structural rules for cpusets. | |
428 | * | |
429 | * If we replaced the flag and mask values of the current cpuset | |
430 | * (cur) with those values in the trial cpuset (trial), would | |
431 | * our various subset and exclusive rules still be valid? Presumes | |
5d21cc2d | 432 | * cpuset_mutex held. |
1da177e4 LT |
433 | * |
434 | * 'cur' is the address of an actual, in-use cpuset. Operations | |
435 | * such as list traversal that depend on the actual address of the | |
436 | * cpuset in the list must use cur below, not trial. | |
437 | * | |
438 | * 'trial' is the address of bulk structure copy of cur, with | |
439 | * perhaps one or more of the fields cpus_allowed, mems_allowed, | |
440 | * or flags changed to new, trial values. | |
441 | * | |
442 | * Return 0 if valid, -errno if not. | |
443 | */ | |
444 | ||
445 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) | |
446 | { | |
8793d854 | 447 | struct cgroup *cont; |
1da177e4 | 448 | struct cpuset *c, *par; |
ae8086ce TH |
449 | int ret; |
450 | ||
451 | rcu_read_lock(); | |
1da177e4 LT |
452 | |
453 | /* Each of our child cpusets must be a subset of us */ | |
ae8086ce TH |
454 | ret = -EBUSY; |
455 | cpuset_for_each_child(c, cont, cur) | |
456 | if (!is_cpuset_subset(c, trial)) | |
457 | goto out; | |
1da177e4 LT |
458 | |
459 | /* Remaining checks don't apply to root cpuset */ | |
ae8086ce | 460 | ret = 0; |
69604067 | 461 | if (cur == &top_cpuset) |
ae8086ce | 462 | goto out; |
1da177e4 | 463 | |
69604067 PJ |
464 | par = cur->parent; |
465 | ||
1da177e4 | 466 | /* We must be a subset of our parent cpuset */ |
ae8086ce | 467 | ret = -EACCES; |
1da177e4 | 468 | if (!is_cpuset_subset(trial, par)) |
ae8086ce | 469 | goto out; |
1da177e4 | 470 | |
2df167a3 PM |
471 | /* |
472 | * If either I or some sibling (!= me) is exclusive, we can't | |
473 | * overlap | |
474 | */ | |
ae8086ce TH |
475 | ret = -EINVAL; |
476 | cpuset_for_each_child(c, cont, par) { | |
1da177e4 LT |
477 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
478 | c != cur && | |
300ed6cb | 479 | cpumask_intersects(trial->cpus_allowed, c->cpus_allowed)) |
ae8086ce | 480 | goto out; |
1da177e4 LT |
481 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
482 | c != cur && | |
483 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) | |
ae8086ce | 484 | goto out; |
1da177e4 LT |
485 | } |
486 | ||
452477fa TH |
487 | /* |
488 | * Cpusets with tasks - existing or newly being attached - can't | |
489 | * have empty cpus_allowed or mems_allowed. | |
490 | */ | |
ae8086ce | 491 | ret = -ENOSPC; |
452477fa | 492 | if ((cgroup_task_count(cur->css.cgroup) || cur->attach_in_progress) && |
ae8086ce TH |
493 | (cpumask_empty(trial->cpus_allowed) || |
494 | nodes_empty(trial->mems_allowed))) | |
495 | goto out; | |
020958b6 | 496 | |
ae8086ce TH |
497 | ret = 0; |
498 | out: | |
499 | rcu_read_unlock(); | |
500 | return ret; | |
1da177e4 LT |
501 | } |
502 | ||
db7f47cf | 503 | #ifdef CONFIG_SMP |
029190c5 | 504 | /* |
cf417141 | 505 | * Helper routine for generate_sched_domains(). |
029190c5 PJ |
506 | * Do cpusets a, b have overlapping cpus_allowed masks? |
507 | */ | |
029190c5 PJ |
508 | static int cpusets_overlap(struct cpuset *a, struct cpuset *b) |
509 | { | |
300ed6cb | 510 | return cpumask_intersects(a->cpus_allowed, b->cpus_allowed); |
029190c5 PJ |
511 | } |
512 | ||
1d3504fc HS |
513 | static void |
514 | update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c) | |
515 | { | |
1d3504fc HS |
516 | if (dattr->relax_domain_level < c->relax_domain_level) |
517 | dattr->relax_domain_level = c->relax_domain_level; | |
518 | return; | |
519 | } | |
520 | ||
fc560a26 TH |
521 | static void update_domain_attr_tree(struct sched_domain_attr *dattr, |
522 | struct cpuset *root_cs) | |
f5393693 | 523 | { |
fc560a26 TH |
524 | struct cpuset *cp; |
525 | struct cgroup *pos_cgrp; | |
f5393693 | 526 | |
fc560a26 TH |
527 | rcu_read_lock(); |
528 | cpuset_for_each_descendant_pre(cp, pos_cgrp, root_cs) { | |
529 | /* skip the whole subtree if @cp doesn't have any CPU */ | |
530 | if (cpumask_empty(cp->cpus_allowed)) { | |
531 | pos_cgrp = cgroup_rightmost_descendant(pos_cgrp); | |
f5393693 | 532 | continue; |
fc560a26 | 533 | } |
f5393693 LJ |
534 | |
535 | if (is_sched_load_balance(cp)) | |
536 | update_domain_attr(dattr, cp); | |
f5393693 | 537 | } |
fc560a26 | 538 | rcu_read_unlock(); |
f5393693 LJ |
539 | } |
540 | ||
029190c5 | 541 | /* |
cf417141 MK |
542 | * generate_sched_domains() |
543 | * | |
544 | * This function builds a partial partition of the systems CPUs | |
545 | * A 'partial partition' is a set of non-overlapping subsets whose | |
546 | * union is a subset of that set. | |
547 | * The output of this function needs to be passed to kernel/sched.c | |
548 | * partition_sched_domains() routine, which will rebuild the scheduler's | |
549 | * load balancing domains (sched domains) as specified by that partial | |
550 | * partition. | |
029190c5 | 551 | * |
45ce80fb | 552 | * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt |
029190c5 PJ |
553 | * for a background explanation of this. |
554 | * | |
555 | * Does not return errors, on the theory that the callers of this | |
556 | * routine would rather not worry about failures to rebuild sched | |
557 | * domains when operating in the severe memory shortage situations | |
558 | * that could cause allocation failures below. | |
559 | * | |
5d21cc2d | 560 | * Must be called with cpuset_mutex held. |
029190c5 PJ |
561 | * |
562 | * The three key local variables below are: | |
aeed6824 | 563 | * q - a linked-list queue of cpuset pointers, used to implement a |
029190c5 PJ |
564 | * top-down scan of all cpusets. This scan loads a pointer |
565 | * to each cpuset marked is_sched_load_balance into the | |
566 | * array 'csa'. For our purposes, rebuilding the schedulers | |
567 | * sched domains, we can ignore !is_sched_load_balance cpusets. | |
568 | * csa - (for CpuSet Array) Array of pointers to all the cpusets | |
569 | * that need to be load balanced, for convenient iterative | |
570 | * access by the subsequent code that finds the best partition, | |
571 | * i.e the set of domains (subsets) of CPUs such that the | |
572 | * cpus_allowed of every cpuset marked is_sched_load_balance | |
573 | * is a subset of one of these domains, while there are as | |
574 | * many such domains as possible, each as small as possible. | |
575 | * doms - Conversion of 'csa' to an array of cpumasks, for passing to | |
576 | * the kernel/sched.c routine partition_sched_domains() in a | |
577 | * convenient format, that can be easily compared to the prior | |
578 | * value to determine what partition elements (sched domains) | |
579 | * were changed (added or removed.) | |
580 | * | |
581 | * Finding the best partition (set of domains): | |
582 | * The triple nested loops below over i, j, k scan over the | |
583 | * load balanced cpusets (using the array of cpuset pointers in | |
584 | * csa[]) looking for pairs of cpusets that have overlapping | |
585 | * cpus_allowed, but which don't have the same 'pn' partition | |
586 | * number and gives them in the same partition number. It keeps | |
587 | * looping on the 'restart' label until it can no longer find | |
588 | * any such pairs. | |
589 | * | |
590 | * The union of the cpus_allowed masks from the set of | |
591 | * all cpusets having the same 'pn' value then form the one | |
592 | * element of the partition (one sched domain) to be passed to | |
593 | * partition_sched_domains(). | |
594 | */ | |
acc3f5d7 | 595 | static int generate_sched_domains(cpumask_var_t **domains, |
cf417141 | 596 | struct sched_domain_attr **attributes) |
029190c5 | 597 | { |
029190c5 PJ |
598 | struct cpuset *cp; /* scans q */ |
599 | struct cpuset **csa; /* array of all cpuset ptrs */ | |
600 | int csn; /* how many cpuset ptrs in csa so far */ | |
601 | int i, j, k; /* indices for partition finding loops */ | |
acc3f5d7 | 602 | cpumask_var_t *doms; /* resulting partition; i.e. sched domains */ |
1d3504fc | 603 | struct sched_domain_attr *dattr; /* attributes for custom domains */ |
1583715d | 604 | int ndoms = 0; /* number of sched domains in result */ |
6af866af | 605 | int nslot; /* next empty doms[] struct cpumask slot */ |
fc560a26 | 606 | struct cgroup *pos_cgrp; |
029190c5 | 607 | |
029190c5 | 608 | doms = NULL; |
1d3504fc | 609 | dattr = NULL; |
cf417141 | 610 | csa = NULL; |
029190c5 PJ |
611 | |
612 | /* Special case for the 99% of systems with one, full, sched domain */ | |
613 | if (is_sched_load_balance(&top_cpuset)) { | |
acc3f5d7 RR |
614 | ndoms = 1; |
615 | doms = alloc_sched_domains(ndoms); | |
029190c5 | 616 | if (!doms) |
cf417141 MK |
617 | goto done; |
618 | ||
1d3504fc HS |
619 | dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL); |
620 | if (dattr) { | |
621 | *dattr = SD_ATTR_INIT; | |
93a65575 | 622 | update_domain_attr_tree(dattr, &top_cpuset); |
1d3504fc | 623 | } |
acc3f5d7 | 624 | cpumask_copy(doms[0], top_cpuset.cpus_allowed); |
cf417141 | 625 | |
cf417141 | 626 | goto done; |
029190c5 PJ |
627 | } |
628 | ||
029190c5 PJ |
629 | csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL); |
630 | if (!csa) | |
631 | goto done; | |
632 | csn = 0; | |
633 | ||
fc560a26 TH |
634 | rcu_read_lock(); |
635 | cpuset_for_each_descendant_pre(cp, pos_cgrp, &top_cpuset) { | |
f5393693 | 636 | /* |
fc560a26 TH |
637 | * Continue traversing beyond @cp iff @cp has some CPUs and |
638 | * isn't load balancing. The former is obvious. The | |
639 | * latter: All child cpusets contain a subset of the | |
640 | * parent's cpus, so just skip them, and then we call | |
641 | * update_domain_attr_tree() to calc relax_domain_level of | |
642 | * the corresponding sched domain. | |
f5393693 | 643 | */ |
fc560a26 TH |
644 | if (!cpumask_empty(cp->cpus_allowed) && |
645 | !is_sched_load_balance(cp)) | |
f5393693 | 646 | continue; |
489a5393 | 647 | |
fc560a26 TH |
648 | if (is_sched_load_balance(cp)) |
649 | csa[csn++] = cp; | |
650 | ||
651 | /* skip @cp's subtree */ | |
652 | pos_cgrp = cgroup_rightmost_descendant(pos_cgrp); | |
653 | } | |
654 | rcu_read_unlock(); | |
029190c5 PJ |
655 | |
656 | for (i = 0; i < csn; i++) | |
657 | csa[i]->pn = i; | |
658 | ndoms = csn; | |
659 | ||
660 | restart: | |
661 | /* Find the best partition (set of sched domains) */ | |
662 | for (i = 0; i < csn; i++) { | |
663 | struct cpuset *a = csa[i]; | |
664 | int apn = a->pn; | |
665 | ||
666 | for (j = 0; j < csn; j++) { | |
667 | struct cpuset *b = csa[j]; | |
668 | int bpn = b->pn; | |
669 | ||
670 | if (apn != bpn && cpusets_overlap(a, b)) { | |
671 | for (k = 0; k < csn; k++) { | |
672 | struct cpuset *c = csa[k]; | |
673 | ||
674 | if (c->pn == bpn) | |
675 | c->pn = apn; | |
676 | } | |
677 | ndoms--; /* one less element */ | |
678 | goto restart; | |
679 | } | |
680 | } | |
681 | } | |
682 | ||
cf417141 MK |
683 | /* |
684 | * Now we know how many domains to create. | |
685 | * Convert <csn, csa> to <ndoms, doms> and populate cpu masks. | |
686 | */ | |
acc3f5d7 | 687 | doms = alloc_sched_domains(ndoms); |
700018e0 | 688 | if (!doms) |
cf417141 | 689 | goto done; |
cf417141 MK |
690 | |
691 | /* | |
692 | * The rest of the code, including the scheduler, can deal with | |
693 | * dattr==NULL case. No need to abort if alloc fails. | |
694 | */ | |
1d3504fc | 695 | dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL); |
029190c5 PJ |
696 | |
697 | for (nslot = 0, i = 0; i < csn; i++) { | |
698 | struct cpuset *a = csa[i]; | |
6af866af | 699 | struct cpumask *dp; |
029190c5 PJ |
700 | int apn = a->pn; |
701 | ||
cf417141 MK |
702 | if (apn < 0) { |
703 | /* Skip completed partitions */ | |
704 | continue; | |
705 | } | |
706 | ||
acc3f5d7 | 707 | dp = doms[nslot]; |
cf417141 MK |
708 | |
709 | if (nslot == ndoms) { | |
710 | static int warnings = 10; | |
711 | if (warnings) { | |
712 | printk(KERN_WARNING | |
713 | "rebuild_sched_domains confused:" | |
714 | " nslot %d, ndoms %d, csn %d, i %d," | |
715 | " apn %d\n", | |
716 | nslot, ndoms, csn, i, apn); | |
717 | warnings--; | |
029190c5 | 718 | } |
cf417141 MK |
719 | continue; |
720 | } | |
029190c5 | 721 | |
6af866af | 722 | cpumask_clear(dp); |
cf417141 MK |
723 | if (dattr) |
724 | *(dattr + nslot) = SD_ATTR_INIT; | |
725 | for (j = i; j < csn; j++) { | |
726 | struct cpuset *b = csa[j]; | |
727 | ||
728 | if (apn == b->pn) { | |
300ed6cb | 729 | cpumask_or(dp, dp, b->cpus_allowed); |
cf417141 MK |
730 | if (dattr) |
731 | update_domain_attr_tree(dattr + nslot, b); | |
732 | ||
733 | /* Done with this partition */ | |
734 | b->pn = -1; | |
029190c5 | 735 | } |
029190c5 | 736 | } |
cf417141 | 737 | nslot++; |
029190c5 PJ |
738 | } |
739 | BUG_ON(nslot != ndoms); | |
740 | ||
cf417141 MK |
741 | done: |
742 | kfree(csa); | |
743 | ||
700018e0 LZ |
744 | /* |
745 | * Fallback to the default domain if kmalloc() failed. | |
746 | * See comments in partition_sched_domains(). | |
747 | */ | |
748 | if (doms == NULL) | |
749 | ndoms = 1; | |
750 | ||
cf417141 MK |
751 | *domains = doms; |
752 | *attributes = dattr; | |
753 | return ndoms; | |
754 | } | |
755 | ||
756 | /* | |
757 | * Rebuild scheduler domains. | |
758 | * | |
699140ba TH |
759 | * If the flag 'sched_load_balance' of any cpuset with non-empty |
760 | * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset | |
761 | * which has that flag enabled, or if any cpuset with a non-empty | |
762 | * 'cpus' is removed, then call this routine to rebuild the | |
763 | * scheduler's dynamic sched domains. | |
cf417141 | 764 | * |
5d21cc2d | 765 | * Call with cpuset_mutex held. Takes get_online_cpus(). |
cf417141 | 766 | */ |
699140ba | 767 | static void rebuild_sched_domains_locked(void) |
cf417141 MK |
768 | { |
769 | struct sched_domain_attr *attr; | |
acc3f5d7 | 770 | cpumask_var_t *doms; |
cf417141 MK |
771 | int ndoms; |
772 | ||
5d21cc2d | 773 | lockdep_assert_held(&cpuset_mutex); |
86ef5c9a | 774 | get_online_cpus(); |
cf417141 MK |
775 | |
776 | /* Generate domain masks and attrs */ | |
cf417141 | 777 | ndoms = generate_sched_domains(&doms, &attr); |
cf417141 MK |
778 | |
779 | /* Have scheduler rebuild the domains */ | |
780 | partition_sched_domains(ndoms, doms, attr); | |
781 | ||
86ef5c9a | 782 | put_online_cpus(); |
cf417141 | 783 | } |
db7f47cf | 784 | #else /* !CONFIG_SMP */ |
699140ba | 785 | static void rebuild_sched_domains_locked(void) |
db7f47cf PM |
786 | { |
787 | } | |
788 | ||
e1b8090b | 789 | static int generate_sched_domains(cpumask_var_t **domains, |
db7f47cf PM |
790 | struct sched_domain_attr **attributes) |
791 | { | |
792 | *domains = NULL; | |
793 | return 1; | |
794 | } | |
795 | #endif /* CONFIG_SMP */ | |
029190c5 | 796 | |
cf417141 MK |
797 | void rebuild_sched_domains(void) |
798 | { | |
5d21cc2d | 799 | mutex_lock(&cpuset_mutex); |
699140ba | 800 | rebuild_sched_domains_locked(); |
5d21cc2d | 801 | mutex_unlock(&cpuset_mutex); |
029190c5 PJ |
802 | } |
803 | ||
58f4790b CW |
804 | /** |
805 | * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's | |
806 | * @tsk: task to test | |
807 | * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner | |
808 | * | |
5d21cc2d | 809 | * Call with cpuset_mutex held. May take callback_mutex during call. |
58f4790b CW |
810 | * Called for each task in a cgroup by cgroup_scan_tasks(). |
811 | * Return nonzero if this tasks's cpus_allowed mask should be changed (in other | |
812 | * words, if its mask is not equal to its cpuset's mask). | |
053199ed | 813 | */ |
9e0c914c AB |
814 | static int cpuset_test_cpumask(struct task_struct *tsk, |
815 | struct cgroup_scanner *scan) | |
58f4790b | 816 | { |
300ed6cb | 817 | return !cpumask_equal(&tsk->cpus_allowed, |
58f4790b CW |
818 | (cgroup_cs(scan->cg))->cpus_allowed); |
819 | } | |
053199ed | 820 | |
58f4790b CW |
821 | /** |
822 | * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's | |
823 | * @tsk: task to test | |
824 | * @scan: struct cgroup_scanner containing the cgroup of the task | |
825 | * | |
826 | * Called by cgroup_scan_tasks() for each task in a cgroup whose | |
827 | * cpus_allowed mask needs to be changed. | |
828 | * | |
829 | * We don't need to re-check for the cgroup/cpuset membership, since we're | |
5d21cc2d | 830 | * holding cpuset_mutex at this point. |
58f4790b | 831 | */ |
9e0c914c AB |
832 | static void cpuset_change_cpumask(struct task_struct *tsk, |
833 | struct cgroup_scanner *scan) | |
58f4790b | 834 | { |
300ed6cb | 835 | set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed)); |
58f4790b CW |
836 | } |
837 | ||
0b2f630a MX |
838 | /** |
839 | * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset. | |
840 | * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed | |
4e74339a | 841 | * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks() |
0b2f630a | 842 | * |
5d21cc2d | 843 | * Called with cpuset_mutex held |
0b2f630a MX |
844 | * |
845 | * The cgroup_scan_tasks() function will scan all the tasks in a cgroup, | |
846 | * calling callback functions for each. | |
847 | * | |
4e74339a LZ |
848 | * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0 |
849 | * if @heap != NULL. | |
0b2f630a | 850 | */ |
4e74339a | 851 | static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap) |
0b2f630a MX |
852 | { |
853 | struct cgroup_scanner scan; | |
0b2f630a MX |
854 | |
855 | scan.cg = cs->css.cgroup; | |
856 | scan.test_task = cpuset_test_cpumask; | |
857 | scan.process_task = cpuset_change_cpumask; | |
4e74339a LZ |
858 | scan.heap = heap; |
859 | cgroup_scan_tasks(&scan); | |
0b2f630a MX |
860 | } |
861 | ||
58f4790b CW |
862 | /** |
863 | * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it | |
864 | * @cs: the cpuset to consider | |
865 | * @buf: buffer of cpu numbers written to this cpuset | |
866 | */ | |
645fcc9d LZ |
867 | static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, |
868 | const char *buf) | |
1da177e4 | 869 | { |
4e74339a | 870 | struct ptr_heap heap; |
58f4790b CW |
871 | int retval; |
872 | int is_load_balanced; | |
1da177e4 | 873 | |
5f054e31 | 874 | /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */ |
4c4d50f7 PJ |
875 | if (cs == &top_cpuset) |
876 | return -EACCES; | |
877 | ||
6f7f02e7 | 878 | /* |
c8d9c90c | 879 | * An empty cpus_allowed is ok only if the cpuset has no tasks. |
020958b6 PJ |
880 | * Since cpulist_parse() fails on an empty mask, we special case |
881 | * that parsing. The validate_change() call ensures that cpusets | |
882 | * with tasks have cpus. | |
6f7f02e7 | 883 | */ |
020958b6 | 884 | if (!*buf) { |
300ed6cb | 885 | cpumask_clear(trialcs->cpus_allowed); |
6f7f02e7 | 886 | } else { |
300ed6cb | 887 | retval = cpulist_parse(buf, trialcs->cpus_allowed); |
6f7f02e7 DR |
888 | if (retval < 0) |
889 | return retval; | |
37340746 | 890 | |
6ad4c188 | 891 | if (!cpumask_subset(trialcs->cpus_allowed, cpu_active_mask)) |
37340746 | 892 | return -EINVAL; |
6f7f02e7 | 893 | } |
645fcc9d | 894 | retval = validate_change(cs, trialcs); |
85d7b949 DG |
895 | if (retval < 0) |
896 | return retval; | |
029190c5 | 897 | |
8707d8b8 | 898 | /* Nothing to do if the cpus didn't change */ |
300ed6cb | 899 | if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed)) |
8707d8b8 | 900 | return 0; |
58f4790b | 901 | |
4e74339a LZ |
902 | retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL); |
903 | if (retval) | |
904 | return retval; | |
905 | ||
645fcc9d | 906 | is_load_balanced = is_sched_load_balance(trialcs); |
029190c5 | 907 | |
3d3f26a7 | 908 | mutex_lock(&callback_mutex); |
300ed6cb | 909 | cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); |
3d3f26a7 | 910 | mutex_unlock(&callback_mutex); |
029190c5 | 911 | |
8707d8b8 PM |
912 | /* |
913 | * Scan tasks in the cpuset, and update the cpumasks of any | |
58f4790b | 914 | * that need an update. |
8707d8b8 | 915 | */ |
4e74339a LZ |
916 | update_tasks_cpumask(cs, &heap); |
917 | ||
918 | heap_free(&heap); | |
58f4790b | 919 | |
8707d8b8 | 920 | if (is_load_balanced) |
699140ba | 921 | rebuild_sched_domains_locked(); |
85d7b949 | 922 | return 0; |
1da177e4 LT |
923 | } |
924 | ||
e4e364e8 PJ |
925 | /* |
926 | * cpuset_migrate_mm | |
927 | * | |
928 | * Migrate memory region from one set of nodes to another. | |
929 | * | |
930 | * Temporarilly set tasks mems_allowed to target nodes of migration, | |
931 | * so that the migration code can allocate pages on these nodes. | |
932 | * | |
5d21cc2d | 933 | * Call holding cpuset_mutex, so current's cpuset won't change |
c8d9c90c | 934 | * during this call, as manage_mutex holds off any cpuset_attach() |
e4e364e8 PJ |
935 | * calls. Therefore we don't need to take task_lock around the |
936 | * call to guarantee_online_mems(), as we know no one is changing | |
2df167a3 | 937 | * our task's cpuset. |
e4e364e8 | 938 | * |
e4e364e8 PJ |
939 | * While the mm_struct we are migrating is typically from some |
940 | * other task, the task_struct mems_allowed that we are hacking | |
941 | * is for our current task, which must allocate new pages for that | |
942 | * migrating memory region. | |
e4e364e8 PJ |
943 | */ |
944 | ||
945 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, | |
946 | const nodemask_t *to) | |
947 | { | |
948 | struct task_struct *tsk = current; | |
949 | ||
e4e364e8 | 950 | tsk->mems_allowed = *to; |
e4e364e8 PJ |
951 | |
952 | do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL); | |
953 | ||
8793d854 | 954 | guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed); |
e4e364e8 PJ |
955 | } |
956 | ||
3b6766fe | 957 | /* |
58568d2a MX |
958 | * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy |
959 | * @tsk: the task to change | |
960 | * @newmems: new nodes that the task will be set | |
961 | * | |
962 | * In order to avoid seeing no nodes if the old and new nodes are disjoint, | |
963 | * we structure updates as setting all new allowed nodes, then clearing newly | |
964 | * disallowed ones. | |
58568d2a MX |
965 | */ |
966 | static void cpuset_change_task_nodemask(struct task_struct *tsk, | |
967 | nodemask_t *newmems) | |
968 | { | |
b246272e | 969 | bool need_loop; |
89e8a244 | 970 | |
c0ff7453 MX |
971 | /* |
972 | * Allow tasks that have access to memory reserves because they have | |
973 | * been OOM killed to get memory anywhere. | |
974 | */ | |
975 | if (unlikely(test_thread_flag(TIF_MEMDIE))) | |
976 | return; | |
977 | if (current->flags & PF_EXITING) /* Let dying task have memory */ | |
978 | return; | |
979 | ||
980 | task_lock(tsk); | |
b246272e DR |
981 | /* |
982 | * Determine if a loop is necessary if another thread is doing | |
983 | * get_mems_allowed(). If at least one node remains unchanged and | |
984 | * tsk does not have a mempolicy, then an empty nodemask will not be | |
985 | * possible when mems_allowed is larger than a word. | |
986 | */ | |
987 | need_loop = task_has_mempolicy(tsk) || | |
988 | !nodes_intersects(*newmems, tsk->mems_allowed); | |
c0ff7453 | 989 | |
cc9a6c87 MG |
990 | if (need_loop) |
991 | write_seqcount_begin(&tsk->mems_allowed_seq); | |
c0ff7453 | 992 | |
cc9a6c87 MG |
993 | nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); |
994 | mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1); | |
c0ff7453 MX |
995 | |
996 | mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2); | |
58568d2a | 997 | tsk->mems_allowed = *newmems; |
cc9a6c87 MG |
998 | |
999 | if (need_loop) | |
1000 | write_seqcount_end(&tsk->mems_allowed_seq); | |
1001 | ||
c0ff7453 | 1002 | task_unlock(tsk); |
58568d2a MX |
1003 | } |
1004 | ||
1005 | /* | |
1006 | * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy | |
1007 | * of it to cpuset's new mems_allowed, and migrate pages to new nodes if | |
5d21cc2d | 1008 | * memory_migrate flag is set. Called with cpuset_mutex held. |
3b6766fe LZ |
1009 | */ |
1010 | static void cpuset_change_nodemask(struct task_struct *p, | |
1011 | struct cgroup_scanner *scan) | |
1012 | { | |
1013 | struct mm_struct *mm; | |
1014 | struct cpuset *cs; | |
1015 | int migrate; | |
1016 | const nodemask_t *oldmem = scan->data; | |
5d21cc2d | 1017 | static nodemask_t newmems; /* protected by cpuset_mutex */ |
58568d2a MX |
1018 | |
1019 | cs = cgroup_cs(scan->cg); | |
ee24d379 | 1020 | guarantee_online_mems(cs, &newmems); |
58568d2a | 1021 | |
ee24d379 | 1022 | cpuset_change_task_nodemask(p, &newmems); |
53feb297 | 1023 | |
3b6766fe LZ |
1024 | mm = get_task_mm(p); |
1025 | if (!mm) | |
1026 | return; | |
1027 | ||
3b6766fe LZ |
1028 | migrate = is_memory_migrate(cs); |
1029 | ||
1030 | mpol_rebind_mm(mm, &cs->mems_allowed); | |
1031 | if (migrate) | |
1032 | cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed); | |
1033 | mmput(mm); | |
1034 | } | |
1035 | ||
8793d854 PM |
1036 | static void *cpuset_being_rebound; |
1037 | ||
0b2f630a MX |
1038 | /** |
1039 | * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. | |
1040 | * @cs: the cpuset in which each task's mems_allowed mask needs to be changed | |
1041 | * @oldmem: old mems_allowed of cpuset cs | |
010cfac4 | 1042 | * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks() |
0b2f630a | 1043 | * |
5d21cc2d | 1044 | * Called with cpuset_mutex held |
010cfac4 LZ |
1045 | * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0 |
1046 | * if @heap != NULL. | |
0b2f630a | 1047 | */ |
010cfac4 LZ |
1048 | static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem, |
1049 | struct ptr_heap *heap) | |
1da177e4 | 1050 | { |
3b6766fe | 1051 | struct cgroup_scanner scan; |
59dac16f | 1052 | |
846a16bf | 1053 | cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ |
4225399a | 1054 | |
3b6766fe LZ |
1055 | scan.cg = cs->css.cgroup; |
1056 | scan.test_task = NULL; | |
1057 | scan.process_task = cpuset_change_nodemask; | |
010cfac4 | 1058 | scan.heap = heap; |
3b6766fe | 1059 | scan.data = (nodemask_t *)oldmem; |
4225399a PJ |
1060 | |
1061 | /* | |
3b6766fe LZ |
1062 | * The mpol_rebind_mm() call takes mmap_sem, which we couldn't |
1063 | * take while holding tasklist_lock. Forks can happen - the | |
1064 | * mpol_dup() cpuset_being_rebound check will catch such forks, | |
1065 | * and rebind their vma mempolicies too. Because we still hold | |
5d21cc2d | 1066 | * the global cpuset_mutex, we know that no other rebind effort |
3b6766fe | 1067 | * will be contending for the global variable cpuset_being_rebound. |
4225399a | 1068 | * It's ok if we rebind the same mm twice; mpol_rebind_mm() |
04c19fa6 | 1069 | * is idempotent. Also migrate pages in each mm to new nodes. |
4225399a | 1070 | */ |
010cfac4 | 1071 | cgroup_scan_tasks(&scan); |
4225399a | 1072 | |
2df167a3 | 1073 | /* We're done rebinding vmas to this cpuset's new mems_allowed. */ |
8793d854 | 1074 | cpuset_being_rebound = NULL; |
1da177e4 LT |
1075 | } |
1076 | ||
0b2f630a MX |
1077 | /* |
1078 | * Handle user request to change the 'mems' memory placement | |
1079 | * of a cpuset. Needs to validate the request, update the | |
58568d2a MX |
1080 | * cpusets mems_allowed, and for each task in the cpuset, |
1081 | * update mems_allowed and rebind task's mempolicy and any vma | |
1082 | * mempolicies and if the cpuset is marked 'memory_migrate', | |
1083 | * migrate the tasks pages to the new memory. | |
0b2f630a | 1084 | * |
5d21cc2d | 1085 | * Call with cpuset_mutex held. May take callback_mutex during call. |
0b2f630a MX |
1086 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
1087 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind | |
1088 | * their mempolicies to the cpusets new mems_allowed. | |
1089 | */ | |
645fcc9d LZ |
1090 | static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, |
1091 | const char *buf) | |
0b2f630a | 1092 | { |
53feb297 | 1093 | NODEMASK_ALLOC(nodemask_t, oldmem, GFP_KERNEL); |
0b2f630a | 1094 | int retval; |
010cfac4 | 1095 | struct ptr_heap heap; |
0b2f630a | 1096 | |
53feb297 MX |
1097 | if (!oldmem) |
1098 | return -ENOMEM; | |
1099 | ||
0b2f630a | 1100 | /* |
38d7bee9 | 1101 | * top_cpuset.mems_allowed tracks node_stats[N_MEMORY]; |
0b2f630a MX |
1102 | * it's read-only |
1103 | */ | |
53feb297 MX |
1104 | if (cs == &top_cpuset) { |
1105 | retval = -EACCES; | |
1106 | goto done; | |
1107 | } | |
0b2f630a | 1108 | |
0b2f630a MX |
1109 | /* |
1110 | * An empty mems_allowed is ok iff there are no tasks in the cpuset. | |
1111 | * Since nodelist_parse() fails on an empty mask, we special case | |
1112 | * that parsing. The validate_change() call ensures that cpusets | |
1113 | * with tasks have memory. | |
1114 | */ | |
1115 | if (!*buf) { | |
645fcc9d | 1116 | nodes_clear(trialcs->mems_allowed); |
0b2f630a | 1117 | } else { |
645fcc9d | 1118 | retval = nodelist_parse(buf, trialcs->mems_allowed); |
0b2f630a MX |
1119 | if (retval < 0) |
1120 | goto done; | |
1121 | ||
645fcc9d | 1122 | if (!nodes_subset(trialcs->mems_allowed, |
38d7bee9 | 1123 | node_states[N_MEMORY])) { |
53feb297 MX |
1124 | retval = -EINVAL; |
1125 | goto done; | |
1126 | } | |
0b2f630a | 1127 | } |
53feb297 MX |
1128 | *oldmem = cs->mems_allowed; |
1129 | if (nodes_equal(*oldmem, trialcs->mems_allowed)) { | |
0b2f630a MX |
1130 | retval = 0; /* Too easy - nothing to do */ |
1131 | goto done; | |
1132 | } | |
645fcc9d | 1133 | retval = validate_change(cs, trialcs); |
0b2f630a MX |
1134 | if (retval < 0) |
1135 | goto done; | |
1136 | ||
010cfac4 LZ |
1137 | retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL); |
1138 | if (retval < 0) | |
1139 | goto done; | |
1140 | ||
0b2f630a | 1141 | mutex_lock(&callback_mutex); |
645fcc9d | 1142 | cs->mems_allowed = trialcs->mems_allowed; |
0b2f630a MX |
1143 | mutex_unlock(&callback_mutex); |
1144 | ||
53feb297 | 1145 | update_tasks_nodemask(cs, oldmem, &heap); |
010cfac4 LZ |
1146 | |
1147 | heap_free(&heap); | |
0b2f630a | 1148 | done: |
53feb297 | 1149 | NODEMASK_FREE(oldmem); |
0b2f630a MX |
1150 | return retval; |
1151 | } | |
1152 | ||
8793d854 PM |
1153 | int current_cpuset_is_being_rebound(void) |
1154 | { | |
1155 | return task_cs(current) == cpuset_being_rebound; | |
1156 | } | |
1157 | ||
5be7a479 | 1158 | static int update_relax_domain_level(struct cpuset *cs, s64 val) |
1d3504fc | 1159 | { |
db7f47cf | 1160 | #ifdef CONFIG_SMP |
60495e77 | 1161 | if (val < -1 || val >= sched_domain_level_max) |
30e0e178 | 1162 | return -EINVAL; |
db7f47cf | 1163 | #endif |
1d3504fc HS |
1164 | |
1165 | if (val != cs->relax_domain_level) { | |
1166 | cs->relax_domain_level = val; | |
300ed6cb LZ |
1167 | if (!cpumask_empty(cs->cpus_allowed) && |
1168 | is_sched_load_balance(cs)) | |
699140ba | 1169 | rebuild_sched_domains_locked(); |
1d3504fc HS |
1170 | } |
1171 | ||
1172 | return 0; | |
1173 | } | |
1174 | ||
950592f7 MX |
1175 | /* |
1176 | * cpuset_change_flag - make a task's spread flags the same as its cpuset's | |
1177 | * @tsk: task to be updated | |
1178 | * @scan: struct cgroup_scanner containing the cgroup of the task | |
1179 | * | |
1180 | * Called by cgroup_scan_tasks() for each task in a cgroup. | |
1181 | * | |
1182 | * We don't need to re-check for the cgroup/cpuset membership, since we're | |
5d21cc2d | 1183 | * holding cpuset_mutex at this point. |
950592f7 MX |
1184 | */ |
1185 | static void cpuset_change_flag(struct task_struct *tsk, | |
1186 | struct cgroup_scanner *scan) | |
1187 | { | |
1188 | cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk); | |
1189 | } | |
1190 | ||
1191 | /* | |
1192 | * update_tasks_flags - update the spread flags of tasks in the cpuset. | |
1193 | * @cs: the cpuset in which each task's spread flags needs to be changed | |
1194 | * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks() | |
1195 | * | |
5d21cc2d | 1196 | * Called with cpuset_mutex held |
950592f7 MX |
1197 | * |
1198 | * The cgroup_scan_tasks() function will scan all the tasks in a cgroup, | |
1199 | * calling callback functions for each. | |
1200 | * | |
1201 | * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0 | |
1202 | * if @heap != NULL. | |
1203 | */ | |
1204 | static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap) | |
1205 | { | |
1206 | struct cgroup_scanner scan; | |
1207 | ||
1208 | scan.cg = cs->css.cgroup; | |
1209 | scan.test_task = NULL; | |
1210 | scan.process_task = cpuset_change_flag; | |
1211 | scan.heap = heap; | |
1212 | cgroup_scan_tasks(&scan); | |
1213 | } | |
1214 | ||
1da177e4 LT |
1215 | /* |
1216 | * update_flag - read a 0 or a 1 in a file and update associated flag | |
78608366 PM |
1217 | * bit: the bit to update (see cpuset_flagbits_t) |
1218 | * cs: the cpuset to update | |
1219 | * turning_on: whether the flag is being set or cleared | |
053199ed | 1220 | * |
5d21cc2d | 1221 | * Call with cpuset_mutex held. |
1da177e4 LT |
1222 | */ |
1223 | ||
700fe1ab PM |
1224 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, |
1225 | int turning_on) | |
1da177e4 | 1226 | { |
645fcc9d | 1227 | struct cpuset *trialcs; |
40b6a762 | 1228 | int balance_flag_changed; |
950592f7 MX |
1229 | int spread_flag_changed; |
1230 | struct ptr_heap heap; | |
1231 | int err; | |
1da177e4 | 1232 | |
645fcc9d LZ |
1233 | trialcs = alloc_trial_cpuset(cs); |
1234 | if (!trialcs) | |
1235 | return -ENOMEM; | |
1236 | ||
1da177e4 | 1237 | if (turning_on) |
645fcc9d | 1238 | set_bit(bit, &trialcs->flags); |
1da177e4 | 1239 | else |
645fcc9d | 1240 | clear_bit(bit, &trialcs->flags); |
1da177e4 | 1241 | |
645fcc9d | 1242 | err = validate_change(cs, trialcs); |
85d7b949 | 1243 | if (err < 0) |
645fcc9d | 1244 | goto out; |
029190c5 | 1245 | |
950592f7 MX |
1246 | err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL); |
1247 | if (err < 0) | |
1248 | goto out; | |
1249 | ||
029190c5 | 1250 | balance_flag_changed = (is_sched_load_balance(cs) != |
645fcc9d | 1251 | is_sched_load_balance(trialcs)); |
029190c5 | 1252 | |
950592f7 MX |
1253 | spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs)) |
1254 | || (is_spread_page(cs) != is_spread_page(trialcs))); | |
1255 | ||
3d3f26a7 | 1256 | mutex_lock(&callback_mutex); |
645fcc9d | 1257 | cs->flags = trialcs->flags; |
3d3f26a7 | 1258 | mutex_unlock(&callback_mutex); |
85d7b949 | 1259 | |
300ed6cb | 1260 | if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) |
699140ba | 1261 | rebuild_sched_domains_locked(); |
029190c5 | 1262 | |
950592f7 MX |
1263 | if (spread_flag_changed) |
1264 | update_tasks_flags(cs, &heap); | |
1265 | heap_free(&heap); | |
645fcc9d LZ |
1266 | out: |
1267 | free_trial_cpuset(trialcs); | |
1268 | return err; | |
1da177e4 LT |
1269 | } |
1270 | ||
3e0d98b9 | 1271 | /* |
80f7228b | 1272 | * Frequency meter - How fast is some event occurring? |
3e0d98b9 PJ |
1273 | * |
1274 | * These routines manage a digitally filtered, constant time based, | |
1275 | * event frequency meter. There are four routines: | |
1276 | * fmeter_init() - initialize a frequency meter. | |
1277 | * fmeter_markevent() - called each time the event happens. | |
1278 | * fmeter_getrate() - returns the recent rate of such events. | |
1279 | * fmeter_update() - internal routine used to update fmeter. | |
1280 | * | |
1281 | * A common data structure is passed to each of these routines, | |
1282 | * which is used to keep track of the state required to manage the | |
1283 | * frequency meter and its digital filter. | |
1284 | * | |
1285 | * The filter works on the number of events marked per unit time. | |
1286 | * The filter is single-pole low-pass recursive (IIR). The time unit | |
1287 | * is 1 second. Arithmetic is done using 32-bit integers scaled to | |
1288 | * simulate 3 decimal digits of precision (multiplied by 1000). | |
1289 | * | |
1290 | * With an FM_COEF of 933, and a time base of 1 second, the filter | |
1291 | * has a half-life of 10 seconds, meaning that if the events quit | |
1292 | * happening, then the rate returned from the fmeter_getrate() | |
1293 | * will be cut in half each 10 seconds, until it converges to zero. | |
1294 | * | |
1295 | * It is not worth doing a real infinitely recursive filter. If more | |
1296 | * than FM_MAXTICKS ticks have elapsed since the last filter event, | |
1297 | * just compute FM_MAXTICKS ticks worth, by which point the level | |
1298 | * will be stable. | |
1299 | * | |
1300 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid | |
1301 | * arithmetic overflow in the fmeter_update() routine. | |
1302 | * | |
1303 | * Given the simple 32 bit integer arithmetic used, this meter works | |
1304 | * best for reporting rates between one per millisecond (msec) and | |
1305 | * one per 32 (approx) seconds. At constant rates faster than one | |
1306 | * per msec it maxes out at values just under 1,000,000. At constant | |
1307 | * rates between one per msec, and one per second it will stabilize | |
1308 | * to a value N*1000, where N is the rate of events per second. | |
1309 | * At constant rates between one per second and one per 32 seconds, | |
1310 | * it will be choppy, moving up on the seconds that have an event, | |
1311 | * and then decaying until the next event. At rates slower than | |
1312 | * about one in 32 seconds, it decays all the way back to zero between | |
1313 | * each event. | |
1314 | */ | |
1315 | ||
1316 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ | |
1317 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ | |
1318 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ | |
1319 | #define FM_SCALE 1000 /* faux fixed point scale */ | |
1320 | ||
1321 | /* Initialize a frequency meter */ | |
1322 | static void fmeter_init(struct fmeter *fmp) | |
1323 | { | |
1324 | fmp->cnt = 0; | |
1325 | fmp->val = 0; | |
1326 | fmp->time = 0; | |
1327 | spin_lock_init(&fmp->lock); | |
1328 | } | |
1329 | ||
1330 | /* Internal meter update - process cnt events and update value */ | |
1331 | static void fmeter_update(struct fmeter *fmp) | |
1332 | { | |
1333 | time_t now = get_seconds(); | |
1334 | time_t ticks = now - fmp->time; | |
1335 | ||
1336 | if (ticks == 0) | |
1337 | return; | |
1338 | ||
1339 | ticks = min(FM_MAXTICKS, ticks); | |
1340 | while (ticks-- > 0) | |
1341 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; | |
1342 | fmp->time = now; | |
1343 | ||
1344 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; | |
1345 | fmp->cnt = 0; | |
1346 | } | |
1347 | ||
1348 | /* Process any previous ticks, then bump cnt by one (times scale). */ | |
1349 | static void fmeter_markevent(struct fmeter *fmp) | |
1350 | { | |
1351 | spin_lock(&fmp->lock); | |
1352 | fmeter_update(fmp); | |
1353 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); | |
1354 | spin_unlock(&fmp->lock); | |
1355 | } | |
1356 | ||
1357 | /* Process any previous ticks, then return current value. */ | |
1358 | static int fmeter_getrate(struct fmeter *fmp) | |
1359 | { | |
1360 | int val; | |
1361 | ||
1362 | spin_lock(&fmp->lock); | |
1363 | fmeter_update(fmp); | |
1364 | val = fmp->val; | |
1365 | spin_unlock(&fmp->lock); | |
1366 | return val; | |
1367 | } | |
1368 | ||
5d21cc2d | 1369 | /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */ |
761b3ef5 | 1370 | static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
f780bdb7 | 1371 | { |
2f7ee569 | 1372 | struct cpuset *cs = cgroup_cs(cgrp); |
bb9d97b6 TH |
1373 | struct task_struct *task; |
1374 | int ret; | |
1da177e4 | 1375 | |
5d21cc2d TH |
1376 | mutex_lock(&cpuset_mutex); |
1377 | ||
1378 | ret = -ENOSPC; | |
300ed6cb | 1379 | if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
5d21cc2d | 1380 | goto out_unlock; |
9985b0ba | 1381 | |
bb9d97b6 TH |
1382 | cgroup_taskset_for_each(task, cgrp, tset) { |
1383 | /* | |
1384 | * Kthreads bound to specific cpus cannot be moved to a new | |
1385 | * cpuset; we cannot change their cpu affinity and | |
1386 | * isolating such threads by their set of allowed nodes is | |
1387 | * unnecessary. Thus, cpusets are not applicable for such | |
1388 | * threads. This prevents checking for success of | |
1389 | * set_cpus_allowed_ptr() on all attached tasks before | |
1390 | * cpus_allowed may be changed. | |
1391 | */ | |
5d21cc2d | 1392 | ret = -EINVAL; |
bb9d97b6 | 1393 | if (task->flags & PF_THREAD_BOUND) |
5d21cc2d TH |
1394 | goto out_unlock; |
1395 | ret = security_task_setscheduler(task); | |
1396 | if (ret) | |
1397 | goto out_unlock; | |
bb9d97b6 | 1398 | } |
f780bdb7 | 1399 | |
452477fa TH |
1400 | /* |
1401 | * Mark attach is in progress. This makes validate_change() fail | |
1402 | * changes which zero cpus/mems_allowed. | |
1403 | */ | |
1404 | cs->attach_in_progress++; | |
5d21cc2d TH |
1405 | ret = 0; |
1406 | out_unlock: | |
1407 | mutex_unlock(&cpuset_mutex); | |
1408 | return ret; | |
8793d854 | 1409 | } |
1da177e4 | 1410 | |
452477fa TH |
1411 | static void cpuset_cancel_attach(struct cgroup *cgrp, |
1412 | struct cgroup_taskset *tset) | |
1413 | { | |
5d21cc2d | 1414 | mutex_lock(&cpuset_mutex); |
452477fa | 1415 | cgroup_cs(cgrp)->attach_in_progress--; |
5d21cc2d | 1416 | mutex_unlock(&cpuset_mutex); |
452477fa TH |
1417 | } |
1418 | ||
4e4c9a14 | 1419 | /* |
5d21cc2d | 1420 | * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach() |
4e4c9a14 TH |
1421 | * but we can't allocate it dynamically there. Define it global and |
1422 | * allocate from cpuset_init(). | |
1423 | */ | |
1424 | static cpumask_var_t cpus_attach; | |
1425 | ||
761b3ef5 | 1426 | static void cpuset_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
8793d854 | 1427 | { |
5d21cc2d | 1428 | /* static bufs protected by cpuset_mutex */ |
4e4c9a14 TH |
1429 | static nodemask_t cpuset_attach_nodemask_from; |
1430 | static nodemask_t cpuset_attach_nodemask_to; | |
8793d854 | 1431 | struct mm_struct *mm; |
bb9d97b6 TH |
1432 | struct task_struct *task; |
1433 | struct task_struct *leader = cgroup_taskset_first(tset); | |
2f7ee569 TH |
1434 | struct cgroup *oldcgrp = cgroup_taskset_cur_cgroup(tset); |
1435 | struct cpuset *cs = cgroup_cs(cgrp); | |
1436 | struct cpuset *oldcs = cgroup_cs(oldcgrp); | |
22fb52dd | 1437 | |
5d21cc2d TH |
1438 | mutex_lock(&cpuset_mutex); |
1439 | ||
4e4c9a14 TH |
1440 | /* prepare for attach */ |
1441 | if (cs == &top_cpuset) | |
1442 | cpumask_copy(cpus_attach, cpu_possible_mask); | |
1443 | else | |
1444 | guarantee_online_cpus(cs, cpus_attach); | |
1445 | ||
1446 | guarantee_online_mems(cs, &cpuset_attach_nodemask_to); | |
1447 | ||
bb9d97b6 TH |
1448 | cgroup_taskset_for_each(task, cgrp, tset) { |
1449 | /* | |
1450 | * can_attach beforehand should guarantee that this doesn't | |
1451 | * fail. TODO: have a better way to handle failure here | |
1452 | */ | |
1453 | WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); | |
1454 | ||
1455 | cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); | |
1456 | cpuset_update_task_spread_flag(cs, task); | |
1457 | } | |
22fb52dd | 1458 | |
f780bdb7 BB |
1459 | /* |
1460 | * Change mm, possibly for multiple threads in a threadgroup. This is | |
1461 | * expensive and may sleep. | |
1462 | */ | |
1463 | cpuset_attach_nodemask_from = oldcs->mems_allowed; | |
1464 | cpuset_attach_nodemask_to = cs->mems_allowed; | |
bb9d97b6 | 1465 | mm = get_task_mm(leader); |
4225399a | 1466 | if (mm) { |
f780bdb7 | 1467 | mpol_rebind_mm(mm, &cpuset_attach_nodemask_to); |
2741a559 | 1468 | if (is_memory_migrate(cs)) |
f780bdb7 BB |
1469 | cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from, |
1470 | &cpuset_attach_nodemask_to); | |
4225399a PJ |
1471 | mmput(mm); |
1472 | } | |
452477fa TH |
1473 | |
1474 | cs->attach_in_progress--; | |
02bb5863 TH |
1475 | |
1476 | /* | |
1477 | * We may have raced with CPU/memory hotunplug. Trigger hotplug | |
1478 | * propagation if @cs doesn't have any CPU or memory. It will move | |
1479 | * the newly added tasks to the nearest parent which can execute. | |
1480 | */ | |
1481 | if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) | |
1482 | schedule_cpuset_propagate_hotplug(cs); | |
5d21cc2d TH |
1483 | |
1484 | mutex_unlock(&cpuset_mutex); | |
1da177e4 LT |
1485 | } |
1486 | ||
1487 | /* The various types of files and directories in a cpuset file system */ | |
1488 | ||
1489 | typedef enum { | |
45b07ef3 | 1490 | FILE_MEMORY_MIGRATE, |
1da177e4 LT |
1491 | FILE_CPULIST, |
1492 | FILE_MEMLIST, | |
1493 | FILE_CPU_EXCLUSIVE, | |
1494 | FILE_MEM_EXCLUSIVE, | |
78608366 | 1495 | FILE_MEM_HARDWALL, |
029190c5 | 1496 | FILE_SCHED_LOAD_BALANCE, |
1d3504fc | 1497 | FILE_SCHED_RELAX_DOMAIN_LEVEL, |
3e0d98b9 PJ |
1498 | FILE_MEMORY_PRESSURE_ENABLED, |
1499 | FILE_MEMORY_PRESSURE, | |
825a46af PJ |
1500 | FILE_SPREAD_PAGE, |
1501 | FILE_SPREAD_SLAB, | |
1da177e4 LT |
1502 | } cpuset_filetype_t; |
1503 | ||
700fe1ab PM |
1504 | static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val) |
1505 | { | |
700fe1ab PM |
1506 | struct cpuset *cs = cgroup_cs(cgrp); |
1507 | cpuset_filetype_t type = cft->private; | |
5d21cc2d | 1508 | int retval = -ENODEV; |
700fe1ab | 1509 | |
5d21cc2d TH |
1510 | mutex_lock(&cpuset_mutex); |
1511 | if (!is_cpuset_online(cs)) | |
1512 | goto out_unlock; | |
700fe1ab PM |
1513 | |
1514 | switch (type) { | |
1da177e4 | 1515 | case FILE_CPU_EXCLUSIVE: |
700fe1ab | 1516 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, val); |
1da177e4 LT |
1517 | break; |
1518 | case FILE_MEM_EXCLUSIVE: | |
700fe1ab | 1519 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, val); |
1da177e4 | 1520 | break; |
78608366 PM |
1521 | case FILE_MEM_HARDWALL: |
1522 | retval = update_flag(CS_MEM_HARDWALL, cs, val); | |
1523 | break; | |
029190c5 | 1524 | case FILE_SCHED_LOAD_BALANCE: |
700fe1ab | 1525 | retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val); |
1d3504fc | 1526 | break; |
45b07ef3 | 1527 | case FILE_MEMORY_MIGRATE: |
700fe1ab | 1528 | retval = update_flag(CS_MEMORY_MIGRATE, cs, val); |
45b07ef3 | 1529 | break; |
3e0d98b9 | 1530 | case FILE_MEMORY_PRESSURE_ENABLED: |
700fe1ab | 1531 | cpuset_memory_pressure_enabled = !!val; |
3e0d98b9 PJ |
1532 | break; |
1533 | case FILE_MEMORY_PRESSURE: | |
1534 | retval = -EACCES; | |
1535 | break; | |
825a46af | 1536 | case FILE_SPREAD_PAGE: |
700fe1ab | 1537 | retval = update_flag(CS_SPREAD_PAGE, cs, val); |
825a46af PJ |
1538 | break; |
1539 | case FILE_SPREAD_SLAB: | |
700fe1ab | 1540 | retval = update_flag(CS_SPREAD_SLAB, cs, val); |
825a46af | 1541 | break; |
1da177e4 LT |
1542 | default: |
1543 | retval = -EINVAL; | |
700fe1ab | 1544 | break; |
1da177e4 | 1545 | } |
5d21cc2d TH |
1546 | out_unlock: |
1547 | mutex_unlock(&cpuset_mutex); | |
1da177e4 LT |
1548 | return retval; |
1549 | } | |
1550 | ||
5be7a479 PM |
1551 | static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val) |
1552 | { | |
5be7a479 PM |
1553 | struct cpuset *cs = cgroup_cs(cgrp); |
1554 | cpuset_filetype_t type = cft->private; | |
5d21cc2d | 1555 | int retval = -ENODEV; |
5be7a479 | 1556 | |
5d21cc2d TH |
1557 | mutex_lock(&cpuset_mutex); |
1558 | if (!is_cpuset_online(cs)) | |
1559 | goto out_unlock; | |
e3712395 | 1560 | |
5be7a479 PM |
1561 | switch (type) { |
1562 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: | |
1563 | retval = update_relax_domain_level(cs, val); | |
1564 | break; | |
1565 | default: | |
1566 | retval = -EINVAL; | |
1567 | break; | |
1568 | } | |
5d21cc2d TH |
1569 | out_unlock: |
1570 | mutex_unlock(&cpuset_mutex); | |
5be7a479 PM |
1571 | return retval; |
1572 | } | |
1573 | ||
e3712395 PM |
1574 | /* |
1575 | * Common handling for a write to a "cpus" or "mems" file. | |
1576 | */ | |
1577 | static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft, | |
1578 | const char *buf) | |
1579 | { | |
645fcc9d LZ |
1580 | struct cpuset *cs = cgroup_cs(cgrp); |
1581 | struct cpuset *trialcs; | |
5d21cc2d | 1582 | int retval = -ENODEV; |
e3712395 | 1583 | |
3a5a6d0c TH |
1584 | /* |
1585 | * CPU or memory hotunplug may leave @cs w/o any execution | |
1586 | * resources, in which case the hotplug code asynchronously updates | |
1587 | * configuration and transfers all tasks to the nearest ancestor | |
1588 | * which can execute. | |
1589 | * | |
1590 | * As writes to "cpus" or "mems" may restore @cs's execution | |
1591 | * resources, wait for the previously scheduled operations before | |
1592 | * proceeding, so that we don't end up keep removing tasks added | |
1593 | * after execution capability is restored. | |
02bb5863 TH |
1594 | * |
1595 | * Flushing cpuset_hotplug_work is enough to synchronize against | |
1596 | * hotplug hanlding; however, cpuset_attach() may schedule | |
1597 | * propagation work directly. Flush the workqueue too. | |
3a5a6d0c TH |
1598 | */ |
1599 | flush_work(&cpuset_hotplug_work); | |
02bb5863 | 1600 | flush_workqueue(cpuset_propagate_hotplug_wq); |
3a5a6d0c | 1601 | |
5d21cc2d TH |
1602 | mutex_lock(&cpuset_mutex); |
1603 | if (!is_cpuset_online(cs)) | |
1604 | goto out_unlock; | |
e3712395 | 1605 | |
645fcc9d | 1606 | trialcs = alloc_trial_cpuset(cs); |
b75f38d6 LZ |
1607 | if (!trialcs) { |
1608 | retval = -ENOMEM; | |
5d21cc2d | 1609 | goto out_unlock; |
b75f38d6 | 1610 | } |
645fcc9d | 1611 | |
e3712395 PM |
1612 | switch (cft->private) { |
1613 | case FILE_CPULIST: | |
645fcc9d | 1614 | retval = update_cpumask(cs, trialcs, buf); |
e3712395 PM |
1615 | break; |
1616 | case FILE_MEMLIST: | |
645fcc9d | 1617 | retval = update_nodemask(cs, trialcs, buf); |
e3712395 PM |
1618 | break; |
1619 | default: | |
1620 | retval = -EINVAL; | |
1621 | break; | |
1622 | } | |
645fcc9d LZ |
1623 | |
1624 | free_trial_cpuset(trialcs); | |
5d21cc2d TH |
1625 | out_unlock: |
1626 | mutex_unlock(&cpuset_mutex); | |
e3712395 PM |
1627 | return retval; |
1628 | } | |
1629 | ||
1da177e4 LT |
1630 | /* |
1631 | * These ascii lists should be read in a single call, by using a user | |
1632 | * buffer large enough to hold the entire map. If read in smaller | |
1633 | * chunks, there is no guarantee of atomicity. Since the display format | |
1634 | * used, list of ranges of sequential numbers, is variable length, | |
1635 | * and since these maps can change value dynamically, one could read | |
1636 | * gibberish by doing partial reads while a list was changing. | |
1637 | * A single large read to a buffer that crosses a page boundary is | |
1638 | * ok, because the result being copied to user land is not recomputed | |
1639 | * across a page fault. | |
1640 | */ | |
1641 | ||
9303e0c4 | 1642 | static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs) |
1da177e4 | 1643 | { |
9303e0c4 | 1644 | size_t count; |
1da177e4 | 1645 | |
3d3f26a7 | 1646 | mutex_lock(&callback_mutex); |
9303e0c4 | 1647 | count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed); |
3d3f26a7 | 1648 | mutex_unlock(&callback_mutex); |
1da177e4 | 1649 | |
9303e0c4 | 1650 | return count; |
1da177e4 LT |
1651 | } |
1652 | ||
9303e0c4 | 1653 | static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs) |
1da177e4 | 1654 | { |
9303e0c4 | 1655 | size_t count; |
1da177e4 | 1656 | |
3d3f26a7 | 1657 | mutex_lock(&callback_mutex); |
9303e0c4 | 1658 | count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed); |
3d3f26a7 | 1659 | mutex_unlock(&callback_mutex); |
1da177e4 | 1660 | |
9303e0c4 | 1661 | return count; |
1da177e4 LT |
1662 | } |
1663 | ||
8793d854 PM |
1664 | static ssize_t cpuset_common_file_read(struct cgroup *cont, |
1665 | struct cftype *cft, | |
1666 | struct file *file, | |
1667 | char __user *buf, | |
1668 | size_t nbytes, loff_t *ppos) | |
1da177e4 | 1669 | { |
8793d854 | 1670 | struct cpuset *cs = cgroup_cs(cont); |
1da177e4 LT |
1671 | cpuset_filetype_t type = cft->private; |
1672 | char *page; | |
1673 | ssize_t retval = 0; | |
1674 | char *s; | |
1da177e4 | 1675 | |
e12ba74d | 1676 | if (!(page = (char *)__get_free_page(GFP_TEMPORARY))) |
1da177e4 LT |
1677 | return -ENOMEM; |
1678 | ||
1679 | s = page; | |
1680 | ||
1681 | switch (type) { | |
1682 | case FILE_CPULIST: | |
1683 | s += cpuset_sprintf_cpulist(s, cs); | |
1684 | break; | |
1685 | case FILE_MEMLIST: | |
1686 | s += cpuset_sprintf_memlist(s, cs); | |
1687 | break; | |
1da177e4 LT |
1688 | default: |
1689 | retval = -EINVAL; | |
1690 | goto out; | |
1691 | } | |
1692 | *s++ = '\n'; | |
1da177e4 | 1693 | |
eacaa1f5 | 1694 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
1da177e4 LT |
1695 | out: |
1696 | free_page((unsigned long)page); | |
1697 | return retval; | |
1698 | } | |
1699 | ||
700fe1ab PM |
1700 | static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft) |
1701 | { | |
1702 | struct cpuset *cs = cgroup_cs(cont); | |
1703 | cpuset_filetype_t type = cft->private; | |
1704 | switch (type) { | |
1705 | case FILE_CPU_EXCLUSIVE: | |
1706 | return is_cpu_exclusive(cs); | |
1707 | case FILE_MEM_EXCLUSIVE: | |
1708 | return is_mem_exclusive(cs); | |
78608366 PM |
1709 | case FILE_MEM_HARDWALL: |
1710 | return is_mem_hardwall(cs); | |
700fe1ab PM |
1711 | case FILE_SCHED_LOAD_BALANCE: |
1712 | return is_sched_load_balance(cs); | |
1713 | case FILE_MEMORY_MIGRATE: | |
1714 | return is_memory_migrate(cs); | |
1715 | case FILE_MEMORY_PRESSURE_ENABLED: | |
1716 | return cpuset_memory_pressure_enabled; | |
1717 | case FILE_MEMORY_PRESSURE: | |
1718 | return fmeter_getrate(&cs->fmeter); | |
1719 | case FILE_SPREAD_PAGE: | |
1720 | return is_spread_page(cs); | |
1721 | case FILE_SPREAD_SLAB: | |
1722 | return is_spread_slab(cs); | |
1723 | default: | |
1724 | BUG(); | |
1725 | } | |
cf417141 MK |
1726 | |
1727 | /* Unreachable but makes gcc happy */ | |
1728 | return 0; | |
700fe1ab | 1729 | } |
1da177e4 | 1730 | |
5be7a479 PM |
1731 | static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft) |
1732 | { | |
1733 | struct cpuset *cs = cgroup_cs(cont); | |
1734 | cpuset_filetype_t type = cft->private; | |
1735 | switch (type) { | |
1736 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: | |
1737 | return cs->relax_domain_level; | |
1738 | default: | |
1739 | BUG(); | |
1740 | } | |
cf417141 MK |
1741 | |
1742 | /* Unrechable but makes gcc happy */ | |
1743 | return 0; | |
5be7a479 PM |
1744 | } |
1745 | ||
1da177e4 LT |
1746 | |
1747 | /* | |
1748 | * for the common functions, 'private' gives the type of file | |
1749 | */ | |
1750 | ||
addf2c73 PM |
1751 | static struct cftype files[] = { |
1752 | { | |
1753 | .name = "cpus", | |
1754 | .read = cpuset_common_file_read, | |
e3712395 PM |
1755 | .write_string = cpuset_write_resmask, |
1756 | .max_write_len = (100U + 6 * NR_CPUS), | |
addf2c73 PM |
1757 | .private = FILE_CPULIST, |
1758 | }, | |
1759 | ||
1760 | { | |
1761 | .name = "mems", | |
1762 | .read = cpuset_common_file_read, | |
e3712395 PM |
1763 | .write_string = cpuset_write_resmask, |
1764 | .max_write_len = (100U + 6 * MAX_NUMNODES), | |
addf2c73 PM |
1765 | .private = FILE_MEMLIST, |
1766 | }, | |
1767 | ||
1768 | { | |
1769 | .name = "cpu_exclusive", | |
1770 | .read_u64 = cpuset_read_u64, | |
1771 | .write_u64 = cpuset_write_u64, | |
1772 | .private = FILE_CPU_EXCLUSIVE, | |
1773 | }, | |
1774 | ||
1775 | { | |
1776 | .name = "mem_exclusive", | |
1777 | .read_u64 = cpuset_read_u64, | |
1778 | .write_u64 = cpuset_write_u64, | |
1779 | .private = FILE_MEM_EXCLUSIVE, | |
1780 | }, | |
1781 | ||
78608366 PM |
1782 | { |
1783 | .name = "mem_hardwall", | |
1784 | .read_u64 = cpuset_read_u64, | |
1785 | .write_u64 = cpuset_write_u64, | |
1786 | .private = FILE_MEM_HARDWALL, | |
1787 | }, | |
1788 | ||
addf2c73 PM |
1789 | { |
1790 | .name = "sched_load_balance", | |
1791 | .read_u64 = cpuset_read_u64, | |
1792 | .write_u64 = cpuset_write_u64, | |
1793 | .private = FILE_SCHED_LOAD_BALANCE, | |
1794 | }, | |
1795 | ||
1796 | { | |
1797 | .name = "sched_relax_domain_level", | |
5be7a479 PM |
1798 | .read_s64 = cpuset_read_s64, |
1799 | .write_s64 = cpuset_write_s64, | |
addf2c73 PM |
1800 | .private = FILE_SCHED_RELAX_DOMAIN_LEVEL, |
1801 | }, | |
1802 | ||
1803 | { | |
1804 | .name = "memory_migrate", | |
1805 | .read_u64 = cpuset_read_u64, | |
1806 | .write_u64 = cpuset_write_u64, | |
1807 | .private = FILE_MEMORY_MIGRATE, | |
1808 | }, | |
1809 | ||
1810 | { | |
1811 | .name = "memory_pressure", | |
1812 | .read_u64 = cpuset_read_u64, | |
1813 | .write_u64 = cpuset_write_u64, | |
1814 | .private = FILE_MEMORY_PRESSURE, | |
099fca32 | 1815 | .mode = S_IRUGO, |
addf2c73 PM |
1816 | }, |
1817 | ||
1818 | { | |
1819 | .name = "memory_spread_page", | |
1820 | .read_u64 = cpuset_read_u64, | |
1821 | .write_u64 = cpuset_write_u64, | |
1822 | .private = FILE_SPREAD_PAGE, | |
1823 | }, | |
1824 | ||
1825 | { | |
1826 | .name = "memory_spread_slab", | |
1827 | .read_u64 = cpuset_read_u64, | |
1828 | .write_u64 = cpuset_write_u64, | |
1829 | .private = FILE_SPREAD_SLAB, | |
1830 | }, | |
3e0d98b9 | 1831 | |
4baf6e33 TH |
1832 | { |
1833 | .name = "memory_pressure_enabled", | |
1834 | .flags = CFTYPE_ONLY_ON_ROOT, | |
1835 | .read_u64 = cpuset_read_u64, | |
1836 | .write_u64 = cpuset_write_u64, | |
1837 | .private = FILE_MEMORY_PRESSURE_ENABLED, | |
1838 | }, | |
1da177e4 | 1839 | |
4baf6e33 TH |
1840 | { } /* terminate */ |
1841 | }; | |
1da177e4 LT |
1842 | |
1843 | /* | |
92fb9748 | 1844 | * cpuset_css_alloc - allocate a cpuset css |
2df167a3 | 1845 | * cont: control group that the new cpuset will be part of |
1da177e4 LT |
1846 | */ |
1847 | ||
92fb9748 | 1848 | static struct cgroup_subsys_state *cpuset_css_alloc(struct cgroup *cont) |
1da177e4 | 1849 | { |
c8f699bb | 1850 | struct cpuset *cs; |
1da177e4 | 1851 | |
c8f699bb | 1852 | if (!cont->parent) |
8793d854 | 1853 | return &top_cpuset.css; |
033fa1c5 | 1854 | |
c8f699bb | 1855 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
1da177e4 | 1856 | if (!cs) |
8793d854 | 1857 | return ERR_PTR(-ENOMEM); |
300ed6cb LZ |
1858 | if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) { |
1859 | kfree(cs); | |
1860 | return ERR_PTR(-ENOMEM); | |
1861 | } | |
1da177e4 | 1862 | |
029190c5 | 1863 | set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
300ed6cb | 1864 | cpumask_clear(cs->cpus_allowed); |
f9a86fcb | 1865 | nodes_clear(cs->mems_allowed); |
3e0d98b9 | 1866 | fmeter_init(&cs->fmeter); |
8d033948 | 1867 | INIT_WORK(&cs->hotplug_work, cpuset_propagate_hotplug_workfn); |
1d3504fc | 1868 | cs->relax_domain_level = -1; |
c8f699bb TH |
1869 | cs->parent = cgroup_cs(cont->parent); |
1870 | ||
1871 | return &cs->css; | |
1872 | } | |
1873 | ||
1874 | static int cpuset_css_online(struct cgroup *cgrp) | |
1875 | { | |
1876 | struct cpuset *cs = cgroup_cs(cgrp); | |
1877 | struct cpuset *parent = cs->parent; | |
ae8086ce TH |
1878 | struct cpuset *tmp_cs; |
1879 | struct cgroup *pos_cg; | |
c8f699bb TH |
1880 | |
1881 | if (!parent) | |
1882 | return 0; | |
1883 | ||
5d21cc2d TH |
1884 | mutex_lock(&cpuset_mutex); |
1885 | ||
efeb77b2 | 1886 | set_bit(CS_ONLINE, &cs->flags); |
c8f699bb TH |
1887 | if (is_spread_page(parent)) |
1888 | set_bit(CS_SPREAD_PAGE, &cs->flags); | |
1889 | if (is_spread_slab(parent)) | |
1890 | set_bit(CS_SPREAD_SLAB, &cs->flags); | |
1da177e4 | 1891 | |
202f72d5 | 1892 | number_of_cpusets++; |
033fa1c5 | 1893 | |
c8f699bb | 1894 | if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags)) |
5d21cc2d | 1895 | goto out_unlock; |
033fa1c5 TH |
1896 | |
1897 | /* | |
1898 | * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is | |
1899 | * set. This flag handling is implemented in cgroup core for | |
1900 | * histrical reasons - the flag may be specified during mount. | |
1901 | * | |
1902 | * Currently, if any sibling cpusets have exclusive cpus or mem, we | |
1903 | * refuse to clone the configuration - thereby refusing the task to | |
1904 | * be entered, and as a result refusing the sys_unshare() or | |
1905 | * clone() which initiated it. If this becomes a problem for some | |
1906 | * users who wish to allow that scenario, then this could be | |
1907 | * changed to grant parent->cpus_allowed-sibling_cpus_exclusive | |
1908 | * (and likewise for mems) to the new cgroup. | |
1909 | */ | |
ae8086ce TH |
1910 | rcu_read_lock(); |
1911 | cpuset_for_each_child(tmp_cs, pos_cg, parent) { | |
1912 | if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) { | |
1913 | rcu_read_unlock(); | |
5d21cc2d | 1914 | goto out_unlock; |
ae8086ce | 1915 | } |
033fa1c5 | 1916 | } |
ae8086ce | 1917 | rcu_read_unlock(); |
033fa1c5 TH |
1918 | |
1919 | mutex_lock(&callback_mutex); | |
1920 | cs->mems_allowed = parent->mems_allowed; | |
1921 | cpumask_copy(cs->cpus_allowed, parent->cpus_allowed); | |
1922 | mutex_unlock(&callback_mutex); | |
5d21cc2d TH |
1923 | out_unlock: |
1924 | mutex_unlock(&cpuset_mutex); | |
c8f699bb TH |
1925 | return 0; |
1926 | } | |
1927 | ||
1928 | static void cpuset_css_offline(struct cgroup *cgrp) | |
1929 | { | |
1930 | struct cpuset *cs = cgroup_cs(cgrp); | |
1931 | ||
5d21cc2d | 1932 | mutex_lock(&cpuset_mutex); |
c8f699bb TH |
1933 | |
1934 | if (is_sched_load_balance(cs)) | |
1935 | update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); | |
1936 | ||
1937 | number_of_cpusets--; | |
efeb77b2 | 1938 | clear_bit(CS_ONLINE, &cs->flags); |
c8f699bb | 1939 | |
5d21cc2d | 1940 | mutex_unlock(&cpuset_mutex); |
1da177e4 LT |
1941 | } |
1942 | ||
029190c5 | 1943 | /* |
029190c5 PJ |
1944 | * If the cpuset being removed has its flag 'sched_load_balance' |
1945 | * enabled, then simulate turning sched_load_balance off, which | |
699140ba | 1946 | * will call rebuild_sched_domains_locked(). |
029190c5 PJ |
1947 | */ |
1948 | ||
92fb9748 | 1949 | static void cpuset_css_free(struct cgroup *cont) |
1da177e4 | 1950 | { |
8793d854 | 1951 | struct cpuset *cs = cgroup_cs(cont); |
1da177e4 | 1952 | |
300ed6cb | 1953 | free_cpumask_var(cs->cpus_allowed); |
8793d854 | 1954 | kfree(cs); |
1da177e4 LT |
1955 | } |
1956 | ||
8793d854 PM |
1957 | struct cgroup_subsys cpuset_subsys = { |
1958 | .name = "cpuset", | |
92fb9748 | 1959 | .css_alloc = cpuset_css_alloc, |
c8f699bb TH |
1960 | .css_online = cpuset_css_online, |
1961 | .css_offline = cpuset_css_offline, | |
92fb9748 | 1962 | .css_free = cpuset_css_free, |
8793d854 | 1963 | .can_attach = cpuset_can_attach, |
452477fa | 1964 | .cancel_attach = cpuset_cancel_attach, |
8793d854 | 1965 | .attach = cpuset_attach, |
8793d854 | 1966 | .subsys_id = cpuset_subsys_id, |
4baf6e33 | 1967 | .base_cftypes = files, |
8793d854 PM |
1968 | .early_init = 1, |
1969 | }; | |
1970 | ||
1da177e4 LT |
1971 | /** |
1972 | * cpuset_init - initialize cpusets at system boot | |
1973 | * | |
1974 | * Description: Initialize top_cpuset and the cpuset internal file system, | |
1975 | **/ | |
1976 | ||
1977 | int __init cpuset_init(void) | |
1978 | { | |
8793d854 | 1979 | int err = 0; |
1da177e4 | 1980 | |
58568d2a MX |
1981 | if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)) |
1982 | BUG(); | |
1983 | ||
300ed6cb | 1984 | cpumask_setall(top_cpuset.cpus_allowed); |
f9a86fcb | 1985 | nodes_setall(top_cpuset.mems_allowed); |
1da177e4 | 1986 | |
3e0d98b9 | 1987 | fmeter_init(&top_cpuset.fmeter); |
029190c5 | 1988 | set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); |
1d3504fc | 1989 | top_cpuset.relax_domain_level = -1; |
1da177e4 | 1990 | |
1da177e4 LT |
1991 | err = register_filesystem(&cpuset_fs_type); |
1992 | if (err < 0) | |
8793d854 PM |
1993 | return err; |
1994 | ||
2341d1b6 LZ |
1995 | if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)) |
1996 | BUG(); | |
1997 | ||
202f72d5 | 1998 | number_of_cpusets = 1; |
8793d854 | 1999 | return 0; |
1da177e4 LT |
2000 | } |
2001 | ||
956db3ca CW |
2002 | /** |
2003 | * cpuset_do_move_task - move a given task to another cpuset | |
2004 | * @tsk: pointer to task_struct the task to move | |
2005 | * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner | |
2006 | * | |
2007 | * Called by cgroup_scan_tasks() for each task in a cgroup. | |
2008 | * Return nonzero to stop the walk through the tasks. | |
2009 | */ | |
9e0c914c AB |
2010 | static void cpuset_do_move_task(struct task_struct *tsk, |
2011 | struct cgroup_scanner *scan) | |
956db3ca | 2012 | { |
7f81b1ae | 2013 | struct cgroup *new_cgroup = scan->data; |
956db3ca | 2014 | |
5d21cc2d | 2015 | cgroup_lock(); |
7f81b1ae | 2016 | cgroup_attach_task(new_cgroup, tsk); |
5d21cc2d | 2017 | cgroup_unlock(); |
956db3ca CW |
2018 | } |
2019 | ||
2020 | /** | |
2021 | * move_member_tasks_to_cpuset - move tasks from one cpuset to another | |
2022 | * @from: cpuset in which the tasks currently reside | |
2023 | * @to: cpuset to which the tasks will be moved | |
2024 | * | |
5d21cc2d | 2025 | * Called with cpuset_mutex held |
c8d9c90c | 2026 | * callback_mutex must not be held, as cpuset_attach() will take it. |
956db3ca CW |
2027 | * |
2028 | * The cgroup_scan_tasks() function will scan all the tasks in a cgroup, | |
2029 | * calling callback functions for each. | |
2030 | */ | |
2031 | static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to) | |
2032 | { | |
7f81b1ae | 2033 | struct cgroup_scanner scan; |
956db3ca | 2034 | |
7f81b1ae LZ |
2035 | scan.cg = from->css.cgroup; |
2036 | scan.test_task = NULL; /* select all tasks in cgroup */ | |
2037 | scan.process_task = cpuset_do_move_task; | |
2038 | scan.heap = NULL; | |
2039 | scan.data = to->css.cgroup; | |
956db3ca | 2040 | |
7f81b1ae | 2041 | if (cgroup_scan_tasks(&scan)) |
956db3ca CW |
2042 | printk(KERN_ERR "move_member_tasks_to_cpuset: " |
2043 | "cgroup_scan_tasks failed\n"); | |
2044 | } | |
2045 | ||
b1aac8bb | 2046 | /* |
cf417141 | 2047 | * If CPU and/or memory hotplug handlers, below, unplug any CPUs |
b1aac8bb PJ |
2048 | * or memory nodes, we need to walk over the cpuset hierarchy, |
2049 | * removing that CPU or node from all cpusets. If this removes the | |
956db3ca CW |
2050 | * last CPU or node from a cpuset, then move the tasks in the empty |
2051 | * cpuset to its next-highest non-empty parent. | |
b1aac8bb | 2052 | */ |
956db3ca CW |
2053 | static void remove_tasks_in_empty_cpuset(struct cpuset *cs) |
2054 | { | |
2055 | struct cpuset *parent; | |
2056 | ||
956db3ca CW |
2057 | /* |
2058 | * Find its next-highest non-empty parent, (top cpuset | |
2059 | * has online cpus, so can't be empty). | |
2060 | */ | |
2061 | parent = cs->parent; | |
300ed6cb | 2062 | while (cpumask_empty(parent->cpus_allowed) || |
b4501295 | 2063 | nodes_empty(parent->mems_allowed)) |
956db3ca | 2064 | parent = parent->parent; |
956db3ca CW |
2065 | |
2066 | move_member_tasks_to_cpuset(cs, parent); | |
2067 | } | |
2068 | ||
deb7aa30 | 2069 | /** |
8d033948 | 2070 | * cpuset_propagate_hotplug_workfn - propagate CPU/memory hotplug to a cpuset |
deb7aa30 | 2071 | * @cs: cpuset in interest |
956db3ca | 2072 | * |
deb7aa30 TH |
2073 | * Compare @cs's cpu and mem masks against top_cpuset and if some have gone |
2074 | * offline, update @cs accordingly. If @cs ends up with no CPU or memory, | |
2075 | * all its tasks are moved to the nearest ancestor with both resources. | |
956db3ca | 2076 | */ |
8d033948 | 2077 | static void cpuset_propagate_hotplug_workfn(struct work_struct *work) |
b1aac8bb | 2078 | { |
deb7aa30 TH |
2079 | static cpumask_t off_cpus; |
2080 | static nodemask_t off_mems, tmp_mems; | |
8d033948 | 2081 | struct cpuset *cs = container_of(work, struct cpuset, hotplug_work); |
5d21cc2d | 2082 | bool is_empty; |
7ddf96b0 | 2083 | |
5d21cc2d | 2084 | mutex_lock(&cpuset_mutex); |
7ddf96b0 | 2085 | |
deb7aa30 TH |
2086 | cpumask_andnot(&off_cpus, cs->cpus_allowed, top_cpuset.cpus_allowed); |
2087 | nodes_andnot(off_mems, cs->mems_allowed, top_cpuset.mems_allowed); | |
7ddf96b0 | 2088 | |
deb7aa30 TH |
2089 | /* remove offline cpus from @cs */ |
2090 | if (!cpumask_empty(&off_cpus)) { | |
2091 | mutex_lock(&callback_mutex); | |
2092 | cpumask_andnot(cs->cpus_allowed, cs->cpus_allowed, &off_cpus); | |
2093 | mutex_unlock(&callback_mutex); | |
2094 | update_tasks_cpumask(cs, NULL); | |
2095 | } | |
b4501295 | 2096 | |
deb7aa30 TH |
2097 | /* remove offline mems from @cs */ |
2098 | if (!nodes_empty(off_mems)) { | |
2099 | tmp_mems = cs->mems_allowed; | |
2100 | mutex_lock(&callback_mutex); | |
2101 | nodes_andnot(cs->mems_allowed, cs->mems_allowed, off_mems); | |
2102 | mutex_unlock(&callback_mutex); | |
2103 | update_tasks_nodemask(cs, &tmp_mems, NULL); | |
b1aac8bb | 2104 | } |
deb7aa30 | 2105 | |
5d21cc2d TH |
2106 | is_empty = cpumask_empty(cs->cpus_allowed) || |
2107 | nodes_empty(cs->mems_allowed); | |
8d033948 | 2108 | |
5d21cc2d TH |
2109 | mutex_unlock(&cpuset_mutex); |
2110 | ||
2111 | /* | |
2112 | * If @cs became empty, move tasks to the nearest ancestor with | |
2113 | * execution resources. This is full cgroup operation which will | |
2114 | * also call back into cpuset. Should be done outside any lock. | |
2115 | */ | |
2116 | if (is_empty) | |
2117 | remove_tasks_in_empty_cpuset(cs); | |
8d033948 TH |
2118 | |
2119 | /* the following may free @cs, should be the last operation */ | |
2120 | css_put(&cs->css); | |
2121 | } | |
2122 | ||
2123 | /** | |
2124 | * schedule_cpuset_propagate_hotplug - schedule hotplug propagation to a cpuset | |
2125 | * @cs: cpuset of interest | |
2126 | * | |
2127 | * Schedule cpuset_propagate_hotplug_workfn() which will update CPU and | |
2128 | * memory masks according to top_cpuset. | |
2129 | */ | |
2130 | static void schedule_cpuset_propagate_hotplug(struct cpuset *cs) | |
2131 | { | |
2132 | /* | |
2133 | * Pin @cs. The refcnt will be released when the work item | |
2134 | * finishes executing. | |
2135 | */ | |
2136 | if (!css_tryget(&cs->css)) | |
2137 | return; | |
2138 | ||
2139 | /* | |
2140 | * Queue @cs->hotplug_work. If already pending, lose the css ref. | |
2141 | * cpuset_propagate_hotplug_wq is ordered and propagation will | |
2142 | * happen in the order this function is called. | |
2143 | */ | |
2144 | if (!queue_work(cpuset_propagate_hotplug_wq, &cs->hotplug_work)) | |
2145 | css_put(&cs->css); | |
b1aac8bb PJ |
2146 | } |
2147 | ||
deb7aa30 | 2148 | /** |
3a5a6d0c | 2149 | * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset |
d35be8ba | 2150 | * |
deb7aa30 TH |
2151 | * This function is called after either CPU or memory configuration has |
2152 | * changed and updates cpuset accordingly. The top_cpuset is always | |
2153 | * synchronized to cpu_active_mask and N_MEMORY, which is necessary in | |
2154 | * order to make cpusets transparent (of no affect) on systems that are | |
2155 | * actively using CPU hotplug but making no active use of cpusets. | |
cf417141 | 2156 | * |
deb7aa30 TH |
2157 | * Non-root cpusets are only affected by offlining. If any CPUs or memory |
2158 | * nodes have been taken down, cpuset_propagate_hotplug() is invoked on all | |
2159 | * descendants. | |
7ddf96b0 | 2160 | * |
deb7aa30 TH |
2161 | * Note that CPU offlining during suspend is ignored. We don't modify |
2162 | * cpusets across suspend/resume cycles at all. | |
4c4d50f7 | 2163 | */ |
3a5a6d0c | 2164 | static void cpuset_hotplug_workfn(struct work_struct *work) |
4c4d50f7 | 2165 | { |
deb7aa30 TH |
2166 | static cpumask_t new_cpus, tmp_cpus; |
2167 | static nodemask_t new_mems, tmp_mems; | |
2168 | bool cpus_updated, mems_updated; | |
2169 | bool cpus_offlined, mems_offlined; | |
cf417141 | 2170 | |
5d21cc2d | 2171 | mutex_lock(&cpuset_mutex); |
7ddf96b0 | 2172 | |
deb7aa30 TH |
2173 | /* fetch the available cpus/mems and find out which changed how */ |
2174 | cpumask_copy(&new_cpus, cpu_active_mask); | |
2175 | new_mems = node_states[N_MEMORY]; | |
2176 | ||
2177 | cpus_updated = !cpumask_equal(top_cpuset.cpus_allowed, &new_cpus); | |
2178 | cpus_offlined = cpumask_andnot(&tmp_cpus, top_cpuset.cpus_allowed, | |
2179 | &new_cpus); | |
2180 | ||
2181 | mems_updated = !nodes_equal(top_cpuset.mems_allowed, new_mems); | |
2182 | nodes_andnot(tmp_mems, top_cpuset.mems_allowed, new_mems); | |
2183 | mems_offlined = !nodes_empty(tmp_mems); | |
2184 | ||
2185 | /* synchronize cpus_allowed to cpu_active_mask */ | |
2186 | if (cpus_updated) { | |
2187 | mutex_lock(&callback_mutex); | |
2188 | cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); | |
2189 | mutex_unlock(&callback_mutex); | |
2190 | /* we don't mess with cpumasks of tasks in top_cpuset */ | |
2191 | } | |
2192 | ||
2193 | /* synchronize mems_allowed to N_MEMORY */ | |
2194 | if (mems_updated) { | |
2195 | tmp_mems = top_cpuset.mems_allowed; | |
2196 | mutex_lock(&callback_mutex); | |
2197 | top_cpuset.mems_allowed = new_mems; | |
2198 | mutex_unlock(&callback_mutex); | |
2199 | update_tasks_nodemask(&top_cpuset, &tmp_mems, NULL); | |
2200 | } | |
2201 | ||
2202 | /* if cpus or mems went down, we need to propagate to descendants */ | |
2203 | if (cpus_offlined || mems_offlined) { | |
2204 | struct cpuset *cs; | |
fc560a26 | 2205 | struct cgroup *pos_cgrp; |
deb7aa30 | 2206 | |
fc560a26 TH |
2207 | rcu_read_lock(); |
2208 | cpuset_for_each_descendant_pre(cs, pos_cgrp, &top_cpuset) | |
2209 | schedule_cpuset_propagate_hotplug(cs); | |
2210 | rcu_read_unlock(); | |
deb7aa30 | 2211 | } |
7ddf96b0 | 2212 | |
5d21cc2d | 2213 | mutex_unlock(&cpuset_mutex); |
cf417141 | 2214 | |
8d033948 TH |
2215 | /* wait for propagations to finish */ |
2216 | flush_workqueue(cpuset_propagate_hotplug_wq); | |
2217 | ||
deb7aa30 TH |
2218 | /* rebuild sched domains if cpus_allowed has changed */ |
2219 | if (cpus_updated) { | |
2220 | struct sched_domain_attr *attr; | |
2221 | cpumask_var_t *doms; | |
2222 | int ndoms; | |
2223 | ||
5d21cc2d | 2224 | mutex_lock(&cpuset_mutex); |
deb7aa30 | 2225 | ndoms = generate_sched_domains(&doms, &attr); |
5d21cc2d | 2226 | mutex_unlock(&cpuset_mutex); |
deb7aa30 TH |
2227 | |
2228 | partition_sched_domains(ndoms, doms, attr); | |
2229 | } | |
2230 | } | |
2231 | ||
2232 | void cpuset_update_active_cpus(bool cpu_online) | |
2233 | { | |
3a5a6d0c TH |
2234 | /* |
2235 | * We're inside cpu hotplug critical region which usually nests | |
2236 | * inside cgroup synchronization. Bounce actual hotplug processing | |
2237 | * to a work item to avoid reverse locking order. | |
2238 | * | |
2239 | * We still need to do partition_sched_domains() synchronously; | |
2240 | * otherwise, the scheduler will get confused and put tasks to the | |
2241 | * dead CPU. Fall back to the default single domain. | |
2242 | * cpuset_hotplug_workfn() will rebuild it as necessary. | |
2243 | */ | |
2244 | partition_sched_domains(1, NULL, NULL); | |
2245 | schedule_work(&cpuset_hotplug_work); | |
4c4d50f7 | 2246 | } |
4c4d50f7 | 2247 | |
b1aac8bb | 2248 | #ifdef CONFIG_MEMORY_HOTPLUG |
38837fc7 | 2249 | /* |
38d7bee9 LJ |
2250 | * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. |
2251 | * Call this routine anytime after node_states[N_MEMORY] changes. | |
a1cd2b13 | 2252 | * See cpuset_update_active_cpus() for CPU hotplug handling. |
38837fc7 | 2253 | */ |
f481891f MX |
2254 | static int cpuset_track_online_nodes(struct notifier_block *self, |
2255 | unsigned long action, void *arg) | |
38837fc7 | 2256 | { |
3a5a6d0c | 2257 | schedule_work(&cpuset_hotplug_work); |
f481891f | 2258 | return NOTIFY_OK; |
38837fc7 PJ |
2259 | } |
2260 | #endif | |
2261 | ||
1da177e4 LT |
2262 | /** |
2263 | * cpuset_init_smp - initialize cpus_allowed | |
2264 | * | |
2265 | * Description: Finish top cpuset after cpu, node maps are initialized | |
2266 | **/ | |
2267 | ||
2268 | void __init cpuset_init_smp(void) | |
2269 | { | |
6ad4c188 | 2270 | cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask); |
38d7bee9 | 2271 | top_cpuset.mems_allowed = node_states[N_MEMORY]; |
4c4d50f7 | 2272 | |
f481891f | 2273 | hotplug_memory_notifier(cpuset_track_online_nodes, 10); |
8d033948 TH |
2274 | |
2275 | cpuset_propagate_hotplug_wq = | |
2276 | alloc_ordered_workqueue("cpuset_hotplug", 0); | |
2277 | BUG_ON(!cpuset_propagate_hotplug_wq); | |
1da177e4 LT |
2278 | } |
2279 | ||
2280 | /** | |
1da177e4 LT |
2281 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
2282 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. | |
6af866af | 2283 | * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. |
1da177e4 | 2284 | * |
300ed6cb | 2285 | * Description: Returns the cpumask_var_t cpus_allowed of the cpuset |
1da177e4 | 2286 | * attached to the specified @tsk. Guaranteed to return some non-empty |
5f054e31 | 2287 | * subset of cpu_online_mask, even if this means going outside the |
1da177e4 LT |
2288 | * tasks cpuset. |
2289 | **/ | |
2290 | ||
6af866af | 2291 | void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) |
1da177e4 | 2292 | { |
3d3f26a7 | 2293 | mutex_lock(&callback_mutex); |
909d75a3 | 2294 | task_lock(tsk); |
f9a86fcb | 2295 | guarantee_online_cpus(task_cs(tsk), pmask); |
909d75a3 | 2296 | task_unlock(tsk); |
897f0b3c | 2297 | mutex_unlock(&callback_mutex); |
1da177e4 LT |
2298 | } |
2299 | ||
2baab4e9 | 2300 | void cpuset_cpus_allowed_fallback(struct task_struct *tsk) |
9084bb82 ON |
2301 | { |
2302 | const struct cpuset *cs; | |
9084bb82 ON |
2303 | |
2304 | rcu_read_lock(); | |
2305 | cs = task_cs(tsk); | |
2306 | if (cs) | |
1e1b6c51 | 2307 | do_set_cpus_allowed(tsk, cs->cpus_allowed); |
9084bb82 ON |
2308 | rcu_read_unlock(); |
2309 | ||
2310 | /* | |
2311 | * We own tsk->cpus_allowed, nobody can change it under us. | |
2312 | * | |
2313 | * But we used cs && cs->cpus_allowed lockless and thus can | |
2314 | * race with cgroup_attach_task() or update_cpumask() and get | |
2315 | * the wrong tsk->cpus_allowed. However, both cases imply the | |
2316 | * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() | |
2317 | * which takes task_rq_lock(). | |
2318 | * | |
2319 | * If we are called after it dropped the lock we must see all | |
2320 | * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary | |
2321 | * set any mask even if it is not right from task_cs() pov, | |
2322 | * the pending set_cpus_allowed_ptr() will fix things. | |
2baab4e9 PZ |
2323 | * |
2324 | * select_fallback_rq() will fix things ups and set cpu_possible_mask | |
2325 | * if required. | |
9084bb82 | 2326 | */ |
9084bb82 ON |
2327 | } |
2328 | ||
1da177e4 LT |
2329 | void cpuset_init_current_mems_allowed(void) |
2330 | { | |
f9a86fcb | 2331 | nodes_setall(current->mems_allowed); |
1da177e4 LT |
2332 | } |
2333 | ||
909d75a3 PJ |
2334 | /** |
2335 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. | |
2336 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. | |
2337 | * | |
2338 | * Description: Returns the nodemask_t mems_allowed of the cpuset | |
2339 | * attached to the specified @tsk. Guaranteed to return some non-empty | |
38d7bee9 | 2340 | * subset of node_states[N_MEMORY], even if this means going outside the |
909d75a3 PJ |
2341 | * tasks cpuset. |
2342 | **/ | |
2343 | ||
2344 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) | |
2345 | { | |
2346 | nodemask_t mask; | |
2347 | ||
3d3f26a7 | 2348 | mutex_lock(&callback_mutex); |
909d75a3 | 2349 | task_lock(tsk); |
8793d854 | 2350 | guarantee_online_mems(task_cs(tsk), &mask); |
909d75a3 | 2351 | task_unlock(tsk); |
3d3f26a7 | 2352 | mutex_unlock(&callback_mutex); |
909d75a3 PJ |
2353 | |
2354 | return mask; | |
2355 | } | |
2356 | ||
d9fd8a6d | 2357 | /** |
19770b32 MG |
2358 | * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed |
2359 | * @nodemask: the nodemask to be checked | |
d9fd8a6d | 2360 | * |
19770b32 | 2361 | * Are any of the nodes in the nodemask allowed in current->mems_allowed? |
1da177e4 | 2362 | */ |
19770b32 | 2363 | int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask) |
1da177e4 | 2364 | { |
19770b32 | 2365 | return nodes_intersects(*nodemask, current->mems_allowed); |
1da177e4 LT |
2366 | } |
2367 | ||
9bf2229f | 2368 | /* |
78608366 PM |
2369 | * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or |
2370 | * mem_hardwall ancestor to the specified cpuset. Call holding | |
2371 | * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall | |
2372 | * (an unusual configuration), then returns the root cpuset. | |
9bf2229f | 2373 | */ |
78608366 | 2374 | static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs) |
9bf2229f | 2375 | { |
78608366 | 2376 | while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent) |
9bf2229f PJ |
2377 | cs = cs->parent; |
2378 | return cs; | |
2379 | } | |
2380 | ||
d9fd8a6d | 2381 | /** |
a1bc5a4e DR |
2382 | * cpuset_node_allowed_softwall - Can we allocate on a memory node? |
2383 | * @node: is this an allowed node? | |
02a0e53d | 2384 | * @gfp_mask: memory allocation flags |
d9fd8a6d | 2385 | * |
a1bc5a4e DR |
2386 | * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is |
2387 | * set, yes, we can always allocate. If node is in our task's mems_allowed, | |
2388 | * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest | |
2389 | * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been | |
2390 | * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE | |
2391 | * flag, yes. | |
9bf2229f PJ |
2392 | * Otherwise, no. |
2393 | * | |
a1bc5a4e DR |
2394 | * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to |
2395 | * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall() | |
2396 | * might sleep, and might allow a node from an enclosing cpuset. | |
02a0e53d | 2397 | * |
a1bc5a4e DR |
2398 | * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall |
2399 | * cpusets, and never sleeps. | |
02a0e53d PJ |
2400 | * |
2401 | * The __GFP_THISNODE placement logic is really handled elsewhere, | |
2402 | * by forcibly using a zonelist starting at a specified node, and by | |
2403 | * (in get_page_from_freelist()) refusing to consider the zones for | |
2404 | * any node on the zonelist except the first. By the time any such | |
2405 | * calls get to this routine, we should just shut up and say 'yes'. | |
2406 | * | |
9bf2229f | 2407 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
c596d9f3 DR |
2408 | * and do not allow allocations outside the current tasks cpuset |
2409 | * unless the task has been OOM killed as is marked TIF_MEMDIE. | |
9bf2229f | 2410 | * GFP_KERNEL allocations are not so marked, so can escape to the |
78608366 | 2411 | * nearest enclosing hardwalled ancestor cpuset. |
9bf2229f | 2412 | * |
02a0e53d PJ |
2413 | * Scanning up parent cpusets requires callback_mutex. The |
2414 | * __alloc_pages() routine only calls here with __GFP_HARDWALL bit | |
2415 | * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the | |
2416 | * current tasks mems_allowed came up empty on the first pass over | |
2417 | * the zonelist. So only GFP_KERNEL allocations, if all nodes in the | |
2418 | * cpuset are short of memory, might require taking the callback_mutex | |
2419 | * mutex. | |
9bf2229f | 2420 | * |
36be57ff | 2421 | * The first call here from mm/page_alloc:get_page_from_freelist() |
02a0e53d PJ |
2422 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, |
2423 | * so no allocation on a node outside the cpuset is allowed (unless | |
2424 | * in interrupt, of course). | |
36be57ff PJ |
2425 | * |
2426 | * The second pass through get_page_from_freelist() doesn't even call | |
2427 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() | |
2428 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set | |
2429 | * in alloc_flags. That logic and the checks below have the combined | |
2430 | * affect that: | |
9bf2229f PJ |
2431 | * in_interrupt - any node ok (current task context irrelevant) |
2432 | * GFP_ATOMIC - any node ok | |
c596d9f3 | 2433 | * TIF_MEMDIE - any node ok |
78608366 | 2434 | * GFP_KERNEL - any node in enclosing hardwalled cpuset ok |
9bf2229f | 2435 | * GFP_USER - only nodes in current tasks mems allowed ok. |
36be57ff PJ |
2436 | * |
2437 | * Rule: | |
a1bc5a4e | 2438 | * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you |
36be57ff PJ |
2439 | * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables |
2440 | * the code that might scan up ancestor cpusets and sleep. | |
02a0e53d | 2441 | */ |
a1bc5a4e | 2442 | int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask) |
1da177e4 | 2443 | { |
9bf2229f | 2444 | const struct cpuset *cs; /* current cpuset ancestors */ |
29afd49b | 2445 | int allowed; /* is allocation in zone z allowed? */ |
9bf2229f | 2446 | |
9b819d20 | 2447 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
9bf2229f | 2448 | return 1; |
92d1dbd2 | 2449 | might_sleep_if(!(gfp_mask & __GFP_HARDWALL)); |
9bf2229f PJ |
2450 | if (node_isset(node, current->mems_allowed)) |
2451 | return 1; | |
c596d9f3 DR |
2452 | /* |
2453 | * Allow tasks that have access to memory reserves because they have | |
2454 | * been OOM killed to get memory anywhere. | |
2455 | */ | |
2456 | if (unlikely(test_thread_flag(TIF_MEMDIE))) | |
2457 | return 1; | |
9bf2229f PJ |
2458 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
2459 | return 0; | |
2460 | ||
5563e770 BP |
2461 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
2462 | return 1; | |
2463 | ||
9bf2229f | 2464 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
3d3f26a7 | 2465 | mutex_lock(&callback_mutex); |
053199ed | 2466 | |
053199ed | 2467 | task_lock(current); |
78608366 | 2468 | cs = nearest_hardwall_ancestor(task_cs(current)); |
053199ed PJ |
2469 | task_unlock(current); |
2470 | ||
9bf2229f | 2471 | allowed = node_isset(node, cs->mems_allowed); |
3d3f26a7 | 2472 | mutex_unlock(&callback_mutex); |
9bf2229f | 2473 | return allowed; |
1da177e4 LT |
2474 | } |
2475 | ||
02a0e53d | 2476 | /* |
a1bc5a4e DR |
2477 | * cpuset_node_allowed_hardwall - Can we allocate on a memory node? |
2478 | * @node: is this an allowed node? | |
02a0e53d PJ |
2479 | * @gfp_mask: memory allocation flags |
2480 | * | |
a1bc5a4e DR |
2481 | * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is |
2482 | * set, yes, we can always allocate. If node is in our task's mems_allowed, | |
2483 | * yes. If the task has been OOM killed and has access to memory reserves as | |
2484 | * specified by the TIF_MEMDIE flag, yes. | |
2485 | * Otherwise, no. | |
02a0e53d PJ |
2486 | * |
2487 | * The __GFP_THISNODE placement logic is really handled elsewhere, | |
2488 | * by forcibly using a zonelist starting at a specified node, and by | |
2489 | * (in get_page_from_freelist()) refusing to consider the zones for | |
2490 | * any node on the zonelist except the first. By the time any such | |
2491 | * calls get to this routine, we should just shut up and say 'yes'. | |
2492 | * | |
a1bc5a4e DR |
2493 | * Unlike the cpuset_node_allowed_softwall() variant, above, |
2494 | * this variant requires that the node be in the current task's | |
02a0e53d PJ |
2495 | * mems_allowed or that we're in interrupt. It does not scan up the |
2496 | * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset. | |
2497 | * It never sleeps. | |
2498 | */ | |
a1bc5a4e | 2499 | int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask) |
02a0e53d | 2500 | { |
02a0e53d PJ |
2501 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
2502 | return 1; | |
02a0e53d PJ |
2503 | if (node_isset(node, current->mems_allowed)) |
2504 | return 1; | |
dedf8b79 DW |
2505 | /* |
2506 | * Allow tasks that have access to memory reserves because they have | |
2507 | * been OOM killed to get memory anywhere. | |
2508 | */ | |
2509 | if (unlikely(test_thread_flag(TIF_MEMDIE))) | |
2510 | return 1; | |
02a0e53d PJ |
2511 | return 0; |
2512 | } | |
2513 | ||
825a46af | 2514 | /** |
6adef3eb JS |
2515 | * cpuset_mem_spread_node() - On which node to begin search for a file page |
2516 | * cpuset_slab_spread_node() - On which node to begin search for a slab page | |
825a46af PJ |
2517 | * |
2518 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for | |
2519 | * tasks in a cpuset with is_spread_page or is_spread_slab set), | |
2520 | * and if the memory allocation used cpuset_mem_spread_node() | |
2521 | * to determine on which node to start looking, as it will for | |
2522 | * certain page cache or slab cache pages such as used for file | |
2523 | * system buffers and inode caches, then instead of starting on the | |
2524 | * local node to look for a free page, rather spread the starting | |
2525 | * node around the tasks mems_allowed nodes. | |
2526 | * | |
2527 | * We don't have to worry about the returned node being offline | |
2528 | * because "it can't happen", and even if it did, it would be ok. | |
2529 | * | |
2530 | * The routines calling guarantee_online_mems() are careful to | |
2531 | * only set nodes in task->mems_allowed that are online. So it | |
2532 | * should not be possible for the following code to return an | |
2533 | * offline node. But if it did, that would be ok, as this routine | |
2534 | * is not returning the node where the allocation must be, only | |
2535 | * the node where the search should start. The zonelist passed to | |
2536 | * __alloc_pages() will include all nodes. If the slab allocator | |
2537 | * is passed an offline node, it will fall back to the local node. | |
2538 | * See kmem_cache_alloc_node(). | |
2539 | */ | |
2540 | ||
6adef3eb | 2541 | static int cpuset_spread_node(int *rotor) |
825a46af PJ |
2542 | { |
2543 | int node; | |
2544 | ||
6adef3eb | 2545 | node = next_node(*rotor, current->mems_allowed); |
825a46af PJ |
2546 | if (node == MAX_NUMNODES) |
2547 | node = first_node(current->mems_allowed); | |
6adef3eb | 2548 | *rotor = node; |
825a46af PJ |
2549 | return node; |
2550 | } | |
6adef3eb JS |
2551 | |
2552 | int cpuset_mem_spread_node(void) | |
2553 | { | |
778d3b0f MH |
2554 | if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) |
2555 | current->cpuset_mem_spread_rotor = | |
2556 | node_random(¤t->mems_allowed); | |
2557 | ||
6adef3eb JS |
2558 | return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); |
2559 | } | |
2560 | ||
2561 | int cpuset_slab_spread_node(void) | |
2562 | { | |
778d3b0f MH |
2563 | if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE) |
2564 | current->cpuset_slab_spread_rotor = | |
2565 | node_random(¤t->mems_allowed); | |
2566 | ||
6adef3eb JS |
2567 | return cpuset_spread_node(¤t->cpuset_slab_spread_rotor); |
2568 | } | |
2569 | ||
825a46af PJ |
2570 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); |
2571 | ||
ef08e3b4 | 2572 | /** |
bbe373f2 DR |
2573 | * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? |
2574 | * @tsk1: pointer to task_struct of some task. | |
2575 | * @tsk2: pointer to task_struct of some other task. | |
2576 | * | |
2577 | * Description: Return true if @tsk1's mems_allowed intersects the | |
2578 | * mems_allowed of @tsk2. Used by the OOM killer to determine if | |
2579 | * one of the task's memory usage might impact the memory available | |
2580 | * to the other. | |
ef08e3b4 PJ |
2581 | **/ |
2582 | ||
bbe373f2 DR |
2583 | int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, |
2584 | const struct task_struct *tsk2) | |
ef08e3b4 | 2585 | { |
bbe373f2 | 2586 | return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); |
ef08e3b4 PJ |
2587 | } |
2588 | ||
75aa1994 DR |
2589 | /** |
2590 | * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed | |
2591 | * @task: pointer to task_struct of some task. | |
2592 | * | |
2593 | * Description: Prints @task's name, cpuset name, and cached copy of its | |
2594 | * mems_allowed to the kernel log. Must hold task_lock(task) to allow | |
2595 | * dereferencing task_cs(task). | |
2596 | */ | |
2597 | void cpuset_print_task_mems_allowed(struct task_struct *tsk) | |
2598 | { | |
2599 | struct dentry *dentry; | |
2600 | ||
2601 | dentry = task_cs(tsk)->css.cgroup->dentry; | |
2602 | spin_lock(&cpuset_buffer_lock); | |
2603 | snprintf(cpuset_name, CPUSET_NAME_LEN, | |
2604 | dentry ? (const char *)dentry->d_name.name : "/"); | |
2605 | nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN, | |
2606 | tsk->mems_allowed); | |
2607 | printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n", | |
2608 | tsk->comm, cpuset_name, cpuset_nodelist); | |
2609 | spin_unlock(&cpuset_buffer_lock); | |
2610 | } | |
2611 | ||
3e0d98b9 PJ |
2612 | /* |
2613 | * Collection of memory_pressure is suppressed unless | |
2614 | * this flag is enabled by writing "1" to the special | |
2615 | * cpuset file 'memory_pressure_enabled' in the root cpuset. | |
2616 | */ | |
2617 | ||
c5b2aff8 | 2618 | int cpuset_memory_pressure_enabled __read_mostly; |
3e0d98b9 PJ |
2619 | |
2620 | /** | |
2621 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. | |
2622 | * | |
2623 | * Keep a running average of the rate of synchronous (direct) | |
2624 | * page reclaim efforts initiated by tasks in each cpuset. | |
2625 | * | |
2626 | * This represents the rate at which some task in the cpuset | |
2627 | * ran low on memory on all nodes it was allowed to use, and | |
2628 | * had to enter the kernels page reclaim code in an effort to | |
2629 | * create more free memory by tossing clean pages or swapping | |
2630 | * or writing dirty pages. | |
2631 | * | |
2632 | * Display to user space in the per-cpuset read-only file | |
2633 | * "memory_pressure". Value displayed is an integer | |
2634 | * representing the recent rate of entry into the synchronous | |
2635 | * (direct) page reclaim by any task attached to the cpuset. | |
2636 | **/ | |
2637 | ||
2638 | void __cpuset_memory_pressure_bump(void) | |
2639 | { | |
3e0d98b9 | 2640 | task_lock(current); |
8793d854 | 2641 | fmeter_markevent(&task_cs(current)->fmeter); |
3e0d98b9 PJ |
2642 | task_unlock(current); |
2643 | } | |
2644 | ||
8793d854 | 2645 | #ifdef CONFIG_PROC_PID_CPUSET |
1da177e4 LT |
2646 | /* |
2647 | * proc_cpuset_show() | |
2648 | * - Print tasks cpuset path into seq_file. | |
2649 | * - Used for /proc/<pid>/cpuset. | |
053199ed PJ |
2650 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
2651 | * doesn't really matter if tsk->cpuset changes after we read it, | |
5d21cc2d | 2652 | * and we take cpuset_mutex, keeping cpuset_attach() from changing it |
2df167a3 | 2653 | * anyway. |
1da177e4 | 2654 | */ |
029190c5 | 2655 | static int proc_cpuset_show(struct seq_file *m, void *unused_v) |
1da177e4 | 2656 | { |
13b41b09 | 2657 | struct pid *pid; |
1da177e4 LT |
2658 | struct task_struct *tsk; |
2659 | char *buf; | |
8793d854 | 2660 | struct cgroup_subsys_state *css; |
99f89551 | 2661 | int retval; |
1da177e4 | 2662 | |
99f89551 | 2663 | retval = -ENOMEM; |
1da177e4 LT |
2664 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
2665 | if (!buf) | |
99f89551 EB |
2666 | goto out; |
2667 | ||
2668 | retval = -ESRCH; | |
13b41b09 EB |
2669 | pid = m->private; |
2670 | tsk = get_pid_task(pid, PIDTYPE_PID); | |
99f89551 EB |
2671 | if (!tsk) |
2672 | goto out_free; | |
1da177e4 | 2673 | |
99f89551 | 2674 | retval = -EINVAL; |
5d21cc2d | 2675 | mutex_lock(&cpuset_mutex); |
8793d854 PM |
2676 | css = task_subsys_state(tsk, cpuset_subsys_id); |
2677 | retval = cgroup_path(css->cgroup, buf, PAGE_SIZE); | |
1da177e4 | 2678 | if (retval < 0) |
99f89551 | 2679 | goto out_unlock; |
1da177e4 LT |
2680 | seq_puts(m, buf); |
2681 | seq_putc(m, '\n'); | |
99f89551 | 2682 | out_unlock: |
5d21cc2d | 2683 | mutex_unlock(&cpuset_mutex); |
99f89551 EB |
2684 | put_task_struct(tsk); |
2685 | out_free: | |
1da177e4 | 2686 | kfree(buf); |
99f89551 | 2687 | out: |
1da177e4 LT |
2688 | return retval; |
2689 | } | |
2690 | ||
2691 | static int cpuset_open(struct inode *inode, struct file *file) | |
2692 | { | |
13b41b09 EB |
2693 | struct pid *pid = PROC_I(inode)->pid; |
2694 | return single_open(file, proc_cpuset_show, pid); | |
1da177e4 LT |
2695 | } |
2696 | ||
9a32144e | 2697 | const struct file_operations proc_cpuset_operations = { |
1da177e4 LT |
2698 | .open = cpuset_open, |
2699 | .read = seq_read, | |
2700 | .llseek = seq_lseek, | |
2701 | .release = single_release, | |
2702 | }; | |
8793d854 | 2703 | #endif /* CONFIG_PROC_PID_CPUSET */ |
1da177e4 | 2704 | |
d01d4827 | 2705 | /* Display task mems_allowed in /proc/<pid>/status file. */ |
df5f8314 EB |
2706 | void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) |
2707 | { | |
df5f8314 | 2708 | seq_printf(m, "Mems_allowed:\t"); |
30e8e136 | 2709 | seq_nodemask(m, &task->mems_allowed); |
df5f8314 | 2710 | seq_printf(m, "\n"); |
39106dcf | 2711 | seq_printf(m, "Mems_allowed_list:\t"); |
30e8e136 | 2712 | seq_nodemask_list(m, &task->mems_allowed); |
39106dcf | 2713 | seq_printf(m, "\n"); |
1da177e4 | 2714 | } |