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; 1831da177e4SLinus Torvalds return policy; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186dc9aa5b9SChristoph Lameter /* Check if we are the only process mapping the page in question */ 187dc9aa5b9SChristoph Lameter static inline int single_mm_mapping(struct mm_struct *mm, 188dc9aa5b9SChristoph Lameter struct address_space *mapping) 189dc9aa5b9SChristoph Lameter { 190dc9aa5b9SChristoph Lameter struct vm_area_struct *vma; 191dc9aa5b9SChristoph Lameter struct prio_tree_iter iter; 192dc9aa5b9SChristoph Lameter int rc = 1; 193dc9aa5b9SChristoph Lameter 194dc9aa5b9SChristoph Lameter spin_lock(&mapping->i_mmap_lock); 195dc9aa5b9SChristoph Lameter vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX) 196dc9aa5b9SChristoph Lameter if (mm != vma->vm_mm) { 197dc9aa5b9SChristoph Lameter rc = 0; 198dc9aa5b9SChristoph Lameter goto out; 199dc9aa5b9SChristoph Lameter } 200dc9aa5b9SChristoph Lameter list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) 201dc9aa5b9SChristoph Lameter if (mm != vma->vm_mm) { 202dc9aa5b9SChristoph Lameter rc = 0; 203dc9aa5b9SChristoph Lameter goto out; 204dc9aa5b9SChristoph Lameter } 205dc9aa5b9SChristoph Lameter out: 206dc9aa5b9SChristoph Lameter spin_unlock(&mapping->i_mmap_lock); 207dc9aa5b9SChristoph Lameter return rc; 208dc9aa5b9SChristoph Lameter } 209dc9aa5b9SChristoph Lameter 210dc9aa5b9SChristoph Lameter /* 211dc9aa5b9SChristoph Lameter * Add a page to be migrated to the pagelist 212dc9aa5b9SChristoph Lameter */ 213dc9aa5b9SChristoph Lameter static void migrate_page_add(struct vm_area_struct *vma, 214dc9aa5b9SChristoph Lameter struct page *page, struct list_head *pagelist, unsigned long flags) 215dc9aa5b9SChristoph Lameter { 216dc9aa5b9SChristoph Lameter /* 217dc9aa5b9SChristoph Lameter * Avoid migrating a page that is shared by others and not writable. 218dc9aa5b9SChristoph Lameter */ 219dc9aa5b9SChristoph Lameter if ((flags & MPOL_MF_MOVE_ALL) || !page->mapping || PageAnon(page) || 220dc9aa5b9SChristoph Lameter mapping_writably_mapped(page->mapping) || 221dc9aa5b9SChristoph Lameter single_mm_mapping(vma->vm_mm, page->mapping)) { 222dc9aa5b9SChristoph Lameter int rc = isolate_lru_page(page); 223dc9aa5b9SChristoph Lameter 224dc9aa5b9SChristoph Lameter if (rc == 1) 225dc9aa5b9SChristoph Lameter list_add(&page->lru, pagelist); 226dc9aa5b9SChristoph Lameter /* 227dc9aa5b9SChristoph Lameter * If the isolate attempt was not successful then we just 228dc9aa5b9SChristoph Lameter * encountered an unswappable page. Something must be wrong. 229dc9aa5b9SChristoph Lameter */ 230dc9aa5b9SChristoph Lameter WARN_ON(rc == 0); 231dc9aa5b9SChristoph Lameter } 232dc9aa5b9SChristoph Lameter } 233dc9aa5b9SChristoph Lameter 2341a75a6c8SChristoph Lameter static void gather_stats(struct page *, void *); 2351a75a6c8SChristoph Lameter 23638e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */ 237b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 238dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 239dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 24038e35860SChristoph Lameter void *private) 2411da177e4SLinus Torvalds { 24291612e0dSHugh Dickins pte_t *orig_pte; 24391612e0dSHugh Dickins pte_t *pte; 244705e87c0SHugh Dickins spinlock_t *ptl; 245941150a3SHugh Dickins 246705e87c0SHugh Dickins orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 24791612e0dSHugh Dickins do { 2486aab341eSLinus Torvalds struct page *page; 24991612e0dSHugh Dickins unsigned int nid; 25091612e0dSHugh Dickins 25191612e0dSHugh Dickins if (!pte_present(*pte)) 25291612e0dSHugh Dickins continue; 2536aab341eSLinus Torvalds page = vm_normal_page(vma, addr, *pte); 2546aab341eSLinus Torvalds if (!page) 25591612e0dSHugh Dickins continue; 2566aab341eSLinus Torvalds nid = page_to_nid(page); 25738e35860SChristoph Lameter if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT)) 25838e35860SChristoph Lameter continue; 25938e35860SChristoph Lameter 2601a75a6c8SChristoph Lameter if (flags & MPOL_MF_STATS) 2611a75a6c8SChristoph Lameter gather_stats(page, private); 262132beacfSChristoph Lameter else if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { 263132beacfSChristoph Lameter spin_unlock(ptl); 26438e35860SChristoph Lameter migrate_page_add(vma, page, private, flags); 265132beacfSChristoph Lameter spin_lock(ptl); 266132beacfSChristoph Lameter } 267dc9aa5b9SChristoph Lameter else 2681da177e4SLinus Torvalds break; 26991612e0dSHugh Dickins } while (pte++, addr += PAGE_SIZE, addr != end); 270705e87c0SHugh Dickins pte_unmap_unlock(orig_pte, ptl); 27191612e0dSHugh Dickins return addr != end; 27291612e0dSHugh Dickins } 27391612e0dSHugh Dickins 274b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud, 275dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 276dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 27738e35860SChristoph Lameter void *private) 27891612e0dSHugh Dickins { 27991612e0dSHugh Dickins pmd_t *pmd; 28091612e0dSHugh Dickins unsigned long next; 28191612e0dSHugh Dickins 28291612e0dSHugh Dickins pmd = pmd_offset(pud, addr); 28391612e0dSHugh Dickins do { 28491612e0dSHugh Dickins next = pmd_addr_end(addr, end); 28591612e0dSHugh Dickins if (pmd_none_or_clear_bad(pmd)) 28691612e0dSHugh Dickins continue; 287dc9aa5b9SChristoph Lameter if (check_pte_range(vma, pmd, addr, next, nodes, 28838e35860SChristoph Lameter flags, private)) 28991612e0dSHugh Dickins return -EIO; 29091612e0dSHugh Dickins } while (pmd++, addr = next, addr != end); 29191612e0dSHugh Dickins return 0; 29291612e0dSHugh Dickins } 29391612e0dSHugh Dickins 294b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 295dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 296dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 29738e35860SChristoph Lameter void *private) 29891612e0dSHugh Dickins { 29991612e0dSHugh Dickins pud_t *pud; 30091612e0dSHugh Dickins unsigned long next; 30191612e0dSHugh Dickins 30291612e0dSHugh Dickins pud = pud_offset(pgd, addr); 30391612e0dSHugh Dickins do { 30491612e0dSHugh Dickins next = pud_addr_end(addr, end); 30591612e0dSHugh Dickins if (pud_none_or_clear_bad(pud)) 30691612e0dSHugh Dickins continue; 307dc9aa5b9SChristoph Lameter if (check_pmd_range(vma, pud, addr, next, nodes, 30838e35860SChristoph Lameter flags, private)) 30991612e0dSHugh Dickins return -EIO; 31091612e0dSHugh Dickins } while (pud++, addr = next, addr != end); 31191612e0dSHugh Dickins return 0; 31291612e0dSHugh Dickins } 31391612e0dSHugh Dickins 314b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma, 315dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 316dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 31738e35860SChristoph Lameter void *private) 31891612e0dSHugh Dickins { 31991612e0dSHugh Dickins pgd_t *pgd; 32091612e0dSHugh Dickins unsigned long next; 32191612e0dSHugh Dickins 322b5810039SNick Piggin pgd = pgd_offset(vma->vm_mm, addr); 32391612e0dSHugh Dickins do { 32491612e0dSHugh Dickins next = pgd_addr_end(addr, end); 32591612e0dSHugh Dickins if (pgd_none_or_clear_bad(pgd)) 32691612e0dSHugh Dickins continue; 327dc9aa5b9SChristoph Lameter if (check_pud_range(vma, pgd, addr, next, nodes, 32838e35860SChristoph Lameter flags, private)) 32991612e0dSHugh Dickins return -EIO; 33091612e0dSHugh Dickins } while (pgd++, addr = next, addr != end); 33191612e0dSHugh Dickins return 0; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 334dc9aa5b9SChristoph Lameter /* Check if a vma is migratable */ 335dc9aa5b9SChristoph Lameter static inline int vma_migratable(struct vm_area_struct *vma) 336dc9aa5b9SChristoph Lameter { 337dc9aa5b9SChristoph Lameter if (vma->vm_flags & ( 338dc9aa5b9SChristoph Lameter VM_LOCKED|VM_IO|VM_HUGETLB|VM_PFNMAP)) 339dc9aa5b9SChristoph Lameter return 0; 340dc9aa5b9SChristoph Lameter return 1; 341dc9aa5b9SChristoph Lameter } 342dc9aa5b9SChristoph Lameter 343dc9aa5b9SChristoph Lameter /* 344dc9aa5b9SChristoph Lameter * Check if all pages in a range are on a set of nodes. 345dc9aa5b9SChristoph Lameter * If pagelist != NULL then isolate pages from the LRU and 346dc9aa5b9SChristoph Lameter * put them on the pagelist. 347dc9aa5b9SChristoph Lameter */ 3481da177e4SLinus Torvalds static struct vm_area_struct * 3491da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end, 35038e35860SChristoph Lameter const nodemask_t *nodes, unsigned long flags, void *private) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds int err; 3531da177e4SLinus Torvalds struct vm_area_struct *first, *vma, *prev; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds first = find_vma(mm, start); 3561da177e4SLinus Torvalds if (!first) 3571da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 3581da177e4SLinus Torvalds prev = NULL; 3591da177e4SLinus Torvalds for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) { 360dc9aa5b9SChristoph Lameter if (!(flags & MPOL_MF_DISCONTIG_OK)) { 3611da177e4SLinus Torvalds if (!vma->vm_next && vma->vm_end < end) 3621da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 3631da177e4SLinus Torvalds if (prev && prev->vm_end < vma->vm_start) 3641da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 365dc9aa5b9SChristoph Lameter } 366dc9aa5b9SChristoph Lameter if (!is_vm_hugetlb_page(vma) && 367dc9aa5b9SChristoph Lameter ((flags & MPOL_MF_STRICT) || 368dc9aa5b9SChristoph Lameter ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) && 369dc9aa5b9SChristoph Lameter vma_migratable(vma)))) { 3705b952b3cSAndi Kleen unsigned long endvma = vma->vm_end; 371dc9aa5b9SChristoph Lameter 3725b952b3cSAndi Kleen if (endvma > end) 3735b952b3cSAndi Kleen endvma = end; 3745b952b3cSAndi Kleen if (vma->vm_start > start) 3755b952b3cSAndi Kleen start = vma->vm_start; 376dc9aa5b9SChristoph Lameter err = check_pgd_range(vma, start, endvma, nodes, 37738e35860SChristoph Lameter flags, private); 3781da177e4SLinus Torvalds if (err) { 3791da177e4SLinus Torvalds first = ERR_PTR(err); 3801da177e4SLinus Torvalds break; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds prev = vma; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds return first; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* Apply policy to a single VMA */ 3891da177e4SLinus Torvalds static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds int err = 0; 3921da177e4SLinus Torvalds struct mempolicy *old = vma->vm_policy; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds PDprintk("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n", 3951da177e4SLinus Torvalds vma->vm_start, vma->vm_end, vma->vm_pgoff, 3961da177e4SLinus Torvalds vma->vm_ops, vma->vm_file, 3971da177e4SLinus Torvalds vma->vm_ops ? vma->vm_ops->set_policy : NULL); 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->set_policy) 4001da177e4SLinus Torvalds err = vma->vm_ops->set_policy(vma, new); 4011da177e4SLinus Torvalds if (!err) { 4021da177e4SLinus Torvalds mpol_get(new); 4031da177e4SLinus Torvalds vma->vm_policy = new; 4041da177e4SLinus Torvalds mpol_free(old); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds return err; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */ 4101da177e4SLinus Torvalds static int mbind_range(struct vm_area_struct *vma, unsigned long start, 4111da177e4SLinus Torvalds unsigned long end, struct mempolicy *new) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct vm_area_struct *next; 4141da177e4SLinus Torvalds int err; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds err = 0; 4171da177e4SLinus Torvalds for (; vma && vma->vm_start < end; vma = next) { 4181da177e4SLinus Torvalds next = vma->vm_next; 4191da177e4SLinus Torvalds if (vma->vm_start < start) 4201da177e4SLinus Torvalds err = split_vma(vma->vm_mm, vma, start, 1); 4211da177e4SLinus Torvalds if (!err && vma->vm_end > end) 4221da177e4SLinus Torvalds err = split_vma(vma->vm_mm, vma, end, 0); 4231da177e4SLinus Torvalds if (!err) 4241da177e4SLinus Torvalds err = policy_vma(vma, new); 4251da177e4SLinus Torvalds if (err) 4261da177e4SLinus Torvalds break; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds return err; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4318bccd85fSChristoph Lameter static int contextualize_policy(int mode, nodemask_t *nodes) 4328bccd85fSChristoph Lameter { 4338bccd85fSChristoph Lameter if (!nodes) 4348bccd85fSChristoph Lameter return 0; 4358bccd85fSChristoph Lameter 4368bccd85fSChristoph Lameter /* Update current mems_allowed */ 4378bccd85fSChristoph Lameter cpuset_update_current_mems_allowed(); 4388bccd85fSChristoph Lameter /* Ignore nodes not set in current->mems_allowed */ 4398bccd85fSChristoph Lameter cpuset_restrict_to_mems_allowed(nodes->bits); 4408bccd85fSChristoph Lameter return mpol_check_policy(mode, nodes); 4418bccd85fSChristoph Lameter } 4428bccd85fSChristoph Lameter 443d4984711SChristoph Lameter static int swap_pages(struct list_head *pagelist) 444d4984711SChristoph Lameter { 445d4984711SChristoph Lameter LIST_HEAD(moved); 446d4984711SChristoph Lameter LIST_HEAD(failed); 447d4984711SChristoph Lameter int n; 448d4984711SChristoph Lameter 449d4984711SChristoph Lameter n = migrate_pages(pagelist, NULL, &moved, &failed); 450d4984711SChristoph Lameter putback_lru_pages(&failed); 451d4984711SChristoph Lameter putback_lru_pages(&moved); 452d4984711SChristoph Lameter 453d4984711SChristoph Lameter return n; 454d4984711SChristoph Lameter } 455d4984711SChristoph Lameter 4568bccd85fSChristoph Lameter long do_mbind(unsigned long start, unsigned long len, 4578bccd85fSChristoph Lameter unsigned long mode, nodemask_t *nmask, unsigned long flags) 4581da177e4SLinus Torvalds { 4591da177e4SLinus Torvalds struct vm_area_struct *vma; 4601da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 4611da177e4SLinus Torvalds struct mempolicy *new; 4621da177e4SLinus Torvalds unsigned long end; 4631da177e4SLinus Torvalds int err; 464dc9aa5b9SChristoph Lameter LIST_HEAD(pagelist); 4651da177e4SLinus Torvalds 46638e35860SChristoph Lameter if ((flags & ~(unsigned long)(MPOL_MF_STRICT | 46738e35860SChristoph Lameter MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) 468dc9aa5b9SChristoph Lameter || mode > MPOL_MAX) 4691da177e4SLinus Torvalds return -EINVAL; 470dc9aa5b9SChristoph Lameter if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_RESOURCE)) 471dc9aa5b9SChristoph Lameter return -EPERM; 472dc9aa5b9SChristoph Lameter 4731da177e4SLinus Torvalds if (start & ~PAGE_MASK) 4741da177e4SLinus Torvalds return -EINVAL; 475dc9aa5b9SChristoph Lameter 4761da177e4SLinus Torvalds if (mode == MPOL_DEFAULT) 4771da177e4SLinus Torvalds flags &= ~MPOL_MF_STRICT; 478dc9aa5b9SChristoph Lameter 4791da177e4SLinus Torvalds len = (len + PAGE_SIZE - 1) & PAGE_MASK; 4801da177e4SLinus Torvalds end = start + len; 481dc9aa5b9SChristoph Lameter 4821da177e4SLinus Torvalds if (end < start) 4831da177e4SLinus Torvalds return -EINVAL; 4841da177e4SLinus Torvalds if (end == start) 4851da177e4SLinus Torvalds return 0; 486dc9aa5b9SChristoph Lameter 4875fcbb230SChristoph Lameter if (mpol_check_policy(mode, nmask)) 4888bccd85fSChristoph Lameter return -EINVAL; 489dc9aa5b9SChristoph Lameter 4908bccd85fSChristoph Lameter new = mpol_new(mode, nmask); 4911da177e4SLinus Torvalds if (IS_ERR(new)) 4921da177e4SLinus Torvalds return PTR_ERR(new); 4931da177e4SLinus Torvalds 494dc9aa5b9SChristoph Lameter /* 495dc9aa5b9SChristoph Lameter * If we are using the default policy then operation 496dc9aa5b9SChristoph Lameter * on discontinuous address spaces is okay after all 497dc9aa5b9SChristoph Lameter */ 498dc9aa5b9SChristoph Lameter if (!new) 499dc9aa5b9SChristoph Lameter flags |= MPOL_MF_DISCONTIG_OK; 500dc9aa5b9SChristoph Lameter 5011da177e4SLinus Torvalds PDprintk("mbind %lx-%lx mode:%ld nodes:%lx\n",start,start+len, 502dfcd3c0dSAndi Kleen mode,nodes_addr(nodes)[0]); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds down_write(&mm->mmap_sem); 50538e35860SChristoph Lameter vma = check_range(mm, start, end, nmask, 50638e35860SChristoph Lameter flags | MPOL_MF_INVERT, &pagelist); 50738e35860SChristoph Lameter 5081da177e4SLinus Torvalds err = PTR_ERR(vma); 509dc9aa5b9SChristoph Lameter if (!IS_ERR(vma)) { 510d4984711SChristoph Lameter int nr_failed = 0; 511d4984711SChristoph Lameter 5121da177e4SLinus Torvalds err = mbind_range(vma, start, end, new); 513dc9aa5b9SChristoph Lameter if (!list_empty(&pagelist)) 514d4984711SChristoph Lameter nr_failed = swap_pages(&pagelist); 515d4984711SChristoph Lameter 516d4984711SChristoph Lameter if (!err && nr_failed && (flags & MPOL_MF_STRICT)) 517dc9aa5b9SChristoph Lameter err = -EIO; 518dc9aa5b9SChristoph Lameter } 519dc9aa5b9SChristoph Lameter if (!list_empty(&pagelist)) 520dc9aa5b9SChristoph Lameter putback_lru_pages(&pagelist); 521dc9aa5b9SChristoph Lameter 5221da177e4SLinus Torvalds up_write(&mm->mmap_sem); 5231da177e4SLinus Torvalds mpol_free(new); 5241da177e4SLinus Torvalds return err; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /* Set the process memory policy */ 5288bccd85fSChristoph Lameter long do_set_mempolicy(int mode, nodemask_t *nodes) 5291da177e4SLinus Torvalds { 5301da177e4SLinus Torvalds struct mempolicy *new; 5311da177e4SLinus Torvalds 5328bccd85fSChristoph Lameter if (contextualize_policy(mode, nodes)) 5331da177e4SLinus Torvalds return -EINVAL; 5348bccd85fSChristoph Lameter new = mpol_new(mode, nodes); 5351da177e4SLinus Torvalds if (IS_ERR(new)) 5361da177e4SLinus Torvalds return PTR_ERR(new); 5371da177e4SLinus Torvalds mpol_free(current->mempolicy); 5381da177e4SLinus Torvalds current->mempolicy = new; 5391da177e4SLinus Torvalds if (new && new->policy == MPOL_INTERLEAVE) 540dfcd3c0dSAndi Kleen current->il_next = first_node(new->v.nodes); 5411da177e4SLinus Torvalds return 0; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* Fill a zone bitmap for a policy */ 545dfcd3c0dSAndi Kleen static void get_zonemask(struct mempolicy *p, nodemask_t *nodes) 5461da177e4SLinus Torvalds { 5471da177e4SLinus Torvalds int i; 5481da177e4SLinus Torvalds 549dfcd3c0dSAndi Kleen nodes_clear(*nodes); 5501da177e4SLinus Torvalds switch (p->policy) { 5511da177e4SLinus Torvalds case MPOL_BIND: 5521da177e4SLinus Torvalds for (i = 0; p->v.zonelist->zones[i]; i++) 5538bccd85fSChristoph Lameter node_set(p->v.zonelist->zones[i]->zone_pgdat->node_id, 5548bccd85fSChristoph Lameter *nodes); 5551da177e4SLinus Torvalds break; 5561da177e4SLinus Torvalds case MPOL_DEFAULT: 5571da177e4SLinus Torvalds break; 5581da177e4SLinus Torvalds case MPOL_INTERLEAVE: 559dfcd3c0dSAndi Kleen *nodes = p->v.nodes; 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds case MPOL_PREFERRED: 5621da177e4SLinus Torvalds /* or use current node instead of online map? */ 5631da177e4SLinus Torvalds if (p->v.preferred_node < 0) 564dfcd3c0dSAndi Kleen *nodes = node_online_map; 5651da177e4SLinus Torvalds else 566dfcd3c0dSAndi Kleen node_set(p->v.preferred_node, *nodes); 5671da177e4SLinus Torvalds break; 5681da177e4SLinus Torvalds default: 5691da177e4SLinus Torvalds BUG(); 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds struct page *p; 5761da177e4SLinus Torvalds int err; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL); 5791da177e4SLinus Torvalds if (err >= 0) { 5801da177e4SLinus Torvalds err = page_to_nid(p); 5811da177e4SLinus Torvalds put_page(p); 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds return err; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* Retrieve NUMA policy */ 5878bccd85fSChristoph Lameter long do_get_mempolicy(int *policy, nodemask_t *nmask, 5881da177e4SLinus Torvalds unsigned long addr, unsigned long flags) 5891da177e4SLinus Torvalds { 5908bccd85fSChristoph Lameter int err; 5911da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 5921da177e4SLinus Torvalds struct vm_area_struct *vma = NULL; 5931da177e4SLinus Torvalds struct mempolicy *pol = current->mempolicy; 5941da177e4SLinus Torvalds 59568860ec1SPaul Jackson cpuset_update_current_mems_allowed(); 5961da177e4SLinus Torvalds if (flags & ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR)) 5971da177e4SLinus Torvalds return -EINVAL; 5981da177e4SLinus Torvalds if (flags & MPOL_F_ADDR) { 5991da177e4SLinus Torvalds down_read(&mm->mmap_sem); 6001da177e4SLinus Torvalds vma = find_vma_intersection(mm, addr, addr+1); 6011da177e4SLinus Torvalds if (!vma) { 6021da177e4SLinus Torvalds up_read(&mm->mmap_sem); 6031da177e4SLinus Torvalds return -EFAULT; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->get_policy) 6061da177e4SLinus Torvalds pol = vma->vm_ops->get_policy(vma, addr); 6071da177e4SLinus Torvalds else 6081da177e4SLinus Torvalds pol = vma->vm_policy; 6091da177e4SLinus Torvalds } else if (addr) 6101da177e4SLinus Torvalds return -EINVAL; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds if (!pol) 6131da177e4SLinus Torvalds pol = &default_policy; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds if (flags & MPOL_F_NODE) { 6161da177e4SLinus Torvalds if (flags & MPOL_F_ADDR) { 6171da177e4SLinus Torvalds err = lookup_node(mm, addr); 6181da177e4SLinus Torvalds if (err < 0) 6191da177e4SLinus Torvalds goto out; 6208bccd85fSChristoph Lameter *policy = err; 6211da177e4SLinus Torvalds } else if (pol == current->mempolicy && 6221da177e4SLinus Torvalds pol->policy == MPOL_INTERLEAVE) { 6238bccd85fSChristoph Lameter *policy = current->il_next; 6241da177e4SLinus Torvalds } else { 6251da177e4SLinus Torvalds err = -EINVAL; 6261da177e4SLinus Torvalds goto out; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds } else 6298bccd85fSChristoph Lameter *policy = pol->policy; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds if (vma) { 6321da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 6331da177e4SLinus Torvalds vma = NULL; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds err = 0; 6378bccd85fSChristoph Lameter if (nmask) 6388bccd85fSChristoph Lameter get_zonemask(pol, nmask); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds out: 6411da177e4SLinus Torvalds if (vma) 6421da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 6431da177e4SLinus Torvalds return err; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6468bccd85fSChristoph Lameter /* 64739743889SChristoph Lameter * For now migrate_pages simply swaps out the pages from nodes that are in 64839743889SChristoph Lameter * the source set but not in the target set. In the future, we would 64939743889SChristoph Lameter * want a function that moves pages between the two nodesets in such 65039743889SChristoph Lameter * a way as to preserve the physical layout as much as possible. 65139743889SChristoph Lameter * 65239743889SChristoph Lameter * Returns the number of page that could not be moved. 65339743889SChristoph Lameter */ 65439743889SChristoph Lameter int do_migrate_pages(struct mm_struct *mm, 65539743889SChristoph Lameter const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags) 65639743889SChristoph Lameter { 65739743889SChristoph Lameter LIST_HEAD(pagelist); 65839743889SChristoph Lameter int count = 0; 65939743889SChristoph Lameter nodemask_t nodes; 66039743889SChristoph Lameter 66139743889SChristoph Lameter nodes_andnot(nodes, *from_nodes, *to_nodes); 66239743889SChristoph Lameter 66339743889SChristoph Lameter down_read(&mm->mmap_sem); 66439743889SChristoph Lameter check_range(mm, mm->mmap->vm_start, TASK_SIZE, &nodes, 66539743889SChristoph Lameter flags | MPOL_MF_DISCONTIG_OK, &pagelist); 666d4984711SChristoph Lameter 66739743889SChristoph Lameter if (!list_empty(&pagelist)) { 668d4984711SChristoph Lameter count = swap_pages(&pagelist); 669d4984711SChristoph Lameter putback_lru_pages(&pagelist); 67039743889SChristoph Lameter } 671d4984711SChristoph Lameter 67239743889SChristoph Lameter up_read(&mm->mmap_sem); 67339743889SChristoph Lameter return count; 67439743889SChristoph Lameter } 67539743889SChristoph Lameter 67639743889SChristoph Lameter /* 6778bccd85fSChristoph Lameter * User space interface with variable sized bitmaps for nodelists. 6788bccd85fSChristoph Lameter */ 6798bccd85fSChristoph Lameter 6808bccd85fSChristoph Lameter /* Copy a node mask from user space. */ 68139743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, 6828bccd85fSChristoph Lameter unsigned long maxnode) 6838bccd85fSChristoph Lameter { 6848bccd85fSChristoph Lameter unsigned long k; 6858bccd85fSChristoph Lameter unsigned long nlongs; 6868bccd85fSChristoph Lameter unsigned long endmask; 6878bccd85fSChristoph Lameter 6888bccd85fSChristoph Lameter --maxnode; 6898bccd85fSChristoph Lameter nodes_clear(*nodes); 6908bccd85fSChristoph Lameter if (maxnode == 0 || !nmask) 6918bccd85fSChristoph Lameter return 0; 6928bccd85fSChristoph Lameter 6938bccd85fSChristoph Lameter nlongs = BITS_TO_LONGS(maxnode); 6948bccd85fSChristoph Lameter if ((maxnode % BITS_PER_LONG) == 0) 6958bccd85fSChristoph Lameter endmask = ~0UL; 6968bccd85fSChristoph Lameter else 6978bccd85fSChristoph Lameter endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1; 6988bccd85fSChristoph Lameter 6998bccd85fSChristoph Lameter /* When the user specified more nodes than supported just check 7008bccd85fSChristoph Lameter if the non supported part is all zero. */ 7018bccd85fSChristoph Lameter if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) { 7028bccd85fSChristoph Lameter if (nlongs > PAGE_SIZE/sizeof(long)) 7038bccd85fSChristoph Lameter return -EINVAL; 7048bccd85fSChristoph Lameter for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) { 7058bccd85fSChristoph Lameter unsigned long t; 7068bccd85fSChristoph Lameter if (get_user(t, nmask + k)) 7078bccd85fSChristoph Lameter return -EFAULT; 7088bccd85fSChristoph Lameter if (k == nlongs - 1) { 7098bccd85fSChristoph Lameter if (t & endmask) 7108bccd85fSChristoph Lameter return -EINVAL; 7118bccd85fSChristoph Lameter } else if (t) 7128bccd85fSChristoph Lameter return -EINVAL; 7138bccd85fSChristoph Lameter } 7148bccd85fSChristoph Lameter nlongs = BITS_TO_LONGS(MAX_NUMNODES); 7158bccd85fSChristoph Lameter endmask = ~0UL; 7168bccd85fSChristoph Lameter } 7178bccd85fSChristoph Lameter 7188bccd85fSChristoph Lameter if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long))) 7198bccd85fSChristoph Lameter return -EFAULT; 7208bccd85fSChristoph Lameter nodes_addr(*nodes)[nlongs-1] &= endmask; 7218bccd85fSChristoph Lameter return 0; 7228bccd85fSChristoph Lameter } 7238bccd85fSChristoph Lameter 7248bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */ 7258bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, 7268bccd85fSChristoph Lameter nodemask_t *nodes) 7278bccd85fSChristoph Lameter { 7288bccd85fSChristoph Lameter unsigned long copy = ALIGN(maxnode-1, 64) / 8; 7298bccd85fSChristoph Lameter const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long); 7308bccd85fSChristoph Lameter 7318bccd85fSChristoph Lameter if (copy > nbytes) { 7328bccd85fSChristoph Lameter if (copy > PAGE_SIZE) 7338bccd85fSChristoph Lameter return -EINVAL; 7348bccd85fSChristoph Lameter if (clear_user((char __user *)mask + nbytes, copy - nbytes)) 7358bccd85fSChristoph Lameter return -EFAULT; 7368bccd85fSChristoph Lameter copy = nbytes; 7378bccd85fSChristoph Lameter } 7388bccd85fSChristoph Lameter return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; 7398bccd85fSChristoph Lameter } 7408bccd85fSChristoph Lameter 7418bccd85fSChristoph Lameter asmlinkage long sys_mbind(unsigned long start, unsigned long len, 7428bccd85fSChristoph Lameter unsigned long mode, 7438bccd85fSChristoph Lameter unsigned long __user *nmask, unsigned long maxnode, 7448bccd85fSChristoph Lameter unsigned flags) 7458bccd85fSChristoph Lameter { 7468bccd85fSChristoph Lameter nodemask_t nodes; 7478bccd85fSChristoph Lameter int err; 7488bccd85fSChristoph Lameter 7498bccd85fSChristoph Lameter err = get_nodes(&nodes, nmask, maxnode); 7508bccd85fSChristoph Lameter if (err) 7518bccd85fSChristoph Lameter return err; 7528bccd85fSChristoph Lameter return do_mbind(start, len, mode, &nodes, flags); 7538bccd85fSChristoph Lameter } 7548bccd85fSChristoph Lameter 7558bccd85fSChristoph Lameter /* Set the process memory policy */ 7568bccd85fSChristoph Lameter asmlinkage long sys_set_mempolicy(int mode, unsigned long __user *nmask, 7578bccd85fSChristoph Lameter unsigned long maxnode) 7588bccd85fSChristoph Lameter { 7598bccd85fSChristoph Lameter int err; 7608bccd85fSChristoph Lameter nodemask_t nodes; 7618bccd85fSChristoph Lameter 7628bccd85fSChristoph Lameter if (mode < 0 || mode > MPOL_MAX) 7638bccd85fSChristoph Lameter return -EINVAL; 7648bccd85fSChristoph Lameter err = get_nodes(&nodes, nmask, maxnode); 7658bccd85fSChristoph Lameter if (err) 7668bccd85fSChristoph Lameter return err; 7678bccd85fSChristoph Lameter return do_set_mempolicy(mode, &nodes); 7688bccd85fSChristoph Lameter } 7698bccd85fSChristoph Lameter 77039743889SChristoph Lameter /* Macro needed until Paul implements this function in kernel/cpusets.c */ 77139743889SChristoph Lameter #define cpuset_mems_allowed(task) node_online_map 77239743889SChristoph Lameter 77339743889SChristoph Lameter asmlinkage long sys_migrate_pages(pid_t pid, unsigned long maxnode, 77439743889SChristoph Lameter const unsigned long __user *old_nodes, 77539743889SChristoph Lameter const unsigned long __user *new_nodes) 77639743889SChristoph Lameter { 77739743889SChristoph Lameter struct mm_struct *mm; 77839743889SChristoph Lameter struct task_struct *task; 77939743889SChristoph Lameter nodemask_t old; 78039743889SChristoph Lameter nodemask_t new; 78139743889SChristoph Lameter nodemask_t task_nodes; 78239743889SChristoph Lameter int err; 78339743889SChristoph Lameter 78439743889SChristoph Lameter err = get_nodes(&old, old_nodes, maxnode); 78539743889SChristoph Lameter if (err) 78639743889SChristoph Lameter return err; 78739743889SChristoph Lameter 78839743889SChristoph Lameter err = get_nodes(&new, new_nodes, maxnode); 78939743889SChristoph Lameter if (err) 79039743889SChristoph Lameter return err; 79139743889SChristoph Lameter 79239743889SChristoph Lameter /* Find the mm_struct */ 79339743889SChristoph Lameter read_lock(&tasklist_lock); 79439743889SChristoph Lameter task = pid ? find_task_by_pid(pid) : current; 79539743889SChristoph Lameter if (!task) { 79639743889SChristoph Lameter read_unlock(&tasklist_lock); 79739743889SChristoph Lameter return -ESRCH; 79839743889SChristoph Lameter } 79939743889SChristoph Lameter mm = get_task_mm(task); 80039743889SChristoph Lameter read_unlock(&tasklist_lock); 80139743889SChristoph Lameter 80239743889SChristoph Lameter if (!mm) 80339743889SChristoph Lameter return -EINVAL; 80439743889SChristoph Lameter 80539743889SChristoph Lameter /* 80639743889SChristoph Lameter * Check if this process has the right to modify the specified 80739743889SChristoph Lameter * process. The right exists if the process has administrative 80839743889SChristoph Lameter * capabilities, superuser priviledges or the same 80939743889SChristoph Lameter * userid as the target process. 81039743889SChristoph Lameter */ 81139743889SChristoph Lameter if ((current->euid != task->suid) && (current->euid != task->uid) && 81239743889SChristoph Lameter (current->uid != task->suid) && (current->uid != task->uid) && 81339743889SChristoph Lameter !capable(CAP_SYS_ADMIN)) { 81439743889SChristoph Lameter err = -EPERM; 81539743889SChristoph Lameter goto out; 81639743889SChristoph Lameter } 81739743889SChristoph Lameter 81839743889SChristoph Lameter task_nodes = cpuset_mems_allowed(task); 81939743889SChristoph Lameter /* Is the user allowed to access the target nodes? */ 82039743889SChristoph Lameter if (!nodes_subset(new, task_nodes) && !capable(CAP_SYS_ADMIN)) { 82139743889SChristoph Lameter err = -EPERM; 82239743889SChristoph Lameter goto out; 82339743889SChristoph Lameter } 82439743889SChristoph Lameter 82539743889SChristoph Lameter err = do_migrate_pages(mm, &old, &new, MPOL_MF_MOVE); 82639743889SChristoph Lameter out: 82739743889SChristoph Lameter mmput(mm); 82839743889SChristoph Lameter return err; 82939743889SChristoph Lameter } 83039743889SChristoph Lameter 83139743889SChristoph Lameter 8328bccd85fSChristoph Lameter /* Retrieve NUMA policy */ 8338bccd85fSChristoph Lameter asmlinkage long sys_get_mempolicy(int __user *policy, 8348bccd85fSChristoph Lameter unsigned long __user *nmask, 8358bccd85fSChristoph Lameter unsigned long maxnode, 8368bccd85fSChristoph Lameter unsigned long addr, unsigned long flags) 8378bccd85fSChristoph Lameter { 8388bccd85fSChristoph Lameter int err, pval; 8398bccd85fSChristoph Lameter nodemask_t nodes; 8408bccd85fSChristoph Lameter 8418bccd85fSChristoph Lameter if (nmask != NULL && maxnode < MAX_NUMNODES) 8428bccd85fSChristoph Lameter return -EINVAL; 8438bccd85fSChristoph Lameter 8448bccd85fSChristoph Lameter err = do_get_mempolicy(&pval, &nodes, addr, flags); 8458bccd85fSChristoph Lameter 8468bccd85fSChristoph Lameter if (err) 8478bccd85fSChristoph Lameter return err; 8488bccd85fSChristoph Lameter 8498bccd85fSChristoph Lameter if (policy && put_user(pval, policy)) 8508bccd85fSChristoph Lameter return -EFAULT; 8518bccd85fSChristoph Lameter 8528bccd85fSChristoph Lameter if (nmask) 8538bccd85fSChristoph Lameter err = copy_nodes_to_user(nmask, maxnode, &nodes); 8548bccd85fSChristoph Lameter 8558bccd85fSChristoph Lameter return err; 8568bccd85fSChristoph Lameter } 8578bccd85fSChristoph Lameter 8581da177e4SLinus Torvalds #ifdef CONFIG_COMPAT 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy, 8611da177e4SLinus Torvalds compat_ulong_t __user *nmask, 8621da177e4SLinus Torvalds compat_ulong_t maxnode, 8631da177e4SLinus Torvalds compat_ulong_t addr, compat_ulong_t flags) 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds long err; 8661da177e4SLinus Torvalds unsigned long __user *nm = NULL; 8671da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 8681da177e4SLinus Torvalds DECLARE_BITMAP(bm, MAX_NUMNODES); 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 8711da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (nmask) 8741da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags); 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds if (!err && nmask) { 8791da177e4SLinus Torvalds err = copy_from_user(bm, nm, alloc_size); 8801da177e4SLinus Torvalds /* ensure entire bitmap is zeroed */ 8811da177e4SLinus Torvalds err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8); 8821da177e4SLinus Torvalds err |= compat_put_bitmap(nmask, bm, nr_bits); 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds return err; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask, 8891da177e4SLinus Torvalds compat_ulong_t maxnode) 8901da177e4SLinus Torvalds { 8911da177e4SLinus Torvalds long err = 0; 8921da177e4SLinus Torvalds unsigned long __user *nm = NULL; 8931da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 8941da177e4SLinus Torvalds DECLARE_BITMAP(bm, MAX_NUMNODES); 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 8971da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds if (nmask) { 9001da177e4SLinus Torvalds err = compat_get_bitmap(bm, nmask, nr_bits); 9011da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 9021da177e4SLinus Torvalds err |= copy_to_user(nm, bm, alloc_size); 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (err) 9061da177e4SLinus Torvalds return -EFAULT; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds return sys_set_mempolicy(mode, nm, nr_bits+1); 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len, 9121da177e4SLinus Torvalds compat_ulong_t mode, compat_ulong_t __user *nmask, 9131da177e4SLinus Torvalds compat_ulong_t maxnode, compat_ulong_t flags) 9141da177e4SLinus Torvalds { 9151da177e4SLinus Torvalds long err = 0; 9161da177e4SLinus Torvalds unsigned long __user *nm = NULL; 9171da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 918dfcd3c0dSAndi Kleen nodemask_t bm; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 9211da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds if (nmask) { 924dfcd3c0dSAndi Kleen err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits); 9251da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 926dfcd3c0dSAndi Kleen err |= copy_to_user(nm, nodes_addr(bm), alloc_size); 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds if (err) 9301da177e4SLinus Torvalds return -EFAULT; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds return sys_mbind(start, len, mode, nm, nr_bits+1, flags); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds #endif 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* Return effective policy for a VMA */ 938*48fce342SChristoph Lameter static struct mempolicy * get_vma_policy(struct task_struct *task, 939*48fce342SChristoph Lameter struct vm_area_struct *vma, unsigned long addr) 9401da177e4SLinus Torvalds { 9416e21c8f1SChristoph Lameter struct mempolicy *pol = task->mempolicy; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds if (vma) { 9441da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->get_policy) 9451da177e4SLinus Torvalds pol = vma->vm_ops->get_policy(vma, addr); 9461da177e4SLinus Torvalds else if (vma->vm_policy && 9471da177e4SLinus Torvalds vma->vm_policy->policy != MPOL_DEFAULT) 9481da177e4SLinus Torvalds pol = vma->vm_policy; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds if (!pol) 9511da177e4SLinus Torvalds pol = &default_policy; 9521da177e4SLinus Torvalds return pol; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* Return a zonelist representing a mempolicy */ 956dd0fc66fSAl Viro static struct zonelist *zonelist_policy(gfp_t gfp, struct mempolicy *policy) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds int nd; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds switch (policy->policy) { 9611da177e4SLinus Torvalds case MPOL_PREFERRED: 9621da177e4SLinus Torvalds nd = policy->v.preferred_node; 9631da177e4SLinus Torvalds if (nd < 0) 9641da177e4SLinus Torvalds nd = numa_node_id(); 9651da177e4SLinus Torvalds break; 9661da177e4SLinus Torvalds case MPOL_BIND: 9671da177e4SLinus Torvalds /* Lower zones don't get a policy applied */ 9681da177e4SLinus Torvalds /* Careful: current->mems_allowed might have moved */ 969af4ca457SAl Viro if (gfp_zone(gfp) >= policy_zone) 9701da177e4SLinus Torvalds if (cpuset_zonelist_valid_mems_allowed(policy->v.zonelist)) 9711da177e4SLinus Torvalds return policy->v.zonelist; 9721da177e4SLinus Torvalds /*FALL THROUGH*/ 9731da177e4SLinus Torvalds case MPOL_INTERLEAVE: /* should not happen */ 9741da177e4SLinus Torvalds case MPOL_DEFAULT: 9751da177e4SLinus Torvalds nd = numa_node_id(); 9761da177e4SLinus Torvalds break; 9771da177e4SLinus Torvalds default: 9781da177e4SLinus Torvalds nd = 0; 9791da177e4SLinus Torvalds BUG(); 9801da177e4SLinus Torvalds } 981af4ca457SAl Viro return NODE_DATA(nd)->node_zonelists + gfp_zone(gfp); 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds /* Do dynamic interleaving for a process */ 9851da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds unsigned nid, next; 9881da177e4SLinus Torvalds struct task_struct *me = current; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds nid = me->il_next; 991dfcd3c0dSAndi Kleen next = next_node(nid, policy->v.nodes); 9921da177e4SLinus Torvalds if (next >= MAX_NUMNODES) 993dfcd3c0dSAndi Kleen next = first_node(policy->v.nodes); 9941da177e4SLinus Torvalds me->il_next = next; 9951da177e4SLinus Torvalds return nid; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */ 9991da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol, 10001da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long off) 10011da177e4SLinus Torvalds { 1002dfcd3c0dSAndi Kleen unsigned nnodes = nodes_weight(pol->v.nodes); 10031da177e4SLinus Torvalds unsigned target = (unsigned)off % nnodes; 10041da177e4SLinus Torvalds int c; 10051da177e4SLinus Torvalds int nid = -1; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds c = 0; 10081da177e4SLinus Torvalds do { 1009dfcd3c0dSAndi Kleen nid = next_node(nid, pol->v.nodes); 10101da177e4SLinus Torvalds c++; 10111da177e4SLinus Torvalds } while (c <= target); 10121da177e4SLinus Torvalds return nid; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 10155da7ca86SChristoph Lameter /* Determine a node number for interleave */ 10165da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol, 10175da7ca86SChristoph Lameter struct vm_area_struct *vma, unsigned long addr, int shift) 10185da7ca86SChristoph Lameter { 10195da7ca86SChristoph Lameter if (vma) { 10205da7ca86SChristoph Lameter unsigned long off; 10215da7ca86SChristoph Lameter 10225da7ca86SChristoph Lameter off = vma->vm_pgoff; 10235da7ca86SChristoph Lameter off += (addr - vma->vm_start) >> shift; 10245da7ca86SChristoph Lameter return offset_il_node(pol, vma, off); 10255da7ca86SChristoph Lameter } else 10265da7ca86SChristoph Lameter return interleave_nodes(pol); 10275da7ca86SChristoph Lameter } 10285da7ca86SChristoph Lameter 10295da7ca86SChristoph Lameter /* Return a zonelist suitable for a huge page allocation. */ 10305da7ca86SChristoph Lameter struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr) 10315da7ca86SChristoph Lameter { 10325da7ca86SChristoph Lameter struct mempolicy *pol = get_vma_policy(current, vma, addr); 10335da7ca86SChristoph Lameter 10345da7ca86SChristoph Lameter if (pol->policy == MPOL_INTERLEAVE) { 10355da7ca86SChristoph Lameter unsigned nid; 10365da7ca86SChristoph Lameter 10375da7ca86SChristoph Lameter nid = interleave_nid(pol, vma, addr, HPAGE_SHIFT); 10385da7ca86SChristoph Lameter return NODE_DATA(nid)->node_zonelists + gfp_zone(GFP_HIGHUSER); 10395da7ca86SChristoph Lameter } 10405da7ca86SChristoph Lameter return zonelist_policy(GFP_HIGHUSER, pol); 10415da7ca86SChristoph Lameter } 10425da7ca86SChristoph Lameter 10431da177e4SLinus Torvalds /* Allocate a page in interleaved policy. 10441da177e4SLinus Torvalds Own path because it needs to do special accounting. */ 1045662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, 1046662f3a0bSAndi Kleen unsigned nid) 10471da177e4SLinus Torvalds { 10481da177e4SLinus Torvalds struct zonelist *zl; 10491da177e4SLinus Torvalds struct page *page; 10501da177e4SLinus Torvalds 1051af4ca457SAl Viro zl = NODE_DATA(nid)->node_zonelists + gfp_zone(gfp); 10521da177e4SLinus Torvalds page = __alloc_pages(gfp, order, zl); 10531da177e4SLinus Torvalds if (page && page_zone(page) == zl->zones[0]) { 1054e7c8d5c9SChristoph Lameter zone_pcp(zl->zones[0],get_cpu())->interleave_hit++; 10551da177e4SLinus Torvalds put_cpu(); 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds return page; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /** 10611da177e4SLinus Torvalds * alloc_page_vma - Allocate a page for a VMA. 10621da177e4SLinus Torvalds * 10631da177e4SLinus Torvalds * @gfp: 10641da177e4SLinus Torvalds * %GFP_USER user allocation. 10651da177e4SLinus Torvalds * %GFP_KERNEL kernel allocations, 10661da177e4SLinus Torvalds * %GFP_HIGHMEM highmem/user allocations, 10671da177e4SLinus Torvalds * %GFP_FS allocation should not call back into a file system. 10681da177e4SLinus Torvalds * %GFP_ATOMIC don't sleep. 10691da177e4SLinus Torvalds * 10701da177e4SLinus Torvalds * @vma: Pointer to VMA or NULL if not available. 10711da177e4SLinus Torvalds * @addr: Virtual Address of the allocation. Must be inside the VMA. 10721da177e4SLinus Torvalds * 10731da177e4SLinus Torvalds * This function allocates a page from the kernel page pool and applies 10741da177e4SLinus Torvalds * a NUMA policy associated with the VMA or the current process. 10751da177e4SLinus Torvalds * When VMA is not NULL caller must hold down_read on the mmap_sem of the 10761da177e4SLinus Torvalds * mm_struct of the VMA to prevent it from going away. Should be used for 10771da177e4SLinus Torvalds * all allocations for pages that will be mapped into 10781da177e4SLinus Torvalds * user space. Returns NULL when no page can be allocated. 10791da177e4SLinus Torvalds * 10801da177e4SLinus Torvalds * Should be called with the mm_sem of the vma hold. 10811da177e4SLinus Torvalds */ 10821da177e4SLinus Torvalds struct page * 1083dd0fc66fSAl Viro alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr) 10841da177e4SLinus Torvalds { 10856e21c8f1SChristoph Lameter struct mempolicy *pol = get_vma_policy(current, vma, addr); 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds cpuset_update_current_mems_allowed(); 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds if (unlikely(pol->policy == MPOL_INTERLEAVE)) { 10901da177e4SLinus Torvalds unsigned nid; 10915da7ca86SChristoph Lameter 10925da7ca86SChristoph Lameter nid = interleave_nid(pol, vma, addr, PAGE_SHIFT); 10931da177e4SLinus Torvalds return alloc_page_interleave(gfp, 0, nid); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds return __alloc_pages(gfp, 0, zonelist_policy(gfp, pol)); 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds /** 10991da177e4SLinus Torvalds * alloc_pages_current - Allocate pages. 11001da177e4SLinus Torvalds * 11011da177e4SLinus Torvalds * @gfp: 11021da177e4SLinus Torvalds * %GFP_USER user allocation, 11031da177e4SLinus Torvalds * %GFP_KERNEL kernel allocation, 11041da177e4SLinus Torvalds * %GFP_HIGHMEM highmem allocation, 11051da177e4SLinus Torvalds * %GFP_FS don't call back into a file system. 11061da177e4SLinus Torvalds * %GFP_ATOMIC don't sleep. 11071da177e4SLinus Torvalds * @order: Power of two of allocation size in pages. 0 is a single page. 11081da177e4SLinus Torvalds * 11091da177e4SLinus Torvalds * Allocate a page from the kernel page pool. When not in 11101da177e4SLinus Torvalds * interrupt context and apply the current process NUMA policy. 11111da177e4SLinus Torvalds * Returns NULL when no page can be allocated. 11121da177e4SLinus Torvalds * 11131da177e4SLinus Torvalds * Don't call cpuset_update_current_mems_allowed() unless 11141da177e4SLinus Torvalds * 1) it's ok to take cpuset_sem (can WAIT), and 11151da177e4SLinus Torvalds * 2) allocating for current task (not interrupt). 11161da177e4SLinus Torvalds */ 1117dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds struct mempolicy *pol = current->mempolicy; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds if ((gfp & __GFP_WAIT) && !in_interrupt()) 11221da177e4SLinus Torvalds cpuset_update_current_mems_allowed(); 11231da177e4SLinus Torvalds if (!pol || in_interrupt()) 11241da177e4SLinus Torvalds pol = &default_policy; 11251da177e4SLinus Torvalds if (pol->policy == MPOL_INTERLEAVE) 11261da177e4SLinus Torvalds return alloc_page_interleave(gfp, order, interleave_nodes(pol)); 11271da177e4SLinus Torvalds return __alloc_pages(gfp, order, zonelist_policy(gfp, pol)); 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current); 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds /* Slow path of a mempolicy copy */ 11321da177e4SLinus Torvalds struct mempolicy *__mpol_copy(struct mempolicy *old) 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL); 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds if (!new) 11371da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11381da177e4SLinus Torvalds *new = *old; 11391da177e4SLinus Torvalds atomic_set(&new->refcnt, 1); 11401da177e4SLinus Torvalds if (new->policy == MPOL_BIND) { 11411da177e4SLinus Torvalds int sz = ksize(old->v.zonelist); 11421da177e4SLinus Torvalds new->v.zonelist = kmalloc(sz, SLAB_KERNEL); 11431da177e4SLinus Torvalds if (!new->v.zonelist) { 11441da177e4SLinus Torvalds kmem_cache_free(policy_cache, new); 11451da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds memcpy(new->v.zonelist, old->v.zonelist, sz); 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds return new; 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */ 11531da177e4SLinus Torvalds int __mpol_equal(struct mempolicy *a, struct mempolicy *b) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds if (!a || !b) 11561da177e4SLinus Torvalds return 0; 11571da177e4SLinus Torvalds if (a->policy != b->policy) 11581da177e4SLinus Torvalds return 0; 11591da177e4SLinus Torvalds switch (a->policy) { 11601da177e4SLinus Torvalds case MPOL_DEFAULT: 11611da177e4SLinus Torvalds return 1; 11621da177e4SLinus Torvalds case MPOL_INTERLEAVE: 1163dfcd3c0dSAndi Kleen return nodes_equal(a->v.nodes, b->v.nodes); 11641da177e4SLinus Torvalds case MPOL_PREFERRED: 11651da177e4SLinus Torvalds return a->v.preferred_node == b->v.preferred_node; 11661da177e4SLinus Torvalds case MPOL_BIND: { 11671da177e4SLinus Torvalds int i; 11681da177e4SLinus Torvalds for (i = 0; a->v.zonelist->zones[i]; i++) 11691da177e4SLinus Torvalds if (a->v.zonelist->zones[i] != b->v.zonelist->zones[i]) 11701da177e4SLinus Torvalds return 0; 11711da177e4SLinus Torvalds return b->v.zonelist->zones[i] == NULL; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds default: 11741da177e4SLinus Torvalds BUG(); 11751da177e4SLinus Torvalds return 0; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds /* Slow path of a mpol destructor. */ 11801da177e4SLinus Torvalds void __mpol_free(struct mempolicy *p) 11811da177e4SLinus Torvalds { 11821da177e4SLinus Torvalds if (!atomic_dec_and_test(&p->refcnt)) 11831da177e4SLinus Torvalds return; 11841da177e4SLinus Torvalds if (p->policy == MPOL_BIND) 11851da177e4SLinus Torvalds kfree(p->v.zonelist); 11861da177e4SLinus Torvalds p->policy = MPOL_DEFAULT; 11871da177e4SLinus Torvalds kmem_cache_free(policy_cache, p); 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds /* 11911da177e4SLinus Torvalds * Shared memory backing store policy support. 11921da177e4SLinus Torvalds * 11931da177e4SLinus Torvalds * Remember policies even when nobody has shared memory mapped. 11941da177e4SLinus Torvalds * The policies are kept in Red-Black tree linked from the inode. 11951da177e4SLinus Torvalds * They are protected by the sp->lock spinlock, which should be held 11961da177e4SLinus Torvalds * for any accesses to the tree. 11971da177e4SLinus Torvalds */ 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds /* lookup first element intersecting start-end */ 12001da177e4SLinus Torvalds /* Caller holds sp->lock */ 12011da177e4SLinus Torvalds static struct sp_node * 12021da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end) 12031da177e4SLinus Torvalds { 12041da177e4SLinus Torvalds struct rb_node *n = sp->root.rb_node; 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds while (n) { 12071da177e4SLinus Torvalds struct sp_node *p = rb_entry(n, struct sp_node, nd); 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds if (start >= p->end) 12101da177e4SLinus Torvalds n = n->rb_right; 12111da177e4SLinus Torvalds else if (end <= p->start) 12121da177e4SLinus Torvalds n = n->rb_left; 12131da177e4SLinus Torvalds else 12141da177e4SLinus Torvalds break; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds if (!n) 12171da177e4SLinus Torvalds return NULL; 12181da177e4SLinus Torvalds for (;;) { 12191da177e4SLinus Torvalds struct sp_node *w = NULL; 12201da177e4SLinus Torvalds struct rb_node *prev = rb_prev(n); 12211da177e4SLinus Torvalds if (!prev) 12221da177e4SLinus Torvalds break; 12231da177e4SLinus Torvalds w = rb_entry(prev, struct sp_node, nd); 12241da177e4SLinus Torvalds if (w->end <= start) 12251da177e4SLinus Torvalds break; 12261da177e4SLinus Torvalds n = prev; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds return rb_entry(n, struct sp_node, nd); 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds /* Insert a new shared policy into the list. */ 12321da177e4SLinus Torvalds /* Caller holds sp->lock */ 12331da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new) 12341da177e4SLinus Torvalds { 12351da177e4SLinus Torvalds struct rb_node **p = &sp->root.rb_node; 12361da177e4SLinus Torvalds struct rb_node *parent = NULL; 12371da177e4SLinus Torvalds struct sp_node *nd; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds while (*p) { 12401da177e4SLinus Torvalds parent = *p; 12411da177e4SLinus Torvalds nd = rb_entry(parent, struct sp_node, nd); 12421da177e4SLinus Torvalds if (new->start < nd->start) 12431da177e4SLinus Torvalds p = &(*p)->rb_left; 12441da177e4SLinus Torvalds else if (new->end > nd->end) 12451da177e4SLinus Torvalds p = &(*p)->rb_right; 12461da177e4SLinus Torvalds else 12471da177e4SLinus Torvalds BUG(); 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds rb_link_node(&new->nd, parent, p); 12501da177e4SLinus Torvalds rb_insert_color(&new->nd, &sp->root); 12511da177e4SLinus Torvalds PDprintk("inserting %lx-%lx: %d\n", new->start, new->end, 12521da177e4SLinus Torvalds new->policy ? new->policy->policy : 0); 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* Find shared policy intersecting idx */ 12561da177e4SLinus Torvalds struct mempolicy * 12571da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct mempolicy *pol = NULL; 12601da177e4SLinus Torvalds struct sp_node *sn; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds if (!sp->root.rb_node) 12631da177e4SLinus Torvalds return NULL; 12641da177e4SLinus Torvalds spin_lock(&sp->lock); 12651da177e4SLinus Torvalds sn = sp_lookup(sp, idx, idx+1); 12661da177e4SLinus Torvalds if (sn) { 12671da177e4SLinus Torvalds mpol_get(sn->policy); 12681da177e4SLinus Torvalds pol = sn->policy; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds spin_unlock(&sp->lock); 12711da177e4SLinus Torvalds return pol; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n) 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds PDprintk("deleting %lx-l%x\n", n->start, n->end); 12771da177e4SLinus Torvalds rb_erase(&n->nd, &sp->root); 12781da177e4SLinus Torvalds mpol_free(n->policy); 12791da177e4SLinus Torvalds kmem_cache_free(sn_cache, n); 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds struct sp_node * 12831da177e4SLinus Torvalds sp_alloc(unsigned long start, unsigned long end, struct mempolicy *pol) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds struct sp_node *n = kmem_cache_alloc(sn_cache, GFP_KERNEL); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds if (!n) 12881da177e4SLinus Torvalds return NULL; 12891da177e4SLinus Torvalds n->start = start; 12901da177e4SLinus Torvalds n->end = end; 12911da177e4SLinus Torvalds mpol_get(pol); 12921da177e4SLinus Torvalds n->policy = pol; 12931da177e4SLinus Torvalds return n; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds /* Replace a policy range. */ 12971da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start, 12981da177e4SLinus Torvalds unsigned long end, struct sp_node *new) 12991da177e4SLinus Torvalds { 13001da177e4SLinus Torvalds struct sp_node *n, *new2 = NULL; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds restart: 13031da177e4SLinus Torvalds spin_lock(&sp->lock); 13041da177e4SLinus Torvalds n = sp_lookup(sp, start, end); 13051da177e4SLinus Torvalds /* Take care of old policies in the same range. */ 13061da177e4SLinus Torvalds while (n && n->start < end) { 13071da177e4SLinus Torvalds struct rb_node *next = rb_next(&n->nd); 13081da177e4SLinus Torvalds if (n->start >= start) { 13091da177e4SLinus Torvalds if (n->end <= end) 13101da177e4SLinus Torvalds sp_delete(sp, n); 13111da177e4SLinus Torvalds else 13121da177e4SLinus Torvalds n->start = end; 13131da177e4SLinus Torvalds } else { 13141da177e4SLinus Torvalds /* Old policy spanning whole new range. */ 13151da177e4SLinus Torvalds if (n->end > end) { 13161da177e4SLinus Torvalds if (!new2) { 13171da177e4SLinus Torvalds spin_unlock(&sp->lock); 13181da177e4SLinus Torvalds new2 = sp_alloc(end, n->end, n->policy); 13191da177e4SLinus Torvalds if (!new2) 13201da177e4SLinus Torvalds return -ENOMEM; 13211da177e4SLinus Torvalds goto restart; 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds n->end = start; 13241da177e4SLinus Torvalds sp_insert(sp, new2); 13251da177e4SLinus Torvalds new2 = NULL; 13261da177e4SLinus Torvalds break; 13271da177e4SLinus Torvalds } else 13281da177e4SLinus Torvalds n->end = start; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds if (!next) 13311da177e4SLinus Torvalds break; 13321da177e4SLinus Torvalds n = rb_entry(next, struct sp_node, nd); 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds if (new) 13351da177e4SLinus Torvalds sp_insert(sp, new); 13361da177e4SLinus Torvalds spin_unlock(&sp->lock); 13371da177e4SLinus Torvalds if (new2) { 13381da177e4SLinus Torvalds mpol_free(new2->policy); 13391da177e4SLinus Torvalds kmem_cache_free(sn_cache, new2); 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds return 0; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info, 13451da177e4SLinus Torvalds struct vm_area_struct *vma, struct mempolicy *npol) 13461da177e4SLinus Torvalds { 13471da177e4SLinus Torvalds int err; 13481da177e4SLinus Torvalds struct sp_node *new = NULL; 13491da177e4SLinus Torvalds unsigned long sz = vma_pages(vma); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds PDprintk("set_shared_policy %lx sz %lu %d %lx\n", 13521da177e4SLinus Torvalds vma->vm_pgoff, 13531da177e4SLinus Torvalds sz, npol? npol->policy : -1, 1354dfcd3c0dSAndi Kleen npol ? nodes_addr(npol->v.nodes)[0] : -1); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds if (npol) { 13571da177e4SLinus Torvalds new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); 13581da177e4SLinus Torvalds if (!new) 13591da177e4SLinus Torvalds return -ENOMEM; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); 13621da177e4SLinus Torvalds if (err && new) 13631da177e4SLinus Torvalds kmem_cache_free(sn_cache, new); 13641da177e4SLinus Torvalds return err; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */ 13681da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct sp_node *n; 13711da177e4SLinus Torvalds struct rb_node *next; 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds if (!p->root.rb_node) 13741da177e4SLinus Torvalds return; 13751da177e4SLinus Torvalds spin_lock(&p->lock); 13761da177e4SLinus Torvalds next = rb_first(&p->root); 13771da177e4SLinus Torvalds while (next) { 13781da177e4SLinus Torvalds n = rb_entry(next, struct sp_node, nd); 13791da177e4SLinus Torvalds next = rb_next(&n->nd); 138090c5029eSAndi Kleen rb_erase(&n->nd, &p->root); 13811da177e4SLinus Torvalds mpol_free(n->policy); 13821da177e4SLinus Torvalds kmem_cache_free(sn_cache, n); 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds spin_unlock(&p->lock); 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */ 13881da177e4SLinus Torvalds void __init numa_policy_init(void) 13891da177e4SLinus Torvalds { 13901da177e4SLinus Torvalds policy_cache = kmem_cache_create("numa_policy", 13911da177e4SLinus Torvalds sizeof(struct mempolicy), 13921da177e4SLinus Torvalds 0, SLAB_PANIC, NULL, NULL); 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds sn_cache = kmem_cache_create("shared_policy_node", 13951da177e4SLinus Torvalds sizeof(struct sp_node), 13961da177e4SLinus Torvalds 0, SLAB_PANIC, NULL, NULL); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds /* Set interleaving policy for system init. This way not all 13991da177e4SLinus Torvalds the data structures allocated at system boot end up in node zero. */ 14001da177e4SLinus Torvalds 14018bccd85fSChristoph Lameter if (do_set_mempolicy(MPOL_INTERLEAVE, &node_online_map)) 14021da177e4SLinus Torvalds printk("numa_policy_init: interleaving failed\n"); 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 14058bccd85fSChristoph Lameter /* Reset policy of current process to default */ 14061da177e4SLinus Torvalds void numa_default_policy(void) 14071da177e4SLinus Torvalds { 14088bccd85fSChristoph Lameter do_set_mempolicy(MPOL_DEFAULT, NULL); 14091da177e4SLinus Torvalds } 141068860ec1SPaul Jackson 141168860ec1SPaul Jackson /* Migrate a policy to a different set of nodes */ 141268860ec1SPaul Jackson static void rebind_policy(struct mempolicy *pol, const nodemask_t *old, 141368860ec1SPaul Jackson const nodemask_t *new) 141468860ec1SPaul Jackson { 141568860ec1SPaul Jackson nodemask_t tmp; 141668860ec1SPaul Jackson 141768860ec1SPaul Jackson if (!pol) 141868860ec1SPaul Jackson return; 141968860ec1SPaul Jackson 142068860ec1SPaul Jackson switch (pol->policy) { 142168860ec1SPaul Jackson case MPOL_DEFAULT: 142268860ec1SPaul Jackson break; 142368860ec1SPaul Jackson case MPOL_INTERLEAVE: 142468860ec1SPaul Jackson nodes_remap(tmp, pol->v.nodes, *old, *new); 142568860ec1SPaul Jackson pol->v.nodes = tmp; 142668860ec1SPaul Jackson current->il_next = node_remap(current->il_next, *old, *new); 142768860ec1SPaul Jackson break; 142868860ec1SPaul Jackson case MPOL_PREFERRED: 142968860ec1SPaul Jackson pol->v.preferred_node = node_remap(pol->v.preferred_node, 143068860ec1SPaul Jackson *old, *new); 143168860ec1SPaul Jackson break; 143268860ec1SPaul Jackson case MPOL_BIND: { 143368860ec1SPaul Jackson nodemask_t nodes; 143468860ec1SPaul Jackson struct zone **z; 143568860ec1SPaul Jackson struct zonelist *zonelist; 143668860ec1SPaul Jackson 143768860ec1SPaul Jackson nodes_clear(nodes); 143868860ec1SPaul Jackson for (z = pol->v.zonelist->zones; *z; z++) 143968860ec1SPaul Jackson node_set((*z)->zone_pgdat->node_id, nodes); 144068860ec1SPaul Jackson nodes_remap(tmp, nodes, *old, *new); 144168860ec1SPaul Jackson nodes = tmp; 144268860ec1SPaul Jackson 144368860ec1SPaul Jackson zonelist = bind_zonelist(&nodes); 144468860ec1SPaul Jackson 144568860ec1SPaul Jackson /* If no mem, then zonelist is NULL and we keep old zonelist. 144668860ec1SPaul Jackson * If that old zonelist has no remaining mems_allowed nodes, 144768860ec1SPaul Jackson * then zonelist_policy() will "FALL THROUGH" to MPOL_DEFAULT. 144868860ec1SPaul Jackson */ 144968860ec1SPaul Jackson 145068860ec1SPaul Jackson if (zonelist) { 145168860ec1SPaul Jackson /* Good - got mem - substitute new zonelist */ 145268860ec1SPaul Jackson kfree(pol->v.zonelist); 145368860ec1SPaul Jackson pol->v.zonelist = zonelist; 145468860ec1SPaul Jackson } 145568860ec1SPaul Jackson break; 145668860ec1SPaul Jackson } 145768860ec1SPaul Jackson default: 145868860ec1SPaul Jackson BUG(); 145968860ec1SPaul Jackson break; 146068860ec1SPaul Jackson } 146168860ec1SPaul Jackson } 146268860ec1SPaul Jackson 146368860ec1SPaul Jackson /* 146468860ec1SPaul Jackson * Someone moved this task to different nodes. Fixup mempolicies. 146568860ec1SPaul Jackson * 146668860ec1SPaul Jackson * TODO - fixup current->mm->vma and shmfs/tmpfs/hugetlbfs policies as well, 146768860ec1SPaul Jackson * once we have a cpuset mechanism to mark which cpuset subtree is migrating. 146868860ec1SPaul Jackson */ 146968860ec1SPaul Jackson void numa_policy_rebind(const nodemask_t *old, const nodemask_t *new) 147068860ec1SPaul Jackson { 147168860ec1SPaul Jackson rebind_policy(current->mempolicy, old, new); 147268860ec1SPaul Jackson } 14731a75a6c8SChristoph Lameter 14741a75a6c8SChristoph Lameter /* 14751a75a6c8SChristoph Lameter * Display pages allocated per node and memory policy via /proc. 14761a75a6c8SChristoph Lameter */ 14771a75a6c8SChristoph Lameter 14781a75a6c8SChristoph Lameter static const char *policy_types[] = { "default", "prefer", "bind", 14791a75a6c8SChristoph Lameter "interleave" }; 14801a75a6c8SChristoph Lameter 14811a75a6c8SChristoph Lameter /* 14821a75a6c8SChristoph Lameter * Convert a mempolicy into a string. 14831a75a6c8SChristoph Lameter * Returns the number of characters in buffer (if positive) 14841a75a6c8SChristoph Lameter * or an error (negative) 14851a75a6c8SChristoph Lameter */ 14861a75a6c8SChristoph Lameter static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) 14871a75a6c8SChristoph Lameter { 14881a75a6c8SChristoph Lameter char *p = buffer; 14891a75a6c8SChristoph Lameter int l; 14901a75a6c8SChristoph Lameter nodemask_t nodes; 14911a75a6c8SChristoph Lameter int mode = pol ? pol->policy : MPOL_DEFAULT; 14921a75a6c8SChristoph Lameter 14931a75a6c8SChristoph Lameter switch (mode) { 14941a75a6c8SChristoph Lameter case MPOL_DEFAULT: 14951a75a6c8SChristoph Lameter nodes_clear(nodes); 14961a75a6c8SChristoph Lameter break; 14971a75a6c8SChristoph Lameter 14981a75a6c8SChristoph Lameter case MPOL_PREFERRED: 14991a75a6c8SChristoph Lameter nodes_clear(nodes); 15001a75a6c8SChristoph Lameter node_set(pol->v.preferred_node, nodes); 15011a75a6c8SChristoph Lameter break; 15021a75a6c8SChristoph Lameter 15031a75a6c8SChristoph Lameter case MPOL_BIND: 15041a75a6c8SChristoph Lameter get_zonemask(pol, &nodes); 15051a75a6c8SChristoph Lameter break; 15061a75a6c8SChristoph Lameter 15071a75a6c8SChristoph Lameter case MPOL_INTERLEAVE: 15081a75a6c8SChristoph Lameter nodes = pol->v.nodes; 15091a75a6c8SChristoph Lameter break; 15101a75a6c8SChristoph Lameter 15111a75a6c8SChristoph Lameter default: 15121a75a6c8SChristoph Lameter BUG(); 15131a75a6c8SChristoph Lameter return -EFAULT; 15141a75a6c8SChristoph Lameter } 15151a75a6c8SChristoph Lameter 15161a75a6c8SChristoph Lameter l = strlen(policy_types[mode]); 15171a75a6c8SChristoph Lameter if (buffer + maxlen < p + l + 1) 15181a75a6c8SChristoph Lameter return -ENOSPC; 15191a75a6c8SChristoph Lameter 15201a75a6c8SChristoph Lameter strcpy(p, policy_types[mode]); 15211a75a6c8SChristoph Lameter p += l; 15221a75a6c8SChristoph Lameter 15231a75a6c8SChristoph Lameter if (!nodes_empty(nodes)) { 15241a75a6c8SChristoph Lameter if (buffer + maxlen < p + 2) 15251a75a6c8SChristoph Lameter return -ENOSPC; 15261a75a6c8SChristoph Lameter *p++ = '='; 15271a75a6c8SChristoph Lameter p += nodelist_scnprintf(p, buffer + maxlen - p, nodes); 15281a75a6c8SChristoph Lameter } 15291a75a6c8SChristoph Lameter return p - buffer; 15301a75a6c8SChristoph Lameter } 15311a75a6c8SChristoph Lameter 15321a75a6c8SChristoph Lameter struct numa_maps { 15331a75a6c8SChristoph Lameter unsigned long pages; 15341a75a6c8SChristoph Lameter unsigned long anon; 15351a75a6c8SChristoph Lameter unsigned long mapped; 15361a75a6c8SChristoph Lameter unsigned long mapcount_max; 15371a75a6c8SChristoph Lameter unsigned long node[MAX_NUMNODES]; 15381a75a6c8SChristoph Lameter }; 15391a75a6c8SChristoph Lameter 15401a75a6c8SChristoph Lameter static void gather_stats(struct page *page, void *private) 15411a75a6c8SChristoph Lameter { 15421a75a6c8SChristoph Lameter struct numa_maps *md = private; 15431a75a6c8SChristoph Lameter int count = page_mapcount(page); 15441a75a6c8SChristoph Lameter 15451a75a6c8SChristoph Lameter if (count) 15461a75a6c8SChristoph Lameter md->mapped++; 15471a75a6c8SChristoph Lameter 15481a75a6c8SChristoph Lameter if (count > md->mapcount_max) 15491a75a6c8SChristoph Lameter md->mapcount_max = count; 15501a75a6c8SChristoph Lameter 15511a75a6c8SChristoph Lameter md->pages++; 15521a75a6c8SChristoph Lameter 15531a75a6c8SChristoph Lameter if (PageAnon(page)) 15541a75a6c8SChristoph Lameter md->anon++; 15551a75a6c8SChristoph Lameter 15561a75a6c8SChristoph Lameter md->node[page_to_nid(page)]++; 15571a75a6c8SChristoph Lameter cond_resched(); 15581a75a6c8SChristoph Lameter } 15591a75a6c8SChristoph Lameter 15601a75a6c8SChristoph Lameter int show_numa_map(struct seq_file *m, void *v) 15611a75a6c8SChristoph Lameter { 15621a75a6c8SChristoph Lameter struct task_struct *task = m->private; 15631a75a6c8SChristoph Lameter struct vm_area_struct *vma = v; 15641a75a6c8SChristoph Lameter struct numa_maps *md; 15651a75a6c8SChristoph Lameter int n; 15661a75a6c8SChristoph Lameter char buffer[50]; 15671a75a6c8SChristoph Lameter 15681a75a6c8SChristoph Lameter if (!vma->vm_mm) 15691a75a6c8SChristoph Lameter return 0; 15701a75a6c8SChristoph Lameter 15711a75a6c8SChristoph Lameter md = kzalloc(sizeof(struct numa_maps), GFP_KERNEL); 15721a75a6c8SChristoph Lameter if (!md) 15731a75a6c8SChristoph Lameter return 0; 15741a75a6c8SChristoph Lameter 15751a75a6c8SChristoph Lameter check_pgd_range(vma, vma->vm_start, vma->vm_end, 15761a75a6c8SChristoph Lameter &node_online_map, MPOL_MF_STATS, md); 15771a75a6c8SChristoph Lameter 15781a75a6c8SChristoph Lameter if (md->pages) { 15791a75a6c8SChristoph Lameter mpol_to_str(buffer, sizeof(buffer), 15801a75a6c8SChristoph Lameter get_vma_policy(task, vma, vma->vm_start)); 15811a75a6c8SChristoph Lameter 15821a75a6c8SChristoph Lameter seq_printf(m, "%08lx %s pages=%lu mapped=%lu maxref=%lu", 15831a75a6c8SChristoph Lameter vma->vm_start, buffer, md->pages, 15841a75a6c8SChristoph Lameter md->mapped, md->mapcount_max); 15851a75a6c8SChristoph Lameter 15861a75a6c8SChristoph Lameter if (md->anon) 15871a75a6c8SChristoph Lameter seq_printf(m," anon=%lu",md->anon); 15881a75a6c8SChristoph Lameter 15891a75a6c8SChristoph Lameter for_each_online_node(n) 15901a75a6c8SChristoph Lameter if (md->node[n]) 15911a75a6c8SChristoph Lameter seq_printf(m, " N%d=%lu", n, md->node[n]); 15921a75a6c8SChristoph Lameter 15931a75a6c8SChristoph Lameter seq_putc(m, '\n'); 15941a75a6c8SChristoph Lameter } 15951a75a6c8SChristoph Lameter kfree(md); 15961a75a6c8SChristoph Lameter 15971a75a6c8SChristoph Lameter if (m->count < m->size) 15981a75a6c8SChristoph Lameter m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0; 15991a75a6c8SChristoph Lameter return 0; 16001a75a6c8SChristoph Lameter } 16011a75a6c8SChristoph Lameter 1602