blob: b5cb469d25456b03292d27e22ece4508ddba1ca2 [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
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800100 struct fmeter fmeter; /* memory_pressure filter */
Paul Jackson029190c2007-10-18 23:40:20 -0700101
102 /* partition number for rebuild_sched_domains() */
103 int pn;
Cliff Wickman956db3c2008-02-07 00:14:43 -0800104
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900105 /* for custom sched domain */
106 int relax_domain_level;
107
Cliff Wickman956db3c2008-02-07 00:14:43 -0800108 /* used for walking a cpuset heirarchy */
109 struct list_head stack_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
Paul Menage8793d852007-10-18 23:39:39 -0700112/* Retrieve the cpuset for a cgroup */
113static inline struct cpuset *cgroup_cs(struct cgroup *cont)
114{
115 return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
116 struct cpuset, css);
117}
118
119/* Retrieve the cpuset for a task */
120static inline struct cpuset *task_cs(struct task_struct *task)
121{
122 return container_of(task_subsys_state(task, cpuset_subsys_id),
123 struct cpuset, css);
124}
Paul Menage8793d852007-10-18 23:39:39 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* bits in struct cpuset flags field */
127typedef enum {
128 CS_CPU_EXCLUSIVE,
129 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700130 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800131 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700132 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800133 CS_SPREAD_PAGE,
134 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135} cpuset_flagbits_t;
136
137/* convenient tests for these bits */
138static inline int is_cpu_exclusive(const struct cpuset *cs)
139{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800140 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static inline int is_mem_exclusive(const struct cpuset *cs)
144{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800145 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Paul Menage78608362008-04-29 01:00:26 -0700148static inline int is_mem_hardwall(const struct cpuset *cs)
149{
150 return test_bit(CS_MEM_HARDWALL, &cs->flags);
151}
152
Paul Jackson029190c2007-10-18 23:40:20 -0700153static inline int is_sched_load_balance(const struct cpuset *cs)
154{
155 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
156}
157
Paul Jackson45b07ef2006-01-08 01:00:56 -0800158static inline int is_memory_migrate(const struct cpuset *cs)
159{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800160 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800161}
162
Paul Jackson825a46a2006-03-24 03:16:03 -0800163static inline int is_spread_page(const struct cpuset *cs)
164{
165 return test_bit(CS_SPREAD_PAGE, &cs->flags);
166}
167
168static inline int is_spread_slab(const struct cpuset *cs)
169{
170 return test_bit(CS_SPREAD_SLAB, &cs->flags);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static struct cpuset top_cpuset = {
174 .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Paul Menage2df167a2008-02-07 00:14:45 -0800178 * There are two global mutexes guarding cpuset structures. The first
179 * is the main control groups cgroup_mutex, accessed via
180 * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
181 * callback_mutex, below. They can nest. It is ok to first take
182 * cgroup_mutex, then nest callback_mutex. We also require taking
183 * task_lock() when dereferencing a task's cpuset pointer. See "The
184 * task_lock() exception", at the end of this comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800186 * A task must hold both mutexes to modify cpusets. If a task
Paul Menage2df167a2008-02-07 00:14:45 -0800187 * holds cgroup_mutex, then it blocks others wanting that mutex,
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800188 * ensuring that it is the only task able to also acquire callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800189 * and be able to modify cpusets. It can perform various checks on
190 * the cpuset structure first, knowing nothing will change. It can
Paul Menage2df167a2008-02-07 00:14:45 -0800191 * also allocate memory while just holding cgroup_mutex. While it is
Paul Jackson053199e2005-10-30 15:02:30 -0800192 * performing these checks, various callback routines can briefly
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800193 * acquire callback_mutex to query cpusets. Once it is ready to make
194 * the changes, it takes callback_mutex, blocking everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Paul Jackson053199e2005-10-30 15:02:30 -0800196 * Calls to the kernel memory allocator can not be made while holding
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800197 * callback_mutex, as that would risk double tripping on callback_mutex
Paul Jackson053199e2005-10-30 15:02:30 -0800198 * from one of the callbacks into the cpuset code from within
199 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800201 * If a task is only holding callback_mutex, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800202 * access to cpusets.
203 *
Miao Xie58568d22009-06-16 15:31:49 -0700204 * Now, the task_struct fields mems_allowed and mempolicy may be changed
205 * by other task, we use alloc_lock in the task_struct fields to protect
206 * them.
Paul Jackson053199e2005-10-30 15:02:30 -0800207 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800208 * The cpuset_common_file_read() handlers only hold callback_mutex across
Paul Jackson053199e2005-10-30 15:02:30 -0800209 * small pieces of code, such as when reading out possibly multi-word
210 * cpumasks and nodemasks.
211 *
Paul Menage2df167a2008-02-07 00:14:45 -0800212 * Accessing a task's cpuset should be done in accordance with the
213 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800216static DEFINE_MUTEX(callback_mutex);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700217
Max Krasnyanskycf417142008-08-11 14:33:53 -0700218/*
David Rientjes75aa1992009-01-06 14:39:01 -0800219 * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist
220 * buffers. They are statically allocated to prevent using excess stack
221 * when calling cpuset_print_task_mems_allowed().
222 */
223#define CPUSET_NAME_LEN (128)
224#define CPUSET_NODELIST_LEN (256)
225static char cpuset_name[CPUSET_NAME_LEN];
226static char cpuset_nodelist[CPUSET_NODELIST_LEN];
227static DEFINE_SPINLOCK(cpuset_buffer_lock);
228
229/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700230 * This is ugly, but preserves the userspace API for existing cpuset
Paul Menage8793d852007-10-18 23:39:39 -0700231 * users. If someone tries to mount the "cpuset" filesystem, we
Max Krasnyanskycf417142008-08-11 14:33:53 -0700232 * silently switch it to mount "cgroup" instead
233 */
David Howells454e2392006-06-23 02:02:57 -0700234static int cpuset_get_sb(struct file_system_type *fs_type,
235 int flags, const char *unused_dev_name,
236 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Paul Menage8793d852007-10-18 23:39:39 -0700238 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
239 int ret = -ENODEV;
240 if (cgroup_fs) {
241 char mountopts[] =
242 "cpuset,noprefix,"
243 "release_agent=/sbin/cpuset_release_agent";
244 ret = cgroup_fs->get_sb(cgroup_fs, flags,
245 unused_dev_name, mountopts, mnt);
246 put_filesystem(cgroup_fs);
247 }
248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static struct file_system_type cpuset_fs_type = {
252 .name = "cpuset",
253 .get_sb = cpuset_get_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800257 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * are online. If none are online, walk up the cpuset hierarchy
259 * until we find one that does have some online cpus. If we get
260 * all the way to the top and still haven't found any online cpus,
261 * return cpu_online_map. Or if passed a NULL cs from an exit'ing
262 * task, return cpu_online_map.
263 *
264 * One way or another, we guarantee to return some non-empty subset
265 * of cpu_online_map.
266 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800267 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269
Li Zefan6af866a2009-01-07 18:08:45 -0800270static void guarantee_online_cpus(const struct cpuset *cs,
271 struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Li Zefan300ed6c2009-01-07 18:08:44 -0800273 while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 cs = cs->parent;
275 if (cs)
Li Zefan300ed6c2009-01-07 18:08:44 -0800276 cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 else
Li Zefan300ed6c2009-01-07 18:08:44 -0800278 cpumask_copy(pmask, cpu_online_mask);
279 BUG_ON(!cpumask_intersects(pmask, cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700284 * are online, with memory. If none are online with memory, walk
285 * up the cpuset hierarchy until we find one that does have some
286 * online mems. If we get all the way to the top and still haven't
287 * found any online mems, return node_states[N_HIGH_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
289 * One way or another, we guarantee to return some non-empty subset
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700290 * of node_states[N_HIGH_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800292 * Call with callback_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294
295static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
296{
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700297 while (cs && !nodes_intersects(cs->mems_allowed,
298 node_states[N_HIGH_MEMORY]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 cs = cs->parent;
300 if (cs)
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700301 nodes_and(*pmask, cs->mems_allowed,
302 node_states[N_HIGH_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 else
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700304 *pmask = node_states[N_HIGH_MEMORY];
305 BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Miao Xief3b39d42009-06-16 15:31:46 -0700308/*
309 * update task's spread flag if cpuset's page/slab spread flag is set
310 *
311 * Called with callback_mutex/cgroup_mutex held
312 */
313static void cpuset_update_task_spread_flag(struct cpuset *cs,
314 struct task_struct *tsk)
315{
316 if (is_spread_page(cs))
317 tsk->flags |= PF_SPREAD_PAGE;
318 else
319 tsk->flags &= ~PF_SPREAD_PAGE;
320 if (is_spread_slab(cs))
321 tsk->flags |= PF_SPREAD_SLAB;
322 else
323 tsk->flags &= ~PF_SPREAD_SLAB;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*
327 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
328 *
329 * One cpuset is a subset of another if all its allowed CPUs and
330 * Memory Nodes are a subset of the other, and its exclusive flags
Paul Menage2df167a2008-02-07 00:14:45 -0800331 * are only set if the other's are set. Call holding cgroup_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
333
334static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
335{
Li Zefan300ed6c2009-01-07 18:08:44 -0800336 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 nodes_subset(p->mems_allowed, q->mems_allowed) &&
338 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
339 is_mem_exclusive(p) <= is_mem_exclusive(q);
340}
341
Li Zefan645fcc92009-01-07 18:08:43 -0800342/**
343 * alloc_trial_cpuset - allocate a trial cpuset
344 * @cs: the cpuset that the trial cpuset duplicates
345 */
346static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs)
347{
Li Zefan300ed6c2009-01-07 18:08:44 -0800348 struct cpuset *trial;
349
350 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
351 if (!trial)
352 return NULL;
353
354 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) {
355 kfree(trial);
356 return NULL;
357 }
358 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
359
360 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800361}
362
363/**
364 * free_trial_cpuset - free the trial cpuset
365 * @trial: the trial cpuset to be freed
366 */
367static void free_trial_cpuset(struct cpuset *trial)
368{
Li Zefan300ed6c2009-01-07 18:08:44 -0800369 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800370 kfree(trial);
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
374 * validate_change() - Used to validate that any proposed cpuset change
375 * follows the structural rules for cpusets.
376 *
377 * If we replaced the flag and mask values of the current cpuset
378 * (cur) with those values in the trial cpuset (trial), would
379 * our various subset and exclusive rules still be valid? Presumes
Paul Menage2df167a2008-02-07 00:14:45 -0800380 * cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
382 * 'cur' is the address of an actual, in-use cpuset. Operations
383 * such as list traversal that depend on the actual address of the
384 * cpuset in the list must use cur below, not trial.
385 *
386 * 'trial' is the address of bulk structure copy of cur, with
387 * perhaps one or more of the fields cpus_allowed, mems_allowed,
388 * or flags changed to new, trial values.
389 *
390 * Return 0 if valid, -errno if not.
391 */
392
393static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
394{
Paul Menage8793d852007-10-18 23:39:39 -0700395 struct cgroup *cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct cpuset *c, *par;
397
398 /* Each of our child cpusets must be a subset of us */
Paul Menage8793d852007-10-18 23:39:39 -0700399 list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
400 if (!is_cpuset_subset(cgroup_cs(cont), trial))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -EBUSY;
402 }
403
404 /* Remaining checks don't apply to root cpuset */
Paul Jackson69604062006-12-06 20:36:15 -0800405 if (cur == &top_cpuset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407
Paul Jackson69604062006-12-06 20:36:15 -0800408 par = cur->parent;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* We must be a subset of our parent cpuset */
411 if (!is_cpuset_subset(trial, par))
412 return -EACCES;
413
Paul Menage2df167a2008-02-07 00:14:45 -0800414 /*
415 * If either I or some sibling (!= me) is exclusive, we can't
416 * overlap
417 */
Paul Menage8793d852007-10-18 23:39:39 -0700418 list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
419 c = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
421 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800422 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EINVAL;
424 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
425 c != cur &&
426 nodes_intersects(trial->mems_allowed, c->mems_allowed))
427 return -EINVAL;
428 }
429
Paul Jackson020958b2007-10-18 23:40:21 -0700430 /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
431 if (cgroup_task_count(cur->css.cgroup)) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800432 if (cpumask_empty(trial->cpus_allowed) ||
Paul Jackson020958b2007-10-18 23:40:21 -0700433 nodes_empty(trial->mems_allowed)) {
434 return -ENOSPC;
435 }
436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
Paul Menagedb7f47c2009-04-02 16:57:55 -0700441#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700442/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700443 * Helper routine for generate_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -0700444 * Do cpusets a, b have overlapping cpus_allowed masks?
445 */
Paul Jackson029190c2007-10-18 23:40:20 -0700446static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
447{
Li Zefan300ed6c2009-01-07 18:08:44 -0800448 return cpumask_intersects(a->cpus_allowed, b->cpus_allowed);
Paul Jackson029190c2007-10-18 23:40:20 -0700449}
450
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900451static void
452update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
453{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900454 if (dattr->relax_domain_level < c->relax_domain_level)
455 dattr->relax_domain_level = c->relax_domain_level;
456 return;
457}
458
Lai Jiangshanf5393692008-07-29 22:33:22 -0700459static void
460update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
461{
462 LIST_HEAD(q);
463
464 list_add(&c->stack_list, &q);
465 while (!list_empty(&q)) {
466 struct cpuset *cp;
467 struct cgroup *cont;
468 struct cpuset *child;
469
470 cp = list_first_entry(&q, struct cpuset, stack_list);
471 list_del(q.next);
472
Li Zefan300ed6c2009-01-07 18:08:44 -0800473 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700474 continue;
475
476 if (is_sched_load_balance(cp))
477 update_domain_attr(dattr, cp);
478
479 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
480 child = cgroup_cs(cont);
481 list_add_tail(&child->stack_list, &q);
482 }
483 }
484}
485
Paul Jackson029190c2007-10-18 23:40:20 -0700486/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700487 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700488 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700489 * This function builds a partial partition of the systems CPUs
490 * A 'partial partition' is a set of non-overlapping subsets whose
491 * union is a subset of that set.
492 * The output of this function needs to be passed to kernel/sched.c
493 * partition_sched_domains() routine, which will rebuild the scheduler's
494 * load balancing domains (sched domains) as specified by that partial
495 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700496 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800497 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700498 * for a background explanation of this.
499 *
500 * Does not return errors, on the theory that the callers of this
501 * routine would rather not worry about failures to rebuild sched
502 * domains when operating in the severe memory shortage situations
503 * that could cause allocation failures below.
504 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700505 * Must be called with cgroup_lock held.
Paul Jackson029190c2007-10-18 23:40:20 -0700506 *
507 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700508 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700509 * top-down scan of all cpusets. This scan loads a pointer
510 * to each cpuset marked is_sched_load_balance into the
511 * array 'csa'. For our purposes, rebuilding the schedulers
512 * sched domains, we can ignore !is_sched_load_balance cpusets.
513 * csa - (for CpuSet Array) Array of pointers to all the cpusets
514 * that need to be load balanced, for convenient iterative
515 * access by the subsequent code that finds the best partition,
516 * i.e the set of domains (subsets) of CPUs such that the
517 * cpus_allowed of every cpuset marked is_sched_load_balance
518 * is a subset of one of these domains, while there are as
519 * many such domains as possible, each as small as possible.
520 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
521 * the kernel/sched.c routine partition_sched_domains() in a
522 * convenient format, that can be easily compared to the prior
523 * value to determine what partition elements (sched domains)
524 * were changed (added or removed.)
525 *
526 * Finding the best partition (set of domains):
527 * The triple nested loops below over i, j, k scan over the
528 * load balanced cpusets (using the array of cpuset pointers in
529 * csa[]) looking for pairs of cpusets that have overlapping
530 * cpus_allowed, but which don't have the same 'pn' partition
531 * number and gives them in the same partition number. It keeps
532 * looping on the 'restart' label until it can no longer find
533 * any such pairs.
534 *
535 * The union of the cpus_allowed masks from the set of
536 * all cpusets having the same 'pn' value then form the one
537 * element of the partition (one sched domain) to be passed to
538 * partition_sched_domains().
539 */
Li Zefan6af866a2009-01-07 18:08:45 -0800540/* FIXME: see the FIXME in partition_sched_domains() */
541static int generate_sched_domains(struct cpumask **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700542 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700543{
Max Krasnyanskycf417142008-08-11 14:33:53 -0700544 LIST_HEAD(q); /* queue of cpusets to be scanned */
Paul Jackson029190c2007-10-18 23:40:20 -0700545 struct cpuset *cp; /* scans q */
546 struct cpuset **csa; /* array of all cpuset ptrs */
547 int csn; /* how many cpuset ptrs in csa so far */
548 int i, j, k; /* indices for partition finding loops */
Li Zefan6af866a2009-01-07 18:08:45 -0800549 struct cpumask *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900550 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100551 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800552 int nslot; /* next empty doms[] struct cpumask slot */
Paul Jackson029190c2007-10-18 23:40:20 -0700553
Paul Jackson029190c2007-10-18 23:40:20 -0700554 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900555 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700556 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700557
558 /* Special case for the 99% of systems with one, full, sched domain */
559 if (is_sched_load_balance(&top_cpuset)) {
Li Zefan6af866a2009-01-07 18:08:45 -0800560 doms = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700561 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700562 goto done;
563
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900564 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
565 if (dattr) {
566 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700567 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900568 }
Li Zefan300ed6c2009-01-07 18:08:44 -0800569 cpumask_copy(doms, top_cpuset.cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700570
571 ndoms = 1;
572 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700573 }
574
Paul Jackson029190c2007-10-18 23:40:20 -0700575 csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
576 if (!csa)
577 goto done;
578 csn = 0;
579
Li Zefanaeed6822008-07-29 22:33:24 -0700580 list_add(&top_cpuset.stack_list, &q);
581 while (!list_empty(&q)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700582 struct cgroup *cont;
583 struct cpuset *child; /* scans child cpusets of cp */
Lai Jiangshan489a5392008-07-25 01:47:23 -0700584
Li Zefanaeed6822008-07-29 22:33:24 -0700585 cp = list_first_entry(&q, struct cpuset, stack_list);
586 list_del(q.next);
587
Li Zefan300ed6c2009-01-07 18:08:44 -0800588 if (cpumask_empty(cp->cpus_allowed))
Lai Jiangshan489a5392008-07-25 01:47:23 -0700589 continue;
590
Lai Jiangshanf5393692008-07-29 22:33:22 -0700591 /*
592 * All child cpusets contain a subset of the parent's cpus, so
593 * just skip them, and then we call update_domain_attr_tree()
594 * to calc relax_domain_level of the corresponding sched
595 * domain.
596 */
597 if (is_sched_load_balance(cp)) {
Paul Jackson029190c2007-10-18 23:40:20 -0700598 csa[csn++] = cp;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700599 continue;
600 }
Lai Jiangshan489a5392008-07-25 01:47:23 -0700601
Paul Jackson029190c2007-10-18 23:40:20 -0700602 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
603 child = cgroup_cs(cont);
Li Zefanaeed6822008-07-29 22:33:24 -0700604 list_add_tail(&child->stack_list, &q);
Paul Jackson029190c2007-10-18 23:40:20 -0700605 }
606 }
607
608 for (i = 0; i < csn; i++)
609 csa[i]->pn = i;
610 ndoms = csn;
611
612restart:
613 /* Find the best partition (set of sched domains) */
614 for (i = 0; i < csn; i++) {
615 struct cpuset *a = csa[i];
616 int apn = a->pn;
617
618 for (j = 0; j < csn; j++) {
619 struct cpuset *b = csa[j];
620 int bpn = b->pn;
621
622 if (apn != bpn && cpusets_overlap(a, b)) {
623 for (k = 0; k < csn; k++) {
624 struct cpuset *c = csa[k];
625
626 if (c->pn == bpn)
627 c->pn = apn;
628 }
629 ndoms--; /* one less element */
630 goto restart;
631 }
632 }
633 }
634
Max Krasnyanskycf417142008-08-11 14:33:53 -0700635 /*
636 * Now we know how many domains to create.
637 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
638 */
Li Zefan6af866a2009-01-07 18:08:45 -0800639 doms = kmalloc(ndoms * cpumask_size(), GFP_KERNEL);
Li Zefan700018e2008-11-18 14:02:03 +0800640 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700641 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700642
643 /*
644 * The rest of the code, including the scheduler, can deal with
645 * dattr==NULL case. No need to abort if alloc fails.
646 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900647 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700648
649 for (nslot = 0, i = 0; i < csn; i++) {
650 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800651 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700652 int apn = a->pn;
653
Max Krasnyanskycf417142008-08-11 14:33:53 -0700654 if (apn < 0) {
655 /* Skip completed partitions */
656 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700657 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700658
659 dp = doms + nslot;
660
661 if (nslot == ndoms) {
662 static int warnings = 10;
663 if (warnings) {
664 printk(KERN_WARNING
665 "rebuild_sched_domains confused:"
666 " nslot %d, ndoms %d, csn %d, i %d,"
667 " apn %d\n",
668 nslot, ndoms, csn, i, apn);
669 warnings--;
670 }
671 continue;
672 }
673
Li Zefan6af866a2009-01-07 18:08:45 -0800674 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700675 if (dattr)
676 *(dattr + nslot) = SD_ATTR_INIT;
677 for (j = i; j < csn; j++) {
678 struct cpuset *b = csa[j];
679
680 if (apn == b->pn) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800681 cpumask_or(dp, dp, b->cpus_allowed);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700682 if (dattr)
683 update_domain_attr_tree(dattr + nslot, b);
684
685 /* Done with this partition */
686 b->pn = -1;
687 }
688 }
689 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700690 }
691 BUG_ON(nslot != ndoms);
692
Paul Jackson029190c2007-10-18 23:40:20 -0700693done:
Paul Jackson029190c2007-10-18 23:40:20 -0700694 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700695
Li Zefan700018e2008-11-18 14:02:03 +0800696 /*
697 * Fallback to the default domain if kmalloc() failed.
698 * See comments in partition_sched_domains().
699 */
700 if (doms == NULL)
701 ndoms = 1;
702
Max Krasnyanskycf417142008-08-11 14:33:53 -0700703 *domains = doms;
704 *attributes = dattr;
705 return ndoms;
706}
707
708/*
709 * Rebuild scheduler domains.
710 *
711 * Call with neither cgroup_mutex held nor within get_online_cpus().
712 * Takes both cgroup_mutex and get_online_cpus().
713 *
714 * Cannot be directly called from cpuset code handling changes
715 * to the cpuset pseudo-filesystem, because it cannot be called
716 * from code that already holds cgroup_mutex.
717 */
718static void do_rebuild_sched_domains(struct work_struct *unused)
719{
720 struct sched_domain_attr *attr;
Li Zefan6af866a2009-01-07 18:08:45 -0800721 struct cpumask *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700722 int ndoms;
723
724 get_online_cpus();
725
726 /* Generate domain masks and attrs */
727 cgroup_lock();
728 ndoms = generate_sched_domains(&doms, &attr);
729 cgroup_unlock();
730
731 /* Have scheduler rebuild the domains */
732 partition_sched_domains(ndoms, doms, attr);
733
734 put_online_cpus();
735}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700736#else /* !CONFIG_SMP */
737static void do_rebuild_sched_domains(struct work_struct *unused)
738{
739}
740
741static int generate_sched_domains(struct cpumask **domains,
742 struct sched_domain_attr **attributes)
743{
744 *domains = NULL;
745 return 1;
746}
747#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700748
749static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
750
751/*
752 * Rebuild scheduler domains, asynchronously via workqueue.
753 *
754 * If the flag 'sched_load_balance' of any cpuset with non-empty
755 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
756 * which has that flag enabled, or if any cpuset with a non-empty
757 * 'cpus' is removed, then call this routine to rebuild the
758 * scheduler's dynamic sched domains.
759 *
760 * The rebuild_sched_domains() and partition_sched_domains()
761 * routines must nest cgroup_lock() inside get_online_cpus(),
762 * but such cpuset changes as these must nest that locking the
763 * other way, holding cgroup_lock() for much of the code.
764 *
765 * So in order to avoid an ABBA deadlock, the cpuset code handling
766 * these user changes delegates the actual sched domain rebuilding
767 * to a separate workqueue thread, which ends up processing the
768 * above do_rebuild_sched_domains() function.
769 */
770static void async_rebuild_sched_domains(void)
771{
Miao Xief90d4112009-01-16 10:24:10 +0800772 queue_work(cpuset_wq, &rebuild_sched_domains_work);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700773}
774
775/*
776 * Accomplishes the same scheduler domain rebuild as the above
777 * async_rebuild_sched_domains(), however it directly calls the
778 * rebuild routine synchronously rather than calling it via an
779 * asynchronous work thread.
780 *
781 * This can only be called from code that is not holding
782 * cgroup_mutex (not nested in a cgroup_lock() call.)
783 */
784void rebuild_sched_domains(void)
785{
786 do_rebuild_sched_domains(NULL);
Paul Jackson029190c2007-10-18 23:40:20 -0700787}
788
Cliff Wickman58f47902008-02-07 00:14:44 -0800789/**
790 * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
791 * @tsk: task to test
792 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
793 *
Paul Menage2df167a2008-02-07 00:14:45 -0800794 * Call with cgroup_mutex held. May take callback_mutex during call.
Cliff Wickman58f47902008-02-07 00:14:44 -0800795 * Called for each task in a cgroup by cgroup_scan_tasks().
796 * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
797 * words, if its mask is not equal to its cpuset's mask).
Paul Jackson053199e2005-10-30 15:02:30 -0800798 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700799static int cpuset_test_cpumask(struct task_struct *tsk,
800 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800801{
Li Zefan300ed6c2009-01-07 18:08:44 -0800802 return !cpumask_equal(&tsk->cpus_allowed,
Cliff Wickman58f47902008-02-07 00:14:44 -0800803 (cgroup_cs(scan->cg))->cpus_allowed);
804}
Paul Jackson053199e2005-10-30 15:02:30 -0800805
Cliff Wickman58f47902008-02-07 00:14:44 -0800806/**
807 * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
808 * @tsk: task to test
809 * @scan: struct cgroup_scanner containing the cgroup of the task
810 *
811 * Called by cgroup_scan_tasks() for each task in a cgroup whose
812 * cpus_allowed mask needs to be changed.
813 *
814 * We don't need to re-check for the cgroup/cpuset membership, since we're
815 * holding cgroup_lock() at this point.
816 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -0700817static void cpuset_change_cpumask(struct task_struct *tsk,
818 struct cgroup_scanner *scan)
Cliff Wickman58f47902008-02-07 00:14:44 -0800819{
Li Zefan300ed6c2009-01-07 18:08:44 -0800820 set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed));
Cliff Wickman58f47902008-02-07 00:14:44 -0800821}
822
823/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700824 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
825 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
Li Zefan4e743392008-09-13 02:33:08 -0700826 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -0700827 *
828 * Called with cgroup_mutex held
829 *
830 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
831 * calling callback functions for each.
832 *
Li Zefan4e743392008-09-13 02:33:08 -0700833 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
834 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -0700835 */
Li Zefan4e743392008-09-13 02:33:08 -0700836static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
Miao Xie0b2f6302008-07-25 01:47:21 -0700837{
838 struct cgroup_scanner scan;
Miao Xie0b2f6302008-07-25 01:47:21 -0700839
840 scan.cg = cs->css.cgroup;
841 scan.test_task = cpuset_test_cpumask;
842 scan.process_task = cpuset_change_cpumask;
Li Zefan4e743392008-09-13 02:33:08 -0700843 scan.heap = heap;
844 cgroup_scan_tasks(&scan);
Miao Xie0b2f6302008-07-25 01:47:21 -0700845}
846
847/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800848 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
849 * @cs: the cpuset to consider
850 * @buf: buffer of cpu numbers written to this cpuset
851 */
Li Zefan645fcc92009-01-07 18:08:43 -0800852static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
853 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Li Zefan4e743392008-09-13 02:33:08 -0700855 struct ptr_heap heap;
Cliff Wickman58f47902008-02-07 00:14:44 -0800856 int retval;
857 int is_load_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700859 /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
860 if (cs == &top_cpuset)
861 return -EACCES;
862
David Rientjes6f7f02e2007-05-08 00:31:43 -0700863 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800864 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700865 * Since cpulist_parse() fails on an empty mask, we special case
866 * that parsing. The validate_change() call ensures that cpusets
867 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700868 */
Paul Jackson020958b2007-10-18 23:40:21 -0700869 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800870 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700871 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -0800872 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700873 if (retval < 0)
874 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700875
Li Zefan300ed6c2009-01-07 18:08:44 -0800876 if (!cpumask_subset(trialcs->cpus_allowed, cpu_online_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700877 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700878 }
Li Zefan645fcc92009-01-07 18:08:43 -0800879 retval = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700880 if (retval < 0)
881 return retval;
Paul Jackson029190c2007-10-18 23:40:20 -0700882
Paul Menage8707d8b2007-10-18 23:40:22 -0700883 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800884 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700885 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800886
Li Zefan4e743392008-09-13 02:33:08 -0700887 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
888 if (retval)
889 return retval;
890
Li Zefan645fcc92009-01-07 18:08:43 -0800891 is_load_balanced = is_sched_load_balance(trialcs);
Paul Jackson029190c2007-10-18 23:40:20 -0700892
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800893 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -0800894 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -0800895 mutex_unlock(&callback_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700896
Paul Menage8707d8b2007-10-18 23:40:22 -0700897 /*
898 * Scan tasks in the cpuset, and update the cpumasks of any
Cliff Wickman58f47902008-02-07 00:14:44 -0800899 * that need an update.
Paul Menage8707d8b2007-10-18 23:40:22 -0700900 */
Li Zefan4e743392008-09-13 02:33:08 -0700901 update_tasks_cpumask(cs, &heap);
902
903 heap_free(&heap);
Cliff Wickman58f47902008-02-07 00:14:44 -0800904
Paul Menage8707d8b2007-10-18 23:40:22 -0700905 if (is_load_balanced)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700906 async_rebuild_sched_domains();
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Paul Jackson053199e2005-10-30 15:02:30 -0800910/*
Paul Jacksone4e364e2006-03-31 02:30:52 -0800911 * cpuset_migrate_mm
912 *
913 * Migrate memory region from one set of nodes to another.
914 *
915 * Temporarilly set tasks mems_allowed to target nodes of migration,
916 * so that the migration code can allocate pages on these nodes.
917 *
Paul Menage2df167a2008-02-07 00:14:45 -0800918 * Call holding cgroup_mutex, so current's cpuset won't change
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800919 * during this call, as manage_mutex holds off any cpuset_attach()
Paul Jacksone4e364e2006-03-31 02:30:52 -0800920 * calls. Therefore we don't need to take task_lock around the
921 * call to guarantee_online_mems(), as we know no one is changing
Paul Menage2df167a2008-02-07 00:14:45 -0800922 * our task's cpuset.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800923 *
924 * Hold callback_mutex around the two modifications of our tasks
925 * mems_allowed to synchronize with cpuset_mems_allowed().
926 *
927 * While the mm_struct we are migrating is typically from some
928 * other task, the task_struct mems_allowed that we are hacking
929 * is for our current task, which must allocate new pages for that
930 * migrating memory region.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800931 */
932
933static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
934 const nodemask_t *to)
935{
936 struct task_struct *tsk = current;
937
Paul Jacksone4e364e2006-03-31 02:30:52 -0800938 tsk->mems_allowed = *to;
Paul Jacksone4e364e2006-03-31 02:30:52 -0800939
940 do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
941
Paul Menage8793d852007-10-18 23:39:39 -0700942 guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
Paul Jacksone4e364e2006-03-31 02:30:52 -0800943}
944
Li Zefan3b6766f2009-04-02 16:57:51 -0700945/*
Miao Xie58568d22009-06-16 15:31:49 -0700946 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
947 * @tsk: the task to change
948 * @newmems: new nodes that the task will be set
949 *
950 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
951 * we structure updates as setting all new allowed nodes, then clearing newly
952 * disallowed ones.
953 *
954 * Called with task's alloc_lock held
955 */
956static void cpuset_change_task_nodemask(struct task_struct *tsk,
957 nodemask_t *newmems)
958{
959 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
960 mpol_rebind_task(tsk, &tsk->mems_allowed);
961 mpol_rebind_task(tsk, newmems);
962 tsk->mems_allowed = *newmems;
963}
964
965/*
966 * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy
967 * of it to cpuset's new mems_allowed, and migrate pages to new nodes if
968 * memory_migrate flag is set. Called with cgroup_mutex held.
Li Zefan3b6766f2009-04-02 16:57:51 -0700969 */
970static void cpuset_change_nodemask(struct task_struct *p,
971 struct cgroup_scanner *scan)
972{
973 struct mm_struct *mm;
974 struct cpuset *cs;
975 int migrate;
976 const nodemask_t *oldmem = scan->data;
Miao Xie58568d22009-06-16 15:31:49 -0700977 nodemask_t newmems;
978
979 cs = cgroup_cs(scan->cg);
980 guarantee_online_mems(cs, &newmems);
981
982 task_lock(p);
983 cpuset_change_task_nodemask(p, &newmems);
984 task_unlock(p);
Li Zefan3b6766f2009-04-02 16:57:51 -0700985
986 mm = get_task_mm(p);
987 if (!mm)
988 return;
989
Li Zefan3b6766f2009-04-02 16:57:51 -0700990 migrate = is_memory_migrate(cs);
991
992 mpol_rebind_mm(mm, &cs->mems_allowed);
993 if (migrate)
994 cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
995 mmput(mm);
996}
997
Paul Menage8793d852007-10-18 23:39:39 -0700998static void *cpuset_being_rebound;
999
Miao Xie0b2f6302008-07-25 01:47:21 -07001000/**
1001 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1002 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
1003 * @oldmem: old mems_allowed of cpuset cs
Li Zefan010cfac2009-04-02 16:57:52 -07001004 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
Miao Xie0b2f6302008-07-25 01:47:21 -07001005 *
1006 * Called with cgroup_mutex held
Li Zefan010cfac2009-04-02 16:57:52 -07001007 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1008 * if @heap != NULL.
Miao Xie0b2f6302008-07-25 01:47:21 -07001009 */
Li Zefan010cfac2009-04-02 16:57:52 -07001010static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem,
1011 struct ptr_heap *heap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Li Zefan3b6766f2009-04-02 16:57:51 -07001013 struct cgroup_scanner scan;
Paul Jackson59dac162006-01-08 01:01:52 -08001014
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001015 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001016
Li Zefan3b6766f2009-04-02 16:57:51 -07001017 scan.cg = cs->css.cgroup;
1018 scan.test_task = NULL;
1019 scan.process_task = cpuset_change_nodemask;
Li Zefan010cfac2009-04-02 16:57:52 -07001020 scan.heap = heap;
Li Zefan3b6766f2009-04-02 16:57:51 -07001021 scan.data = (nodemask_t *)oldmem;
Paul Jackson42253992006-01-08 01:01:59 -08001022
1023 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001024 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1025 * take while holding tasklist_lock. Forks can happen - the
1026 * mpol_dup() cpuset_being_rebound check will catch such forks,
1027 * and rebind their vma mempolicies too. Because we still hold
1028 * the global cgroup_mutex, we know that no other rebind effort
1029 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001030 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001031 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001032 */
Li Zefan010cfac2009-04-02 16:57:52 -07001033 cgroup_scan_tasks(&scan);
Paul Jackson42253992006-01-08 01:01:59 -08001034
Paul Menage2df167a2008-02-07 00:14:45 -08001035 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001036 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Miao Xie0b2f6302008-07-25 01:47:21 -07001039/*
1040 * Handle user request to change the 'mems' memory placement
1041 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001042 * cpusets mems_allowed, and for each task in the cpuset,
1043 * update mems_allowed and rebind task's mempolicy and any vma
1044 * mempolicies and if the cpuset is marked 'memory_migrate',
1045 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001046 *
1047 * Call with cgroup_mutex held. May take callback_mutex during call.
1048 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1049 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1050 * their mempolicies to the cpusets new mems_allowed.
1051 */
Li Zefan645fcc92009-01-07 18:08:43 -08001052static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1053 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001054{
Miao Xie0b2f6302008-07-25 01:47:21 -07001055 nodemask_t oldmem;
1056 int retval;
Li Zefan010cfac2009-04-02 16:57:52 -07001057 struct ptr_heap heap;
Miao Xie0b2f6302008-07-25 01:47:21 -07001058
1059 /*
1060 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
1061 * it's read-only
1062 */
1063 if (cs == &top_cpuset)
1064 return -EACCES;
1065
Miao Xie0b2f6302008-07-25 01:47:21 -07001066 /*
1067 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1068 * Since nodelist_parse() fails on an empty mask, we special case
1069 * that parsing. The validate_change() call ensures that cpusets
1070 * with tasks have memory.
1071 */
1072 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001073 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001074 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001075 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001076 if (retval < 0)
1077 goto done;
1078
Li Zefan645fcc92009-01-07 18:08:43 -08001079 if (!nodes_subset(trialcs->mems_allowed,
Miao Xie0b2f6302008-07-25 01:47:21 -07001080 node_states[N_HIGH_MEMORY]))
1081 return -EINVAL;
1082 }
1083 oldmem = cs->mems_allowed;
Li Zefan645fcc92009-01-07 18:08:43 -08001084 if (nodes_equal(oldmem, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001085 retval = 0; /* Too easy - nothing to do */
1086 goto done;
1087 }
Li Zefan645fcc92009-01-07 18:08:43 -08001088 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001089 if (retval < 0)
1090 goto done;
1091
Li Zefan010cfac2009-04-02 16:57:52 -07001092 retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1093 if (retval < 0)
1094 goto done;
1095
Miao Xie0b2f6302008-07-25 01:47:21 -07001096 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001097 cs->mems_allowed = trialcs->mems_allowed;
Miao Xie0b2f6302008-07-25 01:47:21 -07001098 mutex_unlock(&callback_mutex);
1099
Li Zefan010cfac2009-04-02 16:57:52 -07001100 update_tasks_nodemask(cs, &oldmem, &heap);
1101
1102 heap_free(&heap);
Miao Xie0b2f6302008-07-25 01:47:21 -07001103done:
1104 return retval;
1105}
1106
Paul Menage8793d852007-10-18 23:39:39 -07001107int current_cpuset_is_being_rebound(void)
1108{
1109 return task_cs(current) == cpuset_being_rebound;
1110}
1111
Paul Menage5be7a472008-05-06 20:42:41 -07001112static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001113{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001114#ifdef CONFIG_SMP
Li Zefan30e0e172008-05-13 10:27:17 +08001115 if (val < -1 || val >= SD_LV_MAX)
1116 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001117#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001118
1119 if (val != cs->relax_domain_level) {
1120 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001121 if (!cpumask_empty(cs->cpus_allowed) &&
1122 is_sched_load_balance(cs))
Max Krasnyanskycf417142008-08-11 14:33:53 -07001123 async_rebuild_sched_domains();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001124 }
1125
1126 return 0;
1127}
1128
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001129/*
Miao Xie950592f2009-06-16 15:31:47 -07001130 * cpuset_change_flag - make a task's spread flags the same as its cpuset's
1131 * @tsk: task to be updated
1132 * @scan: struct cgroup_scanner containing the cgroup of the task
1133 *
1134 * Called by cgroup_scan_tasks() for each task in a cgroup.
1135 *
1136 * We don't need to re-check for the cgroup/cpuset membership, since we're
1137 * holding cgroup_lock() at this point.
1138 */
1139static void cpuset_change_flag(struct task_struct *tsk,
1140 struct cgroup_scanner *scan)
1141{
1142 cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk);
1143}
1144
1145/*
1146 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1147 * @cs: the cpuset in which each task's spread flags needs to be changed
1148 * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
1149 *
1150 * Called with cgroup_mutex held
1151 *
1152 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1153 * calling callback functions for each.
1154 *
1155 * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
1156 * if @heap != NULL.
1157 */
1158static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap)
1159{
1160 struct cgroup_scanner scan;
1161
1162 scan.cg = cs->css.cgroup;
1163 scan.test_task = NULL;
1164 scan.process_task = cpuset_change_flag;
1165 scan.heap = heap;
1166 cgroup_scan_tasks(&scan);
1167}
1168
1169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001171 * bit: the bit to update (see cpuset_flagbits_t)
1172 * cs: the cpuset to update
1173 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001174 *
Paul Menage2df167a2008-02-07 00:14:45 -08001175 * Call with cgroup_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
1177
Paul Menage700fe1a2008-04-29 01:00:00 -07001178static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1179 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Li Zefan645fcc92009-01-07 18:08:43 -08001181 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001182 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001183 int spread_flag_changed;
1184 struct ptr_heap heap;
1185 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Li Zefan645fcc92009-01-07 18:08:43 -08001187 trialcs = alloc_trial_cpuset(cs);
1188 if (!trialcs)
1189 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Li Zefan645fcc92009-01-07 18:08:43 -08001191 if (turning_on)
1192 set_bit(bit, &trialcs->flags);
1193 else
1194 clear_bit(bit, &trialcs->flags);
1195
1196 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001197 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001198 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001199
Miao Xie950592f2009-06-16 15:31:47 -07001200 err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
1201 if (err < 0)
1202 goto out;
1203
Paul Jackson029190c2007-10-18 23:40:20 -07001204 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001205 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001206
Miao Xie950592f2009-06-16 15:31:47 -07001207 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1208 || (is_spread_page(cs) != is_spread_page(trialcs)));
1209
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001210 mutex_lock(&callback_mutex);
Li Zefan645fcc92009-01-07 18:08:43 -08001211 cs->flags = trialcs->flags;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001212 mutex_unlock(&callback_mutex);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001213
Li Zefan300ed6c2009-01-07 18:08:44 -08001214 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Max Krasnyanskycf417142008-08-11 14:33:53 -07001215 async_rebuild_sched_domains();
Paul Jackson029190c2007-10-18 23:40:20 -07001216
Miao Xie950592f2009-06-16 15:31:47 -07001217 if (spread_flag_changed)
1218 update_tasks_flags(cs, &heap);
1219 heap_free(&heap);
Li Zefan645fcc92009-01-07 18:08:43 -08001220out:
1221 free_trial_cpuset(trialcs);
1222 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Paul Jackson053199e2005-10-30 15:02:30 -08001225/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001226 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001227 *
1228 * These routines manage a digitally filtered, constant time based,
1229 * event frequency meter. There are four routines:
1230 * fmeter_init() - initialize a frequency meter.
1231 * fmeter_markevent() - called each time the event happens.
1232 * fmeter_getrate() - returns the recent rate of such events.
1233 * fmeter_update() - internal routine used to update fmeter.
1234 *
1235 * A common data structure is passed to each of these routines,
1236 * which is used to keep track of the state required to manage the
1237 * frequency meter and its digital filter.
1238 *
1239 * The filter works on the number of events marked per unit time.
1240 * The filter is single-pole low-pass recursive (IIR). The time unit
1241 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1242 * simulate 3 decimal digits of precision (multiplied by 1000).
1243 *
1244 * With an FM_COEF of 933, and a time base of 1 second, the filter
1245 * has a half-life of 10 seconds, meaning that if the events quit
1246 * happening, then the rate returned from the fmeter_getrate()
1247 * will be cut in half each 10 seconds, until it converges to zero.
1248 *
1249 * It is not worth doing a real infinitely recursive filter. If more
1250 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1251 * just compute FM_MAXTICKS ticks worth, by which point the level
1252 * will be stable.
1253 *
1254 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1255 * arithmetic overflow in the fmeter_update() routine.
1256 *
1257 * Given the simple 32 bit integer arithmetic used, this meter works
1258 * best for reporting rates between one per millisecond (msec) and
1259 * one per 32 (approx) seconds. At constant rates faster than one
1260 * per msec it maxes out at values just under 1,000,000. At constant
1261 * rates between one per msec, and one per second it will stabilize
1262 * to a value N*1000, where N is the rate of events per second.
1263 * At constant rates between one per second and one per 32 seconds,
1264 * it will be choppy, moving up on the seconds that have an event,
1265 * and then decaying until the next event. At rates slower than
1266 * about one in 32 seconds, it decays all the way back to zero between
1267 * each event.
1268 */
1269
1270#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1271#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1272#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1273#define FM_SCALE 1000 /* faux fixed point scale */
1274
1275/* Initialize a frequency meter */
1276static void fmeter_init(struct fmeter *fmp)
1277{
1278 fmp->cnt = 0;
1279 fmp->val = 0;
1280 fmp->time = 0;
1281 spin_lock_init(&fmp->lock);
1282}
1283
1284/* Internal meter update - process cnt events and update value */
1285static void fmeter_update(struct fmeter *fmp)
1286{
1287 time_t now = get_seconds();
1288 time_t ticks = now - fmp->time;
1289
1290 if (ticks == 0)
1291 return;
1292
1293 ticks = min(FM_MAXTICKS, ticks);
1294 while (ticks-- > 0)
1295 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1296 fmp->time = now;
1297
1298 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1299 fmp->cnt = 0;
1300}
1301
1302/* Process any previous ticks, then bump cnt by one (times scale). */
1303static void fmeter_markevent(struct fmeter *fmp)
1304{
1305 spin_lock(&fmp->lock);
1306 fmeter_update(fmp);
1307 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1308 spin_unlock(&fmp->lock);
1309}
1310
1311/* Process any previous ticks, then return current value. */
1312static int fmeter_getrate(struct fmeter *fmp)
1313{
1314 int val;
1315
1316 spin_lock(&fmp->lock);
1317 fmeter_update(fmp);
1318 val = fmp->val;
1319 spin_unlock(&fmp->lock);
1320 return val;
1321}
1322
Li Zefan2341d1b2009-01-07 18:08:42 -08001323/* Protected by cgroup_lock */
1324static cpumask_var_t cpus_attach;
1325
Paul Menage2df167a2008-02-07 00:14:45 -08001326/* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
Ben Blumbe367d02009-09-23 15:56:31 -07001327static int cpuset_can_attach(struct cgroup_subsys *ss, struct cgroup *cont,
1328 struct task_struct *tsk, bool threadgroup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Ben Blumbe367d02009-09-23 15:56:31 -07001330 int ret;
Paul Menage8793d852007-10-18 23:39:39 -07001331 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Li Zefan300ed6c2009-01-07 18:08:44 -08001333 if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -ENOSPC;
David Rientjes9985b0b2008-06-05 12:57:11 -07001335
David Rientjes6d7b2f52009-04-02 16:57:57 -07001336 /*
1337 * Kthreads bound to specific cpus cannot be moved to a new cpuset; we
1338 * cannot change their cpu affinity and isolating such threads by their
1339 * set of allowed nodes is unnecessary. Thus, cpusets are not
1340 * applicable for such threads. This prevents checking for success of
1341 * set_cpus_allowed_ptr() on all attached tasks before cpus_allowed may
1342 * be changed.
1343 */
1344 if (tsk->flags & PF_THREAD_BOUND)
1345 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Ben Blumbe367d02009-09-23 15:56:31 -07001347 ret = security_task_setscheduler(tsk, 0, NULL);
1348 if (ret)
1349 return ret;
1350 if (threadgroup) {
1351 struct task_struct *c;
1352
1353 rcu_read_lock();
1354 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
1355 ret = security_task_setscheduler(c, 0, NULL);
1356 if (ret) {
1357 rcu_read_unlock();
1358 return ret;
1359 }
1360 }
1361 rcu_read_unlock();
1362 }
1363 return 0;
Paul Menage8793d852007-10-18 23:39:39 -07001364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Ben Blumbe367d02009-09-23 15:56:31 -07001366static void cpuset_attach_task(struct task_struct *tsk, nodemask_t *to,
1367 struct cpuset *cs)
1368{
1369 int err;
1370 /*
1371 * can_attach beforehand should guarantee that this doesn't fail.
1372 * TODO: have a better way to handle failure here
1373 */
1374 err = set_cpus_allowed_ptr(tsk, cpus_attach);
1375 WARN_ON_ONCE(err);
1376
1377 task_lock(tsk);
1378 cpuset_change_task_nodemask(tsk, to);
1379 task_unlock(tsk);
1380 cpuset_update_task_spread_flag(cs, tsk);
1381
1382}
1383
1384static void cpuset_attach(struct cgroup_subsys *ss, struct cgroup *cont,
1385 struct cgroup *oldcont, struct task_struct *tsk,
1386 bool threadgroup)
Paul Menage8793d852007-10-18 23:39:39 -07001387{
Paul Menage8793d852007-10-18 23:39:39 -07001388 nodemask_t from, to;
1389 struct mm_struct *mm;
1390 struct cpuset *cs = cgroup_cs(cont);
1391 struct cpuset *oldcs = cgroup_cs(oldcont);
David Quigley22fb52d2006-06-23 02:04:00 -07001392
Miao Xief5813d92009-01-07 18:08:40 -08001393 if (cs == &top_cpuset) {
Li Zefan2341d1b2009-01-07 18:08:42 -08001394 cpumask_copy(cpus_attach, cpu_possible_mask);
Miao Xie58568d22009-06-16 15:31:49 -07001395 to = node_possible_map;
Miao Xief5813d92009-01-07 18:08:40 -08001396 } else {
Li Zefan2341d1b2009-01-07 18:08:42 -08001397 guarantee_online_cpus(cs, cpus_attach);
Miao Xie58568d22009-06-16 15:31:49 -07001398 guarantee_online_mems(cs, &to);
Miao Xief5813d92009-01-07 18:08:40 -08001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Ben Blumbe367d02009-09-23 15:56:31 -07001401 /* do per-task migration stuff possibly for each in the threadgroup */
1402 cpuset_attach_task(tsk, &to, cs);
1403 if (threadgroup) {
1404 struct task_struct *c;
1405 rcu_read_lock();
1406 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
1407 cpuset_attach_task(c, &to, cs);
1408 }
1409 rcu_read_unlock();
1410 }
Miao Xie950592f2009-06-16 15:31:47 -07001411
Ben Blumbe367d02009-09-23 15:56:31 -07001412 /* change mm; only needs to be done once even if threadgroup */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001413 from = oldcs->mems_allowed;
1414 to = cs->mems_allowed;
Paul Jackson42253992006-01-08 01:01:59 -08001415 mm = get_task_mm(tsk);
1416 if (mm) {
1417 mpol_rebind_mm(mm, &to);
Paul Jackson2741a552006-03-31 02:30:51 -08001418 if (is_memory_migrate(cs))
Paul Jacksone4e364e2006-03-31 02:30:52 -08001419 cpuset_migrate_mm(mm, &from, &to);
Paul Jackson42253992006-01-08 01:01:59 -08001420 mmput(mm);
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424/* The various types of files and directories in a cpuset file system */
1425
1426typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001427 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 FILE_CPULIST,
1429 FILE_MEMLIST,
1430 FILE_CPU_EXCLUSIVE,
1431 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001432 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001433 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001434 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001435 FILE_MEMORY_PRESSURE_ENABLED,
1436 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001437 FILE_SPREAD_PAGE,
1438 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439} cpuset_filetype_t;
1440
Paul Menage700fe1a2008-04-29 01:00:00 -07001441static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1442{
1443 int retval = 0;
1444 struct cpuset *cs = cgroup_cs(cgrp);
1445 cpuset_filetype_t type = cft->private;
1446
Paul Menagee3712392008-07-25 01:47:02 -07001447 if (!cgroup_lock_live_group(cgrp))
Paul Menage700fe1a2008-04-29 01:00:00 -07001448 return -ENODEV;
Paul Menage700fe1a2008-04-29 01:00:00 -07001449
1450 switch (type) {
1451 case FILE_CPU_EXCLUSIVE:
1452 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1453 break;
1454 case FILE_MEM_EXCLUSIVE:
1455 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1456 break;
Paul Menage78608362008-04-29 01:00:26 -07001457 case FILE_MEM_HARDWALL:
1458 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1459 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001460 case FILE_SCHED_LOAD_BALANCE:
1461 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1462 break;
1463 case FILE_MEMORY_MIGRATE:
1464 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1465 break;
1466 case FILE_MEMORY_PRESSURE_ENABLED:
1467 cpuset_memory_pressure_enabled = !!val;
1468 break;
1469 case FILE_MEMORY_PRESSURE:
1470 retval = -EACCES;
1471 break;
1472 case FILE_SPREAD_PAGE:
1473 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001474 break;
1475 case FILE_SPREAD_SLAB:
1476 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001477 break;
1478 default:
1479 retval = -EINVAL;
1480 break;
1481 }
1482 cgroup_unlock();
1483 return retval;
1484}
1485
Paul Menage5be7a472008-05-06 20:42:41 -07001486static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
1487{
1488 int retval = 0;
1489 struct cpuset *cs = cgroup_cs(cgrp);
1490 cpuset_filetype_t type = cft->private;
1491
Paul Menagee3712392008-07-25 01:47:02 -07001492 if (!cgroup_lock_live_group(cgrp))
Paul Menage5be7a472008-05-06 20:42:41 -07001493 return -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001494
Paul Menage5be7a472008-05-06 20:42:41 -07001495 switch (type) {
1496 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1497 retval = update_relax_domain_level(cs, val);
1498 break;
1499 default:
1500 retval = -EINVAL;
1501 break;
1502 }
1503 cgroup_unlock();
1504 return retval;
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
Paul Menagee3712392008-07-25 01:47:02 -07001508 * Common handling for a write to a "cpus" or "mems" file.
1509 */
1510static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
1511 const char *buf)
1512{
1513 int retval = 0;
Li Zefan645fcc92009-01-07 18:08:43 -08001514 struct cpuset *cs = cgroup_cs(cgrp);
1515 struct cpuset *trialcs;
Paul Menagee3712392008-07-25 01:47:02 -07001516
1517 if (!cgroup_lock_live_group(cgrp))
1518 return -ENODEV;
1519
Li Zefan645fcc92009-01-07 18:08:43 -08001520 trialcs = alloc_trial_cpuset(cs);
1521 if (!trialcs)
1522 return -ENOMEM;
1523
Paul Menagee3712392008-07-25 01:47:02 -07001524 switch (cft->private) {
1525 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001526 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001527 break;
1528 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001529 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001530 break;
1531 default:
1532 retval = -EINVAL;
1533 break;
1534 }
Li Zefan645fcc92009-01-07 18:08:43 -08001535
1536 free_trial_cpuset(trialcs);
Paul Menagee3712392008-07-25 01:47:02 -07001537 cgroup_unlock();
1538 return retval;
1539}
1540
1541/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * These ascii lists should be read in a single call, by using a user
1543 * buffer large enough to hold the entire map. If read in smaller
1544 * chunks, there is no guarantee of atomicity. Since the display format
1545 * used, list of ranges of sequential numbers, is variable length,
1546 * and since these maps can change value dynamically, one could read
1547 * gibberish by doing partial reads while a list was changing.
1548 * A single large read to a buffer that crosses a page boundary is
1549 * ok, because the result being copied to user land is not recomputed
1550 * across a page fault.
1551 */
1552
1553static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
1554{
Li Zefan5a7625d2009-01-07 18:08:41 -08001555 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001557 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08001558 ret = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001559 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Li Zefan5a7625d2009-01-07 18:08:41 -08001561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
1565{
1566 nodemask_t mask;
1567
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001568 mutex_lock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mask = cs->mems_allowed;
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08001570 mutex_unlock(&callback_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 return nodelist_scnprintf(page, PAGE_SIZE, mask);
1573}
1574
Paul Menage8793d852007-10-18 23:39:39 -07001575static ssize_t cpuset_common_file_read(struct cgroup *cont,
1576 struct cftype *cft,
1577 struct file *file,
1578 char __user *buf,
1579 size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Paul Menage8793d852007-10-18 23:39:39 -07001581 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 cpuset_filetype_t type = cft->private;
1583 char *page;
1584 ssize_t retval = 0;
1585 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Mel Gormane12ba742007-10-16 01:25:52 -07001587 if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return -ENOMEM;
1589
1590 s = page;
1591
1592 switch (type) {
1593 case FILE_CPULIST:
1594 s += cpuset_sprintf_cpulist(s, cs);
1595 break;
1596 case FILE_MEMLIST:
1597 s += cpuset_sprintf_memlist(s, cs);
1598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 default:
1600 retval = -EINVAL;
1601 goto out;
1602 }
1603 *s++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Al Viroeacaa1f2005-09-30 03:26:43 +01001605 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606out:
1607 free_page((unsigned long)page);
1608 return retval;
1609}
1610
Paul Menage700fe1a2008-04-29 01:00:00 -07001611static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
1612{
1613 struct cpuset *cs = cgroup_cs(cont);
1614 cpuset_filetype_t type = cft->private;
1615 switch (type) {
1616 case FILE_CPU_EXCLUSIVE:
1617 return is_cpu_exclusive(cs);
1618 case FILE_MEM_EXCLUSIVE:
1619 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001620 case FILE_MEM_HARDWALL:
1621 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001622 case FILE_SCHED_LOAD_BALANCE:
1623 return is_sched_load_balance(cs);
1624 case FILE_MEMORY_MIGRATE:
1625 return is_memory_migrate(cs);
1626 case FILE_MEMORY_PRESSURE_ENABLED:
1627 return cpuset_memory_pressure_enabled;
1628 case FILE_MEMORY_PRESSURE:
1629 return fmeter_getrate(&cs->fmeter);
1630 case FILE_SPREAD_PAGE:
1631 return is_spread_page(cs);
1632 case FILE_SPREAD_SLAB:
1633 return is_spread_slab(cs);
1634 default:
1635 BUG();
1636 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001637
1638 /* Unreachable but makes gcc happy */
1639 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001640}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Paul Menage5be7a472008-05-06 20:42:41 -07001642static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
1643{
1644 struct cpuset *cs = cgroup_cs(cont);
1645 cpuset_filetype_t type = cft->private;
1646 switch (type) {
1647 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1648 return cs->relax_domain_level;
1649 default:
1650 BUG();
1651 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001652
1653 /* Unrechable but makes gcc happy */
1654 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001655}
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658/*
1659 * for the common functions, 'private' gives the type of file
1660 */
1661
Paul Menageaddf2c72008-04-29 01:00:26 -07001662static struct cftype files[] = {
1663 {
1664 .name = "cpus",
1665 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001666 .write_string = cpuset_write_resmask,
1667 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001668 .private = FILE_CPULIST,
1669 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Paul Menageaddf2c72008-04-29 01:00:26 -07001671 {
1672 .name = "mems",
1673 .read = cpuset_common_file_read,
Paul Menagee3712392008-07-25 01:47:02 -07001674 .write_string = cpuset_write_resmask,
1675 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001676 .private = FILE_MEMLIST,
1677 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Paul Menageaddf2c72008-04-29 01:00:26 -07001679 {
1680 .name = "cpu_exclusive",
1681 .read_u64 = cpuset_read_u64,
1682 .write_u64 = cpuset_write_u64,
1683 .private = FILE_CPU_EXCLUSIVE,
1684 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Paul Menageaddf2c72008-04-29 01:00:26 -07001686 {
1687 .name = "mem_exclusive",
1688 .read_u64 = cpuset_read_u64,
1689 .write_u64 = cpuset_write_u64,
1690 .private = FILE_MEM_EXCLUSIVE,
1691 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Paul Menageaddf2c72008-04-29 01:00:26 -07001693 {
Paul Menage78608362008-04-29 01:00:26 -07001694 .name = "mem_hardwall",
1695 .read_u64 = cpuset_read_u64,
1696 .write_u64 = cpuset_write_u64,
1697 .private = FILE_MEM_HARDWALL,
1698 },
1699
1700 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001701 .name = "sched_load_balance",
1702 .read_u64 = cpuset_read_u64,
1703 .write_u64 = cpuset_write_u64,
1704 .private = FILE_SCHED_LOAD_BALANCE,
1705 },
Paul Jackson029190c2007-10-18 23:40:20 -07001706
Paul Menageaddf2c72008-04-29 01:00:26 -07001707 {
1708 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001709 .read_s64 = cpuset_read_s64,
1710 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001711 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1712 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001713
Paul Menageaddf2c72008-04-29 01:00:26 -07001714 {
1715 .name = "memory_migrate",
1716 .read_u64 = cpuset_read_u64,
1717 .write_u64 = cpuset_write_u64,
1718 .private = FILE_MEMORY_MIGRATE,
1719 },
1720
1721 {
1722 .name = "memory_pressure",
1723 .read_u64 = cpuset_read_u64,
1724 .write_u64 = cpuset_write_u64,
1725 .private = FILE_MEMORY_PRESSURE,
Li Zefan099fca32009-04-02 16:57:29 -07001726 .mode = S_IRUGO,
Paul Menageaddf2c72008-04-29 01:00:26 -07001727 },
1728
1729 {
1730 .name = "memory_spread_page",
1731 .read_u64 = cpuset_read_u64,
1732 .write_u64 = cpuset_write_u64,
1733 .private = FILE_SPREAD_PAGE,
1734 },
1735
1736 {
1737 .name = "memory_spread_slab",
1738 .read_u64 = cpuset_read_u64,
1739 .write_u64 = cpuset_write_u64,
1740 .private = FILE_SPREAD_SLAB,
1741 },
Paul Jackson45b07ef2006-01-08 01:00:56 -08001742};
1743
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001744static struct cftype cft_memory_pressure_enabled = {
1745 .name = "memory_pressure_enabled",
Paul Menage700fe1a2008-04-29 01:00:00 -07001746 .read_u64 = cpuset_read_u64,
1747 .write_u64 = cpuset_write_u64,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001748 .private = FILE_MEMORY_PRESSURE_ENABLED,
1749};
1750
Paul Menage8793d852007-10-18 23:39:39 -07001751static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 int err;
1754
Paul Menageaddf2c72008-04-29 01:00:26 -07001755 err = cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
1756 if (err)
Paul Jackson825a46a2006-03-24 03:16:03 -08001757 return err;
Paul Menage8793d852007-10-18 23:39:39 -07001758 /* memory_pressure_enabled is in root cpuset only */
Paul Menageaddf2c72008-04-29 01:00:26 -07001759 if (!cont->parent)
Paul Menage8793d852007-10-18 23:39:39 -07001760 err = cgroup_add_file(cont, ss,
Paul Menageaddf2c72008-04-29 01:00:26 -07001761 &cft_memory_pressure_enabled);
1762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/*
Paul Menage8793d852007-10-18 23:39:39 -07001766 * post_clone() is called at the end of cgroup_clone().
1767 * 'cgroup' was just created automatically as a result of
1768 * a cgroup_clone(), and the current task is about to
1769 * be moved into 'cgroup'.
1770 *
1771 * Currently we refuse to set up the cgroup - thereby
1772 * refusing the task to be entered, and as a result refusing
1773 * the sys_unshare() or clone() which initiated it - if any
1774 * sibling cpusets have exclusive cpus or mem.
1775 *
1776 * If this becomes a problem for some users who wish to
1777 * allow that scenario, then cpuset_post_clone() could be
1778 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
Paul Menage2df167a2008-02-07 00:14:45 -08001779 * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
1780 * held.
Paul Menage8793d852007-10-18 23:39:39 -07001781 */
1782static void cpuset_post_clone(struct cgroup_subsys *ss,
1783 struct cgroup *cgroup)
1784{
1785 struct cgroup *parent, *child;
1786 struct cpuset *cs, *parent_cs;
1787
1788 parent = cgroup->parent;
1789 list_for_each_entry(child, &parent->children, sibling) {
1790 cs = cgroup_cs(child);
1791 if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
1792 return;
1793 }
1794 cs = cgroup_cs(cgroup);
1795 parent_cs = cgroup_cs(parent);
1796
1797 cs->mems_allowed = parent_cs->mems_allowed;
Li Zefan300ed6c2009-01-07 18:08:44 -08001798 cpumask_copy(cs->cpus_allowed, parent_cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001799 return;
1800}
1801
1802/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 * cpuset_create - create a cpuset
Paul Menage2df167a2008-02-07 00:14:45 -08001804 * ss: cpuset cgroup subsystem
1805 * cont: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
1807
Paul Menage8793d852007-10-18 23:39:39 -07001808static struct cgroup_subsys_state *cpuset_create(
1809 struct cgroup_subsys *ss,
1810 struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct cpuset *cs;
Paul Menage8793d852007-10-18 23:39:39 -07001813 struct cpuset *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Paul Menage8793d852007-10-18 23:39:39 -07001815 if (!cont->parent) {
Paul Menage8793d852007-10-18 23:39:39 -07001816 return &top_cpuset.css;
1817 }
1818 parent = cgroup_cs(cont->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
1820 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001821 return ERR_PTR(-ENOMEM);
Li Zefan300ed6c2009-01-07 18:08:44 -08001822 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
1823 kfree(cs);
1824 return ERR_PTR(-ENOMEM);
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 cs->flags = 0;
Paul Jackson825a46a2006-03-24 03:16:03 -08001828 if (is_spread_page(parent))
1829 set_bit(CS_SPREAD_PAGE, &cs->flags);
1830 if (is_spread_slab(parent))
1831 set_bit(CS_SPREAD_SLAB, &cs->flags);
Paul Jackson029190c2007-10-18 23:40:20 -07001832 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001833 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001834 nodes_clear(cs->mems_allowed);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001835 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001836 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 cs->parent = parent;
Paul Jackson202f72d2006-01-08 01:01:57 -08001839 number_of_cpusets++;
Paul Menage8793d852007-10-18 23:39:39 -07001840 return &cs->css ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Paul Jackson029190c2007-10-18 23:40:20 -07001843/*
Paul Jackson029190c2007-10-18 23:40:20 -07001844 * If the cpuset being removed has its flag 'sched_load_balance'
1845 * enabled, then simulate turning sched_load_balance off, which
Max Krasnyanskycf417142008-08-11 14:33:53 -07001846 * will call async_rebuild_sched_domains().
Paul Jackson029190c2007-10-18 23:40:20 -07001847 */
1848
Paul Menage8793d852007-10-18 23:39:39 -07001849static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Paul Menage8793d852007-10-18 23:39:39 -07001851 struct cpuset *cs = cgroup_cs(cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Paul Jackson029190c2007-10-18 23:40:20 -07001853 if (is_sched_load_balance(cs))
Paul Menage700fe1a2008-04-29 01:00:00 -07001854 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
Paul Jackson029190c2007-10-18 23:40:20 -07001855
Paul Jackson202f72d2006-01-08 01:01:57 -08001856 number_of_cpusets--;
Li Zefan300ed6c2009-01-07 18:08:44 -08001857 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07001858 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
Paul Menage8793d852007-10-18 23:39:39 -07001861struct cgroup_subsys cpuset_subsys = {
1862 .name = "cpuset",
1863 .create = cpuset_create,
Max Krasnyanskycf417142008-08-11 14:33:53 -07001864 .destroy = cpuset_destroy,
Paul Menage8793d852007-10-18 23:39:39 -07001865 .can_attach = cpuset_can_attach,
1866 .attach = cpuset_attach,
1867 .populate = cpuset_populate,
1868 .post_clone = cpuset_post_clone,
1869 .subsys_id = cpuset_subsys_id,
1870 .early_init = 1,
1871};
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/**
1874 * cpuset_init - initialize cpusets at system boot
1875 *
1876 * Description: Initialize top_cpuset and the cpuset internal file system,
1877 **/
1878
1879int __init cpuset_init(void)
1880{
Paul Menage8793d852007-10-18 23:39:39 -07001881 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Miao Xie58568d22009-06-16 15:31:49 -07001883 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
1884 BUG();
1885
Li Zefan300ed6c2009-01-07 18:08:44 -08001886 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001887 nodes_setall(top_cpuset.mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001889 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07001890 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001891 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 err = register_filesystem(&cpuset_fs_type);
1894 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07001895 return err;
1896
Li Zefan2341d1b2009-01-07 18:08:42 -08001897 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
1898 BUG();
1899
Paul Jackson202f72d2006-01-08 01:01:57 -08001900 number_of_cpusets = 1;
Paul Menage8793d852007-10-18 23:39:39 -07001901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Cliff Wickman956db3c2008-02-07 00:14:43 -08001904/**
1905 * cpuset_do_move_task - move a given task to another cpuset
1906 * @tsk: pointer to task_struct the task to move
1907 * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
1908 *
1909 * Called by cgroup_scan_tasks() for each task in a cgroup.
1910 * Return nonzero to stop the walk through the tasks.
1911 */
Adrian Bunk9e0c9142008-04-29 01:00:25 -07001912static void cpuset_do_move_task(struct task_struct *tsk,
1913 struct cgroup_scanner *scan)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001914{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001915 struct cgroup *new_cgroup = scan->data;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001916
Li Zefan7f81b1a2009-04-02 16:57:53 -07001917 cgroup_attach_task(new_cgroup, tsk);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001918}
1919
1920/**
1921 * move_member_tasks_to_cpuset - move tasks from one cpuset to another
1922 * @from: cpuset in which the tasks currently reside
1923 * @to: cpuset to which the tasks will be moved
1924 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001925 * Called with cgroup_mutex held
1926 * callback_mutex must not be held, as cpuset_attach() will take it.
Cliff Wickman956db3c2008-02-07 00:14:43 -08001927 *
1928 * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
1929 * calling callback functions for each.
1930 */
1931static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
1932{
Li Zefan7f81b1a2009-04-02 16:57:53 -07001933 struct cgroup_scanner scan;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001934
Li Zefan7f81b1a2009-04-02 16:57:53 -07001935 scan.cg = from->css.cgroup;
1936 scan.test_task = NULL; /* select all tasks in cgroup */
1937 scan.process_task = cpuset_do_move_task;
1938 scan.heap = NULL;
1939 scan.data = to->css.cgroup;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001940
Li Zefan7f81b1a2009-04-02 16:57:53 -07001941 if (cgroup_scan_tasks(&scan))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001942 printk(KERN_ERR "move_member_tasks_to_cpuset: "
1943 "cgroup_scan_tasks failed\n");
1944}
1945
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001946/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07001947 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001948 * or memory nodes, we need to walk over the cpuset hierarchy,
1949 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08001950 * last CPU or node from a cpuset, then move the tasks in the empty
1951 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001952 *
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001953 * Called with cgroup_mutex held
1954 * callback_mutex must not be held, as cpuset_attach() will take it.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001955 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001956static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001957{
Cliff Wickman956db3c2008-02-07 00:14:43 -08001958 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07001959
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001960 /*
1961 * The cgroup's css_sets list is in use if there are tasks
1962 * in the cpuset; the list is empty if there are none;
1963 * the cs->css.refcnt seems always 0.
1964 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001965 if (list_empty(&cs->css.cgroup->css_sets))
1966 return;
1967
1968 /*
1969 * Find its next-highest non-empty parent, (top cpuset
1970 * has online cpus, so can't be empty).
1971 */
1972 parent = cs->parent;
Li Zefan300ed6c2009-01-07 18:08:44 -08001973 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08001974 nodes_empty(parent->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08001975 parent = parent->parent;
Cliff Wickman956db3c2008-02-07 00:14:43 -08001976
1977 move_member_tasks_to_cpuset(cs, parent);
1978}
1979
1980/*
1981 * Walk the specified cpuset subtree and look for empty cpusets.
1982 * The tasks of such cpuset must be moved to a parent cpuset.
1983 *
Paul Menage2df167a2008-02-07 00:14:45 -08001984 * Called with cgroup_mutex held. We take callback_mutex to modify
Cliff Wickman956db3c2008-02-07 00:14:43 -08001985 * cpus_allowed and mems_allowed.
1986 *
1987 * This walk processes the tree from top to bottom, completing one layer
1988 * before dropping down to the next. It always processes a node before
1989 * any of its children.
1990 *
1991 * For now, since we lack memory hot unplug, we'll never see a cpuset
1992 * that has tasks along with an empty 'mems'. But if we did see such
1993 * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
1994 */
Frederic Weisbeckerd294eb82008-10-03 12:10:10 +02001995static void scan_for_empty_cpusets(struct cpuset *root)
Cliff Wickman956db3c2008-02-07 00:14:43 -08001996{
Li Zefan8d1e6262008-07-29 22:33:21 -07001997 LIST_HEAD(queue);
Cliff Wickman956db3c2008-02-07 00:14:43 -08001998 struct cpuset *cp; /* scans cpusets being updated */
1999 struct cpuset *child; /* scans child cpusets of cp */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002000 struct cgroup *cont;
Miao Xief9b4fb82008-07-25 01:47:22 -07002001 nodemask_t oldmems;
Cliff Wickman956db3c2008-02-07 00:14:43 -08002002
Cliff Wickman956db3c2008-02-07 00:14:43 -08002003 list_add_tail((struct list_head *)&root->stack_list, &queue);
2004
Cliff Wickman956db3c2008-02-07 00:14:43 -08002005 while (!list_empty(&queue)) {
Li Zefan8d1e6262008-07-29 22:33:21 -07002006 cp = list_first_entry(&queue, struct cpuset, stack_list);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002007 list_del(queue.next);
2008 list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
2009 child = cgroup_cs(cont);
2010 list_add_tail(&child->stack_list, &queue);
2011 }
Paul Jacksonb4501292008-02-07 00:14:47 -08002012
2013 /* Continue past cpusets with all cpus, mems online */
Li Zefan300ed6c2009-01-07 18:08:44 -08002014 if (cpumask_subset(cp->cpus_allowed, cpu_online_mask) &&
Paul Jacksonb4501292008-02-07 00:14:47 -08002015 nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
2016 continue;
2017
Miao Xief9b4fb82008-07-25 01:47:22 -07002018 oldmems = cp->mems_allowed;
2019
Cliff Wickman956db3c2008-02-07 00:14:43 -08002020 /* Remove offline cpus and mems from this cpuset. */
Paul Jacksonb4501292008-02-07 00:14:47 -08002021 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08002022 cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
2023 cpu_online_mask);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002024 nodes_and(cp->mems_allowed, cp->mems_allowed,
2025 node_states[N_HIGH_MEMORY]);
Paul Jacksonb4501292008-02-07 00:14:47 -08002026 mutex_unlock(&callback_mutex);
2027
2028 /* Move tasks from the empty cpuset to a parent */
Li Zefan300ed6c2009-01-07 18:08:44 -08002029 if (cpumask_empty(cp->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002030 nodes_empty(cp->mems_allowed))
Cliff Wickman956db3c2008-02-07 00:14:43 -08002031 remove_tasks_in_empty_cpuset(cp);
Miao Xief9b4fb82008-07-25 01:47:22 -07002032 else {
Li Zefan4e743392008-09-13 02:33:08 -07002033 update_tasks_cpumask(cp, NULL);
Li Zefan010cfac2009-04-02 16:57:52 -07002034 update_tasks_nodemask(cp, &oldmems, NULL);
Miao Xief9b4fb82008-07-25 01:47:22 -07002035 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002036 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002037}
2038
2039/*
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002040 * The top_cpuset tracks what CPUs and Memory Nodes are online,
2041 * period. This is necessary in order to make cpusets transparent
2042 * (of no affect) on systems that are actively using CPU hotplug
2043 * but making no active use of cpusets.
2044 *
Paul Jackson38837fc2006-09-29 02:01:16 -07002045 * This routine ensures that top_cpuset.cpus_allowed tracks
2046 * cpu_online_map on each CPU hotplug (cpuhp) event.
Max Krasnyanskycf417142008-08-11 14:33:53 -07002047 *
2048 * Called within get_online_cpus(). Needs to call cgroup_lock()
2049 * before calling generate_sched_domains().
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002050 */
Max Krasnyanskycf417142008-08-11 14:33:53 -07002051static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
Paul Jackson029190c2007-10-18 23:40:20 -07002052 unsigned long phase, void *unused_cpu)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002053{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002054 struct sched_domain_attr *attr;
Li Zefan6af866a2009-01-07 18:08:45 -08002055 struct cpumask *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002056 int ndoms;
2057
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002058 switch (phase) {
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002059 case CPU_ONLINE:
2060 case CPU_ONLINE_FROZEN:
2061 case CPU_DEAD:
2062 case CPU_DEAD_FROZEN:
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002063 break;
Max Krasnyanskycf417142008-08-11 14:33:53 -07002064
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002065 default:
Avi Kivityac076752007-05-24 12:33:15 +03002066 return NOTIFY_DONE;
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002067 }
Avi Kivityac076752007-05-24 12:33:15 +03002068
Max Krasnyanskycf417142008-08-11 14:33:53 -07002069 cgroup_lock();
Li Zefan0b4217b2009-04-02 16:57:49 -07002070 mutex_lock(&callback_mutex);
Li Zefan300ed6c2009-01-07 18:08:44 -08002071 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
Li Zefan0b4217b2009-04-02 16:57:49 -07002072 mutex_unlock(&callback_mutex);
Max Krasnyanskycf417142008-08-11 14:33:53 -07002073 scan_for_empty_cpusets(&top_cpuset);
2074 ndoms = generate_sched_domains(&doms, &attr);
2075 cgroup_unlock();
2076
2077 /* Have scheduler rebuild the domains */
2078 partition_sched_domains(ndoms, doms, attr);
2079
Dmitry Adamushko3e840502008-07-13 02:10:29 +02002080 return NOTIFY_OK;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002081}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002082
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002083#ifdef CONFIG_MEMORY_HOTPLUG
Paul Jackson38837fc2006-09-29 02:01:16 -07002084/*
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002085 * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
Max Krasnyanskycf417142008-08-11 14:33:53 -07002086 * Call this routine anytime after node_states[N_HIGH_MEMORY] changes.
2087 * See also the previous routine cpuset_track_online_cpus().
Paul Jackson38837fc2006-09-29 02:01:16 -07002088 */
Miao Xief4818912008-11-19 15:36:30 -08002089static int cpuset_track_online_nodes(struct notifier_block *self,
2090 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002091{
Max Krasnyanskycf417142008-08-11 14:33:53 -07002092 cgroup_lock();
Miao Xief4818912008-11-19 15:36:30 -08002093 switch (action) {
2094 case MEM_ONLINE:
Miao Xief4818912008-11-19 15:36:30 -08002095 case MEM_OFFLINE:
Li Zefan0b4217b2009-04-02 16:57:49 -07002096 mutex_lock(&callback_mutex);
Miao Xief4818912008-11-19 15:36:30 -08002097 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
Li Zefan0b4217b2009-04-02 16:57:49 -07002098 mutex_unlock(&callback_mutex);
2099 if (action == MEM_OFFLINE)
2100 scan_for_empty_cpusets(&top_cpuset);
Miao Xief4818912008-11-19 15:36:30 -08002101 break;
2102 default:
2103 break;
2104 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002105 cgroup_unlock();
Miao Xief4818912008-11-19 15:36:30 -08002106 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002107}
2108#endif
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110/**
2111 * cpuset_init_smp - initialize cpus_allowed
2112 *
2113 * Description: Finish top cpuset after cpu, node maps are initialized
2114 **/
2115
2116void __init cpuset_init_smp(void)
2117{
Li Zefan300ed6c2009-01-07 18:08:44 -08002118 cpumask_copy(top_cpuset.cpus_allowed, cpu_online_mask);
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002119 top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002120
Max Krasnyanskycf417142008-08-11 14:33:53 -07002121 hotcpu_notifier(cpuset_track_online_cpus, 0);
Miao Xief4818912008-11-19 15:36:30 -08002122 hotplug_memory_notifier(cpuset_track_online_nodes, 10);
Miao Xief90d4112009-01-16 10:24:10 +08002123
2124 cpuset_wq = create_singlethread_workqueue("cpuset");
2125 BUG_ON(!cpuset_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2130 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002131 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002133 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 * attached to the specified @tsk. Guaranteed to return some non-empty
2135 * subset of cpu_online_map, even if this means going outside the
2136 * tasks cpuset.
2137 **/
2138
Li Zefan6af866a2009-01-07 18:08:45 -08002139void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002141 mutex_lock(&callback_mutex);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002142 cpuset_cpus_allowed_locked(tsk, pmask);
Cliff Wickman470fd642007-10-18 23:40:46 -07002143 mutex_unlock(&callback_mutex);
Cliff Wickman470fd642007-10-18 23:40:46 -07002144}
2145
2146/**
2147 * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
Paul Menage2df167a2008-02-07 00:14:45 -08002148 * Must be called with callback_mutex held.
Cliff Wickman470fd642007-10-18 23:40:46 -07002149 **/
Li Zefan6af866a2009-01-07 18:08:45 -08002150void cpuset_cpus_allowed_locked(struct task_struct *tsk, struct cpumask *pmask)
Cliff Wickman470fd642007-10-18 23:40:46 -07002151{
Paul Jackson909d75a2006-01-08 01:01:55 -08002152 task_lock(tsk);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002153 guarantee_online_cpus(task_cs(tsk), pmask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002154 task_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
2157void cpuset_init_current_mems_allowed(void)
2158{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002159 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002162/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002163 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2164 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2165 *
2166 * Description: Returns the nodemask_t mems_allowed of the cpuset
2167 * attached to the specified @tsk. Guaranteed to return some non-empty
Christoph Lameter0e1e7c72007-10-16 01:25:38 -07002168 * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002169 * tasks cpuset.
2170 **/
2171
2172nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2173{
2174 nodemask_t mask;
2175
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002176 mutex_lock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002177 task_lock(tsk);
Paul Menage8793d852007-10-18 23:39:39 -07002178 guarantee_online_mems(task_cs(tsk), &mask);
Paul Jackson909d75a2006-01-08 01:01:55 -08002179 task_unlock(tsk);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002180 mutex_unlock(&callback_mutex);
Paul Jackson909d75a2006-01-08 01:01:55 -08002181
2182 return mask;
2183}
2184
2185/**
Mel Gorman19770b32008-04-28 02:12:18 -07002186 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2187 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002188 *
Mel Gorman19770b32008-04-28 02:12:18 -07002189 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 */
Mel Gorman19770b32008-04-28 02:12:18 -07002191int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Mel Gorman19770b32008-04-28 02:12:18 -07002193 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Paul Jackson9bf22292005-09-06 15:18:12 -07002196/*
Paul Menage78608362008-04-29 01:00:26 -07002197 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2198 * mem_hardwall ancestor to the specified cpuset. Call holding
2199 * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
2200 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 */
Paul Menage78608362008-04-29 01:00:26 -07002202static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Paul Menage78608362008-04-29 01:00:26 -07002204 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
Paul Jackson9bf22292005-09-06 15:18:12 -07002205 cs = cs->parent;
2206 return cs;
2207}
2208
2209/**
David Rientjesa1bc5a42009-04-02 16:57:54 -07002210 * cpuset_node_allowed_softwall - Can we allocate on a memory node?
2211 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002212 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002213 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002214 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2215 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2216 * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
2217 * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
2218 * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
2219 * flag, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002220 * Otherwise, no.
2221 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002222 * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
2223 * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
2224 * might sleep, and might allow a node from an enclosing cpuset.
Paul Jackson02a0e532006-12-13 00:34:25 -08002225 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002226 * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
2227 * cpusets, and never sleeps.
Paul Jackson02a0e532006-12-13 00:34:25 -08002228 *
2229 * The __GFP_THISNODE placement logic is really handled elsewhere,
2230 * by forcibly using a zonelist starting at a specified node, and by
2231 * (in get_page_from_freelist()) refusing to consider the zones for
2232 * any node on the zonelist except the first. By the time any such
2233 * calls get to this routine, we should just shut up and say 'yes'.
2234 *
Paul Jackson9bf22292005-09-06 15:18:12 -07002235 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002236 * and do not allow allocations outside the current tasks cpuset
2237 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002238 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002239 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002240 *
Paul Jackson02a0e532006-12-13 00:34:25 -08002241 * Scanning up parent cpusets requires callback_mutex. The
2242 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2243 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2244 * current tasks mems_allowed came up empty on the first pass over
2245 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
2246 * cpuset are short of memory, might require taking the callback_mutex
2247 * mutex.
Paul Jackson9bf22292005-09-06 15:18:12 -07002248 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002249 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002250 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2251 * so no allocation on a node outside the cpuset is allowed (unless
2252 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002253 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002254 * The second pass through get_page_from_freelist() doesn't even call
2255 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2256 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2257 * in alloc_flags. That logic and the checks below have the combined
2258 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002259 * in_interrupt - any node ok (current task context irrelevant)
2260 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002261 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002262 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002263 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson36be57f2006-05-20 15:00:10 -07002264 *
2265 * Rule:
David Rientjesa1bc5a42009-04-02 16:57:54 -07002266 * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
Paul Jackson36be57f2006-05-20 15:00:10 -07002267 * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
2268 * the code that might scan up ancestor cpusets and sleep.
Paul Jackson02a0e532006-12-13 00:34:25 -08002269 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002270int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002271{
Paul Jackson9bf22292005-09-06 15:18:12 -07002272 const struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002273 int allowed; /* is allocation in zone z allowed? */
Paul Jackson9bf22292005-09-06 15:18:12 -07002274
Christoph Lameter9b819d22006-09-25 23:31:40 -07002275 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
Paul Jackson9bf22292005-09-06 15:18:12 -07002276 return 1;
Paul Jackson92d1dbd2006-05-20 15:00:11 -07002277 might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
Paul Jackson9bf22292005-09-06 15:18:12 -07002278 if (node_isset(node, current->mems_allowed))
2279 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002280 /*
2281 * Allow tasks that have access to memory reserves because they have
2282 * been OOM killed to get memory anywhere.
2283 */
2284 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2285 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002286 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2287 return 0;
2288
Bob Picco5563e772005-11-13 16:06:35 -08002289 if (current->flags & PF_EXITING) /* Let dying task have memory */
2290 return 1;
2291
Paul Jackson9bf22292005-09-06 15:18:12 -07002292 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002293 mutex_lock(&callback_mutex);
Paul Jackson053199e2005-10-30 15:02:30 -08002294
Paul Jackson053199e2005-10-30 15:02:30 -08002295 task_lock(current);
Paul Menage78608362008-04-29 01:00:26 -07002296 cs = nearest_hardwall_ancestor(task_cs(current));
Paul Jackson053199e2005-10-30 15:02:30 -08002297 task_unlock(current);
2298
Paul Jackson9bf22292005-09-06 15:18:12 -07002299 allowed = node_isset(node, cs->mems_allowed);
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002300 mutex_unlock(&callback_mutex);
Paul Jackson9bf22292005-09-06 15:18:12 -07002301 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Paul Jackson02a0e532006-12-13 00:34:25 -08002304/*
David Rientjesa1bc5a42009-04-02 16:57:54 -07002305 * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
2306 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002307 * @gfp_mask: memory allocation flags
2308 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002309 * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
2310 * set, yes, we can always allocate. If node is in our task's mems_allowed,
2311 * yes. If the task has been OOM killed and has access to memory reserves as
2312 * specified by the TIF_MEMDIE flag, yes.
2313 * Otherwise, no.
Paul Jackson02a0e532006-12-13 00:34:25 -08002314 *
2315 * The __GFP_THISNODE placement logic is really handled elsewhere,
2316 * by forcibly using a zonelist starting at a specified node, and by
2317 * (in get_page_from_freelist()) refusing to consider the zones for
2318 * any node on the zonelist except the first. By the time any such
2319 * calls get to this routine, we should just shut up and say 'yes'.
2320 *
David Rientjesa1bc5a42009-04-02 16:57:54 -07002321 * Unlike the cpuset_node_allowed_softwall() variant, above,
2322 * this variant requires that the node be in the current task's
Paul Jackson02a0e532006-12-13 00:34:25 -08002323 * mems_allowed or that we're in interrupt. It does not scan up the
2324 * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
2325 * It never sleeps.
2326 */
David Rientjesa1bc5a42009-04-02 16:57:54 -07002327int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -08002328{
Paul Jackson02a0e532006-12-13 00:34:25 -08002329 if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
2330 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002331 if (node_isset(node, current->mems_allowed))
2332 return 1;
Daniel Walkerdedf8b72007-10-18 03:06:04 -07002333 /*
2334 * Allow tasks that have access to memory reserves because they have
2335 * been OOM killed to get memory anywhere.
2336 */
2337 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2338 return 1;
Paul Jackson02a0e532006-12-13 00:34:25 -08002339 return 0;
2340}
2341
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002342/**
Paul Jackson505970b2006-01-14 13:21:06 -08002343 * cpuset_lock - lock out any changes to cpuset structures
2344 *
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002345 * The out of memory (oom) code needs to mutex_lock cpusets
Paul Jackson505970b2006-01-14 13:21:06 -08002346 * from being changed while it scans the tasklist looking for a
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002347 * task in an overlapping cpuset. Expose callback_mutex via this
Paul Jackson505970b2006-01-14 13:21:06 -08002348 * cpuset_lock() routine, so the oom code can lock it, before
2349 * locking the task list. The tasklist_lock is a spinlock, so
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002350 * must be taken inside callback_mutex.
Paul Jackson505970b2006-01-14 13:21:06 -08002351 */
2352
2353void cpuset_lock(void)
2354{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002355 mutex_lock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002356}
2357
2358/**
2359 * cpuset_unlock - release lock on cpuset changes
2360 *
2361 * Undo the lock taken in a previous cpuset_lock() call.
2362 */
2363
2364void cpuset_unlock(void)
2365{
Ingo Molnar3d3f26a2006-03-23 03:00:18 -08002366 mutex_unlock(&callback_mutex);
Paul Jackson505970b2006-01-14 13:21:06 -08002367}
2368
2369/**
Paul Jackson825a46a2006-03-24 03:16:03 -08002370 * cpuset_mem_spread_node() - On which node to begin search for a page
2371 *
2372 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2373 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2374 * and if the memory allocation used cpuset_mem_spread_node()
2375 * to determine on which node to start looking, as it will for
2376 * certain page cache or slab cache pages such as used for file
2377 * system buffers and inode caches, then instead of starting on the
2378 * local node to look for a free page, rather spread the starting
2379 * node around the tasks mems_allowed nodes.
2380 *
2381 * We don't have to worry about the returned node being offline
2382 * because "it can't happen", and even if it did, it would be ok.
2383 *
2384 * The routines calling guarantee_online_mems() are careful to
2385 * only set nodes in task->mems_allowed that are online. So it
2386 * should not be possible for the following code to return an
2387 * offline node. But if it did, that would be ok, as this routine
2388 * is not returning the node where the allocation must be, only
2389 * the node where the search should start. The zonelist passed to
2390 * __alloc_pages() will include all nodes. If the slab allocator
2391 * is passed an offline node, it will fall back to the local node.
2392 * See kmem_cache_alloc_node().
2393 */
2394
2395int cpuset_mem_spread_node(void)
2396{
2397 int node;
2398
2399 node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
2400 if (node == MAX_NUMNODES)
2401 node = first_node(current->mems_allowed);
2402 current->cpuset_mem_spread_rotor = node;
2403 return node;
2404}
2405EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2406
2407/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002408 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2409 * @tsk1: pointer to task_struct of some task.
2410 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002411 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002412 * Description: Return true if @tsk1's mems_allowed intersects the
2413 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2414 * one of the task's memory usage might impact the memory available
2415 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002416 **/
2417
David Rientjesbbe373f2007-10-16 23:25:58 -07002418int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2419 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002420{
David Rientjesbbe373f2007-10-16 23:25:58 -07002421 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002422}
2423
David Rientjes75aa1992009-01-06 14:39:01 -08002424/**
2425 * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
2426 * @task: pointer to task_struct of some task.
2427 *
2428 * Description: Prints @task's name, cpuset name, and cached copy of its
2429 * mems_allowed to the kernel log. Must hold task_lock(task) to allow
2430 * dereferencing task_cs(task).
2431 */
2432void cpuset_print_task_mems_allowed(struct task_struct *tsk)
2433{
2434 struct dentry *dentry;
2435
2436 dentry = task_cs(tsk)->css.cgroup->dentry;
2437 spin_lock(&cpuset_buffer_lock);
2438 snprintf(cpuset_name, CPUSET_NAME_LEN,
2439 dentry ? (const char *)dentry->d_name.name : "/");
2440 nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
2441 tsk->mems_allowed);
2442 printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
2443 tsk->comm, cpuset_name, cpuset_nodelist);
2444 spin_unlock(&cpuset_buffer_lock);
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002448 * Collection of memory_pressure is suppressed unless
2449 * this flag is enabled by writing "1" to the special
2450 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2451 */
2452
Paul Jacksonc5b2aff82006-01-08 01:01:51 -08002453int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002454
2455/**
2456 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2457 *
2458 * Keep a running average of the rate of synchronous (direct)
2459 * page reclaim efforts initiated by tasks in each cpuset.
2460 *
2461 * This represents the rate at which some task in the cpuset
2462 * ran low on memory on all nodes it was allowed to use, and
2463 * had to enter the kernels page reclaim code in an effort to
2464 * create more free memory by tossing clean pages or swapping
2465 * or writing dirty pages.
2466 *
2467 * Display to user space in the per-cpuset read-only file
2468 * "memory_pressure". Value displayed is an integer
2469 * representing the recent rate of entry into the synchronous
2470 * (direct) page reclaim by any task attached to the cpuset.
2471 **/
2472
2473void __cpuset_memory_pressure_bump(void)
2474{
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002475 task_lock(current);
Paul Menage8793d852007-10-18 23:39:39 -07002476 fmeter_markevent(&task_cs(current)->fmeter);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002477 task_unlock(current);
2478}
2479
Paul Menage8793d852007-10-18 23:39:39 -07002480#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002481/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 * proc_cpuset_show()
2483 * - Print tasks cpuset path into seq_file.
2484 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002485 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2486 * doesn't really matter if tsk->cpuset changes after we read it,
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002487 * and we take cgroup_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002488 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 */
Paul Jackson029190c2007-10-18 23:40:20 -07002490static int proc_cpuset_show(struct seq_file *m, void *unused_v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002492 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 struct task_struct *tsk;
2494 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002495 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002496 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Eric W. Biederman99f89552006-06-26 00:25:55 -07002498 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2500 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Eric W. Biederman99f89552006-06-26 00:25:55 -07002503 retval = -ESRCH;
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002504 pid = m->private;
2505 tsk = get_pid_task(pid, PIDTYPE_PID);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002506 if (!tsk)
2507 goto out_free;
2508
2509 retval = -EINVAL;
Paul Menage8793d852007-10-18 23:39:39 -07002510 cgroup_lock();
2511 css = task_subsys_state(tsk, cpuset_subsys_id);
2512 retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (retval < 0)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002514 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 seq_puts(m, buf);
2516 seq_putc(m, '\n');
Eric W. Biederman99f89552006-06-26 00:25:55 -07002517out_unlock:
Paul Menage8793d852007-10-18 23:39:39 -07002518 cgroup_unlock();
Eric W. Biederman99f89552006-06-26 00:25:55 -07002519 put_task_struct(tsk);
2520out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002522out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return retval;
2524}
2525
2526static int cpuset_open(struct inode *inode, struct file *file)
2527{
Eric W. Biederman13b41b02006-06-26 00:25:56 -07002528 struct pid *pid = PROC_I(inode)->pid;
2529 return single_open(file, proc_cpuset_show, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Arjan van de Ven9a321442007-02-12 00:55:35 -08002532const struct file_operations proc_cpuset_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .open = cpuset_open,
2534 .read = seq_read,
2535 .llseek = seq_lseek,
2536 .release = single_release,
2537};
Paul Menage8793d852007-10-18 23:39:39 -07002538#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540/* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002541void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002543 seq_printf(m, "Cpus_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002544 seq_cpumask(m, &task->cpus_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002545 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002546 seq_printf(m, "Cpus_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002547 seq_cpumask_list(m, &task->cpus_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002548 seq_printf(m, "\n");
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002549 seq_printf(m, "Mems_allowed:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002550 seq_nodemask(m, &task->mems_allowed);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002551 seq_printf(m, "\n");
Mike Travis39106dc2008-04-08 11:43:03 -07002552 seq_printf(m, "Mems_allowed_list:\t");
Lai Jiangshan30e8e132008-10-18 20:28:20 -07002553 seq_nodemask_list(m, &task->mems_allowed);
Mike Travis39106dc2008-04-08 11:43:03 -07002554 seq_printf(m, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}