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> |
| 42 | #include <linux/sched.h> |
| 43 | #include <linux/seq_file.h> |
| 44 | #include <linux/slab.h> |
| 45 | #include <linux/smp_lock.h> |
| 46 | #include <linux/spinlock.h> |
| 47 | #include <linux/stat.h> |
| 48 | #include <linux/string.h> |
| 49 | #include <linux/time.h> |
| 50 | #include <linux/backing-dev.h> |
| 51 | #include <linux/sort.h> |
| 52 | |
| 53 | #include <asm/uaccess.h> |
| 54 | #include <asm/atomic.h> |
| 55 | #include <asm/semaphore.h> |
| 56 | |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 57 | #define CPUSET_SUPER_MAGIC 0x27e0eb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Tracks how many cpusets are currently defined in system. |
| 61 | * When there is only one cpuset (the root cpuset) we can |
| 62 | * short circuit some hooks. |
| 63 | */ |
| 64 | int number_of_cpusets; |
| 65 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 66 | /* See "Frequency meter" comments, below. */ |
| 67 | |
| 68 | struct fmeter { |
| 69 | int cnt; /* unprocessed events count */ |
| 70 | int val; /* most recent output value */ |
| 71 | time_t time; /* clock (secs) when val computed */ |
| 72 | spinlock_t lock; /* guards read or write of above */ |
| 73 | }; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | struct cpuset { |
| 76 | unsigned long flags; /* "unsigned long" so bitops work */ |
| 77 | cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ |
| 78 | nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ |
| 79 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Count is atomic so can incr (fork) or decr (exit) without a lock. |
| 82 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | atomic_t count; /* count tasks using this cpuset */ |
| 84 | |
| 85 | /* |
| 86 | * We link our 'sibling' struct into our parents 'children'. |
| 87 | * Our children link their 'sibling' into our 'children'. |
| 88 | */ |
| 89 | struct list_head sibling; /* my parents children */ |
| 90 | struct list_head children; /* my children */ |
| 91 | |
| 92 | struct cpuset *parent; /* my parent */ |
| 93 | struct dentry *dentry; /* cpuset fs entry */ |
| 94 | |
| 95 | /* |
| 96 | * Copy of global cpuset_mems_generation as of the most |
| 97 | * recent time this cpuset changed its mems_allowed. |
| 98 | */ |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 99 | int mems_generation; |
| 100 | |
| 101 | struct fmeter fmeter; /* memory_pressure filter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* bits in struct cpuset flags field */ |
| 105 | typedef enum { |
| 106 | CS_CPU_EXCLUSIVE, |
| 107 | CS_MEM_EXCLUSIVE, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 108 | CS_MEMORY_MIGRATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | CS_REMOVED, |
| 110 | CS_NOTIFY_ON_RELEASE |
| 111 | } cpuset_flagbits_t; |
| 112 | |
| 113 | /* convenient tests for these bits */ |
| 114 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
| 115 | { |
| 116 | return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
| 117 | } |
| 118 | |
| 119 | static inline int is_mem_exclusive(const struct cpuset *cs) |
| 120 | { |
| 121 | return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
| 122 | } |
| 123 | |
| 124 | static inline int is_removed(const struct cpuset *cs) |
| 125 | { |
| 126 | return !!test_bit(CS_REMOVED, &cs->flags); |
| 127 | } |
| 128 | |
| 129 | static inline int notify_on_release(const struct cpuset *cs) |
| 130 | { |
| 131 | return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); |
| 132 | } |
| 133 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 134 | static inline int is_memory_migrate(const struct cpuset *cs) |
| 135 | { |
| 136 | return !!test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
| 137 | } |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Increment this atomic integer everytime any cpuset changes its |
| 141 | * mems_allowed value. Users of cpusets can track this generation |
| 142 | * number, and avoid having to lock and reload mems_allowed unless |
| 143 | * the cpuset they're using changes generation. |
| 144 | * |
| 145 | * A single, global generation is needed because attach_task() could |
| 146 | * reattach a task to a different cpuset, which must not have its |
| 147 | * generation numbers aliased with those of that tasks previous cpuset. |
| 148 | * |
| 149 | * Generations are needed for mems_allowed because one task cannot |
| 150 | * modify anothers memory placement. So we must enable every task, |
| 151 | * on every visit to __alloc_pages(), to efficiently check whether |
| 152 | * its current->cpuset->mems_allowed has changed, requiring an update |
| 153 | * of its current->mems_allowed. |
| 154 | */ |
| 155 | static atomic_t cpuset_mems_generation = ATOMIC_INIT(1); |
| 156 | |
| 157 | static struct cpuset top_cpuset = { |
| 158 | .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)), |
| 159 | .cpus_allowed = CPU_MASK_ALL, |
| 160 | .mems_allowed = NODE_MASK_ALL, |
| 161 | .count = ATOMIC_INIT(0), |
| 162 | .sibling = LIST_HEAD_INIT(top_cpuset.sibling), |
| 163 | .children = LIST_HEAD_INIT(top_cpuset.children), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | static struct vfsmount *cpuset_mount; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 167 | static struct super_block *cpuset_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 170 | * We have two global cpuset semaphores below. They can nest. |
| 171 | * It is ok to first take manage_sem, then nest callback_sem. We also |
| 172 | * require taking task_lock() when dereferencing a tasks cpuset pointer. |
| 173 | * See "The task_lock() exception", at the end of this comment. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 175 | * A task must hold both semaphores to modify cpusets. If a task |
| 176 | * holds manage_sem, then it blocks others wanting that semaphore, |
| 177 | * ensuring that it is the only task able to also acquire callback_sem |
| 178 | * and be able to modify cpusets. It can perform various checks on |
| 179 | * the cpuset structure first, knowing nothing will change. It can |
| 180 | * also allocate memory while just holding manage_sem. While it is |
| 181 | * performing these checks, various callback routines can briefly |
| 182 | * acquire callback_sem to query cpusets. Once it is ready to make |
| 183 | * the changes, it takes callback_sem, blocking everyone else. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 185 | * Calls to the kernel memory allocator can not be made while holding |
| 186 | * callback_sem, as that would risk double tripping on callback_sem |
| 187 | * from one of the callbacks into the cpuset code from within |
| 188 | * __alloc_pages(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 190 | * If a task is only holding callback_sem, then it has read-only |
| 191 | * access to cpusets. |
| 192 | * |
| 193 | * The task_struct fields mems_allowed and mems_generation may only |
| 194 | * be accessed in the context of that task, so require no locks. |
| 195 | * |
| 196 | * Any task can increment and decrement the count field without lock. |
| 197 | * So in general, code holding manage_sem or callback_sem can't rely |
| 198 | * on the count field not changing. However, if the count goes to |
| 199 | * zero, then only attach_task(), which holds both semaphores, can |
| 200 | * increment it again. Because a count of zero means that no tasks |
| 201 | * are currently attached, therefore there is no way a task attached |
| 202 | * to that cpuset can fork (the other way to increment the count). |
| 203 | * So code holding manage_sem or callback_sem can safely assume that |
| 204 | * if the count is zero, it will stay zero. Similarly, if a task |
| 205 | * holds manage_sem or callback_sem on a cpuset with zero count, it |
| 206 | * knows that the cpuset won't be removed, as cpuset_rmdir() needs |
| 207 | * both of those semaphores. |
| 208 | * |
| 209 | * A possible optimization to improve parallelism would be to make |
| 210 | * callback_sem a R/W semaphore (rwsem), allowing the callback routines |
| 211 | * to proceed in parallel, with read access, until the holder of |
| 212 | * manage_sem needed to take this rwsem for exclusive write access |
| 213 | * and modify some cpusets. |
| 214 | * |
| 215 | * The cpuset_common_file_write handler for operations that modify |
| 216 | * the cpuset hierarchy holds manage_sem across the entire operation, |
| 217 | * single threading all such cpuset modifications across the system. |
| 218 | * |
| 219 | * The cpuset_common_file_read() handlers only hold callback_sem across |
| 220 | * small pieces of code, such as when reading out possibly multi-word |
| 221 | * cpumasks and nodemasks. |
| 222 | * |
| 223 | * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't |
| 224 | * (usually) take either semaphore. These are the two most performance |
| 225 | * critical pieces of code here. The exception occurs on cpuset_exit(), |
| 226 | * when a task in a notify_on_release cpuset exits. Then manage_sem |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 227 | * 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] | 228 | * to /sbin/cpuset_release_agent with the name of the cpuset (path |
| 229 | * relative to the root of cpuset file system) as the argument. |
| 230 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 231 | * A cpuset can only be deleted if both its 'count' of using tasks |
| 232 | * is zero, and its list of 'children' cpusets is empty. Since all |
| 233 | * tasks in the system use _some_ cpuset, and since there is always at |
| 234 | * least one task in the system (init, pid == 1), therefore, top_cpuset |
| 235 | * always has either children cpusets and/or using tasks. So we don't |
| 236 | * need a special hack to ensure that top_cpuset cannot be deleted. |
| 237 | * |
| 238 | * The above "Tale of Two Semaphores" would be complete, but for: |
| 239 | * |
| 240 | * The task_lock() exception |
| 241 | * |
| 242 | * The need for this exception arises from the action of attach_task(), |
| 243 | * which overwrites one tasks cpuset pointer with another. It does |
| 244 | * so using both semaphores, however there are several performance |
| 245 | * critical places that need to reference task->cpuset without the |
| 246 | * expense of grabbing a system global semaphore. Therefore except as |
| 247 | * noted below, when dereferencing or, as in attach_task(), modifying |
| 248 | * a tasks cpuset pointer we use task_lock(), which acts on a spinlock |
| 249 | * (task->alloc_lock) already in the task_struct routinely used for |
| 250 | * such matters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | */ |
| 252 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 253 | static DECLARE_MUTEX(manage_sem); |
| 254 | static DECLARE_MUTEX(callback_sem); |
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) { |
| 328 | struct dentry *d = list_entry(node, struct dentry, d_child); |
| 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 | } |
| 340 | list_del_init(&dentry->d_child); |
| 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 | /* |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 429 | * Call with manage_sem 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 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 481 | * When we had only one cpuset semaphore, we had to call this |
| 482 | * without holding it, to avoid deadlock when call_usermodehelper() |
| 483 | * allocated memory. With two locks, we could now call this while |
| 484 | * holding manage_sem, but we still don't, so as to minimize |
| 485 | * the time manage_sem 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 |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 517 | * cpuset_release_agent() with it later on, once manage_sem is dropped. |
| 518 | * Call here with manage_sem 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 |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 524 | * calling check_for_release() with manage_sem held and the address |
| 525 | * of the pathbuf pointer, then dropping manage_sem, 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 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 556 | * Call with callback_sem 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 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 580 | * Call with callback_sem 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 | * |
| 605 | * Call without callback_sem or task_lock() held. May be called |
| 606 | * with or without manage_sem held. Except in early boot or |
| 607 | * an exiting task, when tsk->cpuset is NULL, this routine will |
| 608 | * acquire task_lock(). We don't need to use task_lock to guard |
| 609 | * against another task changing a non-NULL cpuset pointer to NULL, |
| 610 | * as that is only done by a task on itself, and if the current task |
| 611 | * is here, it is not simultaneously in the exit code NULL'ing its |
| 612 | * cpuset pointer. This routine also might acquire callback_sem and |
| 613 | * current->mm->mmap_sem during call. |
Paul Jackson | 5aa15b5 | 2005-10-30 15:02:28 -0800 | [diff] [blame] | 614 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 615 | * The task_lock() is required to dereference current->cpuset safely. |
| 616 | * Without it, we could pick up the pointer value of current->cpuset |
| 617 | * in one instruction, and then attach_task could give us a different |
| 618 | * cpuset, and then the cpuset we had could be removed and freed, |
| 619 | * and then on our next instruction, we could dereference a no longer |
| 620 | * valid cpuset pointer to get its mems_generation field. |
| 621 | * |
| 622 | * This routine is needed to update the per-task mems_allowed data, |
| 623 | * within the tasks context, when it is trying to allocate memory |
| 624 | * (in various mm/mempolicy.c routines) and notices that some other |
| 625 | * task has been modifying its cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | */ |
| 627 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 628 | void cpuset_update_task_memory_state() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 630 | int my_cpusets_mem_gen; |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 631 | struct task_struct *tsk = current; |
| 632 | struct cpuset *cs = tsk->cpuset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 634 | if (unlikely(!cs)) |
| 635 | return; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 636 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 637 | task_lock(tsk); |
| 638 | my_cpusets_mem_gen = cs->mems_generation; |
| 639 | task_unlock(tsk); |
| 640 | |
| 641 | if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) { |
| 642 | nodemask_t oldmem = tsk->mems_allowed; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 643 | int migrate; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 644 | |
| 645 | down(&callback_sem); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 646 | task_lock(tsk); |
| 647 | cs = tsk->cpuset; /* Maybe changed when task not locked */ |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 648 | migrate = is_memory_migrate(cs); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 649 | guarantee_online_mems(cs, &tsk->mems_allowed); |
| 650 | tsk->cpuset_mems_generation = cs->mems_generation; |
| 651 | task_unlock(tsk); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 652 | up(&callback_sem); |
Paul Jackson | 74cb215 | 2006-01-08 01:01:56 -0800 | [diff] [blame] | 653 | mpol_rebind_task(tsk, &tsk->mems_allowed); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 654 | if (!nodes_equal(oldmem, tsk->mems_allowed)) { |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 655 | if (migrate) { |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 656 | do_migrate_pages(tsk->mm, &oldmem, |
| 657 | &tsk->mems_allowed, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 658 | MPOL_MF_MOVE_ALL); |
| 659 | } |
| 660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? |
| 666 | * |
| 667 | * One cpuset is a subset of another if all its allowed CPUs and |
| 668 | * Memory Nodes are a subset of the other, and its exclusive flags |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 669 | * are only set if the other's are set. Call holding manage_sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | */ |
| 671 | |
| 672 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) |
| 673 | { |
| 674 | return cpus_subset(p->cpus_allowed, q->cpus_allowed) && |
| 675 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
| 676 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && |
| 677 | is_mem_exclusive(p) <= is_mem_exclusive(q); |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * validate_change() - Used to validate that any proposed cpuset change |
| 682 | * follows the structural rules for cpusets. |
| 683 | * |
| 684 | * If we replaced the flag and mask values of the current cpuset |
| 685 | * (cur) with those values in the trial cpuset (trial), would |
| 686 | * our various subset and exclusive rules still be valid? Presumes |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 687 | * manage_sem held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | * |
| 689 | * 'cur' is the address of an actual, in-use cpuset. Operations |
| 690 | * such as list traversal that depend on the actual address of the |
| 691 | * cpuset in the list must use cur below, not trial. |
| 692 | * |
| 693 | * 'trial' is the address of bulk structure copy of cur, with |
| 694 | * perhaps one or more of the fields cpus_allowed, mems_allowed, |
| 695 | * or flags changed to new, trial values. |
| 696 | * |
| 697 | * Return 0 if valid, -errno if not. |
| 698 | */ |
| 699 | |
| 700 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) |
| 701 | { |
| 702 | struct cpuset *c, *par; |
| 703 | |
| 704 | /* Each of our child cpusets must be a subset of us */ |
| 705 | list_for_each_entry(c, &cur->children, sibling) { |
| 706 | if (!is_cpuset_subset(c, trial)) |
| 707 | return -EBUSY; |
| 708 | } |
| 709 | |
| 710 | /* Remaining checks don't apply to root cpuset */ |
| 711 | if ((par = cur->parent) == NULL) |
| 712 | return 0; |
| 713 | |
| 714 | /* We must be a subset of our parent cpuset */ |
| 715 | if (!is_cpuset_subset(trial, par)) |
| 716 | return -EACCES; |
| 717 | |
| 718 | /* If either I or some sibling (!= me) is exclusive, we can't overlap */ |
| 719 | list_for_each_entry(c, &par->children, sibling) { |
| 720 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
| 721 | c != cur && |
| 722 | cpus_intersects(trial->cpus_allowed, c->cpus_allowed)) |
| 723 | return -EINVAL; |
| 724 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
| 725 | c != cur && |
| 726 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 733 | /* |
| 734 | * For a given cpuset cur, partition the system as follows |
| 735 | * a. All cpus in the parent cpuset's cpus_allowed that are not part of any |
| 736 | * exclusive child cpusets |
| 737 | * b. All cpus in the current cpuset's cpus_allowed that are not part of any |
| 738 | * exclusive child cpusets |
| 739 | * Build these two partitions by calling partition_sched_domains |
| 740 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 741 | * Call with manage_sem held. May nest a call to the |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 742 | * lock_cpu_hotplug()/unlock_cpu_hotplug() pair. |
| 743 | */ |
Paul Jackson | 212d6d2 | 2005-08-25 12:47:56 -0700 | [diff] [blame] | 744 | |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 745 | static void update_cpu_domains(struct cpuset *cur) |
| 746 | { |
| 747 | struct cpuset *c, *par = cur->parent; |
| 748 | cpumask_t pspan, cspan; |
| 749 | |
| 750 | if (par == NULL || cpus_empty(cur->cpus_allowed)) |
| 751 | return; |
| 752 | |
| 753 | /* |
| 754 | * Get all cpus from parent's cpus_allowed not part of exclusive |
| 755 | * children |
| 756 | */ |
| 757 | pspan = par->cpus_allowed; |
| 758 | list_for_each_entry(c, &par->children, sibling) { |
| 759 | if (is_cpu_exclusive(c)) |
| 760 | cpus_andnot(pspan, pspan, c->cpus_allowed); |
| 761 | } |
| 762 | if (is_removed(cur) || !is_cpu_exclusive(cur)) { |
| 763 | cpus_or(pspan, pspan, cur->cpus_allowed); |
| 764 | if (cpus_equal(pspan, cur->cpus_allowed)) |
| 765 | return; |
| 766 | cspan = CPU_MASK_NONE; |
| 767 | } else { |
| 768 | if (cpus_empty(pspan)) |
| 769 | return; |
| 770 | cspan = cur->cpus_allowed; |
| 771 | /* |
| 772 | * Get all cpus from current cpuset's cpus_allowed not part |
| 773 | * of exclusive children |
| 774 | */ |
| 775 | list_for_each_entry(c, &cur->children, sibling) { |
| 776 | if (is_cpu_exclusive(c)) |
| 777 | cpus_andnot(cspan, cspan, c->cpus_allowed); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | lock_cpu_hotplug(); |
| 782 | partition_sched_domains(&pspan, &cspan); |
| 783 | unlock_cpu_hotplug(); |
| 784 | } |
| 785 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 786 | /* |
| 787 | * Call with manage_sem held. May take callback_sem during call. |
| 788 | */ |
| 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | static int update_cpumask(struct cpuset *cs, char *buf) |
| 791 | { |
| 792 | struct cpuset trialcs; |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 793 | int retval, cpus_unchanged; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | trialcs = *cs; |
| 796 | retval = cpulist_parse(buf, trialcs.cpus_allowed); |
| 797 | if (retval < 0) |
| 798 | return retval; |
| 799 | cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map); |
| 800 | if (cpus_empty(trialcs.cpus_allowed)) |
| 801 | return -ENOSPC; |
| 802 | retval = validate_change(cs, &trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 803 | if (retval < 0) |
| 804 | return retval; |
| 805 | cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 806 | down(&callback_sem); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 807 | cs->cpus_allowed = trialcs.cpus_allowed; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 808 | up(&callback_sem); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 809 | if (is_cpu_exclusive(cs) && !cpus_unchanged) |
| 810 | update_cpu_domains(cs); |
| 811 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 814 | /* |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame^] | 815 | * Handle user request to change the 'mems' memory placement |
| 816 | * of a cpuset. Needs to validate the request, update the |
| 817 | * cpusets mems_allowed and mems_generation, and for each |
| 818 | * task in the cpuset, rebind any vma mempolicies. |
| 819 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 820 | * Call with manage_sem held. May take callback_sem during call. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame^] | 821 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
| 822 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind |
| 823 | * their mempolicies to the cpusets new mems_allowed. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 824 | */ |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | static int update_nodemask(struct cpuset *cs, char *buf) |
| 827 | { |
| 828 | struct cpuset trialcs; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame^] | 829 | struct task_struct *g, *p; |
| 830 | struct mm_struct **mmarray; |
| 831 | int i, n, ntasks; |
| 832 | int fudge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | int retval; |
| 834 | |
| 835 | trialcs = *cs; |
| 836 | retval = nodelist_parse(buf, trialcs.mems_allowed); |
| 837 | if (retval < 0) |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 838 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map); |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 840 | if (nodes_empty(trialcs.mems_allowed)) { |
| 841 | retval = -ENOSPC; |
| 842 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 844 | retval = validate_change(cs, &trialcs); |
| 845 | if (retval < 0) |
| 846 | goto done; |
| 847 | |
| 848 | down(&callback_sem); |
| 849 | cs->mems_allowed = trialcs.mems_allowed; |
| 850 | atomic_inc(&cpuset_mems_generation); |
| 851 | cs->mems_generation = atomic_read(&cpuset_mems_generation); |
| 852 | up(&callback_sem); |
| 853 | |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame^] | 854 | set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */ |
| 855 | |
| 856 | fudge = 10; /* spare mmarray[] slots */ |
| 857 | fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */ |
| 858 | retval = -ENOMEM; |
| 859 | |
| 860 | /* |
| 861 | * Allocate mmarray[] to hold mm reference for each task |
| 862 | * in cpuset cs. Can't kmalloc GFP_KERNEL while holding |
| 863 | * tasklist_lock. We could use GFP_ATOMIC, but with a |
| 864 | * few more lines of code, we can retry until we get a big |
| 865 | * enough mmarray[] w/o using GFP_ATOMIC. |
| 866 | */ |
| 867 | while (1) { |
| 868 | ntasks = atomic_read(&cs->count); /* guess */ |
| 869 | ntasks += fudge; |
| 870 | mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL); |
| 871 | if (!mmarray) |
| 872 | goto done; |
| 873 | write_lock_irq(&tasklist_lock); /* block fork */ |
| 874 | if (atomic_read(&cs->count) <= ntasks) |
| 875 | break; /* got enough */ |
| 876 | write_unlock_irq(&tasklist_lock); /* try again */ |
| 877 | kfree(mmarray); |
| 878 | } |
| 879 | |
| 880 | n = 0; |
| 881 | |
| 882 | /* Load up mmarray[] with mm reference for each task in cpuset. */ |
| 883 | do_each_thread(g, p) { |
| 884 | struct mm_struct *mm; |
| 885 | |
| 886 | if (n >= ntasks) { |
| 887 | printk(KERN_WARNING |
| 888 | "Cpuset mempolicy rebind incomplete.\n"); |
| 889 | continue; |
| 890 | } |
| 891 | if (p->cpuset != cs) |
| 892 | continue; |
| 893 | mm = get_task_mm(p); |
| 894 | if (!mm) |
| 895 | continue; |
| 896 | mmarray[n++] = mm; |
| 897 | } while_each_thread(g, p); |
| 898 | write_unlock_irq(&tasklist_lock); |
| 899 | |
| 900 | /* |
| 901 | * Now that we've dropped the tasklist spinlock, we can |
| 902 | * rebind the vma mempolicies of each mm in mmarray[] to their |
| 903 | * new cpuset, and release that mm. The mpol_rebind_mm() |
| 904 | * call takes mmap_sem, which we couldn't take while holding |
| 905 | * tasklist_lock. Forks can happen again now - the mpol_copy() |
| 906 | * cpuset_being_rebound check will catch such forks, and rebind |
| 907 | * their vma mempolicies too. Because we still hold the global |
| 908 | * cpuset manage_sem, we know that no other rebind effort will |
| 909 | * be contending for the global variable cpuset_being_rebound. |
| 910 | * It's ok if we rebind the same mm twice; mpol_rebind_mm() |
| 911 | * is idempotent. |
| 912 | */ |
| 913 | for (i = 0; i < n; i++) { |
| 914 | struct mm_struct *mm = mmarray[i]; |
| 915 | |
| 916 | mpol_rebind_mm(mm, &cs->mems_allowed); |
| 917 | mmput(mm); |
| 918 | } |
| 919 | |
| 920 | /* We're done rebinding vma's to this cpusets new mems_allowed. */ |
| 921 | kfree(mmarray); |
| 922 | set_cpuset_being_rebound(NULL); |
| 923 | retval = 0; |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 924 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | return retval; |
| 926 | } |
| 927 | |
| 928 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 929 | * Call with manage_sem held. |
| 930 | */ |
| 931 | |
| 932 | static int update_memory_pressure_enabled(struct cpuset *cs, char *buf) |
| 933 | { |
| 934 | if (simple_strtoul(buf, NULL, 10) != 0) |
| 935 | cpuset_memory_pressure_enabled = 1; |
| 936 | else |
| 937 | cpuset_memory_pressure_enabled = 0; |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | * update_flag - read a 0 or a 1 in a file and update associated flag |
| 943 | * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 944 | * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | * cs: the cpuset to update |
| 946 | * buf: the buffer where we read the 0 or 1 |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 947 | * |
| 948 | * Call with manage_sem held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | */ |
| 950 | |
| 951 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf) |
| 952 | { |
| 953 | int turning_on; |
| 954 | struct cpuset trialcs; |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 955 | int err, cpu_exclusive_changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
| 957 | turning_on = (simple_strtoul(buf, NULL, 10) != 0); |
| 958 | |
| 959 | trialcs = *cs; |
| 960 | if (turning_on) |
| 961 | set_bit(bit, &trialcs.flags); |
| 962 | else |
| 963 | clear_bit(bit, &trialcs.flags); |
| 964 | |
| 965 | err = validate_change(cs, &trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 966 | if (err < 0) |
| 967 | return err; |
| 968 | cpu_exclusive_changed = |
| 969 | (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs)); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 970 | down(&callback_sem); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 971 | if (turning_on) |
| 972 | set_bit(bit, &cs->flags); |
| 973 | else |
| 974 | clear_bit(bit, &cs->flags); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 975 | up(&callback_sem); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 976 | |
| 977 | if (cpu_exclusive_changed) |
| 978 | update_cpu_domains(cs); |
| 979 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 982 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 983 | * Frequency meter - How fast is some event occuring? |
| 984 | * |
| 985 | * These routines manage a digitally filtered, constant time based, |
| 986 | * event frequency meter. There are four routines: |
| 987 | * fmeter_init() - initialize a frequency meter. |
| 988 | * fmeter_markevent() - called each time the event happens. |
| 989 | * fmeter_getrate() - returns the recent rate of such events. |
| 990 | * fmeter_update() - internal routine used to update fmeter. |
| 991 | * |
| 992 | * A common data structure is passed to each of these routines, |
| 993 | * which is used to keep track of the state required to manage the |
| 994 | * frequency meter and its digital filter. |
| 995 | * |
| 996 | * The filter works on the number of events marked per unit time. |
| 997 | * The filter is single-pole low-pass recursive (IIR). The time unit |
| 998 | * is 1 second. Arithmetic is done using 32-bit integers scaled to |
| 999 | * simulate 3 decimal digits of precision (multiplied by 1000). |
| 1000 | * |
| 1001 | * With an FM_COEF of 933, and a time base of 1 second, the filter |
| 1002 | * has a half-life of 10 seconds, meaning that if the events quit |
| 1003 | * happening, then the rate returned from the fmeter_getrate() |
| 1004 | * will be cut in half each 10 seconds, until it converges to zero. |
| 1005 | * |
| 1006 | * It is not worth doing a real infinitely recursive filter. If more |
| 1007 | * than FM_MAXTICKS ticks have elapsed since the last filter event, |
| 1008 | * just compute FM_MAXTICKS ticks worth, by which point the level |
| 1009 | * will be stable. |
| 1010 | * |
| 1011 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid |
| 1012 | * arithmetic overflow in the fmeter_update() routine. |
| 1013 | * |
| 1014 | * Given the simple 32 bit integer arithmetic used, this meter works |
| 1015 | * best for reporting rates between one per millisecond (msec) and |
| 1016 | * one per 32 (approx) seconds. At constant rates faster than one |
| 1017 | * per msec it maxes out at values just under 1,000,000. At constant |
| 1018 | * rates between one per msec, and one per second it will stabilize |
| 1019 | * to a value N*1000, where N is the rate of events per second. |
| 1020 | * At constant rates between one per second and one per 32 seconds, |
| 1021 | * it will be choppy, moving up on the seconds that have an event, |
| 1022 | * and then decaying until the next event. At rates slower than |
| 1023 | * about one in 32 seconds, it decays all the way back to zero between |
| 1024 | * each event. |
| 1025 | */ |
| 1026 | |
| 1027 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ |
| 1028 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ |
| 1029 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ |
| 1030 | #define FM_SCALE 1000 /* faux fixed point scale */ |
| 1031 | |
| 1032 | /* Initialize a frequency meter */ |
| 1033 | static void fmeter_init(struct fmeter *fmp) |
| 1034 | { |
| 1035 | fmp->cnt = 0; |
| 1036 | fmp->val = 0; |
| 1037 | fmp->time = 0; |
| 1038 | spin_lock_init(&fmp->lock); |
| 1039 | } |
| 1040 | |
| 1041 | /* Internal meter update - process cnt events and update value */ |
| 1042 | static void fmeter_update(struct fmeter *fmp) |
| 1043 | { |
| 1044 | time_t now = get_seconds(); |
| 1045 | time_t ticks = now - fmp->time; |
| 1046 | |
| 1047 | if (ticks == 0) |
| 1048 | return; |
| 1049 | |
| 1050 | ticks = min(FM_MAXTICKS, ticks); |
| 1051 | while (ticks-- > 0) |
| 1052 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; |
| 1053 | fmp->time = now; |
| 1054 | |
| 1055 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; |
| 1056 | fmp->cnt = 0; |
| 1057 | } |
| 1058 | |
| 1059 | /* Process any previous ticks, then bump cnt by one (times scale). */ |
| 1060 | static void fmeter_markevent(struct fmeter *fmp) |
| 1061 | { |
| 1062 | spin_lock(&fmp->lock); |
| 1063 | fmeter_update(fmp); |
| 1064 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); |
| 1065 | spin_unlock(&fmp->lock); |
| 1066 | } |
| 1067 | |
| 1068 | /* Process any previous ticks, then return current value. */ |
| 1069 | static int fmeter_getrate(struct fmeter *fmp) |
| 1070 | { |
| 1071 | int val; |
| 1072 | |
| 1073 | spin_lock(&fmp->lock); |
| 1074 | fmeter_update(fmp); |
| 1075 | val = fmp->val; |
| 1076 | spin_unlock(&fmp->lock); |
| 1077 | return val; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1081 | * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly |
| 1082 | * writing the path of the old cpuset in 'ppathbuf' if it needs to be |
| 1083 | * notified on release. |
| 1084 | * |
| 1085 | * Call holding manage_sem. May take callback_sem and task_lock of |
| 1086 | * the task 'pid' during call. |
| 1087 | */ |
| 1088 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1089 | static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
| 1091 | pid_t pid; |
| 1092 | struct task_struct *tsk; |
| 1093 | struct cpuset *oldcs; |
| 1094 | cpumask_t cpus; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1095 | nodemask_t from, to; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame^] | 1096 | struct mm_struct *mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1098 | if (sscanf(pidbuf, "%d", &pid) != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | return -EIO; |
| 1100 | if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
| 1101 | return -ENOSPC; |
| 1102 | |
| 1103 | if (pid) { |
| 1104 | read_lock(&tasklist_lock); |
| 1105 | |
| 1106 | tsk = find_task_by_pid(pid); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1107 | if (!tsk || tsk->flags & PF_EXITING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | read_unlock(&tasklist_lock); |
| 1109 | return -ESRCH; |
| 1110 | } |
| 1111 | |
| 1112 | get_task_struct(tsk); |
| 1113 | read_unlock(&tasklist_lock); |
| 1114 | |
| 1115 | if ((current->euid) && (current->euid != tsk->uid) |
| 1116 | && (current->euid != tsk->suid)) { |
| 1117 | put_task_struct(tsk); |
| 1118 | return -EACCES; |
| 1119 | } |
| 1120 | } else { |
| 1121 | tsk = current; |
| 1122 | get_task_struct(tsk); |
| 1123 | } |
| 1124 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1125 | down(&callback_sem); |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | task_lock(tsk); |
| 1128 | oldcs = tsk->cpuset; |
| 1129 | if (!oldcs) { |
| 1130 | task_unlock(tsk); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1131 | up(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | put_task_struct(tsk); |
| 1133 | return -ESRCH; |
| 1134 | } |
| 1135 | atomic_inc(&cs->count); |
| 1136 | tsk->cpuset = cs; |
| 1137 | task_unlock(tsk); |
| 1138 | |
| 1139 | guarantee_online_cpus(cs, &cpus); |
| 1140 | set_cpus_allowed(tsk, cpus); |
| 1141 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1142 | from = oldcs->mems_allowed; |
| 1143 | to = cs->mems_allowed; |
| 1144 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1145 | up(&callback_sem); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame^] | 1146 | |
| 1147 | mm = get_task_mm(tsk); |
| 1148 | if (mm) { |
| 1149 | mpol_rebind_mm(mm, &to); |
| 1150 | mmput(mm); |
| 1151 | } |
| 1152 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1153 | if (is_memory_migrate(cs)) |
| 1154 | do_migrate_pages(tsk->mm, &from, &to, MPOL_MF_MOVE_ALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | put_task_struct(tsk); |
| 1156 | if (atomic_dec_and_test(&oldcs->count)) |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1157 | check_for_release(oldcs, ppathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | /* The various types of files and directories in a cpuset file system */ |
| 1162 | |
| 1163 | typedef enum { |
| 1164 | FILE_ROOT, |
| 1165 | FILE_DIR, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1166 | FILE_MEMORY_MIGRATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | FILE_CPULIST, |
| 1168 | FILE_MEMLIST, |
| 1169 | FILE_CPU_EXCLUSIVE, |
| 1170 | FILE_MEM_EXCLUSIVE, |
| 1171 | FILE_NOTIFY_ON_RELEASE, |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1172 | FILE_MEMORY_PRESSURE_ENABLED, |
| 1173 | FILE_MEMORY_PRESSURE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | FILE_TASKLIST, |
| 1175 | } cpuset_filetype_t; |
| 1176 | |
| 1177 | static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf, |
| 1178 | size_t nbytes, loff_t *unused_ppos) |
| 1179 | { |
| 1180 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); |
| 1181 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1182 | cpuset_filetype_t type = cft->private; |
| 1183 | char *buffer; |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1184 | char *pathbuf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | int retval = 0; |
| 1186 | |
| 1187 | /* Crude upper limit on largest legitimate cpulist user might write. */ |
| 1188 | if (nbytes > 100 + 6 * NR_CPUS) |
| 1189 | return -E2BIG; |
| 1190 | |
| 1191 | /* +1 for nul-terminator */ |
| 1192 | if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0) |
| 1193 | return -ENOMEM; |
| 1194 | |
| 1195 | if (copy_from_user(buffer, userbuf, nbytes)) { |
| 1196 | retval = -EFAULT; |
| 1197 | goto out1; |
| 1198 | } |
| 1199 | buffer[nbytes] = 0; /* nul-terminate */ |
| 1200 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1201 | down(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | if (is_removed(cs)) { |
| 1204 | retval = -ENODEV; |
| 1205 | goto out2; |
| 1206 | } |
| 1207 | |
| 1208 | switch (type) { |
| 1209 | case FILE_CPULIST: |
| 1210 | retval = update_cpumask(cs, buffer); |
| 1211 | break; |
| 1212 | case FILE_MEMLIST: |
| 1213 | retval = update_nodemask(cs, buffer); |
| 1214 | break; |
| 1215 | case FILE_CPU_EXCLUSIVE: |
| 1216 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer); |
| 1217 | break; |
| 1218 | case FILE_MEM_EXCLUSIVE: |
| 1219 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer); |
| 1220 | break; |
| 1221 | case FILE_NOTIFY_ON_RELEASE: |
| 1222 | retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer); |
| 1223 | break; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1224 | case FILE_MEMORY_MIGRATE: |
| 1225 | retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer); |
| 1226 | break; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1227 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1228 | retval = update_memory_pressure_enabled(cs, buffer); |
| 1229 | break; |
| 1230 | case FILE_MEMORY_PRESSURE: |
| 1231 | retval = -EACCES; |
| 1232 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | case FILE_TASKLIST: |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1234 | retval = attach_task(cs, buffer, &pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | break; |
| 1236 | default: |
| 1237 | retval = -EINVAL; |
| 1238 | goto out2; |
| 1239 | } |
| 1240 | |
| 1241 | if (retval == 0) |
| 1242 | retval = nbytes; |
| 1243 | out2: |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1244 | up(&manage_sem); |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1245 | cpuset_release_agent(pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | out1: |
| 1247 | kfree(buffer); |
| 1248 | return retval; |
| 1249 | } |
| 1250 | |
| 1251 | static ssize_t cpuset_file_write(struct file *file, const char __user *buf, |
| 1252 | size_t nbytes, loff_t *ppos) |
| 1253 | { |
| 1254 | ssize_t retval = 0; |
| 1255 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1256 | if (!cft) |
| 1257 | return -ENODEV; |
| 1258 | |
| 1259 | /* special function ? */ |
| 1260 | if (cft->write) |
| 1261 | retval = cft->write(file, buf, nbytes, ppos); |
| 1262 | else |
| 1263 | retval = cpuset_common_file_write(file, buf, nbytes, ppos); |
| 1264 | |
| 1265 | return retval; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * These ascii lists should be read in a single call, by using a user |
| 1270 | * buffer large enough to hold the entire map. If read in smaller |
| 1271 | * chunks, there is no guarantee of atomicity. Since the display format |
| 1272 | * used, list of ranges of sequential numbers, is variable length, |
| 1273 | * and since these maps can change value dynamically, one could read |
| 1274 | * gibberish by doing partial reads while a list was changing. |
| 1275 | * A single large read to a buffer that crosses a page boundary is |
| 1276 | * ok, because the result being copied to user land is not recomputed |
| 1277 | * across a page fault. |
| 1278 | */ |
| 1279 | |
| 1280 | static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs) |
| 1281 | { |
| 1282 | cpumask_t mask; |
| 1283 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1284 | down(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | mask = cs->cpus_allowed; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1286 | up(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | return cpulist_scnprintf(page, PAGE_SIZE, mask); |
| 1289 | } |
| 1290 | |
| 1291 | static int cpuset_sprintf_memlist(char *page, struct cpuset *cs) |
| 1292 | { |
| 1293 | nodemask_t mask; |
| 1294 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1295 | down(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | mask = cs->mems_allowed; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1297 | up(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
| 1299 | return nodelist_scnprintf(page, PAGE_SIZE, mask); |
| 1300 | } |
| 1301 | |
| 1302 | static ssize_t cpuset_common_file_read(struct file *file, char __user *buf, |
| 1303 | size_t nbytes, loff_t *ppos) |
| 1304 | { |
| 1305 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1306 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); |
| 1307 | cpuset_filetype_t type = cft->private; |
| 1308 | char *page; |
| 1309 | ssize_t retval = 0; |
| 1310 | char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | if (!(page = (char *)__get_free_page(GFP_KERNEL))) |
| 1313 | return -ENOMEM; |
| 1314 | |
| 1315 | s = page; |
| 1316 | |
| 1317 | switch (type) { |
| 1318 | case FILE_CPULIST: |
| 1319 | s += cpuset_sprintf_cpulist(s, cs); |
| 1320 | break; |
| 1321 | case FILE_MEMLIST: |
| 1322 | s += cpuset_sprintf_memlist(s, cs); |
| 1323 | break; |
| 1324 | case FILE_CPU_EXCLUSIVE: |
| 1325 | *s++ = is_cpu_exclusive(cs) ? '1' : '0'; |
| 1326 | break; |
| 1327 | case FILE_MEM_EXCLUSIVE: |
| 1328 | *s++ = is_mem_exclusive(cs) ? '1' : '0'; |
| 1329 | break; |
| 1330 | case FILE_NOTIFY_ON_RELEASE: |
| 1331 | *s++ = notify_on_release(cs) ? '1' : '0'; |
| 1332 | break; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1333 | case FILE_MEMORY_MIGRATE: |
| 1334 | *s++ = is_memory_migrate(cs) ? '1' : '0'; |
| 1335 | break; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1336 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1337 | *s++ = cpuset_memory_pressure_enabled ? '1' : '0'; |
| 1338 | break; |
| 1339 | case FILE_MEMORY_PRESSURE: |
| 1340 | s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter)); |
| 1341 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | default: |
| 1343 | retval = -EINVAL; |
| 1344 | goto out; |
| 1345 | } |
| 1346 | *s++ = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Al Viro | eacaa1f | 2005-09-30 03:26:43 +0100 | [diff] [blame] | 1348 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | out: |
| 1350 | free_page((unsigned long)page); |
| 1351 | return retval; |
| 1352 | } |
| 1353 | |
| 1354 | static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes, |
| 1355 | loff_t *ppos) |
| 1356 | { |
| 1357 | ssize_t retval = 0; |
| 1358 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1359 | if (!cft) |
| 1360 | return -ENODEV; |
| 1361 | |
| 1362 | /* special function ? */ |
| 1363 | if (cft->read) |
| 1364 | retval = cft->read(file, buf, nbytes, ppos); |
| 1365 | else |
| 1366 | retval = cpuset_common_file_read(file, buf, nbytes, ppos); |
| 1367 | |
| 1368 | return retval; |
| 1369 | } |
| 1370 | |
| 1371 | static int cpuset_file_open(struct inode *inode, struct file *file) |
| 1372 | { |
| 1373 | int err; |
| 1374 | struct cftype *cft; |
| 1375 | |
| 1376 | err = generic_file_open(inode, file); |
| 1377 | if (err) |
| 1378 | return err; |
| 1379 | |
| 1380 | cft = __d_cft(file->f_dentry); |
| 1381 | if (!cft) |
| 1382 | return -ENODEV; |
| 1383 | if (cft->open) |
| 1384 | err = cft->open(inode, file); |
| 1385 | else |
| 1386 | err = 0; |
| 1387 | |
| 1388 | return err; |
| 1389 | } |
| 1390 | |
| 1391 | static int cpuset_file_release(struct inode *inode, struct file *file) |
| 1392 | { |
| 1393 | struct cftype *cft = __d_cft(file->f_dentry); |
| 1394 | if (cft->release) |
| 1395 | return cft->release(inode, file); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
Paul Jackson | 18a19cb | 2005-10-30 15:02:31 -0800 | [diff] [blame] | 1399 | /* |
| 1400 | * cpuset_rename - Only allow simple rename of directories in place. |
| 1401 | */ |
| 1402 | static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1403 | struct inode *new_dir, struct dentry *new_dentry) |
| 1404 | { |
| 1405 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 1406 | return -ENOTDIR; |
| 1407 | if (new_dentry->d_inode) |
| 1408 | return -EEXIST; |
| 1409 | if (old_dir != new_dir) |
| 1410 | return -EIO; |
| 1411 | return simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 1412 | } |
| 1413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | static struct file_operations cpuset_file_operations = { |
| 1415 | .read = cpuset_file_read, |
| 1416 | .write = cpuset_file_write, |
| 1417 | .llseek = generic_file_llseek, |
| 1418 | .open = cpuset_file_open, |
| 1419 | .release = cpuset_file_release, |
| 1420 | }; |
| 1421 | |
| 1422 | static struct inode_operations cpuset_dir_inode_operations = { |
| 1423 | .lookup = simple_lookup, |
| 1424 | .mkdir = cpuset_mkdir, |
| 1425 | .rmdir = cpuset_rmdir, |
Paul Jackson | 18a19cb | 2005-10-30 15:02:31 -0800 | [diff] [blame] | 1426 | .rename = cpuset_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | }; |
| 1428 | |
| 1429 | static int cpuset_create_file(struct dentry *dentry, int mode) |
| 1430 | { |
| 1431 | struct inode *inode; |
| 1432 | |
| 1433 | if (!dentry) |
| 1434 | return -ENOENT; |
| 1435 | if (dentry->d_inode) |
| 1436 | return -EEXIST; |
| 1437 | |
| 1438 | inode = cpuset_new_inode(mode); |
| 1439 | if (!inode) |
| 1440 | return -ENOMEM; |
| 1441 | |
| 1442 | if (S_ISDIR(mode)) { |
| 1443 | inode->i_op = &cpuset_dir_inode_operations; |
| 1444 | inode->i_fop = &simple_dir_operations; |
| 1445 | |
| 1446 | /* start off with i_nlink == 2 (for "." entry) */ |
| 1447 | inode->i_nlink++; |
| 1448 | } else if (S_ISREG(mode)) { |
| 1449 | inode->i_size = 0; |
| 1450 | inode->i_fop = &cpuset_file_operations; |
| 1451 | } |
| 1452 | |
| 1453 | d_instantiate(dentry, inode); |
| 1454 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * cpuset_create_dir - create a directory for an object. |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 1460 | * cs: the cpuset we create the directory for. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | * It must have a valid ->parent field |
| 1462 | * And we are going to fill its ->dentry field. |
| 1463 | * name: The name to give to the cpuset directory. Will be copied. |
| 1464 | * mode: mode to set on new directory. |
| 1465 | */ |
| 1466 | |
| 1467 | static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode) |
| 1468 | { |
| 1469 | struct dentry *dentry = NULL; |
| 1470 | struct dentry *parent; |
| 1471 | int error = 0; |
| 1472 | |
| 1473 | parent = cs->parent->dentry; |
| 1474 | dentry = cpuset_get_dentry(parent, name); |
| 1475 | if (IS_ERR(dentry)) |
| 1476 | return PTR_ERR(dentry); |
| 1477 | error = cpuset_create_file(dentry, S_IFDIR | mode); |
| 1478 | if (!error) { |
| 1479 | dentry->d_fsdata = cs; |
| 1480 | parent->d_inode->i_nlink++; |
| 1481 | cs->dentry = dentry; |
| 1482 | } |
| 1483 | dput(dentry); |
| 1484 | |
| 1485 | return error; |
| 1486 | } |
| 1487 | |
| 1488 | static int cpuset_add_file(struct dentry *dir, const struct cftype *cft) |
| 1489 | { |
| 1490 | struct dentry *dentry; |
| 1491 | int error; |
| 1492 | |
| 1493 | down(&dir->d_inode->i_sem); |
| 1494 | dentry = cpuset_get_dentry(dir, cft->name); |
| 1495 | if (!IS_ERR(dentry)) { |
| 1496 | error = cpuset_create_file(dentry, 0644 | S_IFREG); |
| 1497 | if (!error) |
| 1498 | dentry->d_fsdata = (void *)cft; |
| 1499 | dput(dentry); |
| 1500 | } else |
| 1501 | error = PTR_ERR(dentry); |
| 1502 | up(&dir->d_inode->i_sem); |
| 1503 | return error; |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Stuff for reading the 'tasks' file. |
| 1508 | * |
| 1509 | * Reading this file can return large amounts of data if a cpuset has |
| 1510 | * *lots* of attached tasks. So it may need several calls to read(), |
| 1511 | * but we cannot guarantee that the information we produce is correct |
| 1512 | * unless we produce it entirely atomically. |
| 1513 | * |
| 1514 | * Upon tasks file open(), a struct ctr_struct is allocated, that |
| 1515 | * will have a pointer to an array (also allocated here). The struct |
| 1516 | * ctr_struct * is stored in file->private_data. Its resources will |
| 1517 | * be freed by release() when the file is closed. The array is used |
| 1518 | * to sprintf the PIDs and then used by read(). |
| 1519 | */ |
| 1520 | |
| 1521 | /* cpusets_tasks_read array */ |
| 1522 | |
| 1523 | struct ctr_struct { |
| 1524 | char *buf; |
| 1525 | int bufsz; |
| 1526 | }; |
| 1527 | |
| 1528 | /* |
| 1529 | * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1530 | * Return actual number of pids loaded. No need to task_lock(p) |
| 1531 | * when reading out p->cpuset, as we don't really care if it changes |
| 1532 | * 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] | 1533 | */ |
| 1534 | static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs) |
| 1535 | { |
| 1536 | int n = 0; |
| 1537 | struct task_struct *g, *p; |
| 1538 | |
| 1539 | read_lock(&tasklist_lock); |
| 1540 | |
| 1541 | do_each_thread(g, p) { |
| 1542 | if (p->cpuset == cs) { |
| 1543 | pidarray[n++] = p->pid; |
| 1544 | if (unlikely(n == npids)) |
| 1545 | goto array_full; |
| 1546 | } |
| 1547 | } while_each_thread(g, p); |
| 1548 | |
| 1549 | array_full: |
| 1550 | read_unlock(&tasklist_lock); |
| 1551 | return n; |
| 1552 | } |
| 1553 | |
| 1554 | static int cmppid(const void *a, const void *b) |
| 1555 | { |
| 1556 | return *(pid_t *)a - *(pid_t *)b; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Convert array 'a' of 'npids' pid_t's to a string of newline separated |
| 1561 | * decimal pids in 'buf'. Don't write more than 'sz' chars, but return |
| 1562 | * count 'cnt' of how many chars would be written if buf were large enough. |
| 1563 | */ |
| 1564 | static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids) |
| 1565 | { |
| 1566 | int cnt = 0; |
| 1567 | int i; |
| 1568 | |
| 1569 | for (i = 0; i < npids; i++) |
| 1570 | cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]); |
| 1571 | return cnt; |
| 1572 | } |
| 1573 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1574 | /* |
| 1575 | * Handle an open on 'tasks' file. Prepare a buffer listing the |
| 1576 | * process id's of tasks currently attached to the cpuset being opened. |
| 1577 | * |
| 1578 | * Does not require any specific cpuset semaphores, and does not take any. |
| 1579 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | static int cpuset_tasks_open(struct inode *unused, struct file *file) |
| 1581 | { |
| 1582 | struct cpuset *cs = __d_cs(file->f_dentry->d_parent); |
| 1583 | struct ctr_struct *ctr; |
| 1584 | pid_t *pidarray; |
| 1585 | int npids; |
| 1586 | char c; |
| 1587 | |
| 1588 | if (!(file->f_mode & FMODE_READ)) |
| 1589 | return 0; |
| 1590 | |
| 1591 | ctr = kmalloc(sizeof(*ctr), GFP_KERNEL); |
| 1592 | if (!ctr) |
| 1593 | goto err0; |
| 1594 | |
| 1595 | /* |
| 1596 | * If cpuset gets more users after we read count, we won't have |
| 1597 | * enough space - tough. This race is indistinguishable to the |
| 1598 | * caller from the case that the additional cpuset users didn't |
| 1599 | * show up until sometime later on. |
| 1600 | */ |
| 1601 | npids = atomic_read(&cs->count); |
| 1602 | pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL); |
| 1603 | if (!pidarray) |
| 1604 | goto err1; |
| 1605 | |
| 1606 | npids = pid_array_load(pidarray, npids, cs); |
| 1607 | sort(pidarray, npids, sizeof(pid_t), cmppid, NULL); |
| 1608 | |
| 1609 | /* Call pid_array_to_buf() twice, first just to get bufsz */ |
| 1610 | ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1; |
| 1611 | ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL); |
| 1612 | if (!ctr->buf) |
| 1613 | goto err2; |
| 1614 | ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids); |
| 1615 | |
| 1616 | kfree(pidarray); |
| 1617 | file->private_data = ctr; |
| 1618 | return 0; |
| 1619 | |
| 1620 | err2: |
| 1621 | kfree(pidarray); |
| 1622 | err1: |
| 1623 | kfree(ctr); |
| 1624 | err0: |
| 1625 | return -ENOMEM; |
| 1626 | } |
| 1627 | |
| 1628 | static ssize_t cpuset_tasks_read(struct file *file, char __user *buf, |
| 1629 | size_t nbytes, loff_t *ppos) |
| 1630 | { |
| 1631 | struct ctr_struct *ctr = file->private_data; |
| 1632 | |
| 1633 | if (*ppos + nbytes > ctr->bufsz) |
| 1634 | nbytes = ctr->bufsz - *ppos; |
| 1635 | if (copy_to_user(buf, ctr->buf + *ppos, nbytes)) |
| 1636 | return -EFAULT; |
| 1637 | *ppos += nbytes; |
| 1638 | return nbytes; |
| 1639 | } |
| 1640 | |
| 1641 | static int cpuset_tasks_release(struct inode *unused_inode, struct file *file) |
| 1642 | { |
| 1643 | struct ctr_struct *ctr; |
| 1644 | |
| 1645 | if (file->f_mode & FMODE_READ) { |
| 1646 | ctr = file->private_data; |
| 1647 | kfree(ctr->buf); |
| 1648 | kfree(ctr); |
| 1649 | } |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * for the common functions, 'private' gives the type of file |
| 1655 | */ |
| 1656 | |
| 1657 | static struct cftype cft_tasks = { |
| 1658 | .name = "tasks", |
| 1659 | .open = cpuset_tasks_open, |
| 1660 | .read = cpuset_tasks_read, |
| 1661 | .release = cpuset_tasks_release, |
| 1662 | .private = FILE_TASKLIST, |
| 1663 | }; |
| 1664 | |
| 1665 | static struct cftype cft_cpus = { |
| 1666 | .name = "cpus", |
| 1667 | .private = FILE_CPULIST, |
| 1668 | }; |
| 1669 | |
| 1670 | static struct cftype cft_mems = { |
| 1671 | .name = "mems", |
| 1672 | .private = FILE_MEMLIST, |
| 1673 | }; |
| 1674 | |
| 1675 | static struct cftype cft_cpu_exclusive = { |
| 1676 | .name = "cpu_exclusive", |
| 1677 | .private = FILE_CPU_EXCLUSIVE, |
| 1678 | }; |
| 1679 | |
| 1680 | static struct cftype cft_mem_exclusive = { |
| 1681 | .name = "mem_exclusive", |
| 1682 | .private = FILE_MEM_EXCLUSIVE, |
| 1683 | }; |
| 1684 | |
| 1685 | static struct cftype cft_notify_on_release = { |
| 1686 | .name = "notify_on_release", |
| 1687 | .private = FILE_NOTIFY_ON_RELEASE, |
| 1688 | }; |
| 1689 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1690 | static struct cftype cft_memory_migrate = { |
| 1691 | .name = "memory_migrate", |
| 1692 | .private = FILE_MEMORY_MIGRATE, |
| 1693 | }; |
| 1694 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1695 | static struct cftype cft_memory_pressure_enabled = { |
| 1696 | .name = "memory_pressure_enabled", |
| 1697 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
| 1698 | }; |
| 1699 | |
| 1700 | static struct cftype cft_memory_pressure = { |
| 1701 | .name = "memory_pressure", |
| 1702 | .private = FILE_MEMORY_PRESSURE, |
| 1703 | }; |
| 1704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | static int cpuset_populate_dir(struct dentry *cs_dentry) |
| 1706 | { |
| 1707 | int err; |
| 1708 | |
| 1709 | if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0) |
| 1710 | return err; |
| 1711 | if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0) |
| 1712 | return err; |
| 1713 | if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0) |
| 1714 | return err; |
| 1715 | if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0) |
| 1716 | return err; |
| 1717 | if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0) |
| 1718 | return err; |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1719 | if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0) |
| 1720 | return err; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1721 | if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0) |
| 1722 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0) |
| 1724 | return err; |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * cpuset_create - create a cpuset |
| 1730 | * parent: cpuset that will be parent of the new cpuset. |
| 1731 | * name: name of the new cpuset. Will be strcpy'ed. |
| 1732 | * mode: mode to set on new inode |
| 1733 | * |
| 1734 | * Must be called with the semaphore on the parent inode held |
| 1735 | */ |
| 1736 | |
| 1737 | static long cpuset_create(struct cpuset *parent, const char *name, int mode) |
| 1738 | { |
| 1739 | struct cpuset *cs; |
| 1740 | int err; |
| 1741 | |
| 1742 | cs = kmalloc(sizeof(*cs), GFP_KERNEL); |
| 1743 | if (!cs) |
| 1744 | return -ENOMEM; |
| 1745 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1746 | down(&manage_sem); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1747 | cpuset_update_task_memory_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | cs->flags = 0; |
| 1749 | if (notify_on_release(parent)) |
| 1750 | set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags); |
| 1751 | cs->cpus_allowed = CPU_MASK_NONE; |
| 1752 | cs->mems_allowed = NODE_MASK_NONE; |
| 1753 | atomic_set(&cs->count, 0); |
| 1754 | INIT_LIST_HEAD(&cs->sibling); |
| 1755 | INIT_LIST_HEAD(&cs->children); |
| 1756 | atomic_inc(&cpuset_mems_generation); |
| 1757 | cs->mems_generation = atomic_read(&cpuset_mems_generation); |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1758 | fmeter_init(&cs->fmeter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
| 1760 | cs->parent = parent; |
| 1761 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1762 | down(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | list_add(&cs->sibling, &cs->parent->children); |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1764 | number_of_cpusets++; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1765 | up(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | err = cpuset_create_dir(cs, name, mode); |
| 1768 | if (err < 0) |
| 1769 | goto err; |
| 1770 | |
| 1771 | /* |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1772 | * Release manage_sem before cpuset_populate_dir() because it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | * will down() this new directory's i_sem and if we race with |
| 1774 | * another mkdir, we might deadlock. |
| 1775 | */ |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1776 | up(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | |
| 1778 | err = cpuset_populate_dir(cs->dentry); |
| 1779 | /* If err < 0, we have a half-filled directory - oh well ;) */ |
| 1780 | return 0; |
| 1781 | err: |
| 1782 | list_del(&cs->sibling); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1783 | up(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | kfree(cs); |
| 1785 | return err; |
| 1786 | } |
| 1787 | |
| 1788 | static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1789 | { |
| 1790 | struct cpuset *c_parent = dentry->d_parent->d_fsdata; |
| 1791 | |
| 1792 | /* the vfs holds inode->i_sem already */ |
| 1793 | return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR); |
| 1794 | } |
| 1795 | |
| 1796 | static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 1797 | { |
| 1798 | struct cpuset *cs = dentry->d_fsdata; |
| 1799 | struct dentry *d; |
| 1800 | struct cpuset *parent; |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1801 | char *pathbuf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | |
| 1803 | /* the vfs holds both inode->i_sem already */ |
| 1804 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1805 | down(&manage_sem); |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1806 | cpuset_update_task_memory_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | if (atomic_read(&cs->count) > 0) { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1808 | up(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | return -EBUSY; |
| 1810 | } |
| 1811 | if (!list_empty(&cs->children)) { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1812 | up(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | return -EBUSY; |
| 1814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | parent = cs->parent; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1816 | down(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | set_bit(CS_REMOVED, &cs->flags); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1818 | if (is_cpu_exclusive(cs)) |
| 1819 | update_cpu_domains(cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | list_del(&cs->sibling); /* delete my sibling from parent->children */ |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1821 | spin_lock(&cs->dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | d = dget(cs->dentry); |
| 1823 | cs->dentry = NULL; |
| 1824 | spin_unlock(&d->d_lock); |
| 1825 | cpuset_d_remove_dir(d); |
| 1826 | dput(d); |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1827 | number_of_cpusets--; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1828 | up(&callback_sem); |
| 1829 | if (list_empty(&parent->children)) |
| 1830 | check_for_release(parent, &pathbuf); |
| 1831 | up(&manage_sem); |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1832 | cpuset_release_agent(pathbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | /** |
| 1837 | * cpuset_init - initialize cpusets at system boot |
| 1838 | * |
| 1839 | * Description: Initialize top_cpuset and the cpuset internal file system, |
| 1840 | **/ |
| 1841 | |
| 1842 | int __init cpuset_init(void) |
| 1843 | { |
| 1844 | struct dentry *root; |
| 1845 | int err; |
| 1846 | |
| 1847 | top_cpuset.cpus_allowed = CPU_MASK_ALL; |
| 1848 | top_cpuset.mems_allowed = NODE_MASK_ALL; |
| 1849 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1850 | fmeter_init(&top_cpuset.fmeter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | atomic_inc(&cpuset_mems_generation); |
| 1852 | top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation); |
| 1853 | |
| 1854 | init_task.cpuset = &top_cpuset; |
| 1855 | |
| 1856 | err = register_filesystem(&cpuset_fs_type); |
| 1857 | if (err < 0) |
| 1858 | goto out; |
| 1859 | cpuset_mount = kern_mount(&cpuset_fs_type); |
| 1860 | if (IS_ERR(cpuset_mount)) { |
| 1861 | printk(KERN_ERR "cpuset: could not mount!\n"); |
| 1862 | err = PTR_ERR(cpuset_mount); |
| 1863 | cpuset_mount = NULL; |
| 1864 | goto out; |
| 1865 | } |
| 1866 | root = cpuset_mount->mnt_sb->s_root; |
| 1867 | root->d_fsdata = &top_cpuset; |
| 1868 | root->d_inode->i_nlink++; |
| 1869 | top_cpuset.dentry = root; |
| 1870 | root->d_inode->i_op = &cpuset_dir_inode_operations; |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1871 | number_of_cpusets = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | err = cpuset_populate_dir(root); |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1873 | /* memory_pressure_enabled is in root cpuset only */ |
| 1874 | if (err == 0) |
| 1875 | err = cpuset_add_file(root, &cft_memory_pressure_enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | out: |
| 1877 | return err; |
| 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * cpuset_init_smp - initialize cpus_allowed |
| 1882 | * |
| 1883 | * Description: Finish top cpuset after cpu, node maps are initialized |
| 1884 | **/ |
| 1885 | |
| 1886 | void __init cpuset_init_smp(void) |
| 1887 | { |
| 1888 | top_cpuset.cpus_allowed = cpu_online_map; |
| 1889 | top_cpuset.mems_allowed = node_online_map; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * cpuset_fork - attach newly forked task to its parents cpuset. |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 1894 | * @tsk: pointer to task_struct of forking parent process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1896 | * Description: A task inherits its parent's cpuset at fork(). |
| 1897 | * |
| 1898 | * A pointer to the shared cpuset was automatically copied in fork.c |
| 1899 | * by dup_task_struct(). However, we ignore that copy, since it was |
| 1900 | * not made under the protection of task_lock(), so might no longer be |
| 1901 | * a valid cpuset pointer. attach_task() might have already changed |
| 1902 | * current->cpuset, allowing the previously referenced cpuset to |
| 1903 | * be removed and freed. Instead, we task_lock(current) and copy |
| 1904 | * its present value of current->cpuset for our freshly forked child. |
| 1905 | * |
| 1906 | * At the point that cpuset_fork() is called, 'current' is the parent |
| 1907 | * task, and the passed argument 'child' points to the child task. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | **/ |
| 1909 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1910 | void cpuset_fork(struct task_struct *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | { |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1912 | task_lock(current); |
| 1913 | child->cpuset = current->cpuset; |
| 1914 | atomic_inc(&child->cpuset->count); |
| 1915 | task_unlock(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /** |
| 1919 | * cpuset_exit - detach cpuset from exiting task |
| 1920 | * @tsk: pointer to task_struct of exiting process |
| 1921 | * |
| 1922 | * Description: Detach cpuset from @tsk and release it. |
| 1923 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1924 | * Note that cpusets marked notify_on_release force every task in |
| 1925 | * them to take the global manage_sem semaphore when exiting. |
| 1926 | * This could impact scaling on very large systems. Be reluctant to |
| 1927 | * use notify_on_release cpusets where very high task exit scaling |
| 1928 | * is required on large systems. |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 1929 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1930 | * Don't even think about derefencing 'cs' after the cpuset use count |
| 1931 | * goes to zero, except inside a critical section guarded by manage_sem |
| 1932 | * or callback_sem. Otherwise a zero cpuset use count is a license to |
| 1933 | * any other task to nuke the cpuset immediately, via cpuset_rmdir(). |
| 1934 | * |
| 1935 | * This routine has to take manage_sem, not callback_sem, because |
| 1936 | * it is holding that semaphore while calling check_for_release(), |
| 1937 | * which calls kmalloc(), so can't be called holding callback__sem(). |
| 1938 | * |
| 1939 | * We don't need to task_lock() this reference to tsk->cpuset, |
| 1940 | * because tsk is already marked PF_EXITING, so attach_task() won't |
Paul Jackson | b4b2641 | 2006-01-08 01:01:53 -0800 | [diff] [blame] | 1941 | * mess with it, or task is a failed fork, never visible to attach_task. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | **/ |
| 1943 | |
| 1944 | void cpuset_exit(struct task_struct *tsk) |
| 1945 | { |
| 1946 | struct cpuset *cs; |
| 1947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | cs = tsk->cpuset; |
| 1949 | tsk->cpuset = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 1951 | if (notify_on_release(cs)) { |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1952 | char *pathbuf = NULL; |
| 1953 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1954 | down(&manage_sem); |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 1955 | if (atomic_dec_and_test(&cs->count)) |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1956 | check_for_release(cs, &pathbuf); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1957 | up(&manage_sem); |
Paul Jackson | 3077a26 | 2005-08-09 10:07:59 -0700 | [diff] [blame] | 1958 | cpuset_release_agent(pathbuf); |
Paul Jackson | 2efe86b | 2005-05-27 02:02:43 -0700 | [diff] [blame] | 1959 | } else { |
| 1960 | atomic_dec(&cs->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
| 1966 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. |
| 1967 | * |
| 1968 | * Description: Returns the cpumask_t cpus_allowed of the cpuset |
| 1969 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 1970 | * subset of cpu_online_map, even if this means going outside the |
| 1971 | * tasks cpuset. |
| 1972 | **/ |
| 1973 | |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1974 | cpumask_t cpuset_cpus_allowed(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | { |
| 1976 | cpumask_t mask; |
| 1977 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1978 | down(&callback_sem); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1979 | task_lock(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | guarantee_online_cpus(tsk->cpuset, &mask); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1981 | task_unlock(tsk); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1982 | up(&callback_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
| 1984 | return mask; |
| 1985 | } |
| 1986 | |
| 1987 | void cpuset_init_current_mems_allowed(void) |
| 1988 | { |
| 1989 | current->mems_allowed = NODE_MASK_ALL; |
| 1990 | } |
| 1991 | |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 1992 | /** |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 1993 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. |
| 1994 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. |
| 1995 | * |
| 1996 | * Description: Returns the nodemask_t mems_allowed of the cpuset |
| 1997 | * attached to the specified @tsk. Guaranteed to return some non-empty |
| 1998 | * subset of node_online_map, even if this means going outside the |
| 1999 | * tasks cpuset. |
| 2000 | **/ |
| 2001 | |
| 2002 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) |
| 2003 | { |
| 2004 | nodemask_t mask; |
| 2005 | |
| 2006 | down(&callback_sem); |
| 2007 | task_lock(tsk); |
| 2008 | guarantee_online_mems(tsk->cpuset, &mask); |
| 2009 | task_unlock(tsk); |
| 2010 | up(&callback_sem); |
| 2011 | |
| 2012 | return mask; |
| 2013 | } |
| 2014 | |
| 2015 | /** |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 2016 | * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed |
| 2017 | * @zl: the zonelist to be checked |
| 2018 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | * Are any of the nodes on zonelist zl allowed in current->mems_allowed? |
| 2020 | */ |
| 2021 | int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl) |
| 2022 | { |
| 2023 | int i; |
| 2024 | |
| 2025 | for (i = 0; zl->zones[i]; i++) { |
| 2026 | int nid = zl->zones[i]->zone_pgdat->node_id; |
| 2027 | |
| 2028 | if (node_isset(nid, current->mems_allowed)) |
| 2029 | return 1; |
| 2030 | } |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2034 | /* |
| 2035 | * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2036 | * ancestor to the specified cpuset. Call holding callback_sem. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2037 | * If no ancestor is mem_exclusive (an unusual configuration), then |
| 2038 | * returns the root cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2040 | static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2042 | while (!is_mem_exclusive(cs) && cs->parent) |
| 2043 | cs = cs->parent; |
| 2044 | return cs; |
| 2045 | } |
| 2046 | |
| 2047 | /** |
| 2048 | * cpuset_zone_allowed - Can we allocate memory on zone z's memory node? |
| 2049 | * @z: is this zone on an allowed node? |
| 2050 | * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL) |
| 2051 | * |
| 2052 | * If we're in interrupt, yes, we can always allocate. If zone |
| 2053 | * z's node is in our tasks mems_allowed, yes. If it's not a |
| 2054 | * __GFP_HARDWALL request and this zone's nodes is in the nearest |
| 2055 | * mem_exclusive cpuset ancestor to this tasks cpuset, yes. |
| 2056 | * Otherwise, no. |
| 2057 | * |
| 2058 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
| 2059 | * and do not allow allocations outside the current tasks cpuset. |
| 2060 | * GFP_KERNEL allocations are not so marked, so can escape to the |
| 2061 | * nearest mem_exclusive ancestor cpuset. |
| 2062 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2063 | * Scanning up parent cpusets requires callback_sem. The __alloc_pages() |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2064 | * routine only calls here with __GFP_HARDWALL bit _not_ set if |
| 2065 | * it's a GFP_KERNEL allocation, and all nodes in the current tasks |
| 2066 | * mems_allowed came up empty on the first pass over the zonelist. |
| 2067 | * So only GFP_KERNEL allocations, if all nodes in the cpuset are |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2068 | * short of memory, might require taking the callback_sem semaphore. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2069 | * |
| 2070 | * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages() |
| 2071 | * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing |
| 2072 | * hardwall cpusets - no allocation on a node outside the cpuset is |
| 2073 | * allowed (unless in interrupt, of course). |
| 2074 | * |
| 2075 | * The second loop doesn't even call here for GFP_ATOMIC requests |
| 2076 | * (if the __alloc_pages() local variable 'wait' is set). That check |
| 2077 | * and the checks below have the combined affect in the second loop of |
| 2078 | * the __alloc_pages() routine that: |
| 2079 | * in_interrupt - any node ok (current task context irrelevant) |
| 2080 | * GFP_ATOMIC - any node ok |
| 2081 | * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok |
| 2082 | * GFP_USER - only nodes in current tasks mems allowed ok. |
| 2083 | **/ |
| 2084 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 2085 | int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2086 | { |
| 2087 | int node; /* node that zone z is on */ |
| 2088 | const struct cpuset *cs; /* current cpuset ancestors */ |
| 2089 | int allowed = 1; /* is allocation in zone z allowed? */ |
| 2090 | |
| 2091 | if (in_interrupt()) |
| 2092 | return 1; |
| 2093 | node = z->zone_pgdat->node_id; |
| 2094 | if (node_isset(node, current->mems_allowed)) |
| 2095 | return 1; |
| 2096 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
| 2097 | return 0; |
| 2098 | |
Bob Picco | 5563e77 | 2005-11-13 16:06:35 -0800 | [diff] [blame] | 2099 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 2100 | return 1; |
| 2101 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2102 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2103 | down(&callback_sem); |
| 2104 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2105 | task_lock(current); |
| 2106 | cs = nearest_exclusive_ancestor(current->cpuset); |
| 2107 | task_unlock(current); |
| 2108 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2109 | allowed = node_isset(node, cs->mems_allowed); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2110 | up(&callback_sem); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2111 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2114 | /** |
| 2115 | * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors? |
| 2116 | * @p: pointer to task_struct of some other task. |
| 2117 | * |
| 2118 | * Description: Return true if the nearest mem_exclusive ancestor |
| 2119 | * cpusets of tasks @p and current overlap. Used by oom killer to |
| 2120 | * determine if task @p's memory usage might impact the memory |
| 2121 | * available to the current task. |
| 2122 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2123 | * Acquires callback_sem - not suitable for calling from a fast path. |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2124 | **/ |
| 2125 | |
| 2126 | int cpuset_excl_nodes_overlap(const struct task_struct *p) |
| 2127 | { |
| 2128 | const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */ |
| 2129 | int overlap = 0; /* do cpusets overlap? */ |
| 2130 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2131 | down(&callback_sem); |
| 2132 | |
| 2133 | task_lock(current); |
| 2134 | if (current->flags & PF_EXITING) { |
| 2135 | task_unlock(current); |
| 2136 | goto done; |
| 2137 | } |
| 2138 | cs1 = nearest_exclusive_ancestor(current->cpuset); |
| 2139 | task_unlock(current); |
| 2140 | |
| 2141 | task_lock((struct task_struct *)p); |
| 2142 | if (p->flags & PF_EXITING) { |
| 2143 | task_unlock((struct task_struct *)p); |
| 2144 | goto done; |
| 2145 | } |
| 2146 | cs2 = nearest_exclusive_ancestor(p->cpuset); |
| 2147 | task_unlock((struct task_struct *)p); |
| 2148 | |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2149 | overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed); |
| 2150 | done: |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2151 | up(&callback_sem); |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2152 | |
| 2153 | return overlap; |
| 2154 | } |
| 2155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2157 | * Collection of memory_pressure is suppressed unless |
| 2158 | * this flag is enabled by writing "1" to the special |
| 2159 | * cpuset file 'memory_pressure_enabled' in the root cpuset. |
| 2160 | */ |
| 2161 | |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 2162 | int cpuset_memory_pressure_enabled __read_mostly; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2163 | |
| 2164 | /** |
| 2165 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. |
| 2166 | * |
| 2167 | * Keep a running average of the rate of synchronous (direct) |
| 2168 | * page reclaim efforts initiated by tasks in each cpuset. |
| 2169 | * |
| 2170 | * This represents the rate at which some task in the cpuset |
| 2171 | * ran low on memory on all nodes it was allowed to use, and |
| 2172 | * had to enter the kernels page reclaim code in an effort to |
| 2173 | * create more free memory by tossing clean pages or swapping |
| 2174 | * or writing dirty pages. |
| 2175 | * |
| 2176 | * Display to user space in the per-cpuset read-only file |
| 2177 | * "memory_pressure". Value displayed is an integer |
| 2178 | * representing the recent rate of entry into the synchronous |
| 2179 | * (direct) page reclaim by any task attached to the cpuset. |
| 2180 | **/ |
| 2181 | |
| 2182 | void __cpuset_memory_pressure_bump(void) |
| 2183 | { |
| 2184 | struct cpuset *cs; |
| 2185 | |
| 2186 | task_lock(current); |
| 2187 | cs = current->cpuset; |
| 2188 | fmeter_markevent(&cs->fmeter); |
| 2189 | task_unlock(current); |
| 2190 | } |
| 2191 | |
| 2192 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | * proc_cpuset_show() |
| 2194 | * - Print tasks cpuset path into seq_file. |
| 2195 | * - Used for /proc/<pid>/cpuset. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2196 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
| 2197 | * doesn't really matter if tsk->cpuset changes after we read it, |
| 2198 | * and we take manage_sem, keeping attach_task() from changing it |
| 2199 | * anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | */ |
| 2201 | |
| 2202 | static int proc_cpuset_show(struct seq_file *m, void *v) |
| 2203 | { |
| 2204 | struct cpuset *cs; |
| 2205 | struct task_struct *tsk; |
| 2206 | char *buf; |
| 2207 | int retval = 0; |
| 2208 | |
| 2209 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2210 | if (!buf) |
| 2211 | return -ENOMEM; |
| 2212 | |
| 2213 | tsk = m->private; |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2214 | down(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | cs = tsk->cpuset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | if (!cs) { |
| 2217 | retval = -EINVAL; |
| 2218 | goto out; |
| 2219 | } |
| 2220 | |
| 2221 | retval = cpuset_path(cs, buf, PAGE_SIZE); |
| 2222 | if (retval < 0) |
| 2223 | goto out; |
| 2224 | seq_puts(m, buf); |
| 2225 | seq_putc(m, '\n'); |
| 2226 | out: |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2227 | up(&manage_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | kfree(buf); |
| 2229 | return retval; |
| 2230 | } |
| 2231 | |
| 2232 | static int cpuset_open(struct inode *inode, struct file *file) |
| 2233 | { |
| 2234 | struct task_struct *tsk = PROC_I(inode)->task; |
| 2235 | return single_open(file, proc_cpuset_show, tsk); |
| 2236 | } |
| 2237 | |
| 2238 | struct file_operations proc_cpuset_operations = { |
| 2239 | .open = cpuset_open, |
| 2240 | .read = seq_read, |
| 2241 | .llseek = seq_lseek, |
| 2242 | .release = single_release, |
| 2243 | }; |
| 2244 | |
| 2245 | /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */ |
| 2246 | char *cpuset_task_status_allowed(struct task_struct *task, char *buffer) |
| 2247 | { |
| 2248 | buffer += sprintf(buffer, "Cpus_allowed:\t"); |
| 2249 | buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed); |
| 2250 | buffer += sprintf(buffer, "\n"); |
| 2251 | buffer += sprintf(buffer, "Mems_allowed:\t"); |
| 2252 | buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed); |
| 2253 | buffer += sprintf(buffer, "\n"); |
| 2254 | return buffer; |
| 2255 | } |