blob: 658eb1a32084bacb9709d9c0f41f5e16610ce13d [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
151/* convenient tests for these bits */
Tejun Heoefeb77b2013-01-07 08:51:07 -0800152static inline bool is_cpuset_online(const struct cpuset *cs)
153{
154 return test_bit(CS_ONLINE, &cs->flags);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static inline int is_cpu_exclusive(const struct cpuset *cs)
158{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800159 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static inline int is_mem_exclusive(const struct cpuset *cs)
163{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800164 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Paul Menage78608362008-04-29 01:00:26 -0700167static inline int is_mem_hardwall(const struct cpuset *cs)
168{
169 return test_bit(CS_MEM_HARDWALL, &cs->flags);
170}
171
Paul Jackson029190c2007-10-18 23:40:20 -0700172static inline int is_sched_load_balance(const struct cpuset *cs)
173{
174 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
175}
176
Paul Jackson45b07ef2006-01-08 01:00:56 -0800177static inline int is_memory_migrate(const struct cpuset *cs)
178{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800179 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800180}
181
Paul Jackson825a46a2006-03-24 03:16:03 -0800182static inline int is_spread_page(const struct cpuset *cs)
183{
184 return test_bit(CS_SPREAD_PAGE, &cs->flags);
185}
186
187static inline int is_spread_slab(const struct cpuset *cs)
188{
189 return test_bit(CS_SPREAD_SLAB, &cs->flags);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static struct cpuset top_cpuset = {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800193 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) |
194 (1 << CS_MEM_EXCLUSIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Tejun Heoae8086c2013-01-07 08:51:07 -0800197/**
198 * cpuset_for_each_child - traverse online children of a cpuset
199 * @child_cs: loop cursor pointing to the current child
200 * @pos_cgrp: used for iteration
201 * @parent_cs: target cpuset to walk children of
202 *
203 * Walk @child_cs through the online children of @parent_cs. Must be used
204 * with RCU read locked.
205 */
206#define cpuset_for_each_child(child_cs, pos_cgrp, parent_cs) \
207 cgroup_for_each_child((pos_cgrp), (parent_cs)->css.cgroup) \
208 if (is_cpuset_online(((child_cs) = cgroup_cs((pos_cgrp)))))
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Paul Menage2df167a2008-02-07 00:14:45 -0800211 * There are two global mutexes guarding cpuset structures. The first
212 * is the main control groups cgroup_mutex, accessed via
213 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
214 * callback_mutex, below. They can nest. It is ok to first take
215 * cgroup_mutex, then nest callback_mutex. We also require taking
216 * task_lock() when dereferencing a task's cpuset pointer. See "The
217 * task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800219 * A task must hold both mutexes to modify cpusets. If a task
Paul Menage2df167a2008-02-07 00:14:45 -0800220 * holds cgroup_mutex, then it blocks others wanting that mutex,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800221 * ensuring that it is the only task able to also acquire callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800222 * and be able to modify cpusets. It can perform various checks on
223 * the cpuset structure first, knowing nothing will change. It can
Paul Menage2df167a2008-02-07 00:14:45 -0800224 * also allocate memory while just holding cgroup_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800225 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800226 * acquire callback_mutex to query cpusets. Once it is ready to make
227 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
Paul Jackson053199e2005-10-30 15:02:30 -0800229 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800230 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800231 * from one of the callbacks into the cpuset code from within
232 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800234 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800235 * access to cpusets.
236 *
Miao Xie58568d22009-06-16 15:31:49 -0700237 * Now, the task_struct fields mems_allowed and mempolicy may be changed
238 * by other task, we use alloc_lock in the task_struct fields to protect
239 * them.
Paul Jackson053199e2005-10-30 15:02:30 -0800240 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800241 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800242 * small pieces of code, such as when reading out possibly multi-word
243 * cpumasks and nodemasks.
244 *
Paul Menage2df167a2008-02-07 00:14:45 -0800245 * Accessing a task's cpuset should be done in accordance with the
246 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
248
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800249static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700250
Max Krasnyanskycf417142008-08-11 14:33:53 -0700251/*
David Rientjes75aa1992009-01-06 14:39:01 -0800252 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
253 * buffers. They are statically allocated to prevent using excess stack
254 * when calling cpuset_print_task_mems_allowed().
255 */
256#define CPUSET_NAME_LEN (128)
257#define CPUSET_NODELIST_LEN (256)
258static char cpuset_name[CPUSET_NAME_LEN];
259static char cpuset_nodelist[CPUSET_NODELIST_LEN];
260static DEFINE_SPINLOCK(cpuset_buffer_lock);
261
262/*
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800263 * CPU / memory hotplug is handled asynchronously.
264 */
265static void cpuset_hotplug_workfn(struct work_struct *work);
266
267static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn);
268
269/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700270 * This is ugly, but preserves the userspace API for existing cpuset
Paul Menage8793d852007-10-18 23:39:39 -0700271 * users. If someone tries to mount the "cpuset" filesystem, we
Max Krasnyanskycf417142008-08-11 14:33:53 -0700272 * silently switch it to mount "cgroup" instead
273 */
Al Virof7e83572010-07-26 13:23:11 +0400274static struct dentry *cpuset_mount(struct file_system_type *fs_type,
275 int flags, const char *unused_dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Paul Menage8793d852007-10-18 23:39:39 -0700277 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
Al Virof7e83572010-07-26 13:23:11 +0400278 struct dentry *ret = ERR_PTR(-ENODEV);
Paul Menage8793d852007-10-18 23:39:39 -0700279 if (cgroup_fs) {
280 char mountopts[] =
281 "cpuset,noprefix,"
282 "release_agent=/sbin/cpuset_release_agent";
Al Virof7e83572010-07-26 13:23:11 +0400283 ret = cgroup_fs->mount(cgroup_fs, flags,
284 unused_dev_name, mountopts);
Paul Menage8793d852007-10-18 23:39:39 -0700285 put_filesystem(cgroup_fs);
286 }
287 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static struct file_system_type cpuset_fs_type = {
291 .name = "cpuset",
Al Virof7e83572010-07-26 13:23:11 +0400292 .mount = cpuset_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800296 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * are online. If none are online, walk up the cpuset hierarchy
298 * until we find one that does have some online cpus. If we get
299 * all the way to the top and still haven't found any online cpus,
Rusty Russell5f054e32012-03-29 15:38:31 +1030300 * return cpu_online_mask. Or if passed a NULL cs from an exit'ing
301 * task, return cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * One way or another, we guarantee to return some non-empty subset
Rusty Russell5f054e32012-03-29 15:38:31 +1030304 * of cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800306 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308
Li Zefan6af866a2009-01-07 18:08:45 -0800309static void guarantee_online_cpus(const struct cpuset *cs,
310 struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Li Zefan300ed6c2009-01-07 18:08:44 -0800312 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 cs = cs->parent;
314 if (cs)
Li Zefan300ed6c2009-01-07 18:08:44 -0800315 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else
Li Zefan300ed6c2009-01-07 18:08:44 -0800317 cpumask_copy(pmask, cpu_online_mask);
318 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
322 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700323 * are online, with memory. If none are online with memory, walk
324 * up the cpuset hierarchy until we find one that does have some
325 * online mems. If we get all the way to the top and still haven't
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800326 * found any online mems, return node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 *
328 * One way or another, we guarantee to return some non-empty subset
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800329 * of node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800331 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
333
334static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
335{
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700336 while (cs && !nodes_intersects(cs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800337 node_states[N_MEMORY]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 cs = cs->parent;
339 if (cs)
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700340 nodes_and(*pmask, cs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800341 node_states[N_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 else
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800343 *pmask = node_states[N_MEMORY];
344 BUG_ON(!nodes_intersects(*pmask, node_states[N_MEMORY]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Miao Xief3b39d42009-06-16 15:31:46 -0700347/*
348 * update task's spread flag if cpuset's page/slab spread flag is set
349 *
350 * Called with callback_mutex/cgroup_mutex held
351 */
352static void cpuset_update_task_spread_flag(struct cpuset *cs,
353 struct task_struct *tsk)
354{
355 if (is_spread_page(cs))
356 tsk->flags |= PF_SPREAD_PAGE;
357 else
358 tsk->flags &= ~PF_SPREAD_PAGE;
359 if (is_spread_slab(cs))
360 tsk->flags |= PF_SPREAD_SLAB;
361 else
362 tsk->flags &= ~PF_SPREAD_SLAB;
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
366 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
367 *
368 * One cpuset is a subset of another if all its allowed CPUs and
369 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Menage2df167a2008-02-07 00:14:45 -0800370 * are only set if the other's are set. Call holding cgroup_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
372
373static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
374{
Li Zefan300ed6c2009-01-07 18:08:44 -0800375 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 nodes_subset(p->mems_allowed, q->mems_allowed) &&
377 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
378 is_mem_exclusive(p) <= is_mem_exclusive(q);
379}
380
Li Zefan645fcc92009-01-07 18:08:43 -0800381/**
382 * alloc_trial_cpuset - allocate a trial cpuset
383 * @cs: the cpuset that the trial cpuset duplicates
384 */
385static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
386{
Li Zefan300ed6c2009-01-07 18:08:44 -0800387 struct cpuset *trial;
388
389 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
390 if (!trial)
391 return NULL;
392
393 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
394 kfree(trial);
395 return NULL;
396 }
397 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
398
399 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800400}
401
402/**
403 * free_trial_cpuset - free the trial cpuset
404 * @trial: the trial cpuset to be freed
405 */
406static void free_trial_cpuset(struct cpuset *trial)
407{
Li Zefan300ed6c2009-01-07 18:08:44 -0800408 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800409 kfree(trial);
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
413 * validate_change() - Used to validate that any proposed cpuset change
414 * follows the structural rules for cpusets.
415 *
416 * If we replaced the flag and mask values of the current cpuset
417 * (cur) with those values in the trial cpuset (trial), would
418 * our various subset and exclusive rules still be valid? Presumes
Paul Menage2df167a2008-02-07 00:14:45 -0800419 * cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
421 * 'cur' is the address of an actual, in-use cpuset. Operations
422 * such as list traversal that depend on the actual address of the
423 * cpuset in the list must use cur below, not trial.
424 *
425 * 'trial' is the address of bulk structure copy of cur, with
426 * perhaps one or more of the fields cpus_allowed, mems_allowed,
427 * or flags changed to new, trial values.
428 *
429 * Return 0 if valid, -errno if not.
430 */
431
432static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
433{
Paul Menage8793d852007-10-18 23:39:39 -0700434 struct cgroup *cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct cpuset *c, *par;
Tejun Heoae8086c2013-01-07 08:51:07 -0800436 int ret;
437
438 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* Each of our child cpusets must be a subset of us */
Tejun Heoae8086c2013-01-07 08:51:07 -0800441 ret = -EBUSY;
442 cpuset_for_each_child(c, cont, cur)
443 if (!is_cpuset_subset(c, trial))
444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* Remaining checks don't apply to root cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800447 ret = 0;
Paul Jackson69604062006-12-06 20:36:15 -0800448 if (cur == &top_cpuset)
Tejun Heoae8086c2013-01-07 08:51:07 -0800449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Paul Jackson69604062006-12-06 20:36:15 -0800451 par = cur->parent;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* We must be a subset of our parent cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800454 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!is_cpuset_subset(trial, par))
Tejun Heoae8086c2013-01-07 08:51:07 -0800456 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Paul Menage2df167a2008-02-07 00:14:45 -0800458 /*
459 * If either I or some sibling (!= me) is exclusive, we can't
460 * overlap
461 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800462 ret = -EINVAL;
463 cpuset_for_each_child(c, cont, par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
465 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800466 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
469 c != cur &&
470 nodes_intersects(trial->mems_allowed, c->mems_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
Paul Jackson020958b2007-10-18 23:40:21 -0700474 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
Tejun Heoae8086c2013-01-07 08:51:07 -0800475 ret = -ENOSPC;
476 if (cgroup_task_count(cur->css.cgroup) &&
477 (cpumask_empty(trial->cpus_allowed) ||
478 nodes_empty(trial->mems_allowed)))
479 goto out;
Paul Jackson020958b2007-10-18 23:40:21 -0700480
Tejun Heoae8086c2013-01-07 08:51:07 -0800481 ret = 0;
482out:
483 rcu_read_unlock();
484 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Paul Menagedb7f47c2009-04-02 16:57:55 -0700487#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700488/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700489 * Helper routine for generate_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -0700490 * Do cpusets a, b have overlapping cpus_allowed masks?
491 */
Paul Jackson029190c2007-10-18 23:40:20 -0700492static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
493{
Li Zefan300ed6c2009-01-07 18:08:44 -0800494 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
Paul Jackson029190c2007-10-18 23:40:20 -0700495}
496
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900497static void
498update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
499{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900500 if (dattr->relax_domain_level < c->relax_domain_level)
501 dattr->relax_domain_level = c->relax_domain_level;
502 return;
503}
504
Lai Jiangshanf5393692008-07-29 22:33:22 -0700505static void
506update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
507{
508 LIST_HEAD(q);
509
510 list_add(&c->stack_list, &q);
511 while (!list_empty(&q)) {
512 struct cpuset *cp;
513 struct cgroup *cont;
514 struct cpuset *child;
515
516 cp = list_first_entry(&q, struct cpuset, stack_list);
517 list_del(q.next);
518
Li Zefan300ed6c2009-01-07 18:08:44 -0800519 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700520 continue;
521
522 if (is_sched_load_balance(cp))
523 update_domain_attr(dattr, cp);
524
Tejun Heoae8086c2013-01-07 08:51:07 -0800525 rcu_read_lock();
526 cpuset_for_each_child(child, cont, cp)
Lai Jiangshanf5393692008-07-29 22:33:22 -0700527 list_add_tail(&child->stack_list, &q);
Tejun Heoae8086c2013-01-07 08:51:07 -0800528 rcu_read_unlock();
Lai Jiangshanf5393692008-07-29 22:33:22 -0700529 }
530}
531
Paul Jackson029190c2007-10-18 23:40:20 -0700532/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700533 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700534 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700535 * This function builds a partial partition of the systems CPUs
536 * A 'partial partition' is a set of non-overlapping subsets whose
537 * union is a subset of that set.
538 * The output of this function needs to be passed to kernel/sched.c
539 * partition_sched_domains() routine, which will rebuild the scheduler's
540 * load balancing domains (sched domains) as specified by that partial
541 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700542 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800543 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700544 * for a background explanation of this.
545 *
546 * Does not return errors, on the theory that the callers of this
547 * routine would rather not worry about failures to rebuild sched
548 * domains when operating in the severe memory shortage situations
549 * that could cause allocation failures below.
550 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700551 * Must be called with cgroup_lock held.
Paul Jackson029190c2007-10-18 23:40:20 -0700552 *
553 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700554 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700555 * top-down scan of all cpusets. This scan loads a pointer
556 * to each cpuset marked is_sched_load_balance into the
557 * array 'csa'. For our purposes, rebuilding the schedulers
558 * sched domains, we can ignore !is_sched_load_balance cpusets.
559 * csa - (for CpuSet Array) Array of pointers to all the cpusets
560 * that need to be load balanced, for convenient iterative
561 * access by the subsequent code that finds the best partition,
562 * i.e the set of domains (subsets) of CPUs such that the
563 * cpus_allowed of every cpuset marked is_sched_load_balance
564 * is a subset of one of these domains, while there are as
565 * many such domains as possible, each as small as possible.
566 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
567 * the kernel/sched.c routine partition_sched_domains() in a
568 * convenient format, that can be easily compared to the prior
569 * value to determine what partition elements (sched domains)
570 * were changed (added or removed.)
571 *
572 * Finding the best partition (set of domains):
573 * The triple nested loops below over i, j, k scan over the
574 * load balanced cpusets (using the array of cpuset pointers in
575 * csa[]) looking for pairs of cpusets that have overlapping
576 * cpus_allowed, but which don't have the same 'pn' partition
577 * number and gives them in the same partition number. It keeps
578 * looping on the 'restart' label until it can no longer find
579 * any such pairs.
580 *
581 * The union of the cpus_allowed masks from the set of
582 * all cpusets having the same 'pn' value then form the one
583 * element of the partition (one sched domain) to be passed to
584 * partition_sched_domains().
585 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030586static int generate_sched_domains(cpumask_var_t **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700587 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700588{
Max Krasnyanskycf417142008-08-11 14:33:53 -0700589 LIST_HEAD(q); /* queue of cpusets to be scanned */
Paul Jackson029190c2007-10-18 23:40:20 -0700590 struct cpuset *cp; /* scans q */
591 struct cpuset **csa; /* array of all cpuset ptrs */
592 int csn; /* how many cpuset ptrs in csa so far */
593 int i, j, k; /* indices for partition finding loops */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030594 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900595 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100596 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800597 int nslot; /* next empty doms[] struct cpumask slot */
Paul Jackson029190c2007-10-18 23:40:20 -0700598
Paul Jackson029190c2007-10-18 23:40:20 -0700599 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900600 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700601 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700602
603 /* Special case for the 99% of systems with one, full, sched domain */
604 if (is_sched_load_balance(&top_cpuset)) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030605 ndoms = 1;
606 doms = alloc_sched_domains(ndoms);
Paul Jackson029190c2007-10-18 23:40:20 -0700607 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700608 goto done;
609
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900610 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
611 if (dattr) {
612 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700613 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900614 }
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030615 cpumask_copy(doms[0], top_cpuset.cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700616
Max Krasnyanskycf417142008-08-11 14:33:53 -0700617 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700618 }
619
Paul Jackson029190c2007-10-18 23:40:20 -0700620 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
621 if (!csa)
622 goto done;
623 csn = 0;
624
Li Zefanaeed6822008-07-29 22:33:24 -0700625 list_add(&top_cpuset.stack_list, &q);
626 while (!list_empty(&q)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700627 struct cgroup *cont;
628 struct cpuset *child; /* scans child cpusets of cp */
Lai Jiangshan489a5392008-07-25 01:47:23 -0700629
Li Zefanaeed6822008-07-29 22:33:24 -0700630 cp = list_first_entry(&q, struct cpuset, stack_list);
631 list_del(q.next);
632
Li Zefan300ed6c2009-01-07 18:08:44 -0800633 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshan489a5392008-07-25 01:47:23 -0700634 continue;
635
Lai Jiangshanf5393692008-07-29 22:33:22 -0700636 /*
637 * All child cpusets contain a subset of the parent's cpus, so
638 * just skip them, and then we call update_domain_attr_tree()
639 * to calc relax_domain_level of the corresponding sched
640 * domain.
641 */
642 if (is_sched_load_balance(cp)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700643 csa[csn++] = cp;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700644 continue;
645 }
Lai Jiangshan489a5392008-07-25 01:47:23 -0700646
Tejun Heoae8086c2013-01-07 08:51:07 -0800647 rcu_read_lock();
648 cpuset_for_each_child(child, cont, cp)
Li Zefanaeed6822008-07-29 22:33:24 -0700649 list_add_tail(&child->stack_list, &q);
Tejun Heoae8086c2013-01-07 08:51:07 -0800650 rcu_read_unlock();
Paul Jackson029190c2007-10-18 23:40:20 -0700651 }
652
653 for (i = 0; i < csn; i++)
654 csa[i]->pn = i;
655 ndoms = csn;
656
657restart:
658 /* Find the best partition (set of sched domains) */
659 for (i = 0; i < csn; i++) {
660 struct cpuset *a = csa[i];
661 int apn = a->pn;
662
663 for (j = 0; j < csn; j++) {
664 struct cpuset *b = csa[j];
665 int bpn = b->pn;
666
667 if (apn != bpn && cpusets_overlap(a, b)) {
668 for (k = 0; k < csn; k++) {
669 struct cpuset *c = csa[k];
670
671 if (c->pn == bpn)
672 c->pn = apn;
673 }
674 ndoms--; /* one less element */
675 goto restart;
676 }
677 }
678 }
679
Max Krasnyanskycf417142008-08-11 14:33:53 -0700680 /*
681 * Now we know how many domains to create.
682 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
683 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030684 doms = alloc_sched_domains(ndoms);
Li Zefan700018e2008-11-18 14:02:03 +0800685 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700686 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700687
688 /*
689 * The rest of the code, including the scheduler, can deal with
690 * dattr==NULL case. No need to abort if alloc fails.
691 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900692 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700693
694 for (nslot = 0, i = 0; i < csn; i++) {
695 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800696 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700697 int apn = a->pn;
698
Max Krasnyanskycf417142008-08-11 14:33:53 -0700699 if (apn < 0) {
700 /* Skip completed partitions */
701 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700702 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700703
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030704 dp = doms[nslot];
Max Krasnyanskycf417142008-08-11 14:33:53 -0700705
706 if (nslot == ndoms) {
707 static int warnings = 10;
708 if (warnings) {
709 printk(KERN_WARNING
710 "rebuild_sched_domains confused:"
711 " nslot %d, ndoms %d, csn %d, i %d,"
712 " apn %d\n",
713 nslot, ndoms, csn, i, apn);
714 warnings--;
715 }
716 continue;
717 }
718
Li Zefan6af866a2009-01-07 18:08:45 -0800719 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700720 if (dattr)
721 *(dattr + nslot) = SD_ATTR_INIT;
722 for (j = i; j < csn; j++) {
723 struct cpuset *b = csa[j];
724
725 if (apn == b->pn) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800726 cpumask_or(dp, dp, b->cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700727 if (dattr)
728 update_domain_attr_tree(dattr + nslot, b);
729
730 /* Done with this partition */
731 b->pn = -1;
732 }
733 }
734 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700735 }
736 BUG_ON(nslot != ndoms);
737
Paul Jackson029190c2007-10-18 23:40:20 -0700738done:
Paul Jackson029190c2007-10-18 23:40:20 -0700739 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700740
Li Zefan700018e2008-11-18 14:02:03 +0800741 /*
742 * Fallback to the default domain if kmalloc() failed.
743 * See comments in partition_sched_domains().
744 */
745 if (doms == NULL)
746 ndoms = 1;
747
Max Krasnyanskycf417142008-08-11 14:33:53 -0700748 *domains = doms;
749 *attributes = dattr;
750 return ndoms;
751}
752
753/*
754 * Rebuild scheduler domains.
755 *
756 * Call with neither cgroup_mutex held nor within get_online_cpus().
757 * Takes both cgroup_mutex and get_online_cpus().
758 *
759 * Cannot be directly called from cpuset code handling changes
760 * to the cpuset pseudo-filesystem, because it cannot be called
761 * from code that already holds cgroup_mutex.
762 */
763static void do_rebuild_sched_domains(struct work_struct *unused)
764{
765 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030766 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700767 int ndoms;
768
769 get_online_cpus();
770
771 /* Generate domain masks and attrs */
772 cgroup_lock();
773 ndoms = generate_sched_domains(&doms, &attr);
774 cgroup_unlock();
775
776 /* Have scheduler rebuild the domains */
777 partition_sched_domains(ndoms, doms, attr);
778
779 put_online_cpus();
780}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700781#else /* !CONFIG_SMP */
782static void do_rebuild_sched_domains(struct work_struct *unused)
783{
784}
785
Geert Uytterhoevene1b80902009-12-06 20:41:16 +0100786static int generate_sched_domains(cpumask_var_t **domains,
Paul Menagedb7f47c2009-04-02 16:57:55 -0700787 struct sched_domain_attr **attributes)
788{
789 *domains = NULL;
790 return 1;
791}
792#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700793
794static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
795
796/*
797 * Rebuild scheduler domains, asynchronously via workqueue.
798 *
799 * If the flag 'sched_load_balance' of any cpuset with non-empty
800 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
801 * which has that flag enabled, or if any cpuset with a non-empty
802 * 'cpus' is removed, then call this routine to rebuild the
803 * scheduler's dynamic sched domains.
804 *
805 * The rebuild_sched_domains() and partition_sched_domains()
806 * routines must nest cgroup_lock() inside get_online_cpus(),
807 * but such cpuset changes as these must nest that locking the
808 * other way, holding cgroup_lock() for much of the code.
809 *
810 * So in order to avoid an ABBA deadlock, the cpuset code handling
811 * these user changes delegates the actual sched domain rebuilding
812 * to a separate workqueue thread, which ends up processing the
813 * above do_rebuild_sched_domains() function.
814 */
815static void async_rebuild_sched_domains(void)
816{
Miao Xief90d4112009-01-16 10:24:10 +0800817 queue_work(cpuset_wq, &rebuild_sched_domains_work);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700818}
819
820/*
821 * Accomplishes the same scheduler domain rebuild as the above
822 * async_rebuild_sched_domains(), however it directly calls the
823 * rebuild routine synchronously rather than calling it via an
824 * asynchronous work thread.
825 *
826 * This can only be called from code that is not holding
827 * cgroup_mutex (not nested in a cgroup_lock() call.)
828 */
829void rebuild_sched_domains(void)
830{
831 do_rebuild_sched_domains(NULL);
Paul Jackson029190c2007-10-18 23:40:20 -0700832}
833
Cliff Wickman58f47902008-02-07 00:14:44 -0800834/**
835 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
836 * @tsk: task to test
837 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
838 *
Paul Menage2df167a2008-02-07 00:14:45 -0800839 * Call with cgroup_mutex held. May take callback_mutex during call.
Cliff Wickman58f47902008-02-07 00:14:44 -0800840 * Called for each task in a cgroup by cgroup_scan_tasks().
841 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
842 * words, if its mask is not equal to its cpuset's mask).
Paul Jackson053199e2005-10-30 15:02:30 -0800843 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700844static int cpuset_test_cpumask(struct task_struct *tsk,
845 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800846{
Li Zefan300ed6c2009-01-07 18:08:44 -0800847 return !cpumask_equal(&tsk->cpus_allowed,
Cliff Wickman58f47902008-02-07 00:14:44 -0800848 (cgroup_cs(scan->cg))->cpus_allowed);
849}
Paul Jackson053199e2005-10-30 15:02:30 -0800850
Cliff Wickman58f47902008-02-07 00:14:44 -0800851/**
852 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
853 * @tsk: task to test
854 * @scan: struct cgroup_scanner containing the cgroup of the task
855 *
856 * Called by cgroup_scan_tasks() for each task in a cgroup whose
857 * cpus_allowed mask needs to be changed.
858 *
859 * We don't need to re-check for the cgroup/cpuset membership, since we're
860 * holding cgroup_lock() at this point.
861 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700862static void cpuset_change_cpumask(struct task_struct *tsk,
863 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800864{
Li Zefan300ed6c2009-01-07 18:08:44 -0800865 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
Cliff Wickman58f47902008-02-07 00:14:44 -0800866}
867
868/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700869 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
870 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
Li Zefan4e743392008-09-13 02:33:08 -0700871 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -0700872 *
873 * Called with cgroup_mutex held
874 *
875 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
876 * calling callback functions for each.
877 *
Li Zefan4e743392008-09-13 02:33:08 -0700878 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
879 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -0700880 */
Li Zefan4e743392008-09-13 02:33:08 -0700881static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
Miao Xie0b2f6302008-07-25 01:47:21 -0700882{
883 struct cgroup_scanner scan;
Miao Xie0b2f6302008-07-25 01:47:21 -0700884
885 scan.cg = cs->css.cgroup;
886 scan.test_task = cpuset_test_cpumask;
887 scan.process_task = cpuset_change_cpumask;
Li Zefan4e743392008-09-13 02:33:08 -0700888 scan.heap = heap;
889 cgroup_scan_tasks(&scan);
Miao Xie0b2f6302008-07-25 01:47:21 -0700890}
891
892/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800893 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
894 * @cs: the cpuset to consider
895 * @buf: buffer of cpu numbers written to this cpuset
896 */
Li Zefan645fcc92009-01-07 18:08:43 -0800897static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
898 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Li Zefan4e743392008-09-13 02:33:08 -0700900 struct ptr_heap heap;
Cliff Wickman58f47902008-02-07 00:14:44 -0800901 int retval;
902 int is_load_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Rusty Russell5f054e32012-03-29 15:38:31 +1030904 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700905 if (cs == &top_cpuset)
906 return -EACCES;
907
David Rientjes6f7f02e2007-05-08 00:31:43 -0700908 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800909 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700910 * Since cpulist_parse() fails on an empty mask, we special case
911 * that parsing. The validate_change() call ensures that cpusets
912 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700913 */
Paul Jackson020958b2007-10-18 23:40:21 -0700914 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800915 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700916 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -0800917 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700918 if (retval < 0)
919 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700920
Peter Zijlstra6ad4c182009-11-25 13:31:39 +0100921 if (!cpumask_subset(trialcs->cpus_allowed, cpu_active_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700922 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700923 }
Li Zefan645fcc92009-01-07 18:08:43 -0800924 retval = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700925 if (retval < 0)
926 return retval;
Paul Jackson029190c2007-10-18 23:40:20 -0700927
Paul Menage8707d8b2007-10-18 23:40:22 -0700928 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800929 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700930 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800931
Li Zefan4e743392008-09-13 02:33:08 -0700932 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
933 if (retval)
934 return retval;
935
Li Zefan645fcc92009-01-07 18:08:43 -0800936 is_load_balanced = is_sched_load_balance(trialcs);
Paul Jackson029190c2007-10-18 23:40:20 -0700937
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800938 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -0800939 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800940 mutex_unlock(&callback_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700941
Paul Menage8707d8b2007-10-18 23:40:22 -0700942 /*
943 * Scan tasks in the cpuset, and update the cpumasks of any
Cliff Wickman58f47902008-02-07 00:14:44 -0800944 * that need an update.
Paul Menage8707d8b2007-10-18 23:40:22 -0700945 */
Li Zefan4e743392008-09-13 02:33:08 -0700946 update_tasks_cpumask(cs, &heap);
947
948 heap_free(&heap);
Cliff Wickman58f47902008-02-07 00:14:44 -0800949
Paul Menage8707d8b2007-10-18 23:40:22 -0700950 if (is_load_balanced)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700951 async_rebuild_sched_domains();
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Paul Jackson053199e2005-10-30 15:02:30 -0800955/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800956 * cpuset_migrate_mm
957 *
958 * Migrate memory region from one set of nodes to another.
959 *
960 * Temporarilly set tasks mems_allowed to target nodes of migration,
961 * so that the migration code can allocate pages on these nodes.
962 *
Paul Menage2df167a2008-02-07 00:14:45 -0800963 * Call holding cgroup_mutex, so current's cpuset won't change
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800964 * during this call, as manage_mutex holds off any cpuset_attach()
Paul Jacksone4e364e2006-03-31 02:30:52 -0800965 * calls. Therefore we don't need to take task_lock around the
966 * call to guarantee_online_mems(), as we know no one is changing
Paul Menage2df167a2008-02-07 00:14:45 -0800967 * our task's cpuset.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800968 *
Paul Jacksone4e364e2006-03-31 02:30:52 -0800969 * While the mm_struct we are migrating is typically from some
970 * other task, the task_struct mems_allowed that we are hacking
971 * is for our current task, which must allocate new pages for that
972 * migrating memory region.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800973 */
974
975static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
976 const nodemask_t *to)
977{
978 struct task_struct *tsk = current;
979
Paul Jacksone4e364e2006-03-31 02:30:52 -0800980 tsk->mems_allowed = *to;
Paul Jacksone4e364e2006-03-31 02:30:52 -0800981
982 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
983
Paul Menage8793d852007-10-18 23:39:39 -0700984 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -0800985}
986
Li Zefan3b6766f2009-04-02 16:57:51 -0700987/*
Miao Xie58568d22009-06-16 15:31:49 -0700988 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
989 * @tsk: the task to change
990 * @newmems: new nodes that the task will be set
991 *
992 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
993 * we structure updates as setting all new allowed nodes, then clearing newly
994 * disallowed ones.
Miao Xie58568d22009-06-16 15:31:49 -0700995 */
996static void cpuset_change_task_nodemask(struct task_struct *tsk,
997 nodemask_t *newmems)
998{
David Rientjesb2462722011-12-19 17:11:52 -0800999 bool need_loop;
David Rientjes89e8a242011-11-02 13:38:39 -07001000
Miao Xiec0ff7452010-05-24 14:32:08 -07001001 /*
1002 * Allow tasks that have access to memory reserves because they have
1003 * been OOM killed to get memory anywhere.
1004 */
1005 if (unlikely(test_thread_flag(TIF_MEMDIE)))
1006 return;
1007 if (current->flags & PF_EXITING) /* Let dying task have memory */
1008 return;
1009
1010 task_lock(tsk);
David Rientjesb2462722011-12-19 17:11:52 -08001011 /*
1012 * Determine if a loop is necessary if another thread is doing
1013 * get_mems_allowed(). If at least one node remains unchanged and
1014 * tsk does not have a mempolicy, then an empty nodemask will not be
1015 * possible when mems_allowed is larger than a word.
1016 */
1017 need_loop = task_has_mempolicy(tsk) ||
1018 !nodes_intersects(*newmems, tsk->mems_allowed);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001019
1020 if (need_loop)
1021 write_seqcount_begin(&tsk->mems_allowed_seq);
1022
Miao Xie58568d22009-06-16 15:31:49 -07001023 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Miao Xiec0ff7452010-05-24 14:32:08 -07001024 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
1025
Miao Xiec0ff7452010-05-24 14:32:08 -07001026 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
Miao Xie58568d22009-06-16 15:31:49 -07001027 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001028
1029 if (need_loop)
1030 write_seqcount_end(&tsk->mems_allowed_seq);
1031
Miao Xiec0ff7452010-05-24 14:32:08 -07001032 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001033}
1034
1035/*
1036 * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy
1037 * of it to cpuset's new mems_allowed, and migrate pages to new nodes if
1038 * memory_migrate flag is set. Called with cgroup_mutex held.
Li Zefan3b6766f2009-04-02 16:57:51 -07001039 */
1040static void cpuset_change_nodemask(struct task_struct *p,
1041 struct cgroup_scanner *scan)
1042{
1043 struct mm_struct *mm;
1044 struct cpuset *cs;
1045 int migrate;
1046 const nodemask_t *oldmem = scan->data;
Li Zefanee24d372011-03-23 16:42:47 -07001047 static nodemask_t newmems; /* protected by cgroup_mutex */
Miao Xie58568d22009-06-16 15:31:49 -07001048
1049 cs = cgroup_cs(scan->cg);
Li Zefanee24d372011-03-23 16:42:47 -07001050 guarantee_online_mems(cs, &newmems);
Miao Xie58568d22009-06-16 15:31:49 -07001051
Li Zefanee24d372011-03-23 16:42:47 -07001052 cpuset_change_task_nodemask(p, &newmems);
Miao Xie53feb292010-03-23 13:35:35 -07001053
Li Zefan3b6766f2009-04-02 16:57:51 -07001054 mm = get_task_mm(p);
1055 if (!mm)
1056 return;
1057
Li Zefan3b6766f2009-04-02 16:57:51 -07001058 migrate = is_memory_migrate(cs);
1059
1060 mpol_rebind_mm(mm, &cs->mems_allowed);
1061 if (migrate)
1062 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1063 mmput(mm);
1064}
1065
Paul Menage8793d852007-10-18 23:39:39 -07001066static void *cpuset_being_rebound;
1067
Miao Xie0b2f6302008-07-25 01:47:21 -07001068/**
1069 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1070 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1071 * @oldmem: old mems_allowed of cpuset cs
Li Zefan010cfac2009-04-02 16:57:52 -07001072 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -07001073 *
1074 * Called with cgroup_mutex held
Li Zefan010cfac2009-04-02 16:57:52 -07001075 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1076 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -07001077 */
Li Zefan010cfac2009-04-02 16:57:52 -07001078static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1079 struct ptr_heap *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Li Zefan3b6766f2009-04-02 16:57:51 -07001081 struct cgroup_scanner scan;
Paul Jackson59dac162006-01-08 01:01:52 -08001082
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001083 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001084
Li Zefan3b6766f2009-04-02 16:57:51 -07001085 scan.cg = cs->css.cgroup;
1086 scan.test_task = NULL;
1087 scan.process_task = cpuset_change_nodemask;
Li Zefan010cfac2009-04-02 16:57:52 -07001088 scan.heap = heap;
Li Zefan3b6766f2009-04-02 16:57:51 -07001089 scan.data = (nodemask_t *)oldmem;
Paul Jackson42253992006-01-08 01:01:59 -08001090
1091 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001092 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1093 * take while holding tasklist_lock. Forks can happen - the
1094 * mpol_dup() cpuset_being_rebound check will catch such forks,
1095 * and rebind their vma mempolicies too. Because we still hold
1096 * the global cgroup_mutex, we know that no other rebind effort
1097 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001098 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001099 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001100 */
Li Zefan010cfac2009-04-02 16:57:52 -07001101 cgroup_scan_tasks(&scan);
Paul Jackson42253992006-01-08 01:01:59 -08001102
Paul Menage2df167a2008-02-07 00:14:45 -08001103 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001104 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Miao Xie0b2f6302008-07-25 01:47:21 -07001107/*
1108 * Handle user request to change the 'mems' memory placement
1109 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001110 * cpusets mems_allowed, and for each task in the cpuset,
1111 * update mems_allowed and rebind task's mempolicy and any vma
1112 * mempolicies and if the cpuset is marked 'memory_migrate',
1113 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001114 *
1115 * Call with cgroup_mutex held. May take callback_mutex during call.
1116 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1117 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1118 * their mempolicies to the cpusets new mems_allowed.
1119 */
Li Zefan645fcc92009-01-07 18:08:43 -08001120static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1121 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001122{
Miao Xie53feb292010-03-23 13:35:35 -07001123 NODEMASK_ALLOC(nodemask_t, oldmem, GFP_KERNEL);
Miao Xie0b2f6302008-07-25 01:47:21 -07001124 int retval;
Li Zefan010cfac2009-04-02 16:57:52 -07001125 struct ptr_heap heap;
Miao Xie0b2f6302008-07-25 01:47:21 -07001126
Miao Xie53feb292010-03-23 13:35:35 -07001127 if (!oldmem)
1128 return -ENOMEM;
1129
Miao Xie0b2f6302008-07-25 01:47:21 -07001130 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001131 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001132 * it's read-only
1133 */
Miao Xie53feb292010-03-23 13:35:35 -07001134 if (cs == &top_cpuset) {
1135 retval = -EACCES;
1136 goto done;
1137 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001138
Miao Xie0b2f6302008-07-25 01:47:21 -07001139 /*
1140 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1141 * Since nodelist_parse() fails on an empty mask, we special case
1142 * that parsing. The validate_change() call ensures that cpusets
1143 * with tasks have memory.
1144 */
1145 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001146 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001147 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001148 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001149 if (retval < 0)
1150 goto done;
1151
Li Zefan645fcc92009-01-07 18:08:43 -08001152 if (!nodes_subset(trialcs->mems_allowed,
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001153 node_states[N_MEMORY])) {
Miao Xie53feb292010-03-23 13:35:35 -07001154 retval = -EINVAL;
1155 goto done;
1156 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001157 }
Miao Xie53feb292010-03-23 13:35:35 -07001158 *oldmem = cs->mems_allowed;
1159 if (nodes_equal(*oldmem, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001160 retval = 0; /* Too easy - nothing to do */
1161 goto done;
1162 }
Li Zefan645fcc92009-01-07 18:08:43 -08001163 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001164 if (retval < 0)
1165 goto done;
1166
Li Zefan010cfac2009-04-02 16:57:52 -07001167 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1168 if (retval < 0)
1169 goto done;
1170
Miao Xie0b2f6302008-07-25 01:47:21 -07001171 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001172 cs->mems_allowed = trialcs->mems_allowed;
Miao Xie0b2f6302008-07-25 01:47:21 -07001173 mutex_unlock(&callback_mutex);
1174
Miao Xie53feb292010-03-23 13:35:35 -07001175 update_tasks_nodemask(cs, oldmem, &heap);
Li Zefan010cfac2009-04-02 16:57:52 -07001176
1177 heap_free(&heap);
Miao Xie0b2f6302008-07-25 01:47:21 -07001178done:
Miao Xie53feb292010-03-23 13:35:35 -07001179 NODEMASK_FREE(oldmem);
Miao Xie0b2f6302008-07-25 01:47:21 -07001180 return retval;
1181}
1182
Paul Menage8793d852007-10-18 23:39:39 -07001183int current_cpuset_is_being_rebound(void)
1184{
1185 return task_cs(current) == cpuset_being_rebound;
1186}
1187
Paul Menage5be7a472008-05-06 20:42:41 -07001188static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001189{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001190#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001191 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001192 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001193#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001194
1195 if (val != cs->relax_domain_level) {
1196 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001197 if (!cpumask_empty(cs->cpus_allowed) &&
1198 is_sched_load_balance(cs))
Max Krasnyanskycf417142008-08-11 14:33:53 -07001199 async_rebuild_sched_domains();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001200 }
1201
1202 return 0;
1203}
1204
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001205/*
Miao Xie950592f2009-06-16 15:31:47 -07001206 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1207 * @tsk: task to be updated
1208 * @scan: struct cgroup_scanner containing the cgroup of the task
1209 *
1210 * Called by cgroup_scan_tasks() for each task in a cgroup.
1211 *
1212 * We don't need to re-check for the cgroup/cpuset membership, since we're
1213 * holding cgroup_lock() at this point.
1214 */
1215static void cpuset_change_flag(struct task_struct *tsk,
1216 struct cgroup_scanner *scan)
1217{
1218 cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk);
1219}
1220
1221/*
1222 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1223 * @cs: the cpuset in which each task's spread flags needs to be changed
1224 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
1225 *
1226 * Called with cgroup_mutex held
1227 *
1228 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1229 * calling callback functions for each.
1230 *
1231 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1232 * if @heap != NULL.
1233 */
1234static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap)
1235{
1236 struct cgroup_scanner scan;
1237
1238 scan.cg = cs->css.cgroup;
1239 scan.test_task = NULL;
1240 scan.process_task = cpuset_change_flag;
1241 scan.heap = heap;
1242 cgroup_scan_tasks(&scan);
1243}
1244
1245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001247 * bit: the bit to update (see cpuset_flagbits_t)
1248 * cs: the cpuset to update
1249 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001250 *
Paul Menage2df167a2008-02-07 00:14:45 -08001251 * Call with cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
1253
Paul Menage700fe1a2008-04-29 01:00:00 -07001254static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1255 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Li Zefan645fcc92009-01-07 18:08:43 -08001257 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001258 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001259 int spread_flag_changed;
1260 struct ptr_heap heap;
1261 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Li Zefan645fcc92009-01-07 18:08:43 -08001263 trialcs = alloc_trial_cpuset(cs);
1264 if (!trialcs)
1265 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Li Zefan645fcc92009-01-07 18:08:43 -08001267 if (turning_on)
1268 set_bit(bit, &trialcs->flags);
1269 else
1270 clear_bit(bit, &trialcs->flags);
1271
1272 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001273 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001274 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001275
Miao Xie950592f2009-06-16 15:31:47 -07001276 err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1277 if (err < 0)
1278 goto out;
1279
Paul Jackson029190c2007-10-18 23:40:20 -07001280 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001281 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001282
Miao Xie950592f2009-06-16 15:31:47 -07001283 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1284 || (is_spread_page(cs) != is_spread_page(trialcs)));
1285
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001286 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001287 cs->flags = trialcs->flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001288 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001289
Li Zefan300ed6c2009-01-07 18:08:44 -08001290 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Max Krasnyanskycf417142008-08-11 14:33:53 -07001291 async_rebuild_sched_domains();
Paul Jackson029190c2007-10-18 23:40:20 -07001292
Miao Xie950592f2009-06-16 15:31:47 -07001293 if (spread_flag_changed)
1294 update_tasks_flags(cs, &heap);
1295 heap_free(&heap);
Li Zefan645fcc92009-01-07 18:08:43 -08001296out:
1297 free_trial_cpuset(trialcs);
1298 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Paul Jackson053199e2005-10-30 15:02:30 -08001301/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001302 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001303 *
1304 * These routines manage a digitally filtered, constant time based,
1305 * event frequency meter. There are four routines:
1306 * fmeter_init() - initialize a frequency meter.
1307 * fmeter_markevent() - called each time the event happens.
1308 * fmeter_getrate() - returns the recent rate of such events.
1309 * fmeter_update() - internal routine used to update fmeter.
1310 *
1311 * A common data structure is passed to each of these routines,
1312 * which is used to keep track of the state required to manage the
1313 * frequency meter and its digital filter.
1314 *
1315 * The filter works on the number of events marked per unit time.
1316 * The filter is single-pole low-pass recursive (IIR). The time unit
1317 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1318 * simulate 3 decimal digits of precision (multiplied by 1000).
1319 *
1320 * With an FM_COEF of 933, and a time base of 1 second, the filter
1321 * has a half-life of 10 seconds, meaning that if the events quit
1322 * happening, then the rate returned from the fmeter_getrate()
1323 * will be cut in half each 10 seconds, until it converges to zero.
1324 *
1325 * It is not worth doing a real infinitely recursive filter. If more
1326 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1327 * just compute FM_MAXTICKS ticks worth, by which point the level
1328 * will be stable.
1329 *
1330 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1331 * arithmetic overflow in the fmeter_update() routine.
1332 *
1333 * Given the simple 32 bit integer arithmetic used, this meter works
1334 * best for reporting rates between one per millisecond (msec) and
1335 * one per 32 (approx) seconds. At constant rates faster than one
1336 * per msec it maxes out at values just under 1,000,000. At constant
1337 * rates between one per msec, and one per second it will stabilize
1338 * to a value N*1000, where N is the rate of events per second.
1339 * At constant rates between one per second and one per 32 seconds,
1340 * it will be choppy, moving up on the seconds that have an event,
1341 * and then decaying until the next event. At rates slower than
1342 * about one in 32 seconds, it decays all the way back to zero between
1343 * each event.
1344 */
1345
1346#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1347#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1348#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1349#define FM_SCALE 1000 /* faux fixed point scale */
1350
1351/* Initialize a frequency meter */
1352static void fmeter_init(struct fmeter *fmp)
1353{
1354 fmp->cnt = 0;
1355 fmp->val = 0;
1356 fmp->time = 0;
1357 spin_lock_init(&fmp->lock);
1358}
1359
1360/* Internal meter update - process cnt events and update value */
1361static void fmeter_update(struct fmeter *fmp)
1362{
1363 time_t now = get_seconds();
1364 time_t ticks = now - fmp->time;
1365
1366 if (ticks == 0)
1367 return;
1368
1369 ticks = min(FM_MAXTICKS, ticks);
1370 while (ticks-- > 0)
1371 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1372 fmp->time = now;
1373
1374 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1375 fmp->cnt = 0;
1376}
1377
1378/* Process any previous ticks, then bump cnt by one (times scale). */
1379static void fmeter_markevent(struct fmeter *fmp)
1380{
1381 spin_lock(&fmp->lock);
1382 fmeter_update(fmp);
1383 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1384 spin_unlock(&fmp->lock);
1385}
1386
1387/* Process any previous ticks, then return current value. */
1388static int fmeter_getrate(struct fmeter *fmp)
1389{
1390 int val;
1391
1392 spin_lock(&fmp->lock);
1393 fmeter_update(fmp);
1394 val = fmp->val;
1395 spin_unlock(&fmp->lock);
1396 return val;
1397}
1398
Paul Menage2df167a2008-02-07 00:14:45 -08001399/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
Li Zefan761b3ef2012-01-31 13:47:36 +08001400static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07001401{
Tejun Heo2f7ee562011-12-12 18:12:21 -08001402 struct cpuset *cs = cgroup_cs(cgrp);
Tejun Heobb9d97b2011-12-12 18:12:21 -08001403 struct task_struct *task;
1404 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07001405
Ben Blumbe367d02009-09-23 15:56:31 -07001406 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
1407 return -ENOSPC;
1408
Tejun Heobb9d97b2011-12-12 18:12:21 -08001409 cgroup_taskset_for_each(task, cgrp, tset) {
1410 /*
1411 * Kthreads bound to specific cpus cannot be moved to a new
1412 * cpuset; we cannot change their cpu affinity and
1413 * isolating such threads by their set of allowed nodes is
1414 * unnecessary. Thus, cpusets are not applicable for such
1415 * threads. This prevents checking for success of
1416 * set_cpus_allowed_ptr() on all attached tasks before
1417 * cpus_allowed may be changed.
1418 */
1419 if (task->flags & PF_THREAD_BOUND)
1420 return -EINVAL;
1421 if ((ret = security_task_setscheduler(task)))
1422 return ret;
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001425 return 0;
1426}
1427
1428/*
1429 * Protected by cgroup_mutex. cpus_attach is used only by cpuset_attach()
1430 * but we can't allocate it dynamically there. Define it global and
1431 * allocate from cpuset_init().
1432 */
1433static cpumask_var_t cpus_attach;
1434
1435static void cpuset_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
1436{
1437 /* static bufs protected by cgroup_mutex */
1438 static nodemask_t cpuset_attach_nodemask_from;
1439 static nodemask_t cpuset_attach_nodemask_to;
1440 struct mm_struct *mm;
1441 struct task_struct *task;
1442 struct task_struct *leader = cgroup_taskset_first(tset);
1443 struct cgroup *oldcgrp = cgroup_taskset_cur_cgroup(tset);
1444 struct cpuset *cs = cgroup_cs(cgrp);
1445 struct cpuset *oldcs = cgroup_cs(oldcgrp);
1446
Tejun Heo94196f52011-12-12 18:12:22 -08001447 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07001448 if (cs == &top_cpuset)
1449 cpumask_copy(cpus_attach, cpu_possible_mask);
1450 else
1451 guarantee_online_cpus(cs, cpus_attach);
1452
1453 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08001454
Tejun Heobb9d97b2011-12-12 18:12:21 -08001455 cgroup_taskset_for_each(task, cgrp, tset) {
1456 /*
1457 * can_attach beforehand should guarantee that this doesn't
1458 * fail. TODO: have a better way to handle failure here
1459 */
1460 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1461
1462 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1463 cpuset_update_task_spread_flag(cs, task);
1464 }
David Quigley22fb52d2006-06-23 02:04:00 -07001465
Ben Blumf780bdb2011-05-26 16:25:19 -07001466 /*
1467 * Change mm, possibly for multiple threads in a threadgroup. This is
1468 * expensive and may sleep.
1469 */
1470 cpuset_attach_nodemask_from = oldcs->mems_allowed;
1471 cpuset_attach_nodemask_to = cs->mems_allowed;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001472 mm = get_task_mm(leader);
Paul Jackson42253992006-01-08 01:01:59 -08001473 if (mm) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001474 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
Paul Jackson2741a552006-03-31 02:30:51 -08001475 if (is_memory_migrate(cs))
Ben Blumf780bdb2011-05-26 16:25:19 -07001476 cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from,
1477 &cpuset_attach_nodemask_to);
Paul Jackson42253992006-01-08 01:01:59 -08001478 mmput(mm);
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/* The various types of files and directories in a cpuset file system */
1483
1484typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001485 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 FILE_CPULIST,
1487 FILE_MEMLIST,
1488 FILE_CPU_EXCLUSIVE,
1489 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001490 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001491 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001492 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001493 FILE_MEMORY_PRESSURE_ENABLED,
1494 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001495 FILE_SPREAD_PAGE,
1496 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497} cpuset_filetype_t;
1498
Paul Menage700fe1a2008-04-29 01:00:00 -07001499static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1500{
1501 int retval = 0;
1502 struct cpuset *cs = cgroup_cs(cgrp);
1503 cpuset_filetype_t type = cft->private;
1504
Paul Menagee3712392008-07-25 01:47:02 -07001505 if (!cgroup_lock_live_group(cgrp))
Paul Menage700fe1a2008-04-29 01:00:00 -07001506 return -ENODEV;
Paul Menage700fe1a2008-04-29 01:00:00 -07001507
1508 switch (type) {
1509 case FILE_CPU_EXCLUSIVE:
1510 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1511 break;
1512 case FILE_MEM_EXCLUSIVE:
1513 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1514 break;
Paul Menage78608362008-04-29 01:00:26 -07001515 case FILE_MEM_HARDWALL:
1516 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1517 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001518 case FILE_SCHED_LOAD_BALANCE:
1519 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1520 break;
1521 case FILE_MEMORY_MIGRATE:
1522 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1523 break;
1524 case FILE_MEMORY_PRESSURE_ENABLED:
1525 cpuset_memory_pressure_enabled = !!val;
1526 break;
1527 case FILE_MEMORY_PRESSURE:
1528 retval = -EACCES;
1529 break;
1530 case FILE_SPREAD_PAGE:
1531 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001532 break;
1533 case FILE_SPREAD_SLAB:
1534 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001535 break;
1536 default:
1537 retval = -EINVAL;
1538 break;
1539 }
1540 cgroup_unlock();
1541 return retval;
1542}
1543
Paul Menage5be7a472008-05-06 20:42:41 -07001544static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1545{
1546 int retval = 0;
1547 struct cpuset *cs = cgroup_cs(cgrp);
1548 cpuset_filetype_t type = cft->private;
1549
Paul Menagee3712392008-07-25 01:47:02 -07001550 if (!cgroup_lock_live_group(cgrp))
Paul Menage5be7a472008-05-06 20:42:41 -07001551 return -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001552
Paul Menage5be7a472008-05-06 20:42:41 -07001553 switch (type) {
1554 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1555 retval = update_relax_domain_level(cs, val);
1556 break;
1557 default:
1558 retval = -EINVAL;
1559 break;
1560 }
1561 cgroup_unlock();
1562 return retval;
1563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*
Paul Menagee3712392008-07-25 01:47:02 -07001566 * Common handling for a write to a "cpus" or "mems" file.
1567 */
1568static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1569 const char *buf)
1570{
1571 int retval = 0;
Li Zefan645fcc92009-01-07 18:08:43 -08001572 struct cpuset *cs = cgroup_cs(cgrp);
1573 struct cpuset *trialcs;
Paul Menagee3712392008-07-25 01:47:02 -07001574
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001575 /*
1576 * CPU or memory hotunplug may leave @cs w/o any execution
1577 * resources, in which case the hotplug code asynchronously updates
1578 * configuration and transfers all tasks to the nearest ancestor
1579 * which can execute.
1580 *
1581 * As writes to "cpus" or "mems" may restore @cs's execution
1582 * resources, wait for the previously scheduled operations before
1583 * proceeding, so that we don't end up keep removing tasks added
1584 * after execution capability is restored.
1585 */
1586 flush_work(&cpuset_hotplug_work);
1587
Paul Menagee3712392008-07-25 01:47:02 -07001588 if (!cgroup_lock_live_group(cgrp))
1589 return -ENODEV;
1590
Li Zefan645fcc92009-01-07 18:08:43 -08001591 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001592 if (!trialcs) {
1593 retval = -ENOMEM;
1594 goto out;
1595 }
Li Zefan645fcc92009-01-07 18:08:43 -08001596
Paul Menagee3712392008-07-25 01:47:02 -07001597 switch (cft->private) {
1598 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001599 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001600 break;
1601 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001602 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001603 break;
1604 default:
1605 retval = -EINVAL;
1606 break;
1607 }
Li Zefan645fcc92009-01-07 18:08:43 -08001608
1609 free_trial_cpuset(trialcs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001610out:
Paul Menagee3712392008-07-25 01:47:02 -07001611 cgroup_unlock();
1612 return retval;
1613}
1614
1615/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * These ascii lists should be read in a single call, by using a user
1617 * buffer large enough to hold the entire map. If read in smaller
1618 * chunks, there is no guarantee of atomicity. Since the display format
1619 * used, list of ranges of sequential numbers, is variable length,
1620 * and since these maps can change value dynamically, one could read
1621 * gibberish by doing partial reads while a list was changing.
1622 * A single large read to a buffer that crosses a page boundary is
1623 * ok, because the result being copied to user land is not recomputed
1624 * across a page fault.
1625 */
1626
Li Zefan9303e0c2011-03-23 16:42:45 -07001627static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Li Zefan9303e0c2011-03-23 16:42:45 -07001629 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001631 mutex_lock(&callback_mutex);
Li Zefan9303e0c2011-03-23 16:42:45 -07001632 count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001633 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Li Zefan9303e0c2011-03-23 16:42:45 -07001635 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Li Zefan9303e0c2011-03-23 16:42:45 -07001638static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Li Zefan9303e0c2011-03-23 16:42:45 -07001640 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001642 mutex_lock(&callback_mutex);
Li Zefan9303e0c2011-03-23 16:42:45 -07001643 count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001644 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Li Zefan9303e0c2011-03-23 16:42:45 -07001646 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Paul Menage8793d852007-10-18 23:39:39 -07001649static ssize_t cpuset_common_file_read(struct cgroup *cont,
1650 struct cftype *cft,
1651 struct file *file,
1652 char __user *buf,
1653 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Paul Menage8793d852007-10-18 23:39:39 -07001655 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 cpuset_filetype_t type = cft->private;
1657 char *page;
1658 ssize_t retval = 0;
1659 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Mel Gormane12ba742007-10-16 01:25:52 -07001661 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return -ENOMEM;
1663
1664 s = page;
1665
1666 switch (type) {
1667 case FILE_CPULIST:
1668 s += cpuset_sprintf_cpulist(s, cs);
1669 break;
1670 case FILE_MEMLIST:
1671 s += cpuset_sprintf_memlist(s, cs);
1672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 default:
1674 retval = -EINVAL;
1675 goto out;
1676 }
1677 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Al Viroeacaa1f2005-09-30 03:26:43 +01001679 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680out:
1681 free_page((unsigned long)page);
1682 return retval;
1683}
1684
Paul Menage700fe1a2008-04-29 01:00:00 -07001685static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1686{
1687 struct cpuset *cs = cgroup_cs(cont);
1688 cpuset_filetype_t type = cft->private;
1689 switch (type) {
1690 case FILE_CPU_EXCLUSIVE:
1691 return is_cpu_exclusive(cs);
1692 case FILE_MEM_EXCLUSIVE:
1693 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001694 case FILE_MEM_HARDWALL:
1695 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001696 case FILE_SCHED_LOAD_BALANCE:
1697 return is_sched_load_balance(cs);
1698 case FILE_MEMORY_MIGRATE:
1699 return is_memory_migrate(cs);
1700 case FILE_MEMORY_PRESSURE_ENABLED:
1701 return cpuset_memory_pressure_enabled;
1702 case FILE_MEMORY_PRESSURE:
1703 return fmeter_getrate(&cs->fmeter);
1704 case FILE_SPREAD_PAGE:
1705 return is_spread_page(cs);
1706 case FILE_SPREAD_SLAB:
1707 return is_spread_slab(cs);
1708 default:
1709 BUG();
1710 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001711
1712 /* Unreachable but makes gcc happy */
1713 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Paul Menage5be7a472008-05-06 20:42:41 -07001716static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1717{
1718 struct cpuset *cs = cgroup_cs(cont);
1719 cpuset_filetype_t type = cft->private;
1720 switch (type) {
1721 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1722 return cs->relax_domain_level;
1723 default:
1724 BUG();
1725 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001726
1727 /* Unrechable but makes gcc happy */
1728 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732/*
1733 * for the common functions, 'private' gives the type of file
1734 */
1735
Paul Menageaddf2c72008-04-29 01:00:26 -07001736static struct cftype files[] = {
1737 {
1738 .name = "cpus",
1739 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001740 .write_string = cpuset_write_resmask,
1741 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001742 .private = FILE_CPULIST,
1743 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Paul Menageaddf2c72008-04-29 01:00:26 -07001745 {
1746 .name = "mems",
1747 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001748 .write_string = cpuset_write_resmask,
1749 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001750 .private = FILE_MEMLIST,
1751 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Paul Menageaddf2c72008-04-29 01:00:26 -07001753 {
1754 .name = "cpu_exclusive",
1755 .read_u64 = cpuset_read_u64,
1756 .write_u64 = cpuset_write_u64,
1757 .private = FILE_CPU_EXCLUSIVE,
1758 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Paul Menageaddf2c72008-04-29 01:00:26 -07001760 {
1761 .name = "mem_exclusive",
1762 .read_u64 = cpuset_read_u64,
1763 .write_u64 = cpuset_write_u64,
1764 .private = FILE_MEM_EXCLUSIVE,
1765 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Paul Menageaddf2c72008-04-29 01:00:26 -07001767 {
Paul Menage78608362008-04-29 01:00:26 -07001768 .name = "mem_hardwall",
1769 .read_u64 = cpuset_read_u64,
1770 .write_u64 = cpuset_write_u64,
1771 .private = FILE_MEM_HARDWALL,
1772 },
1773
1774 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001775 .name = "sched_load_balance",
1776 .read_u64 = cpuset_read_u64,
1777 .write_u64 = cpuset_write_u64,
1778 .private = FILE_SCHED_LOAD_BALANCE,
1779 },
Paul Jackson029190c2007-10-18 23:40:20 -07001780
Paul Menageaddf2c72008-04-29 01:00:26 -07001781 {
1782 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001783 .read_s64 = cpuset_read_s64,
1784 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001785 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1786 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001787
Paul Menageaddf2c72008-04-29 01:00:26 -07001788 {
1789 .name = "memory_migrate",
1790 .read_u64 = cpuset_read_u64,
1791 .write_u64 = cpuset_write_u64,
1792 .private = FILE_MEMORY_MIGRATE,
1793 },
1794
1795 {
1796 .name = "memory_pressure",
1797 .read_u64 = cpuset_read_u64,
1798 .write_u64 = cpuset_write_u64,
1799 .private = FILE_MEMORY_PRESSURE,
Li Zefan099fca32009-04-02 16:57:29 -07001800 .mode = S_IRUGO,
Paul Menageaddf2c72008-04-29 01:00:26 -07001801 },
1802
1803 {
1804 .name = "memory_spread_page",
1805 .read_u64 = cpuset_read_u64,
1806 .write_u64 = cpuset_write_u64,
1807 .private = FILE_SPREAD_PAGE,
1808 },
1809
1810 {
1811 .name = "memory_spread_slab",
1812 .read_u64 = cpuset_read_u64,
1813 .write_u64 = cpuset_write_u64,
1814 .private = FILE_SPREAD_SLAB,
1815 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07001816
1817 {
1818 .name = "memory_pressure_enabled",
1819 .flags = CFTYPE_ONLY_ON_ROOT,
1820 .read_u64 = cpuset_read_u64,
1821 .write_u64 = cpuset_write_u64,
1822 .private = FILE_MEMORY_PRESSURE_ENABLED,
1823 },
1824
1825 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001826};
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/*
Tejun Heo92fb9742012-11-19 08:13:38 -08001829 * cpuset_css_alloc - allocate a cpuset css
Paul Menage2df167a2008-02-07 00:14:45 -08001830 * cont: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
1832
Tejun Heo92fb9742012-11-19 08:13:38 -08001833static struct cgroup_subsys_state *cpuset_css_alloc(struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Tejun Heoc8f699b2013-01-07 08:51:07 -08001835 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Tejun Heoc8f699b2013-01-07 08:51:07 -08001837 if (!cont->parent)
Paul Menage8793d852007-10-18 23:39:39 -07001838 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001839
Tejun Heoc8f699b2013-01-07 08:51:07 -08001840 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001842 return ERR_PTR(-ENOMEM);
Li Zefan300ed6c2009-01-07 18:08:44 -08001843 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1844 kfree(cs);
1845 return ERR_PTR(-ENOMEM);
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Paul Jackson029190c2007-10-18 23:40:20 -07001848 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001849 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001850 nodes_clear(cs->mems_allowed);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001851 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001852 cs->relax_domain_level = -1;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001853 cs->parent = cgroup_cs(cont->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Tejun Heoc8f699b2013-01-07 08:51:07 -08001855 return &cs->css;
1856}
1857
1858static int cpuset_css_online(struct cgroup *cgrp)
1859{
1860 struct cpuset *cs = cgroup_cs(cgrp);
1861 struct cpuset *parent = cs->parent;
Tejun Heoae8086c2013-01-07 08:51:07 -08001862 struct cpuset *tmp_cs;
1863 struct cgroup *pos_cg;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001864
1865 if (!parent)
1866 return 0;
1867
Tejun Heoefeb77b2013-01-07 08:51:07 -08001868 set_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001869 if (is_spread_page(parent))
1870 set_bit(CS_SPREAD_PAGE, &cs->flags);
1871 if (is_spread_slab(parent))
1872 set_bit(CS_SPREAD_SLAB, &cs->flags);
1873
Paul Jackson202f72d2006-01-08 01:01:57 -08001874 number_of_cpusets++;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001875
Tejun Heoc8f699b2013-01-07 08:51:07 -08001876 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags))
1877 return 0;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001878
1879 /*
1880 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
1881 * set. This flag handling is implemented in cgroup core for
1882 * histrical reasons - the flag may be specified during mount.
1883 *
1884 * Currently, if any sibling cpusets have exclusive cpus or mem, we
1885 * refuse to clone the configuration - thereby refusing the task to
1886 * be entered, and as a result refusing the sys_unshare() or
1887 * clone() which initiated it. If this becomes a problem for some
1888 * users who wish to allow that scenario, then this could be
1889 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
1890 * (and likewise for mems) to the new cgroup.
1891 */
Tejun Heoae8086c2013-01-07 08:51:07 -08001892 rcu_read_lock();
1893 cpuset_for_each_child(tmp_cs, pos_cg, parent) {
1894 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
1895 rcu_read_unlock();
Tejun Heoc8f699b2013-01-07 08:51:07 -08001896 return 0;
Tejun Heoae8086c2013-01-07 08:51:07 -08001897 }
Tejun Heo033fa1c2012-11-19 08:13:39 -08001898 }
Tejun Heoae8086c2013-01-07 08:51:07 -08001899 rcu_read_unlock();
Tejun Heo033fa1c2012-11-19 08:13:39 -08001900
1901 mutex_lock(&callback_mutex);
1902 cs->mems_allowed = parent->mems_allowed;
1903 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
1904 mutex_unlock(&callback_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001905
1906 return 0;
1907}
1908
1909static void cpuset_css_offline(struct cgroup *cgrp)
1910{
1911 struct cpuset *cs = cgroup_cs(cgrp);
1912
1913 /* css_offline is called w/o cgroup_mutex, grab it */
1914 cgroup_lock();
1915
1916 if (is_sched_load_balance(cs))
1917 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
1918
1919 number_of_cpusets--;
Tejun Heoefeb77b2013-01-07 08:51:07 -08001920 clear_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001921
1922 cgroup_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Paul Jackson029190c2007-10-18 23:40:20 -07001925/*
Paul Jackson029190c2007-10-18 23:40:20 -07001926 * If the cpuset being removed has its flag 'sched_load_balance'
1927 * enabled, then simulate turning sched_load_balance off, which
Max Krasnyanskycf417142008-08-11 14:33:53 -07001928 * will call async_rebuild_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -07001929 */
1930
Tejun Heo92fb9742012-11-19 08:13:38 -08001931static void cpuset_css_free(struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Paul Menage8793d852007-10-18 23:39:39 -07001933 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Li Zefan300ed6c2009-01-07 18:08:44 -08001935 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001936 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
Paul Menage8793d852007-10-18 23:39:39 -07001939struct cgroup_subsys cpuset_subsys = {
1940 .name = "cpuset",
Tejun Heo92fb9742012-11-19 08:13:38 -08001941 .css_alloc = cpuset_css_alloc,
Tejun Heoc8f699b2013-01-07 08:51:07 -08001942 .css_online = cpuset_css_online,
1943 .css_offline = cpuset_css_offline,
Tejun Heo92fb9742012-11-19 08:13:38 -08001944 .css_free = cpuset_css_free,
Paul Menage8793d852007-10-18 23:39:39 -07001945 .can_attach = cpuset_can_attach,
1946 .attach = cpuset_attach,
Paul Menage8793d852007-10-18 23:39:39 -07001947 .subsys_id = cpuset_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07001948 .base_cftypes = files,
Paul Menage8793d852007-10-18 23:39:39 -07001949 .early_init = 1,
1950};
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952/**
1953 * cpuset_init - initialize cpusets at system boot
1954 *
1955 * Description: Initialize top_cpuset and the cpuset internal file system,
1956 **/
1957
1958int __init cpuset_init(void)
1959{
Paul Menage8793d852007-10-18 23:39:39 -07001960 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Miao Xie58568d22009-06-16 15:31:49 -07001962 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
1963 BUG();
1964
Li Zefan300ed6c2009-01-07 18:08:44 -08001965 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001966 nodes_setall(top_cpuset.mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001968 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07001969 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001970 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 err = register_filesystem(&cpuset_fs_type);
1973 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07001974 return err;
1975
Li Zefan2341d1b2009-01-07 18:08:42 -08001976 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1977 BUG();
1978
Paul Jackson202f72d2006-01-08 01:01:57 -08001979 number_of_cpusets = 1;
Paul Menage8793d852007-10-18 23:39:39 -07001980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Cliff Wickman956db3c2008-02-07 00:14:43 -08001983/**
1984 * cpuset_do_move_task - move a given task to another cpuset
1985 * @tsk: pointer to task_struct the task to move
1986 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1987 *
1988 * Called by cgroup_scan_tasks() for each task in a cgroup.
1989 * Return nonzero to stop the walk through the tasks.
1990 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -07001991static void cpuset_do_move_task(struct task_struct *tsk,
1992 struct cgroup_scanner *scan)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001993{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001994 struct cgroup *new_cgroup = scan->data;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001995
Li Zefan7f81b1a2009-04-02 16:57:53 -07001996 cgroup_attach_task(new_cgroup, tsk);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001997}
1998
1999/**
2000 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
2001 * @from: cpuset in which the tasks currently reside
2002 * @to: cpuset to which the tasks will be moved
2003 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002004 * Called with cgroup_mutex held
2005 * callback_mutex must not be held, as cpuset_attach() will take it.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002006 *
2007 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
2008 * calling callback functions for each.
2009 */
2010static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
2011{
Li Zefan7f81b1a2009-04-02 16:57:53 -07002012 struct cgroup_scanner scan;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002013
Li Zefan7f81b1a2009-04-02 16:57:53 -07002014 scan.cg = from->css.cgroup;
2015 scan.test_task = NULL; /* select all tasks in cgroup */
2016 scan.process_task = cpuset_do_move_task;
2017 scan.heap = NULL;
2018 scan.data = to->css.cgroup;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002019
Li Zefan7f81b1a2009-04-02 16:57:53 -07002020 if (cgroup_scan_tasks(&scan))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002021 printk(KERN_ERR "move_member_tasks_to_cpuset: "
2022 "cgroup_scan_tasks failed\n");
2023}
2024
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002025/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002026 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002027 * or memory nodes, we need to walk over the cpuset hierarchy,
2028 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002029 * last CPU or node from a cpuset, then move the tasks in the empty
2030 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002031 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002032 * Called with cgroup_mutex held
2033 * callback_mutex must not be held, as cpuset_attach() will take it.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002034 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002035static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002036{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002037 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002038
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002039 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08002040 * Find its next-highest non-empty parent, (top cpuset
2041 * has online cpus, so can't be empty).
2042 */
2043 parent = cs->parent;
Li Zefan300ed6c2009-01-07 18:08:44 -08002044 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002045 nodes_empty(parent->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002046 parent = parent->parent;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002047
2048 move_member_tasks_to_cpuset(cs, parent);
2049}
2050
2051/*
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05302052 * Helper function to traverse cpusets.
2053 * It can be used to walk the cpuset tree from top to bottom, completing
2054 * one layer before dropping down to the next (thus always processing a
2055 * node before any of its children).
2056 */
2057static struct cpuset *cpuset_next(struct list_head *queue)
2058{
2059 struct cpuset *cp;
2060 struct cpuset *child; /* scans child cpusets of cp */
2061 struct cgroup *cont;
2062
2063 if (list_empty(queue))
2064 return NULL;
2065
2066 cp = list_first_entry(queue, struct cpuset, stack_list);
2067 list_del(queue->next);
Tejun Heoae8086c2013-01-07 08:51:07 -08002068 rcu_read_lock();
2069 cpuset_for_each_child(child, cont, cp)
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05302070 list_add_tail(&child->stack_list, queue);
Tejun Heoae8086c2013-01-07 08:51:07 -08002071 rcu_read_unlock();
Srivatsa S. Bhat80d1fa62012-05-24 19:46:41 +05302072
2073 return cp;
2074}
2075
Tejun Heodeb7aa32013-01-07 08:51:07 -08002076/**
2077 * cpuset_propagate_hotplug - propagate CPU/memory hotplug to a cpuset
2078 * @cs: cpuset in interest
Cliff Wickman956db3c2008-02-07 00:14:43 -08002079 *
Tejun Heodeb7aa32013-01-07 08:51:07 -08002080 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2081 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2082 * all its tasks are moved to the nearest ancestor with both resources.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002083 *
Tejun Heodeb7aa32013-01-07 08:51:07 -08002084 * Should be called with cgroup_mutex held.
Cliff Wickman956db3c2008-02-07 00:14:43 -08002085 */
Tejun Heodeb7aa32013-01-07 08:51:07 -08002086static void cpuset_propagate_hotplug(struct cpuset *cs)
Cliff Wickman956db3c2008-02-07 00:14:43 -08002087{
Tejun Heodeb7aa32013-01-07 08:51:07 -08002088 static cpumask_t off_cpus;
2089 static nodemask_t off_mems, tmp_mems;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002090
Tejun Heodeb7aa32013-01-07 08:51:07 -08002091 WARN_ON_ONCE(!cgroup_lock_is_held());
Cliff Wickman956db3c2008-02-07 00:14:43 -08002092
Tejun Heodeb7aa32013-01-07 08:51:07 -08002093 cpumask_andnot(&off_cpus, cs->cpus_allowed, top_cpuset.cpus_allowed);
2094 nodes_andnot(off_mems, cs->mems_allowed, top_cpuset.mems_allowed);
Paul Jacksonb4501292008-02-07 00:14:47 -08002095
Tejun Heodeb7aa32013-01-07 08:51:07 -08002096 /* remove offline cpus from @cs */
2097 if (!cpumask_empty(&off_cpus)) {
2098 mutex_lock(&callback_mutex);
2099 cpumask_andnot(cs->cpus_allowed, cs->cpus_allowed, &off_cpus);
2100 mutex_unlock(&callback_mutex);
2101 update_tasks_cpumask(cs, NULL);
2102 }
Paul Jacksonb4501292008-02-07 00:14:47 -08002103
Tejun Heodeb7aa32013-01-07 08:51:07 -08002104 /* remove offline mems from @cs */
2105 if (!nodes_empty(off_mems)) {
2106 tmp_mems = cs->mems_allowed;
2107 mutex_lock(&callback_mutex);
2108 nodes_andnot(cs->mems_allowed, cs->mems_allowed, off_mems);
2109 mutex_unlock(&callback_mutex);
2110 update_tasks_nodemask(cs, &tmp_mems, NULL);
2111 }
Miao Xief9b4fb82008-07-25 01:47:22 -07002112
Tejun Heodeb7aa32013-01-07 08:51:07 -08002113 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
2114 remove_tasks_in_empty_cpuset(cs);
2115}
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302116
Tejun Heodeb7aa32013-01-07 08:51:07 -08002117/**
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002118 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
Tejun Heodeb7aa32013-01-07 08:51:07 -08002119 *
2120 * This function is called after either CPU or memory configuration has
2121 * changed and updates cpuset accordingly. The top_cpuset is always
2122 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2123 * order to make cpusets transparent (of no affect) on systems that are
2124 * actively using CPU hotplug but making no active use of cpusets.
2125 *
2126 * Non-root cpusets are only affected by offlining. If any CPUs or memory
2127 * nodes have been taken down, cpuset_propagate_hotplug() is invoked on all
2128 * descendants.
2129 *
2130 * Note that CPU offlining during suspend is ignored. We don't modify
2131 * cpusets across suspend/resume cycles at all.
2132 */
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002133static void cpuset_hotplug_workfn(struct work_struct *work)
Tejun Heodeb7aa32013-01-07 08:51:07 -08002134{
2135 static cpumask_t new_cpus, tmp_cpus;
2136 static nodemask_t new_mems, tmp_mems;
2137 bool cpus_updated, mems_updated;
2138 bool cpus_offlined, mems_offlined;
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302139
Tejun Heodeb7aa32013-01-07 08:51:07 -08002140 cgroup_lock();
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302141
Tejun Heodeb7aa32013-01-07 08:51:07 -08002142 /* fetch the available cpus/mems and find out which changed how */
2143 cpumask_copy(&new_cpus, cpu_active_mask);
2144 new_mems = node_states[N_MEMORY];
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302145
Tejun Heodeb7aa32013-01-07 08:51:07 -08002146 cpus_updated = !cpumask_equal(top_cpuset.cpus_allowed, &new_cpus);
2147 cpus_offlined = cpumask_andnot(&tmp_cpus, top_cpuset.cpus_allowed,
2148 &new_cpus);
Paul Jacksonb4501292008-02-07 00:14:47 -08002149
Tejun Heodeb7aa32013-01-07 08:51:07 -08002150 mems_updated = !nodes_equal(top_cpuset.mems_allowed, new_mems);
2151 nodes_andnot(tmp_mems, top_cpuset.mems_allowed, new_mems);
2152 mems_offlined = !nodes_empty(tmp_mems);
2153
2154 /* synchronize cpus_allowed to cpu_active_mask */
2155 if (cpus_updated) {
2156 mutex_lock(&callback_mutex);
2157 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
2158 mutex_unlock(&callback_mutex);
2159 /* we don't mess with cpumasks of tasks in top_cpuset */
2160 }
2161
2162 /* synchronize mems_allowed to N_MEMORY */
2163 if (mems_updated) {
2164 tmp_mems = top_cpuset.mems_allowed;
2165 mutex_lock(&callback_mutex);
2166 top_cpuset.mems_allowed = new_mems;
2167 mutex_unlock(&callback_mutex);
2168 update_tasks_nodemask(&top_cpuset, &tmp_mems, NULL);
2169 }
2170
2171 /* if cpus or mems went down, we need to propagate to descendants */
2172 if (cpus_offlined || mems_offlined) {
2173 struct cpuset *cs;
2174 LIST_HEAD(queue);
2175
2176 list_add_tail(&top_cpuset.stack_list, &queue);
2177 while ((cs = cpuset_next(&queue)))
2178 if (cs != &top_cpuset)
2179 cpuset_propagate_hotplug(cs);
2180 }
2181
2182 cgroup_unlock();
2183
2184 /* rebuild sched domains if cpus_allowed has changed */
2185 if (cpus_updated) {
2186 struct sched_domain_attr *attr;
2187 cpumask_var_t *doms;
2188 int ndoms;
2189
2190 cgroup_lock();
2191 ndoms = generate_sched_domains(&doms, &attr);
2192 cgroup_unlock();
2193
2194 partition_sched_domains(ndoms, doms, attr);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002195 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002196}
2197
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302198void cpuset_update_active_cpus(bool cpu_online)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002199{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002200 /*
2201 * We're inside cpu hotplug critical region which usually nests
2202 * inside cgroup synchronization. Bounce actual hotplug processing
2203 * to a work item to avoid reverse locking order.
2204 *
2205 * We still need to do partition_sched_domains() synchronously;
2206 * otherwise, the scheduler will get confused and put tasks to the
2207 * dead CPU. Fall back to the default single domain.
2208 * cpuset_hotplug_workfn() will rebuild it as necessary.
2209 */
2210 partition_sched_domains(1, NULL, NULL);
2211 schedule_work(&cpuset_hotplug_work);
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002212}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002213
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002214#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002215/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002216 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2217 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302218 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07002219 */
Miao Xief4818912008-11-19 15:36:30 -08002220static int cpuset_track_online_nodes(struct notifier_block *self,
2221 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002222{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002223 schedule_work(&cpuset_hotplug_work);
Miao Xief4818912008-11-19 15:36:30 -08002224 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002225}
2226#endif
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/**
2229 * cpuset_init_smp - initialize cpus_allowed
2230 *
2231 * Description: Finish top cpuset after cpu, node maps are initialized
2232 **/
2233
2234void __init cpuset_init_smp(void)
2235{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002236 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002237 top_cpuset.mems_allowed = node_states[N_MEMORY];
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002238
Miao Xief4818912008-11-19 15:36:30 -08002239 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
Miao Xief90d4112009-01-16 10:24:10 +08002240
2241 cpuset_wq = create_singlethread_workqueue("cpuset");
2242 BUG_ON(!cpuset_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2247 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002248 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002250 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10302252 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 * tasks cpuset.
2254 **/
2255
Li Zefan6af866a2009-01-07 18:08:45 -08002256void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002258 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002259 task_lock(tsk);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002260 guarantee_online_cpus(task_cs(tsk), pmask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002261 task_unlock(tsk);
Oleg Nesterov897f0b32010-03-15 10:10:03 +01002262 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002265void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002266{
2267 const struct cpuset *cs;
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002268
2269 rcu_read_lock();
2270 cs = task_cs(tsk);
2271 if (cs)
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09002272 do_set_cpus_allowed(tsk, cs->cpus_allowed);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002273 rcu_read_unlock();
2274
2275 /*
2276 * We own tsk->cpus_allowed, nobody can change it under us.
2277 *
2278 * But we used cs && cs->cpus_allowed lockless and thus can
2279 * race with cgroup_attach_task() or update_cpumask() and get
2280 * the wrong tsk->cpus_allowed. However, both cases imply the
2281 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2282 * which takes task_rq_lock().
2283 *
2284 * If we are called after it dropped the lock we must see all
2285 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2286 * set any mask even if it is not right from task_cs() pov,
2287 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002288 *
2289 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2290 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002291 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294void cpuset_init_current_mems_allowed(void)
2295{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002296 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002299/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002300 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2301 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2302 *
2303 * Description: Returns the nodemask_t mems_allowed of the cpuset
2304 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002305 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002306 * tasks cpuset.
2307 **/
2308
2309nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2310{
2311 nodemask_t mask;
2312
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002313 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002314 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -07002315 guarantee_online_mems(task_cs(tsk), &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002316 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002317 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002318
2319 return mask;
2320}
2321
2322/**
Mel Gorman19770b32008-04-28 02:12:18 -07002323 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2324 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002325 *
Mel Gorman19770b32008-04-28 02:12:18 -07002326 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 */
Mel Gorman19770b32008-04-28 02:12:18 -07002328int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Mel Gorman19770b32008-04-28 02:12:18 -07002330 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Paul Jackson9bf22292005-09-06 15:18:12 -07002333/*
Paul Menage78608362008-04-29 01:00:26 -07002334 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2335 * mem_hardwall ancestor to the specified cpuset. Call holding
2336 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2337 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
Paul Menage78608362008-04-29 01:00:26 -07002339static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Paul Menage78608362008-04-29 01:00:26 -07002341 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
Paul Jackson9bf22292005-09-06 15:18:12 -07002342 cs = cs->parent;
2343 return cs;
2344}
2345
2346/**
David Rientjesa1bc5a42009-04-02 16:57:54 -07002347 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2348 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002349 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002350 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002351 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2352 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2353 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2354 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2355 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2356 * flag, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002357 * Otherwise, no.
2358 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002359 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2360 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2361 * might sleep, and might allow a node from an enclosing cpuset.
Paul Jackson02a0e532006-12-13 00:34:25 -08002362 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002363 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2364 * cpusets, and never sleeps.
Paul Jackson02a0e532006-12-13 00:34:25 -08002365 *
2366 * The __GFP_THISNODE placement logic is really handled elsewhere,
2367 * by forcibly using a zonelist starting at a specified node, and by
2368 * (in get_page_from_freelist()) refusing to consider the zones for
2369 * any node on the zonelist except the first. By the time any such
2370 * calls get to this routine, we should just shut up and say 'yes'.
2371 *
Paul Jackson9bf22292005-09-06 15:18:12 -07002372 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002373 * and do not allow allocations outside the current tasks cpuset
2374 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002375 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002376 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002377 *
Paul Jackson02a0e532006-12-13 00:34:25 -08002378 * Scanning up parent cpusets requires callback_mutex. The
2379 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2380 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2381 * current tasks mems_allowed came up empty on the first pass over
2382 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2383 * cpuset are short of memory, might require taking the callback_mutex
2384 * mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002385 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002386 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002387 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2388 * so no allocation on a node outside the cpuset is allowed (unless
2389 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002390 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002391 * The second pass through get_page_from_freelist() doesn't even call
2392 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2393 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2394 * in alloc_flags. That logic and the checks below have the combined
2395 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002396 * in_interrupt - any node ok (current task context irrelevant)
2397 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002398 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002399 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002400 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002401 *
2402 * Rule:
David Rientjesa1bc5a42009-04-02 16:57:54 -07002403 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
Paul Jackson36be57f2006-05-20 15:00:10 -07002404 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2405 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson02a0e532006-12-13 00:34:25 -08002406 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002407int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002408{
Paul Jackson9bf22292005-09-06 15:18:12 -07002409 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002410 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002411
Christoph Lameter9b819d22006-09-25 23:31:40 -07002412 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002413 return 1;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002414 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002415 if (node_isset(node, current->mems_allowed))
2416 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002417 /*
2418 * Allow tasks that have access to memory reserves because they have
2419 * been OOM killed to get memory anywhere.
2420 */
2421 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2422 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002423 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2424 return 0;
2425
Bob Picco5563e772005-11-13 16:06:35 -08002426 if (current->flags & PF_EXITING) /* Let dying task have memory */
2427 return 1;
2428
Paul Jackson9bf22292005-09-06 15:18:12 -07002429 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002430 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002431
Paul Jackson053199e2005-10-30 15:02:30 -08002432 task_lock(current);
Paul Menage78608362008-04-29 01:00:26 -07002433 cs = nearest_hardwall_ancestor(task_cs(current));
Paul Jackson053199e2005-10-30 15:02:30 -08002434 task_unlock(current);
2435
Paul Jackson9bf22292005-09-06 15:18:12 -07002436 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002437 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002438 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Paul Jackson02a0e532006-12-13 00:34:25 -08002441/*
David Rientjesa1bc5a42009-04-02 16:57:54 -07002442 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2443 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002444 * @gfp_mask: memory allocation flags
2445 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002446 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2447 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2448 * yes. If the task has been OOM killed and has access to memory reserves as
2449 * specified by the TIF_MEMDIE flag, yes.
2450 * Otherwise, no.
Paul Jackson02a0e532006-12-13 00:34:25 -08002451 *
2452 * The __GFP_THISNODE placement logic is really handled elsewhere,
2453 * by forcibly using a zonelist starting at a specified node, and by
2454 * (in get_page_from_freelist()) refusing to consider the zones for
2455 * any node on the zonelist except the first. By the time any such
2456 * calls get to this routine, we should just shut up and say 'yes'.
2457 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002458 * Unlike the cpuset_node_allowed_softwall() variant, above,
2459 * this variant requires that the node be in the current task's
Paul Jackson02a0e532006-12-13 00:34:25 -08002460 * mems_allowed or that we're in interrupt. It does not scan up the
2461 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2462 * It never sleeps.
2463 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002464int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -08002465{
Paul Jackson02a0e532006-12-13 00:34:25 -08002466 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2467 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002468 if (node_isset(node, current->mems_allowed))
2469 return 1;
Daniel Walkerdedf8b72007-10-18 03:06:04 -07002470 /*
2471 * Allow tasks that have access to memory reserves because they have
2472 * been OOM killed to get memory anywhere.
2473 */
2474 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2475 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002476 return 0;
2477}
2478
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002479/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07002480 * cpuset_mem_spread_node() - On which node to begin search for a file page
2481 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08002482 *
2483 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2484 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2485 * and if the memory allocation used cpuset_mem_spread_node()
2486 * to determine on which node to start looking, as it will for
2487 * certain page cache or slab cache pages such as used for file
2488 * system buffers and inode caches, then instead of starting on the
2489 * local node to look for a free page, rather spread the starting
2490 * node around the tasks mems_allowed nodes.
2491 *
2492 * We don't have to worry about the returned node being offline
2493 * because "it can't happen", and even if it did, it would be ok.
2494 *
2495 * The routines calling guarantee_online_mems() are careful to
2496 * only set nodes in task->mems_allowed that are online. So it
2497 * should not be possible for the following code to return an
2498 * offline node. But if it did, that would be ok, as this routine
2499 * is not returning the node where the allocation must be, only
2500 * the node where the search should start. The zonelist passed to
2501 * __alloc_pages() will include all nodes. If the slab allocator
2502 * is passed an offline node, it will fall back to the local node.
2503 * See kmem_cache_alloc_node().
2504 */
2505
Jack Steiner6adef3e2010-05-26 14:42:49 -07002506static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08002507{
2508 int node;
2509
Jack Steiner6adef3e2010-05-26 14:42:49 -07002510 node = next_node(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08002511 if (node == MAX_NUMNODES)
2512 node = first_node(current->mems_allowed);
Jack Steiner6adef3e2010-05-26 14:42:49 -07002513 *rotor = node;
Paul Jackson825a46a2006-03-24 03:16:03 -08002514 return node;
2515}
Jack Steiner6adef3e2010-05-26 14:42:49 -07002516
2517int cpuset_mem_spread_node(void)
2518{
Michal Hocko778d3b02011-07-26 16:08:30 -07002519 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2520 current->cpuset_mem_spread_rotor =
2521 node_random(&current->mems_allowed);
2522
Jack Steiner6adef3e2010-05-26 14:42:49 -07002523 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2524}
2525
2526int cpuset_slab_spread_node(void)
2527{
Michal Hocko778d3b02011-07-26 16:08:30 -07002528 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2529 current->cpuset_slab_spread_rotor =
2530 node_random(&current->mems_allowed);
2531
Jack Steiner6adef3e2010-05-26 14:42:49 -07002532 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2533}
2534
Paul Jackson825a46a2006-03-24 03:16:03 -08002535EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2536
2537/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002538 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2539 * @tsk1: pointer to task_struct of some task.
2540 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002541 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002542 * Description: Return true if @tsk1's mems_allowed intersects the
2543 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2544 * one of the task's memory usage might impact the memory available
2545 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002546 **/
2547
David Rientjesbbe373f2007-10-16 23:25:58 -07002548int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2549 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002550{
David Rientjesbbe373f2007-10-16 23:25:58 -07002551 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002552}
2553
David Rientjes75aa1992009-01-06 14:39:01 -08002554/**
2555 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2556 * @task: pointer to task_struct of some task.
2557 *
2558 * Description: Prints @task's name, cpuset name, and cached copy of its
2559 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2560 * dereferencing task_cs(task).
2561 */
2562void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2563{
2564 struct dentry *dentry;
2565
2566 dentry = task_cs(tsk)->css.cgroup->dentry;
2567 spin_lock(&cpuset_buffer_lock);
2568 snprintf(cpuset_name, CPUSET_NAME_LEN,
2569 dentry ? (const char *)dentry->d_name.name : "/");
2570 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2571 tsk->mems_allowed);
2572 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2573 tsk->comm, cpuset_name, cpuset_nodelist);
2574 spin_unlock(&cpuset_buffer_lock);
2575}
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002578 * Collection of memory_pressure is suppressed unless
2579 * this flag is enabled by writing "1" to the special
2580 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2581 */
2582
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002583int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002584
2585/**
2586 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2587 *
2588 * Keep a running average of the rate of synchronous (direct)
2589 * page reclaim efforts initiated by tasks in each cpuset.
2590 *
2591 * This represents the rate at which some task in the cpuset
2592 * ran low on memory on all nodes it was allowed to use, and
2593 * had to enter the kernels page reclaim code in an effort to
2594 * create more free memory by tossing clean pages or swapping
2595 * or writing dirty pages.
2596 *
2597 * Display to user space in the per-cpuset read-only file
2598 * "memory_pressure". Value displayed is an integer
2599 * representing the recent rate of entry into the synchronous
2600 * (direct) page reclaim by any task attached to the cpuset.
2601 **/
2602
2603void __cpuset_memory_pressure_bump(void)
2604{
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002605 task_lock(current);
Paul Menage8793d852007-10-18 23:39:39 -07002606 fmeter_markevent(&task_cs(current)->fmeter);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002607 task_unlock(current);
2608}
2609
Paul Menage8793d852007-10-18 23:39:39 -07002610#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002611/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 * proc_cpuset_show()
2613 * - Print tasks cpuset path into seq_file.
2614 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002615 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2616 * doesn't really matter if tsk->cpuset changes after we read it,
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002617 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002618 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 */
Paul Jackson029190c2007-10-18 23:40:20 -07002620static int proc_cpuset_show(struct seq_file *m, void *unused_v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002622 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct task_struct *tsk;
2624 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002625 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002626 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Eric W. Biederman99f89552006-06-26 00:25:55 -07002628 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2630 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Eric W. Biederman99f89552006-06-26 00:25:55 -07002633 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002634 pid = m->private;
2635 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002636 if (!tsk)
2637 goto out_free;
2638
2639 retval = -EINVAL;
Paul Menage8793d852007-10-18 23:39:39 -07002640 cgroup_lock();
2641 css = task_subsys_state(tsk, cpuset_subsys_id);
2642 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002644 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 seq_puts(m, buf);
2646 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002647out_unlock:
Paul Menage8793d852007-10-18 23:39:39 -07002648 cgroup_unlock();
Eric W. Biederman99f89552006-06-26 00:25:55 -07002649 put_task_struct(tsk);
2650out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002652out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return retval;
2654}
2655
2656static int cpuset_open(struct inode *inode, struct file *file)
2657{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002658 struct pid *pid = PROC_I(inode)->pid;
2659 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Arjan van de Ven9a321442007-02-12 00:55:35 -08002662const struct file_operations proc_cpuset_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .open = cpuset_open,
2664 .read = seq_read,
2665 .llseek = seq_lseek,
2666 .release = single_release,
2667};
Paul Menage8793d852007-10-18 23:39:39 -07002668#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Heiko Carstensd01d4822009-09-21 11:06:27 +02002670/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002671void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002673 seq_printf(m, "Mems_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002674 seq_nodemask(m, &task->mems_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002675 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002676 seq_printf(m, "Mems_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002677 seq_nodemask_list(m, &task->mems_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002678 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}