mm: convert some level-less printks to pr_*
[deliverable/linux.git] / mm / mempolicy.c
CommitLineData
1da177e4
LT
1/*
2 * Simple NUMA memory policy for the Linux kernel.
3 *
4 * Copyright 2003,2004 Andi Kleen, SuSE Labs.
8bccd85f 5 * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
1da177e4
LT
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.
8bccd85f 21 *
1da177e4
LT
22 * bind Only allocate memory on a specific set of nodes,
23 * no fallback.
8bccd85f
CL
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 *
1da177e4 28 * preferred Try a specific node first before normal fallback.
00ef2d2f 29 * As a special case NUMA_NO_NODE here means do the allocation
1da177e4
LT
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.
8bccd85f 33 *
1da177e4
LT
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.
1da177e4
LT
66*/
67
b1de0d13
MH
68#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
69
1da177e4
LT
70#include <linux/mempolicy.h>
71#include <linux/mm.h>
72#include <linux/highmem.h>
73#include <linux/hugetlb.h>
74#include <linux/kernel.h>
75#include <linux/sched.h>
1da177e4
LT
76#include <linux/nodemask.h>
77#include <linux/cpuset.h>
1da177e4
LT
78#include <linux/slab.h>
79#include <linux/string.h>
b95f1b31 80#include <linux/export.h>
b488893a 81#include <linux/nsproxy.h>
1da177e4
LT
82#include <linux/interrupt.h>
83#include <linux/init.h>
84#include <linux/compat.h>
dc9aa5b9 85#include <linux/swap.h>
1a75a6c8
CL
86#include <linux/seq_file.h>
87#include <linux/proc_fs.h>
b20a3503 88#include <linux/migrate.h>
62b61f61 89#include <linux/ksm.h>
95a402c3 90#include <linux/rmap.h>
86c3a764 91#include <linux/security.h>
dbcb0f19 92#include <linux/syscalls.h>
095f1fc4 93#include <linux/ctype.h>
6d9c285a 94#include <linux/mm_inline.h>
b24f53a0 95#include <linux/mmu_notifier.h>
b1de0d13 96#include <linux/printk.h>
dc9aa5b9 97
1da177e4
LT
98#include <asm/tlbflush.h>
99#include <asm/uaccess.h>
778d3b0f 100#include <linux/random.h>
1da177e4 101
62695a84
NP
102#include "internal.h"
103
38e35860 104/* Internal flags */
dc9aa5b9 105#define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */
38e35860 106#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */
dc9aa5b9 107
fcc234f8
PE
108static struct kmem_cache *policy_cache;
109static struct kmem_cache *sn_cache;
1da177e4 110
1da177e4
LT
111/* Highest zone. An specific allocation for a zone below that is not
112 policied. */
6267276f 113enum zone_type policy_zone = 0;
1da177e4 114
bea904d5
LS
115/*
116 * run-time system-wide default policy => local allocation
117 */
e754d79d 118static struct mempolicy default_policy = {
1da177e4 119 .refcnt = ATOMIC_INIT(1), /* never free it */
bea904d5 120 .mode = MPOL_PREFERRED,
fc36b8d3 121 .flags = MPOL_F_LOCAL,
1da177e4
LT
122};
123
5606e387
MG
124static struct mempolicy preferred_node_policy[MAX_NUMNODES];
125
126static struct mempolicy *get_task_policy(struct task_struct *p)
127{
128 struct mempolicy *pol = p->mempolicy;
5606e387
MG
129
130 if (!pol) {
1da6f0e1 131 int node = numa_node_id();
5606e387 132
1da6f0e1
JW
133 if (node != NUMA_NO_NODE) {
134 pol = &preferred_node_policy[node];
135 /*
136 * preferred_node_policy is not initialised early in
137 * boot
138 */
139 if (!pol->mode)
140 pol = NULL;
141 }
5606e387
MG
142 }
143
144 return pol;
145}
146
37012946
DR
147static const struct mempolicy_operations {
148 int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
708c1bbc
MX
149 /*
150 * If read-side task has no lock to protect task->mempolicy, write-side
151 * task will rebind the task->mempolicy by two step. The first step is
152 * setting all the newly nodes, and the second step is cleaning all the
153 * disallowed nodes. In this way, we can avoid finding no node to alloc
154 * page.
155 * If we have a lock to protect task->mempolicy in read-side, we do
156 * rebind directly.
157 *
158 * step:
159 * MPOL_REBIND_ONCE - do rebind work at once
160 * MPOL_REBIND_STEP1 - set all the newly nodes
161 * MPOL_REBIND_STEP2 - clean all the disallowed nodes
162 */
163 void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes,
164 enum mpol_rebind_step step);
37012946
DR
165} mpol_ops[MPOL_MAX];
166
19770b32 167/* Check that the nodemask contains at least one populated zone */
37012946 168static int is_valid_nodemask(const nodemask_t *nodemask)
1da177e4 169{
d3eb1570 170 return nodes_intersects(*nodemask, node_states[N_MEMORY]);
1da177e4
LT
171}
172
f5b087b5
DR
173static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
174{
6d556294 175 return pol->flags & MPOL_MODE_FLAGS;
4c50bc01
DR
176}
177
178static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
179 const nodemask_t *rel)
180{
181 nodemask_t tmp;
182 nodes_fold(tmp, *orig, nodes_weight(*rel));
183 nodes_onto(*ret, tmp, *rel);
f5b087b5
DR
184}
185
37012946
DR
186static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
187{
188 if (nodes_empty(*nodes))
189 return -EINVAL;
190 pol->v.nodes = *nodes;
191 return 0;
192}
193
194static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
195{
196 if (!nodes)
fc36b8d3 197 pol->flags |= MPOL_F_LOCAL; /* local allocation */
37012946
DR
198 else if (nodes_empty(*nodes))
199 return -EINVAL; /* no allowed nodes */
200 else
201 pol->v.preferred_node = first_node(*nodes);
202 return 0;
203}
204
205static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
206{
207 if (!is_valid_nodemask(nodes))
208 return -EINVAL;
209 pol->v.nodes = *nodes;
210 return 0;
211}
212
58568d2a
MX
213/*
214 * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
215 * any, for the new policy. mpol_new() has already validated the nodes
216 * parameter with respect to the policy mode and flags. But, we need to
217 * handle an empty nodemask with MPOL_PREFERRED here.
218 *
219 * Must be called holding task's alloc_lock to protect task's mems_allowed
220 * and mempolicy. May also be called holding the mmap_semaphore for write.
221 */
4bfc4495
KH
222static int mpol_set_nodemask(struct mempolicy *pol,
223 const nodemask_t *nodes, struct nodemask_scratch *nsc)
58568d2a 224{
58568d2a
MX
225 int ret;
226
227 /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
228 if (pol == NULL)
229 return 0;
01f13bd6 230 /* Check N_MEMORY */
4bfc4495 231 nodes_and(nsc->mask1,
01f13bd6 232 cpuset_current_mems_allowed, node_states[N_MEMORY]);
58568d2a
MX
233
234 VM_BUG_ON(!nodes);
235 if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
236 nodes = NULL; /* explicit local allocation */
237 else {
238 if (pol->flags & MPOL_F_RELATIVE_NODES)
4bfc4495 239 mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1);
58568d2a 240 else
4bfc4495
KH
241 nodes_and(nsc->mask2, *nodes, nsc->mask1);
242
58568d2a
MX
243 if (mpol_store_user_nodemask(pol))
244 pol->w.user_nodemask = *nodes;
245 else
246 pol->w.cpuset_mems_allowed =
247 cpuset_current_mems_allowed;
248 }
249
4bfc4495
KH
250 if (nodes)
251 ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
252 else
253 ret = mpol_ops[pol->mode].create(pol, NULL);
58568d2a
MX
254 return ret;
255}
256
257/*
258 * This function just creates a new policy, does some check and simple
259 * initialization. You must invoke mpol_set_nodemask() to set nodes.
260 */
028fec41
DR
261static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
262 nodemask_t *nodes)
1da177e4
LT
263{
264 struct mempolicy *policy;
265
028fec41 266 pr_debug("setting mode %d flags %d nodes[0] %lx\n",
00ef2d2f 267 mode, flags, nodes ? nodes_addr(*nodes)[0] : NUMA_NO_NODE);
140d5a49 268
3e1f0645
DR
269 if (mode == MPOL_DEFAULT) {
270 if (nodes && !nodes_empty(*nodes))
37012946 271 return ERR_PTR(-EINVAL);
d3a71033 272 return NULL;
37012946 273 }
3e1f0645
DR
274 VM_BUG_ON(!nodes);
275
276 /*
277 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
278 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
279 * All other modes require a valid pointer to a non-empty nodemask.
280 */
281 if (mode == MPOL_PREFERRED) {
282 if (nodes_empty(*nodes)) {
283 if (((flags & MPOL_F_STATIC_NODES) ||
284 (flags & MPOL_F_RELATIVE_NODES)))
285 return ERR_PTR(-EINVAL);
3e1f0645 286 }
479e2802
PZ
287 } else if (mode == MPOL_LOCAL) {
288 if (!nodes_empty(*nodes))
289 return ERR_PTR(-EINVAL);
290 mode = MPOL_PREFERRED;
3e1f0645
DR
291 } else if (nodes_empty(*nodes))
292 return ERR_PTR(-EINVAL);
1da177e4
LT
293 policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
294 if (!policy)
295 return ERR_PTR(-ENOMEM);
296 atomic_set(&policy->refcnt, 1);
45c4745a 297 policy->mode = mode;
3e1f0645 298 policy->flags = flags;
37012946 299
1da177e4 300 return policy;
37012946
DR
301}
302
52cd3b07
LS
303/* Slow path of a mpol destructor. */
304void __mpol_put(struct mempolicy *p)
305{
306 if (!atomic_dec_and_test(&p->refcnt))
307 return;
52cd3b07
LS
308 kmem_cache_free(policy_cache, p);
309}
310
708c1bbc
MX
311static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes,
312 enum mpol_rebind_step step)
37012946
DR
313{
314}
315
708c1bbc
MX
316/*
317 * step:
318 * MPOL_REBIND_ONCE - do rebind work at once
319 * MPOL_REBIND_STEP1 - set all the newly nodes
320 * MPOL_REBIND_STEP2 - clean all the disallowed nodes
321 */
322static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes,
323 enum mpol_rebind_step step)
37012946
DR
324{
325 nodemask_t tmp;
326
327 if (pol->flags & MPOL_F_STATIC_NODES)
328 nodes_and(tmp, pol->w.user_nodemask, *nodes);
329 else if (pol->flags & MPOL_F_RELATIVE_NODES)
330 mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
331 else {
708c1bbc
MX
332 /*
333 * if step == 1, we use ->w.cpuset_mems_allowed to cache the
334 * result
335 */
336 if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) {
337 nodes_remap(tmp, pol->v.nodes,
338 pol->w.cpuset_mems_allowed, *nodes);
339 pol->w.cpuset_mems_allowed = step ? tmp : *nodes;
340 } else if (step == MPOL_REBIND_STEP2) {
341 tmp = pol->w.cpuset_mems_allowed;
342 pol->w.cpuset_mems_allowed = *nodes;
343 } else
344 BUG();
37012946 345 }
f5b087b5 346
708c1bbc
MX
347 if (nodes_empty(tmp))
348 tmp = *nodes;
349
350 if (step == MPOL_REBIND_STEP1)
351 nodes_or(pol->v.nodes, pol->v.nodes, tmp);
352 else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2)
353 pol->v.nodes = tmp;
354 else
355 BUG();
356
37012946
DR
357 if (!node_isset(current->il_next, tmp)) {
358 current->il_next = next_node(current->il_next, tmp);
359 if (current->il_next >= MAX_NUMNODES)
360 current->il_next = first_node(tmp);
361 if (current->il_next >= MAX_NUMNODES)
362 current->il_next = numa_node_id();
363 }
364}
365
366static void mpol_rebind_preferred(struct mempolicy *pol,
708c1bbc
MX
367 const nodemask_t *nodes,
368 enum mpol_rebind_step step)
37012946
DR
369{
370 nodemask_t tmp;
371
37012946
DR
372 if (pol->flags & MPOL_F_STATIC_NODES) {
373 int node = first_node(pol->w.user_nodemask);
374
fc36b8d3 375 if (node_isset(node, *nodes)) {
37012946 376 pol->v.preferred_node = node;
fc36b8d3
LS
377 pol->flags &= ~MPOL_F_LOCAL;
378 } else
379 pol->flags |= MPOL_F_LOCAL;
37012946
DR
380 } else if (pol->flags & MPOL_F_RELATIVE_NODES) {
381 mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
382 pol->v.preferred_node = first_node(tmp);
fc36b8d3 383 } else if (!(pol->flags & MPOL_F_LOCAL)) {
37012946
DR
384 pol->v.preferred_node = node_remap(pol->v.preferred_node,
385 pol->w.cpuset_mems_allowed,
386 *nodes);
387 pol->w.cpuset_mems_allowed = *nodes;
388 }
1da177e4
LT
389}
390
708c1bbc
MX
391/*
392 * mpol_rebind_policy - Migrate a policy to a different set of nodes
393 *
394 * If read-side task has no lock to protect task->mempolicy, write-side
395 * task will rebind the task->mempolicy by two step. The first step is
396 * setting all the newly nodes, and the second step is cleaning all the
397 * disallowed nodes. In this way, we can avoid finding no node to alloc
398 * page.
399 * If we have a lock to protect task->mempolicy in read-side, we do
400 * rebind directly.
401 *
402 * step:
403 * MPOL_REBIND_ONCE - do rebind work at once
404 * MPOL_REBIND_STEP1 - set all the newly nodes
405 * MPOL_REBIND_STEP2 - clean all the disallowed nodes
406 */
407static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask,
408 enum mpol_rebind_step step)
1d0d2680 409{
1d0d2680
DR
410 if (!pol)
411 return;
89c522c7 412 if (!mpol_store_user_nodemask(pol) && step == MPOL_REBIND_ONCE &&
1d0d2680
DR
413 nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
414 return;
708c1bbc
MX
415
416 if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING))
417 return;
418
419 if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING))
420 BUG();
421
422 if (step == MPOL_REBIND_STEP1)
423 pol->flags |= MPOL_F_REBINDING;
424 else if (step == MPOL_REBIND_STEP2)
425 pol->flags &= ~MPOL_F_REBINDING;
426 else if (step >= MPOL_REBIND_NSTEP)
427 BUG();
428
429 mpol_ops[pol->mode].rebind(pol, newmask, step);
1d0d2680
DR
430}
431
432/*
433 * Wrapper for mpol_rebind_policy() that just requires task
434 * pointer, and updates task mempolicy.
58568d2a
MX
435 *
436 * Called with task's alloc_lock held.
1d0d2680
DR
437 */
438
708c1bbc
MX
439void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
440 enum mpol_rebind_step step)
1d0d2680 441{
708c1bbc 442 mpol_rebind_policy(tsk->mempolicy, new, step);
1d0d2680
DR
443}
444
445/*
446 * Rebind each vma in mm to new nodemask.
447 *
448 * Call holding a reference to mm. Takes mm->mmap_sem during call.
449 */
450
451void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
452{
453 struct vm_area_struct *vma;
454
455 down_write(&mm->mmap_sem);
456 for (vma = mm->mmap; vma; vma = vma->vm_next)
708c1bbc 457 mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE);
1d0d2680
DR
458 up_write(&mm->mmap_sem);
459}
460
37012946
DR
461static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
462 [MPOL_DEFAULT] = {
463 .rebind = mpol_rebind_default,
464 },
465 [MPOL_INTERLEAVE] = {
466 .create = mpol_new_interleave,
467 .rebind = mpol_rebind_nodemask,
468 },
469 [MPOL_PREFERRED] = {
470 .create = mpol_new_preferred,
471 .rebind = mpol_rebind_preferred,
472 },
473 [MPOL_BIND] = {
474 .create = mpol_new_bind,
475 .rebind = mpol_rebind_nodemask,
476 },
477};
478
fc301289
CL
479static void migrate_page_add(struct page *page, struct list_head *pagelist,
480 unsigned long flags);
1a75a6c8 481
98094945
NH
482/*
483 * Scan through pages checking if pages follow certain conditions,
484 * and move them to the pagelist if they do.
485 */
486static int queue_pages_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
dc9aa5b9
CL
487 unsigned long addr, unsigned long end,
488 const nodemask_t *nodes, unsigned long flags,
38e35860 489 void *private)
1da177e4 490{
91612e0d
HD
491 pte_t *orig_pte;
492 pte_t *pte;
705e87c0 493 spinlock_t *ptl;
941150a3 494
705e87c0 495 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
91612e0d 496 do {
6aab341e 497 struct page *page;
25ba77c1 498 int nid;
91612e0d
HD
499
500 if (!pte_present(*pte))
1da177e4 501 continue;
6aab341e
LT
502 page = vm_normal_page(vma, addr, *pte);
503 if (!page)
1da177e4 504 continue;
053837fc 505 /*
62b61f61
HD
506 * vm_normal_page() filters out zero pages, but there might
507 * still be PageReserved pages to skip, perhaps in a VDSO.
053837fc 508 */
b79bc0a0 509 if (PageReserved(page))
f4598c8b 510 continue;
6aab341e 511 nid = page_to_nid(page);
38e35860
CL
512 if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
513 continue;
514
b1f72d18 515 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
fc301289 516 migrate_page_add(page, private, flags);
38e35860
CL
517 else
518 break;
91612e0d 519 } while (pte++, addr += PAGE_SIZE, addr != end);
705e87c0 520 pte_unmap_unlock(orig_pte, ptl);
91612e0d
HD
521 return addr != end;
522}
523
98094945
NH
524static void queue_pages_hugetlb_pmd_range(struct vm_area_struct *vma,
525 pmd_t *pmd, const nodemask_t *nodes, unsigned long flags,
e2d8cf40
NH
526 void *private)
527{
528#ifdef CONFIG_HUGETLB_PAGE
529 int nid;
530 struct page *page;
cb900f41 531 spinlock_t *ptl;
e2d8cf40 532
cb900f41 533 ptl = huge_pte_lock(hstate_vma(vma), vma->vm_mm, (pte_t *)pmd);
e2d8cf40
NH
534 page = pte_page(huge_ptep_get((pte_t *)pmd));
535 nid = page_to_nid(page);
536 if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
537 goto unlock;
538 /* With MPOL_MF_MOVE, we migrate only unshared hugepage. */
539 if (flags & (MPOL_MF_MOVE_ALL) ||
540 (flags & MPOL_MF_MOVE && page_mapcount(page) == 1))
541 isolate_huge_page(page, private);
542unlock:
cb900f41 543 spin_unlock(ptl);
e2d8cf40
NH
544#else
545 BUG();
546#endif
547}
548
98094945 549static inline int queue_pages_pmd_range(struct vm_area_struct *vma, pud_t *pud,
dc9aa5b9
CL
550 unsigned long addr, unsigned long end,
551 const nodemask_t *nodes, unsigned long flags,
38e35860 552 void *private)
91612e0d
HD
553{
554 pmd_t *pmd;
555 unsigned long next;
556
557 pmd = pmd_offset(pud, addr);
558 do {
559 next = pmd_addr_end(addr, end);
e2d8cf40
NH
560 if (!pmd_present(*pmd))
561 continue;
562 if (pmd_huge(*pmd) && is_vm_hugetlb_page(vma)) {
98094945 563 queue_pages_hugetlb_pmd_range(vma, pmd, nodes,
e2d8cf40
NH
564 flags, private);
565 continue;
566 }
e180377f 567 split_huge_page_pmd(vma, addr, pmd);
1a5a9906 568 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
91612e0d 569 continue;
98094945 570 if (queue_pages_pte_range(vma, pmd, addr, next, nodes,
38e35860 571 flags, private))
91612e0d
HD
572 return -EIO;
573 } while (pmd++, addr = next, addr != end);
574 return 0;
575}
576
98094945 577static inline int queue_pages_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
dc9aa5b9
CL
578 unsigned long addr, unsigned long end,
579 const nodemask_t *nodes, unsigned long flags,
38e35860 580 void *private)
91612e0d
HD
581{
582 pud_t *pud;
583 unsigned long next;
584
585 pud = pud_offset(pgd, addr);
586 do {
587 next = pud_addr_end(addr, end);
e2d8cf40
NH
588 if (pud_huge(*pud) && is_vm_hugetlb_page(vma))
589 continue;
91612e0d
HD
590 if (pud_none_or_clear_bad(pud))
591 continue;
98094945 592 if (queue_pages_pmd_range(vma, pud, addr, next, nodes,
38e35860 593 flags, private))
91612e0d
HD
594 return -EIO;
595 } while (pud++, addr = next, addr != end);
596 return 0;
597}
598
98094945 599static inline int queue_pages_pgd_range(struct vm_area_struct *vma,
dc9aa5b9
CL
600 unsigned long addr, unsigned long end,
601 const nodemask_t *nodes, unsigned long flags,
38e35860 602 void *private)
91612e0d
HD
603{
604 pgd_t *pgd;
605 unsigned long next;
606
b5810039 607 pgd = pgd_offset(vma->vm_mm, addr);
91612e0d
HD
608 do {
609 next = pgd_addr_end(addr, end);
610 if (pgd_none_or_clear_bad(pgd))
611 continue;
98094945 612 if (queue_pages_pud_range(vma, pgd, addr, next, nodes,
38e35860 613 flags, private))
91612e0d
HD
614 return -EIO;
615 } while (pgd++, addr = next, addr != end);
616 return 0;
1da177e4
LT
617}
618
5877231f 619#ifdef CONFIG_NUMA_BALANCING
b24f53a0 620/*
4b10e7d5
MG
621 * This is used to mark a range of virtual addresses to be inaccessible.
622 * These are later cleared by a NUMA hinting fault. Depending on these
623 * faults, pages may be migrated for better NUMA placement.
624 *
625 * This is assuming that NUMA faults are handled using PROT_NONE. If
626 * an architecture makes a different choice, it will need further
627 * changes to the core.
b24f53a0 628 */
4b10e7d5
MG
629unsigned long change_prot_numa(struct vm_area_struct *vma,
630 unsigned long addr, unsigned long end)
b24f53a0 631{
4b10e7d5 632 int nr_updated;
b24f53a0 633
4b10e7d5 634 nr_updated = change_protection(vma, addr, end, vma->vm_page_prot, 0, 1);
03c5a6e1
MG
635 if (nr_updated)
636 count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
b24f53a0 637
4b10e7d5 638 return nr_updated;
b24f53a0
LS
639}
640#else
641static unsigned long change_prot_numa(struct vm_area_struct *vma,
642 unsigned long addr, unsigned long end)
643{
644 return 0;
645}
5877231f 646#endif /* CONFIG_NUMA_BALANCING */
b24f53a0 647
dc9aa5b9 648/*
98094945
NH
649 * Walk through page tables and collect pages to be migrated.
650 *
651 * If pages found in a given range are on a set of nodes (determined by
652 * @nodes and @flags,) it's isolated and queued to the pagelist which is
653 * passed via @private.)
dc9aa5b9 654 */
1da177e4 655static struct vm_area_struct *
98094945 656queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
38e35860 657 const nodemask_t *nodes, unsigned long flags, void *private)
1da177e4
LT
658{
659 int err;
660 struct vm_area_struct *first, *vma, *prev;
661
053837fc 662
1da177e4
LT
663 first = find_vma(mm, start);
664 if (!first)
665 return ERR_PTR(-EFAULT);
666 prev = NULL;
667 for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
b24f53a0
LS
668 unsigned long endvma = vma->vm_end;
669
670 if (endvma > end)
671 endvma = end;
672 if (vma->vm_start > start)
673 start = vma->vm_start;
674
dc9aa5b9
CL
675 if (!(flags & MPOL_MF_DISCONTIG_OK)) {
676 if (!vma->vm_next && vma->vm_end < end)
677 return ERR_PTR(-EFAULT);
678 if (prev && prev->vm_end < vma->vm_start)
679 return ERR_PTR(-EFAULT);
680 }
b24f53a0 681
b24f53a0
LS
682 if (flags & MPOL_MF_LAZY) {
683 change_prot_numa(vma, start, endvma);
684 goto next;
685 }
686
687 if ((flags & MPOL_MF_STRICT) ||
dc9aa5b9 688 ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
b24f53a0 689 vma_migratable(vma))) {
dc9aa5b9 690
98094945 691 err = queue_pages_pgd_range(vma, start, endvma, nodes,
38e35860 692 flags, private);
1da177e4
LT
693 if (err) {
694 first = ERR_PTR(err);
695 break;
696 }
697 }
b24f53a0 698next:
1da177e4
LT
699 prev = vma;
700 }
701 return first;
702}
703
869833f2
KM
704/*
705 * Apply policy to a single VMA
706 * This must be called with the mmap_sem held for writing.
707 */
708static int vma_replace_policy(struct vm_area_struct *vma,
709 struct mempolicy *pol)
8d34694c 710{
869833f2
KM
711 int err;
712 struct mempolicy *old;
713 struct mempolicy *new;
8d34694c
KM
714
715 pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
716 vma->vm_start, vma->vm_end, vma->vm_pgoff,
717 vma->vm_ops, vma->vm_file,
718 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
719
869833f2
KM
720 new = mpol_dup(pol);
721 if (IS_ERR(new))
722 return PTR_ERR(new);
723
724 if (vma->vm_ops && vma->vm_ops->set_policy) {
8d34694c 725 err = vma->vm_ops->set_policy(vma, new);
869833f2
KM
726 if (err)
727 goto err_out;
8d34694c 728 }
869833f2
KM
729
730 old = vma->vm_policy;
731 vma->vm_policy = new; /* protected by mmap_sem */
732 mpol_put(old);
733
734 return 0;
735 err_out:
736 mpol_put(new);
8d34694c
KM
737 return err;
738}
739
1da177e4 740/* Step 2: apply policy to a range and do splits. */
9d8cebd4
KM
741static int mbind_range(struct mm_struct *mm, unsigned long start,
742 unsigned long end, struct mempolicy *new_pol)
1da177e4
LT
743{
744 struct vm_area_struct *next;
9d8cebd4
KM
745 struct vm_area_struct *prev;
746 struct vm_area_struct *vma;
747 int err = 0;
e26a5114 748 pgoff_t pgoff;
9d8cebd4
KM
749 unsigned long vmstart;
750 unsigned long vmend;
1da177e4 751
097d5910 752 vma = find_vma(mm, start);
9d8cebd4
KM
753 if (!vma || vma->vm_start > start)
754 return -EFAULT;
755
097d5910 756 prev = vma->vm_prev;
e26a5114
KM
757 if (start > vma->vm_start)
758 prev = vma;
759
9d8cebd4 760 for (; vma && vma->vm_start < end; prev = vma, vma = next) {
1da177e4 761 next = vma->vm_next;
9d8cebd4
KM
762 vmstart = max(start, vma->vm_start);
763 vmend = min(end, vma->vm_end);
764
e26a5114
KM
765 if (mpol_equal(vma_policy(vma), new_pol))
766 continue;
767
768 pgoff = vma->vm_pgoff +
769 ((vmstart - vma->vm_start) >> PAGE_SHIFT);
9d8cebd4 770 prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
e26a5114 771 vma->anon_vma, vma->vm_file, pgoff,
8aacc9f5 772 new_pol);
9d8cebd4
KM
773 if (prev) {
774 vma = prev;
775 next = vma->vm_next;
3964acd0
ON
776 if (mpol_equal(vma_policy(vma), new_pol))
777 continue;
778 /* vma_merge() joined vma && vma->next, case 8 */
779 goto replace;
9d8cebd4
KM
780 }
781 if (vma->vm_start != vmstart) {
782 err = split_vma(vma->vm_mm, vma, vmstart, 1);
783 if (err)
784 goto out;
785 }
786 if (vma->vm_end != vmend) {
787 err = split_vma(vma->vm_mm, vma, vmend, 0);
788 if (err)
789 goto out;
790 }
3964acd0 791 replace:
869833f2 792 err = vma_replace_policy(vma, new_pol);
8d34694c
KM
793 if (err)
794 goto out;
1da177e4 795 }
9d8cebd4
KM
796
797 out:
1da177e4
LT
798 return err;
799}
800
1da177e4 801/* Set the process memory policy */
028fec41
DR
802static long do_set_mempolicy(unsigned short mode, unsigned short flags,
803 nodemask_t *nodes)
1da177e4 804{
58568d2a 805 struct mempolicy *new, *old;
f4e53d91 806 struct mm_struct *mm = current->mm;
4bfc4495 807 NODEMASK_SCRATCH(scratch);
58568d2a 808 int ret;
1da177e4 809
4bfc4495
KH
810 if (!scratch)
811 return -ENOMEM;
f4e53d91 812
4bfc4495
KH
813 new = mpol_new(mode, flags, nodes);
814 if (IS_ERR(new)) {
815 ret = PTR_ERR(new);
816 goto out;
817 }
f4e53d91
LS
818 /*
819 * prevent changing our mempolicy while show_numa_maps()
820 * is using it.
821 * Note: do_set_mempolicy() can be called at init time
822 * with no 'mm'.
823 */
824 if (mm)
825 down_write(&mm->mmap_sem);
58568d2a 826 task_lock(current);
4bfc4495 827 ret = mpol_set_nodemask(new, nodes, scratch);
58568d2a
MX
828 if (ret) {
829 task_unlock(current);
830 if (mm)
831 up_write(&mm->mmap_sem);
832 mpol_put(new);
4bfc4495 833 goto out;
58568d2a
MX
834 }
835 old = current->mempolicy;
1da177e4 836 current->mempolicy = new;
45c4745a 837 if (new && new->mode == MPOL_INTERLEAVE &&
f5b087b5 838 nodes_weight(new->v.nodes))
dfcd3c0d 839 current->il_next = first_node(new->v.nodes);
58568d2a 840 task_unlock(current);
f4e53d91
LS
841 if (mm)
842 up_write(&mm->mmap_sem);
843
58568d2a 844 mpol_put(old);
4bfc4495
KH
845 ret = 0;
846out:
847 NODEMASK_SCRATCH_FREE(scratch);
848 return ret;
1da177e4
LT
849}
850
bea904d5
LS
851/*
852 * Return nodemask for policy for get_mempolicy() query
58568d2a
MX
853 *
854 * Called with task's alloc_lock held
bea904d5
LS
855 */
856static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
1da177e4 857{
dfcd3c0d 858 nodes_clear(*nodes);
bea904d5
LS
859 if (p == &default_policy)
860 return;
861
45c4745a 862 switch (p->mode) {
19770b32
MG
863 case MPOL_BIND:
864 /* Fall through */
1da177e4 865 case MPOL_INTERLEAVE:
dfcd3c0d 866 *nodes = p->v.nodes;
1da177e4
LT
867 break;
868 case MPOL_PREFERRED:
fc36b8d3 869 if (!(p->flags & MPOL_F_LOCAL))
dfcd3c0d 870 node_set(p->v.preferred_node, *nodes);
53f2556b 871 /* else return empty node mask for local allocation */
1da177e4
LT
872 break;
873 default:
874 BUG();
875 }
876}
877
878static int lookup_node(struct mm_struct *mm, unsigned long addr)
879{
880 struct page *p;
881 int err;
882
883 err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
884 if (err >= 0) {
885 err = page_to_nid(p);
886 put_page(p);
887 }
888 return err;
889}
890
1da177e4 891/* Retrieve NUMA policy */
dbcb0f19
AB
892static long do_get_mempolicy(int *policy, nodemask_t *nmask,
893 unsigned long addr, unsigned long flags)
1da177e4 894{
8bccd85f 895 int err;
1da177e4
LT
896 struct mm_struct *mm = current->mm;
897 struct vm_area_struct *vma = NULL;
898 struct mempolicy *pol = current->mempolicy;
899
754af6f5
LS
900 if (flags &
901 ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
1da177e4 902 return -EINVAL;
754af6f5
LS
903
904 if (flags & MPOL_F_MEMS_ALLOWED) {
905 if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
906 return -EINVAL;
907 *policy = 0; /* just so it's initialized */
58568d2a 908 task_lock(current);
754af6f5 909 *nmask = cpuset_current_mems_allowed;
58568d2a 910 task_unlock(current);
754af6f5
LS
911 return 0;
912 }
913
1da177e4 914 if (flags & MPOL_F_ADDR) {
bea904d5
LS
915 /*
916 * Do NOT fall back to task policy if the
917 * vma/shared policy at addr is NULL. We
918 * want to return MPOL_DEFAULT in this case.
919 */
1da177e4
LT
920 down_read(&mm->mmap_sem);
921 vma = find_vma_intersection(mm, addr, addr+1);
922 if (!vma) {
923 up_read(&mm->mmap_sem);
924 return -EFAULT;
925 }
926 if (vma->vm_ops && vma->vm_ops->get_policy)
927 pol = vma->vm_ops->get_policy(vma, addr);
928 else
929 pol = vma->vm_policy;
930 } else if (addr)
931 return -EINVAL;
932
933 if (!pol)
bea904d5 934 pol = &default_policy; /* indicates default behavior */
1da177e4
LT
935
936 if (flags & MPOL_F_NODE) {
937 if (flags & MPOL_F_ADDR) {
938 err = lookup_node(mm, addr);
939 if (err < 0)
940 goto out;
8bccd85f 941 *policy = err;
1da177e4 942 } else if (pol == current->mempolicy &&
45c4745a 943 pol->mode == MPOL_INTERLEAVE) {
8bccd85f 944 *policy = current->il_next;
1da177e4
LT
945 } else {
946 err = -EINVAL;
947 goto out;
948 }
bea904d5
LS
949 } else {
950 *policy = pol == &default_policy ? MPOL_DEFAULT :
951 pol->mode;
d79df630
DR
952 /*
953 * Internal mempolicy flags must be masked off before exposing
954 * the policy to userspace.
955 */
956 *policy |= (pol->flags & MPOL_MODE_FLAGS);
bea904d5 957 }
1da177e4
LT
958
959 if (vma) {
960 up_read(&current->mm->mmap_sem);
961 vma = NULL;
962 }
963
1da177e4 964 err = 0;
58568d2a 965 if (nmask) {
c6b6ef8b
LS
966 if (mpol_store_user_nodemask(pol)) {
967 *nmask = pol->w.user_nodemask;
968 } else {
969 task_lock(current);
970 get_policy_nodemask(pol, nmask);
971 task_unlock(current);
972 }
58568d2a 973 }
1da177e4
LT
974
975 out:
52cd3b07 976 mpol_cond_put(pol);
1da177e4
LT
977 if (vma)
978 up_read(&current->mm->mmap_sem);
979 return err;
980}
981
b20a3503 982#ifdef CONFIG_MIGRATION
6ce3c4c0
CL
983/*
984 * page migration
985 */
fc301289
CL
986static void migrate_page_add(struct page *page, struct list_head *pagelist,
987 unsigned long flags)
6ce3c4c0
CL
988{
989 /*
fc301289 990 * Avoid migrating a page that is shared with others.
6ce3c4c0 991 */
62695a84
NP
992 if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
993 if (!isolate_lru_page(page)) {
994 list_add_tail(&page->lru, pagelist);
6d9c285a
KM
995 inc_zone_page_state(page, NR_ISOLATED_ANON +
996 page_is_file_cache(page));
62695a84
NP
997 }
998 }
7e2ab150 999}
6ce3c4c0 1000
742755a1 1001static struct page *new_node_page(struct page *page, unsigned long node, int **x)
95a402c3 1002{
e2d8cf40
NH
1003 if (PageHuge(page))
1004 return alloc_huge_page_node(page_hstate(compound_head(page)),
1005 node);
1006 else
1007 return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
95a402c3
CL
1008}
1009
7e2ab150
CL
1010/*
1011 * Migrate pages from one node to a target node.
1012 * Returns error or the number of pages not migrated.
1013 */
dbcb0f19
AB
1014static int migrate_to_node(struct mm_struct *mm, int source, int dest,
1015 int flags)
7e2ab150
CL
1016{
1017 nodemask_t nmask;
1018 LIST_HEAD(pagelist);
1019 int err = 0;
1020
1021 nodes_clear(nmask);
1022 node_set(source, nmask);
6ce3c4c0 1023
08270807
MK
1024 /*
1025 * This does not "check" the range but isolates all pages that
1026 * need migration. Between passing in the full user address
1027 * space range and MPOL_MF_DISCONTIG_OK, this call can not fail.
1028 */
1029 VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)));
98094945 1030 queue_pages_range(mm, mm->mmap->vm_start, mm->task_size, &nmask,
7e2ab150
CL
1031 flags | MPOL_MF_DISCONTIG_OK, &pagelist);
1032
cf608ac1 1033 if (!list_empty(&pagelist)) {
68711a74 1034 err = migrate_pages(&pagelist, new_node_page, NULL, dest,
9c620e2b 1035 MIGRATE_SYNC, MR_SYSCALL);
cf608ac1 1036 if (err)
e2d8cf40 1037 putback_movable_pages(&pagelist);
cf608ac1 1038 }
95a402c3 1039
7e2ab150 1040 return err;
6ce3c4c0
CL
1041}
1042
39743889 1043/*
7e2ab150
CL
1044 * Move pages between the two nodesets so as to preserve the physical
1045 * layout as much as possible.
39743889
CL
1046 *
1047 * Returns the number of page that could not be moved.
1048 */
0ce72d4f
AM
1049int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
1050 const nodemask_t *to, int flags)
39743889 1051{
7e2ab150 1052 int busy = 0;
0aedadf9 1053 int err;
7e2ab150 1054 nodemask_t tmp;
39743889 1055
0aedadf9
CL
1056 err = migrate_prep();
1057 if (err)
1058 return err;
1059
53f2556b 1060 down_read(&mm->mmap_sem);
39743889 1061
0ce72d4f 1062 err = migrate_vmas(mm, from, to, flags);
7b2259b3
CL
1063 if (err)
1064 goto out;
1065
da0aa138
KM
1066 /*
1067 * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
1068 * bit in 'to' is not also set in 'tmp'. Clear the found 'source'
1069 * bit in 'tmp', and return that <source, dest> pair for migration.
1070 * The pair of nodemasks 'to' and 'from' define the map.
1071 *
1072 * If no pair of bits is found that way, fallback to picking some
1073 * pair of 'source' and 'dest' bits that are not the same. If the
1074 * 'source' and 'dest' bits are the same, this represents a node
1075 * that will be migrating to itself, so no pages need move.
1076 *
1077 * If no bits are left in 'tmp', or if all remaining bits left
1078 * in 'tmp' correspond to the same bit in 'to', return false
1079 * (nothing left to migrate).
1080 *
1081 * This lets us pick a pair of nodes to migrate between, such that
1082 * if possible the dest node is not already occupied by some other
1083 * source node, minimizing the risk of overloading the memory on a
1084 * node that would happen if we migrated incoming memory to a node
1085 * before migrating outgoing memory source that same node.
1086 *
1087 * A single scan of tmp is sufficient. As we go, we remember the
1088 * most recent <s, d> pair that moved (s != d). If we find a pair
1089 * that not only moved, but what's better, moved to an empty slot
1090 * (d is not set in tmp), then we break out then, with that pair.
ae0e47f0 1091 * Otherwise when we finish scanning from_tmp, we at least have the
da0aa138
KM
1092 * most recent <s, d> pair that moved. If we get all the way through
1093 * the scan of tmp without finding any node that moved, much less
1094 * moved to an empty node, then there is nothing left worth migrating.
1095 */
d4984711 1096
0ce72d4f 1097 tmp = *from;
7e2ab150
CL
1098 while (!nodes_empty(tmp)) {
1099 int s,d;
b76ac7e7 1100 int source = NUMA_NO_NODE;
7e2ab150
CL
1101 int dest = 0;
1102
1103 for_each_node_mask(s, tmp) {
4a5b18cc
LW
1104
1105 /*
1106 * do_migrate_pages() tries to maintain the relative
1107 * node relationship of the pages established between
1108 * threads and memory areas.
1109 *
1110 * However if the number of source nodes is not equal to
1111 * the number of destination nodes we can not preserve
1112 * this node relative relationship. In that case, skip
1113 * copying memory from a node that is in the destination
1114 * mask.
1115 *
1116 * Example: [2,3,4] -> [3,4,5] moves everything.
1117 * [0-7] - > [3,4,5] moves only 0,1,2,6,7.
1118 */
1119
0ce72d4f
AM
1120 if ((nodes_weight(*from) != nodes_weight(*to)) &&
1121 (node_isset(s, *to)))
4a5b18cc
LW
1122 continue;
1123
0ce72d4f 1124 d = node_remap(s, *from, *to);
7e2ab150
CL
1125 if (s == d)
1126 continue;
1127
1128 source = s; /* Node moved. Memorize */
1129 dest = d;
1130
1131 /* dest not in remaining from nodes? */
1132 if (!node_isset(dest, tmp))
1133 break;
1134 }
b76ac7e7 1135 if (source == NUMA_NO_NODE)
7e2ab150
CL
1136 break;
1137
1138 node_clear(source, tmp);
1139 err = migrate_to_node(mm, source, dest, flags);
1140 if (err > 0)
1141 busy += err;
1142 if (err < 0)
1143 break;
39743889 1144 }
7b2259b3 1145out:
39743889 1146 up_read(&mm->mmap_sem);
7e2ab150
CL
1147 if (err < 0)
1148 return err;
1149 return busy;
b20a3503
CL
1150
1151}
1152
3ad33b24
LS
1153/*
1154 * Allocate a new page for page migration based on vma policy.
1155 * Start assuming that page is mapped by vma pointed to by @private.
1156 * Search forward from there, if not. N.B., this assumes that the
1157 * list of pages handed to migrate_pages()--which is how we get here--
1158 * is in virtual address order.
1159 */
742755a1 1160static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
95a402c3
CL
1161{
1162 struct vm_area_struct *vma = (struct vm_area_struct *)private;
3ad33b24 1163 unsigned long uninitialized_var(address);
95a402c3 1164
3ad33b24
LS
1165 while (vma) {
1166 address = page_address_in_vma(page, vma);
1167 if (address != -EFAULT)
1168 break;
1169 vma = vma->vm_next;
1170 }
11c731e8
WL
1171
1172 if (PageHuge(page)) {
cc81717e
MH
1173 BUG_ON(!vma);
1174 return alloc_huge_page_noerr(vma, address, 1);
11c731e8 1175 }
0bf598d8 1176 /*
11c731e8 1177 * if !vma, alloc_page_vma() will use task or system default policy
0bf598d8 1178 */
3ad33b24 1179 return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
95a402c3 1180}
b20a3503
CL
1181#else
1182
1183static void migrate_page_add(struct page *page, struct list_head *pagelist,
1184 unsigned long flags)
1185{
39743889
CL
1186}
1187
0ce72d4f
AM
1188int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
1189 const nodemask_t *to, int flags)
b20a3503
CL
1190{
1191 return -ENOSYS;
1192}
95a402c3 1193
69939749 1194static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
95a402c3
CL
1195{
1196 return NULL;
1197}
b20a3503
CL
1198#endif
1199
dbcb0f19 1200static long do_mbind(unsigned long start, unsigned long len,
028fec41
DR
1201 unsigned short mode, unsigned short mode_flags,
1202 nodemask_t *nmask, unsigned long flags)
6ce3c4c0
CL
1203{
1204 struct vm_area_struct *vma;
1205 struct mm_struct *mm = current->mm;
1206 struct mempolicy *new;
1207 unsigned long end;
1208 int err;
1209 LIST_HEAD(pagelist);
1210
b24f53a0 1211 if (flags & ~(unsigned long)MPOL_MF_VALID)
6ce3c4c0 1212 return -EINVAL;
74c00241 1213 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
6ce3c4c0
CL
1214 return -EPERM;
1215
1216 if (start & ~PAGE_MASK)
1217 return -EINVAL;
1218
1219 if (mode == MPOL_DEFAULT)
1220 flags &= ~MPOL_MF_STRICT;
1221
1222 len = (len + PAGE_SIZE - 1) & PAGE_MASK;
1223 end = start + len;
1224
1225 if (end < start)
1226 return -EINVAL;
1227 if (end == start)
1228 return 0;
1229
028fec41 1230 new = mpol_new(mode, mode_flags, nmask);
6ce3c4c0
CL
1231 if (IS_ERR(new))
1232 return PTR_ERR(new);
1233
b24f53a0
LS
1234 if (flags & MPOL_MF_LAZY)
1235 new->flags |= MPOL_F_MOF;
1236
6ce3c4c0
CL
1237 /*
1238 * If we are using the default policy then operation
1239 * on discontinuous address spaces is okay after all
1240 */
1241 if (!new)
1242 flags |= MPOL_MF_DISCONTIG_OK;
1243
028fec41
DR
1244 pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
1245 start, start + len, mode, mode_flags,
00ef2d2f 1246 nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE);
6ce3c4c0 1247
0aedadf9
CL
1248 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
1249
1250 err = migrate_prep();
1251 if (err)
b05ca738 1252 goto mpol_out;
0aedadf9 1253 }
4bfc4495
KH
1254 {
1255 NODEMASK_SCRATCH(scratch);
1256 if (scratch) {
1257 down_write(&mm->mmap_sem);
1258 task_lock(current);
1259 err = mpol_set_nodemask(new, nmask, scratch);
1260 task_unlock(current);
1261 if (err)
1262 up_write(&mm->mmap_sem);
1263 } else
1264 err = -ENOMEM;
1265 NODEMASK_SCRATCH_FREE(scratch);
1266 }
b05ca738
KM
1267 if (err)
1268 goto mpol_out;
1269
98094945 1270 vma = queue_pages_range(mm, start, end, nmask,
6ce3c4c0
CL
1271 flags | MPOL_MF_INVERT, &pagelist);
1272
b24f53a0 1273 err = PTR_ERR(vma); /* maybe ... */
a720094d 1274 if (!IS_ERR(vma))
9d8cebd4 1275 err = mbind_range(mm, start, end, new);
7e2ab150 1276
b24f53a0
LS
1277 if (!err) {
1278 int nr_failed = 0;
1279
cf608ac1 1280 if (!list_empty(&pagelist)) {
b24f53a0 1281 WARN_ON_ONCE(flags & MPOL_MF_LAZY);
95a402c3 1282 nr_failed = migrate_pages(&pagelist, new_vma_page,
68711a74 1283 NULL, (unsigned long)vma,
9c620e2b 1284 MIGRATE_SYNC, MR_MEMPOLICY_MBIND);
cf608ac1 1285 if (nr_failed)
74060e4d 1286 putback_movable_pages(&pagelist);
cf608ac1 1287 }
6ce3c4c0 1288
b24f53a0 1289 if (nr_failed && (flags & MPOL_MF_STRICT))
6ce3c4c0 1290 err = -EIO;
ab8a3e14 1291 } else
b0e5fd73 1292 putback_movable_pages(&pagelist);
b20a3503 1293
6ce3c4c0 1294 up_write(&mm->mmap_sem);
b05ca738 1295 mpol_out:
f0be3d32 1296 mpol_put(new);
6ce3c4c0
CL
1297 return err;
1298}
1299
8bccd85f
CL
1300/*
1301 * User space interface with variable sized bitmaps for nodelists.
1302 */
1303
1304/* Copy a node mask from user space. */
39743889 1305static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
8bccd85f
CL
1306 unsigned long maxnode)
1307{
1308 unsigned long k;
1309 unsigned long nlongs;
1310 unsigned long endmask;
1311
1312 --maxnode;
1313 nodes_clear(*nodes);
1314 if (maxnode == 0 || !nmask)
1315 return 0;
a9c930ba 1316 if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
636f13c1 1317 return -EINVAL;
8bccd85f
CL
1318
1319 nlongs = BITS_TO_LONGS(maxnode);
1320 if ((maxnode % BITS_PER_LONG) == 0)
1321 endmask = ~0UL;
1322 else
1323 endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
1324
1325 /* When the user specified more nodes than supported just check
1326 if the non supported part is all zero. */
1327 if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
1328 if (nlongs > PAGE_SIZE/sizeof(long))
1329 return -EINVAL;
1330 for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
1331 unsigned long t;
1332 if (get_user(t, nmask + k))
1333 return -EFAULT;
1334 if (k == nlongs - 1) {
1335 if (t & endmask)
1336 return -EINVAL;
1337 } else if (t)
1338 return -EINVAL;
1339 }
1340 nlongs = BITS_TO_LONGS(MAX_NUMNODES);
1341 endmask = ~0UL;
1342 }
1343
1344 if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
1345 return -EFAULT;
1346 nodes_addr(*nodes)[nlongs-1] &= endmask;
1347 return 0;
1348}
1349
1350/* Copy a kernel node mask to user space */
1351static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
1352 nodemask_t *nodes)
1353{
1354 unsigned long copy = ALIGN(maxnode-1, 64) / 8;
1355 const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
1356
1357 if (copy > nbytes) {
1358 if (copy > PAGE_SIZE)
1359 return -EINVAL;
1360 if (clear_user((char __user *)mask + nbytes, copy - nbytes))
1361 return -EFAULT;
1362 copy = nbytes;
1363 }
1364 return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
1365}
1366
938bb9f5 1367SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
f7f28ca9 1368 unsigned long, mode, const unsigned long __user *, nmask,
938bb9f5 1369 unsigned long, maxnode, unsigned, flags)
8bccd85f
CL
1370{
1371 nodemask_t nodes;
1372 int err;
028fec41 1373 unsigned short mode_flags;
8bccd85f 1374
028fec41
DR
1375 mode_flags = mode & MPOL_MODE_FLAGS;
1376 mode &= ~MPOL_MODE_FLAGS;
a3b51e01
DR
1377 if (mode >= MPOL_MAX)
1378 return -EINVAL;
4c50bc01
DR
1379 if ((mode_flags & MPOL_F_STATIC_NODES) &&
1380 (mode_flags & MPOL_F_RELATIVE_NODES))
1381 return -EINVAL;
8bccd85f
CL
1382 err = get_nodes(&nodes, nmask, maxnode);
1383 if (err)
1384 return err;
028fec41 1385 return do_mbind(start, len, mode, mode_flags, &nodes, flags);
8bccd85f
CL
1386}
1387
1388/* Set the process memory policy */
23c8902d 1389SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask,
938bb9f5 1390 unsigned long, maxnode)
8bccd85f
CL
1391{
1392 int err;
1393 nodemask_t nodes;
028fec41 1394 unsigned short flags;
8bccd85f 1395
028fec41
DR
1396 flags = mode & MPOL_MODE_FLAGS;
1397 mode &= ~MPOL_MODE_FLAGS;
1398 if ((unsigned int)mode >= MPOL_MAX)
8bccd85f 1399 return -EINVAL;
4c50bc01
DR
1400 if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
1401 return -EINVAL;
8bccd85f
CL
1402 err = get_nodes(&nodes, nmask, maxnode);
1403 if (err)
1404 return err;
028fec41 1405 return do_set_mempolicy(mode, flags, &nodes);
8bccd85f
CL
1406}
1407
938bb9f5
HC
1408SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
1409 const unsigned long __user *, old_nodes,
1410 const unsigned long __user *, new_nodes)
39743889 1411{
c69e8d9c 1412 const struct cred *cred = current_cred(), *tcred;
596d7cfa 1413 struct mm_struct *mm = NULL;
39743889 1414 struct task_struct *task;
39743889
CL
1415 nodemask_t task_nodes;
1416 int err;
596d7cfa
KM
1417 nodemask_t *old;
1418 nodemask_t *new;
1419 NODEMASK_SCRATCH(scratch);
1420
1421 if (!scratch)
1422 return -ENOMEM;
39743889 1423
596d7cfa
KM
1424 old = &scratch->mask1;
1425 new = &scratch->mask2;
1426
1427 err = get_nodes(old, old_nodes, maxnode);
39743889 1428 if (err)
596d7cfa 1429 goto out;
39743889 1430
596d7cfa 1431 err = get_nodes(new, new_nodes, maxnode);
39743889 1432 if (err)
596d7cfa 1433 goto out;
39743889
CL
1434
1435 /* Find the mm_struct */
55cfaa3c 1436 rcu_read_lock();
228ebcbe 1437 task = pid ? find_task_by_vpid(pid) : current;
39743889 1438 if (!task) {
55cfaa3c 1439 rcu_read_unlock();
596d7cfa
KM
1440 err = -ESRCH;
1441 goto out;
39743889 1442 }
3268c63e 1443 get_task_struct(task);
39743889 1444
596d7cfa 1445 err = -EINVAL;
39743889
CL
1446
1447 /*
1448 * Check if this process has the right to modify the specified
1449 * process. The right exists if the process has administrative
7f927fcc 1450 * capabilities, superuser privileges or the same
39743889
CL
1451 * userid as the target process.
1452 */
c69e8d9c 1453 tcred = __task_cred(task);
b38a86eb
EB
1454 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1455 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
74c00241 1456 !capable(CAP_SYS_NICE)) {
c69e8d9c 1457 rcu_read_unlock();
39743889 1458 err = -EPERM;
3268c63e 1459 goto out_put;
39743889 1460 }
c69e8d9c 1461 rcu_read_unlock();
39743889
CL
1462
1463 task_nodes = cpuset_mems_allowed(task);
1464 /* Is the user allowed to access the target nodes? */
596d7cfa 1465 if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
39743889 1466 err = -EPERM;
3268c63e 1467 goto out_put;
39743889
CL
1468 }
1469
01f13bd6 1470 if (!nodes_subset(*new, node_states[N_MEMORY])) {
3b42d28b 1471 err = -EINVAL;
3268c63e 1472 goto out_put;
3b42d28b
CL
1473 }
1474
86c3a764
DQ
1475 err = security_task_movememory(task);
1476 if (err)
3268c63e 1477 goto out_put;
86c3a764 1478
3268c63e
CL
1479 mm = get_task_mm(task);
1480 put_task_struct(task);
f2a9ef88
SL
1481
1482 if (!mm) {
3268c63e 1483 err = -EINVAL;
f2a9ef88
SL
1484 goto out;
1485 }
1486
1487 err = do_migrate_pages(mm, old, new,
1488 capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
3268c63e
CL
1489
1490 mmput(mm);
1491out:
596d7cfa
KM
1492 NODEMASK_SCRATCH_FREE(scratch);
1493
39743889 1494 return err;
3268c63e
CL
1495
1496out_put:
1497 put_task_struct(task);
1498 goto out;
1499
39743889
CL
1500}
1501
1502
8bccd85f 1503/* Retrieve NUMA policy */
938bb9f5
HC
1504SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1505 unsigned long __user *, nmask, unsigned long, maxnode,
1506 unsigned long, addr, unsigned long, flags)
8bccd85f 1507{
dbcb0f19
AB
1508 int err;
1509 int uninitialized_var(pval);
8bccd85f
CL
1510 nodemask_t nodes;
1511
1512 if (nmask != NULL && maxnode < MAX_NUMNODES)
1513 return -EINVAL;
1514
1515 err = do_get_mempolicy(&pval, &nodes, addr, flags);
1516
1517 if (err)
1518 return err;
1519
1520 if (policy && put_user(pval, policy))
1521 return -EFAULT;
1522
1523 if (nmask)
1524 err = copy_nodes_to_user(nmask, maxnode, &nodes);
1525
1526 return err;
1527}
1528
1da177e4
LT
1529#ifdef CONFIG_COMPAT
1530
c93e0f6c
HC
1531COMPAT_SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1532 compat_ulong_t __user *, nmask,
1533 compat_ulong_t, maxnode,
1534 compat_ulong_t, addr, compat_ulong_t, flags)
1da177e4
LT
1535{
1536 long err;
1537 unsigned long __user *nm = NULL;
1538 unsigned long nr_bits, alloc_size;
1539 DECLARE_BITMAP(bm, MAX_NUMNODES);
1540
1541 nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
1542 alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1543
1544 if (nmask)
1545 nm = compat_alloc_user_space(alloc_size);
1546
1547 err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
1548
1549 if (!err && nmask) {
2bbff6c7
KH
1550 unsigned long copy_size;
1551 copy_size = min_t(unsigned long, sizeof(bm), alloc_size);
1552 err = copy_from_user(bm, nm, copy_size);
1da177e4
LT
1553 /* ensure entire bitmap is zeroed */
1554 err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
1555 err |= compat_put_bitmap(nmask, bm, nr_bits);
1556 }
1557
1558 return err;
1559}
1560
c93e0f6c
HC
1561COMPAT_SYSCALL_DEFINE3(set_mempolicy, int, mode, compat_ulong_t __user *, nmask,
1562 compat_ulong_t, maxnode)
1da177e4
LT
1563{
1564 long err = 0;
1565 unsigned long __user *nm = NULL;
1566 unsigned long nr_bits, alloc_size;
1567 DECLARE_BITMAP(bm, MAX_NUMNODES);
1568
1569 nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
1570 alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1571
1572 if (nmask) {
1573 err = compat_get_bitmap(bm, nmask, nr_bits);
1574 nm = compat_alloc_user_space(alloc_size);
1575 err |= copy_to_user(nm, bm, alloc_size);
1576 }
1577
1578 if (err)
1579 return -EFAULT;
1580
1581 return sys_set_mempolicy(mode, nm, nr_bits+1);
1582}
1583
c93e0f6c
HC
1584COMPAT_SYSCALL_DEFINE6(mbind, compat_ulong_t, start, compat_ulong_t, len,
1585 compat_ulong_t, mode, compat_ulong_t __user *, nmask,
1586 compat_ulong_t, maxnode, compat_ulong_t, flags)
1da177e4
LT
1587{
1588 long err = 0;
1589 unsigned long __user *nm = NULL;
1590 unsigned long nr_bits, alloc_size;
dfcd3c0d 1591 nodemask_t bm;
1da177e4
LT
1592
1593 nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
1594 alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1595
1596 if (nmask) {
dfcd3c0d 1597 err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
1da177e4 1598 nm = compat_alloc_user_space(alloc_size);
dfcd3c0d 1599 err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
1da177e4
LT
1600 }
1601
1602 if (err)
1603 return -EFAULT;
1604
1605 return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
1606}
1607
1608#endif
1609
480eccf9
LS
1610/*
1611 * get_vma_policy(@task, @vma, @addr)
b46e14ac
FF
1612 * @task: task for fallback if vma policy == default
1613 * @vma: virtual memory area whose policy is sought
1614 * @addr: address in @vma for shared policy lookup
480eccf9
LS
1615 *
1616 * Returns effective policy for a VMA at specified address.
1617 * Falls back to @task or system default policy, as necessary.
32f8516a
DR
1618 * Current or other task's task mempolicy and non-shared vma policies must be
1619 * protected by task_lock(task) by the caller.
52cd3b07
LS
1620 * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
1621 * count--added by the get_policy() vm_op, as appropriate--to protect against
1622 * freeing by another task. It is the caller's responsibility to free the
1623 * extra reference for shared policies.
480eccf9 1624 */
d98f6cb6 1625struct mempolicy *get_vma_policy(struct task_struct *task,
48fce342 1626 struct vm_area_struct *vma, unsigned long addr)
1da177e4 1627{
5606e387 1628 struct mempolicy *pol = get_task_policy(task);
1da177e4
LT
1629
1630 if (vma) {
480eccf9 1631 if (vma->vm_ops && vma->vm_ops->get_policy) {
ae4d8c16
LS
1632 struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
1633 addr);
1634 if (vpol)
1635 pol = vpol;
00442ad0 1636 } else if (vma->vm_policy) {
1da177e4 1637 pol = vma->vm_policy;
00442ad0
MG
1638
1639 /*
1640 * shmem_alloc_page() passes MPOL_F_SHARED policy with
1641 * a pseudo vma whose vma->vm_ops=NULL. Take a reference
1642 * count on these policies which will be dropped by
1643 * mpol_cond_put() later
1644 */
1645 if (mpol_needs_cond_ref(pol))
1646 mpol_get(pol);
1647 }
1da177e4
LT
1648 }
1649 if (!pol)
1650 pol = &default_policy;
1651 return pol;
1652}
1653
fc314724
MG
1654bool vma_policy_mof(struct task_struct *task, struct vm_area_struct *vma)
1655{
1656 struct mempolicy *pol = get_task_policy(task);
1657 if (vma) {
1658 if (vma->vm_ops && vma->vm_ops->get_policy) {
1659 bool ret = false;
1660
1661 pol = vma->vm_ops->get_policy(vma, vma->vm_start);
1662 if (pol && (pol->flags & MPOL_F_MOF))
1663 ret = true;
1664 mpol_cond_put(pol);
1665
1666 return ret;
1667 } else if (vma->vm_policy) {
1668 pol = vma->vm_policy;
1669 }
1670 }
1671
1672 if (!pol)
1673 return default_policy.flags & MPOL_F_MOF;
1674
1675 return pol->flags & MPOL_F_MOF;
1676}
1677
d3eb1570
LJ
1678static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
1679{
1680 enum zone_type dynamic_policy_zone = policy_zone;
1681
1682 BUG_ON(dynamic_policy_zone == ZONE_MOVABLE);
1683
1684 /*
1685 * if policy->v.nodes has movable memory only,
1686 * we apply policy when gfp_zone(gfp) = ZONE_MOVABLE only.
1687 *
1688 * policy->v.nodes is intersect with node_states[N_MEMORY].
1689 * so if the following test faile, it implies
1690 * policy->v.nodes has movable memory only.
1691 */
1692 if (!nodes_intersects(policy->v.nodes, node_states[N_HIGH_MEMORY]))
1693 dynamic_policy_zone = ZONE_MOVABLE;
1694
1695 return zone >= dynamic_policy_zone;
1696}
1697
52cd3b07
LS
1698/*
1699 * Return a nodemask representing a mempolicy for filtering nodes for
1700 * page allocation
1701 */
1702static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
19770b32
MG
1703{
1704 /* Lower zones don't get a nodemask applied for MPOL_BIND */
45c4745a 1705 if (unlikely(policy->mode == MPOL_BIND) &&
d3eb1570 1706 apply_policy_zone(policy, gfp_zone(gfp)) &&
19770b32
MG
1707 cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
1708 return &policy->v.nodes;
1709
1710 return NULL;
1711}
1712
52cd3b07 1713/* Return a zonelist indicated by gfp for node representing a mempolicy */
2f5f9486
AK
1714static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy,
1715 int nd)
1da177e4 1716{
45c4745a 1717 switch (policy->mode) {
1da177e4 1718 case MPOL_PREFERRED:
fc36b8d3
LS
1719 if (!(policy->flags & MPOL_F_LOCAL))
1720 nd = policy->v.preferred_node;
1da177e4
LT
1721 break;
1722 case MPOL_BIND:
19770b32 1723 /*
52cd3b07
LS
1724 * Normally, MPOL_BIND allocations are node-local within the
1725 * allowed nodemask. However, if __GFP_THISNODE is set and the
6eb27e1f 1726 * current node isn't part of the mask, we use the zonelist for
52cd3b07 1727 * the first node in the mask instead.
19770b32 1728 */
19770b32
MG
1729 if (unlikely(gfp & __GFP_THISNODE) &&
1730 unlikely(!node_isset(nd, policy->v.nodes)))
1731 nd = first_node(policy->v.nodes);
1732 break;
1da177e4 1733 default:
1da177e4
LT
1734 BUG();
1735 }
0e88460d 1736 return node_zonelist(nd, gfp);
1da177e4
LT
1737}
1738
1739/* Do dynamic interleaving for a process */
1740static unsigned interleave_nodes(struct mempolicy *policy)
1741{
1742 unsigned nid, next;
1743 struct task_struct *me = current;
1744
1745 nid = me->il_next;
dfcd3c0d 1746 next = next_node(nid, policy->v.nodes);
1da177e4 1747 if (next >= MAX_NUMNODES)
dfcd3c0d 1748 next = first_node(policy->v.nodes);
f5b087b5
DR
1749 if (next < MAX_NUMNODES)
1750 me->il_next = next;
1da177e4
LT
1751 return nid;
1752}
1753
dc85da15
CL
1754/*
1755 * Depending on the memory policy provide a node from which to allocate the
1756 * next slab entry.
1757 */
2a389610 1758unsigned int mempolicy_slab_node(void)
dc85da15 1759{
e7b691b0 1760 struct mempolicy *policy;
2a389610 1761 int node = numa_mem_id();
e7b691b0
AK
1762
1763 if (in_interrupt())
2a389610 1764 return node;
e7b691b0
AK
1765
1766 policy = current->mempolicy;
fc36b8d3 1767 if (!policy || policy->flags & MPOL_F_LOCAL)
2a389610 1768 return node;
bea904d5
LS
1769
1770 switch (policy->mode) {
1771 case MPOL_PREFERRED:
fc36b8d3
LS
1772 /*
1773 * handled MPOL_F_LOCAL above
1774 */
1775 return policy->v.preferred_node;
765c4507 1776
dc85da15
CL
1777 case MPOL_INTERLEAVE:
1778 return interleave_nodes(policy);
1779
dd1a239f 1780 case MPOL_BIND: {
dc85da15
CL
1781 /*
1782 * Follow bind policy behavior and start allocation at the
1783 * first node.
1784 */
19770b32
MG
1785 struct zonelist *zonelist;
1786 struct zone *zone;
1787 enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
2a389610 1788 zonelist = &NODE_DATA(node)->node_zonelists[0];
19770b32
MG
1789 (void)first_zones_zonelist(zonelist, highest_zoneidx,
1790 &policy->v.nodes,
1791 &zone);
2a389610 1792 return zone ? zone->node : node;
dd1a239f 1793 }
dc85da15 1794
dc85da15 1795 default:
bea904d5 1796 BUG();
dc85da15
CL
1797 }
1798}
1799
1da177e4
LT
1800/* Do static interleaving for a VMA with known offset. */
1801static unsigned offset_il_node(struct mempolicy *pol,
1802 struct vm_area_struct *vma, unsigned long off)
1803{
dfcd3c0d 1804 unsigned nnodes = nodes_weight(pol->v.nodes);
f5b087b5 1805 unsigned target;
1da177e4 1806 int c;
b76ac7e7 1807 int nid = NUMA_NO_NODE;
1da177e4 1808
f5b087b5
DR
1809 if (!nnodes)
1810 return numa_node_id();
1811 target = (unsigned int)off % nnodes;
1da177e4
LT
1812 c = 0;
1813 do {
dfcd3c0d 1814 nid = next_node(nid, pol->v.nodes);
1da177e4
LT
1815 c++;
1816 } while (c <= target);
1da177e4
LT
1817 return nid;
1818}
1819
5da7ca86
CL
1820/* Determine a node number for interleave */
1821static inline unsigned interleave_nid(struct mempolicy *pol,
1822 struct vm_area_struct *vma, unsigned long addr, int shift)
1823{
1824 if (vma) {
1825 unsigned long off;
1826
3b98b087
NA
1827 /*
1828 * for small pages, there is no difference between
1829 * shift and PAGE_SHIFT, so the bit-shift is safe.
1830 * for huge pages, since vm_pgoff is in units of small
1831 * pages, we need to shift off the always 0 bits to get
1832 * a useful offset.
1833 */
1834 BUG_ON(shift < PAGE_SHIFT);
1835 off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
5da7ca86
CL
1836 off += (addr - vma->vm_start) >> shift;
1837 return offset_il_node(pol, vma, off);
1838 } else
1839 return interleave_nodes(pol);
1840}
1841
778d3b0f
MH
1842/*
1843 * Return the bit number of a random bit set in the nodemask.
b76ac7e7 1844 * (returns NUMA_NO_NODE if nodemask is empty)
778d3b0f
MH
1845 */
1846int node_random(const nodemask_t *maskp)
1847{
b76ac7e7 1848 int w, bit = NUMA_NO_NODE;
778d3b0f
MH
1849
1850 w = nodes_weight(*maskp);
1851 if (w)
1852 bit = bitmap_ord_to_pos(maskp->bits,
1853 get_random_int() % w, MAX_NUMNODES);
1854 return bit;
1855}
1856
00ac59ad 1857#ifdef CONFIG_HUGETLBFS
480eccf9
LS
1858/*
1859 * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
b46e14ac
FF
1860 * @vma: virtual memory area whose policy is sought
1861 * @addr: address in @vma for shared policy lookup and interleave policy
1862 * @gfp_flags: for requested zone
1863 * @mpol: pointer to mempolicy pointer for reference counted mempolicy
1864 * @nodemask: pointer to nodemask pointer for MPOL_BIND nodemask
480eccf9 1865 *
52cd3b07
LS
1866 * Returns a zonelist suitable for a huge page allocation and a pointer
1867 * to the struct mempolicy for conditional unref after allocation.
1868 * If the effective policy is 'BIND, returns a pointer to the mempolicy's
1869 * @nodemask for filtering the zonelist.
c0ff7453 1870 *
d26914d1 1871 * Must be protected by read_mems_allowed_begin()
480eccf9 1872 */
396faf03 1873struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
19770b32
MG
1874 gfp_t gfp_flags, struct mempolicy **mpol,
1875 nodemask_t **nodemask)
5da7ca86 1876{
480eccf9 1877 struct zonelist *zl;
5da7ca86 1878
52cd3b07 1879 *mpol = get_vma_policy(current, vma, addr);
19770b32 1880 *nodemask = NULL; /* assume !MPOL_BIND */
5da7ca86 1881
52cd3b07
LS
1882 if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
1883 zl = node_zonelist(interleave_nid(*mpol, vma, addr,
a5516438 1884 huge_page_shift(hstate_vma(vma))), gfp_flags);
52cd3b07 1885 } else {
2f5f9486 1886 zl = policy_zonelist(gfp_flags, *mpol, numa_node_id());
52cd3b07
LS
1887 if ((*mpol)->mode == MPOL_BIND)
1888 *nodemask = &(*mpol)->v.nodes;
480eccf9
LS
1889 }
1890 return zl;
5da7ca86 1891}
06808b08
LS
1892
1893/*
1894 * init_nodemask_of_mempolicy
1895 *
1896 * If the current task's mempolicy is "default" [NULL], return 'false'
1897 * to indicate default policy. Otherwise, extract the policy nodemask
1898 * for 'bind' or 'interleave' policy into the argument nodemask, or
1899 * initialize the argument nodemask to contain the single node for
1900 * 'preferred' or 'local' policy and return 'true' to indicate presence
1901 * of non-default mempolicy.
1902 *
1903 * We don't bother with reference counting the mempolicy [mpol_get/put]
1904 * because the current task is examining it's own mempolicy and a task's
1905 * mempolicy is only ever changed by the task itself.
1906 *
1907 * N.B., it is the caller's responsibility to free a returned nodemask.
1908 */
1909bool init_nodemask_of_mempolicy(nodemask_t *mask)
1910{
1911 struct mempolicy *mempolicy;
1912 int nid;
1913
1914 if (!(mask && current->mempolicy))
1915 return false;
1916
c0ff7453 1917 task_lock(current);
06808b08
LS
1918 mempolicy = current->mempolicy;
1919 switch (mempolicy->mode) {
1920 case MPOL_PREFERRED:
1921 if (mempolicy->flags & MPOL_F_LOCAL)
1922 nid = numa_node_id();
1923 else
1924 nid = mempolicy->v.preferred_node;
1925 init_nodemask_of_node(mask, nid);
1926 break;
1927
1928 case MPOL_BIND:
1929 /* Fall through */
1930 case MPOL_INTERLEAVE:
1931 *mask = mempolicy->v.nodes;
1932 break;
1933
1934 default:
1935 BUG();
1936 }
c0ff7453 1937 task_unlock(current);
06808b08
LS
1938
1939 return true;
1940}
00ac59ad 1941#endif
5da7ca86 1942
6f48d0eb
DR
1943/*
1944 * mempolicy_nodemask_intersects
1945 *
1946 * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
1947 * policy. Otherwise, check for intersection between mask and the policy
1948 * nodemask for 'bind' or 'interleave' policy. For 'perferred' or 'local'
1949 * policy, always return true since it may allocate elsewhere on fallback.
1950 *
1951 * Takes task_lock(tsk) to prevent freeing of its mempolicy.
1952 */
1953bool mempolicy_nodemask_intersects(struct task_struct *tsk,
1954 const nodemask_t *mask)
1955{
1956 struct mempolicy *mempolicy;
1957 bool ret = true;
1958
1959 if (!mask)
1960 return ret;
1961 task_lock(tsk);
1962 mempolicy = tsk->mempolicy;
1963 if (!mempolicy)
1964 goto out;
1965
1966 switch (mempolicy->mode) {
1967 case MPOL_PREFERRED:
1968 /*
1969 * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
1970 * allocate from, they may fallback to other nodes when oom.
1971 * Thus, it's possible for tsk to have allocated memory from
1972 * nodes in mask.
1973 */
1974 break;
1975 case MPOL_BIND:
1976 case MPOL_INTERLEAVE:
1977 ret = nodes_intersects(mempolicy->v.nodes, *mask);
1978 break;
1979 default:
1980 BUG();
1981 }
1982out:
1983 task_unlock(tsk);
1984 return ret;
1985}
1986
1da177e4
LT
1987/* Allocate a page in interleaved policy.
1988 Own path because it needs to do special accounting. */
662f3a0b
AK
1989static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1990 unsigned nid)
1da177e4
LT
1991{
1992 struct zonelist *zl;
1993 struct page *page;
1994
0e88460d 1995 zl = node_zonelist(nid, gfp);
1da177e4 1996 page = __alloc_pages(gfp, order, zl);
dd1a239f 1997 if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
ca889e6c 1998 inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
1da177e4
LT
1999 return page;
2000}
2001
2002/**
0bbbc0b3 2003 * alloc_pages_vma - Allocate a page for a VMA.
1da177e4
LT
2004 *
2005 * @gfp:
2006 * %GFP_USER user allocation.
2007 * %GFP_KERNEL kernel allocations,
2008 * %GFP_HIGHMEM highmem/user allocations,
2009 * %GFP_FS allocation should not call back into a file system.
2010 * %GFP_ATOMIC don't sleep.
2011 *
0bbbc0b3 2012 * @order:Order of the GFP allocation.
1da177e4
LT
2013 * @vma: Pointer to VMA or NULL if not available.
2014 * @addr: Virtual Address of the allocation. Must be inside the VMA.
2015 *
2016 * This function allocates a page from the kernel page pool and applies
2017 * a NUMA policy associated with the VMA or the current process.
2018 * When VMA is not NULL caller must hold down_read on the mmap_sem of the
2019 * mm_struct of the VMA to prevent it from going away. Should be used for
2020 * all allocations for pages that will be mapped into
2021 * user space. Returns NULL when no page can be allocated.
2022 *
2023 * Should be called with the mm_sem of the vma hold.
2024 */
2025struct page *
0bbbc0b3 2026alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
2f5f9486 2027 unsigned long addr, int node)
1da177e4 2028{
cc9a6c87 2029 struct mempolicy *pol;
c0ff7453 2030 struct page *page;
cc9a6c87
MG
2031 unsigned int cpuset_mems_cookie;
2032
2033retry_cpuset:
2034 pol = get_vma_policy(current, vma, addr);
d26914d1 2035 cpuset_mems_cookie = read_mems_allowed_begin();
1da177e4 2036
45c4745a 2037 if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
1da177e4 2038 unsigned nid;
5da7ca86 2039
8eac563c 2040 nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
52cd3b07 2041 mpol_cond_put(pol);
0bbbc0b3 2042 page = alloc_page_interleave(gfp, order, nid);
d26914d1 2043 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
cc9a6c87
MG
2044 goto retry_cpuset;
2045
c0ff7453 2046 return page;
1da177e4 2047 }
212a0a6f
DR
2048 page = __alloc_pages_nodemask(gfp, order,
2049 policy_zonelist(gfp, pol, node),
0bbbc0b3 2050 policy_nodemask(gfp, pol));
212a0a6f
DR
2051 if (unlikely(mpol_needs_cond_ref(pol)))
2052 __mpol_put(pol);
d26914d1 2053 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
cc9a6c87 2054 goto retry_cpuset;
c0ff7453 2055 return page;
1da177e4
LT
2056}
2057
2058/**
2059 * alloc_pages_current - Allocate pages.
2060 *
2061 * @gfp:
2062 * %GFP_USER user allocation,
2063 * %GFP_KERNEL kernel allocation,
2064 * %GFP_HIGHMEM highmem allocation,
2065 * %GFP_FS don't call back into a file system.
2066 * %GFP_ATOMIC don't sleep.
2067 * @order: Power of two of allocation size in pages. 0 is a single page.
2068 *
2069 * Allocate a page from the kernel page pool. When not in
2070 * interrupt context and apply the current process NUMA policy.
2071 * Returns NULL when no page can be allocated.
2072 *
cf2a473c 2073 * Don't call cpuset_update_task_memory_state() unless
1da177e4
LT
2074 * 1) it's ok to take cpuset_sem (can WAIT), and
2075 * 2) allocating for current task (not interrupt).
2076 */
dd0fc66f 2077struct page *alloc_pages_current(gfp_t gfp, unsigned order)
1da177e4 2078{
5606e387 2079 struct mempolicy *pol = get_task_policy(current);
c0ff7453 2080 struct page *page;
cc9a6c87 2081 unsigned int cpuset_mems_cookie;
1da177e4 2082
9b819d20 2083 if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
1da177e4 2084 pol = &default_policy;
52cd3b07 2085
cc9a6c87 2086retry_cpuset:
d26914d1 2087 cpuset_mems_cookie = read_mems_allowed_begin();
cc9a6c87 2088
52cd3b07
LS
2089 /*
2090 * No reference counting needed for current->mempolicy
2091 * nor system default_policy
2092 */
45c4745a 2093 if (pol->mode == MPOL_INTERLEAVE)
c0ff7453
MX
2094 page = alloc_page_interleave(gfp, order, interleave_nodes(pol));
2095 else
2096 page = __alloc_pages_nodemask(gfp, order,
5c4b4be3
AK
2097 policy_zonelist(gfp, pol, numa_node_id()),
2098 policy_nodemask(gfp, pol));
cc9a6c87 2099
d26914d1 2100 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
cc9a6c87
MG
2101 goto retry_cpuset;
2102
c0ff7453 2103 return page;
1da177e4
LT
2104}
2105EXPORT_SYMBOL(alloc_pages_current);
2106
ef0855d3
ON
2107int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
2108{
2109 struct mempolicy *pol = mpol_dup(vma_policy(src));
2110
2111 if (IS_ERR(pol))
2112 return PTR_ERR(pol);
2113 dst->vm_policy = pol;
2114 return 0;
2115}
2116
4225399a 2117/*
846a16bf 2118 * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
4225399a
PJ
2119 * rebinds the mempolicy its copying by calling mpol_rebind_policy()
2120 * with the mems_allowed returned by cpuset_mems_allowed(). This
2121 * keeps mempolicies cpuset relative after its cpuset moves. See
2122 * further kernel/cpuset.c update_nodemask().
708c1bbc
MX
2123 *
2124 * current's mempolicy may be rebinded by the other task(the task that changes
2125 * cpuset's mems), so we needn't do rebind work for current task.
4225399a 2126 */
4225399a 2127
846a16bf
LS
2128/* Slow path of a mempolicy duplicate */
2129struct mempolicy *__mpol_dup(struct mempolicy *old)
1da177e4
LT
2130{
2131 struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2132
2133 if (!new)
2134 return ERR_PTR(-ENOMEM);
708c1bbc
MX
2135
2136 /* task's mempolicy is protected by alloc_lock */
2137 if (old == current->mempolicy) {
2138 task_lock(current);
2139 *new = *old;
2140 task_unlock(current);
2141 } else
2142 *new = *old;
2143
99ee4ca7 2144 rcu_read_lock();
4225399a
PJ
2145 if (current_cpuset_is_being_rebound()) {
2146 nodemask_t mems = cpuset_mems_allowed(current);
708c1bbc
MX
2147 if (new->flags & MPOL_F_REBINDING)
2148 mpol_rebind_policy(new, &mems, MPOL_REBIND_STEP2);
2149 else
2150 mpol_rebind_policy(new, &mems, MPOL_REBIND_ONCE);
4225399a 2151 }
99ee4ca7 2152 rcu_read_unlock();
1da177e4 2153 atomic_set(&new->refcnt, 1);
1da177e4
LT
2154 return new;
2155}
2156
2157/* Slow path of a mempolicy comparison */
fcfb4dcc 2158bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
1da177e4
LT
2159{
2160 if (!a || !b)
fcfb4dcc 2161 return false;
45c4745a 2162 if (a->mode != b->mode)
fcfb4dcc 2163 return false;
19800502 2164 if (a->flags != b->flags)
fcfb4dcc 2165 return false;
19800502
BL
2166 if (mpol_store_user_nodemask(a))
2167 if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
fcfb4dcc 2168 return false;
19800502 2169
45c4745a 2170 switch (a->mode) {
19770b32
MG
2171 case MPOL_BIND:
2172 /* Fall through */
1da177e4 2173 case MPOL_INTERLEAVE:
fcfb4dcc 2174 return !!nodes_equal(a->v.nodes, b->v.nodes);
1da177e4 2175 case MPOL_PREFERRED:
75719661 2176 return a->v.preferred_node == b->v.preferred_node;
1da177e4
LT
2177 default:
2178 BUG();
fcfb4dcc 2179 return false;
1da177e4
LT
2180 }
2181}
2182
1da177e4
LT
2183/*
2184 * Shared memory backing store policy support.
2185 *
2186 * Remember policies even when nobody has shared memory mapped.
2187 * The policies are kept in Red-Black tree linked from the inode.
2188 * They are protected by the sp->lock spinlock, which should be held
2189 * for any accesses to the tree.
2190 */
2191
2192/* lookup first element intersecting start-end */
42288fe3 2193/* Caller holds sp->lock */
1da177e4
LT
2194static struct sp_node *
2195sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
2196{
2197 struct rb_node *n = sp->root.rb_node;
2198
2199 while (n) {
2200 struct sp_node *p = rb_entry(n, struct sp_node, nd);
2201
2202 if (start >= p->end)
2203 n = n->rb_right;
2204 else if (end <= p->start)
2205 n = n->rb_left;
2206 else
2207 break;
2208 }
2209 if (!n)
2210 return NULL;
2211 for (;;) {
2212 struct sp_node *w = NULL;
2213 struct rb_node *prev = rb_prev(n);
2214 if (!prev)
2215 break;
2216 w = rb_entry(prev, struct sp_node, nd);
2217 if (w->end <= start)
2218 break;
2219 n = prev;
2220 }
2221 return rb_entry(n, struct sp_node, nd);
2222}
2223
2224/* Insert a new shared policy into the list. */
2225/* Caller holds sp->lock */
2226static void sp_insert(struct shared_policy *sp, struct sp_node *new)
2227{
2228 struct rb_node **p = &sp->root.rb_node;
2229 struct rb_node *parent = NULL;
2230 struct sp_node *nd;
2231
2232 while (*p) {
2233 parent = *p;
2234 nd = rb_entry(parent, struct sp_node, nd);
2235 if (new->start < nd->start)
2236 p = &(*p)->rb_left;
2237 else if (new->end > nd->end)
2238 p = &(*p)->rb_right;
2239 else
2240 BUG();
2241 }
2242 rb_link_node(&new->nd, parent, p);
2243 rb_insert_color(&new->nd, &sp->root);
140d5a49 2244 pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
45c4745a 2245 new->policy ? new->policy->mode : 0);
1da177e4
LT
2246}
2247
2248/* Find shared policy intersecting idx */
2249struct mempolicy *
2250mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
2251{
2252 struct mempolicy *pol = NULL;
2253 struct sp_node *sn;
2254
2255 if (!sp->root.rb_node)
2256 return NULL;
42288fe3 2257 spin_lock(&sp->lock);
1da177e4
LT
2258 sn = sp_lookup(sp, idx, idx+1);
2259 if (sn) {
2260 mpol_get(sn->policy);
2261 pol = sn->policy;
2262 }
42288fe3 2263 spin_unlock(&sp->lock);
1da177e4
LT
2264 return pol;
2265}
2266
63f74ca2
KM
2267static void sp_free(struct sp_node *n)
2268{
2269 mpol_put(n->policy);
2270 kmem_cache_free(sn_cache, n);
2271}
2272
771fb4d8
LS
2273/**
2274 * mpol_misplaced - check whether current page node is valid in policy
2275 *
b46e14ac
FF
2276 * @page: page to be checked
2277 * @vma: vm area where page mapped
2278 * @addr: virtual address where page mapped
771fb4d8
LS
2279 *
2280 * Lookup current policy node id for vma,addr and "compare to" page's
2281 * node id.
2282 *
2283 * Returns:
2284 * -1 - not misplaced, page is in the right node
2285 * node - node id where the page should be
2286 *
2287 * Policy determination "mimics" alloc_page_vma().
2288 * Called from fault path where we know the vma and faulting address.
2289 */
2290int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long addr)
2291{
2292 struct mempolicy *pol;
2293 struct zone *zone;
2294 int curnid = page_to_nid(page);
2295 unsigned long pgoff;
90572890
PZ
2296 int thiscpu = raw_smp_processor_id();
2297 int thisnid = cpu_to_node(thiscpu);
771fb4d8
LS
2298 int polnid = -1;
2299 int ret = -1;
2300
2301 BUG_ON(!vma);
2302
2303 pol = get_vma_policy(current, vma, addr);
2304 if (!(pol->flags & MPOL_F_MOF))
2305 goto out;
2306
2307 switch (pol->mode) {
2308 case MPOL_INTERLEAVE:
2309 BUG_ON(addr >= vma->vm_end);
2310 BUG_ON(addr < vma->vm_start);
2311
2312 pgoff = vma->vm_pgoff;
2313 pgoff += (addr - vma->vm_start) >> PAGE_SHIFT;
2314 polnid = offset_il_node(pol, vma, pgoff);
2315 break;
2316
2317 case MPOL_PREFERRED:
2318 if (pol->flags & MPOL_F_LOCAL)
2319 polnid = numa_node_id();
2320 else
2321 polnid = pol->v.preferred_node;
2322 break;
2323
2324 case MPOL_BIND:
2325 /*
2326 * allows binding to multiple nodes.
2327 * use current page if in policy nodemask,
2328 * else select nearest allowed node, if any.
2329 * If no allowed nodes, use current [!misplaced].
2330 */
2331 if (node_isset(curnid, pol->v.nodes))
2332 goto out;
2333 (void)first_zones_zonelist(
2334 node_zonelist(numa_node_id(), GFP_HIGHUSER),
2335 gfp_zone(GFP_HIGHUSER),
2336 &pol->v.nodes, &zone);
2337 polnid = zone->node;
2338 break;
2339
2340 default:
2341 BUG();
2342 }
5606e387
MG
2343
2344 /* Migrate the page towards the node whose CPU is referencing it */
e42c8ff2 2345 if (pol->flags & MPOL_F_MORON) {
90572890 2346 polnid = thisnid;
5606e387 2347
10f39042 2348 if (!should_numa_migrate_memory(current, page, curnid, thiscpu))
de1c9ce6 2349 goto out;
e42c8ff2
MG
2350 }
2351
771fb4d8
LS
2352 if (curnid != polnid)
2353 ret = polnid;
2354out:
2355 mpol_cond_put(pol);
2356
2357 return ret;
2358}
2359
1da177e4
LT
2360static void sp_delete(struct shared_policy *sp, struct sp_node *n)
2361{
140d5a49 2362 pr_debug("deleting %lx-l%lx\n", n->start, n->end);
1da177e4 2363 rb_erase(&n->nd, &sp->root);
63f74ca2 2364 sp_free(n);
1da177e4
LT
2365}
2366
42288fe3
MG
2367static void sp_node_init(struct sp_node *node, unsigned long start,
2368 unsigned long end, struct mempolicy *pol)
2369{
2370 node->start = start;
2371 node->end = end;
2372 node->policy = pol;
2373}
2374
dbcb0f19
AB
2375static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
2376 struct mempolicy *pol)
1da177e4 2377{
869833f2
KM
2378 struct sp_node *n;
2379 struct mempolicy *newpol;
1da177e4 2380
869833f2 2381 n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
1da177e4
LT
2382 if (!n)
2383 return NULL;
869833f2
KM
2384
2385 newpol = mpol_dup(pol);
2386 if (IS_ERR(newpol)) {
2387 kmem_cache_free(sn_cache, n);
2388 return NULL;
2389 }
2390 newpol->flags |= MPOL_F_SHARED;
42288fe3 2391 sp_node_init(n, start, end, newpol);
869833f2 2392
1da177e4
LT
2393 return n;
2394}
2395
2396/* Replace a policy range. */
2397static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
2398 unsigned long end, struct sp_node *new)
2399{
b22d127a 2400 struct sp_node *n;
42288fe3
MG
2401 struct sp_node *n_new = NULL;
2402 struct mempolicy *mpol_new = NULL;
b22d127a 2403 int ret = 0;
1da177e4 2404
42288fe3
MG
2405restart:
2406 spin_lock(&sp->lock);
1da177e4
LT
2407 n = sp_lookup(sp, start, end);
2408 /* Take care of old policies in the same range. */
2409 while (n && n->start < end) {
2410 struct rb_node *next = rb_next(&n->nd);
2411 if (n->start >= start) {
2412 if (n->end <= end)
2413 sp_delete(sp, n);
2414 else
2415 n->start = end;
2416 } else {
2417 /* Old policy spanning whole new range. */
2418 if (n->end > end) {
42288fe3
MG
2419 if (!n_new)
2420 goto alloc_new;
2421
2422 *mpol_new = *n->policy;
2423 atomic_set(&mpol_new->refcnt, 1);
7880639c 2424 sp_node_init(n_new, end, n->end, mpol_new);
1da177e4 2425 n->end = start;
5ca39575 2426 sp_insert(sp, n_new);
42288fe3
MG
2427 n_new = NULL;
2428 mpol_new = NULL;
1da177e4
LT
2429 break;
2430 } else
2431 n->end = start;
2432 }
2433 if (!next)
2434 break;
2435 n = rb_entry(next, struct sp_node, nd);
2436 }
2437 if (new)
2438 sp_insert(sp, new);
42288fe3
MG
2439 spin_unlock(&sp->lock);
2440 ret = 0;
2441
2442err_out:
2443 if (mpol_new)
2444 mpol_put(mpol_new);
2445 if (n_new)
2446 kmem_cache_free(sn_cache, n_new);
2447
b22d127a 2448 return ret;
42288fe3
MG
2449
2450alloc_new:
2451 spin_unlock(&sp->lock);
2452 ret = -ENOMEM;
2453 n_new = kmem_cache_alloc(sn_cache, GFP_KERNEL);
2454 if (!n_new)
2455 goto err_out;
2456 mpol_new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2457 if (!mpol_new)
2458 goto err_out;
2459 goto restart;
1da177e4
LT
2460}
2461
71fe804b
LS
2462/**
2463 * mpol_shared_policy_init - initialize shared policy for inode
2464 * @sp: pointer to inode shared policy
2465 * @mpol: struct mempolicy to install
2466 *
2467 * Install non-NULL @mpol in inode's shared policy rb-tree.
2468 * On entry, the current task has a reference on a non-NULL @mpol.
2469 * This must be released on exit.
4bfc4495 2470 * This is called at get_inode() calls and we can use GFP_KERNEL.
71fe804b
LS
2471 */
2472void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
2473{
58568d2a
MX
2474 int ret;
2475
71fe804b 2476 sp->root = RB_ROOT; /* empty tree == default mempolicy */
42288fe3 2477 spin_lock_init(&sp->lock);
71fe804b
LS
2478
2479 if (mpol) {
2480 struct vm_area_struct pvma;
2481 struct mempolicy *new;
4bfc4495 2482 NODEMASK_SCRATCH(scratch);
71fe804b 2483
4bfc4495 2484 if (!scratch)
5c0c1654 2485 goto put_mpol;
71fe804b
LS
2486 /* contextualize the tmpfs mount point mempolicy */
2487 new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
15d77835 2488 if (IS_ERR(new))
0cae3457 2489 goto free_scratch; /* no valid nodemask intersection */
58568d2a
MX
2490
2491 task_lock(current);
4bfc4495 2492 ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch);
58568d2a 2493 task_unlock(current);
15d77835 2494 if (ret)
5c0c1654 2495 goto put_new;
71fe804b
LS
2496
2497 /* Create pseudo-vma that contains just the policy */
2498 memset(&pvma, 0, sizeof(struct vm_area_struct));
2499 pvma.vm_end = TASK_SIZE; /* policy covers entire file */
2500 mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
15d77835 2501
5c0c1654 2502put_new:
71fe804b 2503 mpol_put(new); /* drop initial ref */
0cae3457 2504free_scratch:
4bfc4495 2505 NODEMASK_SCRATCH_FREE(scratch);
5c0c1654
LS
2506put_mpol:
2507 mpol_put(mpol); /* drop our incoming ref on sb mpol */
7339ff83
RH
2508 }
2509}
2510
1da177e4
LT
2511int mpol_set_shared_policy(struct shared_policy *info,
2512 struct vm_area_struct *vma, struct mempolicy *npol)
2513{
2514 int err;
2515 struct sp_node *new = NULL;
2516 unsigned long sz = vma_pages(vma);
2517
028fec41 2518 pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
1da177e4 2519 vma->vm_pgoff,
45c4745a 2520 sz, npol ? npol->mode : -1,
028fec41 2521 npol ? npol->flags : -1,
00ef2d2f 2522 npol ? nodes_addr(npol->v.nodes)[0] : NUMA_NO_NODE);
1da177e4
LT
2523
2524 if (npol) {
2525 new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
2526 if (!new)
2527 return -ENOMEM;
2528 }
2529 err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
2530 if (err && new)
63f74ca2 2531 sp_free(new);
1da177e4
LT
2532 return err;
2533}
2534
2535/* Free a backing policy store on inode delete. */
2536void mpol_free_shared_policy(struct shared_policy *p)
2537{
2538 struct sp_node *n;
2539 struct rb_node *next;
2540
2541 if (!p->root.rb_node)
2542 return;
42288fe3 2543 spin_lock(&p->lock);
1da177e4
LT
2544 next = rb_first(&p->root);
2545 while (next) {
2546 n = rb_entry(next, struct sp_node, nd);
2547 next = rb_next(&n->nd);
63f74ca2 2548 sp_delete(p, n);
1da177e4 2549 }
42288fe3 2550 spin_unlock(&p->lock);
1da177e4
LT
2551}
2552
1a687c2e 2553#ifdef CONFIG_NUMA_BALANCING
c297663c 2554static int __initdata numabalancing_override;
1a687c2e
MG
2555
2556static void __init check_numabalancing_enable(void)
2557{
2558 bool numabalancing_default = false;
2559
2560 if (IS_ENABLED(CONFIG_NUMA_BALANCING_DEFAULT_ENABLED))
2561 numabalancing_default = true;
2562
c297663c
MG
2563 /* Parsed by setup_numabalancing. override == 1 enables, -1 disables */
2564 if (numabalancing_override)
2565 set_numabalancing_state(numabalancing_override == 1);
2566
1a687c2e 2567 if (nr_node_ids > 1 && !numabalancing_override) {
4a404bea 2568 pr_info("%s automatic NUMA balancing. "
c297663c
MG
2569 "Configure with numa_balancing= or the "
2570 "kernel.numa_balancing sysctl",
2571 numabalancing_default ? "Enabling" : "Disabling");
1a687c2e
MG
2572 set_numabalancing_state(numabalancing_default);
2573 }
2574}
2575
2576static int __init setup_numabalancing(char *str)
2577{
2578 int ret = 0;
2579 if (!str)
2580 goto out;
1a687c2e
MG
2581
2582 if (!strcmp(str, "enable")) {
c297663c 2583 numabalancing_override = 1;
1a687c2e
MG
2584 ret = 1;
2585 } else if (!strcmp(str, "disable")) {
c297663c 2586 numabalancing_override = -1;
1a687c2e
MG
2587 ret = 1;
2588 }
2589out:
2590 if (!ret)
4a404bea 2591 pr_warn("Unable to parse numa_balancing=\n");
1a687c2e
MG
2592
2593 return ret;
2594}
2595__setup("numa_balancing=", setup_numabalancing);
2596#else
2597static inline void __init check_numabalancing_enable(void)
2598{
2599}
2600#endif /* CONFIG_NUMA_BALANCING */
2601
1da177e4
LT
2602/* assumes fs == KERNEL_DS */
2603void __init numa_policy_init(void)
2604{
b71636e2
PM
2605 nodemask_t interleave_nodes;
2606 unsigned long largest = 0;
2607 int nid, prefer = 0;
2608
1da177e4
LT
2609 policy_cache = kmem_cache_create("numa_policy",
2610 sizeof(struct mempolicy),
20c2df83 2611 0, SLAB_PANIC, NULL);
1da177e4
LT
2612
2613 sn_cache = kmem_cache_create("shared_policy_node",
2614 sizeof(struct sp_node),
20c2df83 2615 0, SLAB_PANIC, NULL);
1da177e4 2616
5606e387
MG
2617 for_each_node(nid) {
2618 preferred_node_policy[nid] = (struct mempolicy) {
2619 .refcnt = ATOMIC_INIT(1),
2620 .mode = MPOL_PREFERRED,
2621 .flags = MPOL_F_MOF | MPOL_F_MORON,
2622 .v = { .preferred_node = nid, },
2623 };
2624 }
2625
b71636e2
PM
2626 /*
2627 * Set interleaving policy for system init. Interleaving is only
2628 * enabled across suitably sized nodes (default is >= 16MB), or
2629 * fall back to the largest node if they're all smaller.
2630 */
2631 nodes_clear(interleave_nodes);
01f13bd6 2632 for_each_node_state(nid, N_MEMORY) {
b71636e2
PM
2633 unsigned long total_pages = node_present_pages(nid);
2634
2635 /* Preserve the largest node */
2636 if (largest < total_pages) {
2637 largest = total_pages;
2638 prefer = nid;
2639 }
2640
2641 /* Interleave this node? */
2642 if ((total_pages << PAGE_SHIFT) >= (16 << 20))
2643 node_set(nid, interleave_nodes);
2644 }
2645
2646 /* All too small, use the largest */
2647 if (unlikely(nodes_empty(interleave_nodes)))
2648 node_set(prefer, interleave_nodes);
1da177e4 2649
028fec41 2650 if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
b1de0d13 2651 pr_err("%s: interleaving failed\n", __func__);
1a687c2e
MG
2652
2653 check_numabalancing_enable();
1da177e4
LT
2654}
2655
8bccd85f 2656/* Reset policy of current process to default */
1da177e4
LT
2657void numa_default_policy(void)
2658{
028fec41 2659 do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
1da177e4 2660}
68860ec1 2661
095f1fc4
LS
2662/*
2663 * Parse and format mempolicy from/to strings
2664 */
2665
1a75a6c8 2666/*
f2a07f40 2667 * "local" is implemented internally by MPOL_PREFERRED with MPOL_F_LOCAL flag.
1a75a6c8 2668 */
345ace9c
LS
2669static const char * const policy_modes[] =
2670{
2671 [MPOL_DEFAULT] = "default",
2672 [MPOL_PREFERRED] = "prefer",
2673 [MPOL_BIND] = "bind",
2674 [MPOL_INTERLEAVE] = "interleave",
d3a71033 2675 [MPOL_LOCAL] = "local",
345ace9c 2676};
1a75a6c8 2677
095f1fc4
LS
2678
2679#ifdef CONFIG_TMPFS
2680/**
f2a07f40 2681 * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option.
095f1fc4 2682 * @str: string containing mempolicy to parse
71fe804b 2683 * @mpol: pointer to struct mempolicy pointer, returned on success.
095f1fc4
LS
2684 *
2685 * Format of input:
2686 * <mode>[=<flags>][:<nodelist>]
2687 *
71fe804b 2688 * On success, returns 0, else 1
095f1fc4 2689 */
a7a88b23 2690int mpol_parse_str(char *str, struct mempolicy **mpol)
095f1fc4 2691{
71fe804b 2692 struct mempolicy *new = NULL;
b4652e84 2693 unsigned short mode;
f2a07f40 2694 unsigned short mode_flags;
71fe804b 2695 nodemask_t nodes;
095f1fc4
LS
2696 char *nodelist = strchr(str, ':');
2697 char *flags = strchr(str, '=');
095f1fc4
LS
2698 int err = 1;
2699
2700 if (nodelist) {
2701 /* NUL-terminate mode or flags string */
2702 *nodelist++ = '\0';
71fe804b 2703 if (nodelist_parse(nodelist, nodes))
095f1fc4 2704 goto out;
01f13bd6 2705 if (!nodes_subset(nodes, node_states[N_MEMORY]))
095f1fc4 2706 goto out;
71fe804b
LS
2707 } else
2708 nodes_clear(nodes);
2709
095f1fc4
LS
2710 if (flags)
2711 *flags++ = '\0'; /* terminate mode string */
2712
479e2802 2713 for (mode = 0; mode < MPOL_MAX; mode++) {
345ace9c 2714 if (!strcmp(str, policy_modes[mode])) {
095f1fc4
LS
2715 break;
2716 }
2717 }
a720094d 2718 if (mode >= MPOL_MAX)
095f1fc4
LS
2719 goto out;
2720
71fe804b 2721 switch (mode) {
095f1fc4 2722 case MPOL_PREFERRED:
71fe804b
LS
2723 /*
2724 * Insist on a nodelist of one node only
2725 */
095f1fc4
LS
2726 if (nodelist) {
2727 char *rest = nodelist;
2728 while (isdigit(*rest))
2729 rest++;
926f2ae0
KM
2730 if (*rest)
2731 goto out;
095f1fc4
LS
2732 }
2733 break;
095f1fc4
LS
2734 case MPOL_INTERLEAVE:
2735 /*
2736 * Default to online nodes with memory if no nodelist
2737 */
2738 if (!nodelist)
01f13bd6 2739 nodes = node_states[N_MEMORY];
3f226aa1 2740 break;
71fe804b 2741 case MPOL_LOCAL:
3f226aa1 2742 /*
71fe804b 2743 * Don't allow a nodelist; mpol_new() checks flags
3f226aa1 2744 */
71fe804b 2745 if (nodelist)
3f226aa1 2746 goto out;
71fe804b 2747 mode = MPOL_PREFERRED;
3f226aa1 2748 break;
413b43de
RT
2749 case MPOL_DEFAULT:
2750 /*
2751 * Insist on a empty nodelist
2752 */
2753 if (!nodelist)
2754 err = 0;
2755 goto out;
d69b2e63
KM
2756 case MPOL_BIND:
2757 /*
2758 * Insist on a nodelist
2759 */
2760 if (!nodelist)
2761 goto out;
095f1fc4
LS
2762 }
2763
71fe804b 2764 mode_flags = 0;
095f1fc4
LS
2765 if (flags) {
2766 /*
2767 * Currently, we only support two mutually exclusive
2768 * mode flags.
2769 */
2770 if (!strcmp(flags, "static"))
71fe804b 2771 mode_flags |= MPOL_F_STATIC_NODES;
095f1fc4 2772 else if (!strcmp(flags, "relative"))
71fe804b 2773 mode_flags |= MPOL_F_RELATIVE_NODES;
095f1fc4 2774 else
926f2ae0 2775 goto out;
095f1fc4 2776 }
71fe804b
LS
2777
2778 new = mpol_new(mode, mode_flags, &nodes);
2779 if (IS_ERR(new))
926f2ae0
KM
2780 goto out;
2781
f2a07f40
HD
2782 /*
2783 * Save nodes for mpol_to_str() to show the tmpfs mount options
2784 * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo.
2785 */
2786 if (mode != MPOL_PREFERRED)
2787 new->v.nodes = nodes;
2788 else if (nodelist)
2789 new->v.preferred_node = first_node(nodes);
2790 else
2791 new->flags |= MPOL_F_LOCAL;
2792
2793 /*
2794 * Save nodes for contextualization: this will be used to "clone"
2795 * the mempolicy in a specific context [cpuset] at a later time.
2796 */
2797 new->w.user_nodemask = nodes;
2798
926f2ae0 2799 err = 0;
71fe804b 2800
095f1fc4
LS
2801out:
2802 /* Restore string for error message */
2803 if (nodelist)
2804 *--nodelist = ':';
2805 if (flags)
2806 *--flags = '=';
71fe804b
LS
2807 if (!err)
2808 *mpol = new;
095f1fc4
LS
2809 return err;
2810}
2811#endif /* CONFIG_TMPFS */
2812
71fe804b
LS
2813/**
2814 * mpol_to_str - format a mempolicy structure for printing
2815 * @buffer: to contain formatted mempolicy string
2816 * @maxlen: length of @buffer
2817 * @pol: pointer to mempolicy to be formatted
71fe804b 2818 *
948927ee
DR
2819 * Convert @pol into a string. If @buffer is too short, truncate the string.
2820 * Recommend a @maxlen of at least 32 for the longest mode, "interleave", the
2821 * longest flag, "relative", and to display at least a few node ids.
1a75a6c8 2822 */
948927ee 2823void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
1a75a6c8
CL
2824{
2825 char *p = buffer;
948927ee
DR
2826 nodemask_t nodes = NODE_MASK_NONE;
2827 unsigned short mode = MPOL_DEFAULT;
2828 unsigned short flags = 0;
2291990a 2829
8790c71a 2830 if (pol && pol != &default_policy && !(pol->flags & MPOL_F_MORON)) {
bea904d5 2831 mode = pol->mode;
948927ee
DR
2832 flags = pol->flags;
2833 }
bea904d5 2834
1a75a6c8
CL
2835 switch (mode) {
2836 case MPOL_DEFAULT:
1a75a6c8 2837 break;
1a75a6c8 2838 case MPOL_PREFERRED:
fc36b8d3 2839 if (flags & MPOL_F_LOCAL)
f2a07f40 2840 mode = MPOL_LOCAL;
53f2556b 2841 else
fc36b8d3 2842 node_set(pol->v.preferred_node, nodes);
1a75a6c8 2843 break;
1a75a6c8 2844 case MPOL_BIND:
1a75a6c8 2845 case MPOL_INTERLEAVE:
f2a07f40 2846 nodes = pol->v.nodes;
1a75a6c8 2847 break;
1a75a6c8 2848 default:
948927ee
DR
2849 WARN_ON_ONCE(1);
2850 snprintf(p, maxlen, "unknown");
2851 return;
1a75a6c8
CL
2852 }
2853
b7a9f420 2854 p += snprintf(p, maxlen, "%s", policy_modes[mode]);
1a75a6c8 2855
fc36b8d3 2856 if (flags & MPOL_MODE_FLAGS) {
948927ee 2857 p += snprintf(p, buffer + maxlen - p, "=");
f5b087b5 2858
2291990a
LS
2859 /*
2860 * Currently, the only defined flags are mutually exclusive
2861 */
f5b087b5 2862 if (flags & MPOL_F_STATIC_NODES)
2291990a
LS
2863 p += snprintf(p, buffer + maxlen - p, "static");
2864 else if (flags & MPOL_F_RELATIVE_NODES)
2865 p += snprintf(p, buffer + maxlen - p, "relative");
f5b087b5
DR
2866 }
2867
1a75a6c8 2868 if (!nodes_empty(nodes)) {
948927ee 2869 p += snprintf(p, buffer + maxlen - p, ":");
1a75a6c8
CL
2870 p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
2871 }
1a75a6c8 2872}
This page took 1.083329 seconds and 5 git commands to generate.