blob: 5372b6f5e5b3ed91224b55f41ab4d0a2ded1557c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
Paul Jackson029190c2007-10-18 23:40:20 -07007 * Copyright (C) 2004-2007 Silicon Graphics, Inc.
Paul Menage8793d852007-10-18 23:39:39 -07008 * Copyright (C) 2006 Google, Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Portions derived from Patrick Mochel's sysfs code.
11 * sysfs is Copyright (c) 2001-3 Patrick Mochel
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
Paul Jackson825a46a2006-03-24 03:16:03 -080013 * 2003-10-10 Written by Simon Derr.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 2003-10-22 Updates by Stephen Hemminger.
Paul Jackson825a46a2006-03-24 03:16:03 -080015 * 2004 May-July Rework by Paul Jackson.
Paul Menage8793d852007-10-18 23:39:39 -070016 * 2006 Rework by Paul Menage to use generic cgroups
Max Krasnyanskycf417142008-08-11 14:33:53 -070017 * 2008 Rework of the scheduler domains and CPU hotplug handling
18 * by Max Krasnyansky
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Paul Jackson68860ec2005-10-30 15:02:36 -080037#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mm.h>
Miao Xief4818912008-11-19 15:36:30 -080039#include <linux/memory.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040040#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mount.h>
42#include <linux/namei.h>
43#include <linux/pagemap.h>
44#include <linux/proc_fs.h>
Paul Jackson6b9c2602006-01-08 01:02:02 -080045#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sched.h>
47#include <linux/seq_file.h>
David Quigley22fb52d2006-06-23 02:04:00 -070048#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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>
Arun Sharma600634972011-07-26 16:09:06 -070058#include <linux/atomic.h>
Ingo Molnar3d3f26a2006-03-23 03:00:18 -080059#include <linux/mutex.h>
Cliff Wickman956db3c2008-02-07 00:14:43 -080060#include <linux/workqueue.h>
61#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Paul Jackson202f72d2006-01-08 01:01:57 -080063/*
Miao Xief90d4112009-01-16 10:24:10 +080064 * Workqueue for cpuset related tasks.
65 *
66 * Using kevent workqueue may cause deadlock when memory_migrate
67 * is set. So we create a separate workqueue thread for cpuset.
68 */
69static struct workqueue_struct *cpuset_wq;
70
71/*
Paul Jackson202f72d2006-01-08 01:01:57 -080072 * Tracks how many cpusets are currently defined in system.
73 * When there is only one cpuset (the root cpuset) we can
74 * short circuit some hooks.
75 */
Paul Jackson7edc5962006-01-08 01:02:03 -080076int number_of_cpusets __read_mostly;
Paul Jackson202f72d2006-01-08 01:01:57 -080077
Paul Menage2df167a2008-02-07 00:14:45 -080078/* Forward declare cgroup structures */
Paul Menage8793d852007-10-18 23:39:39 -070079struct cgroup_subsys cpuset_subsys;
80struct cpuset;
81
Paul Jackson3e0d98b2006-01-08 01:01:49 -080082/* See "Frequency meter" comments, below. */
83
84struct fmeter {
85 int cnt; /* unprocessed events count */
86 int val; /* most recent output value */
87 time_t time; /* clock (secs) when val computed */
88 spinlock_t lock; /* guards read or write of above */
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct cpuset {
Paul Menage8793d852007-10-18 23:39:39 -070092 struct cgroup_subsys_state css;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned long flags; /* "unsigned long" so bitops work */
Li Zefan300ed6c2009-01-07 18:08:44 -080095 cpumask_var_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct cpuset *parent; /* my parent */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800100 struct fmeter fmeter; /* memory_pressure filter */
Paul Jackson029190c2007-10-18 23:40:20 -0700101
102 /* partition number for rebuild_sched_domains() */
103 int pn;
Cliff Wickman956db3c2008-02-07 00:14:43 -0800104
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900105 /* for custom sched domain */
106 int relax_domain_level;
107
Uwe Kleine-König732bee72010-06-11 12:16:59 +0200108 /* used for walking a cpuset hierarchy */
Cliff Wickman956db3c2008-02-07 00:14:43 -0800109 struct list_head stack_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
Paul Menage8793d852007-10-18 23:39:39 -0700112/* Retrieve the cpuset for a cgroup */
113static inline struct cpuset *cgroup_cs(struct cgroup *cont)
114{
115 return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
116 struct cpuset, css);
117}
118
119/* Retrieve the cpuset for a task */
120static inline struct cpuset *task_cs(struct task_struct *task)
121{
122 return container_of(task_subsys_state(task, cpuset_subsys_id),
123 struct cpuset, css);
124}
Paul Menage8793d852007-10-18 23:39:39 -0700125
David Rientjesb2462722011-12-19 17:11:52 -0800126#ifdef CONFIG_NUMA
127static inline bool task_has_mempolicy(struct task_struct *task)
128{
129 return task->mempolicy;
130}
131#else
132static inline bool task_has_mempolicy(struct task_struct *task)
133{
134 return false;
135}
136#endif
137
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* bits in struct cpuset flags field */
140typedef enum {
141 CS_CPU_EXCLUSIVE,
142 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700143 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800144 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700145 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800146 CS_SPREAD_PAGE,
147 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148} cpuset_flagbits_t;
149
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +0530150/* the type of hotplug event */
151enum hotplug_event {
152 CPUSET_CPU_OFFLINE,
153 CPUSET_MEM_OFFLINE,
154};
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* convenient tests for these bits */
157static inline int is_cpu_exclusive(const struct cpuset *cs)
158{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800159 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static inline int is_mem_exclusive(const struct cpuset *cs)
163{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800164 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Paul Menage78608362008-04-29 01:00:26 -0700167static inline int is_mem_hardwall(const struct cpuset *cs)
168{
169 return test_bit(CS_MEM_HARDWALL, &cs->flags);
170}
171
Paul Jackson029190c2007-10-18 23:40:20 -0700172static inline int is_sched_load_balance(const struct cpuset *cs)
173{
174 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
175}
176
Paul Jackson45b07ef2006-01-08 01:00:56 -0800177static inline int is_memory_migrate(const struct cpuset *cs)
178{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800179 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800180}
181
Paul Jackson825a46a2006-03-24 03:16:03 -0800182static inline int is_spread_page(const struct cpuset *cs)
183{
184 return test_bit(CS_SPREAD_PAGE, &cs->flags);
185}
186
187static inline int is_spread_slab(const struct cpuset *cs)
188{
189 return test_bit(CS_SPREAD_SLAB, &cs->flags);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static struct cpuset top_cpuset = {
193 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
Paul Menage2df167a2008-02-07 00:14:45 -0800197 * There are two global mutexes guarding cpuset structures. The first
198 * is the main control groups cgroup_mutex, accessed via
199 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
200 * callback_mutex, below. They can nest. It is ok to first take
201 * cgroup_mutex, then nest callback_mutex. We also require taking
202 * task_lock() when dereferencing a task's cpuset pointer. See "The
203 * task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800205 * A task must hold both mutexes to modify cpusets. If a task
Paul Menage2df167a2008-02-07 00:14:45 -0800206 * holds cgroup_mutex, then it blocks others wanting that mutex,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800207 * ensuring that it is the only task able to also acquire callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800208 * and be able to modify cpusets. It can perform various checks on
209 * the cpuset structure first, knowing nothing will change. It can
Paul Menage2df167a2008-02-07 00:14:45 -0800210 * also allocate memory while just holding cgroup_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800211 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800212 * acquire callback_mutex to query cpusets. Once it is ready to make
213 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
Paul Jackson053199e2005-10-30 15:02:30 -0800215 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800216 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800217 * from one of the callbacks into the cpuset code from within
218 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800220 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800221 * access to cpusets.
222 *
Miao Xie58568d22009-06-16 15:31:49 -0700223 * Now, the task_struct fields mems_allowed and mempolicy may be changed
224 * by other task, we use alloc_lock in the task_struct fields to protect
225 * them.
Paul Jackson053199e2005-10-30 15:02:30 -0800226 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800227 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800228 * small pieces of code, such as when reading out possibly multi-word
229 * cpumasks and nodemasks.
230 *
Paul Menage2df167a2008-02-07 00:14:45 -0800231 * Accessing a task's cpuset should be done in accordance with the
232 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800235static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700236
Max Krasnyanskycf417142008-08-11 14:33:53 -0700237/*
David Rientjes75aa1992009-01-06 14:39:01 -0800238 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
239 * buffers. They are statically allocated to prevent using excess stack
240 * when calling cpuset_print_task_mems_allowed().
241 */
242#define CPUSET_NAME_LEN (128)
243#define CPUSET_NODELIST_LEN (256)
244static char cpuset_name[CPUSET_NAME_LEN];
245static char cpuset_nodelist[CPUSET_NODELIST_LEN];
246static DEFINE_SPINLOCK(cpuset_buffer_lock);
247
248/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700249 * This is ugly, but preserves the userspace API for existing cpuset
Paul Menage8793d852007-10-18 23:39:39 -0700250 * users. If someone tries to mount the "cpuset" filesystem, we
Max Krasnyanskycf417142008-08-11 14:33:53 -0700251 * silently switch it to mount "cgroup" instead
252 */
Al Virof7e83572010-07-26 13:23:11 +0400253static struct dentry *cpuset_mount(struct file_system_type *fs_type,
254 int flags, const char *unused_dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Paul Menage8793d852007-10-18 23:39:39 -0700256 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
Al Virof7e83572010-07-26 13:23:11 +0400257 struct dentry *ret = ERR_PTR(-ENODEV);
Paul Menage8793d852007-10-18 23:39:39 -0700258 if (cgroup_fs) {
259 char mountopts[] =
260 "cpuset,noprefix,"
261 "release_agent=/sbin/cpuset_release_agent";
Al Virof7e83572010-07-26 13:23:11 +0400262 ret = cgroup_fs->mount(cgroup_fs, flags,
263 unused_dev_name, mountopts);
Paul Menage8793d852007-10-18 23:39:39 -0700264 put_filesystem(cgroup_fs);
265 }
266 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static struct file_system_type cpuset_fs_type = {
270 .name = "cpuset",
Al Virof7e83572010-07-26 13:23:11 +0400271 .mount = cpuset_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272};
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800275 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * are online. If none are online, walk up the cpuset hierarchy
277 * until we find one that does have some online cpus. If we get
278 * all the way to the top and still haven't found any online cpus,
Rusty Russell5f054e32012-03-29 15:38:31 +1030279 * return cpu_online_mask. Or if passed a NULL cs from an exit'ing
280 * task, return cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *
282 * One way or another, we guarantee to return some non-empty subset
Rusty Russell5f054e32012-03-29 15:38:31 +1030283 * of cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800285 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
287
Li Zefan6af866a2009-01-07 18:08:45 -0800288static void guarantee_online_cpus(const struct cpuset *cs,
289 struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Li Zefan300ed6c2009-01-07 18:08:44 -0800291 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 cs = cs->parent;
293 if (cs)
Li Zefan300ed6c2009-01-07 18:08:44 -0800294 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 else
Li Zefan300ed6c2009-01-07 18:08:44 -0800296 cpumask_copy(pmask, cpu_online_mask);
297 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
301 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700302 * are online, with memory. If none are online with memory, walk
303 * up the cpuset hierarchy until we find one that does have some
304 * online mems. If we get all the way to the top and still haven't
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800305 * found any online mems, return node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 * One way or another, we guarantee to return some non-empty subset
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800308 * of node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800310 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312
313static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
314{
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700315 while (cs && !nodes_intersects(cs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800316 node_states[N_MEMORY]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 cs = cs->parent;
318 if (cs)
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700319 nodes_and(*pmask, cs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800320 node_states[N_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 else
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800322 *pmask = node_states[N_MEMORY];
323 BUG_ON(!nodes_intersects(*pmask, node_states[N_MEMORY]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Miao Xief3b39d42009-06-16 15:31:46 -0700326/*
327 * update task's spread flag if cpuset's page/slab spread flag is set
328 *
329 * Called with callback_mutex/cgroup_mutex held
330 */
331static void cpuset_update_task_spread_flag(struct cpuset *cs,
332 struct task_struct *tsk)
333{
334 if (is_spread_page(cs))
335 tsk->flags |= PF_SPREAD_PAGE;
336 else
337 tsk->flags &= ~PF_SPREAD_PAGE;
338 if (is_spread_slab(cs))
339 tsk->flags |= PF_SPREAD_SLAB;
340 else
341 tsk->flags &= ~PF_SPREAD_SLAB;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/*
345 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
346 *
347 * One cpuset is a subset of another if all its allowed CPUs and
348 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Menage2df167a2008-02-07 00:14:45 -0800349 * are only set if the other's are set. Call holding cgroup_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
351
352static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
353{
Li Zefan300ed6c2009-01-07 18:08:44 -0800354 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 nodes_subset(p->mems_allowed, q->mems_allowed) &&
356 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
357 is_mem_exclusive(p) <= is_mem_exclusive(q);
358}
359
Li Zefan645fcc92009-01-07 18:08:43 -0800360/**
361 * alloc_trial_cpuset - allocate a trial cpuset
362 * @cs: the cpuset that the trial cpuset duplicates
363 */
364static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
365{
Li Zefan300ed6c2009-01-07 18:08:44 -0800366 struct cpuset *trial;
367
368 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
369 if (!trial)
370 return NULL;
371
372 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
373 kfree(trial);
374 return NULL;
375 }
376 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
377
378 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800379}
380
381/**
382 * free_trial_cpuset - free the trial cpuset
383 * @trial: the trial cpuset to be freed
384 */
385static void free_trial_cpuset(struct cpuset *trial)
386{
Li Zefan300ed6c2009-01-07 18:08:44 -0800387 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800388 kfree(trial);
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * validate_change() - Used to validate that any proposed cpuset change
393 * follows the structural rules for cpusets.
394 *
395 * If we replaced the flag and mask values of the current cpuset
396 * (cur) with those values in the trial cpuset (trial), would
397 * our various subset and exclusive rules still be valid? Presumes
Paul Menage2df167a2008-02-07 00:14:45 -0800398 * cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *
400 * 'cur' is the address of an actual, in-use cpuset. Operations
401 * such as list traversal that depend on the actual address of the
402 * cpuset in the list must use cur below, not trial.
403 *
404 * 'trial' is the address of bulk structure copy of cur, with
405 * perhaps one or more of the fields cpus_allowed, mems_allowed,
406 * or flags changed to new, trial values.
407 *
408 * Return 0 if valid, -errno if not.
409 */
410
411static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
412{
Paul Menage8793d852007-10-18 23:39:39 -0700413 struct cgroup *cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct cpuset *c, *par;
415
416 /* Each of our child cpusets must be a subset of us */
Paul Menage8793d852007-10-18 23:39:39 -0700417 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
418 if (!is_cpuset_subset(cgroup_cs(cont), trial))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -EBUSY;
420 }
421
422 /* Remaining checks don't apply to root cpuset */
Paul Jackson69604062006-12-06 20:36:15 -0800423 if (cur == &top_cpuset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425
Paul Jackson69604062006-12-06 20:36:15 -0800426 par = cur->parent;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* We must be a subset of our parent cpuset */
429 if (!is_cpuset_subset(trial, par))
430 return -EACCES;
431
Paul Menage2df167a2008-02-07 00:14:45 -0800432 /*
433 * If either I or some sibling (!= me) is exclusive, we can't
434 * overlap
435 */
Paul Menage8793d852007-10-18 23:39:39 -0700436 list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
437 c = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
439 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800440 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -EINVAL;
442 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
443 c != cur &&
444 nodes_intersects(trial->mems_allowed, c->mems_allowed))
445 return -EINVAL;
446 }
447
Paul Jackson020958b2007-10-18 23:40:21 -0700448 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
449 if (cgroup_task_count(cur->css.cgroup)) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800450 if (cpumask_empty(trial->cpus_allowed) ||
Paul Jackson020958b2007-10-18 23:40:21 -0700451 nodes_empty(trial->mems_allowed)) {
452 return -ENOSPC;
453 }
454 }
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458
Paul Menagedb7f47c2009-04-02 16:57:55 -0700459#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700460/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700461 * Helper routine for generate_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -0700462 * Do cpusets a, b have overlapping cpus_allowed masks?
463 */
Paul Jackson029190c2007-10-18 23:40:20 -0700464static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
465{
Li Zefan300ed6c2009-01-07 18:08:44 -0800466 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
Paul Jackson029190c2007-10-18 23:40:20 -0700467}
468
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900469static void
470update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
471{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900472 if (dattr->relax_domain_level < c->relax_domain_level)
473 dattr->relax_domain_level = c->relax_domain_level;
474 return;
475}
476
Lai Jiangshanf5393692008-07-29 22:33:22 -0700477static void
478update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
479{
480 LIST_HEAD(q);
481
482 list_add(&c->stack_list, &q);
483 while (!list_empty(&q)) {
484 struct cpuset *cp;
485 struct cgroup *cont;
486 struct cpuset *child;
487
488 cp = list_first_entry(&q, struct cpuset, stack_list);
489 list_del(q.next);
490
Li Zefan300ed6c2009-01-07 18:08:44 -0800491 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700492 continue;
493
494 if (is_sched_load_balance(cp))
495 update_domain_attr(dattr, cp);
496
497 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
498 child = cgroup_cs(cont);
499 list_add_tail(&child->stack_list, &q);
500 }
501 }
502}
503
Paul Jackson029190c2007-10-18 23:40:20 -0700504/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700505 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700506 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700507 * This function builds a partial partition of the systems CPUs
508 * A 'partial partition' is a set of non-overlapping subsets whose
509 * union is a subset of that set.
510 * The output of this function needs to be passed to kernel/sched.c
511 * partition_sched_domains() routine, which will rebuild the scheduler's
512 * load balancing domains (sched domains) as specified by that partial
513 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700514 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800515 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700516 * for a background explanation of this.
517 *
518 * Does not return errors, on the theory that the callers of this
519 * routine would rather not worry about failures to rebuild sched
520 * domains when operating in the severe memory shortage situations
521 * that could cause allocation failures below.
522 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700523 * Must be called with cgroup_lock held.
Paul Jackson029190c2007-10-18 23:40:20 -0700524 *
525 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700526 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700527 * top-down scan of all cpusets. This scan loads a pointer
528 * to each cpuset marked is_sched_load_balance into the
529 * array 'csa'. For our purposes, rebuilding the schedulers
530 * sched domains, we can ignore !is_sched_load_balance cpusets.
531 * csa - (for CpuSet Array) Array of pointers to all the cpusets
532 * that need to be load balanced, for convenient iterative
533 * access by the subsequent code that finds the best partition,
534 * i.e the set of domains (subsets) of CPUs such that the
535 * cpus_allowed of every cpuset marked is_sched_load_balance
536 * is a subset of one of these domains, while there are as
537 * many such domains as possible, each as small as possible.
538 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
539 * the kernel/sched.c routine partition_sched_domains() in a
540 * convenient format, that can be easily compared to the prior
541 * value to determine what partition elements (sched domains)
542 * were changed (added or removed.)
543 *
544 * Finding the best partition (set of domains):
545 * The triple nested loops below over i, j, k scan over the
546 * load balanced cpusets (using the array of cpuset pointers in
547 * csa[]) looking for pairs of cpusets that have overlapping
548 * cpus_allowed, but which don't have the same 'pn' partition
549 * number and gives them in the same partition number. It keeps
550 * looping on the 'restart' label until it can no longer find
551 * any such pairs.
552 *
553 * The union of the cpus_allowed masks from the set of
554 * all cpusets having the same 'pn' value then form the one
555 * element of the partition (one sched domain) to be passed to
556 * partition_sched_domains().
557 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030558static int generate_sched_domains(cpumask_var_t **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700559 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700560{
Max Krasnyanskycf417142008-08-11 14:33:53 -0700561 LIST_HEAD(q); /* queue of cpusets to be scanned */
Paul Jackson029190c2007-10-18 23:40:20 -0700562 struct cpuset *cp; /* scans q */
563 struct cpuset **csa; /* array of all cpuset ptrs */
564 int csn; /* how many cpuset ptrs in csa so far */
565 int i, j, k; /* indices for partition finding loops */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030566 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900567 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100568 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800569 int nslot; /* next empty doms[] struct cpumask slot */
Paul Jackson029190c2007-10-18 23:40:20 -0700570
Paul Jackson029190c2007-10-18 23:40:20 -0700571 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900572 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700573 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700574
575 /* Special case for the 99% of systems with one, full, sched domain */
576 if (is_sched_load_balance(&top_cpuset)) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030577 ndoms = 1;
578 doms = alloc_sched_domains(ndoms);
Paul Jackson029190c2007-10-18 23:40:20 -0700579 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700580 goto done;
581
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900582 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
583 if (dattr) {
584 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700585 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900586 }
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030587 cpumask_copy(doms[0], top_cpuset.cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700588
Max Krasnyanskycf417142008-08-11 14:33:53 -0700589 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700590 }
591
Paul Jackson029190c2007-10-18 23:40:20 -0700592 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
593 if (!csa)
594 goto done;
595 csn = 0;
596
Li Zefanaeed6822008-07-29 22:33:24 -0700597 list_add(&top_cpuset.stack_list, &q);
598 while (!list_empty(&q)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700599 struct cgroup *cont;
600 struct cpuset *child; /* scans child cpusets of cp */
Lai Jiangshan489a5392008-07-25 01:47:23 -0700601
Li Zefanaeed6822008-07-29 22:33:24 -0700602 cp = list_first_entry(&q, struct cpuset, stack_list);
603 list_del(q.next);
604
Li Zefan300ed6c2009-01-07 18:08:44 -0800605 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshan489a5392008-07-25 01:47:23 -0700606 continue;
607
Lai Jiangshanf5393692008-07-29 22:33:22 -0700608 /*
609 * All child cpusets contain a subset of the parent's cpus, so
610 * just skip them, and then we call update_domain_attr_tree()
611 * to calc relax_domain_level of the corresponding sched
612 * domain.
613 */
614 if (is_sched_load_balance(cp)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700615 csa[csn++] = cp;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700616 continue;
617 }
Lai Jiangshan489a5392008-07-25 01:47:23 -0700618
Paul Jackson029190c2007-10-18 23:40:20 -0700619 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
620 child = cgroup_cs(cont);
Li Zefanaeed6822008-07-29 22:33:24 -0700621 list_add_tail(&child->stack_list, &q);
Paul Jackson029190c2007-10-18 23:40:20 -0700622 }
623 }
624
625 for (i = 0; i < csn; i++)
626 csa[i]->pn = i;
627 ndoms = csn;
628
629restart:
630 /* Find the best partition (set of sched domains) */
631 for (i = 0; i < csn; i++) {
632 struct cpuset *a = csa[i];
633 int apn = a->pn;
634
635 for (j = 0; j < csn; j++) {
636 struct cpuset *b = csa[j];
637 int bpn = b->pn;
638
639 if (apn != bpn && cpusets_overlap(a, b)) {
640 for (k = 0; k < csn; k++) {
641 struct cpuset *c = csa[k];
642
643 if (c->pn == bpn)
644 c->pn = apn;
645 }
646 ndoms--; /* one less element */
647 goto restart;
648 }
649 }
650 }
651
Max Krasnyanskycf417142008-08-11 14:33:53 -0700652 /*
653 * Now we know how many domains to create.
654 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
655 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030656 doms = alloc_sched_domains(ndoms);
Li Zefan700018e2008-11-18 14:02:03 +0800657 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700658 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700659
660 /*
661 * The rest of the code, including the scheduler, can deal with
662 * dattr==NULL case. No need to abort if alloc fails.
663 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900664 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700665
666 for (nslot = 0, i = 0; i < csn; i++) {
667 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800668 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700669 int apn = a->pn;
670
Max Krasnyanskycf417142008-08-11 14:33:53 -0700671 if (apn < 0) {
672 /* Skip completed partitions */
673 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700674 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700675
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030676 dp = doms[nslot];
Max Krasnyanskycf417142008-08-11 14:33:53 -0700677
678 if (nslot == ndoms) {
679 static int warnings = 10;
680 if (warnings) {
681 printk(KERN_WARNING
682 "rebuild_sched_domains confused:"
683 " nslot %d, ndoms %d, csn %d, i %d,"
684 " apn %d\n",
685 nslot, ndoms, csn, i, apn);
686 warnings--;
687 }
688 continue;
689 }
690
Li Zefan6af866a2009-01-07 18:08:45 -0800691 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700692 if (dattr)
693 *(dattr + nslot) = SD_ATTR_INIT;
694 for (j = i; j < csn; j++) {
695 struct cpuset *b = csa[j];
696
697 if (apn == b->pn) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800698 cpumask_or(dp, dp, b->cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700699 if (dattr)
700 update_domain_attr_tree(dattr + nslot, b);
701
702 /* Done with this partition */
703 b->pn = -1;
704 }
705 }
706 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700707 }
708 BUG_ON(nslot != ndoms);
709
Paul Jackson029190c2007-10-18 23:40:20 -0700710done:
Paul Jackson029190c2007-10-18 23:40:20 -0700711 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700712
Li Zefan700018e2008-11-18 14:02:03 +0800713 /*
714 * Fallback to the default domain if kmalloc() failed.
715 * See comments in partition_sched_domains().
716 */
717 if (doms == NULL)
718 ndoms = 1;
719
Max Krasnyanskycf417142008-08-11 14:33:53 -0700720 *domains = doms;
721 *attributes = dattr;
722 return ndoms;
723}
724
725/*
726 * Rebuild scheduler domains.
727 *
728 * Call with neither cgroup_mutex held nor within get_online_cpus().
729 * Takes both cgroup_mutex and get_online_cpus().
730 *
731 * Cannot be directly called from cpuset code handling changes
732 * to the cpuset pseudo-filesystem, because it cannot be called
733 * from code that already holds cgroup_mutex.
734 */
735static void do_rebuild_sched_domains(struct work_struct *unused)
736{
737 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030738 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700739 int ndoms;
740
741 get_online_cpus();
742
743 /* Generate domain masks and attrs */
744 cgroup_lock();
745 ndoms = generate_sched_domains(&doms, &attr);
746 cgroup_unlock();
747
748 /* Have scheduler rebuild the domains */
749 partition_sched_domains(ndoms, doms, attr);
750
751 put_online_cpus();
752}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700753#else /* !CONFIG_SMP */
754static void do_rebuild_sched_domains(struct work_struct *unused)
755{
756}
757
Geert Uytterhoevene1b80902009-12-06 20:41:16 +0100758static int generate_sched_domains(cpumask_var_t **domains,
Paul Menagedb7f47c2009-04-02 16:57:55 -0700759 struct sched_domain_attr **attributes)
760{
761 *domains = NULL;
762 return 1;
763}
764#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700765
766static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
767
768/*
769 * Rebuild scheduler domains, asynchronously via workqueue.
770 *
771 * If the flag 'sched_load_balance' of any cpuset with non-empty
772 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
773 * which has that flag enabled, or if any cpuset with a non-empty
774 * 'cpus' is removed, then call this routine to rebuild the
775 * scheduler's dynamic sched domains.
776 *
777 * The rebuild_sched_domains() and partition_sched_domains()
778 * routines must nest cgroup_lock() inside get_online_cpus(),
779 * but such cpuset changes as these must nest that locking the
780 * other way, holding cgroup_lock() for much of the code.
781 *
782 * So in order to avoid an ABBA deadlock, the cpuset code handling
783 * these user changes delegates the actual sched domain rebuilding
784 * to a separate workqueue thread, which ends up processing the
785 * above do_rebuild_sched_domains() function.
786 */
787static void async_rebuild_sched_domains(void)
788{
Miao Xief90d4112009-01-16 10:24:10 +0800789 queue_work(cpuset_wq, &rebuild_sched_domains_work);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700790}
791
792/*
793 * Accomplishes the same scheduler domain rebuild as the above
794 * async_rebuild_sched_domains(), however it directly calls the
795 * rebuild routine synchronously rather than calling it via an
796 * asynchronous work thread.
797 *
798 * This can only be called from code that is not holding
799 * cgroup_mutex (not nested in a cgroup_lock() call.)
800 */
801void rebuild_sched_domains(void)
802{
803 do_rebuild_sched_domains(NULL);
Paul Jackson029190c2007-10-18 23:40:20 -0700804}
805
Cliff Wickman58f47902008-02-07 00:14:44 -0800806/**
807 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
808 * @tsk: task to test
809 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
810 *
Paul Menage2df167a2008-02-07 00:14:45 -0800811 * Call with cgroup_mutex held. May take callback_mutex during call.
Cliff Wickman58f47902008-02-07 00:14:44 -0800812 * Called for each task in a cgroup by cgroup_scan_tasks().
813 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
814 * words, if its mask is not equal to its cpuset's mask).
Paul Jackson053199e2005-10-30 15:02:30 -0800815 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700816static int cpuset_test_cpumask(struct task_struct *tsk,
817 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800818{
Li Zefan300ed6c2009-01-07 18:08:44 -0800819 return !cpumask_equal(&tsk->cpus_allowed,
Cliff Wickman58f47902008-02-07 00:14:44 -0800820 (cgroup_cs(scan->cg))->cpus_allowed);
821}
Paul Jackson053199e2005-10-30 15:02:30 -0800822
Cliff Wickman58f47902008-02-07 00:14:44 -0800823/**
824 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
825 * @tsk: task to test
826 * @scan: struct cgroup_scanner containing the cgroup of the task
827 *
828 * Called by cgroup_scan_tasks() for each task in a cgroup whose
829 * cpus_allowed mask needs to be changed.
830 *
831 * We don't need to re-check for the cgroup/cpuset membership, since we're
832 * holding cgroup_lock() at this point.
833 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700834static void cpuset_change_cpumask(struct task_struct *tsk,
835 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800836{
Li Zefan300ed6c2009-01-07 18:08:44 -0800837 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
Cliff Wickman58f47902008-02-07 00:14:44 -0800838}
839
840/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700841 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
842 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
Li Zefan4e743392008-09-13 02:33:08 -0700843 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -0700844 *
845 * Called with cgroup_mutex held
846 *
847 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
848 * calling callback functions for each.
849 *
Li Zefan4e743392008-09-13 02:33:08 -0700850 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
851 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -0700852 */
Li Zefan4e743392008-09-13 02:33:08 -0700853static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
Miao Xie0b2f6302008-07-25 01:47:21 -0700854{
855 struct cgroup_scanner scan;
Miao Xie0b2f6302008-07-25 01:47:21 -0700856
857 scan.cg = cs->css.cgroup;
858 scan.test_task = cpuset_test_cpumask;
859 scan.process_task = cpuset_change_cpumask;
Li Zefan4e743392008-09-13 02:33:08 -0700860 scan.heap = heap;
861 cgroup_scan_tasks(&scan);
Miao Xie0b2f6302008-07-25 01:47:21 -0700862}
863
864/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800865 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
866 * @cs: the cpuset to consider
867 * @buf: buffer of cpu numbers written to this cpuset
868 */
Li Zefan645fcc92009-01-07 18:08:43 -0800869static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
870 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Li Zefan4e743392008-09-13 02:33:08 -0700872 struct ptr_heap heap;
Cliff Wickman58f47902008-02-07 00:14:44 -0800873 int retval;
874 int is_load_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Rusty Russell5f054e32012-03-29 15:38:31 +1030876 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700877 if (cs == &top_cpuset)
878 return -EACCES;
879
David Rientjes6f7f02e2007-05-08 00:31:43 -0700880 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800881 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700882 * Since cpulist_parse() fails on an empty mask, we special case
883 * that parsing. The validate_change() call ensures that cpusets
884 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700885 */
Paul Jackson020958b2007-10-18 23:40:21 -0700886 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800887 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700888 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -0800889 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700890 if (retval < 0)
891 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700892
Peter Zijlstra6ad4c182009-11-25 13:31:39 +0100893 if (!cpumask_subset(trialcs->cpus_allowed, cpu_active_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700894 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700895 }
Li Zefan645fcc92009-01-07 18:08:43 -0800896 retval = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700897 if (retval < 0)
898 return retval;
Paul Jackson029190c2007-10-18 23:40:20 -0700899
Paul Menage8707d8b2007-10-18 23:40:22 -0700900 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800901 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700902 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800903
Li Zefan4e743392008-09-13 02:33:08 -0700904 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
905 if (retval)
906 return retval;
907
Li Zefan645fcc92009-01-07 18:08:43 -0800908 is_load_balanced = is_sched_load_balance(trialcs);
Paul Jackson029190c2007-10-18 23:40:20 -0700909
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800910 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -0800911 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800912 mutex_unlock(&callback_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700913
Paul Menage8707d8b2007-10-18 23:40:22 -0700914 /*
915 * Scan tasks in the cpuset, and update the cpumasks of any
Cliff Wickman58f47902008-02-07 00:14:44 -0800916 * that need an update.
Paul Menage8707d8b2007-10-18 23:40:22 -0700917 */
Li Zefan4e743392008-09-13 02:33:08 -0700918 update_tasks_cpumask(cs, &heap);
919
920 heap_free(&heap);
Cliff Wickman58f47902008-02-07 00:14:44 -0800921
Paul Menage8707d8b2007-10-18 23:40:22 -0700922 if (is_load_balanced)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700923 async_rebuild_sched_domains();
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Paul Jackson053199e2005-10-30 15:02:30 -0800927/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800928 * cpuset_migrate_mm
929 *
930 * Migrate memory region from one set of nodes to another.
931 *
932 * Temporarilly set tasks mems_allowed to target nodes of migration,
933 * so that the migration code can allocate pages on these nodes.
934 *
Paul Menage2df167a2008-02-07 00:14:45 -0800935 * Call holding cgroup_mutex, so current's cpuset won't change
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800936 * during this call, as manage_mutex holds off any cpuset_attach()
Paul Jacksone4e364e2006-03-31 02:30:52 -0800937 * calls. Therefore we don't need to take task_lock around the
938 * call to guarantee_online_mems(), as we know no one is changing
Paul Menage2df167a2008-02-07 00:14:45 -0800939 * our task's cpuset.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800940 *
Paul Jacksone4e364e2006-03-31 02:30:52 -0800941 * While the mm_struct we are migrating is typically from some
942 * other task, the task_struct mems_allowed that we are hacking
943 * is for our current task, which must allocate new pages for that
944 * migrating memory region.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800945 */
946
947static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
948 const nodemask_t *to)
949{
950 struct task_struct *tsk = current;
951
Paul Jacksone4e364e2006-03-31 02:30:52 -0800952 tsk->mems_allowed = *to;
Paul Jacksone4e364e2006-03-31 02:30:52 -0800953
954 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
955
Paul Menage8793d852007-10-18 23:39:39 -0700956 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -0800957}
958
Li Zefan3b6766f2009-04-02 16:57:51 -0700959/*
Miao Xie58568d22009-06-16 15:31:49 -0700960 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
961 * @tsk: the task to change
962 * @newmems: new nodes that the task will be set
963 *
964 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
965 * we structure updates as setting all new allowed nodes, then clearing newly
966 * disallowed ones.
Miao Xie58568d22009-06-16 15:31:49 -0700967 */
968static void cpuset_change_task_nodemask(struct task_struct *tsk,
969 nodemask_t *newmems)
970{
David Rientjesb2462722011-12-19 17:11:52 -0800971 bool need_loop;
David Rientjes89e8a242011-11-02 13:38:39 -0700972
Miao Xiec0ff7452010-05-24 14:32:08 -0700973 /*
974 * Allow tasks that have access to memory reserves because they have
975 * been OOM killed to get memory anywhere.
976 */
977 if (unlikely(test_thread_flag(TIF_MEMDIE)))
978 return;
979 if (current->flags & PF_EXITING) /* Let dying task have memory */
980 return;
981
982 task_lock(tsk);
David Rientjesb2462722011-12-19 17:11:52 -0800983 /*
984 * Determine if a loop is necessary if another thread is doing
985 * get_mems_allowed(). If at least one node remains unchanged and
986 * tsk does not have a mempolicy, then an empty nodemask will not be
987 * possible when mems_allowed is larger than a word.
988 */
989 need_loop = task_has_mempolicy(tsk) ||
990 !nodes_intersects(*newmems, tsk->mems_allowed);
Mel Gormancc9a6c82012-03-21 16:34:11 -0700991
992 if (need_loop)
993 write_seqcount_begin(&tsk->mems_allowed_seq);
994
Miao Xie58568d22009-06-16 15:31:49 -0700995 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Miao Xiec0ff7452010-05-24 14:32:08 -0700996 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
997
Miao Xiec0ff7452010-05-24 14:32:08 -0700998 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
Miao Xie58568d22009-06-16 15:31:49 -0700999 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001000
1001 if (need_loop)
1002 write_seqcount_end(&tsk->mems_allowed_seq);
1003
Miao Xiec0ff7452010-05-24 14:32:08 -07001004 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001005}
1006
1007/*
1008 * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy
1009 * of it to cpuset's new mems_allowed, and migrate pages to new nodes if
1010 * memory_migrate flag is set. Called with cgroup_mutex held.
Li Zefan3b6766f2009-04-02 16:57:51 -07001011 */
1012static void cpuset_change_nodemask(struct task_struct *p,
1013 struct cgroup_scanner *scan)
1014{
1015 struct mm_struct *mm;
1016 struct cpuset *cs;
1017 int migrate;
1018 const nodemask_t *oldmem = scan->data;
Li Zefanee24d372011-03-23 16:42:47 -07001019 static nodemask_t newmems; /* protected by cgroup_mutex */
Miao Xie58568d22009-06-16 15:31:49 -07001020
1021 cs = cgroup_cs(scan->cg);
Li Zefanee24d372011-03-23 16:42:47 -07001022 guarantee_online_mems(cs, &newmems);
Miao Xie58568d22009-06-16 15:31:49 -07001023
Li Zefanee24d372011-03-23 16:42:47 -07001024 cpuset_change_task_nodemask(p, &newmems);
Miao Xie53feb292010-03-23 13:35:35 -07001025
Li Zefan3b6766f2009-04-02 16:57:51 -07001026 mm = get_task_mm(p);
1027 if (!mm)
1028 return;
1029
Li Zefan3b6766f2009-04-02 16:57:51 -07001030 migrate = is_memory_migrate(cs);
1031
1032 mpol_rebind_mm(mm, &cs->mems_allowed);
1033 if (migrate)
1034 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1035 mmput(mm);
1036}
1037
Paul Menage8793d852007-10-18 23:39:39 -07001038static void *cpuset_being_rebound;
1039
Miao Xie0b2f6302008-07-25 01:47:21 -07001040/**
1041 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1042 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1043 * @oldmem: old mems_allowed of cpuset cs
Li Zefan010cfac2009-04-02 16:57:52 -07001044 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -07001045 *
1046 * Called with cgroup_mutex held
Li Zefan010cfac2009-04-02 16:57:52 -07001047 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1048 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -07001049 */
Li Zefan010cfac2009-04-02 16:57:52 -07001050static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1051 struct ptr_heap *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Li Zefan3b6766f2009-04-02 16:57:51 -07001053 struct cgroup_scanner scan;
Paul Jackson59dac162006-01-08 01:01:52 -08001054
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001055 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001056
Li Zefan3b6766f2009-04-02 16:57:51 -07001057 scan.cg = cs->css.cgroup;
1058 scan.test_task = NULL;
1059 scan.process_task = cpuset_change_nodemask;
Li Zefan010cfac2009-04-02 16:57:52 -07001060 scan.heap = heap;
Li Zefan3b6766f2009-04-02 16:57:51 -07001061 scan.data = (nodemask_t *)oldmem;
Paul Jackson42253992006-01-08 01:01:59 -08001062
1063 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001064 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1065 * take while holding tasklist_lock. Forks can happen - the
1066 * mpol_dup() cpuset_being_rebound check will catch such forks,
1067 * and rebind their vma mempolicies too. Because we still hold
1068 * the global cgroup_mutex, we know that no other rebind effort
1069 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001070 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001071 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001072 */
Li Zefan010cfac2009-04-02 16:57:52 -07001073 cgroup_scan_tasks(&scan);
Paul Jackson42253992006-01-08 01:01:59 -08001074
Paul Menage2df167a2008-02-07 00:14:45 -08001075 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001076 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Miao Xie0b2f6302008-07-25 01:47:21 -07001079/*
1080 * Handle user request to change the 'mems' memory placement
1081 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001082 * cpusets mems_allowed, and for each task in the cpuset,
1083 * update mems_allowed and rebind task's mempolicy and any vma
1084 * mempolicies and if the cpuset is marked 'memory_migrate',
1085 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001086 *
1087 * Call with cgroup_mutex held. May take callback_mutex during call.
1088 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1089 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1090 * their mempolicies to the cpusets new mems_allowed.
1091 */
Li Zefan645fcc92009-01-07 18:08:43 -08001092static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1093 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001094{
Miao Xie53feb292010-03-23 13:35:35 -07001095 NODEMASK_ALLOC(nodemask_t, oldmem, GFP_KERNEL);
Miao Xie0b2f6302008-07-25 01:47:21 -07001096 int retval;
Li Zefan010cfac2009-04-02 16:57:52 -07001097 struct ptr_heap heap;
Miao Xie0b2f6302008-07-25 01:47:21 -07001098
Miao Xie53feb292010-03-23 13:35:35 -07001099 if (!oldmem)
1100 return -ENOMEM;
1101
Miao Xie0b2f6302008-07-25 01:47:21 -07001102 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001103 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001104 * it's read-only
1105 */
Miao Xie53feb292010-03-23 13:35:35 -07001106 if (cs == &top_cpuset) {
1107 retval = -EACCES;
1108 goto done;
1109 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001110
Miao Xie0b2f6302008-07-25 01:47:21 -07001111 /*
1112 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1113 * Since nodelist_parse() fails on an empty mask, we special case
1114 * that parsing. The validate_change() call ensures that cpusets
1115 * with tasks have memory.
1116 */
1117 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001118 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001119 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001120 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001121 if (retval < 0)
1122 goto done;
1123
Li Zefan645fcc92009-01-07 18:08:43 -08001124 if (!nodes_subset(trialcs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001125 node_states[N_MEMORY])) {
Miao Xie53feb292010-03-23 13:35:35 -07001126 retval = -EINVAL;
1127 goto done;
1128 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001129 }
Miao Xie53feb292010-03-23 13:35:35 -07001130 *oldmem = cs->mems_allowed;
1131 if (nodes_equal(*oldmem, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001132 retval = 0; /* Too easy - nothing to do */
1133 goto done;
1134 }
Li Zefan645fcc92009-01-07 18:08:43 -08001135 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001136 if (retval < 0)
1137 goto done;
1138
Li Zefan010cfac2009-04-02 16:57:52 -07001139 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1140 if (retval < 0)
1141 goto done;
1142
Miao Xie0b2f6302008-07-25 01:47:21 -07001143 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001144 cs->mems_allowed = trialcs->mems_allowed;
Miao Xie0b2f6302008-07-25 01:47:21 -07001145 mutex_unlock(&callback_mutex);
1146
Miao Xie53feb292010-03-23 13:35:35 -07001147 update_tasks_nodemask(cs, oldmem, &heap);
Li Zefan010cfac2009-04-02 16:57:52 -07001148
1149 heap_free(&heap);
Miao Xie0b2f6302008-07-25 01:47:21 -07001150done:
Miao Xie53feb292010-03-23 13:35:35 -07001151 NODEMASK_FREE(oldmem);
Miao Xie0b2f6302008-07-25 01:47:21 -07001152 return retval;
1153}
1154
Paul Menage8793d852007-10-18 23:39:39 -07001155int current_cpuset_is_being_rebound(void)
1156{
1157 return task_cs(current) == cpuset_being_rebound;
1158}
1159
Paul Menage5be7a472008-05-06 20:42:41 -07001160static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001161{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001162#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001163 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001164 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001165#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001166
1167 if (val != cs->relax_domain_level) {
1168 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001169 if (!cpumask_empty(cs->cpus_allowed) &&
1170 is_sched_load_balance(cs))
Max Krasnyanskycf417142008-08-11 14:33:53 -07001171 async_rebuild_sched_domains();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001172 }
1173
1174 return 0;
1175}
1176
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001177/*
Miao Xie950592f2009-06-16 15:31:47 -07001178 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1179 * @tsk: task to be updated
1180 * @scan: struct cgroup_scanner containing the cgroup of the task
1181 *
1182 * Called by cgroup_scan_tasks() for each task in a cgroup.
1183 *
1184 * We don't need to re-check for the cgroup/cpuset membership, since we're
1185 * holding cgroup_lock() at this point.
1186 */
1187static void cpuset_change_flag(struct task_struct *tsk,
1188 struct cgroup_scanner *scan)
1189{
1190 cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk);
1191}
1192
1193/*
1194 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1195 * @cs: the cpuset in which each task's spread flags needs to be changed
1196 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
1197 *
1198 * Called with cgroup_mutex held
1199 *
1200 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1201 * calling callback functions for each.
1202 *
1203 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1204 * if @heap != NULL.
1205 */
1206static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap)
1207{
1208 struct cgroup_scanner scan;
1209
1210 scan.cg = cs->css.cgroup;
1211 scan.test_task = NULL;
1212 scan.process_task = cpuset_change_flag;
1213 scan.heap = heap;
1214 cgroup_scan_tasks(&scan);
1215}
1216
1217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001219 * bit: the bit to update (see cpuset_flagbits_t)
1220 * cs: the cpuset to update
1221 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001222 *
Paul Menage2df167a2008-02-07 00:14:45 -08001223 * Call with cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
1225
Paul Menage700fe1a2008-04-29 01:00:00 -07001226static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1227 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Li Zefan645fcc92009-01-07 18:08:43 -08001229 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001230 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001231 int spread_flag_changed;
1232 struct ptr_heap heap;
1233 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Li Zefan645fcc92009-01-07 18:08:43 -08001235 trialcs = alloc_trial_cpuset(cs);
1236 if (!trialcs)
1237 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Li Zefan645fcc92009-01-07 18:08:43 -08001239 if (turning_on)
1240 set_bit(bit, &trialcs->flags);
1241 else
1242 clear_bit(bit, &trialcs->flags);
1243
1244 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001245 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001246 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001247
Miao Xie950592f2009-06-16 15:31:47 -07001248 err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1249 if (err < 0)
1250 goto out;
1251
Paul Jackson029190c2007-10-18 23:40:20 -07001252 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001253 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001254
Miao Xie950592f2009-06-16 15:31:47 -07001255 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1256 || (is_spread_page(cs) != is_spread_page(trialcs)));
1257
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001258 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001259 cs->flags = trialcs->flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001260 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001261
Li Zefan300ed6c2009-01-07 18:08:44 -08001262 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Max Krasnyanskycf417142008-08-11 14:33:53 -07001263 async_rebuild_sched_domains();
Paul Jackson029190c2007-10-18 23:40:20 -07001264
Miao Xie950592f2009-06-16 15:31:47 -07001265 if (spread_flag_changed)
1266 update_tasks_flags(cs, &heap);
1267 heap_free(&heap);
Li Zefan645fcc92009-01-07 18:08:43 -08001268out:
1269 free_trial_cpuset(trialcs);
1270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Paul Jackson053199e2005-10-30 15:02:30 -08001273/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001274 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001275 *
1276 * These routines manage a digitally filtered, constant time based,
1277 * event frequency meter. There are four routines:
1278 * fmeter_init() - initialize a frequency meter.
1279 * fmeter_markevent() - called each time the event happens.
1280 * fmeter_getrate() - returns the recent rate of such events.
1281 * fmeter_update() - internal routine used to update fmeter.
1282 *
1283 * A common data structure is passed to each of these routines,
1284 * which is used to keep track of the state required to manage the
1285 * frequency meter and its digital filter.
1286 *
1287 * The filter works on the number of events marked per unit time.
1288 * The filter is single-pole low-pass recursive (IIR). The time unit
1289 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1290 * simulate 3 decimal digits of precision (multiplied by 1000).
1291 *
1292 * With an FM_COEF of 933, and a time base of 1 second, the filter
1293 * has a half-life of 10 seconds, meaning that if the events quit
1294 * happening, then the rate returned from the fmeter_getrate()
1295 * will be cut in half each 10 seconds, until it converges to zero.
1296 *
1297 * It is not worth doing a real infinitely recursive filter. If more
1298 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1299 * just compute FM_MAXTICKS ticks worth, by which point the level
1300 * will be stable.
1301 *
1302 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1303 * arithmetic overflow in the fmeter_update() routine.
1304 *
1305 * Given the simple 32 bit integer arithmetic used, this meter works
1306 * best for reporting rates between one per millisecond (msec) and
1307 * one per 32 (approx) seconds. At constant rates faster than one
1308 * per msec it maxes out at values just under 1,000,000. At constant
1309 * rates between one per msec, and one per second it will stabilize
1310 * to a value N*1000, where N is the rate of events per second.
1311 * At constant rates between one per second and one per 32 seconds,
1312 * it will be choppy, moving up on the seconds that have an event,
1313 * and then decaying until the next event. At rates slower than
1314 * about one in 32 seconds, it decays all the way back to zero between
1315 * each event.
1316 */
1317
1318#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1319#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1320#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1321#define FM_SCALE 1000 /* faux fixed point scale */
1322
1323/* Initialize a frequency meter */
1324static void fmeter_init(struct fmeter *fmp)
1325{
1326 fmp->cnt = 0;
1327 fmp->val = 0;
1328 fmp->time = 0;
1329 spin_lock_init(&fmp->lock);
1330}
1331
1332/* Internal meter update - process cnt events and update value */
1333static void fmeter_update(struct fmeter *fmp)
1334{
1335 time_t now = get_seconds();
1336 time_t ticks = now - fmp->time;
1337
1338 if (ticks == 0)
1339 return;
1340
1341 ticks = min(FM_MAXTICKS, ticks);
1342 while (ticks-- > 0)
1343 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1344 fmp->time = now;
1345
1346 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1347 fmp->cnt = 0;
1348}
1349
1350/* Process any previous ticks, then bump cnt by one (times scale). */
1351static void fmeter_markevent(struct fmeter *fmp)
1352{
1353 spin_lock(&fmp->lock);
1354 fmeter_update(fmp);
1355 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1356 spin_unlock(&fmp->lock);
1357}
1358
1359/* Process any previous ticks, then return current value. */
1360static int fmeter_getrate(struct fmeter *fmp)
1361{
1362 int val;
1363
1364 spin_lock(&fmp->lock);
1365 fmeter_update(fmp);
1366 val = fmp->val;
1367 spin_unlock(&fmp->lock);
1368 return val;
1369}
1370
Ben Blumf780bdb2011-05-26 16:25:19 -07001371/*
1372 * Protected by cgroup_lock. The nodemasks must be stored globally because
Tejun Heo94196f52011-12-12 18:12:22 -08001373 * dynamically allocating them is not allowed in can_attach, and they must
1374 * persist until attach.
Ben Blumf780bdb2011-05-26 16:25:19 -07001375 */
1376static cpumask_var_t cpus_attach;
1377static nodemask_t cpuset_attach_nodemask_from;
1378static nodemask_t cpuset_attach_nodemask_to;
1379
David Rientjes6d7b2f52009-04-02 16:57:57 -07001380/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
Li Zefan761b3ef2012-01-31 13:47:36 +08001381static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07001382{
Tejun Heo2f7ee562011-12-12 18:12:21 -08001383 struct cpuset *cs = cgroup_cs(cgrp);
Tejun Heobb9d97b2011-12-12 18:12:21 -08001384 struct task_struct *task;
1385 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07001386
Ben Blumbe367d02009-09-23 15:56:31 -07001387 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1388 return -ENOSPC;
1389
Tejun Heobb9d97b2011-12-12 18:12:21 -08001390 cgroup_taskset_for_each(task, cgrp, tset) {
1391 /*
1392 * Kthreads bound to specific cpus cannot be moved to a new
1393 * cpuset; we cannot change their cpu affinity and
1394 * isolating such threads by their set of allowed nodes is
1395 * unnecessary. Thus, cpusets are not applicable for such
1396 * threads. This prevents checking for success of
1397 * set_cpus_allowed_ptr() on all attached tasks before
1398 * cpus_allowed may be changed.
1399 */
1400 if (task->flags & PF_THREAD_BOUND)
1401 return -EINVAL;
1402 if ((ret = security_task_setscheduler(task)))
1403 return ret;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Tejun Heo94196f52011-12-12 18:12:22 -08001406 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07001407 if (cs == &top_cpuset)
1408 cpumask_copy(cpus_attach, cpu_possible_mask);
1409 else
1410 guarantee_online_cpus(cs, cpus_attach);
1411
1412 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08001413
1414 return 0;
Ben Blumf780bdb2011-05-26 16:25:19 -07001415}
1416
Li Zefan761b3ef2012-01-31 13:47:36 +08001417static void cpuset_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Paul Menage8793d852007-10-18 23:39:39 -07001418{
Paul Menage8793d852007-10-18 23:39:39 -07001419 struct mm_struct *mm;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001420 struct task_struct *task;
1421 struct task_struct *leader = cgroup_taskset_first(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001422 struct cgroup *oldcgrp = cgroup_taskset_cur_cgroup(tset);
1423 struct cpuset *cs = cgroup_cs(cgrp);
1424 struct cpuset *oldcs = cgroup_cs(oldcgrp);
David Quigley22fb52d2006-06-23 02:04:00 -07001425
Tejun Heobb9d97b2011-12-12 18:12:21 -08001426 cgroup_taskset_for_each(task, cgrp, tset) {
1427 /*
1428 * can_attach beforehand should guarantee that this doesn't
1429 * fail. TODO: have a better way to handle failure here
1430 */
1431 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1432
1433 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1434 cpuset_update_task_spread_flag(cs, task);
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Ben Blumf780bdb2011-05-26 16:25:19 -07001437 /*
1438 * Change mm, possibly for multiple threads in a threadgroup. This is
1439 * expensive and may sleep.
1440 */
1441 cpuset_attach_nodemask_from = oldcs->mems_allowed;
1442 cpuset_attach_nodemask_to = cs->mems_allowed;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001443 mm = get_task_mm(leader);
Paul Jackson42253992006-01-08 01:01:59 -08001444 if (mm) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001445 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
Paul Jackson2741a552006-03-31 02:30:51 -08001446 if (is_memory_migrate(cs))
Ben Blumf780bdb2011-05-26 16:25:19 -07001447 cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from,
1448 &cpuset_attach_nodemask_to);
Paul Jackson42253992006-01-08 01:01:59 -08001449 mmput(mm);
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453/* The various types of files and directories in a cpuset file system */
1454
1455typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001456 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 FILE_CPULIST,
1458 FILE_MEMLIST,
1459 FILE_CPU_EXCLUSIVE,
1460 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001461 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001462 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001463 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001464 FILE_MEMORY_PRESSURE_ENABLED,
1465 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001466 FILE_SPREAD_PAGE,
1467 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468} cpuset_filetype_t;
1469
Paul Menage700fe1a2008-04-29 01:00:00 -07001470static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1471{
1472 int retval = 0;
1473 struct cpuset *cs = cgroup_cs(cgrp);
1474 cpuset_filetype_t type = cft->private;
1475
Paul Menagee3712392008-07-25 01:47:02 -07001476 if (!cgroup_lock_live_group(cgrp))
Paul Menage700fe1a2008-04-29 01:00:00 -07001477 return -ENODEV;
Paul Menage700fe1a2008-04-29 01:00:00 -07001478
1479 switch (type) {
1480 case FILE_CPU_EXCLUSIVE:
1481 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1482 break;
1483 case FILE_MEM_EXCLUSIVE:
1484 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1485 break;
Paul Menage78608362008-04-29 01:00:26 -07001486 case FILE_MEM_HARDWALL:
1487 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1488 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001489 case FILE_SCHED_LOAD_BALANCE:
1490 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1491 break;
1492 case FILE_MEMORY_MIGRATE:
1493 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1494 break;
1495 case FILE_MEMORY_PRESSURE_ENABLED:
1496 cpuset_memory_pressure_enabled = !!val;
1497 break;
1498 case FILE_MEMORY_PRESSURE:
1499 retval = -EACCES;
1500 break;
1501 case FILE_SPREAD_PAGE:
1502 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001503 break;
1504 case FILE_SPREAD_SLAB:
1505 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001506 break;
1507 default:
1508 retval = -EINVAL;
1509 break;
1510 }
1511 cgroup_unlock();
1512 return retval;
1513}
1514
Paul Menage5be7a472008-05-06 20:42:41 -07001515static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1516{
1517 int retval = 0;
1518 struct cpuset *cs = cgroup_cs(cgrp);
1519 cpuset_filetype_t type = cft->private;
1520
Paul Menagee3712392008-07-25 01:47:02 -07001521 if (!cgroup_lock_live_group(cgrp))
Paul Menage5be7a472008-05-06 20:42:41 -07001522 return -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001523
Paul Menage5be7a472008-05-06 20:42:41 -07001524 switch (type) {
1525 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1526 retval = update_relax_domain_level(cs, val);
1527 break;
1528 default:
1529 retval = -EINVAL;
1530 break;
1531 }
1532 cgroup_unlock();
1533 return retval;
1534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536/*
Paul Menagee3712392008-07-25 01:47:02 -07001537 * Common handling for a write to a "cpus" or "mems" file.
1538 */
1539static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1540 const char *buf)
1541{
1542 int retval = 0;
Li Zefan645fcc92009-01-07 18:08:43 -08001543 struct cpuset *cs = cgroup_cs(cgrp);
1544 struct cpuset *trialcs;
Paul Menagee3712392008-07-25 01:47:02 -07001545
1546 if (!cgroup_lock_live_group(cgrp))
1547 return -ENODEV;
1548
Li Zefan645fcc92009-01-07 18:08:43 -08001549 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001550 if (!trialcs) {
1551 retval = -ENOMEM;
1552 goto out;
1553 }
Li Zefan645fcc92009-01-07 18:08:43 -08001554
Paul Menagee3712392008-07-25 01:47:02 -07001555 switch (cft->private) {
1556 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001557 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001558 break;
1559 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001560 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001561 break;
1562 default:
1563 retval = -EINVAL;
1564 break;
1565 }
Li Zefan645fcc92009-01-07 18:08:43 -08001566
1567 free_trial_cpuset(trialcs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001568out:
Paul Menagee3712392008-07-25 01:47:02 -07001569 cgroup_unlock();
1570 return retval;
1571}
1572
1573/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 * These ascii lists should be read in a single call, by using a user
1575 * buffer large enough to hold the entire map. If read in smaller
1576 * chunks, there is no guarantee of atomicity. Since the display format
1577 * used, list of ranges of sequential numbers, is variable length,
1578 * and since these maps can change value dynamically, one could read
1579 * gibberish by doing partial reads while a list was changing.
1580 * A single large read to a buffer that crosses a page boundary is
1581 * ok, because the result being copied to user land is not recomputed
1582 * across a page fault.
1583 */
1584
Li Zefan9303e0c2011-03-23 16:42:45 -07001585static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Li Zefan9303e0c2011-03-23 16:42:45 -07001587 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001589 mutex_lock(&callback_mutex);
Li Zefan9303e0c2011-03-23 16:42:45 -07001590 count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001591 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Li Zefan9303e0c2011-03-23 16:42:45 -07001593 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Li Zefan9303e0c2011-03-23 16:42:45 -07001596static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Li Zefan9303e0c2011-03-23 16:42:45 -07001598 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001600 mutex_lock(&callback_mutex);
Li Zefan9303e0c2011-03-23 16:42:45 -07001601 count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001602 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Li Zefan9303e0c2011-03-23 16:42:45 -07001604 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Paul Menage8793d852007-10-18 23:39:39 -07001607static ssize_t cpuset_common_file_read(struct cgroup *cont,
1608 struct cftype *cft,
1609 struct file *file,
1610 char __user *buf,
1611 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Paul Menage8793d852007-10-18 23:39:39 -07001613 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 cpuset_filetype_t type = cft->private;
1615 char *page;
1616 ssize_t retval = 0;
1617 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Mel Gormane12ba742007-10-16 01:25:52 -07001619 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -ENOMEM;
1621
1622 s = page;
1623
1624 switch (type) {
1625 case FILE_CPULIST:
1626 s += cpuset_sprintf_cpulist(s, cs);
1627 break;
1628 case FILE_MEMLIST:
1629 s += cpuset_sprintf_memlist(s, cs);
1630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 default:
1632 retval = -EINVAL;
1633 goto out;
1634 }
1635 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Al Viroeacaa1f2005-09-30 03:26:43 +01001637 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638out:
1639 free_page((unsigned long)page);
1640 return retval;
1641}
1642
Paul Menage700fe1a2008-04-29 01:00:00 -07001643static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1644{
1645 struct cpuset *cs = cgroup_cs(cont);
1646 cpuset_filetype_t type = cft->private;
1647 switch (type) {
1648 case FILE_CPU_EXCLUSIVE:
1649 return is_cpu_exclusive(cs);
1650 case FILE_MEM_EXCLUSIVE:
1651 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001652 case FILE_MEM_HARDWALL:
1653 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001654 case FILE_SCHED_LOAD_BALANCE:
1655 return is_sched_load_balance(cs);
1656 case FILE_MEMORY_MIGRATE:
1657 return is_memory_migrate(cs);
1658 case FILE_MEMORY_PRESSURE_ENABLED:
1659 return cpuset_memory_pressure_enabled;
1660 case FILE_MEMORY_PRESSURE:
1661 return fmeter_getrate(&cs->fmeter);
1662 case FILE_SPREAD_PAGE:
1663 return is_spread_page(cs);
1664 case FILE_SPREAD_SLAB:
1665 return is_spread_slab(cs);
1666 default:
1667 BUG();
1668 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001669
1670 /* Unreachable but makes gcc happy */
1671 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Paul Menage5be7a472008-05-06 20:42:41 -07001674static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1675{
1676 struct cpuset *cs = cgroup_cs(cont);
1677 cpuset_filetype_t type = cft->private;
1678 switch (type) {
1679 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1680 return cs->relax_domain_level;
1681 default:
1682 BUG();
1683 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001684
1685 /* Unrechable but makes gcc happy */
1686 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690/*
1691 * for the common functions, 'private' gives the type of file
1692 */
1693
Paul Menageaddf2c72008-04-29 01:00:26 -07001694static struct cftype files[] = {
1695 {
1696 .name = "cpus",
1697 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001698 .write_string = cpuset_write_resmask,
1699 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001700 .private = FILE_CPULIST,
1701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Paul Menageaddf2c72008-04-29 01:00:26 -07001703 {
1704 .name = "mems",
1705 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001706 .write_string = cpuset_write_resmask,
1707 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001708 .private = FILE_MEMLIST,
1709 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Paul Menageaddf2c72008-04-29 01:00:26 -07001711 {
1712 .name = "cpu_exclusive",
1713 .read_u64 = cpuset_read_u64,
1714 .write_u64 = cpuset_write_u64,
1715 .private = FILE_CPU_EXCLUSIVE,
1716 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Paul Menageaddf2c72008-04-29 01:00:26 -07001718 {
1719 .name = "mem_exclusive",
1720 .read_u64 = cpuset_read_u64,
1721 .write_u64 = cpuset_write_u64,
1722 .private = FILE_MEM_EXCLUSIVE,
1723 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Paul Menageaddf2c72008-04-29 01:00:26 -07001725 {
Paul Menage78608362008-04-29 01:00:26 -07001726 .name = "mem_hardwall",
1727 .read_u64 = cpuset_read_u64,
1728 .write_u64 = cpuset_write_u64,
1729 .private = FILE_MEM_HARDWALL,
1730 },
1731
1732 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001733 .name = "sched_load_balance",
1734 .read_u64 = cpuset_read_u64,
1735 .write_u64 = cpuset_write_u64,
1736 .private = FILE_SCHED_LOAD_BALANCE,
1737 },
Paul Jackson029190c2007-10-18 23:40:20 -07001738
Paul Menageaddf2c72008-04-29 01:00:26 -07001739 {
1740 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001741 .read_s64 = cpuset_read_s64,
1742 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001743 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1744 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001745
Paul Menageaddf2c72008-04-29 01:00:26 -07001746 {
1747 .name = "memory_migrate",
1748 .read_u64 = cpuset_read_u64,
1749 .write_u64 = cpuset_write_u64,
1750 .private = FILE_MEMORY_MIGRATE,
1751 },
1752
1753 {
1754 .name = "memory_pressure",
1755 .read_u64 = cpuset_read_u64,
1756 .write_u64 = cpuset_write_u64,
1757 .private = FILE_MEMORY_PRESSURE,
Li Zefan099fca32009-04-02 16:57:29 -07001758 .mode = S_IRUGO,
Paul Menageaddf2c72008-04-29 01:00:26 -07001759 },
1760
1761 {
1762 .name = "memory_spread_page",
1763 .read_u64 = cpuset_read_u64,
1764 .write_u64 = cpuset_write_u64,
1765 .private = FILE_SPREAD_PAGE,
1766 },
1767
1768 {
1769 .name = "memory_spread_slab",
1770 .read_u64 = cpuset_read_u64,
1771 .write_u64 = cpuset_write_u64,
1772 .private = FILE_SPREAD_SLAB,
1773 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07001774
1775 {
1776 .name = "memory_pressure_enabled",
1777 .flags = CFTYPE_ONLY_ON_ROOT,
1778 .read_u64 = cpuset_read_u64,
1779 .write_u64 = cpuset_write_u64,
1780 .private = FILE_MEMORY_PRESSURE_ENABLED,
1781 },
1782
1783 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001784};
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786/*
Tejun Heo92fb9742012-11-19 08:13:38 -08001787 * cpuset_css_alloc - allocate a cpuset css
Paul Menage2df167a2008-02-07 00:14:45 -08001788 * cont: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 */
1790
Tejun Heo92fb9742012-11-19 08:13:38 -08001791static struct cgroup_subsys_state *cpuset_css_alloc(struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Tejun Heo033fa1c2012-11-19 08:13:39 -08001793 struct cgroup *parent_cg = cont->parent;
1794 struct cgroup *tmp_cg;
1795 struct cpuset *parent, *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Tejun Heo033fa1c2012-11-19 08:13:39 -08001797 if (!parent_cg)
Paul Menage8793d852007-10-18 23:39:39 -07001798 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001799 parent = cgroup_cs(parent_cg);
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1802 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001803 return ERR_PTR(-ENOMEM);
Li Zefan300ed6c2009-01-07 18:08:44 -08001804 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1805 kfree(cs);
1806 return ERR_PTR(-ENOMEM);
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 cs->flags = 0;
Paul Jackson825a46a2006-03-24 03:16:03 -08001810 if (is_spread_page(parent))
1811 set_bit(CS_SPREAD_PAGE, &cs->flags);
1812 if (is_spread_slab(parent))
1813 set_bit(CS_SPREAD_SLAB, &cs->flags);
Paul Jackson029190c2007-10-18 23:40:20 -07001814 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001815 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001816 nodes_clear(cs->mems_allowed);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001817 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001818 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 cs->parent = parent;
Paul Jackson202f72d2006-01-08 01:01:57 -08001821 number_of_cpusets++;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001822
1823 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cont->flags))
1824 goto skip_clone;
1825
1826 /*
1827 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
1828 * set. This flag handling is implemented in cgroup core for
1829 * histrical reasons - the flag may be specified during mount.
1830 *
1831 * Currently, if any sibling cpusets have exclusive cpus or mem, we
1832 * refuse to clone the configuration - thereby refusing the task to
1833 * be entered, and as a result refusing the sys_unshare() or
1834 * clone() which initiated it. If this becomes a problem for some
1835 * users who wish to allow that scenario, then this could be
1836 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
1837 * (and likewise for mems) to the new cgroup.
1838 */
1839 list_for_each_entry(tmp_cg, &parent_cg->children, sibling) {
1840 struct cpuset *tmp_cs = cgroup_cs(tmp_cg);
1841
1842 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs))
1843 goto skip_clone;
1844 }
1845
1846 mutex_lock(&callback_mutex);
1847 cs->mems_allowed = parent->mems_allowed;
1848 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
1849 mutex_unlock(&callback_mutex);
1850skip_clone:
1851 return &cs->css;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Paul Jackson029190c2007-10-18 23:40:20 -07001854/*
Paul Jackson029190c2007-10-18 23:40:20 -07001855 * If the cpuset being removed has its flag 'sched_load_balance'
1856 * enabled, then simulate turning sched_load_balance off, which
Max Krasnyanskycf417142008-08-11 14:33:53 -07001857 * will call async_rebuild_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -07001858 */
1859
Tejun Heo92fb9742012-11-19 08:13:38 -08001860static void cpuset_css_free(struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Paul Menage8793d852007-10-18 23:39:39 -07001862 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Paul Jackson029190c2007-10-18 23:40:20 -07001864 if (is_sched_load_balance(cs))
Paul Menage700fe1a2008-04-29 01:00:00 -07001865 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
Paul Jackson029190c2007-10-18 23:40:20 -07001866
Paul Jackson202f72d2006-01-08 01:01:57 -08001867 number_of_cpusets--;
Li Zefan300ed6c2009-01-07 18:08:44 -08001868 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001869 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Paul Menage8793d852007-10-18 23:39:39 -07001872struct cgroup_subsys cpuset_subsys = {
1873 .name = "cpuset",
Tejun Heo92fb9742012-11-19 08:13:38 -08001874 .css_alloc = cpuset_css_alloc,
1875 .css_free = cpuset_css_free,
Paul Menage8793d852007-10-18 23:39:39 -07001876 .can_attach = cpuset_can_attach,
1877 .attach = cpuset_attach,
Paul Menage8793d852007-10-18 23:39:39 -07001878 .subsys_id = cpuset_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001879 .base_cftypes = files,
Paul Menage8793d852007-10-18 23:39:39 -07001880 .early_init = 1,
1881};
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883/**
1884 * cpuset_init - initialize cpusets at system boot
1885 *
1886 * Description: Initialize top_cpuset and the cpuset internal file system,
1887 **/
1888
1889int __init cpuset_init(void)
1890{
Paul Menage8793d852007-10-18 23:39:39 -07001891 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Miao Xie58568d22009-06-16 15:31:49 -07001893 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
1894 BUG();
1895
Li Zefan300ed6c2009-01-07 18:08:44 -08001896 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001897 nodes_setall(top_cpuset.mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001899 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07001900 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001901 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 err = register_filesystem(&cpuset_fs_type);
1904 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07001905 return err;
1906
Li Zefan2341d1b2009-01-07 18:08:42 -08001907 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1908 BUG();
1909
Paul Jackson202f72d2006-01-08 01:01:57 -08001910 number_of_cpusets = 1;
Paul Menage8793d852007-10-18 23:39:39 -07001911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Cliff Wickman956db3c2008-02-07 00:14:43 -08001914/**
1915 * cpuset_do_move_task - move a given task to another cpuset
1916 * @tsk: pointer to task_struct the task to move
1917 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1918 *
1919 * Called by cgroup_scan_tasks() for each task in a cgroup.
1920 * Return nonzero to stop the walk through the tasks.
1921 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -07001922static void cpuset_do_move_task(struct task_struct *tsk,
1923 struct cgroup_scanner *scan)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001924{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001925 struct cgroup *new_cgroup = scan->data;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001926
Li Zefan7f81b1a2009-04-02 16:57:53 -07001927 cgroup_attach_task(new_cgroup, tsk);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001928}
1929
1930/**
1931 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
1932 * @from: cpuset in which the tasks currently reside
1933 * @to: cpuset to which the tasks will be moved
1934 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001935 * Called with cgroup_mutex held
1936 * callback_mutex must not be held, as cpuset_attach() will take it.
Cliff Wickman956db3c2008-02-07 00:14:43 -08001937 *
1938 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1939 * calling callback functions for each.
1940 */
1941static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
1942{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001943 struct cgroup_scanner scan;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001944
Li Zefan7f81b1a2009-04-02 16:57:53 -07001945 scan.cg = from->css.cgroup;
1946 scan.test_task = NULL; /* select all tasks in cgroup */
1947 scan.process_task = cpuset_do_move_task;
1948 scan.heap = NULL;
1949 scan.data = to->css.cgroup;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001950
Li Zefan7f81b1a2009-04-02 16:57:53 -07001951 if (cgroup_scan_tasks(&scan))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001952 printk(KERN_ERR "move_member_tasks_to_cpuset: "
1953 "cgroup_scan_tasks failed\n");
1954}
1955
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001956/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07001957 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001958 * or memory nodes, we need to walk over the cpuset hierarchy,
1959 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001960 * last CPU or node from a cpuset, then move the tasks in the empty
1961 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001962 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001963 * Called with cgroup_mutex held
1964 * callback_mutex must not be held, as cpuset_attach() will take it.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001965 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001966static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001967{
Cliff Wickman956db3c2008-02-07 00:14:43 -08001968 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001969
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001970 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08001971 * Find its next-highest non-empty parent, (top cpuset
1972 * has online cpus, so can't be empty).
1973 */
1974 parent = cs->parent;
Li Zefan300ed6c2009-01-07 18:08:44 -08001975 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08001976 nodes_empty(parent->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001977 parent = parent->parent;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001978
1979 move_member_tasks_to_cpuset(cs, parent);
1980}
1981
1982/*
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05301983 * Helper function to traverse cpusets.
1984 * It can be used to walk the cpuset tree from top to bottom, completing
1985 * one layer before dropping down to the next (thus always processing a
1986 * node before any of its children).
1987 */
1988static struct cpuset *cpuset_next(struct list_head *queue)
1989{
1990 struct cpuset *cp;
1991 struct cpuset *child; /* scans child cpusets of cp */
1992 struct cgroup *cont;
1993
1994 if (list_empty(queue))
1995 return NULL;
1996
1997 cp = list_first_entry(queue, struct cpuset, stack_list);
1998 list_del(queue->next);
1999 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
2000 child = cgroup_cs(cont);
2001 list_add_tail(&child->stack_list, queue);
2002 }
2003
2004 return cp;
2005}
2006
2007
2008/*
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302009 * Walk the specified cpuset subtree upon a hotplug operation (CPU/Memory
2010 * online/offline) and update the cpusets accordingly.
2011 * For regular CPU/Mem hotplug, look for empty cpusets; the tasks of such
2012 * cpuset must be moved to a parent cpuset.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002013 *
Paul Menage2df167a2008-02-07 00:14:45 -08002014 * Called with cgroup_mutex held. We take callback_mutex to modify
Cliff Wickman956db3c2008-02-07 00:14:43 -08002015 * cpus_allowed and mems_allowed.
2016 *
2017 * This walk processes the tree from top to bottom, completing one layer
2018 * before dropping down to the next. It always processes a node before
2019 * any of its children.
2020 *
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002021 * In the case of memory hot-unplug, it will remove nodes from N_MEMORY
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302022 * if all present pages from a node are offlined.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002023 */
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302024static void
2025scan_cpusets_upon_hotplug(struct cpuset *root, enum hotplug_event event)
Cliff Wickman956db3c2008-02-07 00:14:43 -08002026{
Li Zefan8d1e6262008-07-29 22:33:21 -07002027 LIST_HEAD(queue);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302028 struct cpuset *cp; /* scans cpusets being updated */
Li Zefanee24d372011-03-23 16:42:47 -07002029 static nodemask_t oldmems; /* protected by cgroup_mutex */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002030
Cliff Wickman956db3c2008-02-07 00:14:43 -08002031 list_add_tail((struct list_head *)&root->stack_list, &queue);
2032
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302033 switch (event) {
2034 case CPUSET_CPU_OFFLINE:
2035 while ((cp = cpuset_next(&queue)) != NULL) {
Paul Jacksonb4501292008-02-07 00:14:47 -08002036
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302037 /* Continue past cpusets with all cpus online */
2038 if (cpumask_subset(cp->cpus_allowed, cpu_active_mask))
2039 continue;
Paul Jacksonb4501292008-02-07 00:14:47 -08002040
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302041 /* Remove offline cpus from this cpuset. */
2042 mutex_lock(&callback_mutex);
2043 cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
2044 cpu_active_mask);
2045 mutex_unlock(&callback_mutex);
Miao Xief9b4fb82008-07-25 01:47:22 -07002046
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302047 /* Move tasks from the empty cpuset to a parent */
2048 if (cpumask_empty(cp->cpus_allowed))
2049 remove_tasks_in_empty_cpuset(cp);
2050 else
2051 update_tasks_cpumask(cp, NULL);
2052 }
2053 break;
2054
2055 case CPUSET_MEM_OFFLINE:
2056 while ((cp = cpuset_next(&queue)) != NULL) {
2057
2058 /* Continue past cpusets with all mems online */
2059 if (nodes_subset(cp->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002060 node_states[N_MEMORY]))
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302061 continue;
2062
2063 oldmems = cp->mems_allowed;
2064
2065 /* Remove offline mems from this cpuset. */
2066 mutex_lock(&callback_mutex);
2067 nodes_and(cp->mems_allowed, cp->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002068 node_states[N_MEMORY]);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302069 mutex_unlock(&callback_mutex);
Paul Jacksonb4501292008-02-07 00:14:47 -08002070
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302071 /* Move tasks from the empty cpuset to a parent */
2072 if (nodes_empty(cp->mems_allowed))
2073 remove_tasks_in_empty_cpuset(cp);
2074 else
2075 update_tasks_nodemask(cp, &oldmems, NULL);
Miao Xief9b4fb82008-07-25 01:47:22 -07002076 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002077 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002078}
2079
2080/*
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002081 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2082 * period. This is necessary in order to make cpusets transparent
2083 * (of no affect) on systems that are actively using CPU hotplug
2084 * but making no active use of cpusets.
2085 *
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05302086 * The only exception to this is suspend/resume, where we don't
2087 * modify cpusets at all.
2088 *
Paul Jackson38837fc2006-09-29 02:01:16 -07002089 * This routine ensures that top_cpuset.cpus_allowed tracks
Tejun Heo3a101d02010-06-08 21:40:36 +02002090 * cpu_active_mask on each CPU hotplug (cpuhp) event.
Max Krasnyanskycf417142008-08-11 14:33:53 -07002091 *
2092 * Called within get_online_cpus(). Needs to call cgroup_lock()
2093 * before calling generate_sched_domains().
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302094 *
2095 * @cpu_online: Indicates whether this is a CPU online event (true) or
2096 * a CPU offline event (false).
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002097 */
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302098void cpuset_update_active_cpus(bool cpu_online)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002099{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002100 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10302101 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002102 int ndoms;
2103
Max Krasnyanskycf417142008-08-11 14:33:53 -07002104 cgroup_lock();
Li Zefan0b4217b2009-04-02 16:57:49 -07002105 mutex_lock(&callback_mutex);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002106 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Li Zefan0b4217b2009-04-02 16:57:49 -07002107 mutex_unlock(&callback_mutex);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302108
2109 if (!cpu_online)
2110 scan_cpusets_upon_hotplug(&top_cpuset, CPUSET_CPU_OFFLINE);
2111
Max Krasnyanskycf417142008-08-11 14:33:53 -07002112 ndoms = generate_sched_domains(&doms, &attr);
2113 cgroup_unlock();
2114
2115 /* Have scheduler rebuild the domains */
2116 partition_sched_domains(ndoms, doms, attr);
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002117}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002118
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002119#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002120/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002121 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2122 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302123 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07002124 */
Miao Xief4818912008-11-19 15:36:30 -08002125static int cpuset_track_online_nodes(struct notifier_block *self,
2126 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002127{
Li Zefanee24d372011-03-23 16:42:47 -07002128 static nodemask_t oldmems; /* protected by cgroup_mutex */
Miao Xie5ab116c2010-03-23 13:35:34 -07002129
Max Krasnyanskycf417142008-08-11 14:33:53 -07002130 cgroup_lock();
Miao Xief4818912008-11-19 15:36:30 -08002131 switch (action) {
2132 case MEM_ONLINE:
Li Zefanee24d372011-03-23 16:42:47 -07002133 oldmems = top_cpuset.mems_allowed;
Li Zefan0b4217b2009-04-02 16:57:49 -07002134 mutex_lock(&callback_mutex);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002135 top_cpuset.mems_allowed = node_states[N_MEMORY];
Li Zefan0b4217b2009-04-02 16:57:49 -07002136 mutex_unlock(&callback_mutex);
Li Zefanee24d372011-03-23 16:42:47 -07002137 update_tasks_nodemask(&top_cpuset, &oldmems, NULL);
Miao Xie5ab116c2010-03-23 13:35:34 -07002138 break;
2139 case MEM_OFFLINE:
2140 /*
2141 * needn't update top_cpuset.mems_allowed explicitly because
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302142 * scan_cpusets_upon_hotplug() will update it.
Miao Xie5ab116c2010-03-23 13:35:34 -07002143 */
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302144 scan_cpusets_upon_hotplug(&top_cpuset, CPUSET_MEM_OFFLINE);
Miao Xief4818912008-11-19 15:36:30 -08002145 break;
2146 default:
2147 break;
2148 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002149 cgroup_unlock();
Miao Xie53feb292010-03-23 13:35:35 -07002150
Miao Xief4818912008-11-19 15:36:30 -08002151 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002152}
2153#endif
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/**
2156 * cpuset_init_smp - initialize cpus_allowed
2157 *
2158 * Description: Finish top cpuset after cpu, node maps are initialized
2159 **/
2160
2161void __init cpuset_init_smp(void)
2162{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002163 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002164 top_cpuset.mems_allowed = node_states[N_MEMORY];
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002165
Miao Xief4818912008-11-19 15:36:30 -08002166 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
Miao Xief90d4112009-01-16 10:24:10 +08002167
2168 cpuset_wq = create_singlethread_workqueue("cpuset");
2169 BUG_ON(!cpuset_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
2172/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2174 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002175 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002177 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10302179 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 * tasks cpuset.
2181 **/
2182
Li Zefan6af866a2009-01-07 18:08:45 -08002183void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002185 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002186 task_lock(tsk);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002187 guarantee_online_cpus(task_cs(tsk), pmask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002188 task_unlock(tsk);
Oleg Nesterov897f0b32010-03-15 10:10:03 +01002189 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002192void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002193{
2194 const struct cpuset *cs;
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002195
2196 rcu_read_lock();
2197 cs = task_cs(tsk);
2198 if (cs)
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09002199 do_set_cpus_allowed(tsk, cs->cpus_allowed);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002200 rcu_read_unlock();
2201
2202 /*
2203 * We own tsk->cpus_allowed, nobody can change it under us.
2204 *
2205 * But we used cs && cs->cpus_allowed lockless and thus can
2206 * race with cgroup_attach_task() or update_cpumask() and get
2207 * the wrong tsk->cpus_allowed. However, both cases imply the
2208 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2209 * which takes task_rq_lock().
2210 *
2211 * If we are called after it dropped the lock we must see all
2212 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2213 * set any mask even if it is not right from task_cs() pov,
2214 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002215 *
2216 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2217 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002218 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221void cpuset_init_current_mems_allowed(void)
2222{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002223 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002226/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002227 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2228 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2229 *
2230 * Description: Returns the nodemask_t mems_allowed of the cpuset
2231 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002232 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002233 * tasks cpuset.
2234 **/
2235
2236nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2237{
2238 nodemask_t mask;
2239
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002240 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002241 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -07002242 guarantee_online_mems(task_cs(tsk), &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002243 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002244 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002245
2246 return mask;
2247}
2248
2249/**
Mel Gorman19770b32008-04-28 02:12:18 -07002250 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2251 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002252 *
Mel Gorman19770b32008-04-28 02:12:18 -07002253 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
Mel Gorman19770b32008-04-28 02:12:18 -07002255int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Mel Gorman19770b32008-04-28 02:12:18 -07002257 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Paul Jackson9bf22292005-09-06 15:18:12 -07002260/*
Paul Menage78608362008-04-29 01:00:26 -07002261 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2262 * mem_hardwall ancestor to the specified cpuset. Call holding
2263 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2264 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 */
Paul Menage78608362008-04-29 01:00:26 -07002266static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Paul Menage78608362008-04-29 01:00:26 -07002268 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
Paul Jackson9bf22292005-09-06 15:18:12 -07002269 cs = cs->parent;
2270 return cs;
2271}
2272
2273/**
David Rientjesa1bc5a42009-04-02 16:57:54 -07002274 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2275 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002276 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002277 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002278 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2279 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2280 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2281 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2282 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2283 * flag, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002284 * Otherwise, no.
2285 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002286 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2287 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2288 * might sleep, and might allow a node from an enclosing cpuset.
Paul Jackson02a0e532006-12-13 00:34:25 -08002289 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002290 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2291 * cpusets, and never sleeps.
Paul Jackson02a0e532006-12-13 00:34:25 -08002292 *
2293 * The __GFP_THISNODE placement logic is really handled elsewhere,
2294 * by forcibly using a zonelist starting at a specified node, and by
2295 * (in get_page_from_freelist()) refusing to consider the zones for
2296 * any node on the zonelist except the first. By the time any such
2297 * calls get to this routine, we should just shut up and say 'yes'.
2298 *
Paul Jackson9bf22292005-09-06 15:18:12 -07002299 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002300 * and do not allow allocations outside the current tasks cpuset
2301 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002302 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002303 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002304 *
Paul Jackson02a0e532006-12-13 00:34:25 -08002305 * Scanning up parent cpusets requires callback_mutex. The
2306 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2307 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2308 * current tasks mems_allowed came up empty on the first pass over
2309 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2310 * cpuset are short of memory, might require taking the callback_mutex
2311 * mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002312 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002313 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002314 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2315 * so no allocation on a node outside the cpuset is allowed (unless
2316 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002317 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002318 * The second pass through get_page_from_freelist() doesn't even call
2319 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2320 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2321 * in alloc_flags. That logic and the checks below have the combined
2322 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002323 * in_interrupt - any node ok (current task context irrelevant)
2324 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002325 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002326 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002327 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002328 *
2329 * Rule:
David Rientjesa1bc5a42009-04-02 16:57:54 -07002330 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
Paul Jackson36be57f2006-05-20 15:00:10 -07002331 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2332 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson02a0e532006-12-13 00:34:25 -08002333 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002334int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002335{
Paul Jackson9bf22292005-09-06 15:18:12 -07002336 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002337 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002338
Christoph Lameter9b819d22006-09-25 23:31:40 -07002339 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002340 return 1;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002341 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002342 if (node_isset(node, current->mems_allowed))
2343 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002344 /*
2345 * Allow tasks that have access to memory reserves because they have
2346 * been OOM killed to get memory anywhere.
2347 */
2348 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2349 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002350 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2351 return 0;
2352
Bob Picco5563e772005-11-13 16:06:35 -08002353 if (current->flags & PF_EXITING) /* Let dying task have memory */
2354 return 1;
2355
Paul Jackson9bf22292005-09-06 15:18:12 -07002356 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002357 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002358
Paul Jackson053199e2005-10-30 15:02:30 -08002359 task_lock(current);
Paul Menage78608362008-04-29 01:00:26 -07002360 cs = nearest_hardwall_ancestor(task_cs(current));
Paul Jackson053199e2005-10-30 15:02:30 -08002361 task_unlock(current);
2362
Paul Jackson9bf22292005-09-06 15:18:12 -07002363 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002364 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002365 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Paul Jackson02a0e532006-12-13 00:34:25 -08002368/*
David Rientjesa1bc5a42009-04-02 16:57:54 -07002369 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2370 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002371 * @gfp_mask: memory allocation flags
2372 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002373 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2374 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2375 * yes. If the task has been OOM killed and has access to memory reserves as
2376 * specified by the TIF_MEMDIE flag, yes.
2377 * Otherwise, no.
Paul Jackson02a0e532006-12-13 00:34:25 -08002378 *
2379 * The __GFP_THISNODE placement logic is really handled elsewhere,
2380 * by forcibly using a zonelist starting at a specified node, and by
2381 * (in get_page_from_freelist()) refusing to consider the zones for
2382 * any node on the zonelist except the first. By the time any such
2383 * calls get to this routine, we should just shut up and say 'yes'.
2384 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002385 * Unlike the cpuset_node_allowed_softwall() variant, above,
2386 * this variant requires that the node be in the current task's
Paul Jackson02a0e532006-12-13 00:34:25 -08002387 * mems_allowed or that we're in interrupt. It does not scan up the
2388 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2389 * It never sleeps.
2390 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002391int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -08002392{
Paul Jackson02a0e532006-12-13 00:34:25 -08002393 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2394 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002395 if (node_isset(node, current->mems_allowed))
2396 return 1;
Daniel Walkerdedf8b72007-10-18 03:06:04 -07002397 /*
2398 * Allow tasks that have access to memory reserves because they have
2399 * been OOM killed to get memory anywhere.
2400 */
2401 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2402 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002403 return 0;
2404}
2405
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002406/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07002407 * cpuset_mem_spread_node() - On which node to begin search for a file page
2408 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08002409 *
2410 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2411 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2412 * and if the memory allocation used cpuset_mem_spread_node()
2413 * to determine on which node to start looking, as it will for
2414 * certain page cache or slab cache pages such as used for file
2415 * system buffers and inode caches, then instead of starting on the
2416 * local node to look for a free page, rather spread the starting
2417 * node around the tasks mems_allowed nodes.
2418 *
2419 * We don't have to worry about the returned node being offline
2420 * because "it can't happen", and even if it did, it would be ok.
2421 *
2422 * The routines calling guarantee_online_mems() are careful to
2423 * only set nodes in task->mems_allowed that are online. So it
2424 * should not be possible for the following code to return an
2425 * offline node. But if it did, that would be ok, as this routine
2426 * is not returning the node where the allocation must be, only
2427 * the node where the search should start. The zonelist passed to
2428 * __alloc_pages() will include all nodes. If the slab allocator
2429 * is passed an offline node, it will fall back to the local node.
2430 * See kmem_cache_alloc_node().
2431 */
2432
Jack Steiner6adef3e2010-05-26 14:42:49 -07002433static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08002434{
2435 int node;
2436
Jack Steiner6adef3e2010-05-26 14:42:49 -07002437 node = next_node(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08002438 if (node == MAX_NUMNODES)
2439 node = first_node(current->mems_allowed);
Jack Steiner6adef3e2010-05-26 14:42:49 -07002440 *rotor = node;
Paul Jackson825a46a2006-03-24 03:16:03 -08002441 return node;
2442}
Jack Steiner6adef3e2010-05-26 14:42:49 -07002443
2444int cpuset_mem_spread_node(void)
2445{
Michal Hocko778d3b02011-07-26 16:08:30 -07002446 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2447 current->cpuset_mem_spread_rotor =
2448 node_random(&current->mems_allowed);
2449
Jack Steiner6adef3e2010-05-26 14:42:49 -07002450 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2451}
2452
2453int cpuset_slab_spread_node(void)
2454{
Michal Hocko778d3b02011-07-26 16:08:30 -07002455 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2456 current->cpuset_slab_spread_rotor =
2457 node_random(&current->mems_allowed);
2458
Jack Steiner6adef3e2010-05-26 14:42:49 -07002459 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2460}
2461
Paul Jackson825a46a2006-03-24 03:16:03 -08002462EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2463
2464/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002465 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2466 * @tsk1: pointer to task_struct of some task.
2467 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002468 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002469 * Description: Return true if @tsk1's mems_allowed intersects the
2470 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2471 * one of the task's memory usage might impact the memory available
2472 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002473 **/
2474
David Rientjesbbe373f2007-10-16 23:25:58 -07002475int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2476 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002477{
David Rientjesbbe373f2007-10-16 23:25:58 -07002478 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002479}
2480
David Rientjes75aa1992009-01-06 14:39:01 -08002481/**
2482 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2483 * @task: pointer to task_struct of some task.
2484 *
2485 * Description: Prints @task's name, cpuset name, and cached copy of its
2486 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2487 * dereferencing task_cs(task).
2488 */
2489void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2490{
2491 struct dentry *dentry;
2492
2493 dentry = task_cs(tsk)->css.cgroup->dentry;
2494 spin_lock(&cpuset_buffer_lock);
2495 snprintf(cpuset_name, CPUSET_NAME_LEN,
2496 dentry ? (const char *)dentry->d_name.name : "/");
2497 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2498 tsk->mems_allowed);
2499 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2500 tsk->comm, cpuset_name, cpuset_nodelist);
2501 spin_unlock(&cpuset_buffer_lock);
2502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002505 * Collection of memory_pressure is suppressed unless
2506 * this flag is enabled by writing "1" to the special
2507 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2508 */
2509
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002510int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002511
2512/**
2513 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2514 *
2515 * Keep a running average of the rate of synchronous (direct)
2516 * page reclaim efforts initiated by tasks in each cpuset.
2517 *
2518 * This represents the rate at which some task in the cpuset
2519 * ran low on memory on all nodes it was allowed to use, and
2520 * had to enter the kernels page reclaim code in an effort to
2521 * create more free memory by tossing clean pages or swapping
2522 * or writing dirty pages.
2523 *
2524 * Display to user space in the per-cpuset read-only file
2525 * "memory_pressure". Value displayed is an integer
2526 * representing the recent rate of entry into the synchronous
2527 * (direct) page reclaim by any task attached to the cpuset.
2528 **/
2529
2530void __cpuset_memory_pressure_bump(void)
2531{
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002532 task_lock(current);
Paul Menage8793d852007-10-18 23:39:39 -07002533 fmeter_markevent(&task_cs(current)->fmeter);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002534 task_unlock(current);
2535}
2536
Paul Menage8793d852007-10-18 23:39:39 -07002537#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002538/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * proc_cpuset_show()
2540 * - Print tasks cpuset path into seq_file.
2541 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002542 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2543 * doesn't really matter if tsk->cpuset changes after we read it,
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002544 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002545 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 */
Paul Jackson029190c2007-10-18 23:40:20 -07002547static int proc_cpuset_show(struct seq_file *m, void *unused_v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002549 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct task_struct *tsk;
2551 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002552 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002553 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Eric W. Biederman99f89552006-06-26 00:25:55 -07002555 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2557 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Eric W. Biederman99f89552006-06-26 00:25:55 -07002560 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002561 pid = m->private;
2562 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002563 if (!tsk)
2564 goto out_free;
2565
2566 retval = -EINVAL;
Paul Menage8793d852007-10-18 23:39:39 -07002567 cgroup_lock();
2568 css = task_subsys_state(tsk, cpuset_subsys_id);
2569 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002571 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 seq_puts(m, buf);
2573 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002574out_unlock:
Paul Menage8793d852007-10-18 23:39:39 -07002575 cgroup_unlock();
Eric W. Biederman99f89552006-06-26 00:25:55 -07002576 put_task_struct(tsk);
2577out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002579out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return retval;
2581}
2582
2583static int cpuset_open(struct inode *inode, struct file *file)
2584{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002585 struct pid *pid = PROC_I(inode)->pid;
2586 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
Arjan van de Ven9a321442007-02-12 00:55:35 -08002589const struct file_operations proc_cpuset_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 .open = cpuset_open,
2591 .read = seq_read,
2592 .llseek = seq_lseek,
2593 .release = single_release,
2594};
Paul Menage8793d852007-10-18 23:39:39 -07002595#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Heiko Carstensd01d4822009-09-21 11:06:27 +02002597/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002598void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002600 seq_printf(m, "Mems_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002601 seq_nodemask(m, &task->mems_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002602 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002603 seq_printf(m, "Mems_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002604 seq_nodemask_list(m, &task->mems_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002605 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}