blob: 026faccca869e396d378693422e29e9c82f739ac [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
41#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>
58#include <asm/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
100 /*
101 * Copy of global cpuset_mems_generation as of the most
102 * recent time this cpuset changed its mems_allowed.
103 */
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800104 int mems_generation;
105
106 struct fmeter fmeter; /* memory_pressure filter */
Paul Jackson029190c2007-10-18 23:40:20 -0700107
108 /* partition number for rebuild_sched_domains() */
109 int pn;
Cliff Wickman956db3c2008-02-07 00:14:43 -0800110
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900111 /* for custom sched domain */
112 int relax_domain_level;
113
Cliff Wickman956db3c2008-02-07 00:14:43 -0800114 /* used for walking a cpuset heirarchy */
115 struct list_head stack_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
Paul Menage8793d852007-10-18 23:39:39 -0700118/* Retrieve the cpuset for a cgroup */
119static inline struct cpuset *cgroup_cs(struct cgroup *cont)
120{
121 return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
122 struct cpuset, css);
123}
124
125/* Retrieve the cpuset for a task */
126static inline struct cpuset *task_cs(struct task_struct *task)
127{
128 return container_of(task_subsys_state(task, cpuset_subsys_id),
129 struct cpuset, css);
130}
Paul Menage8793d852007-10-18 23:39:39 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* bits in struct cpuset flags field */
133typedef enum {
134 CS_CPU_EXCLUSIVE,
135 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700136 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800137 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700138 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800139 CS_SPREAD_PAGE,
140 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141} cpuset_flagbits_t;
142
143/* convenient tests for these bits */
144static inline int is_cpu_exclusive(const struct cpuset *cs)
145{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800146 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static inline int is_mem_exclusive(const struct cpuset *cs)
150{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800151 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Paul Menage78608362008-04-29 01:00:26 -0700154static inline int is_mem_hardwall(const struct cpuset *cs)
155{
156 return test_bit(CS_MEM_HARDWALL, &cs->flags);
157}
158
Paul Jackson029190c2007-10-18 23:40:20 -0700159static inline int is_sched_load_balance(const struct cpuset *cs)
160{
161 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
162}
163
Paul Jackson45b07ef2006-01-08 01:00:56 -0800164static inline int is_memory_migrate(const struct cpuset *cs)
165{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800166 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800167}
168
Paul Jackson825a46a2006-03-24 03:16:03 -0800169static inline int is_spread_page(const struct cpuset *cs)
170{
171 return test_bit(CS_SPREAD_PAGE, &cs->flags);
172}
173
174static inline int is_spread_slab(const struct cpuset *cs)
175{
176 return test_bit(CS_SPREAD_SLAB, &cs->flags);
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Paul Jackson151a4422006-03-24 03:16:11 -0800180 * Increment this integer everytime any cpuset changes its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * mems_allowed value. Users of cpusets can track this generation
182 * number, and avoid having to lock and reload mems_allowed unless
183 * the cpuset they're using changes generation.
184 *
Paul Menage2df167a2008-02-07 00:14:45 -0800185 * A single, global generation is needed because cpuset_attach_task() could
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * reattach a task to a different cpuset, which must not have its
187 * generation numbers aliased with those of that tasks previous cpuset.
188 *
189 * Generations are needed for mems_allowed because one task cannot
Paul Menage2df167a2008-02-07 00:14:45 -0800190 * modify another's memory placement. So we must enable every task,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * on every visit to __alloc_pages(), to efficiently check whether
192 * its current->cpuset->mems_allowed has changed, requiring an update
193 * of its current->mems_allowed.
Paul Jackson151a4422006-03-24 03:16:11 -0800194 *
Paul Menage2df167a2008-02-07 00:14:45 -0800195 * Since writes to cpuset_mems_generation are guarded by the cgroup lock
Paul Jackson151a4422006-03-24 03:16:11 -0800196 * there is no need to mark it atomic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Paul Jackson151a4422006-03-24 03:16:11 -0800198static int cpuset_mems_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200static struct cpuset top_cpuset = {
201 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
Paul Menage2df167a2008-02-07 00:14:45 -0800205 * There are two global mutexes guarding cpuset structures. The first
206 * is the main control groups cgroup_mutex, accessed via
207 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
208 * callback_mutex, below. They can nest. It is ok to first take
209 * cgroup_mutex, then nest callback_mutex. We also require taking
210 * task_lock() when dereferencing a task's cpuset pointer. See "The
211 * task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800213 * A task must hold both mutexes to modify cpusets. If a task
Paul Menage2df167a2008-02-07 00:14:45 -0800214 * holds cgroup_mutex, then it blocks others wanting that mutex,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800215 * ensuring that it is the only task able to also acquire callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800216 * and be able to modify cpusets. It can perform various checks on
217 * the cpuset structure first, knowing nothing will change. It can
Paul Menage2df167a2008-02-07 00:14:45 -0800218 * also allocate memory while just holding cgroup_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800219 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800220 * acquire callback_mutex to query cpusets. Once it is ready to make
221 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Paul Jackson053199e2005-10-30 15:02:30 -0800223 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800224 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800225 * from one of the callbacks into the cpuset code from within
226 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800228 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800229 * access to cpusets.
230 *
231 * The task_struct fields mems_allowed and mems_generation may only
232 * be accessed in the context of that task, so require no locks.
233 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800234 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800235 * small pieces of code, such as when reading out possibly multi-word
236 * cpumasks and nodemasks.
237 *
Paul Menage2df167a2008-02-07 00:14:45 -0800238 * Accessing a task's cpuset should be done in accordance with the
239 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800242static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700243
Max Krasnyanskycf417142008-08-11 14:33:53 -0700244/*
David Rientjes75aa1992009-01-06 14:39:01 -0800245 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
246 * buffers. They are statically allocated to prevent using excess stack
247 * when calling cpuset_print_task_mems_allowed().
248 */
249#define CPUSET_NAME_LEN (128)
250#define CPUSET_NODELIST_LEN (256)
251static char cpuset_name[CPUSET_NAME_LEN];
252static char cpuset_nodelist[CPUSET_NODELIST_LEN];
253static DEFINE_SPINLOCK(cpuset_buffer_lock);
254
255/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700256 * This is ugly, but preserves the userspace API for existing cpuset
Paul Menage8793d852007-10-18 23:39:39 -0700257 * users. If someone tries to mount the "cpuset" filesystem, we
Max Krasnyanskycf417142008-08-11 14:33:53 -0700258 * silently switch it to mount "cgroup" instead
259 */
David Howells454e2392006-06-23 02:02:57 -0700260static int cpuset_get_sb(struct file_system_type *fs_type,
261 int flags, const char *unused_dev_name,
262 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Paul Menage8793d852007-10-18 23:39:39 -0700264 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
265 int ret = -ENODEV;
266 if (cgroup_fs) {
267 char mountopts[] =
268 "cpuset,noprefix,"
269 "release_agent=/sbin/cpuset_release_agent";
270 ret = cgroup_fs->get_sb(cgroup_fs, flags,
271 unused_dev_name, mountopts, mnt);
272 put_filesystem(cgroup_fs);
273 }
274 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277static struct file_system_type cpuset_fs_type = {
278 .name = "cpuset",
279 .get_sb = cpuset_get_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280};
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800283 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * are online. If none are online, walk up the cpuset hierarchy
285 * until we find one that does have some online cpus. If we get
286 * all the way to the top and still haven't found any online cpus,
287 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
288 * task, return cpu_online_map.
289 *
290 * One way or another, we guarantee to return some non-empty subset
291 * of cpu_online_map.
292 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800293 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295
Li Zefan6af866a2009-01-07 18:08:45 -0800296static void guarantee_online_cpus(const struct cpuset *cs,
297 struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Li Zefan300ed6c2009-01-07 18:08:44 -0800299 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 cs = cs->parent;
301 if (cs)
Li Zefan300ed6c2009-01-07 18:08:44 -0800302 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 else
Li Zefan300ed6c2009-01-07 18:08:44 -0800304 cpumask_copy(pmask, cpu_online_mask);
305 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
309 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700310 * are online, with memory. If none are online with memory, walk
311 * up the cpuset hierarchy until we find one that does have some
312 * online mems. If we get all the way to the top and still haven't
313 * found any online mems, return node_states[N_HIGH_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
315 * One way or another, we guarantee to return some non-empty subset
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700316 * of node_states[N_HIGH_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800318 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320
321static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
322{
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700323 while (cs && !nodes_intersects(cs->mems_allowed,
324 node_states[N_HIGH_MEMORY]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 cs = cs->parent;
326 if (cs)
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700327 nodes_and(*pmask, cs->mems_allowed,
328 node_states[N_HIGH_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 else
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700330 *pmask = node_states[N_HIGH_MEMORY];
331 BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800334/**
335 * cpuset_update_task_memory_state - update task memory placement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800337 * If the current tasks cpusets mems_allowed changed behind our
338 * backs, update current->mems_allowed, mems_generation and task NUMA
339 * mempolicy to the new value.
340 *
341 * Task mempolicy is updated by rebinding it relative to the
342 * current->cpuset if a task has its memory placement changed.
343 * Do not call this routine if in_interrupt().
344 *
Paul Jackson4a01c8d2006-03-31 02:30:50 -0800345 * Call without callback_mutex or task_lock() held. May be
Paul Menage2df167a2008-02-07 00:14:45 -0800346 * called with or without cgroup_mutex held. Thanks in part to
347 * 'the_top_cpuset_hack', the task's cpuset pointer will never
David Rientjes41f7f602008-03-04 23:32:38 -0800348 * be NULL. This routine also might acquire callback_mutex during
349 * call.
Paul Jackson5aa15b52005-10-30 15:02:28 -0800350 *
Paul Jackson6b9c2602006-01-08 01:02:02 -0800351 * Reading current->cpuset->mems_generation doesn't need task_lock
352 * to guard the current->cpuset derefence, because it is guarded
Paul Menage2df167a2008-02-07 00:14:45 -0800353 * from concurrent freeing of current->cpuset using RCU.
Paul Jackson6b9c2602006-01-08 01:02:02 -0800354 *
355 * The rcu_dereference() is technically probably not needed,
356 * as I don't actually mind if I see a new cpuset pointer but
357 * an old value of mems_generation. However this really only
358 * matters on alpha systems using cpusets heavily. If I dropped
359 * that rcu_dereference(), it would save them a memory barrier.
360 * For all other arch's, rcu_dereference is a no-op anyway, and for
361 * alpha systems not using cpusets, another planned optimization,
362 * avoiding the rcu critical section for tasks in the root cpuset
363 * which is statically allocated, so can't vanish, will make this
364 * irrelevant. Better to use RCU as intended, than to engage in
365 * some cute trick to save a memory barrier that is impossible to
366 * test, for alpha systems using cpusets heavily, which might not
367 * even exist.
Paul Jackson053199e2005-10-30 15:02:30 -0800368 *
369 * This routine is needed to update the per-task mems_allowed data,
370 * within the tasks context, when it is trying to allocate memory
371 * (in various mm/mempolicy.c routines) and notices that some other
372 * task has been modifying its cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
374
Randy Dunlapfe85a992006-02-03 03:04:23 -0800375void cpuset_update_task_memory_state(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Paul Jackson053199e2005-10-30 15:02:30 -0800377 int my_cpusets_mem_gen;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800378 struct task_struct *tsk = current;
Paul Jackson6b9c2602006-01-08 01:02:02 -0800379 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Lai Jiangshan13337712009-01-07 18:08:39 -0800381 rcu_read_lock();
382 my_cpusets_mem_gen = task_cs(tsk)->mems_generation;
383 rcu_read_unlock();
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800384
385 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800386 mutex_lock(&callback_mutex);
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800387 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -0700388 cs = task_cs(tsk); /* Maybe changed when task not locked */
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800389 guarantee_online_mems(cs, &tsk->mems_allowed);
390 tsk->cpuset_mems_generation = cs->mems_generation;
Paul Jackson825a46a2006-03-24 03:16:03 -0800391 if (is_spread_page(cs))
392 tsk->flags |= PF_SPREAD_PAGE;
393 else
394 tsk->flags &= ~PF_SPREAD_PAGE;
395 if (is_spread_slab(cs))
396 tsk->flags |= PF_SPREAD_SLAB;
397 else
398 tsk->flags &= ~PF_SPREAD_SLAB;
Paul Jacksoncf2a473c2006-01-08 01:01:54 -0800399 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800400 mutex_unlock(&callback_mutex);
Paul Jackson74cb2152006-01-08 01:01:56 -0800401 mpol_rebind_task(tsk, &tsk->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403}
404
405/*
406 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
407 *
408 * One cpuset is a subset of another if all its allowed CPUs and
409 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Menage2df167a2008-02-07 00:14:45 -0800410 * are only set if the other's are set. Call holding cgroup_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
412
413static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
414{
Li Zefan300ed6c2009-01-07 18:08:44 -0800415 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 nodes_subset(p->mems_allowed, q->mems_allowed) &&
417 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
418 is_mem_exclusive(p) <= is_mem_exclusive(q);
419}
420
Li Zefan645fcc92009-01-07 18:08:43 -0800421/**
422 * alloc_trial_cpuset - allocate a trial cpuset
423 * @cs: the cpuset that the trial cpuset duplicates
424 */
425static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
426{
Li Zefan300ed6c2009-01-07 18:08:44 -0800427 struct cpuset *trial;
428
429 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
430 if (!trial)
431 return NULL;
432
433 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
434 kfree(trial);
435 return NULL;
436 }
437 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
438
439 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800440}
441
442/**
443 * free_trial_cpuset - free the trial cpuset
444 * @trial: the trial cpuset to be freed
445 */
446static void free_trial_cpuset(struct cpuset *trial)
447{
Li Zefan300ed6c2009-01-07 18:08:44 -0800448 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800449 kfree(trial);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * validate_change() - Used to validate that any proposed cpuset change
454 * follows the structural rules for cpusets.
455 *
456 * If we replaced the flag and mask values of the current cpuset
457 * (cur) with those values in the trial cpuset (trial), would
458 * our various subset and exclusive rules still be valid? Presumes
Paul Menage2df167a2008-02-07 00:14:45 -0800459 * cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *
461 * 'cur' is the address of an actual, in-use cpuset. Operations
462 * such as list traversal that depend on the actual address of the
463 * cpuset in the list must use cur below, not trial.
464 *
465 * 'trial' is the address of bulk structure copy of cur, with
466 * perhaps one or more of the fields cpus_allowed, mems_allowed,
467 * or flags changed to new, trial values.
468 *
469 * Return 0 if valid, -errno if not.
470 */
471
472static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
473{
Paul Menage8793d852007-10-18 23:39:39 -0700474 struct cgroup *cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct cpuset *c, *par;
476
477 /* Each of our child cpusets must be a subset of us */
Paul Menage8793d852007-10-18 23:39:39 -0700478 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
479 if (!is_cpuset_subset(cgroup_cs(cont), trial))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -EBUSY;
481 }
482
483 /* Remaining checks don't apply to root cpuset */
Paul Jackson69604062006-12-06 20:36:15 -0800484 if (cur == &top_cpuset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486
Paul Jackson69604062006-12-06 20:36:15 -0800487 par = cur->parent;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* We must be a subset of our parent cpuset */
490 if (!is_cpuset_subset(trial, par))
491 return -EACCES;
492
Paul Menage2df167a2008-02-07 00:14:45 -0800493 /*
494 * If either I or some sibling (!= me) is exclusive, we can't
495 * overlap
496 */
Paul Menage8793d852007-10-18 23:39:39 -0700497 list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
498 c = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
500 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800501 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -EINVAL;
503 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
504 c != cur &&
505 nodes_intersects(trial->mems_allowed, c->mems_allowed))
506 return -EINVAL;
507 }
508
Paul Jackson020958b2007-10-18 23:40:21 -0700509 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
510 if (cgroup_task_count(cur->css.cgroup)) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800511 if (cpumask_empty(trial->cpus_allowed) ||
Paul Jackson020958b2007-10-18 23:40:21 -0700512 nodes_empty(trial->mems_allowed)) {
513 return -ENOSPC;
514 }
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 0;
518}
519
Paul Menagedb7f47c2009-04-02 16:57:55 -0700520#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700521/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700522 * Helper routine for generate_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -0700523 * Do cpusets a, b have overlapping cpus_allowed masks?
524 */
Paul Jackson029190c2007-10-18 23:40:20 -0700525static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
526{
Li Zefan300ed6c2009-01-07 18:08:44 -0800527 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
Paul Jackson029190c2007-10-18 23:40:20 -0700528}
529
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900530static void
531update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
532{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900533 if (dattr->relax_domain_level < c->relax_domain_level)
534 dattr->relax_domain_level = c->relax_domain_level;
535 return;
536}
537
Lai Jiangshanf5393692008-07-29 22:33:22 -0700538static void
539update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
540{
541 LIST_HEAD(q);
542
543 list_add(&c->stack_list, &q);
544 while (!list_empty(&q)) {
545 struct cpuset *cp;
546 struct cgroup *cont;
547 struct cpuset *child;
548
549 cp = list_first_entry(&q, struct cpuset, stack_list);
550 list_del(q.next);
551
Li Zefan300ed6c2009-01-07 18:08:44 -0800552 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700553 continue;
554
555 if (is_sched_load_balance(cp))
556 update_domain_attr(dattr, cp);
557
558 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
559 child = cgroup_cs(cont);
560 list_add_tail(&child->stack_list, &q);
561 }
562 }
563}
564
Paul Jackson029190c2007-10-18 23:40:20 -0700565/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700566 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700567 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700568 * This function builds a partial partition of the systems CPUs
569 * A 'partial partition' is a set of non-overlapping subsets whose
570 * union is a subset of that set.
571 * The output of this function needs to be passed to kernel/sched.c
572 * partition_sched_domains() routine, which will rebuild the scheduler's
573 * load balancing domains (sched domains) as specified by that partial
574 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700575 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800576 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700577 * for a background explanation of this.
578 *
579 * Does not return errors, on the theory that the callers of this
580 * routine would rather not worry about failures to rebuild sched
581 * domains when operating in the severe memory shortage situations
582 * that could cause allocation failures below.
583 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700584 * Must be called with cgroup_lock held.
Paul Jackson029190c2007-10-18 23:40:20 -0700585 *
586 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700587 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700588 * top-down scan of all cpusets. This scan loads a pointer
589 * to each cpuset marked is_sched_load_balance into the
590 * array 'csa'. For our purposes, rebuilding the schedulers
591 * sched domains, we can ignore !is_sched_load_balance cpusets.
592 * csa - (for CpuSet Array) Array of pointers to all the cpusets
593 * that need to be load balanced, for convenient iterative
594 * access by the subsequent code that finds the best partition,
595 * i.e the set of domains (subsets) of CPUs such that the
596 * cpus_allowed of every cpuset marked is_sched_load_balance
597 * is a subset of one of these domains, while there are as
598 * many such domains as possible, each as small as possible.
599 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
600 * the kernel/sched.c routine partition_sched_domains() in a
601 * convenient format, that can be easily compared to the prior
602 * value to determine what partition elements (sched domains)
603 * were changed (added or removed.)
604 *
605 * Finding the best partition (set of domains):
606 * The triple nested loops below over i, j, k scan over the
607 * load balanced cpusets (using the array of cpuset pointers in
608 * csa[]) looking for pairs of cpusets that have overlapping
609 * cpus_allowed, but which don't have the same 'pn' partition
610 * number and gives them in the same partition number. It keeps
611 * looping on the 'restart' label until it can no longer find
612 * any such pairs.
613 *
614 * The union of the cpus_allowed masks from the set of
615 * all cpusets having the same 'pn' value then form the one
616 * element of the partition (one sched domain) to be passed to
617 * partition_sched_domains().
618 */
Li Zefan6af866a2009-01-07 18:08:45 -0800619/* FIXME: see the FIXME in partition_sched_domains() */
620static int generate_sched_domains(struct cpumask **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700621 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700622{
Max Krasnyanskycf417142008-08-11 14:33:53 -0700623 LIST_HEAD(q); /* queue of cpusets to be scanned */
Paul Jackson029190c2007-10-18 23:40:20 -0700624 struct cpuset *cp; /* scans q */
625 struct cpuset **csa; /* array of all cpuset ptrs */
626 int csn; /* how many cpuset ptrs in csa so far */
627 int i, j, k; /* indices for partition finding loops */
Li Zefan6af866a2009-01-07 18:08:45 -0800628 struct cpumask *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900629 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100630 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800631 int nslot; /* next empty doms[] struct cpumask slot */
Paul Jackson029190c2007-10-18 23:40:20 -0700632
Paul Jackson029190c2007-10-18 23:40:20 -0700633 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900634 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700635 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700636
637 /* Special case for the 99% of systems with one, full, sched domain */
638 if (is_sched_load_balance(&top_cpuset)) {
Li Zefan6af866a2009-01-07 18:08:45 -0800639 doms = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700640 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700641 goto done;
642
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900643 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
644 if (dattr) {
645 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700646 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900647 }
Li Zefan300ed6c2009-01-07 18:08:44 -0800648 cpumask_copy(doms, top_cpuset.cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700649
650 ndoms = 1;
651 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700652 }
653
Paul Jackson029190c2007-10-18 23:40:20 -0700654 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
655 if (!csa)
656 goto done;
657 csn = 0;
658
Li Zefanaeed6822008-07-29 22:33:24 -0700659 list_add(&top_cpuset.stack_list, &q);
660 while (!list_empty(&q)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700661 struct cgroup *cont;
662 struct cpuset *child; /* scans child cpusets of cp */
Lai Jiangshan489a5392008-07-25 01:47:23 -0700663
Li Zefanaeed6822008-07-29 22:33:24 -0700664 cp = list_first_entry(&q, struct cpuset, stack_list);
665 list_del(q.next);
666
Li Zefan300ed6c2009-01-07 18:08:44 -0800667 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshan489a5392008-07-25 01:47:23 -0700668 continue;
669
Lai Jiangshanf5393692008-07-29 22:33:22 -0700670 /*
671 * All child cpusets contain a subset of the parent's cpus, so
672 * just skip them, and then we call update_domain_attr_tree()
673 * to calc relax_domain_level of the corresponding sched
674 * domain.
675 */
676 if (is_sched_load_balance(cp)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700677 csa[csn++] = cp;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700678 continue;
679 }
Lai Jiangshan489a5392008-07-25 01:47:23 -0700680
Paul Jackson029190c2007-10-18 23:40:20 -0700681 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
682 child = cgroup_cs(cont);
Li Zefanaeed6822008-07-29 22:33:24 -0700683 list_add_tail(&child->stack_list, &q);
Paul Jackson029190c2007-10-18 23:40:20 -0700684 }
685 }
686
687 for (i = 0; i < csn; i++)
688 csa[i]->pn = i;
689 ndoms = csn;
690
691restart:
692 /* Find the best partition (set of sched domains) */
693 for (i = 0; i < csn; i++) {
694 struct cpuset *a = csa[i];
695 int apn = a->pn;
696
697 for (j = 0; j < csn; j++) {
698 struct cpuset *b = csa[j];
699 int bpn = b->pn;
700
701 if (apn != bpn && cpusets_overlap(a, b)) {
702 for (k = 0; k < csn; k++) {
703 struct cpuset *c = csa[k];
704
705 if (c->pn == bpn)
706 c->pn = apn;
707 }
708 ndoms--; /* one less element */
709 goto restart;
710 }
711 }
712 }
713
Max Krasnyanskycf417142008-08-11 14:33:53 -0700714 /*
715 * Now we know how many domains to create.
716 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
717 */
Li Zefan6af866a2009-01-07 18:08:45 -0800718 doms = kmalloc(ndoms * cpumask_size(), GFP_KERNEL);
Li Zefan700018e2008-11-18 14:02:03 +0800719 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700720 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700721
722 /*
723 * The rest of the code, including the scheduler, can deal with
724 * dattr==NULL case. No need to abort if alloc fails.
725 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900726 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700727
728 for (nslot = 0, i = 0; i < csn; i++) {
729 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800730 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700731 int apn = a->pn;
732
Max Krasnyanskycf417142008-08-11 14:33:53 -0700733 if (apn < 0) {
734 /* Skip completed partitions */
735 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700736 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700737
738 dp = doms + nslot;
739
740 if (nslot == ndoms) {
741 static int warnings = 10;
742 if (warnings) {
743 printk(KERN_WARNING
744 "rebuild_sched_domains confused:"
745 " nslot %d, ndoms %d, csn %d, i %d,"
746 " apn %d\n",
747 nslot, ndoms, csn, i, apn);
748 warnings--;
749 }
750 continue;
751 }
752
Li Zefan6af866a2009-01-07 18:08:45 -0800753 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700754 if (dattr)
755 *(dattr + nslot) = SD_ATTR_INIT;
756 for (j = i; j < csn; j++) {
757 struct cpuset *b = csa[j];
758
759 if (apn == b->pn) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800760 cpumask_or(dp, dp, b->cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700761 if (dattr)
762 update_domain_attr_tree(dattr + nslot, b);
763
764 /* Done with this partition */
765 b->pn = -1;
766 }
767 }
768 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700769 }
770 BUG_ON(nslot != ndoms);
771
Paul Jackson029190c2007-10-18 23:40:20 -0700772done:
Paul Jackson029190c2007-10-18 23:40:20 -0700773 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700774
Li Zefan700018e2008-11-18 14:02:03 +0800775 /*
776 * Fallback to the default domain if kmalloc() failed.
777 * See comments in partition_sched_domains().
778 */
779 if (doms == NULL)
780 ndoms = 1;
781
Max Krasnyanskycf417142008-08-11 14:33:53 -0700782 *domains = doms;
783 *attributes = dattr;
784 return ndoms;
785}
786
787/*
788 * Rebuild scheduler domains.
789 *
790 * Call with neither cgroup_mutex held nor within get_online_cpus().
791 * Takes both cgroup_mutex and get_online_cpus().
792 *
793 * Cannot be directly called from cpuset code handling changes
794 * to the cpuset pseudo-filesystem, because it cannot be called
795 * from code that already holds cgroup_mutex.
796 */
797static void do_rebuild_sched_domains(struct work_struct *unused)
798{
799 struct sched_domain_attr *attr;
Li Zefan6af866a2009-01-07 18:08:45 -0800800 struct cpumask *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700801 int ndoms;
802
803 get_online_cpus();
804
805 /* Generate domain masks and attrs */
806 cgroup_lock();
807 ndoms = generate_sched_domains(&doms, &attr);
808 cgroup_unlock();
809
810 /* Have scheduler rebuild the domains */
811 partition_sched_domains(ndoms, doms, attr);
812
813 put_online_cpus();
814}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700815#else /* !CONFIG_SMP */
816static void do_rebuild_sched_domains(struct work_struct *unused)
817{
818}
819
820static int generate_sched_domains(struct cpumask **domains,
821 struct sched_domain_attr **attributes)
822{
823 *domains = NULL;
824 return 1;
825}
826#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700827
828static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
829
830/*
831 * Rebuild scheduler domains, asynchronously via workqueue.
832 *
833 * If the flag 'sched_load_balance' of any cpuset with non-empty
834 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
835 * which has that flag enabled, or if any cpuset with a non-empty
836 * 'cpus' is removed, then call this routine to rebuild the
837 * scheduler's dynamic sched domains.
838 *
839 * The rebuild_sched_domains() and partition_sched_domains()
840 * routines must nest cgroup_lock() inside get_online_cpus(),
841 * but such cpuset changes as these must nest that locking the
842 * other way, holding cgroup_lock() for much of the code.
843 *
844 * So in order to avoid an ABBA deadlock, the cpuset code handling
845 * these user changes delegates the actual sched domain rebuilding
846 * to a separate workqueue thread, which ends up processing the
847 * above do_rebuild_sched_domains() function.
848 */
849static void async_rebuild_sched_domains(void)
850{
Miao Xief90d4112009-01-16 10:24:10 +0800851 queue_work(cpuset_wq, &rebuild_sched_domains_work);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700852}
853
854/*
855 * Accomplishes the same scheduler domain rebuild as the above
856 * async_rebuild_sched_domains(), however it directly calls the
857 * rebuild routine synchronously rather than calling it via an
858 * asynchronous work thread.
859 *
860 * This can only be called from code that is not holding
861 * cgroup_mutex (not nested in a cgroup_lock() call.)
862 */
863void rebuild_sched_domains(void)
864{
865 do_rebuild_sched_domains(NULL);
Paul Jackson029190c2007-10-18 23:40:20 -0700866}
867
Cliff Wickman58f47902008-02-07 00:14:44 -0800868/**
869 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
870 * @tsk: task to test
871 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
872 *
Paul Menage2df167a2008-02-07 00:14:45 -0800873 * Call with cgroup_mutex held. May take callback_mutex during call.
Cliff Wickman58f47902008-02-07 00:14:44 -0800874 * Called for each task in a cgroup by cgroup_scan_tasks().
875 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
876 * words, if its mask is not equal to its cpuset's mask).
Paul Jackson053199e2005-10-30 15:02:30 -0800877 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700878static int cpuset_test_cpumask(struct task_struct *tsk,
879 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800880{
Li Zefan300ed6c2009-01-07 18:08:44 -0800881 return !cpumask_equal(&tsk->cpus_allowed,
Cliff Wickman58f47902008-02-07 00:14:44 -0800882 (cgroup_cs(scan->cg))->cpus_allowed);
883}
Paul Jackson053199e2005-10-30 15:02:30 -0800884
Cliff Wickman58f47902008-02-07 00:14:44 -0800885/**
886 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
887 * @tsk: task to test
888 * @scan: struct cgroup_scanner containing the cgroup of the task
889 *
890 * Called by cgroup_scan_tasks() for each task in a cgroup whose
891 * cpus_allowed mask needs to be changed.
892 *
893 * We don't need to re-check for the cgroup/cpuset membership, since we're
894 * holding cgroup_lock() at this point.
895 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700896static void cpuset_change_cpumask(struct task_struct *tsk,
897 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800898{
Li Zefan300ed6c2009-01-07 18:08:44 -0800899 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
Cliff Wickman58f47902008-02-07 00:14:44 -0800900}
901
902/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700903 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
904 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
Li Zefan4e743392008-09-13 02:33:08 -0700905 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -0700906 *
907 * Called with cgroup_mutex held
908 *
909 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
910 * calling callback functions for each.
911 *
Li Zefan4e743392008-09-13 02:33:08 -0700912 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
913 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -0700914 */
Li Zefan4e743392008-09-13 02:33:08 -0700915static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
Miao Xie0b2f6302008-07-25 01:47:21 -0700916{
917 struct cgroup_scanner scan;
Miao Xie0b2f6302008-07-25 01:47:21 -0700918
919 scan.cg = cs->css.cgroup;
920 scan.test_task = cpuset_test_cpumask;
921 scan.process_task = cpuset_change_cpumask;
Li Zefan4e743392008-09-13 02:33:08 -0700922 scan.heap = heap;
923 cgroup_scan_tasks(&scan);
Miao Xie0b2f6302008-07-25 01:47:21 -0700924}
925
926/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800927 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
928 * @cs: the cpuset to consider
929 * @buf: buffer of cpu numbers written to this cpuset
930 */
Li Zefan645fcc92009-01-07 18:08:43 -0800931static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
932 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Li Zefan4e743392008-09-13 02:33:08 -0700934 struct ptr_heap heap;
Cliff Wickman58f47902008-02-07 00:14:44 -0800935 int retval;
936 int is_load_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700938 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
939 if (cs == &top_cpuset)
940 return -EACCES;
941
David Rientjes6f7f02e2007-05-08 00:31:43 -0700942 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800943 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700944 * Since cpulist_parse() fails on an empty mask, we special case
945 * that parsing. The validate_change() call ensures that cpusets
946 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700947 */
Paul Jackson020958b2007-10-18 23:40:21 -0700948 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800949 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700950 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -0800951 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700952 if (retval < 0)
953 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700954
Li Zefan300ed6c2009-01-07 18:08:44 -0800955 if (!cpumask_subset(trialcs->cpus_allowed, cpu_online_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700956 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700957 }
Li Zefan645fcc92009-01-07 18:08:43 -0800958 retval = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700959 if (retval < 0)
960 return retval;
Paul Jackson029190c2007-10-18 23:40:20 -0700961
Paul Menage8707d8b2007-10-18 23:40:22 -0700962 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800963 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700964 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800965
Li Zefan4e743392008-09-13 02:33:08 -0700966 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
967 if (retval)
968 return retval;
969
Li Zefan645fcc92009-01-07 18:08:43 -0800970 is_load_balanced = is_sched_load_balance(trialcs);
Paul Jackson029190c2007-10-18 23:40:20 -0700971
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800972 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -0800973 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800974 mutex_unlock(&callback_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700975
Paul Menage8707d8b2007-10-18 23:40:22 -0700976 /*
977 * Scan tasks in the cpuset, and update the cpumasks of any
Cliff Wickman58f47902008-02-07 00:14:44 -0800978 * that need an update.
Paul Menage8707d8b2007-10-18 23:40:22 -0700979 */
Li Zefan4e743392008-09-13 02:33:08 -0700980 update_tasks_cpumask(cs, &heap);
981
982 heap_free(&heap);
Cliff Wickman58f47902008-02-07 00:14:44 -0800983
Paul Menage8707d8b2007-10-18 23:40:22 -0700984 if (is_load_balanced)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700985 async_rebuild_sched_domains();
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Paul Jackson053199e2005-10-30 15:02:30 -0800989/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800990 * cpuset_migrate_mm
991 *
992 * Migrate memory region from one set of nodes to another.
993 *
994 * Temporarilly set tasks mems_allowed to target nodes of migration,
995 * so that the migration code can allocate pages on these nodes.
996 *
Paul Menage2df167a2008-02-07 00:14:45 -0800997 * Call holding cgroup_mutex, so current's cpuset won't change
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800998 * during this call, as manage_mutex holds off any cpuset_attach()
Paul Jacksone4e364e2006-03-31 02:30:52 -0800999 * calls. Therefore we don't need to take task_lock around the
1000 * call to guarantee_online_mems(), as we know no one is changing
Paul Menage2df167a2008-02-07 00:14:45 -08001001 * our task's cpuset.
Paul Jacksone4e364e2006-03-31 02:30:52 -08001002 *
1003 * Hold callback_mutex around the two modifications of our tasks
1004 * mems_allowed to synchronize with cpuset_mems_allowed().
1005 *
1006 * While the mm_struct we are migrating is typically from some
1007 * other task, the task_struct mems_allowed that we are hacking
1008 * is for our current task, which must allocate new pages for that
1009 * migrating memory region.
1010 *
1011 * We call cpuset_update_task_memory_state() before hacking
1012 * our tasks mems_allowed, so that we are assured of being in
1013 * sync with our tasks cpuset, and in particular, callbacks to
1014 * cpuset_update_task_memory_state() from nested page allocations
1015 * won't see any mismatch of our cpuset and task mems_generation
1016 * values, so won't overwrite our hacked tasks mems_allowed
1017 * nodemask.
1018 */
1019
1020static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1021 const nodemask_t *to)
1022{
1023 struct task_struct *tsk = current;
1024
1025 cpuset_update_task_memory_state();
1026
1027 mutex_lock(&callback_mutex);
1028 tsk->mems_allowed = *to;
1029 mutex_unlock(&callback_mutex);
1030
1031 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
1032
1033 mutex_lock(&callback_mutex);
Paul Menage8793d852007-10-18 23:39:39 -07001034 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001035 mutex_unlock(&callback_mutex);
1036}
1037
Li Zefan3b6766f2009-04-02 16:57:51 -07001038/*
1039 * Rebind task's vmas to cpuset's new mems_allowed, and migrate pages to new
1040 * nodes if memory_migrate flag is set. Called with cgroup_mutex held.
1041 */
1042static void cpuset_change_nodemask(struct task_struct *p,
1043 struct cgroup_scanner *scan)
1044{
1045 struct mm_struct *mm;
1046 struct cpuset *cs;
1047 int migrate;
1048 const nodemask_t *oldmem = scan->data;
1049
1050 mm = get_task_mm(p);
1051 if (!mm)
1052 return;
1053
1054 cs = cgroup_cs(scan->cg);
1055 migrate = is_memory_migrate(cs);
1056
1057 mpol_rebind_mm(mm, &cs->mems_allowed);
1058 if (migrate)
1059 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1060 mmput(mm);
1061}
1062
Paul Menage8793d852007-10-18 23:39:39 -07001063static void *cpuset_being_rebound;
1064
Miao Xie0b2f6302008-07-25 01:47:21 -07001065/**
1066 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1067 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1068 * @oldmem: old mems_allowed of cpuset cs
Li Zefan010cfac2009-04-02 16:57:52 -07001069 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -07001070 *
1071 * Called with cgroup_mutex held
Li Zefan010cfac2009-04-02 16:57:52 -07001072 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1073 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -07001074 */
Li Zefan010cfac2009-04-02 16:57:52 -07001075static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1076 struct ptr_heap *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Li Zefan3b6766f2009-04-02 16:57:51 -07001078 struct cgroup_scanner scan;
Paul Jackson59dac162006-01-08 01:01:52 -08001079
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001080 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001081
Li Zefan3b6766f2009-04-02 16:57:51 -07001082 scan.cg = cs->css.cgroup;
1083 scan.test_task = NULL;
1084 scan.process_task = cpuset_change_nodemask;
Li Zefan010cfac2009-04-02 16:57:52 -07001085 scan.heap = heap;
Li Zefan3b6766f2009-04-02 16:57:51 -07001086 scan.data = (nodemask_t *)oldmem;
Paul Jackson42253992006-01-08 01:01:59 -08001087
1088 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001089 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1090 * take while holding tasklist_lock. Forks can happen - the
1091 * mpol_dup() cpuset_being_rebound check will catch such forks,
1092 * and rebind their vma mempolicies too. Because we still hold
1093 * the global cgroup_mutex, we know that no other rebind effort
1094 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001095 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001096 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001097 */
Li Zefan010cfac2009-04-02 16:57:52 -07001098 cgroup_scan_tasks(&scan);
Paul Jackson42253992006-01-08 01:01:59 -08001099
Paul Menage2df167a2008-02-07 00:14:45 -08001100 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001101 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Miao Xie0b2f6302008-07-25 01:47:21 -07001104/*
1105 * Handle user request to change the 'mems' memory placement
1106 * of a cpuset. Needs to validate the request, update the
1107 * cpusets mems_allowed and mems_generation, and for each
1108 * task in the cpuset, rebind any vma mempolicies and if
1109 * the cpuset is marked 'memory_migrate', migrate the tasks
1110 * pages to the new memory.
1111 *
1112 * Call with cgroup_mutex held. May take callback_mutex during call.
1113 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1114 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1115 * their mempolicies to the cpusets new mems_allowed.
1116 */
Li Zefan645fcc92009-01-07 18:08:43 -08001117static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1118 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001119{
Miao Xie0b2f6302008-07-25 01:47:21 -07001120 nodemask_t oldmem;
1121 int retval;
Li Zefan010cfac2009-04-02 16:57:52 -07001122 struct ptr_heap heap;
Miao Xie0b2f6302008-07-25 01:47:21 -07001123
1124 /*
1125 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
1126 * it's read-only
1127 */
1128 if (cs == &top_cpuset)
1129 return -EACCES;
1130
Miao Xie0b2f6302008-07-25 01:47:21 -07001131 /*
1132 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1133 * Since nodelist_parse() fails on an empty mask, we special case
1134 * that parsing. The validate_change() call ensures that cpusets
1135 * with tasks have memory.
1136 */
1137 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001138 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001139 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001140 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001141 if (retval < 0)
1142 goto done;
1143
Li Zefan645fcc92009-01-07 18:08:43 -08001144 if (!nodes_subset(trialcs->mems_allowed,
Miao Xie0b2f6302008-07-25 01:47:21 -07001145 node_states[N_HIGH_MEMORY]))
1146 return -EINVAL;
1147 }
1148 oldmem = cs->mems_allowed;
Li Zefan645fcc92009-01-07 18:08:43 -08001149 if (nodes_equal(oldmem, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001150 retval = 0; /* Too easy - nothing to do */
1151 goto done;
1152 }
Li Zefan645fcc92009-01-07 18:08:43 -08001153 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001154 if (retval < 0)
1155 goto done;
1156
Li Zefan010cfac2009-04-02 16:57:52 -07001157 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1158 if (retval < 0)
1159 goto done;
1160
Miao Xie0b2f6302008-07-25 01:47:21 -07001161 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001162 cs->mems_allowed = trialcs->mems_allowed;
Miao Xie0b2f6302008-07-25 01:47:21 -07001163 cs->mems_generation = cpuset_mems_generation++;
1164 mutex_unlock(&callback_mutex);
1165
Li Zefan010cfac2009-04-02 16:57:52 -07001166 update_tasks_nodemask(cs, &oldmem, &heap);
1167
1168 heap_free(&heap);
Miao Xie0b2f6302008-07-25 01:47:21 -07001169done:
1170 return retval;
1171}
1172
Paul Menage8793d852007-10-18 23:39:39 -07001173int current_cpuset_is_being_rebound(void)
1174{
1175 return task_cs(current) == cpuset_being_rebound;
1176}
1177
Paul Menage5be7a472008-05-06 20:42:41 -07001178static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001179{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001180#ifdef CONFIG_SMP
Li Zefan30e0e172008-05-13 10:27:17 +08001181 if (val < -1 || val >= SD_LV_MAX)
1182 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001183#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001184
1185 if (val != cs->relax_domain_level) {
1186 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001187 if (!cpumask_empty(cs->cpus_allowed) &&
1188 is_sched_load_balance(cs))
Max Krasnyanskycf417142008-08-11 14:33:53 -07001189 async_rebuild_sched_domains();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001190 }
1191
1192 return 0;
1193}
1194
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001197 * bit: the bit to update (see cpuset_flagbits_t)
1198 * cs: the cpuset to update
1199 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001200 *
Paul Menage2df167a2008-02-07 00:14:45 -08001201 * Call with cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
1203
Paul Menage700fe1a2008-04-29 01:00:00 -07001204static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1205 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Li Zefan645fcc92009-01-07 18:08:43 -08001207 struct cpuset *trialcs;
Paul Jackson607717a2007-10-16 01:27:43 -07001208 int err;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001209 int balance_flag_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Li Zefan645fcc92009-01-07 18:08:43 -08001211 trialcs = alloc_trial_cpuset(cs);
1212 if (!trialcs)
1213 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Li Zefan645fcc92009-01-07 18:08:43 -08001215 if (turning_on)
1216 set_bit(bit, &trialcs->flags);
1217 else
1218 clear_bit(bit, &trialcs->flags);
1219
1220 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001221 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001222 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001223
Paul Jackson029190c2007-10-18 23:40:20 -07001224 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001225 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001226
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001227 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001228 cs->flags = trialcs->flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001229 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001230
Li Zefan300ed6c2009-01-07 18:08:44 -08001231 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Max Krasnyanskycf417142008-08-11 14:33:53 -07001232 async_rebuild_sched_domains();
Paul Jackson029190c2007-10-18 23:40:20 -07001233
Li Zefan645fcc92009-01-07 18:08:43 -08001234out:
1235 free_trial_cpuset(trialcs);
1236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Paul Jackson053199e2005-10-30 15:02:30 -08001239/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001240 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001241 *
1242 * These routines manage a digitally filtered, constant time based,
1243 * event frequency meter. There are four routines:
1244 * fmeter_init() - initialize a frequency meter.
1245 * fmeter_markevent() - called each time the event happens.
1246 * fmeter_getrate() - returns the recent rate of such events.
1247 * fmeter_update() - internal routine used to update fmeter.
1248 *
1249 * A common data structure is passed to each of these routines,
1250 * which is used to keep track of the state required to manage the
1251 * frequency meter and its digital filter.
1252 *
1253 * The filter works on the number of events marked per unit time.
1254 * The filter is single-pole low-pass recursive (IIR). The time unit
1255 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1256 * simulate 3 decimal digits of precision (multiplied by 1000).
1257 *
1258 * With an FM_COEF of 933, and a time base of 1 second, the filter
1259 * has a half-life of 10 seconds, meaning that if the events quit
1260 * happening, then the rate returned from the fmeter_getrate()
1261 * will be cut in half each 10 seconds, until it converges to zero.
1262 *
1263 * It is not worth doing a real infinitely recursive filter. If more
1264 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1265 * just compute FM_MAXTICKS ticks worth, by which point the level
1266 * will be stable.
1267 *
1268 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1269 * arithmetic overflow in the fmeter_update() routine.
1270 *
1271 * Given the simple 32 bit integer arithmetic used, this meter works
1272 * best for reporting rates between one per millisecond (msec) and
1273 * one per 32 (approx) seconds. At constant rates faster than one
1274 * per msec it maxes out at values just under 1,000,000. At constant
1275 * rates between one per msec, and one per second it will stabilize
1276 * to a value N*1000, where N is the rate of events per second.
1277 * At constant rates between one per second and one per 32 seconds,
1278 * it will be choppy, moving up on the seconds that have an event,
1279 * and then decaying until the next event. At rates slower than
1280 * about one in 32 seconds, it decays all the way back to zero between
1281 * each event.
1282 */
1283
1284#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1285#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1286#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1287#define FM_SCALE 1000 /* faux fixed point scale */
1288
1289/* Initialize a frequency meter */
1290static void fmeter_init(struct fmeter *fmp)
1291{
1292 fmp->cnt = 0;
1293 fmp->val = 0;
1294 fmp->time = 0;
1295 spin_lock_init(&fmp->lock);
1296}
1297
1298/* Internal meter update - process cnt events and update value */
1299static void fmeter_update(struct fmeter *fmp)
1300{
1301 time_t now = get_seconds();
1302 time_t ticks = now - fmp->time;
1303
1304 if (ticks == 0)
1305 return;
1306
1307 ticks = min(FM_MAXTICKS, ticks);
1308 while (ticks-- > 0)
1309 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1310 fmp->time = now;
1311
1312 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1313 fmp->cnt = 0;
1314}
1315
1316/* Process any previous ticks, then bump cnt by one (times scale). */
1317static void fmeter_markevent(struct fmeter *fmp)
1318{
1319 spin_lock(&fmp->lock);
1320 fmeter_update(fmp);
1321 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1322 spin_unlock(&fmp->lock);
1323}
1324
1325/* Process any previous ticks, then return current value. */
1326static int fmeter_getrate(struct fmeter *fmp)
1327{
1328 int val;
1329
1330 spin_lock(&fmp->lock);
1331 fmeter_update(fmp);
1332 val = fmp->val;
1333 spin_unlock(&fmp->lock);
1334 return val;
1335}
1336
Li Zefan2341d1b2009-01-07 18:08:42 -08001337/* Protected by cgroup_lock */
1338static cpumask_var_t cpus_attach;
1339
Paul Menage2df167a2008-02-07 00:14:45 -08001340/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
Paul Menage8793d852007-10-18 23:39:39 -07001341static int cpuset_can_attach(struct cgroup_subsys *ss,
1342 struct cgroup *cont, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Paul Menage8793d852007-10-18 23:39:39 -07001344 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Li Zefan300ed6c2009-01-07 18:08:44 -08001346 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -ENOSPC;
David Rientjes9985b0b2008-06-05 12:57:11 -07001348
David Rientjes6d7b2f52009-04-02 16:57:57 -07001349 /*
1350 * Kthreads bound to specific cpus cannot be moved to a new cpuset; we
1351 * cannot change their cpu affinity and isolating such threads by their
1352 * set of allowed nodes is unnecessary. Thus, cpusets are not
1353 * applicable for such threads. This prevents checking for success of
1354 * set_cpus_allowed_ptr() on all attached tasks before cpus_allowed may
1355 * be changed.
1356 */
1357 if (tsk->flags & PF_THREAD_BOUND)
1358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
David Rientjes6d7b2f52009-04-02 16:57:57 -07001360 return security_task_setscheduler(tsk, 0, NULL);
Paul Menage8793d852007-10-18 23:39:39 -07001361}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Paul Menage8793d852007-10-18 23:39:39 -07001363static void cpuset_attach(struct cgroup_subsys *ss,
1364 struct cgroup *cont, struct cgroup *oldcont,
1365 struct task_struct *tsk)
1366{
Paul Menage8793d852007-10-18 23:39:39 -07001367 nodemask_t from, to;
1368 struct mm_struct *mm;
1369 struct cpuset *cs = cgroup_cs(cont);
1370 struct cpuset *oldcs = cgroup_cs(oldcont);
David Rientjes9985b0b2008-06-05 12:57:11 -07001371 int err;
David Quigley22fb52d2006-06-23 02:04:00 -07001372
Miao Xief5813d92009-01-07 18:08:40 -08001373 if (cs == &top_cpuset) {
Li Zefan2341d1b2009-01-07 18:08:42 -08001374 cpumask_copy(cpus_attach, cpu_possible_mask);
Miao Xief5813d92009-01-07 18:08:40 -08001375 } else {
1376 mutex_lock(&callback_mutex);
Li Zefan2341d1b2009-01-07 18:08:42 -08001377 guarantee_online_cpus(cs, cpus_attach);
Miao Xief5813d92009-01-07 18:08:40 -08001378 mutex_unlock(&callback_mutex);
1379 }
Li Zefan2341d1b2009-01-07 18:08:42 -08001380 err = set_cpus_allowed_ptr(tsk, cpus_attach);
David Rientjes9985b0b2008-06-05 12:57:11 -07001381 if (err)
1382 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Paul Jackson45b07ef2006-01-08 01:00:56 -08001384 from = oldcs->mems_allowed;
1385 to = cs->mems_allowed;
Paul Jackson42253992006-01-08 01:01:59 -08001386 mm = get_task_mm(tsk);
1387 if (mm) {
1388 mpol_rebind_mm(mm, &to);
Paul Jackson2741a552006-03-31 02:30:51 -08001389 if (is_memory_migrate(cs))
Paul Jacksone4e364e2006-03-31 02:30:52 -08001390 cpuset_migrate_mm(mm, &from, &to);
Paul Jackson42253992006-01-08 01:01:59 -08001391 mmput(mm);
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395/* The various types of files and directories in a cpuset file system */
1396
1397typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001398 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 FILE_CPULIST,
1400 FILE_MEMLIST,
1401 FILE_CPU_EXCLUSIVE,
1402 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001403 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001404 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001405 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001406 FILE_MEMORY_PRESSURE_ENABLED,
1407 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001408 FILE_SPREAD_PAGE,
1409 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410} cpuset_filetype_t;
1411
Paul Menage700fe1a2008-04-29 01:00:00 -07001412static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1413{
1414 int retval = 0;
1415 struct cpuset *cs = cgroup_cs(cgrp);
1416 cpuset_filetype_t type = cft->private;
1417
Paul Menagee3712392008-07-25 01:47:02 -07001418 if (!cgroup_lock_live_group(cgrp))
Paul Menage700fe1a2008-04-29 01:00:00 -07001419 return -ENODEV;
Paul Menage700fe1a2008-04-29 01:00:00 -07001420
1421 switch (type) {
1422 case FILE_CPU_EXCLUSIVE:
1423 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1424 break;
1425 case FILE_MEM_EXCLUSIVE:
1426 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1427 break;
Paul Menage78608362008-04-29 01:00:26 -07001428 case FILE_MEM_HARDWALL:
1429 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1430 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001431 case FILE_SCHED_LOAD_BALANCE:
1432 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1433 break;
1434 case FILE_MEMORY_MIGRATE:
1435 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1436 break;
1437 case FILE_MEMORY_PRESSURE_ENABLED:
1438 cpuset_memory_pressure_enabled = !!val;
1439 break;
1440 case FILE_MEMORY_PRESSURE:
1441 retval = -EACCES;
1442 break;
1443 case FILE_SPREAD_PAGE:
1444 retval = update_flag(CS_SPREAD_PAGE, cs, val);
1445 cs->mems_generation = cpuset_mems_generation++;
1446 break;
1447 case FILE_SPREAD_SLAB:
1448 retval = update_flag(CS_SPREAD_SLAB, cs, val);
1449 cs->mems_generation = cpuset_mems_generation++;
1450 break;
1451 default:
1452 retval = -EINVAL;
1453 break;
1454 }
1455 cgroup_unlock();
1456 return retval;
1457}
1458
Paul Menage5be7a472008-05-06 20:42:41 -07001459static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1460{
1461 int retval = 0;
1462 struct cpuset *cs = cgroup_cs(cgrp);
1463 cpuset_filetype_t type = cft->private;
1464
Paul Menagee3712392008-07-25 01:47:02 -07001465 if (!cgroup_lock_live_group(cgrp))
Paul Menage5be7a472008-05-06 20:42:41 -07001466 return -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001467
Paul Menage5be7a472008-05-06 20:42:41 -07001468 switch (type) {
1469 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1470 retval = update_relax_domain_level(cs, val);
1471 break;
1472 default:
1473 retval = -EINVAL;
1474 break;
1475 }
1476 cgroup_unlock();
1477 return retval;
1478}
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/*
Paul Menagee3712392008-07-25 01:47:02 -07001481 * Common handling for a write to a "cpus" or "mems" file.
1482 */
1483static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1484 const char *buf)
1485{
1486 int retval = 0;
Li Zefan645fcc92009-01-07 18:08:43 -08001487 struct cpuset *cs = cgroup_cs(cgrp);
1488 struct cpuset *trialcs;
Paul Menagee3712392008-07-25 01:47:02 -07001489
1490 if (!cgroup_lock_live_group(cgrp))
1491 return -ENODEV;
1492
Li Zefan645fcc92009-01-07 18:08:43 -08001493 trialcs = alloc_trial_cpuset(cs);
1494 if (!trialcs)
1495 return -ENOMEM;
1496
Paul Menagee3712392008-07-25 01:47:02 -07001497 switch (cft->private) {
1498 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001499 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001500 break;
1501 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001502 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001503 break;
1504 default:
1505 retval = -EINVAL;
1506 break;
1507 }
Li Zefan645fcc92009-01-07 18:08:43 -08001508
1509 free_trial_cpuset(trialcs);
Paul Menagee3712392008-07-25 01:47:02 -07001510 cgroup_unlock();
1511 return retval;
1512}
1513
1514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * These ascii lists should be read in a single call, by using a user
1516 * buffer large enough to hold the entire map. If read in smaller
1517 * chunks, there is no guarantee of atomicity. Since the display format
1518 * used, list of ranges of sequential numbers, is variable length,
1519 * and since these maps can change value dynamically, one could read
1520 * gibberish by doing partial reads while a list was changing.
1521 * A single large read to a buffer that crosses a page boundary is
1522 * ok, because the result being copied to user land is not recomputed
1523 * across a page fault.
1524 */
1525
1526static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1527{
Li Zefan5a7625d2009-01-07 18:08:41 -08001528 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001530 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08001531 ret = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001532 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Li Zefan5a7625d2009-01-07 18:08:41 -08001534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1538{
1539 nodemask_t mask;
1540
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001541 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mask = cs->mems_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001543 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1546}
1547
Paul Menage8793d852007-10-18 23:39:39 -07001548static ssize_t cpuset_common_file_read(struct cgroup *cont,
1549 struct cftype *cft,
1550 struct file *file,
1551 char __user *buf,
1552 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Paul Menage8793d852007-10-18 23:39:39 -07001554 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 cpuset_filetype_t type = cft->private;
1556 char *page;
1557 ssize_t retval = 0;
1558 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Mel Gormane12ba742007-10-16 01:25:52 -07001560 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return -ENOMEM;
1562
1563 s = page;
1564
1565 switch (type) {
1566 case FILE_CPULIST:
1567 s += cpuset_sprintf_cpulist(s, cs);
1568 break;
1569 case FILE_MEMLIST:
1570 s += cpuset_sprintf_memlist(s, cs);
1571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 default:
1573 retval = -EINVAL;
1574 goto out;
1575 }
1576 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Al Viroeacaa1f2005-09-30 03:26:43 +01001578 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579out:
1580 free_page((unsigned long)page);
1581 return retval;
1582}
1583
Paul Menage700fe1a2008-04-29 01:00:00 -07001584static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1585{
1586 struct cpuset *cs = cgroup_cs(cont);
1587 cpuset_filetype_t type = cft->private;
1588 switch (type) {
1589 case FILE_CPU_EXCLUSIVE:
1590 return is_cpu_exclusive(cs);
1591 case FILE_MEM_EXCLUSIVE:
1592 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001593 case FILE_MEM_HARDWALL:
1594 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001595 case FILE_SCHED_LOAD_BALANCE:
1596 return is_sched_load_balance(cs);
1597 case FILE_MEMORY_MIGRATE:
1598 return is_memory_migrate(cs);
1599 case FILE_MEMORY_PRESSURE_ENABLED:
1600 return cpuset_memory_pressure_enabled;
1601 case FILE_MEMORY_PRESSURE:
1602 return fmeter_getrate(&cs->fmeter);
1603 case FILE_SPREAD_PAGE:
1604 return is_spread_page(cs);
1605 case FILE_SPREAD_SLAB:
1606 return is_spread_slab(cs);
1607 default:
1608 BUG();
1609 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001610
1611 /* Unreachable but makes gcc happy */
1612 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Paul Menage5be7a472008-05-06 20:42:41 -07001615static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1616{
1617 struct cpuset *cs = cgroup_cs(cont);
1618 cpuset_filetype_t type = cft->private;
1619 switch (type) {
1620 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1621 return cs->relax_domain_level;
1622 default:
1623 BUG();
1624 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001625
1626 /* Unrechable but makes gcc happy */
1627 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631/*
1632 * for the common functions, 'private' gives the type of file
1633 */
1634
Paul Menageaddf2c72008-04-29 01:00:26 -07001635static struct cftype files[] = {
1636 {
1637 .name = "cpus",
1638 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001639 .write_string = cpuset_write_resmask,
1640 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001641 .private = FILE_CPULIST,
1642 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Paul Menageaddf2c72008-04-29 01:00:26 -07001644 {
1645 .name = "mems",
1646 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001647 .write_string = cpuset_write_resmask,
1648 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001649 .private = FILE_MEMLIST,
1650 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Paul Menageaddf2c72008-04-29 01:00:26 -07001652 {
1653 .name = "cpu_exclusive",
1654 .read_u64 = cpuset_read_u64,
1655 .write_u64 = cpuset_write_u64,
1656 .private = FILE_CPU_EXCLUSIVE,
1657 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Paul Menageaddf2c72008-04-29 01:00:26 -07001659 {
1660 .name = "mem_exclusive",
1661 .read_u64 = cpuset_read_u64,
1662 .write_u64 = cpuset_write_u64,
1663 .private = FILE_MEM_EXCLUSIVE,
1664 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Paul Menageaddf2c72008-04-29 01:00:26 -07001666 {
Paul Menage78608362008-04-29 01:00:26 -07001667 .name = "mem_hardwall",
1668 .read_u64 = cpuset_read_u64,
1669 .write_u64 = cpuset_write_u64,
1670 .private = FILE_MEM_HARDWALL,
1671 },
1672
1673 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001674 .name = "sched_load_balance",
1675 .read_u64 = cpuset_read_u64,
1676 .write_u64 = cpuset_write_u64,
1677 .private = FILE_SCHED_LOAD_BALANCE,
1678 },
Paul Jackson029190c2007-10-18 23:40:20 -07001679
Paul Menageaddf2c72008-04-29 01:00:26 -07001680 {
1681 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001682 .read_s64 = cpuset_read_s64,
1683 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001684 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1685 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001686
Paul Menageaddf2c72008-04-29 01:00:26 -07001687 {
1688 .name = "memory_migrate",
1689 .read_u64 = cpuset_read_u64,
1690 .write_u64 = cpuset_write_u64,
1691 .private = FILE_MEMORY_MIGRATE,
1692 },
1693
1694 {
1695 .name = "memory_pressure",
1696 .read_u64 = cpuset_read_u64,
1697 .write_u64 = cpuset_write_u64,
1698 .private = FILE_MEMORY_PRESSURE,
Li Zefan099fca32009-04-02 16:57:29 -07001699 .mode = S_IRUGO,
Paul Menageaddf2c72008-04-29 01:00:26 -07001700 },
1701
1702 {
1703 .name = "memory_spread_page",
1704 .read_u64 = cpuset_read_u64,
1705 .write_u64 = cpuset_write_u64,
1706 .private = FILE_SPREAD_PAGE,
1707 },
1708
1709 {
1710 .name = "memory_spread_slab",
1711 .read_u64 = cpuset_read_u64,
1712 .write_u64 = cpuset_write_u64,
1713 .private = FILE_SPREAD_SLAB,
1714 },
Paul Jackson45b07ef2006-01-08 01:00:56 -08001715};
1716
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001717static struct cftype cft_memory_pressure_enabled = {
1718 .name = "memory_pressure_enabled",
Paul Menage700fe1a2008-04-29 01:00:00 -07001719 .read_u64 = cpuset_read_u64,
1720 .write_u64 = cpuset_write_u64,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001721 .private = FILE_MEMORY_PRESSURE_ENABLED,
1722};
1723
Paul Menage8793d852007-10-18 23:39:39 -07001724static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 int err;
1727
Paul Menageaddf2c72008-04-29 01:00:26 -07001728 err = cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
1729 if (err)
Paul Jackson825a46a2006-03-24 03:16:03 -08001730 return err;
Paul Menage8793d852007-10-18 23:39:39 -07001731 /* memory_pressure_enabled is in root cpuset only */
Paul Menageaddf2c72008-04-29 01:00:26 -07001732 if (!cont->parent)
Paul Menage8793d852007-10-18 23:39:39 -07001733 err = cgroup_add_file(cont, ss,
Paul Menageaddf2c72008-04-29 01:00:26 -07001734 &cft_memory_pressure_enabled);
1735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738/*
Paul Menage8793d852007-10-18 23:39:39 -07001739 * post_clone() is called at the end of cgroup_clone().
1740 * 'cgroup' was just created automatically as a result of
1741 * a cgroup_clone(), and the current task is about to
1742 * be moved into 'cgroup'.
1743 *
1744 * Currently we refuse to set up the cgroup - thereby
1745 * refusing the task to be entered, and as a result refusing
1746 * the sys_unshare() or clone() which initiated it - if any
1747 * sibling cpusets have exclusive cpus or mem.
1748 *
1749 * If this becomes a problem for some users who wish to
1750 * allow that scenario, then cpuset_post_clone() could be
1751 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
Paul Menage2df167a2008-02-07 00:14:45 -08001752 * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
1753 * held.
Paul Menage8793d852007-10-18 23:39:39 -07001754 */
1755static void cpuset_post_clone(struct cgroup_subsys *ss,
1756 struct cgroup *cgroup)
1757{
1758 struct cgroup *parent, *child;
1759 struct cpuset *cs, *parent_cs;
1760
1761 parent = cgroup->parent;
1762 list_for_each_entry(child, &parent->children, sibling) {
1763 cs = cgroup_cs(child);
1764 if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
1765 return;
1766 }
1767 cs = cgroup_cs(cgroup);
1768 parent_cs = cgroup_cs(parent);
1769
1770 cs->mems_allowed = parent_cs->mems_allowed;
Li Zefan300ed6c2009-01-07 18:08:44 -08001771 cpumask_copy(cs->cpus_allowed, parent_cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001772 return;
1773}
1774
1775/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * cpuset_create - create a cpuset
Paul Menage2df167a2008-02-07 00:14:45 -08001777 * ss: cpuset cgroup subsystem
1778 * cont: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
1780
Paul Menage8793d852007-10-18 23:39:39 -07001781static struct cgroup_subsys_state *cpuset_create(
1782 struct cgroup_subsys *ss,
1783 struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct cpuset *cs;
Paul Menage8793d852007-10-18 23:39:39 -07001786 struct cpuset *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Paul Menage8793d852007-10-18 23:39:39 -07001788 if (!cont->parent) {
1789 /* This is early initialization for the top cgroup */
1790 top_cpuset.mems_generation = cpuset_mems_generation++;
1791 return &top_cpuset.css;
1792 }
1793 parent = cgroup_cs(cont->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1795 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001796 return ERR_PTR(-ENOMEM);
Li Zefan300ed6c2009-01-07 18:08:44 -08001797 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1798 kfree(cs);
1799 return ERR_PTR(-ENOMEM);
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001802 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 cs->flags = 0;
Paul Jackson825a46a2006-03-24 03:16:03 -08001804 if (is_spread_page(parent))
1805 set_bit(CS_SPREAD_PAGE, &cs->flags);
1806 if (is_spread_slab(parent))
1807 set_bit(CS_SPREAD_SLAB, &cs->flags);
Paul Jackson029190c2007-10-18 23:40:20 -07001808 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001809 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001810 nodes_clear(cs->mems_allowed);
Paul Jackson151a4422006-03-24 03:16:11 -08001811 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001812 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001813 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 cs->parent = parent;
Paul Jackson202f72d2006-01-08 01:01:57 -08001816 number_of_cpusets++;
Paul Menage8793d852007-10-18 23:39:39 -07001817 return &cs->css ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Paul Jackson029190c2007-10-18 23:40:20 -07001820/*
Paul Jackson029190c2007-10-18 23:40:20 -07001821 * If the cpuset being removed has its flag 'sched_load_balance'
1822 * enabled, then simulate turning sched_load_balance off, which
Max Krasnyanskycf417142008-08-11 14:33:53 -07001823 * will call async_rebuild_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -07001824 */
1825
Paul Menage8793d852007-10-18 23:39:39 -07001826static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Paul Menage8793d852007-10-18 23:39:39 -07001828 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Paul Jacksoncf2a473c2006-01-08 01:01:54 -08001830 cpuset_update_task_memory_state();
Paul Jackson029190c2007-10-18 23:40:20 -07001831
1832 if (is_sched_load_balance(cs))
Paul Menage700fe1a2008-04-29 01:00:00 -07001833 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
Paul Jackson029190c2007-10-18 23:40:20 -07001834
Paul Jackson202f72d2006-01-08 01:01:57 -08001835 number_of_cpusets--;
Li Zefan300ed6c2009-01-07 18:08:44 -08001836 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001837 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Paul Menage8793d852007-10-18 23:39:39 -07001840struct cgroup_subsys cpuset_subsys = {
1841 .name = "cpuset",
1842 .create = cpuset_create,
Max Krasnyanskycf417142008-08-11 14:33:53 -07001843 .destroy = cpuset_destroy,
Paul Menage8793d852007-10-18 23:39:39 -07001844 .can_attach = cpuset_can_attach,
1845 .attach = cpuset_attach,
1846 .populate = cpuset_populate,
1847 .post_clone = cpuset_post_clone,
1848 .subsys_id = cpuset_subsys_id,
1849 .early_init = 1,
1850};
1851
Paul Jacksonc417f022006-01-08 01:02:01 -08001852/*
1853 * cpuset_init_early - just enough so that the calls to
1854 * cpuset_update_task_memory_state() in early init code
1855 * are harmless.
1856 */
1857
1858int __init cpuset_init_early(void)
1859{
Li Zefan300ed6c2009-01-07 18:08:44 -08001860 alloc_bootmem_cpumask_var(&top_cpuset.cpus_allowed);
1861
Paul Menage8793d852007-10-18 23:39:39 -07001862 top_cpuset.mems_generation = cpuset_mems_generation++;
Paul Jacksonc417f022006-01-08 01:02:01 -08001863 return 0;
1864}
1865
Paul Menage8793d852007-10-18 23:39:39 -07001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/**
1868 * cpuset_init - initialize cpusets at system boot
1869 *
1870 * Description: Initialize top_cpuset and the cpuset internal file system,
1871 **/
1872
1873int __init cpuset_init(void)
1874{
Paul Menage8793d852007-10-18 23:39:39 -07001875 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Li Zefan300ed6c2009-01-07 18:08:44 -08001877 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001878 nodes_setall(top_cpuset.mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001880 fmeter_init(&top_cpuset.fmeter);
Paul Jackson151a4422006-03-24 03:16:11 -08001881 top_cpuset.mems_generation = cpuset_mems_generation++;
Paul Jackson029190c2007-10-18 23:40:20 -07001882 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001883 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 err = register_filesystem(&cpuset_fs_type);
1886 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07001887 return err;
1888
Li Zefan2341d1b2009-01-07 18:08:42 -08001889 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1890 BUG();
1891
Paul Jackson202f72d2006-01-08 01:01:57 -08001892 number_of_cpusets = 1;
Paul Menage8793d852007-10-18 23:39:39 -07001893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Cliff Wickman956db3c2008-02-07 00:14:43 -08001896/**
1897 * cpuset_do_move_task - move a given task to another cpuset
1898 * @tsk: pointer to task_struct the task to move
1899 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1900 *
1901 * Called by cgroup_scan_tasks() for each task in a cgroup.
1902 * Return nonzero to stop the walk through the tasks.
1903 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -07001904static void cpuset_do_move_task(struct task_struct *tsk,
1905 struct cgroup_scanner *scan)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001906{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001907 struct cgroup *new_cgroup = scan->data;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001908
Li Zefan7f81b1a2009-04-02 16:57:53 -07001909 cgroup_attach_task(new_cgroup, tsk);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001910}
1911
1912/**
1913 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
1914 * @from: cpuset in which the tasks currently reside
1915 * @to: cpuset to which the tasks will be moved
1916 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001917 * Called with cgroup_mutex held
1918 * callback_mutex must not be held, as cpuset_attach() will take it.
Cliff Wickman956db3c2008-02-07 00:14:43 -08001919 *
1920 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1921 * calling callback functions for each.
1922 */
1923static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
1924{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001925 struct cgroup_scanner scan;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001926
Li Zefan7f81b1a2009-04-02 16:57:53 -07001927 scan.cg = from->css.cgroup;
1928 scan.test_task = NULL; /* select all tasks in cgroup */
1929 scan.process_task = cpuset_do_move_task;
1930 scan.heap = NULL;
1931 scan.data = to->css.cgroup;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001932
Li Zefan7f81b1a2009-04-02 16:57:53 -07001933 if (cgroup_scan_tasks(&scan))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001934 printk(KERN_ERR "move_member_tasks_to_cpuset: "
1935 "cgroup_scan_tasks failed\n");
1936}
1937
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001938/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07001939 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001940 * or memory nodes, we need to walk over the cpuset hierarchy,
1941 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001942 * last CPU or node from a cpuset, then move the tasks in the empty
1943 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001944 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001945 * Called with cgroup_mutex held
1946 * callback_mutex must not be held, as cpuset_attach() will take it.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001947 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001948static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001949{
Cliff Wickman956db3c2008-02-07 00:14:43 -08001950 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001951
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001952 /*
1953 * The cgroup's css_sets list is in use if there are tasks
1954 * in the cpuset; the list is empty if there are none;
1955 * the cs->css.refcnt seems always 0.
1956 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001957 if (list_empty(&cs->css.cgroup->css_sets))
1958 return;
1959
1960 /*
1961 * Find its next-highest non-empty parent, (top cpuset
1962 * has online cpus, so can't be empty).
1963 */
1964 parent = cs->parent;
Li Zefan300ed6c2009-01-07 18:08:44 -08001965 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08001966 nodes_empty(parent->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001967 parent = parent->parent;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001968
1969 move_member_tasks_to_cpuset(cs, parent);
1970}
1971
1972/*
1973 * Walk the specified cpuset subtree and look for empty cpusets.
1974 * The tasks of such cpuset must be moved to a parent cpuset.
1975 *
Paul Menage2df167a2008-02-07 00:14:45 -08001976 * Called with cgroup_mutex held. We take callback_mutex to modify
Cliff Wickman956db3c2008-02-07 00:14:43 -08001977 * cpus_allowed and mems_allowed.
1978 *
1979 * This walk processes the tree from top to bottom, completing one layer
1980 * before dropping down to the next. It always processes a node before
1981 * any of its children.
1982 *
1983 * For now, since we lack memory hot unplug, we'll never see a cpuset
1984 * that has tasks along with an empty 'mems'. But if we did see such
1985 * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
1986 */
Frederic Weisbeckerd294eb82008-10-03 12:10:10 +02001987static void scan_for_empty_cpusets(struct cpuset *root)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001988{
Li Zefan8d1e6262008-07-29 22:33:21 -07001989 LIST_HEAD(queue);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001990 struct cpuset *cp; /* scans cpusets being updated */
1991 struct cpuset *child; /* scans child cpusets of cp */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001992 struct cgroup *cont;
Miao Xief9b4fb82008-07-25 01:47:22 -07001993 nodemask_t oldmems;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001994
Cliff Wickman956db3c2008-02-07 00:14:43 -08001995 list_add_tail((struct list_head *)&root->stack_list, &queue);
1996
Cliff Wickman956db3c2008-02-07 00:14:43 -08001997 while (!list_empty(&queue)) {
Li Zefan8d1e6262008-07-29 22:33:21 -07001998 cp = list_first_entry(&queue, struct cpuset, stack_list);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001999 list_del(queue.next);
2000 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
2001 child = cgroup_cs(cont);
2002 list_add_tail(&child->stack_list, &queue);
2003 }
Paul Jacksonb4501292008-02-07 00:14:47 -08002004
2005 /* Continue past cpusets with all cpus, mems online */
Li Zefan300ed6c2009-01-07 18:08:44 -08002006 if (cpumask_subset(cp->cpus_allowed, cpu_online_mask) &&
Paul Jacksonb4501292008-02-07 00:14:47 -08002007 nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
2008 continue;
2009
Miao Xief9b4fb82008-07-25 01:47:22 -07002010 oldmems = cp->mems_allowed;
2011
Cliff Wickman956db3c2008-02-07 00:14:43 -08002012 /* Remove offline cpus and mems from this cpuset. */
Paul Jacksonb4501292008-02-07 00:14:47 -08002013 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08002014 cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
2015 cpu_online_mask);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002016 nodes_and(cp->mems_allowed, cp->mems_allowed,
2017 node_states[N_HIGH_MEMORY]);
Paul Jacksonb4501292008-02-07 00:14:47 -08002018 mutex_unlock(&callback_mutex);
2019
2020 /* Move tasks from the empty cpuset to a parent */
Li Zefan300ed6c2009-01-07 18:08:44 -08002021 if (cpumask_empty(cp->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002022 nodes_empty(cp->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002023 remove_tasks_in_empty_cpuset(cp);
Miao Xief9b4fb82008-07-25 01:47:22 -07002024 else {
Li Zefan4e743392008-09-13 02:33:08 -07002025 update_tasks_cpumask(cp, NULL);
Li Zefan010cfac2009-04-02 16:57:52 -07002026 update_tasks_nodemask(cp, &oldmems, NULL);
Miao Xief9b4fb82008-07-25 01:47:22 -07002027 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002028 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002029}
2030
2031/*
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002032 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2033 * period. This is necessary in order to make cpusets transparent
2034 * (of no affect) on systems that are actively using CPU hotplug
2035 * but making no active use of cpusets.
2036 *
Paul Jackson38837fc2006-09-29 02:01:16 -07002037 * This routine ensures that top_cpuset.cpus_allowed tracks
2038 * cpu_online_map on each CPU hotplug (cpuhp) event.
Max Krasnyanskycf417142008-08-11 14:33:53 -07002039 *
2040 * Called within get_online_cpus(). Needs to call cgroup_lock()
2041 * before calling generate_sched_domains().
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002042 */
Max Krasnyanskycf417142008-08-11 14:33:53 -07002043static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
Paul Jackson029190c2007-10-18 23:40:20 -07002044 unsigned long phase, void *unused_cpu)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002045{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002046 struct sched_domain_attr *attr;
Li Zefan6af866a2009-01-07 18:08:45 -08002047 struct cpumask *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002048 int ndoms;
2049
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002050 switch (phase) {
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002051 case CPU_ONLINE:
2052 case CPU_ONLINE_FROZEN:
2053 case CPU_DEAD:
2054 case CPU_DEAD_FROZEN:
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002055 break;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002056
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002057 default:
Avi Kivityac076752007-05-24 12:33:15 +03002058 return NOTIFY_DONE;
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002059 }
Avi Kivityac076752007-05-24 12:33:15 +03002060
Max Krasnyanskycf417142008-08-11 14:33:53 -07002061 cgroup_lock();
Li Zefan0b4217b2009-04-02 16:57:49 -07002062 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08002063 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
Li Zefan0b4217b2009-04-02 16:57:49 -07002064 mutex_unlock(&callback_mutex);
Max Krasnyanskycf417142008-08-11 14:33:53 -07002065 scan_for_empty_cpusets(&top_cpuset);
2066 ndoms = generate_sched_domains(&doms, &attr);
2067 cgroup_unlock();
2068
2069 /* Have scheduler rebuild the domains */
2070 partition_sched_domains(ndoms, doms, attr);
2071
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002072 return NOTIFY_OK;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002073}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002074
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002075#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002076/*
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002077 * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
Max Krasnyanskycf417142008-08-11 14:33:53 -07002078 * Call this routine anytime after node_states[N_HIGH_MEMORY] changes.
2079 * See also the previous routine cpuset_track_online_cpus().
Paul Jackson38837fc2006-09-29 02:01:16 -07002080 */
Miao Xief4818912008-11-19 15:36:30 -08002081static int cpuset_track_online_nodes(struct notifier_block *self,
2082 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002083{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002084 cgroup_lock();
Miao Xief4818912008-11-19 15:36:30 -08002085 switch (action) {
2086 case MEM_ONLINE:
Miao Xief4818912008-11-19 15:36:30 -08002087 case MEM_OFFLINE:
Li Zefan0b4217b2009-04-02 16:57:49 -07002088 mutex_lock(&callback_mutex);
Miao Xief4818912008-11-19 15:36:30 -08002089 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
Li Zefan0b4217b2009-04-02 16:57:49 -07002090 mutex_unlock(&callback_mutex);
2091 if (action == MEM_OFFLINE)
2092 scan_for_empty_cpusets(&top_cpuset);
Miao Xief4818912008-11-19 15:36:30 -08002093 break;
2094 default:
2095 break;
2096 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002097 cgroup_unlock();
Miao Xief4818912008-11-19 15:36:30 -08002098 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002099}
2100#endif
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102/**
2103 * cpuset_init_smp - initialize cpus_allowed
2104 *
2105 * Description: Finish top cpuset after cpu, node maps are initialized
2106 **/
2107
2108void __init cpuset_init_smp(void)
2109{
Li Zefan300ed6c2009-01-07 18:08:44 -08002110 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002111 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002112
Max Krasnyanskycf417142008-08-11 14:33:53 -07002113 hotcpu_notifier(cpuset_track_online_cpus, 0);
Miao Xief4818912008-11-19 15:36:30 -08002114 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
Miao Xief90d4112009-01-16 10:24:10 +08002115
2116 cpuset_wq = create_singlethread_workqueue("cpuset");
2117 BUG_ON(!cpuset_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2122 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002123 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002125 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 * attached to the specified @tsk. Guaranteed to return some non-empty
2127 * subset of cpu_online_map, even if this means going outside the
2128 * tasks cpuset.
2129 **/
2130
Li Zefan6af866a2009-01-07 18:08:45 -08002131void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002133 mutex_lock(&callback_mutex);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002134 cpuset_cpus_allowed_locked(tsk, pmask);
Cliff Wickman470fd642007-10-18 23:40:46 -07002135 mutex_unlock(&callback_mutex);
Cliff Wickman470fd642007-10-18 23:40:46 -07002136}
2137
2138/**
2139 * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
Paul Menage2df167a2008-02-07 00:14:45 -08002140 * Must be called with callback_mutex held.
Cliff Wickman470fd642007-10-18 23:40:46 -07002141 **/
Li Zefan6af866a2009-01-07 18:08:45 -08002142void cpuset_cpus_allowed_locked(struct task_struct *tsk, struct cpumask *pmask)
Cliff Wickman470fd642007-10-18 23:40:46 -07002143{
Paul Jackson909d75a2006-01-08 01:01:55 -08002144 task_lock(tsk);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002145 guarantee_online_cpus(task_cs(tsk), pmask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002146 task_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149void cpuset_init_current_mems_allowed(void)
2150{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002151 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002154/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002155 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2156 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2157 *
2158 * Description: Returns the nodemask_t mems_allowed of the cpuset
2159 * attached to the specified @tsk. Guaranteed to return some non-empty
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002160 * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002161 * tasks cpuset.
2162 **/
2163
2164nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2165{
2166 nodemask_t mask;
2167
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002168 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002169 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -07002170 guarantee_online_mems(task_cs(tsk), &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002171 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002172 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002173
2174 return mask;
2175}
2176
2177/**
Mel Gorman19770b32008-04-28 02:12:18 -07002178 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2179 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002180 *
Mel Gorman19770b32008-04-28 02:12:18 -07002181 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 */
Mel Gorman19770b32008-04-28 02:12:18 -07002183int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Mel Gorman19770b32008-04-28 02:12:18 -07002185 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Paul Jackson9bf22292005-09-06 15:18:12 -07002188/*
Paul Menage78608362008-04-29 01:00:26 -07002189 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2190 * mem_hardwall ancestor to the specified cpuset. Call holding
2191 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2192 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 */
Paul Menage78608362008-04-29 01:00:26 -07002194static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Paul Menage78608362008-04-29 01:00:26 -07002196 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
Paul Jackson9bf22292005-09-06 15:18:12 -07002197 cs = cs->parent;
2198 return cs;
2199}
2200
2201/**
David Rientjesa1bc5a42009-04-02 16:57:54 -07002202 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2203 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002204 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002205 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002206 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2207 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2208 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2209 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2210 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2211 * flag, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002212 * Otherwise, no.
2213 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002214 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2215 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2216 * might sleep, and might allow a node from an enclosing cpuset.
Paul Jackson02a0e532006-12-13 00:34:25 -08002217 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002218 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2219 * cpusets, and never sleeps.
Paul Jackson02a0e532006-12-13 00:34:25 -08002220 *
2221 * The __GFP_THISNODE placement logic is really handled elsewhere,
2222 * by forcibly using a zonelist starting at a specified node, and by
2223 * (in get_page_from_freelist()) refusing to consider the zones for
2224 * any node on the zonelist except the first. By the time any such
2225 * calls get to this routine, we should just shut up and say 'yes'.
2226 *
Paul Jackson9bf22292005-09-06 15:18:12 -07002227 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002228 * and do not allow allocations outside the current tasks cpuset
2229 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002230 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002231 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002232 *
Paul Jackson02a0e532006-12-13 00:34:25 -08002233 * Scanning up parent cpusets requires callback_mutex. The
2234 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2235 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2236 * current tasks mems_allowed came up empty on the first pass over
2237 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2238 * cpuset are short of memory, might require taking the callback_mutex
2239 * mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002240 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002241 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002242 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2243 * so no allocation on a node outside the cpuset is allowed (unless
2244 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002245 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002246 * The second pass through get_page_from_freelist() doesn't even call
2247 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2248 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2249 * in alloc_flags. That logic and the checks below have the combined
2250 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002251 * in_interrupt - any node ok (current task context irrelevant)
2252 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002253 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002254 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002255 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002256 *
2257 * Rule:
David Rientjesa1bc5a42009-04-02 16:57:54 -07002258 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
Paul Jackson36be57f2006-05-20 15:00:10 -07002259 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2260 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson02a0e532006-12-13 00:34:25 -08002261 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002262int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002263{
Paul Jackson9bf22292005-09-06 15:18:12 -07002264 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002265 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002266
Christoph Lameter9b819d22006-09-25 23:31:40 -07002267 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002268 return 1;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002269 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002270 if (node_isset(node, current->mems_allowed))
2271 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002272 /*
2273 * Allow tasks that have access to memory reserves because they have
2274 * been OOM killed to get memory anywhere.
2275 */
2276 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2277 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002278 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2279 return 0;
2280
Bob Picco5563e772005-11-13 16:06:35 -08002281 if (current->flags & PF_EXITING) /* Let dying task have memory */
2282 return 1;
2283
Paul Jackson9bf22292005-09-06 15:18:12 -07002284 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002285 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002286
Paul Jackson053199e2005-10-30 15:02:30 -08002287 task_lock(current);
Paul Menage78608362008-04-29 01:00:26 -07002288 cs = nearest_hardwall_ancestor(task_cs(current));
Paul Jackson053199e2005-10-30 15:02:30 -08002289 task_unlock(current);
2290
Paul Jackson9bf22292005-09-06 15:18:12 -07002291 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002292 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002293 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Paul Jackson02a0e532006-12-13 00:34:25 -08002296/*
David Rientjesa1bc5a42009-04-02 16:57:54 -07002297 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2298 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002299 * @gfp_mask: memory allocation flags
2300 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002301 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2302 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2303 * yes. If the task has been OOM killed and has access to memory reserves as
2304 * specified by the TIF_MEMDIE flag, yes.
2305 * Otherwise, no.
Paul Jackson02a0e532006-12-13 00:34:25 -08002306 *
2307 * The __GFP_THISNODE placement logic is really handled elsewhere,
2308 * by forcibly using a zonelist starting at a specified node, and by
2309 * (in get_page_from_freelist()) refusing to consider the zones for
2310 * any node on the zonelist except the first. By the time any such
2311 * calls get to this routine, we should just shut up and say 'yes'.
2312 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002313 * Unlike the cpuset_node_allowed_softwall() variant, above,
2314 * this variant requires that the node be in the current task's
Paul Jackson02a0e532006-12-13 00:34:25 -08002315 * mems_allowed or that we're in interrupt. It does not scan up the
2316 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2317 * It never sleeps.
2318 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002319int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -08002320{
Paul Jackson02a0e532006-12-13 00:34:25 -08002321 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2322 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002323 if (node_isset(node, current->mems_allowed))
2324 return 1;
Daniel Walkerdedf8b72007-10-18 03:06:04 -07002325 /*
2326 * Allow tasks that have access to memory reserves because they have
2327 * been OOM killed to get memory anywhere.
2328 */
2329 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2330 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002331 return 0;
2332}
2333
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002334/**
Paul Jackson505970b2006-01-14 13:21:06 -08002335 * cpuset_lock - lock out any changes to cpuset structures
2336 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002337 * The out of memory (oom) code needs to mutex_lock cpusets
Paul Jackson505970b2006-01-14 13:21:06 -08002338 * from being changed while it scans the tasklist looking for a
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002339 * task in an overlapping cpuset. Expose callback_mutex via this
Paul Jackson505970b2006-01-14 13:21:06 -08002340 * cpuset_lock() routine, so the oom code can lock it, before
2341 * locking the task list. The tasklist_lock is a spinlock, so
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002342 * must be taken inside callback_mutex.
Paul Jackson505970b2006-01-14 13:21:06 -08002343 */
2344
2345void cpuset_lock(void)
2346{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002347 mutex_lock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002348}
2349
2350/**
2351 * cpuset_unlock - release lock on cpuset changes
2352 *
2353 * Undo the lock taken in a previous cpuset_lock() call.
2354 */
2355
2356void cpuset_unlock(void)
2357{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002358 mutex_unlock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002359}
2360
2361/**
Paul Jackson825a46a2006-03-24 03:16:03 -08002362 * cpuset_mem_spread_node() - On which node to begin search for a page
2363 *
2364 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2365 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2366 * and if the memory allocation used cpuset_mem_spread_node()
2367 * to determine on which node to start looking, as it will for
2368 * certain page cache or slab cache pages such as used for file
2369 * system buffers and inode caches, then instead of starting on the
2370 * local node to look for a free page, rather spread the starting
2371 * node around the tasks mems_allowed nodes.
2372 *
2373 * We don't have to worry about the returned node being offline
2374 * because "it can't happen", and even if it did, it would be ok.
2375 *
2376 * The routines calling guarantee_online_mems() are careful to
2377 * only set nodes in task->mems_allowed that are online. So it
2378 * should not be possible for the following code to return an
2379 * offline node. But if it did, that would be ok, as this routine
2380 * is not returning the node where the allocation must be, only
2381 * the node where the search should start. The zonelist passed to
2382 * __alloc_pages() will include all nodes. If the slab allocator
2383 * is passed an offline node, it will fall back to the local node.
2384 * See kmem_cache_alloc_node().
2385 */
2386
2387int cpuset_mem_spread_node(void)
2388{
2389 int node;
2390
2391 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2392 if (node == MAX_NUMNODES)
2393 node = first_node(current->mems_allowed);
2394 current->cpuset_mem_spread_rotor = node;
2395 return node;
2396}
2397EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2398
2399/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002400 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2401 * @tsk1: pointer to task_struct of some task.
2402 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002403 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002404 * Description: Return true if @tsk1's mems_allowed intersects the
2405 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2406 * one of the task's memory usage might impact the memory available
2407 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002408 **/
2409
David Rientjesbbe373f2007-10-16 23:25:58 -07002410int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2411 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002412{
David Rientjesbbe373f2007-10-16 23:25:58 -07002413 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002414}
2415
David Rientjes75aa1992009-01-06 14:39:01 -08002416/**
2417 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2418 * @task: pointer to task_struct of some task.
2419 *
2420 * Description: Prints @task's name, cpuset name, and cached copy of its
2421 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2422 * dereferencing task_cs(task).
2423 */
2424void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2425{
2426 struct dentry *dentry;
2427
2428 dentry = task_cs(tsk)->css.cgroup->dentry;
2429 spin_lock(&cpuset_buffer_lock);
2430 snprintf(cpuset_name, CPUSET_NAME_LEN,
2431 dentry ? (const char *)dentry->d_name.name : "/");
2432 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2433 tsk->mems_allowed);
2434 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2435 tsk->comm, cpuset_name, cpuset_nodelist);
2436 spin_unlock(&cpuset_buffer_lock);
2437}
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002440 * Collection of memory_pressure is suppressed unless
2441 * this flag is enabled by writing "1" to the special
2442 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2443 */
2444
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002445int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002446
2447/**
2448 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2449 *
2450 * Keep a running average of the rate of synchronous (direct)
2451 * page reclaim efforts initiated by tasks in each cpuset.
2452 *
2453 * This represents the rate at which some task in the cpuset
2454 * ran low on memory on all nodes it was allowed to use, and
2455 * had to enter the kernels page reclaim code in an effort to
2456 * create more free memory by tossing clean pages or swapping
2457 * or writing dirty pages.
2458 *
2459 * Display to user space in the per-cpuset read-only file
2460 * "memory_pressure". Value displayed is an integer
2461 * representing the recent rate of entry into the synchronous
2462 * (direct) page reclaim by any task attached to the cpuset.
2463 **/
2464
2465void __cpuset_memory_pressure_bump(void)
2466{
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002467 task_lock(current);
Paul Menage8793d852007-10-18 23:39:39 -07002468 fmeter_markevent(&task_cs(current)->fmeter);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002469 task_unlock(current);
2470}
2471
Paul Menage8793d852007-10-18 23:39:39 -07002472#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002473/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 * proc_cpuset_show()
2475 * - Print tasks cpuset path into seq_file.
2476 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002477 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2478 * doesn't really matter if tsk->cpuset changes after we read it,
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002479 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002480 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 */
Paul Jackson029190c2007-10-18 23:40:20 -07002482static int proc_cpuset_show(struct seq_file *m, void *unused_v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002484 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 struct task_struct *tsk;
2486 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002487 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002488 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Eric W. Biederman99f89552006-06-26 00:25:55 -07002490 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2492 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Eric W. Biederman99f89552006-06-26 00:25:55 -07002495 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002496 pid = m->private;
2497 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002498 if (!tsk)
2499 goto out_free;
2500
2501 retval = -EINVAL;
Paul Menage8793d852007-10-18 23:39:39 -07002502 cgroup_lock();
2503 css = task_subsys_state(tsk, cpuset_subsys_id);
2504 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002506 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 seq_puts(m, buf);
2508 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002509out_unlock:
Paul Menage8793d852007-10-18 23:39:39 -07002510 cgroup_unlock();
Eric W. Biederman99f89552006-06-26 00:25:55 -07002511 put_task_struct(tsk);
2512out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return retval;
2516}
2517
2518static int cpuset_open(struct inode *inode, struct file *file)
2519{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002520 struct pid *pid = PROC_I(inode)->pid;
2521 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Arjan van de Ven9a321442007-02-12 00:55:35 -08002524const struct file_operations proc_cpuset_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 .open = cpuset_open,
2526 .read = seq_read,
2527 .llseek = seq_lseek,
2528 .release = single_release,
2529};
Paul Menage8793d852007-10-18 23:39:39 -07002530#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002533void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002535 seq_printf(m, "Cpus_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002536 seq_cpumask(m, &task->cpus_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002537 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002538 seq_printf(m, "Cpus_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002539 seq_cpumask_list(m, &task->cpus_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002540 seq_printf(m, "\n");
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002541 seq_printf(m, "Mems_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002542 seq_nodemask(m, &task->mems_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002543 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002544 seq_printf(m, "Mems_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002545 seq_nodemask_list(m, &task->mems_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002546 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}