xref: /openbmc/linux/mm/mempolicy.c (revision 8af5e2eb3cc4450ffba9496c875beac41bf4f4f8)
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    could replace all the switch()es with a mempolicy_ops structure.
671da177e4SLinus Torvalds */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <linux/mempolicy.h>
701da177e4SLinus Torvalds #include <linux/mm.h>
711da177e4SLinus Torvalds #include <linux/highmem.h>
721da177e4SLinus Torvalds #include <linux/hugetlb.h>
731da177e4SLinus Torvalds #include <linux/kernel.h>
741da177e4SLinus Torvalds #include <linux/sched.h>
751da177e4SLinus Torvalds #include <linux/mm.h>
761da177e4SLinus Torvalds #include <linux/nodemask.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
781da177e4SLinus Torvalds #include <linux/gfp.h>
791da177e4SLinus Torvalds #include <linux/slab.h>
801da177e4SLinus Torvalds #include <linux/string.h>
811da177e4SLinus Torvalds #include <linux/module.h>
821da177e4SLinus Torvalds #include <linux/interrupt.h>
831da177e4SLinus Torvalds #include <linux/init.h>
841da177e4SLinus Torvalds #include <linux/compat.h>
851da177e4SLinus Torvalds #include <linux/mempolicy.h>
86dc9aa5b9SChristoph Lameter #include <linux/swap.h>
871a75a6c8SChristoph Lameter #include <linux/seq_file.h>
881a75a6c8SChristoph Lameter #include <linux/proc_fs.h>
89b20a3503SChristoph Lameter #include <linux/migrate.h>
9095a402c3SChristoph Lameter #include <linux/rmap.h>
9186c3a764SDavid Quigley #include <linux/security.h>
92dc9aa5b9SChristoph Lameter 
931da177e4SLinus Torvalds #include <asm/tlbflush.h>
941da177e4SLinus Torvalds #include <asm/uaccess.h>
951da177e4SLinus Torvalds 
9638e35860SChristoph Lameter /* Internal flags */
97dc9aa5b9SChristoph Lameter #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)	/* Skip checks for continuous vmas */
9838e35860SChristoph Lameter #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)		/* Invert check for nodemask */
991a75a6c8SChristoph Lameter #define MPOL_MF_STATS (MPOL_MF_INTERNAL << 2)		/* Gather statistics */
100dc9aa5b9SChristoph Lameter 
101fcc234f8SPekka Enberg static struct kmem_cache *policy_cache;
102fcc234f8SPekka Enberg static struct kmem_cache *sn_cache;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds #define PDprintk(fmt...)
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* Highest zone. An specific allocation for a zone below that is not
1071da177e4SLinus Torvalds    policied. */
1086267276fSChristoph Lameter enum zone_type policy_zone = 0;
1091da177e4SLinus Torvalds 
110d42c6997SAndi Kleen struct mempolicy default_policy = {
1111da177e4SLinus Torvalds 	.refcnt = ATOMIC_INIT(1), /* never free it */
1121da177e4SLinus Torvalds 	.policy = MPOL_DEFAULT,
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* Do sanity checking on a policy */
116dfcd3c0dSAndi Kleen static int mpol_check_policy(int mode, nodemask_t *nodes)
1171da177e4SLinus Torvalds {
118dfcd3c0dSAndi Kleen 	int empty = nodes_empty(*nodes);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	switch (mode) {
1211da177e4SLinus Torvalds 	case MPOL_DEFAULT:
1221da177e4SLinus Torvalds 		if (!empty)
1231da177e4SLinus Torvalds 			return -EINVAL;
1241da177e4SLinus Torvalds 		break;
1251da177e4SLinus Torvalds 	case MPOL_BIND:
1261da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
1271da177e4SLinus Torvalds 		/* Preferred will only use the first bit, but allow
1281da177e4SLinus Torvalds 		   more for now. */
1291da177e4SLinus Torvalds 		if (empty)
1301da177e4SLinus Torvalds 			return -EINVAL;
1311da177e4SLinus Torvalds 		break;
1321da177e4SLinus Torvalds 	}
133dfcd3c0dSAndi Kleen 	return nodes_subset(*nodes, node_online_map) ? 0 : -EINVAL;
1341da177e4SLinus Torvalds }
135dd942ae3SAndi Kleen 
1361da177e4SLinus Torvalds /* Generate a custom zonelist for the BIND policy. */
137dfcd3c0dSAndi Kleen static struct zonelist *bind_zonelist(nodemask_t *nodes)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	struct zonelist *zl;
1402f6726e5SChristoph Lameter 	int num, max, nd;
1412f6726e5SChristoph Lameter 	enum zone_type k;
1421da177e4SLinus Torvalds 
143dfcd3c0dSAndi Kleen 	max = 1 + MAX_NR_ZONES * nodes_weight(*nodes);
1449276b1bcSPaul Jackson 	max++;			/* space for zlcache_ptr (see mmzone.h) */
145dd942ae3SAndi Kleen 	zl = kmalloc(sizeof(struct zone *) * max, GFP_KERNEL);
1461da177e4SLinus Torvalds 	if (!zl)
147*8af5e2ebSKAMEZAWA Hiroyuki 		return ERR_PTR(-ENOMEM);
1489276b1bcSPaul Jackson 	zl->zlcache_ptr = NULL;
1491da177e4SLinus Torvalds 	num = 0;
150dd942ae3SAndi Kleen 	/* First put in the highest zones from all nodes, then all the next
151dd942ae3SAndi Kleen 	   lower zones etc. Avoid empty zones because the memory allocator
152dd942ae3SAndi Kleen 	   doesn't like them. If you implement node hot removal you
153dd942ae3SAndi Kleen 	   have to fix that. */
1542f6726e5SChristoph Lameter 	k = policy_zone;
1552f6726e5SChristoph Lameter 	while (1) {
156dd942ae3SAndi Kleen 		for_each_node_mask(nd, *nodes) {
157dd942ae3SAndi Kleen 			struct zone *z = &NODE_DATA(nd)->node_zones[k];
158dd942ae3SAndi Kleen 			if (z->present_pages > 0)
159dd942ae3SAndi Kleen 				zl->zones[num++] = z;
160dd942ae3SAndi Kleen 		}
1612f6726e5SChristoph Lameter 		if (k == 0)
1622f6726e5SChristoph Lameter 			break;
1632f6726e5SChristoph Lameter 		k--;
164dd942ae3SAndi Kleen 	}
165*8af5e2ebSKAMEZAWA Hiroyuki 	if (num == 0) {
166*8af5e2ebSKAMEZAWA Hiroyuki 		kfree(zl);
167*8af5e2ebSKAMEZAWA Hiroyuki 		return ERR_PTR(-EINVAL);
168*8af5e2ebSKAMEZAWA Hiroyuki 	}
1691da177e4SLinus Torvalds 	zl->zones[num] = NULL;
1701da177e4SLinus Torvalds 	return zl;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds /* Create a new policy */
174dfcd3c0dSAndi Kleen static struct mempolicy *mpol_new(int mode, nodemask_t *nodes)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct mempolicy *policy;
1771da177e4SLinus Torvalds 
178dfcd3c0dSAndi Kleen 	PDprintk("setting mode %d nodes[0] %lx\n", mode, nodes_addr(*nodes)[0]);
1791da177e4SLinus Torvalds 	if (mode == MPOL_DEFAULT)
1801da177e4SLinus Torvalds 		return NULL;
1811da177e4SLinus Torvalds 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
1821da177e4SLinus Torvalds 	if (!policy)
1831da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1841da177e4SLinus Torvalds 	atomic_set(&policy->refcnt, 1);
1851da177e4SLinus Torvalds 	switch (mode) {
1861da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
187dfcd3c0dSAndi Kleen 		policy->v.nodes = *nodes;
1888f493d79SAndi Kleen 		if (nodes_weight(*nodes) == 0) {
1898f493d79SAndi Kleen 			kmem_cache_free(policy_cache, policy);
1908f493d79SAndi Kleen 			return ERR_PTR(-EINVAL);
1918f493d79SAndi Kleen 		}
1921da177e4SLinus Torvalds 		break;
1931da177e4SLinus Torvalds 	case MPOL_PREFERRED:
194dfcd3c0dSAndi Kleen 		policy->v.preferred_node = first_node(*nodes);
1951da177e4SLinus Torvalds 		if (policy->v.preferred_node >= MAX_NUMNODES)
1961da177e4SLinus Torvalds 			policy->v.preferred_node = -1;
1971da177e4SLinus Torvalds 		break;
1981da177e4SLinus Torvalds 	case MPOL_BIND:
1991da177e4SLinus Torvalds 		policy->v.zonelist = bind_zonelist(nodes);
200*8af5e2ebSKAMEZAWA Hiroyuki 		if (IS_ERR(policy->v.zonelist)) {
201*8af5e2ebSKAMEZAWA Hiroyuki 			void *error_code = policy->v.zonelist;
2021da177e4SLinus Torvalds 			kmem_cache_free(policy_cache, policy);
203*8af5e2ebSKAMEZAWA Hiroyuki 			return error_code;
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 		break;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 	policy->policy = mode;
20874cb2155SPaul Jackson 	policy->cpuset_mems_allowed = cpuset_mems_allowed(current);
2091da177e4SLinus Torvalds 	return policy;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
212397874dfSChristoph Lameter static void gather_stats(struct page *, void *, int pte_dirty);
213fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
214fc301289SChristoph Lameter 				unsigned long flags);
2151a75a6c8SChristoph Lameter 
21638e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */
217b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
218dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
219dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
22038e35860SChristoph Lameter 		void *private)
2211da177e4SLinus Torvalds {
22291612e0dSHugh Dickins 	pte_t *orig_pte;
22391612e0dSHugh Dickins 	pte_t *pte;
224705e87c0SHugh Dickins 	spinlock_t *ptl;
225941150a3SHugh Dickins 
226705e87c0SHugh Dickins 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
22791612e0dSHugh Dickins 	do {
2286aab341eSLinus Torvalds 		struct page *page;
22925ba77c1SAndy Whitcroft 		int nid;
23091612e0dSHugh Dickins 
23191612e0dSHugh Dickins 		if (!pte_present(*pte))
23291612e0dSHugh Dickins 			continue;
2336aab341eSLinus Torvalds 		page = vm_normal_page(vma, addr, *pte);
2346aab341eSLinus Torvalds 		if (!page)
23591612e0dSHugh Dickins 			continue;
236053837fcSNick Piggin 		/*
237053837fcSNick Piggin 		 * The check for PageReserved here is important to avoid
238053837fcSNick Piggin 		 * handling zero pages and other pages that may have been
239053837fcSNick Piggin 		 * marked special by the system.
240053837fcSNick Piggin 		 *
241053837fcSNick Piggin 		 * If the PageReserved would not be checked here then f.e.
242053837fcSNick Piggin 		 * the location of the zero page could have an influence
243053837fcSNick Piggin 		 * on MPOL_MF_STRICT, zero pages would be counted for
244053837fcSNick Piggin 		 * the per node stats, and there would be useless attempts
245053837fcSNick Piggin 		 * to put zero pages on the migration list.
246053837fcSNick Piggin 		 */
247f4598c8bSChristoph Lameter 		if (PageReserved(page))
248f4598c8bSChristoph Lameter 			continue;
2496aab341eSLinus Torvalds 		nid = page_to_nid(page);
25038e35860SChristoph Lameter 		if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
25138e35860SChristoph Lameter 			continue;
25238e35860SChristoph Lameter 
2531a75a6c8SChristoph Lameter 		if (flags & MPOL_MF_STATS)
254397874dfSChristoph Lameter 			gather_stats(page, private, pte_dirty(*pte));
255053837fcSNick Piggin 		else if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
256fc301289SChristoph Lameter 			migrate_page_add(page, private, flags);
257dc9aa5b9SChristoph Lameter 		else
2581da177e4SLinus Torvalds 			break;
25991612e0dSHugh Dickins 	} while (pte++, addr += PAGE_SIZE, addr != end);
260705e87c0SHugh Dickins 	pte_unmap_unlock(orig_pte, ptl);
26191612e0dSHugh Dickins 	return addr != end;
26291612e0dSHugh Dickins }
26391612e0dSHugh Dickins 
264b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
265dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
266dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
26738e35860SChristoph Lameter 		void *private)
26891612e0dSHugh Dickins {
26991612e0dSHugh Dickins 	pmd_t *pmd;
27091612e0dSHugh Dickins 	unsigned long next;
27191612e0dSHugh Dickins 
27291612e0dSHugh Dickins 	pmd = pmd_offset(pud, addr);
27391612e0dSHugh Dickins 	do {
27491612e0dSHugh Dickins 		next = pmd_addr_end(addr, end);
27591612e0dSHugh Dickins 		if (pmd_none_or_clear_bad(pmd))
27691612e0dSHugh Dickins 			continue;
277dc9aa5b9SChristoph Lameter 		if (check_pte_range(vma, pmd, addr, next, nodes,
27838e35860SChristoph Lameter 				    flags, private))
27991612e0dSHugh Dickins 			return -EIO;
28091612e0dSHugh Dickins 	} while (pmd++, addr = next, addr != end);
28191612e0dSHugh Dickins 	return 0;
28291612e0dSHugh Dickins }
28391612e0dSHugh Dickins 
284b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
285dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
286dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
28738e35860SChristoph Lameter 		void *private)
28891612e0dSHugh Dickins {
28991612e0dSHugh Dickins 	pud_t *pud;
29091612e0dSHugh Dickins 	unsigned long next;
29191612e0dSHugh Dickins 
29291612e0dSHugh Dickins 	pud = pud_offset(pgd, addr);
29391612e0dSHugh Dickins 	do {
29491612e0dSHugh Dickins 		next = pud_addr_end(addr, end);
29591612e0dSHugh Dickins 		if (pud_none_or_clear_bad(pud))
29691612e0dSHugh Dickins 			continue;
297dc9aa5b9SChristoph Lameter 		if (check_pmd_range(vma, pud, addr, next, nodes,
29838e35860SChristoph Lameter 				    flags, private))
29991612e0dSHugh Dickins 			return -EIO;
30091612e0dSHugh Dickins 	} while (pud++, addr = next, addr != end);
30191612e0dSHugh Dickins 	return 0;
30291612e0dSHugh Dickins }
30391612e0dSHugh Dickins 
304b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma,
305dc9aa5b9SChristoph Lameter 		unsigned long addr, unsigned long end,
306dc9aa5b9SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags,
30738e35860SChristoph Lameter 		void *private)
30891612e0dSHugh Dickins {
30991612e0dSHugh Dickins 	pgd_t *pgd;
31091612e0dSHugh Dickins 	unsigned long next;
31191612e0dSHugh Dickins 
312b5810039SNick Piggin 	pgd = pgd_offset(vma->vm_mm, addr);
31391612e0dSHugh Dickins 	do {
31491612e0dSHugh Dickins 		next = pgd_addr_end(addr, end);
31591612e0dSHugh Dickins 		if (pgd_none_or_clear_bad(pgd))
31691612e0dSHugh Dickins 			continue;
317dc9aa5b9SChristoph Lameter 		if (check_pud_range(vma, pgd, addr, next, nodes,
31838e35860SChristoph Lameter 				    flags, private))
31991612e0dSHugh Dickins 			return -EIO;
32091612e0dSHugh Dickins 	} while (pgd++, addr = next, addr != end);
32191612e0dSHugh Dickins 	return 0;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324dc9aa5b9SChristoph Lameter /* Check if a vma is migratable */
325dc9aa5b9SChristoph Lameter static inline int vma_migratable(struct vm_area_struct *vma)
326dc9aa5b9SChristoph Lameter {
327dc9aa5b9SChristoph Lameter 	if (vma->vm_flags & (
328f4598c8bSChristoph Lameter 		VM_LOCKED|VM_IO|VM_HUGETLB|VM_PFNMAP|VM_RESERVED))
329dc9aa5b9SChristoph Lameter 		return 0;
330dc9aa5b9SChristoph Lameter 	return 1;
331dc9aa5b9SChristoph Lameter }
332dc9aa5b9SChristoph Lameter 
333dc9aa5b9SChristoph Lameter /*
334dc9aa5b9SChristoph Lameter  * Check if all pages in a range are on a set of nodes.
335dc9aa5b9SChristoph Lameter  * If pagelist != NULL then isolate pages from the LRU and
336dc9aa5b9SChristoph Lameter  * put them on the pagelist.
337dc9aa5b9SChristoph Lameter  */
3381da177e4SLinus Torvalds static struct vm_area_struct *
3391da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
34038e35860SChristoph Lameter 		const nodemask_t *nodes, unsigned long flags, void *private)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	int err;
3431da177e4SLinus Torvalds 	struct vm_area_struct *first, *vma, *prev;
3441da177e4SLinus Torvalds 
34590036ee5SChristoph Lameter 	if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
34690036ee5SChristoph Lameter 
347b20a3503SChristoph Lameter 		err = migrate_prep();
348b20a3503SChristoph Lameter 		if (err)
349b20a3503SChristoph Lameter 			return ERR_PTR(err);
35090036ee5SChristoph Lameter 	}
351053837fcSNick Piggin 
3521da177e4SLinus Torvalds 	first = find_vma(mm, start);
3531da177e4SLinus Torvalds 	if (!first)
3541da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
3551da177e4SLinus Torvalds 	prev = NULL;
3561da177e4SLinus Torvalds 	for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
357dc9aa5b9SChristoph Lameter 		if (!(flags & MPOL_MF_DISCONTIG_OK)) {
3581da177e4SLinus Torvalds 			if (!vma->vm_next && vma->vm_end < end)
3591da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
3601da177e4SLinus Torvalds 			if (prev && prev->vm_end < vma->vm_start)
3611da177e4SLinus Torvalds 				return ERR_PTR(-EFAULT);
362dc9aa5b9SChristoph Lameter 		}
363dc9aa5b9SChristoph Lameter 		if (!is_vm_hugetlb_page(vma) &&
364dc9aa5b9SChristoph Lameter 		    ((flags & MPOL_MF_STRICT) ||
365dc9aa5b9SChristoph Lameter 		     ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
366dc9aa5b9SChristoph Lameter 				vma_migratable(vma)))) {
3675b952b3cSAndi Kleen 			unsigned long endvma = vma->vm_end;
368dc9aa5b9SChristoph Lameter 
3695b952b3cSAndi Kleen 			if (endvma > end)
3705b952b3cSAndi Kleen 				endvma = end;
3715b952b3cSAndi Kleen 			if (vma->vm_start > start)
3725b952b3cSAndi Kleen 				start = vma->vm_start;
373dc9aa5b9SChristoph Lameter 			err = check_pgd_range(vma, start, endvma, nodes,
37438e35860SChristoph Lameter 						flags, private);
3751da177e4SLinus Torvalds 			if (err) {
3761da177e4SLinus Torvalds 				first = ERR_PTR(err);
3771da177e4SLinus Torvalds 				break;
3781da177e4SLinus Torvalds 			}
3791da177e4SLinus Torvalds 		}
3801da177e4SLinus Torvalds 		prev = vma;
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 	return first;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /* Apply policy to a single VMA */
3861da177e4SLinus Torvalds static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	int err = 0;
3891da177e4SLinus Torvalds 	struct mempolicy *old = vma->vm_policy;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	PDprintk("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
3921da177e4SLinus Torvalds 		 vma->vm_start, vma->vm_end, vma->vm_pgoff,
3931da177e4SLinus Torvalds 		 vma->vm_ops, vma->vm_file,
3941da177e4SLinus Torvalds 		 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->set_policy)
3971da177e4SLinus Torvalds 		err = vma->vm_ops->set_policy(vma, new);
3981da177e4SLinus Torvalds 	if (!err) {
3991da177e4SLinus Torvalds 		mpol_get(new);
4001da177e4SLinus Torvalds 		vma->vm_policy = new;
4011da177e4SLinus Torvalds 		mpol_free(old);
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	return err;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */
4071da177e4SLinus Torvalds static int mbind_range(struct vm_area_struct *vma, unsigned long start,
4081da177e4SLinus Torvalds 		       unsigned long end, struct mempolicy *new)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct vm_area_struct *next;
4111da177e4SLinus Torvalds 	int err;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	err = 0;
4141da177e4SLinus Torvalds 	for (; vma && vma->vm_start < end; vma = next) {
4151da177e4SLinus Torvalds 		next = vma->vm_next;
4161da177e4SLinus Torvalds 		if (vma->vm_start < start)
4171da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, start, 1);
4181da177e4SLinus Torvalds 		if (!err && vma->vm_end > end)
4191da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, end, 0);
4201da177e4SLinus Torvalds 		if (!err)
4211da177e4SLinus Torvalds 			err = policy_vma(vma, new);
4221da177e4SLinus Torvalds 		if (err)
4231da177e4SLinus Torvalds 			break;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	return err;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4288bccd85fSChristoph Lameter static int contextualize_policy(int mode, nodemask_t *nodes)
4298bccd85fSChristoph Lameter {
4308bccd85fSChristoph Lameter 	if (!nodes)
4318bccd85fSChristoph Lameter 		return 0;
4328bccd85fSChristoph Lameter 
433cf2a473cSPaul Jackson 	cpuset_update_task_memory_state();
4345966514dSPaul Jackson 	if (!cpuset_nodes_subset_current_mems_allowed(*nodes))
4355966514dSPaul Jackson 		return -EINVAL;
4368bccd85fSChristoph Lameter 	return mpol_check_policy(mode, nodes);
4378bccd85fSChristoph Lameter }
4388bccd85fSChristoph Lameter 
439c61afb18SPaul Jackson 
440c61afb18SPaul Jackson /*
441c61afb18SPaul Jackson  * Update task->flags PF_MEMPOLICY bit: set iff non-default
442c61afb18SPaul Jackson  * mempolicy.  Allows more rapid checking of this (combined perhaps
443c61afb18SPaul Jackson  * with other PF_* flag bits) on memory allocation hot code paths.
444c61afb18SPaul Jackson  *
445c61afb18SPaul Jackson  * If called from outside this file, the task 'p' should -only- be
446c61afb18SPaul Jackson  * a newly forked child not yet visible on the task list, because
447c61afb18SPaul Jackson  * manipulating the task flags of a visible task is not safe.
448c61afb18SPaul Jackson  *
449c61afb18SPaul Jackson  * The above limitation is why this routine has the funny name
450c61afb18SPaul Jackson  * mpol_fix_fork_child_flag().
451c61afb18SPaul Jackson  *
452c61afb18SPaul Jackson  * It is also safe to call this with a task pointer of current,
453c61afb18SPaul Jackson  * which the static wrapper mpol_set_task_struct_flag() does,
454c61afb18SPaul Jackson  * for use within this file.
455c61afb18SPaul Jackson  */
456c61afb18SPaul Jackson 
457c61afb18SPaul Jackson void mpol_fix_fork_child_flag(struct task_struct *p)
458c61afb18SPaul Jackson {
459c61afb18SPaul Jackson 	if (p->mempolicy)
460c61afb18SPaul Jackson 		p->flags |= PF_MEMPOLICY;
461c61afb18SPaul Jackson 	else
462c61afb18SPaul Jackson 		p->flags &= ~PF_MEMPOLICY;
463c61afb18SPaul Jackson }
464c61afb18SPaul Jackson 
465c61afb18SPaul Jackson static void mpol_set_task_struct_flag(void)
466c61afb18SPaul Jackson {
467c61afb18SPaul Jackson 	mpol_fix_fork_child_flag(current);
468c61afb18SPaul Jackson }
469c61afb18SPaul Jackson 
4701da177e4SLinus Torvalds /* Set the process memory policy */
4718bccd85fSChristoph Lameter long do_set_mempolicy(int mode, nodemask_t *nodes)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	struct mempolicy *new;
4741da177e4SLinus Torvalds 
4758bccd85fSChristoph Lameter 	if (contextualize_policy(mode, nodes))
4761da177e4SLinus Torvalds 		return -EINVAL;
4778bccd85fSChristoph Lameter 	new = mpol_new(mode, nodes);
4781da177e4SLinus Torvalds 	if (IS_ERR(new))
4791da177e4SLinus Torvalds 		return PTR_ERR(new);
4801da177e4SLinus Torvalds 	mpol_free(current->mempolicy);
4811da177e4SLinus Torvalds 	current->mempolicy = new;
482c61afb18SPaul Jackson 	mpol_set_task_struct_flag();
4831da177e4SLinus Torvalds 	if (new && new->policy == MPOL_INTERLEAVE)
484dfcd3c0dSAndi Kleen 		current->il_next = first_node(new->v.nodes);
4851da177e4SLinus Torvalds 	return 0;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /* Fill a zone bitmap for a policy */
489dfcd3c0dSAndi Kleen static void get_zonemask(struct mempolicy *p, nodemask_t *nodes)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	int i;
4921da177e4SLinus Torvalds 
493dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
4941da177e4SLinus Torvalds 	switch (p->policy) {
4951da177e4SLinus Torvalds 	case MPOL_BIND:
4961da177e4SLinus Torvalds 		for (i = 0; p->v.zonelist->zones[i]; i++)
49789fa3024SChristoph Lameter 			node_set(zone_to_nid(p->v.zonelist->zones[i]),
4988bccd85fSChristoph Lameter 				*nodes);
4991da177e4SLinus Torvalds 		break;
5001da177e4SLinus Torvalds 	case MPOL_DEFAULT:
5011da177e4SLinus Torvalds 		break;
5021da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
503dfcd3c0dSAndi Kleen 		*nodes = p->v.nodes;
5041da177e4SLinus Torvalds 		break;
5051da177e4SLinus Torvalds 	case MPOL_PREFERRED:
5061da177e4SLinus Torvalds 		/* or use current node instead of online map? */
5071da177e4SLinus Torvalds 		if (p->v.preferred_node < 0)
508dfcd3c0dSAndi Kleen 			*nodes = node_online_map;
5091da177e4SLinus Torvalds 		else
510dfcd3c0dSAndi Kleen 			node_set(p->v.preferred_node, *nodes);
5111da177e4SLinus Torvalds 		break;
5121da177e4SLinus Torvalds 	default:
5131da177e4SLinus Torvalds 		BUG();
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	struct page *p;
5201da177e4SLinus Torvalds 	int err;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
5231da177e4SLinus Torvalds 	if (err >= 0) {
5241da177e4SLinus Torvalds 		err = page_to_nid(p);
5251da177e4SLinus Torvalds 		put_page(p);
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 	return err;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /* Retrieve NUMA policy */
5318bccd85fSChristoph Lameter long do_get_mempolicy(int *policy, nodemask_t *nmask,
5321da177e4SLinus Torvalds 			unsigned long addr, unsigned long flags)
5331da177e4SLinus Torvalds {
5348bccd85fSChristoph Lameter 	int err;
5351da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
5361da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
5371da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
5381da177e4SLinus Torvalds 
539cf2a473cSPaul Jackson 	cpuset_update_task_memory_state();
5401da177e4SLinus Torvalds 	if (flags & ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR))
5411da177e4SLinus Torvalds 		return -EINVAL;
5421da177e4SLinus Torvalds 	if (flags & MPOL_F_ADDR) {
5431da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
5441da177e4SLinus Torvalds 		vma = find_vma_intersection(mm, addr, addr+1);
5451da177e4SLinus Torvalds 		if (!vma) {
5461da177e4SLinus Torvalds 			up_read(&mm->mmap_sem);
5471da177e4SLinus Torvalds 			return -EFAULT;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
5501da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
5511da177e4SLinus Torvalds 		else
5521da177e4SLinus Torvalds 			pol = vma->vm_policy;
5531da177e4SLinus Torvalds 	} else if (addr)
5541da177e4SLinus Torvalds 		return -EINVAL;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (!pol)
5571da177e4SLinus Torvalds 		pol = &default_policy;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	if (flags & MPOL_F_NODE) {
5601da177e4SLinus Torvalds 		if (flags & MPOL_F_ADDR) {
5611da177e4SLinus Torvalds 			err = lookup_node(mm, addr);
5621da177e4SLinus Torvalds 			if (err < 0)
5631da177e4SLinus Torvalds 				goto out;
5648bccd85fSChristoph Lameter 			*policy = err;
5651da177e4SLinus Torvalds 		} else if (pol == current->mempolicy &&
5661da177e4SLinus Torvalds 				pol->policy == MPOL_INTERLEAVE) {
5678bccd85fSChristoph Lameter 			*policy = current->il_next;
5681da177e4SLinus Torvalds 		} else {
5691da177e4SLinus Torvalds 			err = -EINVAL;
5701da177e4SLinus Torvalds 			goto out;
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 	} else
5738bccd85fSChristoph Lameter 		*policy = pol->policy;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (vma) {
5761da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
5771da177e4SLinus Torvalds 		vma = NULL;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	err = 0;
5818bccd85fSChristoph Lameter 	if (nmask)
5828bccd85fSChristoph Lameter 		get_zonemask(pol, nmask);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds  out:
5851da177e4SLinus Torvalds 	if (vma)
5861da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
5871da177e4SLinus Torvalds 	return err;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
590b20a3503SChristoph Lameter #ifdef CONFIG_MIGRATION
5918bccd85fSChristoph Lameter /*
5926ce3c4c0SChristoph Lameter  * page migration
5936ce3c4c0SChristoph Lameter  */
594fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
595fc301289SChristoph Lameter 				unsigned long flags)
5966ce3c4c0SChristoph Lameter {
5976ce3c4c0SChristoph Lameter 	/*
598fc301289SChristoph Lameter 	 * Avoid migrating a page that is shared with others.
5996ce3c4c0SChristoph Lameter 	 */
600b20a3503SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1)
601b20a3503SChristoph Lameter 		isolate_lru_page(page, pagelist);
6026ce3c4c0SChristoph Lameter }
6036ce3c4c0SChristoph Lameter 
604742755a1SChristoph Lameter static struct page *new_node_page(struct page *page, unsigned long node, int **x)
60595a402c3SChristoph Lameter {
60695a402c3SChristoph Lameter 	return alloc_pages_node(node, GFP_HIGHUSER, 0);
60795a402c3SChristoph Lameter }
60895a402c3SChristoph Lameter 
6096ce3c4c0SChristoph Lameter /*
6107e2ab150SChristoph Lameter  * Migrate pages from one node to a target node.
6117e2ab150SChristoph Lameter  * Returns error or the number of pages not migrated.
6127e2ab150SChristoph Lameter  */
6137e2ab150SChristoph Lameter int migrate_to_node(struct mm_struct *mm, int source, int dest, int flags)
6147e2ab150SChristoph Lameter {
6157e2ab150SChristoph Lameter 	nodemask_t nmask;
6167e2ab150SChristoph Lameter 	LIST_HEAD(pagelist);
6177e2ab150SChristoph Lameter 	int err = 0;
6187e2ab150SChristoph Lameter 
6197e2ab150SChristoph Lameter 	nodes_clear(nmask);
6207e2ab150SChristoph Lameter 	node_set(source, nmask);
6217e2ab150SChristoph Lameter 
6227e2ab150SChristoph Lameter 	check_range(mm, mm->mmap->vm_start, TASK_SIZE, &nmask,
6237e2ab150SChristoph Lameter 			flags | MPOL_MF_DISCONTIG_OK, &pagelist);
6247e2ab150SChristoph Lameter 
6257e2ab150SChristoph Lameter 	if (!list_empty(&pagelist))
62695a402c3SChristoph Lameter 		err = migrate_pages(&pagelist, new_node_page, dest);
62795a402c3SChristoph Lameter 
6287e2ab150SChristoph Lameter 	return err;
6297e2ab150SChristoph Lameter }
6307e2ab150SChristoph Lameter 
6317e2ab150SChristoph Lameter /*
6327e2ab150SChristoph Lameter  * Move pages between the two nodesets so as to preserve the physical
6337e2ab150SChristoph Lameter  * layout as much as possible.
63439743889SChristoph Lameter  *
63539743889SChristoph Lameter  * Returns the number of page that could not be moved.
63639743889SChristoph Lameter  */
63739743889SChristoph Lameter int do_migrate_pages(struct mm_struct *mm,
63839743889SChristoph Lameter 	const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
63939743889SChristoph Lameter {
64039743889SChristoph Lameter 	LIST_HEAD(pagelist);
6417e2ab150SChristoph Lameter 	int busy = 0;
6427e2ab150SChristoph Lameter 	int err = 0;
6437e2ab150SChristoph Lameter 	nodemask_t tmp;
64439743889SChristoph Lameter 
64539743889SChristoph Lameter   	down_read(&mm->mmap_sem);
646d4984711SChristoph Lameter 
6477b2259b3SChristoph Lameter 	err = migrate_vmas(mm, from_nodes, to_nodes, flags);
6487b2259b3SChristoph Lameter 	if (err)
6497b2259b3SChristoph Lameter 		goto out;
6507b2259b3SChristoph Lameter 
6517e2ab150SChristoph Lameter /*
6527e2ab150SChristoph Lameter  * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
6537e2ab150SChristoph Lameter  * bit in 'to' is not also set in 'tmp'.  Clear the found 'source'
6547e2ab150SChristoph Lameter  * bit in 'tmp', and return that <source, dest> pair for migration.
6557e2ab150SChristoph Lameter  * The pair of nodemasks 'to' and 'from' define the map.
6567e2ab150SChristoph Lameter  *
6577e2ab150SChristoph Lameter  * If no pair of bits is found that way, fallback to picking some
6587e2ab150SChristoph Lameter  * pair of 'source' and 'dest' bits that are not the same.  If the
6597e2ab150SChristoph Lameter  * 'source' and 'dest' bits are the same, this represents a node
6607e2ab150SChristoph Lameter  * that will be migrating to itself, so no pages need move.
6617e2ab150SChristoph Lameter  *
6627e2ab150SChristoph Lameter  * If no bits are left in 'tmp', or if all remaining bits left
6637e2ab150SChristoph Lameter  * in 'tmp' correspond to the same bit in 'to', return false
6647e2ab150SChristoph Lameter  * (nothing left to migrate).
6657e2ab150SChristoph Lameter  *
6667e2ab150SChristoph Lameter  * This lets us pick a pair of nodes to migrate between, such that
6677e2ab150SChristoph Lameter  * if possible the dest node is not already occupied by some other
6687e2ab150SChristoph Lameter  * source node, minimizing the risk of overloading the memory on a
6697e2ab150SChristoph Lameter  * node that would happen if we migrated incoming memory to a node
6707e2ab150SChristoph Lameter  * before migrating outgoing memory source that same node.
6717e2ab150SChristoph Lameter  *
6727e2ab150SChristoph Lameter  * A single scan of tmp is sufficient.  As we go, we remember the
6737e2ab150SChristoph Lameter  * most recent <s, d> pair that moved (s != d).  If we find a pair
6747e2ab150SChristoph Lameter  * that not only moved, but what's better, moved to an empty slot
6757e2ab150SChristoph Lameter  * (d is not set in tmp), then we break out then, with that pair.
6767e2ab150SChristoph Lameter  * Otherwise when we finish scannng from_tmp, we at least have the
6777e2ab150SChristoph Lameter  * most recent <s, d> pair that moved.  If we get all the way through
6787e2ab150SChristoph Lameter  * the scan of tmp without finding any node that moved, much less
6797e2ab150SChristoph Lameter  * moved to an empty node, then there is nothing left worth migrating.
6807e2ab150SChristoph Lameter  */
6817e2ab150SChristoph Lameter 
6827e2ab150SChristoph Lameter 	tmp = *from_nodes;
6837e2ab150SChristoph Lameter 	while (!nodes_empty(tmp)) {
6847e2ab150SChristoph Lameter 		int s,d;
6857e2ab150SChristoph Lameter 		int source = -1;
6867e2ab150SChristoph Lameter 		int dest = 0;
6877e2ab150SChristoph Lameter 
6887e2ab150SChristoph Lameter 		for_each_node_mask(s, tmp) {
6897e2ab150SChristoph Lameter 			d = node_remap(s, *from_nodes, *to_nodes);
6907e2ab150SChristoph Lameter 			if (s == d)
6917e2ab150SChristoph Lameter 				continue;
6927e2ab150SChristoph Lameter 
6937e2ab150SChristoph Lameter 			source = s;	/* Node moved. Memorize */
6947e2ab150SChristoph Lameter 			dest = d;
6957e2ab150SChristoph Lameter 
6967e2ab150SChristoph Lameter 			/* dest not in remaining from nodes? */
6977e2ab150SChristoph Lameter 			if (!node_isset(dest, tmp))
6987e2ab150SChristoph Lameter 				break;
6997e2ab150SChristoph Lameter 		}
7007e2ab150SChristoph Lameter 		if (source == -1)
7017e2ab150SChristoph Lameter 			break;
7027e2ab150SChristoph Lameter 
7037e2ab150SChristoph Lameter 		node_clear(source, tmp);
7047e2ab150SChristoph Lameter 		err = migrate_to_node(mm, source, dest, flags);
7057e2ab150SChristoph Lameter 		if (err > 0)
7067e2ab150SChristoph Lameter 			busy += err;
7077e2ab150SChristoph Lameter 		if (err < 0)
7087e2ab150SChristoph Lameter 			break;
70939743889SChristoph Lameter 	}
7107b2259b3SChristoph Lameter out:
71139743889SChristoph Lameter 	up_read(&mm->mmap_sem);
7127e2ab150SChristoph Lameter 	if (err < 0)
7137e2ab150SChristoph Lameter 		return err;
7147e2ab150SChristoph Lameter 	return busy;
715b20a3503SChristoph Lameter 
71639743889SChristoph Lameter }
71739743889SChristoph Lameter 
718742755a1SChristoph Lameter static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
71995a402c3SChristoph Lameter {
72095a402c3SChristoph Lameter 	struct vm_area_struct *vma = (struct vm_area_struct *)private;
72195a402c3SChristoph Lameter 
72295a402c3SChristoph Lameter 	return alloc_page_vma(GFP_HIGHUSER, vma, page_address_in_vma(page, vma));
72395a402c3SChristoph Lameter }
724b20a3503SChristoph Lameter #else
725b20a3503SChristoph Lameter 
726b20a3503SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist,
727b20a3503SChristoph Lameter 				unsigned long flags)
728b20a3503SChristoph Lameter {
729b20a3503SChristoph Lameter }
730b20a3503SChristoph Lameter 
731b20a3503SChristoph Lameter int do_migrate_pages(struct mm_struct *mm,
732b20a3503SChristoph Lameter 	const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
733b20a3503SChristoph Lameter {
734b20a3503SChristoph Lameter 	return -ENOSYS;
735b20a3503SChristoph Lameter }
73695a402c3SChristoph Lameter 
73769939749SKeith Owens static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
73895a402c3SChristoph Lameter {
73995a402c3SChristoph Lameter 	return NULL;
74095a402c3SChristoph Lameter }
741b20a3503SChristoph Lameter #endif
742b20a3503SChristoph Lameter 
7436ce3c4c0SChristoph Lameter long do_mbind(unsigned long start, unsigned long len,
7446ce3c4c0SChristoph Lameter 		unsigned long mode, nodemask_t *nmask, unsigned long flags)
7456ce3c4c0SChristoph Lameter {
7466ce3c4c0SChristoph Lameter 	struct vm_area_struct *vma;
7476ce3c4c0SChristoph Lameter 	struct mm_struct *mm = current->mm;
7486ce3c4c0SChristoph Lameter 	struct mempolicy *new;
7496ce3c4c0SChristoph Lameter 	unsigned long end;
7506ce3c4c0SChristoph Lameter 	int err;
7516ce3c4c0SChristoph Lameter 	LIST_HEAD(pagelist);
7526ce3c4c0SChristoph Lameter 
7536ce3c4c0SChristoph Lameter 	if ((flags & ~(unsigned long)(MPOL_MF_STRICT |
7546ce3c4c0SChristoph Lameter 				      MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
7556ce3c4c0SChristoph Lameter 	    || mode > MPOL_MAX)
7566ce3c4c0SChristoph Lameter 		return -EINVAL;
75774c00241SChristoph Lameter 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
7586ce3c4c0SChristoph Lameter 		return -EPERM;
7596ce3c4c0SChristoph Lameter 
7606ce3c4c0SChristoph Lameter 	if (start & ~PAGE_MASK)
7616ce3c4c0SChristoph Lameter 		return -EINVAL;
7626ce3c4c0SChristoph Lameter 
7636ce3c4c0SChristoph Lameter 	if (mode == MPOL_DEFAULT)
7646ce3c4c0SChristoph Lameter 		flags &= ~MPOL_MF_STRICT;
7656ce3c4c0SChristoph Lameter 
7666ce3c4c0SChristoph Lameter 	len = (len + PAGE_SIZE - 1) & PAGE_MASK;
7676ce3c4c0SChristoph Lameter 	end = start + len;
7686ce3c4c0SChristoph Lameter 
7696ce3c4c0SChristoph Lameter 	if (end < start)
7706ce3c4c0SChristoph Lameter 		return -EINVAL;
7716ce3c4c0SChristoph Lameter 	if (end == start)
7726ce3c4c0SChristoph Lameter 		return 0;
7736ce3c4c0SChristoph Lameter 
7746ce3c4c0SChristoph Lameter 	if (mpol_check_policy(mode, nmask))
7756ce3c4c0SChristoph Lameter 		return -EINVAL;
7766ce3c4c0SChristoph Lameter 
7776ce3c4c0SChristoph Lameter 	new = mpol_new(mode, nmask);
7786ce3c4c0SChristoph Lameter 	if (IS_ERR(new))
7796ce3c4c0SChristoph Lameter 		return PTR_ERR(new);
7806ce3c4c0SChristoph Lameter 
7816ce3c4c0SChristoph Lameter 	/*
7826ce3c4c0SChristoph Lameter 	 * If we are using the default policy then operation
7836ce3c4c0SChristoph Lameter 	 * on discontinuous address spaces is okay after all
7846ce3c4c0SChristoph Lameter 	 */
7856ce3c4c0SChristoph Lameter 	if (!new)
7866ce3c4c0SChristoph Lameter 		flags |= MPOL_MF_DISCONTIG_OK;
7876ce3c4c0SChristoph Lameter 
7886ce3c4c0SChristoph Lameter 	PDprintk("mbind %lx-%lx mode:%ld nodes:%lx\n",start,start+len,
7896ce3c4c0SChristoph Lameter 			mode,nodes_addr(nodes)[0]);
7906ce3c4c0SChristoph Lameter 
7916ce3c4c0SChristoph Lameter 	down_write(&mm->mmap_sem);
7926ce3c4c0SChristoph Lameter 	vma = check_range(mm, start, end, nmask,
7936ce3c4c0SChristoph Lameter 			  flags | MPOL_MF_INVERT, &pagelist);
7946ce3c4c0SChristoph Lameter 
7956ce3c4c0SChristoph Lameter 	err = PTR_ERR(vma);
7966ce3c4c0SChristoph Lameter 	if (!IS_ERR(vma)) {
7976ce3c4c0SChristoph Lameter 		int nr_failed = 0;
7986ce3c4c0SChristoph Lameter 
7996ce3c4c0SChristoph Lameter 		err = mbind_range(vma, start, end, new);
8007e2ab150SChristoph Lameter 
8016ce3c4c0SChristoph Lameter 		if (!list_empty(&pagelist))
80295a402c3SChristoph Lameter 			nr_failed = migrate_pages(&pagelist, new_vma_page,
80395a402c3SChristoph Lameter 						(unsigned long)vma);
8046ce3c4c0SChristoph Lameter 
8056ce3c4c0SChristoph Lameter 		if (!err && nr_failed && (flags & MPOL_MF_STRICT))
8066ce3c4c0SChristoph Lameter 			err = -EIO;
8076ce3c4c0SChristoph Lameter 	}
808b20a3503SChristoph Lameter 
8096ce3c4c0SChristoph Lameter 	up_write(&mm->mmap_sem);
8106ce3c4c0SChristoph Lameter 	mpol_free(new);
8116ce3c4c0SChristoph Lameter 	return err;
8126ce3c4c0SChristoph Lameter }
8136ce3c4c0SChristoph Lameter 
81439743889SChristoph Lameter /*
8158bccd85fSChristoph Lameter  * User space interface with variable sized bitmaps for nodelists.
8168bccd85fSChristoph Lameter  */
8178bccd85fSChristoph Lameter 
8188bccd85fSChristoph Lameter /* Copy a node mask from user space. */
81939743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
8208bccd85fSChristoph Lameter 		     unsigned long maxnode)
8218bccd85fSChristoph Lameter {
8228bccd85fSChristoph Lameter 	unsigned long k;
8238bccd85fSChristoph Lameter 	unsigned long nlongs;
8248bccd85fSChristoph Lameter 	unsigned long endmask;
8258bccd85fSChristoph Lameter 
8268bccd85fSChristoph Lameter 	--maxnode;
8278bccd85fSChristoph Lameter 	nodes_clear(*nodes);
8288bccd85fSChristoph Lameter 	if (maxnode == 0 || !nmask)
8298bccd85fSChristoph Lameter 		return 0;
830a9c930baSAndi Kleen 	if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
831636f13c1SChris Wright 		return -EINVAL;
8328bccd85fSChristoph Lameter 
8338bccd85fSChristoph Lameter 	nlongs = BITS_TO_LONGS(maxnode);
8348bccd85fSChristoph Lameter 	if ((maxnode % BITS_PER_LONG) == 0)
8358bccd85fSChristoph Lameter 		endmask = ~0UL;
8368bccd85fSChristoph Lameter 	else
8378bccd85fSChristoph Lameter 		endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
8388bccd85fSChristoph Lameter 
8398bccd85fSChristoph Lameter 	/* When the user specified more nodes than supported just check
8408bccd85fSChristoph Lameter 	   if the non supported part is all zero. */
8418bccd85fSChristoph Lameter 	if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
8428bccd85fSChristoph Lameter 		if (nlongs > PAGE_SIZE/sizeof(long))
8438bccd85fSChristoph Lameter 			return -EINVAL;
8448bccd85fSChristoph Lameter 		for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
8458bccd85fSChristoph Lameter 			unsigned long t;
8468bccd85fSChristoph Lameter 			if (get_user(t, nmask + k))
8478bccd85fSChristoph Lameter 				return -EFAULT;
8488bccd85fSChristoph Lameter 			if (k == nlongs - 1) {
8498bccd85fSChristoph Lameter 				if (t & endmask)
8508bccd85fSChristoph Lameter 					return -EINVAL;
8518bccd85fSChristoph Lameter 			} else if (t)
8528bccd85fSChristoph Lameter 				return -EINVAL;
8538bccd85fSChristoph Lameter 		}
8548bccd85fSChristoph Lameter 		nlongs = BITS_TO_LONGS(MAX_NUMNODES);
8558bccd85fSChristoph Lameter 		endmask = ~0UL;
8568bccd85fSChristoph Lameter 	}
8578bccd85fSChristoph Lameter 
8588bccd85fSChristoph Lameter 	if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
8598bccd85fSChristoph Lameter 		return -EFAULT;
8608bccd85fSChristoph Lameter 	nodes_addr(*nodes)[nlongs-1] &= endmask;
8618bccd85fSChristoph Lameter 	return 0;
8628bccd85fSChristoph Lameter }
8638bccd85fSChristoph Lameter 
8648bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */
8658bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
8668bccd85fSChristoph Lameter 			      nodemask_t *nodes)
8678bccd85fSChristoph Lameter {
8688bccd85fSChristoph Lameter 	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
8698bccd85fSChristoph Lameter 	const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
8708bccd85fSChristoph Lameter 
8718bccd85fSChristoph Lameter 	if (copy > nbytes) {
8728bccd85fSChristoph Lameter 		if (copy > PAGE_SIZE)
8738bccd85fSChristoph Lameter 			return -EINVAL;
8748bccd85fSChristoph Lameter 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
8758bccd85fSChristoph Lameter 			return -EFAULT;
8768bccd85fSChristoph Lameter 		copy = nbytes;
8778bccd85fSChristoph Lameter 	}
8788bccd85fSChristoph Lameter 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
8798bccd85fSChristoph Lameter }
8808bccd85fSChristoph Lameter 
8818bccd85fSChristoph Lameter asmlinkage long sys_mbind(unsigned long start, unsigned long len,
8828bccd85fSChristoph Lameter 			unsigned long mode,
8838bccd85fSChristoph Lameter 			unsigned long __user *nmask, unsigned long maxnode,
8848bccd85fSChristoph Lameter 			unsigned flags)
8858bccd85fSChristoph Lameter {
8868bccd85fSChristoph Lameter 	nodemask_t nodes;
8878bccd85fSChristoph Lameter 	int err;
8888bccd85fSChristoph Lameter 
8898bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
8908bccd85fSChristoph Lameter 	if (err)
8918bccd85fSChristoph Lameter 		return err;
89230150f8dSChristoph Lameter #ifdef CONFIG_CPUSETS
89330150f8dSChristoph Lameter 	/* Restrict the nodes to the allowed nodes in the cpuset */
89430150f8dSChristoph Lameter 	nodes_and(nodes, nodes, current->mems_allowed);
89530150f8dSChristoph Lameter #endif
8968bccd85fSChristoph Lameter 	return do_mbind(start, len, mode, &nodes, flags);
8978bccd85fSChristoph Lameter }
8988bccd85fSChristoph Lameter 
8998bccd85fSChristoph Lameter /* Set the process memory policy */
9008bccd85fSChristoph Lameter asmlinkage long sys_set_mempolicy(int mode, unsigned long __user *nmask,
9018bccd85fSChristoph Lameter 		unsigned long maxnode)
9028bccd85fSChristoph Lameter {
9038bccd85fSChristoph Lameter 	int err;
9048bccd85fSChristoph Lameter 	nodemask_t nodes;
9058bccd85fSChristoph Lameter 
9068bccd85fSChristoph Lameter 	if (mode < 0 || mode > MPOL_MAX)
9078bccd85fSChristoph Lameter 		return -EINVAL;
9088bccd85fSChristoph Lameter 	err = get_nodes(&nodes, nmask, maxnode);
9098bccd85fSChristoph Lameter 	if (err)
9108bccd85fSChristoph Lameter 		return err;
9118bccd85fSChristoph Lameter 	return do_set_mempolicy(mode, &nodes);
9128bccd85fSChristoph Lameter }
9138bccd85fSChristoph Lameter 
91439743889SChristoph Lameter asmlinkage long sys_migrate_pages(pid_t pid, unsigned long maxnode,
91539743889SChristoph Lameter 		const unsigned long __user *old_nodes,
91639743889SChristoph Lameter 		const unsigned long __user *new_nodes)
91739743889SChristoph Lameter {
91839743889SChristoph Lameter 	struct mm_struct *mm;
91939743889SChristoph Lameter 	struct task_struct *task;
92039743889SChristoph Lameter 	nodemask_t old;
92139743889SChristoph Lameter 	nodemask_t new;
92239743889SChristoph Lameter 	nodemask_t task_nodes;
92339743889SChristoph Lameter 	int err;
92439743889SChristoph Lameter 
92539743889SChristoph Lameter 	err = get_nodes(&old, old_nodes, maxnode);
92639743889SChristoph Lameter 	if (err)
92739743889SChristoph Lameter 		return err;
92839743889SChristoph Lameter 
92939743889SChristoph Lameter 	err = get_nodes(&new, new_nodes, maxnode);
93039743889SChristoph Lameter 	if (err)
93139743889SChristoph Lameter 		return err;
93239743889SChristoph Lameter 
93339743889SChristoph Lameter 	/* Find the mm_struct */
93439743889SChristoph Lameter 	read_lock(&tasklist_lock);
93539743889SChristoph Lameter 	task = pid ? find_task_by_pid(pid) : current;
93639743889SChristoph Lameter 	if (!task) {
93739743889SChristoph Lameter 		read_unlock(&tasklist_lock);
93839743889SChristoph Lameter 		return -ESRCH;
93939743889SChristoph Lameter 	}
94039743889SChristoph Lameter 	mm = get_task_mm(task);
94139743889SChristoph Lameter 	read_unlock(&tasklist_lock);
94239743889SChristoph Lameter 
94339743889SChristoph Lameter 	if (!mm)
94439743889SChristoph Lameter 		return -EINVAL;
94539743889SChristoph Lameter 
94639743889SChristoph Lameter 	/*
94739743889SChristoph Lameter 	 * Check if this process has the right to modify the specified
94839743889SChristoph Lameter 	 * process. The right exists if the process has administrative
9497f927fccSAlexey Dobriyan 	 * capabilities, superuser privileges or the same
95039743889SChristoph Lameter 	 * userid as the target process.
95139743889SChristoph Lameter 	 */
95239743889SChristoph Lameter 	if ((current->euid != task->suid) && (current->euid != task->uid) &&
95339743889SChristoph Lameter 	    (current->uid != task->suid) && (current->uid != task->uid) &&
95474c00241SChristoph Lameter 	    !capable(CAP_SYS_NICE)) {
95539743889SChristoph Lameter 		err = -EPERM;
95639743889SChristoph Lameter 		goto out;
95739743889SChristoph Lameter 	}
95839743889SChristoph Lameter 
95939743889SChristoph Lameter 	task_nodes = cpuset_mems_allowed(task);
96039743889SChristoph Lameter 	/* Is the user allowed to access the target nodes? */
96174c00241SChristoph Lameter 	if (!nodes_subset(new, task_nodes) && !capable(CAP_SYS_NICE)) {
96239743889SChristoph Lameter 		err = -EPERM;
96339743889SChristoph Lameter 		goto out;
96439743889SChristoph Lameter 	}
96539743889SChristoph Lameter 
96686c3a764SDavid Quigley 	err = security_task_movememory(task);
96786c3a764SDavid Quigley 	if (err)
96886c3a764SDavid Quigley 		goto out;
96986c3a764SDavid Quigley 
970511030bcSChristoph Lameter 	err = do_migrate_pages(mm, &old, &new,
97174c00241SChristoph Lameter 		capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
97239743889SChristoph Lameter out:
97339743889SChristoph Lameter 	mmput(mm);
97439743889SChristoph Lameter 	return err;
97539743889SChristoph Lameter }
97639743889SChristoph Lameter 
97739743889SChristoph Lameter 
9788bccd85fSChristoph Lameter /* Retrieve NUMA policy */
9798bccd85fSChristoph Lameter asmlinkage long sys_get_mempolicy(int __user *policy,
9808bccd85fSChristoph Lameter 				unsigned long __user *nmask,
9818bccd85fSChristoph Lameter 				unsigned long maxnode,
9828bccd85fSChristoph Lameter 				unsigned long addr, unsigned long flags)
9838bccd85fSChristoph Lameter {
9848bccd85fSChristoph Lameter 	int err, pval;
9858bccd85fSChristoph Lameter 	nodemask_t nodes;
9868bccd85fSChristoph Lameter 
9878bccd85fSChristoph Lameter 	if (nmask != NULL && maxnode < MAX_NUMNODES)
9888bccd85fSChristoph Lameter 		return -EINVAL;
9898bccd85fSChristoph Lameter 
9908bccd85fSChristoph Lameter 	err = do_get_mempolicy(&pval, &nodes, addr, flags);
9918bccd85fSChristoph Lameter 
9928bccd85fSChristoph Lameter 	if (err)
9938bccd85fSChristoph Lameter 		return err;
9948bccd85fSChristoph Lameter 
9958bccd85fSChristoph Lameter 	if (policy && put_user(pval, policy))
9968bccd85fSChristoph Lameter 		return -EFAULT;
9978bccd85fSChristoph Lameter 
9988bccd85fSChristoph Lameter 	if (nmask)
9998bccd85fSChristoph Lameter 		err = copy_nodes_to_user(nmask, maxnode, &nodes);
10008bccd85fSChristoph Lameter 
10018bccd85fSChristoph Lameter 	return err;
10028bccd85fSChristoph Lameter }
10038bccd85fSChristoph Lameter 
10041da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy,
10071da177e4SLinus Torvalds 				     compat_ulong_t __user *nmask,
10081da177e4SLinus Torvalds 				     compat_ulong_t maxnode,
10091da177e4SLinus Torvalds 				     compat_ulong_t addr, compat_ulong_t flags)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	long err;
10121da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
10131da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
10141da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
10171da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (nmask)
10201da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	if (!err && nmask) {
10251da177e4SLinus Torvalds 		err = copy_from_user(bm, nm, alloc_size);
10261da177e4SLinus Torvalds 		/* ensure entire bitmap is zeroed */
10271da177e4SLinus Torvalds 		err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
10281da177e4SLinus Torvalds 		err |= compat_put_bitmap(nmask, bm, nr_bits);
10291da177e4SLinus Torvalds 	}
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	return err;
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
10351da177e4SLinus Torvalds 				     compat_ulong_t maxnode)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	long err = 0;
10381da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
10391da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
10401da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
10431da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	if (nmask) {
10461da177e4SLinus Torvalds 		err = compat_get_bitmap(bm, nmask, nr_bits);
10471da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
10481da177e4SLinus Torvalds 		err |= copy_to_user(nm, bm, alloc_size);
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	if (err)
10521da177e4SLinus Torvalds 		return -EFAULT;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	return sys_set_mempolicy(mode, nm, nr_bits+1);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
10581da177e4SLinus Torvalds 			     compat_ulong_t mode, compat_ulong_t __user *nmask,
10591da177e4SLinus Torvalds 			     compat_ulong_t maxnode, compat_ulong_t flags)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	long err = 0;
10621da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
10631da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
1064dfcd3c0dSAndi Kleen 	nodemask_t bm;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
10671da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if (nmask) {
1070dfcd3c0dSAndi Kleen 		err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
10711da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
1072dfcd3c0dSAndi Kleen 		err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	if (err)
10761da177e4SLinus Torvalds 		return -EFAULT;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds #endif
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds /* Return effective policy for a VMA */
108448fce342SChristoph Lameter static struct mempolicy * get_vma_policy(struct task_struct *task,
108548fce342SChristoph Lameter 		struct vm_area_struct *vma, unsigned long addr)
10861da177e4SLinus Torvalds {
10876e21c8f1SChristoph Lameter 	struct mempolicy *pol = task->mempolicy;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	if (vma) {
10901da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
10911da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
10921da177e4SLinus Torvalds 		else if (vma->vm_policy &&
10931da177e4SLinus Torvalds 				vma->vm_policy->policy != MPOL_DEFAULT)
10941da177e4SLinus Torvalds 			pol = vma->vm_policy;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 	if (!pol)
10971da177e4SLinus Torvalds 		pol = &default_policy;
10981da177e4SLinus Torvalds 	return pol;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds /* Return a zonelist representing a mempolicy */
1102dd0fc66fSAl Viro static struct zonelist *zonelist_policy(gfp_t gfp, struct mempolicy *policy)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	int nd;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	switch (policy->policy) {
11071da177e4SLinus Torvalds 	case MPOL_PREFERRED:
11081da177e4SLinus Torvalds 		nd = policy->v.preferred_node;
11091da177e4SLinus Torvalds 		if (nd < 0)
11101da177e4SLinus Torvalds 			nd = numa_node_id();
11111da177e4SLinus Torvalds 		break;
11121da177e4SLinus Torvalds 	case MPOL_BIND:
11131da177e4SLinus Torvalds 		/* Lower zones don't get a policy applied */
11141da177e4SLinus Torvalds 		/* Careful: current->mems_allowed might have moved */
111519655d34SChristoph Lameter 		if (gfp_zone(gfp) >= policy_zone)
11161da177e4SLinus Torvalds 			if (cpuset_zonelist_valid_mems_allowed(policy->v.zonelist))
11171da177e4SLinus Torvalds 				return policy->v.zonelist;
11181da177e4SLinus Torvalds 		/*FALL THROUGH*/
11191da177e4SLinus Torvalds 	case MPOL_INTERLEAVE: /* should not happen */
11201da177e4SLinus Torvalds 	case MPOL_DEFAULT:
11211da177e4SLinus Torvalds 		nd = numa_node_id();
11221da177e4SLinus Torvalds 		break;
11231da177e4SLinus Torvalds 	default:
11241da177e4SLinus Torvalds 		nd = 0;
11251da177e4SLinus Torvalds 		BUG();
11261da177e4SLinus Torvalds 	}
1127af4ca457SAl Viro 	return NODE_DATA(nd)->node_zonelists + gfp_zone(gfp);
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds /* Do dynamic interleaving for a process */
11311da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	unsigned nid, next;
11341da177e4SLinus Torvalds 	struct task_struct *me = current;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	nid = me->il_next;
1137dfcd3c0dSAndi Kleen 	next = next_node(nid, policy->v.nodes);
11381da177e4SLinus Torvalds 	if (next >= MAX_NUMNODES)
1139dfcd3c0dSAndi Kleen 		next = first_node(policy->v.nodes);
11401da177e4SLinus Torvalds 	me->il_next = next;
11411da177e4SLinus Torvalds 	return nid;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
1144dc85da15SChristoph Lameter /*
1145dc85da15SChristoph Lameter  * Depending on the memory policy provide a node from which to allocate the
1146dc85da15SChristoph Lameter  * next slab entry.
1147dc85da15SChristoph Lameter  */
1148dc85da15SChristoph Lameter unsigned slab_node(struct mempolicy *policy)
1149dc85da15SChristoph Lameter {
1150765c4507SChristoph Lameter 	int pol = policy ? policy->policy : MPOL_DEFAULT;
1151765c4507SChristoph Lameter 
1152765c4507SChristoph Lameter 	switch (pol) {
1153dc85da15SChristoph Lameter 	case MPOL_INTERLEAVE:
1154dc85da15SChristoph Lameter 		return interleave_nodes(policy);
1155dc85da15SChristoph Lameter 
1156dc85da15SChristoph Lameter 	case MPOL_BIND:
1157dc85da15SChristoph Lameter 		/*
1158dc85da15SChristoph Lameter 		 * Follow bind policy behavior and start allocation at the
1159dc85da15SChristoph Lameter 		 * first node.
1160dc85da15SChristoph Lameter 		 */
116189fa3024SChristoph Lameter 		return zone_to_nid(policy->v.zonelist->zones[0]);
1162dc85da15SChristoph Lameter 
1163dc85da15SChristoph Lameter 	case MPOL_PREFERRED:
1164dc85da15SChristoph Lameter 		if (policy->v.preferred_node >= 0)
1165dc85da15SChristoph Lameter 			return policy->v.preferred_node;
1166dc85da15SChristoph Lameter 		/* Fall through */
1167dc85da15SChristoph Lameter 
1168dc85da15SChristoph Lameter 	default:
1169dc85da15SChristoph Lameter 		return numa_node_id();
1170dc85da15SChristoph Lameter 	}
1171dc85da15SChristoph Lameter }
1172dc85da15SChristoph Lameter 
11731da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */
11741da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol,
11751da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long off)
11761da177e4SLinus Torvalds {
1177dfcd3c0dSAndi Kleen 	unsigned nnodes = nodes_weight(pol->v.nodes);
11781da177e4SLinus Torvalds 	unsigned target = (unsigned)off % nnodes;
11791da177e4SLinus Torvalds 	int c;
11801da177e4SLinus Torvalds 	int nid = -1;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	c = 0;
11831da177e4SLinus Torvalds 	do {
1184dfcd3c0dSAndi Kleen 		nid = next_node(nid, pol->v.nodes);
11851da177e4SLinus Torvalds 		c++;
11861da177e4SLinus Torvalds 	} while (c <= target);
11871da177e4SLinus Torvalds 	return nid;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11905da7ca86SChristoph Lameter /* Determine a node number for interleave */
11915da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol,
11925da7ca86SChristoph Lameter 		 struct vm_area_struct *vma, unsigned long addr, int shift)
11935da7ca86SChristoph Lameter {
11945da7ca86SChristoph Lameter 	if (vma) {
11955da7ca86SChristoph Lameter 		unsigned long off;
11965da7ca86SChristoph Lameter 
11973b98b087SNishanth Aravamudan 		/*
11983b98b087SNishanth Aravamudan 		 * for small pages, there is no difference between
11993b98b087SNishanth Aravamudan 		 * shift and PAGE_SHIFT, so the bit-shift is safe.
12003b98b087SNishanth Aravamudan 		 * for huge pages, since vm_pgoff is in units of small
12013b98b087SNishanth Aravamudan 		 * pages, we need to shift off the always 0 bits to get
12023b98b087SNishanth Aravamudan 		 * a useful offset.
12033b98b087SNishanth Aravamudan 		 */
12043b98b087SNishanth Aravamudan 		BUG_ON(shift < PAGE_SHIFT);
12053b98b087SNishanth Aravamudan 		off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
12065da7ca86SChristoph Lameter 		off += (addr - vma->vm_start) >> shift;
12075da7ca86SChristoph Lameter 		return offset_il_node(pol, vma, off);
12085da7ca86SChristoph Lameter 	} else
12095da7ca86SChristoph Lameter 		return interleave_nodes(pol);
12105da7ca86SChristoph Lameter }
12115da7ca86SChristoph Lameter 
121200ac59adSChen, Kenneth W #ifdef CONFIG_HUGETLBFS
12135da7ca86SChristoph Lameter /* Return a zonelist suitable for a huge page allocation. */
12145da7ca86SChristoph Lameter struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr)
12155da7ca86SChristoph Lameter {
12165da7ca86SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
12175da7ca86SChristoph Lameter 
12185da7ca86SChristoph Lameter 	if (pol->policy == MPOL_INTERLEAVE) {
12195da7ca86SChristoph Lameter 		unsigned nid;
12205da7ca86SChristoph Lameter 
12215da7ca86SChristoph Lameter 		nid = interleave_nid(pol, vma, addr, HPAGE_SHIFT);
12225da7ca86SChristoph Lameter 		return NODE_DATA(nid)->node_zonelists + gfp_zone(GFP_HIGHUSER);
12235da7ca86SChristoph Lameter 	}
12245da7ca86SChristoph Lameter 	return zonelist_policy(GFP_HIGHUSER, pol);
12255da7ca86SChristoph Lameter }
122600ac59adSChen, Kenneth W #endif
12275da7ca86SChristoph Lameter 
12281da177e4SLinus Torvalds /* Allocate a page in interleaved policy.
12291da177e4SLinus Torvalds    Own path because it needs to do special accounting. */
1230662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
1231662f3a0bSAndi Kleen 					unsigned nid)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	struct zonelist *zl;
12341da177e4SLinus Torvalds 	struct page *page;
12351da177e4SLinus Torvalds 
1236af4ca457SAl Viro 	zl = NODE_DATA(nid)->node_zonelists + gfp_zone(gfp);
12371da177e4SLinus Torvalds 	page = __alloc_pages(gfp, order, zl);
1238ca889e6cSChristoph Lameter 	if (page && page_zone(page) == zl->zones[0])
1239ca889e6cSChristoph Lameter 		inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
12401da177e4SLinus Torvalds 	return page;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds /**
12441da177e4SLinus Torvalds  * 	alloc_page_vma	- Allocate a page for a VMA.
12451da177e4SLinus Torvalds  *
12461da177e4SLinus Torvalds  * 	@gfp:
12471da177e4SLinus Torvalds  *      %GFP_USER    user allocation.
12481da177e4SLinus Torvalds  *      %GFP_KERNEL  kernel allocations,
12491da177e4SLinus Torvalds  *      %GFP_HIGHMEM highmem/user allocations,
12501da177e4SLinus Torvalds  *      %GFP_FS      allocation should not call back into a file system.
12511da177e4SLinus Torvalds  *      %GFP_ATOMIC  don't sleep.
12521da177e4SLinus Torvalds  *
12531da177e4SLinus Torvalds  * 	@vma:  Pointer to VMA or NULL if not available.
12541da177e4SLinus Torvalds  *	@addr: Virtual Address of the allocation. Must be inside the VMA.
12551da177e4SLinus Torvalds  *
12561da177e4SLinus Torvalds  * 	This function allocates a page from the kernel page pool and applies
12571da177e4SLinus Torvalds  *	a NUMA policy associated with the VMA or the current process.
12581da177e4SLinus Torvalds  *	When VMA is not NULL caller must hold down_read on the mmap_sem of the
12591da177e4SLinus Torvalds  *	mm_struct of the VMA to prevent it from going away. Should be used for
12601da177e4SLinus Torvalds  *	all allocations for pages that will be mapped into
12611da177e4SLinus Torvalds  * 	user space. Returns NULL when no page can be allocated.
12621da177e4SLinus Torvalds  *
12631da177e4SLinus Torvalds  *	Should be called with the mm_sem of the vma hold.
12641da177e4SLinus Torvalds  */
12651da177e4SLinus Torvalds struct page *
1266dd0fc66fSAl Viro alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr)
12671da177e4SLinus Torvalds {
12686e21c8f1SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
12691da177e4SLinus Torvalds 
1270cf2a473cSPaul Jackson 	cpuset_update_task_memory_state();
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	if (unlikely(pol->policy == MPOL_INTERLEAVE)) {
12731da177e4SLinus Torvalds 		unsigned nid;
12745da7ca86SChristoph Lameter 
12755da7ca86SChristoph Lameter 		nid = interleave_nid(pol, vma, addr, PAGE_SHIFT);
12761da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, 0, nid);
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 	return __alloc_pages(gfp, 0, zonelist_policy(gfp, pol));
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds /**
12821da177e4SLinus Torvalds  * 	alloc_pages_current - Allocate pages.
12831da177e4SLinus Torvalds  *
12841da177e4SLinus Torvalds  *	@gfp:
12851da177e4SLinus Torvalds  *		%GFP_USER   user allocation,
12861da177e4SLinus Torvalds  *      	%GFP_KERNEL kernel allocation,
12871da177e4SLinus Torvalds  *      	%GFP_HIGHMEM highmem allocation,
12881da177e4SLinus Torvalds  *      	%GFP_FS     don't call back into a file system.
12891da177e4SLinus Torvalds  *      	%GFP_ATOMIC don't sleep.
12901da177e4SLinus Torvalds  *	@order: Power of two of allocation size in pages. 0 is a single page.
12911da177e4SLinus Torvalds  *
12921da177e4SLinus Torvalds  *	Allocate a page from the kernel page pool.  When not in
12931da177e4SLinus Torvalds  *	interrupt context and apply the current process NUMA policy.
12941da177e4SLinus Torvalds  *	Returns NULL when no page can be allocated.
12951da177e4SLinus Torvalds  *
1296cf2a473cSPaul Jackson  *	Don't call cpuset_update_task_memory_state() unless
12971da177e4SLinus Torvalds  *	1) it's ok to take cpuset_sem (can WAIT), and
12981da177e4SLinus Torvalds  *	2) allocating for current task (not interrupt).
12991da177e4SLinus Torvalds  */
1300dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order)
13011da177e4SLinus Torvalds {
13021da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	if ((gfp & __GFP_WAIT) && !in_interrupt())
1305cf2a473cSPaul Jackson 		cpuset_update_task_memory_state();
13069b819d20SChristoph Lameter 	if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
13071da177e4SLinus Torvalds 		pol = &default_policy;
13081da177e4SLinus Torvalds 	if (pol->policy == MPOL_INTERLEAVE)
13091da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, order, interleave_nodes(pol));
13101da177e4SLinus Torvalds 	return __alloc_pages(gfp, order, zonelist_policy(gfp, pol));
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current);
13131da177e4SLinus Torvalds 
13144225399aSPaul Jackson /*
13154225399aSPaul Jackson  * If mpol_copy() sees current->cpuset == cpuset_being_rebound, then it
13164225399aSPaul Jackson  * rebinds the mempolicy its copying by calling mpol_rebind_policy()
13174225399aSPaul Jackson  * with the mems_allowed returned by cpuset_mems_allowed().  This
13184225399aSPaul Jackson  * keeps mempolicies cpuset relative after its cpuset moves.  See
13194225399aSPaul Jackson  * further kernel/cpuset.c update_nodemask().
13204225399aSPaul Jackson  */
13214225399aSPaul Jackson void *cpuset_being_rebound;
13224225399aSPaul Jackson 
13231da177e4SLinus Torvalds /* Slow path of a mempolicy copy */
13241da177e4SLinus Torvalds struct mempolicy *__mpol_copy(struct mempolicy *old)
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	if (!new)
13291da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
13304225399aSPaul Jackson 	if (current_cpuset_is_being_rebound()) {
13314225399aSPaul Jackson 		nodemask_t mems = cpuset_mems_allowed(current);
13324225399aSPaul Jackson 		mpol_rebind_policy(old, &mems);
13334225399aSPaul Jackson 	}
13341da177e4SLinus Torvalds 	*new = *old;
13351da177e4SLinus Torvalds 	atomic_set(&new->refcnt, 1);
13361da177e4SLinus Torvalds 	if (new->policy == MPOL_BIND) {
13371da177e4SLinus Torvalds 		int sz = ksize(old->v.zonelist);
1338e94b1766SChristoph Lameter 		new->v.zonelist = kmemdup(old->v.zonelist, sz, GFP_KERNEL);
13391da177e4SLinus Torvalds 		if (!new->v.zonelist) {
13401da177e4SLinus Torvalds 			kmem_cache_free(policy_cache, new);
13411da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
13421da177e4SLinus Torvalds 		}
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 	return new;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */
13481da177e4SLinus Torvalds int __mpol_equal(struct mempolicy *a, struct mempolicy *b)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	if (!a || !b)
13511da177e4SLinus Torvalds 		return 0;
13521da177e4SLinus Torvalds 	if (a->policy != b->policy)
13531da177e4SLinus Torvalds 		return 0;
13541da177e4SLinus Torvalds 	switch (a->policy) {
13551da177e4SLinus Torvalds 	case MPOL_DEFAULT:
13561da177e4SLinus Torvalds 		return 1;
13571da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
1358dfcd3c0dSAndi Kleen 		return nodes_equal(a->v.nodes, b->v.nodes);
13591da177e4SLinus Torvalds 	case MPOL_PREFERRED:
13601da177e4SLinus Torvalds 		return a->v.preferred_node == b->v.preferred_node;
13611da177e4SLinus Torvalds 	case MPOL_BIND: {
13621da177e4SLinus Torvalds 		int i;
13631da177e4SLinus Torvalds 		for (i = 0; a->v.zonelist->zones[i]; i++)
13641da177e4SLinus Torvalds 			if (a->v.zonelist->zones[i] != b->v.zonelist->zones[i])
13651da177e4SLinus Torvalds 				return 0;
13661da177e4SLinus Torvalds 		return b->v.zonelist->zones[i] == NULL;
13671da177e4SLinus Torvalds 	}
13681da177e4SLinus Torvalds 	default:
13691da177e4SLinus Torvalds 		BUG();
13701da177e4SLinus Torvalds 		return 0;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds /* Slow path of a mpol destructor. */
13751da177e4SLinus Torvalds void __mpol_free(struct mempolicy *p)
13761da177e4SLinus Torvalds {
13771da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&p->refcnt))
13781da177e4SLinus Torvalds 		return;
13791da177e4SLinus Torvalds 	if (p->policy == MPOL_BIND)
13801da177e4SLinus Torvalds 		kfree(p->v.zonelist);
13811da177e4SLinus Torvalds 	p->policy = MPOL_DEFAULT;
13821da177e4SLinus Torvalds 	kmem_cache_free(policy_cache, p);
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds /*
13861da177e4SLinus Torvalds  * Shared memory backing store policy support.
13871da177e4SLinus Torvalds  *
13881da177e4SLinus Torvalds  * Remember policies even when nobody has shared memory mapped.
13891da177e4SLinus Torvalds  * The policies are kept in Red-Black tree linked from the inode.
13901da177e4SLinus Torvalds  * They are protected by the sp->lock spinlock, which should be held
13911da177e4SLinus Torvalds  * for any accesses to the tree.
13921da177e4SLinus Torvalds  */
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds /* lookup first element intersecting start-end */
13951da177e4SLinus Torvalds /* Caller holds sp->lock */
13961da177e4SLinus Torvalds static struct sp_node *
13971da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
13981da177e4SLinus Torvalds {
13991da177e4SLinus Torvalds 	struct rb_node *n = sp->root.rb_node;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	while (n) {
14021da177e4SLinus Torvalds 		struct sp_node *p = rb_entry(n, struct sp_node, nd);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		if (start >= p->end)
14051da177e4SLinus Torvalds 			n = n->rb_right;
14061da177e4SLinus Torvalds 		else if (end <= p->start)
14071da177e4SLinus Torvalds 			n = n->rb_left;
14081da177e4SLinus Torvalds 		else
14091da177e4SLinus Torvalds 			break;
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 	if (!n)
14121da177e4SLinus Torvalds 		return NULL;
14131da177e4SLinus Torvalds 	for (;;) {
14141da177e4SLinus Torvalds 		struct sp_node *w = NULL;
14151da177e4SLinus Torvalds 		struct rb_node *prev = rb_prev(n);
14161da177e4SLinus Torvalds 		if (!prev)
14171da177e4SLinus Torvalds 			break;
14181da177e4SLinus Torvalds 		w = rb_entry(prev, struct sp_node, nd);
14191da177e4SLinus Torvalds 		if (w->end <= start)
14201da177e4SLinus Torvalds 			break;
14211da177e4SLinus Torvalds 		n = prev;
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds 	return rb_entry(n, struct sp_node, nd);
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds /* Insert a new shared policy into the list. */
14271da177e4SLinus Torvalds /* Caller holds sp->lock */
14281da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new)
14291da177e4SLinus Torvalds {
14301da177e4SLinus Torvalds 	struct rb_node **p = &sp->root.rb_node;
14311da177e4SLinus Torvalds 	struct rb_node *parent = NULL;
14321da177e4SLinus Torvalds 	struct sp_node *nd;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	while (*p) {
14351da177e4SLinus Torvalds 		parent = *p;
14361da177e4SLinus Torvalds 		nd = rb_entry(parent, struct sp_node, nd);
14371da177e4SLinus Torvalds 		if (new->start < nd->start)
14381da177e4SLinus Torvalds 			p = &(*p)->rb_left;
14391da177e4SLinus Torvalds 		else if (new->end > nd->end)
14401da177e4SLinus Torvalds 			p = &(*p)->rb_right;
14411da177e4SLinus Torvalds 		else
14421da177e4SLinus Torvalds 			BUG();
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 	rb_link_node(&new->nd, parent, p);
14451da177e4SLinus Torvalds 	rb_insert_color(&new->nd, &sp->root);
14461da177e4SLinus Torvalds 	PDprintk("inserting %lx-%lx: %d\n", new->start, new->end,
14471da177e4SLinus Torvalds 		 new->policy ? new->policy->policy : 0);
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds /* Find shared policy intersecting idx */
14511da177e4SLinus Torvalds struct mempolicy *
14521da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
14531da177e4SLinus Torvalds {
14541da177e4SLinus Torvalds 	struct mempolicy *pol = NULL;
14551da177e4SLinus Torvalds 	struct sp_node *sn;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	if (!sp->root.rb_node)
14581da177e4SLinus Torvalds 		return NULL;
14591da177e4SLinus Torvalds 	spin_lock(&sp->lock);
14601da177e4SLinus Torvalds 	sn = sp_lookup(sp, idx, idx+1);
14611da177e4SLinus Torvalds 	if (sn) {
14621da177e4SLinus Torvalds 		mpol_get(sn->policy);
14631da177e4SLinus Torvalds 		pol = sn->policy;
14641da177e4SLinus Torvalds 	}
14651da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
14661da177e4SLinus Torvalds 	return pol;
14671da177e4SLinus Torvalds }
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n)
14701da177e4SLinus Torvalds {
14711da177e4SLinus Torvalds 	PDprintk("deleting %lx-l%x\n", n->start, n->end);
14721da177e4SLinus Torvalds 	rb_erase(&n->nd, &sp->root);
14731da177e4SLinus Torvalds 	mpol_free(n->policy);
14741da177e4SLinus Torvalds 	kmem_cache_free(sn_cache, n);
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds struct sp_node *
14781da177e4SLinus Torvalds sp_alloc(unsigned long start, unsigned long end, struct mempolicy *pol)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	if (!n)
14831da177e4SLinus Torvalds 		return NULL;
14841da177e4SLinus Torvalds 	n->start = start;
14851da177e4SLinus Torvalds 	n->end = end;
14861da177e4SLinus Torvalds 	mpol_get(pol);
14871da177e4SLinus Torvalds 	n->policy = pol;
14881da177e4SLinus Torvalds 	return n;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds /* Replace a policy range. */
14921da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
14931da177e4SLinus Torvalds 				 unsigned long end, struct sp_node *new)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	struct sp_node *n, *new2 = NULL;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds restart:
14981da177e4SLinus Torvalds 	spin_lock(&sp->lock);
14991da177e4SLinus Torvalds 	n = sp_lookup(sp, start, end);
15001da177e4SLinus Torvalds 	/* Take care of old policies in the same range. */
15011da177e4SLinus Torvalds 	while (n && n->start < end) {
15021da177e4SLinus Torvalds 		struct rb_node *next = rb_next(&n->nd);
15031da177e4SLinus Torvalds 		if (n->start >= start) {
15041da177e4SLinus Torvalds 			if (n->end <= end)
15051da177e4SLinus Torvalds 				sp_delete(sp, n);
15061da177e4SLinus Torvalds 			else
15071da177e4SLinus Torvalds 				n->start = end;
15081da177e4SLinus Torvalds 		} else {
15091da177e4SLinus Torvalds 			/* Old policy spanning whole new range. */
15101da177e4SLinus Torvalds 			if (n->end > end) {
15111da177e4SLinus Torvalds 				if (!new2) {
15121da177e4SLinus Torvalds 					spin_unlock(&sp->lock);
15131da177e4SLinus Torvalds 					new2 = sp_alloc(end, n->end, n->policy);
15141da177e4SLinus Torvalds 					if (!new2)
15151da177e4SLinus Torvalds 						return -ENOMEM;
15161da177e4SLinus Torvalds 					goto restart;
15171da177e4SLinus Torvalds 				}
15181da177e4SLinus Torvalds 				n->end = start;
15191da177e4SLinus Torvalds 				sp_insert(sp, new2);
15201da177e4SLinus Torvalds 				new2 = NULL;
15211da177e4SLinus Torvalds 				break;
15221da177e4SLinus Torvalds 			} else
15231da177e4SLinus Torvalds 				n->end = start;
15241da177e4SLinus Torvalds 		}
15251da177e4SLinus Torvalds 		if (!next)
15261da177e4SLinus Torvalds 			break;
15271da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 	if (new)
15301da177e4SLinus Torvalds 		sp_insert(sp, new);
15311da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
15321da177e4SLinus Torvalds 	if (new2) {
15331da177e4SLinus Torvalds 		mpol_free(new2->policy);
15341da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new2);
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 	return 0;
15371da177e4SLinus Torvalds }
15381da177e4SLinus Torvalds 
15397339ff83SRobin Holt void mpol_shared_policy_init(struct shared_policy *info, int policy,
15407339ff83SRobin Holt 				nodemask_t *policy_nodes)
15417339ff83SRobin Holt {
15427339ff83SRobin Holt 	info->root = RB_ROOT;
15437339ff83SRobin Holt 	spin_lock_init(&info->lock);
15447339ff83SRobin Holt 
15457339ff83SRobin Holt 	if (policy != MPOL_DEFAULT) {
15467339ff83SRobin Holt 		struct mempolicy *newpol;
15477339ff83SRobin Holt 
15487339ff83SRobin Holt 		/* Falls back to MPOL_DEFAULT on any error */
15497339ff83SRobin Holt 		newpol = mpol_new(policy, policy_nodes);
15507339ff83SRobin Holt 		if (!IS_ERR(newpol)) {
15517339ff83SRobin Holt 			/* Create pseudo-vma that contains just the policy */
15527339ff83SRobin Holt 			struct vm_area_struct pvma;
15537339ff83SRobin Holt 
15547339ff83SRobin Holt 			memset(&pvma, 0, sizeof(struct vm_area_struct));
15557339ff83SRobin Holt 			/* Policy covers entire file */
15567339ff83SRobin Holt 			pvma.vm_end = TASK_SIZE;
15577339ff83SRobin Holt 			mpol_set_shared_policy(info, &pvma, newpol);
15587339ff83SRobin Holt 			mpol_free(newpol);
15597339ff83SRobin Holt 		}
15607339ff83SRobin Holt 	}
15617339ff83SRobin Holt }
15627339ff83SRobin Holt 
15631da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info,
15641da177e4SLinus Torvalds 			struct vm_area_struct *vma, struct mempolicy *npol)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	int err;
15671da177e4SLinus Torvalds 	struct sp_node *new = NULL;
15681da177e4SLinus Torvalds 	unsigned long sz = vma_pages(vma);
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	PDprintk("set_shared_policy %lx sz %lu %d %lx\n",
15711da177e4SLinus Torvalds 		 vma->vm_pgoff,
15721da177e4SLinus Torvalds 		 sz, npol? npol->policy : -1,
1573dfcd3c0dSAndi Kleen 		npol ? nodes_addr(npol->v.nodes)[0] : -1);
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	if (npol) {
15761da177e4SLinus Torvalds 		new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
15771da177e4SLinus Torvalds 		if (!new)
15781da177e4SLinus Torvalds 			return -ENOMEM;
15791da177e4SLinus Torvalds 	}
15801da177e4SLinus Torvalds 	err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
15811da177e4SLinus Torvalds 	if (err && new)
15821da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new);
15831da177e4SLinus Torvalds 	return err;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */
15871da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	struct sp_node *n;
15901da177e4SLinus Torvalds 	struct rb_node *next;
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	if (!p->root.rb_node)
15931da177e4SLinus Torvalds 		return;
15941da177e4SLinus Torvalds 	spin_lock(&p->lock);
15951da177e4SLinus Torvalds 	next = rb_first(&p->root);
15961da177e4SLinus Torvalds 	while (next) {
15971da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
15981da177e4SLinus Torvalds 		next = rb_next(&n->nd);
159990c5029eSAndi Kleen 		rb_erase(&n->nd, &p->root);
16001da177e4SLinus Torvalds 		mpol_free(n->policy);
16011da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, n);
16021da177e4SLinus Torvalds 	}
16031da177e4SLinus Torvalds 	spin_unlock(&p->lock);
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */
16071da177e4SLinus Torvalds void __init numa_policy_init(void)
16081da177e4SLinus Torvalds {
16091da177e4SLinus Torvalds 	policy_cache = kmem_cache_create("numa_policy",
16101da177e4SLinus Torvalds 					 sizeof(struct mempolicy),
16111da177e4SLinus Torvalds 					 0, SLAB_PANIC, NULL, NULL);
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	sn_cache = kmem_cache_create("shared_policy_node",
16141da177e4SLinus Torvalds 				     sizeof(struct sp_node),
16151da177e4SLinus Torvalds 				     0, SLAB_PANIC, NULL, NULL);
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	/* Set interleaving policy for system init. This way not all
16181da177e4SLinus Torvalds 	   the data structures allocated at system boot end up in node zero. */
16191da177e4SLinus Torvalds 
16208bccd85fSChristoph Lameter 	if (do_set_mempolicy(MPOL_INTERLEAVE, &node_online_map))
16211da177e4SLinus Torvalds 		printk("numa_policy_init: interleaving failed\n");
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
16248bccd85fSChristoph Lameter /* Reset policy of current process to default */
16251da177e4SLinus Torvalds void numa_default_policy(void)
16261da177e4SLinus Torvalds {
16278bccd85fSChristoph Lameter 	do_set_mempolicy(MPOL_DEFAULT, NULL);
16281da177e4SLinus Torvalds }
162968860ec1SPaul Jackson 
163068860ec1SPaul Jackson /* Migrate a policy to a different set of nodes */
163174cb2155SPaul Jackson void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask)
163268860ec1SPaul Jackson {
163374cb2155SPaul Jackson 	nodemask_t *mpolmask;
163468860ec1SPaul Jackson 	nodemask_t tmp;
163568860ec1SPaul Jackson 
163668860ec1SPaul Jackson 	if (!pol)
163768860ec1SPaul Jackson 		return;
163874cb2155SPaul Jackson 	mpolmask = &pol->cpuset_mems_allowed;
163974cb2155SPaul Jackson 	if (nodes_equal(*mpolmask, *newmask))
164074cb2155SPaul Jackson 		return;
164168860ec1SPaul Jackson 
164268860ec1SPaul Jackson 	switch (pol->policy) {
164368860ec1SPaul Jackson 	case MPOL_DEFAULT:
164468860ec1SPaul Jackson 		break;
164568860ec1SPaul Jackson 	case MPOL_INTERLEAVE:
164674cb2155SPaul Jackson 		nodes_remap(tmp, pol->v.nodes, *mpolmask, *newmask);
164768860ec1SPaul Jackson 		pol->v.nodes = tmp;
164874cb2155SPaul Jackson 		*mpolmask = *newmask;
164974cb2155SPaul Jackson 		current->il_next = node_remap(current->il_next,
165074cb2155SPaul Jackson 						*mpolmask, *newmask);
165168860ec1SPaul Jackson 		break;
165268860ec1SPaul Jackson 	case MPOL_PREFERRED:
165368860ec1SPaul Jackson 		pol->v.preferred_node = node_remap(pol->v.preferred_node,
165474cb2155SPaul Jackson 						*mpolmask, *newmask);
165574cb2155SPaul Jackson 		*mpolmask = *newmask;
165668860ec1SPaul Jackson 		break;
165768860ec1SPaul Jackson 	case MPOL_BIND: {
165868860ec1SPaul Jackson 		nodemask_t nodes;
165968860ec1SPaul Jackson 		struct zone **z;
166068860ec1SPaul Jackson 		struct zonelist *zonelist;
166168860ec1SPaul Jackson 
166268860ec1SPaul Jackson 		nodes_clear(nodes);
166368860ec1SPaul Jackson 		for (z = pol->v.zonelist->zones; *z; z++)
166489fa3024SChristoph Lameter 			node_set(zone_to_nid(*z), nodes);
166574cb2155SPaul Jackson 		nodes_remap(tmp, nodes, *mpolmask, *newmask);
166668860ec1SPaul Jackson 		nodes = tmp;
166768860ec1SPaul Jackson 
166868860ec1SPaul Jackson 		zonelist = bind_zonelist(&nodes);
166968860ec1SPaul Jackson 
167068860ec1SPaul Jackson 		/* If no mem, then zonelist is NULL and we keep old zonelist.
167168860ec1SPaul Jackson 		 * If that old zonelist has no remaining mems_allowed nodes,
167268860ec1SPaul Jackson 		 * then zonelist_policy() will "FALL THROUGH" to MPOL_DEFAULT.
167368860ec1SPaul Jackson 		 */
167468860ec1SPaul Jackson 
1675*8af5e2ebSKAMEZAWA Hiroyuki 		if (!IS_ERR(zonelist)) {
167668860ec1SPaul Jackson 			/* Good - got mem - substitute new zonelist */
167768860ec1SPaul Jackson 			kfree(pol->v.zonelist);
167868860ec1SPaul Jackson 			pol->v.zonelist = zonelist;
167968860ec1SPaul Jackson 		}
168074cb2155SPaul Jackson 		*mpolmask = *newmask;
168168860ec1SPaul Jackson 		break;
168268860ec1SPaul Jackson 	}
168368860ec1SPaul Jackson 	default:
168468860ec1SPaul Jackson 		BUG();
168568860ec1SPaul Jackson 		break;
168668860ec1SPaul Jackson 	}
168768860ec1SPaul Jackson }
168868860ec1SPaul Jackson 
168968860ec1SPaul Jackson /*
169074cb2155SPaul Jackson  * Wrapper for mpol_rebind_policy() that just requires task
169174cb2155SPaul Jackson  * pointer, and updates task mempolicy.
169268860ec1SPaul Jackson  */
169374cb2155SPaul Jackson 
169474cb2155SPaul Jackson void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new)
169568860ec1SPaul Jackson {
169674cb2155SPaul Jackson 	mpol_rebind_policy(tsk->mempolicy, new);
169768860ec1SPaul Jackson }
16981a75a6c8SChristoph Lameter 
16991a75a6c8SChristoph Lameter /*
17004225399aSPaul Jackson  * Rebind each vma in mm to new nodemask.
17014225399aSPaul Jackson  *
17024225399aSPaul Jackson  * Call holding a reference to mm.  Takes mm->mmap_sem during call.
17034225399aSPaul Jackson  */
17044225399aSPaul Jackson 
17054225399aSPaul Jackson void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
17064225399aSPaul Jackson {
17074225399aSPaul Jackson 	struct vm_area_struct *vma;
17084225399aSPaul Jackson 
17094225399aSPaul Jackson 	down_write(&mm->mmap_sem);
17104225399aSPaul Jackson 	for (vma = mm->mmap; vma; vma = vma->vm_next)
17114225399aSPaul Jackson 		mpol_rebind_policy(vma->vm_policy, new);
17124225399aSPaul Jackson 	up_write(&mm->mmap_sem);
17134225399aSPaul Jackson }
17144225399aSPaul Jackson 
17154225399aSPaul Jackson /*
17161a75a6c8SChristoph Lameter  * Display pages allocated per node and memory policy via /proc.
17171a75a6c8SChristoph Lameter  */
17181a75a6c8SChristoph Lameter 
171915ad7cdcSHelge Deller static const char * const policy_types[] =
172015ad7cdcSHelge Deller 	{ "default", "prefer", "bind", "interleave" };
17211a75a6c8SChristoph Lameter 
17221a75a6c8SChristoph Lameter /*
17231a75a6c8SChristoph Lameter  * Convert a mempolicy into a string.
17241a75a6c8SChristoph Lameter  * Returns the number of characters in buffer (if positive)
17251a75a6c8SChristoph Lameter  * or an error (negative)
17261a75a6c8SChristoph Lameter  */
17271a75a6c8SChristoph Lameter static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
17281a75a6c8SChristoph Lameter {
17291a75a6c8SChristoph Lameter 	char *p = buffer;
17301a75a6c8SChristoph Lameter 	int l;
17311a75a6c8SChristoph Lameter 	nodemask_t nodes;
17321a75a6c8SChristoph Lameter 	int mode = pol ? pol->policy : MPOL_DEFAULT;
17331a75a6c8SChristoph Lameter 
17341a75a6c8SChristoph Lameter 	switch (mode) {
17351a75a6c8SChristoph Lameter 	case MPOL_DEFAULT:
17361a75a6c8SChristoph Lameter 		nodes_clear(nodes);
17371a75a6c8SChristoph Lameter 		break;
17381a75a6c8SChristoph Lameter 
17391a75a6c8SChristoph Lameter 	case MPOL_PREFERRED:
17401a75a6c8SChristoph Lameter 		nodes_clear(nodes);
17411a75a6c8SChristoph Lameter 		node_set(pol->v.preferred_node, nodes);
17421a75a6c8SChristoph Lameter 		break;
17431a75a6c8SChristoph Lameter 
17441a75a6c8SChristoph Lameter 	case MPOL_BIND:
17451a75a6c8SChristoph Lameter 		get_zonemask(pol, &nodes);
17461a75a6c8SChristoph Lameter 		break;
17471a75a6c8SChristoph Lameter 
17481a75a6c8SChristoph Lameter 	case MPOL_INTERLEAVE:
17491a75a6c8SChristoph Lameter 		nodes = pol->v.nodes;
17501a75a6c8SChristoph Lameter 		break;
17511a75a6c8SChristoph Lameter 
17521a75a6c8SChristoph Lameter 	default:
17531a75a6c8SChristoph Lameter 		BUG();
17541a75a6c8SChristoph Lameter 		return -EFAULT;
17551a75a6c8SChristoph Lameter 	}
17561a75a6c8SChristoph Lameter 
17571a75a6c8SChristoph Lameter 	l = strlen(policy_types[mode]);
17581a75a6c8SChristoph Lameter  	if (buffer + maxlen < p + l + 1)
17591a75a6c8SChristoph Lameter  		return -ENOSPC;
17601a75a6c8SChristoph Lameter 
17611a75a6c8SChristoph Lameter 	strcpy(p, policy_types[mode]);
17621a75a6c8SChristoph Lameter 	p += l;
17631a75a6c8SChristoph Lameter 
17641a75a6c8SChristoph Lameter 	if (!nodes_empty(nodes)) {
17651a75a6c8SChristoph Lameter 		if (buffer + maxlen < p + 2)
17661a75a6c8SChristoph Lameter 			return -ENOSPC;
17671a75a6c8SChristoph Lameter 		*p++ = '=';
17681a75a6c8SChristoph Lameter 	 	p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
17691a75a6c8SChristoph Lameter 	}
17701a75a6c8SChristoph Lameter 	return p - buffer;
17711a75a6c8SChristoph Lameter }
17721a75a6c8SChristoph Lameter 
17731a75a6c8SChristoph Lameter struct numa_maps {
17741a75a6c8SChristoph Lameter 	unsigned long pages;
17751a75a6c8SChristoph Lameter 	unsigned long anon;
1776397874dfSChristoph Lameter 	unsigned long active;
1777397874dfSChristoph Lameter 	unsigned long writeback;
17781a75a6c8SChristoph Lameter 	unsigned long mapcount_max;
1779397874dfSChristoph Lameter 	unsigned long dirty;
1780397874dfSChristoph Lameter 	unsigned long swapcache;
17811a75a6c8SChristoph Lameter 	unsigned long node[MAX_NUMNODES];
17821a75a6c8SChristoph Lameter };
17831a75a6c8SChristoph Lameter 
1784397874dfSChristoph Lameter static void gather_stats(struct page *page, void *private, int pte_dirty)
17851a75a6c8SChristoph Lameter {
17861a75a6c8SChristoph Lameter 	struct numa_maps *md = private;
17871a75a6c8SChristoph Lameter 	int count = page_mapcount(page);
17881a75a6c8SChristoph Lameter 
17891a75a6c8SChristoph Lameter 	md->pages++;
1790397874dfSChristoph Lameter 	if (pte_dirty || PageDirty(page))
1791397874dfSChristoph Lameter 		md->dirty++;
1792397874dfSChristoph Lameter 
1793397874dfSChristoph Lameter 	if (PageSwapCache(page))
1794397874dfSChristoph Lameter 		md->swapcache++;
1795397874dfSChristoph Lameter 
1796397874dfSChristoph Lameter 	if (PageActive(page))
1797397874dfSChristoph Lameter 		md->active++;
1798397874dfSChristoph Lameter 
1799397874dfSChristoph Lameter 	if (PageWriteback(page))
1800397874dfSChristoph Lameter 		md->writeback++;
18011a75a6c8SChristoph Lameter 
18021a75a6c8SChristoph Lameter 	if (PageAnon(page))
18031a75a6c8SChristoph Lameter 		md->anon++;
18041a75a6c8SChristoph Lameter 
1805397874dfSChristoph Lameter 	if (count > md->mapcount_max)
1806397874dfSChristoph Lameter 		md->mapcount_max = count;
1807397874dfSChristoph Lameter 
18081a75a6c8SChristoph Lameter 	md->node[page_to_nid(page)]++;
18091a75a6c8SChristoph Lameter }
18101a75a6c8SChristoph Lameter 
18117f709ed0SAndrew Morton #ifdef CONFIG_HUGETLB_PAGE
1812397874dfSChristoph Lameter static void check_huge_range(struct vm_area_struct *vma,
1813397874dfSChristoph Lameter 		unsigned long start, unsigned long end,
1814397874dfSChristoph Lameter 		struct numa_maps *md)
1815397874dfSChristoph Lameter {
1816397874dfSChristoph Lameter 	unsigned long addr;
1817397874dfSChristoph Lameter 	struct page *page;
1818397874dfSChristoph Lameter 
1819397874dfSChristoph Lameter 	for (addr = start; addr < end; addr += HPAGE_SIZE) {
1820397874dfSChristoph Lameter 		pte_t *ptep = huge_pte_offset(vma->vm_mm, addr & HPAGE_MASK);
1821397874dfSChristoph Lameter 		pte_t pte;
1822397874dfSChristoph Lameter 
1823397874dfSChristoph Lameter 		if (!ptep)
1824397874dfSChristoph Lameter 			continue;
1825397874dfSChristoph Lameter 
1826397874dfSChristoph Lameter 		pte = *ptep;
1827397874dfSChristoph Lameter 		if (pte_none(pte))
1828397874dfSChristoph Lameter 			continue;
1829397874dfSChristoph Lameter 
1830397874dfSChristoph Lameter 		page = pte_page(pte);
1831397874dfSChristoph Lameter 		if (!page)
1832397874dfSChristoph Lameter 			continue;
1833397874dfSChristoph Lameter 
1834397874dfSChristoph Lameter 		gather_stats(page, md, pte_dirty(*ptep));
1835397874dfSChristoph Lameter 	}
1836397874dfSChristoph Lameter }
18377f709ed0SAndrew Morton #else
18387f709ed0SAndrew Morton static inline void check_huge_range(struct vm_area_struct *vma,
18397f709ed0SAndrew Morton 		unsigned long start, unsigned long end,
18407f709ed0SAndrew Morton 		struct numa_maps *md)
18417f709ed0SAndrew Morton {
18427f709ed0SAndrew Morton }
18437f709ed0SAndrew Morton #endif
1844397874dfSChristoph Lameter 
18451a75a6c8SChristoph Lameter int show_numa_map(struct seq_file *m, void *v)
18461a75a6c8SChristoph Lameter {
184799f89551SEric W. Biederman 	struct proc_maps_private *priv = m->private;
18481a75a6c8SChristoph Lameter 	struct vm_area_struct *vma = v;
18491a75a6c8SChristoph Lameter 	struct numa_maps *md;
1850397874dfSChristoph Lameter 	struct file *file = vma->vm_file;
1851397874dfSChristoph Lameter 	struct mm_struct *mm = vma->vm_mm;
18521a75a6c8SChristoph Lameter 	int n;
18531a75a6c8SChristoph Lameter 	char buffer[50];
18541a75a6c8SChristoph Lameter 
1855397874dfSChristoph Lameter 	if (!mm)
18561a75a6c8SChristoph Lameter 		return 0;
18571a75a6c8SChristoph Lameter 
18581a75a6c8SChristoph Lameter 	md = kzalloc(sizeof(struct numa_maps), GFP_KERNEL);
18591a75a6c8SChristoph Lameter 	if (!md)
18601a75a6c8SChristoph Lameter 		return 0;
18611a75a6c8SChristoph Lameter 
18621a75a6c8SChristoph Lameter 	mpol_to_str(buffer, sizeof(buffer),
186399f89551SEric W. Biederman 			    get_vma_policy(priv->task, vma, vma->vm_start));
18641a75a6c8SChristoph Lameter 
1865397874dfSChristoph Lameter 	seq_printf(m, "%08lx %s", vma->vm_start, buffer);
1866397874dfSChristoph Lameter 
1867397874dfSChristoph Lameter 	if (file) {
1868397874dfSChristoph Lameter 		seq_printf(m, " file=");
1869e9536ae7SJosef Sipek 		seq_path(m, file->f_path.mnt, file->f_path.dentry, "\n\t= ");
1870397874dfSChristoph Lameter 	} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
1871397874dfSChristoph Lameter 		seq_printf(m, " heap");
1872397874dfSChristoph Lameter 	} else if (vma->vm_start <= mm->start_stack &&
1873397874dfSChristoph Lameter 			vma->vm_end >= mm->start_stack) {
1874397874dfSChristoph Lameter 		seq_printf(m, " stack");
1875397874dfSChristoph Lameter 	}
1876397874dfSChristoph Lameter 
1877397874dfSChristoph Lameter 	if (is_vm_hugetlb_page(vma)) {
1878397874dfSChristoph Lameter 		check_huge_range(vma, vma->vm_start, vma->vm_end, md);
1879397874dfSChristoph Lameter 		seq_printf(m, " huge");
1880397874dfSChristoph Lameter 	} else {
1881397874dfSChristoph Lameter 		check_pgd_range(vma, vma->vm_start, vma->vm_end,
1882397874dfSChristoph Lameter 				&node_online_map, MPOL_MF_STATS, md);
1883397874dfSChristoph Lameter 	}
1884397874dfSChristoph Lameter 
1885397874dfSChristoph Lameter 	if (!md->pages)
1886397874dfSChristoph Lameter 		goto out;
18871a75a6c8SChristoph Lameter 
18881a75a6c8SChristoph Lameter 	if (md->anon)
18891a75a6c8SChristoph Lameter 		seq_printf(m," anon=%lu",md->anon);
18901a75a6c8SChristoph Lameter 
1891397874dfSChristoph Lameter 	if (md->dirty)
1892397874dfSChristoph Lameter 		seq_printf(m," dirty=%lu",md->dirty);
1893397874dfSChristoph Lameter 
1894397874dfSChristoph Lameter 	if (md->pages != md->anon && md->pages != md->dirty)
1895397874dfSChristoph Lameter 		seq_printf(m, " mapped=%lu", md->pages);
1896397874dfSChristoph Lameter 
1897397874dfSChristoph Lameter 	if (md->mapcount_max > 1)
1898397874dfSChristoph Lameter 		seq_printf(m, " mapmax=%lu", md->mapcount_max);
1899397874dfSChristoph Lameter 
1900397874dfSChristoph Lameter 	if (md->swapcache)
1901397874dfSChristoph Lameter 		seq_printf(m," swapcache=%lu", md->swapcache);
1902397874dfSChristoph Lameter 
1903397874dfSChristoph Lameter 	if (md->active < md->pages && !is_vm_hugetlb_page(vma))
1904397874dfSChristoph Lameter 		seq_printf(m," active=%lu", md->active);
1905397874dfSChristoph Lameter 
1906397874dfSChristoph Lameter 	if (md->writeback)
1907397874dfSChristoph Lameter 		seq_printf(m," writeback=%lu", md->writeback);
1908397874dfSChristoph Lameter 
19091a75a6c8SChristoph Lameter 	for_each_online_node(n)
19101a75a6c8SChristoph Lameter 		if (md->node[n])
19111a75a6c8SChristoph Lameter 			seq_printf(m, " N%d=%lu", n, md->node[n]);
1912397874dfSChristoph Lameter out:
19131a75a6c8SChristoph Lameter 	seq_putc(m, '\n');
19141a75a6c8SChristoph Lameter 	kfree(md);
19151a75a6c8SChristoph Lameter 
19161a75a6c8SChristoph Lameter 	if (m->count < m->size)
191799f89551SEric W. Biederman 		m->version = (vma != priv->tail_vma) ? vma->vm_start : 0;
19181a75a6c8SChristoph Lameter 	return 0;
19191a75a6c8SChristoph Lameter }
19201a75a6c8SChristoph Lameter 
1921