ia64/linux-2.6.18-xen.hg

annotate kernel/cpuset.c @ 798:b02a90bf5bbc

ACPI: Backport missing part for T-State MSR support

Part of below kernel commit was missed while packporting T-State
support.

commit f79f06ab9f86d7203006d2ec8992ac80df36a34e
Author: Zhao Yakui <yakui.zhao@intel.com>
Date: Thu Nov 15 17:06:36 2007 +0800

ACPI: Enable MSR (FixedHW) support for T-States

Add throttling control via MSR when T-states uses
the FixHW Control Status registers.

Signed-off-by: Zhao Yakui <yakui.zhao@intel.com>
Signed-off-by: Li Shaohua <shaohua.li@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>

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