Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * kernel/cpuset.c |
| 3 | * |
| 4 | * Processor and Memory placement constraints for sets of tasks. |
| 5 | * |
| 6 | * Copyright (C) 2003 BULL SA. |
| 7 | * Copyright (C) 2004-2007 Silicon Graphics, Inc. |
| 8 | * Copyright (C) 2006 Google, Inc |
| 9 | * |
| 10 | * Portions derived from Patrick Mochel's sysfs code. |
| 11 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 12 | * |
| 13 | * 2003-10-10 Written by Simon Derr. |
| 14 | * 2003-10-22 Updates by Stephen Hemminger. |
| 15 | * 2004 May-July Rework by Paul Jackson. |
| 16 | * 2006 Rework by Paul Menage to use generic cgroups |
| 17 | * 2008 Rework of the scheduler domains and CPU hotplug handling |
| 18 | * by Max Krasnyansky |
| 19 | * |
| 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 | |
| 25 | #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> |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 36 | #include <linux/kthread.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 37 | #include <linux/list.h> |
| 38 | #include <linux/mempolicy.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/memory.h> |
| 41 | #include <linux/export.h> |
| 42 | #include <linux/mount.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 43 | #include <linux/fs_context.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 44 | #include <linux/namei.h> |
| 45 | #include <linux/pagemap.h> |
| 46 | #include <linux/proc_fs.h> |
| 47 | #include <linux/rcupdate.h> |
| 48 | #include <linux/sched.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 49 | #include <linux/sched/deadline.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | #include <linux/sched/mm.h> |
| 51 | #include <linux/sched/task.h> |
| 52 | #include <linux/seq_file.h> |
| 53 | #include <linux/security.h> |
| 54 | #include <linux/slab.h> |
| 55 | #include <linux/spinlock.h> |
| 56 | #include <linux/stat.h> |
| 57 | #include <linux/string.h> |
| 58 | #include <linux/time.h> |
| 59 | #include <linux/time64.h> |
| 60 | #include <linux/backing-dev.h> |
| 61 | #include <linux/sort.h> |
| 62 | #include <linux/oom.h> |
| 63 | #include <linux/sched/isolation.h> |
| 64 | #include <linux/uaccess.h> |
| 65 | #include <linux/atomic.h> |
| 66 | #include <linux/mutex.h> |
| 67 | #include <linux/cgroup.h> |
| 68 | #include <linux/wait.h> |
| 69 | |
| 70 | DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key); |
| 71 | DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key); |
| 72 | |
| 73 | /* See "Frequency meter" comments, below. */ |
| 74 | |
| 75 | struct fmeter { |
| 76 | int cnt; /* unprocessed events count */ |
| 77 | int val; /* most recent output value */ |
| 78 | time64_t time; /* clock (secs) when val computed */ |
| 79 | spinlock_t lock; /* guards read or write of above */ |
| 80 | }; |
| 81 | |
| 82 | struct cpuset { |
| 83 | struct cgroup_subsys_state css; |
| 84 | |
| 85 | unsigned long flags; /* "unsigned long" so bitops work */ |
| 86 | |
| 87 | /* |
| 88 | * On default hierarchy: |
| 89 | * |
| 90 | * The user-configured masks can only be changed by writing to |
| 91 | * cpuset.cpus and cpuset.mems, and won't be limited by the |
| 92 | * parent masks. |
| 93 | * |
| 94 | * The effective masks is the real masks that apply to the tasks |
| 95 | * in the cpuset. They may be changed if the configured masks are |
| 96 | * changed or hotplug happens. |
| 97 | * |
| 98 | * effective_mask == configured_mask & parent's effective_mask, |
| 99 | * and if it ends up empty, it will inherit the parent's mask. |
| 100 | * |
| 101 | * |
| 102 | * On legacy hierachy: |
| 103 | * |
| 104 | * The user-configured masks are always the same with effective masks. |
| 105 | */ |
| 106 | |
| 107 | /* user-configured CPUs and Memory Nodes allow to tasks */ |
| 108 | cpumask_var_t cpus_allowed; |
| 109 | nodemask_t mems_allowed; |
| 110 | |
| 111 | /* effective CPUs and Memory Nodes allow to tasks */ |
| 112 | cpumask_var_t effective_cpus; |
| 113 | nodemask_t effective_mems; |
| 114 | |
| 115 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 116 | * CPUs allocated to child sub-partitions (default hierarchy only) |
| 117 | * - CPUs granted by the parent = effective_cpus U subparts_cpus |
| 118 | * - effective_cpus and subparts_cpus are mutually exclusive. |
| 119 | * |
| 120 | * effective_cpus contains only onlined CPUs, but subparts_cpus |
| 121 | * may have offlined ones. |
| 122 | */ |
| 123 | cpumask_var_t subparts_cpus; |
| 124 | |
| 125 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 126 | * This is old Memory Nodes tasks took on. |
| 127 | * |
| 128 | * - top_cpuset.old_mems_allowed is initialized to mems_allowed. |
| 129 | * - A new cpuset's old_mems_allowed is initialized when some |
| 130 | * task is moved into it. |
| 131 | * - old_mems_allowed is used in cpuset_migrate_mm() when we change |
| 132 | * cpuset.mems_allowed and have tasks' nodemask updated, and |
| 133 | * then old_mems_allowed is updated to mems_allowed. |
| 134 | */ |
| 135 | nodemask_t old_mems_allowed; |
| 136 | |
| 137 | struct fmeter fmeter; /* memory_pressure filter */ |
| 138 | |
| 139 | /* |
| 140 | * Tasks are being attached to this cpuset. Used to prevent |
| 141 | * zeroing cpus/mems_allowed between ->can_attach() and ->attach(). |
| 142 | */ |
| 143 | int attach_in_progress; |
| 144 | |
| 145 | /* partition number for rebuild_sched_domains() */ |
| 146 | int pn; |
| 147 | |
| 148 | /* for custom sched domain */ |
| 149 | int relax_domain_level; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 150 | |
| 151 | /* number of CPUs in subparts_cpus */ |
| 152 | int nr_subparts_cpus; |
| 153 | |
| 154 | /* partition root state */ |
| 155 | int partition_root_state; |
| 156 | |
| 157 | /* |
| 158 | * Default hierarchy only: |
| 159 | * use_parent_ecpus - set if using parent's effective_cpus |
| 160 | * child_ecpus_count - # of children with use_parent_ecpus set |
| 161 | */ |
| 162 | int use_parent_ecpus; |
| 163 | int child_ecpus_count; |
| 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * Partition root states: |
| 168 | * |
| 169 | * 0 - not a partition root |
| 170 | * |
| 171 | * 1 - partition root |
| 172 | * |
| 173 | * -1 - invalid partition root |
| 174 | * None of the cpus in cpus_allowed can be put into the parent's |
| 175 | * subparts_cpus. In this case, the cpuset is not a real partition |
| 176 | * root anymore. However, the CPU_EXCLUSIVE bit will still be set |
| 177 | * and the cpuset can be restored back to a partition root if the |
| 178 | * parent cpuset can give more CPUs back to this child cpuset. |
| 179 | */ |
| 180 | #define PRS_DISABLED 0 |
| 181 | #define PRS_ENABLED 1 |
| 182 | #define PRS_ERROR -1 |
| 183 | |
| 184 | /* |
| 185 | * Temporary cpumasks for working with partitions that are passed among |
| 186 | * functions to avoid memory allocation in inner functions. |
| 187 | */ |
| 188 | struct tmpmasks { |
| 189 | cpumask_var_t addmask, delmask; /* For partition root */ |
| 190 | cpumask_var_t new_cpus; /* For update_cpumasks_hier() */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static inline struct cpuset *css_cs(struct cgroup_subsys_state *css) |
| 194 | { |
| 195 | return css ? container_of(css, struct cpuset, css) : NULL; |
| 196 | } |
| 197 | |
| 198 | /* Retrieve the cpuset for a task */ |
| 199 | static inline struct cpuset *task_cs(struct task_struct *task) |
| 200 | { |
| 201 | return css_cs(task_css(task, cpuset_cgrp_id)); |
| 202 | } |
| 203 | |
| 204 | static inline struct cpuset *parent_cs(struct cpuset *cs) |
| 205 | { |
| 206 | return css_cs(cs->css.parent); |
| 207 | } |
| 208 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 209 | /* bits in struct cpuset flags field */ |
| 210 | typedef enum { |
| 211 | CS_ONLINE, |
| 212 | CS_CPU_EXCLUSIVE, |
| 213 | CS_MEM_EXCLUSIVE, |
| 214 | CS_MEM_HARDWALL, |
| 215 | CS_MEMORY_MIGRATE, |
| 216 | CS_SCHED_LOAD_BALANCE, |
| 217 | CS_SPREAD_PAGE, |
| 218 | CS_SPREAD_SLAB, |
| 219 | } cpuset_flagbits_t; |
| 220 | |
| 221 | /* convenient tests for these bits */ |
| 222 | static inline bool is_cpuset_online(struct cpuset *cs) |
| 223 | { |
| 224 | return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css); |
| 225 | } |
| 226 | |
| 227 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
| 228 | { |
| 229 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
| 230 | } |
| 231 | |
| 232 | static inline int is_mem_exclusive(const struct cpuset *cs) |
| 233 | { |
| 234 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
| 235 | } |
| 236 | |
| 237 | static inline int is_mem_hardwall(const struct cpuset *cs) |
| 238 | { |
| 239 | return test_bit(CS_MEM_HARDWALL, &cs->flags); |
| 240 | } |
| 241 | |
| 242 | static inline int is_sched_load_balance(const struct cpuset *cs) |
| 243 | { |
| 244 | return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
| 245 | } |
| 246 | |
| 247 | static inline int is_memory_migrate(const struct cpuset *cs) |
| 248 | { |
| 249 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
| 250 | } |
| 251 | |
| 252 | static inline int is_spread_page(const struct cpuset *cs) |
| 253 | { |
| 254 | return test_bit(CS_SPREAD_PAGE, &cs->flags); |
| 255 | } |
| 256 | |
| 257 | static inline int is_spread_slab(const struct cpuset *cs) |
| 258 | { |
| 259 | return test_bit(CS_SPREAD_SLAB, &cs->flags); |
| 260 | } |
| 261 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 262 | static inline int is_partition_root(const struct cpuset *cs) |
| 263 | { |
| 264 | return cs->partition_root_state > 0; |
| 265 | } |
| 266 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 267 | static struct cpuset top_cpuset = { |
| 268 | .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) | |
| 269 | (1 << CS_MEM_EXCLUSIVE)), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 270 | .partition_root_state = PRS_ENABLED, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | /** |
| 274 | * cpuset_for_each_child - traverse online children of a cpuset |
| 275 | * @child_cs: loop cursor pointing to the current child |
| 276 | * @pos_css: used for iteration |
| 277 | * @parent_cs: target cpuset to walk children of |
| 278 | * |
| 279 | * Walk @child_cs through the online children of @parent_cs. Must be used |
| 280 | * with RCU read locked. |
| 281 | */ |
| 282 | #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \ |
| 283 | css_for_each_child((pos_css), &(parent_cs)->css) \ |
| 284 | if (is_cpuset_online(((child_cs) = css_cs((pos_css))))) |
| 285 | |
| 286 | /** |
| 287 | * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants |
| 288 | * @des_cs: loop cursor pointing to the current descendant |
| 289 | * @pos_css: used for iteration |
| 290 | * @root_cs: target cpuset to walk ancestor of |
| 291 | * |
| 292 | * Walk @des_cs through the online descendants of @root_cs. Must be used |
| 293 | * with RCU read locked. The caller may modify @pos_css by calling |
| 294 | * css_rightmost_descendant() to skip subtree. @root_cs is included in the |
| 295 | * iteration and the first node to be visited. |
| 296 | */ |
| 297 | #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \ |
| 298 | css_for_each_descendant_pre((pos_css), &(root_cs)->css) \ |
| 299 | if (is_cpuset_online(((des_cs) = css_cs((pos_css))))) |
| 300 | |
| 301 | /* |
| 302 | * There are two global locks guarding cpuset structures - cpuset_mutex and |
| 303 | * callback_lock. We also require taking task_lock() when dereferencing a |
| 304 | * task's cpuset pointer. See "The task_lock() exception", at the end of this |
| 305 | * comment. |
| 306 | * |
| 307 | * A task must hold both locks to modify cpusets. If a task holds |
| 308 | * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it |
| 309 | * is the only task able to also acquire callback_lock and be able to |
| 310 | * modify cpusets. It can perform various checks on the cpuset structure |
| 311 | * first, knowing nothing will change. It can also allocate memory while |
| 312 | * just holding cpuset_mutex. While it is performing these checks, various |
| 313 | * callback routines can briefly acquire callback_lock to query cpusets. |
| 314 | * Once it is ready to make the changes, it takes callback_lock, blocking |
| 315 | * everyone else. |
| 316 | * |
| 317 | * Calls to the kernel memory allocator can not be made while holding |
| 318 | * callback_lock, as that would risk double tripping on callback_lock |
| 319 | * from one of the callbacks into the cpuset code from within |
| 320 | * __alloc_pages(). |
| 321 | * |
| 322 | * If a task is only holding callback_lock, then it has read-only |
| 323 | * access to cpusets. |
| 324 | * |
| 325 | * Now, the task_struct fields mems_allowed and mempolicy may be changed |
| 326 | * by other task, we use alloc_lock in the task_struct fields to protect |
| 327 | * them. |
| 328 | * |
| 329 | * The cpuset_common_file_read() handlers only hold callback_lock across |
| 330 | * small pieces of code, such as when reading out possibly multi-word |
| 331 | * cpumasks and nodemasks. |
| 332 | * |
| 333 | * Accessing a task's cpuset should be done in accordance with the |
| 334 | * guidelines for accessing subsystem state in kernel/cgroup.c |
| 335 | */ |
| 336 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 337 | DEFINE_STATIC_PERCPU_RWSEM(cpuset_rwsem); |
| 338 | |
| 339 | void cpuset_read_lock(void) |
| 340 | { |
| 341 | percpu_down_read(&cpuset_rwsem); |
| 342 | } |
| 343 | |
| 344 | void cpuset_read_unlock(void) |
| 345 | { |
| 346 | percpu_up_read(&cpuset_rwsem); |
| 347 | } |
| 348 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 349 | static DEFINE_SPINLOCK(callback_lock); |
| 350 | |
| 351 | static struct workqueue_struct *cpuset_migrate_mm_wq; |
| 352 | |
| 353 | /* |
| 354 | * CPU / memory hotplug is handled asynchronously. |
| 355 | */ |
| 356 | static void cpuset_hotplug_workfn(struct work_struct *work); |
| 357 | static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn); |
| 358 | |
| 359 | static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq); |
| 360 | |
| 361 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 362 | * Cgroup v2 behavior is used on the "cpus" and "mems" control files when |
| 363 | * on default hierarchy or when the cpuset_v2_mode flag is set by mounting |
| 364 | * the v1 cpuset cgroup filesystem with the "cpuset_v2_mode" mount option. |
| 365 | * With v2 behavior, "cpus" and "mems" are always what the users have |
| 366 | * requested and won't be changed by hotplug events. Only the effective |
| 367 | * cpus or mems will be affected. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 368 | */ |
| 369 | static inline bool is_in_v2_mode(void) |
| 370 | { |
| 371 | return cgroup_subsys_on_dfl(cpuset_cgrp_subsys) || |
| 372 | (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE); |
| 373 | } |
| 374 | |
| 375 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 376 | * Return in pmask the portion of a cpusets's cpus_allowed that |
| 377 | * are online. If none are online, walk up the cpuset hierarchy |
| 378 | * until we find one that does have some online cpus. |
| 379 | * |
| 380 | * One way or another, we guarantee to return some non-empty subset |
| 381 | * of cpu_online_mask. |
| 382 | * |
| 383 | * Call with callback_lock or cpuset_mutex held. |
| 384 | */ |
| 385 | static void guarantee_online_cpus(struct cpuset *cs, struct cpumask *pmask) |
| 386 | { |
| 387 | while (!cpumask_intersects(cs->effective_cpus, cpu_online_mask)) { |
| 388 | cs = parent_cs(cs); |
| 389 | if (unlikely(!cs)) { |
| 390 | /* |
| 391 | * The top cpuset doesn't have any online cpu as a |
| 392 | * consequence of a race between cpuset_hotplug_work |
| 393 | * and cpu hotplug notifier. But we know the top |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 394 | * cpuset's effective_cpus is on its way to be |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 395 | * identical to cpu_online_mask. |
| 396 | */ |
| 397 | cpumask_copy(pmask, cpu_online_mask); |
| 398 | return; |
| 399 | } |
| 400 | } |
| 401 | cpumask_and(pmask, cs->effective_cpus, cpu_online_mask); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Return in *pmask the portion of a cpusets's mems_allowed that |
| 406 | * are online, with memory. If none are online with memory, walk |
| 407 | * up the cpuset hierarchy until we find one that does have some |
| 408 | * online mems. The top cpuset always has some mems online. |
| 409 | * |
| 410 | * One way or another, we guarantee to return some non-empty subset |
| 411 | * of node_states[N_MEMORY]. |
| 412 | * |
| 413 | * Call with callback_lock or cpuset_mutex held. |
| 414 | */ |
| 415 | static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask) |
| 416 | { |
| 417 | while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY])) |
| 418 | cs = parent_cs(cs); |
| 419 | nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * update task's spread flag if cpuset's page/slab spread flag is set |
| 424 | * |
| 425 | * Call with callback_lock or cpuset_mutex held. |
| 426 | */ |
| 427 | static void cpuset_update_task_spread_flag(struct cpuset *cs, |
| 428 | struct task_struct *tsk) |
| 429 | { |
| 430 | if (is_spread_page(cs)) |
| 431 | task_set_spread_page(tsk); |
| 432 | else |
| 433 | task_clear_spread_page(tsk); |
| 434 | |
| 435 | if (is_spread_slab(cs)) |
| 436 | task_set_spread_slab(tsk); |
| 437 | else |
| 438 | task_clear_spread_slab(tsk); |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? |
| 443 | * |
| 444 | * One cpuset is a subset of another if all its allowed CPUs and |
| 445 | * Memory Nodes are a subset of the other, and its exclusive flags |
| 446 | * are only set if the other's are set. Call holding cpuset_mutex. |
| 447 | */ |
| 448 | |
| 449 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) |
| 450 | { |
| 451 | return cpumask_subset(p->cpus_allowed, q->cpus_allowed) && |
| 452 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
| 453 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && |
| 454 | is_mem_exclusive(p) <= is_mem_exclusive(q); |
| 455 | } |
| 456 | |
| 457 | /** |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 458 | * alloc_cpumasks - allocate three cpumasks for cpuset |
| 459 | * @cs: the cpuset that have cpumasks to be allocated. |
| 460 | * @tmp: the tmpmasks structure pointer |
| 461 | * Return: 0 if successful, -ENOMEM otherwise. |
| 462 | * |
| 463 | * Only one of the two input arguments should be non-NULL. |
| 464 | */ |
| 465 | static inline int alloc_cpumasks(struct cpuset *cs, struct tmpmasks *tmp) |
| 466 | { |
| 467 | cpumask_var_t *pmask1, *pmask2, *pmask3; |
| 468 | |
| 469 | if (cs) { |
| 470 | pmask1 = &cs->cpus_allowed; |
| 471 | pmask2 = &cs->effective_cpus; |
| 472 | pmask3 = &cs->subparts_cpus; |
| 473 | } else { |
| 474 | pmask1 = &tmp->new_cpus; |
| 475 | pmask2 = &tmp->addmask; |
| 476 | pmask3 = &tmp->delmask; |
| 477 | } |
| 478 | |
| 479 | if (!zalloc_cpumask_var(pmask1, GFP_KERNEL)) |
| 480 | return -ENOMEM; |
| 481 | |
| 482 | if (!zalloc_cpumask_var(pmask2, GFP_KERNEL)) |
| 483 | goto free_one; |
| 484 | |
| 485 | if (!zalloc_cpumask_var(pmask3, GFP_KERNEL)) |
| 486 | goto free_two; |
| 487 | |
| 488 | return 0; |
| 489 | |
| 490 | free_two: |
| 491 | free_cpumask_var(*pmask2); |
| 492 | free_one: |
| 493 | free_cpumask_var(*pmask1); |
| 494 | return -ENOMEM; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * free_cpumasks - free cpumasks in a tmpmasks structure |
| 499 | * @cs: the cpuset that have cpumasks to be free. |
| 500 | * @tmp: the tmpmasks structure pointer |
| 501 | */ |
| 502 | static inline void free_cpumasks(struct cpuset *cs, struct tmpmasks *tmp) |
| 503 | { |
| 504 | if (cs) { |
| 505 | free_cpumask_var(cs->cpus_allowed); |
| 506 | free_cpumask_var(cs->effective_cpus); |
| 507 | free_cpumask_var(cs->subparts_cpus); |
| 508 | } |
| 509 | if (tmp) { |
| 510 | free_cpumask_var(tmp->new_cpus); |
| 511 | free_cpumask_var(tmp->addmask); |
| 512 | free_cpumask_var(tmp->delmask); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /** |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 517 | * alloc_trial_cpuset - allocate a trial cpuset |
| 518 | * @cs: the cpuset that the trial cpuset duplicates |
| 519 | */ |
| 520 | static struct cpuset *alloc_trial_cpuset(struct cpuset *cs) |
| 521 | { |
| 522 | struct cpuset *trial; |
| 523 | |
| 524 | trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL); |
| 525 | if (!trial) |
| 526 | return NULL; |
| 527 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 528 | if (alloc_cpumasks(trial, NULL)) { |
| 529 | kfree(trial); |
| 530 | return NULL; |
| 531 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | |
| 533 | cpumask_copy(trial->cpus_allowed, cs->cpus_allowed); |
| 534 | cpumask_copy(trial->effective_cpus, cs->effective_cpus); |
| 535 | return trial; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 539 | * free_cpuset - free the cpuset |
| 540 | * @cs: the cpuset to be freed |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 541 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 542 | static inline void free_cpuset(struct cpuset *cs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 543 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 544 | free_cpumasks(cs, NULL); |
| 545 | kfree(cs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* |
| 549 | * validate_change() - Used to validate that any proposed cpuset change |
| 550 | * follows the structural rules for cpusets. |
| 551 | * |
| 552 | * If we replaced the flag and mask values of the current cpuset |
| 553 | * (cur) with those values in the trial cpuset (trial), would |
| 554 | * our various subset and exclusive rules still be valid? Presumes |
| 555 | * cpuset_mutex held. |
| 556 | * |
| 557 | * 'cur' is the address of an actual, in-use cpuset. Operations |
| 558 | * such as list traversal that depend on the actual address of the |
| 559 | * cpuset in the list must use cur below, not trial. |
| 560 | * |
| 561 | * 'trial' is the address of bulk structure copy of cur, with |
| 562 | * perhaps one or more of the fields cpus_allowed, mems_allowed, |
| 563 | * or flags changed to new, trial values. |
| 564 | * |
| 565 | * Return 0 if valid, -errno if not. |
| 566 | */ |
| 567 | |
| 568 | static int validate_change(struct cpuset *cur, struct cpuset *trial) |
| 569 | { |
| 570 | struct cgroup_subsys_state *css; |
| 571 | struct cpuset *c, *par; |
| 572 | int ret; |
| 573 | |
| 574 | rcu_read_lock(); |
| 575 | |
| 576 | /* Each of our child cpusets must be a subset of us */ |
| 577 | ret = -EBUSY; |
| 578 | cpuset_for_each_child(c, css, cur) |
| 579 | if (!is_cpuset_subset(c, trial)) |
| 580 | goto out; |
| 581 | |
| 582 | /* Remaining checks don't apply to root cpuset */ |
| 583 | ret = 0; |
| 584 | if (cur == &top_cpuset) |
| 585 | goto out; |
| 586 | |
| 587 | par = parent_cs(cur); |
| 588 | |
| 589 | /* On legacy hiearchy, we must be a subset of our parent cpuset. */ |
| 590 | ret = -EACCES; |
| 591 | if (!is_in_v2_mode() && !is_cpuset_subset(trial, par)) |
| 592 | goto out; |
| 593 | |
| 594 | /* |
| 595 | * If either I or some sibling (!= me) is exclusive, we can't |
| 596 | * overlap |
| 597 | */ |
| 598 | ret = -EINVAL; |
| 599 | cpuset_for_each_child(c, css, par) { |
| 600 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
| 601 | c != cur && |
| 602 | cpumask_intersects(trial->cpus_allowed, c->cpus_allowed)) |
| 603 | goto out; |
| 604 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
| 605 | c != cur && |
| 606 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) |
| 607 | goto out; |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Cpusets with tasks - existing or newly being attached - can't |
| 612 | * be changed to have empty cpus_allowed or mems_allowed. |
| 613 | */ |
| 614 | ret = -ENOSPC; |
| 615 | if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) { |
| 616 | if (!cpumask_empty(cur->cpus_allowed) && |
| 617 | cpumask_empty(trial->cpus_allowed)) |
| 618 | goto out; |
| 619 | if (!nodes_empty(cur->mems_allowed) && |
| 620 | nodes_empty(trial->mems_allowed)) |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * We can't shrink if we won't have enough room for SCHED_DEADLINE |
| 626 | * tasks. |
| 627 | */ |
| 628 | ret = -EBUSY; |
| 629 | if (is_cpu_exclusive(cur) && |
| 630 | !cpuset_cpumask_can_shrink(cur->cpus_allowed, |
| 631 | trial->cpus_allowed)) |
| 632 | goto out; |
| 633 | |
| 634 | ret = 0; |
| 635 | out: |
| 636 | rcu_read_unlock(); |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | #ifdef CONFIG_SMP |
| 641 | /* |
| 642 | * Helper routine for generate_sched_domains(). |
| 643 | * Do cpusets a, b have overlapping effective cpus_allowed masks? |
| 644 | */ |
| 645 | static int cpusets_overlap(struct cpuset *a, struct cpuset *b) |
| 646 | { |
| 647 | return cpumask_intersects(a->effective_cpus, b->effective_cpus); |
| 648 | } |
| 649 | |
| 650 | static void |
| 651 | update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c) |
| 652 | { |
| 653 | if (dattr->relax_domain_level < c->relax_domain_level) |
| 654 | dattr->relax_domain_level = c->relax_domain_level; |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | static void update_domain_attr_tree(struct sched_domain_attr *dattr, |
| 659 | struct cpuset *root_cs) |
| 660 | { |
| 661 | struct cpuset *cp; |
| 662 | struct cgroup_subsys_state *pos_css; |
| 663 | |
| 664 | rcu_read_lock(); |
| 665 | cpuset_for_each_descendant_pre(cp, pos_css, root_cs) { |
| 666 | /* skip the whole subtree if @cp doesn't have any CPU */ |
| 667 | if (cpumask_empty(cp->cpus_allowed)) { |
| 668 | pos_css = css_rightmost_descendant(pos_css); |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | if (is_sched_load_balance(cp)) |
| 673 | update_domain_attr(dattr, cp); |
| 674 | } |
| 675 | rcu_read_unlock(); |
| 676 | } |
| 677 | |
| 678 | /* Must be called with cpuset_mutex held. */ |
| 679 | static inline int nr_cpusets(void) |
| 680 | { |
| 681 | /* jump label reference count + the top-level cpuset */ |
| 682 | return static_key_count(&cpusets_enabled_key.key) + 1; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * generate_sched_domains() |
| 687 | * |
| 688 | * This function builds a partial partition of the systems CPUs |
| 689 | * A 'partial partition' is a set of non-overlapping subsets whose |
| 690 | * union is a subset of that set. |
| 691 | * The output of this function needs to be passed to kernel/sched/core.c |
| 692 | * partition_sched_domains() routine, which will rebuild the scheduler's |
| 693 | * load balancing domains (sched domains) as specified by that partial |
| 694 | * partition. |
| 695 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 696 | * See "What is sched_load_balance" in Documentation/admin-guide/cgroup-v1/cpusets.rst |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 697 | * for a background explanation of this. |
| 698 | * |
| 699 | * Does not return errors, on the theory that the callers of this |
| 700 | * routine would rather not worry about failures to rebuild sched |
| 701 | * domains when operating in the severe memory shortage situations |
| 702 | * that could cause allocation failures below. |
| 703 | * |
| 704 | * Must be called with cpuset_mutex held. |
| 705 | * |
| 706 | * The three key local variables below are: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 707 | * cp - cpuset pointer, used (together with pos_css) to perform a |
| 708 | * top-down scan of all cpusets. For our purposes, rebuilding |
| 709 | * the schedulers sched domains, we can ignore !is_sched_load_ |
| 710 | * balance cpusets. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 711 | * csa - (for CpuSet Array) Array of pointers to all the cpusets |
| 712 | * that need to be load balanced, for convenient iterative |
| 713 | * access by the subsequent code that finds the best partition, |
| 714 | * i.e the set of domains (subsets) of CPUs such that the |
| 715 | * cpus_allowed of every cpuset marked is_sched_load_balance |
| 716 | * is a subset of one of these domains, while there are as |
| 717 | * many such domains as possible, each as small as possible. |
| 718 | * doms - Conversion of 'csa' to an array of cpumasks, for passing to |
| 719 | * the kernel/sched/core.c routine partition_sched_domains() in a |
| 720 | * convenient format, that can be easily compared to the prior |
| 721 | * value to determine what partition elements (sched domains) |
| 722 | * were changed (added or removed.) |
| 723 | * |
| 724 | * Finding the best partition (set of domains): |
| 725 | * The triple nested loops below over i, j, k scan over the |
| 726 | * load balanced cpusets (using the array of cpuset pointers in |
| 727 | * csa[]) looking for pairs of cpusets that have overlapping |
| 728 | * cpus_allowed, but which don't have the same 'pn' partition |
| 729 | * number and gives them in the same partition number. It keeps |
| 730 | * looping on the 'restart' label until it can no longer find |
| 731 | * any such pairs. |
| 732 | * |
| 733 | * The union of the cpus_allowed masks from the set of |
| 734 | * all cpusets having the same 'pn' value then form the one |
| 735 | * element of the partition (one sched domain) to be passed to |
| 736 | * partition_sched_domains(). |
| 737 | */ |
| 738 | static int generate_sched_domains(cpumask_var_t **domains, |
| 739 | struct sched_domain_attr **attributes) |
| 740 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 741 | struct cpuset *cp; /* top-down scan of cpusets */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 742 | struct cpuset **csa; /* array of all cpuset ptrs */ |
| 743 | int csn; /* how many cpuset ptrs in csa so far */ |
| 744 | int i, j, k; /* indices for partition finding loops */ |
| 745 | cpumask_var_t *doms; /* resulting partition; i.e. sched domains */ |
| 746 | struct sched_domain_attr *dattr; /* attributes for custom domains */ |
| 747 | int ndoms = 0; /* number of sched domains in result */ |
| 748 | int nslot; /* next empty doms[] struct cpumask slot */ |
| 749 | struct cgroup_subsys_state *pos_css; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 750 | bool root_load_balance = is_sched_load_balance(&top_cpuset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 751 | |
| 752 | doms = NULL; |
| 753 | dattr = NULL; |
| 754 | csa = NULL; |
| 755 | |
| 756 | /* Special case for the 99% of systems with one, full, sched domain */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 757 | if (root_load_balance && !top_cpuset.nr_subparts_cpus) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 758 | ndoms = 1; |
| 759 | doms = alloc_sched_domains(ndoms); |
| 760 | if (!doms) |
| 761 | goto done; |
| 762 | |
| 763 | dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL); |
| 764 | if (dattr) { |
| 765 | *dattr = SD_ATTR_INIT; |
| 766 | update_domain_attr_tree(dattr, &top_cpuset); |
| 767 | } |
| 768 | cpumask_and(doms[0], top_cpuset.effective_cpus, |
| 769 | housekeeping_cpumask(HK_FLAG_DOMAIN)); |
| 770 | |
| 771 | goto done; |
| 772 | } |
| 773 | |
| 774 | csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL); |
| 775 | if (!csa) |
| 776 | goto done; |
| 777 | csn = 0; |
| 778 | |
| 779 | rcu_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 780 | if (root_load_balance) |
| 781 | csa[csn++] = &top_cpuset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 782 | cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) { |
| 783 | if (cp == &top_cpuset) |
| 784 | continue; |
| 785 | /* |
| 786 | * Continue traversing beyond @cp iff @cp has some CPUs and |
| 787 | * isn't load balancing. The former is obvious. The |
| 788 | * latter: All child cpusets contain a subset of the |
| 789 | * parent's cpus, so just skip them, and then we call |
| 790 | * update_domain_attr_tree() to calc relax_domain_level of |
| 791 | * the corresponding sched domain. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 792 | * |
| 793 | * If root is load-balancing, we can skip @cp if it |
| 794 | * is a subset of the root's effective_cpus. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 795 | */ |
| 796 | if (!cpumask_empty(cp->cpus_allowed) && |
| 797 | !(is_sched_load_balance(cp) && |
| 798 | cpumask_intersects(cp->cpus_allowed, |
| 799 | housekeeping_cpumask(HK_FLAG_DOMAIN)))) |
| 800 | continue; |
| 801 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 802 | if (root_load_balance && |
| 803 | cpumask_subset(cp->cpus_allowed, top_cpuset.effective_cpus)) |
| 804 | continue; |
| 805 | |
| 806 | if (is_sched_load_balance(cp) && |
| 807 | !cpumask_empty(cp->effective_cpus)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 808 | csa[csn++] = cp; |
| 809 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 810 | /* skip @cp's subtree if not a partition root */ |
| 811 | if (!is_partition_root(cp)) |
| 812 | pos_css = css_rightmost_descendant(pos_css); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 813 | } |
| 814 | rcu_read_unlock(); |
| 815 | |
| 816 | for (i = 0; i < csn; i++) |
| 817 | csa[i]->pn = i; |
| 818 | ndoms = csn; |
| 819 | |
| 820 | restart: |
| 821 | /* Find the best partition (set of sched domains) */ |
| 822 | for (i = 0; i < csn; i++) { |
| 823 | struct cpuset *a = csa[i]; |
| 824 | int apn = a->pn; |
| 825 | |
| 826 | for (j = 0; j < csn; j++) { |
| 827 | struct cpuset *b = csa[j]; |
| 828 | int bpn = b->pn; |
| 829 | |
| 830 | if (apn != bpn && cpusets_overlap(a, b)) { |
| 831 | for (k = 0; k < csn; k++) { |
| 832 | struct cpuset *c = csa[k]; |
| 833 | |
| 834 | if (c->pn == bpn) |
| 835 | c->pn = apn; |
| 836 | } |
| 837 | ndoms--; /* one less element */ |
| 838 | goto restart; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Now we know how many domains to create. |
| 845 | * Convert <csn, csa> to <ndoms, doms> and populate cpu masks. |
| 846 | */ |
| 847 | doms = alloc_sched_domains(ndoms); |
| 848 | if (!doms) |
| 849 | goto done; |
| 850 | |
| 851 | /* |
| 852 | * The rest of the code, including the scheduler, can deal with |
| 853 | * dattr==NULL case. No need to abort if alloc fails. |
| 854 | */ |
| 855 | dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr), |
| 856 | GFP_KERNEL); |
| 857 | |
| 858 | for (nslot = 0, i = 0; i < csn; i++) { |
| 859 | struct cpuset *a = csa[i]; |
| 860 | struct cpumask *dp; |
| 861 | int apn = a->pn; |
| 862 | |
| 863 | if (apn < 0) { |
| 864 | /* Skip completed partitions */ |
| 865 | continue; |
| 866 | } |
| 867 | |
| 868 | dp = doms[nslot]; |
| 869 | |
| 870 | if (nslot == ndoms) { |
| 871 | static int warnings = 10; |
| 872 | if (warnings) { |
| 873 | pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n", |
| 874 | nslot, ndoms, csn, i, apn); |
| 875 | warnings--; |
| 876 | } |
| 877 | continue; |
| 878 | } |
| 879 | |
| 880 | cpumask_clear(dp); |
| 881 | if (dattr) |
| 882 | *(dattr + nslot) = SD_ATTR_INIT; |
| 883 | for (j = i; j < csn; j++) { |
| 884 | struct cpuset *b = csa[j]; |
| 885 | |
| 886 | if (apn == b->pn) { |
| 887 | cpumask_or(dp, dp, b->effective_cpus); |
| 888 | cpumask_and(dp, dp, housekeeping_cpumask(HK_FLAG_DOMAIN)); |
| 889 | if (dattr) |
| 890 | update_domain_attr_tree(dattr + nslot, b); |
| 891 | |
| 892 | /* Done with this partition */ |
| 893 | b->pn = -1; |
| 894 | } |
| 895 | } |
| 896 | nslot++; |
| 897 | } |
| 898 | BUG_ON(nslot != ndoms); |
| 899 | |
| 900 | done: |
| 901 | kfree(csa); |
| 902 | |
| 903 | /* |
| 904 | * Fallback to the default domain if kmalloc() failed. |
| 905 | * See comments in partition_sched_domains(). |
| 906 | */ |
| 907 | if (doms == NULL) |
| 908 | ndoms = 1; |
| 909 | |
| 910 | *domains = doms; |
| 911 | *attributes = dattr; |
| 912 | return ndoms; |
| 913 | } |
| 914 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 915 | static void update_tasks_root_domain(struct cpuset *cs) |
| 916 | { |
| 917 | struct css_task_iter it; |
| 918 | struct task_struct *task; |
| 919 | |
| 920 | css_task_iter_start(&cs->css, 0, &it); |
| 921 | |
| 922 | while ((task = css_task_iter_next(&it))) |
| 923 | dl_add_task_root_domain(task); |
| 924 | |
| 925 | css_task_iter_end(&it); |
| 926 | } |
| 927 | |
| 928 | static void rebuild_root_domains(void) |
| 929 | { |
| 930 | struct cpuset *cs = NULL; |
| 931 | struct cgroup_subsys_state *pos_css; |
| 932 | |
| 933 | percpu_rwsem_assert_held(&cpuset_rwsem); |
| 934 | lockdep_assert_cpus_held(); |
| 935 | lockdep_assert_held(&sched_domains_mutex); |
| 936 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 937 | rcu_read_lock(); |
| 938 | |
| 939 | /* |
| 940 | * Clear default root domain DL accounting, it will be computed again |
| 941 | * if a task belongs to it. |
| 942 | */ |
| 943 | dl_clear_root_domain(&def_root_domain); |
| 944 | |
| 945 | cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { |
| 946 | |
| 947 | if (cpumask_empty(cs->effective_cpus)) { |
| 948 | pos_css = css_rightmost_descendant(pos_css); |
| 949 | continue; |
| 950 | } |
| 951 | |
| 952 | css_get(&cs->css); |
| 953 | |
| 954 | rcu_read_unlock(); |
| 955 | |
| 956 | update_tasks_root_domain(cs); |
| 957 | |
| 958 | rcu_read_lock(); |
| 959 | css_put(&cs->css); |
| 960 | } |
| 961 | rcu_read_unlock(); |
| 962 | } |
| 963 | |
| 964 | static void |
| 965 | partition_and_rebuild_sched_domains(int ndoms_new, cpumask_var_t doms_new[], |
| 966 | struct sched_domain_attr *dattr_new) |
| 967 | { |
| 968 | mutex_lock(&sched_domains_mutex); |
| 969 | partition_sched_domains_locked(ndoms_new, doms_new, dattr_new); |
| 970 | rebuild_root_domains(); |
| 971 | mutex_unlock(&sched_domains_mutex); |
| 972 | } |
| 973 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 974 | /* |
| 975 | * Rebuild scheduler domains. |
| 976 | * |
| 977 | * If the flag 'sched_load_balance' of any cpuset with non-empty |
| 978 | * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset |
| 979 | * which has that flag enabled, or if any cpuset with a non-empty |
| 980 | * 'cpus' is removed, then call this routine to rebuild the |
| 981 | * scheduler's dynamic sched domains. |
| 982 | * |
| 983 | * Call with cpuset_mutex held. Takes get_online_cpus(). |
| 984 | */ |
| 985 | static void rebuild_sched_domains_locked(void) |
| 986 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 987 | struct cgroup_subsys_state *pos_css; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 988 | struct sched_domain_attr *attr; |
| 989 | cpumask_var_t *doms; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 990 | struct cpuset *cs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 991 | int ndoms; |
| 992 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 993 | lockdep_assert_cpus_held(); |
| 994 | percpu_rwsem_assert_held(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 995 | |
| 996 | /* |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 997 | * If we have raced with CPU hotplug, return early to avoid |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 998 | * passing doms with offlined cpu to partition_sched_domains(). |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 999 | * Anyways, cpuset_hotplug_workfn() will rebuild sched domains. |
| 1000 | * |
| 1001 | * With no CPUs in any subpartitions, top_cpuset's effective CPUs |
| 1002 | * should be the same as the active CPUs, so checking only top_cpuset |
| 1003 | * is enough to detect racing CPU offlines. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1004 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1005 | if (!top_cpuset.nr_subparts_cpus && |
| 1006 | !cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask)) |
| 1007 | return; |
| 1008 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * With subpartition CPUs, however, the effective CPUs of a partition |
| 1011 | * root should be only a subset of the active CPUs. Since a CPU in any |
| 1012 | * partition root could be offlined, all must be checked. |
| 1013 | */ |
| 1014 | if (top_cpuset.nr_subparts_cpus) { |
| 1015 | rcu_read_lock(); |
| 1016 | cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { |
| 1017 | if (!is_partition_root(cs)) { |
| 1018 | pos_css = css_rightmost_descendant(pos_css); |
| 1019 | continue; |
| 1020 | } |
| 1021 | if (!cpumask_subset(cs->effective_cpus, |
| 1022 | cpu_active_mask)) { |
| 1023 | rcu_read_unlock(); |
| 1024 | return; |
| 1025 | } |
| 1026 | } |
| 1027 | rcu_read_unlock(); |
| 1028 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* Generate domain masks and attrs */ |
| 1031 | ndoms = generate_sched_domains(&doms, &attr); |
| 1032 | |
| 1033 | /* Have scheduler rebuild the domains */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1034 | partition_and_rebuild_sched_domains(ndoms, doms, attr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1035 | } |
| 1036 | #else /* !CONFIG_SMP */ |
| 1037 | static void rebuild_sched_domains_locked(void) |
| 1038 | { |
| 1039 | } |
| 1040 | #endif /* CONFIG_SMP */ |
| 1041 | |
| 1042 | void rebuild_sched_domains(void) |
| 1043 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1044 | get_online_cpus(); |
| 1045 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1046 | rebuild_sched_domains_locked(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1047 | percpu_up_write(&cpuset_rwsem); |
| 1048 | put_online_cpus(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset. |
| 1053 | * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed |
| 1054 | * |
| 1055 | * Iterate through each task of @cs updating its cpus_allowed to the |
| 1056 | * effective cpuset's. As this function is called with cpuset_mutex held, |
| 1057 | * cpuset membership stays stable. |
| 1058 | */ |
| 1059 | static void update_tasks_cpumask(struct cpuset *cs) |
| 1060 | { |
| 1061 | struct css_task_iter it; |
| 1062 | struct task_struct *task; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 1063 | bool top_cs = cs == &top_cpuset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1064 | |
| 1065 | css_task_iter_start(&cs->css, 0, &it); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 1066 | while ((task = css_task_iter_next(&it))) { |
| 1067 | /* |
| 1068 | * Percpu kthreads in top_cpuset are ignored |
| 1069 | */ |
| 1070 | if (top_cs && (task->flags & PF_KTHREAD) && |
| 1071 | kthread_is_per_cpu(task)) |
| 1072 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1073 | set_cpus_allowed_ptr(task, cs->effective_cpus); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 1074 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1075 | css_task_iter_end(&it); |
| 1076 | } |
| 1077 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1078 | /** |
| 1079 | * compute_effective_cpumask - Compute the effective cpumask of the cpuset |
| 1080 | * @new_cpus: the temp variable for the new effective_cpus mask |
| 1081 | * @cs: the cpuset the need to recompute the new effective_cpus mask |
| 1082 | * @parent: the parent cpuset |
| 1083 | * |
| 1084 | * If the parent has subpartition CPUs, include them in the list of |
| 1085 | * allowable CPUs in computing the new effective_cpus mask. Since offlined |
| 1086 | * CPUs are not removed from subparts_cpus, we have to use cpu_active_mask |
| 1087 | * to mask those out. |
| 1088 | */ |
| 1089 | static void compute_effective_cpumask(struct cpumask *new_cpus, |
| 1090 | struct cpuset *cs, struct cpuset *parent) |
| 1091 | { |
| 1092 | if (parent->nr_subparts_cpus) { |
| 1093 | cpumask_or(new_cpus, parent->effective_cpus, |
| 1094 | parent->subparts_cpus); |
| 1095 | cpumask_and(new_cpus, new_cpus, cs->cpus_allowed); |
| 1096 | cpumask_and(new_cpus, new_cpus, cpu_active_mask); |
| 1097 | } else { |
| 1098 | cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Commands for update_parent_subparts_cpumask |
| 1104 | */ |
| 1105 | enum subparts_cmd { |
| 1106 | partcmd_enable, /* Enable partition root */ |
| 1107 | partcmd_disable, /* Disable partition root */ |
| 1108 | partcmd_update, /* Update parent's subparts_cpus */ |
| 1109 | }; |
| 1110 | |
| 1111 | /** |
| 1112 | * update_parent_subparts_cpumask - update subparts_cpus mask of parent cpuset |
| 1113 | * @cpuset: The cpuset that requests change in partition root state |
| 1114 | * @cmd: Partition root state change command |
| 1115 | * @newmask: Optional new cpumask for partcmd_update |
| 1116 | * @tmp: Temporary addmask and delmask |
| 1117 | * Return: 0, 1 or an error code |
| 1118 | * |
| 1119 | * For partcmd_enable, the cpuset is being transformed from a non-partition |
| 1120 | * root to a partition root. The cpus_allowed mask of the given cpuset will |
| 1121 | * be put into parent's subparts_cpus and taken away from parent's |
| 1122 | * effective_cpus. The function will return 0 if all the CPUs listed in |
| 1123 | * cpus_allowed can be granted or an error code will be returned. |
| 1124 | * |
| 1125 | * For partcmd_disable, the cpuset is being transofrmed from a partition |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1126 | * root back to a non-partition root. Any CPUs in cpus_allowed that are in |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1127 | * parent's subparts_cpus will be taken away from that cpumask and put back |
| 1128 | * into parent's effective_cpus. 0 should always be returned. |
| 1129 | * |
| 1130 | * For partcmd_update, if the optional newmask is specified, the cpu |
| 1131 | * list is to be changed from cpus_allowed to newmask. Otherwise, |
| 1132 | * cpus_allowed is assumed to remain the same. The cpuset should either |
| 1133 | * be a partition root or an invalid partition root. The partition root |
| 1134 | * state may change if newmask is NULL and none of the requested CPUs can |
| 1135 | * be granted by the parent. The function will return 1 if changes to |
| 1136 | * parent's subparts_cpus and effective_cpus happen or 0 otherwise. |
| 1137 | * Error code should only be returned when newmask is non-NULL. |
| 1138 | * |
| 1139 | * The partcmd_enable and partcmd_disable commands are used by |
| 1140 | * update_prstate(). The partcmd_update command is used by |
| 1141 | * update_cpumasks_hier() with newmask NULL and update_cpumask() with |
| 1142 | * newmask set. |
| 1143 | * |
| 1144 | * The checking is more strict when enabling partition root than the |
| 1145 | * other two commands. |
| 1146 | * |
| 1147 | * Because of the implicit cpu exclusive nature of a partition root, |
| 1148 | * cpumask changes that violates the cpu exclusivity rule will not be |
| 1149 | * permitted when checked by validate_change(). The validate_change() |
| 1150 | * function will also prevent any changes to the cpu list if it is not |
| 1151 | * a superset of children's cpu lists. |
| 1152 | */ |
| 1153 | static int update_parent_subparts_cpumask(struct cpuset *cpuset, int cmd, |
| 1154 | struct cpumask *newmask, |
| 1155 | struct tmpmasks *tmp) |
| 1156 | { |
| 1157 | struct cpuset *parent = parent_cs(cpuset); |
| 1158 | int adding; /* Moving cpus from effective_cpus to subparts_cpus */ |
| 1159 | int deleting; /* Moving cpus from subparts_cpus to effective_cpus */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1160 | int new_prs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1161 | bool part_error = false; /* Partition error? */ |
| 1162 | |
| 1163 | percpu_rwsem_assert_held(&cpuset_rwsem); |
| 1164 | |
| 1165 | /* |
| 1166 | * The parent must be a partition root. |
| 1167 | * The new cpumask, if present, or the current cpus_allowed must |
| 1168 | * not be empty. |
| 1169 | */ |
| 1170 | if (!is_partition_root(parent) || |
| 1171 | (newmask && cpumask_empty(newmask)) || |
| 1172 | (!newmask && cpumask_empty(cpuset->cpus_allowed))) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | /* |
| 1176 | * Enabling/disabling partition root is not allowed if there are |
| 1177 | * online children. |
| 1178 | */ |
| 1179 | if ((cmd != partcmd_update) && css_has_online_children(&cpuset->css)) |
| 1180 | return -EBUSY; |
| 1181 | |
| 1182 | /* |
| 1183 | * Enabling partition root is not allowed if not all the CPUs |
| 1184 | * can be granted from parent's effective_cpus or at least one |
| 1185 | * CPU will be left after that. |
| 1186 | */ |
| 1187 | if ((cmd == partcmd_enable) && |
| 1188 | (!cpumask_subset(cpuset->cpus_allowed, parent->effective_cpus) || |
| 1189 | cpumask_equal(cpuset->cpus_allowed, parent->effective_cpus))) |
| 1190 | return -EINVAL; |
| 1191 | |
| 1192 | /* |
| 1193 | * A cpumask update cannot make parent's effective_cpus become empty. |
| 1194 | */ |
| 1195 | adding = deleting = false; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1196 | new_prs = cpuset->partition_root_state; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1197 | if (cmd == partcmd_enable) { |
| 1198 | cpumask_copy(tmp->addmask, cpuset->cpus_allowed); |
| 1199 | adding = true; |
| 1200 | } else if (cmd == partcmd_disable) { |
| 1201 | deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed, |
| 1202 | parent->subparts_cpus); |
| 1203 | } else if (newmask) { |
| 1204 | /* |
| 1205 | * partcmd_update with newmask: |
| 1206 | * |
| 1207 | * delmask = cpus_allowed & ~newmask & parent->subparts_cpus |
| 1208 | * addmask = newmask & parent->effective_cpus |
| 1209 | * & ~parent->subparts_cpus |
| 1210 | */ |
| 1211 | cpumask_andnot(tmp->delmask, cpuset->cpus_allowed, newmask); |
| 1212 | deleting = cpumask_and(tmp->delmask, tmp->delmask, |
| 1213 | parent->subparts_cpus); |
| 1214 | |
| 1215 | cpumask_and(tmp->addmask, newmask, parent->effective_cpus); |
| 1216 | adding = cpumask_andnot(tmp->addmask, tmp->addmask, |
| 1217 | parent->subparts_cpus); |
| 1218 | /* |
| 1219 | * Return error if the new effective_cpus could become empty. |
| 1220 | */ |
| 1221 | if (adding && |
| 1222 | cpumask_equal(parent->effective_cpus, tmp->addmask)) { |
| 1223 | if (!deleting) |
| 1224 | return -EINVAL; |
| 1225 | /* |
| 1226 | * As some of the CPUs in subparts_cpus might have |
| 1227 | * been offlined, we need to compute the real delmask |
| 1228 | * to confirm that. |
| 1229 | */ |
| 1230 | if (!cpumask_and(tmp->addmask, tmp->delmask, |
| 1231 | cpu_active_mask)) |
| 1232 | return -EINVAL; |
| 1233 | cpumask_copy(tmp->addmask, parent->effective_cpus); |
| 1234 | } |
| 1235 | } else { |
| 1236 | /* |
| 1237 | * partcmd_update w/o newmask: |
| 1238 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1239 | * addmask = cpus_allowed & parent->effective_cpus |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1240 | * |
| 1241 | * Note that parent's subparts_cpus may have been |
| 1242 | * pre-shrunk in case there is a change in the cpu list. |
| 1243 | * So no deletion is needed. |
| 1244 | */ |
| 1245 | adding = cpumask_and(tmp->addmask, cpuset->cpus_allowed, |
| 1246 | parent->effective_cpus); |
| 1247 | part_error = cpumask_equal(tmp->addmask, |
| 1248 | parent->effective_cpus); |
| 1249 | } |
| 1250 | |
| 1251 | if (cmd == partcmd_update) { |
| 1252 | int prev_prs = cpuset->partition_root_state; |
| 1253 | |
| 1254 | /* |
| 1255 | * Check for possible transition between PRS_ENABLED |
| 1256 | * and PRS_ERROR. |
| 1257 | */ |
| 1258 | switch (cpuset->partition_root_state) { |
| 1259 | case PRS_ENABLED: |
| 1260 | if (part_error) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1261 | new_prs = PRS_ERROR; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | case PRS_ERROR: |
| 1264 | if (!part_error) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1265 | new_prs = PRS_ENABLED; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | /* |
| 1269 | * Set part_error if previously in invalid state. |
| 1270 | */ |
| 1271 | part_error = (prev_prs == PRS_ERROR); |
| 1272 | } |
| 1273 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1274 | if (!part_error && (new_prs == PRS_ERROR)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1275 | return 0; /* Nothing need to be done */ |
| 1276 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1277 | if (new_prs == PRS_ERROR) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1278 | /* |
| 1279 | * Remove all its cpus from parent's subparts_cpus. |
| 1280 | */ |
| 1281 | adding = false; |
| 1282 | deleting = cpumask_and(tmp->delmask, cpuset->cpus_allowed, |
| 1283 | parent->subparts_cpus); |
| 1284 | } |
| 1285 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1286 | if (!adding && !deleting && (new_prs == cpuset->partition_root_state)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1287 | return 0; |
| 1288 | |
| 1289 | /* |
| 1290 | * Change the parent's subparts_cpus. |
| 1291 | * Newly added CPUs will be removed from effective_cpus and |
| 1292 | * newly deleted ones will be added back to effective_cpus. |
| 1293 | */ |
| 1294 | spin_lock_irq(&callback_lock); |
| 1295 | if (adding) { |
| 1296 | cpumask_or(parent->subparts_cpus, |
| 1297 | parent->subparts_cpus, tmp->addmask); |
| 1298 | cpumask_andnot(parent->effective_cpus, |
| 1299 | parent->effective_cpus, tmp->addmask); |
| 1300 | } |
| 1301 | if (deleting) { |
| 1302 | cpumask_andnot(parent->subparts_cpus, |
| 1303 | parent->subparts_cpus, tmp->delmask); |
| 1304 | /* |
| 1305 | * Some of the CPUs in subparts_cpus might have been offlined. |
| 1306 | */ |
| 1307 | cpumask_and(tmp->delmask, tmp->delmask, cpu_active_mask); |
| 1308 | cpumask_or(parent->effective_cpus, |
| 1309 | parent->effective_cpus, tmp->delmask); |
| 1310 | } |
| 1311 | |
| 1312 | parent->nr_subparts_cpus = cpumask_weight(parent->subparts_cpus); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1313 | |
| 1314 | if (cpuset->partition_root_state != new_prs) |
| 1315 | cpuset->partition_root_state = new_prs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1316 | spin_unlock_irq(&callback_lock); |
| 1317 | |
| 1318 | return cmd == partcmd_update; |
| 1319 | } |
| 1320 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1321 | /* |
| 1322 | * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1323 | * @cs: the cpuset to consider |
| 1324 | * @tmp: temp variables for calculating effective_cpus & partition setup |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1325 | * |
| 1326 | * When congifured cpumask is changed, the effective cpumasks of this cpuset |
| 1327 | * and all its descendants need to be updated. |
| 1328 | * |
| 1329 | * On legacy hierachy, effective_cpus will be the same with cpu_allowed. |
| 1330 | * |
| 1331 | * Called with cpuset_mutex held |
| 1332 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1333 | static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1334 | { |
| 1335 | struct cpuset *cp; |
| 1336 | struct cgroup_subsys_state *pos_css; |
| 1337 | bool need_rebuild_sched_domains = false; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1338 | int new_prs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1339 | |
| 1340 | rcu_read_lock(); |
| 1341 | cpuset_for_each_descendant_pre(cp, pos_css, cs) { |
| 1342 | struct cpuset *parent = parent_cs(cp); |
| 1343 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1344 | compute_effective_cpumask(tmp->new_cpus, cp, parent); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1345 | |
| 1346 | /* |
| 1347 | * If it becomes empty, inherit the effective mask of the |
| 1348 | * parent, which is guaranteed to have some CPUs. |
| 1349 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1350 | if (is_in_v2_mode() && cpumask_empty(tmp->new_cpus)) { |
| 1351 | cpumask_copy(tmp->new_cpus, parent->effective_cpus); |
| 1352 | if (!cp->use_parent_ecpus) { |
| 1353 | cp->use_parent_ecpus = true; |
| 1354 | parent->child_ecpus_count++; |
| 1355 | } |
| 1356 | } else if (cp->use_parent_ecpus) { |
| 1357 | cp->use_parent_ecpus = false; |
| 1358 | WARN_ON_ONCE(!parent->child_ecpus_count); |
| 1359 | parent->child_ecpus_count--; |
| 1360 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1361 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1362 | /* |
| 1363 | * Skip the whole subtree if the cpumask remains the same |
| 1364 | * and has no partition root state. |
| 1365 | */ |
| 1366 | if (!cp->partition_root_state && |
| 1367 | cpumask_equal(tmp->new_cpus, cp->effective_cpus)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1368 | pos_css = css_rightmost_descendant(pos_css); |
| 1369 | continue; |
| 1370 | } |
| 1371 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1372 | /* |
| 1373 | * update_parent_subparts_cpumask() should have been called |
| 1374 | * for cs already in update_cpumask(). We should also call |
| 1375 | * update_tasks_cpumask() again for tasks in the parent |
| 1376 | * cpuset if the parent's subparts_cpus changes. |
| 1377 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1378 | new_prs = cp->partition_root_state; |
| 1379 | if ((cp != cs) && new_prs) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1380 | switch (parent->partition_root_state) { |
| 1381 | case PRS_DISABLED: |
| 1382 | /* |
| 1383 | * If parent is not a partition root or an |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1384 | * invalid partition root, clear its state |
| 1385 | * and its CS_CPU_EXCLUSIVE flag. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1386 | */ |
| 1387 | WARN_ON_ONCE(cp->partition_root_state |
| 1388 | != PRS_ERROR); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1389 | new_prs = PRS_DISABLED; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* |
| 1392 | * clear_bit() is an atomic operation and |
| 1393 | * readers aren't interested in the state |
| 1394 | * of CS_CPU_EXCLUSIVE anyway. So we can |
| 1395 | * just update the flag without holding |
| 1396 | * the callback_lock. |
| 1397 | */ |
| 1398 | clear_bit(CS_CPU_EXCLUSIVE, &cp->flags); |
| 1399 | break; |
| 1400 | |
| 1401 | case PRS_ENABLED: |
| 1402 | if (update_parent_subparts_cpumask(cp, partcmd_update, NULL, tmp)) |
| 1403 | update_tasks_cpumask(parent); |
| 1404 | break; |
| 1405 | |
| 1406 | case PRS_ERROR: |
| 1407 | /* |
| 1408 | * When parent is invalid, it has to be too. |
| 1409 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1410 | new_prs = PRS_ERROR; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1415 | if (!css_tryget_online(&cp->css)) |
| 1416 | continue; |
| 1417 | rcu_read_unlock(); |
| 1418 | |
| 1419 | spin_lock_irq(&callback_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1420 | |
| 1421 | cpumask_copy(cp->effective_cpus, tmp->new_cpus); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1422 | if (cp->nr_subparts_cpus && (new_prs != PRS_ENABLED)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1423 | cp->nr_subparts_cpus = 0; |
| 1424 | cpumask_clear(cp->subparts_cpus); |
| 1425 | } else if (cp->nr_subparts_cpus) { |
| 1426 | /* |
| 1427 | * Make sure that effective_cpus & subparts_cpus |
| 1428 | * are mutually exclusive. |
| 1429 | * |
| 1430 | * In the unlikely event that effective_cpus |
| 1431 | * becomes empty. we clear cp->nr_subparts_cpus and |
| 1432 | * let its child partition roots to compete for |
| 1433 | * CPUs again. |
| 1434 | */ |
| 1435 | cpumask_andnot(cp->effective_cpus, cp->effective_cpus, |
| 1436 | cp->subparts_cpus); |
| 1437 | if (cpumask_empty(cp->effective_cpus)) { |
| 1438 | cpumask_copy(cp->effective_cpus, tmp->new_cpus); |
| 1439 | cpumask_clear(cp->subparts_cpus); |
| 1440 | cp->nr_subparts_cpus = 0; |
| 1441 | } else if (!cpumask_subset(cp->subparts_cpus, |
| 1442 | tmp->new_cpus)) { |
| 1443 | cpumask_andnot(cp->subparts_cpus, |
| 1444 | cp->subparts_cpus, tmp->new_cpus); |
| 1445 | cp->nr_subparts_cpus |
| 1446 | = cpumask_weight(cp->subparts_cpus); |
| 1447 | } |
| 1448 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1449 | |
| 1450 | if (new_prs != cp->partition_root_state) |
| 1451 | cp->partition_root_state = new_prs; |
| 1452 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1453 | spin_unlock_irq(&callback_lock); |
| 1454 | |
| 1455 | WARN_ON(!is_in_v2_mode() && |
| 1456 | !cpumask_equal(cp->cpus_allowed, cp->effective_cpus)); |
| 1457 | |
| 1458 | update_tasks_cpumask(cp); |
| 1459 | |
| 1460 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1461 | * On legacy hierarchy, if the effective cpumask of any non- |
| 1462 | * empty cpuset is changed, we need to rebuild sched domains. |
| 1463 | * On default hierarchy, the cpuset needs to be a partition |
| 1464 | * root as well. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1465 | */ |
| 1466 | if (!cpumask_empty(cp->cpus_allowed) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1467 | is_sched_load_balance(cp) && |
| 1468 | (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) || |
| 1469 | is_partition_root(cp))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1470 | need_rebuild_sched_domains = true; |
| 1471 | |
| 1472 | rcu_read_lock(); |
| 1473 | css_put(&cp->css); |
| 1474 | } |
| 1475 | rcu_read_unlock(); |
| 1476 | |
| 1477 | if (need_rebuild_sched_domains) |
| 1478 | rebuild_sched_domains_locked(); |
| 1479 | } |
| 1480 | |
| 1481 | /** |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1482 | * update_sibling_cpumasks - Update siblings cpumasks |
| 1483 | * @parent: Parent cpuset |
| 1484 | * @cs: Current cpuset |
| 1485 | * @tmp: Temp variables |
| 1486 | */ |
| 1487 | static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs, |
| 1488 | struct tmpmasks *tmp) |
| 1489 | { |
| 1490 | struct cpuset *sibling; |
| 1491 | struct cgroup_subsys_state *pos_css; |
| 1492 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1493 | percpu_rwsem_assert_held(&cpuset_rwsem); |
| 1494 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1495 | /* |
| 1496 | * Check all its siblings and call update_cpumasks_hier() |
| 1497 | * if their use_parent_ecpus flag is set in order for them |
| 1498 | * to use the right effective_cpus value. |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1499 | * |
| 1500 | * The update_cpumasks_hier() function may sleep. So we have to |
| 1501 | * release the RCU read lock before calling it. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1502 | */ |
| 1503 | rcu_read_lock(); |
| 1504 | cpuset_for_each_child(sibling, pos_css, parent) { |
| 1505 | if (sibling == cs) |
| 1506 | continue; |
| 1507 | if (!sibling->use_parent_ecpus) |
| 1508 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1509 | if (!css_tryget_online(&sibling->css)) |
| 1510 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1511 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1512 | rcu_read_unlock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1513 | update_cpumasks_hier(sibling, tmp); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1514 | rcu_read_lock(); |
| 1515 | css_put(&sibling->css); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1516 | } |
| 1517 | rcu_read_unlock(); |
| 1518 | } |
| 1519 | |
| 1520 | /** |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1521 | * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it |
| 1522 | * @cs: the cpuset to consider |
| 1523 | * @trialcs: trial cpuset |
| 1524 | * @buf: buffer of cpu numbers written to this cpuset |
| 1525 | */ |
| 1526 | static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, |
| 1527 | const char *buf) |
| 1528 | { |
| 1529 | int retval; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1530 | struct tmpmasks tmp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1531 | |
| 1532 | /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */ |
| 1533 | if (cs == &top_cpuset) |
| 1534 | return -EACCES; |
| 1535 | |
| 1536 | /* |
| 1537 | * An empty cpus_allowed is ok only if the cpuset has no tasks. |
| 1538 | * Since cpulist_parse() fails on an empty mask, we special case |
| 1539 | * that parsing. The validate_change() call ensures that cpusets |
| 1540 | * with tasks have cpus. |
| 1541 | */ |
| 1542 | if (!*buf) { |
| 1543 | cpumask_clear(trialcs->cpus_allowed); |
| 1544 | } else { |
| 1545 | retval = cpulist_parse(buf, trialcs->cpus_allowed); |
| 1546 | if (retval < 0) |
| 1547 | return retval; |
| 1548 | |
| 1549 | if (!cpumask_subset(trialcs->cpus_allowed, |
| 1550 | top_cpuset.cpus_allowed)) |
| 1551 | return -EINVAL; |
| 1552 | } |
| 1553 | |
| 1554 | /* Nothing to do if the cpus didn't change */ |
| 1555 | if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed)) |
| 1556 | return 0; |
| 1557 | |
| 1558 | retval = validate_change(cs, trialcs); |
| 1559 | if (retval < 0) |
| 1560 | return retval; |
| 1561 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1562 | #ifdef CONFIG_CPUMASK_OFFSTACK |
| 1563 | /* |
| 1564 | * Use the cpumasks in trialcs for tmpmasks when they are pointers |
| 1565 | * to allocated cpumasks. |
| 1566 | */ |
| 1567 | tmp.addmask = trialcs->subparts_cpus; |
| 1568 | tmp.delmask = trialcs->effective_cpus; |
| 1569 | tmp.new_cpus = trialcs->cpus_allowed; |
| 1570 | #endif |
| 1571 | |
| 1572 | if (cs->partition_root_state) { |
| 1573 | /* Cpumask of a partition root cannot be empty */ |
| 1574 | if (cpumask_empty(trialcs->cpus_allowed)) |
| 1575 | return -EINVAL; |
| 1576 | if (update_parent_subparts_cpumask(cs, partcmd_update, |
| 1577 | trialcs->cpus_allowed, &tmp) < 0) |
| 1578 | return -EINVAL; |
| 1579 | } |
| 1580 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1581 | spin_lock_irq(&callback_lock); |
| 1582 | cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1583 | |
| 1584 | /* |
| 1585 | * Make sure that subparts_cpus is a subset of cpus_allowed. |
| 1586 | */ |
| 1587 | if (cs->nr_subparts_cpus) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1588 | cpumask_and(cs->subparts_cpus, cs->subparts_cpus, cs->cpus_allowed); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1589 | cs->nr_subparts_cpus = cpumask_weight(cs->subparts_cpus); |
| 1590 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1591 | spin_unlock_irq(&callback_lock); |
| 1592 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1593 | update_cpumasks_hier(cs, &tmp); |
| 1594 | |
| 1595 | if (cs->partition_root_state) { |
| 1596 | struct cpuset *parent = parent_cs(cs); |
| 1597 | |
| 1598 | /* |
| 1599 | * For partition root, update the cpumasks of sibling |
| 1600 | * cpusets if they use parent's effective_cpus. |
| 1601 | */ |
| 1602 | if (parent->child_ecpus_count) |
| 1603 | update_sibling_cpumasks(parent, cs, &tmp); |
| 1604 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Migrate memory region from one set of nodes to another. This is |
| 1610 | * performed asynchronously as it can be called from process migration path |
| 1611 | * holding locks involved in process management. All mm migrations are |
| 1612 | * performed in the queued order and can be waited for by flushing |
| 1613 | * cpuset_migrate_mm_wq. |
| 1614 | */ |
| 1615 | |
| 1616 | struct cpuset_migrate_mm_work { |
| 1617 | struct work_struct work; |
| 1618 | struct mm_struct *mm; |
| 1619 | nodemask_t from; |
| 1620 | nodemask_t to; |
| 1621 | }; |
| 1622 | |
| 1623 | static void cpuset_migrate_mm_workfn(struct work_struct *work) |
| 1624 | { |
| 1625 | struct cpuset_migrate_mm_work *mwork = |
| 1626 | container_of(work, struct cpuset_migrate_mm_work, work); |
| 1627 | |
| 1628 | /* on a wq worker, no need to worry about %current's mems_allowed */ |
| 1629 | do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL); |
| 1630 | mmput(mwork->mm); |
| 1631 | kfree(mwork); |
| 1632 | } |
| 1633 | |
| 1634 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, |
| 1635 | const nodemask_t *to) |
| 1636 | { |
| 1637 | struct cpuset_migrate_mm_work *mwork; |
| 1638 | |
| 1639 | mwork = kzalloc(sizeof(*mwork), GFP_KERNEL); |
| 1640 | if (mwork) { |
| 1641 | mwork->mm = mm; |
| 1642 | mwork->from = *from; |
| 1643 | mwork->to = *to; |
| 1644 | INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn); |
| 1645 | queue_work(cpuset_migrate_mm_wq, &mwork->work); |
| 1646 | } else { |
| 1647 | mmput(mm); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | static void cpuset_post_attach(void) |
| 1652 | { |
| 1653 | flush_workqueue(cpuset_migrate_mm_wq); |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy |
| 1658 | * @tsk: the task to change |
| 1659 | * @newmems: new nodes that the task will be set |
| 1660 | * |
| 1661 | * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed |
| 1662 | * and rebind an eventual tasks' mempolicy. If the task is allocating in |
| 1663 | * parallel, it might temporarily see an empty intersection, which results in |
| 1664 | * a seqlock check and retry before OOM or allocation failure. |
| 1665 | */ |
| 1666 | static void cpuset_change_task_nodemask(struct task_struct *tsk, |
| 1667 | nodemask_t *newmems) |
| 1668 | { |
| 1669 | task_lock(tsk); |
| 1670 | |
| 1671 | local_irq_disable(); |
| 1672 | write_seqcount_begin(&tsk->mems_allowed_seq); |
| 1673 | |
| 1674 | nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); |
| 1675 | mpol_rebind_task(tsk, newmems); |
| 1676 | tsk->mems_allowed = *newmems; |
| 1677 | |
| 1678 | write_seqcount_end(&tsk->mems_allowed_seq); |
| 1679 | local_irq_enable(); |
| 1680 | |
| 1681 | task_unlock(tsk); |
| 1682 | } |
| 1683 | |
| 1684 | static void *cpuset_being_rebound; |
| 1685 | |
| 1686 | /** |
| 1687 | * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. |
| 1688 | * @cs: the cpuset in which each task's mems_allowed mask needs to be changed |
| 1689 | * |
| 1690 | * Iterate through each task of @cs updating its mems_allowed to the |
| 1691 | * effective cpuset's. As this function is called with cpuset_mutex held, |
| 1692 | * cpuset membership stays stable. |
| 1693 | */ |
| 1694 | static void update_tasks_nodemask(struct cpuset *cs) |
| 1695 | { |
| 1696 | static nodemask_t newmems; /* protected by cpuset_mutex */ |
| 1697 | struct css_task_iter it; |
| 1698 | struct task_struct *task; |
| 1699 | |
| 1700 | cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ |
| 1701 | |
| 1702 | guarantee_online_mems(cs, &newmems); |
| 1703 | |
| 1704 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1705 | * The mpol_rebind_mm() call takes mmap_lock, which we couldn't |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1706 | * take while holding tasklist_lock. Forks can happen - the |
| 1707 | * mpol_dup() cpuset_being_rebound check will catch such forks, |
| 1708 | * and rebind their vma mempolicies too. Because we still hold |
| 1709 | * the global cpuset_mutex, we know that no other rebind effort |
| 1710 | * will be contending for the global variable cpuset_being_rebound. |
| 1711 | * It's ok if we rebind the same mm twice; mpol_rebind_mm() |
| 1712 | * is idempotent. Also migrate pages in each mm to new nodes. |
| 1713 | */ |
| 1714 | css_task_iter_start(&cs->css, 0, &it); |
| 1715 | while ((task = css_task_iter_next(&it))) { |
| 1716 | struct mm_struct *mm; |
| 1717 | bool migrate; |
| 1718 | |
| 1719 | cpuset_change_task_nodemask(task, &newmems); |
| 1720 | |
| 1721 | mm = get_task_mm(task); |
| 1722 | if (!mm) |
| 1723 | continue; |
| 1724 | |
| 1725 | migrate = is_memory_migrate(cs); |
| 1726 | |
| 1727 | mpol_rebind_mm(mm, &cs->mems_allowed); |
| 1728 | if (migrate) |
| 1729 | cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems); |
| 1730 | else |
| 1731 | mmput(mm); |
| 1732 | } |
| 1733 | css_task_iter_end(&it); |
| 1734 | |
| 1735 | /* |
| 1736 | * All the tasks' nodemasks have been updated, update |
| 1737 | * cs->old_mems_allowed. |
| 1738 | */ |
| 1739 | cs->old_mems_allowed = newmems; |
| 1740 | |
| 1741 | /* We're done rebinding vmas to this cpuset's new mems_allowed. */ |
| 1742 | cpuset_being_rebound = NULL; |
| 1743 | } |
| 1744 | |
| 1745 | /* |
| 1746 | * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree |
| 1747 | * @cs: the cpuset to consider |
| 1748 | * @new_mems: a temp variable for calculating new effective_mems |
| 1749 | * |
| 1750 | * When configured nodemask is changed, the effective nodemasks of this cpuset |
| 1751 | * and all its descendants need to be updated. |
| 1752 | * |
| 1753 | * On legacy hiearchy, effective_mems will be the same with mems_allowed. |
| 1754 | * |
| 1755 | * Called with cpuset_mutex held |
| 1756 | */ |
| 1757 | static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) |
| 1758 | { |
| 1759 | struct cpuset *cp; |
| 1760 | struct cgroup_subsys_state *pos_css; |
| 1761 | |
| 1762 | rcu_read_lock(); |
| 1763 | cpuset_for_each_descendant_pre(cp, pos_css, cs) { |
| 1764 | struct cpuset *parent = parent_cs(cp); |
| 1765 | |
| 1766 | nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems); |
| 1767 | |
| 1768 | /* |
| 1769 | * If it becomes empty, inherit the effective mask of the |
| 1770 | * parent, which is guaranteed to have some MEMs. |
| 1771 | */ |
| 1772 | if (is_in_v2_mode() && nodes_empty(*new_mems)) |
| 1773 | *new_mems = parent->effective_mems; |
| 1774 | |
| 1775 | /* Skip the whole subtree if the nodemask remains the same. */ |
| 1776 | if (nodes_equal(*new_mems, cp->effective_mems)) { |
| 1777 | pos_css = css_rightmost_descendant(pos_css); |
| 1778 | continue; |
| 1779 | } |
| 1780 | |
| 1781 | if (!css_tryget_online(&cp->css)) |
| 1782 | continue; |
| 1783 | rcu_read_unlock(); |
| 1784 | |
| 1785 | spin_lock_irq(&callback_lock); |
| 1786 | cp->effective_mems = *new_mems; |
| 1787 | spin_unlock_irq(&callback_lock); |
| 1788 | |
| 1789 | WARN_ON(!is_in_v2_mode() && |
| 1790 | !nodes_equal(cp->mems_allowed, cp->effective_mems)); |
| 1791 | |
| 1792 | update_tasks_nodemask(cp); |
| 1793 | |
| 1794 | rcu_read_lock(); |
| 1795 | css_put(&cp->css); |
| 1796 | } |
| 1797 | rcu_read_unlock(); |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * Handle user request to change the 'mems' memory placement |
| 1802 | * of a cpuset. Needs to validate the request, update the |
| 1803 | * cpusets mems_allowed, and for each task in the cpuset, |
| 1804 | * update mems_allowed and rebind task's mempolicy and any vma |
| 1805 | * mempolicies and if the cpuset is marked 'memory_migrate', |
| 1806 | * migrate the tasks pages to the new memory. |
| 1807 | * |
| 1808 | * Call with cpuset_mutex held. May take callback_lock during call. |
| 1809 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1810 | * lock each such tasks mm->mmap_lock, scan its vma's and rebind |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1811 | * their mempolicies to the cpusets new mems_allowed. |
| 1812 | */ |
| 1813 | static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, |
| 1814 | const char *buf) |
| 1815 | { |
| 1816 | int retval; |
| 1817 | |
| 1818 | /* |
| 1819 | * top_cpuset.mems_allowed tracks node_stats[N_MEMORY]; |
| 1820 | * it's read-only |
| 1821 | */ |
| 1822 | if (cs == &top_cpuset) { |
| 1823 | retval = -EACCES; |
| 1824 | goto done; |
| 1825 | } |
| 1826 | |
| 1827 | /* |
| 1828 | * An empty mems_allowed is ok iff there are no tasks in the cpuset. |
| 1829 | * Since nodelist_parse() fails on an empty mask, we special case |
| 1830 | * that parsing. The validate_change() call ensures that cpusets |
| 1831 | * with tasks have memory. |
| 1832 | */ |
| 1833 | if (!*buf) { |
| 1834 | nodes_clear(trialcs->mems_allowed); |
| 1835 | } else { |
| 1836 | retval = nodelist_parse(buf, trialcs->mems_allowed); |
| 1837 | if (retval < 0) |
| 1838 | goto done; |
| 1839 | |
| 1840 | if (!nodes_subset(trialcs->mems_allowed, |
| 1841 | top_cpuset.mems_allowed)) { |
| 1842 | retval = -EINVAL; |
| 1843 | goto done; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) { |
| 1848 | retval = 0; /* Too easy - nothing to do */ |
| 1849 | goto done; |
| 1850 | } |
| 1851 | retval = validate_change(cs, trialcs); |
| 1852 | if (retval < 0) |
| 1853 | goto done; |
| 1854 | |
| 1855 | spin_lock_irq(&callback_lock); |
| 1856 | cs->mems_allowed = trialcs->mems_allowed; |
| 1857 | spin_unlock_irq(&callback_lock); |
| 1858 | |
| 1859 | /* use trialcs->mems_allowed as a temp variable */ |
| 1860 | update_nodemasks_hier(cs, &trialcs->mems_allowed); |
| 1861 | done: |
| 1862 | return retval; |
| 1863 | } |
| 1864 | |
| 1865 | bool current_cpuset_is_being_rebound(void) |
| 1866 | { |
| 1867 | bool ret; |
| 1868 | |
| 1869 | rcu_read_lock(); |
| 1870 | ret = task_cs(current) == cpuset_being_rebound; |
| 1871 | rcu_read_unlock(); |
| 1872 | |
| 1873 | return ret; |
| 1874 | } |
| 1875 | |
| 1876 | static int update_relax_domain_level(struct cpuset *cs, s64 val) |
| 1877 | { |
| 1878 | #ifdef CONFIG_SMP |
| 1879 | if (val < -1 || val >= sched_domain_level_max) |
| 1880 | return -EINVAL; |
| 1881 | #endif |
| 1882 | |
| 1883 | if (val != cs->relax_domain_level) { |
| 1884 | cs->relax_domain_level = val; |
| 1885 | if (!cpumask_empty(cs->cpus_allowed) && |
| 1886 | is_sched_load_balance(cs)) |
| 1887 | rebuild_sched_domains_locked(); |
| 1888 | } |
| 1889 | |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | /** |
| 1894 | * update_tasks_flags - update the spread flags of tasks in the cpuset. |
| 1895 | * @cs: the cpuset in which each task's spread flags needs to be changed |
| 1896 | * |
| 1897 | * Iterate through each task of @cs updating its spread flags. As this |
| 1898 | * function is called with cpuset_mutex held, cpuset membership stays |
| 1899 | * stable. |
| 1900 | */ |
| 1901 | static void update_tasks_flags(struct cpuset *cs) |
| 1902 | { |
| 1903 | struct css_task_iter it; |
| 1904 | struct task_struct *task; |
| 1905 | |
| 1906 | css_task_iter_start(&cs->css, 0, &it); |
| 1907 | while ((task = css_task_iter_next(&it))) |
| 1908 | cpuset_update_task_spread_flag(cs, task); |
| 1909 | css_task_iter_end(&it); |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * update_flag - read a 0 or a 1 in a file and update associated flag |
| 1914 | * bit: the bit to update (see cpuset_flagbits_t) |
| 1915 | * cs: the cpuset to update |
| 1916 | * turning_on: whether the flag is being set or cleared |
| 1917 | * |
| 1918 | * Call with cpuset_mutex held. |
| 1919 | */ |
| 1920 | |
| 1921 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, |
| 1922 | int turning_on) |
| 1923 | { |
| 1924 | struct cpuset *trialcs; |
| 1925 | int balance_flag_changed; |
| 1926 | int spread_flag_changed; |
| 1927 | int err; |
| 1928 | |
| 1929 | trialcs = alloc_trial_cpuset(cs); |
| 1930 | if (!trialcs) |
| 1931 | return -ENOMEM; |
| 1932 | |
| 1933 | if (turning_on) |
| 1934 | set_bit(bit, &trialcs->flags); |
| 1935 | else |
| 1936 | clear_bit(bit, &trialcs->flags); |
| 1937 | |
| 1938 | err = validate_change(cs, trialcs); |
| 1939 | if (err < 0) |
| 1940 | goto out; |
| 1941 | |
| 1942 | balance_flag_changed = (is_sched_load_balance(cs) != |
| 1943 | is_sched_load_balance(trialcs)); |
| 1944 | |
| 1945 | spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs)) |
| 1946 | || (is_spread_page(cs) != is_spread_page(trialcs))); |
| 1947 | |
| 1948 | spin_lock_irq(&callback_lock); |
| 1949 | cs->flags = trialcs->flags; |
| 1950 | spin_unlock_irq(&callback_lock); |
| 1951 | |
| 1952 | if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) |
| 1953 | rebuild_sched_domains_locked(); |
| 1954 | |
| 1955 | if (spread_flag_changed) |
| 1956 | update_tasks_flags(cs); |
| 1957 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1958 | free_cpuset(trialcs); |
| 1959 | return err; |
| 1960 | } |
| 1961 | |
| 1962 | /* |
| 1963 | * update_prstate - update partititon_root_state |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1964 | * cs: the cpuset to update |
| 1965 | * new_prs: new partition root state |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1966 | * |
| 1967 | * Call with cpuset_mutex held. |
| 1968 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1969 | static int update_prstate(struct cpuset *cs, int new_prs) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1970 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1971 | int err, old_prs = cs->partition_root_state; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1972 | struct cpuset *parent = parent_cs(cs); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1973 | struct tmpmasks tmpmask; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1974 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1975 | if (old_prs == new_prs) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1976 | return 0; |
| 1977 | |
| 1978 | /* |
| 1979 | * Cannot force a partial or invalid partition root to a full |
| 1980 | * partition root. |
| 1981 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1982 | if (new_prs && (old_prs == PRS_ERROR)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1983 | return -EINVAL; |
| 1984 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1985 | if (alloc_cpumasks(NULL, &tmpmask)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1986 | return -ENOMEM; |
| 1987 | |
| 1988 | err = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1989 | if (!old_prs) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1990 | /* |
| 1991 | * Turning on partition root requires setting the |
| 1992 | * CS_CPU_EXCLUSIVE bit implicitly as well and cpus_allowed |
| 1993 | * cannot be NULL. |
| 1994 | */ |
| 1995 | if (cpumask_empty(cs->cpus_allowed)) |
| 1996 | goto out; |
| 1997 | |
| 1998 | err = update_flag(CS_CPU_EXCLUSIVE, cs, 1); |
| 1999 | if (err) |
| 2000 | goto out; |
| 2001 | |
| 2002 | err = update_parent_subparts_cpumask(cs, partcmd_enable, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2003 | NULL, &tmpmask); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2004 | if (err) { |
| 2005 | update_flag(CS_CPU_EXCLUSIVE, cs, 0); |
| 2006 | goto out; |
| 2007 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2008 | } else { |
| 2009 | /* |
| 2010 | * Turning off partition root will clear the |
| 2011 | * CS_CPU_EXCLUSIVE bit. |
| 2012 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2013 | if (old_prs == PRS_ERROR) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2014 | update_flag(CS_CPU_EXCLUSIVE, cs, 0); |
| 2015 | err = 0; |
| 2016 | goto out; |
| 2017 | } |
| 2018 | |
| 2019 | err = update_parent_subparts_cpumask(cs, partcmd_disable, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2020 | NULL, &tmpmask); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2021 | if (err) |
| 2022 | goto out; |
| 2023 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2024 | /* Turning off CS_CPU_EXCLUSIVE will not return error */ |
| 2025 | update_flag(CS_CPU_EXCLUSIVE, cs, 0); |
| 2026 | } |
| 2027 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 2028 | update_tasks_cpumask(parent); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2029 | |
| 2030 | if (parent->child_ecpus_count) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2031 | update_sibling_cpumasks(parent, cs, &tmpmask); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2032 | |
| 2033 | rebuild_sched_domains_locked(); |
| 2034 | out: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2035 | if (!err) { |
| 2036 | spin_lock_irq(&callback_lock); |
| 2037 | cs->partition_root_state = new_prs; |
| 2038 | spin_unlock_irq(&callback_lock); |
| 2039 | } |
| 2040 | |
| 2041 | free_cpumasks(NULL, &tmpmask); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2042 | return err; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Frequency meter - How fast is some event occurring? |
| 2047 | * |
| 2048 | * These routines manage a digitally filtered, constant time based, |
| 2049 | * event frequency meter. There are four routines: |
| 2050 | * fmeter_init() - initialize a frequency meter. |
| 2051 | * fmeter_markevent() - called each time the event happens. |
| 2052 | * fmeter_getrate() - returns the recent rate of such events. |
| 2053 | * fmeter_update() - internal routine used to update fmeter. |
| 2054 | * |
| 2055 | * A common data structure is passed to each of these routines, |
| 2056 | * which is used to keep track of the state required to manage the |
| 2057 | * frequency meter and its digital filter. |
| 2058 | * |
| 2059 | * The filter works on the number of events marked per unit time. |
| 2060 | * The filter is single-pole low-pass recursive (IIR). The time unit |
| 2061 | * is 1 second. Arithmetic is done using 32-bit integers scaled to |
| 2062 | * simulate 3 decimal digits of precision (multiplied by 1000). |
| 2063 | * |
| 2064 | * With an FM_COEF of 933, and a time base of 1 second, the filter |
| 2065 | * has a half-life of 10 seconds, meaning that if the events quit |
| 2066 | * happening, then the rate returned from the fmeter_getrate() |
| 2067 | * will be cut in half each 10 seconds, until it converges to zero. |
| 2068 | * |
| 2069 | * It is not worth doing a real infinitely recursive filter. If more |
| 2070 | * than FM_MAXTICKS ticks have elapsed since the last filter event, |
| 2071 | * just compute FM_MAXTICKS ticks worth, by which point the level |
| 2072 | * will be stable. |
| 2073 | * |
| 2074 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid |
| 2075 | * arithmetic overflow in the fmeter_update() routine. |
| 2076 | * |
| 2077 | * Given the simple 32 bit integer arithmetic used, this meter works |
| 2078 | * best for reporting rates between one per millisecond (msec) and |
| 2079 | * one per 32 (approx) seconds. At constant rates faster than one |
| 2080 | * per msec it maxes out at values just under 1,000,000. At constant |
| 2081 | * rates between one per msec, and one per second it will stabilize |
| 2082 | * to a value N*1000, where N is the rate of events per second. |
| 2083 | * At constant rates between one per second and one per 32 seconds, |
| 2084 | * it will be choppy, moving up on the seconds that have an event, |
| 2085 | * and then decaying until the next event. At rates slower than |
| 2086 | * about one in 32 seconds, it decays all the way back to zero between |
| 2087 | * each event. |
| 2088 | */ |
| 2089 | |
| 2090 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ |
| 2091 | #define FM_MAXTICKS ((u32)99) /* useless computing more ticks than this */ |
| 2092 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ |
| 2093 | #define FM_SCALE 1000 /* faux fixed point scale */ |
| 2094 | |
| 2095 | /* Initialize a frequency meter */ |
| 2096 | static void fmeter_init(struct fmeter *fmp) |
| 2097 | { |
| 2098 | fmp->cnt = 0; |
| 2099 | fmp->val = 0; |
| 2100 | fmp->time = 0; |
| 2101 | spin_lock_init(&fmp->lock); |
| 2102 | } |
| 2103 | |
| 2104 | /* Internal meter update - process cnt events and update value */ |
| 2105 | static void fmeter_update(struct fmeter *fmp) |
| 2106 | { |
| 2107 | time64_t now; |
| 2108 | u32 ticks; |
| 2109 | |
| 2110 | now = ktime_get_seconds(); |
| 2111 | ticks = now - fmp->time; |
| 2112 | |
| 2113 | if (ticks == 0) |
| 2114 | return; |
| 2115 | |
| 2116 | ticks = min(FM_MAXTICKS, ticks); |
| 2117 | while (ticks-- > 0) |
| 2118 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; |
| 2119 | fmp->time = now; |
| 2120 | |
| 2121 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; |
| 2122 | fmp->cnt = 0; |
| 2123 | } |
| 2124 | |
| 2125 | /* Process any previous ticks, then bump cnt by one (times scale). */ |
| 2126 | static void fmeter_markevent(struct fmeter *fmp) |
| 2127 | { |
| 2128 | spin_lock(&fmp->lock); |
| 2129 | fmeter_update(fmp); |
| 2130 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); |
| 2131 | spin_unlock(&fmp->lock); |
| 2132 | } |
| 2133 | |
| 2134 | /* Process any previous ticks, then return current value. */ |
| 2135 | static int fmeter_getrate(struct fmeter *fmp) |
| 2136 | { |
| 2137 | int val; |
| 2138 | |
| 2139 | spin_lock(&fmp->lock); |
| 2140 | fmeter_update(fmp); |
| 2141 | val = fmp->val; |
| 2142 | spin_unlock(&fmp->lock); |
| 2143 | return val; |
| 2144 | } |
| 2145 | |
| 2146 | static struct cpuset *cpuset_attach_old_cs; |
| 2147 | |
| 2148 | /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */ |
| 2149 | static int cpuset_can_attach(struct cgroup_taskset *tset) |
| 2150 | { |
| 2151 | struct cgroup_subsys_state *css; |
| 2152 | struct cpuset *cs; |
| 2153 | struct task_struct *task; |
| 2154 | int ret; |
| 2155 | |
| 2156 | /* used later by cpuset_attach() */ |
| 2157 | cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css)); |
| 2158 | cs = css_cs(css); |
| 2159 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2160 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2161 | |
| 2162 | /* allow moving tasks into an empty cpuset if on default hierarchy */ |
| 2163 | ret = -ENOSPC; |
| 2164 | if (!is_in_v2_mode() && |
| 2165 | (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))) |
| 2166 | goto out_unlock; |
| 2167 | |
| 2168 | cgroup_taskset_for_each(task, css, tset) { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 2169 | ret = task_can_attach(task, cs->effective_cpus); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2170 | if (ret) |
| 2171 | goto out_unlock; |
| 2172 | ret = security_task_setscheduler(task); |
| 2173 | if (ret) |
| 2174 | goto out_unlock; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Mark attach is in progress. This makes validate_change() fail |
| 2179 | * changes which zero cpus/mems_allowed. |
| 2180 | */ |
| 2181 | cs->attach_in_progress++; |
| 2182 | ret = 0; |
| 2183 | out_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2184 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2185 | return ret; |
| 2186 | } |
| 2187 | |
| 2188 | static void cpuset_cancel_attach(struct cgroup_taskset *tset) |
| 2189 | { |
| 2190 | struct cgroup_subsys_state *css; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2191 | |
| 2192 | cgroup_taskset_first(tset, &css); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2193 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2194 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2195 | css_cs(css)->attach_in_progress--; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2196 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach() |
| 2201 | * but we can't allocate it dynamically there. Define it global and |
| 2202 | * allocate from cpuset_init(). |
| 2203 | */ |
| 2204 | static cpumask_var_t cpus_attach; |
| 2205 | |
| 2206 | static void cpuset_attach(struct cgroup_taskset *tset) |
| 2207 | { |
| 2208 | /* static buf protected by cpuset_mutex */ |
| 2209 | static nodemask_t cpuset_attach_nodemask_to; |
| 2210 | struct task_struct *task; |
| 2211 | struct task_struct *leader; |
| 2212 | struct cgroup_subsys_state *css; |
| 2213 | struct cpuset *cs; |
| 2214 | struct cpuset *oldcs = cpuset_attach_old_cs; |
| 2215 | |
| 2216 | cgroup_taskset_first(tset, &css); |
| 2217 | cs = css_cs(css); |
| 2218 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 2219 | lockdep_assert_cpus_held(); /* see cgroup_attach_lock() */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2220 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2221 | |
| 2222 | /* prepare for attach */ |
| 2223 | if (cs == &top_cpuset) |
| 2224 | cpumask_copy(cpus_attach, cpu_possible_mask); |
| 2225 | else |
| 2226 | guarantee_online_cpus(cs, cpus_attach); |
| 2227 | |
| 2228 | guarantee_online_mems(cs, &cpuset_attach_nodemask_to); |
| 2229 | |
| 2230 | cgroup_taskset_for_each(task, css, tset) { |
| 2231 | /* |
| 2232 | * can_attach beforehand should guarantee that this doesn't |
| 2233 | * fail. TODO: have a better way to handle failure here |
| 2234 | */ |
| 2235 | WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); |
| 2236 | |
| 2237 | cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); |
| 2238 | cpuset_update_task_spread_flag(cs, task); |
| 2239 | } |
| 2240 | |
| 2241 | /* |
| 2242 | * Change mm for all threadgroup leaders. This is expensive and may |
| 2243 | * sleep and should be moved outside migration path proper. |
| 2244 | */ |
| 2245 | cpuset_attach_nodemask_to = cs->effective_mems; |
| 2246 | cgroup_taskset_for_each_leader(leader, css, tset) { |
| 2247 | struct mm_struct *mm = get_task_mm(leader); |
| 2248 | |
| 2249 | if (mm) { |
| 2250 | mpol_rebind_mm(mm, &cpuset_attach_nodemask_to); |
| 2251 | |
| 2252 | /* |
| 2253 | * old_mems_allowed is the same with mems_allowed |
| 2254 | * here, except if this task is being moved |
| 2255 | * automatically due to hotplug. In that case |
| 2256 | * @mems_allowed has been updated and is empty, so |
| 2257 | * @old_mems_allowed is the right nodesets that we |
| 2258 | * migrate mm from. |
| 2259 | */ |
| 2260 | if (is_memory_migrate(cs)) |
| 2261 | cpuset_migrate_mm(mm, &oldcs->old_mems_allowed, |
| 2262 | &cpuset_attach_nodemask_to); |
| 2263 | else |
| 2264 | mmput(mm); |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | cs->old_mems_allowed = cpuset_attach_nodemask_to; |
| 2269 | |
| 2270 | cs->attach_in_progress--; |
| 2271 | if (!cs->attach_in_progress) |
| 2272 | wake_up(&cpuset_attach_wq); |
| 2273 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2274 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* The various types of files and directories in a cpuset file system */ |
| 2278 | |
| 2279 | typedef enum { |
| 2280 | FILE_MEMORY_MIGRATE, |
| 2281 | FILE_CPULIST, |
| 2282 | FILE_MEMLIST, |
| 2283 | FILE_EFFECTIVE_CPULIST, |
| 2284 | FILE_EFFECTIVE_MEMLIST, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2285 | FILE_SUBPARTS_CPULIST, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2286 | FILE_CPU_EXCLUSIVE, |
| 2287 | FILE_MEM_EXCLUSIVE, |
| 2288 | FILE_MEM_HARDWALL, |
| 2289 | FILE_SCHED_LOAD_BALANCE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2290 | FILE_PARTITION_ROOT, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2291 | FILE_SCHED_RELAX_DOMAIN_LEVEL, |
| 2292 | FILE_MEMORY_PRESSURE_ENABLED, |
| 2293 | FILE_MEMORY_PRESSURE, |
| 2294 | FILE_SPREAD_PAGE, |
| 2295 | FILE_SPREAD_SLAB, |
| 2296 | } cpuset_filetype_t; |
| 2297 | |
| 2298 | static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft, |
| 2299 | u64 val) |
| 2300 | { |
| 2301 | struct cpuset *cs = css_cs(css); |
| 2302 | cpuset_filetype_t type = cft->private; |
| 2303 | int retval = 0; |
| 2304 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2305 | get_online_cpus(); |
| 2306 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2307 | if (!is_cpuset_online(cs)) { |
| 2308 | retval = -ENODEV; |
| 2309 | goto out_unlock; |
| 2310 | } |
| 2311 | |
| 2312 | switch (type) { |
| 2313 | case FILE_CPU_EXCLUSIVE: |
| 2314 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, val); |
| 2315 | break; |
| 2316 | case FILE_MEM_EXCLUSIVE: |
| 2317 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, val); |
| 2318 | break; |
| 2319 | case FILE_MEM_HARDWALL: |
| 2320 | retval = update_flag(CS_MEM_HARDWALL, cs, val); |
| 2321 | break; |
| 2322 | case FILE_SCHED_LOAD_BALANCE: |
| 2323 | retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val); |
| 2324 | break; |
| 2325 | case FILE_MEMORY_MIGRATE: |
| 2326 | retval = update_flag(CS_MEMORY_MIGRATE, cs, val); |
| 2327 | break; |
| 2328 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 2329 | cpuset_memory_pressure_enabled = !!val; |
| 2330 | break; |
| 2331 | case FILE_SPREAD_PAGE: |
| 2332 | retval = update_flag(CS_SPREAD_PAGE, cs, val); |
| 2333 | break; |
| 2334 | case FILE_SPREAD_SLAB: |
| 2335 | retval = update_flag(CS_SPREAD_SLAB, cs, val); |
| 2336 | break; |
| 2337 | default: |
| 2338 | retval = -EINVAL; |
| 2339 | break; |
| 2340 | } |
| 2341 | out_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2342 | percpu_up_write(&cpuset_rwsem); |
| 2343 | put_online_cpus(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2344 | return retval; |
| 2345 | } |
| 2346 | |
| 2347 | static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft, |
| 2348 | s64 val) |
| 2349 | { |
| 2350 | struct cpuset *cs = css_cs(css); |
| 2351 | cpuset_filetype_t type = cft->private; |
| 2352 | int retval = -ENODEV; |
| 2353 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2354 | get_online_cpus(); |
| 2355 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2356 | if (!is_cpuset_online(cs)) |
| 2357 | goto out_unlock; |
| 2358 | |
| 2359 | switch (type) { |
| 2360 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: |
| 2361 | retval = update_relax_domain_level(cs, val); |
| 2362 | break; |
| 2363 | default: |
| 2364 | retval = -EINVAL; |
| 2365 | break; |
| 2366 | } |
| 2367 | out_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2368 | percpu_up_write(&cpuset_rwsem); |
| 2369 | put_online_cpus(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2370 | return retval; |
| 2371 | } |
| 2372 | |
| 2373 | /* |
| 2374 | * Common handling for a write to a "cpus" or "mems" file. |
| 2375 | */ |
| 2376 | static ssize_t cpuset_write_resmask(struct kernfs_open_file *of, |
| 2377 | char *buf, size_t nbytes, loff_t off) |
| 2378 | { |
| 2379 | struct cpuset *cs = css_cs(of_css(of)); |
| 2380 | struct cpuset *trialcs; |
| 2381 | int retval = -ENODEV; |
| 2382 | |
| 2383 | buf = strstrip(buf); |
| 2384 | |
| 2385 | /* |
| 2386 | * CPU or memory hotunplug may leave @cs w/o any execution |
| 2387 | * resources, in which case the hotplug code asynchronously updates |
| 2388 | * configuration and transfers all tasks to the nearest ancestor |
| 2389 | * which can execute. |
| 2390 | * |
| 2391 | * As writes to "cpus" or "mems" may restore @cs's execution |
| 2392 | * resources, wait for the previously scheduled operations before |
| 2393 | * proceeding, so that we don't end up keep removing tasks added |
| 2394 | * after execution capability is restored. |
| 2395 | * |
| 2396 | * cpuset_hotplug_work calls back into cgroup core via |
| 2397 | * cgroup_transfer_tasks() and waiting for it from a cgroupfs |
| 2398 | * operation like this one can lead to a deadlock through kernfs |
| 2399 | * active_ref protection. Let's break the protection. Losing the |
| 2400 | * protection is okay as we check whether @cs is online after |
| 2401 | * grabbing cpuset_mutex anyway. This only happens on the legacy |
| 2402 | * hierarchies. |
| 2403 | */ |
| 2404 | css_get(&cs->css); |
| 2405 | kernfs_break_active_protection(of->kn); |
| 2406 | flush_work(&cpuset_hotplug_work); |
| 2407 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2408 | get_online_cpus(); |
| 2409 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2410 | if (!is_cpuset_online(cs)) |
| 2411 | goto out_unlock; |
| 2412 | |
| 2413 | trialcs = alloc_trial_cpuset(cs); |
| 2414 | if (!trialcs) { |
| 2415 | retval = -ENOMEM; |
| 2416 | goto out_unlock; |
| 2417 | } |
| 2418 | |
| 2419 | switch (of_cft(of)->private) { |
| 2420 | case FILE_CPULIST: |
| 2421 | retval = update_cpumask(cs, trialcs, buf); |
| 2422 | break; |
| 2423 | case FILE_MEMLIST: |
| 2424 | retval = update_nodemask(cs, trialcs, buf); |
| 2425 | break; |
| 2426 | default: |
| 2427 | retval = -EINVAL; |
| 2428 | break; |
| 2429 | } |
| 2430 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2431 | free_cpuset(trialcs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2432 | out_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2433 | percpu_up_write(&cpuset_rwsem); |
| 2434 | put_online_cpus(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2435 | kernfs_unbreak_active_protection(of->kn); |
| 2436 | css_put(&cs->css); |
| 2437 | flush_workqueue(cpuset_migrate_mm_wq); |
| 2438 | return retval ?: nbytes; |
| 2439 | } |
| 2440 | |
| 2441 | /* |
| 2442 | * These ascii lists should be read in a single call, by using a user |
| 2443 | * buffer large enough to hold the entire map. If read in smaller |
| 2444 | * chunks, there is no guarantee of atomicity. Since the display format |
| 2445 | * used, list of ranges of sequential numbers, is variable length, |
| 2446 | * and since these maps can change value dynamically, one could read |
| 2447 | * gibberish by doing partial reads while a list was changing. |
| 2448 | */ |
| 2449 | static int cpuset_common_seq_show(struct seq_file *sf, void *v) |
| 2450 | { |
| 2451 | struct cpuset *cs = css_cs(seq_css(sf)); |
| 2452 | cpuset_filetype_t type = seq_cft(sf)->private; |
| 2453 | int ret = 0; |
| 2454 | |
| 2455 | spin_lock_irq(&callback_lock); |
| 2456 | |
| 2457 | switch (type) { |
| 2458 | case FILE_CPULIST: |
| 2459 | seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed)); |
| 2460 | break; |
| 2461 | case FILE_MEMLIST: |
| 2462 | seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed)); |
| 2463 | break; |
| 2464 | case FILE_EFFECTIVE_CPULIST: |
| 2465 | seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus)); |
| 2466 | break; |
| 2467 | case FILE_EFFECTIVE_MEMLIST: |
| 2468 | seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems)); |
| 2469 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2470 | case FILE_SUBPARTS_CPULIST: |
| 2471 | seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->subparts_cpus)); |
| 2472 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2473 | default: |
| 2474 | ret = -EINVAL; |
| 2475 | } |
| 2476 | |
| 2477 | spin_unlock_irq(&callback_lock); |
| 2478 | return ret; |
| 2479 | } |
| 2480 | |
| 2481 | static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft) |
| 2482 | { |
| 2483 | struct cpuset *cs = css_cs(css); |
| 2484 | cpuset_filetype_t type = cft->private; |
| 2485 | switch (type) { |
| 2486 | case FILE_CPU_EXCLUSIVE: |
| 2487 | return is_cpu_exclusive(cs); |
| 2488 | case FILE_MEM_EXCLUSIVE: |
| 2489 | return is_mem_exclusive(cs); |
| 2490 | case FILE_MEM_HARDWALL: |
| 2491 | return is_mem_hardwall(cs); |
| 2492 | case FILE_SCHED_LOAD_BALANCE: |
| 2493 | return is_sched_load_balance(cs); |
| 2494 | case FILE_MEMORY_MIGRATE: |
| 2495 | return is_memory_migrate(cs); |
| 2496 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 2497 | return cpuset_memory_pressure_enabled; |
| 2498 | case FILE_MEMORY_PRESSURE: |
| 2499 | return fmeter_getrate(&cs->fmeter); |
| 2500 | case FILE_SPREAD_PAGE: |
| 2501 | return is_spread_page(cs); |
| 2502 | case FILE_SPREAD_SLAB: |
| 2503 | return is_spread_slab(cs); |
| 2504 | default: |
| 2505 | BUG(); |
| 2506 | } |
| 2507 | |
| 2508 | /* Unreachable but makes gcc happy */ |
| 2509 | return 0; |
| 2510 | } |
| 2511 | |
| 2512 | static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft) |
| 2513 | { |
| 2514 | struct cpuset *cs = css_cs(css); |
| 2515 | cpuset_filetype_t type = cft->private; |
| 2516 | switch (type) { |
| 2517 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: |
| 2518 | return cs->relax_domain_level; |
| 2519 | default: |
| 2520 | BUG(); |
| 2521 | } |
| 2522 | |
| 2523 | /* Unrechable but makes gcc happy */ |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2527 | static int sched_partition_show(struct seq_file *seq, void *v) |
| 2528 | { |
| 2529 | struct cpuset *cs = css_cs(seq_css(seq)); |
| 2530 | |
| 2531 | switch (cs->partition_root_state) { |
| 2532 | case PRS_ENABLED: |
| 2533 | seq_puts(seq, "root\n"); |
| 2534 | break; |
| 2535 | case PRS_DISABLED: |
| 2536 | seq_puts(seq, "member\n"); |
| 2537 | break; |
| 2538 | case PRS_ERROR: |
| 2539 | seq_puts(seq, "root invalid\n"); |
| 2540 | break; |
| 2541 | } |
| 2542 | return 0; |
| 2543 | } |
| 2544 | |
| 2545 | static ssize_t sched_partition_write(struct kernfs_open_file *of, char *buf, |
| 2546 | size_t nbytes, loff_t off) |
| 2547 | { |
| 2548 | struct cpuset *cs = css_cs(of_css(of)); |
| 2549 | int val; |
| 2550 | int retval = -ENODEV; |
| 2551 | |
| 2552 | buf = strstrip(buf); |
| 2553 | |
| 2554 | /* |
| 2555 | * Convert "root" to ENABLED, and convert "member" to DISABLED. |
| 2556 | */ |
| 2557 | if (!strcmp(buf, "root")) |
| 2558 | val = PRS_ENABLED; |
| 2559 | else if (!strcmp(buf, "member")) |
| 2560 | val = PRS_DISABLED; |
| 2561 | else |
| 2562 | return -EINVAL; |
| 2563 | |
| 2564 | css_get(&cs->css); |
| 2565 | get_online_cpus(); |
| 2566 | percpu_down_write(&cpuset_rwsem); |
| 2567 | if (!is_cpuset_online(cs)) |
| 2568 | goto out_unlock; |
| 2569 | |
| 2570 | retval = update_prstate(cs, val); |
| 2571 | out_unlock: |
| 2572 | percpu_up_write(&cpuset_rwsem); |
| 2573 | put_online_cpus(); |
| 2574 | css_put(&cs->css); |
| 2575 | return retval ?: nbytes; |
| 2576 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2577 | |
| 2578 | /* |
| 2579 | * for the common functions, 'private' gives the type of file |
| 2580 | */ |
| 2581 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2582 | static struct cftype legacy_files[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2583 | { |
| 2584 | .name = "cpus", |
| 2585 | .seq_show = cpuset_common_seq_show, |
| 2586 | .write = cpuset_write_resmask, |
| 2587 | .max_write_len = (100U + 6 * NR_CPUS), |
| 2588 | .private = FILE_CPULIST, |
| 2589 | }, |
| 2590 | |
| 2591 | { |
| 2592 | .name = "mems", |
| 2593 | .seq_show = cpuset_common_seq_show, |
| 2594 | .write = cpuset_write_resmask, |
| 2595 | .max_write_len = (100U + 6 * MAX_NUMNODES), |
| 2596 | .private = FILE_MEMLIST, |
| 2597 | }, |
| 2598 | |
| 2599 | { |
| 2600 | .name = "effective_cpus", |
| 2601 | .seq_show = cpuset_common_seq_show, |
| 2602 | .private = FILE_EFFECTIVE_CPULIST, |
| 2603 | }, |
| 2604 | |
| 2605 | { |
| 2606 | .name = "effective_mems", |
| 2607 | .seq_show = cpuset_common_seq_show, |
| 2608 | .private = FILE_EFFECTIVE_MEMLIST, |
| 2609 | }, |
| 2610 | |
| 2611 | { |
| 2612 | .name = "cpu_exclusive", |
| 2613 | .read_u64 = cpuset_read_u64, |
| 2614 | .write_u64 = cpuset_write_u64, |
| 2615 | .private = FILE_CPU_EXCLUSIVE, |
| 2616 | }, |
| 2617 | |
| 2618 | { |
| 2619 | .name = "mem_exclusive", |
| 2620 | .read_u64 = cpuset_read_u64, |
| 2621 | .write_u64 = cpuset_write_u64, |
| 2622 | .private = FILE_MEM_EXCLUSIVE, |
| 2623 | }, |
| 2624 | |
| 2625 | { |
| 2626 | .name = "mem_hardwall", |
| 2627 | .read_u64 = cpuset_read_u64, |
| 2628 | .write_u64 = cpuset_write_u64, |
| 2629 | .private = FILE_MEM_HARDWALL, |
| 2630 | }, |
| 2631 | |
| 2632 | { |
| 2633 | .name = "sched_load_balance", |
| 2634 | .read_u64 = cpuset_read_u64, |
| 2635 | .write_u64 = cpuset_write_u64, |
| 2636 | .private = FILE_SCHED_LOAD_BALANCE, |
| 2637 | }, |
| 2638 | |
| 2639 | { |
| 2640 | .name = "sched_relax_domain_level", |
| 2641 | .read_s64 = cpuset_read_s64, |
| 2642 | .write_s64 = cpuset_write_s64, |
| 2643 | .private = FILE_SCHED_RELAX_DOMAIN_LEVEL, |
| 2644 | }, |
| 2645 | |
| 2646 | { |
| 2647 | .name = "memory_migrate", |
| 2648 | .read_u64 = cpuset_read_u64, |
| 2649 | .write_u64 = cpuset_write_u64, |
| 2650 | .private = FILE_MEMORY_MIGRATE, |
| 2651 | }, |
| 2652 | |
| 2653 | { |
| 2654 | .name = "memory_pressure", |
| 2655 | .read_u64 = cpuset_read_u64, |
| 2656 | .private = FILE_MEMORY_PRESSURE, |
| 2657 | }, |
| 2658 | |
| 2659 | { |
| 2660 | .name = "memory_spread_page", |
| 2661 | .read_u64 = cpuset_read_u64, |
| 2662 | .write_u64 = cpuset_write_u64, |
| 2663 | .private = FILE_SPREAD_PAGE, |
| 2664 | }, |
| 2665 | |
| 2666 | { |
| 2667 | .name = "memory_spread_slab", |
| 2668 | .read_u64 = cpuset_read_u64, |
| 2669 | .write_u64 = cpuset_write_u64, |
| 2670 | .private = FILE_SPREAD_SLAB, |
| 2671 | }, |
| 2672 | |
| 2673 | { |
| 2674 | .name = "memory_pressure_enabled", |
| 2675 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 2676 | .read_u64 = cpuset_read_u64, |
| 2677 | .write_u64 = cpuset_write_u64, |
| 2678 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
| 2679 | }, |
| 2680 | |
| 2681 | { } /* terminate */ |
| 2682 | }; |
| 2683 | |
| 2684 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2685 | * This is currently a minimal set for the default hierarchy. It can be |
| 2686 | * expanded later on by migrating more features and control files from v1. |
| 2687 | */ |
| 2688 | static struct cftype dfl_files[] = { |
| 2689 | { |
| 2690 | .name = "cpus", |
| 2691 | .seq_show = cpuset_common_seq_show, |
| 2692 | .write = cpuset_write_resmask, |
| 2693 | .max_write_len = (100U + 6 * NR_CPUS), |
| 2694 | .private = FILE_CPULIST, |
| 2695 | .flags = CFTYPE_NOT_ON_ROOT, |
| 2696 | }, |
| 2697 | |
| 2698 | { |
| 2699 | .name = "mems", |
| 2700 | .seq_show = cpuset_common_seq_show, |
| 2701 | .write = cpuset_write_resmask, |
| 2702 | .max_write_len = (100U + 6 * MAX_NUMNODES), |
| 2703 | .private = FILE_MEMLIST, |
| 2704 | .flags = CFTYPE_NOT_ON_ROOT, |
| 2705 | }, |
| 2706 | |
| 2707 | { |
| 2708 | .name = "cpus.effective", |
| 2709 | .seq_show = cpuset_common_seq_show, |
| 2710 | .private = FILE_EFFECTIVE_CPULIST, |
| 2711 | }, |
| 2712 | |
| 2713 | { |
| 2714 | .name = "mems.effective", |
| 2715 | .seq_show = cpuset_common_seq_show, |
| 2716 | .private = FILE_EFFECTIVE_MEMLIST, |
| 2717 | }, |
| 2718 | |
| 2719 | { |
| 2720 | .name = "cpus.partition", |
| 2721 | .seq_show = sched_partition_show, |
| 2722 | .write = sched_partition_write, |
| 2723 | .private = FILE_PARTITION_ROOT, |
| 2724 | .flags = CFTYPE_NOT_ON_ROOT, |
| 2725 | }, |
| 2726 | |
| 2727 | { |
| 2728 | .name = "cpus.subpartitions", |
| 2729 | .seq_show = cpuset_common_seq_show, |
| 2730 | .private = FILE_SUBPARTS_CPULIST, |
| 2731 | .flags = CFTYPE_DEBUG, |
| 2732 | }, |
| 2733 | |
| 2734 | { } /* terminate */ |
| 2735 | }; |
| 2736 | |
| 2737 | |
| 2738 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2739 | * cpuset_css_alloc - allocate a cpuset css |
| 2740 | * cgrp: control group that the new cpuset will be part of |
| 2741 | */ |
| 2742 | |
| 2743 | static struct cgroup_subsys_state * |
| 2744 | cpuset_css_alloc(struct cgroup_subsys_state *parent_css) |
| 2745 | { |
| 2746 | struct cpuset *cs; |
| 2747 | |
| 2748 | if (!parent_css) |
| 2749 | return &top_cpuset.css; |
| 2750 | |
| 2751 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
| 2752 | if (!cs) |
| 2753 | return ERR_PTR(-ENOMEM); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2754 | |
| 2755 | if (alloc_cpumasks(cs, NULL)) { |
| 2756 | kfree(cs); |
| 2757 | return ERR_PTR(-ENOMEM); |
| 2758 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2759 | |
| 2760 | set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2761 | nodes_clear(cs->mems_allowed); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2762 | nodes_clear(cs->effective_mems); |
| 2763 | fmeter_init(&cs->fmeter); |
| 2764 | cs->relax_domain_level = -1; |
| 2765 | |
| 2766 | return &cs->css; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | static int cpuset_css_online(struct cgroup_subsys_state *css) |
| 2770 | { |
| 2771 | struct cpuset *cs = css_cs(css); |
| 2772 | struct cpuset *parent = parent_cs(cs); |
| 2773 | struct cpuset *tmp_cs; |
| 2774 | struct cgroup_subsys_state *pos_css; |
| 2775 | |
| 2776 | if (!parent) |
| 2777 | return 0; |
| 2778 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2779 | get_online_cpus(); |
| 2780 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2781 | |
| 2782 | set_bit(CS_ONLINE, &cs->flags); |
| 2783 | if (is_spread_page(parent)) |
| 2784 | set_bit(CS_SPREAD_PAGE, &cs->flags); |
| 2785 | if (is_spread_slab(parent)) |
| 2786 | set_bit(CS_SPREAD_SLAB, &cs->flags); |
| 2787 | |
| 2788 | cpuset_inc(); |
| 2789 | |
| 2790 | spin_lock_irq(&callback_lock); |
| 2791 | if (is_in_v2_mode()) { |
| 2792 | cpumask_copy(cs->effective_cpus, parent->effective_cpus); |
| 2793 | cs->effective_mems = parent->effective_mems; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2794 | cs->use_parent_ecpus = true; |
| 2795 | parent->child_ecpus_count++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2796 | } |
| 2797 | spin_unlock_irq(&callback_lock); |
| 2798 | |
| 2799 | if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags)) |
| 2800 | goto out_unlock; |
| 2801 | |
| 2802 | /* |
| 2803 | * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is |
| 2804 | * set. This flag handling is implemented in cgroup core for |
| 2805 | * histrical reasons - the flag may be specified during mount. |
| 2806 | * |
| 2807 | * Currently, if any sibling cpusets have exclusive cpus or mem, we |
| 2808 | * refuse to clone the configuration - thereby refusing the task to |
| 2809 | * be entered, and as a result refusing the sys_unshare() or |
| 2810 | * clone() which initiated it. If this becomes a problem for some |
| 2811 | * users who wish to allow that scenario, then this could be |
| 2812 | * changed to grant parent->cpus_allowed-sibling_cpus_exclusive |
| 2813 | * (and likewise for mems) to the new cgroup. |
| 2814 | */ |
| 2815 | rcu_read_lock(); |
| 2816 | cpuset_for_each_child(tmp_cs, pos_css, parent) { |
| 2817 | if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) { |
| 2818 | rcu_read_unlock(); |
| 2819 | goto out_unlock; |
| 2820 | } |
| 2821 | } |
| 2822 | rcu_read_unlock(); |
| 2823 | |
| 2824 | spin_lock_irq(&callback_lock); |
| 2825 | cs->mems_allowed = parent->mems_allowed; |
| 2826 | cs->effective_mems = parent->mems_allowed; |
| 2827 | cpumask_copy(cs->cpus_allowed, parent->cpus_allowed); |
| 2828 | cpumask_copy(cs->effective_cpus, parent->cpus_allowed); |
| 2829 | spin_unlock_irq(&callback_lock); |
| 2830 | out_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2831 | percpu_up_write(&cpuset_rwsem); |
| 2832 | put_online_cpus(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2833 | return 0; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * If the cpuset being removed has its flag 'sched_load_balance' |
| 2838 | * enabled, then simulate turning sched_load_balance off, which |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2839 | * will call rebuild_sched_domains_locked(). That is not needed |
| 2840 | * in the default hierarchy where only changes in partition |
| 2841 | * will cause repartitioning. |
| 2842 | * |
| 2843 | * If the cpuset has the 'sched.partition' flag enabled, simulate |
| 2844 | * turning 'sched.partition" off. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2845 | */ |
| 2846 | |
| 2847 | static void cpuset_css_offline(struct cgroup_subsys_state *css) |
| 2848 | { |
| 2849 | struct cpuset *cs = css_cs(css); |
| 2850 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2851 | get_online_cpus(); |
| 2852 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2853 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2854 | if (is_partition_root(cs)) |
| 2855 | update_prstate(cs, 0); |
| 2856 | |
| 2857 | if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 2858 | is_sched_load_balance(cs)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2859 | update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); |
| 2860 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2861 | if (cs->use_parent_ecpus) { |
| 2862 | struct cpuset *parent = parent_cs(cs); |
| 2863 | |
| 2864 | cs->use_parent_ecpus = false; |
| 2865 | parent->child_ecpus_count--; |
| 2866 | } |
| 2867 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2868 | cpuset_dec(); |
| 2869 | clear_bit(CS_ONLINE, &cs->flags); |
| 2870 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2871 | percpu_up_write(&cpuset_rwsem); |
| 2872 | put_online_cpus(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | static void cpuset_css_free(struct cgroup_subsys_state *css) |
| 2876 | { |
| 2877 | struct cpuset *cs = css_cs(css); |
| 2878 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2879 | free_cpuset(cs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | static void cpuset_bind(struct cgroup_subsys_state *root_css) |
| 2883 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2884 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2885 | spin_lock_irq(&callback_lock); |
| 2886 | |
| 2887 | if (is_in_v2_mode()) { |
| 2888 | cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask); |
| 2889 | top_cpuset.mems_allowed = node_possible_map; |
| 2890 | } else { |
| 2891 | cpumask_copy(top_cpuset.cpus_allowed, |
| 2892 | top_cpuset.effective_cpus); |
| 2893 | top_cpuset.mems_allowed = top_cpuset.effective_mems; |
| 2894 | } |
| 2895 | |
| 2896 | spin_unlock_irq(&callback_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2897 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | /* |
| 2901 | * Make sure the new task conform to the current state of its parent, |
| 2902 | * which could have been changed by cpuset just after it inherits the |
| 2903 | * state from the parent and before it sits on the cgroup's task list. |
| 2904 | */ |
| 2905 | static void cpuset_fork(struct task_struct *task) |
| 2906 | { |
| 2907 | if (task_css_is_root(task, cpuset_cgrp_id)) |
| 2908 | return; |
| 2909 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2910 | set_cpus_allowed_ptr(task, current->cpus_ptr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2911 | task->mems_allowed = current->mems_allowed; |
| 2912 | } |
| 2913 | |
| 2914 | struct cgroup_subsys cpuset_cgrp_subsys = { |
| 2915 | .css_alloc = cpuset_css_alloc, |
| 2916 | .css_online = cpuset_css_online, |
| 2917 | .css_offline = cpuset_css_offline, |
| 2918 | .css_free = cpuset_css_free, |
| 2919 | .can_attach = cpuset_can_attach, |
| 2920 | .cancel_attach = cpuset_cancel_attach, |
| 2921 | .attach = cpuset_attach, |
| 2922 | .post_attach = cpuset_post_attach, |
| 2923 | .bind = cpuset_bind, |
| 2924 | .fork = cpuset_fork, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2925 | .legacy_cftypes = legacy_files, |
| 2926 | .dfl_cftypes = dfl_files, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2927 | .early_init = true, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2928 | .threaded = true, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2929 | }; |
| 2930 | |
| 2931 | /** |
| 2932 | * cpuset_init - initialize cpusets at system boot |
| 2933 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2934 | * Description: Initialize top_cpuset |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2935 | **/ |
| 2936 | |
| 2937 | int __init cpuset_init(void) |
| 2938 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2939 | BUG_ON(percpu_init_rwsem(&cpuset_rwsem)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2940 | |
| 2941 | BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)); |
| 2942 | BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2943 | BUG_ON(!zalloc_cpumask_var(&top_cpuset.subparts_cpus, GFP_KERNEL)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2944 | |
| 2945 | cpumask_setall(top_cpuset.cpus_allowed); |
| 2946 | nodes_setall(top_cpuset.mems_allowed); |
| 2947 | cpumask_setall(top_cpuset.effective_cpus); |
| 2948 | nodes_setall(top_cpuset.effective_mems); |
| 2949 | |
| 2950 | fmeter_init(&top_cpuset.fmeter); |
| 2951 | set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); |
| 2952 | top_cpuset.relax_domain_level = -1; |
| 2953 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2954 | BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)); |
| 2955 | |
| 2956 | return 0; |
| 2957 | } |
| 2958 | |
| 2959 | /* |
| 2960 | * If CPU and/or memory hotplug handlers, below, unplug any CPUs |
| 2961 | * or memory nodes, we need to walk over the cpuset hierarchy, |
| 2962 | * removing that CPU or node from all cpusets. If this removes the |
| 2963 | * last CPU or node from a cpuset, then move the tasks in the empty |
| 2964 | * cpuset to its next-highest non-empty parent. |
| 2965 | */ |
| 2966 | static void remove_tasks_in_empty_cpuset(struct cpuset *cs) |
| 2967 | { |
| 2968 | struct cpuset *parent; |
| 2969 | |
| 2970 | /* |
| 2971 | * Find its next-highest non-empty parent, (top cpuset |
| 2972 | * has online cpus, so can't be empty). |
| 2973 | */ |
| 2974 | parent = parent_cs(cs); |
| 2975 | while (cpumask_empty(parent->cpus_allowed) || |
| 2976 | nodes_empty(parent->mems_allowed)) |
| 2977 | parent = parent_cs(parent); |
| 2978 | |
| 2979 | if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) { |
| 2980 | pr_err("cpuset: failed to transfer tasks out of empty cpuset "); |
| 2981 | pr_cont_cgroup_name(cs->css.cgroup); |
| 2982 | pr_cont("\n"); |
| 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | static void |
| 2987 | hotplug_update_tasks_legacy(struct cpuset *cs, |
| 2988 | struct cpumask *new_cpus, nodemask_t *new_mems, |
| 2989 | bool cpus_updated, bool mems_updated) |
| 2990 | { |
| 2991 | bool is_empty; |
| 2992 | |
| 2993 | spin_lock_irq(&callback_lock); |
| 2994 | cpumask_copy(cs->cpus_allowed, new_cpus); |
| 2995 | cpumask_copy(cs->effective_cpus, new_cpus); |
| 2996 | cs->mems_allowed = *new_mems; |
| 2997 | cs->effective_mems = *new_mems; |
| 2998 | spin_unlock_irq(&callback_lock); |
| 2999 | |
| 3000 | /* |
| 3001 | * Don't call update_tasks_cpumask() if the cpuset becomes empty, |
| 3002 | * as the tasks will be migratecd to an ancestor. |
| 3003 | */ |
| 3004 | if (cpus_updated && !cpumask_empty(cs->cpus_allowed)) |
| 3005 | update_tasks_cpumask(cs); |
| 3006 | if (mems_updated && !nodes_empty(cs->mems_allowed)) |
| 3007 | update_tasks_nodemask(cs); |
| 3008 | |
| 3009 | is_empty = cpumask_empty(cs->cpus_allowed) || |
| 3010 | nodes_empty(cs->mems_allowed); |
| 3011 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3012 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3013 | |
| 3014 | /* |
| 3015 | * Move tasks to the nearest ancestor with execution resources, |
| 3016 | * This is full cgroup operation which will also call back into |
| 3017 | * cpuset. Should be done outside any lock. |
| 3018 | */ |
| 3019 | if (is_empty) |
| 3020 | remove_tasks_in_empty_cpuset(cs); |
| 3021 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3022 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | static void |
| 3026 | hotplug_update_tasks(struct cpuset *cs, |
| 3027 | struct cpumask *new_cpus, nodemask_t *new_mems, |
| 3028 | bool cpus_updated, bool mems_updated) |
| 3029 | { |
| 3030 | if (cpumask_empty(new_cpus)) |
| 3031 | cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus); |
| 3032 | if (nodes_empty(*new_mems)) |
| 3033 | *new_mems = parent_cs(cs)->effective_mems; |
| 3034 | |
| 3035 | spin_lock_irq(&callback_lock); |
| 3036 | cpumask_copy(cs->effective_cpus, new_cpus); |
| 3037 | cs->effective_mems = *new_mems; |
| 3038 | spin_unlock_irq(&callback_lock); |
| 3039 | |
| 3040 | if (cpus_updated) |
| 3041 | update_tasks_cpumask(cs); |
| 3042 | if (mems_updated) |
| 3043 | update_tasks_nodemask(cs); |
| 3044 | } |
| 3045 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3046 | static bool force_rebuild; |
| 3047 | |
| 3048 | void cpuset_force_rebuild(void) |
| 3049 | { |
| 3050 | force_rebuild = true; |
| 3051 | } |
| 3052 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3053 | /** |
| 3054 | * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug |
| 3055 | * @cs: cpuset in interest |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3056 | * @tmp: the tmpmasks structure pointer |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3057 | * |
| 3058 | * Compare @cs's cpu and mem masks against top_cpuset and if some have gone |
| 3059 | * offline, update @cs accordingly. If @cs ends up with no CPU or memory, |
| 3060 | * all its tasks are moved to the nearest ancestor with both resources. |
| 3061 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3062 | static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3063 | { |
| 3064 | static cpumask_t new_cpus; |
| 3065 | static nodemask_t new_mems; |
| 3066 | bool cpus_updated; |
| 3067 | bool mems_updated; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3068 | struct cpuset *parent; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3069 | retry: |
| 3070 | wait_event(cpuset_attach_wq, cs->attach_in_progress == 0); |
| 3071 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3072 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3073 | |
| 3074 | /* |
| 3075 | * We have raced with task attaching. We wait until attaching |
| 3076 | * is finished, so we won't attach a task to an empty cpuset. |
| 3077 | */ |
| 3078 | if (cs->attach_in_progress) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3079 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3080 | goto retry; |
| 3081 | } |
| 3082 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3083 | parent = parent_cs(cs); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3084 | compute_effective_cpumask(&new_cpus, cs, parent); |
| 3085 | nodes_and(new_mems, cs->mems_allowed, parent->effective_mems); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3086 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3087 | if (cs->nr_subparts_cpus) |
| 3088 | /* |
| 3089 | * Make sure that CPUs allocated to child partitions |
| 3090 | * do not show up in effective_cpus. |
| 3091 | */ |
| 3092 | cpumask_andnot(&new_cpus, &new_cpus, cs->subparts_cpus); |
| 3093 | |
| 3094 | if (!tmp || !cs->partition_root_state) |
| 3095 | goto update_tasks; |
| 3096 | |
| 3097 | /* |
| 3098 | * In the unlikely event that a partition root has empty |
| 3099 | * effective_cpus or its parent becomes erroneous, we have to |
| 3100 | * transition it to the erroneous state. |
| 3101 | */ |
| 3102 | if (is_partition_root(cs) && (cpumask_empty(&new_cpus) || |
| 3103 | (parent->partition_root_state == PRS_ERROR))) { |
| 3104 | if (cs->nr_subparts_cpus) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3105 | spin_lock_irq(&callback_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3106 | cs->nr_subparts_cpus = 0; |
| 3107 | cpumask_clear(cs->subparts_cpus); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3108 | spin_unlock_irq(&callback_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3109 | compute_effective_cpumask(&new_cpus, cs, parent); |
| 3110 | } |
| 3111 | |
| 3112 | /* |
| 3113 | * If the effective_cpus is empty because the child |
| 3114 | * partitions take away all the CPUs, we can keep |
| 3115 | * the current partition and let the child partitions |
| 3116 | * fight for available CPUs. |
| 3117 | */ |
| 3118 | if ((parent->partition_root_state == PRS_ERROR) || |
| 3119 | cpumask_empty(&new_cpus)) { |
| 3120 | update_parent_subparts_cpumask(cs, partcmd_disable, |
| 3121 | NULL, tmp); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3122 | spin_lock_irq(&callback_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3123 | cs->partition_root_state = PRS_ERROR; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 3124 | spin_unlock_irq(&callback_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3125 | } |
| 3126 | cpuset_force_rebuild(); |
| 3127 | } |
| 3128 | |
| 3129 | /* |
| 3130 | * On the other hand, an erroneous partition root may be transitioned |
| 3131 | * back to a regular one or a partition root with no CPU allocated |
| 3132 | * from the parent may change to erroneous. |
| 3133 | */ |
| 3134 | if (is_partition_root(parent) && |
| 3135 | ((cs->partition_root_state == PRS_ERROR) || |
| 3136 | !cpumask_intersects(&new_cpus, parent->subparts_cpus)) && |
| 3137 | update_parent_subparts_cpumask(cs, partcmd_update, NULL, tmp)) |
| 3138 | cpuset_force_rebuild(); |
| 3139 | |
| 3140 | update_tasks: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3141 | cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus); |
| 3142 | mems_updated = !nodes_equal(new_mems, cs->effective_mems); |
| 3143 | |
| 3144 | if (is_in_v2_mode()) |
| 3145 | hotplug_update_tasks(cs, &new_cpus, &new_mems, |
| 3146 | cpus_updated, mems_updated); |
| 3147 | else |
| 3148 | hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems, |
| 3149 | cpus_updated, mems_updated); |
| 3150 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3151 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset |
| 3156 | * |
| 3157 | * This function is called after either CPU or memory configuration has |
| 3158 | * changed and updates cpuset accordingly. The top_cpuset is always |
| 3159 | * synchronized to cpu_active_mask and N_MEMORY, which is necessary in |
| 3160 | * order to make cpusets transparent (of no affect) on systems that are |
| 3161 | * actively using CPU hotplug but making no active use of cpusets. |
| 3162 | * |
| 3163 | * Non-root cpusets are only affected by offlining. If any CPUs or memory |
| 3164 | * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on |
| 3165 | * all descendants. |
| 3166 | * |
| 3167 | * Note that CPU offlining during suspend is ignored. We don't modify |
| 3168 | * cpusets across suspend/resume cycles at all. |
| 3169 | */ |
| 3170 | static void cpuset_hotplug_workfn(struct work_struct *work) |
| 3171 | { |
| 3172 | static cpumask_t new_cpus; |
| 3173 | static nodemask_t new_mems; |
| 3174 | bool cpus_updated, mems_updated; |
| 3175 | bool on_dfl = is_in_v2_mode(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3176 | struct tmpmasks tmp, *ptmp = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3177 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3178 | if (on_dfl && !alloc_cpumasks(NULL, &tmp)) |
| 3179 | ptmp = &tmp; |
| 3180 | |
| 3181 | percpu_down_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3182 | |
| 3183 | /* fetch the available cpus/mems and find out which changed how */ |
| 3184 | cpumask_copy(&new_cpus, cpu_active_mask); |
| 3185 | new_mems = node_states[N_MEMORY]; |
| 3186 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3187 | /* |
| 3188 | * If subparts_cpus is populated, it is likely that the check below |
| 3189 | * will produce a false positive on cpus_updated when the cpu list |
| 3190 | * isn't changed. It is extra work, but it is better to be safe. |
| 3191 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3192 | cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus); |
| 3193 | mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems); |
| 3194 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3195 | /* |
| 3196 | * In the rare case that hotplug removes all the cpus in subparts_cpus, |
| 3197 | * we assumed that cpus are updated. |
| 3198 | */ |
| 3199 | if (!cpus_updated && top_cpuset.nr_subparts_cpus) |
| 3200 | cpus_updated = true; |
| 3201 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3202 | /* synchronize cpus_allowed to cpu_active_mask */ |
| 3203 | if (cpus_updated) { |
| 3204 | spin_lock_irq(&callback_lock); |
| 3205 | if (!on_dfl) |
| 3206 | cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3207 | /* |
| 3208 | * Make sure that CPUs allocated to child partitions |
| 3209 | * do not show up in effective_cpus. If no CPU is left, |
| 3210 | * we clear the subparts_cpus & let the child partitions |
| 3211 | * fight for the CPUs again. |
| 3212 | */ |
| 3213 | if (top_cpuset.nr_subparts_cpus) { |
| 3214 | if (cpumask_subset(&new_cpus, |
| 3215 | top_cpuset.subparts_cpus)) { |
| 3216 | top_cpuset.nr_subparts_cpus = 0; |
| 3217 | cpumask_clear(top_cpuset.subparts_cpus); |
| 3218 | } else { |
| 3219 | cpumask_andnot(&new_cpus, &new_cpus, |
| 3220 | top_cpuset.subparts_cpus); |
| 3221 | } |
| 3222 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3223 | cpumask_copy(top_cpuset.effective_cpus, &new_cpus); |
| 3224 | spin_unlock_irq(&callback_lock); |
| 3225 | /* we don't mess with cpumasks of tasks in top_cpuset */ |
| 3226 | } |
| 3227 | |
| 3228 | /* synchronize mems_allowed to N_MEMORY */ |
| 3229 | if (mems_updated) { |
| 3230 | spin_lock_irq(&callback_lock); |
| 3231 | if (!on_dfl) |
| 3232 | top_cpuset.mems_allowed = new_mems; |
| 3233 | top_cpuset.effective_mems = new_mems; |
| 3234 | spin_unlock_irq(&callback_lock); |
| 3235 | update_tasks_nodemask(&top_cpuset); |
| 3236 | } |
| 3237 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3238 | percpu_up_write(&cpuset_rwsem); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3239 | |
| 3240 | /* if cpus or mems changed, we need to propagate to descendants */ |
| 3241 | if (cpus_updated || mems_updated) { |
| 3242 | struct cpuset *cs; |
| 3243 | struct cgroup_subsys_state *pos_css; |
| 3244 | |
| 3245 | rcu_read_lock(); |
| 3246 | cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { |
| 3247 | if (cs == &top_cpuset || !css_tryget_online(&cs->css)) |
| 3248 | continue; |
| 3249 | rcu_read_unlock(); |
| 3250 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3251 | cpuset_hotplug_update_tasks(cs, ptmp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3252 | |
| 3253 | rcu_read_lock(); |
| 3254 | css_put(&cs->css); |
| 3255 | } |
| 3256 | rcu_read_unlock(); |
| 3257 | } |
| 3258 | |
| 3259 | /* rebuild sched domains if cpus_allowed has changed */ |
| 3260 | if (cpus_updated || force_rebuild) { |
| 3261 | force_rebuild = false; |
| 3262 | rebuild_sched_domains(); |
| 3263 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3264 | |
| 3265 | free_cpumasks(NULL, ptmp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | void cpuset_update_active_cpus(void) |
| 3269 | { |
| 3270 | /* |
| 3271 | * We're inside cpu hotplug critical region which usually nests |
| 3272 | * inside cgroup synchronization. Bounce actual hotplug processing |
| 3273 | * to a work item to avoid reverse locking order. |
| 3274 | */ |
| 3275 | schedule_work(&cpuset_hotplug_work); |
| 3276 | } |
| 3277 | |
| 3278 | void cpuset_wait_for_hotplug(void) |
| 3279 | { |
| 3280 | flush_work(&cpuset_hotplug_work); |
| 3281 | } |
| 3282 | |
| 3283 | /* |
| 3284 | * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. |
| 3285 | * Call this routine anytime after node_states[N_MEMORY] changes. |
| 3286 | * See cpuset_update_active_cpus() for CPU hotplug handling. |
| 3287 | */ |
| 3288 | static int cpuset_track_online_nodes(struct notifier_block *self, |
| 3289 | unsigned long action, void *arg) |
| 3290 | { |
| 3291 | schedule_work(&cpuset_hotplug_work); |
| 3292 | return NOTIFY_OK; |
| 3293 | } |
| 3294 | |
| 3295 | static struct notifier_block cpuset_track_online_nodes_nb = { |
| 3296 | .notifier_call = cpuset_track_online_nodes, |
| 3297 | .priority = 10, /* ??! */ |
| 3298 | }; |
| 3299 | |
| 3300 | /** |
| 3301 | * cpuset_init_smp - initialize cpus_allowed |
| 3302 | * |
| 3303 | * Description: Finish top cpuset after cpu, node maps are initialized |
| 3304 | */ |
| 3305 | void __init cpuset_init_smp(void) |
| 3306 | { |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 3307 | /* |
| 3308 | * cpus_allowd/mems_allowed set to v2 values in the initial |
| 3309 | * cpuset_bind() call will be reset to v1 values in another |
| 3310 | * cpuset_bind() call when v1 cpuset is mounted. |
| 3311 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3312 | top_cpuset.old_mems_allowed = top_cpuset.mems_allowed; |
| 3313 | |
| 3314 | cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask); |
| 3315 | top_cpuset.effective_mems = node_states[N_MEMORY]; |
| 3316 | |
| 3317 | register_hotmemory_notifier(&cpuset_track_online_nodes_nb); |
| 3318 | |
| 3319 | cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0); |
| 3320 | BUG_ON(!cpuset_migrate_mm_wq); |
| 3321 | } |
| 3322 | |
| 3323 | /** |
| 3324 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
| 3325 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. |
| 3326 | * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. |
| 3327 | * |
| 3328 | * Description: Returns the cpumask_var_t cpus_allowed of the cpuset |
| 3329 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 3330 | * subset of cpu_online_mask, even if this means going outside the |
| 3331 | * tasks cpuset. |
| 3332 | **/ |
| 3333 | |
| 3334 | void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) |
| 3335 | { |
| 3336 | unsigned long flags; |
| 3337 | |
| 3338 | spin_lock_irqsave(&callback_lock, flags); |
| 3339 | rcu_read_lock(); |
| 3340 | guarantee_online_cpus(task_cs(tsk), pmask); |
| 3341 | rcu_read_unlock(); |
| 3342 | spin_unlock_irqrestore(&callback_lock, flags); |
| 3343 | } |
| 3344 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3345 | /** |
| 3346 | * cpuset_cpus_allowed_fallback - final fallback before complete catastrophe. |
| 3347 | * @tsk: pointer to task_struct with which the scheduler is struggling |
| 3348 | * |
| 3349 | * Description: In the case that the scheduler cannot find an allowed cpu in |
| 3350 | * tsk->cpus_allowed, we fall back to task_cs(tsk)->cpus_allowed. In legacy |
| 3351 | * mode however, this value is the same as task_cs(tsk)->effective_cpus, |
| 3352 | * which will not contain a sane cpumask during cases such as cpu hotplugging. |
| 3353 | * This is the absolute last resort for the scheduler and it is only used if |
| 3354 | * _every_ other avenue has been traveled. |
| 3355 | **/ |
| 3356 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3357 | void cpuset_cpus_allowed_fallback(struct task_struct *tsk) |
| 3358 | { |
| 3359 | rcu_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3360 | do_set_cpus_allowed(tsk, is_in_v2_mode() ? |
| 3361 | task_cs(tsk)->cpus_allowed : cpu_possible_mask); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3362 | rcu_read_unlock(); |
| 3363 | |
| 3364 | /* |
| 3365 | * We own tsk->cpus_allowed, nobody can change it under us. |
| 3366 | * |
| 3367 | * But we used cs && cs->cpus_allowed lockless and thus can |
| 3368 | * race with cgroup_attach_task() or update_cpumask() and get |
| 3369 | * the wrong tsk->cpus_allowed. However, both cases imply the |
| 3370 | * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() |
| 3371 | * which takes task_rq_lock(). |
| 3372 | * |
| 3373 | * If we are called after it dropped the lock we must see all |
| 3374 | * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary |
| 3375 | * set any mask even if it is not right from task_cs() pov, |
| 3376 | * the pending set_cpus_allowed_ptr() will fix things. |
| 3377 | * |
| 3378 | * select_fallback_rq() will fix things ups and set cpu_possible_mask |
| 3379 | * if required. |
| 3380 | */ |
| 3381 | } |
| 3382 | |
| 3383 | void __init cpuset_init_current_mems_allowed(void) |
| 3384 | { |
| 3385 | nodes_setall(current->mems_allowed); |
| 3386 | } |
| 3387 | |
| 3388 | /** |
| 3389 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. |
| 3390 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. |
| 3391 | * |
| 3392 | * Description: Returns the nodemask_t mems_allowed of the cpuset |
| 3393 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 3394 | * subset of node_states[N_MEMORY], even if this means going outside the |
| 3395 | * tasks cpuset. |
| 3396 | **/ |
| 3397 | |
| 3398 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) |
| 3399 | { |
| 3400 | nodemask_t mask; |
| 3401 | unsigned long flags; |
| 3402 | |
| 3403 | spin_lock_irqsave(&callback_lock, flags); |
| 3404 | rcu_read_lock(); |
| 3405 | guarantee_online_mems(task_cs(tsk), &mask); |
| 3406 | rcu_read_unlock(); |
| 3407 | spin_unlock_irqrestore(&callback_lock, flags); |
| 3408 | |
| 3409 | return mask; |
| 3410 | } |
| 3411 | |
| 3412 | /** |
| 3413 | * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed |
| 3414 | * @nodemask: the nodemask to be checked |
| 3415 | * |
| 3416 | * Are any of the nodes in the nodemask allowed in current->mems_allowed? |
| 3417 | */ |
| 3418 | int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask) |
| 3419 | { |
| 3420 | return nodes_intersects(*nodemask, current->mems_allowed); |
| 3421 | } |
| 3422 | |
| 3423 | /* |
| 3424 | * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or |
| 3425 | * mem_hardwall ancestor to the specified cpuset. Call holding |
| 3426 | * callback_lock. If no ancestor is mem_exclusive or mem_hardwall |
| 3427 | * (an unusual configuration), then returns the root cpuset. |
| 3428 | */ |
| 3429 | static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs) |
| 3430 | { |
| 3431 | while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs)) |
| 3432 | cs = parent_cs(cs); |
| 3433 | return cs; |
| 3434 | } |
| 3435 | |
| 3436 | /** |
| 3437 | * cpuset_node_allowed - Can we allocate on a memory node? |
| 3438 | * @node: is this an allowed node? |
| 3439 | * @gfp_mask: memory allocation flags |
| 3440 | * |
| 3441 | * If we're in interrupt, yes, we can always allocate. If @node is set in |
| 3442 | * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this |
| 3443 | * node is set in the nearest hardwalled cpuset ancestor to current's cpuset, |
| 3444 | * yes. If current has access to memory reserves as an oom victim, yes. |
| 3445 | * Otherwise, no. |
| 3446 | * |
| 3447 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
| 3448 | * and do not allow allocations outside the current tasks cpuset |
| 3449 | * unless the task has been OOM killed. |
| 3450 | * GFP_KERNEL allocations are not so marked, so can escape to the |
| 3451 | * nearest enclosing hardwalled ancestor cpuset. |
| 3452 | * |
| 3453 | * Scanning up parent cpusets requires callback_lock. The |
| 3454 | * __alloc_pages() routine only calls here with __GFP_HARDWALL bit |
| 3455 | * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the |
| 3456 | * current tasks mems_allowed came up empty on the first pass over |
| 3457 | * the zonelist. So only GFP_KERNEL allocations, if all nodes in the |
| 3458 | * cpuset are short of memory, might require taking the callback_lock. |
| 3459 | * |
| 3460 | * The first call here from mm/page_alloc:get_page_from_freelist() |
| 3461 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, |
| 3462 | * so no allocation on a node outside the cpuset is allowed (unless |
| 3463 | * in interrupt, of course). |
| 3464 | * |
| 3465 | * The second pass through get_page_from_freelist() doesn't even call |
| 3466 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() |
| 3467 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set |
| 3468 | * in alloc_flags. That logic and the checks below have the combined |
| 3469 | * affect that: |
| 3470 | * in_interrupt - any node ok (current task context irrelevant) |
| 3471 | * GFP_ATOMIC - any node ok |
| 3472 | * tsk_is_oom_victim - any node ok |
| 3473 | * GFP_KERNEL - any node in enclosing hardwalled cpuset ok |
| 3474 | * GFP_USER - only nodes in current tasks mems allowed ok. |
| 3475 | */ |
| 3476 | bool __cpuset_node_allowed(int node, gfp_t gfp_mask) |
| 3477 | { |
| 3478 | struct cpuset *cs; /* current cpuset ancestors */ |
| 3479 | int allowed; /* is allocation in zone z allowed? */ |
| 3480 | unsigned long flags; |
| 3481 | |
| 3482 | if (in_interrupt()) |
| 3483 | return true; |
| 3484 | if (node_isset(node, current->mems_allowed)) |
| 3485 | return true; |
| 3486 | /* |
| 3487 | * Allow tasks that have access to memory reserves because they have |
| 3488 | * been OOM killed to get memory anywhere. |
| 3489 | */ |
| 3490 | if (unlikely(tsk_is_oom_victim(current))) |
| 3491 | return true; |
| 3492 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
| 3493 | return false; |
| 3494 | |
| 3495 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 3496 | return true; |
| 3497 | |
| 3498 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
| 3499 | spin_lock_irqsave(&callback_lock, flags); |
| 3500 | |
| 3501 | rcu_read_lock(); |
| 3502 | cs = nearest_hardwall_ancestor(task_cs(current)); |
| 3503 | allowed = node_isset(node, cs->mems_allowed); |
| 3504 | rcu_read_unlock(); |
| 3505 | |
| 3506 | spin_unlock_irqrestore(&callback_lock, flags); |
| 3507 | return allowed; |
| 3508 | } |
| 3509 | |
| 3510 | /** |
| 3511 | * cpuset_mem_spread_node() - On which node to begin search for a file page |
| 3512 | * cpuset_slab_spread_node() - On which node to begin search for a slab page |
| 3513 | * |
| 3514 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for |
| 3515 | * tasks in a cpuset with is_spread_page or is_spread_slab set), |
| 3516 | * and if the memory allocation used cpuset_mem_spread_node() |
| 3517 | * to determine on which node to start looking, as it will for |
| 3518 | * certain page cache or slab cache pages such as used for file |
| 3519 | * system buffers and inode caches, then instead of starting on the |
| 3520 | * local node to look for a free page, rather spread the starting |
| 3521 | * node around the tasks mems_allowed nodes. |
| 3522 | * |
| 3523 | * We don't have to worry about the returned node being offline |
| 3524 | * because "it can't happen", and even if it did, it would be ok. |
| 3525 | * |
| 3526 | * The routines calling guarantee_online_mems() are careful to |
| 3527 | * only set nodes in task->mems_allowed that are online. So it |
| 3528 | * should not be possible for the following code to return an |
| 3529 | * offline node. But if it did, that would be ok, as this routine |
| 3530 | * is not returning the node where the allocation must be, only |
| 3531 | * the node where the search should start. The zonelist passed to |
| 3532 | * __alloc_pages() will include all nodes. If the slab allocator |
| 3533 | * is passed an offline node, it will fall back to the local node. |
| 3534 | * See kmem_cache_alloc_node(). |
| 3535 | */ |
| 3536 | |
| 3537 | static int cpuset_spread_node(int *rotor) |
| 3538 | { |
| 3539 | return *rotor = next_node_in(*rotor, current->mems_allowed); |
| 3540 | } |
| 3541 | |
| 3542 | int cpuset_mem_spread_node(void) |
| 3543 | { |
| 3544 | if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) |
| 3545 | current->cpuset_mem_spread_rotor = |
| 3546 | node_random(¤t->mems_allowed); |
| 3547 | |
| 3548 | return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); |
| 3549 | } |
| 3550 | |
| 3551 | int cpuset_slab_spread_node(void) |
| 3552 | { |
| 3553 | if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE) |
| 3554 | current->cpuset_slab_spread_rotor = |
| 3555 | node_random(¤t->mems_allowed); |
| 3556 | |
| 3557 | return cpuset_spread_node(¤t->cpuset_slab_spread_rotor); |
| 3558 | } |
| 3559 | |
| 3560 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); |
| 3561 | |
| 3562 | /** |
| 3563 | * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? |
| 3564 | * @tsk1: pointer to task_struct of some task. |
| 3565 | * @tsk2: pointer to task_struct of some other task. |
| 3566 | * |
| 3567 | * Description: Return true if @tsk1's mems_allowed intersects the |
| 3568 | * mems_allowed of @tsk2. Used by the OOM killer to determine if |
| 3569 | * one of the task's memory usage might impact the memory available |
| 3570 | * to the other. |
| 3571 | **/ |
| 3572 | |
| 3573 | int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, |
| 3574 | const struct task_struct *tsk2) |
| 3575 | { |
| 3576 | return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); |
| 3577 | } |
| 3578 | |
| 3579 | /** |
| 3580 | * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed |
| 3581 | * |
| 3582 | * Description: Prints current's name, cpuset name, and cached copy of its |
| 3583 | * mems_allowed to the kernel log. |
| 3584 | */ |
| 3585 | void cpuset_print_current_mems_allowed(void) |
| 3586 | { |
| 3587 | struct cgroup *cgrp; |
| 3588 | |
| 3589 | rcu_read_lock(); |
| 3590 | |
| 3591 | cgrp = task_cs(current)->css.cgroup; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3592 | pr_cont(",cpuset="); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3593 | pr_cont_cgroup_name(cgrp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3594 | pr_cont(",mems_allowed=%*pbl", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3595 | nodemask_pr_args(¤t->mems_allowed)); |
| 3596 | |
| 3597 | rcu_read_unlock(); |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | * Collection of memory_pressure is suppressed unless |
| 3602 | * this flag is enabled by writing "1" to the special |
| 3603 | * cpuset file 'memory_pressure_enabled' in the root cpuset. |
| 3604 | */ |
| 3605 | |
| 3606 | int cpuset_memory_pressure_enabled __read_mostly; |
| 3607 | |
| 3608 | /** |
| 3609 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. |
| 3610 | * |
| 3611 | * Keep a running average of the rate of synchronous (direct) |
| 3612 | * page reclaim efforts initiated by tasks in each cpuset. |
| 3613 | * |
| 3614 | * This represents the rate at which some task in the cpuset |
| 3615 | * ran low on memory on all nodes it was allowed to use, and |
| 3616 | * had to enter the kernels page reclaim code in an effort to |
| 3617 | * create more free memory by tossing clean pages or swapping |
| 3618 | * or writing dirty pages. |
| 3619 | * |
| 3620 | * Display to user space in the per-cpuset read-only file |
| 3621 | * "memory_pressure". Value displayed is an integer |
| 3622 | * representing the recent rate of entry into the synchronous |
| 3623 | * (direct) page reclaim by any task attached to the cpuset. |
| 3624 | **/ |
| 3625 | |
| 3626 | void __cpuset_memory_pressure_bump(void) |
| 3627 | { |
| 3628 | rcu_read_lock(); |
| 3629 | fmeter_markevent(&task_cs(current)->fmeter); |
| 3630 | rcu_read_unlock(); |
| 3631 | } |
| 3632 | |
| 3633 | #ifdef CONFIG_PROC_PID_CPUSET |
| 3634 | /* |
| 3635 | * proc_cpuset_show() |
| 3636 | * - Print tasks cpuset path into seq_file. |
| 3637 | * - Used for /proc/<pid>/cpuset. |
| 3638 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
| 3639 | * doesn't really matter if tsk->cpuset changes after we read it, |
| 3640 | * and we take cpuset_mutex, keeping cpuset_attach() from changing it |
| 3641 | * anyway. |
| 3642 | */ |
| 3643 | int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns, |
| 3644 | struct pid *pid, struct task_struct *tsk) |
| 3645 | { |
| 3646 | char *buf; |
| 3647 | struct cgroup_subsys_state *css; |
| 3648 | int retval; |
| 3649 | |
| 3650 | retval = -ENOMEM; |
| 3651 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3652 | if (!buf) |
| 3653 | goto out; |
| 3654 | |
| 3655 | css = task_get_css(tsk, cpuset_cgrp_id); |
| 3656 | retval = cgroup_path_ns(css->cgroup, buf, PATH_MAX, |
| 3657 | current->nsproxy->cgroup_ns); |
| 3658 | css_put(css); |
| 3659 | if (retval >= PATH_MAX) |
| 3660 | retval = -ENAMETOOLONG; |
| 3661 | if (retval < 0) |
| 3662 | goto out_free; |
| 3663 | seq_puts(m, buf); |
| 3664 | seq_putc(m, '\n'); |
| 3665 | retval = 0; |
| 3666 | out_free: |
| 3667 | kfree(buf); |
| 3668 | out: |
| 3669 | return retval; |
| 3670 | } |
| 3671 | #endif /* CONFIG_PROC_PID_CPUSET */ |
| 3672 | |
| 3673 | /* Display task mems_allowed in /proc/<pid>/status file. */ |
| 3674 | void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) |
| 3675 | { |
| 3676 | seq_printf(m, "Mems_allowed:\t%*pb\n", |
| 3677 | nodemask_pr_args(&task->mems_allowed)); |
| 3678 | seq_printf(m, "Mems_allowed_list:\t%*pbl\n", |
| 3679 | nodemask_pr_args(&task->mems_allowed)); |
| 3680 | } |