blob: 4b054b9faf3db48a9bbbf47f17438fae515a395a [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 {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800141 CS_ONLINE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 CS_CPU_EXCLUSIVE,
143 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700144 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800145 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700146 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800147 CS_SPREAD_PAGE,
148 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149} cpuset_flagbits_t;
150
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +0530151/* the type of hotplug event */
152enum hotplug_event {
153 CPUSET_CPU_OFFLINE,
154 CPUSET_MEM_OFFLINE,
155};
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* convenient tests for these bits */
Tejun Heoefeb77b2013-01-07 08:51:07 -0800158static inline bool is_cpuset_online(const struct cpuset *cs)
159{
160 return test_bit(CS_ONLINE, &cs->flags);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static inline int is_cpu_exclusive(const struct cpuset *cs)
164{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800165 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168static inline int is_mem_exclusive(const struct cpuset *cs)
169{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800170 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Paul Menage78608362008-04-29 01:00:26 -0700173static inline int is_mem_hardwall(const struct cpuset *cs)
174{
175 return test_bit(CS_MEM_HARDWALL, &cs->flags);
176}
177
Paul Jackson029190c2007-10-18 23:40:20 -0700178static inline int is_sched_load_balance(const struct cpuset *cs)
179{
180 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
181}
182
Paul Jackson45b07ef2006-01-08 01:00:56 -0800183static inline int is_memory_migrate(const struct cpuset *cs)
184{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800185 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800186}
187
Paul Jackson825a46a2006-03-24 03:16:03 -0800188static inline int is_spread_page(const struct cpuset *cs)
189{
190 return test_bit(CS_SPREAD_PAGE, &cs->flags);
191}
192
193static inline int is_spread_slab(const struct cpuset *cs)
194{
195 return test_bit(CS_SPREAD_SLAB, &cs->flags);
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static struct cpuset top_cpuset = {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800199 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) |
200 (1 << CS_MEM_EXCLUSIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Tejun Heoae8086c2013-01-07 08:51:07 -0800203/**
204 * cpuset_for_each_child - traverse online children of a cpuset
205 * @child_cs: loop cursor pointing to the current child
206 * @pos_cgrp: used for iteration
207 * @parent_cs: target cpuset to walk children of
208 *
209 * Walk @child_cs through the online children of @parent_cs. Must be used
210 * with RCU read locked.
211 */
212#define cpuset_for_each_child(child_cs, pos_cgrp, parent_cs) \
213 cgroup_for_each_child((pos_cgrp), (parent_cs)->css.cgroup) \
214 if (is_cpuset_online(((child_cs) = cgroup_cs((pos_cgrp)))))
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Paul Menage2df167a2008-02-07 00:14:45 -0800217 * There are two global mutexes guarding cpuset structures. The first
218 * is the main control groups cgroup_mutex, accessed via
219 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
220 * callback_mutex, below. They can nest. It is ok to first take
221 * cgroup_mutex, then nest callback_mutex. We also require taking
222 * task_lock() when dereferencing a task's cpuset pointer. See "The
223 * task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800225 * A task must hold both mutexes to modify cpusets. If a task
Paul Menage2df167a2008-02-07 00:14:45 -0800226 * holds cgroup_mutex, then it blocks others wanting that mutex,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800227 * ensuring that it is the only task able to also acquire callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800228 * and be able to modify cpusets. It can perform various checks on
229 * the cpuset structure first, knowing nothing will change. It can
Paul Menage2df167a2008-02-07 00:14:45 -0800230 * also allocate memory while just holding cgroup_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800231 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800232 * acquire callback_mutex to query cpusets. Once it is ready to make
233 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
Paul Jackson053199e2005-10-30 15:02:30 -0800235 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800236 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800237 * from one of the callbacks into the cpuset code from within
238 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800240 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800241 * access to cpusets.
242 *
Miao Xie58568d22009-06-16 15:31:49 -0700243 * Now, the task_struct fields mems_allowed and mempolicy may be changed
244 * by other task, we use alloc_lock in the task_struct fields to protect
245 * them.
Paul Jackson053199e2005-10-30 15:02:30 -0800246 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800247 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800248 * small pieces of code, such as when reading out possibly multi-word
249 * cpumasks and nodemasks.
250 *
Paul Menage2df167a2008-02-07 00:14:45 -0800251 * Accessing a task's cpuset should be done in accordance with the
252 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800255static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700256
Max Krasnyanskycf417142008-08-11 14:33:53 -0700257/*
David Rientjes75aa1992009-01-06 14:39:01 -0800258 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
259 * buffers. They are statically allocated to prevent using excess stack
260 * when calling cpuset_print_task_mems_allowed().
261 */
262#define CPUSET_NAME_LEN (128)
263#define CPUSET_NODELIST_LEN (256)
264static char cpuset_name[CPUSET_NAME_LEN];
265static char cpuset_nodelist[CPUSET_NODELIST_LEN];
266static DEFINE_SPINLOCK(cpuset_buffer_lock);
267
268/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700269 * This is ugly, but preserves the userspace API for existing cpuset
Paul Menage8793d852007-10-18 23:39:39 -0700270 * users. If someone tries to mount the "cpuset" filesystem, we
Max Krasnyanskycf417142008-08-11 14:33:53 -0700271 * silently switch it to mount "cgroup" instead
272 */
Al Virof7e83572010-07-26 13:23:11 +0400273static struct dentry *cpuset_mount(struct file_system_type *fs_type,
274 int flags, const char *unused_dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Paul Menage8793d852007-10-18 23:39:39 -0700276 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
Al Virof7e83572010-07-26 13:23:11 +0400277 struct dentry *ret = ERR_PTR(-ENODEV);
Paul Menage8793d852007-10-18 23:39:39 -0700278 if (cgroup_fs) {
279 char mountopts[] =
280 "cpuset,noprefix,"
281 "release_agent=/sbin/cpuset_release_agent";
Al Virof7e83572010-07-26 13:23:11 +0400282 ret = cgroup_fs->mount(cgroup_fs, flags,
283 unused_dev_name, mountopts);
Paul Menage8793d852007-10-18 23:39:39 -0700284 put_filesystem(cgroup_fs);
285 }
286 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289static struct file_system_type cpuset_fs_type = {
290 .name = "cpuset",
Al Virof7e83572010-07-26 13:23:11 +0400291 .mount = cpuset_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800295 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * are online. If none are online, walk up the cpuset hierarchy
297 * until we find one that does have some online cpus. If we get
298 * all the way to the top and still haven't found any online cpus,
Rusty Russell5f054e32012-03-29 15:38:31 +1030299 * return cpu_online_mask. Or if passed a NULL cs from an exit'ing
300 * task, return cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
302 * One way or another, we guarantee to return some non-empty subset
Rusty Russell5f054e32012-03-29 15:38:31 +1030303 * of cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800305 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
307
Li Zefan6af866a2009-01-07 18:08:45 -0800308static void guarantee_online_cpus(const struct cpuset *cs,
309 struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Li Zefan300ed6c2009-01-07 18:08:44 -0800311 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 cs = cs->parent;
313 if (cs)
Li Zefan300ed6c2009-01-07 18:08:44 -0800314 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else
Li Zefan300ed6c2009-01-07 18:08:44 -0800316 cpumask_copy(pmask, cpu_online_mask);
317 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/*
321 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700322 * are online, with memory. If none are online with memory, walk
323 * up the cpuset hierarchy until we find one that does have some
324 * online mems. If we get all the way to the top and still haven't
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800325 * found any online mems, return node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
327 * One way or another, we guarantee to return some non-empty subset
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800328 * of node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800330 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
332
333static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
334{
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700335 while (cs && !nodes_intersects(cs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800336 node_states[N_MEMORY]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 cs = cs->parent;
338 if (cs)
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700339 nodes_and(*pmask, cs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800340 node_states[N_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 else
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800342 *pmask = node_states[N_MEMORY];
343 BUG_ON(!nodes_intersects(*pmask, node_states[N_MEMORY]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Miao Xief3b39d42009-06-16 15:31:46 -0700346/*
347 * update task's spread flag if cpuset's page/slab spread flag is set
348 *
349 * Called with callback_mutex/cgroup_mutex held
350 */
351static void cpuset_update_task_spread_flag(struct cpuset *cs,
352 struct task_struct *tsk)
353{
354 if (is_spread_page(cs))
355 tsk->flags |= PF_SPREAD_PAGE;
356 else
357 tsk->flags &= ~PF_SPREAD_PAGE;
358 if (is_spread_slab(cs))
359 tsk->flags |= PF_SPREAD_SLAB;
360 else
361 tsk->flags &= ~PF_SPREAD_SLAB;
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*
365 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
366 *
367 * One cpuset is a subset of another if all its allowed CPUs and
368 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Menage2df167a2008-02-07 00:14:45 -0800369 * are only set if the other's are set. Call holding cgroup_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
371
372static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
373{
Li Zefan300ed6c2009-01-07 18:08:44 -0800374 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 nodes_subset(p->mems_allowed, q->mems_allowed) &&
376 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
377 is_mem_exclusive(p) <= is_mem_exclusive(q);
378}
379
Li Zefan645fcc92009-01-07 18:08:43 -0800380/**
381 * alloc_trial_cpuset - allocate a trial cpuset
382 * @cs: the cpuset that the trial cpuset duplicates
383 */
384static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
385{
Li Zefan300ed6c2009-01-07 18:08:44 -0800386 struct cpuset *trial;
387
388 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
389 if (!trial)
390 return NULL;
391
392 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
393 kfree(trial);
394 return NULL;
395 }
396 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
397
398 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800399}
400
401/**
402 * free_trial_cpuset - free the trial cpuset
403 * @trial: the trial cpuset to be freed
404 */
405static void free_trial_cpuset(struct cpuset *trial)
406{
Li Zefan300ed6c2009-01-07 18:08:44 -0800407 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800408 kfree(trial);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
412 * validate_change() - Used to validate that any proposed cpuset change
413 * follows the structural rules for cpusets.
414 *
415 * If we replaced the flag and mask values of the current cpuset
416 * (cur) with those values in the trial cpuset (trial), would
417 * our various subset and exclusive rules still be valid? Presumes
Paul Menage2df167a2008-02-07 00:14:45 -0800418 * cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 *
420 * 'cur' is the address of an actual, in-use cpuset. Operations
421 * such as list traversal that depend on the actual address of the
422 * cpuset in the list must use cur below, not trial.
423 *
424 * 'trial' is the address of bulk structure copy of cur, with
425 * perhaps one or more of the fields cpus_allowed, mems_allowed,
426 * or flags changed to new, trial values.
427 *
428 * Return 0 if valid, -errno if not.
429 */
430
431static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
432{
Paul Menage8793d852007-10-18 23:39:39 -0700433 struct cgroup *cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct cpuset *c, *par;
Tejun Heoae8086c2013-01-07 08:51:07 -0800435 int ret;
436
437 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* Each of our child cpusets must be a subset of us */
Tejun Heoae8086c2013-01-07 08:51:07 -0800440 ret = -EBUSY;
441 cpuset_for_each_child(c, cont, cur)
442 if (!is_cpuset_subset(c, trial))
443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* Remaining checks don't apply to root cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800446 ret = 0;
Paul Jackson69604062006-12-06 20:36:15 -0800447 if (cur == &top_cpuset)
Tejun Heoae8086c2013-01-07 08:51:07 -0800448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Paul Jackson69604062006-12-06 20:36:15 -0800450 par = cur->parent;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* We must be a subset of our parent cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800453 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!is_cpuset_subset(trial, par))
Tejun Heoae8086c2013-01-07 08:51:07 -0800455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Paul Menage2df167a2008-02-07 00:14:45 -0800457 /*
458 * If either I or some sibling (!= me) is exclusive, we can't
459 * overlap
460 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800461 ret = -EINVAL;
462 cpuset_for_each_child(c, cont, par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
464 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800465 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800466 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
468 c != cur &&
469 nodes_intersects(trial->mems_allowed, c->mems_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
Paul Jackson020958b2007-10-18 23:40:21 -0700473 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
Tejun Heoae8086c2013-01-07 08:51:07 -0800474 ret = -ENOSPC;
475 if (cgroup_task_count(cur->css.cgroup) &&
476 (cpumask_empty(trial->cpus_allowed) ||
477 nodes_empty(trial->mems_allowed)))
478 goto out;
Paul Jackson020958b2007-10-18 23:40:21 -0700479
Tejun Heoae8086c2013-01-07 08:51:07 -0800480 ret = 0;
481out:
482 rcu_read_unlock();
483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Paul Menagedb7f47c2009-04-02 16:57:55 -0700486#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700487/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700488 * Helper routine for generate_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -0700489 * Do cpusets a, b have overlapping cpus_allowed masks?
490 */
Paul Jackson029190c2007-10-18 23:40:20 -0700491static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
492{
Li Zefan300ed6c2009-01-07 18:08:44 -0800493 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
Paul Jackson029190c2007-10-18 23:40:20 -0700494}
495
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900496static void
497update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
498{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900499 if (dattr->relax_domain_level < c->relax_domain_level)
500 dattr->relax_domain_level = c->relax_domain_level;
501 return;
502}
503
Lai Jiangshanf5393692008-07-29 22:33:22 -0700504static void
505update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
506{
507 LIST_HEAD(q);
508
509 list_add(&c->stack_list, &q);
510 while (!list_empty(&q)) {
511 struct cpuset *cp;
512 struct cgroup *cont;
513 struct cpuset *child;
514
515 cp = list_first_entry(&q, struct cpuset, stack_list);
516 list_del(q.next);
517
Li Zefan300ed6c2009-01-07 18:08:44 -0800518 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700519 continue;
520
521 if (is_sched_load_balance(cp))
522 update_domain_attr(dattr, cp);
523
Tejun Heoae8086c2013-01-07 08:51:07 -0800524 rcu_read_lock();
525 cpuset_for_each_child(child, cont, cp)
Lai Jiangshanf5393692008-07-29 22:33:22 -0700526 list_add_tail(&child->stack_list, &q);
Tejun Heoae8086c2013-01-07 08:51:07 -0800527 rcu_read_unlock();
Lai Jiangshanf5393692008-07-29 22:33:22 -0700528 }
529}
530
Paul Jackson029190c2007-10-18 23:40:20 -0700531/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700532 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700533 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700534 * This function builds a partial partition of the systems CPUs
535 * A 'partial partition' is a set of non-overlapping subsets whose
536 * union is a subset of that set.
537 * The output of this function needs to be passed to kernel/sched.c
538 * partition_sched_domains() routine, which will rebuild the scheduler's
539 * load balancing domains (sched domains) as specified by that partial
540 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700541 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800542 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700543 * for a background explanation of this.
544 *
545 * Does not return errors, on the theory that the callers of this
546 * routine would rather not worry about failures to rebuild sched
547 * domains when operating in the severe memory shortage situations
548 * that could cause allocation failures below.
549 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700550 * Must be called with cgroup_lock held.
Paul Jackson029190c2007-10-18 23:40:20 -0700551 *
552 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700553 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700554 * top-down scan of all cpusets. This scan loads a pointer
555 * to each cpuset marked is_sched_load_balance into the
556 * array 'csa'. For our purposes, rebuilding the schedulers
557 * sched domains, we can ignore !is_sched_load_balance cpusets.
558 * csa - (for CpuSet Array) Array of pointers to all the cpusets
559 * that need to be load balanced, for convenient iterative
560 * access by the subsequent code that finds the best partition,
561 * i.e the set of domains (subsets) of CPUs such that the
562 * cpus_allowed of every cpuset marked is_sched_load_balance
563 * is a subset of one of these domains, while there are as
564 * many such domains as possible, each as small as possible.
565 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
566 * the kernel/sched.c routine partition_sched_domains() in a
567 * convenient format, that can be easily compared to the prior
568 * value to determine what partition elements (sched domains)
569 * were changed (added or removed.)
570 *
571 * Finding the best partition (set of domains):
572 * The triple nested loops below over i, j, k scan over the
573 * load balanced cpusets (using the array of cpuset pointers in
574 * csa[]) looking for pairs of cpusets that have overlapping
575 * cpus_allowed, but which don't have the same 'pn' partition
576 * number and gives them in the same partition number. It keeps
577 * looping on the 'restart' label until it can no longer find
578 * any such pairs.
579 *
580 * The union of the cpus_allowed masks from the set of
581 * all cpusets having the same 'pn' value then form the one
582 * element of the partition (one sched domain) to be passed to
583 * partition_sched_domains().
584 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030585static int generate_sched_domains(cpumask_var_t **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700586 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700587{
Max Krasnyanskycf417142008-08-11 14:33:53 -0700588 LIST_HEAD(q); /* queue of cpusets to be scanned */
Paul Jackson029190c2007-10-18 23:40:20 -0700589 struct cpuset *cp; /* scans q */
590 struct cpuset **csa; /* array of all cpuset ptrs */
591 int csn; /* how many cpuset ptrs in csa so far */
592 int i, j, k; /* indices for partition finding loops */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030593 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900594 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100595 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800596 int nslot; /* next empty doms[] struct cpumask slot */
Paul Jackson029190c2007-10-18 23:40:20 -0700597
Paul Jackson029190c2007-10-18 23:40:20 -0700598 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900599 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700600 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700601
602 /* Special case for the 99% of systems with one, full, sched domain */
603 if (is_sched_load_balance(&top_cpuset)) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030604 ndoms = 1;
605 doms = alloc_sched_domains(ndoms);
Paul Jackson029190c2007-10-18 23:40:20 -0700606 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700607 goto done;
608
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900609 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
610 if (dattr) {
611 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700612 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900613 }
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030614 cpumask_copy(doms[0], top_cpuset.cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700615
Max Krasnyanskycf417142008-08-11 14:33:53 -0700616 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700617 }
618
Paul Jackson029190c2007-10-18 23:40:20 -0700619 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
620 if (!csa)
621 goto done;
622 csn = 0;
623
Li Zefanaeed6822008-07-29 22:33:24 -0700624 list_add(&top_cpuset.stack_list, &q);
625 while (!list_empty(&q)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700626 struct cgroup *cont;
627 struct cpuset *child; /* scans child cpusets of cp */
Lai Jiangshan489a5392008-07-25 01:47:23 -0700628
Li Zefanaeed6822008-07-29 22:33:24 -0700629 cp = list_first_entry(&q, struct cpuset, stack_list);
630 list_del(q.next);
631
Li Zefan300ed6c2009-01-07 18:08:44 -0800632 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshan489a5392008-07-25 01:47:23 -0700633 continue;
634
Lai Jiangshanf5393692008-07-29 22:33:22 -0700635 /*
636 * All child cpusets contain a subset of the parent's cpus, so
637 * just skip them, and then we call update_domain_attr_tree()
638 * to calc relax_domain_level of the corresponding sched
639 * domain.
640 */
641 if (is_sched_load_balance(cp)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700642 csa[csn++] = cp;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700643 continue;
644 }
Lai Jiangshan489a5392008-07-25 01:47:23 -0700645
Tejun Heoae8086c2013-01-07 08:51:07 -0800646 rcu_read_lock();
647 cpuset_for_each_child(child, cont, cp)
Li Zefanaeed6822008-07-29 22:33:24 -0700648 list_add_tail(&child->stack_list, &q);
Tejun Heoae8086c2013-01-07 08:51:07 -0800649 rcu_read_unlock();
Paul Jackson029190c2007-10-18 23:40:20 -0700650 }
651
652 for (i = 0; i < csn; i++)
653 csa[i]->pn = i;
654 ndoms = csn;
655
656restart:
657 /* Find the best partition (set of sched domains) */
658 for (i = 0; i < csn; i++) {
659 struct cpuset *a = csa[i];
660 int apn = a->pn;
661
662 for (j = 0; j < csn; j++) {
663 struct cpuset *b = csa[j];
664 int bpn = b->pn;
665
666 if (apn != bpn && cpusets_overlap(a, b)) {
667 for (k = 0; k < csn; k++) {
668 struct cpuset *c = csa[k];
669
670 if (c->pn == bpn)
671 c->pn = apn;
672 }
673 ndoms--; /* one less element */
674 goto restart;
675 }
676 }
677 }
678
Max Krasnyanskycf417142008-08-11 14:33:53 -0700679 /*
680 * Now we know how many domains to create.
681 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
682 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030683 doms = alloc_sched_domains(ndoms);
Li Zefan700018e2008-11-18 14:02:03 +0800684 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700685 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700686
687 /*
688 * The rest of the code, including the scheduler, can deal with
689 * dattr==NULL case. No need to abort if alloc fails.
690 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900691 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700692
693 for (nslot = 0, i = 0; i < csn; i++) {
694 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800695 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700696 int apn = a->pn;
697
Max Krasnyanskycf417142008-08-11 14:33:53 -0700698 if (apn < 0) {
699 /* Skip completed partitions */
700 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700701 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700702
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030703 dp = doms[nslot];
Max Krasnyanskycf417142008-08-11 14:33:53 -0700704
705 if (nslot == ndoms) {
706 static int warnings = 10;
707 if (warnings) {
708 printk(KERN_WARNING
709 "rebuild_sched_domains confused:"
710 " nslot %d, ndoms %d, csn %d, i %d,"
711 " apn %d\n",
712 nslot, ndoms, csn, i, apn);
713 warnings--;
714 }
715 continue;
716 }
717
Li Zefan6af866a2009-01-07 18:08:45 -0800718 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700719 if (dattr)
720 *(dattr + nslot) = SD_ATTR_INIT;
721 for (j = i; j < csn; j++) {
722 struct cpuset *b = csa[j];
723
724 if (apn == b->pn) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800725 cpumask_or(dp, dp, b->cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700726 if (dattr)
727 update_domain_attr_tree(dattr + nslot, b);
728
729 /* Done with this partition */
730 b->pn = -1;
731 }
732 }
733 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700734 }
735 BUG_ON(nslot != ndoms);
736
Paul Jackson029190c2007-10-18 23:40:20 -0700737done:
Paul Jackson029190c2007-10-18 23:40:20 -0700738 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700739
Li Zefan700018e2008-11-18 14:02:03 +0800740 /*
741 * Fallback to the default domain if kmalloc() failed.
742 * See comments in partition_sched_domains().
743 */
744 if (doms == NULL)
745 ndoms = 1;
746
Max Krasnyanskycf417142008-08-11 14:33:53 -0700747 *domains = doms;
748 *attributes = dattr;
749 return ndoms;
750}
751
752/*
753 * Rebuild scheduler domains.
754 *
755 * Call with neither cgroup_mutex held nor within get_online_cpus().
756 * Takes both cgroup_mutex and get_online_cpus().
757 *
758 * Cannot be directly called from cpuset code handling changes
759 * to the cpuset pseudo-filesystem, because it cannot be called
760 * from code that already holds cgroup_mutex.
761 */
762static void do_rebuild_sched_domains(struct work_struct *unused)
763{
764 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030765 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700766 int ndoms;
767
768 get_online_cpus();
769
770 /* Generate domain masks and attrs */
771 cgroup_lock();
772 ndoms = generate_sched_domains(&doms, &attr);
773 cgroup_unlock();
774
775 /* Have scheduler rebuild the domains */
776 partition_sched_domains(ndoms, doms, attr);
777
778 put_online_cpus();
779}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700780#else /* !CONFIG_SMP */
781static void do_rebuild_sched_domains(struct work_struct *unused)
782{
783}
784
Geert Uytterhoevene1b80902009-12-06 20:41:16 +0100785static int generate_sched_domains(cpumask_var_t **domains,
Paul Menagedb7f47c2009-04-02 16:57:55 -0700786 struct sched_domain_attr **attributes)
787{
788 *domains = NULL;
789 return 1;
790}
791#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700792
793static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
794
795/*
796 * Rebuild scheduler domains, asynchronously via workqueue.
797 *
798 * If the flag 'sched_load_balance' of any cpuset with non-empty
799 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
800 * which has that flag enabled, or if any cpuset with a non-empty
801 * 'cpus' is removed, then call this routine to rebuild the
802 * scheduler's dynamic sched domains.
803 *
804 * The rebuild_sched_domains() and partition_sched_domains()
805 * routines must nest cgroup_lock() inside get_online_cpus(),
806 * but such cpuset changes as these must nest that locking the
807 * other way, holding cgroup_lock() for much of the code.
808 *
809 * So in order to avoid an ABBA deadlock, the cpuset code handling
810 * these user changes delegates the actual sched domain rebuilding
811 * to a separate workqueue thread, which ends up processing the
812 * above do_rebuild_sched_domains() function.
813 */
814static void async_rebuild_sched_domains(void)
815{
Miao Xief90d4112009-01-16 10:24:10 +0800816 queue_work(cpuset_wq, &rebuild_sched_domains_work);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700817}
818
819/*
820 * Accomplishes the same scheduler domain rebuild as the above
821 * async_rebuild_sched_domains(), however it directly calls the
822 * rebuild routine synchronously rather than calling it via an
823 * asynchronous work thread.
824 *
825 * This can only be called from code that is not holding
826 * cgroup_mutex (not nested in a cgroup_lock() call.)
827 */
828void rebuild_sched_domains(void)
829{
830 do_rebuild_sched_domains(NULL);
Paul Jackson029190c2007-10-18 23:40:20 -0700831}
832
Cliff Wickman58f47902008-02-07 00:14:44 -0800833/**
834 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
835 * @tsk: task to test
836 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
837 *
Paul Menage2df167a2008-02-07 00:14:45 -0800838 * Call with cgroup_mutex held. May take callback_mutex during call.
Cliff Wickman58f47902008-02-07 00:14:44 -0800839 * Called for each task in a cgroup by cgroup_scan_tasks().
840 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
841 * words, if its mask is not equal to its cpuset's mask).
Paul Jackson053199e2005-10-30 15:02:30 -0800842 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700843static int cpuset_test_cpumask(struct task_struct *tsk,
844 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800845{
Li Zefan300ed6c2009-01-07 18:08:44 -0800846 return !cpumask_equal(&tsk->cpus_allowed,
Cliff Wickman58f47902008-02-07 00:14:44 -0800847 (cgroup_cs(scan->cg))->cpus_allowed);
848}
Paul Jackson053199e2005-10-30 15:02:30 -0800849
Cliff Wickman58f47902008-02-07 00:14:44 -0800850/**
851 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
852 * @tsk: task to test
853 * @scan: struct cgroup_scanner containing the cgroup of the task
854 *
855 * Called by cgroup_scan_tasks() for each task in a cgroup whose
856 * cpus_allowed mask needs to be changed.
857 *
858 * We don't need to re-check for the cgroup/cpuset membership, since we're
859 * holding cgroup_lock() at this point.
860 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700861static void cpuset_change_cpumask(struct task_struct *tsk,
862 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800863{
Li Zefan300ed6c2009-01-07 18:08:44 -0800864 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
Cliff Wickman58f47902008-02-07 00:14:44 -0800865}
866
867/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700868 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
869 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
Li Zefan4e743392008-09-13 02:33:08 -0700870 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -0700871 *
872 * Called with cgroup_mutex held
873 *
874 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
875 * calling callback functions for each.
876 *
Li Zefan4e743392008-09-13 02:33:08 -0700877 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
878 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -0700879 */
Li Zefan4e743392008-09-13 02:33:08 -0700880static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
Miao Xie0b2f6302008-07-25 01:47:21 -0700881{
882 struct cgroup_scanner scan;
Miao Xie0b2f6302008-07-25 01:47:21 -0700883
884 scan.cg = cs->css.cgroup;
885 scan.test_task = cpuset_test_cpumask;
886 scan.process_task = cpuset_change_cpumask;
Li Zefan4e743392008-09-13 02:33:08 -0700887 scan.heap = heap;
888 cgroup_scan_tasks(&scan);
Miao Xie0b2f6302008-07-25 01:47:21 -0700889}
890
891/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800892 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
893 * @cs: the cpuset to consider
894 * @buf: buffer of cpu numbers written to this cpuset
895 */
Li Zefan645fcc92009-01-07 18:08:43 -0800896static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
897 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Li Zefan4e743392008-09-13 02:33:08 -0700899 struct ptr_heap heap;
Cliff Wickman58f47902008-02-07 00:14:44 -0800900 int retval;
901 int is_load_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Rusty Russell5f054e32012-03-29 15:38:31 +1030903 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700904 if (cs == &top_cpuset)
905 return -EACCES;
906
David Rientjes6f7f02e2007-05-08 00:31:43 -0700907 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800908 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700909 * Since cpulist_parse() fails on an empty mask, we special case
910 * that parsing. The validate_change() call ensures that cpusets
911 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700912 */
Paul Jackson020958b2007-10-18 23:40:21 -0700913 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800914 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700915 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -0800916 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700917 if (retval < 0)
918 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700919
Peter Zijlstra6ad4c182009-11-25 13:31:39 +0100920 if (!cpumask_subset(trialcs->cpus_allowed, cpu_active_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700921 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700922 }
Li Zefan645fcc92009-01-07 18:08:43 -0800923 retval = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700924 if (retval < 0)
925 return retval;
Paul Jackson029190c2007-10-18 23:40:20 -0700926
Paul Menage8707d8b2007-10-18 23:40:22 -0700927 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800928 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700929 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800930
Li Zefan4e743392008-09-13 02:33:08 -0700931 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
932 if (retval)
933 return retval;
934
Li Zefan645fcc92009-01-07 18:08:43 -0800935 is_load_balanced = is_sched_load_balance(trialcs);
Paul Jackson029190c2007-10-18 23:40:20 -0700936
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800937 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -0800938 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800939 mutex_unlock(&callback_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700940
Paul Menage8707d8b2007-10-18 23:40:22 -0700941 /*
942 * Scan tasks in the cpuset, and update the cpumasks of any
Cliff Wickman58f47902008-02-07 00:14:44 -0800943 * that need an update.
Paul Menage8707d8b2007-10-18 23:40:22 -0700944 */
Li Zefan4e743392008-09-13 02:33:08 -0700945 update_tasks_cpumask(cs, &heap);
946
947 heap_free(&heap);
Cliff Wickman58f47902008-02-07 00:14:44 -0800948
Paul Menage8707d8b2007-10-18 23:40:22 -0700949 if (is_load_balanced)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700950 async_rebuild_sched_domains();
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Paul Jackson053199e2005-10-30 15:02:30 -0800954/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800955 * cpuset_migrate_mm
956 *
957 * Migrate memory region from one set of nodes to another.
958 *
959 * Temporarilly set tasks mems_allowed to target nodes of migration,
960 * so that the migration code can allocate pages on these nodes.
961 *
Paul Menage2df167a2008-02-07 00:14:45 -0800962 * Call holding cgroup_mutex, so current's cpuset won't change
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800963 * during this call, as manage_mutex holds off any cpuset_attach()
Paul Jacksone4e364e2006-03-31 02:30:52 -0800964 * calls. Therefore we don't need to take task_lock around the
965 * call to guarantee_online_mems(), as we know no one is changing
Paul Menage2df167a2008-02-07 00:14:45 -0800966 * our task's cpuset.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800967 *
Paul Jacksone4e364e2006-03-31 02:30:52 -0800968 * While the mm_struct we are migrating is typically from some
969 * other task, the task_struct mems_allowed that we are hacking
970 * is for our current task, which must allocate new pages for that
971 * migrating memory region.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800972 */
973
974static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
975 const nodemask_t *to)
976{
977 struct task_struct *tsk = current;
978
Paul Jacksone4e364e2006-03-31 02:30:52 -0800979 tsk->mems_allowed = *to;
Paul Jacksone4e364e2006-03-31 02:30:52 -0800980
981 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
982
Paul Menage8793d852007-10-18 23:39:39 -0700983 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -0800984}
985
Li Zefan3b6766f2009-04-02 16:57:51 -0700986/*
Miao Xie58568d22009-06-16 15:31:49 -0700987 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
988 * @tsk: the task to change
989 * @newmems: new nodes that the task will be set
990 *
991 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
992 * we structure updates as setting all new allowed nodes, then clearing newly
993 * disallowed ones.
Miao Xie58568d22009-06-16 15:31:49 -0700994 */
995static void cpuset_change_task_nodemask(struct task_struct *tsk,
996 nodemask_t *newmems)
997{
David Rientjesb2462722011-12-19 17:11:52 -0800998 bool need_loop;
David Rientjes89e8a242011-11-02 13:38:39 -0700999
Miao Xiec0ff7452010-05-24 14:32:08 -07001000 /*
1001 * Allow tasks that have access to memory reserves because they have
1002 * been OOM killed to get memory anywhere.
1003 */
1004 if (unlikely(test_thread_flag(TIF_MEMDIE)))
1005 return;
1006 if (current->flags & PF_EXITING) /* Let dying task have memory */
1007 return;
1008
1009 task_lock(tsk);
David Rientjesb2462722011-12-19 17:11:52 -08001010 /*
1011 * Determine if a loop is necessary if another thread is doing
1012 * get_mems_allowed(). If at least one node remains unchanged and
1013 * tsk does not have a mempolicy, then an empty nodemask will not be
1014 * possible when mems_allowed is larger than a word.
1015 */
1016 need_loop = task_has_mempolicy(tsk) ||
1017 !nodes_intersects(*newmems, tsk->mems_allowed);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001018
1019 if (need_loop)
1020 write_seqcount_begin(&tsk->mems_allowed_seq);
1021
Miao Xie58568d22009-06-16 15:31:49 -07001022 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Miao Xiec0ff7452010-05-24 14:32:08 -07001023 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
1024
Miao Xiec0ff7452010-05-24 14:32:08 -07001025 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
Miao Xie58568d22009-06-16 15:31:49 -07001026 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001027
1028 if (need_loop)
1029 write_seqcount_end(&tsk->mems_allowed_seq);
1030
Miao Xiec0ff7452010-05-24 14:32:08 -07001031 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001032}
1033
1034/*
1035 * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy
1036 * of it to cpuset's new mems_allowed, and migrate pages to new nodes if
1037 * memory_migrate flag is set. Called with cgroup_mutex held.
Li Zefan3b6766f2009-04-02 16:57:51 -07001038 */
1039static void cpuset_change_nodemask(struct task_struct *p,
1040 struct cgroup_scanner *scan)
1041{
1042 struct mm_struct *mm;
1043 struct cpuset *cs;
1044 int migrate;
1045 const nodemask_t *oldmem = scan->data;
Li Zefanee24d372011-03-23 16:42:47 -07001046 static nodemask_t newmems; /* protected by cgroup_mutex */
Miao Xie58568d22009-06-16 15:31:49 -07001047
1048 cs = cgroup_cs(scan->cg);
Li Zefanee24d372011-03-23 16:42:47 -07001049 guarantee_online_mems(cs, &newmems);
Miao Xie58568d22009-06-16 15:31:49 -07001050
Li Zefanee24d372011-03-23 16:42:47 -07001051 cpuset_change_task_nodemask(p, &newmems);
Miao Xie53feb292010-03-23 13:35:35 -07001052
Li Zefan3b6766f2009-04-02 16:57:51 -07001053 mm = get_task_mm(p);
1054 if (!mm)
1055 return;
1056
Li Zefan3b6766f2009-04-02 16:57:51 -07001057 migrate = is_memory_migrate(cs);
1058
1059 mpol_rebind_mm(mm, &cs->mems_allowed);
1060 if (migrate)
1061 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1062 mmput(mm);
1063}
1064
Paul Menage8793d852007-10-18 23:39:39 -07001065static void *cpuset_being_rebound;
1066
Miao Xie0b2f6302008-07-25 01:47:21 -07001067/**
1068 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1069 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1070 * @oldmem: old mems_allowed of cpuset cs
Li Zefan010cfac2009-04-02 16:57:52 -07001071 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -07001072 *
1073 * Called with cgroup_mutex held
Li Zefan010cfac2009-04-02 16:57:52 -07001074 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1075 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -07001076 */
Li Zefan010cfac2009-04-02 16:57:52 -07001077static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1078 struct ptr_heap *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Li Zefan3b6766f2009-04-02 16:57:51 -07001080 struct cgroup_scanner scan;
Paul Jackson59dac162006-01-08 01:01:52 -08001081
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001082 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001083
Li Zefan3b6766f2009-04-02 16:57:51 -07001084 scan.cg = cs->css.cgroup;
1085 scan.test_task = NULL;
1086 scan.process_task = cpuset_change_nodemask;
Li Zefan010cfac2009-04-02 16:57:52 -07001087 scan.heap = heap;
Li Zefan3b6766f2009-04-02 16:57:51 -07001088 scan.data = (nodemask_t *)oldmem;
Paul Jackson42253992006-01-08 01:01:59 -08001089
1090 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001091 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1092 * take while holding tasklist_lock. Forks can happen - the
1093 * mpol_dup() cpuset_being_rebound check will catch such forks,
1094 * and rebind their vma mempolicies too. Because we still hold
1095 * the global cgroup_mutex, we know that no other rebind effort
1096 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001097 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001098 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001099 */
Li Zefan010cfac2009-04-02 16:57:52 -07001100 cgroup_scan_tasks(&scan);
Paul Jackson42253992006-01-08 01:01:59 -08001101
Paul Menage2df167a2008-02-07 00:14:45 -08001102 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001103 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Miao Xie0b2f6302008-07-25 01:47:21 -07001106/*
1107 * Handle user request to change the 'mems' memory placement
1108 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001109 * cpusets mems_allowed, and for each task in the cpuset,
1110 * update mems_allowed and rebind task's mempolicy and any vma
1111 * mempolicies and if the cpuset is marked 'memory_migrate',
1112 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001113 *
1114 * Call with cgroup_mutex held. May take callback_mutex during call.
1115 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1116 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1117 * their mempolicies to the cpusets new mems_allowed.
1118 */
Li Zefan645fcc92009-01-07 18:08:43 -08001119static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1120 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001121{
Miao Xie53feb292010-03-23 13:35:35 -07001122 NODEMASK_ALLOC(nodemask_t, oldmem, GFP_KERNEL);
Miao Xie0b2f6302008-07-25 01:47:21 -07001123 int retval;
Li Zefan010cfac2009-04-02 16:57:52 -07001124 struct ptr_heap heap;
Miao Xie0b2f6302008-07-25 01:47:21 -07001125
Miao Xie53feb292010-03-23 13:35:35 -07001126 if (!oldmem)
1127 return -ENOMEM;
1128
Miao Xie0b2f6302008-07-25 01:47:21 -07001129 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001130 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001131 * it's read-only
1132 */
Miao Xie53feb292010-03-23 13:35:35 -07001133 if (cs == &top_cpuset) {
1134 retval = -EACCES;
1135 goto done;
1136 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001137
Miao Xie0b2f6302008-07-25 01:47:21 -07001138 /*
1139 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1140 * Since nodelist_parse() fails on an empty mask, we special case
1141 * that parsing. The validate_change() call ensures that cpusets
1142 * with tasks have memory.
1143 */
1144 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001145 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001146 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001147 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001148 if (retval < 0)
1149 goto done;
1150
Li Zefan645fcc92009-01-07 18:08:43 -08001151 if (!nodes_subset(trialcs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001152 node_states[N_MEMORY])) {
Miao Xie53feb292010-03-23 13:35:35 -07001153 retval = -EINVAL;
1154 goto done;
1155 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001156 }
Miao Xie53feb292010-03-23 13:35:35 -07001157 *oldmem = cs->mems_allowed;
1158 if (nodes_equal(*oldmem, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001159 retval = 0; /* Too easy - nothing to do */
1160 goto done;
1161 }
Li Zefan645fcc92009-01-07 18:08:43 -08001162 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001163 if (retval < 0)
1164 goto done;
1165
Li Zefan010cfac2009-04-02 16:57:52 -07001166 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1167 if (retval < 0)
1168 goto done;
1169
Miao Xie0b2f6302008-07-25 01:47:21 -07001170 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001171 cs->mems_allowed = trialcs->mems_allowed;
Miao Xie0b2f6302008-07-25 01:47:21 -07001172 mutex_unlock(&callback_mutex);
1173
Miao Xie53feb292010-03-23 13:35:35 -07001174 update_tasks_nodemask(cs, oldmem, &heap);
Li Zefan010cfac2009-04-02 16:57:52 -07001175
1176 heap_free(&heap);
Miao Xie0b2f6302008-07-25 01:47:21 -07001177done:
Miao Xie53feb292010-03-23 13:35:35 -07001178 NODEMASK_FREE(oldmem);
Miao Xie0b2f6302008-07-25 01:47:21 -07001179 return retval;
1180}
1181
Paul Menage8793d852007-10-18 23:39:39 -07001182int current_cpuset_is_being_rebound(void)
1183{
1184 return task_cs(current) == cpuset_being_rebound;
1185}
1186
Paul Menage5be7a472008-05-06 20:42:41 -07001187static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001188{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001189#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001190 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001191 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001192#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001193
1194 if (val != cs->relax_domain_level) {
1195 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001196 if (!cpumask_empty(cs->cpus_allowed) &&
1197 is_sched_load_balance(cs))
Max Krasnyanskycf417142008-08-11 14:33:53 -07001198 async_rebuild_sched_domains();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001199 }
1200
1201 return 0;
1202}
1203
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001204/*
Miao Xie950592f2009-06-16 15:31:47 -07001205 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1206 * @tsk: task to be updated
1207 * @scan: struct cgroup_scanner containing the cgroup of the task
1208 *
1209 * Called by cgroup_scan_tasks() for each task in a cgroup.
1210 *
1211 * We don't need to re-check for the cgroup/cpuset membership, since we're
1212 * holding cgroup_lock() at this point.
1213 */
1214static void cpuset_change_flag(struct task_struct *tsk,
1215 struct cgroup_scanner *scan)
1216{
1217 cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk);
1218}
1219
1220/*
1221 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1222 * @cs: the cpuset in which each task's spread flags needs to be changed
1223 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
1224 *
1225 * Called with cgroup_mutex held
1226 *
1227 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1228 * calling callback functions for each.
1229 *
1230 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1231 * if @heap != NULL.
1232 */
1233static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap)
1234{
1235 struct cgroup_scanner scan;
1236
1237 scan.cg = cs->css.cgroup;
1238 scan.test_task = NULL;
1239 scan.process_task = cpuset_change_flag;
1240 scan.heap = heap;
1241 cgroup_scan_tasks(&scan);
1242}
1243
1244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001246 * bit: the bit to update (see cpuset_flagbits_t)
1247 * cs: the cpuset to update
1248 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001249 *
Paul Menage2df167a2008-02-07 00:14:45 -08001250 * Call with cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
1252
Paul Menage700fe1a2008-04-29 01:00:00 -07001253static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1254 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Li Zefan645fcc92009-01-07 18:08:43 -08001256 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001257 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001258 int spread_flag_changed;
1259 struct ptr_heap heap;
1260 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Li Zefan645fcc92009-01-07 18:08:43 -08001262 trialcs = alloc_trial_cpuset(cs);
1263 if (!trialcs)
1264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Li Zefan645fcc92009-01-07 18:08:43 -08001266 if (turning_on)
1267 set_bit(bit, &trialcs->flags);
1268 else
1269 clear_bit(bit, &trialcs->flags);
1270
1271 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001272 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001273 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001274
Miao Xie950592f2009-06-16 15:31:47 -07001275 err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1276 if (err < 0)
1277 goto out;
1278
Paul Jackson029190c2007-10-18 23:40:20 -07001279 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001280 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001281
Miao Xie950592f2009-06-16 15:31:47 -07001282 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1283 || (is_spread_page(cs) != is_spread_page(trialcs)));
1284
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001285 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001286 cs->flags = trialcs->flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001287 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001288
Li Zefan300ed6c2009-01-07 18:08:44 -08001289 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Max Krasnyanskycf417142008-08-11 14:33:53 -07001290 async_rebuild_sched_domains();
Paul Jackson029190c2007-10-18 23:40:20 -07001291
Miao Xie950592f2009-06-16 15:31:47 -07001292 if (spread_flag_changed)
1293 update_tasks_flags(cs, &heap);
1294 heap_free(&heap);
Li Zefan645fcc92009-01-07 18:08:43 -08001295out:
1296 free_trial_cpuset(trialcs);
1297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Paul Jackson053199e2005-10-30 15:02:30 -08001300/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001301 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001302 *
1303 * These routines manage a digitally filtered, constant time based,
1304 * event frequency meter. There are four routines:
1305 * fmeter_init() - initialize a frequency meter.
1306 * fmeter_markevent() - called each time the event happens.
1307 * fmeter_getrate() - returns the recent rate of such events.
1308 * fmeter_update() - internal routine used to update fmeter.
1309 *
1310 * A common data structure is passed to each of these routines,
1311 * which is used to keep track of the state required to manage the
1312 * frequency meter and its digital filter.
1313 *
1314 * The filter works on the number of events marked per unit time.
1315 * The filter is single-pole low-pass recursive (IIR). The time unit
1316 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1317 * simulate 3 decimal digits of precision (multiplied by 1000).
1318 *
1319 * With an FM_COEF of 933, and a time base of 1 second, the filter
1320 * has a half-life of 10 seconds, meaning that if the events quit
1321 * happening, then the rate returned from the fmeter_getrate()
1322 * will be cut in half each 10 seconds, until it converges to zero.
1323 *
1324 * It is not worth doing a real infinitely recursive filter. If more
1325 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1326 * just compute FM_MAXTICKS ticks worth, by which point the level
1327 * will be stable.
1328 *
1329 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1330 * arithmetic overflow in the fmeter_update() routine.
1331 *
1332 * Given the simple 32 bit integer arithmetic used, this meter works
1333 * best for reporting rates between one per millisecond (msec) and
1334 * one per 32 (approx) seconds. At constant rates faster than one
1335 * per msec it maxes out at values just under 1,000,000. At constant
1336 * rates between one per msec, and one per second it will stabilize
1337 * to a value N*1000, where N is the rate of events per second.
1338 * At constant rates between one per second and one per 32 seconds,
1339 * it will be choppy, moving up on the seconds that have an event,
1340 * and then decaying until the next event. At rates slower than
1341 * about one in 32 seconds, it decays all the way back to zero between
1342 * each event.
1343 */
1344
1345#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1346#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1347#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1348#define FM_SCALE 1000 /* faux fixed point scale */
1349
1350/* Initialize a frequency meter */
1351static void fmeter_init(struct fmeter *fmp)
1352{
1353 fmp->cnt = 0;
1354 fmp->val = 0;
1355 fmp->time = 0;
1356 spin_lock_init(&fmp->lock);
1357}
1358
1359/* Internal meter update - process cnt events and update value */
1360static void fmeter_update(struct fmeter *fmp)
1361{
1362 time_t now = get_seconds();
1363 time_t ticks = now - fmp->time;
1364
1365 if (ticks == 0)
1366 return;
1367
1368 ticks = min(FM_MAXTICKS, ticks);
1369 while (ticks-- > 0)
1370 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1371 fmp->time = now;
1372
1373 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1374 fmp->cnt = 0;
1375}
1376
1377/* Process any previous ticks, then bump cnt by one (times scale). */
1378static void fmeter_markevent(struct fmeter *fmp)
1379{
1380 spin_lock(&fmp->lock);
1381 fmeter_update(fmp);
1382 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1383 spin_unlock(&fmp->lock);
1384}
1385
1386/* Process any previous ticks, then return current value. */
1387static int fmeter_getrate(struct fmeter *fmp)
1388{
1389 int val;
1390
1391 spin_lock(&fmp->lock);
1392 fmeter_update(fmp);
1393 val = fmp->val;
1394 spin_unlock(&fmp->lock);
1395 return val;
1396}
1397
Ben Blumf780bdb2011-05-26 16:25:19 -07001398/*
1399 * Protected by cgroup_lock. The nodemasks must be stored globally because
Tejun Heo94196f52011-12-12 18:12:22 -08001400 * dynamically allocating them is not allowed in can_attach, and they must
1401 * persist until attach.
Ben Blumf780bdb2011-05-26 16:25:19 -07001402 */
1403static cpumask_var_t cpus_attach;
1404static nodemask_t cpuset_attach_nodemask_from;
1405static nodemask_t cpuset_attach_nodemask_to;
1406
David Rientjes6d7b2f52009-04-02 16:57:57 -07001407/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
Li Zefan761b3ef2012-01-31 13:47:36 +08001408static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07001409{
Tejun Heo2f7ee562011-12-12 18:12:21 -08001410 struct cpuset *cs = cgroup_cs(cgrp);
Tejun Heobb9d97b2011-12-12 18:12:21 -08001411 struct task_struct *task;
1412 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07001413
Ben Blumbe367d02009-09-23 15:56:31 -07001414 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1415 return -ENOSPC;
1416
Tejun Heobb9d97b2011-12-12 18:12:21 -08001417 cgroup_taskset_for_each(task, cgrp, tset) {
1418 /*
1419 * Kthreads bound to specific cpus cannot be moved to a new
1420 * cpuset; we cannot change their cpu affinity and
1421 * isolating such threads by their set of allowed nodes is
1422 * unnecessary. Thus, cpusets are not applicable for such
1423 * threads. This prevents checking for success of
1424 * set_cpus_allowed_ptr() on all attached tasks before
1425 * cpus_allowed may be changed.
1426 */
1427 if (task->flags & PF_THREAD_BOUND)
1428 return -EINVAL;
1429 if ((ret = security_task_setscheduler(task)))
1430 return ret;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Tejun Heo94196f52011-12-12 18:12:22 -08001433 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07001434 if (cs == &top_cpuset)
1435 cpumask_copy(cpus_attach, cpu_possible_mask);
1436 else
1437 guarantee_online_cpus(cs, cpus_attach);
1438
1439 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08001440
1441 return 0;
Ben Blumf780bdb2011-05-26 16:25:19 -07001442}
1443
Li Zefan761b3ef2012-01-31 13:47:36 +08001444static void cpuset_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Paul Menage8793d852007-10-18 23:39:39 -07001445{
Paul Menage8793d852007-10-18 23:39:39 -07001446 struct mm_struct *mm;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001447 struct task_struct *task;
1448 struct task_struct *leader = cgroup_taskset_first(tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001449 struct cgroup *oldcgrp = cgroup_taskset_cur_cgroup(tset);
1450 struct cpuset *cs = cgroup_cs(cgrp);
1451 struct cpuset *oldcs = cgroup_cs(oldcgrp);
David Quigley22fb52d2006-06-23 02:04:00 -07001452
Tejun Heobb9d97b2011-12-12 18:12:21 -08001453 cgroup_taskset_for_each(task, cgrp, tset) {
1454 /*
1455 * can_attach beforehand should guarantee that this doesn't
1456 * fail. TODO: have a better way to handle failure here
1457 */
1458 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1459
1460 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1461 cpuset_update_task_spread_flag(cs, task);
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Ben Blumf780bdb2011-05-26 16:25:19 -07001464 /*
1465 * Change mm, possibly for multiple threads in a threadgroup. This is
1466 * expensive and may sleep.
1467 */
1468 cpuset_attach_nodemask_from = oldcs->mems_allowed;
1469 cpuset_attach_nodemask_to = cs->mems_allowed;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001470 mm = get_task_mm(leader);
Paul Jackson42253992006-01-08 01:01:59 -08001471 if (mm) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001472 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
Paul Jackson2741a552006-03-31 02:30:51 -08001473 if (is_memory_migrate(cs))
Ben Blumf780bdb2011-05-26 16:25:19 -07001474 cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from,
1475 &cpuset_attach_nodemask_to);
Paul Jackson42253992006-01-08 01:01:59 -08001476 mmput(mm);
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/* The various types of files and directories in a cpuset file system */
1481
1482typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001483 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 FILE_CPULIST,
1485 FILE_MEMLIST,
1486 FILE_CPU_EXCLUSIVE,
1487 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001488 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001489 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001490 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001491 FILE_MEMORY_PRESSURE_ENABLED,
1492 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001493 FILE_SPREAD_PAGE,
1494 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495} cpuset_filetype_t;
1496
Paul Menage700fe1a2008-04-29 01:00:00 -07001497static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1498{
1499 int retval = 0;
1500 struct cpuset *cs = cgroup_cs(cgrp);
1501 cpuset_filetype_t type = cft->private;
1502
Paul Menagee3712392008-07-25 01:47:02 -07001503 if (!cgroup_lock_live_group(cgrp))
Paul Menage700fe1a2008-04-29 01:00:00 -07001504 return -ENODEV;
Paul Menage700fe1a2008-04-29 01:00:00 -07001505
1506 switch (type) {
1507 case FILE_CPU_EXCLUSIVE:
1508 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1509 break;
1510 case FILE_MEM_EXCLUSIVE:
1511 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1512 break;
Paul Menage78608362008-04-29 01:00:26 -07001513 case FILE_MEM_HARDWALL:
1514 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1515 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001516 case FILE_SCHED_LOAD_BALANCE:
1517 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1518 break;
1519 case FILE_MEMORY_MIGRATE:
1520 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1521 break;
1522 case FILE_MEMORY_PRESSURE_ENABLED:
1523 cpuset_memory_pressure_enabled = !!val;
1524 break;
1525 case FILE_MEMORY_PRESSURE:
1526 retval = -EACCES;
1527 break;
1528 case FILE_SPREAD_PAGE:
1529 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001530 break;
1531 case FILE_SPREAD_SLAB:
1532 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001533 break;
1534 default:
1535 retval = -EINVAL;
1536 break;
1537 }
1538 cgroup_unlock();
1539 return retval;
1540}
1541
Paul Menage5be7a472008-05-06 20:42:41 -07001542static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1543{
1544 int retval = 0;
1545 struct cpuset *cs = cgroup_cs(cgrp);
1546 cpuset_filetype_t type = cft->private;
1547
Paul Menagee3712392008-07-25 01:47:02 -07001548 if (!cgroup_lock_live_group(cgrp))
Paul Menage5be7a472008-05-06 20:42:41 -07001549 return -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001550
Paul Menage5be7a472008-05-06 20:42:41 -07001551 switch (type) {
1552 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1553 retval = update_relax_domain_level(cs, val);
1554 break;
1555 default:
1556 retval = -EINVAL;
1557 break;
1558 }
1559 cgroup_unlock();
1560 return retval;
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563/*
Paul Menagee3712392008-07-25 01:47:02 -07001564 * Common handling for a write to a "cpus" or "mems" file.
1565 */
1566static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1567 const char *buf)
1568{
1569 int retval = 0;
Li Zefan645fcc92009-01-07 18:08:43 -08001570 struct cpuset *cs = cgroup_cs(cgrp);
1571 struct cpuset *trialcs;
Paul Menagee3712392008-07-25 01:47:02 -07001572
1573 if (!cgroup_lock_live_group(cgrp))
1574 return -ENODEV;
1575
Li Zefan645fcc92009-01-07 18:08:43 -08001576 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001577 if (!trialcs) {
1578 retval = -ENOMEM;
1579 goto out;
1580 }
Li Zefan645fcc92009-01-07 18:08:43 -08001581
Paul Menagee3712392008-07-25 01:47:02 -07001582 switch (cft->private) {
1583 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001584 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001585 break;
1586 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001587 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001588 break;
1589 default:
1590 retval = -EINVAL;
1591 break;
1592 }
Li Zefan645fcc92009-01-07 18:08:43 -08001593
1594 free_trial_cpuset(trialcs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001595out:
Paul Menagee3712392008-07-25 01:47:02 -07001596 cgroup_unlock();
1597 return retval;
1598}
1599
1600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * These ascii lists should be read in a single call, by using a user
1602 * buffer large enough to hold the entire map. If read in smaller
1603 * chunks, there is no guarantee of atomicity. Since the display format
1604 * used, list of ranges of sequential numbers, is variable length,
1605 * and since these maps can change value dynamically, one could read
1606 * gibberish by doing partial reads while a list was changing.
1607 * A single large read to a buffer that crosses a page boundary is
1608 * ok, because the result being copied to user land is not recomputed
1609 * across a page fault.
1610 */
1611
Li Zefan9303e0c2011-03-23 16:42:45 -07001612static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Li Zefan9303e0c2011-03-23 16:42:45 -07001614 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001616 mutex_lock(&callback_mutex);
Li Zefan9303e0c2011-03-23 16:42:45 -07001617 count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001618 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Li Zefan9303e0c2011-03-23 16:42:45 -07001620 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Li Zefan9303e0c2011-03-23 16:42:45 -07001623static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Li Zefan9303e0c2011-03-23 16:42:45 -07001625 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001627 mutex_lock(&callback_mutex);
Li Zefan9303e0c2011-03-23 16:42:45 -07001628 count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001629 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Li Zefan9303e0c2011-03-23 16:42:45 -07001631 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Paul Menage8793d852007-10-18 23:39:39 -07001634static ssize_t cpuset_common_file_read(struct cgroup *cont,
1635 struct cftype *cft,
1636 struct file *file,
1637 char __user *buf,
1638 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Paul Menage8793d852007-10-18 23:39:39 -07001640 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 cpuset_filetype_t type = cft->private;
1642 char *page;
1643 ssize_t retval = 0;
1644 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Mel Gormane12ba742007-10-16 01:25:52 -07001646 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return -ENOMEM;
1648
1649 s = page;
1650
1651 switch (type) {
1652 case FILE_CPULIST:
1653 s += cpuset_sprintf_cpulist(s, cs);
1654 break;
1655 case FILE_MEMLIST:
1656 s += cpuset_sprintf_memlist(s, cs);
1657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 default:
1659 retval = -EINVAL;
1660 goto out;
1661 }
1662 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Al Viroeacaa1f2005-09-30 03:26:43 +01001664 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665out:
1666 free_page((unsigned long)page);
1667 return retval;
1668}
1669
Paul Menage700fe1a2008-04-29 01:00:00 -07001670static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1671{
1672 struct cpuset *cs = cgroup_cs(cont);
1673 cpuset_filetype_t type = cft->private;
1674 switch (type) {
1675 case FILE_CPU_EXCLUSIVE:
1676 return is_cpu_exclusive(cs);
1677 case FILE_MEM_EXCLUSIVE:
1678 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001679 case FILE_MEM_HARDWALL:
1680 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001681 case FILE_SCHED_LOAD_BALANCE:
1682 return is_sched_load_balance(cs);
1683 case FILE_MEMORY_MIGRATE:
1684 return is_memory_migrate(cs);
1685 case FILE_MEMORY_PRESSURE_ENABLED:
1686 return cpuset_memory_pressure_enabled;
1687 case FILE_MEMORY_PRESSURE:
1688 return fmeter_getrate(&cs->fmeter);
1689 case FILE_SPREAD_PAGE:
1690 return is_spread_page(cs);
1691 case FILE_SPREAD_SLAB:
1692 return is_spread_slab(cs);
1693 default:
1694 BUG();
1695 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001696
1697 /* Unreachable but makes gcc happy */
1698 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Paul Menage5be7a472008-05-06 20:42:41 -07001701static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1702{
1703 struct cpuset *cs = cgroup_cs(cont);
1704 cpuset_filetype_t type = cft->private;
1705 switch (type) {
1706 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1707 return cs->relax_domain_level;
1708 default:
1709 BUG();
1710 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001711
1712 /* Unrechable but makes gcc happy */
1713 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717/*
1718 * for the common functions, 'private' gives the type of file
1719 */
1720
Paul Menageaddf2c72008-04-29 01:00:26 -07001721static struct cftype files[] = {
1722 {
1723 .name = "cpus",
1724 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001725 .write_string = cpuset_write_resmask,
1726 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001727 .private = FILE_CPULIST,
1728 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Paul Menageaddf2c72008-04-29 01:00:26 -07001730 {
1731 .name = "mems",
1732 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001733 .write_string = cpuset_write_resmask,
1734 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001735 .private = FILE_MEMLIST,
1736 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Paul Menageaddf2c72008-04-29 01:00:26 -07001738 {
1739 .name = "cpu_exclusive",
1740 .read_u64 = cpuset_read_u64,
1741 .write_u64 = cpuset_write_u64,
1742 .private = FILE_CPU_EXCLUSIVE,
1743 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Paul Menageaddf2c72008-04-29 01:00:26 -07001745 {
1746 .name = "mem_exclusive",
1747 .read_u64 = cpuset_read_u64,
1748 .write_u64 = cpuset_write_u64,
1749 .private = FILE_MEM_EXCLUSIVE,
1750 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Paul Menageaddf2c72008-04-29 01:00:26 -07001752 {
Paul Menage78608362008-04-29 01:00:26 -07001753 .name = "mem_hardwall",
1754 .read_u64 = cpuset_read_u64,
1755 .write_u64 = cpuset_write_u64,
1756 .private = FILE_MEM_HARDWALL,
1757 },
1758
1759 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001760 .name = "sched_load_balance",
1761 .read_u64 = cpuset_read_u64,
1762 .write_u64 = cpuset_write_u64,
1763 .private = FILE_SCHED_LOAD_BALANCE,
1764 },
Paul Jackson029190c2007-10-18 23:40:20 -07001765
Paul Menageaddf2c72008-04-29 01:00:26 -07001766 {
1767 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001768 .read_s64 = cpuset_read_s64,
1769 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001770 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1771 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001772
Paul Menageaddf2c72008-04-29 01:00:26 -07001773 {
1774 .name = "memory_migrate",
1775 .read_u64 = cpuset_read_u64,
1776 .write_u64 = cpuset_write_u64,
1777 .private = FILE_MEMORY_MIGRATE,
1778 },
1779
1780 {
1781 .name = "memory_pressure",
1782 .read_u64 = cpuset_read_u64,
1783 .write_u64 = cpuset_write_u64,
1784 .private = FILE_MEMORY_PRESSURE,
Li Zefan099fca32009-04-02 16:57:29 -07001785 .mode = S_IRUGO,
Paul Menageaddf2c72008-04-29 01:00:26 -07001786 },
1787
1788 {
1789 .name = "memory_spread_page",
1790 .read_u64 = cpuset_read_u64,
1791 .write_u64 = cpuset_write_u64,
1792 .private = FILE_SPREAD_PAGE,
1793 },
1794
1795 {
1796 .name = "memory_spread_slab",
1797 .read_u64 = cpuset_read_u64,
1798 .write_u64 = cpuset_write_u64,
1799 .private = FILE_SPREAD_SLAB,
1800 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07001801
1802 {
1803 .name = "memory_pressure_enabled",
1804 .flags = CFTYPE_ONLY_ON_ROOT,
1805 .read_u64 = cpuset_read_u64,
1806 .write_u64 = cpuset_write_u64,
1807 .private = FILE_MEMORY_PRESSURE_ENABLED,
1808 },
1809
1810 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001811};
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/*
Tejun Heo92fb9742012-11-19 08:13:38 -08001814 * cpuset_css_alloc - allocate a cpuset css
Paul Menage2df167a2008-02-07 00:14:45 -08001815 * cont: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
1817
Tejun Heo92fb9742012-11-19 08:13:38 -08001818static struct cgroup_subsys_state *cpuset_css_alloc(struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Tejun Heoc8f699b2013-01-07 08:51:07 -08001820 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Tejun Heoc8f699b2013-01-07 08:51:07 -08001822 if (!cont->parent)
Paul Menage8793d852007-10-18 23:39:39 -07001823 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001824
Tejun Heoc8f699b2013-01-07 08:51:07 -08001825 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001827 return ERR_PTR(-ENOMEM);
Li Zefan300ed6c2009-01-07 18:08:44 -08001828 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1829 kfree(cs);
1830 return ERR_PTR(-ENOMEM);
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Paul Jackson029190c2007-10-18 23:40:20 -07001833 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001834 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001835 nodes_clear(cs->mems_allowed);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001836 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001837 cs->relax_domain_level = -1;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001838 cs->parent = cgroup_cs(cont->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Tejun Heoc8f699b2013-01-07 08:51:07 -08001840 return &cs->css;
1841}
1842
1843static int cpuset_css_online(struct cgroup *cgrp)
1844{
1845 struct cpuset *cs = cgroup_cs(cgrp);
1846 struct cpuset *parent = cs->parent;
Tejun Heoae8086c2013-01-07 08:51:07 -08001847 struct cpuset *tmp_cs;
1848 struct cgroup *pos_cg;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001849
1850 if (!parent)
1851 return 0;
1852
Tejun Heoefeb77b2013-01-07 08:51:07 -08001853 set_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001854 if (is_spread_page(parent))
1855 set_bit(CS_SPREAD_PAGE, &cs->flags);
1856 if (is_spread_slab(parent))
1857 set_bit(CS_SPREAD_SLAB, &cs->flags);
1858
Paul Jackson202f72d2006-01-08 01:01:57 -08001859 number_of_cpusets++;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001860
Tejun Heoc8f699b2013-01-07 08:51:07 -08001861 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags))
1862 return 0;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001863
1864 /*
1865 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
1866 * set. This flag handling is implemented in cgroup core for
1867 * histrical reasons - the flag may be specified during mount.
1868 *
1869 * Currently, if any sibling cpusets have exclusive cpus or mem, we
1870 * refuse to clone the configuration - thereby refusing the task to
1871 * be entered, and as a result refusing the sys_unshare() or
1872 * clone() which initiated it. If this becomes a problem for some
1873 * users who wish to allow that scenario, then this could be
1874 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
1875 * (and likewise for mems) to the new cgroup.
1876 */
Tejun Heoae8086c2013-01-07 08:51:07 -08001877 rcu_read_lock();
1878 cpuset_for_each_child(tmp_cs, pos_cg, parent) {
1879 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
1880 rcu_read_unlock();
Tejun Heoc8f699b2013-01-07 08:51:07 -08001881 return 0;
Tejun Heoae8086c2013-01-07 08:51:07 -08001882 }
Tejun Heo033fa1c2012-11-19 08:13:39 -08001883 }
Tejun Heoae8086c2013-01-07 08:51:07 -08001884 rcu_read_unlock();
Tejun Heo033fa1c2012-11-19 08:13:39 -08001885
1886 mutex_lock(&callback_mutex);
1887 cs->mems_allowed = parent->mems_allowed;
1888 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
1889 mutex_unlock(&callback_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001890
1891 return 0;
1892}
1893
1894static void cpuset_css_offline(struct cgroup *cgrp)
1895{
1896 struct cpuset *cs = cgroup_cs(cgrp);
1897
1898 /* css_offline is called w/o cgroup_mutex, grab it */
1899 cgroup_lock();
1900
1901 if (is_sched_load_balance(cs))
1902 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
1903
1904 number_of_cpusets--;
Tejun Heoefeb77b2013-01-07 08:51:07 -08001905 clear_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001906
1907 cgroup_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Paul Jackson029190c2007-10-18 23:40:20 -07001910/*
Paul Jackson029190c2007-10-18 23:40:20 -07001911 * If the cpuset being removed has its flag 'sched_load_balance'
1912 * enabled, then simulate turning sched_load_balance off, which
Max Krasnyanskycf417142008-08-11 14:33:53 -07001913 * will call async_rebuild_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -07001914 */
1915
Tejun Heo92fb9742012-11-19 08:13:38 -08001916static void cpuset_css_free(struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Paul Menage8793d852007-10-18 23:39:39 -07001918 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Li Zefan300ed6c2009-01-07 18:08:44 -08001920 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001921 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Paul Menage8793d852007-10-18 23:39:39 -07001924struct cgroup_subsys cpuset_subsys = {
1925 .name = "cpuset",
Tejun Heo92fb9742012-11-19 08:13:38 -08001926 .css_alloc = cpuset_css_alloc,
Tejun Heoc8f699b2013-01-07 08:51:07 -08001927 .css_online = cpuset_css_online,
1928 .css_offline = cpuset_css_offline,
Tejun Heo92fb9742012-11-19 08:13:38 -08001929 .css_free = cpuset_css_free,
Paul Menage8793d852007-10-18 23:39:39 -07001930 .can_attach = cpuset_can_attach,
1931 .attach = cpuset_attach,
Paul Menage8793d852007-10-18 23:39:39 -07001932 .subsys_id = cpuset_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001933 .base_cftypes = files,
Paul Menage8793d852007-10-18 23:39:39 -07001934 .early_init = 1,
1935};
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/**
1938 * cpuset_init - initialize cpusets at system boot
1939 *
1940 * Description: Initialize top_cpuset and the cpuset internal file system,
1941 **/
1942
1943int __init cpuset_init(void)
1944{
Paul Menage8793d852007-10-18 23:39:39 -07001945 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Miao Xie58568d22009-06-16 15:31:49 -07001947 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
1948 BUG();
1949
Li Zefan300ed6c2009-01-07 18:08:44 -08001950 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001951 nodes_setall(top_cpuset.mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001953 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07001954 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001955 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 err = register_filesystem(&cpuset_fs_type);
1958 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07001959 return err;
1960
Li Zefan2341d1b2009-01-07 18:08:42 -08001961 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1962 BUG();
1963
Paul Jackson202f72d2006-01-08 01:01:57 -08001964 number_of_cpusets = 1;
Paul Menage8793d852007-10-18 23:39:39 -07001965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Cliff Wickman956db3c2008-02-07 00:14:43 -08001968/**
1969 * cpuset_do_move_task - move a given task to another cpuset
1970 * @tsk: pointer to task_struct the task to move
1971 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1972 *
1973 * Called by cgroup_scan_tasks() for each task in a cgroup.
1974 * Return nonzero to stop the walk through the tasks.
1975 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -07001976static void cpuset_do_move_task(struct task_struct *tsk,
1977 struct cgroup_scanner *scan)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001978{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001979 struct cgroup *new_cgroup = scan->data;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001980
Li Zefan7f81b1a2009-04-02 16:57:53 -07001981 cgroup_attach_task(new_cgroup, tsk);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001982}
1983
1984/**
1985 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
1986 * @from: cpuset in which the tasks currently reside
1987 * @to: cpuset to which the tasks will be moved
1988 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001989 * Called with cgroup_mutex held
1990 * callback_mutex must not be held, as cpuset_attach() will take it.
Cliff Wickman956db3c2008-02-07 00:14:43 -08001991 *
1992 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1993 * calling callback functions for each.
1994 */
1995static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
1996{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001997 struct cgroup_scanner scan;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001998
Li Zefan7f81b1a2009-04-02 16:57:53 -07001999 scan.cg = from->css.cgroup;
2000 scan.test_task = NULL; /* select all tasks in cgroup */
2001 scan.process_task = cpuset_do_move_task;
2002 scan.heap = NULL;
2003 scan.data = to->css.cgroup;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002004
Li Zefan7f81b1a2009-04-02 16:57:53 -07002005 if (cgroup_scan_tasks(&scan))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002006 printk(KERN_ERR "move_member_tasks_to_cpuset: "
2007 "cgroup_scan_tasks failed\n");
2008}
2009
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002010/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002011 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002012 * or memory nodes, we need to walk over the cpuset hierarchy,
2013 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002014 * last CPU or node from a cpuset, then move the tasks in the empty
2015 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002016 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002017 * Called with cgroup_mutex held
2018 * callback_mutex must not be held, as cpuset_attach() will take it.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002019 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002020static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002021{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002022 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002023
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002024 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08002025 * Find its next-highest non-empty parent, (top cpuset
2026 * has online cpus, so can't be empty).
2027 */
2028 parent = cs->parent;
Li Zefan300ed6c2009-01-07 18:08:44 -08002029 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002030 nodes_empty(parent->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002031 parent = parent->parent;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002032
2033 move_member_tasks_to_cpuset(cs, parent);
2034}
2035
2036/*
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05302037 * Helper function to traverse cpusets.
2038 * It can be used to walk the cpuset tree from top to bottom, completing
2039 * one layer before dropping down to the next (thus always processing a
2040 * node before any of its children).
2041 */
2042static struct cpuset *cpuset_next(struct list_head *queue)
2043{
2044 struct cpuset *cp;
2045 struct cpuset *child; /* scans child cpusets of cp */
2046 struct cgroup *cont;
2047
2048 if (list_empty(queue))
2049 return NULL;
2050
2051 cp = list_first_entry(queue, struct cpuset, stack_list);
2052 list_del(queue->next);
Tejun Heoae8086c2013-01-07 08:51:07 -08002053 rcu_read_lock();
2054 cpuset_for_each_child(child, cont, cp)
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05302055 list_add_tail(&child->stack_list, queue);
Tejun Heoae8086c2013-01-07 08:51:07 -08002056 rcu_read_unlock();
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05302057
2058 return cp;
2059}
2060
2061
2062/*
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302063 * Walk the specified cpuset subtree upon a hotplug operation (CPU/Memory
2064 * online/offline) and update the cpusets accordingly.
2065 * For regular CPU/Mem hotplug, look for empty cpusets; the tasks of such
2066 * cpuset must be moved to a parent cpuset.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002067 *
Paul Menage2df167a2008-02-07 00:14:45 -08002068 * Called with cgroup_mutex held. We take callback_mutex to modify
Cliff Wickman956db3c2008-02-07 00:14:43 -08002069 * cpus_allowed and mems_allowed.
2070 *
2071 * This walk processes the tree from top to bottom, completing one layer
2072 * before dropping down to the next. It always processes a node before
2073 * any of its children.
2074 *
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002075 * In the case of memory hot-unplug, it will remove nodes from N_MEMORY
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302076 * if all present pages from a node are offlined.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002077 */
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302078static void
2079scan_cpusets_upon_hotplug(struct cpuset *root, enum hotplug_event event)
Cliff Wickman956db3c2008-02-07 00:14:43 -08002080{
Li Zefan8d1e6262008-07-29 22:33:21 -07002081 LIST_HEAD(queue);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302082 struct cpuset *cp; /* scans cpusets being updated */
Li Zefanee24d372011-03-23 16:42:47 -07002083 static nodemask_t oldmems; /* protected by cgroup_mutex */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002084
Cliff Wickman956db3c2008-02-07 00:14:43 -08002085 list_add_tail((struct list_head *)&root->stack_list, &queue);
2086
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302087 switch (event) {
2088 case CPUSET_CPU_OFFLINE:
2089 while ((cp = cpuset_next(&queue)) != NULL) {
Paul Jacksonb4501292008-02-07 00:14:47 -08002090
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302091 /* Continue past cpusets with all cpus online */
2092 if (cpumask_subset(cp->cpus_allowed, cpu_active_mask))
2093 continue;
Paul Jacksonb4501292008-02-07 00:14:47 -08002094
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302095 /* Remove offline cpus from this cpuset. */
2096 mutex_lock(&callback_mutex);
2097 cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
2098 cpu_active_mask);
2099 mutex_unlock(&callback_mutex);
Miao Xief9b4fb82008-07-25 01:47:22 -07002100
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302101 /* Move tasks from the empty cpuset to a parent */
2102 if (cpumask_empty(cp->cpus_allowed))
2103 remove_tasks_in_empty_cpuset(cp);
2104 else
2105 update_tasks_cpumask(cp, NULL);
2106 }
2107 break;
2108
2109 case CPUSET_MEM_OFFLINE:
2110 while ((cp = cpuset_next(&queue)) != NULL) {
2111
2112 /* Continue past cpusets with all mems online */
2113 if (nodes_subset(cp->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002114 node_states[N_MEMORY]))
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302115 continue;
2116
2117 oldmems = cp->mems_allowed;
2118
2119 /* Remove offline mems from this cpuset. */
2120 mutex_lock(&callback_mutex);
2121 nodes_and(cp->mems_allowed, cp->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002122 node_states[N_MEMORY]);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302123 mutex_unlock(&callback_mutex);
Paul Jacksonb4501292008-02-07 00:14:47 -08002124
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302125 /* Move tasks from the empty cpuset to a parent */
2126 if (nodes_empty(cp->mems_allowed))
2127 remove_tasks_in_empty_cpuset(cp);
2128 else
2129 update_tasks_nodemask(cp, &oldmems, NULL);
Miao Xief9b4fb82008-07-25 01:47:22 -07002130 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002131 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002132}
2133
2134/*
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002135 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2136 * period. This is necessary in order to make cpusets transparent
2137 * (of no affect) on systems that are actively using CPU hotplug
2138 * but making no active use of cpusets.
2139 *
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05302140 * The only exception to this is suspend/resume, where we don't
2141 * modify cpusets at all.
2142 *
Paul Jackson38837fc2006-09-29 02:01:16 -07002143 * This routine ensures that top_cpuset.cpus_allowed tracks
Tejun Heo3a101d02010-06-08 21:40:36 +02002144 * cpu_active_mask on each CPU hotplug (cpuhp) event.
Max Krasnyanskycf417142008-08-11 14:33:53 -07002145 *
2146 * Called within get_online_cpus(). Needs to call cgroup_lock()
2147 * before calling generate_sched_domains().
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302148 *
2149 * @cpu_online: Indicates whether this is a CPU online event (true) or
2150 * a CPU offline event (false).
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002151 */
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302152void cpuset_update_active_cpus(bool cpu_online)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002153{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002154 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10302155 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002156 int ndoms;
2157
Max Krasnyanskycf417142008-08-11 14:33:53 -07002158 cgroup_lock();
Li Zefan0b4217b2009-04-02 16:57:49 -07002159 mutex_lock(&callback_mutex);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002160 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Li Zefan0b4217b2009-04-02 16:57:49 -07002161 mutex_unlock(&callback_mutex);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302162
2163 if (!cpu_online)
2164 scan_cpusets_upon_hotplug(&top_cpuset, CPUSET_CPU_OFFLINE);
2165
Max Krasnyanskycf417142008-08-11 14:33:53 -07002166 ndoms = generate_sched_domains(&doms, &attr);
2167 cgroup_unlock();
2168
2169 /* Have scheduler rebuild the domains */
2170 partition_sched_domains(ndoms, doms, attr);
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002171}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002172
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002173#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002174/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002175 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2176 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302177 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07002178 */
Miao Xief4818912008-11-19 15:36:30 -08002179static int cpuset_track_online_nodes(struct notifier_block *self,
2180 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002181{
Li Zefanee24d372011-03-23 16:42:47 -07002182 static nodemask_t oldmems; /* protected by cgroup_mutex */
Miao Xie5ab116c2010-03-23 13:35:34 -07002183
Max Krasnyanskycf417142008-08-11 14:33:53 -07002184 cgroup_lock();
Miao Xief4818912008-11-19 15:36:30 -08002185 switch (action) {
2186 case MEM_ONLINE:
Li Zefanee24d372011-03-23 16:42:47 -07002187 oldmems = top_cpuset.mems_allowed;
Li Zefan0b4217b2009-04-02 16:57:49 -07002188 mutex_lock(&callback_mutex);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002189 top_cpuset.mems_allowed = node_states[N_MEMORY];
Li Zefan0b4217b2009-04-02 16:57:49 -07002190 mutex_unlock(&callback_mutex);
Li Zefanee24d372011-03-23 16:42:47 -07002191 update_tasks_nodemask(&top_cpuset, &oldmems, NULL);
Miao Xie5ab116c2010-03-23 13:35:34 -07002192 break;
2193 case MEM_OFFLINE:
2194 /*
2195 * needn't update top_cpuset.mems_allowed explicitly because
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302196 * scan_cpusets_upon_hotplug() will update it.
Miao Xie5ab116c2010-03-23 13:35:34 -07002197 */
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302198 scan_cpusets_upon_hotplug(&top_cpuset, CPUSET_MEM_OFFLINE);
Miao Xief4818912008-11-19 15:36:30 -08002199 break;
2200 default:
2201 break;
2202 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002203 cgroup_unlock();
Miao Xie53feb292010-03-23 13:35:35 -07002204
Miao Xief4818912008-11-19 15:36:30 -08002205 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002206}
2207#endif
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/**
2210 * cpuset_init_smp - initialize cpus_allowed
2211 *
2212 * Description: Finish top cpuset after cpu, node maps are initialized
2213 **/
2214
2215void __init cpuset_init_smp(void)
2216{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002217 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002218 top_cpuset.mems_allowed = node_states[N_MEMORY];
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002219
Miao Xief4818912008-11-19 15:36:30 -08002220 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
Miao Xief90d4112009-01-16 10:24:10 +08002221
2222 cpuset_wq = create_singlethread_workqueue("cpuset");
2223 BUG_ON(!cpuset_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
2226/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2228 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002229 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002231 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10302233 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 * tasks cpuset.
2235 **/
2236
Li Zefan6af866a2009-01-07 18:08:45 -08002237void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002239 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002240 task_lock(tsk);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002241 guarantee_online_cpus(task_cs(tsk), pmask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002242 task_unlock(tsk);
Oleg Nesterov897f0b32010-03-15 10:10:03 +01002243 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002246void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002247{
2248 const struct cpuset *cs;
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002249
2250 rcu_read_lock();
2251 cs = task_cs(tsk);
2252 if (cs)
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09002253 do_set_cpus_allowed(tsk, cs->cpus_allowed);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002254 rcu_read_unlock();
2255
2256 /*
2257 * We own tsk->cpus_allowed, nobody can change it under us.
2258 *
2259 * But we used cs && cs->cpus_allowed lockless and thus can
2260 * race with cgroup_attach_task() or update_cpumask() and get
2261 * the wrong tsk->cpus_allowed. However, both cases imply the
2262 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2263 * which takes task_rq_lock().
2264 *
2265 * If we are called after it dropped the lock we must see all
2266 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2267 * set any mask even if it is not right from task_cs() pov,
2268 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002269 *
2270 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2271 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002272 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002273}
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275void cpuset_init_current_mems_allowed(void)
2276{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002277 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002280/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002281 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2282 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2283 *
2284 * Description: Returns the nodemask_t mems_allowed of the cpuset
2285 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002286 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002287 * tasks cpuset.
2288 **/
2289
2290nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2291{
2292 nodemask_t mask;
2293
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002294 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002295 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -07002296 guarantee_online_mems(task_cs(tsk), &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002297 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002298 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002299
2300 return mask;
2301}
2302
2303/**
Mel Gorman19770b32008-04-28 02:12:18 -07002304 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2305 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002306 *
Mel Gorman19770b32008-04-28 02:12:18 -07002307 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 */
Mel Gorman19770b32008-04-28 02:12:18 -07002309int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Mel Gorman19770b32008-04-28 02:12:18 -07002311 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Paul Jackson9bf22292005-09-06 15:18:12 -07002314/*
Paul Menage78608362008-04-29 01:00:26 -07002315 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2316 * mem_hardwall ancestor to the specified cpuset. Call holding
2317 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2318 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 */
Paul Menage78608362008-04-29 01:00:26 -07002320static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Paul Menage78608362008-04-29 01:00:26 -07002322 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
Paul Jackson9bf22292005-09-06 15:18:12 -07002323 cs = cs->parent;
2324 return cs;
2325}
2326
2327/**
David Rientjesa1bc5a42009-04-02 16:57:54 -07002328 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2329 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002330 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002331 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002332 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2333 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2334 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2335 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2336 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2337 * flag, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002338 * Otherwise, no.
2339 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002340 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2341 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2342 * might sleep, and might allow a node from an enclosing cpuset.
Paul Jackson02a0e532006-12-13 00:34:25 -08002343 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002344 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2345 * cpusets, and never sleeps.
Paul Jackson02a0e532006-12-13 00:34:25 -08002346 *
2347 * The __GFP_THISNODE placement logic is really handled elsewhere,
2348 * by forcibly using a zonelist starting at a specified node, and by
2349 * (in get_page_from_freelist()) refusing to consider the zones for
2350 * any node on the zonelist except the first. By the time any such
2351 * calls get to this routine, we should just shut up and say 'yes'.
2352 *
Paul Jackson9bf22292005-09-06 15:18:12 -07002353 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002354 * and do not allow allocations outside the current tasks cpuset
2355 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002356 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002357 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002358 *
Paul Jackson02a0e532006-12-13 00:34:25 -08002359 * Scanning up parent cpusets requires callback_mutex. The
2360 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2361 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2362 * current tasks mems_allowed came up empty on the first pass over
2363 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2364 * cpuset are short of memory, might require taking the callback_mutex
2365 * mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002366 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002367 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002368 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2369 * so no allocation on a node outside the cpuset is allowed (unless
2370 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002371 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002372 * The second pass through get_page_from_freelist() doesn't even call
2373 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2374 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2375 * in alloc_flags. That logic and the checks below have the combined
2376 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002377 * in_interrupt - any node ok (current task context irrelevant)
2378 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002379 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002380 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002381 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002382 *
2383 * Rule:
David Rientjesa1bc5a42009-04-02 16:57:54 -07002384 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
Paul Jackson36be57f2006-05-20 15:00:10 -07002385 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2386 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson02a0e532006-12-13 00:34:25 -08002387 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002388int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002389{
Paul Jackson9bf22292005-09-06 15:18:12 -07002390 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002391 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002392
Christoph Lameter9b819d22006-09-25 23:31:40 -07002393 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002394 return 1;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002395 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002396 if (node_isset(node, current->mems_allowed))
2397 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002398 /*
2399 * Allow tasks that have access to memory reserves because they have
2400 * been OOM killed to get memory anywhere.
2401 */
2402 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2403 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002404 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2405 return 0;
2406
Bob Picco5563e772005-11-13 16:06:35 -08002407 if (current->flags & PF_EXITING) /* Let dying task have memory */
2408 return 1;
2409
Paul Jackson9bf22292005-09-06 15:18:12 -07002410 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002411 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002412
Paul Jackson053199e2005-10-30 15:02:30 -08002413 task_lock(current);
Paul Menage78608362008-04-29 01:00:26 -07002414 cs = nearest_hardwall_ancestor(task_cs(current));
Paul Jackson053199e2005-10-30 15:02:30 -08002415 task_unlock(current);
2416
Paul Jackson9bf22292005-09-06 15:18:12 -07002417 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002418 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002419 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Paul Jackson02a0e532006-12-13 00:34:25 -08002422/*
David Rientjesa1bc5a42009-04-02 16:57:54 -07002423 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2424 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002425 * @gfp_mask: memory allocation flags
2426 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002427 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2428 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2429 * yes. If the task has been OOM killed and has access to memory reserves as
2430 * specified by the TIF_MEMDIE flag, yes.
2431 * Otherwise, no.
Paul Jackson02a0e532006-12-13 00:34:25 -08002432 *
2433 * The __GFP_THISNODE placement logic is really handled elsewhere,
2434 * by forcibly using a zonelist starting at a specified node, and by
2435 * (in get_page_from_freelist()) refusing to consider the zones for
2436 * any node on the zonelist except the first. By the time any such
2437 * calls get to this routine, we should just shut up and say 'yes'.
2438 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002439 * Unlike the cpuset_node_allowed_softwall() variant, above,
2440 * this variant requires that the node be in the current task's
Paul Jackson02a0e532006-12-13 00:34:25 -08002441 * mems_allowed or that we're in interrupt. It does not scan up the
2442 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2443 * It never sleeps.
2444 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002445int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -08002446{
Paul Jackson02a0e532006-12-13 00:34:25 -08002447 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2448 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002449 if (node_isset(node, current->mems_allowed))
2450 return 1;
Daniel Walkerdedf8b72007-10-18 03:06:04 -07002451 /*
2452 * Allow tasks that have access to memory reserves because they have
2453 * been OOM killed to get memory anywhere.
2454 */
2455 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2456 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002457 return 0;
2458}
2459
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002460/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07002461 * cpuset_mem_spread_node() - On which node to begin search for a file page
2462 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08002463 *
2464 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2465 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2466 * and if the memory allocation used cpuset_mem_spread_node()
2467 * to determine on which node to start looking, as it will for
2468 * certain page cache or slab cache pages such as used for file
2469 * system buffers and inode caches, then instead of starting on the
2470 * local node to look for a free page, rather spread the starting
2471 * node around the tasks mems_allowed nodes.
2472 *
2473 * We don't have to worry about the returned node being offline
2474 * because "it can't happen", and even if it did, it would be ok.
2475 *
2476 * The routines calling guarantee_online_mems() are careful to
2477 * only set nodes in task->mems_allowed that are online. So it
2478 * should not be possible for the following code to return an
2479 * offline node. But if it did, that would be ok, as this routine
2480 * is not returning the node where the allocation must be, only
2481 * the node where the search should start. The zonelist passed to
2482 * __alloc_pages() will include all nodes. If the slab allocator
2483 * is passed an offline node, it will fall back to the local node.
2484 * See kmem_cache_alloc_node().
2485 */
2486
Jack Steiner6adef3e2010-05-26 14:42:49 -07002487static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08002488{
2489 int node;
2490
Jack Steiner6adef3e2010-05-26 14:42:49 -07002491 node = next_node(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08002492 if (node == MAX_NUMNODES)
2493 node = first_node(current->mems_allowed);
Jack Steiner6adef3e2010-05-26 14:42:49 -07002494 *rotor = node;
Paul Jackson825a46a2006-03-24 03:16:03 -08002495 return node;
2496}
Jack Steiner6adef3e2010-05-26 14:42:49 -07002497
2498int cpuset_mem_spread_node(void)
2499{
Michal Hocko778d3b02011-07-26 16:08:30 -07002500 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2501 current->cpuset_mem_spread_rotor =
2502 node_random(&current->mems_allowed);
2503
Jack Steiner6adef3e2010-05-26 14:42:49 -07002504 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2505}
2506
2507int cpuset_slab_spread_node(void)
2508{
Michal Hocko778d3b02011-07-26 16:08:30 -07002509 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2510 current->cpuset_slab_spread_rotor =
2511 node_random(&current->mems_allowed);
2512
Jack Steiner6adef3e2010-05-26 14:42:49 -07002513 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2514}
2515
Paul Jackson825a46a2006-03-24 03:16:03 -08002516EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2517
2518/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002519 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2520 * @tsk1: pointer to task_struct of some task.
2521 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002522 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002523 * Description: Return true if @tsk1's mems_allowed intersects the
2524 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2525 * one of the task's memory usage might impact the memory available
2526 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002527 **/
2528
David Rientjesbbe373f2007-10-16 23:25:58 -07002529int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2530 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002531{
David Rientjesbbe373f2007-10-16 23:25:58 -07002532 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002533}
2534
David Rientjes75aa1992009-01-06 14:39:01 -08002535/**
2536 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2537 * @task: pointer to task_struct of some task.
2538 *
2539 * Description: Prints @task's name, cpuset name, and cached copy of its
2540 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2541 * dereferencing task_cs(task).
2542 */
2543void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2544{
2545 struct dentry *dentry;
2546
2547 dentry = task_cs(tsk)->css.cgroup->dentry;
2548 spin_lock(&cpuset_buffer_lock);
2549 snprintf(cpuset_name, CPUSET_NAME_LEN,
2550 dentry ? (const char *)dentry->d_name.name : "/");
2551 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2552 tsk->mems_allowed);
2553 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2554 tsk->comm, cpuset_name, cpuset_nodelist);
2555 spin_unlock(&cpuset_buffer_lock);
2556}
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002559 * Collection of memory_pressure is suppressed unless
2560 * this flag is enabled by writing "1" to the special
2561 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2562 */
2563
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002564int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002565
2566/**
2567 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2568 *
2569 * Keep a running average of the rate of synchronous (direct)
2570 * page reclaim efforts initiated by tasks in each cpuset.
2571 *
2572 * This represents the rate at which some task in the cpuset
2573 * ran low on memory on all nodes it was allowed to use, and
2574 * had to enter the kernels page reclaim code in an effort to
2575 * create more free memory by tossing clean pages or swapping
2576 * or writing dirty pages.
2577 *
2578 * Display to user space in the per-cpuset read-only file
2579 * "memory_pressure". Value displayed is an integer
2580 * representing the recent rate of entry into the synchronous
2581 * (direct) page reclaim by any task attached to the cpuset.
2582 **/
2583
2584void __cpuset_memory_pressure_bump(void)
2585{
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002586 task_lock(current);
Paul Menage8793d852007-10-18 23:39:39 -07002587 fmeter_markevent(&task_cs(current)->fmeter);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002588 task_unlock(current);
2589}
2590
Paul Menage8793d852007-10-18 23:39:39 -07002591#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002592/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 * proc_cpuset_show()
2594 * - Print tasks cpuset path into seq_file.
2595 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002596 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2597 * doesn't really matter if tsk->cpuset changes after we read it,
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002598 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002599 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 */
Paul Jackson029190c2007-10-18 23:40:20 -07002601static int proc_cpuset_show(struct seq_file *m, void *unused_v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002603 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct task_struct *tsk;
2605 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002606 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002607 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Eric W. Biederman99f89552006-06-26 00:25:55 -07002609 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2611 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002612 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Eric W. Biederman99f89552006-06-26 00:25:55 -07002614 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002615 pid = m->private;
2616 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002617 if (!tsk)
2618 goto out_free;
2619
2620 retval = -EINVAL;
Paul Menage8793d852007-10-18 23:39:39 -07002621 cgroup_lock();
2622 css = task_subsys_state(tsk, cpuset_subsys_id);
2623 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002625 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 seq_puts(m, buf);
2627 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002628out_unlock:
Paul Menage8793d852007-10-18 23:39:39 -07002629 cgroup_unlock();
Eric W. Biederman99f89552006-06-26 00:25:55 -07002630 put_task_struct(tsk);
2631out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002633out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return retval;
2635}
2636
2637static int cpuset_open(struct inode *inode, struct file *file)
2638{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002639 struct pid *pid = PROC_I(inode)->pid;
2640 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Arjan van de Ven9a321442007-02-12 00:55:35 -08002643const struct file_operations proc_cpuset_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .open = cpuset_open,
2645 .read = seq_read,
2646 .llseek = seq_lseek,
2647 .release = single_release,
2648};
Paul Menage8793d852007-10-18 23:39:39 -07002649#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Heiko Carstensd01d4822009-09-21 11:06:27 +02002651/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002652void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002654 seq_printf(m, "Mems_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002655 seq_nodemask(m, &task->mems_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002656 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002657 seq_printf(m, "Mems_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002658 seq_nodemask_list(m, &task->mems_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002659 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}