xref: /openbmc/linux/mm/mempolicy.c (revision 71fe804b6d56d6a7aed680e096901434cef6a2c3)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Simple NUMA memory policy for the Linux kernel.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright 2003,2004 Andi Kleen, SuSE Labs.
58bccd85fSChristoph Lameter  * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
61da177e4SLinus Torvalds  * Subject to the GNU Public License, version 2.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * NUMA policy allows the user to give hints in which node(s) memory should
91da177e4SLinus Torvalds  * be allocated.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Support four policies per VMA and per process:
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * The VMA policy has priority over the process policy for a page fault.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * interleave     Allocate memory interleaved over a set of nodes,
161da177e4SLinus Torvalds  *                with normal fallback if it fails.
171da177e4SLinus Torvalds  *                For VMA based allocations this interleaves based on the
181da177e4SLinus Torvalds  *                offset into the backing object or offset into the mapping
191da177e4SLinus Torvalds  *                for anonymous memory. For process policy an process counter
201da177e4SLinus Torvalds  *                is used.
218bccd85fSChristoph Lameter  *
221da177e4SLinus Torvalds  * bind           Only allocate memory on a specific set of nodes,
231da177e4SLinus Torvalds  *                no fallback.
248bccd85fSChristoph Lameter  *                FIXME: memory is allocated starting with the first node
258bccd85fSChristoph Lameter  *                to the last. It would be better if bind would truly restrict
268bccd85fSChristoph Lameter  *                the allocation to memory nodes instead
278bccd85fSChristoph Lameter  *
281da177e4SLinus Torvalds  * preferred       Try a specific node first before normal fallback.
291da177e4SLinus Torvalds  *                As a special case node -1 here means do the allocation
301da177e4SLinus Torvalds  *                on the local CPU. This is normally identical to default,
311da177e4SLinus Torvalds  *                but useful to set in a VMA when you have a non default
321da177e4SLinus Torvalds  *                process policy.
338bccd85fSChristoph Lameter  *
341da177e4SLinus Torvalds  * default        Allocate on the local node first, or when on a VMA
351da177e4SLinus Torvalds  *                use the process policy. This is what Linux always did
361da177e4SLinus Torvalds  *		  in a NUMA aware kernel and still does by, ahem, default.
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * The process policy is applied for most non interrupt memory allocations
391da177e4SLinus Torvalds  * in that process' context. Interrupts ignore the policies and always
401da177e4SLinus Torvalds  * try to allocate on the local CPU. The VMA policy is only applied for memory
411da177e4SLinus Torvalds  * allocations for a VMA in the VM.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * Currently there are a few corner cases in swapping where the policy
441da177e4SLinus Torvalds  * is not applied, but the majority should be handled. When process policy
451da177e4SLinus Torvalds  * is used it is not remembered over swap outs/swap ins.
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Only the highest zone in the zone hierarchy gets policied. Allocations
481da177e4SLinus Torvalds  * requesting a lower zone just use default policy. This implies that
491da177e4SLinus Torvalds  * on systems with highmem kernel lowmem allocation don't get policied.
501da177e4SLinus Torvalds  * Same with GFP_DMA allocations.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
531da177e4SLinus Torvalds  * all users and remembered even when nobody has memory mapped.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* Notebook:
571da177e4SLinus Torvalds    fix mmap readahead to honour policy and enable policy for any page cache
581da177e4SLinus Torvalds    object
591da177e4SLinus Torvalds    statistics for bigpages
601da177e4SLinus Torvalds    global policy for page cache? currently it uses process policy. Requires
611da177e4SLinus Torvalds    first item above.
621da177e4SLinus Torvalds    handle mremap for shared memory (currently ignored for the policy)
631da177e4SLinus Torvalds    grows down?
641da177e4SLinus Torvalds    make bind policy root only? It can trigger oom much faster and the
651da177e4SLinus Torvalds    kernel is not always grateful with that.
661da177e4SLinus Torvalds */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <linux/mempolicy.h>
691da177e4SLinus Torvalds #include <linux/mm.h>
701da177e4SLinus Torvalds #include <linux/highmem.h>
711da177e4SLinus Torvalds #include <linux/hugetlb.h>
721da177e4SLinus Torvalds #include <linux/kernel.h>
731da177e4SLinus Torvalds #include <linux/sched.h>
741da177e4SLinus Torvalds #include <linux/nodemask.h>
751da177e4SLinus Torvalds #include <linux/cpuset.h>
761da177e4SLinus Torvalds #include <linux/gfp.h>
771da177e4SLinus Torvalds #include <linux/slab.h>
781da177e4SLinus Torvalds #include <linux/string.h>
791da177e4SLinus Torvalds #include <linux/module.h>
80b488893aSPavel Emelyanov #include <linux/nsproxy.h>
811da177e4SLinus Torvalds #include <linux/interrupt.h>
821da177e4SLinus Torvalds #include <linux/init.h>
831da177e4SLinus Torvalds #include <linux/compat.h>
84dc9aa5b9SChristoph Lameter #include <linux/swap.h>
851a75a6c8SChristoph Lameter #include <linux/seq_file.h>
861a75a6c8SChristoph Lameter #include <linux/proc_fs.h>
87b20a3503SChristoph Lameter #include <linux/migrate.h>
8895a402c3SChristoph Lameter #include <linux/rmap.h>
8986c3a764SDavid Quigley #include <linux/security.h>
90dbcb0f19SAdrian Bunk #include <linux/syscalls.h>
91095f1fc4SLee Schermerhorn #include <linux/ctype.h>
92dc9aa5b9SChristoph Lameter 
931da177e4SLinus Torvalds #include <asm/tlbflush.h>
941da177e4SLinus Torvalds #include <asm/uaccess.h>
951da177e4SLinus Torvalds 
9638e35860SChristoph Lameter /* Internal flags */
97dc9aa5b9SChristoph Lameter #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)	/* Skip checks for continuous vmas */
9838e35860SChristoph Lameter #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)		/* Invert check for nodemask */
991a75a6c8SChristoph Lameter #define MPOL_MF_STATS (MPOL_MF_INTERNAL << 2)		/* Gather statistics */
100dc9aa5b9SChristoph Lameter 
101fcc234f8SPekka Enberg static struct kmem_cache *policy_cache;
102fcc234f8SPekka Enberg static struct kmem_cache *sn_cache;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /* Highest zone. An specific allocation for a zone below that is not
1051da177e4SLinus Torvalds    policied. */
1066267276fSChristoph Lameter enum zone_type policy_zone = 0;
1071da177e4SLinus Torvalds 
108bea904d5SLee Schermerhorn /*
109bea904d5SLee Schermerhorn  * run-time system-wide default policy => local allocation
110bea904d5SLee Schermerhorn  */
111d42c6997SAndi Kleen struct mempolicy default_policy = {
1121da177e4SLinus Torvalds 	.refcnt = ATOMIC_INIT(1), /* never free it */
113bea904d5SLee Schermerhorn 	.mode = MPOL_PREFERRED,
114fc36b8d3SLee Schermerhorn 	.flags = MPOL_F_LOCAL,
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
11737012946SDavid Rientjes static const struct mempolicy_operations {
11837012946SDavid Rientjes 	int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
11937012946SDavid Rientjes 	void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
12037012946SDavid Rientjes } mpol_ops[MPOL_MAX];
12137012946SDavid Rientjes 
12219770b32SMel Gorman /* Check that the nodemask contains at least one populated zone */
12337012946SDavid Rientjes static int is_valid_nodemask(const nodemask_t *nodemask)
1241da177e4SLinus Torvalds {
12519770b32SMel Gorman 	int nd, k;
1261da177e4SLinus Torvalds 
12719770b32SMel Gorman 	/* Check that there is something useful in this mask */
12819770b32SMel Gorman 	k = policy_zone;
12919770b32SMel Gorman 
13019770b32SMel Gorman 	for_each_node_mask(nd, *nodemask) {
13119770b32SMel Gorman 		struct zone *z;
13219770b32SMel Gorman 
13319770b32SMel Gorman 		for (k = 0; k <= policy_zone; k++) {
13419770b32SMel Gorman 			z = &NODE_DATA(nd)->node_zones[k];
135dd942ae3SAndi Kleen 			if (z->present_pages > 0)
13619770b32SMel Gorman 				return 1;
137dd942ae3SAndi Kleen 		}
138dd942ae3SAndi Kleen 	}
13919770b32SMel Gorman 
14019770b32SMel Gorman 	return 0;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
143f5b087b5SDavid Rientjes static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
144f5b087b5SDavid Rientjes {
1454c50bc01SDavid Rientjes 	return pol->flags & (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES);
1464c50bc01SDavid Rientjes }
1474c50bc01SDavid Rientjes 
1484c50bc01SDavid Rientjes static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
1494c50bc01SDavid Rientjes 				   const nodemask_t *rel)
1504c50bc01SDavid Rientjes {
1514c50bc01SDavid Rientjes 	nodemask_t tmp;
1524c50bc01SDavid Rientjes 	nodes_fold(tmp, *orig, nodes_weight(*rel));
1534c50bc01SDavid Rientjes 	nodes_onto(*ret, tmp, *rel);
154f5b087b5SDavid Rientjes }
155f5b087b5SDavid Rientjes 
15637012946SDavid Rientjes static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
15737012946SDavid Rientjes {
15837012946SDavid Rientjes 	if (nodes_empty(*nodes))
15937012946SDavid Rientjes 		return -EINVAL;
16037012946SDavid Rientjes 	pol->v.nodes = *nodes;
16137012946SDavid Rientjes 	return 0;
16237012946SDavid Rientjes }
16337012946SDavid Rientjes 
16437012946SDavid Rientjes static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
16537012946SDavid Rientjes {
16637012946SDavid Rientjes 	if (!nodes)
167fc36b8d3SLee Schermerhorn 		pol->flags |= MPOL_F_LOCAL;	/* local allocation */
16837012946SDavid Rientjes 	else if (nodes_empty(*nodes))
16937012946SDavid Rientjes 		return -EINVAL;			/*  no allowed nodes */
17037012946SDavid Rientjes 	else
17137012946SDavid Rientjes 		pol->v.preferred_node = first_node(*nodes);
17237012946SDavid Rientjes 	return 0;
17337012946SDavid Rientjes }
17437012946SDavid Rientjes 
17537012946SDavid Rientjes static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
17637012946SDavid Rientjes {
17737012946SDavid Rientjes 	if (!is_valid_nodemask(nodes))
17837012946SDavid Rientjes 		return -EINVAL;
17937012946SDavid Rientjes 	pol->v.nodes = *nodes;
18037012946SDavid Rientjes 	return 0;
18137012946SDavid Rientjes }
18237012946SDavid Rientjes 
1831da177e4SLinus Torvalds /* Create a new policy */
184028fec41SDavid Rientjes static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
185028fec41SDavid Rientjes 				  nodemask_t *nodes)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct mempolicy *policy;
188f5b087b5SDavid Rientjes 	nodemask_t cpuset_context_nmask;
18937012946SDavid Rientjes 	int ret;
1901da177e4SLinus Torvalds 
191028fec41SDavid Rientjes 	pr_debug("setting mode %d flags %d nodes[0] %lx\n",
192028fec41SDavid Rientjes 		 mode, flags, nodes ? nodes_addr(*nodes)[0] : -1);
193140d5a49SPaul Mundt 
1943e1f0645SDavid Rientjes 	if (mode == MPOL_DEFAULT) {
1953e1f0645SDavid Rientjes 		if (nodes && !nodes_empty(*nodes))
19637012946SDavid Rientjes 			return ERR_PTR(-EINVAL);
197bea904d5SLee Schermerhorn 		return NULL;	/* simply delete any existing policy */
19837012946SDavid Rientjes 	}
1993e1f0645SDavid Rientjes 	VM_BUG_ON(!nodes);
2003e1f0645SDavid Rientjes 
2013e1f0645SDavid Rientjes 	/*
2023e1f0645SDavid Rientjes 	 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
2033e1f0645SDavid Rientjes 	 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
2043e1f0645SDavid Rientjes 	 * All other modes require a valid pointer to a non-empty nodemask.
2053e1f0645SDavid Rientjes 	 */
2063e1f0645SDavid Rientjes 	if (mode == MPOL_PREFERRED) {
2073e1f0645SDavid Rientjes 		if (nodes_empty(*nodes)) {
2083e1f0645SDavid Rientjes 			if (((flags & MPOL_F_STATIC_NODES) ||
2093e1f0645SDavid Rientjes 			     (flags & MPOL_F_RELATIVE_NODES)))
2103e1f0645SDavid Rientjes 				return ERR_PTR(-EINVAL);
2113e1f0645SDavid Rientjes 			nodes = NULL;	/* flag local alloc */
2123e1f0645SDavid Rientjes 		}
2133e1f0645SDavid Rientjes 	} else if (nodes_empty(*nodes))
2143e1f0645SDavid Rientjes 		return ERR_PTR(-EINVAL);
2151da177e4SLinus Torvalds 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2161da177e4SLinus Torvalds 	if (!policy)
2171da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2181da177e4SLinus Torvalds 	atomic_set(&policy->refcnt, 1);
21945c4745aSLee Schermerhorn 	policy->mode = mode;
22037012946SDavid Rientjes 	policy->flags = flags;
2213e1f0645SDavid Rientjes 
2223e1f0645SDavid Rientjes 	if (nodes) {
2233e1f0645SDavid Rientjes 		/*
2243e1f0645SDavid Rientjes 		 * cpuset related setup doesn't apply to local allocation
2253e1f0645SDavid Rientjes 		 */
226f5b087b5SDavid Rientjes 		cpuset_update_task_memory_state();
2274c50bc01SDavid Rientjes 		if (flags & MPOL_F_RELATIVE_NODES)
2284c50bc01SDavid Rientjes 			mpol_relative_nodemask(&cpuset_context_nmask, nodes,
2294c50bc01SDavid Rientjes 					       &cpuset_current_mems_allowed);
2304c50bc01SDavid Rientjes 		else
2314c50bc01SDavid Rientjes 			nodes_and(cpuset_context_nmask, *nodes,
2324c50bc01SDavid Rientjes 				  cpuset_current_mems_allowed);
233f5b087b5SDavid Rientjes 		if (mpol_store_user_nodemask(policy))
234f5b087b5SDavid Rientjes 			policy->w.user_nodemask = *nodes;
235f5b087b5SDavid Rientjes 		else
23637012946SDavid Rientjes 			policy->w.cpuset_mems_allowed =
23737012946SDavid Rientjes 						cpuset_mems_allowed(current);
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds 
24037012946SDavid Rientjes 	ret = mpol_ops[mode].create(policy,
2413e1f0645SDavid Rientjes 				nodes ? &cpuset_context_nmask : NULL);
24237012946SDavid Rientjes 	if (ret < 0) {
24337012946SDavid Rientjes 		kmem_cache_free(policy_cache, policy);
24437012946SDavid Rientjes 		return ERR_PTR(ret);
24537012946SDavid Rientjes 	}
24637012946SDavid Rientjes 	return policy;
24737012946SDavid Rientjes }
24837012946SDavid Rientjes 
24952cd3b07SLee Schermerhorn /* Slow path of a mpol destructor. */
25052cd3b07SLee Schermerhorn void __mpol_put(struct mempolicy *p)
25152cd3b07SLee Schermerhorn {
25252cd3b07SLee Schermerhorn 	if (!atomic_dec_and_test(&p->refcnt))
25352cd3b07SLee Schermerhorn 		return;
25452cd3b07SLee Schermerhorn 	kmem_cache_free(policy_cache, p);
25552cd3b07SLee Schermerhorn }
25652cd3b07SLee Schermerhorn 
25737012946SDavid Rientjes static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes)
25837012946SDavid Rientjes {
25937012946SDavid Rientjes }
26037012946SDavid Rientjes 
26137012946SDavid Rientjes static void mpol_rebind_nodemask(struct mempolicy *pol,
26237012946SDavid Rientjes 				 const nodemask_t *nodes)
2631d0d2680SDavid Rientjes {
2641d0d2680SDavid Rientjes 	nodemask_t tmp;
2651d0d2680SDavid Rientjes 
26637012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES)
26737012946SDavid Rientjes 		nodes_and(tmp, pol->w.user_nodemask, *nodes);
26837012946SDavid Rientjes 	else if (pol->flags & MPOL_F_RELATIVE_NODES)
26937012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
2701d0d2680SDavid Rientjes 	else {
27137012946SDavid Rientjes 		nodes_remap(tmp, pol->v.nodes, pol->w.cpuset_mems_allowed,
27237012946SDavid Rientjes 			    *nodes);
27337012946SDavid Rientjes 		pol->w.cpuset_mems_allowed = *nodes;
2741d0d2680SDavid Rientjes 	}
27537012946SDavid Rientjes 
2761d0d2680SDavid Rientjes 	pol->v.nodes = tmp;
2771d0d2680SDavid Rientjes 	if (!node_isset(current->il_next, tmp)) {
2781d0d2680SDavid Rientjes 		current->il_next = next_node(current->il_next, tmp);
2791d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
2801d0d2680SDavid Rientjes 			current->il_next = first_node(tmp);
2811d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
2821d0d2680SDavid Rientjes 			current->il_next = numa_node_id();
2831d0d2680SDavid Rientjes 	}
28437012946SDavid Rientjes }
28537012946SDavid Rientjes 
28637012946SDavid Rientjes static void mpol_rebind_preferred(struct mempolicy *pol,
28737012946SDavid Rientjes 				  const nodemask_t *nodes)
28837012946SDavid Rientjes {
28937012946SDavid Rientjes 	nodemask_t tmp;
29037012946SDavid Rientjes 
29137012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES) {
2921d0d2680SDavid Rientjes 		int node = first_node(pol->w.user_nodemask);
2931d0d2680SDavid Rientjes 
294fc36b8d3SLee Schermerhorn 		if (node_isset(node, *nodes)) {
2951d0d2680SDavid Rientjes 			pol->v.preferred_node = node;
296fc36b8d3SLee Schermerhorn 			pol->flags &= ~MPOL_F_LOCAL;
297fc36b8d3SLee Schermerhorn 		} else
298fc36b8d3SLee Schermerhorn 			pol->flags |= MPOL_F_LOCAL;
29937012946SDavid Rientjes 	} else if (pol->flags & MPOL_F_RELATIVE_NODES) {
30037012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
3011d0d2680SDavid Rientjes 		pol->v.preferred_node = first_node(tmp);
302fc36b8d3SLee Schermerhorn 	} else if (!(pol->flags & MPOL_F_LOCAL)) {
3031d0d2680SDavid Rientjes 		pol->v.preferred_node = node_remap(pol->v.preferred_node,
30437012946SDavid Rientjes 						   pol->w.cpuset_mems_allowed,
30537012946SDavid Rientjes 						   *nodes);
30637012946SDavid Rientjes 		pol->w.cpuset_mems_allowed = *nodes;
3071d0d2680SDavid Rientjes 	}
3081d0d2680SDavid Rientjes }
30937012946SDavid Rientjes 
31037012946SDavid Rientjes /* Migrate a policy to a different set of nodes */
31137012946SDavid Rientjes static void mpol_rebind_policy(struct mempolicy *pol,
31237012946SDavid Rientjes 			       const nodemask_t *newmask)
31337012946SDavid Rientjes {
31437012946SDavid Rientjes 	if (!pol)
31537012946SDavid Rientjes 		return;
31637012946SDavid Rientjes 	if (!mpol_store_user_nodemask(pol) &&
31737012946SDavid Rientjes 	    nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
31837012946SDavid Rientjes 		return;
31945c4745aSLee Schermerhorn 	mpol_ops[pol->mode].rebind(pol, newmask);
3201d0d2680SDavid Rientjes }
3211d0d2680SDavid Rientjes 
3221d0d2680SDavid Rientjes /*
3231d0d2680SDavid Rientjes  * Wrapper for mpol_rebind_policy() that just requires task
3241d0d2680SDavid Rientjes  * pointer, and updates task mempolicy.
3251d0d2680SDavid Rientjes  */
3261d0d2680SDavid Rientjes 
3271d0d2680SDavid Rientjes void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new)
3281d0d2680SDavid Rientjes {
3291d0d2680SDavid Rientjes 	mpol_rebind_policy(tsk->mempolicy, new);
3301d0d2680SDavid Rientjes }
3311d0d2680SDavid Rientjes 
3321d0d2680SDavid Rientjes /*
3331d0d2680SDavid Rientjes  * Rebind each vma in mm to new nodemask.
3341d0d2680SDavid Rientjes  *
3351d0d2680SDavid Rientjes  * Call holding a reference to mm.  Takes mm->mmap_sem during call.
3361d0d2680SDavid Rientjes  */
3371d0d2680SDavid Rientjes 
3381d0d2680SDavid Rientjes void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
3391d0d2680SDavid Rientjes {
3401d0d2680SDavid Rientjes 	struct vm_area_struct *vma;
3411d0d2680SDavid Rientjes 
3421d0d2680SDavid Rientjes 	down_write(&mm->mmap_sem);
3431d0d2680SDavid Rientjes 	for (vma = mm->mmap; vma; vma = vma->vm_next)
3441d0d2680SDavid Rientjes 		mpol_rebind_policy(vma->vm_policy, new);
3451d0d2680SDavid Rientjes 	up_write(&mm->mmap_sem);
3461d0d2680SDavid Rientjes }
3471d0d2680SDavid Rientjes 
34837012946SDavid Rientjes static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
34937012946SDavid Rientjes 	[MPOL_DEFAULT] = {
35037012946SDavid Rientjes 		.rebind = mpol_rebind_default,
35137012946SDavid Rientjes 	},
35237012946SDavid Rientjes 	[MPOL_INTERLEAVE] = {
35337012946SDavid Rientjes 		.create = mpol_new_interleave,
35437012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
35537012946SDavid Rientjes 	},
35637012946SDavid Rientjes 	[MPOL_PREFERRED] = {
35737012946SDavid Rientjes 		.create = mpol_new_preferred,
35837012946SDavid Rientjes 		.rebind = mpol_rebind_preferred,
35937012946SDavid Rientjes 	},
36037012946SDavid Rientjes 	[MPOL_BIND] = {
36137012946SDavid Rientjes 		.create = mpol_new_bind,
36237012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
36337012946SDavid Rientjes 	},
36437012946SDavid Rientjes };
36537012946SDavid Rientjes 
366397874dfSChristoph Lameter static void gather_stats(struct page *, void *, int pte_dirty);
367fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
368fc301289SChristoph Lameter 				unsigned long flags);
3691a75a6c8SChristoph Lameter 
37038e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */
371b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
372dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
373dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
37438e35860SChristoph Lameter 		void *private)
3751da177e4SLinus Torvalds {
37691612e0dSHugh Dickins 	pte_t *orig_pte;
37791612e0dSHugh Dickins 	pte_t *pte;
378705e87c0SHugh Dickins 	spinlock_t *ptl;
379941150a3SHugh Dickins 
380705e87c0SHugh Dickins 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
38191612e0dSHugh Dickins 	do {
3826aab341eSLinus Torvalds 		struct page *page;
38325ba77c1SAndy Whitcroft 		int nid;
38491612e0dSHugh Dickins 
38591612e0dSHugh Dickins 		if (!pte_present(*pte))
38691612e0dSHugh Dickins 			continue;
3876aab341eSLinus Torvalds 		page = vm_normal_page(vma, addr, *pte);
3886aab341eSLinus Torvalds 		if (!page)
38991612e0dSHugh Dickins 			continue;
390053837fcSNick Piggin 		/*
391053837fcSNick Piggin 		 * The check for PageReserved here is important to avoid
392053837fcSNick Piggin 		 * handling zero pages and other pages that may have been
393053837fcSNick Piggin 		 * marked special by the system.
394053837fcSNick Piggin 		 *
395053837fcSNick Piggin 		 * If the PageReserved would not be checked here then f.e.
396053837fcSNick Piggin 		 * the location of the zero page could have an influence
397053837fcSNick Piggin 		 * on MPOL_MF_STRICT, zero pages would be counted for
398053837fcSNick Piggin 		 * the per node stats, and there would be useless attempts
399053837fcSNick Piggin 		 * to put zero pages on the migration list.
400053837fcSNick Piggin 		 */
401f4598c8bSChristoph Lameter 		if (PageReserved(page))
402f4598c8bSChristoph Lameter 			continue;
4036aab341eSLinus Torvalds 		nid = page_to_nid(page);
40438e35860SChristoph Lameter 		if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
40538e35860SChristoph Lameter 			continue;
40638e35860SChristoph Lameter 
4071a75a6c8SChristoph Lameter 		if (flags & MPOL_MF_STATS)
408397874dfSChristoph Lameter 			gather_stats(page, private, pte_dirty(*pte));
409053837fcSNick Piggin 		else if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
410fc301289SChristoph Lameter 			migrate_page_add(page, private, flags);
411dc9aa5b9SChristoph Lameter 		else
4121da177e4SLinus Torvalds 			break;
41391612e0dSHugh Dickins 	} while (pte++, addr += PAGE_SIZE, addr != end);
414705e87c0SHugh Dickins 	pte_unmap_unlock(orig_pte, ptl);
41591612e0dSHugh Dickins 	return addr != end;
41691612e0dSHugh Dickins }
41791612e0dSHugh Dickins 
418b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
419dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
420dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
42138e35860SChristoph Lameter 		void *private)
42291612e0dSHugh Dickins {
42391612e0dSHugh Dickins 	pmd_t *pmd;
42491612e0dSHugh Dickins 	unsigned long next;
42591612e0dSHugh Dickins 
42691612e0dSHugh Dickins 	pmd = pmd_offset(pud, addr);
42791612e0dSHugh Dickins 	do {
42891612e0dSHugh Dickins 		next = pmd_addr_end(addr, end);
42991612e0dSHugh Dickins 		if (pmd_none_or_clear_bad(pmd))
43091612e0dSHugh Dickins 			continue;
431dc9aa5b9SChristoph Lameter 		if (check_pte_range(vma, pmd, addr, next, nodes,
43238e35860SChristoph Lameter 				    flags, private))
43391612e0dSHugh Dickins 			return -EIO;
43491612e0dSHugh Dickins 	} while (pmd++, addr = next, addr != end);
43591612e0dSHugh Dickins 	return 0;
43691612e0dSHugh Dickins }
43791612e0dSHugh Dickins 
438b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
439dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
440dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
44138e35860SChristoph Lameter 		void *private)
44291612e0dSHugh Dickins {
44391612e0dSHugh Dickins 	pud_t *pud;
44491612e0dSHugh Dickins 	unsigned long next;
44591612e0dSHugh Dickins 
44691612e0dSHugh Dickins 	pud = pud_offset(pgd, addr);
44791612e0dSHugh Dickins 	do {
44891612e0dSHugh Dickins 		next = pud_addr_end(addr, end);
44991612e0dSHugh Dickins 		if (pud_none_or_clear_bad(pud))
45091612e0dSHugh Dickins 			continue;
451dc9aa5b9SChristoph Lameter 		if (check_pmd_range(vma, pud, addr, next, nodes,
45238e35860SChristoph Lameter 				    flags, private))
45391612e0dSHugh Dickins 			return -EIO;
45491612e0dSHugh Dickins 	} while (pud++, addr = next, addr != end);
45591612e0dSHugh Dickins 	return 0;
45691612e0dSHugh Dickins }
45791612e0dSHugh Dickins 
458b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma,
459dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
460dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
46138e35860SChristoph Lameter 		void *private)
46291612e0dSHugh Dickins {
46391612e0dSHugh Dickins 	pgd_t *pgd;
46491612e0dSHugh Dickins 	unsigned long next;
46591612e0dSHugh Dickins 
466b5810039SNick Piggin 	pgd = pgd_offset(vma->vm_mm, addr);
46791612e0dSHugh Dickins 	do {
46891612e0dSHugh Dickins 		next = pgd_addr_end(addr, end);
46991612e0dSHugh Dickins 		if (pgd_none_or_clear_bad(pgd))
47091612e0dSHugh Dickins 			continue;
471dc9aa5b9SChristoph Lameter 		if (check_pud_range(vma, pgd, addr, next, nodes,
47238e35860SChristoph Lameter 				    flags, private))
47391612e0dSHugh Dickins 			return -EIO;
47491612e0dSHugh Dickins 	} while (pgd++, addr = next, addr != end);
47591612e0dSHugh Dickins 	return 0;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478dc9aa5b9SChristoph Lameter /*
479dc9aa5b9SChristoph Lameter  * Check if all pages in a range are on a set of nodes.
480dc9aa5b9SChristoph Lameter  * If pagelist != NULL then isolate pages from the LRU and
481dc9aa5b9SChristoph Lameter  * put them on the pagelist.
482dc9aa5b9SChristoph Lameter  */
4831da177e4SLinus Torvalds static struct vm_area_struct *
4841da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
48538e35860SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags, void *private)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	int err;
4881da177e4SLinus Torvalds 	struct vm_area_struct *first, *vma, *prev;
4891da177e4SLinus Torvalds 
49090036ee5SChristoph Lameter 	if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
49190036ee5SChristoph Lameter 
492b20a3503SChristoph Lameter 		err = migrate_prep();
493b20a3503SChristoph Lameter 		if (err)
494b20a3503SChristoph Lameter 			return ERR_PTR(err);
49590036ee5SChristoph Lameter 	}
496053837fcSNick Piggin 
4971da177e4SLinus Torvalds 	first = find_vma(mm, start);
4981da177e4SLinus Torvalds 	if (!first)
4991da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
5001da177e4SLinus Torvalds 	prev = NULL;
5011da177e4SLinus Torvalds 	for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
502dc9aa5b9SChristoph Lameter 		if (!(flags & MPOL_MF_DISCONTIG_OK)) {
5031da177e4SLinus Torvalds 			if (!vma->vm_next && vma->vm_end < end)
5041da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
5051da177e4SLinus Torvalds 			if (prev && prev->vm_end < vma->vm_start)
5061da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
507dc9aa5b9SChristoph Lameter 		}
508dc9aa5b9SChristoph Lameter 		if (!is_vm_hugetlb_page(vma) &&
509dc9aa5b9SChristoph Lameter 		    ((flags & MPOL_MF_STRICT) ||
510dc9aa5b9SChristoph Lameter 		     ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
511dc9aa5b9SChristoph Lameter 				vma_migratable(vma)))) {
5125b952b3cSAndi Kleen 			unsigned long endvma = vma->vm_end;
513dc9aa5b9SChristoph Lameter 
5145b952b3cSAndi Kleen 			if (endvma > end)
5155b952b3cSAndi Kleen 				endvma = end;
5165b952b3cSAndi Kleen 			if (vma->vm_start > start)
5175b952b3cSAndi Kleen 				start = vma->vm_start;
518dc9aa5b9SChristoph Lameter 			err = check_pgd_range(vma, start, endvma, nodes,
51938e35860SChristoph Lameter 						flags, private);
5201da177e4SLinus Torvalds 			if (err) {
5211da177e4SLinus Torvalds 				first = ERR_PTR(err);
5221da177e4SLinus Torvalds 				break;
5231da177e4SLinus Torvalds 			}
5241da177e4SLinus Torvalds 		}
5251da177e4SLinus Torvalds 		prev = vma;
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 	return first;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /* Apply policy to a single VMA */
5311da177e4SLinus Torvalds static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	int err = 0;
5341da177e4SLinus Torvalds 	struct mempolicy *old = vma->vm_policy;
5351da177e4SLinus Torvalds 
536140d5a49SPaul Mundt 	pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
5371da177e4SLinus Torvalds 		 vma->vm_start, vma->vm_end, vma->vm_pgoff,
5381da177e4SLinus Torvalds 		 vma->vm_ops, vma->vm_file,
5391da177e4SLinus Torvalds 		 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->set_policy)
5421da177e4SLinus Torvalds 		err = vma->vm_ops->set_policy(vma, new);
5431da177e4SLinus Torvalds 	if (!err) {
5441da177e4SLinus Torvalds 		mpol_get(new);
5451da177e4SLinus Torvalds 		vma->vm_policy = new;
546f0be3d32SLee Schermerhorn 		mpol_put(old);
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 	return err;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */
5521da177e4SLinus Torvalds static int mbind_range(struct vm_area_struct *vma, unsigned long start,
5531da177e4SLinus Torvalds 		       unsigned long end, struct mempolicy *new)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct vm_area_struct *next;
5561da177e4SLinus Torvalds 	int err;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	err = 0;
5591da177e4SLinus Torvalds 	for (; vma && vma->vm_start < end; vma = next) {
5601da177e4SLinus Torvalds 		next = vma->vm_next;
5611da177e4SLinus Torvalds 		if (vma->vm_start < start)
5621da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, start, 1);
5631da177e4SLinus Torvalds 		if (!err && vma->vm_end > end)
5641da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, end, 0);
5651da177e4SLinus Torvalds 		if (!err)
5661da177e4SLinus Torvalds 			err = policy_vma(vma, new);
5671da177e4SLinus Torvalds 		if (err)
5681da177e4SLinus Torvalds 			break;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 	return err;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
573c61afb18SPaul Jackson /*
574c61afb18SPaul Jackson  * Update task->flags PF_MEMPOLICY bit: set iff non-default
575c61afb18SPaul Jackson  * mempolicy.  Allows more rapid checking of this (combined perhaps
576c61afb18SPaul Jackson  * with other PF_* flag bits) on memory allocation hot code paths.
577c61afb18SPaul Jackson  *
578c61afb18SPaul Jackson  * If called from outside this file, the task 'p' should -only- be
579c61afb18SPaul Jackson  * a newly forked child not yet visible on the task list, because
580c61afb18SPaul Jackson  * manipulating the task flags of a visible task is not safe.
581c61afb18SPaul Jackson  *
582c61afb18SPaul Jackson  * The above limitation is why this routine has the funny name
583c61afb18SPaul Jackson  * mpol_fix_fork_child_flag().
584c61afb18SPaul Jackson  *
585c61afb18SPaul Jackson  * It is also safe to call this with a task pointer of current,
586c61afb18SPaul Jackson  * which the static wrapper mpol_set_task_struct_flag() does,
587c61afb18SPaul Jackson  * for use within this file.
588c61afb18SPaul Jackson  */
589c61afb18SPaul Jackson 
590c61afb18SPaul Jackson void mpol_fix_fork_child_flag(struct task_struct *p)
591c61afb18SPaul Jackson {
592c61afb18SPaul Jackson 	if (p->mempolicy)
593c61afb18SPaul Jackson 		p->flags |= PF_MEMPOLICY;
594c61afb18SPaul Jackson 	else
595c61afb18SPaul Jackson 		p->flags &= ~PF_MEMPOLICY;
596c61afb18SPaul Jackson }
597c61afb18SPaul Jackson 
598c61afb18SPaul Jackson static void mpol_set_task_struct_flag(void)
599c61afb18SPaul Jackson {
600c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(current);
601c61afb18SPaul Jackson }
602c61afb18SPaul Jackson 
6031da177e4SLinus Torvalds /* Set the process memory policy */
604028fec41SDavid Rientjes static long do_set_mempolicy(unsigned short mode, unsigned short flags,
605028fec41SDavid Rientjes 			     nodemask_t *nodes)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	struct mempolicy *new;
608f4e53d91SLee Schermerhorn 	struct mm_struct *mm = current->mm;
6091da177e4SLinus Torvalds 
610028fec41SDavid Rientjes 	new = mpol_new(mode, flags, nodes);
6111da177e4SLinus Torvalds 	if (IS_ERR(new))
6121da177e4SLinus Torvalds 		return PTR_ERR(new);
613f4e53d91SLee Schermerhorn 
614f4e53d91SLee Schermerhorn 	/*
615f4e53d91SLee Schermerhorn 	 * prevent changing our mempolicy while show_numa_maps()
616f4e53d91SLee Schermerhorn 	 * is using it.
617f4e53d91SLee Schermerhorn 	 * Note:  do_set_mempolicy() can be called at init time
618f4e53d91SLee Schermerhorn 	 * with no 'mm'.
619f4e53d91SLee Schermerhorn 	 */
620f4e53d91SLee Schermerhorn 	if (mm)
621f4e53d91SLee Schermerhorn 		down_write(&mm->mmap_sem);
622f0be3d32SLee Schermerhorn 	mpol_put(current->mempolicy);
6231da177e4SLinus Torvalds 	current->mempolicy = new;
624c61afb18SPaul Jackson 	mpol_set_task_struct_flag();
62545c4745aSLee Schermerhorn 	if (new && new->mode == MPOL_INTERLEAVE &&
626f5b087b5SDavid Rientjes 	    nodes_weight(new->v.nodes))
627dfcd3c0dSAndi Kleen 		current->il_next = first_node(new->v.nodes);
628f4e53d91SLee Schermerhorn 	if (mm)
629f4e53d91SLee Schermerhorn 		up_write(&mm->mmap_sem);
630f4e53d91SLee Schermerhorn 
6311da177e4SLinus Torvalds 	return 0;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
634bea904d5SLee Schermerhorn /*
635bea904d5SLee Schermerhorn  * Return nodemask for policy for get_mempolicy() query
636bea904d5SLee Schermerhorn  */
637bea904d5SLee Schermerhorn static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
6381da177e4SLinus Torvalds {
639dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
640bea904d5SLee Schermerhorn 	if (p == &default_policy)
641bea904d5SLee Schermerhorn 		return;
642bea904d5SLee Schermerhorn 
64345c4745aSLee Schermerhorn 	switch (p->mode) {
64419770b32SMel Gorman 	case MPOL_BIND:
64519770b32SMel Gorman 		/* Fall through */
6461da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
647dfcd3c0dSAndi Kleen 		*nodes = p->v.nodes;
6481da177e4SLinus Torvalds 		break;
6491da177e4SLinus Torvalds 	case MPOL_PREFERRED:
650fc36b8d3SLee Schermerhorn 		if (!(p->flags & MPOL_F_LOCAL))
651dfcd3c0dSAndi Kleen 			node_set(p->v.preferred_node, *nodes);
65253f2556bSLee Schermerhorn 		/* else return empty node mask for local allocation */
6531da177e4SLinus Torvalds 		break;
6541da177e4SLinus Torvalds 	default:
6551da177e4SLinus Torvalds 		BUG();
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct page *p;
6621da177e4SLinus Torvalds 	int err;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
6651da177e4SLinus Torvalds 	if (err >= 0) {
6661da177e4SLinus Torvalds 		err = page_to_nid(p);
6671da177e4SLinus Torvalds 		put_page(p);
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 	return err;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds /* Retrieve NUMA policy */
673dbcb0f19SAdrian Bunk static long do_get_mempolicy(int *policy, nodemask_t *nmask,
6741da177e4SLinus Torvalds 			     unsigned long addr, unsigned long flags)
6751da177e4SLinus Torvalds {
6768bccd85fSChristoph Lameter 	int err;
6771da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
6781da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
6791da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
6801da177e4SLinus Torvalds 
681cf2a473cSPaul Jackson 	cpuset_update_task_memory_state();
682754af6f5SLee Schermerhorn 	if (flags &
683754af6f5SLee Schermerhorn 		~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
6841da177e4SLinus Torvalds 		return -EINVAL;
685754af6f5SLee Schermerhorn 
686754af6f5SLee Schermerhorn 	if (flags & MPOL_F_MEMS_ALLOWED) {
687754af6f5SLee Schermerhorn 		if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
688754af6f5SLee Schermerhorn 			return -EINVAL;
689754af6f5SLee Schermerhorn 		*policy = 0;	/* just so it's initialized */
690754af6f5SLee Schermerhorn 		*nmask  = cpuset_current_mems_allowed;
691754af6f5SLee Schermerhorn 		return 0;
692754af6f5SLee Schermerhorn 	}
693754af6f5SLee Schermerhorn 
6941da177e4SLinus Torvalds 	if (flags & MPOL_F_ADDR) {
695bea904d5SLee Schermerhorn 		/*
696bea904d5SLee Schermerhorn 		 * Do NOT fall back to task policy if the
697bea904d5SLee Schermerhorn 		 * vma/shared policy at addr is NULL.  We
698bea904d5SLee Schermerhorn 		 * want to return MPOL_DEFAULT in this case.
699bea904d5SLee Schermerhorn 		 */
7001da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
7011da177e4SLinus Torvalds 		vma = find_vma_intersection(mm, addr, addr+1);
7021da177e4SLinus Torvalds 		if (!vma) {
7031da177e4SLinus Torvalds 			up_read(&mm->mmap_sem);
7041da177e4SLinus Torvalds 			return -EFAULT;
7051da177e4SLinus Torvalds 		}
7061da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
7071da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
7081da177e4SLinus Torvalds 		else
7091da177e4SLinus Torvalds 			pol = vma->vm_policy;
7101da177e4SLinus Torvalds 	} else if (addr)
7111da177e4SLinus Torvalds 		return -EINVAL;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (!pol)
714bea904d5SLee Schermerhorn 		pol = &default_policy;	/* indicates default behavior */
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if (flags & MPOL_F_NODE) {
7171da177e4SLinus Torvalds 		if (flags & MPOL_F_ADDR) {
7181da177e4SLinus Torvalds 			err = lookup_node(mm, addr);
7191da177e4SLinus Torvalds 			if (err < 0)
7201da177e4SLinus Torvalds 				goto out;
7218bccd85fSChristoph Lameter 			*policy = err;
7221da177e4SLinus Torvalds 		} else if (pol == current->mempolicy &&
72345c4745aSLee Schermerhorn 				pol->mode == MPOL_INTERLEAVE) {
7248bccd85fSChristoph Lameter 			*policy = current->il_next;
7251da177e4SLinus Torvalds 		} else {
7261da177e4SLinus Torvalds 			err = -EINVAL;
7271da177e4SLinus Torvalds 			goto out;
7281da177e4SLinus Torvalds 		}
729bea904d5SLee Schermerhorn 	} else {
730bea904d5SLee Schermerhorn 		*policy = pol == &default_policy ? MPOL_DEFAULT :
731bea904d5SLee Schermerhorn 						pol->mode;
732bea904d5SLee Schermerhorn 		*policy |= pol->flags;
733bea904d5SLee Schermerhorn 	}
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (vma) {
7361da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
7371da177e4SLinus Torvalds 		vma = NULL;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	err = 0;
7418bccd85fSChristoph Lameter 	if (nmask)
742bea904d5SLee Schermerhorn 		get_policy_nodemask(pol, nmask);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds  out:
74552cd3b07SLee Schermerhorn 	mpol_cond_put(pol);
7461da177e4SLinus Torvalds 	if (vma)
7471da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
7481da177e4SLinus Torvalds 	return err;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
751b20a3503SChristoph Lameter #ifdef CONFIG_MIGRATION
7528bccd85fSChristoph Lameter /*
7536ce3c4c0SChristoph Lameter  * page migration
7546ce3c4c0SChristoph Lameter  */
755fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
756fc301289SChristoph Lameter 				unsigned long flags)
7576ce3c4c0SChristoph Lameter {
7586ce3c4c0SChristoph Lameter 	/*
759fc301289SChristoph Lameter 	 * Avoid migrating a page that is shared with others.
7606ce3c4c0SChristoph Lameter 	 */
761b20a3503SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1)
762b20a3503SChristoph Lameter 		isolate_lru_page(page, pagelist);
7636ce3c4c0SChristoph Lameter }
7646ce3c4c0SChristoph Lameter 
765742755a1SChristoph Lameter static struct page *new_node_page(struct page *page, unsigned long node, int **x)
76695a402c3SChristoph Lameter {
767769848c0SMel Gorman 	return alloc_pages_node(node, GFP_HIGHUSER_MOVABLE, 0);
76895a402c3SChristoph Lameter }
76995a402c3SChristoph Lameter 
7706ce3c4c0SChristoph Lameter /*
7717e2ab150SChristoph Lameter  * Migrate pages from one node to a target node.
7727e2ab150SChristoph Lameter  * Returns error or the number of pages not migrated.
7737e2ab150SChristoph Lameter  */
774dbcb0f19SAdrian Bunk static int migrate_to_node(struct mm_struct *mm, int source, int dest,
775dbcb0f19SAdrian Bunk 			   int flags)
7767e2ab150SChristoph Lameter {
7777e2ab150SChristoph Lameter 	nodemask_t nmask;
7787e2ab150SChristoph Lameter 	LIST_HEAD(pagelist);
7797e2ab150SChristoph Lameter 	int err = 0;
7807e2ab150SChristoph Lameter 
7817e2ab150SChristoph Lameter 	nodes_clear(nmask);
7827e2ab150SChristoph Lameter 	node_set(source, nmask);
7837e2ab150SChristoph Lameter 
7847e2ab150SChristoph Lameter 	check_range(mm, mm->mmap->vm_start, TASK_SIZE, &nmask,
7857e2ab150SChristoph Lameter 			flags | MPOL_MF_DISCONTIG_OK, &pagelist);
7867e2ab150SChristoph Lameter 
7877e2ab150SChristoph Lameter 	if (!list_empty(&pagelist))
78895a402c3SChristoph Lameter 		err = migrate_pages(&pagelist, new_node_page, dest);
78995a402c3SChristoph Lameter 
7907e2ab150SChristoph Lameter 	return err;
7917e2ab150SChristoph Lameter }
7927e2ab150SChristoph Lameter 
7937e2ab150SChristoph Lameter /*
7947e2ab150SChristoph Lameter  * Move pages between the two nodesets so as to preserve the physical
7957e2ab150SChristoph Lameter  * layout as much as possible.
79639743889SChristoph Lameter  *
79739743889SChristoph Lameter  * Returns the number of page that could not be moved.
79839743889SChristoph Lameter  */
79939743889SChristoph Lameter int do_migrate_pages(struct mm_struct *mm,
80039743889SChristoph Lameter 	const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
80139743889SChristoph Lameter {
80239743889SChristoph Lameter 	LIST_HEAD(pagelist);
8037e2ab150SChristoph Lameter 	int busy = 0;
8047e2ab150SChristoph Lameter 	int err = 0;
8057e2ab150SChristoph Lameter 	nodemask_t tmp;
80639743889SChristoph Lameter 
80739743889SChristoph Lameter 	down_read(&mm->mmap_sem);
808d4984711SChristoph Lameter 
8097b2259b3SChristoph Lameter 	err = migrate_vmas(mm, from_nodes, to_nodes, flags);
8107b2259b3SChristoph Lameter 	if (err)
8117b2259b3SChristoph Lameter 		goto out;
8127b2259b3SChristoph Lameter 
8137e2ab150SChristoph Lameter /*
8147e2ab150SChristoph Lameter  * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
8157e2ab150SChristoph Lameter  * bit in 'to' is not also set in 'tmp'.  Clear the found 'source'
8167e2ab150SChristoph Lameter  * bit in 'tmp', and return that <source, dest> pair for migration.
8177e2ab150SChristoph Lameter  * The pair of nodemasks 'to' and 'from' define the map.
8187e2ab150SChristoph Lameter  *
8197e2ab150SChristoph Lameter  * If no pair of bits is found that way, fallback to picking some
8207e2ab150SChristoph Lameter  * pair of 'source' and 'dest' bits that are not the same.  If the
8217e2ab150SChristoph Lameter  * 'source' and 'dest' bits are the same, this represents a node
8227e2ab150SChristoph Lameter  * that will be migrating to itself, so no pages need move.
8237e2ab150SChristoph Lameter  *
8247e2ab150SChristoph Lameter  * If no bits are left in 'tmp', or if all remaining bits left
8257e2ab150SChristoph Lameter  * in 'tmp' correspond to the same bit in 'to', return false
8267e2ab150SChristoph Lameter  * (nothing left to migrate).
8277e2ab150SChristoph Lameter  *
8287e2ab150SChristoph Lameter  * This lets us pick a pair of nodes to migrate between, such that
8297e2ab150SChristoph Lameter  * if possible the dest node is not already occupied by some other
8307e2ab150SChristoph Lameter  * source node, minimizing the risk of overloading the memory on a
8317e2ab150SChristoph Lameter  * node that would happen if we migrated incoming memory to a node
8327e2ab150SChristoph Lameter  * before migrating outgoing memory source that same node.
8337e2ab150SChristoph Lameter  *
8347e2ab150SChristoph Lameter  * A single scan of tmp is sufficient.  As we go, we remember the
8357e2ab150SChristoph Lameter  * most recent <s, d> pair that moved (s != d).  If we find a pair
8367e2ab150SChristoph Lameter  * that not only moved, but what's better, moved to an empty slot
8377e2ab150SChristoph Lameter  * (d is not set in tmp), then we break out then, with that pair.
8387e2ab150SChristoph Lameter  * Otherwise when we finish scannng from_tmp, we at least have the
8397e2ab150SChristoph Lameter  * most recent <s, d> pair that moved.  If we get all the way through
8407e2ab150SChristoph Lameter  * the scan of tmp without finding any node that moved, much less
8417e2ab150SChristoph Lameter  * moved to an empty node, then there is nothing left worth migrating.
8427e2ab150SChristoph Lameter  */
8437e2ab150SChristoph Lameter 
8447e2ab150SChristoph Lameter 	tmp = *from_nodes;
8457e2ab150SChristoph Lameter 	while (!nodes_empty(tmp)) {
8467e2ab150SChristoph Lameter 		int s,d;
8477e2ab150SChristoph Lameter 		int source = -1;
8487e2ab150SChristoph Lameter 		int dest = 0;
8497e2ab150SChristoph Lameter 
8507e2ab150SChristoph Lameter 		for_each_node_mask(s, tmp) {
8517e2ab150SChristoph Lameter 			d = node_remap(s, *from_nodes, *to_nodes);
8527e2ab150SChristoph Lameter 			if (s == d)
8537e2ab150SChristoph Lameter 				continue;
8547e2ab150SChristoph Lameter 
8557e2ab150SChristoph Lameter 			source = s;	/* Node moved. Memorize */
8567e2ab150SChristoph Lameter 			dest = d;
8577e2ab150SChristoph Lameter 
8587e2ab150SChristoph Lameter 			/* dest not in remaining from nodes? */
8597e2ab150SChristoph Lameter 			if (!node_isset(dest, tmp))
8607e2ab150SChristoph Lameter 				break;
8617e2ab150SChristoph Lameter 		}
8627e2ab150SChristoph Lameter 		if (source == -1)
8637e2ab150SChristoph Lameter 			break;
8647e2ab150SChristoph Lameter 
8657e2ab150SChristoph Lameter 		node_clear(source, tmp);
8667e2ab150SChristoph Lameter 		err = migrate_to_node(mm, source, dest, flags);
8677e2ab150SChristoph Lameter 		if (err > 0)
8687e2ab150SChristoph Lameter 			busy += err;
8697e2ab150SChristoph Lameter 		if (err < 0)
8707e2ab150SChristoph Lameter 			break;
87139743889SChristoph Lameter 	}
8727b2259b3SChristoph Lameter out:
87339743889SChristoph Lameter 	up_read(&mm->mmap_sem);
8747e2ab150SChristoph Lameter 	if (err < 0)
8757e2ab150SChristoph Lameter 		return err;
8767e2ab150SChristoph Lameter 	return busy;
877b20a3503SChristoph Lameter 
87839743889SChristoph Lameter }
87939743889SChristoph Lameter 
8803ad33b24SLee Schermerhorn /*
8813ad33b24SLee Schermerhorn  * Allocate a new page for page migration based on vma policy.
8823ad33b24SLee Schermerhorn  * Start assuming that page is mapped by vma pointed to by @private.
8833ad33b24SLee Schermerhorn  * Search forward from there, if not.  N.B., this assumes that the
8843ad33b24SLee Schermerhorn  * list of pages handed to migrate_pages()--which is how we get here--
8853ad33b24SLee Schermerhorn  * is in virtual address order.
8863ad33b24SLee Schermerhorn  */
887742755a1SChristoph Lameter static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
88895a402c3SChristoph Lameter {
88995a402c3SChristoph Lameter 	struct vm_area_struct *vma = (struct vm_area_struct *)private;
8903ad33b24SLee Schermerhorn 	unsigned long uninitialized_var(address);
89195a402c3SChristoph Lameter 
8923ad33b24SLee Schermerhorn 	while (vma) {
8933ad33b24SLee Schermerhorn 		address = page_address_in_vma(page, vma);
8943ad33b24SLee Schermerhorn 		if (address != -EFAULT)
8953ad33b24SLee Schermerhorn 			break;
8963ad33b24SLee Schermerhorn 		vma = vma->vm_next;
8973ad33b24SLee Schermerhorn 	}
8983ad33b24SLee Schermerhorn 
8993ad33b24SLee Schermerhorn 	/*
9003ad33b24SLee Schermerhorn 	 * if !vma, alloc_page_vma() will use task or system default policy
9013ad33b24SLee Schermerhorn 	 */
9023ad33b24SLee Schermerhorn 	return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
90395a402c3SChristoph Lameter }
904b20a3503SChristoph Lameter #else
905b20a3503SChristoph Lameter 
906b20a3503SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
907b20a3503SChristoph Lameter 				unsigned long flags)
908b20a3503SChristoph Lameter {
909b20a3503SChristoph Lameter }
910b20a3503SChristoph Lameter 
911b20a3503SChristoph Lameter int do_migrate_pages(struct mm_struct *mm,
912b20a3503SChristoph Lameter 	const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
913b20a3503SChristoph Lameter {
914b20a3503SChristoph Lameter 	return -ENOSYS;
915b20a3503SChristoph Lameter }
91695a402c3SChristoph Lameter 
91769939749SKeith Owens static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
91895a402c3SChristoph Lameter {
91995a402c3SChristoph Lameter 	return NULL;
92095a402c3SChristoph Lameter }
921b20a3503SChristoph Lameter #endif
922b20a3503SChristoph Lameter 
923dbcb0f19SAdrian Bunk static long do_mbind(unsigned long start, unsigned long len,
924028fec41SDavid Rientjes 		     unsigned short mode, unsigned short mode_flags,
925028fec41SDavid Rientjes 		     nodemask_t *nmask, unsigned long flags)
9266ce3c4c0SChristoph Lameter {
9276ce3c4c0SChristoph Lameter 	struct vm_area_struct *vma;
9286ce3c4c0SChristoph Lameter 	struct mm_struct *mm = current->mm;
9296ce3c4c0SChristoph Lameter 	struct mempolicy *new;
9306ce3c4c0SChristoph Lameter 	unsigned long end;
9316ce3c4c0SChristoph Lameter 	int err;
9326ce3c4c0SChristoph Lameter 	LIST_HEAD(pagelist);
9336ce3c4c0SChristoph Lameter 
934a3b51e01SDavid Rientjes 	if (flags & ~(unsigned long)(MPOL_MF_STRICT |
9356ce3c4c0SChristoph Lameter 				     MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
9366ce3c4c0SChristoph Lameter 		return -EINVAL;
93774c00241SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
9386ce3c4c0SChristoph Lameter 		return -EPERM;
9396ce3c4c0SChristoph Lameter 
9406ce3c4c0SChristoph Lameter 	if (start & ~PAGE_MASK)
9416ce3c4c0SChristoph Lameter 		return -EINVAL;
9426ce3c4c0SChristoph Lameter 
9436ce3c4c0SChristoph Lameter 	if (mode == MPOL_DEFAULT)
9446ce3c4c0SChristoph Lameter 		flags &= ~MPOL_MF_STRICT;
9456ce3c4c0SChristoph Lameter 
9466ce3c4c0SChristoph Lameter 	len = (len + PAGE_SIZE - 1) & PAGE_MASK;
9476ce3c4c0SChristoph Lameter 	end = start + len;
9486ce3c4c0SChristoph Lameter 
9496ce3c4c0SChristoph Lameter 	if (end < start)
9506ce3c4c0SChristoph Lameter 		return -EINVAL;
9516ce3c4c0SChristoph Lameter 	if (end == start)
9526ce3c4c0SChristoph Lameter 		return 0;
9536ce3c4c0SChristoph Lameter 
954028fec41SDavid Rientjes 	new = mpol_new(mode, mode_flags, nmask);
9556ce3c4c0SChristoph Lameter 	if (IS_ERR(new))
9566ce3c4c0SChristoph Lameter 		return PTR_ERR(new);
9576ce3c4c0SChristoph Lameter 
9586ce3c4c0SChristoph Lameter 	/*
9596ce3c4c0SChristoph Lameter 	 * If we are using the default policy then operation
9606ce3c4c0SChristoph Lameter 	 * on discontinuous address spaces is okay after all
9616ce3c4c0SChristoph Lameter 	 */
9626ce3c4c0SChristoph Lameter 	if (!new)
9636ce3c4c0SChristoph Lameter 		flags |= MPOL_MF_DISCONTIG_OK;
9646ce3c4c0SChristoph Lameter 
965028fec41SDavid Rientjes 	pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
966028fec41SDavid Rientjes 		 start, start + len, mode, mode_flags,
967028fec41SDavid Rientjes 		 nmask ? nodes_addr(*nmask)[0] : -1);
9686ce3c4c0SChristoph Lameter 
9696ce3c4c0SChristoph Lameter 	down_write(&mm->mmap_sem);
9706ce3c4c0SChristoph Lameter 	vma = check_range(mm, start, end, nmask,
9716ce3c4c0SChristoph Lameter 			  flags | MPOL_MF_INVERT, &pagelist);
9726ce3c4c0SChristoph Lameter 
9736ce3c4c0SChristoph Lameter 	err = PTR_ERR(vma);
9746ce3c4c0SChristoph Lameter 	if (!IS_ERR(vma)) {
9756ce3c4c0SChristoph Lameter 		int nr_failed = 0;
9766ce3c4c0SChristoph Lameter 
9776ce3c4c0SChristoph Lameter 		err = mbind_range(vma, start, end, new);
9787e2ab150SChristoph Lameter 
9796ce3c4c0SChristoph Lameter 		if (!list_empty(&pagelist))
98095a402c3SChristoph Lameter 			nr_failed = migrate_pages(&pagelist, new_vma_page,
98195a402c3SChristoph Lameter 						(unsigned long)vma);
9826ce3c4c0SChristoph Lameter 
9836ce3c4c0SChristoph Lameter 		if (!err && nr_failed && (flags & MPOL_MF_STRICT))
9846ce3c4c0SChristoph Lameter 			err = -EIO;
9856ce3c4c0SChristoph Lameter 	}
986b20a3503SChristoph Lameter 
9876ce3c4c0SChristoph Lameter 	up_write(&mm->mmap_sem);
988f0be3d32SLee Schermerhorn 	mpol_put(new);
9896ce3c4c0SChristoph Lameter 	return err;
9906ce3c4c0SChristoph Lameter }
9916ce3c4c0SChristoph Lameter 
99239743889SChristoph Lameter /*
9938bccd85fSChristoph Lameter  * User space interface with variable sized bitmaps for nodelists.
9948bccd85fSChristoph Lameter  */
9958bccd85fSChristoph Lameter 
9968bccd85fSChristoph Lameter /* Copy a node mask from user space. */
99739743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
9988bccd85fSChristoph Lameter 		     unsigned long maxnode)
9998bccd85fSChristoph Lameter {
10008bccd85fSChristoph Lameter 	unsigned long k;
10018bccd85fSChristoph Lameter 	unsigned long nlongs;
10028bccd85fSChristoph Lameter 	unsigned long endmask;
10038bccd85fSChristoph Lameter 
10048bccd85fSChristoph Lameter 	--maxnode;
10058bccd85fSChristoph Lameter 	nodes_clear(*nodes);
10068bccd85fSChristoph Lameter 	if (maxnode == 0 || !nmask)
10078bccd85fSChristoph Lameter 		return 0;
1008a9c930baSAndi Kleen 	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
1009636f13c1SChris Wright 		return -EINVAL;
10108bccd85fSChristoph Lameter 
10118bccd85fSChristoph Lameter 	nlongs = BITS_TO_LONGS(maxnode);
10128bccd85fSChristoph Lameter 	if ((maxnode % BITS_PER_LONG) == 0)
10138bccd85fSChristoph Lameter 		endmask = ~0UL;
10148bccd85fSChristoph Lameter 	else
10158bccd85fSChristoph Lameter 		endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
10168bccd85fSChristoph Lameter 
10178bccd85fSChristoph Lameter 	/* When the user specified more nodes than supported just check
10188bccd85fSChristoph Lameter 	   if the non supported part is all zero. */
10198bccd85fSChristoph Lameter 	if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
10208bccd85fSChristoph Lameter 		if (nlongs > PAGE_SIZE/sizeof(long))
10218bccd85fSChristoph Lameter 			return -EINVAL;
10228bccd85fSChristoph Lameter 		for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
10238bccd85fSChristoph Lameter 			unsigned long t;
10248bccd85fSChristoph Lameter 			if (get_user(t, nmask + k))
10258bccd85fSChristoph Lameter 				return -EFAULT;
10268bccd85fSChristoph Lameter 			if (k == nlongs - 1) {
10278bccd85fSChristoph Lameter 				if (t & endmask)
10288bccd85fSChristoph Lameter 					return -EINVAL;
10298bccd85fSChristoph Lameter 			} else if (t)
10308bccd85fSChristoph Lameter 				return -EINVAL;
10318bccd85fSChristoph Lameter 		}
10328bccd85fSChristoph Lameter 		nlongs = BITS_TO_LONGS(MAX_NUMNODES);
10338bccd85fSChristoph Lameter 		endmask = ~0UL;
10348bccd85fSChristoph Lameter 	}
10358bccd85fSChristoph Lameter 
10368bccd85fSChristoph Lameter 	if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
10378bccd85fSChristoph Lameter 		return -EFAULT;
10388bccd85fSChristoph Lameter 	nodes_addr(*nodes)[nlongs-1] &= endmask;
10398bccd85fSChristoph Lameter 	return 0;
10408bccd85fSChristoph Lameter }
10418bccd85fSChristoph Lameter 
10428bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */
10438bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
10448bccd85fSChristoph Lameter 			      nodemask_t *nodes)
10458bccd85fSChristoph Lameter {
10468bccd85fSChristoph Lameter 	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
10478bccd85fSChristoph Lameter 	const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
10488bccd85fSChristoph Lameter 
10498bccd85fSChristoph Lameter 	if (copy > nbytes) {
10508bccd85fSChristoph Lameter 		if (copy > PAGE_SIZE)
10518bccd85fSChristoph Lameter 			return -EINVAL;
10528bccd85fSChristoph Lameter 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
10538bccd85fSChristoph Lameter 			return -EFAULT;
10548bccd85fSChristoph Lameter 		copy = nbytes;
10558bccd85fSChristoph Lameter 	}
10568bccd85fSChristoph Lameter 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
10578bccd85fSChristoph Lameter }
10588bccd85fSChristoph Lameter 
10598bccd85fSChristoph Lameter asmlinkage long sys_mbind(unsigned long start, unsigned long len,
10608bccd85fSChristoph Lameter 			unsigned long mode,
10618bccd85fSChristoph Lameter 			unsigned long __user *nmask, unsigned long maxnode,
10628bccd85fSChristoph Lameter 			unsigned flags)
10638bccd85fSChristoph Lameter {
10648bccd85fSChristoph Lameter 	nodemask_t nodes;
10658bccd85fSChristoph Lameter 	int err;
1066028fec41SDavid Rientjes 	unsigned short mode_flags;
10678bccd85fSChristoph Lameter 
1068028fec41SDavid Rientjes 	mode_flags = mode & MPOL_MODE_FLAGS;
1069028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1070a3b51e01SDavid Rientjes 	if (mode >= MPOL_MAX)
1071a3b51e01SDavid Rientjes 		return -EINVAL;
10724c50bc01SDavid Rientjes 	if ((mode_flags & MPOL_F_STATIC_NODES) &&
10734c50bc01SDavid Rientjes 	    (mode_flags & MPOL_F_RELATIVE_NODES))
10744c50bc01SDavid Rientjes 		return -EINVAL;
10758bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
10768bccd85fSChristoph Lameter 	if (err)
10778bccd85fSChristoph Lameter 		return err;
1078028fec41SDavid Rientjes 	return do_mbind(start, len, mode, mode_flags, &nodes, flags);
10798bccd85fSChristoph Lameter }
10808bccd85fSChristoph Lameter 
10818bccd85fSChristoph Lameter /* Set the process memory policy */
10828bccd85fSChristoph Lameter asmlinkage long sys_set_mempolicy(int mode, unsigned long __user *nmask,
10838bccd85fSChristoph Lameter 		unsigned long maxnode)
10848bccd85fSChristoph Lameter {
10858bccd85fSChristoph Lameter 	int err;
10868bccd85fSChristoph Lameter 	nodemask_t nodes;
1087028fec41SDavid Rientjes 	unsigned short flags;
10888bccd85fSChristoph Lameter 
1089028fec41SDavid Rientjes 	flags = mode & MPOL_MODE_FLAGS;
1090028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1091028fec41SDavid Rientjes 	if ((unsigned int)mode >= MPOL_MAX)
10928bccd85fSChristoph Lameter 		return -EINVAL;
10934c50bc01SDavid Rientjes 	if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
10944c50bc01SDavid Rientjes 		return -EINVAL;
10958bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
10968bccd85fSChristoph Lameter 	if (err)
10978bccd85fSChristoph Lameter 		return err;
1098028fec41SDavid Rientjes 	return do_set_mempolicy(mode, flags, &nodes);
10998bccd85fSChristoph Lameter }
11008bccd85fSChristoph Lameter 
110139743889SChristoph Lameter asmlinkage long sys_migrate_pages(pid_t pid, unsigned long maxnode,
110239743889SChristoph Lameter 		const unsigned long __user *old_nodes,
110339743889SChristoph Lameter 		const unsigned long __user *new_nodes)
110439743889SChristoph Lameter {
110539743889SChristoph Lameter 	struct mm_struct *mm;
110639743889SChristoph Lameter 	struct task_struct *task;
110739743889SChristoph Lameter 	nodemask_t old;
110839743889SChristoph Lameter 	nodemask_t new;
110939743889SChristoph Lameter 	nodemask_t task_nodes;
111039743889SChristoph Lameter 	int err;
111139743889SChristoph Lameter 
111239743889SChristoph Lameter 	err = get_nodes(&old, old_nodes, maxnode);
111339743889SChristoph Lameter 	if (err)
111439743889SChristoph Lameter 		return err;
111539743889SChristoph Lameter 
111639743889SChristoph Lameter 	err = get_nodes(&new, new_nodes, maxnode);
111739743889SChristoph Lameter 	if (err)
111839743889SChristoph Lameter 		return err;
111939743889SChristoph Lameter 
112039743889SChristoph Lameter 	/* Find the mm_struct */
112139743889SChristoph Lameter 	read_lock(&tasklist_lock);
1122228ebcbeSPavel Emelyanov 	task = pid ? find_task_by_vpid(pid) : current;
112339743889SChristoph Lameter 	if (!task) {
112439743889SChristoph Lameter 		read_unlock(&tasklist_lock);
112539743889SChristoph Lameter 		return -ESRCH;
112639743889SChristoph Lameter 	}
112739743889SChristoph Lameter 	mm = get_task_mm(task);
112839743889SChristoph Lameter 	read_unlock(&tasklist_lock);
112939743889SChristoph Lameter 
113039743889SChristoph Lameter 	if (!mm)
113139743889SChristoph Lameter 		return -EINVAL;
113239743889SChristoph Lameter 
113339743889SChristoph Lameter 	/*
113439743889SChristoph Lameter 	 * Check if this process has the right to modify the specified
113539743889SChristoph Lameter 	 * process. The right exists if the process has administrative
11367f927fccSAlexey Dobriyan 	 * capabilities, superuser privileges or the same
113739743889SChristoph Lameter 	 * userid as the target process.
113839743889SChristoph Lameter 	 */
113939743889SChristoph Lameter 	if ((current->euid != task->suid) && (current->euid != task->uid) &&
114039743889SChristoph Lameter 	    (current->uid != task->suid) && (current->uid != task->uid) &&
114174c00241SChristoph Lameter 	    !capable(CAP_SYS_NICE)) {
114239743889SChristoph Lameter 		err = -EPERM;
114339743889SChristoph Lameter 		goto out;
114439743889SChristoph Lameter 	}
114539743889SChristoph Lameter 
114639743889SChristoph Lameter 	task_nodes = cpuset_mems_allowed(task);
114739743889SChristoph Lameter 	/* Is the user allowed to access the target nodes? */
114874c00241SChristoph Lameter 	if (!nodes_subset(new, task_nodes) && !capable(CAP_SYS_NICE)) {
114939743889SChristoph Lameter 		err = -EPERM;
115039743889SChristoph Lameter 		goto out;
115139743889SChristoph Lameter 	}
115239743889SChristoph Lameter 
115337b07e41SLee Schermerhorn 	if (!nodes_subset(new, node_states[N_HIGH_MEMORY])) {
11543b42d28bSChristoph Lameter 		err = -EINVAL;
11553b42d28bSChristoph Lameter 		goto out;
11563b42d28bSChristoph Lameter 	}
11573b42d28bSChristoph Lameter 
115886c3a764SDavid Quigley 	err = security_task_movememory(task);
115986c3a764SDavid Quigley 	if (err)
116086c3a764SDavid Quigley 		goto out;
116186c3a764SDavid Quigley 
1162511030bcSChristoph Lameter 	err = do_migrate_pages(mm, &old, &new,
116374c00241SChristoph Lameter 		capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
116439743889SChristoph Lameter out:
116539743889SChristoph Lameter 	mmput(mm);
116639743889SChristoph Lameter 	return err;
116739743889SChristoph Lameter }
116839743889SChristoph Lameter 
116939743889SChristoph Lameter 
11708bccd85fSChristoph Lameter /* Retrieve NUMA policy */
11718bccd85fSChristoph Lameter asmlinkage long sys_get_mempolicy(int __user *policy,
11728bccd85fSChristoph Lameter 				unsigned long __user *nmask,
11738bccd85fSChristoph Lameter 				unsigned long maxnode,
11748bccd85fSChristoph Lameter 				unsigned long addr, unsigned long flags)
11758bccd85fSChristoph Lameter {
1176dbcb0f19SAdrian Bunk 	int err;
1177dbcb0f19SAdrian Bunk 	int uninitialized_var(pval);
11788bccd85fSChristoph Lameter 	nodemask_t nodes;
11798bccd85fSChristoph Lameter 
11808bccd85fSChristoph Lameter 	if (nmask != NULL && maxnode < MAX_NUMNODES)
11818bccd85fSChristoph Lameter 		return -EINVAL;
11828bccd85fSChristoph Lameter 
11838bccd85fSChristoph Lameter 	err = do_get_mempolicy(&pval, &nodes, addr, flags);
11848bccd85fSChristoph Lameter 
11858bccd85fSChristoph Lameter 	if (err)
11868bccd85fSChristoph Lameter 		return err;
11878bccd85fSChristoph Lameter 
11888bccd85fSChristoph Lameter 	if (policy && put_user(pval, policy))
11898bccd85fSChristoph Lameter 		return -EFAULT;
11908bccd85fSChristoph Lameter 
11918bccd85fSChristoph Lameter 	if (nmask)
11928bccd85fSChristoph Lameter 		err = copy_nodes_to_user(nmask, maxnode, &nodes);
11938bccd85fSChristoph Lameter 
11948bccd85fSChristoph Lameter 	return err;
11958bccd85fSChristoph Lameter }
11968bccd85fSChristoph Lameter 
11971da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy,
12001da177e4SLinus Torvalds 				     compat_ulong_t __user *nmask,
12011da177e4SLinus Torvalds 				     compat_ulong_t maxnode,
12021da177e4SLinus Torvalds 				     compat_ulong_t addr, compat_ulong_t flags)
12031da177e4SLinus Torvalds {
12041da177e4SLinus Torvalds 	long err;
12051da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
12061da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
12071da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
12101da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	if (nmask)
12131da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	if (!err && nmask) {
12181da177e4SLinus Torvalds 		err = copy_from_user(bm, nm, alloc_size);
12191da177e4SLinus Torvalds 		/* ensure entire bitmap is zeroed */
12201da177e4SLinus Torvalds 		err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
12211da177e4SLinus Torvalds 		err |= compat_put_bitmap(nmask, bm, nr_bits);
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	return err;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
12281da177e4SLinus Torvalds 				     compat_ulong_t maxnode)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 	long err = 0;
12311da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
12321da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
12331da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
12361da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	if (nmask) {
12391da177e4SLinus Torvalds 		err = compat_get_bitmap(bm, nmask, nr_bits);
12401da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
12411da177e4SLinus Torvalds 		err |= copy_to_user(nm, bm, alloc_size);
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	if (err)
12451da177e4SLinus Torvalds 		return -EFAULT;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	return sys_set_mempolicy(mode, nm, nr_bits+1);
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
12511da177e4SLinus Torvalds 			     compat_ulong_t mode, compat_ulong_t __user *nmask,
12521da177e4SLinus Torvalds 			     compat_ulong_t maxnode, compat_ulong_t flags)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	long err = 0;
12551da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
12561da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
1257dfcd3c0dSAndi Kleen 	nodemask_t bm;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
12601da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	if (nmask) {
1263dfcd3c0dSAndi Kleen 		err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
12641da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
1265dfcd3c0dSAndi Kleen 		err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
12661da177e4SLinus Torvalds 	}
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	if (err)
12691da177e4SLinus Torvalds 		return -EFAULT;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds #endif
12751da177e4SLinus Torvalds 
1276480eccf9SLee Schermerhorn /*
1277480eccf9SLee Schermerhorn  * get_vma_policy(@task, @vma, @addr)
1278480eccf9SLee Schermerhorn  * @task - task for fallback if vma policy == default
1279480eccf9SLee Schermerhorn  * @vma   - virtual memory area whose policy is sought
1280480eccf9SLee Schermerhorn  * @addr  - address in @vma for shared policy lookup
1281480eccf9SLee Schermerhorn  *
1282480eccf9SLee Schermerhorn  * Returns effective policy for a VMA at specified address.
1283480eccf9SLee Schermerhorn  * Falls back to @task or system default policy, as necessary.
128452cd3b07SLee Schermerhorn  * Current or other task's task mempolicy and non-shared vma policies
128552cd3b07SLee Schermerhorn  * are protected by the task's mmap_sem, which must be held for read by
128652cd3b07SLee Schermerhorn  * the caller.
128752cd3b07SLee Schermerhorn  * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
128852cd3b07SLee Schermerhorn  * count--added by the get_policy() vm_op, as appropriate--to protect against
128952cd3b07SLee Schermerhorn  * freeing by another task.  It is the caller's responsibility to free the
129052cd3b07SLee Schermerhorn  * extra reference for shared policies.
1291480eccf9SLee Schermerhorn  */
129248fce342SChristoph Lameter static struct mempolicy *get_vma_policy(struct task_struct *task,
129348fce342SChristoph Lameter 		struct vm_area_struct *vma, unsigned long addr)
12941da177e4SLinus Torvalds {
12956e21c8f1SChristoph Lameter 	struct mempolicy *pol = task->mempolicy;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	if (vma) {
1298480eccf9SLee Schermerhorn 		if (vma->vm_ops && vma->vm_ops->get_policy) {
1299ae4d8c16SLee Schermerhorn 			struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
1300ae4d8c16SLee Schermerhorn 									addr);
1301ae4d8c16SLee Schermerhorn 			if (vpol)
1302ae4d8c16SLee Schermerhorn 				pol = vpol;
1303bea904d5SLee Schermerhorn 		} else if (vma->vm_policy)
13041da177e4SLinus Torvalds 			pol = vma->vm_policy;
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 	if (!pol)
13071da177e4SLinus Torvalds 		pol = &default_policy;
13081da177e4SLinus Torvalds 	return pol;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
131152cd3b07SLee Schermerhorn /*
131252cd3b07SLee Schermerhorn  * Return a nodemask representing a mempolicy for filtering nodes for
131352cd3b07SLee Schermerhorn  * page allocation
131452cd3b07SLee Schermerhorn  */
131552cd3b07SLee Schermerhorn static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
131619770b32SMel Gorman {
131719770b32SMel Gorman 	/* Lower zones don't get a nodemask applied for MPOL_BIND */
131845c4745aSLee Schermerhorn 	if (unlikely(policy->mode == MPOL_BIND) &&
131919770b32SMel Gorman 			gfp_zone(gfp) >= policy_zone &&
132019770b32SMel Gorman 			cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
132119770b32SMel Gorman 		return &policy->v.nodes;
132219770b32SMel Gorman 
132319770b32SMel Gorman 	return NULL;
132419770b32SMel Gorman }
132519770b32SMel Gorman 
132652cd3b07SLee Schermerhorn /* Return a zonelist indicated by gfp for node representing a mempolicy */
132752cd3b07SLee Schermerhorn static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy)
13281da177e4SLinus Torvalds {
1329fc36b8d3SLee Schermerhorn 	int nd = numa_node_id();
13301da177e4SLinus Torvalds 
133145c4745aSLee Schermerhorn 	switch (policy->mode) {
13321da177e4SLinus Torvalds 	case MPOL_PREFERRED:
1333fc36b8d3SLee Schermerhorn 		if (!(policy->flags & MPOL_F_LOCAL))
13341da177e4SLinus Torvalds 			nd = policy->v.preferred_node;
13351da177e4SLinus Torvalds 		break;
13361da177e4SLinus Torvalds 	case MPOL_BIND:
133719770b32SMel Gorman 		/*
133852cd3b07SLee Schermerhorn 		 * Normally, MPOL_BIND allocations are node-local within the
133952cd3b07SLee Schermerhorn 		 * allowed nodemask.  However, if __GFP_THISNODE is set and the
134052cd3b07SLee Schermerhorn 		 * current node is part of the mask, we use the zonelist for
134152cd3b07SLee Schermerhorn 		 * the first node in the mask instead.
134219770b32SMel Gorman 		 */
134319770b32SMel Gorman 		if (unlikely(gfp & __GFP_THISNODE) &&
134419770b32SMel Gorman 				unlikely(!node_isset(nd, policy->v.nodes)))
134519770b32SMel Gorman 			nd = first_node(policy->v.nodes);
134619770b32SMel Gorman 		break;
13471da177e4SLinus Torvalds 	case MPOL_INTERLEAVE: /* should not happen */
13481da177e4SLinus Torvalds 		break;
13491da177e4SLinus Torvalds 	default:
13501da177e4SLinus Torvalds 		BUG();
13511da177e4SLinus Torvalds 	}
13520e88460dSMel Gorman 	return node_zonelist(nd, gfp);
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds /* Do dynamic interleaving for a process */
13561da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	unsigned nid, next;
13591da177e4SLinus Torvalds 	struct task_struct *me = current;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	nid = me->il_next;
1362dfcd3c0dSAndi Kleen 	next = next_node(nid, policy->v.nodes);
13631da177e4SLinus Torvalds 	if (next >= MAX_NUMNODES)
1364dfcd3c0dSAndi Kleen 		next = first_node(policy->v.nodes);
1365f5b087b5SDavid Rientjes 	if (next < MAX_NUMNODES)
13661da177e4SLinus Torvalds 		me->il_next = next;
13671da177e4SLinus Torvalds 	return nid;
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
1370dc85da15SChristoph Lameter /*
1371dc85da15SChristoph Lameter  * Depending on the memory policy provide a node from which to allocate the
1372dc85da15SChristoph Lameter  * next slab entry.
137352cd3b07SLee Schermerhorn  * @policy must be protected by freeing by the caller.  If @policy is
137452cd3b07SLee Schermerhorn  * the current task's mempolicy, this protection is implicit, as only the
137552cd3b07SLee Schermerhorn  * task can change it's policy.  The system default policy requires no
137652cd3b07SLee Schermerhorn  * such protection.
1377dc85da15SChristoph Lameter  */
1378dc85da15SChristoph Lameter unsigned slab_node(struct mempolicy *policy)
1379dc85da15SChristoph Lameter {
1380fc36b8d3SLee Schermerhorn 	if (!policy || policy->flags & MPOL_F_LOCAL)
1381bea904d5SLee Schermerhorn 		return numa_node_id();
1382765c4507SChristoph Lameter 
1383bea904d5SLee Schermerhorn 	switch (policy->mode) {
1384bea904d5SLee Schermerhorn 	case MPOL_PREFERRED:
1385fc36b8d3SLee Schermerhorn 		/*
1386fc36b8d3SLee Schermerhorn 		 * handled MPOL_F_LOCAL above
1387fc36b8d3SLee Schermerhorn 		 */
1388bea904d5SLee Schermerhorn 		return policy->v.preferred_node;
1389bea904d5SLee Schermerhorn 
1390dc85da15SChristoph Lameter 	case MPOL_INTERLEAVE:
1391dc85da15SChristoph Lameter 		return interleave_nodes(policy);
1392dc85da15SChristoph Lameter 
1393dd1a239fSMel Gorman 	case MPOL_BIND: {
1394dc85da15SChristoph Lameter 		/*
1395dc85da15SChristoph Lameter 		 * Follow bind policy behavior and start allocation at the
1396dc85da15SChristoph Lameter 		 * first node.
1397dc85da15SChristoph Lameter 		 */
139819770b32SMel Gorman 		struct zonelist *zonelist;
139919770b32SMel Gorman 		struct zone *zone;
140019770b32SMel Gorman 		enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
140119770b32SMel Gorman 		zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0];
140219770b32SMel Gorman 		(void)first_zones_zonelist(zonelist, highest_zoneidx,
140319770b32SMel Gorman 							&policy->v.nodes,
140419770b32SMel Gorman 							&zone);
140519770b32SMel Gorman 		return zone->node;
1406dd1a239fSMel Gorman 	}
1407dc85da15SChristoph Lameter 
1408dc85da15SChristoph Lameter 	default:
1409bea904d5SLee Schermerhorn 		BUG();
1410dc85da15SChristoph Lameter 	}
1411dc85da15SChristoph Lameter }
1412dc85da15SChristoph Lameter 
14131da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */
14141da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol,
14151da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long off)
14161da177e4SLinus Torvalds {
1417dfcd3c0dSAndi Kleen 	unsigned nnodes = nodes_weight(pol->v.nodes);
1418f5b087b5SDavid Rientjes 	unsigned target;
14191da177e4SLinus Torvalds 	int c;
14201da177e4SLinus Torvalds 	int nid = -1;
14211da177e4SLinus Torvalds 
1422f5b087b5SDavid Rientjes 	if (!nnodes)
1423f5b087b5SDavid Rientjes 		return numa_node_id();
1424f5b087b5SDavid Rientjes 	target = (unsigned int)off % nnodes;
14251da177e4SLinus Torvalds 	c = 0;
14261da177e4SLinus Torvalds 	do {
1427dfcd3c0dSAndi Kleen 		nid = next_node(nid, pol->v.nodes);
14281da177e4SLinus Torvalds 		c++;
14291da177e4SLinus Torvalds 	} while (c <= target);
14301da177e4SLinus Torvalds 	return nid;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
14335da7ca86SChristoph Lameter /* Determine a node number for interleave */
14345da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol,
14355da7ca86SChristoph Lameter 		 struct vm_area_struct *vma, unsigned long addr, int shift)
14365da7ca86SChristoph Lameter {
14375da7ca86SChristoph Lameter 	if (vma) {
14385da7ca86SChristoph Lameter 		unsigned long off;
14395da7ca86SChristoph Lameter 
14403b98b087SNishanth Aravamudan 		/*
14413b98b087SNishanth Aravamudan 		 * for small pages, there is no difference between
14423b98b087SNishanth Aravamudan 		 * shift and PAGE_SHIFT, so the bit-shift is safe.
14433b98b087SNishanth Aravamudan 		 * for huge pages, since vm_pgoff is in units of small
14443b98b087SNishanth Aravamudan 		 * pages, we need to shift off the always 0 bits to get
14453b98b087SNishanth Aravamudan 		 * a useful offset.
14463b98b087SNishanth Aravamudan 		 */
14473b98b087SNishanth Aravamudan 		BUG_ON(shift < PAGE_SHIFT);
14483b98b087SNishanth Aravamudan 		off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
14495da7ca86SChristoph Lameter 		off += (addr - vma->vm_start) >> shift;
14505da7ca86SChristoph Lameter 		return offset_il_node(pol, vma, off);
14515da7ca86SChristoph Lameter 	} else
14525da7ca86SChristoph Lameter 		return interleave_nodes(pol);
14535da7ca86SChristoph Lameter }
14545da7ca86SChristoph Lameter 
145500ac59adSChen, Kenneth W #ifdef CONFIG_HUGETLBFS
1456480eccf9SLee Schermerhorn /*
1457480eccf9SLee Schermerhorn  * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
1458480eccf9SLee Schermerhorn  * @vma = virtual memory area whose policy is sought
1459480eccf9SLee Schermerhorn  * @addr = address in @vma for shared policy lookup and interleave policy
1460480eccf9SLee Schermerhorn  * @gfp_flags = for requested zone
146119770b32SMel Gorman  * @mpol = pointer to mempolicy pointer for reference counted mempolicy
146219770b32SMel Gorman  * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask
1463480eccf9SLee Schermerhorn  *
146452cd3b07SLee Schermerhorn  * Returns a zonelist suitable for a huge page allocation and a pointer
146552cd3b07SLee Schermerhorn  * to the struct mempolicy for conditional unref after allocation.
146652cd3b07SLee Schermerhorn  * If the effective policy is 'BIND, returns a pointer to the mempolicy's
146752cd3b07SLee Schermerhorn  * @nodemask for filtering the zonelist.
1468480eccf9SLee Schermerhorn  */
1469396faf03SMel Gorman struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
147019770b32SMel Gorman 				gfp_t gfp_flags, struct mempolicy **mpol,
147119770b32SMel Gorman 				nodemask_t **nodemask)
14725da7ca86SChristoph Lameter {
1473480eccf9SLee Schermerhorn 	struct zonelist *zl;
14745da7ca86SChristoph Lameter 
147552cd3b07SLee Schermerhorn 	*mpol = get_vma_policy(current, vma, addr);
147619770b32SMel Gorman 	*nodemask = NULL;	/* assume !MPOL_BIND */
14775da7ca86SChristoph Lameter 
147852cd3b07SLee Schermerhorn 	if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
147952cd3b07SLee Schermerhorn 		zl = node_zonelist(interleave_nid(*mpol, vma, addr,
148052cd3b07SLee Schermerhorn 						HPAGE_SHIFT), gfp_flags);
148152cd3b07SLee Schermerhorn 	} else {
148252cd3b07SLee Schermerhorn 		zl = policy_zonelist(gfp_flags, *mpol);
148352cd3b07SLee Schermerhorn 		if ((*mpol)->mode == MPOL_BIND)
148452cd3b07SLee Schermerhorn 			*nodemask = &(*mpol)->v.nodes;
1485480eccf9SLee Schermerhorn 	}
1486480eccf9SLee Schermerhorn 	return zl;
14875da7ca86SChristoph Lameter }
148800ac59adSChen, Kenneth W #endif
14895da7ca86SChristoph Lameter 
14901da177e4SLinus Torvalds /* Allocate a page in interleaved policy.
14911da177e4SLinus Torvalds    Own path because it needs to do special accounting. */
1492662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1493662f3a0bSAndi Kleen 					unsigned nid)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	struct zonelist *zl;
14961da177e4SLinus Torvalds 	struct page *page;
14971da177e4SLinus Torvalds 
14980e88460dSMel Gorman 	zl = node_zonelist(nid, gfp);
14991da177e4SLinus Torvalds 	page = __alloc_pages(gfp, order, zl);
1500dd1a239fSMel Gorman 	if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
1501ca889e6cSChristoph Lameter 		inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
15021da177e4SLinus Torvalds 	return page;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds /**
15061da177e4SLinus Torvalds  * 	alloc_page_vma	- Allocate a page for a VMA.
15071da177e4SLinus Torvalds  *
15081da177e4SLinus Torvalds  * 	@gfp:
15091da177e4SLinus Torvalds  *      %GFP_USER    user allocation.
15101da177e4SLinus Torvalds  *      %GFP_KERNEL  kernel allocations,
15111da177e4SLinus Torvalds  *      %GFP_HIGHMEM highmem/user allocations,
15121da177e4SLinus Torvalds  *      %GFP_FS      allocation should not call back into a file system.
15131da177e4SLinus Torvalds  *      %GFP_ATOMIC  don't sleep.
15141da177e4SLinus Torvalds  *
15151da177e4SLinus Torvalds  * 	@vma:  Pointer to VMA or NULL if not available.
15161da177e4SLinus Torvalds  *	@addr: Virtual Address of the allocation. Must be inside the VMA.
15171da177e4SLinus Torvalds  *
15181da177e4SLinus Torvalds  * 	This function allocates a page from the kernel page pool and applies
15191da177e4SLinus Torvalds  *	a NUMA policy associated with the VMA or the current process.
15201da177e4SLinus Torvalds  *	When VMA is not NULL caller must hold down_read on the mmap_sem of the
15211da177e4SLinus Torvalds  *	mm_struct of the VMA to prevent it from going away. Should be used for
15221da177e4SLinus Torvalds  *	all allocations for pages that will be mapped into
15231da177e4SLinus Torvalds  * 	user space. Returns NULL when no page can be allocated.
15241da177e4SLinus Torvalds  *
15251da177e4SLinus Torvalds  *	Should be called with the mm_sem of the vma hold.
15261da177e4SLinus Torvalds  */
15271da177e4SLinus Torvalds struct page *
1528dd0fc66fSAl Viro alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr)
15291da177e4SLinus Torvalds {
15306e21c8f1SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
1531480eccf9SLee Schermerhorn 	struct zonelist *zl;
15321da177e4SLinus Torvalds 
1533cf2a473cSPaul Jackson 	cpuset_update_task_memory_state();
15341da177e4SLinus Torvalds 
153545c4745aSLee Schermerhorn 	if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
15361da177e4SLinus Torvalds 		unsigned nid;
15375da7ca86SChristoph Lameter 
15385da7ca86SChristoph Lameter 		nid = interleave_nid(pol, vma, addr, PAGE_SHIFT);
153952cd3b07SLee Schermerhorn 		mpol_cond_put(pol);
15401da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, 0, nid);
15411da177e4SLinus Torvalds 	}
154252cd3b07SLee Schermerhorn 	zl = policy_zonelist(gfp, pol);
154352cd3b07SLee Schermerhorn 	if (unlikely(mpol_needs_cond_ref(pol))) {
1544480eccf9SLee Schermerhorn 		/*
154552cd3b07SLee Schermerhorn 		 * slow path: ref counted shared policy
1546480eccf9SLee Schermerhorn 		 */
154719770b32SMel Gorman 		struct page *page =  __alloc_pages_nodemask(gfp, 0,
154852cd3b07SLee Schermerhorn 						zl, policy_nodemask(gfp, pol));
1549f0be3d32SLee Schermerhorn 		__mpol_put(pol);
1550480eccf9SLee Schermerhorn 		return page;
1551480eccf9SLee Schermerhorn 	}
1552480eccf9SLee Schermerhorn 	/*
1553480eccf9SLee Schermerhorn 	 * fast path:  default or task policy
1554480eccf9SLee Schermerhorn 	 */
155552cd3b07SLee Schermerhorn 	return __alloc_pages_nodemask(gfp, 0, zl, policy_nodemask(gfp, pol));
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds /**
15591da177e4SLinus Torvalds  * 	alloc_pages_current - Allocate pages.
15601da177e4SLinus Torvalds  *
15611da177e4SLinus Torvalds  *	@gfp:
15621da177e4SLinus Torvalds  *		%GFP_USER   user allocation,
15631da177e4SLinus Torvalds  *      	%GFP_KERNEL kernel allocation,
15641da177e4SLinus Torvalds  *      	%GFP_HIGHMEM highmem allocation,
15651da177e4SLinus Torvalds  *      	%GFP_FS     don't call back into a file system.
15661da177e4SLinus Torvalds  *      	%GFP_ATOMIC don't sleep.
15671da177e4SLinus Torvalds  *	@order: Power of two of allocation size in pages. 0 is a single page.
15681da177e4SLinus Torvalds  *
15691da177e4SLinus Torvalds  *	Allocate a page from the kernel page pool.  When not in
15701da177e4SLinus Torvalds  *	interrupt context and apply the current process NUMA policy.
15711da177e4SLinus Torvalds  *	Returns NULL when no page can be allocated.
15721da177e4SLinus Torvalds  *
1573cf2a473cSPaul Jackson  *	Don't call cpuset_update_task_memory_state() unless
15741da177e4SLinus Torvalds  *	1) it's ok to take cpuset_sem (can WAIT), and
15751da177e4SLinus Torvalds  *	2) allocating for current task (not interrupt).
15761da177e4SLinus Torvalds  */
1577dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if ((gfp & __GFP_WAIT) && !in_interrupt())
1582cf2a473cSPaul Jackson 		cpuset_update_task_memory_state();
15839b819d20SChristoph Lameter 	if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
15841da177e4SLinus Torvalds 		pol = &default_policy;
158552cd3b07SLee Schermerhorn 
158652cd3b07SLee Schermerhorn 	/*
158752cd3b07SLee Schermerhorn 	 * No reference counting needed for current->mempolicy
158852cd3b07SLee Schermerhorn 	 * nor system default_policy
158952cd3b07SLee Schermerhorn 	 */
159045c4745aSLee Schermerhorn 	if (pol->mode == MPOL_INTERLEAVE)
15911da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, order, interleave_nodes(pol));
159219770b32SMel Gorman 	return __alloc_pages_nodemask(gfp, order,
159352cd3b07SLee Schermerhorn 			policy_zonelist(gfp, pol), policy_nodemask(gfp, pol));
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current);
15961da177e4SLinus Torvalds 
15974225399aSPaul Jackson /*
1598846a16bfSLee Schermerhorn  * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
15994225399aSPaul Jackson  * rebinds the mempolicy its copying by calling mpol_rebind_policy()
16004225399aSPaul Jackson  * with the mems_allowed returned by cpuset_mems_allowed().  This
16014225399aSPaul Jackson  * keeps mempolicies cpuset relative after its cpuset moves.  See
16024225399aSPaul Jackson  * further kernel/cpuset.c update_nodemask().
16034225399aSPaul Jackson  */
16044225399aSPaul Jackson 
1605846a16bfSLee Schermerhorn /* Slow path of a mempolicy duplicate */
1606846a16bfSLee Schermerhorn struct mempolicy *__mpol_dup(struct mempolicy *old)
16071da177e4SLinus Torvalds {
16081da177e4SLinus Torvalds 	struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	if (!new)
16111da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
16124225399aSPaul Jackson 	if (current_cpuset_is_being_rebound()) {
16134225399aSPaul Jackson 		nodemask_t mems = cpuset_mems_allowed(current);
16144225399aSPaul Jackson 		mpol_rebind_policy(old, &mems);
16154225399aSPaul Jackson 	}
16161da177e4SLinus Torvalds 	*new = *old;
16171da177e4SLinus Torvalds 	atomic_set(&new->refcnt, 1);
16181da177e4SLinus Torvalds 	return new;
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds 
162152cd3b07SLee Schermerhorn /*
162252cd3b07SLee Schermerhorn  * If *frompol needs [has] an extra ref, copy *frompol to *tompol ,
162352cd3b07SLee Schermerhorn  * eliminate the * MPOL_F_* flags that require conditional ref and
162452cd3b07SLee Schermerhorn  * [NOTE!!!] drop the extra ref.  Not safe to reference *frompol directly
162552cd3b07SLee Schermerhorn  * after return.  Use the returned value.
162652cd3b07SLee Schermerhorn  *
162752cd3b07SLee Schermerhorn  * Allows use of a mempolicy for, e.g., multiple allocations with a single
162852cd3b07SLee Schermerhorn  * policy lookup, even if the policy needs/has extra ref on lookup.
162952cd3b07SLee Schermerhorn  * shmem_readahead needs this.
163052cd3b07SLee Schermerhorn  */
163152cd3b07SLee Schermerhorn struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
163252cd3b07SLee Schermerhorn 						struct mempolicy *frompol)
163352cd3b07SLee Schermerhorn {
163452cd3b07SLee Schermerhorn 	if (!mpol_needs_cond_ref(frompol))
163552cd3b07SLee Schermerhorn 		return frompol;
163652cd3b07SLee Schermerhorn 
163752cd3b07SLee Schermerhorn 	*tompol = *frompol;
163852cd3b07SLee Schermerhorn 	tompol->flags &= ~MPOL_F_SHARED;	/* copy doesn't need unref */
163952cd3b07SLee Schermerhorn 	__mpol_put(frompol);
164052cd3b07SLee Schermerhorn 	return tompol;
164152cd3b07SLee Schermerhorn }
164252cd3b07SLee Schermerhorn 
1643f5b087b5SDavid Rientjes static int mpol_match_intent(const struct mempolicy *a,
1644f5b087b5SDavid Rientjes 			     const struct mempolicy *b)
1645f5b087b5SDavid Rientjes {
1646f5b087b5SDavid Rientjes 	if (a->flags != b->flags)
1647f5b087b5SDavid Rientjes 		return 0;
1648f5b087b5SDavid Rientjes 	if (!mpol_store_user_nodemask(a))
1649f5b087b5SDavid Rientjes 		return 1;
1650f5b087b5SDavid Rientjes 	return nodes_equal(a->w.user_nodemask, b->w.user_nodemask);
1651f5b087b5SDavid Rientjes }
1652f5b087b5SDavid Rientjes 
16531da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */
16541da177e4SLinus Torvalds int __mpol_equal(struct mempolicy *a, struct mempolicy *b)
16551da177e4SLinus Torvalds {
16561da177e4SLinus Torvalds 	if (!a || !b)
16571da177e4SLinus Torvalds 		return 0;
165845c4745aSLee Schermerhorn 	if (a->mode != b->mode)
16591da177e4SLinus Torvalds 		return 0;
166045c4745aSLee Schermerhorn 	if (a->mode != MPOL_DEFAULT && !mpol_match_intent(a, b))
1661f5b087b5SDavid Rientjes 		return 0;
166245c4745aSLee Schermerhorn 	switch (a->mode) {
166319770b32SMel Gorman 	case MPOL_BIND:
166419770b32SMel Gorman 		/* Fall through */
16651da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
1666dfcd3c0dSAndi Kleen 		return nodes_equal(a->v.nodes, b->v.nodes);
16671da177e4SLinus Torvalds 	case MPOL_PREFERRED:
1668fc36b8d3SLee Schermerhorn 		return a->v.preferred_node == b->v.preferred_node &&
1669fc36b8d3SLee Schermerhorn 			a->flags == b->flags;
16701da177e4SLinus Torvalds 	default:
16711da177e4SLinus Torvalds 		BUG();
16721da177e4SLinus Torvalds 		return 0;
16731da177e4SLinus Torvalds 	}
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds /*
16771da177e4SLinus Torvalds  * Shared memory backing store policy support.
16781da177e4SLinus Torvalds  *
16791da177e4SLinus Torvalds  * Remember policies even when nobody has shared memory mapped.
16801da177e4SLinus Torvalds  * The policies are kept in Red-Black tree linked from the inode.
16811da177e4SLinus Torvalds  * They are protected by the sp->lock spinlock, which should be held
16821da177e4SLinus Torvalds  * for any accesses to the tree.
16831da177e4SLinus Torvalds  */
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds /* lookup first element intersecting start-end */
16861da177e4SLinus Torvalds /* Caller holds sp->lock */
16871da177e4SLinus Torvalds static struct sp_node *
16881da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
16891da177e4SLinus Torvalds {
16901da177e4SLinus Torvalds 	struct rb_node *n = sp->root.rb_node;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	while (n) {
16931da177e4SLinus Torvalds 		struct sp_node *p = rb_entry(n, struct sp_node, nd);
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 		if (start >= p->end)
16961da177e4SLinus Torvalds 			n = n->rb_right;
16971da177e4SLinus Torvalds 		else if (end <= p->start)
16981da177e4SLinus Torvalds 			n = n->rb_left;
16991da177e4SLinus Torvalds 		else
17001da177e4SLinus Torvalds 			break;
17011da177e4SLinus Torvalds 	}
17021da177e4SLinus Torvalds 	if (!n)
17031da177e4SLinus Torvalds 		return NULL;
17041da177e4SLinus Torvalds 	for (;;) {
17051da177e4SLinus Torvalds 		struct sp_node *w = NULL;
17061da177e4SLinus Torvalds 		struct rb_node *prev = rb_prev(n);
17071da177e4SLinus Torvalds 		if (!prev)
17081da177e4SLinus Torvalds 			break;
17091da177e4SLinus Torvalds 		w = rb_entry(prev, struct sp_node, nd);
17101da177e4SLinus Torvalds 		if (w->end <= start)
17111da177e4SLinus Torvalds 			break;
17121da177e4SLinus Torvalds 		n = prev;
17131da177e4SLinus Torvalds 	}
17141da177e4SLinus Torvalds 	return rb_entry(n, struct sp_node, nd);
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds /* Insert a new shared policy into the list. */
17181da177e4SLinus Torvalds /* Caller holds sp->lock */
17191da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new)
17201da177e4SLinus Torvalds {
17211da177e4SLinus Torvalds 	struct rb_node **p = &sp->root.rb_node;
17221da177e4SLinus Torvalds 	struct rb_node *parent = NULL;
17231da177e4SLinus Torvalds 	struct sp_node *nd;
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	while (*p) {
17261da177e4SLinus Torvalds 		parent = *p;
17271da177e4SLinus Torvalds 		nd = rb_entry(parent, struct sp_node, nd);
17281da177e4SLinus Torvalds 		if (new->start < nd->start)
17291da177e4SLinus Torvalds 			p = &(*p)->rb_left;
17301da177e4SLinus Torvalds 		else if (new->end > nd->end)
17311da177e4SLinus Torvalds 			p = &(*p)->rb_right;
17321da177e4SLinus Torvalds 		else
17331da177e4SLinus Torvalds 			BUG();
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds 	rb_link_node(&new->nd, parent, p);
17361da177e4SLinus Torvalds 	rb_insert_color(&new->nd, &sp->root);
1737140d5a49SPaul Mundt 	pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
173845c4745aSLee Schermerhorn 		 new->policy ? new->policy->mode : 0);
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /* Find shared policy intersecting idx */
17421da177e4SLinus Torvalds struct mempolicy *
17431da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
17441da177e4SLinus Torvalds {
17451da177e4SLinus Torvalds 	struct mempolicy *pol = NULL;
17461da177e4SLinus Torvalds 	struct sp_node *sn;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	if (!sp->root.rb_node)
17491da177e4SLinus Torvalds 		return NULL;
17501da177e4SLinus Torvalds 	spin_lock(&sp->lock);
17511da177e4SLinus Torvalds 	sn = sp_lookup(sp, idx, idx+1);
17521da177e4SLinus Torvalds 	if (sn) {
17531da177e4SLinus Torvalds 		mpol_get(sn->policy);
17541da177e4SLinus Torvalds 		pol = sn->policy;
17551da177e4SLinus Torvalds 	}
17561da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
17571da177e4SLinus Torvalds 	return pol;
17581da177e4SLinus Torvalds }
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n)
17611da177e4SLinus Torvalds {
1762140d5a49SPaul Mundt 	pr_debug("deleting %lx-l%lx\n", n->start, n->end);
17631da177e4SLinus Torvalds 	rb_erase(&n->nd, &sp->root);
1764f0be3d32SLee Schermerhorn 	mpol_put(n->policy);
17651da177e4SLinus Torvalds 	kmem_cache_free(sn_cache, n);
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
1768dbcb0f19SAdrian Bunk static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
1769dbcb0f19SAdrian Bunk 				struct mempolicy *pol)
17701da177e4SLinus Torvalds {
17711da177e4SLinus Torvalds 	struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	if (!n)
17741da177e4SLinus Torvalds 		return NULL;
17751da177e4SLinus Torvalds 	n->start = start;
17761da177e4SLinus Torvalds 	n->end = end;
17771da177e4SLinus Torvalds 	mpol_get(pol);
1778aab0b102SLee Schermerhorn 	pol->flags |= MPOL_F_SHARED;	/* for unref */
17791da177e4SLinus Torvalds 	n->policy = pol;
17801da177e4SLinus Torvalds 	return n;
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds /* Replace a policy range. */
17841da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
17851da177e4SLinus Torvalds 				 unsigned long end, struct sp_node *new)
17861da177e4SLinus Torvalds {
17871da177e4SLinus Torvalds 	struct sp_node *n, *new2 = NULL;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds restart:
17901da177e4SLinus Torvalds 	spin_lock(&sp->lock);
17911da177e4SLinus Torvalds 	n = sp_lookup(sp, start, end);
17921da177e4SLinus Torvalds 	/* Take care of old policies in the same range. */
17931da177e4SLinus Torvalds 	while (n && n->start < end) {
17941da177e4SLinus Torvalds 		struct rb_node *next = rb_next(&n->nd);
17951da177e4SLinus Torvalds 		if (n->start >= start) {
17961da177e4SLinus Torvalds 			if (n->end <= end)
17971da177e4SLinus Torvalds 				sp_delete(sp, n);
17981da177e4SLinus Torvalds 			else
17991da177e4SLinus Torvalds 				n->start = end;
18001da177e4SLinus Torvalds 		} else {
18011da177e4SLinus Torvalds 			/* Old policy spanning whole new range. */
18021da177e4SLinus Torvalds 			if (n->end > end) {
18031da177e4SLinus Torvalds 				if (!new2) {
18041da177e4SLinus Torvalds 					spin_unlock(&sp->lock);
18051da177e4SLinus Torvalds 					new2 = sp_alloc(end, n->end, n->policy);
18061da177e4SLinus Torvalds 					if (!new2)
18071da177e4SLinus Torvalds 						return -ENOMEM;
18081da177e4SLinus Torvalds 					goto restart;
18091da177e4SLinus Torvalds 				}
18101da177e4SLinus Torvalds 				n->end = start;
18111da177e4SLinus Torvalds 				sp_insert(sp, new2);
18121da177e4SLinus Torvalds 				new2 = NULL;
18131da177e4SLinus Torvalds 				break;
18141da177e4SLinus Torvalds 			} else
18151da177e4SLinus Torvalds 				n->end = start;
18161da177e4SLinus Torvalds 		}
18171da177e4SLinus Torvalds 		if (!next)
18181da177e4SLinus Torvalds 			break;
18191da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 	if (new)
18221da177e4SLinus Torvalds 		sp_insert(sp, new);
18231da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
18241da177e4SLinus Torvalds 	if (new2) {
1825f0be3d32SLee Schermerhorn 		mpol_put(new2->policy);
18261da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new2);
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 	return 0;
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
1831*71fe804bSLee Schermerhorn /**
1832*71fe804bSLee Schermerhorn  * mpol_shared_policy_init - initialize shared policy for inode
1833*71fe804bSLee Schermerhorn  * @sp: pointer to inode shared policy
1834*71fe804bSLee Schermerhorn  * @mpol:  struct mempolicy to install
1835*71fe804bSLee Schermerhorn  *
1836*71fe804bSLee Schermerhorn  * Install non-NULL @mpol in inode's shared policy rb-tree.
1837*71fe804bSLee Schermerhorn  * On entry, the current task has a reference on a non-NULL @mpol.
1838*71fe804bSLee Schermerhorn  * This must be released on exit.
1839*71fe804bSLee Schermerhorn  */
1840*71fe804bSLee Schermerhorn void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
18417339ff83SRobin Holt {
1842*71fe804bSLee Schermerhorn 	sp->root = RB_ROOT;		/* empty tree == default mempolicy */
1843*71fe804bSLee Schermerhorn 	spin_lock_init(&sp->lock);
18447339ff83SRobin Holt 
1845*71fe804bSLee Schermerhorn 	if (mpol) {
18467339ff83SRobin Holt 		struct vm_area_struct pvma;
1847*71fe804bSLee Schermerhorn 		struct mempolicy *new;
18487339ff83SRobin Holt 
1849*71fe804bSLee Schermerhorn 		/* contextualize the tmpfs mount point mempolicy */
1850*71fe804bSLee Schermerhorn 		new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
1851*71fe804bSLee Schermerhorn 		mpol_put(mpol);	/* drop our ref on sb mpol */
1852*71fe804bSLee Schermerhorn 		if (IS_ERR(new))
1853*71fe804bSLee Schermerhorn 			return;		/* no valid nodemask intersection */
1854*71fe804bSLee Schermerhorn 
1855*71fe804bSLee Schermerhorn 		/* Create pseudo-vma that contains just the policy */
18567339ff83SRobin Holt 		memset(&pvma, 0, sizeof(struct vm_area_struct));
1857*71fe804bSLee Schermerhorn 		pvma.vm_end = TASK_SIZE;	/* policy covers entire file */
1858*71fe804bSLee Schermerhorn 		mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
1859*71fe804bSLee Schermerhorn 		mpol_put(new);			/* drop initial ref */
18607339ff83SRobin Holt 	}
18617339ff83SRobin Holt }
18627339ff83SRobin Holt 
18631da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info,
18641da177e4SLinus Torvalds 			struct vm_area_struct *vma, struct mempolicy *npol)
18651da177e4SLinus Torvalds {
18661da177e4SLinus Torvalds 	int err;
18671da177e4SLinus Torvalds 	struct sp_node *new = NULL;
18681da177e4SLinus Torvalds 	unsigned long sz = vma_pages(vma);
18691da177e4SLinus Torvalds 
1870028fec41SDavid Rientjes 	pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
18711da177e4SLinus Torvalds 		 vma->vm_pgoff,
187245c4745aSLee Schermerhorn 		 sz, npol ? npol->mode : -1,
1873028fec41SDavid Rientjes 		 npol ? npol->flags : -1,
1874dfcd3c0dSAndi Kleen 		 npol ? nodes_addr(npol->v.nodes)[0] : -1);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	if (npol) {
18771da177e4SLinus Torvalds 		new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
18781da177e4SLinus Torvalds 		if (!new)
18791da177e4SLinus Torvalds 			return -ENOMEM;
18801da177e4SLinus Torvalds 	}
18811da177e4SLinus Torvalds 	err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
18821da177e4SLinus Torvalds 	if (err && new)
18831da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new);
18841da177e4SLinus Torvalds 	return err;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */
18881da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	struct sp_node *n;
18911da177e4SLinus Torvalds 	struct rb_node *next;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	if (!p->root.rb_node)
18941da177e4SLinus Torvalds 		return;
18951da177e4SLinus Torvalds 	spin_lock(&p->lock);
18961da177e4SLinus Torvalds 	next = rb_first(&p->root);
18971da177e4SLinus Torvalds 	while (next) {
18981da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
18991da177e4SLinus Torvalds 		next = rb_next(&n->nd);
190090c5029eSAndi Kleen 		rb_erase(&n->nd, &p->root);
1901f0be3d32SLee Schermerhorn 		mpol_put(n->policy);
19021da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, n);
19031da177e4SLinus Torvalds 	}
19041da177e4SLinus Torvalds 	spin_unlock(&p->lock);
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */
19081da177e4SLinus Torvalds void __init numa_policy_init(void)
19091da177e4SLinus Torvalds {
1910b71636e2SPaul Mundt 	nodemask_t interleave_nodes;
1911b71636e2SPaul Mundt 	unsigned long largest = 0;
1912b71636e2SPaul Mundt 	int nid, prefer = 0;
1913b71636e2SPaul Mundt 
19141da177e4SLinus Torvalds 	policy_cache = kmem_cache_create("numa_policy",
19151da177e4SLinus Torvalds 					 sizeof(struct mempolicy),
191620c2df83SPaul Mundt 					 0, SLAB_PANIC, NULL);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	sn_cache = kmem_cache_create("shared_policy_node",
19191da177e4SLinus Torvalds 				     sizeof(struct sp_node),
192020c2df83SPaul Mundt 				     0, SLAB_PANIC, NULL);
19211da177e4SLinus Torvalds 
1922b71636e2SPaul Mundt 	/*
1923b71636e2SPaul Mundt 	 * Set interleaving policy for system init. Interleaving is only
1924b71636e2SPaul Mundt 	 * enabled across suitably sized nodes (default is >= 16MB), or
1925b71636e2SPaul Mundt 	 * fall back to the largest node if they're all smaller.
1926b71636e2SPaul Mundt 	 */
1927b71636e2SPaul Mundt 	nodes_clear(interleave_nodes);
192856bbd65dSChristoph Lameter 	for_each_node_state(nid, N_HIGH_MEMORY) {
1929b71636e2SPaul Mundt 		unsigned long total_pages = node_present_pages(nid);
19301da177e4SLinus Torvalds 
1931b71636e2SPaul Mundt 		/* Preserve the largest node */
1932b71636e2SPaul Mundt 		if (largest < total_pages) {
1933b71636e2SPaul Mundt 			largest = total_pages;
1934b71636e2SPaul Mundt 			prefer = nid;
1935b71636e2SPaul Mundt 		}
1936b71636e2SPaul Mundt 
1937b71636e2SPaul Mundt 		/* Interleave this node? */
1938b71636e2SPaul Mundt 		if ((total_pages << PAGE_SHIFT) >= (16 << 20))
1939b71636e2SPaul Mundt 			node_set(nid, interleave_nodes);
1940b71636e2SPaul Mundt 	}
1941b71636e2SPaul Mundt 
1942b71636e2SPaul Mundt 	/* All too small, use the largest */
1943b71636e2SPaul Mundt 	if (unlikely(nodes_empty(interleave_nodes)))
1944b71636e2SPaul Mundt 		node_set(prefer, interleave_nodes);
1945b71636e2SPaul Mundt 
1946028fec41SDavid Rientjes 	if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
19471da177e4SLinus Torvalds 		printk("numa_policy_init: interleaving failed\n");
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
19508bccd85fSChristoph Lameter /* Reset policy of current process to default */
19511da177e4SLinus Torvalds void numa_default_policy(void)
19521da177e4SLinus Torvalds {
1953028fec41SDavid Rientjes 	do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
19541da177e4SLinus Torvalds }
195568860ec1SPaul Jackson 
19564225399aSPaul Jackson /*
1957095f1fc4SLee Schermerhorn  * Parse and format mempolicy from/to strings
1958095f1fc4SLee Schermerhorn  */
1959095f1fc4SLee Schermerhorn 
1960095f1fc4SLee Schermerhorn /*
1961fc36b8d3SLee Schermerhorn  * "local" is pseudo-policy:  MPOL_PREFERRED with MPOL_F_LOCAL flag
19623f226aa1SLee Schermerhorn  * Used only for mpol_parse_str() and mpol_to_str()
19631a75a6c8SChristoph Lameter  */
196453f2556bSLee Schermerhorn #define MPOL_LOCAL (MPOL_INTERLEAVE + 1)
196515ad7cdcSHelge Deller static const char * const policy_types[] =
196653f2556bSLee Schermerhorn 	{ "default", "prefer", "bind", "interleave", "local" };
19671a75a6c8SChristoph Lameter 
1968095f1fc4SLee Schermerhorn 
1969095f1fc4SLee Schermerhorn #ifdef CONFIG_TMPFS
1970095f1fc4SLee Schermerhorn /**
1971095f1fc4SLee Schermerhorn  * mpol_parse_str - parse string to mempolicy
1972095f1fc4SLee Schermerhorn  * @str:  string containing mempolicy to parse
1973*71fe804bSLee Schermerhorn  * @mpol:  pointer to struct mempolicy pointer, returned on success.
1974*71fe804bSLee Schermerhorn  * @no_context:  flag whether to "contextualize" the mempolicy
1975095f1fc4SLee Schermerhorn  *
1976095f1fc4SLee Schermerhorn  * Format of input:
1977095f1fc4SLee Schermerhorn  *	<mode>[=<flags>][:<nodelist>]
1978095f1fc4SLee Schermerhorn  *
1979*71fe804bSLee Schermerhorn  * if @no_context is true, save the input nodemask in w.user_nodemask in
1980*71fe804bSLee Schermerhorn  * the returned mempolicy.  This will be used to "clone" the mempolicy in
1981*71fe804bSLee Schermerhorn  * a specific context [cpuset] at a later time.  Used to parse tmpfs mpol
1982*71fe804bSLee Schermerhorn  * mount option.  Note that if 'static' or 'relative' mode flags were
1983*71fe804bSLee Schermerhorn  * specified, the input nodemask will already have been saved.  Saving
1984*71fe804bSLee Schermerhorn  * it again is redundant, but safe.
1985*71fe804bSLee Schermerhorn  *
1986*71fe804bSLee Schermerhorn  * On success, returns 0, else 1
1987095f1fc4SLee Schermerhorn  */
1988*71fe804bSLee Schermerhorn int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context)
1989095f1fc4SLee Schermerhorn {
1990*71fe804bSLee Schermerhorn 	struct mempolicy *new = NULL;
1991*71fe804bSLee Schermerhorn 	unsigned short uninitialized_var(mode);
1992*71fe804bSLee Schermerhorn 	unsigned short uninitialized_var(mode_flags);
1993*71fe804bSLee Schermerhorn 	nodemask_t nodes;
1994095f1fc4SLee Schermerhorn 	char *nodelist = strchr(str, ':');
1995095f1fc4SLee Schermerhorn 	char *flags = strchr(str, '=');
1996095f1fc4SLee Schermerhorn 	int i;
1997095f1fc4SLee Schermerhorn 	int err = 1;
1998095f1fc4SLee Schermerhorn 
1999095f1fc4SLee Schermerhorn 	if (nodelist) {
2000095f1fc4SLee Schermerhorn 		/* NUL-terminate mode or flags string */
2001095f1fc4SLee Schermerhorn 		*nodelist++ = '\0';
2002*71fe804bSLee Schermerhorn 		if (nodelist_parse(nodelist, nodes))
2003095f1fc4SLee Schermerhorn 			goto out;
2004*71fe804bSLee Schermerhorn 		if (!nodes_subset(nodes, node_states[N_HIGH_MEMORY]))
2005095f1fc4SLee Schermerhorn 			goto out;
2006*71fe804bSLee Schermerhorn 	} else
2007*71fe804bSLee Schermerhorn 		nodes_clear(nodes);
2008*71fe804bSLee Schermerhorn 
2009095f1fc4SLee Schermerhorn 	if (flags)
2010095f1fc4SLee Schermerhorn 		*flags++ = '\0';	/* terminate mode string */
2011095f1fc4SLee Schermerhorn 
20123f226aa1SLee Schermerhorn 	for (i = 0; i <= MPOL_LOCAL; i++) {
2013095f1fc4SLee Schermerhorn 		if (!strcmp(str, policy_types[i])) {
2014*71fe804bSLee Schermerhorn 			mode = i;
2015095f1fc4SLee Schermerhorn 			break;
2016095f1fc4SLee Schermerhorn 		}
2017095f1fc4SLee Schermerhorn 	}
20183f226aa1SLee Schermerhorn 	if (i > MPOL_LOCAL)
2019095f1fc4SLee Schermerhorn 		goto out;
2020095f1fc4SLee Schermerhorn 
2021*71fe804bSLee Schermerhorn 	switch (mode) {
2022095f1fc4SLee Schermerhorn 	case MPOL_PREFERRED:
2023*71fe804bSLee Schermerhorn 		/*
2024*71fe804bSLee Schermerhorn 		 * Insist on a nodelist of one node only
2025*71fe804bSLee Schermerhorn 		 */
2026095f1fc4SLee Schermerhorn 		if (nodelist) {
2027095f1fc4SLee Schermerhorn 			char *rest = nodelist;
2028095f1fc4SLee Schermerhorn 			while (isdigit(*rest))
2029095f1fc4SLee Schermerhorn 				rest++;
2030095f1fc4SLee Schermerhorn 			if (!*rest)
2031095f1fc4SLee Schermerhorn 				err = 0;
2032095f1fc4SLee Schermerhorn 		}
2033095f1fc4SLee Schermerhorn 		break;
2034095f1fc4SLee Schermerhorn 	case MPOL_INTERLEAVE:
2035095f1fc4SLee Schermerhorn 		/*
2036095f1fc4SLee Schermerhorn 		 * Default to online nodes with memory if no nodelist
2037095f1fc4SLee Schermerhorn 		 */
2038095f1fc4SLee Schermerhorn 		if (!nodelist)
2039*71fe804bSLee Schermerhorn 			nodes = node_states[N_HIGH_MEMORY];
2040095f1fc4SLee Schermerhorn 		err = 0;
20413f226aa1SLee Schermerhorn 		break;
2042*71fe804bSLee Schermerhorn 	case MPOL_LOCAL:
20433f226aa1SLee Schermerhorn 		/*
2044*71fe804bSLee Schermerhorn 		 * Don't allow a nodelist;  mpol_new() checks flags
20453f226aa1SLee Schermerhorn 		 */
2046*71fe804bSLee Schermerhorn 		if (nodelist)
20473f226aa1SLee Schermerhorn 			goto out;
2048*71fe804bSLee Schermerhorn 		mode = MPOL_PREFERRED;
20493f226aa1SLee Schermerhorn 		break;
2050*71fe804bSLee Schermerhorn 
2051*71fe804bSLee Schermerhorn 	/*
2052*71fe804bSLee Schermerhorn 	 * case MPOL_BIND:    mpol_new() enforces non-empty nodemask.
2053*71fe804bSLee Schermerhorn 	 * case MPOL_DEFAULT: mpol_new() enforces empty nodemask, ignores flags.
2054*71fe804bSLee Schermerhorn 	 */
2055095f1fc4SLee Schermerhorn 	}
2056095f1fc4SLee Schermerhorn 
2057*71fe804bSLee Schermerhorn 	mode_flags = 0;
2058095f1fc4SLee Schermerhorn 	if (flags) {
2059095f1fc4SLee Schermerhorn 		/*
2060095f1fc4SLee Schermerhorn 		 * Currently, we only support two mutually exclusive
2061095f1fc4SLee Schermerhorn 		 * mode flags.
2062095f1fc4SLee Schermerhorn 		 */
2063095f1fc4SLee Schermerhorn 		if (!strcmp(flags, "static"))
2064*71fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_STATIC_NODES;
2065095f1fc4SLee Schermerhorn 		else if (!strcmp(flags, "relative"))
2066*71fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_RELATIVE_NODES;
2067095f1fc4SLee Schermerhorn 		else
2068095f1fc4SLee Schermerhorn 			err = 1;
2069095f1fc4SLee Schermerhorn 	}
2070*71fe804bSLee Schermerhorn 
2071*71fe804bSLee Schermerhorn 	new = mpol_new(mode, mode_flags, &nodes);
2072*71fe804bSLee Schermerhorn 	if (IS_ERR(new))
2073*71fe804bSLee Schermerhorn 		err = 1;
2074*71fe804bSLee Schermerhorn 	else if (no_context)
2075*71fe804bSLee Schermerhorn 		new->w.user_nodemask = nodes;	/* save for contextualization */
2076*71fe804bSLee Schermerhorn 
2077095f1fc4SLee Schermerhorn out:
2078095f1fc4SLee Schermerhorn 	/* Restore string for error message */
2079095f1fc4SLee Schermerhorn 	if (nodelist)
2080095f1fc4SLee Schermerhorn 		*--nodelist = ':';
2081095f1fc4SLee Schermerhorn 	if (flags)
2082095f1fc4SLee Schermerhorn 		*--flags = '=';
2083*71fe804bSLee Schermerhorn 	if (!err)
2084*71fe804bSLee Schermerhorn 		*mpol = new;
2085095f1fc4SLee Schermerhorn 	return err;
2086095f1fc4SLee Schermerhorn }
2087095f1fc4SLee Schermerhorn #endif /* CONFIG_TMPFS */
2088095f1fc4SLee Schermerhorn 
2089*71fe804bSLee Schermerhorn /**
2090*71fe804bSLee Schermerhorn  * mpol_to_str - format a mempolicy structure for printing
2091*71fe804bSLee Schermerhorn  * @buffer:  to contain formatted mempolicy string
2092*71fe804bSLee Schermerhorn  * @maxlen:  length of @buffer
2093*71fe804bSLee Schermerhorn  * @pol:  pointer to mempolicy to be formatted
2094*71fe804bSLee Schermerhorn  * @no_context:  "context free" mempolicy - use nodemask in w.user_nodemask
2095*71fe804bSLee Schermerhorn  *
20961a75a6c8SChristoph Lameter  * Convert a mempolicy into a string.
20971a75a6c8SChristoph Lameter  * Returns the number of characters in buffer (if positive)
20981a75a6c8SChristoph Lameter  * or an error (negative)
20991a75a6c8SChristoph Lameter  */
2100*71fe804bSLee Schermerhorn int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol, int no_context)
21011a75a6c8SChristoph Lameter {
21021a75a6c8SChristoph Lameter 	char *p = buffer;
21031a75a6c8SChristoph Lameter 	int l;
21041a75a6c8SChristoph Lameter 	nodemask_t nodes;
2105bea904d5SLee Schermerhorn 	unsigned short mode;
2106f5b087b5SDavid Rientjes 	unsigned short flags = pol ? pol->flags : 0;
21071a75a6c8SChristoph Lameter 
21082291990aSLee Schermerhorn 	/*
21092291990aSLee Schermerhorn 	 * Sanity check:  room for longest mode, flag and some nodes
21102291990aSLee Schermerhorn 	 */
21112291990aSLee Schermerhorn 	VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16);
21122291990aSLee Schermerhorn 
2113bea904d5SLee Schermerhorn 	if (!pol || pol == &default_policy)
2114bea904d5SLee Schermerhorn 		mode = MPOL_DEFAULT;
2115bea904d5SLee Schermerhorn 	else
2116bea904d5SLee Schermerhorn 		mode = pol->mode;
2117bea904d5SLee Schermerhorn 
21181a75a6c8SChristoph Lameter 	switch (mode) {
21191a75a6c8SChristoph Lameter 	case MPOL_DEFAULT:
21201a75a6c8SChristoph Lameter 		nodes_clear(nodes);
21211a75a6c8SChristoph Lameter 		break;
21221a75a6c8SChristoph Lameter 
21231a75a6c8SChristoph Lameter 	case MPOL_PREFERRED:
21241a75a6c8SChristoph Lameter 		nodes_clear(nodes);
2125fc36b8d3SLee Schermerhorn 		if (flags & MPOL_F_LOCAL)
212653f2556bSLee Schermerhorn 			mode = MPOL_LOCAL;	/* pseudo-policy */
212753f2556bSLee Schermerhorn 		else
2128fc36b8d3SLee Schermerhorn 			node_set(pol->v.preferred_node, nodes);
21291a75a6c8SChristoph Lameter 		break;
21301a75a6c8SChristoph Lameter 
21311a75a6c8SChristoph Lameter 	case MPOL_BIND:
213219770b32SMel Gorman 		/* Fall through */
21331a75a6c8SChristoph Lameter 	case MPOL_INTERLEAVE:
2134*71fe804bSLee Schermerhorn 		if (no_context)
2135*71fe804bSLee Schermerhorn 			nodes = pol->w.user_nodemask;
2136*71fe804bSLee Schermerhorn 		else
21371a75a6c8SChristoph Lameter 			nodes = pol->v.nodes;
21381a75a6c8SChristoph Lameter 		break;
21391a75a6c8SChristoph Lameter 
21401a75a6c8SChristoph Lameter 	default:
21411a75a6c8SChristoph Lameter 		BUG();
21421a75a6c8SChristoph Lameter 	}
21431a75a6c8SChristoph Lameter 
21441a75a6c8SChristoph Lameter 	l = strlen(policy_types[mode]);
21451a75a6c8SChristoph Lameter 	if (buffer + maxlen < p + l + 1)
21461a75a6c8SChristoph Lameter 		return -ENOSPC;
21471a75a6c8SChristoph Lameter 
21481a75a6c8SChristoph Lameter 	strcpy(p, policy_types[mode]);
21491a75a6c8SChristoph Lameter 	p += l;
21501a75a6c8SChristoph Lameter 
2151fc36b8d3SLee Schermerhorn 	if (flags & MPOL_MODE_FLAGS) {
2152f5b087b5SDavid Rientjes 		if (buffer + maxlen < p + 2)
2153f5b087b5SDavid Rientjes 			return -ENOSPC;
2154f5b087b5SDavid Rientjes 		*p++ = '=';
2155f5b087b5SDavid Rientjes 
21562291990aSLee Schermerhorn 		/*
21572291990aSLee Schermerhorn 		 * Currently, the only defined flags are mutually exclusive
21582291990aSLee Schermerhorn 		 */
2159f5b087b5SDavid Rientjes 		if (flags & MPOL_F_STATIC_NODES)
21602291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "static");
21612291990aSLee Schermerhorn 		else if (flags & MPOL_F_RELATIVE_NODES)
21622291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "relative");
2163f5b087b5SDavid Rientjes 	}
2164f5b087b5SDavid Rientjes 
21651a75a6c8SChristoph Lameter 	if (!nodes_empty(nodes)) {
21661a75a6c8SChristoph Lameter 		if (buffer + maxlen < p + 2)
21671a75a6c8SChristoph Lameter 			return -ENOSPC;
2168095f1fc4SLee Schermerhorn 		*p++ = ':';
21691a75a6c8SChristoph Lameter 	 	p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
21701a75a6c8SChristoph Lameter 	}
21711a75a6c8SChristoph Lameter 	return p - buffer;
21721a75a6c8SChristoph Lameter }
21731a75a6c8SChristoph Lameter 
21741a75a6c8SChristoph Lameter struct numa_maps {
21751a75a6c8SChristoph Lameter 	unsigned long pages;
21761a75a6c8SChristoph Lameter 	unsigned long anon;
2177397874dfSChristoph Lameter 	unsigned long active;
2178397874dfSChristoph Lameter 	unsigned long writeback;
21791a75a6c8SChristoph Lameter 	unsigned long mapcount_max;
2180397874dfSChristoph Lameter 	unsigned long dirty;
2181397874dfSChristoph Lameter 	unsigned long swapcache;
21821a75a6c8SChristoph Lameter 	unsigned long node[MAX_NUMNODES];
21831a75a6c8SChristoph Lameter };
21841a75a6c8SChristoph Lameter 
2185397874dfSChristoph Lameter static void gather_stats(struct page *page, void *private, int pte_dirty)
21861a75a6c8SChristoph Lameter {
21871a75a6c8SChristoph Lameter 	struct numa_maps *md = private;
21881a75a6c8SChristoph Lameter 	int count = page_mapcount(page);
21891a75a6c8SChristoph Lameter 
21901a75a6c8SChristoph Lameter 	md->pages++;
2191397874dfSChristoph Lameter 	if (pte_dirty || PageDirty(page))
2192397874dfSChristoph Lameter 		md->dirty++;
2193397874dfSChristoph Lameter 
2194397874dfSChristoph Lameter 	if (PageSwapCache(page))
2195397874dfSChristoph Lameter 		md->swapcache++;
2196397874dfSChristoph Lameter 
2197397874dfSChristoph Lameter 	if (PageActive(page))
2198397874dfSChristoph Lameter 		md->active++;
2199397874dfSChristoph Lameter 
2200397874dfSChristoph Lameter 	if (PageWriteback(page))
2201397874dfSChristoph Lameter 		md->writeback++;
22021a75a6c8SChristoph Lameter 
22031a75a6c8SChristoph Lameter 	if (PageAnon(page))
22041a75a6c8SChristoph Lameter 		md->anon++;
22051a75a6c8SChristoph Lameter 
2206397874dfSChristoph Lameter 	if (count > md->mapcount_max)
2207397874dfSChristoph Lameter 		md->mapcount_max = count;
2208397874dfSChristoph Lameter 
22091a75a6c8SChristoph Lameter 	md->node[page_to_nid(page)]++;
22101a75a6c8SChristoph Lameter }
22111a75a6c8SChristoph Lameter 
22127f709ed0SAndrew Morton #ifdef CONFIG_HUGETLB_PAGE
2213397874dfSChristoph Lameter static void check_huge_range(struct vm_area_struct *vma,
2214397874dfSChristoph Lameter 		unsigned long start, unsigned long end,
2215397874dfSChristoph Lameter 		struct numa_maps *md)
2216397874dfSChristoph Lameter {
2217397874dfSChristoph Lameter 	unsigned long addr;
2218397874dfSChristoph Lameter 	struct page *page;
2219397874dfSChristoph Lameter 
2220397874dfSChristoph Lameter 	for (addr = start; addr < end; addr += HPAGE_SIZE) {
2221397874dfSChristoph Lameter 		pte_t *ptep = huge_pte_offset(vma->vm_mm, addr & HPAGE_MASK);
2222397874dfSChristoph Lameter 		pte_t pte;
2223397874dfSChristoph Lameter 
2224397874dfSChristoph Lameter 		if (!ptep)
2225397874dfSChristoph Lameter 			continue;
2226397874dfSChristoph Lameter 
2227397874dfSChristoph Lameter 		pte = *ptep;
2228397874dfSChristoph Lameter 		if (pte_none(pte))
2229397874dfSChristoph Lameter 			continue;
2230397874dfSChristoph Lameter 
2231397874dfSChristoph Lameter 		page = pte_page(pte);
2232397874dfSChristoph Lameter 		if (!page)
2233397874dfSChristoph Lameter 			continue;
2234397874dfSChristoph Lameter 
2235397874dfSChristoph Lameter 		gather_stats(page, md, pte_dirty(*ptep));
2236397874dfSChristoph Lameter 	}
2237397874dfSChristoph Lameter }
22387f709ed0SAndrew Morton #else
22397f709ed0SAndrew Morton static inline void check_huge_range(struct vm_area_struct *vma,
22407f709ed0SAndrew Morton 		unsigned long start, unsigned long end,
22417f709ed0SAndrew Morton 		struct numa_maps *md)
22427f709ed0SAndrew Morton {
22437f709ed0SAndrew Morton }
22447f709ed0SAndrew Morton #endif
2245397874dfSChristoph Lameter 
224653f2556bSLee Schermerhorn /*
224753f2556bSLee Schermerhorn  * Display pages allocated per node and memory policy via /proc.
224853f2556bSLee Schermerhorn  */
22491a75a6c8SChristoph Lameter int show_numa_map(struct seq_file *m, void *v)
22501a75a6c8SChristoph Lameter {
225199f89551SEric W. Biederman 	struct proc_maps_private *priv = m->private;
22521a75a6c8SChristoph Lameter 	struct vm_area_struct *vma = v;
22531a75a6c8SChristoph Lameter 	struct numa_maps *md;
2254397874dfSChristoph Lameter 	struct file *file = vma->vm_file;
2255397874dfSChristoph Lameter 	struct mm_struct *mm = vma->vm_mm;
2256480eccf9SLee Schermerhorn 	struct mempolicy *pol;
22571a75a6c8SChristoph Lameter 	int n;
22581a75a6c8SChristoph Lameter 	char buffer[50];
22591a75a6c8SChristoph Lameter 
2260397874dfSChristoph Lameter 	if (!mm)
22611a75a6c8SChristoph Lameter 		return 0;
22621a75a6c8SChristoph Lameter 
22631a75a6c8SChristoph Lameter 	md = kzalloc(sizeof(struct numa_maps), GFP_KERNEL);
22641a75a6c8SChristoph Lameter 	if (!md)
22651a75a6c8SChristoph Lameter 		return 0;
22661a75a6c8SChristoph Lameter 
2267480eccf9SLee Schermerhorn 	pol = get_vma_policy(priv->task, vma, vma->vm_start);
2268*71fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), pol, 0);
226952cd3b07SLee Schermerhorn 	mpol_cond_put(pol);
22701a75a6c8SChristoph Lameter 
2271397874dfSChristoph Lameter 	seq_printf(m, "%08lx %s", vma->vm_start, buffer);
2272397874dfSChristoph Lameter 
2273397874dfSChristoph Lameter 	if (file) {
2274397874dfSChristoph Lameter 		seq_printf(m, " file=");
2275c32c2f63SJan Blunck 		seq_path(m, &file->f_path, "\n\t= ");
2276397874dfSChristoph Lameter 	} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
2277397874dfSChristoph Lameter 		seq_printf(m, " heap");
2278397874dfSChristoph Lameter 	} else if (vma->vm_start <= mm->start_stack &&
2279397874dfSChristoph Lameter 			vma->vm_end >= mm->start_stack) {
2280397874dfSChristoph Lameter 		seq_printf(m, " stack");
2281397874dfSChristoph Lameter 	}
2282397874dfSChristoph Lameter 
2283397874dfSChristoph Lameter 	if (is_vm_hugetlb_page(vma)) {
2284397874dfSChristoph Lameter 		check_huge_range(vma, vma->vm_start, vma->vm_end, md);
2285397874dfSChristoph Lameter 		seq_printf(m, " huge");
2286397874dfSChristoph Lameter 	} else {
2287397874dfSChristoph Lameter 		check_pgd_range(vma, vma->vm_start, vma->vm_end,
228856bbd65dSChristoph Lameter 			&node_states[N_HIGH_MEMORY], MPOL_MF_STATS, md);
2289397874dfSChristoph Lameter 	}
2290397874dfSChristoph Lameter 
2291397874dfSChristoph Lameter 	if (!md->pages)
2292397874dfSChristoph Lameter 		goto out;
22931a75a6c8SChristoph Lameter 
22941a75a6c8SChristoph Lameter 	if (md->anon)
22951a75a6c8SChristoph Lameter 		seq_printf(m," anon=%lu",md->anon);
22961a75a6c8SChristoph Lameter 
2297397874dfSChristoph Lameter 	if (md->dirty)
2298397874dfSChristoph Lameter 		seq_printf(m," dirty=%lu",md->dirty);
2299397874dfSChristoph Lameter 
2300397874dfSChristoph Lameter 	if (md->pages != md->anon && md->pages != md->dirty)
2301397874dfSChristoph Lameter 		seq_printf(m, " mapped=%lu", md->pages);
2302397874dfSChristoph Lameter 
2303397874dfSChristoph Lameter 	if (md->mapcount_max > 1)
2304397874dfSChristoph Lameter 		seq_printf(m, " mapmax=%lu", md->mapcount_max);
2305397874dfSChristoph Lameter 
2306397874dfSChristoph Lameter 	if (md->swapcache)
2307397874dfSChristoph Lameter 		seq_printf(m," swapcache=%lu", md->swapcache);
2308397874dfSChristoph Lameter 
2309397874dfSChristoph Lameter 	if (md->active < md->pages && !is_vm_hugetlb_page(vma))
2310397874dfSChristoph Lameter 		seq_printf(m," active=%lu", md->active);
2311397874dfSChristoph Lameter 
2312397874dfSChristoph Lameter 	if (md->writeback)
2313397874dfSChristoph Lameter 		seq_printf(m," writeback=%lu", md->writeback);
2314397874dfSChristoph Lameter 
231556bbd65dSChristoph Lameter 	for_each_node_state(n, N_HIGH_MEMORY)
23161a75a6c8SChristoph Lameter 		if (md->node[n])
23171a75a6c8SChristoph Lameter 			seq_printf(m, " N%d=%lu", n, md->node[n]);
2318397874dfSChristoph Lameter out:
23191a75a6c8SChristoph Lameter 	seq_putc(m, '\n');
23201a75a6c8SChristoph Lameter 	kfree(md);
23211a75a6c8SChristoph Lameter 
23221a75a6c8SChristoph Lameter 	if (m->count < m->size)
232399f89551SEric W. Biederman 		m->version = (vma != priv->tail_vma) ? vma->vm_start : 0;
23241a75a6c8SChristoph Lameter 	return 0;
23251a75a6c8SChristoph Lameter }
2326