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> 89dc9aa5b9SChristoph Lameter 901da177e4SLinus Torvalds #include <asm/tlbflush.h> 911da177e4SLinus Torvalds #include <asm/uaccess.h> 921da177e4SLinus Torvalds 9338e35860SChristoph Lameter /* Internal flags */ 94dc9aa5b9SChristoph Lameter #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */ 9538e35860SChristoph Lameter #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */ 961a75a6c8SChristoph Lameter #define MPOL_MF_STATS (MPOL_MF_INTERNAL << 2) /* Gather statistics */ 97dc9aa5b9SChristoph Lameter 981da177e4SLinus Torvalds static kmem_cache_t *policy_cache; 991da177e4SLinus Torvalds static kmem_cache_t *sn_cache; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds #define PDprintk(fmt...) 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* Highest zone. An specific allocation for a zone below that is not 1041da177e4SLinus Torvalds policied. */ 1054be38e35SChristoph Lameter int policy_zone = ZONE_DMA; 1061da177e4SLinus Torvalds 107d42c6997SAndi Kleen struct mempolicy default_policy = { 1081da177e4SLinus Torvalds .refcnt = ATOMIC_INIT(1), /* never free it */ 1091da177e4SLinus Torvalds .policy = MPOL_DEFAULT, 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* Do sanity checking on a policy */ 113dfcd3c0dSAndi Kleen static int mpol_check_policy(int mode, nodemask_t *nodes) 1141da177e4SLinus Torvalds { 115dfcd3c0dSAndi Kleen int empty = nodes_empty(*nodes); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds switch (mode) { 1181da177e4SLinus Torvalds case MPOL_DEFAULT: 1191da177e4SLinus Torvalds if (!empty) 1201da177e4SLinus Torvalds return -EINVAL; 1211da177e4SLinus Torvalds break; 1221da177e4SLinus Torvalds case MPOL_BIND: 1231da177e4SLinus Torvalds case MPOL_INTERLEAVE: 1241da177e4SLinus Torvalds /* Preferred will only use the first bit, but allow 1251da177e4SLinus Torvalds more for now. */ 1261da177e4SLinus Torvalds if (empty) 1271da177e4SLinus Torvalds return -EINVAL; 1281da177e4SLinus Torvalds break; 1291da177e4SLinus Torvalds } 130dfcd3c0dSAndi Kleen return nodes_subset(*nodes, node_online_map) ? 0 : -EINVAL; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds /* Generate a custom zonelist for the BIND policy. */ 133dfcd3c0dSAndi Kleen static struct zonelist *bind_zonelist(nodemask_t *nodes) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds struct zonelist *zl; 1361da177e4SLinus Torvalds int num, max, nd; 1371da177e4SLinus Torvalds 138dfcd3c0dSAndi Kleen max = 1 + MAX_NR_ZONES * nodes_weight(*nodes); 1391da177e4SLinus Torvalds zl = kmalloc(sizeof(void *) * max, GFP_KERNEL); 1401da177e4SLinus Torvalds if (!zl) 1411da177e4SLinus Torvalds return NULL; 1421da177e4SLinus Torvalds num = 0; 1434be38e35SChristoph Lameter for_each_node_mask(nd, *nodes) 1444be38e35SChristoph Lameter zl->zones[num++] = &NODE_DATA(nd)->node_zones[policy_zone]; 1451da177e4SLinus Torvalds zl->zones[num] = NULL; 1461da177e4SLinus Torvalds return zl; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* Create a new policy */ 150dfcd3c0dSAndi Kleen static struct mempolicy *mpol_new(int mode, nodemask_t *nodes) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct mempolicy *policy; 1531da177e4SLinus Torvalds 154dfcd3c0dSAndi Kleen PDprintk("setting mode %d nodes[0] %lx\n", mode, nodes_addr(*nodes)[0]); 1551da177e4SLinus Torvalds if (mode == MPOL_DEFAULT) 1561da177e4SLinus Torvalds return NULL; 1571da177e4SLinus Torvalds policy = kmem_cache_alloc(policy_cache, GFP_KERNEL); 1581da177e4SLinus Torvalds if (!policy) 1591da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 1601da177e4SLinus Torvalds atomic_set(&policy->refcnt, 1); 1611da177e4SLinus Torvalds switch (mode) { 1621da177e4SLinus Torvalds case MPOL_INTERLEAVE: 163dfcd3c0dSAndi Kleen policy->v.nodes = *nodes; 1648f493d79SAndi Kleen if (nodes_weight(*nodes) == 0) { 1658f493d79SAndi Kleen kmem_cache_free(policy_cache, policy); 1668f493d79SAndi Kleen return ERR_PTR(-EINVAL); 1678f493d79SAndi Kleen } 1681da177e4SLinus Torvalds break; 1691da177e4SLinus Torvalds case MPOL_PREFERRED: 170dfcd3c0dSAndi Kleen policy->v.preferred_node = first_node(*nodes); 1711da177e4SLinus Torvalds if (policy->v.preferred_node >= MAX_NUMNODES) 1721da177e4SLinus Torvalds policy->v.preferred_node = -1; 1731da177e4SLinus Torvalds break; 1741da177e4SLinus Torvalds case MPOL_BIND: 1751da177e4SLinus Torvalds policy->v.zonelist = bind_zonelist(nodes); 1761da177e4SLinus Torvalds if (policy->v.zonelist == NULL) { 1771da177e4SLinus Torvalds kmem_cache_free(policy_cache, policy); 1781da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds break; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds policy->policy = mode; 18374cb2155SPaul Jackson policy->cpuset_mems_allowed = cpuset_mems_allowed(current); 1841da177e4SLinus Torvalds return policy; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871a75a6c8SChristoph Lameter static void gather_stats(struct page *, void *); 1886ce3c4c0SChristoph Lameter static void migrate_page_add(struct vm_area_struct *vma, 1896ce3c4c0SChristoph Lameter struct page *page, struct list_head *pagelist, unsigned long flags); 1901a75a6c8SChristoph Lameter 19138e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */ 192b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 193dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 194dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 19538e35860SChristoph Lameter void *private) 1961da177e4SLinus Torvalds { 19791612e0dSHugh Dickins pte_t *orig_pte; 19891612e0dSHugh Dickins pte_t *pte; 199705e87c0SHugh Dickins spinlock_t *ptl; 200941150a3SHugh Dickins 201705e87c0SHugh Dickins orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 20291612e0dSHugh Dickins do { 2036aab341eSLinus Torvalds struct page *page; 20491612e0dSHugh Dickins unsigned int nid; 20591612e0dSHugh Dickins 20691612e0dSHugh Dickins if (!pte_present(*pte)) 20791612e0dSHugh Dickins continue; 2086aab341eSLinus Torvalds page = vm_normal_page(vma, addr, *pte); 2096aab341eSLinus Torvalds if (!page) 21091612e0dSHugh Dickins continue; 211f4598c8bSChristoph Lameter if (PageReserved(page)) 212f4598c8bSChristoph Lameter continue; 2136aab341eSLinus Torvalds nid = page_to_nid(page); 21438e35860SChristoph Lameter if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT)) 21538e35860SChristoph Lameter continue; 21638e35860SChristoph Lameter 2171a75a6c8SChristoph Lameter if (flags & MPOL_MF_STATS) 2181a75a6c8SChristoph Lameter gather_stats(page, private); 219132beacfSChristoph Lameter else if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { 220132beacfSChristoph Lameter spin_unlock(ptl); 22138e35860SChristoph Lameter migrate_page_add(vma, page, private, flags); 222132beacfSChristoph Lameter spin_lock(ptl); 223132beacfSChristoph Lameter } 224dc9aa5b9SChristoph Lameter else 2251da177e4SLinus Torvalds break; 22691612e0dSHugh Dickins } while (pte++, addr += PAGE_SIZE, addr != end); 227705e87c0SHugh Dickins pte_unmap_unlock(orig_pte, ptl); 22891612e0dSHugh Dickins return addr != end; 22991612e0dSHugh Dickins } 23091612e0dSHugh Dickins 231b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud, 232dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 233dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 23438e35860SChristoph Lameter void *private) 23591612e0dSHugh Dickins { 23691612e0dSHugh Dickins pmd_t *pmd; 23791612e0dSHugh Dickins unsigned long next; 23891612e0dSHugh Dickins 23991612e0dSHugh Dickins pmd = pmd_offset(pud, addr); 24091612e0dSHugh Dickins do { 24191612e0dSHugh Dickins next = pmd_addr_end(addr, end); 24291612e0dSHugh Dickins if (pmd_none_or_clear_bad(pmd)) 24391612e0dSHugh Dickins continue; 244dc9aa5b9SChristoph Lameter if (check_pte_range(vma, pmd, addr, next, nodes, 24538e35860SChristoph Lameter flags, private)) 24691612e0dSHugh Dickins return -EIO; 24791612e0dSHugh Dickins } while (pmd++, addr = next, addr != end); 24891612e0dSHugh Dickins return 0; 24991612e0dSHugh Dickins } 25091612e0dSHugh Dickins 251b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 252dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 253dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 25438e35860SChristoph Lameter void *private) 25591612e0dSHugh Dickins { 25691612e0dSHugh Dickins pud_t *pud; 25791612e0dSHugh Dickins unsigned long next; 25891612e0dSHugh Dickins 25991612e0dSHugh Dickins pud = pud_offset(pgd, addr); 26091612e0dSHugh Dickins do { 26191612e0dSHugh Dickins next = pud_addr_end(addr, end); 26291612e0dSHugh Dickins if (pud_none_or_clear_bad(pud)) 26391612e0dSHugh Dickins continue; 264dc9aa5b9SChristoph Lameter if (check_pmd_range(vma, pud, addr, next, nodes, 26538e35860SChristoph Lameter flags, private)) 26691612e0dSHugh Dickins return -EIO; 26791612e0dSHugh Dickins } while (pud++, addr = next, addr != end); 26891612e0dSHugh Dickins return 0; 26991612e0dSHugh Dickins } 27091612e0dSHugh Dickins 271b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma, 272dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 273dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 27438e35860SChristoph Lameter void *private) 27591612e0dSHugh Dickins { 27691612e0dSHugh Dickins pgd_t *pgd; 27791612e0dSHugh Dickins unsigned long next; 27891612e0dSHugh Dickins 279b5810039SNick Piggin pgd = pgd_offset(vma->vm_mm, addr); 28091612e0dSHugh Dickins do { 28191612e0dSHugh Dickins next = pgd_addr_end(addr, end); 28291612e0dSHugh Dickins if (pgd_none_or_clear_bad(pgd)) 28391612e0dSHugh Dickins continue; 284dc9aa5b9SChristoph Lameter if (check_pud_range(vma, pgd, addr, next, nodes, 28538e35860SChristoph Lameter flags, private)) 28691612e0dSHugh Dickins return -EIO; 28791612e0dSHugh Dickins } while (pgd++, addr = next, addr != end); 28891612e0dSHugh Dickins return 0; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 291dc9aa5b9SChristoph Lameter /* Check if a vma is migratable */ 292dc9aa5b9SChristoph Lameter static inline int vma_migratable(struct vm_area_struct *vma) 293dc9aa5b9SChristoph Lameter { 294dc9aa5b9SChristoph Lameter if (vma->vm_flags & ( 295f4598c8bSChristoph Lameter VM_LOCKED|VM_IO|VM_HUGETLB|VM_PFNMAP|VM_RESERVED)) 296dc9aa5b9SChristoph Lameter return 0; 297dc9aa5b9SChristoph Lameter return 1; 298dc9aa5b9SChristoph Lameter } 299dc9aa5b9SChristoph Lameter 300dc9aa5b9SChristoph Lameter /* 301dc9aa5b9SChristoph Lameter * Check if all pages in a range are on a set of nodes. 302dc9aa5b9SChristoph Lameter * If pagelist != NULL then isolate pages from the LRU and 303dc9aa5b9SChristoph Lameter * put them on the pagelist. 304dc9aa5b9SChristoph Lameter */ 3051da177e4SLinus Torvalds static struct vm_area_struct * 3061da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end, 30738e35860SChristoph Lameter const nodemask_t *nodes, unsigned long flags, void *private) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds int err; 3101da177e4SLinus Torvalds struct vm_area_struct *first, *vma, *prev; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds first = find_vma(mm, start); 3131da177e4SLinus Torvalds if (!first) 3141da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 3151da177e4SLinus Torvalds prev = NULL; 3161da177e4SLinus Torvalds for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) { 317dc9aa5b9SChristoph Lameter if (!(flags & MPOL_MF_DISCONTIG_OK)) { 3181da177e4SLinus Torvalds if (!vma->vm_next && vma->vm_end < end) 3191da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 3201da177e4SLinus Torvalds if (prev && prev->vm_end < vma->vm_start) 3211da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 322dc9aa5b9SChristoph Lameter } 323dc9aa5b9SChristoph Lameter if (!is_vm_hugetlb_page(vma) && 324dc9aa5b9SChristoph Lameter ((flags & MPOL_MF_STRICT) || 325dc9aa5b9SChristoph Lameter ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) && 326dc9aa5b9SChristoph Lameter vma_migratable(vma)))) { 3275b952b3cSAndi Kleen unsigned long endvma = vma->vm_end; 328dc9aa5b9SChristoph Lameter 3295b952b3cSAndi Kleen if (endvma > end) 3305b952b3cSAndi Kleen endvma = end; 3315b952b3cSAndi Kleen if (vma->vm_start > start) 3325b952b3cSAndi Kleen start = vma->vm_start; 333dc9aa5b9SChristoph Lameter err = check_pgd_range(vma, start, endvma, nodes, 33438e35860SChristoph Lameter flags, private); 3351da177e4SLinus Torvalds if (err) { 3361da177e4SLinus Torvalds first = ERR_PTR(err); 3371da177e4SLinus Torvalds break; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds prev = vma; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds return first; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* Apply policy to a single VMA */ 3461da177e4SLinus Torvalds static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds int err = 0; 3491da177e4SLinus Torvalds struct mempolicy *old = vma->vm_policy; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds PDprintk("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n", 3521da177e4SLinus Torvalds vma->vm_start, vma->vm_end, vma->vm_pgoff, 3531da177e4SLinus Torvalds vma->vm_ops, vma->vm_file, 3541da177e4SLinus Torvalds vma->vm_ops ? vma->vm_ops->set_policy : NULL); 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->set_policy) 3571da177e4SLinus Torvalds err = vma->vm_ops->set_policy(vma, new); 3581da177e4SLinus Torvalds if (!err) { 3591da177e4SLinus Torvalds mpol_get(new); 3601da177e4SLinus Torvalds vma->vm_policy = new; 3611da177e4SLinus Torvalds mpol_free(old); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds return err; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */ 3671da177e4SLinus Torvalds static int mbind_range(struct vm_area_struct *vma, unsigned long start, 3681da177e4SLinus Torvalds unsigned long end, struct mempolicy *new) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds struct vm_area_struct *next; 3711da177e4SLinus Torvalds int err; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds err = 0; 3741da177e4SLinus Torvalds for (; vma && vma->vm_start < end; vma = next) { 3751da177e4SLinus Torvalds next = vma->vm_next; 3761da177e4SLinus Torvalds if (vma->vm_start < start) 3771da177e4SLinus Torvalds err = split_vma(vma->vm_mm, vma, start, 1); 3781da177e4SLinus Torvalds if (!err && vma->vm_end > end) 3791da177e4SLinus Torvalds err = split_vma(vma->vm_mm, vma, end, 0); 3801da177e4SLinus Torvalds if (!err) 3811da177e4SLinus Torvalds err = policy_vma(vma, new); 3821da177e4SLinus Torvalds if (err) 3831da177e4SLinus Torvalds break; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds return err; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3888bccd85fSChristoph Lameter static int contextualize_policy(int mode, nodemask_t *nodes) 3898bccd85fSChristoph Lameter { 3908bccd85fSChristoph Lameter if (!nodes) 3918bccd85fSChristoph Lameter return 0; 3928bccd85fSChristoph Lameter 393cf2a473cSPaul Jackson cpuset_update_task_memory_state(); 3945966514dSPaul Jackson if (!cpuset_nodes_subset_current_mems_allowed(*nodes)) 3955966514dSPaul Jackson return -EINVAL; 3968bccd85fSChristoph Lameter return mpol_check_policy(mode, nodes); 3978bccd85fSChristoph Lameter } 3988bccd85fSChristoph Lameter 3991da177e4SLinus Torvalds /* Set the process memory policy */ 4008bccd85fSChristoph Lameter long do_set_mempolicy(int mode, nodemask_t *nodes) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds struct mempolicy *new; 4031da177e4SLinus Torvalds 4048bccd85fSChristoph Lameter if (contextualize_policy(mode, nodes)) 4051da177e4SLinus Torvalds return -EINVAL; 4068bccd85fSChristoph Lameter new = mpol_new(mode, nodes); 4071da177e4SLinus Torvalds if (IS_ERR(new)) 4081da177e4SLinus Torvalds return PTR_ERR(new); 4091da177e4SLinus Torvalds mpol_free(current->mempolicy); 4101da177e4SLinus Torvalds current->mempolicy = new; 4111da177e4SLinus Torvalds if (new && new->policy == MPOL_INTERLEAVE) 412dfcd3c0dSAndi Kleen current->il_next = first_node(new->v.nodes); 4131da177e4SLinus Torvalds return 0; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /* Fill a zone bitmap for a policy */ 417dfcd3c0dSAndi Kleen static void get_zonemask(struct mempolicy *p, nodemask_t *nodes) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds int i; 4201da177e4SLinus Torvalds 421dfcd3c0dSAndi Kleen nodes_clear(*nodes); 4221da177e4SLinus Torvalds switch (p->policy) { 4231da177e4SLinus Torvalds case MPOL_BIND: 4241da177e4SLinus Torvalds for (i = 0; p->v.zonelist->zones[i]; i++) 4258bccd85fSChristoph Lameter node_set(p->v.zonelist->zones[i]->zone_pgdat->node_id, 4268bccd85fSChristoph Lameter *nodes); 4271da177e4SLinus Torvalds break; 4281da177e4SLinus Torvalds case MPOL_DEFAULT: 4291da177e4SLinus Torvalds break; 4301da177e4SLinus Torvalds case MPOL_INTERLEAVE: 431dfcd3c0dSAndi Kleen *nodes = p->v.nodes; 4321da177e4SLinus Torvalds break; 4331da177e4SLinus Torvalds case MPOL_PREFERRED: 4341da177e4SLinus Torvalds /* or use current node instead of online map? */ 4351da177e4SLinus Torvalds if (p->v.preferred_node < 0) 436dfcd3c0dSAndi Kleen *nodes = node_online_map; 4371da177e4SLinus Torvalds else 438dfcd3c0dSAndi Kleen node_set(p->v.preferred_node, *nodes); 4391da177e4SLinus Torvalds break; 4401da177e4SLinus Torvalds default: 4411da177e4SLinus Torvalds BUG(); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds struct page *p; 4481da177e4SLinus Torvalds int err; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL); 4511da177e4SLinus Torvalds if (err >= 0) { 4521da177e4SLinus Torvalds err = page_to_nid(p); 4531da177e4SLinus Torvalds put_page(p); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds return err; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds /* Retrieve NUMA policy */ 4598bccd85fSChristoph Lameter long do_get_mempolicy(int *policy, nodemask_t *nmask, 4601da177e4SLinus Torvalds unsigned long addr, unsigned long flags) 4611da177e4SLinus Torvalds { 4628bccd85fSChristoph Lameter int err; 4631da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 4641da177e4SLinus Torvalds struct vm_area_struct *vma = NULL; 4651da177e4SLinus Torvalds struct mempolicy *pol = current->mempolicy; 4661da177e4SLinus Torvalds 467cf2a473cSPaul Jackson cpuset_update_task_memory_state(); 4681da177e4SLinus Torvalds if (flags & ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR)) 4691da177e4SLinus Torvalds return -EINVAL; 4701da177e4SLinus Torvalds if (flags & MPOL_F_ADDR) { 4711da177e4SLinus Torvalds down_read(&mm->mmap_sem); 4721da177e4SLinus Torvalds vma = find_vma_intersection(mm, addr, addr+1); 4731da177e4SLinus Torvalds if (!vma) { 4741da177e4SLinus Torvalds up_read(&mm->mmap_sem); 4751da177e4SLinus Torvalds return -EFAULT; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->get_policy) 4781da177e4SLinus Torvalds pol = vma->vm_ops->get_policy(vma, addr); 4791da177e4SLinus Torvalds else 4801da177e4SLinus Torvalds pol = vma->vm_policy; 4811da177e4SLinus Torvalds } else if (addr) 4821da177e4SLinus Torvalds return -EINVAL; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds if (!pol) 4851da177e4SLinus Torvalds pol = &default_policy; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds if (flags & MPOL_F_NODE) { 4881da177e4SLinus Torvalds if (flags & MPOL_F_ADDR) { 4891da177e4SLinus Torvalds err = lookup_node(mm, addr); 4901da177e4SLinus Torvalds if (err < 0) 4911da177e4SLinus Torvalds goto out; 4928bccd85fSChristoph Lameter *policy = err; 4931da177e4SLinus Torvalds } else if (pol == current->mempolicy && 4941da177e4SLinus Torvalds pol->policy == MPOL_INTERLEAVE) { 4958bccd85fSChristoph Lameter *policy = current->il_next; 4961da177e4SLinus Torvalds } else { 4971da177e4SLinus Torvalds err = -EINVAL; 4981da177e4SLinus Torvalds goto out; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds } else 5018bccd85fSChristoph Lameter *policy = pol->policy; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (vma) { 5041da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 5051da177e4SLinus Torvalds vma = NULL; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds err = 0; 5098bccd85fSChristoph Lameter if (nmask) 5108bccd85fSChristoph Lameter get_zonemask(pol, nmask); 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds out: 5131da177e4SLinus Torvalds if (vma) 5141da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 5151da177e4SLinus Torvalds return err; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5188bccd85fSChristoph Lameter /* 5196ce3c4c0SChristoph Lameter * page migration 5206ce3c4c0SChristoph Lameter */ 5216ce3c4c0SChristoph Lameter 5226ce3c4c0SChristoph Lameter /* Check if we are the only process mapping the page in question */ 5236ce3c4c0SChristoph Lameter static inline int single_mm_mapping(struct mm_struct *mm, 5246ce3c4c0SChristoph Lameter struct address_space *mapping) 5256ce3c4c0SChristoph Lameter { 5266ce3c4c0SChristoph Lameter struct vm_area_struct *vma; 5276ce3c4c0SChristoph Lameter struct prio_tree_iter iter; 5286ce3c4c0SChristoph Lameter int rc = 1; 5296ce3c4c0SChristoph Lameter 5306ce3c4c0SChristoph Lameter spin_lock(&mapping->i_mmap_lock); 5316ce3c4c0SChristoph Lameter vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 5326ce3c4c0SChristoph Lameter if (mm != vma->vm_mm) { 5336ce3c4c0SChristoph Lameter rc = 0; 5346ce3c4c0SChristoph Lameter goto out; 5356ce3c4c0SChristoph Lameter } 5366ce3c4c0SChristoph Lameter list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 5376ce3c4c0SChristoph Lameter if (mm != vma->vm_mm) { 5386ce3c4c0SChristoph Lameter rc = 0; 5396ce3c4c0SChristoph Lameter goto out; 5406ce3c4c0SChristoph Lameter } 5416ce3c4c0SChristoph Lameter out: 5426ce3c4c0SChristoph Lameter spin_unlock(&mapping->i_mmap_lock); 5436ce3c4c0SChristoph Lameter return rc; 5446ce3c4c0SChristoph Lameter } 5456ce3c4c0SChristoph Lameter 5466ce3c4c0SChristoph Lameter /* 5476ce3c4c0SChristoph Lameter * Add a page to be migrated to the pagelist 5486ce3c4c0SChristoph Lameter */ 5496ce3c4c0SChristoph Lameter static void migrate_page_add(struct vm_area_struct *vma, 5506ce3c4c0SChristoph Lameter struct page *page, struct list_head *pagelist, unsigned long flags) 5516ce3c4c0SChristoph Lameter { 5526ce3c4c0SChristoph Lameter /* 5536ce3c4c0SChristoph Lameter * Avoid migrating a page that is shared by others and not writable. 5546ce3c4c0SChristoph Lameter */ 5556ce3c4c0SChristoph Lameter if ((flags & MPOL_MF_MOVE_ALL) || !page->mapping || PageAnon(page) || 5566ce3c4c0SChristoph Lameter mapping_writably_mapped(page->mapping) || 5576ce3c4c0SChristoph Lameter single_mm_mapping(vma->vm_mm, page->mapping)) { 5586ce3c4c0SChristoph Lameter int rc = isolate_lru_page(page); 5596ce3c4c0SChristoph Lameter 5606ce3c4c0SChristoph Lameter if (rc == 1) 5616ce3c4c0SChristoph Lameter list_add(&page->lru, pagelist); 5626ce3c4c0SChristoph Lameter /* 5636ce3c4c0SChristoph Lameter * If the isolate attempt was not successful then we just 5646ce3c4c0SChristoph Lameter * encountered an unswappable page. Something must be wrong. 5656ce3c4c0SChristoph Lameter */ 5666ce3c4c0SChristoph Lameter WARN_ON(rc == 0); 5676ce3c4c0SChristoph Lameter } 5686ce3c4c0SChristoph Lameter } 5696ce3c4c0SChristoph Lameter 5706ce3c4c0SChristoph Lameter static int swap_pages(struct list_head *pagelist) 5716ce3c4c0SChristoph Lameter { 5726ce3c4c0SChristoph Lameter LIST_HEAD(moved); 5736ce3c4c0SChristoph Lameter LIST_HEAD(failed); 5746ce3c4c0SChristoph Lameter int n; 5756ce3c4c0SChristoph Lameter 5766ce3c4c0SChristoph Lameter n = migrate_pages(pagelist, NULL, &moved, &failed); 5776ce3c4c0SChristoph Lameter putback_lru_pages(&failed); 5786ce3c4c0SChristoph Lameter putback_lru_pages(&moved); 5796ce3c4c0SChristoph Lameter 5806ce3c4c0SChristoph Lameter return n; 5816ce3c4c0SChristoph Lameter } 5826ce3c4c0SChristoph Lameter 5836ce3c4c0SChristoph Lameter /* 58439743889SChristoph Lameter * For now migrate_pages simply swaps out the pages from nodes that are in 58539743889SChristoph Lameter * the source set but not in the target set. In the future, we would 58639743889SChristoph Lameter * want a function that moves pages between the two nodesets in such 58739743889SChristoph Lameter * a way as to preserve the physical layout as much as possible. 58839743889SChristoph Lameter * 58939743889SChristoph Lameter * Returns the number of page that could not be moved. 59039743889SChristoph Lameter */ 59139743889SChristoph Lameter int do_migrate_pages(struct mm_struct *mm, 59239743889SChristoph Lameter const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags) 59339743889SChristoph Lameter { 59439743889SChristoph Lameter LIST_HEAD(pagelist); 59539743889SChristoph Lameter int count = 0; 59639743889SChristoph Lameter nodemask_t nodes; 59739743889SChristoph Lameter 59839743889SChristoph Lameter nodes_andnot(nodes, *from_nodes, *to_nodes); 59939743889SChristoph Lameter 60039743889SChristoph Lameter down_read(&mm->mmap_sem); 60139743889SChristoph Lameter check_range(mm, mm->mmap->vm_start, TASK_SIZE, &nodes, 60239743889SChristoph Lameter flags | MPOL_MF_DISCONTIG_OK, &pagelist); 603d4984711SChristoph Lameter 60439743889SChristoph Lameter if (!list_empty(&pagelist)) { 605d4984711SChristoph Lameter count = swap_pages(&pagelist); 606d4984711SChristoph Lameter putback_lru_pages(&pagelist); 60739743889SChristoph Lameter } 608d4984711SChristoph Lameter 60939743889SChristoph Lameter up_read(&mm->mmap_sem); 61039743889SChristoph Lameter return count; 61139743889SChristoph Lameter } 61239743889SChristoph Lameter 6136ce3c4c0SChristoph Lameter long do_mbind(unsigned long start, unsigned long len, 6146ce3c4c0SChristoph Lameter unsigned long mode, nodemask_t *nmask, unsigned long flags) 6156ce3c4c0SChristoph Lameter { 6166ce3c4c0SChristoph Lameter struct vm_area_struct *vma; 6176ce3c4c0SChristoph Lameter struct mm_struct *mm = current->mm; 6186ce3c4c0SChristoph Lameter struct mempolicy *new; 6196ce3c4c0SChristoph Lameter unsigned long end; 6206ce3c4c0SChristoph Lameter int err; 6216ce3c4c0SChristoph Lameter LIST_HEAD(pagelist); 6226ce3c4c0SChristoph Lameter 6236ce3c4c0SChristoph Lameter if ((flags & ~(unsigned long)(MPOL_MF_STRICT | 6246ce3c4c0SChristoph Lameter MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) 6256ce3c4c0SChristoph Lameter || mode > MPOL_MAX) 6266ce3c4c0SChristoph Lameter return -EINVAL; 6276ce3c4c0SChristoph Lameter if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_RESOURCE)) 6286ce3c4c0SChristoph Lameter return -EPERM; 6296ce3c4c0SChristoph Lameter 6306ce3c4c0SChristoph Lameter if (start & ~PAGE_MASK) 6316ce3c4c0SChristoph Lameter return -EINVAL; 6326ce3c4c0SChristoph Lameter 6336ce3c4c0SChristoph Lameter if (mode == MPOL_DEFAULT) 6346ce3c4c0SChristoph Lameter flags &= ~MPOL_MF_STRICT; 6356ce3c4c0SChristoph Lameter 6366ce3c4c0SChristoph Lameter len = (len + PAGE_SIZE - 1) & PAGE_MASK; 6376ce3c4c0SChristoph Lameter end = start + len; 6386ce3c4c0SChristoph Lameter 6396ce3c4c0SChristoph Lameter if (end < start) 6406ce3c4c0SChristoph Lameter return -EINVAL; 6416ce3c4c0SChristoph Lameter if (end == start) 6426ce3c4c0SChristoph Lameter return 0; 6436ce3c4c0SChristoph Lameter 6446ce3c4c0SChristoph Lameter if (mpol_check_policy(mode, nmask)) 6456ce3c4c0SChristoph Lameter return -EINVAL; 6466ce3c4c0SChristoph Lameter 6476ce3c4c0SChristoph Lameter new = mpol_new(mode, nmask); 6486ce3c4c0SChristoph Lameter if (IS_ERR(new)) 6496ce3c4c0SChristoph Lameter return PTR_ERR(new); 6506ce3c4c0SChristoph Lameter 6516ce3c4c0SChristoph Lameter /* 6526ce3c4c0SChristoph Lameter * If we are using the default policy then operation 6536ce3c4c0SChristoph Lameter * on discontinuous address spaces is okay after all 6546ce3c4c0SChristoph Lameter */ 6556ce3c4c0SChristoph Lameter if (!new) 6566ce3c4c0SChristoph Lameter flags |= MPOL_MF_DISCONTIG_OK; 6576ce3c4c0SChristoph Lameter 6586ce3c4c0SChristoph Lameter PDprintk("mbind %lx-%lx mode:%ld nodes:%lx\n",start,start+len, 6596ce3c4c0SChristoph Lameter mode,nodes_addr(nodes)[0]); 6606ce3c4c0SChristoph Lameter 6616ce3c4c0SChristoph Lameter down_write(&mm->mmap_sem); 6626ce3c4c0SChristoph Lameter vma = check_range(mm, start, end, nmask, 6636ce3c4c0SChristoph Lameter flags | MPOL_MF_INVERT, &pagelist); 6646ce3c4c0SChristoph Lameter 6656ce3c4c0SChristoph Lameter err = PTR_ERR(vma); 6666ce3c4c0SChristoph Lameter if (!IS_ERR(vma)) { 6676ce3c4c0SChristoph Lameter int nr_failed = 0; 6686ce3c4c0SChristoph Lameter 6696ce3c4c0SChristoph Lameter err = mbind_range(vma, start, end, new); 6706ce3c4c0SChristoph Lameter if (!list_empty(&pagelist)) 6716ce3c4c0SChristoph Lameter nr_failed = swap_pages(&pagelist); 6726ce3c4c0SChristoph Lameter 6736ce3c4c0SChristoph Lameter if (!err && nr_failed && (flags & MPOL_MF_STRICT)) 6746ce3c4c0SChristoph Lameter err = -EIO; 6756ce3c4c0SChristoph Lameter } 6766ce3c4c0SChristoph Lameter if (!list_empty(&pagelist)) 6776ce3c4c0SChristoph Lameter putback_lru_pages(&pagelist); 6786ce3c4c0SChristoph Lameter 6796ce3c4c0SChristoph Lameter up_write(&mm->mmap_sem); 6806ce3c4c0SChristoph Lameter mpol_free(new); 6816ce3c4c0SChristoph Lameter return err; 6826ce3c4c0SChristoph Lameter } 6836ce3c4c0SChristoph Lameter 68439743889SChristoph Lameter /* 6858bccd85fSChristoph Lameter * User space interface with variable sized bitmaps for nodelists. 6868bccd85fSChristoph Lameter */ 6878bccd85fSChristoph Lameter 6888bccd85fSChristoph Lameter /* Copy a node mask from user space. */ 68939743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, 6908bccd85fSChristoph Lameter unsigned long maxnode) 6918bccd85fSChristoph Lameter { 6928bccd85fSChristoph Lameter unsigned long k; 6938bccd85fSChristoph Lameter unsigned long nlongs; 6948bccd85fSChristoph Lameter unsigned long endmask; 6958bccd85fSChristoph Lameter 6968bccd85fSChristoph Lameter --maxnode; 6978bccd85fSChristoph Lameter nodes_clear(*nodes); 6988bccd85fSChristoph Lameter if (maxnode == 0 || !nmask) 6998bccd85fSChristoph Lameter return 0; 7008bccd85fSChristoph Lameter 7018bccd85fSChristoph Lameter nlongs = BITS_TO_LONGS(maxnode); 7028bccd85fSChristoph Lameter if ((maxnode % BITS_PER_LONG) == 0) 7038bccd85fSChristoph Lameter endmask = ~0UL; 7048bccd85fSChristoph Lameter else 7058bccd85fSChristoph Lameter endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1; 7068bccd85fSChristoph Lameter 7078bccd85fSChristoph Lameter /* When the user specified more nodes than supported just check 7088bccd85fSChristoph Lameter if the non supported part is all zero. */ 7098bccd85fSChristoph Lameter if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) { 7108bccd85fSChristoph Lameter if (nlongs > PAGE_SIZE/sizeof(long)) 7118bccd85fSChristoph Lameter return -EINVAL; 7128bccd85fSChristoph Lameter for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) { 7138bccd85fSChristoph Lameter unsigned long t; 7148bccd85fSChristoph Lameter if (get_user(t, nmask + k)) 7158bccd85fSChristoph Lameter return -EFAULT; 7168bccd85fSChristoph Lameter if (k == nlongs - 1) { 7178bccd85fSChristoph Lameter if (t & endmask) 7188bccd85fSChristoph Lameter return -EINVAL; 7198bccd85fSChristoph Lameter } else if (t) 7208bccd85fSChristoph Lameter return -EINVAL; 7218bccd85fSChristoph Lameter } 7228bccd85fSChristoph Lameter nlongs = BITS_TO_LONGS(MAX_NUMNODES); 7238bccd85fSChristoph Lameter endmask = ~0UL; 7248bccd85fSChristoph Lameter } 7258bccd85fSChristoph Lameter 7268bccd85fSChristoph Lameter if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long))) 7278bccd85fSChristoph Lameter return -EFAULT; 7288bccd85fSChristoph Lameter nodes_addr(*nodes)[nlongs-1] &= endmask; 7298bccd85fSChristoph Lameter return 0; 7308bccd85fSChristoph Lameter } 7318bccd85fSChristoph Lameter 7328bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */ 7338bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, 7348bccd85fSChristoph Lameter nodemask_t *nodes) 7358bccd85fSChristoph Lameter { 7368bccd85fSChristoph Lameter unsigned long copy = ALIGN(maxnode-1, 64) / 8; 7378bccd85fSChristoph Lameter const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long); 7388bccd85fSChristoph Lameter 7398bccd85fSChristoph Lameter if (copy > nbytes) { 7408bccd85fSChristoph Lameter if (copy > PAGE_SIZE) 7418bccd85fSChristoph Lameter return -EINVAL; 7428bccd85fSChristoph Lameter if (clear_user((char __user *)mask + nbytes, copy - nbytes)) 7438bccd85fSChristoph Lameter return -EFAULT; 7448bccd85fSChristoph Lameter copy = nbytes; 7458bccd85fSChristoph Lameter } 7468bccd85fSChristoph Lameter return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; 7478bccd85fSChristoph Lameter } 7488bccd85fSChristoph Lameter 7498bccd85fSChristoph Lameter asmlinkage long sys_mbind(unsigned long start, unsigned long len, 7508bccd85fSChristoph Lameter unsigned long mode, 7518bccd85fSChristoph Lameter unsigned long __user *nmask, unsigned long maxnode, 7528bccd85fSChristoph Lameter unsigned flags) 7538bccd85fSChristoph Lameter { 7548bccd85fSChristoph Lameter nodemask_t nodes; 7558bccd85fSChristoph Lameter int err; 7568bccd85fSChristoph Lameter 7578bccd85fSChristoph Lameter err = get_nodes(&nodes, nmask, maxnode); 7588bccd85fSChristoph Lameter if (err) 7598bccd85fSChristoph Lameter return err; 7608bccd85fSChristoph Lameter return do_mbind(start, len, mode, &nodes, flags); 7618bccd85fSChristoph Lameter } 7628bccd85fSChristoph Lameter 7638bccd85fSChristoph Lameter /* Set the process memory policy */ 7648bccd85fSChristoph Lameter asmlinkage long sys_set_mempolicy(int mode, unsigned long __user *nmask, 7658bccd85fSChristoph Lameter unsigned long maxnode) 7668bccd85fSChristoph Lameter { 7678bccd85fSChristoph Lameter int err; 7688bccd85fSChristoph Lameter nodemask_t nodes; 7698bccd85fSChristoph Lameter 7708bccd85fSChristoph Lameter if (mode < 0 || mode > MPOL_MAX) 7718bccd85fSChristoph Lameter return -EINVAL; 7728bccd85fSChristoph Lameter err = get_nodes(&nodes, nmask, maxnode); 7738bccd85fSChristoph Lameter if (err) 7748bccd85fSChristoph Lameter return err; 7758bccd85fSChristoph Lameter return do_set_mempolicy(mode, &nodes); 7768bccd85fSChristoph Lameter } 7778bccd85fSChristoph Lameter 77839743889SChristoph Lameter asmlinkage long sys_migrate_pages(pid_t pid, unsigned long maxnode, 77939743889SChristoph Lameter const unsigned long __user *old_nodes, 78039743889SChristoph Lameter const unsigned long __user *new_nodes) 78139743889SChristoph Lameter { 78239743889SChristoph Lameter struct mm_struct *mm; 78339743889SChristoph Lameter struct task_struct *task; 78439743889SChristoph Lameter nodemask_t old; 78539743889SChristoph Lameter nodemask_t new; 78639743889SChristoph Lameter nodemask_t task_nodes; 78739743889SChristoph Lameter int err; 78839743889SChristoph Lameter 78939743889SChristoph Lameter err = get_nodes(&old, old_nodes, maxnode); 79039743889SChristoph Lameter if (err) 79139743889SChristoph Lameter return err; 79239743889SChristoph Lameter 79339743889SChristoph Lameter err = get_nodes(&new, new_nodes, maxnode); 79439743889SChristoph Lameter if (err) 79539743889SChristoph Lameter return err; 79639743889SChristoph Lameter 79739743889SChristoph Lameter /* Find the mm_struct */ 79839743889SChristoph Lameter read_lock(&tasklist_lock); 79939743889SChristoph Lameter task = pid ? find_task_by_pid(pid) : current; 80039743889SChristoph Lameter if (!task) { 80139743889SChristoph Lameter read_unlock(&tasklist_lock); 80239743889SChristoph Lameter return -ESRCH; 80339743889SChristoph Lameter } 80439743889SChristoph Lameter mm = get_task_mm(task); 80539743889SChristoph Lameter read_unlock(&tasklist_lock); 80639743889SChristoph Lameter 80739743889SChristoph Lameter if (!mm) 80839743889SChristoph Lameter return -EINVAL; 80939743889SChristoph Lameter 81039743889SChristoph Lameter /* 81139743889SChristoph Lameter * Check if this process has the right to modify the specified 81239743889SChristoph Lameter * process. The right exists if the process has administrative 81339743889SChristoph Lameter * capabilities, superuser priviledges or the same 81439743889SChristoph Lameter * userid as the target process. 81539743889SChristoph Lameter */ 81639743889SChristoph Lameter if ((current->euid != task->suid) && (current->euid != task->uid) && 81739743889SChristoph Lameter (current->uid != task->suid) && (current->uid != task->uid) && 81839743889SChristoph Lameter !capable(CAP_SYS_ADMIN)) { 81939743889SChristoph Lameter err = -EPERM; 82039743889SChristoph Lameter goto out; 82139743889SChristoph Lameter } 82239743889SChristoph Lameter 82339743889SChristoph Lameter task_nodes = cpuset_mems_allowed(task); 82439743889SChristoph Lameter /* Is the user allowed to access the target nodes? */ 82539743889SChristoph Lameter if (!nodes_subset(new, task_nodes) && !capable(CAP_SYS_ADMIN)) { 82639743889SChristoph Lameter err = -EPERM; 82739743889SChristoph Lameter goto out; 82839743889SChristoph Lameter } 82939743889SChristoph Lameter 83039743889SChristoph Lameter err = do_migrate_pages(mm, &old, &new, MPOL_MF_MOVE); 83139743889SChristoph Lameter out: 83239743889SChristoph Lameter mmput(mm); 83339743889SChristoph Lameter return err; 83439743889SChristoph Lameter } 83539743889SChristoph Lameter 83639743889SChristoph Lameter 8378bccd85fSChristoph Lameter /* Retrieve NUMA policy */ 8388bccd85fSChristoph Lameter asmlinkage long sys_get_mempolicy(int __user *policy, 8398bccd85fSChristoph Lameter unsigned long __user *nmask, 8408bccd85fSChristoph Lameter unsigned long maxnode, 8418bccd85fSChristoph Lameter unsigned long addr, unsigned long flags) 8428bccd85fSChristoph Lameter { 8438bccd85fSChristoph Lameter int err, pval; 8448bccd85fSChristoph Lameter nodemask_t nodes; 8458bccd85fSChristoph Lameter 8468bccd85fSChristoph Lameter if (nmask != NULL && maxnode < MAX_NUMNODES) 8478bccd85fSChristoph Lameter return -EINVAL; 8488bccd85fSChristoph Lameter 8498bccd85fSChristoph Lameter err = do_get_mempolicy(&pval, &nodes, addr, flags); 8508bccd85fSChristoph Lameter 8518bccd85fSChristoph Lameter if (err) 8528bccd85fSChristoph Lameter return err; 8538bccd85fSChristoph Lameter 8548bccd85fSChristoph Lameter if (policy && put_user(pval, policy)) 8558bccd85fSChristoph Lameter return -EFAULT; 8568bccd85fSChristoph Lameter 8578bccd85fSChristoph Lameter if (nmask) 8588bccd85fSChristoph Lameter err = copy_nodes_to_user(nmask, maxnode, &nodes); 8598bccd85fSChristoph Lameter 8608bccd85fSChristoph Lameter return err; 8618bccd85fSChristoph Lameter } 8628bccd85fSChristoph Lameter 8631da177e4SLinus Torvalds #ifdef CONFIG_COMPAT 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy, 8661da177e4SLinus Torvalds compat_ulong_t __user *nmask, 8671da177e4SLinus Torvalds compat_ulong_t maxnode, 8681da177e4SLinus Torvalds compat_ulong_t addr, compat_ulong_t flags) 8691da177e4SLinus Torvalds { 8701da177e4SLinus Torvalds long err; 8711da177e4SLinus Torvalds unsigned long __user *nm = NULL; 8721da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 8731da177e4SLinus Torvalds DECLARE_BITMAP(bm, MAX_NUMNODES); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 8761da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds if (nmask) 8791da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags); 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds if (!err && nmask) { 8841da177e4SLinus Torvalds err = copy_from_user(bm, nm, alloc_size); 8851da177e4SLinus Torvalds /* ensure entire bitmap is zeroed */ 8861da177e4SLinus Torvalds err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8); 8871da177e4SLinus Torvalds err |= compat_put_bitmap(nmask, bm, nr_bits); 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds return err; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask, 8941da177e4SLinus Torvalds compat_ulong_t maxnode) 8951da177e4SLinus Torvalds { 8961da177e4SLinus Torvalds long err = 0; 8971da177e4SLinus Torvalds unsigned long __user *nm = NULL; 8981da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 8991da177e4SLinus Torvalds DECLARE_BITMAP(bm, MAX_NUMNODES); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 9021da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (nmask) { 9051da177e4SLinus Torvalds err = compat_get_bitmap(bm, nmask, nr_bits); 9061da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 9071da177e4SLinus Torvalds err |= copy_to_user(nm, bm, alloc_size); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds if (err) 9111da177e4SLinus Torvalds return -EFAULT; 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds return sys_set_mempolicy(mode, nm, nr_bits+1); 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len, 9171da177e4SLinus Torvalds compat_ulong_t mode, compat_ulong_t __user *nmask, 9181da177e4SLinus Torvalds compat_ulong_t maxnode, compat_ulong_t flags) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds long err = 0; 9211da177e4SLinus Torvalds unsigned long __user *nm = NULL; 9221da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 923dfcd3c0dSAndi Kleen nodemask_t bm; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 9261da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds if (nmask) { 929dfcd3c0dSAndi Kleen err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits); 9301da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 931dfcd3c0dSAndi Kleen err |= copy_to_user(nm, nodes_addr(bm), alloc_size); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds if (err) 9351da177e4SLinus Torvalds return -EFAULT; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds return sys_mbind(start, len, mode, nm, nr_bits+1, flags); 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds #endif 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* Return effective policy for a VMA */ 94348fce342SChristoph Lameter static struct mempolicy * get_vma_policy(struct task_struct *task, 94448fce342SChristoph Lameter struct vm_area_struct *vma, unsigned long addr) 9451da177e4SLinus Torvalds { 9466e21c8f1SChristoph Lameter struct mempolicy *pol = task->mempolicy; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds if (vma) { 9491da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->get_policy) 9501da177e4SLinus Torvalds pol = vma->vm_ops->get_policy(vma, addr); 9511da177e4SLinus Torvalds else if (vma->vm_policy && 9521da177e4SLinus Torvalds vma->vm_policy->policy != MPOL_DEFAULT) 9531da177e4SLinus Torvalds pol = vma->vm_policy; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds if (!pol) 9561da177e4SLinus Torvalds pol = &default_policy; 9571da177e4SLinus Torvalds return pol; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* Return a zonelist representing a mempolicy */ 961dd0fc66fSAl Viro static struct zonelist *zonelist_policy(gfp_t gfp, struct mempolicy *policy) 9621da177e4SLinus Torvalds { 9631da177e4SLinus Torvalds int nd; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds switch (policy->policy) { 9661da177e4SLinus Torvalds case MPOL_PREFERRED: 9671da177e4SLinus Torvalds nd = policy->v.preferred_node; 9681da177e4SLinus Torvalds if (nd < 0) 9691da177e4SLinus Torvalds nd = numa_node_id(); 9701da177e4SLinus Torvalds break; 9711da177e4SLinus Torvalds case MPOL_BIND: 9721da177e4SLinus Torvalds /* Lower zones don't get a policy applied */ 9731da177e4SLinus Torvalds /* Careful: current->mems_allowed might have moved */ 974af4ca457SAl Viro if (gfp_zone(gfp) >= policy_zone) 9751da177e4SLinus Torvalds if (cpuset_zonelist_valid_mems_allowed(policy->v.zonelist)) 9761da177e4SLinus Torvalds return policy->v.zonelist; 9771da177e4SLinus Torvalds /*FALL THROUGH*/ 9781da177e4SLinus Torvalds case MPOL_INTERLEAVE: /* should not happen */ 9791da177e4SLinus Torvalds case MPOL_DEFAULT: 9801da177e4SLinus Torvalds nd = numa_node_id(); 9811da177e4SLinus Torvalds break; 9821da177e4SLinus Torvalds default: 9831da177e4SLinus Torvalds nd = 0; 9841da177e4SLinus Torvalds BUG(); 9851da177e4SLinus Torvalds } 986af4ca457SAl Viro return NODE_DATA(nd)->node_zonelists + gfp_zone(gfp); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds /* Do dynamic interleaving for a process */ 9901da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy) 9911da177e4SLinus Torvalds { 9921da177e4SLinus Torvalds unsigned nid, next; 9931da177e4SLinus Torvalds struct task_struct *me = current; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds nid = me->il_next; 996dfcd3c0dSAndi Kleen next = next_node(nid, policy->v.nodes); 9971da177e4SLinus Torvalds if (next >= MAX_NUMNODES) 998dfcd3c0dSAndi Kleen next = first_node(policy->v.nodes); 9991da177e4SLinus Torvalds me->il_next = next; 10001da177e4SLinus Torvalds return nid; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */ 10041da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol, 10051da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long off) 10061da177e4SLinus Torvalds { 1007dfcd3c0dSAndi Kleen unsigned nnodes = nodes_weight(pol->v.nodes); 10081da177e4SLinus Torvalds unsigned target = (unsigned)off % nnodes; 10091da177e4SLinus Torvalds int c; 10101da177e4SLinus Torvalds int nid = -1; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds c = 0; 10131da177e4SLinus Torvalds do { 1014dfcd3c0dSAndi Kleen nid = next_node(nid, pol->v.nodes); 10151da177e4SLinus Torvalds c++; 10161da177e4SLinus Torvalds } while (c <= target); 10171da177e4SLinus Torvalds return nid; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10205da7ca86SChristoph Lameter /* Determine a node number for interleave */ 10215da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol, 10225da7ca86SChristoph Lameter struct vm_area_struct *vma, unsigned long addr, int shift) 10235da7ca86SChristoph Lameter { 10245da7ca86SChristoph Lameter if (vma) { 10255da7ca86SChristoph Lameter unsigned long off; 10265da7ca86SChristoph Lameter 10275da7ca86SChristoph Lameter off = vma->vm_pgoff; 10285da7ca86SChristoph Lameter off += (addr - vma->vm_start) >> shift; 10295da7ca86SChristoph Lameter return offset_il_node(pol, vma, off); 10305da7ca86SChristoph Lameter } else 10315da7ca86SChristoph Lameter return interleave_nodes(pol); 10325da7ca86SChristoph Lameter } 10335da7ca86SChristoph Lameter 10345da7ca86SChristoph Lameter /* Return a zonelist suitable for a huge page allocation. */ 10355da7ca86SChristoph Lameter struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr) 10365da7ca86SChristoph Lameter { 10375da7ca86SChristoph Lameter struct mempolicy *pol = get_vma_policy(current, vma, addr); 10385da7ca86SChristoph Lameter 10395da7ca86SChristoph Lameter if (pol->policy == MPOL_INTERLEAVE) { 10405da7ca86SChristoph Lameter unsigned nid; 10415da7ca86SChristoph Lameter 10425da7ca86SChristoph Lameter nid = interleave_nid(pol, vma, addr, HPAGE_SHIFT); 10435da7ca86SChristoph Lameter return NODE_DATA(nid)->node_zonelists + gfp_zone(GFP_HIGHUSER); 10445da7ca86SChristoph Lameter } 10455da7ca86SChristoph Lameter return zonelist_policy(GFP_HIGHUSER, pol); 10465da7ca86SChristoph Lameter } 10475da7ca86SChristoph Lameter 10481da177e4SLinus Torvalds /* Allocate a page in interleaved policy. 10491da177e4SLinus Torvalds Own path because it needs to do special accounting. */ 1050662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, 1051662f3a0bSAndi Kleen unsigned nid) 10521da177e4SLinus Torvalds { 10531da177e4SLinus Torvalds struct zonelist *zl; 10541da177e4SLinus Torvalds struct page *page; 10551da177e4SLinus Torvalds 1056af4ca457SAl Viro zl = NODE_DATA(nid)->node_zonelists + gfp_zone(gfp); 10571da177e4SLinus Torvalds page = __alloc_pages(gfp, order, zl); 10581da177e4SLinus Torvalds if (page && page_zone(page) == zl->zones[0]) { 1059e7c8d5c9SChristoph Lameter zone_pcp(zl->zones[0],get_cpu())->interleave_hit++; 10601da177e4SLinus Torvalds put_cpu(); 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds return page; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds /** 10661da177e4SLinus Torvalds * alloc_page_vma - Allocate a page for a VMA. 10671da177e4SLinus Torvalds * 10681da177e4SLinus Torvalds * @gfp: 10691da177e4SLinus Torvalds * %GFP_USER user allocation. 10701da177e4SLinus Torvalds * %GFP_KERNEL kernel allocations, 10711da177e4SLinus Torvalds * %GFP_HIGHMEM highmem/user allocations, 10721da177e4SLinus Torvalds * %GFP_FS allocation should not call back into a file system. 10731da177e4SLinus Torvalds * %GFP_ATOMIC don't sleep. 10741da177e4SLinus Torvalds * 10751da177e4SLinus Torvalds * @vma: Pointer to VMA or NULL if not available. 10761da177e4SLinus Torvalds * @addr: Virtual Address of the allocation. Must be inside the VMA. 10771da177e4SLinus Torvalds * 10781da177e4SLinus Torvalds * This function allocates a page from the kernel page pool and applies 10791da177e4SLinus Torvalds * a NUMA policy associated with the VMA or the current process. 10801da177e4SLinus Torvalds * When VMA is not NULL caller must hold down_read on the mmap_sem of the 10811da177e4SLinus Torvalds * mm_struct of the VMA to prevent it from going away. Should be used for 10821da177e4SLinus Torvalds * all allocations for pages that will be mapped into 10831da177e4SLinus Torvalds * user space. Returns NULL when no page can be allocated. 10841da177e4SLinus Torvalds * 10851da177e4SLinus Torvalds * Should be called with the mm_sem of the vma hold. 10861da177e4SLinus Torvalds */ 10871da177e4SLinus Torvalds struct page * 1088dd0fc66fSAl Viro alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr) 10891da177e4SLinus Torvalds { 10906e21c8f1SChristoph Lameter struct mempolicy *pol = get_vma_policy(current, vma, addr); 10911da177e4SLinus Torvalds 1092cf2a473cSPaul Jackson cpuset_update_task_memory_state(); 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds if (unlikely(pol->policy == MPOL_INTERLEAVE)) { 10951da177e4SLinus Torvalds unsigned nid; 10965da7ca86SChristoph Lameter 10975da7ca86SChristoph Lameter nid = interleave_nid(pol, vma, addr, PAGE_SHIFT); 10981da177e4SLinus Torvalds return alloc_page_interleave(gfp, 0, nid); 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds return __alloc_pages(gfp, 0, zonelist_policy(gfp, pol)); 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds /** 11041da177e4SLinus Torvalds * alloc_pages_current - Allocate pages. 11051da177e4SLinus Torvalds * 11061da177e4SLinus Torvalds * @gfp: 11071da177e4SLinus Torvalds * %GFP_USER user allocation, 11081da177e4SLinus Torvalds * %GFP_KERNEL kernel allocation, 11091da177e4SLinus Torvalds * %GFP_HIGHMEM highmem allocation, 11101da177e4SLinus Torvalds * %GFP_FS don't call back into a file system. 11111da177e4SLinus Torvalds * %GFP_ATOMIC don't sleep. 11121da177e4SLinus Torvalds * @order: Power of two of allocation size in pages. 0 is a single page. 11131da177e4SLinus Torvalds * 11141da177e4SLinus Torvalds * Allocate a page from the kernel page pool. When not in 11151da177e4SLinus Torvalds * interrupt context and apply the current process NUMA policy. 11161da177e4SLinus Torvalds * Returns NULL when no page can be allocated. 11171da177e4SLinus Torvalds * 1118cf2a473cSPaul Jackson * Don't call cpuset_update_task_memory_state() unless 11191da177e4SLinus Torvalds * 1) it's ok to take cpuset_sem (can WAIT), and 11201da177e4SLinus Torvalds * 2) allocating for current task (not interrupt). 11211da177e4SLinus Torvalds */ 1122dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds struct mempolicy *pol = current->mempolicy; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds if ((gfp & __GFP_WAIT) && !in_interrupt()) 1127cf2a473cSPaul Jackson cpuset_update_task_memory_state(); 11281da177e4SLinus Torvalds if (!pol || in_interrupt()) 11291da177e4SLinus Torvalds pol = &default_policy; 11301da177e4SLinus Torvalds if (pol->policy == MPOL_INTERLEAVE) 11311da177e4SLinus Torvalds return alloc_page_interleave(gfp, order, interleave_nodes(pol)); 11321da177e4SLinus Torvalds return __alloc_pages(gfp, order, zonelist_policy(gfp, pol)); 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current); 11351da177e4SLinus Torvalds 11364225399aSPaul Jackson /* 11374225399aSPaul Jackson * If mpol_copy() sees current->cpuset == cpuset_being_rebound, then it 11384225399aSPaul Jackson * rebinds the mempolicy its copying by calling mpol_rebind_policy() 11394225399aSPaul Jackson * with the mems_allowed returned by cpuset_mems_allowed(). This 11404225399aSPaul Jackson * keeps mempolicies cpuset relative after its cpuset moves. See 11414225399aSPaul Jackson * further kernel/cpuset.c update_nodemask(). 11424225399aSPaul Jackson */ 11434225399aSPaul Jackson void *cpuset_being_rebound; 11444225399aSPaul Jackson 11451da177e4SLinus Torvalds /* Slow path of a mempolicy copy */ 11461da177e4SLinus Torvalds struct mempolicy *__mpol_copy(struct mempolicy *old) 11471da177e4SLinus Torvalds { 11481da177e4SLinus Torvalds struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL); 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds if (!new) 11511da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11524225399aSPaul Jackson if (current_cpuset_is_being_rebound()) { 11534225399aSPaul Jackson nodemask_t mems = cpuset_mems_allowed(current); 11544225399aSPaul Jackson mpol_rebind_policy(old, &mems); 11554225399aSPaul Jackson } 11561da177e4SLinus Torvalds *new = *old; 11571da177e4SLinus Torvalds atomic_set(&new->refcnt, 1); 11581da177e4SLinus Torvalds if (new->policy == MPOL_BIND) { 11591da177e4SLinus Torvalds int sz = ksize(old->v.zonelist); 11601da177e4SLinus Torvalds new->v.zonelist = kmalloc(sz, SLAB_KERNEL); 11611da177e4SLinus Torvalds if (!new->v.zonelist) { 11621da177e4SLinus Torvalds kmem_cache_free(policy_cache, new); 11631da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds memcpy(new->v.zonelist, old->v.zonelist, sz); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds return new; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */ 11711da177e4SLinus Torvalds int __mpol_equal(struct mempolicy *a, struct mempolicy *b) 11721da177e4SLinus Torvalds { 11731da177e4SLinus Torvalds if (!a || !b) 11741da177e4SLinus Torvalds return 0; 11751da177e4SLinus Torvalds if (a->policy != b->policy) 11761da177e4SLinus Torvalds return 0; 11771da177e4SLinus Torvalds switch (a->policy) { 11781da177e4SLinus Torvalds case MPOL_DEFAULT: 11791da177e4SLinus Torvalds return 1; 11801da177e4SLinus Torvalds case MPOL_INTERLEAVE: 1181dfcd3c0dSAndi Kleen return nodes_equal(a->v.nodes, b->v.nodes); 11821da177e4SLinus Torvalds case MPOL_PREFERRED: 11831da177e4SLinus Torvalds return a->v.preferred_node == b->v.preferred_node; 11841da177e4SLinus Torvalds case MPOL_BIND: { 11851da177e4SLinus Torvalds int i; 11861da177e4SLinus Torvalds for (i = 0; a->v.zonelist->zones[i]; i++) 11871da177e4SLinus Torvalds if (a->v.zonelist->zones[i] != b->v.zonelist->zones[i]) 11881da177e4SLinus Torvalds return 0; 11891da177e4SLinus Torvalds return b->v.zonelist->zones[i] == NULL; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds default: 11921da177e4SLinus Torvalds BUG(); 11931da177e4SLinus Torvalds return 0; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* Slow path of a mpol destructor. */ 11981da177e4SLinus Torvalds void __mpol_free(struct mempolicy *p) 11991da177e4SLinus Torvalds { 12001da177e4SLinus Torvalds if (!atomic_dec_and_test(&p->refcnt)) 12011da177e4SLinus Torvalds return; 12021da177e4SLinus Torvalds if (p->policy == MPOL_BIND) 12031da177e4SLinus Torvalds kfree(p->v.zonelist); 12041da177e4SLinus Torvalds p->policy = MPOL_DEFAULT; 12051da177e4SLinus Torvalds kmem_cache_free(policy_cache, p); 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* 12091da177e4SLinus Torvalds * Shared memory backing store policy support. 12101da177e4SLinus Torvalds * 12111da177e4SLinus Torvalds * Remember policies even when nobody has shared memory mapped. 12121da177e4SLinus Torvalds * The policies are kept in Red-Black tree linked from the inode. 12131da177e4SLinus Torvalds * They are protected by the sp->lock spinlock, which should be held 12141da177e4SLinus Torvalds * for any accesses to the tree. 12151da177e4SLinus Torvalds */ 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds /* lookup first element intersecting start-end */ 12181da177e4SLinus Torvalds /* Caller holds sp->lock */ 12191da177e4SLinus Torvalds static struct sp_node * 12201da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end) 12211da177e4SLinus Torvalds { 12221da177e4SLinus Torvalds struct rb_node *n = sp->root.rb_node; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds while (n) { 12251da177e4SLinus Torvalds struct sp_node *p = rb_entry(n, struct sp_node, nd); 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds if (start >= p->end) 12281da177e4SLinus Torvalds n = n->rb_right; 12291da177e4SLinus Torvalds else if (end <= p->start) 12301da177e4SLinus Torvalds n = n->rb_left; 12311da177e4SLinus Torvalds else 12321da177e4SLinus Torvalds break; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds if (!n) 12351da177e4SLinus Torvalds return NULL; 12361da177e4SLinus Torvalds for (;;) { 12371da177e4SLinus Torvalds struct sp_node *w = NULL; 12381da177e4SLinus Torvalds struct rb_node *prev = rb_prev(n); 12391da177e4SLinus Torvalds if (!prev) 12401da177e4SLinus Torvalds break; 12411da177e4SLinus Torvalds w = rb_entry(prev, struct sp_node, nd); 12421da177e4SLinus Torvalds if (w->end <= start) 12431da177e4SLinus Torvalds break; 12441da177e4SLinus Torvalds n = prev; 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds return rb_entry(n, struct sp_node, nd); 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds /* Insert a new shared policy into the list. */ 12501da177e4SLinus Torvalds /* Caller holds sp->lock */ 12511da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new) 12521da177e4SLinus Torvalds { 12531da177e4SLinus Torvalds struct rb_node **p = &sp->root.rb_node; 12541da177e4SLinus Torvalds struct rb_node *parent = NULL; 12551da177e4SLinus Torvalds struct sp_node *nd; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds while (*p) { 12581da177e4SLinus Torvalds parent = *p; 12591da177e4SLinus Torvalds nd = rb_entry(parent, struct sp_node, nd); 12601da177e4SLinus Torvalds if (new->start < nd->start) 12611da177e4SLinus Torvalds p = &(*p)->rb_left; 12621da177e4SLinus Torvalds else if (new->end > nd->end) 12631da177e4SLinus Torvalds p = &(*p)->rb_right; 12641da177e4SLinus Torvalds else 12651da177e4SLinus Torvalds BUG(); 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds rb_link_node(&new->nd, parent, p); 12681da177e4SLinus Torvalds rb_insert_color(&new->nd, &sp->root); 12691da177e4SLinus Torvalds PDprintk("inserting %lx-%lx: %d\n", new->start, new->end, 12701da177e4SLinus Torvalds new->policy ? new->policy->policy : 0); 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds /* Find shared policy intersecting idx */ 12741da177e4SLinus Torvalds struct mempolicy * 12751da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) 12761da177e4SLinus Torvalds { 12771da177e4SLinus Torvalds struct mempolicy *pol = NULL; 12781da177e4SLinus Torvalds struct sp_node *sn; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds if (!sp->root.rb_node) 12811da177e4SLinus Torvalds return NULL; 12821da177e4SLinus Torvalds spin_lock(&sp->lock); 12831da177e4SLinus Torvalds sn = sp_lookup(sp, idx, idx+1); 12841da177e4SLinus Torvalds if (sn) { 12851da177e4SLinus Torvalds mpol_get(sn->policy); 12861da177e4SLinus Torvalds pol = sn->policy; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds spin_unlock(&sp->lock); 12891da177e4SLinus Torvalds return pol; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n) 12931da177e4SLinus Torvalds { 12941da177e4SLinus Torvalds PDprintk("deleting %lx-l%x\n", n->start, n->end); 12951da177e4SLinus Torvalds rb_erase(&n->nd, &sp->root); 12961da177e4SLinus Torvalds mpol_free(n->policy); 12971da177e4SLinus Torvalds kmem_cache_free(sn_cache, n); 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds struct sp_node * 13011da177e4SLinus Torvalds sp_alloc(unsigned long start, unsigned long end, struct mempolicy *pol) 13021da177e4SLinus Torvalds { 13031da177e4SLinus Torvalds struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds if (!n) 13061da177e4SLinus Torvalds return NULL; 13071da177e4SLinus Torvalds n->start = start; 13081da177e4SLinus Torvalds n->end = end; 13091da177e4SLinus Torvalds mpol_get(pol); 13101da177e4SLinus Torvalds n->policy = pol; 13111da177e4SLinus Torvalds return n; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* Replace a policy range. */ 13151da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start, 13161da177e4SLinus Torvalds unsigned long end, struct sp_node *new) 13171da177e4SLinus Torvalds { 13181da177e4SLinus Torvalds struct sp_node *n, *new2 = NULL; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds restart: 13211da177e4SLinus Torvalds spin_lock(&sp->lock); 13221da177e4SLinus Torvalds n = sp_lookup(sp, start, end); 13231da177e4SLinus Torvalds /* Take care of old policies in the same range. */ 13241da177e4SLinus Torvalds while (n && n->start < end) { 13251da177e4SLinus Torvalds struct rb_node *next = rb_next(&n->nd); 13261da177e4SLinus Torvalds if (n->start >= start) { 13271da177e4SLinus Torvalds if (n->end <= end) 13281da177e4SLinus Torvalds sp_delete(sp, n); 13291da177e4SLinus Torvalds else 13301da177e4SLinus Torvalds n->start = end; 13311da177e4SLinus Torvalds } else { 13321da177e4SLinus Torvalds /* Old policy spanning whole new range. */ 13331da177e4SLinus Torvalds if (n->end > end) { 13341da177e4SLinus Torvalds if (!new2) { 13351da177e4SLinus Torvalds spin_unlock(&sp->lock); 13361da177e4SLinus Torvalds new2 = sp_alloc(end, n->end, n->policy); 13371da177e4SLinus Torvalds if (!new2) 13381da177e4SLinus Torvalds return -ENOMEM; 13391da177e4SLinus Torvalds goto restart; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds n->end = start; 13421da177e4SLinus Torvalds sp_insert(sp, new2); 13431da177e4SLinus Torvalds new2 = NULL; 13441da177e4SLinus Torvalds break; 13451da177e4SLinus Torvalds } else 13461da177e4SLinus Torvalds n->end = start; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds if (!next) 13491da177e4SLinus Torvalds break; 13501da177e4SLinus Torvalds n = rb_entry(next, struct sp_node, nd); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds if (new) 13531da177e4SLinus Torvalds sp_insert(sp, new); 13541da177e4SLinus Torvalds spin_unlock(&sp->lock); 13551da177e4SLinus Torvalds if (new2) { 13561da177e4SLinus Torvalds mpol_free(new2->policy); 13571da177e4SLinus Torvalds kmem_cache_free(sn_cache, new2); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds return 0; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 1362*7339ff83SRobin Holt void mpol_shared_policy_init(struct shared_policy *info, int policy, 1363*7339ff83SRobin Holt nodemask_t *policy_nodes) 1364*7339ff83SRobin Holt { 1365*7339ff83SRobin Holt info->root = RB_ROOT; 1366*7339ff83SRobin Holt spin_lock_init(&info->lock); 1367*7339ff83SRobin Holt 1368*7339ff83SRobin Holt if (policy != MPOL_DEFAULT) { 1369*7339ff83SRobin Holt struct mempolicy *newpol; 1370*7339ff83SRobin Holt 1371*7339ff83SRobin Holt /* Falls back to MPOL_DEFAULT on any error */ 1372*7339ff83SRobin Holt newpol = mpol_new(policy, policy_nodes); 1373*7339ff83SRobin Holt if (!IS_ERR(newpol)) { 1374*7339ff83SRobin Holt /* Create pseudo-vma that contains just the policy */ 1375*7339ff83SRobin Holt struct vm_area_struct pvma; 1376*7339ff83SRobin Holt 1377*7339ff83SRobin Holt memset(&pvma, 0, sizeof(struct vm_area_struct)); 1378*7339ff83SRobin Holt /* Policy covers entire file */ 1379*7339ff83SRobin Holt pvma.vm_end = TASK_SIZE; 1380*7339ff83SRobin Holt mpol_set_shared_policy(info, &pvma, newpol); 1381*7339ff83SRobin Holt mpol_free(newpol); 1382*7339ff83SRobin Holt } 1383*7339ff83SRobin Holt } 1384*7339ff83SRobin Holt } 1385*7339ff83SRobin Holt 13861da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info, 13871da177e4SLinus Torvalds struct vm_area_struct *vma, struct mempolicy *npol) 13881da177e4SLinus Torvalds { 13891da177e4SLinus Torvalds int err; 13901da177e4SLinus Torvalds struct sp_node *new = NULL; 13911da177e4SLinus Torvalds unsigned long sz = vma_pages(vma); 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds PDprintk("set_shared_policy %lx sz %lu %d %lx\n", 13941da177e4SLinus Torvalds vma->vm_pgoff, 13951da177e4SLinus Torvalds sz, npol? npol->policy : -1, 1396dfcd3c0dSAndi Kleen npol ? nodes_addr(npol->v.nodes)[0] : -1); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds if (npol) { 13991da177e4SLinus Torvalds new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); 14001da177e4SLinus Torvalds if (!new) 14011da177e4SLinus Torvalds return -ENOMEM; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); 14041da177e4SLinus Torvalds if (err && new) 14051da177e4SLinus Torvalds kmem_cache_free(sn_cache, new); 14061da177e4SLinus Torvalds return err; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */ 14101da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds struct sp_node *n; 14131da177e4SLinus Torvalds struct rb_node *next; 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds if (!p->root.rb_node) 14161da177e4SLinus Torvalds return; 14171da177e4SLinus Torvalds spin_lock(&p->lock); 14181da177e4SLinus Torvalds next = rb_first(&p->root); 14191da177e4SLinus Torvalds while (next) { 14201da177e4SLinus Torvalds n = rb_entry(next, struct sp_node, nd); 14211da177e4SLinus Torvalds next = rb_next(&n->nd); 142290c5029eSAndi Kleen rb_erase(&n->nd, &p->root); 14231da177e4SLinus Torvalds mpol_free(n->policy); 14241da177e4SLinus Torvalds kmem_cache_free(sn_cache, n); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds spin_unlock(&p->lock); 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */ 14301da177e4SLinus Torvalds void __init numa_policy_init(void) 14311da177e4SLinus Torvalds { 14321da177e4SLinus Torvalds policy_cache = kmem_cache_create("numa_policy", 14331da177e4SLinus Torvalds sizeof(struct mempolicy), 14341da177e4SLinus Torvalds 0, SLAB_PANIC, NULL, NULL); 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds sn_cache = kmem_cache_create("shared_policy_node", 14371da177e4SLinus Torvalds sizeof(struct sp_node), 14381da177e4SLinus Torvalds 0, SLAB_PANIC, NULL, NULL); 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds /* Set interleaving policy for system init. This way not all 14411da177e4SLinus Torvalds the data structures allocated at system boot end up in node zero. */ 14421da177e4SLinus Torvalds 14438bccd85fSChristoph Lameter if (do_set_mempolicy(MPOL_INTERLEAVE, &node_online_map)) 14441da177e4SLinus Torvalds printk("numa_policy_init: interleaving failed\n"); 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 14478bccd85fSChristoph Lameter /* Reset policy of current process to default */ 14481da177e4SLinus Torvalds void numa_default_policy(void) 14491da177e4SLinus Torvalds { 14508bccd85fSChristoph Lameter do_set_mempolicy(MPOL_DEFAULT, NULL); 14511da177e4SLinus Torvalds } 145268860ec1SPaul Jackson 145368860ec1SPaul Jackson /* Migrate a policy to a different set of nodes */ 145474cb2155SPaul Jackson void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask) 145568860ec1SPaul Jackson { 145674cb2155SPaul Jackson nodemask_t *mpolmask; 145768860ec1SPaul Jackson nodemask_t tmp; 145868860ec1SPaul Jackson 145968860ec1SPaul Jackson if (!pol) 146068860ec1SPaul Jackson return; 146174cb2155SPaul Jackson mpolmask = &pol->cpuset_mems_allowed; 146274cb2155SPaul Jackson if (nodes_equal(*mpolmask, *newmask)) 146374cb2155SPaul Jackson return; 146468860ec1SPaul Jackson 146568860ec1SPaul Jackson switch (pol->policy) { 146668860ec1SPaul Jackson case MPOL_DEFAULT: 146768860ec1SPaul Jackson break; 146868860ec1SPaul Jackson case MPOL_INTERLEAVE: 146974cb2155SPaul Jackson nodes_remap(tmp, pol->v.nodes, *mpolmask, *newmask); 147068860ec1SPaul Jackson pol->v.nodes = tmp; 147174cb2155SPaul Jackson *mpolmask = *newmask; 147274cb2155SPaul Jackson current->il_next = node_remap(current->il_next, 147374cb2155SPaul Jackson *mpolmask, *newmask); 147468860ec1SPaul Jackson break; 147568860ec1SPaul Jackson case MPOL_PREFERRED: 147668860ec1SPaul Jackson pol->v.preferred_node = node_remap(pol->v.preferred_node, 147774cb2155SPaul Jackson *mpolmask, *newmask); 147874cb2155SPaul Jackson *mpolmask = *newmask; 147968860ec1SPaul Jackson break; 148068860ec1SPaul Jackson case MPOL_BIND: { 148168860ec1SPaul Jackson nodemask_t nodes; 148268860ec1SPaul Jackson struct zone **z; 148368860ec1SPaul Jackson struct zonelist *zonelist; 148468860ec1SPaul Jackson 148568860ec1SPaul Jackson nodes_clear(nodes); 148668860ec1SPaul Jackson for (z = pol->v.zonelist->zones; *z; z++) 148768860ec1SPaul Jackson node_set((*z)->zone_pgdat->node_id, nodes); 148874cb2155SPaul Jackson nodes_remap(tmp, nodes, *mpolmask, *newmask); 148968860ec1SPaul Jackson nodes = tmp; 149068860ec1SPaul Jackson 149168860ec1SPaul Jackson zonelist = bind_zonelist(&nodes); 149268860ec1SPaul Jackson 149368860ec1SPaul Jackson /* If no mem, then zonelist is NULL and we keep old zonelist. 149468860ec1SPaul Jackson * If that old zonelist has no remaining mems_allowed nodes, 149568860ec1SPaul Jackson * then zonelist_policy() will "FALL THROUGH" to MPOL_DEFAULT. 149668860ec1SPaul Jackson */ 149768860ec1SPaul Jackson 149868860ec1SPaul Jackson if (zonelist) { 149968860ec1SPaul Jackson /* Good - got mem - substitute new zonelist */ 150068860ec1SPaul Jackson kfree(pol->v.zonelist); 150168860ec1SPaul Jackson pol->v.zonelist = zonelist; 150268860ec1SPaul Jackson } 150374cb2155SPaul Jackson *mpolmask = *newmask; 150468860ec1SPaul Jackson break; 150568860ec1SPaul Jackson } 150668860ec1SPaul Jackson default: 150768860ec1SPaul Jackson BUG(); 150868860ec1SPaul Jackson break; 150968860ec1SPaul Jackson } 151068860ec1SPaul Jackson } 151168860ec1SPaul Jackson 151268860ec1SPaul Jackson /* 151374cb2155SPaul Jackson * Wrapper for mpol_rebind_policy() that just requires task 151474cb2155SPaul Jackson * pointer, and updates task mempolicy. 151568860ec1SPaul Jackson */ 151674cb2155SPaul Jackson 151774cb2155SPaul Jackson void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new) 151868860ec1SPaul Jackson { 151974cb2155SPaul Jackson mpol_rebind_policy(tsk->mempolicy, new); 152068860ec1SPaul Jackson } 15211a75a6c8SChristoph Lameter 15221a75a6c8SChristoph Lameter /* 15234225399aSPaul Jackson * Rebind each vma in mm to new nodemask. 15244225399aSPaul Jackson * 15254225399aSPaul Jackson * Call holding a reference to mm. Takes mm->mmap_sem during call. 15264225399aSPaul Jackson */ 15274225399aSPaul Jackson 15284225399aSPaul Jackson void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new) 15294225399aSPaul Jackson { 15304225399aSPaul Jackson struct vm_area_struct *vma; 15314225399aSPaul Jackson 15324225399aSPaul Jackson down_write(&mm->mmap_sem); 15334225399aSPaul Jackson for (vma = mm->mmap; vma; vma = vma->vm_next) 15344225399aSPaul Jackson mpol_rebind_policy(vma->vm_policy, new); 15354225399aSPaul Jackson up_write(&mm->mmap_sem); 15364225399aSPaul Jackson } 15374225399aSPaul Jackson 15384225399aSPaul Jackson /* 15391a75a6c8SChristoph Lameter * Display pages allocated per node and memory policy via /proc. 15401a75a6c8SChristoph Lameter */ 15411a75a6c8SChristoph Lameter 15421a75a6c8SChristoph Lameter static const char *policy_types[] = { "default", "prefer", "bind", 15431a75a6c8SChristoph Lameter "interleave" }; 15441a75a6c8SChristoph Lameter 15451a75a6c8SChristoph Lameter /* 15461a75a6c8SChristoph Lameter * Convert a mempolicy into a string. 15471a75a6c8SChristoph Lameter * Returns the number of characters in buffer (if positive) 15481a75a6c8SChristoph Lameter * or an error (negative) 15491a75a6c8SChristoph Lameter */ 15501a75a6c8SChristoph Lameter static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) 15511a75a6c8SChristoph Lameter { 15521a75a6c8SChristoph Lameter char *p = buffer; 15531a75a6c8SChristoph Lameter int l; 15541a75a6c8SChristoph Lameter nodemask_t nodes; 15551a75a6c8SChristoph Lameter int mode = pol ? pol->policy : MPOL_DEFAULT; 15561a75a6c8SChristoph Lameter 15571a75a6c8SChristoph Lameter switch (mode) { 15581a75a6c8SChristoph Lameter case MPOL_DEFAULT: 15591a75a6c8SChristoph Lameter nodes_clear(nodes); 15601a75a6c8SChristoph Lameter break; 15611a75a6c8SChristoph Lameter 15621a75a6c8SChristoph Lameter case MPOL_PREFERRED: 15631a75a6c8SChristoph Lameter nodes_clear(nodes); 15641a75a6c8SChristoph Lameter node_set(pol->v.preferred_node, nodes); 15651a75a6c8SChristoph Lameter break; 15661a75a6c8SChristoph Lameter 15671a75a6c8SChristoph Lameter case MPOL_BIND: 15681a75a6c8SChristoph Lameter get_zonemask(pol, &nodes); 15691a75a6c8SChristoph Lameter break; 15701a75a6c8SChristoph Lameter 15711a75a6c8SChristoph Lameter case MPOL_INTERLEAVE: 15721a75a6c8SChristoph Lameter nodes = pol->v.nodes; 15731a75a6c8SChristoph Lameter break; 15741a75a6c8SChristoph Lameter 15751a75a6c8SChristoph Lameter default: 15761a75a6c8SChristoph Lameter BUG(); 15771a75a6c8SChristoph Lameter return -EFAULT; 15781a75a6c8SChristoph Lameter } 15791a75a6c8SChristoph Lameter 15801a75a6c8SChristoph Lameter l = strlen(policy_types[mode]); 15811a75a6c8SChristoph Lameter if (buffer + maxlen < p + l + 1) 15821a75a6c8SChristoph Lameter return -ENOSPC; 15831a75a6c8SChristoph Lameter 15841a75a6c8SChristoph Lameter strcpy(p, policy_types[mode]); 15851a75a6c8SChristoph Lameter p += l; 15861a75a6c8SChristoph Lameter 15871a75a6c8SChristoph Lameter if (!nodes_empty(nodes)) { 15881a75a6c8SChristoph Lameter if (buffer + maxlen < p + 2) 15891a75a6c8SChristoph Lameter return -ENOSPC; 15901a75a6c8SChristoph Lameter *p++ = '='; 15911a75a6c8SChristoph Lameter p += nodelist_scnprintf(p, buffer + maxlen - p, nodes); 15921a75a6c8SChristoph Lameter } 15931a75a6c8SChristoph Lameter return p - buffer; 15941a75a6c8SChristoph Lameter } 15951a75a6c8SChristoph Lameter 15961a75a6c8SChristoph Lameter struct numa_maps { 15971a75a6c8SChristoph Lameter unsigned long pages; 15981a75a6c8SChristoph Lameter unsigned long anon; 15991a75a6c8SChristoph Lameter unsigned long mapped; 16001a75a6c8SChristoph Lameter unsigned long mapcount_max; 16011a75a6c8SChristoph Lameter unsigned long node[MAX_NUMNODES]; 16021a75a6c8SChristoph Lameter }; 16031a75a6c8SChristoph Lameter 16041a75a6c8SChristoph Lameter static void gather_stats(struct page *page, void *private) 16051a75a6c8SChristoph Lameter { 16061a75a6c8SChristoph Lameter struct numa_maps *md = private; 16071a75a6c8SChristoph Lameter int count = page_mapcount(page); 16081a75a6c8SChristoph Lameter 16091a75a6c8SChristoph Lameter if (count) 16101a75a6c8SChristoph Lameter md->mapped++; 16111a75a6c8SChristoph Lameter 16121a75a6c8SChristoph Lameter if (count > md->mapcount_max) 16131a75a6c8SChristoph Lameter md->mapcount_max = count; 16141a75a6c8SChristoph Lameter 16151a75a6c8SChristoph Lameter md->pages++; 16161a75a6c8SChristoph Lameter 16171a75a6c8SChristoph Lameter if (PageAnon(page)) 16181a75a6c8SChristoph Lameter md->anon++; 16191a75a6c8SChristoph Lameter 16201a75a6c8SChristoph Lameter md->node[page_to_nid(page)]++; 16211a75a6c8SChristoph Lameter cond_resched(); 16221a75a6c8SChristoph Lameter } 16231a75a6c8SChristoph Lameter 16241a75a6c8SChristoph Lameter int show_numa_map(struct seq_file *m, void *v) 16251a75a6c8SChristoph Lameter { 16261a75a6c8SChristoph Lameter struct task_struct *task = m->private; 16271a75a6c8SChristoph Lameter struct vm_area_struct *vma = v; 16281a75a6c8SChristoph Lameter struct numa_maps *md; 16291a75a6c8SChristoph Lameter int n; 16301a75a6c8SChristoph Lameter char buffer[50]; 16311a75a6c8SChristoph Lameter 16321a75a6c8SChristoph Lameter if (!vma->vm_mm) 16331a75a6c8SChristoph Lameter return 0; 16341a75a6c8SChristoph Lameter 16351a75a6c8SChristoph Lameter md = kzalloc(sizeof(struct numa_maps), GFP_KERNEL); 16361a75a6c8SChristoph Lameter if (!md) 16371a75a6c8SChristoph Lameter return 0; 16381a75a6c8SChristoph Lameter 16391a75a6c8SChristoph Lameter check_pgd_range(vma, vma->vm_start, vma->vm_end, 16401a75a6c8SChristoph Lameter &node_online_map, MPOL_MF_STATS, md); 16411a75a6c8SChristoph Lameter 16421a75a6c8SChristoph Lameter if (md->pages) { 16431a75a6c8SChristoph Lameter mpol_to_str(buffer, sizeof(buffer), 16441a75a6c8SChristoph Lameter get_vma_policy(task, vma, vma->vm_start)); 16451a75a6c8SChristoph Lameter 16461a75a6c8SChristoph Lameter seq_printf(m, "%08lx %s pages=%lu mapped=%lu maxref=%lu", 16471a75a6c8SChristoph Lameter vma->vm_start, buffer, md->pages, 16481a75a6c8SChristoph Lameter md->mapped, md->mapcount_max); 16491a75a6c8SChristoph Lameter 16501a75a6c8SChristoph Lameter if (md->anon) 16511a75a6c8SChristoph Lameter seq_printf(m," anon=%lu",md->anon); 16521a75a6c8SChristoph Lameter 16531a75a6c8SChristoph Lameter for_each_online_node(n) 16541a75a6c8SChristoph Lameter if (md->node[n]) 16551a75a6c8SChristoph Lameter seq_printf(m, " N%d=%lu", n, md->node[n]); 16561a75a6c8SChristoph Lameter 16571a75a6c8SChristoph Lameter seq_putc(m, '\n'); 16581a75a6c8SChristoph Lameter } 16591a75a6c8SChristoph Lameter kfree(md); 16601a75a6c8SChristoph Lameter 16611a75a6c8SChristoph Lameter if (m->count < m->size) 16621a75a6c8SChristoph Lameter m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0; 16631a75a6c8SChristoph Lameter return 0; 16641a75a6c8SChristoph Lameter } 16651a75a6c8SChristoph Lameter 1666