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 | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 7 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * Portions derived from Patrick Mochel's sysfs code. |
| 10 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 12 | * 2003-10-10 Written by Simon Derr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * 2003-10-22 Updates by Stephen Hemminger. |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 14 | * 2004 May-July Rework by Paul Jackson. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
| 16 | * This file is subject to the terms and conditions of the GNU General Public |
| 17 | * License. See the file COPYING in the main directory of the Linux |
| 18 | * distribution for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/config.h> |
| 22 | #include <linux/cpu.h> |
| 23 | #include <linux/cpumask.h> |
| 24 | #include <linux/cpuset.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/file.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/kmod.h> |
| 33 | #include <linux/list.h> |
Paul Jackson | 68860ec | 2005-10-30 15:02:36 -0800 | [diff] [blame] | 34 | #include <linux/mempolicy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/mm.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/mount.h> |
| 38 | #include <linux/namei.h> |
| 39 | #include <linux/pagemap.h> |
| 40 | #include <linux/proc_fs.h> |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/sched.h> |
| 43 | #include <linux/seq_file.h> |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame^] | 44 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/slab.h> |
| 46 | #include <linux/smp_lock.h> |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 58 | #define CPUSET_SUPER_MAGIC 0x27e0eb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 60 | /* |
| 61 | * Tracks how many cpusets are currently defined in system. |
| 62 | * When there is only one cpuset (the root cpuset) we can |
| 63 | * short circuit some hooks. |
| 64 | */ |
Paul Jackson | 7edc596 | 2006-01-08 01:02:03 -0800 | [diff] [blame] | 65 | int number_of_cpusets __read_mostly; |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 66 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 67 | /* See "Frequency meter" comments, below. */ |
| 68 | |
| 69 | struct fmeter { |
| 70 | int cnt; /* unprocessed events count */ |
| 71 | int val; /* most recent output value */ |
| 72 | time_t time; /* clock (secs) when val computed */ |
| 73 | spinlock_t lock; /* guards read or write of above */ |
| 74 | }; |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | struct cpuset { |
| 77 | unsigned long flags; /* "unsigned long" so bitops work */ |
| 78 | cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ |
| 79 | nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ |
| 80 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Count is atomic so can incr (fork) or decr (exit) without a lock. |
| 83 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | atomic_t count; /* count tasks using this cpuset */ |
| 85 | |
| 86 | /* |
| 87 | * We link our 'sibling' struct into our parents 'children'. |
| 88 | * Our children link their 'sibling' into our 'children'. |
| 89 | */ |
| 90 | struct list_head sibling; /* my parents children */ |
| 91 | struct list_head children; /* my children */ |
| 92 | |
| 93 | struct cpuset *parent; /* my parent */ |
| 94 | struct dentry *dentry; /* cpuset fs entry */ |
| 95 | |
| 96 | /* |
| 97 | * Copy of global cpuset_mems_generation as of the most |
| 98 | * recent time this cpuset changed its mems_allowed. |
| 99 | */ |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 100 | int mems_generation; |
| 101 | |
| 102 | struct fmeter fmeter; /* memory_pressure filter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* bits in struct cpuset flags field */ |
| 106 | typedef enum { |
| 107 | CS_CPU_EXCLUSIVE, |
| 108 | CS_MEM_EXCLUSIVE, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 109 | CS_MEMORY_MIGRATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | CS_REMOVED, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 111 | CS_NOTIFY_ON_RELEASE, |
| 112 | CS_SPREAD_PAGE, |
| 113 | CS_SPREAD_SLAB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } cpuset_flagbits_t; |
| 115 | |
| 116 | /* convenient tests for these bits */ |
| 117 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
| 118 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 119 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static inline int is_mem_exclusive(const struct cpuset *cs) |
| 123 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 124 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline int is_removed(const struct cpuset *cs) |
| 128 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 129 | return test_bit(CS_REMOVED, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static inline int notify_on_release(const struct cpuset *cs) |
| 133 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 134 | return test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 137 | static inline int is_memory_migrate(const struct cpuset *cs) |
| 138 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 139 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 142 | static inline int is_spread_page(const struct cpuset *cs) |
| 143 | { |
| 144 | return test_bit(CS_SPREAD_PAGE, &cs->flags); |
| 145 | } |
| 146 | |
| 147 | static inline int is_spread_slab(const struct cpuset *cs) |
| 148 | { |
| 149 | return test_bit(CS_SPREAD_SLAB, &cs->flags); |
| 150 | } |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 153 | * Increment this integer everytime any cpuset changes its |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * mems_allowed value. Users of cpusets can track this generation |
| 155 | * number, and avoid having to lock and reload mems_allowed unless |
| 156 | * the cpuset they're using changes generation. |
| 157 | * |
| 158 | * A single, global generation is needed because attach_task() could |
| 159 | * reattach a task to a different cpuset, which must not have its |
| 160 | * generation numbers aliased with those of that tasks previous cpuset. |
| 161 | * |
| 162 | * Generations are needed for mems_allowed because one task cannot |
| 163 | * modify anothers memory placement. So we must enable every task, |
| 164 | * on every visit to __alloc_pages(), to efficiently check whether |
| 165 | * its current->cpuset->mems_allowed has changed, requiring an update |
| 166 | * of its current->mems_allowed. |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 167 | * |
| 168 | * Since cpuset_mems_generation is guarded by manage_mutex, |
| 169 | * there is no need to mark it atomic. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | */ |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 171 | static int cpuset_mems_generation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | static struct cpuset top_cpuset = { |
| 174 | .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)), |
| 175 | .cpus_allowed = CPU_MASK_ALL, |
| 176 | .mems_allowed = NODE_MASK_ALL, |
| 177 | .count = ATOMIC_INIT(0), |
| 178 | .sibling = LIST_HEAD_INIT(top_cpuset.sibling), |
| 179 | .children = LIST_HEAD_INIT(top_cpuset.children), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static struct vfsmount *cpuset_mount; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 183 | static struct super_block *cpuset_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 186 | * We have two global cpuset mutexes below. They can nest. |
| 187 | * 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] | 188 | * require taking task_lock() when dereferencing a tasks cpuset pointer. |
| 189 | * See "The task_lock() exception", at the end of this comment. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 191 | * A task must hold both mutexes to modify cpusets. If a task |
| 192 | * holds manage_mutex, then it blocks others wanting that mutex, |
| 193 | * 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] | 194 | * and be able to modify cpusets. It can perform various checks on |
| 195 | * the cpuset structure first, knowing nothing will change. It can |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 196 | * also allocate memory while just holding manage_mutex. While it is |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 197 | * performing these checks, various callback routines can briefly |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 198 | * acquire callback_mutex to query cpusets. Once it is ready to make |
| 199 | * the changes, it takes callback_mutex, blocking everyone else. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 201 | * Calls to the kernel memory allocator can not be made while holding |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 202 | * callback_mutex, as that would risk double tripping on callback_mutex |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 203 | * from one of the callbacks into the cpuset code from within |
| 204 | * __alloc_pages(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 206 | * 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] | 207 | * access to cpusets. |
| 208 | * |
| 209 | * The task_struct fields mems_allowed and mems_generation may only |
| 210 | * be accessed in the context of that task, so require no locks. |
| 211 | * |
| 212 | * Any task can increment and decrement the count field without lock. |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 213 | * 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] | 214 | * on the count field not changing. However, if the count goes to |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 215 | * zero, then only attach_task(), which holds both mutexes, can |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 216 | * increment it again. Because a count of zero means that no tasks |
| 217 | * are currently attached, therefore there is no way a task attached |
| 218 | * to that cpuset can fork (the other way to increment the count). |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 219 | * So code holding manage_mutex or callback_mutex can safely assume that |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 220 | * 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] | 221 | * 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] | 222 | * knows that the cpuset won't be removed, as cpuset_rmdir() needs |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 223 | * both of those mutexes. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 224 | * |
| 225 | * The cpuset_common_file_write handler for operations that modify |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 226 | * the cpuset hierarchy holds manage_mutex across the entire operation, |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 227 | * single threading all such cpuset modifications across the system. |
| 228 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 229 | * The cpuset_common_file_read() handlers only hold callback_mutex across |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 230 | * small pieces of code, such as when reading out possibly multi-word |
| 231 | * cpumasks and nodemasks. |
| 232 | * |
| 233 | * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 234 | * (usually) take either mutex. These are the two most performance |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 235 | * critical pieces of code here. The exception occurs on cpuset_exit(), |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 236 | * 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] | 237 | * 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] | 238 | * to /sbin/cpuset_release_agent with the name of the cpuset (path |
| 239 | * relative to the root of cpuset file system) as the argument. |
| 240 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 241 | * A cpuset can only be deleted if both its 'count' of using tasks |
| 242 | * is zero, and its list of 'children' cpusets is empty. Since all |
| 243 | * tasks in the system use _some_ cpuset, and since there is always at |
| 244 | * least one task in the system (init, pid == 1), therefore, top_cpuset |
| 245 | * always has either children cpusets and/or using tasks. So we don't |
| 246 | * need a special hack to ensure that top_cpuset cannot be deleted. |
| 247 | * |
| 248 | * The above "Tale of Two Semaphores" would be complete, but for: |
| 249 | * |
| 250 | * The task_lock() exception |
| 251 | * |
| 252 | * The need for this exception arises from the action of attach_task(), |
| 253 | * which overwrites one tasks cpuset pointer with another. It does |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 254 | * so using both mutexes, however there are several performance |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 255 | * critical places that need to reference task->cpuset without the |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 256 | * expense of grabbing a system global mutex. Therefore except as |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 257 | * noted below, when dereferencing or, as in attach_task(), modifying |
| 258 | * a tasks cpuset pointer we use task_lock(), which acts on a spinlock |
| 259 | * (task->alloc_lock) already in the task_struct routinely used for |
| 260 | * such matters. |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 261 | * |
| 262 | * P.S. One more locking exception. RCU is used to guard the |
| 263 | * update of a tasks cpuset pointer by attach_task() and the |
| 264 | * access of task->cpuset->mems_generation via that pointer in |
| 265 | * the routine cpuset_update_task_memory_state(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | */ |
| 267 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 268 | static DEFINE_MUTEX(manage_mutex); |
| 269 | static DEFINE_MUTEX(callback_mutex); |
Paul Jackson | 4247bdc | 2005-09-10 00:26:06 -0700 | [diff] [blame] | 270 | |
| 271 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | * A couple of forward declarations required, due to cyclic reference loop: |
| 273 | * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file |
| 274 | * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir. |
| 275 | */ |
| 276 | |
| 277 | static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode); |
| 278 | static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry); |
| 279 | |
| 280 | static struct backing_dev_info cpuset_backing_dev_info = { |
| 281 | .ra_pages = 0, /* No readahead */ |
| 282 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, |
| 283 | }; |
| 284 | |
| 285 | static struct inode *cpuset_new_inode(mode_t mode) |
| 286 | { |
| 287 | struct inode *inode = new_inode(cpuset_sb); |
| 288 | |
| 289 | if (inode) { |
| 290 | inode->i_mode = mode; |
| 291 | inode->i_uid = current->fsuid; |
| 292 | inode->i_gid = current->fsgid; |
| 293 | inode->i_blksize = PAGE_CACHE_SIZE; |
| 294 | inode->i_blocks = 0; |
| 295 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 296 | inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info; |
| 297 | } |
| 298 | return inode; |
| 299 | } |
| 300 | |
| 301 | static void cpuset_diput(struct dentry *dentry, struct inode *inode) |
| 302 | { |
| 303 | /* is dentry a directory ? if so, kfree() associated cpuset */ |
| 304 | if (S_ISDIR(inode->i_mode)) { |
| 305 | struct cpuset *cs = dentry->d_fsdata; |
| 306 | BUG_ON(!(is_removed(cs))); |
| 307 | kfree(cs); |
| 308 | } |
| 309 | iput(inode); |
| 310 | } |
| 311 | |
| 312 | static struct dentry_operations cpuset_dops = { |
| 313 | .d_iput = cpuset_diput, |
| 314 | }; |
| 315 | |
| 316 | static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name) |
| 317 | { |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 318 | struct dentry *d = lookup_one_len(name, parent, strlen(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (!IS_ERR(d)) |
| 320 | d->d_op = &cpuset_dops; |
| 321 | return d; |
| 322 | } |
| 323 | |
| 324 | static void remove_dir(struct dentry *d) |
| 325 | { |
| 326 | struct dentry *parent = dget(d->d_parent); |
| 327 | |
| 328 | d_delete(d); |
| 329 | simple_rmdir(parent->d_inode, d); |
| 330 | dput(parent); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * NOTE : the dentry must have been dget()'ed |
| 335 | */ |
| 336 | static void cpuset_d_remove_dir(struct dentry *dentry) |
| 337 | { |
| 338 | struct list_head *node; |
| 339 | |
| 340 | spin_lock(&dcache_lock); |
| 341 | node = dentry->d_subdirs.next; |
| 342 | while (node != &dentry->d_subdirs) { |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 343 | struct dentry *d = list_entry(node, struct dentry, d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | list_del_init(node); |
| 345 | if (d->d_inode) { |
| 346 | d = dget_locked(d); |
| 347 | spin_unlock(&dcache_lock); |
| 348 | d_delete(d); |
| 349 | simple_unlink(dentry->d_inode, d); |
| 350 | dput(d); |
| 351 | spin_lock(&dcache_lock); |
| 352 | } |
| 353 | node = dentry->d_subdirs.next; |
| 354 | } |
Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 355 | list_del_init(&dentry->d_u.d_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | spin_unlock(&dcache_lock); |
| 357 | remove_dir(dentry); |
| 358 | } |
| 359 | |
| 360 | static struct super_operations cpuset_ops = { |
| 361 | .statfs = simple_statfs, |
| 362 | .drop_inode = generic_delete_inode, |
| 363 | }; |
| 364 | |
| 365 | static int cpuset_fill_super(struct super_block *sb, void *unused_data, |
| 366 | int unused_silent) |
| 367 | { |
| 368 | struct inode *inode; |
| 369 | struct dentry *root; |
| 370 | |
| 371 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 372 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 373 | sb->s_magic = CPUSET_SUPER_MAGIC; |
| 374 | sb->s_op = &cpuset_ops; |
| 375 | cpuset_sb = sb; |
| 376 | |
| 377 | inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR); |
| 378 | if (inode) { |
| 379 | inode->i_op = &simple_dir_inode_operations; |
| 380 | inode->i_fop = &simple_dir_operations; |
| 381 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 382 | inode->i_nlink++; |
| 383 | } else { |
| 384 | return -ENOMEM; |
| 385 | } |
| 386 | |
| 387 | root = d_alloc_root(inode); |
| 388 | if (!root) { |
| 389 | iput(inode); |
| 390 | return -ENOMEM; |
| 391 | } |
| 392 | sb->s_root = root; |
| 393 | return 0; |
| 394 | } |
| 395 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 396 | static int cpuset_get_sb(struct file_system_type *fs_type, |
| 397 | int flags, const char *unused_dev_name, |
| 398 | void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 400 | return get_sb_single(fs_type, flags, data, cpuset_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static struct file_system_type cpuset_fs_type = { |
| 404 | .name = "cpuset", |
| 405 | .get_sb = cpuset_get_sb, |
| 406 | .kill_sb = kill_litter_super, |
| 407 | }; |
| 408 | |
| 409 | /* struct cftype: |
| 410 | * |
| 411 | * The files in the cpuset filesystem mostly have a very simple read/write |
| 412 | * handling, some common function will take care of it. Nevertheless some cases |
| 413 | * (read tasks) are special and therefore I define this structure for every |
| 414 | * kind of file. |
| 415 | * |
| 416 | * |
| 417 | * When reading/writing to a file: |
| 418 | * - the cpuset to use in file->f_dentry->d_parent->d_fsdata |
| 419 | * - the 'cftype' of the file is file->f_dentry->d_fsdata |
| 420 | */ |
| 421 | |
| 422 | struct cftype { |
| 423 | char *name; |
| 424 | int private; |
| 425 | int (*open) (struct inode *inode, struct file *file); |
| 426 | ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes, |
| 427 | loff_t *ppos); |
| 428 | int (*write) (struct file *file, const char __user *buf, size_t nbytes, |
| 429 | loff_t *ppos); |
| 430 | int (*release) (struct inode *inode, struct file *file); |
| 431 | }; |
| 432 | |
| 433 | static inline struct cpuset *__d_cs(struct dentry *dentry) |
| 434 | { |
| 435 | return dentry->d_fsdata; |
| 436 | } |
| 437 | |
| 438 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 439 | { |
| 440 | return dentry->d_fsdata; |
| 441 | } |
| 442 | |
| 443 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 444 | * Call with manage_mutex held. Writes path of cpuset into buf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | * Returns 0 on success, -errno on error. |
| 446 | */ |
| 447 | |
| 448 | static int cpuset_path(const struct cpuset *cs, char *buf, int buflen) |
| 449 | { |
| 450 | char *start; |
| 451 | |
| 452 | start = buf + buflen; |
| 453 | |
| 454 | *--start = '\0'; |
| 455 | for (;;) { |
| 456 | int len = cs->dentry->d_name.len; |
| 457 | if ((start -= len) < buf) |
| 458 | return -ENAMETOOLONG; |
| 459 | memcpy(start, cs->dentry->d_name.name, len); |
| 460 | cs = cs->parent; |
| 461 | if (!cs) |
| 462 | break; |
| 463 | if (!cs->parent) |
| 464 | continue; |
| 465 | if (--start < buf) |
| 466 | return -ENAMETOOLONG; |
| 467 | *start = '/'; |
| 468 | } |
| 469 | memmove(buf, start, buf + buflen - start); |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Notify userspace when a cpuset is released, by running |
| 475 | * /sbin/cpuset_release_agent with the name of the cpuset (path |
| 476 | * relative to the root of cpuset file system) as the argument. |
| 477 | * |
| 478 | * Most likely, this user command will try to rmdir this cpuset. |
| 479 | * |
| 480 | * This races with the possibility that some other task will be |
| 481 | * attached to this cpuset before it is removed, or that some other |
| 482 | * user task will 'mkdir' a child cpuset of this cpuset. That's ok. |
| 483 | * The presumed 'rmdir' will fail quietly if this cpuset is no longer |
| 484 | * unused, and this cpuset will be reprieved from its death sentence, |
| 485 | * to continue to serve a useful existence. Next time it's released, |
| 486 | * we will get notified again, if it still has 'notify_on_release' set. |
| 487 | * |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 488 | * The final arg to call_usermodehelper() is 0, which means don't |
| 489 | * wait. The separate /sbin/cpuset_release_agent task is forked by |
| 490 | * call_usermodehelper(), then control in this thread returns here, |
| 491 | * without waiting for the release agent task. We don't bother to |
| 492 | * wait because the caller of this routine has no use for the exit |
| 493 | * status of the /sbin/cpuset_release_agent task, so no sense holding |
| 494 | * our caller up for that. |
| 495 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 496 | * When we had only one cpuset mutex, we had to call this |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 497 | * without holding it, to avoid deadlock when call_usermodehelper() |
| 498 | * allocated memory. With two locks, we could now call this while |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 499 | * holding manage_mutex, but we still don't, so as to minimize |
| 500 | * the time manage_mutex is held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | */ |
| 502 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 503 | static void cpuset_release_agent(const char *pathbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
| 505 | char *argv[3], *envp[3]; |
| 506 | int i; |
| 507 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 508 | if (!pathbuf) |
| 509 | return; |
| 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | i = 0; |
| 512 | argv[i++] = "/sbin/cpuset_release_agent"; |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 513 | argv[i++] = (char *)pathbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | argv[i] = NULL; |
| 515 | |
| 516 | i = 0; |
| 517 | /* minimal command environment */ |
| 518 | envp[i++] = "HOME=/"; |
| 519 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 520 | envp[i] = NULL; |
| 521 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 522 | call_usermodehelper(argv[0], argv, envp, 0); |
| 523 | kfree(pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Either cs->count of using tasks transitioned to zero, or the |
| 528 | * cs->children list of child cpusets just became empty. If this |
| 529 | * cs is notify_on_release() and now both the user count is zero and |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 530 | * the list of children is empty, prepare cpuset path in a kmalloc'd |
| 531 | * buffer, to be returned via ppathbuf, so that the caller can invoke |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 532 | * cpuset_release_agent() with it later on, once manage_mutex is dropped. |
| 533 | * Call here with manage_mutex held. |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 534 | * |
| 535 | * This check_for_release() routine is responsible for kmalloc'ing |
| 536 | * pathbuf. The above cpuset_release_agent() is responsible for |
| 537 | * kfree'ing pathbuf. The caller of these routines is responsible |
| 538 | * for providing a pathbuf pointer, initialized to NULL, then |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 539 | * calling check_for_release() with manage_mutex held and the address |
| 540 | * of the pathbuf pointer, then dropping manage_mutex, then calling |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 541 | * cpuset_release_agent() with pathbuf, as set by check_for_release(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | */ |
| 543 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 544 | static void check_for_release(struct cpuset *cs, char **ppathbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
| 546 | if (notify_on_release(cs) && atomic_read(&cs->count) == 0 && |
| 547 | list_empty(&cs->children)) { |
| 548 | char *buf; |
| 549 | |
| 550 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 551 | if (!buf) |
| 552 | return; |
| 553 | if (cpuset_path(cs, buf, PAGE_SIZE) < 0) |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 554 | kfree(buf); |
| 555 | else |
| 556 | *ppathbuf = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Return in *pmask the portion of a cpusets's cpus_allowed that |
| 562 | * are online. If none are online, walk up the cpuset hierarchy |
| 563 | * until we find one that does have some online cpus. If we get |
| 564 | * all the way to the top and still haven't found any online cpus, |
| 565 | * return cpu_online_map. Or if passed a NULL cs from an exit'ing |
| 566 | * task, return cpu_online_map. |
| 567 | * |
| 568 | * One way or another, we guarantee to return some non-empty subset |
| 569 | * of cpu_online_map. |
| 570 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 571 | * Call with callback_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | */ |
| 573 | |
| 574 | static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask) |
| 575 | { |
| 576 | while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map)) |
| 577 | cs = cs->parent; |
| 578 | if (cs) |
| 579 | cpus_and(*pmask, cs->cpus_allowed, cpu_online_map); |
| 580 | else |
| 581 | *pmask = cpu_online_map; |
| 582 | BUG_ON(!cpus_intersects(*pmask, cpu_online_map)); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Return in *pmask the portion of a cpusets's mems_allowed that |
| 587 | * are online. If none are online, walk up the cpuset hierarchy |
| 588 | * until we find one that does have some online mems. If we get |
| 589 | * all the way to the top and still haven't found any online mems, |
| 590 | * return node_online_map. |
| 591 | * |
| 592 | * One way or another, we guarantee to return some non-empty subset |
| 593 | * of node_online_map. |
| 594 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 595 | * Call with callback_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | */ |
| 597 | |
| 598 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) |
| 599 | { |
| 600 | while (cs && !nodes_intersects(cs->mems_allowed, node_online_map)) |
| 601 | cs = cs->parent; |
| 602 | if (cs) |
| 603 | nodes_and(*pmask, cs->mems_allowed, node_online_map); |
| 604 | else |
| 605 | *pmask = node_online_map; |
| 606 | BUG_ON(!nodes_intersects(*pmask, node_online_map)); |
| 607 | } |
| 608 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 609 | /** |
| 610 | * cpuset_update_task_memory_state - update task memory placement |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | * |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 612 | * If the current tasks cpusets mems_allowed changed behind our |
| 613 | * backs, update current->mems_allowed, mems_generation and task NUMA |
| 614 | * mempolicy to the new value. |
| 615 | * |
| 616 | * Task mempolicy is updated by rebinding it relative to the |
| 617 | * current->cpuset if a task has its memory placement changed. |
| 618 | * Do not call this routine if in_interrupt(). |
| 619 | * |
Paul Jackson | 4a01c8d | 2006-03-31 02:30:50 -0800 | [diff] [blame] | 620 | * Call without callback_mutex or task_lock() held. May be |
| 621 | * called with or without manage_mutex held. Thanks in part to |
| 622 | * 'the_top_cpuset_hack', the tasks cpuset pointer will never |
| 623 | * be NULL. This routine also might acquire callback_mutex and |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 624 | * current->mm->mmap_sem during call. |
Paul Jackson | 5aa15b5 | 2005-10-30 15:02:28 -0800 | [diff] [blame] | 625 | * |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 626 | * Reading current->cpuset->mems_generation doesn't need task_lock |
| 627 | * to guard the current->cpuset derefence, because it is guarded |
| 628 | * from concurrent freeing of current->cpuset by attach_task(), |
| 629 | * using RCU. |
| 630 | * |
| 631 | * The rcu_dereference() is technically probably not needed, |
| 632 | * as I don't actually mind if I see a new cpuset pointer but |
| 633 | * an old value of mems_generation. However this really only |
| 634 | * matters on alpha systems using cpusets heavily. If I dropped |
| 635 | * that rcu_dereference(), it would save them a memory barrier. |
| 636 | * For all other arch's, rcu_dereference is a no-op anyway, and for |
| 637 | * alpha systems not using cpusets, another planned optimization, |
| 638 | * avoiding the rcu critical section for tasks in the root cpuset |
| 639 | * which is statically allocated, so can't vanish, will make this |
| 640 | * irrelevant. Better to use RCU as intended, than to engage in |
| 641 | * some cute trick to save a memory barrier that is impossible to |
| 642 | * test, for alpha systems using cpusets heavily, which might not |
| 643 | * even exist. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 644 | * |
| 645 | * This routine is needed to update the per-task mems_allowed data, |
| 646 | * within the tasks context, when it is trying to allocate memory |
| 647 | * (in various mm/mempolicy.c routines) and notices that some other |
| 648 | * task has been modifying its cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | */ |
| 650 | |
Randy Dunlap | fe85a99 | 2006-02-03 03:04:23 -0800 | [diff] [blame] | 651 | void cpuset_update_task_memory_state(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 653 | int my_cpusets_mem_gen; |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 654 | struct task_struct *tsk = current; |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 655 | struct cpuset *cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Paul Jackson | 03a285f | 2006-01-08 01:02:04 -0800 | [diff] [blame] | 657 | if (tsk->cpuset == &top_cpuset) { |
| 658 | /* Don't need rcu for top_cpuset. It's never freed. */ |
| 659 | my_cpusets_mem_gen = top_cpuset.mems_generation; |
| 660 | } else { |
| 661 | rcu_read_lock(); |
| 662 | cs = rcu_dereference(tsk->cpuset); |
| 663 | my_cpusets_mem_gen = cs->mems_generation; |
| 664 | rcu_read_unlock(); |
| 665 | } |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 666 | |
| 667 | if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 668 | mutex_lock(&callback_mutex); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 669 | task_lock(tsk); |
| 670 | cs = tsk->cpuset; /* Maybe changed when task not locked */ |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 671 | guarantee_online_mems(cs, &tsk->mems_allowed); |
| 672 | tsk->cpuset_mems_generation = cs->mems_generation; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 673 | if (is_spread_page(cs)) |
| 674 | tsk->flags |= PF_SPREAD_PAGE; |
| 675 | else |
| 676 | tsk->flags &= ~PF_SPREAD_PAGE; |
| 677 | if (is_spread_slab(cs)) |
| 678 | tsk->flags |= PF_SPREAD_SLAB; |
| 679 | else |
| 680 | tsk->flags &= ~PF_SPREAD_SLAB; |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 681 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 682 | mutex_unlock(&callback_mutex); |
Paul Jackson | 74cb215 | 2006-01-08 01:01:56 -0800 | [diff] [blame] | 683 | mpol_rebind_task(tsk, &tsk->mems_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? |
| 689 | * |
| 690 | * One cpuset is a subset of another if all its allowed CPUs and |
| 691 | * Memory Nodes are a subset of the other, and its exclusive flags |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 692 | * 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] | 693 | */ |
| 694 | |
| 695 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) |
| 696 | { |
| 697 | return cpus_subset(p->cpus_allowed, q->cpus_allowed) && |
| 698 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
| 699 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && |
| 700 | is_mem_exclusive(p) <= is_mem_exclusive(q); |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * validate_change() - Used to validate that any proposed cpuset change |
| 705 | * follows the structural rules for cpusets. |
| 706 | * |
| 707 | * If we replaced the flag and mask values of the current cpuset |
| 708 | * (cur) with those values in the trial cpuset (trial), would |
| 709 | * our various subset and exclusive rules still be valid? Presumes |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 710 | * manage_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | * |
| 712 | * 'cur' is the address of an actual, in-use cpuset. Operations |
| 713 | * such as list traversal that depend on the actual address of the |
| 714 | * cpuset in the list must use cur below, not trial. |
| 715 | * |
| 716 | * 'trial' is the address of bulk structure copy of cur, with |
| 717 | * perhaps one or more of the fields cpus_allowed, mems_allowed, |
| 718 | * or flags changed to new, trial values. |
| 719 | * |
| 720 | * Return 0 if valid, -errno if not. |
| 721 | */ |
| 722 | |
| 723 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) |
| 724 | { |
| 725 | struct cpuset *c, *par; |
| 726 | |
| 727 | /* Each of our child cpusets must be a subset of us */ |
| 728 | list_for_each_entry(c, &cur->children, sibling) { |
| 729 | if (!is_cpuset_subset(c, trial)) |
| 730 | return -EBUSY; |
| 731 | } |
| 732 | |
| 733 | /* Remaining checks don't apply to root cpuset */ |
| 734 | if ((par = cur->parent) == NULL) |
| 735 | return 0; |
| 736 | |
| 737 | /* We must be a subset of our parent cpuset */ |
| 738 | if (!is_cpuset_subset(trial, par)) |
| 739 | return -EACCES; |
| 740 | |
| 741 | /* If either I or some sibling (!= me) is exclusive, we can't overlap */ |
| 742 | list_for_each_entry(c, &par->children, sibling) { |
| 743 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
| 744 | c != cur && |
| 745 | cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) |
| 746 | return -EINVAL; |
| 747 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
| 748 | c != cur && |
| 749 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) |
| 750 | return -EINVAL; |
| 751 | } |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 756 | /* |
| 757 | * For a given cpuset cur, partition the system as follows |
| 758 | * a. All cpus in the parent cpuset's cpus_allowed that are not part of any |
| 759 | * exclusive child cpusets |
| 760 | * b. All cpus in the current cpuset's cpus_allowed that are not part of any |
| 761 | * exclusive child cpusets |
| 762 | * Build these two partitions by calling partition_sched_domains |
| 763 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 764 | * Call with manage_mutex held. May nest a call to the |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 765 | * lock_cpu_hotplug()/unlock_cpu_hotplug() pair. |
| 766 | */ |
Paul Jackson | 212d6d2 | 2005-08-25 12:47:56 -0700 | [diff] [blame] | 767 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 768 | static void update_cpu_domains(struct cpuset *cur) |
| 769 | { |
| 770 | struct cpuset *c, *par = cur->parent; |
| 771 | cpumask_t pspan, cspan; |
| 772 | |
| 773 | if (par == NULL || cpus_empty(cur->cpus_allowed)) |
| 774 | return; |
| 775 | |
| 776 | /* |
| 777 | * Get all cpus from parent's cpus_allowed not part of exclusive |
| 778 | * children |
| 779 | */ |
| 780 | pspan = par->cpus_allowed; |
| 781 | list_for_each_entry(c, &par->children, sibling) { |
| 782 | if (is_cpu_exclusive(c)) |
| 783 | cpus_andnot(pspan, pspan, c->cpus_allowed); |
| 784 | } |
| 785 | if (is_removed(cur) || !is_cpu_exclusive(cur)) { |
| 786 | cpus_or(pspan, pspan, cur->cpus_allowed); |
| 787 | if (cpus_equal(pspan, cur->cpus_allowed)) |
| 788 | return; |
| 789 | cspan = CPU_MASK_NONE; |
| 790 | } else { |
| 791 | if (cpus_empty(pspan)) |
| 792 | return; |
| 793 | cspan = cur->cpus_allowed; |
| 794 | /* |
| 795 | * Get all cpus from current cpuset's cpus_allowed not part |
| 796 | * of exclusive children |
| 797 | */ |
| 798 | list_for_each_entry(c, &cur->children, sibling) { |
| 799 | if (is_cpu_exclusive(c)) |
| 800 | cpus_andnot(cspan, cspan, c->cpus_allowed); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | lock_cpu_hotplug(); |
| 805 | partition_sched_domains(&pspan, &cspan); |
| 806 | unlock_cpu_hotplug(); |
| 807 | } |
| 808 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 809 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 810 | * Call with manage_mutex held. May take callback_mutex during call. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 811 | */ |
| 812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | static int update_cpumask(struct cpuset *cs, char *buf) |
| 814 | { |
| 815 | struct cpuset trialcs; |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 816 | int retval, cpus_unchanged; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | trialcs = *cs; |
| 819 | retval = cpulist_parse(buf, trialcs.cpus_allowed); |
| 820 | if (retval < 0) |
| 821 | return retval; |
| 822 | cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); |
| 823 | if (cpus_empty(trialcs.cpus_allowed)) |
| 824 | return -ENOSPC; |
| 825 | retval = validate_change(cs, &trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 826 | if (retval < 0) |
| 827 | return retval; |
| 828 | cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 829 | mutex_lock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 830 | cs->cpus_allowed = trialcs.cpus_allowed; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 831 | mutex_unlock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 832 | if (is_cpu_exclusive(cs) && !cpus_unchanged) |
| 833 | update_cpu_domains(cs); |
| 834 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 837 | /* |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 838 | * cpuset_migrate_mm |
| 839 | * |
| 840 | * Migrate memory region from one set of nodes to another. |
| 841 | * |
| 842 | * Temporarilly set tasks mems_allowed to target nodes of migration, |
| 843 | * so that the migration code can allocate pages on these nodes. |
| 844 | * |
| 845 | * Call holding manage_mutex, so our current->cpuset won't change |
| 846 | * during this call, as manage_mutex holds off any attach_task() |
| 847 | * calls. Therefore we don't need to take task_lock around the |
| 848 | * call to guarantee_online_mems(), as we know no one is changing |
| 849 | * our tasks cpuset. |
| 850 | * |
| 851 | * Hold callback_mutex around the two modifications of our tasks |
| 852 | * mems_allowed to synchronize with cpuset_mems_allowed(). |
| 853 | * |
| 854 | * While the mm_struct we are migrating is typically from some |
| 855 | * other task, the task_struct mems_allowed that we are hacking |
| 856 | * is for our current task, which must allocate new pages for that |
| 857 | * migrating memory region. |
| 858 | * |
| 859 | * We call cpuset_update_task_memory_state() before hacking |
| 860 | * our tasks mems_allowed, so that we are assured of being in |
| 861 | * sync with our tasks cpuset, and in particular, callbacks to |
| 862 | * cpuset_update_task_memory_state() from nested page allocations |
| 863 | * won't see any mismatch of our cpuset and task mems_generation |
| 864 | * values, so won't overwrite our hacked tasks mems_allowed |
| 865 | * nodemask. |
| 866 | */ |
| 867 | |
| 868 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, |
| 869 | const nodemask_t *to) |
| 870 | { |
| 871 | struct task_struct *tsk = current; |
| 872 | |
| 873 | cpuset_update_task_memory_state(); |
| 874 | |
| 875 | mutex_lock(&callback_mutex); |
| 876 | tsk->mems_allowed = *to; |
| 877 | mutex_unlock(&callback_mutex); |
| 878 | |
| 879 | do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL); |
| 880 | |
| 881 | mutex_lock(&callback_mutex); |
| 882 | guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed); |
| 883 | mutex_unlock(&callback_mutex); |
| 884 | } |
| 885 | |
| 886 | /* |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 887 | * Handle user request to change the 'mems' memory placement |
| 888 | * of a cpuset. Needs to validate the request, update the |
| 889 | * cpusets mems_allowed and mems_generation, and for each |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 890 | * task in the cpuset, rebind any vma mempolicies and if |
| 891 | * the cpuset is marked 'memory_migrate', migrate the tasks |
| 892 | * pages to the new memory. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 893 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 894 | * Call with manage_mutex held. May take callback_mutex during call. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 895 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
| 896 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind |
| 897 | * their mempolicies to the cpusets new mems_allowed. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 898 | */ |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | static int update_nodemask(struct cpuset *cs, char *buf) |
| 901 | { |
| 902 | struct cpuset trialcs; |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 903 | nodemask_t oldmem; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 904 | struct task_struct *g, *p; |
| 905 | struct mm_struct **mmarray; |
| 906 | int i, n, ntasks; |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 907 | int migrate; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 908 | int fudge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | int retval; |
| 910 | |
| 911 | trialcs = *cs; |
| 912 | retval = nodelist_parse(buf, trialcs.mems_allowed); |
| 913 | if (retval < 0) |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 914 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map); |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 916 | oldmem = cs->mems_allowed; |
| 917 | if (nodes_equal(oldmem, trialcs.mems_allowed)) { |
| 918 | retval = 0; /* Too easy - nothing to do */ |
| 919 | goto done; |
| 920 | } |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 921 | if (nodes_empty(trialcs.mems_allowed)) { |
| 922 | retval = -ENOSPC; |
| 923 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 925 | retval = validate_change(cs, &trialcs); |
| 926 | if (retval < 0) |
| 927 | goto done; |
| 928 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 929 | mutex_lock(&callback_mutex); |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 930 | cs->mems_allowed = trialcs.mems_allowed; |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 931 | cs->mems_generation = cpuset_mems_generation++; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 932 | mutex_unlock(&callback_mutex); |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 933 | |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 934 | set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */ |
| 935 | |
| 936 | fudge = 10; /* spare mmarray[] slots */ |
| 937 | fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */ |
| 938 | retval = -ENOMEM; |
| 939 | |
| 940 | /* |
| 941 | * Allocate mmarray[] to hold mm reference for each task |
| 942 | * in cpuset cs. Can't kmalloc GFP_KERNEL while holding |
| 943 | * tasklist_lock. We could use GFP_ATOMIC, but with a |
| 944 | * few more lines of code, we can retry until we get a big |
| 945 | * enough mmarray[] w/o using GFP_ATOMIC. |
| 946 | */ |
| 947 | while (1) { |
| 948 | ntasks = atomic_read(&cs->count); /* guess */ |
| 949 | ntasks += fudge; |
| 950 | mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL); |
| 951 | if (!mmarray) |
| 952 | goto done; |
| 953 | write_lock_irq(&tasklist_lock); /* block fork */ |
| 954 | if (atomic_read(&cs->count) <= ntasks) |
| 955 | break; /* got enough */ |
| 956 | write_unlock_irq(&tasklist_lock); /* try again */ |
| 957 | kfree(mmarray); |
| 958 | } |
| 959 | |
| 960 | n = 0; |
| 961 | |
| 962 | /* Load up mmarray[] with mm reference for each task in cpuset. */ |
| 963 | do_each_thread(g, p) { |
| 964 | struct mm_struct *mm; |
| 965 | |
| 966 | if (n >= ntasks) { |
| 967 | printk(KERN_WARNING |
| 968 | "Cpuset mempolicy rebind incomplete.\n"); |
| 969 | continue; |
| 970 | } |
| 971 | if (p->cpuset != cs) |
| 972 | continue; |
| 973 | mm = get_task_mm(p); |
| 974 | if (!mm) |
| 975 | continue; |
| 976 | mmarray[n++] = mm; |
| 977 | } while_each_thread(g, p); |
| 978 | write_unlock_irq(&tasklist_lock); |
| 979 | |
| 980 | /* |
| 981 | * Now that we've dropped the tasklist spinlock, we can |
| 982 | * rebind the vma mempolicies of each mm in mmarray[] to their |
| 983 | * new cpuset, and release that mm. The mpol_rebind_mm() |
| 984 | * call takes mmap_sem, which we couldn't take while holding |
| 985 | * tasklist_lock. Forks can happen again now - the mpol_copy() |
| 986 | * cpuset_being_rebound check will catch such forks, and rebind |
| 987 | * their vma mempolicies too. Because we still hold the global |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 988 | * cpuset manage_mutex, we know that no other rebind effort will |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 989 | * be contending for the global variable cpuset_being_rebound. |
| 990 | * 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] | 991 | * is idempotent. Also migrate pages in each mm to new nodes. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 992 | */ |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 993 | migrate = is_memory_migrate(cs); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 994 | for (i = 0; i < n; i++) { |
| 995 | struct mm_struct *mm = mmarray[i]; |
| 996 | |
| 997 | mpol_rebind_mm(mm, &cs->mems_allowed); |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 998 | if (migrate) |
| 999 | cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1000 | mmput(mm); |
| 1001 | } |
| 1002 | |
| 1003 | /* We're done rebinding vma's to this cpusets new mems_allowed. */ |
| 1004 | kfree(mmarray); |
| 1005 | set_cpuset_being_rebound(NULL); |
| 1006 | retval = 0; |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 1007 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return retval; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1012 | * Call with manage_mutex held. |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1013 | */ |
| 1014 | |
| 1015 | static int update_memory_pressure_enabled(struct cpuset *cs, char *buf) |
| 1016 | { |
| 1017 | if (simple_strtoul(buf, NULL, 10) != 0) |
| 1018 | cpuset_memory_pressure_enabled = 1; |
| 1019 | else |
| 1020 | cpuset_memory_pressure_enabled = 0; |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | * update_flag - read a 0 or a 1 in a file and update associated flag |
| 1026 | * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1027 | * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE, |
| 1028 | * CS_SPREAD_PAGE, CS_SPREAD_SLAB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | * cs: the cpuset to update |
| 1030 | * buf: the buffer where we read the 0 or 1 |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1031 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1032 | * Call with manage_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | */ |
| 1034 | |
| 1035 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf) |
| 1036 | { |
| 1037 | int turning_on; |
| 1038 | struct cpuset trialcs; |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1039 | int err, cpu_exclusive_changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | turning_on = (simple_strtoul(buf, NULL, 10) != 0); |
| 1042 | |
| 1043 | trialcs = *cs; |
| 1044 | if (turning_on) |
| 1045 | set_bit(bit, &trialcs.flags); |
| 1046 | else |
| 1047 | clear_bit(bit, &trialcs.flags); |
| 1048 | |
| 1049 | err = validate_change(cs, &trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1050 | if (err < 0) |
| 1051 | return err; |
| 1052 | cpu_exclusive_changed = |
| 1053 | (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs)); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1054 | mutex_lock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1055 | if (turning_on) |
| 1056 | set_bit(bit, &cs->flags); |
| 1057 | else |
| 1058 | clear_bit(bit, &cs->flags); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1059 | mutex_unlock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1060 | |
| 1061 | if (cpu_exclusive_changed) |
| 1062 | update_cpu_domains(cs); |
| 1063 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1066 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1067 | * Frequency meter - How fast is some event occuring? |
| 1068 | * |
| 1069 | * These routines manage a digitally filtered, constant time based, |
| 1070 | * event frequency meter. There are four routines: |
| 1071 | * fmeter_init() - initialize a frequency meter. |
| 1072 | * fmeter_markevent() - called each time the event happens. |
| 1073 | * fmeter_getrate() - returns the recent rate of such events. |
| 1074 | * fmeter_update() - internal routine used to update fmeter. |
| 1075 | * |
| 1076 | * A common data structure is passed to each of these routines, |
| 1077 | * which is used to keep track of the state required to manage the |
| 1078 | * frequency meter and its digital filter. |
| 1079 | * |
| 1080 | * The filter works on the number of events marked per unit time. |
| 1081 | * The filter is single-pole low-pass recursive (IIR). The time unit |
| 1082 | * is 1 second. Arithmetic is done using 32-bit integers scaled to |
| 1083 | * simulate 3 decimal digits of precision (multiplied by 1000). |
| 1084 | * |
| 1085 | * With an FM_COEF of 933, and a time base of 1 second, the filter |
| 1086 | * has a half-life of 10 seconds, meaning that if the events quit |
| 1087 | * happening, then the rate returned from the fmeter_getrate() |
| 1088 | * will be cut in half each 10 seconds, until it converges to zero. |
| 1089 | * |
| 1090 | * It is not worth doing a real infinitely recursive filter. If more |
| 1091 | * than FM_MAXTICKS ticks have elapsed since the last filter event, |
| 1092 | * just compute FM_MAXTICKS ticks worth, by which point the level |
| 1093 | * will be stable. |
| 1094 | * |
| 1095 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid |
| 1096 | * arithmetic overflow in the fmeter_update() routine. |
| 1097 | * |
| 1098 | * Given the simple 32 bit integer arithmetic used, this meter works |
| 1099 | * best for reporting rates between one per millisecond (msec) and |
| 1100 | * one per 32 (approx) seconds. At constant rates faster than one |
| 1101 | * per msec it maxes out at values just under 1,000,000. At constant |
| 1102 | * rates between one per msec, and one per second it will stabilize |
| 1103 | * to a value N*1000, where N is the rate of events per second. |
| 1104 | * At constant rates between one per second and one per 32 seconds, |
| 1105 | * it will be choppy, moving up on the seconds that have an event, |
| 1106 | * and then decaying until the next event. At rates slower than |
| 1107 | * about one in 32 seconds, it decays all the way back to zero between |
| 1108 | * each event. |
| 1109 | */ |
| 1110 | |
| 1111 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ |
| 1112 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ |
| 1113 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ |
| 1114 | #define FM_SCALE 1000 /* faux fixed point scale */ |
| 1115 | |
| 1116 | /* Initialize a frequency meter */ |
| 1117 | static void fmeter_init(struct fmeter *fmp) |
| 1118 | { |
| 1119 | fmp->cnt = 0; |
| 1120 | fmp->val = 0; |
| 1121 | fmp->time = 0; |
| 1122 | spin_lock_init(&fmp->lock); |
| 1123 | } |
| 1124 | |
| 1125 | /* Internal meter update - process cnt events and update value */ |
| 1126 | static void fmeter_update(struct fmeter *fmp) |
| 1127 | { |
| 1128 | time_t now = get_seconds(); |
| 1129 | time_t ticks = now - fmp->time; |
| 1130 | |
| 1131 | if (ticks == 0) |
| 1132 | return; |
| 1133 | |
| 1134 | ticks = min(FM_MAXTICKS, ticks); |
| 1135 | while (ticks-- > 0) |
| 1136 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; |
| 1137 | fmp->time = now; |
| 1138 | |
| 1139 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; |
| 1140 | fmp->cnt = 0; |
| 1141 | } |
| 1142 | |
| 1143 | /* Process any previous ticks, then bump cnt by one (times scale). */ |
| 1144 | static void fmeter_markevent(struct fmeter *fmp) |
| 1145 | { |
| 1146 | spin_lock(&fmp->lock); |
| 1147 | fmeter_update(fmp); |
| 1148 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); |
| 1149 | spin_unlock(&fmp->lock); |
| 1150 | } |
| 1151 | |
| 1152 | /* Process any previous ticks, then return current value. */ |
| 1153 | static int fmeter_getrate(struct fmeter *fmp) |
| 1154 | { |
| 1155 | int val; |
| 1156 | |
| 1157 | spin_lock(&fmp->lock); |
| 1158 | fmeter_update(fmp); |
| 1159 | val = fmp->val; |
| 1160 | spin_unlock(&fmp->lock); |
| 1161 | return val; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1165 | * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly |
| 1166 | * writing the path of the old cpuset in 'ppathbuf' if it needs to be |
| 1167 | * notified on release. |
| 1168 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1169 | * Call holding manage_mutex. May take callback_mutex and task_lock of |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1170 | * the task 'pid' during call. |
| 1171 | */ |
| 1172 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1173 | static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | { |
| 1175 | pid_t pid; |
| 1176 | struct task_struct *tsk; |
| 1177 | struct cpuset *oldcs; |
| 1178 | cpumask_t cpus; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1179 | nodemask_t from, to; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1180 | struct mm_struct *mm; |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame^] | 1181 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1183 | if (sscanf(pidbuf, "%d", &pid) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | return -EIO; |
| 1185 | if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
| 1186 | return -ENOSPC; |
| 1187 | |
| 1188 | if (pid) { |
| 1189 | read_lock(&tasklist_lock); |
| 1190 | |
| 1191 | tsk = find_task_by_pid(pid); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1192 | if (!tsk || tsk->flags & PF_EXITING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | read_unlock(&tasklist_lock); |
| 1194 | return -ESRCH; |
| 1195 | } |
| 1196 | |
| 1197 | get_task_struct(tsk); |
| 1198 | read_unlock(&tasklist_lock); |
| 1199 | |
| 1200 | if ((current->euid) && (current->euid != tsk->uid) |
| 1201 | && (current->euid != tsk->suid)) { |
| 1202 | put_task_struct(tsk); |
| 1203 | return -EACCES; |
| 1204 | } |
| 1205 | } else { |
| 1206 | tsk = current; |
| 1207 | get_task_struct(tsk); |
| 1208 | } |
| 1209 | |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame^] | 1210 | retval = security_task_setscheduler(tsk, 0, NULL); |
| 1211 | if (retval) { |
| 1212 | put_task_struct(tsk); |
| 1213 | return retval; |
| 1214 | } |
| 1215 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1216 | mutex_lock(&callback_mutex); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | task_lock(tsk); |
| 1219 | oldcs = tsk->cpuset; |
| 1220 | if (!oldcs) { |
| 1221 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1222 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | put_task_struct(tsk); |
| 1224 | return -ESRCH; |
| 1225 | } |
| 1226 | atomic_inc(&cs->count); |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 1227 | rcu_assign_pointer(tsk->cpuset, cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | task_unlock(tsk); |
| 1229 | |
| 1230 | guarantee_online_cpus(cs, &cpus); |
| 1231 | set_cpus_allowed(tsk, cpus); |
| 1232 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1233 | from = oldcs->mems_allowed; |
| 1234 | to = cs->mems_allowed; |
| 1235 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1236 | mutex_unlock(&callback_mutex); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1237 | |
| 1238 | mm = get_task_mm(tsk); |
| 1239 | if (mm) { |
| 1240 | mpol_rebind_mm(mm, &to); |
Paul Jackson | 2741a55 | 2006-03-31 02:30:51 -0800 | [diff] [blame] | 1241 | if (is_memory_migrate(cs)) |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 1242 | cpuset_migrate_mm(mm, &from, &to); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1243 | mmput(mm); |
| 1244 | } |
| 1245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | put_task_struct(tsk); |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 1247 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | if (atomic_dec_and_test(&oldcs->count)) |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1249 | check_for_release(oldcs, ppathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | /* The various types of files and directories in a cpuset file system */ |
| 1254 | |
| 1255 | typedef enum { |
| 1256 | FILE_ROOT, |
| 1257 | FILE_DIR, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1258 | FILE_MEMORY_MIGRATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | FILE_CPULIST, |
| 1260 | FILE_MEMLIST, |
| 1261 | FILE_CPU_EXCLUSIVE, |
| 1262 | FILE_MEM_EXCLUSIVE, |
| 1263 | FILE_NOTIFY_ON_RELEASE, |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1264 | FILE_MEMORY_PRESSURE_ENABLED, |
| 1265 | FILE_MEMORY_PRESSURE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1266 | FILE_SPREAD_PAGE, |
| 1267 | FILE_SPREAD_SLAB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | FILE_TASKLIST, |
| 1269 | } cpuset_filetype_t; |
| 1270 | |
| 1271 | static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf, |
| 1272 | size_t nbytes, loff_t *unused_ppos) |
| 1273 | { |
| 1274 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); |
| 1275 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1276 | cpuset_filetype_t type = cft->private; |
| 1277 | char *buffer; |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1278 | char *pathbuf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | int retval = 0; |
| 1280 | |
| 1281 | /* Crude upper limit on largest legitimate cpulist user might write. */ |
| 1282 | if (nbytes > 100 + 6 * NR_CPUS) |
| 1283 | return -E2BIG; |
| 1284 | |
| 1285 | /* +1 for nul-terminator */ |
| 1286 | if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0) |
| 1287 | return -ENOMEM; |
| 1288 | |
| 1289 | if (copy_from_user(buffer, userbuf, nbytes)) { |
| 1290 | retval = -EFAULT; |
| 1291 | goto out1; |
| 1292 | } |
| 1293 | buffer[nbytes] = 0; /* nul-terminate */ |
| 1294 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1295 | mutex_lock(&manage_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | if (is_removed(cs)) { |
| 1298 | retval = -ENODEV; |
| 1299 | goto out2; |
| 1300 | } |
| 1301 | |
| 1302 | switch (type) { |
| 1303 | case FILE_CPULIST: |
| 1304 | retval = update_cpumask(cs, buffer); |
| 1305 | break; |
| 1306 | case FILE_MEMLIST: |
| 1307 | retval = update_nodemask(cs, buffer); |
| 1308 | break; |
| 1309 | case FILE_CPU_EXCLUSIVE: |
| 1310 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer); |
| 1311 | break; |
| 1312 | case FILE_MEM_EXCLUSIVE: |
| 1313 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); |
| 1314 | break; |
| 1315 | case FILE_NOTIFY_ON_RELEASE: |
| 1316 | retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer); |
| 1317 | break; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1318 | case FILE_MEMORY_MIGRATE: |
| 1319 | retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer); |
| 1320 | break; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1321 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1322 | retval = update_memory_pressure_enabled(cs, buffer); |
| 1323 | break; |
| 1324 | case FILE_MEMORY_PRESSURE: |
| 1325 | retval = -EACCES; |
| 1326 | break; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1327 | case FILE_SPREAD_PAGE: |
| 1328 | retval = update_flag(CS_SPREAD_PAGE, cs, buffer); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1329 | cs->mems_generation = cpuset_mems_generation++; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1330 | break; |
| 1331 | case FILE_SPREAD_SLAB: |
| 1332 | retval = update_flag(CS_SPREAD_SLAB, cs, buffer); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1333 | cs->mems_generation = cpuset_mems_generation++; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1334 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | case FILE_TASKLIST: |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1336 | retval = attach_task(cs, buffer, &pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | break; |
| 1338 | default: |
| 1339 | retval = -EINVAL; |
| 1340 | goto out2; |
| 1341 | } |
| 1342 | |
| 1343 | if (retval == 0) |
| 1344 | retval = nbytes; |
| 1345 | out2: |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1346 | mutex_unlock(&manage_mutex); |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1347 | cpuset_release_agent(pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | out1: |
| 1349 | kfree(buffer); |
| 1350 | return retval; |
| 1351 | } |
| 1352 | |
| 1353 | static ssize_t cpuset_file_write(struct file *file, const char __user *buf, |
| 1354 | size_t nbytes, loff_t *ppos) |
| 1355 | { |
| 1356 | ssize_t retval = 0; |
| 1357 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1358 | if (!cft) |
| 1359 | return -ENODEV; |
| 1360 | |
| 1361 | /* special function ? */ |
| 1362 | if (cft->write) |
| 1363 | retval = cft->write(file, buf, nbytes, ppos); |
| 1364 | else |
| 1365 | retval = cpuset_common_file_write(file, buf, nbytes, ppos); |
| 1366 | |
| 1367 | return retval; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * These ascii lists should be read in a single call, by using a user |
| 1372 | * buffer large enough to hold the entire map. If read in smaller |
| 1373 | * chunks, there is no guarantee of atomicity. Since the display format |
| 1374 | * used, list of ranges of sequential numbers, is variable length, |
| 1375 | * and since these maps can change value dynamically, one could read |
| 1376 | * gibberish by doing partial reads while a list was changing. |
| 1377 | * A single large read to a buffer that crosses a page boundary is |
| 1378 | * ok, because the result being copied to user land is not recomputed |
| 1379 | * across a page fault. |
| 1380 | */ |
| 1381 | |
| 1382 | static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs) |
| 1383 | { |
| 1384 | cpumask_t mask; |
| 1385 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1386 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | mask = cs->cpus_allowed; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1388 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | return cpulist_scnprintf(page, PAGE_SIZE, mask); |
| 1391 | } |
| 1392 | |
| 1393 | static int cpuset_sprintf_memlist(char *page, struct cpuset *cs) |
| 1394 | { |
| 1395 | nodemask_t mask; |
| 1396 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1397 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | mask = cs->mems_allowed; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1399 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
| 1401 | return nodelist_scnprintf(page, PAGE_SIZE, mask); |
| 1402 | } |
| 1403 | |
| 1404 | static ssize_t cpuset_common_file_read(struct file *file, char __user *buf, |
| 1405 | size_t nbytes, loff_t *ppos) |
| 1406 | { |
| 1407 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1408 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); |
| 1409 | cpuset_filetype_t type = cft->private; |
| 1410 | char *page; |
| 1411 | ssize_t retval = 0; |
| 1412 | char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
| 1414 | if (!(page = (char *)__get_free_page(GFP_KERNEL))) |
| 1415 | return -ENOMEM; |
| 1416 | |
| 1417 | s = page; |
| 1418 | |
| 1419 | switch (type) { |
| 1420 | case FILE_CPULIST: |
| 1421 | s += cpuset_sprintf_cpulist(s, cs); |
| 1422 | break; |
| 1423 | case FILE_MEMLIST: |
| 1424 | s += cpuset_sprintf_memlist(s, cs); |
| 1425 | break; |
| 1426 | case FILE_CPU_EXCLUSIVE: |
| 1427 | *s++ = is_cpu_exclusive(cs) ? '1' : '0'; |
| 1428 | break; |
| 1429 | case FILE_MEM_EXCLUSIVE: |
| 1430 | *s++ = is_mem_exclusive(cs) ? '1' : '0'; |
| 1431 | break; |
| 1432 | case FILE_NOTIFY_ON_RELEASE: |
| 1433 | *s++ = notify_on_release(cs) ? '1' : '0'; |
| 1434 | break; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1435 | case FILE_MEMORY_MIGRATE: |
| 1436 | *s++ = is_memory_migrate(cs) ? '1' : '0'; |
| 1437 | break; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1438 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1439 | *s++ = cpuset_memory_pressure_enabled ? '1' : '0'; |
| 1440 | break; |
| 1441 | case FILE_MEMORY_PRESSURE: |
| 1442 | s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter)); |
| 1443 | break; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1444 | case FILE_SPREAD_PAGE: |
| 1445 | *s++ = is_spread_page(cs) ? '1' : '0'; |
| 1446 | break; |
| 1447 | case FILE_SPREAD_SLAB: |
| 1448 | *s++ = is_spread_slab(cs) ? '1' : '0'; |
| 1449 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | default: |
| 1451 | retval = -EINVAL; |
| 1452 | goto out; |
| 1453 | } |
| 1454 | *s++ = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
Al Viro | eacaa1f | 2005-09-30 03:26:43 +0100 | [diff] [blame] | 1456 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | out: |
| 1458 | free_page((unsigned long)page); |
| 1459 | return retval; |
| 1460 | } |
| 1461 | |
| 1462 | static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes, |
| 1463 | loff_t *ppos) |
| 1464 | { |
| 1465 | ssize_t retval = 0; |
| 1466 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1467 | if (!cft) |
| 1468 | return -ENODEV; |
| 1469 | |
| 1470 | /* special function ? */ |
| 1471 | if (cft->read) |
| 1472 | retval = cft->read(file, buf, nbytes, ppos); |
| 1473 | else |
| 1474 | retval = cpuset_common_file_read(file, buf, nbytes, ppos); |
| 1475 | |
| 1476 | return retval; |
| 1477 | } |
| 1478 | |
| 1479 | static int cpuset_file_open(struct inode *inode, struct file *file) |
| 1480 | { |
| 1481 | int err; |
| 1482 | struct cftype *cft; |
| 1483 | |
| 1484 | err = generic_file_open(inode, file); |
| 1485 | if (err) |
| 1486 | return err; |
| 1487 | |
| 1488 | cft = __d_cft(file->f_dentry); |
| 1489 | if (!cft) |
| 1490 | return -ENODEV; |
| 1491 | if (cft->open) |
| 1492 | err = cft->open(inode, file); |
| 1493 | else |
| 1494 | err = 0; |
| 1495 | |
| 1496 | return err; |
| 1497 | } |
| 1498 | |
| 1499 | static int cpuset_file_release(struct inode *inode, struct file *file) |
| 1500 | { |
| 1501 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1502 | if (cft->release) |
| 1503 | return cft->release(inode, file); |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
Paul Jackson | 18a19cb | 2005-10-30 15:02:31 -0800 | [diff] [blame] | 1507 | /* |
| 1508 | * cpuset_rename - Only allow simple rename of directories in place. |
| 1509 | */ |
| 1510 | static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1511 | struct inode *new_dir, struct dentry *new_dentry) |
| 1512 | { |
| 1513 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 1514 | return -ENOTDIR; |
| 1515 | if (new_dentry->d_inode) |
| 1516 | return -EEXIST; |
| 1517 | if (old_dir != new_dir) |
| 1518 | return -EIO; |
| 1519 | return simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 1520 | } |
| 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | static struct file_operations cpuset_file_operations = { |
| 1523 | .read = cpuset_file_read, |
| 1524 | .write = cpuset_file_write, |
| 1525 | .llseek = generic_file_llseek, |
| 1526 | .open = cpuset_file_open, |
| 1527 | .release = cpuset_file_release, |
| 1528 | }; |
| 1529 | |
| 1530 | static struct inode_operations cpuset_dir_inode_operations = { |
| 1531 | .lookup = simple_lookup, |
| 1532 | .mkdir = cpuset_mkdir, |
| 1533 | .rmdir = cpuset_rmdir, |
Paul Jackson | 18a19cb | 2005-10-30 15:02:31 -0800 | [diff] [blame] | 1534 | .rename = cpuset_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | }; |
| 1536 | |
| 1537 | static int cpuset_create_file(struct dentry *dentry, int mode) |
| 1538 | { |
| 1539 | struct inode *inode; |
| 1540 | |
| 1541 | if (!dentry) |
| 1542 | return -ENOENT; |
| 1543 | if (dentry->d_inode) |
| 1544 | return -EEXIST; |
| 1545 | |
| 1546 | inode = cpuset_new_inode(mode); |
| 1547 | if (!inode) |
| 1548 | return -ENOMEM; |
| 1549 | |
| 1550 | if (S_ISDIR(mode)) { |
| 1551 | inode->i_op = &cpuset_dir_inode_operations; |
| 1552 | inode->i_fop = &simple_dir_operations; |
| 1553 | |
| 1554 | /* start off with i_nlink == 2 (for "." entry) */ |
| 1555 | inode->i_nlink++; |
| 1556 | } else if (S_ISREG(mode)) { |
| 1557 | inode->i_size = 0; |
| 1558 | inode->i_fop = &cpuset_file_operations; |
| 1559 | } |
| 1560 | |
| 1561 | d_instantiate(dentry, inode); |
| 1562 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * cpuset_create_dir - create a directory for an object. |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 1568 | * cs: the cpuset we create the directory for. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | * It must have a valid ->parent field |
| 1570 | * And we are going to fill its ->dentry field. |
| 1571 | * name: The name to give to the cpuset directory. Will be copied. |
| 1572 | * mode: mode to set on new directory. |
| 1573 | */ |
| 1574 | |
| 1575 | static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode) |
| 1576 | { |
| 1577 | struct dentry *dentry = NULL; |
| 1578 | struct dentry *parent; |
| 1579 | int error = 0; |
| 1580 | |
| 1581 | parent = cs->parent->dentry; |
| 1582 | dentry = cpuset_get_dentry(parent, name); |
| 1583 | if (IS_ERR(dentry)) |
| 1584 | return PTR_ERR(dentry); |
| 1585 | error = cpuset_create_file(dentry, S_IFDIR | mode); |
| 1586 | if (!error) { |
| 1587 | dentry->d_fsdata = cs; |
| 1588 | parent->d_inode->i_nlink++; |
| 1589 | cs->dentry = dentry; |
| 1590 | } |
| 1591 | dput(dentry); |
| 1592 | |
| 1593 | return error; |
| 1594 | } |
| 1595 | |
| 1596 | static int cpuset_add_file(struct dentry *dir, const struct cftype *cft) |
| 1597 | { |
| 1598 | struct dentry *dentry; |
| 1599 | int error; |
| 1600 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1601 | mutex_lock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | dentry = cpuset_get_dentry(dir, cft->name); |
| 1603 | if (!IS_ERR(dentry)) { |
| 1604 | error = cpuset_create_file(dentry, 0644 | S_IFREG); |
| 1605 | if (!error) |
| 1606 | dentry->d_fsdata = (void *)cft; |
| 1607 | dput(dentry); |
| 1608 | } else |
| 1609 | error = PTR_ERR(dentry); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1610 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | return error; |
| 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | * Stuff for reading the 'tasks' file. |
| 1616 | * |
| 1617 | * Reading this file can return large amounts of data if a cpuset has |
| 1618 | * *lots* of attached tasks. So it may need several calls to read(), |
| 1619 | * but we cannot guarantee that the information we produce is correct |
| 1620 | * unless we produce it entirely atomically. |
| 1621 | * |
| 1622 | * Upon tasks file open(), a struct ctr_struct is allocated, that |
| 1623 | * will have a pointer to an array (also allocated here). The struct |
| 1624 | * ctr_struct * is stored in file->private_data. Its resources will |
| 1625 | * be freed by release() when the file is closed. The array is used |
| 1626 | * to sprintf the PIDs and then used by read(). |
| 1627 | */ |
| 1628 | |
| 1629 | /* cpusets_tasks_read array */ |
| 1630 | |
| 1631 | struct ctr_struct { |
| 1632 | char *buf; |
| 1633 | int bufsz; |
| 1634 | }; |
| 1635 | |
| 1636 | /* |
| 1637 | * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1638 | * Return actual number of pids loaded. No need to task_lock(p) |
| 1639 | * when reading out p->cpuset, as we don't really care if it changes |
| 1640 | * on the next cycle, and we are not going to try to dereference it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1642 | static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | { |
| 1644 | int n = 0; |
| 1645 | struct task_struct *g, *p; |
| 1646 | |
| 1647 | read_lock(&tasklist_lock); |
| 1648 | |
| 1649 | do_each_thread(g, p) { |
| 1650 | if (p->cpuset == cs) { |
| 1651 | pidarray[n++] = p->pid; |
| 1652 | if (unlikely(n == npids)) |
| 1653 | goto array_full; |
| 1654 | } |
| 1655 | } while_each_thread(g, p); |
| 1656 | |
| 1657 | array_full: |
| 1658 | read_unlock(&tasklist_lock); |
| 1659 | return n; |
| 1660 | } |
| 1661 | |
| 1662 | static int cmppid(const void *a, const void *b) |
| 1663 | { |
| 1664 | return *(pid_t *)a - *(pid_t *)b; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * Convert array 'a' of 'npids' pid_t's to a string of newline separated |
| 1669 | * decimal pids in 'buf'. Don't write more than 'sz' chars, but return |
| 1670 | * count 'cnt' of how many chars would be written if buf were large enough. |
| 1671 | */ |
| 1672 | static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids) |
| 1673 | { |
| 1674 | int cnt = 0; |
| 1675 | int i; |
| 1676 | |
| 1677 | for (i = 0; i < npids; i++) |
| 1678 | cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]); |
| 1679 | return cnt; |
| 1680 | } |
| 1681 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1682 | /* |
| 1683 | * Handle an open on 'tasks' file. Prepare a buffer listing the |
| 1684 | * process id's of tasks currently attached to the cpuset being opened. |
| 1685 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1686 | * Does not require any specific cpuset mutexes, and does not take any. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1687 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | static int cpuset_tasks_open(struct inode *unused, struct file *file) |
| 1689 | { |
| 1690 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); |
| 1691 | struct ctr_struct *ctr; |
| 1692 | pid_t *pidarray; |
| 1693 | int npids; |
| 1694 | char c; |
| 1695 | |
| 1696 | if (!(file->f_mode & FMODE_READ)) |
| 1697 | return 0; |
| 1698 | |
| 1699 | ctr = kmalloc(sizeof(*ctr), GFP_KERNEL); |
| 1700 | if (!ctr) |
| 1701 | goto err0; |
| 1702 | |
| 1703 | /* |
| 1704 | * If cpuset gets more users after we read count, we won't have |
| 1705 | * enough space - tough. This race is indistinguishable to the |
| 1706 | * caller from the case that the additional cpuset users didn't |
| 1707 | * show up until sometime later on. |
| 1708 | */ |
| 1709 | npids = atomic_read(&cs->count); |
| 1710 | pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL); |
| 1711 | if (!pidarray) |
| 1712 | goto err1; |
| 1713 | |
| 1714 | npids = pid_array_load(pidarray, npids, cs); |
| 1715 | sort(pidarray, npids, sizeof(pid_t), cmppid, NULL); |
| 1716 | |
| 1717 | /* Call pid_array_to_buf() twice, first just to get bufsz */ |
| 1718 | ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1; |
| 1719 | ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL); |
| 1720 | if (!ctr->buf) |
| 1721 | goto err2; |
| 1722 | ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids); |
| 1723 | |
| 1724 | kfree(pidarray); |
| 1725 | file->private_data = ctr; |
| 1726 | return 0; |
| 1727 | |
| 1728 | err2: |
| 1729 | kfree(pidarray); |
| 1730 | err1: |
| 1731 | kfree(ctr); |
| 1732 | err0: |
| 1733 | return -ENOMEM; |
| 1734 | } |
| 1735 | |
| 1736 | static ssize_t cpuset_tasks_read(struct file *file, char __user *buf, |
| 1737 | size_t nbytes, loff_t *ppos) |
| 1738 | { |
| 1739 | struct ctr_struct *ctr = file->private_data; |
| 1740 | |
| 1741 | if (*ppos + nbytes > ctr->bufsz) |
| 1742 | nbytes = ctr->bufsz - *ppos; |
| 1743 | if (copy_to_user(buf, ctr->buf + *ppos, nbytes)) |
| 1744 | return -EFAULT; |
| 1745 | *ppos += nbytes; |
| 1746 | return nbytes; |
| 1747 | } |
| 1748 | |
| 1749 | static int cpuset_tasks_release(struct inode *unused_inode, struct file *file) |
| 1750 | { |
| 1751 | struct ctr_struct *ctr; |
| 1752 | |
| 1753 | if (file->f_mode & FMODE_READ) { |
| 1754 | ctr = file->private_data; |
| 1755 | kfree(ctr->buf); |
| 1756 | kfree(ctr); |
| 1757 | } |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * for the common functions, 'private' gives the type of file |
| 1763 | */ |
| 1764 | |
| 1765 | static struct cftype cft_tasks = { |
| 1766 | .name = "tasks", |
| 1767 | .open = cpuset_tasks_open, |
| 1768 | .read = cpuset_tasks_read, |
| 1769 | .release = cpuset_tasks_release, |
| 1770 | .private = FILE_TASKLIST, |
| 1771 | }; |
| 1772 | |
| 1773 | static struct cftype cft_cpus = { |
| 1774 | .name = "cpus", |
| 1775 | .private = FILE_CPULIST, |
| 1776 | }; |
| 1777 | |
| 1778 | static struct cftype cft_mems = { |
| 1779 | .name = "mems", |
| 1780 | .private = FILE_MEMLIST, |
| 1781 | }; |
| 1782 | |
| 1783 | static struct cftype cft_cpu_exclusive = { |
| 1784 | .name = "cpu_exclusive", |
| 1785 | .private = FILE_CPU_EXCLUSIVE, |
| 1786 | }; |
| 1787 | |
| 1788 | static struct cftype cft_mem_exclusive = { |
| 1789 | .name = "mem_exclusive", |
| 1790 | .private = FILE_MEM_EXCLUSIVE, |
| 1791 | }; |
| 1792 | |
| 1793 | static struct cftype cft_notify_on_release = { |
| 1794 | .name = "notify_on_release", |
| 1795 | .private = FILE_NOTIFY_ON_RELEASE, |
| 1796 | }; |
| 1797 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1798 | static struct cftype cft_memory_migrate = { |
| 1799 | .name = "memory_migrate", |
| 1800 | .private = FILE_MEMORY_MIGRATE, |
| 1801 | }; |
| 1802 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1803 | static struct cftype cft_memory_pressure_enabled = { |
| 1804 | .name = "memory_pressure_enabled", |
| 1805 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
| 1806 | }; |
| 1807 | |
| 1808 | static struct cftype cft_memory_pressure = { |
| 1809 | .name = "memory_pressure", |
| 1810 | .private = FILE_MEMORY_PRESSURE, |
| 1811 | }; |
| 1812 | |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1813 | static struct cftype cft_spread_page = { |
| 1814 | .name = "memory_spread_page", |
| 1815 | .private = FILE_SPREAD_PAGE, |
| 1816 | }; |
| 1817 | |
| 1818 | static struct cftype cft_spread_slab = { |
| 1819 | .name = "memory_spread_slab", |
| 1820 | .private = FILE_SPREAD_SLAB, |
| 1821 | }; |
| 1822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | static int cpuset_populate_dir(struct dentry *cs_dentry) |
| 1824 | { |
| 1825 | int err; |
| 1826 | |
| 1827 | if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0) |
| 1828 | return err; |
| 1829 | if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0) |
| 1830 | return err; |
| 1831 | if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0) |
| 1832 | return err; |
| 1833 | if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0) |
| 1834 | return err; |
| 1835 | if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0) |
| 1836 | return err; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1837 | if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0) |
| 1838 | return err; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1839 | if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0) |
| 1840 | return err; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1841 | if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0) |
| 1842 | return err; |
| 1843 | if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0) |
| 1844 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0) |
| 1846 | return err; |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
| 1851 | * cpuset_create - create a cpuset |
| 1852 | * parent: cpuset that will be parent of the new cpuset. |
| 1853 | * name: name of the new cpuset. Will be strcpy'ed. |
| 1854 | * mode: mode to set on new inode |
| 1855 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1856 | * Must be called with the mutex on the parent inode held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | */ |
| 1858 | |
| 1859 | static long cpuset_create(struct cpuset *parent, const char *name, int mode) |
| 1860 | { |
| 1861 | struct cpuset *cs; |
| 1862 | int err; |
| 1863 | |
| 1864 | cs = kmalloc(sizeof(*cs), GFP_KERNEL); |
| 1865 | if (!cs) |
| 1866 | return -ENOMEM; |
| 1867 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1868 | mutex_lock(&manage_mutex); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1869 | cpuset_update_task_memory_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | cs->flags = 0; |
| 1871 | if (notify_on_release(parent)) |
| 1872 | set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1873 | if (is_spread_page(parent)) |
| 1874 | set_bit(CS_SPREAD_PAGE, &cs->flags); |
| 1875 | if (is_spread_slab(parent)) |
| 1876 | set_bit(CS_SPREAD_SLAB, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | cs->cpus_allowed = CPU_MASK_NONE; |
| 1878 | cs->mems_allowed = NODE_MASK_NONE; |
| 1879 | atomic_set(&cs->count, 0); |
| 1880 | INIT_LIST_HEAD(&cs->sibling); |
| 1881 | INIT_LIST_HEAD(&cs->children); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1882 | cs->mems_generation = cpuset_mems_generation++; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1883 | fmeter_init(&cs->fmeter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | |
| 1885 | cs->parent = parent; |
| 1886 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1887 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | list_add(&cs->sibling, &cs->parent->children); |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1889 | number_of_cpusets++; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1890 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
| 1892 | err = cpuset_create_dir(cs, name, mode); |
| 1893 | if (err < 0) |
| 1894 | goto err; |
| 1895 | |
| 1896 | /* |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1897 | * Release manage_mutex before cpuset_populate_dir() because it |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1898 | * will down() this new directory's i_mutex and if we race with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | * another mkdir, we might deadlock. |
| 1900 | */ |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1901 | mutex_unlock(&manage_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | |
| 1903 | err = cpuset_populate_dir(cs->dentry); |
| 1904 | /* If err < 0, we have a half-filled directory - oh well ;) */ |
| 1905 | return 0; |
| 1906 | err: |
| 1907 | list_del(&cs->sibling); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1908 | mutex_unlock(&manage_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | kfree(cs); |
| 1910 | return err; |
| 1911 | } |
| 1912 | |
| 1913 | static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1914 | { |
| 1915 | struct cpuset *c_parent = dentry->d_parent->d_fsdata; |
| 1916 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1917 | /* the vfs holds inode->i_mutex already */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR); |
| 1919 | } |
| 1920 | |
| 1921 | static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 1922 | { |
| 1923 | struct cpuset *cs = dentry->d_fsdata; |
| 1924 | struct dentry *d; |
| 1925 | struct cpuset *parent; |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1926 | char *pathbuf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1928 | /* the vfs holds both inode->i_mutex already */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1930 | mutex_lock(&manage_mutex); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1931 | cpuset_update_task_memory_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | if (atomic_read(&cs->count) > 0) { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1933 | mutex_unlock(&manage_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | return -EBUSY; |
| 1935 | } |
| 1936 | if (!list_empty(&cs->children)) { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1937 | mutex_unlock(&manage_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | return -EBUSY; |
| 1939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | parent = cs->parent; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1941 | mutex_lock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | set_bit(CS_REMOVED, &cs->flags); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1943 | if (is_cpu_exclusive(cs)) |
| 1944 | update_cpu_domains(cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | list_del(&cs->sibling); /* delete my sibling from parent->children */ |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1946 | spin_lock(&cs->dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | d = dget(cs->dentry); |
| 1948 | cs->dentry = NULL; |
| 1949 | spin_unlock(&d->d_lock); |
| 1950 | cpuset_d_remove_dir(d); |
| 1951 | dput(d); |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1952 | number_of_cpusets--; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1953 | mutex_unlock(&callback_mutex); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1954 | if (list_empty(&parent->children)) |
| 1955 | check_for_release(parent, &pathbuf); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1956 | mutex_unlock(&manage_mutex); |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1957 | cpuset_release_agent(pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | return 0; |
| 1959 | } |
| 1960 | |
Paul Jackson | c417f02 | 2006-01-08 01:02:01 -0800 | [diff] [blame] | 1961 | /* |
| 1962 | * cpuset_init_early - just enough so that the calls to |
| 1963 | * cpuset_update_task_memory_state() in early init code |
| 1964 | * are harmless. |
| 1965 | */ |
| 1966 | |
| 1967 | int __init cpuset_init_early(void) |
| 1968 | { |
| 1969 | struct task_struct *tsk = current; |
| 1970 | |
| 1971 | tsk->cpuset = &top_cpuset; |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1972 | tsk->cpuset->mems_generation = cpuset_mems_generation++; |
Paul Jackson | c417f02 | 2006-01-08 01:02:01 -0800 | [diff] [blame] | 1973 | return 0; |
| 1974 | } |
| 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | /** |
| 1977 | * cpuset_init - initialize cpusets at system boot |
| 1978 | * |
| 1979 | * Description: Initialize top_cpuset and the cpuset internal file system, |
| 1980 | **/ |
| 1981 | |
| 1982 | int __init cpuset_init(void) |
| 1983 | { |
| 1984 | struct dentry *root; |
| 1985 | int err; |
| 1986 | |
| 1987 | top_cpuset.cpus_allowed = CPU_MASK_ALL; |
| 1988 | top_cpuset.mems_allowed = NODE_MASK_ALL; |
| 1989 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1990 | fmeter_init(&top_cpuset.fmeter); |
Paul Jackson | 151a442 | 2006-03-24 03:16:11 -0800 | [diff] [blame] | 1991 | top_cpuset.mems_generation = cpuset_mems_generation++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
| 1993 | init_task.cpuset = &top_cpuset; |
| 1994 | |
| 1995 | err = register_filesystem(&cpuset_fs_type); |
| 1996 | if (err < 0) |
| 1997 | goto out; |
| 1998 | cpuset_mount = kern_mount(&cpuset_fs_type); |
| 1999 | if (IS_ERR(cpuset_mount)) { |
| 2000 | printk(KERN_ERR "cpuset: could not mount!\n"); |
| 2001 | err = PTR_ERR(cpuset_mount); |
| 2002 | cpuset_mount = NULL; |
| 2003 | goto out; |
| 2004 | } |
| 2005 | root = cpuset_mount->mnt_sb->s_root; |
| 2006 | root->d_fsdata = &top_cpuset; |
| 2007 | root->d_inode->i_nlink++; |
| 2008 | top_cpuset.dentry = root; |
| 2009 | root->d_inode->i_op = &cpuset_dir_inode_operations; |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 2010 | number_of_cpusets = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | err = cpuset_populate_dir(root); |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2012 | /* memory_pressure_enabled is in root cpuset only */ |
| 2013 | if (err == 0) |
| 2014 | err = cpuset_add_file(root, &cft_memory_pressure_enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | out: |
| 2016 | return err; |
| 2017 | } |
| 2018 | |
| 2019 | /** |
| 2020 | * cpuset_init_smp - initialize cpus_allowed |
| 2021 | * |
| 2022 | * Description: Finish top cpuset after cpu, node maps are initialized |
| 2023 | **/ |
| 2024 | |
| 2025 | void __init cpuset_init_smp(void) |
| 2026 | { |
| 2027 | top_cpuset.cpus_allowed = cpu_online_map; |
| 2028 | top_cpuset.mems_allowed = node_online_map; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * cpuset_fork - attach newly forked task to its parents cpuset. |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 2033 | * @tsk: pointer to task_struct of forking parent process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2035 | * Description: A task inherits its parent's cpuset at fork(). |
| 2036 | * |
| 2037 | * A pointer to the shared cpuset was automatically copied in fork.c |
| 2038 | * by dup_task_struct(). However, we ignore that copy, since it was |
| 2039 | * not made under the protection of task_lock(), so might no longer be |
| 2040 | * a valid cpuset pointer. attach_task() might have already changed |
| 2041 | * current->cpuset, allowing the previously referenced cpuset to |
| 2042 | * be removed and freed. Instead, we task_lock(current) and copy |
| 2043 | * its present value of current->cpuset for our freshly forked child. |
| 2044 | * |
| 2045 | * At the point that cpuset_fork() is called, 'current' is the parent |
| 2046 | * task, and the passed argument 'child' points to the child task. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | **/ |
| 2048 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2049 | void cpuset_fork(struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2051 | task_lock(current); |
| 2052 | child->cpuset = current->cpuset; |
| 2053 | atomic_inc(&child->cpuset->count); |
| 2054 | task_unlock(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | * cpuset_exit - detach cpuset from exiting task |
| 2059 | * @tsk: pointer to task_struct of exiting process |
| 2060 | * |
| 2061 | * Description: Detach cpuset from @tsk and release it. |
| 2062 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2063 | * Note that cpusets marked notify_on_release force every task in |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2064 | * them to take the global manage_mutex mutex when exiting. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2065 | * This could impact scaling on very large systems. Be reluctant to |
| 2066 | * use notify_on_release cpusets where very high task exit scaling |
| 2067 | * is required on large systems. |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 2068 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2069 | * Don't even think about derefencing 'cs' after the cpuset use count |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2070 | * goes to zero, except inside a critical section guarded by manage_mutex |
| 2071 | * or callback_mutex. Otherwise a zero cpuset use count is a license to |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2072 | * any other task to nuke the cpuset immediately, via cpuset_rmdir(). |
| 2073 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2074 | * This routine has to take manage_mutex, not callback_mutex, because |
| 2075 | * it is holding that mutex while calling check_for_release(), |
| 2076 | * which calls kmalloc(), so can't be called holding callback_mutex(). |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2077 | * |
| 2078 | * We don't need to task_lock() this reference to tsk->cpuset, |
| 2079 | * because tsk is already marked PF_EXITING, so attach_task() won't |
Paul Jackson | b4b2641 | 2006-01-08 01:01:53 -0800 | [diff] [blame] | 2080 | * mess with it, or task is a failed fork, never visible to attach_task. |
Paul Jackson | 06fed33 | 2006-02-15 15:17:38 -0800 | [diff] [blame] | 2081 | * |
Paul Jackson | 8488bc3 | 2006-03-24 03:16:10 -0800 | [diff] [blame] | 2082 | * the_top_cpuset_hack: |
Paul Jackson | 06fed33 | 2006-02-15 15:17:38 -0800 | [diff] [blame] | 2083 | * |
| 2084 | * Set the exiting tasks cpuset to the root cpuset (top_cpuset). |
| 2085 | * |
| 2086 | * Don't leave a task unable to allocate memory, as that is an |
| 2087 | * accident waiting to happen should someone add a callout in |
| 2088 | * do_exit() after the cpuset_exit() call that might allocate. |
| 2089 | * If a task tries to allocate memory with an invalid cpuset, |
| 2090 | * it will oops in cpuset_update_task_memory_state(). |
| 2091 | * |
| 2092 | * We call cpuset_exit() while the task is still competent to |
| 2093 | * handle notify_on_release(), then leave the task attached to |
| 2094 | * the root cpuset (top_cpuset) for the remainder of its exit. |
| 2095 | * |
| 2096 | * To do this properly, we would increment the reference count on |
| 2097 | * top_cpuset, and near the very end of the kernel/exit.c do_exit() |
| 2098 | * code we would add a second cpuset function call, to drop that |
| 2099 | * reference. This would just create an unnecessary hot spot on |
| 2100 | * the top_cpuset reference count, to no avail. |
| 2101 | * |
| 2102 | * Normally, holding a reference to a cpuset without bumping its |
| 2103 | * count is unsafe. The cpuset could go away, or someone could |
| 2104 | * attach us to a different cpuset, decrementing the count on |
| 2105 | * the first cpuset that we never incremented. But in this case, |
| 2106 | * top_cpuset isn't going away, and either task has PF_EXITING set, |
| 2107 | * which wards off any attach_task() attempts, or task is a failed |
| 2108 | * fork, never visible to attach_task. |
| 2109 | * |
| 2110 | * Another way to do this would be to set the cpuset pointer |
| 2111 | * to NULL here, and check in cpuset_update_task_memory_state() |
| 2112 | * for a NULL pointer. This hack avoids that NULL check, for no |
| 2113 | * cost (other than this way too long comment ;). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | **/ |
| 2115 | |
| 2116 | void cpuset_exit(struct task_struct *tsk) |
| 2117 | { |
| 2118 | struct cpuset *cs; |
| 2119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | cs = tsk->cpuset; |
Paul Jackson | 8488bc3 | 2006-03-24 03:16:10 -0800 | [diff] [blame] | 2121 | tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 2123 | if (notify_on_release(cs)) { |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 2124 | char *pathbuf = NULL; |
| 2125 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2126 | mutex_lock(&manage_mutex); |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 2127 | if (atomic_dec_and_test(&cs->count)) |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 2128 | check_for_release(cs, &pathbuf); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2129 | mutex_unlock(&manage_mutex); |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 2130 | cpuset_release_agent(pathbuf); |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 2131 | } else { |
| 2132 | atomic_dec(&cs->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | /** |
| 2137 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
| 2138 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. |
| 2139 | * |
| 2140 | * Description: Returns the cpumask_t cpus_allowed of the cpuset |
| 2141 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 2142 | * subset of cpu_online_map, even if this means going outside the |
| 2143 | * tasks cpuset. |
| 2144 | **/ |
| 2145 | |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2146 | cpumask_t cpuset_cpus_allowed(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | { |
| 2148 | cpumask_t mask; |
| 2149 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2150 | mutex_lock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2151 | task_lock(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | guarantee_online_cpus(tsk->cpuset, &mask); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2153 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2154 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
| 2156 | return mask; |
| 2157 | } |
| 2158 | |
| 2159 | void cpuset_init_current_mems_allowed(void) |
| 2160 | { |
| 2161 | current->mems_allowed = NODE_MASK_ALL; |
| 2162 | } |
| 2163 | |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 2164 | /** |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2165 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. |
| 2166 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. |
| 2167 | * |
| 2168 | * Description: Returns the nodemask_t mems_allowed of the cpuset |
| 2169 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 2170 | * subset of node_online_map, even if this means going outside the |
| 2171 | * tasks cpuset. |
| 2172 | **/ |
| 2173 | |
| 2174 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) |
| 2175 | { |
| 2176 | nodemask_t mask; |
| 2177 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2178 | mutex_lock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2179 | task_lock(tsk); |
| 2180 | guarantee_online_mems(tsk->cpuset, &mask); |
| 2181 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2182 | mutex_unlock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2183 | |
| 2184 | return mask; |
| 2185 | } |
| 2186 | |
| 2187 | /** |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 2188 | * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed |
| 2189 | * @zl: the zonelist to be checked |
| 2190 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | * Are any of the nodes on zonelist zl allowed in current->mems_allowed? |
| 2192 | */ |
| 2193 | int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl) |
| 2194 | { |
| 2195 | int i; |
| 2196 | |
| 2197 | for (i = 0; zl->zones[i]; i++) { |
| 2198 | int nid = zl->zones[i]->zone_pgdat->node_id; |
| 2199 | |
| 2200 | if (node_isset(nid, current->mems_allowed)) |
| 2201 | return 1; |
| 2202 | } |
| 2203 | return 0; |
| 2204 | } |
| 2205 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2206 | /* |
| 2207 | * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2208 | * ancestor to the specified cpuset. Call holding callback_mutex. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2209 | * If no ancestor is mem_exclusive (an unusual configuration), then |
| 2210 | * returns the root cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2212 | static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | { |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2214 | while (!is_mem_exclusive(cs) && cs->parent) |
| 2215 | cs = cs->parent; |
| 2216 | return cs; |
| 2217 | } |
| 2218 | |
| 2219 | /** |
| 2220 | * cpuset_zone_allowed - Can we allocate memory on zone z's memory node? |
| 2221 | * @z: is this zone on an allowed node? |
| 2222 | * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL) |
| 2223 | * |
| 2224 | * If we're in interrupt, yes, we can always allocate. If zone |
| 2225 | * z's node is in our tasks mems_allowed, yes. If it's not a |
| 2226 | * __GFP_HARDWALL request and this zone's nodes is in the nearest |
| 2227 | * mem_exclusive cpuset ancestor to this tasks cpuset, yes. |
| 2228 | * Otherwise, no. |
| 2229 | * |
| 2230 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
| 2231 | * and do not allow allocations outside the current tasks cpuset. |
| 2232 | * GFP_KERNEL allocations are not so marked, so can escape to the |
| 2233 | * nearest mem_exclusive ancestor cpuset. |
| 2234 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2235 | * Scanning up parent cpusets requires callback_mutex. The __alloc_pages() |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2236 | * routine only calls here with __GFP_HARDWALL bit _not_ set if |
| 2237 | * it's a GFP_KERNEL allocation, and all nodes in the current tasks |
| 2238 | * mems_allowed came up empty on the first pass over the zonelist. |
| 2239 | * So only GFP_KERNEL allocations, if all nodes in the cpuset are |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2240 | * short of memory, might require taking the callback_mutex mutex. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2241 | * |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2242 | * The first call here from mm/page_alloc:get_page_from_freelist() |
| 2243 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, so |
| 2244 | * no allocation on a node outside the cpuset is allowed (unless in |
| 2245 | * interrupt, of course). |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2246 | * |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2247 | * The second pass through get_page_from_freelist() doesn't even call |
| 2248 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() |
| 2249 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set |
| 2250 | * in alloc_flags. That logic and the checks below have the combined |
| 2251 | * affect that: |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2252 | * in_interrupt - any node ok (current task context irrelevant) |
| 2253 | * GFP_ATOMIC - any node ok |
| 2254 | * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok |
| 2255 | * GFP_USER - only nodes in current tasks mems allowed ok. |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2256 | * |
| 2257 | * Rule: |
| 2258 | * Don't call cpuset_zone_allowed() if you can't sleep, unless you |
| 2259 | * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables |
| 2260 | * the code that might scan up ancestor cpusets and sleep. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2261 | **/ |
| 2262 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 2263 | int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2264 | { |
| 2265 | int node; /* node that zone z is on */ |
| 2266 | const struct cpuset *cs; /* current cpuset ancestors */ |
Paul Jackson | 29afd49 | 2006-03-24 03:16:12 -0800 | [diff] [blame] | 2267 | int allowed; /* is allocation in zone z allowed? */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2268 | |
| 2269 | if (in_interrupt()) |
| 2270 | return 1; |
| 2271 | node = z->zone_pgdat->node_id; |
Paul Jackson | 92d1dbd | 2006-05-20 15:00:11 -0700 | [diff] [blame] | 2272 | might_sleep_if(!(gfp_mask & __GFP_HARDWALL)); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2273 | if (node_isset(node, current->mems_allowed)) |
| 2274 | return 1; |
| 2275 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
| 2276 | return 0; |
| 2277 | |
Bob Picco | 5563e77 | 2005-11-13 16:06:35 -0800 | [diff] [blame] | 2278 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 2279 | return 1; |
| 2280 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2281 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2282 | mutex_lock(&callback_mutex); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2283 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2284 | task_lock(current); |
| 2285 | cs = nearest_exclusive_ancestor(current->cpuset); |
| 2286 | task_unlock(current); |
| 2287 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2288 | allowed = node_isset(node, cs->mems_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2289 | mutex_unlock(&callback_mutex); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2290 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2293 | /** |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 2294 | * cpuset_lock - lock out any changes to cpuset structures |
| 2295 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2296 | * The out of memory (oom) code needs to mutex_lock cpusets |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 2297 | * from being changed while it scans the tasklist looking for a |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2298 | * task in an overlapping cpuset. Expose callback_mutex via this |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 2299 | * cpuset_lock() routine, so the oom code can lock it, before |
| 2300 | * locking the task list. The tasklist_lock is a spinlock, so |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2301 | * must be taken inside callback_mutex. |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 2302 | */ |
| 2303 | |
| 2304 | void cpuset_lock(void) |
| 2305 | { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2306 | mutex_lock(&callback_mutex); |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /** |
| 2310 | * cpuset_unlock - release lock on cpuset changes |
| 2311 | * |
| 2312 | * Undo the lock taken in a previous cpuset_lock() call. |
| 2313 | */ |
| 2314 | |
| 2315 | void cpuset_unlock(void) |
| 2316 | { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2317 | mutex_unlock(&callback_mutex); |
Paul Jackson | 505970b | 2006-01-14 13:21:06 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /** |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 2321 | * cpuset_mem_spread_node() - On which node to begin search for a page |
| 2322 | * |
| 2323 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for |
| 2324 | * tasks in a cpuset with is_spread_page or is_spread_slab set), |
| 2325 | * and if the memory allocation used cpuset_mem_spread_node() |
| 2326 | * to determine on which node to start looking, as it will for |
| 2327 | * certain page cache or slab cache pages such as used for file |
| 2328 | * system buffers and inode caches, then instead of starting on the |
| 2329 | * local node to look for a free page, rather spread the starting |
| 2330 | * node around the tasks mems_allowed nodes. |
| 2331 | * |
| 2332 | * We don't have to worry about the returned node being offline |
| 2333 | * because "it can't happen", and even if it did, it would be ok. |
| 2334 | * |
| 2335 | * The routines calling guarantee_online_mems() are careful to |
| 2336 | * only set nodes in task->mems_allowed that are online. So it |
| 2337 | * should not be possible for the following code to return an |
| 2338 | * offline node. But if it did, that would be ok, as this routine |
| 2339 | * is not returning the node where the allocation must be, only |
| 2340 | * the node where the search should start. The zonelist passed to |
| 2341 | * __alloc_pages() will include all nodes. If the slab allocator |
| 2342 | * is passed an offline node, it will fall back to the local node. |
| 2343 | * See kmem_cache_alloc_node(). |
| 2344 | */ |
| 2345 | |
| 2346 | int cpuset_mem_spread_node(void) |
| 2347 | { |
| 2348 | int node; |
| 2349 | |
| 2350 | node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed); |
| 2351 | if (node == MAX_NUMNODES) |
| 2352 | node = first_node(current->mems_allowed); |
| 2353 | current->cpuset_mem_spread_rotor = node; |
| 2354 | return node; |
| 2355 | } |
| 2356 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); |
| 2357 | |
| 2358 | /** |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2359 | * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors? |
| 2360 | * @p: pointer to task_struct of some other task. |
| 2361 | * |
| 2362 | * Description: Return true if the nearest mem_exclusive ancestor |
| 2363 | * cpusets of tasks @p and current overlap. Used by oom killer to |
| 2364 | * determine if task @p's memory usage might impact the memory |
| 2365 | * available to the current task. |
| 2366 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2367 | * Call while holding callback_mutex. |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2368 | **/ |
| 2369 | |
| 2370 | int cpuset_excl_nodes_overlap(const struct task_struct *p) |
| 2371 | { |
| 2372 | const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */ |
| 2373 | int overlap = 0; /* do cpusets overlap? */ |
| 2374 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2375 | task_lock(current); |
| 2376 | if (current->flags & PF_EXITING) { |
| 2377 | task_unlock(current); |
| 2378 | goto done; |
| 2379 | } |
| 2380 | cs1 = nearest_exclusive_ancestor(current->cpuset); |
| 2381 | task_unlock(current); |
| 2382 | |
| 2383 | task_lock((struct task_struct *)p); |
| 2384 | if (p->flags & PF_EXITING) { |
| 2385 | task_unlock((struct task_struct *)p); |
| 2386 | goto done; |
| 2387 | } |
| 2388 | cs2 = nearest_exclusive_ancestor(p->cpuset); |
| 2389 | task_unlock((struct task_struct *)p); |
| 2390 | |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2391 | overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed); |
| 2392 | done: |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2393 | return overlap; |
| 2394 | } |
| 2395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2397 | * Collection of memory_pressure is suppressed unless |
| 2398 | * this flag is enabled by writing "1" to the special |
| 2399 | * cpuset file 'memory_pressure_enabled' in the root cpuset. |
| 2400 | */ |
| 2401 | |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 2402 | int cpuset_memory_pressure_enabled __read_mostly; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2403 | |
| 2404 | /** |
| 2405 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. |
| 2406 | * |
| 2407 | * Keep a running average of the rate of synchronous (direct) |
| 2408 | * page reclaim efforts initiated by tasks in each cpuset. |
| 2409 | * |
| 2410 | * This represents the rate at which some task in the cpuset |
| 2411 | * ran low on memory on all nodes it was allowed to use, and |
| 2412 | * had to enter the kernels page reclaim code in an effort to |
| 2413 | * create more free memory by tossing clean pages or swapping |
| 2414 | * or writing dirty pages. |
| 2415 | * |
| 2416 | * Display to user space in the per-cpuset read-only file |
| 2417 | * "memory_pressure". Value displayed is an integer |
| 2418 | * representing the recent rate of entry into the synchronous |
| 2419 | * (direct) page reclaim by any task attached to the cpuset. |
| 2420 | **/ |
| 2421 | |
| 2422 | void __cpuset_memory_pressure_bump(void) |
| 2423 | { |
| 2424 | struct cpuset *cs; |
| 2425 | |
| 2426 | task_lock(current); |
| 2427 | cs = current->cpuset; |
| 2428 | fmeter_markevent(&cs->fmeter); |
| 2429 | task_unlock(current); |
| 2430 | } |
| 2431 | |
| 2432 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | * proc_cpuset_show() |
| 2434 | * - Print tasks cpuset path into seq_file. |
| 2435 | * - Used for /proc/<pid>/cpuset. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2436 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
| 2437 | * doesn't really matter if tsk->cpuset changes after we read it, |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2438 | * and we take manage_mutex, keeping attach_task() from changing it |
Paul Jackson | 8488bc3 | 2006-03-24 03:16:10 -0800 | [diff] [blame] | 2439 | * anyway. No need to check that tsk->cpuset != NULL, thanks to |
| 2440 | * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks |
| 2441 | * cpuset to top_cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | static int proc_cpuset_show(struct seq_file *m, void *v) |
| 2444 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | struct task_struct *tsk; |
| 2446 | char *buf; |
| 2447 | int retval = 0; |
| 2448 | |
| 2449 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2450 | if (!buf) |
| 2451 | return -ENOMEM; |
| 2452 | |
| 2453 | tsk = m->private; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2454 | mutex_lock(&manage_mutex); |
Paul Jackson | 8488bc3 | 2006-03-24 03:16:10 -0800 | [diff] [blame] | 2455 | retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | if (retval < 0) |
| 2457 | goto out; |
| 2458 | seq_puts(m, buf); |
| 2459 | seq_putc(m, '\n'); |
| 2460 | out: |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2461 | mutex_unlock(&manage_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | kfree(buf); |
| 2463 | return retval; |
| 2464 | } |
| 2465 | |
| 2466 | static int cpuset_open(struct inode *inode, struct file *file) |
| 2467 | { |
| 2468 | struct task_struct *tsk = PROC_I(inode)->task; |
| 2469 | return single_open(file, proc_cpuset_show, tsk); |
| 2470 | } |
| 2471 | |
| 2472 | struct file_operations proc_cpuset_operations = { |
| 2473 | .open = cpuset_open, |
| 2474 | .read = seq_read, |
| 2475 | .llseek = seq_lseek, |
| 2476 | .release = single_release, |
| 2477 | }; |
| 2478 | |
| 2479 | /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ |
| 2480 | char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) |
| 2481 | { |
| 2482 | buffer += sprintf(buffer, "Cpus_allowed:\t"); |
| 2483 | buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed); |
| 2484 | buffer += sprintf(buffer, "\n"); |
| 2485 | buffer += sprintf(buffer, "Mems_allowed:\t"); |
| 2486 | buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed); |
| 2487 | buffer += sprintf(buffer, "\n"); |
| 2488 | return buffer; |
| 2489 | } |