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