1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple NUMA memory policy for the Linux kernel.
5 * Copyright 2003,2004 Andi Kleen, SuSE Labs.
6 * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
8 * NUMA policy allows the user to give hints in which node(s) memory should
11 * Support six policies per VMA and per process:
13 * The VMA policy has priority over the process policy for a page fault.
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
23 * Allocate memory interleaved over a set of nodes based on
24 * a set of weights (per-node), with normal fallback if it
25 * fails. Otherwise operates the same as interleave.
26 * Example: nodeset(0,1) & weights (2,1) - 2 pages allocated
27 * on node 0 for every 1 page allocated on node 1.
29 * bind Only allocate memory on a specific set of nodes,
31 * FIXME: memory is allocated starting with the first node
32 * to the last. It would be better if bind would truly restrict
33 * the allocation to memory nodes instead
35 * preferred Try a specific node first before normal fallback.
36 * As a special case NUMA_NO_NODE here means do the allocation
37 * on the local CPU. This is normally identical to default,
38 * but useful to set in a VMA when you have a non default
41 * preferred many Try a set of nodes first before normal fallback. This is
42 * similar to preferred without the special case.
44 * default Allocate on the local node first, or when on a VMA
45 * use the process policy. This is what Linux always did
46 * in a NUMA aware kernel and still does by, ahem, default.
48 * The process policy is applied for most non interrupt memory allocations
49 * in that process' context. Interrupts ignore the policies and always
50 * try to allocate on the local CPU. The VMA policy is only applied for memory
51 * allocations for a VMA in the VM.
53 * Currently there are a few corner cases in swapping where the policy
54 * is not applied, but the majority should be handled. When process policy
55 * is used it is not remembered over swap outs/swap ins.
57 * Only the highest zone in the zone hierarchy gets policied. Allocations
58 * requesting a lower zone just use default policy. This implies that
59 * on systems with highmem kernel lowmem allocation don't get policied.
60 * Same with GFP_DMA allocations.
62 * For shmem/tmpfs shared memory the policy is shared between
63 * all users and remembered even when nobody has memory mapped.
67 fix mmap readahead to honour policy and enable policy for any page cache
69 statistics for bigpages
70 global policy for page cache? currently it uses process policy. Requires
72 handle mremap for shared memory (currently ignored for the policy)
74 make bind policy root only? It can trigger oom much faster and the
75 kernel is not always grateful with that.
78 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
80 #include <linux/mempolicy.h>
81 #include <linux/pagewalk.h>
82 #include <linux/highmem.h>
83 #include <linux/hugetlb.h>
84 #include <linux/kernel.h>
85 #include <linux/sched.h>
86 #include <linux/sched/mm.h>
87 #include <linux/sched/numa_balancing.h>
88 #include <linux/sched/task.h>
89 #include <linux/nodemask.h>
90 #include <linux/cpuset.h>
91 #include <linux/slab.h>
92 #include <linux/string.h>
93 #include <linux/export.h>
94 #include <linux/nsproxy.h>
95 #include <linux/interrupt.h>
96 #include <linux/init.h>
97 #include <linux/compat.h>
98 #include <linux/ptrace.h>
99 #include <linux/swap.h>
100 #include <linux/seq_file.h>
101 #include <linux/proc_fs.h>
102 #include <linux/migrate.h>
103 #include <linux/ksm.h>
104 #include <linux/rmap.h>
105 #include <linux/security.h>
106 #include <linux/syscalls.h>
107 #include <linux/ctype.h>
108 #include <linux/mm_inline.h>
109 #include <linux/mmu_notifier.h>
110 #include <linux/printk.h>
111 #include <linux/swapops.h>
112 #include <linux/gcd.h>
114 #include <asm/tlbflush.h>
116 #include <linux/uaccess.h>
117 #include <linux/memory.h>
119 #include "internal.h"
122 #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */
123 #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */
124 #define MPOL_MF_WRLOCK (MPOL_MF_INTERNAL << 2) /* Write-lock walked vmas */
126 static struct kmem_cache *policy_cache;
127 static struct kmem_cache *sn_cache;
129 /* Highest zone. An specific allocation for a zone below that is not
131 enum zone_type policy_zone = 0;
134 * run-time system-wide default policy => local allocation
136 static struct mempolicy default_policy = {
137 .refcnt = ATOMIC_INIT(1), /* never free it */
141 static struct mempolicy preferred_node_policy[MAX_NUMNODES];
144 * weightiness balances the tradeoff between small weights (cycles through nodes
145 * faster, more fair/even distribution) and large weights (smaller errors
146 * between actual bandwidth ratios and weight ratios). 32 is a number that has
147 * been found to perform at a reasonable compromise between the two goals.
149 static const int weightiness = 32;
152 * A null weighted_interleave_state is interpreted as having .mode="auto",
153 * and .iw_table is interpreted as an array of 1s with length nr_node_ids.
155 struct weighted_interleave_state {
159 static struct weighted_interleave_state __rcu *wi_state;
160 static unsigned int *node_bw_table;
163 * wi_state_lock protects both wi_state and node_bw_table.
164 * node_bw_table is only used by writers to update wi_state.
166 static DEFINE_MUTEX(wi_state_lock);
168 static u8 get_il_weight(int node)
170 struct weighted_interleave_state *state;
174 state = rcu_dereference(wi_state);
176 weight = state->iw_table[node];
182 * Convert bandwidth values into weighted interleave weights.
183 * Call with wi_state_lock.
185 static void reduce_interleave_weights(unsigned int *bw, u8 *new_iw)
188 unsigned int cast_sum_bw, scaling_factor = 1, iw_gcd = 0;
191 for_each_node_state(nid, N_MEMORY)
194 /* Scale bandwidths to whole numbers in the range [1, weightiness] */
195 for_each_node_state(nid, N_MEMORY) {
197 * Try not to perform 64-bit division.
198 * If sum_bw < scaling_factor, then sum_bw < U32_MAX.
199 * If sum_bw > scaling_factor, then round the weight up to 1.
201 scaling_factor = weightiness * bw[nid];
202 if (bw[nid] && sum_bw < scaling_factor) {
203 cast_sum_bw = (unsigned int)sum_bw;
204 new_iw[nid] = scaling_factor / cast_sum_bw;
209 iw_gcd = new_iw[nid];
210 iw_gcd = gcd(iw_gcd, new_iw[nid]);
213 /* 1:2 is strictly better than 16:32. Reduce by the weights' GCD. */
214 for_each_node_state(nid, N_MEMORY)
215 new_iw[nid] /= iw_gcd;
218 int mempolicy_set_node_perf(unsigned int node, struct access_coordinate *coords)
220 struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL;
221 unsigned int *old_bw, *new_bw;
225 bw_val = min(coords->read_bandwidth, coords->write_bandwidth);
226 new_bw = kcalloc(nr_node_ids, sizeof(unsigned int), GFP_KERNEL);
230 new_wi_state = kmalloc(struct_size(new_wi_state, iw_table, nr_node_ids),
236 new_wi_state->mode_auto = true;
237 for (i = 0; i < nr_node_ids; i++)
238 new_wi_state->iw_table[i] = 1;
241 * Update bandwidth info, even in manual mode. That way, when switching
242 * to auto mode in the future, iw_table can be overwritten using
245 mutex_lock(&wi_state_lock);
247 old_bw = node_bw_table;
249 memcpy(new_bw, old_bw, nr_node_ids * sizeof(*old_bw));
250 new_bw[node] = bw_val;
251 node_bw_table = new_bw;
253 old_wi_state = rcu_dereference_protected(wi_state,
254 lockdep_is_held(&wi_state_lock));
255 if (old_wi_state && !old_wi_state->mode_auto) {
256 /* Manual mode; skip reducing weights and updating wi_state */
257 mutex_unlock(&wi_state_lock);
262 /* NULL wi_state assumes auto=true; reduce weights and update wi_state*/
263 reduce_interleave_weights(new_bw, new_wi_state->iw_table);
264 rcu_assign_pointer(wi_state, new_wi_state);
266 mutex_unlock(&wi_state_lock);
277 * numa_nearest_node - Find nearest node by state
278 * @node: Node id to start the search
279 * @state: State to filter the search
281 * Lookup the closest node by distance if @nid is not in state.
283 * Return: this @node if it is in state, otherwise the closest node by distance
285 int numa_nearest_node(int node, unsigned int state)
287 int min_dist = INT_MAX, dist, n, min_node;
289 if (state >= NR_NODE_STATES)
292 if (node == NUMA_NO_NODE || node_state(node, state))
296 for_each_node_state(n, state) {
297 dist = node_distance(node, n);
298 if (dist < min_dist) {
306 EXPORT_SYMBOL_GPL(numa_nearest_node);
309 * nearest_node_nodemask - Find the node in @mask at the nearest distance
312 * @node: a valid node ID to start the search from.
313 * @mask: a pointer to a nodemask representing the allowed nodes.
315 * This function iterates over all nodes in @mask and calculates the
316 * distance from the starting @node, then it returns the node ID that is
317 * the closest to @node, or MAX_NUMNODES if no node is found.
319 * Note that @node must be a valid node ID usable with node_distance(),
320 * providing an invalid node ID (e.g., NUMA_NO_NODE) may result in crashes
321 * or unexpected behavior.
323 int nearest_node_nodemask(int node, nodemask_t *mask)
325 int dist, n, min_dist = INT_MAX, min_node = MAX_NUMNODES;
327 for_each_node_mask(n, *mask) {
328 dist = node_distance(node, n);
329 if (dist < min_dist) {
337 EXPORT_SYMBOL_GPL(nearest_node_nodemask);
339 struct mempolicy *get_task_policy(struct task_struct *p)
341 struct mempolicy *pol = p->mempolicy;
347 node = numa_node_id();
348 if (node != NUMA_NO_NODE) {
349 pol = &preferred_node_policy[node];
350 /* preferred_node_policy is not initialised early in boot */
355 return &default_policy;
358 static const struct mempolicy_operations {
359 int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
360 void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
361 } mpol_ops[MPOL_MAX];
363 static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
365 return pol->flags & MPOL_MODE_FLAGS;
368 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
369 const nodemask_t *rel)
372 nodes_fold(tmp, *orig, nodes_weight(*rel));
373 nodes_onto(*ret, tmp, *rel);
376 static int mpol_new_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
378 if (nodes_empty(*nodes))
384 static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
386 if (nodes_empty(*nodes))
389 nodes_clear(pol->nodes);
390 node_set(first_node(*nodes), pol->nodes);
395 * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
396 * any, for the new policy. mpol_new() has already validated the nodes
397 * parameter with respect to the policy mode and flags.
399 * Must be called holding task's alloc_lock to protect task's mems_allowed
400 * and mempolicy. May also be called holding the mmap_lock for write.
402 static int mpol_set_nodemask(struct mempolicy *pol,
403 const nodemask_t *nodes, struct nodemask_scratch *nsc)
408 * Default (pol==NULL) resp. local memory policies are not a
409 * subject of any remapping. They also do not need any special
412 if (!pol || pol->mode == MPOL_LOCAL)
416 nodes_and(nsc->mask1,
417 cpuset_current_mems_allowed, node_states[N_MEMORY]);
421 if (pol->flags & MPOL_F_RELATIVE_NODES)
422 mpol_relative_nodemask(&nsc->mask2, nodes, &nsc->mask1);
424 nodes_and(nsc->mask2, *nodes, nsc->mask1);
426 if (mpol_store_user_nodemask(pol))
427 pol->w.user_nodemask = *nodes;
429 pol->w.cpuset_mems_allowed = cpuset_current_mems_allowed;
431 ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
436 * This function just creates a new policy, does some check and simple
437 * initialization. You must invoke mpol_set_nodemask() to set nodes.
439 static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
442 struct mempolicy *policy;
444 if (mode == MPOL_DEFAULT) {
445 if (nodes && !nodes_empty(*nodes))
446 return ERR_PTR(-EINVAL);
452 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
453 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
454 * All other modes require a valid pointer to a non-empty nodemask.
456 if (mode == MPOL_PREFERRED) {
457 if (nodes_empty(*nodes)) {
458 if (((flags & MPOL_F_STATIC_NODES) ||
459 (flags & MPOL_F_RELATIVE_NODES)))
460 return ERR_PTR(-EINVAL);
464 } else if (mode == MPOL_LOCAL) {
465 if (!nodes_empty(*nodes) ||
466 (flags & MPOL_F_STATIC_NODES) ||
467 (flags & MPOL_F_RELATIVE_NODES))
468 return ERR_PTR(-EINVAL);
469 } else if (nodes_empty(*nodes))
470 return ERR_PTR(-EINVAL);
472 policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
474 return ERR_PTR(-ENOMEM);
475 atomic_set(&policy->refcnt, 1);
477 policy->flags = flags;
478 policy->home_node = NUMA_NO_NODE;
483 /* Slow path of a mpol destructor. */
484 void __mpol_put(struct mempolicy *pol)
486 if (!atomic_dec_and_test(&pol->refcnt))
488 kmem_cache_free(policy_cache, pol);
491 static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes)
495 static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
499 if (pol->flags & MPOL_F_STATIC_NODES)
500 nodes_and(tmp, pol->w.user_nodemask, *nodes);
501 else if (pol->flags & MPOL_F_RELATIVE_NODES)
502 mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
504 nodes_remap(tmp, pol->nodes, pol->w.cpuset_mems_allowed,
506 pol->w.cpuset_mems_allowed = *nodes;
509 if (nodes_empty(tmp))
515 static void mpol_rebind_preferred(struct mempolicy *pol,
516 const nodemask_t *nodes)
518 pol->w.cpuset_mems_allowed = *nodes;
522 * mpol_rebind_policy - Migrate a policy to a different set of nodes
524 * Per-vma policies are protected by mmap_lock. Allocations using per-task
525 * policies are protected by task->mems_allowed_seq to prevent a premature
526 * OOM/allocation failure due to parallel nodemask modification.
528 static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask)
530 if (!pol || pol->mode == MPOL_LOCAL)
532 if (!mpol_store_user_nodemask(pol) &&
533 nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
536 mpol_ops[pol->mode].rebind(pol, newmask);
540 * Wrapper for mpol_rebind_policy() that just requires task
541 * pointer, and updates task mempolicy.
543 * Called with task's alloc_lock held.
545 void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new)
547 mpol_rebind_policy(tsk->mempolicy, new);
551 * Rebind each vma in mm to new nodemask.
553 * Call holding a reference to mm. Takes mm->mmap_lock during call.
555 void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
557 struct vm_area_struct *vma;
558 VMA_ITERATOR(vmi, mm, 0);
561 for_each_vma(vmi, vma) {
562 vma_start_write(vma);
563 mpol_rebind_policy(vma->vm_policy, new);
565 mmap_write_unlock(mm);
568 static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
570 .rebind = mpol_rebind_default,
572 [MPOL_INTERLEAVE] = {
573 .create = mpol_new_nodemask,
574 .rebind = mpol_rebind_nodemask,
577 .create = mpol_new_preferred,
578 .rebind = mpol_rebind_preferred,
581 .create = mpol_new_nodemask,
582 .rebind = mpol_rebind_nodemask,
585 .rebind = mpol_rebind_default,
587 [MPOL_PREFERRED_MANY] = {
588 .create = mpol_new_nodemask,
589 .rebind = mpol_rebind_preferred,
591 [MPOL_WEIGHTED_INTERLEAVE] = {
592 .create = mpol_new_nodemask,
593 .rebind = mpol_rebind_nodemask,
597 static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist,
598 unsigned long flags);
599 static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *pol,
600 pgoff_t ilx, int *nid);
602 static bool strictly_unmovable(unsigned long flags)
605 * STRICT without MOVE flags lets do_mbind() fail immediately with -EIO
606 * if any misplaced page is found.
608 return (flags & (MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) ==
612 struct migration_mpol { /* for alloc_migration_target_by_mpol() */
613 struct mempolicy *pol;
618 struct list_head *pagelist;
623 struct vm_area_struct *first;
624 struct folio *large; /* note last large folio encountered */
625 long nr_failed; /* could not be isolated at this time */
629 * Check if the folio's nid is in qp->nmask.
631 * If MPOL_MF_INVERT is set in qp->flags, check if the nid is
632 * in the invert of qp->nmask.
634 static inline bool queue_folio_required(struct folio *folio,
635 struct queue_pages *qp)
637 int nid = folio_nid(folio);
638 unsigned long flags = qp->flags;
640 return node_isset(nid, *qp->nmask) == !(flags & MPOL_MF_INVERT);
643 static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk)
646 struct queue_pages *qp = walk->private;
648 if (unlikely(is_pmd_migration_entry(*pmd))) {
652 folio = pmd_folio(*pmd);
653 if (is_huge_zero_folio(folio)) {
654 walk->action = ACTION_CONTINUE;
657 if (!queue_folio_required(folio, qp))
659 if (!(qp->flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) ||
660 !vma_migratable(walk->vma) ||
661 !migrate_folio_add(folio, qp->pagelist, qp->flags))
666 * Scan through folios, checking if they satisfy the required conditions,
667 * moving them from LRU to local pagelist for migration if they do (or not).
669 * queue_folios_pte_range() has two possible return values:
670 * 0 - continue walking to scan for more, even if an existing folio on the
671 * wrong node could not be isolated and queued for migration.
672 * -EIO - only MPOL_MF_STRICT was specified, without MPOL_MF_MOVE or ..._ALL,
673 * and an existing folio was on a node that does not follow the policy.
675 static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr,
676 unsigned long end, struct mm_walk *walk)
678 const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY;
679 struct vm_area_struct *vma = walk->vma;
681 struct queue_pages *qp = walk->private;
682 unsigned long flags = qp->flags;
683 pte_t *pte, *mapped_pte;
688 ptl = pmd_trans_huge_lock(pmd, vma);
690 queue_folios_pmd(pmd, walk);
695 mapped_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
697 walk->action = ACTION_AGAIN;
700 for (; addr != end; pte += nr, addr += nr * PAGE_SIZE) {
701 max_nr = (end - addr) >> PAGE_SHIFT;
703 ptent = ptep_get(pte);
706 if (!pte_present(ptent)) {
707 if (is_migration_entry(pte_to_swp_entry(ptent)))
711 folio = vm_normal_folio(vma, addr, ptent);
712 if (!folio || folio_is_zone_device(folio))
714 if (folio_test_large(folio) && max_nr != 1)
715 nr = folio_pte_batch(folio, addr, pte, ptent,
719 * vm_normal_folio() filters out zero pages, but there might
720 * still be reserved folios to skip, perhaps in a VDSO.
722 if (folio_test_reserved(folio))
724 if (!queue_folio_required(folio, qp))
726 if (folio_test_large(folio)) {
728 * A large folio can only be isolated from LRU once,
729 * but may be mapped by many PTEs (and Copy-On-Write may
730 * intersperse PTEs of other, order 0, folios). This is
731 * a common case, so don't mistake it for failure (but
732 * there can be other cases of multi-mapped pages which
733 * this quick check does not help to filter out - and a
734 * search of the pagelist might grow to be prohibitive).
736 * migrate_pages(&pagelist) returns nr_failed folios, so
737 * check "large" now so that queue_pages_range() returns
738 * a comparable nr_failed folios. This does imply that
739 * if folio could not be isolated for some racy reason
740 * at its first PTE, later PTEs will not give it another
741 * chance of isolation; but keeps the accounting simple.
743 if (folio == qp->large)
747 if (!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) ||
748 !vma_migratable(vma) ||
749 !migrate_folio_add(folio, qp->pagelist, flags)) {
751 if (strictly_unmovable(flags))
755 pte_unmap_unlock(mapped_pte, ptl);
758 if (qp->nr_failed && strictly_unmovable(flags))
763 static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask,
764 unsigned long addr, unsigned long end,
765 struct mm_walk *walk)
767 #ifdef CONFIG_HUGETLB_PAGE
768 struct queue_pages *qp = walk->private;
769 unsigned long flags = qp->flags;
774 ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
775 entry = huge_ptep_get(walk->mm, addr, pte);
776 if (!pte_present(entry)) {
777 if (unlikely(is_hugetlb_entry_migration(entry)))
781 folio = pfn_folio(pte_pfn(entry));
782 if (!queue_folio_required(folio, qp))
784 if (!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) ||
785 !vma_migratable(walk->vma)) {
790 * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio.
791 * Choosing not to migrate a shared folio is not counted as a failure.
793 * See folio_maybe_mapped_shared() on possible imprecision when we
794 * cannot easily detect if a folio is shared.
796 if ((flags & MPOL_MF_MOVE_ALL) ||
797 (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte)))
798 if (!folio_isolate_hugetlb(folio, qp->pagelist))
802 if (qp->nr_failed && strictly_unmovable(flags))
808 #ifdef CONFIG_NUMA_BALANCING
810 * This is used to mark a range of virtual addresses to be inaccessible.
811 * These are later cleared by a NUMA hinting fault. Depending on these
812 * faults, pages may be migrated for better NUMA placement.
814 * This is assuming that NUMA faults are handled using PROT_NONE. If
815 * an architecture makes a different choice, it will need further
816 * changes to the core.
818 unsigned long change_prot_numa(struct vm_area_struct *vma,
819 unsigned long addr, unsigned long end)
821 struct mmu_gather tlb;
824 tlb_gather_mmu(&tlb, vma->vm_mm);
826 nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA);
827 if (nr_updated > 0) {
828 count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
829 count_memcg_events_mm(vma->vm_mm, NUMA_PTE_UPDATES, nr_updated);
832 tlb_finish_mmu(&tlb);
836 #endif /* CONFIG_NUMA_BALANCING */
838 static int queue_pages_test_walk(unsigned long start, unsigned long end,
839 struct mm_walk *walk)
841 struct vm_area_struct *next, *vma = walk->vma;
842 struct queue_pages *qp = walk->private;
843 unsigned long flags = qp->flags;
845 /* range check first */
846 VM_BUG_ON_VMA(!range_in_vma(vma, start, end), vma);
850 if (!(flags & MPOL_MF_DISCONTIG_OK) &&
851 (qp->start < vma->vm_start))
852 /* hole at head side of range */
855 next = find_vma(vma->vm_mm, vma->vm_end);
856 if (!(flags & MPOL_MF_DISCONTIG_OK) &&
857 ((vma->vm_end < qp->end) &&
858 (!next || vma->vm_end < next->vm_start)))
859 /* hole at middle or tail of range */
863 * Need check MPOL_MF_STRICT to return -EIO if possible
864 * regardless of vma_migratable
866 if (!vma_migratable(vma) &&
867 !(flags & MPOL_MF_STRICT))
871 * Check page nodes, and queue pages to move, in the current vma.
872 * But if no moving, and no strict checking, the scan can be skipped.
874 if (flags & (MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
879 static const struct mm_walk_ops queue_pages_walk_ops = {
880 .hugetlb_entry = queue_folios_hugetlb,
881 .pmd_entry = queue_folios_pte_range,
882 .test_walk = queue_pages_test_walk,
883 .walk_lock = PGWALK_RDLOCK,
886 static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {
887 .hugetlb_entry = queue_folios_hugetlb,
888 .pmd_entry = queue_folios_pte_range,
889 .test_walk = queue_pages_test_walk,
890 .walk_lock = PGWALK_WRLOCK,
894 * Walk through page tables and collect pages to be migrated.
896 * If pages found in a given range are not on the required set of @nodes,
897 * and migration is allowed, they are isolated and queued to @pagelist.
899 * queue_pages_range() may return:
900 * 0 - all pages already on the right node, or successfully queued for moving
901 * (or neither strict checking nor moving requested: only range checking).
902 * >0 - this number of misplaced folios could not be queued for moving
903 * (a hugetlbfs page or a transparent huge page being counted as 1).
904 * -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs.
905 * -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified.
908 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
909 nodemask_t *nodes, unsigned long flags,
910 struct list_head *pagelist)
913 struct queue_pages qp = {
914 .pagelist = pagelist,
921 const struct mm_walk_ops *ops = (flags & MPOL_MF_WRLOCK) ?
922 &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops;
924 err = walk_page_range(mm, start, end, ops, &qp);
927 /* whole range in hole */
930 return err ? : qp.nr_failed;
934 * Apply policy to a single VMA
935 * This must be called with the mmap_lock held for writing.
937 static int vma_replace_policy(struct vm_area_struct *vma,
938 struct mempolicy *pol)
941 struct mempolicy *old;
942 struct mempolicy *new;
944 vma_assert_write_locked(vma);
950 if (vma->vm_ops && vma->vm_ops->set_policy) {
951 err = vma->vm_ops->set_policy(vma, new);
956 old = vma->vm_policy;
957 vma->vm_policy = new; /* protected by mmap_lock */
966 /* Split or merge the VMA (if required) and apply the new policy */
967 static int mbind_range(struct vma_iterator *vmi, struct vm_area_struct *vma,
968 struct vm_area_struct **prev, unsigned long start,
969 unsigned long end, struct mempolicy *new_pol)
971 unsigned long vmstart, vmend;
973 vmend = min(end, vma->vm_end);
974 if (start > vma->vm_start) {
978 vmstart = vma->vm_start;
981 if (mpol_equal(vma->vm_policy, new_pol)) {
986 vma = vma_modify_policy(vmi, *prev, vma, vmstart, vmend, new_pol);
991 return vma_replace_policy(vma, new_pol);
994 /* Set the process memory policy */
995 static long do_set_mempolicy(unsigned short mode, unsigned short flags,
998 struct mempolicy *new, *old;
999 NODEMASK_SCRATCH(scratch);
1005 new = mpol_new(mode, flags, nodes);
1012 ret = mpol_set_nodemask(new, nodes, scratch);
1014 task_unlock(current);
1019 old = current->mempolicy;
1020 current->mempolicy = new;
1021 if (new && (new->mode == MPOL_INTERLEAVE ||
1022 new->mode == MPOL_WEIGHTED_INTERLEAVE)) {
1023 current->il_prev = MAX_NUMNODES-1;
1024 current->il_weight = 0;
1026 task_unlock(current);
1030 NODEMASK_SCRATCH_FREE(scratch);
1035 * Return nodemask for policy for get_mempolicy() query
1037 * Called with task's alloc_lock held
1039 static void get_policy_nodemask(struct mempolicy *pol, nodemask_t *nodes)
1041 nodes_clear(*nodes);
1042 if (pol == &default_policy)
1045 switch (pol->mode) {
1047 case MPOL_INTERLEAVE:
1048 case MPOL_PREFERRED:
1049 case MPOL_PREFERRED_MANY:
1050 case MPOL_WEIGHTED_INTERLEAVE:
1051 *nodes = pol->nodes;
1054 /* return empty node mask for local allocation */
1061 static int lookup_node(struct mm_struct *mm, unsigned long addr)
1063 struct page *p = NULL;
1066 ret = get_user_pages_fast(addr & PAGE_MASK, 1, 0, &p);
1068 ret = page_to_nid(p);
1074 /* Retrieve NUMA policy */
1075 static long do_get_mempolicy(int *policy, nodemask_t *nmask,
1076 unsigned long addr, unsigned long flags)
1079 struct mm_struct *mm = current->mm;
1080 struct vm_area_struct *vma = NULL;
1081 struct mempolicy *pol = current->mempolicy, *pol_refcount = NULL;
1084 ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
1087 if (flags & MPOL_F_MEMS_ALLOWED) {
1088 if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
1090 *policy = 0; /* just so it's initialized */
1092 *nmask = cpuset_current_mems_allowed;
1093 task_unlock(current);
1097 if (flags & MPOL_F_ADDR) {
1098 pgoff_t ilx; /* ignored here */
1100 * Do NOT fall back to task policy if the
1101 * vma/shared policy at addr is NULL. We
1102 * want to return MPOL_DEFAULT in this case.
1105 vma = vma_lookup(mm, addr);
1107 mmap_read_unlock(mm);
1110 pol = __get_vma_policy(vma, addr, &ilx);
1115 pol = &default_policy; /* indicates default behavior */
1117 if (flags & MPOL_F_NODE) {
1118 if (flags & MPOL_F_ADDR) {
1120 * Take a refcount on the mpol, because we are about to
1121 * drop the mmap_lock, after which only "pol" remains
1122 * valid, "vma" is stale.
1127 mmap_read_unlock(mm);
1128 err = lookup_node(mm, addr);
1132 } else if (pol == current->mempolicy &&
1133 pol->mode == MPOL_INTERLEAVE) {
1134 *policy = next_node_in(current->il_prev, pol->nodes);
1135 } else if (pol == current->mempolicy &&
1136 pol->mode == MPOL_WEIGHTED_INTERLEAVE) {
1137 if (current->il_weight)
1138 *policy = current->il_prev;
1140 *policy = next_node_in(current->il_prev,
1147 *policy = pol == &default_policy ? MPOL_DEFAULT :
1150 * Internal mempolicy flags must be masked off before exposing
1151 * the policy to userspace.
1153 *policy |= (pol->flags & MPOL_MODE_FLAGS);
1158 if (mpol_store_user_nodemask(pol)) {
1159 *nmask = pol->w.user_nodemask;
1162 get_policy_nodemask(pol, nmask);
1163 task_unlock(current);
1170 mmap_read_unlock(mm);
1172 mpol_put(pol_refcount);
1176 #ifdef CONFIG_MIGRATION
1177 static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist,
1178 unsigned long flags)
1181 * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio.
1182 * Choosing not to migrate a shared folio is not counted as a failure.
1184 * See folio_maybe_mapped_shared() on possible imprecision when we
1185 * cannot easily detect if a folio is shared.
1187 if ((flags & MPOL_MF_MOVE_ALL) || !folio_maybe_mapped_shared(folio)) {
1188 if (folio_isolate_lru(folio)) {
1189 list_add_tail(&folio->lru, foliolist);
1190 node_stat_mod_folio(folio,
1191 NR_ISOLATED_ANON + folio_is_file_lru(folio),
1192 folio_nr_pages(folio));
1195 * Non-movable folio may reach here. And, there may be
1196 * temporary off LRU folios or non-LRU movable folios.
1197 * Treat them as unmovable folios since they can't be
1198 * isolated, so they can't be moved at the moment.
1207 * Migrate pages from one node to a target node.
1208 * Returns error or the number of pages not migrated.
1210 static long migrate_to_node(struct mm_struct *mm, int source, int dest,
1214 struct vm_area_struct *vma;
1215 LIST_HEAD(pagelist);
1218 struct migration_target_control mtc = {
1220 .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
1221 .reason = MR_SYSCALL,
1225 node_set(source, nmask);
1227 VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)));
1230 vma = find_vma(mm, 0);
1231 if (unlikely(!vma)) {
1232 mmap_read_unlock(mm);
1237 * This does not migrate the range, but isolates all pages that
1238 * need migration. Between passing in the full user address
1239 * space range and MPOL_MF_DISCONTIG_OK, this call cannot fail,
1240 * but passes back the count of pages which could not be isolated.
1242 nr_failed = queue_pages_range(mm, vma->vm_start, mm->task_size, &nmask,
1243 flags | MPOL_MF_DISCONTIG_OK, &pagelist);
1244 mmap_read_unlock(mm);
1246 if (!list_empty(&pagelist)) {
1247 err = migrate_pages(&pagelist, alloc_migration_target, NULL,
1248 (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
1250 putback_movable_pages(&pagelist);
1259 * Move pages between the two nodesets so as to preserve the physical
1260 * layout as much as possible.
1262 * Returns the number of page that could not be moved.
1264 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
1265 const nodemask_t *to, int flags)
1271 lru_cache_disable();
1274 * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
1275 * bit in 'to' is not also set in 'tmp'. Clear the found 'source'
1276 * bit in 'tmp', and return that <source, dest> pair for migration.
1277 * The pair of nodemasks 'to' and 'from' define the map.
1279 * If no pair of bits is found that way, fallback to picking some
1280 * pair of 'source' and 'dest' bits that are not the same. If the
1281 * 'source' and 'dest' bits are the same, this represents a node
1282 * that will be migrating to itself, so no pages need move.
1284 * If no bits are left in 'tmp', or if all remaining bits left
1285 * in 'tmp' correspond to the same bit in 'to', return false
1286 * (nothing left to migrate).
1288 * This lets us pick a pair of nodes to migrate between, such that
1289 * if possible the dest node is not already occupied by some other
1290 * source node, minimizing the risk of overloading the memory on a
1291 * node that would happen if we migrated incoming memory to a node
1292 * before migrating outgoing memory source that same node.
1294 * A single scan of tmp is sufficient. As we go, we remember the
1295 * most recent <s, d> pair that moved (s != d). If we find a pair
1296 * that not only moved, but what's better, moved to an empty slot
1297 * (d is not set in tmp), then we break out then, with that pair.
1298 * Otherwise when we finish scanning from_tmp, we at least have the
1299 * most recent <s, d> pair that moved. If we get all the way through
1300 * the scan of tmp without finding any node that moved, much less
1301 * moved to an empty node, then there is nothing left worth migrating.
1305 while (!nodes_empty(tmp)) {
1307 int source = NUMA_NO_NODE;
1310 for_each_node_mask(s, tmp) {
1313 * do_migrate_pages() tries to maintain the relative
1314 * node relationship of the pages established between
1315 * threads and memory areas.
1317 * However if the number of source nodes is not equal to
1318 * the number of destination nodes we can not preserve
1319 * this node relative relationship. In that case, skip
1320 * copying memory from a node that is in the destination
1323 * Example: [2,3,4] -> [3,4,5] moves everything.
1324 * [0-7] - > [3,4,5] moves only 0,1,2,6,7.
1327 if ((nodes_weight(*from) != nodes_weight(*to)) &&
1328 (node_isset(s, *to)))
1331 d = node_remap(s, *from, *to);
1335 source = s; /* Node moved. Memorize */
1338 /* dest not in remaining from nodes? */
1339 if (!node_isset(dest, tmp))
1342 if (source == NUMA_NO_NODE)
1345 node_clear(source, tmp);
1346 err = migrate_to_node(mm, source, dest, flags);
1356 return (nr_failed < INT_MAX) ? nr_failed : INT_MAX;
1360 * Allocate a new folio for page migration, according to NUMA mempolicy.
1362 static struct folio *alloc_migration_target_by_mpol(struct folio *src,
1363 unsigned long private)
1365 struct migration_mpol *mmpol = (struct migration_mpol *)private;
1366 struct mempolicy *pol = mmpol->pol;
1367 pgoff_t ilx = mmpol->ilx;
1369 int nid = numa_node_id();
1372 order = folio_order(src);
1373 ilx += src->index >> order;
1375 if (folio_test_hugetlb(src)) {
1376 nodemask_t *nodemask;
1379 h = folio_hstate(src);
1380 gfp = htlb_alloc_mask(h);
1381 nodemask = policy_nodemask(gfp, pol, ilx, &nid);
1382 return alloc_hugetlb_folio_nodemask(h, nid, nodemask, gfp,
1383 htlb_allow_alloc_fallback(MR_MEMPOLICY_MBIND));
1386 if (folio_test_large(src))
1387 gfp = GFP_TRANSHUGE;
1389 gfp = GFP_HIGHUSER_MOVABLE | __GFP_RETRY_MAYFAIL | __GFP_COMP;
1391 return folio_alloc_mpol(gfp, order, pol, ilx, nid);
1395 static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist,
1396 unsigned long flags)
1401 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
1402 const nodemask_t *to, int flags)
1407 static struct folio *alloc_migration_target_by_mpol(struct folio *src,
1408 unsigned long private)
1414 static long do_mbind(unsigned long start, unsigned long len,
1415 unsigned short mode, unsigned short mode_flags,
1416 nodemask_t *nmask, unsigned long flags)
1418 struct mm_struct *mm = current->mm;
1419 struct vm_area_struct *vma, *prev;
1420 struct vma_iterator vmi;
1421 struct migration_mpol mmpol;
1422 struct mempolicy *new;
1426 LIST_HEAD(pagelist);
1428 if (flags & ~(unsigned long)MPOL_MF_VALID)
1430 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1433 if (start & ~PAGE_MASK)
1436 if (mode == MPOL_DEFAULT)
1437 flags &= ~MPOL_MF_STRICT;
1439 len = PAGE_ALIGN(len);
1447 new = mpol_new(mode, mode_flags, nmask);
1449 return PTR_ERR(new);
1452 * If we are using the default policy then operation
1453 * on discontinuous address spaces is okay after all
1456 flags |= MPOL_MF_DISCONTIG_OK;
1458 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
1459 lru_cache_disable();
1461 NODEMASK_SCRATCH(scratch);
1463 mmap_write_lock(mm);
1464 err = mpol_set_nodemask(new, nmask, scratch);
1466 mmap_write_unlock(mm);
1469 NODEMASK_SCRATCH_FREE(scratch);
1475 * Lock the VMAs before scanning for pages to migrate,
1476 * to ensure we don't miss a concurrently inserted page.
1478 nr_failed = queue_pages_range(mm, start, end, nmask,
1479 flags | MPOL_MF_INVERT | MPOL_MF_WRLOCK, &pagelist);
1481 if (nr_failed < 0) {
1485 vma_iter_init(&vmi, mm, start);
1486 prev = vma_prev(&vmi);
1487 for_each_vma_range(vmi, vma, end) {
1488 err = mbind_range(&vmi, vma, &prev, start, end, new);
1494 if (!err && !list_empty(&pagelist)) {
1495 /* Convert MPOL_DEFAULT's NULL to task or default policy */
1497 new = get_task_policy(current);
1504 * In the interleaved case, attempt to allocate on exactly the
1505 * targeted nodes, for the first VMA to be migrated; for later
1506 * VMAs, the nodes will still be interleaved from the targeted
1507 * nodemask, but one by one may be selected differently.
1509 if (new->mode == MPOL_INTERLEAVE ||
1510 new->mode == MPOL_WEIGHTED_INTERLEAVE) {
1511 struct folio *folio;
1513 unsigned long addr = -EFAULT;
1515 list_for_each_entry(folio, &pagelist, lru) {
1516 if (!folio_test_ksm(folio))
1519 if (!list_entry_is_head(folio, &pagelist, lru)) {
1520 vma_iter_init(&vmi, mm, start);
1521 for_each_vma_range(vmi, vma, end) {
1522 addr = page_address_in_vma(folio,
1523 folio_page(folio, 0), vma);
1524 if (addr != -EFAULT)
1528 if (addr != -EFAULT) {
1529 order = folio_order(folio);
1530 /* We already know the pol, but not the ilx */
1531 mpol_cond_put(get_vma_policy(vma, addr, order,
1533 /* Set base from which to increment by index */
1534 mmpol.ilx -= folio->index >> order;
1539 mmap_write_unlock(mm);
1541 if (!err && !list_empty(&pagelist)) {
1542 nr_failed |= migrate_pages(&pagelist,
1543 alloc_migration_target_by_mpol, NULL,
1544 (unsigned long)&mmpol, MIGRATE_SYNC,
1545 MR_MEMPOLICY_MBIND, NULL);
1548 if (nr_failed && (flags & MPOL_MF_STRICT))
1550 if (!list_empty(&pagelist))
1551 putback_movable_pages(&pagelist);
1554 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
1560 * User space interface with variable sized bitmaps for nodelists.
1562 static int get_bitmap(unsigned long *mask, const unsigned long __user *nmask,
1563 unsigned long maxnode)
1565 unsigned long nlongs = BITS_TO_LONGS(maxnode);
1568 if (in_compat_syscall())
1569 ret = compat_get_bitmap(mask,
1570 (const compat_ulong_t __user *)nmask,
1573 ret = copy_from_user(mask, nmask,
1574 nlongs * sizeof(unsigned long));
1579 if (maxnode % BITS_PER_LONG)
1580 mask[nlongs - 1] &= (1UL << (maxnode % BITS_PER_LONG)) - 1;
1585 /* Copy a node mask from user space. */
1586 static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
1587 unsigned long maxnode)
1590 nodes_clear(*nodes);
1591 if (maxnode == 0 || !nmask)
1593 if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
1597 * When the user specified more nodes than supported just check
1598 * if the non supported part is all zero, one word at a time,
1599 * starting at the end.
1601 while (maxnode > MAX_NUMNODES) {
1602 unsigned long bits = min_t(unsigned long, maxnode, BITS_PER_LONG);
1605 if (get_bitmap(&t, &nmask[(maxnode - 1) / BITS_PER_LONG], bits))
1608 if (maxnode - bits >= MAX_NUMNODES) {
1611 maxnode = MAX_NUMNODES;
1612 t &= ~((1UL << (MAX_NUMNODES % BITS_PER_LONG)) - 1);
1618 return get_bitmap(nodes_addr(*nodes), nmask, maxnode);
1621 /* Copy a kernel node mask to user space */
1622 static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
1625 unsigned long copy = ALIGN(maxnode-1, 64) / 8;
1626 unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long);
1627 bool compat = in_compat_syscall();
1630 nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
1632 if (copy > nbytes) {
1633 if (copy > PAGE_SIZE)
1635 if (clear_user((char __user *)mask + nbytes, copy - nbytes))
1638 maxnode = nr_node_ids;
1642 return compat_put_bitmap((compat_ulong_t __user *)mask,
1643 nodes_addr(*nodes), maxnode);
1645 return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
1648 /* Basic parameter sanity check used by both mbind() and set_mempolicy() */
1649 static inline int sanitize_mpol_flags(int *mode, unsigned short *flags)
1651 *flags = *mode & MPOL_MODE_FLAGS;
1652 *mode &= ~MPOL_MODE_FLAGS;
1654 if ((unsigned int)(*mode) >= MPOL_MAX)
1656 if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
1658 if (*flags & MPOL_F_NUMA_BALANCING) {
1659 if (*mode == MPOL_BIND || *mode == MPOL_PREFERRED_MANY)
1660 *flags |= (MPOL_F_MOF | MPOL_F_MORON);
1667 static long kernel_mbind(unsigned long start, unsigned long len,
1668 unsigned long mode, const unsigned long __user *nmask,
1669 unsigned long maxnode, unsigned int flags)
1671 unsigned short mode_flags;
1676 start = untagged_addr(start);
1677 err = sanitize_mpol_flags(&lmode, &mode_flags);
1681 err = get_nodes(&nodes, nmask, maxnode);
1685 return do_mbind(start, len, lmode, mode_flags, &nodes, flags);
1688 SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, len,
1689 unsigned long, home_node, unsigned long, flags)
1691 struct mm_struct *mm = current->mm;
1692 struct vm_area_struct *vma, *prev;
1693 struct mempolicy *new, *old;
1696 VMA_ITERATOR(vmi, mm, start);
1698 start = untagged_addr(start);
1699 if (start & ~PAGE_MASK)
1702 * flags is used for future extension if any.
1708 * Check home_node is online to avoid accessing uninitialized
1711 if (home_node >= MAX_NUMNODES || !node_online(home_node))
1714 len = PAGE_ALIGN(len);
1721 mmap_write_lock(mm);
1722 prev = vma_prev(&vmi);
1723 for_each_vma_range(vmi, vma, end) {
1725 * If any vma in the range got policy other than MPOL_BIND
1726 * or MPOL_PREFERRED_MANY we return error. We don't reset
1727 * the home node for vmas we already updated before.
1729 old = vma_policy(vma);
1734 if (old->mode != MPOL_BIND && old->mode != MPOL_PREFERRED_MANY) {
1738 new = mpol_dup(old);
1744 vma_start_write(vma);
1745 new->home_node = home_node;
1746 err = mbind_range(&vmi, vma, &prev, start, end, new);
1751 mmap_write_unlock(mm);
1755 SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
1756 unsigned long, mode, const unsigned long __user *, nmask,
1757 unsigned long, maxnode, unsigned int, flags)
1759 return kernel_mbind(start, len, mode, nmask, maxnode, flags);
1762 /* Set the process memory policy */
1763 static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask,
1764 unsigned long maxnode)
1766 unsigned short mode_flags;
1771 err = sanitize_mpol_flags(&lmode, &mode_flags);
1775 err = get_nodes(&nodes, nmask, maxnode);
1779 return do_set_mempolicy(lmode, mode_flags, &nodes);
1782 SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask,
1783 unsigned long, maxnode)
1785 return kernel_set_mempolicy(mode, nmask, maxnode);
1788 static int kernel_migrate_pages(pid_t pid, unsigned long maxnode,
1789 const unsigned long __user *old_nodes,
1790 const unsigned long __user *new_nodes)
1792 struct mm_struct *mm = NULL;
1793 struct task_struct *task;
1794 nodemask_t task_nodes;
1798 NODEMASK_SCRATCH(scratch);
1803 old = &scratch->mask1;
1804 new = &scratch->mask2;
1806 err = get_nodes(old, old_nodes, maxnode);
1810 err = get_nodes(new, new_nodes, maxnode);
1814 /* Find the mm_struct */
1816 task = pid ? find_task_by_vpid(pid) : current;
1822 get_task_struct(task);
1827 * Check if this process has the right to modify the specified process.
1828 * Use the regular "ptrace_may_access()" checks.
1830 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
1837 task_nodes = cpuset_mems_allowed(task);
1838 /* Is the user allowed to access the target nodes? */
1839 if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
1844 task_nodes = cpuset_mems_allowed(current);
1845 nodes_and(*new, *new, task_nodes);
1846 if (nodes_empty(*new))
1849 err = security_task_movememory(task);
1853 mm = get_task_mm(task);
1854 put_task_struct(task);
1861 err = do_migrate_pages(mm, old, new,
1862 capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
1866 NODEMASK_SCRATCH_FREE(scratch);
1871 put_task_struct(task);
1875 SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
1876 const unsigned long __user *, old_nodes,
1877 const unsigned long __user *, new_nodes)
1879 return kernel_migrate_pages(pid, maxnode, old_nodes, new_nodes);
1882 /* Retrieve NUMA policy */
1883 static int kernel_get_mempolicy(int __user *policy,
1884 unsigned long __user *nmask,
1885 unsigned long maxnode,
1887 unsigned long flags)
1893 if (nmask != NULL && maxnode < nr_node_ids)
1896 addr = untagged_addr(addr);
1898 err = do_get_mempolicy(&pval, &nodes, addr, flags);
1903 if (policy && put_user(pval, policy))
1907 err = copy_nodes_to_user(nmask, maxnode, &nodes);
1912 SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1913 unsigned long __user *, nmask, unsigned long, maxnode,
1914 unsigned long, addr, unsigned long, flags)
1916 return kernel_get_mempolicy(policy, nmask, maxnode, addr, flags);
1919 bool vma_migratable(struct vm_area_struct *vma)
1921 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
1925 * DAX device mappings require predictable access latency, so avoid
1926 * incurring periodic faults.
1928 if (vma_is_dax(vma))
1931 if (is_vm_hugetlb_page(vma) &&
1932 !hugepage_migration_supported(hstate_vma(vma)))
1936 * Migration allocates pages in the highest zone. If we cannot
1937 * do so then migration (at least from node to node) is not
1941 gfp_zone(mapping_gfp_mask(vma->vm_file->f_mapping))
1947 struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
1948 unsigned long addr, pgoff_t *ilx)
1951 return (vma->vm_ops && vma->vm_ops->get_policy) ?
1952 vma->vm_ops->get_policy(vma, addr, ilx) : vma->vm_policy;
1956 * get_vma_policy(@vma, @addr, @order, @ilx)
1957 * @vma: virtual memory area whose policy is sought
1958 * @addr: address in @vma for shared policy lookup
1959 * @order: 0, or appropriate huge_page_order for interleaving
1960 * @ilx: interleave index (output), for use only when MPOL_INTERLEAVE or
1961 * MPOL_WEIGHTED_INTERLEAVE
1963 * Returns effective policy for a VMA at specified address.
1964 * Falls back to current->mempolicy or system default policy, as necessary.
1965 * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
1966 * count--added by the get_policy() vm_op, as appropriate--to protect against
1967 * freeing by another task. It is the caller's responsibility to free the
1968 * extra reference for shared policies.
1970 struct mempolicy *get_vma_policy(struct vm_area_struct *vma,
1971 unsigned long addr, int order, pgoff_t *ilx)
1973 struct mempolicy *pol;
1975 pol = __get_vma_policy(vma, addr, ilx);
1977 pol = get_task_policy(current);
1978 if (pol->mode == MPOL_INTERLEAVE ||
1979 pol->mode == MPOL_WEIGHTED_INTERLEAVE) {
1980 *ilx += vma->vm_pgoff >> order;
1981 *ilx += (addr - vma->vm_start) >> (PAGE_SHIFT + order);
1986 bool vma_policy_mof(struct vm_area_struct *vma)
1988 struct mempolicy *pol;
1990 if (vma->vm_ops && vma->vm_ops->get_policy) {
1992 pgoff_t ilx; /* ignored here */
1994 pol = vma->vm_ops->get_policy(vma, vma->vm_start, &ilx);
1995 if (pol && (pol->flags & MPOL_F_MOF))
2002 pol = vma->vm_policy;
2004 pol = get_task_policy(current);
2006 return pol->flags & MPOL_F_MOF;
2009 bool apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
2011 enum zone_type dynamic_policy_zone = policy_zone;
2013 BUG_ON(dynamic_policy_zone == ZONE_MOVABLE);
2016 * if policy->nodes has movable memory only,
2017 * we apply policy when gfp_zone(gfp) = ZONE_MOVABLE only.
2019 * policy->nodes is intersect with node_states[N_MEMORY].
2020 * so if the following test fails, it implies
2021 * policy->nodes has movable memory only.
2023 if (!nodes_intersects(policy->nodes, node_states[N_HIGH_MEMORY]))
2024 dynamic_policy_zone = ZONE_MOVABLE;
2026 return zone >= dynamic_policy_zone;
2029 static unsigned int weighted_interleave_nodes(struct mempolicy *policy)
2032 unsigned int cpuset_mems_cookie;
2035 /* to prevent miscount use tsk->mems_allowed_seq to detect rebind */
2036 cpuset_mems_cookie = read_mems_allowed_begin();
2037 node = current->il_prev;
2038 if (!current->il_weight || !node_isset(node, policy->nodes)) {
2039 node = next_node_in(node, policy->nodes);
2040 if (read_mems_allowed_retry(cpuset_mems_cookie))
2042 if (node == MAX_NUMNODES)
2044 current->il_prev = node;
2045 current->il_weight = get_il_weight(node);
2047 current->il_weight--;
2051 /* Do dynamic interleaving for a process */
2052 static unsigned int interleave_nodes(struct mempolicy *policy)
2055 unsigned int cpuset_mems_cookie;
2057 /* to prevent miscount, use tsk->mems_allowed_seq to detect rebind */
2059 cpuset_mems_cookie = read_mems_allowed_begin();
2060 nid = next_node_in(current->il_prev, policy->nodes);
2061 } while (read_mems_allowed_retry(cpuset_mems_cookie));
2063 if (nid < MAX_NUMNODES)
2064 current->il_prev = nid;
2069 * Depending on the memory policy provide a node from which to allocate the
2072 unsigned int mempolicy_slab_node(void)
2074 struct mempolicy *policy;
2075 int node = numa_mem_id();
2080 policy = current->mempolicy;
2084 switch (policy->mode) {
2085 case MPOL_PREFERRED:
2086 return first_node(policy->nodes);
2088 case MPOL_INTERLEAVE:
2089 return interleave_nodes(policy);
2091 case MPOL_WEIGHTED_INTERLEAVE:
2092 return weighted_interleave_nodes(policy);
2095 case MPOL_PREFERRED_MANY:
2100 * Follow bind policy behavior and start allocation at the
2103 struct zonelist *zonelist;
2104 enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
2105 zonelist = &NODE_DATA(node)->node_zonelists[ZONELIST_FALLBACK];
2106 z = first_zones_zonelist(zonelist, highest_zoneidx,
2108 return zonelist_zone(z) ? zonelist_node_idx(z) : node;
2118 static unsigned int read_once_policy_nodemask(struct mempolicy *pol,
2122 * barrier stabilizes the nodemask locally so that it can be iterated
2123 * over safely without concern for changes. Allocators validate node
2124 * selection does not violate mems_allowed, so this is safe.
2127 memcpy(mask, &pol->nodes, sizeof(nodemask_t));
2129 return nodes_weight(*mask);
2132 static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx)
2134 struct weighted_interleave_state *state;
2135 nodemask_t nodemask;
2136 unsigned int target, nr_nodes;
2138 unsigned int weight_total = 0;
2142 nr_nodes = read_once_policy_nodemask(pol, &nodemask);
2144 return numa_node_id();
2148 state = rcu_dereference(wi_state);
2149 /* Uninitialized wi_state means we should assume all weights are 1 */
2151 table = state->iw_table;
2153 /* calculate the total weight */
2154 for_each_node_mask(nid, nodemask)
2155 weight_total += table ? table[nid] : 1;
2157 /* Calculate the node offset based on totals */
2158 target = ilx % weight_total;
2159 nid = first_node(nodemask);
2161 /* detect system default usage */
2162 weight = table ? table[nid] : 1;
2163 if (target < weight)
2166 nid = next_node_in(nid, nodemask);
2173 * Do static interleaving for interleave index @ilx. Returns the ilx'th
2174 * node in pol->nodes (starting from ilx=0), wrapping around if ilx
2175 * exceeds the number of present nodes.
2177 static unsigned int interleave_nid(struct mempolicy *pol, pgoff_t ilx)
2179 nodemask_t nodemask;
2180 unsigned int target, nnodes;
2184 nnodes = read_once_policy_nodemask(pol, &nodemask);
2186 return numa_node_id();
2187 target = ilx % nnodes;
2188 nid = first_node(nodemask);
2189 for (i = 0; i < target; i++)
2190 nid = next_node(nid, nodemask);
2195 * Return a nodemask representing a mempolicy for filtering nodes for
2196 * page allocation, together with preferred node id (or the input node id).
2198 static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *pol,
2199 pgoff_t ilx, int *nid)
2201 nodemask_t *nodemask = NULL;
2203 switch (pol->mode) {
2204 case MPOL_PREFERRED:
2205 /* Override input node id */
2206 *nid = first_node(pol->nodes);
2208 case MPOL_PREFERRED_MANY:
2209 nodemask = &pol->nodes;
2210 if (pol->home_node != NUMA_NO_NODE)
2211 *nid = pol->home_node;
2214 /* Restrict to nodemask (but not on lower zones) */
2215 if (apply_policy_zone(pol, gfp_zone(gfp)) &&
2216 cpuset_nodemask_valid_mems_allowed(&pol->nodes))
2217 nodemask = &pol->nodes;
2218 if (pol->home_node != NUMA_NO_NODE)
2219 *nid = pol->home_node;
2221 * __GFP_THISNODE shouldn't even be used with the bind policy
2222 * because we might easily break the expectation to stay on the
2223 * requested node and not break the policy.
2225 WARN_ON_ONCE(gfp & __GFP_THISNODE);
2227 case MPOL_INTERLEAVE:
2228 /* Override input node id */
2229 *nid = (ilx == NO_INTERLEAVE_INDEX) ?
2230 interleave_nodes(pol) : interleave_nid(pol, ilx);
2232 case MPOL_WEIGHTED_INTERLEAVE:
2233 *nid = (ilx == NO_INTERLEAVE_INDEX) ?
2234 weighted_interleave_nodes(pol) :
2235 weighted_interleave_nid(pol, ilx);
2242 #ifdef CONFIG_HUGETLBFS
2244 * huge_node(@vma, @addr, @gfp_flags, @mpol)
2245 * @vma: virtual memory area whose policy is sought
2246 * @addr: address in @vma for shared policy lookup and interleave policy
2247 * @gfp_flags: for requested zone
2248 * @mpol: pointer to mempolicy pointer for reference counted mempolicy
2249 * @nodemask: pointer to nodemask pointer for 'bind' and 'prefer-many' policy
2251 * Returns a nid suitable for a huge page allocation and a pointer
2252 * to the struct mempolicy for conditional unref after allocation.
2253 * If the effective policy is 'bind' or 'prefer-many', returns a pointer
2254 * to the mempolicy's @nodemask for filtering the zonelist.
2256 int huge_node(struct vm_area_struct *vma, unsigned long addr, gfp_t gfp_flags,
2257 struct mempolicy **mpol, nodemask_t **nodemask)
2262 nid = numa_node_id();
2263 *mpol = get_vma_policy(vma, addr, hstate_vma(vma)->order, &ilx);
2264 *nodemask = policy_nodemask(gfp_flags, *mpol, ilx, &nid);
2269 * init_nodemask_of_mempolicy
2271 * If the current task's mempolicy is "default" [NULL], return 'false'
2272 * to indicate default policy. Otherwise, extract the policy nodemask
2273 * for 'bind' or 'interleave' policy into the argument nodemask, or
2274 * initialize the argument nodemask to contain the single node for
2275 * 'preferred' or 'local' policy and return 'true' to indicate presence
2276 * of non-default mempolicy.
2278 * We don't bother with reference counting the mempolicy [mpol_get/put]
2279 * because the current task is examining it's own mempolicy and a task's
2280 * mempolicy is only ever changed by the task itself.
2282 * N.B., it is the caller's responsibility to free a returned nodemask.
2284 bool init_nodemask_of_mempolicy(nodemask_t *mask)
2286 struct mempolicy *mempolicy;
2288 if (!(mask && current->mempolicy))
2292 mempolicy = current->mempolicy;
2293 switch (mempolicy->mode) {
2294 case MPOL_PREFERRED:
2295 case MPOL_PREFERRED_MANY:
2297 case MPOL_INTERLEAVE:
2298 case MPOL_WEIGHTED_INTERLEAVE:
2299 *mask = mempolicy->nodes;
2303 init_nodemask_of_node(mask, numa_node_id());
2309 task_unlock(current);
2316 * mempolicy_in_oom_domain
2318 * If tsk's mempolicy is "bind", check for intersection between mask and
2319 * the policy nodemask. Otherwise, return true for all other policies
2320 * including "interleave", as a tsk with "interleave" policy may have
2321 * memory allocated from all nodes in system.
2323 * Takes task_lock(tsk) to prevent freeing of its mempolicy.
2325 bool mempolicy_in_oom_domain(struct task_struct *tsk,
2326 const nodemask_t *mask)
2328 struct mempolicy *mempolicy;
2335 mempolicy = tsk->mempolicy;
2336 if (mempolicy && mempolicy->mode == MPOL_BIND)
2337 ret = nodes_intersects(mempolicy->nodes, *mask);
2343 static struct page *alloc_pages_preferred_many(gfp_t gfp, unsigned int order,
2344 int nid, nodemask_t *nodemask)
2347 gfp_t preferred_gfp;
2350 * This is a two pass approach. The first pass will only try the
2351 * preferred nodes but skip the direct reclaim and allow the
2352 * allocation to fail, while the second pass will try all the
2355 preferred_gfp = gfp | __GFP_NOWARN;
2356 preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
2357 page = __alloc_frozen_pages_noprof(preferred_gfp, order, nid, nodemask);
2359 page = __alloc_frozen_pages_noprof(gfp, order, nid, NULL);
2365 * alloc_pages_mpol - Allocate pages according to NUMA mempolicy.
2367 * @order: Order of the page allocation.
2368 * @pol: Pointer to the NUMA mempolicy.
2369 * @ilx: Index for interleave mempolicy (also distinguishes alloc_pages()).
2370 * @nid: Preferred node (usually numa_node_id() but @mpol may override it).
2372 * Return: The page on success or NULL if allocation fails.
2374 static struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order,
2375 struct mempolicy *pol, pgoff_t ilx, int nid)
2377 nodemask_t *nodemask;
2380 nodemask = policy_nodemask(gfp, pol, ilx, &nid);
2382 if (pol->mode == MPOL_PREFERRED_MANY)
2383 return alloc_pages_preferred_many(gfp, order, nid, nodemask);
2385 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
2386 /* filter "hugepage" allocation, unless from alloc_pages() */
2387 order == HPAGE_PMD_ORDER && ilx != NO_INTERLEAVE_INDEX) {
2389 * For hugepage allocation and non-interleave policy which
2390 * allows the current node (or other explicitly preferred
2391 * node) we only try to allocate from the current/preferred
2392 * node and don't fall back to other nodes, as the cost of
2393 * remote accesses would likely offset THP benefits.
2395 * If the policy is interleave or does not allow the current
2396 * node in its nodemask, we allocate the standard way.
2398 if (pol->mode != MPOL_INTERLEAVE &&
2399 pol->mode != MPOL_WEIGHTED_INTERLEAVE &&
2400 (!nodemask || node_isset(nid, *nodemask))) {
2402 * First, try to allocate THP only on local node, but
2403 * don't reclaim unnecessarily, just compact.
2405 page = __alloc_frozen_pages_noprof(
2406 gfp | __GFP_THISNODE | __GFP_NORETRY, order,
2408 if (page || !(gfp & __GFP_DIRECT_RECLAIM))
2411 * If hugepage allocations are configured to always
2412 * synchronous compact or the vma has been madvised
2413 * to prefer hugepage backing, retry allowing remote
2414 * memory with both reclaim and compact as well.
2419 page = __alloc_frozen_pages_noprof(gfp, order, nid, nodemask);
2421 if (unlikely(pol->mode == MPOL_INTERLEAVE ||
2422 pol->mode == MPOL_WEIGHTED_INTERLEAVE) && page) {
2423 /* skip NUMA_INTERLEAVE_HIT update if numa stats is disabled */
2424 if (static_branch_likely(&vm_numa_stat_key) &&
2425 page_to_nid(page) == nid) {
2427 __count_numa_event(page_zone(page), NUMA_INTERLEAVE_HIT);
2435 struct folio *folio_alloc_mpol_noprof(gfp_t gfp, unsigned int order,
2436 struct mempolicy *pol, pgoff_t ilx, int nid)
2438 struct page *page = alloc_pages_mpol(gfp | __GFP_COMP, order, pol,
2443 set_page_refcounted(page);
2444 return page_rmappable_folio(page);
2448 * vma_alloc_folio - Allocate a folio for a VMA.
2450 * @order: Order of the folio.
2451 * @vma: Pointer to VMA.
2452 * @addr: Virtual address of the allocation. Must be inside @vma.
2454 * Allocate a folio for a specific address in @vma, using the appropriate
2455 * NUMA policy. The caller must hold the mmap_lock of the mm_struct of the
2456 * VMA to prevent it from going away. Should be used for all allocations
2457 * for folios that will be mapped into user space, excepting hugetlbfs, and
2458 * excepting where direct use of folio_alloc_mpol() is more appropriate.
2460 * Return: The folio on success or NULL if allocation fails.
2462 struct folio *vma_alloc_folio_noprof(gfp_t gfp, int order, struct vm_area_struct *vma,
2465 struct mempolicy *pol;
2467 struct folio *folio;
2469 if (vma->vm_flags & VM_DROPPABLE)
2470 gfp |= __GFP_NOWARN;
2472 pol = get_vma_policy(vma, addr, order, &ilx);
2473 folio = folio_alloc_mpol_noprof(gfp, order, pol, ilx, numa_node_id());
2477 EXPORT_SYMBOL(vma_alloc_folio_noprof);
2479 struct page *alloc_frozen_pages_noprof(gfp_t gfp, unsigned order)
2481 struct mempolicy *pol = &default_policy;
2484 * No reference counting needed for current->mempolicy
2485 * nor system default_policy
2487 if (!in_interrupt() && !(gfp & __GFP_THISNODE))
2488 pol = get_task_policy(current);
2490 return alloc_pages_mpol(gfp, order, pol, NO_INTERLEAVE_INDEX,
2495 * alloc_pages - Allocate pages.
2497 * @order: Power of two of number of pages to allocate.
2499 * Allocate 1 << @order contiguous pages. The physical address of the
2500 * first page is naturally aligned (eg an order-3 allocation will be aligned
2501 * to a multiple of 8 * PAGE_SIZE bytes). The NUMA policy of the current
2502 * process is honoured when in process context.
2504 * Context: Can be called from any context, providing the appropriate GFP
2506 * Return: The page on success or NULL if allocation fails.
2508 struct page *alloc_pages_noprof(gfp_t gfp, unsigned int order)
2510 struct page *page = alloc_frozen_pages_noprof(gfp, order);
2513 set_page_refcounted(page);
2516 EXPORT_SYMBOL(alloc_pages_noprof);
2518 struct folio *folio_alloc_noprof(gfp_t gfp, unsigned int order)
2520 return page_rmappable_folio(alloc_pages_noprof(gfp | __GFP_COMP, order));
2522 EXPORT_SYMBOL(folio_alloc_noprof);
2524 static unsigned long alloc_pages_bulk_interleave(gfp_t gfp,
2525 struct mempolicy *pol, unsigned long nr_pages,
2526 struct page **page_array)
2529 unsigned long nr_pages_per_node;
2532 unsigned long nr_allocated;
2533 unsigned long total_allocated = 0;
2535 nodes = nodes_weight(pol->nodes);
2536 nr_pages_per_node = nr_pages / nodes;
2537 delta = nr_pages - nodes * nr_pages_per_node;
2539 for (i = 0; i < nodes; i++) {
2541 nr_allocated = alloc_pages_bulk_noprof(gfp,
2542 interleave_nodes(pol), NULL,
2543 nr_pages_per_node + 1,
2547 nr_allocated = alloc_pages_bulk_noprof(gfp,
2548 interleave_nodes(pol), NULL,
2549 nr_pages_per_node, page_array);
2552 page_array += nr_allocated;
2553 total_allocated += nr_allocated;
2556 return total_allocated;
2559 static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp,
2560 struct mempolicy *pol, unsigned long nr_pages,
2561 struct page **page_array)
2563 struct weighted_interleave_state *state;
2564 struct task_struct *me = current;
2565 unsigned int cpuset_mems_cookie;
2566 unsigned long total_allocated = 0;
2567 unsigned long nr_allocated = 0;
2568 unsigned long rounds;
2569 unsigned long node_pages, delta;
2570 u8 *weights, weight;
2571 unsigned int weight_total = 0;
2572 unsigned long rem_pages = nr_pages;
2575 int resume_node = MAX_NUMNODES - 1;
2576 u8 resume_weight = 0;
2583 /* read the nodes onto the stack, retry if done during rebind */
2585 cpuset_mems_cookie = read_mems_allowed_begin();
2586 nnodes = read_once_policy_nodemask(pol, &nodes);
2587 } while (read_mems_allowed_retry(cpuset_mems_cookie));
2589 /* if the nodemask has become invalid, we cannot do anything */
2593 /* Continue allocating from most recent node and adjust the nr_pages */
2595 weight = me->il_weight;
2596 if (weight && node_isset(node, nodes)) {
2597 node_pages = min(rem_pages, weight);
2598 nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages,
2600 page_array += nr_allocated;
2601 total_allocated += nr_allocated;
2602 /* if that's all the pages, no need to interleave */
2603 if (rem_pages <= weight) {
2604 me->il_weight -= rem_pages;
2605 return total_allocated;
2607 /* Otherwise we adjust remaining pages, continue from there */
2608 rem_pages -= weight;
2610 /* clear active weight in case of an allocation failure */
2614 /* create a local copy of node weights to operate on outside rcu */
2615 weights = kzalloc(nr_node_ids, GFP_KERNEL);
2617 return total_allocated;
2620 state = rcu_dereference(wi_state);
2622 memcpy(weights, state->iw_table, nr_node_ids * sizeof(u8));
2626 for (i = 0; i < nr_node_ids; i++)
2630 /* calculate total, detect system default usage */
2631 for_each_node_mask(node, nodes)
2632 weight_total += weights[node];
2635 * Calculate rounds/partial rounds to minimize __alloc_pages_bulk calls.
2636 * Track which node weighted interleave should resume from.
2638 * if (rounds > 0) and (delta == 0), resume_node will always be
2639 * the node following prev_node and its weight.
2641 rounds = rem_pages / weight_total;
2642 delta = rem_pages % weight_total;
2643 resume_node = next_node_in(prev_node, nodes);
2644 resume_weight = weights[resume_node];
2645 for (i = 0; i < nnodes; i++) {
2646 node = next_node_in(prev_node, nodes);
2647 weight = weights[node];
2648 node_pages = weight * rounds;
2649 /* If a delta exists, add this node's portion of the delta */
2650 if (delta > weight) {
2651 node_pages += weight;
2654 /* when delta is depleted, resume from that node */
2655 node_pages += delta;
2657 resume_weight = weight - delta;
2660 /* node_pages can be 0 if an allocation fails and rounds == 0 */
2663 nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages,
2665 page_array += nr_allocated;
2666 total_allocated += nr_allocated;
2667 if (total_allocated == nr_pages)
2671 me->il_prev = resume_node;
2672 me->il_weight = resume_weight;
2674 return total_allocated;
2677 static unsigned long alloc_pages_bulk_preferred_many(gfp_t gfp, int nid,
2678 struct mempolicy *pol, unsigned long nr_pages,
2679 struct page **page_array)
2681 gfp_t preferred_gfp;
2682 unsigned long nr_allocated = 0;
2684 preferred_gfp = gfp | __GFP_NOWARN;
2685 preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
2687 nr_allocated = alloc_pages_bulk_noprof(preferred_gfp, nid, &pol->nodes,
2688 nr_pages, page_array);
2690 if (nr_allocated < nr_pages)
2691 nr_allocated += alloc_pages_bulk_noprof(gfp, numa_node_id(), NULL,
2692 nr_pages - nr_allocated,
2693 page_array + nr_allocated);
2694 return nr_allocated;
2697 /* alloc pages bulk and mempolicy should be considered at the
2698 * same time in some situation such as vmalloc.
2700 * It can accelerate memory allocation especially interleaving
2703 unsigned long alloc_pages_bulk_mempolicy_noprof(gfp_t gfp,
2704 unsigned long nr_pages, struct page **page_array)
2706 struct mempolicy *pol = &default_policy;
2707 nodemask_t *nodemask;
2710 if (!in_interrupt() && !(gfp & __GFP_THISNODE))
2711 pol = get_task_policy(current);
2713 if (pol->mode == MPOL_INTERLEAVE)
2714 return alloc_pages_bulk_interleave(gfp, pol,
2715 nr_pages, page_array);
2717 if (pol->mode == MPOL_WEIGHTED_INTERLEAVE)
2718 return alloc_pages_bulk_weighted_interleave(
2719 gfp, pol, nr_pages, page_array);
2721 if (pol->mode == MPOL_PREFERRED_MANY)
2722 return alloc_pages_bulk_preferred_many(gfp,
2723 numa_node_id(), pol, nr_pages, page_array);
2725 nid = numa_node_id();
2726 nodemask = policy_nodemask(gfp, pol, NO_INTERLEAVE_INDEX, &nid);
2727 return alloc_pages_bulk_noprof(gfp, nid, nodemask,
2728 nr_pages, page_array);
2731 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
2733 struct mempolicy *pol = mpol_dup(src->vm_policy);
2736 return PTR_ERR(pol);
2737 dst->vm_policy = pol;
2742 * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
2743 * rebinds the mempolicy its copying by calling mpol_rebind_policy()
2744 * with the mems_allowed returned by cpuset_mems_allowed(). This
2745 * keeps mempolicies cpuset relative after its cpuset moves. See
2746 * further kernel/cpuset.c update_nodemask().
2748 * current's mempolicy may be rebinded by the other task(the task that changes
2749 * cpuset's mems), so we needn't do rebind work for current task.
2752 /* Slow path of a mempolicy duplicate */
2753 struct mempolicy *__mpol_dup(struct mempolicy *old)
2755 struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2758 return ERR_PTR(-ENOMEM);
2760 /* task's mempolicy is protected by alloc_lock */
2761 if (old == current->mempolicy) {
2764 task_unlock(current);
2768 if (current_cpuset_is_being_rebound()) {
2769 nodemask_t mems = cpuset_mems_allowed(current);
2770 mpol_rebind_policy(new, &mems);
2772 atomic_set(&new->refcnt, 1);
2776 /* Slow path of a mempolicy comparison */
2777 bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
2781 if (a->mode != b->mode)
2783 if (a->flags != b->flags)
2785 if (a->home_node != b->home_node)
2787 if (mpol_store_user_nodemask(a))
2788 if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
2793 case MPOL_INTERLEAVE:
2794 case MPOL_PREFERRED:
2795 case MPOL_PREFERRED_MANY:
2796 case MPOL_WEIGHTED_INTERLEAVE:
2797 return !!nodes_equal(a->nodes, b->nodes);
2807 * Shared memory backing store policy support.
2809 * Remember policies even when nobody has shared memory mapped.
2810 * The policies are kept in Red-Black tree linked from the inode.
2811 * They are protected by the sp->lock rwlock, which should be held
2812 * for any accesses to the tree.
2816 * lookup first element intersecting start-end. Caller holds sp->lock for
2817 * reading or for writing
2819 static struct sp_node *sp_lookup(struct shared_policy *sp,
2820 pgoff_t start, pgoff_t end)
2822 struct rb_node *n = sp->root.rb_node;
2825 struct sp_node *p = rb_entry(n, struct sp_node, nd);
2827 if (start >= p->end)
2829 else if (end <= p->start)
2837 struct sp_node *w = NULL;
2838 struct rb_node *prev = rb_prev(n);
2841 w = rb_entry(prev, struct sp_node, nd);
2842 if (w->end <= start)
2846 return rb_entry(n, struct sp_node, nd);
2850 * Insert a new shared policy into the list. Caller holds sp->lock for
2853 static void sp_insert(struct shared_policy *sp, struct sp_node *new)
2855 struct rb_node **p = &sp->root.rb_node;
2856 struct rb_node *parent = NULL;
2861 nd = rb_entry(parent, struct sp_node, nd);
2862 if (new->start < nd->start)
2864 else if (new->end > nd->end)
2865 p = &(*p)->rb_right;
2869 rb_link_node(&new->nd, parent, p);
2870 rb_insert_color(&new->nd, &sp->root);
2873 /* Find shared policy intersecting idx */
2874 struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
2877 struct mempolicy *pol = NULL;
2880 if (!sp->root.rb_node)
2882 read_lock(&sp->lock);
2883 sn = sp_lookup(sp, idx, idx+1);
2885 mpol_get(sn->policy);
2888 read_unlock(&sp->lock);
2892 static void sp_free(struct sp_node *n)
2894 mpol_put(n->policy);
2895 kmem_cache_free(sn_cache, n);
2899 * mpol_misplaced - check whether current folio node is valid in policy
2901 * @folio: folio to be checked
2902 * @vmf: structure describing the fault
2903 * @addr: virtual address in @vma for shared policy lookup and interleave policy
2905 * Lookup current policy node id for vma,addr and "compare to" folio's
2906 * node id. Policy determination "mimics" alloc_page_vma().
2907 * Called from fault path where we know the vma and faulting address.
2909 * Return: NUMA_NO_NODE if the page is in a node that is valid for this
2910 * policy, or a suitable node ID to allocate a replacement folio from.
2912 int mpol_misplaced(struct folio *folio, struct vm_fault *vmf,
2915 struct mempolicy *pol;
2918 int curnid = folio_nid(folio);
2919 struct vm_area_struct *vma = vmf->vma;
2920 int thiscpu = raw_smp_processor_id();
2921 int thisnid = numa_node_id();
2922 int polnid = NUMA_NO_NODE;
2923 int ret = NUMA_NO_NODE;
2926 * Make sure ptl is held so that we don't preempt and we
2927 * have a stable smp processor id
2929 lockdep_assert_held(vmf->ptl);
2930 pol = get_vma_policy(vma, addr, folio_order(folio), &ilx);
2931 if (!(pol->flags & MPOL_F_MOF))
2934 switch (pol->mode) {
2935 case MPOL_INTERLEAVE:
2936 polnid = interleave_nid(pol, ilx);
2939 case MPOL_WEIGHTED_INTERLEAVE:
2940 polnid = weighted_interleave_nid(pol, ilx);
2943 case MPOL_PREFERRED:
2944 if (node_isset(curnid, pol->nodes))
2946 polnid = first_node(pol->nodes);
2950 polnid = numa_node_id();
2954 case MPOL_PREFERRED_MANY:
2956 * Even though MPOL_PREFERRED_MANY can allocate pages outside
2957 * policy nodemask we don't allow numa migration to nodes
2958 * outside policy nodemask for now. This is done so that if we
2959 * want demotion to slow memory to happen, before allocating
2960 * from some DRAM node say 'x', we will end up using a
2961 * MPOL_PREFERRED_MANY mask excluding node 'x'. In such scenario
2962 * we should not promote to node 'x' from slow memory node.
2964 if (pol->flags & MPOL_F_MORON) {
2966 * Optimize placement among multiple nodes
2967 * via NUMA balancing
2969 if (node_isset(thisnid, pol->nodes))
2975 * use current page if in policy nodemask,
2976 * else select nearest allowed node, if any.
2977 * If no allowed nodes, use current [!misplaced].
2979 if (node_isset(curnid, pol->nodes))
2981 z = first_zones_zonelist(
2982 node_zonelist(thisnid, GFP_HIGHUSER),
2983 gfp_zone(GFP_HIGHUSER),
2985 polnid = zonelist_node_idx(z);
2992 /* Migrate the folio towards the node whose CPU is referencing it */
2993 if (pol->flags & MPOL_F_MORON) {
2996 if (!should_numa_migrate_memory(current, folio, curnid,
3001 if (curnid != polnid)
3010 * Drop the (possibly final) reference to task->mempolicy. It needs to be
3011 * dropped after task->mempolicy is set to NULL so that any allocation done as
3012 * part of its kmem_cache_free(), such as by KASAN, doesn't reference a freed
3015 void mpol_put_task_policy(struct task_struct *task)
3017 struct mempolicy *pol;
3020 pol = task->mempolicy;
3021 task->mempolicy = NULL;
3026 static void sp_delete(struct shared_policy *sp, struct sp_node *n)
3028 rb_erase(&n->nd, &sp->root);
3032 static void sp_node_init(struct sp_node *node, unsigned long start,
3033 unsigned long end, struct mempolicy *pol)
3035 node->start = start;
3040 static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
3041 struct mempolicy *pol)
3044 struct mempolicy *newpol;
3046 n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
3050 newpol = mpol_dup(pol);
3051 if (IS_ERR(newpol)) {
3052 kmem_cache_free(sn_cache, n);
3055 newpol->flags |= MPOL_F_SHARED;
3056 sp_node_init(n, start, end, newpol);
3061 /* Replace a policy range. */
3062 static int shared_policy_replace(struct shared_policy *sp, pgoff_t start,
3063 pgoff_t end, struct sp_node *new)
3066 struct sp_node *n_new = NULL;
3067 struct mempolicy *mpol_new = NULL;
3071 write_lock(&sp->lock);
3072 n = sp_lookup(sp, start, end);
3073 /* Take care of old policies in the same range. */
3074 while (n && n->start < end) {
3075 struct rb_node *next = rb_next(&n->nd);
3076 if (n->start >= start) {
3082 /* Old policy spanning whole new range. */
3087 *mpol_new = *n->policy;
3088 atomic_set(&mpol_new->refcnt, 1);
3089 sp_node_init(n_new, end, n->end, mpol_new);
3091 sp_insert(sp, n_new);
3100 n = rb_entry(next, struct sp_node, nd);
3104 write_unlock(&sp->lock);
3111 kmem_cache_free(sn_cache, n_new);
3116 write_unlock(&sp->lock);
3118 n_new = kmem_cache_alloc(sn_cache, GFP_KERNEL);
3121 mpol_new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
3124 atomic_set(&mpol_new->refcnt, 1);
3129 * mpol_shared_policy_init - initialize shared policy for inode
3130 * @sp: pointer to inode shared policy
3131 * @mpol: struct mempolicy to install
3133 * Install non-NULL @mpol in inode's shared policy rb-tree.
3134 * On entry, the current task has a reference on a non-NULL @mpol.
3135 * This must be released on exit.
3136 * This is called at get_inode() calls and we can use GFP_KERNEL.
3138 void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
3142 sp->root = RB_ROOT; /* empty tree == default mempolicy */
3143 rwlock_init(&sp->lock);
3147 struct mempolicy *npol;
3148 NODEMASK_SCRATCH(scratch);
3153 /* contextualize the tmpfs mount point mempolicy to this file */
3154 npol = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
3156 goto free_scratch; /* no valid nodemask intersection */
3159 ret = mpol_set_nodemask(npol, &mpol->w.user_nodemask, scratch);
3160 task_unlock(current);
3164 /* alloc node covering entire file; adds ref to file's npol */
3165 sn = sp_alloc(0, MAX_LFS_FILESIZE >> PAGE_SHIFT, npol);
3169 mpol_put(npol); /* drop initial ref on file's npol */
3171 NODEMASK_SCRATCH_FREE(scratch);
3173 mpol_put(mpol); /* drop our incoming ref on sb mpol */
3177 int mpol_set_shared_policy(struct shared_policy *sp,
3178 struct vm_area_struct *vma, struct mempolicy *pol)
3181 struct sp_node *new = NULL;
3182 unsigned long sz = vma_pages(vma);
3185 new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, pol);
3189 err = shared_policy_replace(sp, vma->vm_pgoff, vma->vm_pgoff + sz, new);
3195 /* Free a backing policy store on inode delete. */
3196 void mpol_free_shared_policy(struct shared_policy *sp)
3199 struct rb_node *next;
3201 if (!sp->root.rb_node)
3203 write_lock(&sp->lock);
3204 next = rb_first(&sp->root);
3206 n = rb_entry(next, struct sp_node, nd);
3207 next = rb_next(&n->nd);
3210 write_unlock(&sp->lock);
3213 #ifdef CONFIG_NUMA_BALANCING
3214 static int __initdata numabalancing_override;
3216 static void __init check_numabalancing_enable(void)
3218 bool numabalancing_default = false;
3220 if (IS_ENABLED(CONFIG_NUMA_BALANCING_DEFAULT_ENABLED))
3221 numabalancing_default = true;
3223 /* Parsed by setup_numabalancing. override == 1 enables, -1 disables */
3224 if (numabalancing_override)
3225 set_numabalancing_state(numabalancing_override == 1);
3227 if (num_online_nodes() > 1 && !numabalancing_override) {
3228 pr_info("%s automatic NUMA balancing. Configure with numa_balancing= or the kernel.numa_balancing sysctl\n",
3229 numabalancing_default ? "Enabling" : "Disabling");
3230 set_numabalancing_state(numabalancing_default);
3234 static int __init setup_numabalancing(char *str)
3240 if (!strcmp(str, "enable")) {
3241 numabalancing_override = 1;
3243 } else if (!strcmp(str, "disable")) {
3244 numabalancing_override = -1;
3249 pr_warn("Unable to parse numa_balancing=\n");
3253 __setup("numa_balancing=", setup_numabalancing);
3255 static inline void __init check_numabalancing_enable(void)
3258 #endif /* CONFIG_NUMA_BALANCING */
3260 void __init numa_policy_init(void)
3262 nodemask_t interleave_nodes;
3263 unsigned long largest = 0;
3264 int nid, prefer = 0;
3266 policy_cache = kmem_cache_create("numa_policy",
3267 sizeof(struct mempolicy),
3268 0, SLAB_PANIC, NULL);
3270 sn_cache = kmem_cache_create("shared_policy_node",
3271 sizeof(struct sp_node),
3272 0, SLAB_PANIC, NULL);
3274 for_each_node(nid) {
3275 preferred_node_policy[nid] = (struct mempolicy) {
3276 .refcnt = ATOMIC_INIT(1),
3277 .mode = MPOL_PREFERRED,
3278 .flags = MPOL_F_MOF | MPOL_F_MORON,
3279 .nodes = nodemask_of_node(nid),
3284 * Set interleaving policy for system init. Interleaving is only
3285 * enabled across suitably sized nodes (default is >= 16MB), or
3286 * fall back to the largest node if they're all smaller.
3288 nodes_clear(interleave_nodes);
3289 for_each_node_state(nid, N_MEMORY) {
3290 unsigned long total_pages = node_present_pages(nid);
3292 /* Preserve the largest node */
3293 if (largest < total_pages) {
3294 largest = total_pages;
3298 /* Interleave this node? */
3299 if ((total_pages << PAGE_SHIFT) >= (16 << 20))
3300 node_set(nid, interleave_nodes);
3303 /* All too small, use the largest */
3304 if (unlikely(nodes_empty(interleave_nodes)))
3305 node_set(prefer, interleave_nodes);
3307 if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
3308 pr_err("%s: interleaving failed\n", __func__);
3310 check_numabalancing_enable();
3313 /* Reset policy of current process to default */
3314 void numa_default_policy(void)
3316 do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
3320 * Parse and format mempolicy from/to strings
3322 static const char * const policy_modes[] =
3324 [MPOL_DEFAULT] = "default",
3325 [MPOL_PREFERRED] = "prefer",
3326 [MPOL_BIND] = "bind",
3327 [MPOL_INTERLEAVE] = "interleave",
3328 [MPOL_WEIGHTED_INTERLEAVE] = "weighted interleave",
3329 [MPOL_LOCAL] = "local",
3330 [MPOL_PREFERRED_MANY] = "prefer (many)",
3335 * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option.
3336 * @str: string containing mempolicy to parse
3337 * @mpol: pointer to struct mempolicy pointer, returned on success.
3340 * <mode>[=<flags>][:<nodelist>]
3342 * Return: %0 on success, else %1
3344 int mpol_parse_str(char *str, struct mempolicy **mpol)
3346 struct mempolicy *new = NULL;
3347 unsigned short mode_flags;
3349 char *nodelist = strchr(str, ':');
3350 char *flags = strchr(str, '=');
3354 *flags++ = '\0'; /* terminate mode string */
3357 /* NUL-terminate mode or flags string */
3359 if (nodelist_parse(nodelist, nodes))
3361 if (!nodes_subset(nodes, node_states[N_MEMORY]))
3366 mode = match_string(policy_modes, MPOL_MAX, str);
3371 case MPOL_PREFERRED:
3373 * Insist on a nodelist of one node only, although later
3374 * we use first_node(nodes) to grab a single node, so here
3375 * nodelist (or nodes) cannot be empty.
3378 char *rest = nodelist;
3379 while (isdigit(*rest))
3383 if (nodes_empty(nodes))
3387 case MPOL_INTERLEAVE:
3388 case MPOL_WEIGHTED_INTERLEAVE:
3390 * Default to online nodes with memory if no nodelist
3393 nodes = node_states[N_MEMORY];
3397 * Don't allow a nodelist; mpol_new() checks flags
3404 * Insist on a empty nodelist
3409 case MPOL_PREFERRED_MANY:
3412 * Insist on a nodelist
3421 * Currently, we only support two mutually exclusive
3424 if (!strcmp(flags, "static"))
3425 mode_flags |= MPOL_F_STATIC_NODES;
3426 else if (!strcmp(flags, "relative"))
3427 mode_flags |= MPOL_F_RELATIVE_NODES;
3432 new = mpol_new(mode, mode_flags, &nodes);
3437 * Save nodes for mpol_to_str() to show the tmpfs mount options
3438 * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo.
3440 if (mode != MPOL_PREFERRED) {
3442 } else if (nodelist) {
3443 nodes_clear(new->nodes);
3444 node_set(first_node(nodes), new->nodes);
3446 new->mode = MPOL_LOCAL;
3450 * Save nodes for contextualization: this will be used to "clone"
3451 * the mempolicy in a specific context [cpuset] at a later time.
3453 new->w.user_nodemask = nodes;
3458 /* Restore string for error message */
3467 #endif /* CONFIG_TMPFS */
3470 * mpol_to_str - format a mempolicy structure for printing
3471 * @buffer: to contain formatted mempolicy string
3472 * @maxlen: length of @buffer
3473 * @pol: pointer to mempolicy to be formatted
3475 * Convert @pol into a string. If @buffer is too short, truncate the string.
3476 * Recommend a @maxlen of at least 51 for the longest mode, "weighted
3477 * interleave", plus the longest flag flags, "relative|balancing", and to
3478 * display at least a few node ids.
3480 void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
3483 nodemask_t nodes = NODE_MASK_NONE;
3484 unsigned short mode = MPOL_DEFAULT;
3485 unsigned short flags = 0;
3488 pol != &default_policy &&
3489 !(pol >= &preferred_node_policy[0] &&
3490 pol <= &preferred_node_policy[ARRAY_SIZE(preferred_node_policy) - 1])) {
3499 case MPOL_PREFERRED:
3500 case MPOL_PREFERRED_MANY:
3502 case MPOL_INTERLEAVE:
3503 case MPOL_WEIGHTED_INTERLEAVE:
3508 snprintf(p, maxlen, "unknown");
3512 p += snprintf(p, maxlen, "%s", policy_modes[mode]);
3514 if (flags & MPOL_MODE_FLAGS) {
3515 p += snprintf(p, buffer + maxlen - p, "=");
3518 * Static and relative are mutually exclusive.
3520 if (flags & MPOL_F_STATIC_NODES)
3521 p += snprintf(p, buffer + maxlen - p, "static");
3522 else if (flags & MPOL_F_RELATIVE_NODES)
3523 p += snprintf(p, buffer + maxlen - p, "relative");
3525 if (flags & MPOL_F_NUMA_BALANCING) {
3526 if (!is_power_of_2(flags & MPOL_MODE_FLAGS))
3527 p += snprintf(p, buffer + maxlen - p, "|");
3528 p += snprintf(p, buffer + maxlen - p, "balancing");
3532 if (!nodes_empty(nodes))
3533 p += scnprintf(p, buffer + maxlen - p, ":%*pbl",
3534 nodemask_pr_args(&nodes));
3538 struct iw_node_attr {
3539 struct kobj_attribute kobj_attr;
3543 struct sysfs_wi_group {
3544 struct kobject wi_kobj;
3545 struct mutex kobj_lock;
3546 struct iw_node_attr *nattrs[];
3549 static struct sysfs_wi_group *wi_group;
3551 static ssize_t node_show(struct kobject *kobj, struct kobj_attribute *attr,
3554 struct iw_node_attr *node_attr;
3557 node_attr = container_of(attr, struct iw_node_attr, kobj_attr);
3558 weight = get_il_weight(node_attr->nid);
3559 return sysfs_emit(buf, "%d\n", weight);
3562 static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr,
3563 const char *buf, size_t count)
3565 struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL;
3566 struct iw_node_attr *node_attr;
3570 node_attr = container_of(attr, struct iw_node_attr, kobj_attr);
3571 if (count == 0 || sysfs_streq(buf, "") ||
3572 kstrtou8(buf, 0, &weight) || weight == 0)
3575 new_wi_state = kzalloc(struct_size(new_wi_state, iw_table, nr_node_ids),
3580 mutex_lock(&wi_state_lock);
3581 old_wi_state = rcu_dereference_protected(wi_state,
3582 lockdep_is_held(&wi_state_lock));
3584 memcpy(new_wi_state->iw_table, old_wi_state->iw_table,
3585 nr_node_ids * sizeof(u8));
3587 for (i = 0; i < nr_node_ids; i++)
3588 new_wi_state->iw_table[i] = 1;
3590 new_wi_state->iw_table[node_attr->nid] = weight;
3591 new_wi_state->mode_auto = false;
3593 rcu_assign_pointer(wi_state, new_wi_state);
3594 mutex_unlock(&wi_state_lock);
3597 kfree(old_wi_state);
3602 static ssize_t weighted_interleave_auto_show(struct kobject *kobj,
3603 struct kobj_attribute *attr, char *buf)
3605 struct weighted_interleave_state *state;
3606 bool wi_auto = true;
3609 state = rcu_dereference(wi_state);
3611 wi_auto = state->mode_auto;
3614 return sysfs_emit(buf, "%s\n", str_true_false(wi_auto));
3617 static ssize_t weighted_interleave_auto_store(struct kobject *kobj,
3618 struct kobj_attribute *attr, const char *buf, size_t count)
3620 struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL;
3625 if (kstrtobool(buf, &input))
3628 new_wi_state = kzalloc(struct_size(new_wi_state, iw_table, nr_node_ids),
3632 for (i = 0; i < nr_node_ids; i++)
3633 new_wi_state->iw_table[i] = 1;
3635 mutex_lock(&wi_state_lock);
3637 old_wi_state = rcu_dereference_protected(wi_state,
3638 lockdep_is_held(&wi_state_lock));
3640 goto update_wi_state;
3641 if (input == old_wi_state->mode_auto) {
3642 mutex_unlock(&wi_state_lock);
3646 memcpy(new_wi_state->iw_table, old_wi_state->iw_table,
3647 nr_node_ids * sizeof(u8));
3648 goto update_wi_state;
3653 mutex_unlock(&wi_state_lock);
3654 kfree(new_wi_state);
3658 new_wi_state->mode_auto = true;
3659 reduce_interleave_weights(bw, new_wi_state->iw_table);
3662 rcu_assign_pointer(wi_state, new_wi_state);
3663 mutex_unlock(&wi_state_lock);
3666 kfree(old_wi_state);
3671 static void sysfs_wi_node_delete(int nid)
3673 struct iw_node_attr *attr;
3675 if (nid < 0 || nid >= nr_node_ids)
3678 mutex_lock(&wi_group->kobj_lock);
3679 attr = wi_group->nattrs[nid];
3681 mutex_unlock(&wi_group->kobj_lock);
3685 wi_group->nattrs[nid] = NULL;
3686 mutex_unlock(&wi_group->kobj_lock);
3688 sysfs_remove_file(&wi_group->wi_kobj, &attr->kobj_attr.attr);
3689 kfree(attr->kobj_attr.attr.name);
3693 static void sysfs_wi_node_delete_all(void)
3697 for (nid = 0; nid < nr_node_ids; nid++)
3698 sysfs_wi_node_delete(nid);
3701 static void wi_state_free(void)
3703 struct weighted_interleave_state *old_wi_state;
3705 mutex_lock(&wi_state_lock);
3707 old_wi_state = rcu_dereference_protected(wi_state,
3708 lockdep_is_held(&wi_state_lock));
3709 if (!old_wi_state) {
3710 mutex_unlock(&wi_state_lock);
3714 rcu_assign_pointer(wi_state, NULL);
3715 mutex_unlock(&wi_state_lock);
3717 kfree(old_wi_state);
3720 static struct kobj_attribute wi_auto_attr =
3721 __ATTR(auto, 0664, weighted_interleave_auto_show,
3722 weighted_interleave_auto_store);
3724 static void wi_cleanup(void) {
3725 sysfs_remove_file(&wi_group->wi_kobj, &wi_auto_attr.attr);
3726 sysfs_wi_node_delete_all();
3730 static void wi_kobj_release(struct kobject *wi_kobj)
3735 static const struct kobj_type wi_ktype = {
3736 .sysfs_ops = &kobj_sysfs_ops,
3737 .release = wi_kobj_release,
3740 static int sysfs_wi_node_add(int nid)
3744 struct iw_node_attr *new_attr;
3746 if (nid < 0 || nid >= nr_node_ids) {
3747 pr_err("invalid node id: %d\n", nid);
3751 new_attr = kzalloc(sizeof(*new_attr), GFP_KERNEL);
3755 name = kasprintf(GFP_KERNEL, "node%d", nid);
3761 sysfs_attr_init(&new_attr->kobj_attr.attr);
3762 new_attr->kobj_attr.attr.name = name;
3763 new_attr->kobj_attr.attr.mode = 0644;
3764 new_attr->kobj_attr.show = node_show;
3765 new_attr->kobj_attr.store = node_store;
3766 new_attr->nid = nid;
3768 mutex_lock(&wi_group->kobj_lock);
3769 if (wi_group->nattrs[nid]) {
3770 mutex_unlock(&wi_group->kobj_lock);
3775 ret = sysfs_create_file(&wi_group->wi_kobj, &new_attr->kobj_attr.attr);
3777 mutex_unlock(&wi_group->kobj_lock);
3780 wi_group->nattrs[nid] = new_attr;
3781 mutex_unlock(&wi_group->kobj_lock);
3785 kfree(new_attr->kobj_attr.attr.name);
3790 static int wi_node_notifier(struct notifier_block *nb,
3791 unsigned long action, void *data)
3794 struct memory_notify *arg = data;
3795 int nid = arg->status_change_nid;
3802 err = sysfs_wi_node_add(nid);
3804 pr_err("failed to add sysfs for node%d during hotplug: %d\n",
3808 sysfs_wi_node_delete(nid);
3815 static int __init add_weighted_interleave_group(struct kobject *mempolicy_kobj)
3819 wi_group = kzalloc(struct_size(wi_group, nattrs, nr_node_ids),
3823 mutex_init(&wi_group->kobj_lock);
3825 err = kobject_init_and_add(&wi_group->wi_kobj, &wi_ktype, mempolicy_kobj,
3826 "weighted_interleave");
3830 err = sysfs_create_file(&wi_group->wi_kobj, &wi_auto_attr.attr);
3834 for_each_online_node(nid) {
3835 if (!node_state(nid, N_MEMORY))
3838 err = sysfs_wi_node_add(nid);
3840 pr_err("failed to add sysfs for node%d during init: %d\n",
3842 goto err_cleanup_kobj;
3846 hotplug_memory_notifier(wi_node_notifier, DEFAULT_CALLBACK_PRI);
3851 kobject_del(&wi_group->wi_kobj);
3853 kobject_put(&wi_group->wi_kobj);
3857 static int __init mempolicy_sysfs_init(void)
3860 static struct kobject *mempolicy_kobj;
3862 mempolicy_kobj = kobject_create_and_add("mempolicy", mm_kobj);
3863 if (!mempolicy_kobj)
3866 err = add_weighted_interleave_group(mempolicy_kobj);
3873 kobject_del(mempolicy_kobj);
3874 kobject_put(mempolicy_kobj);
3878 late_initcall(mempolicy_sysfs_init);
3879 #endif /* CONFIG_SYSFS */