xref: /openbmc/linux/mm/mempolicy.c (revision 082708072a4250f5c4dbc62065e7af93f5e45646)
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 
2543e1f0645SDavid Rientjes 	if (mode == MPOL_DEFAULT) {
2553e1f0645SDavid Rientjes 		if (nodes && !nodes_empty(*nodes))
25637012946SDavid Rientjes 			return ERR_PTR(-EINVAL);
257bea904d5SLee Schermerhorn 		return NULL;	/* simply delete any existing policy */
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 		}
2723e1f0645SDavid Rientjes 	} else if (nodes_empty(*nodes))
2733e1f0645SDavid Rientjes 		return ERR_PTR(-EINVAL);
2741da177e4SLinus Torvalds 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2751da177e4SLinus Torvalds 	if (!policy)
2761da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2771da177e4SLinus Torvalds 	atomic_set(&policy->refcnt, 1);
27845c4745aSLee Schermerhorn 	policy->mode = mode;
27937012946SDavid Rientjes 	policy->flags = flags;
2803e1f0645SDavid Rientjes 
28137012946SDavid Rientjes 	return policy;
28237012946SDavid Rientjes }
28337012946SDavid Rientjes 
28452cd3b07SLee Schermerhorn /* Slow path of a mpol destructor. */
28552cd3b07SLee Schermerhorn void __mpol_put(struct mempolicy *p)
28652cd3b07SLee Schermerhorn {
28752cd3b07SLee Schermerhorn 	if (!atomic_dec_and_test(&p->refcnt))
28852cd3b07SLee Schermerhorn 		return;
28952cd3b07SLee Schermerhorn 	kmem_cache_free(policy_cache, p);
29052cd3b07SLee Schermerhorn }
29152cd3b07SLee Schermerhorn 
292708c1bbcSMiao Xie static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes,
293708c1bbcSMiao Xie 				enum mpol_rebind_step step)
29437012946SDavid Rientjes {
29537012946SDavid Rientjes }
29637012946SDavid Rientjes 
297708c1bbcSMiao Xie /*
298708c1bbcSMiao Xie  * step:
299708c1bbcSMiao Xie  * 	MPOL_REBIND_ONCE  - do rebind work at once
300708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP1 - set all the newly nodes
301708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP2 - clean all the disallowed nodes
302708c1bbcSMiao Xie  */
303708c1bbcSMiao Xie static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes,
304708c1bbcSMiao Xie 				 enum mpol_rebind_step step)
3051d0d2680SDavid Rientjes {
3061d0d2680SDavid Rientjes 	nodemask_t tmp;
3071d0d2680SDavid Rientjes 
30837012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES)
30937012946SDavid Rientjes 		nodes_and(tmp, pol->w.user_nodemask, *nodes);
31037012946SDavid Rientjes 	else if (pol->flags & MPOL_F_RELATIVE_NODES)
31137012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
3121d0d2680SDavid Rientjes 	else {
313708c1bbcSMiao Xie 		/*
314708c1bbcSMiao Xie 		 * if step == 1, we use ->w.cpuset_mems_allowed to cache the
315708c1bbcSMiao Xie 		 * result
316708c1bbcSMiao Xie 		 */
317708c1bbcSMiao Xie 		if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) {
318708c1bbcSMiao Xie 			nodes_remap(tmp, pol->v.nodes,
319708c1bbcSMiao Xie 					pol->w.cpuset_mems_allowed, *nodes);
320708c1bbcSMiao Xie 			pol->w.cpuset_mems_allowed = step ? tmp : *nodes;
321708c1bbcSMiao Xie 		} else if (step == MPOL_REBIND_STEP2) {
322708c1bbcSMiao Xie 			tmp = pol->w.cpuset_mems_allowed;
32337012946SDavid Rientjes 			pol->w.cpuset_mems_allowed = *nodes;
324708c1bbcSMiao Xie 		} else
325708c1bbcSMiao Xie 			BUG();
3261d0d2680SDavid Rientjes 	}
32737012946SDavid Rientjes 
328708c1bbcSMiao Xie 	if (nodes_empty(tmp))
329708c1bbcSMiao Xie 		tmp = *nodes;
330708c1bbcSMiao Xie 
331708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP1)
332708c1bbcSMiao Xie 		nodes_or(pol->v.nodes, pol->v.nodes, tmp);
333708c1bbcSMiao Xie 	else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2)
3341d0d2680SDavid Rientjes 		pol->v.nodes = tmp;
335708c1bbcSMiao Xie 	else
336708c1bbcSMiao Xie 		BUG();
337708c1bbcSMiao Xie 
3381d0d2680SDavid Rientjes 	if (!node_isset(current->il_next, tmp)) {
3391d0d2680SDavid Rientjes 		current->il_next = next_node(current->il_next, tmp);
3401d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
3411d0d2680SDavid Rientjes 			current->il_next = first_node(tmp);
3421d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
3431d0d2680SDavid Rientjes 			current->il_next = numa_node_id();
3441d0d2680SDavid Rientjes 	}
34537012946SDavid Rientjes }
34637012946SDavid Rientjes 
34737012946SDavid Rientjes static void mpol_rebind_preferred(struct mempolicy *pol,
348708c1bbcSMiao Xie 				  const nodemask_t *nodes,
349708c1bbcSMiao Xie 				  enum mpol_rebind_step step)
35037012946SDavid Rientjes {
35137012946SDavid Rientjes 	nodemask_t tmp;
35237012946SDavid Rientjes 
35337012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES) {
3541d0d2680SDavid Rientjes 		int node = first_node(pol->w.user_nodemask);
3551d0d2680SDavid Rientjes 
356fc36b8d3SLee Schermerhorn 		if (node_isset(node, *nodes)) {
3571d0d2680SDavid Rientjes 			pol->v.preferred_node = node;
358fc36b8d3SLee Schermerhorn 			pol->flags &= ~MPOL_F_LOCAL;
359fc36b8d3SLee Schermerhorn 		} else
360fc36b8d3SLee Schermerhorn 			pol->flags |= MPOL_F_LOCAL;
36137012946SDavid Rientjes 	} else if (pol->flags & MPOL_F_RELATIVE_NODES) {
36237012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
3631d0d2680SDavid Rientjes 		pol->v.preferred_node = first_node(tmp);
364fc36b8d3SLee Schermerhorn 	} else if (!(pol->flags & MPOL_F_LOCAL)) {
3651d0d2680SDavid Rientjes 		pol->v.preferred_node = node_remap(pol->v.preferred_node,
36637012946SDavid Rientjes 						   pol->w.cpuset_mems_allowed,
36737012946SDavid Rientjes 						   *nodes);
36837012946SDavid Rientjes 		pol->w.cpuset_mems_allowed = *nodes;
3691d0d2680SDavid Rientjes 	}
3701d0d2680SDavid Rientjes }
37137012946SDavid Rientjes 
372708c1bbcSMiao Xie /*
373708c1bbcSMiao Xie  * mpol_rebind_policy - Migrate a policy to a different set of nodes
374708c1bbcSMiao Xie  *
375708c1bbcSMiao Xie  * If read-side task has no lock to protect task->mempolicy, write-side
376708c1bbcSMiao Xie  * task will rebind the task->mempolicy by two step. The first step is
377708c1bbcSMiao Xie  * setting all the newly nodes, and the second step is cleaning all the
378708c1bbcSMiao Xie  * disallowed nodes. In this way, we can avoid finding no node to alloc
379708c1bbcSMiao Xie  * page.
380708c1bbcSMiao Xie  * If we have a lock to protect task->mempolicy in read-side, we do
381708c1bbcSMiao Xie  * rebind directly.
382708c1bbcSMiao Xie  *
383708c1bbcSMiao Xie  * step:
384708c1bbcSMiao Xie  * 	MPOL_REBIND_ONCE  - do rebind work at once
385708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP1 - set all the newly nodes
386708c1bbcSMiao Xie  * 	MPOL_REBIND_STEP2 - clean all the disallowed nodes
387708c1bbcSMiao Xie  */
388708c1bbcSMiao Xie static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask,
389708c1bbcSMiao Xie 				enum mpol_rebind_step step)
39037012946SDavid Rientjes {
39137012946SDavid Rientjes 	if (!pol)
39237012946SDavid Rientjes 		return;
39389c522c7SWang Sheng-Hui 	if (!mpol_store_user_nodemask(pol) && step == MPOL_REBIND_ONCE &&
39437012946SDavid Rientjes 	    nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
39537012946SDavid Rientjes 		return;
396708c1bbcSMiao Xie 
397708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING))
398708c1bbcSMiao Xie 		return;
399708c1bbcSMiao Xie 
400708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING))
401708c1bbcSMiao Xie 		BUG();
402708c1bbcSMiao Xie 
403708c1bbcSMiao Xie 	if (step == MPOL_REBIND_STEP1)
404708c1bbcSMiao Xie 		pol->flags |= MPOL_F_REBINDING;
405708c1bbcSMiao Xie 	else if (step == MPOL_REBIND_STEP2)
406708c1bbcSMiao Xie 		pol->flags &= ~MPOL_F_REBINDING;
407708c1bbcSMiao Xie 	else if (step >= MPOL_REBIND_NSTEP)
408708c1bbcSMiao Xie 		BUG();
409708c1bbcSMiao Xie 
410708c1bbcSMiao Xie 	mpol_ops[pol->mode].rebind(pol, newmask, step);
4111d0d2680SDavid Rientjes }
4121d0d2680SDavid Rientjes 
4131d0d2680SDavid Rientjes /*
4141d0d2680SDavid Rientjes  * Wrapper for mpol_rebind_policy() that just requires task
4151d0d2680SDavid Rientjes  * pointer, and updates task mempolicy.
41658568d2aSMiao Xie  *
41758568d2aSMiao Xie  * Called with task's alloc_lock held.
4181d0d2680SDavid Rientjes  */
4191d0d2680SDavid Rientjes 
420708c1bbcSMiao Xie void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
421708c1bbcSMiao Xie 			enum mpol_rebind_step step)
4221d0d2680SDavid Rientjes {
423708c1bbcSMiao Xie 	mpol_rebind_policy(tsk->mempolicy, new, step);
4241d0d2680SDavid Rientjes }
4251d0d2680SDavid Rientjes 
4261d0d2680SDavid Rientjes /*
4271d0d2680SDavid Rientjes  * Rebind each vma in mm to new nodemask.
4281d0d2680SDavid Rientjes  *
4291d0d2680SDavid Rientjes  * Call holding a reference to mm.  Takes mm->mmap_sem during call.
4301d0d2680SDavid Rientjes  */
4311d0d2680SDavid Rientjes 
4321d0d2680SDavid Rientjes void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
4331d0d2680SDavid Rientjes {
4341d0d2680SDavid Rientjes 	struct vm_area_struct *vma;
4351d0d2680SDavid Rientjes 
4361d0d2680SDavid Rientjes 	down_write(&mm->mmap_sem);
4371d0d2680SDavid Rientjes 	for (vma = mm->mmap; vma; vma = vma->vm_next)
438708c1bbcSMiao Xie 		mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE);
4391d0d2680SDavid Rientjes 	up_write(&mm->mmap_sem);
4401d0d2680SDavid Rientjes }
4411d0d2680SDavid Rientjes 
44237012946SDavid Rientjes static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
44337012946SDavid Rientjes 	[MPOL_DEFAULT] = {
44437012946SDavid Rientjes 		.rebind = mpol_rebind_default,
44537012946SDavid Rientjes 	},
44637012946SDavid Rientjes 	[MPOL_INTERLEAVE] = {
44737012946SDavid Rientjes 		.create = mpol_new_interleave,
44837012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
44937012946SDavid Rientjes 	},
45037012946SDavid Rientjes 	[MPOL_PREFERRED] = {
45137012946SDavid Rientjes 		.create = mpol_new_preferred,
45237012946SDavid Rientjes 		.rebind = mpol_rebind_preferred,
45337012946SDavid Rientjes 	},
45437012946SDavid Rientjes 	[MPOL_BIND] = {
45537012946SDavid Rientjes 		.create = mpol_new_bind,
45637012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
45737012946SDavid Rientjes 	},
45837012946SDavid Rientjes };
45937012946SDavid Rientjes 
460fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
461fc301289SChristoph Lameter 				unsigned long flags);
4621a75a6c8SChristoph Lameter 
46338e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */
464b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
465dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
466dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
46738e35860SChristoph Lameter 		void *private)
4681da177e4SLinus Torvalds {
46991612e0dSHugh Dickins 	pte_t *orig_pte;
47091612e0dSHugh Dickins 	pte_t *pte;
471705e87c0SHugh Dickins 	spinlock_t *ptl;
472941150a3SHugh Dickins 
473705e87c0SHugh Dickins 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
47491612e0dSHugh Dickins 	do {
4756aab341eSLinus Torvalds 		struct page *page;
47625ba77c1SAndy Whitcroft 		int nid;
47791612e0dSHugh Dickins 
47891612e0dSHugh Dickins 		if (!pte_present(*pte))
47991612e0dSHugh Dickins 			continue;
4806aab341eSLinus Torvalds 		page = vm_normal_page(vma, addr, *pte);
4816aab341eSLinus Torvalds 		if (!page)
48291612e0dSHugh Dickins 			continue;
483053837fcSNick Piggin 		/*
48462b61f61SHugh Dickins 		 * vm_normal_page() filters out zero pages, but there might
48562b61f61SHugh Dickins 		 * still be PageReserved pages to skip, perhaps in a VDSO.
48662b61f61SHugh Dickins 		 * And we cannot move PageKsm pages sensibly or safely yet.
487053837fcSNick Piggin 		 */
48862b61f61SHugh Dickins 		if (PageReserved(page) || PageKsm(page))
489f4598c8bSChristoph Lameter 			continue;
4906aab341eSLinus Torvalds 		nid = page_to_nid(page);
49138e35860SChristoph Lameter 		if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
49238e35860SChristoph Lameter 			continue;
49338e35860SChristoph Lameter 
494b1f72d18SStephen Wilson 		if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
495fc301289SChristoph Lameter 			migrate_page_add(page, private, flags);
496dc9aa5b9SChristoph Lameter 		else
4971da177e4SLinus Torvalds 			break;
49891612e0dSHugh Dickins 	} while (pte++, addr += PAGE_SIZE, addr != end);
499705e87c0SHugh Dickins 	pte_unmap_unlock(orig_pte, ptl);
50091612e0dSHugh Dickins 	return addr != end;
50191612e0dSHugh Dickins }
50291612e0dSHugh Dickins 
503b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
504dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
505dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
50638e35860SChristoph Lameter 		void *private)
50791612e0dSHugh Dickins {
50891612e0dSHugh Dickins 	pmd_t *pmd;
50991612e0dSHugh Dickins 	unsigned long next;
51091612e0dSHugh Dickins 
51191612e0dSHugh Dickins 	pmd = pmd_offset(pud, addr);
51291612e0dSHugh Dickins 	do {
51391612e0dSHugh Dickins 		next = pmd_addr_end(addr, end);
514bae9c19bSAndrea Arcangeli 		split_huge_page_pmd(vma->vm_mm, pmd);
5151a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
51691612e0dSHugh Dickins 			continue;
517dc9aa5b9SChristoph Lameter 		if (check_pte_range(vma, pmd, addr, next, nodes,
51838e35860SChristoph Lameter 				    flags, private))
51991612e0dSHugh Dickins 			return -EIO;
52091612e0dSHugh Dickins 	} while (pmd++, addr = next, addr != end);
52191612e0dSHugh Dickins 	return 0;
52291612e0dSHugh Dickins }
52391612e0dSHugh Dickins 
524b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
525dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
526dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
52738e35860SChristoph Lameter 		void *private)
52891612e0dSHugh Dickins {
52991612e0dSHugh Dickins 	pud_t *pud;
53091612e0dSHugh Dickins 	unsigned long next;
53191612e0dSHugh Dickins 
53291612e0dSHugh Dickins 	pud = pud_offset(pgd, addr);
53391612e0dSHugh Dickins 	do {
53491612e0dSHugh Dickins 		next = pud_addr_end(addr, end);
53591612e0dSHugh Dickins 		if (pud_none_or_clear_bad(pud))
53691612e0dSHugh Dickins 			continue;
537dc9aa5b9SChristoph Lameter 		if (check_pmd_range(vma, pud, addr, next, nodes,
53838e35860SChristoph Lameter 				    flags, private))
53991612e0dSHugh Dickins 			return -EIO;
54091612e0dSHugh Dickins 	} while (pud++, addr = next, addr != end);
54191612e0dSHugh Dickins 	return 0;
54291612e0dSHugh Dickins }
54391612e0dSHugh Dickins 
544b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma,
545dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
546dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
54738e35860SChristoph Lameter 		void *private)
54891612e0dSHugh Dickins {
54991612e0dSHugh Dickins 	pgd_t *pgd;
55091612e0dSHugh Dickins 	unsigned long next;
55191612e0dSHugh Dickins 
552b5810039SNick Piggin 	pgd = pgd_offset(vma->vm_mm, addr);
55391612e0dSHugh Dickins 	do {
55491612e0dSHugh Dickins 		next = pgd_addr_end(addr, end);
55591612e0dSHugh Dickins 		if (pgd_none_or_clear_bad(pgd))
55691612e0dSHugh Dickins 			continue;
557dc9aa5b9SChristoph Lameter 		if (check_pud_range(vma, pgd, addr, next, nodes,
55838e35860SChristoph Lameter 				    flags, private))
55991612e0dSHugh Dickins 			return -EIO;
56091612e0dSHugh Dickins 	} while (pgd++, addr = next, addr != end);
56191612e0dSHugh Dickins 	return 0;
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
564dc9aa5b9SChristoph Lameter /*
565dc9aa5b9SChristoph Lameter  * Check if all pages in a range are on a set of nodes.
566dc9aa5b9SChristoph Lameter  * If pagelist != NULL then isolate pages from the LRU and
567dc9aa5b9SChristoph Lameter  * put them on the pagelist.
568dc9aa5b9SChristoph Lameter  */
5691da177e4SLinus Torvalds static struct vm_area_struct *
5701da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
57138e35860SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags, void *private)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	int err;
5741da177e4SLinus Torvalds 	struct vm_area_struct *first, *vma, *prev;
5751da177e4SLinus Torvalds 
576053837fcSNick Piggin 
5771da177e4SLinus Torvalds 	first = find_vma(mm, start);
5781da177e4SLinus Torvalds 	if (!first)
5791da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
5801da177e4SLinus Torvalds 	prev = NULL;
5811da177e4SLinus Torvalds 	for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
582dc9aa5b9SChristoph Lameter 		if (!(flags & MPOL_MF_DISCONTIG_OK)) {
5831da177e4SLinus Torvalds 			if (!vma->vm_next && vma->vm_end < end)
5841da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
5851da177e4SLinus Torvalds 			if (prev && prev->vm_end < vma->vm_start)
5861da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
587dc9aa5b9SChristoph Lameter 		}
588dc9aa5b9SChristoph Lameter 		if (!is_vm_hugetlb_page(vma) &&
589dc9aa5b9SChristoph Lameter 		    ((flags & MPOL_MF_STRICT) ||
590dc9aa5b9SChristoph Lameter 		     ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
591dc9aa5b9SChristoph Lameter 				vma_migratable(vma)))) {
5925b952b3cSAndi Kleen 			unsigned long endvma = vma->vm_end;
593dc9aa5b9SChristoph Lameter 
5945b952b3cSAndi Kleen 			if (endvma > end)
5955b952b3cSAndi Kleen 				endvma = end;
5965b952b3cSAndi Kleen 			if (vma->vm_start > start)
5975b952b3cSAndi Kleen 				start = vma->vm_start;
598dc9aa5b9SChristoph Lameter 			err = check_pgd_range(vma, start, endvma, nodes,
59938e35860SChristoph Lameter 						flags, private);
6001da177e4SLinus Torvalds 			if (err) {
6011da177e4SLinus Torvalds 				first = ERR_PTR(err);
6021da177e4SLinus Torvalds 				break;
6031da177e4SLinus Torvalds 			}
6041da177e4SLinus Torvalds 		}
6051da177e4SLinus Torvalds 		prev = vma;
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 	return first;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610869833f2SKOSAKI Motohiro /*
611869833f2SKOSAKI Motohiro  * Apply policy to a single VMA
612869833f2SKOSAKI Motohiro  * This must be called with the mmap_sem held for writing.
613869833f2SKOSAKI Motohiro  */
614869833f2SKOSAKI Motohiro static int vma_replace_policy(struct vm_area_struct *vma,
615869833f2SKOSAKI Motohiro 						struct mempolicy *pol)
6168d34694cSKOSAKI Motohiro {
617869833f2SKOSAKI Motohiro 	int err;
618869833f2SKOSAKI Motohiro 	struct mempolicy *old;
619869833f2SKOSAKI Motohiro 	struct mempolicy *new;
6208d34694cSKOSAKI Motohiro 
6218d34694cSKOSAKI Motohiro 	pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
6228d34694cSKOSAKI Motohiro 		 vma->vm_start, vma->vm_end, vma->vm_pgoff,
6238d34694cSKOSAKI Motohiro 		 vma->vm_ops, vma->vm_file,
6248d34694cSKOSAKI Motohiro 		 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
6258d34694cSKOSAKI Motohiro 
626869833f2SKOSAKI Motohiro 	new = mpol_dup(pol);
627869833f2SKOSAKI Motohiro 	if (IS_ERR(new))
628869833f2SKOSAKI Motohiro 		return PTR_ERR(new);
629869833f2SKOSAKI Motohiro 
630869833f2SKOSAKI Motohiro 	if (vma->vm_ops && vma->vm_ops->set_policy) {
6318d34694cSKOSAKI Motohiro 		err = vma->vm_ops->set_policy(vma, new);
632869833f2SKOSAKI Motohiro 		if (err)
633869833f2SKOSAKI Motohiro 			goto err_out;
6348d34694cSKOSAKI Motohiro 	}
635869833f2SKOSAKI Motohiro 
636869833f2SKOSAKI Motohiro 	old = vma->vm_policy;
637869833f2SKOSAKI Motohiro 	vma->vm_policy = new; /* protected by mmap_sem */
638869833f2SKOSAKI Motohiro 	mpol_put(old);
639869833f2SKOSAKI Motohiro 
640869833f2SKOSAKI Motohiro 	return 0;
641869833f2SKOSAKI Motohiro  err_out:
642869833f2SKOSAKI Motohiro 	mpol_put(new);
6438d34694cSKOSAKI Motohiro 	return err;
6448d34694cSKOSAKI Motohiro }
6458d34694cSKOSAKI Motohiro 
6461da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */
6479d8cebd4SKOSAKI Motohiro static int mbind_range(struct mm_struct *mm, unsigned long start,
6489d8cebd4SKOSAKI Motohiro 		       unsigned long end, struct mempolicy *new_pol)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	struct vm_area_struct *next;
6519d8cebd4SKOSAKI Motohiro 	struct vm_area_struct *prev;
6529d8cebd4SKOSAKI Motohiro 	struct vm_area_struct *vma;
6539d8cebd4SKOSAKI Motohiro 	int err = 0;
654e26a5114SKOSAKI Motohiro 	pgoff_t pgoff;
6559d8cebd4SKOSAKI Motohiro 	unsigned long vmstart;
6569d8cebd4SKOSAKI Motohiro 	unsigned long vmend;
6571da177e4SLinus Torvalds 
658097d5910SLinus Torvalds 	vma = find_vma(mm, start);
6599d8cebd4SKOSAKI Motohiro 	if (!vma || vma->vm_start > start)
6609d8cebd4SKOSAKI Motohiro 		return -EFAULT;
6619d8cebd4SKOSAKI Motohiro 
662097d5910SLinus Torvalds 	prev = vma->vm_prev;
663e26a5114SKOSAKI Motohiro 	if (start > vma->vm_start)
664e26a5114SKOSAKI Motohiro 		prev = vma;
665e26a5114SKOSAKI Motohiro 
6669d8cebd4SKOSAKI Motohiro 	for (; vma && vma->vm_start < end; prev = vma, vma = next) {
6671da177e4SLinus Torvalds 		next = vma->vm_next;
6689d8cebd4SKOSAKI Motohiro 		vmstart = max(start, vma->vm_start);
6699d8cebd4SKOSAKI Motohiro 		vmend   = min(end, vma->vm_end);
6709d8cebd4SKOSAKI Motohiro 
671e26a5114SKOSAKI Motohiro 		if (mpol_equal(vma_policy(vma), new_pol))
672e26a5114SKOSAKI Motohiro 			continue;
673e26a5114SKOSAKI Motohiro 
674e26a5114SKOSAKI Motohiro 		pgoff = vma->vm_pgoff +
675e26a5114SKOSAKI Motohiro 			((vmstart - vma->vm_start) >> PAGE_SHIFT);
6769d8cebd4SKOSAKI Motohiro 		prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
677e26a5114SKOSAKI Motohiro 				  vma->anon_vma, vma->vm_file, pgoff,
6788aacc9f5SCaspar Zhang 				  new_pol);
6799d8cebd4SKOSAKI Motohiro 		if (prev) {
6809d8cebd4SKOSAKI Motohiro 			vma = prev;
6819d8cebd4SKOSAKI Motohiro 			next = vma->vm_next;
6829d8cebd4SKOSAKI Motohiro 			continue;
6831da177e4SLinus Torvalds 		}
6849d8cebd4SKOSAKI Motohiro 		if (vma->vm_start != vmstart) {
6859d8cebd4SKOSAKI Motohiro 			err = split_vma(vma->vm_mm, vma, vmstart, 1);
6869d8cebd4SKOSAKI Motohiro 			if (err)
6879d8cebd4SKOSAKI Motohiro 				goto out;
6889d8cebd4SKOSAKI Motohiro 		}
6899d8cebd4SKOSAKI Motohiro 		if (vma->vm_end != vmend) {
6909d8cebd4SKOSAKI Motohiro 			err = split_vma(vma->vm_mm, vma, vmend, 0);
6919d8cebd4SKOSAKI Motohiro 			if (err)
6929d8cebd4SKOSAKI Motohiro 				goto out;
6939d8cebd4SKOSAKI Motohiro 		}
694869833f2SKOSAKI Motohiro 		err = vma_replace_policy(vma, new_pol);
6959d8cebd4SKOSAKI Motohiro 		if (err)
6969d8cebd4SKOSAKI Motohiro 			goto out;
6979d8cebd4SKOSAKI Motohiro 	}
6989d8cebd4SKOSAKI Motohiro 
6999d8cebd4SKOSAKI Motohiro  out:
7001da177e4SLinus Torvalds 	return err;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
703c61afb18SPaul Jackson /*
704c61afb18SPaul Jackson  * Update task->flags PF_MEMPOLICY bit: set iff non-default
705c61afb18SPaul Jackson  * mempolicy.  Allows more rapid checking of this (combined perhaps
706c61afb18SPaul Jackson  * with other PF_* flag bits) on memory allocation hot code paths.
707c61afb18SPaul Jackson  *
708c61afb18SPaul Jackson  * If called from outside this file, the task 'p' should -only- be
709c61afb18SPaul Jackson  * a newly forked child not yet visible on the task list, because
710c61afb18SPaul Jackson  * manipulating the task flags of a visible task is not safe.
711c61afb18SPaul Jackson  *
712c61afb18SPaul Jackson  * The above limitation is why this routine has the funny name
713c61afb18SPaul Jackson  * mpol_fix_fork_child_flag().
714c61afb18SPaul Jackson  *
715c61afb18SPaul Jackson  * It is also safe to call this with a task pointer of current,
716c61afb18SPaul Jackson  * which the static wrapper mpol_set_task_struct_flag() does,
717c61afb18SPaul Jackson  * for use within this file.
718c61afb18SPaul Jackson  */
719c61afb18SPaul Jackson 
720c61afb18SPaul Jackson void mpol_fix_fork_child_flag(struct task_struct *p)
721c61afb18SPaul Jackson {
722c61afb18SPaul Jackson 	if (p->mempolicy)
723c61afb18SPaul Jackson 		p->flags |= PF_MEMPOLICY;
724c61afb18SPaul Jackson 	else
725c61afb18SPaul Jackson 		p->flags &= ~PF_MEMPOLICY;
726c61afb18SPaul Jackson }
727c61afb18SPaul Jackson 
728c61afb18SPaul Jackson static void mpol_set_task_struct_flag(void)
729c61afb18SPaul Jackson {
730c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(current);
731c61afb18SPaul Jackson }
732c61afb18SPaul Jackson 
7331da177e4SLinus Torvalds /* Set the process memory policy */
734028fec41SDavid Rientjes static long do_set_mempolicy(unsigned short mode, unsigned short flags,
735028fec41SDavid Rientjes 			     nodemask_t *nodes)
7361da177e4SLinus Torvalds {
73758568d2aSMiao Xie 	struct mempolicy *new, *old;
738f4e53d91SLee Schermerhorn 	struct mm_struct *mm = current->mm;
7394bfc4495SKAMEZAWA Hiroyuki 	NODEMASK_SCRATCH(scratch);
74058568d2aSMiao Xie 	int ret;
7411da177e4SLinus Torvalds 
7424bfc4495SKAMEZAWA Hiroyuki 	if (!scratch)
7434bfc4495SKAMEZAWA Hiroyuki 		return -ENOMEM;
744f4e53d91SLee Schermerhorn 
7454bfc4495SKAMEZAWA Hiroyuki 	new = mpol_new(mode, flags, nodes);
7464bfc4495SKAMEZAWA Hiroyuki 	if (IS_ERR(new)) {
7474bfc4495SKAMEZAWA Hiroyuki 		ret = PTR_ERR(new);
7484bfc4495SKAMEZAWA Hiroyuki 		goto out;
7494bfc4495SKAMEZAWA Hiroyuki 	}
750f4e53d91SLee Schermerhorn 	/*
751f4e53d91SLee Schermerhorn 	 * prevent changing our mempolicy while show_numa_maps()
752f4e53d91SLee Schermerhorn 	 * is using it.
753f4e53d91SLee Schermerhorn 	 * Note:  do_set_mempolicy() can be called at init time
754f4e53d91SLee Schermerhorn 	 * with no 'mm'.
755f4e53d91SLee Schermerhorn 	 */
756f4e53d91SLee Schermerhorn 	if (mm)
757f4e53d91SLee Schermerhorn 		down_write(&mm->mmap_sem);
75858568d2aSMiao Xie 	task_lock(current);
7594bfc4495SKAMEZAWA Hiroyuki 	ret = mpol_set_nodemask(new, nodes, scratch);
76058568d2aSMiao Xie 	if (ret) {
76158568d2aSMiao Xie 		task_unlock(current);
76258568d2aSMiao Xie 		if (mm)
76358568d2aSMiao Xie 			up_write(&mm->mmap_sem);
76458568d2aSMiao Xie 		mpol_put(new);
7654bfc4495SKAMEZAWA Hiroyuki 		goto out;
76658568d2aSMiao Xie 	}
76758568d2aSMiao Xie 	old = current->mempolicy;
7681da177e4SLinus Torvalds 	current->mempolicy = new;
769c61afb18SPaul Jackson 	mpol_set_task_struct_flag();
77045c4745aSLee Schermerhorn 	if (new && new->mode == MPOL_INTERLEAVE &&
771f5b087b5SDavid Rientjes 	    nodes_weight(new->v.nodes))
772dfcd3c0dSAndi Kleen 		current->il_next = first_node(new->v.nodes);
77358568d2aSMiao Xie 	task_unlock(current);
774f4e53d91SLee Schermerhorn 	if (mm)
775f4e53d91SLee Schermerhorn 		up_write(&mm->mmap_sem);
776f4e53d91SLee Schermerhorn 
77758568d2aSMiao Xie 	mpol_put(old);
7784bfc4495SKAMEZAWA Hiroyuki 	ret = 0;
7794bfc4495SKAMEZAWA Hiroyuki out:
7804bfc4495SKAMEZAWA Hiroyuki 	NODEMASK_SCRATCH_FREE(scratch);
7814bfc4495SKAMEZAWA Hiroyuki 	return ret;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
784bea904d5SLee Schermerhorn /*
785bea904d5SLee Schermerhorn  * Return nodemask for policy for get_mempolicy() query
78658568d2aSMiao Xie  *
78758568d2aSMiao Xie  * Called with task's alloc_lock held
788bea904d5SLee Schermerhorn  */
789bea904d5SLee Schermerhorn static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
7901da177e4SLinus Torvalds {
791dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
792bea904d5SLee Schermerhorn 	if (p == &default_policy)
793bea904d5SLee Schermerhorn 		return;
794bea904d5SLee Schermerhorn 
79545c4745aSLee Schermerhorn 	switch (p->mode) {
79619770b32SMel Gorman 	case MPOL_BIND:
79719770b32SMel Gorman 		/* Fall through */
7981da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
799dfcd3c0dSAndi Kleen 		*nodes = p->v.nodes;
8001da177e4SLinus Torvalds 		break;
8011da177e4SLinus Torvalds 	case MPOL_PREFERRED:
802fc36b8d3SLee Schermerhorn 		if (!(p->flags & MPOL_F_LOCAL))
803dfcd3c0dSAndi Kleen 			node_set(p->v.preferred_node, *nodes);
80453f2556bSLee Schermerhorn 		/* else return empty node mask for local allocation */
8051da177e4SLinus Torvalds 		break;
8061da177e4SLinus Torvalds 	default:
8071da177e4SLinus Torvalds 		BUG();
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	struct page *p;
8141da177e4SLinus Torvalds 	int err;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
8171da177e4SLinus Torvalds 	if (err >= 0) {
8181da177e4SLinus Torvalds 		err = page_to_nid(p);
8191da177e4SLinus Torvalds 		put_page(p);
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 	return err;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds /* Retrieve NUMA policy */
825dbcb0f19SAdrian Bunk static long do_get_mempolicy(int *policy, nodemask_t *nmask,
8261da177e4SLinus Torvalds 			     unsigned long addr, unsigned long flags)
8271da177e4SLinus Torvalds {
8288bccd85fSChristoph Lameter 	int err;
8291da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
8301da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
8311da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
8321da177e4SLinus Torvalds 
833754af6f5SLee Schermerhorn 	if (flags &
834754af6f5SLee Schermerhorn 		~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
8351da177e4SLinus Torvalds 		return -EINVAL;
836754af6f5SLee Schermerhorn 
837754af6f5SLee Schermerhorn 	if (flags & MPOL_F_MEMS_ALLOWED) {
838754af6f5SLee Schermerhorn 		if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
839754af6f5SLee Schermerhorn 			return -EINVAL;
840754af6f5SLee Schermerhorn 		*policy = 0;	/* just so it's initialized */
84158568d2aSMiao Xie 		task_lock(current);
842754af6f5SLee Schermerhorn 		*nmask  = cpuset_current_mems_allowed;
84358568d2aSMiao Xie 		task_unlock(current);
844754af6f5SLee Schermerhorn 		return 0;
845754af6f5SLee Schermerhorn 	}
846754af6f5SLee Schermerhorn 
8471da177e4SLinus Torvalds 	if (flags & MPOL_F_ADDR) {
848bea904d5SLee Schermerhorn 		/*
849bea904d5SLee Schermerhorn 		 * Do NOT fall back to task policy if the
850bea904d5SLee Schermerhorn 		 * vma/shared policy at addr is NULL.  We
851bea904d5SLee Schermerhorn 		 * want to return MPOL_DEFAULT in this case.
852bea904d5SLee Schermerhorn 		 */
8531da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
8541da177e4SLinus Torvalds 		vma = find_vma_intersection(mm, addr, addr+1);
8551da177e4SLinus Torvalds 		if (!vma) {
8561da177e4SLinus Torvalds 			up_read(&mm->mmap_sem);
8571da177e4SLinus Torvalds 			return -EFAULT;
8581da177e4SLinus Torvalds 		}
8591da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
8601da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
8611da177e4SLinus Torvalds 		else
8621da177e4SLinus Torvalds 			pol = vma->vm_policy;
8631da177e4SLinus Torvalds 	} else if (addr)
8641da177e4SLinus Torvalds 		return -EINVAL;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	if (!pol)
867bea904d5SLee Schermerhorn 		pol = &default_policy;	/* indicates default behavior */
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	if (flags & MPOL_F_NODE) {
8701da177e4SLinus Torvalds 		if (flags & MPOL_F_ADDR) {
8711da177e4SLinus Torvalds 			err = lookup_node(mm, addr);
8721da177e4SLinus Torvalds 			if (err < 0)
8731da177e4SLinus Torvalds 				goto out;
8748bccd85fSChristoph Lameter 			*policy = err;
8751da177e4SLinus Torvalds 		} else if (pol == current->mempolicy &&
87645c4745aSLee Schermerhorn 				pol->mode == MPOL_INTERLEAVE) {
8778bccd85fSChristoph Lameter 			*policy = current->il_next;
8781da177e4SLinus Torvalds 		} else {
8791da177e4SLinus Torvalds 			err = -EINVAL;
8801da177e4SLinus Torvalds 			goto out;
8811da177e4SLinus Torvalds 		}
882bea904d5SLee Schermerhorn 	} else {
883bea904d5SLee Schermerhorn 		*policy = pol == &default_policy ? MPOL_DEFAULT :
884bea904d5SLee Schermerhorn 						pol->mode;
885d79df630SDavid Rientjes 		/*
886d79df630SDavid Rientjes 		 * Internal mempolicy flags must be masked off before exposing
887d79df630SDavid Rientjes 		 * the policy to userspace.
888d79df630SDavid Rientjes 		 */
889d79df630SDavid Rientjes 		*policy |= (pol->flags & MPOL_MODE_FLAGS);
890bea904d5SLee Schermerhorn 	}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	if (vma) {
8931da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
8941da177e4SLinus Torvalds 		vma = NULL;
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	err = 0;
89858568d2aSMiao Xie 	if (nmask) {
899c6b6ef8bSLee Schermerhorn 		if (mpol_store_user_nodemask(pol)) {
900c6b6ef8bSLee Schermerhorn 			*nmask = pol->w.user_nodemask;
901c6b6ef8bSLee Schermerhorn 		} else {
90258568d2aSMiao Xie 			task_lock(current);
903bea904d5SLee Schermerhorn 			get_policy_nodemask(pol, nmask);
90458568d2aSMiao Xie 			task_unlock(current);
90558568d2aSMiao Xie 		}
906c6b6ef8bSLee Schermerhorn 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds  out:
90952cd3b07SLee Schermerhorn 	mpol_cond_put(pol);
9101da177e4SLinus Torvalds 	if (vma)
9111da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
9121da177e4SLinus Torvalds 	return err;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
915b20a3503SChristoph Lameter #ifdef CONFIG_MIGRATION
9168bccd85fSChristoph Lameter /*
9176ce3c4c0SChristoph Lameter  * page migration
9186ce3c4c0SChristoph Lameter  */
919fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
920fc301289SChristoph Lameter 				unsigned long flags)
9216ce3c4c0SChristoph Lameter {
9226ce3c4c0SChristoph Lameter 	/*
923fc301289SChristoph Lameter 	 * Avoid migrating a page that is shared with others.
9246ce3c4c0SChristoph Lameter 	 */
92562695a84SNick Piggin 	if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
92662695a84SNick Piggin 		if (!isolate_lru_page(page)) {
92762695a84SNick Piggin 			list_add_tail(&page->lru, pagelist);
9286d9c285aSKOSAKI Motohiro 			inc_zone_page_state(page, NR_ISOLATED_ANON +
9296d9c285aSKOSAKI Motohiro 					    page_is_file_cache(page));
93062695a84SNick Piggin 		}
93162695a84SNick Piggin 	}
9326ce3c4c0SChristoph Lameter }
9336ce3c4c0SChristoph Lameter 
934742755a1SChristoph Lameter static struct page *new_node_page(struct page *page, unsigned long node, int **x)
93595a402c3SChristoph Lameter {
9366484eb3eSMel Gorman 	return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
93795a402c3SChristoph Lameter }
93895a402c3SChristoph Lameter 
9396ce3c4c0SChristoph Lameter /*
9407e2ab150SChristoph Lameter  * Migrate pages from one node to a target node.
9417e2ab150SChristoph Lameter  * Returns error or the number of pages not migrated.
9427e2ab150SChristoph Lameter  */
943dbcb0f19SAdrian Bunk static int migrate_to_node(struct mm_struct *mm, int source, int dest,
944dbcb0f19SAdrian Bunk 			   int flags)
9457e2ab150SChristoph Lameter {
9467e2ab150SChristoph Lameter 	nodemask_t nmask;
9477e2ab150SChristoph Lameter 	LIST_HEAD(pagelist);
9487e2ab150SChristoph Lameter 	int err = 0;
9497e2ab150SChristoph Lameter 
9507e2ab150SChristoph Lameter 	nodes_clear(nmask);
9517e2ab150SChristoph Lameter 	node_set(source, nmask);
9527e2ab150SChristoph Lameter 
953*08270807SMinchan Kim 	/*
954*08270807SMinchan Kim 	 * This does not "check" the range but isolates all pages that
955*08270807SMinchan Kim 	 * need migration.  Between passing in the full user address
956*08270807SMinchan Kim 	 * space range and MPOL_MF_DISCONTIG_OK, this call can not fail.
957*08270807SMinchan Kim 	 */
958*08270807SMinchan Kim 	VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)));
959*08270807SMinchan Kim 	check_range(mm, mm->mmap->vm_start, mm->task_size, &nmask,
9607e2ab150SChristoph Lameter 			flags | MPOL_MF_DISCONTIG_OK, &pagelist);
9617e2ab150SChristoph Lameter 
962cf608ac1SMinchan Kim 	if (!list_empty(&pagelist)) {
9637f0f2496SMel Gorman 		err = migrate_pages(&pagelist, new_node_page, dest,
964a6bc32b8SMel Gorman 							false, MIGRATE_SYNC);
965cf608ac1SMinchan Kim 		if (err)
966cf608ac1SMinchan Kim 			putback_lru_pages(&pagelist);
967cf608ac1SMinchan Kim 	}
96895a402c3SChristoph Lameter 
9697e2ab150SChristoph Lameter 	return err;
9707e2ab150SChristoph Lameter }
9717e2ab150SChristoph Lameter 
9727e2ab150SChristoph Lameter /*
9737e2ab150SChristoph Lameter  * Move pages between the two nodesets so as to preserve the physical
9747e2ab150SChristoph Lameter  * layout as much as possible.
97539743889SChristoph Lameter  *
97639743889SChristoph Lameter  * Returns the number of page that could not be moved.
97739743889SChristoph Lameter  */
9780ce72d4fSAndrew Morton int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
9790ce72d4fSAndrew Morton 		     const nodemask_t *to, int flags)
98039743889SChristoph Lameter {
9817e2ab150SChristoph Lameter 	int busy = 0;
9820aedadf9SChristoph Lameter 	int err;
9837e2ab150SChristoph Lameter 	nodemask_t tmp;
98439743889SChristoph Lameter 
9850aedadf9SChristoph Lameter 	err = migrate_prep();
9860aedadf9SChristoph Lameter 	if (err)
9870aedadf9SChristoph Lameter 		return err;
9880aedadf9SChristoph Lameter 
98939743889SChristoph Lameter 	down_read(&mm->mmap_sem);
990d4984711SChristoph Lameter 
9910ce72d4fSAndrew Morton 	err = migrate_vmas(mm, from, to, flags);
9927b2259b3SChristoph Lameter 	if (err)
9937b2259b3SChristoph Lameter 		goto out;
9947b2259b3SChristoph Lameter 
9957e2ab150SChristoph Lameter 	/*
9967e2ab150SChristoph Lameter 	 * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
9977e2ab150SChristoph Lameter 	 * bit in 'to' is not also set in 'tmp'.  Clear the found 'source'
9987e2ab150SChristoph Lameter 	 * bit in 'tmp', and return that <source, dest> pair for migration.
9997e2ab150SChristoph Lameter 	 * The pair of nodemasks 'to' and 'from' define the map.
10007e2ab150SChristoph Lameter 	 *
10017e2ab150SChristoph Lameter 	 * If no pair of bits is found that way, fallback to picking some
10027e2ab150SChristoph Lameter 	 * pair of 'source' and 'dest' bits that are not the same.  If the
10037e2ab150SChristoph Lameter 	 * 'source' and 'dest' bits are the same, this represents a node
10047e2ab150SChristoph Lameter 	 * that will be migrating to itself, so no pages need move.
10057e2ab150SChristoph Lameter 	 *
10067e2ab150SChristoph Lameter 	 * If no bits are left in 'tmp', or if all remaining bits left
10077e2ab150SChristoph Lameter 	 * in 'tmp' correspond to the same bit in 'to', return false
10087e2ab150SChristoph Lameter 	 * (nothing left to migrate).
10097e2ab150SChristoph Lameter 	 *
10107e2ab150SChristoph Lameter 	 * This lets us pick a pair of nodes to migrate between, such that
10117e2ab150SChristoph Lameter 	 * if possible the dest node is not already occupied by some other
10127e2ab150SChristoph Lameter 	 * source node, minimizing the risk of overloading the memory on a
10137e2ab150SChristoph Lameter 	 * node that would happen if we migrated incoming memory to a node
10147e2ab150SChristoph Lameter 	 * before migrating outgoing memory source that same node.
10157e2ab150SChristoph Lameter 	 *
10167e2ab150SChristoph Lameter 	 * A single scan of tmp is sufficient.  As we go, we remember the
10177e2ab150SChristoph Lameter 	 * most recent <s, d> pair that moved (s != d).  If we find a pair
10187e2ab150SChristoph Lameter 	 * that not only moved, but what's better, moved to an empty slot
10197e2ab150SChristoph Lameter 	 * (d is not set in tmp), then we break out then, with that pair.
1020ae0e47f0SJustin P. Mattock 	 * Otherwise when we finish scanning from_tmp, we at least have the
10217e2ab150SChristoph Lameter 	 * most recent <s, d> pair that moved.  If we get all the way through
10227e2ab150SChristoph Lameter 	 * the scan of tmp without finding any node that moved, much less
10237e2ab150SChristoph Lameter 	 * moved to an empty node, then there is nothing left worth migrating.
10247e2ab150SChristoph Lameter 	 */
10257e2ab150SChristoph Lameter 
10260ce72d4fSAndrew Morton 	tmp = *from;
10277e2ab150SChristoph Lameter 	while (!nodes_empty(tmp)) {
10287e2ab150SChristoph Lameter 		int s,d;
10297e2ab150SChristoph Lameter 		int source = -1;
10307e2ab150SChristoph Lameter 		int dest = 0;
10317e2ab150SChristoph Lameter 
10327e2ab150SChristoph Lameter 		for_each_node_mask(s, tmp) {
10334a5b18ccSLarry Woodman 
10344a5b18ccSLarry Woodman 			/*
10354a5b18ccSLarry Woodman 			 * do_migrate_pages() tries to maintain the relative
10364a5b18ccSLarry Woodman 			 * node relationship of the pages established between
10374a5b18ccSLarry Woodman 			 * threads and memory areas.
10384a5b18ccSLarry Woodman                          *
10394a5b18ccSLarry Woodman 			 * However if the number of source nodes is not equal to
10404a5b18ccSLarry Woodman 			 * the number of destination nodes we can not preserve
10414a5b18ccSLarry Woodman 			 * this node relative relationship.  In that case, skip
10424a5b18ccSLarry Woodman 			 * copying memory from a node that is in the destination
10434a5b18ccSLarry Woodman 			 * mask.
10444a5b18ccSLarry Woodman 			 *
10454a5b18ccSLarry Woodman 			 * Example: [2,3,4] -> [3,4,5] moves everything.
10464a5b18ccSLarry Woodman 			 *          [0-7] - > [3,4,5] moves only 0,1,2,6,7.
10474a5b18ccSLarry Woodman 			 */
10484a5b18ccSLarry Woodman 
10490ce72d4fSAndrew Morton 			if ((nodes_weight(*from) != nodes_weight(*to)) &&
10500ce72d4fSAndrew Morton 						(node_isset(s, *to)))
10514a5b18ccSLarry Woodman 				continue;
10524a5b18ccSLarry Woodman 
10530ce72d4fSAndrew Morton 			d = node_remap(s, *from, *to);
10547e2ab150SChristoph Lameter 			if (s == d)
10557e2ab150SChristoph Lameter 				continue;
10567e2ab150SChristoph Lameter 
10577e2ab150SChristoph Lameter 			source = s;	/* Node moved. Memorize */
10587e2ab150SChristoph Lameter 			dest = d;
10597e2ab150SChristoph Lameter 
10607e2ab150SChristoph Lameter 			/* dest not in remaining from nodes? */
10617e2ab150SChristoph Lameter 			if (!node_isset(dest, tmp))
10627e2ab150SChristoph Lameter 				break;
10637e2ab150SChristoph Lameter 		}
10647e2ab150SChristoph Lameter 		if (source == -1)
10657e2ab150SChristoph Lameter 			break;
10667e2ab150SChristoph Lameter 
10677e2ab150SChristoph Lameter 		node_clear(source, tmp);
10687e2ab150SChristoph Lameter 		err = migrate_to_node(mm, source, dest, flags);
10697e2ab150SChristoph Lameter 		if (err > 0)
10707e2ab150SChristoph Lameter 			busy += err;
10717e2ab150SChristoph Lameter 		if (err < 0)
10727e2ab150SChristoph Lameter 			break;
107339743889SChristoph Lameter 	}
10747b2259b3SChristoph Lameter out:
107539743889SChristoph Lameter 	up_read(&mm->mmap_sem);
10767e2ab150SChristoph Lameter 	if (err < 0)
10777e2ab150SChristoph Lameter 		return err;
10787e2ab150SChristoph Lameter 	return busy;
1079b20a3503SChristoph Lameter 
108039743889SChristoph Lameter }
108139743889SChristoph Lameter 
10823ad33b24SLee Schermerhorn /*
10833ad33b24SLee Schermerhorn  * Allocate a new page for page migration based on vma policy.
10843ad33b24SLee Schermerhorn  * Start assuming that page is mapped by vma pointed to by @private.
10853ad33b24SLee Schermerhorn  * Search forward from there, if not.  N.B., this assumes that the
10863ad33b24SLee Schermerhorn  * list of pages handed to migrate_pages()--which is how we get here--
10873ad33b24SLee Schermerhorn  * is in virtual address order.
10883ad33b24SLee Schermerhorn  */
1089742755a1SChristoph Lameter static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
109095a402c3SChristoph Lameter {
109195a402c3SChristoph Lameter 	struct vm_area_struct *vma = (struct vm_area_struct *)private;
10923ad33b24SLee Schermerhorn 	unsigned long uninitialized_var(address);
109395a402c3SChristoph Lameter 
10943ad33b24SLee Schermerhorn 	while (vma) {
10953ad33b24SLee Schermerhorn 		address = page_address_in_vma(page, vma);
10963ad33b24SLee Schermerhorn 		if (address != -EFAULT)
10973ad33b24SLee Schermerhorn 			break;
10983ad33b24SLee Schermerhorn 		vma = vma->vm_next;
10993ad33b24SLee Schermerhorn 	}
11003ad33b24SLee Schermerhorn 
11013ad33b24SLee Schermerhorn 	/*
11023ad33b24SLee Schermerhorn 	 * if !vma, alloc_page_vma() will use task or system default policy
11033ad33b24SLee Schermerhorn 	 */
11043ad33b24SLee Schermerhorn 	return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
110595a402c3SChristoph Lameter }
1106b20a3503SChristoph Lameter #else
1107b20a3503SChristoph Lameter 
1108b20a3503SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
1109b20a3503SChristoph Lameter 				unsigned long flags)
1110b20a3503SChristoph Lameter {
1111b20a3503SChristoph Lameter }
1112b20a3503SChristoph Lameter 
11130ce72d4fSAndrew Morton int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
11140ce72d4fSAndrew Morton 		     const nodemask_t *to, int flags)
1115b20a3503SChristoph Lameter {
1116b20a3503SChristoph Lameter 	return -ENOSYS;
1117b20a3503SChristoph Lameter }
111895a402c3SChristoph Lameter 
111969939749SKeith Owens static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
112095a402c3SChristoph Lameter {
112195a402c3SChristoph Lameter 	return NULL;
112295a402c3SChristoph Lameter }
1123b20a3503SChristoph Lameter #endif
1124b20a3503SChristoph Lameter 
1125dbcb0f19SAdrian Bunk static long do_mbind(unsigned long start, unsigned long len,
1126028fec41SDavid Rientjes 		     unsigned short mode, unsigned short mode_flags,
1127028fec41SDavid Rientjes 		     nodemask_t *nmask, unsigned long flags)
11286ce3c4c0SChristoph Lameter {
11296ce3c4c0SChristoph Lameter 	struct vm_area_struct *vma;
11306ce3c4c0SChristoph Lameter 	struct mm_struct *mm = current->mm;
11316ce3c4c0SChristoph Lameter 	struct mempolicy *new;
11326ce3c4c0SChristoph Lameter 	unsigned long end;
11336ce3c4c0SChristoph Lameter 	int err;
11346ce3c4c0SChristoph Lameter 	LIST_HEAD(pagelist);
11356ce3c4c0SChristoph Lameter 
1136a3b51e01SDavid Rientjes 	if (flags & ~(unsigned long)(MPOL_MF_STRICT |
11376ce3c4c0SChristoph Lameter 				     MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
11386ce3c4c0SChristoph Lameter 		return -EINVAL;
113974c00241SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
11406ce3c4c0SChristoph Lameter 		return -EPERM;
11416ce3c4c0SChristoph Lameter 
11426ce3c4c0SChristoph Lameter 	if (start & ~PAGE_MASK)
11436ce3c4c0SChristoph Lameter 		return -EINVAL;
11446ce3c4c0SChristoph Lameter 
11456ce3c4c0SChristoph Lameter 	if (mode == MPOL_DEFAULT)
11466ce3c4c0SChristoph Lameter 		flags &= ~MPOL_MF_STRICT;
11476ce3c4c0SChristoph Lameter 
11486ce3c4c0SChristoph Lameter 	len = (len + PAGE_SIZE - 1) & PAGE_MASK;
11496ce3c4c0SChristoph Lameter 	end = start + len;
11506ce3c4c0SChristoph Lameter 
11516ce3c4c0SChristoph Lameter 	if (end < start)
11526ce3c4c0SChristoph Lameter 		return -EINVAL;
11536ce3c4c0SChristoph Lameter 	if (end == start)
11546ce3c4c0SChristoph Lameter 		return 0;
11556ce3c4c0SChristoph Lameter 
1156028fec41SDavid Rientjes 	new = mpol_new(mode, mode_flags, nmask);
11576ce3c4c0SChristoph Lameter 	if (IS_ERR(new))
11586ce3c4c0SChristoph Lameter 		return PTR_ERR(new);
11596ce3c4c0SChristoph Lameter 
11606ce3c4c0SChristoph Lameter 	/*
11616ce3c4c0SChristoph Lameter 	 * If we are using the default policy then operation
11626ce3c4c0SChristoph Lameter 	 * on discontinuous address spaces is okay after all
11636ce3c4c0SChristoph Lameter 	 */
11646ce3c4c0SChristoph Lameter 	if (!new)
11656ce3c4c0SChristoph Lameter 		flags |= MPOL_MF_DISCONTIG_OK;
11666ce3c4c0SChristoph Lameter 
1167028fec41SDavid Rientjes 	pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
1168028fec41SDavid Rientjes 		 start, start + len, mode, mode_flags,
1169028fec41SDavid Rientjes 		 nmask ? nodes_addr(*nmask)[0] : -1);
11706ce3c4c0SChristoph Lameter 
11710aedadf9SChristoph Lameter 	if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
11720aedadf9SChristoph Lameter 
11730aedadf9SChristoph Lameter 		err = migrate_prep();
11740aedadf9SChristoph Lameter 		if (err)
1175b05ca738SKOSAKI Motohiro 			goto mpol_out;
11760aedadf9SChristoph Lameter 	}
11774bfc4495SKAMEZAWA Hiroyuki 	{
11784bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
11794bfc4495SKAMEZAWA Hiroyuki 		if (scratch) {
11806ce3c4c0SChristoph Lameter 			down_write(&mm->mmap_sem);
118158568d2aSMiao Xie 			task_lock(current);
11824bfc4495SKAMEZAWA Hiroyuki 			err = mpol_set_nodemask(new, nmask, scratch);
118358568d2aSMiao Xie 			task_unlock(current);
11844bfc4495SKAMEZAWA Hiroyuki 			if (err)
118558568d2aSMiao Xie 				up_write(&mm->mmap_sem);
11864bfc4495SKAMEZAWA Hiroyuki 		} else
11874bfc4495SKAMEZAWA Hiroyuki 			err = -ENOMEM;
11884bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
11894bfc4495SKAMEZAWA Hiroyuki 	}
1190b05ca738SKOSAKI Motohiro 	if (err)
1191b05ca738SKOSAKI Motohiro 		goto mpol_out;
1192b05ca738SKOSAKI Motohiro 
11936ce3c4c0SChristoph Lameter 	vma = check_range(mm, start, end, nmask,
11946ce3c4c0SChristoph Lameter 			  flags | MPOL_MF_INVERT, &pagelist);
11956ce3c4c0SChristoph Lameter 
11966ce3c4c0SChristoph Lameter 	err = PTR_ERR(vma);
11976ce3c4c0SChristoph Lameter 	if (!IS_ERR(vma)) {
11986ce3c4c0SChristoph Lameter 		int nr_failed = 0;
11996ce3c4c0SChristoph Lameter 
12009d8cebd4SKOSAKI Motohiro 		err = mbind_range(mm, start, end, new);
12017e2ab150SChristoph Lameter 
1202cf608ac1SMinchan Kim 		if (!list_empty(&pagelist)) {
120395a402c3SChristoph Lameter 			nr_failed = migrate_pages(&pagelist, new_vma_page,
12047f0f2496SMel Gorman 						(unsigned long)vma,
1205c4c0e9e5SDavid Rientjes 						false, MIGRATE_SYNC);
1206cf608ac1SMinchan Kim 			if (nr_failed)
1207cf608ac1SMinchan Kim 				putback_lru_pages(&pagelist);
1208cf608ac1SMinchan Kim 		}
12096ce3c4c0SChristoph Lameter 
12106ce3c4c0SChristoph Lameter 		if (!err && nr_failed && (flags & MPOL_MF_STRICT))
12116ce3c4c0SChristoph Lameter 			err = -EIO;
1212ab8a3e14SKOSAKI Motohiro 	} else
1213ab8a3e14SKOSAKI Motohiro 		putback_lru_pages(&pagelist);
1214b20a3503SChristoph Lameter 
12156ce3c4c0SChristoph Lameter 	up_write(&mm->mmap_sem);
1216b05ca738SKOSAKI Motohiro  mpol_out:
1217f0be3d32SLee Schermerhorn 	mpol_put(new);
12186ce3c4c0SChristoph Lameter 	return err;
12196ce3c4c0SChristoph Lameter }
12206ce3c4c0SChristoph Lameter 
122139743889SChristoph Lameter /*
12228bccd85fSChristoph Lameter  * User space interface with variable sized bitmaps for nodelists.
12238bccd85fSChristoph Lameter  */
12248bccd85fSChristoph Lameter 
12258bccd85fSChristoph Lameter /* Copy a node mask from user space. */
122639743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
12278bccd85fSChristoph Lameter 		     unsigned long maxnode)
12288bccd85fSChristoph Lameter {
12298bccd85fSChristoph Lameter 	unsigned long k;
12308bccd85fSChristoph Lameter 	unsigned long nlongs;
12318bccd85fSChristoph Lameter 	unsigned long endmask;
12328bccd85fSChristoph Lameter 
12338bccd85fSChristoph Lameter 	--maxnode;
12348bccd85fSChristoph Lameter 	nodes_clear(*nodes);
12358bccd85fSChristoph Lameter 	if (maxnode == 0 || !nmask)
12368bccd85fSChristoph Lameter 		return 0;
1237a9c930baSAndi Kleen 	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
1238636f13c1SChris Wright 		return -EINVAL;
12398bccd85fSChristoph Lameter 
12408bccd85fSChristoph Lameter 	nlongs = BITS_TO_LONGS(maxnode);
12418bccd85fSChristoph Lameter 	if ((maxnode % BITS_PER_LONG) == 0)
12428bccd85fSChristoph Lameter 		endmask = ~0UL;
12438bccd85fSChristoph Lameter 	else
12448bccd85fSChristoph Lameter 		endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
12458bccd85fSChristoph Lameter 
12468bccd85fSChristoph Lameter 	/* When the user specified more nodes than supported just check
12478bccd85fSChristoph Lameter 	   if the non supported part is all zero. */
12488bccd85fSChristoph Lameter 	if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
12498bccd85fSChristoph Lameter 		if (nlongs > PAGE_SIZE/sizeof(long))
12508bccd85fSChristoph Lameter 			return -EINVAL;
12518bccd85fSChristoph Lameter 		for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
12528bccd85fSChristoph Lameter 			unsigned long t;
12538bccd85fSChristoph Lameter 			if (get_user(t, nmask + k))
12548bccd85fSChristoph Lameter 				return -EFAULT;
12558bccd85fSChristoph Lameter 			if (k == nlongs - 1) {
12568bccd85fSChristoph Lameter 				if (t & endmask)
12578bccd85fSChristoph Lameter 					return -EINVAL;
12588bccd85fSChristoph Lameter 			} else if (t)
12598bccd85fSChristoph Lameter 				return -EINVAL;
12608bccd85fSChristoph Lameter 		}
12618bccd85fSChristoph Lameter 		nlongs = BITS_TO_LONGS(MAX_NUMNODES);
12628bccd85fSChristoph Lameter 		endmask = ~0UL;
12638bccd85fSChristoph Lameter 	}
12648bccd85fSChristoph Lameter 
12658bccd85fSChristoph Lameter 	if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
12668bccd85fSChristoph Lameter 		return -EFAULT;
12678bccd85fSChristoph Lameter 	nodes_addr(*nodes)[nlongs-1] &= endmask;
12688bccd85fSChristoph Lameter 	return 0;
12698bccd85fSChristoph Lameter }
12708bccd85fSChristoph Lameter 
12718bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */
12728bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
12738bccd85fSChristoph Lameter 			      nodemask_t *nodes)
12748bccd85fSChristoph Lameter {
12758bccd85fSChristoph Lameter 	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
12768bccd85fSChristoph Lameter 	const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
12778bccd85fSChristoph Lameter 
12788bccd85fSChristoph Lameter 	if (copy > nbytes) {
12798bccd85fSChristoph Lameter 		if (copy > PAGE_SIZE)
12808bccd85fSChristoph Lameter 			return -EINVAL;
12818bccd85fSChristoph Lameter 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
12828bccd85fSChristoph Lameter 			return -EFAULT;
12838bccd85fSChristoph Lameter 		copy = nbytes;
12848bccd85fSChristoph Lameter 	}
12858bccd85fSChristoph Lameter 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
12868bccd85fSChristoph Lameter }
12878bccd85fSChristoph Lameter 
1288938bb9f5SHeiko Carstens SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
1289938bb9f5SHeiko Carstens 		unsigned long, mode, unsigned long __user *, nmask,
1290938bb9f5SHeiko Carstens 		unsigned long, maxnode, unsigned, flags)
12918bccd85fSChristoph Lameter {
12928bccd85fSChristoph Lameter 	nodemask_t nodes;
12938bccd85fSChristoph Lameter 	int err;
1294028fec41SDavid Rientjes 	unsigned short mode_flags;
12958bccd85fSChristoph Lameter 
1296028fec41SDavid Rientjes 	mode_flags = mode & MPOL_MODE_FLAGS;
1297028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1298a3b51e01SDavid Rientjes 	if (mode >= MPOL_MAX)
1299a3b51e01SDavid Rientjes 		return -EINVAL;
13004c50bc01SDavid Rientjes 	if ((mode_flags & MPOL_F_STATIC_NODES) &&
13014c50bc01SDavid Rientjes 	    (mode_flags & MPOL_F_RELATIVE_NODES))
13024c50bc01SDavid Rientjes 		return -EINVAL;
13038bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
13048bccd85fSChristoph Lameter 	if (err)
13058bccd85fSChristoph Lameter 		return err;
1306028fec41SDavid Rientjes 	return do_mbind(start, len, mode, mode_flags, &nodes, flags);
13078bccd85fSChristoph Lameter }
13088bccd85fSChristoph Lameter 
13098bccd85fSChristoph Lameter /* Set the process memory policy */
1310938bb9f5SHeiko Carstens SYSCALL_DEFINE3(set_mempolicy, int, mode, unsigned long __user *, nmask,
1311938bb9f5SHeiko Carstens 		unsigned long, maxnode)
13128bccd85fSChristoph Lameter {
13138bccd85fSChristoph Lameter 	int err;
13148bccd85fSChristoph Lameter 	nodemask_t nodes;
1315028fec41SDavid Rientjes 	unsigned short flags;
13168bccd85fSChristoph Lameter 
1317028fec41SDavid Rientjes 	flags = mode & MPOL_MODE_FLAGS;
1318028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1319028fec41SDavid Rientjes 	if ((unsigned int)mode >= MPOL_MAX)
13208bccd85fSChristoph Lameter 		return -EINVAL;
13214c50bc01SDavid Rientjes 	if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
13224c50bc01SDavid Rientjes 		return -EINVAL;
13238bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
13248bccd85fSChristoph Lameter 	if (err)
13258bccd85fSChristoph Lameter 		return err;
1326028fec41SDavid Rientjes 	return do_set_mempolicy(mode, flags, &nodes);
13278bccd85fSChristoph Lameter }
13288bccd85fSChristoph Lameter 
1329938bb9f5SHeiko Carstens SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
1330938bb9f5SHeiko Carstens 		const unsigned long __user *, old_nodes,
1331938bb9f5SHeiko Carstens 		const unsigned long __user *, new_nodes)
133239743889SChristoph Lameter {
1333c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
1334596d7cfaSKOSAKI Motohiro 	struct mm_struct *mm = NULL;
133539743889SChristoph Lameter 	struct task_struct *task;
133639743889SChristoph Lameter 	nodemask_t task_nodes;
133739743889SChristoph Lameter 	int err;
1338596d7cfaSKOSAKI Motohiro 	nodemask_t *old;
1339596d7cfaSKOSAKI Motohiro 	nodemask_t *new;
1340596d7cfaSKOSAKI Motohiro 	NODEMASK_SCRATCH(scratch);
134139743889SChristoph Lameter 
1342596d7cfaSKOSAKI Motohiro 	if (!scratch)
1343596d7cfaSKOSAKI Motohiro 		return -ENOMEM;
134439743889SChristoph Lameter 
1345596d7cfaSKOSAKI Motohiro 	old = &scratch->mask1;
1346596d7cfaSKOSAKI Motohiro 	new = &scratch->mask2;
1347596d7cfaSKOSAKI Motohiro 
1348596d7cfaSKOSAKI Motohiro 	err = get_nodes(old, old_nodes, maxnode);
134939743889SChristoph Lameter 	if (err)
1350596d7cfaSKOSAKI Motohiro 		goto out;
1351596d7cfaSKOSAKI Motohiro 
1352596d7cfaSKOSAKI Motohiro 	err = get_nodes(new, new_nodes, maxnode);
1353596d7cfaSKOSAKI Motohiro 	if (err)
1354596d7cfaSKOSAKI Motohiro 		goto out;
135539743889SChristoph Lameter 
135639743889SChristoph Lameter 	/* Find the mm_struct */
135755cfaa3cSZeng Zhaoming 	rcu_read_lock();
1358228ebcbeSPavel Emelyanov 	task = pid ? find_task_by_vpid(pid) : current;
135939743889SChristoph Lameter 	if (!task) {
136055cfaa3cSZeng Zhaoming 		rcu_read_unlock();
1361596d7cfaSKOSAKI Motohiro 		err = -ESRCH;
1362596d7cfaSKOSAKI Motohiro 		goto out;
136339743889SChristoph Lameter 	}
13643268c63eSChristoph Lameter 	get_task_struct(task);
136539743889SChristoph Lameter 
1366596d7cfaSKOSAKI Motohiro 	err = -EINVAL;
136739743889SChristoph Lameter 
136839743889SChristoph Lameter 	/*
136939743889SChristoph Lameter 	 * Check if this process has the right to modify the specified
137039743889SChristoph Lameter 	 * process. The right exists if the process has administrative
13717f927fccSAlexey Dobriyan 	 * capabilities, superuser privileges or the same
137239743889SChristoph Lameter 	 * userid as the target process.
137339743889SChristoph Lameter 	 */
1374c69e8d9cSDavid Howells 	tcred = __task_cred(task);
1375b38a86ebSEric W. Biederman 	if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
1376b38a86ebSEric W. Biederman 	    !uid_eq(cred->uid,  tcred->suid) && !uid_eq(cred->uid,  tcred->uid) &&
137774c00241SChristoph Lameter 	    !capable(CAP_SYS_NICE)) {
1378c69e8d9cSDavid Howells 		rcu_read_unlock();
137939743889SChristoph Lameter 		err = -EPERM;
13803268c63eSChristoph Lameter 		goto out_put;
138139743889SChristoph Lameter 	}
1382c69e8d9cSDavid Howells 	rcu_read_unlock();
138339743889SChristoph Lameter 
138439743889SChristoph Lameter 	task_nodes = cpuset_mems_allowed(task);
138539743889SChristoph Lameter 	/* Is the user allowed to access the target nodes? */
1386596d7cfaSKOSAKI Motohiro 	if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
138739743889SChristoph Lameter 		err = -EPERM;
13883268c63eSChristoph Lameter 		goto out_put;
138939743889SChristoph Lameter 	}
139039743889SChristoph Lameter 
1391596d7cfaSKOSAKI Motohiro 	if (!nodes_subset(*new, node_states[N_HIGH_MEMORY])) {
13923b42d28bSChristoph Lameter 		err = -EINVAL;
13933268c63eSChristoph Lameter 		goto out_put;
13943b42d28bSChristoph Lameter 	}
13953b42d28bSChristoph Lameter 
139686c3a764SDavid Quigley 	err = security_task_movememory(task);
139786c3a764SDavid Quigley 	if (err)
13983268c63eSChristoph Lameter 		goto out_put;
139986c3a764SDavid Quigley 
14003268c63eSChristoph Lameter 	mm = get_task_mm(task);
14013268c63eSChristoph Lameter 	put_task_struct(task);
1402f2a9ef88SSasha Levin 
1403f2a9ef88SSasha Levin 	if (!mm) {
1404f2a9ef88SSasha Levin 		err = -EINVAL;
1405f2a9ef88SSasha Levin 		goto out;
1406f2a9ef88SSasha Levin 	}
1407f2a9ef88SSasha Levin 
1408596d7cfaSKOSAKI Motohiro 	err = do_migrate_pages(mm, old, new,
140974c00241SChristoph Lameter 		capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
14103268c63eSChristoph Lameter 
141139743889SChristoph Lameter 	mmput(mm);
14123268c63eSChristoph Lameter out:
1413596d7cfaSKOSAKI Motohiro 	NODEMASK_SCRATCH_FREE(scratch);
1414596d7cfaSKOSAKI Motohiro 
141539743889SChristoph Lameter 	return err;
14163268c63eSChristoph Lameter 
14173268c63eSChristoph Lameter out_put:
14183268c63eSChristoph Lameter 	put_task_struct(task);
14193268c63eSChristoph Lameter 	goto out;
14203268c63eSChristoph Lameter 
142139743889SChristoph Lameter }
142239743889SChristoph Lameter 
142339743889SChristoph Lameter 
14248bccd85fSChristoph Lameter /* Retrieve NUMA policy */
1425938bb9f5SHeiko Carstens SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1426938bb9f5SHeiko Carstens 		unsigned long __user *, nmask, unsigned long, maxnode,
1427938bb9f5SHeiko Carstens 		unsigned long, addr, unsigned long, flags)
14288bccd85fSChristoph Lameter {
1429dbcb0f19SAdrian Bunk 	int err;
1430dbcb0f19SAdrian Bunk 	int uninitialized_var(pval);
14318bccd85fSChristoph Lameter 	nodemask_t nodes;
14328bccd85fSChristoph Lameter 
14338bccd85fSChristoph Lameter 	if (nmask != NULL && maxnode < MAX_NUMNODES)
14348bccd85fSChristoph Lameter 		return -EINVAL;
14358bccd85fSChristoph Lameter 
14368bccd85fSChristoph Lameter 	err = do_get_mempolicy(&pval, &nodes, addr, flags);
14378bccd85fSChristoph Lameter 
14388bccd85fSChristoph Lameter 	if (err)
14398bccd85fSChristoph Lameter 		return err;
14408bccd85fSChristoph Lameter 
14418bccd85fSChristoph Lameter 	if (policy && put_user(pval, policy))
14428bccd85fSChristoph Lameter 		return -EFAULT;
14438bccd85fSChristoph Lameter 
14448bccd85fSChristoph Lameter 	if (nmask)
14458bccd85fSChristoph Lameter 		err = copy_nodes_to_user(nmask, maxnode, &nodes);
14468bccd85fSChristoph Lameter 
14478bccd85fSChristoph Lameter 	return err;
14488bccd85fSChristoph Lameter }
14498bccd85fSChristoph Lameter 
14501da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy,
14531da177e4SLinus Torvalds 				     compat_ulong_t __user *nmask,
14541da177e4SLinus Torvalds 				     compat_ulong_t maxnode,
14551da177e4SLinus Torvalds 				     compat_ulong_t addr, compat_ulong_t flags)
14561da177e4SLinus Torvalds {
14571da177e4SLinus Torvalds 	long err;
14581da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
14591da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
14601da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
14631da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	if (nmask)
14661da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	if (!err && nmask) {
14712bbff6c7SKAMEZAWA Hiroyuki 		unsigned long copy_size;
14722bbff6c7SKAMEZAWA Hiroyuki 		copy_size = min_t(unsigned long, sizeof(bm), alloc_size);
14732bbff6c7SKAMEZAWA Hiroyuki 		err = copy_from_user(bm, nm, copy_size);
14741da177e4SLinus Torvalds 		/* ensure entire bitmap is zeroed */
14751da177e4SLinus Torvalds 		err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
14761da177e4SLinus Torvalds 		err |= compat_put_bitmap(nmask, bm, nr_bits);
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	return err;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
14831da177e4SLinus Torvalds 				     compat_ulong_t maxnode)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	long err = 0;
14861da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
14871da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
14881da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
14911da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	if (nmask) {
14941da177e4SLinus Torvalds 		err = compat_get_bitmap(bm, nmask, nr_bits);
14951da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
14961da177e4SLinus Torvalds 		err |= copy_to_user(nm, bm, alloc_size);
14971da177e4SLinus Torvalds 	}
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	if (err)
15001da177e4SLinus Torvalds 		return -EFAULT;
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	return sys_set_mempolicy(mode, nm, nr_bits+1);
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
15061da177e4SLinus Torvalds 			     compat_ulong_t mode, compat_ulong_t __user *nmask,
15071da177e4SLinus Torvalds 			     compat_ulong_t maxnode, compat_ulong_t flags)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	long err = 0;
15101da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
15111da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
1512dfcd3c0dSAndi Kleen 	nodemask_t bm;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
15151da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	if (nmask) {
1518dfcd3c0dSAndi Kleen 		err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
15191da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
1520dfcd3c0dSAndi Kleen 		err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
15211da177e4SLinus Torvalds 	}
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	if (err)
15241da177e4SLinus Torvalds 		return -EFAULT;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds #endif
15301da177e4SLinus Torvalds 
1531480eccf9SLee Schermerhorn /*
1532480eccf9SLee Schermerhorn  * get_vma_policy(@task, @vma, @addr)
1533480eccf9SLee Schermerhorn  * @task - task for fallback if vma policy == default
1534480eccf9SLee Schermerhorn  * @vma   - virtual memory area whose policy is sought
1535480eccf9SLee Schermerhorn  * @addr  - address in @vma for shared policy lookup
1536480eccf9SLee Schermerhorn  *
1537480eccf9SLee Schermerhorn  * Returns effective policy for a VMA at specified address.
1538480eccf9SLee Schermerhorn  * Falls back to @task or system default policy, as necessary.
153952cd3b07SLee Schermerhorn  * Current or other task's task mempolicy and non-shared vma policies
154052cd3b07SLee Schermerhorn  * are protected by the task's mmap_sem, which must be held for read by
154152cd3b07SLee Schermerhorn  * the caller.
154252cd3b07SLee Schermerhorn  * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
154352cd3b07SLee Schermerhorn  * count--added by the get_policy() vm_op, as appropriate--to protect against
154452cd3b07SLee Schermerhorn  * freeing by another task.  It is the caller's responsibility to free the
154552cd3b07SLee Schermerhorn  * extra reference for shared policies.
1546480eccf9SLee Schermerhorn  */
1547d98f6cb6SStephen Wilson struct mempolicy *get_vma_policy(struct task_struct *task,
154848fce342SChristoph Lameter 		struct vm_area_struct *vma, unsigned long addr)
15491da177e4SLinus Torvalds {
15506e21c8f1SChristoph Lameter 	struct mempolicy *pol = task->mempolicy;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (vma) {
1553480eccf9SLee Schermerhorn 		if (vma->vm_ops && vma->vm_ops->get_policy) {
1554ae4d8c16SLee Schermerhorn 			struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
1555ae4d8c16SLee Schermerhorn 									addr);
1556ae4d8c16SLee Schermerhorn 			if (vpol)
1557ae4d8c16SLee Schermerhorn 				pol = vpol;
155800442ad0SMel Gorman 		} else if (vma->vm_policy) {
15591da177e4SLinus Torvalds 			pol = vma->vm_policy;
156000442ad0SMel Gorman 
156100442ad0SMel Gorman 			/*
156200442ad0SMel Gorman 			 * shmem_alloc_page() passes MPOL_F_SHARED policy with
156300442ad0SMel Gorman 			 * a pseudo vma whose vma->vm_ops=NULL. Take a reference
156400442ad0SMel Gorman 			 * count on these policies which will be dropped by
156500442ad0SMel Gorman 			 * mpol_cond_put() later
156600442ad0SMel Gorman 			 */
156700442ad0SMel Gorman 			if (mpol_needs_cond_ref(pol))
156800442ad0SMel Gorman 				mpol_get(pol);
156900442ad0SMel Gorman 		}
15701da177e4SLinus Torvalds 	}
15711da177e4SLinus Torvalds 	if (!pol)
15721da177e4SLinus Torvalds 		pol = &default_policy;
15731da177e4SLinus Torvalds 	return pol;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
157652cd3b07SLee Schermerhorn /*
157752cd3b07SLee Schermerhorn  * Return a nodemask representing a mempolicy for filtering nodes for
157852cd3b07SLee Schermerhorn  * page allocation
157952cd3b07SLee Schermerhorn  */
158052cd3b07SLee Schermerhorn static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
158119770b32SMel Gorman {
158219770b32SMel Gorman 	/* Lower zones don't get a nodemask applied for MPOL_BIND */
158345c4745aSLee Schermerhorn 	if (unlikely(policy->mode == MPOL_BIND) &&
158419770b32SMel Gorman 			gfp_zone(gfp) >= policy_zone &&
158519770b32SMel Gorman 			cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
158619770b32SMel Gorman 		return &policy->v.nodes;
158719770b32SMel Gorman 
158819770b32SMel Gorman 	return NULL;
158919770b32SMel Gorman }
159019770b32SMel Gorman 
159152cd3b07SLee Schermerhorn /* Return a zonelist indicated by gfp for node representing a mempolicy */
15922f5f9486SAndi Kleen static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy,
15932f5f9486SAndi Kleen 	int nd)
15941da177e4SLinus Torvalds {
159545c4745aSLee Schermerhorn 	switch (policy->mode) {
15961da177e4SLinus Torvalds 	case MPOL_PREFERRED:
1597fc36b8d3SLee Schermerhorn 		if (!(policy->flags & MPOL_F_LOCAL))
15981da177e4SLinus Torvalds 			nd = policy->v.preferred_node;
15991da177e4SLinus Torvalds 		break;
16001da177e4SLinus Torvalds 	case MPOL_BIND:
160119770b32SMel Gorman 		/*
160252cd3b07SLee Schermerhorn 		 * Normally, MPOL_BIND allocations are node-local within the
160352cd3b07SLee Schermerhorn 		 * allowed nodemask.  However, if __GFP_THISNODE is set and the
16046eb27e1fSBob Liu 		 * current node isn't part of the mask, we use the zonelist for
160552cd3b07SLee Schermerhorn 		 * the first node in the mask instead.
160619770b32SMel Gorman 		 */
160719770b32SMel Gorman 		if (unlikely(gfp & __GFP_THISNODE) &&
160819770b32SMel Gorman 				unlikely(!node_isset(nd, policy->v.nodes)))
160919770b32SMel Gorman 			nd = first_node(policy->v.nodes);
161019770b32SMel Gorman 		break;
16111da177e4SLinus Torvalds 	default:
16121da177e4SLinus Torvalds 		BUG();
16131da177e4SLinus Torvalds 	}
16140e88460dSMel Gorman 	return node_zonelist(nd, gfp);
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds /* Do dynamic interleaving for a process */
16181da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	unsigned nid, next;
16211da177e4SLinus Torvalds 	struct task_struct *me = current;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	nid = me->il_next;
1624dfcd3c0dSAndi Kleen 	next = next_node(nid, policy->v.nodes);
16251da177e4SLinus Torvalds 	if (next >= MAX_NUMNODES)
1626dfcd3c0dSAndi Kleen 		next = first_node(policy->v.nodes);
1627f5b087b5SDavid Rientjes 	if (next < MAX_NUMNODES)
16281da177e4SLinus Torvalds 		me->il_next = next;
16291da177e4SLinus Torvalds 	return nid;
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds 
1632dc85da15SChristoph Lameter /*
1633dc85da15SChristoph Lameter  * Depending on the memory policy provide a node from which to allocate the
1634dc85da15SChristoph Lameter  * next slab entry.
163552cd3b07SLee Schermerhorn  * @policy must be protected by freeing by the caller.  If @policy is
163652cd3b07SLee Schermerhorn  * the current task's mempolicy, this protection is implicit, as only the
163752cd3b07SLee Schermerhorn  * task can change it's policy.  The system default policy requires no
163852cd3b07SLee Schermerhorn  * such protection.
1639dc85da15SChristoph Lameter  */
1640e7b691b0SAndi Kleen unsigned slab_node(void)
1641dc85da15SChristoph Lameter {
1642e7b691b0SAndi Kleen 	struct mempolicy *policy;
1643e7b691b0SAndi Kleen 
1644e7b691b0SAndi Kleen 	if (in_interrupt())
1645e7b691b0SAndi Kleen 		return numa_node_id();
1646e7b691b0SAndi Kleen 
1647e7b691b0SAndi Kleen 	policy = current->mempolicy;
1648fc36b8d3SLee Schermerhorn 	if (!policy || policy->flags & MPOL_F_LOCAL)
1649bea904d5SLee Schermerhorn 		return numa_node_id();
1650765c4507SChristoph Lameter 
1651bea904d5SLee Schermerhorn 	switch (policy->mode) {
1652bea904d5SLee Schermerhorn 	case MPOL_PREFERRED:
1653fc36b8d3SLee Schermerhorn 		/*
1654fc36b8d3SLee Schermerhorn 		 * handled MPOL_F_LOCAL above
1655fc36b8d3SLee Schermerhorn 		 */
1656bea904d5SLee Schermerhorn 		return policy->v.preferred_node;
1657bea904d5SLee Schermerhorn 
1658dc85da15SChristoph Lameter 	case MPOL_INTERLEAVE:
1659dc85da15SChristoph Lameter 		return interleave_nodes(policy);
1660dc85da15SChristoph Lameter 
1661dd1a239fSMel Gorman 	case MPOL_BIND: {
1662dc85da15SChristoph Lameter 		/*
1663dc85da15SChristoph Lameter 		 * Follow bind policy behavior and start allocation at the
1664dc85da15SChristoph Lameter 		 * first node.
1665dc85da15SChristoph Lameter 		 */
166619770b32SMel Gorman 		struct zonelist *zonelist;
166719770b32SMel Gorman 		struct zone *zone;
166819770b32SMel Gorman 		enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
166919770b32SMel Gorman 		zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0];
167019770b32SMel Gorman 		(void)first_zones_zonelist(zonelist, highest_zoneidx,
167119770b32SMel Gorman 							&policy->v.nodes,
167219770b32SMel Gorman 							&zone);
1673800416f7SEric Dumazet 		return zone ? zone->node : numa_node_id();
1674dd1a239fSMel Gorman 	}
1675dc85da15SChristoph Lameter 
1676dc85da15SChristoph Lameter 	default:
1677bea904d5SLee Schermerhorn 		BUG();
1678dc85da15SChristoph Lameter 	}
1679dc85da15SChristoph Lameter }
1680dc85da15SChristoph Lameter 
16811da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */
16821da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol,
16831da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long off)
16841da177e4SLinus Torvalds {
1685dfcd3c0dSAndi Kleen 	unsigned nnodes = nodes_weight(pol->v.nodes);
1686f5b087b5SDavid Rientjes 	unsigned target;
16871da177e4SLinus Torvalds 	int c;
16881da177e4SLinus Torvalds 	int nid = -1;
16891da177e4SLinus Torvalds 
1690f5b087b5SDavid Rientjes 	if (!nnodes)
1691f5b087b5SDavid Rientjes 		return numa_node_id();
1692f5b087b5SDavid Rientjes 	target = (unsigned int)off % nnodes;
16931da177e4SLinus Torvalds 	c = 0;
16941da177e4SLinus Torvalds 	do {
1695dfcd3c0dSAndi Kleen 		nid = next_node(nid, pol->v.nodes);
16961da177e4SLinus Torvalds 		c++;
16971da177e4SLinus Torvalds 	} while (c <= target);
16981da177e4SLinus Torvalds 	return nid;
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
17015da7ca86SChristoph Lameter /* Determine a node number for interleave */
17025da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol,
17035da7ca86SChristoph Lameter 		 struct vm_area_struct *vma, unsigned long addr, int shift)
17045da7ca86SChristoph Lameter {
17055da7ca86SChristoph Lameter 	if (vma) {
17065da7ca86SChristoph Lameter 		unsigned long off;
17075da7ca86SChristoph Lameter 
17083b98b087SNishanth Aravamudan 		/*
17093b98b087SNishanth Aravamudan 		 * for small pages, there is no difference between
17103b98b087SNishanth Aravamudan 		 * shift and PAGE_SHIFT, so the bit-shift is safe.
17113b98b087SNishanth Aravamudan 		 * for huge pages, since vm_pgoff is in units of small
17123b98b087SNishanth Aravamudan 		 * pages, we need to shift off the always 0 bits to get
17133b98b087SNishanth Aravamudan 		 * a useful offset.
17143b98b087SNishanth Aravamudan 		 */
17153b98b087SNishanth Aravamudan 		BUG_ON(shift < PAGE_SHIFT);
17163b98b087SNishanth Aravamudan 		off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
17175da7ca86SChristoph Lameter 		off += (addr - vma->vm_start) >> shift;
17185da7ca86SChristoph Lameter 		return offset_il_node(pol, vma, off);
17195da7ca86SChristoph Lameter 	} else
17205da7ca86SChristoph Lameter 		return interleave_nodes(pol);
17215da7ca86SChristoph Lameter }
17225da7ca86SChristoph Lameter 
1723778d3b0fSMichal Hocko /*
1724778d3b0fSMichal Hocko  * Return the bit number of a random bit set in the nodemask.
1725778d3b0fSMichal Hocko  * (returns -1 if nodemask is empty)
1726778d3b0fSMichal Hocko  */
1727778d3b0fSMichal Hocko int node_random(const nodemask_t *maskp)
1728778d3b0fSMichal Hocko {
1729778d3b0fSMichal Hocko 	int w, bit = -1;
1730778d3b0fSMichal Hocko 
1731778d3b0fSMichal Hocko 	w = nodes_weight(*maskp);
1732778d3b0fSMichal Hocko 	if (w)
1733778d3b0fSMichal Hocko 		bit = bitmap_ord_to_pos(maskp->bits,
1734778d3b0fSMichal Hocko 			get_random_int() % w, MAX_NUMNODES);
1735778d3b0fSMichal Hocko 	return bit;
1736778d3b0fSMichal Hocko }
1737778d3b0fSMichal Hocko 
173800ac59adSChen, Kenneth W #ifdef CONFIG_HUGETLBFS
1739480eccf9SLee Schermerhorn /*
1740480eccf9SLee Schermerhorn  * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
1741480eccf9SLee Schermerhorn  * @vma = virtual memory area whose policy is sought
1742480eccf9SLee Schermerhorn  * @addr = address in @vma for shared policy lookup and interleave policy
1743480eccf9SLee Schermerhorn  * @gfp_flags = for requested zone
174419770b32SMel Gorman  * @mpol = pointer to mempolicy pointer for reference counted mempolicy
174519770b32SMel Gorman  * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask
1746480eccf9SLee Schermerhorn  *
174752cd3b07SLee Schermerhorn  * Returns a zonelist suitable for a huge page allocation and a pointer
174852cd3b07SLee Schermerhorn  * to the struct mempolicy for conditional unref after allocation.
174952cd3b07SLee Schermerhorn  * If the effective policy is 'BIND, returns a pointer to the mempolicy's
175052cd3b07SLee Schermerhorn  * @nodemask for filtering the zonelist.
1751c0ff7453SMiao Xie  *
1752c0ff7453SMiao Xie  * Must be protected by get_mems_allowed()
1753480eccf9SLee Schermerhorn  */
1754396faf03SMel Gorman struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
175519770b32SMel Gorman 				gfp_t gfp_flags, struct mempolicy **mpol,
175619770b32SMel Gorman 				nodemask_t **nodemask)
17575da7ca86SChristoph Lameter {
1758480eccf9SLee Schermerhorn 	struct zonelist *zl;
17595da7ca86SChristoph Lameter 
176052cd3b07SLee Schermerhorn 	*mpol = get_vma_policy(current, vma, addr);
176119770b32SMel Gorman 	*nodemask = NULL;	/* assume !MPOL_BIND */
17625da7ca86SChristoph Lameter 
176352cd3b07SLee Schermerhorn 	if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
176452cd3b07SLee Schermerhorn 		zl = node_zonelist(interleave_nid(*mpol, vma, addr,
1765a5516438SAndi Kleen 				huge_page_shift(hstate_vma(vma))), gfp_flags);
176652cd3b07SLee Schermerhorn 	} else {
17672f5f9486SAndi Kleen 		zl = policy_zonelist(gfp_flags, *mpol, numa_node_id());
176852cd3b07SLee Schermerhorn 		if ((*mpol)->mode == MPOL_BIND)
176952cd3b07SLee Schermerhorn 			*nodemask = &(*mpol)->v.nodes;
1770480eccf9SLee Schermerhorn 	}
1771480eccf9SLee Schermerhorn 	return zl;
17725da7ca86SChristoph Lameter }
177306808b08SLee Schermerhorn 
177406808b08SLee Schermerhorn /*
177506808b08SLee Schermerhorn  * init_nodemask_of_mempolicy
177606808b08SLee Schermerhorn  *
177706808b08SLee Schermerhorn  * If the current task's mempolicy is "default" [NULL], return 'false'
177806808b08SLee Schermerhorn  * to indicate default policy.  Otherwise, extract the policy nodemask
177906808b08SLee Schermerhorn  * for 'bind' or 'interleave' policy into the argument nodemask, or
178006808b08SLee Schermerhorn  * initialize the argument nodemask to contain the single node for
178106808b08SLee Schermerhorn  * 'preferred' or 'local' policy and return 'true' to indicate presence
178206808b08SLee Schermerhorn  * of non-default mempolicy.
178306808b08SLee Schermerhorn  *
178406808b08SLee Schermerhorn  * We don't bother with reference counting the mempolicy [mpol_get/put]
178506808b08SLee Schermerhorn  * because the current task is examining it's own mempolicy and a task's
178606808b08SLee Schermerhorn  * mempolicy is only ever changed by the task itself.
178706808b08SLee Schermerhorn  *
178806808b08SLee Schermerhorn  * N.B., it is the caller's responsibility to free a returned nodemask.
178906808b08SLee Schermerhorn  */
179006808b08SLee Schermerhorn bool init_nodemask_of_mempolicy(nodemask_t *mask)
179106808b08SLee Schermerhorn {
179206808b08SLee Schermerhorn 	struct mempolicy *mempolicy;
179306808b08SLee Schermerhorn 	int nid;
179406808b08SLee Schermerhorn 
179506808b08SLee Schermerhorn 	if (!(mask && current->mempolicy))
179606808b08SLee Schermerhorn 		return false;
179706808b08SLee Schermerhorn 
1798c0ff7453SMiao Xie 	task_lock(current);
179906808b08SLee Schermerhorn 	mempolicy = current->mempolicy;
180006808b08SLee Schermerhorn 	switch (mempolicy->mode) {
180106808b08SLee Schermerhorn 	case MPOL_PREFERRED:
180206808b08SLee Schermerhorn 		if (mempolicy->flags & MPOL_F_LOCAL)
180306808b08SLee Schermerhorn 			nid = numa_node_id();
180406808b08SLee Schermerhorn 		else
180506808b08SLee Schermerhorn 			nid = mempolicy->v.preferred_node;
180606808b08SLee Schermerhorn 		init_nodemask_of_node(mask, nid);
180706808b08SLee Schermerhorn 		break;
180806808b08SLee Schermerhorn 
180906808b08SLee Schermerhorn 	case MPOL_BIND:
181006808b08SLee Schermerhorn 		/* Fall through */
181106808b08SLee Schermerhorn 	case MPOL_INTERLEAVE:
181206808b08SLee Schermerhorn 		*mask =  mempolicy->v.nodes;
181306808b08SLee Schermerhorn 		break;
181406808b08SLee Schermerhorn 
181506808b08SLee Schermerhorn 	default:
181606808b08SLee Schermerhorn 		BUG();
181706808b08SLee Schermerhorn 	}
1818c0ff7453SMiao Xie 	task_unlock(current);
181906808b08SLee Schermerhorn 
182006808b08SLee Schermerhorn 	return true;
182106808b08SLee Schermerhorn }
182200ac59adSChen, Kenneth W #endif
18235da7ca86SChristoph Lameter 
18246f48d0ebSDavid Rientjes /*
18256f48d0ebSDavid Rientjes  * mempolicy_nodemask_intersects
18266f48d0ebSDavid Rientjes  *
18276f48d0ebSDavid Rientjes  * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
18286f48d0ebSDavid Rientjes  * policy.  Otherwise, check for intersection between mask and the policy
18296f48d0ebSDavid Rientjes  * nodemask for 'bind' or 'interleave' policy.  For 'perferred' or 'local'
18306f48d0ebSDavid Rientjes  * policy, always return true since it may allocate elsewhere on fallback.
18316f48d0ebSDavid Rientjes  *
18326f48d0ebSDavid Rientjes  * Takes task_lock(tsk) to prevent freeing of its mempolicy.
18336f48d0ebSDavid Rientjes  */
18346f48d0ebSDavid Rientjes bool mempolicy_nodemask_intersects(struct task_struct *tsk,
18356f48d0ebSDavid Rientjes 					const nodemask_t *mask)
18366f48d0ebSDavid Rientjes {
18376f48d0ebSDavid Rientjes 	struct mempolicy *mempolicy;
18386f48d0ebSDavid Rientjes 	bool ret = true;
18396f48d0ebSDavid Rientjes 
18406f48d0ebSDavid Rientjes 	if (!mask)
18416f48d0ebSDavid Rientjes 		return ret;
18426f48d0ebSDavid Rientjes 	task_lock(tsk);
18436f48d0ebSDavid Rientjes 	mempolicy = tsk->mempolicy;
18446f48d0ebSDavid Rientjes 	if (!mempolicy)
18456f48d0ebSDavid Rientjes 		goto out;
18466f48d0ebSDavid Rientjes 
18476f48d0ebSDavid Rientjes 	switch (mempolicy->mode) {
18486f48d0ebSDavid Rientjes 	case MPOL_PREFERRED:
18496f48d0ebSDavid Rientjes 		/*
18506f48d0ebSDavid Rientjes 		 * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
18516f48d0ebSDavid Rientjes 		 * allocate from, they may fallback to other nodes when oom.
18526f48d0ebSDavid Rientjes 		 * Thus, it's possible for tsk to have allocated memory from
18536f48d0ebSDavid Rientjes 		 * nodes in mask.
18546f48d0ebSDavid Rientjes 		 */
18556f48d0ebSDavid Rientjes 		break;
18566f48d0ebSDavid Rientjes 	case MPOL_BIND:
18576f48d0ebSDavid Rientjes 	case MPOL_INTERLEAVE:
18586f48d0ebSDavid Rientjes 		ret = nodes_intersects(mempolicy->v.nodes, *mask);
18596f48d0ebSDavid Rientjes 		break;
18606f48d0ebSDavid Rientjes 	default:
18616f48d0ebSDavid Rientjes 		BUG();
18626f48d0ebSDavid Rientjes 	}
18636f48d0ebSDavid Rientjes out:
18646f48d0ebSDavid Rientjes 	task_unlock(tsk);
18656f48d0ebSDavid Rientjes 	return ret;
18666f48d0ebSDavid Rientjes }
18676f48d0ebSDavid Rientjes 
18681da177e4SLinus Torvalds /* Allocate a page in interleaved policy.
18691da177e4SLinus Torvalds    Own path because it needs to do special accounting. */
1870662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1871662f3a0bSAndi Kleen 					unsigned nid)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	struct zonelist *zl;
18741da177e4SLinus Torvalds 	struct page *page;
18751da177e4SLinus Torvalds 
18760e88460dSMel Gorman 	zl = node_zonelist(nid, gfp);
18771da177e4SLinus Torvalds 	page = __alloc_pages(gfp, order, zl);
1878dd1a239fSMel Gorman 	if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
1879ca889e6cSChristoph Lameter 		inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
18801da177e4SLinus Torvalds 	return page;
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds /**
18840bbbc0b3SAndrea Arcangeli  * 	alloc_pages_vma	- Allocate a page for a VMA.
18851da177e4SLinus Torvalds  *
18861da177e4SLinus Torvalds  * 	@gfp:
18871da177e4SLinus Torvalds  *      %GFP_USER    user allocation.
18881da177e4SLinus Torvalds  *      %GFP_KERNEL  kernel allocations,
18891da177e4SLinus Torvalds  *      %GFP_HIGHMEM highmem/user allocations,
18901da177e4SLinus Torvalds  *      %GFP_FS      allocation should not call back into a file system.
18911da177e4SLinus Torvalds  *      %GFP_ATOMIC  don't sleep.
18921da177e4SLinus Torvalds  *
18930bbbc0b3SAndrea Arcangeli  *	@order:Order of the GFP allocation.
18941da177e4SLinus Torvalds  * 	@vma:  Pointer to VMA or NULL if not available.
18951da177e4SLinus Torvalds  *	@addr: Virtual Address of the allocation. Must be inside the VMA.
18961da177e4SLinus Torvalds  *
18971da177e4SLinus Torvalds  * 	This function allocates a page from the kernel page pool and applies
18981da177e4SLinus Torvalds  *	a NUMA policy associated with the VMA or the current process.
18991da177e4SLinus Torvalds  *	When VMA is not NULL caller must hold down_read on the mmap_sem of the
19001da177e4SLinus Torvalds  *	mm_struct of the VMA to prevent it from going away. Should be used for
19011da177e4SLinus Torvalds  *	all allocations for pages that will be mapped into
19021da177e4SLinus Torvalds  * 	user space. Returns NULL when no page can be allocated.
19031da177e4SLinus Torvalds  *
19041da177e4SLinus Torvalds  *	Should be called with the mm_sem of the vma hold.
19051da177e4SLinus Torvalds  */
19061da177e4SLinus Torvalds struct page *
19070bbbc0b3SAndrea Arcangeli alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
19082f5f9486SAndi Kleen 		unsigned long addr, int node)
19091da177e4SLinus Torvalds {
1910cc9a6c87SMel Gorman 	struct mempolicy *pol;
1911480eccf9SLee Schermerhorn 	struct zonelist *zl;
1912c0ff7453SMiao Xie 	struct page *page;
1913cc9a6c87SMel Gorman 	unsigned int cpuset_mems_cookie;
19141da177e4SLinus Torvalds 
1915cc9a6c87SMel Gorman retry_cpuset:
1916cc9a6c87SMel Gorman 	pol = get_vma_policy(current, vma, addr);
1917cc9a6c87SMel Gorman 	cpuset_mems_cookie = get_mems_allowed();
1918cc9a6c87SMel Gorman 
191945c4745aSLee Schermerhorn 	if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
19201da177e4SLinus Torvalds 		unsigned nid;
19215da7ca86SChristoph Lameter 
19228eac563cSAndi Kleen 		nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
192352cd3b07SLee Schermerhorn 		mpol_cond_put(pol);
19240bbbc0b3SAndrea Arcangeli 		page = alloc_page_interleave(gfp, order, nid);
1925cc9a6c87SMel Gorman 		if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1926cc9a6c87SMel Gorman 			goto retry_cpuset;
1927cc9a6c87SMel Gorman 
1928c0ff7453SMiao Xie 		return page;
19291da177e4SLinus Torvalds 	}
19302f5f9486SAndi Kleen 	zl = policy_zonelist(gfp, pol, node);
193152cd3b07SLee Schermerhorn 	if (unlikely(mpol_needs_cond_ref(pol))) {
1932480eccf9SLee Schermerhorn 		/*
193352cd3b07SLee Schermerhorn 		 * slow path: ref counted shared policy
1934480eccf9SLee Schermerhorn 		 */
19350bbbc0b3SAndrea Arcangeli 		struct page *page =  __alloc_pages_nodemask(gfp, order,
193652cd3b07SLee Schermerhorn 						zl, policy_nodemask(gfp, pol));
1937f0be3d32SLee Schermerhorn 		__mpol_put(pol);
1938cc9a6c87SMel Gorman 		if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1939cc9a6c87SMel Gorman 			goto retry_cpuset;
1940480eccf9SLee Schermerhorn 		return page;
1941480eccf9SLee Schermerhorn 	}
1942480eccf9SLee Schermerhorn 	/*
1943480eccf9SLee Schermerhorn 	 * fast path:  default or task policy
1944480eccf9SLee Schermerhorn 	 */
19450bbbc0b3SAndrea Arcangeli 	page = __alloc_pages_nodemask(gfp, order, zl,
19460bbbc0b3SAndrea Arcangeli 				      policy_nodemask(gfp, pol));
1947cc9a6c87SMel Gorman 	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1948cc9a6c87SMel Gorman 		goto retry_cpuset;
1949c0ff7453SMiao Xie 	return page;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds /**
19531da177e4SLinus Torvalds  * 	alloc_pages_current - Allocate pages.
19541da177e4SLinus Torvalds  *
19551da177e4SLinus Torvalds  *	@gfp:
19561da177e4SLinus Torvalds  *		%GFP_USER   user allocation,
19571da177e4SLinus Torvalds  *      	%GFP_KERNEL kernel allocation,
19581da177e4SLinus Torvalds  *      	%GFP_HIGHMEM highmem allocation,
19591da177e4SLinus Torvalds  *      	%GFP_FS     don't call back into a file system.
19601da177e4SLinus Torvalds  *      	%GFP_ATOMIC don't sleep.
19611da177e4SLinus Torvalds  *	@order: Power of two of allocation size in pages. 0 is a single page.
19621da177e4SLinus Torvalds  *
19631da177e4SLinus Torvalds  *	Allocate a page from the kernel page pool.  When not in
19641da177e4SLinus Torvalds  *	interrupt context and apply the current process NUMA policy.
19651da177e4SLinus Torvalds  *	Returns NULL when no page can be allocated.
19661da177e4SLinus Torvalds  *
1967cf2a473cSPaul Jackson  *	Don't call cpuset_update_task_memory_state() unless
19681da177e4SLinus Torvalds  *	1) it's ok to take cpuset_sem (can WAIT), and
19691da177e4SLinus Torvalds  *	2) allocating for current task (not interrupt).
19701da177e4SLinus Torvalds  */
1971dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order)
19721da177e4SLinus Torvalds {
19731da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
1974c0ff7453SMiao Xie 	struct page *page;
1975cc9a6c87SMel Gorman 	unsigned int cpuset_mems_cookie;
19761da177e4SLinus Torvalds 
19779b819d20SChristoph Lameter 	if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
19781da177e4SLinus Torvalds 		pol = &default_policy;
197952cd3b07SLee Schermerhorn 
1980cc9a6c87SMel Gorman retry_cpuset:
1981cc9a6c87SMel Gorman 	cpuset_mems_cookie = get_mems_allowed();
1982cc9a6c87SMel Gorman 
198352cd3b07SLee Schermerhorn 	/*
198452cd3b07SLee Schermerhorn 	 * No reference counting needed for current->mempolicy
198552cd3b07SLee Schermerhorn 	 * nor system default_policy
198652cd3b07SLee Schermerhorn 	 */
198745c4745aSLee Schermerhorn 	if (pol->mode == MPOL_INTERLEAVE)
1988c0ff7453SMiao Xie 		page = alloc_page_interleave(gfp, order, interleave_nodes(pol));
1989c0ff7453SMiao Xie 	else
1990c0ff7453SMiao Xie 		page = __alloc_pages_nodemask(gfp, order,
19915c4b4be3SAndi Kleen 				policy_zonelist(gfp, pol, numa_node_id()),
19925c4b4be3SAndi Kleen 				policy_nodemask(gfp, pol));
1993cc9a6c87SMel Gorman 
1994cc9a6c87SMel Gorman 	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
1995cc9a6c87SMel Gorman 		goto retry_cpuset;
1996cc9a6c87SMel Gorman 
1997c0ff7453SMiao Xie 	return page;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current);
20001da177e4SLinus Torvalds 
20014225399aSPaul Jackson /*
2002846a16bfSLee Schermerhorn  * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
20034225399aSPaul Jackson  * rebinds the mempolicy its copying by calling mpol_rebind_policy()
20044225399aSPaul Jackson  * with the mems_allowed returned by cpuset_mems_allowed().  This
20054225399aSPaul Jackson  * keeps mempolicies cpuset relative after its cpuset moves.  See
20064225399aSPaul Jackson  * further kernel/cpuset.c update_nodemask().
2007708c1bbcSMiao Xie  *
2008708c1bbcSMiao Xie  * current's mempolicy may be rebinded by the other task(the task that changes
2009708c1bbcSMiao Xie  * cpuset's mems), so we needn't do rebind work for current task.
20104225399aSPaul Jackson  */
20114225399aSPaul Jackson 
2012846a16bfSLee Schermerhorn /* Slow path of a mempolicy duplicate */
2013846a16bfSLee Schermerhorn struct mempolicy *__mpol_dup(struct mempolicy *old)
20141da177e4SLinus Torvalds {
20151da177e4SLinus Torvalds 	struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	if (!new)
20181da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2019708c1bbcSMiao Xie 
2020708c1bbcSMiao Xie 	/* task's mempolicy is protected by alloc_lock */
2021708c1bbcSMiao Xie 	if (old == current->mempolicy) {
2022708c1bbcSMiao Xie 		task_lock(current);
2023708c1bbcSMiao Xie 		*new = *old;
2024708c1bbcSMiao Xie 		task_unlock(current);
2025708c1bbcSMiao Xie 	} else
2026708c1bbcSMiao Xie 		*new = *old;
2027708c1bbcSMiao Xie 
202899ee4ca7SPaul E. McKenney 	rcu_read_lock();
20294225399aSPaul Jackson 	if (current_cpuset_is_being_rebound()) {
20304225399aSPaul Jackson 		nodemask_t mems = cpuset_mems_allowed(current);
2031708c1bbcSMiao Xie 		if (new->flags & MPOL_F_REBINDING)
2032708c1bbcSMiao Xie 			mpol_rebind_policy(new, &mems, MPOL_REBIND_STEP2);
2033708c1bbcSMiao Xie 		else
2034708c1bbcSMiao Xie 			mpol_rebind_policy(new, &mems, MPOL_REBIND_ONCE);
20354225399aSPaul Jackson 	}
203699ee4ca7SPaul E. McKenney 	rcu_read_unlock();
20371da177e4SLinus Torvalds 	atomic_set(&new->refcnt, 1);
20381da177e4SLinus Torvalds 	return new;
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
204152cd3b07SLee Schermerhorn /*
204252cd3b07SLee Schermerhorn  * If *frompol needs [has] an extra ref, copy *frompol to *tompol ,
204352cd3b07SLee Schermerhorn  * eliminate the * MPOL_F_* flags that require conditional ref and
204452cd3b07SLee Schermerhorn  * [NOTE!!!] drop the extra ref.  Not safe to reference *frompol directly
204552cd3b07SLee Schermerhorn  * after return.  Use the returned value.
204652cd3b07SLee Schermerhorn  *
204752cd3b07SLee Schermerhorn  * Allows use of a mempolicy for, e.g., multiple allocations with a single
204852cd3b07SLee Schermerhorn  * policy lookup, even if the policy needs/has extra ref on lookup.
204952cd3b07SLee Schermerhorn  * shmem_readahead needs this.
205052cd3b07SLee Schermerhorn  */
205152cd3b07SLee Schermerhorn struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
205252cd3b07SLee Schermerhorn 						struct mempolicy *frompol)
205352cd3b07SLee Schermerhorn {
205452cd3b07SLee Schermerhorn 	if (!mpol_needs_cond_ref(frompol))
205552cd3b07SLee Schermerhorn 		return frompol;
205652cd3b07SLee Schermerhorn 
205752cd3b07SLee Schermerhorn 	*tompol = *frompol;
205852cd3b07SLee Schermerhorn 	tompol->flags &= ~MPOL_F_SHARED;	/* copy doesn't need unref */
205952cd3b07SLee Schermerhorn 	__mpol_put(frompol);
206052cd3b07SLee Schermerhorn 	return tompol;
206152cd3b07SLee Schermerhorn }
206252cd3b07SLee Schermerhorn 
20631da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */
2064fcfb4dccSKOSAKI Motohiro bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
20651da177e4SLinus Torvalds {
20661da177e4SLinus Torvalds 	if (!a || !b)
2067fcfb4dccSKOSAKI Motohiro 		return false;
206845c4745aSLee Schermerhorn 	if (a->mode != b->mode)
2069fcfb4dccSKOSAKI Motohiro 		return false;
207019800502SBob Liu 	if (a->flags != b->flags)
2071fcfb4dccSKOSAKI Motohiro 		return false;
207219800502SBob Liu 	if (mpol_store_user_nodemask(a))
207319800502SBob Liu 		if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
2074fcfb4dccSKOSAKI Motohiro 			return false;
207519800502SBob Liu 
207645c4745aSLee Schermerhorn 	switch (a->mode) {
207719770b32SMel Gorman 	case MPOL_BIND:
207819770b32SMel Gorman 		/* Fall through */
20791da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
2080fcfb4dccSKOSAKI Motohiro 		return !!nodes_equal(a->v.nodes, b->v.nodes);
20811da177e4SLinus Torvalds 	case MPOL_PREFERRED:
208275719661SNamhyung Kim 		return a->v.preferred_node == b->v.preferred_node;
20831da177e4SLinus Torvalds 	default:
20841da177e4SLinus Torvalds 		BUG();
2085fcfb4dccSKOSAKI Motohiro 		return false;
20861da177e4SLinus Torvalds 	}
20871da177e4SLinus Torvalds }
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds /*
20901da177e4SLinus Torvalds  * Shared memory backing store policy support.
20911da177e4SLinus Torvalds  *
20921da177e4SLinus Torvalds  * Remember policies even when nobody has shared memory mapped.
20931da177e4SLinus Torvalds  * The policies are kept in Red-Black tree linked from the inode.
20941da177e4SLinus Torvalds  * They are protected by the sp->lock spinlock, which should be held
20951da177e4SLinus Torvalds  * for any accesses to the tree.
20961da177e4SLinus Torvalds  */
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds /* lookup first element intersecting start-end */
2099b22d127aSMel Gorman /* Caller holds sp->mutex */
21001da177e4SLinus Torvalds static struct sp_node *
21011da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	struct rb_node *n = sp->root.rb_node;
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	while (n) {
21061da177e4SLinus Torvalds 		struct sp_node *p = rb_entry(n, struct sp_node, nd);
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 		if (start >= p->end)
21091da177e4SLinus Torvalds 			n = n->rb_right;
21101da177e4SLinus Torvalds 		else if (end <= p->start)
21111da177e4SLinus Torvalds 			n = n->rb_left;
21121da177e4SLinus Torvalds 		else
21131da177e4SLinus Torvalds 			break;
21141da177e4SLinus Torvalds 	}
21151da177e4SLinus Torvalds 	if (!n)
21161da177e4SLinus Torvalds 		return NULL;
21171da177e4SLinus Torvalds 	for (;;) {
21181da177e4SLinus Torvalds 		struct sp_node *w = NULL;
21191da177e4SLinus Torvalds 		struct rb_node *prev = rb_prev(n);
21201da177e4SLinus Torvalds 		if (!prev)
21211da177e4SLinus Torvalds 			break;
21221da177e4SLinus Torvalds 		w = rb_entry(prev, struct sp_node, nd);
21231da177e4SLinus Torvalds 		if (w->end <= start)
21241da177e4SLinus Torvalds 			break;
21251da177e4SLinus Torvalds 		n = prev;
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 	return rb_entry(n, struct sp_node, nd);
21281da177e4SLinus Torvalds }
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds /* Insert a new shared policy into the list. */
21311da177e4SLinus Torvalds /* Caller holds sp->lock */
21321da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new)
21331da177e4SLinus Torvalds {
21341da177e4SLinus Torvalds 	struct rb_node **p = &sp->root.rb_node;
21351da177e4SLinus Torvalds 	struct rb_node *parent = NULL;
21361da177e4SLinus Torvalds 	struct sp_node *nd;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	while (*p) {
21391da177e4SLinus Torvalds 		parent = *p;
21401da177e4SLinus Torvalds 		nd = rb_entry(parent, struct sp_node, nd);
21411da177e4SLinus Torvalds 		if (new->start < nd->start)
21421da177e4SLinus Torvalds 			p = &(*p)->rb_left;
21431da177e4SLinus Torvalds 		else if (new->end > nd->end)
21441da177e4SLinus Torvalds 			p = &(*p)->rb_right;
21451da177e4SLinus Torvalds 		else
21461da177e4SLinus Torvalds 			BUG();
21471da177e4SLinus Torvalds 	}
21481da177e4SLinus Torvalds 	rb_link_node(&new->nd, parent, p);
21491da177e4SLinus Torvalds 	rb_insert_color(&new->nd, &sp->root);
2150140d5a49SPaul Mundt 	pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
215145c4745aSLee Schermerhorn 		 new->policy ? new->policy->mode : 0);
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds /* Find shared policy intersecting idx */
21551da177e4SLinus Torvalds struct mempolicy *
21561da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
21571da177e4SLinus Torvalds {
21581da177e4SLinus Torvalds 	struct mempolicy *pol = NULL;
21591da177e4SLinus Torvalds 	struct sp_node *sn;
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	if (!sp->root.rb_node)
21621da177e4SLinus Torvalds 		return NULL;
2163b22d127aSMel Gorman 	mutex_lock(&sp->mutex);
21641da177e4SLinus Torvalds 	sn = sp_lookup(sp, idx, idx+1);
21651da177e4SLinus Torvalds 	if (sn) {
21661da177e4SLinus Torvalds 		mpol_get(sn->policy);
21671da177e4SLinus Torvalds 		pol = sn->policy;
21681da177e4SLinus Torvalds 	}
2169b22d127aSMel Gorman 	mutex_unlock(&sp->mutex);
21701da177e4SLinus Torvalds 	return pol;
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
217363f74ca2SKOSAKI Motohiro static void sp_free(struct sp_node *n)
217463f74ca2SKOSAKI Motohiro {
217563f74ca2SKOSAKI Motohiro 	mpol_put(n->policy);
217663f74ca2SKOSAKI Motohiro 	kmem_cache_free(sn_cache, n);
217763f74ca2SKOSAKI Motohiro }
217863f74ca2SKOSAKI Motohiro 
21791da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n)
21801da177e4SLinus Torvalds {
2181140d5a49SPaul Mundt 	pr_debug("deleting %lx-l%lx\n", n->start, n->end);
21821da177e4SLinus Torvalds 	rb_erase(&n->nd, &sp->root);
218363f74ca2SKOSAKI Motohiro 	sp_free(n);
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
2186dbcb0f19SAdrian Bunk static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
2187dbcb0f19SAdrian Bunk 				struct mempolicy *pol)
21881da177e4SLinus Torvalds {
2189869833f2SKOSAKI Motohiro 	struct sp_node *n;
2190869833f2SKOSAKI Motohiro 	struct mempolicy *newpol;
21911da177e4SLinus Torvalds 
2192869833f2SKOSAKI Motohiro 	n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
21931da177e4SLinus Torvalds 	if (!n)
21941da177e4SLinus Torvalds 		return NULL;
2195869833f2SKOSAKI Motohiro 
2196869833f2SKOSAKI Motohiro 	newpol = mpol_dup(pol);
2197869833f2SKOSAKI Motohiro 	if (IS_ERR(newpol)) {
2198869833f2SKOSAKI Motohiro 		kmem_cache_free(sn_cache, n);
2199869833f2SKOSAKI Motohiro 		return NULL;
2200869833f2SKOSAKI Motohiro 	}
2201869833f2SKOSAKI Motohiro 	newpol->flags |= MPOL_F_SHARED;
2202869833f2SKOSAKI Motohiro 
22031da177e4SLinus Torvalds 	n->start = start;
22041da177e4SLinus Torvalds 	n->end = end;
2205869833f2SKOSAKI Motohiro 	n->policy = newpol;
2206869833f2SKOSAKI Motohiro 
22071da177e4SLinus Torvalds 	return n;
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds /* Replace a policy range. */
22111da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
22121da177e4SLinus Torvalds 				 unsigned long end, struct sp_node *new)
22131da177e4SLinus Torvalds {
2214b22d127aSMel Gorman 	struct sp_node *n;
2215b22d127aSMel Gorman 	int ret = 0;
22161da177e4SLinus Torvalds 
2217b22d127aSMel Gorman 	mutex_lock(&sp->mutex);
22181da177e4SLinus Torvalds 	n = sp_lookup(sp, start, end);
22191da177e4SLinus Torvalds 	/* Take care of old policies in the same range. */
22201da177e4SLinus Torvalds 	while (n && n->start < end) {
22211da177e4SLinus Torvalds 		struct rb_node *next = rb_next(&n->nd);
22221da177e4SLinus Torvalds 		if (n->start >= start) {
22231da177e4SLinus Torvalds 			if (n->end <= end)
22241da177e4SLinus Torvalds 				sp_delete(sp, n);
22251da177e4SLinus Torvalds 			else
22261da177e4SLinus Torvalds 				n->start = end;
22271da177e4SLinus Torvalds 		} else {
22281da177e4SLinus Torvalds 			/* Old policy spanning whole new range. */
22291da177e4SLinus Torvalds 			if (n->end > end) {
2230b22d127aSMel Gorman 				struct sp_node *new2;
22311da177e4SLinus Torvalds 				new2 = sp_alloc(end, n->end, n->policy);
2232b22d127aSMel Gorman 				if (!new2) {
2233b22d127aSMel Gorman 					ret = -ENOMEM;
2234b22d127aSMel Gorman 					goto out;
22351da177e4SLinus Torvalds 				}
22361da177e4SLinus Torvalds 				n->end = start;
22371da177e4SLinus Torvalds 				sp_insert(sp, new2);
22381da177e4SLinus Torvalds 				break;
22391da177e4SLinus Torvalds 			} else
22401da177e4SLinus Torvalds 				n->end = start;
22411da177e4SLinus Torvalds 		}
22421da177e4SLinus Torvalds 		if (!next)
22431da177e4SLinus Torvalds 			break;
22441da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
22451da177e4SLinus Torvalds 	}
22461da177e4SLinus Torvalds 	if (new)
22471da177e4SLinus Torvalds 		sp_insert(sp, new);
2248b22d127aSMel Gorman out:
2249b22d127aSMel Gorman 	mutex_unlock(&sp->mutex);
2250b22d127aSMel Gorman 	return ret;
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds 
225371fe804bSLee Schermerhorn /**
225471fe804bSLee Schermerhorn  * mpol_shared_policy_init - initialize shared policy for inode
225571fe804bSLee Schermerhorn  * @sp: pointer to inode shared policy
225671fe804bSLee Schermerhorn  * @mpol:  struct mempolicy to install
225771fe804bSLee Schermerhorn  *
225871fe804bSLee Schermerhorn  * Install non-NULL @mpol in inode's shared policy rb-tree.
225971fe804bSLee Schermerhorn  * On entry, the current task has a reference on a non-NULL @mpol.
226071fe804bSLee Schermerhorn  * This must be released on exit.
22614bfc4495SKAMEZAWA Hiroyuki  * This is called at get_inode() calls and we can use GFP_KERNEL.
226271fe804bSLee Schermerhorn  */
226371fe804bSLee Schermerhorn void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
22647339ff83SRobin Holt {
226558568d2aSMiao Xie 	int ret;
226658568d2aSMiao Xie 
226771fe804bSLee Schermerhorn 	sp->root = RB_ROOT;		/* empty tree == default mempolicy */
2268b22d127aSMel Gorman 	mutex_init(&sp->mutex);
22697339ff83SRobin Holt 
227071fe804bSLee Schermerhorn 	if (mpol) {
22717339ff83SRobin Holt 		struct vm_area_struct pvma;
227271fe804bSLee Schermerhorn 		struct mempolicy *new;
22734bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
22747339ff83SRobin Holt 
22754bfc4495SKAMEZAWA Hiroyuki 		if (!scratch)
22765c0c1654SLee Schermerhorn 			goto put_mpol;
227771fe804bSLee Schermerhorn 		/* contextualize the tmpfs mount point mempolicy */
227871fe804bSLee Schermerhorn 		new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
227915d77835SLee Schermerhorn 		if (IS_ERR(new))
22800cae3457SDan Carpenter 			goto free_scratch; /* no valid nodemask intersection */
228158568d2aSMiao Xie 
228258568d2aSMiao Xie 		task_lock(current);
22834bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch);
228458568d2aSMiao Xie 		task_unlock(current);
228515d77835SLee Schermerhorn 		if (ret)
22865c0c1654SLee Schermerhorn 			goto put_new;
228771fe804bSLee Schermerhorn 
228871fe804bSLee Schermerhorn 		/* Create pseudo-vma that contains just the policy */
22897339ff83SRobin Holt 		memset(&pvma, 0, sizeof(struct vm_area_struct));
229071fe804bSLee Schermerhorn 		pvma.vm_end = TASK_SIZE;	/* policy covers entire file */
229171fe804bSLee Schermerhorn 		mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
229215d77835SLee Schermerhorn 
22935c0c1654SLee Schermerhorn put_new:
229471fe804bSLee Schermerhorn 		mpol_put(new);			/* drop initial ref */
22950cae3457SDan Carpenter free_scratch:
22964bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
22975c0c1654SLee Schermerhorn put_mpol:
22985c0c1654SLee Schermerhorn 		mpol_put(mpol);	/* drop our incoming ref on sb mpol */
22997339ff83SRobin Holt 	}
23007339ff83SRobin Holt }
23017339ff83SRobin Holt 
23021da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info,
23031da177e4SLinus Torvalds 			struct vm_area_struct *vma, struct mempolicy *npol)
23041da177e4SLinus Torvalds {
23051da177e4SLinus Torvalds 	int err;
23061da177e4SLinus Torvalds 	struct sp_node *new = NULL;
23071da177e4SLinus Torvalds 	unsigned long sz = vma_pages(vma);
23081da177e4SLinus Torvalds 
2309028fec41SDavid Rientjes 	pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
23101da177e4SLinus Torvalds 		 vma->vm_pgoff,
231145c4745aSLee Schermerhorn 		 sz, npol ? npol->mode : -1,
2312028fec41SDavid Rientjes 		 npol ? npol->flags : -1,
2313dfcd3c0dSAndi Kleen 		 npol ? nodes_addr(npol->v.nodes)[0] : -1);
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds 	if (npol) {
23161da177e4SLinus Torvalds 		new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
23171da177e4SLinus Torvalds 		if (!new)
23181da177e4SLinus Torvalds 			return -ENOMEM;
23191da177e4SLinus Torvalds 	}
23201da177e4SLinus Torvalds 	err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
23211da177e4SLinus Torvalds 	if (err && new)
232263f74ca2SKOSAKI Motohiro 		sp_free(new);
23231da177e4SLinus Torvalds 	return err;
23241da177e4SLinus Torvalds }
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */
23271da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p)
23281da177e4SLinus Torvalds {
23291da177e4SLinus Torvalds 	struct sp_node *n;
23301da177e4SLinus Torvalds 	struct rb_node *next;
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	if (!p->root.rb_node)
23331da177e4SLinus Torvalds 		return;
2334b22d127aSMel Gorman 	mutex_lock(&p->mutex);
23351da177e4SLinus Torvalds 	next = rb_first(&p->root);
23361da177e4SLinus Torvalds 	while (next) {
23371da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
23381da177e4SLinus Torvalds 		next = rb_next(&n->nd);
233963f74ca2SKOSAKI Motohiro 		sp_delete(p, n);
23401da177e4SLinus Torvalds 	}
2341b22d127aSMel Gorman 	mutex_unlock(&p->mutex);
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */
23451da177e4SLinus Torvalds void __init numa_policy_init(void)
23461da177e4SLinus Torvalds {
2347b71636e2SPaul Mundt 	nodemask_t interleave_nodes;
2348b71636e2SPaul Mundt 	unsigned long largest = 0;
2349b71636e2SPaul Mundt 	int nid, prefer = 0;
2350b71636e2SPaul Mundt 
23511da177e4SLinus Torvalds 	policy_cache = kmem_cache_create("numa_policy",
23521da177e4SLinus Torvalds 					 sizeof(struct mempolicy),
235320c2df83SPaul Mundt 					 0, SLAB_PANIC, NULL);
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	sn_cache = kmem_cache_create("shared_policy_node",
23561da177e4SLinus Torvalds 				     sizeof(struct sp_node),
235720c2df83SPaul Mundt 				     0, SLAB_PANIC, NULL);
23581da177e4SLinus Torvalds 
2359b71636e2SPaul Mundt 	/*
2360b71636e2SPaul Mundt 	 * Set interleaving policy for system init. Interleaving is only
2361b71636e2SPaul Mundt 	 * enabled across suitably sized nodes (default is >= 16MB), or
2362b71636e2SPaul Mundt 	 * fall back to the largest node if they're all smaller.
2363b71636e2SPaul Mundt 	 */
2364b71636e2SPaul Mundt 	nodes_clear(interleave_nodes);
236556bbd65dSChristoph Lameter 	for_each_node_state(nid, N_HIGH_MEMORY) {
2366b71636e2SPaul Mundt 		unsigned long total_pages = node_present_pages(nid);
23671da177e4SLinus Torvalds 
2368b71636e2SPaul Mundt 		/* Preserve the largest node */
2369b71636e2SPaul Mundt 		if (largest < total_pages) {
2370b71636e2SPaul Mundt 			largest = total_pages;
2371b71636e2SPaul Mundt 			prefer = nid;
2372b71636e2SPaul Mundt 		}
2373b71636e2SPaul Mundt 
2374b71636e2SPaul Mundt 		/* Interleave this node? */
2375b71636e2SPaul Mundt 		if ((total_pages << PAGE_SHIFT) >= (16 << 20))
2376b71636e2SPaul Mundt 			node_set(nid, interleave_nodes);
2377b71636e2SPaul Mundt 	}
2378b71636e2SPaul Mundt 
2379b71636e2SPaul Mundt 	/* All too small, use the largest */
2380b71636e2SPaul Mundt 	if (unlikely(nodes_empty(interleave_nodes)))
2381b71636e2SPaul Mundt 		node_set(prefer, interleave_nodes);
2382b71636e2SPaul Mundt 
2383028fec41SDavid Rientjes 	if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
23841da177e4SLinus Torvalds 		printk("numa_policy_init: interleaving failed\n");
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds 
23878bccd85fSChristoph Lameter /* Reset policy of current process to default */
23881da177e4SLinus Torvalds void numa_default_policy(void)
23891da177e4SLinus Torvalds {
2390028fec41SDavid Rientjes 	do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
23911da177e4SLinus Torvalds }
239268860ec1SPaul Jackson 
23934225399aSPaul Jackson /*
2394095f1fc4SLee Schermerhorn  * Parse and format mempolicy from/to strings
2395095f1fc4SLee Schermerhorn  */
2396095f1fc4SLee Schermerhorn 
2397095f1fc4SLee Schermerhorn /*
2398fc36b8d3SLee Schermerhorn  * "local" is pseudo-policy:  MPOL_PREFERRED with MPOL_F_LOCAL flag
23993f226aa1SLee Schermerhorn  * Used only for mpol_parse_str() and mpol_to_str()
24001a75a6c8SChristoph Lameter  */
2401345ace9cSLee Schermerhorn #define MPOL_LOCAL MPOL_MAX
2402345ace9cSLee Schermerhorn static const char * const policy_modes[] =
2403345ace9cSLee Schermerhorn {
2404345ace9cSLee Schermerhorn 	[MPOL_DEFAULT]    = "default",
2405345ace9cSLee Schermerhorn 	[MPOL_PREFERRED]  = "prefer",
2406345ace9cSLee Schermerhorn 	[MPOL_BIND]       = "bind",
2407345ace9cSLee Schermerhorn 	[MPOL_INTERLEAVE] = "interleave",
2408345ace9cSLee Schermerhorn 	[MPOL_LOCAL]      = "local"
2409345ace9cSLee Schermerhorn };
24101a75a6c8SChristoph Lameter 
2411095f1fc4SLee Schermerhorn 
2412095f1fc4SLee Schermerhorn #ifdef CONFIG_TMPFS
2413095f1fc4SLee Schermerhorn /**
2414095f1fc4SLee Schermerhorn  * mpol_parse_str - parse string to mempolicy
2415095f1fc4SLee Schermerhorn  * @str:  string containing mempolicy to parse
241671fe804bSLee Schermerhorn  * @mpol:  pointer to struct mempolicy pointer, returned on success.
241771fe804bSLee Schermerhorn  * @no_context:  flag whether to "contextualize" the mempolicy
2418095f1fc4SLee Schermerhorn  *
2419095f1fc4SLee Schermerhorn  * Format of input:
2420095f1fc4SLee Schermerhorn  *	<mode>[=<flags>][:<nodelist>]
2421095f1fc4SLee Schermerhorn  *
242271fe804bSLee Schermerhorn  * if @no_context is true, save the input nodemask in w.user_nodemask in
242371fe804bSLee Schermerhorn  * the returned mempolicy.  This will be used to "clone" the mempolicy in
242471fe804bSLee Schermerhorn  * a specific context [cpuset] at a later time.  Used to parse tmpfs mpol
242571fe804bSLee Schermerhorn  * mount option.  Note that if 'static' or 'relative' mode flags were
242671fe804bSLee Schermerhorn  * specified, the input nodemask will already have been saved.  Saving
242771fe804bSLee Schermerhorn  * it again is redundant, but safe.
242871fe804bSLee Schermerhorn  *
242971fe804bSLee Schermerhorn  * On success, returns 0, else 1
2430095f1fc4SLee Schermerhorn  */
243171fe804bSLee Schermerhorn int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context)
2432095f1fc4SLee Schermerhorn {
243371fe804bSLee Schermerhorn 	struct mempolicy *new = NULL;
2434b4652e84SLee Schermerhorn 	unsigned short mode;
243571fe804bSLee Schermerhorn 	unsigned short uninitialized_var(mode_flags);
243671fe804bSLee Schermerhorn 	nodemask_t nodes;
2437095f1fc4SLee Schermerhorn 	char *nodelist = strchr(str, ':');
2438095f1fc4SLee Schermerhorn 	char *flags = strchr(str, '=');
2439095f1fc4SLee Schermerhorn 	int err = 1;
2440095f1fc4SLee Schermerhorn 
2441095f1fc4SLee Schermerhorn 	if (nodelist) {
2442095f1fc4SLee Schermerhorn 		/* NUL-terminate mode or flags string */
2443095f1fc4SLee Schermerhorn 		*nodelist++ = '\0';
244471fe804bSLee Schermerhorn 		if (nodelist_parse(nodelist, nodes))
2445095f1fc4SLee Schermerhorn 			goto out;
244671fe804bSLee Schermerhorn 		if (!nodes_subset(nodes, node_states[N_HIGH_MEMORY]))
2447095f1fc4SLee Schermerhorn 			goto out;
244871fe804bSLee Schermerhorn 	} else
244971fe804bSLee Schermerhorn 		nodes_clear(nodes);
245071fe804bSLee Schermerhorn 
2451095f1fc4SLee Schermerhorn 	if (flags)
2452095f1fc4SLee Schermerhorn 		*flags++ = '\0';	/* terminate mode string */
2453095f1fc4SLee Schermerhorn 
2454b4652e84SLee Schermerhorn 	for (mode = 0; mode <= MPOL_LOCAL; mode++) {
2455345ace9cSLee Schermerhorn 		if (!strcmp(str, policy_modes[mode])) {
2456095f1fc4SLee Schermerhorn 			break;
2457095f1fc4SLee Schermerhorn 		}
2458095f1fc4SLee Schermerhorn 	}
2459b4652e84SLee Schermerhorn 	if (mode > MPOL_LOCAL)
2460095f1fc4SLee Schermerhorn 		goto out;
2461095f1fc4SLee Schermerhorn 
246271fe804bSLee Schermerhorn 	switch (mode) {
2463095f1fc4SLee Schermerhorn 	case MPOL_PREFERRED:
246471fe804bSLee Schermerhorn 		/*
246571fe804bSLee Schermerhorn 		 * Insist on a nodelist of one node only
246671fe804bSLee Schermerhorn 		 */
2467095f1fc4SLee Schermerhorn 		if (nodelist) {
2468095f1fc4SLee Schermerhorn 			char *rest = nodelist;
2469095f1fc4SLee Schermerhorn 			while (isdigit(*rest))
2470095f1fc4SLee Schermerhorn 				rest++;
2471926f2ae0SKOSAKI Motohiro 			if (*rest)
2472926f2ae0SKOSAKI Motohiro 				goto out;
2473095f1fc4SLee Schermerhorn 		}
2474095f1fc4SLee Schermerhorn 		break;
2475095f1fc4SLee Schermerhorn 	case MPOL_INTERLEAVE:
2476095f1fc4SLee Schermerhorn 		/*
2477095f1fc4SLee Schermerhorn 		 * Default to online nodes with memory if no nodelist
2478095f1fc4SLee Schermerhorn 		 */
2479095f1fc4SLee Schermerhorn 		if (!nodelist)
248071fe804bSLee Schermerhorn 			nodes = node_states[N_HIGH_MEMORY];
24813f226aa1SLee Schermerhorn 		break;
248271fe804bSLee Schermerhorn 	case MPOL_LOCAL:
24833f226aa1SLee Schermerhorn 		/*
248471fe804bSLee Schermerhorn 		 * Don't allow a nodelist;  mpol_new() checks flags
24853f226aa1SLee Schermerhorn 		 */
248671fe804bSLee Schermerhorn 		if (nodelist)
24873f226aa1SLee Schermerhorn 			goto out;
248871fe804bSLee Schermerhorn 		mode = MPOL_PREFERRED;
24893f226aa1SLee Schermerhorn 		break;
2490413b43deSRavikiran G Thirumalai 	case MPOL_DEFAULT:
2491413b43deSRavikiran G Thirumalai 		/*
2492413b43deSRavikiran G Thirumalai 		 * Insist on a empty nodelist
2493413b43deSRavikiran G Thirumalai 		 */
2494413b43deSRavikiran G Thirumalai 		if (!nodelist)
2495413b43deSRavikiran G Thirumalai 			err = 0;
2496413b43deSRavikiran G Thirumalai 		goto out;
2497d69b2e63SKOSAKI Motohiro 	case MPOL_BIND:
249871fe804bSLee Schermerhorn 		/*
2499d69b2e63SKOSAKI Motohiro 		 * Insist on a nodelist
250071fe804bSLee Schermerhorn 		 */
2501d69b2e63SKOSAKI Motohiro 		if (!nodelist)
2502d69b2e63SKOSAKI Motohiro 			goto out;
2503095f1fc4SLee Schermerhorn 	}
2504095f1fc4SLee Schermerhorn 
250571fe804bSLee Schermerhorn 	mode_flags = 0;
2506095f1fc4SLee Schermerhorn 	if (flags) {
2507095f1fc4SLee Schermerhorn 		/*
2508095f1fc4SLee Schermerhorn 		 * Currently, we only support two mutually exclusive
2509095f1fc4SLee Schermerhorn 		 * mode flags.
2510095f1fc4SLee Schermerhorn 		 */
2511095f1fc4SLee Schermerhorn 		if (!strcmp(flags, "static"))
251271fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_STATIC_NODES;
2513095f1fc4SLee Schermerhorn 		else if (!strcmp(flags, "relative"))
251471fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_RELATIVE_NODES;
2515095f1fc4SLee Schermerhorn 		else
2516926f2ae0SKOSAKI Motohiro 			goto out;
2517095f1fc4SLee Schermerhorn 	}
251871fe804bSLee Schermerhorn 
251971fe804bSLee Schermerhorn 	new = mpol_new(mode, mode_flags, &nodes);
252071fe804bSLee Schermerhorn 	if (IS_ERR(new))
2521926f2ae0SKOSAKI Motohiro 		goto out;
2522926f2ae0SKOSAKI Motohiro 
2523e17f74afSLee Schermerhorn 	if (no_context) {
2524e17f74afSLee Schermerhorn 		/* save for contextualization */
2525e17f74afSLee Schermerhorn 		new->w.user_nodemask = nodes;
2526e17f74afSLee Schermerhorn 	} else {
252758568d2aSMiao Xie 		int ret;
25284bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
25294bfc4495SKAMEZAWA Hiroyuki 		if (scratch) {
253058568d2aSMiao Xie 			task_lock(current);
25314bfc4495SKAMEZAWA Hiroyuki 			ret = mpol_set_nodemask(new, &nodes, scratch);
253258568d2aSMiao Xie 			task_unlock(current);
25334bfc4495SKAMEZAWA Hiroyuki 		} else
25344bfc4495SKAMEZAWA Hiroyuki 			ret = -ENOMEM;
25354bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
25364bfc4495SKAMEZAWA Hiroyuki 		if (ret) {
25374bfc4495SKAMEZAWA Hiroyuki 			mpol_put(new);
2538926f2ae0SKOSAKI Motohiro 			goto out;
2539926f2ae0SKOSAKI Motohiro 		}
2540926f2ae0SKOSAKI Motohiro 	}
2541926f2ae0SKOSAKI Motohiro 	err = 0;
254271fe804bSLee Schermerhorn 
2543095f1fc4SLee Schermerhorn out:
2544095f1fc4SLee Schermerhorn 	/* Restore string for error message */
2545095f1fc4SLee Schermerhorn 	if (nodelist)
2546095f1fc4SLee Schermerhorn 		*--nodelist = ':';
2547095f1fc4SLee Schermerhorn 	if (flags)
2548095f1fc4SLee Schermerhorn 		*--flags = '=';
254971fe804bSLee Schermerhorn 	if (!err)
255071fe804bSLee Schermerhorn 		*mpol = new;
2551095f1fc4SLee Schermerhorn 	return err;
2552095f1fc4SLee Schermerhorn }
2553095f1fc4SLee Schermerhorn #endif /* CONFIG_TMPFS */
2554095f1fc4SLee Schermerhorn 
255571fe804bSLee Schermerhorn /**
255671fe804bSLee Schermerhorn  * mpol_to_str - format a mempolicy structure for printing
255771fe804bSLee Schermerhorn  * @buffer:  to contain formatted mempolicy string
255871fe804bSLee Schermerhorn  * @maxlen:  length of @buffer
255971fe804bSLee Schermerhorn  * @pol:  pointer to mempolicy to be formatted
256071fe804bSLee Schermerhorn  * @no_context:  "context free" mempolicy - use nodemask in w.user_nodemask
256171fe804bSLee Schermerhorn  *
25621a75a6c8SChristoph Lameter  * Convert a mempolicy into a string.
25631a75a6c8SChristoph Lameter  * Returns the number of characters in buffer (if positive)
25641a75a6c8SChristoph Lameter  * or an error (negative)
25651a75a6c8SChristoph Lameter  */
256671fe804bSLee Schermerhorn int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol, int no_context)
25671a75a6c8SChristoph Lameter {
25681a75a6c8SChristoph Lameter 	char *p = buffer;
25691a75a6c8SChristoph Lameter 	int l;
25701a75a6c8SChristoph Lameter 	nodemask_t nodes;
2571bea904d5SLee Schermerhorn 	unsigned short mode;
2572f5b087b5SDavid Rientjes 	unsigned short flags = pol ? pol->flags : 0;
25731a75a6c8SChristoph Lameter 
25742291990aSLee Schermerhorn 	/*
25752291990aSLee Schermerhorn 	 * Sanity check:  room for longest mode, flag and some nodes
25762291990aSLee Schermerhorn 	 */
25772291990aSLee Schermerhorn 	VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16);
25782291990aSLee Schermerhorn 
2579bea904d5SLee Schermerhorn 	if (!pol || pol == &default_policy)
2580bea904d5SLee Schermerhorn 		mode = MPOL_DEFAULT;
2581bea904d5SLee Schermerhorn 	else
2582bea904d5SLee Schermerhorn 		mode = pol->mode;
2583bea904d5SLee Schermerhorn 
25841a75a6c8SChristoph Lameter 	switch (mode) {
25851a75a6c8SChristoph Lameter 	case MPOL_DEFAULT:
25861a75a6c8SChristoph Lameter 		nodes_clear(nodes);
25871a75a6c8SChristoph Lameter 		break;
25881a75a6c8SChristoph Lameter 
25891a75a6c8SChristoph Lameter 	case MPOL_PREFERRED:
25901a75a6c8SChristoph Lameter 		nodes_clear(nodes);
2591fc36b8d3SLee Schermerhorn 		if (flags & MPOL_F_LOCAL)
259253f2556bSLee Schermerhorn 			mode = MPOL_LOCAL;	/* pseudo-policy */
259353f2556bSLee Schermerhorn 		else
2594fc36b8d3SLee Schermerhorn 			node_set(pol->v.preferred_node, nodes);
25951a75a6c8SChristoph Lameter 		break;
25961a75a6c8SChristoph Lameter 
25971a75a6c8SChristoph Lameter 	case MPOL_BIND:
259819770b32SMel Gorman 		/* Fall through */
25991a75a6c8SChristoph Lameter 	case MPOL_INTERLEAVE:
260071fe804bSLee Schermerhorn 		if (no_context)
260171fe804bSLee Schermerhorn 			nodes = pol->w.user_nodemask;
260271fe804bSLee Schermerhorn 		else
26031a75a6c8SChristoph Lameter 			nodes = pol->v.nodes;
26041a75a6c8SChristoph Lameter 		break;
26051a75a6c8SChristoph Lameter 
26061a75a6c8SChristoph Lameter 	default:
260780de7c31SDave Jones 		return -EINVAL;
26081a75a6c8SChristoph Lameter 	}
26091a75a6c8SChristoph Lameter 
2610345ace9cSLee Schermerhorn 	l = strlen(policy_modes[mode]);
26111a75a6c8SChristoph Lameter 	if (buffer + maxlen < p + l + 1)
26121a75a6c8SChristoph Lameter 		return -ENOSPC;
26131a75a6c8SChristoph Lameter 
2614345ace9cSLee Schermerhorn 	strcpy(p, policy_modes[mode]);
26151a75a6c8SChristoph Lameter 	p += l;
26161a75a6c8SChristoph Lameter 
2617fc36b8d3SLee Schermerhorn 	if (flags & MPOL_MODE_FLAGS) {
2618f5b087b5SDavid Rientjes 		if (buffer + maxlen < p + 2)
2619f5b087b5SDavid Rientjes 			return -ENOSPC;
2620f5b087b5SDavid Rientjes 		*p++ = '=';
2621f5b087b5SDavid Rientjes 
26222291990aSLee Schermerhorn 		/*
26232291990aSLee Schermerhorn 		 * Currently, the only defined flags are mutually exclusive
26242291990aSLee Schermerhorn 		 */
2625f5b087b5SDavid Rientjes 		if (flags & MPOL_F_STATIC_NODES)
26262291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "static");
26272291990aSLee Schermerhorn 		else if (flags & MPOL_F_RELATIVE_NODES)
26282291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "relative");
2629f5b087b5SDavid Rientjes 	}
2630f5b087b5SDavid Rientjes 
26311a75a6c8SChristoph Lameter 	if (!nodes_empty(nodes)) {
26321a75a6c8SChristoph Lameter 		if (buffer + maxlen < p + 2)
26331a75a6c8SChristoph Lameter 			return -ENOSPC;
2634095f1fc4SLee Schermerhorn 		*p++ = ':';
26351a75a6c8SChristoph Lameter 	 	p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
26361a75a6c8SChristoph Lameter 	}
26371a75a6c8SChristoph Lameter 	return p - buffer;
26381a75a6c8SChristoph Lameter }
2639