xref: /openbmc/linux/mm/mempolicy.c (revision 771fb4d806a92bf6c988fcfbd286ae40a9374332)
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/slab.h>
771da177e4SLinus Torvalds #include <linux/string.h>
78b95f1b31SPaul Gortmaker #include <linux/export.h>
79b488893aSPavel Emelyanov #include <linux/nsproxy.h>
801da177e4SLinus Torvalds #include <linux/interrupt.h>
811da177e4SLinus Torvalds #include <linux/init.h>
821da177e4SLinus Torvalds #include <linux/compat.h>
83dc9aa5b9SChristoph Lameter #include <linux/swap.h>
841a75a6c8SChristoph Lameter #include <linux/seq_file.h>
851a75a6c8SChristoph Lameter #include <linux/proc_fs.h>
86b20a3503SChristoph Lameter #include <linux/migrate.h>
8762b61f61SHugh Dickins #include <linux/ksm.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>
926d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h>
93dc9aa5b9SChristoph Lameter 
941da177e4SLinus Torvalds #include <asm/tlbflush.h>
951da177e4SLinus Torvalds #include <asm/uaccess.h>
96778d3b0fSMichal Hocko #include <linux/random.h>
971da177e4SLinus Torvalds 
9862695a84SNick Piggin #include "internal.h"
9962695a84SNick Piggin 
10038e35860SChristoph Lameter /* Internal flags */
101dc9aa5b9SChristoph Lameter #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)	/* Skip checks for continuous vmas */
10238e35860SChristoph Lameter #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)		/* Invert check for nodemask */
103dc9aa5b9SChristoph Lameter 
104fcc234f8SPekka Enberg static struct kmem_cache *policy_cache;
105fcc234f8SPekka Enberg static struct kmem_cache *sn_cache;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /* Highest zone. An specific allocation for a zone below that is not
1081da177e4SLinus Torvalds    policied. */
1096267276fSChristoph Lameter enum zone_type policy_zone = 0;
1101da177e4SLinus Torvalds 
111bea904d5SLee Schermerhorn /*
112bea904d5SLee Schermerhorn  * run-time system-wide default policy => local allocation
113bea904d5SLee Schermerhorn  */
114e754d79dSH Hartley Sweeten static struct mempolicy default_policy = {
1151da177e4SLinus Torvalds 	.refcnt = ATOMIC_INIT(1), /* never free it */
116bea904d5SLee Schermerhorn 	.mode = MPOL_PREFERRED,
117fc36b8d3SLee Schermerhorn 	.flags = MPOL_F_LOCAL,
1181da177e4SLinus Torvalds };
1191da177e4SLinus Torvalds 
12037012946SDavid Rientjes static const struct mempolicy_operations {
12137012946SDavid Rientjes 	int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
122708c1bbcSMiao Xie 	/*
123708c1bbcSMiao Xie 	 * If read-side task has no lock to protect task->mempolicy, write-side
124708c1bbcSMiao Xie 	 * task will rebind the task->mempolicy by two step. The first step is
125708c1bbcSMiao Xie 	 * setting all the newly nodes, and the second step is cleaning all the
126708c1bbcSMiao Xie 	 * disallowed nodes. In this way, we can avoid finding no node to alloc
127708c1bbcSMiao Xie 	 * page.
128708c1bbcSMiao Xie 	 * If we have a lock to protect task->mempolicy in read-side, we do
129708c1bbcSMiao Xie 	 * rebind directly.
130708c1bbcSMiao Xie 	 *
131708c1bbcSMiao Xie 	 * step:
132708c1bbcSMiao Xie 	 * 	MPOL_REBIND_ONCE - do rebind work at once
133708c1bbcSMiao Xie 	 * 	MPOL_REBIND_STEP1 - set all the newly nodes
134708c1bbcSMiao Xie 	 * 	MPOL_REBIND_STEP2 - clean all the disallowed nodes
135708c1bbcSMiao Xie 	 */
136708c1bbcSMiao Xie 	void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes,
137708c1bbcSMiao Xie 			enum mpol_rebind_step step);
13837012946SDavid Rientjes } mpol_ops[MPOL_MAX];
13937012946SDavid Rientjes 
14019770b32SMel Gorman /* Check that the nodemask contains at least one populated zone */
14137012946SDavid Rientjes static int is_valid_nodemask(const nodemask_t *nodemask)
1421da177e4SLinus Torvalds {
14319770b32SMel Gorman 	int nd, k;
1441da177e4SLinus Torvalds 
14519770b32SMel Gorman 	for_each_node_mask(nd, *nodemask) {
14619770b32SMel Gorman 		struct zone *z;
14719770b32SMel Gorman 
14819770b32SMel Gorman 		for (k = 0; k <= policy_zone; k++) {
14919770b32SMel Gorman 			z = &NODE_DATA(nd)->node_zones[k];
150dd942ae3SAndi Kleen 			if (z->present_pages > 0)
15119770b32SMel Gorman 				return 1;
152dd942ae3SAndi Kleen 		}
153dd942ae3SAndi Kleen 	}
15419770b32SMel Gorman 
15519770b32SMel Gorman 	return 0;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
158f5b087b5SDavid Rientjes static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
159f5b087b5SDavid Rientjes {
1606d556294SBob Liu 	return pol->flags & MPOL_MODE_FLAGS;
1614c50bc01SDavid Rientjes }
1624c50bc01SDavid Rientjes 
1634c50bc01SDavid Rientjes static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
1644c50bc01SDavid Rientjes 				   const nodemask_t *rel)
1654c50bc01SDavid Rientjes {
1664c50bc01SDavid Rientjes 	nodemask_t tmp;
1674c50bc01SDavid Rientjes 	nodes_fold(tmp, *orig, nodes_weight(*rel));
1684c50bc01SDavid Rientjes 	nodes_onto(*ret, tmp, *rel);
169f5b087b5SDavid Rientjes }
170f5b087b5SDavid Rientjes 
17137012946SDavid Rientjes static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
17237012946SDavid Rientjes {
17337012946SDavid Rientjes 	if (nodes_empty(*nodes))
17437012946SDavid Rientjes 		return -EINVAL;
17537012946SDavid Rientjes 	pol->v.nodes = *nodes;
17637012946SDavid Rientjes 	return 0;
17737012946SDavid Rientjes }
17837012946SDavid Rientjes 
17937012946SDavid Rientjes static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
18037012946SDavid Rientjes {
18137012946SDavid Rientjes 	if (!nodes)
182fc36b8d3SLee Schermerhorn 		pol->flags |= MPOL_F_LOCAL;	/* local allocation */
18337012946SDavid Rientjes 	else if (nodes_empty(*nodes))
18437012946SDavid Rientjes 		return -EINVAL;			/*  no allowed nodes */
18537012946SDavid Rientjes 	else
18637012946SDavid Rientjes 		pol->v.preferred_node = first_node(*nodes);
18737012946SDavid Rientjes 	return 0;
18837012946SDavid Rientjes }
18937012946SDavid Rientjes 
19037012946SDavid Rientjes static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
19137012946SDavid Rientjes {
19237012946SDavid Rientjes 	if (!is_valid_nodemask(nodes))
19337012946SDavid Rientjes 		return -EINVAL;
19437012946SDavid Rientjes 	pol->v.nodes = *nodes;
19537012946SDavid Rientjes 	return 0;
19637012946SDavid Rientjes }
19737012946SDavid Rientjes 
19858568d2aSMiao Xie /*
19958568d2aSMiao Xie  * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
20058568d2aSMiao Xie  * any, for the new policy.  mpol_new() has already validated the nodes
20158568d2aSMiao Xie  * parameter with respect to the policy mode and flags.  But, we need to
20258568d2aSMiao Xie  * handle an empty nodemask with MPOL_PREFERRED here.
20358568d2aSMiao Xie  *
20458568d2aSMiao Xie  * Must be called holding task's alloc_lock to protect task's mems_allowed
20558568d2aSMiao Xie  * and mempolicy.  May also be called holding the mmap_semaphore for write.
20658568d2aSMiao Xie  */
2074bfc4495SKAMEZAWA Hiroyuki static int mpol_set_nodemask(struct mempolicy *pol,
2084bfc4495SKAMEZAWA Hiroyuki 		     const nodemask_t *nodes, struct nodemask_scratch *nsc)
20958568d2aSMiao Xie {
21058568d2aSMiao Xie 	int ret;
21158568d2aSMiao Xie 
21258568d2aSMiao Xie 	/* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
21358568d2aSMiao Xie 	if (pol == NULL)
21458568d2aSMiao Xie 		return 0;
2154bfc4495SKAMEZAWA Hiroyuki 	/* Check N_HIGH_MEMORY */
2164bfc4495SKAMEZAWA Hiroyuki 	nodes_and(nsc->mask1,
2174bfc4495SKAMEZAWA Hiroyuki 		  cpuset_current_mems_allowed, node_states[N_HIGH_MEMORY]);
21858568d2aSMiao Xie 
21958568d2aSMiao Xie 	VM_BUG_ON(!nodes);
22058568d2aSMiao Xie 	if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
22158568d2aSMiao Xie 		nodes = NULL;	/* explicit local allocation */
22258568d2aSMiao Xie 	else {
22358568d2aSMiao Xie 		if (pol->flags & MPOL_F_RELATIVE_NODES)
2244bfc4495SKAMEZAWA Hiroyuki 			mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1);
22558568d2aSMiao Xie 		else
2264bfc4495SKAMEZAWA Hiroyuki 			nodes_and(nsc->mask2, *nodes, nsc->mask1);
2274bfc4495SKAMEZAWA Hiroyuki 
22858568d2aSMiao Xie 		if (mpol_store_user_nodemask(pol))
22958568d2aSMiao Xie 			pol->w.user_nodemask = *nodes;
23058568d2aSMiao Xie 		else
23158568d2aSMiao Xie 			pol->w.cpuset_mems_allowed =
23258568d2aSMiao Xie 						cpuset_current_mems_allowed;
23358568d2aSMiao Xie 	}
23458568d2aSMiao Xie 
2354bfc4495SKAMEZAWA Hiroyuki 	if (nodes)
2364bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
2374bfc4495SKAMEZAWA Hiroyuki 	else
2384bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_ops[pol->mode].create(pol, NULL);
23958568d2aSMiao Xie 	return ret;
24058568d2aSMiao Xie }
24158568d2aSMiao Xie 
24258568d2aSMiao Xie /*
24358568d2aSMiao Xie  * This function just creates a new policy, does some check and simple
24458568d2aSMiao Xie  * initialization. You must invoke mpol_set_nodemask() to set nodes.
24558568d2aSMiao Xie  */
246028fec41SDavid Rientjes static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
247028fec41SDavid Rientjes 				  nodemask_t *nodes)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	struct mempolicy *policy;
2501da177e4SLinus Torvalds 
251028fec41SDavid Rientjes 	pr_debug("setting mode %d flags %d nodes[0] %lx\n",
252028fec41SDavid Rientjes 		 mode, flags, nodes ? nodes_addr(*nodes)[0] : -1);
253140d5a49SPaul Mundt 
254d3a71033SLee Schermerhorn 	if (mode == MPOL_DEFAULT || mode == MPOL_NOOP) {
2553e1f0645SDavid Rientjes 		if (nodes && !nodes_empty(*nodes))
25637012946SDavid Rientjes 			return ERR_PTR(-EINVAL);
257d3a71033SLee Schermerhorn 		return NULL;
25837012946SDavid Rientjes 	}
2593e1f0645SDavid Rientjes 	VM_BUG_ON(!nodes);
2603e1f0645SDavid Rientjes 
2613e1f0645SDavid Rientjes 	/*
2623e1f0645SDavid Rientjes 	 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
2633e1f0645SDavid Rientjes 	 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
2643e1f0645SDavid Rientjes 	 * All other modes require a valid pointer to a non-empty nodemask.
2653e1f0645SDavid Rientjes 	 */
2663e1f0645SDavid Rientjes 	if (mode == MPOL_PREFERRED) {
2673e1f0645SDavid Rientjes 		if (nodes_empty(*nodes)) {
2683e1f0645SDavid Rientjes 			if (((flags & MPOL_F_STATIC_NODES) ||
2693e1f0645SDavid Rientjes 			     (flags & MPOL_F_RELATIVE_NODES)))
2703e1f0645SDavid Rientjes 				return ERR_PTR(-EINVAL);
2713e1f0645SDavid Rientjes 		}
272479e2802SPeter Zijlstra 	} else if (mode == MPOL_LOCAL) {
273479e2802SPeter Zijlstra 		if (!nodes_empty(*nodes))
274479e2802SPeter Zijlstra 			return ERR_PTR(-EINVAL);
275479e2802SPeter Zijlstra 		mode = MPOL_PREFERRED;
2763e1f0645SDavid Rientjes 	} else if (nodes_empty(*nodes))
2773e1f0645SDavid Rientjes 		return ERR_PTR(-EINVAL);
2781da177e4SLinus Torvalds 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2791da177e4SLinus Torvalds 	if (!policy)
2801da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2811da177e4SLinus Torvalds 	atomic_set(&policy->refcnt, 1);
28245c4745aSLee Schermerhorn 	policy->mode = mode;
28337012946SDavid Rientjes 	policy->flags = flags;
2843e1f0645SDavid Rientjes 
28537012946SDavid Rientjes 	return policy;
28637012946SDavid Rientjes }
28737012946SDavid Rientjes 
28852cd3b07SLee Schermerhorn /* Slow path of a mpol destructor. */
28952cd3b07SLee Schermerhorn void __mpol_put(struct mempolicy *p)
29052cd3b07SLee Schermerhorn {
29152cd3b07SLee Schermerhorn 	if (!atomic_dec_and_test(&p->refcnt))
29252cd3b07SLee Schermerhorn 		return;
29352cd3b07SLee Schermerhorn 	kmem_cache_free(policy_cache, p);
29452cd3b07SLee Schermerhorn }
29552cd3b07SLee Schermerhorn 
296708c1bbcSMiao Xie static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes,
297708c1bbcSMiao Xie 				enum mpol_rebind_step step)
29837012946SDavid Rientjes {
29937012946SDavid Rientjes }
30037012946SDavid Rientjes 
301708c1bbcSMiao Xie /*
302708c1bbcSMiao Xie  * step:
303708c1bbcSMiao Xie  * 	MPOL_REBIND_ONCE  - do rebind work at once
304708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP1 - set all the newly nodes
305708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP2 - clean all the disallowed nodes
306708c1bbcSMiao Xie  */
307708c1bbcSMiao Xie static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes,
308708c1bbcSMiao Xie 				 enum mpol_rebind_step step)
3091d0d2680SDavid Rientjes {
3101d0d2680SDavid Rientjes 	nodemask_t tmp;
3111d0d2680SDavid Rientjes 
31237012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES)
31337012946SDavid Rientjes 		nodes_and(tmp, pol->w.user_nodemask, *nodes);
31437012946SDavid Rientjes 	else if (pol->flags & MPOL_F_RELATIVE_NODES)
31537012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
3161d0d2680SDavid Rientjes 	else {
317708c1bbcSMiao Xie 		/*
318708c1bbcSMiao Xie 		 * if step == 1, we use ->w.cpuset_mems_allowed to cache the
319708c1bbcSMiao Xie 		 * result
320708c1bbcSMiao Xie 		 */
321708c1bbcSMiao Xie 		if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) {
322708c1bbcSMiao Xie 			nodes_remap(tmp, pol->v.nodes,
323708c1bbcSMiao Xie 					pol->w.cpuset_mems_allowed, *nodes);
324708c1bbcSMiao Xie 			pol->w.cpuset_mems_allowed = step ? tmp : *nodes;
325708c1bbcSMiao Xie 		} else if (step == MPOL_REBIND_STEP2) {
326708c1bbcSMiao Xie 			tmp = pol->w.cpuset_mems_allowed;
32737012946SDavid Rientjes 			pol->w.cpuset_mems_allowed = *nodes;
328708c1bbcSMiao Xie 		} else
329708c1bbcSMiao Xie 			BUG();
3301d0d2680SDavid Rientjes 	}
33137012946SDavid Rientjes 
332708c1bbcSMiao Xie 	if (nodes_empty(tmp))
333708c1bbcSMiao Xie 		tmp = *nodes;
334708c1bbcSMiao Xie 
335708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP1)
336708c1bbcSMiao Xie 		nodes_or(pol->v.nodes, pol->v.nodes, tmp);
337708c1bbcSMiao Xie 	else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2)
3381d0d2680SDavid Rientjes 		pol->v.nodes = tmp;
339708c1bbcSMiao Xie 	else
340708c1bbcSMiao Xie 		BUG();
341708c1bbcSMiao Xie 
3421d0d2680SDavid Rientjes 	if (!node_isset(current->il_next, tmp)) {
3431d0d2680SDavid Rientjes 		current->il_next = next_node(current->il_next, tmp);
3441d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
3451d0d2680SDavid Rientjes 			current->il_next = first_node(tmp);
3461d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
3471d0d2680SDavid Rientjes 			current->il_next = numa_node_id();
3481d0d2680SDavid Rientjes 	}
34937012946SDavid Rientjes }
35037012946SDavid Rientjes 
35137012946SDavid Rientjes static void mpol_rebind_preferred(struct mempolicy *pol,
352708c1bbcSMiao Xie 				  const nodemask_t *nodes,
353708c1bbcSMiao Xie 				  enum mpol_rebind_step step)
35437012946SDavid Rientjes {
35537012946SDavid Rientjes 	nodemask_t tmp;
35637012946SDavid Rientjes 
35737012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES) {
3581d0d2680SDavid Rientjes 		int node = first_node(pol->w.user_nodemask);
3591d0d2680SDavid Rientjes 
360fc36b8d3SLee Schermerhorn 		if (node_isset(node, *nodes)) {
3611d0d2680SDavid Rientjes 			pol->v.preferred_node = node;
362fc36b8d3SLee Schermerhorn 			pol->flags &= ~MPOL_F_LOCAL;
363fc36b8d3SLee Schermerhorn 		} else
364fc36b8d3SLee Schermerhorn 			pol->flags |= MPOL_F_LOCAL;
36537012946SDavid Rientjes 	} else if (pol->flags & MPOL_F_RELATIVE_NODES) {
36637012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
3671d0d2680SDavid Rientjes 		pol->v.preferred_node = first_node(tmp);
368fc36b8d3SLee Schermerhorn 	} else if (!(pol->flags & MPOL_F_LOCAL)) {
3691d0d2680SDavid Rientjes 		pol->v.preferred_node = node_remap(pol->v.preferred_node,
37037012946SDavid Rientjes 						   pol->w.cpuset_mems_allowed,
37137012946SDavid Rientjes 						   *nodes);
37237012946SDavid Rientjes 		pol->w.cpuset_mems_allowed = *nodes;
3731d0d2680SDavid Rientjes 	}
3741d0d2680SDavid Rientjes }
37537012946SDavid Rientjes 
376708c1bbcSMiao Xie /*
377708c1bbcSMiao Xie  * mpol_rebind_policy - Migrate a policy to a different set of nodes
378708c1bbcSMiao Xie  *
379708c1bbcSMiao Xie  * If read-side task has no lock to protect task->mempolicy, write-side
380708c1bbcSMiao Xie  * task will rebind the task->mempolicy by two step. The first step is
381708c1bbcSMiao Xie  * setting all the newly nodes, and the second step is cleaning all the
382708c1bbcSMiao Xie  * disallowed nodes. In this way, we can avoid finding no node to alloc
383708c1bbcSMiao Xie  * page.
384708c1bbcSMiao Xie  * If we have a lock to protect task->mempolicy in read-side, we do
385708c1bbcSMiao Xie  * rebind directly.
386708c1bbcSMiao Xie  *
387708c1bbcSMiao Xie  * step:
388708c1bbcSMiao Xie  * 	MPOL_REBIND_ONCE  - do rebind work at once
389708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP1 - set all the newly nodes
390708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP2 - clean all the disallowed nodes
391708c1bbcSMiao Xie  */
392708c1bbcSMiao Xie static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask,
393708c1bbcSMiao Xie 				enum mpol_rebind_step step)
39437012946SDavid Rientjes {
39537012946SDavid Rientjes 	if (!pol)
39637012946SDavid Rientjes 		return;
39789c522c7SWang Sheng-Hui 	if (!mpol_store_user_nodemask(pol) && step == MPOL_REBIND_ONCE &&
39837012946SDavid Rientjes 	    nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
39937012946SDavid Rientjes 		return;
400708c1bbcSMiao Xie 
401708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING))
402708c1bbcSMiao Xie 		return;
403708c1bbcSMiao Xie 
404708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING))
405708c1bbcSMiao Xie 		BUG();
406708c1bbcSMiao Xie 
407708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP1)
408708c1bbcSMiao Xie 		pol->flags |= MPOL_F_REBINDING;
409708c1bbcSMiao Xie 	else if (step == MPOL_REBIND_STEP2)
410708c1bbcSMiao Xie 		pol->flags &= ~MPOL_F_REBINDING;
411708c1bbcSMiao Xie 	else if (step >= MPOL_REBIND_NSTEP)
412708c1bbcSMiao Xie 		BUG();
413708c1bbcSMiao Xie 
414708c1bbcSMiao Xie 	mpol_ops[pol->mode].rebind(pol, newmask, step);
4151d0d2680SDavid Rientjes }
4161d0d2680SDavid Rientjes 
4171d0d2680SDavid Rientjes /*
4181d0d2680SDavid Rientjes  * Wrapper for mpol_rebind_policy() that just requires task
4191d0d2680SDavid Rientjes  * pointer, and updates task mempolicy.
42058568d2aSMiao Xie  *
42158568d2aSMiao Xie  * Called with task's alloc_lock held.
4221d0d2680SDavid Rientjes  */
4231d0d2680SDavid Rientjes 
424708c1bbcSMiao Xie void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
425708c1bbcSMiao Xie 			enum mpol_rebind_step step)
4261d0d2680SDavid Rientjes {
427708c1bbcSMiao Xie 	mpol_rebind_policy(tsk->mempolicy, new, step);
4281d0d2680SDavid Rientjes }
4291d0d2680SDavid Rientjes 
4301d0d2680SDavid Rientjes /*
4311d0d2680SDavid Rientjes  * Rebind each vma in mm to new nodemask.
4321d0d2680SDavid Rientjes  *
4331d0d2680SDavid Rientjes  * Call holding a reference to mm.  Takes mm->mmap_sem during call.
4341d0d2680SDavid Rientjes  */
4351d0d2680SDavid Rientjes 
4361d0d2680SDavid Rientjes void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
4371d0d2680SDavid Rientjes {
4381d0d2680SDavid Rientjes 	struct vm_area_struct *vma;
4391d0d2680SDavid Rientjes 
4401d0d2680SDavid Rientjes 	down_write(&mm->mmap_sem);
4411d0d2680SDavid Rientjes 	for (vma = mm->mmap; vma; vma = vma->vm_next)
442708c1bbcSMiao Xie 		mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE);
4431d0d2680SDavid Rientjes 	up_write(&mm->mmap_sem);
4441d0d2680SDavid Rientjes }
4451d0d2680SDavid Rientjes 
44637012946SDavid Rientjes static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
44737012946SDavid Rientjes 	[MPOL_DEFAULT] = {
44837012946SDavid Rientjes 		.rebind = mpol_rebind_default,
44937012946SDavid Rientjes 	},
45037012946SDavid Rientjes 	[MPOL_INTERLEAVE] = {
45137012946SDavid Rientjes 		.create = mpol_new_interleave,
45237012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
45337012946SDavid Rientjes 	},
45437012946SDavid Rientjes 	[MPOL_PREFERRED] = {
45537012946SDavid Rientjes 		.create = mpol_new_preferred,
45637012946SDavid Rientjes 		.rebind = mpol_rebind_preferred,
45737012946SDavid Rientjes 	},
45837012946SDavid Rientjes 	[MPOL_BIND] = {
45937012946SDavid Rientjes 		.create = mpol_new_bind,
46037012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
46137012946SDavid Rientjes 	},
46237012946SDavid Rientjes };
46337012946SDavid Rientjes 
464fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
465fc301289SChristoph Lameter 				unsigned long flags);
4661a75a6c8SChristoph Lameter 
46738e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */
468b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
469dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
470dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
47138e35860SChristoph Lameter 		void *private)
4721da177e4SLinus Torvalds {
47391612e0dSHugh Dickins 	pte_t *orig_pte;
47491612e0dSHugh Dickins 	pte_t *pte;
475705e87c0SHugh Dickins 	spinlock_t *ptl;
476941150a3SHugh Dickins 
477705e87c0SHugh Dickins 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
47891612e0dSHugh Dickins 	do {
4796aab341eSLinus Torvalds 		struct page *page;
48025ba77c1SAndy Whitcroft 		int nid;
48191612e0dSHugh Dickins 
48291612e0dSHugh Dickins 		if (!pte_present(*pte))
48391612e0dSHugh Dickins 			continue;
4846aab341eSLinus Torvalds 		page = vm_normal_page(vma, addr, *pte);
4856aab341eSLinus Torvalds 		if (!page)
48691612e0dSHugh Dickins 			continue;
487053837fcSNick Piggin 		/*
48862b61f61SHugh Dickins 		 * vm_normal_page() filters out zero pages, but there might
48962b61f61SHugh Dickins 		 * still be PageReserved pages to skip, perhaps in a VDSO.
49062b61f61SHugh Dickins 		 * And we cannot move PageKsm pages sensibly or safely yet.
491053837fcSNick Piggin 		 */
49262b61f61SHugh Dickins 		if (PageReserved(page) || PageKsm(page))
493f4598c8bSChristoph Lameter 			continue;
4946aab341eSLinus Torvalds 		nid = page_to_nid(page);
49538e35860SChristoph Lameter 		if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
49638e35860SChristoph Lameter 			continue;
49738e35860SChristoph Lameter 
498b1f72d18SStephen Wilson 		if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
499fc301289SChristoph Lameter 			migrate_page_add(page, private, flags);
500dc9aa5b9SChristoph Lameter 		else
5011da177e4SLinus Torvalds 			break;
50291612e0dSHugh Dickins 	} while (pte++, addr += PAGE_SIZE, addr != end);
503705e87c0SHugh Dickins 	pte_unmap_unlock(orig_pte, ptl);
50491612e0dSHugh Dickins 	return addr != end;
50591612e0dSHugh Dickins }
50691612e0dSHugh Dickins 
507b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
508dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
509dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
51038e35860SChristoph Lameter 		void *private)
51191612e0dSHugh Dickins {
51291612e0dSHugh Dickins 	pmd_t *pmd;
51391612e0dSHugh Dickins 	unsigned long next;
51491612e0dSHugh Dickins 
51591612e0dSHugh Dickins 	pmd = pmd_offset(pud, addr);
51691612e0dSHugh Dickins 	do {
51791612e0dSHugh Dickins 		next = pmd_addr_end(addr, end);
518bae9c19bSAndrea Arcangeli 		split_huge_page_pmd(vma->vm_mm, pmd);
5191a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
52091612e0dSHugh Dickins 			continue;
521dc9aa5b9SChristoph Lameter 		if (check_pte_range(vma, pmd, addr, next, nodes,
52238e35860SChristoph Lameter 				    flags, private))
52391612e0dSHugh Dickins 			return -EIO;
52491612e0dSHugh Dickins 	} while (pmd++, addr = next, addr != end);
52591612e0dSHugh Dickins 	return 0;
52691612e0dSHugh Dickins }
52791612e0dSHugh Dickins 
528b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
529dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
530dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
53138e35860SChristoph Lameter 		void *private)
53291612e0dSHugh Dickins {
53391612e0dSHugh Dickins 	pud_t *pud;
53491612e0dSHugh Dickins 	unsigned long next;
53591612e0dSHugh Dickins 
53691612e0dSHugh Dickins 	pud = pud_offset(pgd, addr);
53791612e0dSHugh Dickins 	do {
53891612e0dSHugh Dickins 		next = pud_addr_end(addr, end);
53991612e0dSHugh Dickins 		if (pud_none_or_clear_bad(pud))
54091612e0dSHugh Dickins 			continue;
541dc9aa5b9SChristoph Lameter 		if (check_pmd_range(vma, pud, addr, next, nodes,
54238e35860SChristoph Lameter 				    flags, private))
54391612e0dSHugh Dickins 			return -EIO;
54491612e0dSHugh Dickins 	} while (pud++, addr = next, addr != end);
54591612e0dSHugh Dickins 	return 0;
54691612e0dSHugh Dickins }
54791612e0dSHugh Dickins 
548b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma,
549dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
550dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
55138e35860SChristoph Lameter 		void *private)
55291612e0dSHugh Dickins {
55391612e0dSHugh Dickins 	pgd_t *pgd;
55491612e0dSHugh Dickins 	unsigned long next;
55591612e0dSHugh Dickins 
556b5810039SNick Piggin 	pgd = pgd_offset(vma->vm_mm, addr);
55791612e0dSHugh Dickins 	do {
55891612e0dSHugh Dickins 		next = pgd_addr_end(addr, end);
55991612e0dSHugh Dickins 		if (pgd_none_or_clear_bad(pgd))
56091612e0dSHugh Dickins 			continue;
561dc9aa5b9SChristoph Lameter 		if (check_pud_range(vma, pgd, addr, next, nodes,
56238e35860SChristoph Lameter 				    flags, private))
56391612e0dSHugh Dickins 			return -EIO;
56491612e0dSHugh Dickins 	} while (pgd++, addr = next, addr != end);
56591612e0dSHugh Dickins 	return 0;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
568dc9aa5b9SChristoph Lameter /*
569dc9aa5b9SChristoph Lameter  * Check if all pages in a range are on a set of nodes.
570dc9aa5b9SChristoph Lameter  * If pagelist != NULL then isolate pages from the LRU and
571dc9aa5b9SChristoph Lameter  * put them on the pagelist.
572dc9aa5b9SChristoph Lameter  */
5731da177e4SLinus Torvalds static struct vm_area_struct *
5741da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
57538e35860SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags, void *private)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	int err;
5781da177e4SLinus Torvalds 	struct vm_area_struct *first, *vma, *prev;
5791da177e4SLinus Torvalds 
580053837fcSNick Piggin 
5811da177e4SLinus Torvalds 	first = find_vma(mm, start);
5821da177e4SLinus Torvalds 	if (!first)
5831da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
5841da177e4SLinus Torvalds 	prev = NULL;
5851da177e4SLinus Torvalds 	for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
586dc9aa5b9SChristoph Lameter 		if (!(flags & MPOL_MF_DISCONTIG_OK)) {
5871da177e4SLinus Torvalds 			if (!vma->vm_next && vma->vm_end < end)
5881da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
5891da177e4SLinus Torvalds 			if (prev && prev->vm_end < vma->vm_start)
5901da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
591dc9aa5b9SChristoph Lameter 		}
592dc9aa5b9SChristoph Lameter 		if (!is_vm_hugetlb_page(vma) &&
593dc9aa5b9SChristoph Lameter 		    ((flags & MPOL_MF_STRICT) ||
594dc9aa5b9SChristoph Lameter 		     ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
595dc9aa5b9SChristoph Lameter 				vma_migratable(vma)))) {
5965b952b3cSAndi Kleen 			unsigned long endvma = vma->vm_end;
597dc9aa5b9SChristoph Lameter 
5985b952b3cSAndi Kleen 			if (endvma > end)
5995b952b3cSAndi Kleen 				endvma = end;
6005b952b3cSAndi Kleen 			if (vma->vm_start > start)
6015b952b3cSAndi Kleen 				start = vma->vm_start;
602dc9aa5b9SChristoph Lameter 			err = check_pgd_range(vma, start, endvma, nodes,
60338e35860SChristoph Lameter 						flags, private);
6041da177e4SLinus Torvalds 			if (err) {
6051da177e4SLinus Torvalds 				first = ERR_PTR(err);
6061da177e4SLinus Torvalds 				break;
6071da177e4SLinus Torvalds 			}
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 		prev = vma;
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 	return first;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
614869833f2SKOSAKI Motohiro /*
615869833f2SKOSAKI Motohiro  * Apply policy to a single VMA
616869833f2SKOSAKI Motohiro  * This must be called with the mmap_sem held for writing.
617869833f2SKOSAKI Motohiro  */
618869833f2SKOSAKI Motohiro static int vma_replace_policy(struct vm_area_struct *vma,
619869833f2SKOSAKI Motohiro 						struct mempolicy *pol)
6208d34694cSKOSAKI Motohiro {
621869833f2SKOSAKI Motohiro 	int err;
622869833f2SKOSAKI Motohiro 	struct mempolicy *old;
623869833f2SKOSAKI Motohiro 	struct mempolicy *new;
6248d34694cSKOSAKI Motohiro 
6258d34694cSKOSAKI Motohiro 	pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
6268d34694cSKOSAKI Motohiro 		 vma->vm_start, vma->vm_end, vma->vm_pgoff,
6278d34694cSKOSAKI Motohiro 		 vma->vm_ops, vma->vm_file,
6288d34694cSKOSAKI Motohiro 		 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
6298d34694cSKOSAKI Motohiro 
630869833f2SKOSAKI Motohiro 	new = mpol_dup(pol);
631869833f2SKOSAKI Motohiro 	if (IS_ERR(new))
632869833f2SKOSAKI Motohiro 		return PTR_ERR(new);
633869833f2SKOSAKI Motohiro 
634869833f2SKOSAKI Motohiro 	if (vma->vm_ops && vma->vm_ops->set_policy) {
6358d34694cSKOSAKI Motohiro 		err = vma->vm_ops->set_policy(vma, new);
636869833f2SKOSAKI Motohiro 		if (err)
637869833f2SKOSAKI Motohiro 			goto err_out;
6388d34694cSKOSAKI Motohiro 	}
639869833f2SKOSAKI Motohiro 
640869833f2SKOSAKI Motohiro 	old = vma->vm_policy;
641869833f2SKOSAKI Motohiro 	vma->vm_policy = new; /* protected by mmap_sem */
642869833f2SKOSAKI Motohiro 	mpol_put(old);
643869833f2SKOSAKI Motohiro 
644869833f2SKOSAKI Motohiro 	return 0;
645869833f2SKOSAKI Motohiro  err_out:
646869833f2SKOSAKI Motohiro 	mpol_put(new);
6478d34694cSKOSAKI Motohiro 	return err;
6488d34694cSKOSAKI Motohiro }
6498d34694cSKOSAKI Motohiro 
6501da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */
6519d8cebd4SKOSAKI Motohiro static int mbind_range(struct mm_struct *mm, unsigned long start,
6529d8cebd4SKOSAKI Motohiro 		       unsigned long end, struct mempolicy *new_pol)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	struct vm_area_struct *next;
6559d8cebd4SKOSAKI Motohiro 	struct vm_area_struct *prev;
6569d8cebd4SKOSAKI Motohiro 	struct vm_area_struct *vma;
6579d8cebd4SKOSAKI Motohiro 	int err = 0;
658e26a5114SKOSAKI Motohiro 	pgoff_t pgoff;
6599d8cebd4SKOSAKI Motohiro 	unsigned long vmstart;
6609d8cebd4SKOSAKI Motohiro 	unsigned long vmend;
6611da177e4SLinus Torvalds 
662097d5910SLinus Torvalds 	vma = find_vma(mm, start);
6639d8cebd4SKOSAKI Motohiro 	if (!vma || vma->vm_start > start)
6649d8cebd4SKOSAKI Motohiro 		return -EFAULT;
6659d8cebd4SKOSAKI Motohiro 
666097d5910SLinus Torvalds 	prev = vma->vm_prev;
667e26a5114SKOSAKI Motohiro 	if (start > vma->vm_start)
668e26a5114SKOSAKI Motohiro 		prev = vma;
669e26a5114SKOSAKI Motohiro 
6709d8cebd4SKOSAKI Motohiro 	for (; vma && vma->vm_start < end; prev = vma, vma = next) {
6711da177e4SLinus Torvalds 		next = vma->vm_next;
6729d8cebd4SKOSAKI Motohiro 		vmstart = max(start, vma->vm_start);
6739d8cebd4SKOSAKI Motohiro 		vmend   = min(end, vma->vm_end);
6749d8cebd4SKOSAKI Motohiro 
675e26a5114SKOSAKI Motohiro 		if (mpol_equal(vma_policy(vma), new_pol))
676e26a5114SKOSAKI Motohiro 			continue;
677e26a5114SKOSAKI Motohiro 
678e26a5114SKOSAKI Motohiro 		pgoff = vma->vm_pgoff +
679e26a5114SKOSAKI Motohiro 			((vmstart - vma->vm_start) >> PAGE_SHIFT);
6809d8cebd4SKOSAKI Motohiro 		prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
681e26a5114SKOSAKI Motohiro 				  vma->anon_vma, vma->vm_file, pgoff,
6828aacc9f5SCaspar Zhang 				  new_pol);
6839d8cebd4SKOSAKI Motohiro 		if (prev) {
6849d8cebd4SKOSAKI Motohiro 			vma = prev;
6859d8cebd4SKOSAKI Motohiro 			next = vma->vm_next;
6869d8cebd4SKOSAKI Motohiro 			continue;
6871da177e4SLinus Torvalds 		}
6889d8cebd4SKOSAKI Motohiro 		if (vma->vm_start != vmstart) {
6899d8cebd4SKOSAKI Motohiro 			err = split_vma(vma->vm_mm, vma, vmstart, 1);
6909d8cebd4SKOSAKI Motohiro 			if (err)
6919d8cebd4SKOSAKI Motohiro 				goto out;
6929d8cebd4SKOSAKI Motohiro 		}
6939d8cebd4SKOSAKI Motohiro 		if (vma->vm_end != vmend) {
6949d8cebd4SKOSAKI Motohiro 			err = split_vma(vma->vm_mm, vma, vmend, 0);
6959d8cebd4SKOSAKI Motohiro 			if (err)
6969d8cebd4SKOSAKI Motohiro 				goto out;
6979d8cebd4SKOSAKI Motohiro 		}
698869833f2SKOSAKI Motohiro 		err = vma_replace_policy(vma, new_pol);
6999d8cebd4SKOSAKI Motohiro 		if (err)
7009d8cebd4SKOSAKI Motohiro 			goto out;
7019d8cebd4SKOSAKI Motohiro 	}
7029d8cebd4SKOSAKI Motohiro 
7039d8cebd4SKOSAKI Motohiro  out:
7041da177e4SLinus Torvalds 	return err;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
707c61afb18SPaul Jackson /*
708c61afb18SPaul Jackson  * Update task->flags PF_MEMPOLICY bit: set iff non-default
709c61afb18SPaul Jackson  * mempolicy.  Allows more rapid checking of this (combined perhaps
710c61afb18SPaul Jackson  * with other PF_* flag bits) on memory allocation hot code paths.
711c61afb18SPaul Jackson  *
712c61afb18SPaul Jackson  * If called from outside this file, the task 'p' should -only- be
713c61afb18SPaul Jackson  * a newly forked child not yet visible on the task list, because
714c61afb18SPaul Jackson  * manipulating the task flags of a visible task is not safe.
715c61afb18SPaul Jackson  *
716c61afb18SPaul Jackson  * The above limitation is why this routine has the funny name
717c61afb18SPaul Jackson  * mpol_fix_fork_child_flag().
718c61afb18SPaul Jackson  *
719c61afb18SPaul Jackson  * It is also safe to call this with a task pointer of current,
720c61afb18SPaul Jackson  * which the static wrapper mpol_set_task_struct_flag() does,
721c61afb18SPaul Jackson  * for use within this file.
722c61afb18SPaul Jackson  */
723c61afb18SPaul Jackson 
724c61afb18SPaul Jackson void mpol_fix_fork_child_flag(struct task_struct *p)
725c61afb18SPaul Jackson {
726c61afb18SPaul Jackson 	if (p->mempolicy)
727c61afb18SPaul Jackson 		p->flags |= PF_MEMPOLICY;
728c61afb18SPaul Jackson 	else
729c61afb18SPaul Jackson 		p->flags &= ~PF_MEMPOLICY;
730c61afb18SPaul Jackson }
731c61afb18SPaul Jackson 
732c61afb18SPaul Jackson static void mpol_set_task_struct_flag(void)
733c61afb18SPaul Jackson {
734c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(current);
735c61afb18SPaul Jackson }
736c61afb18SPaul Jackson 
7371da177e4SLinus Torvalds /* Set the process memory policy */
738028fec41SDavid Rientjes static long do_set_mempolicy(unsigned short mode, unsigned short flags,
739028fec41SDavid Rientjes 			     nodemask_t *nodes)
7401da177e4SLinus Torvalds {
74158568d2aSMiao Xie 	struct mempolicy *new, *old;
742f4e53d91SLee Schermerhorn 	struct mm_struct *mm = current->mm;
7434bfc4495SKAMEZAWA Hiroyuki 	NODEMASK_SCRATCH(scratch);
74458568d2aSMiao Xie 	int ret;
7451da177e4SLinus Torvalds 
7464bfc4495SKAMEZAWA Hiroyuki 	if (!scratch)
7474bfc4495SKAMEZAWA Hiroyuki 		return -ENOMEM;
748f4e53d91SLee Schermerhorn 
7494bfc4495SKAMEZAWA Hiroyuki 	new = mpol_new(mode, flags, nodes);
7504bfc4495SKAMEZAWA Hiroyuki 	if (IS_ERR(new)) {
7514bfc4495SKAMEZAWA Hiroyuki 		ret = PTR_ERR(new);
7524bfc4495SKAMEZAWA Hiroyuki 		goto out;
7534bfc4495SKAMEZAWA Hiroyuki 	}
754f4e53d91SLee Schermerhorn 	/*
755f4e53d91SLee Schermerhorn 	 * prevent changing our mempolicy while show_numa_maps()
756f4e53d91SLee Schermerhorn 	 * is using it.
757f4e53d91SLee Schermerhorn 	 * Note:  do_set_mempolicy() can be called at init time
758f4e53d91SLee Schermerhorn 	 * with no 'mm'.
759f4e53d91SLee Schermerhorn 	 */
760f4e53d91SLee Schermerhorn 	if (mm)
761f4e53d91SLee Schermerhorn 		down_write(&mm->mmap_sem);
76258568d2aSMiao Xie 	task_lock(current);
7634bfc4495SKAMEZAWA Hiroyuki 	ret = mpol_set_nodemask(new, nodes, scratch);
76458568d2aSMiao Xie 	if (ret) {
76558568d2aSMiao Xie 		task_unlock(current);
76658568d2aSMiao Xie 		if (mm)
76758568d2aSMiao Xie 			up_write(&mm->mmap_sem);
76858568d2aSMiao Xie 		mpol_put(new);
7694bfc4495SKAMEZAWA Hiroyuki 		goto out;
77058568d2aSMiao Xie 	}
77158568d2aSMiao Xie 	old = current->mempolicy;
7721da177e4SLinus Torvalds 	current->mempolicy = new;
773c61afb18SPaul Jackson 	mpol_set_task_struct_flag();
77445c4745aSLee Schermerhorn 	if (new && new->mode == MPOL_INTERLEAVE &&
775f5b087b5SDavid Rientjes 	    nodes_weight(new->v.nodes))
776dfcd3c0dSAndi Kleen 		current->il_next = first_node(new->v.nodes);
77758568d2aSMiao Xie 	task_unlock(current);
778f4e53d91SLee Schermerhorn 	if (mm)
779f4e53d91SLee Schermerhorn 		up_write(&mm->mmap_sem);
780f4e53d91SLee Schermerhorn 
78158568d2aSMiao Xie 	mpol_put(old);
7824bfc4495SKAMEZAWA Hiroyuki 	ret = 0;
7834bfc4495SKAMEZAWA Hiroyuki out:
7844bfc4495SKAMEZAWA Hiroyuki 	NODEMASK_SCRATCH_FREE(scratch);
7854bfc4495SKAMEZAWA Hiroyuki 	return ret;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
788bea904d5SLee Schermerhorn /*
789bea904d5SLee Schermerhorn  * Return nodemask for policy for get_mempolicy() query
79058568d2aSMiao Xie  *
79158568d2aSMiao Xie  * Called with task's alloc_lock held
792bea904d5SLee Schermerhorn  */
793bea904d5SLee Schermerhorn static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
7941da177e4SLinus Torvalds {
795dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
796bea904d5SLee Schermerhorn 	if (p == &default_policy)
797bea904d5SLee Schermerhorn 		return;
798bea904d5SLee Schermerhorn 
79945c4745aSLee Schermerhorn 	switch (p->mode) {
80019770b32SMel Gorman 	case MPOL_BIND:
80119770b32SMel Gorman 		/* Fall through */
8021da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
803dfcd3c0dSAndi Kleen 		*nodes = p->v.nodes;
8041da177e4SLinus Torvalds 		break;
8051da177e4SLinus Torvalds 	case MPOL_PREFERRED:
806fc36b8d3SLee Schermerhorn 		if (!(p->flags & MPOL_F_LOCAL))
807dfcd3c0dSAndi Kleen 			node_set(p->v.preferred_node, *nodes);
80853f2556bSLee Schermerhorn 		/* else return empty node mask for local allocation */
8091da177e4SLinus Torvalds 		break;
8101da177e4SLinus Torvalds 	default:
8111da177e4SLinus Torvalds 		BUG();
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	struct page *p;
8181da177e4SLinus Torvalds 	int err;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
8211da177e4SLinus Torvalds 	if (err >= 0) {
8221da177e4SLinus Torvalds 		err = page_to_nid(p);
8231da177e4SLinus Torvalds 		put_page(p);
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 	return err;
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds /* Retrieve NUMA policy */
829dbcb0f19SAdrian Bunk static long do_get_mempolicy(int *policy, nodemask_t *nmask,
8301da177e4SLinus Torvalds 			     unsigned long addr, unsigned long flags)
8311da177e4SLinus Torvalds {
8328bccd85fSChristoph Lameter 	int err;
8331da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
8341da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
8351da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
8361da177e4SLinus Torvalds 
837754af6f5SLee Schermerhorn 	if (flags &
838754af6f5SLee Schermerhorn 		~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
8391da177e4SLinus Torvalds 		return -EINVAL;
840754af6f5SLee Schermerhorn 
841754af6f5SLee Schermerhorn 	if (flags & MPOL_F_MEMS_ALLOWED) {
842754af6f5SLee Schermerhorn 		if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
843754af6f5SLee Schermerhorn 			return -EINVAL;
844754af6f5SLee Schermerhorn 		*policy = 0;	/* just so it's initialized */
84558568d2aSMiao Xie 		task_lock(current);
846754af6f5SLee Schermerhorn 		*nmask  = cpuset_current_mems_allowed;
84758568d2aSMiao Xie 		task_unlock(current);
848754af6f5SLee Schermerhorn 		return 0;
849754af6f5SLee Schermerhorn 	}
850754af6f5SLee Schermerhorn 
8511da177e4SLinus Torvalds 	if (flags & MPOL_F_ADDR) {
852bea904d5SLee Schermerhorn 		/*
853bea904d5SLee Schermerhorn 		 * Do NOT fall back to task policy if the
854bea904d5SLee Schermerhorn 		 * vma/shared policy at addr is NULL.  We
855bea904d5SLee Schermerhorn 		 * want to return MPOL_DEFAULT in this case.
856bea904d5SLee Schermerhorn 		 */
8571da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
8581da177e4SLinus Torvalds 		vma = find_vma_intersection(mm, addr, addr+1);
8591da177e4SLinus Torvalds 		if (!vma) {
8601da177e4SLinus Torvalds 			up_read(&mm->mmap_sem);
8611da177e4SLinus Torvalds 			return -EFAULT;
8621da177e4SLinus Torvalds 		}
8631da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
8641da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
8651da177e4SLinus Torvalds 		else
8661da177e4SLinus Torvalds 			pol = vma->vm_policy;
8671da177e4SLinus Torvalds 	} else if (addr)
8681da177e4SLinus Torvalds 		return -EINVAL;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	if (!pol)
871bea904d5SLee Schermerhorn 		pol = &default_policy;	/* indicates default behavior */
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (flags & MPOL_F_NODE) {
8741da177e4SLinus Torvalds 		if (flags & MPOL_F_ADDR) {
8751da177e4SLinus Torvalds 			err = lookup_node(mm, addr);
8761da177e4SLinus Torvalds 			if (err < 0)
8771da177e4SLinus Torvalds 				goto out;
8788bccd85fSChristoph Lameter 			*policy = err;
8791da177e4SLinus Torvalds 		} else if (pol == current->mempolicy &&
88045c4745aSLee Schermerhorn 				pol->mode == MPOL_INTERLEAVE) {
8818bccd85fSChristoph Lameter 			*policy = current->il_next;
8821da177e4SLinus Torvalds 		} else {
8831da177e4SLinus Torvalds 			err = -EINVAL;
8841da177e4SLinus Torvalds 			goto out;
8851da177e4SLinus Torvalds 		}
886bea904d5SLee Schermerhorn 	} else {
887bea904d5SLee Schermerhorn 		*policy = pol == &default_policy ? MPOL_DEFAULT :
888bea904d5SLee Schermerhorn 						pol->mode;
889d79df630SDavid Rientjes 		/*
890d79df630SDavid Rientjes 		 * Internal mempolicy flags must be masked off before exposing
891d79df630SDavid Rientjes 		 * the policy to userspace.
892d79df630SDavid Rientjes 		 */
893d79df630SDavid Rientjes 		*policy |= (pol->flags & MPOL_MODE_FLAGS);
894bea904d5SLee Schermerhorn 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	if (vma) {
8971da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
8981da177e4SLinus Torvalds 		vma = NULL;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	err = 0;
90258568d2aSMiao Xie 	if (nmask) {
903c6b6ef8bSLee Schermerhorn 		if (mpol_store_user_nodemask(pol)) {
904c6b6ef8bSLee Schermerhorn 			*nmask = pol->w.user_nodemask;
905c6b6ef8bSLee Schermerhorn 		} else {
90658568d2aSMiao Xie 			task_lock(current);
907bea904d5SLee Schermerhorn 			get_policy_nodemask(pol, nmask);
90858568d2aSMiao Xie 			task_unlock(current);
90958568d2aSMiao Xie 		}
910c6b6ef8bSLee Schermerhorn 	}
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds  out:
91352cd3b07SLee Schermerhorn 	mpol_cond_put(pol);
9141da177e4SLinus Torvalds 	if (vma)
9151da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
9161da177e4SLinus Torvalds 	return err;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
919b20a3503SChristoph Lameter #ifdef CONFIG_MIGRATION
9208bccd85fSChristoph Lameter /*
9216ce3c4c0SChristoph Lameter  * page migration
9226ce3c4c0SChristoph Lameter  */
923fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
924fc301289SChristoph Lameter 				unsigned long flags)
9256ce3c4c0SChristoph Lameter {
9266ce3c4c0SChristoph Lameter 	/*
927fc301289SChristoph Lameter 	 * Avoid migrating a page that is shared with others.
9286ce3c4c0SChristoph Lameter 	 */
92962695a84SNick Piggin 	if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
93062695a84SNick Piggin 		if (!isolate_lru_page(page)) {
93162695a84SNick Piggin 			list_add_tail(&page->lru, pagelist);
9326d9c285aSKOSAKI Motohiro 			inc_zone_page_state(page, NR_ISOLATED_ANON +
9336d9c285aSKOSAKI Motohiro 					    page_is_file_cache(page));
93462695a84SNick Piggin 		}
93562695a84SNick Piggin 	}
9366ce3c4c0SChristoph Lameter }
9376ce3c4c0SChristoph Lameter 
938742755a1SChristoph Lameter static struct page *new_node_page(struct page *page, unsigned long node, int **x)
93995a402c3SChristoph Lameter {
9406484eb3eSMel Gorman 	return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
94195a402c3SChristoph Lameter }
94295a402c3SChristoph Lameter 
9436ce3c4c0SChristoph Lameter /*
9447e2ab150SChristoph Lameter  * Migrate pages from one node to a target node.
9457e2ab150SChristoph Lameter  * Returns error or the number of pages not migrated.
9467e2ab150SChristoph Lameter  */
947dbcb0f19SAdrian Bunk static int migrate_to_node(struct mm_struct *mm, int source, int dest,
948dbcb0f19SAdrian Bunk 			   int flags)
9497e2ab150SChristoph Lameter {
9507e2ab150SChristoph Lameter 	nodemask_t nmask;
9517e2ab150SChristoph Lameter 	LIST_HEAD(pagelist);
9527e2ab150SChristoph Lameter 	int err = 0;
9537e2ab150SChristoph Lameter 
9547e2ab150SChristoph Lameter 	nodes_clear(nmask);
9557e2ab150SChristoph Lameter 	node_set(source, nmask);
9567e2ab150SChristoph Lameter 
95708270807SMinchan Kim 	/*
95808270807SMinchan Kim 	 * This does not "check" the range but isolates all pages that
95908270807SMinchan Kim 	 * need migration.  Between passing in the full user address
96008270807SMinchan Kim 	 * space range and MPOL_MF_DISCONTIG_OK, this call can not fail.
96108270807SMinchan Kim 	 */
96208270807SMinchan Kim 	VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)));
96308270807SMinchan Kim 	check_range(mm, mm->mmap->vm_start, mm->task_size, &nmask,
9647e2ab150SChristoph Lameter 			flags | MPOL_MF_DISCONTIG_OK, &pagelist);
9657e2ab150SChristoph Lameter 
966cf608ac1SMinchan Kim 	if (!list_empty(&pagelist)) {
9677f0f2496SMel Gorman 		err = migrate_pages(&pagelist, new_node_page, dest,
9687b2a2d4aSMel Gorman 							false, MIGRATE_SYNC,
9697b2a2d4aSMel Gorman 							MR_SYSCALL);
970cf608ac1SMinchan Kim 		if (err)
971cf608ac1SMinchan Kim 			putback_lru_pages(&pagelist);
972cf608ac1SMinchan Kim 	}
97395a402c3SChristoph Lameter 
9747e2ab150SChristoph Lameter 	return err;
9757e2ab150SChristoph Lameter }
9767e2ab150SChristoph Lameter 
9777e2ab150SChristoph Lameter /*
9787e2ab150SChristoph Lameter  * Move pages between the two nodesets so as to preserve the physical
9797e2ab150SChristoph Lameter  * layout as much as possible.
98039743889SChristoph Lameter  *
98139743889SChristoph Lameter  * Returns the number of page that could not be moved.
98239743889SChristoph Lameter  */
9830ce72d4fSAndrew Morton int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
9840ce72d4fSAndrew Morton 		     const nodemask_t *to, int flags)
98539743889SChristoph Lameter {
9867e2ab150SChristoph Lameter 	int busy = 0;
9870aedadf9SChristoph Lameter 	int err;
9887e2ab150SChristoph Lameter 	nodemask_t tmp;
98939743889SChristoph Lameter 
9900aedadf9SChristoph Lameter 	err = migrate_prep();
9910aedadf9SChristoph Lameter 	if (err)
9920aedadf9SChristoph Lameter 		return err;
9930aedadf9SChristoph Lameter 
99439743889SChristoph Lameter 	down_read(&mm->mmap_sem);
995d4984711SChristoph Lameter 
9960ce72d4fSAndrew Morton 	err = migrate_vmas(mm, from, to, flags);
9977b2259b3SChristoph Lameter 	if (err)
9987b2259b3SChristoph Lameter 		goto out;
9997b2259b3SChristoph Lameter 
10007e2ab150SChristoph Lameter 	/*
10017e2ab150SChristoph Lameter 	 * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
10027e2ab150SChristoph Lameter 	 * bit in 'to' is not also set in 'tmp'.  Clear the found 'source'
10037e2ab150SChristoph Lameter 	 * bit in 'tmp', and return that <source, dest> pair for migration.
10047e2ab150SChristoph Lameter 	 * The pair of nodemasks 'to' and 'from' define the map.
10057e2ab150SChristoph Lameter 	 *
10067e2ab150SChristoph Lameter 	 * If no pair of bits is found that way, fallback to picking some
10077e2ab150SChristoph Lameter 	 * pair of 'source' and 'dest' bits that are not the same.  If the
10087e2ab150SChristoph Lameter 	 * 'source' and 'dest' bits are the same, this represents a node
10097e2ab150SChristoph Lameter 	 * that will be migrating to itself, so no pages need move.
10107e2ab150SChristoph Lameter 	 *
10117e2ab150SChristoph Lameter 	 * If no bits are left in 'tmp', or if all remaining bits left
10127e2ab150SChristoph Lameter 	 * in 'tmp' correspond to the same bit in 'to', return false
10137e2ab150SChristoph Lameter 	 * (nothing left to migrate).
10147e2ab150SChristoph Lameter 	 *
10157e2ab150SChristoph Lameter 	 * This lets us pick a pair of nodes to migrate between, such that
10167e2ab150SChristoph Lameter 	 * if possible the dest node is not already occupied by some other
10177e2ab150SChristoph Lameter 	 * source node, minimizing the risk of overloading the memory on a
10187e2ab150SChristoph Lameter 	 * node that would happen if we migrated incoming memory to a node
10197e2ab150SChristoph Lameter 	 * before migrating outgoing memory source that same node.
10207e2ab150SChristoph Lameter 	 *
10217e2ab150SChristoph Lameter 	 * A single scan of tmp is sufficient.  As we go, we remember the
10227e2ab150SChristoph Lameter 	 * most recent <s, d> pair that moved (s != d).  If we find a pair
10237e2ab150SChristoph Lameter 	 * that not only moved, but what's better, moved to an empty slot
10247e2ab150SChristoph Lameter 	 * (d is not set in tmp), then we break out then, with that pair.
1025ae0e47f0SJustin P. Mattock 	 * Otherwise when we finish scanning from_tmp, we at least have the
10267e2ab150SChristoph Lameter 	 * most recent <s, d> pair that moved.  If we get all the way through
10277e2ab150SChristoph Lameter 	 * the scan of tmp without finding any node that moved, much less
10287e2ab150SChristoph Lameter 	 * moved to an empty node, then there is nothing left worth migrating.
10297e2ab150SChristoph Lameter 	 */
10307e2ab150SChristoph Lameter 
10310ce72d4fSAndrew Morton 	tmp = *from;
10327e2ab150SChristoph Lameter 	while (!nodes_empty(tmp)) {
10337e2ab150SChristoph Lameter 		int s,d;
10347e2ab150SChristoph Lameter 		int source = -1;
10357e2ab150SChristoph Lameter 		int dest = 0;
10367e2ab150SChristoph Lameter 
10377e2ab150SChristoph Lameter 		for_each_node_mask(s, tmp) {
10384a5b18ccSLarry Woodman 
10394a5b18ccSLarry Woodman 			/*
10404a5b18ccSLarry Woodman 			 * do_migrate_pages() tries to maintain the relative
10414a5b18ccSLarry Woodman 			 * node relationship of the pages established between
10424a5b18ccSLarry Woodman 			 * threads and memory areas.
10434a5b18ccSLarry Woodman                          *
10444a5b18ccSLarry Woodman 			 * However if the number of source nodes is not equal to
10454a5b18ccSLarry Woodman 			 * the number of destination nodes we can not preserve
10464a5b18ccSLarry Woodman 			 * this node relative relationship.  In that case, skip
10474a5b18ccSLarry Woodman 			 * copying memory from a node that is in the destination
10484a5b18ccSLarry Woodman 			 * mask.
10494a5b18ccSLarry Woodman 			 *
10504a5b18ccSLarry Woodman 			 * Example: [2,3,4] -> [3,4,5] moves everything.
10514a5b18ccSLarry Woodman 			 *          [0-7] - > [3,4,5] moves only 0,1,2,6,7.
10524a5b18ccSLarry Woodman 			 */
10534a5b18ccSLarry Woodman 
10540ce72d4fSAndrew Morton 			if ((nodes_weight(*from) != nodes_weight(*to)) &&
10550ce72d4fSAndrew Morton 						(node_isset(s, *to)))
10564a5b18ccSLarry Woodman 				continue;
10574a5b18ccSLarry Woodman 
10580ce72d4fSAndrew Morton 			d = node_remap(s, *from, *to);
10597e2ab150SChristoph Lameter 			if (s == d)
10607e2ab150SChristoph Lameter 				continue;
10617e2ab150SChristoph Lameter 
10627e2ab150SChristoph Lameter 			source = s;	/* Node moved. Memorize */
10637e2ab150SChristoph Lameter 			dest = d;
10647e2ab150SChristoph Lameter 
10657e2ab150SChristoph Lameter 			/* dest not in remaining from nodes? */
10667e2ab150SChristoph Lameter 			if (!node_isset(dest, tmp))
10677e2ab150SChristoph Lameter 				break;
10687e2ab150SChristoph Lameter 		}
10697e2ab150SChristoph Lameter 		if (source == -1)
10707e2ab150SChristoph Lameter 			break;
10717e2ab150SChristoph Lameter 
10727e2ab150SChristoph Lameter 		node_clear(source, tmp);
10737e2ab150SChristoph Lameter 		err = migrate_to_node(mm, source, dest, flags);
10747e2ab150SChristoph Lameter 		if (err > 0)
10757e2ab150SChristoph Lameter 			busy += err;
10767e2ab150SChristoph Lameter 		if (err < 0)
10777e2ab150SChristoph Lameter 			break;
107839743889SChristoph Lameter 	}
10797b2259b3SChristoph Lameter out:
108039743889SChristoph Lameter 	up_read(&mm->mmap_sem);
10817e2ab150SChristoph Lameter 	if (err < 0)
10827e2ab150SChristoph Lameter 		return err;
10837e2ab150SChristoph Lameter 	return busy;
1084b20a3503SChristoph Lameter 
108539743889SChristoph Lameter }
108639743889SChristoph Lameter 
10873ad33b24SLee Schermerhorn /*
10883ad33b24SLee Schermerhorn  * Allocate a new page for page migration based on vma policy.
10893ad33b24SLee Schermerhorn  * Start assuming that page is mapped by vma pointed to by @private.
10903ad33b24SLee Schermerhorn  * Search forward from there, if not.  N.B., this assumes that the
10913ad33b24SLee Schermerhorn  * list of pages handed to migrate_pages()--which is how we get here--
10923ad33b24SLee Schermerhorn  * is in virtual address order.
10933ad33b24SLee Schermerhorn  */
1094742755a1SChristoph Lameter static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
109595a402c3SChristoph Lameter {
109695a402c3SChristoph Lameter 	struct vm_area_struct *vma = (struct vm_area_struct *)private;
10973ad33b24SLee Schermerhorn 	unsigned long uninitialized_var(address);
109895a402c3SChristoph Lameter 
10993ad33b24SLee Schermerhorn 	while (vma) {
11003ad33b24SLee Schermerhorn 		address = page_address_in_vma(page, vma);
11013ad33b24SLee Schermerhorn 		if (address != -EFAULT)
11023ad33b24SLee Schermerhorn 			break;
11033ad33b24SLee Schermerhorn 		vma = vma->vm_next;
11043ad33b24SLee Schermerhorn 	}
11053ad33b24SLee Schermerhorn 
11063ad33b24SLee Schermerhorn 	/*
11073ad33b24SLee Schermerhorn 	 * if !vma, alloc_page_vma() will use task or system default policy
11083ad33b24SLee Schermerhorn 	 */
11093ad33b24SLee Schermerhorn 	return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
111095a402c3SChristoph Lameter }
1111b20a3503SChristoph Lameter #else
1112b20a3503SChristoph Lameter 
1113b20a3503SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
1114b20a3503SChristoph Lameter 				unsigned long flags)
1115b20a3503SChristoph Lameter {
1116b20a3503SChristoph Lameter }
1117b20a3503SChristoph Lameter 
11180ce72d4fSAndrew Morton int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
11190ce72d4fSAndrew Morton 		     const nodemask_t *to, int flags)
1120b20a3503SChristoph Lameter {
1121b20a3503SChristoph Lameter 	return -ENOSYS;
1122b20a3503SChristoph Lameter }
112395a402c3SChristoph Lameter 
112469939749SKeith Owens static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
112595a402c3SChristoph Lameter {
112695a402c3SChristoph Lameter 	return NULL;
112795a402c3SChristoph Lameter }
1128b20a3503SChristoph Lameter #endif
1129b20a3503SChristoph Lameter 
1130dbcb0f19SAdrian Bunk static long do_mbind(unsigned long start, unsigned long len,
1131028fec41SDavid Rientjes 		     unsigned short mode, unsigned short mode_flags,
1132028fec41SDavid Rientjes 		     nodemask_t *nmask, unsigned long flags)
11336ce3c4c0SChristoph Lameter {
11346ce3c4c0SChristoph Lameter 	struct vm_area_struct *vma;
11356ce3c4c0SChristoph Lameter 	struct mm_struct *mm = current->mm;
11366ce3c4c0SChristoph Lameter 	struct mempolicy *new;
11376ce3c4c0SChristoph Lameter 	unsigned long end;
11386ce3c4c0SChristoph Lameter 	int err;
11396ce3c4c0SChristoph Lameter 	LIST_HEAD(pagelist);
11406ce3c4c0SChristoph Lameter 
1141a3b51e01SDavid Rientjes 	if (flags & ~(unsigned long)(MPOL_MF_STRICT |
11426ce3c4c0SChristoph Lameter 				     MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
11436ce3c4c0SChristoph Lameter 		return -EINVAL;
114474c00241SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
11456ce3c4c0SChristoph Lameter 		return -EPERM;
11466ce3c4c0SChristoph Lameter 
11476ce3c4c0SChristoph Lameter 	if (start & ~PAGE_MASK)
11486ce3c4c0SChristoph Lameter 		return -EINVAL;
11496ce3c4c0SChristoph Lameter 
1150d3a71033SLee Schermerhorn 	if (mode == MPOL_DEFAULT || mode == MPOL_NOOP)
11516ce3c4c0SChristoph Lameter 		flags &= ~MPOL_MF_STRICT;
11526ce3c4c0SChristoph Lameter 
11536ce3c4c0SChristoph Lameter 	len = (len + PAGE_SIZE - 1) & PAGE_MASK;
11546ce3c4c0SChristoph Lameter 	end = start + len;
11556ce3c4c0SChristoph Lameter 
11566ce3c4c0SChristoph Lameter 	if (end < start)
11576ce3c4c0SChristoph Lameter 		return -EINVAL;
11586ce3c4c0SChristoph Lameter 	if (end == start)
11596ce3c4c0SChristoph Lameter 		return 0;
11606ce3c4c0SChristoph Lameter 
1161028fec41SDavid Rientjes 	new = mpol_new(mode, mode_flags, nmask);
11626ce3c4c0SChristoph Lameter 	if (IS_ERR(new))
11636ce3c4c0SChristoph Lameter 		return PTR_ERR(new);
11646ce3c4c0SChristoph Lameter 
11656ce3c4c0SChristoph Lameter 	/*
11666ce3c4c0SChristoph Lameter 	 * If we are using the default policy then operation
11676ce3c4c0SChristoph Lameter 	 * on discontinuous address spaces is okay after all
11686ce3c4c0SChristoph Lameter 	 */
11696ce3c4c0SChristoph Lameter 	if (!new)
11706ce3c4c0SChristoph Lameter 		flags |= MPOL_MF_DISCONTIG_OK;
11716ce3c4c0SChristoph Lameter 
1172028fec41SDavid Rientjes 	pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
1173028fec41SDavid Rientjes 		 start, start + len, mode, mode_flags,
1174028fec41SDavid Rientjes 		 nmask ? nodes_addr(*nmask)[0] : -1);
11756ce3c4c0SChristoph Lameter 
11760aedadf9SChristoph Lameter 	if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
11770aedadf9SChristoph Lameter 
11780aedadf9SChristoph Lameter 		err = migrate_prep();
11790aedadf9SChristoph Lameter 		if (err)
1180b05ca738SKOSAKI Motohiro 			goto mpol_out;
11810aedadf9SChristoph Lameter 	}
11824bfc4495SKAMEZAWA Hiroyuki 	{
11834bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
11844bfc4495SKAMEZAWA Hiroyuki 		if (scratch) {
11856ce3c4c0SChristoph Lameter 			down_write(&mm->mmap_sem);
118658568d2aSMiao Xie 			task_lock(current);
11874bfc4495SKAMEZAWA Hiroyuki 			err = mpol_set_nodemask(new, nmask, scratch);
118858568d2aSMiao Xie 			task_unlock(current);
11894bfc4495SKAMEZAWA Hiroyuki 			if (err)
119058568d2aSMiao Xie 				up_write(&mm->mmap_sem);
11914bfc4495SKAMEZAWA Hiroyuki 		} else
11924bfc4495SKAMEZAWA Hiroyuki 			err = -ENOMEM;
11934bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
11944bfc4495SKAMEZAWA Hiroyuki 	}
1195b05ca738SKOSAKI Motohiro 	if (err)
1196b05ca738SKOSAKI Motohiro 		goto mpol_out;
1197b05ca738SKOSAKI Motohiro 
11986ce3c4c0SChristoph Lameter 	vma = check_range(mm, start, end, nmask,
11996ce3c4c0SChristoph Lameter 			  flags | MPOL_MF_INVERT, &pagelist);
12006ce3c4c0SChristoph Lameter 
12016ce3c4c0SChristoph Lameter 	err = PTR_ERR(vma);
12026ce3c4c0SChristoph Lameter 	if (!IS_ERR(vma)) {
12036ce3c4c0SChristoph Lameter 		int nr_failed = 0;
12046ce3c4c0SChristoph Lameter 
12059d8cebd4SKOSAKI Motohiro 		err = mbind_range(mm, start, end, new);
12067e2ab150SChristoph Lameter 
1207cf608ac1SMinchan Kim 		if (!list_empty(&pagelist)) {
120895a402c3SChristoph Lameter 			nr_failed = migrate_pages(&pagelist, new_vma_page,
12097f0f2496SMel Gorman 						(unsigned long)vma,
12107b2a2d4aSMel Gorman 						false, MIGRATE_SYNC,
12117b2a2d4aSMel Gorman 						MR_MEMPOLICY_MBIND);
1212cf608ac1SMinchan Kim 			if (nr_failed)
1213cf608ac1SMinchan Kim 				putback_lru_pages(&pagelist);
1214cf608ac1SMinchan Kim 		}
12156ce3c4c0SChristoph Lameter 
12166ce3c4c0SChristoph Lameter 		if (!err && nr_failed && (flags & MPOL_MF_STRICT))
12176ce3c4c0SChristoph Lameter 			err = -EIO;
1218ab8a3e14SKOSAKI Motohiro 	} else
1219ab8a3e14SKOSAKI Motohiro 		putback_lru_pages(&pagelist);
1220b20a3503SChristoph Lameter 
12216ce3c4c0SChristoph Lameter 	up_write(&mm->mmap_sem);
1222b05ca738SKOSAKI Motohiro  mpol_out:
1223f0be3d32SLee Schermerhorn 	mpol_put(new);
12246ce3c4c0SChristoph Lameter 	return err;
12256ce3c4c0SChristoph Lameter }
12266ce3c4c0SChristoph Lameter 
122739743889SChristoph Lameter /*
12288bccd85fSChristoph Lameter  * User space interface with variable sized bitmaps for nodelists.
12298bccd85fSChristoph Lameter  */
12308bccd85fSChristoph Lameter 
12318bccd85fSChristoph Lameter /* Copy a node mask from user space. */
123239743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
12338bccd85fSChristoph Lameter 		     unsigned long maxnode)
12348bccd85fSChristoph Lameter {
12358bccd85fSChristoph Lameter 	unsigned long k;
12368bccd85fSChristoph Lameter 	unsigned long nlongs;
12378bccd85fSChristoph Lameter 	unsigned long endmask;
12388bccd85fSChristoph Lameter 
12398bccd85fSChristoph Lameter 	--maxnode;
12408bccd85fSChristoph Lameter 	nodes_clear(*nodes);
12418bccd85fSChristoph Lameter 	if (maxnode == 0 || !nmask)
12428bccd85fSChristoph Lameter 		return 0;
1243a9c930baSAndi Kleen 	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
1244636f13c1SChris Wright 		return -EINVAL;
12458bccd85fSChristoph Lameter 
12468bccd85fSChristoph Lameter 	nlongs = BITS_TO_LONGS(maxnode);
12478bccd85fSChristoph Lameter 	if ((maxnode % BITS_PER_LONG) == 0)
12488bccd85fSChristoph Lameter 		endmask = ~0UL;
12498bccd85fSChristoph Lameter 	else
12508bccd85fSChristoph Lameter 		endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
12518bccd85fSChristoph Lameter 
12528bccd85fSChristoph Lameter 	/* When the user specified more nodes than supported just check
12538bccd85fSChristoph Lameter 	   if the non supported part is all zero. */
12548bccd85fSChristoph Lameter 	if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
12558bccd85fSChristoph Lameter 		if (nlongs > PAGE_SIZE/sizeof(long))
12568bccd85fSChristoph Lameter 			return -EINVAL;
12578bccd85fSChristoph Lameter 		for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
12588bccd85fSChristoph Lameter 			unsigned long t;
12598bccd85fSChristoph Lameter 			if (get_user(t, nmask + k))
12608bccd85fSChristoph Lameter 				return -EFAULT;
12618bccd85fSChristoph Lameter 			if (k == nlongs - 1) {
12628bccd85fSChristoph Lameter 				if (t & endmask)
12638bccd85fSChristoph Lameter 					return -EINVAL;
12648bccd85fSChristoph Lameter 			} else if (t)
12658bccd85fSChristoph Lameter 				return -EINVAL;
12668bccd85fSChristoph Lameter 		}
12678bccd85fSChristoph Lameter 		nlongs = BITS_TO_LONGS(MAX_NUMNODES);
12688bccd85fSChristoph Lameter 		endmask = ~0UL;
12698bccd85fSChristoph Lameter 	}
12708bccd85fSChristoph Lameter 
12718bccd85fSChristoph Lameter 	if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
12728bccd85fSChristoph Lameter 		return -EFAULT;
12738bccd85fSChristoph Lameter 	nodes_addr(*nodes)[nlongs-1] &= endmask;
12748bccd85fSChristoph Lameter 	return 0;
12758bccd85fSChristoph Lameter }
12768bccd85fSChristoph Lameter 
12778bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */
12788bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
12798bccd85fSChristoph Lameter 			      nodemask_t *nodes)
12808bccd85fSChristoph Lameter {
12818bccd85fSChristoph Lameter 	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
12828bccd85fSChristoph Lameter 	const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
12838bccd85fSChristoph Lameter 
12848bccd85fSChristoph Lameter 	if (copy > nbytes) {
12858bccd85fSChristoph Lameter 		if (copy > PAGE_SIZE)
12868bccd85fSChristoph Lameter 			return -EINVAL;
12878bccd85fSChristoph Lameter 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
12888bccd85fSChristoph Lameter 			return -EFAULT;
12898bccd85fSChristoph Lameter 		copy = nbytes;
12908bccd85fSChristoph Lameter 	}
12918bccd85fSChristoph Lameter 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
12928bccd85fSChristoph Lameter }
12938bccd85fSChristoph Lameter 
1294938bb9f5SHeiko Carstens SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
1295938bb9f5SHeiko Carstens 		unsigned long, mode, unsigned long __user *, nmask,
1296938bb9f5SHeiko Carstens 		unsigned long, maxnode, unsigned, flags)
12978bccd85fSChristoph Lameter {
12988bccd85fSChristoph Lameter 	nodemask_t nodes;
12998bccd85fSChristoph Lameter 	int err;
1300028fec41SDavid Rientjes 	unsigned short mode_flags;
13018bccd85fSChristoph Lameter 
1302028fec41SDavid Rientjes 	mode_flags = mode & MPOL_MODE_FLAGS;
1303028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1304a3b51e01SDavid Rientjes 	if (mode >= MPOL_MAX)
1305a3b51e01SDavid Rientjes 		return -EINVAL;
13064c50bc01SDavid Rientjes 	if ((mode_flags & MPOL_F_STATIC_NODES) &&
13074c50bc01SDavid Rientjes 	    (mode_flags & MPOL_F_RELATIVE_NODES))
13084c50bc01SDavid Rientjes 		return -EINVAL;
13098bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
13108bccd85fSChristoph Lameter 	if (err)
13118bccd85fSChristoph Lameter 		return err;
1312028fec41SDavid Rientjes 	return do_mbind(start, len, mode, mode_flags, &nodes, flags);
13138bccd85fSChristoph Lameter }
13148bccd85fSChristoph Lameter 
13158bccd85fSChristoph Lameter /* Set the process memory policy */
1316938bb9f5SHeiko Carstens SYSCALL_DEFINE3(set_mempolicy, int, mode, unsigned long __user *, nmask,
1317938bb9f5SHeiko Carstens 		unsigned long, maxnode)
13188bccd85fSChristoph Lameter {
13198bccd85fSChristoph Lameter 	int err;
13208bccd85fSChristoph Lameter 	nodemask_t nodes;
1321028fec41SDavid Rientjes 	unsigned short flags;
13228bccd85fSChristoph Lameter 
1323028fec41SDavid Rientjes 	flags = mode & MPOL_MODE_FLAGS;
1324028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1325028fec41SDavid Rientjes 	if ((unsigned int)mode >= MPOL_MAX)
13268bccd85fSChristoph Lameter 		return -EINVAL;
13274c50bc01SDavid Rientjes 	if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
13284c50bc01SDavid Rientjes 		return -EINVAL;
13298bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
13308bccd85fSChristoph Lameter 	if (err)
13318bccd85fSChristoph Lameter 		return err;
1332028fec41SDavid Rientjes 	return do_set_mempolicy(mode, flags, &nodes);
13338bccd85fSChristoph Lameter }
13348bccd85fSChristoph Lameter 
1335938bb9f5SHeiko Carstens SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
1336938bb9f5SHeiko Carstens 		const unsigned long __user *, old_nodes,
1337938bb9f5SHeiko Carstens 		const unsigned long __user *, new_nodes)
133839743889SChristoph Lameter {
1339c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
1340596d7cfaSKOSAKI Motohiro 	struct mm_struct *mm = NULL;
134139743889SChristoph Lameter 	struct task_struct *task;
134239743889SChristoph Lameter 	nodemask_t task_nodes;
134339743889SChristoph Lameter 	int err;
1344596d7cfaSKOSAKI Motohiro 	nodemask_t *old;
1345596d7cfaSKOSAKI Motohiro 	nodemask_t *new;
1346596d7cfaSKOSAKI Motohiro 	NODEMASK_SCRATCH(scratch);
134739743889SChristoph Lameter 
1348596d7cfaSKOSAKI Motohiro 	if (!scratch)
1349596d7cfaSKOSAKI Motohiro 		return -ENOMEM;
135039743889SChristoph Lameter 
1351596d7cfaSKOSAKI Motohiro 	old = &scratch->mask1;
1352596d7cfaSKOSAKI Motohiro 	new = &scratch->mask2;
1353596d7cfaSKOSAKI Motohiro 
1354596d7cfaSKOSAKI Motohiro 	err = get_nodes(old, old_nodes, maxnode);
135539743889SChristoph Lameter 	if (err)
1356596d7cfaSKOSAKI Motohiro 		goto out;
1357596d7cfaSKOSAKI Motohiro 
1358596d7cfaSKOSAKI Motohiro 	err = get_nodes(new, new_nodes, maxnode);
1359596d7cfaSKOSAKI Motohiro 	if (err)
1360596d7cfaSKOSAKI Motohiro 		goto out;
136139743889SChristoph Lameter 
136239743889SChristoph Lameter 	/* Find the mm_struct */
136355cfaa3cSZeng Zhaoming 	rcu_read_lock();
1364228ebcbeSPavel Emelyanov 	task = pid ? find_task_by_vpid(pid) : current;
136539743889SChristoph Lameter 	if (!task) {
136655cfaa3cSZeng Zhaoming 		rcu_read_unlock();
1367596d7cfaSKOSAKI Motohiro 		err = -ESRCH;
1368596d7cfaSKOSAKI Motohiro 		goto out;
136939743889SChristoph Lameter 	}
13703268c63eSChristoph Lameter 	get_task_struct(task);
137139743889SChristoph Lameter 
1372596d7cfaSKOSAKI Motohiro 	err = -EINVAL;
137339743889SChristoph Lameter 
137439743889SChristoph Lameter 	/*
137539743889SChristoph Lameter 	 * Check if this process has the right to modify the specified
137639743889SChristoph Lameter 	 * process. The right exists if the process has administrative
13777f927fccSAlexey Dobriyan 	 * capabilities, superuser privileges or the same
137839743889SChristoph Lameter 	 * userid as the target process.
137939743889SChristoph Lameter 	 */
1380c69e8d9cSDavid Howells 	tcred = __task_cred(task);
1381b38a86ebSEric W. Biederman 	if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1382b38a86ebSEric W. Biederman 	    !uid_eq(cred->uid,  tcred->suid) && !uid_eq(cred->uid,  tcred->uid) &&
138374c00241SChristoph Lameter 	    !capable(CAP_SYS_NICE)) {
1384c69e8d9cSDavid Howells 		rcu_read_unlock();
138539743889SChristoph Lameter 		err = -EPERM;
13863268c63eSChristoph Lameter 		goto out_put;
138739743889SChristoph Lameter 	}
1388c69e8d9cSDavid Howells 	rcu_read_unlock();
138939743889SChristoph Lameter 
139039743889SChristoph Lameter 	task_nodes = cpuset_mems_allowed(task);
139139743889SChristoph Lameter 	/* Is the user allowed to access the target nodes? */
1392596d7cfaSKOSAKI Motohiro 	if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
139339743889SChristoph Lameter 		err = -EPERM;
13943268c63eSChristoph Lameter 		goto out_put;
139539743889SChristoph Lameter 	}
139639743889SChristoph Lameter 
1397596d7cfaSKOSAKI Motohiro 	if (!nodes_subset(*new, node_states[N_HIGH_MEMORY])) {
13983b42d28bSChristoph Lameter 		err = -EINVAL;
13993268c63eSChristoph Lameter 		goto out_put;
14003b42d28bSChristoph Lameter 	}
14013b42d28bSChristoph Lameter 
140286c3a764SDavid Quigley 	err = security_task_movememory(task);
140386c3a764SDavid Quigley 	if (err)
14043268c63eSChristoph Lameter 		goto out_put;
140586c3a764SDavid Quigley 
14063268c63eSChristoph Lameter 	mm = get_task_mm(task);
14073268c63eSChristoph Lameter 	put_task_struct(task);
1408f2a9ef88SSasha Levin 
1409f2a9ef88SSasha Levin 	if (!mm) {
1410f2a9ef88SSasha Levin 		err = -EINVAL;
1411f2a9ef88SSasha Levin 		goto out;
1412f2a9ef88SSasha Levin 	}
1413f2a9ef88SSasha Levin 
1414596d7cfaSKOSAKI Motohiro 	err = do_migrate_pages(mm, old, new,
141574c00241SChristoph Lameter 		capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
14163268c63eSChristoph Lameter 
141739743889SChristoph Lameter 	mmput(mm);
14183268c63eSChristoph Lameter out:
1419596d7cfaSKOSAKI Motohiro 	NODEMASK_SCRATCH_FREE(scratch);
1420596d7cfaSKOSAKI Motohiro 
142139743889SChristoph Lameter 	return err;
14223268c63eSChristoph Lameter 
14233268c63eSChristoph Lameter out_put:
14243268c63eSChristoph Lameter 	put_task_struct(task);
14253268c63eSChristoph Lameter 	goto out;
14263268c63eSChristoph Lameter 
142739743889SChristoph Lameter }
142839743889SChristoph Lameter 
142939743889SChristoph Lameter 
14308bccd85fSChristoph Lameter /* Retrieve NUMA policy */
1431938bb9f5SHeiko Carstens SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1432938bb9f5SHeiko Carstens 		unsigned long __user *, nmask, unsigned long, maxnode,
1433938bb9f5SHeiko Carstens 		unsigned long, addr, unsigned long, flags)
14348bccd85fSChristoph Lameter {
1435dbcb0f19SAdrian Bunk 	int err;
1436dbcb0f19SAdrian Bunk 	int uninitialized_var(pval);
14378bccd85fSChristoph Lameter 	nodemask_t nodes;
14388bccd85fSChristoph Lameter 
14398bccd85fSChristoph Lameter 	if (nmask != NULL && maxnode < MAX_NUMNODES)
14408bccd85fSChristoph Lameter 		return -EINVAL;
14418bccd85fSChristoph Lameter 
14428bccd85fSChristoph Lameter 	err = do_get_mempolicy(&pval, &nodes, addr, flags);
14438bccd85fSChristoph Lameter 
14448bccd85fSChristoph Lameter 	if (err)
14458bccd85fSChristoph Lameter 		return err;
14468bccd85fSChristoph Lameter 
14478bccd85fSChristoph Lameter 	if (policy && put_user(pval, policy))
14488bccd85fSChristoph Lameter 		return -EFAULT;
14498bccd85fSChristoph Lameter 
14508bccd85fSChristoph Lameter 	if (nmask)
14518bccd85fSChristoph Lameter 		err = copy_nodes_to_user(nmask, maxnode, &nodes);
14528bccd85fSChristoph Lameter 
14538bccd85fSChristoph Lameter 	return err;
14548bccd85fSChristoph Lameter }
14558bccd85fSChristoph Lameter 
14561da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy,
14591da177e4SLinus Torvalds 				     compat_ulong_t __user *nmask,
14601da177e4SLinus Torvalds 				     compat_ulong_t maxnode,
14611da177e4SLinus Torvalds 				     compat_ulong_t addr, compat_ulong_t flags)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	long err;
14641da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
14651da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
14661da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
14691da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	if (nmask)
14721da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	if (!err && nmask) {
14772bbff6c7SKAMEZAWA Hiroyuki 		unsigned long copy_size;
14782bbff6c7SKAMEZAWA Hiroyuki 		copy_size = min_t(unsigned long, sizeof(bm), alloc_size);
14792bbff6c7SKAMEZAWA Hiroyuki 		err = copy_from_user(bm, nm, copy_size);
14801da177e4SLinus Torvalds 		/* ensure entire bitmap is zeroed */
14811da177e4SLinus Torvalds 		err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
14821da177e4SLinus Torvalds 		err |= compat_put_bitmap(nmask, bm, nr_bits);
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	return err;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
14891da177e4SLinus Torvalds 				     compat_ulong_t maxnode)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	long err = 0;
14921da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
14931da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
14941da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
14971da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	if (nmask) {
15001da177e4SLinus Torvalds 		err = compat_get_bitmap(bm, nmask, nr_bits);
15011da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
15021da177e4SLinus Torvalds 		err |= copy_to_user(nm, bm, alloc_size);
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (err)
15061da177e4SLinus Torvalds 		return -EFAULT;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	return sys_set_mempolicy(mode, nm, nr_bits+1);
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
15121da177e4SLinus Torvalds 			     compat_ulong_t mode, compat_ulong_t __user *nmask,
15131da177e4SLinus Torvalds 			     compat_ulong_t maxnode, compat_ulong_t flags)
15141da177e4SLinus Torvalds {
15151da177e4SLinus Torvalds 	long err = 0;
15161da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
15171da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
1518dfcd3c0dSAndi Kleen 	nodemask_t bm;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
15211da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	if (nmask) {
1524dfcd3c0dSAndi Kleen 		err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
15251da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
1526dfcd3c0dSAndi Kleen 		err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
15271da177e4SLinus Torvalds 	}
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	if (err)
15301da177e4SLinus Torvalds 		return -EFAULT;
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds #endif
15361da177e4SLinus Torvalds 
1537480eccf9SLee Schermerhorn /*
1538480eccf9SLee Schermerhorn  * get_vma_policy(@task, @vma, @addr)
1539480eccf9SLee Schermerhorn  * @task - task for fallback if vma policy == default
1540480eccf9SLee Schermerhorn  * @vma   - virtual memory area whose policy is sought
1541480eccf9SLee Schermerhorn  * @addr  - address in @vma for shared policy lookup
1542480eccf9SLee Schermerhorn  *
1543480eccf9SLee Schermerhorn  * Returns effective policy for a VMA at specified address.
1544480eccf9SLee Schermerhorn  * Falls back to @task or system default policy, as necessary.
154532f8516aSDavid Rientjes  * Current or other task's task mempolicy and non-shared vma policies must be
154632f8516aSDavid Rientjes  * protected by task_lock(task) by the caller.
154752cd3b07SLee Schermerhorn  * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
154852cd3b07SLee Schermerhorn  * count--added by the get_policy() vm_op, as appropriate--to protect against
154952cd3b07SLee Schermerhorn  * freeing by another task.  It is the caller's responsibility to free the
155052cd3b07SLee Schermerhorn  * extra reference for shared policies.
1551480eccf9SLee Schermerhorn  */
1552d98f6cb6SStephen Wilson struct mempolicy *get_vma_policy(struct task_struct *task,
155348fce342SChristoph Lameter 		struct vm_area_struct *vma, unsigned long addr)
15541da177e4SLinus Torvalds {
15556e21c8f1SChristoph Lameter 	struct mempolicy *pol = task->mempolicy;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (vma) {
1558480eccf9SLee Schermerhorn 		if (vma->vm_ops && vma->vm_ops->get_policy) {
1559ae4d8c16SLee Schermerhorn 			struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
1560ae4d8c16SLee Schermerhorn 									addr);
1561ae4d8c16SLee Schermerhorn 			if (vpol)
1562ae4d8c16SLee Schermerhorn 				pol = vpol;
156300442ad0SMel Gorman 		} else if (vma->vm_policy) {
15641da177e4SLinus Torvalds 			pol = vma->vm_policy;
156500442ad0SMel Gorman 
156600442ad0SMel Gorman 			/*
156700442ad0SMel Gorman 			 * shmem_alloc_page() passes MPOL_F_SHARED policy with
156800442ad0SMel Gorman 			 * a pseudo vma whose vma->vm_ops=NULL. Take a reference
156900442ad0SMel Gorman 			 * count on these policies which will be dropped by
157000442ad0SMel Gorman 			 * mpol_cond_put() later
157100442ad0SMel Gorman 			 */
157200442ad0SMel Gorman 			if (mpol_needs_cond_ref(pol))
157300442ad0SMel Gorman 				mpol_get(pol);
157400442ad0SMel Gorman 		}
15751da177e4SLinus Torvalds 	}
15761da177e4SLinus Torvalds 	if (!pol)
15771da177e4SLinus Torvalds 		pol = &default_policy;
15781da177e4SLinus Torvalds 	return pol;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
158152cd3b07SLee Schermerhorn /*
158252cd3b07SLee Schermerhorn  * Return a nodemask representing a mempolicy for filtering nodes for
158352cd3b07SLee Schermerhorn  * page allocation
158452cd3b07SLee Schermerhorn  */
158552cd3b07SLee Schermerhorn static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
158619770b32SMel Gorman {
158719770b32SMel Gorman 	/* Lower zones don't get a nodemask applied for MPOL_BIND */
158845c4745aSLee Schermerhorn 	if (unlikely(policy->mode == MPOL_BIND) &&
158919770b32SMel Gorman 			gfp_zone(gfp) >= policy_zone &&
159019770b32SMel Gorman 			cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
159119770b32SMel Gorman 		return &policy->v.nodes;
159219770b32SMel Gorman 
159319770b32SMel Gorman 	return NULL;
159419770b32SMel Gorman }
159519770b32SMel Gorman 
159652cd3b07SLee Schermerhorn /* Return a zonelist indicated by gfp for node representing a mempolicy */
15972f5f9486SAndi Kleen static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy,
15982f5f9486SAndi Kleen 	int nd)
15991da177e4SLinus Torvalds {
160045c4745aSLee Schermerhorn 	switch (policy->mode) {
16011da177e4SLinus Torvalds 	case MPOL_PREFERRED:
1602fc36b8d3SLee Schermerhorn 		if (!(policy->flags & MPOL_F_LOCAL))
16031da177e4SLinus Torvalds 			nd = policy->v.preferred_node;
16041da177e4SLinus Torvalds 		break;
16051da177e4SLinus Torvalds 	case MPOL_BIND:
160619770b32SMel Gorman 		/*
160752cd3b07SLee Schermerhorn 		 * Normally, MPOL_BIND allocations are node-local within the
160852cd3b07SLee Schermerhorn 		 * allowed nodemask.  However, if __GFP_THISNODE is set and the
16096eb27e1fSBob Liu 		 * current node isn't part of the mask, we use the zonelist for
161052cd3b07SLee Schermerhorn 		 * the first node in the mask instead.
161119770b32SMel Gorman 		 */
161219770b32SMel Gorman 		if (unlikely(gfp & __GFP_THISNODE) &&
161319770b32SMel Gorman 				unlikely(!node_isset(nd, policy->v.nodes)))
161419770b32SMel Gorman 			nd = first_node(policy->v.nodes);
161519770b32SMel Gorman 		break;
16161da177e4SLinus Torvalds 	default:
16171da177e4SLinus Torvalds 		BUG();
16181da177e4SLinus Torvalds 	}
16190e88460dSMel Gorman 	return node_zonelist(nd, gfp);
16201da177e4SLinus Torvalds }
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds /* Do dynamic interleaving for a process */
16231da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	unsigned nid, next;
16261da177e4SLinus Torvalds 	struct task_struct *me = current;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	nid = me->il_next;
1629dfcd3c0dSAndi Kleen 	next = next_node(nid, policy->v.nodes);
16301da177e4SLinus Torvalds 	if (next >= MAX_NUMNODES)
1631dfcd3c0dSAndi Kleen 		next = first_node(policy->v.nodes);
1632f5b087b5SDavid Rientjes 	if (next < MAX_NUMNODES)
16331da177e4SLinus Torvalds 		me->il_next = next;
16341da177e4SLinus Torvalds 	return nid;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
1637dc85da15SChristoph Lameter /*
1638dc85da15SChristoph Lameter  * Depending on the memory policy provide a node from which to allocate the
1639dc85da15SChristoph Lameter  * next slab entry.
164052cd3b07SLee Schermerhorn  * @policy must be protected by freeing by the caller.  If @policy is
164152cd3b07SLee Schermerhorn  * the current task's mempolicy, this protection is implicit, as only the
164252cd3b07SLee Schermerhorn  * task can change it's policy.  The system default policy requires no
164352cd3b07SLee Schermerhorn  * such protection.
1644dc85da15SChristoph Lameter  */
1645e7b691b0SAndi Kleen unsigned slab_node(void)
1646dc85da15SChristoph Lameter {
1647e7b691b0SAndi Kleen 	struct mempolicy *policy;
1648e7b691b0SAndi Kleen 
1649e7b691b0SAndi Kleen 	if (in_interrupt())
1650e7b691b0SAndi Kleen 		return numa_node_id();
1651e7b691b0SAndi Kleen 
1652e7b691b0SAndi Kleen 	policy = current->mempolicy;
1653fc36b8d3SLee Schermerhorn 	if (!policy || policy->flags & MPOL_F_LOCAL)
1654bea904d5SLee Schermerhorn 		return numa_node_id();
1655765c4507SChristoph Lameter 
1656bea904d5SLee Schermerhorn 	switch (policy->mode) {
1657bea904d5SLee Schermerhorn 	case MPOL_PREFERRED:
1658fc36b8d3SLee Schermerhorn 		/*
1659fc36b8d3SLee Schermerhorn 		 * handled MPOL_F_LOCAL above
1660fc36b8d3SLee Schermerhorn 		 */
1661bea904d5SLee Schermerhorn 		return policy->v.preferred_node;
1662bea904d5SLee Schermerhorn 
1663dc85da15SChristoph Lameter 	case MPOL_INTERLEAVE:
1664dc85da15SChristoph Lameter 		return interleave_nodes(policy);
1665dc85da15SChristoph Lameter 
1666dd1a239fSMel Gorman 	case MPOL_BIND: {
1667dc85da15SChristoph Lameter 		/*
1668dc85da15SChristoph Lameter 		 * Follow bind policy behavior and start allocation at the
1669dc85da15SChristoph Lameter 		 * first node.
1670dc85da15SChristoph Lameter 		 */
167119770b32SMel Gorman 		struct zonelist *zonelist;
167219770b32SMel Gorman 		struct zone *zone;
167319770b32SMel Gorman 		enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
167419770b32SMel Gorman 		zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0];
167519770b32SMel Gorman 		(void)first_zones_zonelist(zonelist, highest_zoneidx,
167619770b32SMel Gorman 							&policy->v.nodes,
167719770b32SMel Gorman 							&zone);
1678800416f7SEric Dumazet 		return zone ? zone->node : numa_node_id();
1679dd1a239fSMel Gorman 	}
1680dc85da15SChristoph Lameter 
1681dc85da15SChristoph Lameter 	default:
1682bea904d5SLee Schermerhorn 		BUG();
1683dc85da15SChristoph Lameter 	}
1684dc85da15SChristoph Lameter }
1685dc85da15SChristoph Lameter 
16861da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */
16871da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol,
16881da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long off)
16891da177e4SLinus Torvalds {
1690dfcd3c0dSAndi Kleen 	unsigned nnodes = nodes_weight(pol->v.nodes);
1691f5b087b5SDavid Rientjes 	unsigned target;
16921da177e4SLinus Torvalds 	int c;
16931da177e4SLinus Torvalds 	int nid = -1;
16941da177e4SLinus Torvalds 
1695f5b087b5SDavid Rientjes 	if (!nnodes)
1696f5b087b5SDavid Rientjes 		return numa_node_id();
1697f5b087b5SDavid Rientjes 	target = (unsigned int)off % nnodes;
16981da177e4SLinus Torvalds 	c = 0;
16991da177e4SLinus Torvalds 	do {
1700dfcd3c0dSAndi Kleen 		nid = next_node(nid, pol->v.nodes);
17011da177e4SLinus Torvalds 		c++;
17021da177e4SLinus Torvalds 	} while (c <= target);
17031da177e4SLinus Torvalds 	return nid;
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
17065da7ca86SChristoph Lameter /* Determine a node number for interleave */
17075da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol,
17085da7ca86SChristoph Lameter 		 struct vm_area_struct *vma, unsigned long addr, int shift)
17095da7ca86SChristoph Lameter {
17105da7ca86SChristoph Lameter 	if (vma) {
17115da7ca86SChristoph Lameter 		unsigned long off;
17125da7ca86SChristoph Lameter 
17133b98b087SNishanth Aravamudan 		/*
17143b98b087SNishanth Aravamudan 		 * for small pages, there is no difference between
17153b98b087SNishanth Aravamudan 		 * shift and PAGE_SHIFT, so the bit-shift is safe.
17163b98b087SNishanth Aravamudan 		 * for huge pages, since vm_pgoff is in units of small
17173b98b087SNishanth Aravamudan 		 * pages, we need to shift off the always 0 bits to get
17183b98b087SNishanth Aravamudan 		 * a useful offset.
17193b98b087SNishanth Aravamudan 		 */
17203b98b087SNishanth Aravamudan 		BUG_ON(shift < PAGE_SHIFT);
17213b98b087SNishanth Aravamudan 		off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
17225da7ca86SChristoph Lameter 		off += (addr - vma->vm_start) >> shift;
17235da7ca86SChristoph Lameter 		return offset_il_node(pol, vma, off);
17245da7ca86SChristoph Lameter 	} else
17255da7ca86SChristoph Lameter 		return interleave_nodes(pol);
17265da7ca86SChristoph Lameter }
17275da7ca86SChristoph Lameter 
1728778d3b0fSMichal Hocko /*
1729778d3b0fSMichal Hocko  * Return the bit number of a random bit set in the nodemask.
1730778d3b0fSMichal Hocko  * (returns -1 if nodemask is empty)
1731778d3b0fSMichal Hocko  */
1732778d3b0fSMichal Hocko int node_random(const nodemask_t *maskp)
1733778d3b0fSMichal Hocko {
1734778d3b0fSMichal Hocko 	int w, bit = -1;
1735778d3b0fSMichal Hocko 
1736778d3b0fSMichal Hocko 	w = nodes_weight(*maskp);
1737778d3b0fSMichal Hocko 	if (w)
1738778d3b0fSMichal Hocko 		bit = bitmap_ord_to_pos(maskp->bits,
1739778d3b0fSMichal Hocko 			get_random_int() % w, MAX_NUMNODES);
1740778d3b0fSMichal Hocko 	return bit;
1741778d3b0fSMichal Hocko }
1742778d3b0fSMichal Hocko 
174300ac59adSChen, Kenneth W #ifdef CONFIG_HUGETLBFS
1744480eccf9SLee Schermerhorn /*
1745480eccf9SLee Schermerhorn  * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
1746480eccf9SLee Schermerhorn  * @vma = virtual memory area whose policy is sought
1747480eccf9SLee Schermerhorn  * @addr = address in @vma for shared policy lookup and interleave policy
1748480eccf9SLee Schermerhorn  * @gfp_flags = for requested zone
174919770b32SMel Gorman  * @mpol = pointer to mempolicy pointer for reference counted mempolicy
175019770b32SMel Gorman  * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask
1751480eccf9SLee Schermerhorn  *
175252cd3b07SLee Schermerhorn  * Returns a zonelist suitable for a huge page allocation and a pointer
175352cd3b07SLee Schermerhorn  * to the struct mempolicy for conditional unref after allocation.
175452cd3b07SLee Schermerhorn  * If the effective policy is 'BIND, returns a pointer to the mempolicy's
175552cd3b07SLee Schermerhorn  * @nodemask for filtering the zonelist.
1756c0ff7453SMiao Xie  *
1757c0ff7453SMiao Xie  * Must be protected by get_mems_allowed()
1758480eccf9SLee Schermerhorn  */
1759396faf03SMel Gorman struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
176019770b32SMel Gorman 				gfp_t gfp_flags, struct mempolicy **mpol,
176119770b32SMel Gorman 				nodemask_t **nodemask)
17625da7ca86SChristoph Lameter {
1763480eccf9SLee Schermerhorn 	struct zonelist *zl;
17645da7ca86SChristoph Lameter 
176552cd3b07SLee Schermerhorn 	*mpol = get_vma_policy(current, vma, addr);
176619770b32SMel Gorman 	*nodemask = NULL;	/* assume !MPOL_BIND */
17675da7ca86SChristoph Lameter 
176852cd3b07SLee Schermerhorn 	if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
176952cd3b07SLee Schermerhorn 		zl = node_zonelist(interleave_nid(*mpol, vma, addr,
1770a5516438SAndi Kleen 				huge_page_shift(hstate_vma(vma))), gfp_flags);
177152cd3b07SLee Schermerhorn 	} else {
17722f5f9486SAndi Kleen 		zl = policy_zonelist(gfp_flags, *mpol, numa_node_id());
177352cd3b07SLee Schermerhorn 		if ((*mpol)->mode == MPOL_BIND)
177452cd3b07SLee Schermerhorn 			*nodemask = &(*mpol)->v.nodes;
1775480eccf9SLee Schermerhorn 	}
1776480eccf9SLee Schermerhorn 	return zl;
17775da7ca86SChristoph Lameter }
177806808b08SLee Schermerhorn 
177906808b08SLee Schermerhorn /*
178006808b08SLee Schermerhorn  * init_nodemask_of_mempolicy
178106808b08SLee Schermerhorn  *
178206808b08SLee Schermerhorn  * If the current task's mempolicy is "default" [NULL], return 'false'
178306808b08SLee Schermerhorn  * to indicate default policy.  Otherwise, extract the policy nodemask
178406808b08SLee Schermerhorn  * for 'bind' or 'interleave' policy into the argument nodemask, or
178506808b08SLee Schermerhorn  * initialize the argument nodemask to contain the single node for
178606808b08SLee Schermerhorn  * 'preferred' or 'local' policy and return 'true' to indicate presence
178706808b08SLee Schermerhorn  * of non-default mempolicy.
178806808b08SLee Schermerhorn  *
178906808b08SLee Schermerhorn  * We don't bother with reference counting the mempolicy [mpol_get/put]
179006808b08SLee Schermerhorn  * because the current task is examining it's own mempolicy and a task's
179106808b08SLee Schermerhorn  * mempolicy is only ever changed by the task itself.
179206808b08SLee Schermerhorn  *
179306808b08SLee Schermerhorn  * N.B., it is the caller's responsibility to free a returned nodemask.
179406808b08SLee Schermerhorn  */
179506808b08SLee Schermerhorn bool init_nodemask_of_mempolicy(nodemask_t *mask)
179606808b08SLee Schermerhorn {
179706808b08SLee Schermerhorn 	struct mempolicy *mempolicy;
179806808b08SLee Schermerhorn 	int nid;
179906808b08SLee Schermerhorn 
180006808b08SLee Schermerhorn 	if (!(mask && current->mempolicy))
180106808b08SLee Schermerhorn 		return false;
180206808b08SLee Schermerhorn 
1803c0ff7453SMiao Xie 	task_lock(current);
180406808b08SLee Schermerhorn 	mempolicy = current->mempolicy;
180506808b08SLee Schermerhorn 	switch (mempolicy->mode) {
180606808b08SLee Schermerhorn 	case MPOL_PREFERRED:
180706808b08SLee Schermerhorn 		if (mempolicy->flags & MPOL_F_LOCAL)
180806808b08SLee Schermerhorn 			nid = numa_node_id();
180906808b08SLee Schermerhorn 		else
181006808b08SLee Schermerhorn 			nid = mempolicy->v.preferred_node;
181106808b08SLee Schermerhorn 		init_nodemask_of_node(mask, nid);
181206808b08SLee Schermerhorn 		break;
181306808b08SLee Schermerhorn 
181406808b08SLee Schermerhorn 	case MPOL_BIND:
181506808b08SLee Schermerhorn 		/* Fall through */
181606808b08SLee Schermerhorn 	case MPOL_INTERLEAVE:
181706808b08SLee Schermerhorn 		*mask =  mempolicy->v.nodes;
181806808b08SLee Schermerhorn 		break;
181906808b08SLee Schermerhorn 
182006808b08SLee Schermerhorn 	default:
182106808b08SLee Schermerhorn 		BUG();
182206808b08SLee Schermerhorn 	}
1823c0ff7453SMiao Xie 	task_unlock(current);
182406808b08SLee Schermerhorn 
182506808b08SLee Schermerhorn 	return true;
182606808b08SLee Schermerhorn }
182700ac59adSChen, Kenneth W #endif
18285da7ca86SChristoph Lameter 
18296f48d0ebSDavid Rientjes /*
18306f48d0ebSDavid Rientjes  * mempolicy_nodemask_intersects
18316f48d0ebSDavid Rientjes  *
18326f48d0ebSDavid Rientjes  * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
18336f48d0ebSDavid Rientjes  * policy.  Otherwise, check for intersection between mask and the policy
18346f48d0ebSDavid Rientjes  * nodemask for 'bind' or 'interleave' policy.  For 'perferred' or 'local'
18356f48d0ebSDavid Rientjes  * policy, always return true since it may allocate elsewhere on fallback.
18366f48d0ebSDavid Rientjes  *
18376f48d0ebSDavid Rientjes  * Takes task_lock(tsk) to prevent freeing of its mempolicy.
18386f48d0ebSDavid Rientjes  */
18396f48d0ebSDavid Rientjes bool mempolicy_nodemask_intersects(struct task_struct *tsk,
18406f48d0ebSDavid Rientjes 					const nodemask_t *mask)
18416f48d0ebSDavid Rientjes {
18426f48d0ebSDavid Rientjes 	struct mempolicy *mempolicy;
18436f48d0ebSDavid Rientjes 	bool ret = true;
18446f48d0ebSDavid Rientjes 
18456f48d0ebSDavid Rientjes 	if (!mask)
18466f48d0ebSDavid Rientjes 		return ret;
18476f48d0ebSDavid Rientjes 	task_lock(tsk);
18486f48d0ebSDavid Rientjes 	mempolicy = tsk->mempolicy;
18496f48d0ebSDavid Rientjes 	if (!mempolicy)
18506f48d0ebSDavid Rientjes 		goto out;
18516f48d0ebSDavid Rientjes 
18526f48d0ebSDavid Rientjes 	switch (mempolicy->mode) {
18536f48d0ebSDavid Rientjes 	case MPOL_PREFERRED:
18546f48d0ebSDavid Rientjes 		/*
18556f48d0ebSDavid Rientjes 		 * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
18566f48d0ebSDavid Rientjes 		 * allocate from, they may fallback to other nodes when oom.
18576f48d0ebSDavid Rientjes 		 * Thus, it's possible for tsk to have allocated memory from
18586f48d0ebSDavid Rientjes 		 * nodes in mask.
18596f48d0ebSDavid Rientjes 		 */
18606f48d0ebSDavid Rientjes 		break;
18616f48d0ebSDavid Rientjes 	case MPOL_BIND:
18626f48d0ebSDavid Rientjes 	case MPOL_INTERLEAVE:
18636f48d0ebSDavid Rientjes 		ret = nodes_intersects(mempolicy->v.nodes, *mask);
18646f48d0ebSDavid Rientjes 		break;
18656f48d0ebSDavid Rientjes 	default:
18666f48d0ebSDavid Rientjes 		BUG();
18676f48d0ebSDavid Rientjes 	}
18686f48d0ebSDavid Rientjes out:
18696f48d0ebSDavid Rientjes 	task_unlock(tsk);
18706f48d0ebSDavid Rientjes 	return ret;
18716f48d0ebSDavid Rientjes }
18726f48d0ebSDavid Rientjes 
18731da177e4SLinus Torvalds /* Allocate a page in interleaved policy.
18741da177e4SLinus Torvalds    Own path because it needs to do special accounting. */
1875662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1876662f3a0bSAndi Kleen 					unsigned nid)
18771da177e4SLinus Torvalds {
18781da177e4SLinus Torvalds 	struct zonelist *zl;
18791da177e4SLinus Torvalds 	struct page *page;
18801da177e4SLinus Torvalds 
18810e88460dSMel Gorman 	zl = node_zonelist(nid, gfp);
18821da177e4SLinus Torvalds 	page = __alloc_pages(gfp, order, zl);
1883dd1a239fSMel Gorman 	if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
1884ca889e6cSChristoph Lameter 		inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
18851da177e4SLinus Torvalds 	return page;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds /**
18890bbbc0b3SAndrea Arcangeli  * 	alloc_pages_vma	- Allocate a page for a VMA.
18901da177e4SLinus Torvalds  *
18911da177e4SLinus Torvalds  * 	@gfp:
18921da177e4SLinus Torvalds  *      %GFP_USER    user allocation.
18931da177e4SLinus Torvalds  *      %GFP_KERNEL  kernel allocations,
18941da177e4SLinus Torvalds  *      %GFP_HIGHMEM highmem/user allocations,
18951da177e4SLinus Torvalds  *      %GFP_FS      allocation should not call back into a file system.
18961da177e4SLinus Torvalds  *      %GFP_ATOMIC  don't sleep.
18971da177e4SLinus Torvalds  *
18980bbbc0b3SAndrea Arcangeli  *	@order:Order of the GFP allocation.
18991da177e4SLinus Torvalds  * 	@vma:  Pointer to VMA or NULL if not available.
19001da177e4SLinus Torvalds  *	@addr: Virtual Address of the allocation. Must be inside the VMA.
19011da177e4SLinus Torvalds  *
19021da177e4SLinus Torvalds  * 	This function allocates a page from the kernel page pool and applies
19031da177e4SLinus Torvalds  *	a NUMA policy associated with the VMA or the current process.
19041da177e4SLinus Torvalds  *	When VMA is not NULL caller must hold down_read on the mmap_sem of the
19051da177e4SLinus Torvalds  *	mm_struct of the VMA to prevent it from going away. Should be used for
19061da177e4SLinus Torvalds  *	all allocations for pages that will be mapped into
19071da177e4SLinus Torvalds  * 	user space. Returns NULL when no page can be allocated.
19081da177e4SLinus Torvalds  *
19091da177e4SLinus Torvalds  *	Should be called with the mm_sem of the vma hold.
19101da177e4SLinus Torvalds  */
19111da177e4SLinus Torvalds struct page *
19120bbbc0b3SAndrea Arcangeli alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
19132f5f9486SAndi Kleen 		unsigned long addr, int node)
19141da177e4SLinus Torvalds {
1915cc9a6c87SMel Gorman 	struct mempolicy *pol;
1916480eccf9SLee Schermerhorn 	struct zonelist *zl;
1917c0ff7453SMiao Xie 	struct page *page;
1918cc9a6c87SMel Gorman 	unsigned int cpuset_mems_cookie;
19191da177e4SLinus Torvalds 
1920cc9a6c87SMel Gorman retry_cpuset:
1921cc9a6c87SMel Gorman 	pol = get_vma_policy(current, vma, addr);
1922cc9a6c87SMel Gorman 	cpuset_mems_cookie = get_mems_allowed();
1923cc9a6c87SMel Gorman 
192445c4745aSLee Schermerhorn 	if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
19251da177e4SLinus Torvalds 		unsigned nid;
19265da7ca86SChristoph Lameter 
19278eac563cSAndi Kleen 		nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
192852cd3b07SLee Schermerhorn 		mpol_cond_put(pol);
19290bbbc0b3SAndrea Arcangeli 		page = alloc_page_interleave(gfp, order, nid);
1930cc9a6c87SMel Gorman 		if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1931cc9a6c87SMel Gorman 			goto retry_cpuset;
1932cc9a6c87SMel Gorman 
1933c0ff7453SMiao Xie 		return page;
19341da177e4SLinus Torvalds 	}
19352f5f9486SAndi Kleen 	zl = policy_zonelist(gfp, pol, node);
193652cd3b07SLee Schermerhorn 	if (unlikely(mpol_needs_cond_ref(pol))) {
1937480eccf9SLee Schermerhorn 		/*
193852cd3b07SLee Schermerhorn 		 * slow path: ref counted shared policy
1939480eccf9SLee Schermerhorn 		 */
19400bbbc0b3SAndrea Arcangeli 		struct page *page =  __alloc_pages_nodemask(gfp, order,
194152cd3b07SLee Schermerhorn 						zl, policy_nodemask(gfp, pol));
1942f0be3d32SLee Schermerhorn 		__mpol_put(pol);
1943cc9a6c87SMel Gorman 		if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1944cc9a6c87SMel Gorman 			goto retry_cpuset;
1945480eccf9SLee Schermerhorn 		return page;
1946480eccf9SLee Schermerhorn 	}
1947480eccf9SLee Schermerhorn 	/*
1948480eccf9SLee Schermerhorn 	 * fast path:  default or task policy
1949480eccf9SLee Schermerhorn 	 */
19500bbbc0b3SAndrea Arcangeli 	page = __alloc_pages_nodemask(gfp, order, zl,
19510bbbc0b3SAndrea Arcangeli 				      policy_nodemask(gfp, pol));
1952cc9a6c87SMel Gorman 	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1953cc9a6c87SMel Gorman 		goto retry_cpuset;
1954c0ff7453SMiao Xie 	return page;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds /**
19581da177e4SLinus Torvalds  * 	alloc_pages_current - Allocate pages.
19591da177e4SLinus Torvalds  *
19601da177e4SLinus Torvalds  *	@gfp:
19611da177e4SLinus Torvalds  *		%GFP_USER   user allocation,
19621da177e4SLinus Torvalds  *      	%GFP_KERNEL kernel allocation,
19631da177e4SLinus Torvalds  *      	%GFP_HIGHMEM highmem allocation,
19641da177e4SLinus Torvalds  *      	%GFP_FS     don't call back into a file system.
19651da177e4SLinus Torvalds  *      	%GFP_ATOMIC don't sleep.
19661da177e4SLinus Torvalds  *	@order: Power of two of allocation size in pages. 0 is a single page.
19671da177e4SLinus Torvalds  *
19681da177e4SLinus Torvalds  *	Allocate a page from the kernel page pool.  When not in
19691da177e4SLinus Torvalds  *	interrupt context and apply the current process NUMA policy.
19701da177e4SLinus Torvalds  *	Returns NULL when no page can be allocated.
19711da177e4SLinus Torvalds  *
1972cf2a473cSPaul Jackson  *	Don't call cpuset_update_task_memory_state() unless
19731da177e4SLinus Torvalds  *	1) it's ok to take cpuset_sem (can WAIT), and
19741da177e4SLinus Torvalds  *	2) allocating for current task (not interrupt).
19751da177e4SLinus Torvalds  */
1976dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order)
19771da177e4SLinus Torvalds {
19781da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
1979c0ff7453SMiao Xie 	struct page *page;
1980cc9a6c87SMel Gorman 	unsigned int cpuset_mems_cookie;
19811da177e4SLinus Torvalds 
19829b819d20SChristoph Lameter 	if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
19831da177e4SLinus Torvalds 		pol = &default_policy;
198452cd3b07SLee Schermerhorn 
1985cc9a6c87SMel Gorman retry_cpuset:
1986cc9a6c87SMel Gorman 	cpuset_mems_cookie = get_mems_allowed();
1987cc9a6c87SMel Gorman 
198852cd3b07SLee Schermerhorn 	/*
198952cd3b07SLee Schermerhorn 	 * No reference counting needed for current->mempolicy
199052cd3b07SLee Schermerhorn 	 * nor system default_policy
199152cd3b07SLee Schermerhorn 	 */
199245c4745aSLee Schermerhorn 	if (pol->mode == MPOL_INTERLEAVE)
1993c0ff7453SMiao Xie 		page = alloc_page_interleave(gfp, order, interleave_nodes(pol));
1994c0ff7453SMiao Xie 	else
1995c0ff7453SMiao Xie 		page = __alloc_pages_nodemask(gfp, order,
19965c4b4be3SAndi Kleen 				policy_zonelist(gfp, pol, numa_node_id()),
19975c4b4be3SAndi Kleen 				policy_nodemask(gfp, pol));
1998cc9a6c87SMel Gorman 
1999cc9a6c87SMel Gorman 	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2000cc9a6c87SMel Gorman 		goto retry_cpuset;
2001cc9a6c87SMel Gorman 
2002c0ff7453SMiao Xie 	return page;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current);
20051da177e4SLinus Torvalds 
20064225399aSPaul Jackson /*
2007846a16bfSLee Schermerhorn  * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
20084225399aSPaul Jackson  * rebinds the mempolicy its copying by calling mpol_rebind_policy()
20094225399aSPaul Jackson  * with the mems_allowed returned by cpuset_mems_allowed().  This
20104225399aSPaul Jackson  * keeps mempolicies cpuset relative after its cpuset moves.  See
20114225399aSPaul Jackson  * further kernel/cpuset.c update_nodemask().
2012708c1bbcSMiao Xie  *
2013708c1bbcSMiao Xie  * current's mempolicy may be rebinded by the other task(the task that changes
2014708c1bbcSMiao Xie  * cpuset's mems), so we needn't do rebind work for current task.
20154225399aSPaul Jackson  */
20164225399aSPaul Jackson 
2017846a16bfSLee Schermerhorn /* Slow path of a mempolicy duplicate */
2018846a16bfSLee Schermerhorn struct mempolicy *__mpol_dup(struct mempolicy *old)
20191da177e4SLinus Torvalds {
20201da177e4SLinus Torvalds 	struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	if (!new)
20231da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2024708c1bbcSMiao Xie 
2025708c1bbcSMiao Xie 	/* task's mempolicy is protected by alloc_lock */
2026708c1bbcSMiao Xie 	if (old == current->mempolicy) {
2027708c1bbcSMiao Xie 		task_lock(current);
2028708c1bbcSMiao Xie 		*new = *old;
2029708c1bbcSMiao Xie 		task_unlock(current);
2030708c1bbcSMiao Xie 	} else
2031708c1bbcSMiao Xie 		*new = *old;
2032708c1bbcSMiao Xie 
203399ee4ca7SPaul E. McKenney 	rcu_read_lock();
20344225399aSPaul Jackson 	if (current_cpuset_is_being_rebound()) {
20354225399aSPaul Jackson 		nodemask_t mems = cpuset_mems_allowed(current);
2036708c1bbcSMiao Xie 		if (new->flags & MPOL_F_REBINDING)
2037708c1bbcSMiao Xie 			mpol_rebind_policy(new, &mems, MPOL_REBIND_STEP2);
2038708c1bbcSMiao Xie 		else
2039708c1bbcSMiao Xie 			mpol_rebind_policy(new, &mems, MPOL_REBIND_ONCE);
20404225399aSPaul Jackson 	}
204199ee4ca7SPaul E. McKenney 	rcu_read_unlock();
20421da177e4SLinus Torvalds 	atomic_set(&new->refcnt, 1);
20431da177e4SLinus Torvalds 	return new;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
204652cd3b07SLee Schermerhorn /*
204752cd3b07SLee Schermerhorn  * If *frompol needs [has] an extra ref, copy *frompol to *tompol ,
204852cd3b07SLee Schermerhorn  * eliminate the * MPOL_F_* flags that require conditional ref and
204952cd3b07SLee Schermerhorn  * [NOTE!!!] drop the extra ref.  Not safe to reference *frompol directly
205052cd3b07SLee Schermerhorn  * after return.  Use the returned value.
205152cd3b07SLee Schermerhorn  *
205252cd3b07SLee Schermerhorn  * Allows use of a mempolicy for, e.g., multiple allocations with a single
205352cd3b07SLee Schermerhorn  * policy lookup, even if the policy needs/has extra ref on lookup.
205452cd3b07SLee Schermerhorn  * shmem_readahead needs this.
205552cd3b07SLee Schermerhorn  */
205652cd3b07SLee Schermerhorn struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
205752cd3b07SLee Schermerhorn 						struct mempolicy *frompol)
205852cd3b07SLee Schermerhorn {
205952cd3b07SLee Schermerhorn 	if (!mpol_needs_cond_ref(frompol))
206052cd3b07SLee Schermerhorn 		return frompol;
206152cd3b07SLee Schermerhorn 
206252cd3b07SLee Schermerhorn 	*tompol = *frompol;
206352cd3b07SLee Schermerhorn 	tompol->flags &= ~MPOL_F_SHARED;	/* copy doesn't need unref */
206452cd3b07SLee Schermerhorn 	__mpol_put(frompol);
206552cd3b07SLee Schermerhorn 	return tompol;
206652cd3b07SLee Schermerhorn }
206752cd3b07SLee Schermerhorn 
20681da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */
2069fcfb4dccSKOSAKI Motohiro bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
20701da177e4SLinus Torvalds {
20711da177e4SLinus Torvalds 	if (!a || !b)
2072fcfb4dccSKOSAKI Motohiro 		return false;
207345c4745aSLee Schermerhorn 	if (a->mode != b->mode)
2074fcfb4dccSKOSAKI Motohiro 		return false;
207519800502SBob Liu 	if (a->flags != b->flags)
2076fcfb4dccSKOSAKI Motohiro 		return false;
207719800502SBob Liu 	if (mpol_store_user_nodemask(a))
207819800502SBob Liu 		if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
2079fcfb4dccSKOSAKI Motohiro 			return false;
208019800502SBob Liu 
208145c4745aSLee Schermerhorn 	switch (a->mode) {
208219770b32SMel Gorman 	case MPOL_BIND:
208319770b32SMel Gorman 		/* Fall through */
20841da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
2085fcfb4dccSKOSAKI Motohiro 		return !!nodes_equal(a->v.nodes, b->v.nodes);
20861da177e4SLinus Torvalds 	case MPOL_PREFERRED:
208775719661SNamhyung Kim 		return a->v.preferred_node == b->v.preferred_node;
20881da177e4SLinus Torvalds 	default:
20891da177e4SLinus Torvalds 		BUG();
2090fcfb4dccSKOSAKI Motohiro 		return false;
20911da177e4SLinus Torvalds 	}
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds /*
20951da177e4SLinus Torvalds  * Shared memory backing store policy support.
20961da177e4SLinus Torvalds  *
20971da177e4SLinus Torvalds  * Remember policies even when nobody has shared memory mapped.
20981da177e4SLinus Torvalds  * The policies are kept in Red-Black tree linked from the inode.
20991da177e4SLinus Torvalds  * They are protected by the sp->lock spinlock, which should be held
21001da177e4SLinus Torvalds  * for any accesses to the tree.
21011da177e4SLinus Torvalds  */
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds /* lookup first element intersecting start-end */
2104b22d127aSMel Gorman /* Caller holds sp->mutex */
21051da177e4SLinus Torvalds static struct sp_node *
21061da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
21071da177e4SLinus Torvalds {
21081da177e4SLinus Torvalds 	struct rb_node *n = sp->root.rb_node;
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	while (n) {
21111da177e4SLinus Torvalds 		struct sp_node *p = rb_entry(n, struct sp_node, nd);
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 		if (start >= p->end)
21141da177e4SLinus Torvalds 			n = n->rb_right;
21151da177e4SLinus Torvalds 		else if (end <= p->start)
21161da177e4SLinus Torvalds 			n = n->rb_left;
21171da177e4SLinus Torvalds 		else
21181da177e4SLinus Torvalds 			break;
21191da177e4SLinus Torvalds 	}
21201da177e4SLinus Torvalds 	if (!n)
21211da177e4SLinus Torvalds 		return NULL;
21221da177e4SLinus Torvalds 	for (;;) {
21231da177e4SLinus Torvalds 		struct sp_node *w = NULL;
21241da177e4SLinus Torvalds 		struct rb_node *prev = rb_prev(n);
21251da177e4SLinus Torvalds 		if (!prev)
21261da177e4SLinus Torvalds 			break;
21271da177e4SLinus Torvalds 		w = rb_entry(prev, struct sp_node, nd);
21281da177e4SLinus Torvalds 		if (w->end <= start)
21291da177e4SLinus Torvalds 			break;
21301da177e4SLinus Torvalds 		n = prev;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 	return rb_entry(n, struct sp_node, nd);
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds /* Insert a new shared policy into the list. */
21361da177e4SLinus Torvalds /* Caller holds sp->lock */
21371da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new)
21381da177e4SLinus Torvalds {
21391da177e4SLinus Torvalds 	struct rb_node **p = &sp->root.rb_node;
21401da177e4SLinus Torvalds 	struct rb_node *parent = NULL;
21411da177e4SLinus Torvalds 	struct sp_node *nd;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	while (*p) {
21441da177e4SLinus Torvalds 		parent = *p;
21451da177e4SLinus Torvalds 		nd = rb_entry(parent, struct sp_node, nd);
21461da177e4SLinus Torvalds 		if (new->start < nd->start)
21471da177e4SLinus Torvalds 			p = &(*p)->rb_left;
21481da177e4SLinus Torvalds 		else if (new->end > nd->end)
21491da177e4SLinus Torvalds 			p = &(*p)->rb_right;
21501da177e4SLinus Torvalds 		else
21511da177e4SLinus Torvalds 			BUG();
21521da177e4SLinus Torvalds 	}
21531da177e4SLinus Torvalds 	rb_link_node(&new->nd, parent, p);
21541da177e4SLinus Torvalds 	rb_insert_color(&new->nd, &sp->root);
2155140d5a49SPaul Mundt 	pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
215645c4745aSLee Schermerhorn 		 new->policy ? new->policy->mode : 0);
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds /* Find shared policy intersecting idx */
21601da177e4SLinus Torvalds struct mempolicy *
21611da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	struct mempolicy *pol = NULL;
21641da177e4SLinus Torvalds 	struct sp_node *sn;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	if (!sp->root.rb_node)
21671da177e4SLinus Torvalds 		return NULL;
2168b22d127aSMel Gorman 	mutex_lock(&sp->mutex);
21691da177e4SLinus Torvalds 	sn = sp_lookup(sp, idx, idx+1);
21701da177e4SLinus Torvalds 	if (sn) {
21711da177e4SLinus Torvalds 		mpol_get(sn->policy);
21721da177e4SLinus Torvalds 		pol = sn->policy;
21731da177e4SLinus Torvalds 	}
2174b22d127aSMel Gorman 	mutex_unlock(&sp->mutex);
21751da177e4SLinus Torvalds 	return pol;
21761da177e4SLinus Torvalds }
21771da177e4SLinus Torvalds 
217863f74ca2SKOSAKI Motohiro static void sp_free(struct sp_node *n)
217963f74ca2SKOSAKI Motohiro {
218063f74ca2SKOSAKI Motohiro 	mpol_put(n->policy);
218163f74ca2SKOSAKI Motohiro 	kmem_cache_free(sn_cache, n);
218263f74ca2SKOSAKI Motohiro }
218363f74ca2SKOSAKI Motohiro 
2184*771fb4d8SLee Schermerhorn /**
2185*771fb4d8SLee Schermerhorn  * mpol_misplaced - check whether current page node is valid in policy
2186*771fb4d8SLee Schermerhorn  *
2187*771fb4d8SLee Schermerhorn  * @page   - page to be checked
2188*771fb4d8SLee Schermerhorn  * @vma    - vm area where page mapped
2189*771fb4d8SLee Schermerhorn  * @addr   - virtual address where page mapped
2190*771fb4d8SLee Schermerhorn  *
2191*771fb4d8SLee Schermerhorn  * Lookup current policy node id for vma,addr and "compare to" page's
2192*771fb4d8SLee Schermerhorn  * node id.
2193*771fb4d8SLee Schermerhorn  *
2194*771fb4d8SLee Schermerhorn  * Returns:
2195*771fb4d8SLee Schermerhorn  *	-1	- not misplaced, page is in the right node
2196*771fb4d8SLee Schermerhorn  *	node	- node id where the page should be
2197*771fb4d8SLee Schermerhorn  *
2198*771fb4d8SLee Schermerhorn  * Policy determination "mimics" alloc_page_vma().
2199*771fb4d8SLee Schermerhorn  * Called from fault path where we know the vma and faulting address.
2200*771fb4d8SLee Schermerhorn  */
2201*771fb4d8SLee Schermerhorn int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long addr)
2202*771fb4d8SLee Schermerhorn {
2203*771fb4d8SLee Schermerhorn 	struct mempolicy *pol;
2204*771fb4d8SLee Schermerhorn 	struct zone *zone;
2205*771fb4d8SLee Schermerhorn 	int curnid = page_to_nid(page);
2206*771fb4d8SLee Schermerhorn 	unsigned long pgoff;
2207*771fb4d8SLee Schermerhorn 	int polnid = -1;
2208*771fb4d8SLee Schermerhorn 	int ret = -1;
2209*771fb4d8SLee Schermerhorn 
2210*771fb4d8SLee Schermerhorn 	BUG_ON(!vma);
2211*771fb4d8SLee Schermerhorn 
2212*771fb4d8SLee Schermerhorn 	pol = get_vma_policy(current, vma, addr);
2213*771fb4d8SLee Schermerhorn 	if (!(pol->flags & MPOL_F_MOF))
2214*771fb4d8SLee Schermerhorn 		goto out;
2215*771fb4d8SLee Schermerhorn 
2216*771fb4d8SLee Schermerhorn 	switch (pol->mode) {
2217*771fb4d8SLee Schermerhorn 	case MPOL_INTERLEAVE:
2218*771fb4d8SLee Schermerhorn 		BUG_ON(addr >= vma->vm_end);
2219*771fb4d8SLee Schermerhorn 		BUG_ON(addr < vma->vm_start);
2220*771fb4d8SLee Schermerhorn 
2221*771fb4d8SLee Schermerhorn 		pgoff = vma->vm_pgoff;
2222*771fb4d8SLee Schermerhorn 		pgoff += (addr - vma->vm_start) >> PAGE_SHIFT;
2223*771fb4d8SLee Schermerhorn 		polnid = offset_il_node(pol, vma, pgoff);
2224*771fb4d8SLee Schermerhorn 		break;
2225*771fb4d8SLee Schermerhorn 
2226*771fb4d8SLee Schermerhorn 	case MPOL_PREFERRED:
2227*771fb4d8SLee Schermerhorn 		if (pol->flags & MPOL_F_LOCAL)
2228*771fb4d8SLee Schermerhorn 			polnid = numa_node_id();
2229*771fb4d8SLee Schermerhorn 		else
2230*771fb4d8SLee Schermerhorn 			polnid = pol->v.preferred_node;
2231*771fb4d8SLee Schermerhorn 		break;
2232*771fb4d8SLee Schermerhorn 
2233*771fb4d8SLee Schermerhorn 	case MPOL_BIND:
2234*771fb4d8SLee Schermerhorn 		/*
2235*771fb4d8SLee Schermerhorn 		 * allows binding to multiple nodes.
2236*771fb4d8SLee Schermerhorn 		 * use current page if in policy nodemask,
2237*771fb4d8SLee Schermerhorn 		 * else select nearest allowed node, if any.
2238*771fb4d8SLee Schermerhorn 		 * If no allowed nodes, use current [!misplaced].
2239*771fb4d8SLee Schermerhorn 		 */
2240*771fb4d8SLee Schermerhorn 		if (node_isset(curnid, pol->v.nodes))
2241*771fb4d8SLee Schermerhorn 			goto out;
2242*771fb4d8SLee Schermerhorn 		(void)first_zones_zonelist(
2243*771fb4d8SLee Schermerhorn 				node_zonelist(numa_node_id(), GFP_HIGHUSER),
2244*771fb4d8SLee Schermerhorn 				gfp_zone(GFP_HIGHUSER),
2245*771fb4d8SLee Schermerhorn 				&pol->v.nodes, &zone);
2246*771fb4d8SLee Schermerhorn 		polnid = zone->node;
2247*771fb4d8SLee Schermerhorn 		break;
2248*771fb4d8SLee Schermerhorn 
2249*771fb4d8SLee Schermerhorn 	default:
2250*771fb4d8SLee Schermerhorn 		BUG();
2251*771fb4d8SLee Schermerhorn 	}
2252*771fb4d8SLee Schermerhorn 	if (curnid != polnid)
2253*771fb4d8SLee Schermerhorn 		ret = polnid;
2254*771fb4d8SLee Schermerhorn out:
2255*771fb4d8SLee Schermerhorn 	mpol_cond_put(pol);
2256*771fb4d8SLee Schermerhorn 
2257*771fb4d8SLee Schermerhorn 	return ret;
2258*771fb4d8SLee Schermerhorn }
2259*771fb4d8SLee Schermerhorn 
22601da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n)
22611da177e4SLinus Torvalds {
2262140d5a49SPaul Mundt 	pr_debug("deleting %lx-l%lx\n", n->start, n->end);
22631da177e4SLinus Torvalds 	rb_erase(&n->nd, &sp->root);
226463f74ca2SKOSAKI Motohiro 	sp_free(n);
22651da177e4SLinus Torvalds }
22661da177e4SLinus Torvalds 
2267dbcb0f19SAdrian Bunk static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
2268dbcb0f19SAdrian Bunk 				struct mempolicy *pol)
22691da177e4SLinus Torvalds {
2270869833f2SKOSAKI Motohiro 	struct sp_node *n;
2271869833f2SKOSAKI Motohiro 	struct mempolicy *newpol;
22721da177e4SLinus Torvalds 
2273869833f2SKOSAKI Motohiro 	n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
22741da177e4SLinus Torvalds 	if (!n)
22751da177e4SLinus Torvalds 		return NULL;
2276869833f2SKOSAKI Motohiro 
2277869833f2SKOSAKI Motohiro 	newpol = mpol_dup(pol);
2278869833f2SKOSAKI Motohiro 	if (IS_ERR(newpol)) {
2279869833f2SKOSAKI Motohiro 		kmem_cache_free(sn_cache, n);
2280869833f2SKOSAKI Motohiro 		return NULL;
2281869833f2SKOSAKI Motohiro 	}
2282869833f2SKOSAKI Motohiro 	newpol->flags |= MPOL_F_SHARED;
2283869833f2SKOSAKI Motohiro 
22841da177e4SLinus Torvalds 	n->start = start;
22851da177e4SLinus Torvalds 	n->end = end;
2286869833f2SKOSAKI Motohiro 	n->policy = newpol;
2287869833f2SKOSAKI Motohiro 
22881da177e4SLinus Torvalds 	return n;
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds /* Replace a policy range. */
22921da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
22931da177e4SLinus Torvalds 				 unsigned long end, struct sp_node *new)
22941da177e4SLinus Torvalds {
2295b22d127aSMel Gorman 	struct sp_node *n;
2296b22d127aSMel Gorman 	int ret = 0;
22971da177e4SLinus Torvalds 
2298b22d127aSMel Gorman 	mutex_lock(&sp->mutex);
22991da177e4SLinus Torvalds 	n = sp_lookup(sp, start, end);
23001da177e4SLinus Torvalds 	/* Take care of old policies in the same range. */
23011da177e4SLinus Torvalds 	while (n && n->start < end) {
23021da177e4SLinus Torvalds 		struct rb_node *next = rb_next(&n->nd);
23031da177e4SLinus Torvalds 		if (n->start >= start) {
23041da177e4SLinus Torvalds 			if (n->end <= end)
23051da177e4SLinus Torvalds 				sp_delete(sp, n);
23061da177e4SLinus Torvalds 			else
23071da177e4SLinus Torvalds 				n->start = end;
23081da177e4SLinus Torvalds 		} else {
23091da177e4SLinus Torvalds 			/* Old policy spanning whole new range. */
23101da177e4SLinus Torvalds 			if (n->end > end) {
2311b22d127aSMel Gorman 				struct sp_node *new2;
23121da177e4SLinus Torvalds 				new2 = sp_alloc(end, n->end, n->policy);
2313b22d127aSMel Gorman 				if (!new2) {
2314b22d127aSMel Gorman 					ret = -ENOMEM;
2315b22d127aSMel Gorman 					goto out;
23161da177e4SLinus Torvalds 				}
23171da177e4SLinus Torvalds 				n->end = start;
23181da177e4SLinus Torvalds 				sp_insert(sp, new2);
23191da177e4SLinus Torvalds 				break;
23201da177e4SLinus Torvalds 			} else
23211da177e4SLinus Torvalds 				n->end = start;
23221da177e4SLinus Torvalds 		}
23231da177e4SLinus Torvalds 		if (!next)
23241da177e4SLinus Torvalds 			break;
23251da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
23261da177e4SLinus Torvalds 	}
23271da177e4SLinus Torvalds 	if (new)
23281da177e4SLinus Torvalds 		sp_insert(sp, new);
2329b22d127aSMel Gorman out:
2330b22d127aSMel Gorman 	mutex_unlock(&sp->mutex);
2331b22d127aSMel Gorman 	return ret;
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
233471fe804bSLee Schermerhorn /**
233571fe804bSLee Schermerhorn  * mpol_shared_policy_init - initialize shared policy for inode
233671fe804bSLee Schermerhorn  * @sp: pointer to inode shared policy
233771fe804bSLee Schermerhorn  * @mpol:  struct mempolicy to install
233871fe804bSLee Schermerhorn  *
233971fe804bSLee Schermerhorn  * Install non-NULL @mpol in inode's shared policy rb-tree.
234071fe804bSLee Schermerhorn  * On entry, the current task has a reference on a non-NULL @mpol.
234171fe804bSLee Schermerhorn  * This must be released on exit.
23424bfc4495SKAMEZAWA Hiroyuki  * This is called at get_inode() calls and we can use GFP_KERNEL.
234371fe804bSLee Schermerhorn  */
234471fe804bSLee Schermerhorn void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
23457339ff83SRobin Holt {
234658568d2aSMiao Xie 	int ret;
234758568d2aSMiao Xie 
234871fe804bSLee Schermerhorn 	sp->root = RB_ROOT;		/* empty tree == default mempolicy */
2349b22d127aSMel Gorman 	mutex_init(&sp->mutex);
23507339ff83SRobin Holt 
235171fe804bSLee Schermerhorn 	if (mpol) {
23527339ff83SRobin Holt 		struct vm_area_struct pvma;
235371fe804bSLee Schermerhorn 		struct mempolicy *new;
23544bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
23557339ff83SRobin Holt 
23564bfc4495SKAMEZAWA Hiroyuki 		if (!scratch)
23575c0c1654SLee Schermerhorn 			goto put_mpol;
235871fe804bSLee Schermerhorn 		/* contextualize the tmpfs mount point mempolicy */
235971fe804bSLee Schermerhorn 		new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
236015d77835SLee Schermerhorn 		if (IS_ERR(new))
23610cae3457SDan Carpenter 			goto free_scratch; /* no valid nodemask intersection */
236258568d2aSMiao Xie 
236358568d2aSMiao Xie 		task_lock(current);
23644bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch);
236558568d2aSMiao Xie 		task_unlock(current);
236615d77835SLee Schermerhorn 		if (ret)
23675c0c1654SLee Schermerhorn 			goto put_new;
236871fe804bSLee Schermerhorn 
236971fe804bSLee Schermerhorn 		/* Create pseudo-vma that contains just the policy */
23707339ff83SRobin Holt 		memset(&pvma, 0, sizeof(struct vm_area_struct));
237171fe804bSLee Schermerhorn 		pvma.vm_end = TASK_SIZE;	/* policy covers entire file */
237271fe804bSLee Schermerhorn 		mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
237315d77835SLee Schermerhorn 
23745c0c1654SLee Schermerhorn put_new:
237571fe804bSLee Schermerhorn 		mpol_put(new);			/* drop initial ref */
23760cae3457SDan Carpenter free_scratch:
23774bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
23785c0c1654SLee Schermerhorn put_mpol:
23795c0c1654SLee Schermerhorn 		mpol_put(mpol);	/* drop our incoming ref on sb mpol */
23807339ff83SRobin Holt 	}
23817339ff83SRobin Holt }
23827339ff83SRobin Holt 
23831da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info,
23841da177e4SLinus Torvalds 			struct vm_area_struct *vma, struct mempolicy *npol)
23851da177e4SLinus Torvalds {
23861da177e4SLinus Torvalds 	int err;
23871da177e4SLinus Torvalds 	struct sp_node *new = NULL;
23881da177e4SLinus Torvalds 	unsigned long sz = vma_pages(vma);
23891da177e4SLinus Torvalds 
2390028fec41SDavid Rientjes 	pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
23911da177e4SLinus Torvalds 		 vma->vm_pgoff,
239245c4745aSLee Schermerhorn 		 sz, npol ? npol->mode : -1,
2393028fec41SDavid Rientjes 		 npol ? npol->flags : -1,
2394dfcd3c0dSAndi Kleen 		 npol ? nodes_addr(npol->v.nodes)[0] : -1);
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	if (npol) {
23971da177e4SLinus Torvalds 		new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
23981da177e4SLinus Torvalds 		if (!new)
23991da177e4SLinus Torvalds 			return -ENOMEM;
24001da177e4SLinus Torvalds 	}
24011da177e4SLinus Torvalds 	err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
24021da177e4SLinus Torvalds 	if (err && new)
240363f74ca2SKOSAKI Motohiro 		sp_free(new);
24041da177e4SLinus Torvalds 	return err;
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */
24081da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p)
24091da177e4SLinus Torvalds {
24101da177e4SLinus Torvalds 	struct sp_node *n;
24111da177e4SLinus Torvalds 	struct rb_node *next;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	if (!p->root.rb_node)
24141da177e4SLinus Torvalds 		return;
2415b22d127aSMel Gorman 	mutex_lock(&p->mutex);
24161da177e4SLinus Torvalds 	next = rb_first(&p->root);
24171da177e4SLinus Torvalds 	while (next) {
24181da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
24191da177e4SLinus Torvalds 		next = rb_next(&n->nd);
242063f74ca2SKOSAKI Motohiro 		sp_delete(p, n);
24211da177e4SLinus Torvalds 	}
2422b22d127aSMel Gorman 	mutex_unlock(&p->mutex);
24231da177e4SLinus Torvalds }
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */
24261da177e4SLinus Torvalds void __init numa_policy_init(void)
24271da177e4SLinus Torvalds {
2428b71636e2SPaul Mundt 	nodemask_t interleave_nodes;
2429b71636e2SPaul Mundt 	unsigned long largest = 0;
2430b71636e2SPaul Mundt 	int nid, prefer = 0;
2431b71636e2SPaul Mundt 
24321da177e4SLinus Torvalds 	policy_cache = kmem_cache_create("numa_policy",
24331da177e4SLinus Torvalds 					 sizeof(struct mempolicy),
243420c2df83SPaul Mundt 					 0, SLAB_PANIC, NULL);
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	sn_cache = kmem_cache_create("shared_policy_node",
24371da177e4SLinus Torvalds 				     sizeof(struct sp_node),
243820c2df83SPaul Mundt 				     0, SLAB_PANIC, NULL);
24391da177e4SLinus Torvalds 
2440b71636e2SPaul Mundt 	/*
2441b71636e2SPaul Mundt 	 * Set interleaving policy for system init. Interleaving is only
2442b71636e2SPaul Mundt 	 * enabled across suitably sized nodes (default is >= 16MB), or
2443b71636e2SPaul Mundt 	 * fall back to the largest node if they're all smaller.
2444b71636e2SPaul Mundt 	 */
2445b71636e2SPaul Mundt 	nodes_clear(interleave_nodes);
244656bbd65dSChristoph Lameter 	for_each_node_state(nid, N_HIGH_MEMORY) {
2447b71636e2SPaul Mundt 		unsigned long total_pages = node_present_pages(nid);
24481da177e4SLinus Torvalds 
2449b71636e2SPaul Mundt 		/* Preserve the largest node */
2450b71636e2SPaul Mundt 		if (largest < total_pages) {
2451b71636e2SPaul Mundt 			largest = total_pages;
2452b71636e2SPaul Mundt 			prefer = nid;
2453b71636e2SPaul Mundt 		}
2454b71636e2SPaul Mundt 
2455b71636e2SPaul Mundt 		/* Interleave this node? */
2456b71636e2SPaul Mundt 		if ((total_pages << PAGE_SHIFT) >= (16 << 20))
2457b71636e2SPaul Mundt 			node_set(nid, interleave_nodes);
2458b71636e2SPaul Mundt 	}
2459b71636e2SPaul Mundt 
2460b71636e2SPaul Mundt 	/* All too small, use the largest */
2461b71636e2SPaul Mundt 	if (unlikely(nodes_empty(interleave_nodes)))
2462b71636e2SPaul Mundt 		node_set(prefer, interleave_nodes);
2463b71636e2SPaul Mundt 
2464028fec41SDavid Rientjes 	if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
24651da177e4SLinus Torvalds 		printk("numa_policy_init: interleaving failed\n");
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24688bccd85fSChristoph Lameter /* Reset policy of current process to default */
24691da177e4SLinus Torvalds void numa_default_policy(void)
24701da177e4SLinus Torvalds {
2471028fec41SDavid Rientjes 	do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
24721da177e4SLinus Torvalds }
247368860ec1SPaul Jackson 
24744225399aSPaul Jackson /*
2475095f1fc4SLee Schermerhorn  * Parse and format mempolicy from/to strings
2476095f1fc4SLee Schermerhorn  */
2477095f1fc4SLee Schermerhorn 
2478095f1fc4SLee Schermerhorn /*
2479fc36b8d3SLee Schermerhorn  * "local" is pseudo-policy:  MPOL_PREFERRED with MPOL_F_LOCAL flag
24803f226aa1SLee Schermerhorn  * Used only for mpol_parse_str() and mpol_to_str()
24811a75a6c8SChristoph Lameter  */
2482345ace9cSLee Schermerhorn static const char * const policy_modes[] =
2483345ace9cSLee Schermerhorn {
2484345ace9cSLee Schermerhorn 	[MPOL_DEFAULT]    = "default",
2485345ace9cSLee Schermerhorn 	[MPOL_PREFERRED]  = "prefer",
2486345ace9cSLee Schermerhorn 	[MPOL_BIND]       = "bind",
2487345ace9cSLee Schermerhorn 	[MPOL_INTERLEAVE] = "interleave",
2488d3a71033SLee Schermerhorn 	[MPOL_LOCAL]      = "local",
2489d3a71033SLee Schermerhorn 	[MPOL_NOOP]	  = "noop",	/* should not actually be used */
2490345ace9cSLee Schermerhorn };
24911a75a6c8SChristoph Lameter 
2492095f1fc4SLee Schermerhorn 
2493095f1fc4SLee Schermerhorn #ifdef CONFIG_TMPFS
2494095f1fc4SLee Schermerhorn /**
2495095f1fc4SLee Schermerhorn  * mpol_parse_str - parse string to mempolicy
2496095f1fc4SLee Schermerhorn  * @str:  string containing mempolicy to parse
249771fe804bSLee Schermerhorn  * @mpol:  pointer to struct mempolicy pointer, returned on success.
249871fe804bSLee Schermerhorn  * @no_context:  flag whether to "contextualize" the mempolicy
2499095f1fc4SLee Schermerhorn  *
2500095f1fc4SLee Schermerhorn  * Format of input:
2501095f1fc4SLee Schermerhorn  *	<mode>[=<flags>][:<nodelist>]
2502095f1fc4SLee Schermerhorn  *
250371fe804bSLee Schermerhorn  * if @no_context is true, save the input nodemask in w.user_nodemask in
250471fe804bSLee Schermerhorn  * the returned mempolicy.  This will be used to "clone" the mempolicy in
250571fe804bSLee Schermerhorn  * a specific context [cpuset] at a later time.  Used to parse tmpfs mpol
250671fe804bSLee Schermerhorn  * mount option.  Note that if 'static' or 'relative' mode flags were
250771fe804bSLee Schermerhorn  * specified, the input nodemask will already have been saved.  Saving
250871fe804bSLee Schermerhorn  * it again is redundant, but safe.
250971fe804bSLee Schermerhorn  *
251071fe804bSLee Schermerhorn  * On success, returns 0, else 1
2511095f1fc4SLee Schermerhorn  */
251271fe804bSLee Schermerhorn int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context)
2513095f1fc4SLee Schermerhorn {
251471fe804bSLee Schermerhorn 	struct mempolicy *new = NULL;
2515b4652e84SLee Schermerhorn 	unsigned short mode;
251671fe804bSLee Schermerhorn 	unsigned short uninitialized_var(mode_flags);
251771fe804bSLee Schermerhorn 	nodemask_t nodes;
2518095f1fc4SLee Schermerhorn 	char *nodelist = strchr(str, ':');
2519095f1fc4SLee Schermerhorn 	char *flags = strchr(str, '=');
2520095f1fc4SLee Schermerhorn 	int err = 1;
2521095f1fc4SLee Schermerhorn 
2522095f1fc4SLee Schermerhorn 	if (nodelist) {
2523095f1fc4SLee Schermerhorn 		/* NUL-terminate mode or flags string */
2524095f1fc4SLee Schermerhorn 		*nodelist++ = '\0';
252571fe804bSLee Schermerhorn 		if (nodelist_parse(nodelist, nodes))
2526095f1fc4SLee Schermerhorn 			goto out;
252771fe804bSLee Schermerhorn 		if (!nodes_subset(nodes, node_states[N_HIGH_MEMORY]))
2528095f1fc4SLee Schermerhorn 			goto out;
252971fe804bSLee Schermerhorn 	} else
253071fe804bSLee Schermerhorn 		nodes_clear(nodes);
253171fe804bSLee Schermerhorn 
2532095f1fc4SLee Schermerhorn 	if (flags)
2533095f1fc4SLee Schermerhorn 		*flags++ = '\0';	/* terminate mode string */
2534095f1fc4SLee Schermerhorn 
2535479e2802SPeter Zijlstra 	for (mode = 0; mode < MPOL_MAX; mode++) {
2536345ace9cSLee Schermerhorn 		if (!strcmp(str, policy_modes[mode])) {
2537095f1fc4SLee Schermerhorn 			break;
2538095f1fc4SLee Schermerhorn 		}
2539095f1fc4SLee Schermerhorn 	}
2540d3a71033SLee Schermerhorn 	if (mode >= MPOL_MAX || mode == MPOL_NOOP)
2541095f1fc4SLee Schermerhorn 		goto out;
2542095f1fc4SLee Schermerhorn 
254371fe804bSLee Schermerhorn 	switch (mode) {
2544095f1fc4SLee Schermerhorn 	case MPOL_PREFERRED:
254571fe804bSLee Schermerhorn 		/*
254671fe804bSLee Schermerhorn 		 * Insist on a nodelist of one node only
254771fe804bSLee Schermerhorn 		 */
2548095f1fc4SLee Schermerhorn 		if (nodelist) {
2549095f1fc4SLee Schermerhorn 			char *rest = nodelist;
2550095f1fc4SLee Schermerhorn 			while (isdigit(*rest))
2551095f1fc4SLee Schermerhorn 				rest++;
2552926f2ae0SKOSAKI Motohiro 			if (*rest)
2553926f2ae0SKOSAKI Motohiro 				goto out;
2554095f1fc4SLee Schermerhorn 		}
2555095f1fc4SLee Schermerhorn 		break;
2556095f1fc4SLee Schermerhorn 	case MPOL_INTERLEAVE:
2557095f1fc4SLee Schermerhorn 		/*
2558095f1fc4SLee Schermerhorn 		 * Default to online nodes with memory if no nodelist
2559095f1fc4SLee Schermerhorn 		 */
2560095f1fc4SLee Schermerhorn 		if (!nodelist)
256171fe804bSLee Schermerhorn 			nodes = node_states[N_HIGH_MEMORY];
25623f226aa1SLee Schermerhorn 		break;
256371fe804bSLee Schermerhorn 	case MPOL_LOCAL:
25643f226aa1SLee Schermerhorn 		/*
256571fe804bSLee Schermerhorn 		 * Don't allow a nodelist;  mpol_new() checks flags
25663f226aa1SLee Schermerhorn 		 */
256771fe804bSLee Schermerhorn 		if (nodelist)
25683f226aa1SLee Schermerhorn 			goto out;
256971fe804bSLee Schermerhorn 		mode = MPOL_PREFERRED;
25703f226aa1SLee Schermerhorn 		break;
2571413b43deSRavikiran G Thirumalai 	case MPOL_DEFAULT:
2572413b43deSRavikiran G Thirumalai 		/*
2573413b43deSRavikiran G Thirumalai 		 * Insist on a empty nodelist
2574413b43deSRavikiran G Thirumalai 		 */
2575413b43deSRavikiran G Thirumalai 		if (!nodelist)
2576413b43deSRavikiran G Thirumalai 			err = 0;
2577413b43deSRavikiran G Thirumalai 		goto out;
2578d69b2e63SKOSAKI Motohiro 	case MPOL_BIND:
257971fe804bSLee Schermerhorn 		/*
2580d69b2e63SKOSAKI Motohiro 		 * Insist on a nodelist
258171fe804bSLee Schermerhorn 		 */
2582d69b2e63SKOSAKI Motohiro 		if (!nodelist)
2583d69b2e63SKOSAKI Motohiro 			goto out;
2584095f1fc4SLee Schermerhorn 	}
2585095f1fc4SLee Schermerhorn 
258671fe804bSLee Schermerhorn 	mode_flags = 0;
2587095f1fc4SLee Schermerhorn 	if (flags) {
2588095f1fc4SLee Schermerhorn 		/*
2589095f1fc4SLee Schermerhorn 		 * Currently, we only support two mutually exclusive
2590095f1fc4SLee Schermerhorn 		 * mode flags.
2591095f1fc4SLee Schermerhorn 		 */
2592095f1fc4SLee Schermerhorn 		if (!strcmp(flags, "static"))
259371fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_STATIC_NODES;
2594095f1fc4SLee Schermerhorn 		else if (!strcmp(flags, "relative"))
259571fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_RELATIVE_NODES;
2596095f1fc4SLee Schermerhorn 		else
2597926f2ae0SKOSAKI Motohiro 			goto out;
2598095f1fc4SLee Schermerhorn 	}
259971fe804bSLee Schermerhorn 
260071fe804bSLee Schermerhorn 	new = mpol_new(mode, mode_flags, &nodes);
260171fe804bSLee Schermerhorn 	if (IS_ERR(new))
2602926f2ae0SKOSAKI Motohiro 		goto out;
2603926f2ae0SKOSAKI Motohiro 
2604e17f74afSLee Schermerhorn 	if (no_context) {
2605e17f74afSLee Schermerhorn 		/* save for contextualization */
2606e17f74afSLee Schermerhorn 		new->w.user_nodemask = nodes;
2607e17f74afSLee Schermerhorn 	} else {
260858568d2aSMiao Xie 		int ret;
26094bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
26104bfc4495SKAMEZAWA Hiroyuki 		if (scratch) {
261158568d2aSMiao Xie 			task_lock(current);
26124bfc4495SKAMEZAWA Hiroyuki 			ret = mpol_set_nodemask(new, &nodes, scratch);
261358568d2aSMiao Xie 			task_unlock(current);
26144bfc4495SKAMEZAWA Hiroyuki 		} else
26154bfc4495SKAMEZAWA Hiroyuki 			ret = -ENOMEM;
26164bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
26174bfc4495SKAMEZAWA Hiroyuki 		if (ret) {
26184bfc4495SKAMEZAWA Hiroyuki 			mpol_put(new);
2619926f2ae0SKOSAKI Motohiro 			goto out;
2620926f2ae0SKOSAKI Motohiro 		}
2621926f2ae0SKOSAKI Motohiro 	}
2622926f2ae0SKOSAKI Motohiro 	err = 0;
262371fe804bSLee Schermerhorn 
2624095f1fc4SLee Schermerhorn out:
2625095f1fc4SLee Schermerhorn 	/* Restore string for error message */
2626095f1fc4SLee Schermerhorn 	if (nodelist)
2627095f1fc4SLee Schermerhorn 		*--nodelist = ':';
2628095f1fc4SLee Schermerhorn 	if (flags)
2629095f1fc4SLee Schermerhorn 		*--flags = '=';
263071fe804bSLee Schermerhorn 	if (!err)
263171fe804bSLee Schermerhorn 		*mpol = new;
2632095f1fc4SLee Schermerhorn 	return err;
2633095f1fc4SLee Schermerhorn }
2634095f1fc4SLee Schermerhorn #endif /* CONFIG_TMPFS */
2635095f1fc4SLee Schermerhorn 
263671fe804bSLee Schermerhorn /**
263771fe804bSLee Schermerhorn  * mpol_to_str - format a mempolicy structure for printing
263871fe804bSLee Schermerhorn  * @buffer:  to contain formatted mempolicy string
263971fe804bSLee Schermerhorn  * @maxlen:  length of @buffer
264071fe804bSLee Schermerhorn  * @pol:  pointer to mempolicy to be formatted
264171fe804bSLee Schermerhorn  * @no_context:  "context free" mempolicy - use nodemask in w.user_nodemask
264271fe804bSLee Schermerhorn  *
26431a75a6c8SChristoph Lameter  * Convert a mempolicy into a string.
26441a75a6c8SChristoph Lameter  * Returns the number of characters in buffer (if positive)
26451a75a6c8SChristoph Lameter  * or an error (negative)
26461a75a6c8SChristoph Lameter  */
264771fe804bSLee Schermerhorn int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol, int no_context)
26481a75a6c8SChristoph Lameter {
26491a75a6c8SChristoph Lameter 	char *p = buffer;
26501a75a6c8SChristoph Lameter 	int l;
26511a75a6c8SChristoph Lameter 	nodemask_t nodes;
2652bea904d5SLee Schermerhorn 	unsigned short mode;
2653f5b087b5SDavid Rientjes 	unsigned short flags = pol ? pol->flags : 0;
26541a75a6c8SChristoph Lameter 
26552291990aSLee Schermerhorn 	/*
26562291990aSLee Schermerhorn 	 * Sanity check:  room for longest mode, flag and some nodes
26572291990aSLee Schermerhorn 	 */
26582291990aSLee Schermerhorn 	VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16);
26592291990aSLee Schermerhorn 
2660bea904d5SLee Schermerhorn 	if (!pol || pol == &default_policy)
2661bea904d5SLee Schermerhorn 		mode = MPOL_DEFAULT;
2662bea904d5SLee Schermerhorn 	else
2663bea904d5SLee Schermerhorn 		mode = pol->mode;
2664bea904d5SLee Schermerhorn 
26651a75a6c8SChristoph Lameter 	switch (mode) {
26661a75a6c8SChristoph Lameter 	case MPOL_DEFAULT:
26671a75a6c8SChristoph Lameter 		nodes_clear(nodes);
26681a75a6c8SChristoph Lameter 		break;
26691a75a6c8SChristoph Lameter 
26701a75a6c8SChristoph Lameter 	case MPOL_PREFERRED:
26711a75a6c8SChristoph Lameter 		nodes_clear(nodes);
2672fc36b8d3SLee Schermerhorn 		if (flags & MPOL_F_LOCAL)
267353f2556bSLee Schermerhorn 			mode = MPOL_LOCAL;	/* pseudo-policy */
267453f2556bSLee Schermerhorn 		else
2675fc36b8d3SLee Schermerhorn 			node_set(pol->v.preferred_node, nodes);
26761a75a6c8SChristoph Lameter 		break;
26771a75a6c8SChristoph Lameter 
26781a75a6c8SChristoph Lameter 	case MPOL_BIND:
267919770b32SMel Gorman 		/* Fall through */
26801a75a6c8SChristoph Lameter 	case MPOL_INTERLEAVE:
268171fe804bSLee Schermerhorn 		if (no_context)
268271fe804bSLee Schermerhorn 			nodes = pol->w.user_nodemask;
268371fe804bSLee Schermerhorn 		else
26841a75a6c8SChristoph Lameter 			nodes = pol->v.nodes;
26851a75a6c8SChristoph Lameter 		break;
26861a75a6c8SChristoph Lameter 
26871a75a6c8SChristoph Lameter 	default:
268880de7c31SDave Jones 		return -EINVAL;
26891a75a6c8SChristoph Lameter 	}
26901a75a6c8SChristoph Lameter 
2691345ace9cSLee Schermerhorn 	l = strlen(policy_modes[mode]);
26921a75a6c8SChristoph Lameter 	if (buffer + maxlen < p + l + 1)
26931a75a6c8SChristoph Lameter 		return -ENOSPC;
26941a75a6c8SChristoph Lameter 
2695345ace9cSLee Schermerhorn 	strcpy(p, policy_modes[mode]);
26961a75a6c8SChristoph Lameter 	p += l;
26971a75a6c8SChristoph Lameter 
2698fc36b8d3SLee Schermerhorn 	if (flags & MPOL_MODE_FLAGS) {
2699f5b087b5SDavid Rientjes 		if (buffer + maxlen < p + 2)
2700f5b087b5SDavid Rientjes 			return -ENOSPC;
2701f5b087b5SDavid Rientjes 		*p++ = '=';
2702f5b087b5SDavid Rientjes 
27032291990aSLee Schermerhorn 		/*
27042291990aSLee Schermerhorn 		 * Currently, the only defined flags are mutually exclusive
27052291990aSLee Schermerhorn 		 */
2706f5b087b5SDavid Rientjes 		if (flags & MPOL_F_STATIC_NODES)
27072291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "static");
27082291990aSLee Schermerhorn 		else if (flags & MPOL_F_RELATIVE_NODES)
27092291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "relative");
2710f5b087b5SDavid Rientjes 	}
2711f5b087b5SDavid Rientjes 
27121a75a6c8SChristoph Lameter 	if (!nodes_empty(nodes)) {
27131a75a6c8SChristoph Lameter 		if (buffer + maxlen < p + 2)
27141a75a6c8SChristoph Lameter 			return -ENOSPC;
2715095f1fc4SLee Schermerhorn 		*p++ = ':';
27161a75a6c8SChristoph Lameter 	 	p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
27171a75a6c8SChristoph Lameter 	}
27181a75a6c8SChristoph Lameter 	return p - buffer;
27191a75a6c8SChristoph Lameter }
2720