xref: /openbmc/linux/mm/mempolicy.c (revision 06808b0827e1cd14eedc96bac2655d5b37ac246c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Simple NUMA memory policy for the Linux kernel.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright 2003,2004 Andi Kleen, SuSE Labs.
58bccd85fSChristoph Lameter  * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
61da177e4SLinus Torvalds  * Subject to the GNU Public License, version 2.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * NUMA policy allows the user to give hints in which node(s) memory should
91da177e4SLinus Torvalds  * be allocated.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Support four policies per VMA and per process:
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * The VMA policy has priority over the process policy for a page fault.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * interleave     Allocate memory interleaved over a set of nodes,
161da177e4SLinus Torvalds  *                with normal fallback if it fails.
171da177e4SLinus Torvalds  *                For VMA based allocations this interleaves based on the
181da177e4SLinus Torvalds  *                offset into the backing object or offset into the mapping
191da177e4SLinus Torvalds  *                for anonymous memory. For process policy an process counter
201da177e4SLinus Torvalds  *                is used.
218bccd85fSChristoph Lameter  *
221da177e4SLinus Torvalds  * bind           Only allocate memory on a specific set of nodes,
231da177e4SLinus Torvalds  *                no fallback.
248bccd85fSChristoph Lameter  *                FIXME: memory is allocated starting with the first node
258bccd85fSChristoph Lameter  *                to the last. It would be better if bind would truly restrict
268bccd85fSChristoph Lameter  *                the allocation to memory nodes instead
278bccd85fSChristoph Lameter  *
281da177e4SLinus Torvalds  * preferred       Try a specific node first before normal fallback.
291da177e4SLinus Torvalds  *                As a special case node -1 here means do the allocation
301da177e4SLinus Torvalds  *                on the local CPU. This is normally identical to default,
311da177e4SLinus Torvalds  *                but useful to set in a VMA when you have a non default
321da177e4SLinus Torvalds  *                process policy.
338bccd85fSChristoph Lameter  *
341da177e4SLinus Torvalds  * default        Allocate on the local node first, or when on a VMA
351da177e4SLinus Torvalds  *                use the process policy. This is what Linux always did
361da177e4SLinus Torvalds  *		  in a NUMA aware kernel and still does by, ahem, default.
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * The process policy is applied for most non interrupt memory allocations
391da177e4SLinus Torvalds  * in that process' context. Interrupts ignore the policies and always
401da177e4SLinus Torvalds  * try to allocate on the local CPU. The VMA policy is only applied for memory
411da177e4SLinus Torvalds  * allocations for a VMA in the VM.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * Currently there are a few corner cases in swapping where the policy
441da177e4SLinus Torvalds  * is not applied, but the majority should be handled. When process policy
451da177e4SLinus Torvalds  * is used it is not remembered over swap outs/swap ins.
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Only the highest zone in the zone hierarchy gets policied. Allocations
481da177e4SLinus Torvalds  * requesting a lower zone just use default policy. This implies that
491da177e4SLinus Torvalds  * on systems with highmem kernel lowmem allocation don't get policied.
501da177e4SLinus Torvalds  * Same with GFP_DMA allocations.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
531da177e4SLinus Torvalds  * all users and remembered even when nobody has memory mapped.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* Notebook:
571da177e4SLinus Torvalds    fix mmap readahead to honour policy and enable policy for any page cache
581da177e4SLinus Torvalds    object
591da177e4SLinus Torvalds    statistics for bigpages
601da177e4SLinus Torvalds    global policy for page cache? currently it uses process policy. Requires
611da177e4SLinus Torvalds    first item above.
621da177e4SLinus Torvalds    handle mremap for shared memory (currently ignored for the policy)
631da177e4SLinus Torvalds    grows down?
641da177e4SLinus Torvalds    make bind policy root only? It can trigger oom much faster and the
651da177e4SLinus Torvalds    kernel is not always grateful with that.
661da177e4SLinus Torvalds */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <linux/mempolicy.h>
691da177e4SLinus Torvalds #include <linux/mm.h>
701da177e4SLinus Torvalds #include <linux/highmem.h>
711da177e4SLinus Torvalds #include <linux/hugetlb.h>
721da177e4SLinus Torvalds #include <linux/kernel.h>
731da177e4SLinus Torvalds #include <linux/sched.h>
741da177e4SLinus Torvalds #include <linux/nodemask.h>
751da177e4SLinus Torvalds #include <linux/cpuset.h>
761da177e4SLinus Torvalds #include <linux/gfp.h>
771da177e4SLinus Torvalds #include <linux/slab.h>
781da177e4SLinus Torvalds #include <linux/string.h>
791da177e4SLinus Torvalds #include <linux/module.h>
80b488893aSPavel Emelyanov #include <linux/nsproxy.h>
811da177e4SLinus Torvalds #include <linux/interrupt.h>
821da177e4SLinus Torvalds #include <linux/init.h>
831da177e4SLinus Torvalds #include <linux/compat.h>
84dc9aa5b9SChristoph Lameter #include <linux/swap.h>
851a75a6c8SChristoph Lameter #include <linux/seq_file.h>
861a75a6c8SChristoph Lameter #include <linux/proc_fs.h>
87b20a3503SChristoph Lameter #include <linux/migrate.h>
8895a402c3SChristoph Lameter #include <linux/rmap.h>
8986c3a764SDavid Quigley #include <linux/security.h>
90dbcb0f19SAdrian Bunk #include <linux/syscalls.h>
91095f1fc4SLee Schermerhorn #include <linux/ctype.h>
926d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h>
93dc9aa5b9SChristoph Lameter 
941da177e4SLinus Torvalds #include <asm/tlbflush.h>
951da177e4SLinus Torvalds #include <asm/uaccess.h>
961da177e4SLinus Torvalds 
9762695a84SNick Piggin #include "internal.h"
9862695a84SNick Piggin 
9938e35860SChristoph Lameter /* Internal flags */
100dc9aa5b9SChristoph Lameter #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)	/* Skip checks for continuous vmas */
10138e35860SChristoph Lameter #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)		/* Invert check for nodemask */
1021a75a6c8SChristoph Lameter #define MPOL_MF_STATS (MPOL_MF_INTERNAL << 2)		/* Gather statistics */
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  */
114d42c6997SAndi Kleen 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);
12237012946SDavid Rientjes 	void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
12337012946SDavid Rientjes } mpol_ops[MPOL_MAX];
12437012946SDavid Rientjes 
12519770b32SMel Gorman /* Check that the nodemask contains at least one populated zone */
12637012946SDavid Rientjes static int is_valid_nodemask(const nodemask_t *nodemask)
1271da177e4SLinus Torvalds {
12819770b32SMel Gorman 	int nd, k;
1291da177e4SLinus Torvalds 
13019770b32SMel Gorman 	/* Check that there is something useful in this mask */
13119770b32SMel Gorman 	k = policy_zone;
13219770b32SMel Gorman 
13319770b32SMel Gorman 	for_each_node_mask(nd, *nodemask) {
13419770b32SMel Gorman 		struct zone *z;
13519770b32SMel Gorman 
13619770b32SMel Gorman 		for (k = 0; k <= policy_zone; k++) {
13719770b32SMel Gorman 			z = &NODE_DATA(nd)->node_zones[k];
138dd942ae3SAndi Kleen 			if (z->present_pages > 0)
13919770b32SMel Gorman 				return 1;
140dd942ae3SAndi Kleen 		}
141dd942ae3SAndi Kleen 	}
14219770b32SMel Gorman 
14319770b32SMel Gorman 	return 0;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
146f5b087b5SDavid Rientjes static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
147f5b087b5SDavid Rientjes {
1484c50bc01SDavid Rientjes 	return pol->flags & (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES);
1494c50bc01SDavid Rientjes }
1504c50bc01SDavid Rientjes 
1514c50bc01SDavid Rientjes static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
1524c50bc01SDavid Rientjes 				   const nodemask_t *rel)
1534c50bc01SDavid Rientjes {
1544c50bc01SDavid Rientjes 	nodemask_t tmp;
1554c50bc01SDavid Rientjes 	nodes_fold(tmp, *orig, nodes_weight(*rel));
1564c50bc01SDavid Rientjes 	nodes_onto(*ret, tmp, *rel);
157f5b087b5SDavid Rientjes }
158f5b087b5SDavid Rientjes 
15937012946SDavid Rientjes static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
16037012946SDavid Rientjes {
16137012946SDavid Rientjes 	if (nodes_empty(*nodes))
16237012946SDavid Rientjes 		return -EINVAL;
16337012946SDavid Rientjes 	pol->v.nodes = *nodes;
16437012946SDavid Rientjes 	return 0;
16537012946SDavid Rientjes }
16637012946SDavid Rientjes 
16737012946SDavid Rientjes static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
16837012946SDavid Rientjes {
16937012946SDavid Rientjes 	if (!nodes)
170fc36b8d3SLee Schermerhorn 		pol->flags |= MPOL_F_LOCAL;	/* local allocation */
17137012946SDavid Rientjes 	else if (nodes_empty(*nodes))
17237012946SDavid Rientjes 		return -EINVAL;			/*  no allowed nodes */
17337012946SDavid Rientjes 	else
17437012946SDavid Rientjes 		pol->v.preferred_node = first_node(*nodes);
17537012946SDavid Rientjes 	return 0;
17637012946SDavid Rientjes }
17737012946SDavid Rientjes 
17837012946SDavid Rientjes static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
17937012946SDavid Rientjes {
18037012946SDavid Rientjes 	if (!is_valid_nodemask(nodes))
18137012946SDavid Rientjes 		return -EINVAL;
18237012946SDavid Rientjes 	pol->v.nodes = *nodes;
18337012946SDavid Rientjes 	return 0;
18437012946SDavid Rientjes }
18537012946SDavid Rientjes 
18658568d2aSMiao Xie /*
18758568d2aSMiao Xie  * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
18858568d2aSMiao Xie  * any, for the new policy.  mpol_new() has already validated the nodes
18958568d2aSMiao Xie  * parameter with respect to the policy mode and flags.  But, we need to
19058568d2aSMiao Xie  * handle an empty nodemask with MPOL_PREFERRED here.
19158568d2aSMiao Xie  *
19258568d2aSMiao Xie  * Must be called holding task's alloc_lock to protect task's mems_allowed
19358568d2aSMiao Xie  * and mempolicy.  May also be called holding the mmap_semaphore for write.
19458568d2aSMiao Xie  */
1954bfc4495SKAMEZAWA Hiroyuki static int mpol_set_nodemask(struct mempolicy *pol,
1964bfc4495SKAMEZAWA Hiroyuki 		     const nodemask_t *nodes, struct nodemask_scratch *nsc)
19758568d2aSMiao Xie {
19858568d2aSMiao Xie 	int ret;
19958568d2aSMiao Xie 
20058568d2aSMiao Xie 	/* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
20158568d2aSMiao Xie 	if (pol == NULL)
20258568d2aSMiao Xie 		return 0;
2034bfc4495SKAMEZAWA Hiroyuki 	/* Check N_HIGH_MEMORY */
2044bfc4495SKAMEZAWA Hiroyuki 	nodes_and(nsc->mask1,
2054bfc4495SKAMEZAWA Hiroyuki 		  cpuset_current_mems_allowed, node_states[N_HIGH_MEMORY]);
20658568d2aSMiao Xie 
20758568d2aSMiao Xie 	VM_BUG_ON(!nodes);
20858568d2aSMiao Xie 	if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
20958568d2aSMiao Xie 		nodes = NULL;	/* explicit local allocation */
21058568d2aSMiao Xie 	else {
21158568d2aSMiao Xie 		if (pol->flags & MPOL_F_RELATIVE_NODES)
2124bfc4495SKAMEZAWA Hiroyuki 			mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1);
21358568d2aSMiao Xie 		else
2144bfc4495SKAMEZAWA Hiroyuki 			nodes_and(nsc->mask2, *nodes, nsc->mask1);
2154bfc4495SKAMEZAWA Hiroyuki 
21658568d2aSMiao Xie 		if (mpol_store_user_nodemask(pol))
21758568d2aSMiao Xie 			pol->w.user_nodemask = *nodes;
21858568d2aSMiao Xie 		else
21958568d2aSMiao Xie 			pol->w.cpuset_mems_allowed =
22058568d2aSMiao Xie 						cpuset_current_mems_allowed;
22158568d2aSMiao Xie 	}
22258568d2aSMiao Xie 
2234bfc4495SKAMEZAWA Hiroyuki 	if (nodes)
2244bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
2254bfc4495SKAMEZAWA Hiroyuki 	else
2264bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_ops[pol->mode].create(pol, NULL);
22758568d2aSMiao Xie 	return ret;
22858568d2aSMiao Xie }
22958568d2aSMiao Xie 
23058568d2aSMiao Xie /*
23158568d2aSMiao Xie  * This function just creates a new policy, does some check and simple
23258568d2aSMiao Xie  * initialization. You must invoke mpol_set_nodemask() to set nodes.
23358568d2aSMiao Xie  */
234028fec41SDavid Rientjes static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
235028fec41SDavid Rientjes 				  nodemask_t *nodes)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct mempolicy *policy;
2381da177e4SLinus Torvalds 
239028fec41SDavid Rientjes 	pr_debug("setting mode %d flags %d nodes[0] %lx\n",
240028fec41SDavid Rientjes 		 mode, flags, nodes ? nodes_addr(*nodes)[0] : -1);
241140d5a49SPaul Mundt 
2423e1f0645SDavid Rientjes 	if (mode == MPOL_DEFAULT) {
2433e1f0645SDavid Rientjes 		if (nodes && !nodes_empty(*nodes))
24437012946SDavid Rientjes 			return ERR_PTR(-EINVAL);
245bea904d5SLee Schermerhorn 		return NULL;	/* simply delete any existing policy */
24637012946SDavid Rientjes 	}
2473e1f0645SDavid Rientjes 	VM_BUG_ON(!nodes);
2483e1f0645SDavid Rientjes 
2493e1f0645SDavid Rientjes 	/*
2503e1f0645SDavid Rientjes 	 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
2513e1f0645SDavid Rientjes 	 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
2523e1f0645SDavid Rientjes 	 * All other modes require a valid pointer to a non-empty nodemask.
2533e1f0645SDavid Rientjes 	 */
2543e1f0645SDavid Rientjes 	if (mode == MPOL_PREFERRED) {
2553e1f0645SDavid Rientjes 		if (nodes_empty(*nodes)) {
2563e1f0645SDavid Rientjes 			if (((flags & MPOL_F_STATIC_NODES) ||
2573e1f0645SDavid Rientjes 			     (flags & MPOL_F_RELATIVE_NODES)))
2583e1f0645SDavid Rientjes 				return ERR_PTR(-EINVAL);
2593e1f0645SDavid Rientjes 		}
2603e1f0645SDavid Rientjes 	} else if (nodes_empty(*nodes))
2613e1f0645SDavid Rientjes 		return ERR_PTR(-EINVAL);
2621da177e4SLinus Torvalds 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2631da177e4SLinus Torvalds 	if (!policy)
2641da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2651da177e4SLinus Torvalds 	atomic_set(&policy->refcnt, 1);
26645c4745aSLee Schermerhorn 	policy->mode = mode;
26737012946SDavid Rientjes 	policy->flags = flags;
2683e1f0645SDavid Rientjes 
26937012946SDavid Rientjes 	return policy;
27037012946SDavid Rientjes }
27137012946SDavid Rientjes 
27252cd3b07SLee Schermerhorn /* Slow path of a mpol destructor. */
27352cd3b07SLee Schermerhorn void __mpol_put(struct mempolicy *p)
27452cd3b07SLee Schermerhorn {
27552cd3b07SLee Schermerhorn 	if (!atomic_dec_and_test(&p->refcnt))
27652cd3b07SLee Schermerhorn 		return;
27752cd3b07SLee Schermerhorn 	kmem_cache_free(policy_cache, p);
27852cd3b07SLee Schermerhorn }
27952cd3b07SLee Schermerhorn 
28037012946SDavid Rientjes static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes)
28137012946SDavid Rientjes {
28237012946SDavid Rientjes }
28337012946SDavid Rientjes 
28437012946SDavid Rientjes static void mpol_rebind_nodemask(struct mempolicy *pol,
28537012946SDavid Rientjes 				 const nodemask_t *nodes)
2861d0d2680SDavid Rientjes {
2871d0d2680SDavid Rientjes 	nodemask_t tmp;
2881d0d2680SDavid Rientjes 
28937012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES)
29037012946SDavid Rientjes 		nodes_and(tmp, pol->w.user_nodemask, *nodes);
29137012946SDavid Rientjes 	else if (pol->flags & MPOL_F_RELATIVE_NODES)
29237012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
2931d0d2680SDavid Rientjes 	else {
29437012946SDavid Rientjes 		nodes_remap(tmp, pol->v.nodes, pol->w.cpuset_mems_allowed,
29537012946SDavid Rientjes 			    *nodes);
29637012946SDavid Rientjes 		pol->w.cpuset_mems_allowed = *nodes;
2971d0d2680SDavid Rientjes 	}
29837012946SDavid Rientjes 
2991d0d2680SDavid Rientjes 	pol->v.nodes = tmp;
3001d0d2680SDavid Rientjes 	if (!node_isset(current->il_next, tmp)) {
3011d0d2680SDavid Rientjes 		current->il_next = next_node(current->il_next, tmp);
3021d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
3031d0d2680SDavid Rientjes 			current->il_next = first_node(tmp);
3041d0d2680SDavid Rientjes 		if (current->il_next >= MAX_NUMNODES)
3051d0d2680SDavid Rientjes 			current->il_next = numa_node_id();
3061d0d2680SDavid Rientjes 	}
30737012946SDavid Rientjes }
30837012946SDavid Rientjes 
30937012946SDavid Rientjes static void mpol_rebind_preferred(struct mempolicy *pol,
31037012946SDavid Rientjes 				  const nodemask_t *nodes)
31137012946SDavid Rientjes {
31237012946SDavid Rientjes 	nodemask_t tmp;
31337012946SDavid Rientjes 
31437012946SDavid Rientjes 	if (pol->flags & MPOL_F_STATIC_NODES) {
3151d0d2680SDavid Rientjes 		int node = first_node(pol->w.user_nodemask);
3161d0d2680SDavid Rientjes 
317fc36b8d3SLee Schermerhorn 		if (node_isset(node, *nodes)) {
3181d0d2680SDavid Rientjes 			pol->v.preferred_node = node;
319fc36b8d3SLee Schermerhorn 			pol->flags &= ~MPOL_F_LOCAL;
320fc36b8d3SLee Schermerhorn 		} else
321fc36b8d3SLee Schermerhorn 			pol->flags |= MPOL_F_LOCAL;
32237012946SDavid Rientjes 	} else if (pol->flags & MPOL_F_RELATIVE_NODES) {
32337012946SDavid Rientjes 		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
3241d0d2680SDavid Rientjes 		pol->v.preferred_node = first_node(tmp);
325fc36b8d3SLee Schermerhorn 	} else if (!(pol->flags & MPOL_F_LOCAL)) {
3261d0d2680SDavid Rientjes 		pol->v.preferred_node = node_remap(pol->v.preferred_node,
32737012946SDavid Rientjes 						   pol->w.cpuset_mems_allowed,
32837012946SDavid Rientjes 						   *nodes);
32937012946SDavid Rientjes 		pol->w.cpuset_mems_allowed = *nodes;
3301d0d2680SDavid Rientjes 	}
3311d0d2680SDavid Rientjes }
33237012946SDavid Rientjes 
33337012946SDavid Rientjes /* Migrate a policy to a different set of nodes */
33437012946SDavid Rientjes static void mpol_rebind_policy(struct mempolicy *pol,
33537012946SDavid Rientjes 			       const nodemask_t *newmask)
33637012946SDavid Rientjes {
33737012946SDavid Rientjes 	if (!pol)
33837012946SDavid Rientjes 		return;
33937012946SDavid Rientjes 	if (!mpol_store_user_nodemask(pol) &&
34037012946SDavid Rientjes 	    nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
34137012946SDavid Rientjes 		return;
34245c4745aSLee Schermerhorn 	mpol_ops[pol->mode].rebind(pol, newmask);
3431d0d2680SDavid Rientjes }
3441d0d2680SDavid Rientjes 
3451d0d2680SDavid Rientjes /*
3461d0d2680SDavid Rientjes  * Wrapper for mpol_rebind_policy() that just requires task
3471d0d2680SDavid Rientjes  * pointer, and updates task mempolicy.
34858568d2aSMiao Xie  *
34958568d2aSMiao Xie  * Called with task's alloc_lock held.
3501d0d2680SDavid Rientjes  */
3511d0d2680SDavid Rientjes 
3521d0d2680SDavid Rientjes void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new)
3531d0d2680SDavid Rientjes {
3541d0d2680SDavid Rientjes 	mpol_rebind_policy(tsk->mempolicy, new);
3551d0d2680SDavid Rientjes }
3561d0d2680SDavid Rientjes 
3571d0d2680SDavid Rientjes /*
3581d0d2680SDavid Rientjes  * Rebind each vma in mm to new nodemask.
3591d0d2680SDavid Rientjes  *
3601d0d2680SDavid Rientjes  * Call holding a reference to mm.  Takes mm->mmap_sem during call.
3611d0d2680SDavid Rientjes  */
3621d0d2680SDavid Rientjes 
3631d0d2680SDavid Rientjes void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
3641d0d2680SDavid Rientjes {
3651d0d2680SDavid Rientjes 	struct vm_area_struct *vma;
3661d0d2680SDavid Rientjes 
3671d0d2680SDavid Rientjes 	down_write(&mm->mmap_sem);
3681d0d2680SDavid Rientjes 	for (vma = mm->mmap; vma; vma = vma->vm_next)
3691d0d2680SDavid Rientjes 		mpol_rebind_policy(vma->vm_policy, new);
3701d0d2680SDavid Rientjes 	up_write(&mm->mmap_sem);
3711d0d2680SDavid Rientjes }
3721d0d2680SDavid Rientjes 
37337012946SDavid Rientjes static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
37437012946SDavid Rientjes 	[MPOL_DEFAULT] = {
37537012946SDavid Rientjes 		.rebind = mpol_rebind_default,
37637012946SDavid Rientjes 	},
37737012946SDavid Rientjes 	[MPOL_INTERLEAVE] = {
37837012946SDavid Rientjes 		.create = mpol_new_interleave,
37937012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
38037012946SDavid Rientjes 	},
38137012946SDavid Rientjes 	[MPOL_PREFERRED] = {
38237012946SDavid Rientjes 		.create = mpol_new_preferred,
38337012946SDavid Rientjes 		.rebind = mpol_rebind_preferred,
38437012946SDavid Rientjes 	},
38537012946SDavid Rientjes 	[MPOL_BIND] = {
38637012946SDavid Rientjes 		.create = mpol_new_bind,
38737012946SDavid Rientjes 		.rebind = mpol_rebind_nodemask,
38837012946SDavid Rientjes 	},
38937012946SDavid Rientjes };
39037012946SDavid Rientjes 
391397874dfSChristoph Lameter static void gather_stats(struct page *, void *, int pte_dirty);
392fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
393fc301289SChristoph Lameter 				unsigned long flags);
3941a75a6c8SChristoph Lameter 
39538e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */
396b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
397dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
398dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
39938e35860SChristoph Lameter 		void *private)
4001da177e4SLinus Torvalds {
40191612e0dSHugh Dickins 	pte_t *orig_pte;
40291612e0dSHugh Dickins 	pte_t *pte;
403705e87c0SHugh Dickins 	spinlock_t *ptl;
404941150a3SHugh Dickins 
405705e87c0SHugh Dickins 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
40691612e0dSHugh Dickins 	do {
4076aab341eSLinus Torvalds 		struct page *page;
40825ba77c1SAndy Whitcroft 		int nid;
40991612e0dSHugh Dickins 
41091612e0dSHugh Dickins 		if (!pte_present(*pte))
41191612e0dSHugh Dickins 			continue;
4126aab341eSLinus Torvalds 		page = vm_normal_page(vma, addr, *pte);
4136aab341eSLinus Torvalds 		if (!page)
41491612e0dSHugh Dickins 			continue;
415053837fcSNick Piggin 		/*
416053837fcSNick Piggin 		 * The check for PageReserved here is important to avoid
417053837fcSNick Piggin 		 * handling zero pages and other pages that may have been
418053837fcSNick Piggin 		 * marked special by the system.
419053837fcSNick Piggin 		 *
420053837fcSNick Piggin 		 * If the PageReserved would not be checked here then f.e.
421053837fcSNick Piggin 		 * the location of the zero page could have an influence
422053837fcSNick Piggin 		 * on MPOL_MF_STRICT, zero pages would be counted for
423053837fcSNick Piggin 		 * the per node stats, and there would be useless attempts
424053837fcSNick Piggin 		 * to put zero pages on the migration list.
425053837fcSNick Piggin 		 */
426f4598c8bSChristoph Lameter 		if (PageReserved(page))
427f4598c8bSChristoph Lameter 			continue;
4286aab341eSLinus Torvalds 		nid = page_to_nid(page);
42938e35860SChristoph Lameter 		if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
43038e35860SChristoph Lameter 			continue;
43138e35860SChristoph Lameter 
4321a75a6c8SChristoph Lameter 		if (flags & MPOL_MF_STATS)
433397874dfSChristoph Lameter 			gather_stats(page, private, pte_dirty(*pte));
434053837fcSNick Piggin 		else if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
435fc301289SChristoph Lameter 			migrate_page_add(page, private, flags);
436dc9aa5b9SChristoph Lameter 		else
4371da177e4SLinus Torvalds 			break;
43891612e0dSHugh Dickins 	} while (pte++, addr += PAGE_SIZE, addr != end);
439705e87c0SHugh Dickins 	pte_unmap_unlock(orig_pte, ptl);
44091612e0dSHugh Dickins 	return addr != end;
44191612e0dSHugh Dickins }
44291612e0dSHugh Dickins 
443b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
444dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
445dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
44638e35860SChristoph Lameter 		void *private)
44791612e0dSHugh Dickins {
44891612e0dSHugh Dickins 	pmd_t *pmd;
44991612e0dSHugh Dickins 	unsigned long next;
45091612e0dSHugh Dickins 
45191612e0dSHugh Dickins 	pmd = pmd_offset(pud, addr);
45291612e0dSHugh Dickins 	do {
45391612e0dSHugh Dickins 		next = pmd_addr_end(addr, end);
45491612e0dSHugh Dickins 		if (pmd_none_or_clear_bad(pmd))
45591612e0dSHugh Dickins 			continue;
456dc9aa5b9SChristoph Lameter 		if (check_pte_range(vma, pmd, addr, next, nodes,
45738e35860SChristoph Lameter 				    flags, private))
45891612e0dSHugh Dickins 			return -EIO;
45991612e0dSHugh Dickins 	} while (pmd++, addr = next, addr != end);
46091612e0dSHugh Dickins 	return 0;
46191612e0dSHugh Dickins }
46291612e0dSHugh Dickins 
463b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
464dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
465dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
46638e35860SChristoph Lameter 		void *private)
46791612e0dSHugh Dickins {
46891612e0dSHugh Dickins 	pud_t *pud;
46991612e0dSHugh Dickins 	unsigned long next;
47091612e0dSHugh Dickins 
47191612e0dSHugh Dickins 	pud = pud_offset(pgd, addr);
47291612e0dSHugh Dickins 	do {
47391612e0dSHugh Dickins 		next = pud_addr_end(addr, end);
47491612e0dSHugh Dickins 		if (pud_none_or_clear_bad(pud))
47591612e0dSHugh Dickins 			continue;
476dc9aa5b9SChristoph Lameter 		if (check_pmd_range(vma, pud, addr, next, nodes,
47738e35860SChristoph Lameter 				    flags, private))
47891612e0dSHugh Dickins 			return -EIO;
47991612e0dSHugh Dickins 	} while (pud++, addr = next, addr != end);
48091612e0dSHugh Dickins 	return 0;
48191612e0dSHugh Dickins }
48291612e0dSHugh Dickins 
483b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma,
484dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
485dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
48638e35860SChristoph Lameter 		void *private)
48791612e0dSHugh Dickins {
48891612e0dSHugh Dickins 	pgd_t *pgd;
48991612e0dSHugh Dickins 	unsigned long next;
49091612e0dSHugh Dickins 
491b5810039SNick Piggin 	pgd = pgd_offset(vma->vm_mm, addr);
49291612e0dSHugh Dickins 	do {
49391612e0dSHugh Dickins 		next = pgd_addr_end(addr, end);
49491612e0dSHugh Dickins 		if (pgd_none_or_clear_bad(pgd))
49591612e0dSHugh Dickins 			continue;
496dc9aa5b9SChristoph Lameter 		if (check_pud_range(vma, pgd, addr, next, nodes,
49738e35860SChristoph Lameter 				    flags, private))
49891612e0dSHugh Dickins 			return -EIO;
49991612e0dSHugh Dickins 	} while (pgd++, addr = next, addr != end);
50091612e0dSHugh Dickins 	return 0;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
503dc9aa5b9SChristoph Lameter /*
504dc9aa5b9SChristoph Lameter  * Check if all pages in a range are on a set of nodes.
505dc9aa5b9SChristoph Lameter  * If pagelist != NULL then isolate pages from the LRU and
506dc9aa5b9SChristoph Lameter  * put them on the pagelist.
507dc9aa5b9SChristoph Lameter  */
5081da177e4SLinus Torvalds static struct vm_area_struct *
5091da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
51038e35860SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags, void *private)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	int err;
5131da177e4SLinus Torvalds 	struct vm_area_struct *first, *vma, *prev;
5141da177e4SLinus Torvalds 
515053837fcSNick Piggin 
5161da177e4SLinus Torvalds 	first = find_vma(mm, start);
5171da177e4SLinus Torvalds 	if (!first)
5181da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
5191da177e4SLinus Torvalds 	prev = NULL;
5201da177e4SLinus Torvalds 	for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
521dc9aa5b9SChristoph Lameter 		if (!(flags & MPOL_MF_DISCONTIG_OK)) {
5221da177e4SLinus Torvalds 			if (!vma->vm_next && vma->vm_end < end)
5231da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
5241da177e4SLinus Torvalds 			if (prev && prev->vm_end < vma->vm_start)
5251da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
526dc9aa5b9SChristoph Lameter 		}
527dc9aa5b9SChristoph Lameter 		if (!is_vm_hugetlb_page(vma) &&
528dc9aa5b9SChristoph Lameter 		    ((flags & MPOL_MF_STRICT) ||
529dc9aa5b9SChristoph Lameter 		     ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
530dc9aa5b9SChristoph Lameter 				vma_migratable(vma)))) {
5315b952b3cSAndi Kleen 			unsigned long endvma = vma->vm_end;
532dc9aa5b9SChristoph Lameter 
5335b952b3cSAndi Kleen 			if (endvma > end)
5345b952b3cSAndi Kleen 				endvma = end;
5355b952b3cSAndi Kleen 			if (vma->vm_start > start)
5365b952b3cSAndi Kleen 				start = vma->vm_start;
537dc9aa5b9SChristoph Lameter 			err = check_pgd_range(vma, start, endvma, nodes,
53838e35860SChristoph Lameter 						flags, private);
5391da177e4SLinus Torvalds 			if (err) {
5401da177e4SLinus Torvalds 				first = ERR_PTR(err);
5411da177e4SLinus Torvalds 				break;
5421da177e4SLinus Torvalds 			}
5431da177e4SLinus Torvalds 		}
5441da177e4SLinus Torvalds 		prev = vma;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 	return first;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /* Apply policy to a single VMA */
5501da177e4SLinus Torvalds static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	int err = 0;
5531da177e4SLinus Torvalds 	struct mempolicy *old = vma->vm_policy;
5541da177e4SLinus Torvalds 
555140d5a49SPaul Mundt 	pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
5561da177e4SLinus Torvalds 		 vma->vm_start, vma->vm_end, vma->vm_pgoff,
5571da177e4SLinus Torvalds 		 vma->vm_ops, vma->vm_file,
5581da177e4SLinus Torvalds 		 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->set_policy)
5611da177e4SLinus Torvalds 		err = vma->vm_ops->set_policy(vma, new);
5621da177e4SLinus Torvalds 	if (!err) {
5631da177e4SLinus Torvalds 		mpol_get(new);
5641da177e4SLinus Torvalds 		vma->vm_policy = new;
565f0be3d32SLee Schermerhorn 		mpol_put(old);
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 	return err;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */
5711da177e4SLinus Torvalds static int mbind_range(struct vm_area_struct *vma, unsigned long start,
5721da177e4SLinus Torvalds 		       unsigned long end, struct mempolicy *new)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct vm_area_struct *next;
5751da177e4SLinus Torvalds 	int err;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	err = 0;
5781da177e4SLinus Torvalds 	for (; vma && vma->vm_start < end; vma = next) {
5791da177e4SLinus Torvalds 		next = vma->vm_next;
5801da177e4SLinus Torvalds 		if (vma->vm_start < start)
5811da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, start, 1);
5821da177e4SLinus Torvalds 		if (!err && vma->vm_end > end)
5831da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, end, 0);
5841da177e4SLinus Torvalds 		if (!err)
5851da177e4SLinus Torvalds 			err = policy_vma(vma, new);
5861da177e4SLinus Torvalds 		if (err)
5871da177e4SLinus Torvalds 			break;
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds 	return err;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
592c61afb18SPaul Jackson /*
593c61afb18SPaul Jackson  * Update task->flags PF_MEMPOLICY bit: set iff non-default
594c61afb18SPaul Jackson  * mempolicy.  Allows more rapid checking of this (combined perhaps
595c61afb18SPaul Jackson  * with other PF_* flag bits) on memory allocation hot code paths.
596c61afb18SPaul Jackson  *
597c61afb18SPaul Jackson  * If called from outside this file, the task 'p' should -only- be
598c61afb18SPaul Jackson  * a newly forked child not yet visible on the task list, because
599c61afb18SPaul Jackson  * manipulating the task flags of a visible task is not safe.
600c61afb18SPaul Jackson  *
601c61afb18SPaul Jackson  * The above limitation is why this routine has the funny name
602c61afb18SPaul Jackson  * mpol_fix_fork_child_flag().
603c61afb18SPaul Jackson  *
604c61afb18SPaul Jackson  * It is also safe to call this with a task pointer of current,
605c61afb18SPaul Jackson  * which the static wrapper mpol_set_task_struct_flag() does,
606c61afb18SPaul Jackson  * for use within this file.
607c61afb18SPaul Jackson  */
608c61afb18SPaul Jackson 
609c61afb18SPaul Jackson void mpol_fix_fork_child_flag(struct task_struct *p)
610c61afb18SPaul Jackson {
611c61afb18SPaul Jackson 	if (p->mempolicy)
612c61afb18SPaul Jackson 		p->flags |= PF_MEMPOLICY;
613c61afb18SPaul Jackson 	else
614c61afb18SPaul Jackson 		p->flags &= ~PF_MEMPOLICY;
615c61afb18SPaul Jackson }
616c61afb18SPaul Jackson 
617c61afb18SPaul Jackson static void mpol_set_task_struct_flag(void)
618c61afb18SPaul Jackson {
619c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(current);
620c61afb18SPaul Jackson }
621c61afb18SPaul Jackson 
6221da177e4SLinus Torvalds /* Set the process memory policy */
623028fec41SDavid Rientjes static long do_set_mempolicy(unsigned short mode, unsigned short flags,
624028fec41SDavid Rientjes 			     nodemask_t *nodes)
6251da177e4SLinus Torvalds {
62658568d2aSMiao Xie 	struct mempolicy *new, *old;
627f4e53d91SLee Schermerhorn 	struct mm_struct *mm = current->mm;
6284bfc4495SKAMEZAWA Hiroyuki 	NODEMASK_SCRATCH(scratch);
62958568d2aSMiao Xie 	int ret;
6301da177e4SLinus Torvalds 
6314bfc4495SKAMEZAWA Hiroyuki 	if (!scratch)
6324bfc4495SKAMEZAWA Hiroyuki 		return -ENOMEM;
633f4e53d91SLee Schermerhorn 
6344bfc4495SKAMEZAWA Hiroyuki 	new = mpol_new(mode, flags, nodes);
6354bfc4495SKAMEZAWA Hiroyuki 	if (IS_ERR(new)) {
6364bfc4495SKAMEZAWA Hiroyuki 		ret = PTR_ERR(new);
6374bfc4495SKAMEZAWA Hiroyuki 		goto out;
6384bfc4495SKAMEZAWA Hiroyuki 	}
639f4e53d91SLee Schermerhorn 	/*
640f4e53d91SLee Schermerhorn 	 * prevent changing our mempolicy while show_numa_maps()
641f4e53d91SLee Schermerhorn 	 * is using it.
642f4e53d91SLee Schermerhorn 	 * Note:  do_set_mempolicy() can be called at init time
643f4e53d91SLee Schermerhorn 	 * with no 'mm'.
644f4e53d91SLee Schermerhorn 	 */
645f4e53d91SLee Schermerhorn 	if (mm)
646f4e53d91SLee Schermerhorn 		down_write(&mm->mmap_sem);
64758568d2aSMiao Xie 	task_lock(current);
6484bfc4495SKAMEZAWA Hiroyuki 	ret = mpol_set_nodemask(new, nodes, scratch);
64958568d2aSMiao Xie 	if (ret) {
65058568d2aSMiao Xie 		task_unlock(current);
65158568d2aSMiao Xie 		if (mm)
65258568d2aSMiao Xie 			up_write(&mm->mmap_sem);
65358568d2aSMiao Xie 		mpol_put(new);
6544bfc4495SKAMEZAWA Hiroyuki 		goto out;
65558568d2aSMiao Xie 	}
65658568d2aSMiao Xie 	old = current->mempolicy;
6571da177e4SLinus Torvalds 	current->mempolicy = new;
658c61afb18SPaul Jackson 	mpol_set_task_struct_flag();
65945c4745aSLee Schermerhorn 	if (new && new->mode == MPOL_INTERLEAVE &&
660f5b087b5SDavid Rientjes 	    nodes_weight(new->v.nodes))
661dfcd3c0dSAndi Kleen 		current->il_next = first_node(new->v.nodes);
66258568d2aSMiao Xie 	task_unlock(current);
663f4e53d91SLee Schermerhorn 	if (mm)
664f4e53d91SLee Schermerhorn 		up_write(&mm->mmap_sem);
665f4e53d91SLee Schermerhorn 
66658568d2aSMiao Xie 	mpol_put(old);
6674bfc4495SKAMEZAWA Hiroyuki 	ret = 0;
6684bfc4495SKAMEZAWA Hiroyuki out:
6694bfc4495SKAMEZAWA Hiroyuki 	NODEMASK_SCRATCH_FREE(scratch);
6704bfc4495SKAMEZAWA Hiroyuki 	return ret;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
673bea904d5SLee Schermerhorn /*
674bea904d5SLee Schermerhorn  * Return nodemask for policy for get_mempolicy() query
67558568d2aSMiao Xie  *
67658568d2aSMiao Xie  * Called with task's alloc_lock held
677bea904d5SLee Schermerhorn  */
678bea904d5SLee Schermerhorn static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
6791da177e4SLinus Torvalds {
680dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
681bea904d5SLee Schermerhorn 	if (p == &default_policy)
682bea904d5SLee Schermerhorn 		return;
683bea904d5SLee Schermerhorn 
68445c4745aSLee Schermerhorn 	switch (p->mode) {
68519770b32SMel Gorman 	case MPOL_BIND:
68619770b32SMel Gorman 		/* Fall through */
6871da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
688dfcd3c0dSAndi Kleen 		*nodes = p->v.nodes;
6891da177e4SLinus Torvalds 		break;
6901da177e4SLinus Torvalds 	case MPOL_PREFERRED:
691fc36b8d3SLee Schermerhorn 		if (!(p->flags & MPOL_F_LOCAL))
692dfcd3c0dSAndi Kleen 			node_set(p->v.preferred_node, *nodes);
69353f2556bSLee Schermerhorn 		/* else return empty node mask for local allocation */
6941da177e4SLinus Torvalds 		break;
6951da177e4SLinus Torvalds 	default:
6961da177e4SLinus Torvalds 		BUG();
6971da177e4SLinus Torvalds 	}
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	struct page *p;
7031da177e4SLinus Torvalds 	int err;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
7061da177e4SLinus Torvalds 	if (err >= 0) {
7071da177e4SLinus Torvalds 		err = page_to_nid(p);
7081da177e4SLinus Torvalds 		put_page(p);
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 	return err;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds /* Retrieve NUMA policy */
714dbcb0f19SAdrian Bunk static long do_get_mempolicy(int *policy, nodemask_t *nmask,
7151da177e4SLinus Torvalds 			     unsigned long addr, unsigned long flags)
7161da177e4SLinus Torvalds {
7178bccd85fSChristoph Lameter 	int err;
7181da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
7191da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
7201da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
7211da177e4SLinus Torvalds 
722754af6f5SLee Schermerhorn 	if (flags &
723754af6f5SLee Schermerhorn 		~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
7241da177e4SLinus Torvalds 		return -EINVAL;
725754af6f5SLee Schermerhorn 
726754af6f5SLee Schermerhorn 	if (flags & MPOL_F_MEMS_ALLOWED) {
727754af6f5SLee Schermerhorn 		if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
728754af6f5SLee Schermerhorn 			return -EINVAL;
729754af6f5SLee Schermerhorn 		*policy = 0;	/* just so it's initialized */
73058568d2aSMiao Xie 		task_lock(current);
731754af6f5SLee Schermerhorn 		*nmask  = cpuset_current_mems_allowed;
73258568d2aSMiao Xie 		task_unlock(current);
733754af6f5SLee Schermerhorn 		return 0;
734754af6f5SLee Schermerhorn 	}
735754af6f5SLee Schermerhorn 
7361da177e4SLinus Torvalds 	if (flags & MPOL_F_ADDR) {
737bea904d5SLee Schermerhorn 		/*
738bea904d5SLee Schermerhorn 		 * Do NOT fall back to task policy if the
739bea904d5SLee Schermerhorn 		 * vma/shared policy at addr is NULL.  We
740bea904d5SLee Schermerhorn 		 * want to return MPOL_DEFAULT in this case.
741bea904d5SLee Schermerhorn 		 */
7421da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
7431da177e4SLinus Torvalds 		vma = find_vma_intersection(mm, addr, addr+1);
7441da177e4SLinus Torvalds 		if (!vma) {
7451da177e4SLinus Torvalds 			up_read(&mm->mmap_sem);
7461da177e4SLinus Torvalds 			return -EFAULT;
7471da177e4SLinus Torvalds 		}
7481da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
7491da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
7501da177e4SLinus Torvalds 		else
7511da177e4SLinus Torvalds 			pol = vma->vm_policy;
7521da177e4SLinus Torvalds 	} else if (addr)
7531da177e4SLinus Torvalds 		return -EINVAL;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	if (!pol)
756bea904d5SLee Schermerhorn 		pol = &default_policy;	/* indicates default behavior */
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	if (flags & MPOL_F_NODE) {
7591da177e4SLinus Torvalds 		if (flags & MPOL_F_ADDR) {
7601da177e4SLinus Torvalds 			err = lookup_node(mm, addr);
7611da177e4SLinus Torvalds 			if (err < 0)
7621da177e4SLinus Torvalds 				goto out;
7638bccd85fSChristoph Lameter 			*policy = err;
7641da177e4SLinus Torvalds 		} else if (pol == current->mempolicy &&
76545c4745aSLee Schermerhorn 				pol->mode == MPOL_INTERLEAVE) {
7668bccd85fSChristoph Lameter 			*policy = current->il_next;
7671da177e4SLinus Torvalds 		} else {
7681da177e4SLinus Torvalds 			err = -EINVAL;
7691da177e4SLinus Torvalds 			goto out;
7701da177e4SLinus Torvalds 		}
771bea904d5SLee Schermerhorn 	} else {
772bea904d5SLee Schermerhorn 		*policy = pol == &default_policy ? MPOL_DEFAULT :
773bea904d5SLee Schermerhorn 						pol->mode;
774d79df630SDavid Rientjes 		/*
775d79df630SDavid Rientjes 		 * Internal mempolicy flags must be masked off before exposing
776d79df630SDavid Rientjes 		 * the policy to userspace.
777d79df630SDavid Rientjes 		 */
778d79df630SDavid Rientjes 		*policy |= (pol->flags & MPOL_MODE_FLAGS);
779bea904d5SLee Schermerhorn 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (vma) {
7821da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
7831da177e4SLinus Torvalds 		vma = NULL;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	err = 0;
78758568d2aSMiao Xie 	if (nmask) {
78858568d2aSMiao Xie 		task_lock(current);
789bea904d5SLee Schermerhorn 		get_policy_nodemask(pol, nmask);
79058568d2aSMiao Xie 		task_unlock(current);
79158568d2aSMiao Xie 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds  out:
79452cd3b07SLee Schermerhorn 	mpol_cond_put(pol);
7951da177e4SLinus Torvalds 	if (vma)
7961da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
7971da177e4SLinus Torvalds 	return err;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
800b20a3503SChristoph Lameter #ifdef CONFIG_MIGRATION
8018bccd85fSChristoph Lameter /*
8026ce3c4c0SChristoph Lameter  * page migration
8036ce3c4c0SChristoph Lameter  */
804fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
805fc301289SChristoph Lameter 				unsigned long flags)
8066ce3c4c0SChristoph Lameter {
8076ce3c4c0SChristoph Lameter 	/*
808fc301289SChristoph Lameter 	 * Avoid migrating a page that is shared with others.
8096ce3c4c0SChristoph Lameter 	 */
81062695a84SNick Piggin 	if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
81162695a84SNick Piggin 		if (!isolate_lru_page(page)) {
81262695a84SNick Piggin 			list_add_tail(&page->lru, pagelist);
8136d9c285aSKOSAKI Motohiro 			inc_zone_page_state(page, NR_ISOLATED_ANON +
8146d9c285aSKOSAKI Motohiro 					    page_is_file_cache(page));
81562695a84SNick Piggin 		}
81662695a84SNick Piggin 	}
8176ce3c4c0SChristoph Lameter }
8186ce3c4c0SChristoph Lameter 
819742755a1SChristoph Lameter static struct page *new_node_page(struct page *page, unsigned long node, int **x)
82095a402c3SChristoph Lameter {
8216484eb3eSMel Gorman 	return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
82295a402c3SChristoph Lameter }
82395a402c3SChristoph Lameter 
8246ce3c4c0SChristoph Lameter /*
8257e2ab150SChristoph Lameter  * Migrate pages from one node to a target node.
8267e2ab150SChristoph Lameter  * Returns error or the number of pages not migrated.
8277e2ab150SChristoph Lameter  */
828dbcb0f19SAdrian Bunk static int migrate_to_node(struct mm_struct *mm, int source, int dest,
829dbcb0f19SAdrian Bunk 			   int flags)
8307e2ab150SChristoph Lameter {
8317e2ab150SChristoph Lameter 	nodemask_t nmask;
8327e2ab150SChristoph Lameter 	LIST_HEAD(pagelist);
8337e2ab150SChristoph Lameter 	int err = 0;
8347e2ab150SChristoph Lameter 
8357e2ab150SChristoph Lameter 	nodes_clear(nmask);
8367e2ab150SChristoph Lameter 	node_set(source, nmask);
8377e2ab150SChristoph Lameter 
8387e2ab150SChristoph Lameter 	check_range(mm, mm->mmap->vm_start, TASK_SIZE, &nmask,
8397e2ab150SChristoph Lameter 			flags | MPOL_MF_DISCONTIG_OK, &pagelist);
8407e2ab150SChristoph Lameter 
8417e2ab150SChristoph Lameter 	if (!list_empty(&pagelist))
84295a402c3SChristoph Lameter 		err = migrate_pages(&pagelist, new_node_page, dest);
84395a402c3SChristoph Lameter 
8447e2ab150SChristoph Lameter 	return err;
8457e2ab150SChristoph Lameter }
8467e2ab150SChristoph Lameter 
8477e2ab150SChristoph Lameter /*
8487e2ab150SChristoph Lameter  * Move pages between the two nodesets so as to preserve the physical
8497e2ab150SChristoph Lameter  * layout as much as possible.
85039743889SChristoph Lameter  *
85139743889SChristoph Lameter  * Returns the number of page that could not be moved.
85239743889SChristoph Lameter  */
85339743889SChristoph Lameter int do_migrate_pages(struct mm_struct *mm,
85439743889SChristoph Lameter 	const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
85539743889SChristoph Lameter {
8567e2ab150SChristoph Lameter 	int busy = 0;
8570aedadf9SChristoph Lameter 	int err;
8587e2ab150SChristoph Lameter 	nodemask_t tmp;
85939743889SChristoph Lameter 
8600aedadf9SChristoph Lameter 	err = migrate_prep();
8610aedadf9SChristoph Lameter 	if (err)
8620aedadf9SChristoph Lameter 		return err;
8630aedadf9SChristoph Lameter 
86439743889SChristoph Lameter 	down_read(&mm->mmap_sem);
865d4984711SChristoph Lameter 
8667b2259b3SChristoph Lameter 	err = migrate_vmas(mm, from_nodes, to_nodes, flags);
8677b2259b3SChristoph Lameter 	if (err)
8687b2259b3SChristoph Lameter 		goto out;
8697b2259b3SChristoph Lameter 
8707e2ab150SChristoph Lameter /*
8717e2ab150SChristoph Lameter  * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
8727e2ab150SChristoph Lameter  * bit in 'to' is not also set in 'tmp'.  Clear the found 'source'
8737e2ab150SChristoph Lameter  * bit in 'tmp', and return that <source, dest> pair for migration.
8747e2ab150SChristoph Lameter  * The pair of nodemasks 'to' and 'from' define the map.
8757e2ab150SChristoph Lameter  *
8767e2ab150SChristoph Lameter  * If no pair of bits is found that way, fallback to picking some
8777e2ab150SChristoph Lameter  * pair of 'source' and 'dest' bits that are not the same.  If the
8787e2ab150SChristoph Lameter  * 'source' and 'dest' bits are the same, this represents a node
8797e2ab150SChristoph Lameter  * that will be migrating to itself, so no pages need move.
8807e2ab150SChristoph Lameter  *
8817e2ab150SChristoph Lameter  * If no bits are left in 'tmp', or if all remaining bits left
8827e2ab150SChristoph Lameter  * in 'tmp' correspond to the same bit in 'to', return false
8837e2ab150SChristoph Lameter  * (nothing left to migrate).
8847e2ab150SChristoph Lameter  *
8857e2ab150SChristoph Lameter  * This lets us pick a pair of nodes to migrate between, such that
8867e2ab150SChristoph Lameter  * if possible the dest node is not already occupied by some other
8877e2ab150SChristoph Lameter  * source node, minimizing the risk of overloading the memory on a
8887e2ab150SChristoph Lameter  * node that would happen if we migrated incoming memory to a node
8897e2ab150SChristoph Lameter  * before migrating outgoing memory source that same node.
8907e2ab150SChristoph Lameter  *
8917e2ab150SChristoph Lameter  * A single scan of tmp is sufficient.  As we go, we remember the
8927e2ab150SChristoph Lameter  * most recent <s, d> pair that moved (s != d).  If we find a pair
8937e2ab150SChristoph Lameter  * that not only moved, but what's better, moved to an empty slot
8947e2ab150SChristoph Lameter  * (d is not set in tmp), then we break out then, with that pair.
8957e2ab150SChristoph Lameter  * Otherwise when we finish scannng from_tmp, we at least have the
8967e2ab150SChristoph Lameter  * most recent <s, d> pair that moved.  If we get all the way through
8977e2ab150SChristoph Lameter  * the scan of tmp without finding any node that moved, much less
8987e2ab150SChristoph Lameter  * moved to an empty node, then there is nothing left worth migrating.
8997e2ab150SChristoph Lameter  */
9007e2ab150SChristoph Lameter 
9017e2ab150SChristoph Lameter 	tmp = *from_nodes;
9027e2ab150SChristoph Lameter 	while (!nodes_empty(tmp)) {
9037e2ab150SChristoph Lameter 		int s,d;
9047e2ab150SChristoph Lameter 		int source = -1;
9057e2ab150SChristoph Lameter 		int dest = 0;
9067e2ab150SChristoph Lameter 
9077e2ab150SChristoph Lameter 		for_each_node_mask(s, tmp) {
9087e2ab150SChristoph Lameter 			d = node_remap(s, *from_nodes, *to_nodes);
9097e2ab150SChristoph Lameter 			if (s == d)
9107e2ab150SChristoph Lameter 				continue;
9117e2ab150SChristoph Lameter 
9127e2ab150SChristoph Lameter 			source = s;	/* Node moved. Memorize */
9137e2ab150SChristoph Lameter 			dest = d;
9147e2ab150SChristoph Lameter 
9157e2ab150SChristoph Lameter 			/* dest not in remaining from nodes? */
9167e2ab150SChristoph Lameter 			if (!node_isset(dest, tmp))
9177e2ab150SChristoph Lameter 				break;
9187e2ab150SChristoph Lameter 		}
9197e2ab150SChristoph Lameter 		if (source == -1)
9207e2ab150SChristoph Lameter 			break;
9217e2ab150SChristoph Lameter 
9227e2ab150SChristoph Lameter 		node_clear(source, tmp);
9237e2ab150SChristoph Lameter 		err = migrate_to_node(mm, source, dest, flags);
9247e2ab150SChristoph Lameter 		if (err > 0)
9257e2ab150SChristoph Lameter 			busy += err;
9267e2ab150SChristoph Lameter 		if (err < 0)
9277e2ab150SChristoph Lameter 			break;
92839743889SChristoph Lameter 	}
9297b2259b3SChristoph Lameter out:
93039743889SChristoph Lameter 	up_read(&mm->mmap_sem);
9317e2ab150SChristoph Lameter 	if (err < 0)
9327e2ab150SChristoph Lameter 		return err;
9337e2ab150SChristoph Lameter 	return busy;
934b20a3503SChristoph Lameter 
93539743889SChristoph Lameter }
93639743889SChristoph Lameter 
9373ad33b24SLee Schermerhorn /*
9383ad33b24SLee Schermerhorn  * Allocate a new page for page migration based on vma policy.
9393ad33b24SLee Schermerhorn  * Start assuming that page is mapped by vma pointed to by @private.
9403ad33b24SLee Schermerhorn  * Search forward from there, if not.  N.B., this assumes that the
9413ad33b24SLee Schermerhorn  * list of pages handed to migrate_pages()--which is how we get here--
9423ad33b24SLee Schermerhorn  * is in virtual address order.
9433ad33b24SLee Schermerhorn  */
944742755a1SChristoph Lameter static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
94595a402c3SChristoph Lameter {
94695a402c3SChristoph Lameter 	struct vm_area_struct *vma = (struct vm_area_struct *)private;
9473ad33b24SLee Schermerhorn 	unsigned long uninitialized_var(address);
94895a402c3SChristoph Lameter 
9493ad33b24SLee Schermerhorn 	while (vma) {
9503ad33b24SLee Schermerhorn 		address = page_address_in_vma(page, vma);
9513ad33b24SLee Schermerhorn 		if (address != -EFAULT)
9523ad33b24SLee Schermerhorn 			break;
9533ad33b24SLee Schermerhorn 		vma = vma->vm_next;
9543ad33b24SLee Schermerhorn 	}
9553ad33b24SLee Schermerhorn 
9563ad33b24SLee Schermerhorn 	/*
9573ad33b24SLee Schermerhorn 	 * if !vma, alloc_page_vma() will use task or system default policy
9583ad33b24SLee Schermerhorn 	 */
9593ad33b24SLee Schermerhorn 	return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
96095a402c3SChristoph Lameter }
961b20a3503SChristoph Lameter #else
962b20a3503SChristoph Lameter 
963b20a3503SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
964b20a3503SChristoph Lameter 				unsigned long flags)
965b20a3503SChristoph Lameter {
966b20a3503SChristoph Lameter }
967b20a3503SChristoph Lameter 
968b20a3503SChristoph Lameter int do_migrate_pages(struct mm_struct *mm,
969b20a3503SChristoph Lameter 	const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
970b20a3503SChristoph Lameter {
971b20a3503SChristoph Lameter 	return -ENOSYS;
972b20a3503SChristoph Lameter }
97395a402c3SChristoph Lameter 
97469939749SKeith Owens static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
97595a402c3SChristoph Lameter {
97695a402c3SChristoph Lameter 	return NULL;
97795a402c3SChristoph Lameter }
978b20a3503SChristoph Lameter #endif
979b20a3503SChristoph Lameter 
980dbcb0f19SAdrian Bunk static long do_mbind(unsigned long start, unsigned long len,
981028fec41SDavid Rientjes 		     unsigned short mode, unsigned short mode_flags,
982028fec41SDavid Rientjes 		     nodemask_t *nmask, unsigned long flags)
9836ce3c4c0SChristoph Lameter {
9846ce3c4c0SChristoph Lameter 	struct vm_area_struct *vma;
9856ce3c4c0SChristoph Lameter 	struct mm_struct *mm = current->mm;
9866ce3c4c0SChristoph Lameter 	struct mempolicy *new;
9876ce3c4c0SChristoph Lameter 	unsigned long end;
9886ce3c4c0SChristoph Lameter 	int err;
9896ce3c4c0SChristoph Lameter 	LIST_HEAD(pagelist);
9906ce3c4c0SChristoph Lameter 
991a3b51e01SDavid Rientjes 	if (flags & ~(unsigned long)(MPOL_MF_STRICT |
9926ce3c4c0SChristoph Lameter 				     MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
9936ce3c4c0SChristoph Lameter 		return -EINVAL;
99474c00241SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
9956ce3c4c0SChristoph Lameter 		return -EPERM;
9966ce3c4c0SChristoph Lameter 
9976ce3c4c0SChristoph Lameter 	if (start & ~PAGE_MASK)
9986ce3c4c0SChristoph Lameter 		return -EINVAL;
9996ce3c4c0SChristoph Lameter 
10006ce3c4c0SChristoph Lameter 	if (mode == MPOL_DEFAULT)
10016ce3c4c0SChristoph Lameter 		flags &= ~MPOL_MF_STRICT;
10026ce3c4c0SChristoph Lameter 
10036ce3c4c0SChristoph Lameter 	len = (len + PAGE_SIZE - 1) & PAGE_MASK;
10046ce3c4c0SChristoph Lameter 	end = start + len;
10056ce3c4c0SChristoph Lameter 
10066ce3c4c0SChristoph Lameter 	if (end < start)
10076ce3c4c0SChristoph Lameter 		return -EINVAL;
10086ce3c4c0SChristoph Lameter 	if (end == start)
10096ce3c4c0SChristoph Lameter 		return 0;
10106ce3c4c0SChristoph Lameter 
1011028fec41SDavid Rientjes 	new = mpol_new(mode, mode_flags, nmask);
10126ce3c4c0SChristoph Lameter 	if (IS_ERR(new))
10136ce3c4c0SChristoph Lameter 		return PTR_ERR(new);
10146ce3c4c0SChristoph Lameter 
10156ce3c4c0SChristoph Lameter 	/*
10166ce3c4c0SChristoph Lameter 	 * If we are using the default policy then operation
10176ce3c4c0SChristoph Lameter 	 * on discontinuous address spaces is okay after all
10186ce3c4c0SChristoph Lameter 	 */
10196ce3c4c0SChristoph Lameter 	if (!new)
10206ce3c4c0SChristoph Lameter 		flags |= MPOL_MF_DISCONTIG_OK;
10216ce3c4c0SChristoph Lameter 
1022028fec41SDavid Rientjes 	pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
1023028fec41SDavid Rientjes 		 start, start + len, mode, mode_flags,
1024028fec41SDavid Rientjes 		 nmask ? nodes_addr(*nmask)[0] : -1);
10256ce3c4c0SChristoph Lameter 
10260aedadf9SChristoph Lameter 	if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
10270aedadf9SChristoph Lameter 
10280aedadf9SChristoph Lameter 		err = migrate_prep();
10290aedadf9SChristoph Lameter 		if (err)
1030b05ca738SKOSAKI Motohiro 			goto mpol_out;
10310aedadf9SChristoph Lameter 	}
10324bfc4495SKAMEZAWA Hiroyuki 	{
10334bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
10344bfc4495SKAMEZAWA Hiroyuki 		if (scratch) {
10356ce3c4c0SChristoph Lameter 			down_write(&mm->mmap_sem);
103658568d2aSMiao Xie 			task_lock(current);
10374bfc4495SKAMEZAWA Hiroyuki 			err = mpol_set_nodemask(new, nmask, scratch);
103858568d2aSMiao Xie 			task_unlock(current);
10394bfc4495SKAMEZAWA Hiroyuki 			if (err)
104058568d2aSMiao Xie 				up_write(&mm->mmap_sem);
10414bfc4495SKAMEZAWA Hiroyuki 		} else
10424bfc4495SKAMEZAWA Hiroyuki 			err = -ENOMEM;
10434bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
10444bfc4495SKAMEZAWA Hiroyuki 	}
1045b05ca738SKOSAKI Motohiro 	if (err)
1046b05ca738SKOSAKI Motohiro 		goto mpol_out;
1047b05ca738SKOSAKI Motohiro 
10486ce3c4c0SChristoph Lameter 	vma = check_range(mm, start, end, nmask,
10496ce3c4c0SChristoph Lameter 			  flags | MPOL_MF_INVERT, &pagelist);
10506ce3c4c0SChristoph Lameter 
10516ce3c4c0SChristoph Lameter 	err = PTR_ERR(vma);
10526ce3c4c0SChristoph Lameter 	if (!IS_ERR(vma)) {
10536ce3c4c0SChristoph Lameter 		int nr_failed = 0;
10546ce3c4c0SChristoph Lameter 
10556ce3c4c0SChristoph Lameter 		err = mbind_range(vma, start, end, new);
10567e2ab150SChristoph Lameter 
10576ce3c4c0SChristoph Lameter 		if (!list_empty(&pagelist))
105895a402c3SChristoph Lameter 			nr_failed = migrate_pages(&pagelist, new_vma_page,
105995a402c3SChristoph Lameter 						(unsigned long)vma);
10606ce3c4c0SChristoph Lameter 
10616ce3c4c0SChristoph Lameter 		if (!err && nr_failed && (flags & MPOL_MF_STRICT))
10626ce3c4c0SChristoph Lameter 			err = -EIO;
1063ab8a3e14SKOSAKI Motohiro 	} else
1064ab8a3e14SKOSAKI Motohiro 		putback_lru_pages(&pagelist);
1065b20a3503SChristoph Lameter 
10666ce3c4c0SChristoph Lameter 	up_write(&mm->mmap_sem);
1067b05ca738SKOSAKI Motohiro  mpol_out:
1068f0be3d32SLee Schermerhorn 	mpol_put(new);
10696ce3c4c0SChristoph Lameter 	return err;
10706ce3c4c0SChristoph Lameter }
10716ce3c4c0SChristoph Lameter 
107239743889SChristoph Lameter /*
10738bccd85fSChristoph Lameter  * User space interface with variable sized bitmaps for nodelists.
10748bccd85fSChristoph Lameter  */
10758bccd85fSChristoph Lameter 
10768bccd85fSChristoph Lameter /* Copy a node mask from user space. */
107739743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
10788bccd85fSChristoph Lameter 		     unsigned long maxnode)
10798bccd85fSChristoph Lameter {
10808bccd85fSChristoph Lameter 	unsigned long k;
10818bccd85fSChristoph Lameter 	unsigned long nlongs;
10828bccd85fSChristoph Lameter 	unsigned long endmask;
10838bccd85fSChristoph Lameter 
10848bccd85fSChristoph Lameter 	--maxnode;
10858bccd85fSChristoph Lameter 	nodes_clear(*nodes);
10868bccd85fSChristoph Lameter 	if (maxnode == 0 || !nmask)
10878bccd85fSChristoph Lameter 		return 0;
1088a9c930baSAndi Kleen 	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
1089636f13c1SChris Wright 		return -EINVAL;
10908bccd85fSChristoph Lameter 
10918bccd85fSChristoph Lameter 	nlongs = BITS_TO_LONGS(maxnode);
10928bccd85fSChristoph Lameter 	if ((maxnode % BITS_PER_LONG) == 0)
10938bccd85fSChristoph Lameter 		endmask = ~0UL;
10948bccd85fSChristoph Lameter 	else
10958bccd85fSChristoph Lameter 		endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
10968bccd85fSChristoph Lameter 
10978bccd85fSChristoph Lameter 	/* When the user specified more nodes than supported just check
10988bccd85fSChristoph Lameter 	   if the non supported part is all zero. */
10998bccd85fSChristoph Lameter 	if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
11008bccd85fSChristoph Lameter 		if (nlongs > PAGE_SIZE/sizeof(long))
11018bccd85fSChristoph Lameter 			return -EINVAL;
11028bccd85fSChristoph Lameter 		for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
11038bccd85fSChristoph Lameter 			unsigned long t;
11048bccd85fSChristoph Lameter 			if (get_user(t, nmask + k))
11058bccd85fSChristoph Lameter 				return -EFAULT;
11068bccd85fSChristoph Lameter 			if (k == nlongs - 1) {
11078bccd85fSChristoph Lameter 				if (t & endmask)
11088bccd85fSChristoph Lameter 					return -EINVAL;
11098bccd85fSChristoph Lameter 			} else if (t)
11108bccd85fSChristoph Lameter 				return -EINVAL;
11118bccd85fSChristoph Lameter 		}
11128bccd85fSChristoph Lameter 		nlongs = BITS_TO_LONGS(MAX_NUMNODES);
11138bccd85fSChristoph Lameter 		endmask = ~0UL;
11148bccd85fSChristoph Lameter 	}
11158bccd85fSChristoph Lameter 
11168bccd85fSChristoph Lameter 	if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
11178bccd85fSChristoph Lameter 		return -EFAULT;
11188bccd85fSChristoph Lameter 	nodes_addr(*nodes)[nlongs-1] &= endmask;
11198bccd85fSChristoph Lameter 	return 0;
11208bccd85fSChristoph Lameter }
11218bccd85fSChristoph Lameter 
11228bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */
11238bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
11248bccd85fSChristoph Lameter 			      nodemask_t *nodes)
11258bccd85fSChristoph Lameter {
11268bccd85fSChristoph Lameter 	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
11278bccd85fSChristoph Lameter 	const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
11288bccd85fSChristoph Lameter 
11298bccd85fSChristoph Lameter 	if (copy > nbytes) {
11308bccd85fSChristoph Lameter 		if (copy > PAGE_SIZE)
11318bccd85fSChristoph Lameter 			return -EINVAL;
11328bccd85fSChristoph Lameter 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
11338bccd85fSChristoph Lameter 			return -EFAULT;
11348bccd85fSChristoph Lameter 		copy = nbytes;
11358bccd85fSChristoph Lameter 	}
11368bccd85fSChristoph Lameter 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
11378bccd85fSChristoph Lameter }
11388bccd85fSChristoph Lameter 
1139938bb9f5SHeiko Carstens SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
1140938bb9f5SHeiko Carstens 		unsigned long, mode, unsigned long __user *, nmask,
1141938bb9f5SHeiko Carstens 		unsigned long, maxnode, unsigned, flags)
11428bccd85fSChristoph Lameter {
11438bccd85fSChristoph Lameter 	nodemask_t nodes;
11448bccd85fSChristoph Lameter 	int err;
1145028fec41SDavid Rientjes 	unsigned short mode_flags;
11468bccd85fSChristoph Lameter 
1147028fec41SDavid Rientjes 	mode_flags = mode & MPOL_MODE_FLAGS;
1148028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1149a3b51e01SDavid Rientjes 	if (mode >= MPOL_MAX)
1150a3b51e01SDavid Rientjes 		return -EINVAL;
11514c50bc01SDavid Rientjes 	if ((mode_flags & MPOL_F_STATIC_NODES) &&
11524c50bc01SDavid Rientjes 	    (mode_flags & MPOL_F_RELATIVE_NODES))
11534c50bc01SDavid Rientjes 		return -EINVAL;
11548bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
11558bccd85fSChristoph Lameter 	if (err)
11568bccd85fSChristoph Lameter 		return err;
1157028fec41SDavid Rientjes 	return do_mbind(start, len, mode, mode_flags, &nodes, flags);
11588bccd85fSChristoph Lameter }
11598bccd85fSChristoph Lameter 
11608bccd85fSChristoph Lameter /* Set the process memory policy */
1161938bb9f5SHeiko Carstens SYSCALL_DEFINE3(set_mempolicy, int, mode, unsigned long __user *, nmask,
1162938bb9f5SHeiko Carstens 		unsigned long, maxnode)
11638bccd85fSChristoph Lameter {
11648bccd85fSChristoph Lameter 	int err;
11658bccd85fSChristoph Lameter 	nodemask_t nodes;
1166028fec41SDavid Rientjes 	unsigned short flags;
11678bccd85fSChristoph Lameter 
1168028fec41SDavid Rientjes 	flags = mode & MPOL_MODE_FLAGS;
1169028fec41SDavid Rientjes 	mode &= ~MPOL_MODE_FLAGS;
1170028fec41SDavid Rientjes 	if ((unsigned int)mode >= MPOL_MAX)
11718bccd85fSChristoph Lameter 		return -EINVAL;
11724c50bc01SDavid Rientjes 	if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
11734c50bc01SDavid Rientjes 		return -EINVAL;
11748bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
11758bccd85fSChristoph Lameter 	if (err)
11768bccd85fSChristoph Lameter 		return err;
1177028fec41SDavid Rientjes 	return do_set_mempolicy(mode, flags, &nodes);
11788bccd85fSChristoph Lameter }
11798bccd85fSChristoph Lameter 
1180938bb9f5SHeiko Carstens SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
1181938bb9f5SHeiko Carstens 		const unsigned long __user *, old_nodes,
1182938bb9f5SHeiko Carstens 		const unsigned long __user *, new_nodes)
118339743889SChristoph Lameter {
1184c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
118539743889SChristoph Lameter 	struct mm_struct *mm;
118639743889SChristoph Lameter 	struct task_struct *task;
118739743889SChristoph Lameter 	nodemask_t old;
118839743889SChristoph Lameter 	nodemask_t new;
118939743889SChristoph Lameter 	nodemask_t task_nodes;
119039743889SChristoph Lameter 	int err;
119139743889SChristoph Lameter 
119239743889SChristoph Lameter 	err = get_nodes(&old, old_nodes, maxnode);
119339743889SChristoph Lameter 	if (err)
119439743889SChristoph Lameter 		return err;
119539743889SChristoph Lameter 
119639743889SChristoph Lameter 	err = get_nodes(&new, new_nodes, maxnode);
119739743889SChristoph Lameter 	if (err)
119839743889SChristoph Lameter 		return err;
119939743889SChristoph Lameter 
120039743889SChristoph Lameter 	/* Find the mm_struct */
120139743889SChristoph Lameter 	read_lock(&tasklist_lock);
1202228ebcbeSPavel Emelyanov 	task = pid ? find_task_by_vpid(pid) : current;
120339743889SChristoph Lameter 	if (!task) {
120439743889SChristoph Lameter 		read_unlock(&tasklist_lock);
120539743889SChristoph Lameter 		return -ESRCH;
120639743889SChristoph Lameter 	}
120739743889SChristoph Lameter 	mm = get_task_mm(task);
120839743889SChristoph Lameter 	read_unlock(&tasklist_lock);
120939743889SChristoph Lameter 
121039743889SChristoph Lameter 	if (!mm)
121139743889SChristoph Lameter 		return -EINVAL;
121239743889SChristoph Lameter 
121339743889SChristoph Lameter 	/*
121439743889SChristoph Lameter 	 * Check if this process has the right to modify the specified
121539743889SChristoph Lameter 	 * process. The right exists if the process has administrative
12167f927fccSAlexey Dobriyan 	 * capabilities, superuser privileges or the same
121739743889SChristoph Lameter 	 * userid as the target process.
121839743889SChristoph Lameter 	 */
1219c69e8d9cSDavid Howells 	rcu_read_lock();
1220c69e8d9cSDavid Howells 	tcred = __task_cred(task);
1221b6dff3ecSDavid Howells 	if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
1222b6dff3ecSDavid Howells 	    cred->uid  != tcred->suid && cred->uid  != tcred->uid &&
122374c00241SChristoph Lameter 	    !capable(CAP_SYS_NICE)) {
1224c69e8d9cSDavid Howells 		rcu_read_unlock();
122539743889SChristoph Lameter 		err = -EPERM;
122639743889SChristoph Lameter 		goto out;
122739743889SChristoph Lameter 	}
1228c69e8d9cSDavid Howells 	rcu_read_unlock();
122939743889SChristoph Lameter 
123039743889SChristoph Lameter 	task_nodes = cpuset_mems_allowed(task);
123139743889SChristoph Lameter 	/* Is the user allowed to access the target nodes? */
123274c00241SChristoph Lameter 	if (!nodes_subset(new, task_nodes) && !capable(CAP_SYS_NICE)) {
123339743889SChristoph Lameter 		err = -EPERM;
123439743889SChristoph Lameter 		goto out;
123539743889SChristoph Lameter 	}
123639743889SChristoph Lameter 
123737b07e41SLee Schermerhorn 	if (!nodes_subset(new, node_states[N_HIGH_MEMORY])) {
12383b42d28bSChristoph Lameter 		err = -EINVAL;
12393b42d28bSChristoph Lameter 		goto out;
12403b42d28bSChristoph Lameter 	}
12413b42d28bSChristoph Lameter 
124286c3a764SDavid Quigley 	err = security_task_movememory(task);
124386c3a764SDavid Quigley 	if (err)
124486c3a764SDavid Quigley 		goto out;
124586c3a764SDavid Quigley 
1246511030bcSChristoph Lameter 	err = do_migrate_pages(mm, &old, &new,
124774c00241SChristoph Lameter 		capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
124839743889SChristoph Lameter out:
124939743889SChristoph Lameter 	mmput(mm);
125039743889SChristoph Lameter 	return err;
125139743889SChristoph Lameter }
125239743889SChristoph Lameter 
125339743889SChristoph Lameter 
12548bccd85fSChristoph Lameter /* Retrieve NUMA policy */
1255938bb9f5SHeiko Carstens SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
1256938bb9f5SHeiko Carstens 		unsigned long __user *, nmask, unsigned long, maxnode,
1257938bb9f5SHeiko Carstens 		unsigned long, addr, unsigned long, flags)
12588bccd85fSChristoph Lameter {
1259dbcb0f19SAdrian Bunk 	int err;
1260dbcb0f19SAdrian Bunk 	int uninitialized_var(pval);
12618bccd85fSChristoph Lameter 	nodemask_t nodes;
12628bccd85fSChristoph Lameter 
12638bccd85fSChristoph Lameter 	if (nmask != NULL && maxnode < MAX_NUMNODES)
12648bccd85fSChristoph Lameter 		return -EINVAL;
12658bccd85fSChristoph Lameter 
12668bccd85fSChristoph Lameter 	err = do_get_mempolicy(&pval, &nodes, addr, flags);
12678bccd85fSChristoph Lameter 
12688bccd85fSChristoph Lameter 	if (err)
12698bccd85fSChristoph Lameter 		return err;
12708bccd85fSChristoph Lameter 
12718bccd85fSChristoph Lameter 	if (policy && put_user(pval, policy))
12728bccd85fSChristoph Lameter 		return -EFAULT;
12738bccd85fSChristoph Lameter 
12748bccd85fSChristoph Lameter 	if (nmask)
12758bccd85fSChristoph Lameter 		err = copy_nodes_to_user(nmask, maxnode, &nodes);
12768bccd85fSChristoph Lameter 
12778bccd85fSChristoph Lameter 	return err;
12788bccd85fSChristoph Lameter }
12798bccd85fSChristoph Lameter 
12801da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy,
12831da177e4SLinus Torvalds 				     compat_ulong_t __user *nmask,
12841da177e4SLinus Torvalds 				     compat_ulong_t maxnode,
12851da177e4SLinus Torvalds 				     compat_ulong_t addr, compat_ulong_t flags)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds 	long err;
12881da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
12891da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
12901da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
12931da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	if (nmask)
12961da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	if (!err && nmask) {
13011da177e4SLinus Torvalds 		err = copy_from_user(bm, nm, alloc_size);
13021da177e4SLinus Torvalds 		/* ensure entire bitmap is zeroed */
13031da177e4SLinus Torvalds 		err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
13041da177e4SLinus Torvalds 		err |= compat_put_bitmap(nmask, bm, nr_bits);
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	return err;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
13111da177e4SLinus Torvalds 				     compat_ulong_t maxnode)
13121da177e4SLinus Torvalds {
13131da177e4SLinus Torvalds 	long err = 0;
13141da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
13151da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
13161da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
13191da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	if (nmask) {
13221da177e4SLinus Torvalds 		err = compat_get_bitmap(bm, nmask, nr_bits);
13231da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
13241da177e4SLinus Torvalds 		err |= copy_to_user(nm, bm, alloc_size);
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	if (err)
13281da177e4SLinus Torvalds 		return -EFAULT;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	return sys_set_mempolicy(mode, nm, nr_bits+1);
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
13341da177e4SLinus Torvalds 			     compat_ulong_t mode, compat_ulong_t __user *nmask,
13351da177e4SLinus Torvalds 			     compat_ulong_t maxnode, compat_ulong_t flags)
13361da177e4SLinus Torvalds {
13371da177e4SLinus Torvalds 	long err = 0;
13381da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
13391da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
1340dfcd3c0dSAndi Kleen 	nodemask_t bm;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
13431da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	if (nmask) {
1346dfcd3c0dSAndi Kleen 		err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
13471da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
1348dfcd3c0dSAndi Kleen 		err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
13491da177e4SLinus Torvalds 	}
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	if (err)
13521da177e4SLinus Torvalds 		return -EFAULT;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds #endif
13581da177e4SLinus Torvalds 
1359480eccf9SLee Schermerhorn /*
1360480eccf9SLee Schermerhorn  * get_vma_policy(@task, @vma, @addr)
1361480eccf9SLee Schermerhorn  * @task - task for fallback if vma policy == default
1362480eccf9SLee Schermerhorn  * @vma   - virtual memory area whose policy is sought
1363480eccf9SLee Schermerhorn  * @addr  - address in @vma for shared policy lookup
1364480eccf9SLee Schermerhorn  *
1365480eccf9SLee Schermerhorn  * Returns effective policy for a VMA at specified address.
1366480eccf9SLee Schermerhorn  * Falls back to @task or system default policy, as necessary.
136752cd3b07SLee Schermerhorn  * Current or other task's task mempolicy and non-shared vma policies
136852cd3b07SLee Schermerhorn  * are protected by the task's mmap_sem, which must be held for read by
136952cd3b07SLee Schermerhorn  * the caller.
137052cd3b07SLee Schermerhorn  * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
137152cd3b07SLee Schermerhorn  * count--added by the get_policy() vm_op, as appropriate--to protect against
137252cd3b07SLee Schermerhorn  * freeing by another task.  It is the caller's responsibility to free the
137352cd3b07SLee Schermerhorn  * extra reference for shared policies.
1374480eccf9SLee Schermerhorn  */
137548fce342SChristoph Lameter static struct mempolicy *get_vma_policy(struct task_struct *task,
137648fce342SChristoph Lameter 		struct vm_area_struct *vma, unsigned long addr)
13771da177e4SLinus Torvalds {
13786e21c8f1SChristoph Lameter 	struct mempolicy *pol = task->mempolicy;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	if (vma) {
1381480eccf9SLee Schermerhorn 		if (vma->vm_ops && vma->vm_ops->get_policy) {
1382ae4d8c16SLee Schermerhorn 			struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
1383ae4d8c16SLee Schermerhorn 									addr);
1384ae4d8c16SLee Schermerhorn 			if (vpol)
1385ae4d8c16SLee Schermerhorn 				pol = vpol;
1386bea904d5SLee Schermerhorn 		} else if (vma->vm_policy)
13871da177e4SLinus Torvalds 			pol = vma->vm_policy;
13881da177e4SLinus Torvalds 	}
13891da177e4SLinus Torvalds 	if (!pol)
13901da177e4SLinus Torvalds 		pol = &default_policy;
13911da177e4SLinus Torvalds 	return pol;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
139452cd3b07SLee Schermerhorn /*
139552cd3b07SLee Schermerhorn  * Return a nodemask representing a mempolicy for filtering nodes for
139652cd3b07SLee Schermerhorn  * page allocation
139752cd3b07SLee Schermerhorn  */
139852cd3b07SLee Schermerhorn static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
139919770b32SMel Gorman {
140019770b32SMel Gorman 	/* Lower zones don't get a nodemask applied for MPOL_BIND */
140145c4745aSLee Schermerhorn 	if (unlikely(policy->mode == MPOL_BIND) &&
140219770b32SMel Gorman 			gfp_zone(gfp) >= policy_zone &&
140319770b32SMel Gorman 			cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
140419770b32SMel Gorman 		return &policy->v.nodes;
140519770b32SMel Gorman 
140619770b32SMel Gorman 	return NULL;
140719770b32SMel Gorman }
140819770b32SMel Gorman 
140952cd3b07SLee Schermerhorn /* Return a zonelist indicated by gfp for node representing a mempolicy */
141052cd3b07SLee Schermerhorn static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy)
14111da177e4SLinus Torvalds {
1412fc36b8d3SLee Schermerhorn 	int nd = numa_node_id();
14131da177e4SLinus Torvalds 
141445c4745aSLee Schermerhorn 	switch (policy->mode) {
14151da177e4SLinus Torvalds 	case MPOL_PREFERRED:
1416fc36b8d3SLee Schermerhorn 		if (!(policy->flags & MPOL_F_LOCAL))
14171da177e4SLinus Torvalds 			nd = policy->v.preferred_node;
14181da177e4SLinus Torvalds 		break;
14191da177e4SLinus Torvalds 	case MPOL_BIND:
142019770b32SMel Gorman 		/*
142152cd3b07SLee Schermerhorn 		 * Normally, MPOL_BIND allocations are node-local within the
142252cd3b07SLee Schermerhorn 		 * allowed nodemask.  However, if __GFP_THISNODE is set and the
142352cd3b07SLee Schermerhorn 		 * current node is part of the mask, we use the zonelist for
142452cd3b07SLee Schermerhorn 		 * the first node in the mask instead.
142519770b32SMel Gorman 		 */
142619770b32SMel Gorman 		if (unlikely(gfp & __GFP_THISNODE) &&
142719770b32SMel Gorman 				unlikely(!node_isset(nd, policy->v.nodes)))
142819770b32SMel Gorman 			nd = first_node(policy->v.nodes);
142919770b32SMel Gorman 		break;
14301da177e4SLinus Torvalds 	case MPOL_INTERLEAVE: /* should not happen */
14311da177e4SLinus Torvalds 		break;
14321da177e4SLinus Torvalds 	default:
14331da177e4SLinus Torvalds 		BUG();
14341da177e4SLinus Torvalds 	}
14350e88460dSMel Gorman 	return node_zonelist(nd, gfp);
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds /* Do dynamic interleaving for a process */
14391da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	unsigned nid, next;
14421da177e4SLinus Torvalds 	struct task_struct *me = current;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	nid = me->il_next;
1445dfcd3c0dSAndi Kleen 	next = next_node(nid, policy->v.nodes);
14461da177e4SLinus Torvalds 	if (next >= MAX_NUMNODES)
1447dfcd3c0dSAndi Kleen 		next = first_node(policy->v.nodes);
1448f5b087b5SDavid Rientjes 	if (next < MAX_NUMNODES)
14491da177e4SLinus Torvalds 		me->il_next = next;
14501da177e4SLinus Torvalds 	return nid;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
1453dc85da15SChristoph Lameter /*
1454dc85da15SChristoph Lameter  * Depending on the memory policy provide a node from which to allocate the
1455dc85da15SChristoph Lameter  * next slab entry.
145652cd3b07SLee Schermerhorn  * @policy must be protected by freeing by the caller.  If @policy is
145752cd3b07SLee Schermerhorn  * the current task's mempolicy, this protection is implicit, as only the
145852cd3b07SLee Schermerhorn  * task can change it's policy.  The system default policy requires no
145952cd3b07SLee Schermerhorn  * such protection.
1460dc85da15SChristoph Lameter  */
1461dc85da15SChristoph Lameter unsigned slab_node(struct mempolicy *policy)
1462dc85da15SChristoph Lameter {
1463fc36b8d3SLee Schermerhorn 	if (!policy || policy->flags & MPOL_F_LOCAL)
1464bea904d5SLee Schermerhorn 		return numa_node_id();
1465765c4507SChristoph Lameter 
1466bea904d5SLee Schermerhorn 	switch (policy->mode) {
1467bea904d5SLee Schermerhorn 	case MPOL_PREFERRED:
1468fc36b8d3SLee Schermerhorn 		/*
1469fc36b8d3SLee Schermerhorn 		 * handled MPOL_F_LOCAL above
1470fc36b8d3SLee Schermerhorn 		 */
1471bea904d5SLee Schermerhorn 		return policy->v.preferred_node;
1472bea904d5SLee Schermerhorn 
1473dc85da15SChristoph Lameter 	case MPOL_INTERLEAVE:
1474dc85da15SChristoph Lameter 		return interleave_nodes(policy);
1475dc85da15SChristoph Lameter 
1476dd1a239fSMel Gorman 	case MPOL_BIND: {
1477dc85da15SChristoph Lameter 		/*
1478dc85da15SChristoph Lameter 		 * Follow bind policy behavior and start allocation at the
1479dc85da15SChristoph Lameter 		 * first node.
1480dc85da15SChristoph Lameter 		 */
148119770b32SMel Gorman 		struct zonelist *zonelist;
148219770b32SMel Gorman 		struct zone *zone;
148319770b32SMel Gorman 		enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
148419770b32SMel Gorman 		zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0];
148519770b32SMel Gorman 		(void)first_zones_zonelist(zonelist, highest_zoneidx,
148619770b32SMel Gorman 							&policy->v.nodes,
148719770b32SMel Gorman 							&zone);
148819770b32SMel Gorman 		return zone->node;
1489dd1a239fSMel Gorman 	}
1490dc85da15SChristoph Lameter 
1491dc85da15SChristoph Lameter 	default:
1492bea904d5SLee Schermerhorn 		BUG();
1493dc85da15SChristoph Lameter 	}
1494dc85da15SChristoph Lameter }
1495dc85da15SChristoph Lameter 
14961da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */
14971da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol,
14981da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long off)
14991da177e4SLinus Torvalds {
1500dfcd3c0dSAndi Kleen 	unsigned nnodes = nodes_weight(pol->v.nodes);
1501f5b087b5SDavid Rientjes 	unsigned target;
15021da177e4SLinus Torvalds 	int c;
15031da177e4SLinus Torvalds 	int nid = -1;
15041da177e4SLinus Torvalds 
1505f5b087b5SDavid Rientjes 	if (!nnodes)
1506f5b087b5SDavid Rientjes 		return numa_node_id();
1507f5b087b5SDavid Rientjes 	target = (unsigned int)off % nnodes;
15081da177e4SLinus Torvalds 	c = 0;
15091da177e4SLinus Torvalds 	do {
1510dfcd3c0dSAndi Kleen 		nid = next_node(nid, pol->v.nodes);
15111da177e4SLinus Torvalds 		c++;
15121da177e4SLinus Torvalds 	} while (c <= target);
15131da177e4SLinus Torvalds 	return nid;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15165da7ca86SChristoph Lameter /* Determine a node number for interleave */
15175da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol,
15185da7ca86SChristoph Lameter 		 struct vm_area_struct *vma, unsigned long addr, int shift)
15195da7ca86SChristoph Lameter {
15205da7ca86SChristoph Lameter 	if (vma) {
15215da7ca86SChristoph Lameter 		unsigned long off;
15225da7ca86SChristoph Lameter 
15233b98b087SNishanth Aravamudan 		/*
15243b98b087SNishanth Aravamudan 		 * for small pages, there is no difference between
15253b98b087SNishanth Aravamudan 		 * shift and PAGE_SHIFT, so the bit-shift is safe.
15263b98b087SNishanth Aravamudan 		 * for huge pages, since vm_pgoff is in units of small
15273b98b087SNishanth Aravamudan 		 * pages, we need to shift off the always 0 bits to get
15283b98b087SNishanth Aravamudan 		 * a useful offset.
15293b98b087SNishanth Aravamudan 		 */
15303b98b087SNishanth Aravamudan 		BUG_ON(shift < PAGE_SHIFT);
15313b98b087SNishanth Aravamudan 		off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
15325da7ca86SChristoph Lameter 		off += (addr - vma->vm_start) >> shift;
15335da7ca86SChristoph Lameter 		return offset_il_node(pol, vma, off);
15345da7ca86SChristoph Lameter 	} else
15355da7ca86SChristoph Lameter 		return interleave_nodes(pol);
15365da7ca86SChristoph Lameter }
15375da7ca86SChristoph Lameter 
153800ac59adSChen, Kenneth W #ifdef CONFIG_HUGETLBFS
1539480eccf9SLee Schermerhorn /*
1540480eccf9SLee Schermerhorn  * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
1541480eccf9SLee Schermerhorn  * @vma = virtual memory area whose policy is sought
1542480eccf9SLee Schermerhorn  * @addr = address in @vma for shared policy lookup and interleave policy
1543480eccf9SLee Schermerhorn  * @gfp_flags = for requested zone
154419770b32SMel Gorman  * @mpol = pointer to mempolicy pointer for reference counted mempolicy
154519770b32SMel Gorman  * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask
1546480eccf9SLee Schermerhorn  *
154752cd3b07SLee Schermerhorn  * Returns a zonelist suitable for a huge page allocation and a pointer
154852cd3b07SLee Schermerhorn  * to the struct mempolicy for conditional unref after allocation.
154952cd3b07SLee Schermerhorn  * If the effective policy is 'BIND, returns a pointer to the mempolicy's
155052cd3b07SLee Schermerhorn  * @nodemask for filtering the zonelist.
1551480eccf9SLee Schermerhorn  */
1552396faf03SMel Gorman struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
155319770b32SMel Gorman 				gfp_t gfp_flags, struct mempolicy **mpol,
155419770b32SMel Gorman 				nodemask_t **nodemask)
15555da7ca86SChristoph Lameter {
1556480eccf9SLee Schermerhorn 	struct zonelist *zl;
15575da7ca86SChristoph Lameter 
155852cd3b07SLee Schermerhorn 	*mpol = get_vma_policy(current, vma, addr);
155919770b32SMel Gorman 	*nodemask = NULL;	/* assume !MPOL_BIND */
15605da7ca86SChristoph Lameter 
156152cd3b07SLee Schermerhorn 	if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
156252cd3b07SLee Schermerhorn 		zl = node_zonelist(interleave_nid(*mpol, vma, addr,
1563a5516438SAndi Kleen 				huge_page_shift(hstate_vma(vma))), gfp_flags);
156452cd3b07SLee Schermerhorn 	} else {
156552cd3b07SLee Schermerhorn 		zl = policy_zonelist(gfp_flags, *mpol);
156652cd3b07SLee Schermerhorn 		if ((*mpol)->mode == MPOL_BIND)
156752cd3b07SLee Schermerhorn 			*nodemask = &(*mpol)->v.nodes;
1568480eccf9SLee Schermerhorn 	}
1569480eccf9SLee Schermerhorn 	return zl;
15705da7ca86SChristoph Lameter }
1571*06808b08SLee Schermerhorn 
1572*06808b08SLee Schermerhorn /*
1573*06808b08SLee Schermerhorn  * init_nodemask_of_mempolicy
1574*06808b08SLee Schermerhorn  *
1575*06808b08SLee Schermerhorn  * If the current task's mempolicy is "default" [NULL], return 'false'
1576*06808b08SLee Schermerhorn  * to indicate default policy.  Otherwise, extract the policy nodemask
1577*06808b08SLee Schermerhorn  * for 'bind' or 'interleave' policy into the argument nodemask, or
1578*06808b08SLee Schermerhorn  * initialize the argument nodemask to contain the single node for
1579*06808b08SLee Schermerhorn  * 'preferred' or 'local' policy and return 'true' to indicate presence
1580*06808b08SLee Schermerhorn  * of non-default mempolicy.
1581*06808b08SLee Schermerhorn  *
1582*06808b08SLee Schermerhorn  * We don't bother with reference counting the mempolicy [mpol_get/put]
1583*06808b08SLee Schermerhorn  * because the current task is examining it's own mempolicy and a task's
1584*06808b08SLee Schermerhorn  * mempolicy is only ever changed by the task itself.
1585*06808b08SLee Schermerhorn  *
1586*06808b08SLee Schermerhorn  * N.B., it is the caller's responsibility to free a returned nodemask.
1587*06808b08SLee Schermerhorn  */
1588*06808b08SLee Schermerhorn bool init_nodemask_of_mempolicy(nodemask_t *mask)
1589*06808b08SLee Schermerhorn {
1590*06808b08SLee Schermerhorn 	struct mempolicy *mempolicy;
1591*06808b08SLee Schermerhorn 	int nid;
1592*06808b08SLee Schermerhorn 
1593*06808b08SLee Schermerhorn 	if (!(mask && current->mempolicy))
1594*06808b08SLee Schermerhorn 		return false;
1595*06808b08SLee Schermerhorn 
1596*06808b08SLee Schermerhorn 	mempolicy = current->mempolicy;
1597*06808b08SLee Schermerhorn 	switch (mempolicy->mode) {
1598*06808b08SLee Schermerhorn 	case MPOL_PREFERRED:
1599*06808b08SLee Schermerhorn 		if (mempolicy->flags & MPOL_F_LOCAL)
1600*06808b08SLee Schermerhorn 			nid = numa_node_id();
1601*06808b08SLee Schermerhorn 		else
1602*06808b08SLee Schermerhorn 			nid = mempolicy->v.preferred_node;
1603*06808b08SLee Schermerhorn 		init_nodemask_of_node(mask, nid);
1604*06808b08SLee Schermerhorn 		break;
1605*06808b08SLee Schermerhorn 
1606*06808b08SLee Schermerhorn 	case MPOL_BIND:
1607*06808b08SLee Schermerhorn 		/* Fall through */
1608*06808b08SLee Schermerhorn 	case MPOL_INTERLEAVE:
1609*06808b08SLee Schermerhorn 		*mask =  mempolicy->v.nodes;
1610*06808b08SLee Schermerhorn 		break;
1611*06808b08SLee Schermerhorn 
1612*06808b08SLee Schermerhorn 	default:
1613*06808b08SLee Schermerhorn 		BUG();
1614*06808b08SLee Schermerhorn 	}
1615*06808b08SLee Schermerhorn 
1616*06808b08SLee Schermerhorn 	return true;
1617*06808b08SLee Schermerhorn }
161800ac59adSChen, Kenneth W #endif
16195da7ca86SChristoph Lameter 
16201da177e4SLinus Torvalds /* Allocate a page in interleaved policy.
16211da177e4SLinus Torvalds    Own path because it needs to do special accounting. */
1622662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1623662f3a0bSAndi Kleen 					unsigned nid)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	struct zonelist *zl;
16261da177e4SLinus Torvalds 	struct page *page;
16271da177e4SLinus Torvalds 
16280e88460dSMel Gorman 	zl = node_zonelist(nid, gfp);
16291da177e4SLinus Torvalds 	page = __alloc_pages(gfp, order, zl);
1630dd1a239fSMel Gorman 	if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
1631ca889e6cSChristoph Lameter 		inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
16321da177e4SLinus Torvalds 	return page;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds /**
16361da177e4SLinus Torvalds  * 	alloc_page_vma	- Allocate a page for a VMA.
16371da177e4SLinus Torvalds  *
16381da177e4SLinus Torvalds  * 	@gfp:
16391da177e4SLinus Torvalds  *      %GFP_USER    user allocation.
16401da177e4SLinus Torvalds  *      %GFP_KERNEL  kernel allocations,
16411da177e4SLinus Torvalds  *      %GFP_HIGHMEM highmem/user allocations,
16421da177e4SLinus Torvalds  *      %GFP_FS      allocation should not call back into a file system.
16431da177e4SLinus Torvalds  *      %GFP_ATOMIC  don't sleep.
16441da177e4SLinus Torvalds  *
16451da177e4SLinus Torvalds  * 	@vma:  Pointer to VMA or NULL if not available.
16461da177e4SLinus Torvalds  *	@addr: Virtual Address of the allocation. Must be inside the VMA.
16471da177e4SLinus Torvalds  *
16481da177e4SLinus Torvalds  * 	This function allocates a page from the kernel page pool and applies
16491da177e4SLinus Torvalds  *	a NUMA policy associated with the VMA or the current process.
16501da177e4SLinus Torvalds  *	When VMA is not NULL caller must hold down_read on the mmap_sem of the
16511da177e4SLinus Torvalds  *	mm_struct of the VMA to prevent it from going away. Should be used for
16521da177e4SLinus Torvalds  *	all allocations for pages that will be mapped into
16531da177e4SLinus Torvalds  * 	user space. Returns NULL when no page can be allocated.
16541da177e4SLinus Torvalds  *
16551da177e4SLinus Torvalds  *	Should be called with the mm_sem of the vma hold.
16561da177e4SLinus Torvalds  */
16571da177e4SLinus Torvalds struct page *
1658dd0fc66fSAl Viro alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr)
16591da177e4SLinus Torvalds {
16606e21c8f1SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
1661480eccf9SLee Schermerhorn 	struct zonelist *zl;
16621da177e4SLinus Torvalds 
166345c4745aSLee Schermerhorn 	if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
16641da177e4SLinus Torvalds 		unsigned nid;
16655da7ca86SChristoph Lameter 
16665da7ca86SChristoph Lameter 		nid = interleave_nid(pol, vma, addr, PAGE_SHIFT);
166752cd3b07SLee Schermerhorn 		mpol_cond_put(pol);
16681da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, 0, nid);
16691da177e4SLinus Torvalds 	}
167052cd3b07SLee Schermerhorn 	zl = policy_zonelist(gfp, pol);
167152cd3b07SLee Schermerhorn 	if (unlikely(mpol_needs_cond_ref(pol))) {
1672480eccf9SLee Schermerhorn 		/*
167352cd3b07SLee Schermerhorn 		 * slow path: ref counted shared policy
1674480eccf9SLee Schermerhorn 		 */
167519770b32SMel Gorman 		struct page *page =  __alloc_pages_nodemask(gfp, 0,
167652cd3b07SLee Schermerhorn 						zl, policy_nodemask(gfp, pol));
1677f0be3d32SLee Schermerhorn 		__mpol_put(pol);
1678480eccf9SLee Schermerhorn 		return page;
1679480eccf9SLee Schermerhorn 	}
1680480eccf9SLee Schermerhorn 	/*
1681480eccf9SLee Schermerhorn 	 * fast path:  default or task policy
1682480eccf9SLee Schermerhorn 	 */
168352cd3b07SLee Schermerhorn 	return __alloc_pages_nodemask(gfp, 0, zl, policy_nodemask(gfp, pol));
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds /**
16871da177e4SLinus Torvalds  * 	alloc_pages_current - Allocate pages.
16881da177e4SLinus Torvalds  *
16891da177e4SLinus Torvalds  *	@gfp:
16901da177e4SLinus Torvalds  *		%GFP_USER   user allocation,
16911da177e4SLinus Torvalds  *      	%GFP_KERNEL kernel allocation,
16921da177e4SLinus Torvalds  *      	%GFP_HIGHMEM highmem allocation,
16931da177e4SLinus Torvalds  *      	%GFP_FS     don't call back into a file system.
16941da177e4SLinus Torvalds  *      	%GFP_ATOMIC don't sleep.
16951da177e4SLinus Torvalds  *	@order: Power of two of allocation size in pages. 0 is a single page.
16961da177e4SLinus Torvalds  *
16971da177e4SLinus Torvalds  *	Allocate a page from the kernel page pool.  When not in
16981da177e4SLinus Torvalds  *	interrupt context and apply the current process NUMA policy.
16991da177e4SLinus Torvalds  *	Returns NULL when no page can be allocated.
17001da177e4SLinus Torvalds  *
1701cf2a473cSPaul Jackson  *	Don't call cpuset_update_task_memory_state() unless
17021da177e4SLinus Torvalds  *	1) it's ok to take cpuset_sem (can WAIT), and
17031da177e4SLinus Torvalds  *	2) allocating for current task (not interrupt).
17041da177e4SLinus Torvalds  */
1705dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
17081da177e4SLinus Torvalds 
17099b819d20SChristoph Lameter 	if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
17101da177e4SLinus Torvalds 		pol = &default_policy;
171152cd3b07SLee Schermerhorn 
171252cd3b07SLee Schermerhorn 	/*
171352cd3b07SLee Schermerhorn 	 * No reference counting needed for current->mempolicy
171452cd3b07SLee Schermerhorn 	 * nor system default_policy
171552cd3b07SLee Schermerhorn 	 */
171645c4745aSLee Schermerhorn 	if (pol->mode == MPOL_INTERLEAVE)
17171da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, order, interleave_nodes(pol));
171819770b32SMel Gorman 	return __alloc_pages_nodemask(gfp, order,
171952cd3b07SLee Schermerhorn 			policy_zonelist(gfp, pol), policy_nodemask(gfp, pol));
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current);
17221da177e4SLinus Torvalds 
17234225399aSPaul Jackson /*
1724846a16bfSLee Schermerhorn  * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
17254225399aSPaul Jackson  * rebinds the mempolicy its copying by calling mpol_rebind_policy()
17264225399aSPaul Jackson  * with the mems_allowed returned by cpuset_mems_allowed().  This
17274225399aSPaul Jackson  * keeps mempolicies cpuset relative after its cpuset moves.  See
17284225399aSPaul Jackson  * further kernel/cpuset.c update_nodemask().
17294225399aSPaul Jackson  */
17304225399aSPaul Jackson 
1731846a16bfSLee Schermerhorn /* Slow path of a mempolicy duplicate */
1732846a16bfSLee Schermerhorn struct mempolicy *__mpol_dup(struct mempolicy *old)
17331da177e4SLinus Torvalds {
17341da177e4SLinus Torvalds 	struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	if (!new)
17371da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
17384225399aSPaul Jackson 	if (current_cpuset_is_being_rebound()) {
17394225399aSPaul Jackson 		nodemask_t mems = cpuset_mems_allowed(current);
17404225399aSPaul Jackson 		mpol_rebind_policy(old, &mems);
17414225399aSPaul Jackson 	}
17421da177e4SLinus Torvalds 	*new = *old;
17431da177e4SLinus Torvalds 	atomic_set(&new->refcnt, 1);
17441da177e4SLinus Torvalds 	return new;
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
174752cd3b07SLee Schermerhorn /*
174852cd3b07SLee Schermerhorn  * If *frompol needs [has] an extra ref, copy *frompol to *tompol ,
174952cd3b07SLee Schermerhorn  * eliminate the * MPOL_F_* flags that require conditional ref and
175052cd3b07SLee Schermerhorn  * [NOTE!!!] drop the extra ref.  Not safe to reference *frompol directly
175152cd3b07SLee Schermerhorn  * after return.  Use the returned value.
175252cd3b07SLee Schermerhorn  *
175352cd3b07SLee Schermerhorn  * Allows use of a mempolicy for, e.g., multiple allocations with a single
175452cd3b07SLee Schermerhorn  * policy lookup, even if the policy needs/has extra ref on lookup.
175552cd3b07SLee Schermerhorn  * shmem_readahead needs this.
175652cd3b07SLee Schermerhorn  */
175752cd3b07SLee Schermerhorn struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
175852cd3b07SLee Schermerhorn 						struct mempolicy *frompol)
175952cd3b07SLee Schermerhorn {
176052cd3b07SLee Schermerhorn 	if (!mpol_needs_cond_ref(frompol))
176152cd3b07SLee Schermerhorn 		return frompol;
176252cd3b07SLee Schermerhorn 
176352cd3b07SLee Schermerhorn 	*tompol = *frompol;
176452cd3b07SLee Schermerhorn 	tompol->flags &= ~MPOL_F_SHARED;	/* copy doesn't need unref */
176552cd3b07SLee Schermerhorn 	__mpol_put(frompol);
176652cd3b07SLee Schermerhorn 	return tompol;
176752cd3b07SLee Schermerhorn }
176852cd3b07SLee Schermerhorn 
1769f5b087b5SDavid Rientjes static int mpol_match_intent(const struct mempolicy *a,
1770f5b087b5SDavid Rientjes 			     const struct mempolicy *b)
1771f5b087b5SDavid Rientjes {
1772f5b087b5SDavid Rientjes 	if (a->flags != b->flags)
1773f5b087b5SDavid Rientjes 		return 0;
1774f5b087b5SDavid Rientjes 	if (!mpol_store_user_nodemask(a))
1775f5b087b5SDavid Rientjes 		return 1;
1776f5b087b5SDavid Rientjes 	return nodes_equal(a->w.user_nodemask, b->w.user_nodemask);
1777f5b087b5SDavid Rientjes }
1778f5b087b5SDavid Rientjes 
17791da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */
17801da177e4SLinus Torvalds int __mpol_equal(struct mempolicy *a, struct mempolicy *b)
17811da177e4SLinus Torvalds {
17821da177e4SLinus Torvalds 	if (!a || !b)
17831da177e4SLinus Torvalds 		return 0;
178445c4745aSLee Schermerhorn 	if (a->mode != b->mode)
17851da177e4SLinus Torvalds 		return 0;
178645c4745aSLee Schermerhorn 	if (a->mode != MPOL_DEFAULT && !mpol_match_intent(a, b))
1787f5b087b5SDavid Rientjes 		return 0;
178845c4745aSLee Schermerhorn 	switch (a->mode) {
178919770b32SMel Gorman 	case MPOL_BIND:
179019770b32SMel Gorman 		/* Fall through */
17911da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
1792dfcd3c0dSAndi Kleen 		return nodes_equal(a->v.nodes, b->v.nodes);
17931da177e4SLinus Torvalds 	case MPOL_PREFERRED:
1794fc36b8d3SLee Schermerhorn 		return a->v.preferred_node == b->v.preferred_node &&
1795fc36b8d3SLee Schermerhorn 			a->flags == b->flags;
17961da177e4SLinus Torvalds 	default:
17971da177e4SLinus Torvalds 		BUG();
17981da177e4SLinus Torvalds 		return 0;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds /*
18031da177e4SLinus Torvalds  * Shared memory backing store policy support.
18041da177e4SLinus Torvalds  *
18051da177e4SLinus Torvalds  * Remember policies even when nobody has shared memory mapped.
18061da177e4SLinus Torvalds  * The policies are kept in Red-Black tree linked from the inode.
18071da177e4SLinus Torvalds  * They are protected by the sp->lock spinlock, which should be held
18081da177e4SLinus Torvalds  * for any accesses to the tree.
18091da177e4SLinus Torvalds  */
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds /* lookup first element intersecting start-end */
18121da177e4SLinus Torvalds /* Caller holds sp->lock */
18131da177e4SLinus Torvalds static struct sp_node *
18141da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds 	struct rb_node *n = sp->root.rb_node;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	while (n) {
18191da177e4SLinus Torvalds 		struct sp_node *p = rb_entry(n, struct sp_node, nd);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 		if (start >= p->end)
18221da177e4SLinus Torvalds 			n = n->rb_right;
18231da177e4SLinus Torvalds 		else if (end <= p->start)
18241da177e4SLinus Torvalds 			n = n->rb_left;
18251da177e4SLinus Torvalds 		else
18261da177e4SLinus Torvalds 			break;
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 	if (!n)
18291da177e4SLinus Torvalds 		return NULL;
18301da177e4SLinus Torvalds 	for (;;) {
18311da177e4SLinus Torvalds 		struct sp_node *w = NULL;
18321da177e4SLinus Torvalds 		struct rb_node *prev = rb_prev(n);
18331da177e4SLinus Torvalds 		if (!prev)
18341da177e4SLinus Torvalds 			break;
18351da177e4SLinus Torvalds 		w = rb_entry(prev, struct sp_node, nd);
18361da177e4SLinus Torvalds 		if (w->end <= start)
18371da177e4SLinus Torvalds 			break;
18381da177e4SLinus Torvalds 		n = prev;
18391da177e4SLinus Torvalds 	}
18401da177e4SLinus Torvalds 	return rb_entry(n, struct sp_node, nd);
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /* Insert a new shared policy into the list. */
18441da177e4SLinus Torvalds /* Caller holds sp->lock */
18451da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new)
18461da177e4SLinus Torvalds {
18471da177e4SLinus Torvalds 	struct rb_node **p = &sp->root.rb_node;
18481da177e4SLinus Torvalds 	struct rb_node *parent = NULL;
18491da177e4SLinus Torvalds 	struct sp_node *nd;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	while (*p) {
18521da177e4SLinus Torvalds 		parent = *p;
18531da177e4SLinus Torvalds 		nd = rb_entry(parent, struct sp_node, nd);
18541da177e4SLinus Torvalds 		if (new->start < nd->start)
18551da177e4SLinus Torvalds 			p = &(*p)->rb_left;
18561da177e4SLinus Torvalds 		else if (new->end > nd->end)
18571da177e4SLinus Torvalds 			p = &(*p)->rb_right;
18581da177e4SLinus Torvalds 		else
18591da177e4SLinus Torvalds 			BUG();
18601da177e4SLinus Torvalds 	}
18611da177e4SLinus Torvalds 	rb_link_node(&new->nd, parent, p);
18621da177e4SLinus Torvalds 	rb_insert_color(&new->nd, &sp->root);
1863140d5a49SPaul Mundt 	pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
186445c4745aSLee Schermerhorn 		 new->policy ? new->policy->mode : 0);
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds /* Find shared policy intersecting idx */
18681da177e4SLinus Torvalds struct mempolicy *
18691da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
18701da177e4SLinus Torvalds {
18711da177e4SLinus Torvalds 	struct mempolicy *pol = NULL;
18721da177e4SLinus Torvalds 	struct sp_node *sn;
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	if (!sp->root.rb_node)
18751da177e4SLinus Torvalds 		return NULL;
18761da177e4SLinus Torvalds 	spin_lock(&sp->lock);
18771da177e4SLinus Torvalds 	sn = sp_lookup(sp, idx, idx+1);
18781da177e4SLinus Torvalds 	if (sn) {
18791da177e4SLinus Torvalds 		mpol_get(sn->policy);
18801da177e4SLinus Torvalds 		pol = sn->policy;
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
18831da177e4SLinus Torvalds 	return pol;
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n)
18871da177e4SLinus Torvalds {
1888140d5a49SPaul Mundt 	pr_debug("deleting %lx-l%lx\n", n->start, n->end);
18891da177e4SLinus Torvalds 	rb_erase(&n->nd, &sp->root);
1890f0be3d32SLee Schermerhorn 	mpol_put(n->policy);
18911da177e4SLinus Torvalds 	kmem_cache_free(sn_cache, n);
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
1894dbcb0f19SAdrian Bunk static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
1895dbcb0f19SAdrian Bunk 				struct mempolicy *pol)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	if (!n)
19001da177e4SLinus Torvalds 		return NULL;
19011da177e4SLinus Torvalds 	n->start = start;
19021da177e4SLinus Torvalds 	n->end = end;
19031da177e4SLinus Torvalds 	mpol_get(pol);
1904aab0b102SLee Schermerhorn 	pol->flags |= MPOL_F_SHARED;	/* for unref */
19051da177e4SLinus Torvalds 	n->policy = pol;
19061da177e4SLinus Torvalds 	return n;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds /* Replace a policy range. */
19101da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
19111da177e4SLinus Torvalds 				 unsigned long end, struct sp_node *new)
19121da177e4SLinus Torvalds {
19131da177e4SLinus Torvalds 	struct sp_node *n, *new2 = NULL;
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds restart:
19161da177e4SLinus Torvalds 	spin_lock(&sp->lock);
19171da177e4SLinus Torvalds 	n = sp_lookup(sp, start, end);
19181da177e4SLinus Torvalds 	/* Take care of old policies in the same range. */
19191da177e4SLinus Torvalds 	while (n && n->start < end) {
19201da177e4SLinus Torvalds 		struct rb_node *next = rb_next(&n->nd);
19211da177e4SLinus Torvalds 		if (n->start >= start) {
19221da177e4SLinus Torvalds 			if (n->end <= end)
19231da177e4SLinus Torvalds 				sp_delete(sp, n);
19241da177e4SLinus Torvalds 			else
19251da177e4SLinus Torvalds 				n->start = end;
19261da177e4SLinus Torvalds 		} else {
19271da177e4SLinus Torvalds 			/* Old policy spanning whole new range. */
19281da177e4SLinus Torvalds 			if (n->end > end) {
19291da177e4SLinus Torvalds 				if (!new2) {
19301da177e4SLinus Torvalds 					spin_unlock(&sp->lock);
19311da177e4SLinus Torvalds 					new2 = sp_alloc(end, n->end, n->policy);
19321da177e4SLinus Torvalds 					if (!new2)
19331da177e4SLinus Torvalds 						return -ENOMEM;
19341da177e4SLinus Torvalds 					goto restart;
19351da177e4SLinus Torvalds 				}
19361da177e4SLinus Torvalds 				n->end = start;
19371da177e4SLinus Torvalds 				sp_insert(sp, new2);
19381da177e4SLinus Torvalds 				new2 = NULL;
19391da177e4SLinus Torvalds 				break;
19401da177e4SLinus Torvalds 			} else
19411da177e4SLinus Torvalds 				n->end = start;
19421da177e4SLinus Torvalds 		}
19431da177e4SLinus Torvalds 		if (!next)
19441da177e4SLinus Torvalds 			break;
19451da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 	if (new)
19481da177e4SLinus Torvalds 		sp_insert(sp, new);
19491da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
19501da177e4SLinus Torvalds 	if (new2) {
1951f0be3d32SLee Schermerhorn 		mpol_put(new2->policy);
19521da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new2);
19531da177e4SLinus Torvalds 	}
19541da177e4SLinus Torvalds 	return 0;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
195771fe804bSLee Schermerhorn /**
195871fe804bSLee Schermerhorn  * mpol_shared_policy_init - initialize shared policy for inode
195971fe804bSLee Schermerhorn  * @sp: pointer to inode shared policy
196071fe804bSLee Schermerhorn  * @mpol:  struct mempolicy to install
196171fe804bSLee Schermerhorn  *
196271fe804bSLee Schermerhorn  * Install non-NULL @mpol in inode's shared policy rb-tree.
196371fe804bSLee Schermerhorn  * On entry, the current task has a reference on a non-NULL @mpol.
196471fe804bSLee Schermerhorn  * This must be released on exit.
19654bfc4495SKAMEZAWA Hiroyuki  * This is called at get_inode() calls and we can use GFP_KERNEL.
196671fe804bSLee Schermerhorn  */
196771fe804bSLee Schermerhorn void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
19687339ff83SRobin Holt {
196958568d2aSMiao Xie 	int ret;
197058568d2aSMiao Xie 
197171fe804bSLee Schermerhorn 	sp->root = RB_ROOT;		/* empty tree == default mempolicy */
197271fe804bSLee Schermerhorn 	spin_lock_init(&sp->lock);
19737339ff83SRobin Holt 
197471fe804bSLee Schermerhorn 	if (mpol) {
19757339ff83SRobin Holt 		struct vm_area_struct pvma;
197671fe804bSLee Schermerhorn 		struct mempolicy *new;
19774bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
19787339ff83SRobin Holt 
19794bfc4495SKAMEZAWA Hiroyuki 		if (!scratch)
19804bfc4495SKAMEZAWA Hiroyuki 			return;
198171fe804bSLee Schermerhorn 		/* contextualize the tmpfs mount point mempolicy */
198271fe804bSLee Schermerhorn 		new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
198358568d2aSMiao Xie 		if (IS_ERR(new)) {
198471fe804bSLee Schermerhorn 			mpol_put(mpol);	/* drop our ref on sb mpol */
19854bfc4495SKAMEZAWA Hiroyuki 			NODEMASK_SCRATCH_FREE(scratch);
198671fe804bSLee Schermerhorn 			return;		/* no valid nodemask intersection */
198758568d2aSMiao Xie 		}
198858568d2aSMiao Xie 
198958568d2aSMiao Xie 		task_lock(current);
19904bfc4495SKAMEZAWA Hiroyuki 		ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch);
199158568d2aSMiao Xie 		task_unlock(current);
199258568d2aSMiao Xie 		mpol_put(mpol);	/* drop our ref on sb mpol */
199358568d2aSMiao Xie 		if (ret) {
19944bfc4495SKAMEZAWA Hiroyuki 			NODEMASK_SCRATCH_FREE(scratch);
199558568d2aSMiao Xie 			mpol_put(new);
199658568d2aSMiao Xie 			return;
199758568d2aSMiao Xie 		}
199871fe804bSLee Schermerhorn 
199971fe804bSLee Schermerhorn 		/* Create pseudo-vma that contains just the policy */
20007339ff83SRobin Holt 		memset(&pvma, 0, sizeof(struct vm_area_struct));
200171fe804bSLee Schermerhorn 		pvma.vm_end = TASK_SIZE;	/* policy covers entire file */
200271fe804bSLee Schermerhorn 		mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
200371fe804bSLee Schermerhorn 		mpol_put(new);			/* drop initial ref */
20044bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
20057339ff83SRobin Holt 	}
20067339ff83SRobin Holt }
20077339ff83SRobin Holt 
20081da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info,
20091da177e4SLinus Torvalds 			struct vm_area_struct *vma, struct mempolicy *npol)
20101da177e4SLinus Torvalds {
20111da177e4SLinus Torvalds 	int err;
20121da177e4SLinus Torvalds 	struct sp_node *new = NULL;
20131da177e4SLinus Torvalds 	unsigned long sz = vma_pages(vma);
20141da177e4SLinus Torvalds 
2015028fec41SDavid Rientjes 	pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
20161da177e4SLinus Torvalds 		 vma->vm_pgoff,
201745c4745aSLee Schermerhorn 		 sz, npol ? npol->mode : -1,
2018028fec41SDavid Rientjes 		 npol ? npol->flags : -1,
2019dfcd3c0dSAndi Kleen 		 npol ? nodes_addr(npol->v.nodes)[0] : -1);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	if (npol) {
20221da177e4SLinus Torvalds 		new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
20231da177e4SLinus Torvalds 		if (!new)
20241da177e4SLinus Torvalds 			return -ENOMEM;
20251da177e4SLinus Torvalds 	}
20261da177e4SLinus Torvalds 	err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
20271da177e4SLinus Torvalds 	if (err && new)
20281da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new);
20291da177e4SLinus Torvalds 	return err;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */
20331da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	struct sp_node *n;
20361da177e4SLinus Torvalds 	struct rb_node *next;
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	if (!p->root.rb_node)
20391da177e4SLinus Torvalds 		return;
20401da177e4SLinus Torvalds 	spin_lock(&p->lock);
20411da177e4SLinus Torvalds 	next = rb_first(&p->root);
20421da177e4SLinus Torvalds 	while (next) {
20431da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
20441da177e4SLinus Torvalds 		next = rb_next(&n->nd);
204590c5029eSAndi Kleen 		rb_erase(&n->nd, &p->root);
2046f0be3d32SLee Schermerhorn 		mpol_put(n->policy);
20471da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, n);
20481da177e4SLinus Torvalds 	}
20491da177e4SLinus Torvalds 	spin_unlock(&p->lock);
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */
20531da177e4SLinus Torvalds void __init numa_policy_init(void)
20541da177e4SLinus Torvalds {
2055b71636e2SPaul Mundt 	nodemask_t interleave_nodes;
2056b71636e2SPaul Mundt 	unsigned long largest = 0;
2057b71636e2SPaul Mundt 	int nid, prefer = 0;
2058b71636e2SPaul Mundt 
20591da177e4SLinus Torvalds 	policy_cache = kmem_cache_create("numa_policy",
20601da177e4SLinus Torvalds 					 sizeof(struct mempolicy),
206120c2df83SPaul Mundt 					 0, SLAB_PANIC, NULL);
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	sn_cache = kmem_cache_create("shared_policy_node",
20641da177e4SLinus Torvalds 				     sizeof(struct sp_node),
206520c2df83SPaul Mundt 				     0, SLAB_PANIC, NULL);
20661da177e4SLinus Torvalds 
2067b71636e2SPaul Mundt 	/*
2068b71636e2SPaul Mundt 	 * Set interleaving policy for system init. Interleaving is only
2069b71636e2SPaul Mundt 	 * enabled across suitably sized nodes (default is >= 16MB), or
2070b71636e2SPaul Mundt 	 * fall back to the largest node if they're all smaller.
2071b71636e2SPaul Mundt 	 */
2072b71636e2SPaul Mundt 	nodes_clear(interleave_nodes);
207356bbd65dSChristoph Lameter 	for_each_node_state(nid, N_HIGH_MEMORY) {
2074b71636e2SPaul Mundt 		unsigned long total_pages = node_present_pages(nid);
20751da177e4SLinus Torvalds 
2076b71636e2SPaul Mundt 		/* Preserve the largest node */
2077b71636e2SPaul Mundt 		if (largest < total_pages) {
2078b71636e2SPaul Mundt 			largest = total_pages;
2079b71636e2SPaul Mundt 			prefer = nid;
2080b71636e2SPaul Mundt 		}
2081b71636e2SPaul Mundt 
2082b71636e2SPaul Mundt 		/* Interleave this node? */
2083b71636e2SPaul Mundt 		if ((total_pages << PAGE_SHIFT) >= (16 << 20))
2084b71636e2SPaul Mundt 			node_set(nid, interleave_nodes);
2085b71636e2SPaul Mundt 	}
2086b71636e2SPaul Mundt 
2087b71636e2SPaul Mundt 	/* All too small, use the largest */
2088b71636e2SPaul Mundt 	if (unlikely(nodes_empty(interleave_nodes)))
2089b71636e2SPaul Mundt 		node_set(prefer, interleave_nodes);
2090b71636e2SPaul Mundt 
2091028fec41SDavid Rientjes 	if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
20921da177e4SLinus Torvalds 		printk("numa_policy_init: interleaving failed\n");
20931da177e4SLinus Torvalds }
20941da177e4SLinus Torvalds 
20958bccd85fSChristoph Lameter /* Reset policy of current process to default */
20961da177e4SLinus Torvalds void numa_default_policy(void)
20971da177e4SLinus Torvalds {
2098028fec41SDavid Rientjes 	do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
20991da177e4SLinus Torvalds }
210068860ec1SPaul Jackson 
21014225399aSPaul Jackson /*
2102095f1fc4SLee Schermerhorn  * Parse and format mempolicy from/to strings
2103095f1fc4SLee Schermerhorn  */
2104095f1fc4SLee Schermerhorn 
2105095f1fc4SLee Schermerhorn /*
2106fc36b8d3SLee Schermerhorn  * "local" is pseudo-policy:  MPOL_PREFERRED with MPOL_F_LOCAL flag
21073f226aa1SLee Schermerhorn  * Used only for mpol_parse_str() and mpol_to_str()
21081a75a6c8SChristoph Lameter  */
210953f2556bSLee Schermerhorn #define MPOL_LOCAL (MPOL_INTERLEAVE + 1)
211015ad7cdcSHelge Deller static const char * const policy_types[] =
211153f2556bSLee Schermerhorn 	{ "default", "prefer", "bind", "interleave", "local" };
21121a75a6c8SChristoph Lameter 
2113095f1fc4SLee Schermerhorn 
2114095f1fc4SLee Schermerhorn #ifdef CONFIG_TMPFS
2115095f1fc4SLee Schermerhorn /**
2116095f1fc4SLee Schermerhorn  * mpol_parse_str - parse string to mempolicy
2117095f1fc4SLee Schermerhorn  * @str:  string containing mempolicy to parse
211871fe804bSLee Schermerhorn  * @mpol:  pointer to struct mempolicy pointer, returned on success.
211971fe804bSLee Schermerhorn  * @no_context:  flag whether to "contextualize" the mempolicy
2120095f1fc4SLee Schermerhorn  *
2121095f1fc4SLee Schermerhorn  * Format of input:
2122095f1fc4SLee Schermerhorn  *	<mode>[=<flags>][:<nodelist>]
2123095f1fc4SLee Schermerhorn  *
212471fe804bSLee Schermerhorn  * if @no_context is true, save the input nodemask in w.user_nodemask in
212571fe804bSLee Schermerhorn  * the returned mempolicy.  This will be used to "clone" the mempolicy in
212671fe804bSLee Schermerhorn  * a specific context [cpuset] at a later time.  Used to parse tmpfs mpol
212771fe804bSLee Schermerhorn  * mount option.  Note that if 'static' or 'relative' mode flags were
212871fe804bSLee Schermerhorn  * specified, the input nodemask will already have been saved.  Saving
212971fe804bSLee Schermerhorn  * it again is redundant, but safe.
213071fe804bSLee Schermerhorn  *
213171fe804bSLee Schermerhorn  * On success, returns 0, else 1
2132095f1fc4SLee Schermerhorn  */
213371fe804bSLee Schermerhorn int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context)
2134095f1fc4SLee Schermerhorn {
213571fe804bSLee Schermerhorn 	struct mempolicy *new = NULL;
213671fe804bSLee Schermerhorn 	unsigned short uninitialized_var(mode);
213771fe804bSLee Schermerhorn 	unsigned short uninitialized_var(mode_flags);
213871fe804bSLee Schermerhorn 	nodemask_t nodes;
2139095f1fc4SLee Schermerhorn 	char *nodelist = strchr(str, ':');
2140095f1fc4SLee Schermerhorn 	char *flags = strchr(str, '=');
2141095f1fc4SLee Schermerhorn 	int i;
2142095f1fc4SLee Schermerhorn 	int err = 1;
2143095f1fc4SLee Schermerhorn 
2144095f1fc4SLee Schermerhorn 	if (nodelist) {
2145095f1fc4SLee Schermerhorn 		/* NUL-terminate mode or flags string */
2146095f1fc4SLee Schermerhorn 		*nodelist++ = '\0';
214771fe804bSLee Schermerhorn 		if (nodelist_parse(nodelist, nodes))
2148095f1fc4SLee Schermerhorn 			goto out;
214971fe804bSLee Schermerhorn 		if (!nodes_subset(nodes, node_states[N_HIGH_MEMORY]))
2150095f1fc4SLee Schermerhorn 			goto out;
215171fe804bSLee Schermerhorn 	} else
215271fe804bSLee Schermerhorn 		nodes_clear(nodes);
215371fe804bSLee Schermerhorn 
2154095f1fc4SLee Schermerhorn 	if (flags)
2155095f1fc4SLee Schermerhorn 		*flags++ = '\0';	/* terminate mode string */
2156095f1fc4SLee Schermerhorn 
21573f226aa1SLee Schermerhorn 	for (i = 0; i <= MPOL_LOCAL; i++) {
2158095f1fc4SLee Schermerhorn 		if (!strcmp(str, policy_types[i])) {
215971fe804bSLee Schermerhorn 			mode = i;
2160095f1fc4SLee Schermerhorn 			break;
2161095f1fc4SLee Schermerhorn 		}
2162095f1fc4SLee Schermerhorn 	}
21633f226aa1SLee Schermerhorn 	if (i > MPOL_LOCAL)
2164095f1fc4SLee Schermerhorn 		goto out;
2165095f1fc4SLee Schermerhorn 
216671fe804bSLee Schermerhorn 	switch (mode) {
2167095f1fc4SLee Schermerhorn 	case MPOL_PREFERRED:
216871fe804bSLee Schermerhorn 		/*
216971fe804bSLee Schermerhorn 		 * Insist on a nodelist of one node only
217071fe804bSLee Schermerhorn 		 */
2171095f1fc4SLee Schermerhorn 		if (nodelist) {
2172095f1fc4SLee Schermerhorn 			char *rest = nodelist;
2173095f1fc4SLee Schermerhorn 			while (isdigit(*rest))
2174095f1fc4SLee Schermerhorn 				rest++;
2175095f1fc4SLee Schermerhorn 			if (!*rest)
2176095f1fc4SLee Schermerhorn 				err = 0;
2177095f1fc4SLee Schermerhorn 		}
2178095f1fc4SLee Schermerhorn 		break;
2179095f1fc4SLee Schermerhorn 	case MPOL_INTERLEAVE:
2180095f1fc4SLee Schermerhorn 		/*
2181095f1fc4SLee Schermerhorn 		 * Default to online nodes with memory if no nodelist
2182095f1fc4SLee Schermerhorn 		 */
2183095f1fc4SLee Schermerhorn 		if (!nodelist)
218471fe804bSLee Schermerhorn 			nodes = node_states[N_HIGH_MEMORY];
2185095f1fc4SLee Schermerhorn 		err = 0;
21863f226aa1SLee Schermerhorn 		break;
218771fe804bSLee Schermerhorn 	case MPOL_LOCAL:
21883f226aa1SLee Schermerhorn 		/*
218971fe804bSLee Schermerhorn 		 * Don't allow a nodelist;  mpol_new() checks flags
21903f226aa1SLee Schermerhorn 		 */
219171fe804bSLee Schermerhorn 		if (nodelist)
21923f226aa1SLee Schermerhorn 			goto out;
219371fe804bSLee Schermerhorn 		mode = MPOL_PREFERRED;
21943f226aa1SLee Schermerhorn 		break;
219571fe804bSLee Schermerhorn 
219671fe804bSLee Schermerhorn 	/*
219771fe804bSLee Schermerhorn 	 * case MPOL_BIND:    mpol_new() enforces non-empty nodemask.
219871fe804bSLee Schermerhorn 	 * case MPOL_DEFAULT: mpol_new() enforces empty nodemask, ignores flags.
219971fe804bSLee Schermerhorn 	 */
2200095f1fc4SLee Schermerhorn 	}
2201095f1fc4SLee Schermerhorn 
220271fe804bSLee Schermerhorn 	mode_flags = 0;
2203095f1fc4SLee Schermerhorn 	if (flags) {
2204095f1fc4SLee Schermerhorn 		/*
2205095f1fc4SLee Schermerhorn 		 * Currently, we only support two mutually exclusive
2206095f1fc4SLee Schermerhorn 		 * mode flags.
2207095f1fc4SLee Schermerhorn 		 */
2208095f1fc4SLee Schermerhorn 		if (!strcmp(flags, "static"))
220971fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_STATIC_NODES;
2210095f1fc4SLee Schermerhorn 		else if (!strcmp(flags, "relative"))
221171fe804bSLee Schermerhorn 			mode_flags |= MPOL_F_RELATIVE_NODES;
2212095f1fc4SLee Schermerhorn 		else
2213095f1fc4SLee Schermerhorn 			err = 1;
2214095f1fc4SLee Schermerhorn 	}
221571fe804bSLee Schermerhorn 
221671fe804bSLee Schermerhorn 	new = mpol_new(mode, mode_flags, &nodes);
221771fe804bSLee Schermerhorn 	if (IS_ERR(new))
221871fe804bSLee Schermerhorn 		err = 1;
221958568d2aSMiao Xie 	else {
222058568d2aSMiao Xie 		int ret;
22214bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH(scratch);
22224bfc4495SKAMEZAWA Hiroyuki 		if (scratch) {
222358568d2aSMiao Xie 			task_lock(current);
22244bfc4495SKAMEZAWA Hiroyuki 			ret = mpol_set_nodemask(new, &nodes, scratch);
222558568d2aSMiao Xie 			task_unlock(current);
22264bfc4495SKAMEZAWA Hiroyuki 		} else
22274bfc4495SKAMEZAWA Hiroyuki 			ret = -ENOMEM;
22284bfc4495SKAMEZAWA Hiroyuki 		NODEMASK_SCRATCH_FREE(scratch);
22294bfc4495SKAMEZAWA Hiroyuki 		if (ret) {
223058568d2aSMiao Xie 			err = 1;
22314bfc4495SKAMEZAWA Hiroyuki 			mpol_put(new);
22324bfc4495SKAMEZAWA Hiroyuki 		} else if (no_context) {
223358568d2aSMiao Xie 			/* save for contextualization */
223458568d2aSMiao Xie 			new->w.user_nodemask = nodes;
223558568d2aSMiao Xie 		}
223658568d2aSMiao Xie 	}
223771fe804bSLee Schermerhorn 
2238095f1fc4SLee Schermerhorn out:
2239095f1fc4SLee Schermerhorn 	/* Restore string for error message */
2240095f1fc4SLee Schermerhorn 	if (nodelist)
2241095f1fc4SLee Schermerhorn 		*--nodelist = ':';
2242095f1fc4SLee Schermerhorn 	if (flags)
2243095f1fc4SLee Schermerhorn 		*--flags = '=';
224471fe804bSLee Schermerhorn 	if (!err)
224571fe804bSLee Schermerhorn 		*mpol = new;
2246095f1fc4SLee Schermerhorn 	return err;
2247095f1fc4SLee Schermerhorn }
2248095f1fc4SLee Schermerhorn #endif /* CONFIG_TMPFS */
2249095f1fc4SLee Schermerhorn 
225071fe804bSLee Schermerhorn /**
225171fe804bSLee Schermerhorn  * mpol_to_str - format a mempolicy structure for printing
225271fe804bSLee Schermerhorn  * @buffer:  to contain formatted mempolicy string
225371fe804bSLee Schermerhorn  * @maxlen:  length of @buffer
225471fe804bSLee Schermerhorn  * @pol:  pointer to mempolicy to be formatted
225571fe804bSLee Schermerhorn  * @no_context:  "context free" mempolicy - use nodemask in w.user_nodemask
225671fe804bSLee Schermerhorn  *
22571a75a6c8SChristoph Lameter  * Convert a mempolicy into a string.
22581a75a6c8SChristoph Lameter  * Returns the number of characters in buffer (if positive)
22591a75a6c8SChristoph Lameter  * or an error (negative)
22601a75a6c8SChristoph Lameter  */
226171fe804bSLee Schermerhorn int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol, int no_context)
22621a75a6c8SChristoph Lameter {
22631a75a6c8SChristoph Lameter 	char *p = buffer;
22641a75a6c8SChristoph Lameter 	int l;
22651a75a6c8SChristoph Lameter 	nodemask_t nodes;
2266bea904d5SLee Schermerhorn 	unsigned short mode;
2267f5b087b5SDavid Rientjes 	unsigned short flags = pol ? pol->flags : 0;
22681a75a6c8SChristoph Lameter 
22692291990aSLee Schermerhorn 	/*
22702291990aSLee Schermerhorn 	 * Sanity check:  room for longest mode, flag and some nodes
22712291990aSLee Schermerhorn 	 */
22722291990aSLee Schermerhorn 	VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16);
22732291990aSLee Schermerhorn 
2274bea904d5SLee Schermerhorn 	if (!pol || pol == &default_policy)
2275bea904d5SLee Schermerhorn 		mode = MPOL_DEFAULT;
2276bea904d5SLee Schermerhorn 	else
2277bea904d5SLee Schermerhorn 		mode = pol->mode;
2278bea904d5SLee Schermerhorn 
22791a75a6c8SChristoph Lameter 	switch (mode) {
22801a75a6c8SChristoph Lameter 	case MPOL_DEFAULT:
22811a75a6c8SChristoph Lameter 		nodes_clear(nodes);
22821a75a6c8SChristoph Lameter 		break;
22831a75a6c8SChristoph Lameter 
22841a75a6c8SChristoph Lameter 	case MPOL_PREFERRED:
22851a75a6c8SChristoph Lameter 		nodes_clear(nodes);
2286fc36b8d3SLee Schermerhorn 		if (flags & MPOL_F_LOCAL)
228753f2556bSLee Schermerhorn 			mode = MPOL_LOCAL;	/* pseudo-policy */
228853f2556bSLee Schermerhorn 		else
2289fc36b8d3SLee Schermerhorn 			node_set(pol->v.preferred_node, nodes);
22901a75a6c8SChristoph Lameter 		break;
22911a75a6c8SChristoph Lameter 
22921a75a6c8SChristoph Lameter 	case MPOL_BIND:
229319770b32SMel Gorman 		/* Fall through */
22941a75a6c8SChristoph Lameter 	case MPOL_INTERLEAVE:
229571fe804bSLee Schermerhorn 		if (no_context)
229671fe804bSLee Schermerhorn 			nodes = pol->w.user_nodemask;
229771fe804bSLee Schermerhorn 		else
22981a75a6c8SChristoph Lameter 			nodes = pol->v.nodes;
22991a75a6c8SChristoph Lameter 		break;
23001a75a6c8SChristoph Lameter 
23011a75a6c8SChristoph Lameter 	default:
23021a75a6c8SChristoph Lameter 		BUG();
23031a75a6c8SChristoph Lameter 	}
23041a75a6c8SChristoph Lameter 
23051a75a6c8SChristoph Lameter 	l = strlen(policy_types[mode]);
23061a75a6c8SChristoph Lameter 	if (buffer + maxlen < p + l + 1)
23071a75a6c8SChristoph Lameter 		return -ENOSPC;
23081a75a6c8SChristoph Lameter 
23091a75a6c8SChristoph Lameter 	strcpy(p, policy_types[mode]);
23101a75a6c8SChristoph Lameter 	p += l;
23111a75a6c8SChristoph Lameter 
2312fc36b8d3SLee Schermerhorn 	if (flags & MPOL_MODE_FLAGS) {
2313f5b087b5SDavid Rientjes 		if (buffer + maxlen < p + 2)
2314f5b087b5SDavid Rientjes 			return -ENOSPC;
2315f5b087b5SDavid Rientjes 		*p++ = '=';
2316f5b087b5SDavid Rientjes 
23172291990aSLee Schermerhorn 		/*
23182291990aSLee Schermerhorn 		 * Currently, the only defined flags are mutually exclusive
23192291990aSLee Schermerhorn 		 */
2320f5b087b5SDavid Rientjes 		if (flags & MPOL_F_STATIC_NODES)
23212291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "static");
23222291990aSLee Schermerhorn 		else if (flags & MPOL_F_RELATIVE_NODES)
23232291990aSLee Schermerhorn 			p += snprintf(p, buffer + maxlen - p, "relative");
2324f5b087b5SDavid Rientjes 	}
2325f5b087b5SDavid Rientjes 
23261a75a6c8SChristoph Lameter 	if (!nodes_empty(nodes)) {
23271a75a6c8SChristoph Lameter 		if (buffer + maxlen < p + 2)
23281a75a6c8SChristoph Lameter 			return -ENOSPC;
2329095f1fc4SLee Schermerhorn 		*p++ = ':';
23301a75a6c8SChristoph Lameter 	 	p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
23311a75a6c8SChristoph Lameter 	}
23321a75a6c8SChristoph Lameter 	return p - buffer;
23331a75a6c8SChristoph Lameter }
23341a75a6c8SChristoph Lameter 
23351a75a6c8SChristoph Lameter struct numa_maps {
23361a75a6c8SChristoph Lameter 	unsigned long pages;
23371a75a6c8SChristoph Lameter 	unsigned long anon;
2338397874dfSChristoph Lameter 	unsigned long active;
2339397874dfSChristoph Lameter 	unsigned long writeback;
23401a75a6c8SChristoph Lameter 	unsigned long mapcount_max;
2341397874dfSChristoph Lameter 	unsigned long dirty;
2342397874dfSChristoph Lameter 	unsigned long swapcache;
23431a75a6c8SChristoph Lameter 	unsigned long node[MAX_NUMNODES];
23441a75a6c8SChristoph Lameter };
23451a75a6c8SChristoph Lameter 
2346397874dfSChristoph Lameter static void gather_stats(struct page *page, void *private, int pte_dirty)
23471a75a6c8SChristoph Lameter {
23481a75a6c8SChristoph Lameter 	struct numa_maps *md = private;
23491a75a6c8SChristoph Lameter 	int count = page_mapcount(page);
23501a75a6c8SChristoph Lameter 
23511a75a6c8SChristoph Lameter 	md->pages++;
2352397874dfSChristoph Lameter 	if (pte_dirty || PageDirty(page))
2353397874dfSChristoph Lameter 		md->dirty++;
2354397874dfSChristoph Lameter 
2355397874dfSChristoph Lameter 	if (PageSwapCache(page))
2356397874dfSChristoph Lameter 		md->swapcache++;
2357397874dfSChristoph Lameter 
2358894bc310SLee Schermerhorn 	if (PageActive(page) || PageUnevictable(page))
2359397874dfSChristoph Lameter 		md->active++;
2360397874dfSChristoph Lameter 
2361397874dfSChristoph Lameter 	if (PageWriteback(page))
2362397874dfSChristoph Lameter 		md->writeback++;
23631a75a6c8SChristoph Lameter 
23641a75a6c8SChristoph Lameter 	if (PageAnon(page))
23651a75a6c8SChristoph Lameter 		md->anon++;
23661a75a6c8SChristoph Lameter 
2367397874dfSChristoph Lameter 	if (count > md->mapcount_max)
2368397874dfSChristoph Lameter 		md->mapcount_max = count;
2369397874dfSChristoph Lameter 
23701a75a6c8SChristoph Lameter 	md->node[page_to_nid(page)]++;
23711a75a6c8SChristoph Lameter }
23721a75a6c8SChristoph Lameter 
23737f709ed0SAndrew Morton #ifdef CONFIG_HUGETLB_PAGE
2374397874dfSChristoph Lameter static void check_huge_range(struct vm_area_struct *vma,
2375397874dfSChristoph Lameter 		unsigned long start, unsigned long end,
2376397874dfSChristoph Lameter 		struct numa_maps *md)
2377397874dfSChristoph Lameter {
2378397874dfSChristoph Lameter 	unsigned long addr;
2379397874dfSChristoph Lameter 	struct page *page;
2380a5516438SAndi Kleen 	struct hstate *h = hstate_vma(vma);
2381a5516438SAndi Kleen 	unsigned long sz = huge_page_size(h);
2382397874dfSChristoph Lameter 
2383a5516438SAndi Kleen 	for (addr = start; addr < end; addr += sz) {
2384a5516438SAndi Kleen 		pte_t *ptep = huge_pte_offset(vma->vm_mm,
2385a5516438SAndi Kleen 						addr & huge_page_mask(h));
2386397874dfSChristoph Lameter 		pte_t pte;
2387397874dfSChristoph Lameter 
2388397874dfSChristoph Lameter 		if (!ptep)
2389397874dfSChristoph Lameter 			continue;
2390397874dfSChristoph Lameter 
2391397874dfSChristoph Lameter 		pte = *ptep;
2392397874dfSChristoph Lameter 		if (pte_none(pte))
2393397874dfSChristoph Lameter 			continue;
2394397874dfSChristoph Lameter 
2395397874dfSChristoph Lameter 		page = pte_page(pte);
2396397874dfSChristoph Lameter 		if (!page)
2397397874dfSChristoph Lameter 			continue;
2398397874dfSChristoph Lameter 
2399397874dfSChristoph Lameter 		gather_stats(page, md, pte_dirty(*ptep));
2400397874dfSChristoph Lameter 	}
2401397874dfSChristoph Lameter }
24027f709ed0SAndrew Morton #else
24037f709ed0SAndrew Morton static inline void check_huge_range(struct vm_area_struct *vma,
24047f709ed0SAndrew Morton 		unsigned long start, unsigned long end,
24057f709ed0SAndrew Morton 		struct numa_maps *md)
24067f709ed0SAndrew Morton {
24077f709ed0SAndrew Morton }
24087f709ed0SAndrew Morton #endif
2409397874dfSChristoph Lameter 
241053f2556bSLee Schermerhorn /*
241153f2556bSLee Schermerhorn  * Display pages allocated per node and memory policy via /proc.
241253f2556bSLee Schermerhorn  */
24131a75a6c8SChristoph Lameter int show_numa_map(struct seq_file *m, void *v)
24141a75a6c8SChristoph Lameter {
241599f89551SEric W. Biederman 	struct proc_maps_private *priv = m->private;
24161a75a6c8SChristoph Lameter 	struct vm_area_struct *vma = v;
24171a75a6c8SChristoph Lameter 	struct numa_maps *md;
2418397874dfSChristoph Lameter 	struct file *file = vma->vm_file;
2419397874dfSChristoph Lameter 	struct mm_struct *mm = vma->vm_mm;
2420480eccf9SLee Schermerhorn 	struct mempolicy *pol;
24211a75a6c8SChristoph Lameter 	int n;
24221a75a6c8SChristoph Lameter 	char buffer[50];
24231a75a6c8SChristoph Lameter 
2424397874dfSChristoph Lameter 	if (!mm)
24251a75a6c8SChristoph Lameter 		return 0;
24261a75a6c8SChristoph Lameter 
24271a75a6c8SChristoph Lameter 	md = kzalloc(sizeof(struct numa_maps), GFP_KERNEL);
24281a75a6c8SChristoph Lameter 	if (!md)
24291a75a6c8SChristoph Lameter 		return 0;
24301a75a6c8SChristoph Lameter 
2431480eccf9SLee Schermerhorn 	pol = get_vma_policy(priv->task, vma, vma->vm_start);
243271fe804bSLee Schermerhorn 	mpol_to_str(buffer, sizeof(buffer), pol, 0);
243352cd3b07SLee Schermerhorn 	mpol_cond_put(pol);
24341a75a6c8SChristoph Lameter 
2435397874dfSChristoph Lameter 	seq_printf(m, "%08lx %s", vma->vm_start, buffer);
2436397874dfSChristoph Lameter 
2437397874dfSChristoph Lameter 	if (file) {
2438397874dfSChristoph Lameter 		seq_printf(m, " file=");
2439c32c2f63SJan Blunck 		seq_path(m, &file->f_path, "\n\t= ");
2440397874dfSChristoph Lameter 	} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
2441397874dfSChristoph Lameter 		seq_printf(m, " heap");
2442397874dfSChristoph Lameter 	} else if (vma->vm_start <= mm->start_stack &&
2443397874dfSChristoph Lameter 			vma->vm_end >= mm->start_stack) {
2444397874dfSChristoph Lameter 		seq_printf(m, " stack");
2445397874dfSChristoph Lameter 	}
2446397874dfSChristoph Lameter 
2447397874dfSChristoph Lameter 	if (is_vm_hugetlb_page(vma)) {
2448397874dfSChristoph Lameter 		check_huge_range(vma, vma->vm_start, vma->vm_end, md);
2449397874dfSChristoph Lameter 		seq_printf(m, " huge");
2450397874dfSChristoph Lameter 	} else {
2451397874dfSChristoph Lameter 		check_pgd_range(vma, vma->vm_start, vma->vm_end,
245256bbd65dSChristoph Lameter 			&node_states[N_HIGH_MEMORY], MPOL_MF_STATS, md);
2453397874dfSChristoph Lameter 	}
2454397874dfSChristoph Lameter 
2455397874dfSChristoph Lameter 	if (!md->pages)
2456397874dfSChristoph Lameter 		goto out;
24571a75a6c8SChristoph Lameter 
24581a75a6c8SChristoph Lameter 	if (md->anon)
24591a75a6c8SChristoph Lameter 		seq_printf(m," anon=%lu",md->anon);
24601a75a6c8SChristoph Lameter 
2461397874dfSChristoph Lameter 	if (md->dirty)
2462397874dfSChristoph Lameter 		seq_printf(m," dirty=%lu",md->dirty);
2463397874dfSChristoph Lameter 
2464397874dfSChristoph Lameter 	if (md->pages != md->anon && md->pages != md->dirty)
2465397874dfSChristoph Lameter 		seq_printf(m, " mapped=%lu", md->pages);
2466397874dfSChristoph Lameter 
2467397874dfSChristoph Lameter 	if (md->mapcount_max > 1)
2468397874dfSChristoph Lameter 		seq_printf(m, " mapmax=%lu", md->mapcount_max);
2469397874dfSChristoph Lameter 
2470397874dfSChristoph Lameter 	if (md->swapcache)
2471397874dfSChristoph Lameter 		seq_printf(m," swapcache=%lu", md->swapcache);
2472397874dfSChristoph Lameter 
2473397874dfSChristoph Lameter 	if (md->active < md->pages && !is_vm_hugetlb_page(vma))
2474397874dfSChristoph Lameter 		seq_printf(m," active=%lu", md->active);
2475397874dfSChristoph Lameter 
2476397874dfSChristoph Lameter 	if (md->writeback)
2477397874dfSChristoph Lameter 		seq_printf(m," writeback=%lu", md->writeback);
2478397874dfSChristoph Lameter 
247956bbd65dSChristoph Lameter 	for_each_node_state(n, N_HIGH_MEMORY)
24801a75a6c8SChristoph Lameter 		if (md->node[n])
24811a75a6c8SChristoph Lameter 			seq_printf(m, " N%d=%lu", n, md->node[n]);
2482397874dfSChristoph Lameter out:
24831a75a6c8SChristoph Lameter 	seq_putc(m, '\n');
24841a75a6c8SChristoph Lameter 	kfree(md);
24851a75a6c8SChristoph Lameter 
24861a75a6c8SChristoph Lameter 	if (m->count < m->size)
248799f89551SEric W. Biederman 		m->version = (vma != priv->tail_vma) ? vma->vm_start : 0;
24881a75a6c8SChristoph Lameter 	return 0;
24891a75a6c8SChristoph Lameter }
2490