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