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