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