2 * Simple NUMA memory policy for the Linux kernel.
4 * Copyright 2003,2004 Andi Kleen, SuSE Labs.
5 * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
6 * Subject to the GNU Public License, version 2.
8 * NUMA policy allows the user to give hints in which node(s) memory should
11 * Support four 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
22 * bind Only allocate memory on a specific set of nodes,
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
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
34 * default Allocate on the local node first, or when on a VMA
35 * use the process policy. This is what Linux always did
36 * in a NUMA aware kernel and still does by, ahem, default.
38 * The process policy is applied for most non interrupt memory allocations
39 * in that process' context. Interrupts ignore the policies and always
40 * try to allocate on the local CPU. The VMA policy is only applied for memory
41 * allocations for a VMA in the VM.
43 * Currently there are a few corner cases in swapping where the policy
44 * is not applied, but the majority should be handled. When process policy
45 * is used it is not remembered over swap outs/swap ins.
47 * Only the highest zone in the zone hierarchy gets policied. Allocations
48 * requesting a lower zone just use default policy. This implies that
49 * on systems with highmem kernel lowmem allocation don't get policied.
50 * Same with GFP_DMA allocations.
52 * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
53 * all users and remembered even when nobody has memory mapped.
57 fix mmap readahead to honour policy and enable policy for any page cache
59 statistics for bigpages
60 global policy for page cache? currently it uses process policy. Requires
62 handle mremap for shared memory (currently ignored for the policy)
64 make bind policy root only? It can trigger oom much faster and the
65 kernel is not always grateful with that.
68 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
70 #include <linux/mempolicy.h>
72 #include <linux/highmem.h>
73 #include <linux/hugetlb.h>
74 #include <linux/kernel.h>
75 #include <linux/sched.h>
76 #include <linux/nodemask.h>
77 #include <linux/cpuset.h>
78 #include <linux/slab.h>
79 #include <linux/string.h>
80 #include <linux/export.h>
81 #include <linux/nsproxy.h>
82 #include <linux/interrupt.h>
83 #include <linux/init.h>
84 #include <linux/compat.h>
85 #include <linux/swap.h>
86 #include <linux/seq_file.h>
87 #include <linux/proc_fs.h>
88 #include <linux/migrate.h>
89 #include <linux/ksm.h>
90 #include <linux/rmap.h>
91 #include <linux/security.h>
92 #include <linux/syscalls.h>
93 #include <linux/ctype.h>
94 #include <linux/mm_inline.h>
95 #include <linux/mmu_notifier.h>
96 #include <linux/printk.h>
98 #include <asm/tlbflush.h>
99 #include <asm/uaccess.h>
100 #include <linux/random.h>
102 #include "internal.h"
105 #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */
106 #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */
108 static struct kmem_cache *policy_cache;
109 static struct kmem_cache *sn_cache;
111 /* Highest zone. An specific allocation for a zone below that is not
113 enum zone_type policy_zone = 0;
116 * run-time system-wide default policy => local allocation
118 static struct mempolicy default_policy = {
119 .refcnt = ATOMIC_INIT(1), /* never free it */
120 .mode = MPOL_PREFERRED,
121 .flags = MPOL_F_LOCAL,
124 static struct mempolicy preferred_node_policy[MAX_NUMNODES];
126 static struct mempolicy *get_task_policy(struct task_struct *p)
128 struct mempolicy *pol = p->mempolicy;
131 int node = numa_node_id();
133 if (node != NUMA_NO_NODE) {
134 pol = &preferred_node_policy[node];
136 * preferred_node_policy is not initialised early in
147 static const struct mempolicy_operations {
148 int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
150 * If read-side task has no lock to protect task->mempolicy, write-side
151 * task will rebind the task->mempolicy by two step. The first step is
152 * setting all the newly nodes, and the second step is cleaning all the
153 * disallowed nodes. In this way, we can avoid finding no node to alloc
155 * If we have a lock to protect task->mempolicy in read-side, we do
159 * MPOL_REBIND_ONCE - do rebind work at once
160 * MPOL_REBIND_STEP1 - set all the newly nodes
161 * MPOL_REBIND_STEP2 - clean all the disallowed nodes
163 void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes,
164 enum mpol_rebind_step step);
165 } mpol_ops[MPOL_MAX];
167 /* Check that the nodemask contains at least one populated zone */
168 static int is_valid_nodemask(const nodemask_t *nodemask)
170 return nodes_intersects(*nodemask, node_states[N_MEMORY]);
173 static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
175 return pol->flags & MPOL_MODE_FLAGS;
178 static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
179 const nodemask_t *rel)
182 nodes_fold(tmp, *orig, nodes_weight(*rel));
183 nodes_onto(*ret, tmp, *rel);
186 static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
188 if (nodes_empty(*nodes))
190 pol->v.nodes = *nodes;
194 static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
197 pol->flags |= MPOL_F_LOCAL; /* local allocation */
198 else if (nodes_empty(*nodes))
199 return -EINVAL; /* no allowed nodes */
201 pol->v.preferred_node = first_node(*nodes);
205 static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
207 if (!is_valid_nodemask(nodes))
209 pol->v.nodes = *nodes;
214 * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
215 * any, for the new policy. mpol_new() has already validated the nodes
216 * parameter with respect to the policy mode and flags. But, we need to
217 * handle an empty nodemask with MPOL_PREFERRED here.
219 * Must be called holding task's alloc_lock to protect task's mems_allowed
220 * and mempolicy. May also be called holding the mmap_semaphore for write.
222 static int mpol_set_nodemask(struct mempolicy *pol,
223 const nodemask_t *nodes, struct nodemask_scratch *nsc)
227 /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
231 nodes_and(nsc->mask1,
232 cpuset_current_mems_allowed, node_states[N_MEMORY]);
235 if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
236 nodes = NULL; /* explicit local allocation */
238 if (pol->flags & MPOL_F_RELATIVE_NODES)
239 mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1);
241 nodes_and(nsc->mask2, *nodes, nsc->mask1);
243 if (mpol_store_user_nodemask(pol))
244 pol->w.user_nodemask = *nodes;
246 pol->w.cpuset_mems_allowed =
247 cpuset_current_mems_allowed;
251 ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
253 ret = mpol_ops[pol->mode].create(pol, NULL);
258 * This function just creates a new policy, does some check and simple
259 * initialization. You must invoke mpol_set_nodemask() to set nodes.
261 static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
264 struct mempolicy *policy;
266 pr_debug("setting mode %d flags %d nodes[0] %lx\n",
267 mode, flags, nodes ? nodes_addr(*nodes)[0] : NUMA_NO_NODE);
269 if (mode == MPOL_DEFAULT) {
270 if (nodes && !nodes_empty(*nodes))
271 return ERR_PTR(-EINVAL);
277 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
278 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
279 * All other modes require a valid pointer to a non-empty nodemask.
281 if (mode == MPOL_PREFERRED) {
282 if (nodes_empty(*nodes)) {
283 if (((flags & MPOL_F_STATIC_NODES) ||
284 (flags & MPOL_F_RELATIVE_NODES)))
285 return ERR_PTR(-EINVAL);
287 } else if (mode == MPOL_LOCAL) {
288 if (!nodes_empty(*nodes))
289 return ERR_PTR(-EINVAL);
290 mode = MPOL_PREFERRED;
291 } else if (nodes_empty(*nodes))
292 return ERR_PTR(-EINVAL);
293 policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
295 return ERR_PTR(-ENOMEM);
296 atomic_set(&policy->refcnt, 1);
298 policy->flags = flags;
303 /* Slow path of a mpol destructor. */
304 void __mpol_put(struct mempolicy *p)
306 if (!atomic_dec_and_test(&p->refcnt))
308 kmem_cache_free(policy_cache, p);
311 static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes,
312 enum mpol_rebind_step step)
318 * MPOL_REBIND_ONCE - do rebind work at once
319 * MPOL_REBIND_STEP1 - set all the newly nodes
320 * MPOL_REBIND_STEP2 - clean all the disallowed nodes
322 static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes,
323 enum mpol_rebind_step step)
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);
333 * if step == 1, we use ->w.cpuset_mems_allowed to cache the
336 if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) {
337 nodes_remap(tmp, pol->v.nodes,
338 pol->w.cpuset_mems_allowed, *nodes);
339 pol->w.cpuset_mems_allowed = step ? tmp : *nodes;
340 } else if (step == MPOL_REBIND_STEP2) {
341 tmp = pol->w.cpuset_mems_allowed;
342 pol->w.cpuset_mems_allowed = *nodes;
347 if (nodes_empty(tmp))
350 if (step == MPOL_REBIND_STEP1)
351 nodes_or(pol->v.nodes, pol->v.nodes, tmp);
352 else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2)
357 if (!node_isset(current->il_next, tmp)) {
358 current->il_next = next_node(current->il_next, tmp);
359 if (current->il_next >= MAX_NUMNODES)
360 current->il_next = first_node(tmp);
361 if (current->il_next >= MAX_NUMNODES)
362 current->il_next = numa_node_id();
366 static void mpol_rebind_preferred(struct mempolicy *pol,
367 const nodemask_t *nodes,
368 enum mpol_rebind_step step)
372 if (pol->flags & MPOL_F_STATIC_NODES) {
373 int node = first_node(pol->w.user_nodemask);
375 if (node_isset(node, *nodes)) {
376 pol->v.preferred_node = node;
377 pol->flags &= ~MPOL_F_LOCAL;
379 pol->flags |= MPOL_F_LOCAL;
380 } else if (pol->flags & MPOL_F_RELATIVE_NODES) {
381 mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
382 pol->v.preferred_node = first_node(tmp);
383 } else if (!(pol->flags & MPOL_F_LOCAL)) {
384 pol->v.preferred_node = node_remap(pol->v.preferred_node,
385 pol->w.cpuset_mems_allowed,
387 pol->w.cpuset_mems_allowed = *nodes;
392 * mpol_rebind_policy - Migrate a policy to a different set of nodes
394 * If read-side task has no lock to protect task->mempolicy, write-side
395 * task will rebind the task->mempolicy by two step. The first step is
396 * setting all the newly nodes, and the second step is cleaning all the
397 * disallowed nodes. In this way, we can avoid finding no node to alloc
399 * If we have a lock to protect task->mempolicy in read-side, we do
403 * MPOL_REBIND_ONCE - do rebind work at once
404 * MPOL_REBIND_STEP1 - set all the newly nodes
405 * MPOL_REBIND_STEP2 - clean all the disallowed nodes
407 static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask,
408 enum mpol_rebind_step step)
412 if (!mpol_store_user_nodemask(pol) && step == MPOL_REBIND_ONCE &&
413 nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
416 if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING))
419 if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING))
422 if (step == MPOL_REBIND_STEP1)
423 pol->flags |= MPOL_F_REBINDING;
424 else if (step == MPOL_REBIND_STEP2)
425 pol->flags &= ~MPOL_F_REBINDING;
426 else if (step >= MPOL_REBIND_NSTEP)
429 mpol_ops[pol->mode].rebind(pol, newmask, step);
433 * Wrapper for mpol_rebind_policy() that just requires task
434 * pointer, and updates task mempolicy.
436 * Called with task's alloc_lock held.
439 void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
440 enum mpol_rebind_step step)
442 mpol_rebind_policy(tsk->mempolicy, new, step);
446 * Rebind each vma in mm to new nodemask.
448 * Call holding a reference to mm. Takes mm->mmap_sem during call.
451 void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
453 struct vm_area_struct *vma;
455 down_write(&mm->mmap_sem);
456 for (vma = mm->mmap; vma; vma = vma->vm_next)
457 mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE);
458 up_write(&mm->mmap_sem);
461 static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
463 .rebind = mpol_rebind_default,
465 [MPOL_INTERLEAVE] = {
466 .create = mpol_new_interleave,
467 .rebind = mpol_rebind_nodemask,
470 .create = mpol_new_preferred,
471 .rebind = mpol_rebind_preferred,
474 .create = mpol_new_bind,
475 .rebind = mpol_rebind_nodemask,
479 static void migrate_page_add(struct page *page, struct list_head *pagelist,
480 unsigned long flags);
483 * Scan through pages checking if pages follow certain conditions,
484 * and move them to the pagelist if they do.
486 static int queue_pages_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
487 unsigned long addr, unsigned long end,
488 const nodemask_t *nodes, unsigned long flags,
495 orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
500 if (!pte_present(*pte))
502 page = vm_normal_page(vma, addr, *pte);
506 * vm_normal_page() filters out zero pages, but there might
507 * still be PageReserved pages to skip, perhaps in a VDSO.
509 if (PageReserved(page))
511 nid = page_to_nid(page);
512 if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
515 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
516 migrate_page_add(page, private, flags);
519 } while (pte++, addr += PAGE_SIZE, addr != end);
520 pte_unmap_unlock(orig_pte, ptl);
524 static void queue_pages_hugetlb_pmd_range(struct vm_area_struct *vma,
525 pmd_t *pmd, const nodemask_t *nodes, unsigned long flags,
528 #ifdef CONFIG_HUGETLB_PAGE
533 ptl = huge_pte_lock(hstate_vma(vma), vma->vm_mm, (pte_t *)pmd);
534 page = pte_page(huge_ptep_get((pte_t *)pmd));
535 nid = page_to_nid(page);
536 if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
538 /* With MPOL_MF_MOVE, we migrate only unshared hugepage. */
539 if (flags & (MPOL_MF_MOVE_ALL) ||
540 (flags & MPOL_MF_MOVE && page_mapcount(page) == 1))
541 isolate_huge_page(page, private);
549 static inline int queue_pages_pmd_range(struct vm_area_struct *vma, pud_t *pud,
550 unsigned long addr, unsigned long end,
551 const nodemask_t *nodes, unsigned long flags,
557 pmd = pmd_offset(pud, addr);
559 next = pmd_addr_end(addr, end);
560 if (!pmd_present(*pmd))
562 if (pmd_huge(*pmd) && is_vm_hugetlb_page(vma)) {
563 queue_pages_hugetlb_pmd_range(vma, pmd, nodes,
567 split_huge_page_pmd(vma, addr, pmd);
568 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
570 if (queue_pages_pte_range(vma, pmd, addr, next, nodes,
573 } while (pmd++, addr = next, addr != end);
577 static inline int queue_pages_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
578 unsigned long addr, unsigned long end,
579 const nodemask_t *nodes, unsigned long flags,
585 pud = pud_offset(pgd, addr);
587 next = pud_addr_end(addr, end);
588 if (pud_huge(*pud) && is_vm_hugetlb_page(vma))
590 if (pud_none_or_clear_bad(pud))
592 if (queue_pages_pmd_range(vma, pud, addr, next, nodes,
595 } while (pud++, addr = next, addr != end);
599 static inline int queue_pages_pgd_range(struct vm_area_struct *vma,
600 unsigned long addr, unsigned long end,
601 const nodemask_t *nodes, unsigned long flags,
607 pgd = pgd_offset(vma->vm_mm, addr);
609 next = pgd_addr_end(addr, end);
610 if (pgd_none_or_clear_bad(pgd))
612 if (queue_pages_pud_range(vma, pgd, addr, next, nodes,
615 } while (pgd++, addr = next, addr != end);
619 #ifdef CONFIG_NUMA_BALANCING
621 * This is used to mark a range of virtual addresses to be inaccessible.
622 * These are later cleared by a NUMA hinting fault. Depending on these
623 * faults, pages may be migrated for better NUMA placement.
625 * This is assuming that NUMA faults are handled using PROT_NONE. If
626 * an architecture makes a different choice, it will need further
627 * changes to the core.
629 unsigned long change_prot_numa(struct vm_area_struct *vma,
630 unsigned long addr, unsigned long end)
634 nr_updated = change_protection(vma, addr, end, vma->vm_page_prot, 0, 1);
636 count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
641 static unsigned long change_prot_numa(struct vm_area_struct *vma,
642 unsigned long addr, unsigned long end)
646 #endif /* CONFIG_NUMA_BALANCING */
649 * Walk through page tables and collect pages to be migrated.
651 * If pages found in a given range are on a set of nodes (determined by
652 * @nodes and @flags,) it's isolated and queued to the pagelist which is
653 * passed via @private.)
655 static struct vm_area_struct *
656 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
657 const nodemask_t *nodes, unsigned long flags, void *private)
660 struct vm_area_struct *first, *vma, *prev;
663 first = find_vma(mm, start);
665 return ERR_PTR(-EFAULT);
667 for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
668 unsigned long endvma = vma->vm_end;
672 if (vma->vm_start > start)
673 start = vma->vm_start;
675 if (!(flags & MPOL_MF_DISCONTIG_OK)) {
676 if (!vma->vm_next && vma->vm_end < end)
677 return ERR_PTR(-EFAULT);
678 if (prev && prev->vm_end < vma->vm_start)
679 return ERR_PTR(-EFAULT);
682 if (flags & MPOL_MF_LAZY) {
683 change_prot_numa(vma, start, endvma);
687 if ((flags & MPOL_MF_STRICT) ||
688 ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
689 vma_migratable(vma))) {
691 err = queue_pages_pgd_range(vma, start, endvma, nodes,
694 first = ERR_PTR(err);
705 * Apply policy to a single VMA
706 * This must be called with the mmap_sem held for writing.
708 static int vma_replace_policy(struct vm_area_struct *vma,
709 struct mempolicy *pol)
712 struct mempolicy *old;
713 struct mempolicy *new;
715 pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
716 vma->vm_start, vma->vm_end, vma->vm_pgoff,
717 vma->vm_ops, vma->vm_file,
718 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
724 if (vma->vm_ops && vma->vm_ops->set_policy) {
725 err = vma->vm_ops->set_policy(vma, new);
730 old = vma->vm_policy;
731 vma->vm_policy = new; /* protected by mmap_sem */
740 /* Step 2: apply policy to a range and do splits. */
741 static int mbind_range(struct mm_struct *mm, unsigned long start,
742 unsigned long end, struct mempolicy *new_pol)
744 struct vm_area_struct *next;
745 struct vm_area_struct *prev;
746 struct vm_area_struct *vma;
749 unsigned long vmstart;
752 vma = find_vma(mm, start);
753 if (!vma || vma->vm_start > start)
757 if (start > vma->vm_start)
760 for (; vma && vma->vm_start < end; prev = vma, vma = next) {
762 vmstart = max(start, vma->vm_start);
763 vmend = min(end, vma->vm_end);
765 if (mpol_equal(vma_policy(vma), new_pol))
768 pgoff = vma->vm_pgoff +
769 ((vmstart - vma->vm_start) >> PAGE_SHIFT);
770 prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
771 vma->anon_vma, vma->vm_file, pgoff,
776 if (mpol_equal(vma_policy(vma), new_pol))
778 /* vma_merge() joined vma && vma->next, case 8 */
781 if (vma->vm_start != vmstart) {
782 err = split_vma(vma->vm_mm, vma, vmstart, 1);
786 if (vma->vm_end != vmend) {
787 err = split_vma(vma->vm_mm, vma, vmend, 0);
792 err = vma_replace_policy(vma, new_pol);
801 /* Set the process memory policy */
802 static long do_set_mempolicy(unsigned short mode, unsigned short flags,
805 struct mempolicy *new, *old;
806 struct mm_struct *mm = current->mm;
807 NODEMASK_SCRATCH(scratch);
813 new = mpol_new(mode, flags, nodes);
819 * prevent changing our mempolicy while show_numa_maps()
821 * Note: do_set_mempolicy() can be called at init time
825 down_write(&mm->mmap_sem);
827 ret = mpol_set_nodemask(new, nodes, scratch);
829 task_unlock(current);
831 up_write(&mm->mmap_sem);
835 old = current->mempolicy;
836 current->mempolicy = new;
837 if (new && new->mode == MPOL_INTERLEAVE &&
838 nodes_weight(new->v.nodes))
839 current->il_next = first_node(new->v.nodes);
840 task_unlock(current);
842 up_write(&mm->mmap_sem);
847 NODEMASK_SCRATCH_FREE(scratch);
852 * Return nodemask for policy for get_mempolicy() query
854 * Called with task's alloc_lock held
856 static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
859 if (p == &default_policy)
865 case MPOL_INTERLEAVE:
869 if (!(p->flags & MPOL_F_LOCAL))
870 node_set(p->v.preferred_node, *nodes);
871 /* else return empty node mask for local allocation */
878 static int lookup_node(struct mm_struct *mm, unsigned long addr)
883 err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
885 err = page_to_nid(p);
891 /* Retrieve NUMA policy */
892 static long do_get_mempolicy(int *policy, nodemask_t *nmask,
893 unsigned long addr, unsigned long flags)
896 struct mm_struct *mm = current->mm;
897 struct vm_area_struct *vma = NULL;
898 struct mempolicy *pol = current->mempolicy;
901 ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
904 if (flags & MPOL_F_MEMS_ALLOWED) {
905 if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
907 *policy = 0; /* just so it's initialized */
909 *nmask = cpuset_current_mems_allowed;
910 task_unlock(current);
914 if (flags & MPOL_F_ADDR) {
916 * Do NOT fall back to task policy if the
917 * vma/shared policy at addr is NULL. We
918 * want to return MPOL_DEFAULT in this case.
920 down_read(&mm->mmap_sem);
921 vma = find_vma_intersection(mm, addr, addr+1);
923 up_read(&mm->mmap_sem);
926 if (vma->vm_ops && vma->vm_ops->get_policy)
927 pol = vma->vm_ops->get_policy(vma, addr);
929 pol = vma->vm_policy;
934 pol = &default_policy; /* indicates default behavior */
936 if (flags & MPOL_F_NODE) {
937 if (flags & MPOL_F_ADDR) {
938 err = lookup_node(mm, addr);
942 } else if (pol == current->mempolicy &&
943 pol->mode == MPOL_INTERLEAVE) {
944 *policy = current->il_next;
950 *policy = pol == &default_policy ? MPOL_DEFAULT :
953 * Internal mempolicy flags must be masked off before exposing
954 * the policy to userspace.
956 *policy |= (pol->flags & MPOL_MODE_FLAGS);
960 up_read(¤t->mm->mmap_sem);
966 if (mpol_store_user_nodemask(pol)) {
967 *nmask = pol->w.user_nodemask;
970 get_policy_nodemask(pol, nmask);
971 task_unlock(current);
978 up_read(¤t->mm->mmap_sem);
982 #ifdef CONFIG_MIGRATION
986 static void migrate_page_add(struct page *page, struct list_head *pagelist,
990 * Avoid migrating a page that is shared with others.
992 if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
993 if (!isolate_lru_page(page)) {
994 list_add_tail(&page->lru, pagelist);
995 inc_zone_page_state(page, NR_ISOLATED_ANON +
996 page_is_file_cache(page));
1001 static struct page *new_node_page(struct page *page, unsigned long node, int **x)
1004 return alloc_huge_page_node(page_hstate(compound_head(page)),
1007 return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
1011 * Migrate pages from one node to a target node.
1012 * Returns error or the number of pages not migrated.
1014 static int migrate_to_node(struct mm_struct *mm, int source, int dest,
1018 LIST_HEAD(pagelist);
1022 node_set(source, nmask);
1025 * This does not "check" the range but isolates all pages that
1026 * need migration. Between passing in the full user address
1027 * space range and MPOL_MF_DISCONTIG_OK, this call can not fail.
1029 VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)));
1030 queue_pages_range(mm, mm->mmap->vm_start, mm->task_size, &nmask,
1031 flags | MPOL_MF_DISCONTIG_OK, &pagelist);
1033 if (!list_empty(&pagelist)) {
1034 err = migrate_pages(&pagelist, new_node_page, NULL, dest,
1035 MIGRATE_SYNC, MR_SYSCALL);
1037 putback_movable_pages(&pagelist);
1044 * Move pages between the two nodesets so as to preserve the physical
1045 * layout as much as possible.
1047 * Returns the number of page that could not be moved.
1049 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
1050 const nodemask_t *to, int flags)
1056 err = migrate_prep();
1060 down_read(&mm->mmap_sem);
1062 err = migrate_vmas(mm, from, to, flags);
1067 * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
1068 * bit in 'to' is not also set in 'tmp'. Clear the found 'source'
1069 * bit in 'tmp', and return that <source, dest> pair for migration.
1070 * The pair of nodemasks 'to' and 'from' define the map.
1072 * If no pair of bits is found that way, fallback to picking some
1073 * pair of 'source' and 'dest' bits that are not the same. If the
1074 * 'source' and 'dest' bits are the same, this represents a node
1075 * that will be migrating to itself, so no pages need move.
1077 * If no bits are left in 'tmp', or if all remaining bits left
1078 * in 'tmp' correspond to the same bit in 'to', return false
1079 * (nothing left to migrate).
1081 * This lets us pick a pair of nodes to migrate between, such that
1082 * if possible the dest node is not already occupied by some other
1083 * source node, minimizing the risk of overloading the memory on a
1084 * node that would happen if we migrated incoming memory to a node
1085 * before migrating outgoing memory source that same node.
1087 * A single scan of tmp is sufficient. As we go, we remember the
1088 * most recent <s, d> pair that moved (s != d). If we find a pair
1089 * that not only moved, but what's better, moved to an empty slot
1090 * (d is not set in tmp), then we break out then, with that pair.
1091 * Otherwise when we finish scanning from_tmp, we at least have the
1092 * most recent <s, d> pair that moved. If we get all the way through
1093 * the scan of tmp without finding any node that moved, much less
1094 * moved to an empty node, then there is nothing left worth migrating.
1098 while (!nodes_empty(tmp)) {
1100 int source = NUMA_NO_NODE;
1103 for_each_node_mask(s, tmp) {
1106 * do_migrate_pages() tries to maintain the relative
1107 * node relationship of the pages established between
1108 * threads and memory areas.
1110 * However if the number of source nodes is not equal to
1111 * the number of destination nodes we can not preserve
1112 * this node relative relationship. In that case, skip
1113 * copying memory from a node that is in the destination
1116 * Example: [2,3,4] -> [3,4,5] moves everything.
1117 * [0-7] - > [3,4,5] moves only 0,1,2,6,7.
1120 if ((nodes_weight(*from) != nodes_weight(*to)) &&
1121 (node_isset(s, *to)))
1124 d = node_remap(s, *from, *to);
1128 source = s; /* Node moved. Memorize */
1131 /* dest not in remaining from nodes? */
1132 if (!node_isset(dest, tmp))
1135 if (source == NUMA_NO_NODE)
1138 node_clear(source, tmp);
1139 err = migrate_to_node(mm, source, dest, flags);
1146 up_read(&mm->mmap_sem);
1154 * Allocate a new page for page migration based on vma policy.
1155 * Start assuming that page is mapped by vma pointed to by @private.
1156 * Search forward from there, if not. N.B., this assumes that the
1157 * list of pages handed to migrate_pages()--which is how we get here--
1158 * is in virtual address order.
1160 static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
1162 struct vm_area_struct *vma = (struct vm_area_struct *)private;
1163 unsigned long uninitialized_var(address);
1166 address = page_address_in_vma(page, vma);
1167 if (address != -EFAULT)
1172 if (PageHuge(page)) {
1174 return alloc_huge_page_noerr(vma, address, 1);
1177 * if !vma, alloc_page_vma() will use task or system default policy
1179 return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
1183 static void migrate_page_add(struct page *page, struct list_head *pagelist,
1184 unsigned long flags)
1188 int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
1189 const nodemask_t *to, int flags)
1194 static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
1200 static long do_mbind(unsigned long start, unsigned long len,
1201 unsigned short mode, unsigned short mode_flags,
1202 nodemask_t *nmask, unsigned long flags)
1204 struct vm_area_struct *vma;
1205 struct mm_struct *mm = current->mm;
1206 struct mempolicy *new;
1209 LIST_HEAD(pagelist);
1211 if (flags & ~(unsigned long)MPOL_MF_VALID)
1213 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1216 if (start & ~PAGE_MASK)
1219 if (mode == MPOL_DEFAULT)
1220 flags &= ~MPOL_MF_STRICT;
1222 len = (len + PAGE_SIZE - 1) & PAGE_MASK;
1230 new = mpol_new(mode, mode_flags, nmask);
1232 return PTR_ERR(new);
1234 if (flags & MPOL_MF_LAZY)
1235 new->flags |= MPOL_F_MOF;
1238 * If we are using the default policy then operation
1239 * on discontinuous address spaces is okay after all
1242 flags |= MPOL_MF_DISCONTIG_OK;
1244 pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
1245 start, start + len, mode, mode_flags,
1246 nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE);
1248 if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
1250 err = migrate_prep();
1255 NODEMASK_SCRATCH(scratch);
1257 down_write(&mm->mmap_sem);
1259 err = mpol_set_nodemask(new, nmask, scratch);
1260 task_unlock(current);
1262 up_write(&mm->mmap_sem);
1265 NODEMASK_SCRATCH_FREE(scratch);
1270 vma = queue_pages_range(mm, start, end, nmask,
1271 flags | MPOL_MF_INVERT, &pagelist);
1273 err = PTR_ERR(vma); /* maybe ... */
1275 err = mbind_range(mm, start, end, new);
1280 if (!list_empty(&pagelist)) {
1281 WARN_ON_ONCE(flags & MPOL_MF_LAZY);
1282 nr_failed = migrate_pages(&pagelist, new_vma_page,
1283 NULL, (unsigned long)vma,
1284 MIGRATE_SYNC, MR_MEMPOLICY_MBIND);
1286 putback_movable_pages(&pagelist);
1289 if (nr_failed && (flags & MPOL_MF_STRICT))
1292 putback_movable_pages(&pagelist);
1294 up_write(&mm->mmap_sem);
1301 * User space interface with variable sized bitmaps for nodelists.
1304 /* Copy a node mask from user space. */
1305 static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
1306 unsigned long maxnode)
1309 unsigned long nlongs;
1310 unsigned long endmask;
1313 nodes_clear(*nodes);
1314 if (maxnode == 0 || !nmask)
1316 if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
1319 nlongs = BITS_TO_LONGS(maxnode);
1320 if ((maxnode % BITS_PER_LONG) == 0)
1323 endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
1325 /* When the user specified more nodes than supported just check
1326 if the non supported part is all zero. */
1327 if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
1328 if (nlongs > PAGE_SIZE/sizeof(long))
1330 for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
1332 if (get_user(t, nmask + k))
1334 if (k == nlongs - 1) {
1340 nlongs = BITS_TO_LONGS(MAX_NUMNODES);
1344 if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
1346 nodes_addr(*nodes)[nlongs-1] &= endmask;
1350 /* Copy a kernel node mask to user space */
1351 static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
1354 unsigned long copy = ALIGN(maxnode-1, 64) / 8;
1355 const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
1357 if (copy > nbytes) {
1358 if (copy > PAGE_SIZE)
1360 if (clear_user((char __user *)mask + nbytes, copy - nbytes))
1364 return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
1367 SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
1368 unsigned long, mode, const unsigned long __user *, nmask,
1369 unsigned long, maxnode, unsigned, flags)
1373 unsigned short mode_flags;
1375 mode_flags = mode & MPOL_MODE_FLAGS;
1376 mode &= ~MPOL_MODE_FLAGS;
1377 if (mode >= MPOL_MAX)
1379 if ((mode_flags & MPOL_F_STATIC_NODES) &&
1380 (mode_flags & MPOL_F_RELATIVE_NODES))
1382 err = get_nodes(&nodes, nmask, maxnode);
1385 return do_mbind(start, len, mode, mode_flags, &nodes, flags);
1388 /* Set the process memory policy */
1389 SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask,
1390 unsigned long, maxnode)
1394 unsigned short flags;
1396 flags = mode & MPOL_MODE_FLAGS;
1397 mode &= ~MPOL_MODE_FLAGS;
1398 if ((unsigned int)mode >= MPOL_MAX)
1400 if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
1402 err = get_nodes(&nodes, nmask, maxnode);
1405 return do_set_mempolicy(mode, flags, &nodes);
1408 SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
1409 const unsigned long __user *, old_nodes,
1410 const unsigned long __user *, new_nodes)
1412 const struct cred *cred = current_cred(), *tcred;
1413 struct mm_struct *mm = NULL;
1414 struct task_struct *task;
1415 nodemask_t task_nodes;
1419 NODEMASK_SCRATCH(scratch);
1424 old = &scratch->mask1;
1425 new = &scratch->mask2;
1427 err = get_nodes(old, old_nodes, maxnode);
1431 err = get_nodes(new, new_nodes, maxnode);
1435 /* Find the mm_struct */
1437 task = pid ? find_task_by_vpid(pid) : current;
1443 get_task_struct(task);
1448 * Check if this process has the right to modify the specified
1449 * process. The right exists if the process has administrative
1450 * capabilities, superuser privileges or the same
1451 * userid as the target process.
1453 tcred = __task_cred(task);
1454 if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1455 !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
1456 !capable(CAP_SYS_NICE)) {
1463 task_nodes = cpuset_mems_allowed(task);
1464 /* Is the user allowed to access the target nodes? */
1465 if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
1470 if (!nodes_subset(*new, node_states[N_MEMORY])) {
1475 err = security_task_movememory(task);
1479 mm = get_task_mm(task);
1480 put_task_struct(task);
1487 err = do_migrate_pages(mm, old, new,
1488 capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
1492 NODEMASK_SCRATCH_FREE(scratch);
1497 put_task_struct(task);
1503 /* Retrieve NUMA policy */
1504 SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1505 unsigned long __user *, nmask, unsigned long, maxnode,
1506 unsigned long, addr, unsigned long, flags)
1509 int uninitialized_var(pval);
1512 if (nmask != NULL && maxnode < MAX_NUMNODES)
1515 err = do_get_mempolicy(&pval, &nodes, addr, flags);
1520 if (policy && put_user(pval, policy))
1524 err = copy_nodes_to_user(nmask, maxnode, &nodes);
1529 #ifdef CONFIG_COMPAT
1531 COMPAT_SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1532 compat_ulong_t __user *, nmask,
1533 compat_ulong_t, maxnode,
1534 compat_ulong_t, addr, compat_ulong_t, flags)
1537 unsigned long __user *nm = NULL;
1538 unsigned long nr_bits, alloc_size;
1539 DECLARE_BITMAP(bm, MAX_NUMNODES);
1541 nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
1542 alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1545 nm = compat_alloc_user_space(alloc_size);
1547 err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
1549 if (!err && nmask) {
1550 unsigned long copy_size;
1551 copy_size = min_t(unsigned long, sizeof(bm), alloc_size);
1552 err = copy_from_user(bm, nm, copy_size);
1553 /* ensure entire bitmap is zeroed */
1554 err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
1555 err |= compat_put_bitmap(nmask, bm, nr_bits);
1561 COMPAT_SYSCALL_DEFINE3(set_mempolicy, int, mode, compat_ulong_t __user *, nmask,
1562 compat_ulong_t, maxnode)
1565 unsigned long __user *nm = NULL;
1566 unsigned long nr_bits, alloc_size;
1567 DECLARE_BITMAP(bm, MAX_NUMNODES);
1569 nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
1570 alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1573 err = compat_get_bitmap(bm, nmask, nr_bits);
1574 nm = compat_alloc_user_space(alloc_size);
1575 err |= copy_to_user(nm, bm, alloc_size);
1581 return sys_set_mempolicy(mode, nm, nr_bits+1);
1584 COMPAT_SYSCALL_DEFINE6(mbind, compat_ulong_t, start, compat_ulong_t, len,
1585 compat_ulong_t, mode, compat_ulong_t __user *, nmask,
1586 compat_ulong_t, maxnode, compat_ulong_t, flags)
1589 unsigned long __user *nm = NULL;
1590 unsigned long nr_bits, alloc_size;
1593 nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
1594 alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
1597 err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
1598 nm = compat_alloc_user_space(alloc_size);
1599 err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
1605 return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
1611 * get_vma_policy(@task, @vma, @addr)
1612 * @task: task for fallback if vma policy == default
1613 * @vma: virtual memory area whose policy is sought
1614 * @addr: address in @vma for shared policy lookup
1616 * Returns effective policy for a VMA at specified address.
1617 * Falls back to @task or system default policy, as necessary.
1618 * Current or other task's task mempolicy and non-shared vma policies must be
1619 * protected by task_lock(task) by the caller.
1620 * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
1621 * count--added by the get_policy() vm_op, as appropriate--to protect against
1622 * freeing by another task. It is the caller's responsibility to free the
1623 * extra reference for shared policies.
1625 struct mempolicy *get_vma_policy(struct task_struct *task,
1626 struct vm_area_struct *vma, unsigned long addr)
1628 struct mempolicy *pol = get_task_policy(task);
1631 if (vma->vm_ops && vma->vm_ops->get_policy) {
1632 struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
1636 } else if (vma->vm_policy) {
1637 pol = vma->vm_policy;
1640 * shmem_alloc_page() passes MPOL_F_SHARED policy with
1641 * a pseudo vma whose vma->vm_ops=NULL. Take a reference
1642 * count on these policies which will be dropped by
1643 * mpol_cond_put() later
1645 if (mpol_needs_cond_ref(pol))
1650 pol = &default_policy;
1654 bool vma_policy_mof(struct task_struct *task, struct vm_area_struct *vma)
1656 struct mempolicy *pol = get_task_policy(task);
1658 if (vma->vm_ops && vma->vm_ops->get_policy) {
1661 pol = vma->vm_ops->get_policy(vma, vma->vm_start);
1662 if (pol && (pol->flags & MPOL_F_MOF))
1667 } else if (vma->vm_policy) {
1668 pol = vma->vm_policy;
1673 return default_policy.flags & MPOL_F_MOF;
1675 return pol->flags & MPOL_F_MOF;
1678 static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
1680 enum zone_type dynamic_policy_zone = policy_zone;
1682 BUG_ON(dynamic_policy_zone == ZONE_MOVABLE);
1685 * if policy->v.nodes has movable memory only,
1686 * we apply policy when gfp_zone(gfp) = ZONE_MOVABLE only.
1688 * policy->v.nodes is intersect with node_states[N_MEMORY].
1689 * so if the following test faile, it implies
1690 * policy->v.nodes has movable memory only.
1692 if (!nodes_intersects(policy->v.nodes, node_states[N_HIGH_MEMORY]))
1693 dynamic_policy_zone = ZONE_MOVABLE;
1695 return zone >= dynamic_policy_zone;
1699 * Return a nodemask representing a mempolicy for filtering nodes for
1702 static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
1704 /* Lower zones don't get a nodemask applied for MPOL_BIND */
1705 if (unlikely(policy->mode == MPOL_BIND) &&
1706 apply_policy_zone(policy, gfp_zone(gfp)) &&
1707 cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
1708 return &policy->v.nodes;
1713 /* Return a zonelist indicated by gfp for node representing a mempolicy */
1714 static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy,
1717 switch (policy->mode) {
1718 case MPOL_PREFERRED:
1719 if (!(policy->flags & MPOL_F_LOCAL))
1720 nd = policy->v.preferred_node;
1724 * Normally, MPOL_BIND allocations are node-local within the
1725 * allowed nodemask. However, if __GFP_THISNODE is set and the
1726 * current node isn't part of the mask, we use the zonelist for
1727 * the first node in the mask instead.
1729 if (unlikely(gfp & __GFP_THISNODE) &&
1730 unlikely(!node_isset(nd, policy->v.nodes)))
1731 nd = first_node(policy->v.nodes);
1736 return node_zonelist(nd, gfp);
1739 /* Do dynamic interleaving for a process */
1740 static unsigned interleave_nodes(struct mempolicy *policy)
1743 struct task_struct *me = current;
1746 next = next_node(nid, policy->v.nodes);
1747 if (next >= MAX_NUMNODES)
1748 next = first_node(policy->v.nodes);
1749 if (next < MAX_NUMNODES)
1755 * Depending on the memory policy provide a node from which to allocate the
1758 unsigned int mempolicy_slab_node(void)
1760 struct mempolicy *policy;
1761 int node = numa_mem_id();
1766 policy = current->mempolicy;
1767 if (!policy || policy->flags & MPOL_F_LOCAL)
1770 switch (policy->mode) {
1771 case MPOL_PREFERRED:
1773 * handled MPOL_F_LOCAL above
1775 return policy->v.preferred_node;
1777 case MPOL_INTERLEAVE:
1778 return interleave_nodes(policy);
1782 * Follow bind policy behavior and start allocation at the
1785 struct zonelist *zonelist;
1787 enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
1788 zonelist = &NODE_DATA(node)->node_zonelists[0];
1789 (void)first_zones_zonelist(zonelist, highest_zoneidx,
1792 return zone ? zone->node : node;
1800 /* Do static interleaving for a VMA with known offset. */
1801 static unsigned offset_il_node(struct mempolicy *pol,
1802 struct vm_area_struct *vma, unsigned long off)
1804 unsigned nnodes = nodes_weight(pol->v.nodes);
1807 int nid = NUMA_NO_NODE;
1810 return numa_node_id();
1811 target = (unsigned int)off % nnodes;
1814 nid = next_node(nid, pol->v.nodes);
1816 } while (c <= target);
1820 /* Determine a node number for interleave */
1821 static inline unsigned interleave_nid(struct mempolicy *pol,
1822 struct vm_area_struct *vma, unsigned long addr, int shift)
1828 * for small pages, there is no difference between
1829 * shift and PAGE_SHIFT, so the bit-shift is safe.
1830 * for huge pages, since vm_pgoff is in units of small
1831 * pages, we need to shift off the always 0 bits to get
1834 BUG_ON(shift < PAGE_SHIFT);
1835 off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
1836 off += (addr - vma->vm_start) >> shift;
1837 return offset_il_node(pol, vma, off);
1839 return interleave_nodes(pol);
1843 * Return the bit number of a random bit set in the nodemask.
1844 * (returns NUMA_NO_NODE if nodemask is empty)
1846 int node_random(const nodemask_t *maskp)
1848 int w, bit = NUMA_NO_NODE;
1850 w = nodes_weight(*maskp);
1852 bit = bitmap_ord_to_pos(maskp->bits,
1853 get_random_int() % w, MAX_NUMNODES);
1857 #ifdef CONFIG_HUGETLBFS
1859 * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
1860 * @vma: virtual memory area whose policy is sought
1861 * @addr: address in @vma for shared policy lookup and interleave policy
1862 * @gfp_flags: for requested zone
1863 * @mpol: pointer to mempolicy pointer for reference counted mempolicy
1864 * @nodemask: pointer to nodemask pointer for MPOL_BIND nodemask
1866 * Returns a zonelist suitable for a huge page allocation and a pointer
1867 * to the struct mempolicy for conditional unref after allocation.
1868 * If the effective policy is 'BIND, returns a pointer to the mempolicy's
1869 * @nodemask for filtering the zonelist.
1871 * Must be protected by read_mems_allowed_begin()
1873 struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
1874 gfp_t gfp_flags, struct mempolicy **mpol,
1875 nodemask_t **nodemask)
1877 struct zonelist *zl;
1879 *mpol = get_vma_policy(current, vma, addr);
1880 *nodemask = NULL; /* assume !MPOL_BIND */
1882 if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
1883 zl = node_zonelist(interleave_nid(*mpol, vma, addr,
1884 huge_page_shift(hstate_vma(vma))), gfp_flags);
1886 zl = policy_zonelist(gfp_flags, *mpol, numa_node_id());
1887 if ((*mpol)->mode == MPOL_BIND)
1888 *nodemask = &(*mpol)->v.nodes;
1894 * init_nodemask_of_mempolicy
1896 * If the current task's mempolicy is "default" [NULL], return 'false'
1897 * to indicate default policy. Otherwise, extract the policy nodemask
1898 * for 'bind' or 'interleave' policy into the argument nodemask, or
1899 * initialize the argument nodemask to contain the single node for
1900 * 'preferred' or 'local' policy and return 'true' to indicate presence
1901 * of non-default mempolicy.
1903 * We don't bother with reference counting the mempolicy [mpol_get/put]
1904 * because the current task is examining it's own mempolicy and a task's
1905 * mempolicy is only ever changed by the task itself.
1907 * N.B., it is the caller's responsibility to free a returned nodemask.
1909 bool init_nodemask_of_mempolicy(nodemask_t *mask)
1911 struct mempolicy *mempolicy;
1914 if (!(mask && current->mempolicy))
1918 mempolicy = current->mempolicy;
1919 switch (mempolicy->mode) {
1920 case MPOL_PREFERRED:
1921 if (mempolicy->flags & MPOL_F_LOCAL)
1922 nid = numa_node_id();
1924 nid = mempolicy->v.preferred_node;
1925 init_nodemask_of_node(mask, nid);
1930 case MPOL_INTERLEAVE:
1931 *mask = mempolicy->v.nodes;
1937 task_unlock(current);
1944 * mempolicy_nodemask_intersects
1946 * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
1947 * policy. Otherwise, check for intersection between mask and the policy
1948 * nodemask for 'bind' or 'interleave' policy. For 'perferred' or 'local'
1949 * policy, always return true since it may allocate elsewhere on fallback.
1951 * Takes task_lock(tsk) to prevent freeing of its mempolicy.
1953 bool mempolicy_nodemask_intersects(struct task_struct *tsk,
1954 const nodemask_t *mask)
1956 struct mempolicy *mempolicy;
1962 mempolicy = tsk->mempolicy;
1966 switch (mempolicy->mode) {
1967 case MPOL_PREFERRED:
1969 * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
1970 * allocate from, they may fallback to other nodes when oom.
1971 * Thus, it's possible for tsk to have allocated memory from
1976 case MPOL_INTERLEAVE:
1977 ret = nodes_intersects(mempolicy->v.nodes, *mask);
1987 /* Allocate a page in interleaved policy.
1988 Own path because it needs to do special accounting. */
1989 static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1992 struct zonelist *zl;
1995 zl = node_zonelist(nid, gfp);
1996 page = __alloc_pages(gfp, order, zl);
1997 if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
1998 inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
2003 * alloc_pages_vma - Allocate a page for a VMA.
2006 * %GFP_USER user allocation.
2007 * %GFP_KERNEL kernel allocations,
2008 * %GFP_HIGHMEM highmem/user allocations,
2009 * %GFP_FS allocation should not call back into a file system.
2010 * %GFP_ATOMIC don't sleep.
2012 * @order:Order of the GFP allocation.
2013 * @vma: Pointer to VMA or NULL if not available.
2014 * @addr: Virtual Address of the allocation. Must be inside the VMA.
2016 * This function allocates a page from the kernel page pool and applies
2017 * a NUMA policy associated with the VMA or the current process.
2018 * When VMA is not NULL caller must hold down_read on the mmap_sem of the
2019 * mm_struct of the VMA to prevent it from going away. Should be used for
2020 * all allocations for pages that will be mapped into
2021 * user space. Returns NULL when no page can be allocated.
2023 * Should be called with the mm_sem of the vma hold.
2026 alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
2027 unsigned long addr, int node)
2029 struct mempolicy *pol;
2031 unsigned int cpuset_mems_cookie;
2034 pol = get_vma_policy(current, vma, addr);
2035 cpuset_mems_cookie = read_mems_allowed_begin();
2037 if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
2040 nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
2042 page = alloc_page_interleave(gfp, order, nid);
2043 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
2048 page = __alloc_pages_nodemask(gfp, order,
2049 policy_zonelist(gfp, pol, node),
2050 policy_nodemask(gfp, pol));
2051 if (unlikely(mpol_needs_cond_ref(pol)))
2053 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
2059 * alloc_pages_current - Allocate pages.
2062 * %GFP_USER user allocation,
2063 * %GFP_KERNEL kernel allocation,
2064 * %GFP_HIGHMEM highmem allocation,
2065 * %GFP_FS don't call back into a file system.
2066 * %GFP_ATOMIC don't sleep.
2067 * @order: Power of two of allocation size in pages. 0 is a single page.
2069 * Allocate a page from the kernel page pool. When not in
2070 * interrupt context and apply the current process NUMA policy.
2071 * Returns NULL when no page can be allocated.
2073 * Don't call cpuset_update_task_memory_state() unless
2074 * 1) it's ok to take cpuset_sem (can WAIT), and
2075 * 2) allocating for current task (not interrupt).
2077 struct page *alloc_pages_current(gfp_t gfp, unsigned order)
2079 struct mempolicy *pol = get_task_policy(current);
2081 unsigned int cpuset_mems_cookie;
2083 if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
2084 pol = &default_policy;
2087 cpuset_mems_cookie = read_mems_allowed_begin();
2090 * No reference counting needed for current->mempolicy
2091 * nor system default_policy
2093 if (pol->mode == MPOL_INTERLEAVE)
2094 page = alloc_page_interleave(gfp, order, interleave_nodes(pol));
2096 page = __alloc_pages_nodemask(gfp, order,
2097 policy_zonelist(gfp, pol, numa_node_id()),
2098 policy_nodemask(gfp, pol));
2100 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
2105 EXPORT_SYMBOL(alloc_pages_current);
2107 int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
2109 struct mempolicy *pol = mpol_dup(vma_policy(src));
2112 return PTR_ERR(pol);
2113 dst->vm_policy = pol;
2118 * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
2119 * rebinds the mempolicy its copying by calling mpol_rebind_policy()
2120 * with the mems_allowed returned by cpuset_mems_allowed(). This
2121 * keeps mempolicies cpuset relative after its cpuset moves. See
2122 * further kernel/cpuset.c update_nodemask().
2124 * current's mempolicy may be rebinded by the other task(the task that changes
2125 * cpuset's mems), so we needn't do rebind work for current task.
2128 /* Slow path of a mempolicy duplicate */
2129 struct mempolicy *__mpol_dup(struct mempolicy *old)
2131 struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2134 return ERR_PTR(-ENOMEM);
2136 /* task's mempolicy is protected by alloc_lock */
2137 if (old == current->mempolicy) {
2140 task_unlock(current);
2145 if (current_cpuset_is_being_rebound()) {
2146 nodemask_t mems = cpuset_mems_allowed(current);
2147 if (new->flags & MPOL_F_REBINDING)
2148 mpol_rebind_policy(new, &mems, MPOL_REBIND_STEP2);
2150 mpol_rebind_policy(new, &mems, MPOL_REBIND_ONCE);
2153 atomic_set(&new->refcnt, 1);
2157 /* Slow path of a mempolicy comparison */
2158 bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
2162 if (a->mode != b->mode)
2164 if (a->flags != b->flags)
2166 if (mpol_store_user_nodemask(a))
2167 if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
2173 case MPOL_INTERLEAVE:
2174 return !!nodes_equal(a->v.nodes, b->v.nodes);
2175 case MPOL_PREFERRED:
2176 return a->v.preferred_node == b->v.preferred_node;
2184 * Shared memory backing store policy support.
2186 * Remember policies even when nobody has shared memory mapped.
2187 * The policies are kept in Red-Black tree linked from the inode.
2188 * They are protected by the sp->lock spinlock, which should be held
2189 * for any accesses to the tree.
2192 /* lookup first element intersecting start-end */
2193 /* Caller holds sp->lock */
2194 static struct sp_node *
2195 sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
2197 struct rb_node *n = sp->root.rb_node;
2200 struct sp_node *p = rb_entry(n, struct sp_node, nd);
2202 if (start >= p->end)
2204 else if (end <= p->start)
2212 struct sp_node *w = NULL;
2213 struct rb_node *prev = rb_prev(n);
2216 w = rb_entry(prev, struct sp_node, nd);
2217 if (w->end <= start)
2221 return rb_entry(n, struct sp_node, nd);
2224 /* Insert a new shared policy into the list. */
2225 /* Caller holds sp->lock */
2226 static void sp_insert(struct shared_policy *sp, struct sp_node *new)
2228 struct rb_node **p = &sp->root.rb_node;
2229 struct rb_node *parent = NULL;
2234 nd = rb_entry(parent, struct sp_node, nd);
2235 if (new->start < nd->start)
2237 else if (new->end > nd->end)
2238 p = &(*p)->rb_right;
2242 rb_link_node(&new->nd, parent, p);
2243 rb_insert_color(&new->nd, &sp->root);
2244 pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
2245 new->policy ? new->policy->mode : 0);
2248 /* Find shared policy intersecting idx */
2250 mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
2252 struct mempolicy *pol = NULL;
2255 if (!sp->root.rb_node)
2257 spin_lock(&sp->lock);
2258 sn = sp_lookup(sp, idx, idx+1);
2260 mpol_get(sn->policy);
2263 spin_unlock(&sp->lock);
2267 static void sp_free(struct sp_node *n)
2269 mpol_put(n->policy);
2270 kmem_cache_free(sn_cache, n);
2274 * mpol_misplaced - check whether current page node is valid in policy
2276 * @page: page to be checked
2277 * @vma: vm area where page mapped
2278 * @addr: virtual address where page mapped
2280 * Lookup current policy node id for vma,addr and "compare to" page's
2284 * -1 - not misplaced, page is in the right node
2285 * node - node id where the page should be
2287 * Policy determination "mimics" alloc_page_vma().
2288 * Called from fault path where we know the vma and faulting address.
2290 int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long addr)
2292 struct mempolicy *pol;
2294 int curnid = page_to_nid(page);
2295 unsigned long pgoff;
2296 int thiscpu = raw_smp_processor_id();
2297 int thisnid = cpu_to_node(thiscpu);
2303 pol = get_vma_policy(current, vma, addr);
2304 if (!(pol->flags & MPOL_F_MOF))
2307 switch (pol->mode) {
2308 case MPOL_INTERLEAVE:
2309 BUG_ON(addr >= vma->vm_end);
2310 BUG_ON(addr < vma->vm_start);
2312 pgoff = vma->vm_pgoff;
2313 pgoff += (addr - vma->vm_start) >> PAGE_SHIFT;
2314 polnid = offset_il_node(pol, vma, pgoff);
2317 case MPOL_PREFERRED:
2318 if (pol->flags & MPOL_F_LOCAL)
2319 polnid = numa_node_id();
2321 polnid = pol->v.preferred_node;
2326 * allows binding to multiple nodes.
2327 * use current page if in policy nodemask,
2328 * else select nearest allowed node, if any.
2329 * If no allowed nodes, use current [!misplaced].
2331 if (node_isset(curnid, pol->v.nodes))
2333 (void)first_zones_zonelist(
2334 node_zonelist(numa_node_id(), GFP_HIGHUSER),
2335 gfp_zone(GFP_HIGHUSER),
2336 &pol->v.nodes, &zone);
2337 polnid = zone->node;
2344 /* Migrate the page towards the node whose CPU is referencing it */
2345 if (pol->flags & MPOL_F_MORON) {
2348 if (!should_numa_migrate_memory(current, page, curnid, thiscpu))
2352 if (curnid != polnid)
2360 static void sp_delete(struct shared_policy *sp, struct sp_node *n)
2362 pr_debug("deleting %lx-l%lx\n", n->start, n->end);
2363 rb_erase(&n->nd, &sp->root);
2367 static void sp_node_init(struct sp_node *node, unsigned long start,
2368 unsigned long end, struct mempolicy *pol)
2370 node->start = start;
2375 static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
2376 struct mempolicy *pol)
2379 struct mempolicy *newpol;
2381 n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
2385 newpol = mpol_dup(pol);
2386 if (IS_ERR(newpol)) {
2387 kmem_cache_free(sn_cache, n);
2390 newpol->flags |= MPOL_F_SHARED;
2391 sp_node_init(n, start, end, newpol);
2396 /* Replace a policy range. */
2397 static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
2398 unsigned long end, struct sp_node *new)
2401 struct sp_node *n_new = NULL;
2402 struct mempolicy *mpol_new = NULL;
2406 spin_lock(&sp->lock);
2407 n = sp_lookup(sp, start, end);
2408 /* Take care of old policies in the same range. */
2409 while (n && n->start < end) {
2410 struct rb_node *next = rb_next(&n->nd);
2411 if (n->start >= start) {
2417 /* Old policy spanning whole new range. */
2422 *mpol_new = *n->policy;
2423 atomic_set(&mpol_new->refcnt, 1);
2424 sp_node_init(n_new, end, n->end, mpol_new);
2426 sp_insert(sp, n_new);
2435 n = rb_entry(next, struct sp_node, nd);
2439 spin_unlock(&sp->lock);
2446 kmem_cache_free(sn_cache, n_new);
2451 spin_unlock(&sp->lock);
2453 n_new = kmem_cache_alloc(sn_cache, GFP_KERNEL);
2456 mpol_new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2463 * mpol_shared_policy_init - initialize shared policy for inode
2464 * @sp: pointer to inode shared policy
2465 * @mpol: struct mempolicy to install
2467 * Install non-NULL @mpol in inode's shared policy rb-tree.
2468 * On entry, the current task has a reference on a non-NULL @mpol.
2469 * This must be released on exit.
2470 * This is called at get_inode() calls and we can use GFP_KERNEL.
2472 void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
2476 sp->root = RB_ROOT; /* empty tree == default mempolicy */
2477 spin_lock_init(&sp->lock);
2480 struct vm_area_struct pvma;
2481 struct mempolicy *new;
2482 NODEMASK_SCRATCH(scratch);
2486 /* contextualize the tmpfs mount point mempolicy */
2487 new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
2489 goto free_scratch; /* no valid nodemask intersection */
2492 ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch);
2493 task_unlock(current);
2497 /* Create pseudo-vma that contains just the policy */
2498 memset(&pvma, 0, sizeof(struct vm_area_struct));
2499 pvma.vm_end = TASK_SIZE; /* policy covers entire file */
2500 mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
2503 mpol_put(new); /* drop initial ref */
2505 NODEMASK_SCRATCH_FREE(scratch);
2507 mpol_put(mpol); /* drop our incoming ref on sb mpol */
2511 int mpol_set_shared_policy(struct shared_policy *info,
2512 struct vm_area_struct *vma, struct mempolicy *npol)
2515 struct sp_node *new = NULL;
2516 unsigned long sz = vma_pages(vma);
2518 pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
2520 sz, npol ? npol->mode : -1,
2521 npol ? npol->flags : -1,
2522 npol ? nodes_addr(npol->v.nodes)[0] : NUMA_NO_NODE);
2525 new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
2529 err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
2535 /* Free a backing policy store on inode delete. */
2536 void mpol_free_shared_policy(struct shared_policy *p)
2539 struct rb_node *next;
2541 if (!p->root.rb_node)
2543 spin_lock(&p->lock);
2544 next = rb_first(&p->root);
2546 n = rb_entry(next, struct sp_node, nd);
2547 next = rb_next(&n->nd);
2550 spin_unlock(&p->lock);
2553 #ifdef CONFIG_NUMA_BALANCING
2554 static int __initdata numabalancing_override;
2556 static void __init check_numabalancing_enable(void)
2558 bool numabalancing_default = false;
2560 if (IS_ENABLED(CONFIG_NUMA_BALANCING_DEFAULT_ENABLED))
2561 numabalancing_default = true;
2563 /* Parsed by setup_numabalancing. override == 1 enables, -1 disables */
2564 if (numabalancing_override)
2565 set_numabalancing_state(numabalancing_override == 1);
2567 if (nr_node_ids > 1 && !numabalancing_override) {
2568 pr_info("%s automatic NUMA balancing. "
2569 "Configure with numa_balancing= or the "
2570 "kernel.numa_balancing sysctl",
2571 numabalancing_default ? "Enabling" : "Disabling");
2572 set_numabalancing_state(numabalancing_default);
2576 static int __init setup_numabalancing(char *str)
2582 if (!strcmp(str, "enable")) {
2583 numabalancing_override = 1;
2585 } else if (!strcmp(str, "disable")) {
2586 numabalancing_override = -1;
2591 pr_warn("Unable to parse numa_balancing=\n");
2595 __setup("numa_balancing=", setup_numabalancing);
2597 static inline void __init check_numabalancing_enable(void)
2600 #endif /* CONFIG_NUMA_BALANCING */
2602 /* assumes fs == KERNEL_DS */
2603 void __init numa_policy_init(void)
2605 nodemask_t interleave_nodes;
2606 unsigned long largest = 0;
2607 int nid, prefer = 0;
2609 policy_cache = kmem_cache_create("numa_policy",
2610 sizeof(struct mempolicy),
2611 0, SLAB_PANIC, NULL);
2613 sn_cache = kmem_cache_create("shared_policy_node",
2614 sizeof(struct sp_node),
2615 0, SLAB_PANIC, NULL);
2617 for_each_node(nid) {
2618 preferred_node_policy[nid] = (struct mempolicy) {
2619 .refcnt = ATOMIC_INIT(1),
2620 .mode = MPOL_PREFERRED,
2621 .flags = MPOL_F_MOF | MPOL_F_MORON,
2622 .v = { .preferred_node = nid, },
2627 * Set interleaving policy for system init. Interleaving is only
2628 * enabled across suitably sized nodes (default is >= 16MB), or
2629 * fall back to the largest node if they're all smaller.
2631 nodes_clear(interleave_nodes);
2632 for_each_node_state(nid, N_MEMORY) {
2633 unsigned long total_pages = node_present_pages(nid);
2635 /* Preserve the largest node */
2636 if (largest < total_pages) {
2637 largest = total_pages;
2641 /* Interleave this node? */
2642 if ((total_pages << PAGE_SHIFT) >= (16 << 20))
2643 node_set(nid, interleave_nodes);
2646 /* All too small, use the largest */
2647 if (unlikely(nodes_empty(interleave_nodes)))
2648 node_set(prefer, interleave_nodes);
2650 if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
2651 pr_err("%s: interleaving failed\n", __func__);
2653 check_numabalancing_enable();
2656 /* Reset policy of current process to default */
2657 void numa_default_policy(void)
2659 do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
2663 * Parse and format mempolicy from/to strings
2667 * "local" is implemented internally by MPOL_PREFERRED with MPOL_F_LOCAL flag.
2669 static const char * const policy_modes[] =
2671 [MPOL_DEFAULT] = "default",
2672 [MPOL_PREFERRED] = "prefer",
2673 [MPOL_BIND] = "bind",
2674 [MPOL_INTERLEAVE] = "interleave",
2675 [MPOL_LOCAL] = "local",
2681 * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option.
2682 * @str: string containing mempolicy to parse
2683 * @mpol: pointer to struct mempolicy pointer, returned on success.
2686 * <mode>[=<flags>][:<nodelist>]
2688 * On success, returns 0, else 1
2690 int mpol_parse_str(char *str, struct mempolicy **mpol)
2692 struct mempolicy *new = NULL;
2693 unsigned short mode;
2694 unsigned short mode_flags;
2696 char *nodelist = strchr(str, ':');
2697 char *flags = strchr(str, '=');
2701 /* NUL-terminate mode or flags string */
2703 if (nodelist_parse(nodelist, nodes))
2705 if (!nodes_subset(nodes, node_states[N_MEMORY]))
2711 *flags++ = '\0'; /* terminate mode string */
2713 for (mode = 0; mode < MPOL_MAX; mode++) {
2714 if (!strcmp(str, policy_modes[mode])) {
2718 if (mode >= MPOL_MAX)
2722 case MPOL_PREFERRED:
2724 * Insist on a nodelist of one node only
2727 char *rest = nodelist;
2728 while (isdigit(*rest))
2734 case MPOL_INTERLEAVE:
2736 * Default to online nodes with memory if no nodelist
2739 nodes = node_states[N_MEMORY];
2743 * Don't allow a nodelist; mpol_new() checks flags
2747 mode = MPOL_PREFERRED;
2751 * Insist on a empty nodelist
2758 * Insist on a nodelist
2767 * Currently, we only support two mutually exclusive
2770 if (!strcmp(flags, "static"))
2771 mode_flags |= MPOL_F_STATIC_NODES;
2772 else if (!strcmp(flags, "relative"))
2773 mode_flags |= MPOL_F_RELATIVE_NODES;
2778 new = mpol_new(mode, mode_flags, &nodes);
2783 * Save nodes for mpol_to_str() to show the tmpfs mount options
2784 * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo.
2786 if (mode != MPOL_PREFERRED)
2787 new->v.nodes = nodes;
2789 new->v.preferred_node = first_node(nodes);
2791 new->flags |= MPOL_F_LOCAL;
2794 * Save nodes for contextualization: this will be used to "clone"
2795 * the mempolicy in a specific context [cpuset] at a later time.
2797 new->w.user_nodemask = nodes;
2802 /* Restore string for error message */
2811 #endif /* CONFIG_TMPFS */
2814 * mpol_to_str - format a mempolicy structure for printing
2815 * @buffer: to contain formatted mempolicy string
2816 * @maxlen: length of @buffer
2817 * @pol: pointer to mempolicy to be formatted
2819 * Convert @pol into a string. If @buffer is too short, truncate the string.
2820 * Recommend a @maxlen of at least 32 for the longest mode, "interleave", the
2821 * longest flag, "relative", and to display at least a few node ids.
2823 void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
2826 nodemask_t nodes = NODE_MASK_NONE;
2827 unsigned short mode = MPOL_DEFAULT;
2828 unsigned short flags = 0;
2830 if (pol && pol != &default_policy && !(pol->flags & MPOL_F_MORON)) {
2838 case MPOL_PREFERRED:
2839 if (flags & MPOL_F_LOCAL)
2842 node_set(pol->v.preferred_node, nodes);
2845 case MPOL_INTERLEAVE:
2846 nodes = pol->v.nodes;
2850 snprintf(p, maxlen, "unknown");
2854 p += snprintf(p, maxlen, "%s", policy_modes[mode]);
2856 if (flags & MPOL_MODE_FLAGS) {
2857 p += snprintf(p, buffer + maxlen - p, "=");
2860 * Currently, the only defined flags are mutually exclusive
2862 if (flags & MPOL_F_STATIC_NODES)
2863 p += snprintf(p, buffer + maxlen - p, "static");
2864 else if (flags & MPOL_F_RELATIVE_NODES)
2865 p += snprintf(p, buffer + maxlen - p, "relative");
2868 if (!nodes_empty(nodes)) {
2869 p += snprintf(p, buffer + maxlen - p, ":");
2870 p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);