blob: 511b1dd8ff0923b83304f5c414b0e06a80232a4a [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 Bergmannd2b43652015-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
Dima Zavin45a636e2017-08-02 13:32:18 -070064DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key);
Vlastimil Babka002f2902016-05-19 17:14:30 -070065DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key);
Paul Jackson202f72d2006-01-08 01:01:57 -080066
Paul Jackson3e0d98b2006-01-08 01:01:49 -080067/* See "Frequency meter" comments, below. */
68
69struct fmeter {
70 int cnt; /* unprocessed events count */
71 int val; /* most recent output value */
Arnd Bergmannd2b43652015-11-25 16:16:55 +010072 time64_t time; /* clock (secs) when val computed */
Paul Jackson3e0d98b2006-01-08 01:01:49 -080073 spinlock_t lock; /* guards read or write of above */
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct cpuset {
Paul Menage8793d852007-10-18 23:39:39 -070077 struct cgroup_subsys_state css;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned long flags; /* "unsigned long" so bitops work */
Li Zefane2b9a3d2014-07-09 16:47:03 +080080
Li Zefan7e882912014-07-09 16:48:42 +080081 /*
82 * On default hierarchy:
83 *
84 * The user-configured masks can only be changed by writing to
85 * cpuset.cpus and cpuset.mems, and won't be limited by the
86 * parent masks.
87 *
88 * The effective masks is the real masks that apply to the tasks
89 * in the cpuset. They may be changed if the configured masks are
90 * changed or hotplug happens.
91 *
92 * effective_mask == configured_mask & parent's effective_mask,
93 * and if it ends up empty, it will inherit the parent's mask.
94 *
95 *
96 * On legacy hierachy:
97 *
98 * The user-configured masks are always the same with effective masks.
99 */
100
Li Zefane2b9a3d2014-07-09 16:47:03 +0800101 /* user-configured CPUs and Memory Nodes allow to tasks */
102 cpumask_var_t cpus_allowed;
103 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{
Li Zefan300ed6c2009-01-07 18:08:44 -0800402 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
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 &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800502 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
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 {
Li Zefan300ed6c2009-01-07 18:08:44 -0800961 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700962 if (retval < 0)
963 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700964
Li Zefan5d8ba822014-07-09 16:49:12 +0800965 if (!cpumask_subset(trialcs->cpus_allowed,
966 top_cpuset.cpus_allowed))
Lai Jiangshan37340742008-06-05 22:46:32 -0700967 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -0700968 }
Paul Jackson029190c2007-10-18 23:40:20 -0700969
Paul Menage8707d8b2007-10-18 23:40:22 -0700970 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -0800971 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -0700972 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800973
Li Zefana73456f2013-06-05 17:15:59 +0800974 retval = validate_change(cs, trialcs);
975 if (retval < 0)
976 return retval;
977
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400978 spin_lock_irq(&callback_lock);
Li Zefan300ed6c2009-01-07 18:08:44 -0800979 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400980 spin_unlock_irq(&callback_lock);
Paul Jackson029190c2007-10-18 23:40:20 -0700981
Li Zefan734d4512014-07-09 16:47:29 +0800982 /* use trialcs->cpus_allowed as a temp variable */
983 update_cpumasks_hier(cs, trialcs->cpus_allowed);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Paul Jackson053199e2005-10-30 15:02:30 -0800987/*
Tejun Heoe93ad192016-01-19 12:18:41 -0500988 * Migrate memory region from one set of nodes to another. This is
989 * performed asynchronously as it can be called from process migration path
990 * holding locks involved in process management. All mm migrations are
991 * performed in the queued order and can be waited for by flushing
992 * cpuset_migrate_mm_wq.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800993 */
994
Tejun Heoe93ad192016-01-19 12:18:41 -0500995struct cpuset_migrate_mm_work {
996 struct work_struct work;
997 struct mm_struct *mm;
998 nodemask_t from;
999 nodemask_t to;
1000};
1001
1002static void cpuset_migrate_mm_workfn(struct work_struct *work)
1003{
1004 struct cpuset_migrate_mm_work *mwork =
1005 container_of(work, struct cpuset_migrate_mm_work, work);
1006
1007 /* on a wq worker, no need to worry about %current's mems_allowed */
1008 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
1009 mmput(mwork->mm);
1010 kfree(mwork);
1011}
1012
Paul Jacksone4e364e2006-03-31 02:30:52 -08001013static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1014 const nodemask_t *to)
1015{
Tejun Heoe93ad192016-01-19 12:18:41 -05001016 struct cpuset_migrate_mm_work *mwork;
Paul Jacksone4e364e2006-03-31 02:30:52 -08001017
Tejun Heoe93ad192016-01-19 12:18:41 -05001018 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL);
1019 if (mwork) {
1020 mwork->mm = mm;
1021 mwork->from = *from;
1022 mwork->to = *to;
1023 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn);
1024 queue_work(cpuset_migrate_mm_wq, &mwork->work);
1025 } else {
1026 mmput(mm);
1027 }
1028}
Paul Jacksone4e364e2006-03-31 02:30:52 -08001029
Tejun Heo5cf1cac2016-04-21 19:06:48 -04001030static void cpuset_post_attach(void)
Tejun Heoe93ad192016-01-19 12:18:41 -05001031{
1032 flush_workqueue(cpuset_migrate_mm_wq);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001033}
1034
Li Zefan3b6766f2009-04-02 16:57:51 -07001035/*
Miao Xie58568d22009-06-16 15:31:49 -07001036 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
1037 * @tsk: the task to change
1038 * @newmems: new nodes that the task will be set
1039 *
1040 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
1041 * we structure updates as setting all new allowed nodes, then clearing newly
1042 * disallowed ones.
Miao Xie58568d22009-06-16 15:31:49 -07001043 */
1044static void cpuset_change_task_nodemask(struct task_struct *tsk,
1045 nodemask_t *newmems)
1046{
David Rientjesb2462722011-12-19 17:11:52 -08001047 bool need_loop;
David Rientjes89e8a242011-11-02 13:38:39 -07001048
Miao Xiec0ff7452010-05-24 14:32:08 -07001049 task_lock(tsk);
David Rientjesb2462722011-12-19 17:11:52 -08001050 /*
1051 * Determine if a loop is necessary if another thread is doing
Mel Gormand26914d2014-04-03 14:47:24 -07001052 * read_mems_allowed_begin(). If at least one node remains unchanged and
David Rientjesb2462722011-12-19 17:11:52 -08001053 * tsk does not have a mempolicy, then an empty nodemask will not be
1054 * possible when mems_allowed is larger than a word.
1055 */
1056 need_loop = task_has_mempolicy(tsk) ||
1057 !nodes_intersects(*newmems, tsk->mems_allowed);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001058
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001059 if (need_loop) {
1060 local_irq_disable();
Mel Gormancc9a6c82012-03-21 16:34:11 -07001061 write_seqcount_begin(&tsk->mems_allowed_seq);
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001062 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001063
Miao Xie58568d22009-06-16 15:31:49 -07001064 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Miao Xiec0ff7452010-05-24 14:32:08 -07001065 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
1066
Miao Xiec0ff7452010-05-24 14:32:08 -07001067 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
Miao Xie58568d22009-06-16 15:31:49 -07001068 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001069
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001070 if (need_loop) {
Mel Gormancc9a6c82012-03-21 16:34:11 -07001071 write_seqcount_end(&tsk->mems_allowed_seq);
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001072 local_irq_enable();
1073 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001074
Miao Xiec0ff7452010-05-24 14:32:08 -07001075 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001076}
1077
Paul Menage8793d852007-10-18 23:39:39 -07001078static void *cpuset_being_rebound;
1079
Miao Xie0b2f6302008-07-25 01:47:21 -07001080/**
1081 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1082 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
Miao Xie0b2f6302008-07-25 01:47:21 -07001083 *
Tejun Heod66393e2014-02-13 06:58:40 -05001084 * Iterate through each task of @cs updating its mems_allowed to the
1085 * effective cpuset's. As this function is called with cpuset_mutex held,
1086 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -07001087 */
Tejun Heod66393e2014-02-13 06:58:40 -05001088static void update_tasks_nodemask(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Li Zefan33ad8012013-06-09 17:15:08 +08001090 static nodemask_t newmems; /* protected by cpuset_mutex */
Tejun Heod66393e2014-02-13 06:58:40 -05001091 struct css_task_iter it;
1092 struct task_struct *task;
Paul Jackson59dac162006-01-08 01:01:52 -08001093
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001094 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001095
Li Zefanae1c8022014-07-09 16:48:32 +08001096 guarantee_online_mems(cs, &newmems);
Li Zefan33ad8012013-06-09 17:15:08 +08001097
Paul Jackson42253992006-01-08 01:01:59 -08001098 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001099 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1100 * take while holding tasklist_lock. Forks can happen - the
1101 * mpol_dup() cpuset_being_rebound check will catch such forks,
1102 * and rebind their vma mempolicies too. Because we still hold
Tejun Heo5d21cc22013-01-07 08:51:08 -08001103 * the global cpuset_mutex, we know that no other rebind effort
Li Zefan3b6766f2009-04-02 16:57:51 -07001104 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001105 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001106 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001107 */
Tejun Heod66393e2014-02-13 06:58:40 -05001108 css_task_iter_start(&cs->css, &it);
1109 while ((task = css_task_iter_next(&it))) {
1110 struct mm_struct *mm;
1111 bool migrate;
1112
1113 cpuset_change_task_nodemask(task, &newmems);
1114
1115 mm = get_task_mm(task);
1116 if (!mm)
1117 continue;
1118
1119 migrate = is_memory_migrate(cs);
1120
1121 mpol_rebind_mm(mm, &cs->mems_allowed);
1122 if (migrate)
1123 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
Tejun Heoe93ad192016-01-19 12:18:41 -05001124 else
1125 mmput(mm);
Tejun Heod66393e2014-02-13 06:58:40 -05001126 }
1127 css_task_iter_end(&it);
Paul Jackson42253992006-01-08 01:01:59 -08001128
Li Zefan33ad8012013-06-09 17:15:08 +08001129 /*
1130 * All the tasks' nodemasks have been updated, update
1131 * cs->old_mems_allowed.
1132 */
1133 cs->old_mems_allowed = newmems;
1134
Paul Menage2df167a2008-02-07 00:14:45 -08001135 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001136 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Miao Xie0b2f6302008-07-25 01:47:21 -07001139/*
Li Zefan734d4512014-07-09 16:47:29 +08001140 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1141 * @cs: the cpuset to consider
1142 * @new_mems: a temp variable for calculating new effective_mems
Li Zefan5c5cc622013-06-09 17:16:29 +08001143 *
Li Zefan734d4512014-07-09 16:47:29 +08001144 * When configured nodemask is changed, the effective nodemasks of this cpuset
1145 * and all its descendants need to be updated.
1146 *
1147 * On legacy hiearchy, effective_mems will be the same with mems_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +08001148 *
1149 * Called with cpuset_mutex held
1150 */
Li Zefan734d4512014-07-09 16:47:29 +08001151static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
Li Zefan5c5cc622013-06-09 17:16:29 +08001152{
1153 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -04001154 struct cgroup_subsys_state *pos_css;
Li Zefan5c5cc622013-06-09 17:16:29 +08001155
1156 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +08001157 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1158 struct cpuset *parent = parent_cs(cp);
1159
1160 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
1161
Li Zefan554b0d12014-07-09 16:47:41 +08001162 /*
1163 * If it becomes empty, inherit the effective mask of the
1164 * parent, which is guaranteed to have some MEMs.
1165 */
Tejun Heo9e10a132015-09-18 11:56:28 -04001166 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
1167 nodes_empty(*new_mems))
Li Zefan554b0d12014-07-09 16:47:41 +08001168 *new_mems = parent->effective_mems;
1169
Li Zefan734d4512014-07-09 16:47:29 +08001170 /* Skip the whole subtree if the nodemask remains the same. */
1171 if (nodes_equal(*new_mems, cp->effective_mems)) {
1172 pos_css = css_rightmost_descendant(pos_css);
1173 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +08001174 }
Li Zefan734d4512014-07-09 16:47:29 +08001175
Tejun Heoec903c02014-05-13 12:11:01 -04001176 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +08001177 continue;
1178 rcu_read_unlock();
1179
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001180 spin_lock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001181 cp->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001182 spin_unlock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001183
Tejun Heo9e10a132015-09-18 11:56:28 -04001184 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefana1381262014-07-30 15:07:13 +08001185 !nodes_equal(cp->mems_allowed, cp->effective_mems));
Li Zefan734d4512014-07-09 16:47:29 +08001186
Tejun Heod66393e2014-02-13 06:58:40 -05001187 update_tasks_nodemask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +08001188
1189 rcu_read_lock();
1190 css_put(&cp->css);
1191 }
1192 rcu_read_unlock();
1193}
1194
1195/*
Miao Xie0b2f6302008-07-25 01:47:21 -07001196 * Handle user request to change the 'mems' memory placement
1197 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001198 * cpusets mems_allowed, and for each task in the cpuset,
1199 * update mems_allowed and rebind task's mempolicy and any vma
1200 * mempolicies and if the cpuset is marked 'memory_migrate',
1201 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001202 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001203 * Call with cpuset_mutex held. May take callback_lock during call.
Miao Xie0b2f6302008-07-25 01:47:21 -07001204 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1205 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1206 * their mempolicies to the cpusets new mems_allowed.
1207 */
Li Zefan645fcc92009-01-07 18:08:43 -08001208static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1209 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001210{
Miao Xie0b2f6302008-07-25 01:47:21 -07001211 int retval;
1212
1213 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001214 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001215 * it's read-only
1216 */
Miao Xie53feb292010-03-23 13:35:35 -07001217 if (cs == &top_cpuset) {
1218 retval = -EACCES;
1219 goto done;
1220 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001221
Miao Xie0b2f6302008-07-25 01:47:21 -07001222 /*
1223 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1224 * Since nodelist_parse() fails on an empty mask, we special case
1225 * that parsing. The validate_change() call ensures that cpusets
1226 * with tasks have memory.
1227 */
1228 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001229 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001230 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001231 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001232 if (retval < 0)
1233 goto done;
1234
Li Zefan645fcc92009-01-07 18:08:43 -08001235 if (!nodes_subset(trialcs->mems_allowed,
Li Zefan5d8ba822014-07-09 16:49:12 +08001236 top_cpuset.mems_allowed)) {
1237 retval = -EINVAL;
Miao Xie53feb292010-03-23 13:35:35 -07001238 goto done;
1239 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001240 }
Li Zefan33ad8012013-06-09 17:15:08 +08001241
1242 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001243 retval = 0; /* Too easy - nothing to do */
1244 goto done;
1245 }
Li Zefan645fcc92009-01-07 18:08:43 -08001246 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001247 if (retval < 0)
1248 goto done;
1249
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001250 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001251 cs->mems_allowed = trialcs->mems_allowed;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001252 spin_unlock_irq(&callback_lock);
Miao Xie0b2f6302008-07-25 01:47:21 -07001253
Li Zefan734d4512014-07-09 16:47:29 +08001254 /* use trialcs->mems_allowed as a temp variable */
Alban Crequy24ee3cf2015-08-06 16:21:05 +02001255 update_nodemasks_hier(cs, &trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001256done:
1257 return retval;
1258}
1259
Paul Menage8793d852007-10-18 23:39:39 -07001260int current_cpuset_is_being_rebound(void)
1261{
Gu Zheng391acf92014-06-25 09:57:18 +08001262 int ret;
1263
1264 rcu_read_lock();
1265 ret = task_cs(current) == cpuset_being_rebound;
1266 rcu_read_unlock();
1267
1268 return ret;
Paul Menage8793d852007-10-18 23:39:39 -07001269}
1270
Paul Menage5be7a472008-05-06 20:42:41 -07001271static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001272{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001273#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001274 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001275 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001276#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001277
1278 if (val != cs->relax_domain_level) {
1279 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001280 if (!cpumask_empty(cs->cpus_allowed) &&
1281 is_sched_load_balance(cs))
Tejun Heo699140b2013-01-07 08:51:07 -08001282 rebuild_sched_domains_locked();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001283 }
1284
1285 return 0;
1286}
1287
Tejun Heo72ec7022013-08-08 20:11:26 -04001288/**
Miao Xie950592f2009-06-16 15:31:47 -07001289 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1290 * @cs: the cpuset in which each task's spread flags needs to be changed
Miao Xie950592f2009-06-16 15:31:47 -07001291 *
Tejun Heod66393e2014-02-13 06:58:40 -05001292 * Iterate through each task of @cs updating its spread flags. As this
1293 * function is called with cpuset_mutex held, cpuset membership stays
1294 * stable.
Miao Xie950592f2009-06-16 15:31:47 -07001295 */
Tejun Heod66393e2014-02-13 06:58:40 -05001296static void update_tasks_flags(struct cpuset *cs)
Miao Xie950592f2009-06-16 15:31:47 -07001297{
Tejun Heod66393e2014-02-13 06:58:40 -05001298 struct css_task_iter it;
1299 struct task_struct *task;
1300
1301 css_task_iter_start(&cs->css, &it);
1302 while ((task = css_task_iter_next(&it)))
1303 cpuset_update_task_spread_flag(cs, task);
1304 css_task_iter_end(&it);
Miao Xie950592f2009-06-16 15:31:47 -07001305}
1306
1307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001309 * bit: the bit to update (see cpuset_flagbits_t)
1310 * cs: the cpuset to update
1311 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001312 *
Tejun Heo5d21cc22013-01-07 08:51:08 -08001313 * Call with cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
1315
Paul Menage700fe1a2008-04-29 01:00:00 -07001316static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1317 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Li Zefan645fcc92009-01-07 18:08:43 -08001319 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001320 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001321 int spread_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001322 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Li Zefan645fcc92009-01-07 18:08:43 -08001324 trialcs = alloc_trial_cpuset(cs);
1325 if (!trialcs)
1326 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Li Zefan645fcc92009-01-07 18:08:43 -08001328 if (turning_on)
1329 set_bit(bit, &trialcs->flags);
1330 else
1331 clear_bit(bit, &trialcs->flags);
1332
1333 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001334 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001335 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001336
Paul Jackson029190c2007-10-18 23:40:20 -07001337 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001338 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001339
Miao Xie950592f2009-06-16 15:31:47 -07001340 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1341 || (is_spread_page(cs) != is_spread_page(trialcs)));
1342
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001343 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001344 cs->flags = trialcs->flags;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001345 spin_unlock_irq(&callback_lock);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001346
Li Zefan300ed6c2009-01-07 18:08:44 -08001347 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Tejun Heo699140b2013-01-07 08:51:07 -08001348 rebuild_sched_domains_locked();
Paul Jackson029190c2007-10-18 23:40:20 -07001349
Miao Xie950592f2009-06-16 15:31:47 -07001350 if (spread_flag_changed)
Tejun Heod66393e2014-02-13 06:58:40 -05001351 update_tasks_flags(cs);
Li Zefan645fcc92009-01-07 18:08:43 -08001352out:
1353 free_trial_cpuset(trialcs);
1354 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Paul Jackson053199e2005-10-30 15:02:30 -08001357/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001358 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001359 *
1360 * These routines manage a digitally filtered, constant time based,
1361 * event frequency meter. There are four routines:
1362 * fmeter_init() - initialize a frequency meter.
1363 * fmeter_markevent() - called each time the event happens.
1364 * fmeter_getrate() - returns the recent rate of such events.
1365 * fmeter_update() - internal routine used to update fmeter.
1366 *
1367 * A common data structure is passed to each of these routines,
1368 * which is used to keep track of the state required to manage the
1369 * frequency meter and its digital filter.
1370 *
1371 * The filter works on the number of events marked per unit time.
1372 * The filter is single-pole low-pass recursive (IIR). The time unit
1373 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1374 * simulate 3 decimal digits of precision (multiplied by 1000).
1375 *
1376 * With an FM_COEF of 933, and a time base of 1 second, the filter
1377 * has a half-life of 10 seconds, meaning that if the events quit
1378 * happening, then the rate returned from the fmeter_getrate()
1379 * will be cut in half each 10 seconds, until it converges to zero.
1380 *
1381 * It is not worth doing a real infinitely recursive filter. If more
1382 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1383 * just compute FM_MAXTICKS ticks worth, by which point the level
1384 * will be stable.
1385 *
1386 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1387 * arithmetic overflow in the fmeter_update() routine.
1388 *
1389 * Given the simple 32 bit integer arithmetic used, this meter works
1390 * best for reporting rates between one per millisecond (msec) and
1391 * one per 32 (approx) seconds. At constant rates faster than one
1392 * per msec it maxes out at values just under 1,000,000. At constant
1393 * rates between one per msec, and one per second it will stabilize
1394 * to a value N*1000, where N is the rate of events per second.
1395 * At constant rates between one per second and one per 32 seconds,
1396 * it will be choppy, moving up on the seconds that have an event,
1397 * and then decaying until the next event. At rates slower than
1398 * about one in 32 seconds, it decays all the way back to zero between
1399 * each event.
1400 */
1401
1402#define FM_COEF 933 /* coefficient for half-life of 10 secs */
Arnd Bergmannd2b43652015-11-25 16:16:55 +01001403#define FM_MAXTICKS ((u32)99) /* useless computing more ticks than this */
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001404#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1405#define FM_SCALE 1000 /* faux fixed point scale */
1406
1407/* Initialize a frequency meter */
1408static void fmeter_init(struct fmeter *fmp)
1409{
1410 fmp->cnt = 0;
1411 fmp->val = 0;
1412 fmp->time = 0;
1413 spin_lock_init(&fmp->lock);
1414}
1415
1416/* Internal meter update - process cnt events and update value */
1417static void fmeter_update(struct fmeter *fmp)
1418{
Arnd Bergmannd2b43652015-11-25 16:16:55 +01001419 time64_t now;
1420 u32 ticks;
1421
1422 now = ktime_get_seconds();
1423 ticks = now - fmp->time;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001424
1425 if (ticks == 0)
1426 return;
1427
1428 ticks = min(FM_MAXTICKS, ticks);
1429 while (ticks-- > 0)
1430 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1431 fmp->time = now;
1432
1433 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1434 fmp->cnt = 0;
1435}
1436
1437/* Process any previous ticks, then bump cnt by one (times scale). */
1438static void fmeter_markevent(struct fmeter *fmp)
1439{
1440 spin_lock(&fmp->lock);
1441 fmeter_update(fmp);
1442 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1443 spin_unlock(&fmp->lock);
1444}
1445
1446/* Process any previous ticks, then return current value. */
1447static int fmeter_getrate(struct fmeter *fmp)
1448{
1449 int val;
1450
1451 spin_lock(&fmp->lock);
1452 fmeter_update(fmp);
1453 val = fmp->val;
1454 spin_unlock(&fmp->lock);
1455 return val;
1456}
1457
Tejun Heo57fce0a2014-02-13 06:58:41 -05001458static struct cpuset *cpuset_attach_old_cs;
1459
Tejun Heo5d21cc22013-01-07 08:51:08 -08001460/* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001461static int cpuset_can_attach(struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07001462{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001463 struct cgroup_subsys_state *css;
1464 struct cpuset *cs;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001465 struct task_struct *task;
1466 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07001467
Tejun Heo57fce0a2014-02-13 06:58:41 -05001468 /* used later by cpuset_attach() */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001469 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
1470 cs = css_cs(css);
Tejun Heo57fce0a2014-02-13 06:58:41 -05001471
Tejun Heo5d21cc22013-01-07 08:51:08 -08001472 mutex_lock(&cpuset_mutex);
1473
Tejun Heoaa6ec292014-07-09 10:08:08 -04001474 /* allow moving tasks into an empty cpuset if on default hierarchy */
Tejun Heo5d21cc22013-01-07 08:51:08 -08001475 ret = -ENOSPC;
Tejun Heo9e10a132015-09-18 11:56:28 -04001476 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefan88fa5232013-06-09 17:16:46 +08001477 (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)))
Tejun Heo5d21cc22013-01-07 08:51:08 -08001478 goto out_unlock;
Ben Blumbe367d02009-09-23 15:56:31 -07001479
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001480 cgroup_taskset_for_each(task, css, tset) {
Juri Lelli7f514122014-09-19 10:22:40 +01001481 ret = task_can_attach(task, cs->cpus_allowed);
1482 if (ret)
Tejun Heo5d21cc22013-01-07 08:51:08 -08001483 goto out_unlock;
1484 ret = security_task_setscheduler(task);
1485 if (ret)
1486 goto out_unlock;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Tejun Heo452477f2013-01-07 08:51:07 -08001489 /*
1490 * Mark attach is in progress. This makes validate_change() fail
1491 * changes which zero cpus/mems_allowed.
1492 */
1493 cs->attach_in_progress++;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001494 ret = 0;
1495out_unlock:
1496 mutex_unlock(&cpuset_mutex);
1497 return ret;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001498}
1499
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001500static void cpuset_cancel_attach(struct cgroup_taskset *tset)
Tejun Heo452477f2013-01-07 08:51:07 -08001501{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001502 struct cgroup_subsys_state *css;
1503 struct cpuset *cs;
1504
1505 cgroup_taskset_first(tset, &css);
1506 cs = css_cs(css);
1507
Tejun Heo5d21cc22013-01-07 08:51:08 -08001508 mutex_lock(&cpuset_mutex);
Tejun Heoeb954192013-08-08 20:11:23 -04001509 css_cs(css)->attach_in_progress--;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001510 mutex_unlock(&cpuset_mutex);
Tejun Heo452477f2013-01-07 08:51:07 -08001511}
1512
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001513/*
Tejun Heo5d21cc22013-01-07 08:51:08 -08001514 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001515 * but we can't allocate it dynamically there. Define it global and
1516 * allocate from cpuset_init().
1517 */
1518static cpumask_var_t cpus_attach;
1519
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001520static void cpuset_attach(struct cgroup_taskset *tset)
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001521{
Li Zefan67bd2c52013-06-05 17:15:35 +08001522 /* static buf protected by cpuset_mutex */
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001523 static nodemask_t cpuset_attach_nodemask_to;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001524 struct task_struct *task;
Tejun Heo4530edd2015-09-11 15:00:19 -04001525 struct task_struct *leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001526 struct cgroup_subsys_state *css;
1527 struct cpuset *cs;
Tejun Heo57fce0a2014-02-13 06:58:41 -05001528 struct cpuset *oldcs = cpuset_attach_old_cs;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001529
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001530 cgroup_taskset_first(tset, &css);
1531 cs = css_cs(css);
1532
Tejun Heo5d21cc22013-01-07 08:51:08 -08001533 mutex_lock(&cpuset_mutex);
1534
Tejun Heo94196f52011-12-12 18:12:22 -08001535 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07001536 if (cs == &top_cpuset)
1537 cpumask_copy(cpus_attach, cpu_possible_mask);
1538 else
Li Zefanae1c8022014-07-09 16:48:32 +08001539 guarantee_online_cpus(cs, cpus_attach);
Ben Blumf780bdb2011-05-26 16:25:19 -07001540
Li Zefanae1c8022014-07-09 16:48:32 +08001541 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08001542
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001543 cgroup_taskset_for_each(task, css, tset) {
Tejun Heobb9d97b2011-12-12 18:12:21 -08001544 /*
1545 * can_attach beforehand should guarantee that this doesn't
1546 * fail. TODO: have a better way to handle failure here
1547 */
1548 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1549
1550 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1551 cpuset_update_task_spread_flag(cs, task);
1552 }
David Quigley22fb52d2006-06-23 02:04:00 -07001553
Ben Blumf780bdb2011-05-26 16:25:19 -07001554 /*
Tejun Heo4530edd2015-09-11 15:00:19 -04001555 * Change mm for all threadgroup leaders. This is expensive and may
1556 * sleep and should be moved outside migration path proper.
Ben Blumf780bdb2011-05-26 16:25:19 -07001557 */
Li Zefanae1c8022014-07-09 16:48:32 +08001558 cpuset_attach_nodemask_to = cs->effective_mems;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001559 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo3df9ca02015-09-11 15:00:18 -04001560 struct mm_struct *mm = get_task_mm(leader);
Li Zefanf047cec2013-06-13 15:11:44 +08001561
Tejun Heo3df9ca02015-09-11 15:00:18 -04001562 if (mm) {
1563 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
1564
1565 /*
1566 * old_mems_allowed is the same with mems_allowed
1567 * here, except if this task is being moved
1568 * automatically due to hotplug. In that case
1569 * @mems_allowed has been updated and is empty, so
1570 * @old_mems_allowed is the right nodesets that we
1571 * migrate mm from.
1572 */
Tejun Heoe93ad192016-01-19 12:18:41 -05001573 if (is_memory_migrate(cs))
Tejun Heo3df9ca02015-09-11 15:00:18 -04001574 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed,
1575 &cpuset_attach_nodemask_to);
Tejun Heoe93ad192016-01-19 12:18:41 -05001576 else
1577 mmput(mm);
Li Zefanf047cec2013-06-13 15:11:44 +08001578 }
Paul Jackson42253992006-01-08 01:01:59 -08001579 }
Tejun Heo452477f2013-01-07 08:51:07 -08001580
Li Zefan33ad8012013-06-09 17:15:08 +08001581 cs->old_mems_allowed = cpuset_attach_nodemask_to;
Tejun Heo02bb5862013-01-07 08:51:08 -08001582
Tejun Heo452477f2013-01-07 08:51:07 -08001583 cs->attach_in_progress--;
Li Zefane44193d2013-06-09 17:14:22 +08001584 if (!cs->attach_in_progress)
1585 wake_up(&cpuset_attach_wq);
Tejun Heo5d21cc22013-01-07 08:51:08 -08001586
1587 mutex_unlock(&cpuset_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590/* The various types of files and directories in a cpuset file system */
1591
1592typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001593 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 FILE_CPULIST,
1595 FILE_MEMLIST,
Li Zefanafd1a8b2014-07-09 16:49:25 +08001596 FILE_EFFECTIVE_CPULIST,
1597 FILE_EFFECTIVE_MEMLIST,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 FILE_CPU_EXCLUSIVE,
1599 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001600 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001601 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001602 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001603 FILE_MEMORY_PRESSURE_ENABLED,
1604 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001605 FILE_SPREAD_PAGE,
1606 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607} cpuset_filetype_t;
1608
Tejun Heo182446d2013-08-08 20:11:24 -04001609static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
1610 u64 val)
Paul Menage700fe1a2008-04-29 01:00:00 -07001611{
Tejun Heo182446d2013-08-08 20:11:24 -04001612 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07001613 cpuset_filetype_t type = cft->private;
Li Zefana903f082013-08-13 10:05:59 +08001614 int retval = 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001615
Tejun Heo5d21cc22013-01-07 08:51:08 -08001616 mutex_lock(&cpuset_mutex);
Li Zefana903f082013-08-13 10:05:59 +08001617 if (!is_cpuset_online(cs)) {
1618 retval = -ENODEV;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001619 goto out_unlock;
Li Zefana903f082013-08-13 10:05:59 +08001620 }
Paul Menage700fe1a2008-04-29 01:00:00 -07001621
1622 switch (type) {
1623 case FILE_CPU_EXCLUSIVE:
1624 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1625 break;
1626 case FILE_MEM_EXCLUSIVE:
1627 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1628 break;
Paul Menage78608362008-04-29 01:00:26 -07001629 case FILE_MEM_HARDWALL:
1630 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1631 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001632 case FILE_SCHED_LOAD_BALANCE:
1633 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1634 break;
1635 case FILE_MEMORY_MIGRATE:
1636 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1637 break;
1638 case FILE_MEMORY_PRESSURE_ENABLED:
1639 cpuset_memory_pressure_enabled = !!val;
1640 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001641 case FILE_SPREAD_PAGE:
1642 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001643 break;
1644 case FILE_SPREAD_SLAB:
1645 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001646 break;
1647 default:
1648 retval = -EINVAL;
1649 break;
1650 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08001651out_unlock:
1652 mutex_unlock(&cpuset_mutex);
Paul Menage700fe1a2008-04-29 01:00:00 -07001653 return retval;
1654}
1655
Tejun Heo182446d2013-08-08 20:11:24 -04001656static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
1657 s64 val)
Paul Menage5be7a472008-05-06 20:42:41 -07001658{
Tejun Heo182446d2013-08-08 20:11:24 -04001659 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07001660 cpuset_filetype_t type = cft->private;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001661 int retval = -ENODEV;
Paul Menage5be7a472008-05-06 20:42:41 -07001662
Tejun Heo5d21cc22013-01-07 08:51:08 -08001663 mutex_lock(&cpuset_mutex);
1664 if (!is_cpuset_online(cs))
1665 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07001666
Paul Menage5be7a472008-05-06 20:42:41 -07001667 switch (type) {
1668 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1669 retval = update_relax_domain_level(cs, val);
1670 break;
1671 default:
1672 retval = -EINVAL;
1673 break;
1674 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08001675out_unlock:
1676 mutex_unlock(&cpuset_mutex);
Paul Menage5be7a472008-05-06 20:42:41 -07001677 return retval;
1678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
Paul Menagee3712392008-07-25 01:47:02 -07001681 * Common handling for a write to a "cpus" or "mems" file.
1682 */
Tejun Heo451af502014-05-13 12:16:21 -04001683static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
1684 char *buf, size_t nbytes, loff_t off)
Paul Menagee3712392008-07-25 01:47:02 -07001685{
Tejun Heo451af502014-05-13 12:16:21 -04001686 struct cpuset *cs = css_cs(of_css(of));
Li Zefan645fcc92009-01-07 18:08:43 -08001687 struct cpuset *trialcs;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001688 int retval = -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001689
Tejun Heo451af502014-05-13 12:16:21 -04001690 buf = strstrip(buf);
1691
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001692 /*
1693 * CPU or memory hotunplug may leave @cs w/o any execution
1694 * resources, in which case the hotplug code asynchronously updates
1695 * configuration and transfers all tasks to the nearest ancestor
1696 * which can execute.
1697 *
1698 * As writes to "cpus" or "mems" may restore @cs's execution
1699 * resources, wait for the previously scheduled operations before
1700 * proceeding, so that we don't end up keep removing tasks added
1701 * after execution capability is restored.
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001702 *
1703 * cpuset_hotplug_work calls back into cgroup core via
1704 * cgroup_transfer_tasks() and waiting for it from a cgroupfs
1705 * operation like this one can lead to a deadlock through kernfs
1706 * active_ref protection. Let's break the protection. Losing the
1707 * protection is okay as we check whether @cs is online after
1708 * grabbing cpuset_mutex anyway. This only happens on the legacy
1709 * hierarchies.
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001710 */
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001711 css_get(&cs->css);
1712 kernfs_break_active_protection(of->kn);
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001713 flush_work(&cpuset_hotplug_work);
1714
Tejun Heo5d21cc22013-01-07 08:51:08 -08001715 mutex_lock(&cpuset_mutex);
1716 if (!is_cpuset_online(cs))
1717 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07001718
Li Zefan645fcc92009-01-07 18:08:43 -08001719 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001720 if (!trialcs) {
1721 retval = -ENOMEM;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001722 goto out_unlock;
Li Zefanb75f38d2011-03-04 17:36:21 -08001723 }
Li Zefan645fcc92009-01-07 18:08:43 -08001724
Tejun Heo451af502014-05-13 12:16:21 -04001725 switch (of_cft(of)->private) {
Paul Menagee3712392008-07-25 01:47:02 -07001726 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001727 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001728 break;
1729 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001730 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001731 break;
1732 default:
1733 retval = -EINVAL;
1734 break;
1735 }
Li Zefan645fcc92009-01-07 18:08:43 -08001736
1737 free_trial_cpuset(trialcs);
Tejun Heo5d21cc22013-01-07 08:51:08 -08001738out_unlock:
1739 mutex_unlock(&cpuset_mutex);
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001740 kernfs_unbreak_active_protection(of->kn);
1741 css_put(&cs->css);
Tejun Heoe93ad192016-01-19 12:18:41 -05001742 flush_workqueue(cpuset_migrate_mm_wq);
Tejun Heo451af502014-05-13 12:16:21 -04001743 return retval ?: nbytes;
Paul Menagee3712392008-07-25 01:47:02 -07001744}
1745
1746/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * These ascii lists should be read in a single call, by using a user
1748 * buffer large enough to hold the entire map. If read in smaller
1749 * chunks, there is no guarantee of atomicity. Since the display format
1750 * used, list of ranges of sequential numbers, is variable length,
1751 * and since these maps can change value dynamically, one could read
1752 * gibberish by doing partial reads while a list was changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001754static int cpuset_common_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001756 struct cpuset *cs = css_cs(seq_css(sf));
1757 cpuset_filetype_t type = seq_cft(sf)->private;
Tejun Heo51ffe412013-12-05 12:28:02 -05001758 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001760 spin_lock_irq(&callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 switch (type) {
1763 case FILE_CPULIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001764 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 break;
1766 case FILE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001767 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 break;
Li Zefanafd1a8b2014-07-09 16:49:25 +08001769 case FILE_EFFECTIVE_CPULIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001770 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus));
Li Zefanafd1a8b2014-07-09 16:49:25 +08001771 break;
1772 case FILE_EFFECTIVE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001773 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems));
Li Zefanafd1a8b2014-07-09 16:49:25 +08001774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 default:
Tejun Heo51ffe412013-12-05 12:28:02 -05001776 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001779 spin_unlock_irq(&callback_lock);
Tejun Heo51ffe412013-12-05 12:28:02 -05001780 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Tejun Heo182446d2013-08-08 20:11:24 -04001783static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage700fe1a2008-04-29 01:00:00 -07001784{
Tejun Heo182446d2013-08-08 20:11:24 -04001785 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07001786 cpuset_filetype_t type = cft->private;
1787 switch (type) {
1788 case FILE_CPU_EXCLUSIVE:
1789 return is_cpu_exclusive(cs);
1790 case FILE_MEM_EXCLUSIVE:
1791 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001792 case FILE_MEM_HARDWALL:
1793 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001794 case FILE_SCHED_LOAD_BALANCE:
1795 return is_sched_load_balance(cs);
1796 case FILE_MEMORY_MIGRATE:
1797 return is_memory_migrate(cs);
1798 case FILE_MEMORY_PRESSURE_ENABLED:
1799 return cpuset_memory_pressure_enabled;
1800 case FILE_MEMORY_PRESSURE:
1801 return fmeter_getrate(&cs->fmeter);
1802 case FILE_SPREAD_PAGE:
1803 return is_spread_page(cs);
1804 case FILE_SPREAD_SLAB:
1805 return is_spread_slab(cs);
1806 default:
1807 BUG();
1808 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001809
1810 /* Unreachable but makes gcc happy */
1811 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Tejun Heo182446d2013-08-08 20:11:24 -04001814static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage5be7a472008-05-06 20:42:41 -07001815{
Tejun Heo182446d2013-08-08 20:11:24 -04001816 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07001817 cpuset_filetype_t type = cft->private;
1818 switch (type) {
1819 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1820 return cs->relax_domain_level;
1821 default:
1822 BUG();
1823 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001824
1825 /* Unrechable but makes gcc happy */
1826 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830/*
1831 * for the common functions, 'private' gives the type of file
1832 */
1833
Paul Menageaddf2c72008-04-29 01:00:26 -07001834static struct cftype files[] = {
1835 {
1836 .name = "cpus",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001837 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04001838 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07001839 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001840 .private = FILE_CPULIST,
1841 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Paul Menageaddf2c72008-04-29 01:00:26 -07001843 {
1844 .name = "mems",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001845 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04001846 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07001847 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001848 .private = FILE_MEMLIST,
1849 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Paul Menageaddf2c72008-04-29 01:00:26 -07001851 {
Li Zefanafd1a8b2014-07-09 16:49:25 +08001852 .name = "effective_cpus",
1853 .seq_show = cpuset_common_seq_show,
1854 .private = FILE_EFFECTIVE_CPULIST,
1855 },
1856
1857 {
1858 .name = "effective_mems",
1859 .seq_show = cpuset_common_seq_show,
1860 .private = FILE_EFFECTIVE_MEMLIST,
1861 },
1862
1863 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001864 .name = "cpu_exclusive",
1865 .read_u64 = cpuset_read_u64,
1866 .write_u64 = cpuset_write_u64,
1867 .private = FILE_CPU_EXCLUSIVE,
1868 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Paul Menageaddf2c72008-04-29 01:00:26 -07001870 {
1871 .name = "mem_exclusive",
1872 .read_u64 = cpuset_read_u64,
1873 .write_u64 = cpuset_write_u64,
1874 .private = FILE_MEM_EXCLUSIVE,
1875 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Paul Menageaddf2c72008-04-29 01:00:26 -07001877 {
Paul Menage78608362008-04-29 01:00:26 -07001878 .name = "mem_hardwall",
1879 .read_u64 = cpuset_read_u64,
1880 .write_u64 = cpuset_write_u64,
1881 .private = FILE_MEM_HARDWALL,
1882 },
1883
1884 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001885 .name = "sched_load_balance",
1886 .read_u64 = cpuset_read_u64,
1887 .write_u64 = cpuset_write_u64,
1888 .private = FILE_SCHED_LOAD_BALANCE,
1889 },
Paul Jackson029190c2007-10-18 23:40:20 -07001890
Paul Menageaddf2c72008-04-29 01:00:26 -07001891 {
1892 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001893 .read_s64 = cpuset_read_s64,
1894 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001895 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1896 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001897
Paul Menageaddf2c72008-04-29 01:00:26 -07001898 {
1899 .name = "memory_migrate",
1900 .read_u64 = cpuset_read_u64,
1901 .write_u64 = cpuset_write_u64,
1902 .private = FILE_MEMORY_MIGRATE,
1903 },
1904
1905 {
1906 .name = "memory_pressure",
1907 .read_u64 = cpuset_read_u64,
Waiman Long309e4db2017-08-24 12:04:29 -04001908 .private = FILE_MEMORY_PRESSURE,
Paul Menageaddf2c72008-04-29 01:00:26 -07001909 },
1910
1911 {
1912 .name = "memory_spread_page",
1913 .read_u64 = cpuset_read_u64,
1914 .write_u64 = cpuset_write_u64,
1915 .private = FILE_SPREAD_PAGE,
1916 },
1917
1918 {
1919 .name = "memory_spread_slab",
1920 .read_u64 = cpuset_read_u64,
1921 .write_u64 = cpuset_write_u64,
1922 .private = FILE_SPREAD_SLAB,
1923 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07001924
1925 {
1926 .name = "memory_pressure_enabled",
1927 .flags = CFTYPE_ONLY_ON_ROOT,
1928 .read_u64 = cpuset_read_u64,
1929 .write_u64 = cpuset_write_u64,
1930 .private = FILE_MEMORY_PRESSURE_ENABLED,
1931 },
1932
1933 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001934};
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/*
Tejun Heo92fb9742012-11-19 08:13:38 -08001937 * cpuset_css_alloc - allocate a cpuset css
Li Zefanc9e5fe62013-06-14 11:18:27 +08001938 * cgrp: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
1940
Tejun Heoeb954192013-08-08 20:11:23 -04001941static struct cgroup_subsys_state *
1942cpuset_css_alloc(struct cgroup_subsys_state *parent_css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Tejun Heoc8f699b2013-01-07 08:51:07 -08001944 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Tejun Heoeb954192013-08-08 20:11:23 -04001946 if (!parent_css)
Paul Menage8793d852007-10-18 23:39:39 -07001947 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001948
Tejun Heoc8f699b2013-01-07 08:51:07 -08001949 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001951 return ERR_PTR(-ENOMEM);
Li Zefane2b9a3d2014-07-09 16:47:03 +08001952 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL))
1953 goto free_cs;
1954 if (!alloc_cpumask_var(&cs->effective_cpus, GFP_KERNEL))
1955 goto free_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Paul Jackson029190c2007-10-18 23:40:20 -07001957 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001958 cpumask_clear(cs->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001959 nodes_clear(cs->mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08001960 cpumask_clear(cs->effective_cpus);
1961 nodes_clear(cs->effective_mems);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001962 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001963 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Tejun Heoc8f699b2013-01-07 08:51:07 -08001965 return &cs->css;
Li Zefane2b9a3d2014-07-09 16:47:03 +08001966
1967free_cpus:
1968 free_cpumask_var(cs->cpus_allowed);
1969free_cs:
1970 kfree(cs);
1971 return ERR_PTR(-ENOMEM);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001972}
1973
Tejun Heoeb954192013-08-08 20:11:23 -04001974static int cpuset_css_online(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08001975{
Tejun Heoeb954192013-08-08 20:11:23 -04001976 struct cpuset *cs = css_cs(css);
Tejun Heoc4310692013-01-07 08:51:08 -08001977 struct cpuset *parent = parent_cs(cs);
Tejun Heoae8086c2013-01-07 08:51:07 -08001978 struct cpuset *tmp_cs;
Tejun Heo492eb212013-08-08 20:11:25 -04001979 struct cgroup_subsys_state *pos_css;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001980
1981 if (!parent)
1982 return 0;
1983
Tejun Heo5d21cc22013-01-07 08:51:08 -08001984 mutex_lock(&cpuset_mutex);
1985
Tejun Heoefeb77b2013-01-07 08:51:07 -08001986 set_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001987 if (is_spread_page(parent))
1988 set_bit(CS_SPREAD_PAGE, &cs->flags);
1989 if (is_spread_slab(parent))
1990 set_bit(CS_SPREAD_SLAB, &cs->flags);
1991
Mel Gorman664eedd2014-06-04 16:10:08 -07001992 cpuset_inc();
Tejun Heo033fa1c2012-11-19 08:13:39 -08001993
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001994 spin_lock_irq(&callback_lock);
Tejun Heo9e10a132015-09-18 11:56:28 -04001995 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
Li Zefane2b9a3d2014-07-09 16:47:03 +08001996 cpumask_copy(cs->effective_cpus, parent->effective_cpus);
1997 cs->effective_mems = parent->effective_mems;
1998 }
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001999 spin_unlock_irq(&callback_lock);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002000
Tejun Heoeb954192013-08-08 20:11:23 -04002001 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
Tejun Heo5d21cc22013-01-07 08:51:08 -08002002 goto out_unlock;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002003
2004 /*
2005 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
2006 * set. This flag handling is implemented in cgroup core for
2007 * histrical reasons - the flag may be specified during mount.
2008 *
2009 * Currently, if any sibling cpusets have exclusive cpus or mem, we
2010 * refuse to clone the configuration - thereby refusing the task to
2011 * be entered, and as a result refusing the sys_unshare() or
2012 * clone() which initiated it. If this becomes a problem for some
2013 * users who wish to allow that scenario, then this could be
2014 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2015 * (and likewise for mems) to the new cgroup.
2016 */
Tejun Heoae8086c2013-01-07 08:51:07 -08002017 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002018 cpuset_for_each_child(tmp_cs, pos_css, parent) {
Tejun Heoae8086c2013-01-07 08:51:07 -08002019 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
2020 rcu_read_unlock();
Tejun Heo5d21cc22013-01-07 08:51:08 -08002021 goto out_unlock;
Tejun Heoae8086c2013-01-07 08:51:07 -08002022 }
Tejun Heo033fa1c2012-11-19 08:13:39 -08002023 }
Tejun Heoae8086c2013-01-07 08:51:07 -08002024 rcu_read_unlock();
Tejun Heo033fa1c2012-11-19 08:13:39 -08002025
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002026 spin_lock_irq(&callback_lock);
Tejun Heo033fa1c2012-11-19 08:13:39 -08002027 cs->mems_allowed = parent->mems_allowed;
Zefan Li790317e2015-02-13 11:19:49 +08002028 cs->effective_mems = parent->mems_allowed;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002029 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
Zefan Li790317e2015-02-13 11:19:49 +08002030 cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
Dan Carpentercea74462014-10-27 16:27:02 +03002031 spin_unlock_irq(&callback_lock);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002032out_unlock:
2033 mutex_unlock(&cpuset_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002034 return 0;
2035}
2036
Zhao Hongjiang0b9e6962013-07-27 11:56:53 +08002037/*
2038 * If the cpuset being removed has its flag 'sched_load_balance'
2039 * enabled, then simulate turning sched_load_balance off, which
2040 * will call rebuild_sched_domains_locked().
2041 */
2042
Tejun Heoeb954192013-08-08 20:11:23 -04002043static void cpuset_css_offline(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08002044{
Tejun Heoeb954192013-08-08 20:11:23 -04002045 struct cpuset *cs = css_cs(css);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002046
Tejun Heo5d21cc22013-01-07 08:51:08 -08002047 mutex_lock(&cpuset_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002048
2049 if (is_sched_load_balance(cs))
2050 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
2051
Mel Gorman664eedd2014-06-04 16:10:08 -07002052 cpuset_dec();
Tejun Heoefeb77b2013-01-07 08:51:07 -08002053 clear_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002054
Tejun Heo5d21cc22013-01-07 08:51:08 -08002055 mutex_unlock(&cpuset_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Tejun Heoeb954192013-08-08 20:11:23 -04002058static void cpuset_css_free(struct cgroup_subsys_state *css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Tejun Heoeb954192013-08-08 20:11:23 -04002060 struct cpuset *cs = css_cs(css);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Li Zefane2b9a3d2014-07-09 16:47:03 +08002062 free_cpumask_var(cs->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -08002063 free_cpumask_var(cs->cpus_allowed);
Paul Menage8793d852007-10-18 23:39:39 -07002064 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Li Zefan39bd0d12014-07-09 16:48:01 +08002067static void cpuset_bind(struct cgroup_subsys_state *root_css)
2068{
2069 mutex_lock(&cpuset_mutex);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002070 spin_lock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002071
Tejun Heo9e10a132015-09-18 11:56:28 -04002072 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
Li Zefan39bd0d12014-07-09 16:48:01 +08002073 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
2074 top_cpuset.mems_allowed = node_possible_map;
2075 } else {
2076 cpumask_copy(top_cpuset.cpus_allowed,
2077 top_cpuset.effective_cpus);
2078 top_cpuset.mems_allowed = top_cpuset.effective_mems;
2079 }
2080
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002081 spin_unlock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002082 mutex_unlock(&cpuset_mutex);
2083}
2084
Zefan Li06f4e9482016-08-09 11:25:01 +08002085/*
2086 * Make sure the new task conform to the current state of its parent,
2087 * which could have been changed by cpuset just after it inherits the
2088 * state from the parent and before it sits on the cgroup's task list.
2089 */
Wei Yongjun8a15b812016-09-16 13:02:37 +00002090static void cpuset_fork(struct task_struct *task)
Zefan Li06f4e9482016-08-09 11:25:01 +08002091{
2092 if (task_css_is_root(task, cpuset_cgrp_id))
2093 return;
2094
2095 set_cpus_allowed_ptr(task, &current->cpus_allowed);
2096 task->mems_allowed = current->mems_allowed;
2097}
2098
Tejun Heo073219e2014-02-08 10:36:58 -05002099struct cgroup_subsys cpuset_cgrp_subsys = {
Li Zefan39bd0d12014-07-09 16:48:01 +08002100 .css_alloc = cpuset_css_alloc,
2101 .css_online = cpuset_css_online,
2102 .css_offline = cpuset_css_offline,
2103 .css_free = cpuset_css_free,
2104 .can_attach = cpuset_can_attach,
2105 .cancel_attach = cpuset_cancel_attach,
2106 .attach = cpuset_attach,
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002107 .post_attach = cpuset_post_attach,
Li Zefan39bd0d12014-07-09 16:48:01 +08002108 .bind = cpuset_bind,
Zefan Li06f4e9482016-08-09 11:25:01 +08002109 .fork = cpuset_fork,
Tejun Heo55779642014-07-15 11:05:09 -04002110 .legacy_cftypes = files,
Tejun Heob38e42e2016-02-23 10:00:50 -05002111 .early_init = true,
Paul Menage8793d852007-10-18 23:39:39 -07002112};
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/**
2115 * cpuset_init - initialize cpusets at system boot
2116 *
2117 * Description: Initialize top_cpuset and the cpuset internal file system,
2118 **/
2119
2120int __init cpuset_init(void)
2121{
Paul Menage8793d852007-10-18 23:39:39 -07002122 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Miao Xie58568d22009-06-16 15:31:49 -07002124 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
2125 BUG();
Li Zefane2b9a3d2014-07-09 16:47:03 +08002126 if (!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL))
2127 BUG();
Miao Xie58568d22009-06-16 15:31:49 -07002128
Li Zefan300ed6c2009-01-07 18:08:44 -08002129 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002130 nodes_setall(top_cpuset.mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002131 cpumask_setall(top_cpuset.effective_cpus);
2132 nodes_setall(top_cpuset.effective_mems);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002134 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07002135 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002136 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 err = register_filesystem(&cpuset_fs_type);
2139 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07002140 return err;
2141
Li Zefan2341d1b2009-01-07 18:08:42 -08002142 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
2143 BUG();
2144
Paul Menage8793d852007-10-18 23:39:39 -07002145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002148/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002149 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002150 * or memory nodes, we need to walk over the cpuset hierarchy,
2151 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002152 * last CPU or node from a cpuset, then move the tasks in the empty
2153 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002154 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002155static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002156{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002157 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002158
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002159 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08002160 * Find its next-highest non-empty parent, (top cpuset
2161 * has online cpus, so can't be empty).
2162 */
Tejun Heoc4310692013-01-07 08:51:08 -08002163 parent = parent_cs(cs);
Li Zefan300ed6c2009-01-07 18:08:44 -08002164 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002165 nodes_empty(parent->mems_allowed))
Tejun Heoc4310692013-01-07 08:51:08 -08002166 parent = parent_cs(parent);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002167
Tejun Heo8cc99342013-04-07 09:29:50 -07002168 if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) {
Fabian Frederick12d30892014-05-05 19:49:00 +02002169 pr_err("cpuset: failed to transfer tasks out of empty cpuset ");
Tejun Heoe61734c2014-02-12 09:29:50 -05002170 pr_cont_cgroup_name(cs->css.cgroup);
2171 pr_cont("\n");
Tejun Heo8cc99342013-04-07 09:29:50 -07002172 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002173}
2174
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002175static void
2176hotplug_update_tasks_legacy(struct cpuset *cs,
2177 struct cpumask *new_cpus, nodemask_t *new_mems,
2178 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002179{
2180 bool is_empty;
2181
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002182 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002183 cpumask_copy(cs->cpus_allowed, new_cpus);
2184 cpumask_copy(cs->effective_cpus, new_cpus);
2185 cs->mems_allowed = *new_mems;
2186 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002187 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08002188
2189 /*
2190 * Don't call update_tasks_cpumask() if the cpuset becomes empty,
2191 * as the tasks will be migratecd to an ancestor.
2192 */
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002193 if (cpus_updated && !cpumask_empty(cs->cpus_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08002194 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002195 if (mems_updated && !nodes_empty(cs->mems_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08002196 update_tasks_nodemask(cs);
2197
2198 is_empty = cpumask_empty(cs->cpus_allowed) ||
2199 nodes_empty(cs->mems_allowed);
2200
2201 mutex_unlock(&cpuset_mutex);
2202
2203 /*
2204 * Move tasks to the nearest ancestor with execution resources,
2205 * This is full cgroup operation which will also call back into
2206 * cpuset. Should be done outside any lock.
2207 */
2208 if (is_empty)
2209 remove_tasks_in_empty_cpuset(cs);
2210
2211 mutex_lock(&cpuset_mutex);
2212}
2213
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002214static void
2215hotplug_update_tasks(struct cpuset *cs,
2216 struct cpumask *new_cpus, nodemask_t *new_mems,
2217 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002218{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002219 if (cpumask_empty(new_cpus))
2220 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus);
2221 if (nodes_empty(*new_mems))
2222 *new_mems = parent_cs(cs)->effective_mems;
Li Zefan390a36a2014-07-09 16:48:54 +08002223
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002224 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002225 cpumask_copy(cs->effective_cpus, new_cpus);
2226 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002227 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08002228
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002229 if (cpus_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002230 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002231 if (mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002232 update_tasks_nodemask(cs);
2233}
2234
Tejun Heodeb7aa32013-01-07 08:51:07 -08002235/**
Li Zefan388afd82013-06-09 17:14:47 +08002236 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
Tejun Heodeb7aa32013-01-07 08:51:07 -08002237 * @cs: cpuset in interest
Cliff Wickman956db3c2008-02-07 00:14:43 -08002238 *
Tejun Heodeb7aa32013-01-07 08:51:07 -08002239 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2240 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2241 * all its tasks are moved to the nearest ancestor with both resources.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002242 */
Li Zefan388afd82013-06-09 17:14:47 +08002243static void cpuset_hotplug_update_tasks(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002244{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002245 static cpumask_t new_cpus;
2246 static nodemask_t new_mems;
2247 bool cpus_updated;
2248 bool mems_updated;
Li Zefane44193d2013-06-09 17:14:22 +08002249retry:
2250 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002251
Tejun Heo5d21cc22013-01-07 08:51:08 -08002252 mutex_lock(&cpuset_mutex);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002253
Li Zefane44193d2013-06-09 17:14:22 +08002254 /*
2255 * We have raced with task attaching. We wait until attaching
2256 * is finished, so we won't attach a task to an empty cpuset.
2257 */
2258 if (cs->attach_in_progress) {
2259 mutex_unlock(&cpuset_mutex);
2260 goto retry;
2261 }
2262
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002263 cpumask_and(&new_cpus, cs->cpus_allowed, parent_cs(cs)->effective_cpus);
2264 nodes_and(new_mems, cs->mems_allowed, parent_cs(cs)->effective_mems);
Paul Jacksonb4501292008-02-07 00:14:47 -08002265
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002266 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
2267 mems_updated = !nodes_equal(new_mems, cs->effective_mems);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002268
Tejun Heo9e10a132015-09-18 11:56:28 -04002269 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys))
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002270 hotplug_update_tasks(cs, &new_cpus, &new_mems,
2271 cpus_updated, mems_updated);
Li Zefan390a36a2014-07-09 16:48:54 +08002272 else
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002273 hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems,
2274 cpus_updated, mems_updated);
Tejun Heo8d033942013-01-07 08:51:07 -08002275
Tejun Heo5d21cc22013-01-07 08:51:08 -08002276 mutex_unlock(&cpuset_mutex);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002277}
Paul Jacksonb4501292008-02-07 00:14:47 -08002278
Peter Zijlstraba155182017-09-07 11:13:38 +02002279static bool force_rebuild;
2280
2281void cpuset_force_rebuild(void)
2282{
2283 force_rebuild = true;
2284}
2285
Tejun Heodeb7aa32013-01-07 08:51:07 -08002286/**
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002287 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
Tejun Heodeb7aa32013-01-07 08:51:07 -08002288 *
2289 * This function is called after either CPU or memory configuration has
2290 * changed and updates cpuset accordingly. The top_cpuset is always
2291 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2292 * order to make cpusets transparent (of no affect) on systems that are
2293 * actively using CPU hotplug but making no active use of cpusets.
2294 *
2295 * Non-root cpusets are only affected by offlining. If any CPUs or memory
Li Zefan388afd82013-06-09 17:14:47 +08002296 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
2297 * all descendants.
Tejun Heodeb7aa32013-01-07 08:51:07 -08002298 *
2299 * Note that CPU offlining during suspend is ignored. We don't modify
2300 * cpusets across suspend/resume cycles at all.
2301 */
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002302static void cpuset_hotplug_workfn(struct work_struct *work)
Tejun Heodeb7aa32013-01-07 08:51:07 -08002303{
Li Zefan5c5cc622013-06-09 17:16:29 +08002304 static cpumask_t new_cpus;
2305 static nodemask_t new_mems;
Tejun Heodeb7aa32013-01-07 08:51:07 -08002306 bool cpus_updated, mems_updated;
Tejun Heo9e10a132015-09-18 11:56:28 -04002307 bool on_dfl = cgroup_subsys_on_dfl(cpuset_cgrp_subsys);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002308
Tejun Heo5d21cc22013-01-07 08:51:08 -08002309 mutex_lock(&cpuset_mutex);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002310
Tejun Heodeb7aa32013-01-07 08:51:07 -08002311 /* fetch the available cpus/mems and find out which changed how */
2312 cpumask_copy(&new_cpus, cpu_active_mask);
2313 new_mems = node_states[N_MEMORY];
Cliff Wickman956db3c2008-02-07 00:14:43 -08002314
Li Zefan7e882912014-07-09 16:48:42 +08002315 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus);
2316 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302317
Tejun Heodeb7aa32013-01-07 08:51:07 -08002318 /* synchronize cpus_allowed to cpu_active_mask */
2319 if (cpus_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002320 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08002321 if (!on_dfl)
2322 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
Li Zefan1344ab92014-07-09 16:47:16 +08002323 cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002324 spin_unlock_irq(&callback_lock);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002325 /* we don't mess with cpumasks of tasks in top_cpuset */
2326 }
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302327
Tejun Heodeb7aa32013-01-07 08:51:07 -08002328 /* synchronize mems_allowed to N_MEMORY */
2329 if (mems_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002330 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08002331 if (!on_dfl)
2332 top_cpuset.mems_allowed = new_mems;
Li Zefan1344ab92014-07-09 16:47:16 +08002333 top_cpuset.effective_mems = new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002334 spin_unlock_irq(&callback_lock);
Tejun Heod66393e2014-02-13 06:58:40 -05002335 update_tasks_nodemask(&top_cpuset);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002336 }
2337
Tejun Heo5d21cc22013-01-07 08:51:08 -08002338 mutex_unlock(&cpuset_mutex);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002339
Li Zefan5c5cc622013-06-09 17:16:29 +08002340 /* if cpus or mems changed, we need to propagate to descendants */
2341 if (cpus_updated || mems_updated) {
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002342 struct cpuset *cs;
Tejun Heo492eb212013-08-08 20:11:25 -04002343 struct cgroup_subsys_state *pos_css;
Paul Jacksonb4501292008-02-07 00:14:47 -08002344
Paul Jacksonb4501292008-02-07 00:14:47 -08002345 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002346 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
Tejun Heoec903c02014-05-13 12:11:01 -04002347 if (cs == &top_cpuset || !css_tryget_online(&cs->css))
Li Zefan388afd82013-06-09 17:14:47 +08002348 continue;
2349 rcu_read_unlock();
2350
2351 cpuset_hotplug_update_tasks(cs);
2352
2353 rcu_read_lock();
2354 css_put(&cs->css);
2355 }
Tejun Heodeb7aa32013-01-07 08:51:07 -08002356 rcu_read_unlock();
2357 }
Tejun Heo8d033942013-01-07 08:51:07 -08002358
Tejun Heodeb7aa32013-01-07 08:51:07 -08002359 /* rebuild sched domains if cpus_allowed has changed */
Peter Zijlstraba155182017-09-07 11:13:38 +02002360 if (cpus_updated || force_rebuild) {
2361 force_rebuild = false;
Li Zhonge0e80a02013-04-27 06:52:43 -07002362 rebuild_sched_domains();
Peter Zijlstraba155182017-09-07 11:13:38 +02002363 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002364}
2365
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302366void cpuset_update_active_cpus(bool cpu_online)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002367{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002368 /*
2369 * We're inside cpu hotplug critical region which usually nests
2370 * inside cgroup synchronization. Bounce actual hotplug processing
2371 * to a work item to avoid reverse locking order.
2372 *
2373 * We still need to do partition_sched_domains() synchronously;
2374 * otherwise, the scheduler will get confused and put tasks to the
2375 * dead CPU. Fall back to the default single domain.
2376 * cpuset_hotplug_workfn() will rebuild it as necessary.
2377 */
2378 partition_sched_domains(1, NULL, NULL);
2379 schedule_work(&cpuset_hotplug_work);
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002380}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002381
Peter Zijlstraba155182017-09-07 11:13:38 +02002382void cpuset_wait_for_hotplug(void)
2383{
2384 flush_work(&cpuset_hotplug_work);
2385}
2386
Paul Jackson38837fc2006-09-29 02:01:16 -07002387/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002388 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2389 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302390 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07002391 */
Miao Xief4818912008-11-19 15:36:30 -08002392static int cpuset_track_online_nodes(struct notifier_block *self,
2393 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002394{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002395 schedule_work(&cpuset_hotplug_work);
Miao Xief4818912008-11-19 15:36:30 -08002396 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002397}
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002398
2399static struct notifier_block cpuset_track_online_nodes_nb = {
2400 .notifier_call = cpuset_track_online_nodes,
2401 .priority = 10, /* ??! */
2402};
Paul Jackson38837fc2006-09-29 02:01:16 -07002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404/**
2405 * cpuset_init_smp - initialize cpus_allowed
2406 *
2407 * Description: Finish top cpuset after cpu, node maps are initialized
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409void __init cpuset_init_smp(void)
2410{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002411 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002412 top_cpuset.mems_allowed = node_states[N_MEMORY];
Li Zefan33ad8012013-06-09 17:15:08 +08002413 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002414
Li Zefane2b9a3d2014-07-09 16:47:03 +08002415 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask);
2416 top_cpuset.effective_mems = node_states[N_MEMORY];
2417
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002418 register_hotmemory_notifier(&cpuset_track_online_nodes_nb);
Tejun Heoe93ad192016-01-19 12:18:41 -05002419
2420 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0);
2421 BUG_ON(!cpuset_migrate_mm_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2426 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002427 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002429 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10302431 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 * tasks cpuset.
2433 **/
2434
Li Zefan6af866a2009-01-07 18:08:45 -08002435void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002437 unsigned long flags;
2438
2439 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002440 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002441 guarantee_online_cpus(task_cs(tsk), pmask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002442 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002443 spin_unlock_irqrestore(&callback_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002446void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002447{
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002448 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002449 do_set_cpus_allowed(tsk, task_cs(tsk)->effective_cpus);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002450 rcu_read_unlock();
2451
2452 /*
2453 * We own tsk->cpus_allowed, nobody can change it under us.
2454 *
2455 * But we used cs && cs->cpus_allowed lockless and thus can
2456 * race with cgroup_attach_task() or update_cpumask() and get
2457 * the wrong tsk->cpus_allowed. However, both cases imply the
2458 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2459 * which takes task_rq_lock().
2460 *
2461 * If we are called after it dropped the lock we must see all
2462 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2463 * set any mask even if it is not right from task_cs() pov,
2464 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002465 *
2466 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2467 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002468 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002469}
2470
Rasmus Villemoes8f4ab072015-02-12 15:00:16 -08002471void __init cpuset_init_current_mems_allowed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002473 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002476/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002477 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2478 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2479 *
2480 * Description: Returns the nodemask_t mems_allowed of the cpuset
2481 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002482 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002483 * tasks cpuset.
2484 **/
2485
2486nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2487{
2488 nodemask_t mask;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002489 unsigned long flags;
Paul Jackson909d75a2006-01-08 01:01:55 -08002490
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002491 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002492 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002493 guarantee_online_mems(task_cs(tsk), &mask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002494 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002495 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson909d75a2006-01-08 01:01:55 -08002496
2497 return mask;
2498}
2499
2500/**
Mel Gorman19770b32008-04-28 02:12:18 -07002501 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2502 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002503 *
Mel Gorman19770b32008-04-28 02:12:18 -07002504 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
Mel Gorman19770b32008-04-28 02:12:18 -07002506int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Mel Gorman19770b32008-04-28 02:12:18 -07002508 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Paul Jackson9bf22292005-09-06 15:18:12 -07002511/*
Paul Menage78608362008-04-29 01:00:26 -07002512 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2513 * mem_hardwall ancestor to the specified cpuset. Call holding
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002514 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
Paul Menage78608362008-04-29 01:00:26 -07002515 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
Tejun Heoc9710d82013-08-08 20:11:22 -04002517static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Tejun Heoc4310692013-01-07 08:51:08 -08002519 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs))
2520 cs = parent_cs(cs);
Paul Jackson9bf22292005-09-06 15:18:12 -07002521 return cs;
2522}
2523
2524/**
Vladimir Davydov344736f2014-10-20 15:50:30 +04002525 * cpuset_node_allowed - Can we allocate on a memory node?
David Rientjesa1bc5a42009-04-02 16:57:54 -07002526 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002527 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002528 *
David Rientjes6e276d22015-04-14 15:47:01 -07002529 * If we're in interrupt, yes, we can always allocate. If @node is set in
2530 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
2531 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
2532 * yes. If current has access to memory reserves due to TIF_MEMDIE, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002533 * Otherwise, no.
2534 *
2535 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002536 * and do not allow allocations outside the current tasks cpuset
2537 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002538 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002539 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002540 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002541 * Scanning up parent cpusets requires callback_lock. The
Paul Jackson02a0e532006-12-13 00:34:25 -08002542 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2543 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2544 * current tasks mems_allowed came up empty on the first pass over
2545 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002546 * cpuset are short of memory, might require taking the callback_lock.
Paul Jackson9bf22292005-09-06 15:18:12 -07002547 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002548 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002549 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2550 * so no allocation on a node outside the cpuset is allowed (unless
2551 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002552 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002553 * The second pass through get_page_from_freelist() doesn't even call
2554 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2555 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2556 * in alloc_flags. That logic and the checks below have the combined
2557 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002558 * in_interrupt - any node ok (current task context irrelevant)
2559 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002560 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002561 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002562 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson02a0e532006-12-13 00:34:25 -08002563 */
Vlastimil Babka002f2902016-05-19 17:14:30 -07002564bool __cpuset_node_allowed(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002565{
Tejun Heoc9710d82013-08-08 20:11:22 -04002566 struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002567 int allowed; /* is allocation in zone z allowed? */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002568 unsigned long flags;
Paul Jackson9bf22292005-09-06 15:18:12 -07002569
David Rientjes6e276d22015-04-14 15:47:01 -07002570 if (in_interrupt())
Vlastimil Babka002f2902016-05-19 17:14:30 -07002571 return true;
Paul Jackson9bf22292005-09-06 15:18:12 -07002572 if (node_isset(node, current->mems_allowed))
Vlastimil Babka002f2902016-05-19 17:14:30 -07002573 return true;
David Rientjesc596d9f2007-05-06 14:49:32 -07002574 /*
2575 * Allow tasks that have access to memory reserves because they have
2576 * been OOM killed to get memory anywhere.
2577 */
2578 if (unlikely(test_thread_flag(TIF_MEMDIE)))
Vlastimil Babka002f2902016-05-19 17:14:30 -07002579 return true;
Paul Jackson9bf22292005-09-06 15:18:12 -07002580 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
Vlastimil Babka002f2902016-05-19 17:14:30 -07002581 return false;
Paul Jackson9bf22292005-09-06 15:18:12 -07002582
Bob Picco5563e772005-11-13 16:06:35 -08002583 if (current->flags & PF_EXITING) /* Let dying task have memory */
Vlastimil Babka002f2902016-05-19 17:14:30 -07002584 return true;
Bob Picco5563e772005-11-13 16:06:35 -08002585
Paul Jackson9bf22292005-09-06 15:18:12 -07002586 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002587 spin_lock_irqsave(&callback_lock, flags);
Paul Jackson053199e2005-10-30 15:02:30 -08002588
Li Zefanb8dadcb2014-03-03 17:28:36 -05002589 rcu_read_lock();
Paul Menage78608362008-04-29 01:00:26 -07002590 cs = nearest_hardwall_ancestor(task_cs(current));
Li Zefan99afb0f2014-02-27 18:19:36 +08002591 allowed = node_isset(node, cs->mems_allowed);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002592 rcu_read_unlock();
Paul Jackson053199e2005-10-30 15:02:30 -08002593
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002594 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson9bf22292005-09-06 15:18:12 -07002595 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002598/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07002599 * cpuset_mem_spread_node() - On which node to begin search for a file page
2600 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08002601 *
2602 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2603 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2604 * and if the memory allocation used cpuset_mem_spread_node()
2605 * to determine on which node to start looking, as it will for
2606 * certain page cache or slab cache pages such as used for file
2607 * system buffers and inode caches, then instead of starting on the
2608 * local node to look for a free page, rather spread the starting
2609 * node around the tasks mems_allowed nodes.
2610 *
2611 * We don't have to worry about the returned node being offline
2612 * because "it can't happen", and even if it did, it would be ok.
2613 *
2614 * The routines calling guarantee_online_mems() are careful to
2615 * only set nodes in task->mems_allowed that are online. So it
2616 * should not be possible for the following code to return an
2617 * offline node. But if it did, that would be ok, as this routine
2618 * is not returning the node where the allocation must be, only
2619 * the node where the search should start. The zonelist passed to
2620 * __alloc_pages() will include all nodes. If the slab allocator
2621 * is passed an offline node, it will fall back to the local node.
2622 * See kmem_cache_alloc_node().
2623 */
2624
Jack Steiner6adef3e2010-05-26 14:42:49 -07002625static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08002626{
Andrew Morton0edaf862016-05-19 17:10:58 -07002627 return *rotor = next_node_in(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08002628}
Jack Steiner6adef3e2010-05-26 14:42:49 -07002629
2630int cpuset_mem_spread_node(void)
2631{
Michal Hocko778d3b02011-07-26 16:08:30 -07002632 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2633 current->cpuset_mem_spread_rotor =
2634 node_random(&current->mems_allowed);
2635
Jack Steiner6adef3e2010-05-26 14:42:49 -07002636 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2637}
2638
2639int cpuset_slab_spread_node(void)
2640{
Michal Hocko778d3b02011-07-26 16:08:30 -07002641 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2642 current->cpuset_slab_spread_rotor =
2643 node_random(&current->mems_allowed);
2644
Jack Steiner6adef3e2010-05-26 14:42:49 -07002645 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2646}
2647
Paul Jackson825a46a2006-03-24 03:16:03 -08002648EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2649
2650/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002651 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2652 * @tsk1: pointer to task_struct of some task.
2653 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002654 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002655 * Description: Return true if @tsk1's mems_allowed intersects the
2656 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2657 * one of the task's memory usage might impact the memory available
2658 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002659 **/
2660
David Rientjesbbe373f2007-10-16 23:25:58 -07002661int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2662 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002663{
David Rientjesbbe373f2007-10-16 23:25:58 -07002664 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002665}
2666
David Rientjes75aa1992009-01-06 14:39:01 -08002667/**
David Rientjesda39da32015-11-05 18:48:05 -08002668 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
David Rientjes75aa1992009-01-06 14:39:01 -08002669 *
David Rientjesda39da32015-11-05 18:48:05 -08002670 * Description: Prints current's name, cpuset name, and cached copy of its
Li Zefanb8dadcb2014-03-03 17:28:36 -05002671 * mems_allowed to the kernel log.
David Rientjes75aa1992009-01-06 14:39:01 -08002672 */
David Rientjesda39da32015-11-05 18:48:05 -08002673void cpuset_print_current_mems_allowed(void)
David Rientjes75aa1992009-01-06 14:39:01 -08002674{
Li Zefanb8dadcb2014-03-03 17:28:36 -05002675 struct cgroup *cgrp;
David Rientjes75aa1992009-01-06 14:39:01 -08002676
Li Zefanb8dadcb2014-03-03 17:28:36 -05002677 rcu_read_lock();
Li Zefan63f43f52013-01-25 16:08:01 +08002678
David Rientjesda39da32015-11-05 18:48:05 -08002679 cgrp = task_cs(current)->css.cgroup;
2680 pr_info("%s cpuset=", current->comm);
Tejun Heoe61734c2014-02-12 09:29:50 -05002681 pr_cont_cgroup_name(cgrp);
David Rientjesda39da32015-11-05 18:48:05 -08002682 pr_cont(" mems_allowed=%*pbl\n",
2683 nodemask_pr_args(&current->mems_allowed));
Li Zefanf440d982013-03-01 15:02:15 +08002684
Li Zefancfb59662013-03-12 10:28:39 +08002685 rcu_read_unlock();
David Rientjes75aa1992009-01-06 14:39:01 -08002686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002689 * Collection of memory_pressure is suppressed unless
2690 * this flag is enabled by writing "1" to the special
2691 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2692 */
2693
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002694int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002695
2696/**
2697 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2698 *
2699 * Keep a running average of the rate of synchronous (direct)
2700 * page reclaim efforts initiated by tasks in each cpuset.
2701 *
2702 * This represents the rate at which some task in the cpuset
2703 * ran low on memory on all nodes it was allowed to use, and
2704 * had to enter the kernels page reclaim code in an effort to
2705 * create more free memory by tossing clean pages or swapping
2706 * or writing dirty pages.
2707 *
2708 * Display to user space in the per-cpuset read-only file
2709 * "memory_pressure". Value displayed is an integer
2710 * representing the recent rate of entry into the synchronous
2711 * (direct) page reclaim by any task attached to the cpuset.
2712 **/
2713
2714void __cpuset_memory_pressure_bump(void)
2715{
Li Zefanb8dadcb2014-03-03 17:28:36 -05002716 rcu_read_lock();
Paul Menage8793d852007-10-18 23:39:39 -07002717 fmeter_markevent(&task_cs(current)->fmeter);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002718 rcu_read_unlock();
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002719}
2720
Paul Menage8793d852007-10-18 23:39:39 -07002721#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002722/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * proc_cpuset_show()
2724 * - Print tasks cpuset path into seq_file.
2725 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002726 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2727 * doesn't really matter if tsk->cpuset changes after we read it,
Tejun Heo5d21cc22013-01-07 08:51:08 -08002728 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002729 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
Zefan Li52de4772014-09-18 16:03:36 +08002731int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
2732 struct pid *pid, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Tejun Heo4c737b42016-08-10 11:23:44 -04002734 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07002735 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002736 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Eric W. Biederman99f89552006-06-26 00:25:55 -07002738 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05002739 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Aditya Kalia79a9082016-01-29 02:54:06 -06002743 css = task_get_css(tsk, cpuset_cgrp_id);
Tejun Heo4c737b42016-08-10 11:23:44 -04002744 retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX,
2745 current->nsproxy->cgroup_ns);
Aditya Kalia79a9082016-01-29 02:54:06 -06002746 css_put(css);
Tejun Heo4c737b42016-08-10 11:23:44 -04002747 if (retval >= PATH_MAX)
Tejun Heo679a5e32016-09-29 11:58:36 +02002748 retval = -ENAMETOOLONG;
2749 if (retval < 0)
Zefan Li52de4772014-09-18 16:03:36 +08002750 goto out_free;
Tejun Heo4c737b42016-08-10 11:23:44 -04002751 seq_puts(m, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 seq_putc(m, '\n');
Tejun Heoe61734c2014-02-12 09:29:50 -05002753 retval = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002754out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002756out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return retval;
2758}
Paul Menage8793d852007-10-18 23:39:39 -07002759#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Heiko Carstensd01d4822009-09-21 11:06:27 +02002761/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002762void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Tejun Heoe8e6d972015-02-13 14:37:23 -08002764 seq_printf(m, "Mems_allowed:\t%*pb\n",
2765 nodemask_pr_args(&task->mems_allowed));
2766 seq_printf(m, "Mems_allowed_list:\t%*pbl\n",
2767 nodemask_pr_args(&task->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}