blob: 7442e08d37ea5f35065303db5c8bb963c7c8b953 [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>
54#include <linux/backing-dev.h>
55#include <linux/sort.h>
56
57#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070058#include <linux/atomic.h>
Ingo Molnar3d3f26a2006-03-23 03:00:18 -080059#include <linux/mutex.h>
Cliff Wickman956db3c2008-02-07 00:14:43 -080060#include <linux/cgroup.h>
Li Zefane44193d2013-06-09 17:14:22 +080061#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Dima Zavin97e37142017-08-02 13:32:18 -070063struct static_key cpusets_pre_enable_key __read_mostly = STATIC_KEY_INIT_FALSE;
Mel Gorman664eedd2014-06-04 16:10:08 -070064struct static_key cpusets_enabled_key __read_mostly = STATIC_KEY_INIT_FALSE;
Paul Jackson202f72d2006-01-08 01:01:57 -080065
Paul Jackson3e0d98b2006-01-08 01:01:49 -080066/* See "Frequency meter" comments, below. */
67
68struct fmeter {
69 int cnt; /* unprocessed events count */
70 int val; /* most recent output value */
71 time_t time; /* clock (secs) when val computed */
72 spinlock_t lock; /* guards read or write of above */
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct cpuset {
Paul Menage8793d852007-10-18 23:39:39 -070076 struct cgroup_subsys_state css;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned long flags; /* "unsigned long" so bitops work */
Li Zefane2b9a3d2014-07-09 16:47:03 +080079
Li Zefan7e882912014-07-09 16:48:42 +080080 /*
81 * On default hierarchy:
82 *
83 * The user-configured masks can only be changed by writing to
84 * cpuset.cpus and cpuset.mems, and won't be limited by the
85 * parent masks.
86 *
87 * The effective masks is the real masks that apply to the tasks
88 * in the cpuset. They may be changed if the configured masks are
89 * changed or hotplug happens.
90 *
91 * effective_mask == configured_mask & parent's effective_mask,
92 * and if it ends up empty, it will inherit the parent's mask.
93 *
94 *
95 * On legacy hierachy:
96 *
97 * The user-configured masks are always the same with effective masks.
98 */
99
Li Zefane2b9a3d2014-07-09 16:47:03 +0800100 /* user-configured CPUs and Memory Nodes allow to tasks */
101 cpumask_var_t cpus_allowed;
Riley Andrews7ec5a322016-09-06 15:16:25 -0700102 cpumask_var_t cpus_requested;
Li Zefane2b9a3d2014-07-09 16:47:03 +0800103 nodemask_t mems_allowed;
104
105 /* effective CPUs and Memory Nodes allow to tasks */
106 cpumask_var_t effective_cpus;
107 nodemask_t effective_mems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Li Zefan33ad8012013-06-09 17:15:08 +0800109 /*
110 * This is old Memory Nodes tasks took on.
111 *
112 * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
113 * - A new cpuset's old_mems_allowed is initialized when some
114 * task is moved into it.
115 * - old_mems_allowed is used in cpuset_migrate_mm() when we change
116 * cpuset.mems_allowed and have tasks' nodemask updated, and
117 * then old_mems_allowed is updated to mems_allowed.
118 */
119 nodemask_t old_mems_allowed;
120
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800121 struct fmeter fmeter; /* memory_pressure filter */
Paul Jackson029190c2007-10-18 23:40:20 -0700122
Tejun Heo452477f2013-01-07 08:51:07 -0800123 /*
124 * Tasks are being attached to this cpuset. Used to prevent
125 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
126 */
127 int attach_in_progress;
128
Paul Jackson029190c2007-10-18 23:40:20 -0700129 /* partition number for rebuild_sched_domains() */
130 int pn;
Cliff Wickman956db3c2008-02-07 00:14:43 -0800131
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900132 /* for custom sched domain */
133 int relax_domain_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Tejun Heoa7c6d552013-08-08 20:11:23 -0400136static inline struct cpuset *css_cs(struct cgroup_subsys_state *css)
Paul Menage8793d852007-10-18 23:39:39 -0700137{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400138 return css ? container_of(css, struct cpuset, css) : NULL;
Paul Menage8793d852007-10-18 23:39:39 -0700139}
140
141/* Retrieve the cpuset for a task */
142static inline struct cpuset *task_cs(struct task_struct *task)
143{
Tejun Heo073219e2014-02-08 10:36:58 -0500144 return css_cs(task_css(task, cpuset_cgrp_id));
Paul Menage8793d852007-10-18 23:39:39 -0700145}
Paul Menage8793d852007-10-18 23:39:39 -0700146
Tejun Heoc9710d82013-08-08 20:11:22 -0400147static inline struct cpuset *parent_cs(struct cpuset *cs)
Tejun Heoc4310692013-01-07 08:51:08 -0800148{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400149 return css_cs(cs->css.parent);
Tejun Heoc4310692013-01-07 08:51:08 -0800150}
151
David Rientjesb2462722011-12-19 17:11:52 -0800152#ifdef CONFIG_NUMA
153static inline bool task_has_mempolicy(struct task_struct *task)
154{
155 return task->mempolicy;
156}
157#else
158static inline bool task_has_mempolicy(struct task_struct *task)
159{
160 return false;
161}
162#endif
163
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* bits in struct cpuset flags field */
166typedef enum {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800167 CS_ONLINE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 CS_CPU_EXCLUSIVE,
169 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700170 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800171 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700172 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800173 CS_SPREAD_PAGE,
174 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175} cpuset_flagbits_t;
176
177/* convenient tests for these bits */
Tejun Heoc8acec92017-05-24 12:03:48 -0400178static inline bool is_cpuset_online(struct cpuset *cs)
Tejun Heoefeb77b2013-01-07 08:51:07 -0800179{
Tejun Heoc8acec92017-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 Heofff4dc82016-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 Park4b36fab2016-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 Park4b36fab2016-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 Park4b36fab2016-09-11 21:14:58 -0700340 if (unlikely(!cs)) {
341 /*
342 * The top cpuset doesn't have any online cpu as a
343 * consequence of a race between cpuset_hotplug_work
344 * and cpu hotplug notifier. But we know the top
345 * cpuset's effective_cpus is on its way to to be
346 * identical to cpu_online_mask.
347 */
348 cpumask_copy(pmask, cpu_online_mask);
349 return;
350 }
351 }
Li Zefanae1c8022014-07-09 16:48:32 +0800352 cpumask_and(pmask, cs->effective_cpus, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/*
356 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700357 * are online, with memory. If none are online with memory, walk
358 * up the cpuset hierarchy until we find one that does have some
Li Zefan40df2de2013-06-05 17:15:23 +0800359 * online mems. The top cpuset always has some mems online.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * One way or another, we guarantee to return some non-empty subset
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800362 * of node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400364 * Call with callback_lock or cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400366static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Li Zefanae1c8022014-07-09 16:48:32 +0800368 while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
Tejun Heoc4310692013-01-07 08:51:08 -0800369 cs = parent_cs(cs);
Li Zefanae1c8022014-07-09 16:48:32 +0800370 nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Miao Xief3b39d42009-06-16 15:31:46 -0700373/*
374 * update task's spread flag if cpuset's page/slab spread flag is set
375 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400376 * Call with callback_lock or cpuset_mutex held.
Miao Xief3b39d42009-06-16 15:31:46 -0700377 */
378static void cpuset_update_task_spread_flag(struct cpuset *cs,
379 struct task_struct *tsk)
380{
381 if (is_spread_page(cs))
Zefan Li2ad654b2014-09-25 09:41:02 +0800382 task_set_spread_page(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700383 else
Zefan Li2ad654b2014-09-25 09:41:02 +0800384 task_clear_spread_page(tsk);
385
Miao Xief3b39d42009-06-16 15:31:46 -0700386 if (is_spread_slab(cs))
Zefan Li2ad654b2014-09-25 09:41:02 +0800387 task_set_spread_slab(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700388 else
Zefan Li2ad654b2014-09-25 09:41:02 +0800389 task_clear_spread_slab(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
393 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
394 *
395 * One cpuset is a subset of another if all its allowed CPUs and
396 * Memory Nodes are a subset of the other, and its exclusive flags
Tejun Heo5d21cc22013-01-07 08:51:08 -0800397 * are only set if the other's are set. Call holding cpuset_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399
400static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
401{
Riley Andrews7ec5a322016-09-06 15:16:25 -0700402 return cpumask_subset(p->cpus_requested, q->cpus_requested) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 nodes_subset(p->mems_allowed, q->mems_allowed) &&
404 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
405 is_mem_exclusive(p) <= is_mem_exclusive(q);
406}
407
Li Zefan645fcc92009-01-07 18:08:43 -0800408/**
409 * alloc_trial_cpuset - allocate a trial cpuset
410 * @cs: the cpuset that the trial cpuset duplicates
411 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400412static struct cpuset *alloc_trial_cpuset(struct cpuset *cs)
Li Zefan645fcc92009-01-07 18:08:43 -0800413{
Li Zefan300ed6c2009-01-07 18:08:44 -0800414 struct cpuset *trial;
415
416 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
417 if (!trial)
418 return NULL;
419
Li Zefane2b9a3d2014-07-09 16:47:03 +0800420 if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL))
421 goto free_cs;
422 if (!alloc_cpumask_var(&trial->effective_cpus, GFP_KERNEL))
423 goto free_cpus;
Li Zefan300ed6c2009-01-07 18:08:44 -0800424
Li Zefane2b9a3d2014-07-09 16:47:03 +0800425 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
426 cpumask_copy(trial->effective_cpus, cs->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -0800427 return trial;
Li Zefane2b9a3d2014-07-09 16:47:03 +0800428
429free_cpus:
430 free_cpumask_var(trial->cpus_allowed);
431free_cs:
432 kfree(trial);
433 return NULL;
Li Zefan645fcc92009-01-07 18:08:43 -0800434}
435
436/**
437 * free_trial_cpuset - free the trial cpuset
438 * @trial: the trial cpuset to be freed
439 */
440static void free_trial_cpuset(struct cpuset *trial)
441{
Li Zefane2b9a3d2014-07-09 16:47:03 +0800442 free_cpumask_var(trial->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -0800443 free_cpumask_var(trial->cpus_allowed);
Li Zefan645fcc92009-01-07 18:08:43 -0800444 kfree(trial);
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
448 * validate_change() - Used to validate that any proposed cpuset change
449 * follows the structural rules for cpusets.
450 *
451 * If we replaced the flag and mask values of the current cpuset
452 * (cur) with those values in the trial cpuset (trial), would
453 * our various subset and exclusive rules still be valid? Presumes
Tejun Heo5d21cc22013-01-07 08:51:08 -0800454 * cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
456 * 'cur' is the address of an actual, in-use cpuset. Operations
457 * such as list traversal that depend on the actual address of the
458 * cpuset in the list must use cur below, not trial.
459 *
460 * 'trial' is the address of bulk structure copy of cur, with
461 * perhaps one or more of the fields cpus_allowed, mems_allowed,
462 * or flags changed to new, trial values.
463 *
464 * Return 0 if valid, -errno if not.
465 */
466
Tejun Heoc9710d82013-08-08 20:11:22 -0400467static int validate_change(struct cpuset *cur, struct cpuset *trial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Tejun Heo492eb212013-08-08 20:11:25 -0400469 struct cgroup_subsys_state *css;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct cpuset *c, *par;
Tejun Heoae8086c2013-01-07 08:51:07 -0800471 int ret;
472
473 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Each of our child cpusets must be a subset of us */
Tejun Heoae8086c2013-01-07 08:51:07 -0800476 ret = -EBUSY;
Tejun Heo492eb212013-08-08 20:11:25 -0400477 cpuset_for_each_child(c, css, cur)
Tejun Heoae8086c2013-01-07 08:51:07 -0800478 if (!is_cpuset_subset(c, trial))
479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* Remaining checks don't apply to root cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800482 ret = 0;
Paul Jackson69604062006-12-06 20:36:15 -0800483 if (cur == &top_cpuset)
Tejun Heoae8086c2013-01-07 08:51:07 -0800484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Tejun Heoc4310692013-01-07 08:51:08 -0800486 par = parent_cs(cur);
Paul Jackson69604062006-12-06 20:36:15 -0800487
Li Zefan7e882912014-07-09 16:48:42 +0800488 /* On legacy hiearchy, we must be a subset of our parent cpuset. */
Tejun Heoae8086c2013-01-07 08:51:07 -0800489 ret = -EACCES;
Tejun Heo9e10a132015-09-18 11:56:28 -0400490 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
491 !is_cpuset_subset(trial, par))
Tejun Heoae8086c2013-01-07 08:51:07 -0800492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Paul Menage2df167a2008-02-07 00:14:45 -0800494 /*
495 * If either I or some sibling (!= me) is exclusive, we can't
496 * overlap
497 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800498 ret = -EINVAL;
Tejun Heo492eb212013-08-08 20:11:25 -0400499 cpuset_for_each_child(c, css, par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
501 c != cur &&
Riley Andrews7ec5a322016-09-06 15:16:25 -0700502 cpumask_intersects(trial->cpus_requested, c->cpus_requested))
Tejun Heoae8086c2013-01-07 08:51:07 -0800503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
505 c != cur &&
506 nodes_intersects(trial->mems_allowed, c->mems_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
Tejun Heo452477f2013-01-07 08:51:07 -0800510 /*
511 * Cpusets with tasks - existing or newly being attached - can't
Li Zefan1c09b192013-08-21 10:22:28 +0800512 * be changed to have empty cpus_allowed or mems_allowed.
Tejun Heo452477f2013-01-07 08:51:07 -0800513 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800514 ret = -ENOSPC;
Tejun Heo27bd4db2015-10-15 16:41:50 -0400515 if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) {
Li Zefan1c09b192013-08-21 10:22:28 +0800516 if (!cpumask_empty(cur->cpus_allowed) &&
517 cpumask_empty(trial->cpus_allowed))
518 goto out;
519 if (!nodes_empty(cur->mems_allowed) &&
520 nodes_empty(trial->mems_allowed))
521 goto out;
522 }
Paul Jackson020958b2007-10-18 23:40:21 -0700523
Juri Lellif82f8042014-10-07 09:52:11 +0100524 /*
525 * We can't shrink if we won't have enough room for SCHED_DEADLINE
526 * tasks.
527 */
528 ret = -EBUSY;
529 if (is_cpu_exclusive(cur) &&
530 !cpuset_cpumask_can_shrink(cur->cpus_allowed,
531 trial->cpus_allowed))
532 goto out;
533
Tejun Heoae8086c2013-01-07 08:51:07 -0800534 ret = 0;
535out:
536 rcu_read_unlock();
537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Paul Menagedb7f47c2009-04-02 16:57:55 -0700540#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700541/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700542 * Helper routine for generate_sched_domains().
Li Zefan8b5f1c52014-07-09 16:47:50 +0800543 * Do cpusets a, b have overlapping effective cpus_allowed masks?
Paul Jackson029190c2007-10-18 23:40:20 -0700544 */
Paul Jackson029190c2007-10-18 23:40:20 -0700545static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
546{
Li Zefan8b5f1c52014-07-09 16:47:50 +0800547 return cpumask_intersects(a->effective_cpus, b->effective_cpus);
Paul Jackson029190c2007-10-18 23:40:20 -0700548}
549
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900550static void
551update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
552{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900553 if (dattr->relax_domain_level < c->relax_domain_level)
554 dattr->relax_domain_level = c->relax_domain_level;
555 return;
556}
557
Tejun Heofc560a22013-01-07 08:51:08 -0800558static void update_domain_attr_tree(struct sched_domain_attr *dattr,
559 struct cpuset *root_cs)
Lai Jiangshanf5393692008-07-29 22:33:22 -0700560{
Tejun Heofc560a22013-01-07 08:51:08 -0800561 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -0400562 struct cgroup_subsys_state *pos_css;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700563
Tejun Heofc560a22013-01-07 08:51:08 -0800564 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400565 cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
Tejun Heofc560a22013-01-07 08:51:08 -0800566 /* skip the whole subtree if @cp doesn't have any CPU */
567 if (cpumask_empty(cp->cpus_allowed)) {
Tejun Heo492eb212013-08-08 20:11:25 -0400568 pos_css = css_rightmost_descendant(pos_css);
Lai Jiangshanf5393692008-07-29 22:33:22 -0700569 continue;
Tejun Heofc560a22013-01-07 08:51:08 -0800570 }
Lai Jiangshanf5393692008-07-29 22:33:22 -0700571
572 if (is_sched_load_balance(cp))
573 update_domain_attr(dattr, cp);
Lai Jiangshanf5393692008-07-29 22:33:22 -0700574 }
Tejun Heofc560a22013-01-07 08:51:08 -0800575 rcu_read_unlock();
Lai Jiangshanf5393692008-07-29 22:33:22 -0700576}
577
Paul Jackson029190c2007-10-18 23:40:20 -0700578/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700579 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700580 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700581 * This function builds a partial partition of the systems CPUs
582 * A 'partial partition' is a set of non-overlapping subsets whose
583 * union is a subset of that set.
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530584 * The output of this function needs to be passed to kernel/sched/core.c
Max Krasnyanskycf417142008-08-11 14:33:53 -0700585 * partition_sched_domains() routine, which will rebuild the scheduler's
586 * load balancing domains (sched domains) as specified by that partial
587 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700588 *
Li Zefan45ce80f2009-01-15 13:50:59 -0800589 * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt
Paul Jackson029190c2007-10-18 23:40:20 -0700590 * for a background explanation of this.
591 *
592 * Does not return errors, on the theory that the callers of this
593 * routine would rather not worry about failures to rebuild sched
594 * domains when operating in the severe memory shortage situations
595 * that could cause allocation failures below.
596 *
Tejun Heo5d21cc22013-01-07 08:51:08 -0800597 * Must be called with cpuset_mutex held.
Paul Jackson029190c2007-10-18 23:40:20 -0700598 *
599 * The three key local variables below are:
Li Zefanaeed6822008-07-29 22:33:24 -0700600 * q - a linked-list queue of cpuset pointers, used to implement a
Paul Jackson029190c2007-10-18 23:40:20 -0700601 * top-down scan of all cpusets. This scan loads a pointer
602 * to each cpuset marked is_sched_load_balance into the
603 * array 'csa'. For our purposes, rebuilding the schedulers
604 * sched domains, we can ignore !is_sched_load_balance cpusets.
605 * csa - (for CpuSet Array) Array of pointers to all the cpusets
606 * that need to be load balanced, for convenient iterative
607 * access by the subsequent code that finds the best partition,
608 * i.e the set of domains (subsets) of CPUs such that the
609 * cpus_allowed of every cpuset marked is_sched_load_balance
610 * is a subset of one of these domains, while there are as
611 * many such domains as possible, each as small as possible.
612 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530613 * the kernel/sched/core.c routine partition_sched_domains() in a
Paul Jackson029190c2007-10-18 23:40:20 -0700614 * convenient format, that can be easily compared to the prior
615 * value to determine what partition elements (sched domains)
616 * were changed (added or removed.)
617 *
618 * Finding the best partition (set of domains):
619 * The triple nested loops below over i, j, k scan over the
620 * load balanced cpusets (using the array of cpuset pointers in
621 * csa[]) looking for pairs of cpusets that have overlapping
622 * cpus_allowed, but which don't have the same 'pn' partition
623 * number and gives them in the same partition number. It keeps
624 * looping on the 'restart' label until it can no longer find
625 * any such pairs.
626 *
627 * The union of the cpus_allowed masks from the set of
628 * all cpusets having the same 'pn' value then form the one
629 * element of the partition (one sched domain) to be passed to
630 * partition_sched_domains().
631 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030632static int generate_sched_domains(cpumask_var_t **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700633 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700634{
Paul Jackson029190c2007-10-18 23:40:20 -0700635 struct cpuset *cp; /* scans q */
636 struct cpuset **csa; /* array of all cpuset ptrs */
637 int csn; /* how many cpuset ptrs in csa so far */
638 int i, j, k; /* indices for partition finding loops */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030639 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
Rik van Riel47b8ea72015-03-09 12:12:08 -0400640 cpumask_var_t non_isolated_cpus; /* load balanced CPUs */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900641 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100642 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800643 int nslot; /* next empty doms[] struct cpumask slot */
Tejun Heo492eb212013-08-08 20:11:25 -0400644 struct cgroup_subsys_state *pos_css;
Paul Jackson029190c2007-10-18 23:40:20 -0700645
Paul Jackson029190c2007-10-18 23:40:20 -0700646 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900647 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700648 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700649
Rik van Riel47b8ea72015-03-09 12:12:08 -0400650 if (!alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL))
651 goto done;
652 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
653
Paul Jackson029190c2007-10-18 23:40:20 -0700654 /* Special case for the 99% of systems with one, full, sched domain */
655 if (is_sched_load_balance(&top_cpuset)) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030656 ndoms = 1;
657 doms = alloc_sched_domains(ndoms);
Paul Jackson029190c2007-10-18 23:40:20 -0700658 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700659 goto done;
660
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900661 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
662 if (dattr) {
663 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700664 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900665 }
Rik van Riel47b8ea72015-03-09 12:12:08 -0400666 cpumask_and(doms[0], top_cpuset.effective_cpus,
667 non_isolated_cpus);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700668
Max Krasnyanskycf417142008-08-11 14:33:53 -0700669 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700670 }
671
Mel Gorman664eedd2014-06-04 16:10:08 -0700672 csa = kmalloc(nr_cpusets() * sizeof(cp), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700673 if (!csa)
674 goto done;
675 csn = 0;
676
Tejun Heofc560a22013-01-07 08:51:08 -0800677 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400678 cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
Tejun Heobd8815a2013-08-08 20:11:27 -0400679 if (cp == &top_cpuset)
680 continue;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700681 /*
Tejun Heofc560a22013-01-07 08:51:08 -0800682 * Continue traversing beyond @cp iff @cp has some CPUs and
683 * isn't load balancing. The former is obvious. The
684 * latter: All child cpusets contain a subset of the
685 * parent's cpus, so just skip them, and then we call
686 * update_domain_attr_tree() to calc relax_domain_level of
687 * the corresponding sched domain.
Lai Jiangshanf5393692008-07-29 22:33:22 -0700688 */
Tejun Heofc560a22013-01-07 08:51:08 -0800689 if (!cpumask_empty(cp->cpus_allowed) &&
Rik van Riel47b8ea72015-03-09 12:12:08 -0400690 !(is_sched_load_balance(cp) &&
691 cpumask_intersects(cp->cpus_allowed, non_isolated_cpus)))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700692 continue;
Lai Jiangshan489a5392008-07-25 01:47:23 -0700693
Tejun Heofc560a22013-01-07 08:51:08 -0800694 if (is_sched_load_balance(cp))
695 csa[csn++] = cp;
696
697 /* skip @cp's subtree */
Tejun Heo492eb212013-08-08 20:11:25 -0400698 pos_css = css_rightmost_descendant(pos_css);
Tejun Heofc560a22013-01-07 08:51:08 -0800699 }
700 rcu_read_unlock();
Paul Jackson029190c2007-10-18 23:40:20 -0700701
702 for (i = 0; i < csn; i++)
703 csa[i]->pn = i;
704 ndoms = csn;
705
706restart:
707 /* Find the best partition (set of sched domains) */
708 for (i = 0; i < csn; i++) {
709 struct cpuset *a = csa[i];
710 int apn = a->pn;
711
712 for (j = 0; j < csn; j++) {
713 struct cpuset *b = csa[j];
714 int bpn = b->pn;
715
716 if (apn != bpn && cpusets_overlap(a, b)) {
717 for (k = 0; k < csn; k++) {
718 struct cpuset *c = csa[k];
719
720 if (c->pn == bpn)
721 c->pn = apn;
722 }
723 ndoms--; /* one less element */
724 goto restart;
725 }
726 }
727 }
728
Max Krasnyanskycf417142008-08-11 14:33:53 -0700729 /*
730 * Now we know how many domains to create.
731 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
732 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030733 doms = alloc_sched_domains(ndoms);
Li Zefan700018e2008-11-18 14:02:03 +0800734 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700735 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700736
737 /*
738 * The rest of the code, including the scheduler, can deal with
739 * dattr==NULL case. No need to abort if alloc fails.
740 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900741 dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700742
743 for (nslot = 0, i = 0; i < csn; i++) {
744 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800745 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700746 int apn = a->pn;
747
Max Krasnyanskycf417142008-08-11 14:33:53 -0700748 if (apn < 0) {
749 /* Skip completed partitions */
750 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700751 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700752
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030753 dp = doms[nslot];
Max Krasnyanskycf417142008-08-11 14:33:53 -0700754
755 if (nslot == ndoms) {
756 static int warnings = 10;
757 if (warnings) {
Fabian Frederick12d30892014-05-05 19:49:00 +0200758 pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n",
759 nslot, ndoms, csn, i, apn);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700760 warnings--;
761 }
762 continue;
763 }
764
Li Zefan6af866a2009-01-07 18:08:45 -0800765 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700766 if (dattr)
767 *(dattr + nslot) = SD_ATTR_INIT;
768 for (j = i; j < csn; j++) {
769 struct cpuset *b = csa[j];
770
771 if (apn == b->pn) {
Li Zefan8b5f1c52014-07-09 16:47:50 +0800772 cpumask_or(dp, dp, b->effective_cpus);
Rik van Riel47b8ea72015-03-09 12:12:08 -0400773 cpumask_and(dp, dp, non_isolated_cpus);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700774 if (dattr)
775 update_domain_attr_tree(dattr + nslot, b);
776
777 /* Done with this partition */
778 b->pn = -1;
779 }
780 }
781 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700782 }
783 BUG_ON(nslot != ndoms);
784
Paul Jackson029190c2007-10-18 23:40:20 -0700785done:
Rik van Riel47b8ea72015-03-09 12:12:08 -0400786 free_cpumask_var(non_isolated_cpus);
Paul Jackson029190c2007-10-18 23:40:20 -0700787 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700788
Li Zefan700018e2008-11-18 14:02:03 +0800789 /*
790 * Fallback to the default domain if kmalloc() failed.
791 * See comments in partition_sched_domains().
792 */
793 if (doms == NULL)
794 ndoms = 1;
795
Max Krasnyanskycf417142008-08-11 14:33:53 -0700796 *domains = doms;
797 *attributes = dattr;
798 return ndoms;
799}
800
801/*
802 * Rebuild scheduler domains.
803 *
Tejun Heo699140b2013-01-07 08:51:07 -0800804 * If the flag 'sched_load_balance' of any cpuset with non-empty
805 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
806 * which has that flag enabled, or if any cpuset with a non-empty
807 * 'cpus' is removed, then call this routine to rebuild the
808 * scheduler's dynamic sched domains.
Max Krasnyanskycf417142008-08-11 14:33:53 -0700809 *
Tejun Heo5d21cc22013-01-07 08:51:08 -0800810 * Call with cpuset_mutex held. Takes get_online_cpus().
Max Krasnyanskycf417142008-08-11 14:33:53 -0700811 */
Tejun Heo699140b2013-01-07 08:51:07 -0800812static void rebuild_sched_domains_locked(void)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700813{
814 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030815 cpumask_var_t *doms;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700816 int ndoms;
817
Tejun Heo5d21cc22013-01-07 08:51:08 -0800818 lockdep_assert_held(&cpuset_mutex);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700819 get_online_cpus();
820
Li Zefan5b16c2a2013-04-27 06:52:43 -0700821 /*
822 * We have raced with CPU hotplug. Don't do anything to avoid
823 * passing doms with offlined cpu to partition_sched_domains().
824 * Anyways, hotplug work item will rebuild sched domains.
825 */
Li Zefan8b5f1c52014-07-09 16:47:50 +0800826 if (!cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask))
Li Zefan5b16c2a2013-04-27 06:52:43 -0700827 goto out;
828
Max Krasnyanskycf417142008-08-11 14:33:53 -0700829 /* Generate domain masks and attrs */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700830 ndoms = generate_sched_domains(&doms, &attr);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700831
832 /* Have scheduler rebuild the domains */
833 partition_sched_domains(ndoms, doms, attr);
Li Zefan5b16c2a2013-04-27 06:52:43 -0700834out:
Max Krasnyanskycf417142008-08-11 14:33:53 -0700835 put_online_cpus();
836}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700837#else /* !CONFIG_SMP */
Tejun Heo699140b2013-01-07 08:51:07 -0800838static void rebuild_sched_domains_locked(void)
Paul Menagedb7f47c2009-04-02 16:57:55 -0700839{
840}
Paul Menagedb7f47c2009-04-02 16:57:55 -0700841#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -0700842
Max Krasnyanskycf417142008-08-11 14:33:53 -0700843void rebuild_sched_domains(void)
844{
Tejun Heo5d21cc22013-01-07 08:51:08 -0800845 mutex_lock(&cpuset_mutex);
Tejun Heo699140b2013-01-07 08:51:07 -0800846 rebuild_sched_domains_locked();
Tejun Heo5d21cc22013-01-07 08:51:08 -0800847 mutex_unlock(&cpuset_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -0700848}
849
Cliff Wickman58f47902008-02-07 00:14:44 -0800850/**
Miao Xie0b2f6302008-07-25 01:47:21 -0700851 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
852 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
853 *
Tejun Heod66393e2014-02-13 06:58:40 -0500854 * Iterate through each task of @cs updating its cpus_allowed to the
855 * effective cpuset's. As this function is called with cpuset_mutex held,
856 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -0700857 */
Tejun Heod66393e2014-02-13 06:58:40 -0500858static void update_tasks_cpumask(struct cpuset *cs)
Miao Xie0b2f6302008-07-25 01:47:21 -0700859{
Tejun Heod66393e2014-02-13 06:58:40 -0500860 struct css_task_iter it;
861 struct task_struct *task;
862
863 css_task_iter_start(&cs->css, &it);
864 while ((task = css_task_iter_next(&it)))
Li Zefanae1c8022014-07-09 16:48:32 +0800865 set_cpus_allowed_ptr(task, cs->effective_cpus);
Tejun Heod66393e2014-02-13 06:58:40 -0500866 css_task_iter_end(&it);
Miao Xie0b2f6302008-07-25 01:47:21 -0700867}
868
Li Zefan5c5cc622013-06-09 17:16:29 +0800869/*
Li Zefan734d4512014-07-09 16:47:29 +0800870 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
871 * @cs: the cpuset to consider
872 * @new_cpus: temp variable for calculating new effective_cpus
Li Zefan5c5cc622013-06-09 17:16:29 +0800873 *
Li Zefan734d4512014-07-09 16:47:29 +0800874 * When congifured cpumask is changed, the effective cpumasks of this cpuset
875 * and all its descendants need to be updated.
876 *
877 * On legacy hierachy, effective_cpus will be the same with cpu_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +0800878 *
879 * Called with cpuset_mutex held
880 */
Li Zefan734d4512014-07-09 16:47:29 +0800881static void update_cpumasks_hier(struct cpuset *cs, struct cpumask *new_cpus)
Li Zefan5c5cc622013-06-09 17:16:29 +0800882{
883 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -0400884 struct cgroup_subsys_state *pos_css;
Li Zefan8b5f1c52014-07-09 16:47:50 +0800885 bool need_rebuild_sched_domains = false;
Li Zefan5c5cc622013-06-09 17:16:29 +0800886
887 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +0800888 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
889 struct cpuset *parent = parent_cs(cp);
890
891 cpumask_and(new_cpus, cp->cpus_allowed, parent->effective_cpus);
892
Li Zefan554b0d12014-07-09 16:47:41 +0800893 /*
894 * If it becomes empty, inherit the effective mask of the
895 * parent, which is guaranteed to have some CPUs.
896 */
Tejun Heo9e10a132015-09-18 11:56:28 -0400897 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
898 cpumask_empty(new_cpus))
Li Zefan554b0d12014-07-09 16:47:41 +0800899 cpumask_copy(new_cpus, parent->effective_cpus);
900
Li Zefan734d4512014-07-09 16:47:29 +0800901 /* Skip the whole subtree if the cpumask remains the same. */
902 if (cpumask_equal(new_cpus, cp->effective_cpus)) {
903 pos_css = css_rightmost_descendant(pos_css);
904 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +0800905 }
Li Zefan734d4512014-07-09 16:47:29 +0800906
Tejun Heoec903c02014-05-13 12:11:01 -0400907 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +0800908 continue;
909 rcu_read_unlock();
910
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400911 spin_lock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +0800912 cpumask_copy(cp->effective_cpus, new_cpus);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400913 spin_unlock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +0800914
Tejun Heo9e10a132015-09-18 11:56:28 -0400915 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefan734d4512014-07-09 16:47:29 +0800916 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
917
Tejun Heod66393e2014-02-13 06:58:40 -0500918 update_tasks_cpumask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +0800919
Li Zefan8b5f1c52014-07-09 16:47:50 +0800920 /*
921 * If the effective cpumask of any non-empty cpuset is changed,
922 * we need to rebuild sched domains.
923 */
924 if (!cpumask_empty(cp->cpus_allowed) &&
925 is_sched_load_balance(cp))
926 need_rebuild_sched_domains = true;
927
Li Zefan5c5cc622013-06-09 17:16:29 +0800928 rcu_read_lock();
929 css_put(&cp->css);
930 }
931 rcu_read_unlock();
Li Zefan8b5f1c52014-07-09 16:47:50 +0800932
933 if (need_rebuild_sched_domains)
934 rebuild_sched_domains_locked();
Li Zefan5c5cc622013-06-09 17:16:29 +0800935}
936
Miao Xie0b2f6302008-07-25 01:47:21 -0700937/**
Cliff Wickman58f47902008-02-07 00:14:44 -0800938 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
939 * @cs: the cpuset to consider
Fabian Frederickfc34ac12014-05-05 19:46:55 +0200940 * @trialcs: trial cpuset
Cliff Wickman58f47902008-02-07 00:14:44 -0800941 * @buf: buffer of cpu numbers written to this cpuset
942 */
Li Zefan645fcc92009-01-07 18:08:43 -0800943static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
944 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Cliff Wickman58f47902008-02-07 00:14:44 -0800946 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rusty Russell5f054e32012-03-29 15:38:31 +1030948 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
Paul Jackson4c4d50f2006-08-27 01:23:51 -0700949 if (cs == &top_cpuset)
950 return -EACCES;
951
David Rientjes6f7f02e2007-05-08 00:31:43 -0700952 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -0800953 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -0700954 * Since cpulist_parse() fails on an empty mask, we special case
955 * that parsing. The validate_change() call ensures that cpusets
956 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -0700957 */
Paul Jackson020958b2007-10-18 23:40:21 -0700958 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -0800959 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700960 } else {
Riley Andrews7ec5a322016-09-06 15:16:25 -0700961 retval = cpulist_parse(buf, trialcs->cpus_requested);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700962 if (retval < 0)
963 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -0700964
Riley Andrews7ec5a322016-09-06 15:16:25 -0700965 if (!cpumask_subset(trialcs->cpus_requested, cpu_present_mask))
Lai Jiangshan37340742008-06-05 22:46:32 -0700966 return -EINVAL;
Riley Andrews7ec5a322016-09-06 15:16:25 -0700967
968 cpumask_and(trialcs->cpus_allowed, trialcs->cpus_requested, cpu_active_mask);
David Rientjes6f7f02e2007-05-08 00:31:43 -0700969 }
Paul Jackson029190c2007-10-18 23:40:20 -0700970
Paul Menage8707d8b2007-10-18 23:40:22 -0700971 /* Nothing to do if the cpus didn't change */
Riley Andrews7ec5a322016-09-06 15:16:25 -0700972 if (cpumask_equal(cs->cpus_requested, trialcs->cpus_requested))
Paul Menage8707d8b2007-10-18 23:40:22 -0700973 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -0800974
Li Zefana73456f2013-06-05 17:15:59 +0800975 retval = validate_change(cs, trialcs);
976 if (retval < 0)
977 return retval;
978
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400979 spin_lock_irq(&callback_lock);
Li Zefan300ed6c2009-01-07 18:08:44 -0800980 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Riley Andrews7ec5a322016-09-06 15:16:25 -0700981 cpumask_copy(cs->cpus_requested, trialcs->cpus_requested);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400982 spin_unlock_irq(&callback_lock);
Paul Jackson029190c2007-10-18 23:40:20 -0700983
Li Zefan734d4512014-07-09 16:47:29 +0800984 /* use trialcs->cpus_allowed as a temp variable */
985 update_cpumasks_hier(cs, trialcs->cpus_allowed);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Paul Jackson053199e2005-10-30 15:02:30 -0800989/*
Tejun Heofff4dc82016-01-19 12:18:41 -0500990 * Migrate memory region from one set of nodes to another. This is
991 * performed asynchronously as it can be called from process migration path
992 * holding locks involved in process management. All mm migrations are
993 * performed in the queued order and can be waited for by flushing
994 * cpuset_migrate_mm_wq.
Paul Jacksone4e364e2006-03-31 02:30:52 -0800995 */
996
Tejun Heofff4dc82016-01-19 12:18:41 -0500997struct cpuset_migrate_mm_work {
998 struct work_struct work;
999 struct mm_struct *mm;
1000 nodemask_t from;
1001 nodemask_t to;
1002};
1003
1004static void cpuset_migrate_mm_workfn(struct work_struct *work)
1005{
1006 struct cpuset_migrate_mm_work *mwork =
1007 container_of(work, struct cpuset_migrate_mm_work, work);
1008
1009 /* on a wq worker, no need to worry about %current's mems_allowed */
1010 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
1011 mmput(mwork->mm);
1012 kfree(mwork);
1013}
1014
Paul Jacksone4e364e2006-03-31 02:30:52 -08001015static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1016 const nodemask_t *to)
1017{
Tejun Heofff4dc82016-01-19 12:18:41 -05001018 struct cpuset_migrate_mm_work *mwork;
Paul Jacksone4e364e2006-03-31 02:30:52 -08001019
Tejun Heofff4dc82016-01-19 12:18:41 -05001020 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL);
1021 if (mwork) {
1022 mwork->mm = mm;
1023 mwork->from = *from;
1024 mwork->to = *to;
1025 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn);
1026 queue_work(cpuset_migrate_mm_wq, &mwork->work);
1027 } else {
1028 mmput(mm);
1029 }
1030}
Paul Jacksone4e364e2006-03-31 02:30:52 -08001031
Tejun Heod5209742016-04-21 19:06:48 -04001032static void cpuset_post_attach(void)
Tejun Heofff4dc82016-01-19 12:18:41 -05001033{
1034 flush_workqueue(cpuset_migrate_mm_wq);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001035}
1036
Li Zefan3b6766f2009-04-02 16:57:51 -07001037/*
Miao Xie58568d22009-06-16 15:31:49 -07001038 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
1039 * @tsk: the task to change
1040 * @newmems: new nodes that the task will be set
1041 *
1042 * In order to avoid seeing no nodes if the old and new nodes are disjoint,
1043 * we structure updates as setting all new allowed nodes, then clearing newly
1044 * disallowed ones.
Miao Xie58568d22009-06-16 15:31:49 -07001045 */
1046static void cpuset_change_task_nodemask(struct task_struct *tsk,
1047 nodemask_t *newmems)
1048{
David Rientjesb2462722011-12-19 17:11:52 -08001049 bool need_loop;
David Rientjes89e8a242011-11-02 13:38:39 -07001050
Miao Xiec0ff7452010-05-24 14:32:08 -07001051 /*
1052 * Allow tasks that have access to memory reserves because they have
1053 * been OOM killed to get memory anywhere.
1054 */
1055 if (unlikely(test_thread_flag(TIF_MEMDIE)))
1056 return;
1057 if (current->flags & PF_EXITING) /* Let dying task have memory */
1058 return;
1059
1060 task_lock(tsk);
David Rientjesb2462722011-12-19 17:11:52 -08001061 /*
1062 * Determine if a loop is necessary if another thread is doing
Mel Gormand26914d2014-04-03 14:47:24 -07001063 * read_mems_allowed_begin(). If at least one node remains unchanged and
David Rientjesb2462722011-12-19 17:11:52 -08001064 * tsk does not have a mempolicy, then an empty nodemask will not be
1065 * possible when mems_allowed is larger than a word.
1066 */
1067 need_loop = task_has_mempolicy(tsk) ||
1068 !nodes_intersects(*newmems, tsk->mems_allowed);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001069
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001070 if (need_loop) {
1071 local_irq_disable();
Mel Gormancc9a6c82012-03-21 16:34:11 -07001072 write_seqcount_begin(&tsk->mems_allowed_seq);
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001073 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001074
Miao Xie58568d22009-06-16 15:31:49 -07001075 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Miao Xiec0ff7452010-05-24 14:32:08 -07001076 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1);
1077
Miao Xiec0ff7452010-05-24 14:32:08 -07001078 mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2);
Miao Xie58568d22009-06-16 15:31:49 -07001079 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001080
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001081 if (need_loop) {
Mel Gormancc9a6c82012-03-21 16:34:11 -07001082 write_seqcount_end(&tsk->mems_allowed_seq);
Peter Zijlstra0fc02872013-11-26 15:03:41 +01001083 local_irq_enable();
1084 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07001085
Miao Xiec0ff7452010-05-24 14:32:08 -07001086 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001087}
1088
Paul Menage8793d852007-10-18 23:39:39 -07001089static void *cpuset_being_rebound;
1090
Miao Xie0b2f6302008-07-25 01:47:21 -07001091/**
1092 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1093 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
Miao Xie0b2f6302008-07-25 01:47:21 -07001094 *
Tejun Heod66393e2014-02-13 06:58:40 -05001095 * Iterate through each task of @cs updating its mems_allowed to the
1096 * effective cpuset's. As this function is called with cpuset_mutex held,
1097 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -07001098 */
Tejun Heod66393e2014-02-13 06:58:40 -05001099static void update_tasks_nodemask(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Li Zefan33ad8012013-06-09 17:15:08 +08001101 static nodemask_t newmems; /* protected by cpuset_mutex */
Tejun Heod66393e2014-02-13 06:58:40 -05001102 struct css_task_iter it;
1103 struct task_struct *task;
Paul Jackson59dac162006-01-08 01:01:52 -08001104
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001105 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001106
Li Zefanae1c8022014-07-09 16:48:32 +08001107 guarantee_online_mems(cs, &newmems);
Li Zefan33ad8012013-06-09 17:15:08 +08001108
Paul Jackson42253992006-01-08 01:01:59 -08001109 /*
Li Zefan3b6766f2009-04-02 16:57:51 -07001110 * The mpol_rebind_mm() call takes mmap_sem, which we couldn't
1111 * take while holding tasklist_lock. Forks can happen - the
1112 * mpol_dup() cpuset_being_rebound check will catch such forks,
1113 * and rebind their vma mempolicies too. Because we still hold
Tejun Heo5d21cc22013-01-07 08:51:08 -08001114 * the global cpuset_mutex, we know that no other rebind effort
Li Zefan3b6766f2009-04-02 16:57:51 -07001115 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001116 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001117 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001118 */
Tejun Heod66393e2014-02-13 06:58:40 -05001119 css_task_iter_start(&cs->css, &it);
1120 while ((task = css_task_iter_next(&it))) {
1121 struct mm_struct *mm;
1122 bool migrate;
1123
1124 cpuset_change_task_nodemask(task, &newmems);
1125
1126 mm = get_task_mm(task);
1127 if (!mm)
1128 continue;
1129
1130 migrate = is_memory_migrate(cs);
1131
1132 mpol_rebind_mm(mm, &cs->mems_allowed);
1133 if (migrate)
1134 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
Tejun Heofff4dc82016-01-19 12:18:41 -05001135 else
1136 mmput(mm);
Tejun Heod66393e2014-02-13 06:58:40 -05001137 }
1138 css_task_iter_end(&it);
Paul Jackson42253992006-01-08 01:01:59 -08001139
Li Zefan33ad8012013-06-09 17:15:08 +08001140 /*
1141 * All the tasks' nodemasks have been updated, update
1142 * cs->old_mems_allowed.
1143 */
1144 cs->old_mems_allowed = newmems;
1145
Paul Menage2df167a2008-02-07 00:14:45 -08001146 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001147 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Miao Xie0b2f6302008-07-25 01:47:21 -07001150/*
Li Zefan734d4512014-07-09 16:47:29 +08001151 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1152 * @cs: the cpuset to consider
1153 * @new_mems: a temp variable for calculating new effective_mems
Li Zefan5c5cc622013-06-09 17:16:29 +08001154 *
Li Zefan734d4512014-07-09 16:47:29 +08001155 * When configured nodemask is changed, the effective nodemasks of this cpuset
1156 * and all its descendants need to be updated.
1157 *
1158 * On legacy hiearchy, effective_mems will be the same with mems_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +08001159 *
1160 * Called with cpuset_mutex held
1161 */
Li Zefan734d4512014-07-09 16:47:29 +08001162static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
Li Zefan5c5cc622013-06-09 17:16:29 +08001163{
1164 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -04001165 struct cgroup_subsys_state *pos_css;
Li Zefan5c5cc622013-06-09 17:16:29 +08001166
1167 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +08001168 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1169 struct cpuset *parent = parent_cs(cp);
1170
1171 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
1172
Li Zefan554b0d12014-07-09 16:47:41 +08001173 /*
1174 * If it becomes empty, inherit the effective mask of the
1175 * parent, which is guaranteed to have some MEMs.
1176 */
Tejun Heo9e10a132015-09-18 11:56:28 -04001177 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
1178 nodes_empty(*new_mems))
Li Zefan554b0d12014-07-09 16:47:41 +08001179 *new_mems = parent->effective_mems;
1180
Li Zefan734d4512014-07-09 16:47:29 +08001181 /* Skip the whole subtree if the nodemask remains the same. */
1182 if (nodes_equal(*new_mems, cp->effective_mems)) {
1183 pos_css = css_rightmost_descendant(pos_css);
1184 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +08001185 }
Li Zefan734d4512014-07-09 16:47:29 +08001186
Tejun Heoec903c02014-05-13 12:11:01 -04001187 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +08001188 continue;
1189 rcu_read_unlock();
1190
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001191 spin_lock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001192 cp->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001193 spin_unlock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001194
Tejun Heo9e10a132015-09-18 11:56:28 -04001195 WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefana1381262014-07-30 15:07:13 +08001196 !nodes_equal(cp->mems_allowed, cp->effective_mems));
Li Zefan734d4512014-07-09 16:47:29 +08001197
Tejun Heod66393e2014-02-13 06:58:40 -05001198 update_tasks_nodemask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +08001199
1200 rcu_read_lock();
1201 css_put(&cp->css);
1202 }
1203 rcu_read_unlock();
1204}
1205
1206/*
Miao Xie0b2f6302008-07-25 01:47:21 -07001207 * Handle user request to change the 'mems' memory placement
1208 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001209 * cpusets mems_allowed, and for each task in the cpuset,
1210 * update mems_allowed and rebind task's mempolicy and any vma
1211 * mempolicies and if the cpuset is marked 'memory_migrate',
1212 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001213 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001214 * Call with cpuset_mutex held. May take callback_lock during call.
Miao Xie0b2f6302008-07-25 01:47:21 -07001215 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
1216 * lock each such tasks mm->mmap_sem, scan its vma's and rebind
1217 * their mempolicies to the cpusets new mems_allowed.
1218 */
Li Zefan645fcc92009-01-07 18:08:43 -08001219static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1220 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001221{
Miao Xie0b2f6302008-07-25 01:47:21 -07001222 int retval;
1223
1224 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001225 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001226 * it's read-only
1227 */
Miao Xie53feb292010-03-23 13:35:35 -07001228 if (cs == &top_cpuset) {
1229 retval = -EACCES;
1230 goto done;
1231 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001232
Miao Xie0b2f6302008-07-25 01:47:21 -07001233 /*
1234 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1235 * Since nodelist_parse() fails on an empty mask, we special case
1236 * that parsing. The validate_change() call ensures that cpusets
1237 * with tasks have memory.
1238 */
1239 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001240 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001241 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001242 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001243 if (retval < 0)
1244 goto done;
1245
Li Zefan645fcc92009-01-07 18:08:43 -08001246 if (!nodes_subset(trialcs->mems_allowed,
Li Zefan5d8ba822014-07-09 16:49:12 +08001247 top_cpuset.mems_allowed)) {
1248 retval = -EINVAL;
Miao Xie53feb292010-03-23 13:35:35 -07001249 goto done;
1250 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001251 }
Li Zefan33ad8012013-06-09 17:15:08 +08001252
1253 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001254 retval = 0; /* Too easy - nothing to do */
1255 goto done;
1256 }
Li Zefan645fcc92009-01-07 18:08:43 -08001257 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001258 if (retval < 0)
1259 goto done;
1260
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001261 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001262 cs->mems_allowed = trialcs->mems_allowed;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001263 spin_unlock_irq(&callback_lock);
Miao Xie0b2f6302008-07-25 01:47:21 -07001264
Li Zefan734d4512014-07-09 16:47:29 +08001265 /* use trialcs->mems_allowed as a temp variable */
Alban Crequy24ee3cf2015-08-06 16:21:05 +02001266 update_nodemasks_hier(cs, &trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001267done:
1268 return retval;
1269}
1270
Paul Menage8793d852007-10-18 23:39:39 -07001271int current_cpuset_is_being_rebound(void)
1272{
Gu Zheng391acf92014-06-25 09:57:18 +08001273 int ret;
1274
1275 rcu_read_lock();
1276 ret = task_cs(current) == cpuset_being_rebound;
1277 rcu_read_unlock();
1278
1279 return ret;
Paul Menage8793d852007-10-18 23:39:39 -07001280}
1281
Paul Menage5be7a472008-05-06 20:42:41 -07001282static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001283{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001284#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001285 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001286 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001287#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001288
1289 if (val != cs->relax_domain_level) {
1290 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001291 if (!cpumask_empty(cs->cpus_allowed) &&
1292 is_sched_load_balance(cs))
Tejun Heo699140b2013-01-07 08:51:07 -08001293 rebuild_sched_domains_locked();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001294 }
1295
1296 return 0;
1297}
1298
Tejun Heo72ec7022013-08-08 20:11:26 -04001299/**
Miao Xie950592f2009-06-16 15:31:47 -07001300 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1301 * @cs: the cpuset in which each task's spread flags needs to be changed
Miao Xie950592f2009-06-16 15:31:47 -07001302 *
Tejun Heod66393e2014-02-13 06:58:40 -05001303 * Iterate through each task of @cs updating its spread flags. As this
1304 * function is called with cpuset_mutex held, cpuset membership stays
1305 * stable.
Miao Xie950592f2009-06-16 15:31:47 -07001306 */
Tejun Heod66393e2014-02-13 06:58:40 -05001307static void update_tasks_flags(struct cpuset *cs)
Miao Xie950592f2009-06-16 15:31:47 -07001308{
Tejun Heod66393e2014-02-13 06:58:40 -05001309 struct css_task_iter it;
1310 struct task_struct *task;
1311
1312 css_task_iter_start(&cs->css, &it);
1313 while ((task = css_task_iter_next(&it)))
1314 cpuset_update_task_spread_flag(cs, task);
1315 css_task_iter_end(&it);
Miao Xie950592f2009-06-16 15:31:47 -07001316}
1317
1318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001320 * bit: the bit to update (see cpuset_flagbits_t)
1321 * cs: the cpuset to update
1322 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001323 *
Tejun Heo5d21cc22013-01-07 08:51:08 -08001324 * Call with cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
1326
Paul Menage700fe1a2008-04-29 01:00:00 -07001327static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1328 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Li Zefan645fcc92009-01-07 18:08:43 -08001330 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001331 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001332 int spread_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Li Zefan645fcc92009-01-07 18:08:43 -08001335 trialcs = alloc_trial_cpuset(cs);
1336 if (!trialcs)
1337 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Li Zefan645fcc92009-01-07 18:08:43 -08001339 if (turning_on)
1340 set_bit(bit, &trialcs->flags);
1341 else
1342 clear_bit(bit, &trialcs->flags);
1343
1344 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001345 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001346 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001347
Paul Jackson029190c2007-10-18 23:40:20 -07001348 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001349 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001350
Miao Xie950592f2009-06-16 15:31:47 -07001351 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1352 || (is_spread_page(cs) != is_spread_page(trialcs)));
1353
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001354 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001355 cs->flags = trialcs->flags;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001356 spin_unlock_irq(&callback_lock);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001357
Li Zefan300ed6c2009-01-07 18:08:44 -08001358 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Tejun Heo699140b2013-01-07 08:51:07 -08001359 rebuild_sched_domains_locked();
Paul Jackson029190c2007-10-18 23:40:20 -07001360
Miao Xie950592f2009-06-16 15:31:47 -07001361 if (spread_flag_changed)
Tejun Heod66393e2014-02-13 06:58:40 -05001362 update_tasks_flags(cs);
Li Zefan645fcc92009-01-07 18:08:43 -08001363out:
1364 free_trial_cpuset(trialcs);
1365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Paul Jackson053199e2005-10-30 15:02:30 -08001368/*
Adrian Bunk80f72282006-06-30 18:27:16 +02001369 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001370 *
1371 * These routines manage a digitally filtered, constant time based,
1372 * event frequency meter. There are four routines:
1373 * fmeter_init() - initialize a frequency meter.
1374 * fmeter_markevent() - called each time the event happens.
1375 * fmeter_getrate() - returns the recent rate of such events.
1376 * fmeter_update() - internal routine used to update fmeter.
1377 *
1378 * A common data structure is passed to each of these routines,
1379 * which is used to keep track of the state required to manage the
1380 * frequency meter and its digital filter.
1381 *
1382 * The filter works on the number of events marked per unit time.
1383 * The filter is single-pole low-pass recursive (IIR). The time unit
1384 * is 1 second. Arithmetic is done using 32-bit integers scaled to
1385 * simulate 3 decimal digits of precision (multiplied by 1000).
1386 *
1387 * With an FM_COEF of 933, and a time base of 1 second, the filter
1388 * has a half-life of 10 seconds, meaning that if the events quit
1389 * happening, then the rate returned from the fmeter_getrate()
1390 * will be cut in half each 10 seconds, until it converges to zero.
1391 *
1392 * It is not worth doing a real infinitely recursive filter. If more
1393 * than FM_MAXTICKS ticks have elapsed since the last filter event,
1394 * just compute FM_MAXTICKS ticks worth, by which point the level
1395 * will be stable.
1396 *
1397 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
1398 * arithmetic overflow in the fmeter_update() routine.
1399 *
1400 * Given the simple 32 bit integer arithmetic used, this meter works
1401 * best for reporting rates between one per millisecond (msec) and
1402 * one per 32 (approx) seconds. At constant rates faster than one
1403 * per msec it maxes out at values just under 1,000,000. At constant
1404 * rates between one per msec, and one per second it will stabilize
1405 * to a value N*1000, where N is the rate of events per second.
1406 * At constant rates between one per second and one per 32 seconds,
1407 * it will be choppy, moving up on the seconds that have an event,
1408 * and then decaying until the next event. At rates slower than
1409 * about one in 32 seconds, it decays all the way back to zero between
1410 * each event.
1411 */
1412
1413#define FM_COEF 933 /* coefficient for half-life of 10 secs */
1414#define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
1415#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
1416#define FM_SCALE 1000 /* faux fixed point scale */
1417
1418/* Initialize a frequency meter */
1419static void fmeter_init(struct fmeter *fmp)
1420{
1421 fmp->cnt = 0;
1422 fmp->val = 0;
1423 fmp->time = 0;
1424 spin_lock_init(&fmp->lock);
1425}
1426
1427/* Internal meter update - process cnt events and update value */
1428static void fmeter_update(struct fmeter *fmp)
1429{
1430 time_t now = get_seconds();
1431 time_t ticks = now - fmp->time;
1432
1433 if (ticks == 0)
1434 return;
1435
1436 ticks = min(FM_MAXTICKS, ticks);
1437 while (ticks-- > 0)
1438 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
1439 fmp->time = now;
1440
1441 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
1442 fmp->cnt = 0;
1443}
1444
1445/* Process any previous ticks, then bump cnt by one (times scale). */
1446static void fmeter_markevent(struct fmeter *fmp)
1447{
1448 spin_lock(&fmp->lock);
1449 fmeter_update(fmp);
1450 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
1451 spin_unlock(&fmp->lock);
1452}
1453
1454/* Process any previous ticks, then return current value. */
1455static int fmeter_getrate(struct fmeter *fmp)
1456{
1457 int val;
1458
1459 spin_lock(&fmp->lock);
1460 fmeter_update(fmp);
1461 val = fmp->val;
1462 spin_unlock(&fmp->lock);
1463 return val;
1464}
1465
Tejun Heo57fce0a2014-02-13 06:58:41 -05001466static struct cpuset *cpuset_attach_old_cs;
1467
Tejun Heo5d21cc22013-01-07 08:51:08 -08001468/* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001469static int cpuset_can_attach(struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07001470{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001471 struct cgroup_subsys_state *css;
1472 struct cpuset *cs;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001473 struct task_struct *task;
1474 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07001475
Tejun Heo57fce0a2014-02-13 06:58:41 -05001476 /* used later by cpuset_attach() */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001477 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
1478 cs = css_cs(css);
Tejun Heo57fce0a2014-02-13 06:58:41 -05001479
Tejun Heo5d21cc22013-01-07 08:51:08 -08001480 mutex_lock(&cpuset_mutex);
1481
Tejun Heoaa6ec292014-07-09 10:08:08 -04001482 /* allow moving tasks into an empty cpuset if on default hierarchy */
Tejun Heo5d21cc22013-01-07 08:51:08 -08001483 ret = -ENOSPC;
Tejun Heo9e10a132015-09-18 11:56:28 -04001484 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
Li Zefan88fa5232013-06-09 17:16:46 +08001485 (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)))
Tejun Heo5d21cc22013-01-07 08:51:08 -08001486 goto out_unlock;
Ben Blumbe367d02009-09-23 15:56:31 -07001487
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001488 cgroup_taskset_for_each(task, css, tset) {
Juri Lelli7f514122014-09-19 10:22:40 +01001489 ret = task_can_attach(task, cs->cpus_allowed);
1490 if (ret)
Tejun Heo5d21cc22013-01-07 08:51:08 -08001491 goto out_unlock;
1492 ret = security_task_setscheduler(task);
1493 if (ret)
1494 goto out_unlock;
Tejun Heobb9d97b2011-12-12 18:12:21 -08001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Tejun Heo452477f2013-01-07 08:51:07 -08001497 /*
1498 * Mark attach is in progress. This makes validate_change() fail
1499 * changes which zero cpus/mems_allowed.
1500 */
1501 cs->attach_in_progress++;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001502 ret = 0;
1503out_unlock:
1504 mutex_unlock(&cpuset_mutex);
1505 return ret;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001506}
1507
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001508static void cpuset_cancel_attach(struct cgroup_taskset *tset)
Tejun Heo452477f2013-01-07 08:51:07 -08001509{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001510 struct cgroup_subsys_state *css;
1511 struct cpuset *cs;
1512
1513 cgroup_taskset_first(tset, &css);
1514 cs = css_cs(css);
1515
Tejun Heo5d21cc22013-01-07 08:51:08 -08001516 mutex_lock(&cpuset_mutex);
Tejun Heoeb954192013-08-08 20:11:23 -04001517 css_cs(css)->attach_in_progress--;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001518 mutex_unlock(&cpuset_mutex);
Tejun Heo452477f2013-01-07 08:51:07 -08001519}
1520
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001521/*
Tejun Heo5d21cc22013-01-07 08:51:08 -08001522 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001523 * but we can't allocate it dynamically there. Define it global and
1524 * allocate from cpuset_init().
1525 */
1526static cpumask_var_t cpus_attach;
1527
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001528static void cpuset_attach(struct cgroup_taskset *tset)
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001529{
Li Zefan67bd2c52013-06-05 17:15:35 +08001530 /* static buf protected by cpuset_mutex */
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001531 static nodemask_t cpuset_attach_nodemask_to;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001532 struct task_struct *task;
Tejun Heo4530edd2015-09-11 15:00:19 -04001533 struct task_struct *leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001534 struct cgroup_subsys_state *css;
1535 struct cpuset *cs;
Tejun Heo57fce0a2014-02-13 06:58:41 -05001536 struct cpuset *oldcs = cpuset_attach_old_cs;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08001537
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001538 cgroup_taskset_first(tset, &css);
1539 cs = css_cs(css);
1540
Tejun Heo5d21cc22013-01-07 08:51:08 -08001541 mutex_lock(&cpuset_mutex);
1542
Tejun Heo94196f52011-12-12 18:12:22 -08001543 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07001544 if (cs == &top_cpuset)
1545 cpumask_copy(cpus_attach, cpu_possible_mask);
1546 else
Li Zefanae1c8022014-07-09 16:48:32 +08001547 guarantee_online_cpus(cs, cpus_attach);
Ben Blumf780bdb2011-05-26 16:25:19 -07001548
Li Zefanae1c8022014-07-09 16:48:32 +08001549 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08001550
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001551 cgroup_taskset_for_each(task, css, tset) {
Tejun Heobb9d97b2011-12-12 18:12:21 -08001552 /*
1553 * can_attach beforehand should guarantee that this doesn't
1554 * fail. TODO: have a better way to handle failure here
1555 */
1556 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
1557
1558 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
1559 cpuset_update_task_spread_flag(cs, task);
1560 }
David Quigley22fb52d2006-06-23 02:04:00 -07001561
Ben Blumf780bdb2011-05-26 16:25:19 -07001562 /*
Tejun Heo4530edd2015-09-11 15:00:19 -04001563 * Change mm for all threadgroup leaders. This is expensive and may
1564 * sleep and should be moved outside migration path proper.
Ben Blumf780bdb2011-05-26 16:25:19 -07001565 */
Li Zefanae1c8022014-07-09 16:48:32 +08001566 cpuset_attach_nodemask_to = cs->effective_mems;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05001567 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo3df9ca02015-09-11 15:00:18 -04001568 struct mm_struct *mm = get_task_mm(leader);
Li Zefanf047cec2013-06-13 15:11:44 +08001569
Tejun Heo3df9ca02015-09-11 15:00:18 -04001570 if (mm) {
1571 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
1572
1573 /*
1574 * old_mems_allowed is the same with mems_allowed
1575 * here, except if this task is being moved
1576 * automatically due to hotplug. In that case
1577 * @mems_allowed has been updated and is empty, so
1578 * @old_mems_allowed is the right nodesets that we
1579 * migrate mm from.
1580 */
Tejun Heofff4dc82016-01-19 12:18:41 -05001581 if (is_memory_migrate(cs))
Tejun Heo3df9ca02015-09-11 15:00:18 -04001582 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed,
1583 &cpuset_attach_nodemask_to);
Tejun Heofff4dc82016-01-19 12:18:41 -05001584 else
1585 mmput(mm);
Li Zefanf047cec2013-06-13 15:11:44 +08001586 }
Paul Jackson42253992006-01-08 01:01:59 -08001587 }
Tejun Heo452477f2013-01-07 08:51:07 -08001588
Li Zefan33ad8012013-06-09 17:15:08 +08001589 cs->old_mems_allowed = cpuset_attach_nodemask_to;
Tejun Heo02bb5862013-01-07 08:51:08 -08001590
Tejun Heo452477f2013-01-07 08:51:07 -08001591 cs->attach_in_progress--;
Li Zefane44193d2013-06-09 17:14:22 +08001592 if (!cs->attach_in_progress)
1593 wake_up(&cpuset_attach_wq);
Tejun Heo5d21cc22013-01-07 08:51:08 -08001594
1595 mutex_unlock(&cpuset_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598/* The various types of files and directories in a cpuset file system */
1599
1600typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08001601 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 FILE_CPULIST,
1603 FILE_MEMLIST,
Li Zefanafd1a8b2014-07-09 16:49:25 +08001604 FILE_EFFECTIVE_CPULIST,
1605 FILE_EFFECTIVE_MEMLIST,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 FILE_CPU_EXCLUSIVE,
1607 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07001608 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07001609 FILE_SCHED_LOAD_BALANCE,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001610 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001611 FILE_MEMORY_PRESSURE_ENABLED,
1612 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08001613 FILE_SPREAD_PAGE,
1614 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615} cpuset_filetype_t;
1616
Tejun Heo182446d2013-08-08 20:11:24 -04001617static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
1618 u64 val)
Paul Menage700fe1a2008-04-29 01:00:00 -07001619{
Tejun Heo182446d2013-08-08 20:11:24 -04001620 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07001621 cpuset_filetype_t type = cft->private;
Li Zefana903f082013-08-13 10:05:59 +08001622 int retval = 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001623
Tejun Heo5d21cc22013-01-07 08:51:08 -08001624 mutex_lock(&cpuset_mutex);
Li Zefana903f082013-08-13 10:05:59 +08001625 if (!is_cpuset_online(cs)) {
1626 retval = -ENODEV;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001627 goto out_unlock;
Li Zefana903f082013-08-13 10:05:59 +08001628 }
Paul Menage700fe1a2008-04-29 01:00:00 -07001629
1630 switch (type) {
1631 case FILE_CPU_EXCLUSIVE:
1632 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
1633 break;
1634 case FILE_MEM_EXCLUSIVE:
1635 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
1636 break;
Paul Menage78608362008-04-29 01:00:26 -07001637 case FILE_MEM_HARDWALL:
1638 retval = update_flag(CS_MEM_HARDWALL, cs, val);
1639 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001640 case FILE_SCHED_LOAD_BALANCE:
1641 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
1642 break;
1643 case FILE_MEMORY_MIGRATE:
1644 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
1645 break;
1646 case FILE_MEMORY_PRESSURE_ENABLED:
1647 cpuset_memory_pressure_enabled = !!val;
1648 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07001649 case FILE_SPREAD_PAGE:
1650 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001651 break;
1652 case FILE_SPREAD_SLAB:
1653 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07001654 break;
1655 default:
1656 retval = -EINVAL;
1657 break;
1658 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08001659out_unlock:
1660 mutex_unlock(&cpuset_mutex);
Paul Menage700fe1a2008-04-29 01:00:00 -07001661 return retval;
1662}
1663
Tejun Heo182446d2013-08-08 20:11:24 -04001664static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
1665 s64 val)
Paul Menage5be7a472008-05-06 20:42:41 -07001666{
Tejun Heo182446d2013-08-08 20:11:24 -04001667 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07001668 cpuset_filetype_t type = cft->private;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001669 int retval = -ENODEV;
Paul Menage5be7a472008-05-06 20:42:41 -07001670
Tejun Heo5d21cc22013-01-07 08:51:08 -08001671 mutex_lock(&cpuset_mutex);
1672 if (!is_cpuset_online(cs))
1673 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07001674
Paul Menage5be7a472008-05-06 20:42:41 -07001675 switch (type) {
1676 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1677 retval = update_relax_domain_level(cs, val);
1678 break;
1679 default:
1680 retval = -EINVAL;
1681 break;
1682 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08001683out_unlock:
1684 mutex_unlock(&cpuset_mutex);
Paul Menage5be7a472008-05-06 20:42:41 -07001685 return retval;
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/*
Paul Menagee3712392008-07-25 01:47:02 -07001689 * Common handling for a write to a "cpus" or "mems" file.
1690 */
Tejun Heo451af502014-05-13 12:16:21 -04001691static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
1692 char *buf, size_t nbytes, loff_t off)
Paul Menagee3712392008-07-25 01:47:02 -07001693{
Tejun Heo451af502014-05-13 12:16:21 -04001694 struct cpuset *cs = css_cs(of_css(of));
Li Zefan645fcc92009-01-07 18:08:43 -08001695 struct cpuset *trialcs;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001696 int retval = -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07001697
Tejun Heo451af502014-05-13 12:16:21 -04001698 buf = strstrip(buf);
1699
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001700 /*
1701 * CPU or memory hotunplug may leave @cs w/o any execution
1702 * resources, in which case the hotplug code asynchronously updates
1703 * configuration and transfers all tasks to the nearest ancestor
1704 * which can execute.
1705 *
1706 * As writes to "cpus" or "mems" may restore @cs's execution
1707 * resources, wait for the previously scheduled operations before
1708 * proceeding, so that we don't end up keep removing tasks added
1709 * after execution capability is restored.
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001710 *
1711 * cpuset_hotplug_work calls back into cgroup core via
1712 * cgroup_transfer_tasks() and waiting for it from a cgroupfs
1713 * operation like this one can lead to a deadlock through kernfs
1714 * active_ref protection. Let's break the protection. Losing the
1715 * protection is okay as we check whether @cs is online after
1716 * grabbing cpuset_mutex anyway. This only happens on the legacy
1717 * hierarchies.
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001718 */
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001719 css_get(&cs->css);
1720 kernfs_break_active_protection(of->kn);
Tejun Heo3a5a6d02013-01-07 08:51:07 -08001721 flush_work(&cpuset_hotplug_work);
1722
Tejun Heo5d21cc22013-01-07 08:51:08 -08001723 mutex_lock(&cpuset_mutex);
1724 if (!is_cpuset_online(cs))
1725 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07001726
Li Zefan645fcc92009-01-07 18:08:43 -08001727 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08001728 if (!trialcs) {
1729 retval = -ENOMEM;
Tejun Heo5d21cc22013-01-07 08:51:08 -08001730 goto out_unlock;
Li Zefanb75f38d2011-03-04 17:36:21 -08001731 }
Li Zefan645fcc92009-01-07 18:08:43 -08001732
Tejun Heo451af502014-05-13 12:16:21 -04001733 switch (of_cft(of)->private) {
Paul Menagee3712392008-07-25 01:47:02 -07001734 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001735 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001736 break;
1737 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08001738 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07001739 break;
1740 default:
1741 retval = -EINVAL;
1742 break;
1743 }
Li Zefan645fcc92009-01-07 18:08:43 -08001744
1745 free_trial_cpuset(trialcs);
Tejun Heo5d21cc22013-01-07 08:51:08 -08001746out_unlock:
1747 mutex_unlock(&cpuset_mutex);
Tejun Heo76bb5ab2014-06-30 15:47:32 -04001748 kernfs_unbreak_active_protection(of->kn);
1749 css_put(&cs->css);
Tejun Heofff4dc82016-01-19 12:18:41 -05001750 flush_workqueue(cpuset_migrate_mm_wq);
Tejun Heo451af502014-05-13 12:16:21 -04001751 return retval ?: nbytes;
Paul Menagee3712392008-07-25 01:47:02 -07001752}
1753
1754/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * These ascii lists should be read in a single call, by using a user
1756 * buffer large enough to hold the entire map. If read in smaller
1757 * chunks, there is no guarantee of atomicity. Since the display format
1758 * used, list of ranges of sequential numbers, is variable length,
1759 * and since these maps can change value dynamically, one could read
1760 * gibberish by doing partial reads while a list was changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001762static int cpuset_common_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001764 struct cpuset *cs = css_cs(seq_css(sf));
1765 cpuset_filetype_t type = seq_cft(sf)->private;
Tejun Heo51ffe412013-12-05 12:28:02 -05001766 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001768 spin_lock_irq(&callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 switch (type) {
1771 case FILE_CPULIST:
Riley Andrews7ec5a322016-09-06 15:16:25 -07001772 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_requested));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 break;
1774 case FILE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001775 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
Li Zefanafd1a8b2014-07-09 16:49:25 +08001777 case FILE_EFFECTIVE_CPULIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001778 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus));
Li Zefanafd1a8b2014-07-09 16:49:25 +08001779 break;
1780 case FILE_EFFECTIVE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08001781 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems));
Li Zefanafd1a8b2014-07-09 16:49:25 +08001782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 default:
Tejun Heo51ffe412013-12-05 12:28:02 -05001784 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001787 spin_unlock_irq(&callback_lock);
Tejun Heo51ffe412013-12-05 12:28:02 -05001788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Tejun Heo182446d2013-08-08 20:11:24 -04001791static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage700fe1a2008-04-29 01:00:00 -07001792{
Tejun Heo182446d2013-08-08 20:11:24 -04001793 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07001794 cpuset_filetype_t type = cft->private;
1795 switch (type) {
1796 case FILE_CPU_EXCLUSIVE:
1797 return is_cpu_exclusive(cs);
1798 case FILE_MEM_EXCLUSIVE:
1799 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07001800 case FILE_MEM_HARDWALL:
1801 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07001802 case FILE_SCHED_LOAD_BALANCE:
1803 return is_sched_load_balance(cs);
1804 case FILE_MEMORY_MIGRATE:
1805 return is_memory_migrate(cs);
1806 case FILE_MEMORY_PRESSURE_ENABLED:
1807 return cpuset_memory_pressure_enabled;
1808 case FILE_MEMORY_PRESSURE:
1809 return fmeter_getrate(&cs->fmeter);
1810 case FILE_SPREAD_PAGE:
1811 return is_spread_page(cs);
1812 case FILE_SPREAD_SLAB:
1813 return is_spread_slab(cs);
1814 default:
1815 BUG();
1816 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001817
1818 /* Unreachable but makes gcc happy */
1819 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07001820}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Tejun Heo182446d2013-08-08 20:11:24 -04001822static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage5be7a472008-05-06 20:42:41 -07001823{
Tejun Heo182446d2013-08-08 20:11:24 -04001824 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07001825 cpuset_filetype_t type = cft->private;
1826 switch (type) {
1827 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
1828 return cs->relax_domain_level;
1829 default:
1830 BUG();
1831 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07001832
1833 /* Unrechable but makes gcc happy */
1834 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07001835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838/*
1839 * for the common functions, 'private' gives the type of file
1840 */
1841
Paul Menageaddf2c72008-04-29 01:00:26 -07001842static struct cftype files[] = {
1843 {
1844 .name = "cpus",
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 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07001848 .private = FILE_CPULIST,
1849 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Paul Menageaddf2c72008-04-29 01:00:26 -07001851 {
1852 .name = "mems",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001853 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04001854 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07001855 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07001856 .private = FILE_MEMLIST,
1857 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Paul Menageaddf2c72008-04-29 01:00:26 -07001859 {
Li Zefanafd1a8b2014-07-09 16:49:25 +08001860 .name = "effective_cpus",
1861 .seq_show = cpuset_common_seq_show,
1862 .private = FILE_EFFECTIVE_CPULIST,
1863 },
1864
1865 {
1866 .name = "effective_mems",
1867 .seq_show = cpuset_common_seq_show,
1868 .private = FILE_EFFECTIVE_MEMLIST,
1869 },
1870
1871 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001872 .name = "cpu_exclusive",
1873 .read_u64 = cpuset_read_u64,
1874 .write_u64 = cpuset_write_u64,
1875 .private = FILE_CPU_EXCLUSIVE,
1876 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Paul Menageaddf2c72008-04-29 01:00:26 -07001878 {
1879 .name = "mem_exclusive",
1880 .read_u64 = cpuset_read_u64,
1881 .write_u64 = cpuset_write_u64,
1882 .private = FILE_MEM_EXCLUSIVE,
1883 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Paul Menageaddf2c72008-04-29 01:00:26 -07001885 {
Paul Menage78608362008-04-29 01:00:26 -07001886 .name = "mem_hardwall",
1887 .read_u64 = cpuset_read_u64,
1888 .write_u64 = cpuset_write_u64,
1889 .private = FILE_MEM_HARDWALL,
1890 },
1891
1892 {
Paul Menageaddf2c72008-04-29 01:00:26 -07001893 .name = "sched_load_balance",
1894 .read_u64 = cpuset_read_u64,
1895 .write_u64 = cpuset_write_u64,
1896 .private = FILE_SCHED_LOAD_BALANCE,
1897 },
Paul Jackson029190c2007-10-18 23:40:20 -07001898
Paul Menageaddf2c72008-04-29 01:00:26 -07001899 {
1900 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07001901 .read_s64 = cpuset_read_s64,
1902 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07001903 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
1904 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001905
Paul Menageaddf2c72008-04-29 01:00:26 -07001906 {
1907 .name = "memory_migrate",
1908 .read_u64 = cpuset_read_u64,
1909 .write_u64 = cpuset_write_u64,
1910 .private = FILE_MEMORY_MIGRATE,
1911 },
1912
1913 {
1914 .name = "memory_pressure",
1915 .read_u64 = cpuset_read_u64,
Waiman Longed48d922017-08-24 12:04:29 -04001916 .private = FILE_MEMORY_PRESSURE,
Paul Menageaddf2c72008-04-29 01:00:26 -07001917 },
1918
1919 {
1920 .name = "memory_spread_page",
1921 .read_u64 = cpuset_read_u64,
1922 .write_u64 = cpuset_write_u64,
1923 .private = FILE_SPREAD_PAGE,
1924 },
1925
1926 {
1927 .name = "memory_spread_slab",
1928 .read_u64 = cpuset_read_u64,
1929 .write_u64 = cpuset_write_u64,
1930 .private = FILE_SPREAD_SLAB,
1931 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07001932
1933 {
1934 .name = "memory_pressure_enabled",
1935 .flags = CFTYPE_ONLY_ON_ROOT,
1936 .read_u64 = cpuset_read_u64,
1937 .write_u64 = cpuset_write_u64,
1938 .private = FILE_MEMORY_PRESSURE_ENABLED,
1939 },
1940
1941 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08001942};
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944/*
Tejun Heo92fb9742012-11-19 08:13:38 -08001945 * cpuset_css_alloc - allocate a cpuset css
Li Zefanc9e5fe62013-06-14 11:18:27 +08001946 * cgrp: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
1948
Tejun Heoeb954192013-08-08 20:11:23 -04001949static struct cgroup_subsys_state *
1950cpuset_css_alloc(struct cgroup_subsys_state *parent_css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Tejun Heoc8f699b2013-01-07 08:51:07 -08001952 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Tejun Heoeb954192013-08-08 20:11:23 -04001954 if (!parent_css)
Paul Menage8793d852007-10-18 23:39:39 -07001955 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08001956
Tejun Heoc8f699b2013-01-07 08:51:07 -08001957 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07001959 return ERR_PTR(-ENOMEM);
Li Zefane2b9a3d2014-07-09 16:47:03 +08001960 if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL))
1961 goto free_cs;
Riley Andrews7ec5a322016-09-06 15:16:25 -07001962 if (!alloc_cpumask_var(&cs->cpus_requested, GFP_KERNEL))
1963 goto free_allowed;
Li Zefane2b9a3d2014-07-09 16:47:03 +08001964 if (!alloc_cpumask_var(&cs->effective_cpus, GFP_KERNEL))
Riley Andrews7ec5a322016-09-06 15:16:25 -07001965 goto free_requested;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Paul Jackson029190c2007-10-18 23:40:20 -07001967 set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Li Zefan300ed6c2009-01-07 18:08:44 -08001968 cpumask_clear(cs->cpus_allowed);
Riley Andrews7ec5a322016-09-06 15:16:25 -07001969 cpumask_clear(cs->cpus_requested);
Mike Travisf9a86fc2008-04-04 18:11:07 -07001970 nodes_clear(cs->mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08001971 cpumask_clear(cs->effective_cpus);
1972 nodes_clear(cs->effective_mems);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08001973 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001974 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Tejun Heoc8f699b2013-01-07 08:51:07 -08001976 return &cs->css;
Li Zefane2b9a3d2014-07-09 16:47:03 +08001977
Riley Andrews7ec5a322016-09-06 15:16:25 -07001978free_requested:
1979 free_cpumask_var(cs->cpus_requested);
1980free_allowed:
Li Zefane2b9a3d2014-07-09 16:47:03 +08001981 free_cpumask_var(cs->cpus_allowed);
1982free_cs:
1983 kfree(cs);
1984 return ERR_PTR(-ENOMEM);
Tejun Heoc8f699b2013-01-07 08:51:07 -08001985}
1986
Tejun Heoeb954192013-08-08 20:11:23 -04001987static int cpuset_css_online(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08001988{
Tejun Heoeb954192013-08-08 20:11:23 -04001989 struct cpuset *cs = css_cs(css);
Tejun Heoc4310692013-01-07 08:51:08 -08001990 struct cpuset *parent = parent_cs(cs);
Tejun Heoae8086c2013-01-07 08:51:07 -08001991 struct cpuset *tmp_cs;
Tejun Heo492eb212013-08-08 20:11:25 -04001992 struct cgroup_subsys_state *pos_css;
Tejun Heoc8f699b2013-01-07 08:51:07 -08001993
1994 if (!parent)
1995 return 0;
1996
Tejun Heo5d21cc22013-01-07 08:51:08 -08001997 mutex_lock(&cpuset_mutex);
1998
Tejun Heoefeb77b2013-01-07 08:51:07 -08001999 set_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002000 if (is_spread_page(parent))
2001 set_bit(CS_SPREAD_PAGE, &cs->flags);
2002 if (is_spread_slab(parent))
2003 set_bit(CS_SPREAD_SLAB, &cs->flags);
2004
Mel Gorman664eedd2014-06-04 16:10:08 -07002005 cpuset_inc();
Tejun Heo033fa1c2012-11-19 08:13:39 -08002006
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002007 spin_lock_irq(&callback_lock);
Tejun Heo9e10a132015-09-18 11:56:28 -04002008 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
Li Zefane2b9a3d2014-07-09 16:47:03 +08002009 cpumask_copy(cs->effective_cpus, parent->effective_cpus);
2010 cs->effective_mems = parent->effective_mems;
2011 }
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002012 spin_unlock_irq(&callback_lock);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002013
Tejun Heoeb954192013-08-08 20:11:23 -04002014 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
Tejun Heo5d21cc22013-01-07 08:51:08 -08002015 goto out_unlock;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002016
2017 /*
2018 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
2019 * set. This flag handling is implemented in cgroup core for
2020 * histrical reasons - the flag may be specified during mount.
2021 *
2022 * Currently, if any sibling cpusets have exclusive cpus or mem, we
2023 * refuse to clone the configuration - thereby refusing the task to
2024 * be entered, and as a result refusing the sys_unshare() or
2025 * clone() which initiated it. If this becomes a problem for some
2026 * users who wish to allow that scenario, then this could be
2027 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2028 * (and likewise for mems) to the new cgroup.
2029 */
Tejun Heoae8086c2013-01-07 08:51:07 -08002030 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002031 cpuset_for_each_child(tmp_cs, pos_css, parent) {
Tejun Heoae8086c2013-01-07 08:51:07 -08002032 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
2033 rcu_read_unlock();
Tejun Heo5d21cc22013-01-07 08:51:08 -08002034 goto out_unlock;
Tejun Heoae8086c2013-01-07 08:51:07 -08002035 }
Tejun Heo033fa1c2012-11-19 08:13:39 -08002036 }
Tejun Heoae8086c2013-01-07 08:51:07 -08002037 rcu_read_unlock();
Tejun Heo033fa1c2012-11-19 08:13:39 -08002038
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002039 spin_lock_irq(&callback_lock);
Tejun Heo033fa1c2012-11-19 08:13:39 -08002040 cs->mems_allowed = parent->mems_allowed;
Zefan Li790317e2015-02-13 11:19:49 +08002041 cs->effective_mems = parent->mems_allowed;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002042 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
Riley Andrews7ec5a322016-09-06 15:16:25 -07002043 cpumask_copy(cs->cpus_requested, parent->cpus_requested);
Zefan Li790317e2015-02-13 11:19:49 +08002044 cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
Dan Carpentercea74462014-10-27 16:27:02 +03002045 spin_unlock_irq(&callback_lock);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002046out_unlock:
2047 mutex_unlock(&cpuset_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002048 return 0;
2049}
2050
Zhao Hongjiang0b9e6962013-07-27 11:56:53 +08002051/*
2052 * If the cpuset being removed has its flag 'sched_load_balance'
2053 * enabled, then simulate turning sched_load_balance off, which
2054 * will call rebuild_sched_domains_locked().
2055 */
2056
Tejun Heoeb954192013-08-08 20:11:23 -04002057static void cpuset_css_offline(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08002058{
Tejun Heoeb954192013-08-08 20:11:23 -04002059 struct cpuset *cs = css_cs(css);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002060
Tejun Heo5d21cc22013-01-07 08:51:08 -08002061 mutex_lock(&cpuset_mutex);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002062
2063 if (is_sched_load_balance(cs))
2064 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
2065
Mel Gorman664eedd2014-06-04 16:10:08 -07002066 cpuset_dec();
Tejun Heoefeb77b2013-01-07 08:51:07 -08002067 clear_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002068
Tejun Heo5d21cc22013-01-07 08:51:08 -08002069 mutex_unlock(&cpuset_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Tejun Heoeb954192013-08-08 20:11:23 -04002072static void cpuset_css_free(struct cgroup_subsys_state *css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Tejun Heoeb954192013-08-08 20:11:23 -04002074 struct cpuset *cs = css_cs(css);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Li Zefane2b9a3d2014-07-09 16:47:03 +08002076 free_cpumask_var(cs->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -08002077 free_cpumask_var(cs->cpus_allowed);
Riley Andrews7ec5a322016-09-06 15:16:25 -07002078 free_cpumask_var(cs->cpus_requested);
Paul Menage8793d852007-10-18 23:39:39 -07002079 kfree(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Li Zefan39bd0d12014-07-09 16:48:01 +08002082static void cpuset_bind(struct cgroup_subsys_state *root_css)
2083{
2084 mutex_lock(&cpuset_mutex);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002085 spin_lock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002086
Tejun Heo9e10a132015-09-18 11:56:28 -04002087 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
Li Zefan39bd0d12014-07-09 16:48:01 +08002088 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
2089 top_cpuset.mems_allowed = node_possible_map;
2090 } else {
2091 cpumask_copy(top_cpuset.cpus_allowed,
2092 top_cpuset.effective_cpus);
2093 top_cpuset.mems_allowed = top_cpuset.effective_mems;
2094 }
2095
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002096 spin_unlock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002097 mutex_unlock(&cpuset_mutex);
2098}
2099
Riley Andrews5554a302015-06-05 18:59:29 -07002100static int cpuset_allow_attach(struct cgroup_taskset *tset)
2101{
2102 const struct cred *cred = current_cred(), *tcred;
2103 struct task_struct *task;
2104 struct cgroup_subsys_state *css;
2105
2106 cgroup_taskset_for_each(task, css, tset) {
2107 tcred = __task_cred(task);
2108
2109 if ((current != task) && !capable(CAP_SYS_ADMIN) &&
2110 cred->euid.val != tcred->uid.val && cred->euid.val != tcred->suid.val)
2111 return -EACCES;
2112 }
2113
2114 return 0;
2115}
2116
Zefan Li83f68762016-08-09 11:25:01 +08002117/*
2118 * Make sure the new task conform to the current state of its parent,
2119 * which could have been changed by cpuset just after it inherits the
2120 * state from the parent and before it sits on the cgroup's task list.
2121 */
Arnd Bergmannd0d76972016-09-24 23:29:51 +02002122void cpuset_fork(struct task_struct *task, void *priv)
Zefan Li83f68762016-08-09 11:25:01 +08002123{
2124 if (task_css_is_root(task, cpuset_cgrp_id))
2125 return;
2126
2127 set_cpus_allowed_ptr(task, &current->cpus_allowed);
2128 task->mems_allowed = current->mems_allowed;
2129}
2130
Tejun Heo073219e2014-02-08 10:36:58 -05002131struct cgroup_subsys cpuset_cgrp_subsys = {
Li Zefan39bd0d12014-07-09 16:48:01 +08002132 .css_alloc = cpuset_css_alloc,
2133 .css_online = cpuset_css_online,
2134 .css_offline = cpuset_css_offline,
2135 .css_free = cpuset_css_free,
2136 .can_attach = cpuset_can_attach,
Riley Andrews5554a302015-06-05 18:59:29 -07002137 .allow_attach = cpuset_allow_attach,
Li Zefan39bd0d12014-07-09 16:48:01 +08002138 .cancel_attach = cpuset_cancel_attach,
2139 .attach = cpuset_attach,
Tejun Heod5209742016-04-21 19:06:48 -04002140 .post_attach = cpuset_post_attach,
Li Zefan39bd0d12014-07-09 16:48:01 +08002141 .bind = cpuset_bind,
Zefan Li83f68762016-08-09 11:25:01 +08002142 .fork = cpuset_fork,
Tejun Heo55779642014-07-15 11:05:09 -04002143 .legacy_cftypes = files,
Li Zefan39bd0d12014-07-09 16:48:01 +08002144 .early_init = 1,
Paul Menage8793d852007-10-18 23:39:39 -07002145};
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/**
2148 * cpuset_init - initialize cpusets at system boot
2149 *
2150 * Description: Initialize top_cpuset and the cpuset internal file system,
2151 **/
2152
2153int __init cpuset_init(void)
2154{
Paul Menage8793d852007-10-18 23:39:39 -07002155 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Miao Xie58568d22009-06-16 15:31:49 -07002157 if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
2158 BUG();
Li Zefane2b9a3d2014-07-09 16:47:03 +08002159 if (!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL))
2160 BUG();
Riley Andrews7ec5a322016-09-06 15:16:25 -07002161 if (!alloc_cpumask_var(&top_cpuset.cpus_requested, GFP_KERNEL))
2162 BUG();
Miao Xie58568d22009-06-16 15:31:49 -07002163
Li Zefan300ed6c2009-01-07 18:08:44 -08002164 cpumask_setall(top_cpuset.cpus_allowed);
Riley Andrews7ec5a322016-09-06 15:16:25 -07002165 cpumask_setall(top_cpuset.cpus_requested);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002166 nodes_setall(top_cpuset.mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002167 cpumask_setall(top_cpuset.effective_cpus);
2168 nodes_setall(top_cpuset.effective_mems);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002170 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07002171 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002172 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 err = register_filesystem(&cpuset_fs_type);
2175 if (err < 0)
Paul Menage8793d852007-10-18 23:39:39 -07002176 return err;
2177
Li Zefan2341d1b2009-01-07 18:08:42 -08002178 if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
2179 BUG();
2180
Paul Menage8793d852007-10-18 23:39:39 -07002181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002184/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002185 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002186 * or memory nodes, we need to walk over the cpuset hierarchy,
2187 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002188 * last CPU or node from a cpuset, then move the tasks in the empty
2189 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002190 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002191static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002192{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002193 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002194
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002195 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08002196 * Find its next-highest non-empty parent, (top cpuset
2197 * has online cpus, so can't be empty).
2198 */
Tejun Heoc4310692013-01-07 08:51:08 -08002199 parent = parent_cs(cs);
Li Zefan300ed6c2009-01-07 18:08:44 -08002200 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002201 nodes_empty(parent->mems_allowed))
Tejun Heoc4310692013-01-07 08:51:08 -08002202 parent = parent_cs(parent);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002203
Tejun Heo8cc99342013-04-07 09:29:50 -07002204 if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) {
Fabian Frederick12d30892014-05-05 19:49:00 +02002205 pr_err("cpuset: failed to transfer tasks out of empty cpuset ");
Tejun Heoe61734c2014-02-12 09:29:50 -05002206 pr_cont_cgroup_name(cs->css.cgroup);
2207 pr_cont("\n");
Tejun Heo8cc99342013-04-07 09:29:50 -07002208 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002209}
2210
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002211static void
2212hotplug_update_tasks_legacy(struct cpuset *cs,
2213 struct cpumask *new_cpus, nodemask_t *new_mems,
2214 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002215{
2216 bool is_empty;
2217
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002218 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002219 cpumask_copy(cs->cpus_allowed, new_cpus);
2220 cpumask_copy(cs->effective_cpus, new_cpus);
2221 cs->mems_allowed = *new_mems;
2222 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002223 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08002224
2225 /*
2226 * Don't call update_tasks_cpumask() if the cpuset becomes empty,
2227 * as the tasks will be migratecd to an ancestor.
2228 */
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002229 if (cpus_updated && !cpumask_empty(cs->cpus_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08002230 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002231 if (mems_updated && !nodes_empty(cs->mems_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08002232 update_tasks_nodemask(cs);
2233
2234 is_empty = cpumask_empty(cs->cpus_allowed) ||
2235 nodes_empty(cs->mems_allowed);
2236
2237 mutex_unlock(&cpuset_mutex);
2238
2239 /*
2240 * Move tasks to the nearest ancestor with execution resources,
2241 * This is full cgroup operation which will also call back into
2242 * cpuset. Should be done outside any lock.
2243 */
2244 if (is_empty)
2245 remove_tasks_in_empty_cpuset(cs);
2246
2247 mutex_lock(&cpuset_mutex);
2248}
2249
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002250static void
2251hotplug_update_tasks(struct cpuset *cs,
2252 struct cpumask *new_cpus, nodemask_t *new_mems,
2253 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002254{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002255 if (cpumask_empty(new_cpus))
2256 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus);
2257 if (nodes_empty(*new_mems))
2258 *new_mems = parent_cs(cs)->effective_mems;
Li Zefan390a36a2014-07-09 16:48:54 +08002259
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002260 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002261 cpumask_copy(cs->effective_cpus, new_cpus);
2262 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002263 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08002264
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002265 if (cpus_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002266 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002267 if (mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002268 update_tasks_nodemask(cs);
2269}
2270
Tejun Heodeb7aa32013-01-07 08:51:07 -08002271/**
Li Zefan388afd82013-06-09 17:14:47 +08002272 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
Tejun Heodeb7aa32013-01-07 08:51:07 -08002273 * @cs: cpuset in interest
Cliff Wickman956db3c2008-02-07 00:14:43 -08002274 *
Tejun Heodeb7aa32013-01-07 08:51:07 -08002275 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
2276 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
2277 * all its tasks are moved to the nearest ancestor with both resources.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002278 */
Li Zefan388afd82013-06-09 17:14:47 +08002279static void cpuset_hotplug_update_tasks(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002280{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002281 static cpumask_t new_cpus;
2282 static nodemask_t new_mems;
2283 bool cpus_updated;
2284 bool mems_updated;
Li Zefane44193d2013-06-09 17:14:22 +08002285retry:
2286 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002287
Tejun Heo5d21cc22013-01-07 08:51:08 -08002288 mutex_lock(&cpuset_mutex);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002289
Li Zefane44193d2013-06-09 17:14:22 +08002290 /*
2291 * We have raced with task attaching. We wait until attaching
2292 * is finished, so we won't attach a task to an empty cpuset.
2293 */
2294 if (cs->attach_in_progress) {
2295 mutex_unlock(&cpuset_mutex);
2296 goto retry;
2297 }
2298
Riley Andrews7ec5a322016-09-06 15:16:25 -07002299 cpumask_and(&new_cpus, cs->cpus_requested, parent_cs(cs)->effective_cpus);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002300 nodes_and(new_mems, cs->mems_allowed, parent_cs(cs)->effective_mems);
Paul Jacksonb4501292008-02-07 00:14:47 -08002301
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002302 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
2303 mems_updated = !nodes_equal(new_mems, cs->effective_mems);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002304
Tejun Heo9e10a132015-09-18 11:56:28 -04002305 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys))
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002306 hotplug_update_tasks(cs, &new_cpus, &new_mems,
2307 cpus_updated, mems_updated);
Li Zefan390a36a2014-07-09 16:48:54 +08002308 else
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002309 hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems,
2310 cpus_updated, mems_updated);
Tejun Heo8d033942013-01-07 08:51:07 -08002311
Tejun Heo5d21cc22013-01-07 08:51:08 -08002312 mutex_unlock(&cpuset_mutex);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002313}
Paul Jacksonb4501292008-02-07 00:14:47 -08002314
Peter Zijlstra90fd6732017-09-07 11:13:38 +02002315static bool force_rebuild;
2316
2317void cpuset_force_rebuild(void)
2318{
2319 force_rebuild = true;
2320}
2321
Tejun Heodeb7aa32013-01-07 08:51:07 -08002322/**
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002323 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
Tejun Heodeb7aa32013-01-07 08:51:07 -08002324 *
2325 * This function is called after either CPU or memory configuration has
2326 * changed and updates cpuset accordingly. The top_cpuset is always
2327 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
2328 * order to make cpusets transparent (of no affect) on systems that are
2329 * actively using CPU hotplug but making no active use of cpusets.
2330 *
2331 * Non-root cpusets are only affected by offlining. If any CPUs or memory
Li Zefan388afd82013-06-09 17:14:47 +08002332 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
2333 * all descendants.
Tejun Heodeb7aa32013-01-07 08:51:07 -08002334 *
2335 * Note that CPU offlining during suspend is ignored. We don't modify
2336 * cpusets across suspend/resume cycles at all.
2337 */
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002338static void cpuset_hotplug_workfn(struct work_struct *work)
Tejun Heodeb7aa32013-01-07 08:51:07 -08002339{
Li Zefan5c5cc622013-06-09 17:16:29 +08002340 static cpumask_t new_cpus;
2341 static nodemask_t new_mems;
Tejun Heodeb7aa32013-01-07 08:51:07 -08002342 bool cpus_updated, mems_updated;
Tejun Heo9e10a132015-09-18 11:56:28 -04002343 bool on_dfl = cgroup_subsys_on_dfl(cpuset_cgrp_subsys);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002344
Tejun Heo5d21cc22013-01-07 08:51:08 -08002345 mutex_lock(&cpuset_mutex);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002346
Tejun Heodeb7aa32013-01-07 08:51:07 -08002347 /* fetch the available cpus/mems and find out which changed how */
2348 cpumask_copy(&new_cpus, cpu_active_mask);
2349 new_mems = node_states[N_MEMORY];
Cliff Wickman956db3c2008-02-07 00:14:43 -08002350
Li Zefan7e882912014-07-09 16:48:42 +08002351 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus);
2352 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302353
Tejun Heodeb7aa32013-01-07 08:51:07 -08002354 /* synchronize cpus_allowed to cpu_active_mask */
2355 if (cpus_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002356 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08002357 if (!on_dfl)
2358 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
Li Zefan1344ab92014-07-09 16:47:16 +08002359 cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002360 spin_unlock_irq(&callback_lock);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002361 /* we don't mess with cpumasks of tasks in top_cpuset */
2362 }
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302363
Tejun Heodeb7aa32013-01-07 08:51:07 -08002364 /* synchronize mems_allowed to N_MEMORY */
2365 if (mems_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002366 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08002367 if (!on_dfl)
2368 top_cpuset.mems_allowed = new_mems;
Li Zefan1344ab92014-07-09 16:47:16 +08002369 top_cpuset.effective_mems = new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002370 spin_unlock_irq(&callback_lock);
Tejun Heod66393e2014-02-13 06:58:40 -05002371 update_tasks_nodemask(&top_cpuset);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002372 }
2373
Tejun Heo5d21cc22013-01-07 08:51:08 -08002374 mutex_unlock(&cpuset_mutex);
Tejun Heodeb7aa32013-01-07 08:51:07 -08002375
Li Zefan5c5cc622013-06-09 17:16:29 +08002376 /* if cpus or mems changed, we need to propagate to descendants */
2377 if (cpus_updated || mems_updated) {
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002378 struct cpuset *cs;
Tejun Heo492eb212013-08-08 20:11:25 -04002379 struct cgroup_subsys_state *pos_css;
Paul Jacksonb4501292008-02-07 00:14:47 -08002380
Paul Jacksonb4501292008-02-07 00:14:47 -08002381 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002382 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
Tejun Heoec903c02014-05-13 12:11:01 -04002383 if (cs == &top_cpuset || !css_tryget_online(&cs->css))
Li Zefan388afd82013-06-09 17:14:47 +08002384 continue;
2385 rcu_read_unlock();
2386
2387 cpuset_hotplug_update_tasks(cs);
2388
2389 rcu_read_lock();
2390 css_put(&cs->css);
2391 }
Tejun Heodeb7aa32013-01-07 08:51:07 -08002392 rcu_read_unlock();
2393 }
Tejun Heo8d033942013-01-07 08:51:07 -08002394
Tejun Heodeb7aa32013-01-07 08:51:07 -08002395 /* rebuild sched domains if cpus_allowed has changed */
Peter Zijlstra90fd6732017-09-07 11:13:38 +02002396 if (cpus_updated || force_rebuild) {
2397 force_rebuild = false;
Li Zhonge0e80a02013-04-27 06:52:43 -07002398 rebuild_sched_domains();
Peter Zijlstra90fd6732017-09-07 11:13:38 +02002399 }
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002400}
2401
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05302402void cpuset_update_active_cpus(bool cpu_online)
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002403{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002404 /*
2405 * We're inside cpu hotplug critical region which usually nests
2406 * inside cgroup synchronization. Bounce actual hotplug processing
2407 * to a work item to avoid reverse locking order.
2408 *
2409 * We still need to do partition_sched_domains() synchronously;
2410 * otherwise, the scheduler will get confused and put tasks to the
2411 * dead CPU. Fall back to the default single domain.
2412 * cpuset_hotplug_workfn() will rebuild it as necessary.
2413 */
2414 partition_sched_domains(1, NULL, NULL);
2415 schedule_work(&cpuset_hotplug_work);
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002416}
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002417
Peter Zijlstra90fd6732017-09-07 11:13:38 +02002418void cpuset_wait_for_hotplug(void)
2419{
2420 flush_work(&cpuset_hotplug_work);
2421}
2422
Paul Jackson38837fc2006-09-29 02:01:16 -07002423/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002424 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
2425 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05302426 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07002427 */
Miao Xief4818912008-11-19 15:36:30 -08002428static int cpuset_track_online_nodes(struct notifier_block *self,
2429 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07002430{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002431 schedule_work(&cpuset_hotplug_work);
Miao Xief4818912008-11-19 15:36:30 -08002432 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07002433}
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002434
2435static struct notifier_block cpuset_track_online_nodes_nb = {
2436 .notifier_call = cpuset_track_online_nodes,
2437 .priority = 10, /* ??! */
2438};
Paul Jackson38837fc2006-09-29 02:01:16 -07002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440/**
2441 * cpuset_init_smp - initialize cpus_allowed
2442 *
2443 * Description: Finish top cpuset after cpu, node maps are initialized
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445void __init cpuset_init_smp(void)
2446{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01002447 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002448 top_cpuset.mems_allowed = node_states[N_MEMORY];
Li Zefan33ad8012013-06-09 17:15:08 +08002449 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07002450
Li Zefane2b9a3d2014-07-09 16:47:03 +08002451 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask);
2452 top_cpuset.effective_mems = node_states[N_MEMORY];
2453
Andrew Mortond8f10cb2013-04-29 15:08:08 -07002454 register_hotmemory_notifier(&cpuset_track_online_nodes_nb);
Tejun Heofff4dc82016-01-19 12:18:41 -05002455
2456 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0);
2457 BUG_ON(!cpuset_migrate_mm_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
2460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
2462 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08002463 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 *
Li Zefan300ed6c2009-01-07 18:08:44 -08002465 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10302467 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * tasks cpuset.
2469 **/
2470
Li Zefan6af866a2009-01-07 18:08:45 -08002471void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002473 unsigned long flags;
2474
2475 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002476 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002477 guarantee_online_cpus(task_cs(tsk), pmask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002478 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002479 spin_unlock_irqrestore(&callback_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002482void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002483{
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002484 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002485 do_set_cpus_allowed(tsk, task_cs(tsk)->effective_cpus);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002486 rcu_read_unlock();
2487
2488 /*
2489 * We own tsk->cpus_allowed, nobody can change it under us.
2490 *
2491 * But we used cs && cs->cpus_allowed lockless and thus can
2492 * race with cgroup_attach_task() or update_cpumask() and get
2493 * the wrong tsk->cpus_allowed. However, both cases imply the
2494 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
2495 * which takes task_rq_lock().
2496 *
2497 * If we are called after it dropped the lock we must see all
2498 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
2499 * set any mask even if it is not right from task_cs() pov,
2500 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002501 *
2502 * select_fallback_rq() will fix things ups and set cpu_possible_mask
2503 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002504 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002505}
2506
Rasmus Villemoes8f4ab072015-02-12 15:00:16 -08002507void __init cpuset_init_current_mems_allowed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Mike Travisf9a86fc2008-04-04 18:11:07 -07002509 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002512/**
Paul Jackson909d75a2006-01-08 01:01:55 -08002513 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
2514 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
2515 *
2516 * Description: Returns the nodemask_t mems_allowed of the cpuset
2517 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08002518 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08002519 * tasks cpuset.
2520 **/
2521
2522nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
2523{
2524 nodemask_t mask;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002525 unsigned long flags;
Paul Jackson909d75a2006-01-08 01:01:55 -08002526
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002527 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002528 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08002529 guarantee_online_mems(task_cs(tsk), &mask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002530 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002531 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson909d75a2006-01-08 01:01:55 -08002532
2533 return mask;
2534}
2535
2536/**
Mel Gorman19770b32008-04-28 02:12:18 -07002537 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
2538 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07002539 *
Mel Gorman19770b32008-04-28 02:12:18 -07002540 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 */
Mel Gorman19770b32008-04-28 02:12:18 -07002542int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Mel Gorman19770b32008-04-28 02:12:18 -07002544 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Paul Jackson9bf22292005-09-06 15:18:12 -07002547/*
Paul Menage78608362008-04-29 01:00:26 -07002548 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
2549 * mem_hardwall ancestor to the specified cpuset. Call holding
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002550 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
Paul Menage78608362008-04-29 01:00:26 -07002551 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 */
Tejun Heoc9710d82013-08-08 20:11:22 -04002553static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Tejun Heoc4310692013-01-07 08:51:08 -08002555 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs))
2556 cs = parent_cs(cs);
Paul Jackson9bf22292005-09-06 15:18:12 -07002557 return cs;
2558}
2559
2560/**
Vladimir Davydov344736f2014-10-20 15:50:30 +04002561 * cpuset_node_allowed - Can we allocate on a memory node?
David Rientjesa1bc5a42009-04-02 16:57:54 -07002562 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08002563 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07002564 *
David Rientjes6e276d22015-04-14 15:47:01 -07002565 * If we're in interrupt, yes, we can always allocate. If @node is set in
2566 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
2567 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
2568 * yes. If current has access to memory reserves due to TIF_MEMDIE, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07002569 * Otherwise, no.
2570 *
2571 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07002572 * and do not allow allocations outside the current tasks cpuset
2573 * unless the task has been OOM killed as is marked TIF_MEMDIE.
Paul Jackson9bf22292005-09-06 15:18:12 -07002574 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07002575 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07002576 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002577 * Scanning up parent cpusets requires callback_lock. The
Paul Jackson02a0e532006-12-13 00:34:25 -08002578 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
2579 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
2580 * current tasks mems_allowed came up empty on the first pass over
2581 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002582 * cpuset are short of memory, might require taking the callback_lock.
Paul Jackson9bf22292005-09-06 15:18:12 -07002583 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002584 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08002585 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
2586 * so no allocation on a node outside the cpuset is allowed (unless
2587 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07002588 *
Paul Jackson36be57f2006-05-20 15:00:10 -07002589 * The second pass through get_page_from_freelist() doesn't even call
2590 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
2591 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
2592 * in alloc_flags. That logic and the checks below have the combined
2593 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07002594 * in_interrupt - any node ok (current task context irrelevant)
2595 * GFP_ATOMIC - any node ok
David Rientjesc596d9f2007-05-06 14:49:32 -07002596 * TIF_MEMDIE - any node ok
Paul Menage78608362008-04-29 01:00:26 -07002597 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07002598 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson02a0e532006-12-13 00:34:25 -08002599 */
Vladimir Davydov344736f2014-10-20 15:50:30 +04002600int __cpuset_node_allowed(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07002601{
Tejun Heoc9710d82013-08-08 20:11:22 -04002602 struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08002603 int allowed; /* is allocation in zone z allowed? */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002604 unsigned long flags;
Paul Jackson9bf22292005-09-06 15:18:12 -07002605
David Rientjes6e276d22015-04-14 15:47:01 -07002606 if (in_interrupt())
Paul Jackson9bf22292005-09-06 15:18:12 -07002607 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002608 if (node_isset(node, current->mems_allowed))
2609 return 1;
David Rientjesc596d9f2007-05-06 14:49:32 -07002610 /*
2611 * Allow tasks that have access to memory reserves because they have
2612 * been OOM killed to get memory anywhere.
2613 */
2614 if (unlikely(test_thread_flag(TIF_MEMDIE)))
2615 return 1;
Paul Jackson9bf22292005-09-06 15:18:12 -07002616 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
2617 return 0;
2618
Bob Picco5563e772005-11-13 16:06:35 -08002619 if (current->flags & PF_EXITING) /* Let dying task have memory */
2620 return 1;
2621
Paul Jackson9bf22292005-09-06 15:18:12 -07002622 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002623 spin_lock_irqsave(&callback_lock, flags);
Paul Jackson053199e2005-10-30 15:02:30 -08002624
Li Zefanb8dadcb2014-03-03 17:28:36 -05002625 rcu_read_lock();
Paul Menage78608362008-04-29 01:00:26 -07002626 cs = nearest_hardwall_ancestor(task_cs(current));
Li Zefan99afb0f2014-02-27 18:19:36 +08002627 allowed = node_isset(node, cs->mems_allowed);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002628 rcu_read_unlock();
Paul Jackson053199e2005-10-30 15:02:30 -08002629
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002630 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson9bf22292005-09-06 15:18:12 -07002631 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002634/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07002635 * cpuset_mem_spread_node() - On which node to begin search for a file page
2636 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08002637 *
2638 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
2639 * tasks in a cpuset with is_spread_page or is_spread_slab set),
2640 * and if the memory allocation used cpuset_mem_spread_node()
2641 * to determine on which node to start looking, as it will for
2642 * certain page cache or slab cache pages such as used for file
2643 * system buffers and inode caches, then instead of starting on the
2644 * local node to look for a free page, rather spread the starting
2645 * node around the tasks mems_allowed nodes.
2646 *
2647 * We don't have to worry about the returned node being offline
2648 * because "it can't happen", and even if it did, it would be ok.
2649 *
2650 * The routines calling guarantee_online_mems() are careful to
2651 * only set nodes in task->mems_allowed that are online. So it
2652 * should not be possible for the following code to return an
2653 * offline node. But if it did, that would be ok, as this routine
2654 * is not returning the node where the allocation must be, only
2655 * the node where the search should start. The zonelist passed to
2656 * __alloc_pages() will include all nodes. If the slab allocator
2657 * is passed an offline node, it will fall back to the local node.
2658 * See kmem_cache_alloc_node().
2659 */
2660
Jack Steiner6adef3e2010-05-26 14:42:49 -07002661static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08002662{
2663 int node;
2664
Jack Steiner6adef3e2010-05-26 14:42:49 -07002665 node = next_node(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08002666 if (node == MAX_NUMNODES)
2667 node = first_node(current->mems_allowed);
Jack Steiner6adef3e2010-05-26 14:42:49 -07002668 *rotor = node;
Paul Jackson825a46a2006-03-24 03:16:03 -08002669 return node;
2670}
Jack Steiner6adef3e2010-05-26 14:42:49 -07002671
2672int cpuset_mem_spread_node(void)
2673{
Michal Hocko778d3b02011-07-26 16:08:30 -07002674 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
2675 current->cpuset_mem_spread_rotor =
2676 node_random(&current->mems_allowed);
2677
Jack Steiner6adef3e2010-05-26 14:42:49 -07002678 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
2679}
2680
2681int cpuset_slab_spread_node(void)
2682{
Michal Hocko778d3b02011-07-26 16:08:30 -07002683 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
2684 current->cpuset_slab_spread_rotor =
2685 node_random(&current->mems_allowed);
2686
Jack Steiner6adef3e2010-05-26 14:42:49 -07002687 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
2688}
2689
Paul Jackson825a46a2006-03-24 03:16:03 -08002690EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
2691
2692/**
David Rientjesbbe373f2007-10-16 23:25:58 -07002693 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
2694 * @tsk1: pointer to task_struct of some task.
2695 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002696 *
David Rientjesbbe373f2007-10-16 23:25:58 -07002697 * Description: Return true if @tsk1's mems_allowed intersects the
2698 * mems_allowed of @tsk2. Used by the OOM killer to determine if
2699 * one of the task's memory usage might impact the memory available
2700 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002701 **/
2702
David Rientjesbbe373f2007-10-16 23:25:58 -07002703int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
2704 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002705{
David Rientjesbbe373f2007-10-16 23:25:58 -07002706 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07002707}
2708
David Rientjes75aa1992009-01-06 14:39:01 -08002709/**
David Rientjesda39da32015-11-05 18:48:05 -08002710 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
David Rientjes75aa1992009-01-06 14:39:01 -08002711 *
David Rientjesda39da32015-11-05 18:48:05 -08002712 * Description: Prints current's name, cpuset name, and cached copy of its
Li Zefanb8dadcb2014-03-03 17:28:36 -05002713 * mems_allowed to the kernel log.
David Rientjes75aa1992009-01-06 14:39:01 -08002714 */
David Rientjesda39da32015-11-05 18:48:05 -08002715void cpuset_print_current_mems_allowed(void)
David Rientjes75aa1992009-01-06 14:39:01 -08002716{
Li Zefanb8dadcb2014-03-03 17:28:36 -05002717 struct cgroup *cgrp;
David Rientjes75aa1992009-01-06 14:39:01 -08002718
Li Zefanb8dadcb2014-03-03 17:28:36 -05002719 rcu_read_lock();
Li Zefan63f43f52013-01-25 16:08:01 +08002720
David Rientjesda39da32015-11-05 18:48:05 -08002721 cgrp = task_cs(current)->css.cgroup;
2722 pr_info("%s cpuset=", current->comm);
Tejun Heoe61734c2014-02-12 09:29:50 -05002723 pr_cont_cgroup_name(cgrp);
David Rientjesda39da32015-11-05 18:48:05 -08002724 pr_cont(" mems_allowed=%*pbl\n",
2725 nodemask_pr_args(&current->mems_allowed));
Li Zefanf440d982013-03-01 15:02:15 +08002726
Li Zefancfb59662013-03-12 10:28:39 +08002727 rcu_read_unlock();
David Rientjes75aa1992009-01-06 14:39:01 -08002728}
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002731 * Collection of memory_pressure is suppressed unless
2732 * this flag is enabled by writing "1" to the special
2733 * cpuset file 'memory_pressure_enabled' in the root cpuset.
2734 */
2735
Paul Jacksonc5b2aff2006-01-08 01:01:51 -08002736int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002737
2738/**
2739 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
2740 *
2741 * Keep a running average of the rate of synchronous (direct)
2742 * page reclaim efforts initiated by tasks in each cpuset.
2743 *
2744 * This represents the rate at which some task in the cpuset
2745 * ran low on memory on all nodes it was allowed to use, and
2746 * had to enter the kernels page reclaim code in an effort to
2747 * create more free memory by tossing clean pages or swapping
2748 * or writing dirty pages.
2749 *
2750 * Display to user space in the per-cpuset read-only file
2751 * "memory_pressure". Value displayed is an integer
2752 * representing the recent rate of entry into the synchronous
2753 * (direct) page reclaim by any task attached to the cpuset.
2754 **/
2755
2756void __cpuset_memory_pressure_bump(void)
2757{
Li Zefanb8dadcb2014-03-03 17:28:36 -05002758 rcu_read_lock();
Paul Menage8793d852007-10-18 23:39:39 -07002759 fmeter_markevent(&task_cs(current)->fmeter);
Li Zefanb8dadcb2014-03-03 17:28:36 -05002760 rcu_read_unlock();
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002761}
2762
Paul Menage8793d852007-10-18 23:39:39 -07002763#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002764/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * proc_cpuset_show()
2766 * - Print tasks cpuset path into seq_file.
2767 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08002768 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
2769 * doesn't really matter if tsk->cpuset changes after we read it,
Tejun Heo5d21cc22013-01-07 08:51:08 -08002770 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08002771 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 */
Zefan Li52de4772014-09-18 16:03:36 +08002773int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
2774 struct pid *pid, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Tejun Heoe61734c2014-02-12 09:29:50 -05002776 char *buf, *p;
Paul Menage8793d852007-10-18 23:39:39 -07002777 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002778 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Eric W. Biederman99f89552006-06-26 00:25:55 -07002780 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05002781 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07002783 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Tejun Heoe61734c2014-02-12 09:29:50 -05002785 retval = -ENAMETOOLONG;
Li Zefan27e89ae2013-01-15 14:10:57 +08002786 rcu_read_lock();
Tejun Heo073219e2014-02-08 10:36:58 -05002787 css = task_css(tsk, cpuset_cgrp_id);
Tejun Heoe61734c2014-02-12 09:29:50 -05002788 p = cgroup_path(css->cgroup, buf, PATH_MAX);
Li Zefan27e89ae2013-01-15 14:10:57 +08002789 rcu_read_unlock();
Tejun Heoe61734c2014-02-12 09:29:50 -05002790 if (!p)
Zefan Li52de4772014-09-18 16:03:36 +08002791 goto out_free;
Tejun Heoe61734c2014-02-12 09:29:50 -05002792 seq_puts(m, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 seq_putc(m, '\n');
Tejun Heoe61734c2014-02-12 09:29:50 -05002794 retval = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002795out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002797out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return retval;
2799}
Paul Menage8793d852007-10-18 23:39:39 -07002800#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Heiko Carstensd01d4822009-09-21 11:06:27 +02002802/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002803void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Tejun Heoe8e6d972015-02-13 14:37:23 -08002805 seq_printf(m, "Mems_allowed:\t%*pb\n",
2806 nodemask_pr_args(&task->mems_allowed));
2807 seq_printf(m, "Mems_allowed_list:\t%*pbl\n",
2808 nodemask_pr_args(&task->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}