blob: af5a83d52187b59b562d211e8f9c00d49f3ad731 [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
Miao Xief3b39d42009-06-16 15:31:46 -0700334/*
335 * update task's spread flag if cpuset's page/slab spread flag is set
336 *
337 * Called with callback_mutex/cgroup_mutex held
338 */
339static void cpuset_update_task_spread_flag(struct cpuset *cs,
340 struct task_struct *tsk)
341{
342 if (is_spread_page(cs))
343 tsk->flags |= PF_SPREAD_PAGE;
344 else
345 tsk->flags &= ~PF_SPREAD_PAGE;
346 if (is_spread_slab(cs))
347 tsk->flags |= PF_SPREAD_SLAB;
348 else
349 tsk->flags &= ~PF_SPREAD_SLAB;
350}
351
Paul Jacksoncf2a4732006-01-08 01:01:54 -0800352/**
353 * cpuset_update_task_memory_state - update task memory placement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *
Paul Jacksoncf2a4732006-01-08 01:01:54 -0800355 * If the current tasks cpusets mems_allowed changed behind our
356 * backs, update current->mems_allowed, mems_generation and task NUMA
357 * mempolicy to the new value.
358 *
359 * Task mempolicy is updated by rebinding it relative to the
360 * current->cpuset if a task has its memory placement changed.
361 * Do not call this routine if in_interrupt().
362 *
Paul Jackson4a01c8d2006-03-31 02:30:50 -0800363 * Call without callback_mutex or task_lock() held. May be
Paul Menage2df167a2008-02-07 00:14:45 -0800364 * called with or without cgroup_mutex held. Thanks in part to
365 * 'the_top_cpuset_hack', the task's cpuset pointer will never
David Rientjes41f7f602008-03-04 23:32:38 -0800366 * be NULL. This routine also might acquire callback_mutex during
367 * call.
Paul Jackson5aa15b52005-10-30 15:02:28 -0800368 *
Paul Jackson6b9c2602006-01-08 01:02:02 -0800369 * Reading current->cpuset->mems_generation doesn't need task_lock
370 * to guard the current->cpuset derefence, because it is guarded
Paul Menage2df167a2008-02-07 00:14:45 -0800371 * from concurrent freeing of current->cpuset using RCU.
Paul Jackson6b9c2602006-01-08 01:02:02 -0800372 *
373 * The rcu_dereference() is technically probably not needed,
374 * as I don't actually mind if I see a new cpuset pointer but
375 * an old value of mems_generation. However this really only
376 * matters on alpha systems using cpusets heavily. If I dropped
377 * that rcu_dereference(), it would save them a memory barrier.
378 * For all other arch's, rcu_dereference is a no-op anyway, and for
379 * alpha systems not using cpusets, another planned optimization,
380 * avoiding the rcu critical section for tasks in the root cpuset
381 * which is statically allocated, so can't vanish, will make this
382 * irrelevant. Better to use RCU as intended, than to engage in
383 * some cute trick to save a memory barrier that is impossible to
384 * test, for alpha systems using cpusets heavily, which might not
385 * even exist.
Paul Jackson053199e2005-10-30 15:02:30 -0800386 *
387 * This routine is needed to update the per-task mems_allowed data,
388 * within the tasks context, when it is trying to allocate memory
389 * (in various mm/mempolicy.c routines) and notices that some other
390 * task has been modifying its cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392
Randy Dunlapfe85a992006-02-03 03:04:23 -0800393void cpuset_update_task_memory_state(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Paul Jackson053199e2005-10-30 15:02:30 -0800395 int my_cpusets_mem_gen;
Paul Jacksoncf2a4732006-01-08 01:01:54 -0800396 struct task_struct *tsk = current;
Paul Jackson6b9c2602006-01-08 01:02:02 -0800397 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Lai Jiangshan13337712009-01-07 18:08:39 -0800399 rcu_read_lock();
400 my_cpusets_mem_gen = task_cs(tsk)->mems_generation;
401 rcu_read_unlock();
Paul Jacksoncf2a4732006-01-08 01:01:54 -0800402
403 if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800404 mutex_lock(&callback_mutex);
Paul Jacksoncf2a4732006-01-08 01:01:54 -0800405 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -0700406 cs = task_cs(tsk); /* Maybe changed when task not locked */
Paul Jacksoncf2a4732006-01-08 01:01:54 -0800407 guarantee_online_mems(cs, &tsk->mems_allowed);
408 tsk->cpuset_mems_generation = cs->mems_generation;
409 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800410 mutex_unlock(&callback_mutex);
Paul Jackson74cb2152006-01-08 01:01:56 -0800411 mpol_rebind_task(tsk, &tsk->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413}
414
415/*
416 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
417 *
418 * One cpuset is a subset of another if all its allowed CPUs and
419 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Menage2df167a2008-02-07 00:14:45 -0800420 * are only set if the other's are set. Call holding cgroup_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
422
423static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
424{
Li Zefan300ed6c2009-01-07 18:08:44 -0800425 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 nodes_subset(p->mems_allowed, q->mems_allowed) &&
427 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
428 is_mem_exclusive(p) <= is_mem_exclusive(q);
429}
430
Li Zefan645fcc92009-01-07 18:08:43 -0800431/**
432 * alloc_trial_cpuset - allocate a trial cpuset
433 * @cs: the cpuset that the trial cpuset duplicates
434 */
435static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
436{
Li Zefan300ed6c2009-01-07 18:08:44 -0800437 struct cpuset *trial;
438
439 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
440 if (!trial)
441 return NULL;
442
443 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
444 kfree(trial);
445 return NULL;
446 }
447 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
448
449 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800450}
451
452/**
453 * free_trial_cpuset - free the trial cpuset
454 * @trial: the trial cpuset to be freed
455 */
456static void free_trial_cpuset(struct cpuset *trial)
457{
Li Zefan300ed6c2009-01-07 18:08:44 -0800458 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800459 kfree(trial);
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
463 * validate_change() - Used to validate that any proposed cpuset change
464 * follows the structural rules for cpusets.
465 *
466 * If we replaced the flag and mask values of the current cpuset
467 * (cur) with those values in the trial cpuset (trial), would
468 * our various subset and exclusive rules still be valid? Presumes
Paul Menage2df167a2008-02-07 00:14:45 -0800469 * cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
471 * 'cur' is the address of an actual, in-use cpuset. Operations
472 * such as list traversal that depend on the actual address of the
473 * cpuset in the list must use cur below, not trial.
474 *
475 * 'trial' is the address of bulk structure copy of cur, with
476 * perhaps one or more of the fields cpus_allowed, mems_allowed,
477 * or flags changed to new, trial values.
478 *
479 * Return 0 if valid, -errno if not.
480 */
481
482static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
483{
Paul Menage8793d852007-10-18 23:39:39 -0700484 struct cgroup *cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct cpuset *c, *par;
486
487 /* Each of our child cpusets must be a subset of us */
Paul Menage8793d852007-10-18 23:39:39 -0700488 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
489 if (!is_cpuset_subset(cgroup_cs(cont), trial))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EBUSY;
491 }
492
493 /* Remaining checks don't apply to root cpuset */
Paul Jackson69604062006-12-06 20:36:15 -0800494 if (cur == &top_cpuset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496
Paul Jackson69604062006-12-06 20:36:15 -0800497 par = cur->parent;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* We must be a subset of our parent cpuset */
500 if (!is_cpuset_subset(trial, par))
501 return -EACCES;
502
Paul Menage2df167a2008-02-07 00:14:45 -0800503 /*
504 * If either I or some sibling (!= me) is exclusive, we can't
505 * overlap
506 */
Paul Menage8793d852007-10-18 23:39:39 -0700507 list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
508 c = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
510 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800511 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EINVAL;
513 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
514 c != cur &&
515 nodes_intersects(trial->mems_allowed, c->mems_allowed))
516 return -EINVAL;
517 }
518
Paul Jackson020958b2007-10-18 23:40:21 -0700519 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
520 if (cgroup_task_count(cur->css.cgroup)) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800521 if (cpumask_empty(trial->cpus_allowed) ||
Paul Jackson020958b2007-10-18 23:40:21 -0700522 nodes_empty(trial->mems_allowed)) {
523 return -ENOSPC;
524 }
525 }
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528}
529
Paul Menagedb7f47c2009-04-02 16:57:55 -0700530#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700531/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700532 * Helper routine for generate_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -0700533 * Do cpusets a, b have overlapping cpus_allowed masks?
534 */
Paul Jackson029190c2007-10-18 23:40:20 -0700535static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
536{
Li Zefan300ed6c2009-01-07 18:08:44 -0800537 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
Paul Jackson029190c2007-10-18 23:40:20 -0700538}
539
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900540static void
541update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
542{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900543 if (dattr->relax_domain_level < c->relax_domain_level)
544 dattr->relax_domain_level = c->relax_domain_level;
545 return;
546}
547
Lai Jiangshanf5393692008-07-29 22:33:22 -0700548static void
549update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
550{
551 LIST_HEAD(q);
552
553 list_add(&c->stack_list, &q);
554 while (!list_empty(&q)) {
555 struct cpuset *cp;
556 struct cgroup *cont;
557 struct cpuset *child;
558
559 cp = list_first_entry(&q, struct cpuset, stack_list);
560 list_del(q.next);
561
Li Zefan300ed6c2009-01-07 18:08:44 -0800562 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700563 continue;
564
565 if (is_sched_load_balance(cp))
566 update_domain_attr(dattr, cp);
567
568 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
569 child = cgroup_cs(cont);
570 list_add_tail(&child->stack_list, &q);
571 }
572 }
573}
574
Paul Jackson029190c2007-10-18 23:40:20 -0700575/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700576 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700577 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700578 * This function builds a partial partition of the systems CPUs
579 * A 'partial partition' is a set of non-overlapping subsets whose
580 * union is a subset of that set.
581 * The output of this function needs to be passed to kernel/sched.c
582 * partition_sched_domains() routine, which will rebuild the scheduler's
583 * load balancing domains (sched domains) as specified by that partial
584 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700585 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800586 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700587 * for a background explanation of this.
588 *
589 * Does not return errors, on the theory that the callers of this
590 * routine would rather not worry about failures to rebuild sched
591 * domains when operating in the severe memory shortage situations
592 * that could cause allocation failures below.
593 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700594 * Must be called with cgroup_lock held.
Paul Jackson029190c2007-10-18 23:40:20 -0700595 *
596 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700597 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700598 * top-down scan of all cpusets. This scan loads a pointer
599 * to each cpuset marked is_sched_load_balance into the
600 * array 'csa'. For our purposes, rebuilding the schedulers
601 * sched domains, we can ignore !is_sched_load_balance cpusets.
602 * csa - (for CpuSet Array) Array of pointers to all the cpusets
603 * that need to be load balanced, for convenient iterative
604 * access by the subsequent code that finds the best partition,
605 * i.e the set of domains (subsets) of CPUs such that the
606 * cpus_allowed of every cpuset marked is_sched_load_balance
607 * is a subset of one of these domains, while there are as
608 * many such domains as possible, each as small as possible.
609 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
610 * the kernel/sched.c routine partition_sched_domains() in a
611 * convenient format, that can be easily compared to the prior
612 * value to determine what partition elements (sched domains)
613 * were changed (added or removed.)
614 *
615 * Finding the best partition (set of domains):
616 * The triple nested loops below over i, j, k scan over the
617 * load balanced cpusets (using the array of cpuset pointers in
618 * csa[]) looking for pairs of cpusets that have overlapping
619 * cpus_allowed, but which don't have the same 'pn' partition
620 * number and gives them in the same partition number. It keeps
621 * looping on the 'restart' label until it can no longer find
622 * any such pairs.
623 *
624 * The union of the cpus_allowed masks from the set of
625 * all cpusets having the same 'pn' value then form the one
626 * element of the partition (one sched domain) to be passed to
627 * partition_sched_domains().
628 */
Li Zefan6af866a2009-01-07 18:08:45 -0800629/* FIXME: see the FIXME in partition_sched_domains() */
630static int generate_sched_domains(struct cpumask **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700631 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700632{
Max Krasnyanskycf417142008-08-11 14:33:53 -0700633 LIST_HEAD(q); /* queue of cpusets to be scanned */
Paul Jackson029190c2007-10-18 23:40:20 -0700634 struct cpuset *cp; /* scans q */
635 struct cpuset **csa; /* array of all cpuset ptrs */
636 int csn; /* how many cpuset ptrs in csa so far */
637 int i, j, k; /* indices for partition finding loops */
Li Zefan6af866a2009-01-07 18:08:45 -0800638 struct cpumask *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900639 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100640 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800641 int nslot; /* next empty doms[] struct cpumask slot */
Paul Jackson029190c2007-10-18 23:40:20 -0700642
Paul Jackson029190c2007-10-18 23:40:20 -0700643 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900644 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700645 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700646
647 /* Special case for the 99% of systems with one, full, sched domain */
648 if (is_sched_load_balance(&top_cpuset)) {
Li Zefan6af866a2009-01-07 18:08:45 -0800649 doms = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700650 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700651 goto done;
652
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900653 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
654 if (dattr) {
655 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700656 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900657 }
Li Zefan300ed6c2009-01-07 18:08:44 -0800658 cpumask_copy(doms, top_cpuset.cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700659
660 ndoms = 1;
661 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700662 }
663
Paul Jackson029190c2007-10-18 23:40:20 -0700664 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
665 if (!csa)
666 goto done;
667 csn = 0;
668
Li Zefanaeed6822008-07-29 22:33:24 -0700669 list_add(&top_cpuset.stack_list, &q);
670 while (!list_empty(&q)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700671 struct cgroup *cont;
672 struct cpuset *child; /* scans child cpusets of cp */
Lai Jiangshan489a5392008-07-25 01:47:23 -0700673
Li Zefanaeed6822008-07-29 22:33:24 -0700674 cp = list_first_entry(&q, struct cpuset, stack_list);
675 list_del(q.next);
676
Li Zefan300ed6c2009-01-07 18:08:44 -0800677 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshan489a5392008-07-25 01:47:23 -0700678 continue;
679
Lai Jiangshanf5393692008-07-29 22:33:22 -0700680 /*
681 * All child cpusets contain a subset of the parent's cpus, so
682 * just skip them, and then we call update_domain_attr_tree()
683 * to calc relax_domain_level of the corresponding sched
684 * domain.
685 */
686 if (is_sched_load_balance(cp)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700687 csa[csn++] = cp;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700688 continue;
689 }
Lai Jiangshan489a5392008-07-25 01:47:23 -0700690
Paul Jackson029190c2007-10-18 23:40:20 -0700691 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
692 child = cgroup_cs(cont);
Li Zefanaeed6822008-07-29 22:33:24 -0700693 list_add_tail(&child->stack_list, &q);
Paul Jackson029190c2007-10-18 23:40:20 -0700694 }
695 }
696
697 for (i = 0; i < csn; i++)
698 csa[i]->pn = i;
699 ndoms = csn;
700
701restart:
702 /* Find the best partition (set of sched domains) */
703 for (i = 0; i < csn; i++) {
704 struct cpuset *a = csa[i];
705 int apn = a->pn;
706
707 for (j = 0; j < csn; j++) {
708 struct cpuset *b = csa[j];
709 int bpn = b->pn;
710
711 if (apn != bpn && cpusets_overlap(a, b)) {
712 for (k = 0; k < csn; k++) {
713 struct cpuset *c = csa[k];
714
715 if (c->pn == bpn)
716 c->pn = apn;
717 }
718 ndoms--; /* one less element */
719 goto restart;
720 }
721 }
722 }
723
Max Krasnyanskycf417142008-08-11 14:33:53 -0700724 /*
725 * Now we know how many domains to create.
726 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
727 */
Li Zefan6af866a2009-01-07 18:08:45 -0800728 doms = kmalloc(ndoms * cpumask_size(), GFP_KERNEL);
Li Zefan700018e2008-11-18 14:02:03 +0800729 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700730 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700731
732 /*
733 * The rest of the code, including the scheduler, can deal with
734 * dattr==NULL case. No need to abort if alloc fails.
735 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900736 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700737
738 for (nslot = 0, i = 0; i < csn; i++) {
739 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800740 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700741 int apn = a->pn;
742
Max Krasnyanskycf417142008-08-11 14:33:53 -0700743 if (apn < 0) {
744 /* Skip completed partitions */
745 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700746 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700747
748 dp = doms + nslot;
749
750 if (nslot == ndoms) {
751 static int warnings = 10;
752 if (warnings) {
753 printk(KERN_WARNING
754 "rebuild_sched_domains confused:"
755 " nslot %d, ndoms %d, csn %d, i %d,"
756 " apn %d\n",
757 nslot, ndoms, csn, i, apn);
758 warnings--;
759 }
760 continue;
761 }
762
Li Zefan6af866a2009-01-07 18:08:45 -0800763 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700764 if (dattr)
765 *(dattr + nslot) = SD_ATTR_INIT;
766 for (j = i; j < csn; j++) {
767 struct cpuset *b = csa[j];
768
769 if (apn == b->pn) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800770 cpumask_or(dp, dp, b->cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700771 if (dattr)
772 update_domain_attr_tree(dattr + nslot, b);
773
774 /* Done with this partition */
775 b->pn = -1;
776 }
777 }
778 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700779 }
780 BUG_ON(nslot != ndoms);
781
Paul Jackson029190c2007-10-18 23:40:20 -0700782done:
Paul Jackson029190c2007-10-18 23:40:20 -0700783 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700784
Li Zefan700018e2008-11-18 14:02:03 +0800785 /*
786 * Fallback to the default domain if kmalloc() failed.
787 * See comments in partition_sched_domains().
788 */
789 if (doms == NULL)
790 ndoms = 1;
791
Max Krasnyanskycf417142008-08-11 14:33:53 -0700792 *domains = doms;
793 *attributes = dattr;
794 return ndoms;
795}
796
797/*
798 * Rebuild scheduler domains.
799 *
800 * Call with neither cgroup_mutex held nor within get_online_cpus().
801 * Takes both cgroup_mutex and get_online_cpus().
802 *
803 * Cannot be directly called from cpuset code handling changes
804 * to the cpuset pseudo-filesystem, because it cannot be called
805 * from code that already holds cgroup_mutex.
806 */
807static void do_rebuild_sched_domains(struct work_struct *unused)
808{
809 struct sched_domain_attr *attr;
Li Zefan6af866a2009-01-07 18:08:45 -0800810 struct cpumask *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700811 int ndoms;
812
813 get_online_cpus();
814
815 /* Generate domain masks and attrs */
816 cgroup_lock();
817 ndoms = generate_sched_domains(&doms, &attr);
818 cgroup_unlock();
819
820 /* Have scheduler rebuild the domains */
821 partition_sched_domains(ndoms, doms, attr);
822
823 put_online_cpus();
824}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700825#else /* !CONFIG_SMP */
826static void do_rebuild_sched_domains(struct work_struct *unused)
827{
828}
829
830static int generate_sched_domains(struct cpumask **domains,
831 struct sched_domain_attr **attributes)
832{
833 *domains = NULL;
834 return 1;
835}
836#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700837
838static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
839
840/*
841 * Rebuild scheduler domains, asynchronously via workqueue.
842 *
843 * If the flag 'sched_load_balance' of any cpuset with non-empty
844 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
845 * which has that flag enabled, or if any cpuset with a non-empty
846 * 'cpus' is removed, then call this routine to rebuild the
847 * scheduler's dynamic sched domains.
848 *
849 * The rebuild_sched_domains() and partition_sched_domains()
850 * routines must nest cgroup_lock() inside get_online_cpus(),
851 * but such cpuset changes as these must nest that locking the
852 * other way, holding cgroup_lock() for much of the code.
853 *
854 * So in order to avoid an ABBA deadlock, the cpuset code handling
855 * these user changes delegates the actual sched domain rebuilding
856 * to a separate workqueue thread, which ends up processing the
857 * above do_rebuild_sched_domains() function.
858 */
859static void async_rebuild_sched_domains(void)
860{
Miao Xief90d4112009-01-16 10:24:10 +0800861 queue_work(cpuset_wq, &rebuild_sched_domains_work);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700862}
863
864/*
865 * Accomplishes the same scheduler domain rebuild as the above
866 * async_rebuild_sched_domains(), however it directly calls the
867 * rebuild routine synchronously rather than calling it via an
868 * asynchronous work thread.
869 *
870 * This can only be called from code that is not holding
871 * cgroup_mutex (not nested in a cgroup_lock() call.)
872 */
873void rebuild_sched_domains(void)
874{
875 do_rebuild_sched_domains(NULL);
Paul Jackson029190c2007-10-18 23:40:20 -0700876}
877
Cliff Wickman58f47902008-02-07 00:14:44 -0800878/**
879 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
880 * @tsk: task to test
881 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
882 *
Paul Menage2df167a2008-02-07 00:14:45 -0800883 * Call with cgroup_mutex held. May take callback_mutex during call.
Cliff Wickman58f47902008-02-07 00:14:44 -0800884 * Called for each task in a cgroup by cgroup_scan_tasks().
885 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
886 * words, if its mask is not equal to its cpuset's mask).
Paul Jackson053199e2005-10-30 15:02:30 -0800887 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700888static int cpuset_test_cpumask(struct task_struct *tsk,
889 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800890{
Li Zefan300ed6c2009-01-07 18:08:44 -0800891 return !cpumask_equal(&tsk->cpus_allowed,
Cliff Wickman58f47902008-02-07 00:14:44 -0800892 (cgroup_cs(scan->cg))->cpus_allowed);
893}
Paul Jackson053199e2005-10-30 15:02:30 -0800894
Cliff Wickman58f47902008-02-07 00:14:44 -0800895/**
896 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
897 * @tsk: task to test
898 * @scan: struct cgroup_scanner containing the cgroup of the task
899 *
900 * Called by cgroup_scan_tasks() for each task in a cgroup whose
901 * cpus_allowed mask needs to be changed.
902 *
903 * We don't need to re-check for the cgroup/cpuset membership, since we're
904 * holding cgroup_lock() at this point.
905 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700906static void cpuset_change_cpumask(struct task_struct *tsk,
907 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800908{
Li Zefan300ed6c2009-01-07 18:08:44 -0800909 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
Cliff Wickman58f47902008-02-07 00:14:44 -0800910}
911
912/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700913 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
914 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
Li Zefan4e743392008-09-13 02:33:08 -0700915 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -0700916 *
917 * Called with cgroup_mutex held
918 *
919 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
920 * calling callback functions for each.
921 *
Li Zefan4e743392008-09-13 02:33:08 -0700922 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
923 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -0700924 */
Li Zefan4e743392008-09-13 02:33:08 -0700925static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
Miao Xie0b2f6302008-07-25 01:47:21 -0700926{
927 struct cgroup_scanner scan;
Miao Xie0b2f6302008-07-25 01:47:21 -0700928
929 scan.cg = cs->css.cgroup;
930 scan.test_task = cpuset_test_cpumask;
931 scan.process_task = cpuset_change_cpumask;
Li Zefan4e743392008-09-13 02:33:08 -0700932 scan.heap = heap;
933 cgroup_scan_tasks(&scan);
Miao Xie0b2f6302008-07-25 01:47:21 -0700934}
935
936/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800937 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
938 * @cs: the cpuset to consider
939 * @buf: buffer of cpu numbers written to this cpuset
940 */
Li Zefan645fcc92009-01-07 18:08:43 -0800941static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
942 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Li Zefan4e743392008-09-13 02:33:08 -0700944 struct ptr_heap heap;
Cliff Wickman58f47902008-02-07 00:14:44 -0800945 int retval;
946 int is_load_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700948 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
949 if (cs == &top_cpuset)
950 return -EACCES;
951
David Rientjes6f7f02e2007-05-08 00:31:43 -0700952 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800953 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700954 * Since cpulist_parse() fails on an empty mask, we special case
955 * that parsing. The validate_change() call ensures that cpusets
956 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700957 */
Paul Jackson020958b2007-10-18 23:40:21 -0700958 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800959 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700960 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -0800961 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700962 if (retval < 0)
963 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700964
Li Zefan300ed6c2009-01-07 18:08:44 -0800965 if (!cpumask_subset(trialcs->cpus_allowed, cpu_online_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700966 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700967 }
Li Zefan645fcc92009-01-07 18:08:43 -0800968 retval = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700969 if (retval < 0)
970 return retval;
Paul Jackson029190c2007-10-18 23:40:20 -0700971
Paul Menage8707d8b2007-10-18 23:40:22 -0700972 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800973 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700974 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800975
Li Zefan4e743392008-09-13 02:33:08 -0700976 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
977 if (retval)
978 return retval;
979
Li Zefan645fcc92009-01-07 18:08:43 -0800980 is_load_balanced = is_sched_load_balance(trialcs);
Paul Jackson029190c2007-10-18 23:40:20 -0700981
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800982 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -0800983 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800984 mutex_unlock(&callback_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700985
Paul Menage8707d8b2007-10-18 23:40:22 -0700986 /*
987 * Scan tasks in the cpuset, and update the cpumasks of any
Cliff Wickman58f47902008-02-07 00:14:44 -0800988 * that need an update.
Paul Menage8707d8b2007-10-18 23:40:22 -0700989 */
Li Zefan4e743392008-09-13 02:33:08 -0700990 update_tasks_cpumask(cs, &heap);
991
992 heap_free(&heap);
Cliff Wickman58f47902008-02-07 00:14:44 -0800993
Paul Menage8707d8b2007-10-18 23:40:22 -0700994 if (is_load_balanced)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700995 async_rebuild_sched_domains();
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Paul Jackson053199e2005-10-30 15:02:30 -0800999/*
Paul Jacksone4e364e2006-03-31 02:30:52 -08001000 * cpuset_migrate_mm
1001 *
1002 * Migrate memory region from one set of nodes to another.
1003 *
1004 * Temporarilly set tasks mems_allowed to target nodes of migration,
1005 * so that the migration code can allocate pages on these nodes.
1006 *
Paul Menage2df167a2008-02-07 00:14:45 -08001007 * Call holding cgroup_mutex, so current's cpuset won't change
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001008 * during this call, as manage_mutex holds off any cpuset_attach()
Paul Jacksone4e364e2006-03-31 02:30:52 -08001009 * calls. Therefore we don't need to take task_lock around the
1010 * call to guarantee_online_mems(), as we know no one is changing
Paul Menage2df167a2008-02-07 00:14:45 -08001011 * our task's cpuset.
Paul Jacksone4e364e2006-03-31 02:30:52 -08001012 *
1013 * Hold callback_mutex around the two modifications of our tasks
1014 * mems_allowed to synchronize with cpuset_mems_allowed().
1015 *
1016 * While the mm_struct we are migrating is typically from some
1017 * other task, the task_struct mems_allowed that we are hacking
1018 * is for our current task, which must allocate new pages for that
1019 * migrating memory region.
1020 *
1021 * We call cpuset_update_task_memory_state() before hacking
1022 * our tasks mems_allowed, so that we are assured of being in
1023 * sync with our tasks cpuset, and in particular, callbacks to
1024 * cpuset_update_task_memory_state() from nested page allocations
1025 * won't see any mismatch of our cpuset and task mems_generation
1026 * values, so won't overwrite our hacked tasks mems_allowed
1027 * nodemask.
1028 */
1029
1030static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1031 const nodemask_t *to)
1032{
1033 struct task_struct *tsk = current;
1034
1035 cpuset_update_task_memory_state();
1036
1037 mutex_lock(&callback_mutex);
1038 tsk->mems_allowed = *to;
1039 mutex_unlock(&callback_mutex);
1040
1041 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
1042
1043 mutex_lock(&callback_mutex);
Paul Menage8793d852007-10-18 23:39:39 -07001044 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001045 mutex_unlock(&callback_mutex);
1046}
1047
Li Zefan3b6766f2009-04-02 16:57:51 -07001048/*
1049 * Rebind task's vmas to cpuset's new mems_allowed, and migrate pages to new
1050 * nodes if memory_migrate flag is set. Called with cgroup_mutex held.
1051 */
1052static void cpuset_change_nodemask(struct task_struct *p,
1053 struct cgroup_scanner *scan)
1054{
1055 struct mm_struct *mm;
1056 struct cpuset *cs;
1057 int migrate;
1058 const nodemask_t *oldmem = scan->data;
1059
1060 mm = get_task_mm(p);
1061 if (!mm)
1062 return;
1063
1064 cs = cgroup_cs(scan->cg);
1065 migrate = is_memory_migrate(cs);
1066
1067 mpol_rebind_mm(mm, &cs->mems_allowed);
1068 if (migrate)
1069 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
1070 mmput(mm);
1071}
1072
Paul Menage8793d852007-10-18 23:39:39 -07001073static void *cpuset_being_rebound;
1074
Miao Xie0b2f6302008-07-25 01:47:21 -07001075/**
1076 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1077 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1078 * @oldmem: old mems_allowed of cpuset cs
Li Zefan010cfac2009-04-02 16:57:52 -07001079 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -07001080 *
1081 * Called with cgroup_mutex held
Li Zefan010cfac2009-04-02 16:57:52 -07001082 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1083 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -07001084 */
Li Zefan010cfac2009-04-02 16:57:52 -07001085static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1086 struct ptr_heap *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Li Zefan3b6766f2009-04-02 16:57:51 -07001088 struct cgroup_scanner scan;
Paul Jackson59dac162006-01-08 01:01:52 -08001089
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001090 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001091
Li Zefan3b6766f2009-04-02 16:57:51 -07001092 scan.cg = cs->css.cgroup;
1093 scan.test_task = NULL;
1094 scan.process_task = cpuset_change_nodemask;
Li Zefan010cfac2009-04-02 16:57:52 -07001095 scan.heap = heap;
Li Zefan3b6766f2009-04-02 16:57:51 -07001096 scan.data = (nodemask_t *)oldmem;
Paul Jackson42253992006-01-08 01:01:59 -08001097
1098 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001099 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1100 * take while holding tasklist_lock. Forks can happen - the
1101 * mpol_dup() cpuset_being_rebound check will catch such forks,
1102 * and rebind their vma mempolicies too. Because we still hold
1103 * the global cgroup_mutex, we know that no other rebind effort
1104 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001105 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001106 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001107 */
Li Zefan010cfac2009-04-02 16:57:52 -07001108 cgroup_scan_tasks(&scan);
Paul Jackson42253992006-01-08 01:01:59 -08001109
Paul Menage2df167a2008-02-07 00:14:45 -08001110 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001111 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Miao Xie0b2f6302008-07-25 01:47:21 -07001114/*
1115 * Handle user request to change the 'mems' memory placement
1116 * of a cpuset. Needs to validate the request, update the
1117 * cpusets mems_allowed and mems_generation, and for each
1118 * task in the cpuset, rebind any vma mempolicies and if
1119 * the cpuset is marked 'memory_migrate', migrate the tasks
1120 * pages to the new memory.
1121 *
1122 * Call with cgroup_mutex held. May take callback_mutex during call.
1123 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1124 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1125 * their mempolicies to the cpusets new mems_allowed.
1126 */
Li Zefan645fcc92009-01-07 18:08:43 -08001127static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1128 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001129{
Miao Xie0b2f6302008-07-25 01:47:21 -07001130 nodemask_t oldmem;
1131 int retval;
Li Zefan010cfac2009-04-02 16:57:52 -07001132 struct ptr_heap heap;
Miao Xie0b2f6302008-07-25 01:47:21 -07001133
1134 /*
1135 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
1136 * it's read-only
1137 */
1138 if (cs == &top_cpuset)
1139 return -EACCES;
1140
Miao Xie0b2f6302008-07-25 01:47:21 -07001141 /*
1142 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1143 * Since nodelist_parse() fails on an empty mask, we special case
1144 * that parsing. The validate_change() call ensures that cpusets
1145 * with tasks have memory.
1146 */
1147 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001148 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001149 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001150 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001151 if (retval < 0)
1152 goto done;
1153
Li Zefan645fcc92009-01-07 18:08:43 -08001154 if (!nodes_subset(trialcs->mems_allowed,
Miao Xie0b2f6302008-07-25 01:47:21 -07001155 node_states[N_HIGH_MEMORY]))
1156 return -EINVAL;
1157 }
1158 oldmem = cs->mems_allowed;
Li Zefan645fcc92009-01-07 18:08:43 -08001159 if (nodes_equal(oldmem, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001160 retval = 0; /* Too easy - nothing to do */
1161 goto done;
1162 }
Li Zefan645fcc92009-01-07 18:08:43 -08001163 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001164 if (retval < 0)
1165 goto done;
1166
Li Zefan010cfac2009-04-02 16:57:52 -07001167 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1168 if (retval < 0)
1169 goto done;
1170
Miao Xie0b2f6302008-07-25 01:47:21 -07001171 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001172 cs->mems_allowed = trialcs->mems_allowed;
Miao Xie0b2f6302008-07-25 01:47:21 -07001173 cs->mems_generation = cpuset_mems_generation++;
1174 mutex_unlock(&callback_mutex);
1175
Li Zefan010cfac2009-04-02 16:57:52 -07001176 update_tasks_nodemask(cs, &oldmem, &heap);
1177
1178 heap_free(&heap);
Miao Xie0b2f6302008-07-25 01:47:21 -07001179done:
1180 return retval;
1181}
1182
Paul Menage8793d852007-10-18 23:39:39 -07001183int current_cpuset_is_being_rebound(void)
1184{
1185 return task_cs(current) == cpuset_being_rebound;
1186}
1187
Paul Menage5be7a472008-05-06 20:42:41 -07001188static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001189{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001190#ifdef CONFIG_SMP
Li Zefan30e0e172008-05-13 10:27:17 +08001191 if (val < -1 || val >= SD_LV_MAX)
1192 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001193#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001194
1195 if (val != cs->relax_domain_level) {
1196 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001197 if (!cpumask_empty(cs->cpus_allowed) &&
1198 is_sched_load_balance(cs))
Max Krasnyanskycf417142008-08-11 14:33:53 -07001199 async_rebuild_sched_domains();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001200 }
1201
1202 return 0;
1203}
1204
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001205/*
Miao Xie950592f2009-06-16 15:31:47 -07001206 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1207 * @tsk: task to be updated
1208 * @scan: struct cgroup_scanner containing the cgroup of the task
1209 *
1210 * Called by cgroup_scan_tasks() for each task in a cgroup.
1211 *
1212 * We don't need to re-check for the cgroup/cpuset membership, since we're
1213 * holding cgroup_lock() at this point.
1214 */
1215static void cpuset_change_flag(struct task_struct *tsk,
1216 struct cgroup_scanner *scan)
1217{
1218 cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk);
1219}
1220
1221/*
1222 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1223 * @cs: the cpuset in which each task's spread flags needs to be changed
1224 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
1225 *
1226 * Called with cgroup_mutex held
1227 *
1228 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1229 * calling callback functions for each.
1230 *
1231 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1232 * if @heap != NULL.
1233 */
1234static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap)
1235{
1236 struct cgroup_scanner scan;
1237
1238 scan.cg = cs->css.cgroup;
1239 scan.test_task = NULL;
1240 scan.process_task = cpuset_change_flag;
1241 scan.heap = heap;
1242 cgroup_scan_tasks(&scan);
1243}
1244
1245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001247 * bit: the bit to update (see cpuset_flagbits_t)
1248 * cs: the cpuset to update
1249 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001250 *
Paul Menage2df167a2008-02-07 00:14:45 -08001251 * Call with cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
1253
Paul Menage700fe1a2008-04-29 01:00:00 -07001254static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1255 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Li Zefan645fcc92009-01-07 18:08:43 -08001257 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001258 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001259 int spread_flag_changed;
1260 struct ptr_heap heap;
1261 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Li Zefan645fcc92009-01-07 18:08:43 -08001263 trialcs = alloc_trial_cpuset(cs);
1264 if (!trialcs)
1265 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Li Zefan645fcc92009-01-07 18:08:43 -08001267 if (turning_on)
1268 set_bit(bit, &trialcs->flags);
1269 else
1270 clear_bit(bit, &trialcs->flags);
1271
1272 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001273 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001274 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001275
Miao Xie950592f2009-06-16 15:31:47 -07001276 err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1277 if (err < 0)
1278 goto out;
1279
Paul Jackson029190c2007-10-18 23:40:20 -07001280 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001281 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001282
Miao Xie950592f2009-06-16 15:31:47 -07001283 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1284 || (is_spread_page(cs) != is_spread_page(trialcs)));
1285
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001286 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001287 cs->flags = trialcs->flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001288 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001289
Li Zefan300ed6c2009-01-07 18:08:44 -08001290 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Max Krasnyanskycf417142008-08-11 14:33:53 -07001291 async_rebuild_sched_domains();
Paul Jackson029190c2007-10-18 23:40:20 -07001292
Miao Xie950592f2009-06-16 15:31:47 -07001293 if (spread_flag_changed)
1294 update_tasks_flags(cs, &heap);
1295 heap_free(&heap);
Li Zefan645fcc92009-01-07 18:08:43 -08001296out:
1297 free_trial_cpuset(trialcs);
1298 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Paul Jackson053199e2005-10-30 15:02:30 -08001301/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001302 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001303 *
1304 * These routines manage a digitally filtered, constant time based,
1305 * event frequency meter. There are four routines:
1306 * fmeter_init() - initialize a frequency meter.
1307 * fmeter_markevent() - called each time the event happens.
1308 * fmeter_getrate() - returns the recent rate of such events.
1309 * fmeter_update() - internal routine used to update fmeter.
1310 *
1311 * A common data structure is passed to each of these routines,
1312 * which is used to keep track of the state required to manage the
1313 * frequency meter and its digital filter.
1314 *
1315 * The filter works on the number of events marked per unit time.
1316 * The filter is single-pole low-pass recursive (IIR). The time unit
1317 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1318 * simulate 3 decimal digits of precision (multiplied by 1000).
1319 *
1320 * With an FM_COEF of 933, and a time base of 1 second, the filter
1321 * has a half-life of 10 seconds, meaning that if the events quit
1322 * happening, then the rate returned from the fmeter_getrate()
1323 * will be cut in half each 10 seconds, until it converges to zero.
1324 *
1325 * It is not worth doing a real infinitely recursive filter. If more
1326 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1327 * just compute FM_MAXTICKS ticks worth, by which point the level
1328 * will be stable.
1329 *
1330 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1331 * arithmetic overflow in the fmeter_update() routine.
1332 *
1333 * Given the simple 32 bit integer arithmetic used, this meter works
1334 * best for reporting rates between one per millisecond (msec) and
1335 * one per 32 (approx) seconds. At constant rates faster than one
1336 * per msec it maxes out at values just under 1,000,000. At constant
1337 * rates between one per msec, and one per second it will stabilize
1338 * to a value N*1000, where N is the rate of events per second.
1339 * At constant rates between one per second and one per 32 seconds,
1340 * it will be choppy, moving up on the seconds that have an event,
1341 * and then decaying until the next event. At rates slower than
1342 * about one in 32 seconds, it decays all the way back to zero between
1343 * each event.
1344 */
1345
1346#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1347#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1348#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1349#define FM_SCALE 1000 /* faux fixed point scale */
1350
1351/* Initialize a frequency meter */
1352static void fmeter_init(struct fmeter *fmp)
1353{
1354 fmp->cnt = 0;
1355 fmp->val = 0;
1356 fmp->time = 0;
1357 spin_lock_init(&fmp->lock);
1358}
1359
1360/* Internal meter update - process cnt events and update value */
1361static void fmeter_update(struct fmeter *fmp)
1362{
1363 time_t now = get_seconds();
1364 time_t ticks = now - fmp->time;
1365
1366 if (ticks == 0)
1367 return;
1368
1369 ticks = min(FM_MAXTICKS, ticks);
1370 while (ticks-- > 0)
1371 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1372 fmp->time = now;
1373
1374 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1375 fmp->cnt = 0;
1376}
1377
1378/* Process any previous ticks, then bump cnt by one (times scale). */
1379static void fmeter_markevent(struct fmeter *fmp)
1380{
1381 spin_lock(&fmp->lock);
1382 fmeter_update(fmp);
1383 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1384 spin_unlock(&fmp->lock);
1385}
1386
1387/* Process any previous ticks, then return current value. */
1388static int fmeter_getrate(struct fmeter *fmp)
1389{
1390 int val;
1391
1392 spin_lock(&fmp->lock);
1393 fmeter_update(fmp);
1394 val = fmp->val;
1395 spin_unlock(&fmp->lock);
1396 return val;
1397}
1398
Li Zefan2341d1b2009-01-07 18:08:42 -08001399/* Protected by cgroup_lock */
1400static cpumask_var_t cpus_attach;
1401
Paul Menage2df167a2008-02-07 00:14:45 -08001402/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
Paul Menage8793d852007-10-18 23:39:39 -07001403static int cpuset_can_attach(struct cgroup_subsys *ss,
1404 struct cgroup *cont, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Paul Menage8793d852007-10-18 23:39:39 -07001406 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Li Zefan300ed6c2009-01-07 18:08:44 -08001408 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -ENOSPC;
David Rientjes9985b0b2008-06-05 12:57:11 -07001410
David Rientjes6d7b2f52009-04-02 16:57:57 -07001411 /*
1412 * Kthreads bound to specific cpus cannot be moved to a new cpuset; we
1413 * cannot change their cpu affinity and isolating such threads by their
1414 * set of allowed nodes is unnecessary. Thus, cpusets are not
1415 * applicable for such threads. This prevents checking for success of
1416 * set_cpus_allowed_ptr() on all attached tasks before cpus_allowed may
1417 * be changed.
1418 */
1419 if (tsk->flags & PF_THREAD_BOUND)
1420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
David Rientjes6d7b2f52009-04-02 16:57:57 -07001422 return security_task_setscheduler(tsk, 0, NULL);
Paul Menage8793d852007-10-18 23:39:39 -07001423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Paul Menage8793d852007-10-18 23:39:39 -07001425static void cpuset_attach(struct cgroup_subsys *ss,
1426 struct cgroup *cont, struct cgroup *oldcont,
1427 struct task_struct *tsk)
1428{
Paul Menage8793d852007-10-18 23:39:39 -07001429 nodemask_t from, to;
1430 struct mm_struct *mm;
1431 struct cpuset *cs = cgroup_cs(cont);
1432 struct cpuset *oldcs = cgroup_cs(oldcont);
David Rientjes9985b0b2008-06-05 12:57:11 -07001433 int err;
David Quigley22fb52d2006-06-23 02:04:00 -07001434
Miao Xief5813d92009-01-07 18:08:40 -08001435 if (cs == &top_cpuset) {
Li Zefan2341d1b2009-01-07 18:08:42 -08001436 cpumask_copy(cpus_attach, cpu_possible_mask);
Miao Xief5813d92009-01-07 18:08:40 -08001437 } else {
1438 mutex_lock(&callback_mutex);
Li Zefan2341d1b2009-01-07 18:08:42 -08001439 guarantee_online_cpus(cs, cpus_attach);
Miao Xief5813d92009-01-07 18:08:40 -08001440 mutex_unlock(&callback_mutex);
1441 }
Li Zefan2341d1b2009-01-07 18:08:42 -08001442 err = set_cpus_allowed_ptr(tsk, cpus_attach);
David Rientjes9985b0b2008-06-05 12:57:11 -07001443 if (err)
1444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Miao Xie950592f2009-06-16 15:31:47 -07001446 cpuset_update_task_spread_flag(cs, tsk);
1447
Paul Jackson45b07ef2006-01-08 01:00:56 -08001448 from = oldcs->mems_allowed;
1449 to = cs->mems_allowed;
Paul Jackson42253992006-01-08 01:01:59 -08001450 mm = get_task_mm(tsk);
1451 if (mm) {
1452 mpol_rebind_mm(mm, &to);
Paul Jackson2741a552006-03-31 02:30:51 -08001453 if (is_memory_migrate(cs))
Paul Jacksone4e364e2006-03-31 02:30:52 -08001454 cpuset_migrate_mm(mm, &from, &to);
Paul Jackson42253992006-01-08 01:01:59 -08001455 mmput(mm);
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459/* The various types of files and directories in a cpuset file system */
1460
1461typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001462 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 FILE_CPULIST,
1464 FILE_MEMLIST,
1465 FILE_CPU_EXCLUSIVE,
1466 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001467 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001468 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001469 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001470 FILE_MEMORY_PRESSURE_ENABLED,
1471 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001472 FILE_SPREAD_PAGE,
1473 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474} cpuset_filetype_t;
1475
Paul Menage700fe1a2008-04-29 01:00:00 -07001476static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1477{
1478 int retval = 0;
1479 struct cpuset *cs = cgroup_cs(cgrp);
1480 cpuset_filetype_t type = cft->private;
1481
Paul Menagee3712392008-07-25 01:47:02 -07001482 if (!cgroup_lock_live_group(cgrp))
Paul Menage700fe1a2008-04-29 01:00:00 -07001483 return -ENODEV;
Paul Menage700fe1a2008-04-29 01:00:00 -07001484
1485 switch (type) {
1486 case FILE_CPU_EXCLUSIVE:
1487 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1488 break;
1489 case FILE_MEM_EXCLUSIVE:
1490 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1491 break;
Paul Menage78608362008-04-29 01:00:26 -07001492 case FILE_MEM_HARDWALL:
1493 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1494 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001495 case FILE_SCHED_LOAD_BALANCE:
1496 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1497 break;
1498 case FILE_MEMORY_MIGRATE:
1499 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1500 break;
1501 case FILE_MEMORY_PRESSURE_ENABLED:
1502 cpuset_memory_pressure_enabled = !!val;
1503 break;
1504 case FILE_MEMORY_PRESSURE:
1505 retval = -EACCES;
1506 break;
1507 case FILE_SPREAD_PAGE:
1508 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001509 break;
1510 case FILE_SPREAD_SLAB:
1511 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001512 break;
1513 default:
1514 retval = -EINVAL;
1515 break;
1516 }
1517 cgroup_unlock();
1518 return retval;
1519}
1520
Paul Menage5be7a472008-05-06 20:42:41 -07001521static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1522{
1523 int retval = 0;
1524 struct cpuset *cs = cgroup_cs(cgrp);
1525 cpuset_filetype_t type = cft->private;
1526
Paul Menagee3712392008-07-25 01:47:02 -07001527 if (!cgroup_lock_live_group(cgrp))
Paul Menage5be7a472008-05-06 20:42:41 -07001528 return -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001529
Paul Menage5be7a472008-05-06 20:42:41 -07001530 switch (type) {
1531 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1532 retval = update_relax_domain_level(cs, val);
1533 break;
1534 default:
1535 retval = -EINVAL;
1536 break;
1537 }
1538 cgroup_unlock();
1539 return retval;
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/*
Paul Menagee3712392008-07-25 01:47:02 -07001543 * Common handling for a write to a "cpus" or "mems" file.
1544 */
1545static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1546 const char *buf)
1547{
1548 int retval = 0;
Li Zefan645fcc92009-01-07 18:08:43 -08001549 struct cpuset *cs = cgroup_cs(cgrp);
1550 struct cpuset *trialcs;
Paul Menagee3712392008-07-25 01:47:02 -07001551
1552 if (!cgroup_lock_live_group(cgrp))
1553 return -ENODEV;
1554
Li Zefan645fcc92009-01-07 18:08:43 -08001555 trialcs = alloc_trial_cpuset(cs);
1556 if (!trialcs)
1557 return -ENOMEM;
1558
Paul Menagee3712392008-07-25 01:47:02 -07001559 switch (cft->private) {
1560 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001561 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001562 break;
1563 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001564 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001565 break;
1566 default:
1567 retval = -EINVAL;
1568 break;
1569 }
Li Zefan645fcc92009-01-07 18:08:43 -08001570
1571 free_trial_cpuset(trialcs);
Paul Menagee3712392008-07-25 01:47:02 -07001572 cgroup_unlock();
1573 return retval;
1574}
1575
1576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * These ascii lists should be read in a single call, by using a user
1578 * buffer large enough to hold the entire map. If read in smaller
1579 * chunks, there is no guarantee of atomicity. Since the display format
1580 * used, list of ranges of sequential numbers, is variable length,
1581 * and since these maps can change value dynamically, one could read
1582 * gibberish by doing partial reads while a list was changing.
1583 * A single large read to a buffer that crosses a page boundary is
1584 * ok, because the result being copied to user land is not recomputed
1585 * across a page fault.
1586 */
1587
1588static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1589{
Li Zefan5a7625d2009-01-07 18:08:41 -08001590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001592 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08001593 ret = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001594 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Li Zefan5a7625d2009-01-07 18:08:41 -08001596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
1599static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1600{
1601 nodemask_t mask;
1602
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001603 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 mask = cs->mems_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001605 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1608}
1609
Paul Menage8793d852007-10-18 23:39:39 -07001610static ssize_t cpuset_common_file_read(struct cgroup *cont,
1611 struct cftype *cft,
1612 struct file *file,
1613 char __user *buf,
1614 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Paul Menage8793d852007-10-18 23:39:39 -07001616 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 cpuset_filetype_t type = cft->private;
1618 char *page;
1619 ssize_t retval = 0;
1620 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Mel Gormane12ba742007-10-16 01:25:52 -07001622 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return -ENOMEM;
1624
1625 s = page;
1626
1627 switch (type) {
1628 case FILE_CPULIST:
1629 s += cpuset_sprintf_cpulist(s, cs);
1630 break;
1631 case FILE_MEMLIST:
1632 s += cpuset_sprintf_memlist(s, cs);
1633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 default:
1635 retval = -EINVAL;
1636 goto out;
1637 }
1638 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Al Viroeacaa1f2005-09-30 03:26:43 +01001640 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641out:
1642 free_page((unsigned long)page);
1643 return retval;
1644}
1645
Paul Menage700fe1a2008-04-29 01:00:00 -07001646static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1647{
1648 struct cpuset *cs = cgroup_cs(cont);
1649 cpuset_filetype_t type = cft->private;
1650 switch (type) {
1651 case FILE_CPU_EXCLUSIVE:
1652 return is_cpu_exclusive(cs);
1653 case FILE_MEM_EXCLUSIVE:
1654 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001655 case FILE_MEM_HARDWALL:
1656 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001657 case FILE_SCHED_LOAD_BALANCE:
1658 return is_sched_load_balance(cs);
1659 case FILE_MEMORY_MIGRATE:
1660 return is_memory_migrate(cs);
1661 case FILE_MEMORY_PRESSURE_ENABLED:
1662 return cpuset_memory_pressure_enabled;
1663 case FILE_MEMORY_PRESSURE:
1664 return fmeter_getrate(&cs->fmeter);
1665 case FILE_SPREAD_PAGE:
1666 return is_spread_page(cs);
1667 case FILE_SPREAD_SLAB:
1668 return is_spread_slab(cs);
1669 default:
1670 BUG();
1671 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001672
1673 /* Unreachable but makes gcc happy */
1674 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Paul Menage5be7a472008-05-06 20:42:41 -07001677static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1678{
1679 struct cpuset *cs = cgroup_cs(cont);
1680 cpuset_filetype_t type = cft->private;
1681 switch (type) {
1682 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1683 return cs->relax_domain_level;
1684 default:
1685 BUG();
1686 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001687
1688 /* Unrechable but makes gcc happy */
1689 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001690}
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693/*
1694 * for the common functions, 'private' gives the type of file
1695 */
1696
Paul Menageaddf2c72008-04-29 01:00:26 -07001697static struct cftype files[] = {
1698 {
1699 .name = "cpus",
1700 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001701 .write_string = cpuset_write_resmask,
1702 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001703 .private = FILE_CPULIST,
1704 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Paul Menageaddf2c72008-04-29 01:00:26 -07001706 {
1707 .name = "mems",
1708 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001709 .write_string = cpuset_write_resmask,
1710 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001711 .private = FILE_MEMLIST,
1712 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Paul Menageaddf2c72008-04-29 01:00:26 -07001714 {
1715 .name = "cpu_exclusive",
1716 .read_u64 = cpuset_read_u64,
1717 .write_u64 = cpuset_write_u64,
1718 .private = FILE_CPU_EXCLUSIVE,
1719 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Paul Menageaddf2c72008-04-29 01:00:26 -07001721 {
1722 .name = "mem_exclusive",
1723 .read_u64 = cpuset_read_u64,
1724 .write_u64 = cpuset_write_u64,
1725 .private = FILE_MEM_EXCLUSIVE,
1726 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Paul Menageaddf2c72008-04-29 01:00:26 -07001728 {
Paul Menage78608362008-04-29 01:00:26 -07001729 .name = "mem_hardwall",
1730 .read_u64 = cpuset_read_u64,
1731 .write_u64 = cpuset_write_u64,
1732 .private = FILE_MEM_HARDWALL,
1733 },
1734
1735 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001736 .name = "sched_load_balance",
1737 .read_u64 = cpuset_read_u64,
1738 .write_u64 = cpuset_write_u64,
1739 .private = FILE_SCHED_LOAD_BALANCE,
1740 },
Paul Jackson029190c2007-10-18 23:40:20 -07001741
Paul Menageaddf2c72008-04-29 01:00:26 -07001742 {
1743 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001744 .read_s64 = cpuset_read_s64,
1745 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001746 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1747 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001748
Paul Menageaddf2c72008-04-29 01:00:26 -07001749 {
1750 .name = "memory_migrate",
1751 .read_u64 = cpuset_read_u64,
1752 .write_u64 = cpuset_write_u64,
1753 .private = FILE_MEMORY_MIGRATE,
1754 },
1755
1756 {
1757 .name = "memory_pressure",
1758 .read_u64 = cpuset_read_u64,
1759 .write_u64 = cpuset_write_u64,
1760 .private = FILE_MEMORY_PRESSURE,
Li Zefan099fca32009-04-02 16:57:29 -07001761 .mode = S_IRUGO,
Paul Menageaddf2c72008-04-29 01:00:26 -07001762 },
1763
1764 {
1765 .name = "memory_spread_page",
1766 .read_u64 = cpuset_read_u64,
1767 .write_u64 = cpuset_write_u64,
1768 .private = FILE_SPREAD_PAGE,
1769 },
1770
1771 {
1772 .name = "memory_spread_slab",
1773 .read_u64 = cpuset_read_u64,
1774 .write_u64 = cpuset_write_u64,
1775 .private = FILE_SPREAD_SLAB,
1776 },
Paul Jackson45b07ef2006-01-08 01:00:56 -08001777};
1778
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001779static struct cftype cft_memory_pressure_enabled = {
1780 .name = "memory_pressure_enabled",
Paul Menage700fe1a2008-04-29 01:00:00 -07001781 .read_u64 = cpuset_read_u64,
1782 .write_u64 = cpuset_write_u64,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001783 .private = FILE_MEMORY_PRESSURE_ENABLED,
1784};
1785
Paul Menage8793d852007-10-18 23:39:39 -07001786static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 int err;
1789
Paul Menageaddf2c72008-04-29 01:00:26 -07001790 err = cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
1791 if (err)
Paul Jackson825a46a2006-03-24 03:16:03 -08001792 return err;
Paul Menage8793d852007-10-18 23:39:39 -07001793 /* memory_pressure_enabled is in root cpuset only */
Paul Menageaddf2c72008-04-29 01:00:26 -07001794 if (!cont->parent)
Paul Menage8793d852007-10-18 23:39:39 -07001795 err = cgroup_add_file(cont, ss,
Paul Menageaddf2c72008-04-29 01:00:26 -07001796 &cft_memory_pressure_enabled);
1797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
1800/*
Paul Menage8793d852007-10-18 23:39:39 -07001801 * post_clone() is called at the end of cgroup_clone().
1802 * 'cgroup' was just created automatically as a result of
1803 * a cgroup_clone(), and the current task is about to
1804 * be moved into 'cgroup'.
1805 *
1806 * Currently we refuse to set up the cgroup - thereby
1807 * refusing the task to be entered, and as a result refusing
1808 * the sys_unshare() or clone() which initiated it - if any
1809 * sibling cpusets have exclusive cpus or mem.
1810 *
1811 * If this becomes a problem for some users who wish to
1812 * allow that scenario, then cpuset_post_clone() could be
1813 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
Paul Menage2df167a2008-02-07 00:14:45 -08001814 * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
1815 * held.
Paul Menage8793d852007-10-18 23:39:39 -07001816 */
1817static void cpuset_post_clone(struct cgroup_subsys *ss,
1818 struct cgroup *cgroup)
1819{
1820 struct cgroup *parent, *child;
1821 struct cpuset *cs, *parent_cs;
1822
1823 parent = cgroup->parent;
1824 list_for_each_entry(child, &parent->children, sibling) {
1825 cs = cgroup_cs(child);
1826 if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
1827 return;
1828 }
1829 cs = cgroup_cs(cgroup);
1830 parent_cs = cgroup_cs(parent);
1831
1832 cs->mems_allowed = parent_cs->mems_allowed;
Li Zefan300ed6c2009-01-07 18:08:44 -08001833 cpumask_copy(cs->cpus_allowed, parent_cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001834 return;
1835}
1836
1837/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * cpuset_create - create a cpuset
Paul Menage2df167a2008-02-07 00:14:45 -08001839 * ss: cpuset cgroup subsystem
1840 * cont: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
1842
Paul Menage8793d852007-10-18 23:39:39 -07001843static struct cgroup_subsys_state *cpuset_create(
1844 struct cgroup_subsys *ss,
1845 struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct cpuset *cs;
Paul Menage8793d852007-10-18 23:39:39 -07001848 struct cpuset *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Paul Menage8793d852007-10-18 23:39:39 -07001850 if (!cont->parent) {
1851 /* This is early initialization for the top cgroup */
1852 top_cpuset.mems_generation = cpuset_mems_generation++;
1853 return &top_cpuset.css;
1854 }
1855 parent = cgroup_cs(cont->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1857 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001858 return ERR_PTR(-ENOMEM);
Li Zefan300ed6c2009-01-07 18:08:44 -08001859 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1860 kfree(cs);
1861 return ERR_PTR(-ENOMEM);
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Paul Jacksoncf2a4732006-01-08 01:01:54 -08001864 cpuset_update_task_memory_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 cs->flags = 0;
Paul Jackson825a46a2006-03-24 03:16:03 -08001866 if (is_spread_page(parent))
1867 set_bit(CS_SPREAD_PAGE, &cs->flags);
1868 if (is_spread_slab(parent))
1869 set_bit(CS_SPREAD_SLAB, &cs->flags);
Paul Jackson029190c2007-10-18 23:40:20 -07001870 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001871 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001872 nodes_clear(cs->mems_allowed);
Paul Jackson151a4422006-03-24 03:16:11 -08001873 cs->mems_generation = cpuset_mems_generation++;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001874 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001875 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 cs->parent = parent;
Paul Jackson202f72d2006-01-08 01:01:57 -08001878 number_of_cpusets++;
Paul Menage8793d852007-10-18 23:39:39 -07001879 return &cs->css ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Paul Jackson029190c2007-10-18 23:40:20 -07001882/*
Paul Jackson029190c2007-10-18 23:40:20 -07001883 * If the cpuset being removed has its flag 'sched_load_balance'
1884 * enabled, then simulate turning sched_load_balance off, which
Max Krasnyanskycf417142008-08-11 14:33:53 -07001885 * will call async_rebuild_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -07001886 */
1887
Paul Menage8793d852007-10-18 23:39:39 -07001888static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Paul Menage8793d852007-10-18 23:39:39 -07001890 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Paul Jacksoncf2a4732006-01-08 01:01:54 -08001892 cpuset_update_task_memory_state();
Paul Jackson029190c2007-10-18 23:40:20 -07001893
1894 if (is_sched_load_balance(cs))
Paul Menage700fe1a2008-04-29 01:00:00 -07001895 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
Paul Jackson029190c2007-10-18 23:40:20 -07001896
Paul Jackson202f72d2006-01-08 01:01:57 -08001897 number_of_cpusets--;
Li Zefan300ed6c2009-01-07 18:08:44 -08001898 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001899 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Paul Menage8793d852007-10-18 23:39:39 -07001902struct cgroup_subsys cpuset_subsys = {
1903 .name = "cpuset",
1904 .create = cpuset_create,
Max Krasnyanskycf417142008-08-11 14:33:53 -07001905 .destroy = cpuset_destroy,
Paul Menage8793d852007-10-18 23:39:39 -07001906 .can_attach = cpuset_can_attach,
1907 .attach = cpuset_attach,
1908 .populate = cpuset_populate,
1909 .post_clone = cpuset_post_clone,
1910 .subsys_id = cpuset_subsys_id,
1911 .early_init = 1,
1912};
1913
Paul Jacksonc417f022006-01-08 01:02:01 -08001914/*
1915 * cpuset_init_early - just enough so that the calls to
1916 * cpuset_update_task_memory_state() in early init code
1917 * are harmless.
1918 */
1919
1920int __init cpuset_init_early(void)
1921{
Yinghai Lu38c7fed2009-05-25 15:10:58 +03001922 alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_NOWAIT);
Li Zefan300ed6c2009-01-07 18:08:44 -08001923
Paul Menage8793d852007-10-18 23:39:39 -07001924 top_cpuset.mems_generation = cpuset_mems_generation++;
Paul Jacksonc417f022006-01-08 01:02:01 -08001925 return 0;
1926}
1927
Paul Menage8793d852007-10-18 23:39:39 -07001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929/**
1930 * cpuset_init - initialize cpusets at system boot
1931 *
1932 * Description: Initialize top_cpuset and the cpuset internal file system,
1933 **/
1934
1935int __init cpuset_init(void)
1936{
Paul Menage8793d852007-10-18 23:39:39 -07001937 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Li Zefan300ed6c2009-01-07 18:08:44 -08001939 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001940 nodes_setall(top_cpuset.mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001942 fmeter_init(&top_cpuset.fmeter);
Paul Jackson151a4422006-03-24 03:16:11 -08001943 top_cpuset.mems_generation = cpuset_mems_generation++;
Paul Jackson029190c2007-10-18 23:40:20 -07001944 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001945 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 err = register_filesystem(&cpuset_fs_type);
1948 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07001949 return err;
1950
Li Zefan2341d1b2009-01-07 18:08:42 -08001951 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1952 BUG();
1953
Paul Jackson202f72d2006-01-08 01:01:57 -08001954 number_of_cpusets = 1;
Paul Menage8793d852007-10-18 23:39:39 -07001955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Cliff Wickman956db3c2008-02-07 00:14:43 -08001958/**
1959 * cpuset_do_move_task - move a given task to another cpuset
1960 * @tsk: pointer to task_struct the task to move
1961 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1962 *
1963 * Called by cgroup_scan_tasks() for each task in a cgroup.
1964 * Return nonzero to stop the walk through the tasks.
1965 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -07001966static void cpuset_do_move_task(struct task_struct *tsk,
1967 struct cgroup_scanner *scan)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001968{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001969 struct cgroup *new_cgroup = scan->data;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001970
Li Zefan7f81b1a2009-04-02 16:57:53 -07001971 cgroup_attach_task(new_cgroup, tsk);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001972}
1973
1974/**
1975 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
1976 * @from: cpuset in which the tasks currently reside
1977 * @to: cpuset to which the tasks will be moved
1978 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001979 * Called with cgroup_mutex held
1980 * callback_mutex must not be held, as cpuset_attach() will take it.
Cliff Wickman956db3c2008-02-07 00:14:43 -08001981 *
1982 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1983 * calling callback functions for each.
1984 */
1985static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
1986{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001987 struct cgroup_scanner scan;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001988
Li Zefan7f81b1a2009-04-02 16:57:53 -07001989 scan.cg = from->css.cgroup;
1990 scan.test_task = NULL; /* select all tasks in cgroup */
1991 scan.process_task = cpuset_do_move_task;
1992 scan.heap = NULL;
1993 scan.data = to->css.cgroup;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001994
Li Zefan7f81b1a2009-04-02 16:57:53 -07001995 if (cgroup_scan_tasks(&scan))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001996 printk(KERN_ERR "move_member_tasks_to_cpuset: "
1997 "cgroup_scan_tasks failed\n");
1998}
1999
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002000/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002001 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002002 * or memory nodes, we need to walk over the cpuset hierarchy,
2003 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002004 * last CPU or node from a cpuset, then move the tasks in the empty
2005 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002006 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002007 * Called with cgroup_mutex held
2008 * callback_mutex must not be held, as cpuset_attach() will take it.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002009 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002010static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002011{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002012 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002013
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002014 /*
2015 * The cgroup's css_sets list is in use if there are tasks
2016 * in the cpuset; the list is empty if there are none;
2017 * the cs->css.refcnt seems always 0.
2018 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002019 if (list_empty(&cs->css.cgroup->css_sets))
2020 return;
2021
2022 /*
2023 * Find its next-highest non-empty parent, (top cpuset
2024 * has online cpus, so can't be empty).
2025 */
2026 parent = cs->parent;
Li Zefan300ed6c2009-01-07 18:08:44 -08002027 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002028 nodes_empty(parent->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002029 parent = parent->parent;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002030
2031 move_member_tasks_to_cpuset(cs, parent);
2032}
2033
2034/*
2035 * Walk the specified cpuset subtree and look for empty cpusets.
2036 * The tasks of such cpuset must be moved to a parent cpuset.
2037 *
Paul Menage2df167a2008-02-07 00:14:45 -08002038 * Called with cgroup_mutex held. We take callback_mutex to modify
Cliff Wickman956db3c2008-02-07 00:14:43 -08002039 * cpus_allowed and mems_allowed.
2040 *
2041 * This walk processes the tree from top to bottom, completing one layer
2042 * before dropping down to the next. It always processes a node before
2043 * any of its children.
2044 *
2045 * For now, since we lack memory hot unplug, we'll never see a cpuset
2046 * that has tasks along with an empty 'mems'. But if we did see such
2047 * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
2048 */
Frederic Weisbeckerd294eb82008-10-03 12:10:10 +02002049static void scan_for_empty_cpusets(struct cpuset *root)
Cliff Wickman956db3c2008-02-07 00:14:43 -08002050{
Li Zefan8d1e6262008-07-29 22:33:21 -07002051 LIST_HEAD(queue);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002052 struct cpuset *cp; /* scans cpusets being updated */
2053 struct cpuset *child; /* scans child cpusets of cp */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002054 struct cgroup *cont;
Miao Xief9b4fb82008-07-25 01:47:22 -07002055 nodemask_t oldmems;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002056
Cliff Wickman956db3c2008-02-07 00:14:43 -08002057 list_add_tail((struct list_head *)&root->stack_list, &queue);
2058
Cliff Wickman956db3c2008-02-07 00:14:43 -08002059 while (!list_empty(&queue)) {
Li Zefan8d1e6262008-07-29 22:33:21 -07002060 cp = list_first_entry(&queue, struct cpuset, stack_list);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002061 list_del(queue.next);
2062 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
2063 child = cgroup_cs(cont);
2064 list_add_tail(&child->stack_list, &queue);
2065 }
Paul Jacksonb4501292008-02-07 00:14:47 -08002066
2067 /* Continue past cpusets with all cpus, mems online */
Li Zefan300ed6c2009-01-07 18:08:44 -08002068 if (cpumask_subset(cp->cpus_allowed, cpu_online_mask) &&
Paul Jacksonb4501292008-02-07 00:14:47 -08002069 nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
2070 continue;
2071
Miao Xief9b4fb82008-07-25 01:47:22 -07002072 oldmems = cp->mems_allowed;
2073
Cliff Wickman956db3c2008-02-07 00:14:43 -08002074 /* Remove offline cpus and mems from this cpuset. */
Paul Jacksonb4501292008-02-07 00:14:47 -08002075 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08002076 cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
2077 cpu_online_mask);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002078 nodes_and(cp->mems_allowed, cp->mems_allowed,
2079 node_states[N_HIGH_MEMORY]);
Paul Jacksonb4501292008-02-07 00:14:47 -08002080 mutex_unlock(&callback_mutex);
2081
2082 /* Move tasks from the empty cpuset to a parent */
Li Zefan300ed6c2009-01-07 18:08:44 -08002083 if (cpumask_empty(cp->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002084 nodes_empty(cp->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002085 remove_tasks_in_empty_cpuset(cp);
Miao Xief9b4fb82008-07-25 01:47:22 -07002086 else {
Li Zefan4e743392008-09-13 02:33:08 -07002087 update_tasks_cpumask(cp, NULL);
Li Zefan010cfac2009-04-02 16:57:52 -07002088 update_tasks_nodemask(cp, &oldmems, NULL);
Miao Xief9b4fb82008-07-25 01:47:22 -07002089 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002090 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002091}
2092
2093/*
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002094 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2095 * period. This is necessary in order to make cpusets transparent
2096 * (of no affect) on systems that are actively using CPU hotplug
2097 * but making no active use of cpusets.
2098 *
Paul Jackson38837fc2006-09-29 02:01:16 -07002099 * This routine ensures that top_cpuset.cpus_allowed tracks
2100 * cpu_online_map on each CPU hotplug (cpuhp) event.
Max Krasnyanskycf417142008-08-11 14:33:53 -07002101 *
2102 * Called within get_online_cpus(). Needs to call cgroup_lock()
2103 * before calling generate_sched_domains().
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002104 */
Max Krasnyanskycf417142008-08-11 14:33:53 -07002105static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
Paul Jackson029190c2007-10-18 23:40:20 -07002106 unsigned long phase, void *unused_cpu)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002107{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002108 struct sched_domain_attr *attr;
Li Zefan6af866a2009-01-07 18:08:45 -08002109 struct cpumask *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002110 int ndoms;
2111
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002112 switch (phase) {
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002113 case CPU_ONLINE:
2114 case CPU_ONLINE_FROZEN:
2115 case CPU_DEAD:
2116 case CPU_DEAD_FROZEN:
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002117 break;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002118
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002119 default:
Avi Kivityac076752007-05-24 12:33:15 +03002120 return NOTIFY_DONE;
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002121 }
Avi Kivityac076752007-05-24 12:33:15 +03002122
Max Krasnyanskycf417142008-08-11 14:33:53 -07002123 cgroup_lock();
Li Zefan0b4217b2009-04-02 16:57:49 -07002124 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08002125 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
Li Zefan0b4217b2009-04-02 16:57:49 -07002126 mutex_unlock(&callback_mutex);
Max Krasnyanskycf417142008-08-11 14:33:53 -07002127 scan_for_empty_cpusets(&top_cpuset);
2128 ndoms = generate_sched_domains(&doms, &attr);
2129 cgroup_unlock();
2130
2131 /* Have scheduler rebuild the domains */
2132 partition_sched_domains(ndoms, doms, attr);
2133
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002134 return NOTIFY_OK;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002135}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002136
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002137#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002138/*
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002139 * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
Max Krasnyanskycf417142008-08-11 14:33:53 -07002140 * Call this routine anytime after node_states[N_HIGH_MEMORY] changes.
2141 * See also the previous routine cpuset_track_online_cpus().
Paul Jackson38837fc2006-09-29 02:01:16 -07002142 */
Miao Xief4818912008-11-19 15:36:30 -08002143static int cpuset_track_online_nodes(struct notifier_block *self,
2144 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002145{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002146 cgroup_lock();
Miao Xief4818912008-11-19 15:36:30 -08002147 switch (action) {
2148 case MEM_ONLINE:
Miao Xief4818912008-11-19 15:36:30 -08002149 case MEM_OFFLINE:
Li Zefan0b4217b2009-04-02 16:57:49 -07002150 mutex_lock(&callback_mutex);
Miao Xief4818912008-11-19 15:36:30 -08002151 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
Li Zefan0b4217b2009-04-02 16:57:49 -07002152 mutex_unlock(&callback_mutex);
2153 if (action == MEM_OFFLINE)
2154 scan_for_empty_cpusets(&top_cpuset);
Miao Xief4818912008-11-19 15:36:30 -08002155 break;
2156 default:
2157 break;
2158 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002159 cgroup_unlock();
Miao Xief4818912008-11-19 15:36:30 -08002160 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002161}
2162#endif
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164/**
2165 * cpuset_init_smp - initialize cpus_allowed
2166 *
2167 * Description: Finish top cpuset after cpu, node maps are initialized
2168 **/
2169
2170void __init cpuset_init_smp(void)
2171{
Li Zefan300ed6c2009-01-07 18:08:44 -08002172 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002173 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002174
Max Krasnyanskycf417142008-08-11 14:33:53 -07002175 hotcpu_notifier(cpuset_track_online_cpus, 0);
Miao Xief4818912008-11-19 15:36:30 -08002176 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
Miao Xief90d4112009-01-16 10:24:10 +08002177
2178 cpuset_wq = create_singlethread_workqueue("cpuset");
2179 BUG_ON(!cpuset_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2184 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002185 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002187 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 * attached to the specified @tsk. Guaranteed to return some non-empty
2189 * subset of cpu_online_map, even if this means going outside the
2190 * tasks cpuset.
2191 **/
2192
Li Zefan6af866a2009-01-07 18:08:45 -08002193void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002195 mutex_lock(&callback_mutex);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002196 cpuset_cpus_allowed_locked(tsk, pmask);
Cliff Wickman470fd642007-10-18 23:40:46 -07002197 mutex_unlock(&callback_mutex);
Cliff Wickman470fd642007-10-18 23:40:46 -07002198}
2199
2200/**
2201 * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
Paul Menage2df167a2008-02-07 00:14:45 -08002202 * Must be called with callback_mutex held.
Cliff Wickman470fd642007-10-18 23:40:46 -07002203 **/
Li Zefan6af866a2009-01-07 18:08:45 -08002204void cpuset_cpus_allowed_locked(struct task_struct *tsk, struct cpumask *pmask)
Cliff Wickman470fd642007-10-18 23:40:46 -07002205{
Paul Jackson909d75a2006-01-08 01:01:55 -08002206 task_lock(tsk);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002207 guarantee_online_cpus(task_cs(tsk), pmask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002208 task_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211void cpuset_init_current_mems_allowed(void)
2212{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002213 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002216/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002217 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2218 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2219 *
2220 * Description: Returns the nodemask_t mems_allowed of the cpuset
2221 * attached to the specified @tsk. Guaranteed to return some non-empty
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002222 * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002223 * tasks cpuset.
2224 **/
2225
2226nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2227{
2228 nodemask_t mask;
2229
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002230 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002231 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -07002232 guarantee_online_mems(task_cs(tsk), &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002233 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002234 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002235
2236 return mask;
2237}
2238
2239/**
Mel Gorman19770b32008-04-28 02:12:18 -07002240 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2241 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002242 *
Mel Gorman19770b32008-04-28 02:12:18 -07002243 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
Mel Gorman19770b32008-04-28 02:12:18 -07002245int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Mel Gorman19770b32008-04-28 02:12:18 -07002247 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Paul Jackson9bf22292005-09-06 15:18:12 -07002250/*
Paul Menage78608362008-04-29 01:00:26 -07002251 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2252 * mem_hardwall ancestor to the specified cpuset. Call holding
2253 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2254 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 */
Paul Menage78608362008-04-29 01:00:26 -07002256static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Paul Menage78608362008-04-29 01:00:26 -07002258 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
Paul Jackson9bf22292005-09-06 15:18:12 -07002259 cs = cs->parent;
2260 return cs;
2261}
2262
2263/**
David Rientjesa1bc5a42009-04-02 16:57:54 -07002264 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2265 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002266 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002267 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002268 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2269 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2270 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2271 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2272 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2273 * flag, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002274 * Otherwise, no.
2275 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002276 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2277 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2278 * might sleep, and might allow a node from an enclosing cpuset.
Paul Jackson02a0e532006-12-13 00:34:25 -08002279 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002280 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2281 * cpusets, and never sleeps.
Paul Jackson02a0e532006-12-13 00:34:25 -08002282 *
2283 * The __GFP_THISNODE placement logic is really handled elsewhere,
2284 * by forcibly using a zonelist starting at a specified node, and by
2285 * (in get_page_from_freelist()) refusing to consider the zones for
2286 * any node on the zonelist except the first. By the time any such
2287 * calls get to this routine, we should just shut up and say 'yes'.
2288 *
Paul Jackson9bf22292005-09-06 15:18:12 -07002289 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002290 * and do not allow allocations outside the current tasks cpuset
2291 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002292 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002293 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002294 *
Paul Jackson02a0e532006-12-13 00:34:25 -08002295 * Scanning up parent cpusets requires callback_mutex. The
2296 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2297 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2298 * current tasks mems_allowed came up empty on the first pass over
2299 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2300 * cpuset are short of memory, might require taking the callback_mutex
2301 * mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002302 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002303 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002304 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2305 * so no allocation on a node outside the cpuset is allowed (unless
2306 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002307 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002308 * The second pass through get_page_from_freelist() doesn't even call
2309 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2310 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2311 * in alloc_flags. That logic and the checks below have the combined
2312 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002313 * in_interrupt - any node ok (current task context irrelevant)
2314 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002315 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002316 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002317 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002318 *
2319 * Rule:
David Rientjesa1bc5a42009-04-02 16:57:54 -07002320 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
Paul Jackson36be57f2006-05-20 15:00:10 -07002321 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2322 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson02a0e532006-12-13 00:34:25 -08002323 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002324int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002325{
Paul Jackson9bf22292005-09-06 15:18:12 -07002326 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002327 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002328
Christoph Lameter9b819d22006-09-25 23:31:40 -07002329 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002330 return 1;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002331 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002332 if (node_isset(node, current->mems_allowed))
2333 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002334 /*
2335 * Allow tasks that have access to memory reserves because they have
2336 * been OOM killed to get memory anywhere.
2337 */
2338 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2339 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002340 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2341 return 0;
2342
Bob Picco5563e772005-11-13 16:06:35 -08002343 if (current->flags & PF_EXITING) /* Let dying task have memory */
2344 return 1;
2345
Paul Jackson9bf22292005-09-06 15:18:12 -07002346 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002347 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002348
Paul Jackson053199e2005-10-30 15:02:30 -08002349 task_lock(current);
Paul Menage78608362008-04-29 01:00:26 -07002350 cs = nearest_hardwall_ancestor(task_cs(current));
Paul Jackson053199e2005-10-30 15:02:30 -08002351 task_unlock(current);
2352
Paul Jackson9bf22292005-09-06 15:18:12 -07002353 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002354 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002355 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Paul Jackson02a0e532006-12-13 00:34:25 -08002358/*
David Rientjesa1bc5a42009-04-02 16:57:54 -07002359 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2360 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002361 * @gfp_mask: memory allocation flags
2362 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002363 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2364 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2365 * yes. If the task has been OOM killed and has access to memory reserves as
2366 * specified by the TIF_MEMDIE flag, yes.
2367 * Otherwise, no.
Paul Jackson02a0e532006-12-13 00:34:25 -08002368 *
2369 * The __GFP_THISNODE placement logic is really handled elsewhere,
2370 * by forcibly using a zonelist starting at a specified node, and by
2371 * (in get_page_from_freelist()) refusing to consider the zones for
2372 * any node on the zonelist except the first. By the time any such
2373 * calls get to this routine, we should just shut up and say 'yes'.
2374 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002375 * Unlike the cpuset_node_allowed_softwall() variant, above,
2376 * this variant requires that the node be in the current task's
Paul Jackson02a0e532006-12-13 00:34:25 -08002377 * mems_allowed or that we're in interrupt. It does not scan up the
2378 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2379 * It never sleeps.
2380 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002381int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -08002382{
Paul Jackson02a0e532006-12-13 00:34:25 -08002383 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2384 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002385 if (node_isset(node, current->mems_allowed))
2386 return 1;
Daniel Walkerdedf8b72007-10-18 03:06:04 -07002387 /*
2388 * Allow tasks that have access to memory reserves because they have
2389 * been OOM killed to get memory anywhere.
2390 */
2391 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2392 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002393 return 0;
2394}
2395
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002396/**
Paul Jackson505970b2006-01-14 13:21:06 -08002397 * cpuset_lock - lock out any changes to cpuset structures
2398 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002399 * The out of memory (oom) code needs to mutex_lock cpusets
Paul Jackson505970b2006-01-14 13:21:06 -08002400 * from being changed while it scans the tasklist looking for a
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002401 * task in an overlapping cpuset. Expose callback_mutex via this
Paul Jackson505970b2006-01-14 13:21:06 -08002402 * cpuset_lock() routine, so the oom code can lock it, before
2403 * locking the task list. The tasklist_lock is a spinlock, so
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002404 * must be taken inside callback_mutex.
Paul Jackson505970b2006-01-14 13:21:06 -08002405 */
2406
2407void cpuset_lock(void)
2408{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002409 mutex_lock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002410}
2411
2412/**
2413 * cpuset_unlock - release lock on cpuset changes
2414 *
2415 * Undo the lock taken in a previous cpuset_lock() call.
2416 */
2417
2418void cpuset_unlock(void)
2419{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002420 mutex_unlock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002421}
2422
2423/**
Paul Jackson825a46a2006-03-24 03:16:03 -08002424 * cpuset_mem_spread_node() - On which node to begin search for a page
2425 *
2426 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2427 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2428 * and if the memory allocation used cpuset_mem_spread_node()
2429 * to determine on which node to start looking, as it will for
2430 * certain page cache or slab cache pages such as used for file
2431 * system buffers and inode caches, then instead of starting on the
2432 * local node to look for a free page, rather spread the starting
2433 * node around the tasks mems_allowed nodes.
2434 *
2435 * We don't have to worry about the returned node being offline
2436 * because "it can't happen", and even if it did, it would be ok.
2437 *
2438 * The routines calling guarantee_online_mems() are careful to
2439 * only set nodes in task->mems_allowed that are online. So it
2440 * should not be possible for the following code to return an
2441 * offline node. But if it did, that would be ok, as this routine
2442 * is not returning the node where the allocation must be, only
2443 * the node where the search should start. The zonelist passed to
2444 * __alloc_pages() will include all nodes. If the slab allocator
2445 * is passed an offline node, it will fall back to the local node.
2446 * See kmem_cache_alloc_node().
2447 */
2448
2449int cpuset_mem_spread_node(void)
2450{
2451 int node;
2452
2453 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2454 if (node == MAX_NUMNODES)
2455 node = first_node(current->mems_allowed);
2456 current->cpuset_mem_spread_rotor = node;
2457 return node;
2458}
2459EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2460
2461/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002462 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2463 * @tsk1: pointer to task_struct of some task.
2464 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002465 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002466 * Description: Return true if @tsk1's mems_allowed intersects the
2467 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2468 * one of the task's memory usage might impact the memory available
2469 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002470 **/
2471
David Rientjesbbe373f2007-10-16 23:25:58 -07002472int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2473 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002474{
David Rientjesbbe373f2007-10-16 23:25:58 -07002475 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002476}
2477
David Rientjes75aa1992009-01-06 14:39:01 -08002478/**
2479 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2480 * @task: pointer to task_struct of some task.
2481 *
2482 * Description: Prints @task's name, cpuset name, and cached copy of its
2483 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2484 * dereferencing task_cs(task).
2485 */
2486void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2487{
2488 struct dentry *dentry;
2489
2490 dentry = task_cs(tsk)->css.cgroup->dentry;
2491 spin_lock(&cpuset_buffer_lock);
2492 snprintf(cpuset_name, CPUSET_NAME_LEN,
2493 dentry ? (const char *)dentry->d_name.name : "/");
2494 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2495 tsk->mems_allowed);
2496 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2497 tsk->comm, cpuset_name, cpuset_nodelist);
2498 spin_unlock(&cpuset_buffer_lock);
2499}
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002502 * Collection of memory_pressure is suppressed unless
2503 * this flag is enabled by writing "1" to the special
2504 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2505 */
2506
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002507int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002508
2509/**
2510 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2511 *
2512 * Keep a running average of the rate of synchronous (direct)
2513 * page reclaim efforts initiated by tasks in each cpuset.
2514 *
2515 * This represents the rate at which some task in the cpuset
2516 * ran low on memory on all nodes it was allowed to use, and
2517 * had to enter the kernels page reclaim code in an effort to
2518 * create more free memory by tossing clean pages or swapping
2519 * or writing dirty pages.
2520 *
2521 * Display to user space in the per-cpuset read-only file
2522 * "memory_pressure". Value displayed is an integer
2523 * representing the recent rate of entry into the synchronous
2524 * (direct) page reclaim by any task attached to the cpuset.
2525 **/
2526
2527void __cpuset_memory_pressure_bump(void)
2528{
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002529 task_lock(current);
Paul Menage8793d852007-10-18 23:39:39 -07002530 fmeter_markevent(&task_cs(current)->fmeter);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002531 task_unlock(current);
2532}
2533
Paul Menage8793d852007-10-18 23:39:39 -07002534#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002535/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 * proc_cpuset_show()
2537 * - Print tasks cpuset path into seq_file.
2538 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002539 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2540 * doesn't really matter if tsk->cpuset changes after we read it,
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002541 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002542 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 */
Paul Jackson029190c2007-10-18 23:40:20 -07002544static int proc_cpuset_show(struct seq_file *m, void *unused_v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002546 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 struct task_struct *tsk;
2548 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002549 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002550 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Eric W. Biederman99f89552006-06-26 00:25:55 -07002552 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2554 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Eric W. Biederman99f89552006-06-26 00:25:55 -07002557 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002558 pid = m->private;
2559 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002560 if (!tsk)
2561 goto out_free;
2562
2563 retval = -EINVAL;
Paul Menage8793d852007-10-18 23:39:39 -07002564 cgroup_lock();
2565 css = task_subsys_state(tsk, cpuset_subsys_id);
2566 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002568 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 seq_puts(m, buf);
2570 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002571out_unlock:
Paul Menage8793d852007-10-18 23:39:39 -07002572 cgroup_unlock();
Eric W. Biederman99f89552006-06-26 00:25:55 -07002573 put_task_struct(tsk);
2574out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002576out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return retval;
2578}
2579
2580static int cpuset_open(struct inode *inode, struct file *file)
2581{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002582 struct pid *pid = PROC_I(inode)->pid;
2583 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Arjan van de Ven9a321442007-02-12 00:55:35 -08002586const struct file_operations proc_cpuset_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 .open = cpuset_open,
2588 .read = seq_read,
2589 .llseek = seq_lseek,
2590 .release = single_release,
2591};
Paul Menage8793d852007-10-18 23:39:39 -07002592#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002595void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002597 seq_printf(m, "Cpus_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002598 seq_cpumask(m, &task->cpus_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002599 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002600 seq_printf(m, "Cpus_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002601 seq_cpumask_list(m, &task->cpus_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002602 seq_printf(m, "\n");
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002603 seq_printf(m, "Mems_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002604 seq_nodemask(m, &task->mems_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002605 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002606 seq_printf(m, "Mems_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002607 seq_nodemask_list(m, &task->mems_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002608 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}