| 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> |
| 36 | #include <linux/list.h> |
| 37 | #include <linux/mempolicy.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/memory.h> |
| 40 | #include <linux/export.h> |
| 41 | #include <linux/mount.h> |
| 42 | #include <linux/namei.h> |
| 43 | #include <linux/pagemap.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/rcupdate.h> |
| 46 | #include <linux/sched.h> |
| 47 | #include <linux/seq_file.h> |
| 48 | #include <linux/security.h> |
| 49 | #include <linux/slab.h> |
| 50 | #include <linux/spinlock.h> |
| 51 | #include <linux/stat.h> |
| 52 | #include <linux/string.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/backing-dev.h> |
| 55 | #include <linux/sort.h> |
| 56 | |
| 57 | #include <asm/uaccess.h> |
| 58 | #include <linux/atomic.h> |
| 59 | #include <linux/mutex.h> |
| 60 | #include <linux/cgroup.h> |
| 61 | #include <linux/wait.h> |
| 62 | |
| 63 | struct static_key cpusets_pre_enable_key __read_mostly = STATIC_KEY_INIT_FALSE; |
| 64 | struct static_key cpusets_enabled_key __read_mostly = STATIC_KEY_INIT_FALSE; |
| 65 | |
| 66 | /* See "Frequency meter" comments, below. */ |
| 67 | |
| 68 | struct fmeter { |
| 69 | int cnt; /* unprocessed events count */ |
| 70 | int val; /* most recent output value */ |
| 71 | time_t time; /* clock (secs) when val computed */ |
| 72 | spinlock_t lock; /* guards read or write of above */ |
| 73 | }; |
| 74 | |
| 75 | struct cpuset { |
| 76 | struct cgroup_subsys_state css; |
| 77 | |
| 78 | unsigned long flags; /* "unsigned long" so bitops work */ |
| 79 | |
| 80 | /* |
| 81 | * On default hierarchy: |
| 82 | * |
| 83 | * The user-configured masks can only be changed by writing to |
| 84 | * cpuset.cpus and cpuset.mems, and won't be limited by the |
| 85 | * parent masks. |
| 86 | * |
| 87 | * The effective masks is the real masks that apply to the tasks |
| 88 | * in the cpuset. They may be changed if the configured masks are |
| 89 | * changed or hotplug happens. |
| 90 | * |
| 91 | * effective_mask == configured_mask & parent's effective_mask, |
| 92 | * and if it ends up empty, it will inherit the parent's mask. |
| 93 | * |
| 94 | * |
| 95 | * On legacy hierachy: |
| 96 | * |
| 97 | * The user-configured masks are always the same with effective masks. |
| 98 | */ |
| 99 | |
| 100 | /* user-configured CPUs and Memory Nodes allow to tasks */ |
| 101 | cpumask_var_t cpus_allowed; |
| 102 | cpumask_var_t cpus_requested; |
| 103 | nodemask_t mems_allowed; |
| 104 | |
| 105 | /* effective CPUs and Memory Nodes allow to tasks */ |
| 106 | cpumask_var_t effective_cpus; |
| 107 | nodemask_t effective_mems; |
| 108 | |
| 109 | /* |
| 110 | * This is old Memory Nodes tasks took on. |
| 111 | * |
| 112 | * - top_cpuset.old_mems_allowed is initialized to mems_allowed. |
| 113 | * - A new cpuset's old_mems_allowed is initialized when some |
| 114 | * task is moved into it. |
| 115 | * - old_mems_allowed is used in cpuset_migrate_mm() when we change |
| 116 | * cpuset.mems_allowed and have tasks' nodemask updated, and |
| 117 | * then old_mems_allowed is updated to mems_allowed. |
| 118 | */ |
| 119 | nodemask_t old_mems_allowed; |
| 120 | |
| 121 | struct fmeter fmeter; /* memory_pressure filter */ |
| 122 | |
| 123 | /* |
| 124 | * Tasks are being attached to this cpuset. Used to prevent |
| 125 | * zeroing cpus/mems_allowed between ->can_attach() and ->attach(). |
| 126 | */ |
| 127 | int attach_in_progress; |
| 128 | |
| 129 | /* partition number for rebuild_sched_domains() */ |
| 130 | int pn; |
| 131 | |
| 132 | /* for custom sched domain */ |
| 133 | int relax_domain_level; |
| 134 | }; |
| 135 | |
| 136 | static inline struct cpuset *css_cs(struct cgroup_subsys_state *css) |
| 137 | { |
| 138 | return css ? container_of(css, struct cpuset, css) : NULL; |
| 139 | } |
| 140 | |
| 141 | /* Retrieve the cpuset for a task */ |
| 142 | static inline struct cpuset *task_cs(struct task_struct *task) |
| 143 | { |
| 144 | return css_cs(task_css(task, cpuset_cgrp_id)); |
| 145 | } |
| 146 | |
| 147 | static inline struct cpuset *parent_cs(struct cpuset *cs) |
| 148 | { |
| 149 | return css_cs(cs->css.parent); |
| 150 | } |
| 151 | |
| 152 | #ifdef CONFIG_NUMA |
| 153 | static inline bool task_has_mempolicy(struct task_struct *task) |
| 154 | { |
| 155 | return task->mempolicy; |
| 156 | } |
| 157 | #else |
| 158 | static inline bool task_has_mempolicy(struct task_struct *task) |
| 159 | { |
| 160 | return false; |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | |
| 165 | /* bits in struct cpuset flags field */ |
| 166 | typedef enum { |
| 167 | CS_ONLINE, |
| 168 | CS_CPU_EXCLUSIVE, |
| 169 | CS_MEM_EXCLUSIVE, |
| 170 | CS_MEM_HARDWALL, |
| 171 | CS_MEMORY_MIGRATE, |
| 172 | CS_SCHED_LOAD_BALANCE, |
| 173 | CS_SPREAD_PAGE, |
| 174 | CS_SPREAD_SLAB, |
| 175 | CS_FAMILY_BOOST, |
| 176 | } cpuset_flagbits_t; |
| 177 | |
| 178 | /* convenient tests for these bits */ |
| 179 | static inline bool is_cpuset_online(struct cpuset *cs) |
| 180 | { |
| 181 | return test_bit(CS_ONLINE, &cs->flags) && !css_is_dying(&cs->css); |
| 182 | } |
| 183 | |
| 184 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
| 185 | { |
| 186 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
| 187 | } |
| 188 | |
| 189 | static inline int is_mem_exclusive(const struct cpuset *cs) |
| 190 | { |
| 191 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
| 192 | } |
| 193 | |
| 194 | static inline int is_mem_hardwall(const struct cpuset *cs) |
| 195 | { |
| 196 | return test_bit(CS_MEM_HARDWALL, &cs->flags); |
| 197 | } |
| 198 | |
| 199 | static inline int is_sched_load_balance(const struct cpuset *cs) |
| 200 | { |
| 201 | return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
| 202 | } |
| 203 | |
| 204 | static inline int is_memory_migrate(const struct cpuset *cs) |
| 205 | { |
| 206 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
| 207 | } |
| 208 | |
| 209 | static inline int is_spread_page(const struct cpuset *cs) |
| 210 | { |
| 211 | return test_bit(CS_SPREAD_PAGE, &cs->flags); |
| 212 | } |
| 213 | |
| 214 | static inline int is_spread_slab(const struct cpuset *cs) |
| 215 | { |
| 216 | return test_bit(CS_SPREAD_SLAB, &cs->flags); |
| 217 | } |
| 218 | |
| 219 | static struct cpuset top_cpuset = { |
| 220 | .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) | |
| 221 | (1 << CS_MEM_EXCLUSIVE)), |
| 222 | }; |
| 223 | |
| 224 | static inline int is_family_boost_enabled(const struct cpuset *cs) |
| 225 | { |
| 226 | return test_bit(CS_FAMILY_BOOST, &cs->flags); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * cpuset_for_each_child - traverse online children of a cpuset |
| 231 | * @child_cs: loop cursor pointing to the current child |
| 232 | * @pos_css: used for iteration |
| 233 | * @parent_cs: target cpuset to walk children of |
| 234 | * |
| 235 | * Walk @child_cs through the online children of @parent_cs. Must be used |
| 236 | * with RCU read locked. |
| 237 | */ |
| 238 | #define cpuset_for_each_child(child_cs, pos_css, parent_cs) \ |
| 239 | css_for_each_child((pos_css), &(parent_cs)->css) \ |
| 240 | if (is_cpuset_online(((child_cs) = css_cs((pos_css))))) |
| 241 | |
| 242 | /** |
| 243 | * cpuset_for_each_descendant_pre - pre-order walk of a cpuset's descendants |
| 244 | * @des_cs: loop cursor pointing to the current descendant |
| 245 | * @pos_css: used for iteration |
| 246 | * @root_cs: target cpuset to walk ancestor of |
| 247 | * |
| 248 | * Walk @des_cs through the online descendants of @root_cs. Must be used |
| 249 | * with RCU read locked. The caller may modify @pos_css by calling |
| 250 | * css_rightmost_descendant() to skip subtree. @root_cs is included in the |
| 251 | * iteration and the first node to be visited. |
| 252 | */ |
| 253 | #define cpuset_for_each_descendant_pre(des_cs, pos_css, root_cs) \ |
| 254 | css_for_each_descendant_pre((pos_css), &(root_cs)->css) \ |
| 255 | if (is_cpuset_online(((des_cs) = css_cs((pos_css))))) |
| 256 | |
| 257 | /* |
| 258 | * There are two global locks guarding cpuset structures - cpuset_mutex and |
| 259 | * callback_lock. We also require taking task_lock() when dereferencing a |
| 260 | * task's cpuset pointer. See "The task_lock() exception", at the end of this |
| 261 | * comment. |
| 262 | * |
| 263 | * A task must hold both locks to modify cpusets. If a task holds |
| 264 | * cpuset_mutex, then it blocks others wanting that mutex, ensuring that it |
| 265 | * is the only task able to also acquire callback_lock and be able to |
| 266 | * modify cpusets. It can perform various checks on the cpuset structure |
| 267 | * first, knowing nothing will change. It can also allocate memory while |
| 268 | * just holding cpuset_mutex. While it is performing these checks, various |
| 269 | * callback routines can briefly acquire callback_lock to query cpusets. |
| 270 | * Once it is ready to make the changes, it takes callback_lock, blocking |
| 271 | * everyone else. |
| 272 | * |
| 273 | * Calls to the kernel memory allocator can not be made while holding |
| 274 | * callback_lock, as that would risk double tripping on callback_lock |
| 275 | * from one of the callbacks into the cpuset code from within |
| 276 | * __alloc_pages(). |
| 277 | * |
| 278 | * If a task is only holding callback_lock, then it has read-only |
| 279 | * access to cpusets. |
| 280 | * |
| 281 | * Now, the task_struct fields mems_allowed and mempolicy may be changed |
| 282 | * by other task, we use alloc_lock in the task_struct fields to protect |
| 283 | * them. |
| 284 | * |
| 285 | * The cpuset_common_file_read() handlers only hold callback_lock across |
| 286 | * small pieces of code, such as when reading out possibly multi-word |
| 287 | * cpumasks and nodemasks. |
| 288 | * |
| 289 | * Accessing a task's cpuset should be done in accordance with the |
| 290 | * guidelines for accessing subsystem state in kernel/cgroup.c |
| 291 | */ |
| 292 | |
| 293 | static DEFINE_MUTEX(cpuset_mutex); |
| 294 | static DEFINE_SPINLOCK(callback_lock); |
| 295 | |
| 296 | static struct workqueue_struct *cpuset_migrate_mm_wq; |
| 297 | |
| 298 | /* |
| 299 | * CPU / memory hotplug is handled asynchronously. |
| 300 | */ |
| 301 | static void cpuset_hotplug_workfn(struct work_struct *work); |
| 302 | static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn); |
| 303 | |
| 304 | static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq); |
| 305 | |
| 306 | /* |
| 307 | * This is ugly, but preserves the userspace API for existing cpuset |
| 308 | * users. If someone tries to mount the "cpuset" filesystem, we |
| 309 | * silently switch it to mount "cgroup" instead |
| 310 | */ |
| 311 | static struct dentry *cpuset_mount(struct file_system_type *fs_type, |
| 312 | int flags, const char *unused_dev_name, void *data) |
| 313 | { |
| 314 | struct file_system_type *cgroup_fs = get_fs_type("cgroup"); |
| 315 | struct dentry *ret = ERR_PTR(-ENODEV); |
| 316 | if (cgroup_fs) { |
| 317 | char mountopts[] = |
| 318 | "cpuset,noprefix," |
| 319 | "release_agent=/sbin/cpuset_release_agent"; |
| 320 | ret = cgroup_fs->mount(cgroup_fs, flags, |
| 321 | unused_dev_name, mountopts); |
| 322 | put_filesystem(cgroup_fs); |
| 323 | } |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | static struct file_system_type cpuset_fs_type = { |
| 328 | .name = "cpuset", |
| 329 | .mount = cpuset_mount, |
| 330 | }; |
| 331 | |
| 332 | int is_top_task(struct task_struct *p) |
| 333 | { |
| 334 | struct cpuset *cpuset_for_task; |
| 335 | int ret; |
| 336 | |
| 337 | rcu_read_lock(); |
| 338 | cpuset_for_task = task_cs(p); |
| 339 | ret = is_family_boost_enabled(cpuset_for_task); |
| 340 | rcu_read_unlock(); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | EXPORT_SYMBOL(is_top_task); |
| 345 | |
| 346 | /* |
| 347 | * Return in pmask the portion of a cpusets's cpus_allowed that |
| 348 | * are online. If none are online, walk up the cpuset hierarchy |
| 349 | * until we find one that does have some online cpus. |
| 350 | * |
| 351 | * One way or another, we guarantee to return some non-empty subset |
| 352 | * of cpu_online_mask. |
| 353 | * |
| 354 | * Call with callback_lock or cpuset_mutex held. |
| 355 | */ |
| 356 | static void guarantee_online_cpus(struct cpuset *cs, struct cpumask *pmask) |
| 357 | { |
| 358 | while (!cpumask_intersects(cs->effective_cpus, cpu_online_mask)) { |
| 359 | cs = parent_cs(cs); |
| 360 | if (unlikely(!cs)) { |
| 361 | /* |
| 362 | * The top cpuset doesn't have any online cpu as a |
| 363 | * consequence of a race between cpuset_hotplug_work |
| 364 | * and cpu hotplug notifier. But we know the top |
| 365 | * cpuset's effective_cpus is on its way to to be |
| 366 | * identical to cpu_online_mask. |
| 367 | */ |
| 368 | cpumask_copy(pmask, cpu_online_mask); |
| 369 | return; |
| 370 | } |
| 371 | } |
| 372 | cpumask_and(pmask, cs->effective_cpus, cpu_online_mask); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Return in *pmask the portion of a cpusets's mems_allowed that |
| 377 | * are online, with memory. If none are online with memory, walk |
| 378 | * up the cpuset hierarchy until we find one that does have some |
| 379 | * online mems. The top cpuset always has some mems online. |
| 380 | * |
| 381 | * One way or another, we guarantee to return some non-empty subset |
| 382 | * of node_states[N_MEMORY]. |
| 383 | * |
| 384 | * Call with callback_lock or cpuset_mutex held. |
| 385 | */ |
| 386 | static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask) |
| 387 | { |
| 388 | while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY])) |
| 389 | cs = parent_cs(cs); |
| 390 | nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * update task's spread flag if cpuset's page/slab spread flag is set |
| 395 | * |
| 396 | * Call with callback_lock or cpuset_mutex held. |
| 397 | */ |
| 398 | static void cpuset_update_task_spread_flag(struct cpuset *cs, |
| 399 | struct task_struct *tsk) |
| 400 | { |
| 401 | if (is_spread_page(cs)) |
| 402 | task_set_spread_page(tsk); |
| 403 | else |
| 404 | task_clear_spread_page(tsk); |
| 405 | |
| 406 | if (is_spread_slab(cs)) |
| 407 | task_set_spread_slab(tsk); |
| 408 | else |
| 409 | task_clear_spread_slab(tsk); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? |
| 414 | * |
| 415 | * One cpuset is a subset of another if all its allowed CPUs and |
| 416 | * Memory Nodes are a subset of the other, and its exclusive flags |
| 417 | * are only set if the other's are set. Call holding cpuset_mutex. |
| 418 | */ |
| 419 | |
| 420 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) |
| 421 | { |
| 422 | return cpumask_subset(p->cpus_requested, q->cpus_requested) && |
| 423 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
| 424 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && |
| 425 | is_mem_exclusive(p) <= is_mem_exclusive(q); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * alloc_trial_cpuset - allocate a trial cpuset |
| 430 | * @cs: the cpuset that the trial cpuset duplicates |
| 431 | */ |
| 432 | static struct cpuset *alloc_trial_cpuset(struct cpuset *cs) |
| 433 | { |
| 434 | struct cpuset *trial; |
| 435 | |
| 436 | trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL); |
| 437 | if (!trial) |
| 438 | return NULL; |
| 439 | |
| 440 | if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) |
| 441 | goto free_cs; |
| 442 | if (!alloc_cpumask_var(&trial->effective_cpus, GFP_KERNEL)) |
| 443 | goto free_cpus; |
| 444 | |
| 445 | cpumask_copy(trial->cpus_allowed, cs->cpus_allowed); |
| 446 | cpumask_copy(trial->effective_cpus, cs->effective_cpus); |
| 447 | return trial; |
| 448 | |
| 449 | free_cpus: |
| 450 | free_cpumask_var(trial->cpus_allowed); |
| 451 | free_cs: |
| 452 | kfree(trial); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * free_trial_cpuset - free the trial cpuset |
| 458 | * @trial: the trial cpuset to be freed |
| 459 | */ |
| 460 | static void free_trial_cpuset(struct cpuset *trial) |
| 461 | { |
| 462 | free_cpumask_var(trial->effective_cpus); |
| 463 | free_cpumask_var(trial->cpus_allowed); |
| 464 | kfree(trial); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * validate_change() - Used to validate that any proposed cpuset change |
| 469 | * follows the structural rules for cpusets. |
| 470 | * |
| 471 | * If we replaced the flag and mask values of the current cpuset |
| 472 | * (cur) with those values in the trial cpuset (trial), would |
| 473 | * our various subset and exclusive rules still be valid? Presumes |
| 474 | * cpuset_mutex held. |
| 475 | * |
| 476 | * 'cur' is the address of an actual, in-use cpuset. Operations |
| 477 | * such as list traversal that depend on the actual address of the |
| 478 | * cpuset in the list must use cur below, not trial. |
| 479 | * |
| 480 | * 'trial' is the address of bulk structure copy of cur, with |
| 481 | * perhaps one or more of the fields cpus_allowed, mems_allowed, |
| 482 | * or flags changed to new, trial values. |
| 483 | * |
| 484 | * Return 0 if valid, -errno if not. |
| 485 | */ |
| 486 | |
| 487 | static int validate_change(struct cpuset *cur, struct cpuset *trial) |
| 488 | { |
| 489 | struct cgroup_subsys_state *css; |
| 490 | struct cpuset *c, *par; |
| 491 | int ret; |
| 492 | |
| 493 | rcu_read_lock(); |
| 494 | |
| 495 | /* Each of our child cpusets must be a subset of us */ |
| 496 | ret = -EBUSY; |
| 497 | cpuset_for_each_child(c, css, cur) |
| 498 | if (!is_cpuset_subset(c, trial)) |
| 499 | goto out; |
| 500 | |
| 501 | /* Remaining checks don't apply to root cpuset */ |
| 502 | ret = 0; |
| 503 | if (cur == &top_cpuset) |
| 504 | goto out; |
| 505 | |
| 506 | par = parent_cs(cur); |
| 507 | |
| 508 | /* On legacy hiearchy, we must be a subset of our parent cpuset. */ |
| 509 | ret = -EACCES; |
| 510 | if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 511 | !is_cpuset_subset(trial, par)) |
| 512 | goto out; |
| 513 | |
| 514 | /* |
| 515 | * If either I or some sibling (!= me) is exclusive, we can't |
| 516 | * overlap |
| 517 | */ |
| 518 | ret = -EINVAL; |
| 519 | cpuset_for_each_child(c, css, par) { |
| 520 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
| 521 | c != cur && |
| 522 | cpumask_intersects(trial->cpus_requested, c->cpus_requested)) |
| 523 | goto out; |
| 524 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
| 525 | c != cur && |
| 526 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) |
| 527 | goto out; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Cpusets with tasks - existing or newly being attached - can't |
| 532 | * be changed to have empty cpus_allowed or mems_allowed. |
| 533 | */ |
| 534 | ret = -ENOSPC; |
| 535 | if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) { |
| 536 | if (!cpumask_empty(cur->cpus_allowed) && |
| 537 | cpumask_empty(trial->cpus_allowed)) |
| 538 | goto out; |
| 539 | if (!nodes_empty(cur->mems_allowed) && |
| 540 | nodes_empty(trial->mems_allowed)) |
| 541 | goto out; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * We can't shrink if we won't have enough room for SCHED_DEADLINE |
| 546 | * tasks. |
| 547 | */ |
| 548 | ret = -EBUSY; |
| 549 | if (is_cpu_exclusive(cur) && |
| 550 | !cpuset_cpumask_can_shrink(cur->cpus_allowed, |
| 551 | trial->cpus_allowed)) |
| 552 | goto out; |
| 553 | |
| 554 | ret = 0; |
| 555 | out: |
| 556 | rcu_read_unlock(); |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | #ifdef CONFIG_SMP |
| 561 | /* |
| 562 | * Helper routine for generate_sched_domains(). |
| 563 | * Do cpusets a, b have overlapping effective cpus_allowed masks? |
| 564 | */ |
| 565 | static int cpusets_overlap(struct cpuset *a, struct cpuset *b) |
| 566 | { |
| 567 | return cpumask_intersects(a->effective_cpus, b->effective_cpus); |
| 568 | } |
| 569 | |
| 570 | static void |
| 571 | update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c) |
| 572 | { |
| 573 | if (dattr->relax_domain_level < c->relax_domain_level) |
| 574 | dattr->relax_domain_level = c->relax_domain_level; |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | static void update_domain_attr_tree(struct sched_domain_attr *dattr, |
| 579 | struct cpuset *root_cs) |
| 580 | { |
| 581 | struct cpuset *cp; |
| 582 | struct cgroup_subsys_state *pos_css; |
| 583 | |
| 584 | rcu_read_lock(); |
| 585 | cpuset_for_each_descendant_pre(cp, pos_css, root_cs) { |
| 586 | /* skip the whole subtree if @cp doesn't have any CPU */ |
| 587 | if (cpumask_empty(cp->cpus_allowed)) { |
| 588 | pos_css = css_rightmost_descendant(pos_css); |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | if (is_sched_load_balance(cp)) |
| 593 | update_domain_attr(dattr, cp); |
| 594 | } |
| 595 | rcu_read_unlock(); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * generate_sched_domains() |
| 600 | * |
| 601 | * This function builds a partial partition of the systems CPUs |
| 602 | * A 'partial partition' is a set of non-overlapping subsets whose |
| 603 | * union is a subset of that set. |
| 604 | * The output of this function needs to be passed to kernel/sched/core.c |
| 605 | * partition_sched_domains() routine, which will rebuild the scheduler's |
| 606 | * load balancing domains (sched domains) as specified by that partial |
| 607 | * partition. |
| 608 | * |
| 609 | * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt |
| 610 | * for a background explanation of this. |
| 611 | * |
| 612 | * Does not return errors, on the theory that the callers of this |
| 613 | * routine would rather not worry about failures to rebuild sched |
| 614 | * domains when operating in the severe memory shortage situations |
| 615 | * that could cause allocation failures below. |
| 616 | * |
| 617 | * Must be called with cpuset_mutex held. |
| 618 | * |
| 619 | * The three key local variables below are: |
| 620 | * q - a linked-list queue of cpuset pointers, used to implement a |
| 621 | * top-down scan of all cpusets. This scan loads a pointer |
| 622 | * to each cpuset marked is_sched_load_balance into the |
| 623 | * array 'csa'. For our purposes, rebuilding the schedulers |
| 624 | * sched domains, we can ignore !is_sched_load_balance cpusets. |
| 625 | * csa - (for CpuSet Array) Array of pointers to all the cpusets |
| 626 | * that need to be load balanced, for convenient iterative |
| 627 | * access by the subsequent code that finds the best partition, |
| 628 | * i.e the set of domains (subsets) of CPUs such that the |
| 629 | * cpus_allowed of every cpuset marked is_sched_load_balance |
| 630 | * is a subset of one of these domains, while there are as |
| 631 | * many such domains as possible, each as small as possible. |
| 632 | * doms - Conversion of 'csa' to an array of cpumasks, for passing to |
| 633 | * the kernel/sched/core.c routine partition_sched_domains() in a |
| 634 | * convenient format, that can be easily compared to the prior |
| 635 | * value to determine what partition elements (sched domains) |
| 636 | * were changed (added or removed.) |
| 637 | * |
| 638 | * Finding the best partition (set of domains): |
| 639 | * The triple nested loops below over i, j, k scan over the |
| 640 | * load balanced cpusets (using the array of cpuset pointers in |
| 641 | * csa[]) looking for pairs of cpusets that have overlapping |
| 642 | * cpus_allowed, but which don't have the same 'pn' partition |
| 643 | * number and gives them in the same partition number. It keeps |
| 644 | * looping on the 'restart' label until it can no longer find |
| 645 | * any such pairs. |
| 646 | * |
| 647 | * The union of the cpus_allowed masks from the set of |
| 648 | * all cpusets having the same 'pn' value then form the one |
| 649 | * element of the partition (one sched domain) to be passed to |
| 650 | * partition_sched_domains(). |
| 651 | */ |
| 652 | static int generate_sched_domains(cpumask_var_t **domains, |
| 653 | struct sched_domain_attr **attributes) |
| 654 | { |
| 655 | struct cpuset *cp; /* scans q */ |
| 656 | struct cpuset **csa; /* array of all cpuset ptrs */ |
| 657 | int csn; /* how many cpuset ptrs in csa so far */ |
| 658 | int i, j, k; /* indices for partition finding loops */ |
| 659 | cpumask_var_t *doms; /* resulting partition; i.e. sched domains */ |
| 660 | cpumask_var_t non_isolated_cpus; /* load balanced CPUs */ |
| 661 | struct sched_domain_attr *dattr; /* attributes for custom domains */ |
| 662 | int ndoms = 0; /* number of sched domains in result */ |
| 663 | int nslot; /* next empty doms[] struct cpumask slot */ |
| 664 | struct cgroup_subsys_state *pos_css; |
| 665 | |
| 666 | doms = NULL; |
| 667 | dattr = NULL; |
| 668 | csa = NULL; |
| 669 | |
| 670 | if (!alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL)) |
| 671 | goto done; |
| 672 | cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map); |
| 673 | |
| 674 | /* Special case for the 99% of systems with one, full, sched domain */ |
| 675 | if (is_sched_load_balance(&top_cpuset)) { |
| 676 | ndoms = 1; |
| 677 | doms = alloc_sched_domains(ndoms); |
| 678 | if (!doms) |
| 679 | goto done; |
| 680 | |
| 681 | dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL); |
| 682 | if (dattr) { |
| 683 | *dattr = SD_ATTR_INIT; |
| 684 | update_domain_attr_tree(dattr, &top_cpuset); |
| 685 | } |
| 686 | cpumask_and(doms[0], top_cpuset.effective_cpus, |
| 687 | non_isolated_cpus); |
| 688 | |
| 689 | goto done; |
| 690 | } |
| 691 | |
| 692 | csa = kmalloc(nr_cpusets() * sizeof(cp), GFP_KERNEL); |
| 693 | if (!csa) |
| 694 | goto done; |
| 695 | csn = 0; |
| 696 | |
| 697 | rcu_read_lock(); |
| 698 | cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) { |
| 699 | if (cp == &top_cpuset) |
| 700 | continue; |
| 701 | /* |
| 702 | * Continue traversing beyond @cp iff @cp has some CPUs and |
| 703 | * isn't load balancing. The former is obvious. The |
| 704 | * latter: All child cpusets contain a subset of the |
| 705 | * parent's cpus, so just skip them, and then we call |
| 706 | * update_domain_attr_tree() to calc relax_domain_level of |
| 707 | * the corresponding sched domain. |
| 708 | */ |
| 709 | if (!cpumask_empty(cp->cpus_allowed) && |
| 710 | !(is_sched_load_balance(cp) && |
| 711 | cpumask_intersects(cp->cpus_allowed, non_isolated_cpus))) |
| 712 | continue; |
| 713 | |
| 714 | if (is_sched_load_balance(cp)) |
| 715 | csa[csn++] = cp; |
| 716 | |
| 717 | /* skip @cp's subtree */ |
| 718 | pos_css = css_rightmost_descendant(pos_css); |
| 719 | } |
| 720 | rcu_read_unlock(); |
| 721 | |
| 722 | for (i = 0; i < csn; i++) |
| 723 | csa[i]->pn = i; |
| 724 | ndoms = csn; |
| 725 | |
| 726 | restart: |
| 727 | /* Find the best partition (set of sched domains) */ |
| 728 | for (i = 0; i < csn; i++) { |
| 729 | struct cpuset *a = csa[i]; |
| 730 | int apn = a->pn; |
| 731 | |
| 732 | for (j = 0; j < csn; j++) { |
| 733 | struct cpuset *b = csa[j]; |
| 734 | int bpn = b->pn; |
| 735 | |
| 736 | if (apn != bpn && cpusets_overlap(a, b)) { |
| 737 | for (k = 0; k < csn; k++) { |
| 738 | struct cpuset *c = csa[k]; |
| 739 | |
| 740 | if (c->pn == bpn) |
| 741 | c->pn = apn; |
| 742 | } |
| 743 | ndoms--; /* one less element */ |
| 744 | goto restart; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Now we know how many domains to create. |
| 751 | * Convert <csn, csa> to <ndoms, doms> and populate cpu masks. |
| 752 | */ |
| 753 | doms = alloc_sched_domains(ndoms); |
| 754 | if (!doms) |
| 755 | goto done; |
| 756 | |
| 757 | /* |
| 758 | * The rest of the code, including the scheduler, can deal with |
| 759 | * dattr==NULL case. No need to abort if alloc fails. |
| 760 | */ |
| 761 | dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL); |
| 762 | |
| 763 | for (nslot = 0, i = 0; i < csn; i++) { |
| 764 | struct cpuset *a = csa[i]; |
| 765 | struct cpumask *dp; |
| 766 | int apn = a->pn; |
| 767 | |
| 768 | if (apn < 0) { |
| 769 | /* Skip completed partitions */ |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | dp = doms[nslot]; |
| 774 | |
| 775 | if (nslot == ndoms) { |
| 776 | static int warnings = 10; |
| 777 | if (warnings) { |
| 778 | pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n", |
| 779 | nslot, ndoms, csn, i, apn); |
| 780 | warnings--; |
| 781 | } |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | cpumask_clear(dp); |
| 786 | if (dattr) |
| 787 | *(dattr + nslot) = SD_ATTR_INIT; |
| 788 | for (j = i; j < csn; j++) { |
| 789 | struct cpuset *b = csa[j]; |
| 790 | |
| 791 | if (apn == b->pn) { |
| 792 | cpumask_or(dp, dp, b->effective_cpus); |
| 793 | cpumask_and(dp, dp, non_isolated_cpus); |
| 794 | if (dattr) |
| 795 | update_domain_attr_tree(dattr + nslot, b); |
| 796 | |
| 797 | /* Done with this partition */ |
| 798 | b->pn = -1; |
| 799 | } |
| 800 | } |
| 801 | nslot++; |
| 802 | } |
| 803 | BUG_ON(nslot != ndoms); |
| 804 | |
| 805 | done: |
| 806 | free_cpumask_var(non_isolated_cpus); |
| 807 | kfree(csa); |
| 808 | |
| 809 | /* |
| 810 | * Fallback to the default domain if kmalloc() failed. |
| 811 | * See comments in partition_sched_domains(). |
| 812 | */ |
| 813 | if (doms == NULL) |
| 814 | ndoms = 1; |
| 815 | |
| 816 | *domains = doms; |
| 817 | *attributes = dattr; |
| 818 | return ndoms; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * Rebuild scheduler domains. |
| 823 | * |
| 824 | * If the flag 'sched_load_balance' of any cpuset with non-empty |
| 825 | * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset |
| 826 | * which has that flag enabled, or if any cpuset with a non-empty |
| 827 | * 'cpus' is removed, then call this routine to rebuild the |
| 828 | * scheduler's dynamic sched domains. |
| 829 | * |
| 830 | * Call with cpuset_mutex held. Takes get_online_cpus(). |
| 831 | */ |
| 832 | static void rebuild_sched_domains_locked(void) |
| 833 | { |
| 834 | struct sched_domain_attr *attr; |
| 835 | cpumask_var_t *doms; |
| 836 | int ndoms; |
| 837 | |
| 838 | lockdep_assert_held(&cpuset_mutex); |
| 839 | get_online_cpus(); |
| 840 | |
| 841 | /* |
| 842 | * We have raced with CPU hotplug. Don't do anything to avoid |
| 843 | * passing doms with offlined cpu to partition_sched_domains(). |
| 844 | * Anyways, hotplug work item will rebuild sched domains. |
| 845 | */ |
| 846 | if (!cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask)) |
| 847 | goto out; |
| 848 | |
| 849 | /* Generate domain masks and attrs */ |
| 850 | ndoms = generate_sched_domains(&doms, &attr); |
| 851 | |
| 852 | /* Have scheduler rebuild the domains */ |
| 853 | partition_sched_domains(ndoms, doms, attr); |
| 854 | out: |
| 855 | put_online_cpus(); |
| 856 | } |
| 857 | #else /* !CONFIG_SMP */ |
| 858 | static void rebuild_sched_domains_locked(void) |
| 859 | { |
| 860 | } |
| 861 | #endif /* CONFIG_SMP */ |
| 862 | |
| 863 | void rebuild_sched_domains(void) |
| 864 | { |
| 865 | mutex_lock(&cpuset_mutex); |
| 866 | rebuild_sched_domains_locked(); |
| 867 | mutex_unlock(&cpuset_mutex); |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset. |
| 872 | * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed |
| 873 | * |
| 874 | * Iterate through each task of @cs updating its cpus_allowed to the |
| 875 | * effective cpuset's. As this function is called with cpuset_mutex held, |
| 876 | * cpuset membership stays stable. |
| 877 | */ |
| 878 | static void update_tasks_cpumask(struct cpuset *cs) |
| 879 | { |
| 880 | struct css_task_iter it; |
| 881 | struct task_struct *task; |
| 882 | |
| 883 | css_task_iter_start(&cs->css, &it); |
| 884 | while ((task = css_task_iter_next(&it))) |
| 885 | set_cpus_allowed_ptr(task, cs->effective_cpus); |
| 886 | css_task_iter_end(&it); |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree |
| 891 | * @cs: the cpuset to consider |
| 892 | * @new_cpus: temp variable for calculating new effective_cpus |
| 893 | * |
| 894 | * When congifured cpumask is changed, the effective cpumasks of this cpuset |
| 895 | * and all its descendants need to be updated. |
| 896 | * |
| 897 | * On legacy hierachy, effective_cpus will be the same with cpu_allowed. |
| 898 | * |
| 899 | * Called with cpuset_mutex held |
| 900 | */ |
| 901 | static void update_cpumasks_hier(struct cpuset *cs, struct cpumask *new_cpus) |
| 902 | { |
| 903 | struct cpuset *cp; |
| 904 | struct cgroup_subsys_state *pos_css; |
| 905 | bool need_rebuild_sched_domains = false; |
| 906 | |
| 907 | rcu_read_lock(); |
| 908 | cpuset_for_each_descendant_pre(cp, pos_css, cs) { |
| 909 | struct cpuset *parent = parent_cs(cp); |
| 910 | |
| 911 | cpumask_and(new_cpus, cp->cpus_allowed, parent->effective_cpus); |
| 912 | |
| 913 | /* |
| 914 | * If it becomes empty, inherit the effective mask of the |
| 915 | * parent, which is guaranteed to have some CPUs. |
| 916 | */ |
| 917 | if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 918 | cpumask_empty(new_cpus)) |
| 919 | cpumask_copy(new_cpus, parent->effective_cpus); |
| 920 | |
| 921 | /* Skip the whole subtree if the cpumask remains the same. */ |
| 922 | if (cpumask_equal(new_cpus, cp->effective_cpus)) { |
| 923 | pos_css = css_rightmost_descendant(pos_css); |
| 924 | continue; |
| 925 | } |
| 926 | |
| 927 | if (!css_tryget_online(&cp->css)) |
| 928 | continue; |
| 929 | rcu_read_unlock(); |
| 930 | |
| 931 | spin_lock_irq(&callback_lock); |
| 932 | cpumask_copy(cp->effective_cpus, new_cpus); |
| 933 | spin_unlock_irq(&callback_lock); |
| 934 | |
| 935 | WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 936 | !cpumask_equal(cp->cpus_allowed, cp->effective_cpus)); |
| 937 | |
| 938 | update_tasks_cpumask(cp); |
| 939 | |
| 940 | /* |
| 941 | * If the effective cpumask of any non-empty cpuset is changed, |
| 942 | * we need to rebuild sched domains. |
| 943 | */ |
| 944 | if (!cpumask_empty(cp->cpus_allowed) && |
| 945 | is_sched_load_balance(cp)) |
| 946 | need_rebuild_sched_domains = true; |
| 947 | |
| 948 | rcu_read_lock(); |
| 949 | css_put(&cp->css); |
| 950 | } |
| 951 | rcu_read_unlock(); |
| 952 | |
| 953 | if (need_rebuild_sched_domains) |
| 954 | rebuild_sched_domains_locked(); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it |
| 959 | * @cs: the cpuset to consider |
| 960 | * @trialcs: trial cpuset |
| 961 | * @buf: buffer of cpu numbers written to this cpuset |
| 962 | */ |
| 963 | static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, |
| 964 | const char *buf) |
| 965 | { |
| 966 | int retval; |
| 967 | |
| 968 | /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */ |
| 969 | if (cs == &top_cpuset) |
| 970 | return -EACCES; |
| 971 | |
| 972 | /* |
| 973 | * An empty cpus_allowed is ok only if the cpuset has no tasks. |
| 974 | * Since cpulist_parse() fails on an empty mask, we special case |
| 975 | * that parsing. The validate_change() call ensures that cpusets |
| 976 | * with tasks have cpus. |
| 977 | */ |
| 978 | if (!*buf) { |
| 979 | cpumask_clear(trialcs->cpus_allowed); |
| 980 | } else { |
| 981 | retval = cpulist_parse(buf, trialcs->cpus_requested); |
| 982 | if (retval < 0) |
| 983 | return retval; |
| 984 | |
| 985 | if (!cpumask_subset(trialcs->cpus_requested, cpu_present_mask)) |
| 986 | return -EINVAL; |
| 987 | |
| 988 | cpumask_and(trialcs->cpus_allowed, trialcs->cpus_requested, cpu_active_mask); |
| 989 | } |
| 990 | |
| 991 | /* Nothing to do if the cpus didn't change */ |
| 992 | if (cpumask_equal(cs->cpus_requested, trialcs->cpus_requested)) |
| 993 | return 0; |
| 994 | |
| 995 | retval = validate_change(cs, trialcs); |
| 996 | if (retval < 0) |
| 997 | return retval; |
| 998 | |
| 999 | spin_lock_irq(&callback_lock); |
| 1000 | cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); |
| 1001 | cpumask_copy(cs->cpus_requested, trialcs->cpus_requested); |
| 1002 | spin_unlock_irq(&callback_lock); |
| 1003 | |
| 1004 | /* use trialcs->cpus_allowed as a temp variable */ |
| 1005 | update_cpumasks_hier(cs, trialcs->cpus_allowed); |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Migrate memory region from one set of nodes to another. This is |
| 1011 | * performed asynchronously as it can be called from process migration path |
| 1012 | * holding locks involved in process management. All mm migrations are |
| 1013 | * performed in the queued order and can be waited for by flushing |
| 1014 | * cpuset_migrate_mm_wq. |
| 1015 | */ |
| 1016 | |
| 1017 | struct cpuset_migrate_mm_work { |
| 1018 | struct work_struct work; |
| 1019 | struct mm_struct *mm; |
| 1020 | nodemask_t from; |
| 1021 | nodemask_t to; |
| 1022 | }; |
| 1023 | |
| 1024 | static void cpuset_migrate_mm_workfn(struct work_struct *work) |
| 1025 | { |
| 1026 | struct cpuset_migrate_mm_work *mwork = |
| 1027 | container_of(work, struct cpuset_migrate_mm_work, work); |
| 1028 | |
| 1029 | /* on a wq worker, no need to worry about %current's mems_allowed */ |
| 1030 | do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL); |
| 1031 | mmput(mwork->mm); |
| 1032 | kfree(mwork); |
| 1033 | } |
| 1034 | |
| 1035 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, |
| 1036 | const nodemask_t *to) |
| 1037 | { |
| 1038 | struct cpuset_migrate_mm_work *mwork; |
| 1039 | |
| 1040 | mwork = kzalloc(sizeof(*mwork), GFP_KERNEL); |
| 1041 | if (mwork) { |
| 1042 | mwork->mm = mm; |
| 1043 | mwork->from = *from; |
| 1044 | mwork->to = *to; |
| 1045 | INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn); |
| 1046 | queue_work(cpuset_migrate_mm_wq, &mwork->work); |
| 1047 | } else { |
| 1048 | mmput(mm); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | static void cpuset_post_attach(void) |
| 1053 | { |
| 1054 | flush_workqueue(cpuset_migrate_mm_wq); |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy |
| 1059 | * @tsk: the task to change |
| 1060 | * @newmems: new nodes that the task will be set |
| 1061 | * |
| 1062 | * In order to avoid seeing no nodes if the old and new nodes are disjoint, |
| 1063 | * we structure updates as setting all new allowed nodes, then clearing newly |
| 1064 | * disallowed ones. |
| 1065 | */ |
| 1066 | static void cpuset_change_task_nodemask(struct task_struct *tsk, |
| 1067 | nodemask_t *newmems) |
| 1068 | { |
| 1069 | bool need_loop; |
| 1070 | |
| 1071 | /* |
| 1072 | * Allow tasks that have access to memory reserves because they have |
| 1073 | * been OOM killed to get memory anywhere. |
| 1074 | */ |
| 1075 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 1076 | return; |
| 1077 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 1078 | return; |
| 1079 | |
| 1080 | task_lock(tsk); |
| 1081 | /* |
| 1082 | * Determine if a loop is necessary if another thread is doing |
| 1083 | * read_mems_allowed_begin(). If at least one node remains unchanged and |
| 1084 | * tsk does not have a mempolicy, then an empty nodemask will not be |
| 1085 | * possible when mems_allowed is larger than a word. |
| 1086 | */ |
| 1087 | need_loop = task_has_mempolicy(tsk) || |
| 1088 | !nodes_intersects(*newmems, tsk->mems_allowed); |
| 1089 | |
| 1090 | if (need_loop) { |
| 1091 | local_irq_disable(); |
| 1092 | write_seqcount_begin(&tsk->mems_allowed_seq); |
| 1093 | } |
| 1094 | |
| 1095 | nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); |
| 1096 | mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1); |
| 1097 | |
| 1098 | mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2); |
| 1099 | tsk->mems_allowed = *newmems; |
| 1100 | |
| 1101 | if (need_loop) { |
| 1102 | write_seqcount_end(&tsk->mems_allowed_seq); |
| 1103 | local_irq_enable(); |
| 1104 | } |
| 1105 | |
| 1106 | task_unlock(tsk); |
| 1107 | } |
| 1108 | |
| 1109 | static void *cpuset_being_rebound; |
| 1110 | |
| 1111 | /** |
| 1112 | * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. |
| 1113 | * @cs: the cpuset in which each task's mems_allowed mask needs to be changed |
| 1114 | * |
| 1115 | * Iterate through each task of @cs updating its mems_allowed to the |
| 1116 | * effective cpuset's. As this function is called with cpuset_mutex held, |
| 1117 | * cpuset membership stays stable. |
| 1118 | */ |
| 1119 | static void update_tasks_nodemask(struct cpuset *cs) |
| 1120 | { |
| 1121 | static nodemask_t newmems; /* protected by cpuset_mutex */ |
| 1122 | struct css_task_iter it; |
| 1123 | struct task_struct *task; |
| 1124 | |
| 1125 | cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ |
| 1126 | |
| 1127 | guarantee_online_mems(cs, &newmems); |
| 1128 | |
| 1129 | /* |
| 1130 | * The mpol_rebind_mm() call takes mmap_sem, which we couldn't |
| 1131 | * take while holding tasklist_lock. Forks can happen - the |
| 1132 | * mpol_dup() cpuset_being_rebound check will catch such forks, |
| 1133 | * and rebind their vma mempolicies too. Because we still hold |
| 1134 | * the global cpuset_mutex, we know that no other rebind effort |
| 1135 | * will be contending for the global variable cpuset_being_rebound. |
| 1136 | * It's ok if we rebind the same mm twice; mpol_rebind_mm() |
| 1137 | * is idempotent. Also migrate pages in each mm to new nodes. |
| 1138 | */ |
| 1139 | css_task_iter_start(&cs->css, &it); |
| 1140 | while ((task = css_task_iter_next(&it))) { |
| 1141 | struct mm_struct *mm; |
| 1142 | bool migrate; |
| 1143 | |
| 1144 | cpuset_change_task_nodemask(task, &newmems); |
| 1145 | |
| 1146 | mm = get_task_mm(task); |
| 1147 | if (!mm) |
| 1148 | continue; |
| 1149 | |
| 1150 | migrate = is_memory_migrate(cs); |
| 1151 | |
| 1152 | mpol_rebind_mm(mm, &cs->mems_allowed); |
| 1153 | if (migrate) |
| 1154 | cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems); |
| 1155 | else |
| 1156 | mmput(mm); |
| 1157 | } |
| 1158 | css_task_iter_end(&it); |
| 1159 | |
| 1160 | /* |
| 1161 | * All the tasks' nodemasks have been updated, update |
| 1162 | * cs->old_mems_allowed. |
| 1163 | */ |
| 1164 | cs->old_mems_allowed = newmems; |
| 1165 | |
| 1166 | /* We're done rebinding vmas to this cpuset's new mems_allowed. */ |
| 1167 | cpuset_being_rebound = NULL; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree |
| 1172 | * @cs: the cpuset to consider |
| 1173 | * @new_mems: a temp variable for calculating new effective_mems |
| 1174 | * |
| 1175 | * When configured nodemask is changed, the effective nodemasks of this cpuset |
| 1176 | * and all its descendants need to be updated. |
| 1177 | * |
| 1178 | * On legacy hiearchy, effective_mems will be the same with mems_allowed. |
| 1179 | * |
| 1180 | * Called with cpuset_mutex held |
| 1181 | */ |
| 1182 | static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems) |
| 1183 | { |
| 1184 | struct cpuset *cp; |
| 1185 | struct cgroup_subsys_state *pos_css; |
| 1186 | |
| 1187 | rcu_read_lock(); |
| 1188 | cpuset_for_each_descendant_pre(cp, pos_css, cs) { |
| 1189 | struct cpuset *parent = parent_cs(cp); |
| 1190 | |
| 1191 | nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems); |
| 1192 | |
| 1193 | /* |
| 1194 | * If it becomes empty, inherit the effective mask of the |
| 1195 | * parent, which is guaranteed to have some MEMs. |
| 1196 | */ |
| 1197 | if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 1198 | nodes_empty(*new_mems)) |
| 1199 | *new_mems = parent->effective_mems; |
| 1200 | |
| 1201 | /* Skip the whole subtree if the nodemask remains the same. */ |
| 1202 | if (nodes_equal(*new_mems, cp->effective_mems)) { |
| 1203 | pos_css = css_rightmost_descendant(pos_css); |
| 1204 | continue; |
| 1205 | } |
| 1206 | |
| 1207 | if (!css_tryget_online(&cp->css)) |
| 1208 | continue; |
| 1209 | rcu_read_unlock(); |
| 1210 | |
| 1211 | spin_lock_irq(&callback_lock); |
| 1212 | cp->effective_mems = *new_mems; |
| 1213 | spin_unlock_irq(&callback_lock); |
| 1214 | |
| 1215 | WARN_ON(!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 1216 | !nodes_equal(cp->mems_allowed, cp->effective_mems)); |
| 1217 | |
| 1218 | update_tasks_nodemask(cp); |
| 1219 | |
| 1220 | rcu_read_lock(); |
| 1221 | css_put(&cp->css); |
| 1222 | } |
| 1223 | rcu_read_unlock(); |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Handle user request to change the 'mems' memory placement |
| 1228 | * of a cpuset. Needs to validate the request, update the |
| 1229 | * cpusets mems_allowed, and for each task in the cpuset, |
| 1230 | * update mems_allowed and rebind task's mempolicy and any vma |
| 1231 | * mempolicies and if the cpuset is marked 'memory_migrate', |
| 1232 | * migrate the tasks pages to the new memory. |
| 1233 | * |
| 1234 | * Call with cpuset_mutex held. May take callback_lock during call. |
| 1235 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
| 1236 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind |
| 1237 | * their mempolicies to the cpusets new mems_allowed. |
| 1238 | */ |
| 1239 | static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, |
| 1240 | const char *buf) |
| 1241 | { |
| 1242 | int retval; |
| 1243 | |
| 1244 | /* |
| 1245 | * top_cpuset.mems_allowed tracks node_stats[N_MEMORY]; |
| 1246 | * it's read-only |
| 1247 | */ |
| 1248 | if (cs == &top_cpuset) { |
| 1249 | retval = -EACCES; |
| 1250 | goto done; |
| 1251 | } |
| 1252 | |
| 1253 | /* |
| 1254 | * An empty mems_allowed is ok iff there are no tasks in the cpuset. |
| 1255 | * Since nodelist_parse() fails on an empty mask, we special case |
| 1256 | * that parsing. The validate_change() call ensures that cpusets |
| 1257 | * with tasks have memory. |
| 1258 | */ |
| 1259 | if (!*buf) { |
| 1260 | nodes_clear(trialcs->mems_allowed); |
| 1261 | } else { |
| 1262 | retval = nodelist_parse(buf, trialcs->mems_allowed); |
| 1263 | if (retval < 0) |
| 1264 | goto done; |
| 1265 | |
| 1266 | if (!nodes_subset(trialcs->mems_allowed, |
| 1267 | top_cpuset.mems_allowed)) { |
| 1268 | retval = -EINVAL; |
| 1269 | goto done; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) { |
| 1274 | retval = 0; /* Too easy - nothing to do */ |
| 1275 | goto done; |
| 1276 | } |
| 1277 | retval = validate_change(cs, trialcs); |
| 1278 | if (retval < 0) |
| 1279 | goto done; |
| 1280 | |
| 1281 | spin_lock_irq(&callback_lock); |
| 1282 | cs->mems_allowed = trialcs->mems_allowed; |
| 1283 | spin_unlock_irq(&callback_lock); |
| 1284 | |
| 1285 | /* use trialcs->mems_allowed as a temp variable */ |
| 1286 | update_nodemasks_hier(cs, &trialcs->mems_allowed); |
| 1287 | done: |
| 1288 | return retval; |
| 1289 | } |
| 1290 | |
| 1291 | int current_cpuset_is_being_rebound(void) |
| 1292 | { |
| 1293 | int ret; |
| 1294 | |
| 1295 | rcu_read_lock(); |
| 1296 | ret = task_cs(current) == cpuset_being_rebound; |
| 1297 | rcu_read_unlock(); |
| 1298 | |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | static int update_relax_domain_level(struct cpuset *cs, s64 val) |
| 1303 | { |
| 1304 | #ifdef CONFIG_SMP |
| 1305 | if (val < -1 || val >= sched_domain_level_max) |
| 1306 | return -EINVAL; |
| 1307 | #endif |
| 1308 | |
| 1309 | if (val != cs->relax_domain_level) { |
| 1310 | cs->relax_domain_level = val; |
| 1311 | if (!cpumask_empty(cs->cpus_allowed) && |
| 1312 | is_sched_load_balance(cs)) |
| 1313 | rebuild_sched_domains_locked(); |
| 1314 | } |
| 1315 | |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * update_tasks_flags - update the spread flags of tasks in the cpuset. |
| 1321 | * @cs: the cpuset in which each task's spread flags needs to be changed |
| 1322 | * |
| 1323 | * Iterate through each task of @cs updating its spread flags. As this |
| 1324 | * function is called with cpuset_mutex held, cpuset membership stays |
| 1325 | * stable. |
| 1326 | */ |
| 1327 | static void update_tasks_flags(struct cpuset *cs) |
| 1328 | { |
| 1329 | struct css_task_iter it; |
| 1330 | struct task_struct *task; |
| 1331 | |
| 1332 | css_task_iter_start(&cs->css, &it); |
| 1333 | while ((task = css_task_iter_next(&it))) |
| 1334 | cpuset_update_task_spread_flag(cs, task); |
| 1335 | css_task_iter_end(&it); |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * update_flag - read a 0 or a 1 in a file and update associated flag |
| 1340 | * bit: the bit to update (see cpuset_flagbits_t) |
| 1341 | * cs: the cpuset to update |
| 1342 | * turning_on: whether the flag is being set or cleared |
| 1343 | * |
| 1344 | * Call with cpuset_mutex held. |
| 1345 | */ |
| 1346 | |
| 1347 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, |
| 1348 | int turning_on) |
| 1349 | { |
| 1350 | struct cpuset *trialcs; |
| 1351 | int balance_flag_changed; |
| 1352 | int spread_flag_changed; |
| 1353 | int err; |
| 1354 | |
| 1355 | trialcs = alloc_trial_cpuset(cs); |
| 1356 | if (!trialcs) |
| 1357 | return -ENOMEM; |
| 1358 | |
| 1359 | if (turning_on) |
| 1360 | set_bit(bit, &trialcs->flags); |
| 1361 | else |
| 1362 | clear_bit(bit, &trialcs->flags); |
| 1363 | |
| 1364 | err = validate_change(cs, trialcs); |
| 1365 | if (err < 0) |
| 1366 | goto out; |
| 1367 | |
| 1368 | balance_flag_changed = (is_sched_load_balance(cs) != |
| 1369 | is_sched_load_balance(trialcs)); |
| 1370 | |
| 1371 | spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs)) |
| 1372 | || (is_spread_page(cs) != is_spread_page(trialcs))); |
| 1373 | |
| 1374 | spin_lock_irq(&callback_lock); |
| 1375 | cs->flags = trialcs->flags; |
| 1376 | spin_unlock_irq(&callback_lock); |
| 1377 | |
| 1378 | if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) |
| 1379 | rebuild_sched_domains_locked(); |
| 1380 | |
| 1381 | if (spread_flag_changed) |
| 1382 | update_tasks_flags(cs); |
| 1383 | out: |
| 1384 | free_trial_cpuset(trialcs); |
| 1385 | return err; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Frequency meter - How fast is some event occurring? |
| 1390 | * |
| 1391 | * These routines manage a digitally filtered, constant time based, |
| 1392 | * event frequency meter. There are four routines: |
| 1393 | * fmeter_init() - initialize a frequency meter. |
| 1394 | * fmeter_markevent() - called each time the event happens. |
| 1395 | * fmeter_getrate() - returns the recent rate of such events. |
| 1396 | * fmeter_update() - internal routine used to update fmeter. |
| 1397 | * |
| 1398 | * A common data structure is passed to each of these routines, |
| 1399 | * which is used to keep track of the state required to manage the |
| 1400 | * frequency meter and its digital filter. |
| 1401 | * |
| 1402 | * The filter works on the number of events marked per unit time. |
| 1403 | * The filter is single-pole low-pass recursive (IIR). The time unit |
| 1404 | * is 1 second. Arithmetic is done using 32-bit integers scaled to |
| 1405 | * simulate 3 decimal digits of precision (multiplied by 1000). |
| 1406 | * |
| 1407 | * With an FM_COEF of 933, and a time base of 1 second, the filter |
| 1408 | * has a half-life of 10 seconds, meaning that if the events quit |
| 1409 | * happening, then the rate returned from the fmeter_getrate() |
| 1410 | * will be cut in half each 10 seconds, until it converges to zero. |
| 1411 | * |
| 1412 | * It is not worth doing a real infinitely recursive filter. If more |
| 1413 | * than FM_MAXTICKS ticks have elapsed since the last filter event, |
| 1414 | * just compute FM_MAXTICKS ticks worth, by which point the level |
| 1415 | * will be stable. |
| 1416 | * |
| 1417 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid |
| 1418 | * arithmetic overflow in the fmeter_update() routine. |
| 1419 | * |
| 1420 | * Given the simple 32 bit integer arithmetic used, this meter works |
| 1421 | * best for reporting rates between one per millisecond (msec) and |
| 1422 | * one per 32 (approx) seconds. At constant rates faster than one |
| 1423 | * per msec it maxes out at values just under 1,000,000. At constant |
| 1424 | * rates between one per msec, and one per second it will stabilize |
| 1425 | * to a value N*1000, where N is the rate of events per second. |
| 1426 | * At constant rates between one per second and one per 32 seconds, |
| 1427 | * it will be choppy, moving up on the seconds that have an event, |
| 1428 | * and then decaying until the next event. At rates slower than |
| 1429 | * about one in 32 seconds, it decays all the way back to zero between |
| 1430 | * each event. |
| 1431 | */ |
| 1432 | |
| 1433 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ |
| 1434 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ |
| 1435 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ |
| 1436 | #define FM_SCALE 1000 /* faux fixed point scale */ |
| 1437 | |
| 1438 | /* Initialize a frequency meter */ |
| 1439 | static void fmeter_init(struct fmeter *fmp) |
| 1440 | { |
| 1441 | fmp->cnt = 0; |
| 1442 | fmp->val = 0; |
| 1443 | fmp->time = 0; |
| 1444 | spin_lock_init(&fmp->lock); |
| 1445 | } |
| 1446 | |
| 1447 | /* Internal meter update - process cnt events and update value */ |
| 1448 | static void fmeter_update(struct fmeter *fmp) |
| 1449 | { |
| 1450 | time_t now = get_seconds(); |
| 1451 | time_t ticks = now - fmp->time; |
| 1452 | |
| 1453 | if (ticks == 0) |
| 1454 | return; |
| 1455 | |
| 1456 | ticks = min(FM_MAXTICKS, ticks); |
| 1457 | while (ticks-- > 0) |
| 1458 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; |
| 1459 | fmp->time = now; |
| 1460 | |
| 1461 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; |
| 1462 | fmp->cnt = 0; |
| 1463 | } |
| 1464 | |
| 1465 | /* Process any previous ticks, then bump cnt by one (times scale). */ |
| 1466 | static void fmeter_markevent(struct fmeter *fmp) |
| 1467 | { |
| 1468 | spin_lock(&fmp->lock); |
| 1469 | fmeter_update(fmp); |
| 1470 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); |
| 1471 | spin_unlock(&fmp->lock); |
| 1472 | } |
| 1473 | |
| 1474 | /* Process any previous ticks, then return current value. */ |
| 1475 | static int fmeter_getrate(struct fmeter *fmp) |
| 1476 | { |
| 1477 | int val; |
| 1478 | |
| 1479 | spin_lock(&fmp->lock); |
| 1480 | fmeter_update(fmp); |
| 1481 | val = fmp->val; |
| 1482 | spin_unlock(&fmp->lock); |
| 1483 | return val; |
| 1484 | } |
| 1485 | |
| 1486 | static struct cpuset *cpuset_attach_old_cs; |
| 1487 | |
| 1488 | /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */ |
| 1489 | static int cpuset_can_attach(struct cgroup_taskset *tset) |
| 1490 | { |
| 1491 | struct cgroup_subsys_state *css; |
| 1492 | struct cpuset *cs; |
| 1493 | struct task_struct *task; |
| 1494 | int ret; |
| 1495 | |
| 1496 | /* used later by cpuset_attach() */ |
| 1497 | cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css)); |
| 1498 | cs = css_cs(css); |
| 1499 | |
| 1500 | mutex_lock(&cpuset_mutex); |
| 1501 | |
| 1502 | /* allow moving tasks into an empty cpuset if on default hierarchy */ |
| 1503 | ret = -ENOSPC; |
| 1504 | if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) && |
| 1505 | (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))) |
| 1506 | goto out_unlock; |
| 1507 | |
| 1508 | cgroup_taskset_for_each(task, css, tset) { |
| 1509 | ret = task_can_attach(task, cs->cpus_allowed); |
| 1510 | if (ret) |
| 1511 | goto out_unlock; |
| 1512 | ret = security_task_setscheduler(task); |
| 1513 | if (ret) |
| 1514 | goto out_unlock; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Mark attach is in progress. This makes validate_change() fail |
| 1519 | * changes which zero cpus/mems_allowed. |
| 1520 | */ |
| 1521 | cs->attach_in_progress++; |
| 1522 | ret = 0; |
| 1523 | out_unlock: |
| 1524 | mutex_unlock(&cpuset_mutex); |
| 1525 | return ret; |
| 1526 | } |
| 1527 | |
| 1528 | static void cpuset_cancel_attach(struct cgroup_taskset *tset) |
| 1529 | { |
| 1530 | struct cgroup_subsys_state *css; |
| 1531 | struct cpuset *cs; |
| 1532 | |
| 1533 | cgroup_taskset_first(tset, &css); |
| 1534 | cs = css_cs(css); |
| 1535 | |
| 1536 | mutex_lock(&cpuset_mutex); |
| 1537 | css_cs(css)->attach_in_progress--; |
| 1538 | mutex_unlock(&cpuset_mutex); |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach() |
| 1543 | * but we can't allocate it dynamically there. Define it global and |
| 1544 | * allocate from cpuset_init(). |
| 1545 | */ |
| 1546 | static cpumask_var_t cpus_attach; |
| 1547 | |
| 1548 | static void cpuset_attach(struct cgroup_taskset *tset) |
| 1549 | { |
| 1550 | /* static buf protected by cpuset_mutex */ |
| 1551 | static nodemask_t cpuset_attach_nodemask_to; |
| 1552 | struct task_struct *task; |
| 1553 | struct task_struct *leader; |
| 1554 | struct cgroup_subsys_state *css; |
| 1555 | struct cpuset *cs; |
| 1556 | struct cpuset *oldcs = cpuset_attach_old_cs; |
| 1557 | |
| 1558 | cgroup_taskset_first(tset, &css); |
| 1559 | cs = css_cs(css); |
| 1560 | |
| 1561 | mutex_lock(&cpuset_mutex); |
| 1562 | |
| 1563 | /* prepare for attach */ |
| 1564 | if (cs == &top_cpuset) |
| 1565 | cpumask_copy(cpus_attach, cpu_possible_mask); |
| 1566 | else |
| 1567 | guarantee_online_cpus(cs, cpus_attach); |
| 1568 | |
| 1569 | guarantee_online_mems(cs, &cpuset_attach_nodemask_to); |
| 1570 | |
| 1571 | cgroup_taskset_for_each(task, css, tset) { |
| 1572 | /* |
| 1573 | * can_attach beforehand should guarantee that this doesn't |
| 1574 | * fail. TODO: have a better way to handle failure here |
| 1575 | */ |
| 1576 | WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); |
| 1577 | |
| 1578 | cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); |
| 1579 | cpuset_update_task_spread_flag(cs, task); |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * Change mm for all threadgroup leaders. This is expensive and may |
| 1584 | * sleep and should be moved outside migration path proper. |
| 1585 | */ |
| 1586 | cpuset_attach_nodemask_to = cs->effective_mems; |
| 1587 | cgroup_taskset_for_each_leader(leader, css, tset) { |
| 1588 | struct mm_struct *mm = get_task_mm(leader); |
| 1589 | |
| 1590 | if (mm) { |
| 1591 | mpol_rebind_mm(mm, &cpuset_attach_nodemask_to); |
| 1592 | |
| 1593 | /* |
| 1594 | * old_mems_allowed is the same with mems_allowed |
| 1595 | * here, except if this task is being moved |
| 1596 | * automatically due to hotplug. In that case |
| 1597 | * @mems_allowed has been updated and is empty, so |
| 1598 | * @old_mems_allowed is the right nodesets that we |
| 1599 | * migrate mm from. |
| 1600 | */ |
| 1601 | if (is_memory_migrate(cs)) |
| 1602 | cpuset_migrate_mm(mm, &oldcs->old_mems_allowed, |
| 1603 | &cpuset_attach_nodemask_to); |
| 1604 | else |
| 1605 | mmput(mm); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | cs->old_mems_allowed = cpuset_attach_nodemask_to; |
| 1610 | |
| 1611 | cs->attach_in_progress--; |
| 1612 | if (!cs->attach_in_progress) |
| 1613 | wake_up(&cpuset_attach_wq); |
| 1614 | |
| 1615 | mutex_unlock(&cpuset_mutex); |
| 1616 | } |
| 1617 | |
| 1618 | /* The various types of files and directories in a cpuset file system */ |
| 1619 | |
| 1620 | typedef enum { |
| 1621 | FILE_MEMORY_MIGRATE, |
| 1622 | FILE_CPULIST, |
| 1623 | FILE_MEMLIST, |
| 1624 | FILE_EFFECTIVE_CPULIST, |
| 1625 | FILE_EFFECTIVE_MEMLIST, |
| 1626 | FILE_CPU_EXCLUSIVE, |
| 1627 | FILE_MEM_EXCLUSIVE, |
| 1628 | FILE_MEM_HARDWALL, |
| 1629 | FILE_SCHED_LOAD_BALANCE, |
| 1630 | FILE_SCHED_RELAX_DOMAIN_LEVEL, |
| 1631 | FILE_MEMORY_PRESSURE_ENABLED, |
| 1632 | FILE_MEMORY_PRESSURE, |
| 1633 | FILE_SPREAD_PAGE, |
| 1634 | FILE_SPREAD_SLAB, |
| 1635 | FILE_FAMILY_BOOST, |
| 1636 | } cpuset_filetype_t; |
| 1637 | |
| 1638 | static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft, |
| 1639 | u64 val) |
| 1640 | { |
| 1641 | struct cpuset *cs = css_cs(css); |
| 1642 | cpuset_filetype_t type = cft->private; |
| 1643 | int retval = 0; |
| 1644 | |
| 1645 | mutex_lock(&cpuset_mutex); |
| 1646 | if (!is_cpuset_online(cs)) { |
| 1647 | retval = -ENODEV; |
| 1648 | goto out_unlock; |
| 1649 | } |
| 1650 | |
| 1651 | switch (type) { |
| 1652 | case FILE_CPU_EXCLUSIVE: |
| 1653 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, val); |
| 1654 | break; |
| 1655 | case FILE_MEM_EXCLUSIVE: |
| 1656 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, val); |
| 1657 | break; |
| 1658 | case FILE_MEM_HARDWALL: |
| 1659 | retval = update_flag(CS_MEM_HARDWALL, cs, val); |
| 1660 | break; |
| 1661 | case FILE_SCHED_LOAD_BALANCE: |
| 1662 | retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val); |
| 1663 | break; |
| 1664 | case FILE_MEMORY_MIGRATE: |
| 1665 | retval = update_flag(CS_MEMORY_MIGRATE, cs, val); |
| 1666 | break; |
| 1667 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1668 | cpuset_memory_pressure_enabled = !!val; |
| 1669 | break; |
| 1670 | case FILE_SPREAD_PAGE: |
| 1671 | retval = update_flag(CS_SPREAD_PAGE, cs, val); |
| 1672 | break; |
| 1673 | case FILE_SPREAD_SLAB: |
| 1674 | retval = update_flag(CS_SPREAD_SLAB, cs, val); |
| 1675 | break; |
| 1676 | case FILE_FAMILY_BOOST: |
| 1677 | retval = update_flag(CS_FAMILY_BOOST, cs, val); |
| 1678 | break; |
| 1679 | default: |
| 1680 | retval = -EINVAL; |
| 1681 | break; |
| 1682 | } |
| 1683 | out_unlock: |
| 1684 | mutex_unlock(&cpuset_mutex); |
| 1685 | return retval; |
| 1686 | } |
| 1687 | |
| 1688 | static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft, |
| 1689 | s64 val) |
| 1690 | { |
| 1691 | struct cpuset *cs = css_cs(css); |
| 1692 | cpuset_filetype_t type = cft->private; |
| 1693 | int retval = -ENODEV; |
| 1694 | |
| 1695 | mutex_lock(&cpuset_mutex); |
| 1696 | if (!is_cpuset_online(cs)) |
| 1697 | goto out_unlock; |
| 1698 | |
| 1699 | switch (type) { |
| 1700 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: |
| 1701 | retval = update_relax_domain_level(cs, val); |
| 1702 | break; |
| 1703 | default: |
| 1704 | retval = -EINVAL; |
| 1705 | break; |
| 1706 | } |
| 1707 | out_unlock: |
| 1708 | mutex_unlock(&cpuset_mutex); |
| 1709 | return retval; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * Common handling for a write to a "cpus" or "mems" file. |
| 1714 | */ |
| 1715 | static ssize_t cpuset_write_resmask(struct kernfs_open_file *of, |
| 1716 | char *buf, size_t nbytes, loff_t off) |
| 1717 | { |
| 1718 | struct cpuset *cs = css_cs(of_css(of)); |
| 1719 | struct cpuset *trialcs; |
| 1720 | int retval = -ENODEV; |
| 1721 | |
| 1722 | buf = strstrip(buf); |
| 1723 | |
| 1724 | /* |
| 1725 | * CPU or memory hotunplug may leave @cs w/o any execution |
| 1726 | * resources, in which case the hotplug code asynchronously updates |
| 1727 | * configuration and transfers all tasks to the nearest ancestor |
| 1728 | * which can execute. |
| 1729 | * |
| 1730 | * As writes to "cpus" or "mems" may restore @cs's execution |
| 1731 | * resources, wait for the previously scheduled operations before |
| 1732 | * proceeding, so that we don't end up keep removing tasks added |
| 1733 | * after execution capability is restored. |
| 1734 | * |
| 1735 | * cpuset_hotplug_work calls back into cgroup core via |
| 1736 | * cgroup_transfer_tasks() and waiting for it from a cgroupfs |
| 1737 | * operation like this one can lead to a deadlock through kernfs |
| 1738 | * active_ref protection. Let's break the protection. Losing the |
| 1739 | * protection is okay as we check whether @cs is online after |
| 1740 | * grabbing cpuset_mutex anyway. This only happens on the legacy |
| 1741 | * hierarchies. |
| 1742 | */ |
| 1743 | css_get(&cs->css); |
| 1744 | kernfs_break_active_protection(of->kn); |
| 1745 | flush_work(&cpuset_hotplug_work); |
| 1746 | |
| 1747 | mutex_lock(&cpuset_mutex); |
| 1748 | if (!is_cpuset_online(cs)) |
| 1749 | goto out_unlock; |
| 1750 | |
| 1751 | trialcs = alloc_trial_cpuset(cs); |
| 1752 | if (!trialcs) { |
| 1753 | retval = -ENOMEM; |
| 1754 | goto out_unlock; |
| 1755 | } |
| 1756 | |
| 1757 | switch (of_cft(of)->private) { |
| 1758 | case FILE_CPULIST: |
| 1759 | retval = update_cpumask(cs, trialcs, buf); |
| 1760 | break; |
| 1761 | case FILE_MEMLIST: |
| 1762 | retval = update_nodemask(cs, trialcs, buf); |
| 1763 | break; |
| 1764 | default: |
| 1765 | retval = -EINVAL; |
| 1766 | break; |
| 1767 | } |
| 1768 | |
| 1769 | free_trial_cpuset(trialcs); |
| 1770 | out_unlock: |
| 1771 | mutex_unlock(&cpuset_mutex); |
| 1772 | kernfs_unbreak_active_protection(of->kn); |
| 1773 | css_put(&cs->css); |
| 1774 | flush_workqueue(cpuset_migrate_mm_wq); |
| 1775 | return retval ?: nbytes; |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * These ascii lists should be read in a single call, by using a user |
| 1780 | * buffer large enough to hold the entire map. If read in smaller |
| 1781 | * chunks, there is no guarantee of atomicity. Since the display format |
| 1782 | * used, list of ranges of sequential numbers, is variable length, |
| 1783 | * and since these maps can change value dynamically, one could read |
| 1784 | * gibberish by doing partial reads while a list was changing. |
| 1785 | */ |
| 1786 | static int cpuset_common_seq_show(struct seq_file *sf, void *v) |
| 1787 | { |
| 1788 | struct cpuset *cs = css_cs(seq_css(sf)); |
| 1789 | cpuset_filetype_t type = seq_cft(sf)->private; |
| 1790 | int ret = 0; |
| 1791 | |
| 1792 | spin_lock_irq(&callback_lock); |
| 1793 | |
| 1794 | switch (type) { |
| 1795 | case FILE_CPULIST: |
| 1796 | seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_requested)); |
| 1797 | break; |
| 1798 | case FILE_MEMLIST: |
| 1799 | seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed)); |
| 1800 | break; |
| 1801 | case FILE_EFFECTIVE_CPULIST: |
| 1802 | seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus)); |
| 1803 | break; |
| 1804 | case FILE_EFFECTIVE_MEMLIST: |
| 1805 | seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems)); |
| 1806 | break; |
| 1807 | default: |
| 1808 | ret = -EINVAL; |
| 1809 | } |
| 1810 | |
| 1811 | spin_unlock_irq(&callback_lock); |
| 1812 | return ret; |
| 1813 | } |
| 1814 | |
| 1815 | static u64 cpuset_read_u64(struct cgroup_subsys_state *css, struct cftype *cft) |
| 1816 | { |
| 1817 | struct cpuset *cs = css_cs(css); |
| 1818 | cpuset_filetype_t type = cft->private; |
| 1819 | switch (type) { |
| 1820 | case FILE_CPU_EXCLUSIVE: |
| 1821 | return is_cpu_exclusive(cs); |
| 1822 | case FILE_MEM_EXCLUSIVE: |
| 1823 | return is_mem_exclusive(cs); |
| 1824 | case FILE_MEM_HARDWALL: |
| 1825 | return is_mem_hardwall(cs); |
| 1826 | case FILE_SCHED_LOAD_BALANCE: |
| 1827 | return is_sched_load_balance(cs); |
| 1828 | case FILE_MEMORY_MIGRATE: |
| 1829 | return is_memory_migrate(cs); |
| 1830 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1831 | return cpuset_memory_pressure_enabled; |
| 1832 | case FILE_MEMORY_PRESSURE: |
| 1833 | return fmeter_getrate(&cs->fmeter); |
| 1834 | case FILE_SPREAD_PAGE: |
| 1835 | return is_spread_page(cs); |
| 1836 | case FILE_SPREAD_SLAB: |
| 1837 | return is_spread_slab(cs); |
| 1838 | case FILE_FAMILY_BOOST: |
| 1839 | return is_family_boost_enabled(cs); |
| 1840 | default: |
| 1841 | BUG(); |
| 1842 | } |
| 1843 | |
| 1844 | /* Unreachable but makes gcc happy */ |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | static s64 cpuset_read_s64(struct cgroup_subsys_state *css, struct cftype *cft) |
| 1849 | { |
| 1850 | struct cpuset *cs = css_cs(css); |
| 1851 | cpuset_filetype_t type = cft->private; |
| 1852 | switch (type) { |
| 1853 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: |
| 1854 | return cs->relax_domain_level; |
| 1855 | default: |
| 1856 | BUG(); |
| 1857 | } |
| 1858 | |
| 1859 | /* Unrechable but makes gcc happy */ |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
| 1863 | |
| 1864 | /* |
| 1865 | * for the common functions, 'private' gives the type of file |
| 1866 | */ |
| 1867 | |
| 1868 | static struct cftype files[] = { |
| 1869 | { |
| 1870 | .name = "cpus", |
| 1871 | .seq_show = cpuset_common_seq_show, |
| 1872 | .write = cpuset_write_resmask, |
| 1873 | .max_write_len = (100U + 6 * NR_CPUS), |
| 1874 | .private = FILE_CPULIST, |
| 1875 | }, |
| 1876 | |
| 1877 | { |
| 1878 | .name = "mems", |
| 1879 | .seq_show = cpuset_common_seq_show, |
| 1880 | .write = cpuset_write_resmask, |
| 1881 | .max_write_len = (100U + 6 * MAX_NUMNODES), |
| 1882 | .private = FILE_MEMLIST, |
| 1883 | }, |
| 1884 | |
| 1885 | { |
| 1886 | .name = "effective_cpus", |
| 1887 | .seq_show = cpuset_common_seq_show, |
| 1888 | .private = FILE_EFFECTIVE_CPULIST, |
| 1889 | }, |
| 1890 | |
| 1891 | { |
| 1892 | .name = "effective_mems", |
| 1893 | .seq_show = cpuset_common_seq_show, |
| 1894 | .private = FILE_EFFECTIVE_MEMLIST, |
| 1895 | }, |
| 1896 | |
| 1897 | { |
| 1898 | .name = "cpu_exclusive", |
| 1899 | .read_u64 = cpuset_read_u64, |
| 1900 | .write_u64 = cpuset_write_u64, |
| 1901 | .private = FILE_CPU_EXCLUSIVE, |
| 1902 | }, |
| 1903 | |
| 1904 | { |
| 1905 | .name = "mem_exclusive", |
| 1906 | .read_u64 = cpuset_read_u64, |
| 1907 | .write_u64 = cpuset_write_u64, |
| 1908 | .private = FILE_MEM_EXCLUSIVE, |
| 1909 | }, |
| 1910 | |
| 1911 | { |
| 1912 | .name = "mem_hardwall", |
| 1913 | .read_u64 = cpuset_read_u64, |
| 1914 | .write_u64 = cpuset_write_u64, |
| 1915 | .private = FILE_MEM_HARDWALL, |
| 1916 | }, |
| 1917 | |
| 1918 | { |
| 1919 | .name = "sched_load_balance", |
| 1920 | .read_u64 = cpuset_read_u64, |
| 1921 | .write_u64 = cpuset_write_u64, |
| 1922 | .private = FILE_SCHED_LOAD_BALANCE, |
| 1923 | }, |
| 1924 | |
| 1925 | { |
| 1926 | .name = "sched_relax_domain_level", |
| 1927 | .read_s64 = cpuset_read_s64, |
| 1928 | .write_s64 = cpuset_write_s64, |
| 1929 | .private = FILE_SCHED_RELAX_DOMAIN_LEVEL, |
| 1930 | }, |
| 1931 | |
| 1932 | { |
| 1933 | .name = "memory_migrate", |
| 1934 | .read_u64 = cpuset_read_u64, |
| 1935 | .write_u64 = cpuset_write_u64, |
| 1936 | .private = FILE_MEMORY_MIGRATE, |
| 1937 | }, |
| 1938 | |
| 1939 | { |
| 1940 | .name = "memory_pressure", |
| 1941 | .read_u64 = cpuset_read_u64, |
| 1942 | .private = FILE_MEMORY_PRESSURE, |
| 1943 | }, |
| 1944 | |
| 1945 | { |
| 1946 | .name = "memory_spread_page", |
| 1947 | .read_u64 = cpuset_read_u64, |
| 1948 | .write_u64 = cpuset_write_u64, |
| 1949 | .private = FILE_SPREAD_PAGE, |
| 1950 | }, |
| 1951 | |
| 1952 | { |
| 1953 | .name = "memory_spread_slab", |
| 1954 | .read_u64 = cpuset_read_u64, |
| 1955 | .write_u64 = cpuset_write_u64, |
| 1956 | .private = FILE_SPREAD_SLAB, |
| 1957 | }, |
| 1958 | |
| 1959 | { |
| 1960 | .name = "memory_pressure_enabled", |
| 1961 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 1962 | .read_u64 = cpuset_read_u64, |
| 1963 | .write_u64 = cpuset_write_u64, |
| 1964 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
| 1965 | }, |
| 1966 | |
| 1967 | { |
| 1968 | .name = "family_boost", |
| 1969 | .read_u64 = cpuset_read_u64, |
| 1970 | .write_u64 = cpuset_write_u64, |
| 1971 | .private = FILE_FAMILY_BOOST, |
| 1972 | }, |
| 1973 | |
| 1974 | |
| 1975 | { } /* terminate */ |
| 1976 | }; |
| 1977 | |
| 1978 | /* |
| 1979 | * cpuset_css_alloc - allocate a cpuset css |
| 1980 | * cgrp: control group that the new cpuset will be part of |
| 1981 | */ |
| 1982 | |
| 1983 | static struct cgroup_subsys_state * |
| 1984 | cpuset_css_alloc(struct cgroup_subsys_state *parent_css) |
| 1985 | { |
| 1986 | struct cpuset *cs; |
| 1987 | |
| 1988 | if (!parent_css) |
| 1989 | return &top_cpuset.css; |
| 1990 | |
| 1991 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
| 1992 | if (!cs) |
| 1993 | return ERR_PTR(-ENOMEM); |
| 1994 | if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) |
| 1995 | goto free_cs; |
| 1996 | if (!alloc_cpumask_var(&cs->cpus_requested, GFP_KERNEL)) |
| 1997 | goto free_allowed; |
| 1998 | if (!alloc_cpumask_var(&cs->effective_cpus, GFP_KERNEL)) |
| 1999 | goto free_requested; |
| 2000 | |
| 2001 | set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
| 2002 | cpumask_clear(cs->cpus_allowed); |
| 2003 | cpumask_clear(cs->cpus_requested); |
| 2004 | nodes_clear(cs->mems_allowed); |
| 2005 | cpumask_clear(cs->effective_cpus); |
| 2006 | nodes_clear(cs->effective_mems); |
| 2007 | fmeter_init(&cs->fmeter); |
| 2008 | cs->relax_domain_level = -1; |
| 2009 | |
| 2010 | return &cs->css; |
| 2011 | |
| 2012 | free_requested: |
| 2013 | free_cpumask_var(cs->cpus_requested); |
| 2014 | free_allowed: |
| 2015 | free_cpumask_var(cs->cpus_allowed); |
| 2016 | free_cs: |
| 2017 | kfree(cs); |
| 2018 | return ERR_PTR(-ENOMEM); |
| 2019 | } |
| 2020 | |
| 2021 | static int cpuset_css_online(struct cgroup_subsys_state *css) |
| 2022 | { |
| 2023 | struct cpuset *cs = css_cs(css); |
| 2024 | struct cpuset *parent = parent_cs(cs); |
| 2025 | struct cpuset *tmp_cs; |
| 2026 | struct cgroup_subsys_state *pos_css; |
| 2027 | |
| 2028 | if (!parent) |
| 2029 | return 0; |
| 2030 | |
| 2031 | mutex_lock(&cpuset_mutex); |
| 2032 | |
| 2033 | set_bit(CS_ONLINE, &cs->flags); |
| 2034 | if (is_spread_page(parent)) |
| 2035 | set_bit(CS_SPREAD_PAGE, &cs->flags); |
| 2036 | if (is_spread_slab(parent)) |
| 2037 | set_bit(CS_SPREAD_SLAB, &cs->flags); |
| 2038 | |
| 2039 | cpuset_inc(); |
| 2040 | |
| 2041 | spin_lock_irq(&callback_lock); |
| 2042 | if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) { |
| 2043 | cpumask_copy(cs->effective_cpus, parent->effective_cpus); |
| 2044 | cs->effective_mems = parent->effective_mems; |
| 2045 | } |
| 2046 | spin_unlock_irq(&callback_lock); |
| 2047 | |
| 2048 | if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags)) |
| 2049 | goto out_unlock; |
| 2050 | |
| 2051 | /* |
| 2052 | * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is |
| 2053 | * set. This flag handling is implemented in cgroup core for |
| 2054 | * histrical reasons - the flag may be specified during mount. |
| 2055 | * |
| 2056 | * Currently, if any sibling cpusets have exclusive cpus or mem, we |
| 2057 | * refuse to clone the configuration - thereby refusing the task to |
| 2058 | * be entered, and as a result refusing the sys_unshare() or |
| 2059 | * clone() which initiated it. If this becomes a problem for some |
| 2060 | * users who wish to allow that scenario, then this could be |
| 2061 | * changed to grant parent->cpus_allowed-sibling_cpus_exclusive |
| 2062 | * (and likewise for mems) to the new cgroup. |
| 2063 | */ |
| 2064 | rcu_read_lock(); |
| 2065 | cpuset_for_each_child(tmp_cs, pos_css, parent) { |
| 2066 | if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) { |
| 2067 | rcu_read_unlock(); |
| 2068 | goto out_unlock; |
| 2069 | } |
| 2070 | } |
| 2071 | rcu_read_unlock(); |
| 2072 | |
| 2073 | spin_lock_irq(&callback_lock); |
| 2074 | cs->mems_allowed = parent->mems_allowed; |
| 2075 | cs->effective_mems = parent->mems_allowed; |
| 2076 | cpumask_copy(cs->cpus_allowed, parent->cpus_allowed); |
| 2077 | cpumask_copy(cs->cpus_requested, parent->cpus_requested); |
| 2078 | cpumask_copy(cs->effective_cpus, parent->cpus_allowed); |
| 2079 | spin_unlock_irq(&callback_lock); |
| 2080 | out_unlock: |
| 2081 | mutex_unlock(&cpuset_mutex); |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | /* |
| 2086 | * If the cpuset being removed has its flag 'sched_load_balance' |
| 2087 | * enabled, then simulate turning sched_load_balance off, which |
| 2088 | * will call rebuild_sched_domains_locked(). |
| 2089 | */ |
| 2090 | |
| 2091 | static void cpuset_css_offline(struct cgroup_subsys_state *css) |
| 2092 | { |
| 2093 | struct cpuset *cs = css_cs(css); |
| 2094 | |
| 2095 | mutex_lock(&cpuset_mutex); |
| 2096 | |
| 2097 | if (is_sched_load_balance(cs)) |
| 2098 | update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); |
| 2099 | |
| 2100 | cpuset_dec(); |
| 2101 | clear_bit(CS_ONLINE, &cs->flags); |
| 2102 | |
| 2103 | mutex_unlock(&cpuset_mutex); |
| 2104 | } |
| 2105 | |
| 2106 | static void cpuset_css_free(struct cgroup_subsys_state *css) |
| 2107 | { |
| 2108 | struct cpuset *cs = css_cs(css); |
| 2109 | |
| 2110 | free_cpumask_var(cs->effective_cpus); |
| 2111 | free_cpumask_var(cs->cpus_allowed); |
| 2112 | free_cpumask_var(cs->cpus_requested); |
| 2113 | kfree(cs); |
| 2114 | } |
| 2115 | |
| 2116 | static void cpuset_bind(struct cgroup_subsys_state *root_css) |
| 2117 | { |
| 2118 | mutex_lock(&cpuset_mutex); |
| 2119 | spin_lock_irq(&callback_lock); |
| 2120 | |
| 2121 | if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) { |
| 2122 | cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask); |
| 2123 | top_cpuset.mems_allowed = node_possible_map; |
| 2124 | } else { |
| 2125 | cpumask_copy(top_cpuset.cpus_allowed, |
| 2126 | top_cpuset.effective_cpus); |
| 2127 | top_cpuset.mems_allowed = top_cpuset.effective_mems; |
| 2128 | } |
| 2129 | |
| 2130 | spin_unlock_irq(&callback_lock); |
| 2131 | mutex_unlock(&cpuset_mutex); |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | * Make sure the new task conform to the current state of its parent, |
| 2136 | * which could have been changed by cpuset just after it inherits the |
| 2137 | * state from the parent and before it sits on the cgroup's task list. |
| 2138 | */ |
| 2139 | void cpuset_fork(struct task_struct *task, void *priv) |
| 2140 | { |
| 2141 | if (task_css_is_root(task, cpuset_cgrp_id)) |
| 2142 | return; |
| 2143 | |
| 2144 | set_cpus_allowed_ptr(task, ¤t->cpus_allowed); |
| 2145 | task->mems_allowed = current->mems_allowed; |
| 2146 | } |
| 2147 | |
| 2148 | static int cpuset_allow_attach(struct cgroup_taskset *tset) |
| 2149 | { |
| 2150 | const struct cred *cred = current_cred(), *tcred; |
| 2151 | struct task_struct *task; |
| 2152 | struct cgroup_subsys_state *css; |
| 2153 | |
| 2154 | cgroup_taskset_for_each(task, css, tset) { |
| 2155 | tcred = __task_cred(task); |
| 2156 | |
| 2157 | if ((current != task) && !capable(CAP_SYS_ADMIN) && |
| 2158 | cred->euid.val != tcred->uid.val && cred->euid.val != tcred->suid.val) |
| 2159 | return -EACCES; |
| 2160 | } |
| 2161 | |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
| 2165 | struct cgroup_subsys cpuset_cgrp_subsys = { |
| 2166 | .css_alloc = cpuset_css_alloc, |
| 2167 | .css_online = cpuset_css_online, |
| 2168 | .css_offline = cpuset_css_offline, |
| 2169 | .css_free = cpuset_css_free, |
| 2170 | .can_attach = cpuset_can_attach, |
| 2171 | .allow_attach = cpuset_allow_attach, |
| 2172 | .cancel_attach = cpuset_cancel_attach, |
| 2173 | .attach = cpuset_attach, |
| 2174 | .post_attach = cpuset_post_attach, |
| 2175 | .bind = cpuset_bind, |
| 2176 | .fork = cpuset_fork, |
| 2177 | .legacy_cftypes = files, |
| 2178 | .early_init = 1, |
| 2179 | }; |
| 2180 | |
| 2181 | /** |
| 2182 | * cpuset_init - initialize cpusets at system boot |
| 2183 | * |
| 2184 | * Description: Initialize top_cpuset and the cpuset internal file system, |
| 2185 | **/ |
| 2186 | |
| 2187 | int __init cpuset_init(void) |
| 2188 | { |
| 2189 | int err = 0; |
| 2190 | |
| 2191 | if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)) |
| 2192 | BUG(); |
| 2193 | if (!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL)) |
| 2194 | BUG(); |
| 2195 | if (!alloc_cpumask_var(&top_cpuset.cpus_requested, GFP_KERNEL)) |
| 2196 | BUG(); |
| 2197 | |
| 2198 | cpumask_setall(top_cpuset.cpus_allowed); |
| 2199 | cpumask_setall(top_cpuset.cpus_requested); |
| 2200 | nodes_setall(top_cpuset.mems_allowed); |
| 2201 | cpumask_setall(top_cpuset.effective_cpus); |
| 2202 | nodes_setall(top_cpuset.effective_mems); |
| 2203 | |
| 2204 | fmeter_init(&top_cpuset.fmeter); |
| 2205 | set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); |
| 2206 | top_cpuset.relax_domain_level = -1; |
| 2207 | |
| 2208 | err = register_filesystem(&cpuset_fs_type); |
| 2209 | if (err < 0) |
| 2210 | return err; |
| 2211 | |
| 2212 | if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)) |
| 2213 | BUG(); |
| 2214 | |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * If CPU and/or memory hotplug handlers, below, unplug any CPUs |
| 2220 | * or memory nodes, we need to walk over the cpuset hierarchy, |
| 2221 | * removing that CPU or node from all cpusets. If this removes the |
| 2222 | * last CPU or node from a cpuset, then move the tasks in the empty |
| 2223 | * cpuset to its next-highest non-empty parent. |
| 2224 | */ |
| 2225 | static void remove_tasks_in_empty_cpuset(struct cpuset *cs) |
| 2226 | { |
| 2227 | struct cpuset *parent; |
| 2228 | |
| 2229 | /* |
| 2230 | * Find its next-highest non-empty parent, (top cpuset |
| 2231 | * has online cpus, so can't be empty). |
| 2232 | */ |
| 2233 | parent = parent_cs(cs); |
| 2234 | while (cpumask_empty(parent->cpus_allowed) || |
| 2235 | nodes_empty(parent->mems_allowed)) |
| 2236 | parent = parent_cs(parent); |
| 2237 | |
| 2238 | if (cgroup_transfer_tasks(parent->css.cgroup, cs->css.cgroup)) { |
| 2239 | pr_err("cpuset: failed to transfer tasks out of empty cpuset "); |
| 2240 | pr_cont_cgroup_name(cs->css.cgroup); |
| 2241 | pr_cont("\n"); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | static void |
| 2246 | hotplug_update_tasks_legacy(struct cpuset *cs, |
| 2247 | struct cpumask *new_cpus, nodemask_t *new_mems, |
| 2248 | bool cpus_updated, bool mems_updated) |
| 2249 | { |
| 2250 | bool is_empty; |
| 2251 | |
| 2252 | spin_lock_irq(&callback_lock); |
| 2253 | cpumask_copy(cs->cpus_allowed, new_cpus); |
| 2254 | cpumask_copy(cs->effective_cpus, new_cpus); |
| 2255 | cs->mems_allowed = *new_mems; |
| 2256 | cs->effective_mems = *new_mems; |
| 2257 | spin_unlock_irq(&callback_lock); |
| 2258 | |
| 2259 | /* |
| 2260 | * Don't call update_tasks_cpumask() if the cpuset becomes empty, |
| 2261 | * as the tasks will be migratecd to an ancestor. |
| 2262 | */ |
| 2263 | if (cpus_updated && !cpumask_empty(cs->cpus_allowed)) |
| 2264 | update_tasks_cpumask(cs); |
| 2265 | if (mems_updated && !nodes_empty(cs->mems_allowed)) |
| 2266 | update_tasks_nodemask(cs); |
| 2267 | |
| 2268 | is_empty = cpumask_empty(cs->cpus_allowed) || |
| 2269 | nodes_empty(cs->mems_allowed); |
| 2270 | |
| 2271 | mutex_unlock(&cpuset_mutex); |
| 2272 | |
| 2273 | /* |
| 2274 | * Move tasks to the nearest ancestor with execution resources, |
| 2275 | * This is full cgroup operation which will also call back into |
| 2276 | * cpuset. Should be done outside any lock. |
| 2277 | */ |
| 2278 | if (is_empty) |
| 2279 | remove_tasks_in_empty_cpuset(cs); |
| 2280 | |
| 2281 | mutex_lock(&cpuset_mutex); |
| 2282 | } |
| 2283 | |
| 2284 | static void |
| 2285 | hotplug_update_tasks(struct cpuset *cs, |
| 2286 | struct cpumask *new_cpus, nodemask_t *new_mems, |
| 2287 | bool cpus_updated, bool mems_updated) |
| 2288 | { |
| 2289 | if (cpumask_empty(new_cpus)) |
| 2290 | cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus); |
| 2291 | if (nodes_empty(*new_mems)) |
| 2292 | *new_mems = parent_cs(cs)->effective_mems; |
| 2293 | |
| 2294 | spin_lock_irq(&callback_lock); |
| 2295 | cpumask_copy(cs->effective_cpus, new_cpus); |
| 2296 | cs->effective_mems = *new_mems; |
| 2297 | spin_unlock_irq(&callback_lock); |
| 2298 | |
| 2299 | if (cpus_updated) |
| 2300 | update_tasks_cpumask(cs); |
| 2301 | if (mems_updated) |
| 2302 | update_tasks_nodemask(cs); |
| 2303 | } |
| 2304 | |
| 2305 | /** |
| 2306 | * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug |
| 2307 | * @cs: cpuset in interest |
| 2308 | * |
| 2309 | * Compare @cs's cpu and mem masks against top_cpuset and if some have gone |
| 2310 | * offline, update @cs accordingly. If @cs ends up with no CPU or memory, |
| 2311 | * all its tasks are moved to the nearest ancestor with both resources. |
| 2312 | */ |
| 2313 | static void cpuset_hotplug_update_tasks(struct cpuset *cs) |
| 2314 | { |
| 2315 | static cpumask_t new_cpus; |
| 2316 | static nodemask_t new_mems; |
| 2317 | bool cpus_updated; |
| 2318 | bool mems_updated; |
| 2319 | retry: |
| 2320 | wait_event(cpuset_attach_wq, cs->attach_in_progress == 0); |
| 2321 | |
| 2322 | mutex_lock(&cpuset_mutex); |
| 2323 | |
| 2324 | /* |
| 2325 | * We have raced with task attaching. We wait until attaching |
| 2326 | * is finished, so we won't attach a task to an empty cpuset. |
| 2327 | */ |
| 2328 | if (cs->attach_in_progress) { |
| 2329 | mutex_unlock(&cpuset_mutex); |
| 2330 | goto retry; |
| 2331 | } |
| 2332 | |
| 2333 | cpumask_and(&new_cpus, cs->cpus_requested, parent_cs(cs)->effective_cpus); |
| 2334 | nodes_and(new_mems, cs->mems_allowed, parent_cs(cs)->effective_mems); |
| 2335 | |
| 2336 | cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus); |
| 2337 | mems_updated = !nodes_equal(new_mems, cs->effective_mems); |
| 2338 | |
| 2339 | if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) |
| 2340 | hotplug_update_tasks(cs, &new_cpus, &new_mems, |
| 2341 | cpus_updated, mems_updated); |
| 2342 | else |
| 2343 | hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems, |
| 2344 | cpus_updated, mems_updated); |
| 2345 | |
| 2346 | mutex_unlock(&cpuset_mutex); |
| 2347 | } |
| 2348 | |
| 2349 | static bool force_rebuild; |
| 2350 | |
| 2351 | void cpuset_force_rebuild(void) |
| 2352 | { |
| 2353 | force_rebuild = true; |
| 2354 | } |
| 2355 | |
| 2356 | /** |
| 2357 | * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset |
| 2358 | * |
| 2359 | * This function is called after either CPU or memory configuration has |
| 2360 | * changed and updates cpuset accordingly. The top_cpuset is always |
| 2361 | * synchronized to cpu_active_mask and N_MEMORY, which is necessary in |
| 2362 | * order to make cpusets transparent (of no affect) on systems that are |
| 2363 | * actively using CPU hotplug but making no active use of cpusets. |
| 2364 | * |
| 2365 | * Non-root cpusets are only affected by offlining. If any CPUs or memory |
| 2366 | * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on |
| 2367 | * all descendants. |
| 2368 | * |
| 2369 | * Note that CPU offlining during suspend is ignored. We don't modify |
| 2370 | * cpusets across suspend/resume cycles at all. |
| 2371 | */ |
| 2372 | static void cpuset_hotplug_workfn(struct work_struct *work) |
| 2373 | { |
| 2374 | static cpumask_t new_cpus; |
| 2375 | static nodemask_t new_mems; |
| 2376 | bool cpus_updated, mems_updated; |
| 2377 | bool on_dfl = cgroup_subsys_on_dfl(cpuset_cgrp_subsys); |
| 2378 | |
| 2379 | mutex_lock(&cpuset_mutex); |
| 2380 | |
| 2381 | /* fetch the available cpus/mems and find out which changed how */ |
| 2382 | cpumask_copy(&new_cpus, cpu_active_mask); |
| 2383 | new_mems = node_states[N_MEMORY]; |
| 2384 | |
| 2385 | cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus); |
| 2386 | mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems); |
| 2387 | |
| 2388 | /* synchronize cpus_allowed to cpu_active_mask */ |
| 2389 | if (cpus_updated) { |
| 2390 | spin_lock_irq(&callback_lock); |
| 2391 | if (!on_dfl) |
| 2392 | cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); |
| 2393 | cpumask_copy(top_cpuset.effective_cpus, &new_cpus); |
| 2394 | spin_unlock_irq(&callback_lock); |
| 2395 | /* we don't mess with cpumasks of tasks in top_cpuset */ |
| 2396 | } |
| 2397 | |
| 2398 | /* synchronize mems_allowed to N_MEMORY */ |
| 2399 | if (mems_updated) { |
| 2400 | spin_lock_irq(&callback_lock); |
| 2401 | if (!on_dfl) |
| 2402 | top_cpuset.mems_allowed = new_mems; |
| 2403 | top_cpuset.effective_mems = new_mems; |
| 2404 | spin_unlock_irq(&callback_lock); |
| 2405 | update_tasks_nodemask(&top_cpuset); |
| 2406 | } |
| 2407 | |
| 2408 | mutex_unlock(&cpuset_mutex); |
| 2409 | |
| 2410 | /* if cpus or mems changed, we need to propagate to descendants */ |
| 2411 | if (cpus_updated || mems_updated) { |
| 2412 | struct cpuset *cs; |
| 2413 | struct cgroup_subsys_state *pos_css; |
| 2414 | |
| 2415 | rcu_read_lock(); |
| 2416 | cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) { |
| 2417 | if (cs == &top_cpuset || !css_tryget_online(&cs->css)) |
| 2418 | continue; |
| 2419 | rcu_read_unlock(); |
| 2420 | |
| 2421 | cpuset_hotplug_update_tasks(cs); |
| 2422 | |
| 2423 | rcu_read_lock(); |
| 2424 | css_put(&cs->css); |
| 2425 | } |
| 2426 | rcu_read_unlock(); |
| 2427 | } |
| 2428 | |
| 2429 | /* rebuild sched domains if cpus_allowed has changed */ |
| 2430 | if (cpus_updated || force_rebuild) { |
| 2431 | force_rebuild = false; |
| 2432 | rebuild_sched_domains(); |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | void cpuset_update_active_cpus(bool cpu_online) |
| 2437 | { |
| 2438 | /* |
| 2439 | * We're inside cpu hotplug critical region which usually nests |
| 2440 | * inside cgroup synchronization. Bounce actual hotplug processing |
| 2441 | * to a work item to avoid reverse locking order. |
| 2442 | * |
| 2443 | * We still need to do partition_sched_domains() synchronously; |
| 2444 | * otherwise, the scheduler will get confused and put tasks to the |
| 2445 | * dead CPU. Fall back to the default single domain. |
| 2446 | * cpuset_hotplug_workfn() will rebuild it as necessary. |
| 2447 | */ |
| 2448 | partition_sched_domains(1, NULL, NULL); |
| 2449 | schedule_work(&cpuset_hotplug_work); |
| 2450 | } |
| 2451 | |
| 2452 | void cpuset_wait_for_hotplug(void) |
| 2453 | { |
| 2454 | flush_work(&cpuset_hotplug_work); |
| 2455 | } |
| 2456 | |
| 2457 | /* |
| 2458 | * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. |
| 2459 | * Call this routine anytime after node_states[N_MEMORY] changes. |
| 2460 | * See cpuset_update_active_cpus() for CPU hotplug handling. |
| 2461 | */ |
| 2462 | static int cpuset_track_online_nodes(struct notifier_block *self, |
| 2463 | unsigned long action, void *arg) |
| 2464 | { |
| 2465 | schedule_work(&cpuset_hotplug_work); |
| 2466 | return NOTIFY_OK; |
| 2467 | } |
| 2468 | |
| 2469 | static struct notifier_block cpuset_track_online_nodes_nb = { |
| 2470 | .notifier_call = cpuset_track_online_nodes, |
| 2471 | .priority = 10, /* ??! */ |
| 2472 | }; |
| 2473 | |
| 2474 | /** |
| 2475 | * cpuset_init_smp - initialize cpus_allowed |
| 2476 | * |
| 2477 | * Description: Finish top cpuset after cpu, node maps are initialized |
| 2478 | */ |
| 2479 | void __init cpuset_init_smp(void) |
| 2480 | { |
| 2481 | cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask); |
| 2482 | top_cpuset.mems_allowed = node_states[N_MEMORY]; |
| 2483 | top_cpuset.old_mems_allowed = top_cpuset.mems_allowed; |
| 2484 | |
| 2485 | cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask); |
| 2486 | top_cpuset.effective_mems = node_states[N_MEMORY]; |
| 2487 | |
| 2488 | register_hotmemory_notifier(&cpuset_track_online_nodes_nb); |
| 2489 | |
| 2490 | cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0); |
| 2491 | BUG_ON(!cpuset_migrate_mm_wq); |
| 2492 | } |
| 2493 | |
| 2494 | /** |
| 2495 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
| 2496 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. |
| 2497 | * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. |
| 2498 | * |
| 2499 | * Description: Returns the cpumask_var_t cpus_allowed of the cpuset |
| 2500 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 2501 | * subset of cpu_online_mask, even if this means going outside the |
| 2502 | * tasks cpuset. |
| 2503 | **/ |
| 2504 | |
| 2505 | void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) |
| 2506 | { |
| 2507 | unsigned long flags; |
| 2508 | |
| 2509 | spin_lock_irqsave(&callback_lock, flags); |
| 2510 | rcu_read_lock(); |
| 2511 | guarantee_online_cpus(task_cs(tsk), pmask); |
| 2512 | rcu_read_unlock(); |
| 2513 | spin_unlock_irqrestore(&callback_lock, flags); |
| 2514 | } |
| 2515 | |
| 2516 | void cpuset_cpus_allowed_fallback(struct task_struct *tsk) |
| 2517 | { |
| 2518 | rcu_read_lock(); |
| 2519 | do_set_cpus_allowed(tsk, task_cs(tsk)->effective_cpus); |
| 2520 | rcu_read_unlock(); |
| 2521 | |
| 2522 | /* |
| 2523 | * We own tsk->cpus_allowed, nobody can change it under us. |
| 2524 | * |
| 2525 | * But we used cs && cs->cpus_allowed lockless and thus can |
| 2526 | * race with cgroup_attach_task() or update_cpumask() and get |
| 2527 | * the wrong tsk->cpus_allowed. However, both cases imply the |
| 2528 | * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() |
| 2529 | * which takes task_rq_lock(). |
| 2530 | * |
| 2531 | * If we are called after it dropped the lock we must see all |
| 2532 | * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary |
| 2533 | * set any mask even if it is not right from task_cs() pov, |
| 2534 | * the pending set_cpus_allowed_ptr() will fix things. |
| 2535 | * |
| 2536 | * select_fallback_rq() will fix things ups and set cpu_possible_mask |
| 2537 | * if required. |
| 2538 | */ |
| 2539 | } |
| 2540 | |
| 2541 | void __init cpuset_init_current_mems_allowed(void) |
| 2542 | { |
| 2543 | nodes_setall(current->mems_allowed); |
| 2544 | } |
| 2545 | |
| 2546 | /** |
| 2547 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. |
| 2548 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. |
| 2549 | * |
| 2550 | * Description: Returns the nodemask_t mems_allowed of the cpuset |
| 2551 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 2552 | * subset of node_states[N_MEMORY], even if this means going outside the |
| 2553 | * tasks cpuset. |
| 2554 | **/ |
| 2555 | |
| 2556 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) |
| 2557 | { |
| 2558 | nodemask_t mask; |
| 2559 | unsigned long flags; |
| 2560 | |
| 2561 | spin_lock_irqsave(&callback_lock, flags); |
| 2562 | rcu_read_lock(); |
| 2563 | guarantee_online_mems(task_cs(tsk), &mask); |
| 2564 | rcu_read_unlock(); |
| 2565 | spin_unlock_irqrestore(&callback_lock, flags); |
| 2566 | |
| 2567 | return mask; |
| 2568 | } |
| 2569 | |
| 2570 | /** |
| 2571 | * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed |
| 2572 | * @nodemask: the nodemask to be checked |
| 2573 | * |
| 2574 | * Are any of the nodes in the nodemask allowed in current->mems_allowed? |
| 2575 | */ |
| 2576 | int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask) |
| 2577 | { |
| 2578 | return nodes_intersects(*nodemask, current->mems_allowed); |
| 2579 | } |
| 2580 | |
| 2581 | /* |
| 2582 | * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or |
| 2583 | * mem_hardwall ancestor to the specified cpuset. Call holding |
| 2584 | * callback_lock. If no ancestor is mem_exclusive or mem_hardwall |
| 2585 | * (an unusual configuration), then returns the root cpuset. |
| 2586 | */ |
| 2587 | static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs) |
| 2588 | { |
| 2589 | while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs)) |
| 2590 | cs = parent_cs(cs); |
| 2591 | return cs; |
| 2592 | } |
| 2593 | |
| 2594 | /** |
| 2595 | * cpuset_node_allowed - Can we allocate on a memory node? |
| 2596 | * @node: is this an allowed node? |
| 2597 | * @gfp_mask: memory allocation flags |
| 2598 | * |
| 2599 | * If we're in interrupt, yes, we can always allocate. If @node is set in |
| 2600 | * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this |
| 2601 | * node is set in the nearest hardwalled cpuset ancestor to current's cpuset, |
| 2602 | * yes. If current has access to memory reserves due to TIF_MEMDIE, yes. |
| 2603 | * Otherwise, no. |
| 2604 | * |
| 2605 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
| 2606 | * and do not allow allocations outside the current tasks cpuset |
| 2607 | * unless the task has been OOM killed as is marked TIF_MEMDIE. |
| 2608 | * GFP_KERNEL allocations are not so marked, so can escape to the |
| 2609 | * nearest enclosing hardwalled ancestor cpuset. |
| 2610 | * |
| 2611 | * Scanning up parent cpusets requires callback_lock. The |
| 2612 | * __alloc_pages() routine only calls here with __GFP_HARDWALL bit |
| 2613 | * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the |
| 2614 | * current tasks mems_allowed came up empty on the first pass over |
| 2615 | * the zonelist. So only GFP_KERNEL allocations, if all nodes in the |
| 2616 | * cpuset are short of memory, might require taking the callback_lock. |
| 2617 | * |
| 2618 | * The first call here from mm/page_alloc:get_page_from_freelist() |
| 2619 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, |
| 2620 | * so no allocation on a node outside the cpuset is allowed (unless |
| 2621 | * in interrupt, of course). |
| 2622 | * |
| 2623 | * The second pass through get_page_from_freelist() doesn't even call |
| 2624 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() |
| 2625 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set |
| 2626 | * in alloc_flags. That logic and the checks below have the combined |
| 2627 | * affect that: |
| 2628 | * in_interrupt - any node ok (current task context irrelevant) |
| 2629 | * GFP_ATOMIC - any node ok |
| 2630 | * TIF_MEMDIE - any node ok |
| 2631 | * GFP_KERNEL - any node in enclosing hardwalled cpuset ok |
| 2632 | * GFP_USER - only nodes in current tasks mems allowed ok. |
| 2633 | */ |
| 2634 | int __cpuset_node_allowed(int node, gfp_t gfp_mask) |
| 2635 | { |
| 2636 | struct cpuset *cs; /* current cpuset ancestors */ |
| 2637 | int allowed; /* is allocation in zone z allowed? */ |
| 2638 | unsigned long flags; |
| 2639 | |
| 2640 | if (in_interrupt()) |
| 2641 | return 1; |
| 2642 | if (node_isset(node, current->mems_allowed)) |
| 2643 | return 1; |
| 2644 | /* |
| 2645 | * Allow tasks that have access to memory reserves because they have |
| 2646 | * been OOM killed to get memory anywhere. |
| 2647 | */ |
| 2648 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 2649 | return 1; |
| 2650 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
| 2651 | return 0; |
| 2652 | |
| 2653 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 2654 | return 1; |
| 2655 | |
| 2656 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
| 2657 | spin_lock_irqsave(&callback_lock, flags); |
| 2658 | |
| 2659 | rcu_read_lock(); |
| 2660 | cs = nearest_hardwall_ancestor(task_cs(current)); |
| 2661 | allowed = node_isset(node, cs->mems_allowed); |
| 2662 | rcu_read_unlock(); |
| 2663 | |
| 2664 | spin_unlock_irqrestore(&callback_lock, flags); |
| 2665 | return allowed; |
| 2666 | } |
| 2667 | |
| 2668 | /** |
| 2669 | * cpuset_mem_spread_node() - On which node to begin search for a file page |
| 2670 | * cpuset_slab_spread_node() - On which node to begin search for a slab page |
| 2671 | * |
| 2672 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for |
| 2673 | * tasks in a cpuset with is_spread_page or is_spread_slab set), |
| 2674 | * and if the memory allocation used cpuset_mem_spread_node() |
| 2675 | * to determine on which node to start looking, as it will for |
| 2676 | * certain page cache or slab cache pages such as used for file |
| 2677 | * system buffers and inode caches, then instead of starting on the |
| 2678 | * local node to look for a free page, rather spread the starting |
| 2679 | * node around the tasks mems_allowed nodes. |
| 2680 | * |
| 2681 | * We don't have to worry about the returned node being offline |
| 2682 | * because "it can't happen", and even if it did, it would be ok. |
| 2683 | * |
| 2684 | * The routines calling guarantee_online_mems() are careful to |
| 2685 | * only set nodes in task->mems_allowed that are online. So it |
| 2686 | * should not be possible for the following code to return an |
| 2687 | * offline node. But if it did, that would be ok, as this routine |
| 2688 | * is not returning the node where the allocation must be, only |
| 2689 | * the node where the search should start. The zonelist passed to |
| 2690 | * __alloc_pages() will include all nodes. If the slab allocator |
| 2691 | * is passed an offline node, it will fall back to the local node. |
| 2692 | * See kmem_cache_alloc_node(). |
| 2693 | */ |
| 2694 | |
| 2695 | static int cpuset_spread_node(int *rotor) |
| 2696 | { |
| 2697 | int node; |
| 2698 | |
| 2699 | node = next_node(*rotor, current->mems_allowed); |
| 2700 | if (node == MAX_NUMNODES) |
| 2701 | node = first_node(current->mems_allowed); |
| 2702 | *rotor = node; |
| 2703 | return node; |
| 2704 | } |
| 2705 | |
| 2706 | int cpuset_mem_spread_node(void) |
| 2707 | { |
| 2708 | if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) |
| 2709 | current->cpuset_mem_spread_rotor = |
| 2710 | node_random(¤t->mems_allowed); |
| 2711 | |
| 2712 | return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); |
| 2713 | } |
| 2714 | |
| 2715 | int cpuset_slab_spread_node(void) |
| 2716 | { |
| 2717 | if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE) |
| 2718 | current->cpuset_slab_spread_rotor = |
| 2719 | node_random(¤t->mems_allowed); |
| 2720 | |
| 2721 | return cpuset_spread_node(¤t->cpuset_slab_spread_rotor); |
| 2722 | } |
| 2723 | |
| 2724 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); |
| 2725 | |
| 2726 | /** |
| 2727 | * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? |
| 2728 | * @tsk1: pointer to task_struct of some task. |
| 2729 | * @tsk2: pointer to task_struct of some other task. |
| 2730 | * |
| 2731 | * Description: Return true if @tsk1's mems_allowed intersects the |
| 2732 | * mems_allowed of @tsk2. Used by the OOM killer to determine if |
| 2733 | * one of the task's memory usage might impact the memory available |
| 2734 | * to the other. |
| 2735 | **/ |
| 2736 | |
| 2737 | int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, |
| 2738 | const struct task_struct *tsk2) |
| 2739 | { |
| 2740 | return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); |
| 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed |
| 2745 | * |
| 2746 | * Description: Prints current's name, cpuset name, and cached copy of its |
| 2747 | * mems_allowed to the kernel log. |
| 2748 | */ |
| 2749 | void cpuset_print_current_mems_allowed(void) |
| 2750 | { |
| 2751 | struct cgroup *cgrp; |
| 2752 | |
| 2753 | rcu_read_lock(); |
| 2754 | |
| 2755 | cgrp = task_cs(current)->css.cgroup; |
| 2756 | pr_info("%s cpuset=", current->comm); |
| 2757 | pr_cont_cgroup_name(cgrp); |
| 2758 | pr_cont(" mems_allowed=%*pbl\n", |
| 2759 | nodemask_pr_args(¤t->mems_allowed)); |
| 2760 | |
| 2761 | rcu_read_unlock(); |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * Collection of memory_pressure is suppressed unless |
| 2766 | * this flag is enabled by writing "1" to the special |
| 2767 | * cpuset file 'memory_pressure_enabled' in the root cpuset. |
| 2768 | */ |
| 2769 | |
| 2770 | int cpuset_memory_pressure_enabled __read_mostly; |
| 2771 | |
| 2772 | /** |
| 2773 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. |
| 2774 | * |
| 2775 | * Keep a running average of the rate of synchronous (direct) |
| 2776 | * page reclaim efforts initiated by tasks in each cpuset. |
| 2777 | * |
| 2778 | * This represents the rate at which some task in the cpuset |
| 2779 | * ran low on memory on all nodes it was allowed to use, and |
| 2780 | * had to enter the kernels page reclaim code in an effort to |
| 2781 | * create more free memory by tossing clean pages or swapping |
| 2782 | * or writing dirty pages. |
| 2783 | * |
| 2784 | * Display to user space in the per-cpuset read-only file |
| 2785 | * "memory_pressure". Value displayed is an integer |
| 2786 | * representing the recent rate of entry into the synchronous |
| 2787 | * (direct) page reclaim by any task attached to the cpuset. |
| 2788 | **/ |
| 2789 | |
| 2790 | void __cpuset_memory_pressure_bump(void) |
| 2791 | { |
| 2792 | rcu_read_lock(); |
| 2793 | fmeter_markevent(&task_cs(current)->fmeter); |
| 2794 | rcu_read_unlock(); |
| 2795 | } |
| 2796 | |
| 2797 | #ifdef CONFIG_PROC_PID_CPUSET |
| 2798 | /* |
| 2799 | * proc_cpuset_show() |
| 2800 | * - Print tasks cpuset path into seq_file. |
| 2801 | * - Used for /proc/<pid>/cpuset. |
| 2802 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
| 2803 | * doesn't really matter if tsk->cpuset changes after we read it, |
| 2804 | * and we take cpuset_mutex, keeping cpuset_attach() from changing it |
| 2805 | * anyway. |
| 2806 | */ |
| 2807 | int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns, |
| 2808 | struct pid *pid, struct task_struct *tsk) |
| 2809 | { |
| 2810 | char *buf, *p; |
| 2811 | struct cgroup_subsys_state *css; |
| 2812 | int retval; |
| 2813 | |
| 2814 | retval = -ENOMEM; |
| 2815 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 2816 | if (!buf) |
| 2817 | goto out; |
| 2818 | |
| 2819 | retval = -ENAMETOOLONG; |
| 2820 | rcu_read_lock(); |
| 2821 | css = task_css(tsk, cpuset_cgrp_id); |
| 2822 | p = cgroup_path(css->cgroup, buf, PATH_MAX); |
| 2823 | rcu_read_unlock(); |
| 2824 | if (!p) |
| 2825 | goto out_free; |
| 2826 | seq_puts(m, p); |
| 2827 | seq_putc(m, '\n'); |
| 2828 | retval = 0; |
| 2829 | out_free: |
| 2830 | kfree(buf); |
| 2831 | out: |
| 2832 | return retval; |
| 2833 | } |
| 2834 | #endif /* CONFIG_PROC_PID_CPUSET */ |
| 2835 | |
| 2836 | /* Display task mems_allowed in /proc/<pid>/status file. */ |
| 2837 | void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) |
| 2838 | { |
| 2839 | seq_printf(m, "Mems_allowed:\t%*pb\n", |
| 2840 | nodemask_pr_args(&task->mems_allowed)); |
| 2841 | seq_printf(m, "Mems_allowed_list:\t%*pbl\n", |
| 2842 | nodemask_pr_args(&task->mems_allowed)); |
| 2843 | } |