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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 18 | * This file is subject to the terms and conditions of the GNU General Public |
| 19 | * License. See the file COPYING in the main directory of the Linux |
| 20 | * distribution for more details. |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/cpu.h> |
| 24 | #include <linux/cpumask.h> |
| 25 | #include <linux/cpuset.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/file.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/kmod.h> |
| 34 | #include <linux/list.h> |
Paul Jackson | 68860ec | 2005-10-30 15:02:36 -0800 | [diff] [blame] | 35 | #include <linux/mempolicy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/mm.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/namei.h> |
| 40 | #include <linux/pagemap.h> |
| 41 | #include <linux/proc_fs.h> |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 42 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/sched.h> |
| 44 | #include <linux/seq_file.h> |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame] | 45 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/stat.h> |
| 49 | #include <linux/string.h> |
| 50 | #include <linux/time.h> |
| 51 | #include <linux/backing-dev.h> |
| 52 | #include <linux/sort.h> |
| 53 | |
| 54 | #include <asm/uaccess.h> |
| 55 | #include <asm/atomic.h> |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 56 | #include <linux/mutex.h> |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 57 | #include <linux/kfifo.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Tracks how many cpusets are currently defined in system. |
| 61 | * When there is only one cpuset (the root cpuset) we can |
| 62 | * short circuit some hooks. |
| 63 | */ |
Paul Jackson | 7edc596 | 2006-01-08 01:02:03 -0800 | [diff] [blame] | 64 | int number_of_cpusets __read_mostly; |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 65 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 66 | /* Retrieve the cpuset from a cgroup */ |
| 67 | struct cgroup_subsys cpuset_subsys; |
| 68 | struct cpuset; |
| 69 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 70 | /* See "Frequency meter" comments, below. */ |
| 71 | |
| 72 | struct fmeter { |
| 73 | int cnt; /* unprocessed events count */ |
| 74 | int val; /* most recent output value */ |
| 75 | time_t time; /* clock (secs) when val computed */ |
| 76 | spinlock_t lock; /* guards read or write of above */ |
| 77 | }; |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct cpuset { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 80 | struct cgroup_subsys_state css; |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | unsigned long flags; /* "unsigned long" so bitops work */ |
| 83 | cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ |
| 84 | nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | struct cpuset *parent; /* my parent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Copy of global cpuset_mems_generation as of the most |
| 90 | * recent time this cpuset changed its mems_allowed. |
| 91 | */ |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 92 | int mems_generation; |
| 93 | |
| 94 | struct fmeter fmeter; /* memory_pressure filter */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 95 | |
| 96 | /* partition number for rebuild_sched_domains() */ |
| 97 | int pn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 100 | /* Retrieve the cpuset for a cgroup */ |
| 101 | static inline struct cpuset *cgroup_cs(struct cgroup *cont) |
| 102 | { |
| 103 | return container_of(cgroup_subsys_state(cont, cpuset_subsys_id), |
| 104 | struct cpuset, css); |
| 105 | } |
| 106 | |
| 107 | /* Retrieve the cpuset for a task */ |
| 108 | static inline struct cpuset *task_cs(struct task_struct *task) |
| 109 | { |
| 110 | return container_of(task_subsys_state(task, cpuset_subsys_id), |
| 111 | struct cpuset, css); |
| 112 | } |
| 113 | |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* bits in struct cpuset flags field */ |
| 116 | typedef enum { |
| 117 | CS_CPU_EXCLUSIVE, |
| 118 | CS_MEM_EXCLUSIVE, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 119 | CS_MEMORY_MIGRATE, |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 120 | CS_SCHED_LOAD_BALANCE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 121 | CS_SPREAD_PAGE, |
| 122 | CS_SPREAD_SLAB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } cpuset_flagbits_t; |
| 124 | |
| 125 | /* convenient tests for these bits */ |
| 126 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
| 127 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 128 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static inline int is_mem_exclusive(const struct cpuset *cs) |
| 132 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 133 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 136 | static inline int is_sched_load_balance(const struct cpuset *cs) |
| 137 | { |
| 138 | return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
| 139 | } |
| 140 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 141 | static inline int is_memory_migrate(const struct cpuset *cs) |
| 142 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 143 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 146 | static inline int is_spread_page(const struct cpuset *cs) |
| 147 | { |
| 148 | return test_bit(CS_SPREAD_PAGE, &cs->flags); |
| 149 | } |
| 150 | |
| 151 | static inline int is_spread_slab(const struct cpuset *cs) |
| 152 | { |
| 153 | return test_bit(CS_SPREAD_SLAB, &cs->flags); |
| 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 157 | * Increment this integer everytime any cpuset changes its |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | * mems_allowed value. Users of cpusets can track this generation |
| 159 | * number, and avoid having to lock and reload mems_allowed unless |
| 160 | * the cpuset they're using changes generation. |
| 161 | * |
| 162 | * A single, global generation is needed because attach_task() could |
| 163 | * reattach a task to a different cpuset, which must not have its |
| 164 | * generation numbers aliased with those of that tasks previous cpuset. |
| 165 | * |
| 166 | * Generations are needed for mems_allowed because one task cannot |
| 167 | * modify anothers memory placement. So we must enable every task, |
| 168 | * on every visit to __alloc_pages(), to efficiently check whether |
| 169 | * its current->cpuset->mems_allowed has changed, requiring an update |
| 170 | * of its current->mems_allowed. |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 171 | * |
| 172 | * Since cpuset_mems_generation is guarded by manage_mutex, |
| 173 | * there is no need to mark it atomic. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | */ |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 175 | static int cpuset_mems_generation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | static struct cpuset top_cpuset = { |
| 178 | .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)), |
| 179 | .cpus_allowed = CPU_MASK_ALL, |
| 180 | .mems_allowed = NODE_MASK_ALL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 184 | * We have two global cpuset mutexes below. They can nest. |
| 185 | * It is ok to first take manage_mutex, then nest callback_mutex. We also |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 186 | * require taking task_lock() when dereferencing a tasks cpuset pointer. |
| 187 | * See "The task_lock() exception", at the end of this comment. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 189 | * A task must hold both mutexes to modify cpusets. If a task |
| 190 | * holds manage_mutex, then it blocks others wanting that mutex, |
| 191 | * ensuring that it is the only task able to also acquire callback_mutex |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 192 | * and be able to modify cpusets. It can perform various checks on |
| 193 | * the cpuset structure first, knowing nothing will change. It can |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 194 | * also allocate memory while just holding manage_mutex. While it is |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 195 | * performing these checks, various callback routines can briefly |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 196 | * acquire callback_mutex to query cpusets. Once it is ready to make |
| 197 | * the changes, it takes callback_mutex, blocking everyone else. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 199 | * Calls to the kernel memory allocator can not be made while holding |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 200 | * callback_mutex, as that would risk double tripping on callback_mutex |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 201 | * from one of the callbacks into the cpuset code from within |
| 202 | * __alloc_pages(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 204 | * 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] | 205 | * access to cpusets. |
| 206 | * |
| 207 | * The task_struct fields mems_allowed and mems_generation may only |
| 208 | * be accessed in the context of that task, so require no locks. |
| 209 | * |
| 210 | * Any task can increment and decrement the count field without lock. |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 211 | * So in general, code holding manage_mutex or callback_mutex can't rely |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 212 | * on the count field not changing. However, if the count goes to |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 213 | * zero, then only attach_task(), which holds both mutexes, can |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 214 | * increment it again. Because a count of zero means that no tasks |
| 215 | * are currently attached, therefore there is no way a task attached |
| 216 | * to that cpuset can fork (the other way to increment the count). |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 217 | * So code holding manage_mutex or callback_mutex can safely assume that |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 218 | * if the count is zero, it will stay zero. Similarly, if a task |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 219 | * holds manage_mutex or callback_mutex on a cpuset with zero count, it |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 220 | * knows that the cpuset won't be removed, as cpuset_rmdir() needs |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 221 | * both of those mutexes. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 222 | * |
| 223 | * The cpuset_common_file_write handler for operations that modify |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 224 | * the cpuset hierarchy holds manage_mutex across the entire operation, |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 225 | * single threading all such cpuset modifications across the system. |
| 226 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 227 | * The cpuset_common_file_read() handlers only hold callback_mutex across |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 228 | * small pieces of code, such as when reading out possibly multi-word |
| 229 | * cpumasks and nodemasks. |
| 230 | * |
| 231 | * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 232 | * (usually) take either mutex. These are the two most performance |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 233 | * critical pieces of code here. The exception occurs on cpuset_exit(), |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 234 | * when a task in a notify_on_release cpuset exits. Then manage_mutex |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 235 | * is taken, and if the cpuset count is zero, a usermode call made |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * to /sbin/cpuset_release_agent with the name of the cpuset (path |
| 237 | * relative to the root of cpuset file system) as the argument. |
| 238 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 239 | * A cpuset can only be deleted if both its 'count' of using tasks |
| 240 | * is zero, and its list of 'children' cpusets is empty. Since all |
| 241 | * tasks in the system use _some_ cpuset, and since there is always at |
Sukadev Bhattiprolu | f400e19 | 2006-09-29 02:00:07 -0700 | [diff] [blame] | 242 | * least one task in the system (init), therefore, top_cpuset |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 243 | * always has either children cpusets and/or using tasks. So we don't |
| 244 | * need a special hack to ensure that top_cpuset cannot be deleted. |
| 245 | * |
| 246 | * The above "Tale of Two Semaphores" would be complete, but for: |
| 247 | * |
| 248 | * The task_lock() exception |
| 249 | * |
| 250 | * The need for this exception arises from the action of attach_task(), |
| 251 | * which overwrites one tasks cpuset pointer with another. It does |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 252 | * so using both mutexes, however there are several performance |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 253 | * critical places that need to reference task->cpuset without the |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 254 | * expense of grabbing a system global mutex. Therefore except as |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 255 | * noted below, when dereferencing or, as in attach_task(), modifying |
| 256 | * a tasks cpuset pointer we use task_lock(), which acts on a spinlock |
| 257 | * (task->alloc_lock) already in the task_struct routinely used for |
| 258 | * such matters. |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 259 | * |
| 260 | * P.S. One more locking exception. RCU is used to guard the |
| 261 | * update of a tasks cpuset pointer by attach_task() and the |
| 262 | * access of task->cpuset->mems_generation via that pointer in |
| 263 | * the routine cpuset_update_task_memory_state(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | */ |
| 265 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 266 | static DEFINE_MUTEX(callback_mutex); |
Paul Jackson | 4247bdc | 2005-09-10 00:26:06 -0700 | [diff] [blame] | 267 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 268 | /* This is ugly, but preserves the userspace API for existing cpuset |
| 269 | * users. If someone tries to mount the "cpuset" filesystem, we |
| 270 | * silently switch it to mount "cgroup" instead */ |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 271 | static int cpuset_get_sb(struct file_system_type *fs_type, |
| 272 | int flags, const char *unused_dev_name, |
| 273 | void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 275 | struct file_system_type *cgroup_fs = get_fs_type("cgroup"); |
| 276 | int ret = -ENODEV; |
| 277 | if (cgroup_fs) { |
| 278 | char mountopts[] = |
| 279 | "cpuset,noprefix," |
| 280 | "release_agent=/sbin/cpuset_release_agent"; |
| 281 | ret = cgroup_fs->get_sb(cgroup_fs, flags, |
| 282 | unused_dev_name, mountopts, mnt); |
| 283 | put_filesystem(cgroup_fs); |
| 284 | } |
| 285 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static struct file_system_type cpuset_fs_type = { |
| 289 | .name = "cpuset", |
| 290 | .get_sb = cpuset_get_sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* |
| 294 | * Return in *pmask the portion of a cpusets's cpus_allowed that |
| 295 | * are online. If none are online, walk up the cpuset hierarchy |
| 296 | * until we find one that does have some online cpus. If we get |
| 297 | * all the way to the top and still haven't found any online cpus, |
| 298 | * return cpu_online_map. Or if passed a NULL cs from an exit'ing |
| 299 | * task, return cpu_online_map. |
| 300 | * |
| 301 | * One way or another, we guarantee to return some non-empty subset |
| 302 | * of cpu_online_map. |
| 303 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 304 | * Call with callback_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | */ |
| 306 | |
| 307 | static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask) |
| 308 | { |
| 309 | while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map)) |
| 310 | cs = cs->parent; |
| 311 | if (cs) |
| 312 | cpus_and(*pmask, cs->cpus_allowed, cpu_online_map); |
| 313 | else |
| 314 | *pmask = cpu_online_map; |
| 315 | BUG_ON(!cpus_intersects(*pmask, cpu_online_map)); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Return in *pmask the portion of a cpusets's mems_allowed that |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 320 | * are online, with memory. If none are online with memory, walk |
| 321 | * up the cpuset hierarchy until we find one that does have some |
| 322 | * online mems. If we get all the way to the top and still haven't |
| 323 | * found any online mems, return node_states[N_HIGH_MEMORY]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | * |
| 325 | * One way or another, we guarantee to return some non-empty subset |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 326 | * of node_states[N_HIGH_MEMORY]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 328 | * Call with callback_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ |
| 330 | |
| 331 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) |
| 332 | { |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 333 | while (cs && !nodes_intersects(cs->mems_allowed, |
| 334 | node_states[N_HIGH_MEMORY])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | cs = cs->parent; |
| 336 | if (cs) |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 337 | nodes_and(*pmask, cs->mems_allowed, |
| 338 | node_states[N_HIGH_MEMORY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | else |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 340 | *pmask = node_states[N_HIGH_MEMORY]; |
| 341 | BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 344 | /** |
| 345 | * cpuset_update_task_memory_state - update task memory placement |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 347 | * If the current tasks cpusets mems_allowed changed behind our |
| 348 | * backs, update current->mems_allowed, mems_generation and task NUMA |
| 349 | * mempolicy to the new value. |
| 350 | * |
| 351 | * Task mempolicy is updated by rebinding it relative to the |
| 352 | * current->cpuset if a task has its memory placement changed. |
| 353 | * Do not call this routine if in_interrupt(). |
| 354 | * |
Paul Jackson | 4a01c8d | 2006-03-31 02:30:50 -0800 | [diff] [blame] | 355 | * Call without callback_mutex or task_lock() held. May be |
| 356 | * called with or without manage_mutex held. Thanks in part to |
| 357 | * 'the_top_cpuset_hack', the tasks cpuset pointer will never |
| 358 | * be NULL. This routine also might acquire callback_mutex and |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 359 | * current->mm->mmap_sem during call. |
Paul Jackson | 5aa15b5 | 2005-10-30 15:02:28 -0800 | [diff] [blame] | 360 | * |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 361 | * Reading current->cpuset->mems_generation doesn't need task_lock |
| 362 | * to guard the current->cpuset derefence, because it is guarded |
| 363 | * from concurrent freeing of current->cpuset by attach_task(), |
| 364 | * using RCU. |
| 365 | * |
| 366 | * The rcu_dereference() is technically probably not needed, |
| 367 | * as I don't actually mind if I see a new cpuset pointer but |
| 368 | * an old value of mems_generation. However this really only |
| 369 | * matters on alpha systems using cpusets heavily. If I dropped |
| 370 | * that rcu_dereference(), it would save them a memory barrier. |
| 371 | * For all other arch's, rcu_dereference is a no-op anyway, and for |
| 372 | * alpha systems not using cpusets, another planned optimization, |
| 373 | * avoiding the rcu critical section for tasks in the root cpuset |
| 374 | * which is statically allocated, so can't vanish, will make this |
| 375 | * irrelevant. Better to use RCU as intended, than to engage in |
| 376 | * some cute trick to save a memory barrier that is impossible to |
| 377 | * test, for alpha systems using cpusets heavily, which might not |
| 378 | * even exist. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 379 | * |
| 380 | * This routine is needed to update the per-task mems_allowed data, |
| 381 | * within the tasks context, when it is trying to allocate memory |
| 382 | * (in various mm/mempolicy.c routines) and notices that some other |
| 383 | * task has been modifying its cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | */ |
| 385 | |
Randy Dunlap | fe85a99 | 2006-02-03 03:04:23 -0800 | [diff] [blame] | 386 | void cpuset_update_task_memory_state(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 388 | int my_cpusets_mem_gen; |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 389 | struct task_struct *tsk = current; |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 390 | struct cpuset *cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 392 | if (task_cs(tsk) == &top_cpuset) { |
Paul Jackson | 03a285f | 2006-01-08 01:02:04 -0800 | [diff] [blame] | 393 | /* Don't need rcu for top_cpuset. It's never freed. */ |
| 394 | my_cpusets_mem_gen = top_cpuset.mems_generation; |
| 395 | } else { |
| 396 | rcu_read_lock(); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 397 | my_cpusets_mem_gen = task_cs(current)->mems_generation; |
Paul Jackson | 03a285f | 2006-01-08 01:02:04 -0800 | [diff] [blame] | 398 | rcu_read_unlock(); |
| 399 | } |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 400 | |
| 401 | if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 402 | mutex_lock(&callback_mutex); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 403 | task_lock(tsk); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 404 | cs = task_cs(tsk); /* Maybe changed when task not locked */ |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 405 | guarantee_online_mems(cs, &tsk->mems_allowed); |
| 406 | tsk->cpuset_mems_generation = cs->mems_generation; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 407 | if (is_spread_page(cs)) |
| 408 | tsk->flags |= PF_SPREAD_PAGE; |
| 409 | else |
| 410 | tsk->flags &= ~PF_SPREAD_PAGE; |
| 411 | if (is_spread_slab(cs)) |
| 412 | tsk->flags |= PF_SPREAD_SLAB; |
| 413 | else |
| 414 | tsk->flags &= ~PF_SPREAD_SLAB; |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 415 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 416 | mutex_unlock(&callback_mutex); |
Paul Jackson | 74cb215 | 2006-01-08 01:01:56 -0800 | [diff] [blame] | 417 | mpol_rebind_task(tsk, &tsk->mems_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? |
| 423 | * |
| 424 | * One cpuset is a subset of another if all its allowed CPUs and |
| 425 | * Memory Nodes are a subset of the other, and its exclusive flags |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 426 | * are only set if the other's are set. Call holding manage_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | */ |
| 428 | |
| 429 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) |
| 430 | { |
| 431 | return cpus_subset(p->cpus_allowed, q->cpus_allowed) && |
| 432 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
| 433 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && |
| 434 | is_mem_exclusive(p) <= is_mem_exclusive(q); |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * validate_change() - Used to validate that any proposed cpuset change |
| 439 | * follows the structural rules for cpusets. |
| 440 | * |
| 441 | * If we replaced the flag and mask values of the current cpuset |
| 442 | * (cur) with those values in the trial cpuset (trial), would |
| 443 | * our various subset and exclusive rules still be valid? Presumes |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 444 | * manage_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | * |
| 446 | * 'cur' is the address of an actual, in-use cpuset. Operations |
| 447 | * such as list traversal that depend on the actual address of the |
| 448 | * cpuset in the list must use cur below, not trial. |
| 449 | * |
| 450 | * 'trial' is the address of bulk structure copy of cur, with |
| 451 | * perhaps one or more of the fields cpus_allowed, mems_allowed, |
| 452 | * or flags changed to new, trial values. |
| 453 | * |
| 454 | * Return 0 if valid, -errno if not. |
| 455 | */ |
| 456 | |
| 457 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) |
| 458 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 459 | struct cgroup *cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | struct cpuset *c, *par; |
| 461 | |
| 462 | /* Each of our child cpusets must be a subset of us */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 463 | list_for_each_entry(cont, &cur->css.cgroup->children, sibling) { |
| 464 | if (!is_cpuset_subset(cgroup_cs(cont), trial)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | return -EBUSY; |
| 466 | } |
| 467 | |
| 468 | /* Remaining checks don't apply to root cpuset */ |
Paul Jackson | 6960406 | 2006-12-06 20:36:15 -0800 | [diff] [blame] | 469 | if (cur == &top_cpuset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return 0; |
| 471 | |
Paul Jackson | 6960406 | 2006-12-06 20:36:15 -0800 | [diff] [blame] | 472 | par = cur->parent; |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* We must be a subset of our parent cpuset */ |
| 475 | if (!is_cpuset_subset(trial, par)) |
| 476 | return -EACCES; |
| 477 | |
| 478 | /* If either I or some sibling (!= me) is exclusive, we can't overlap */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 479 | list_for_each_entry(cont, &par->css.cgroup->children, sibling) { |
| 480 | c = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
| 482 | c != cur && |
| 483 | cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) |
| 484 | return -EINVAL; |
| 485 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
| 486 | c != cur && |
| 487 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 494 | /* |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 495 | * Helper routine for rebuild_sched_domains(). |
| 496 | * Do cpusets a, b have overlapping cpus_allowed masks? |
| 497 | */ |
| 498 | |
| 499 | static int cpusets_overlap(struct cpuset *a, struct cpuset *b) |
| 500 | { |
| 501 | return cpus_intersects(a->cpus_allowed, b->cpus_allowed); |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * rebuild_sched_domains() |
| 506 | * |
| 507 | * If the flag 'sched_load_balance' of any cpuset with non-empty |
| 508 | * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset |
| 509 | * which has that flag enabled, or if any cpuset with a non-empty |
| 510 | * 'cpus' is removed, then call this routine to rebuild the |
| 511 | * scheduler's dynamic sched domains. |
| 512 | * |
| 513 | * This routine builds a partial partition of the systems CPUs |
| 514 | * (the set of non-overlappping cpumask_t's in the array 'part' |
| 515 | * below), and passes that partial partition to the kernel/sched.c |
| 516 | * partition_sched_domains() routine, which will rebuild the |
| 517 | * schedulers load balancing domains (sched domains) as specified |
| 518 | * by that partial partition. A 'partial partition' is a set of |
| 519 | * non-overlapping subsets whose union is a subset of that set. |
| 520 | * |
| 521 | * See "What is sched_load_balance" in Documentation/cpusets.txt |
| 522 | * for a background explanation of this. |
| 523 | * |
| 524 | * Does not return errors, on the theory that the callers of this |
| 525 | * routine would rather not worry about failures to rebuild sched |
| 526 | * domains when operating in the severe memory shortage situations |
| 527 | * that could cause allocation failures below. |
| 528 | * |
| 529 | * Call with cgroup_mutex held. May take callback_mutex during |
| 530 | * call due to the kfifo_alloc() and kmalloc() calls. May nest |
| 531 | * a call to the lock_cpu_hotplug()/unlock_cpu_hotplug() pair. |
| 532 | * Must not be called holding callback_mutex, because we must not |
| 533 | * call lock_cpu_hotplug() while holding callback_mutex. Elsewhere |
| 534 | * the kernel nests callback_mutex inside lock_cpu_hotplug() calls. |
| 535 | * So the reverse nesting would risk an ABBA deadlock. |
| 536 | * |
| 537 | * The three key local variables below are: |
| 538 | * q - a kfifo queue of cpuset pointers, used to implement a |
| 539 | * top-down scan of all cpusets. This scan loads a pointer |
| 540 | * to each cpuset marked is_sched_load_balance into the |
| 541 | * array 'csa'. For our purposes, rebuilding the schedulers |
| 542 | * sched domains, we can ignore !is_sched_load_balance cpusets. |
| 543 | * csa - (for CpuSet Array) Array of pointers to all the cpusets |
| 544 | * that need to be load balanced, for convenient iterative |
| 545 | * access by the subsequent code that finds the best partition, |
| 546 | * i.e the set of domains (subsets) of CPUs such that the |
| 547 | * cpus_allowed of every cpuset marked is_sched_load_balance |
| 548 | * is a subset of one of these domains, while there are as |
| 549 | * many such domains as possible, each as small as possible. |
| 550 | * doms - Conversion of 'csa' to an array of cpumasks, for passing to |
| 551 | * the kernel/sched.c routine partition_sched_domains() in a |
| 552 | * convenient format, that can be easily compared to the prior |
| 553 | * value to determine what partition elements (sched domains) |
| 554 | * were changed (added or removed.) |
| 555 | * |
| 556 | * Finding the best partition (set of domains): |
| 557 | * The triple nested loops below over i, j, k scan over the |
| 558 | * load balanced cpusets (using the array of cpuset pointers in |
| 559 | * csa[]) looking for pairs of cpusets that have overlapping |
| 560 | * cpus_allowed, but which don't have the same 'pn' partition |
| 561 | * number and gives them in the same partition number. It keeps |
| 562 | * looping on the 'restart' label until it can no longer find |
| 563 | * any such pairs. |
| 564 | * |
| 565 | * The union of the cpus_allowed masks from the set of |
| 566 | * all cpusets having the same 'pn' value then form the one |
| 567 | * element of the partition (one sched domain) to be passed to |
| 568 | * partition_sched_domains(). |
| 569 | */ |
| 570 | |
| 571 | static void rebuild_sched_domains(void) |
| 572 | { |
| 573 | struct kfifo *q; /* queue of cpusets to be scanned */ |
| 574 | struct cpuset *cp; /* scans q */ |
| 575 | struct cpuset **csa; /* array of all cpuset ptrs */ |
| 576 | int csn; /* how many cpuset ptrs in csa so far */ |
| 577 | int i, j, k; /* indices for partition finding loops */ |
| 578 | cpumask_t *doms; /* resulting partition; i.e. sched domains */ |
| 579 | int ndoms; /* number of sched domains in result */ |
| 580 | int nslot; /* next empty doms[] cpumask_t slot */ |
| 581 | |
| 582 | q = NULL; |
| 583 | csa = NULL; |
| 584 | doms = NULL; |
| 585 | |
| 586 | /* Special case for the 99% of systems with one, full, sched domain */ |
| 587 | if (is_sched_load_balance(&top_cpuset)) { |
| 588 | ndoms = 1; |
| 589 | doms = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 590 | if (!doms) |
| 591 | goto rebuild; |
| 592 | *doms = top_cpuset.cpus_allowed; |
| 593 | goto rebuild; |
| 594 | } |
| 595 | |
| 596 | q = kfifo_alloc(number_of_cpusets * sizeof(cp), GFP_KERNEL, NULL); |
| 597 | if (IS_ERR(q)) |
| 598 | goto done; |
| 599 | csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL); |
| 600 | if (!csa) |
| 601 | goto done; |
| 602 | csn = 0; |
| 603 | |
| 604 | cp = &top_cpuset; |
| 605 | __kfifo_put(q, (void *)&cp, sizeof(cp)); |
| 606 | while (__kfifo_get(q, (void *)&cp, sizeof(cp))) { |
| 607 | struct cgroup *cont; |
| 608 | struct cpuset *child; /* scans child cpusets of cp */ |
| 609 | if (is_sched_load_balance(cp)) |
| 610 | csa[csn++] = cp; |
| 611 | list_for_each_entry(cont, &cp->css.cgroup->children, sibling) { |
| 612 | child = cgroup_cs(cont); |
| 613 | __kfifo_put(q, (void *)&child, sizeof(cp)); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | for (i = 0; i < csn; i++) |
| 618 | csa[i]->pn = i; |
| 619 | ndoms = csn; |
| 620 | |
| 621 | restart: |
| 622 | /* Find the best partition (set of sched domains) */ |
| 623 | for (i = 0; i < csn; i++) { |
| 624 | struct cpuset *a = csa[i]; |
| 625 | int apn = a->pn; |
| 626 | |
| 627 | for (j = 0; j < csn; j++) { |
| 628 | struct cpuset *b = csa[j]; |
| 629 | int bpn = b->pn; |
| 630 | |
| 631 | if (apn != bpn && cpusets_overlap(a, b)) { |
| 632 | for (k = 0; k < csn; k++) { |
| 633 | struct cpuset *c = csa[k]; |
| 634 | |
| 635 | if (c->pn == bpn) |
| 636 | c->pn = apn; |
| 637 | } |
| 638 | ndoms--; /* one less element */ |
| 639 | goto restart; |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /* Convert <csn, csa> to <ndoms, doms> */ |
| 645 | doms = kmalloc(ndoms * sizeof(cpumask_t), GFP_KERNEL); |
| 646 | if (!doms) |
| 647 | goto rebuild; |
| 648 | |
| 649 | for (nslot = 0, i = 0; i < csn; i++) { |
| 650 | struct cpuset *a = csa[i]; |
| 651 | int apn = a->pn; |
| 652 | |
| 653 | if (apn >= 0) { |
| 654 | cpumask_t *dp = doms + nslot; |
| 655 | |
| 656 | if (nslot == ndoms) { |
| 657 | static int warnings = 10; |
| 658 | if (warnings) { |
| 659 | printk(KERN_WARNING |
| 660 | "rebuild_sched_domains confused:" |
| 661 | " nslot %d, ndoms %d, csn %d, i %d," |
| 662 | " apn %d\n", |
| 663 | nslot, ndoms, csn, i, apn); |
| 664 | warnings--; |
| 665 | } |
| 666 | continue; |
| 667 | } |
| 668 | |
| 669 | cpus_clear(*dp); |
| 670 | for (j = i; j < csn; j++) { |
| 671 | struct cpuset *b = csa[j]; |
| 672 | |
| 673 | if (apn == b->pn) { |
| 674 | cpus_or(*dp, *dp, b->cpus_allowed); |
| 675 | b->pn = -1; |
| 676 | } |
| 677 | } |
| 678 | nslot++; |
| 679 | } |
| 680 | } |
| 681 | BUG_ON(nslot != ndoms); |
| 682 | |
| 683 | rebuild: |
| 684 | /* Have scheduler rebuild sched domains */ |
| 685 | lock_cpu_hotplug(); |
| 686 | partition_sched_domains(ndoms, doms); |
| 687 | unlock_cpu_hotplug(); |
| 688 | |
| 689 | done: |
| 690 | if (q && !IS_ERR(q)) |
| 691 | kfifo_free(q); |
| 692 | kfree(csa); |
| 693 | /* Don't kfree(doms) -- partition_sched_domains() does that. */ |
| 694 | } |
| 695 | |
| 696 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 697 | * Call with manage_mutex held. May take callback_mutex during call. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 698 | */ |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | static int update_cpumask(struct cpuset *cs, char *buf) |
| 701 | { |
| 702 | struct cpuset trialcs; |
Paul Jackson | 607717a | 2007-10-16 01:27:43 -0700 | [diff] [blame] | 703 | int retval; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 704 | int cpus_changed, is_load_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 706 | /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */ |
| 707 | if (cs == &top_cpuset) |
| 708 | return -EACCES; |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | trialcs = *cs; |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 711 | |
| 712 | /* |
| 713 | * We allow a cpuset's cpus_allowed to be empty; if it has attached |
| 714 | * tasks, we'll catch it later when we validate the change and return |
| 715 | * -ENOSPC. |
| 716 | */ |
| 717 | if (!buf[0] || (buf[0] == '\n' && !buf[1])) { |
| 718 | cpus_clear(trialcs.cpus_allowed); |
| 719 | } else { |
| 720 | retval = cpulist_parse(buf, trialcs.cpus_allowed); |
| 721 | if (retval < 0) |
| 722 | return retval; |
| 723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 725 | /* cpus_allowed cannot be empty for a cpuset with attached tasks. */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 726 | if (cgroup_task_count(cs->css.cgroup) && |
| 727 | cpus_empty(trialcs.cpus_allowed)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | return -ENOSPC; |
| 729 | retval = validate_change(cs, &trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 730 | if (retval < 0) |
| 731 | return retval; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 732 | |
| 733 | cpus_changed = !cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); |
| 734 | is_load_balanced = is_sched_load_balance(&trialcs); |
| 735 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 736 | mutex_lock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 737 | cs->cpus_allowed = trialcs.cpus_allowed; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 738 | mutex_unlock(&callback_mutex); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 739 | |
| 740 | if (cpus_changed && is_load_balanced) |
| 741 | rebuild_sched_domains(); |
| 742 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 743 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 746 | /* |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 747 | * cpuset_migrate_mm |
| 748 | * |
| 749 | * Migrate memory region from one set of nodes to another. |
| 750 | * |
| 751 | * Temporarilly set tasks mems_allowed to target nodes of migration, |
| 752 | * so that the migration code can allocate pages on these nodes. |
| 753 | * |
| 754 | * Call holding manage_mutex, so our current->cpuset won't change |
| 755 | * during this call, as manage_mutex holds off any attach_task() |
| 756 | * calls. Therefore we don't need to take task_lock around the |
| 757 | * call to guarantee_online_mems(), as we know no one is changing |
| 758 | * our tasks cpuset. |
| 759 | * |
| 760 | * Hold callback_mutex around the two modifications of our tasks |
| 761 | * mems_allowed to synchronize with cpuset_mems_allowed(). |
| 762 | * |
| 763 | * While the mm_struct we are migrating is typically from some |
| 764 | * other task, the task_struct mems_allowed that we are hacking |
| 765 | * is for our current task, which must allocate new pages for that |
| 766 | * migrating memory region. |
| 767 | * |
| 768 | * We call cpuset_update_task_memory_state() before hacking |
| 769 | * our tasks mems_allowed, so that we are assured of being in |
| 770 | * sync with our tasks cpuset, and in particular, callbacks to |
| 771 | * cpuset_update_task_memory_state() from nested page allocations |
| 772 | * won't see any mismatch of our cpuset and task mems_generation |
| 773 | * values, so won't overwrite our hacked tasks mems_allowed |
| 774 | * nodemask. |
| 775 | */ |
| 776 | |
| 777 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, |
| 778 | const nodemask_t *to) |
| 779 | { |
| 780 | struct task_struct *tsk = current; |
| 781 | |
| 782 | cpuset_update_task_memory_state(); |
| 783 | |
| 784 | mutex_lock(&callback_mutex); |
| 785 | tsk->mems_allowed = *to; |
| 786 | mutex_unlock(&callback_mutex); |
| 787 | |
| 788 | do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL); |
| 789 | |
| 790 | mutex_lock(&callback_mutex); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 791 | guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed); |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 792 | mutex_unlock(&callback_mutex); |
| 793 | } |
| 794 | |
| 795 | /* |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 796 | * Handle user request to change the 'mems' memory placement |
| 797 | * of a cpuset. Needs to validate the request, update the |
| 798 | * cpusets mems_allowed and mems_generation, and for each |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 799 | * task in the cpuset, rebind any vma mempolicies and if |
| 800 | * the cpuset is marked 'memory_migrate', migrate the tasks |
| 801 | * pages to the new memory. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 802 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 803 | * Call with manage_mutex held. May take callback_mutex during call. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 804 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
| 805 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind |
| 806 | * their mempolicies to the cpusets new mems_allowed. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 807 | */ |
| 808 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 809 | static void *cpuset_being_rebound; |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | static int update_nodemask(struct cpuset *cs, char *buf) |
| 812 | { |
| 813 | struct cpuset trialcs; |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 814 | nodemask_t oldmem; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 815 | struct task_struct *p; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 816 | struct mm_struct **mmarray; |
| 817 | int i, n, ntasks; |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 818 | int migrate; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 819 | int fudge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | int retval; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 821 | struct cgroup_iter it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 823 | /* |
| 824 | * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY]; |
| 825 | * it's read-only |
| 826 | */ |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 827 | if (cs == &top_cpuset) |
| 828 | return -EACCES; |
| 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | trialcs = *cs; |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * We allow a cpuset's mems_allowed to be empty; if it has attached |
| 834 | * tasks, we'll catch it later when we validate the change and return |
| 835 | * -ENOSPC. |
| 836 | */ |
| 837 | if (!buf[0] || (buf[0] == '\n' && !buf[1])) { |
| 838 | nodes_clear(trialcs.mems_allowed); |
| 839 | } else { |
| 840 | retval = nodelist_parse(buf, trialcs.mems_allowed); |
| 841 | if (retval < 0) |
| 842 | goto done; |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 843 | if (!nodes_intersects(trialcs.mems_allowed, |
| 844 | node_states[N_HIGH_MEMORY])) { |
| 845 | /* |
| 846 | * error if only memoryless nodes specified. |
| 847 | */ |
| 848 | retval = -ENOSPC; |
| 849 | goto done; |
| 850 | } |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 851 | } |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 852 | /* |
| 853 | * Exclude memoryless nodes. We know that trialcs.mems_allowed |
| 854 | * contains at least one node with memory. |
| 855 | */ |
| 856 | nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, |
| 857 | node_states[N_HIGH_MEMORY]); |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 858 | oldmem = cs->mems_allowed; |
| 859 | if (nodes_equal(oldmem, trialcs.mems_allowed)) { |
| 860 | retval = 0; /* Too easy - nothing to do */ |
| 861 | goto done; |
| 862 | } |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 863 | /* mems_allowed cannot be empty for a cpuset with attached tasks. */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 864 | if (cgroup_task_count(cs->css.cgroup) && |
| 865 | nodes_empty(trialcs.mems_allowed)) { |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 866 | retval = -ENOSPC; |
| 867 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 869 | retval = validate_change(cs, &trialcs); |
| 870 | if (retval < 0) |
| 871 | goto done; |
| 872 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 873 | mutex_lock(&callback_mutex); |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 874 | cs->mems_allowed = trialcs.mems_allowed; |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 875 | cs->mems_generation = cpuset_mems_generation++; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 876 | mutex_unlock(&callback_mutex); |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 877 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 878 | cpuset_being_rebound = cs; /* causes mpol_copy() rebind */ |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 879 | |
| 880 | fudge = 10; /* spare mmarray[] slots */ |
| 881 | fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */ |
| 882 | retval = -ENOMEM; |
| 883 | |
| 884 | /* |
| 885 | * Allocate mmarray[] to hold mm reference for each task |
| 886 | * in cpuset cs. Can't kmalloc GFP_KERNEL while holding |
| 887 | * tasklist_lock. We could use GFP_ATOMIC, but with a |
| 888 | * few more lines of code, we can retry until we get a big |
| 889 | * enough mmarray[] w/o using GFP_ATOMIC. |
| 890 | */ |
| 891 | while (1) { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 892 | ntasks = cgroup_task_count(cs->css.cgroup); /* guess */ |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 893 | ntasks += fudge; |
| 894 | mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL); |
| 895 | if (!mmarray) |
| 896 | goto done; |
Paul Menage | c2aef33 | 2007-07-15 23:40:11 -0700 | [diff] [blame] | 897 | read_lock(&tasklist_lock); /* block fork */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 898 | if (cgroup_task_count(cs->css.cgroup) <= ntasks) |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 899 | break; /* got enough */ |
Paul Menage | c2aef33 | 2007-07-15 23:40:11 -0700 | [diff] [blame] | 900 | read_unlock(&tasklist_lock); /* try again */ |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 901 | kfree(mmarray); |
| 902 | } |
| 903 | |
| 904 | n = 0; |
| 905 | |
| 906 | /* Load up mmarray[] with mm reference for each task in cpuset. */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 907 | cgroup_iter_start(cs->css.cgroup, &it); |
| 908 | while ((p = cgroup_iter_next(cs->css.cgroup, &it))) { |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 909 | struct mm_struct *mm; |
| 910 | |
| 911 | if (n >= ntasks) { |
| 912 | printk(KERN_WARNING |
| 913 | "Cpuset mempolicy rebind incomplete.\n"); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 914 | break; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 915 | } |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 916 | mm = get_task_mm(p); |
| 917 | if (!mm) |
| 918 | continue; |
| 919 | mmarray[n++] = mm; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 920 | } |
| 921 | cgroup_iter_end(cs->css.cgroup, &it); |
Paul Menage | c2aef33 | 2007-07-15 23:40:11 -0700 | [diff] [blame] | 922 | read_unlock(&tasklist_lock); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 923 | |
| 924 | /* |
| 925 | * Now that we've dropped the tasklist spinlock, we can |
| 926 | * rebind the vma mempolicies of each mm in mmarray[] to their |
| 927 | * new cpuset, and release that mm. The mpol_rebind_mm() |
| 928 | * call takes mmap_sem, which we couldn't take while holding |
| 929 | * tasklist_lock. Forks can happen again now - the mpol_copy() |
| 930 | * cpuset_being_rebound check will catch such forks, and rebind |
| 931 | * their vma mempolicies too. Because we still hold the global |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 932 | * cpuset manage_mutex, we know that no other rebind effort will |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 933 | * be contending for the global variable cpuset_being_rebound. |
| 934 | * 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] | 935 | * is idempotent. Also migrate pages in each mm to new nodes. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 936 | */ |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 937 | migrate = is_memory_migrate(cs); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 938 | for (i = 0; i < n; i++) { |
| 939 | struct mm_struct *mm = mmarray[i]; |
| 940 | |
| 941 | mpol_rebind_mm(mm, &cs->mems_allowed); |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 942 | if (migrate) |
| 943 | cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 944 | mmput(mm); |
| 945 | } |
| 946 | |
| 947 | /* We're done rebinding vma's to this cpusets new mems_allowed. */ |
| 948 | kfree(mmarray); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 949 | cpuset_being_rebound = NULL; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 950 | retval = 0; |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 951 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | return retval; |
| 953 | } |
| 954 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 955 | int current_cpuset_is_being_rebound(void) |
| 956 | { |
| 957 | return task_cs(current) == cpuset_being_rebound; |
| 958 | } |
| 959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 961 | * Call with manage_mutex held. |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 962 | */ |
| 963 | |
| 964 | static int update_memory_pressure_enabled(struct cpuset *cs, char *buf) |
| 965 | { |
| 966 | if (simple_strtoul(buf, NULL, 10) != 0) |
| 967 | cpuset_memory_pressure_enabled = 1; |
| 968 | else |
| 969 | cpuset_memory_pressure_enabled = 0; |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | * update_flag - read a 0 or a 1 in a file and update associated flag |
| 975 | * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 976 | * CS_SCHED_LOAD_BALANCE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 977 | * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE, |
| 978 | * CS_SPREAD_PAGE, CS_SPREAD_SLAB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | * cs: the cpuset to update |
| 980 | * buf: the buffer where we read the 0 or 1 |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 981 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 982 | * Call with manage_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | */ |
| 984 | |
| 985 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf) |
| 986 | { |
| 987 | int turning_on; |
| 988 | struct cpuset trialcs; |
Paul Jackson | 607717a | 2007-10-16 01:27:43 -0700 | [diff] [blame] | 989 | int err; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 990 | int cpus_nonempty, balance_flag_changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | turning_on = (simple_strtoul(buf, NULL, 10) != 0); |
| 993 | |
| 994 | trialcs = *cs; |
| 995 | if (turning_on) |
| 996 | set_bit(bit, &trialcs.flags); |
| 997 | else |
| 998 | clear_bit(bit, &trialcs.flags); |
| 999 | |
| 1000 | err = validate_change(cs, &trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1001 | if (err < 0) |
| 1002 | return err; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1003 | |
| 1004 | cpus_nonempty = !cpus_empty(trialcs.cpus_allowed); |
| 1005 | balance_flag_changed = (is_sched_load_balance(cs) != |
| 1006 | is_sched_load_balance(&trialcs)); |
| 1007 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1008 | mutex_lock(&callback_mutex); |
Paul Jackson | 6960406 | 2006-12-06 20:36:15 -0800 | [diff] [blame] | 1009 | cs->flags = trialcs.flags; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1010 | mutex_unlock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1011 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1012 | if (cpus_nonempty && balance_flag_changed) |
| 1013 | rebuild_sched_domains(); |
| 1014 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1015 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1018 | /* |
Adrian Bunk | 80f7228 | 2006-06-30 18:27:16 +0200 | [diff] [blame] | 1019 | * Frequency meter - How fast is some event occurring? |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1020 | * |
| 1021 | * These routines manage a digitally filtered, constant time based, |
| 1022 | * event frequency meter. There are four routines: |
| 1023 | * fmeter_init() - initialize a frequency meter. |
| 1024 | * fmeter_markevent() - called each time the event happens. |
| 1025 | * fmeter_getrate() - returns the recent rate of such events. |
| 1026 | * fmeter_update() - internal routine used to update fmeter. |
| 1027 | * |
| 1028 | * A common data structure is passed to each of these routines, |
| 1029 | * which is used to keep track of the state required to manage the |
| 1030 | * frequency meter and its digital filter. |
| 1031 | * |
| 1032 | * The filter works on the number of events marked per unit time. |
| 1033 | * The filter is single-pole low-pass recursive (IIR). The time unit |
| 1034 | * is 1 second. Arithmetic is done using 32-bit integers scaled to |
| 1035 | * simulate 3 decimal digits of precision (multiplied by 1000). |
| 1036 | * |
| 1037 | * With an FM_COEF of 933, and a time base of 1 second, the filter |
| 1038 | * has a half-life of 10 seconds, meaning that if the events quit |
| 1039 | * happening, then the rate returned from the fmeter_getrate() |
| 1040 | * will be cut in half each 10 seconds, until it converges to zero. |
| 1041 | * |
| 1042 | * It is not worth doing a real infinitely recursive filter. If more |
| 1043 | * than FM_MAXTICKS ticks have elapsed since the last filter event, |
| 1044 | * just compute FM_MAXTICKS ticks worth, by which point the level |
| 1045 | * will be stable. |
| 1046 | * |
| 1047 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid |
| 1048 | * arithmetic overflow in the fmeter_update() routine. |
| 1049 | * |
| 1050 | * Given the simple 32 bit integer arithmetic used, this meter works |
| 1051 | * best for reporting rates between one per millisecond (msec) and |
| 1052 | * one per 32 (approx) seconds. At constant rates faster than one |
| 1053 | * per msec it maxes out at values just under 1,000,000. At constant |
| 1054 | * rates between one per msec, and one per second it will stabilize |
| 1055 | * to a value N*1000, where N is the rate of events per second. |
| 1056 | * At constant rates between one per second and one per 32 seconds, |
| 1057 | * it will be choppy, moving up on the seconds that have an event, |
| 1058 | * and then decaying until the next event. At rates slower than |
| 1059 | * about one in 32 seconds, it decays all the way back to zero between |
| 1060 | * each event. |
| 1061 | */ |
| 1062 | |
| 1063 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ |
| 1064 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ |
| 1065 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ |
| 1066 | #define FM_SCALE 1000 /* faux fixed point scale */ |
| 1067 | |
| 1068 | /* Initialize a frequency meter */ |
| 1069 | static void fmeter_init(struct fmeter *fmp) |
| 1070 | { |
| 1071 | fmp->cnt = 0; |
| 1072 | fmp->val = 0; |
| 1073 | fmp->time = 0; |
| 1074 | spin_lock_init(&fmp->lock); |
| 1075 | } |
| 1076 | |
| 1077 | /* Internal meter update - process cnt events and update value */ |
| 1078 | static void fmeter_update(struct fmeter *fmp) |
| 1079 | { |
| 1080 | time_t now = get_seconds(); |
| 1081 | time_t ticks = now - fmp->time; |
| 1082 | |
| 1083 | if (ticks == 0) |
| 1084 | return; |
| 1085 | |
| 1086 | ticks = min(FM_MAXTICKS, ticks); |
| 1087 | while (ticks-- > 0) |
| 1088 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; |
| 1089 | fmp->time = now; |
| 1090 | |
| 1091 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; |
| 1092 | fmp->cnt = 0; |
| 1093 | } |
| 1094 | |
| 1095 | /* Process any previous ticks, then bump cnt by one (times scale). */ |
| 1096 | static void fmeter_markevent(struct fmeter *fmp) |
| 1097 | { |
| 1098 | spin_lock(&fmp->lock); |
| 1099 | fmeter_update(fmp); |
| 1100 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); |
| 1101 | spin_unlock(&fmp->lock); |
| 1102 | } |
| 1103 | |
| 1104 | /* Process any previous ticks, then return current value. */ |
| 1105 | static int fmeter_getrate(struct fmeter *fmp) |
| 1106 | { |
| 1107 | int val; |
| 1108 | |
| 1109 | spin_lock(&fmp->lock); |
| 1110 | fmeter_update(fmp); |
| 1111 | val = fmp->val; |
| 1112 | spin_unlock(&fmp->lock); |
| 1113 | return val; |
| 1114 | } |
| 1115 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1116 | static int cpuset_can_attach(struct cgroup_subsys *ss, |
| 1117 | struct cgroup *cont, struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1119 | struct cpuset *cs = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
| 1122 | return -ENOSPC; |
| 1123 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1124 | return security_task_setscheduler(tsk, 0, NULL); |
| 1125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1127 | static void cpuset_attach(struct cgroup_subsys *ss, |
| 1128 | struct cgroup *cont, struct cgroup *oldcont, |
| 1129 | struct task_struct *tsk) |
| 1130 | { |
| 1131 | cpumask_t cpus; |
| 1132 | nodemask_t from, to; |
| 1133 | struct mm_struct *mm; |
| 1134 | struct cpuset *cs = cgroup_cs(cont); |
| 1135 | struct cpuset *oldcs = cgroup_cs(oldcont); |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame] | 1136 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1137 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | guarantee_online_cpus(cs, &cpus); |
| 1139 | set_cpus_allowed(tsk, cpus); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1140 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1142 | from = oldcs->mems_allowed; |
| 1143 | to = cs->mems_allowed; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1144 | mm = get_task_mm(tsk); |
| 1145 | if (mm) { |
| 1146 | mpol_rebind_mm(mm, &to); |
Paul Jackson | 2741a55 | 2006-03-31 02:30:51 -0800 | [diff] [blame] | 1147 | if (is_memory_migrate(cs)) |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 1148 | cpuset_migrate_mm(mm, &from, &to); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1149 | mmput(mm); |
| 1150 | } |
| 1151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /* The various types of files and directories in a cpuset file system */ |
| 1155 | |
| 1156 | typedef enum { |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1157 | FILE_MEMORY_MIGRATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | FILE_CPULIST, |
| 1159 | FILE_MEMLIST, |
| 1160 | FILE_CPU_EXCLUSIVE, |
| 1161 | FILE_MEM_EXCLUSIVE, |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1162 | FILE_SCHED_LOAD_BALANCE, |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1163 | FILE_MEMORY_PRESSURE_ENABLED, |
| 1164 | FILE_MEMORY_PRESSURE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1165 | FILE_SPREAD_PAGE, |
| 1166 | FILE_SPREAD_SLAB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } cpuset_filetype_t; |
| 1168 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1169 | static ssize_t cpuset_common_file_write(struct cgroup *cont, |
| 1170 | struct cftype *cft, |
| 1171 | struct file *file, |
Paul Menage | d3ed11c | 2006-12-06 20:41:37 -0800 | [diff] [blame] | 1172 | const char __user *userbuf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | size_t nbytes, loff_t *unused_ppos) |
| 1174 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1175 | struct cpuset *cs = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | cpuset_filetype_t type = cft->private; |
| 1177 | char *buffer; |
| 1178 | int retval = 0; |
| 1179 | |
| 1180 | /* Crude upper limit on largest legitimate cpulist user might write. */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1181 | if (nbytes > 100U + 6 * max(NR_CPUS, MAX_NUMNODES)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | return -E2BIG; |
| 1183 | |
| 1184 | /* +1 for nul-terminator */ |
| 1185 | if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0) |
| 1186 | return -ENOMEM; |
| 1187 | |
| 1188 | if (copy_from_user(buffer, userbuf, nbytes)) { |
| 1189 | retval = -EFAULT; |
| 1190 | goto out1; |
| 1191 | } |
| 1192 | buffer[nbytes] = 0; /* nul-terminate */ |
| 1193 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1194 | cgroup_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1196 | if (cgroup_is_removed(cont)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | retval = -ENODEV; |
| 1198 | goto out2; |
| 1199 | } |
| 1200 | |
| 1201 | switch (type) { |
| 1202 | case FILE_CPULIST: |
| 1203 | retval = update_cpumask(cs, buffer); |
| 1204 | break; |
| 1205 | case FILE_MEMLIST: |
| 1206 | retval = update_nodemask(cs, buffer); |
| 1207 | break; |
| 1208 | case FILE_CPU_EXCLUSIVE: |
| 1209 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer); |
| 1210 | break; |
| 1211 | case FILE_MEM_EXCLUSIVE: |
| 1212 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); |
| 1213 | break; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1214 | case FILE_SCHED_LOAD_BALANCE: |
| 1215 | retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, buffer); |
| 1216 | break; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1217 | case FILE_MEMORY_MIGRATE: |
| 1218 | retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer); |
| 1219 | break; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1220 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1221 | retval = update_memory_pressure_enabled(cs, buffer); |
| 1222 | break; |
| 1223 | case FILE_MEMORY_PRESSURE: |
| 1224 | retval = -EACCES; |
| 1225 | break; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1226 | case FILE_SPREAD_PAGE: |
| 1227 | retval = update_flag(CS_SPREAD_PAGE, cs, buffer); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1228 | cs->mems_generation = cpuset_mems_generation++; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1229 | break; |
| 1230 | case FILE_SPREAD_SLAB: |
| 1231 | retval = update_flag(CS_SPREAD_SLAB, cs, buffer); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1232 | cs->mems_generation = cpuset_mems_generation++; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1233 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | default: |
| 1235 | retval = -EINVAL; |
| 1236 | goto out2; |
| 1237 | } |
| 1238 | |
| 1239 | if (retval == 0) |
| 1240 | retval = nbytes; |
| 1241 | out2: |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1242 | cgroup_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | out1: |
| 1244 | kfree(buffer); |
| 1245 | return retval; |
| 1246 | } |
| 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | /* |
| 1249 | * These ascii lists should be read in a single call, by using a user |
| 1250 | * buffer large enough to hold the entire map. If read in smaller |
| 1251 | * chunks, there is no guarantee of atomicity. Since the display format |
| 1252 | * used, list of ranges of sequential numbers, is variable length, |
| 1253 | * and since these maps can change value dynamically, one could read |
| 1254 | * gibberish by doing partial reads while a list was changing. |
| 1255 | * A single large read to a buffer that crosses a page boundary is |
| 1256 | * ok, because the result being copied to user land is not recomputed |
| 1257 | * across a page fault. |
| 1258 | */ |
| 1259 | |
| 1260 | static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs) |
| 1261 | { |
| 1262 | cpumask_t mask; |
| 1263 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1264 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | mask = cs->cpus_allowed; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1266 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | return cpulist_scnprintf(page, PAGE_SIZE, mask); |
| 1269 | } |
| 1270 | |
| 1271 | static int cpuset_sprintf_memlist(char *page, struct cpuset *cs) |
| 1272 | { |
| 1273 | nodemask_t mask; |
| 1274 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1275 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | mask = cs->mems_allowed; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1277 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | return nodelist_scnprintf(page, PAGE_SIZE, mask); |
| 1280 | } |
| 1281 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1282 | static ssize_t cpuset_common_file_read(struct cgroup *cont, |
| 1283 | struct cftype *cft, |
| 1284 | struct file *file, |
| 1285 | char __user *buf, |
| 1286 | size_t nbytes, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1288 | struct cpuset *cs = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | cpuset_filetype_t type = cft->private; |
| 1290 | char *page; |
| 1291 | ssize_t retval = 0; |
| 1292 | char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1294 | if (!(page = (char *)__get_free_page(GFP_TEMPORARY))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | return -ENOMEM; |
| 1296 | |
| 1297 | s = page; |
| 1298 | |
| 1299 | switch (type) { |
| 1300 | case FILE_CPULIST: |
| 1301 | s += cpuset_sprintf_cpulist(s, cs); |
| 1302 | break; |
| 1303 | case FILE_MEMLIST: |
| 1304 | s += cpuset_sprintf_memlist(s, cs); |
| 1305 | break; |
| 1306 | case FILE_CPU_EXCLUSIVE: |
| 1307 | *s++ = is_cpu_exclusive(cs) ? '1' : '0'; |
| 1308 | break; |
| 1309 | case FILE_MEM_EXCLUSIVE: |
| 1310 | *s++ = is_mem_exclusive(cs) ? '1' : '0'; |
| 1311 | break; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1312 | case FILE_SCHED_LOAD_BALANCE: |
| 1313 | *s++ = is_sched_load_balance(cs) ? '1' : '0'; |
| 1314 | break; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1315 | case FILE_MEMORY_MIGRATE: |
| 1316 | *s++ = is_memory_migrate(cs) ? '1' : '0'; |
| 1317 | break; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1318 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1319 | *s++ = cpuset_memory_pressure_enabled ? '1' : '0'; |
| 1320 | break; |
| 1321 | case FILE_MEMORY_PRESSURE: |
| 1322 | s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter)); |
| 1323 | break; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1324 | case FILE_SPREAD_PAGE: |
| 1325 | *s++ = is_spread_page(cs) ? '1' : '0'; |
| 1326 | break; |
| 1327 | case FILE_SPREAD_SLAB: |
| 1328 | *s++ = is_spread_slab(cs) ? '1' : '0'; |
| 1329 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | default: |
| 1331 | retval = -EINVAL; |
| 1332 | goto out; |
| 1333 | } |
| 1334 | *s++ = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
Al Viro | eacaa1f | 2005-09-30 03:26:43 +0100 | [diff] [blame] | 1336 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | out: |
| 1338 | free_page((unsigned long)page); |
| 1339 | return retval; |
| 1340 | } |
| 1341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | /* |
| 1347 | * for the common functions, 'private' gives the type of file |
| 1348 | */ |
| 1349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | static struct cftype cft_cpus = { |
| 1351 | .name = "cpus", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1352 | .read = cpuset_common_file_read, |
| 1353 | .write = cpuset_common_file_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | .private = FILE_CPULIST, |
| 1355 | }; |
| 1356 | |
| 1357 | static struct cftype cft_mems = { |
| 1358 | .name = "mems", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1359 | .read = cpuset_common_file_read, |
| 1360 | .write = cpuset_common_file_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | .private = FILE_MEMLIST, |
| 1362 | }; |
| 1363 | |
| 1364 | static struct cftype cft_cpu_exclusive = { |
| 1365 | .name = "cpu_exclusive", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1366 | .read = cpuset_common_file_read, |
| 1367 | .write = cpuset_common_file_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | .private = FILE_CPU_EXCLUSIVE, |
| 1369 | }; |
| 1370 | |
| 1371 | static struct cftype cft_mem_exclusive = { |
| 1372 | .name = "mem_exclusive", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1373 | .read = cpuset_common_file_read, |
| 1374 | .write = cpuset_common_file_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | .private = FILE_MEM_EXCLUSIVE, |
| 1376 | }; |
| 1377 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1378 | static struct cftype cft_sched_load_balance = { |
| 1379 | .name = "sched_load_balance", |
| 1380 | .read = cpuset_common_file_read, |
| 1381 | .write = cpuset_common_file_write, |
| 1382 | .private = FILE_SCHED_LOAD_BALANCE, |
| 1383 | }; |
| 1384 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1385 | static struct cftype cft_memory_migrate = { |
| 1386 | .name = "memory_migrate", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1387 | .read = cpuset_common_file_read, |
| 1388 | .write = cpuset_common_file_write, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1389 | .private = FILE_MEMORY_MIGRATE, |
| 1390 | }; |
| 1391 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1392 | static struct cftype cft_memory_pressure_enabled = { |
| 1393 | .name = "memory_pressure_enabled", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1394 | .read = cpuset_common_file_read, |
| 1395 | .write = cpuset_common_file_write, |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1396 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
| 1397 | }; |
| 1398 | |
| 1399 | static struct cftype cft_memory_pressure = { |
| 1400 | .name = "memory_pressure", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1401 | .read = cpuset_common_file_read, |
| 1402 | .write = cpuset_common_file_write, |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1403 | .private = FILE_MEMORY_PRESSURE, |
| 1404 | }; |
| 1405 | |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1406 | static struct cftype cft_spread_page = { |
| 1407 | .name = "memory_spread_page", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1408 | .read = cpuset_common_file_read, |
| 1409 | .write = cpuset_common_file_write, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1410 | .private = FILE_SPREAD_PAGE, |
| 1411 | }; |
| 1412 | |
| 1413 | static struct cftype cft_spread_slab = { |
| 1414 | .name = "memory_spread_slab", |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1415 | .read = cpuset_common_file_read, |
| 1416 | .write = cpuset_common_file_write, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1417 | .private = FILE_SPREAD_SLAB, |
| 1418 | }; |
| 1419 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1420 | static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | { |
| 1422 | int err; |
| 1423 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1424 | if ((err = cgroup_add_file(cont, ss, &cft_cpus)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1426 | if ((err = cgroup_add_file(cont, ss, &cft_mems)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1428 | if ((err = cgroup_add_file(cont, ss, &cft_cpu_exclusive)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1430 | if ((err = cgroup_add_file(cont, ss, &cft_mem_exclusive)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1432 | if ((err = cgroup_add_file(cont, ss, &cft_memory_migrate)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | return err; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1434 | if ((err = cgroup_add_file(cont, ss, &cft_sched_load_balance)) < 0) |
| 1435 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1436 | if ((err = cgroup_add_file(cont, ss, &cft_memory_pressure)) < 0) |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1437 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1438 | if ((err = cgroup_add_file(cont, ss, &cft_spread_page)) < 0) |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1439 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1440 | if ((err = cgroup_add_file(cont, ss, &cft_spread_slab)) < 0) |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1441 | return err; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1442 | /* memory_pressure_enabled is in root cpuset only */ |
| 1443 | if (err == 0 && !cont->parent) |
| 1444 | err = cgroup_add_file(cont, ss, |
| 1445 | &cft_memory_pressure_enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1450 | * post_clone() is called at the end of cgroup_clone(). |
| 1451 | * 'cgroup' was just created automatically as a result of |
| 1452 | * a cgroup_clone(), and the current task is about to |
| 1453 | * be moved into 'cgroup'. |
| 1454 | * |
| 1455 | * Currently we refuse to set up the cgroup - thereby |
| 1456 | * refusing the task to be entered, and as a result refusing |
| 1457 | * the sys_unshare() or clone() which initiated it - if any |
| 1458 | * sibling cpusets have exclusive cpus or mem. |
| 1459 | * |
| 1460 | * If this becomes a problem for some users who wish to |
| 1461 | * allow that scenario, then cpuset_post_clone() could be |
| 1462 | * changed to grant parent->cpus_allowed-sibling_cpus_exclusive |
| 1463 | * (and likewise for mems) to the new cgroup. |
| 1464 | */ |
| 1465 | static void cpuset_post_clone(struct cgroup_subsys *ss, |
| 1466 | struct cgroup *cgroup) |
| 1467 | { |
| 1468 | struct cgroup *parent, *child; |
| 1469 | struct cpuset *cs, *parent_cs; |
| 1470 | |
| 1471 | parent = cgroup->parent; |
| 1472 | list_for_each_entry(child, &parent->children, sibling) { |
| 1473 | cs = cgroup_cs(child); |
| 1474 | if (is_mem_exclusive(cs) || is_cpu_exclusive(cs)) |
| 1475 | return; |
| 1476 | } |
| 1477 | cs = cgroup_cs(cgroup); |
| 1478 | parent_cs = cgroup_cs(parent); |
| 1479 | |
| 1480 | cs->mems_allowed = parent_cs->mems_allowed; |
| 1481 | cs->cpus_allowed = parent_cs->cpus_allowed; |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | * cpuset_create - create a cpuset |
| 1487 | * parent: cpuset that will be parent of the new cpuset. |
| 1488 | * name: name of the new cpuset. Will be strcpy'ed. |
| 1489 | * mode: mode to set on new inode |
| 1490 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1491 | * Must be called with the mutex on the parent inode held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | */ |
| 1493 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1494 | static struct cgroup_subsys_state *cpuset_create( |
| 1495 | struct cgroup_subsys *ss, |
| 1496 | struct cgroup *cont) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | { |
| 1498 | struct cpuset *cs; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1499 | struct cpuset *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1501 | if (!cont->parent) { |
| 1502 | /* This is early initialization for the top cgroup */ |
| 1503 | top_cpuset.mems_generation = cpuset_mems_generation++; |
| 1504 | return &top_cpuset.css; |
| 1505 | } |
| 1506 | parent = cgroup_cs(cont->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | cs = kmalloc(sizeof(*cs), GFP_KERNEL); |
| 1508 | if (!cs) |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1509 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1511 | cpuset_update_task_memory_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | cs->flags = 0; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1513 | if (is_spread_page(parent)) |
| 1514 | set_bit(CS_SPREAD_PAGE, &cs->flags); |
| 1515 | if (is_spread_slab(parent)) |
| 1516 | set_bit(CS_SPREAD_SLAB, &cs->flags); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1517 | set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | cs->cpus_allowed = CPU_MASK_NONE; |
| 1519 | cs->mems_allowed = NODE_MASK_NONE; |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1520 | cs->mems_generation = cpuset_mems_generation++; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1521 | fmeter_init(&cs->fmeter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
| 1523 | cs->parent = parent; |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1524 | number_of_cpusets++; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1525 | return &cs->css ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1528 | /* |
| 1529 | * Locking note on the strange update_flag() call below: |
| 1530 | * |
| 1531 | * If the cpuset being removed has its flag 'sched_load_balance' |
| 1532 | * enabled, then simulate turning sched_load_balance off, which |
| 1533 | * will call rebuild_sched_domains(). The lock_cpu_hotplug() |
| 1534 | * call in rebuild_sched_domains() must not be made while holding |
| 1535 | * callback_mutex. Elsewhere the kernel nests callback_mutex inside |
| 1536 | * lock_cpu_hotplug() calls. So the reverse nesting would risk an |
| 1537 | * ABBA deadlock. |
| 1538 | */ |
| 1539 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1540 | static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1542 | struct cpuset *cs = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1544 | cpuset_update_task_memory_state(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1545 | |
| 1546 | if (is_sched_load_balance(cs)) |
| 1547 | update_flag(CS_SCHED_LOAD_BALANCE, cs, "0"); |
| 1548 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1549 | number_of_cpusets--; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1550 | kfree(cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1553 | struct cgroup_subsys cpuset_subsys = { |
| 1554 | .name = "cpuset", |
| 1555 | .create = cpuset_create, |
| 1556 | .destroy = cpuset_destroy, |
| 1557 | .can_attach = cpuset_can_attach, |
| 1558 | .attach = cpuset_attach, |
| 1559 | .populate = cpuset_populate, |
| 1560 | .post_clone = cpuset_post_clone, |
| 1561 | .subsys_id = cpuset_subsys_id, |
| 1562 | .early_init = 1, |
| 1563 | }; |
| 1564 | |
Paul Jackson | c417f02 | 2006-01-08 01:02:01 -0800 | [diff] [blame] | 1565 | /* |
| 1566 | * cpuset_init_early - just enough so that the calls to |
| 1567 | * cpuset_update_task_memory_state() in early init code |
| 1568 | * are harmless. |
| 1569 | */ |
| 1570 | |
| 1571 | int __init cpuset_init_early(void) |
| 1572 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1573 | top_cpuset.mems_generation = cpuset_mems_generation++; |
Paul Jackson | c417f02 | 2006-01-08 01:02:01 -0800 | [diff] [blame] | 1574 | return 0; |
| 1575 | } |
| 1576 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | /** |
| 1579 | * cpuset_init - initialize cpusets at system boot |
| 1580 | * |
| 1581 | * Description: Initialize top_cpuset and the cpuset internal file system, |
| 1582 | **/ |
| 1583 | |
| 1584 | int __init cpuset_init(void) |
| 1585 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1586 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
| 1588 | top_cpuset.cpus_allowed = CPU_MASK_ALL; |
| 1589 | top_cpuset.mems_allowed = NODE_MASK_ALL; |
| 1590 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1591 | fmeter_init(&top_cpuset.fmeter); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1592 | top_cpuset.mems_generation = cpuset_mems_generation++; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1593 | set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | err = register_filesystem(&cpuset_fs_type); |
| 1596 | if (err < 0) |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1597 | return err; |
| 1598 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1599 | number_of_cpusets = 1; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1600 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1603 | /* |
| 1604 | * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs |
| 1605 | * or memory nodes, we need to walk over the cpuset hierarchy, |
| 1606 | * removing that CPU or node from all cpusets. If this removes the |
| 1607 | * last CPU or node from a cpuset, then the guarantee_online_cpus() |
| 1608 | * or guarantee_online_mems() code will use that emptied cpusets |
| 1609 | * parent online CPUs or nodes. Cpusets that were already empty of |
| 1610 | * CPUs or nodes are left empty. |
| 1611 | * |
| 1612 | * This routine is intentionally inefficient in a couple of regards. |
| 1613 | * It will check all cpusets in a subtree even if the top cpuset of |
| 1614 | * the subtree has no offline CPUs or nodes. It checks both CPUs and |
| 1615 | * nodes, even though the caller could have been coded to know that |
| 1616 | * only one of CPUs or nodes needed to be checked on a given call. |
| 1617 | * This was done to minimize text size rather than cpu cycles. |
| 1618 | * |
| 1619 | * Call with both manage_mutex and callback_mutex held. |
| 1620 | * |
| 1621 | * Recursive, on depth of cpuset subtree. |
| 1622 | */ |
| 1623 | |
| 1624 | static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur) |
| 1625 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1626 | struct cgroup *cont; |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1627 | struct cpuset *c; |
| 1628 | |
| 1629 | /* Each of our child cpusets mems must be online */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1630 | list_for_each_entry(cont, &cur->css.cgroup->children, sibling) { |
| 1631 | c = cgroup_cs(cont); |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1632 | guarantee_online_cpus_mems_in_subtree(c); |
| 1633 | if (!cpus_empty(c->cpus_allowed)) |
| 1634 | guarantee_online_cpus(c, &c->cpus_allowed); |
| 1635 | if (!nodes_empty(c->mems_allowed)) |
| 1636 | guarantee_online_mems(c, &c->mems_allowed); |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 1642 | * cpu_online_map and node_states[N_HIGH_MEMORY]. Force the top cpuset to |
| 1643 | * track what's online after any CPU or memory node hotplug or unplug |
| 1644 | * event. |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1645 | * |
| 1646 | * To ensure that we don't remove a CPU or node from the top cpuset |
| 1647 | * that is currently in use by a child cpuset (which would violate |
| 1648 | * the rule that cpusets must be subsets of their parent), we first |
| 1649 | * call the recursive routine guarantee_online_cpus_mems_in_subtree(). |
| 1650 | * |
| 1651 | * Since there are two callers of this routine, one for CPU hotplug |
| 1652 | * events and one for memory node hotplug events, we could have coded |
| 1653 | * two separate routines here. We code it as a single common routine |
| 1654 | * in order to minimize text size. |
| 1655 | */ |
| 1656 | |
| 1657 | static void common_cpu_mem_hotplug_unplug(void) |
| 1658 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1659 | cgroup_lock(); |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1660 | mutex_lock(&callback_mutex); |
| 1661 | |
| 1662 | guarantee_online_cpus_mems_in_subtree(&top_cpuset); |
| 1663 | top_cpuset.cpus_allowed = cpu_online_map; |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 1664 | top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY]; |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1665 | |
| 1666 | mutex_unlock(&callback_mutex); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1667 | cgroup_unlock(); |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1668 | } |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1669 | |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * The top_cpuset tracks what CPUs and Memory Nodes are online, |
| 1672 | * period. This is necessary in order to make cpusets transparent |
| 1673 | * (of no affect) on systems that are actively using CPU hotplug |
| 1674 | * but making no active use of cpusets. |
| 1675 | * |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 1676 | * This routine ensures that top_cpuset.cpus_allowed tracks |
| 1677 | * cpu_online_map on each CPU hotplug (cpuhp) event. |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 1678 | */ |
| 1679 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 1680 | static int cpuset_handle_cpuhp(struct notifier_block *unused_nb, |
| 1681 | unsigned long phase, void *unused_cpu) |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 1682 | { |
Avi Kivity | ac07675 | 2007-05-24 12:33:15 +0300 | [diff] [blame] | 1683 | if (phase == CPU_DYING || phase == CPU_DYING_FROZEN) |
| 1684 | return NOTIFY_DONE; |
| 1685 | |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1686 | common_cpu_mem_hotplug_unplug(); |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 1689 | |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1690 | #ifdef CONFIG_MEMORY_HOTPLUG |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 1691 | /* |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 1692 | * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY]. |
| 1693 | * Call this routine anytime after you change |
| 1694 | * node_states[N_HIGH_MEMORY]. |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 1695 | * See also the previous routine cpuset_handle_cpuhp(). |
| 1696 | */ |
| 1697 | |
Al Viro | 1af9892 | 2006-10-10 22:48:57 +0100 | [diff] [blame] | 1698 | void cpuset_track_online_nodes(void) |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 1699 | { |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 1700 | common_cpu_mem_hotplug_unplug(); |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 1701 | } |
| 1702 | #endif |
| 1703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | /** |
| 1705 | * cpuset_init_smp - initialize cpus_allowed |
| 1706 | * |
| 1707 | * Description: Finish top cpuset after cpu, node maps are initialized |
| 1708 | **/ |
| 1709 | |
| 1710 | void __init cpuset_init_smp(void) |
| 1711 | { |
| 1712 | top_cpuset.cpus_allowed = cpu_online_map; |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 1713 | top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY]; |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 1714 | |
| 1715 | hotcpu_notifier(cpuset_handle_cpuhp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
| 1721 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. |
| 1722 | * |
| 1723 | * Description: Returns the cpumask_t cpus_allowed of the cpuset |
| 1724 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 1725 | * subset of cpu_online_map, even if this means going outside the |
| 1726 | * tasks cpuset. |
| 1727 | **/ |
| 1728 | |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1729 | cpumask_t cpuset_cpus_allowed(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | { |
| 1731 | cpumask_t mask; |
| 1732 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1733 | mutex_lock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1734 | task_lock(tsk); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1735 | guarantee_online_cpus(task_cs(tsk), &mask); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1736 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1737 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
| 1739 | return mask; |
| 1740 | } |
| 1741 | |
| 1742 | void cpuset_init_current_mems_allowed(void) |
| 1743 | { |
| 1744 | current->mems_allowed = NODE_MASK_ALL; |
| 1745 | } |
| 1746 | |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 1747 | /** |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1748 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. |
| 1749 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. |
| 1750 | * |
| 1751 | * Description: Returns the nodemask_t mems_allowed of the cpuset |
| 1752 | * attached to the specified @tsk. Guaranteed to return some non-empty |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 1753 | * subset of node_states[N_HIGH_MEMORY], even if this means going outside the |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1754 | * tasks cpuset. |
| 1755 | **/ |
| 1756 | |
| 1757 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) |
| 1758 | { |
| 1759 | nodemask_t mask; |
| 1760 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1761 | mutex_lock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1762 | task_lock(tsk); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1763 | guarantee_online_mems(task_cs(tsk), &mask); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1764 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1765 | mutex_unlock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1766 | |
| 1767 | return mask; |
| 1768 | } |
| 1769 | |
| 1770 | /** |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 1771 | * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed |
| 1772 | * @zl: the zonelist to be checked |
| 1773 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | * Are any of the nodes on zonelist zl allowed in current->mems_allowed? |
| 1775 | */ |
| 1776 | int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl) |
| 1777 | { |
| 1778 | int i; |
| 1779 | |
| 1780 | for (i = 0; zl->zones[i]; i++) { |
Christoph Lameter | 89fa302 | 2006-09-25 23:31:55 -0700 | [diff] [blame] | 1781 | int nid = zone_to_nid(zl->zones[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
| 1783 | if (node_isset(nid, current->mems_allowed)) |
| 1784 | return 1; |
| 1785 | } |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1789 | /* |
| 1790 | * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1791 | * ancestor to the specified cpuset. Call holding callback_mutex. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1792 | * If no ancestor is mem_exclusive (an unusual configuration), then |
| 1793 | * returns the root cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1795 | static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | { |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1797 | while (!is_mem_exclusive(cs) && cs->parent) |
| 1798 | cs = cs->parent; |
| 1799 | return cs; |
| 1800 | } |
| 1801 | |
| 1802 | /** |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1803 | * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node? |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1804 | * @z: is this zone on an allowed node? |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1805 | * @gfp_mask: memory allocation flags |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1806 | * |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1807 | * If we're in interrupt, yes, we can always allocate. If |
| 1808 | * __GFP_THISNODE is set, yes, we can always allocate. If zone |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1809 | * z's node is in our tasks mems_allowed, yes. If it's not a |
| 1810 | * __GFP_HARDWALL request and this zone's nodes is in the nearest |
| 1811 | * mem_exclusive cpuset ancestor to this tasks cpuset, yes. |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 1812 | * If the task has been OOM killed and has access to memory reserves |
| 1813 | * as specified by the TIF_MEMDIE flag, yes. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1814 | * Otherwise, no. |
| 1815 | * |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1816 | * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall() |
| 1817 | * reduces to cpuset_zone_allowed_hardwall(). Otherwise, |
| 1818 | * cpuset_zone_allowed_softwall() might sleep, and might allow a zone |
| 1819 | * from an enclosing cpuset. |
| 1820 | * |
| 1821 | * cpuset_zone_allowed_hardwall() only handles the simpler case of |
| 1822 | * hardwall cpusets, and never sleeps. |
| 1823 | * |
| 1824 | * The __GFP_THISNODE placement logic is really handled elsewhere, |
| 1825 | * by forcibly using a zonelist starting at a specified node, and by |
| 1826 | * (in get_page_from_freelist()) refusing to consider the zones for |
| 1827 | * any node on the zonelist except the first. By the time any such |
| 1828 | * calls get to this routine, we should just shut up and say 'yes'. |
| 1829 | * |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1830 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 1831 | * and do not allow allocations outside the current tasks cpuset |
| 1832 | * unless the task has been OOM killed as is marked TIF_MEMDIE. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1833 | * GFP_KERNEL allocations are not so marked, so can escape to the |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1834 | * nearest enclosing mem_exclusive ancestor cpuset. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1835 | * |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1836 | * Scanning up parent cpusets requires callback_mutex. The |
| 1837 | * __alloc_pages() routine only calls here with __GFP_HARDWALL bit |
| 1838 | * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the |
| 1839 | * current tasks mems_allowed came up empty on the first pass over |
| 1840 | * the zonelist. So only GFP_KERNEL allocations, if all nodes in the |
| 1841 | * cpuset are short of memory, might require taking the callback_mutex |
| 1842 | * mutex. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1843 | * |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 1844 | * The first call here from mm/page_alloc:get_page_from_freelist() |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1845 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, |
| 1846 | * so no allocation on a node outside the cpuset is allowed (unless |
| 1847 | * in interrupt, of course). |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1848 | * |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 1849 | * The second pass through get_page_from_freelist() doesn't even call |
| 1850 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() |
| 1851 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set |
| 1852 | * in alloc_flags. That logic and the checks below have the combined |
| 1853 | * affect that: |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1854 | * in_interrupt - any node ok (current task context irrelevant) |
| 1855 | * GFP_ATOMIC - any node ok |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 1856 | * TIF_MEMDIE - any node ok |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1857 | * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok |
| 1858 | * GFP_USER - only nodes in current tasks mems allowed ok. |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 1859 | * |
| 1860 | * Rule: |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1861 | * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 1862 | * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables |
| 1863 | * the code that might scan up ancestor cpusets and sleep. |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1864 | */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1865 | |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1866 | int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1867 | { |
| 1868 | int node; /* node that zone z is on */ |
| 1869 | const struct cpuset *cs; /* current cpuset ancestors */ |
Paul Jackson | 29afd49 | 2006-03-24 03:16:12 -0800 | [diff] [blame] | 1870 | int allowed; /* is allocation in zone z allowed? */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1871 | |
Christoph Lameter | 9b819d2 | 2006-09-25 23:31:40 -0700 | [diff] [blame] | 1872 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1873 | return 1; |
Christoph Lameter | 89fa302 | 2006-09-25 23:31:55 -0700 | [diff] [blame] | 1874 | node = zone_to_nid(z); |
Paul Jackson | 92d1dbd | 2006-05-20 15:00:11 -0700 | [diff] [blame] | 1875 | might_sleep_if(!(gfp_mask & __GFP_HARDWALL)); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1876 | if (node_isset(node, current->mems_allowed)) |
| 1877 | return 1; |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 1878 | /* |
| 1879 | * Allow tasks that have access to memory reserves because they have |
| 1880 | * been OOM killed to get memory anywhere. |
| 1881 | */ |
| 1882 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 1883 | return 1; |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1884 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
| 1885 | return 0; |
| 1886 | |
Bob Picco | 5563e77 | 2005-11-13 16:06:35 -0800 | [diff] [blame] | 1887 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 1888 | return 1; |
| 1889 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1890 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1891 | mutex_lock(&callback_mutex); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1892 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1893 | task_lock(current); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1894 | cs = nearest_exclusive_ancestor(task_cs(current)); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1895 | task_unlock(current); |
| 1896 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1897 | allowed = node_isset(node, cs->mems_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1898 | mutex_unlock(&callback_mutex); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 1899 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1902 | /* |
| 1903 | * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node? |
| 1904 | * @z: is this zone on an allowed node? |
| 1905 | * @gfp_mask: memory allocation flags |
| 1906 | * |
| 1907 | * If we're in interrupt, yes, we can always allocate. |
| 1908 | * If __GFP_THISNODE is set, yes, we can always allocate. If zone |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 1909 | * z's node is in our tasks mems_allowed, yes. If the task has been |
| 1910 | * OOM killed and has access to memory reserves as specified by the |
| 1911 | * TIF_MEMDIE flag, yes. Otherwise, no. |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1912 | * |
| 1913 | * The __GFP_THISNODE placement logic is really handled elsewhere, |
| 1914 | * by forcibly using a zonelist starting at a specified node, and by |
| 1915 | * (in get_page_from_freelist()) refusing to consider the zones for |
| 1916 | * any node on the zonelist except the first. By the time any such |
| 1917 | * calls get to this routine, we should just shut up and say 'yes'. |
| 1918 | * |
| 1919 | * Unlike the cpuset_zone_allowed_softwall() variant, above, |
| 1920 | * this variant requires that the zone be in the current tasks |
| 1921 | * mems_allowed or that we're in interrupt. It does not scan up the |
| 1922 | * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset. |
| 1923 | * It never sleeps. |
| 1924 | */ |
| 1925 | |
| 1926 | int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask) |
| 1927 | { |
| 1928 | int node; /* node that zone z is on */ |
| 1929 | |
| 1930 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
| 1931 | return 1; |
| 1932 | node = zone_to_nid(z); |
| 1933 | if (node_isset(node, current->mems_allowed)) |
| 1934 | return 1; |
Daniel Walker | dedf8b7 | 2007-10-18 03:06:04 -0700 | [diff] [blame] | 1935 | /* |
| 1936 | * Allow tasks that have access to memory reserves because they have |
| 1937 | * been OOM killed to get memory anywhere. |
| 1938 | */ |
| 1939 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 1940 | return 1; |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 1941 | return 0; |
| 1942 | } |
| 1943 | |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 1944 | /** |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 1945 | * cpuset_lock - lock out any changes to cpuset structures |
| 1946 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1947 | * The out of memory (oom) code needs to mutex_lock cpusets |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 1948 | * from being changed while it scans the tasklist looking for a |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1949 | * task in an overlapping cpuset. Expose callback_mutex via this |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 1950 | * cpuset_lock() routine, so the oom code can lock it, before |
| 1951 | * locking the task list. The tasklist_lock is a spinlock, so |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1952 | * must be taken inside callback_mutex. |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 1953 | */ |
| 1954 | |
| 1955 | void cpuset_lock(void) |
| 1956 | { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1957 | mutex_lock(&callback_mutex); |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * cpuset_unlock - release lock on cpuset changes |
| 1962 | * |
| 1963 | * Undo the lock taken in a previous cpuset_lock() call. |
| 1964 | */ |
| 1965 | |
| 1966 | void cpuset_unlock(void) |
| 1967 | { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1968 | mutex_unlock(&callback_mutex); |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /** |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1972 | * cpuset_mem_spread_node() - On which node to begin search for a page |
| 1973 | * |
| 1974 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for |
| 1975 | * tasks in a cpuset with is_spread_page or is_spread_slab set), |
| 1976 | * and if the memory allocation used cpuset_mem_spread_node() |
| 1977 | * to determine on which node to start looking, as it will for |
| 1978 | * certain page cache or slab cache pages such as used for file |
| 1979 | * system buffers and inode caches, then instead of starting on the |
| 1980 | * local node to look for a free page, rather spread the starting |
| 1981 | * node around the tasks mems_allowed nodes. |
| 1982 | * |
| 1983 | * We don't have to worry about the returned node being offline |
| 1984 | * because "it can't happen", and even if it did, it would be ok. |
| 1985 | * |
| 1986 | * The routines calling guarantee_online_mems() are careful to |
| 1987 | * only set nodes in task->mems_allowed that are online. So it |
| 1988 | * should not be possible for the following code to return an |
| 1989 | * offline node. But if it did, that would be ok, as this routine |
| 1990 | * is not returning the node where the allocation must be, only |
| 1991 | * the node where the search should start. The zonelist passed to |
| 1992 | * __alloc_pages() will include all nodes. If the slab allocator |
| 1993 | * is passed an offline node, it will fall back to the local node. |
| 1994 | * See kmem_cache_alloc_node(). |
| 1995 | */ |
| 1996 | |
| 1997 | int cpuset_mem_spread_node(void) |
| 1998 | { |
| 1999 | int node; |
| 2000 | |
| 2001 | node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed); |
| 2002 | if (node == MAX_NUMNODES) |
| 2003 | node = first_node(current->mems_allowed); |
| 2004 | current->cpuset_mem_spread_rotor = node; |
| 2005 | return node; |
| 2006 | } |
| 2007 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); |
| 2008 | |
| 2009 | /** |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2010 | * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? |
| 2011 | * @tsk1: pointer to task_struct of some task. |
| 2012 | * @tsk2: pointer to task_struct of some other task. |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2013 | * |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2014 | * Description: Return true if @tsk1's mems_allowed intersects the |
| 2015 | * mems_allowed of @tsk2. Used by the OOM killer to determine if |
| 2016 | * one of the task's memory usage might impact the memory available |
| 2017 | * to the other. |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2018 | **/ |
| 2019 | |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2020 | int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, |
| 2021 | const struct task_struct *tsk2) |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2022 | { |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2023 | return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2027 | * Collection of memory_pressure is suppressed unless |
| 2028 | * this flag is enabled by writing "1" to the special |
| 2029 | * cpuset file 'memory_pressure_enabled' in the root cpuset. |
| 2030 | */ |
| 2031 | |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 2032 | int cpuset_memory_pressure_enabled __read_mostly; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2033 | |
| 2034 | /** |
| 2035 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. |
| 2036 | * |
| 2037 | * Keep a running average of the rate of synchronous (direct) |
| 2038 | * page reclaim efforts initiated by tasks in each cpuset. |
| 2039 | * |
| 2040 | * This represents the rate at which some task in the cpuset |
| 2041 | * ran low on memory on all nodes it was allowed to use, and |
| 2042 | * had to enter the kernels page reclaim code in an effort to |
| 2043 | * create more free memory by tossing clean pages or swapping |
| 2044 | * or writing dirty pages. |
| 2045 | * |
| 2046 | * Display to user space in the per-cpuset read-only file |
| 2047 | * "memory_pressure". Value displayed is an integer |
| 2048 | * representing the recent rate of entry into the synchronous |
| 2049 | * (direct) page reclaim by any task attached to the cpuset. |
| 2050 | **/ |
| 2051 | |
| 2052 | void __cpuset_memory_pressure_bump(void) |
| 2053 | { |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2054 | task_lock(current); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2055 | fmeter_markevent(&task_cs(current)->fmeter); |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2056 | task_unlock(current); |
| 2057 | } |
| 2058 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2059 | #ifdef CONFIG_PROC_PID_CPUSET |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2060 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | * proc_cpuset_show() |
| 2062 | * - Print tasks cpuset path into seq_file. |
| 2063 | * - Used for /proc/<pid>/cpuset. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2064 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
| 2065 | * doesn't really matter if tsk->cpuset changes after we read it, |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2066 | * and we take manage_mutex, keeping attach_task() from changing it |
Paul Jackson | 8488bc3 | 2006-03-24 03:16:10 -0800 | [diff] [blame] | 2067 | * anyway. No need to check that tsk->cpuset != NULL, thanks to |
| 2068 | * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks |
| 2069 | * cpuset to top_cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame^] | 2071 | static int proc_cpuset_show(struct seq_file *m, void *unused_v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | { |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 2073 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | struct task_struct *tsk; |
| 2075 | char *buf; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2076 | struct cgroup_subsys_state *css; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2077 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2079 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2081 | if (!buf) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2082 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2084 | retval = -ESRCH; |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 2085 | pid = m->private; |
| 2086 | tsk = get_pid_task(pid, PIDTYPE_PID); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2087 | if (!tsk) |
| 2088 | goto out_free; |
| 2089 | |
| 2090 | retval = -EINVAL; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2091 | cgroup_lock(); |
| 2092 | css = task_subsys_state(tsk, cpuset_subsys_id); |
| 2093 | retval = cgroup_path(css->cgroup, buf, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | if (retval < 0) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2095 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | seq_puts(m, buf); |
| 2097 | seq_putc(m, '\n'); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2098 | out_unlock: |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2099 | cgroup_unlock(); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2100 | put_task_struct(tsk); |
| 2101 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | kfree(buf); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2103 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | return retval; |
| 2105 | } |
| 2106 | |
| 2107 | static int cpuset_open(struct inode *inode, struct file *file) |
| 2108 | { |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 2109 | struct pid *pid = PROC_I(inode)->pid; |
| 2110 | return single_open(file, proc_cpuset_show, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2113 | const struct file_operations proc_cpuset_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | .open = cpuset_open, |
| 2115 | .read = seq_read, |
| 2116 | .llseek = seq_lseek, |
| 2117 | .release = single_release, |
| 2118 | }; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2119 | #endif /* CONFIG_PROC_PID_CPUSET */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
| 2121 | /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ |
| 2122 | char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) |
| 2123 | { |
| 2124 | buffer += sprintf(buffer, "Cpus_allowed:\t"); |
| 2125 | buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed); |
| 2126 | buffer += sprintf(buffer, "\n"); |
| 2127 | buffer += sprintf(buffer, "Mems_allowed:\t"); |
| 2128 | buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed); |
| 2129 | buffer += sprintf(buffer, "\n"); |
| 2130 | return buffer; |
| 2131 | } |