Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kernel/cpuset.c |
| 3 | * |
| 4 | * Processor and Memory placement constraints for sets of tasks. |
| 5 | * |
| 6 | * Copyright (C) 2003 BULL SA. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 7 | * Copyright (C) 2004-2007 Silicon Graphics, Inc. |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 8 | * Copyright (C) 2006 Google, Inc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * Portions derived from Patrick Mochel's sysfs code. |
| 11 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 13 | * 2003-10-10 Written by Simon Derr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * 2003-10-22 Updates by Stephen Hemminger. |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 15 | * 2004 May-July Rework by Paul Jackson. |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 16 | * 2006 Rework by Paul Menage to use generic cgroups |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 17 | * 2008 Rework of the scheduler domains and CPU hotplug handling |
| 18 | * by Max Krasnyansky |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * |
| 20 | * This file is subject to the terms and conditions of the GNU General Public |
| 21 | * License. See the file COPYING in the main directory of the Linux |
| 22 | * distribution for more details. |
| 23 | */ |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/cpu.h> |
| 26 | #include <linux/cpumask.h> |
| 27 | #include <linux/cpuset.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/fs.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/kmod.h> |
| 36 | #include <linux/list.h> |
Paul Jackson | 68860ec | 2005-10-30 15:02:36 -0800 | [diff] [blame] | 37 | #include <linux/mempolicy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/mm.h> |
Miao Xie | f481891 | 2008-11-19 15:36:30 -0800 | [diff] [blame] | 39 | #include <linux/memory.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 40 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/mount.h> |
| 42 | #include <linux/namei.h> |
| 43 | #include <linux/pagemap.h> |
| 44 | #include <linux/proc_fs.h> |
Paul Jackson | 6b9c260 | 2006-01-08 01:02:02 -0800 | [diff] [blame] | 45 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/sched.h> |
| 47 | #include <linux/seq_file.h> |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame] | 48 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/spinlock.h> |
| 51 | #include <linux/stat.h> |
| 52 | #include <linux/string.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/backing-dev.h> |
| 55 | #include <linux/sort.h> |
| 56 | |
| 57 | #include <asm/uaccess.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 58 | #include <linux/atomic.h> |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 59 | #include <linux/mutex.h> |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 60 | #include <linux/workqueue.h> |
| 61 | #include <linux/cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 63 | /* |
Miao Xie | f90d411 | 2009-01-16 10:24:10 +0800 | [diff] [blame] | 64 | * Workqueue for cpuset related tasks. |
| 65 | * |
| 66 | * Using kevent workqueue may cause deadlock when memory_migrate |
| 67 | * is set. So we create a separate workqueue thread for cpuset. |
| 68 | */ |
| 69 | static struct workqueue_struct *cpuset_wq; |
| 70 | |
| 71 | /* |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 72 | * Tracks how many cpusets are currently defined in system. |
| 73 | * When there is only one cpuset (the root cpuset) we can |
| 74 | * short circuit some hooks. |
| 75 | */ |
Paul Jackson | 7edc596 | 2006-01-08 01:02:03 -0800 | [diff] [blame] | 76 | int number_of_cpusets __read_mostly; |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 77 | |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 78 | /* Forward declare cgroup structures */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 79 | struct cgroup_subsys cpuset_subsys; |
| 80 | struct cpuset; |
| 81 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 82 | /* See "Frequency meter" comments, below. */ |
| 83 | |
| 84 | struct fmeter { |
| 85 | int cnt; /* unprocessed events count */ |
| 86 | int val; /* most recent output value */ |
| 87 | time_t time; /* clock (secs) when val computed */ |
| 88 | spinlock_t lock; /* guards read or write of above */ |
| 89 | }; |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | struct cpuset { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 92 | struct cgroup_subsys_state css; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | unsigned long flags; /* "unsigned long" so bitops work */ |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 95 | cpumask_var_t cpus_allowed; /* CPUs allowed to tasks in cpuset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */ |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct cpuset *parent; /* my parent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 100 | struct fmeter fmeter; /* memory_pressure filter */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 101 | |
| 102 | /* partition number for rebuild_sched_domains() */ |
| 103 | int pn; |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 104 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 105 | /* for custom sched domain */ |
| 106 | int relax_domain_level; |
| 107 | |
Uwe Kleine-König | 732bee7 | 2010-06-11 12:16:59 +0200 | [diff] [blame] | 108 | /* used for walking a cpuset hierarchy */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 109 | struct list_head stack_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 112 | /* Retrieve the cpuset for a cgroup */ |
| 113 | static inline struct cpuset *cgroup_cs(struct cgroup *cont) |
| 114 | { |
| 115 | return container_of(cgroup_subsys_state(cont, cpuset_subsys_id), |
| 116 | struct cpuset, css); |
| 117 | } |
| 118 | |
| 119 | /* Retrieve the cpuset for a task */ |
| 120 | static inline struct cpuset *task_cs(struct task_struct *task) |
| 121 | { |
| 122 | return container_of(task_subsys_state(task, cpuset_subsys_id), |
| 123 | struct cpuset, css); |
| 124 | } |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 125 | |
David Rientjes | b246272 | 2011-12-19 17:11:52 -0800 | [diff] [blame] | 126 | #ifdef CONFIG_NUMA |
| 127 | static inline bool task_has_mempolicy(struct task_struct *task) |
| 128 | { |
| 129 | return task->mempolicy; |
| 130 | } |
| 131 | #else |
| 132 | static inline bool task_has_mempolicy(struct task_struct *task) |
| 133 | { |
| 134 | return false; |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | /* bits in struct cpuset flags field */ |
| 140 | typedef enum { |
Tejun Heo | efeb77b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 141 | CS_ONLINE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | CS_CPU_EXCLUSIVE, |
| 143 | CS_MEM_EXCLUSIVE, |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 144 | CS_MEM_HARDWALL, |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 145 | CS_MEMORY_MIGRATE, |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 146 | CS_SCHED_LOAD_BALANCE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 147 | CS_SPREAD_PAGE, |
| 148 | CS_SPREAD_SLAB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } cpuset_flagbits_t; |
| 150 | |
| 151 | /* convenient tests for these bits */ |
Tejun Heo | efeb77b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 152 | static inline bool is_cpuset_online(const struct cpuset *cs) |
| 153 | { |
| 154 | return test_bit(CS_ONLINE, &cs->flags); |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static inline int is_cpu_exclusive(const struct cpuset *cs) |
| 158 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 159 | return test_bit(CS_CPU_EXCLUSIVE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static inline int is_mem_exclusive(const struct cpuset *cs) |
| 163 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 164 | return test_bit(CS_MEM_EXCLUSIVE, &cs->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 167 | static inline int is_mem_hardwall(const struct cpuset *cs) |
| 168 | { |
| 169 | return test_bit(CS_MEM_HARDWALL, &cs->flags); |
| 170 | } |
| 171 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 172 | static inline int is_sched_load_balance(const struct cpuset *cs) |
| 173 | { |
| 174 | return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
| 175 | } |
| 176 | |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 177 | static inline int is_memory_migrate(const struct cpuset *cs) |
| 178 | { |
Paul Jackson | 7b5b9ef | 2006-03-24 03:16:00 -0800 | [diff] [blame] | 179 | return test_bit(CS_MEMORY_MIGRATE, &cs->flags); |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 182 | static inline int is_spread_page(const struct cpuset *cs) |
| 183 | { |
| 184 | return test_bit(CS_SPREAD_PAGE, &cs->flags); |
| 185 | } |
| 186 | |
| 187 | static inline int is_spread_slab(const struct cpuset *cs) |
| 188 | { |
| 189 | return test_bit(CS_SPREAD_SLAB, &cs->flags); |
| 190 | } |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | static struct cpuset top_cpuset = { |
Tejun Heo | efeb77b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 193 | .flags = ((1 << CS_ONLINE) | (1 << CS_CPU_EXCLUSIVE) | |
| 194 | (1 << CS_MEM_EXCLUSIVE)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 197 | /** |
| 198 | * cpuset_for_each_child - traverse online children of a cpuset |
| 199 | * @child_cs: loop cursor pointing to the current child |
| 200 | * @pos_cgrp: used for iteration |
| 201 | * @parent_cs: target cpuset to walk children of |
| 202 | * |
| 203 | * Walk @child_cs through the online children of @parent_cs. Must be used |
| 204 | * with RCU read locked. |
| 205 | */ |
| 206 | #define cpuset_for_each_child(child_cs, pos_cgrp, parent_cs) \ |
| 207 | cgroup_for_each_child((pos_cgrp), (parent_cs)->css.cgroup) \ |
| 208 | if (is_cpuset_online(((child_cs) = cgroup_cs((pos_cgrp))))) |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 211 | * There are two global mutexes guarding cpuset structures. The first |
| 212 | * is the main control groups cgroup_mutex, accessed via |
| 213 | * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific |
| 214 | * callback_mutex, below. They can nest. It is ok to first take |
| 215 | * cgroup_mutex, then nest callback_mutex. We also require taking |
| 216 | * task_lock() when dereferencing a task's cpuset pointer. See "The |
| 217 | * task_lock() exception", at the end of this comment. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 219 | * A task must hold both mutexes to modify cpusets. If a task |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 220 | * holds cgroup_mutex, then it blocks others wanting that mutex, |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 221 | * ensuring that it is the only task able to also acquire callback_mutex |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 222 | * and be able to modify cpusets. It can perform various checks on |
| 223 | * the cpuset structure first, knowing nothing will change. It can |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 224 | * also allocate memory while just holding cgroup_mutex. While it is |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 225 | * performing these checks, various callback routines can briefly |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 226 | * acquire callback_mutex to query cpusets. Once it is ready to make |
| 227 | * the changes, it takes callback_mutex, blocking everyone else. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | * |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 229 | * Calls to the kernel memory allocator can not be made while holding |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 230 | * callback_mutex, as that would risk double tripping on callback_mutex |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 231 | * from one of the callbacks into the cpuset code from within |
| 232 | * __alloc_pages(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 234 | * If a task is only holding callback_mutex, then it has read-only |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 235 | * access to cpusets. |
| 236 | * |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 237 | * Now, the task_struct fields mems_allowed and mempolicy may be changed |
| 238 | * by other task, we use alloc_lock in the task_struct fields to protect |
| 239 | * them. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 240 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 241 | * The cpuset_common_file_read() handlers only hold callback_mutex across |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 242 | * small pieces of code, such as when reading out possibly multi-word |
| 243 | * cpumasks and nodemasks. |
| 244 | * |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 245 | * Accessing a task's cpuset should be done in accordance with the |
| 246 | * guidelines for accessing subsystem state in kernel/cgroup.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | */ |
| 248 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 249 | static DEFINE_MUTEX(callback_mutex); |
Paul Jackson | 4247bdc | 2005-09-10 00:26:06 -0700 | [diff] [blame] | 250 | |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 251 | /* |
David Rientjes | 75aa199 | 2009-01-06 14:39:01 -0800 | [diff] [blame] | 252 | * cpuset_buffer_lock protects both the cpuset_name and cpuset_nodelist |
| 253 | * buffers. They are statically allocated to prevent using excess stack |
| 254 | * when calling cpuset_print_task_mems_allowed(). |
| 255 | */ |
| 256 | #define CPUSET_NAME_LEN (128) |
| 257 | #define CPUSET_NODELIST_LEN (256) |
| 258 | static char cpuset_name[CPUSET_NAME_LEN]; |
| 259 | static char cpuset_nodelist[CPUSET_NODELIST_LEN]; |
| 260 | static DEFINE_SPINLOCK(cpuset_buffer_lock); |
| 261 | |
| 262 | /* |
Tejun Heo | 3a5a6d0 | 2013-01-07 08:51:07 -0800 | [diff] [blame^] | 263 | * CPU / memory hotplug is handled asynchronously. |
| 264 | */ |
| 265 | static void cpuset_hotplug_workfn(struct work_struct *work); |
| 266 | |
| 267 | static DECLARE_WORK(cpuset_hotplug_work, cpuset_hotplug_workfn); |
| 268 | |
| 269 | /* |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 270 | * This is ugly, but preserves the userspace API for existing cpuset |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 271 | * users. If someone tries to mount the "cpuset" filesystem, we |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 272 | * silently switch it to mount "cgroup" instead |
| 273 | */ |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 274 | static struct dentry *cpuset_mount(struct file_system_type *fs_type, |
| 275 | int flags, const char *unused_dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 277 | struct file_system_type *cgroup_fs = get_fs_type("cgroup"); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 278 | struct dentry *ret = ERR_PTR(-ENODEV); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 279 | if (cgroup_fs) { |
| 280 | char mountopts[] = |
| 281 | "cpuset,noprefix," |
| 282 | "release_agent=/sbin/cpuset_release_agent"; |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 283 | ret = cgroup_fs->mount(cgroup_fs, flags, |
| 284 | unused_dev_name, mountopts); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 285 | put_filesystem(cgroup_fs); |
| 286 | } |
| 287 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static struct file_system_type cpuset_fs_type = { |
| 291 | .name = "cpuset", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 292 | .mount = cpuset_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 296 | * Return in pmask the portion of a cpusets's cpus_allowed that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * are online. If none are online, walk up the cpuset hierarchy |
| 298 | * until we find one that does have some online cpus. If we get |
| 299 | * all the way to the top and still haven't found any online cpus, |
Rusty Russell | 5f054e3 | 2012-03-29 15:38:31 +1030 | [diff] [blame] | 300 | * return cpu_online_mask. Or if passed a NULL cs from an exit'ing |
| 301 | * task, return cpu_online_mask. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * |
| 303 | * One way or another, we guarantee to return some non-empty subset |
Rusty Russell | 5f054e3 | 2012-03-29 15:38:31 +1030 | [diff] [blame] | 304 | * of cpu_online_mask. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 306 | * Call with callback_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | */ |
| 308 | |
Li Zefan | 6af866a | 2009-01-07 18:08:45 -0800 | [diff] [blame] | 309 | static void guarantee_online_cpus(const struct cpuset *cs, |
| 310 | struct cpumask *pmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 312 | while (cs && !cpumask_intersects(cs->cpus_allowed, cpu_online_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | cs = cs->parent; |
| 314 | if (cs) |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 315 | cpumask_and(pmask, cs->cpus_allowed, cpu_online_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | else |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 317 | cpumask_copy(pmask, cpu_online_mask); |
| 318 | BUG_ON(!cpumask_intersects(pmask, cpu_online_mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Return in *pmask the portion of a cpusets's mems_allowed that |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 323 | * are online, with memory. If none are online with memory, walk |
| 324 | * up the cpuset hierarchy until we find one that does have some |
| 325 | * online mems. If we get all the way to the top and still haven't |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 326 | * found any online mems, return node_states[N_MEMORY]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | * |
| 328 | * One way or another, we guarantee to return some non-empty subset |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 329 | * of node_states[N_MEMORY]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | * |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 331 | * Call with callback_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | */ |
| 333 | |
| 334 | static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask) |
| 335 | { |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 336 | while (cs && !nodes_intersects(cs->mems_allowed, |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 337 | node_states[N_MEMORY])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | cs = cs->parent; |
| 339 | if (cs) |
Christoph Lameter | 0e1e7c7 | 2007-10-16 01:25:38 -0700 | [diff] [blame] | 340 | nodes_and(*pmask, cs->mems_allowed, |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 341 | node_states[N_MEMORY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | else |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 343 | *pmask = node_states[N_MEMORY]; |
| 344 | BUG_ON(!nodes_intersects(*pmask, node_states[N_MEMORY])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Miao Xie | f3b39d4 | 2009-06-16 15:31:46 -0700 | [diff] [blame] | 347 | /* |
| 348 | * update task's spread flag if cpuset's page/slab spread flag is set |
| 349 | * |
| 350 | * Called with callback_mutex/cgroup_mutex held |
| 351 | */ |
| 352 | static void cpuset_update_task_spread_flag(struct cpuset *cs, |
| 353 | struct task_struct *tsk) |
| 354 | { |
| 355 | if (is_spread_page(cs)) |
| 356 | tsk->flags |= PF_SPREAD_PAGE; |
| 357 | else |
| 358 | tsk->flags &= ~PF_SPREAD_PAGE; |
| 359 | if (is_spread_slab(cs)) |
| 360 | tsk->flags |= PF_SPREAD_SLAB; |
| 361 | else |
| 362 | tsk->flags &= ~PF_SPREAD_SLAB; |
| 363 | } |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | /* |
| 366 | * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q? |
| 367 | * |
| 368 | * One cpuset is a subset of another if all its allowed CPUs and |
| 369 | * Memory Nodes are a subset of the other, and its exclusive flags |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 370 | * are only set if the other's are set. Call holding cgroup_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | */ |
| 372 | |
| 373 | static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q) |
| 374 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 375 | return cpumask_subset(p->cpus_allowed, q->cpus_allowed) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | nodes_subset(p->mems_allowed, q->mems_allowed) && |
| 377 | is_cpu_exclusive(p) <= is_cpu_exclusive(q) && |
| 378 | is_mem_exclusive(p) <= is_mem_exclusive(q); |
| 379 | } |
| 380 | |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 381 | /** |
| 382 | * alloc_trial_cpuset - allocate a trial cpuset |
| 383 | * @cs: the cpuset that the trial cpuset duplicates |
| 384 | */ |
| 385 | static struct cpuset *alloc_trial_cpuset(const struct cpuset *cs) |
| 386 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 387 | struct cpuset *trial; |
| 388 | |
| 389 | trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL); |
| 390 | if (!trial) |
| 391 | return NULL; |
| 392 | |
| 393 | if (!alloc_cpumask_var(&trial->cpus_allowed, GFP_KERNEL)) { |
| 394 | kfree(trial); |
| 395 | return NULL; |
| 396 | } |
| 397 | cpumask_copy(trial->cpus_allowed, cs->cpus_allowed); |
| 398 | |
| 399 | return trial; |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
| 403 | * free_trial_cpuset - free the trial cpuset |
| 404 | * @trial: the trial cpuset to be freed |
| 405 | */ |
| 406 | static void free_trial_cpuset(struct cpuset *trial) |
| 407 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 408 | free_cpumask_var(trial->cpus_allowed); |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 409 | kfree(trial); |
| 410 | } |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* |
| 413 | * validate_change() - Used to validate that any proposed cpuset change |
| 414 | * follows the structural rules for cpusets. |
| 415 | * |
| 416 | * If we replaced the flag and mask values of the current cpuset |
| 417 | * (cur) with those values in the trial cpuset (trial), would |
| 418 | * our various subset and exclusive rules still be valid? Presumes |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 419 | * cgroup_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | * |
| 421 | * 'cur' is the address of an actual, in-use cpuset. Operations |
| 422 | * such as list traversal that depend on the actual address of the |
| 423 | * cpuset in the list must use cur below, not trial. |
| 424 | * |
| 425 | * 'trial' is the address of bulk structure copy of cur, with |
| 426 | * perhaps one or more of the fields cpus_allowed, mems_allowed, |
| 427 | * or flags changed to new, trial values. |
| 428 | * |
| 429 | * Return 0 if valid, -errno if not. |
| 430 | */ |
| 431 | |
| 432 | static int validate_change(const struct cpuset *cur, const struct cpuset *trial) |
| 433 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 434 | struct cgroup *cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | struct cpuset *c, *par; |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 436 | int ret; |
| 437 | |
| 438 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | /* Each of our child cpusets must be a subset of us */ |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 441 | ret = -EBUSY; |
| 442 | cpuset_for_each_child(c, cont, cur) |
| 443 | if (!is_cpuset_subset(c, trial)) |
| 444 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | /* Remaining checks don't apply to root cpuset */ |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 447 | ret = 0; |
Paul Jackson | 6960406 | 2006-12-06 20:36:15 -0800 | [diff] [blame] | 448 | if (cur == &top_cpuset) |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 449 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Paul Jackson | 6960406 | 2006-12-06 20:36:15 -0800 | [diff] [blame] | 451 | par = cur->parent; |
| 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | /* We must be a subset of our parent cpuset */ |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 454 | ret = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if (!is_cpuset_subset(trial, par)) |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 456 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 458 | /* |
| 459 | * If either I or some sibling (!= me) is exclusive, we can't |
| 460 | * overlap |
| 461 | */ |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 462 | ret = -EINVAL; |
| 463 | cpuset_for_each_child(c, cont, par) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) && |
| 465 | c != cur && |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 466 | cpumask_intersects(trial->cpus_allowed, c->cpus_allowed)) |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 467 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) && |
| 469 | c != cur && |
| 470 | nodes_intersects(trial->mems_allowed, c->mems_allowed)) |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 471 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Paul Jackson | 020958b | 2007-10-18 23:40:21 -0700 | [diff] [blame] | 474 | /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */ |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 475 | ret = -ENOSPC; |
| 476 | if (cgroup_task_count(cur->css.cgroup) && |
| 477 | (cpumask_empty(trial->cpus_allowed) || |
| 478 | nodes_empty(trial->mems_allowed))) |
| 479 | goto out; |
Paul Jackson | 020958b | 2007-10-18 23:40:21 -0700 | [diff] [blame] | 480 | |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 481 | ret = 0; |
| 482 | out: |
| 483 | rcu_read_unlock(); |
| 484 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Paul Menage | db7f47c | 2009-04-02 16:57:55 -0700 | [diff] [blame] | 487 | #ifdef CONFIG_SMP |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 488 | /* |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 489 | * Helper routine for generate_sched_domains(). |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 490 | * Do cpusets a, b have overlapping cpus_allowed masks? |
| 491 | */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 492 | static int cpusets_overlap(struct cpuset *a, struct cpuset *b) |
| 493 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 494 | return cpumask_intersects(a->cpus_allowed, b->cpus_allowed); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 497 | static void |
| 498 | update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c) |
| 499 | { |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 500 | if (dattr->relax_domain_level < c->relax_domain_level) |
| 501 | dattr->relax_domain_level = c->relax_domain_level; |
| 502 | return; |
| 503 | } |
| 504 | |
Lai Jiangshan | f539369 | 2008-07-29 22:33:22 -0700 | [diff] [blame] | 505 | static void |
| 506 | update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c) |
| 507 | { |
| 508 | LIST_HEAD(q); |
| 509 | |
| 510 | list_add(&c->stack_list, &q); |
| 511 | while (!list_empty(&q)) { |
| 512 | struct cpuset *cp; |
| 513 | struct cgroup *cont; |
| 514 | struct cpuset *child; |
| 515 | |
| 516 | cp = list_first_entry(&q, struct cpuset, stack_list); |
| 517 | list_del(q.next); |
| 518 | |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 519 | if (cpumask_empty(cp->cpus_allowed)) |
Lai Jiangshan | f539369 | 2008-07-29 22:33:22 -0700 | [diff] [blame] | 520 | continue; |
| 521 | |
| 522 | if (is_sched_load_balance(cp)) |
| 523 | update_domain_attr(dattr, cp); |
| 524 | |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 525 | rcu_read_lock(); |
| 526 | cpuset_for_each_child(child, cont, cp) |
Lai Jiangshan | f539369 | 2008-07-29 22:33:22 -0700 | [diff] [blame] | 527 | list_add_tail(&child->stack_list, &q); |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 528 | rcu_read_unlock(); |
Lai Jiangshan | f539369 | 2008-07-29 22:33:22 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 532 | /* |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 533 | * generate_sched_domains() |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 534 | * |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 535 | * This function builds a partial partition of the systems CPUs |
| 536 | * A 'partial partition' is a set of non-overlapping subsets whose |
| 537 | * union is a subset of that set. |
| 538 | * The output of this function needs to be passed to kernel/sched.c |
| 539 | * partition_sched_domains() routine, which will rebuild the scheduler's |
| 540 | * load balancing domains (sched domains) as specified by that partial |
| 541 | * partition. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 542 | * |
Li Zefan | 45ce80f | 2009-01-15 13:50:59 -0800 | [diff] [blame] | 543 | * See "What is sched_load_balance" in Documentation/cgroups/cpusets.txt |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 544 | * for a background explanation of this. |
| 545 | * |
| 546 | * Does not return errors, on the theory that the callers of this |
| 547 | * routine would rather not worry about failures to rebuild sched |
| 548 | * domains when operating in the severe memory shortage situations |
| 549 | * that could cause allocation failures below. |
| 550 | * |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 551 | * Must be called with cgroup_lock held. |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 552 | * |
| 553 | * The three key local variables below are: |
Li Zefan | aeed682 | 2008-07-29 22:33:24 -0700 | [diff] [blame] | 554 | * q - a linked-list queue of cpuset pointers, used to implement a |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 555 | * top-down scan of all cpusets. This scan loads a pointer |
| 556 | * to each cpuset marked is_sched_load_balance into the |
| 557 | * array 'csa'. For our purposes, rebuilding the schedulers |
| 558 | * sched domains, we can ignore !is_sched_load_balance cpusets. |
| 559 | * csa - (for CpuSet Array) Array of pointers to all the cpusets |
| 560 | * that need to be load balanced, for convenient iterative |
| 561 | * access by the subsequent code that finds the best partition, |
| 562 | * i.e the set of domains (subsets) of CPUs such that the |
| 563 | * cpus_allowed of every cpuset marked is_sched_load_balance |
| 564 | * is a subset of one of these domains, while there are as |
| 565 | * many such domains as possible, each as small as possible. |
| 566 | * doms - Conversion of 'csa' to an array of cpumasks, for passing to |
| 567 | * the kernel/sched.c routine partition_sched_domains() in a |
| 568 | * convenient format, that can be easily compared to the prior |
| 569 | * value to determine what partition elements (sched domains) |
| 570 | * were changed (added or removed.) |
| 571 | * |
| 572 | * Finding the best partition (set of domains): |
| 573 | * The triple nested loops below over i, j, k scan over the |
| 574 | * load balanced cpusets (using the array of cpuset pointers in |
| 575 | * csa[]) looking for pairs of cpusets that have overlapping |
| 576 | * cpus_allowed, but which don't have the same 'pn' partition |
| 577 | * number and gives them in the same partition number. It keeps |
| 578 | * looping on the 'restart' label until it can no longer find |
| 579 | * any such pairs. |
| 580 | * |
| 581 | * The union of the cpus_allowed masks from the set of |
| 582 | * all cpusets having the same 'pn' value then form the one |
| 583 | * element of the partition (one sched domain) to be passed to |
| 584 | * partition_sched_domains(). |
| 585 | */ |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 586 | static int generate_sched_domains(cpumask_var_t **domains, |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 587 | struct sched_domain_attr **attributes) |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 588 | { |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 589 | LIST_HEAD(q); /* queue of cpusets to be scanned */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 590 | struct cpuset *cp; /* scans q */ |
| 591 | struct cpuset **csa; /* array of all cpuset ptrs */ |
| 592 | int csn; /* how many cpuset ptrs in csa so far */ |
| 593 | int i, j, k; /* indices for partition finding loops */ |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 594 | cpumask_var_t *doms; /* resulting partition; i.e. sched domains */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 595 | struct sched_domain_attr *dattr; /* attributes for custom domains */ |
Ingo Molnar | 1583715 | 2008-11-25 10:27:49 +0100 | [diff] [blame] | 596 | int ndoms = 0; /* number of sched domains in result */ |
Li Zefan | 6af866a | 2009-01-07 18:08:45 -0800 | [diff] [blame] | 597 | int nslot; /* next empty doms[] struct cpumask slot */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 598 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 599 | doms = NULL; |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 600 | dattr = NULL; |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 601 | csa = NULL; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 602 | |
| 603 | /* Special case for the 99% of systems with one, full, sched domain */ |
| 604 | if (is_sched_load_balance(&top_cpuset)) { |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 605 | ndoms = 1; |
| 606 | doms = alloc_sched_domains(ndoms); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 607 | if (!doms) |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 608 | goto done; |
| 609 | |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 610 | dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL); |
| 611 | if (dattr) { |
| 612 | *dattr = SD_ATTR_INIT; |
Li Zefan | 93a6557 | 2008-07-29 22:33:23 -0700 | [diff] [blame] | 613 | update_domain_attr_tree(dattr, &top_cpuset); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 614 | } |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 615 | cpumask_copy(doms[0], top_cpuset.cpus_allowed); |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 616 | |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 617 | goto done; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 620 | csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL); |
| 621 | if (!csa) |
| 622 | goto done; |
| 623 | csn = 0; |
| 624 | |
Li Zefan | aeed682 | 2008-07-29 22:33:24 -0700 | [diff] [blame] | 625 | list_add(&top_cpuset.stack_list, &q); |
| 626 | while (!list_empty(&q)) { |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 627 | struct cgroup *cont; |
| 628 | struct cpuset *child; /* scans child cpusets of cp */ |
Lai Jiangshan | 489a539 | 2008-07-25 01:47:23 -0700 | [diff] [blame] | 629 | |
Li Zefan | aeed682 | 2008-07-29 22:33:24 -0700 | [diff] [blame] | 630 | cp = list_first_entry(&q, struct cpuset, stack_list); |
| 631 | list_del(q.next); |
| 632 | |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 633 | if (cpumask_empty(cp->cpus_allowed)) |
Lai Jiangshan | 489a539 | 2008-07-25 01:47:23 -0700 | [diff] [blame] | 634 | continue; |
| 635 | |
Lai Jiangshan | f539369 | 2008-07-29 22:33:22 -0700 | [diff] [blame] | 636 | /* |
| 637 | * All child cpusets contain a subset of the parent's cpus, so |
| 638 | * just skip them, and then we call update_domain_attr_tree() |
| 639 | * to calc relax_domain_level of the corresponding sched |
| 640 | * domain. |
| 641 | */ |
| 642 | if (is_sched_load_balance(cp)) { |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 643 | csa[csn++] = cp; |
Lai Jiangshan | f539369 | 2008-07-29 22:33:22 -0700 | [diff] [blame] | 644 | continue; |
| 645 | } |
Lai Jiangshan | 489a539 | 2008-07-25 01:47:23 -0700 | [diff] [blame] | 646 | |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 647 | rcu_read_lock(); |
| 648 | cpuset_for_each_child(child, cont, cp) |
Li Zefan | aeed682 | 2008-07-29 22:33:24 -0700 | [diff] [blame] | 649 | list_add_tail(&child->stack_list, &q); |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 650 | rcu_read_unlock(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | for (i = 0; i < csn; i++) |
| 654 | csa[i]->pn = i; |
| 655 | ndoms = csn; |
| 656 | |
| 657 | restart: |
| 658 | /* Find the best partition (set of sched domains) */ |
| 659 | for (i = 0; i < csn; i++) { |
| 660 | struct cpuset *a = csa[i]; |
| 661 | int apn = a->pn; |
| 662 | |
| 663 | for (j = 0; j < csn; j++) { |
| 664 | struct cpuset *b = csa[j]; |
| 665 | int bpn = b->pn; |
| 666 | |
| 667 | if (apn != bpn && cpusets_overlap(a, b)) { |
| 668 | for (k = 0; k < csn; k++) { |
| 669 | struct cpuset *c = csa[k]; |
| 670 | |
| 671 | if (c->pn == bpn) |
| 672 | c->pn = apn; |
| 673 | } |
| 674 | ndoms--; /* one less element */ |
| 675 | goto restart; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 680 | /* |
| 681 | * Now we know how many domains to create. |
| 682 | * Convert <csn, csa> to <ndoms, doms> and populate cpu masks. |
| 683 | */ |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 684 | doms = alloc_sched_domains(ndoms); |
Li Zefan | 700018e | 2008-11-18 14:02:03 +0800 | [diff] [blame] | 685 | if (!doms) |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 686 | goto done; |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 687 | |
| 688 | /* |
| 689 | * The rest of the code, including the scheduler, can deal with |
| 690 | * dattr==NULL case. No need to abort if alloc fails. |
| 691 | */ |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 692 | dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 693 | |
| 694 | for (nslot = 0, i = 0; i < csn; i++) { |
| 695 | struct cpuset *a = csa[i]; |
Li Zefan | 6af866a | 2009-01-07 18:08:45 -0800 | [diff] [blame] | 696 | struct cpumask *dp; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 697 | int apn = a->pn; |
| 698 | |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 699 | if (apn < 0) { |
| 700 | /* Skip completed partitions */ |
| 701 | continue; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 702 | } |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 703 | |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 704 | dp = doms[nslot]; |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 705 | |
| 706 | if (nslot == ndoms) { |
| 707 | static int warnings = 10; |
| 708 | if (warnings) { |
| 709 | printk(KERN_WARNING |
| 710 | "rebuild_sched_domains confused:" |
| 711 | " nslot %d, ndoms %d, csn %d, i %d," |
| 712 | " apn %d\n", |
| 713 | nslot, ndoms, csn, i, apn); |
| 714 | warnings--; |
| 715 | } |
| 716 | continue; |
| 717 | } |
| 718 | |
Li Zefan | 6af866a | 2009-01-07 18:08:45 -0800 | [diff] [blame] | 719 | cpumask_clear(dp); |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 720 | if (dattr) |
| 721 | *(dattr + nslot) = SD_ATTR_INIT; |
| 722 | for (j = i; j < csn; j++) { |
| 723 | struct cpuset *b = csa[j]; |
| 724 | |
| 725 | if (apn == b->pn) { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 726 | cpumask_or(dp, dp, b->cpus_allowed); |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 727 | if (dattr) |
| 728 | update_domain_attr_tree(dattr + nslot, b); |
| 729 | |
| 730 | /* Done with this partition */ |
| 731 | b->pn = -1; |
| 732 | } |
| 733 | } |
| 734 | nslot++; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 735 | } |
| 736 | BUG_ON(nslot != ndoms); |
| 737 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 738 | done: |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 739 | kfree(csa); |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 740 | |
Li Zefan | 700018e | 2008-11-18 14:02:03 +0800 | [diff] [blame] | 741 | /* |
| 742 | * Fallback to the default domain if kmalloc() failed. |
| 743 | * See comments in partition_sched_domains(). |
| 744 | */ |
| 745 | if (doms == NULL) |
| 746 | ndoms = 1; |
| 747 | |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 748 | *domains = doms; |
| 749 | *attributes = dattr; |
| 750 | return ndoms; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Rebuild scheduler domains. |
| 755 | * |
| 756 | * Call with neither cgroup_mutex held nor within get_online_cpus(). |
| 757 | * Takes both cgroup_mutex and get_online_cpus(). |
| 758 | * |
| 759 | * Cannot be directly called from cpuset code handling changes |
| 760 | * to the cpuset pseudo-filesystem, because it cannot be called |
| 761 | * from code that already holds cgroup_mutex. |
| 762 | */ |
| 763 | static void do_rebuild_sched_domains(struct work_struct *unused) |
| 764 | { |
| 765 | struct sched_domain_attr *attr; |
Rusty Russell | acc3f5d | 2009-11-03 14:53:40 +1030 | [diff] [blame] | 766 | cpumask_var_t *doms; |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 767 | int ndoms; |
| 768 | |
| 769 | get_online_cpus(); |
| 770 | |
| 771 | /* Generate domain masks and attrs */ |
| 772 | cgroup_lock(); |
| 773 | ndoms = generate_sched_domains(&doms, &attr); |
| 774 | cgroup_unlock(); |
| 775 | |
| 776 | /* Have scheduler rebuild the domains */ |
| 777 | partition_sched_domains(ndoms, doms, attr); |
| 778 | |
| 779 | put_online_cpus(); |
| 780 | } |
Paul Menage | db7f47c | 2009-04-02 16:57:55 -0700 | [diff] [blame] | 781 | #else /* !CONFIG_SMP */ |
| 782 | static void do_rebuild_sched_domains(struct work_struct *unused) |
| 783 | { |
| 784 | } |
| 785 | |
Geert Uytterhoeven | e1b8090 | 2009-12-06 20:41:16 +0100 | [diff] [blame] | 786 | static int generate_sched_domains(cpumask_var_t **domains, |
Paul Menage | db7f47c | 2009-04-02 16:57:55 -0700 | [diff] [blame] | 787 | struct sched_domain_attr **attributes) |
| 788 | { |
| 789 | *domains = NULL; |
| 790 | return 1; |
| 791 | } |
| 792 | #endif /* CONFIG_SMP */ |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 793 | |
| 794 | static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains); |
| 795 | |
| 796 | /* |
| 797 | * Rebuild scheduler domains, asynchronously via workqueue. |
| 798 | * |
| 799 | * If the flag 'sched_load_balance' of any cpuset with non-empty |
| 800 | * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset |
| 801 | * which has that flag enabled, or if any cpuset with a non-empty |
| 802 | * 'cpus' is removed, then call this routine to rebuild the |
| 803 | * scheduler's dynamic sched domains. |
| 804 | * |
| 805 | * The rebuild_sched_domains() and partition_sched_domains() |
| 806 | * routines must nest cgroup_lock() inside get_online_cpus(), |
| 807 | * but such cpuset changes as these must nest that locking the |
| 808 | * other way, holding cgroup_lock() for much of the code. |
| 809 | * |
| 810 | * So in order to avoid an ABBA deadlock, the cpuset code handling |
| 811 | * these user changes delegates the actual sched domain rebuilding |
| 812 | * to a separate workqueue thread, which ends up processing the |
| 813 | * above do_rebuild_sched_domains() function. |
| 814 | */ |
| 815 | static void async_rebuild_sched_domains(void) |
| 816 | { |
Miao Xie | f90d411 | 2009-01-16 10:24:10 +0800 | [diff] [blame] | 817 | queue_work(cpuset_wq, &rebuild_sched_domains_work); |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Accomplishes the same scheduler domain rebuild as the above |
| 822 | * async_rebuild_sched_domains(), however it directly calls the |
| 823 | * rebuild routine synchronously rather than calling it via an |
| 824 | * asynchronous work thread. |
| 825 | * |
| 826 | * This can only be called from code that is not holding |
| 827 | * cgroup_mutex (not nested in a cgroup_lock() call.) |
| 828 | */ |
| 829 | void rebuild_sched_domains(void) |
| 830 | { |
| 831 | do_rebuild_sched_domains(NULL); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 834 | /** |
| 835 | * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's |
| 836 | * @tsk: task to test |
| 837 | * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner |
| 838 | * |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 839 | * Call with cgroup_mutex held. May take callback_mutex during call. |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 840 | * Called for each task in a cgroup by cgroup_scan_tasks(). |
| 841 | * Return nonzero if this tasks's cpus_allowed mask should be changed (in other |
| 842 | * words, if its mask is not equal to its cpuset's mask). |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 843 | */ |
Adrian Bunk | 9e0c914 | 2008-04-29 01:00:25 -0700 | [diff] [blame] | 844 | static int cpuset_test_cpumask(struct task_struct *tsk, |
| 845 | struct cgroup_scanner *scan) |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 846 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 847 | return !cpumask_equal(&tsk->cpus_allowed, |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 848 | (cgroup_cs(scan->cg))->cpus_allowed); |
| 849 | } |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 850 | |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 851 | /** |
| 852 | * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's |
| 853 | * @tsk: task to test |
| 854 | * @scan: struct cgroup_scanner containing the cgroup of the task |
| 855 | * |
| 856 | * Called by cgroup_scan_tasks() for each task in a cgroup whose |
| 857 | * cpus_allowed mask needs to be changed. |
| 858 | * |
| 859 | * We don't need to re-check for the cgroup/cpuset membership, since we're |
| 860 | * holding cgroup_lock() at this point. |
| 861 | */ |
Adrian Bunk | 9e0c914 | 2008-04-29 01:00:25 -0700 | [diff] [blame] | 862 | static void cpuset_change_cpumask(struct task_struct *tsk, |
| 863 | struct cgroup_scanner *scan) |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 864 | { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 865 | set_cpus_allowed_ptr(tsk, ((cgroup_cs(scan->cg))->cpus_allowed)); |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /** |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 869 | * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset. |
| 870 | * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 871 | * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks() |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 872 | * |
| 873 | * Called with cgroup_mutex held |
| 874 | * |
| 875 | * The cgroup_scan_tasks() function will scan all the tasks in a cgroup, |
| 876 | * calling callback functions for each. |
| 877 | * |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 878 | * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0 |
| 879 | * if @heap != NULL. |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 880 | */ |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 881 | static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap) |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 882 | { |
| 883 | struct cgroup_scanner scan; |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 884 | |
| 885 | scan.cg = cs->css.cgroup; |
| 886 | scan.test_task = cpuset_test_cpumask; |
| 887 | scan.process_task = cpuset_change_cpumask; |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 888 | scan.heap = heap; |
| 889 | cgroup_scan_tasks(&scan); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /** |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 893 | * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it |
| 894 | * @cs: the cpuset to consider |
| 895 | * @buf: buffer of cpu numbers written to this cpuset |
| 896 | */ |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 897 | static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, |
| 898 | const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 900 | struct ptr_heap heap; |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 901 | int retval; |
| 902 | int is_load_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Rusty Russell | 5f054e3 | 2012-03-29 15:38:31 +1030 | [diff] [blame] | 904 | /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */ |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 905 | if (cs == &top_cpuset) |
| 906 | return -EACCES; |
| 907 | |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 908 | /* |
Paul Jackson | c8d9c90 | 2008-02-07 00:14:46 -0800 | [diff] [blame] | 909 | * An empty cpus_allowed is ok only if the cpuset has no tasks. |
Paul Jackson | 020958b | 2007-10-18 23:40:21 -0700 | [diff] [blame] | 910 | * Since cpulist_parse() fails on an empty mask, we special case |
| 911 | * that parsing. The validate_change() call ensures that cpusets |
| 912 | * with tasks have cpus. |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 913 | */ |
Paul Jackson | 020958b | 2007-10-18 23:40:21 -0700 | [diff] [blame] | 914 | if (!*buf) { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 915 | cpumask_clear(trialcs->cpus_allowed); |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 916 | } else { |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 917 | retval = cpulist_parse(buf, trialcs->cpus_allowed); |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 918 | if (retval < 0) |
| 919 | return retval; |
Lai Jiangshan | 3734074 | 2008-06-05 22:46:32 -0700 | [diff] [blame] | 920 | |
Peter Zijlstra | 6ad4c18 | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 921 | if (!cpumask_subset(trialcs->cpus_allowed, cpu_active_mask)) |
Lai Jiangshan | 3734074 | 2008-06-05 22:46:32 -0700 | [diff] [blame] | 922 | return -EINVAL; |
David Rientjes | 6f7f02e | 2007-05-08 00:31:43 -0700 | [diff] [blame] | 923 | } |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 924 | retval = validate_change(cs, trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 925 | if (retval < 0) |
| 926 | return retval; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 927 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 928 | /* Nothing to do if the cpus didn't change */ |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 929 | if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed)) |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 930 | return 0; |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 931 | |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 932 | retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL); |
| 933 | if (retval) |
| 934 | return retval; |
| 935 | |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 936 | is_load_balanced = is_sched_load_balance(trialcs); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 937 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 938 | mutex_lock(&callback_mutex); |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 939 | cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 940 | mutex_unlock(&callback_mutex); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 941 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 942 | /* |
| 943 | * Scan tasks in the cpuset, and update the cpumasks of any |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 944 | * that need an update. |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 945 | */ |
Li Zefan | 4e74339 | 2008-09-13 02:33:08 -0700 | [diff] [blame] | 946 | update_tasks_cpumask(cs, &heap); |
| 947 | |
| 948 | heap_free(&heap); |
Cliff Wickman | 58f4790 | 2008-02-07 00:14:44 -0800 | [diff] [blame] | 949 | |
Paul Menage | 8707d8b | 2007-10-18 23:40:22 -0700 | [diff] [blame] | 950 | if (is_load_balanced) |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 951 | async_rebuild_sched_domains(); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 952 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 955 | /* |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 956 | * cpuset_migrate_mm |
| 957 | * |
| 958 | * Migrate memory region from one set of nodes to another. |
| 959 | * |
| 960 | * Temporarilly set tasks mems_allowed to target nodes of migration, |
| 961 | * so that the migration code can allocate pages on these nodes. |
| 962 | * |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 963 | * Call holding cgroup_mutex, so current's cpuset won't change |
Paul Jackson | c8d9c90 | 2008-02-07 00:14:46 -0800 | [diff] [blame] | 964 | * during this call, as manage_mutex holds off any cpuset_attach() |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 965 | * calls. Therefore we don't need to take task_lock around the |
| 966 | * call to guarantee_online_mems(), as we know no one is changing |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 967 | * our task's cpuset. |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 968 | * |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 969 | * While the mm_struct we are migrating is typically from some |
| 970 | * other task, the task_struct mems_allowed that we are hacking |
| 971 | * is for our current task, which must allocate new pages for that |
| 972 | * migrating memory region. |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 973 | */ |
| 974 | |
| 975 | static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from, |
| 976 | const nodemask_t *to) |
| 977 | { |
| 978 | struct task_struct *tsk = current; |
| 979 | |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 980 | tsk->mems_allowed = *to; |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 981 | |
| 982 | do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL); |
| 983 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 984 | guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed); |
Paul Jackson | e4e364e | 2006-03-31 02:30:52 -0800 | [diff] [blame] | 985 | } |
| 986 | |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 987 | /* |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 988 | * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy |
| 989 | * @tsk: the task to change |
| 990 | * @newmems: new nodes that the task will be set |
| 991 | * |
| 992 | * In order to avoid seeing no nodes if the old and new nodes are disjoint, |
| 993 | * we structure updates as setting all new allowed nodes, then clearing newly |
| 994 | * disallowed ones. |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 995 | */ |
| 996 | static void cpuset_change_task_nodemask(struct task_struct *tsk, |
| 997 | nodemask_t *newmems) |
| 998 | { |
David Rientjes | b246272 | 2011-12-19 17:11:52 -0800 | [diff] [blame] | 999 | bool need_loop; |
David Rientjes | 89e8a24 | 2011-11-02 13:38:39 -0700 | [diff] [blame] | 1000 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 1001 | /* |
| 1002 | * Allow tasks that have access to memory reserves because they have |
| 1003 | * been OOM killed to get memory anywhere. |
| 1004 | */ |
| 1005 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 1006 | return; |
| 1007 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 1008 | return; |
| 1009 | |
| 1010 | task_lock(tsk); |
David Rientjes | b246272 | 2011-12-19 17:11:52 -0800 | [diff] [blame] | 1011 | /* |
| 1012 | * Determine if a loop is necessary if another thread is doing |
| 1013 | * get_mems_allowed(). If at least one node remains unchanged and |
| 1014 | * tsk does not have a mempolicy, then an empty nodemask will not be |
| 1015 | * possible when mems_allowed is larger than a word. |
| 1016 | */ |
| 1017 | need_loop = task_has_mempolicy(tsk) || |
| 1018 | !nodes_intersects(*newmems, tsk->mems_allowed); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 1019 | |
| 1020 | if (need_loop) |
| 1021 | write_seqcount_begin(&tsk->mems_allowed_seq); |
| 1022 | |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1023 | nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems); |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 1024 | mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP1); |
| 1025 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 1026 | mpol_rebind_task(tsk, newmems, MPOL_REBIND_STEP2); |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1027 | tsk->mems_allowed = *newmems; |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 1028 | |
| 1029 | if (need_loop) |
| 1030 | write_seqcount_end(&tsk->mems_allowed_seq); |
| 1031 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 1032 | task_unlock(tsk); |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Update task's mems_allowed and rebind its mempolicy and vmas' mempolicy |
| 1037 | * of it to cpuset's new mems_allowed, and migrate pages to new nodes if |
| 1038 | * memory_migrate flag is set. Called with cgroup_mutex held. |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1039 | */ |
| 1040 | static void cpuset_change_nodemask(struct task_struct *p, |
| 1041 | struct cgroup_scanner *scan) |
| 1042 | { |
| 1043 | struct mm_struct *mm; |
| 1044 | struct cpuset *cs; |
| 1045 | int migrate; |
| 1046 | const nodemask_t *oldmem = scan->data; |
Li Zefan | ee24d37 | 2011-03-23 16:42:47 -0700 | [diff] [blame] | 1047 | static nodemask_t newmems; /* protected by cgroup_mutex */ |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1048 | |
| 1049 | cs = cgroup_cs(scan->cg); |
Li Zefan | ee24d37 | 2011-03-23 16:42:47 -0700 | [diff] [blame] | 1050 | guarantee_online_mems(cs, &newmems); |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1051 | |
Li Zefan | ee24d37 | 2011-03-23 16:42:47 -0700 | [diff] [blame] | 1052 | cpuset_change_task_nodemask(p, &newmems); |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1053 | |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1054 | mm = get_task_mm(p); |
| 1055 | if (!mm) |
| 1056 | return; |
| 1057 | |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1058 | migrate = is_memory_migrate(cs); |
| 1059 | |
| 1060 | mpol_rebind_mm(mm, &cs->mems_allowed); |
| 1061 | if (migrate) |
| 1062 | cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed); |
| 1063 | mmput(mm); |
| 1064 | } |
| 1065 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1066 | static void *cpuset_being_rebound; |
| 1067 | |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1068 | /** |
| 1069 | * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset. |
| 1070 | * @cs: the cpuset in which each task's mems_allowed mask needs to be changed |
| 1071 | * @oldmem: old mems_allowed of cpuset cs |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1072 | * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks() |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1073 | * |
| 1074 | * Called with cgroup_mutex held |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1075 | * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0 |
| 1076 | * if @heap != NULL. |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1077 | */ |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1078 | static void update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem, |
| 1079 | struct ptr_heap *heap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1081 | struct cgroup_scanner scan; |
Paul Jackson | 59dac16 | 2006-01-08 01:01:52 -0800 | [diff] [blame] | 1082 | |
Lee Schermerhorn | 846a16b | 2008-04-28 02:13:09 -0700 | [diff] [blame] | 1083 | cpuset_being_rebound = cs; /* causes mpol_dup() rebind */ |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1084 | |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1085 | scan.cg = cs->css.cgroup; |
| 1086 | scan.test_task = NULL; |
| 1087 | scan.process_task = cpuset_change_nodemask; |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1088 | scan.heap = heap; |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1089 | scan.data = (nodemask_t *)oldmem; |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1090 | |
| 1091 | /* |
Li Zefan | 3b6766f | 2009-04-02 16:57:51 -0700 | [diff] [blame] | 1092 | * The mpol_rebind_mm() call takes mmap_sem, which we couldn't |
| 1093 | * take while holding tasklist_lock. Forks can happen - the |
| 1094 | * mpol_dup() cpuset_being_rebound check will catch such forks, |
| 1095 | * and rebind their vma mempolicies too. Because we still hold |
| 1096 | * the global cgroup_mutex, we know that no other rebind effort |
| 1097 | * will be contending for the global variable cpuset_being_rebound. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1098 | * It's ok if we rebind the same mm twice; mpol_rebind_mm() |
Paul Jackson | 04c19fa | 2006-01-08 01:02:00 -0800 | [diff] [blame] | 1099 | * is idempotent. Also migrate pages in each mm to new nodes. |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1100 | */ |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1101 | cgroup_scan_tasks(&scan); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1102 | |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 1103 | /* We're done rebinding vmas to this cpuset's new mems_allowed. */ |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1104 | cpuset_being_rebound = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1107 | /* |
| 1108 | * Handle user request to change the 'mems' memory placement |
| 1109 | * of a cpuset. Needs to validate the request, update the |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1110 | * cpusets mems_allowed, and for each task in the cpuset, |
| 1111 | * update mems_allowed and rebind task's mempolicy and any vma |
| 1112 | * mempolicies and if the cpuset is marked 'memory_migrate', |
| 1113 | * migrate the tasks pages to the new memory. |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1114 | * |
| 1115 | * Call with cgroup_mutex held. May take callback_mutex during call. |
| 1116 | * Will take tasklist_lock, scan tasklist for tasks in cpuset cs, |
| 1117 | * lock each such tasks mm->mmap_sem, scan its vma's and rebind |
| 1118 | * their mempolicies to the cpusets new mems_allowed. |
| 1119 | */ |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1120 | static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs, |
| 1121 | const char *buf) |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1122 | { |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1123 | NODEMASK_ALLOC(nodemask_t, oldmem, GFP_KERNEL); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1124 | int retval; |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1125 | struct ptr_heap heap; |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1126 | |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1127 | if (!oldmem) |
| 1128 | return -ENOMEM; |
| 1129 | |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1130 | /* |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 1131 | * top_cpuset.mems_allowed tracks node_stats[N_MEMORY]; |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1132 | * it's read-only |
| 1133 | */ |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1134 | if (cs == &top_cpuset) { |
| 1135 | retval = -EACCES; |
| 1136 | goto done; |
| 1137 | } |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1138 | |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1139 | /* |
| 1140 | * An empty mems_allowed is ok iff there are no tasks in the cpuset. |
| 1141 | * Since nodelist_parse() fails on an empty mask, we special case |
| 1142 | * that parsing. The validate_change() call ensures that cpusets |
| 1143 | * with tasks have memory. |
| 1144 | */ |
| 1145 | if (!*buf) { |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1146 | nodes_clear(trialcs->mems_allowed); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1147 | } else { |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1148 | retval = nodelist_parse(buf, trialcs->mems_allowed); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1149 | if (retval < 0) |
| 1150 | goto done; |
| 1151 | |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1152 | if (!nodes_subset(trialcs->mems_allowed, |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 1153 | node_states[N_MEMORY])) { |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1154 | retval = -EINVAL; |
| 1155 | goto done; |
| 1156 | } |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1157 | } |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1158 | *oldmem = cs->mems_allowed; |
| 1159 | if (nodes_equal(*oldmem, trialcs->mems_allowed)) { |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1160 | retval = 0; /* Too easy - nothing to do */ |
| 1161 | goto done; |
| 1162 | } |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1163 | retval = validate_change(cs, trialcs); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1164 | if (retval < 0) |
| 1165 | goto done; |
| 1166 | |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1167 | retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL); |
| 1168 | if (retval < 0) |
| 1169 | goto done; |
| 1170 | |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1171 | mutex_lock(&callback_mutex); |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1172 | cs->mems_allowed = trialcs->mems_allowed; |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1173 | mutex_unlock(&callback_mutex); |
| 1174 | |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1175 | update_tasks_nodemask(cs, oldmem, &heap); |
Li Zefan | 010cfac | 2009-04-02 16:57:52 -0700 | [diff] [blame] | 1176 | |
| 1177 | heap_free(&heap); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1178 | done: |
Miao Xie | 53feb29 | 2010-03-23 13:35:35 -0700 | [diff] [blame] | 1179 | NODEMASK_FREE(oldmem); |
Miao Xie | 0b2f630 | 2008-07-25 01:47:21 -0700 | [diff] [blame] | 1180 | return retval; |
| 1181 | } |
| 1182 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1183 | int current_cpuset_is_being_rebound(void) |
| 1184 | { |
| 1185 | return task_cs(current) == cpuset_being_rebound; |
| 1186 | } |
| 1187 | |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1188 | static int update_relax_domain_level(struct cpuset *cs, s64 val) |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1189 | { |
Paul Menage | db7f47c | 2009-04-02 16:57:55 -0700 | [diff] [blame] | 1190 | #ifdef CONFIG_SMP |
Peter Zijlstra | 60495e7 | 2011-04-07 14:10:04 +0200 | [diff] [blame] | 1191 | if (val < -1 || val >= sched_domain_level_max) |
Li Zefan | 30e0e17 | 2008-05-13 10:27:17 +0800 | [diff] [blame] | 1192 | return -EINVAL; |
Paul Menage | db7f47c | 2009-04-02 16:57:55 -0700 | [diff] [blame] | 1193 | #endif |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1194 | |
| 1195 | if (val != cs->relax_domain_level) { |
| 1196 | cs->relax_domain_level = val; |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 1197 | if (!cpumask_empty(cs->cpus_allowed) && |
| 1198 | is_sched_load_balance(cs)) |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 1199 | async_rebuild_sched_domains(); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1205 | /* |
Miao Xie | 950592f | 2009-06-16 15:31:47 -0700 | [diff] [blame] | 1206 | * cpuset_change_flag - make a task's spread flags the same as its cpuset's |
| 1207 | * @tsk: task to be updated |
| 1208 | * @scan: struct cgroup_scanner containing the cgroup of the task |
| 1209 | * |
| 1210 | * Called by cgroup_scan_tasks() for each task in a cgroup. |
| 1211 | * |
| 1212 | * We don't need to re-check for the cgroup/cpuset membership, since we're |
| 1213 | * holding cgroup_lock() at this point. |
| 1214 | */ |
| 1215 | static void cpuset_change_flag(struct task_struct *tsk, |
| 1216 | struct cgroup_scanner *scan) |
| 1217 | { |
| 1218 | cpuset_update_task_spread_flag(cgroup_cs(scan->cg), tsk); |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * update_tasks_flags - update the spread flags of tasks in the cpuset. |
| 1223 | * @cs: the cpuset in which each task's spread flags needs to be changed |
| 1224 | * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks() |
| 1225 | * |
| 1226 | * Called with cgroup_mutex held |
| 1227 | * |
| 1228 | * The cgroup_scan_tasks() function will scan all the tasks in a cgroup, |
| 1229 | * calling callback functions for each. |
| 1230 | * |
| 1231 | * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0 |
| 1232 | * if @heap != NULL. |
| 1233 | */ |
| 1234 | static void update_tasks_flags(struct cpuset *cs, struct ptr_heap *heap) |
| 1235 | { |
| 1236 | struct cgroup_scanner scan; |
| 1237 | |
| 1238 | scan.cg = cs->css.cgroup; |
| 1239 | scan.test_task = NULL; |
| 1240 | scan.process_task = cpuset_change_flag; |
| 1241 | scan.heap = heap; |
| 1242 | cgroup_scan_tasks(&scan); |
| 1243 | } |
| 1244 | |
| 1245 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | * update_flag - read a 0 or a 1 in a file and update associated flag |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1247 | * bit: the bit to update (see cpuset_flagbits_t) |
| 1248 | * cs: the cpuset to update |
| 1249 | * turning_on: whether the flag is being set or cleared |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1250 | * |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 1251 | * Call with cgroup_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | */ |
| 1253 | |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1254 | static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, |
| 1255 | int turning_on) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | { |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1257 | struct cpuset *trialcs; |
Rakib Mullick | 40b6a76 | 2008-10-18 20:28:18 -0700 | [diff] [blame] | 1258 | int balance_flag_changed; |
Miao Xie | 950592f | 2009-06-16 15:31:47 -0700 | [diff] [blame] | 1259 | int spread_flag_changed; |
| 1260 | struct ptr_heap heap; |
| 1261 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1263 | trialcs = alloc_trial_cpuset(cs); |
| 1264 | if (!trialcs) |
| 1265 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1267 | if (turning_on) |
| 1268 | set_bit(bit, &trialcs->flags); |
| 1269 | else |
| 1270 | clear_bit(bit, &trialcs->flags); |
| 1271 | |
| 1272 | err = validate_change(cs, trialcs); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1273 | if (err < 0) |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1274 | goto out; |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1275 | |
Miao Xie | 950592f | 2009-06-16 15:31:47 -0700 | [diff] [blame] | 1276 | err = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL); |
| 1277 | if (err < 0) |
| 1278 | goto out; |
| 1279 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1280 | balance_flag_changed = (is_sched_load_balance(cs) != |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1281 | is_sched_load_balance(trialcs)); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1282 | |
Miao Xie | 950592f | 2009-06-16 15:31:47 -0700 | [diff] [blame] | 1283 | spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs)) |
| 1284 | || (is_spread_page(cs) != is_spread_page(trialcs))); |
| 1285 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1286 | mutex_lock(&callback_mutex); |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1287 | cs->flags = trialcs->flags; |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1288 | mutex_unlock(&callback_mutex); |
Dinakar Guniguntala | 85d7b94 | 2005-06-25 14:57:34 -0700 | [diff] [blame] | 1289 | |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 1290 | if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed) |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 1291 | async_rebuild_sched_domains(); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1292 | |
Miao Xie | 950592f | 2009-06-16 15:31:47 -0700 | [diff] [blame] | 1293 | if (spread_flag_changed) |
| 1294 | update_tasks_flags(cs, &heap); |
| 1295 | heap_free(&heap); |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1296 | out: |
| 1297 | free_trial_cpuset(trialcs); |
| 1298 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 1301 | /* |
Adrian Bunk | 80f7228 | 2006-06-30 18:27:16 +0200 | [diff] [blame] | 1302 | * Frequency meter - How fast is some event occurring? |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1303 | * |
| 1304 | * These routines manage a digitally filtered, constant time based, |
| 1305 | * event frequency meter. There are four routines: |
| 1306 | * fmeter_init() - initialize a frequency meter. |
| 1307 | * fmeter_markevent() - called each time the event happens. |
| 1308 | * fmeter_getrate() - returns the recent rate of such events. |
| 1309 | * fmeter_update() - internal routine used to update fmeter. |
| 1310 | * |
| 1311 | * A common data structure is passed to each of these routines, |
| 1312 | * which is used to keep track of the state required to manage the |
| 1313 | * frequency meter and its digital filter. |
| 1314 | * |
| 1315 | * The filter works on the number of events marked per unit time. |
| 1316 | * The filter is single-pole low-pass recursive (IIR). The time unit |
| 1317 | * is 1 second. Arithmetic is done using 32-bit integers scaled to |
| 1318 | * simulate 3 decimal digits of precision (multiplied by 1000). |
| 1319 | * |
| 1320 | * With an FM_COEF of 933, and a time base of 1 second, the filter |
| 1321 | * has a half-life of 10 seconds, meaning that if the events quit |
| 1322 | * happening, then the rate returned from the fmeter_getrate() |
| 1323 | * will be cut in half each 10 seconds, until it converges to zero. |
| 1324 | * |
| 1325 | * It is not worth doing a real infinitely recursive filter. If more |
| 1326 | * than FM_MAXTICKS ticks have elapsed since the last filter event, |
| 1327 | * just compute FM_MAXTICKS ticks worth, by which point the level |
| 1328 | * will be stable. |
| 1329 | * |
| 1330 | * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid |
| 1331 | * arithmetic overflow in the fmeter_update() routine. |
| 1332 | * |
| 1333 | * Given the simple 32 bit integer arithmetic used, this meter works |
| 1334 | * best for reporting rates between one per millisecond (msec) and |
| 1335 | * one per 32 (approx) seconds. At constant rates faster than one |
| 1336 | * per msec it maxes out at values just under 1,000,000. At constant |
| 1337 | * rates between one per msec, and one per second it will stabilize |
| 1338 | * to a value N*1000, where N is the rate of events per second. |
| 1339 | * At constant rates between one per second and one per 32 seconds, |
| 1340 | * it will be choppy, moving up on the seconds that have an event, |
| 1341 | * and then decaying until the next event. At rates slower than |
| 1342 | * about one in 32 seconds, it decays all the way back to zero between |
| 1343 | * each event. |
| 1344 | */ |
| 1345 | |
| 1346 | #define FM_COEF 933 /* coefficient for half-life of 10 secs */ |
| 1347 | #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */ |
| 1348 | #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */ |
| 1349 | #define FM_SCALE 1000 /* faux fixed point scale */ |
| 1350 | |
| 1351 | /* Initialize a frequency meter */ |
| 1352 | static void fmeter_init(struct fmeter *fmp) |
| 1353 | { |
| 1354 | fmp->cnt = 0; |
| 1355 | fmp->val = 0; |
| 1356 | fmp->time = 0; |
| 1357 | spin_lock_init(&fmp->lock); |
| 1358 | } |
| 1359 | |
| 1360 | /* Internal meter update - process cnt events and update value */ |
| 1361 | static void fmeter_update(struct fmeter *fmp) |
| 1362 | { |
| 1363 | time_t now = get_seconds(); |
| 1364 | time_t ticks = now - fmp->time; |
| 1365 | |
| 1366 | if (ticks == 0) |
| 1367 | return; |
| 1368 | |
| 1369 | ticks = min(FM_MAXTICKS, ticks); |
| 1370 | while (ticks-- > 0) |
| 1371 | fmp->val = (FM_COEF * fmp->val) / FM_SCALE; |
| 1372 | fmp->time = now; |
| 1373 | |
| 1374 | fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE; |
| 1375 | fmp->cnt = 0; |
| 1376 | } |
| 1377 | |
| 1378 | /* Process any previous ticks, then bump cnt by one (times scale). */ |
| 1379 | static void fmeter_markevent(struct fmeter *fmp) |
| 1380 | { |
| 1381 | spin_lock(&fmp->lock); |
| 1382 | fmeter_update(fmp); |
| 1383 | fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE); |
| 1384 | spin_unlock(&fmp->lock); |
| 1385 | } |
| 1386 | |
| 1387 | /* Process any previous ticks, then return current value. */ |
| 1388 | static int fmeter_getrate(struct fmeter *fmp) |
| 1389 | { |
| 1390 | int val; |
| 1391 | |
| 1392 | spin_lock(&fmp->lock); |
| 1393 | fmeter_update(fmp); |
| 1394 | val = fmp->val; |
| 1395 | spin_unlock(&fmp->lock); |
| 1396 | return val; |
| 1397 | } |
| 1398 | |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 1399 | /* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */ |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1400 | static int cpuset_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 1401 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1402 | struct cpuset *cs = cgroup_cs(cgrp); |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1403 | struct task_struct *task; |
| 1404 | int ret; |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 1405 | |
Ben Blum | be367d0 | 2009-09-23 15:56:31 -0700 | [diff] [blame] | 1406 | if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
| 1407 | return -ENOSPC; |
| 1408 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1409 | cgroup_taskset_for_each(task, cgrp, tset) { |
| 1410 | /* |
| 1411 | * Kthreads bound to specific cpus cannot be moved to a new |
| 1412 | * cpuset; we cannot change their cpu affinity and |
| 1413 | * isolating such threads by their set of allowed nodes is |
| 1414 | * unnecessary. Thus, cpusets are not applicable for such |
| 1415 | * threads. This prevents checking for success of |
| 1416 | * set_cpus_allowed_ptr() on all attached tasks before |
| 1417 | * cpus_allowed may be changed. |
| 1418 | */ |
| 1419 | if (task->flags & PF_THREAD_BOUND) |
| 1420 | return -EINVAL; |
| 1421 | if ((ret = security_task_setscheduler(task))) |
| 1422 | return ret; |
| 1423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Tejun Heo | 4e4c9a1 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | * Protected by cgroup_mutex. cpus_attach is used only by cpuset_attach() |
| 1430 | * but we can't allocate it dynamically there. Define it global and |
| 1431 | * allocate from cpuset_init(). |
| 1432 | */ |
| 1433 | static cpumask_var_t cpus_attach; |
| 1434 | |
| 1435 | static void cpuset_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
| 1436 | { |
| 1437 | /* static bufs protected by cgroup_mutex */ |
| 1438 | static nodemask_t cpuset_attach_nodemask_from; |
| 1439 | static nodemask_t cpuset_attach_nodemask_to; |
| 1440 | struct mm_struct *mm; |
| 1441 | struct task_struct *task; |
| 1442 | struct task_struct *leader = cgroup_taskset_first(tset); |
| 1443 | struct cgroup *oldcgrp = cgroup_taskset_cur_cgroup(tset); |
| 1444 | struct cpuset *cs = cgroup_cs(cgrp); |
| 1445 | struct cpuset *oldcs = cgroup_cs(oldcgrp); |
| 1446 | |
Tejun Heo | 94196f5 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 1447 | /* prepare for attach */ |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 1448 | if (cs == &top_cpuset) |
| 1449 | cpumask_copy(cpus_attach, cpu_possible_mask); |
| 1450 | else |
| 1451 | guarantee_online_cpus(cs, cpus_attach); |
| 1452 | |
| 1453 | guarantee_online_mems(cs, &cpuset_attach_nodemask_to); |
Tejun Heo | 94196f5 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 1454 | |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1455 | cgroup_taskset_for_each(task, cgrp, tset) { |
| 1456 | /* |
| 1457 | * can_attach beforehand should guarantee that this doesn't |
| 1458 | * fail. TODO: have a better way to handle failure here |
| 1459 | */ |
| 1460 | WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach)); |
| 1461 | |
| 1462 | cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to); |
| 1463 | cpuset_update_task_spread_flag(cs, task); |
| 1464 | } |
David Quigley | 22fb52d | 2006-06-23 02:04:00 -0700 | [diff] [blame] | 1465 | |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 1466 | /* |
| 1467 | * Change mm, possibly for multiple threads in a threadgroup. This is |
| 1468 | * expensive and may sleep. |
| 1469 | */ |
| 1470 | cpuset_attach_nodemask_from = oldcs->mems_allowed; |
| 1471 | cpuset_attach_nodemask_to = cs->mems_allowed; |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1472 | mm = get_task_mm(leader); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1473 | if (mm) { |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 1474 | mpol_rebind_mm(mm, &cpuset_attach_nodemask_to); |
Paul Jackson | 2741a55 | 2006-03-31 02:30:51 -0800 | [diff] [blame] | 1475 | if (is_memory_migrate(cs)) |
Ben Blum | f780bdb | 2011-05-26 16:25:19 -0700 | [diff] [blame] | 1476 | cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from, |
| 1477 | &cpuset_attach_nodemask_to); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1478 | mmput(mm); |
| 1479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | /* The various types of files and directories in a cpuset file system */ |
| 1483 | |
| 1484 | typedef enum { |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1485 | FILE_MEMORY_MIGRATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | FILE_CPULIST, |
| 1487 | FILE_MEMLIST, |
| 1488 | FILE_CPU_EXCLUSIVE, |
| 1489 | FILE_MEM_EXCLUSIVE, |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1490 | FILE_MEM_HARDWALL, |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1491 | FILE_SCHED_LOAD_BALANCE, |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1492 | FILE_SCHED_RELAX_DOMAIN_LEVEL, |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1493 | FILE_MEMORY_PRESSURE_ENABLED, |
| 1494 | FILE_MEMORY_PRESSURE, |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 1495 | FILE_SPREAD_PAGE, |
| 1496 | FILE_SPREAD_SLAB, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } cpuset_filetype_t; |
| 1498 | |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1499 | static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val) |
| 1500 | { |
| 1501 | int retval = 0; |
| 1502 | struct cpuset *cs = cgroup_cs(cgrp); |
| 1503 | cpuset_filetype_t type = cft->private; |
| 1504 | |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1505 | if (!cgroup_lock_live_group(cgrp)) |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1506 | return -ENODEV; |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1507 | |
| 1508 | switch (type) { |
| 1509 | case FILE_CPU_EXCLUSIVE: |
| 1510 | retval = update_flag(CS_CPU_EXCLUSIVE, cs, val); |
| 1511 | break; |
| 1512 | case FILE_MEM_EXCLUSIVE: |
| 1513 | retval = update_flag(CS_MEM_EXCLUSIVE, cs, val); |
| 1514 | break; |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1515 | case FILE_MEM_HARDWALL: |
| 1516 | retval = update_flag(CS_MEM_HARDWALL, cs, val); |
| 1517 | break; |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1518 | case FILE_SCHED_LOAD_BALANCE: |
| 1519 | retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val); |
| 1520 | break; |
| 1521 | case FILE_MEMORY_MIGRATE: |
| 1522 | retval = update_flag(CS_MEMORY_MIGRATE, cs, val); |
| 1523 | break; |
| 1524 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1525 | cpuset_memory_pressure_enabled = !!val; |
| 1526 | break; |
| 1527 | case FILE_MEMORY_PRESSURE: |
| 1528 | retval = -EACCES; |
| 1529 | break; |
| 1530 | case FILE_SPREAD_PAGE: |
| 1531 | retval = update_flag(CS_SPREAD_PAGE, cs, val); |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1532 | break; |
| 1533 | case FILE_SPREAD_SLAB: |
| 1534 | retval = update_flag(CS_SPREAD_SLAB, cs, val); |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1535 | break; |
| 1536 | default: |
| 1537 | retval = -EINVAL; |
| 1538 | break; |
| 1539 | } |
| 1540 | cgroup_unlock(); |
| 1541 | return retval; |
| 1542 | } |
| 1543 | |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1544 | static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val) |
| 1545 | { |
| 1546 | int retval = 0; |
| 1547 | struct cpuset *cs = cgroup_cs(cgrp); |
| 1548 | cpuset_filetype_t type = cft->private; |
| 1549 | |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1550 | if (!cgroup_lock_live_group(cgrp)) |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1551 | return -ENODEV; |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1552 | |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1553 | switch (type) { |
| 1554 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: |
| 1555 | retval = update_relax_domain_level(cs, val); |
| 1556 | break; |
| 1557 | default: |
| 1558 | retval = -EINVAL; |
| 1559 | break; |
| 1560 | } |
| 1561 | cgroup_unlock(); |
| 1562 | return retval; |
| 1563 | } |
| 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | /* |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1566 | * Common handling for a write to a "cpus" or "mems" file. |
| 1567 | */ |
| 1568 | static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft, |
| 1569 | const char *buf) |
| 1570 | { |
| 1571 | int retval = 0; |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1572 | struct cpuset *cs = cgroup_cs(cgrp); |
| 1573 | struct cpuset *trialcs; |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1574 | |
Tejun Heo | 3a5a6d0 | 2013-01-07 08:51:07 -0800 | [diff] [blame^] | 1575 | /* |
| 1576 | * CPU or memory hotunplug may leave @cs w/o any execution |
| 1577 | * resources, in which case the hotplug code asynchronously updates |
| 1578 | * configuration and transfers all tasks to the nearest ancestor |
| 1579 | * which can execute. |
| 1580 | * |
| 1581 | * As writes to "cpus" or "mems" may restore @cs's execution |
| 1582 | * resources, wait for the previously scheduled operations before |
| 1583 | * proceeding, so that we don't end up keep removing tasks added |
| 1584 | * after execution capability is restored. |
| 1585 | */ |
| 1586 | flush_work(&cpuset_hotplug_work); |
| 1587 | |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1588 | if (!cgroup_lock_live_group(cgrp)) |
| 1589 | return -ENODEV; |
| 1590 | |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1591 | trialcs = alloc_trial_cpuset(cs); |
Li Zefan | b75f38d | 2011-03-04 17:36:21 -0800 | [diff] [blame] | 1592 | if (!trialcs) { |
| 1593 | retval = -ENOMEM; |
| 1594 | goto out; |
| 1595 | } |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1596 | |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1597 | switch (cft->private) { |
| 1598 | case FILE_CPULIST: |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1599 | retval = update_cpumask(cs, trialcs, buf); |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1600 | break; |
| 1601 | case FILE_MEMLIST: |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1602 | retval = update_nodemask(cs, trialcs, buf); |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1603 | break; |
| 1604 | default: |
| 1605 | retval = -EINVAL; |
| 1606 | break; |
| 1607 | } |
Li Zefan | 645fcc9 | 2009-01-07 18:08:43 -0800 | [diff] [blame] | 1608 | |
| 1609 | free_trial_cpuset(trialcs); |
Li Zefan | b75f38d | 2011-03-04 17:36:21 -0800 | [diff] [blame] | 1610 | out: |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1611 | cgroup_unlock(); |
| 1612 | return retval; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | * These ascii lists should be read in a single call, by using a user |
| 1617 | * buffer large enough to hold the entire map. If read in smaller |
| 1618 | * chunks, there is no guarantee of atomicity. Since the display format |
| 1619 | * used, list of ranges of sequential numbers, is variable length, |
| 1620 | * and since these maps can change value dynamically, one could read |
| 1621 | * gibberish by doing partial reads while a list was changing. |
| 1622 | * A single large read to a buffer that crosses a page boundary is |
| 1623 | * ok, because the result being copied to user land is not recomputed |
| 1624 | * across a page fault. |
| 1625 | */ |
| 1626 | |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1627 | static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1629 | size_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1631 | mutex_lock(&callback_mutex); |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1632 | count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1633 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1635 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1638 | static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | { |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1640 | size_t count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1642 | mutex_lock(&callback_mutex); |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1643 | count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 1644 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Li Zefan | 9303e0c | 2011-03-23 16:42:45 -0700 | [diff] [blame] | 1646 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1649 | static ssize_t cpuset_common_file_read(struct cgroup *cont, |
| 1650 | struct cftype *cft, |
| 1651 | struct file *file, |
| 1652 | char __user *buf, |
| 1653 | size_t nbytes, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1655 | struct cpuset *cs = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | cpuset_filetype_t type = cft->private; |
| 1657 | char *page; |
| 1658 | ssize_t retval = 0; |
| 1659 | char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1661 | if (!(page = (char *)__get_free_page(GFP_TEMPORARY))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | return -ENOMEM; |
| 1663 | |
| 1664 | s = page; |
| 1665 | |
| 1666 | switch (type) { |
| 1667 | case FILE_CPULIST: |
| 1668 | s += cpuset_sprintf_cpulist(s, cs); |
| 1669 | break; |
| 1670 | case FILE_MEMLIST: |
| 1671 | s += cpuset_sprintf_memlist(s, cs); |
| 1672 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | default: |
| 1674 | retval = -EINVAL; |
| 1675 | goto out; |
| 1676 | } |
| 1677 | *s++ = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
Al Viro | eacaa1f | 2005-09-30 03:26:43 +0100 | [diff] [blame] | 1679 | retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | out: |
| 1681 | free_page((unsigned long)page); |
| 1682 | return retval; |
| 1683 | } |
| 1684 | |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1685 | static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft) |
| 1686 | { |
| 1687 | struct cpuset *cs = cgroup_cs(cont); |
| 1688 | cpuset_filetype_t type = cft->private; |
| 1689 | switch (type) { |
| 1690 | case FILE_CPU_EXCLUSIVE: |
| 1691 | return is_cpu_exclusive(cs); |
| 1692 | case FILE_MEM_EXCLUSIVE: |
| 1693 | return is_mem_exclusive(cs); |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1694 | case FILE_MEM_HARDWALL: |
| 1695 | return is_mem_hardwall(cs); |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1696 | case FILE_SCHED_LOAD_BALANCE: |
| 1697 | return is_sched_load_balance(cs); |
| 1698 | case FILE_MEMORY_MIGRATE: |
| 1699 | return is_memory_migrate(cs); |
| 1700 | case FILE_MEMORY_PRESSURE_ENABLED: |
| 1701 | return cpuset_memory_pressure_enabled; |
| 1702 | case FILE_MEMORY_PRESSURE: |
| 1703 | return fmeter_getrate(&cs->fmeter); |
| 1704 | case FILE_SPREAD_PAGE: |
| 1705 | return is_spread_page(cs); |
| 1706 | case FILE_SPREAD_SLAB: |
| 1707 | return is_spread_slab(cs); |
| 1708 | default: |
| 1709 | BUG(); |
| 1710 | } |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 1711 | |
| 1712 | /* Unreachable but makes gcc happy */ |
| 1713 | return 0; |
Paul Menage | 700fe1a | 2008-04-29 01:00:00 -0700 | [diff] [blame] | 1714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1716 | static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft) |
| 1717 | { |
| 1718 | struct cpuset *cs = cgroup_cs(cont); |
| 1719 | cpuset_filetype_t type = cft->private; |
| 1720 | switch (type) { |
| 1721 | case FILE_SCHED_RELAX_DOMAIN_LEVEL: |
| 1722 | return cs->relax_domain_level; |
| 1723 | default: |
| 1724 | BUG(); |
| 1725 | } |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 1726 | |
| 1727 | /* Unrechable but makes gcc happy */ |
| 1728 | return 0; |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
| 1732 | /* |
| 1733 | * for the common functions, 'private' gives the type of file |
| 1734 | */ |
| 1735 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1736 | static struct cftype files[] = { |
| 1737 | { |
| 1738 | .name = "cpus", |
| 1739 | .read = cpuset_common_file_read, |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1740 | .write_string = cpuset_write_resmask, |
| 1741 | .max_write_len = (100U + 6 * NR_CPUS), |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1742 | .private = FILE_CPULIST, |
| 1743 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1745 | { |
| 1746 | .name = "mems", |
| 1747 | .read = cpuset_common_file_read, |
Paul Menage | e371239 | 2008-07-25 01:47:02 -0700 | [diff] [blame] | 1748 | .write_string = cpuset_write_resmask, |
| 1749 | .max_write_len = (100U + 6 * MAX_NUMNODES), |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1750 | .private = FILE_MEMLIST, |
| 1751 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1753 | { |
| 1754 | .name = "cpu_exclusive", |
| 1755 | .read_u64 = cpuset_read_u64, |
| 1756 | .write_u64 = cpuset_write_u64, |
| 1757 | .private = FILE_CPU_EXCLUSIVE, |
| 1758 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1760 | { |
| 1761 | .name = "mem_exclusive", |
| 1762 | .read_u64 = cpuset_read_u64, |
| 1763 | .write_u64 = cpuset_write_u64, |
| 1764 | .private = FILE_MEM_EXCLUSIVE, |
| 1765 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1767 | { |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1768 | .name = "mem_hardwall", |
| 1769 | .read_u64 = cpuset_read_u64, |
| 1770 | .write_u64 = cpuset_write_u64, |
| 1771 | .private = FILE_MEM_HARDWALL, |
| 1772 | }, |
| 1773 | |
| 1774 | { |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1775 | .name = "sched_load_balance", |
| 1776 | .read_u64 = cpuset_read_u64, |
| 1777 | .write_u64 = cpuset_write_u64, |
| 1778 | .private = FILE_SCHED_LOAD_BALANCE, |
| 1779 | }, |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1780 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1781 | { |
| 1782 | .name = "sched_relax_domain_level", |
Paul Menage | 5be7a47 | 2008-05-06 20:42:41 -0700 | [diff] [blame] | 1783 | .read_s64 = cpuset_read_s64, |
| 1784 | .write_s64 = cpuset_write_s64, |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1785 | .private = FILE_SCHED_RELAX_DOMAIN_LEVEL, |
| 1786 | }, |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1787 | |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1788 | { |
| 1789 | .name = "memory_migrate", |
| 1790 | .read_u64 = cpuset_read_u64, |
| 1791 | .write_u64 = cpuset_write_u64, |
| 1792 | .private = FILE_MEMORY_MIGRATE, |
| 1793 | }, |
| 1794 | |
| 1795 | { |
| 1796 | .name = "memory_pressure", |
| 1797 | .read_u64 = cpuset_read_u64, |
| 1798 | .write_u64 = cpuset_write_u64, |
| 1799 | .private = FILE_MEMORY_PRESSURE, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 1800 | .mode = S_IRUGO, |
Paul Menage | addf2c7 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 1801 | }, |
| 1802 | |
| 1803 | { |
| 1804 | .name = "memory_spread_page", |
| 1805 | .read_u64 = cpuset_read_u64, |
| 1806 | .write_u64 = cpuset_write_u64, |
| 1807 | .private = FILE_SPREAD_PAGE, |
| 1808 | }, |
| 1809 | |
| 1810 | { |
| 1811 | .name = "memory_spread_slab", |
| 1812 | .read_u64 = cpuset_read_u64, |
| 1813 | .write_u64 = cpuset_write_u64, |
| 1814 | .private = FILE_SPREAD_SLAB, |
| 1815 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1816 | |
| 1817 | { |
| 1818 | .name = "memory_pressure_enabled", |
| 1819 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 1820 | .read_u64 = cpuset_read_u64, |
| 1821 | .write_u64 = cpuset_write_u64, |
| 1822 | .private = FILE_MEMORY_PRESSURE_ENABLED, |
| 1823 | }, |
| 1824 | |
| 1825 | { } /* terminate */ |
Paul Jackson | 45b07ef | 2006-01-08 01:00:56 -0800 | [diff] [blame] | 1826 | }; |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1829 | * cpuset_css_alloc - allocate a cpuset css |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 1830 | * cont: control group that the new cpuset will be part of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | */ |
| 1832 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1833 | static struct cgroup_subsys_state *cpuset_css_alloc(struct cgroup *cont) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | { |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1835 | struct cpuset *cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1837 | if (!cont->parent) |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1838 | return &top_cpuset.css; |
Tejun Heo | 033fa1c | 2012-11-19 08:13:39 -0800 | [diff] [blame] | 1839 | |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1840 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | if (!cs) |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1842 | return ERR_PTR(-ENOMEM); |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 1843 | if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) { |
| 1844 | kfree(cs); |
| 1845 | return ERR_PTR(-ENOMEM); |
| 1846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1848 | set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags); |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 1849 | cpumask_clear(cs->cpus_allowed); |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 1850 | nodes_clear(cs->mems_allowed); |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1851 | fmeter_init(&cs->fmeter); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1852 | cs->relax_domain_level = -1; |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1853 | cs->parent = cgroup_cs(cont->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1855 | return &cs->css; |
| 1856 | } |
| 1857 | |
| 1858 | static int cpuset_css_online(struct cgroup *cgrp) |
| 1859 | { |
| 1860 | struct cpuset *cs = cgroup_cs(cgrp); |
| 1861 | struct cpuset *parent = cs->parent; |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1862 | struct cpuset *tmp_cs; |
| 1863 | struct cgroup *pos_cg; |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1864 | |
| 1865 | if (!parent) |
| 1866 | return 0; |
| 1867 | |
Tejun Heo | efeb77b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1868 | set_bit(CS_ONLINE, &cs->flags); |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1869 | if (is_spread_page(parent)) |
| 1870 | set_bit(CS_SPREAD_PAGE, &cs->flags); |
| 1871 | if (is_spread_slab(parent)) |
| 1872 | set_bit(CS_SPREAD_SLAB, &cs->flags); |
| 1873 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1874 | number_of_cpusets++; |
Tejun Heo | 033fa1c | 2012-11-19 08:13:39 -0800 | [diff] [blame] | 1875 | |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1876 | if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags)) |
| 1877 | return 0; |
Tejun Heo | 033fa1c | 2012-11-19 08:13:39 -0800 | [diff] [blame] | 1878 | |
| 1879 | /* |
| 1880 | * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is |
| 1881 | * set. This flag handling is implemented in cgroup core for |
| 1882 | * histrical reasons - the flag may be specified during mount. |
| 1883 | * |
| 1884 | * Currently, if any sibling cpusets have exclusive cpus or mem, we |
| 1885 | * refuse to clone the configuration - thereby refusing the task to |
| 1886 | * be entered, and as a result refusing the sys_unshare() or |
| 1887 | * clone() which initiated it. If this becomes a problem for some |
| 1888 | * users who wish to allow that scenario, then this could be |
| 1889 | * changed to grant parent->cpus_allowed-sibling_cpus_exclusive |
| 1890 | * (and likewise for mems) to the new cgroup. |
| 1891 | */ |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1892 | rcu_read_lock(); |
| 1893 | cpuset_for_each_child(tmp_cs, pos_cg, parent) { |
| 1894 | if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) { |
| 1895 | rcu_read_unlock(); |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1896 | return 0; |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1897 | } |
Tejun Heo | 033fa1c | 2012-11-19 08:13:39 -0800 | [diff] [blame] | 1898 | } |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1899 | rcu_read_unlock(); |
Tejun Heo | 033fa1c | 2012-11-19 08:13:39 -0800 | [diff] [blame] | 1900 | |
| 1901 | mutex_lock(&callback_mutex); |
| 1902 | cs->mems_allowed = parent->mems_allowed; |
| 1903 | cpumask_copy(cs->cpus_allowed, parent->cpus_allowed); |
| 1904 | mutex_unlock(&callback_mutex); |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1905 | |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | static void cpuset_css_offline(struct cgroup *cgrp) |
| 1910 | { |
| 1911 | struct cpuset *cs = cgroup_cs(cgrp); |
| 1912 | |
| 1913 | /* css_offline is called w/o cgroup_mutex, grab it */ |
| 1914 | cgroup_lock(); |
| 1915 | |
| 1916 | if (is_sched_load_balance(cs)) |
| 1917 | update_flag(CS_SCHED_LOAD_BALANCE, cs, 0); |
| 1918 | |
| 1919 | number_of_cpusets--; |
Tejun Heo | efeb77b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1920 | clear_bit(CS_ONLINE, &cs->flags); |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1921 | |
| 1922 | cgroup_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1925 | /* |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1926 | * If the cpuset being removed has its flag 'sched_load_balance' |
| 1927 | * enabled, then simulate turning sched_load_balance off, which |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 1928 | * will call async_rebuild_sched_domains(). |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1929 | */ |
| 1930 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1931 | static void cpuset_css_free(struct cgroup *cont) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1933 | struct cpuset *cs = cgroup_cs(cont); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 1935 | free_cpumask_var(cs->cpus_allowed); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1936 | kfree(cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1939 | struct cgroup_subsys cpuset_subsys = { |
| 1940 | .name = "cpuset", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1941 | .css_alloc = cpuset_css_alloc, |
Tejun Heo | c8f699b | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 1942 | .css_online = cpuset_css_online, |
| 1943 | .css_offline = cpuset_css_offline, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1944 | .css_free = cpuset_css_free, |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1945 | .can_attach = cpuset_can_attach, |
| 1946 | .attach = cpuset_attach, |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1947 | .subsys_id = cpuset_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1948 | .base_cftypes = files, |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1949 | .early_init = 1, |
| 1950 | }; |
| 1951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | /** |
| 1953 | * cpuset_init - initialize cpusets at system boot |
| 1954 | * |
| 1955 | * Description: Initialize top_cpuset and the cpuset internal file system, |
| 1956 | **/ |
| 1957 | |
| 1958 | int __init cpuset_init(void) |
| 1959 | { |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1960 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | |
Miao Xie | 58568d2 | 2009-06-16 15:31:49 -0700 | [diff] [blame] | 1962 | if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL)) |
| 1963 | BUG(); |
| 1964 | |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 1965 | cpumask_setall(top_cpuset.cpus_allowed); |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 1966 | nodes_setall(top_cpuset.mems_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 1968 | fmeter_init(&top_cpuset.fmeter); |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 1969 | set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags); |
Hidetoshi Seto | 1d3504f | 2008-04-15 14:04:23 +0900 | [diff] [blame] | 1970 | top_cpuset.relax_domain_level = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | err = register_filesystem(&cpuset_fs_type); |
| 1973 | if (err < 0) |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1974 | return err; |
| 1975 | |
Li Zefan | 2341d1b | 2009-01-07 18:08:42 -0800 | [diff] [blame] | 1976 | if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)) |
| 1977 | BUG(); |
| 1978 | |
Paul Jackson | 202f72d | 2006-01-08 01:01:57 -0800 | [diff] [blame] | 1979 | number_of_cpusets = 1; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 1980 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1983 | /** |
| 1984 | * cpuset_do_move_task - move a given task to another cpuset |
| 1985 | * @tsk: pointer to task_struct the task to move |
| 1986 | * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner |
| 1987 | * |
| 1988 | * Called by cgroup_scan_tasks() for each task in a cgroup. |
| 1989 | * Return nonzero to stop the walk through the tasks. |
| 1990 | */ |
Adrian Bunk | 9e0c914 | 2008-04-29 01:00:25 -0700 | [diff] [blame] | 1991 | static void cpuset_do_move_task(struct task_struct *tsk, |
| 1992 | struct cgroup_scanner *scan) |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1993 | { |
Li Zefan | 7f81b1a | 2009-04-02 16:57:53 -0700 | [diff] [blame] | 1994 | struct cgroup *new_cgroup = scan->data; |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1995 | |
Li Zefan | 7f81b1a | 2009-04-02 16:57:53 -0700 | [diff] [blame] | 1996 | cgroup_attach_task(new_cgroup, tsk); |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | /** |
| 2000 | * move_member_tasks_to_cpuset - move tasks from one cpuset to another |
| 2001 | * @from: cpuset in which the tasks currently reside |
| 2002 | * @to: cpuset to which the tasks will be moved |
| 2003 | * |
Paul Jackson | c8d9c90 | 2008-02-07 00:14:46 -0800 | [diff] [blame] | 2004 | * Called with cgroup_mutex held |
| 2005 | * callback_mutex must not be held, as cpuset_attach() will take it. |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2006 | * |
| 2007 | * The cgroup_scan_tasks() function will scan all the tasks in a cgroup, |
| 2008 | * calling callback functions for each. |
| 2009 | */ |
| 2010 | static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to) |
| 2011 | { |
Li Zefan | 7f81b1a | 2009-04-02 16:57:53 -0700 | [diff] [blame] | 2012 | struct cgroup_scanner scan; |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2013 | |
Li Zefan | 7f81b1a | 2009-04-02 16:57:53 -0700 | [diff] [blame] | 2014 | scan.cg = from->css.cgroup; |
| 2015 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 2016 | scan.process_task = cpuset_do_move_task; |
| 2017 | scan.heap = NULL; |
| 2018 | scan.data = to->css.cgroup; |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2019 | |
Li Zefan | 7f81b1a | 2009-04-02 16:57:53 -0700 | [diff] [blame] | 2020 | if (cgroup_scan_tasks(&scan)) |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2021 | printk(KERN_ERR "move_member_tasks_to_cpuset: " |
| 2022 | "cgroup_scan_tasks failed\n"); |
| 2023 | } |
| 2024 | |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2025 | /* |
Max Krasnyansky | cf41714 | 2008-08-11 14:33:53 -0700 | [diff] [blame] | 2026 | * If CPU and/or memory hotplug handlers, below, unplug any CPUs |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2027 | * or memory nodes, we need to walk over the cpuset hierarchy, |
| 2028 | * removing that CPU or node from all cpusets. If this removes the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2029 | * last CPU or node from a cpuset, then move the tasks in the empty |
| 2030 | * cpuset to its next-highest non-empty parent. |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2031 | * |
Paul Jackson | c8d9c90 | 2008-02-07 00:14:46 -0800 | [diff] [blame] | 2032 | * Called with cgroup_mutex held |
| 2033 | * callback_mutex must not be held, as cpuset_attach() will take it. |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2034 | */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2035 | static void remove_tasks_in_empty_cpuset(struct cpuset *cs) |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2036 | { |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2037 | struct cpuset *parent; |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2038 | |
Paul Jackson | c8d9c90 | 2008-02-07 00:14:46 -0800 | [diff] [blame] | 2039 | /* |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2040 | * Find its next-highest non-empty parent, (top cpuset |
| 2041 | * has online cpus, so can't be empty). |
| 2042 | */ |
| 2043 | parent = cs->parent; |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 2044 | while (cpumask_empty(parent->cpus_allowed) || |
Paul Jackson | b450129 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2045 | nodes_empty(parent->mems_allowed)) |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2046 | parent = parent->parent; |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2047 | |
| 2048 | move_member_tasks_to_cpuset(cs, parent); |
| 2049 | } |
| 2050 | |
| 2051 | /* |
Srivatsa S. Bhat | 80d1fa6 | 2012-05-24 19:46:41 +0530 | [diff] [blame] | 2052 | * Helper function to traverse cpusets. |
| 2053 | * It can be used to walk the cpuset tree from top to bottom, completing |
| 2054 | * one layer before dropping down to the next (thus always processing a |
| 2055 | * node before any of its children). |
| 2056 | */ |
| 2057 | static struct cpuset *cpuset_next(struct list_head *queue) |
| 2058 | { |
| 2059 | struct cpuset *cp; |
| 2060 | struct cpuset *child; /* scans child cpusets of cp */ |
| 2061 | struct cgroup *cont; |
| 2062 | |
| 2063 | if (list_empty(queue)) |
| 2064 | return NULL; |
| 2065 | |
| 2066 | cp = list_first_entry(queue, struct cpuset, stack_list); |
| 2067 | list_del(queue->next); |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2068 | rcu_read_lock(); |
| 2069 | cpuset_for_each_child(child, cont, cp) |
Srivatsa S. Bhat | 80d1fa6 | 2012-05-24 19:46:41 +0530 | [diff] [blame] | 2070 | list_add_tail(&child->stack_list, queue); |
Tejun Heo | ae8086c | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2071 | rcu_read_unlock(); |
Srivatsa S. Bhat | 80d1fa6 | 2012-05-24 19:46:41 +0530 | [diff] [blame] | 2072 | |
| 2073 | return cp; |
| 2074 | } |
| 2075 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2076 | /** |
| 2077 | * cpuset_propagate_hotplug - propagate CPU/memory hotplug to a cpuset |
| 2078 | * @cs: cpuset in interest |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2079 | * |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2080 | * Compare @cs's cpu and mem masks against top_cpuset and if some have gone |
| 2081 | * offline, update @cs accordingly. If @cs ends up with no CPU or memory, |
| 2082 | * all its tasks are moved to the nearest ancestor with both resources. |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2083 | * |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2084 | * Should be called with cgroup_mutex held. |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2085 | */ |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2086 | static void cpuset_propagate_hotplug(struct cpuset *cs) |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2087 | { |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2088 | static cpumask_t off_cpus; |
| 2089 | static nodemask_t off_mems, tmp_mems; |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2090 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2091 | WARN_ON_ONCE(!cgroup_lock_is_held()); |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2092 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2093 | cpumask_andnot(&off_cpus, cs->cpus_allowed, top_cpuset.cpus_allowed); |
| 2094 | nodes_andnot(off_mems, cs->mems_allowed, top_cpuset.mems_allowed); |
Paul Jackson | b450129 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2095 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2096 | /* remove offline cpus from @cs */ |
| 2097 | if (!cpumask_empty(&off_cpus)) { |
| 2098 | mutex_lock(&callback_mutex); |
| 2099 | cpumask_andnot(cs->cpus_allowed, cs->cpus_allowed, &off_cpus); |
| 2100 | mutex_unlock(&callback_mutex); |
| 2101 | update_tasks_cpumask(cs, NULL); |
| 2102 | } |
Paul Jackson | b450129 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2103 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2104 | /* remove offline mems from @cs */ |
| 2105 | if (!nodes_empty(off_mems)) { |
| 2106 | tmp_mems = cs->mems_allowed; |
| 2107 | mutex_lock(&callback_mutex); |
| 2108 | nodes_andnot(cs->mems_allowed, cs->mems_allowed, off_mems); |
| 2109 | mutex_unlock(&callback_mutex); |
| 2110 | update_tasks_nodemask(cs, &tmp_mems, NULL); |
| 2111 | } |
Miao Xie | f9b4fb8 | 2008-07-25 01:47:22 -0700 | [diff] [blame] | 2112 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2113 | if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed)) |
| 2114 | remove_tasks_in_empty_cpuset(cs); |
| 2115 | } |
Srivatsa S. Bhat | 7ddf96b | 2012-05-24 19:46:55 +0530 | [diff] [blame] | 2116 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2117 | /** |
Tejun Heo | 3a5a6d0 | 2013-01-07 08:51:07 -0800 | [diff] [blame^] | 2118 | * cpuset_hotplug_workfn - handle CPU/memory hotunplug for a cpuset |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2119 | * |
| 2120 | * This function is called after either CPU or memory configuration has |
| 2121 | * changed and updates cpuset accordingly. The top_cpuset is always |
| 2122 | * synchronized to cpu_active_mask and N_MEMORY, which is necessary in |
| 2123 | * order to make cpusets transparent (of no affect) on systems that are |
| 2124 | * actively using CPU hotplug but making no active use of cpusets. |
| 2125 | * |
| 2126 | * Non-root cpusets are only affected by offlining. If any CPUs or memory |
| 2127 | * nodes have been taken down, cpuset_propagate_hotplug() is invoked on all |
| 2128 | * descendants. |
| 2129 | * |
| 2130 | * Note that CPU offlining during suspend is ignored. We don't modify |
| 2131 | * cpusets across suspend/resume cycles at all. |
| 2132 | */ |
Tejun Heo | 3a5a6d0 | 2013-01-07 08:51:07 -0800 | [diff] [blame^] | 2133 | static void cpuset_hotplug_workfn(struct work_struct *work) |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2134 | { |
| 2135 | static cpumask_t new_cpus, tmp_cpus; |
| 2136 | static nodemask_t new_mems, tmp_mems; |
| 2137 | bool cpus_updated, mems_updated; |
| 2138 | bool cpus_offlined, mems_offlined; |
Srivatsa S. Bhat | 7ddf96b | 2012-05-24 19:46:55 +0530 | [diff] [blame] | 2139 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2140 | cgroup_lock(); |
Srivatsa S. Bhat | 7ddf96b | 2012-05-24 19:46:55 +0530 | [diff] [blame] | 2141 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2142 | /* fetch the available cpus/mems and find out which changed how */ |
| 2143 | cpumask_copy(&new_cpus, cpu_active_mask); |
| 2144 | new_mems = node_states[N_MEMORY]; |
Srivatsa S. Bhat | 7ddf96b | 2012-05-24 19:46:55 +0530 | [diff] [blame] | 2145 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2146 | cpus_updated = !cpumask_equal(top_cpuset.cpus_allowed, &new_cpus); |
| 2147 | cpus_offlined = cpumask_andnot(&tmp_cpus, top_cpuset.cpus_allowed, |
| 2148 | &new_cpus); |
Paul Jackson | b450129 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2149 | |
Tejun Heo | deb7aa3 | 2013-01-07 08:51:07 -0800 | [diff] [blame] | 2150 | mems_updated = !nodes_equal(top_cpuset.mems_allowed, new_mems); |
| 2151 | nodes_andnot(tmp_mems, top_cpuset.mems_allowed, new_mems); |
| 2152 | mems_offlined = !nodes_empty(tmp_mems); |
| 2153 | |
| 2154 | /* synchronize cpus_allowed to cpu_active_mask */ |
| 2155 | if (cpus_updated) { |
| 2156 | mutex_lock(&callback_mutex); |
| 2157 | cpumask_copy(top_cpuset.cpus_allowed, &new_cpus); |
| 2158 | mutex_unlock(&callback_mutex); |
| 2159 | /* we don't mess with cpumasks of tasks in top_cpuset */ |
| 2160 | } |
| 2161 | |
| 2162 | /* synchronize mems_allowed to N_MEMORY */ |
| 2163 | if (mems_updated) { |
| 2164 | tmp_mems = top_cpuset.mems_allowed; |
| 2165 | mutex_lock(&callback_mutex); |
| 2166 | top_cpuset.mems_allowed = new_mems; |
| 2167 | mutex_unlock(&callback_mutex); |
| 2168 | update_tasks_nodemask(&top_cpuset, &tmp_mems, NULL); |
| 2169 | } |
| 2170 | |
| 2171 | /* if cpus or mems went down, we need to propagate to descendants */ |
| 2172 | if (cpus_offlined || mems_offlined) { |
| 2173 | struct cpuset *cs; |
| 2174 | LIST_HEAD(queue); |
| 2175 | |
| 2176 | list_add_tail(&top_cpuset.stack_list, &queue); |
| 2177 | while ((cs = cpuset_next(&queue))) |
| 2178 | if (cs != &top_cpuset) |
| 2179 | cpuset_propagate_hotplug(cs); |
| 2180 | } |
| 2181 | |
| 2182 | cgroup_unlock(); |
| 2183 | |
| 2184 | /* rebuild sched domains if cpus_allowed has changed */ |
| 2185 | if (cpus_updated) { |
| 2186 | struct sched_domain_attr *attr; |
| 2187 | cpumask_var_t *doms; |
| 2188 | int ndoms; |
| 2189 | |
| 2190 | cgroup_lock(); |
| 2191 | ndoms = generate_sched_domains(&doms, &attr); |
| 2192 | cgroup_unlock(); |
| 2193 | |
| 2194 | partition_sched_domains(ndoms, doms, attr); |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 2195 | } |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Srivatsa S. Bhat | 7ddf96b | 2012-05-24 19:46:55 +0530 | [diff] [blame] | 2198 | void cpuset_update_active_cpus(bool cpu_online) |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 2199 | { |
Tejun Heo | 3a5a6d0 | 2013-01-07 08:51:07 -0800 | [diff] [blame^] | 2200 | /* |
| 2201 | * We're inside cpu hotplug critical region which usually nests |
| 2202 | * inside cgroup synchronization. Bounce actual hotplug processing |
| 2203 | * to a work item to avoid reverse locking order. |
| 2204 | * |
| 2205 | * We still need to do partition_sched_domains() synchronously; |
| 2206 | * otherwise, the scheduler will get confused and put tasks to the |
| 2207 | * dead CPU. Fall back to the default single domain. |
| 2208 | * cpuset_hotplug_workfn() will rebuild it as necessary. |
| 2209 | */ |
| 2210 | partition_sched_domains(1, NULL, NULL); |
| 2211 | schedule_work(&cpuset_hotplug_work); |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 2212 | } |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 2213 | |
Paul Jackson | b1aac8b | 2006-09-29 02:01:17 -0700 | [diff] [blame] | 2214 | #ifdef CONFIG_MEMORY_HOTPLUG |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 2215 | /* |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 2216 | * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY]. |
| 2217 | * Call this routine anytime after node_states[N_MEMORY] changes. |
Srivatsa S. Bhat | a1cd2b1 | 2012-05-24 19:47:03 +0530 | [diff] [blame] | 2218 | * See cpuset_update_active_cpus() for CPU hotplug handling. |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 2219 | */ |
Miao Xie | f481891 | 2008-11-19 15:36:30 -0800 | [diff] [blame] | 2220 | static int cpuset_track_online_nodes(struct notifier_block *self, |
| 2221 | unsigned long action, void *arg) |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 2222 | { |
Tejun Heo | 3a5a6d0 | 2013-01-07 08:51:07 -0800 | [diff] [blame^] | 2223 | schedule_work(&cpuset_hotplug_work); |
Miao Xie | f481891 | 2008-11-19 15:36:30 -0800 | [diff] [blame] | 2224 | return NOTIFY_OK; |
Paul Jackson | 38837fc | 2006-09-29 02:01:16 -0700 | [diff] [blame] | 2225 | } |
| 2226 | #endif |
| 2227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | /** |
| 2229 | * cpuset_init_smp - initialize cpus_allowed |
| 2230 | * |
| 2231 | * Description: Finish top cpuset after cpu, node maps are initialized |
| 2232 | **/ |
| 2233 | |
| 2234 | void __init cpuset_init_smp(void) |
| 2235 | { |
Peter Zijlstra | 6ad4c18 | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 2236 | cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask); |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 2237 | top_cpuset.mems_allowed = node_states[N_MEMORY]; |
Paul Jackson | 4c4d50f | 2006-08-27 01:23:51 -0700 | [diff] [blame] | 2238 | |
Miao Xie | f481891 | 2008-11-19 15:36:30 -0800 | [diff] [blame] | 2239 | hotplug_memory_notifier(cpuset_track_online_nodes, 10); |
Miao Xie | f90d411 | 2009-01-16 10:24:10 +0800 | [diff] [blame] | 2240 | |
| 2241 | cpuset_wq = create_singlethread_workqueue("cpuset"); |
| 2242 | BUG_ON(!cpuset_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset. |
| 2247 | * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed. |
Li Zefan | 6af866a | 2009-01-07 18:08:45 -0800 | [diff] [blame] | 2248 | * @pmask: pointer to struct cpumask variable to receive cpus_allowed set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | * |
Li Zefan | 300ed6c | 2009-01-07 18:08:44 -0800 | [diff] [blame] | 2250 | * Description: Returns the cpumask_var_t cpus_allowed of the cpuset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | * attached to the specified @tsk. Guaranteed to return some non-empty |
Rusty Russell | 5f054e3 | 2012-03-29 15:38:31 +1030 | [diff] [blame] | 2252 | * subset of cpu_online_mask, even if this means going outside the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | * tasks cpuset. |
| 2254 | **/ |
| 2255 | |
Li Zefan | 6af866a | 2009-01-07 18:08:45 -0800 | [diff] [blame] | 2256 | void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | { |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2258 | mutex_lock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2259 | task_lock(tsk); |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 2260 | guarantee_online_cpus(task_cs(tsk), pmask); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2261 | task_unlock(tsk); |
Oleg Nesterov | 897f0b3 | 2010-03-15 10:10:03 +0100 | [diff] [blame] | 2262 | mutex_unlock(&callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 2265 | void cpuset_cpus_allowed_fallback(struct task_struct *tsk) |
Oleg Nesterov | 9084bb8 | 2010-03-15 10:10:27 +0100 | [diff] [blame] | 2266 | { |
| 2267 | const struct cpuset *cs; |
Oleg Nesterov | 9084bb8 | 2010-03-15 10:10:27 +0100 | [diff] [blame] | 2268 | |
| 2269 | rcu_read_lock(); |
| 2270 | cs = task_cs(tsk); |
| 2271 | if (cs) |
KOSAKI Motohiro | 1e1b6c5 | 2011-05-19 15:08:58 +0900 | [diff] [blame] | 2272 | do_set_cpus_allowed(tsk, cs->cpus_allowed); |
Oleg Nesterov | 9084bb8 | 2010-03-15 10:10:27 +0100 | [diff] [blame] | 2273 | rcu_read_unlock(); |
| 2274 | |
| 2275 | /* |
| 2276 | * We own tsk->cpus_allowed, nobody can change it under us. |
| 2277 | * |
| 2278 | * But we used cs && cs->cpus_allowed lockless and thus can |
| 2279 | * race with cgroup_attach_task() or update_cpumask() and get |
| 2280 | * the wrong tsk->cpus_allowed. However, both cases imply the |
| 2281 | * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr() |
| 2282 | * which takes task_rq_lock(). |
| 2283 | * |
| 2284 | * If we are called after it dropped the lock we must see all |
| 2285 | * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary |
| 2286 | * set any mask even if it is not right from task_cs() pov, |
| 2287 | * the pending set_cpus_allowed_ptr() will fix things. |
Peter Zijlstra | 2baab4e | 2012-03-20 15:57:01 +0100 | [diff] [blame] | 2288 | * |
| 2289 | * select_fallback_rq() will fix things ups and set cpu_possible_mask |
| 2290 | * if required. |
Oleg Nesterov | 9084bb8 | 2010-03-15 10:10:27 +0100 | [diff] [blame] | 2291 | */ |
Oleg Nesterov | 9084bb8 | 2010-03-15 10:10:27 +0100 | [diff] [blame] | 2292 | } |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | void cpuset_init_current_mems_allowed(void) |
| 2295 | { |
Mike Travis | f9a86fc | 2008-04-04 18:11:07 -0700 | [diff] [blame] | 2296 | nodes_setall(current->mems_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 2299 | /** |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2300 | * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset. |
| 2301 | * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed. |
| 2302 | * |
| 2303 | * Description: Returns the nodemask_t mems_allowed of the cpuset |
| 2304 | * attached to the specified @tsk. Guaranteed to return some non-empty |
Lai Jiangshan | 38d7bee | 2012-12-12 13:51:24 -0800 | [diff] [blame] | 2305 | * subset of node_states[N_MEMORY], even if this means going outside the |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2306 | * tasks cpuset. |
| 2307 | **/ |
| 2308 | |
| 2309 | nodemask_t cpuset_mems_allowed(struct task_struct *tsk) |
| 2310 | { |
| 2311 | nodemask_t mask; |
| 2312 | |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2313 | mutex_lock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2314 | task_lock(tsk); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2315 | guarantee_online_mems(task_cs(tsk), &mask); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2316 | task_unlock(tsk); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2317 | mutex_unlock(&callback_mutex); |
Paul Jackson | 909d75a | 2006-01-08 01:01:55 -0800 | [diff] [blame] | 2318 | |
| 2319 | return mask; |
| 2320 | } |
| 2321 | |
| 2322 | /** |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 2323 | * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed |
| 2324 | * @nodemask: the nodemask to be checked |
Randy Dunlap | d9fd8a6 | 2005-07-27 11:45:11 -0700 | [diff] [blame] | 2325 | * |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 2326 | * Are any of the nodes in the nodemask allowed in current->mems_allowed? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | */ |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 2328 | int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | { |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 2330 | return nodes_intersects(*nodemask, current->mems_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | } |
| 2332 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2333 | /* |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 2334 | * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or |
| 2335 | * mem_hardwall ancestor to the specified cpuset. Call holding |
| 2336 | * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall |
| 2337 | * (an unusual configuration), then returns the root cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | */ |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 2339 | static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | { |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 2341 | while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2342 | cs = cs->parent; |
| 2343 | return cs; |
| 2344 | } |
| 2345 | |
| 2346 | /** |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2347 | * cpuset_node_allowed_softwall - Can we allocate on a memory node? |
| 2348 | * @node: is this an allowed node? |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2349 | * @gfp_mask: memory allocation flags |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2350 | * |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2351 | * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is |
| 2352 | * set, yes, we can always allocate. If node is in our task's mems_allowed, |
| 2353 | * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest |
| 2354 | * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been |
| 2355 | * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE |
| 2356 | * flag, yes. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2357 | * Otherwise, no. |
| 2358 | * |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2359 | * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to |
| 2360 | * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall() |
| 2361 | * might sleep, and might allow a node from an enclosing cpuset. |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2362 | * |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2363 | * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall |
| 2364 | * cpusets, and never sleeps. |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2365 | * |
| 2366 | * The __GFP_THISNODE placement logic is really handled elsewhere, |
| 2367 | * by forcibly using a zonelist starting at a specified node, and by |
| 2368 | * (in get_page_from_freelist()) refusing to consider the zones for |
| 2369 | * any node on the zonelist except the first. By the time any such |
| 2370 | * calls get to this routine, we should just shut up and say 'yes'. |
| 2371 | * |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2372 | * GFP_USER allocations are marked with the __GFP_HARDWALL bit, |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 2373 | * and do not allow allocations outside the current tasks cpuset |
| 2374 | * unless the task has been OOM killed as is marked TIF_MEMDIE. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2375 | * GFP_KERNEL allocations are not so marked, so can escape to the |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 2376 | * nearest enclosing hardwalled ancestor cpuset. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2377 | * |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2378 | * Scanning up parent cpusets requires callback_mutex. The |
| 2379 | * __alloc_pages() routine only calls here with __GFP_HARDWALL bit |
| 2380 | * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the |
| 2381 | * current tasks mems_allowed came up empty on the first pass over |
| 2382 | * the zonelist. So only GFP_KERNEL allocations, if all nodes in the |
| 2383 | * cpuset are short of memory, might require taking the callback_mutex |
| 2384 | * mutex. |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2385 | * |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2386 | * The first call here from mm/page_alloc:get_page_from_freelist() |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2387 | * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, |
| 2388 | * so no allocation on a node outside the cpuset is allowed (unless |
| 2389 | * in interrupt, of course). |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2390 | * |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2391 | * The second pass through get_page_from_freelist() doesn't even call |
| 2392 | * here for GFP_ATOMIC calls. For those calls, the __alloc_pages() |
| 2393 | * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set |
| 2394 | * in alloc_flags. That logic and the checks below have the combined |
| 2395 | * affect that: |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2396 | * in_interrupt - any node ok (current task context irrelevant) |
| 2397 | * GFP_ATOMIC - any node ok |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 2398 | * TIF_MEMDIE - any node ok |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 2399 | * GFP_KERNEL - any node in enclosing hardwalled cpuset ok |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2400 | * GFP_USER - only nodes in current tasks mems allowed ok. |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2401 | * |
| 2402 | * Rule: |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2403 | * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you |
Paul Jackson | 36be57f | 2006-05-20 15:00:10 -0700 | [diff] [blame] | 2404 | * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables |
| 2405 | * the code that might scan up ancestor cpusets and sleep. |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2406 | */ |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2407 | int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2408 | { |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2409 | const struct cpuset *cs; /* current cpuset ancestors */ |
Paul Jackson | 29afd49 | 2006-03-24 03:16:12 -0800 | [diff] [blame] | 2410 | int allowed; /* is allocation in zone z allowed? */ |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2411 | |
Christoph Lameter | 9b819d2 | 2006-09-25 23:31:40 -0700 | [diff] [blame] | 2412 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2413 | return 1; |
Paul Jackson | 92d1dbd | 2006-05-20 15:00:11 -0700 | [diff] [blame] | 2414 | might_sleep_if(!(gfp_mask & __GFP_HARDWALL)); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2415 | if (node_isset(node, current->mems_allowed)) |
| 2416 | return 1; |
David Rientjes | c596d9f | 2007-05-06 14:49:32 -0700 | [diff] [blame] | 2417 | /* |
| 2418 | * Allow tasks that have access to memory reserves because they have |
| 2419 | * been OOM killed to get memory anywhere. |
| 2420 | */ |
| 2421 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 2422 | return 1; |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2423 | if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */ |
| 2424 | return 0; |
| 2425 | |
Bob Picco | 5563e77 | 2005-11-13 16:06:35 -0800 | [diff] [blame] | 2426 | if (current->flags & PF_EXITING) /* Let dying task have memory */ |
| 2427 | return 1; |
| 2428 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2429 | /* Not hardwall and node outside mems_allowed: scan up cpusets */ |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2430 | mutex_lock(&callback_mutex); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2431 | |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2432 | task_lock(current); |
Paul Menage | 7860836 | 2008-04-29 01:00:26 -0700 | [diff] [blame] | 2433 | cs = nearest_hardwall_ancestor(task_cs(current)); |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2434 | task_unlock(current); |
| 2435 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2436 | allowed = node_isset(node, cs->mems_allowed); |
Ingo Molnar | 3d3f26a | 2006-03-23 03:00:18 -0800 | [diff] [blame] | 2437 | mutex_unlock(&callback_mutex); |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 2438 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | } |
| 2440 | |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2441 | /* |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2442 | * cpuset_node_allowed_hardwall - Can we allocate on a memory node? |
| 2443 | * @node: is this an allowed node? |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2444 | * @gfp_mask: memory allocation flags |
| 2445 | * |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2446 | * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is |
| 2447 | * set, yes, we can always allocate. If node is in our task's mems_allowed, |
| 2448 | * yes. If the task has been OOM killed and has access to memory reserves as |
| 2449 | * specified by the TIF_MEMDIE flag, yes. |
| 2450 | * Otherwise, no. |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2451 | * |
| 2452 | * The __GFP_THISNODE placement logic is really handled elsewhere, |
| 2453 | * by forcibly using a zonelist starting at a specified node, and by |
| 2454 | * (in get_page_from_freelist()) refusing to consider the zones for |
| 2455 | * any node on the zonelist except the first. By the time any such |
| 2456 | * calls get to this routine, we should just shut up and say 'yes'. |
| 2457 | * |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2458 | * Unlike the cpuset_node_allowed_softwall() variant, above, |
| 2459 | * this variant requires that the node be in the current task's |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2460 | * mems_allowed or that we're in interrupt. It does not scan up the |
| 2461 | * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset. |
| 2462 | * It never sleeps. |
| 2463 | */ |
David Rientjes | a1bc5a4 | 2009-04-02 16:57:54 -0700 | [diff] [blame] | 2464 | int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask) |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2465 | { |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2466 | if (in_interrupt() || (gfp_mask & __GFP_THISNODE)) |
| 2467 | return 1; |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2468 | if (node_isset(node, current->mems_allowed)) |
| 2469 | return 1; |
Daniel Walker | dedf8b7 | 2007-10-18 03:06:04 -0700 | [diff] [blame] | 2470 | /* |
| 2471 | * Allow tasks that have access to memory reserves because they have |
| 2472 | * been OOM killed to get memory anywhere. |
| 2473 | */ |
| 2474 | if (unlikely(test_thread_flag(TIF_MEMDIE))) |
| 2475 | return 1; |
Paul Jackson | 02a0e53 | 2006-12-13 00:34:25 -0800 | [diff] [blame] | 2476 | return 0; |
| 2477 | } |
| 2478 | |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2479 | /** |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2480 | * cpuset_mem_spread_node() - On which node to begin search for a file page |
| 2481 | * cpuset_slab_spread_node() - On which node to begin search for a slab page |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 2482 | * |
| 2483 | * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for |
| 2484 | * tasks in a cpuset with is_spread_page or is_spread_slab set), |
| 2485 | * and if the memory allocation used cpuset_mem_spread_node() |
| 2486 | * to determine on which node to start looking, as it will for |
| 2487 | * certain page cache or slab cache pages such as used for file |
| 2488 | * system buffers and inode caches, then instead of starting on the |
| 2489 | * local node to look for a free page, rather spread the starting |
| 2490 | * node around the tasks mems_allowed nodes. |
| 2491 | * |
| 2492 | * We don't have to worry about the returned node being offline |
| 2493 | * because "it can't happen", and even if it did, it would be ok. |
| 2494 | * |
| 2495 | * The routines calling guarantee_online_mems() are careful to |
| 2496 | * only set nodes in task->mems_allowed that are online. So it |
| 2497 | * should not be possible for the following code to return an |
| 2498 | * offline node. But if it did, that would be ok, as this routine |
| 2499 | * is not returning the node where the allocation must be, only |
| 2500 | * the node where the search should start. The zonelist passed to |
| 2501 | * __alloc_pages() will include all nodes. If the slab allocator |
| 2502 | * is passed an offline node, it will fall back to the local node. |
| 2503 | * See kmem_cache_alloc_node(). |
| 2504 | */ |
| 2505 | |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2506 | static int cpuset_spread_node(int *rotor) |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 2507 | { |
| 2508 | int node; |
| 2509 | |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2510 | node = next_node(*rotor, current->mems_allowed); |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 2511 | if (node == MAX_NUMNODES) |
| 2512 | node = first_node(current->mems_allowed); |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2513 | *rotor = node; |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 2514 | return node; |
| 2515 | } |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2516 | |
| 2517 | int cpuset_mem_spread_node(void) |
| 2518 | { |
Michal Hocko | 778d3b0 | 2011-07-26 16:08:30 -0700 | [diff] [blame] | 2519 | if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE) |
| 2520 | current->cpuset_mem_spread_rotor = |
| 2521 | node_random(¤t->mems_allowed); |
| 2522 | |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2523 | return cpuset_spread_node(¤t->cpuset_mem_spread_rotor); |
| 2524 | } |
| 2525 | |
| 2526 | int cpuset_slab_spread_node(void) |
| 2527 | { |
Michal Hocko | 778d3b0 | 2011-07-26 16:08:30 -0700 | [diff] [blame] | 2528 | if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE) |
| 2529 | current->cpuset_slab_spread_rotor = |
| 2530 | node_random(¤t->mems_allowed); |
| 2531 | |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 2532 | return cpuset_spread_node(¤t->cpuset_slab_spread_rotor); |
| 2533 | } |
| 2534 | |
Paul Jackson | 825a46a | 2006-03-24 03:16:03 -0800 | [diff] [blame] | 2535 | EXPORT_SYMBOL_GPL(cpuset_mem_spread_node); |
| 2536 | |
| 2537 | /** |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2538 | * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's? |
| 2539 | * @tsk1: pointer to task_struct of some task. |
| 2540 | * @tsk2: pointer to task_struct of some other task. |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2541 | * |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2542 | * Description: Return true if @tsk1's mems_allowed intersects the |
| 2543 | * mems_allowed of @tsk2. Used by the OOM killer to determine if |
| 2544 | * one of the task's memory usage might impact the memory available |
| 2545 | * to the other. |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2546 | **/ |
| 2547 | |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2548 | int cpuset_mems_allowed_intersects(const struct task_struct *tsk1, |
| 2549 | const struct task_struct *tsk2) |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2550 | { |
David Rientjes | bbe373f | 2007-10-16 23:25:58 -0700 | [diff] [blame] | 2551 | return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed); |
Paul Jackson | ef08e3b | 2005-09-06 15:18:13 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
David Rientjes | 75aa199 | 2009-01-06 14:39:01 -0800 | [diff] [blame] | 2554 | /** |
| 2555 | * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed |
| 2556 | * @task: pointer to task_struct of some task. |
| 2557 | * |
| 2558 | * Description: Prints @task's name, cpuset name, and cached copy of its |
| 2559 | * mems_allowed to the kernel log. Must hold task_lock(task) to allow |
| 2560 | * dereferencing task_cs(task). |
| 2561 | */ |
| 2562 | void cpuset_print_task_mems_allowed(struct task_struct *tsk) |
| 2563 | { |
| 2564 | struct dentry *dentry; |
| 2565 | |
| 2566 | dentry = task_cs(tsk)->css.cgroup->dentry; |
| 2567 | spin_lock(&cpuset_buffer_lock); |
| 2568 | snprintf(cpuset_name, CPUSET_NAME_LEN, |
| 2569 | dentry ? (const char *)dentry->d_name.name : "/"); |
| 2570 | nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN, |
| 2571 | tsk->mems_allowed); |
| 2572 | printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n", |
| 2573 | tsk->comm, cpuset_name, cpuset_nodelist); |
| 2574 | spin_unlock(&cpuset_buffer_lock); |
| 2575 | } |
| 2576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | /* |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2578 | * Collection of memory_pressure is suppressed unless |
| 2579 | * this flag is enabled by writing "1" to the special |
| 2580 | * cpuset file 'memory_pressure_enabled' in the root cpuset. |
| 2581 | */ |
| 2582 | |
Paul Jackson | c5b2aff | 2006-01-08 01:01:51 -0800 | [diff] [blame] | 2583 | int cpuset_memory_pressure_enabled __read_mostly; |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2584 | |
| 2585 | /** |
| 2586 | * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims. |
| 2587 | * |
| 2588 | * Keep a running average of the rate of synchronous (direct) |
| 2589 | * page reclaim efforts initiated by tasks in each cpuset. |
| 2590 | * |
| 2591 | * This represents the rate at which some task in the cpuset |
| 2592 | * ran low on memory on all nodes it was allowed to use, and |
| 2593 | * had to enter the kernels page reclaim code in an effort to |
| 2594 | * create more free memory by tossing clean pages or swapping |
| 2595 | * or writing dirty pages. |
| 2596 | * |
| 2597 | * Display to user space in the per-cpuset read-only file |
| 2598 | * "memory_pressure". Value displayed is an integer |
| 2599 | * representing the recent rate of entry into the synchronous |
| 2600 | * (direct) page reclaim by any task attached to the cpuset. |
| 2601 | **/ |
| 2602 | |
| 2603 | void __cpuset_memory_pressure_bump(void) |
| 2604 | { |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2605 | task_lock(current); |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2606 | fmeter_markevent(&task_cs(current)->fmeter); |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2607 | task_unlock(current); |
| 2608 | } |
| 2609 | |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2610 | #ifdef CONFIG_PROC_PID_CPUSET |
Paul Jackson | 3e0d98b | 2006-01-08 01:01:49 -0800 | [diff] [blame] | 2611 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | * proc_cpuset_show() |
| 2613 | * - Print tasks cpuset path into seq_file. |
| 2614 | * - Used for /proc/<pid>/cpuset. |
Paul Jackson | 053199e | 2005-10-30 15:02:30 -0800 | [diff] [blame] | 2615 | * - No need to task_lock(tsk) on this tsk->cpuset reference, as it |
| 2616 | * doesn't really matter if tsk->cpuset changes after we read it, |
Paul Jackson | c8d9c90 | 2008-02-07 00:14:46 -0800 | [diff] [blame] | 2617 | * and we take cgroup_mutex, keeping cpuset_attach() from changing it |
Paul Menage | 2df167a | 2008-02-07 00:14:45 -0800 | [diff] [blame] | 2618 | * anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | */ |
Paul Jackson | 029190c | 2007-10-18 23:40:20 -0700 | [diff] [blame] | 2620 | static int proc_cpuset_show(struct seq_file *m, void *unused_v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | { |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 2622 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | struct task_struct *tsk; |
| 2624 | char *buf; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2625 | struct cgroup_subsys_state *css; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2626 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2628 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2630 | if (!buf) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2631 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2633 | retval = -ESRCH; |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 2634 | pid = m->private; |
| 2635 | tsk = get_pid_task(pid, PIDTYPE_PID); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2636 | if (!tsk) |
| 2637 | goto out_free; |
| 2638 | |
| 2639 | retval = -EINVAL; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2640 | cgroup_lock(); |
| 2641 | css = task_subsys_state(tsk, cpuset_subsys_id); |
| 2642 | retval = cgroup_path(css->cgroup, buf, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | if (retval < 0) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2644 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | seq_puts(m, buf); |
| 2646 | seq_putc(m, '\n'); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2647 | out_unlock: |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2648 | cgroup_unlock(); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2649 | put_task_struct(tsk); |
| 2650 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | kfree(buf); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2652 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | return retval; |
| 2654 | } |
| 2655 | |
| 2656 | static int cpuset_open(struct inode *inode, struct file *file) |
| 2657 | { |
Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 2658 | struct pid *pid = PROC_I(inode)->pid; |
| 2659 | return single_open(file, proc_cpuset_show, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | } |
| 2661 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2662 | const struct file_operations proc_cpuset_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | .open = cpuset_open, |
| 2664 | .read = seq_read, |
| 2665 | .llseek = seq_lseek, |
| 2666 | .release = single_release, |
| 2667 | }; |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2668 | #endif /* CONFIG_PROC_PID_CPUSET */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | |
Heiko Carstens | d01d482 | 2009-09-21 11:06:27 +0200 | [diff] [blame] | 2670 | /* Display task mems_allowed in /proc/<pid>/status file. */ |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 2671 | void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | { |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 2673 | seq_printf(m, "Mems_allowed:\t"); |
Lai Jiangshan | 30e8e13 | 2008-10-18 20:28:20 -0700 | [diff] [blame] | 2674 | seq_nodemask(m, &task->mems_allowed); |
Eric W. Biederman | df5f831 | 2008-02-08 04:18:33 -0800 | [diff] [blame] | 2675 | seq_printf(m, "\n"); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 2676 | seq_printf(m, "Mems_allowed_list:\t"); |
Lai Jiangshan | 30e8e13 | 2008-10-18 20:28:20 -0700 | [diff] [blame] | 2677 | seq_nodemask_list(m, &task->mems_allowed); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 2678 | seq_printf(m, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | } |