blob: 44d234b0df5e36d795f80d3e8af30d607c577cdb [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>
David Howellsa1875372018-11-01 23:07:25 +000042#include <linux/fs_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/namei.h>
44#include <linux/pagemap.h>
45#include <linux/proc_fs.h>
Paul Jackson6b9c2602006-01-08 01:02:02 -080046#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/sched.h>
Mathieu Poirierf9a25f72019-07-19 15:59:55 +020048#include <linux/sched/deadline.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010049#include <linux/sched/mm.h>
Ingo Molnarf719ff9b2017-02-06 10:57:33 +010050#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/seq_file.h>
David Quigley22fb52d2006-06-23 02:04:00 -070052#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/spinlock.h>
55#include <linux/stat.h>
56#include <linux/string.h>
57#include <linux/time.h>
Arnd Bergmannd2b436582015-11-25 16:16:55 +010058#include <linux/time64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/backing-dev.h>
60#include <linux/sort.h>
Michal Hockoda99ecf2017-09-06 16:24:53 -070061#include <linux/oom.h>
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020062#include <linux/sched/isolation.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080063#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Ingo Molnar3d3f26a2006-03-23 03:00:18 -080065#include <linux/mutex.h>
Cliff Wickman956db3c2008-02-07 00:14:43 -080066#include <linux/cgroup.h>
Li Zefane44193d2013-06-09 17:14:22 +080067#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dima Zavin89affbf2017-08-02 13:32:18 -070069DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key);
Vlastimil Babka002f2902016-05-19 17:14:30 -070070DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key);
Paul Jackson202f72d2006-01-08 01:01:57 -080071
Paul Jackson3e0d98b2006-01-08 01:01:49 -080072/* See "Frequency meter" comments, below. */
73
74struct fmeter {
75 int cnt; /* unprocessed events count */
76 int val; /* most recent output value */
Arnd Bergmannd2b436582015-11-25 16:16:55 +010077 time64_t time; /* clock (secs) when val computed */
Paul Jackson3e0d98b2006-01-08 01:01:49 -080078 spinlock_t lock; /* guards read or write of above */
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct cpuset {
Paul Menage8793d852007-10-18 23:39:39 -070082 struct cgroup_subsys_state css;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned long flags; /* "unsigned long" so bitops work */
Li Zefane2b9a3d2014-07-09 16:47:03 +080085
Li Zefan7e882912014-07-09 16:48:42 +080086 /*
87 * On default hierarchy:
88 *
89 * The user-configured masks can only be changed by writing to
90 * cpuset.cpus and cpuset.mems, and won't be limited by the
91 * parent masks.
92 *
93 * The effective masks is the real masks that apply to the tasks
94 * in the cpuset. They may be changed if the configured masks are
95 * changed or hotplug happens.
96 *
97 * effective_mask == configured_mask & parent's effective_mask,
98 * and if it ends up empty, it will inherit the parent's mask.
99 *
100 *
Aubrey Li415de5f2021-01-13 12:37:41 +0800101 * On legacy hierarchy:
Li Zefan7e882912014-07-09 16:48:42 +0800102 *
103 * The user-configured masks are always the same with effective masks.
104 */
105
Li Zefane2b9a3d2014-07-09 16:47:03 +0800106 /* user-configured CPUs and Memory Nodes allow to tasks */
107 cpumask_var_t cpus_allowed;
108 nodemask_t mems_allowed;
109
110 /* effective CPUs and Memory Nodes allow to tasks */
111 cpumask_var_t effective_cpus;
112 nodemask_t effective_mems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Li Zefan33ad8012013-06-09 17:15:08 +0800114 /*
Waiman Long58b74842018-11-08 10:08:36 -0500115 * CPUs allocated to child sub-partitions (default hierarchy only)
116 * - CPUs granted by the parent = effective_cpus U subparts_cpus
117 * - effective_cpus and subparts_cpus are mutually exclusive.
Waiman Long4b842da2018-11-08 10:08:41 -0500118 *
119 * effective_cpus contains only onlined CPUs, but subparts_cpus
120 * may have offlined ones.
Waiman Long58b74842018-11-08 10:08:36 -0500121 */
122 cpumask_var_t subparts_cpus;
123
124 /*
Li Zefan33ad8012013-06-09 17:15:08 +0800125 * This is old Memory Nodes tasks took on.
126 *
127 * - top_cpuset.old_mems_allowed is initialized to mems_allowed.
128 * - A new cpuset's old_mems_allowed is initialized when some
129 * task is moved into it.
130 * - old_mems_allowed is used in cpuset_migrate_mm() when we change
131 * cpuset.mems_allowed and have tasks' nodemask updated, and
132 * then old_mems_allowed is updated to mems_allowed.
133 */
134 nodemask_t old_mems_allowed;
135
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800136 struct fmeter fmeter; /* memory_pressure filter */
Paul Jackson029190c2007-10-18 23:40:20 -0700137
Tejun Heo452477f2013-01-07 08:51:07 -0800138 /*
139 * Tasks are being attached to this cpuset. Used to prevent
140 * zeroing cpus/mems_allowed between ->can_attach() and ->attach().
141 */
142 int attach_in_progress;
143
Paul Jackson029190c2007-10-18 23:40:20 -0700144 /* partition number for rebuild_sched_domains() */
145 int pn;
Cliff Wickman956db3c2008-02-07 00:14:43 -0800146
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900147 /* for custom sched domain */
148 int relax_domain_level;
Waiman Long58b74842018-11-08 10:08:36 -0500149
150 /* number of CPUs in subparts_cpus */
151 int nr_subparts_cpus;
152
153 /* partition root state */
154 int partition_root_state;
Waiman Long47169092018-11-08 10:08:40 -0500155
156 /*
157 * Default hierarchy only:
158 * use_parent_ecpus - set if using parent's effective_cpus
159 * child_ecpus_count - # of children with use_parent_ecpus set
160 */
161 int use_parent_ecpus;
162 int child_ecpus_count;
Waiman Longe7cc9882021-08-10 23:06:02 -0400163
164 /* Handle for cpuset.cpus.partition */
165 struct cgroup_file partition_file;
Waiman Long58b74842018-11-08 10:08:36 -0500166};
167
168/*
169 * Partition root states:
170 *
171 * 0 - not a partition root
Waiman Long3881b862018-11-08 10:08:39 -0500172 *
Waiman Long58b74842018-11-08 10:08:36 -0500173 * 1 - partition root
Waiman Long3881b862018-11-08 10:08:39 -0500174 *
175 * -1 - invalid partition root
176 * None of the cpus in cpus_allowed can be put into the parent's
177 * subparts_cpus. In this case, the cpuset is not a real partition
178 * root anymore. However, the CPU_EXCLUSIVE bit will still be set
179 * and the cpuset can be restored back to a partition root if the
180 * parent cpuset can give more CPUs back to this child cpuset.
Waiman Long58b74842018-11-08 10:08:36 -0500181 */
182#define PRS_DISABLED 0
183#define PRS_ENABLED 1
Waiman Long3881b862018-11-08 10:08:39 -0500184#define PRS_ERROR -1
Waiman Long58b74842018-11-08 10:08:36 -0500185
186/*
187 * Temporary cpumasks for working with partitions that are passed among
188 * functions to avoid memory allocation in inner functions.
189 */
190struct tmpmasks {
191 cpumask_var_t addmask, delmask; /* For partition root */
192 cpumask_var_t new_cpus; /* For update_cpumasks_hier() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Tejun Heoa7c6d552013-08-08 20:11:23 -0400195static inline struct cpuset *css_cs(struct cgroup_subsys_state *css)
Paul Menage8793d852007-10-18 23:39:39 -0700196{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400197 return css ? container_of(css, struct cpuset, css) : NULL;
Paul Menage8793d852007-10-18 23:39:39 -0700198}
199
200/* Retrieve the cpuset for a task */
201static inline struct cpuset *task_cs(struct task_struct *task)
202{
Tejun Heo073219e2014-02-08 10:36:58 -0500203 return css_cs(task_css(task, cpuset_cgrp_id));
Paul Menage8793d852007-10-18 23:39:39 -0700204}
Paul Menage8793d852007-10-18 23:39:39 -0700205
Tejun Heoc9710d82013-08-08 20:11:22 -0400206static inline struct cpuset *parent_cs(struct cpuset *cs)
Tejun Heoc4310692013-01-07 08:51:08 -0800207{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400208 return css_cs(cs->css.parent);
Tejun Heoc4310692013-01-07 08:51:08 -0800209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* bits in struct cpuset flags field */
212typedef enum {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800213 CS_ONLINE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 CS_CPU_EXCLUSIVE,
215 CS_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -0700216 CS_MEM_HARDWALL,
Paul Jackson45b07ef2006-01-08 01:00:56 -0800217 CS_MEMORY_MIGRATE,
Paul Jackson029190c2007-10-18 23:40:20 -0700218 CS_SCHED_LOAD_BALANCE,
Paul Jackson825a46a2006-03-24 03:16:03 -0800219 CS_SPREAD_PAGE,
220 CS_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221} cpuset_flagbits_t;
222
223/* convenient tests for these bits */
Tejun Heo41c25702017-05-24 12:03:48 -0400224static inline bool is_cpuset_online(struct cpuset *cs)
Tejun Heoefeb77b2013-01-07 08:51:07 -0800225{
Tejun Heo41c25702017-05-24 12:03:48 -0400226 return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css);
Tejun Heoefeb77b2013-01-07 08:51:07 -0800227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static inline int is_cpu_exclusive(const struct cpuset *cs)
230{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800231 return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static inline int is_mem_exclusive(const struct cpuset *cs)
235{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800236 return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Paul Menage78608362008-04-29 01:00:26 -0700239static inline int is_mem_hardwall(const struct cpuset *cs)
240{
241 return test_bit(CS_MEM_HARDWALL, &cs->flags);
242}
243
Paul Jackson029190c2007-10-18 23:40:20 -0700244static inline int is_sched_load_balance(const struct cpuset *cs)
245{
246 return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
247}
248
Paul Jackson45b07ef2006-01-08 01:00:56 -0800249static inline int is_memory_migrate(const struct cpuset *cs)
250{
Paul Jackson7b5b9ef2006-03-24 03:16:00 -0800251 return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
Paul Jackson45b07ef2006-01-08 01:00:56 -0800252}
253
Paul Jackson825a46a2006-03-24 03:16:03 -0800254static inline int is_spread_page(const struct cpuset *cs)
255{
256 return test_bit(CS_SPREAD_PAGE, &cs->flags);
257}
258
259static inline int is_spread_slab(const struct cpuset *cs)
260{
261 return test_bit(CS_SPREAD_SLAB, &cs->flags);
262}
263
Waiman Long58b74842018-11-08 10:08:36 -0500264static inline int is_partition_root(const struct cpuset *cs)
265{
Waiman Long3881b862018-11-08 10:08:39 -0500266 return cs->partition_root_state > 0;
Waiman Long58b74842018-11-08 10:08:36 -0500267}
268
Waiman Longe7cc9882021-08-10 23:06:02 -0400269/*
270 * Send notification event of whenever partition_root_state changes.
271 */
272static inline void notify_partition_change(struct cpuset *cs,
273 int old_prs, int new_prs)
274{
275 if (old_prs != new_prs)
276 cgroup_file_notify(&cs->partition_file);
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static struct cpuset top_cpuset = {
Tejun Heoefeb77b2013-01-07 08:51:07 -0800280 .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) |
281 (1 << CS_MEM_EXCLUSIVE)),
Waiman Long58b74842018-11-08 10:08:36 -0500282 .partition_root_state = PRS_ENABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
284
Tejun Heoae8086c2013-01-07 08:51:07 -0800285/**
286 * cpuset_for_each_child - traverse online children of a cpuset
287 * @child_cs: loop cursor pointing to the current child
Tejun Heo492eb212013-08-08 20:11:25 -0400288 * @pos_css: used for iteration
Tejun Heoae8086c2013-01-07 08:51:07 -0800289 * @parent_cs: target cpuset to walk children of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Tejun Heoae8086c2013-01-07 08:51:07 -0800291 * Walk @child_cs through the online children of @parent_cs. Must be used
292 * with RCU read locked.
293 */
Tejun Heo492eb212013-08-08 20:11:25 -0400294#define cpuset_for_each_child(child_cs, pos_css, parent_cs) \
295 css_for_each_child((pos_css), &(parent_cs)->css) \
296 if (is_cpuset_online(((child_cs) = css_cs((pos_css)))))
Tejun Heoae8086c2013-01-07 08:51:07 -0800297
Tejun Heofc560a22013-01-07 08:51:08 -0800298/**
299 * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants
300 * @des_cs: loop cursor pointing to the current descendant
Tejun Heo492eb212013-08-08 20:11:25 -0400301 * @pos_css: used for iteration
Tejun Heofc560a22013-01-07 08:51:08 -0800302 * @root_cs: target cpuset to walk ancestor of
303 *
304 * Walk @des_cs through the online descendants of @root_cs. Must be used
Tejun Heo492eb212013-08-08 20:11:25 -0400305 * with RCU read locked. The caller may modify @pos_css by calling
Tejun Heobd8815a2013-08-08 20:11:27 -0400306 * css_rightmost_descendant() to skip subtree. @root_cs is included in the
307 * iteration and the first node to be visited.
Tejun Heofc560a22013-01-07 08:51:08 -0800308 */
Tejun Heo492eb212013-08-08 20:11:25 -0400309#define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \
310 css_for_each_descendant_pre((pos_css), &(root_cs)->css) \
311 if (is_cpuset_online(((des_cs) = css_cs((pos_css)))))
Tejun Heofc560a22013-01-07 08:51:08 -0800312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400314 * There are two global locks guarding cpuset structures - cpuset_mutex and
315 * callback_lock. We also require taking task_lock() when dereferencing a
316 * task's cpuset pointer. See "The task_lock() exception", at the end of this
317 * comment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400319 * A task must hold both locks to modify cpusets. If a task holds
Tejun Heo5d21cc22013-01-07 08:51:08 -0800320 * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400321 * is the only task able to also acquire callback_lock and be able to
Tejun Heo5d21cc22013-01-07 08:51:08 -0800322 * modify cpusets. It can perform various checks on the cpuset structure
323 * first, knowing nothing will change. It can also allocate memory while
324 * just holding cpuset_mutex. While it is performing these checks, various
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400325 * callback routines can briefly acquire callback_lock to query cpusets.
326 * Once it is ready to make the changes, it takes callback_lock, blocking
Tejun Heo5d21cc22013-01-07 08:51:08 -0800327 * everyone else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *
Paul Jackson053199e2005-10-30 15:02:30 -0800329 * Calls to the kernel memory allocator can not be made while holding
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400330 * callback_lock, as that would risk double tripping on callback_lock
Paul Jackson053199e2005-10-30 15:02:30 -0800331 * from one of the callbacks into the cpuset code from within
332 * __alloc_pages().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400334 * If a task is only holding callback_lock, then it has read-only
Paul Jackson053199e2005-10-30 15:02:30 -0800335 * access to cpusets.
336 *
Miao Xie58568d22009-06-16 15:31:49 -0700337 * Now, the task_struct fields mems_allowed and mempolicy may be changed
338 * by other task, we use alloc_lock in the task_struct fields to protect
339 * them.
Paul Jackson053199e2005-10-30 15:02:30 -0800340 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400341 * The cpuset_common_file_read() handlers only hold callback_lock across
Paul Jackson053199e2005-10-30 15:02:30 -0800342 * small pieces of code, such as when reading out possibly multi-word
343 * cpumasks and nodemasks.
344 *
Paul Menage2df167a2008-02-07 00:14:45 -0800345 * Accessing a task's cpuset should be done in accordance with the
346 * guidelines for accessing subsystem state in kernel/cgroup.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348
Juri Lelli1243dc52019-07-19 15:59:57 +0200349DEFINE_STATIC_PERCPU_RWSEM(cpuset_rwsem);
Juri Lelli710da3c2019-07-19 16:00:00 +0200350
351void cpuset_read_lock(void)
352{
353 percpu_down_read(&cpuset_rwsem);
354}
355
356void cpuset_read_unlock(void)
357{
358 percpu_up_read(&cpuset_rwsem);
359}
360
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400361static DEFINE_SPINLOCK(callback_lock);
Paul Jackson4247bdc2005-09-10 00:26:06 -0700362
Tejun Heoe93ad192016-01-19 12:18:41 -0500363static struct workqueue_struct *cpuset_migrate_mm_wq;
364
Max Krasnyanskycf417142008-08-11 14:33:53 -0700365/*
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800366 * CPU / memory hotplug is handled asynchronously.
367 */
368static void cpuset_hotplug_workfn(struct work_struct *work);
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800369static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn);
370
Li Zefane44193d2013-06-09 17:14:22 +0800371static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq);
372
Tejun Heo3a5a6d02013-01-07 08:51:07 -0800373/*
Waiman Long0c05b9b2020-03-30 10:06:15 -0400374 * Cgroup v2 behavior is used on the "cpus" and "mems" control files when
375 * on default hierarchy or when the cpuset_v2_mode flag is set by mounting
376 * the v1 cpuset cgroup filesystem with the "cpuset_v2_mode" mount option.
377 * With v2 behavior, "cpus" and "mems" are always what the users have
378 * requested and won't be changed by hotplug events. Only the effective
379 * cpus or mems will be affected.
Waiman Longb8d1b8e2017-08-17 15:33:10 -0400380 */
381static inline bool is_in_v2_mode(void)
382{
383 return cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
384 (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
385}
386
387/*
Li Zefan300ed6c2009-01-07 18:08:44 -0800388 * Return in pmask the portion of a cpusets's cpus_allowed that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * are online. If none are online, walk up the cpuset hierarchy
Joonwoo Park28b89b92016-09-11 21:14:58 -0700390 * until we find one that does have some online cpus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
392 * One way or another, we guarantee to return some non-empty subset
Rusty Russell5f054e32012-03-29 15:38:31 +1030393 * of cpu_online_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400395 * Call with callback_lock or cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400397static void guarantee_online_cpus(struct cpuset *cs, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Joonwoo Park28b89b92016-09-11 21:14:58 -0700399 while (!cpumask_intersects(cs->effective_cpus, cpu_online_mask)) {
Tejun Heoc4310692013-01-07 08:51:08 -0800400 cs = parent_cs(cs);
Joonwoo Park28b89b92016-09-11 21:14:58 -0700401 if (unlikely(!cs)) {
402 /*
403 * The top cpuset doesn't have any online cpu as a
404 * consequence of a race between cpuset_hotplug_work
405 * and cpu hotplug notifier. But we know the top
Randy Dunlap7b7b8a22020-10-15 20:10:28 -0700406 * cpuset's effective_cpus is on its way to be
Joonwoo Park28b89b92016-09-11 21:14:58 -0700407 * identical to cpu_online_mask.
408 */
409 cpumask_copy(pmask, cpu_online_mask);
410 return;
411 }
412 }
Li Zefanae1c8022014-07-09 16:48:32 +0800413 cpumask_and(pmask, cs->effective_cpus, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416/*
417 * Return in *pmask the portion of a cpusets's mems_allowed that
Christoph Lameter0e1e7c72007-10-16 01:25:38 -0700418 * are online, with memory. If none are online with memory, walk
419 * up the cpuset hierarchy until we find one that does have some
Li Zefan40df2de2013-06-05 17:15:23 +0800420 * online mems. The top cpuset always has some mems online.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *
422 * One way or another, we guarantee to return some non-empty subset
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800423 * of node_states[N_MEMORY].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400425 * Call with callback_lock or cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400427static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Li Zefanae1c8022014-07-09 16:48:32 +0800429 while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
Tejun Heoc4310692013-01-07 08:51:08 -0800430 cs = parent_cs(cs);
Li Zefanae1c8022014-07-09 16:48:32 +0800431 nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Miao Xief3b39d42009-06-16 15:31:46 -0700434/*
435 * update task's spread flag if cpuset's page/slab spread flag is set
436 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +0400437 * Call with callback_lock or cpuset_mutex held.
Miao Xief3b39d42009-06-16 15:31:46 -0700438 */
439static void cpuset_update_task_spread_flag(struct cpuset *cs,
440 struct task_struct *tsk)
441{
442 if (is_spread_page(cs))
Zefan Li2ad654b2014-09-25 09:41:02 +0800443 task_set_spread_page(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700444 else
Zefan Li2ad654b2014-09-25 09:41:02 +0800445 task_clear_spread_page(tsk);
446
Miao Xief3b39d42009-06-16 15:31:46 -0700447 if (is_spread_slab(cs))
Zefan Li2ad654b2014-09-25 09:41:02 +0800448 task_set_spread_slab(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700449 else
Zefan Li2ad654b2014-09-25 09:41:02 +0800450 task_clear_spread_slab(tsk);
Miao Xief3b39d42009-06-16 15:31:46 -0700451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
454 * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
455 *
456 * One cpuset is a subset of another if all its allowed CPUs and
457 * Memory Nodes are a subset of the other, and its exclusive flags
Tejun Heo5d21cc22013-01-07 08:51:08 -0800458 * are only set if the other's are set. Call holding cpuset_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
460
461static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
462{
Li Zefan300ed6c2009-01-07 18:08:44 -0800463 return cpumask_subset(p->cpus_allowed, q->cpus_allowed) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 nodes_subset(p->mems_allowed, q->mems_allowed) &&
465 is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
466 is_mem_exclusive(p) <= is_mem_exclusive(q);
467}
468
Li Zefan645fcc92009-01-07 18:08:43 -0800469/**
Waiman Longbf923702018-11-08 10:08:37 -0500470 * alloc_cpumasks - allocate three cpumasks for cpuset
471 * @cs: the cpuset that have cpumasks to be allocated.
472 * @tmp: the tmpmasks structure pointer
473 * Return: 0 if successful, -ENOMEM otherwise.
474 *
475 * Only one of the two input arguments should be non-NULL.
476 */
477static inline int alloc_cpumasks(struct cpuset *cs, struct tmpmasks *tmp)
478{
479 cpumask_var_t *pmask1, *pmask2, *pmask3;
480
481 if (cs) {
482 pmask1 = &cs->cpus_allowed;
483 pmask2 = &cs->effective_cpus;
484 pmask3 = &cs->subparts_cpus;
485 } else {
486 pmask1 = &tmp->new_cpus;
487 pmask2 = &tmp->addmask;
488 pmask3 = &tmp->delmask;
489 }
490
491 if (!zalloc_cpumask_var(pmask1, GFP_KERNEL))
492 return -ENOMEM;
493
494 if (!zalloc_cpumask_var(pmask2, GFP_KERNEL))
495 goto free_one;
496
497 if (!zalloc_cpumask_var(pmask3, GFP_KERNEL))
498 goto free_two;
499
500 return 0;
501
502free_two:
503 free_cpumask_var(*pmask2);
504free_one:
505 free_cpumask_var(*pmask1);
506 return -ENOMEM;
507}
508
509/**
510 * free_cpumasks - free cpumasks in a tmpmasks structure
511 * @cs: the cpuset that have cpumasks to be free.
512 * @tmp: the tmpmasks structure pointer
513 */
514static inline void free_cpumasks(struct cpuset *cs, struct tmpmasks *tmp)
515{
516 if (cs) {
517 free_cpumask_var(cs->cpus_allowed);
518 free_cpumask_var(cs->effective_cpus);
519 free_cpumask_var(cs->subparts_cpus);
520 }
521 if (tmp) {
522 free_cpumask_var(tmp->new_cpus);
523 free_cpumask_var(tmp->addmask);
524 free_cpumask_var(tmp->delmask);
525 }
526}
527
528/**
Li Zefan645fcc92009-01-07 18:08:43 -0800529 * alloc_trial_cpuset - allocate a trial cpuset
530 * @cs: the cpuset that the trial cpuset duplicates
531 */
Tejun Heoc9710d82013-08-08 20:11:22 -0400532static struct cpuset *alloc_trial_cpuset(struct cpuset *cs)
Li Zefan645fcc92009-01-07 18:08:43 -0800533{
Li Zefan300ed6c2009-01-07 18:08:44 -0800534 struct cpuset *trial;
535
536 trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
537 if (!trial)
538 return NULL;
539
Waiman Longbf923702018-11-08 10:08:37 -0500540 if (alloc_cpumasks(trial, NULL)) {
541 kfree(trial);
542 return NULL;
543 }
Li Zefan300ed6c2009-01-07 18:08:44 -0800544
Li Zefane2b9a3d2014-07-09 16:47:03 +0800545 cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
546 cpumask_copy(trial->effective_cpus, cs->effective_cpus);
Li Zefan300ed6c2009-01-07 18:08:44 -0800547 return trial;
Li Zefan645fcc92009-01-07 18:08:43 -0800548}
549
550/**
Waiman Longbf923702018-11-08 10:08:37 -0500551 * free_cpuset - free the cpuset
552 * @cs: the cpuset to be freed
Li Zefan645fcc92009-01-07 18:08:43 -0800553 */
Waiman Longbf923702018-11-08 10:08:37 -0500554static inline void free_cpuset(struct cpuset *cs)
Li Zefan645fcc92009-01-07 18:08:43 -0800555{
Waiman Longbf923702018-11-08 10:08:37 -0500556 free_cpumasks(cs, NULL);
557 kfree(cs);
Li Zefan645fcc92009-01-07 18:08:43 -0800558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/*
561 * validate_change() - Used to validate that any proposed cpuset change
562 * follows the structural rules for cpusets.
563 *
564 * If we replaced the flag and mask values of the current cpuset
565 * (cur) with those values in the trial cpuset (trial), would
566 * our various subset and exclusive rules still be valid? Presumes
Tejun Heo5d21cc22013-01-07 08:51:08 -0800567 * cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 *
569 * 'cur' is the address of an actual, in-use cpuset. Operations
570 * such as list traversal that depend on the actual address of the
571 * cpuset in the list must use cur below, not trial.
572 *
573 * 'trial' is the address of bulk structure copy of cur, with
574 * perhaps one or more of the fields cpus_allowed, mems_allowed,
575 * or flags changed to new, trial values.
576 *
577 * Return 0 if valid, -errno if not.
578 */
579
Tejun Heoc9710d82013-08-08 20:11:22 -0400580static int validate_change(struct cpuset *cur, struct cpuset *trial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Tejun Heo492eb212013-08-08 20:11:25 -0400582 struct cgroup_subsys_state *css;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct cpuset *c, *par;
Tejun Heoae8086c2013-01-07 08:51:07 -0800584 int ret;
585
586 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Each of our child cpusets must be a subset of us */
Tejun Heoae8086c2013-01-07 08:51:07 -0800589 ret = -EBUSY;
Tejun Heo492eb212013-08-08 20:11:25 -0400590 cpuset_for_each_child(c, css, cur)
Tejun Heoae8086c2013-01-07 08:51:07 -0800591 if (!is_cpuset_subset(c, trial))
592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Remaining checks don't apply to root cpuset */
Tejun Heoae8086c2013-01-07 08:51:07 -0800595 ret = 0;
Paul Jackson69604062006-12-06 20:36:15 -0800596 if (cur == &top_cpuset)
Tejun Heoae8086c2013-01-07 08:51:07 -0800597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Tejun Heoc4310692013-01-07 08:51:08 -0800599 par = parent_cs(cur);
Paul Jackson69604062006-12-06 20:36:15 -0800600
Lu Jialind95af612021-04-08 16:03:46 +0800601 /* On legacy hierarchy, we must be a subset of our parent cpuset. */
Tejun Heoae8086c2013-01-07 08:51:07 -0800602 ret = -EACCES;
Waiman Longb8d1b8e2017-08-17 15:33:10 -0400603 if (!is_in_v2_mode() && !is_cpuset_subset(trial, par))
Tejun Heoae8086c2013-01-07 08:51:07 -0800604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Paul Menage2df167a2008-02-07 00:14:45 -0800606 /*
607 * If either I or some sibling (!= me) is exclusive, we can't
608 * overlap
609 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800610 ret = -EINVAL;
Tejun Heo492eb212013-08-08 20:11:25 -0400611 cpuset_for_each_child(c, css, par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
613 c != cur &&
Li Zefan300ed6c2009-01-07 18:08:44 -0800614 cpumask_intersects(trial->cpus_allowed, c->cpus_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
617 c != cur &&
618 nodes_intersects(trial->mems_allowed, c->mems_allowed))
Tejun Heoae8086c2013-01-07 08:51:07 -0800619 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Tejun Heo452477f2013-01-07 08:51:07 -0800622 /*
623 * Cpusets with tasks - existing or newly being attached - can't
Li Zefan1c09b192013-08-21 10:22:28 +0800624 * be changed to have empty cpus_allowed or mems_allowed.
Tejun Heo452477f2013-01-07 08:51:07 -0800625 */
Tejun Heoae8086c2013-01-07 08:51:07 -0800626 ret = -ENOSPC;
Tejun Heo27bd4db2015-10-15 16:41:50 -0400627 if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) {
Li Zefan1c09b192013-08-21 10:22:28 +0800628 if (!cpumask_empty(cur->cpus_allowed) &&
629 cpumask_empty(trial->cpus_allowed))
630 goto out;
631 if (!nodes_empty(cur->mems_allowed) &&
632 nodes_empty(trial->mems_allowed))
633 goto out;
634 }
Paul Jackson020958b2007-10-18 23:40:21 -0700635
Juri Lellif82f8042014-10-07 09:52:11 +0100636 /*
637 * We can't shrink if we won't have enough room for SCHED_DEADLINE
638 * tasks.
639 */
640 ret = -EBUSY;
641 if (is_cpu_exclusive(cur) &&
642 !cpuset_cpumask_can_shrink(cur->cpus_allowed,
643 trial->cpus_allowed))
644 goto out;
645
Tejun Heoae8086c2013-01-07 08:51:07 -0800646 ret = 0;
647out:
648 rcu_read_unlock();
649 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Paul Menagedb7f47c2009-04-02 16:57:55 -0700652#ifdef CONFIG_SMP
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -0700653/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700654 * Helper routine for generate_sched_domains().
Li Zefan8b5f1c52014-07-09 16:47:50 +0800655 * Do cpusets a, b have overlapping effective cpus_allowed masks?
Paul Jackson029190c2007-10-18 23:40:20 -0700656 */
Paul Jackson029190c2007-10-18 23:40:20 -0700657static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
658{
Li Zefan8b5f1c52014-07-09 16:47:50 +0800659 return cpumask_intersects(a->effective_cpus, b->effective_cpus);
Paul Jackson029190c2007-10-18 23:40:20 -0700660}
661
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900662static void
663update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
664{
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900665 if (dattr->relax_domain_level < c->relax_domain_level)
666 dattr->relax_domain_level = c->relax_domain_level;
667 return;
668}
669
Tejun Heofc560a22013-01-07 08:51:08 -0800670static void update_domain_attr_tree(struct sched_domain_attr *dattr,
671 struct cpuset *root_cs)
Lai Jiangshanf5393692008-07-29 22:33:22 -0700672{
Tejun Heofc560a22013-01-07 08:51:08 -0800673 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -0400674 struct cgroup_subsys_state *pos_css;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700675
Tejun Heofc560a22013-01-07 08:51:08 -0800676 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400677 cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
Tejun Heofc560a22013-01-07 08:51:08 -0800678 /* skip the whole subtree if @cp doesn't have any CPU */
679 if (cpumask_empty(cp->cpus_allowed)) {
Tejun Heo492eb212013-08-08 20:11:25 -0400680 pos_css = css_rightmost_descendant(pos_css);
Lai Jiangshanf5393692008-07-29 22:33:22 -0700681 continue;
Tejun Heofc560a22013-01-07 08:51:08 -0800682 }
Lai Jiangshanf5393692008-07-29 22:33:22 -0700683
684 if (is_sched_load_balance(cp))
685 update_domain_attr(dattr, cp);
Lai Jiangshanf5393692008-07-29 22:33:22 -0700686 }
Tejun Heofc560a22013-01-07 08:51:08 -0800687 rcu_read_unlock();
Lai Jiangshanf5393692008-07-29 22:33:22 -0700688}
689
Paolo Bonzinibe040be2017-08-01 17:24:06 +0200690/* Must be called with cpuset_mutex held. */
691static inline int nr_cpusets(void)
692{
693 /* jump label reference count + the top-level cpuset */
694 return static_key_count(&cpusets_enabled_key.key) + 1;
695}
696
Paul Jackson029190c2007-10-18 23:40:20 -0700697/*
Max Krasnyanskycf417142008-08-11 14:33:53 -0700698 * generate_sched_domains()
Paul Jackson029190c2007-10-18 23:40:20 -0700699 *
Max Krasnyanskycf417142008-08-11 14:33:53 -0700700 * This function builds a partial partition of the systems CPUs
701 * A 'partial partition' is a set of non-overlapping subsets whose
702 * union is a subset of that set.
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530703 * The output of this function needs to be passed to kernel/sched/core.c
Max Krasnyanskycf417142008-08-11 14:33:53 -0700704 * partition_sched_domains() routine, which will rebuild the scheduler's
705 * load balancing domains (sched domains) as specified by that partial
706 * partition.
Paul Jackson029190c2007-10-18 23:40:20 -0700707 *
Mauro Carvalho Chehabda82c922019-06-27 13:08:35 -0300708 * See "What is sched_load_balance" in Documentation/admin-guide/cgroup-v1/cpusets.rst
Paul Jackson029190c2007-10-18 23:40:20 -0700709 * for a background explanation of this.
710 *
711 * Does not return errors, on the theory that the callers of this
712 * routine would rather not worry about failures to rebuild sched
713 * domains when operating in the severe memory shortage situations
714 * that could cause allocation failures below.
715 *
Tejun Heo5d21cc22013-01-07 08:51:08 -0800716 * Must be called with cpuset_mutex held.
Paul Jackson029190c2007-10-18 23:40:20 -0700717 *
718 * The three key local variables below are:
Juri Lellib6fbbf32018-12-19 14:34:44 +0100719 * cp - cpuset pointer, used (together with pos_css) to perform a
720 * top-down scan of all cpusets. For our purposes, rebuilding
721 * the schedulers sched domains, we can ignore !is_sched_load_
722 * balance cpusets.
Paul Jackson029190c2007-10-18 23:40:20 -0700723 * csa - (for CpuSet Array) Array of pointers to all the cpusets
724 * that need to be load balanced, for convenient iterative
725 * access by the subsequent code that finds the best partition,
726 * i.e the set of domains (subsets) of CPUs such that the
727 * cpus_allowed of every cpuset marked is_sched_load_balance
728 * is a subset of one of these domains, while there are as
729 * many such domains as possible, each as small as possible.
730 * doms - Conversion of 'csa' to an array of cpumasks, for passing to
Viresh Kumar0a0fca92013-06-04 13:10:24 +0530731 * the kernel/sched/core.c routine partition_sched_domains() in a
Paul Jackson029190c2007-10-18 23:40:20 -0700732 * convenient format, that can be easily compared to the prior
733 * value to determine what partition elements (sched domains)
734 * were changed (added or removed.)
735 *
736 * Finding the best partition (set of domains):
737 * The triple nested loops below over i, j, k scan over the
738 * load balanced cpusets (using the array of cpuset pointers in
739 * csa[]) looking for pairs of cpusets that have overlapping
740 * cpus_allowed, but which don't have the same 'pn' partition
741 * number and gives them in the same partition number. It keeps
742 * looping on the 'restart' label until it can no longer find
743 * any such pairs.
744 *
745 * The union of the cpus_allowed masks from the set of
746 * all cpusets having the same 'pn' value then form the one
747 * element of the partition (one sched domain) to be passed to
748 * partition_sched_domains().
749 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030750static int generate_sched_domains(cpumask_var_t **domains,
Max Krasnyanskycf417142008-08-11 14:33:53 -0700751 struct sched_domain_attr **attributes)
Paul Jackson029190c2007-10-18 23:40:20 -0700752{
Juri Lellib6fbbf32018-12-19 14:34:44 +0100753 struct cpuset *cp; /* top-down scan of cpusets */
Paul Jackson029190c2007-10-18 23:40:20 -0700754 struct cpuset **csa; /* array of all cpuset ptrs */
755 int csn; /* how many cpuset ptrs in csa so far */
756 int i, j, k; /* indices for partition finding loops */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030757 cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900758 struct sched_domain_attr *dattr; /* attributes for custom domains */
Ingo Molnar15837152008-11-25 10:27:49 +0100759 int ndoms = 0; /* number of sched domains in result */
Li Zefan6af866a2009-01-07 18:08:45 -0800760 int nslot; /* next empty doms[] struct cpumask slot */
Tejun Heo492eb212013-08-08 20:11:25 -0400761 struct cgroup_subsys_state *pos_css;
Waiman Long0ccea8f2018-11-08 10:08:42 -0500762 bool root_load_balance = is_sched_load_balance(&top_cpuset);
Paul Jackson029190c2007-10-18 23:40:20 -0700763
Paul Jackson029190c2007-10-18 23:40:20 -0700764 doms = NULL;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900765 dattr = NULL;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700766 csa = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -0700767
768 /* Special case for the 99% of systems with one, full, sched domain */
Waiman Long0ccea8f2018-11-08 10:08:42 -0500769 if (root_load_balance && !top_cpuset.nr_subparts_cpus) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030770 ndoms = 1;
771 doms = alloc_sched_domains(ndoms);
Paul Jackson029190c2007-10-18 23:40:20 -0700772 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700773 goto done;
774
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900775 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
776 if (dattr) {
777 *dattr = SD_ATTR_INIT;
Li Zefan93a65572008-07-29 22:33:23 -0700778 update_domain_attr_tree(dattr, &top_cpuset);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +0900779 }
Rik van Riel47b8ea72015-03-09 12:12:08 -0400780 cpumask_and(doms[0], top_cpuset.effective_cpus,
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200781 housekeeping_cpumask(HK_FLAG_DOMAIN));
Max Krasnyanskycf417142008-08-11 14:33:53 -0700782
Max Krasnyanskycf417142008-08-11 14:33:53 -0700783 goto done;
Paul Jackson029190c2007-10-18 23:40:20 -0700784 }
785
Kees Cook6da2ec52018-06-12 13:55:00 -0700786 csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700787 if (!csa)
788 goto done;
789 csn = 0;
790
Tejun Heofc560a22013-01-07 08:51:08 -0800791 rcu_read_lock();
Waiman Long0ccea8f2018-11-08 10:08:42 -0500792 if (root_load_balance)
793 csa[csn++] = &top_cpuset;
Tejun Heo492eb212013-08-08 20:11:25 -0400794 cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
Tejun Heobd8815a2013-08-08 20:11:27 -0400795 if (cp == &top_cpuset)
796 continue;
Lai Jiangshanf5393692008-07-29 22:33:22 -0700797 /*
Tejun Heofc560a22013-01-07 08:51:08 -0800798 * Continue traversing beyond @cp iff @cp has some CPUs and
799 * isn't load balancing. The former is obvious. The
800 * latter: All child cpusets contain a subset of the
801 * parent's cpus, so just skip them, and then we call
802 * update_domain_attr_tree() to calc relax_domain_level of
803 * the corresponding sched domain.
Waiman Long0ccea8f2018-11-08 10:08:42 -0500804 *
805 * If root is load-balancing, we can skip @cp if it
806 * is a subset of the root's effective_cpus.
Lai Jiangshanf5393692008-07-29 22:33:22 -0700807 */
Tejun Heofc560a22013-01-07 08:51:08 -0800808 if (!cpumask_empty(cp->cpus_allowed) &&
Rik van Riel47b8ea72015-03-09 12:12:08 -0400809 !(is_sched_load_balance(cp) &&
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200810 cpumask_intersects(cp->cpus_allowed,
811 housekeeping_cpumask(HK_FLAG_DOMAIN))))
Lai Jiangshanf5393692008-07-29 22:33:22 -0700812 continue;
Lai Jiangshan489a5392008-07-25 01:47:23 -0700813
Waiman Long0ccea8f2018-11-08 10:08:42 -0500814 if (root_load_balance &&
815 cpumask_subset(cp->cpus_allowed, top_cpuset.effective_cpus))
816 continue;
817
Valentin Schneidercd1cb332019-10-23 16:37:44 +0100818 if (is_sched_load_balance(cp) &&
819 !cpumask_empty(cp->effective_cpus))
Tejun Heofc560a22013-01-07 08:51:08 -0800820 csa[csn++] = cp;
821
Waiman Long0ccea8f2018-11-08 10:08:42 -0500822 /* skip @cp's subtree if not a partition root */
823 if (!is_partition_root(cp))
824 pos_css = css_rightmost_descendant(pos_css);
Tejun Heofc560a22013-01-07 08:51:08 -0800825 }
826 rcu_read_unlock();
Paul Jackson029190c2007-10-18 23:40:20 -0700827
828 for (i = 0; i < csn; i++)
829 csa[i]->pn = i;
830 ndoms = csn;
831
832restart:
833 /* Find the best partition (set of sched domains) */
834 for (i = 0; i < csn; i++) {
835 struct cpuset *a = csa[i];
836 int apn = a->pn;
837
838 for (j = 0; j < csn; j++) {
839 struct cpuset *b = csa[j];
840 int bpn = b->pn;
841
842 if (apn != bpn && cpusets_overlap(a, b)) {
843 for (k = 0; k < csn; k++) {
844 struct cpuset *c = csa[k];
845
846 if (c->pn == bpn)
847 c->pn = apn;
848 }
849 ndoms--; /* one less element */
850 goto restart;
851 }
852 }
853 }
854
Max Krasnyanskycf417142008-08-11 14:33:53 -0700855 /*
856 * Now we know how many domains to create.
857 * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
858 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030859 doms = alloc_sched_domains(ndoms);
Li Zefan700018e2008-11-18 14:02:03 +0800860 if (!doms)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700861 goto done;
Max Krasnyanskycf417142008-08-11 14:33:53 -0700862
863 /*
864 * The rest of the code, including the scheduler, can deal with
865 * dattr==NULL case. No need to abort if alloc fails.
866 */
Kees Cook6da2ec52018-06-12 13:55:00 -0700867 dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr),
868 GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -0700869
870 for (nslot = 0, i = 0; i < csn; i++) {
871 struct cpuset *a = csa[i];
Li Zefan6af866a2009-01-07 18:08:45 -0800872 struct cpumask *dp;
Paul Jackson029190c2007-10-18 23:40:20 -0700873 int apn = a->pn;
874
Max Krasnyanskycf417142008-08-11 14:33:53 -0700875 if (apn < 0) {
876 /* Skip completed partitions */
877 continue;
Paul Jackson029190c2007-10-18 23:40:20 -0700878 }
Max Krasnyanskycf417142008-08-11 14:33:53 -0700879
Rusty Russellacc3f5d2009-11-03 14:53:40 +1030880 dp = doms[nslot];
Max Krasnyanskycf417142008-08-11 14:33:53 -0700881
882 if (nslot == ndoms) {
883 static int warnings = 10;
884 if (warnings) {
Fabian Frederick12d30892014-05-05 19:49:00 +0200885 pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n",
886 nslot, ndoms, csn, i, apn);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700887 warnings--;
888 }
889 continue;
890 }
891
Li Zefan6af866a2009-01-07 18:08:45 -0800892 cpumask_clear(dp);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700893 if (dattr)
894 *(dattr + nslot) = SD_ATTR_INIT;
895 for (j = i; j < csn; j++) {
896 struct cpuset *b = csa[j];
897
898 if (apn == b->pn) {
Li Zefan8b5f1c52014-07-09 16:47:50 +0800899 cpumask_or(dp, dp, b->effective_cpus);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200900 cpumask_and(dp, dp, housekeeping_cpumask(HK_FLAG_DOMAIN));
Max Krasnyanskycf417142008-08-11 14:33:53 -0700901 if (dattr)
902 update_domain_attr_tree(dattr + nslot, b);
903
904 /* Done with this partition */
905 b->pn = -1;
906 }
907 }
908 nslot++;
Paul Jackson029190c2007-10-18 23:40:20 -0700909 }
910 BUG_ON(nslot != ndoms);
911
Paul Jackson029190c2007-10-18 23:40:20 -0700912done:
Paul Jackson029190c2007-10-18 23:40:20 -0700913 kfree(csa);
Max Krasnyanskycf417142008-08-11 14:33:53 -0700914
Li Zefan700018e2008-11-18 14:02:03 +0800915 /*
916 * Fallback to the default domain if kmalloc() failed.
917 * See comments in partition_sched_domains().
918 */
919 if (doms == NULL)
920 ndoms = 1;
921
Max Krasnyanskycf417142008-08-11 14:33:53 -0700922 *domains = doms;
923 *attributes = dattr;
924 return ndoms;
925}
926
Mathieu Poirierf9a25f72019-07-19 15:59:55 +0200927static void update_tasks_root_domain(struct cpuset *cs)
928{
929 struct css_task_iter it;
930 struct task_struct *task;
931
932 css_task_iter_start(&cs->css, 0, &it);
933
934 while ((task = css_task_iter_next(&it)))
935 dl_add_task_root_domain(task);
936
937 css_task_iter_end(&it);
938}
939
940static void rebuild_root_domains(void)
941{
942 struct cpuset *cs = NULL;
943 struct cgroup_subsys_state *pos_css;
944
Juri Lelli1243dc52019-07-19 15:59:57 +0200945 percpu_rwsem_assert_held(&cpuset_rwsem);
Mathieu Poirierf9a25f72019-07-19 15:59:55 +0200946 lockdep_assert_cpus_held();
947 lockdep_assert_held(&sched_domains_mutex);
948
Mathieu Poirierf9a25f72019-07-19 15:59:55 +0200949 rcu_read_lock();
950
951 /*
952 * Clear default root domain DL accounting, it will be computed again
953 * if a task belongs to it.
954 */
955 dl_clear_root_domain(&def_root_domain);
956
957 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
958
959 if (cpumask_empty(cs->effective_cpus)) {
960 pos_css = css_rightmost_descendant(pos_css);
961 continue;
962 }
963
964 css_get(&cs->css);
965
966 rcu_read_unlock();
967
968 update_tasks_root_domain(cs);
969
970 rcu_read_lock();
971 css_put(&cs->css);
972 }
973 rcu_read_unlock();
974}
975
976static void
977partition_and_rebuild_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
978 struct sched_domain_attr *dattr_new)
979{
980 mutex_lock(&sched_domains_mutex);
981 partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
982 rebuild_root_domains();
983 mutex_unlock(&sched_domains_mutex);
984}
985
Max Krasnyanskycf417142008-08-11 14:33:53 -0700986/*
987 * Rebuild scheduler domains.
988 *
Tejun Heo699140b2013-01-07 08:51:07 -0800989 * If the flag 'sched_load_balance' of any cpuset with non-empty
990 * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
991 * which has that flag enabled, or if any cpuset with a non-empty
992 * 'cpus' is removed, then call this routine to rebuild the
993 * scheduler's dynamic sched domains.
Max Krasnyanskycf417142008-08-11 14:33:53 -0700994 *
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +0200995 * Call with cpuset_mutex held. Takes cpus_read_lock().
Max Krasnyanskycf417142008-08-11 14:33:53 -0700996 */
Tejun Heo699140b2013-01-07 08:51:07 -0800997static void rebuild_sched_domains_locked(void)
Max Krasnyanskycf417142008-08-11 14:33:53 -0700998{
Daniel Jordan406100f2020-11-12 12:17:11 -0500999 struct cgroup_subsys_state *pos_css;
Max Krasnyanskycf417142008-08-11 14:33:53 -07001000 struct sched_domain_attr *attr;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10301001 cpumask_var_t *doms;
Daniel Jordan406100f2020-11-12 12:17:11 -05001002 struct cpuset *cs;
Max Krasnyanskycf417142008-08-11 14:33:53 -07001003 int ndoms;
1004
Juri Lellid74b27d2019-07-19 15:59:58 +02001005 lockdep_assert_cpus_held();
Juri Lelli1243dc52019-07-19 15:59:57 +02001006 percpu_rwsem_assert_held(&cpuset_rwsem);
Max Krasnyanskycf417142008-08-11 14:33:53 -07001007
Li Zefan5b16c2a2013-04-27 06:52:43 -07001008 /*
Daniel Jordan406100f2020-11-12 12:17:11 -05001009 * If we have raced with CPU hotplug, return early to avoid
Li Zefan5b16c2a2013-04-27 06:52:43 -07001010 * passing doms with offlined cpu to partition_sched_domains().
Daniel Jordan406100f2020-11-12 12:17:11 -05001011 * Anyways, cpuset_hotplug_workfn() will rebuild sched domains.
1012 *
1013 * With no CPUs in any subpartitions, top_cpuset's effective CPUs
1014 * should be the same as the active CPUs, so checking only top_cpuset
1015 * is enough to detect racing CPU offlines.
Li Zefan5b16c2a2013-04-27 06:52:43 -07001016 */
Waiman Long0ccea8f2018-11-08 10:08:42 -05001017 if (!top_cpuset.nr_subparts_cpus &&
1018 !cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask))
Juri Lellid74b27d2019-07-19 15:59:58 +02001019 return;
Waiman Long0ccea8f2018-11-08 10:08:42 -05001020
Daniel Jordan406100f2020-11-12 12:17:11 -05001021 /*
1022 * With subpartition CPUs, however, the effective CPUs of a partition
1023 * root should be only a subset of the active CPUs. Since a CPU in any
1024 * partition root could be offlined, all must be checked.
1025 */
1026 if (top_cpuset.nr_subparts_cpus) {
1027 rcu_read_lock();
1028 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
1029 if (!is_partition_root(cs)) {
1030 pos_css = css_rightmost_descendant(pos_css);
1031 continue;
1032 }
1033 if (!cpumask_subset(cs->effective_cpus,
1034 cpu_active_mask)) {
1035 rcu_read_unlock();
1036 return;
1037 }
1038 }
1039 rcu_read_unlock();
1040 }
Li Zefan5b16c2a2013-04-27 06:52:43 -07001041
Max Krasnyanskycf417142008-08-11 14:33:53 -07001042 /* Generate domain masks and attrs */
Max Krasnyanskycf417142008-08-11 14:33:53 -07001043 ndoms = generate_sched_domains(&doms, &attr);
Max Krasnyanskycf417142008-08-11 14:33:53 -07001044
1045 /* Have scheduler rebuild the domains */
Mathieu Poirierf9a25f72019-07-19 15:59:55 +02001046 partition_and_rebuild_sched_domains(ndoms, doms, attr);
Max Krasnyanskycf417142008-08-11 14:33:53 -07001047}
Paul Menagedb7f47c2009-04-02 16:57:55 -07001048#else /* !CONFIG_SMP */
Tejun Heo699140b2013-01-07 08:51:07 -08001049static void rebuild_sched_domains_locked(void)
Paul Menagedb7f47c2009-04-02 16:57:55 -07001050{
1051}
Paul Menagedb7f47c2009-04-02 16:57:55 -07001052#endif /* CONFIG_SMP */
Max Krasnyanskycf417142008-08-11 14:33:53 -07001053
Max Krasnyanskycf417142008-08-11 14:33:53 -07001054void rebuild_sched_domains(void)
1055{
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02001056 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02001057 percpu_down_write(&cpuset_rwsem);
Tejun Heo699140b2013-01-07 08:51:07 -08001058 rebuild_sched_domains_locked();
Juri Lelli1243dc52019-07-19 15:59:57 +02001059 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02001060 cpus_read_unlock();
Paul Jackson029190c2007-10-18 23:40:20 -07001061}
1062
Cliff Wickman58f47902008-02-07 00:14:44 -08001063/**
Miao Xie0b2f6302008-07-25 01:47:21 -07001064 * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
1065 * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
1066 *
Tejun Heod66393e2014-02-13 06:58:40 -05001067 * Iterate through each task of @cs updating its cpus_allowed to the
1068 * effective cpuset's. As this function is called with cpuset_mutex held,
1069 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -07001070 */
Tejun Heod66393e2014-02-13 06:58:40 -05001071static void update_tasks_cpumask(struct cpuset *cs)
Miao Xie0b2f6302008-07-25 01:47:21 -07001072{
Tejun Heod66393e2014-02-13 06:58:40 -05001073 struct css_task_iter it;
1074 struct task_struct *task;
1075
Tejun Heobc2fb7e2017-05-15 09:34:01 -04001076 css_task_iter_start(&cs->css, 0, &it);
Tejun Heod66393e2014-02-13 06:58:40 -05001077 while ((task = css_task_iter_next(&it)))
Li Zefanae1c8022014-07-09 16:48:32 +08001078 set_cpus_allowed_ptr(task, cs->effective_cpus);
Tejun Heod66393e2014-02-13 06:58:40 -05001079 css_task_iter_end(&it);
Miao Xie0b2f6302008-07-25 01:47:21 -07001080}
1081
Waiman Longee8dde02018-11-08 10:08:38 -05001082/**
1083 * compute_effective_cpumask - Compute the effective cpumask of the cpuset
1084 * @new_cpus: the temp variable for the new effective_cpus mask
1085 * @cs: the cpuset the need to recompute the new effective_cpus mask
1086 * @parent: the parent cpuset
1087 *
1088 * If the parent has subpartition CPUs, include them in the list of
Waiman Long4b842da2018-11-08 10:08:41 -05001089 * allowable CPUs in computing the new effective_cpus mask. Since offlined
1090 * CPUs are not removed from subparts_cpus, we have to use cpu_active_mask
1091 * to mask those out.
Waiman Longee8dde02018-11-08 10:08:38 -05001092 */
1093static void compute_effective_cpumask(struct cpumask *new_cpus,
1094 struct cpuset *cs, struct cpuset *parent)
1095{
1096 if (parent->nr_subparts_cpus) {
1097 cpumask_or(new_cpus, parent->effective_cpus,
1098 parent->subparts_cpus);
1099 cpumask_and(new_cpus, new_cpus, cs->cpus_allowed);
Waiman Long4b842da2018-11-08 10:08:41 -05001100 cpumask_and(new_cpus, new_cpus, cpu_active_mask);
Waiman Longee8dde02018-11-08 10:08:38 -05001101 } else {
1102 cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus);
1103 }
1104}
1105
1106/*
1107 * Commands for update_parent_subparts_cpumask
1108 */
1109enum subparts_cmd {
1110 partcmd_enable, /* Enable partition root */
1111 partcmd_disable, /* Disable partition root */
1112 partcmd_update, /* Update parent's subparts_cpus */
1113};
1114
1115/**
1116 * update_parent_subparts_cpumask - update subparts_cpus mask of parent cpuset
1117 * @cpuset: The cpuset that requests change in partition root state
1118 * @cmd: Partition root state change command
1119 * @newmask: Optional new cpumask for partcmd_update
1120 * @tmp: Temporary addmask and delmask
1121 * Return: 0, 1 or an error code
1122 *
1123 * For partcmd_enable, the cpuset is being transformed from a non-partition
1124 * root to a partition root. The cpus_allowed mask of the given cpuset will
1125 * be put into parent's subparts_cpus and taken away from parent's
1126 * effective_cpus. The function will return 0 if all the CPUs listed in
1127 * cpus_allowed can be granted or an error code will be returned.
1128 *
1129 * For partcmd_disable, the cpuset is being transofrmed from a partition
Waiman Long0f3adb82021-07-20 10:18:26 -04001130 * root back to a non-partition root. Any CPUs in cpus_allowed that are in
Waiman Longee8dde02018-11-08 10:08:38 -05001131 * parent's subparts_cpus will be taken away from that cpumask and put back
1132 * into parent's effective_cpus. 0 should always be returned.
1133 *
1134 * For partcmd_update, if the optional newmask is specified, the cpu
1135 * list is to be changed from cpus_allowed to newmask. Otherwise,
Waiman Long3881b862018-11-08 10:08:39 -05001136 * cpus_allowed is assumed to remain the same. The cpuset should either
1137 * be a partition root or an invalid partition root. The partition root
1138 * state may change if newmask is NULL and none of the requested CPUs can
1139 * be granted by the parent. The function will return 1 if changes to
1140 * parent's subparts_cpus and effective_cpus happen or 0 otherwise.
1141 * Error code should only be returned when newmask is non-NULL.
Waiman Longee8dde02018-11-08 10:08:38 -05001142 *
1143 * The partcmd_enable and partcmd_disable commands are used by
1144 * update_prstate(). The partcmd_update command is used by
1145 * update_cpumasks_hier() with newmask NULL and update_cpumask() with
1146 * newmask set.
1147 *
1148 * The checking is more strict when enabling partition root than the
1149 * other two commands.
1150 *
1151 * Because of the implicit cpu exclusive nature of a partition root,
1152 * cpumask changes that violates the cpu exclusivity rule will not be
1153 * permitted when checked by validate_change(). The validate_change()
1154 * function will also prevent any changes to the cpu list if it is not
1155 * a superset of children's cpu lists.
1156 */
1157static int update_parent_subparts_cpumask(struct cpuset *cpuset, int cmd,
1158 struct cpumask *newmask,
1159 struct tmpmasks *tmp)
1160{
1161 struct cpuset *parent = parent_cs(cpuset);
1162 int adding; /* Moving cpus from effective_cpus to subparts_cpus */
1163 int deleting; /* Moving cpus from subparts_cpus to effective_cpus */
Waiman Longe7cc9882021-08-10 23:06:02 -04001164 int old_prs, new_prs;
Waiman Long3881b862018-11-08 10:08:39 -05001165 bool part_error = false; /* Partition error? */
Waiman Longee8dde02018-11-08 10:08:38 -05001166
Juri Lelli1243dc52019-07-19 15:59:57 +02001167 percpu_rwsem_assert_held(&cpuset_rwsem);
Waiman Longee8dde02018-11-08 10:08:38 -05001168
1169 /*
1170 * The parent must be a partition root.
1171 * The new cpumask, if present, or the current cpus_allowed must
1172 * not be empty.
1173 */
1174 if (!is_partition_root(parent) ||
1175 (newmask && cpumask_empty(newmask)) ||
1176 (!newmask && cpumask_empty(cpuset->cpus_allowed)))
1177 return -EINVAL;
1178
1179 /*
1180 * Enabling/disabling partition root is not allowed if there are
1181 * online children.
1182 */
1183 if ((cmd != partcmd_update) && css_has_online_children(&cpuset->css))
1184 return -EBUSY;
1185
1186 /*
1187 * Enabling partition root is not allowed if not all the CPUs
1188 * can be granted from parent's effective_cpus or at least one
1189 * CPU will be left after that.
1190 */
1191 if ((cmd == partcmd_enable) &&
1192 (!cpumask_subset(cpuset->cpus_allowed, parent->effective_cpus) ||
1193 cpumask_equal(cpuset->cpus_allowed, parent->effective_cpus)))
1194 return -EINVAL;
1195
1196 /*
1197 * A cpumask update cannot make parent's effective_cpus become empty.
1198 */
1199 adding = deleting = false;
Waiman Longe7cc9882021-08-10 23:06:02 -04001200 old_prs = new_prs = cpuset->partition_root_state;
Waiman Longee8dde02018-11-08 10:08:38 -05001201 if (cmd == partcmd_enable) {
1202 cpumask_copy(tmp->addmask, cpuset->cpus_allowed);
1203 adding = true;
1204 } else if (cmd == partcmd_disable) {
1205 deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed,
1206 parent->subparts_cpus);
1207 } else if (newmask) {
1208 /*
1209 * partcmd_update with newmask:
1210 *
1211 * delmask = cpus_allowed & ~newmask & parent->subparts_cpus
1212 * addmask = newmask & parent->effective_cpus
1213 * & ~parent->subparts_cpus
1214 */
1215 cpumask_andnot(tmp->delmask, cpuset->cpus_allowed, newmask);
1216 deleting = cpumask_and(tmp->delmask, tmp->delmask,
1217 parent->subparts_cpus);
1218
1219 cpumask_and(tmp->addmask, newmask, parent->effective_cpus);
1220 adding = cpumask_andnot(tmp->addmask, tmp->addmask,
1221 parent->subparts_cpus);
1222 /*
1223 * Return error if the new effective_cpus could become empty.
1224 */
Waiman Long4b842da2018-11-08 10:08:41 -05001225 if (adding &&
1226 cpumask_equal(parent->effective_cpus, tmp->addmask)) {
1227 if (!deleting)
1228 return -EINVAL;
1229 /*
1230 * As some of the CPUs in subparts_cpus might have
1231 * been offlined, we need to compute the real delmask
1232 * to confirm that.
1233 */
1234 if (!cpumask_and(tmp->addmask, tmp->delmask,
1235 cpu_active_mask))
1236 return -EINVAL;
1237 cpumask_copy(tmp->addmask, parent->effective_cpus);
1238 }
Waiman Longee8dde02018-11-08 10:08:38 -05001239 } else {
1240 /*
1241 * partcmd_update w/o newmask:
1242 *
Waiman Long0f3adb82021-07-20 10:18:26 -04001243 * addmask = cpus_allowed & parent->effective_cpus
Waiman Longee8dde02018-11-08 10:08:38 -05001244 *
1245 * Note that parent's subparts_cpus may have been
Waiman Long3881b862018-11-08 10:08:39 -05001246 * pre-shrunk in case there is a change in the cpu list.
1247 * So no deletion is needed.
Waiman Longee8dde02018-11-08 10:08:38 -05001248 */
1249 adding = cpumask_and(tmp->addmask, cpuset->cpus_allowed,
1250 parent->effective_cpus);
Waiman Long3881b862018-11-08 10:08:39 -05001251 part_error = cpumask_equal(tmp->addmask,
1252 parent->effective_cpus);
1253 }
1254
1255 if (cmd == partcmd_update) {
1256 int prev_prs = cpuset->partition_root_state;
1257
1258 /*
1259 * Check for possible transition between PRS_ENABLED
1260 * and PRS_ERROR.
1261 */
1262 switch (cpuset->partition_root_state) {
1263 case PRS_ENABLED:
1264 if (part_error)
Waiman Long6ba34d32021-07-20 10:18:28 -04001265 new_prs = PRS_ERROR;
Waiman Long3881b862018-11-08 10:08:39 -05001266 break;
1267 case PRS_ERROR:
1268 if (!part_error)
Waiman Long6ba34d32021-07-20 10:18:28 -04001269 new_prs = PRS_ENABLED;
Waiman Long3881b862018-11-08 10:08:39 -05001270 break;
1271 }
1272 /*
1273 * Set part_error if previously in invalid state.
1274 */
1275 part_error = (prev_prs == PRS_ERROR);
1276 }
1277
Waiman Long6ba34d32021-07-20 10:18:28 -04001278 if (!part_error && (new_prs == PRS_ERROR))
Waiman Long3881b862018-11-08 10:08:39 -05001279 return 0; /* Nothing need to be done */
1280
Waiman Long6ba34d32021-07-20 10:18:28 -04001281 if (new_prs == PRS_ERROR) {
Waiman Long3881b862018-11-08 10:08:39 -05001282 /*
1283 * Remove all its cpus from parent's subparts_cpus.
1284 */
1285 adding = false;
1286 deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed,
1287 parent->subparts_cpus);
Waiman Longee8dde02018-11-08 10:08:38 -05001288 }
1289
Waiman Longe7cc9882021-08-10 23:06:02 -04001290 if (!adding && !deleting && (new_prs == old_prs))
Waiman Longee8dde02018-11-08 10:08:38 -05001291 return 0;
1292
1293 /*
1294 * Change the parent's subparts_cpus.
1295 * Newly added CPUs will be removed from effective_cpus and
1296 * newly deleted ones will be added back to effective_cpus.
1297 */
1298 spin_lock_irq(&callback_lock);
1299 if (adding) {
1300 cpumask_or(parent->subparts_cpus,
1301 parent->subparts_cpus, tmp->addmask);
1302 cpumask_andnot(parent->effective_cpus,
1303 parent->effective_cpus, tmp->addmask);
1304 }
1305 if (deleting) {
1306 cpumask_andnot(parent->subparts_cpus,
1307 parent->subparts_cpus, tmp->delmask);
Waiman Long4b842da2018-11-08 10:08:41 -05001308 /*
1309 * Some of the CPUs in subparts_cpus might have been offlined.
1310 */
1311 cpumask_and(tmp->delmask, tmp->delmask, cpu_active_mask);
Waiman Longee8dde02018-11-08 10:08:38 -05001312 cpumask_or(parent->effective_cpus,
1313 parent->effective_cpus, tmp->delmask);
1314 }
1315
1316 parent->nr_subparts_cpus = cpumask_weight(parent->subparts_cpus);
Waiman Long6ba34d32021-07-20 10:18:28 -04001317
Waiman Longe7cc9882021-08-10 23:06:02 -04001318 if (old_prs != new_prs)
Waiman Long6ba34d32021-07-20 10:18:28 -04001319 cpuset->partition_root_state = new_prs;
Waiman Longe7cc9882021-08-10 23:06:02 -04001320
Waiman Longee8dde02018-11-08 10:08:38 -05001321 spin_unlock_irq(&callback_lock);
Waiman Longe7cc9882021-08-10 23:06:02 -04001322 notify_partition_change(cpuset, old_prs, new_prs);
Waiman Longee8dde02018-11-08 10:08:38 -05001323
1324 return cmd == partcmd_update;
1325}
1326
Li Zefan5c5cc622013-06-09 17:16:29 +08001327/*
Li Zefan734d4512014-07-09 16:47:29 +08001328 * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
Waiman Longee8dde02018-11-08 10:08:38 -05001329 * @cs: the cpuset to consider
1330 * @tmp: temp variables for calculating effective_cpus & partition setup
Li Zefan5c5cc622013-06-09 17:16:29 +08001331 *
Aubrey Li415de5f2021-01-13 12:37:41 +08001332 * When configured cpumask is changed, the effective cpumasks of this cpuset
Li Zefan734d4512014-07-09 16:47:29 +08001333 * and all its descendants need to be updated.
1334 *
Aubrey Li415de5f2021-01-13 12:37:41 +08001335 * On legacy hierarchy, effective_cpus will be the same with cpu_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +08001336 *
1337 * Called with cpuset_mutex held
1338 */
Waiman Longee8dde02018-11-08 10:08:38 -05001339static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp)
Li Zefan5c5cc622013-06-09 17:16:29 +08001340{
1341 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -04001342 struct cgroup_subsys_state *pos_css;
Li Zefan8b5f1c52014-07-09 16:47:50 +08001343 bool need_rebuild_sched_domains = false;
Waiman Longe7cc9882021-08-10 23:06:02 -04001344 int old_prs, new_prs;
Li Zefan5c5cc622013-06-09 17:16:29 +08001345
1346 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +08001347 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1348 struct cpuset *parent = parent_cs(cp);
1349
Waiman Longee8dde02018-11-08 10:08:38 -05001350 compute_effective_cpumask(tmp->new_cpus, cp, parent);
Li Zefan734d4512014-07-09 16:47:29 +08001351
Li Zefan554b0d12014-07-09 16:47:41 +08001352 /*
1353 * If it becomes empty, inherit the effective mask of the
1354 * parent, which is guaranteed to have some CPUs.
1355 */
Waiman Long47169092018-11-08 10:08:40 -05001356 if (is_in_v2_mode() && cpumask_empty(tmp->new_cpus)) {
Waiman Longee8dde02018-11-08 10:08:38 -05001357 cpumask_copy(tmp->new_cpus, parent->effective_cpus);
Waiman Long47169092018-11-08 10:08:40 -05001358 if (!cp->use_parent_ecpus) {
1359 cp->use_parent_ecpus = true;
1360 parent->child_ecpus_count++;
1361 }
1362 } else if (cp->use_parent_ecpus) {
1363 cp->use_parent_ecpus = false;
1364 WARN_ON_ONCE(!parent->child_ecpus_count);
1365 parent->child_ecpus_count--;
1366 }
Li Zefan554b0d12014-07-09 16:47:41 +08001367
Waiman Longee8dde02018-11-08 10:08:38 -05001368 /*
1369 * Skip the whole subtree if the cpumask remains the same
1370 * and has no partition root state.
1371 */
Waiman Long3881b862018-11-08 10:08:39 -05001372 if (!cp->partition_root_state &&
Waiman Longee8dde02018-11-08 10:08:38 -05001373 cpumask_equal(tmp->new_cpus, cp->effective_cpus)) {
Li Zefan734d4512014-07-09 16:47:29 +08001374 pos_css = css_rightmost_descendant(pos_css);
1375 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +08001376 }
Li Zefan734d4512014-07-09 16:47:29 +08001377
Waiman Longee8dde02018-11-08 10:08:38 -05001378 /*
1379 * update_parent_subparts_cpumask() should have been called
1380 * for cs already in update_cpumask(). We should also call
1381 * update_tasks_cpumask() again for tasks in the parent
1382 * cpuset if the parent's subparts_cpus changes.
1383 */
Waiman Longe7cc9882021-08-10 23:06:02 -04001384 old_prs = new_prs = cp->partition_root_state;
1385 if ((cp != cs) && old_prs) {
Waiman Long3881b862018-11-08 10:08:39 -05001386 switch (parent->partition_root_state) {
1387 case PRS_DISABLED:
1388 /*
1389 * If parent is not a partition root or an
Waiman Long0f3adb82021-07-20 10:18:26 -04001390 * invalid partition root, clear its state
1391 * and its CS_CPU_EXCLUSIVE flag.
Waiman Long3881b862018-11-08 10:08:39 -05001392 */
1393 WARN_ON_ONCE(cp->partition_root_state
1394 != PRS_ERROR);
Waiman Long6ba34d32021-07-20 10:18:28 -04001395 new_prs = PRS_DISABLED;
Waiman Long3881b862018-11-08 10:08:39 -05001396
1397 /*
1398 * clear_bit() is an atomic operation and
1399 * readers aren't interested in the state
1400 * of CS_CPU_EXCLUSIVE anyway. So we can
1401 * just update the flag without holding
1402 * the callback_lock.
1403 */
1404 clear_bit(CS_CPU_EXCLUSIVE, &cp->flags);
1405 break;
1406
1407 case PRS_ENABLED:
1408 if (update_parent_subparts_cpumask(cp, partcmd_update, NULL, tmp))
1409 update_tasks_cpumask(parent);
1410 break;
1411
1412 case PRS_ERROR:
1413 /*
1414 * When parent is invalid, it has to be too.
1415 */
Waiman Long6ba34d32021-07-20 10:18:28 -04001416 new_prs = PRS_ERROR;
Waiman Long3881b862018-11-08 10:08:39 -05001417 break;
1418 }
Waiman Longee8dde02018-11-08 10:08:38 -05001419 }
1420
Tejun Heoec903c02014-05-13 12:11:01 -04001421 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +08001422 continue;
1423 rcu_read_unlock();
1424
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001425 spin_lock_irq(&callback_lock);
Waiman Longee8dde02018-11-08 10:08:38 -05001426
1427 cpumask_copy(cp->effective_cpus, tmp->new_cpus);
Waiman Long6ba34d32021-07-20 10:18:28 -04001428 if (cp->nr_subparts_cpus && (new_prs != PRS_ENABLED)) {
Waiman Long3881b862018-11-08 10:08:39 -05001429 cp->nr_subparts_cpus = 0;
1430 cpumask_clear(cp->subparts_cpus);
1431 } else if (cp->nr_subparts_cpus) {
Waiman Longee8dde02018-11-08 10:08:38 -05001432 /*
1433 * Make sure that effective_cpus & subparts_cpus
1434 * are mutually exclusive.
Waiman Long3881b862018-11-08 10:08:39 -05001435 *
1436 * In the unlikely event that effective_cpus
1437 * becomes empty. we clear cp->nr_subparts_cpus and
1438 * let its child partition roots to compete for
1439 * CPUs again.
Waiman Longee8dde02018-11-08 10:08:38 -05001440 */
1441 cpumask_andnot(cp->effective_cpus, cp->effective_cpus,
1442 cp->subparts_cpus);
Waiman Long3881b862018-11-08 10:08:39 -05001443 if (cpumask_empty(cp->effective_cpus)) {
1444 cpumask_copy(cp->effective_cpus, tmp->new_cpus);
1445 cpumask_clear(cp->subparts_cpus);
1446 cp->nr_subparts_cpus = 0;
1447 } else if (!cpumask_subset(cp->subparts_cpus,
1448 tmp->new_cpus)) {
1449 cpumask_andnot(cp->subparts_cpus,
1450 cp->subparts_cpus, tmp->new_cpus);
1451 cp->nr_subparts_cpus
1452 = cpumask_weight(cp->subparts_cpus);
1453 }
Waiman Longee8dde02018-11-08 10:08:38 -05001454 }
Waiman Long6ba34d32021-07-20 10:18:28 -04001455
Waiman Longe7cc9882021-08-10 23:06:02 -04001456 if (new_prs != old_prs)
Waiman Long6ba34d32021-07-20 10:18:28 -04001457 cp->partition_root_state = new_prs;
1458
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001459 spin_unlock_irq(&callback_lock);
Waiman Longe7cc9882021-08-10 23:06:02 -04001460 notify_partition_change(cp, old_prs, new_prs);
Li Zefan734d4512014-07-09 16:47:29 +08001461
Waiman Longb8d1b8e2017-08-17 15:33:10 -04001462 WARN_ON(!is_in_v2_mode() &&
Li Zefan734d4512014-07-09 16:47:29 +08001463 !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
1464
Tejun Heod66393e2014-02-13 06:58:40 -05001465 update_tasks_cpumask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +08001466
Li Zefan8b5f1c52014-07-09 16:47:50 +08001467 /*
Waiman Long0ccea8f2018-11-08 10:08:42 -05001468 * On legacy hierarchy, if the effective cpumask of any non-
1469 * empty cpuset is changed, we need to rebuild sched domains.
1470 * On default hierarchy, the cpuset needs to be a partition
1471 * root as well.
Li Zefan8b5f1c52014-07-09 16:47:50 +08001472 */
1473 if (!cpumask_empty(cp->cpus_allowed) &&
Waiman Long0ccea8f2018-11-08 10:08:42 -05001474 is_sched_load_balance(cp) &&
1475 (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
1476 is_partition_root(cp)))
Li Zefan8b5f1c52014-07-09 16:47:50 +08001477 need_rebuild_sched_domains = true;
1478
Li Zefan5c5cc622013-06-09 17:16:29 +08001479 rcu_read_lock();
1480 css_put(&cp->css);
1481 }
1482 rcu_read_unlock();
Li Zefan8b5f1c52014-07-09 16:47:50 +08001483
1484 if (need_rebuild_sched_domains)
1485 rebuild_sched_domains_locked();
Li Zefan5c5cc622013-06-09 17:16:29 +08001486}
1487
Miao Xie0b2f6302008-07-25 01:47:21 -07001488/**
Waiman Long47169092018-11-08 10:08:40 -05001489 * update_sibling_cpumasks - Update siblings cpumasks
1490 * @parent: Parent cpuset
1491 * @cs: Current cpuset
1492 * @tmp: Temp variables
1493 */
1494static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
1495 struct tmpmasks *tmp)
1496{
1497 struct cpuset *sibling;
1498 struct cgroup_subsys_state *pos_css;
1499
1500 /*
1501 * Check all its siblings and call update_cpumasks_hier()
1502 * if their use_parent_ecpus flag is set in order for them
1503 * to use the right effective_cpus value.
1504 */
1505 rcu_read_lock();
1506 cpuset_for_each_child(sibling, pos_css, parent) {
1507 if (sibling == cs)
1508 continue;
1509 if (!sibling->use_parent_ecpus)
1510 continue;
1511
1512 update_cpumasks_hier(sibling, tmp);
1513 }
1514 rcu_read_unlock();
1515}
1516
1517/**
Cliff Wickman58f47902008-02-07 00:14:44 -08001518 * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
1519 * @cs: the cpuset to consider
Fabian Frederickfc34ac12014-05-05 19:46:55 +02001520 * @trialcs: trial cpuset
Cliff Wickman58f47902008-02-07 00:14:44 -08001521 * @buf: buffer of cpu numbers written to this cpuset
1522 */
Li Zefan645fcc92009-01-07 18:08:43 -08001523static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
1524 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Cliff Wickman58f47902008-02-07 00:14:44 -08001526 int retval;
Waiman Longee8dde02018-11-08 10:08:38 -05001527 struct tmpmasks tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Rusty Russell5f054e32012-03-29 15:38:31 +10301529 /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
Paul Jackson4c4d50f2006-08-27 01:23:51 -07001530 if (cs == &top_cpuset)
1531 return -EACCES;
1532
David Rientjes6f7f02e2007-05-08 00:31:43 -07001533 /*
Paul Jacksonc8d9c902008-02-07 00:14:46 -08001534 * An empty cpus_allowed is ok only if the cpuset has no tasks.
Paul Jackson020958b2007-10-18 23:40:21 -07001535 * Since cpulist_parse() fails on an empty mask, we special case
1536 * that parsing. The validate_change() call ensures that cpusets
1537 * with tasks have cpus.
David Rientjes6f7f02e2007-05-08 00:31:43 -07001538 */
Paul Jackson020958b2007-10-18 23:40:21 -07001539 if (!*buf) {
Li Zefan300ed6c2009-01-07 18:08:44 -08001540 cpumask_clear(trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -07001541 } else {
Li Zefan300ed6c2009-01-07 18:08:44 -08001542 retval = cpulist_parse(buf, trialcs->cpus_allowed);
David Rientjes6f7f02e2007-05-08 00:31:43 -07001543 if (retval < 0)
1544 return retval;
Lai Jiangshan37340742008-06-05 22:46:32 -07001545
Li Zefan5d8ba822014-07-09 16:49:12 +08001546 if (!cpumask_subset(trialcs->cpus_allowed,
1547 top_cpuset.cpus_allowed))
Lai Jiangshan37340742008-06-05 22:46:32 -07001548 return -EINVAL;
David Rientjes6f7f02e2007-05-08 00:31:43 -07001549 }
Paul Jackson029190c2007-10-18 23:40:20 -07001550
Paul Menage8707d8b2007-10-18 23:40:22 -07001551 /* Nothing to do if the cpus didn't change */
Li Zefan300ed6c2009-01-07 18:08:44 -08001552 if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
Paul Menage8707d8b2007-10-18 23:40:22 -07001553 return 0;
Cliff Wickman58f47902008-02-07 00:14:44 -08001554
Li Zefana73456f2013-06-05 17:15:59 +08001555 retval = validate_change(cs, trialcs);
1556 if (retval < 0)
1557 return retval;
1558
Waiman Longee8dde02018-11-08 10:08:38 -05001559#ifdef CONFIG_CPUMASK_OFFSTACK
1560 /*
1561 * Use the cpumasks in trialcs for tmpmasks when they are pointers
1562 * to allocated cpumasks.
1563 */
1564 tmp.addmask = trialcs->subparts_cpus;
1565 tmp.delmask = trialcs->effective_cpus;
1566 tmp.new_cpus = trialcs->cpus_allowed;
1567#endif
1568
1569 if (cs->partition_root_state) {
1570 /* Cpumask of a partition root cannot be empty */
1571 if (cpumask_empty(trialcs->cpus_allowed))
1572 return -EINVAL;
1573 if (update_parent_subparts_cpumask(cs, partcmd_update,
1574 trialcs->cpus_allowed, &tmp) < 0)
1575 return -EINVAL;
1576 }
1577
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001578 spin_lock_irq(&callback_lock);
Li Zefan300ed6c2009-01-07 18:08:44 -08001579 cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
Waiman Longee8dde02018-11-08 10:08:38 -05001580
1581 /*
1582 * Make sure that subparts_cpus is a subset of cpus_allowed.
1583 */
1584 if (cs->nr_subparts_cpus) {
1585 cpumask_andnot(cs->subparts_cpus, cs->subparts_cpus,
1586 cs->cpus_allowed);
1587 cs->nr_subparts_cpus = cpumask_weight(cs->subparts_cpus);
1588 }
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001589 spin_unlock_irq(&callback_lock);
Paul Jackson029190c2007-10-18 23:40:20 -07001590
Waiman Longee8dde02018-11-08 10:08:38 -05001591 update_cpumasks_hier(cs, &tmp);
Waiman Long47169092018-11-08 10:08:40 -05001592
1593 if (cs->partition_root_state) {
1594 struct cpuset *parent = parent_cs(cs);
1595
1596 /*
1597 * For partition root, update the cpumasks of sibling
1598 * cpusets if they use parent's effective_cpus.
1599 */
1600 if (parent->child_ecpus_count)
1601 update_sibling_cpumasks(parent, cs, &tmp);
1602 }
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Paul Jackson053199e2005-10-30 15:02:30 -08001606/*
Tejun Heoe93ad192016-01-19 12:18:41 -05001607 * Migrate memory region from one set of nodes to another. This is
1608 * performed asynchronously as it can be called from process migration path
1609 * holding locks involved in process management. All mm migrations are
1610 * performed in the queued order and can be waited for by flushing
1611 * cpuset_migrate_mm_wq.
Paul Jacksone4e364e2006-03-31 02:30:52 -08001612 */
1613
Tejun Heoe93ad192016-01-19 12:18:41 -05001614struct cpuset_migrate_mm_work {
1615 struct work_struct work;
1616 struct mm_struct *mm;
1617 nodemask_t from;
1618 nodemask_t to;
1619};
1620
1621static void cpuset_migrate_mm_workfn(struct work_struct *work)
1622{
1623 struct cpuset_migrate_mm_work *mwork =
1624 container_of(work, struct cpuset_migrate_mm_work, work);
1625
1626 /* on a wq worker, no need to worry about %current's mems_allowed */
1627 do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
1628 mmput(mwork->mm);
1629 kfree(mwork);
1630}
1631
Paul Jacksone4e364e2006-03-31 02:30:52 -08001632static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
1633 const nodemask_t *to)
1634{
Tejun Heoe93ad192016-01-19 12:18:41 -05001635 struct cpuset_migrate_mm_work *mwork;
Paul Jacksone4e364e2006-03-31 02:30:52 -08001636
Tejun Heoe93ad192016-01-19 12:18:41 -05001637 mwork = kzalloc(sizeof(*mwork), GFP_KERNEL);
1638 if (mwork) {
1639 mwork->mm = mm;
1640 mwork->from = *from;
1641 mwork->to = *to;
1642 INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn);
1643 queue_work(cpuset_migrate_mm_wq, &mwork->work);
1644 } else {
1645 mmput(mm);
1646 }
1647}
Paul Jacksone4e364e2006-03-31 02:30:52 -08001648
Tejun Heo5cf1cac2016-04-21 19:06:48 -04001649static void cpuset_post_attach(void)
Tejun Heoe93ad192016-01-19 12:18:41 -05001650{
1651 flush_workqueue(cpuset_migrate_mm_wq);
Paul Jacksone4e364e2006-03-31 02:30:52 -08001652}
1653
Li Zefan3b6766f2009-04-02 16:57:51 -07001654/*
Miao Xie58568d22009-06-16 15:31:49 -07001655 * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
1656 * @tsk: the task to change
1657 * @newmems: new nodes that the task will be set
1658 *
Vlastimil Babka5f155f22017-07-06 15:40:09 -07001659 * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed
1660 * and rebind an eventual tasks' mempolicy. If the task is allocating in
1661 * parallel, it might temporarily see an empty intersection, which results in
1662 * a seqlock check and retry before OOM or allocation failure.
Miao Xie58568d22009-06-16 15:31:49 -07001663 */
1664static void cpuset_change_task_nodemask(struct task_struct *tsk,
1665 nodemask_t *newmems)
1666{
Miao Xiec0ff7452010-05-24 14:32:08 -07001667 task_lock(tsk);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001668
Vlastimil Babka5f155f22017-07-06 15:40:09 -07001669 local_irq_disable();
1670 write_seqcount_begin(&tsk->mems_allowed_seq);
Mel Gormancc9a6c82012-03-21 16:34:11 -07001671
Miao Xie58568d22009-06-16 15:31:49 -07001672 nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
Vlastimil Babka213980c2017-07-06 15:40:06 -07001673 mpol_rebind_task(tsk, newmems);
Miao Xie58568d22009-06-16 15:31:49 -07001674 tsk->mems_allowed = *newmems;
Mel Gormancc9a6c82012-03-21 16:34:11 -07001675
Vlastimil Babka5f155f22017-07-06 15:40:09 -07001676 write_seqcount_end(&tsk->mems_allowed_seq);
1677 local_irq_enable();
Mel Gormancc9a6c82012-03-21 16:34:11 -07001678
Miao Xiec0ff7452010-05-24 14:32:08 -07001679 task_unlock(tsk);
Miao Xie58568d22009-06-16 15:31:49 -07001680}
1681
Paul Menage8793d852007-10-18 23:39:39 -07001682static void *cpuset_being_rebound;
1683
Miao Xie0b2f6302008-07-25 01:47:21 -07001684/**
1685 * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
1686 * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
Miao Xie0b2f6302008-07-25 01:47:21 -07001687 *
Tejun Heod66393e2014-02-13 06:58:40 -05001688 * Iterate through each task of @cs updating its mems_allowed to the
1689 * effective cpuset's. As this function is called with cpuset_mutex held,
1690 * cpuset membership stays stable.
Miao Xie0b2f6302008-07-25 01:47:21 -07001691 */
Tejun Heod66393e2014-02-13 06:58:40 -05001692static void update_tasks_nodemask(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Li Zefan33ad8012013-06-09 17:15:08 +08001694 static nodemask_t newmems; /* protected by cpuset_mutex */
Tejun Heod66393e2014-02-13 06:58:40 -05001695 struct css_task_iter it;
1696 struct task_struct *task;
Paul Jackson59dac162006-01-08 01:01:52 -08001697
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07001698 cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
Paul Jackson42253992006-01-08 01:01:59 -08001699
Li Zefanae1c8022014-07-09 16:48:32 +08001700 guarantee_online_mems(cs, &newmems);
Li Zefan33ad8012013-06-09 17:15:08 +08001701
Paul Jackson42253992006-01-08 01:01:59 -08001702 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001703 * The mpol_rebind_mm() call takes mmap_lock, which we couldn't
Li Zefan3b6766f2009-04-02 16:57:51 -07001704 * take while holding tasklist_lock. Forks can happen - the
1705 * mpol_dup() cpuset_being_rebound check will catch such forks,
1706 * and rebind their vma mempolicies too. Because we still hold
Tejun Heo5d21cc22013-01-07 08:51:08 -08001707 * the global cpuset_mutex, we know that no other rebind effort
Li Zefan3b6766f2009-04-02 16:57:51 -07001708 * will be contending for the global variable cpuset_being_rebound.
Paul Jackson42253992006-01-08 01:01:59 -08001709 * It's ok if we rebind the same mm twice; mpol_rebind_mm()
Paul Jackson04c19fa2006-01-08 01:02:00 -08001710 * is idempotent. Also migrate pages in each mm to new nodes.
Paul Jackson42253992006-01-08 01:01:59 -08001711 */
Tejun Heobc2fb7e2017-05-15 09:34:01 -04001712 css_task_iter_start(&cs->css, 0, &it);
Tejun Heod66393e2014-02-13 06:58:40 -05001713 while ((task = css_task_iter_next(&it))) {
1714 struct mm_struct *mm;
1715 bool migrate;
1716
1717 cpuset_change_task_nodemask(task, &newmems);
1718
1719 mm = get_task_mm(task);
1720 if (!mm)
1721 continue;
1722
1723 migrate = is_memory_migrate(cs);
1724
1725 mpol_rebind_mm(mm, &cs->mems_allowed);
1726 if (migrate)
1727 cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
Tejun Heoe93ad192016-01-19 12:18:41 -05001728 else
1729 mmput(mm);
Tejun Heod66393e2014-02-13 06:58:40 -05001730 }
1731 css_task_iter_end(&it);
Paul Jackson42253992006-01-08 01:01:59 -08001732
Li Zefan33ad8012013-06-09 17:15:08 +08001733 /*
1734 * All the tasks' nodemasks have been updated, update
1735 * cs->old_mems_allowed.
1736 */
1737 cs->old_mems_allowed = newmems;
1738
Paul Menage2df167a2008-02-07 00:14:45 -08001739 /* We're done rebinding vmas to this cpuset's new mems_allowed. */
Paul Menage8793d852007-10-18 23:39:39 -07001740 cpuset_being_rebound = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Miao Xie0b2f6302008-07-25 01:47:21 -07001743/*
Li Zefan734d4512014-07-09 16:47:29 +08001744 * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
1745 * @cs: the cpuset to consider
1746 * @new_mems: a temp variable for calculating new effective_mems
Li Zefan5c5cc622013-06-09 17:16:29 +08001747 *
Li Zefan734d4512014-07-09 16:47:29 +08001748 * When configured nodemask is changed, the effective nodemasks of this cpuset
1749 * and all its descendants need to be updated.
1750 *
Lu Jialind95af612021-04-08 16:03:46 +08001751 * On legacy hierarchy, effective_mems will be the same with mems_allowed.
Li Zefan5c5cc622013-06-09 17:16:29 +08001752 *
1753 * Called with cpuset_mutex held
1754 */
Li Zefan734d4512014-07-09 16:47:29 +08001755static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
Li Zefan5c5cc622013-06-09 17:16:29 +08001756{
1757 struct cpuset *cp;
Tejun Heo492eb212013-08-08 20:11:25 -04001758 struct cgroup_subsys_state *pos_css;
Li Zefan5c5cc622013-06-09 17:16:29 +08001759
1760 rcu_read_lock();
Li Zefan734d4512014-07-09 16:47:29 +08001761 cpuset_for_each_descendant_pre(cp, pos_css, cs) {
1762 struct cpuset *parent = parent_cs(cp);
1763
1764 nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
1765
Li Zefan554b0d12014-07-09 16:47:41 +08001766 /*
1767 * If it becomes empty, inherit the effective mask of the
1768 * parent, which is guaranteed to have some MEMs.
1769 */
Waiman Longb8d1b8e2017-08-17 15:33:10 -04001770 if (is_in_v2_mode() && nodes_empty(*new_mems))
Li Zefan554b0d12014-07-09 16:47:41 +08001771 *new_mems = parent->effective_mems;
1772
Li Zefan734d4512014-07-09 16:47:29 +08001773 /* Skip the whole subtree if the nodemask remains the same. */
1774 if (nodes_equal(*new_mems, cp->effective_mems)) {
1775 pos_css = css_rightmost_descendant(pos_css);
1776 continue;
Li Zefan5c5cc622013-06-09 17:16:29 +08001777 }
Li Zefan734d4512014-07-09 16:47:29 +08001778
Tejun Heoec903c02014-05-13 12:11:01 -04001779 if (!css_tryget_online(&cp->css))
Li Zefan5c5cc622013-06-09 17:16:29 +08001780 continue;
1781 rcu_read_unlock();
1782
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001783 spin_lock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001784 cp->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001785 spin_unlock_irq(&callback_lock);
Li Zefan734d4512014-07-09 16:47:29 +08001786
Waiman Longb8d1b8e2017-08-17 15:33:10 -04001787 WARN_ON(!is_in_v2_mode() &&
Li Zefana1381262014-07-30 15:07:13 +08001788 !nodes_equal(cp->mems_allowed, cp->effective_mems));
Li Zefan734d4512014-07-09 16:47:29 +08001789
Tejun Heod66393e2014-02-13 06:58:40 -05001790 update_tasks_nodemask(cp);
Li Zefan5c5cc622013-06-09 17:16:29 +08001791
1792 rcu_read_lock();
1793 css_put(&cp->css);
1794 }
1795 rcu_read_unlock();
1796}
1797
1798/*
Miao Xie0b2f6302008-07-25 01:47:21 -07001799 * Handle user request to change the 'mems' memory placement
1800 * of a cpuset. Needs to validate the request, update the
Miao Xie58568d22009-06-16 15:31:49 -07001801 * cpusets mems_allowed, and for each task in the cpuset,
1802 * update mems_allowed and rebind task's mempolicy and any vma
1803 * mempolicies and if the cpuset is marked 'memory_migrate',
1804 * migrate the tasks pages to the new memory.
Miao Xie0b2f6302008-07-25 01:47:21 -07001805 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001806 * Call with cpuset_mutex held. May take callback_lock during call.
Miao Xie0b2f6302008-07-25 01:47:21 -07001807 * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001808 * lock each such tasks mm->mmap_lock, scan its vma's and rebind
Miao Xie0b2f6302008-07-25 01:47:21 -07001809 * their mempolicies to the cpusets new mems_allowed.
1810 */
Li Zefan645fcc92009-01-07 18:08:43 -08001811static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
1812 const char *buf)
Miao Xie0b2f6302008-07-25 01:47:21 -07001813{
Miao Xie0b2f6302008-07-25 01:47:21 -07001814 int retval;
1815
1816 /*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08001817 * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
Miao Xie0b2f6302008-07-25 01:47:21 -07001818 * it's read-only
1819 */
Miao Xie53feb292010-03-23 13:35:35 -07001820 if (cs == &top_cpuset) {
1821 retval = -EACCES;
1822 goto done;
1823 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001824
Miao Xie0b2f6302008-07-25 01:47:21 -07001825 /*
1826 * An empty mems_allowed is ok iff there are no tasks in the cpuset.
1827 * Since nodelist_parse() fails on an empty mask, we special case
1828 * that parsing. The validate_change() call ensures that cpusets
1829 * with tasks have memory.
1830 */
1831 if (!*buf) {
Li Zefan645fcc92009-01-07 18:08:43 -08001832 nodes_clear(trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001833 } else {
Li Zefan645fcc92009-01-07 18:08:43 -08001834 retval = nodelist_parse(buf, trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001835 if (retval < 0)
1836 goto done;
1837
Li Zefan645fcc92009-01-07 18:08:43 -08001838 if (!nodes_subset(trialcs->mems_allowed,
Li Zefan5d8ba822014-07-09 16:49:12 +08001839 top_cpuset.mems_allowed)) {
1840 retval = -EINVAL;
Miao Xie53feb292010-03-23 13:35:35 -07001841 goto done;
1842 }
Miao Xie0b2f6302008-07-25 01:47:21 -07001843 }
Li Zefan33ad8012013-06-09 17:15:08 +08001844
1845 if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) {
Miao Xie0b2f6302008-07-25 01:47:21 -07001846 retval = 0; /* Too easy - nothing to do */
1847 goto done;
1848 }
Li Zefan645fcc92009-01-07 18:08:43 -08001849 retval = validate_change(cs, trialcs);
Miao Xie0b2f6302008-07-25 01:47:21 -07001850 if (retval < 0)
1851 goto done;
1852
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001853 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001854 cs->mems_allowed = trialcs->mems_allowed;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001855 spin_unlock_irq(&callback_lock);
Miao Xie0b2f6302008-07-25 01:47:21 -07001856
Li Zefan734d4512014-07-09 16:47:29 +08001857 /* use trialcs->mems_allowed as a temp variable */
Alban Crequy24ee3cf2015-08-06 16:21:05 +02001858 update_nodemasks_hier(cs, &trialcs->mems_allowed);
Miao Xie0b2f6302008-07-25 01:47:21 -07001859done:
1860 return retval;
1861}
1862
Yaowei Bai77ef80c2018-02-06 15:41:24 -08001863bool current_cpuset_is_being_rebound(void)
Paul Menage8793d852007-10-18 23:39:39 -07001864{
Yaowei Bai77ef80c2018-02-06 15:41:24 -08001865 bool ret;
Gu Zheng391acf92014-06-25 09:57:18 +08001866
1867 rcu_read_lock();
1868 ret = task_cs(current) == cpuset_being_rebound;
1869 rcu_read_unlock();
1870
1871 return ret;
Paul Menage8793d852007-10-18 23:39:39 -07001872}
1873
Paul Menage5be7a472008-05-06 20:42:41 -07001874static int update_relax_domain_level(struct cpuset *cs, s64 val)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001875{
Paul Menagedb7f47c2009-04-02 16:57:55 -07001876#ifdef CONFIG_SMP
Peter Zijlstra60495e72011-04-07 14:10:04 +02001877 if (val < -1 || val >= sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08001878 return -EINVAL;
Paul Menagedb7f47c2009-04-02 16:57:55 -07001879#endif
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001880
1881 if (val != cs->relax_domain_level) {
1882 cs->relax_domain_level = val;
Li Zefan300ed6c2009-01-07 18:08:44 -08001883 if (!cpumask_empty(cs->cpus_allowed) &&
1884 is_sched_load_balance(cs))
Tejun Heo699140b2013-01-07 08:51:07 -08001885 rebuild_sched_domains_locked();
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09001886 }
1887
1888 return 0;
1889}
1890
Tejun Heo72ec7022013-08-08 20:11:26 -04001891/**
Miao Xie950592f2009-06-16 15:31:47 -07001892 * update_tasks_flags - update the spread flags of tasks in the cpuset.
1893 * @cs: the cpuset in which each task's spread flags needs to be changed
Miao Xie950592f2009-06-16 15:31:47 -07001894 *
Tejun Heod66393e2014-02-13 06:58:40 -05001895 * Iterate through each task of @cs updating its spread flags. As this
1896 * function is called with cpuset_mutex held, cpuset membership stays
1897 * stable.
Miao Xie950592f2009-06-16 15:31:47 -07001898 */
Tejun Heod66393e2014-02-13 06:58:40 -05001899static void update_tasks_flags(struct cpuset *cs)
Miao Xie950592f2009-06-16 15:31:47 -07001900{
Tejun Heod66393e2014-02-13 06:58:40 -05001901 struct css_task_iter it;
1902 struct task_struct *task;
1903
Tejun Heobc2fb7e2017-05-15 09:34:01 -04001904 css_task_iter_start(&cs->css, 0, &it);
Tejun Heod66393e2014-02-13 06:58:40 -05001905 while ((task = css_task_iter_next(&it)))
1906 cpuset_update_task_spread_flag(cs, task);
1907 css_task_iter_end(&it);
Miao Xie950592f2009-06-16 15:31:47 -07001908}
1909
1910/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 * update_flag - read a 0 or a 1 in a file and update associated flag
Paul Menage78608362008-04-29 01:00:26 -07001912 * bit: the bit to update (see cpuset_flagbits_t)
1913 * cs: the cpuset to update
1914 * turning_on: whether the flag is being set or cleared
Paul Jackson053199e2005-10-30 15:02:30 -08001915 *
Tejun Heo5d21cc22013-01-07 08:51:08 -08001916 * Call with cpuset_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
1918
Paul Menage700fe1a2008-04-29 01:00:00 -07001919static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
1920 int turning_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Li Zefan645fcc92009-01-07 18:08:43 -08001922 struct cpuset *trialcs;
Rakib Mullick40b6a762008-10-18 20:28:18 -07001923 int balance_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001924 int spread_flag_changed;
Miao Xie950592f2009-06-16 15:31:47 -07001925 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Li Zefan645fcc92009-01-07 18:08:43 -08001927 trialcs = alloc_trial_cpuset(cs);
1928 if (!trialcs)
1929 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Li Zefan645fcc92009-01-07 18:08:43 -08001931 if (turning_on)
1932 set_bit(bit, &trialcs->flags);
1933 else
1934 clear_bit(bit, &trialcs->flags);
1935
1936 err = validate_change(cs, trialcs);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001937 if (err < 0)
Li Zefan645fcc92009-01-07 18:08:43 -08001938 goto out;
Paul Jackson029190c2007-10-18 23:40:20 -07001939
Paul Jackson029190c2007-10-18 23:40:20 -07001940 balance_flag_changed = (is_sched_load_balance(cs) !=
Li Zefan645fcc92009-01-07 18:08:43 -08001941 is_sched_load_balance(trialcs));
Paul Jackson029190c2007-10-18 23:40:20 -07001942
Miao Xie950592f2009-06-16 15:31:47 -07001943 spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
1944 || (is_spread_page(cs) != is_spread_page(trialcs)));
1945
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001946 spin_lock_irq(&callback_lock);
Li Zefan645fcc92009-01-07 18:08:43 -08001947 cs->flags = trialcs->flags;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04001948 spin_unlock_irq(&callback_lock);
Dinakar Guniguntala85d7b942005-06-25 14:57:34 -07001949
Li Zefan300ed6c2009-01-07 18:08:44 -08001950 if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed)
Tejun Heo699140b2013-01-07 08:51:07 -08001951 rebuild_sched_domains_locked();
Paul Jackson029190c2007-10-18 23:40:20 -07001952
Miao Xie950592f2009-06-16 15:31:47 -07001953 if (spread_flag_changed)
Tejun Heod66393e2014-02-13 06:58:40 -05001954 update_tasks_flags(cs);
Li Zefan645fcc92009-01-07 18:08:43 -08001955out:
Waiman Longbf923702018-11-08 10:08:37 -05001956 free_cpuset(trialcs);
Li Zefan645fcc92009-01-07 18:08:43 -08001957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Paul Jackson053199e2005-10-30 15:02:30 -08001960/*
Waiman Longee8dde02018-11-08 10:08:38 -05001961 * update_prstate - update partititon_root_state
Waiman Long0f3adb82021-07-20 10:18:26 -04001962 * cs: the cpuset to update
1963 * new_prs: new partition root state
Waiman Longee8dde02018-11-08 10:08:38 -05001964 *
1965 * Call with cpuset_mutex held.
1966 */
Waiman Long0f3adb82021-07-20 10:18:26 -04001967static int update_prstate(struct cpuset *cs, int new_prs)
Waiman Longee8dde02018-11-08 10:08:38 -05001968{
Waiman Long6ba34d32021-07-20 10:18:28 -04001969 int err, old_prs = cs->partition_root_state;
Waiman Longee8dde02018-11-08 10:08:38 -05001970 struct cpuset *parent = parent_cs(cs);
Waiman Long0f3adb82021-07-20 10:18:26 -04001971 struct tmpmasks tmpmask;
Waiman Longee8dde02018-11-08 10:08:38 -05001972
Waiman Long6ba34d32021-07-20 10:18:28 -04001973 if (old_prs == new_prs)
Waiman Longee8dde02018-11-08 10:08:38 -05001974 return 0;
1975
1976 /*
Waiman Long3881b862018-11-08 10:08:39 -05001977 * Cannot force a partial or invalid partition root to a full
Waiman Longee8dde02018-11-08 10:08:38 -05001978 * partition root.
1979 */
Waiman Long6ba34d32021-07-20 10:18:28 -04001980 if (new_prs && (old_prs == PRS_ERROR))
Waiman Longee8dde02018-11-08 10:08:38 -05001981 return -EINVAL;
1982
Waiman Long0f3adb82021-07-20 10:18:26 -04001983 if (alloc_cpumasks(NULL, &tmpmask))
Waiman Longee8dde02018-11-08 10:08:38 -05001984 return -ENOMEM;
1985
1986 err = -EINVAL;
Waiman Long6ba34d32021-07-20 10:18:28 -04001987 if (!old_prs) {
Waiman Longee8dde02018-11-08 10:08:38 -05001988 /*
1989 * Turning on partition root requires setting the
1990 * CS_CPU_EXCLUSIVE bit implicitly as well and cpus_allowed
1991 * cannot be NULL.
1992 */
1993 if (cpumask_empty(cs->cpus_allowed))
1994 goto out;
1995
1996 err = update_flag(CS_CPU_EXCLUSIVE, cs, 1);
1997 if (err)
1998 goto out;
1999
2000 err = update_parent_subparts_cpumask(cs, partcmd_enable,
Waiman Long0f3adb82021-07-20 10:18:26 -04002001 NULL, &tmpmask);
Waiman Longee8dde02018-11-08 10:08:38 -05002002 if (err) {
2003 update_flag(CS_CPU_EXCLUSIVE, cs, 0);
2004 goto out;
2005 }
Waiman Longee8dde02018-11-08 10:08:38 -05002006 } else {
Waiman Long3881b862018-11-08 10:08:39 -05002007 /*
2008 * Turning off partition root will clear the
2009 * CS_CPU_EXCLUSIVE bit.
2010 */
Waiman Long6ba34d32021-07-20 10:18:28 -04002011 if (old_prs == PRS_ERROR) {
Waiman Long3881b862018-11-08 10:08:39 -05002012 update_flag(CS_CPU_EXCLUSIVE, cs, 0);
2013 err = 0;
2014 goto out;
2015 }
2016
Waiman Longee8dde02018-11-08 10:08:38 -05002017 err = update_parent_subparts_cpumask(cs, partcmd_disable,
Waiman Long0f3adb82021-07-20 10:18:26 -04002018 NULL, &tmpmask);
Waiman Longee8dde02018-11-08 10:08:38 -05002019 if (err)
2020 goto out;
2021
Waiman Longee8dde02018-11-08 10:08:38 -05002022 /* Turning off CS_CPU_EXCLUSIVE will not return error */
2023 update_flag(CS_CPU_EXCLUSIVE, cs, 0);
2024 }
2025
2026 /*
2027 * Update cpumask of parent's tasks except when it is the top
2028 * cpuset as some system daemons cannot be mapped to other CPUs.
2029 */
2030 if (parent != &top_cpuset)
2031 update_tasks_cpumask(parent);
2032
Waiman Long47169092018-11-08 10:08:40 -05002033 if (parent->child_ecpus_count)
Waiman Long0f3adb82021-07-20 10:18:26 -04002034 update_sibling_cpumasks(parent, cs, &tmpmask);
Waiman Long47169092018-11-08 10:08:40 -05002035
Waiman Longee8dde02018-11-08 10:08:38 -05002036 rebuild_sched_domains_locked();
2037out:
Waiman Long6ba34d32021-07-20 10:18:28 -04002038 if (!err) {
2039 spin_lock_irq(&callback_lock);
2040 cs->partition_root_state = new_prs;
2041 spin_unlock_irq(&callback_lock);
Waiman Longe7cc9882021-08-10 23:06:02 -04002042 notify_partition_change(cs, old_prs, new_prs);
Waiman Long6ba34d32021-07-20 10:18:28 -04002043 }
2044
Waiman Long0f3adb82021-07-20 10:18:26 -04002045 free_cpumasks(NULL, &tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return err;
2047}
2048
Paul Jackson053199e2005-10-30 15:02:30 -08002049/*
Adrian Bunk80f72282006-06-30 18:27:16 +02002050 * Frequency meter - How fast is some event occurring?
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002051 *
2052 * These routines manage a digitally filtered, constant time based,
2053 * event frequency meter. There are four routines:
2054 * fmeter_init() - initialize a frequency meter.
2055 * fmeter_markevent() - called each time the event happens.
2056 * fmeter_getrate() - returns the recent rate of such events.
2057 * fmeter_update() - internal routine used to update fmeter.
2058 *
2059 * A common data structure is passed to each of these routines,
2060 * which is used to keep track of the state required to manage the
2061 * frequency meter and its digital filter.
2062 *
2063 * The filter works on the number of events marked per unit time.
2064 * The filter is single-pole low-pass recursive (IIR). The time unit
2065 * is 1 second. Arithmetic is done using 32-bit integers scaled to
2066 * simulate 3 decimal digits of precision (multiplied by 1000).
2067 *
2068 * With an FM_COEF of 933, and a time base of 1 second, the filter
2069 * has a half-life of 10 seconds, meaning that if the events quit
2070 * happening, then the rate returned from the fmeter_getrate()
2071 * will be cut in half each 10 seconds, until it converges to zero.
2072 *
2073 * It is not worth doing a real infinitely recursive filter. If more
2074 * than FM_MAXTICKS ticks have elapsed since the last filter event,
2075 * just compute FM_MAXTICKS ticks worth, by which point the level
2076 * will be stable.
2077 *
2078 * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
2079 * arithmetic overflow in the fmeter_update() routine.
2080 *
2081 * Given the simple 32 bit integer arithmetic used, this meter works
2082 * best for reporting rates between one per millisecond (msec) and
2083 * one per 32 (approx) seconds. At constant rates faster than one
2084 * per msec it maxes out at values just under 1,000,000. At constant
2085 * rates between one per msec, and one per second it will stabilize
2086 * to a value N*1000, where N is the rate of events per second.
2087 * At constant rates between one per second and one per 32 seconds,
2088 * it will be choppy, moving up on the seconds that have an event,
2089 * and then decaying until the next event. At rates slower than
2090 * about one in 32 seconds, it decays all the way back to zero between
2091 * each event.
2092 */
2093
2094#define FM_COEF 933 /* coefficient for half-life of 10 secs */
Arnd Bergmannd2b436582015-11-25 16:16:55 +01002095#define FM_MAXTICKS ((u32)99) /* useless computing more ticks than this */
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002096#define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
2097#define FM_SCALE 1000 /* faux fixed point scale */
2098
2099/* Initialize a frequency meter */
2100static void fmeter_init(struct fmeter *fmp)
2101{
2102 fmp->cnt = 0;
2103 fmp->val = 0;
2104 fmp->time = 0;
2105 spin_lock_init(&fmp->lock);
2106}
2107
2108/* Internal meter update - process cnt events and update value */
2109static void fmeter_update(struct fmeter *fmp)
2110{
Arnd Bergmannd2b436582015-11-25 16:16:55 +01002111 time64_t now;
2112 u32 ticks;
2113
2114 now = ktime_get_seconds();
2115 ticks = now - fmp->time;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002116
2117 if (ticks == 0)
2118 return;
2119
2120 ticks = min(FM_MAXTICKS, ticks);
2121 while (ticks-- > 0)
2122 fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
2123 fmp->time = now;
2124
2125 fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
2126 fmp->cnt = 0;
2127}
2128
2129/* Process any previous ticks, then bump cnt by one (times scale). */
2130static void fmeter_markevent(struct fmeter *fmp)
2131{
2132 spin_lock(&fmp->lock);
2133 fmeter_update(fmp);
2134 fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
2135 spin_unlock(&fmp->lock);
2136}
2137
2138/* Process any previous ticks, then return current value. */
2139static int fmeter_getrate(struct fmeter *fmp)
2140{
2141 int val;
2142
2143 spin_lock(&fmp->lock);
2144 fmeter_update(fmp);
2145 val = fmp->val;
2146 spin_unlock(&fmp->lock);
2147 return val;
2148}
2149
Tejun Heo57fce0a2014-02-13 06:58:41 -05002150static struct cpuset *cpuset_attach_old_cs;
2151
Tejun Heo5d21cc22013-01-07 08:51:08 -08002152/* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002153static int cpuset_can_attach(struct cgroup_taskset *tset)
Ben Blumf780bdb2011-05-26 16:25:19 -07002154{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002155 struct cgroup_subsys_state *css;
2156 struct cpuset *cs;
Tejun Heobb9d97b2011-12-12 18:12:21 -08002157 struct task_struct *task;
2158 int ret;
Ben Blumf780bdb2011-05-26 16:25:19 -07002159
Tejun Heo57fce0a2014-02-13 06:58:41 -05002160 /* used later by cpuset_attach() */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002161 cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
2162 cs = css_cs(css);
Tejun Heo57fce0a2014-02-13 06:58:41 -05002163
Juri Lelli1243dc52019-07-19 15:59:57 +02002164 percpu_down_write(&cpuset_rwsem);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002165
Tejun Heoaa6ec292014-07-09 10:08:08 -04002166 /* allow moving tasks into an empty cpuset if on default hierarchy */
Tejun Heo5d21cc22013-01-07 08:51:08 -08002167 ret = -ENOSPC;
Waiman Longb8d1b8e2017-08-17 15:33:10 -04002168 if (!is_in_v2_mode() &&
Li Zefan88fa5232013-06-09 17:16:46 +08002169 (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)))
Tejun Heo5d21cc22013-01-07 08:51:08 -08002170 goto out_unlock;
Ben Blumbe367d02009-09-23 15:56:31 -07002171
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002172 cgroup_taskset_for_each(task, css, tset) {
Juri Lelli7f514122014-09-19 10:22:40 +01002173 ret = task_can_attach(task, cs->cpus_allowed);
2174 if (ret)
Tejun Heo5d21cc22013-01-07 08:51:08 -08002175 goto out_unlock;
2176 ret = security_task_setscheduler(task);
2177 if (ret)
2178 goto out_unlock;
Tejun Heobb9d97b2011-12-12 18:12:21 -08002179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Tejun Heo452477f2013-01-07 08:51:07 -08002181 /*
2182 * Mark attach is in progress. This makes validate_change() fail
2183 * changes which zero cpus/mems_allowed.
2184 */
2185 cs->attach_in_progress++;
Tejun Heo5d21cc22013-01-07 08:51:08 -08002186 ret = 0;
2187out_unlock:
Juri Lelli1243dc52019-07-19 15:59:57 +02002188 percpu_up_write(&cpuset_rwsem);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002189 return ret;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002190}
2191
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002192static void cpuset_cancel_attach(struct cgroup_taskset *tset)
Tejun Heo452477f2013-01-07 08:51:07 -08002193{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002194 struct cgroup_subsys_state *css;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002195
2196 cgroup_taskset_first(tset, &css);
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002197
Juri Lelli1243dc52019-07-19 15:59:57 +02002198 percpu_down_write(&cpuset_rwsem);
Tejun Heoeb954192013-08-08 20:11:23 -04002199 css_cs(css)->attach_in_progress--;
Juri Lelli1243dc52019-07-19 15:59:57 +02002200 percpu_up_write(&cpuset_rwsem);
Tejun Heo452477f2013-01-07 08:51:07 -08002201}
2202
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002203/*
Tejun Heo5d21cc22013-01-07 08:51:08 -08002204 * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach()
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002205 * but we can't allocate it dynamically there. Define it global and
2206 * allocate from cpuset_init().
2207 */
2208static cpumask_var_t cpus_attach;
2209
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002210static void cpuset_attach(struct cgroup_taskset *tset)
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002211{
Li Zefan67bd2c52013-06-05 17:15:35 +08002212 /* static buf protected by cpuset_mutex */
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002213 static nodemask_t cpuset_attach_nodemask_to;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002214 struct task_struct *task;
Tejun Heo4530edd2015-09-11 15:00:19 -04002215 struct task_struct *leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002216 struct cgroup_subsys_state *css;
2217 struct cpuset *cs;
Tejun Heo57fce0a2014-02-13 06:58:41 -05002218 struct cpuset *oldcs = cpuset_attach_old_cs;
Tejun Heo4e4c9a12013-01-07 08:51:07 -08002219
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002220 cgroup_taskset_first(tset, &css);
2221 cs = css_cs(css);
2222
Juri Lelli1243dc52019-07-19 15:59:57 +02002223 percpu_down_write(&cpuset_rwsem);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002224
Tejun Heo94196f52011-12-12 18:12:22 -08002225 /* prepare for attach */
Ben Blumf780bdb2011-05-26 16:25:19 -07002226 if (cs == &top_cpuset)
2227 cpumask_copy(cpus_attach, cpu_possible_mask);
2228 else
Li Zefanae1c8022014-07-09 16:48:32 +08002229 guarantee_online_cpus(cs, cpus_attach);
Ben Blumf780bdb2011-05-26 16:25:19 -07002230
Li Zefanae1c8022014-07-09 16:48:32 +08002231 guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
Tejun Heo94196f52011-12-12 18:12:22 -08002232
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002233 cgroup_taskset_for_each(task, css, tset) {
Tejun Heobb9d97b2011-12-12 18:12:21 -08002234 /*
2235 * can_attach beforehand should guarantee that this doesn't
2236 * fail. TODO: have a better way to handle failure here
2237 */
2238 WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
2239
2240 cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
2241 cpuset_update_task_spread_flag(cs, task);
2242 }
David Quigley22fb52d2006-06-23 02:04:00 -07002243
Ben Blumf780bdb2011-05-26 16:25:19 -07002244 /*
Tejun Heo4530edd2015-09-11 15:00:19 -04002245 * Change mm for all threadgroup leaders. This is expensive and may
2246 * sleep and should be moved outside migration path proper.
Ben Blumf780bdb2011-05-26 16:25:19 -07002247 */
Li Zefanae1c8022014-07-09 16:48:32 +08002248 cpuset_attach_nodemask_to = cs->effective_mems;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05002249 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo3df9ca02015-09-11 15:00:18 -04002250 struct mm_struct *mm = get_task_mm(leader);
Li Zefanf047cec2013-06-13 15:11:44 +08002251
Tejun Heo3df9ca02015-09-11 15:00:18 -04002252 if (mm) {
2253 mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
2254
2255 /*
2256 * old_mems_allowed is the same with mems_allowed
2257 * here, except if this task is being moved
2258 * automatically due to hotplug. In that case
2259 * @mems_allowed has been updated and is empty, so
2260 * @old_mems_allowed is the right nodesets that we
2261 * migrate mm from.
2262 */
Tejun Heoe93ad192016-01-19 12:18:41 -05002263 if (is_memory_migrate(cs))
Tejun Heo3df9ca02015-09-11 15:00:18 -04002264 cpuset_migrate_mm(mm, &oldcs->old_mems_allowed,
2265 &cpuset_attach_nodemask_to);
Tejun Heoe93ad192016-01-19 12:18:41 -05002266 else
2267 mmput(mm);
Li Zefanf047cec2013-06-13 15:11:44 +08002268 }
Paul Jackson42253992006-01-08 01:01:59 -08002269 }
Tejun Heo452477f2013-01-07 08:51:07 -08002270
Li Zefan33ad8012013-06-09 17:15:08 +08002271 cs->old_mems_allowed = cpuset_attach_nodemask_to;
Tejun Heo02bb5862013-01-07 08:51:08 -08002272
Tejun Heo452477f2013-01-07 08:51:07 -08002273 cs->attach_in_progress--;
Li Zefane44193d2013-06-09 17:14:22 +08002274 if (!cs->attach_in_progress)
2275 wake_up(&cpuset_attach_wq);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002276
Juri Lelli1243dc52019-07-19 15:59:57 +02002277 percpu_up_write(&cpuset_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/* The various types of files and directories in a cpuset file system */
2281
2282typedef enum {
Paul Jackson45b07ef2006-01-08 01:00:56 -08002283 FILE_MEMORY_MIGRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 FILE_CPULIST,
2285 FILE_MEMLIST,
Li Zefanafd1a8b2014-07-09 16:49:25 +08002286 FILE_EFFECTIVE_CPULIST,
2287 FILE_EFFECTIVE_MEMLIST,
Waiman Long5cf81142018-11-08 10:08:46 -05002288 FILE_SUBPARTS_CPULIST,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 FILE_CPU_EXCLUSIVE,
2290 FILE_MEM_EXCLUSIVE,
Paul Menage78608362008-04-29 01:00:26 -07002291 FILE_MEM_HARDWALL,
Paul Jackson029190c2007-10-18 23:40:20 -07002292 FILE_SCHED_LOAD_BALANCE,
Waiman Longee8dde02018-11-08 10:08:38 -05002293 FILE_PARTITION_ROOT,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002294 FILE_SCHED_RELAX_DOMAIN_LEVEL,
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002295 FILE_MEMORY_PRESSURE_ENABLED,
2296 FILE_MEMORY_PRESSURE,
Paul Jackson825a46a2006-03-24 03:16:03 -08002297 FILE_SPREAD_PAGE,
2298 FILE_SPREAD_SLAB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299} cpuset_filetype_t;
2300
Tejun Heo182446d2013-08-08 20:11:24 -04002301static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
2302 u64 val)
Paul Menage700fe1a2008-04-29 01:00:00 -07002303{
Tejun Heo182446d2013-08-08 20:11:24 -04002304 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07002305 cpuset_filetype_t type = cft->private;
Li Zefana903f082013-08-13 10:05:59 +08002306 int retval = 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07002307
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002308 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02002309 percpu_down_write(&cpuset_rwsem);
Li Zefana903f082013-08-13 10:05:59 +08002310 if (!is_cpuset_online(cs)) {
2311 retval = -ENODEV;
Tejun Heo5d21cc22013-01-07 08:51:08 -08002312 goto out_unlock;
Li Zefana903f082013-08-13 10:05:59 +08002313 }
Paul Menage700fe1a2008-04-29 01:00:00 -07002314
2315 switch (type) {
2316 case FILE_CPU_EXCLUSIVE:
2317 retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
2318 break;
2319 case FILE_MEM_EXCLUSIVE:
2320 retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
2321 break;
Paul Menage78608362008-04-29 01:00:26 -07002322 case FILE_MEM_HARDWALL:
2323 retval = update_flag(CS_MEM_HARDWALL, cs, val);
2324 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07002325 case FILE_SCHED_LOAD_BALANCE:
2326 retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
2327 break;
2328 case FILE_MEMORY_MIGRATE:
2329 retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
2330 break;
2331 case FILE_MEMORY_PRESSURE_ENABLED:
2332 cpuset_memory_pressure_enabled = !!val;
2333 break;
Paul Menage700fe1a2008-04-29 01:00:00 -07002334 case FILE_SPREAD_PAGE:
2335 retval = update_flag(CS_SPREAD_PAGE, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07002336 break;
2337 case FILE_SPREAD_SLAB:
2338 retval = update_flag(CS_SPREAD_SLAB, cs, val);
Paul Menage700fe1a2008-04-29 01:00:00 -07002339 break;
2340 default:
2341 retval = -EINVAL;
2342 break;
2343 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08002344out_unlock:
Juri Lelli1243dc52019-07-19 15:59:57 +02002345 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002346 cpus_read_unlock();
Paul Menage700fe1a2008-04-29 01:00:00 -07002347 return retval;
2348}
2349
Tejun Heo182446d2013-08-08 20:11:24 -04002350static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
2351 s64 val)
Paul Menage5be7a472008-05-06 20:42:41 -07002352{
Tejun Heo182446d2013-08-08 20:11:24 -04002353 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07002354 cpuset_filetype_t type = cft->private;
Tejun Heo5d21cc22013-01-07 08:51:08 -08002355 int retval = -ENODEV;
Paul Menage5be7a472008-05-06 20:42:41 -07002356
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002357 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02002358 percpu_down_write(&cpuset_rwsem);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002359 if (!is_cpuset_online(cs))
2360 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07002361
Paul Menage5be7a472008-05-06 20:42:41 -07002362 switch (type) {
2363 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
2364 retval = update_relax_domain_level(cs, val);
2365 break;
2366 default:
2367 retval = -EINVAL;
2368 break;
2369 }
Tejun Heo5d21cc22013-01-07 08:51:08 -08002370out_unlock:
Juri Lelli1243dc52019-07-19 15:59:57 +02002371 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002372 cpus_read_unlock();
Paul Menage5be7a472008-05-06 20:42:41 -07002373 return retval;
2374}
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376/*
Paul Menagee3712392008-07-25 01:47:02 -07002377 * Common handling for a write to a "cpus" or "mems" file.
2378 */
Tejun Heo451af502014-05-13 12:16:21 -04002379static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
2380 char *buf, size_t nbytes, loff_t off)
Paul Menagee3712392008-07-25 01:47:02 -07002381{
Tejun Heo451af502014-05-13 12:16:21 -04002382 struct cpuset *cs = css_cs(of_css(of));
Li Zefan645fcc92009-01-07 18:08:43 -08002383 struct cpuset *trialcs;
Tejun Heo5d21cc22013-01-07 08:51:08 -08002384 int retval = -ENODEV;
Paul Menagee3712392008-07-25 01:47:02 -07002385
Tejun Heo451af502014-05-13 12:16:21 -04002386 buf = strstrip(buf);
2387
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002388 /*
2389 * CPU or memory hotunplug may leave @cs w/o any execution
2390 * resources, in which case the hotplug code asynchronously updates
2391 * configuration and transfers all tasks to the nearest ancestor
2392 * which can execute.
2393 *
2394 * As writes to "cpus" or "mems" may restore @cs's execution
2395 * resources, wait for the previously scheduled operations before
2396 * proceeding, so that we don't end up keep removing tasks added
2397 * after execution capability is restored.
Tejun Heo76bb5ab2014-06-30 15:47:32 -04002398 *
2399 * cpuset_hotplug_work calls back into cgroup core via
2400 * cgroup_transfer_tasks() and waiting for it from a cgroupfs
2401 * operation like this one can lead to a deadlock through kernfs
2402 * active_ref protection. Let's break the protection. Losing the
2403 * protection is okay as we check whether @cs is online after
2404 * grabbing cpuset_mutex anyway. This only happens on the legacy
2405 * hierarchies.
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002406 */
Tejun Heo76bb5ab2014-06-30 15:47:32 -04002407 css_get(&cs->css);
2408 kernfs_break_active_protection(of->kn);
Tejun Heo3a5a6d02013-01-07 08:51:07 -08002409 flush_work(&cpuset_hotplug_work);
2410
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002411 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02002412 percpu_down_write(&cpuset_rwsem);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002413 if (!is_cpuset_online(cs))
2414 goto out_unlock;
Paul Menagee3712392008-07-25 01:47:02 -07002415
Li Zefan645fcc92009-01-07 18:08:43 -08002416 trialcs = alloc_trial_cpuset(cs);
Li Zefanb75f38d2011-03-04 17:36:21 -08002417 if (!trialcs) {
2418 retval = -ENOMEM;
Tejun Heo5d21cc22013-01-07 08:51:08 -08002419 goto out_unlock;
Li Zefanb75f38d2011-03-04 17:36:21 -08002420 }
Li Zefan645fcc92009-01-07 18:08:43 -08002421
Tejun Heo451af502014-05-13 12:16:21 -04002422 switch (of_cft(of)->private) {
Paul Menagee3712392008-07-25 01:47:02 -07002423 case FILE_CPULIST:
Li Zefan645fcc92009-01-07 18:08:43 -08002424 retval = update_cpumask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07002425 break;
2426 case FILE_MEMLIST:
Li Zefan645fcc92009-01-07 18:08:43 -08002427 retval = update_nodemask(cs, trialcs, buf);
Paul Menagee3712392008-07-25 01:47:02 -07002428 break;
2429 default:
2430 retval = -EINVAL;
2431 break;
2432 }
Li Zefan645fcc92009-01-07 18:08:43 -08002433
Waiman Longbf923702018-11-08 10:08:37 -05002434 free_cpuset(trialcs);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002435out_unlock:
Juri Lelli1243dc52019-07-19 15:59:57 +02002436 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002437 cpus_read_unlock();
Tejun Heo76bb5ab2014-06-30 15:47:32 -04002438 kernfs_unbreak_active_protection(of->kn);
2439 css_put(&cs->css);
Tejun Heoe93ad192016-01-19 12:18:41 -05002440 flush_workqueue(cpuset_migrate_mm_wq);
Tejun Heo451af502014-05-13 12:16:21 -04002441 return retval ?: nbytes;
Paul Menagee3712392008-07-25 01:47:02 -07002442}
2443
2444/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 * These ascii lists should be read in a single call, by using a user
2446 * buffer large enough to hold the entire map. If read in smaller
2447 * chunks, there is no guarantee of atomicity. Since the display format
2448 * used, list of ranges of sequential numbers, is variable length,
2449 * and since these maps can change value dynamically, one could read
2450 * gibberish by doing partial reads while a list was changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 */
Tejun Heo2da8ca82013-12-05 12:28:04 -05002452static int cpuset_common_seq_show(struct seq_file *sf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002454 struct cpuset *cs = css_cs(seq_css(sf));
2455 cpuset_filetype_t type = seq_cft(sf)->private;
Tejun Heo51ffe412013-12-05 12:28:02 -05002456 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002458 spin_lock_irq(&callback_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 switch (type) {
2461 case FILE_CPULIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08002462 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 break;
2464 case FILE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08002465 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 break;
Li Zefanafd1a8b2014-07-09 16:49:25 +08002467 case FILE_EFFECTIVE_CPULIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08002468 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus));
Li Zefanafd1a8b2014-07-09 16:49:25 +08002469 break;
2470 case FILE_EFFECTIVE_MEMLIST:
Tejun Heoe8e6d972015-02-13 14:37:23 -08002471 seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems));
Li Zefanafd1a8b2014-07-09 16:49:25 +08002472 break;
Waiman Long5cf81142018-11-08 10:08:46 -05002473 case FILE_SUBPARTS_CPULIST:
2474 seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->subparts_cpus));
2475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 default:
Tejun Heo51ffe412013-12-05 12:28:02 -05002477 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002480 spin_unlock_irq(&callback_lock);
Tejun Heo51ffe412013-12-05 12:28:02 -05002481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Tejun Heo182446d2013-08-08 20:11:24 -04002484static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage700fe1a2008-04-29 01:00:00 -07002485{
Tejun Heo182446d2013-08-08 20:11:24 -04002486 struct cpuset *cs = css_cs(css);
Paul Menage700fe1a2008-04-29 01:00:00 -07002487 cpuset_filetype_t type = cft->private;
2488 switch (type) {
2489 case FILE_CPU_EXCLUSIVE:
2490 return is_cpu_exclusive(cs);
2491 case FILE_MEM_EXCLUSIVE:
2492 return is_mem_exclusive(cs);
Paul Menage78608362008-04-29 01:00:26 -07002493 case FILE_MEM_HARDWALL:
2494 return is_mem_hardwall(cs);
Paul Menage700fe1a2008-04-29 01:00:00 -07002495 case FILE_SCHED_LOAD_BALANCE:
2496 return is_sched_load_balance(cs);
2497 case FILE_MEMORY_MIGRATE:
2498 return is_memory_migrate(cs);
2499 case FILE_MEMORY_PRESSURE_ENABLED:
2500 return cpuset_memory_pressure_enabled;
2501 case FILE_MEMORY_PRESSURE:
2502 return fmeter_getrate(&cs->fmeter);
2503 case FILE_SPREAD_PAGE:
2504 return is_spread_page(cs);
2505 case FILE_SPREAD_SLAB:
2506 return is_spread_slab(cs);
2507 default:
2508 BUG();
2509 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002510
2511 /* Unreachable but makes gcc happy */
2512 return 0;
Paul Menage700fe1a2008-04-29 01:00:00 -07002513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Tejun Heo182446d2013-08-08 20:11:24 -04002515static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menage5be7a472008-05-06 20:42:41 -07002516{
Tejun Heo182446d2013-08-08 20:11:24 -04002517 struct cpuset *cs = css_cs(css);
Paul Menage5be7a472008-05-06 20:42:41 -07002518 cpuset_filetype_t type = cft->private;
2519 switch (type) {
2520 case FILE_SCHED_RELAX_DOMAIN_LEVEL:
2521 return cs->relax_domain_level;
2522 default:
2523 BUG();
2524 }
Max Krasnyanskycf417142008-08-11 14:33:53 -07002525
Lu Jialind95af612021-04-08 16:03:46 +08002526 /* Unreachable but makes gcc happy */
Max Krasnyanskycf417142008-08-11 14:33:53 -07002527 return 0;
Paul Menage5be7a472008-05-06 20:42:41 -07002528}
2529
Waiman Longbb5b5532018-11-08 10:08:44 -05002530static int sched_partition_show(struct seq_file *seq, void *v)
2531{
2532 struct cpuset *cs = css_cs(seq_css(seq));
2533
2534 switch (cs->partition_root_state) {
2535 case PRS_ENABLED:
2536 seq_puts(seq, "root\n");
2537 break;
2538 case PRS_DISABLED:
2539 seq_puts(seq, "member\n");
2540 break;
2541 case PRS_ERROR:
2542 seq_puts(seq, "root invalid\n");
2543 break;
2544 }
2545 return 0;
2546}
2547
2548static ssize_t sched_partition_write(struct kernfs_open_file *of, char *buf,
2549 size_t nbytes, loff_t off)
2550{
2551 struct cpuset *cs = css_cs(of_css(of));
2552 int val;
2553 int retval = -ENODEV;
2554
2555 buf = strstrip(buf);
2556
2557 /*
Tejun Heob1e3aeb2018-11-13 12:03:33 -08002558 * Convert "root" to ENABLED, and convert "member" to DISABLED.
Waiman Longbb5b5532018-11-08 10:08:44 -05002559 */
Tejun Heob1e3aeb2018-11-13 12:03:33 -08002560 if (!strcmp(buf, "root"))
Waiman Longbb5b5532018-11-08 10:08:44 -05002561 val = PRS_ENABLED;
Tejun Heob1e3aeb2018-11-13 12:03:33 -08002562 else if (!strcmp(buf, "member"))
Waiman Longbb5b5532018-11-08 10:08:44 -05002563 val = PRS_DISABLED;
2564 else
2565 return -EINVAL;
2566
2567 css_get(&cs->css);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002568 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02002569 percpu_down_write(&cpuset_rwsem);
Waiman Longbb5b5532018-11-08 10:08:44 -05002570 if (!is_cpuset_online(cs))
2571 goto out_unlock;
2572
2573 retval = update_prstate(cs, val);
2574out_unlock:
Juri Lelli1243dc52019-07-19 15:59:57 +02002575 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002576 cpus_read_unlock();
Waiman Longbb5b5532018-11-08 10:08:44 -05002577 css_put(&cs->css);
2578 return retval ?: nbytes;
2579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581/*
2582 * for the common functions, 'private' gives the type of file
2583 */
2584
Waiman Long4ec22e92018-11-08 10:08:35 -05002585static struct cftype legacy_files[] = {
Paul Menageaddf2c72008-04-29 01:00:26 -07002586 {
2587 .name = "cpus",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002588 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04002589 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07002590 .max_write_len = (100U + 6 * NR_CPUS),
Paul Menageaddf2c72008-04-29 01:00:26 -07002591 .private = FILE_CPULIST,
2592 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Paul Menageaddf2c72008-04-29 01:00:26 -07002594 {
2595 .name = "mems",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002596 .seq_show = cpuset_common_seq_show,
Tejun Heo451af502014-05-13 12:16:21 -04002597 .write = cpuset_write_resmask,
Paul Menagee3712392008-07-25 01:47:02 -07002598 .max_write_len = (100U + 6 * MAX_NUMNODES),
Paul Menageaddf2c72008-04-29 01:00:26 -07002599 .private = FILE_MEMLIST,
2600 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Paul Menageaddf2c72008-04-29 01:00:26 -07002602 {
Li Zefanafd1a8b2014-07-09 16:49:25 +08002603 .name = "effective_cpus",
2604 .seq_show = cpuset_common_seq_show,
2605 .private = FILE_EFFECTIVE_CPULIST,
2606 },
2607
2608 {
2609 .name = "effective_mems",
2610 .seq_show = cpuset_common_seq_show,
2611 .private = FILE_EFFECTIVE_MEMLIST,
2612 },
2613
2614 {
Paul Menageaddf2c72008-04-29 01:00:26 -07002615 .name = "cpu_exclusive",
2616 .read_u64 = cpuset_read_u64,
2617 .write_u64 = cpuset_write_u64,
2618 .private = FILE_CPU_EXCLUSIVE,
2619 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Paul Menageaddf2c72008-04-29 01:00:26 -07002621 {
2622 .name = "mem_exclusive",
2623 .read_u64 = cpuset_read_u64,
2624 .write_u64 = cpuset_write_u64,
2625 .private = FILE_MEM_EXCLUSIVE,
2626 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Paul Menageaddf2c72008-04-29 01:00:26 -07002628 {
Paul Menage78608362008-04-29 01:00:26 -07002629 .name = "mem_hardwall",
2630 .read_u64 = cpuset_read_u64,
2631 .write_u64 = cpuset_write_u64,
2632 .private = FILE_MEM_HARDWALL,
2633 },
2634
2635 {
Paul Menageaddf2c72008-04-29 01:00:26 -07002636 .name = "sched_load_balance",
2637 .read_u64 = cpuset_read_u64,
2638 .write_u64 = cpuset_write_u64,
2639 .private = FILE_SCHED_LOAD_BALANCE,
2640 },
Paul Jackson029190c2007-10-18 23:40:20 -07002641
Paul Menageaddf2c72008-04-29 01:00:26 -07002642 {
2643 .name = "sched_relax_domain_level",
Paul Menage5be7a472008-05-06 20:42:41 -07002644 .read_s64 = cpuset_read_s64,
2645 .write_s64 = cpuset_write_s64,
Paul Menageaddf2c72008-04-29 01:00:26 -07002646 .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
2647 },
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002648
Paul Menageaddf2c72008-04-29 01:00:26 -07002649 {
2650 .name = "memory_migrate",
2651 .read_u64 = cpuset_read_u64,
2652 .write_u64 = cpuset_write_u64,
2653 .private = FILE_MEMORY_MIGRATE,
2654 },
2655
2656 {
2657 .name = "memory_pressure",
2658 .read_u64 = cpuset_read_u64,
Waiman Long1c08c222017-08-24 12:04:29 -04002659 .private = FILE_MEMORY_PRESSURE,
Paul Menageaddf2c72008-04-29 01:00:26 -07002660 },
2661
2662 {
2663 .name = "memory_spread_page",
2664 .read_u64 = cpuset_read_u64,
2665 .write_u64 = cpuset_write_u64,
2666 .private = FILE_SPREAD_PAGE,
2667 },
2668
2669 {
2670 .name = "memory_spread_slab",
2671 .read_u64 = cpuset_read_u64,
2672 .write_u64 = cpuset_write_u64,
2673 .private = FILE_SPREAD_SLAB,
2674 },
Tejun Heo4baf6e32012-04-01 12:09:55 -07002675
2676 {
2677 .name = "memory_pressure_enabled",
2678 .flags = CFTYPE_ONLY_ON_ROOT,
2679 .read_u64 = cpuset_read_u64,
2680 .write_u64 = cpuset_write_u64,
2681 .private = FILE_MEMORY_PRESSURE_ENABLED,
2682 },
2683
2684 { } /* terminate */
Paul Jackson45b07ef2006-01-08 01:00:56 -08002685};
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687/*
Waiman Long4ec22e92018-11-08 10:08:35 -05002688 * This is currently a minimal set for the default hierarchy. It can be
2689 * expanded later on by migrating more features and control files from v1.
2690 */
2691static struct cftype dfl_files[] = {
2692 {
2693 .name = "cpus",
2694 .seq_show = cpuset_common_seq_show,
2695 .write = cpuset_write_resmask,
2696 .max_write_len = (100U + 6 * NR_CPUS),
2697 .private = FILE_CPULIST,
2698 .flags = CFTYPE_NOT_ON_ROOT,
2699 },
2700
2701 {
2702 .name = "mems",
2703 .seq_show = cpuset_common_seq_show,
2704 .write = cpuset_write_resmask,
2705 .max_write_len = (100U + 6 * MAX_NUMNODES),
2706 .private = FILE_MEMLIST,
2707 .flags = CFTYPE_NOT_ON_ROOT,
2708 },
2709
2710 {
2711 .name = "cpus.effective",
2712 .seq_show = cpuset_common_seq_show,
2713 .private = FILE_EFFECTIVE_CPULIST,
Waiman Long4ec22e92018-11-08 10:08:35 -05002714 },
2715
2716 {
2717 .name = "mems.effective",
2718 .seq_show = cpuset_common_seq_show,
2719 .private = FILE_EFFECTIVE_MEMLIST,
Waiman Long4ec22e92018-11-08 10:08:35 -05002720 },
2721
Waiman Longee8dde02018-11-08 10:08:38 -05002722 {
Tejun Heob1e3aeb2018-11-13 12:03:33 -08002723 .name = "cpus.partition",
Waiman Longbb5b5532018-11-08 10:08:44 -05002724 .seq_show = sched_partition_show,
2725 .write = sched_partition_write,
Waiman Longee8dde02018-11-08 10:08:38 -05002726 .private = FILE_PARTITION_ROOT,
2727 .flags = CFTYPE_NOT_ON_ROOT,
Waiman Longe7cc9882021-08-10 23:06:02 -04002728 .file_offset = offsetof(struct cpuset, partition_file),
Waiman Longee8dde02018-11-08 10:08:38 -05002729 },
2730
Waiman Long5cf81142018-11-08 10:08:46 -05002731 {
2732 .name = "cpus.subpartitions",
2733 .seq_show = cpuset_common_seq_show,
2734 .private = FILE_SUBPARTS_CPULIST,
2735 .flags = CFTYPE_DEBUG,
2736 },
2737
Waiman Long4ec22e92018-11-08 10:08:35 -05002738 { } /* terminate */
2739};
2740
2741
2742/*
Tejun Heo92fb9742012-11-19 08:13:38 -08002743 * cpuset_css_alloc - allocate a cpuset css
Li Zefanc9e5fe62013-06-14 11:18:27 +08002744 * cgrp: control group that the new cpuset will be part of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 */
2746
Tejun Heoeb954192013-08-08 20:11:23 -04002747static struct cgroup_subsys_state *
2748cpuset_css_alloc(struct cgroup_subsys_state *parent_css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Tejun Heoc8f699b2013-01-07 08:51:07 -08002750 struct cpuset *cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Tejun Heoeb954192013-08-08 20:11:23 -04002752 if (!parent_css)
Paul Menage8793d852007-10-18 23:39:39 -07002753 return &top_cpuset.css;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002754
Tejun Heoc8f699b2013-01-07 08:51:07 -08002755 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (!cs)
Paul Menage8793d852007-10-18 23:39:39 -07002757 return ERR_PTR(-ENOMEM);
Waiman Longbf923702018-11-08 10:08:37 -05002758
2759 if (alloc_cpumasks(cs, NULL)) {
2760 kfree(cs);
2761 return ERR_PTR(-ENOMEM);
2762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Waiman Longee9707e2021-08-11 15:57:07 -04002764 __set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002765 nodes_clear(cs->mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002766 nodes_clear(cs->effective_mems);
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002767 fmeter_init(&cs->fmeter);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002768 cs->relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Waiman Longee9707e2021-08-11 15:57:07 -04002770 /* Set CS_MEMORY_MIGRATE for default hierarchy */
2771 if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys))
2772 __set_bit(CS_MEMORY_MIGRATE, &cs->flags);
2773
Tejun Heoc8f699b2013-01-07 08:51:07 -08002774 return &cs->css;
2775}
2776
Tejun Heoeb954192013-08-08 20:11:23 -04002777static int cpuset_css_online(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08002778{
Tejun Heoeb954192013-08-08 20:11:23 -04002779 struct cpuset *cs = css_cs(css);
Tejun Heoc4310692013-01-07 08:51:08 -08002780 struct cpuset *parent = parent_cs(cs);
Tejun Heoae8086c2013-01-07 08:51:07 -08002781 struct cpuset *tmp_cs;
Tejun Heo492eb212013-08-08 20:11:25 -04002782 struct cgroup_subsys_state *pos_css;
Tejun Heoc8f699b2013-01-07 08:51:07 -08002783
2784 if (!parent)
2785 return 0;
2786
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002787 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02002788 percpu_down_write(&cpuset_rwsem);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002789
Tejun Heoefeb77b2013-01-07 08:51:07 -08002790 set_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002791 if (is_spread_page(parent))
2792 set_bit(CS_SPREAD_PAGE, &cs->flags);
2793 if (is_spread_slab(parent))
2794 set_bit(CS_SPREAD_SLAB, &cs->flags);
2795
Mel Gorman664eedd2014-06-04 16:10:08 -07002796 cpuset_inc();
Tejun Heo033fa1c2012-11-19 08:13:39 -08002797
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002798 spin_lock_irq(&callback_lock);
Waiman Longb8d1b8e2017-08-17 15:33:10 -04002799 if (is_in_v2_mode()) {
Li Zefane2b9a3d2014-07-09 16:47:03 +08002800 cpumask_copy(cs->effective_cpus, parent->effective_cpus);
2801 cs->effective_mems = parent->effective_mems;
Waiman Long47169092018-11-08 10:08:40 -05002802 cs->use_parent_ecpus = true;
2803 parent->child_ecpus_count++;
Li Zefane2b9a3d2014-07-09 16:47:03 +08002804 }
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002805 spin_unlock_irq(&callback_lock);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002806
Tejun Heoeb954192013-08-08 20:11:23 -04002807 if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
Tejun Heo5d21cc22013-01-07 08:51:08 -08002808 goto out_unlock;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002809
2810 /*
2811 * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
2812 * set. This flag handling is implemented in cgroup core for
2813 * histrical reasons - the flag may be specified during mount.
2814 *
2815 * Currently, if any sibling cpusets have exclusive cpus or mem, we
2816 * refuse to clone the configuration - thereby refusing the task to
2817 * be entered, and as a result refusing the sys_unshare() or
2818 * clone() which initiated it. If this becomes a problem for some
2819 * users who wish to allow that scenario, then this could be
2820 * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
2821 * (and likewise for mems) to the new cgroup.
2822 */
Tejun Heoae8086c2013-01-07 08:51:07 -08002823 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002824 cpuset_for_each_child(tmp_cs, pos_css, parent) {
Tejun Heoae8086c2013-01-07 08:51:07 -08002825 if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
2826 rcu_read_unlock();
Tejun Heo5d21cc22013-01-07 08:51:08 -08002827 goto out_unlock;
Tejun Heoae8086c2013-01-07 08:51:07 -08002828 }
Tejun Heo033fa1c2012-11-19 08:13:39 -08002829 }
Tejun Heoae8086c2013-01-07 08:51:07 -08002830 rcu_read_unlock();
Tejun Heo033fa1c2012-11-19 08:13:39 -08002831
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002832 spin_lock_irq(&callback_lock);
Tejun Heo033fa1c2012-11-19 08:13:39 -08002833 cs->mems_allowed = parent->mems_allowed;
Zefan Li790317e2015-02-13 11:19:49 +08002834 cs->effective_mems = parent->mems_allowed;
Tejun Heo033fa1c2012-11-19 08:13:39 -08002835 cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
Zefan Li790317e2015-02-13 11:19:49 +08002836 cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
Dan Carpentercea74462014-10-27 16:27:02 +03002837 spin_unlock_irq(&callback_lock);
Tejun Heo5d21cc22013-01-07 08:51:08 -08002838out_unlock:
Juri Lelli1243dc52019-07-19 15:59:57 +02002839 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002840 cpus_read_unlock();
Tejun Heoc8f699b2013-01-07 08:51:07 -08002841 return 0;
2842}
2843
Zhao Hongjiang0b9e6962013-07-27 11:56:53 +08002844/*
2845 * If the cpuset being removed has its flag 'sched_load_balance'
2846 * enabled, then simulate turning sched_load_balance off, which
Waiman Longee8dde02018-11-08 10:08:38 -05002847 * will call rebuild_sched_domains_locked(). That is not needed
2848 * in the default hierarchy where only changes in partition
2849 * will cause repartitioning.
2850 *
2851 * If the cpuset has the 'sched.partition' flag enabled, simulate
2852 * turning 'sched.partition" off.
Zhao Hongjiang0b9e6962013-07-27 11:56:53 +08002853 */
2854
Tejun Heoeb954192013-08-08 20:11:23 -04002855static void cpuset_css_offline(struct cgroup_subsys_state *css)
Tejun Heoc8f699b2013-01-07 08:51:07 -08002856{
Tejun Heoeb954192013-08-08 20:11:23 -04002857 struct cpuset *cs = css_cs(css);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002858
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002859 cpus_read_lock();
Juri Lelli1243dc52019-07-19 15:59:57 +02002860 percpu_down_write(&cpuset_rwsem);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002861
Waiman Longee8dde02018-11-08 10:08:38 -05002862 if (is_partition_root(cs))
2863 update_prstate(cs, 0);
2864
2865 if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
2866 is_sched_load_balance(cs))
Tejun Heoc8f699b2013-01-07 08:51:07 -08002867 update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
2868
Waiman Long47169092018-11-08 10:08:40 -05002869 if (cs->use_parent_ecpus) {
2870 struct cpuset *parent = parent_cs(cs);
2871
2872 cs->use_parent_ecpus = false;
2873 parent->child_ecpus_count--;
2874 }
2875
Mel Gorman664eedd2014-06-04 16:10:08 -07002876 cpuset_dec();
Tejun Heoefeb77b2013-01-07 08:51:07 -08002877 clear_bit(CS_ONLINE, &cs->flags);
Tejun Heoc8f699b2013-01-07 08:51:07 -08002878
Juri Lelli1243dc52019-07-19 15:59:57 +02002879 percpu_up_write(&cpuset_rwsem);
Sebastian Andrzej Siewiorc5c63b92021-08-03 16:16:07 +02002880 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
Tejun Heoeb954192013-08-08 20:11:23 -04002883static void cpuset_css_free(struct cgroup_subsys_state *css)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Tejun Heoeb954192013-08-08 20:11:23 -04002885 struct cpuset *cs = css_cs(css);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Waiman Longbf923702018-11-08 10:08:37 -05002887 free_cpuset(cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Li Zefan39bd0d12014-07-09 16:48:01 +08002890static void cpuset_bind(struct cgroup_subsys_state *root_css)
2891{
Juri Lelli1243dc52019-07-19 15:59:57 +02002892 percpu_down_write(&cpuset_rwsem);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002893 spin_lock_irq(&callback_lock);
Li Zefan39bd0d12014-07-09 16:48:01 +08002894
Waiman Longb8d1b8e2017-08-17 15:33:10 -04002895 if (is_in_v2_mode()) {
Li Zefan39bd0d12014-07-09 16:48:01 +08002896 cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
2897 top_cpuset.mems_allowed = node_possible_map;
2898 } else {
2899 cpumask_copy(top_cpuset.cpus_allowed,
2900 top_cpuset.effective_cpus);
2901 top_cpuset.mems_allowed = top_cpuset.effective_mems;
2902 }
2903
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04002904 spin_unlock_irq(&callback_lock);
Juri Lelli1243dc52019-07-19 15:59:57 +02002905 percpu_up_write(&cpuset_rwsem);
Li Zefan39bd0d12014-07-09 16:48:01 +08002906}
2907
Zefan Li06f4e942016-08-09 11:25:01 +08002908/*
2909 * Make sure the new task conform to the current state of its parent,
2910 * which could have been changed by cpuset just after it inherits the
2911 * state from the parent and before it sits on the cgroup's task list.
2912 */
Wei Yongjun8a15b812016-09-16 13:02:37 +00002913static void cpuset_fork(struct task_struct *task)
Zefan Li06f4e942016-08-09 11:25:01 +08002914{
2915 if (task_css_is_root(task, cpuset_cgrp_id))
2916 return;
2917
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002918 set_cpus_allowed_ptr(task, current->cpus_ptr);
Zefan Li06f4e942016-08-09 11:25:01 +08002919 task->mems_allowed = current->mems_allowed;
2920}
2921
Tejun Heo073219e2014-02-08 10:36:58 -05002922struct cgroup_subsys cpuset_cgrp_subsys = {
Li Zefan39bd0d12014-07-09 16:48:01 +08002923 .css_alloc = cpuset_css_alloc,
2924 .css_online = cpuset_css_online,
2925 .css_offline = cpuset_css_offline,
2926 .css_free = cpuset_css_free,
2927 .can_attach = cpuset_can_attach,
2928 .cancel_attach = cpuset_cancel_attach,
2929 .attach = cpuset_attach,
Tejun Heo5cf1cac2016-04-21 19:06:48 -04002930 .post_attach = cpuset_post_attach,
Li Zefan39bd0d12014-07-09 16:48:01 +08002931 .bind = cpuset_bind,
Zefan Li06f4e942016-08-09 11:25:01 +08002932 .fork = cpuset_fork,
Waiman Long4ec22e92018-11-08 10:08:35 -05002933 .legacy_cftypes = legacy_files,
2934 .dfl_cftypes = dfl_files,
Tejun Heob38e42e2016-02-23 10:00:50 -05002935 .early_init = true,
Waiman Long4ec22e92018-11-08 10:08:35 -05002936 .threaded = true,
Paul Menage8793d852007-10-18 23:39:39 -07002937};
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939/**
2940 * cpuset_init - initialize cpusets at system boot
2941 *
Al Virod5f68d32019-05-13 12:33:22 -04002942 * Description: Initialize top_cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 **/
2944
2945int __init cpuset_init(void)
2946{
Juri Lelli1243dc52019-07-19 15:59:57 +02002947 BUG_ON(percpu_init_rwsem(&cpuset_rwsem));
2948
Nicholas Mc Guire75fa8e52017-03-26 18:24:06 +02002949 BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL));
2950 BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL));
Waiman Longbf923702018-11-08 10:08:37 -05002951 BUG_ON(!zalloc_cpumask_var(&top_cpuset.subparts_cpus, GFP_KERNEL));
Miao Xie58568d22009-06-16 15:31:49 -07002952
Li Zefan300ed6c2009-01-07 18:08:44 -08002953 cpumask_setall(top_cpuset.cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07002954 nodes_setall(top_cpuset.mems_allowed);
Li Zefane2b9a3d2014-07-09 16:47:03 +08002955 cpumask_setall(top_cpuset.effective_cpus);
2956 nodes_setall(top_cpuset.effective_mems);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Paul Jackson3e0d98b2006-01-08 01:01:49 -08002958 fmeter_init(&top_cpuset.fmeter);
Paul Jackson029190c2007-10-18 23:40:20 -07002959 set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09002960 top_cpuset.relax_domain_level = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Nicholas Mc Guire75fa8e52017-03-26 18:24:06 +02002962 BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL));
Li Zefan2341d1b2009-01-07 18:08:42 -08002963
Paul Menage8793d852007-10-18 23:39:39 -07002964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002967/*
Max Krasnyanskycf417142008-08-11 14:33:53 -07002968 * If CPU and/or memory hotplug handlers, below, unplug any CPUs
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002969 * or memory nodes, we need to walk over the cpuset hierarchy,
2970 * removing that CPU or node from all cpusets. If this removes the
Cliff Wickman956db3c2008-02-07 00:14:43 -08002971 * last CPU or node from a cpuset, then move the tasks in the empty
2972 * cpuset to its next-highest non-empty parent.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002973 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08002974static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002975{
Cliff Wickman956db3c2008-02-07 00:14:43 -08002976 struct cpuset *parent;
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07002977
Paul Jacksonc8d9c902008-02-07 00:14:46 -08002978 /*
Cliff Wickman956db3c2008-02-07 00:14:43 -08002979 * Find its next-highest non-empty parent, (top cpuset
2980 * has online cpus, so can't be empty).
2981 */
Tejun Heoc4310692013-01-07 08:51:08 -08002982 parent = parent_cs(cs);
Li Zefan300ed6c2009-01-07 18:08:44 -08002983 while (cpumask_empty(parent->cpus_allowed) ||
Paul Jacksonb4501292008-02-07 00:14:47 -08002984 nodes_empty(parent->mems_allowed))
Tejun Heoc4310692013-01-07 08:51:08 -08002985 parent = parent_cs(parent);
Cliff Wickman956db3c2008-02-07 00:14:43 -08002986
Tejun Heo8cc99342013-04-07 09:29:50 -07002987 if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) {
Fabian Frederick12d30892014-05-05 19:49:00 +02002988 pr_err("cpuset: failed to transfer tasks out of empty cpuset ");
Tejun Heoe61734c2014-02-12 09:29:50 -05002989 pr_cont_cgroup_name(cs->css.cgroup);
2990 pr_cont("\n");
Tejun Heo8cc99342013-04-07 09:29:50 -07002991 }
Cliff Wickman956db3c2008-02-07 00:14:43 -08002992}
2993
Li Zefanbe4c9dd2014-07-09 16:49:04 +08002994static void
2995hotplug_update_tasks_legacy(struct cpuset *cs,
2996 struct cpumask *new_cpus, nodemask_t *new_mems,
2997 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08002998{
2999 bool is_empty;
3000
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003001 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003002 cpumask_copy(cs->cpus_allowed, new_cpus);
3003 cpumask_copy(cs->effective_cpus, new_cpus);
3004 cs->mems_allowed = *new_mems;
3005 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003006 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08003007
3008 /*
3009 * Don't call update_tasks_cpumask() if the cpuset becomes empty,
3010 * as the tasks will be migratecd to an ancestor.
3011 */
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003012 if (cpus_updated && !cpumask_empty(cs->cpus_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08003013 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003014 if (mems_updated && !nodes_empty(cs->mems_allowed))
Li Zefan390a36a2014-07-09 16:48:54 +08003015 update_tasks_nodemask(cs);
3016
3017 is_empty = cpumask_empty(cs->cpus_allowed) ||
3018 nodes_empty(cs->mems_allowed);
3019
Juri Lelli1243dc52019-07-19 15:59:57 +02003020 percpu_up_write(&cpuset_rwsem);
Li Zefan390a36a2014-07-09 16:48:54 +08003021
3022 /*
3023 * Move tasks to the nearest ancestor with execution resources,
3024 * This is full cgroup operation which will also call back into
3025 * cpuset. Should be done outside any lock.
3026 */
3027 if (is_empty)
3028 remove_tasks_in_empty_cpuset(cs);
3029
Juri Lelli1243dc52019-07-19 15:59:57 +02003030 percpu_down_write(&cpuset_rwsem);
Li Zefan390a36a2014-07-09 16:48:54 +08003031}
3032
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003033static void
3034hotplug_update_tasks(struct cpuset *cs,
3035 struct cpumask *new_cpus, nodemask_t *new_mems,
3036 bool cpus_updated, bool mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08003037{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003038 if (cpumask_empty(new_cpus))
3039 cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus);
3040 if (nodes_empty(*new_mems))
3041 *new_mems = parent_cs(cs)->effective_mems;
Li Zefan390a36a2014-07-09 16:48:54 +08003042
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003043 spin_lock_irq(&callback_lock);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003044 cpumask_copy(cs->effective_cpus, new_cpus);
3045 cs->effective_mems = *new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003046 spin_unlock_irq(&callback_lock);
Li Zefan390a36a2014-07-09 16:48:54 +08003047
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003048 if (cpus_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08003049 update_tasks_cpumask(cs);
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003050 if (mems_updated)
Li Zefan390a36a2014-07-09 16:48:54 +08003051 update_tasks_nodemask(cs);
3052}
3053
Waiman Long4b842da2018-11-08 10:08:41 -05003054static bool force_rebuild;
3055
3056void cpuset_force_rebuild(void)
3057{
3058 force_rebuild = true;
3059}
3060
Tejun Heodeb7aa32013-01-07 08:51:07 -08003061/**
Li Zefan388afd82013-06-09 17:14:47 +08003062 * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
Tejun Heodeb7aa32013-01-07 08:51:07 -08003063 * @cs: cpuset in interest
Waiman Long4b842da2018-11-08 10:08:41 -05003064 * @tmp: the tmpmasks structure pointer
Cliff Wickman956db3c2008-02-07 00:14:43 -08003065 *
Tejun Heodeb7aa32013-01-07 08:51:07 -08003066 * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
3067 * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
3068 * all its tasks are moved to the nearest ancestor with both resources.
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07003069 */
Waiman Long4b842da2018-11-08 10:08:41 -05003070static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07003071{
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003072 static cpumask_t new_cpus;
3073 static nodemask_t new_mems;
3074 bool cpus_updated;
3075 bool mems_updated;
Waiman Long4b842da2018-11-08 10:08:41 -05003076 struct cpuset *parent;
Li Zefane44193d2013-06-09 17:14:22 +08003077retry:
3078 wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07003079
Juri Lelli1243dc52019-07-19 15:59:57 +02003080 percpu_down_write(&cpuset_rwsem);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07003081
Li Zefane44193d2013-06-09 17:14:22 +08003082 /*
3083 * We have raced with task attaching. We wait until attaching
3084 * is finished, so we won't attach a task to an empty cpuset.
3085 */
3086 if (cs->attach_in_progress) {
Juri Lelli1243dc52019-07-19 15:59:57 +02003087 percpu_up_write(&cpuset_rwsem);
Li Zefane44193d2013-06-09 17:14:22 +08003088 goto retry;
3089 }
3090
Waiman Long0f3adb82021-07-20 10:18:26 -04003091 parent = parent_cs(cs);
Waiman Long4b842da2018-11-08 10:08:41 -05003092 compute_effective_cpumask(&new_cpus, cs, parent);
3093 nodes_and(new_mems, cs->mems_allowed, parent->effective_mems);
Paul Jacksonb4501292008-02-07 00:14:47 -08003094
Waiman Long4b842da2018-11-08 10:08:41 -05003095 if (cs->nr_subparts_cpus)
3096 /*
3097 * Make sure that CPUs allocated to child partitions
3098 * do not show up in effective_cpus.
3099 */
3100 cpumask_andnot(&new_cpus, &new_cpus, cs->subparts_cpus);
3101
3102 if (!tmp || !cs->partition_root_state)
3103 goto update_tasks;
3104
3105 /*
3106 * In the unlikely event that a partition root has empty
3107 * effective_cpus or its parent becomes erroneous, we have to
3108 * transition it to the erroneous state.
3109 */
3110 if (is_partition_root(cs) && (cpumask_empty(&new_cpus) ||
3111 (parent->partition_root_state == PRS_ERROR))) {
3112 if (cs->nr_subparts_cpus) {
Waiman Long6ba34d32021-07-20 10:18:28 -04003113 spin_lock_irq(&callback_lock);
Waiman Long4b842da2018-11-08 10:08:41 -05003114 cs->nr_subparts_cpus = 0;
3115 cpumask_clear(cs->subparts_cpus);
Waiman Long6ba34d32021-07-20 10:18:28 -04003116 spin_unlock_irq(&callback_lock);
Waiman Long4b842da2018-11-08 10:08:41 -05003117 compute_effective_cpumask(&new_cpus, cs, parent);
3118 }
3119
3120 /*
3121 * If the effective_cpus is empty because the child
3122 * partitions take away all the CPUs, we can keep
3123 * the current partition and let the child partitions
3124 * fight for available CPUs.
3125 */
3126 if ((parent->partition_root_state == PRS_ERROR) ||
3127 cpumask_empty(&new_cpus)) {
Waiman Longe7cc9882021-08-10 23:06:02 -04003128 int old_prs;
3129
Waiman Long4b842da2018-11-08 10:08:41 -05003130 update_parent_subparts_cpumask(cs, partcmd_disable,
3131 NULL, tmp);
Waiman Longe7cc9882021-08-10 23:06:02 -04003132 old_prs = cs->partition_root_state;
3133 if (old_prs != PRS_ERROR) {
3134 spin_lock_irq(&callback_lock);
3135 cs->partition_root_state = PRS_ERROR;
3136 spin_unlock_irq(&callback_lock);
3137 notify_partition_change(cs, old_prs, PRS_ERROR);
3138 }
Waiman Long4b842da2018-11-08 10:08:41 -05003139 }
3140 cpuset_force_rebuild();
3141 }
3142
3143 /*
3144 * On the other hand, an erroneous partition root may be transitioned
3145 * back to a regular one or a partition root with no CPU allocated
3146 * from the parent may change to erroneous.
3147 */
3148 if (is_partition_root(parent) &&
3149 ((cs->partition_root_state == PRS_ERROR) ||
3150 !cpumask_intersects(&new_cpus, parent->subparts_cpus)) &&
3151 update_parent_subparts_cpumask(cs, partcmd_update, NULL, tmp))
3152 cpuset_force_rebuild();
3153
3154update_tasks:
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003155 cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
3156 mems_updated = !nodes_equal(new_mems, cs->effective_mems);
Cliff Wickman956db3c2008-02-07 00:14:43 -08003157
Waiman Longb8d1b8e2017-08-17 15:33:10 -04003158 if (is_in_v2_mode())
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003159 hotplug_update_tasks(cs, &new_cpus, &new_mems,
3160 cpus_updated, mems_updated);
Li Zefan390a36a2014-07-09 16:48:54 +08003161 else
Li Zefanbe4c9dd2014-07-09 16:49:04 +08003162 hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems,
3163 cpus_updated, mems_updated);
Tejun Heo8d033942013-01-07 08:51:07 -08003164
Juri Lelli1243dc52019-07-19 15:59:57 +02003165 percpu_up_write(&cpuset_rwsem);
Tejun Heodeb7aa32013-01-07 08:51:07 -08003166}
Paul Jacksonb4501292008-02-07 00:14:47 -08003167
Tejun Heodeb7aa32013-01-07 08:51:07 -08003168/**
Tejun Heo2b729fe2020-04-03 11:32:13 -04003169 * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset
Tejun Heodeb7aa32013-01-07 08:51:07 -08003170 *
3171 * This function is called after either CPU or memory configuration has
3172 * changed and updates cpuset accordingly. The top_cpuset is always
3173 * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
3174 * order to make cpusets transparent (of no affect) on systems that are
3175 * actively using CPU hotplug but making no active use of cpusets.
3176 *
3177 * Non-root cpusets are only affected by offlining. If any CPUs or memory
Li Zefan388afd82013-06-09 17:14:47 +08003178 * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
3179 * all descendants.
Tejun Heodeb7aa32013-01-07 08:51:07 -08003180 *
3181 * Note that CPU offlining during suspend is ignored. We don't modify
3182 * cpusets across suspend/resume cycles at all.
3183 */
Tejun Heo2b729fe2020-04-03 11:32:13 -04003184static void cpuset_hotplug_workfn(struct work_struct *work)
Tejun Heodeb7aa32013-01-07 08:51:07 -08003185{
Li Zefan5c5cc622013-06-09 17:16:29 +08003186 static cpumask_t new_cpus;
3187 static nodemask_t new_mems;
Tejun Heodeb7aa32013-01-07 08:51:07 -08003188 bool cpus_updated, mems_updated;
Waiman Longb8d1b8e2017-08-17 15:33:10 -04003189 bool on_dfl = is_in_v2_mode();
Waiman Long4b842da2018-11-08 10:08:41 -05003190 struct tmpmasks tmp, *ptmp = NULL;
3191
3192 if (on_dfl && !alloc_cpumasks(NULL, &tmp))
3193 ptmp = &tmp;
Cliff Wickman956db3c2008-02-07 00:14:43 -08003194
Juri Lelli1243dc52019-07-19 15:59:57 +02003195 percpu_down_write(&cpuset_rwsem);
Cliff Wickman956db3c2008-02-07 00:14:43 -08003196
Tejun Heodeb7aa32013-01-07 08:51:07 -08003197 /* fetch the available cpus/mems and find out which changed how */
3198 cpumask_copy(&new_cpus, cpu_active_mask);
3199 new_mems = node_states[N_MEMORY];
Cliff Wickman956db3c2008-02-07 00:14:43 -08003200
Waiman Long4b842da2018-11-08 10:08:41 -05003201 /*
3202 * If subparts_cpus is populated, it is likely that the check below
3203 * will produce a false positive on cpus_updated when the cpu list
3204 * isn't changed. It is extra work, but it is better to be safe.
3205 */
Li Zefan7e882912014-07-09 16:48:42 +08003206 cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus);
3207 mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05303208
Waiman Long15d428e2021-07-20 10:18:27 -04003209 /*
3210 * In the rare case that hotplug removes all the cpus in subparts_cpus,
3211 * we assumed that cpus are updated.
3212 */
3213 if (!cpus_updated && top_cpuset.nr_subparts_cpus)
3214 cpus_updated = true;
3215
Tejun Heodeb7aa32013-01-07 08:51:07 -08003216 /* synchronize cpus_allowed to cpu_active_mask */
3217 if (cpus_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003218 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08003219 if (!on_dfl)
3220 cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
Waiman Long4b842da2018-11-08 10:08:41 -05003221 /*
3222 * Make sure that CPUs allocated to child partitions
3223 * do not show up in effective_cpus. If no CPU is left,
3224 * we clear the subparts_cpus & let the child partitions
3225 * fight for the CPUs again.
3226 */
3227 if (top_cpuset.nr_subparts_cpus) {
3228 if (cpumask_subset(&new_cpus,
3229 top_cpuset.subparts_cpus)) {
3230 top_cpuset.nr_subparts_cpus = 0;
3231 cpumask_clear(top_cpuset.subparts_cpus);
3232 } else {
3233 cpumask_andnot(&new_cpus, &new_cpus,
3234 top_cpuset.subparts_cpus);
3235 }
3236 }
Li Zefan1344ab92014-07-09 16:47:16 +08003237 cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003238 spin_unlock_irq(&callback_lock);
Tejun Heodeb7aa32013-01-07 08:51:07 -08003239 /* we don't mess with cpumasks of tasks in top_cpuset */
3240 }
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05303241
Tejun Heodeb7aa32013-01-07 08:51:07 -08003242 /* synchronize mems_allowed to N_MEMORY */
3243 if (mems_updated) {
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003244 spin_lock_irq(&callback_lock);
Li Zefan7e882912014-07-09 16:48:42 +08003245 if (!on_dfl)
3246 top_cpuset.mems_allowed = new_mems;
Li Zefan1344ab92014-07-09 16:47:16 +08003247 top_cpuset.effective_mems = new_mems;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003248 spin_unlock_irq(&callback_lock);
Tejun Heod66393e2014-02-13 06:58:40 -05003249 update_tasks_nodemask(&top_cpuset);
Tejun Heodeb7aa32013-01-07 08:51:07 -08003250 }
3251
Juri Lelli1243dc52019-07-19 15:59:57 +02003252 percpu_up_write(&cpuset_rwsem);
Tejun Heodeb7aa32013-01-07 08:51:07 -08003253
Li Zefan5c5cc622013-06-09 17:16:29 +08003254 /* if cpus or mems changed, we need to propagate to descendants */
3255 if (cpus_updated || mems_updated) {
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08003256 struct cpuset *cs;
Tejun Heo492eb212013-08-08 20:11:25 -04003257 struct cgroup_subsys_state *pos_css;
Paul Jacksonb4501292008-02-07 00:14:47 -08003258
Paul Jacksonb4501292008-02-07 00:14:47 -08003259 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04003260 cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
Tejun Heoec903c02014-05-13 12:11:01 -04003261 if (cs == &top_cpuset || !css_tryget_online(&cs->css))
Li Zefan388afd82013-06-09 17:14:47 +08003262 continue;
3263 rcu_read_unlock();
3264
Waiman Long4b842da2018-11-08 10:08:41 -05003265 cpuset_hotplug_update_tasks(cs, ptmp);
Li Zefan388afd82013-06-09 17:14:47 +08003266
3267 rcu_read_lock();
3268 css_put(&cs->css);
3269 }
Tejun Heodeb7aa32013-01-07 08:51:07 -08003270 rcu_read_unlock();
3271 }
Tejun Heo8d033942013-01-07 08:51:07 -08003272
Tejun Heodeb7aa32013-01-07 08:51:07 -08003273 /* rebuild sched domains if cpus_allowed has changed */
Peter Zijlstra50e76632017-09-07 11:13:38 +02003274 if (cpus_updated || force_rebuild) {
3275 force_rebuild = false;
Tejun Heo2b729fe2020-04-03 11:32:13 -04003276 rebuild_sched_domains();
Peter Zijlstra50e76632017-09-07 11:13:38 +02003277 }
Waiman Long4b842da2018-11-08 10:08:41 -05003278
3279 free_cpumasks(NULL, ptmp);
Paul Jacksonb1aac8b2006-09-29 02:01:17 -07003280}
3281
Prateek Sooda49e4622020-01-24 20:37:29 +05303282void cpuset_update_active_cpus(void)
Peter Zijlstra50e76632017-09-07 11:13:38 +02003283{
Tejun Heo2b729fe2020-04-03 11:32:13 -04003284 /*
3285 * We're inside cpu hotplug critical region which usually nests
3286 * inside cgroup synchronization. Bounce actual hotplug processing
3287 * to a work item to avoid reverse locking order.
3288 */
3289 schedule_work(&cpuset_hotplug_work);
3290}
3291
3292void cpuset_wait_for_hotplug(void)
3293{
3294 flush_work(&cpuset_hotplug_work);
Peter Zijlstra50e76632017-09-07 11:13:38 +02003295}
3296
Paul Jackson38837fc2006-09-29 02:01:16 -07003297/*
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08003298 * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
3299 * Call this routine anytime after node_states[N_MEMORY] changes.
Srivatsa S. Bhata1cd2b12012-05-24 19:47:03 +05303300 * See cpuset_update_active_cpus() for CPU hotplug handling.
Paul Jackson38837fc2006-09-29 02:01:16 -07003301 */
Miao Xief4818912008-11-19 15:36:30 -08003302static int cpuset_track_online_nodes(struct notifier_block *self,
3303 unsigned long action, void *arg)
Paul Jackson38837fc2006-09-29 02:01:16 -07003304{
Tejun Heo3a5a6d02013-01-07 08:51:07 -08003305 schedule_work(&cpuset_hotplug_work);
Miao Xief4818912008-11-19 15:36:30 -08003306 return NOTIFY_OK;
Paul Jackson38837fc2006-09-29 02:01:16 -07003307}
Andrew Mortond8f10cb2013-04-29 15:08:08 -07003308
3309static struct notifier_block cpuset_track_online_nodes_nb = {
3310 .notifier_call = cpuset_track_online_nodes,
3311 .priority = 10, /* ??! */
3312};
Paul Jackson38837fc2006-09-29 02:01:16 -07003313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314/**
3315 * cpuset_init_smp - initialize cpus_allowed
3316 *
3317 * Description: Finish top cpuset after cpu, node maps are initialized
Andrew Mortond8f10cb2013-04-29 15:08:08 -07003318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319void __init cpuset_init_smp(void)
3320{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01003321 cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08003322 top_cpuset.mems_allowed = node_states[N_MEMORY];
Li Zefan33ad8012013-06-09 17:15:08 +08003323 top_cpuset.old_mems_allowed = top_cpuset.mems_allowed;
Paul Jackson4c4d50f2006-08-27 01:23:51 -07003324
Li Zefane2b9a3d2014-07-09 16:47:03 +08003325 cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask);
3326 top_cpuset.effective_mems = node_states[N_MEMORY];
3327
Andrew Mortond8f10cb2013-04-29 15:08:08 -07003328 register_hotmemory_notifier(&cpuset_track_online_nodes_nb);
Tejun Heoe93ad192016-01-19 12:18:41 -05003329
3330 cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0);
3331 BUG_ON(!cpuset_migrate_mm_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
3336 * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
Li Zefan6af866a2009-01-07 18:08:45 -08003337 * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 *
Li Zefan300ed6c2009-01-07 18:08:44 -08003339 * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 * attached to the specified @tsk. Guaranteed to return some non-empty
Rusty Russell5f054e32012-03-29 15:38:31 +10303341 * subset of cpu_online_mask, even if this means going outside the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 * tasks cpuset.
3343 **/
3344
Li Zefan6af866a2009-01-07 18:08:45 -08003345void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003347 unsigned long flags;
3348
3349 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05003350 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08003351 guarantee_online_cpus(task_cs(tsk), pmask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05003352 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003353 spin_unlock_irqrestore(&callback_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354}
3355
Joel Savitzd477f8c2019-06-12 11:50:48 -04003356/**
3357 * cpuset_cpus_allowed_fallback - final fallback before complete catastrophe.
3358 * @tsk: pointer to task_struct with which the scheduler is struggling
3359 *
3360 * Description: In the case that the scheduler cannot find an allowed cpu in
3361 * tsk->cpus_allowed, we fall back to task_cs(tsk)->cpus_allowed. In legacy
3362 * mode however, this value is the same as task_cs(tsk)->effective_cpus,
3363 * which will not contain a sane cpumask during cases such as cpu hotplugging.
3364 * This is the absolute last resort for the scheduler and it is only used if
3365 * _every_ other avenue has been traveled.
3366 **/
3367
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01003368void cpuset_cpus_allowed_fallback(struct task_struct *tsk)
Oleg Nesterov9084bb82010-03-15 10:10:27 +01003369{
Oleg Nesterov9084bb82010-03-15 10:10:27 +01003370 rcu_read_lock();
Joel Savitzd477f8c2019-06-12 11:50:48 -04003371 do_set_cpus_allowed(tsk, is_in_v2_mode() ?
3372 task_cs(tsk)->cpus_allowed : cpu_possible_mask);
Oleg Nesterov9084bb82010-03-15 10:10:27 +01003373 rcu_read_unlock();
3374
3375 /*
3376 * We own tsk->cpus_allowed, nobody can change it under us.
3377 *
3378 * But we used cs && cs->cpus_allowed lockless and thus can
3379 * race with cgroup_attach_task() or update_cpumask() and get
3380 * the wrong tsk->cpus_allowed. However, both cases imply the
3381 * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
3382 * which takes task_rq_lock().
3383 *
3384 * If we are called after it dropped the lock we must see all
3385 * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
3386 * set any mask even if it is not right from task_cs() pov,
3387 * the pending set_cpus_allowed_ptr() will fix things.
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01003388 *
3389 * select_fallback_rq() will fix things ups and set cpu_possible_mask
3390 * if required.
Oleg Nesterov9084bb82010-03-15 10:10:27 +01003391 */
Oleg Nesterov9084bb82010-03-15 10:10:27 +01003392}
3393
Rasmus Villemoes8f4ab072015-02-12 15:00:16 -08003394void __init cpuset_init_current_mems_allowed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
Mike Travisf9a86fc2008-04-04 18:11:07 -07003396 nodes_setall(current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07003399/**
Paul Jackson909d75a2006-01-08 01:01:55 -08003400 * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
3401 * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
3402 *
3403 * Description: Returns the nodemask_t mems_allowed of the cpuset
3404 * attached to the specified @tsk. Guaranteed to return some non-empty
Lai Jiangshan38d7bee2012-12-12 13:51:24 -08003405 * subset of node_states[N_MEMORY], even if this means going outside the
Paul Jackson909d75a2006-01-08 01:01:55 -08003406 * tasks cpuset.
3407 **/
3408
3409nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
3410{
3411 nodemask_t mask;
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003412 unsigned long flags;
Paul Jackson909d75a2006-01-08 01:01:55 -08003413
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003414 spin_lock_irqsave(&callback_lock, flags);
Li Zefanb8dadcb2014-03-03 17:28:36 -05003415 rcu_read_lock();
Li Zefanae1c8022014-07-09 16:48:32 +08003416 guarantee_online_mems(task_cs(tsk), &mask);
Li Zefanb8dadcb2014-03-03 17:28:36 -05003417 rcu_read_unlock();
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003418 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson909d75a2006-01-08 01:01:55 -08003419
3420 return mask;
3421}
3422
3423/**
Zhen Lei08b2b6f2021-05-24 16:29:43 +08003424 * cpuset_nodemask_valid_mems_allowed - check nodemask vs. current mems_allowed
Mel Gorman19770b32008-04-28 02:12:18 -07003425 * @nodemask: the nodemask to be checked
Randy Dunlapd9fd8a62005-07-27 11:45:11 -07003426 *
Mel Gorman19770b32008-04-28 02:12:18 -07003427 * Are any of the nodes in the nodemask allowed in current->mems_allowed?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 */
Mel Gorman19770b32008-04-28 02:12:18 -07003429int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Mel Gorman19770b32008-04-28 02:12:18 -07003431 return nodes_intersects(*nodemask, current->mems_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
Paul Jackson9bf22292005-09-06 15:18:12 -07003434/*
Paul Menage78608362008-04-29 01:00:26 -07003435 * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
3436 * mem_hardwall ancestor to the specified cpuset. Call holding
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003437 * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
Paul Menage78608362008-04-29 01:00:26 -07003438 * (an unusual configuration), then returns the root cpuset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 */
Tejun Heoc9710d82013-08-08 20:11:22 -04003440static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Tejun Heoc4310692013-01-07 08:51:08 -08003442 while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs))
3443 cs = parent_cs(cs);
Paul Jackson9bf22292005-09-06 15:18:12 -07003444 return cs;
3445}
3446
3447/**
Vladimir Davydov344736f2014-10-20 15:50:30 +04003448 * cpuset_node_allowed - Can we allocate on a memory node?
David Rientjesa1bc5a42009-04-02 16:57:54 -07003449 * @node: is this an allowed node?
Paul Jackson02a0e532006-12-13 00:34:25 -08003450 * @gfp_mask: memory allocation flags
Paul Jackson9bf22292005-09-06 15:18:12 -07003451 *
David Rientjes6e276d22015-04-14 15:47:01 -07003452 * If we're in interrupt, yes, we can always allocate. If @node is set in
3453 * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
3454 * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
Michal Hockoda99ecf2017-09-06 16:24:53 -07003455 * yes. If current has access to memory reserves as an oom victim, yes.
Paul Jackson9bf22292005-09-06 15:18:12 -07003456 * Otherwise, no.
3457 *
3458 * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
David Rientjesc596d9f2007-05-06 14:49:32 -07003459 * and do not allow allocations outside the current tasks cpuset
Michal Hockoda99ecf2017-09-06 16:24:53 -07003460 * unless the task has been OOM killed.
Paul Jackson9bf22292005-09-06 15:18:12 -07003461 * GFP_KERNEL allocations are not so marked, so can escape to the
Paul Menage78608362008-04-29 01:00:26 -07003462 * nearest enclosing hardwalled ancestor cpuset.
Paul Jackson9bf22292005-09-06 15:18:12 -07003463 *
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003464 * Scanning up parent cpusets requires callback_lock. The
Paul Jackson02a0e532006-12-13 00:34:25 -08003465 * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
3466 * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
3467 * current tasks mems_allowed came up empty on the first pass over
3468 * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003469 * cpuset are short of memory, might require taking the callback_lock.
Paul Jackson9bf22292005-09-06 15:18:12 -07003470 *
Paul Jackson36be57f2006-05-20 15:00:10 -07003471 * The first call here from mm/page_alloc:get_page_from_freelist()
Paul Jackson02a0e532006-12-13 00:34:25 -08003472 * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
3473 * so no allocation on a node outside the cpuset is allowed (unless
3474 * in interrupt, of course).
Paul Jackson9bf22292005-09-06 15:18:12 -07003475 *
Paul Jackson36be57f2006-05-20 15:00:10 -07003476 * The second pass through get_page_from_freelist() doesn't even call
3477 * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
3478 * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
3479 * in alloc_flags. That logic and the checks below have the combined
3480 * affect that:
Paul Jackson9bf22292005-09-06 15:18:12 -07003481 * in_interrupt - any node ok (current task context irrelevant)
3482 * GFP_ATOMIC - any node ok
Michal Hockoda99ecf2017-09-06 16:24:53 -07003483 * tsk_is_oom_victim - any node ok
Paul Menage78608362008-04-29 01:00:26 -07003484 * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
Paul Jackson9bf22292005-09-06 15:18:12 -07003485 * GFP_USER - only nodes in current tasks mems allowed ok.
Paul Jackson02a0e532006-12-13 00:34:25 -08003486 */
Vlastimil Babka002f2902016-05-19 17:14:30 -07003487bool __cpuset_node_allowed(int node, gfp_t gfp_mask)
Paul Jackson9bf22292005-09-06 15:18:12 -07003488{
Tejun Heoc9710d82013-08-08 20:11:22 -04003489 struct cpuset *cs; /* current cpuset ancestors */
Paul Jackson29afd492006-03-24 03:16:12 -08003490 int allowed; /* is allocation in zone z allowed? */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003491 unsigned long flags;
Paul Jackson9bf22292005-09-06 15:18:12 -07003492
David Rientjes6e276d22015-04-14 15:47:01 -07003493 if (in_interrupt())
Vlastimil Babka002f2902016-05-19 17:14:30 -07003494 return true;
Paul Jackson9bf22292005-09-06 15:18:12 -07003495 if (node_isset(node, current->mems_allowed))
Vlastimil Babka002f2902016-05-19 17:14:30 -07003496 return true;
David Rientjesc596d9f2007-05-06 14:49:32 -07003497 /*
3498 * Allow tasks that have access to memory reserves because they have
3499 * been OOM killed to get memory anywhere.
3500 */
Michal Hockoda99ecf2017-09-06 16:24:53 -07003501 if (unlikely(tsk_is_oom_victim(current)))
Vlastimil Babka002f2902016-05-19 17:14:30 -07003502 return true;
Paul Jackson9bf22292005-09-06 15:18:12 -07003503 if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
Vlastimil Babka002f2902016-05-19 17:14:30 -07003504 return false;
Paul Jackson9bf22292005-09-06 15:18:12 -07003505
Bob Picco5563e772005-11-13 16:06:35 -08003506 if (current->flags & PF_EXITING) /* Let dying task have memory */
Vlastimil Babka002f2902016-05-19 17:14:30 -07003507 return true;
Bob Picco5563e772005-11-13 16:06:35 -08003508
Paul Jackson9bf22292005-09-06 15:18:12 -07003509 /* Not hardwall and node outside mems_allowed: scan up cpusets */
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003510 spin_lock_irqsave(&callback_lock, flags);
Paul Jackson053199e2005-10-30 15:02:30 -08003511
Li Zefanb8dadcb2014-03-03 17:28:36 -05003512 rcu_read_lock();
Paul Menage78608362008-04-29 01:00:26 -07003513 cs = nearest_hardwall_ancestor(task_cs(current));
Li Zefan99afb0f2014-02-27 18:19:36 +08003514 allowed = node_isset(node, cs->mems_allowed);
Li Zefanb8dadcb2014-03-03 17:28:36 -05003515 rcu_read_unlock();
Paul Jackson053199e2005-10-30 15:02:30 -08003516
Vladimir Davydov8447a0f2014-10-20 15:50:29 +04003517 spin_unlock_irqrestore(&callback_lock, flags);
Paul Jackson9bf22292005-09-06 15:18:12 -07003518 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Paul Jacksonef08e3b2005-09-06 15:18:13 -07003521/**
Jack Steiner6adef3e2010-05-26 14:42:49 -07003522 * cpuset_mem_spread_node() - On which node to begin search for a file page
3523 * cpuset_slab_spread_node() - On which node to begin search for a slab page
Paul Jackson825a46a2006-03-24 03:16:03 -08003524 *
3525 * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
3526 * tasks in a cpuset with is_spread_page or is_spread_slab set),
3527 * and if the memory allocation used cpuset_mem_spread_node()
3528 * to determine on which node to start looking, as it will for
3529 * certain page cache or slab cache pages such as used for file
3530 * system buffers and inode caches, then instead of starting on the
3531 * local node to look for a free page, rather spread the starting
3532 * node around the tasks mems_allowed nodes.
3533 *
3534 * We don't have to worry about the returned node being offline
3535 * because "it can't happen", and even if it did, it would be ok.
3536 *
3537 * The routines calling guarantee_online_mems() are careful to
3538 * only set nodes in task->mems_allowed that are online. So it
3539 * should not be possible for the following code to return an
3540 * offline node. But if it did, that would be ok, as this routine
3541 * is not returning the node where the allocation must be, only
3542 * the node where the search should start. The zonelist passed to
3543 * __alloc_pages() will include all nodes. If the slab allocator
3544 * is passed an offline node, it will fall back to the local node.
3545 * See kmem_cache_alloc_node().
3546 */
3547
Jack Steiner6adef3e2010-05-26 14:42:49 -07003548static int cpuset_spread_node(int *rotor)
Paul Jackson825a46a2006-03-24 03:16:03 -08003549{
Andrew Morton0edaf862016-05-19 17:10:58 -07003550 return *rotor = next_node_in(*rotor, current->mems_allowed);
Paul Jackson825a46a2006-03-24 03:16:03 -08003551}
Jack Steiner6adef3e2010-05-26 14:42:49 -07003552
3553int cpuset_mem_spread_node(void)
3554{
Michal Hocko778d3b02011-07-26 16:08:30 -07003555 if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
3556 current->cpuset_mem_spread_rotor =
3557 node_random(&current->mems_allowed);
3558
Jack Steiner6adef3e2010-05-26 14:42:49 -07003559 return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
3560}
3561
3562int cpuset_slab_spread_node(void)
3563{
Michal Hocko778d3b02011-07-26 16:08:30 -07003564 if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
3565 current->cpuset_slab_spread_rotor =
3566 node_random(&current->mems_allowed);
3567
Jack Steiner6adef3e2010-05-26 14:42:49 -07003568 return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
3569}
3570
Paul Jackson825a46a2006-03-24 03:16:03 -08003571EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
3572
3573/**
David Rientjesbbe373f2007-10-16 23:25:58 -07003574 * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
3575 * @tsk1: pointer to task_struct of some task.
3576 * @tsk2: pointer to task_struct of some other task.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07003577 *
David Rientjesbbe373f2007-10-16 23:25:58 -07003578 * Description: Return true if @tsk1's mems_allowed intersects the
3579 * mems_allowed of @tsk2. Used by the OOM killer to determine if
3580 * one of the task's memory usage might impact the memory available
3581 * to the other.
Paul Jacksonef08e3b2005-09-06 15:18:13 -07003582 **/
3583
David Rientjesbbe373f2007-10-16 23:25:58 -07003584int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
3585 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -07003586{
David Rientjesbbe373f2007-10-16 23:25:58 -07003587 return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
Paul Jacksonef08e3b2005-09-06 15:18:13 -07003588}
3589
David Rientjes75aa1992009-01-06 14:39:01 -08003590/**
David Rientjesda39da32015-11-05 18:48:05 -08003591 * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
David Rientjes75aa1992009-01-06 14:39:01 -08003592 *
David Rientjesda39da32015-11-05 18:48:05 -08003593 * Description: Prints current's name, cpuset name, and cached copy of its
Li Zefanb8dadcb2014-03-03 17:28:36 -05003594 * mems_allowed to the kernel log.
David Rientjes75aa1992009-01-06 14:39:01 -08003595 */
David Rientjesda39da32015-11-05 18:48:05 -08003596void cpuset_print_current_mems_allowed(void)
David Rientjes75aa1992009-01-06 14:39:01 -08003597{
Li Zefanb8dadcb2014-03-03 17:28:36 -05003598 struct cgroup *cgrp;
David Rientjes75aa1992009-01-06 14:39:01 -08003599
Li Zefanb8dadcb2014-03-03 17:28:36 -05003600 rcu_read_lock();
Li Zefan63f43f52013-01-25 16:08:01 +08003601
David Rientjesda39da32015-11-05 18:48:05 -08003602 cgrp = task_cs(current)->css.cgroup;
yuzhoujianef8444e2018-12-28 00:36:07 -08003603 pr_cont(",cpuset=");
Tejun Heoe61734c2014-02-12 09:29:50 -05003604 pr_cont_cgroup_name(cgrp);
yuzhoujianef8444e2018-12-28 00:36:07 -08003605 pr_cont(",mems_allowed=%*pbl",
David Rientjesda39da32015-11-05 18:48:05 -08003606 nodemask_pr_args(&current->mems_allowed));
Li Zefanf440d982013-03-01 15:02:15 +08003607
Li Zefancfb59662013-03-12 10:28:39 +08003608 rcu_read_unlock();
David Rientjes75aa1992009-01-06 14:39:01 -08003609}
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611/*
Paul Jackson3e0d98b2006-01-08 01:01:49 -08003612 * Collection of memory_pressure is suppressed unless
3613 * this flag is enabled by writing "1" to the special
3614 * cpuset file 'memory_pressure_enabled' in the root cpuset.
3615 */
3616
Paul Jacksonc5b2aff82006-01-08 01:01:51 -08003617int cpuset_memory_pressure_enabled __read_mostly;
Paul Jackson3e0d98b2006-01-08 01:01:49 -08003618
3619/**
3620 * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
3621 *
3622 * Keep a running average of the rate of synchronous (direct)
3623 * page reclaim efforts initiated by tasks in each cpuset.
3624 *
3625 * This represents the rate at which some task in the cpuset
3626 * ran low on memory on all nodes it was allowed to use, and
3627 * had to enter the kernels page reclaim code in an effort to
3628 * create more free memory by tossing clean pages or swapping
3629 * or writing dirty pages.
3630 *
3631 * Display to user space in the per-cpuset read-only file
3632 * "memory_pressure". Value displayed is an integer
3633 * representing the recent rate of entry into the synchronous
3634 * (direct) page reclaim by any task attached to the cpuset.
3635 **/
3636
3637void __cpuset_memory_pressure_bump(void)
3638{
Li Zefanb8dadcb2014-03-03 17:28:36 -05003639 rcu_read_lock();
Paul Menage8793d852007-10-18 23:39:39 -07003640 fmeter_markevent(&task_cs(current)->fmeter);
Li Zefanb8dadcb2014-03-03 17:28:36 -05003641 rcu_read_unlock();
Paul Jackson3e0d98b2006-01-08 01:01:49 -08003642}
3643
Paul Menage8793d852007-10-18 23:39:39 -07003644#ifdef CONFIG_PROC_PID_CPUSET
Paul Jackson3e0d98b2006-01-08 01:01:49 -08003645/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 * proc_cpuset_show()
3647 * - Print tasks cpuset path into seq_file.
3648 * - Used for /proc/<pid>/cpuset.
Paul Jackson053199e2005-10-30 15:02:30 -08003649 * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
3650 * doesn't really matter if tsk->cpuset changes after we read it,
Tejun Heo5d21cc22013-01-07 08:51:08 -08003651 * and we take cpuset_mutex, keeping cpuset_attach() from changing it
Paul Menage2df167a2008-02-07 00:14:45 -08003652 * anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 */
Zefan Li52de4772014-09-18 16:03:36 +08003654int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
3655 struct pid *pid, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
Tejun Heo4c737b42016-08-10 11:23:44 -04003657 char *buf;
Paul Menage8793d852007-10-18 23:39:39 -07003658 struct cgroup_subsys_state *css;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003659 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Eric W. Biederman99f89552006-06-26 00:25:55 -07003661 retval = -ENOMEM;
Tejun Heoe61734c2014-02-12 09:29:50 -05003662 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 if (!buf)
Eric W. Biederman99f89552006-06-26 00:25:55 -07003664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Aditya Kalia79a9082016-01-29 02:54:06 -06003666 css = task_get_css(tsk, cpuset_cgrp_id);
Tejun Heo4c737b42016-08-10 11:23:44 -04003667 retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX,
3668 current->nsproxy->cgroup_ns);
Aditya Kalia79a9082016-01-29 02:54:06 -06003669 css_put(css);
Tejun Heo4c737b42016-08-10 11:23:44 -04003670 if (retval >= PATH_MAX)
Tejun Heo679a5e32016-09-29 11:58:36 +02003671 retval = -ENAMETOOLONG;
3672 if (retval < 0)
Zefan Li52de4772014-09-18 16:03:36 +08003673 goto out_free;
Tejun Heo4c737b42016-08-10 11:23:44 -04003674 seq_puts(m, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 seq_putc(m, '\n');
Tejun Heoe61734c2014-02-12 09:29:50 -05003676 retval = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003677out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 kfree(buf);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003679out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return retval;
3681}
Paul Menage8793d852007-10-18 23:39:39 -07003682#endif /* CONFIG_PROC_PID_CPUSET */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Heiko Carstensd01d4822009-09-21 11:06:27 +02003684/* Display task mems_allowed in /proc/<pid>/status file. */
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08003685void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Tejun Heoe8e6d972015-02-13 14:37:23 -08003687 seq_printf(m, "Mems_allowed:\t%*pb\n",
3688 nodemask_pr_args(&task->mems_allowed));
3689 seq_printf(m, "Mems_allowed_list:\t%*pbl\n",
3690 nodemask_pr_args(&task->mems_allowed));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}