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