Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simple NUMA memory policy for the Linux kernel. |
| 3 | * |
| 4 | * Copyright 2003,2004 Andi Kleen, SuSE Labs. |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 5 | * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Subject to the GNU Public License, version 2. |
| 7 | * |
| 8 | * NUMA policy allows the user to give hints in which node(s) memory should |
| 9 | * be allocated. |
| 10 | * |
| 11 | * Support four policies per VMA and per process: |
| 12 | * |
| 13 | * The VMA policy has priority over the process policy for a page fault. |
| 14 | * |
| 15 | * interleave Allocate memory interleaved over a set of nodes, |
| 16 | * with normal fallback if it fails. |
| 17 | * For VMA based allocations this interleaves based on the |
| 18 | * offset into the backing object or offset into the mapping |
| 19 | * for anonymous memory. For process policy an process counter |
| 20 | * is used. |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * bind Only allocate memory on a specific set of nodes, |
| 23 | * no fallback. |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 24 | * FIXME: memory is allocated starting with the first node |
| 25 | * to the last. It would be better if bind would truly restrict |
| 26 | * the allocation to memory nodes instead |
| 27 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * preferred Try a specific node first before normal fallback. |
| 29 | * As a special case node -1 here means do the allocation |
| 30 | * on the local CPU. This is normally identical to default, |
| 31 | * but useful to set in a VMA when you have a non default |
| 32 | * process policy. |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 33 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * default Allocate on the local node first, or when on a VMA |
| 35 | * use the process policy. This is what Linux always did |
| 36 | * in a NUMA aware kernel and still does by, ahem, default. |
| 37 | * |
| 38 | * The process policy is applied for most non interrupt memory allocations |
| 39 | * in that process' context. Interrupts ignore the policies and always |
| 40 | * try to allocate on the local CPU. The VMA policy is only applied for memory |
| 41 | * allocations for a VMA in the VM. |
| 42 | * |
| 43 | * Currently there are a few corner cases in swapping where the policy |
| 44 | * is not applied, but the majority should be handled. When process policy |
| 45 | * is used it is not remembered over swap outs/swap ins. |
| 46 | * |
| 47 | * Only the highest zone in the zone hierarchy gets policied. Allocations |
| 48 | * requesting a lower zone just use default policy. This implies that |
| 49 | * on systems with highmem kernel lowmem allocation don't get policied. |
| 50 | * Same with GFP_DMA allocations. |
| 51 | * |
| 52 | * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between |
| 53 | * all users and remembered even when nobody has memory mapped. |
| 54 | */ |
| 55 | |
| 56 | /* Notebook: |
| 57 | fix mmap readahead to honour policy and enable policy for any page cache |
| 58 | object |
| 59 | statistics for bigpages |
| 60 | global policy for page cache? currently it uses process policy. Requires |
| 61 | first item above. |
| 62 | handle mremap for shared memory (currently ignored for the policy) |
| 63 | grows down? |
| 64 | make bind policy root only? It can trigger oom much faster and the |
| 65 | kernel is not always grateful with that. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
| 67 | |
| 68 | #include <linux/mempolicy.h> |
| 69 | #include <linux/mm.h> |
| 70 | #include <linux/highmem.h> |
| 71 | #include <linux/hugetlb.h> |
| 72 | #include <linux/kernel.h> |
| 73 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include <linux/nodemask.h> |
| 75 | #include <linux/cpuset.h> |
| 76 | #include <linux/gfp.h> |
| 77 | #include <linux/slab.h> |
| 78 | #include <linux/string.h> |
| 79 | #include <linux/module.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 80 | #include <linux/nsproxy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/interrupt.h> |
| 82 | #include <linux/init.h> |
| 83 | #include <linux/compat.h> |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 84 | #include <linux/swap.h> |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 85 | #include <linux/seq_file.h> |
| 86 | #include <linux/proc_fs.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 87 | #include <linux/migrate.h> |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 88 | #include <linux/rmap.h> |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 89 | #include <linux/security.h> |
Adrian Bunk | dbcb0f1 | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 90 | #include <linux/syscalls.h> |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #include <asm/tlbflush.h> |
| 93 | #include <asm/uaccess.h> |
| 94 | |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 95 | /* Internal flags */ |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 96 | #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */ |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 97 | #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */ |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 98 | #define MPOL_MF_STATS (MPOL_MF_INTERNAL << 2) /* Gather statistics */ |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 99 | |
Pekka Enberg | fcc234f | 2006-03-22 00:08:13 -0800 | [diff] [blame] | 100 | static struct kmem_cache *policy_cache; |
| 101 | static struct kmem_cache *sn_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* Highest zone. An specific allocation for a zone below that is not |
| 104 | policied. */ |
Christoph Lameter | 6267276 | 2007-02-10 01:43:07 -0800 | [diff] [blame] | 105 | enum zone_type policy_zone = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Andi Kleen | d42c699 | 2005-07-06 19:56:03 +0200 | [diff] [blame] | 107 | struct mempolicy default_policy = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .refcnt = ATOMIC_INIT(1), /* never free it */ |
| 109 | .policy = MPOL_DEFAULT, |
| 110 | }; |
| 111 | |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 112 | static const struct mempolicy_operations { |
| 113 | int (*create)(struct mempolicy *pol, const nodemask_t *nodes); |
| 114 | void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes); |
| 115 | } mpol_ops[MPOL_MAX]; |
| 116 | |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 117 | /* Check that the nodemask contains at least one populated zone */ |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 118 | static int is_valid_nodemask(const nodemask_t *nodemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 120 | int nd, k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 122 | /* Check that there is something useful in this mask */ |
| 123 | k = policy_zone; |
| 124 | |
| 125 | for_each_node_mask(nd, *nodemask) { |
| 126 | struct zone *z; |
| 127 | |
| 128 | for (k = 0; k <= policy_zone; k++) { |
| 129 | z = &NODE_DATA(nd)->node_zones[k]; |
| 130 | if (z->present_pages > 0) |
| 131 | return 1; |
Andi Kleen | dd942ae | 2006-02-17 01:39:16 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 134 | |
| 135 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 138 | static inline int mpol_store_user_nodemask(const struct mempolicy *pol) |
| 139 | { |
David Rientjes | 4c50bc0 | 2008-04-28 02:12:30 -0700 | [diff] [blame] | 140 | return pol->flags & (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES); |
| 141 | } |
| 142 | |
| 143 | static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig, |
| 144 | const nodemask_t *rel) |
| 145 | { |
| 146 | nodemask_t tmp; |
| 147 | nodes_fold(tmp, *orig, nodes_weight(*rel)); |
| 148 | nodes_onto(*ret, tmp, *rel); |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 149 | } |
| 150 | |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 151 | static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes) |
| 152 | { |
| 153 | if (nodes_empty(*nodes)) |
| 154 | return -EINVAL; |
| 155 | pol->v.nodes = *nodes; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes) |
| 160 | { |
| 161 | if (!nodes) |
| 162 | pol->v.preferred_node = -1; /* local allocation */ |
| 163 | else if (nodes_empty(*nodes)) |
| 164 | return -EINVAL; /* no allowed nodes */ |
| 165 | else |
| 166 | pol->v.preferred_node = first_node(*nodes); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes) |
| 171 | { |
| 172 | if (!is_valid_nodemask(nodes)) |
| 173 | return -EINVAL; |
| 174 | pol->v.nodes = *nodes; |
| 175 | return 0; |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | /* Create a new policy */ |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 179 | static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, |
| 180 | nodemask_t *nodes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | struct mempolicy *policy; |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 183 | nodemask_t cpuset_context_nmask; |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 184 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 186 | pr_debug("setting mode %d flags %d nodes[0] %lx\n", |
| 187 | mode, flags, nodes ? nodes_addr(*nodes)[0] : -1); |
Paul Mundt | 140d5a4 | 2007-07-15 23:38:16 -0700 | [diff] [blame] | 188 | |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 189 | if (mode == MPOL_DEFAULT) { |
| 190 | if (nodes && !nodes_empty(*nodes)) |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 191 | return ERR_PTR(-EINVAL); |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 192 | return NULL; |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 193 | } |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 194 | VM_BUG_ON(!nodes); |
| 195 | |
| 196 | /* |
| 197 | * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or |
| 198 | * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation). |
| 199 | * All other modes require a valid pointer to a non-empty nodemask. |
| 200 | */ |
| 201 | if (mode == MPOL_PREFERRED) { |
| 202 | if (nodes_empty(*nodes)) { |
| 203 | if (((flags & MPOL_F_STATIC_NODES) || |
| 204 | (flags & MPOL_F_RELATIVE_NODES))) |
| 205 | return ERR_PTR(-EINVAL); |
| 206 | nodes = NULL; /* flag local alloc */ |
| 207 | } |
| 208 | } else if (nodes_empty(*nodes)) |
| 209 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | policy = kmem_cache_alloc(policy_cache, GFP_KERNEL); |
| 211 | if (!policy) |
| 212 | return ERR_PTR(-ENOMEM); |
| 213 | atomic_set(&policy->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | policy->policy = mode; |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 215 | policy->flags = flags; |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 216 | |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 217 | if (nodes) { |
| 218 | /* |
| 219 | * cpuset related setup doesn't apply to local allocation |
| 220 | */ |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 221 | cpuset_update_task_memory_state(); |
| 222 | if (flags & MPOL_F_RELATIVE_NODES) |
| 223 | mpol_relative_nodemask(&cpuset_context_nmask, nodes, |
| 224 | &cpuset_current_mems_allowed); |
| 225 | else |
| 226 | nodes_and(cpuset_context_nmask, *nodes, |
| 227 | cpuset_current_mems_allowed); |
| 228 | if (mpol_store_user_nodemask(policy)) |
| 229 | policy->w.user_nodemask = *nodes; |
| 230 | else |
| 231 | policy->w.cpuset_mems_allowed = |
| 232 | cpuset_mems_allowed(current); |
| 233 | } |
| 234 | |
| 235 | ret = mpol_ops[mode].create(policy, |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 236 | nodes ? &cpuset_context_nmask : NULL); |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 237 | if (ret < 0) { |
| 238 | kmem_cache_free(policy_cache, policy); |
| 239 | return ERR_PTR(ret); |
| 240 | } |
| 241 | return policy; |
| 242 | } |
| 243 | |
| 244 | static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes) |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | static void mpol_rebind_nodemask(struct mempolicy *pol, |
| 249 | const nodemask_t *nodes) |
| 250 | { |
| 251 | nodemask_t tmp; |
| 252 | |
| 253 | if (pol->flags & MPOL_F_STATIC_NODES) |
| 254 | nodes_and(tmp, pol->w.user_nodemask, *nodes); |
| 255 | else if (pol->flags & MPOL_F_RELATIVE_NODES) |
| 256 | mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); |
| 257 | else { |
| 258 | nodes_remap(tmp, pol->v.nodes, pol->w.cpuset_mems_allowed, |
| 259 | *nodes); |
| 260 | pol->w.cpuset_mems_allowed = *nodes; |
| 261 | } |
| 262 | |
| 263 | pol->v.nodes = tmp; |
| 264 | if (!node_isset(current->il_next, tmp)) { |
| 265 | current->il_next = next_node(current->il_next, tmp); |
| 266 | if (current->il_next >= MAX_NUMNODES) |
| 267 | current->il_next = first_node(tmp); |
| 268 | if (current->il_next >= MAX_NUMNODES) |
| 269 | current->il_next = numa_node_id(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static void mpol_rebind_preferred(struct mempolicy *pol, |
| 274 | const nodemask_t *nodes) |
| 275 | { |
| 276 | nodemask_t tmp; |
| 277 | |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 278 | if (pol->flags & MPOL_F_STATIC_NODES) { |
| 279 | int node = first_node(pol->w.user_nodemask); |
| 280 | |
| 281 | if (node_isset(node, *nodes)) |
| 282 | pol->v.preferred_node = node; |
| 283 | else |
| 284 | pol->v.preferred_node = -1; |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 285 | } else if (pol->flags & MPOL_F_RELATIVE_NODES) { |
| 286 | mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); |
| 287 | pol->v.preferred_node = first_node(tmp); |
David Rientjes | 3e1f0645 | 2008-04-28 02:12:34 -0700 | [diff] [blame] | 288 | } else if (pol->v.preferred_node != -1) { |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 289 | pol->v.preferred_node = node_remap(pol->v.preferred_node, |
| 290 | pol->w.cpuset_mems_allowed, |
| 291 | *nodes); |
| 292 | pol->w.cpuset_mems_allowed = *nodes; |
| 293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
David Rientjes | 1d0d268 | 2008-04-28 02:12:32 -0700 | [diff] [blame] | 296 | /* Migrate a policy to a different set of nodes */ |
| 297 | static void mpol_rebind_policy(struct mempolicy *pol, |
| 298 | const nodemask_t *newmask) |
| 299 | { |
David Rientjes | 1d0d268 | 2008-04-28 02:12:32 -0700 | [diff] [blame] | 300 | if (!pol) |
| 301 | return; |
David Rientjes | 1d0d268 | 2008-04-28 02:12:32 -0700 | [diff] [blame] | 302 | if (!mpol_store_user_nodemask(pol) && |
| 303 | nodes_equal(pol->w.cpuset_mems_allowed, *newmask)) |
| 304 | return; |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 305 | mpol_ops[pol->policy].rebind(pol, newmask); |
David Rientjes | 1d0d268 | 2008-04-28 02:12:32 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Wrapper for mpol_rebind_policy() that just requires task |
| 310 | * pointer, and updates task mempolicy. |
| 311 | */ |
| 312 | |
| 313 | void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new) |
| 314 | { |
| 315 | mpol_rebind_policy(tsk->mempolicy, new); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Rebind each vma in mm to new nodemask. |
| 320 | * |
| 321 | * Call holding a reference to mm. Takes mm->mmap_sem during call. |
| 322 | */ |
| 323 | |
| 324 | void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new) |
| 325 | { |
| 326 | struct vm_area_struct *vma; |
| 327 | |
| 328 | down_write(&mm->mmap_sem); |
| 329 | for (vma = mm->mmap; vma; vma = vma->vm_next) |
| 330 | mpol_rebind_policy(vma->vm_policy, new); |
| 331 | up_write(&mm->mmap_sem); |
| 332 | } |
| 333 | |
David Rientjes | 3701294 | 2008-04-28 02:12:33 -0700 | [diff] [blame] | 334 | static const struct mempolicy_operations mpol_ops[MPOL_MAX] = { |
| 335 | [MPOL_DEFAULT] = { |
| 336 | .rebind = mpol_rebind_default, |
| 337 | }, |
| 338 | [MPOL_INTERLEAVE] = { |
| 339 | .create = mpol_new_interleave, |
| 340 | .rebind = mpol_rebind_nodemask, |
| 341 | }, |
| 342 | [MPOL_PREFERRED] = { |
| 343 | .create = mpol_new_preferred, |
| 344 | .rebind = mpol_rebind_preferred, |
| 345 | }, |
| 346 | [MPOL_BIND] = { |
| 347 | .create = mpol_new_bind, |
| 348 | .rebind = mpol_rebind_nodemask, |
| 349 | }, |
| 350 | }; |
| 351 | |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 352 | static void gather_stats(struct page *, void *, int pte_dirty); |
Christoph Lameter | fc30128 | 2006-01-18 17:42:29 -0800 | [diff] [blame] | 353 | static void migrate_page_add(struct page *page, struct list_head *pagelist, |
| 354 | unsigned long flags); |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 355 | |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 356 | /* Scan through pages checking if pages follow certain conditions. */ |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 357 | static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd, |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 358 | unsigned long addr, unsigned long end, |
| 359 | const nodemask_t *nodes, unsigned long flags, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 360 | void *private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 362 | pte_t *orig_pte; |
| 363 | pte_t *pte; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 364 | spinlock_t *ptl; |
Hugh Dickins | 941150a | 2005-06-21 17:15:06 -0700 | [diff] [blame] | 365 | |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 366 | orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 367 | do { |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 368 | struct page *page; |
Andy Whitcroft | 25ba77c | 2006-12-06 20:33:03 -0800 | [diff] [blame] | 369 | int nid; |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 370 | |
| 371 | if (!pte_present(*pte)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | continue; |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 373 | page = vm_normal_page(vma, addr, *pte); |
| 374 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | continue; |
Nick Piggin | 053837f | 2006-01-18 17:42:27 -0800 | [diff] [blame] | 376 | /* |
| 377 | * The check for PageReserved here is important to avoid |
| 378 | * handling zero pages and other pages that may have been |
| 379 | * marked special by the system. |
| 380 | * |
| 381 | * If the PageReserved would not be checked here then f.e. |
| 382 | * the location of the zero page could have an influence |
| 383 | * on MPOL_MF_STRICT, zero pages would be counted for |
| 384 | * the per node stats, and there would be useless attempts |
| 385 | * to put zero pages on the migration list. |
| 386 | */ |
Christoph Lameter | f4598c8 | 2006-01-12 01:05:20 -0800 | [diff] [blame] | 387 | if (PageReserved(page)) |
| 388 | continue; |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 389 | nid = page_to_nid(page); |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 390 | if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT)) |
| 391 | continue; |
| 392 | |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 393 | if (flags & MPOL_MF_STATS) |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 394 | gather_stats(page, private, pte_dirty(*pte)); |
Nick Piggin | 053837f | 2006-01-18 17:42:27 -0800 | [diff] [blame] | 395 | else if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) |
Christoph Lameter | fc30128 | 2006-01-18 17:42:29 -0800 | [diff] [blame] | 396 | migrate_page_add(page, private, flags); |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 397 | else |
| 398 | break; |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 399 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 400 | pte_unmap_unlock(orig_pte, ptl); |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 401 | return addr != end; |
| 402 | } |
| 403 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 404 | static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud, |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 405 | unsigned long addr, unsigned long end, |
| 406 | const nodemask_t *nodes, unsigned long flags, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 407 | void *private) |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 408 | { |
| 409 | pmd_t *pmd; |
| 410 | unsigned long next; |
| 411 | |
| 412 | pmd = pmd_offset(pud, addr); |
| 413 | do { |
| 414 | next = pmd_addr_end(addr, end); |
| 415 | if (pmd_none_or_clear_bad(pmd)) |
| 416 | continue; |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 417 | if (check_pte_range(vma, pmd, addr, next, nodes, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 418 | flags, private)) |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 419 | return -EIO; |
| 420 | } while (pmd++, addr = next, addr != end); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 424 | static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd, |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 425 | unsigned long addr, unsigned long end, |
| 426 | const nodemask_t *nodes, unsigned long flags, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 427 | void *private) |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 428 | { |
| 429 | pud_t *pud; |
| 430 | unsigned long next; |
| 431 | |
| 432 | pud = pud_offset(pgd, addr); |
| 433 | do { |
| 434 | next = pud_addr_end(addr, end); |
| 435 | if (pud_none_or_clear_bad(pud)) |
| 436 | continue; |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 437 | if (check_pmd_range(vma, pud, addr, next, nodes, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 438 | flags, private)) |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 439 | return -EIO; |
| 440 | } while (pud++, addr = next, addr != end); |
| 441 | return 0; |
| 442 | } |
| 443 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 444 | static inline int check_pgd_range(struct vm_area_struct *vma, |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 445 | unsigned long addr, unsigned long end, |
| 446 | const nodemask_t *nodes, unsigned long flags, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 447 | void *private) |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 448 | { |
| 449 | pgd_t *pgd; |
| 450 | unsigned long next; |
| 451 | |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 452 | pgd = pgd_offset(vma->vm_mm, addr); |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 453 | do { |
| 454 | next = pgd_addr_end(addr, end); |
| 455 | if (pgd_none_or_clear_bad(pgd)) |
| 456 | continue; |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 457 | if (check_pud_range(vma, pgd, addr, next, nodes, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 458 | flags, private)) |
Hugh Dickins | 91612e0 | 2005-06-21 17:15:07 -0700 | [diff] [blame] | 459 | return -EIO; |
| 460 | } while (pgd++, addr = next, addr != end); |
| 461 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 464 | /* |
| 465 | * Check if all pages in a range are on a set of nodes. |
| 466 | * If pagelist != NULL then isolate pages from the LRU and |
| 467 | * put them on the pagelist. |
| 468 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | static struct vm_area_struct * |
| 470 | check_range(struct mm_struct *mm, unsigned long start, unsigned long end, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 471 | const nodemask_t *nodes, unsigned long flags, void *private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | int err; |
| 474 | struct vm_area_struct *first, *vma, *prev; |
| 475 | |
Christoph Lameter | 90036ee | 2006-03-16 23:03:59 -0800 | [diff] [blame] | 476 | if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { |
Christoph Lameter | 90036ee | 2006-03-16 23:03:59 -0800 | [diff] [blame] | 477 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 478 | err = migrate_prep(); |
| 479 | if (err) |
| 480 | return ERR_PTR(err); |
Christoph Lameter | 90036ee | 2006-03-16 23:03:59 -0800 | [diff] [blame] | 481 | } |
Nick Piggin | 053837f | 2006-01-18 17:42:27 -0800 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | first = find_vma(mm, start); |
| 484 | if (!first) |
| 485 | return ERR_PTR(-EFAULT); |
| 486 | prev = NULL; |
| 487 | for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) { |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 488 | if (!(flags & MPOL_MF_DISCONTIG_OK)) { |
| 489 | if (!vma->vm_next && vma->vm_end < end) |
| 490 | return ERR_PTR(-EFAULT); |
| 491 | if (prev && prev->vm_end < vma->vm_start) |
| 492 | return ERR_PTR(-EFAULT); |
| 493 | } |
| 494 | if (!is_vm_hugetlb_page(vma) && |
| 495 | ((flags & MPOL_MF_STRICT) || |
| 496 | ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) && |
| 497 | vma_migratable(vma)))) { |
Andi Kleen | 5b952b3 | 2005-09-13 01:25:08 -0700 | [diff] [blame] | 498 | unsigned long endvma = vma->vm_end; |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 499 | |
Andi Kleen | 5b952b3 | 2005-09-13 01:25:08 -0700 | [diff] [blame] | 500 | if (endvma > end) |
| 501 | endvma = end; |
| 502 | if (vma->vm_start > start) |
| 503 | start = vma->vm_start; |
Christoph Lameter | dc9aa5b | 2006-01-08 01:00:50 -0800 | [diff] [blame] | 504 | err = check_pgd_range(vma, start, endvma, nodes, |
Christoph Lameter | 38e3586 | 2006-01-08 01:01:01 -0800 | [diff] [blame] | 505 | flags, private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (err) { |
| 507 | first = ERR_PTR(err); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | prev = vma; |
| 512 | } |
| 513 | return first; |
| 514 | } |
| 515 | |
| 516 | /* Apply policy to a single VMA */ |
| 517 | static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new) |
| 518 | { |
| 519 | int err = 0; |
| 520 | struct mempolicy *old = vma->vm_policy; |
| 521 | |
Paul Mundt | 140d5a4 | 2007-07-15 23:38:16 -0700 | [diff] [blame] | 522 | pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | vma->vm_start, vma->vm_end, vma->vm_pgoff, |
| 524 | vma->vm_ops, vma->vm_file, |
| 525 | vma->vm_ops ? vma->vm_ops->set_policy : NULL); |
| 526 | |
| 527 | if (vma->vm_ops && vma->vm_ops->set_policy) |
| 528 | err = vma->vm_ops->set_policy(vma, new); |
| 529 | if (!err) { |
| 530 | mpol_get(new); |
| 531 | vma->vm_policy = new; |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 532 | mpol_put(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | return err; |
| 535 | } |
| 536 | |
| 537 | /* Step 2: apply policy to a range and do splits. */ |
| 538 | static int mbind_range(struct vm_area_struct *vma, unsigned long start, |
| 539 | unsigned long end, struct mempolicy *new) |
| 540 | { |
| 541 | struct vm_area_struct *next; |
| 542 | int err; |
| 543 | |
| 544 | err = 0; |
| 545 | for (; vma && vma->vm_start < end; vma = next) { |
| 546 | next = vma->vm_next; |
| 547 | if (vma->vm_start < start) |
| 548 | err = split_vma(vma->vm_mm, vma, start, 1); |
| 549 | if (!err && vma->vm_end > end) |
| 550 | err = split_vma(vma->vm_mm, vma, end, 0); |
| 551 | if (!err) |
| 552 | err = policy_vma(vma, new); |
| 553 | if (err) |
| 554 | break; |
| 555 | } |
| 556 | return err; |
| 557 | } |
| 558 | |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 559 | /* |
| 560 | * Update task->flags PF_MEMPOLICY bit: set iff non-default |
| 561 | * mempolicy. Allows more rapid checking of this (combined perhaps |
| 562 | * with other PF_* flag bits) on memory allocation hot code paths. |
| 563 | * |
| 564 | * If called from outside this file, the task 'p' should -only- be |
| 565 | * a newly forked child not yet visible on the task list, because |
| 566 | * manipulating the task flags of a visible task is not safe. |
| 567 | * |
| 568 | * The above limitation is why this routine has the funny name |
| 569 | * mpol_fix_fork_child_flag(). |
| 570 | * |
| 571 | * It is also safe to call this with a task pointer of current, |
| 572 | * which the static wrapper mpol_set_task_struct_flag() does, |
| 573 | * for use within this file. |
| 574 | */ |
| 575 | |
| 576 | void mpol_fix_fork_child_flag(struct task_struct *p) |
| 577 | { |
| 578 | if (p->mempolicy) |
| 579 | p->flags |= PF_MEMPOLICY; |
| 580 | else |
| 581 | p->flags &= ~PF_MEMPOLICY; |
| 582 | } |
| 583 | |
| 584 | static void mpol_set_task_struct_flag(void) |
| 585 | { |
| 586 | mpol_fix_fork_child_flag(current); |
| 587 | } |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* Set the process memory policy */ |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 590 | static long do_set_mempolicy(unsigned short mode, unsigned short flags, |
| 591 | nodemask_t *nodes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | struct mempolicy *new; |
Lee Schermerhorn | f4e53d9 | 2008-04-28 02:13:10 -0700 | [diff] [blame^] | 594 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 596 | new = mpol_new(mode, flags, nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if (IS_ERR(new)) |
| 598 | return PTR_ERR(new); |
Lee Schermerhorn | f4e53d9 | 2008-04-28 02:13:10 -0700 | [diff] [blame^] | 599 | |
| 600 | /* |
| 601 | * prevent changing our mempolicy while show_numa_maps() |
| 602 | * is using it. |
| 603 | * Note: do_set_mempolicy() can be called at init time |
| 604 | * with no 'mm'. |
| 605 | */ |
| 606 | if (mm) |
| 607 | down_write(&mm->mmap_sem); |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 608 | mpol_put(current->mempolicy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | current->mempolicy = new; |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 610 | mpol_set_task_struct_flag(); |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 611 | if (new && new->policy == MPOL_INTERLEAVE && |
| 612 | nodes_weight(new->v.nodes)) |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 613 | current->il_next = first_node(new->v.nodes); |
Lee Schermerhorn | f4e53d9 | 2008-04-28 02:13:10 -0700 | [diff] [blame^] | 614 | if (mm) |
| 615 | up_write(&mm->mmap_sem); |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | /* Fill a zone bitmap for a policy */ |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 621 | static void get_zonemask(struct mempolicy *p, nodemask_t *nodes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 623 | nodes_clear(*nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | switch (p->policy) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | case MPOL_DEFAULT: |
| 626 | break; |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 627 | case MPOL_BIND: |
| 628 | /* Fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | case MPOL_INTERLEAVE: |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 630 | *nodes = p->v.nodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | break; |
| 632 | case MPOL_PREFERRED: |
Christoph Lameter | 56bbd65 | 2007-10-16 01:25:35 -0700 | [diff] [blame] | 633 | /* or use current node instead of memory_map? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (p->v.preferred_node < 0) |
Christoph Lameter | 56bbd65 | 2007-10-16 01:25:35 -0700 | [diff] [blame] | 635 | *nodes = node_states[N_HIGH_MEMORY]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | else |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 637 | node_set(p->v.preferred_node, *nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | break; |
| 639 | default: |
| 640 | BUG(); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | static int lookup_node(struct mm_struct *mm, unsigned long addr) |
| 645 | { |
| 646 | struct page *p; |
| 647 | int err; |
| 648 | |
| 649 | err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL); |
| 650 | if (err >= 0) { |
| 651 | err = page_to_nid(p); |
| 652 | put_page(p); |
| 653 | } |
| 654 | return err; |
| 655 | } |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | /* Retrieve NUMA policy */ |
Adrian Bunk | dbcb0f1 | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 658 | static long do_get_mempolicy(int *policy, nodemask_t *nmask, |
| 659 | unsigned long addr, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 661 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | struct mm_struct *mm = current->mm; |
| 663 | struct vm_area_struct *vma = NULL; |
| 664 | struct mempolicy *pol = current->mempolicy; |
| 665 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 666 | cpuset_update_task_memory_state(); |
Lee Schermerhorn | 754af6f | 2007-10-16 01:24:51 -0700 | [diff] [blame] | 667 | if (flags & |
| 668 | ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return -EINVAL; |
Lee Schermerhorn | 754af6f | 2007-10-16 01:24:51 -0700 | [diff] [blame] | 670 | |
| 671 | if (flags & MPOL_F_MEMS_ALLOWED) { |
| 672 | if (flags & (MPOL_F_NODE|MPOL_F_ADDR)) |
| 673 | return -EINVAL; |
| 674 | *policy = 0; /* just so it's initialized */ |
| 675 | *nmask = cpuset_current_mems_allowed; |
| 676 | return 0; |
| 677 | } |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | if (flags & MPOL_F_ADDR) { |
| 680 | down_read(&mm->mmap_sem); |
| 681 | vma = find_vma_intersection(mm, addr, addr+1); |
| 682 | if (!vma) { |
| 683 | up_read(&mm->mmap_sem); |
| 684 | return -EFAULT; |
| 685 | } |
| 686 | if (vma->vm_ops && vma->vm_ops->get_policy) |
| 687 | pol = vma->vm_ops->get_policy(vma, addr); |
| 688 | else |
| 689 | pol = vma->vm_policy; |
| 690 | } else if (addr) |
| 691 | return -EINVAL; |
| 692 | |
| 693 | if (!pol) |
| 694 | pol = &default_policy; |
| 695 | |
| 696 | if (flags & MPOL_F_NODE) { |
| 697 | if (flags & MPOL_F_ADDR) { |
| 698 | err = lookup_node(mm, addr); |
| 699 | if (err < 0) |
| 700 | goto out; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 701 | *policy = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } else if (pol == current->mempolicy && |
| 703 | pol->policy == MPOL_INTERLEAVE) { |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 704 | *policy = current->il_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } else { |
| 706 | err = -EINVAL; |
| 707 | goto out; |
| 708 | } |
| 709 | } else |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 710 | *policy = pol->policy | pol->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | if (vma) { |
| 713 | up_read(¤t->mm->mmap_sem); |
| 714 | vma = NULL; |
| 715 | } |
| 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | err = 0; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 718 | if (nmask) |
| 719 | get_zonemask(pol, nmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | out: |
| 722 | if (vma) |
| 723 | up_read(¤t->mm->mmap_sem); |
| 724 | return err; |
| 725 | } |
| 726 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 727 | #ifdef CONFIG_MIGRATION |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 728 | /* |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 729 | * page migration |
| 730 | */ |
Christoph Lameter | fc30128 | 2006-01-18 17:42:29 -0800 | [diff] [blame] | 731 | static void migrate_page_add(struct page *page, struct list_head *pagelist, |
| 732 | unsigned long flags) |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 733 | { |
| 734 | /* |
Christoph Lameter | fc30128 | 2006-01-18 17:42:29 -0800 | [diff] [blame] | 735 | * Avoid migrating a page that is shared with others. |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 736 | */ |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 737 | if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) |
| 738 | isolate_lru_page(page, pagelist); |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 741 | static struct page *new_node_page(struct page *page, unsigned long node, int **x) |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 742 | { |
Mel Gorman | 769848c | 2007-07-17 04:03:05 -0700 | [diff] [blame] | 743 | return alloc_pages_node(node, GFP_HIGHUSER_MOVABLE, 0); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 746 | /* |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 747 | * Migrate pages from one node to a target node. |
| 748 | * Returns error or the number of pages not migrated. |
| 749 | */ |
Adrian Bunk | dbcb0f1 | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 750 | static int migrate_to_node(struct mm_struct *mm, int source, int dest, |
| 751 | int flags) |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 752 | { |
| 753 | nodemask_t nmask; |
| 754 | LIST_HEAD(pagelist); |
| 755 | int err = 0; |
| 756 | |
| 757 | nodes_clear(nmask); |
| 758 | node_set(source, nmask); |
| 759 | |
| 760 | check_range(mm, mm->mmap->vm_start, TASK_SIZE, &nmask, |
| 761 | flags | MPOL_MF_DISCONTIG_OK, &pagelist); |
| 762 | |
Christoph Lameter | aaa994b | 2006-06-23 02:03:52 -0700 | [diff] [blame] | 763 | if (!list_empty(&pagelist)) |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 764 | err = migrate_pages(&pagelist, new_node_page, dest); |
| 765 | |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 766 | return err; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Move pages between the two nodesets so as to preserve the physical |
| 771 | * layout as much as possible. |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 772 | * |
| 773 | * Returns the number of page that could not be moved. |
| 774 | */ |
| 775 | int do_migrate_pages(struct mm_struct *mm, |
| 776 | const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags) |
| 777 | { |
| 778 | LIST_HEAD(pagelist); |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 779 | int busy = 0; |
| 780 | int err = 0; |
| 781 | nodemask_t tmp; |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 782 | |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 783 | down_read(&mm->mmap_sem); |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 784 | |
Christoph Lameter | 7b2259b | 2006-06-25 05:46:48 -0700 | [diff] [blame] | 785 | err = migrate_vmas(mm, from_nodes, to_nodes, flags); |
| 786 | if (err) |
| 787 | goto out; |
| 788 | |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 789 | /* |
| 790 | * Find a 'source' bit set in 'tmp' whose corresponding 'dest' |
| 791 | * bit in 'to' is not also set in 'tmp'. Clear the found 'source' |
| 792 | * bit in 'tmp', and return that <source, dest> pair for migration. |
| 793 | * The pair of nodemasks 'to' and 'from' define the map. |
| 794 | * |
| 795 | * If no pair of bits is found that way, fallback to picking some |
| 796 | * pair of 'source' and 'dest' bits that are not the same. If the |
| 797 | * 'source' and 'dest' bits are the same, this represents a node |
| 798 | * that will be migrating to itself, so no pages need move. |
| 799 | * |
| 800 | * If no bits are left in 'tmp', or if all remaining bits left |
| 801 | * in 'tmp' correspond to the same bit in 'to', return false |
| 802 | * (nothing left to migrate). |
| 803 | * |
| 804 | * This lets us pick a pair of nodes to migrate between, such that |
| 805 | * if possible the dest node is not already occupied by some other |
| 806 | * source node, minimizing the risk of overloading the memory on a |
| 807 | * node that would happen if we migrated incoming memory to a node |
| 808 | * before migrating outgoing memory source that same node. |
| 809 | * |
| 810 | * A single scan of tmp is sufficient. As we go, we remember the |
| 811 | * most recent <s, d> pair that moved (s != d). If we find a pair |
| 812 | * that not only moved, but what's better, moved to an empty slot |
| 813 | * (d is not set in tmp), then we break out then, with that pair. |
| 814 | * Otherwise when we finish scannng from_tmp, we at least have the |
| 815 | * most recent <s, d> pair that moved. If we get all the way through |
| 816 | * the scan of tmp without finding any node that moved, much less |
| 817 | * moved to an empty node, then there is nothing left worth migrating. |
| 818 | */ |
Christoph Lameter | d498471 | 2006-01-08 01:00:55 -0800 | [diff] [blame] | 819 | |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 820 | tmp = *from_nodes; |
| 821 | while (!nodes_empty(tmp)) { |
| 822 | int s,d; |
| 823 | int source = -1; |
| 824 | int dest = 0; |
| 825 | |
| 826 | for_each_node_mask(s, tmp) { |
| 827 | d = node_remap(s, *from_nodes, *to_nodes); |
| 828 | if (s == d) |
| 829 | continue; |
| 830 | |
| 831 | source = s; /* Node moved. Memorize */ |
| 832 | dest = d; |
| 833 | |
| 834 | /* dest not in remaining from nodes? */ |
| 835 | if (!node_isset(dest, tmp)) |
| 836 | break; |
| 837 | } |
| 838 | if (source == -1) |
| 839 | break; |
| 840 | |
| 841 | node_clear(source, tmp); |
| 842 | err = migrate_to_node(mm, source, dest, flags); |
| 843 | if (err > 0) |
| 844 | busy += err; |
| 845 | if (err < 0) |
| 846 | break; |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 847 | } |
Christoph Lameter | 7b2259b | 2006-06-25 05:46:48 -0700 | [diff] [blame] | 848 | out: |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 849 | up_read(&mm->mmap_sem); |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 850 | if (err < 0) |
| 851 | return err; |
| 852 | return busy; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 853 | |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Lee Schermerhorn | 3ad33b24 | 2007-11-14 16:59:10 -0800 | [diff] [blame] | 856 | /* |
| 857 | * Allocate a new page for page migration based on vma policy. |
| 858 | * Start assuming that page is mapped by vma pointed to by @private. |
| 859 | * Search forward from there, if not. N.B., this assumes that the |
| 860 | * list of pages handed to migrate_pages()--which is how we get here-- |
| 861 | * is in virtual address order. |
| 862 | */ |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 863 | static struct page *new_vma_page(struct page *page, unsigned long private, int **x) |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 864 | { |
| 865 | struct vm_area_struct *vma = (struct vm_area_struct *)private; |
Lee Schermerhorn | 3ad33b24 | 2007-11-14 16:59:10 -0800 | [diff] [blame] | 866 | unsigned long uninitialized_var(address); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 867 | |
Lee Schermerhorn | 3ad33b24 | 2007-11-14 16:59:10 -0800 | [diff] [blame] | 868 | while (vma) { |
| 869 | address = page_address_in_vma(page, vma); |
| 870 | if (address != -EFAULT) |
| 871 | break; |
| 872 | vma = vma->vm_next; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * if !vma, alloc_page_vma() will use task or system default policy |
| 877 | */ |
| 878 | return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 879 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 880 | #else |
| 881 | |
| 882 | static void migrate_page_add(struct page *page, struct list_head *pagelist, |
| 883 | unsigned long flags) |
| 884 | { |
| 885 | } |
| 886 | |
| 887 | int do_migrate_pages(struct mm_struct *mm, |
| 888 | const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags) |
| 889 | { |
| 890 | return -ENOSYS; |
| 891 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 892 | |
Keith Owens | 6993974 | 2006-10-11 01:21:28 -0700 | [diff] [blame] | 893 | static struct page *new_vma_page(struct page *page, unsigned long private, int **x) |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 894 | { |
| 895 | return NULL; |
| 896 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 897 | #endif |
| 898 | |
Adrian Bunk | dbcb0f1 | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 899 | static long do_mbind(unsigned long start, unsigned long len, |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 900 | unsigned short mode, unsigned short mode_flags, |
| 901 | nodemask_t *nmask, unsigned long flags) |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 902 | { |
| 903 | struct vm_area_struct *vma; |
| 904 | struct mm_struct *mm = current->mm; |
| 905 | struct mempolicy *new; |
| 906 | unsigned long end; |
| 907 | int err; |
| 908 | LIST_HEAD(pagelist); |
| 909 | |
David Rientjes | a3b51e0 | 2008-04-28 02:12:23 -0700 | [diff] [blame] | 910 | if (flags & ~(unsigned long)(MPOL_MF_STRICT | |
| 911 | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 912 | return -EINVAL; |
Christoph Lameter | 74c0024 | 2006-03-14 19:50:21 -0800 | [diff] [blame] | 913 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 914 | return -EPERM; |
| 915 | |
| 916 | if (start & ~PAGE_MASK) |
| 917 | return -EINVAL; |
| 918 | |
| 919 | if (mode == MPOL_DEFAULT) |
| 920 | flags &= ~MPOL_MF_STRICT; |
| 921 | |
| 922 | len = (len + PAGE_SIZE - 1) & PAGE_MASK; |
| 923 | end = start + len; |
| 924 | |
| 925 | if (end < start) |
| 926 | return -EINVAL; |
| 927 | if (end == start) |
| 928 | return 0; |
| 929 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 930 | new = mpol_new(mode, mode_flags, nmask); |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 931 | if (IS_ERR(new)) |
| 932 | return PTR_ERR(new); |
| 933 | |
| 934 | /* |
| 935 | * If we are using the default policy then operation |
| 936 | * on discontinuous address spaces is okay after all |
| 937 | */ |
| 938 | if (!new) |
| 939 | flags |= MPOL_MF_DISCONTIG_OK; |
| 940 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 941 | pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n", |
| 942 | start, start + len, mode, mode_flags, |
| 943 | nmask ? nodes_addr(*nmask)[0] : -1); |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 944 | |
| 945 | down_write(&mm->mmap_sem); |
| 946 | vma = check_range(mm, start, end, nmask, |
| 947 | flags | MPOL_MF_INVERT, &pagelist); |
| 948 | |
| 949 | err = PTR_ERR(vma); |
| 950 | if (!IS_ERR(vma)) { |
| 951 | int nr_failed = 0; |
| 952 | |
| 953 | err = mbind_range(vma, start, end, new); |
Christoph Lameter | 7e2ab15 | 2006-02-01 03:05:40 -0800 | [diff] [blame] | 954 | |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 955 | if (!list_empty(&pagelist)) |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 956 | nr_failed = migrate_pages(&pagelist, new_vma_page, |
| 957 | (unsigned long)vma); |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 958 | |
| 959 | if (!err && nr_failed && (flags & MPOL_MF_STRICT)) |
| 960 | err = -EIO; |
| 961 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 962 | |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 963 | up_write(&mm->mmap_sem); |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 964 | mpol_put(new); |
Christoph Lameter | 6ce3c4c | 2006-01-08 01:01:04 -0800 | [diff] [blame] | 965 | return err; |
| 966 | } |
| 967 | |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 968 | /* |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 969 | * User space interface with variable sized bitmaps for nodelists. |
| 970 | */ |
| 971 | |
| 972 | /* Copy a node mask from user space. */ |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 973 | static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 974 | unsigned long maxnode) |
| 975 | { |
| 976 | unsigned long k; |
| 977 | unsigned long nlongs; |
| 978 | unsigned long endmask; |
| 979 | |
| 980 | --maxnode; |
| 981 | nodes_clear(*nodes); |
| 982 | if (maxnode == 0 || !nmask) |
| 983 | return 0; |
Andi Kleen | a9c930b | 2006-02-20 18:27:59 -0800 | [diff] [blame] | 984 | if (maxnode > PAGE_SIZE*BITS_PER_BYTE) |
Chris Wright | 636f13c | 2006-02-17 13:59:36 -0800 | [diff] [blame] | 985 | return -EINVAL; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 986 | |
| 987 | nlongs = BITS_TO_LONGS(maxnode); |
| 988 | if ((maxnode % BITS_PER_LONG) == 0) |
| 989 | endmask = ~0UL; |
| 990 | else |
| 991 | endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1; |
| 992 | |
| 993 | /* When the user specified more nodes than supported just check |
| 994 | if the non supported part is all zero. */ |
| 995 | if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) { |
| 996 | if (nlongs > PAGE_SIZE/sizeof(long)) |
| 997 | return -EINVAL; |
| 998 | for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) { |
| 999 | unsigned long t; |
| 1000 | if (get_user(t, nmask + k)) |
| 1001 | return -EFAULT; |
| 1002 | if (k == nlongs - 1) { |
| 1003 | if (t & endmask) |
| 1004 | return -EINVAL; |
| 1005 | } else if (t) |
| 1006 | return -EINVAL; |
| 1007 | } |
| 1008 | nlongs = BITS_TO_LONGS(MAX_NUMNODES); |
| 1009 | endmask = ~0UL; |
| 1010 | } |
| 1011 | |
| 1012 | if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long))) |
| 1013 | return -EFAULT; |
| 1014 | nodes_addr(*nodes)[nlongs-1] &= endmask; |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | /* Copy a kernel node mask to user space */ |
| 1019 | static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, |
| 1020 | nodemask_t *nodes) |
| 1021 | { |
| 1022 | unsigned long copy = ALIGN(maxnode-1, 64) / 8; |
| 1023 | const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long); |
| 1024 | |
| 1025 | if (copy > nbytes) { |
| 1026 | if (copy > PAGE_SIZE) |
| 1027 | return -EINVAL; |
| 1028 | if (clear_user((char __user *)mask + nbytes, copy - nbytes)) |
| 1029 | return -EFAULT; |
| 1030 | copy = nbytes; |
| 1031 | } |
| 1032 | return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; |
| 1033 | } |
| 1034 | |
| 1035 | asmlinkage long sys_mbind(unsigned long start, unsigned long len, |
| 1036 | unsigned long mode, |
| 1037 | unsigned long __user *nmask, unsigned long maxnode, |
| 1038 | unsigned flags) |
| 1039 | { |
| 1040 | nodemask_t nodes; |
| 1041 | int err; |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1042 | unsigned short mode_flags; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1043 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1044 | mode_flags = mode & MPOL_MODE_FLAGS; |
| 1045 | mode &= ~MPOL_MODE_FLAGS; |
David Rientjes | a3b51e0 | 2008-04-28 02:12:23 -0700 | [diff] [blame] | 1046 | if (mode >= MPOL_MAX) |
| 1047 | return -EINVAL; |
David Rientjes | 4c50bc0 | 2008-04-28 02:12:30 -0700 | [diff] [blame] | 1048 | if ((mode_flags & MPOL_F_STATIC_NODES) && |
| 1049 | (mode_flags & MPOL_F_RELATIVE_NODES)) |
| 1050 | return -EINVAL; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1051 | err = get_nodes(&nodes, nmask, maxnode); |
| 1052 | if (err) |
| 1053 | return err; |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1054 | return do_mbind(start, len, mode, mode_flags, &nodes, flags); |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /* Set the process memory policy */ |
| 1058 | asmlinkage long sys_set_mempolicy(int mode, unsigned long __user *nmask, |
| 1059 | unsigned long maxnode) |
| 1060 | { |
| 1061 | int err; |
| 1062 | nodemask_t nodes; |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1063 | unsigned short flags; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1064 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1065 | flags = mode & MPOL_MODE_FLAGS; |
| 1066 | mode &= ~MPOL_MODE_FLAGS; |
| 1067 | if ((unsigned int)mode >= MPOL_MAX) |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1068 | return -EINVAL; |
David Rientjes | 4c50bc0 | 2008-04-28 02:12:30 -0700 | [diff] [blame] | 1069 | if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES)) |
| 1070 | return -EINVAL; |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1071 | err = get_nodes(&nodes, nmask, maxnode); |
| 1072 | if (err) |
| 1073 | return err; |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1074 | return do_set_mempolicy(mode, flags, &nodes); |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 1077 | asmlinkage long sys_migrate_pages(pid_t pid, unsigned long maxnode, |
| 1078 | const unsigned long __user *old_nodes, |
| 1079 | const unsigned long __user *new_nodes) |
| 1080 | { |
| 1081 | struct mm_struct *mm; |
| 1082 | struct task_struct *task; |
| 1083 | nodemask_t old; |
| 1084 | nodemask_t new; |
| 1085 | nodemask_t task_nodes; |
| 1086 | int err; |
| 1087 | |
| 1088 | err = get_nodes(&old, old_nodes, maxnode); |
| 1089 | if (err) |
| 1090 | return err; |
| 1091 | |
| 1092 | err = get_nodes(&new, new_nodes, maxnode); |
| 1093 | if (err) |
| 1094 | return err; |
| 1095 | |
| 1096 | /* Find the mm_struct */ |
| 1097 | read_lock(&tasklist_lock); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 1098 | task = pid ? find_task_by_vpid(pid) : current; |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 1099 | if (!task) { |
| 1100 | read_unlock(&tasklist_lock); |
| 1101 | return -ESRCH; |
| 1102 | } |
| 1103 | mm = get_task_mm(task); |
| 1104 | read_unlock(&tasklist_lock); |
| 1105 | |
| 1106 | if (!mm) |
| 1107 | return -EINVAL; |
| 1108 | |
| 1109 | /* |
| 1110 | * Check if this process has the right to modify the specified |
| 1111 | * process. The right exists if the process has administrative |
Alexey Dobriyan | 7f927fc | 2006-03-28 01:56:53 -0800 | [diff] [blame] | 1112 | * capabilities, superuser privileges or the same |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 1113 | * userid as the target process. |
| 1114 | */ |
| 1115 | if ((current->euid != task->suid) && (current->euid != task->uid) && |
| 1116 | (current->uid != task->suid) && (current->uid != task->uid) && |
Christoph Lameter | 74c0024 | 2006-03-14 19:50:21 -0800 | [diff] [blame] | 1117 | !capable(CAP_SYS_NICE)) { |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 1118 | err = -EPERM; |
| 1119 | goto out; |
| 1120 | } |
| 1121 | |
| 1122 | task_nodes = cpuset_mems_allowed(task); |
| 1123 | /* Is the user allowed to access the target nodes? */ |
Christoph Lameter | 74c0024 | 2006-03-14 19:50:21 -0800 | [diff] [blame] | 1124 | if (!nodes_subset(new, task_nodes) && !capable(CAP_SYS_NICE)) { |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 1125 | err = -EPERM; |
| 1126 | goto out; |
| 1127 | } |
| 1128 | |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 1129 | if (!nodes_subset(new, node_states[N_HIGH_MEMORY])) { |
Christoph Lameter | 3b42d28 | 2007-08-31 00:12:08 -0700 | [diff] [blame] | 1130 | err = -EINVAL; |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1134 | err = security_task_movememory(task); |
| 1135 | if (err) |
| 1136 | goto out; |
| 1137 | |
Christoph Lameter | 511030b | 2006-02-28 16:58:57 -0800 | [diff] [blame] | 1138 | err = do_migrate_pages(mm, &old, &new, |
Christoph Lameter | 74c0024 | 2006-03-14 19:50:21 -0800 | [diff] [blame] | 1139 | capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE); |
Christoph Lameter | 3974388 | 2006-01-08 01:00:51 -0800 | [diff] [blame] | 1140 | out: |
| 1141 | mmput(mm); |
| 1142 | return err; |
| 1143 | } |
| 1144 | |
| 1145 | |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1146 | /* Retrieve NUMA policy */ |
| 1147 | asmlinkage long sys_get_mempolicy(int __user *policy, |
| 1148 | unsigned long __user *nmask, |
| 1149 | unsigned long maxnode, |
| 1150 | unsigned long addr, unsigned long flags) |
| 1151 | { |
Adrian Bunk | dbcb0f1 | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 1152 | int err; |
| 1153 | int uninitialized_var(pval); |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1154 | nodemask_t nodes; |
| 1155 | |
| 1156 | if (nmask != NULL && maxnode < MAX_NUMNODES) |
| 1157 | return -EINVAL; |
| 1158 | |
| 1159 | err = do_get_mempolicy(&pval, &nodes, addr, flags); |
| 1160 | |
| 1161 | if (err) |
| 1162 | return err; |
| 1163 | |
| 1164 | if (policy && put_user(pval, policy)) |
| 1165 | return -EFAULT; |
| 1166 | |
| 1167 | if (nmask) |
| 1168 | err = copy_nodes_to_user(nmask, maxnode, &nodes); |
| 1169 | |
| 1170 | return err; |
| 1171 | } |
| 1172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | #ifdef CONFIG_COMPAT |
| 1174 | |
| 1175 | asmlinkage long compat_sys_get_mempolicy(int __user *policy, |
| 1176 | compat_ulong_t __user *nmask, |
| 1177 | compat_ulong_t maxnode, |
| 1178 | compat_ulong_t addr, compat_ulong_t flags) |
| 1179 | { |
| 1180 | long err; |
| 1181 | unsigned long __user *nm = NULL; |
| 1182 | unsigned long nr_bits, alloc_size; |
| 1183 | DECLARE_BITMAP(bm, MAX_NUMNODES); |
| 1184 | |
| 1185 | nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); |
| 1186 | alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; |
| 1187 | |
| 1188 | if (nmask) |
| 1189 | nm = compat_alloc_user_space(alloc_size); |
| 1190 | |
| 1191 | err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags); |
| 1192 | |
| 1193 | if (!err && nmask) { |
| 1194 | err = copy_from_user(bm, nm, alloc_size); |
| 1195 | /* ensure entire bitmap is zeroed */ |
| 1196 | err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8); |
| 1197 | err |= compat_put_bitmap(nmask, bm, nr_bits); |
| 1198 | } |
| 1199 | |
| 1200 | return err; |
| 1201 | } |
| 1202 | |
| 1203 | asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask, |
| 1204 | compat_ulong_t maxnode) |
| 1205 | { |
| 1206 | long err = 0; |
| 1207 | unsigned long __user *nm = NULL; |
| 1208 | unsigned long nr_bits, alloc_size; |
| 1209 | DECLARE_BITMAP(bm, MAX_NUMNODES); |
| 1210 | |
| 1211 | nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); |
| 1212 | alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; |
| 1213 | |
| 1214 | if (nmask) { |
| 1215 | err = compat_get_bitmap(bm, nmask, nr_bits); |
| 1216 | nm = compat_alloc_user_space(alloc_size); |
| 1217 | err |= copy_to_user(nm, bm, alloc_size); |
| 1218 | } |
| 1219 | |
| 1220 | if (err) |
| 1221 | return -EFAULT; |
| 1222 | |
| 1223 | return sys_set_mempolicy(mode, nm, nr_bits+1); |
| 1224 | } |
| 1225 | |
| 1226 | asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len, |
| 1227 | compat_ulong_t mode, compat_ulong_t __user *nmask, |
| 1228 | compat_ulong_t maxnode, compat_ulong_t flags) |
| 1229 | { |
| 1230 | long err = 0; |
| 1231 | unsigned long __user *nm = NULL; |
| 1232 | unsigned long nr_bits, alloc_size; |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1233 | nodemask_t bm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); |
| 1236 | alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; |
| 1237 | |
| 1238 | if (nmask) { |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1239 | err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | nm = compat_alloc_user_space(alloc_size); |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1241 | err |= copy_to_user(nm, nodes_addr(bm), alloc_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | if (err) |
| 1245 | return -EFAULT; |
| 1246 | |
| 1247 | return sys_mbind(start, len, mode, nm, nr_bits+1, flags); |
| 1248 | } |
| 1249 | |
| 1250 | #endif |
| 1251 | |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1252 | /* |
| 1253 | * get_vma_policy(@task, @vma, @addr) |
| 1254 | * @task - task for fallback if vma policy == default |
| 1255 | * @vma - virtual memory area whose policy is sought |
| 1256 | * @addr - address in @vma for shared policy lookup |
| 1257 | * |
| 1258 | * Returns effective policy for a VMA at specified address. |
| 1259 | * Falls back to @task or system default policy, as necessary. |
| 1260 | * Returned policy has extra reference count if shared, vma, |
| 1261 | * or some other task's policy [show_numa_maps() can pass |
| 1262 | * @task != current]. It is the caller's responsibility to |
| 1263 | * free the reference in these cases. |
| 1264 | */ |
Christoph Lameter | 48fce34 | 2006-01-08 01:01:03 -0800 | [diff] [blame] | 1265 | static struct mempolicy * get_vma_policy(struct task_struct *task, |
| 1266 | struct vm_area_struct *vma, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | { |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1268 | struct mempolicy *pol = task->mempolicy; |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1269 | int shared_pol = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | if (vma) { |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1272 | if (vma->vm_ops && vma->vm_ops->get_policy) { |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1273 | pol = vma->vm_ops->get_policy(vma, addr); |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1274 | shared_pol = 1; /* if pol non-NULL, add ref below */ |
| 1275 | } else if (vma->vm_policy && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | vma->vm_policy->policy != MPOL_DEFAULT) |
| 1277 | pol = vma->vm_policy; |
| 1278 | } |
| 1279 | if (!pol) |
| 1280 | pol = &default_policy; |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1281 | else if (!shared_pol && pol != current->mempolicy) |
| 1282 | mpol_get(pol); /* vma or other task's policy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | return pol; |
| 1284 | } |
| 1285 | |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1286 | /* Return a nodemask representing a mempolicy */ |
| 1287 | static nodemask_t *nodemask_policy(gfp_t gfp, struct mempolicy *policy) |
| 1288 | { |
| 1289 | /* Lower zones don't get a nodemask applied for MPOL_BIND */ |
| 1290 | if (unlikely(policy->policy == MPOL_BIND) && |
| 1291 | gfp_zone(gfp) >= policy_zone && |
| 1292 | cpuset_nodemask_valid_mems_allowed(&policy->v.nodes)) |
| 1293 | return &policy->v.nodes; |
| 1294 | |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | /* Return a zonelist representing a mempolicy */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1299 | static struct zonelist *zonelist_policy(gfp_t gfp, struct mempolicy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | { |
| 1301 | int nd; |
| 1302 | |
| 1303 | switch (policy->policy) { |
| 1304 | case MPOL_PREFERRED: |
| 1305 | nd = policy->v.preferred_node; |
| 1306 | if (nd < 0) |
| 1307 | nd = numa_node_id(); |
| 1308 | break; |
| 1309 | case MPOL_BIND: |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * Normally, MPOL_BIND allocations node-local are node-local |
| 1312 | * within the allowed nodemask. However, if __GFP_THISNODE is |
| 1313 | * set and the current node is part of the mask, we use the |
| 1314 | * the zonelist for the first node in the mask instead. |
| 1315 | */ |
| 1316 | nd = numa_node_id(); |
| 1317 | if (unlikely(gfp & __GFP_THISNODE) && |
| 1318 | unlikely(!node_isset(nd, policy->v.nodes))) |
| 1319 | nd = first_node(policy->v.nodes); |
| 1320 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | case MPOL_INTERLEAVE: /* should not happen */ |
| 1322 | case MPOL_DEFAULT: |
| 1323 | nd = numa_node_id(); |
| 1324 | break; |
| 1325 | default: |
| 1326 | nd = 0; |
| 1327 | BUG(); |
| 1328 | } |
Mel Gorman | 0e88460 | 2008-04-28 02:12:14 -0700 | [diff] [blame] | 1329 | return node_zonelist(nd, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* Do dynamic interleaving for a process */ |
| 1333 | static unsigned interleave_nodes(struct mempolicy *policy) |
| 1334 | { |
| 1335 | unsigned nid, next; |
| 1336 | struct task_struct *me = current; |
| 1337 | |
| 1338 | nid = me->il_next; |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1339 | next = next_node(nid, policy->v.nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | if (next >= MAX_NUMNODES) |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1341 | next = first_node(policy->v.nodes); |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1342 | if (next < MAX_NUMNODES) |
| 1343 | me->il_next = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | return nid; |
| 1345 | } |
| 1346 | |
Christoph Lameter | dc85da1 | 2006-01-18 17:42:36 -0800 | [diff] [blame] | 1347 | /* |
| 1348 | * Depending on the memory policy provide a node from which to allocate the |
| 1349 | * next slab entry. |
| 1350 | */ |
| 1351 | unsigned slab_node(struct mempolicy *policy) |
| 1352 | { |
David Rientjes | a3b51e0 | 2008-04-28 02:12:23 -0700 | [diff] [blame] | 1353 | unsigned short pol = policy ? policy->policy : MPOL_DEFAULT; |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 1354 | |
| 1355 | switch (pol) { |
Christoph Lameter | dc85da1 | 2006-01-18 17:42:36 -0800 | [diff] [blame] | 1356 | case MPOL_INTERLEAVE: |
| 1357 | return interleave_nodes(policy); |
| 1358 | |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1359 | case MPOL_BIND: { |
Christoph Lameter | dc85da1 | 2006-01-18 17:42:36 -0800 | [diff] [blame] | 1360 | /* |
| 1361 | * Follow bind policy behavior and start allocation at the |
| 1362 | * first node. |
| 1363 | */ |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1364 | struct zonelist *zonelist; |
| 1365 | struct zone *zone; |
| 1366 | enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL); |
| 1367 | zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0]; |
| 1368 | (void)first_zones_zonelist(zonelist, highest_zoneidx, |
| 1369 | &policy->v.nodes, |
| 1370 | &zone); |
| 1371 | return zone->node; |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1372 | } |
Christoph Lameter | dc85da1 | 2006-01-18 17:42:36 -0800 | [diff] [blame] | 1373 | |
| 1374 | case MPOL_PREFERRED: |
| 1375 | if (policy->v.preferred_node >= 0) |
| 1376 | return policy->v.preferred_node; |
| 1377 | /* Fall through */ |
| 1378 | |
| 1379 | default: |
| 1380 | return numa_node_id(); |
| 1381 | } |
| 1382 | } |
| 1383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | /* Do static interleaving for a VMA with known offset. */ |
| 1385 | static unsigned offset_il_node(struct mempolicy *pol, |
| 1386 | struct vm_area_struct *vma, unsigned long off) |
| 1387 | { |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1388 | unsigned nnodes = nodes_weight(pol->v.nodes); |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1389 | unsigned target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | int c; |
| 1391 | int nid = -1; |
| 1392 | |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1393 | if (!nnodes) |
| 1394 | return numa_node_id(); |
| 1395 | target = (unsigned int)off % nnodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | c = 0; |
| 1397 | do { |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1398 | nid = next_node(nid, pol->v.nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | c++; |
| 1400 | } while (c <= target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | return nid; |
| 1402 | } |
| 1403 | |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1404 | /* Determine a node number for interleave */ |
| 1405 | static inline unsigned interleave_nid(struct mempolicy *pol, |
| 1406 | struct vm_area_struct *vma, unsigned long addr, int shift) |
| 1407 | { |
| 1408 | if (vma) { |
| 1409 | unsigned long off; |
| 1410 | |
Nishanth Aravamudan | 3b98b08 | 2006-08-31 21:27:53 -0700 | [diff] [blame] | 1411 | /* |
| 1412 | * for small pages, there is no difference between |
| 1413 | * shift and PAGE_SHIFT, so the bit-shift is safe. |
| 1414 | * for huge pages, since vm_pgoff is in units of small |
| 1415 | * pages, we need to shift off the always 0 bits to get |
| 1416 | * a useful offset. |
| 1417 | */ |
| 1418 | BUG_ON(shift < PAGE_SHIFT); |
| 1419 | off = vma->vm_pgoff >> (shift - PAGE_SHIFT); |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1420 | off += (addr - vma->vm_start) >> shift; |
| 1421 | return offset_il_node(pol, vma, off); |
| 1422 | } else |
| 1423 | return interleave_nodes(pol); |
| 1424 | } |
| 1425 | |
Chen, Kenneth W | 00ac59a | 2006-02-03 21:51:14 +0100 | [diff] [blame] | 1426 | #ifdef CONFIG_HUGETLBFS |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1427 | /* |
| 1428 | * huge_zonelist(@vma, @addr, @gfp_flags, @mpol) |
| 1429 | * @vma = virtual memory area whose policy is sought |
| 1430 | * @addr = address in @vma for shared policy lookup and interleave policy |
| 1431 | * @gfp_flags = for requested zone |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1432 | * @mpol = pointer to mempolicy pointer for reference counted mempolicy |
| 1433 | * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1434 | * |
| 1435 | * Returns a zonelist suitable for a huge page allocation. |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1436 | * If the effective policy is 'BIND, returns pointer to local node's zonelist, |
| 1437 | * and a pointer to the mempolicy's @nodemask for filtering the zonelist. |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1438 | * If it is also a policy for which get_vma_policy() returns an extra |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1439 | * reference, we must hold that reference until after the allocation. |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1440 | * In that case, return policy via @mpol so hugetlb allocation can drop |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1441 | * the reference. For non-'BIND referenced policies, we can/do drop the |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1442 | * reference here, so the caller doesn't need to know about the special case |
| 1443 | * for default and current task policy. |
| 1444 | */ |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 1445 | struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr, |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1446 | gfp_t gfp_flags, struct mempolicy **mpol, |
| 1447 | nodemask_t **nodemask) |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1448 | { |
| 1449 | struct mempolicy *pol = get_vma_policy(current, vma, addr); |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1450 | struct zonelist *zl; |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1451 | |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1452 | *mpol = NULL; /* probably no unref needed */ |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1453 | *nodemask = NULL; /* assume !MPOL_BIND */ |
| 1454 | if (pol->policy == MPOL_BIND) { |
| 1455 | *nodemask = &pol->v.nodes; |
| 1456 | } else if (pol->policy == MPOL_INTERLEAVE) { |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1457 | unsigned nid; |
| 1458 | |
| 1459 | nid = interleave_nid(pol, vma, addr, HPAGE_SHIFT); |
Lee Schermerhorn | 69682d8 | 2008-03-10 11:43:45 -0700 | [diff] [blame] | 1460 | if (unlikely(pol != &default_policy && |
| 1461 | pol != current->mempolicy)) |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1462 | __mpol_put(pol); /* finished with pol */ |
Mel Gorman | 0e88460 | 2008-04-28 02:12:14 -0700 | [diff] [blame] | 1463 | return node_zonelist(nid, gfp_flags); |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1464 | } |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1465 | |
| 1466 | zl = zonelist_policy(GFP_HIGHUSER, pol); |
| 1467 | if (unlikely(pol != &default_policy && pol != current->mempolicy)) { |
| 1468 | if (pol->policy != MPOL_BIND) |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1469 | __mpol_put(pol); /* finished with pol */ |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1470 | else |
| 1471 | *mpol = pol; /* unref needed after allocation */ |
| 1472 | } |
| 1473 | return zl; |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1474 | } |
Chen, Kenneth W | 00ac59a | 2006-02-03 21:51:14 +0100 | [diff] [blame] | 1475 | #endif |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | /* Allocate a page in interleaved policy. |
| 1478 | Own path because it needs to do special accounting. */ |
Andi Kleen | 662f3a0 | 2005-10-29 18:15:49 -0700 | [diff] [blame] | 1479 | static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, |
| 1480 | unsigned nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | { |
| 1482 | struct zonelist *zl; |
| 1483 | struct page *page; |
| 1484 | |
Mel Gorman | 0e88460 | 2008-04-28 02:12:14 -0700 | [diff] [blame] | 1485 | zl = node_zonelist(nid, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | page = __alloc_pages(gfp, order, zl); |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 1487 | if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0])) |
Christoph Lameter | ca889e6 | 2006-06-30 01:55:44 -0700 | [diff] [blame] | 1488 | inc_zone_page_state(page, NUMA_INTERLEAVE_HIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | return page; |
| 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * alloc_page_vma - Allocate a page for a VMA. |
| 1494 | * |
| 1495 | * @gfp: |
| 1496 | * %GFP_USER user allocation. |
| 1497 | * %GFP_KERNEL kernel allocations, |
| 1498 | * %GFP_HIGHMEM highmem/user allocations, |
| 1499 | * %GFP_FS allocation should not call back into a file system. |
| 1500 | * %GFP_ATOMIC don't sleep. |
| 1501 | * |
| 1502 | * @vma: Pointer to VMA or NULL if not available. |
| 1503 | * @addr: Virtual Address of the allocation. Must be inside the VMA. |
| 1504 | * |
| 1505 | * This function allocates a page from the kernel page pool and applies |
| 1506 | * a NUMA policy associated with the VMA or the current process. |
| 1507 | * When VMA is not NULL caller must hold down_read on the mmap_sem of the |
| 1508 | * mm_struct of the VMA to prevent it from going away. Should be used for |
| 1509 | * all allocations for pages that will be mapped into |
| 1510 | * user space. Returns NULL when no page can be allocated. |
| 1511 | * |
| 1512 | * Should be called with the mm_sem of the vma hold. |
| 1513 | */ |
| 1514 | struct page * |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1515 | alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | { |
Christoph Lameter | 6e21c8f | 2005-09-03 15:54:45 -0700 | [diff] [blame] | 1517 | struct mempolicy *pol = get_vma_policy(current, vma, addr); |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1518 | struct zonelist *zl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1520 | cpuset_update_task_memory_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | if (unlikely(pol->policy == MPOL_INTERLEAVE)) { |
| 1523 | unsigned nid; |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 1524 | |
| 1525 | nid = interleave_nid(pol, vma, addr, PAGE_SHIFT); |
Lee Schermerhorn | 69682d8 | 2008-03-10 11:43:45 -0700 | [diff] [blame] | 1526 | if (unlikely(pol != &default_policy && |
| 1527 | pol != current->mempolicy)) |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1528 | __mpol_put(pol); /* finished with pol */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | return alloc_page_interleave(gfp, 0, nid); |
| 1530 | } |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1531 | zl = zonelist_policy(gfp, pol); |
| 1532 | if (pol != &default_policy && pol != current->mempolicy) { |
| 1533 | /* |
| 1534 | * slow path: ref counted policy -- shared or vma |
| 1535 | */ |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1536 | struct page *page = __alloc_pages_nodemask(gfp, 0, |
| 1537 | zl, nodemask_policy(gfp, pol)); |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1538 | __mpol_put(pol); |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 1539 | return page; |
| 1540 | } |
| 1541 | /* |
| 1542 | * fast path: default or task policy |
| 1543 | */ |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1544 | return __alloc_pages_nodemask(gfp, 0, zl, nodemask_policy(gfp, pol)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * alloc_pages_current - Allocate pages. |
| 1549 | * |
| 1550 | * @gfp: |
| 1551 | * %GFP_USER user allocation, |
| 1552 | * %GFP_KERNEL kernel allocation, |
| 1553 | * %GFP_HIGHMEM highmem allocation, |
| 1554 | * %GFP_FS don't call back into a file system. |
| 1555 | * %GFP_ATOMIC don't sleep. |
| 1556 | * @order: Power of two of allocation size in pages. 0 is a single page. |
| 1557 | * |
| 1558 | * Allocate a page from the kernel page pool. When not in |
| 1559 | * interrupt context and apply the current process NUMA policy. |
| 1560 | * Returns NULL when no page can be allocated. |
| 1561 | * |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1562 | * Don't call cpuset_update_task_memory_state() unless |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | * 1) it's ok to take cpuset_sem (can WAIT), and |
| 1564 | * 2) allocating for current task (not interrupt). |
| 1565 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1566 | struct page *alloc_pages_current(gfp_t gfp, unsigned order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | { |
| 1568 | struct mempolicy *pol = current->mempolicy; |
| 1569 | |
| 1570 | if ((gfp & __GFP_WAIT) && !in_interrupt()) |
Paul Jackson | cf2a473c | 2006-01-08 01:01:54 -0800 | [diff] [blame] | 1571 | cpuset_update_task_memory_state(); |
Christoph Lameter | 9b819d2 | 2006-09-25 23:31:40 -0700 | [diff] [blame] | 1572 | if (!pol || in_interrupt() || (gfp & __GFP_THISNODE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | pol = &default_policy; |
| 1574 | if (pol->policy == MPOL_INTERLEAVE) |
| 1575 | return alloc_page_interleave(gfp, order, interleave_nodes(pol)); |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1576 | return __alloc_pages_nodemask(gfp, order, |
| 1577 | zonelist_policy(gfp, pol), nodemask_policy(gfp, pol)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | } |
| 1579 | EXPORT_SYMBOL(alloc_pages_current); |
| 1580 | |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1581 | /* |
Lee Schermerhorn | 846a16b | 2008-04-28 02:13:09 -0700 | [diff] [blame] | 1582 | * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1583 | * rebinds the mempolicy its copying by calling mpol_rebind_policy() |
| 1584 | * with the mems_allowed returned by cpuset_mems_allowed(). This |
| 1585 | * keeps mempolicies cpuset relative after its cpuset moves. See |
| 1586 | * further kernel/cpuset.c update_nodemask(). |
| 1587 | */ |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1588 | |
Lee Schermerhorn | 846a16b | 2008-04-28 02:13:09 -0700 | [diff] [blame] | 1589 | /* Slow path of a mempolicy duplicate */ |
| 1590 | struct mempolicy *__mpol_dup(struct mempolicy *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | { |
| 1592 | struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL); |
| 1593 | |
| 1594 | if (!new) |
| 1595 | return ERR_PTR(-ENOMEM); |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1596 | if (current_cpuset_is_being_rebound()) { |
| 1597 | nodemask_t mems = cpuset_mems_allowed(current); |
| 1598 | mpol_rebind_policy(old, &mems); |
| 1599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | *new = *old; |
| 1601 | atomic_set(&new->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | return new; |
| 1603 | } |
| 1604 | |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1605 | static int mpol_match_intent(const struct mempolicy *a, |
| 1606 | const struct mempolicy *b) |
| 1607 | { |
| 1608 | if (a->flags != b->flags) |
| 1609 | return 0; |
| 1610 | if (!mpol_store_user_nodemask(a)) |
| 1611 | return 1; |
| 1612 | return nodes_equal(a->w.user_nodemask, b->w.user_nodemask); |
| 1613 | } |
| 1614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | /* Slow path of a mempolicy comparison */ |
| 1616 | int __mpol_equal(struct mempolicy *a, struct mempolicy *b) |
| 1617 | { |
| 1618 | if (!a || !b) |
| 1619 | return 0; |
| 1620 | if (a->policy != b->policy) |
| 1621 | return 0; |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1622 | if (a->policy != MPOL_DEFAULT && !mpol_match_intent(a, b)) |
| 1623 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | switch (a->policy) { |
| 1625 | case MPOL_DEFAULT: |
| 1626 | return 1; |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1627 | case MPOL_BIND: |
| 1628 | /* Fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | case MPOL_INTERLEAVE: |
Andi Kleen | dfcd3c0 | 2005-10-29 18:15:48 -0700 | [diff] [blame] | 1630 | return nodes_equal(a->v.nodes, b->v.nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | case MPOL_PREFERRED: |
| 1632 | return a->v.preferred_node == b->v.preferred_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | default: |
| 1634 | BUG(); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /* Slow path of a mpol destructor. */ |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1640 | void __mpol_put(struct mempolicy *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | { |
| 1642 | if (!atomic_dec_and_test(&p->refcnt)) |
| 1643 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | p->policy = MPOL_DEFAULT; |
| 1645 | kmem_cache_free(policy_cache, p); |
| 1646 | } |
| 1647 | |
| 1648 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | * Shared memory backing store policy support. |
| 1650 | * |
| 1651 | * Remember policies even when nobody has shared memory mapped. |
| 1652 | * The policies are kept in Red-Black tree linked from the inode. |
| 1653 | * They are protected by the sp->lock spinlock, which should be held |
| 1654 | * for any accesses to the tree. |
| 1655 | */ |
| 1656 | |
| 1657 | /* lookup first element intersecting start-end */ |
| 1658 | /* Caller holds sp->lock */ |
| 1659 | static struct sp_node * |
| 1660 | sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end) |
| 1661 | { |
| 1662 | struct rb_node *n = sp->root.rb_node; |
| 1663 | |
| 1664 | while (n) { |
| 1665 | struct sp_node *p = rb_entry(n, struct sp_node, nd); |
| 1666 | |
| 1667 | if (start >= p->end) |
| 1668 | n = n->rb_right; |
| 1669 | else if (end <= p->start) |
| 1670 | n = n->rb_left; |
| 1671 | else |
| 1672 | break; |
| 1673 | } |
| 1674 | if (!n) |
| 1675 | return NULL; |
| 1676 | for (;;) { |
| 1677 | struct sp_node *w = NULL; |
| 1678 | struct rb_node *prev = rb_prev(n); |
| 1679 | if (!prev) |
| 1680 | break; |
| 1681 | w = rb_entry(prev, struct sp_node, nd); |
| 1682 | if (w->end <= start) |
| 1683 | break; |
| 1684 | n = prev; |
| 1685 | } |
| 1686 | return rb_entry(n, struct sp_node, nd); |
| 1687 | } |
| 1688 | |
| 1689 | /* Insert a new shared policy into the list. */ |
| 1690 | /* Caller holds sp->lock */ |
| 1691 | static void sp_insert(struct shared_policy *sp, struct sp_node *new) |
| 1692 | { |
| 1693 | struct rb_node **p = &sp->root.rb_node; |
| 1694 | struct rb_node *parent = NULL; |
| 1695 | struct sp_node *nd; |
| 1696 | |
| 1697 | while (*p) { |
| 1698 | parent = *p; |
| 1699 | nd = rb_entry(parent, struct sp_node, nd); |
| 1700 | if (new->start < nd->start) |
| 1701 | p = &(*p)->rb_left; |
| 1702 | else if (new->end > nd->end) |
| 1703 | p = &(*p)->rb_right; |
| 1704 | else |
| 1705 | BUG(); |
| 1706 | } |
| 1707 | rb_link_node(&new->nd, parent, p); |
| 1708 | rb_insert_color(&new->nd, &sp->root); |
Paul Mundt | 140d5a4 | 2007-07-15 23:38:16 -0700 | [diff] [blame] | 1709 | pr_debug("inserting %lx-%lx: %d\n", new->start, new->end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | new->policy ? new->policy->policy : 0); |
| 1711 | } |
| 1712 | |
| 1713 | /* Find shared policy intersecting idx */ |
| 1714 | struct mempolicy * |
| 1715 | mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) |
| 1716 | { |
| 1717 | struct mempolicy *pol = NULL; |
| 1718 | struct sp_node *sn; |
| 1719 | |
| 1720 | if (!sp->root.rb_node) |
| 1721 | return NULL; |
| 1722 | spin_lock(&sp->lock); |
| 1723 | sn = sp_lookup(sp, idx, idx+1); |
| 1724 | if (sn) { |
| 1725 | mpol_get(sn->policy); |
| 1726 | pol = sn->policy; |
| 1727 | } |
| 1728 | spin_unlock(&sp->lock); |
| 1729 | return pol; |
| 1730 | } |
| 1731 | |
| 1732 | static void sp_delete(struct shared_policy *sp, struct sp_node *n) |
| 1733 | { |
Paul Mundt | 140d5a4 | 2007-07-15 23:38:16 -0700 | [diff] [blame] | 1734 | pr_debug("deleting %lx-l%lx\n", n->start, n->end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | rb_erase(&n->nd, &sp->root); |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1736 | mpol_put(n->policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | kmem_cache_free(sn_cache, n); |
| 1738 | } |
| 1739 | |
Adrian Bunk | dbcb0f1 | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 1740 | static struct sp_node *sp_alloc(unsigned long start, unsigned long end, |
| 1741 | struct mempolicy *pol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { |
| 1743 | struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL); |
| 1744 | |
| 1745 | if (!n) |
| 1746 | return NULL; |
| 1747 | n->start = start; |
| 1748 | n->end = end; |
| 1749 | mpol_get(pol); |
| 1750 | n->policy = pol; |
| 1751 | return n; |
| 1752 | } |
| 1753 | |
| 1754 | /* Replace a policy range. */ |
| 1755 | static int shared_policy_replace(struct shared_policy *sp, unsigned long start, |
| 1756 | unsigned long end, struct sp_node *new) |
| 1757 | { |
| 1758 | struct sp_node *n, *new2 = NULL; |
| 1759 | |
| 1760 | restart: |
| 1761 | spin_lock(&sp->lock); |
| 1762 | n = sp_lookup(sp, start, end); |
| 1763 | /* Take care of old policies in the same range. */ |
| 1764 | while (n && n->start < end) { |
| 1765 | struct rb_node *next = rb_next(&n->nd); |
| 1766 | if (n->start >= start) { |
| 1767 | if (n->end <= end) |
| 1768 | sp_delete(sp, n); |
| 1769 | else |
| 1770 | n->start = end; |
| 1771 | } else { |
| 1772 | /* Old policy spanning whole new range. */ |
| 1773 | if (n->end > end) { |
| 1774 | if (!new2) { |
| 1775 | spin_unlock(&sp->lock); |
| 1776 | new2 = sp_alloc(end, n->end, n->policy); |
| 1777 | if (!new2) |
| 1778 | return -ENOMEM; |
| 1779 | goto restart; |
| 1780 | } |
| 1781 | n->end = start; |
| 1782 | sp_insert(sp, new2); |
| 1783 | new2 = NULL; |
| 1784 | break; |
| 1785 | } else |
| 1786 | n->end = start; |
| 1787 | } |
| 1788 | if (!next) |
| 1789 | break; |
| 1790 | n = rb_entry(next, struct sp_node, nd); |
| 1791 | } |
| 1792 | if (new) |
| 1793 | sp_insert(sp, new); |
| 1794 | spin_unlock(&sp->lock); |
| 1795 | if (new2) { |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1796 | mpol_put(new2->policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | kmem_cache_free(sn_cache, new2); |
| 1798 | } |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
David Rientjes | a3b51e0 | 2008-04-28 02:12:23 -0700 | [diff] [blame] | 1802 | void mpol_shared_policy_init(struct shared_policy *info, unsigned short policy, |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1803 | unsigned short flags, nodemask_t *policy_nodes) |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 1804 | { |
| 1805 | info->root = RB_ROOT; |
| 1806 | spin_lock_init(&info->lock); |
| 1807 | |
| 1808 | if (policy != MPOL_DEFAULT) { |
| 1809 | struct mempolicy *newpol; |
| 1810 | |
| 1811 | /* Falls back to MPOL_DEFAULT on any error */ |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1812 | newpol = mpol_new(policy, flags, policy_nodes); |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 1813 | if (!IS_ERR(newpol)) { |
| 1814 | /* Create pseudo-vma that contains just the policy */ |
| 1815 | struct vm_area_struct pvma; |
| 1816 | |
| 1817 | memset(&pvma, 0, sizeof(struct vm_area_struct)); |
| 1818 | /* Policy covers entire file */ |
| 1819 | pvma.vm_end = TASK_SIZE; |
| 1820 | mpol_set_shared_policy(info, &pvma, newpol); |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1821 | mpol_put(newpol); |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | int mpol_set_shared_policy(struct shared_policy *info, |
| 1827 | struct vm_area_struct *vma, struct mempolicy *npol) |
| 1828 | { |
| 1829 | int err; |
| 1830 | struct sp_node *new = NULL; |
| 1831 | unsigned long sz = vma_pages(vma); |
| 1832 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1833 | pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | vma->vm_pgoff, |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1835 | sz, npol ? npol->policy : -1, |
| 1836 | npol ? npol->flags : -1, |
Paul Mundt | 140d5a4 | 2007-07-15 23:38:16 -0700 | [diff] [blame] | 1837 | npol ? nodes_addr(npol->v.nodes)[0] : -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
| 1839 | if (npol) { |
| 1840 | new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); |
| 1841 | if (!new) |
| 1842 | return -ENOMEM; |
| 1843 | } |
| 1844 | err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); |
| 1845 | if (err && new) |
| 1846 | kmem_cache_free(sn_cache, new); |
| 1847 | return err; |
| 1848 | } |
| 1849 | |
| 1850 | /* Free a backing policy store on inode delete. */ |
| 1851 | void mpol_free_shared_policy(struct shared_policy *p) |
| 1852 | { |
| 1853 | struct sp_node *n; |
| 1854 | struct rb_node *next; |
| 1855 | |
| 1856 | if (!p->root.rb_node) |
| 1857 | return; |
| 1858 | spin_lock(&p->lock); |
| 1859 | next = rb_first(&p->root); |
| 1860 | while (next) { |
| 1861 | n = rb_entry(next, struct sp_node, nd); |
| 1862 | next = rb_next(&n->nd); |
Andi Kleen | 90c5029 | 2005-07-27 11:43:50 -0700 | [diff] [blame] | 1863 | rb_erase(&n->nd, &p->root); |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 1864 | mpol_put(n->policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | kmem_cache_free(sn_cache, n); |
| 1866 | } |
| 1867 | spin_unlock(&p->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | /* assumes fs == KERNEL_DS */ |
| 1871 | void __init numa_policy_init(void) |
| 1872 | { |
Paul Mundt | b71636e2 | 2007-07-15 23:38:15 -0700 | [diff] [blame] | 1873 | nodemask_t interleave_nodes; |
| 1874 | unsigned long largest = 0; |
| 1875 | int nid, prefer = 0; |
| 1876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | policy_cache = kmem_cache_create("numa_policy", |
| 1878 | sizeof(struct mempolicy), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1879 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
| 1881 | sn_cache = kmem_cache_create("shared_policy_node", |
| 1882 | sizeof(struct sp_node), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1883 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | |
Paul Mundt | b71636e2 | 2007-07-15 23:38:15 -0700 | [diff] [blame] | 1885 | /* |
| 1886 | * Set interleaving policy for system init. Interleaving is only |
| 1887 | * enabled across suitably sized nodes (default is >= 16MB), or |
| 1888 | * fall back to the largest node if they're all smaller. |
| 1889 | */ |
| 1890 | nodes_clear(interleave_nodes); |
Christoph Lameter | 56bbd65 | 2007-10-16 01:25:35 -0700 | [diff] [blame] | 1891 | for_each_node_state(nid, N_HIGH_MEMORY) { |
Paul Mundt | b71636e2 | 2007-07-15 23:38:15 -0700 | [diff] [blame] | 1892 | unsigned long total_pages = node_present_pages(nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
Paul Mundt | b71636e2 | 2007-07-15 23:38:15 -0700 | [diff] [blame] | 1894 | /* Preserve the largest node */ |
| 1895 | if (largest < total_pages) { |
| 1896 | largest = total_pages; |
| 1897 | prefer = nid; |
| 1898 | } |
| 1899 | |
| 1900 | /* Interleave this node? */ |
| 1901 | if ((total_pages << PAGE_SHIFT) >= (16 << 20)) |
| 1902 | node_set(nid, interleave_nodes); |
| 1903 | } |
| 1904 | |
| 1905 | /* All too small, use the largest */ |
| 1906 | if (unlikely(nodes_empty(interleave_nodes))) |
| 1907 | node_set(prefer, interleave_nodes); |
| 1908 | |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1909 | if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | printk("numa_policy_init: interleaving failed\n"); |
| 1911 | } |
| 1912 | |
Christoph Lameter | 8bccd85 | 2005-10-29 18:16:59 -0700 | [diff] [blame] | 1913 | /* Reset policy of current process to default */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | void numa_default_policy(void) |
| 1915 | { |
David Rientjes | 028fec4 | 2008-04-28 02:12:25 -0700 | [diff] [blame] | 1916 | do_set_mempolicy(MPOL_DEFAULT, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | } |
Paul Jackson | 68860ec | 2005-10-30 15:02:36 -0800 | [diff] [blame] | 1918 | |
Paul Jackson | 4225399 | 2006-01-08 01:01:59 -0800 | [diff] [blame] | 1919 | /* |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1920 | * Display pages allocated per node and memory policy via /proc. |
| 1921 | */ |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 1922 | static const char * const policy_types[] = |
| 1923 | { "default", "prefer", "bind", "interleave" }; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1924 | |
| 1925 | /* |
| 1926 | * Convert a mempolicy into a string. |
| 1927 | * Returns the number of characters in buffer (if positive) |
| 1928 | * or an error (negative) |
| 1929 | */ |
| 1930 | static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) |
| 1931 | { |
| 1932 | char *p = buffer; |
| 1933 | int l; |
| 1934 | nodemask_t nodes; |
David Rientjes | a3b51e0 | 2008-04-28 02:12:23 -0700 | [diff] [blame] | 1935 | unsigned short mode = pol ? pol->policy : MPOL_DEFAULT; |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1936 | unsigned short flags = pol ? pol->flags : 0; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1937 | |
| 1938 | switch (mode) { |
| 1939 | case MPOL_DEFAULT: |
| 1940 | nodes_clear(nodes); |
| 1941 | break; |
| 1942 | |
| 1943 | case MPOL_PREFERRED: |
| 1944 | nodes_clear(nodes); |
| 1945 | node_set(pol->v.preferred_node, nodes); |
| 1946 | break; |
| 1947 | |
| 1948 | case MPOL_BIND: |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 1949 | /* Fall through */ |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1950 | case MPOL_INTERLEAVE: |
| 1951 | nodes = pol->v.nodes; |
| 1952 | break; |
| 1953 | |
| 1954 | default: |
| 1955 | BUG(); |
| 1956 | return -EFAULT; |
| 1957 | } |
| 1958 | |
| 1959 | l = strlen(policy_types[mode]); |
| 1960 | if (buffer + maxlen < p + l + 1) |
| 1961 | return -ENOSPC; |
| 1962 | |
| 1963 | strcpy(p, policy_types[mode]); |
| 1964 | p += l; |
| 1965 | |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1966 | if (flags) { |
| 1967 | int need_bar = 0; |
| 1968 | |
| 1969 | if (buffer + maxlen < p + 2) |
| 1970 | return -ENOSPC; |
| 1971 | *p++ = '='; |
| 1972 | |
| 1973 | if (flags & MPOL_F_STATIC_NODES) |
| 1974 | p += sprintf(p, "%sstatic", need_bar++ ? "|" : ""); |
David Rientjes | 4c50bc0 | 2008-04-28 02:12:30 -0700 | [diff] [blame] | 1975 | if (flags & MPOL_F_RELATIVE_NODES) |
| 1976 | p += sprintf(p, "%srelative", need_bar++ ? "|" : ""); |
David Rientjes | f5b087b | 2008-04-28 02:12:27 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1979 | if (!nodes_empty(nodes)) { |
| 1980 | if (buffer + maxlen < p + 2) |
| 1981 | return -ENOSPC; |
| 1982 | *p++ = '='; |
| 1983 | p += nodelist_scnprintf(p, buffer + maxlen - p, nodes); |
| 1984 | } |
| 1985 | return p - buffer; |
| 1986 | } |
| 1987 | |
| 1988 | struct numa_maps { |
| 1989 | unsigned long pages; |
| 1990 | unsigned long anon; |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 1991 | unsigned long active; |
| 1992 | unsigned long writeback; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1993 | unsigned long mapcount_max; |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 1994 | unsigned long dirty; |
| 1995 | unsigned long swapcache; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 1996 | unsigned long node[MAX_NUMNODES]; |
| 1997 | }; |
| 1998 | |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 1999 | static void gather_stats(struct page *page, void *private, int pte_dirty) |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2000 | { |
| 2001 | struct numa_maps *md = private; |
| 2002 | int count = page_mapcount(page); |
| 2003 | |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2004 | md->pages++; |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2005 | if (pte_dirty || PageDirty(page)) |
| 2006 | md->dirty++; |
| 2007 | |
| 2008 | if (PageSwapCache(page)) |
| 2009 | md->swapcache++; |
| 2010 | |
| 2011 | if (PageActive(page)) |
| 2012 | md->active++; |
| 2013 | |
| 2014 | if (PageWriteback(page)) |
| 2015 | md->writeback++; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2016 | |
| 2017 | if (PageAnon(page)) |
| 2018 | md->anon++; |
| 2019 | |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2020 | if (count > md->mapcount_max) |
| 2021 | md->mapcount_max = count; |
| 2022 | |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2023 | md->node[page_to_nid(page)]++; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
Andrew Morton | 7f709ed | 2006-03-07 21:55:22 -0800 | [diff] [blame] | 2026 | #ifdef CONFIG_HUGETLB_PAGE |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2027 | static void check_huge_range(struct vm_area_struct *vma, |
| 2028 | unsigned long start, unsigned long end, |
| 2029 | struct numa_maps *md) |
| 2030 | { |
| 2031 | unsigned long addr; |
| 2032 | struct page *page; |
| 2033 | |
| 2034 | for (addr = start; addr < end; addr += HPAGE_SIZE) { |
| 2035 | pte_t *ptep = huge_pte_offset(vma->vm_mm, addr & HPAGE_MASK); |
| 2036 | pte_t pte; |
| 2037 | |
| 2038 | if (!ptep) |
| 2039 | continue; |
| 2040 | |
| 2041 | pte = *ptep; |
| 2042 | if (pte_none(pte)) |
| 2043 | continue; |
| 2044 | |
| 2045 | page = pte_page(pte); |
| 2046 | if (!page) |
| 2047 | continue; |
| 2048 | |
| 2049 | gather_stats(page, md, pte_dirty(*ptep)); |
| 2050 | } |
| 2051 | } |
Andrew Morton | 7f709ed | 2006-03-07 21:55:22 -0800 | [diff] [blame] | 2052 | #else |
| 2053 | static inline void check_huge_range(struct vm_area_struct *vma, |
| 2054 | unsigned long start, unsigned long end, |
| 2055 | struct numa_maps *md) |
| 2056 | { |
| 2057 | } |
| 2058 | #endif |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2059 | |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2060 | int show_numa_map(struct seq_file *m, void *v) |
| 2061 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2062 | struct proc_maps_private *priv = m->private; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2063 | struct vm_area_struct *vma = v; |
| 2064 | struct numa_maps *md; |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2065 | struct file *file = vma->vm_file; |
| 2066 | struct mm_struct *mm = vma->vm_mm; |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 2067 | struct mempolicy *pol; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2068 | int n; |
| 2069 | char buffer[50]; |
| 2070 | |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2071 | if (!mm) |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2072 | return 0; |
| 2073 | |
| 2074 | md = kzalloc(sizeof(struct numa_maps), GFP_KERNEL); |
| 2075 | if (!md) |
| 2076 | return 0; |
| 2077 | |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 2078 | pol = get_vma_policy(priv->task, vma, vma->vm_start); |
| 2079 | mpol_to_str(buffer, sizeof(buffer), pol); |
| 2080 | /* |
| 2081 | * unref shared or other task's mempolicy |
| 2082 | */ |
| 2083 | if (pol != &default_policy && pol != current->mempolicy) |
Lee Schermerhorn | f0be3d3 | 2008-04-28 02:13:08 -0700 | [diff] [blame] | 2084 | __mpol_put(pol); |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2085 | |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2086 | seq_printf(m, "%08lx %s", vma->vm_start, buffer); |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2087 | |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2088 | if (file) { |
| 2089 | seq_printf(m, " file="); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 2090 | seq_path(m, &file->f_path, "\n\t= "); |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2091 | } else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { |
| 2092 | seq_printf(m, " heap"); |
| 2093 | } else if (vma->vm_start <= mm->start_stack && |
| 2094 | vma->vm_end >= mm->start_stack) { |
| 2095 | seq_printf(m, " stack"); |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2096 | } |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2097 | |
| 2098 | if (is_vm_hugetlb_page(vma)) { |
| 2099 | check_huge_range(vma, vma->vm_start, vma->vm_end, md); |
| 2100 | seq_printf(m, " huge"); |
| 2101 | } else { |
| 2102 | check_pgd_range(vma, vma->vm_start, vma->vm_end, |
Christoph Lameter | 56bbd65 | 2007-10-16 01:25:35 -0700 | [diff] [blame] | 2103 | &node_states[N_HIGH_MEMORY], MPOL_MF_STATS, md); |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | if (!md->pages) |
| 2107 | goto out; |
| 2108 | |
| 2109 | if (md->anon) |
| 2110 | seq_printf(m," anon=%lu",md->anon); |
| 2111 | |
| 2112 | if (md->dirty) |
| 2113 | seq_printf(m," dirty=%lu",md->dirty); |
| 2114 | |
| 2115 | if (md->pages != md->anon && md->pages != md->dirty) |
| 2116 | seq_printf(m, " mapped=%lu", md->pages); |
| 2117 | |
| 2118 | if (md->mapcount_max > 1) |
| 2119 | seq_printf(m, " mapmax=%lu", md->mapcount_max); |
| 2120 | |
| 2121 | if (md->swapcache) |
| 2122 | seq_printf(m," swapcache=%lu", md->swapcache); |
| 2123 | |
| 2124 | if (md->active < md->pages && !is_vm_hugetlb_page(vma)) |
| 2125 | seq_printf(m," active=%lu", md->active); |
| 2126 | |
| 2127 | if (md->writeback) |
| 2128 | seq_printf(m," writeback=%lu", md->writeback); |
| 2129 | |
Christoph Lameter | 56bbd65 | 2007-10-16 01:25:35 -0700 | [diff] [blame] | 2130 | for_each_node_state(n, N_HIGH_MEMORY) |
Christoph Lameter | 397874d | 2006-03-06 15:42:53 -0800 | [diff] [blame] | 2131 | if (md->node[n]) |
| 2132 | seq_printf(m, " N%d=%lu", n, md->node[n]); |
| 2133 | out: |
| 2134 | seq_putc(m, '\n'); |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2135 | kfree(md); |
| 2136 | |
| 2137 | if (m->count < m->size) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2138 | m->version = (vma != priv->tail_vma) ? vma->vm_start : 0; |
Christoph Lameter | 1a75a6c | 2006-01-08 01:01:02 -0800 | [diff] [blame] | 2139 | return 0; |
| 2140 | } |