xref: /openbmc/linux/mm/mempolicy.c (revision b5810039a54e5babf428e9a1e89fc1940fabff11)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Simple NUMA memory policy for the Linux kernel.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright 2003,2004 Andi Kleen, SuSE Labs.
51da177e4SLinus Torvalds  * Subject to the GNU Public License, version 2.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * NUMA policy allows the user to give hints in which node(s) memory should
81da177e4SLinus Torvalds  * be allocated.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Support four policies per VMA and per process:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * The VMA policy has priority over the process policy for a page fault.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * interleave     Allocate memory interleaved over a set of nodes,
151da177e4SLinus Torvalds  *                with normal fallback if it fails.
161da177e4SLinus Torvalds  *                For VMA based allocations this interleaves based on the
171da177e4SLinus Torvalds  *                offset into the backing object or offset into the mapping
181da177e4SLinus Torvalds  *                for anonymous memory. For process policy an process counter
191da177e4SLinus Torvalds  *                is used.
201da177e4SLinus Torvalds  * bind           Only allocate memory on a specific set of nodes,
211da177e4SLinus Torvalds  *                no fallback.
221da177e4SLinus Torvalds  * preferred       Try a specific node first before normal fallback.
231da177e4SLinus Torvalds  *                As a special case node -1 here means do the allocation
241da177e4SLinus Torvalds  *                on the local CPU. This is normally identical to default,
251da177e4SLinus Torvalds  *                but useful to set in a VMA when you have a non default
261da177e4SLinus Torvalds  *                process policy.
271da177e4SLinus Torvalds  * default        Allocate on the local node first, or when on a VMA
281da177e4SLinus Torvalds  *                use the process policy. This is what Linux always did
291da177e4SLinus Torvalds  *		  in a NUMA aware kernel and still does by, ahem, default.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  * The process policy is applied for most non interrupt memory allocations
321da177e4SLinus Torvalds  * in that process' context. Interrupts ignore the policies and always
331da177e4SLinus Torvalds  * try to allocate on the local CPU. The VMA policy is only applied for memory
341da177e4SLinus Torvalds  * allocations for a VMA in the VM.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  * Currently there are a few corner cases in swapping where the policy
371da177e4SLinus Torvalds  * is not applied, but the majority should be handled. When process policy
381da177e4SLinus Torvalds  * is used it is not remembered over swap outs/swap ins.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  * Only the highest zone in the zone hierarchy gets policied. Allocations
411da177e4SLinus Torvalds  * requesting a lower zone just use default policy. This implies that
421da177e4SLinus Torvalds  * on systems with highmem kernel lowmem allocation don't get policied.
431da177e4SLinus Torvalds  * Same with GFP_DMA allocations.
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
461da177e4SLinus Torvalds  * all users and remembered even when nobody has memory mapped.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /* Notebook:
501da177e4SLinus Torvalds    fix mmap readahead to honour policy and enable policy for any page cache
511da177e4SLinus Torvalds    object
521da177e4SLinus Torvalds    statistics for bigpages
531da177e4SLinus Torvalds    global policy for page cache? currently it uses process policy. Requires
541da177e4SLinus Torvalds    first item above.
551da177e4SLinus Torvalds    handle mremap for shared memory (currently ignored for the policy)
561da177e4SLinus Torvalds    grows down?
571da177e4SLinus Torvalds    make bind policy root only? It can trigger oom much faster and the
581da177e4SLinus Torvalds    kernel is not always grateful with that.
591da177e4SLinus Torvalds    could replace all the switch()es with a mempolicy_ops structure.
601da177e4SLinus Torvalds */
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <linux/mempolicy.h>
631da177e4SLinus Torvalds #include <linux/mm.h>
641da177e4SLinus Torvalds #include <linux/highmem.h>
651da177e4SLinus Torvalds #include <linux/hugetlb.h>
661da177e4SLinus Torvalds #include <linux/kernel.h>
671da177e4SLinus Torvalds #include <linux/sched.h>
681da177e4SLinus Torvalds #include <linux/mm.h>
691da177e4SLinus Torvalds #include <linux/nodemask.h>
701da177e4SLinus Torvalds #include <linux/cpuset.h>
711da177e4SLinus Torvalds #include <linux/gfp.h>
721da177e4SLinus Torvalds #include <linux/slab.h>
731da177e4SLinus Torvalds #include <linux/string.h>
741da177e4SLinus Torvalds #include <linux/module.h>
751da177e4SLinus Torvalds #include <linux/interrupt.h>
761da177e4SLinus Torvalds #include <linux/init.h>
771da177e4SLinus Torvalds #include <linux/compat.h>
781da177e4SLinus Torvalds #include <linux/mempolicy.h>
791da177e4SLinus Torvalds #include <asm/tlbflush.h>
801da177e4SLinus Torvalds #include <asm/uaccess.h>
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static kmem_cache_t *policy_cache;
831da177e4SLinus Torvalds static kmem_cache_t *sn_cache;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #define PDprintk(fmt...)
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* Highest zone. An specific allocation for a zone below that is not
881da177e4SLinus Torvalds    policied. */
891da177e4SLinus Torvalds static int policy_zone;
901da177e4SLinus Torvalds 
91d42c6997SAndi Kleen struct mempolicy default_policy = {
921da177e4SLinus Torvalds 	.refcnt = ATOMIC_INIT(1), /* never free it */
931da177e4SLinus Torvalds 	.policy = MPOL_DEFAULT,
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Do sanity checking on a policy */
97dfcd3c0dSAndi Kleen static int mpol_check_policy(int mode, nodemask_t *nodes)
981da177e4SLinus Torvalds {
99dfcd3c0dSAndi Kleen 	int empty = nodes_empty(*nodes);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	switch (mode) {
1021da177e4SLinus Torvalds 	case MPOL_DEFAULT:
1031da177e4SLinus Torvalds 		if (!empty)
1041da177e4SLinus Torvalds 			return -EINVAL;
1051da177e4SLinus Torvalds 		break;
1061da177e4SLinus Torvalds 	case MPOL_BIND:
1071da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
1081da177e4SLinus Torvalds 		/* Preferred will only use the first bit, but allow
1091da177e4SLinus Torvalds 		   more for now. */
1101da177e4SLinus Torvalds 		if (empty)
1111da177e4SLinus Torvalds 			return -EINVAL;
1121da177e4SLinus Torvalds 		break;
1131da177e4SLinus Torvalds 	}
114dfcd3c0dSAndi Kleen 	return nodes_subset(*nodes, node_online_map) ? 0 : -EINVAL;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* Copy a node mask from user space. */
118dfcd3c0dSAndi Kleen static int get_nodes(nodemask_t *nodes, unsigned long __user *nmask,
1191da177e4SLinus Torvalds 		     unsigned long maxnode, int mode)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	unsigned long k;
1221da177e4SLinus Torvalds 	unsigned long nlongs;
1231da177e4SLinus Torvalds 	unsigned long endmask;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	--maxnode;
126dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
1271da177e4SLinus Torvalds 	if (maxnode == 0 || !nmask)
1281da177e4SLinus Torvalds 		return 0;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	nlongs = BITS_TO_LONGS(maxnode);
1311da177e4SLinus Torvalds 	if ((maxnode % BITS_PER_LONG) == 0)
1321da177e4SLinus Torvalds 		endmask = ~0UL;
1331da177e4SLinus Torvalds 	else
1341da177e4SLinus Torvalds 		endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* When the user specified more nodes than supported just check
1371da177e4SLinus Torvalds 	   if the non supported part is all zero. */
1381da177e4SLinus Torvalds 	if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
1391da177e4SLinus Torvalds 		if (nlongs > PAGE_SIZE/sizeof(long))
1401da177e4SLinus Torvalds 			return -EINVAL;
1411da177e4SLinus Torvalds 		for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
1421da177e4SLinus Torvalds 			unsigned long t;
1431da177e4SLinus Torvalds 			if (get_user(t, nmask + k))
1441da177e4SLinus Torvalds 				return -EFAULT;
1451da177e4SLinus Torvalds 			if (k == nlongs - 1) {
1461da177e4SLinus Torvalds 				if (t & endmask)
1471da177e4SLinus Torvalds 					return -EINVAL;
1481da177e4SLinus Torvalds 			} else if (t)
1491da177e4SLinus Torvalds 				return -EINVAL;
1501da177e4SLinus Torvalds 		}
1511da177e4SLinus Torvalds 		nlongs = BITS_TO_LONGS(MAX_NUMNODES);
1521da177e4SLinus Torvalds 		endmask = ~0UL;
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 
155dfcd3c0dSAndi Kleen 	if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
1561da177e4SLinus Torvalds 		return -EFAULT;
157dfcd3c0dSAndi Kleen 	nodes_addr(*nodes)[nlongs-1] &= endmask;
1581da177e4SLinus Torvalds 	/* Update current mems_allowed */
1591da177e4SLinus Torvalds 	cpuset_update_current_mems_allowed();
1601da177e4SLinus Torvalds 	/* Ignore nodes not set in current->mems_allowed */
161dfcd3c0dSAndi Kleen 	/* AK: shouldn't this error out instead? */
162dfcd3c0dSAndi Kleen 	cpuset_restrict_to_mems_allowed(nodes_addr(*nodes));
1631da177e4SLinus Torvalds 	return mpol_check_policy(mode, nodes);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Generate a custom zonelist for the BIND policy. */
167dfcd3c0dSAndi Kleen static struct zonelist *bind_zonelist(nodemask_t *nodes)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct zonelist *zl;
1701da177e4SLinus Torvalds 	int num, max, nd;
1711da177e4SLinus Torvalds 
172dfcd3c0dSAndi Kleen 	max = 1 + MAX_NR_ZONES * nodes_weight(*nodes);
1731da177e4SLinus Torvalds 	zl = kmalloc(sizeof(void *) * max, GFP_KERNEL);
1741da177e4SLinus Torvalds 	if (!zl)
1751da177e4SLinus Torvalds 		return NULL;
1761da177e4SLinus Torvalds 	num = 0;
177dfcd3c0dSAndi Kleen 	for_each_node_mask(nd, *nodes) {
1781da177e4SLinus Torvalds 		int k;
1791da177e4SLinus Torvalds 		for (k = MAX_NR_ZONES-1; k >= 0; k--) {
1801da177e4SLinus Torvalds 			struct zone *z = &NODE_DATA(nd)->node_zones[k];
1811da177e4SLinus Torvalds 			if (!z->present_pages)
1821da177e4SLinus Torvalds 				continue;
1831da177e4SLinus Torvalds 			zl->zones[num++] = z;
1841da177e4SLinus Torvalds 			if (k > policy_zone)
1851da177e4SLinus Torvalds 				policy_zone = k;
1861da177e4SLinus Torvalds 		}
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	zl->zones[num] = NULL;
1891da177e4SLinus Torvalds 	return zl;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /* Create a new policy */
193dfcd3c0dSAndi Kleen static struct mempolicy *mpol_new(int mode, nodemask_t *nodes)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct mempolicy *policy;
1961da177e4SLinus Torvalds 
197dfcd3c0dSAndi Kleen 	PDprintk("setting mode %d nodes[0] %lx\n", mode, nodes_addr(*nodes)[0]);
1981da177e4SLinus Torvalds 	if (mode == MPOL_DEFAULT)
1991da177e4SLinus Torvalds 		return NULL;
2001da177e4SLinus Torvalds 	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
2011da177e4SLinus Torvalds 	if (!policy)
2021da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2031da177e4SLinus Torvalds 	atomic_set(&policy->refcnt, 1);
2041da177e4SLinus Torvalds 	switch (mode) {
2051da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
206dfcd3c0dSAndi Kleen 		policy->v.nodes = *nodes;
2071da177e4SLinus Torvalds 		break;
2081da177e4SLinus Torvalds 	case MPOL_PREFERRED:
209dfcd3c0dSAndi Kleen 		policy->v.preferred_node = first_node(*nodes);
2101da177e4SLinus Torvalds 		if (policy->v.preferred_node >= MAX_NUMNODES)
2111da177e4SLinus Torvalds 			policy->v.preferred_node = -1;
2121da177e4SLinus Torvalds 		break;
2131da177e4SLinus Torvalds 	case MPOL_BIND:
2141da177e4SLinus Torvalds 		policy->v.zonelist = bind_zonelist(nodes);
2151da177e4SLinus Torvalds 		if (policy->v.zonelist == NULL) {
2161da177e4SLinus Torvalds 			kmem_cache_free(policy_cache, policy);
2171da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
2181da177e4SLinus Torvalds 		}
2191da177e4SLinus Torvalds 		break;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 	policy->policy = mode;
2221da177e4SLinus Torvalds 	return policy;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /* Ensure all existing pages follow the policy. */
226*b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
227dfcd3c0dSAndi Kleen 		unsigned long addr, unsigned long end, nodemask_t *nodes)
2281da177e4SLinus Torvalds {
22991612e0dSHugh Dickins 	pte_t *orig_pte;
23091612e0dSHugh Dickins 	pte_t *pte;
231941150a3SHugh Dickins 
232*b5810039SNick Piggin 	spin_lock(&vma->vm_mm->page_table_lock);
23391612e0dSHugh Dickins 	orig_pte = pte = pte_offset_map(pmd, addr);
23491612e0dSHugh Dickins 	do {
23591612e0dSHugh Dickins 		unsigned long pfn;
23691612e0dSHugh Dickins 		unsigned int nid;
23791612e0dSHugh Dickins 
23891612e0dSHugh Dickins 		if (!pte_present(*pte))
23991612e0dSHugh Dickins 			continue;
24091612e0dSHugh Dickins 		pfn = pte_pfn(*pte);
241*b5810039SNick Piggin 		if (!pfn_valid(pfn)) {
242*b5810039SNick Piggin 			print_bad_pte(vma, *pte, addr);
24391612e0dSHugh Dickins 			continue;
244*b5810039SNick Piggin 		}
24591612e0dSHugh Dickins 		nid = pfn_to_nid(pfn);
246dfcd3c0dSAndi Kleen 		if (!node_isset(nid, *nodes))
2471da177e4SLinus Torvalds 			break;
24891612e0dSHugh Dickins 	} while (pte++, addr += PAGE_SIZE, addr != end);
24991612e0dSHugh Dickins 	pte_unmap(orig_pte);
250*b5810039SNick Piggin 	spin_unlock(&vma->vm_mm->page_table_lock);
25191612e0dSHugh Dickins 	return addr != end;
25291612e0dSHugh Dickins }
25391612e0dSHugh Dickins 
254*b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
255dfcd3c0dSAndi Kleen 		unsigned long addr, unsigned long end, nodemask_t *nodes)
25691612e0dSHugh Dickins {
25791612e0dSHugh Dickins 	pmd_t *pmd;
25891612e0dSHugh Dickins 	unsigned long next;
25991612e0dSHugh Dickins 
26091612e0dSHugh Dickins 	pmd = pmd_offset(pud, addr);
26191612e0dSHugh Dickins 	do {
26291612e0dSHugh Dickins 		next = pmd_addr_end(addr, end);
26391612e0dSHugh Dickins 		if (pmd_none_or_clear_bad(pmd))
26491612e0dSHugh Dickins 			continue;
265*b5810039SNick Piggin 		if (check_pte_range(vma, pmd, addr, next, nodes))
26691612e0dSHugh Dickins 			return -EIO;
26791612e0dSHugh Dickins 	} while (pmd++, addr = next, addr != end);
26891612e0dSHugh Dickins 	return 0;
26991612e0dSHugh Dickins }
27091612e0dSHugh Dickins 
271*b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
272dfcd3c0dSAndi Kleen 		unsigned long addr, unsigned long end, nodemask_t *nodes)
27391612e0dSHugh Dickins {
27491612e0dSHugh Dickins 	pud_t *pud;
27591612e0dSHugh Dickins 	unsigned long next;
27691612e0dSHugh Dickins 
27791612e0dSHugh Dickins 	pud = pud_offset(pgd, addr);
27891612e0dSHugh Dickins 	do {
27991612e0dSHugh Dickins 		next = pud_addr_end(addr, end);
28091612e0dSHugh Dickins 		if (pud_none_or_clear_bad(pud))
28191612e0dSHugh Dickins 			continue;
282*b5810039SNick Piggin 		if (check_pmd_range(vma, pud, addr, next, nodes))
28391612e0dSHugh Dickins 			return -EIO;
28491612e0dSHugh Dickins 	} while (pud++, addr = next, addr != end);
28591612e0dSHugh Dickins 	return 0;
28691612e0dSHugh Dickins }
28791612e0dSHugh Dickins 
288*b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma,
289dfcd3c0dSAndi Kleen 		unsigned long addr, unsigned long end, nodemask_t *nodes)
29091612e0dSHugh Dickins {
29191612e0dSHugh Dickins 	pgd_t *pgd;
29291612e0dSHugh Dickins 	unsigned long next;
29391612e0dSHugh Dickins 
294*b5810039SNick Piggin 	pgd = pgd_offset(vma->vm_mm, addr);
29591612e0dSHugh Dickins 	do {
29691612e0dSHugh Dickins 		next = pgd_addr_end(addr, end);
29791612e0dSHugh Dickins 		if (pgd_none_or_clear_bad(pgd))
29891612e0dSHugh Dickins 			continue;
299*b5810039SNick Piggin 		if (check_pud_range(vma, pgd, addr, next, nodes))
30091612e0dSHugh Dickins 			return -EIO;
30191612e0dSHugh Dickins 	} while (pgd++, addr = next, addr != end);
30291612e0dSHugh Dickins 	return 0;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /* Step 1: check the range */
3061da177e4SLinus Torvalds static struct vm_area_struct *
3071da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
308dfcd3c0dSAndi Kleen 	    nodemask_t *nodes, unsigned long flags)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	int err;
3111da177e4SLinus Torvalds 	struct vm_area_struct *first, *vma, *prev;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	first = find_vma(mm, start);
3141da177e4SLinus Torvalds 	if (!first)
3151da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
316*b5810039SNick Piggin 	if (first->vm_flags & VM_RESERVED)
317*b5810039SNick Piggin 		return ERR_PTR(-EACCES);
3181da177e4SLinus Torvalds 	prev = NULL;
3191da177e4SLinus Torvalds 	for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
3201da177e4SLinus Torvalds 		if (!vma->vm_next && vma->vm_end < end)
3211da177e4SLinus Torvalds 			return ERR_PTR(-EFAULT);
3221da177e4SLinus Torvalds 		if (prev && prev->vm_end < vma->vm_start)
3231da177e4SLinus Torvalds 			return ERR_PTR(-EFAULT);
3241da177e4SLinus Torvalds 		if ((flags & MPOL_MF_STRICT) && !is_vm_hugetlb_page(vma)) {
3255b952b3cSAndi Kleen 			unsigned long endvma = vma->vm_end;
3265b952b3cSAndi Kleen 			if (endvma > end)
3275b952b3cSAndi Kleen 				endvma = end;
3285b952b3cSAndi Kleen 			if (vma->vm_start > start)
3295b952b3cSAndi Kleen 				start = vma->vm_start;
330*b5810039SNick Piggin 			err = check_pgd_range(vma, start, endvma, nodes);
3311da177e4SLinus Torvalds 			if (err) {
3321da177e4SLinus Torvalds 				first = ERR_PTR(err);
3331da177e4SLinus Torvalds 				break;
3341da177e4SLinus Torvalds 			}
3351da177e4SLinus Torvalds 		}
3361da177e4SLinus Torvalds 		prev = vma;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	return first;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* Apply policy to a single VMA */
3421da177e4SLinus Torvalds static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	int err = 0;
3451da177e4SLinus Torvalds 	struct mempolicy *old = vma->vm_policy;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	PDprintk("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
3481da177e4SLinus Torvalds 		 vma->vm_start, vma->vm_end, vma->vm_pgoff,
3491da177e4SLinus Torvalds 		 vma->vm_ops, vma->vm_file,
3501da177e4SLinus Torvalds 		 vma->vm_ops ? vma->vm_ops->set_policy : NULL);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	if (vma->vm_ops && vma->vm_ops->set_policy)
3531da177e4SLinus Torvalds 		err = vma->vm_ops->set_policy(vma, new);
3541da177e4SLinus Torvalds 	if (!err) {
3551da177e4SLinus Torvalds 		mpol_get(new);
3561da177e4SLinus Torvalds 		vma->vm_policy = new;
3571da177e4SLinus Torvalds 		mpol_free(old);
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 	return err;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */
3631da177e4SLinus Torvalds static int mbind_range(struct vm_area_struct *vma, unsigned long start,
3641da177e4SLinus Torvalds 		       unsigned long end, struct mempolicy *new)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct vm_area_struct *next;
3671da177e4SLinus Torvalds 	int err;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	err = 0;
3701da177e4SLinus Torvalds 	for (; vma && vma->vm_start < end; vma = next) {
3711da177e4SLinus Torvalds 		next = vma->vm_next;
3721da177e4SLinus Torvalds 		if (vma->vm_start < start)
3731da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, start, 1);
3741da177e4SLinus Torvalds 		if (!err && vma->vm_end > end)
3751da177e4SLinus Torvalds 			err = split_vma(vma->vm_mm, vma, end, 0);
3761da177e4SLinus Torvalds 		if (!err)
3771da177e4SLinus Torvalds 			err = policy_vma(vma, new);
3781da177e4SLinus Torvalds 		if (err)
3791da177e4SLinus Torvalds 			break;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	return err;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds /* Change policy for a memory range */
3851da177e4SLinus Torvalds asmlinkage long sys_mbind(unsigned long start, unsigned long len,
3861da177e4SLinus Torvalds 			  unsigned long mode,
3871da177e4SLinus Torvalds 			  unsigned long __user *nmask, unsigned long maxnode,
3881da177e4SLinus Torvalds 			  unsigned flags)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	struct vm_area_struct *vma;
3911da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
3921da177e4SLinus Torvalds 	struct mempolicy *new;
3931da177e4SLinus Torvalds 	unsigned long end;
394dfcd3c0dSAndi Kleen 	nodemask_t nodes;
3951da177e4SLinus Torvalds 	int err;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	if ((flags & ~(unsigned long)(MPOL_MF_STRICT)) || mode > MPOL_MAX)
3981da177e4SLinus Torvalds 		return -EINVAL;
3991da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
4001da177e4SLinus Torvalds 		return -EINVAL;
4011da177e4SLinus Torvalds 	if (mode == MPOL_DEFAULT)
4021da177e4SLinus Torvalds 		flags &= ~MPOL_MF_STRICT;
4031da177e4SLinus Torvalds 	len = (len + PAGE_SIZE - 1) & PAGE_MASK;
4041da177e4SLinus Torvalds 	end = start + len;
4051da177e4SLinus Torvalds 	if (end < start)
4061da177e4SLinus Torvalds 		return -EINVAL;
4071da177e4SLinus Torvalds 	if (end == start)
4081da177e4SLinus Torvalds 		return 0;
4091da177e4SLinus Torvalds 
410dfcd3c0dSAndi Kleen 	err = get_nodes(&nodes, nmask, maxnode, mode);
4111da177e4SLinus Torvalds 	if (err)
4121da177e4SLinus Torvalds 		return err;
4131da177e4SLinus Torvalds 
414dfcd3c0dSAndi Kleen 	new = mpol_new(mode, &nodes);
4151da177e4SLinus Torvalds 	if (IS_ERR(new))
4161da177e4SLinus Torvalds 		return PTR_ERR(new);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	PDprintk("mbind %lx-%lx mode:%ld nodes:%lx\n",start,start+len,
419dfcd3c0dSAndi Kleen 			mode,nodes_addr(nodes)[0]);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	down_write(&mm->mmap_sem);
422dfcd3c0dSAndi Kleen 	vma = check_range(mm, start, end, &nodes, flags);
4231da177e4SLinus Torvalds 	err = PTR_ERR(vma);
4241da177e4SLinus Torvalds 	if (!IS_ERR(vma))
4251da177e4SLinus Torvalds 		err = mbind_range(vma, start, end, new);
4261da177e4SLinus Torvalds 	up_write(&mm->mmap_sem);
4271da177e4SLinus Torvalds 	mpol_free(new);
4281da177e4SLinus Torvalds 	return err;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /* Set the process memory policy */
4321da177e4SLinus Torvalds asmlinkage long sys_set_mempolicy(int mode, unsigned long __user *nmask,
4331da177e4SLinus Torvalds 				   unsigned long maxnode)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	int err;
4361da177e4SLinus Torvalds 	struct mempolicy *new;
437dfcd3c0dSAndi Kleen 	nodemask_t nodes;
4381da177e4SLinus Torvalds 
439ba17101bSEric Dumazet 	if (mode < 0 || mode > MPOL_MAX)
4401da177e4SLinus Torvalds 		return -EINVAL;
441dfcd3c0dSAndi Kleen 	err = get_nodes(&nodes, nmask, maxnode, mode);
4421da177e4SLinus Torvalds 	if (err)
4431da177e4SLinus Torvalds 		return err;
444dfcd3c0dSAndi Kleen 	new = mpol_new(mode, &nodes);
4451da177e4SLinus Torvalds 	if (IS_ERR(new))
4461da177e4SLinus Torvalds 		return PTR_ERR(new);
4471da177e4SLinus Torvalds 	mpol_free(current->mempolicy);
4481da177e4SLinus Torvalds 	current->mempolicy = new;
4491da177e4SLinus Torvalds 	if (new && new->policy == MPOL_INTERLEAVE)
450dfcd3c0dSAndi Kleen 		current->il_next = first_node(new->v.nodes);
4511da177e4SLinus Torvalds 	return 0;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /* Fill a zone bitmap for a policy */
455dfcd3c0dSAndi Kleen static void get_zonemask(struct mempolicy *p, nodemask_t *nodes)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	int i;
4581da177e4SLinus Torvalds 
459dfcd3c0dSAndi Kleen 	nodes_clear(*nodes);
4601da177e4SLinus Torvalds 	switch (p->policy) {
4611da177e4SLinus Torvalds 	case MPOL_BIND:
4621da177e4SLinus Torvalds 		for (i = 0; p->v.zonelist->zones[i]; i++)
463dfcd3c0dSAndi Kleen 			node_set(p->v.zonelist->zones[i]->zone_pgdat->node_id, *nodes);
4641da177e4SLinus Torvalds 		break;
4651da177e4SLinus Torvalds 	case MPOL_DEFAULT:
4661da177e4SLinus Torvalds 		break;
4671da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
468dfcd3c0dSAndi Kleen 		*nodes = p->v.nodes;
4691da177e4SLinus Torvalds 		break;
4701da177e4SLinus Torvalds 	case MPOL_PREFERRED:
4711da177e4SLinus Torvalds 		/* or use current node instead of online map? */
4721da177e4SLinus Torvalds 		if (p->v.preferred_node < 0)
473dfcd3c0dSAndi Kleen 			*nodes = node_online_map;
4741da177e4SLinus Torvalds 		else
475dfcd3c0dSAndi Kleen 			node_set(p->v.preferred_node, *nodes);
4761da177e4SLinus Torvalds 		break;
4771da177e4SLinus Torvalds 	default:
4781da177e4SLinus Torvalds 		BUG();
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	struct page *p;
4851da177e4SLinus Torvalds 	int err;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
4881da177e4SLinus Torvalds 	if (err >= 0) {
4891da177e4SLinus Torvalds 		err = page_to_nid(p);
4901da177e4SLinus Torvalds 		put_page(p);
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 	return err;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /* Copy a kernel node mask to user space */
4961da177e4SLinus Torvalds static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
497dfcd3c0dSAndi Kleen 			      nodemask_t *nodes)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	unsigned long copy = ALIGN(maxnode-1, 64) / 8;
500dfcd3c0dSAndi Kleen 	const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	if (copy > nbytes) {
5031da177e4SLinus Torvalds 		if (copy > PAGE_SIZE)
5041da177e4SLinus Torvalds 			return -EINVAL;
5051da177e4SLinus Torvalds 		if (clear_user((char __user *)mask + nbytes, copy - nbytes))
5061da177e4SLinus Torvalds 			return -EFAULT;
5071da177e4SLinus Torvalds 		copy = nbytes;
5081da177e4SLinus Torvalds 	}
509dfcd3c0dSAndi Kleen 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /* Retrieve NUMA policy */
5131da177e4SLinus Torvalds asmlinkage long sys_get_mempolicy(int __user *policy,
5141da177e4SLinus Torvalds 				  unsigned long __user *nmask,
5151da177e4SLinus Torvalds 				  unsigned long maxnode,
5161da177e4SLinus Torvalds 				  unsigned long addr, unsigned long flags)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	int err, pval;
5191da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
5201da177e4SLinus Torvalds 	struct vm_area_struct *vma = NULL;
5211da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	if (flags & ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR))
5241da177e4SLinus Torvalds 		return -EINVAL;
5251da177e4SLinus Torvalds 	if (nmask != NULL && maxnode < MAX_NUMNODES)
5261da177e4SLinus Torvalds 		return -EINVAL;
5271da177e4SLinus Torvalds 	if (flags & MPOL_F_ADDR) {
5281da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
5291da177e4SLinus Torvalds 		vma = find_vma_intersection(mm, addr, addr+1);
5301da177e4SLinus Torvalds 		if (!vma) {
5311da177e4SLinus Torvalds 			up_read(&mm->mmap_sem);
5321da177e4SLinus Torvalds 			return -EFAULT;
5331da177e4SLinus Torvalds 		}
5341da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
5351da177e4SLinus Torvalds 			pol = vma->vm_ops->get_policy(vma, addr);
5361da177e4SLinus Torvalds 		else
5371da177e4SLinus Torvalds 			pol = vma->vm_policy;
5381da177e4SLinus Torvalds 	} else if (addr)
5391da177e4SLinus Torvalds 		return -EINVAL;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	if (!pol)
5421da177e4SLinus Torvalds 		pol = &default_policy;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	if (flags & MPOL_F_NODE) {
5451da177e4SLinus Torvalds 		if (flags & MPOL_F_ADDR) {
5461da177e4SLinus Torvalds 			err = lookup_node(mm, addr);
5471da177e4SLinus Torvalds 			if (err < 0)
5481da177e4SLinus Torvalds 				goto out;
5491da177e4SLinus Torvalds 			pval = err;
5501da177e4SLinus Torvalds 		} else if (pol == current->mempolicy &&
5511da177e4SLinus Torvalds 				pol->policy == MPOL_INTERLEAVE) {
5521da177e4SLinus Torvalds 			pval = current->il_next;
5531da177e4SLinus Torvalds 		} else {
5541da177e4SLinus Torvalds 			err = -EINVAL;
5551da177e4SLinus Torvalds 			goto out;
5561da177e4SLinus Torvalds 		}
5571da177e4SLinus Torvalds 	} else
5581da177e4SLinus Torvalds 		pval = pol->policy;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	if (vma) {
5611da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
5621da177e4SLinus Torvalds 		vma = NULL;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	if (policy && put_user(pval, policy))
5661da177e4SLinus Torvalds 		return -EFAULT;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	err = 0;
5691da177e4SLinus Torvalds 	if (nmask) {
570dfcd3c0dSAndi Kleen 		nodemask_t nodes;
571dfcd3c0dSAndi Kleen 		get_zonemask(pol, &nodes);
572dfcd3c0dSAndi Kleen 		err = copy_nodes_to_user(nmask, maxnode, &nodes);
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds  out:
5761da177e4SLinus Torvalds 	if (vma)
5771da177e4SLinus Torvalds 		up_read(&current->mm->mmap_sem);
5781da177e4SLinus Torvalds 	return err;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds #ifdef CONFIG_COMPAT
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy,
5841da177e4SLinus Torvalds 				     compat_ulong_t __user *nmask,
5851da177e4SLinus Torvalds 				     compat_ulong_t maxnode,
5861da177e4SLinus Torvalds 				     compat_ulong_t addr, compat_ulong_t flags)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	long err;
5891da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
5901da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
5911da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
5941da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (nmask)
5971da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	if (!err && nmask) {
6021da177e4SLinus Torvalds 		err = copy_from_user(bm, nm, alloc_size);
6031da177e4SLinus Torvalds 		/* ensure entire bitmap is zeroed */
6041da177e4SLinus Torvalds 		err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
6051da177e4SLinus Torvalds 		err |= compat_put_bitmap(nmask, bm, nr_bits);
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	return err;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
6121da177e4SLinus Torvalds 				     compat_ulong_t maxnode)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	long err = 0;
6151da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
6161da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
6171da177e4SLinus Torvalds 	DECLARE_BITMAP(bm, MAX_NUMNODES);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
6201da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (nmask) {
6231da177e4SLinus Torvalds 		err = compat_get_bitmap(bm, nmask, nr_bits);
6241da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
6251da177e4SLinus Torvalds 		err |= copy_to_user(nm, bm, alloc_size);
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	if (err)
6291da177e4SLinus Torvalds 		return -EFAULT;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	return sys_set_mempolicy(mode, nm, nr_bits+1);
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
6351da177e4SLinus Torvalds 			     compat_ulong_t mode, compat_ulong_t __user *nmask,
6361da177e4SLinus Torvalds 			     compat_ulong_t maxnode, compat_ulong_t flags)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	long err = 0;
6391da177e4SLinus Torvalds 	unsigned long __user *nm = NULL;
6401da177e4SLinus Torvalds 	unsigned long nr_bits, alloc_size;
641dfcd3c0dSAndi Kleen 	nodemask_t bm;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
6441da177e4SLinus Torvalds 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (nmask) {
647dfcd3c0dSAndi Kleen 		err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
6481da177e4SLinus Torvalds 		nm = compat_alloc_user_space(alloc_size);
649dfcd3c0dSAndi Kleen 		err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
6501da177e4SLinus Torvalds 	}
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (err)
6531da177e4SLinus Torvalds 		return -EFAULT;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds #endif
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds /* Return effective policy for a VMA */
6616e21c8f1SChristoph Lameter struct mempolicy *
6626e21c8f1SChristoph Lameter get_vma_policy(struct task_struct *task, struct vm_area_struct *vma, unsigned long addr)
6631da177e4SLinus Torvalds {
6646e21c8f1SChristoph Lameter 	struct mempolicy *pol = task->mempolicy;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (vma) {
6671da177e4SLinus Torvalds 		if (vma->vm_ops && vma->vm_ops->get_policy)
6681da177e4SLinus Torvalds 		        pol = vma->vm_ops->get_policy(vma, addr);
6691da177e4SLinus Torvalds 		else if (vma->vm_policy &&
6701da177e4SLinus Torvalds 				vma->vm_policy->policy != MPOL_DEFAULT)
6711da177e4SLinus Torvalds 			pol = vma->vm_policy;
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 	if (!pol)
6741da177e4SLinus Torvalds 		pol = &default_policy;
6751da177e4SLinus Torvalds 	return pol;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /* Return a zonelist representing a mempolicy */
679dd0fc66fSAl Viro static struct zonelist *zonelist_policy(gfp_t gfp, struct mempolicy *policy)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	int nd;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	switch (policy->policy) {
6841da177e4SLinus Torvalds 	case MPOL_PREFERRED:
6851da177e4SLinus Torvalds 		nd = policy->v.preferred_node;
6861da177e4SLinus Torvalds 		if (nd < 0)
6871da177e4SLinus Torvalds 			nd = numa_node_id();
6881da177e4SLinus Torvalds 		break;
6891da177e4SLinus Torvalds 	case MPOL_BIND:
6901da177e4SLinus Torvalds 		/* Lower zones don't get a policy applied */
6911da177e4SLinus Torvalds 		/* Careful: current->mems_allowed might have moved */
692af4ca457SAl Viro 		if (gfp_zone(gfp) >= policy_zone)
6931da177e4SLinus Torvalds 			if (cpuset_zonelist_valid_mems_allowed(policy->v.zonelist))
6941da177e4SLinus Torvalds 				return policy->v.zonelist;
6951da177e4SLinus Torvalds 		/*FALL THROUGH*/
6961da177e4SLinus Torvalds 	case MPOL_INTERLEAVE: /* should not happen */
6971da177e4SLinus Torvalds 	case MPOL_DEFAULT:
6981da177e4SLinus Torvalds 		nd = numa_node_id();
6991da177e4SLinus Torvalds 		break;
7001da177e4SLinus Torvalds 	default:
7011da177e4SLinus Torvalds 		nd = 0;
7021da177e4SLinus Torvalds 		BUG();
7031da177e4SLinus Torvalds 	}
704af4ca457SAl Viro 	return NODE_DATA(nd)->node_zonelists + gfp_zone(gfp);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /* Do dynamic interleaving for a process */
7081da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	unsigned nid, next;
7111da177e4SLinus Torvalds 	struct task_struct *me = current;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	nid = me->il_next;
714dfcd3c0dSAndi Kleen 	next = next_node(nid, policy->v.nodes);
7151da177e4SLinus Torvalds 	if (next >= MAX_NUMNODES)
716dfcd3c0dSAndi Kleen 		next = first_node(policy->v.nodes);
7171da177e4SLinus Torvalds 	me->il_next = next;
7181da177e4SLinus Torvalds 	return nid;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */
7221da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol,
7231da177e4SLinus Torvalds 		struct vm_area_struct *vma, unsigned long off)
7241da177e4SLinus Torvalds {
725dfcd3c0dSAndi Kleen 	unsigned nnodes = nodes_weight(pol->v.nodes);
7261da177e4SLinus Torvalds 	unsigned target = (unsigned)off % nnodes;
7271da177e4SLinus Torvalds 	int c;
7281da177e4SLinus Torvalds 	int nid = -1;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	c = 0;
7311da177e4SLinus Torvalds 	do {
732dfcd3c0dSAndi Kleen 		nid = next_node(nid, pol->v.nodes);
7331da177e4SLinus Torvalds 		c++;
7341da177e4SLinus Torvalds 	} while (c <= target);
7351da177e4SLinus Torvalds 	return nid;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /* Allocate a page in interleaved policy.
7391da177e4SLinus Torvalds    Own path because it needs to do special accounting. */
740662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
741662f3a0bSAndi Kleen 					unsigned nid)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	struct zonelist *zl;
7441da177e4SLinus Torvalds 	struct page *page;
7451da177e4SLinus Torvalds 
746af4ca457SAl Viro 	zl = NODE_DATA(nid)->node_zonelists + gfp_zone(gfp);
7471da177e4SLinus Torvalds 	page = __alloc_pages(gfp, order, zl);
7481da177e4SLinus Torvalds 	if (page && page_zone(page) == zl->zones[0]) {
749e7c8d5c9SChristoph Lameter 		zone_pcp(zl->zones[0],get_cpu())->interleave_hit++;
7501da177e4SLinus Torvalds 		put_cpu();
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 	return page;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds /**
7561da177e4SLinus Torvalds  * 	alloc_page_vma	- Allocate a page for a VMA.
7571da177e4SLinus Torvalds  *
7581da177e4SLinus Torvalds  * 	@gfp:
7591da177e4SLinus Torvalds  *      %GFP_USER    user allocation.
7601da177e4SLinus Torvalds  *      %GFP_KERNEL  kernel allocations,
7611da177e4SLinus Torvalds  *      %GFP_HIGHMEM highmem/user allocations,
7621da177e4SLinus Torvalds  *      %GFP_FS      allocation should not call back into a file system.
7631da177e4SLinus Torvalds  *      %GFP_ATOMIC  don't sleep.
7641da177e4SLinus Torvalds  *
7651da177e4SLinus Torvalds  * 	@vma:  Pointer to VMA or NULL if not available.
7661da177e4SLinus Torvalds  *	@addr: Virtual Address of the allocation. Must be inside the VMA.
7671da177e4SLinus Torvalds  *
7681da177e4SLinus Torvalds  * 	This function allocates a page from the kernel page pool and applies
7691da177e4SLinus Torvalds  *	a NUMA policy associated with the VMA or the current process.
7701da177e4SLinus Torvalds  *	When VMA is not NULL caller must hold down_read on the mmap_sem of the
7711da177e4SLinus Torvalds  *	mm_struct of the VMA to prevent it from going away. Should be used for
7721da177e4SLinus Torvalds  *	all allocations for pages that will be mapped into
7731da177e4SLinus Torvalds  * 	user space. Returns NULL when no page can be allocated.
7741da177e4SLinus Torvalds  *
7751da177e4SLinus Torvalds  *	Should be called with the mm_sem of the vma hold.
7761da177e4SLinus Torvalds  */
7771da177e4SLinus Torvalds struct page *
778dd0fc66fSAl Viro alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr)
7791da177e4SLinus Torvalds {
7806e21c8f1SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	cpuset_update_current_mems_allowed();
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	if (unlikely(pol->policy == MPOL_INTERLEAVE)) {
7851da177e4SLinus Torvalds 		unsigned nid;
7861da177e4SLinus Torvalds 		if (vma) {
7871da177e4SLinus Torvalds 			unsigned long off;
7881da177e4SLinus Torvalds 			off = vma->vm_pgoff;
7891da177e4SLinus Torvalds 			off += (addr - vma->vm_start) >> PAGE_SHIFT;
7901da177e4SLinus Torvalds 			nid = offset_il_node(pol, vma, off);
7911da177e4SLinus Torvalds 		} else {
7921da177e4SLinus Torvalds 			/* fall back to process interleaving */
7931da177e4SLinus Torvalds 			nid = interleave_nodes(pol);
7941da177e4SLinus Torvalds 		}
7951da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, 0, nid);
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 	return __alloc_pages(gfp, 0, zonelist_policy(gfp, pol));
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /**
8011da177e4SLinus Torvalds  * 	alloc_pages_current - Allocate pages.
8021da177e4SLinus Torvalds  *
8031da177e4SLinus Torvalds  *	@gfp:
8041da177e4SLinus Torvalds  *		%GFP_USER   user allocation,
8051da177e4SLinus Torvalds  *      	%GFP_KERNEL kernel allocation,
8061da177e4SLinus Torvalds  *      	%GFP_HIGHMEM highmem allocation,
8071da177e4SLinus Torvalds  *      	%GFP_FS     don't call back into a file system.
8081da177e4SLinus Torvalds  *      	%GFP_ATOMIC don't sleep.
8091da177e4SLinus Torvalds  *	@order: Power of two of allocation size in pages. 0 is a single page.
8101da177e4SLinus Torvalds  *
8111da177e4SLinus Torvalds  *	Allocate a page from the kernel page pool.  When not in
8121da177e4SLinus Torvalds  *	interrupt context and apply the current process NUMA policy.
8131da177e4SLinus Torvalds  *	Returns NULL when no page can be allocated.
8141da177e4SLinus Torvalds  *
8151da177e4SLinus Torvalds  *	Don't call cpuset_update_current_mems_allowed() unless
8161da177e4SLinus Torvalds  *	1) it's ok to take cpuset_sem (can WAIT), and
8171da177e4SLinus Torvalds  *	2) allocating for current task (not interrupt).
8181da177e4SLinus Torvalds  */
819dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	struct mempolicy *pol = current->mempolicy;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	if ((gfp & __GFP_WAIT) && !in_interrupt())
8241da177e4SLinus Torvalds 		cpuset_update_current_mems_allowed();
8251da177e4SLinus Torvalds 	if (!pol || in_interrupt())
8261da177e4SLinus Torvalds 		pol = &default_policy;
8271da177e4SLinus Torvalds 	if (pol->policy == MPOL_INTERLEAVE)
8281da177e4SLinus Torvalds 		return alloc_page_interleave(gfp, order, interleave_nodes(pol));
8291da177e4SLinus Torvalds 	return __alloc_pages(gfp, order, zonelist_policy(gfp, pol));
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds /* Slow path of a mempolicy copy */
8341da177e4SLinus Torvalds struct mempolicy *__mpol_copy(struct mempolicy *old)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	if (!new)
8391da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
8401da177e4SLinus Torvalds 	*new = *old;
8411da177e4SLinus Torvalds 	atomic_set(&new->refcnt, 1);
8421da177e4SLinus Torvalds 	if (new->policy == MPOL_BIND) {
8431da177e4SLinus Torvalds 		int sz = ksize(old->v.zonelist);
8441da177e4SLinus Torvalds 		new->v.zonelist = kmalloc(sz, SLAB_KERNEL);
8451da177e4SLinus Torvalds 		if (!new->v.zonelist) {
8461da177e4SLinus Torvalds 			kmem_cache_free(policy_cache, new);
8471da177e4SLinus Torvalds 			return ERR_PTR(-ENOMEM);
8481da177e4SLinus Torvalds 		}
8491da177e4SLinus Torvalds 		memcpy(new->v.zonelist, old->v.zonelist, sz);
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	return new;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */
8551da177e4SLinus Torvalds int __mpol_equal(struct mempolicy *a, struct mempolicy *b)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	if (!a || !b)
8581da177e4SLinus Torvalds 		return 0;
8591da177e4SLinus Torvalds 	if (a->policy != b->policy)
8601da177e4SLinus Torvalds 		return 0;
8611da177e4SLinus Torvalds 	switch (a->policy) {
8621da177e4SLinus Torvalds 	case MPOL_DEFAULT:
8631da177e4SLinus Torvalds 		return 1;
8641da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
865dfcd3c0dSAndi Kleen 		return nodes_equal(a->v.nodes, b->v.nodes);
8661da177e4SLinus Torvalds 	case MPOL_PREFERRED:
8671da177e4SLinus Torvalds 		return a->v.preferred_node == b->v.preferred_node;
8681da177e4SLinus Torvalds 	case MPOL_BIND: {
8691da177e4SLinus Torvalds 		int i;
8701da177e4SLinus Torvalds 		for (i = 0; a->v.zonelist->zones[i]; i++)
8711da177e4SLinus Torvalds 			if (a->v.zonelist->zones[i] != b->v.zonelist->zones[i])
8721da177e4SLinus Torvalds 				return 0;
8731da177e4SLinus Torvalds 		return b->v.zonelist->zones[i] == NULL;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 	default:
8761da177e4SLinus Torvalds 		BUG();
8771da177e4SLinus Torvalds 		return 0;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds /* Slow path of a mpol destructor. */
8821da177e4SLinus Torvalds void __mpol_free(struct mempolicy *p)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&p->refcnt))
8851da177e4SLinus Torvalds 		return;
8861da177e4SLinus Torvalds 	if (p->policy == MPOL_BIND)
8871da177e4SLinus Torvalds 		kfree(p->v.zonelist);
8881da177e4SLinus Torvalds 	p->policy = MPOL_DEFAULT;
8891da177e4SLinus Torvalds 	kmem_cache_free(policy_cache, p);
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds /*
8931da177e4SLinus Torvalds  * Hugetlb policy. Same as above, just works with node numbers instead of
8941da177e4SLinus Torvalds  * zonelists.
8951da177e4SLinus Torvalds  */
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /* Find first node suitable for an allocation */
8981da177e4SLinus Torvalds int mpol_first_node(struct vm_area_struct *vma, unsigned long addr)
8991da177e4SLinus Torvalds {
9006e21c8f1SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	switch (pol->policy) {
9031da177e4SLinus Torvalds 	case MPOL_DEFAULT:
9041da177e4SLinus Torvalds 		return numa_node_id();
9051da177e4SLinus Torvalds 	case MPOL_BIND:
9061da177e4SLinus Torvalds 		return pol->v.zonelist->zones[0]->zone_pgdat->node_id;
9071da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
9081da177e4SLinus Torvalds 		return interleave_nodes(pol);
9091da177e4SLinus Torvalds 	case MPOL_PREFERRED:
9101da177e4SLinus Torvalds 		return pol->v.preferred_node >= 0 ?
9111da177e4SLinus Torvalds 				pol->v.preferred_node : numa_node_id();
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 	BUG();
9141da177e4SLinus Torvalds 	return 0;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds /* Find secondary valid nodes for an allocation */
9181da177e4SLinus Torvalds int mpol_node_valid(int nid, struct vm_area_struct *vma, unsigned long addr)
9191da177e4SLinus Torvalds {
9206e21c8f1SChristoph Lameter 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	switch (pol->policy) {
9231da177e4SLinus Torvalds 	case MPOL_PREFERRED:
9241da177e4SLinus Torvalds 	case MPOL_DEFAULT:
9251da177e4SLinus Torvalds 	case MPOL_INTERLEAVE:
9261da177e4SLinus Torvalds 		return 1;
9271da177e4SLinus Torvalds 	case MPOL_BIND: {
9281da177e4SLinus Torvalds 		struct zone **z;
9291da177e4SLinus Torvalds 		for (z = pol->v.zonelist->zones; *z; z++)
9301da177e4SLinus Torvalds 			if ((*z)->zone_pgdat->node_id == nid)
9311da177e4SLinus Torvalds 				return 1;
9321da177e4SLinus Torvalds 		return 0;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 	default:
9351da177e4SLinus Torvalds 		BUG();
9361da177e4SLinus Torvalds 		return 0;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds /*
9411da177e4SLinus Torvalds  * Shared memory backing store policy support.
9421da177e4SLinus Torvalds  *
9431da177e4SLinus Torvalds  * Remember policies even when nobody has shared memory mapped.
9441da177e4SLinus Torvalds  * The policies are kept in Red-Black tree linked from the inode.
9451da177e4SLinus Torvalds  * They are protected by the sp->lock spinlock, which should be held
9461da177e4SLinus Torvalds  * for any accesses to the tree.
9471da177e4SLinus Torvalds  */
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds /* lookup first element intersecting start-end */
9501da177e4SLinus Torvalds /* Caller holds sp->lock */
9511da177e4SLinus Torvalds static struct sp_node *
9521da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	struct rb_node *n = sp->root.rb_node;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	while (n) {
9571da177e4SLinus Torvalds 		struct sp_node *p = rb_entry(n, struct sp_node, nd);
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 		if (start >= p->end)
9601da177e4SLinus Torvalds 			n = n->rb_right;
9611da177e4SLinus Torvalds 		else if (end <= p->start)
9621da177e4SLinus Torvalds 			n = n->rb_left;
9631da177e4SLinus Torvalds 		else
9641da177e4SLinus Torvalds 			break;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 	if (!n)
9671da177e4SLinus Torvalds 		return NULL;
9681da177e4SLinus Torvalds 	for (;;) {
9691da177e4SLinus Torvalds 		struct sp_node *w = NULL;
9701da177e4SLinus Torvalds 		struct rb_node *prev = rb_prev(n);
9711da177e4SLinus Torvalds 		if (!prev)
9721da177e4SLinus Torvalds 			break;
9731da177e4SLinus Torvalds 		w = rb_entry(prev, struct sp_node, nd);
9741da177e4SLinus Torvalds 		if (w->end <= start)
9751da177e4SLinus Torvalds 			break;
9761da177e4SLinus Torvalds 		n = prev;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds 	return rb_entry(n, struct sp_node, nd);
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds /* Insert a new shared policy into the list. */
9821da177e4SLinus Torvalds /* Caller holds sp->lock */
9831da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	struct rb_node **p = &sp->root.rb_node;
9861da177e4SLinus Torvalds 	struct rb_node *parent = NULL;
9871da177e4SLinus Torvalds 	struct sp_node *nd;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	while (*p) {
9901da177e4SLinus Torvalds 		parent = *p;
9911da177e4SLinus Torvalds 		nd = rb_entry(parent, struct sp_node, nd);
9921da177e4SLinus Torvalds 		if (new->start < nd->start)
9931da177e4SLinus Torvalds 			p = &(*p)->rb_left;
9941da177e4SLinus Torvalds 		else if (new->end > nd->end)
9951da177e4SLinus Torvalds 			p = &(*p)->rb_right;
9961da177e4SLinus Torvalds 		else
9971da177e4SLinus Torvalds 			BUG();
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 	rb_link_node(&new->nd, parent, p);
10001da177e4SLinus Torvalds 	rb_insert_color(&new->nd, &sp->root);
10011da177e4SLinus Torvalds 	PDprintk("inserting %lx-%lx: %d\n", new->start, new->end,
10021da177e4SLinus Torvalds 		 new->policy ? new->policy->policy : 0);
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds /* Find shared policy intersecting idx */
10061da177e4SLinus Torvalds struct mempolicy *
10071da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	struct mempolicy *pol = NULL;
10101da177e4SLinus Torvalds 	struct sp_node *sn;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (!sp->root.rb_node)
10131da177e4SLinus Torvalds 		return NULL;
10141da177e4SLinus Torvalds 	spin_lock(&sp->lock);
10151da177e4SLinus Torvalds 	sn = sp_lookup(sp, idx, idx+1);
10161da177e4SLinus Torvalds 	if (sn) {
10171da177e4SLinus Torvalds 		mpol_get(sn->policy);
10181da177e4SLinus Torvalds 		pol = sn->policy;
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
10211da177e4SLinus Torvalds 	return pol;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	PDprintk("deleting %lx-l%x\n", n->start, n->end);
10271da177e4SLinus Torvalds 	rb_erase(&n->nd, &sp->root);
10281da177e4SLinus Torvalds 	mpol_free(n->policy);
10291da177e4SLinus Torvalds 	kmem_cache_free(sn_cache, n);
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds struct sp_node *
10331da177e4SLinus Torvalds sp_alloc(unsigned long start, unsigned long end, struct mempolicy *pol)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	if (!n)
10381da177e4SLinus Torvalds 		return NULL;
10391da177e4SLinus Torvalds 	n->start = start;
10401da177e4SLinus Torvalds 	n->end = end;
10411da177e4SLinus Torvalds 	mpol_get(pol);
10421da177e4SLinus Torvalds 	n->policy = pol;
10431da177e4SLinus Torvalds 	return n;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds /* Replace a policy range. */
10471da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
10481da177e4SLinus Torvalds 				 unsigned long end, struct sp_node *new)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	struct sp_node *n, *new2 = NULL;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds restart:
10531da177e4SLinus Torvalds 	spin_lock(&sp->lock);
10541da177e4SLinus Torvalds 	n = sp_lookup(sp, start, end);
10551da177e4SLinus Torvalds 	/* Take care of old policies in the same range. */
10561da177e4SLinus Torvalds 	while (n && n->start < end) {
10571da177e4SLinus Torvalds 		struct rb_node *next = rb_next(&n->nd);
10581da177e4SLinus Torvalds 		if (n->start >= start) {
10591da177e4SLinus Torvalds 			if (n->end <= end)
10601da177e4SLinus Torvalds 				sp_delete(sp, n);
10611da177e4SLinus Torvalds 			else
10621da177e4SLinus Torvalds 				n->start = end;
10631da177e4SLinus Torvalds 		} else {
10641da177e4SLinus Torvalds 			/* Old policy spanning whole new range. */
10651da177e4SLinus Torvalds 			if (n->end > end) {
10661da177e4SLinus Torvalds 				if (!new2) {
10671da177e4SLinus Torvalds 					spin_unlock(&sp->lock);
10681da177e4SLinus Torvalds 					new2 = sp_alloc(end, n->end, n->policy);
10691da177e4SLinus Torvalds 					if (!new2)
10701da177e4SLinus Torvalds 						return -ENOMEM;
10711da177e4SLinus Torvalds 					goto restart;
10721da177e4SLinus Torvalds 				}
10731da177e4SLinus Torvalds 				n->end = start;
10741da177e4SLinus Torvalds 				sp_insert(sp, new2);
10751da177e4SLinus Torvalds 				new2 = NULL;
10761da177e4SLinus Torvalds 				break;
10771da177e4SLinus Torvalds 			} else
10781da177e4SLinus Torvalds 				n->end = start;
10791da177e4SLinus Torvalds 		}
10801da177e4SLinus Torvalds 		if (!next)
10811da177e4SLinus Torvalds 			break;
10821da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 	if (new)
10851da177e4SLinus Torvalds 		sp_insert(sp, new);
10861da177e4SLinus Torvalds 	spin_unlock(&sp->lock);
10871da177e4SLinus Torvalds 	if (new2) {
10881da177e4SLinus Torvalds 		mpol_free(new2->policy);
10891da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new2);
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 	return 0;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info,
10951da177e4SLinus Torvalds 			struct vm_area_struct *vma, struct mempolicy *npol)
10961da177e4SLinus Torvalds {
10971da177e4SLinus Torvalds 	int err;
10981da177e4SLinus Torvalds 	struct sp_node *new = NULL;
10991da177e4SLinus Torvalds 	unsigned long sz = vma_pages(vma);
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	PDprintk("set_shared_policy %lx sz %lu %d %lx\n",
11021da177e4SLinus Torvalds 		 vma->vm_pgoff,
11031da177e4SLinus Torvalds 		 sz, npol? npol->policy : -1,
1104dfcd3c0dSAndi Kleen 		npol ? nodes_addr(npol->v.nodes)[0] : -1);
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	if (npol) {
11071da177e4SLinus Torvalds 		new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
11081da177e4SLinus Torvalds 		if (!new)
11091da177e4SLinus Torvalds 			return -ENOMEM;
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 	err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
11121da177e4SLinus Torvalds 	if (err && new)
11131da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, new);
11141da177e4SLinus Torvalds 	return err;
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */
11181da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p)
11191da177e4SLinus Torvalds {
11201da177e4SLinus Torvalds 	struct sp_node *n;
11211da177e4SLinus Torvalds 	struct rb_node *next;
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	if (!p->root.rb_node)
11241da177e4SLinus Torvalds 		return;
11251da177e4SLinus Torvalds 	spin_lock(&p->lock);
11261da177e4SLinus Torvalds 	next = rb_first(&p->root);
11271da177e4SLinus Torvalds 	while (next) {
11281da177e4SLinus Torvalds 		n = rb_entry(next, struct sp_node, nd);
11291da177e4SLinus Torvalds 		next = rb_next(&n->nd);
113090c5029eSAndi Kleen 		rb_erase(&n->nd, &p->root);
11311da177e4SLinus Torvalds 		mpol_free(n->policy);
11321da177e4SLinus Torvalds 		kmem_cache_free(sn_cache, n);
11331da177e4SLinus Torvalds 	}
11341da177e4SLinus Torvalds 	spin_unlock(&p->lock);
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */
11381da177e4SLinus Torvalds void __init numa_policy_init(void)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	policy_cache = kmem_cache_create("numa_policy",
11411da177e4SLinus Torvalds 					 sizeof(struct mempolicy),
11421da177e4SLinus Torvalds 					 0, SLAB_PANIC, NULL, NULL);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	sn_cache = kmem_cache_create("shared_policy_node",
11451da177e4SLinus Torvalds 				     sizeof(struct sp_node),
11461da177e4SLinus Torvalds 				     0, SLAB_PANIC, NULL, NULL);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	/* Set interleaving policy for system init. This way not all
11491da177e4SLinus Torvalds 	   the data structures allocated at system boot end up in node zero. */
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (sys_set_mempolicy(MPOL_INTERLEAVE, nodes_addr(node_online_map),
11521da177e4SLinus Torvalds 							MAX_NUMNODES) < 0)
11531da177e4SLinus Torvalds 		printk("numa_policy_init: interleaving failed\n");
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds /* Reset policy of current process to default.
11571da177e4SLinus Torvalds  * Assumes fs == KERNEL_DS */
11581da177e4SLinus Torvalds void numa_default_policy(void)
11591da177e4SLinus Torvalds {
11601da177e4SLinus Torvalds 	sys_set_mempolicy(MPOL_DEFAULT, NULL, 0);
11611da177e4SLinus Torvalds }
1162