blob: 59fbf66cf4221e15b8593247c3fca27bc099e018 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/cpuset.c
3 *
4 * Processor and Memory placement constraints for sets of tasks.
5 *
6 * Copyright (C) 2003 BULL SA.
Paul Jackson029190c2007-10-18 23:40:20 -07007 * Copyright (C) 2004-2007 Silicon Graphics, Inc.
Paul Menage8793d852007-10-18 23:39:39 -07008 * Copyright (C) 2006 Google, Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Portions derived from Patrick Mochel's sysfs code.
11 * sysfs is Copyright (c) 2001-3 Patrick Mochel
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
Paul Jackson825a46a2006-03-24 03:16:03 -080013 * 2003-10-10 Written by Simon Derr.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 2003-10-22 Updates by Stephen Hemminger.
Paul Jackson825a46a2006-03-24 03:16:03 -080015 * 2004 May-July Rework by Paul Jackson.
Paul Menage8793d852007-10-18 23:39:39 -070016 * 2006 Rework by Paul Menage to use generic cgroups
Max Krasnyanskycf417142008-08-11 14:33:53 -070017 * 2008 Rework of the scheduler domains and CPU hotplug handling
18 * by Max Krasnyansky
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/cpu.h>
26#include <linux/cpumask.h>
27#include <linux/cpuset.h>
28#include <linux/err.h>
29#include <linux/errno.h>
30#include <linux/file.h>
31#include <linux/fs.h>
32#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/kernel.h>
35#include <linux/kmod.h>
36#include <linux/list.h>
Paul Jackson68860ec2005-10-30 15:02:36 -080037#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mm.h>
Miao Xief4818912008-11-19 15:36:30 -080039#include <linux/memory.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040040#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mount.h>
42#include <linux/namei.h>
43#include <linux/pagemap.h>
44#include <linux/proc_fs.h>
Paul Jackson6b9c2602006-01-08 01:02:02 -080045#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sched.h>
47#include <linux/seq_file.h>
David Quigley22fb52d2006-06-23 02:04:00 -070048#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/spinlock.h>
51#include <linux/stat.h>
52#include <linux/string.h>
53#include <linux/time.h>
Arnd Bergmannd2b436582015-11-25 16:16:55 +010054#include <linux/time64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/backing-dev.h>
56#include <linux/sort.h>
57
58#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070059#include <linux/atomic.h>
Ingo Molnar3d3f26a2006-03-23 03:00:18 -080060#include <linux/mutex.h>
Cliff Wickman956db3c2008-02-07 00:14:43 -080061#include <linux/cgroup.h>
Li Zefane44193d2013-06-09 17:14:22 +080062#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Vlastimil Babka002f2902016-05-19 17:14:30 -070064DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key);
Paul Jackson202f72d2006-01-08 01:01:57 -080065
Paul Jackson3e0d98b2006-01-08 01:01:49 -080066/* See "Frequency meter" comments, below. */
67
68struct fmeter {
69 int cnt; /* unprocessed events count */
70 int val; /* most recent output value */
Arnd Bergmannd2b436582015-11-25 16:16:55 +010071 time64_t time; /* clock (secs) when val computed */
Paul Jackson3e0d98b2006-01-08 01:01:49 -080072 spinlock_t lock; /* guards read or write of above */
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct cpuset {
Paul Menage8793d852007-10-18 23:39:39 -070076 struct cgroup_subsys_state css;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned long flags; /* "unsigned long" so bitops work */
Li Zefane2b9a3d2014-07-09 16:47:03 +080079
Li Zefan7e882912014-07-09 16:48:42 +080080 /*
81 * On default hierarchy:
82 *
83 * The user-configured masks can only be changed by writing to
84 * cpuset.cpus and cpuset.mems, and won't be limited by the
85 * parent masks.
86 *
87 * The effective masks is the real masks that apply to the tasks
88 * in the cpuset. They may be changed if the configured masks are
89 * changed or hotplug happens.
90 *
91 * effective_mask == configured_mask & parent's effective_mask,
92 * and if it ends up empty, it will inherit the parent's mask.
93 *
94 *
95 * On legacy hierachy:
96 *
97 * The user-configured masks are always the same with effective masks.
98 */
99
Li Zefane2b9a3d2014-07-09 16:47:03 +0800100 /* user-configured CPUs and Memory Nodes allow to tasks */
101 cpumask_var_t cpus_allowed;
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700102 cpumask_var_t cpus_requested;
Li Zefane2b9a3d2014-07-09 16:47:03 +0800103 nodemask_t mems_allowed;
104
105 /* effective CPUs and Memory Nodes allow to tasks */
106 cpumask_var_t effective_cpus;
107 nodemask_t effective_mems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Li Zefan33ad8012013-06-09 17:15:08 +0800109 /*
110 * This is old Memory Nodes tasks took on.
111 *
112 * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
113 * - A new cpuset's old_mems_allowed is initialized when some
114 * task is moved into it.
115 * - old_mems_allowed is used in cpuset_migrate_mm() when we change
116 * cpuset.mems_allowed and have tasks' nodemask updated, and
117 * then old_mems_allowed is updated to mems_allowed.
118 */
119 nodemask_t old_mems_allowed;
120
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800121 struct fmeter fmeter; /* memory_pressure filter */
Paul Jackson029190c2007-10-18 23:40:20 -0700122
Tejun Heo452477f2013-01-07 08:51:07 -0800123 /*
124 * Tasks are being attached to this cpuset. Used to prevent
125 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
126 */
127 int attach_in_progress;
128
Paul Jackson029190c2007-10-18 23:40:20 -0700129 /* partition number for rebuild_sched_domains() */
130 int pn;
Cliff Wickman956db3c2008-02-07 00:14:43 -0800131
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900132 /* for custom sched domain */
133 int relax_domain_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Tejun Heoa7c6d552013-08-08 20:11:23 -0400136static inline struct cpuset *css_cs(struct cgroup_subsys_state *css)
Paul Menage8793d852007-10-18 23:39:39 -0700137{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400138 return css ? container_of(css, struct cpuset, css) : NULL;
Paul Menage8793d852007-10-18 23:39:39 -0700139}
140
141/* Retrieve the cpuset for a task */
142static inline struct cpuset *task_cs(struct task_struct *task)
143{
Tejun Heo073219e2014-02-08 10:36:58 -0500144 return css_cs(task_css(task, cpuset_cgrp_id));
Paul Menage8793d852007-10-18 23:39:39 -0700145}
Paul Menage8793d852007-10-18 23:39:39 -0700146
Tejun Heoc9710d82013-08-08 20:11:22 -0400147static inline struct cpuset *parent_cs(struct cpuset *cs)
Tejun Heoc4310692013-01-07 08:51:08 -0800148{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400149 return css_cs(cs->css.parent);
Tejun Heoc4310692013-01-07 08:51:08 -0800150}
151
David Rientjesb2462722011-12-19 17:11:52 -0800152#ifdef CONFIG_NUMA
153static inline bool task_has_mempolicy(struct task_struct *task)
154{
155 return task->mempolicy;
156}
157#else
158static inline bool task_has_mempolicy(struct task_struct *task)
159{
160 return false;
161}
162#endif
163
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* bits in struct cpuset flags field */
166typedef enum {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800167 CS_ONLINE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 CS_CPU_EXCLUSIVE,
169 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700170 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800171 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700172 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800173 CS_SPREAD_PAGE,
174 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175} cpuset_flagbits_t;
176
177/* convenient tests for these bits */
Tejun Heo829a1ca2017-05-24 12:03:48 -0400178static inline bool is_cpuset_online(struct cpuset *cs)
Tejun Heoefeb77b2013-01-07 08:51:07 -0800179{
Tejun Heo829a1ca2017-05-24 12:03:48 -0400180 return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css);
Tejun Heoefeb77b2013-01-07 08:51:07 -0800181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static inline int is_cpu_exclusive(const struct cpuset *cs)
184{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800185 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static inline int is_mem_exclusive(const struct cpuset *cs)
189{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800190 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Paul Menage78608362008-04-29 01:00:26 -0700193static inline int is_mem_hardwall(const struct cpuset *cs)
194{
195 return test_bit(CS_MEM_HARDWALL, &cs->flags);
196}
197
Paul Jackson029190c2007-10-18 23:40:20 -0700198static inline int is_sched_load_balance(const struct cpuset *cs)
199{
200 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
201}
202
Paul Jackson45b07ef2006-01-08 01:00:56 -0800203static inline int is_memory_migrate(const struct cpuset *cs)
204{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800205 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800206}
207
Paul Jackson825a46a2006-03-24 03:16:03 -0800208static inline int is_spread_page(const struct cpuset *cs)
209{
210 return test_bit(CS_SPREAD_PAGE, &cs->flags);
211}
212
213static inline int is_spread_slab(const struct cpuset *cs)
214{
215 return test_bit(CS_SPREAD_SLAB, &cs->flags);
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static struct cpuset top_cpuset = {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800219 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) |
220 (1 << CS_MEM_EXCLUSIVE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Tejun Heoae8086c2013-01-07 08:51:07 -0800223/**
224 * cpuset_for_each_child - traverse online children of a cpuset
225 * @child_cs: loop cursor pointing to the current child
Tejun Heo492eb212013-08-08 20:11:25 -0400226 * @pos_css: used for iteration
Tejun Heoae8086c2013-01-07 08:51:07 -0800227 * @parent_cs: target cpuset to walk children of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
Tejun Heoae8086c2013-01-07 08:51:07 -0800229 * Walk @child_cs through the online children of @parent_cs. Must be used
230 * with RCU read locked.
231 */
Tejun Heo492eb212013-08-08 20:11:25 -0400232#define cpuset_for_each_child(child_cs, pos_css, parent_cs) \
233 css_for_each_child((pos_css), &(parent_cs)->css) \
234 if (is_cpuset_online(((child_cs) = css_cs((pos_css)))))
Tejun Heoae8086c2013-01-07 08:51:07 -0800235
Tejun Heofc560a22013-01-07 08:51:08 -0800236/**
237 * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
238 * @des_cs: loop cursor pointing to the current descendant
Tejun Heo492eb212013-08-08 20:11:25 -0400239 * @pos_css: used for iteration
Tejun Heofc560a22013-01-07 08:51:08 -0800240 * @root_cs: target cpuset to walk ancestor of
241 *
242 * Walk @des_cs through the online descendants of @root_cs. Must be used
Tejun Heo492eb212013-08-08 20:11:25 -0400243 * with RCU read locked. The caller may modify @pos_css by calling
Tejun Heobd8815a2013-08-08 20:11:27 -0400244 * css_rightmost_descendant() to skip subtree. @root_cs is included in the
245 * iteration and the first node to be visited.
Tejun Heofc560a22013-01-07 08:51:08 -0800246 */
Tejun Heo492eb212013-08-08 20:11:25 -0400247#define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \
248 css_for_each_descendant_pre((pos_css), &(root_cs)->css) \
249 if (is_cpuset_online(((des_cs) = css_cs((pos_css)))))
Tejun Heofc560a22013-01-07 08:51:08 -0800250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400252 * There are two global locks guarding cpuset structures - cpuset_mutex and
253 * callback_lock. We also require taking task_lock() when dereferencing a
254 * task's cpuset pointer. See "The task_lock() exception", at the end of this
255 * comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400257 * A task must hold both locks to modify cpusets. If a task holds
Tejun Heo5d21cc22013-01-07 08:51:08 -0800258 * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400259 * is the only task able to also acquire callback_lock and be able to
Tejun Heo5d21cc22013-01-07 08:51:08 -0800260 * modify cpusets. It can perform various checks on the cpuset structure
261 * first, knowing nothing will change. It can also allocate memory while
262 * just holding cpuset_mutex. While it is performing these checks, various
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400263 * callback routines can briefly acquire callback_lock to query cpusets.
264 * Once it is ready to make the changes, it takes callback_lock, blocking
Tejun Heo5d21cc22013-01-07 08:51:08 -0800265 * everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
Paul Jackson053199e2005-10-30 15:02:30 -0800267 * Calls to the kernel memory allocator can not be made while holding
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400268 * callback_lock, as that would risk double tripping on callback_lock
Paul Jackson053199e2005-10-30 15:02:30 -0800269 * from one of the callbacks into the cpuset code from within
270 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400272 * If a task is only holding callback_lock, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800273 * access to cpusets.
274 *
Miao Xie58568d22009-06-16 15:31:49 -0700275 * Now, the task_struct fields mems_allowed and mempolicy may be changed
276 * by other task, we use alloc_lock in the task_struct fields to protect
277 * them.
Paul Jackson053199e2005-10-30 15:02:30 -0800278 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400279 * The cpuset_common_file_read() handlers only hold callback_lock across
Paul Jackson053199e2005-10-30 15:02:30 -0800280 * small pieces of code, such as when reading out possibly multi-word
281 * cpumasks and nodemasks.
282 *
Paul Menage2df167a2008-02-07 00:14:45 -0800283 * Accessing a task's cpuset should be done in accordance with the
284 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286
Tejun Heo5d21cc22013-01-07 08:51:08 -0800287static DEFINE_MUTEX(cpuset_mutex);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400288static DEFINE_SPINLOCK(callback_lock);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700289
Tejun Heoe93ad192016-01-19 12:18:41 -0500290static struct workqueue_struct *cpuset_migrate_mm_wq;
291
Max Krasnyanskycf417142008-08-11 14:33:53 -0700292/*
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800293 * CPU / memory hotplug is handled asynchronously.
294 */
295static void cpuset_hotplug_workfn(struct work_struct *work);
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800296static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn);
297
Li Zefane44193d2013-06-09 17:14:22 +0800298static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq);
299
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800300/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700301 * This is ugly, but preserves the userspace API for existing cpuset
Paul Menage8793d852007-10-18 23:39:39 -0700302 * users. If someone tries to mount the "cpuset" filesystem, we
Max Krasnyanskycf417142008-08-11 14:33:53 -0700303 * silently switch it to mount "cgroup" instead
304 */
Al Virof7e83572010-07-26 13:23:11 +0400305static struct dentry *cpuset_mount(struct file_system_type *fs_type,
306 int flags, const char *unused_dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Paul Menage8793d852007-10-18 23:39:39 -0700308 struct file_system_type *cgroup_fs = get_fs_type("cgroup");
Al Virof7e83572010-07-26 13:23:11 +0400309 struct dentry *ret = ERR_PTR(-ENODEV);
Paul Menage8793d852007-10-18 23:39:39 -0700310 if (cgroup_fs) {
311 char mountopts[] =
312 "cpuset,noprefix,"
313 "release_agent=/sbin/cpuset_release_agent";
Al Virof7e83572010-07-26 13:23:11 +0400314 ret = cgroup_fs->mount(cgroup_fs, flags,
315 unused_dev_name, mountopts);
Paul Menage8793d852007-10-18 23:39:39 -0700316 put_filesystem(cgroup_fs);
317 }
318 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321static struct file_system_type cpuset_fs_type = {
322 .name = "cpuset",
Al Virof7e83572010-07-26 13:23:11 +0400323 .mount = cpuset_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324};
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800327 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * are online. If none are online, walk up the cpuset hierarchy
Joonwoo Park28b89b92016-09-11 21:14:58 -0700329 * until we find one that does have some online cpus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
331 * One way or another, we guarantee to return some non-empty subset
Rusty Russell5f054e32012-03-29 15:38:31 +1030332 * of cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400334 * Call with callback_lock or cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400336static void guarantee_online_cpus(struct cpuset *cs, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Joonwoo Park28b89b92016-09-11 21:14:58 -0700338 while (!cpumask_intersects(cs->effective_cpus, cpu_online_mask)) {
Tejun Heoc4310692013-01-07 08:51:08 -0800339 cs = parent_cs(cs);
Joonwoo Park28b89b92016-09-11 21:14:58 -0700340 if (unlikely(!cs)) {
341 /*
342 * The top cpuset doesn't have any online cpu as a
343 * consequence of a race between cpuset_hotplug_work
344 * and cpu hotplug notifier. But we know the top
345 * cpuset's effective_cpus is on its way to to be
346 * identical to cpu_online_mask.
347 */
348 cpumask_copy(pmask, cpu_online_mask);
349 return;
350 }
351 }
Li Zefanae1c8022014-07-09 16:48:32 +0800352 cpumask_and(pmask, cs->effective_cpus, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/*
356 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700357 * are online, with memory. If none are online with memory, walk
358 * up the cpuset hierarchy until we find one that does have some
Li Zefan40df2de2013-06-05 17:15:23 +0800359 * online mems. The top cpuset always has some mems online.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * One way or another, we guarantee to return some non-empty subset
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800362 * of node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400364 * Call with callback_lock or cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400366static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Li Zefanae1c8022014-07-09 16:48:32 +0800368 while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
Tejun Heoc4310692013-01-07 08:51:08 -0800369 cs = parent_cs(cs);
Li Zefanae1c8022014-07-09 16:48:32 +0800370 nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Miao Xief3b39d42009-06-16 15:31:46 -0700373/*
374 * update task's spread flag if cpuset's page/slab spread flag is set
375 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400376 * Call with callback_lock or cpuset_mutex held.
Miao Xief3b39d42009-06-16 15:31:46 -0700377 */
378static void cpuset_update_task_spread_flag(struct cpuset *cs,
379 struct task_struct *tsk)
380{
381 if (is_spread_page(cs))
Zefan Li2ad654b2014-09-25 09:41:02 +0800382 task_set_spread_page(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700383 else
Zefan Li2ad654b2014-09-25 09:41:02 +0800384 task_clear_spread_page(tsk);
385
Miao Xief3b39d42009-06-16 15:31:46 -0700386 if (is_spread_slab(cs))
Zefan Li2ad654b2014-09-25 09:41:02 +0800387 task_set_spread_slab(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700388 else
Zefan Li2ad654b2014-09-25 09:41:02 +0800389 task_clear_spread_slab(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
393 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
394 *
395 * One cpuset is a subset of another if all its allowed CPUs and
396 * Memory Nodes are a subset of the other, and its exclusive flags
Tejun Heo5d21cc22013-01-07 08:51:08 -0800397 * are only set if the other's are set. Call holding cpuset_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399
400static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
401{
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700402 return cpumask_subset(p->cpus_requested, q->cpus_requested) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 nodes_subset(p->mems_allowed, q->mems_allowed) &&
404 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
405 is_mem_exclusive(p) <= is_mem_exclusive(q);
406}
407
Li Zefan645fcc92009-01-07 18:08:43 -0800408/**
409 * alloc_trial_cpuset - allocate a trial cpuset
410 * @cs: the cpuset that the trial cpuset duplicates
411 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400412static struct cpuset *alloc_trial_cpuset(struct cpuset *cs)
Li Zefan645fcc92009-01-07 18:08:43 -0800413{
Li Zefan300ed6c2009-01-07 18:08:44 -0800414 struct cpuset *trial;
415
416 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
417 if (!trial)
418 return NULL;
419
Li Zefane2b9a3d2014-07-09 16:47:03 +0800420 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL))
421 goto free_cs;
422 if (!alloc_cpumask_var(&trial->effective_cpus, GFP_KERNEL))
423 goto free_cpus;
Li Zefan300ed6c2009-01-07 18:08:44 -0800424
Li Zefane2b9a3d2014-07-09 16:47:03 +0800425 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
426 cpumask_copy(trial->effective_cpus, cs->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -0800427 return trial;
Li Zefane2b9a3d2014-07-09 16:47:03 +0800428
429free_cpus:
430 free_cpumask_var(trial->cpus_allowed);
431free_cs:
432 kfree(trial);
433 return NULL;
Li Zefan645fcc92009-01-07 18:08:43 -0800434}
435
436/**
437 * free_trial_cpuset - free the trial cpuset
438 * @trial: the trial cpuset to be freed
439 */
440static void free_trial_cpuset(struct cpuset *trial)
441{
Li Zefane2b9a3d2014-07-09 16:47:03 +0800442 free_cpumask_var(trial->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -0800443 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800444 kfree(trial);
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
448 * validate_change() - Used to validate that any proposed cpuset change
449 * follows the structural rules for cpusets.
450 *
451 * If we replaced the flag and mask values of the current cpuset
452 * (cur) with those values in the trial cpuset (trial), would
453 * our various subset and exclusive rules still be valid? Presumes
Tejun Heo5d21cc22013-01-07 08:51:08 -0800454 * cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
456 * 'cur' is the address of an actual, in-use cpuset. Operations
457 * such as list traversal that depend on the actual address of the
458 * cpuset in the list must use cur below, not trial.
459 *
460 * 'trial' is the address of bulk structure copy of cur, with
461 * perhaps one or more of the fields cpus_allowed, mems_allowed,
462 * or flags changed to new, trial values.
463 *
464 * Return 0 if valid, -errno if not.
465 */
466
Tejun Heoc9710d82013-08-08 20:11:22 -0400467static int validate_change(struct cpuset *cur, struct cpuset *trial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Tejun Heo492eb212013-08-08 20:11:25 -0400469 struct cgroup_subsys_state *css;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct cpuset *c, *par;
Tejun Heoae8086c2013-01-07 08:51:07 -0800471 int ret;
472
473 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Each of our child cpusets must be a subset of us */
Tejun Heoae8086c2013-01-07 08:51:07 -0800476 ret = -EBUSY;
Tejun Heo492eb212013-08-08 20:11:25 -0400477 cpuset_for_each_child(c, css, cur)
Tejun Heoae8086c2013-01-07 08:51:07 -0800478 if (!is_cpuset_subset(c, trial))
479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* Remaining checks don't apply to root cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800482 ret = 0;
Paul Jackson69604062006-12-06 20:36:15 -0800483 if (cur == &top_cpuset)
Tejun Heoae8086c2013-01-07 08:51:07 -0800484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Tejun Heoc4310692013-01-07 08:51:08 -0800486 par = parent_cs(cur);
Paul Jackson69604062006-12-06 20:36:15 -0800487
Li Zefan7e882912014-07-09 16:48:42 +0800488 /* On legacy hiearchy, we must be a subset of our parent cpuset. */
Tejun Heoae8086c2013-01-07 08:51:07 -0800489 ret = -EACCES;
Tejun Heo9e10a132015-09-18 11:56:28 -0400490 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
491 !is_cpuset_subset(trial, par))
Tejun Heoae8086c2013-01-07 08:51:07 -0800492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Paul Menage2df167a2008-02-07 00:14:45 -0800494 /*
495 * If either I or some sibling (!= me) is exclusive, we can't
496 * overlap
497 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800498 ret = -EINVAL;
Tejun Heo492eb212013-08-08 20:11:25 -0400499 cpuset_for_each_child(c, css, par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
501 c != cur &&
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700502 cpumask_intersects(trial->cpus_requested, c->cpus_requested))
Tejun Heoae8086c2013-01-07 08:51:07 -0800503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
505 c != cur &&
506 nodes_intersects(trial->mems_allowed, c->mems_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
Tejun Heo452477f2013-01-07 08:51:07 -0800510 /*
511 * Cpusets with tasks - existing or newly being attached - can't
Li Zefan1c09b192013-08-21 10:22:28 +0800512 * be changed to have empty cpus_allowed or mems_allowed.
Tejun Heo452477f2013-01-07 08:51:07 -0800513 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800514 ret = -ENOSPC;
Tejun Heo27bd4db2015-10-15 16:41:50 -0400515 if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) {
Li Zefan1c09b192013-08-21 10:22:28 +0800516 if (!cpumask_empty(cur->cpus_allowed) &&
517 cpumask_empty(trial->cpus_allowed))
518 goto out;
519 if (!nodes_empty(cur->mems_allowed) &&
520 nodes_empty(trial->mems_allowed))
521 goto out;
522 }
Paul Jackson020958b2007-10-18 23:40:21 -0700523
Juri Lellif82f8042014-10-07 09:52:11 +0100524 /*
525 * We can't shrink if we won't have enough room for SCHED_DEADLINE
526 * tasks.
527 */
528 ret = -EBUSY;
529 if (is_cpu_exclusive(cur) &&
530 !cpuset_cpumask_can_shrink(cur->cpus_allowed,
531 trial->cpus_allowed))
532 goto out;
533
Tejun Heoae8086c2013-01-07 08:51:07 -0800534 ret = 0;
535out:
536 rcu_read_unlock();
537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Paul Menagedb7f47c2009-04-02 16:57:55 -0700540#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700541/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700542 * Helper routine for generate_sched_domains().
Li Zefan8b5f1c52014-07-09 16:47:50 +0800543 * Do cpusets a, b have overlapping effective cpus_allowed masks?
Paul Jackson029190c2007-10-18 23:40:20 -0700544 */
Paul Jackson029190c2007-10-18 23:40:20 -0700545static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
546{
Li Zefan8b5f1c52014-07-09 16:47:50 +0800547 return cpumask_intersects(a->effective_cpus, b->effective_cpus);
Paul Jackson029190c2007-10-18 23:40:20 -0700548}
549
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900550static void
551update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
552{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900553 if (dattr->relax_domain_level < c->relax_domain_level)
554 dattr->relax_domain_level = c->relax_domain_level;
555 return;
556}
557
Tejun Heofc560a22013-01-07 08:51:08 -0800558static void update_domain_attr_tree(struct sched_domain_attr *dattr,
559 struct cpuset *root_cs)
Lai Jiangshanf5393692008-07-29 22:33:22 -0700560{
Tejun Heofc560a22013-01-07 08:51:08 -0800561 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -0400562 struct cgroup_subsys_state *pos_css;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700563
Tejun Heofc560a22013-01-07 08:51:08 -0800564 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400565 cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
Tejun Heofc560a22013-01-07 08:51:08 -0800566 /* skip the whole subtree if @cp doesn't have any CPU */
567 if (cpumask_empty(cp->cpus_allowed)) {
Tejun Heo492eb212013-08-08 20:11:25 -0400568 pos_css = css_rightmost_descendant(pos_css);
Lai Jiangshanf5393692008-07-29 22:33:22 -0700569 continue;
Tejun Heofc560a22013-01-07 08:51:08 -0800570 }
Lai Jiangshanf5393692008-07-29 22:33:22 -0700571
572 if (is_sched_load_balance(cp))
573 update_domain_attr(dattr, cp);
Lai Jiangshanf5393692008-07-29 22:33:22 -0700574 }
Tejun Heofc560a22013-01-07 08:51:08 -0800575 rcu_read_unlock();
Lai Jiangshanf5393692008-07-29 22:33:22 -0700576}
577
Paul Jackson029190c2007-10-18 23:40:20 -0700578/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700579 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700580 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700581 * This function builds a partial partition of the systems CPUs
582 * A 'partial partition' is a set of non-overlapping subsets whose
583 * union is a subset of that set.
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530584 * The output of this function needs to be passed to kernel/sched/core.c
Max Krasnyanskycf417142008-08-11 14:33:53 -0700585 * partition_sched_domains() routine, which will rebuild the scheduler's
586 * load balancing domains (sched domains) as specified by that partial
587 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700588 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800589 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700590 * for a background explanation of this.
591 *
592 * Does not return errors, on the theory that the callers of this
593 * routine would rather not worry about failures to rebuild sched
594 * domains when operating in the severe memory shortage situations
595 * that could cause allocation failures below.
596 *
Tejun Heo5d21cc22013-01-07 08:51:08 -0800597 * Must be called with cpuset_mutex held.
Paul Jackson029190c2007-10-18 23:40:20 -0700598 *
599 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700600 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700601 * top-down scan of all cpusets. This scan loads a pointer
602 * to each cpuset marked is_sched_load_balance into the
603 * array 'csa'. For our purposes, rebuilding the schedulers
604 * sched domains, we can ignore !is_sched_load_balance cpusets.
605 * csa - (for CpuSet Array) Array of pointers to all the cpusets
606 * that need to be load balanced, for convenient iterative
607 * access by the subsequent code that finds the best partition,
608 * i.e the set of domains (subsets) of CPUs such that the
609 * cpus_allowed of every cpuset marked is_sched_load_balance
610 * is a subset of one of these domains, while there are as
611 * many such domains as possible, each as small as possible.
612 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530613 * the kernel/sched/core.c routine partition_sched_domains() in a
Paul Jackson029190c2007-10-18 23:40:20 -0700614 * convenient format, that can be easily compared to the prior
615 * value to determine what partition elements (sched domains)
616 * were changed (added or removed.)
617 *
618 * Finding the best partition (set of domains):
619 * The triple nested loops below over i, j, k scan over the
620 * load balanced cpusets (using the array of cpuset pointers in
621 * csa[]) looking for pairs of cpusets that have overlapping
622 * cpus_allowed, but which don't have the same 'pn' partition
623 * number and gives them in the same partition number. It keeps
624 * looping on the 'restart' label until it can no longer find
625 * any such pairs.
626 *
627 * The union of the cpus_allowed masks from the set of
628 * all cpusets having the same 'pn' value then form the one
629 * element of the partition (one sched domain) to be passed to
630 * partition_sched_domains().
631 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030632static int generate_sched_domains(cpumask_var_t **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700633 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700634{
Paul Jackson029190c2007-10-18 23:40:20 -0700635 struct cpuset *cp; /* scans q */
636 struct cpuset **csa; /* array of all cpuset ptrs */
637 int csn; /* how many cpuset ptrs in csa so far */
638 int i, j, k; /* indices for partition finding loops */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030639 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
Rik van Riel47b8ea72015-03-09 12:12:08 -0400640 cpumask_var_t non_isolated_cpus; /* load balanced CPUs */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900641 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100642 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800643 int nslot; /* next empty doms[] struct cpumask slot */
Tejun Heo492eb212013-08-08 20:11:25 -0400644 struct cgroup_subsys_state *pos_css;
Paul Jackson029190c2007-10-18 23:40:20 -0700645
Paul Jackson029190c2007-10-18 23:40:20 -0700646 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900647 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700648 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700649
Rik van Riel47b8ea72015-03-09 12:12:08 -0400650 if (!alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL))
651 goto done;
652 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
653
Paul Jackson029190c2007-10-18 23:40:20 -0700654 /* Special case for the 99% of systems with one, full, sched domain */
655 if (is_sched_load_balance(&top_cpuset)) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030656 ndoms = 1;
657 doms = alloc_sched_domains(ndoms);
Paul Jackson029190c2007-10-18 23:40:20 -0700658 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700659 goto done;
660
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900661 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
662 if (dattr) {
663 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700664 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900665 }
Rik van Riel47b8ea72015-03-09 12:12:08 -0400666 cpumask_and(doms[0], top_cpuset.effective_cpus,
667 non_isolated_cpus);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700668
Max Krasnyanskycf417142008-08-11 14:33:53 -0700669 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700670 }
671
Mel Gorman664eedd2014-06-04 16:10:08 -0700672 csa = kmalloc(nr_cpusets() * sizeof(cp), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700673 if (!csa)
674 goto done;
675 csn = 0;
676
Tejun Heofc560a22013-01-07 08:51:08 -0800677 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400678 cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
Tejun Heobd8815a2013-08-08 20:11:27 -0400679 if (cp == &top_cpuset)
680 continue;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700681 /*
Tejun Heofc560a22013-01-07 08:51:08 -0800682 * Continue traversing beyond @cp iff @cp has some CPUs and
683 * isn't load balancing. The former is obvious. The
684 * latter: All child cpusets contain a subset of the
685 * parent's cpus, so just skip them, and then we call
686 * update_domain_attr_tree() to calc relax_domain_level of
687 * the corresponding sched domain.
Lai Jiangshanf5393692008-07-29 22:33:22 -0700688 */
Tejun Heofc560a22013-01-07 08:51:08 -0800689 if (!cpumask_empty(cp->cpus_allowed) &&
Rik van Riel47b8ea72015-03-09 12:12:08 -0400690 !(is_sched_load_balance(cp) &&
691 cpumask_intersects(cp->cpus_allowed, non_isolated_cpus)))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700692 continue;
Lai Jiangshan489a5392008-07-25 01:47:23 -0700693
Tejun Heofc560a22013-01-07 08:51:08 -0800694 if (is_sched_load_balance(cp))
695 csa[csn++] = cp;
696
697 /* skip @cp's subtree */
Tejun Heo492eb212013-08-08 20:11:25 -0400698 pos_css = css_rightmost_descendant(pos_css);
Tejun Heofc560a22013-01-07 08:51:08 -0800699 }
700 rcu_read_unlock();
Paul Jackson029190c2007-10-18 23:40:20 -0700701
702 for (i = 0; i < csn; i++)
703 csa[i]->pn = i;
704 ndoms = csn;
705
706restart:
707 /* Find the best partition (set of sched domains) */
708 for (i = 0; i < csn; i++) {
709 struct cpuset *a = csa[i];
710 int apn = a->pn;
711
712 for (j = 0; j < csn; j++) {
713 struct cpuset *b = csa[j];
714 int bpn = b->pn;
715
716 if (apn != bpn && cpusets_overlap(a, b)) {
717 for (k = 0; k < csn; k++) {
718 struct cpuset *c = csa[k];
719
720 if (c->pn == bpn)
721 c->pn = apn;
722 }
723 ndoms--; /* one less element */
724 goto restart;
725 }
726 }
727 }
728
Max Krasnyanskycf417142008-08-11 14:33:53 -0700729 /*
730 * Now we know how many domains to create.
731 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
732 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030733 doms = alloc_sched_domains(ndoms);
Li Zefan700018e2008-11-18 14:02:03 +0800734 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700735 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700736
737 /*
738 * The rest of the code, including the scheduler, can deal with
739 * dattr==NULL case. No need to abort if alloc fails.
740 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900741 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700742
743 for (nslot = 0, i = 0; i < csn; i++) {
744 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800745 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700746 int apn = a->pn;
747
Max Krasnyanskycf417142008-08-11 14:33:53 -0700748 if (apn < 0) {
749 /* Skip completed partitions */
750 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700751 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700752
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030753 dp = doms[nslot];
Max Krasnyanskycf417142008-08-11 14:33:53 -0700754
755 if (nslot == ndoms) {
756 static int warnings = 10;
757 if (warnings) {
Fabian Frederick12d30892014-05-05 19:49:00 +0200758 pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n",
759 nslot, ndoms, csn, i, apn);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700760 warnings--;
761 }
762 continue;
763 }
764
Li Zefan6af866a2009-01-07 18:08:45 -0800765 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700766 if (dattr)
767 *(dattr + nslot) = SD_ATTR_INIT;
768 for (j = i; j < csn; j++) {
769 struct cpuset *b = csa[j];
770
771 if (apn == b->pn) {
Li Zefan8b5f1c52014-07-09 16:47:50 +0800772 cpumask_or(dp, dp, b->effective_cpus);
Rik van Riel47b8ea72015-03-09 12:12:08 -0400773 cpumask_and(dp, dp, non_isolated_cpus);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700774 if (dattr)
775 update_domain_attr_tree(dattr + nslot, b);
776
777 /* Done with this partition */
778 b->pn = -1;
779 }
780 }
781 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700782 }
783 BUG_ON(nslot != ndoms);
784
Paul Jackson029190c2007-10-18 23:40:20 -0700785done:
Rik van Riel47b8ea72015-03-09 12:12:08 -0400786 free_cpumask_var(non_isolated_cpus);
Paul Jackson029190c2007-10-18 23:40:20 -0700787 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700788
Li Zefan700018e2008-11-18 14:02:03 +0800789 /*
790 * Fallback to the default domain if kmalloc() failed.
791 * See comments in partition_sched_domains().
792 */
793 if (doms == NULL)
794 ndoms = 1;
795
Max Krasnyanskycf417142008-08-11 14:33:53 -0700796 *domains = doms;
797 *attributes = dattr;
798 return ndoms;
799}
800
801/*
802 * Rebuild scheduler domains.
803 *
Tejun Heo699140b2013-01-07 08:51:07 -0800804 * If the flag 'sched_load_balance' of any cpuset with non-empty
805 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
806 * which has that flag enabled, or if any cpuset with a non-empty
807 * 'cpus' is removed, then call this routine to rebuild the
808 * scheduler's dynamic sched domains.
Max Krasnyanskycf417142008-08-11 14:33:53 -0700809 *
Tejun Heo5d21cc22013-01-07 08:51:08 -0800810 * Call with cpuset_mutex held. Takes get_online_cpus().
Max Krasnyanskycf417142008-08-11 14:33:53 -0700811 */
Tejun Heo699140b2013-01-07 08:51:07 -0800812static void rebuild_sched_domains_locked(void)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700813{
814 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030815 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700816 int ndoms;
817
Tejun Heo5d21cc22013-01-07 08:51:08 -0800818 lockdep_assert_held(&cpuset_mutex);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700819 get_online_cpus();
820
Li Zefan5b16c2a2013-04-27 06:52:43 -0700821 /*
822 * We have raced with CPU hotplug. Don't do anything to avoid
823 * passing doms with offlined cpu to partition_sched_domains().
824 * Anyways, hotplug work item will rebuild sched domains.
825 */
Li Zefan8b5f1c52014-07-09 16:47:50 +0800826 if (!cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask))
Li Zefan5b16c2a2013-04-27 06:52:43 -0700827 goto out;
828
Max Krasnyanskycf417142008-08-11 14:33:53 -0700829 /* Generate domain masks and attrs */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700830 ndoms = generate_sched_domains(&doms, &attr);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700831
832 /* Have scheduler rebuild the domains */
833 partition_sched_domains(ndoms, doms, attr);
Li Zefan5b16c2a2013-04-27 06:52:43 -0700834out:
Max Krasnyanskycf417142008-08-11 14:33:53 -0700835 put_online_cpus();
836}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700837#else /* !CONFIG_SMP */
Tejun Heo699140b2013-01-07 08:51:07 -0800838static void rebuild_sched_domains_locked(void)
Paul Menagedb7f47c2009-04-02 16:57:55 -0700839{
840}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700841#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700842
Max Krasnyanskycf417142008-08-11 14:33:53 -0700843void rebuild_sched_domains(void)
844{
Tejun Heo5d21cc22013-01-07 08:51:08 -0800845 mutex_lock(&cpuset_mutex);
Tejun Heo699140b2013-01-07 08:51:07 -0800846 rebuild_sched_domains_locked();
Tejun Heo5d21cc22013-01-07 08:51:08 -0800847 mutex_unlock(&cpuset_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700848}
849
Cliff Wickman58f47902008-02-07 00:14:44 -0800850/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700851 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
852 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
853 *
Tejun Heod66393e2014-02-13 06:58:40 -0500854 * Iterate through each task of @cs updating its cpus_allowed to the
855 * effective cpuset's. As this function is called with cpuset_mutex held,
856 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -0700857 */
Tejun Heod66393e2014-02-13 06:58:40 -0500858static void update_tasks_cpumask(struct cpuset *cs)
Miao Xie0b2f6302008-07-25 01:47:21 -0700859{
Tejun Heod66393e2014-02-13 06:58:40 -0500860 struct css_task_iter it;
861 struct task_struct *task;
862
863 css_task_iter_start(&cs->css, &it);
864 while ((task = css_task_iter_next(&it)))
Li Zefanae1c8022014-07-09 16:48:32 +0800865 set_cpus_allowed_ptr(task, cs->effective_cpus);
Tejun Heod66393e2014-02-13 06:58:40 -0500866 css_task_iter_end(&it);
Miao Xie0b2f6302008-07-25 01:47:21 -0700867}
868
Li Zefan5c5cc622013-06-09 17:16:29 +0800869/*
Li Zefan734d4512014-07-09 16:47:29 +0800870 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
871 * @cs: the cpuset to consider
872 * @new_cpus: temp variable for calculating new effective_cpus
Li Zefan5c5cc622013-06-09 17:16:29 +0800873 *
Li Zefan734d4512014-07-09 16:47:29 +0800874 * When congifured cpumask is changed, the effective cpumasks of this cpuset
875 * and all its descendants need to be updated.
876 *
877 * On legacy hierachy, effective_cpus will be the same with cpu_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +0800878 *
879 * Called with cpuset_mutex held
880 */
Li Zefan734d4512014-07-09 16:47:29 +0800881static void update_cpumasks_hier(struct cpuset *cs, struct cpumask *new_cpus)
Li Zefan5c5cc622013-06-09 17:16:29 +0800882{
883 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -0400884 struct cgroup_subsys_state *pos_css;
Li Zefan8b5f1c52014-07-09 16:47:50 +0800885 bool need_rebuild_sched_domains = false;
Li Zefan5c5cc622013-06-09 17:16:29 +0800886
887 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +0800888 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
889 struct cpuset *parent = parent_cs(cp);
890
891 cpumask_and(new_cpus, cp->cpus_allowed, parent->effective_cpus);
892
Li Zefan554b0d12014-07-09 16:47:41 +0800893 /*
894 * If it becomes empty, inherit the effective mask of the
895 * parent, which is guaranteed to have some CPUs.
896 */
Tejun Heo9e10a132015-09-18 11:56:28 -0400897 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
898 cpumask_empty(new_cpus))
Li Zefan554b0d12014-07-09 16:47:41 +0800899 cpumask_copy(new_cpus, parent->effective_cpus);
900
Li Zefan734d4512014-07-09 16:47:29 +0800901 /* Skip the whole subtree if the cpumask remains the same. */
902 if (cpumask_equal(new_cpus, cp->effective_cpus)) {
903 pos_css = css_rightmost_descendant(pos_css);
904 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +0800905 }
Li Zefan734d4512014-07-09 16:47:29 +0800906
Tejun Heoec903c02014-05-13 12:11:01 -0400907 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +0800908 continue;
909 rcu_read_unlock();
910
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400911 spin_lock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +0800912 cpumask_copy(cp->effective_cpus, new_cpus);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400913 spin_unlock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +0800914
Tejun Heo9e10a132015-09-18 11:56:28 -0400915 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefan734d4512014-07-09 16:47:29 +0800916 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
917
Tejun Heod66393e2014-02-13 06:58:40 -0500918 update_tasks_cpumask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +0800919
Li Zefan8b5f1c52014-07-09 16:47:50 +0800920 /*
921 * If the effective cpumask of any non-empty cpuset is changed,
922 * we need to rebuild sched domains.
923 */
924 if (!cpumask_empty(cp->cpus_allowed) &&
925 is_sched_load_balance(cp))
926 need_rebuild_sched_domains = true;
927
Li Zefan5c5cc622013-06-09 17:16:29 +0800928 rcu_read_lock();
929 css_put(&cp->css);
930 }
931 rcu_read_unlock();
Li Zefan8b5f1c52014-07-09 16:47:50 +0800932
933 if (need_rebuild_sched_domains)
934 rebuild_sched_domains_locked();
Li Zefan5c5cc622013-06-09 17:16:29 +0800935}
936
Miao Xie0b2f6302008-07-25 01:47:21 -0700937/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800938 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
939 * @cs: the cpuset to consider
Fabian Frederickfc34ac12014-05-05 19:46:55 +0200940 * @trialcs: trial cpuset
Cliff Wickman58f47902008-02-07 00:14:44 -0800941 * @buf: buffer of cpu numbers written to this cpuset
942 */
Li Zefan645fcc92009-01-07 18:08:43 -0800943static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
944 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Cliff Wickman58f47902008-02-07 00:14:44 -0800946 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rusty Russell5f054e32012-03-29 15:38:31 +1030948 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700949 if (cs == &top_cpuset)
950 return -EACCES;
951
David Rientjes6f7f02e2007-05-08 00:31:43 -0700952 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800953 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700954 * Since cpulist_parse() fails on an empty mask, we special case
955 * that parsing. The validate_change() call ensures that cpusets
956 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700957 */
Paul Jackson020958b2007-10-18 23:40:21 -0700958 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800959 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700960 } else {
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700961 retval = cpulist_parse(buf, trialcs->cpus_requested);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700962 if (retval < 0)
963 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700964
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700965 if (!cpumask_subset(trialcs->cpus_requested, cpu_present_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700966 return -EINVAL;
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700967
968 cpumask_and(trialcs->cpus_allowed, trialcs->cpus_requested, cpu_active_mask);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700969 }
Paul Jackson029190c2007-10-18 23:40:20 -0700970
Paul Menage8707d8b2007-10-18 23:40:22 -0700971 /* Nothing to do if the cpus didn't change */
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700972 if (cpumask_equal(cs->cpus_requested, trialcs->cpus_requested))
Paul Menage8707d8b2007-10-18 23:40:22 -0700973 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800974
Li Zefana73456f2013-06-05 17:15:59 +0800975 retval = validate_change(cs, trialcs);
976 if (retval < 0)
977 return retval;
978
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400979 spin_lock_irq(&callback_lock);
Li Zefan300ed6c2009-01-07 18:08:44 -0800980 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -0700981 cpumask_copy(cs->cpus_requested, trialcs->cpus_requested);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400982 spin_unlock_irq(&callback_lock);
Paul Jackson029190c2007-10-18 23:40:20 -0700983
Li Zefan734d4512014-07-09 16:47:29 +0800984 /* use trialcs->cpus_allowed as a temp variable */
985 update_cpumasks_hier(cs, trialcs->cpus_allowed);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Paul Jackson053199e2005-10-30 15:02:30 -0800989/*
Tejun Heoe93ad192016-01-19 12:18:41 -0500990 * Migrate memory region from one set of nodes to another. This is
991 * performed asynchronously as it can be called from process migration path
992 * holding locks involved in process management. All mm migrations are
993 * performed in the queued order and can be waited for by flushing
994 * cpuset_migrate_mm_wq.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800995 */
996
Tejun Heoe93ad192016-01-19 12:18:41 -0500997struct cpuset_migrate_mm_work {
998 struct work_struct work;
999 struct mm_struct *mm;
1000 nodemask_t from;
1001 nodemask_t to;
1002};
1003
1004static void cpuset_migrate_mm_workfn(struct work_struct *work)
1005{
1006 struct cpuset_migrate_mm_work *mwork =
1007 container_of(work, struct cpuset_migrate_mm_work, work);
1008
1009 /* on a wq worker, no need to worry about %current's mems_allowed */
1010 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
1011 mmput(mwork->mm);
1012 kfree(mwork);
1013}
1014
Paul Jacksone4e364e2006-03-31 02:30:52 -08001015static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1016 const nodemask_t *to)
1017{
Tejun Heoe93ad192016-01-19 12:18:41 -05001018 struct cpuset_migrate_mm_work *mwork;
Paul Jacksone4e364e2006-03-31 02:30:52 -08001019
Tejun Heoe93ad192016-01-19 12:18:41 -05001020 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL);
1021 if (mwork) {
1022 mwork->mm = mm;
1023 mwork->from = *from;
1024 mwork->to = *to;
1025 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn);
1026 queue_work(cpuset_migrate_mm_wq, &mwork->work);
1027 } else {
1028 mmput(mm);
1029 }
1030}
Paul Jacksone4e364e2006-03-31 02:30:52 -08001031
Tejun Heo5cf1cac2016-04-21 19:06:48 -04001032static void cpuset_post_attach(void)
Tejun Heoe93ad192016-01-19 12:18:41 -05001033{
1034 flush_workqueue(cpuset_migrate_mm_wq);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001035}
1036
Li Zefan3b6766f2009-04-02 16:57:51 -07001037/*
Miao Xie58568d22009-06-16 15:31:49 -07001038 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
1039 * @tsk: the task to change
1040 * @newmems: new nodes that the task will be set
1041 *
1042 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
1043 * we structure updates as setting all new allowed nodes, then clearing newly
1044 * disallowed ones.
Miao Xie58568d22009-06-16 15:31:49 -07001045 */
1046static void cpuset_change_task_nodemask(struct task_struct *tsk,
1047 nodemask_t *newmems)
1048{
David Rientjesb2462722011-12-19 17:11:52 -08001049 bool need_loop;
David Rientjes89e8a242011-11-02 13:38:39 -07001050
Miao Xiec0ff7452010-05-24 14:32:08 -07001051 task_lock(tsk);
David Rientjesb2462722011-12-19 17:11:52 -08001052 /*
1053 * Determine if a loop is necessary if another thread is doing
Mel Gormand26914d2014-04-03 14:47:24 -07001054 * read_mems_allowed_begin(). If at least one node remains unchanged and
David Rientjesb2462722011-12-19 17:11:52 -08001055 * tsk does not have a mempolicy, then an empty nodemask will not be
1056 * possible when mems_allowed is larger than a word.
1057 */
1058 need_loop = task_has_mempolicy(tsk) ||
1059 !nodes_intersects(*newmems, tsk->mems_allowed);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001060
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001061 if (need_loop) {
1062 local_irq_disable();
Mel Gormancc9a6c82012-03-21 16:34:11 -07001063 write_seqcount_begin(&tsk->mems_allowed_seq);
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001064 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001065
Miao Xie58568d22009-06-16 15:31:49 -07001066 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Miao Xiec0ff7452010-05-24 14:32:08 -07001067 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
1068
Miao Xiec0ff7452010-05-24 14:32:08 -07001069 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
Miao Xie58568d22009-06-16 15:31:49 -07001070 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001071
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001072 if (need_loop) {
Mel Gormancc9a6c82012-03-21 16:34:11 -07001073 write_seqcount_end(&tsk->mems_allowed_seq);
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001074 local_irq_enable();
1075 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001076
Miao Xiec0ff7452010-05-24 14:32:08 -07001077 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001078}
1079
Paul Menage8793d852007-10-18 23:39:39 -07001080static void *cpuset_being_rebound;
1081
Miao Xie0b2f6302008-07-25 01:47:21 -07001082/**
1083 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1084 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
Miao Xie0b2f6302008-07-25 01:47:21 -07001085 *
Tejun Heod66393e2014-02-13 06:58:40 -05001086 * Iterate through each task of @cs updating its mems_allowed to the
1087 * effective cpuset's. As this function is called with cpuset_mutex held,
1088 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -07001089 */
Tejun Heod66393e2014-02-13 06:58:40 -05001090static void update_tasks_nodemask(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Li Zefan33ad8012013-06-09 17:15:08 +08001092 static nodemask_t newmems; /* protected by cpuset_mutex */
Tejun Heod66393e2014-02-13 06:58:40 -05001093 struct css_task_iter it;
1094 struct task_struct *task;
Paul Jackson59dac162006-01-08 01:01:52 -08001095
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001096 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001097
Li Zefanae1c8022014-07-09 16:48:32 +08001098 guarantee_online_mems(cs, &newmems);
Li Zefan33ad8012013-06-09 17:15:08 +08001099
Paul Jackson42253992006-01-08 01:01:59 -08001100 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001101 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1102 * take while holding tasklist_lock. Forks can happen - the
1103 * mpol_dup() cpuset_being_rebound check will catch such forks,
1104 * and rebind their vma mempolicies too. Because we still hold
Tejun Heo5d21cc22013-01-07 08:51:08 -08001105 * the global cpuset_mutex, we know that no other rebind effort
Li Zefan3b6766f2009-04-02 16:57:51 -07001106 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001107 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001108 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001109 */
Tejun Heod66393e2014-02-13 06:58:40 -05001110 css_task_iter_start(&cs->css, &it);
1111 while ((task = css_task_iter_next(&it))) {
1112 struct mm_struct *mm;
1113 bool migrate;
1114
1115 cpuset_change_task_nodemask(task, &newmems);
1116
1117 mm = get_task_mm(task);
1118 if (!mm)
1119 continue;
1120
1121 migrate = is_memory_migrate(cs);
1122
1123 mpol_rebind_mm(mm, &cs->mems_allowed);
1124 if (migrate)
1125 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
Tejun Heoe93ad192016-01-19 12:18:41 -05001126 else
1127 mmput(mm);
Tejun Heod66393e2014-02-13 06:58:40 -05001128 }
1129 css_task_iter_end(&it);
Paul Jackson42253992006-01-08 01:01:59 -08001130
Li Zefan33ad8012013-06-09 17:15:08 +08001131 /*
1132 * All the tasks' nodemasks have been updated, update
1133 * cs->old_mems_allowed.
1134 */
1135 cs->old_mems_allowed = newmems;
1136
Paul Menage2df167a2008-02-07 00:14:45 -08001137 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001138 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Miao Xie0b2f6302008-07-25 01:47:21 -07001141/*
Li Zefan734d4512014-07-09 16:47:29 +08001142 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1143 * @cs: the cpuset to consider
1144 * @new_mems: a temp variable for calculating new effective_mems
Li Zefan5c5cc622013-06-09 17:16:29 +08001145 *
Li Zefan734d4512014-07-09 16:47:29 +08001146 * When configured nodemask is changed, the effective nodemasks of this cpuset
1147 * and all its descendants need to be updated.
1148 *
1149 * On legacy hiearchy, effective_mems will be the same with mems_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +08001150 *
1151 * Called with cpuset_mutex held
1152 */
Li Zefan734d4512014-07-09 16:47:29 +08001153static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
Li Zefan5c5cc622013-06-09 17:16:29 +08001154{
1155 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -04001156 struct cgroup_subsys_state *pos_css;
Li Zefan5c5cc622013-06-09 17:16:29 +08001157
1158 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +08001159 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1160 struct cpuset *parent = parent_cs(cp);
1161
1162 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
1163
Li Zefan554b0d12014-07-09 16:47:41 +08001164 /*
1165 * If it becomes empty, inherit the effective mask of the
1166 * parent, which is guaranteed to have some MEMs.
1167 */
Tejun Heo9e10a132015-09-18 11:56:28 -04001168 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
1169 nodes_empty(*new_mems))
Li Zefan554b0d12014-07-09 16:47:41 +08001170 *new_mems = parent->effective_mems;
1171
Li Zefan734d4512014-07-09 16:47:29 +08001172 /* Skip the whole subtree if the nodemask remains the same. */
1173 if (nodes_equal(*new_mems, cp->effective_mems)) {
1174 pos_css = css_rightmost_descendant(pos_css);
1175 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +08001176 }
Li Zefan734d4512014-07-09 16:47:29 +08001177
Tejun Heoec903c02014-05-13 12:11:01 -04001178 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +08001179 continue;
1180 rcu_read_unlock();
1181
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001182 spin_lock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001183 cp->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001184 spin_unlock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001185
Tejun Heo9e10a132015-09-18 11:56:28 -04001186 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefana1381262014-07-30 15:07:13 +08001187 !nodes_equal(cp->mems_allowed, cp->effective_mems));
Li Zefan734d4512014-07-09 16:47:29 +08001188
Tejun Heod66393e2014-02-13 06:58:40 -05001189 update_tasks_nodemask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +08001190
1191 rcu_read_lock();
1192 css_put(&cp->css);
1193 }
1194 rcu_read_unlock();
1195}
1196
1197/*
Miao Xie0b2f6302008-07-25 01:47:21 -07001198 * Handle user request to change the 'mems' memory placement
1199 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001200 * cpusets mems_allowed, and for each task in the cpuset,
1201 * update mems_allowed and rebind task's mempolicy and any vma
1202 * mempolicies and if the cpuset is marked 'memory_migrate',
1203 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001204 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001205 * Call with cpuset_mutex held. May take callback_lock during call.
Miao Xie0b2f6302008-07-25 01:47:21 -07001206 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1207 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1208 * their mempolicies to the cpusets new mems_allowed.
1209 */
Li Zefan645fcc92009-01-07 18:08:43 -08001210static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1211 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001212{
Miao Xie0b2f6302008-07-25 01:47:21 -07001213 int retval;
1214
1215 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001216 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001217 * it's read-only
1218 */
Miao Xie53feb292010-03-23 13:35:35 -07001219 if (cs == &top_cpuset) {
1220 retval = -EACCES;
1221 goto done;
1222 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001223
Miao Xie0b2f6302008-07-25 01:47:21 -07001224 /*
1225 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1226 * Since nodelist_parse() fails on an empty mask, we special case
1227 * that parsing. The validate_change() call ensures that cpusets
1228 * with tasks have memory.
1229 */
1230 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001231 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001232 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001233 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001234 if (retval < 0)
1235 goto done;
1236
Li Zefan645fcc92009-01-07 18:08:43 -08001237 if (!nodes_subset(trialcs->mems_allowed,
Li Zefan5d8ba822014-07-09 16:49:12 +08001238 top_cpuset.mems_allowed)) {
1239 retval = -EINVAL;
Miao Xie53feb292010-03-23 13:35:35 -07001240 goto done;
1241 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001242 }
Li Zefan33ad8012013-06-09 17:15:08 +08001243
1244 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001245 retval = 0; /* Too easy - nothing to do */
1246 goto done;
1247 }
Li Zefan645fcc92009-01-07 18:08:43 -08001248 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001249 if (retval < 0)
1250 goto done;
1251
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001252 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001253 cs->mems_allowed = trialcs->mems_allowed;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001254 spin_unlock_irq(&callback_lock);
Miao Xie0b2f6302008-07-25 01:47:21 -07001255
Li Zefan734d4512014-07-09 16:47:29 +08001256 /* use trialcs->mems_allowed as a temp variable */
Alban Crequy24ee3cf2015-08-06 16:21:05 +02001257 update_nodemasks_hier(cs, &trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001258done:
1259 return retval;
1260}
1261
Paul Menage8793d852007-10-18 23:39:39 -07001262int current_cpuset_is_being_rebound(void)
1263{
Gu Zheng391acf92014-06-25 09:57:18 +08001264 int ret;
1265
1266 rcu_read_lock();
1267 ret = task_cs(current) == cpuset_being_rebound;
1268 rcu_read_unlock();
1269
1270 return ret;
Paul Menage8793d852007-10-18 23:39:39 -07001271}
1272
Paul Menage5be7a472008-05-06 20:42:41 -07001273static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001274{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001275#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001276 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001277 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001278#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001279
1280 if (val != cs->relax_domain_level) {
1281 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001282 if (!cpumask_empty(cs->cpus_allowed) &&
1283 is_sched_load_balance(cs))
Tejun Heo699140b2013-01-07 08:51:07 -08001284 rebuild_sched_domains_locked();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001285 }
1286
1287 return 0;
1288}
1289
Tejun Heo72ec7022013-08-08 20:11:26 -04001290/**
Miao Xie950592f2009-06-16 15:31:47 -07001291 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1292 * @cs: the cpuset in which each task's spread flags needs to be changed
Miao Xie950592f2009-06-16 15:31:47 -07001293 *
Tejun Heod66393e2014-02-13 06:58:40 -05001294 * Iterate through each task of @cs updating its spread flags. As this
1295 * function is called with cpuset_mutex held, cpuset membership stays
1296 * stable.
Miao Xie950592f2009-06-16 15:31:47 -07001297 */
Tejun Heod66393e2014-02-13 06:58:40 -05001298static void update_tasks_flags(struct cpuset *cs)
Miao Xie950592f2009-06-16 15:31:47 -07001299{
Tejun Heod66393e2014-02-13 06:58:40 -05001300 struct css_task_iter it;
1301 struct task_struct *task;
1302
1303 css_task_iter_start(&cs->css, &it);
1304 while ((task = css_task_iter_next(&it)))
1305 cpuset_update_task_spread_flag(cs, task);
1306 css_task_iter_end(&it);
Miao Xie950592f2009-06-16 15:31:47 -07001307}
1308
1309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001311 * bit: the bit to update (see cpuset_flagbits_t)
1312 * cs: the cpuset to update
1313 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001314 *
Tejun Heo5d21cc22013-01-07 08:51:08 -08001315 * Call with cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
1317
Paul Menage700fe1a2008-04-29 01:00:00 -07001318static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1319 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Li Zefan645fcc92009-01-07 18:08:43 -08001321 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001322 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001323 int spread_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001324 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Li Zefan645fcc92009-01-07 18:08:43 -08001326 trialcs = alloc_trial_cpuset(cs);
1327 if (!trialcs)
1328 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Li Zefan645fcc92009-01-07 18:08:43 -08001330 if (turning_on)
1331 set_bit(bit, &trialcs->flags);
1332 else
1333 clear_bit(bit, &trialcs->flags);
1334
1335 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001336 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001337 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001338
Paul Jackson029190c2007-10-18 23:40:20 -07001339 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001340 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001341
Miao Xie950592f2009-06-16 15:31:47 -07001342 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1343 || (is_spread_page(cs) != is_spread_page(trialcs)));
1344
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001345 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001346 cs->flags = trialcs->flags;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001347 spin_unlock_irq(&callback_lock);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001348
Li Zefan300ed6c2009-01-07 18:08:44 -08001349 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Tejun Heo699140b2013-01-07 08:51:07 -08001350 rebuild_sched_domains_locked();
Paul Jackson029190c2007-10-18 23:40:20 -07001351
Miao Xie950592f2009-06-16 15:31:47 -07001352 if (spread_flag_changed)
Tejun Heod66393e2014-02-13 06:58:40 -05001353 update_tasks_flags(cs);
Li Zefan645fcc92009-01-07 18:08:43 -08001354out:
1355 free_trial_cpuset(trialcs);
1356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Paul Jackson053199e2005-10-30 15:02:30 -08001359/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001360 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001361 *
1362 * These routines manage a digitally filtered, constant time based,
1363 * event frequency meter. There are four routines:
1364 * fmeter_init() - initialize a frequency meter.
1365 * fmeter_markevent() - called each time the event happens.
1366 * fmeter_getrate() - returns the recent rate of such events.
1367 * fmeter_update() - internal routine used to update fmeter.
1368 *
1369 * A common data structure is passed to each of these routines,
1370 * which is used to keep track of the state required to manage the
1371 * frequency meter and its digital filter.
1372 *
1373 * The filter works on the number of events marked per unit time.
1374 * The filter is single-pole low-pass recursive (IIR). The time unit
1375 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1376 * simulate 3 decimal digits of precision (multiplied by 1000).
1377 *
1378 * With an FM_COEF of 933, and a time base of 1 second, the filter
1379 * has a half-life of 10 seconds, meaning that if the events quit
1380 * happening, then the rate returned from the fmeter_getrate()
1381 * will be cut in half each 10 seconds, until it converges to zero.
1382 *
1383 * It is not worth doing a real infinitely recursive filter. If more
1384 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1385 * just compute FM_MAXTICKS ticks worth, by which point the level
1386 * will be stable.
1387 *
1388 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1389 * arithmetic overflow in the fmeter_update() routine.
1390 *
1391 * Given the simple 32 bit integer arithmetic used, this meter works
1392 * best for reporting rates between one per millisecond (msec) and
1393 * one per 32 (approx) seconds. At constant rates faster than one
1394 * per msec it maxes out at values just under 1,000,000. At constant
1395 * rates between one per msec, and one per second it will stabilize
1396 * to a value N*1000, where N is the rate of events per second.
1397 * At constant rates between one per second and one per 32 seconds,
1398 * it will be choppy, moving up on the seconds that have an event,
1399 * and then decaying until the next event. At rates slower than
1400 * about one in 32 seconds, it decays all the way back to zero between
1401 * each event.
1402 */
1403
1404#define FM_COEF 933 /* coefficient for half-life of 10 secs */
Arnd Bergmannd2b436582015-11-25 16:16:55 +01001405#define FM_MAXTICKS ((u32)99) /* useless computing more ticks than this */
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001406#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1407#define FM_SCALE 1000 /* faux fixed point scale */
1408
1409/* Initialize a frequency meter */
1410static void fmeter_init(struct fmeter *fmp)
1411{
1412 fmp->cnt = 0;
1413 fmp->val = 0;
1414 fmp->time = 0;
1415 spin_lock_init(&fmp->lock);
1416}
1417
1418/* Internal meter update - process cnt events and update value */
1419static void fmeter_update(struct fmeter *fmp)
1420{
Arnd Bergmannd2b436582015-11-25 16:16:55 +01001421 time64_t now;
1422 u32 ticks;
1423
1424 now = ktime_get_seconds();
1425 ticks = now - fmp->time;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001426
1427 if (ticks == 0)
1428 return;
1429
1430 ticks = min(FM_MAXTICKS, ticks);
1431 while (ticks-- > 0)
1432 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1433 fmp->time = now;
1434
1435 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1436 fmp->cnt = 0;
1437}
1438
1439/* Process any previous ticks, then bump cnt by one (times scale). */
1440static void fmeter_markevent(struct fmeter *fmp)
1441{
1442 spin_lock(&fmp->lock);
1443 fmeter_update(fmp);
1444 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1445 spin_unlock(&fmp->lock);
1446}
1447
1448/* Process any previous ticks, then return current value. */
1449static int fmeter_getrate(struct fmeter *fmp)
1450{
1451 int val;
1452
1453 spin_lock(&fmp->lock);
1454 fmeter_update(fmp);
1455 val = fmp->val;
1456 spin_unlock(&fmp->lock);
1457 return val;
1458}
1459
Tejun Heo57fce0a2014-02-13 06:58:41 -05001460static struct cpuset *cpuset_attach_old_cs;
1461
Tejun Heo5d21cc22013-01-07 08:51:08 -08001462/* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001463static int cpuset_can_attach(struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07001464{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001465 struct cgroup_subsys_state *css;
1466 struct cpuset *cs;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001467 struct task_struct *task;
1468 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07001469
Tejun Heo57fce0a2014-02-13 06:58:41 -05001470 /* used later by cpuset_attach() */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001471 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
1472 cs = css_cs(css);
Tejun Heo57fce0a2014-02-13 06:58:41 -05001473
Tejun Heo5d21cc22013-01-07 08:51:08 -08001474 mutex_lock(&cpuset_mutex);
1475
Tejun Heoaa6ec292014-07-09 10:08:08 -04001476 /* allow moving tasks into an empty cpuset if on default hierarchy */
Tejun Heo5d21cc22013-01-07 08:51:08 -08001477 ret = -ENOSPC;
Tejun Heo9e10a132015-09-18 11:56:28 -04001478 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefan88fa5232013-06-09 17:16:46 +08001479 (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)))
Tejun Heo5d21cc22013-01-07 08:51:08 -08001480 goto out_unlock;
Ben Blumbe367d02009-09-23 15:56:31 -07001481
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001482 cgroup_taskset_for_each(task, css, tset) {
Juri Lelli7f514122014-09-19 10:22:40 +01001483 ret = task_can_attach(task, cs->cpus_allowed);
1484 if (ret)
Tejun Heo5d21cc22013-01-07 08:51:08 -08001485 goto out_unlock;
1486 ret = security_task_setscheduler(task);
1487 if (ret)
1488 goto out_unlock;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Tejun Heo452477f2013-01-07 08:51:07 -08001491 /*
1492 * Mark attach is in progress. This makes validate_change() fail
1493 * changes which zero cpus/mems_allowed.
1494 */
1495 cs->attach_in_progress++;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001496 ret = 0;
1497out_unlock:
1498 mutex_unlock(&cpuset_mutex);
1499 return ret;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001500}
1501
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001502static void cpuset_cancel_attach(struct cgroup_taskset *tset)
Tejun Heo452477f2013-01-07 08:51:07 -08001503{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001504 struct cgroup_subsys_state *css;
1505 struct cpuset *cs;
1506
1507 cgroup_taskset_first(tset, &css);
1508 cs = css_cs(css);
1509
Tejun Heo5d21cc22013-01-07 08:51:08 -08001510 mutex_lock(&cpuset_mutex);
Tejun Heoeb954192013-08-08 20:11:23 -04001511 css_cs(css)->attach_in_progress--;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001512 mutex_unlock(&cpuset_mutex);
Tejun Heo452477f2013-01-07 08:51:07 -08001513}
1514
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001515/*
Tejun Heo5d21cc22013-01-07 08:51:08 -08001516 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001517 * but we can't allocate it dynamically there. Define it global and
1518 * allocate from cpuset_init().
1519 */
1520static cpumask_var_t cpus_attach;
1521
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001522static void cpuset_attach(struct cgroup_taskset *tset)
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001523{
Li Zefan67bd2c52013-06-05 17:15:35 +08001524 /* static buf protected by cpuset_mutex */
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001525 static nodemask_t cpuset_attach_nodemask_to;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001526 struct task_struct *task;
Tejun Heo4530edd2015-09-11 15:00:19 -04001527 struct task_struct *leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001528 struct cgroup_subsys_state *css;
1529 struct cpuset *cs;
Tejun Heo57fce0a2014-02-13 06:58:41 -05001530 struct cpuset *oldcs = cpuset_attach_old_cs;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001531
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001532 cgroup_taskset_first(tset, &css);
1533 cs = css_cs(css);
1534
Tejun Heo5d21cc22013-01-07 08:51:08 -08001535 mutex_lock(&cpuset_mutex);
1536
Tejun Heo94196f52011-12-12 18:12:22 -08001537 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07001538 if (cs == &top_cpuset)
1539 cpumask_copy(cpus_attach, cpu_possible_mask);
1540 else
Li Zefanae1c8022014-07-09 16:48:32 +08001541 guarantee_online_cpus(cs, cpus_attach);
Ben Blumf780bdb2011-05-26 16:25:19 -07001542
Li Zefanae1c8022014-07-09 16:48:32 +08001543 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08001544
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001545 cgroup_taskset_for_each(task, css, tset) {
Tejun Heobb9d97b2011-12-12 18:12:21 -08001546 /*
1547 * can_attach beforehand should guarantee that this doesn't
1548 * fail. TODO: have a better way to handle failure here
1549 */
1550 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1551
1552 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1553 cpuset_update_task_spread_flag(cs, task);
1554 }
David Quigley22fb52d2006-06-23 02:04:00 -07001555
Ben Blumf780bdb2011-05-26 16:25:19 -07001556 /*
Tejun Heo4530edd2015-09-11 15:00:19 -04001557 * Change mm for all threadgroup leaders. This is expensive and may
1558 * sleep and should be moved outside migration path proper.
Ben Blumf780bdb2011-05-26 16:25:19 -07001559 */
Li Zefanae1c8022014-07-09 16:48:32 +08001560 cpuset_attach_nodemask_to = cs->effective_mems;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001561 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo3df9ca02015-09-11 15:00:18 -04001562 struct mm_struct *mm = get_task_mm(leader);
Li Zefanf047cec2013-06-13 15:11:44 +08001563
Tejun Heo3df9ca02015-09-11 15:00:18 -04001564 if (mm) {
1565 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
1566
1567 /*
1568 * old_mems_allowed is the same with mems_allowed
1569 * here, except if this task is being moved
1570 * automatically due to hotplug. In that case
1571 * @mems_allowed has been updated and is empty, so
1572 * @old_mems_allowed is the right nodesets that we
1573 * migrate mm from.
1574 */
Tejun Heoe93ad192016-01-19 12:18:41 -05001575 if (is_memory_migrate(cs))
Tejun Heo3df9ca02015-09-11 15:00:18 -04001576 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed,
1577 &cpuset_attach_nodemask_to);
Tejun Heoe93ad192016-01-19 12:18:41 -05001578 else
1579 mmput(mm);
Li Zefanf047cec2013-06-13 15:11:44 +08001580 }
Paul Jackson42253992006-01-08 01:01:59 -08001581 }
Tejun Heo452477f2013-01-07 08:51:07 -08001582
Li Zefan33ad8012013-06-09 17:15:08 +08001583 cs->old_mems_allowed = cpuset_attach_nodemask_to;
Tejun Heo02bb5862013-01-07 08:51:08 -08001584
Tejun Heo452477f2013-01-07 08:51:07 -08001585 cs->attach_in_progress--;
Li Zefane44193d2013-06-09 17:14:22 +08001586 if (!cs->attach_in_progress)
1587 wake_up(&cpuset_attach_wq);
Tejun Heo5d21cc22013-01-07 08:51:08 -08001588
1589 mutex_unlock(&cpuset_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592/* The various types of files and directories in a cpuset file system */
1593
1594typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001595 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 FILE_CPULIST,
1597 FILE_MEMLIST,
Li Zefanafd1a8b2014-07-09 16:49:25 +08001598 FILE_EFFECTIVE_CPULIST,
1599 FILE_EFFECTIVE_MEMLIST,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 FILE_CPU_EXCLUSIVE,
1601 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001602 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001603 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001604 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001605 FILE_MEMORY_PRESSURE_ENABLED,
1606 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001607 FILE_SPREAD_PAGE,
1608 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609} cpuset_filetype_t;
1610
Tejun Heo182446d2013-08-08 20:11:24 -04001611static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
1612 u64 val)
Paul Menage700fe1a2008-04-29 01:00:00 -07001613{
Tejun Heo182446d2013-08-08 20:11:24 -04001614 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07001615 cpuset_filetype_t type = cft->private;
Li Zefana903f082013-08-13 10:05:59 +08001616 int retval = 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001617
Tejun Heo5d21cc22013-01-07 08:51:08 -08001618 mutex_lock(&cpuset_mutex);
Li Zefana903f082013-08-13 10:05:59 +08001619 if (!is_cpuset_online(cs)) {
1620 retval = -ENODEV;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001621 goto out_unlock;
Li Zefana903f082013-08-13 10:05:59 +08001622 }
Paul Menage700fe1a2008-04-29 01:00:00 -07001623
1624 switch (type) {
1625 case FILE_CPU_EXCLUSIVE:
1626 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1627 break;
1628 case FILE_MEM_EXCLUSIVE:
1629 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1630 break;
Paul Menage78608362008-04-29 01:00:26 -07001631 case FILE_MEM_HARDWALL:
1632 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1633 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001634 case FILE_SCHED_LOAD_BALANCE:
1635 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1636 break;
1637 case FILE_MEMORY_MIGRATE:
1638 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1639 break;
1640 case FILE_MEMORY_PRESSURE_ENABLED:
1641 cpuset_memory_pressure_enabled = !!val;
1642 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001643 case FILE_SPREAD_PAGE:
1644 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001645 break;
1646 case FILE_SPREAD_SLAB:
1647 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001648 break;
1649 default:
1650 retval = -EINVAL;
1651 break;
1652 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08001653out_unlock:
1654 mutex_unlock(&cpuset_mutex);
Paul Menage700fe1a2008-04-29 01:00:00 -07001655 return retval;
1656}
1657
Tejun Heo182446d2013-08-08 20:11:24 -04001658static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
1659 s64 val)
Paul Menage5be7a472008-05-06 20:42:41 -07001660{
Tejun Heo182446d2013-08-08 20:11:24 -04001661 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07001662 cpuset_filetype_t type = cft->private;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001663 int retval = -ENODEV;
Paul Menage5be7a472008-05-06 20:42:41 -07001664
Tejun Heo5d21cc22013-01-07 08:51:08 -08001665 mutex_lock(&cpuset_mutex);
1666 if (!is_cpuset_online(cs))
1667 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07001668
Paul Menage5be7a472008-05-06 20:42:41 -07001669 switch (type) {
1670 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1671 retval = update_relax_domain_level(cs, val);
1672 break;
1673 default:
1674 retval = -EINVAL;
1675 break;
1676 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08001677out_unlock:
1678 mutex_unlock(&cpuset_mutex);
Paul Menage5be7a472008-05-06 20:42:41 -07001679 return retval;
1680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/*
Paul Menagee3712392008-07-25 01:47:02 -07001683 * Common handling for a write to a "cpus" or "mems" file.
1684 */
Tejun Heo451af502014-05-13 12:16:21 -04001685static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
1686 char *buf, size_t nbytes, loff_t off)
Paul Menagee3712392008-07-25 01:47:02 -07001687{
Tejun Heo451af502014-05-13 12:16:21 -04001688 struct cpuset *cs = css_cs(of_css(of));
Li Zefan645fcc92009-01-07 18:08:43 -08001689 struct cpuset *trialcs;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001690 int retval = -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001691
Tejun Heo451af502014-05-13 12:16:21 -04001692 buf = strstrip(buf);
1693
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001694 /*
1695 * CPU or memory hotunplug may leave @cs w/o any execution
1696 * resources, in which case the hotplug code asynchronously updates
1697 * configuration and transfers all tasks to the nearest ancestor
1698 * which can execute.
1699 *
1700 * As writes to "cpus" or "mems" may restore @cs's execution
1701 * resources, wait for the previously scheduled operations before
1702 * proceeding, so that we don't end up keep removing tasks added
1703 * after execution capability is restored.
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001704 *
1705 * cpuset_hotplug_work calls back into cgroup core via
1706 * cgroup_transfer_tasks() and waiting for it from a cgroupfs
1707 * operation like this one can lead to a deadlock through kernfs
1708 * active_ref protection. Let's break the protection. Losing the
1709 * protection is okay as we check whether @cs is online after
1710 * grabbing cpuset_mutex anyway. This only happens on the legacy
1711 * hierarchies.
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001712 */
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001713 css_get(&cs->css);
1714 kernfs_break_active_protection(of->kn);
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001715 flush_work(&cpuset_hotplug_work);
1716
Tejun Heo5d21cc22013-01-07 08:51:08 -08001717 mutex_lock(&cpuset_mutex);
1718 if (!is_cpuset_online(cs))
1719 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07001720
Li Zefan645fcc92009-01-07 18:08:43 -08001721 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001722 if (!trialcs) {
1723 retval = -ENOMEM;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001724 goto out_unlock;
Li Zefanb75f38d2011-03-04 17:36:21 -08001725 }
Li Zefan645fcc92009-01-07 18:08:43 -08001726
Tejun Heo451af502014-05-13 12:16:21 -04001727 switch (of_cft(of)->private) {
Paul Menagee3712392008-07-25 01:47:02 -07001728 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001729 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001730 break;
1731 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001732 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001733 break;
1734 default:
1735 retval = -EINVAL;
1736 break;
1737 }
Li Zefan645fcc92009-01-07 18:08:43 -08001738
1739 free_trial_cpuset(trialcs);
Tejun Heo5d21cc22013-01-07 08:51:08 -08001740out_unlock:
1741 mutex_unlock(&cpuset_mutex);
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001742 kernfs_unbreak_active_protection(of->kn);
1743 css_put(&cs->css);
Tejun Heoe93ad192016-01-19 12:18:41 -05001744 flush_workqueue(cpuset_migrate_mm_wq);
Tejun Heo451af502014-05-13 12:16:21 -04001745 return retval ?: nbytes;
Paul Menagee3712392008-07-25 01:47:02 -07001746}
1747
1748/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * These ascii lists should be read in a single call, by using a user
1750 * buffer large enough to hold the entire map. If read in smaller
1751 * chunks, there is no guarantee of atomicity. Since the display format
1752 * used, list of ranges of sequential numbers, is variable length,
1753 * and since these maps can change value dynamically, one could read
1754 * gibberish by doing partial reads while a list was changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001756static int cpuset_common_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001758 struct cpuset *cs = css_cs(seq_css(sf));
1759 cpuset_filetype_t type = seq_cft(sf)->private;
Tejun Heo51ffe412013-12-05 12:28:02 -05001760 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001762 spin_lock_irq(&callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 switch (type) {
1765 case FILE_CPULIST:
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07001766 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_requested));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 break;
1768 case FILE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001769 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 break;
Li Zefanafd1a8b2014-07-09 16:49:25 +08001771 case FILE_EFFECTIVE_CPULIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001772 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus));
Li Zefanafd1a8b2014-07-09 16:49:25 +08001773 break;
1774 case FILE_EFFECTIVE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001775 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems));
Li Zefanafd1a8b2014-07-09 16:49:25 +08001776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 default:
Tejun Heo51ffe412013-12-05 12:28:02 -05001778 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001781 spin_unlock_irq(&callback_lock);
Tejun Heo51ffe412013-12-05 12:28:02 -05001782 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Tejun Heo182446d2013-08-08 20:11:24 -04001785static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage700fe1a2008-04-29 01:00:00 -07001786{
Tejun Heo182446d2013-08-08 20:11:24 -04001787 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07001788 cpuset_filetype_t type = cft->private;
1789 switch (type) {
1790 case FILE_CPU_EXCLUSIVE:
1791 return is_cpu_exclusive(cs);
1792 case FILE_MEM_EXCLUSIVE:
1793 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001794 case FILE_MEM_HARDWALL:
1795 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001796 case FILE_SCHED_LOAD_BALANCE:
1797 return is_sched_load_balance(cs);
1798 case FILE_MEMORY_MIGRATE:
1799 return is_memory_migrate(cs);
1800 case FILE_MEMORY_PRESSURE_ENABLED:
1801 return cpuset_memory_pressure_enabled;
1802 case FILE_MEMORY_PRESSURE:
1803 return fmeter_getrate(&cs->fmeter);
1804 case FILE_SPREAD_PAGE:
1805 return is_spread_page(cs);
1806 case FILE_SPREAD_SLAB:
1807 return is_spread_slab(cs);
1808 default:
1809 BUG();
1810 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001811
1812 /* Unreachable but makes gcc happy */
1813 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001814}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Tejun Heo182446d2013-08-08 20:11:24 -04001816static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage5be7a472008-05-06 20:42:41 -07001817{
Tejun Heo182446d2013-08-08 20:11:24 -04001818 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07001819 cpuset_filetype_t type = cft->private;
1820 switch (type) {
1821 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1822 return cs->relax_domain_level;
1823 default:
1824 BUG();
1825 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001826
1827 /* Unrechable but makes gcc happy */
1828 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832/*
1833 * for the common functions, 'private' gives the type of file
1834 */
1835
Paul Menageaddf2c72008-04-29 01:00:26 -07001836static struct cftype files[] = {
1837 {
1838 .name = "cpus",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001839 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04001840 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07001841 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001842 .private = FILE_CPULIST,
1843 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Paul Menageaddf2c72008-04-29 01:00:26 -07001845 {
1846 .name = "mems",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001847 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04001848 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07001849 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001850 .private = FILE_MEMLIST,
1851 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Paul Menageaddf2c72008-04-29 01:00:26 -07001853 {
Li Zefanafd1a8b2014-07-09 16:49:25 +08001854 .name = "effective_cpus",
1855 .seq_show = cpuset_common_seq_show,
1856 .private = FILE_EFFECTIVE_CPULIST,
1857 },
1858
1859 {
1860 .name = "effective_mems",
1861 .seq_show = cpuset_common_seq_show,
1862 .private = FILE_EFFECTIVE_MEMLIST,
1863 },
1864
1865 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001866 .name = "cpu_exclusive",
1867 .read_u64 = cpuset_read_u64,
1868 .write_u64 = cpuset_write_u64,
1869 .private = FILE_CPU_EXCLUSIVE,
1870 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Paul Menageaddf2c72008-04-29 01:00:26 -07001872 {
1873 .name = "mem_exclusive",
1874 .read_u64 = cpuset_read_u64,
1875 .write_u64 = cpuset_write_u64,
1876 .private = FILE_MEM_EXCLUSIVE,
1877 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Paul Menageaddf2c72008-04-29 01:00:26 -07001879 {
Paul Menage78608362008-04-29 01:00:26 -07001880 .name = "mem_hardwall",
1881 .read_u64 = cpuset_read_u64,
1882 .write_u64 = cpuset_write_u64,
1883 .private = FILE_MEM_HARDWALL,
1884 },
1885
1886 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001887 .name = "sched_load_balance",
1888 .read_u64 = cpuset_read_u64,
1889 .write_u64 = cpuset_write_u64,
1890 .private = FILE_SCHED_LOAD_BALANCE,
1891 },
Paul Jackson029190c2007-10-18 23:40:20 -07001892
Paul Menageaddf2c72008-04-29 01:00:26 -07001893 {
1894 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001895 .read_s64 = cpuset_read_s64,
1896 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001897 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1898 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001899
Paul Menageaddf2c72008-04-29 01:00:26 -07001900 {
1901 .name = "memory_migrate",
1902 .read_u64 = cpuset_read_u64,
1903 .write_u64 = cpuset_write_u64,
1904 .private = FILE_MEMORY_MIGRATE,
1905 },
1906
1907 {
1908 .name = "memory_pressure",
1909 .read_u64 = cpuset_read_u64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001910 },
1911
1912 {
1913 .name = "memory_spread_page",
1914 .read_u64 = cpuset_read_u64,
1915 .write_u64 = cpuset_write_u64,
1916 .private = FILE_SPREAD_PAGE,
1917 },
1918
1919 {
1920 .name = "memory_spread_slab",
1921 .read_u64 = cpuset_read_u64,
1922 .write_u64 = cpuset_write_u64,
1923 .private = FILE_SPREAD_SLAB,
1924 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07001925
1926 {
1927 .name = "memory_pressure_enabled",
1928 .flags = CFTYPE_ONLY_ON_ROOT,
1929 .read_u64 = cpuset_read_u64,
1930 .write_u64 = cpuset_write_u64,
1931 .private = FILE_MEMORY_PRESSURE_ENABLED,
1932 },
1933
1934 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001935};
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/*
Tejun Heo92fb9742012-11-19 08:13:38 -08001938 * cpuset_css_alloc - allocate a cpuset css
Li Zefanc9e5fe62013-06-14 11:18:27 +08001939 * cgrp: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 */
1941
Tejun Heoeb954192013-08-08 20:11:23 -04001942static struct cgroup_subsys_state *
1943cpuset_css_alloc(struct cgroup_subsys_state *parent_css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Tejun Heoc8f699b2013-01-07 08:51:07 -08001945 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Tejun Heoeb954192013-08-08 20:11:23 -04001947 if (!parent_css)
Paul Menage8793d852007-10-18 23:39:39 -07001948 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001949
Tejun Heoc8f699b2013-01-07 08:51:07 -08001950 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001952 return ERR_PTR(-ENOMEM);
Li Zefane2b9a3d2014-07-09 16:47:03 +08001953 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL))
1954 goto free_cs;
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07001955 if (!alloc_cpumask_var(&cs->cpus_requested, GFP_KERNEL))
1956 goto free_allowed;
Li Zefane2b9a3d2014-07-09 16:47:03 +08001957 if (!alloc_cpumask_var(&cs->effective_cpus, GFP_KERNEL))
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07001958 goto free_requested;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Paul Jackson029190c2007-10-18 23:40:20 -07001960 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001961 cpumask_clear(cs->cpus_allowed);
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07001962 cpumask_clear(cs->cpus_requested);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001963 nodes_clear(cs->mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08001964 cpumask_clear(cs->effective_cpus);
1965 nodes_clear(cs->effective_mems);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001966 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001967 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Tejun Heoc8f699b2013-01-07 08:51:07 -08001969 return &cs->css;
Li Zefane2b9a3d2014-07-09 16:47:03 +08001970
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07001971free_requested:
1972 free_cpumask_var(cs->cpus_requested);
1973free_allowed:
Li Zefane2b9a3d2014-07-09 16:47:03 +08001974 free_cpumask_var(cs->cpus_allowed);
1975free_cs:
1976 kfree(cs);
1977 return ERR_PTR(-ENOMEM);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001978}
1979
Tejun Heoeb954192013-08-08 20:11:23 -04001980static int cpuset_css_online(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08001981{
Tejun Heoeb954192013-08-08 20:11:23 -04001982 struct cpuset *cs = css_cs(css);
Tejun Heoc4310692013-01-07 08:51:08 -08001983 struct cpuset *parent = parent_cs(cs);
Tejun Heoae8086c2013-01-07 08:51:07 -08001984 struct cpuset *tmp_cs;
Tejun Heo492eb212013-08-08 20:11:25 -04001985 struct cgroup_subsys_state *pos_css;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001986
1987 if (!parent)
1988 return 0;
1989
Tejun Heo5d21cc22013-01-07 08:51:08 -08001990 mutex_lock(&cpuset_mutex);
1991
Tejun Heoefeb77b2013-01-07 08:51:07 -08001992 set_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001993 if (is_spread_page(parent))
1994 set_bit(CS_SPREAD_PAGE, &cs->flags);
1995 if (is_spread_slab(parent))
1996 set_bit(CS_SPREAD_SLAB, &cs->flags);
1997
Mel Gorman664eedd2014-06-04 16:10:08 -07001998 cpuset_inc();
Tejun Heo033fa1c2012-11-19 08:13:39 -08001999
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002000 spin_lock_irq(&callback_lock);
Tejun Heo9e10a132015-09-18 11:56:28 -04002001 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
Li Zefane2b9a3d2014-07-09 16:47:03 +08002002 cpumask_copy(cs->effective_cpus, parent->effective_cpus);
2003 cs->effective_mems = parent->effective_mems;
2004 }
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002005 spin_unlock_irq(&callback_lock);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002006
Tejun Heoeb954192013-08-08 20:11:23 -04002007 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
Tejun Heo5d21cc22013-01-07 08:51:08 -08002008 goto out_unlock;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002009
2010 /*
2011 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
2012 * set. This flag handling is implemented in cgroup core for
2013 * histrical reasons - the flag may be specified during mount.
2014 *
2015 * Currently, if any sibling cpusets have exclusive cpus or mem, we
2016 * refuse to clone the configuration - thereby refusing the task to
2017 * be entered, and as a result refusing the sys_unshare() or
2018 * clone() which initiated it. If this becomes a problem for some
2019 * users who wish to allow that scenario, then this could be
2020 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2021 * (and likewise for mems) to the new cgroup.
2022 */
Tejun Heoae8086c2013-01-07 08:51:07 -08002023 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002024 cpuset_for_each_child(tmp_cs, pos_css, parent) {
Tejun Heoae8086c2013-01-07 08:51:07 -08002025 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
2026 rcu_read_unlock();
Tejun Heo5d21cc22013-01-07 08:51:08 -08002027 goto out_unlock;
Tejun Heoae8086c2013-01-07 08:51:07 -08002028 }
Tejun Heo033fa1c2012-11-19 08:13:39 -08002029 }
Tejun Heoae8086c2013-01-07 08:51:07 -08002030 rcu_read_unlock();
Tejun Heo033fa1c2012-11-19 08:13:39 -08002031
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002032 spin_lock_irq(&callback_lock);
Tejun Heo033fa1c2012-11-19 08:13:39 -08002033 cs->mems_allowed = parent->mems_allowed;
Zefan Li790317e2015-02-13 11:19:49 +08002034 cs->effective_mems = parent->mems_allowed;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002035 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07002036 cpumask_copy(cs->cpus_requested, parent->cpus_requested);
Zefan Li790317e2015-02-13 11:19:49 +08002037 cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
Dan Carpentercea74462014-10-27 16:27:02 +03002038 spin_unlock_irq(&callback_lock);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002039out_unlock:
2040 mutex_unlock(&cpuset_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002041 return 0;
2042}
2043
Zhao Hongjiang0b9e6962013-07-27 11:56:53 +08002044/*
2045 * If the cpuset being removed has its flag 'sched_load_balance'
2046 * enabled, then simulate turning sched_load_balance off, which
2047 * will call rebuild_sched_domains_locked().
2048 */
2049
Tejun Heoeb954192013-08-08 20:11:23 -04002050static void cpuset_css_offline(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08002051{
Tejun Heoeb954192013-08-08 20:11:23 -04002052 struct cpuset *cs = css_cs(css);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002053
Tejun Heo5d21cc22013-01-07 08:51:08 -08002054 mutex_lock(&cpuset_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002055
2056 if (is_sched_load_balance(cs))
2057 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
2058
Mel Gorman664eedd2014-06-04 16:10:08 -07002059 cpuset_dec();
Tejun Heoefeb77b2013-01-07 08:51:07 -08002060 clear_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002061
Tejun Heo5d21cc22013-01-07 08:51:08 -08002062 mutex_unlock(&cpuset_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Tejun Heoeb954192013-08-08 20:11:23 -04002065static void cpuset_css_free(struct cgroup_subsys_state *css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Tejun Heoeb954192013-08-08 20:11:23 -04002067 struct cpuset *cs = css_cs(css);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Li Zefane2b9a3d2014-07-09 16:47:03 +08002069 free_cpumask_var(cs->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -08002070 free_cpumask_var(cs->cpus_allowed);
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07002071 free_cpumask_var(cs->cpus_requested);
Paul Menage8793d852007-10-18 23:39:39 -07002072 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Li Zefan39bd0d12014-07-09 16:48:01 +08002075static void cpuset_bind(struct cgroup_subsys_state *root_css)
2076{
2077 mutex_lock(&cpuset_mutex);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002078 spin_lock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002079
Tejun Heo9e10a132015-09-18 11:56:28 -04002080 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
Li Zefan39bd0d12014-07-09 16:48:01 +08002081 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
2082 top_cpuset.mems_allowed = node_possible_map;
2083 } else {
2084 cpumask_copy(top_cpuset.cpus_allowed,
2085 top_cpuset.effective_cpus);
2086 top_cpuset.mems_allowed = top_cpuset.effective_mems;
2087 }
2088
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002089 spin_unlock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002090 mutex_unlock(&cpuset_mutex);
2091}
2092
Zefan Li06f4e9482016-08-09 11:25:01 +08002093/*
2094 * Make sure the new task conform to the current state of its parent,
2095 * which could have been changed by cpuset just after it inherits the
2096 * state from the parent and before it sits on the cgroup's task list.
2097 */
Wei Yongjun8a15b812016-09-16 13:02:37 +00002098static void cpuset_fork(struct task_struct *task)
Zefan Li06f4e9482016-08-09 11:25:01 +08002099{
2100 if (task_css_is_root(task, cpuset_cgrp_id))
2101 return;
2102
2103 set_cpus_allowed_ptr(task, &current->cpus_allowed);
2104 task->mems_allowed = current->mems_allowed;
2105}
2106
Tejun Heo073219e2014-02-08 10:36:58 -05002107struct cgroup_subsys cpuset_cgrp_subsys = {
Li Zefan39bd0d12014-07-09 16:48:01 +08002108 .css_alloc = cpuset_css_alloc,
2109 .css_online = cpuset_css_online,
2110 .css_offline = cpuset_css_offline,
2111 .css_free = cpuset_css_free,
2112 .can_attach = cpuset_can_attach,
2113 .cancel_attach = cpuset_cancel_attach,
2114 .attach = cpuset_attach,
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002115 .post_attach = cpuset_post_attach,
Li Zefan39bd0d12014-07-09 16:48:01 +08002116 .bind = cpuset_bind,
Zefan Li06f4e9482016-08-09 11:25:01 +08002117 .fork = cpuset_fork,
Tejun Heo55779642014-07-15 11:05:09 -04002118 .legacy_cftypes = files,
Tejun Heob38e42e2016-02-23 10:00:50 -05002119 .early_init = true,
Paul Menage8793d852007-10-18 23:39:39 -07002120};
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122/**
2123 * cpuset_init - initialize cpusets at system boot
2124 *
2125 * Description: Initialize top_cpuset and the cpuset internal file system,
2126 **/
2127
2128int __init cpuset_init(void)
2129{
Paul Menage8793d852007-10-18 23:39:39 -07002130 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Miao Xie58568d22009-06-16 15:31:49 -07002132 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
2133 BUG();
Li Zefane2b9a3d2014-07-09 16:47:03 +08002134 if (!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL))
2135 BUG();
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07002136 if (!alloc_cpumask_var(&top_cpuset.cpus_requested, GFP_KERNEL))
2137 BUG();
Miao Xie58568d22009-06-16 15:31:49 -07002138
Li Zefan300ed6c2009-01-07 18:08:44 -08002139 cpumask_setall(top_cpuset.cpus_allowed);
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07002140 cpumask_setall(top_cpuset.cpus_requested);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002141 nodes_setall(top_cpuset.mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002142 cpumask_setall(top_cpuset.effective_cpus);
2143 nodes_setall(top_cpuset.effective_mems);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002145 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07002146 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002147 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 err = register_filesystem(&cpuset_fs_type);
2150 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07002151 return err;
2152
Li Zefan2341d1b2009-01-07 18:08:42 -08002153 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
2154 BUG();
2155
Paul Menage8793d852007-10-18 23:39:39 -07002156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002159/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002160 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002161 * or memory nodes, we need to walk over the cpuset hierarchy,
2162 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002163 * last CPU or node from a cpuset, then move the tasks in the empty
2164 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002165 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002166static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002167{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002168 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002169
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002170 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08002171 * Find its next-highest non-empty parent, (top cpuset
2172 * has online cpus, so can't be empty).
2173 */
Tejun Heoc4310692013-01-07 08:51:08 -08002174 parent = parent_cs(cs);
Li Zefan300ed6c2009-01-07 18:08:44 -08002175 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002176 nodes_empty(parent->mems_allowed))
Tejun Heoc4310692013-01-07 08:51:08 -08002177 parent = parent_cs(parent);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002178
Tejun Heo8cc99342013-04-07 09:29:50 -07002179 if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) {
Fabian Frederick12d30892014-05-05 19:49:00 +02002180 pr_err("cpuset: failed to transfer tasks out of empty cpuset ");
Tejun Heoe61734c2014-02-12 09:29:50 -05002181 pr_cont_cgroup_name(cs->css.cgroup);
2182 pr_cont("\n");
Tejun Heo8cc99342013-04-07 09:29:50 -07002183 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002184}
2185
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002186static void
2187hotplug_update_tasks_legacy(struct cpuset *cs,
2188 struct cpumask *new_cpus, nodemask_t *new_mems,
2189 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002190{
2191 bool is_empty;
2192
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002193 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002194 cpumask_copy(cs->cpus_allowed, new_cpus);
2195 cpumask_copy(cs->effective_cpus, new_cpus);
2196 cs->mems_allowed = *new_mems;
2197 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002198 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08002199
2200 /*
2201 * Don't call update_tasks_cpumask() if the cpuset becomes empty,
2202 * as the tasks will be migratecd to an ancestor.
2203 */
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002204 if (cpus_updated && !cpumask_empty(cs->cpus_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08002205 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002206 if (mems_updated && !nodes_empty(cs->mems_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08002207 update_tasks_nodemask(cs);
2208
2209 is_empty = cpumask_empty(cs->cpus_allowed) ||
2210 nodes_empty(cs->mems_allowed);
2211
2212 mutex_unlock(&cpuset_mutex);
2213
2214 /*
2215 * Move tasks to the nearest ancestor with execution resources,
2216 * This is full cgroup operation which will also call back into
2217 * cpuset. Should be done outside any lock.
2218 */
2219 if (is_empty)
2220 remove_tasks_in_empty_cpuset(cs);
2221
2222 mutex_lock(&cpuset_mutex);
2223}
2224
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002225static void
2226hotplug_update_tasks(struct cpuset *cs,
2227 struct cpumask *new_cpus, nodemask_t *new_mems,
2228 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002229{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002230 if (cpumask_empty(new_cpus))
2231 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus);
2232 if (nodes_empty(*new_mems))
2233 *new_mems = parent_cs(cs)->effective_mems;
Li Zefan390a36a2014-07-09 16:48:54 +08002234
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002235 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002236 cpumask_copy(cs->effective_cpus, new_cpus);
2237 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002238 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08002239
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002240 if (cpus_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002241 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002242 if (mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002243 update_tasks_nodemask(cs);
2244}
2245
Tejun Heodeb7aa32013-01-07 08:51:07 -08002246/**
Li Zefan388afd82013-06-09 17:14:47 +08002247 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
Tejun Heodeb7aa32013-01-07 08:51:07 -08002248 * @cs: cpuset in interest
Cliff Wickman956db3c2008-02-07 00:14:43 -08002249 *
Tejun Heodeb7aa32013-01-07 08:51:07 -08002250 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2251 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2252 * all its tasks are moved to the nearest ancestor with both resources.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002253 */
Li Zefan388afd82013-06-09 17:14:47 +08002254static void cpuset_hotplug_update_tasks(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002255{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002256 static cpumask_t new_cpus;
2257 static nodemask_t new_mems;
2258 bool cpus_updated;
2259 bool mems_updated;
Li Zefane44193d2013-06-09 17:14:22 +08002260retry:
2261 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002262
Tejun Heo5d21cc22013-01-07 08:51:08 -08002263 mutex_lock(&cpuset_mutex);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002264
Li Zefane44193d2013-06-09 17:14:22 +08002265 /*
2266 * We have raced with task attaching. We wait until attaching
2267 * is finished, so we won't attach a task to an empty cpuset.
2268 */
2269 if (cs->attach_in_progress) {
2270 mutex_unlock(&cpuset_mutex);
2271 goto retry;
2272 }
2273
Riley Andrewsb3ea9ac2016-09-06 15:16:25 -07002274 cpumask_and(&new_cpus, cs->cpus_requested, parent_cs(cs)->effective_cpus);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002275 nodes_and(new_mems, cs->mems_allowed, parent_cs(cs)->effective_mems);
Paul Jacksonb4501292008-02-07 00:14:47 -08002276
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002277 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
2278 mems_updated = !nodes_equal(new_mems, cs->effective_mems);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002279
Tejun Heo9e10a132015-09-18 11:56:28 -04002280 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys))
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002281 hotplug_update_tasks(cs, &new_cpus, &new_mems,
2282 cpus_updated, mems_updated);
Li Zefan390a36a2014-07-09 16:48:54 +08002283 else
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002284 hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems,
2285 cpus_updated, mems_updated);
Tejun Heo8d033942013-01-07 08:51:07 -08002286
Tejun Heo5d21cc22013-01-07 08:51:08 -08002287 mutex_unlock(&cpuset_mutex);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002288}
Paul Jacksonb4501292008-02-07 00:14:47 -08002289
Tejun Heodeb7aa32013-01-07 08:51:07 -08002290/**
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002291 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
Tejun Heodeb7aa32013-01-07 08:51:07 -08002292 *
2293 * This function is called after either CPU or memory configuration has
2294 * changed and updates cpuset accordingly. The top_cpuset is always
2295 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2296 * order to make cpusets transparent (of no affect) on systems that are
2297 * actively using CPU hotplug but making no active use of cpusets.
2298 *
2299 * Non-root cpusets are only affected by offlining. If any CPUs or memory
Li Zefan388afd82013-06-09 17:14:47 +08002300 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
2301 * all descendants.
Tejun Heodeb7aa32013-01-07 08:51:07 -08002302 *
2303 * Note that CPU offlining during suspend is ignored. We don't modify
2304 * cpusets across suspend/resume cycles at all.
2305 */
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002306static void cpuset_hotplug_workfn(struct work_struct *work)
Tejun Heodeb7aa32013-01-07 08:51:07 -08002307{
Li Zefan5c5cc622013-06-09 17:16:29 +08002308 static cpumask_t new_cpus;
2309 static nodemask_t new_mems;
Tejun Heodeb7aa32013-01-07 08:51:07 -08002310 bool cpus_updated, mems_updated;
Tejun Heo9e10a132015-09-18 11:56:28 -04002311 bool on_dfl = cgroup_subsys_on_dfl(cpuset_cgrp_subsys);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002312
Tejun Heo5d21cc22013-01-07 08:51:08 -08002313 mutex_lock(&cpuset_mutex);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002314
Tejun Heodeb7aa32013-01-07 08:51:07 -08002315 /* fetch the available cpus/mems and find out which changed how */
2316 cpumask_copy(&new_cpus, cpu_active_mask);
2317 new_mems = node_states[N_MEMORY];
Cliff Wickman956db3c2008-02-07 00:14:43 -08002318
Li Zefan7e882912014-07-09 16:48:42 +08002319 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus);
2320 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302321
Tejun Heodeb7aa32013-01-07 08:51:07 -08002322 /* synchronize cpus_allowed to cpu_active_mask */
2323 if (cpus_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002324 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08002325 if (!on_dfl)
2326 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
Li Zefan1344ab92014-07-09 16:47:16 +08002327 cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002328 spin_unlock_irq(&callback_lock);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002329 /* we don't mess with cpumasks of tasks in top_cpuset */
2330 }
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302331
Tejun Heodeb7aa32013-01-07 08:51:07 -08002332 /* synchronize mems_allowed to N_MEMORY */
2333 if (mems_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002334 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08002335 if (!on_dfl)
2336 top_cpuset.mems_allowed = new_mems;
Li Zefan1344ab92014-07-09 16:47:16 +08002337 top_cpuset.effective_mems = new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002338 spin_unlock_irq(&callback_lock);
Tejun Heod66393e2014-02-13 06:58:40 -05002339 update_tasks_nodemask(&top_cpuset);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002340 }
2341
Tejun Heo5d21cc22013-01-07 08:51:08 -08002342 mutex_unlock(&cpuset_mutex);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002343
Li Zefan5c5cc622013-06-09 17:16:29 +08002344 /* if cpus or mems changed, we need to propagate to descendants */
2345 if (cpus_updated || mems_updated) {
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002346 struct cpuset *cs;
Tejun Heo492eb212013-08-08 20:11:25 -04002347 struct cgroup_subsys_state *pos_css;
Paul Jacksonb4501292008-02-07 00:14:47 -08002348
Paul Jacksonb4501292008-02-07 00:14:47 -08002349 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002350 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
Tejun Heoec903c02014-05-13 12:11:01 -04002351 if (cs == &top_cpuset || !css_tryget_online(&cs->css))
Li Zefan388afd82013-06-09 17:14:47 +08002352 continue;
2353 rcu_read_unlock();
2354
2355 cpuset_hotplug_update_tasks(cs);
2356
2357 rcu_read_lock();
2358 css_put(&cs->css);
2359 }
Tejun Heodeb7aa32013-01-07 08:51:07 -08002360 rcu_read_unlock();
2361 }
Tejun Heo8d033942013-01-07 08:51:07 -08002362
Tejun Heodeb7aa32013-01-07 08:51:07 -08002363 /* rebuild sched domains if cpus_allowed has changed */
Li Zhonge0e80a02013-04-27 06:52:43 -07002364 if (cpus_updated)
2365 rebuild_sched_domains();
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002366}
2367
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302368void cpuset_update_active_cpus(bool cpu_online)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002369{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002370 /*
2371 * We're inside cpu hotplug critical region which usually nests
2372 * inside cgroup synchronization. Bounce actual hotplug processing
2373 * to a work item to avoid reverse locking order.
2374 *
2375 * We still need to do partition_sched_domains() synchronously;
2376 * otherwise, the scheduler will get confused and put tasks to the
2377 * dead CPU. Fall back to the default single domain.
2378 * cpuset_hotplug_workfn() will rebuild it as necessary.
2379 */
2380 partition_sched_domains(1, NULL, NULL);
2381 schedule_work(&cpuset_hotplug_work);
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002382}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002383
Paul Jackson38837fc2006-09-29 02:01:16 -07002384/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002385 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2386 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302387 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07002388 */
Miao Xief4818912008-11-19 15:36:30 -08002389static int cpuset_track_online_nodes(struct notifier_block *self,
2390 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002391{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002392 schedule_work(&cpuset_hotplug_work);
Miao Xief4818912008-11-19 15:36:30 -08002393 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002394}
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002395
2396static struct notifier_block cpuset_track_online_nodes_nb = {
2397 .notifier_call = cpuset_track_online_nodes,
2398 .priority = 10, /* ??! */
2399};
Paul Jackson38837fc2006-09-29 02:01:16 -07002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401/**
2402 * cpuset_init_smp - initialize cpus_allowed
2403 *
2404 * Description: Finish top cpuset after cpu, node maps are initialized
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406void __init cpuset_init_smp(void)
2407{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002408 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002409 top_cpuset.mems_allowed = node_states[N_MEMORY];
Li Zefan33ad8012013-06-09 17:15:08 +08002410 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002411
Li Zefane2b9a3d2014-07-09 16:47:03 +08002412 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask);
2413 top_cpuset.effective_mems = node_states[N_MEMORY];
2414
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002415 register_hotmemory_notifier(&cpuset_track_online_nodes_nb);
Tejun Heoe93ad192016-01-19 12:18:41 -05002416
2417 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0);
2418 BUG_ON(!cpuset_migrate_mm_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
2421/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2423 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002424 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002426 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10302428 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 * tasks cpuset.
2430 **/
2431
Li Zefan6af866a2009-01-07 18:08:45 -08002432void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002434 unsigned long flags;
2435
2436 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002437 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002438 guarantee_online_cpus(task_cs(tsk), pmask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002439 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002440 spin_unlock_irqrestore(&callback_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002443void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002444{
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002445 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002446 do_set_cpus_allowed(tsk, task_cs(tsk)->effective_cpus);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002447 rcu_read_unlock();
2448
2449 /*
2450 * We own tsk->cpus_allowed, nobody can change it under us.
2451 *
2452 * But we used cs && cs->cpus_allowed lockless and thus can
2453 * race with cgroup_attach_task() or update_cpumask() and get
2454 * the wrong tsk->cpus_allowed. However, both cases imply the
2455 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2456 * which takes task_rq_lock().
2457 *
2458 * If we are called after it dropped the lock we must see all
2459 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2460 * set any mask even if it is not right from task_cs() pov,
2461 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002462 *
2463 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2464 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002465 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002466}
2467
Rasmus Villemoes8f4ab072015-02-12 15:00:16 -08002468void __init cpuset_init_current_mems_allowed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002470 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002473/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002474 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2475 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2476 *
2477 * Description: Returns the nodemask_t mems_allowed of the cpuset
2478 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002479 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002480 * tasks cpuset.
2481 **/
2482
2483nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2484{
2485 nodemask_t mask;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002486 unsigned long flags;
Paul Jackson909d75a2006-01-08 01:01:55 -08002487
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002488 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002489 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002490 guarantee_online_mems(task_cs(tsk), &mask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002491 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002492 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson909d75a2006-01-08 01:01:55 -08002493
2494 return mask;
2495}
2496
2497/**
Mel Gorman19770b32008-04-28 02:12:18 -07002498 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2499 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002500 *
Mel Gorman19770b32008-04-28 02:12:18 -07002501 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 */
Mel Gorman19770b32008-04-28 02:12:18 -07002503int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Mel Gorman19770b32008-04-28 02:12:18 -07002505 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Paul Jackson9bf22292005-09-06 15:18:12 -07002508/*
Paul Menage78608362008-04-29 01:00:26 -07002509 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2510 * mem_hardwall ancestor to the specified cpuset. Call holding
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002511 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
Paul Menage78608362008-04-29 01:00:26 -07002512 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 */
Tejun Heoc9710d82013-08-08 20:11:22 -04002514static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Tejun Heoc4310692013-01-07 08:51:08 -08002516 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs))
2517 cs = parent_cs(cs);
Paul Jackson9bf22292005-09-06 15:18:12 -07002518 return cs;
2519}
2520
2521/**
Vladimir Davydov344736f2014-10-20 15:50:30 +04002522 * cpuset_node_allowed - Can we allocate on a memory node?
David Rientjesa1bc5a42009-04-02 16:57:54 -07002523 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002524 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002525 *
David Rientjes6e276d22015-04-14 15:47:01 -07002526 * If we're in interrupt, yes, we can always allocate. If @node is set in
2527 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
2528 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
2529 * yes. If current has access to memory reserves due to TIF_MEMDIE, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002530 * Otherwise, no.
2531 *
2532 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002533 * and do not allow allocations outside the current tasks cpuset
2534 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002535 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002536 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002537 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002538 * Scanning up parent cpusets requires callback_lock. The
Paul Jackson02a0e532006-12-13 00:34:25 -08002539 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2540 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2541 * current tasks mems_allowed came up empty on the first pass over
2542 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002543 * cpuset are short of memory, might require taking the callback_lock.
Paul Jackson9bf22292005-09-06 15:18:12 -07002544 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002545 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002546 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2547 * so no allocation on a node outside the cpuset is allowed (unless
2548 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002549 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002550 * The second pass through get_page_from_freelist() doesn't even call
2551 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2552 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2553 * in alloc_flags. That logic and the checks below have the combined
2554 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002555 * in_interrupt - any node ok (current task context irrelevant)
2556 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002557 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002558 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002559 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson02a0e532006-12-13 00:34:25 -08002560 */
Vlastimil Babka002f2902016-05-19 17:14:30 -07002561bool __cpuset_node_allowed(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002562{
Tejun Heoc9710d82013-08-08 20:11:22 -04002563 struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002564 int allowed; /* is allocation in zone z allowed? */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002565 unsigned long flags;
Paul Jackson9bf22292005-09-06 15:18:12 -07002566
David Rientjes6e276d22015-04-14 15:47:01 -07002567 if (in_interrupt())
Vlastimil Babka002f2902016-05-19 17:14:30 -07002568 return true;
Paul Jackson9bf22292005-09-06 15:18:12 -07002569 if (node_isset(node, current->mems_allowed))
Vlastimil Babka002f2902016-05-19 17:14:30 -07002570 return true;
David Rientjesc596d9f2007-05-06 14:49:32 -07002571 /*
2572 * Allow tasks that have access to memory reserves because they have
2573 * been OOM killed to get memory anywhere.
2574 */
2575 if (unlikely(test_thread_flag(TIF_MEMDIE)))
Vlastimil Babka002f2902016-05-19 17:14:30 -07002576 return true;
Paul Jackson9bf22292005-09-06 15:18:12 -07002577 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
Vlastimil Babka002f2902016-05-19 17:14:30 -07002578 return false;
Paul Jackson9bf22292005-09-06 15:18:12 -07002579
Bob Picco5563e772005-11-13 16:06:35 -08002580 if (current->flags & PF_EXITING) /* Let dying task have memory */
Vlastimil Babka002f2902016-05-19 17:14:30 -07002581 return true;
Bob Picco5563e772005-11-13 16:06:35 -08002582
Paul Jackson9bf22292005-09-06 15:18:12 -07002583 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002584 spin_lock_irqsave(&callback_lock, flags);
Paul Jackson053199e2005-10-30 15:02:30 -08002585
Li Zefanb8dadcb2014-03-03 17:28:36 -05002586 rcu_read_lock();
Paul Menage78608362008-04-29 01:00:26 -07002587 cs = nearest_hardwall_ancestor(task_cs(current));
Li Zefan99afb0f2014-02-27 18:19:36 +08002588 allowed = node_isset(node, cs->mems_allowed);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002589 rcu_read_unlock();
Paul Jackson053199e2005-10-30 15:02:30 -08002590
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002591 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson9bf22292005-09-06 15:18:12 -07002592 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002595/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07002596 * cpuset_mem_spread_node() - On which node to begin search for a file page
2597 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08002598 *
2599 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2600 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2601 * and if the memory allocation used cpuset_mem_spread_node()
2602 * to determine on which node to start looking, as it will for
2603 * certain page cache or slab cache pages such as used for file
2604 * system buffers and inode caches, then instead of starting on the
2605 * local node to look for a free page, rather spread the starting
2606 * node around the tasks mems_allowed nodes.
2607 *
2608 * We don't have to worry about the returned node being offline
2609 * because "it can't happen", and even if it did, it would be ok.
2610 *
2611 * The routines calling guarantee_online_mems() are careful to
2612 * only set nodes in task->mems_allowed that are online. So it
2613 * should not be possible for the following code to return an
2614 * offline node. But if it did, that would be ok, as this routine
2615 * is not returning the node where the allocation must be, only
2616 * the node where the search should start. The zonelist passed to
2617 * __alloc_pages() will include all nodes. If the slab allocator
2618 * is passed an offline node, it will fall back to the local node.
2619 * See kmem_cache_alloc_node().
2620 */
2621
Jack Steiner6adef3e2010-05-26 14:42:49 -07002622static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08002623{
Andrew Morton0edaf862016-05-19 17:10:58 -07002624 return *rotor = next_node_in(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08002625}
Jack Steiner6adef3e2010-05-26 14:42:49 -07002626
2627int cpuset_mem_spread_node(void)
2628{
Michal Hocko778d3b02011-07-26 16:08:30 -07002629 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2630 current->cpuset_mem_spread_rotor =
2631 node_random(&current->mems_allowed);
2632
Jack Steiner6adef3e2010-05-26 14:42:49 -07002633 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2634}
2635
2636int cpuset_slab_spread_node(void)
2637{
Michal Hocko778d3b02011-07-26 16:08:30 -07002638 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2639 current->cpuset_slab_spread_rotor =
2640 node_random(&current->mems_allowed);
2641
Jack Steiner6adef3e2010-05-26 14:42:49 -07002642 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2643}
2644
Paul Jackson825a46a2006-03-24 03:16:03 -08002645EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2646
2647/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002648 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2649 * @tsk1: pointer to task_struct of some task.
2650 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002651 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002652 * Description: Return true if @tsk1's mems_allowed intersects the
2653 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2654 * one of the task's memory usage might impact the memory available
2655 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002656 **/
2657
David Rientjesbbe373f2007-10-16 23:25:58 -07002658int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2659 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002660{
David Rientjesbbe373f2007-10-16 23:25:58 -07002661 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002662}
2663
David Rientjes75aa1992009-01-06 14:39:01 -08002664/**
David Rientjesda39da32015-11-05 18:48:05 -08002665 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
David Rientjes75aa1992009-01-06 14:39:01 -08002666 *
David Rientjesda39da32015-11-05 18:48:05 -08002667 * Description: Prints current's name, cpuset name, and cached copy of its
Li Zefanb8dadcb2014-03-03 17:28:36 -05002668 * mems_allowed to the kernel log.
David Rientjes75aa1992009-01-06 14:39:01 -08002669 */
David Rientjesda39da32015-11-05 18:48:05 -08002670void cpuset_print_current_mems_allowed(void)
David Rientjes75aa1992009-01-06 14:39:01 -08002671{
Li Zefanb8dadcb2014-03-03 17:28:36 -05002672 struct cgroup *cgrp;
David Rientjes75aa1992009-01-06 14:39:01 -08002673
Li Zefanb8dadcb2014-03-03 17:28:36 -05002674 rcu_read_lock();
Li Zefan63f43f52013-01-25 16:08:01 +08002675
David Rientjesda39da32015-11-05 18:48:05 -08002676 cgrp = task_cs(current)->css.cgroup;
2677 pr_info("%s cpuset=", current->comm);
Tejun Heoe61734c2014-02-12 09:29:50 -05002678 pr_cont_cgroup_name(cgrp);
David Rientjesda39da32015-11-05 18:48:05 -08002679 pr_cont(" mems_allowed=%*pbl\n",
2680 nodemask_pr_args(&current->mems_allowed));
Li Zefanf440d982013-03-01 15:02:15 +08002681
Li Zefancfb59662013-03-12 10:28:39 +08002682 rcu_read_unlock();
David Rientjes75aa1992009-01-06 14:39:01 -08002683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002686 * Collection of memory_pressure is suppressed unless
2687 * this flag is enabled by writing "1" to the special
2688 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2689 */
2690
Paul Jacksonc5b2aff82006-01-08 01:01:51 -08002691int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002692
2693/**
2694 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2695 *
2696 * Keep a running average of the rate of synchronous (direct)
2697 * page reclaim efforts initiated by tasks in each cpuset.
2698 *
2699 * This represents the rate at which some task in the cpuset
2700 * ran low on memory on all nodes it was allowed to use, and
2701 * had to enter the kernels page reclaim code in an effort to
2702 * create more free memory by tossing clean pages or swapping
2703 * or writing dirty pages.
2704 *
2705 * Display to user space in the per-cpuset read-only file
2706 * "memory_pressure". Value displayed is an integer
2707 * representing the recent rate of entry into the synchronous
2708 * (direct) page reclaim by any task attached to the cpuset.
2709 **/
2710
2711void __cpuset_memory_pressure_bump(void)
2712{
Li Zefanb8dadcb2014-03-03 17:28:36 -05002713 rcu_read_lock();
Paul Menage8793d852007-10-18 23:39:39 -07002714 fmeter_markevent(&task_cs(current)->fmeter);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002715 rcu_read_unlock();
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002716}
2717
Paul Menage8793d852007-10-18 23:39:39 -07002718#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * proc_cpuset_show()
2721 * - Print tasks cpuset path into seq_file.
2722 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002723 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2724 * doesn't really matter if tsk->cpuset changes after we read it,
Tejun Heo5d21cc22013-01-07 08:51:08 -08002725 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002726 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 */
Zefan Li52de4772014-09-18 16:03:36 +08002728int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
2729 struct pid *pid, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Tejun Heo4c737b42016-08-10 11:23:44 -04002731 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002732 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002733 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Eric W. Biederman99f89552006-06-26 00:25:55 -07002735 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05002736 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002738 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Aditya Kalia79a9082016-01-29 02:54:06 -06002740 css = task_get_css(tsk, cpuset_cgrp_id);
Tejun Heo4c737b42016-08-10 11:23:44 -04002741 retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX,
2742 current->nsproxy->cgroup_ns);
Aditya Kalia79a9082016-01-29 02:54:06 -06002743 css_put(css);
Tejun Heo4c737b42016-08-10 11:23:44 -04002744 if (retval >= PATH_MAX)
Tejun Heo679a5e32016-09-29 11:58:36 +02002745 retval = -ENAMETOOLONG;
2746 if (retval < 0)
Zefan Li52de4772014-09-18 16:03:36 +08002747 goto out_free;
Tejun Heo4c737b42016-08-10 11:23:44 -04002748 seq_puts(m, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 seq_putc(m, '\n');
Tejun Heoe61734c2014-02-12 09:29:50 -05002750 retval = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002751out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002753out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return retval;
2755}
Paul Menage8793d852007-10-18 23:39:39 -07002756#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Heiko Carstensd01d4822009-09-21 11:06:27 +02002758/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002759void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Tejun Heoe8e6d972015-02-13 14:37:23 -08002761 seq_printf(m, "Mems_allowed:\t%*pb\n",
2762 nodemask_pr_args(&task->mems_allowed));
2763 seq_printf(m, "Mems_allowed_list:\t%*pbl\n",
2764 nodemask_pr_args(&task->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}