11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Simple NUMA memory policy for the Linux kernel. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright 2003,2004 Andi Kleen, SuSE Labs. 58bccd85fSChristoph Lameter * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc. 61da177e4SLinus Torvalds * Subject to the GNU Public License, version 2. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * NUMA policy allows the user to give hints in which node(s) memory should 91da177e4SLinus Torvalds * be allocated. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Support four policies per VMA and per process: 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * The VMA policy has priority over the process policy for a page fault. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * interleave Allocate memory interleaved over a set of nodes, 161da177e4SLinus Torvalds * with normal fallback if it fails. 171da177e4SLinus Torvalds * For VMA based allocations this interleaves based on the 181da177e4SLinus Torvalds * offset into the backing object or offset into the mapping 191da177e4SLinus Torvalds * for anonymous memory. For process policy an process counter 201da177e4SLinus Torvalds * is used. 218bccd85fSChristoph Lameter * 221da177e4SLinus Torvalds * bind Only allocate memory on a specific set of nodes, 231da177e4SLinus Torvalds * no fallback. 248bccd85fSChristoph Lameter * FIXME: memory is allocated starting with the first node 258bccd85fSChristoph Lameter * to the last. It would be better if bind would truly restrict 268bccd85fSChristoph Lameter * the allocation to memory nodes instead 278bccd85fSChristoph Lameter * 281da177e4SLinus Torvalds * preferred Try a specific node first before normal fallback. 291da177e4SLinus Torvalds * As a special case node -1 here means do the allocation 301da177e4SLinus Torvalds * on the local CPU. This is normally identical to default, 311da177e4SLinus Torvalds * but useful to set in a VMA when you have a non default 321da177e4SLinus Torvalds * process policy. 338bccd85fSChristoph Lameter * 341da177e4SLinus Torvalds * default Allocate on the local node first, or when on a VMA 351da177e4SLinus Torvalds * use the process policy. This is what Linux always did 361da177e4SLinus Torvalds * in a NUMA aware kernel and still does by, ahem, default. 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * The process policy is applied for most non interrupt memory allocations 391da177e4SLinus Torvalds * in that process' context. Interrupts ignore the policies and always 401da177e4SLinus Torvalds * try to allocate on the local CPU. The VMA policy is only applied for memory 411da177e4SLinus Torvalds * allocations for a VMA in the VM. 421da177e4SLinus Torvalds * 431da177e4SLinus Torvalds * Currently there are a few corner cases in swapping where the policy 441da177e4SLinus Torvalds * is not applied, but the majority should be handled. When process policy 451da177e4SLinus Torvalds * is used it is not remembered over swap outs/swap ins. 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Only the highest zone in the zone hierarchy gets policied. Allocations 481da177e4SLinus Torvalds * requesting a lower zone just use default policy. This implies that 491da177e4SLinus Torvalds * on systems with highmem kernel lowmem allocation don't get policied. 501da177e4SLinus Torvalds * Same with GFP_DMA allocations. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between 531da177e4SLinus Torvalds * all users and remembered even when nobody has memory mapped. 541da177e4SLinus Torvalds */ 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* Notebook: 571da177e4SLinus Torvalds fix mmap readahead to honour policy and enable policy for any page cache 581da177e4SLinus Torvalds object 591da177e4SLinus Torvalds statistics for bigpages 601da177e4SLinus Torvalds global policy for page cache? currently it uses process policy. Requires 611da177e4SLinus Torvalds first item above. 621da177e4SLinus Torvalds handle mremap for shared memory (currently ignored for the policy) 631da177e4SLinus Torvalds grows down? 641da177e4SLinus Torvalds make bind policy root only? It can trigger oom much faster and the 651da177e4SLinus Torvalds kernel is not always grateful with that. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #include <linux/mempolicy.h> 691da177e4SLinus Torvalds #include <linux/mm.h> 701da177e4SLinus Torvalds #include <linux/highmem.h> 711da177e4SLinus Torvalds #include <linux/hugetlb.h> 721da177e4SLinus Torvalds #include <linux/kernel.h> 731da177e4SLinus Torvalds #include <linux/sched.h> 741da177e4SLinus Torvalds #include <linux/nodemask.h> 751da177e4SLinus Torvalds #include <linux/cpuset.h> 761da177e4SLinus Torvalds #include <linux/slab.h> 771da177e4SLinus Torvalds #include <linux/string.h> 78b95f1b31SPaul Gortmaker #include <linux/export.h> 79b488893aSPavel Emelyanov #include <linux/nsproxy.h> 801da177e4SLinus Torvalds #include <linux/interrupt.h> 811da177e4SLinus Torvalds #include <linux/init.h> 821da177e4SLinus Torvalds #include <linux/compat.h> 83dc9aa5b9SChristoph Lameter #include <linux/swap.h> 841a75a6c8SChristoph Lameter #include <linux/seq_file.h> 851a75a6c8SChristoph Lameter #include <linux/proc_fs.h> 86b20a3503SChristoph Lameter #include <linux/migrate.h> 8762b61f61SHugh Dickins #include <linux/ksm.h> 8895a402c3SChristoph Lameter #include <linux/rmap.h> 8986c3a764SDavid Quigley #include <linux/security.h> 90dbcb0f19SAdrian Bunk #include <linux/syscalls.h> 91095f1fc4SLee Schermerhorn #include <linux/ctype.h> 926d9c285aSKOSAKI Motohiro #include <linux/mm_inline.h> 93*b24f53a0SLee Schermerhorn #include <linux/mmu_notifier.h> 94dc9aa5b9SChristoph Lameter 951da177e4SLinus Torvalds #include <asm/tlbflush.h> 961da177e4SLinus Torvalds #include <asm/uaccess.h> 97778d3b0fSMichal Hocko #include <linux/random.h> 981da177e4SLinus Torvalds 9962695a84SNick Piggin #include "internal.h" 10062695a84SNick Piggin 10138e35860SChristoph Lameter /* Internal flags */ 102dc9aa5b9SChristoph Lameter #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */ 10338e35860SChristoph Lameter #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */ 104dc9aa5b9SChristoph Lameter 105fcc234f8SPekka Enberg static struct kmem_cache *policy_cache; 106fcc234f8SPekka Enberg static struct kmem_cache *sn_cache; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* Highest zone. An specific allocation for a zone below that is not 1091da177e4SLinus Torvalds policied. */ 1106267276fSChristoph Lameter enum zone_type policy_zone = 0; 1111da177e4SLinus Torvalds 112bea904d5SLee Schermerhorn /* 113bea904d5SLee Schermerhorn * run-time system-wide default policy => local allocation 114bea904d5SLee Schermerhorn */ 115e754d79dSH Hartley Sweeten static struct mempolicy default_policy = { 1161da177e4SLinus Torvalds .refcnt = ATOMIC_INIT(1), /* never free it */ 117bea904d5SLee Schermerhorn .mode = MPOL_PREFERRED, 118fc36b8d3SLee Schermerhorn .flags = MPOL_F_LOCAL, 1191da177e4SLinus Torvalds }; 1201da177e4SLinus Torvalds 12137012946SDavid Rientjes static const struct mempolicy_operations { 12237012946SDavid Rientjes int (*create)(struct mempolicy *pol, const nodemask_t *nodes); 123708c1bbcSMiao Xie /* 124708c1bbcSMiao Xie * If read-side task has no lock to protect task->mempolicy, write-side 125708c1bbcSMiao Xie * task will rebind the task->mempolicy by two step. The first step is 126708c1bbcSMiao Xie * setting all the newly nodes, and the second step is cleaning all the 127708c1bbcSMiao Xie * disallowed nodes. In this way, we can avoid finding no node to alloc 128708c1bbcSMiao Xie * page. 129708c1bbcSMiao Xie * If we have a lock to protect task->mempolicy in read-side, we do 130708c1bbcSMiao Xie * rebind directly. 131708c1bbcSMiao Xie * 132708c1bbcSMiao Xie * step: 133708c1bbcSMiao Xie * MPOL_REBIND_ONCE - do rebind work at once 134708c1bbcSMiao Xie * MPOL_REBIND_STEP1 - set all the newly nodes 135708c1bbcSMiao Xie * MPOL_REBIND_STEP2 - clean all the disallowed nodes 136708c1bbcSMiao Xie */ 137708c1bbcSMiao Xie void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes, 138708c1bbcSMiao Xie enum mpol_rebind_step step); 13937012946SDavid Rientjes } mpol_ops[MPOL_MAX]; 14037012946SDavid Rientjes 14119770b32SMel Gorman /* Check that the nodemask contains at least one populated zone */ 14237012946SDavid Rientjes static int is_valid_nodemask(const nodemask_t *nodemask) 1431da177e4SLinus Torvalds { 14419770b32SMel Gorman int nd, k; 1451da177e4SLinus Torvalds 14619770b32SMel Gorman for_each_node_mask(nd, *nodemask) { 14719770b32SMel Gorman struct zone *z; 14819770b32SMel Gorman 14919770b32SMel Gorman for (k = 0; k <= policy_zone; k++) { 15019770b32SMel Gorman z = &NODE_DATA(nd)->node_zones[k]; 151dd942ae3SAndi Kleen if (z->present_pages > 0) 15219770b32SMel Gorman return 1; 153dd942ae3SAndi Kleen } 154dd942ae3SAndi Kleen } 15519770b32SMel Gorman 15619770b32SMel Gorman return 0; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 159f5b087b5SDavid Rientjes static inline int mpol_store_user_nodemask(const struct mempolicy *pol) 160f5b087b5SDavid Rientjes { 1616d556294SBob Liu return pol->flags & MPOL_MODE_FLAGS; 1624c50bc01SDavid Rientjes } 1634c50bc01SDavid Rientjes 1644c50bc01SDavid Rientjes static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig, 1654c50bc01SDavid Rientjes const nodemask_t *rel) 1664c50bc01SDavid Rientjes { 1674c50bc01SDavid Rientjes nodemask_t tmp; 1684c50bc01SDavid Rientjes nodes_fold(tmp, *orig, nodes_weight(*rel)); 1694c50bc01SDavid Rientjes nodes_onto(*ret, tmp, *rel); 170f5b087b5SDavid Rientjes } 171f5b087b5SDavid Rientjes 17237012946SDavid Rientjes static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes) 17337012946SDavid Rientjes { 17437012946SDavid Rientjes if (nodes_empty(*nodes)) 17537012946SDavid Rientjes return -EINVAL; 17637012946SDavid Rientjes pol->v.nodes = *nodes; 17737012946SDavid Rientjes return 0; 17837012946SDavid Rientjes } 17937012946SDavid Rientjes 18037012946SDavid Rientjes static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes) 18137012946SDavid Rientjes { 18237012946SDavid Rientjes if (!nodes) 183fc36b8d3SLee Schermerhorn pol->flags |= MPOL_F_LOCAL; /* local allocation */ 18437012946SDavid Rientjes else if (nodes_empty(*nodes)) 18537012946SDavid Rientjes return -EINVAL; /* no allowed nodes */ 18637012946SDavid Rientjes else 18737012946SDavid Rientjes pol->v.preferred_node = first_node(*nodes); 18837012946SDavid Rientjes return 0; 18937012946SDavid Rientjes } 19037012946SDavid Rientjes 19137012946SDavid Rientjes static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes) 19237012946SDavid Rientjes { 19337012946SDavid Rientjes if (!is_valid_nodemask(nodes)) 19437012946SDavid Rientjes return -EINVAL; 19537012946SDavid Rientjes pol->v.nodes = *nodes; 19637012946SDavid Rientjes return 0; 19737012946SDavid Rientjes } 19837012946SDavid Rientjes 19958568d2aSMiao Xie /* 20058568d2aSMiao Xie * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if 20158568d2aSMiao Xie * any, for the new policy. mpol_new() has already validated the nodes 20258568d2aSMiao Xie * parameter with respect to the policy mode and flags. But, we need to 20358568d2aSMiao Xie * handle an empty nodemask with MPOL_PREFERRED here. 20458568d2aSMiao Xie * 20558568d2aSMiao Xie * Must be called holding task's alloc_lock to protect task's mems_allowed 20658568d2aSMiao Xie * and mempolicy. May also be called holding the mmap_semaphore for write. 20758568d2aSMiao Xie */ 2084bfc4495SKAMEZAWA Hiroyuki static int mpol_set_nodemask(struct mempolicy *pol, 2094bfc4495SKAMEZAWA Hiroyuki const nodemask_t *nodes, struct nodemask_scratch *nsc) 21058568d2aSMiao Xie { 21158568d2aSMiao Xie int ret; 21258568d2aSMiao Xie 21358568d2aSMiao Xie /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */ 21458568d2aSMiao Xie if (pol == NULL) 21558568d2aSMiao Xie return 0; 2164bfc4495SKAMEZAWA Hiroyuki /* Check N_HIGH_MEMORY */ 2174bfc4495SKAMEZAWA Hiroyuki nodes_and(nsc->mask1, 2184bfc4495SKAMEZAWA Hiroyuki cpuset_current_mems_allowed, node_states[N_HIGH_MEMORY]); 21958568d2aSMiao Xie 22058568d2aSMiao Xie VM_BUG_ON(!nodes); 22158568d2aSMiao Xie if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes)) 22258568d2aSMiao Xie nodes = NULL; /* explicit local allocation */ 22358568d2aSMiao Xie else { 22458568d2aSMiao Xie if (pol->flags & MPOL_F_RELATIVE_NODES) 2254bfc4495SKAMEZAWA Hiroyuki mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1); 22658568d2aSMiao Xie else 2274bfc4495SKAMEZAWA Hiroyuki nodes_and(nsc->mask2, *nodes, nsc->mask1); 2284bfc4495SKAMEZAWA Hiroyuki 22958568d2aSMiao Xie if (mpol_store_user_nodemask(pol)) 23058568d2aSMiao Xie pol->w.user_nodemask = *nodes; 23158568d2aSMiao Xie else 23258568d2aSMiao Xie pol->w.cpuset_mems_allowed = 23358568d2aSMiao Xie cpuset_current_mems_allowed; 23458568d2aSMiao Xie } 23558568d2aSMiao Xie 2364bfc4495SKAMEZAWA Hiroyuki if (nodes) 2374bfc4495SKAMEZAWA Hiroyuki ret = mpol_ops[pol->mode].create(pol, &nsc->mask2); 2384bfc4495SKAMEZAWA Hiroyuki else 2394bfc4495SKAMEZAWA Hiroyuki ret = mpol_ops[pol->mode].create(pol, NULL); 24058568d2aSMiao Xie return ret; 24158568d2aSMiao Xie } 24258568d2aSMiao Xie 24358568d2aSMiao Xie /* 24458568d2aSMiao Xie * This function just creates a new policy, does some check and simple 24558568d2aSMiao Xie * initialization. You must invoke mpol_set_nodemask() to set nodes. 24658568d2aSMiao Xie */ 247028fec41SDavid Rientjes static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, 248028fec41SDavid Rientjes nodemask_t *nodes) 2491da177e4SLinus Torvalds { 2501da177e4SLinus Torvalds struct mempolicy *policy; 2511da177e4SLinus Torvalds 252028fec41SDavid Rientjes pr_debug("setting mode %d flags %d nodes[0] %lx\n", 253028fec41SDavid Rientjes mode, flags, nodes ? nodes_addr(*nodes)[0] : -1); 254140d5a49SPaul Mundt 255d3a71033SLee Schermerhorn if (mode == MPOL_DEFAULT || mode == MPOL_NOOP) { 2563e1f0645SDavid Rientjes if (nodes && !nodes_empty(*nodes)) 25737012946SDavid Rientjes return ERR_PTR(-EINVAL); 258d3a71033SLee Schermerhorn return NULL; 25937012946SDavid Rientjes } 2603e1f0645SDavid Rientjes VM_BUG_ON(!nodes); 2613e1f0645SDavid Rientjes 2623e1f0645SDavid Rientjes /* 2633e1f0645SDavid Rientjes * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or 2643e1f0645SDavid Rientjes * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation). 2653e1f0645SDavid Rientjes * All other modes require a valid pointer to a non-empty nodemask. 2663e1f0645SDavid Rientjes */ 2673e1f0645SDavid Rientjes if (mode == MPOL_PREFERRED) { 2683e1f0645SDavid Rientjes if (nodes_empty(*nodes)) { 2693e1f0645SDavid Rientjes if (((flags & MPOL_F_STATIC_NODES) || 2703e1f0645SDavid Rientjes (flags & MPOL_F_RELATIVE_NODES))) 2713e1f0645SDavid Rientjes return ERR_PTR(-EINVAL); 2723e1f0645SDavid Rientjes } 273479e2802SPeter Zijlstra } else if (mode == MPOL_LOCAL) { 274479e2802SPeter Zijlstra if (!nodes_empty(*nodes)) 275479e2802SPeter Zijlstra return ERR_PTR(-EINVAL); 276479e2802SPeter Zijlstra mode = MPOL_PREFERRED; 2773e1f0645SDavid Rientjes } else if (nodes_empty(*nodes)) 2783e1f0645SDavid Rientjes return ERR_PTR(-EINVAL); 2791da177e4SLinus Torvalds policy = kmem_cache_alloc(policy_cache, GFP_KERNEL); 2801da177e4SLinus Torvalds if (!policy) 2811da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 2821da177e4SLinus Torvalds atomic_set(&policy->refcnt, 1); 28345c4745aSLee Schermerhorn policy->mode = mode; 28437012946SDavid Rientjes policy->flags = flags; 2853e1f0645SDavid Rientjes 28637012946SDavid Rientjes return policy; 28737012946SDavid Rientjes } 28837012946SDavid Rientjes 28952cd3b07SLee Schermerhorn /* Slow path of a mpol destructor. */ 29052cd3b07SLee Schermerhorn void __mpol_put(struct mempolicy *p) 29152cd3b07SLee Schermerhorn { 29252cd3b07SLee Schermerhorn if (!atomic_dec_and_test(&p->refcnt)) 29352cd3b07SLee Schermerhorn return; 29452cd3b07SLee Schermerhorn kmem_cache_free(policy_cache, p); 29552cd3b07SLee Schermerhorn } 29652cd3b07SLee Schermerhorn 297708c1bbcSMiao Xie static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes, 298708c1bbcSMiao Xie enum mpol_rebind_step step) 29937012946SDavid Rientjes { 30037012946SDavid Rientjes } 30137012946SDavid Rientjes 302708c1bbcSMiao Xie /* 303708c1bbcSMiao Xie * step: 304708c1bbcSMiao Xie * MPOL_REBIND_ONCE - do rebind work at once 305708c1bbcSMiao Xie * MPOL_REBIND_STEP1 - set all the newly nodes 306708c1bbcSMiao Xie * MPOL_REBIND_STEP2 - clean all the disallowed nodes 307708c1bbcSMiao Xie */ 308708c1bbcSMiao Xie static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes, 309708c1bbcSMiao Xie enum mpol_rebind_step step) 3101d0d2680SDavid Rientjes { 3111d0d2680SDavid Rientjes nodemask_t tmp; 3121d0d2680SDavid Rientjes 31337012946SDavid Rientjes if (pol->flags & MPOL_F_STATIC_NODES) 31437012946SDavid Rientjes nodes_and(tmp, pol->w.user_nodemask, *nodes); 31537012946SDavid Rientjes else if (pol->flags & MPOL_F_RELATIVE_NODES) 31637012946SDavid Rientjes mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); 3171d0d2680SDavid Rientjes else { 318708c1bbcSMiao Xie /* 319708c1bbcSMiao Xie * if step == 1, we use ->w.cpuset_mems_allowed to cache the 320708c1bbcSMiao Xie * result 321708c1bbcSMiao Xie */ 322708c1bbcSMiao Xie if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) { 323708c1bbcSMiao Xie nodes_remap(tmp, pol->v.nodes, 324708c1bbcSMiao Xie pol->w.cpuset_mems_allowed, *nodes); 325708c1bbcSMiao Xie pol->w.cpuset_mems_allowed = step ? tmp : *nodes; 326708c1bbcSMiao Xie } else if (step == MPOL_REBIND_STEP2) { 327708c1bbcSMiao Xie tmp = pol->w.cpuset_mems_allowed; 32837012946SDavid Rientjes pol->w.cpuset_mems_allowed = *nodes; 329708c1bbcSMiao Xie } else 330708c1bbcSMiao Xie BUG(); 3311d0d2680SDavid Rientjes } 33237012946SDavid Rientjes 333708c1bbcSMiao Xie if (nodes_empty(tmp)) 334708c1bbcSMiao Xie tmp = *nodes; 335708c1bbcSMiao Xie 336708c1bbcSMiao Xie if (step == MPOL_REBIND_STEP1) 337708c1bbcSMiao Xie nodes_or(pol->v.nodes, pol->v.nodes, tmp); 338708c1bbcSMiao Xie else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2) 3391d0d2680SDavid Rientjes pol->v.nodes = tmp; 340708c1bbcSMiao Xie else 341708c1bbcSMiao Xie BUG(); 342708c1bbcSMiao Xie 3431d0d2680SDavid Rientjes if (!node_isset(current->il_next, tmp)) { 3441d0d2680SDavid Rientjes current->il_next = next_node(current->il_next, tmp); 3451d0d2680SDavid Rientjes if (current->il_next >= MAX_NUMNODES) 3461d0d2680SDavid Rientjes current->il_next = first_node(tmp); 3471d0d2680SDavid Rientjes if (current->il_next >= MAX_NUMNODES) 3481d0d2680SDavid Rientjes current->il_next = numa_node_id(); 3491d0d2680SDavid Rientjes } 35037012946SDavid Rientjes } 35137012946SDavid Rientjes 35237012946SDavid Rientjes static void mpol_rebind_preferred(struct mempolicy *pol, 353708c1bbcSMiao Xie const nodemask_t *nodes, 354708c1bbcSMiao Xie enum mpol_rebind_step step) 35537012946SDavid Rientjes { 35637012946SDavid Rientjes nodemask_t tmp; 35737012946SDavid Rientjes 35837012946SDavid Rientjes if (pol->flags & MPOL_F_STATIC_NODES) { 3591d0d2680SDavid Rientjes int node = first_node(pol->w.user_nodemask); 3601d0d2680SDavid Rientjes 361fc36b8d3SLee Schermerhorn if (node_isset(node, *nodes)) { 3621d0d2680SDavid Rientjes pol->v.preferred_node = node; 363fc36b8d3SLee Schermerhorn pol->flags &= ~MPOL_F_LOCAL; 364fc36b8d3SLee Schermerhorn } else 365fc36b8d3SLee Schermerhorn pol->flags |= MPOL_F_LOCAL; 36637012946SDavid Rientjes } else if (pol->flags & MPOL_F_RELATIVE_NODES) { 36737012946SDavid Rientjes mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); 3681d0d2680SDavid Rientjes pol->v.preferred_node = first_node(tmp); 369fc36b8d3SLee Schermerhorn } else if (!(pol->flags & MPOL_F_LOCAL)) { 3701d0d2680SDavid Rientjes pol->v.preferred_node = node_remap(pol->v.preferred_node, 37137012946SDavid Rientjes pol->w.cpuset_mems_allowed, 37237012946SDavid Rientjes *nodes); 37337012946SDavid Rientjes pol->w.cpuset_mems_allowed = *nodes; 3741d0d2680SDavid Rientjes } 3751d0d2680SDavid Rientjes } 37637012946SDavid Rientjes 377708c1bbcSMiao Xie /* 378708c1bbcSMiao Xie * mpol_rebind_policy - Migrate a policy to a different set of nodes 379708c1bbcSMiao Xie * 380708c1bbcSMiao Xie * If read-side task has no lock to protect task->mempolicy, write-side 381708c1bbcSMiao Xie * task will rebind the task->mempolicy by two step. The first step is 382708c1bbcSMiao Xie * setting all the newly nodes, and the second step is cleaning all the 383708c1bbcSMiao Xie * disallowed nodes. In this way, we can avoid finding no node to alloc 384708c1bbcSMiao Xie * page. 385708c1bbcSMiao Xie * If we have a lock to protect task->mempolicy in read-side, we do 386708c1bbcSMiao Xie * rebind directly. 387708c1bbcSMiao Xie * 388708c1bbcSMiao Xie * step: 389708c1bbcSMiao Xie * MPOL_REBIND_ONCE - do rebind work at once 390708c1bbcSMiao Xie * MPOL_REBIND_STEP1 - set all the newly nodes 391708c1bbcSMiao Xie * MPOL_REBIND_STEP2 - clean all the disallowed nodes 392708c1bbcSMiao Xie */ 393708c1bbcSMiao Xie static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask, 394708c1bbcSMiao Xie enum mpol_rebind_step step) 39537012946SDavid Rientjes { 39637012946SDavid Rientjes if (!pol) 39737012946SDavid Rientjes return; 39889c522c7SWang Sheng-Hui if (!mpol_store_user_nodemask(pol) && step == MPOL_REBIND_ONCE && 39937012946SDavid Rientjes nodes_equal(pol->w.cpuset_mems_allowed, *newmask)) 40037012946SDavid Rientjes return; 401708c1bbcSMiao Xie 402708c1bbcSMiao Xie if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING)) 403708c1bbcSMiao Xie return; 404708c1bbcSMiao Xie 405708c1bbcSMiao Xie if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING)) 406708c1bbcSMiao Xie BUG(); 407708c1bbcSMiao Xie 408708c1bbcSMiao Xie if (step == MPOL_REBIND_STEP1) 409708c1bbcSMiao Xie pol->flags |= MPOL_F_REBINDING; 410708c1bbcSMiao Xie else if (step == MPOL_REBIND_STEP2) 411708c1bbcSMiao Xie pol->flags &= ~MPOL_F_REBINDING; 412708c1bbcSMiao Xie else if (step >= MPOL_REBIND_NSTEP) 413708c1bbcSMiao Xie BUG(); 414708c1bbcSMiao Xie 415708c1bbcSMiao Xie mpol_ops[pol->mode].rebind(pol, newmask, step); 4161d0d2680SDavid Rientjes } 4171d0d2680SDavid Rientjes 4181d0d2680SDavid Rientjes /* 4191d0d2680SDavid Rientjes * Wrapper for mpol_rebind_policy() that just requires task 4201d0d2680SDavid Rientjes * pointer, and updates task mempolicy. 42158568d2aSMiao Xie * 42258568d2aSMiao Xie * Called with task's alloc_lock held. 4231d0d2680SDavid Rientjes */ 4241d0d2680SDavid Rientjes 425708c1bbcSMiao Xie void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new, 426708c1bbcSMiao Xie enum mpol_rebind_step step) 4271d0d2680SDavid Rientjes { 428708c1bbcSMiao Xie mpol_rebind_policy(tsk->mempolicy, new, step); 4291d0d2680SDavid Rientjes } 4301d0d2680SDavid Rientjes 4311d0d2680SDavid Rientjes /* 4321d0d2680SDavid Rientjes * Rebind each vma in mm to new nodemask. 4331d0d2680SDavid Rientjes * 4341d0d2680SDavid Rientjes * Call holding a reference to mm. Takes mm->mmap_sem during call. 4351d0d2680SDavid Rientjes */ 4361d0d2680SDavid Rientjes 4371d0d2680SDavid Rientjes void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new) 4381d0d2680SDavid Rientjes { 4391d0d2680SDavid Rientjes struct vm_area_struct *vma; 4401d0d2680SDavid Rientjes 4411d0d2680SDavid Rientjes down_write(&mm->mmap_sem); 4421d0d2680SDavid Rientjes for (vma = mm->mmap; vma; vma = vma->vm_next) 443708c1bbcSMiao Xie mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE); 4441d0d2680SDavid Rientjes up_write(&mm->mmap_sem); 4451d0d2680SDavid Rientjes } 4461d0d2680SDavid Rientjes 44737012946SDavid Rientjes static const struct mempolicy_operations mpol_ops[MPOL_MAX] = { 44837012946SDavid Rientjes [MPOL_DEFAULT] = { 44937012946SDavid Rientjes .rebind = mpol_rebind_default, 45037012946SDavid Rientjes }, 45137012946SDavid Rientjes [MPOL_INTERLEAVE] = { 45237012946SDavid Rientjes .create = mpol_new_interleave, 45337012946SDavid Rientjes .rebind = mpol_rebind_nodemask, 45437012946SDavid Rientjes }, 45537012946SDavid Rientjes [MPOL_PREFERRED] = { 45637012946SDavid Rientjes .create = mpol_new_preferred, 45737012946SDavid Rientjes .rebind = mpol_rebind_preferred, 45837012946SDavid Rientjes }, 45937012946SDavid Rientjes [MPOL_BIND] = { 46037012946SDavid Rientjes .create = mpol_new_bind, 46137012946SDavid Rientjes .rebind = mpol_rebind_nodemask, 46237012946SDavid Rientjes }, 46337012946SDavid Rientjes }; 46437012946SDavid Rientjes 465fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist, 466fc301289SChristoph Lameter unsigned long flags); 4671a75a6c8SChristoph Lameter 46838e35860SChristoph Lameter /* Scan through pages checking if pages follow certain conditions. */ 469b5810039SNick Piggin static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 470dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 471dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 47238e35860SChristoph Lameter void *private) 4731da177e4SLinus Torvalds { 47491612e0dSHugh Dickins pte_t *orig_pte; 47591612e0dSHugh Dickins pte_t *pte; 476705e87c0SHugh Dickins spinlock_t *ptl; 477941150a3SHugh Dickins 478705e87c0SHugh Dickins orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 47991612e0dSHugh Dickins do { 4806aab341eSLinus Torvalds struct page *page; 48125ba77c1SAndy Whitcroft int nid; 48291612e0dSHugh Dickins 48391612e0dSHugh Dickins if (!pte_present(*pte)) 48491612e0dSHugh Dickins continue; 4856aab341eSLinus Torvalds page = vm_normal_page(vma, addr, *pte); 4866aab341eSLinus Torvalds if (!page) 48791612e0dSHugh Dickins continue; 488053837fcSNick Piggin /* 48962b61f61SHugh Dickins * vm_normal_page() filters out zero pages, but there might 49062b61f61SHugh Dickins * still be PageReserved pages to skip, perhaps in a VDSO. 49162b61f61SHugh Dickins * And we cannot move PageKsm pages sensibly or safely yet. 492053837fcSNick Piggin */ 49362b61f61SHugh Dickins if (PageReserved(page) || PageKsm(page)) 494f4598c8bSChristoph Lameter continue; 4956aab341eSLinus Torvalds nid = page_to_nid(page); 49638e35860SChristoph Lameter if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT)) 49738e35860SChristoph Lameter continue; 49838e35860SChristoph Lameter 499b1f72d18SStephen Wilson if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) 500fc301289SChristoph Lameter migrate_page_add(page, private, flags); 501dc9aa5b9SChristoph Lameter else 5021da177e4SLinus Torvalds break; 50391612e0dSHugh Dickins } while (pte++, addr += PAGE_SIZE, addr != end); 504705e87c0SHugh Dickins pte_unmap_unlock(orig_pte, ptl); 50591612e0dSHugh Dickins return addr != end; 50691612e0dSHugh Dickins } 50791612e0dSHugh Dickins 508b5810039SNick Piggin static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud, 509dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 510dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 51138e35860SChristoph Lameter void *private) 51291612e0dSHugh Dickins { 51391612e0dSHugh Dickins pmd_t *pmd; 51491612e0dSHugh Dickins unsigned long next; 51591612e0dSHugh Dickins 51691612e0dSHugh Dickins pmd = pmd_offset(pud, addr); 51791612e0dSHugh Dickins do { 51891612e0dSHugh Dickins next = pmd_addr_end(addr, end); 519bae9c19bSAndrea Arcangeli split_huge_page_pmd(vma->vm_mm, pmd); 5201a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 52191612e0dSHugh Dickins continue; 522dc9aa5b9SChristoph Lameter if (check_pte_range(vma, pmd, addr, next, nodes, 52338e35860SChristoph Lameter flags, private)) 52491612e0dSHugh Dickins return -EIO; 52591612e0dSHugh Dickins } while (pmd++, addr = next, addr != end); 52691612e0dSHugh Dickins return 0; 52791612e0dSHugh Dickins } 52891612e0dSHugh Dickins 529b5810039SNick Piggin static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 530dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 531dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 53238e35860SChristoph Lameter void *private) 53391612e0dSHugh Dickins { 53491612e0dSHugh Dickins pud_t *pud; 53591612e0dSHugh Dickins unsigned long next; 53691612e0dSHugh Dickins 53791612e0dSHugh Dickins pud = pud_offset(pgd, addr); 53891612e0dSHugh Dickins do { 53991612e0dSHugh Dickins next = pud_addr_end(addr, end); 54091612e0dSHugh Dickins if (pud_none_or_clear_bad(pud)) 54191612e0dSHugh Dickins continue; 542dc9aa5b9SChristoph Lameter if (check_pmd_range(vma, pud, addr, next, nodes, 54338e35860SChristoph Lameter flags, private)) 54491612e0dSHugh Dickins return -EIO; 54591612e0dSHugh Dickins } while (pud++, addr = next, addr != end); 54691612e0dSHugh Dickins return 0; 54791612e0dSHugh Dickins } 54891612e0dSHugh Dickins 549b5810039SNick Piggin static inline int check_pgd_range(struct vm_area_struct *vma, 550dc9aa5b9SChristoph Lameter unsigned long addr, unsigned long end, 551dc9aa5b9SChristoph Lameter const nodemask_t *nodes, unsigned long flags, 55238e35860SChristoph Lameter void *private) 55391612e0dSHugh Dickins { 55491612e0dSHugh Dickins pgd_t *pgd; 55591612e0dSHugh Dickins unsigned long next; 55691612e0dSHugh Dickins 557b5810039SNick Piggin pgd = pgd_offset(vma->vm_mm, addr); 55891612e0dSHugh Dickins do { 55991612e0dSHugh Dickins next = pgd_addr_end(addr, end); 56091612e0dSHugh Dickins if (pgd_none_or_clear_bad(pgd)) 56191612e0dSHugh Dickins continue; 562dc9aa5b9SChristoph Lameter if (check_pud_range(vma, pgd, addr, next, nodes, 56338e35860SChristoph Lameter flags, private)) 56491612e0dSHugh Dickins return -EIO; 56591612e0dSHugh Dickins } while (pgd++, addr = next, addr != end); 56691612e0dSHugh Dickins return 0; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 569*b24f53a0SLee Schermerhorn #ifdef CONFIG_ARCH_USES_NUMA_PROT_NONE 570*b24f53a0SLee Schermerhorn /* 571*b24f53a0SLee Schermerhorn * Here we search for not shared page mappings (mapcount == 1) and we 572*b24f53a0SLee Schermerhorn * set up the pmd/pte_numa on those mappings so the very next access 573*b24f53a0SLee Schermerhorn * will fire a NUMA hinting page fault. 574*b24f53a0SLee Schermerhorn */ 575*b24f53a0SLee Schermerhorn static int 576*b24f53a0SLee Schermerhorn change_prot_numa_range(struct mm_struct *mm, struct vm_area_struct *vma, 577*b24f53a0SLee Schermerhorn unsigned long address) 578*b24f53a0SLee Schermerhorn { 579*b24f53a0SLee Schermerhorn pgd_t *pgd; 580*b24f53a0SLee Schermerhorn pud_t *pud; 581*b24f53a0SLee Schermerhorn pmd_t *pmd; 582*b24f53a0SLee Schermerhorn pte_t *pte, *_pte; 583*b24f53a0SLee Schermerhorn struct page *page; 584*b24f53a0SLee Schermerhorn unsigned long _address, end; 585*b24f53a0SLee Schermerhorn spinlock_t *ptl; 586*b24f53a0SLee Schermerhorn int ret = 0; 587*b24f53a0SLee Schermerhorn 588*b24f53a0SLee Schermerhorn VM_BUG_ON(address & ~PAGE_MASK); 589*b24f53a0SLee Schermerhorn 590*b24f53a0SLee Schermerhorn pgd = pgd_offset(mm, address); 591*b24f53a0SLee Schermerhorn if (!pgd_present(*pgd)) 592*b24f53a0SLee Schermerhorn goto out; 593*b24f53a0SLee Schermerhorn 594*b24f53a0SLee Schermerhorn pud = pud_offset(pgd, address); 595*b24f53a0SLee Schermerhorn if (!pud_present(*pud)) 596*b24f53a0SLee Schermerhorn goto out; 597*b24f53a0SLee Schermerhorn 598*b24f53a0SLee Schermerhorn pmd = pmd_offset(pud, address); 599*b24f53a0SLee Schermerhorn if (pmd_none(*pmd)) 600*b24f53a0SLee Schermerhorn goto out; 601*b24f53a0SLee Schermerhorn 602*b24f53a0SLee Schermerhorn if (pmd_trans_huge_lock(pmd, vma) == 1) { 603*b24f53a0SLee Schermerhorn int page_nid; 604*b24f53a0SLee Schermerhorn ret = HPAGE_PMD_NR; 605*b24f53a0SLee Schermerhorn 606*b24f53a0SLee Schermerhorn VM_BUG_ON(address & ~HPAGE_PMD_MASK); 607*b24f53a0SLee Schermerhorn 608*b24f53a0SLee Schermerhorn if (pmd_numa(*pmd)) { 609*b24f53a0SLee Schermerhorn spin_unlock(&mm->page_table_lock); 610*b24f53a0SLee Schermerhorn goto out; 611*b24f53a0SLee Schermerhorn } 612*b24f53a0SLee Schermerhorn 613*b24f53a0SLee Schermerhorn page = pmd_page(*pmd); 614*b24f53a0SLee Schermerhorn 615*b24f53a0SLee Schermerhorn /* only check non-shared pages */ 616*b24f53a0SLee Schermerhorn if (page_mapcount(page) != 1) { 617*b24f53a0SLee Schermerhorn spin_unlock(&mm->page_table_lock); 618*b24f53a0SLee Schermerhorn goto out; 619*b24f53a0SLee Schermerhorn } 620*b24f53a0SLee Schermerhorn 621*b24f53a0SLee Schermerhorn page_nid = page_to_nid(page); 622*b24f53a0SLee Schermerhorn 623*b24f53a0SLee Schermerhorn if (pmd_numa(*pmd)) { 624*b24f53a0SLee Schermerhorn spin_unlock(&mm->page_table_lock); 625*b24f53a0SLee Schermerhorn goto out; 626*b24f53a0SLee Schermerhorn } 627*b24f53a0SLee Schermerhorn 628*b24f53a0SLee Schermerhorn set_pmd_at(mm, address, pmd, pmd_mknuma(*pmd)); 629*b24f53a0SLee Schermerhorn ret += HPAGE_PMD_NR; 630*b24f53a0SLee Schermerhorn /* defer TLB flush to lower the overhead */ 631*b24f53a0SLee Schermerhorn spin_unlock(&mm->page_table_lock); 632*b24f53a0SLee Schermerhorn goto out; 633*b24f53a0SLee Schermerhorn } 634*b24f53a0SLee Schermerhorn 635*b24f53a0SLee Schermerhorn if (pmd_trans_unstable(pmd)) 636*b24f53a0SLee Schermerhorn goto out; 637*b24f53a0SLee Schermerhorn VM_BUG_ON(!pmd_present(*pmd)); 638*b24f53a0SLee Schermerhorn 639*b24f53a0SLee Schermerhorn end = min(vma->vm_end, (address + PMD_SIZE) & PMD_MASK); 640*b24f53a0SLee Schermerhorn pte = pte_offset_map_lock(mm, pmd, address, &ptl); 641*b24f53a0SLee Schermerhorn for (_address = address, _pte = pte; _address < end; 642*b24f53a0SLee Schermerhorn _pte++, _address += PAGE_SIZE) { 643*b24f53a0SLee Schermerhorn pte_t pteval = *_pte; 644*b24f53a0SLee Schermerhorn if (!pte_present(pteval)) 645*b24f53a0SLee Schermerhorn continue; 646*b24f53a0SLee Schermerhorn if (pte_numa(pteval)) 647*b24f53a0SLee Schermerhorn continue; 648*b24f53a0SLee Schermerhorn page = vm_normal_page(vma, _address, pteval); 649*b24f53a0SLee Schermerhorn if (unlikely(!page)) 650*b24f53a0SLee Schermerhorn continue; 651*b24f53a0SLee Schermerhorn /* only check non-shared pages */ 652*b24f53a0SLee Schermerhorn if (page_mapcount(page) != 1) 653*b24f53a0SLee Schermerhorn continue; 654*b24f53a0SLee Schermerhorn 655*b24f53a0SLee Schermerhorn set_pte_at(mm, _address, _pte, pte_mknuma(pteval)); 656*b24f53a0SLee Schermerhorn 657*b24f53a0SLee Schermerhorn /* defer TLB flush to lower the overhead */ 658*b24f53a0SLee Schermerhorn ret++; 659*b24f53a0SLee Schermerhorn } 660*b24f53a0SLee Schermerhorn pte_unmap_unlock(pte, ptl); 661*b24f53a0SLee Schermerhorn 662*b24f53a0SLee Schermerhorn if (ret && !pmd_numa(*pmd)) { 663*b24f53a0SLee Schermerhorn spin_lock(&mm->page_table_lock); 664*b24f53a0SLee Schermerhorn set_pmd_at(mm, address, pmd, pmd_mknuma(*pmd)); 665*b24f53a0SLee Schermerhorn spin_unlock(&mm->page_table_lock); 666*b24f53a0SLee Schermerhorn /* defer TLB flush to lower the overhead */ 667*b24f53a0SLee Schermerhorn } 668*b24f53a0SLee Schermerhorn 669*b24f53a0SLee Schermerhorn out: 670*b24f53a0SLee Schermerhorn return ret; 671*b24f53a0SLee Schermerhorn } 672*b24f53a0SLee Schermerhorn 673*b24f53a0SLee Schermerhorn /* Assumes mmap_sem is held */ 674*b24f53a0SLee Schermerhorn void 675*b24f53a0SLee Schermerhorn change_prot_numa(struct vm_area_struct *vma, 676*b24f53a0SLee Schermerhorn unsigned long address, unsigned long end) 677*b24f53a0SLee Schermerhorn { 678*b24f53a0SLee Schermerhorn struct mm_struct *mm = vma->vm_mm; 679*b24f53a0SLee Schermerhorn int progress = 0; 680*b24f53a0SLee Schermerhorn 681*b24f53a0SLee Schermerhorn while (address < end) { 682*b24f53a0SLee Schermerhorn VM_BUG_ON(address < vma->vm_start || 683*b24f53a0SLee Schermerhorn address + PAGE_SIZE > vma->vm_end); 684*b24f53a0SLee Schermerhorn 685*b24f53a0SLee Schermerhorn progress += change_prot_numa_range(mm, vma, address); 686*b24f53a0SLee Schermerhorn address = (address + PMD_SIZE) & PMD_MASK; 687*b24f53a0SLee Schermerhorn } 688*b24f53a0SLee Schermerhorn 689*b24f53a0SLee Schermerhorn /* 690*b24f53a0SLee Schermerhorn * Flush the TLB for the mm to start the NUMA hinting 691*b24f53a0SLee Schermerhorn * page faults after we finish scanning this vma part 692*b24f53a0SLee Schermerhorn * if there were any PTE updates 693*b24f53a0SLee Schermerhorn */ 694*b24f53a0SLee Schermerhorn if (progress) { 695*b24f53a0SLee Schermerhorn mmu_notifier_invalidate_range_start(vma->vm_mm, address, end); 696*b24f53a0SLee Schermerhorn flush_tlb_range(vma, address, end); 697*b24f53a0SLee Schermerhorn mmu_notifier_invalidate_range_end(vma->vm_mm, address, end); 698*b24f53a0SLee Schermerhorn } 699*b24f53a0SLee Schermerhorn } 700*b24f53a0SLee Schermerhorn #else 701*b24f53a0SLee Schermerhorn static unsigned long change_prot_numa(struct vm_area_struct *vma, 702*b24f53a0SLee Schermerhorn unsigned long addr, unsigned long end) 703*b24f53a0SLee Schermerhorn { 704*b24f53a0SLee Schermerhorn return 0; 705*b24f53a0SLee Schermerhorn } 706*b24f53a0SLee Schermerhorn #endif /* CONFIG_ARCH_USES_NUMA_PROT_NONE */ 707*b24f53a0SLee Schermerhorn 708dc9aa5b9SChristoph Lameter /* 709dc9aa5b9SChristoph Lameter * Check if all pages in a range are on a set of nodes. 710dc9aa5b9SChristoph Lameter * If pagelist != NULL then isolate pages from the LRU and 711dc9aa5b9SChristoph Lameter * put them on the pagelist. 712dc9aa5b9SChristoph Lameter */ 7131da177e4SLinus Torvalds static struct vm_area_struct * 7141da177e4SLinus Torvalds check_range(struct mm_struct *mm, unsigned long start, unsigned long end, 71538e35860SChristoph Lameter const nodemask_t *nodes, unsigned long flags, void *private) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds int err; 7181da177e4SLinus Torvalds struct vm_area_struct *first, *vma, *prev; 7191da177e4SLinus Torvalds 720053837fcSNick Piggin 7211da177e4SLinus Torvalds first = find_vma(mm, start); 7221da177e4SLinus Torvalds if (!first) 7231da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 7241da177e4SLinus Torvalds prev = NULL; 7251da177e4SLinus Torvalds for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) { 7265b952b3cSAndi Kleen unsigned long endvma = vma->vm_end; 727dc9aa5b9SChristoph Lameter 7285b952b3cSAndi Kleen if (endvma > end) 7295b952b3cSAndi Kleen endvma = end; 7305b952b3cSAndi Kleen if (vma->vm_start > start) 7315b952b3cSAndi Kleen start = vma->vm_start; 732*b24f53a0SLee Schermerhorn 733*b24f53a0SLee Schermerhorn if (!(flags & MPOL_MF_DISCONTIG_OK)) { 734*b24f53a0SLee Schermerhorn if (!vma->vm_next && vma->vm_end < end) 735*b24f53a0SLee Schermerhorn return ERR_PTR(-EFAULT); 736*b24f53a0SLee Schermerhorn if (prev && prev->vm_end < vma->vm_start) 737*b24f53a0SLee Schermerhorn return ERR_PTR(-EFAULT); 738*b24f53a0SLee Schermerhorn } 739*b24f53a0SLee Schermerhorn 740*b24f53a0SLee Schermerhorn if (is_vm_hugetlb_page(vma)) 741*b24f53a0SLee Schermerhorn goto next; 742*b24f53a0SLee Schermerhorn 743*b24f53a0SLee Schermerhorn if (flags & MPOL_MF_LAZY) { 744*b24f53a0SLee Schermerhorn change_prot_numa(vma, start, endvma); 745*b24f53a0SLee Schermerhorn goto next; 746*b24f53a0SLee Schermerhorn } 747*b24f53a0SLee Schermerhorn 748*b24f53a0SLee Schermerhorn if ((flags & MPOL_MF_STRICT) || 749*b24f53a0SLee Schermerhorn ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) && 750*b24f53a0SLee Schermerhorn vma_migratable(vma))) { 751*b24f53a0SLee Schermerhorn 752dc9aa5b9SChristoph Lameter err = check_pgd_range(vma, start, endvma, nodes, 75338e35860SChristoph Lameter flags, private); 7541da177e4SLinus Torvalds if (err) { 7551da177e4SLinus Torvalds first = ERR_PTR(err); 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds } 759*b24f53a0SLee Schermerhorn next: 7601da177e4SLinus Torvalds prev = vma; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds return first; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 765869833f2SKOSAKI Motohiro /* 766869833f2SKOSAKI Motohiro * Apply policy to a single VMA 767869833f2SKOSAKI Motohiro * This must be called with the mmap_sem held for writing. 768869833f2SKOSAKI Motohiro */ 769869833f2SKOSAKI Motohiro static int vma_replace_policy(struct vm_area_struct *vma, 770869833f2SKOSAKI Motohiro struct mempolicy *pol) 7718d34694cSKOSAKI Motohiro { 772869833f2SKOSAKI Motohiro int err; 773869833f2SKOSAKI Motohiro struct mempolicy *old; 774869833f2SKOSAKI Motohiro struct mempolicy *new; 7758d34694cSKOSAKI Motohiro 7768d34694cSKOSAKI Motohiro pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n", 7778d34694cSKOSAKI Motohiro vma->vm_start, vma->vm_end, vma->vm_pgoff, 7788d34694cSKOSAKI Motohiro vma->vm_ops, vma->vm_file, 7798d34694cSKOSAKI Motohiro vma->vm_ops ? vma->vm_ops->set_policy : NULL); 7808d34694cSKOSAKI Motohiro 781869833f2SKOSAKI Motohiro new = mpol_dup(pol); 782869833f2SKOSAKI Motohiro if (IS_ERR(new)) 783869833f2SKOSAKI Motohiro return PTR_ERR(new); 784869833f2SKOSAKI Motohiro 785869833f2SKOSAKI Motohiro if (vma->vm_ops && vma->vm_ops->set_policy) { 7868d34694cSKOSAKI Motohiro err = vma->vm_ops->set_policy(vma, new); 787869833f2SKOSAKI Motohiro if (err) 788869833f2SKOSAKI Motohiro goto err_out; 7898d34694cSKOSAKI Motohiro } 790869833f2SKOSAKI Motohiro 791869833f2SKOSAKI Motohiro old = vma->vm_policy; 792869833f2SKOSAKI Motohiro vma->vm_policy = new; /* protected by mmap_sem */ 793869833f2SKOSAKI Motohiro mpol_put(old); 794869833f2SKOSAKI Motohiro 795869833f2SKOSAKI Motohiro return 0; 796869833f2SKOSAKI Motohiro err_out: 797869833f2SKOSAKI Motohiro mpol_put(new); 7988d34694cSKOSAKI Motohiro return err; 7998d34694cSKOSAKI Motohiro } 8008d34694cSKOSAKI Motohiro 8011da177e4SLinus Torvalds /* Step 2: apply policy to a range and do splits. */ 8029d8cebd4SKOSAKI Motohiro static int mbind_range(struct mm_struct *mm, unsigned long start, 8039d8cebd4SKOSAKI Motohiro unsigned long end, struct mempolicy *new_pol) 8041da177e4SLinus Torvalds { 8051da177e4SLinus Torvalds struct vm_area_struct *next; 8069d8cebd4SKOSAKI Motohiro struct vm_area_struct *prev; 8079d8cebd4SKOSAKI Motohiro struct vm_area_struct *vma; 8089d8cebd4SKOSAKI Motohiro int err = 0; 809e26a5114SKOSAKI Motohiro pgoff_t pgoff; 8109d8cebd4SKOSAKI Motohiro unsigned long vmstart; 8119d8cebd4SKOSAKI Motohiro unsigned long vmend; 8121da177e4SLinus Torvalds 813097d5910SLinus Torvalds vma = find_vma(mm, start); 8149d8cebd4SKOSAKI Motohiro if (!vma || vma->vm_start > start) 8159d8cebd4SKOSAKI Motohiro return -EFAULT; 8169d8cebd4SKOSAKI Motohiro 817097d5910SLinus Torvalds prev = vma->vm_prev; 818e26a5114SKOSAKI Motohiro if (start > vma->vm_start) 819e26a5114SKOSAKI Motohiro prev = vma; 820e26a5114SKOSAKI Motohiro 8219d8cebd4SKOSAKI Motohiro for (; vma && vma->vm_start < end; prev = vma, vma = next) { 8221da177e4SLinus Torvalds next = vma->vm_next; 8239d8cebd4SKOSAKI Motohiro vmstart = max(start, vma->vm_start); 8249d8cebd4SKOSAKI Motohiro vmend = min(end, vma->vm_end); 8259d8cebd4SKOSAKI Motohiro 826e26a5114SKOSAKI Motohiro if (mpol_equal(vma_policy(vma), new_pol)) 827e26a5114SKOSAKI Motohiro continue; 828e26a5114SKOSAKI Motohiro 829e26a5114SKOSAKI Motohiro pgoff = vma->vm_pgoff + 830e26a5114SKOSAKI Motohiro ((vmstart - vma->vm_start) >> PAGE_SHIFT); 8319d8cebd4SKOSAKI Motohiro prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags, 832e26a5114SKOSAKI Motohiro vma->anon_vma, vma->vm_file, pgoff, 8338aacc9f5SCaspar Zhang new_pol); 8349d8cebd4SKOSAKI Motohiro if (prev) { 8359d8cebd4SKOSAKI Motohiro vma = prev; 8369d8cebd4SKOSAKI Motohiro next = vma->vm_next; 8379d8cebd4SKOSAKI Motohiro continue; 8381da177e4SLinus Torvalds } 8399d8cebd4SKOSAKI Motohiro if (vma->vm_start != vmstart) { 8409d8cebd4SKOSAKI Motohiro err = split_vma(vma->vm_mm, vma, vmstart, 1); 8419d8cebd4SKOSAKI Motohiro if (err) 8429d8cebd4SKOSAKI Motohiro goto out; 8439d8cebd4SKOSAKI Motohiro } 8449d8cebd4SKOSAKI Motohiro if (vma->vm_end != vmend) { 8459d8cebd4SKOSAKI Motohiro err = split_vma(vma->vm_mm, vma, vmend, 0); 8469d8cebd4SKOSAKI Motohiro if (err) 8479d8cebd4SKOSAKI Motohiro goto out; 8489d8cebd4SKOSAKI Motohiro } 849869833f2SKOSAKI Motohiro err = vma_replace_policy(vma, new_pol); 8509d8cebd4SKOSAKI Motohiro if (err) 8519d8cebd4SKOSAKI Motohiro goto out; 8529d8cebd4SKOSAKI Motohiro } 8539d8cebd4SKOSAKI Motohiro 8549d8cebd4SKOSAKI Motohiro out: 8551da177e4SLinus Torvalds return err; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 858c61afb18SPaul Jackson /* 859c61afb18SPaul Jackson * Update task->flags PF_MEMPOLICY bit: set iff non-default 860c61afb18SPaul Jackson * mempolicy. Allows more rapid checking of this (combined perhaps 861c61afb18SPaul Jackson * with other PF_* flag bits) on memory allocation hot code paths. 862c61afb18SPaul Jackson * 863c61afb18SPaul Jackson * If called from outside this file, the task 'p' should -only- be 864c61afb18SPaul Jackson * a newly forked child not yet visible on the task list, because 865c61afb18SPaul Jackson * manipulating the task flags of a visible task is not safe. 866c61afb18SPaul Jackson * 867c61afb18SPaul Jackson * The above limitation is why this routine has the funny name 868c61afb18SPaul Jackson * mpol_fix_fork_child_flag(). 869c61afb18SPaul Jackson * 870c61afb18SPaul Jackson * It is also safe to call this with a task pointer of current, 871c61afb18SPaul Jackson * which the static wrapper mpol_set_task_struct_flag() does, 872c61afb18SPaul Jackson * for use within this file. 873c61afb18SPaul Jackson */ 874c61afb18SPaul Jackson 875c61afb18SPaul Jackson void mpol_fix_fork_child_flag(struct task_struct *p) 876c61afb18SPaul Jackson { 877c61afb18SPaul Jackson if (p->mempolicy) 878c61afb18SPaul Jackson p->flags |= PF_MEMPOLICY; 879c61afb18SPaul Jackson else 880c61afb18SPaul Jackson p->flags &= ~PF_MEMPOLICY; 881c61afb18SPaul Jackson } 882c61afb18SPaul Jackson 883c61afb18SPaul Jackson static void mpol_set_task_struct_flag(void) 884c61afb18SPaul Jackson { 885c61afb18SPaul Jackson mpol_fix_fork_child_flag(current); 886c61afb18SPaul Jackson } 887c61afb18SPaul Jackson 8881da177e4SLinus Torvalds /* Set the process memory policy */ 889028fec41SDavid Rientjes static long do_set_mempolicy(unsigned short mode, unsigned short flags, 890028fec41SDavid Rientjes nodemask_t *nodes) 8911da177e4SLinus Torvalds { 89258568d2aSMiao Xie struct mempolicy *new, *old; 893f4e53d91SLee Schermerhorn struct mm_struct *mm = current->mm; 8944bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH(scratch); 89558568d2aSMiao Xie int ret; 8961da177e4SLinus Torvalds 8974bfc4495SKAMEZAWA Hiroyuki if (!scratch) 8984bfc4495SKAMEZAWA Hiroyuki return -ENOMEM; 899f4e53d91SLee Schermerhorn 9004bfc4495SKAMEZAWA Hiroyuki new = mpol_new(mode, flags, nodes); 9014bfc4495SKAMEZAWA Hiroyuki if (IS_ERR(new)) { 9024bfc4495SKAMEZAWA Hiroyuki ret = PTR_ERR(new); 9034bfc4495SKAMEZAWA Hiroyuki goto out; 9044bfc4495SKAMEZAWA Hiroyuki } 905f4e53d91SLee Schermerhorn /* 906f4e53d91SLee Schermerhorn * prevent changing our mempolicy while show_numa_maps() 907f4e53d91SLee Schermerhorn * is using it. 908f4e53d91SLee Schermerhorn * Note: do_set_mempolicy() can be called at init time 909f4e53d91SLee Schermerhorn * with no 'mm'. 910f4e53d91SLee Schermerhorn */ 911f4e53d91SLee Schermerhorn if (mm) 912f4e53d91SLee Schermerhorn down_write(&mm->mmap_sem); 91358568d2aSMiao Xie task_lock(current); 9144bfc4495SKAMEZAWA Hiroyuki ret = mpol_set_nodemask(new, nodes, scratch); 91558568d2aSMiao Xie if (ret) { 91658568d2aSMiao Xie task_unlock(current); 91758568d2aSMiao Xie if (mm) 91858568d2aSMiao Xie up_write(&mm->mmap_sem); 91958568d2aSMiao Xie mpol_put(new); 9204bfc4495SKAMEZAWA Hiroyuki goto out; 92158568d2aSMiao Xie } 92258568d2aSMiao Xie old = current->mempolicy; 9231da177e4SLinus Torvalds current->mempolicy = new; 924c61afb18SPaul Jackson mpol_set_task_struct_flag(); 92545c4745aSLee Schermerhorn if (new && new->mode == MPOL_INTERLEAVE && 926f5b087b5SDavid Rientjes nodes_weight(new->v.nodes)) 927dfcd3c0dSAndi Kleen current->il_next = first_node(new->v.nodes); 92858568d2aSMiao Xie task_unlock(current); 929f4e53d91SLee Schermerhorn if (mm) 930f4e53d91SLee Schermerhorn up_write(&mm->mmap_sem); 931f4e53d91SLee Schermerhorn 93258568d2aSMiao Xie mpol_put(old); 9334bfc4495SKAMEZAWA Hiroyuki ret = 0; 9344bfc4495SKAMEZAWA Hiroyuki out: 9354bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH_FREE(scratch); 9364bfc4495SKAMEZAWA Hiroyuki return ret; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 939bea904d5SLee Schermerhorn /* 940bea904d5SLee Schermerhorn * Return nodemask for policy for get_mempolicy() query 94158568d2aSMiao Xie * 94258568d2aSMiao Xie * Called with task's alloc_lock held 943bea904d5SLee Schermerhorn */ 944bea904d5SLee Schermerhorn static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes) 9451da177e4SLinus Torvalds { 946dfcd3c0dSAndi Kleen nodes_clear(*nodes); 947bea904d5SLee Schermerhorn if (p == &default_policy) 948bea904d5SLee Schermerhorn return; 949bea904d5SLee Schermerhorn 95045c4745aSLee Schermerhorn switch (p->mode) { 95119770b32SMel Gorman case MPOL_BIND: 95219770b32SMel Gorman /* Fall through */ 9531da177e4SLinus Torvalds case MPOL_INTERLEAVE: 954dfcd3c0dSAndi Kleen *nodes = p->v.nodes; 9551da177e4SLinus Torvalds break; 9561da177e4SLinus Torvalds case MPOL_PREFERRED: 957fc36b8d3SLee Schermerhorn if (!(p->flags & MPOL_F_LOCAL)) 958dfcd3c0dSAndi Kleen node_set(p->v.preferred_node, *nodes); 95953f2556bSLee Schermerhorn /* else return empty node mask for local allocation */ 9601da177e4SLinus Torvalds break; 9611da177e4SLinus Torvalds default: 9621da177e4SLinus Torvalds BUG(); 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds static int lookup_node(struct mm_struct *mm, unsigned long addr) 9671da177e4SLinus Torvalds { 9681da177e4SLinus Torvalds struct page *p; 9691da177e4SLinus Torvalds int err; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL); 9721da177e4SLinus Torvalds if (err >= 0) { 9731da177e4SLinus Torvalds err = page_to_nid(p); 9741da177e4SLinus Torvalds put_page(p); 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds return err; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* Retrieve NUMA policy */ 980dbcb0f19SAdrian Bunk static long do_get_mempolicy(int *policy, nodemask_t *nmask, 9811da177e4SLinus Torvalds unsigned long addr, unsigned long flags) 9821da177e4SLinus Torvalds { 9838bccd85fSChristoph Lameter int err; 9841da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 9851da177e4SLinus Torvalds struct vm_area_struct *vma = NULL; 9861da177e4SLinus Torvalds struct mempolicy *pol = current->mempolicy; 9871da177e4SLinus Torvalds 988754af6f5SLee Schermerhorn if (flags & 989754af6f5SLee Schermerhorn ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED)) 9901da177e4SLinus Torvalds return -EINVAL; 991754af6f5SLee Schermerhorn 992754af6f5SLee Schermerhorn if (flags & MPOL_F_MEMS_ALLOWED) { 993754af6f5SLee Schermerhorn if (flags & (MPOL_F_NODE|MPOL_F_ADDR)) 994754af6f5SLee Schermerhorn return -EINVAL; 995754af6f5SLee Schermerhorn *policy = 0; /* just so it's initialized */ 99658568d2aSMiao Xie task_lock(current); 997754af6f5SLee Schermerhorn *nmask = cpuset_current_mems_allowed; 99858568d2aSMiao Xie task_unlock(current); 999754af6f5SLee Schermerhorn return 0; 1000754af6f5SLee Schermerhorn } 1001754af6f5SLee Schermerhorn 10021da177e4SLinus Torvalds if (flags & MPOL_F_ADDR) { 1003bea904d5SLee Schermerhorn /* 1004bea904d5SLee Schermerhorn * Do NOT fall back to task policy if the 1005bea904d5SLee Schermerhorn * vma/shared policy at addr is NULL. We 1006bea904d5SLee Schermerhorn * want to return MPOL_DEFAULT in this case. 1007bea904d5SLee Schermerhorn */ 10081da177e4SLinus Torvalds down_read(&mm->mmap_sem); 10091da177e4SLinus Torvalds vma = find_vma_intersection(mm, addr, addr+1); 10101da177e4SLinus Torvalds if (!vma) { 10111da177e4SLinus Torvalds up_read(&mm->mmap_sem); 10121da177e4SLinus Torvalds return -EFAULT; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds if (vma->vm_ops && vma->vm_ops->get_policy) 10151da177e4SLinus Torvalds pol = vma->vm_ops->get_policy(vma, addr); 10161da177e4SLinus Torvalds else 10171da177e4SLinus Torvalds pol = vma->vm_policy; 10181da177e4SLinus Torvalds } else if (addr) 10191da177e4SLinus Torvalds return -EINVAL; 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds if (!pol) 1022bea904d5SLee Schermerhorn pol = &default_policy; /* indicates default behavior */ 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds if (flags & MPOL_F_NODE) { 10251da177e4SLinus Torvalds if (flags & MPOL_F_ADDR) { 10261da177e4SLinus Torvalds err = lookup_node(mm, addr); 10271da177e4SLinus Torvalds if (err < 0) 10281da177e4SLinus Torvalds goto out; 10298bccd85fSChristoph Lameter *policy = err; 10301da177e4SLinus Torvalds } else if (pol == current->mempolicy && 103145c4745aSLee Schermerhorn pol->mode == MPOL_INTERLEAVE) { 10328bccd85fSChristoph Lameter *policy = current->il_next; 10331da177e4SLinus Torvalds } else { 10341da177e4SLinus Torvalds err = -EINVAL; 10351da177e4SLinus Torvalds goto out; 10361da177e4SLinus Torvalds } 1037bea904d5SLee Schermerhorn } else { 1038bea904d5SLee Schermerhorn *policy = pol == &default_policy ? MPOL_DEFAULT : 1039bea904d5SLee Schermerhorn pol->mode; 1040d79df630SDavid Rientjes /* 1041d79df630SDavid Rientjes * Internal mempolicy flags must be masked off before exposing 1042d79df630SDavid Rientjes * the policy to userspace. 1043d79df630SDavid Rientjes */ 1044d79df630SDavid Rientjes *policy |= (pol->flags & MPOL_MODE_FLAGS); 1045bea904d5SLee Schermerhorn } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds if (vma) { 10481da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 10491da177e4SLinus Torvalds vma = NULL; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds err = 0; 105358568d2aSMiao Xie if (nmask) { 1054c6b6ef8bSLee Schermerhorn if (mpol_store_user_nodemask(pol)) { 1055c6b6ef8bSLee Schermerhorn *nmask = pol->w.user_nodemask; 1056c6b6ef8bSLee Schermerhorn } else { 105758568d2aSMiao Xie task_lock(current); 1058bea904d5SLee Schermerhorn get_policy_nodemask(pol, nmask); 105958568d2aSMiao Xie task_unlock(current); 106058568d2aSMiao Xie } 1061c6b6ef8bSLee Schermerhorn } 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds out: 106452cd3b07SLee Schermerhorn mpol_cond_put(pol); 10651da177e4SLinus Torvalds if (vma) 10661da177e4SLinus Torvalds up_read(¤t->mm->mmap_sem); 10671da177e4SLinus Torvalds return err; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 1070b20a3503SChristoph Lameter #ifdef CONFIG_MIGRATION 10718bccd85fSChristoph Lameter /* 10726ce3c4c0SChristoph Lameter * page migration 10736ce3c4c0SChristoph Lameter */ 1074fc301289SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist, 1075fc301289SChristoph Lameter unsigned long flags) 10766ce3c4c0SChristoph Lameter { 10776ce3c4c0SChristoph Lameter /* 1078fc301289SChristoph Lameter * Avoid migrating a page that is shared with others. 10796ce3c4c0SChristoph Lameter */ 108062695a84SNick Piggin if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) { 108162695a84SNick Piggin if (!isolate_lru_page(page)) { 108262695a84SNick Piggin list_add_tail(&page->lru, pagelist); 10836d9c285aSKOSAKI Motohiro inc_zone_page_state(page, NR_ISOLATED_ANON + 10846d9c285aSKOSAKI Motohiro page_is_file_cache(page)); 108562695a84SNick Piggin } 108662695a84SNick Piggin } 10876ce3c4c0SChristoph Lameter } 10886ce3c4c0SChristoph Lameter 1089742755a1SChristoph Lameter static struct page *new_node_page(struct page *page, unsigned long node, int **x) 109095a402c3SChristoph Lameter { 10916484eb3eSMel Gorman return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0); 109295a402c3SChristoph Lameter } 109395a402c3SChristoph Lameter 10946ce3c4c0SChristoph Lameter /* 10957e2ab150SChristoph Lameter * Migrate pages from one node to a target node. 10967e2ab150SChristoph Lameter * Returns error or the number of pages not migrated. 10977e2ab150SChristoph Lameter */ 1098dbcb0f19SAdrian Bunk static int migrate_to_node(struct mm_struct *mm, int source, int dest, 1099dbcb0f19SAdrian Bunk int flags) 11007e2ab150SChristoph Lameter { 11017e2ab150SChristoph Lameter nodemask_t nmask; 11027e2ab150SChristoph Lameter LIST_HEAD(pagelist); 11037e2ab150SChristoph Lameter int err = 0; 11047e2ab150SChristoph Lameter 11057e2ab150SChristoph Lameter nodes_clear(nmask); 11067e2ab150SChristoph Lameter node_set(source, nmask); 11077e2ab150SChristoph Lameter 110808270807SMinchan Kim /* 110908270807SMinchan Kim * This does not "check" the range but isolates all pages that 111008270807SMinchan Kim * need migration. Between passing in the full user address 111108270807SMinchan Kim * space range and MPOL_MF_DISCONTIG_OK, this call can not fail. 111208270807SMinchan Kim */ 111308270807SMinchan Kim VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))); 111408270807SMinchan Kim check_range(mm, mm->mmap->vm_start, mm->task_size, &nmask, 11157e2ab150SChristoph Lameter flags | MPOL_MF_DISCONTIG_OK, &pagelist); 11167e2ab150SChristoph Lameter 1117cf608ac1SMinchan Kim if (!list_empty(&pagelist)) { 11187f0f2496SMel Gorman err = migrate_pages(&pagelist, new_node_page, dest, 11197b2a2d4aSMel Gorman false, MIGRATE_SYNC, 11207b2a2d4aSMel Gorman MR_SYSCALL); 1121cf608ac1SMinchan Kim if (err) 1122cf608ac1SMinchan Kim putback_lru_pages(&pagelist); 1123cf608ac1SMinchan Kim } 112495a402c3SChristoph Lameter 11257e2ab150SChristoph Lameter return err; 11267e2ab150SChristoph Lameter } 11277e2ab150SChristoph Lameter 11287e2ab150SChristoph Lameter /* 11297e2ab150SChristoph Lameter * Move pages between the two nodesets so as to preserve the physical 11307e2ab150SChristoph Lameter * layout as much as possible. 113139743889SChristoph Lameter * 113239743889SChristoph Lameter * Returns the number of page that could not be moved. 113339743889SChristoph Lameter */ 11340ce72d4fSAndrew Morton int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, 11350ce72d4fSAndrew Morton const nodemask_t *to, int flags) 113639743889SChristoph Lameter { 11377e2ab150SChristoph Lameter int busy = 0; 11380aedadf9SChristoph Lameter int err; 11397e2ab150SChristoph Lameter nodemask_t tmp; 114039743889SChristoph Lameter 11410aedadf9SChristoph Lameter err = migrate_prep(); 11420aedadf9SChristoph Lameter if (err) 11430aedadf9SChristoph Lameter return err; 11440aedadf9SChristoph Lameter 114539743889SChristoph Lameter down_read(&mm->mmap_sem); 1146d4984711SChristoph Lameter 11470ce72d4fSAndrew Morton err = migrate_vmas(mm, from, to, flags); 11487b2259b3SChristoph Lameter if (err) 11497b2259b3SChristoph Lameter goto out; 11507b2259b3SChristoph Lameter 11517e2ab150SChristoph Lameter /* 11527e2ab150SChristoph Lameter * Find a 'source' bit set in 'tmp' whose corresponding 'dest' 11537e2ab150SChristoph Lameter * bit in 'to' is not also set in 'tmp'. Clear the found 'source' 11547e2ab150SChristoph Lameter * bit in 'tmp', and return that <source, dest> pair for migration. 11557e2ab150SChristoph Lameter * The pair of nodemasks 'to' and 'from' define the map. 11567e2ab150SChristoph Lameter * 11577e2ab150SChristoph Lameter * If no pair of bits is found that way, fallback to picking some 11587e2ab150SChristoph Lameter * pair of 'source' and 'dest' bits that are not the same. If the 11597e2ab150SChristoph Lameter * 'source' and 'dest' bits are the same, this represents a node 11607e2ab150SChristoph Lameter * that will be migrating to itself, so no pages need move. 11617e2ab150SChristoph Lameter * 11627e2ab150SChristoph Lameter * If no bits are left in 'tmp', or if all remaining bits left 11637e2ab150SChristoph Lameter * in 'tmp' correspond to the same bit in 'to', return false 11647e2ab150SChristoph Lameter * (nothing left to migrate). 11657e2ab150SChristoph Lameter * 11667e2ab150SChristoph Lameter * This lets us pick a pair of nodes to migrate between, such that 11677e2ab150SChristoph Lameter * if possible the dest node is not already occupied by some other 11687e2ab150SChristoph Lameter * source node, minimizing the risk of overloading the memory on a 11697e2ab150SChristoph Lameter * node that would happen if we migrated incoming memory to a node 11707e2ab150SChristoph Lameter * before migrating outgoing memory source that same node. 11717e2ab150SChristoph Lameter * 11727e2ab150SChristoph Lameter * A single scan of tmp is sufficient. As we go, we remember the 11737e2ab150SChristoph Lameter * most recent <s, d> pair that moved (s != d). If we find a pair 11747e2ab150SChristoph Lameter * that not only moved, but what's better, moved to an empty slot 11757e2ab150SChristoph Lameter * (d is not set in tmp), then we break out then, with that pair. 1176ae0e47f0SJustin P. Mattock * Otherwise when we finish scanning from_tmp, we at least have the 11777e2ab150SChristoph Lameter * most recent <s, d> pair that moved. If we get all the way through 11787e2ab150SChristoph Lameter * the scan of tmp without finding any node that moved, much less 11797e2ab150SChristoph Lameter * moved to an empty node, then there is nothing left worth migrating. 11807e2ab150SChristoph Lameter */ 11817e2ab150SChristoph Lameter 11820ce72d4fSAndrew Morton tmp = *from; 11837e2ab150SChristoph Lameter while (!nodes_empty(tmp)) { 11847e2ab150SChristoph Lameter int s,d; 11857e2ab150SChristoph Lameter int source = -1; 11867e2ab150SChristoph Lameter int dest = 0; 11877e2ab150SChristoph Lameter 11887e2ab150SChristoph Lameter for_each_node_mask(s, tmp) { 11894a5b18ccSLarry Woodman 11904a5b18ccSLarry Woodman /* 11914a5b18ccSLarry Woodman * do_migrate_pages() tries to maintain the relative 11924a5b18ccSLarry Woodman * node relationship of the pages established between 11934a5b18ccSLarry Woodman * threads and memory areas. 11944a5b18ccSLarry Woodman * 11954a5b18ccSLarry Woodman * However if the number of source nodes is not equal to 11964a5b18ccSLarry Woodman * the number of destination nodes we can not preserve 11974a5b18ccSLarry Woodman * this node relative relationship. In that case, skip 11984a5b18ccSLarry Woodman * copying memory from a node that is in the destination 11994a5b18ccSLarry Woodman * mask. 12004a5b18ccSLarry Woodman * 12014a5b18ccSLarry Woodman * Example: [2,3,4] -> [3,4,5] moves everything. 12024a5b18ccSLarry Woodman * [0-7] - > [3,4,5] moves only 0,1,2,6,7. 12034a5b18ccSLarry Woodman */ 12044a5b18ccSLarry Woodman 12050ce72d4fSAndrew Morton if ((nodes_weight(*from) != nodes_weight(*to)) && 12060ce72d4fSAndrew Morton (node_isset(s, *to))) 12074a5b18ccSLarry Woodman continue; 12084a5b18ccSLarry Woodman 12090ce72d4fSAndrew Morton d = node_remap(s, *from, *to); 12107e2ab150SChristoph Lameter if (s == d) 12117e2ab150SChristoph Lameter continue; 12127e2ab150SChristoph Lameter 12137e2ab150SChristoph Lameter source = s; /* Node moved. Memorize */ 12147e2ab150SChristoph Lameter dest = d; 12157e2ab150SChristoph Lameter 12167e2ab150SChristoph Lameter /* dest not in remaining from nodes? */ 12177e2ab150SChristoph Lameter if (!node_isset(dest, tmp)) 12187e2ab150SChristoph Lameter break; 12197e2ab150SChristoph Lameter } 12207e2ab150SChristoph Lameter if (source == -1) 12217e2ab150SChristoph Lameter break; 12227e2ab150SChristoph Lameter 12237e2ab150SChristoph Lameter node_clear(source, tmp); 12247e2ab150SChristoph Lameter err = migrate_to_node(mm, source, dest, flags); 12257e2ab150SChristoph Lameter if (err > 0) 12267e2ab150SChristoph Lameter busy += err; 12277e2ab150SChristoph Lameter if (err < 0) 12287e2ab150SChristoph Lameter break; 122939743889SChristoph Lameter } 12307b2259b3SChristoph Lameter out: 123139743889SChristoph Lameter up_read(&mm->mmap_sem); 12327e2ab150SChristoph Lameter if (err < 0) 12337e2ab150SChristoph Lameter return err; 12347e2ab150SChristoph Lameter return busy; 1235b20a3503SChristoph Lameter 123639743889SChristoph Lameter } 123739743889SChristoph Lameter 12383ad33b24SLee Schermerhorn /* 12393ad33b24SLee Schermerhorn * Allocate a new page for page migration based on vma policy. 12403ad33b24SLee Schermerhorn * Start assuming that page is mapped by vma pointed to by @private. 12413ad33b24SLee Schermerhorn * Search forward from there, if not. N.B., this assumes that the 12423ad33b24SLee Schermerhorn * list of pages handed to migrate_pages()--which is how we get here-- 12433ad33b24SLee Schermerhorn * is in virtual address order. 12443ad33b24SLee Schermerhorn */ 1245742755a1SChristoph Lameter static struct page *new_vma_page(struct page *page, unsigned long private, int **x) 124695a402c3SChristoph Lameter { 124795a402c3SChristoph Lameter struct vm_area_struct *vma = (struct vm_area_struct *)private; 12483ad33b24SLee Schermerhorn unsigned long uninitialized_var(address); 124995a402c3SChristoph Lameter 12503ad33b24SLee Schermerhorn while (vma) { 12513ad33b24SLee Schermerhorn address = page_address_in_vma(page, vma); 12523ad33b24SLee Schermerhorn if (address != -EFAULT) 12533ad33b24SLee Schermerhorn break; 12543ad33b24SLee Schermerhorn vma = vma->vm_next; 12553ad33b24SLee Schermerhorn } 12563ad33b24SLee Schermerhorn 12573ad33b24SLee Schermerhorn /* 12583ad33b24SLee Schermerhorn * if !vma, alloc_page_vma() will use task or system default policy 12593ad33b24SLee Schermerhorn */ 12603ad33b24SLee Schermerhorn return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 126195a402c3SChristoph Lameter } 1262b20a3503SChristoph Lameter #else 1263b20a3503SChristoph Lameter 1264b20a3503SChristoph Lameter static void migrate_page_add(struct page *page, struct list_head *pagelist, 1265b20a3503SChristoph Lameter unsigned long flags) 1266b20a3503SChristoph Lameter { 1267b20a3503SChristoph Lameter } 1268b20a3503SChristoph Lameter 12690ce72d4fSAndrew Morton int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, 12700ce72d4fSAndrew Morton const nodemask_t *to, int flags) 1271b20a3503SChristoph Lameter { 1272b20a3503SChristoph Lameter return -ENOSYS; 1273b20a3503SChristoph Lameter } 127495a402c3SChristoph Lameter 127569939749SKeith Owens static struct page *new_vma_page(struct page *page, unsigned long private, int **x) 127695a402c3SChristoph Lameter { 127795a402c3SChristoph Lameter return NULL; 127895a402c3SChristoph Lameter } 1279b20a3503SChristoph Lameter #endif 1280b20a3503SChristoph Lameter 1281dbcb0f19SAdrian Bunk static long do_mbind(unsigned long start, unsigned long len, 1282028fec41SDavid Rientjes unsigned short mode, unsigned short mode_flags, 1283028fec41SDavid Rientjes nodemask_t *nmask, unsigned long flags) 12846ce3c4c0SChristoph Lameter { 12856ce3c4c0SChristoph Lameter struct vm_area_struct *vma; 12866ce3c4c0SChristoph Lameter struct mm_struct *mm = current->mm; 12876ce3c4c0SChristoph Lameter struct mempolicy *new; 12886ce3c4c0SChristoph Lameter unsigned long end; 12896ce3c4c0SChristoph Lameter int err; 12906ce3c4c0SChristoph Lameter LIST_HEAD(pagelist); 12916ce3c4c0SChristoph Lameter 1292*b24f53a0SLee Schermerhorn if (flags & ~(unsigned long)MPOL_MF_VALID) 12936ce3c4c0SChristoph Lameter return -EINVAL; 129474c00241SChristoph Lameter if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) 12956ce3c4c0SChristoph Lameter return -EPERM; 12966ce3c4c0SChristoph Lameter 12976ce3c4c0SChristoph Lameter if (start & ~PAGE_MASK) 12986ce3c4c0SChristoph Lameter return -EINVAL; 12996ce3c4c0SChristoph Lameter 1300d3a71033SLee Schermerhorn if (mode == MPOL_DEFAULT || mode == MPOL_NOOP) 13016ce3c4c0SChristoph Lameter flags &= ~MPOL_MF_STRICT; 13026ce3c4c0SChristoph Lameter 13036ce3c4c0SChristoph Lameter len = (len + PAGE_SIZE - 1) & PAGE_MASK; 13046ce3c4c0SChristoph Lameter end = start + len; 13056ce3c4c0SChristoph Lameter 13066ce3c4c0SChristoph Lameter if (end < start) 13076ce3c4c0SChristoph Lameter return -EINVAL; 13086ce3c4c0SChristoph Lameter if (end == start) 13096ce3c4c0SChristoph Lameter return 0; 13106ce3c4c0SChristoph Lameter 1311028fec41SDavid Rientjes new = mpol_new(mode, mode_flags, nmask); 13126ce3c4c0SChristoph Lameter if (IS_ERR(new)) 13136ce3c4c0SChristoph Lameter return PTR_ERR(new); 13146ce3c4c0SChristoph Lameter 1315*b24f53a0SLee Schermerhorn if (flags & MPOL_MF_LAZY) 1316*b24f53a0SLee Schermerhorn new->flags |= MPOL_F_MOF; 1317*b24f53a0SLee Schermerhorn 13186ce3c4c0SChristoph Lameter /* 13196ce3c4c0SChristoph Lameter * If we are using the default policy then operation 13206ce3c4c0SChristoph Lameter * on discontinuous address spaces is okay after all 13216ce3c4c0SChristoph Lameter */ 13226ce3c4c0SChristoph Lameter if (!new) 13236ce3c4c0SChristoph Lameter flags |= MPOL_MF_DISCONTIG_OK; 13246ce3c4c0SChristoph Lameter 1325028fec41SDavid Rientjes pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n", 1326028fec41SDavid Rientjes start, start + len, mode, mode_flags, 1327028fec41SDavid Rientjes nmask ? nodes_addr(*nmask)[0] : -1); 13286ce3c4c0SChristoph Lameter 13290aedadf9SChristoph Lameter if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { 13300aedadf9SChristoph Lameter 13310aedadf9SChristoph Lameter err = migrate_prep(); 13320aedadf9SChristoph Lameter if (err) 1333b05ca738SKOSAKI Motohiro goto mpol_out; 13340aedadf9SChristoph Lameter } 13354bfc4495SKAMEZAWA Hiroyuki { 13364bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH(scratch); 13374bfc4495SKAMEZAWA Hiroyuki if (scratch) { 13386ce3c4c0SChristoph Lameter down_write(&mm->mmap_sem); 133958568d2aSMiao Xie task_lock(current); 13404bfc4495SKAMEZAWA Hiroyuki err = mpol_set_nodemask(new, nmask, scratch); 134158568d2aSMiao Xie task_unlock(current); 13424bfc4495SKAMEZAWA Hiroyuki if (err) 134358568d2aSMiao Xie up_write(&mm->mmap_sem); 13444bfc4495SKAMEZAWA Hiroyuki } else 13454bfc4495SKAMEZAWA Hiroyuki err = -ENOMEM; 13464bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH_FREE(scratch); 13474bfc4495SKAMEZAWA Hiroyuki } 1348b05ca738SKOSAKI Motohiro if (err) 1349b05ca738SKOSAKI Motohiro goto mpol_out; 1350b05ca738SKOSAKI Motohiro 13516ce3c4c0SChristoph Lameter vma = check_range(mm, start, end, nmask, 13526ce3c4c0SChristoph Lameter flags | MPOL_MF_INVERT, &pagelist); 13536ce3c4c0SChristoph Lameter 1354*b24f53a0SLee Schermerhorn err = PTR_ERR(vma); /* maybe ... */ 1355*b24f53a0SLee Schermerhorn if (!IS_ERR(vma) && mode != MPOL_NOOP) 13569d8cebd4SKOSAKI Motohiro err = mbind_range(mm, start, end, new); 13577e2ab150SChristoph Lameter 1358*b24f53a0SLee Schermerhorn if (!err) { 1359*b24f53a0SLee Schermerhorn int nr_failed = 0; 1360*b24f53a0SLee Schermerhorn 1361cf608ac1SMinchan Kim if (!list_empty(&pagelist)) { 1362*b24f53a0SLee Schermerhorn WARN_ON_ONCE(flags & MPOL_MF_LAZY); 136395a402c3SChristoph Lameter nr_failed = migrate_pages(&pagelist, new_vma_page, 13647f0f2496SMel Gorman (unsigned long)vma, 13657b2a2d4aSMel Gorman false, MIGRATE_SYNC, 13667b2a2d4aSMel Gorman MR_MEMPOLICY_MBIND); 1367cf608ac1SMinchan Kim if (nr_failed) 1368cf608ac1SMinchan Kim putback_lru_pages(&pagelist); 1369cf608ac1SMinchan Kim } 13706ce3c4c0SChristoph Lameter 1371*b24f53a0SLee Schermerhorn if (nr_failed && (flags & MPOL_MF_STRICT)) 13726ce3c4c0SChristoph Lameter err = -EIO; 1373ab8a3e14SKOSAKI Motohiro } else 1374ab8a3e14SKOSAKI Motohiro putback_lru_pages(&pagelist); 1375b20a3503SChristoph Lameter 13766ce3c4c0SChristoph Lameter up_write(&mm->mmap_sem); 1377b05ca738SKOSAKI Motohiro mpol_out: 1378f0be3d32SLee Schermerhorn mpol_put(new); 13796ce3c4c0SChristoph Lameter return err; 13806ce3c4c0SChristoph Lameter } 13816ce3c4c0SChristoph Lameter 138239743889SChristoph Lameter /* 13838bccd85fSChristoph Lameter * User space interface with variable sized bitmaps for nodelists. 13848bccd85fSChristoph Lameter */ 13858bccd85fSChristoph Lameter 13868bccd85fSChristoph Lameter /* Copy a node mask from user space. */ 138739743889SChristoph Lameter static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, 13888bccd85fSChristoph Lameter unsigned long maxnode) 13898bccd85fSChristoph Lameter { 13908bccd85fSChristoph Lameter unsigned long k; 13918bccd85fSChristoph Lameter unsigned long nlongs; 13928bccd85fSChristoph Lameter unsigned long endmask; 13938bccd85fSChristoph Lameter 13948bccd85fSChristoph Lameter --maxnode; 13958bccd85fSChristoph Lameter nodes_clear(*nodes); 13968bccd85fSChristoph Lameter if (maxnode == 0 || !nmask) 13978bccd85fSChristoph Lameter return 0; 1398a9c930baSAndi Kleen if (maxnode > PAGE_SIZE*BITS_PER_BYTE) 1399636f13c1SChris Wright return -EINVAL; 14008bccd85fSChristoph Lameter 14018bccd85fSChristoph Lameter nlongs = BITS_TO_LONGS(maxnode); 14028bccd85fSChristoph Lameter if ((maxnode % BITS_PER_LONG) == 0) 14038bccd85fSChristoph Lameter endmask = ~0UL; 14048bccd85fSChristoph Lameter else 14058bccd85fSChristoph Lameter endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1; 14068bccd85fSChristoph Lameter 14078bccd85fSChristoph Lameter /* When the user specified more nodes than supported just check 14088bccd85fSChristoph Lameter if the non supported part is all zero. */ 14098bccd85fSChristoph Lameter if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) { 14108bccd85fSChristoph Lameter if (nlongs > PAGE_SIZE/sizeof(long)) 14118bccd85fSChristoph Lameter return -EINVAL; 14128bccd85fSChristoph Lameter for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) { 14138bccd85fSChristoph Lameter unsigned long t; 14148bccd85fSChristoph Lameter if (get_user(t, nmask + k)) 14158bccd85fSChristoph Lameter return -EFAULT; 14168bccd85fSChristoph Lameter if (k == nlongs - 1) { 14178bccd85fSChristoph Lameter if (t & endmask) 14188bccd85fSChristoph Lameter return -EINVAL; 14198bccd85fSChristoph Lameter } else if (t) 14208bccd85fSChristoph Lameter return -EINVAL; 14218bccd85fSChristoph Lameter } 14228bccd85fSChristoph Lameter nlongs = BITS_TO_LONGS(MAX_NUMNODES); 14238bccd85fSChristoph Lameter endmask = ~0UL; 14248bccd85fSChristoph Lameter } 14258bccd85fSChristoph Lameter 14268bccd85fSChristoph Lameter if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long))) 14278bccd85fSChristoph Lameter return -EFAULT; 14288bccd85fSChristoph Lameter nodes_addr(*nodes)[nlongs-1] &= endmask; 14298bccd85fSChristoph Lameter return 0; 14308bccd85fSChristoph Lameter } 14318bccd85fSChristoph Lameter 14328bccd85fSChristoph Lameter /* Copy a kernel node mask to user space */ 14338bccd85fSChristoph Lameter static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, 14348bccd85fSChristoph Lameter nodemask_t *nodes) 14358bccd85fSChristoph Lameter { 14368bccd85fSChristoph Lameter unsigned long copy = ALIGN(maxnode-1, 64) / 8; 14378bccd85fSChristoph Lameter const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long); 14388bccd85fSChristoph Lameter 14398bccd85fSChristoph Lameter if (copy > nbytes) { 14408bccd85fSChristoph Lameter if (copy > PAGE_SIZE) 14418bccd85fSChristoph Lameter return -EINVAL; 14428bccd85fSChristoph Lameter if (clear_user((char __user *)mask + nbytes, copy - nbytes)) 14438bccd85fSChristoph Lameter return -EFAULT; 14448bccd85fSChristoph Lameter copy = nbytes; 14458bccd85fSChristoph Lameter } 14468bccd85fSChristoph Lameter return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; 14478bccd85fSChristoph Lameter } 14488bccd85fSChristoph Lameter 1449938bb9f5SHeiko Carstens SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len, 1450938bb9f5SHeiko Carstens unsigned long, mode, unsigned long __user *, nmask, 1451938bb9f5SHeiko Carstens unsigned long, maxnode, unsigned, flags) 14528bccd85fSChristoph Lameter { 14538bccd85fSChristoph Lameter nodemask_t nodes; 14548bccd85fSChristoph Lameter int err; 1455028fec41SDavid Rientjes unsigned short mode_flags; 14568bccd85fSChristoph Lameter 1457028fec41SDavid Rientjes mode_flags = mode & MPOL_MODE_FLAGS; 1458028fec41SDavid Rientjes mode &= ~MPOL_MODE_FLAGS; 1459a3b51e01SDavid Rientjes if (mode >= MPOL_MAX) 1460a3b51e01SDavid Rientjes return -EINVAL; 14614c50bc01SDavid Rientjes if ((mode_flags & MPOL_F_STATIC_NODES) && 14624c50bc01SDavid Rientjes (mode_flags & MPOL_F_RELATIVE_NODES)) 14634c50bc01SDavid Rientjes return -EINVAL; 14648bccd85fSChristoph Lameter err = get_nodes(&nodes, nmask, maxnode); 14658bccd85fSChristoph Lameter if (err) 14668bccd85fSChristoph Lameter return err; 1467028fec41SDavid Rientjes return do_mbind(start, len, mode, mode_flags, &nodes, flags); 14688bccd85fSChristoph Lameter } 14698bccd85fSChristoph Lameter 14708bccd85fSChristoph Lameter /* Set the process memory policy */ 1471938bb9f5SHeiko Carstens SYSCALL_DEFINE3(set_mempolicy, int, mode, unsigned long __user *, nmask, 1472938bb9f5SHeiko Carstens unsigned long, maxnode) 14738bccd85fSChristoph Lameter { 14748bccd85fSChristoph Lameter int err; 14758bccd85fSChristoph Lameter nodemask_t nodes; 1476028fec41SDavid Rientjes unsigned short flags; 14778bccd85fSChristoph Lameter 1478028fec41SDavid Rientjes flags = mode & MPOL_MODE_FLAGS; 1479028fec41SDavid Rientjes mode &= ~MPOL_MODE_FLAGS; 1480028fec41SDavid Rientjes if ((unsigned int)mode >= MPOL_MAX) 14818bccd85fSChristoph Lameter return -EINVAL; 14824c50bc01SDavid Rientjes if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES)) 14834c50bc01SDavid Rientjes return -EINVAL; 14848bccd85fSChristoph Lameter err = get_nodes(&nodes, nmask, maxnode); 14858bccd85fSChristoph Lameter if (err) 14868bccd85fSChristoph Lameter return err; 1487028fec41SDavid Rientjes return do_set_mempolicy(mode, flags, &nodes); 14888bccd85fSChristoph Lameter } 14898bccd85fSChristoph Lameter 1490938bb9f5SHeiko Carstens SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode, 1491938bb9f5SHeiko Carstens const unsigned long __user *, old_nodes, 1492938bb9f5SHeiko Carstens const unsigned long __user *, new_nodes) 149339743889SChristoph Lameter { 1494c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 1495596d7cfaSKOSAKI Motohiro struct mm_struct *mm = NULL; 149639743889SChristoph Lameter struct task_struct *task; 149739743889SChristoph Lameter nodemask_t task_nodes; 149839743889SChristoph Lameter int err; 1499596d7cfaSKOSAKI Motohiro nodemask_t *old; 1500596d7cfaSKOSAKI Motohiro nodemask_t *new; 1501596d7cfaSKOSAKI Motohiro NODEMASK_SCRATCH(scratch); 150239743889SChristoph Lameter 1503596d7cfaSKOSAKI Motohiro if (!scratch) 1504596d7cfaSKOSAKI Motohiro return -ENOMEM; 150539743889SChristoph Lameter 1506596d7cfaSKOSAKI Motohiro old = &scratch->mask1; 1507596d7cfaSKOSAKI Motohiro new = &scratch->mask2; 1508596d7cfaSKOSAKI Motohiro 1509596d7cfaSKOSAKI Motohiro err = get_nodes(old, old_nodes, maxnode); 151039743889SChristoph Lameter if (err) 1511596d7cfaSKOSAKI Motohiro goto out; 1512596d7cfaSKOSAKI Motohiro 1513596d7cfaSKOSAKI Motohiro err = get_nodes(new, new_nodes, maxnode); 1514596d7cfaSKOSAKI Motohiro if (err) 1515596d7cfaSKOSAKI Motohiro goto out; 151639743889SChristoph Lameter 151739743889SChristoph Lameter /* Find the mm_struct */ 151855cfaa3cSZeng Zhaoming rcu_read_lock(); 1519228ebcbeSPavel Emelyanov task = pid ? find_task_by_vpid(pid) : current; 152039743889SChristoph Lameter if (!task) { 152155cfaa3cSZeng Zhaoming rcu_read_unlock(); 1522596d7cfaSKOSAKI Motohiro err = -ESRCH; 1523596d7cfaSKOSAKI Motohiro goto out; 152439743889SChristoph Lameter } 15253268c63eSChristoph Lameter get_task_struct(task); 152639743889SChristoph Lameter 1527596d7cfaSKOSAKI Motohiro err = -EINVAL; 152839743889SChristoph Lameter 152939743889SChristoph Lameter /* 153039743889SChristoph Lameter * Check if this process has the right to modify the specified 153139743889SChristoph Lameter * process. The right exists if the process has administrative 15327f927fccSAlexey Dobriyan * capabilities, superuser privileges or the same 153339743889SChristoph Lameter * userid as the target process. 153439743889SChristoph Lameter */ 1535c69e8d9cSDavid Howells tcred = __task_cred(task); 1536b38a86ebSEric W. Biederman if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) && 1537b38a86ebSEric W. Biederman !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) && 153874c00241SChristoph Lameter !capable(CAP_SYS_NICE)) { 1539c69e8d9cSDavid Howells rcu_read_unlock(); 154039743889SChristoph Lameter err = -EPERM; 15413268c63eSChristoph Lameter goto out_put; 154239743889SChristoph Lameter } 1543c69e8d9cSDavid Howells rcu_read_unlock(); 154439743889SChristoph Lameter 154539743889SChristoph Lameter task_nodes = cpuset_mems_allowed(task); 154639743889SChristoph Lameter /* Is the user allowed to access the target nodes? */ 1547596d7cfaSKOSAKI Motohiro if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) { 154839743889SChristoph Lameter err = -EPERM; 15493268c63eSChristoph Lameter goto out_put; 155039743889SChristoph Lameter } 155139743889SChristoph Lameter 1552596d7cfaSKOSAKI Motohiro if (!nodes_subset(*new, node_states[N_HIGH_MEMORY])) { 15533b42d28bSChristoph Lameter err = -EINVAL; 15543268c63eSChristoph Lameter goto out_put; 15553b42d28bSChristoph Lameter } 15563b42d28bSChristoph Lameter 155786c3a764SDavid Quigley err = security_task_movememory(task); 155886c3a764SDavid Quigley if (err) 15593268c63eSChristoph Lameter goto out_put; 156086c3a764SDavid Quigley 15613268c63eSChristoph Lameter mm = get_task_mm(task); 15623268c63eSChristoph Lameter put_task_struct(task); 1563f2a9ef88SSasha Levin 1564f2a9ef88SSasha Levin if (!mm) { 1565f2a9ef88SSasha Levin err = -EINVAL; 1566f2a9ef88SSasha Levin goto out; 1567f2a9ef88SSasha Levin } 1568f2a9ef88SSasha Levin 1569596d7cfaSKOSAKI Motohiro err = do_migrate_pages(mm, old, new, 157074c00241SChristoph Lameter capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE); 15713268c63eSChristoph Lameter 157239743889SChristoph Lameter mmput(mm); 15733268c63eSChristoph Lameter out: 1574596d7cfaSKOSAKI Motohiro NODEMASK_SCRATCH_FREE(scratch); 1575596d7cfaSKOSAKI Motohiro 157639743889SChristoph Lameter return err; 15773268c63eSChristoph Lameter 15783268c63eSChristoph Lameter out_put: 15793268c63eSChristoph Lameter put_task_struct(task); 15803268c63eSChristoph Lameter goto out; 15813268c63eSChristoph Lameter 158239743889SChristoph Lameter } 158339743889SChristoph Lameter 158439743889SChristoph Lameter 15858bccd85fSChristoph Lameter /* Retrieve NUMA policy */ 1586938bb9f5SHeiko Carstens SYSCALL_DEFINE5(get_mempolicy, int __user *, policy, 1587938bb9f5SHeiko Carstens unsigned long __user *, nmask, unsigned long, maxnode, 1588938bb9f5SHeiko Carstens unsigned long, addr, unsigned long, flags) 15898bccd85fSChristoph Lameter { 1590dbcb0f19SAdrian Bunk int err; 1591dbcb0f19SAdrian Bunk int uninitialized_var(pval); 15928bccd85fSChristoph Lameter nodemask_t nodes; 15938bccd85fSChristoph Lameter 15948bccd85fSChristoph Lameter if (nmask != NULL && maxnode < MAX_NUMNODES) 15958bccd85fSChristoph Lameter return -EINVAL; 15968bccd85fSChristoph Lameter 15978bccd85fSChristoph Lameter err = do_get_mempolicy(&pval, &nodes, addr, flags); 15988bccd85fSChristoph Lameter 15998bccd85fSChristoph Lameter if (err) 16008bccd85fSChristoph Lameter return err; 16018bccd85fSChristoph Lameter 16028bccd85fSChristoph Lameter if (policy && put_user(pval, policy)) 16038bccd85fSChristoph Lameter return -EFAULT; 16048bccd85fSChristoph Lameter 16058bccd85fSChristoph Lameter if (nmask) 16068bccd85fSChristoph Lameter err = copy_nodes_to_user(nmask, maxnode, &nodes); 16078bccd85fSChristoph Lameter 16088bccd85fSChristoph Lameter return err; 16098bccd85fSChristoph Lameter } 16108bccd85fSChristoph Lameter 16111da177e4SLinus Torvalds #ifdef CONFIG_COMPAT 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds asmlinkage long compat_sys_get_mempolicy(int __user *policy, 16141da177e4SLinus Torvalds compat_ulong_t __user *nmask, 16151da177e4SLinus Torvalds compat_ulong_t maxnode, 16161da177e4SLinus Torvalds compat_ulong_t addr, compat_ulong_t flags) 16171da177e4SLinus Torvalds { 16181da177e4SLinus Torvalds long err; 16191da177e4SLinus Torvalds unsigned long __user *nm = NULL; 16201da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 16211da177e4SLinus Torvalds DECLARE_BITMAP(bm, MAX_NUMNODES); 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 16241da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds if (nmask) 16271da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags); 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds if (!err && nmask) { 16322bbff6c7SKAMEZAWA Hiroyuki unsigned long copy_size; 16332bbff6c7SKAMEZAWA Hiroyuki copy_size = min_t(unsigned long, sizeof(bm), alloc_size); 16342bbff6c7SKAMEZAWA Hiroyuki err = copy_from_user(bm, nm, copy_size); 16351da177e4SLinus Torvalds /* ensure entire bitmap is zeroed */ 16361da177e4SLinus Torvalds err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8); 16371da177e4SLinus Torvalds err |= compat_put_bitmap(nmask, bm, nr_bits); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds return err; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask, 16441da177e4SLinus Torvalds compat_ulong_t maxnode) 16451da177e4SLinus Torvalds { 16461da177e4SLinus Torvalds long err = 0; 16471da177e4SLinus Torvalds unsigned long __user *nm = NULL; 16481da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 16491da177e4SLinus Torvalds DECLARE_BITMAP(bm, MAX_NUMNODES); 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 16521da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds if (nmask) { 16551da177e4SLinus Torvalds err = compat_get_bitmap(bm, nmask, nr_bits); 16561da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 16571da177e4SLinus Torvalds err |= copy_to_user(nm, bm, alloc_size); 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds if (err) 16611da177e4SLinus Torvalds return -EFAULT; 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds return sys_set_mempolicy(mode, nm, nr_bits+1); 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len, 16671da177e4SLinus Torvalds compat_ulong_t mode, compat_ulong_t __user *nmask, 16681da177e4SLinus Torvalds compat_ulong_t maxnode, compat_ulong_t flags) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds long err = 0; 16711da177e4SLinus Torvalds unsigned long __user *nm = NULL; 16721da177e4SLinus Torvalds unsigned long nr_bits, alloc_size; 1673dfcd3c0dSAndi Kleen nodemask_t bm; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); 16761da177e4SLinus Torvalds alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds if (nmask) { 1679dfcd3c0dSAndi Kleen err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits); 16801da177e4SLinus Torvalds nm = compat_alloc_user_space(alloc_size); 1681dfcd3c0dSAndi Kleen err |= copy_to_user(nm, nodes_addr(bm), alloc_size); 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds if (err) 16851da177e4SLinus Torvalds return -EFAULT; 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds return sys_mbind(start, len, mode, nm, nr_bits+1, flags); 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds #endif 16911da177e4SLinus Torvalds 1692480eccf9SLee Schermerhorn /* 1693480eccf9SLee Schermerhorn * get_vma_policy(@task, @vma, @addr) 1694480eccf9SLee Schermerhorn * @task - task for fallback if vma policy == default 1695480eccf9SLee Schermerhorn * @vma - virtual memory area whose policy is sought 1696480eccf9SLee Schermerhorn * @addr - address in @vma for shared policy lookup 1697480eccf9SLee Schermerhorn * 1698480eccf9SLee Schermerhorn * Returns effective policy for a VMA at specified address. 1699480eccf9SLee Schermerhorn * Falls back to @task or system default policy, as necessary. 170032f8516aSDavid Rientjes * Current or other task's task mempolicy and non-shared vma policies must be 170132f8516aSDavid Rientjes * protected by task_lock(task) by the caller. 170252cd3b07SLee Schermerhorn * Shared policies [those marked as MPOL_F_SHARED] require an extra reference 170352cd3b07SLee Schermerhorn * count--added by the get_policy() vm_op, as appropriate--to protect against 170452cd3b07SLee Schermerhorn * freeing by another task. It is the caller's responsibility to free the 170552cd3b07SLee Schermerhorn * extra reference for shared policies. 1706480eccf9SLee Schermerhorn */ 1707d98f6cb6SStephen Wilson struct mempolicy *get_vma_policy(struct task_struct *task, 170848fce342SChristoph Lameter struct vm_area_struct *vma, unsigned long addr) 17091da177e4SLinus Torvalds { 17106e21c8f1SChristoph Lameter struct mempolicy *pol = task->mempolicy; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds if (vma) { 1713480eccf9SLee Schermerhorn if (vma->vm_ops && vma->vm_ops->get_policy) { 1714ae4d8c16SLee Schermerhorn struct mempolicy *vpol = vma->vm_ops->get_policy(vma, 1715ae4d8c16SLee Schermerhorn addr); 1716ae4d8c16SLee Schermerhorn if (vpol) 1717ae4d8c16SLee Schermerhorn pol = vpol; 171800442ad0SMel Gorman } else if (vma->vm_policy) { 17191da177e4SLinus Torvalds pol = vma->vm_policy; 172000442ad0SMel Gorman 172100442ad0SMel Gorman /* 172200442ad0SMel Gorman * shmem_alloc_page() passes MPOL_F_SHARED policy with 172300442ad0SMel Gorman * a pseudo vma whose vma->vm_ops=NULL. Take a reference 172400442ad0SMel Gorman * count on these policies which will be dropped by 172500442ad0SMel Gorman * mpol_cond_put() later 172600442ad0SMel Gorman */ 172700442ad0SMel Gorman if (mpol_needs_cond_ref(pol)) 172800442ad0SMel Gorman mpol_get(pol); 172900442ad0SMel Gorman } 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds if (!pol) 17321da177e4SLinus Torvalds pol = &default_policy; 17331da177e4SLinus Torvalds return pol; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 173652cd3b07SLee Schermerhorn /* 173752cd3b07SLee Schermerhorn * Return a nodemask representing a mempolicy for filtering nodes for 173852cd3b07SLee Schermerhorn * page allocation 173952cd3b07SLee Schermerhorn */ 174052cd3b07SLee Schermerhorn static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy) 174119770b32SMel Gorman { 174219770b32SMel Gorman /* Lower zones don't get a nodemask applied for MPOL_BIND */ 174345c4745aSLee Schermerhorn if (unlikely(policy->mode == MPOL_BIND) && 174419770b32SMel Gorman gfp_zone(gfp) >= policy_zone && 174519770b32SMel Gorman cpuset_nodemask_valid_mems_allowed(&policy->v.nodes)) 174619770b32SMel Gorman return &policy->v.nodes; 174719770b32SMel Gorman 174819770b32SMel Gorman return NULL; 174919770b32SMel Gorman } 175019770b32SMel Gorman 175152cd3b07SLee Schermerhorn /* Return a zonelist indicated by gfp for node representing a mempolicy */ 17522f5f9486SAndi Kleen static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy, 17532f5f9486SAndi Kleen int nd) 17541da177e4SLinus Torvalds { 175545c4745aSLee Schermerhorn switch (policy->mode) { 17561da177e4SLinus Torvalds case MPOL_PREFERRED: 1757fc36b8d3SLee Schermerhorn if (!(policy->flags & MPOL_F_LOCAL)) 17581da177e4SLinus Torvalds nd = policy->v.preferred_node; 17591da177e4SLinus Torvalds break; 17601da177e4SLinus Torvalds case MPOL_BIND: 176119770b32SMel Gorman /* 176252cd3b07SLee Schermerhorn * Normally, MPOL_BIND allocations are node-local within the 176352cd3b07SLee Schermerhorn * allowed nodemask. However, if __GFP_THISNODE is set and the 17646eb27e1fSBob Liu * current node isn't part of the mask, we use the zonelist for 176552cd3b07SLee Schermerhorn * the first node in the mask instead. 176619770b32SMel Gorman */ 176719770b32SMel Gorman if (unlikely(gfp & __GFP_THISNODE) && 176819770b32SMel Gorman unlikely(!node_isset(nd, policy->v.nodes))) 176919770b32SMel Gorman nd = first_node(policy->v.nodes); 177019770b32SMel Gorman break; 17711da177e4SLinus Torvalds default: 17721da177e4SLinus Torvalds BUG(); 17731da177e4SLinus Torvalds } 17740e88460dSMel Gorman return node_zonelist(nd, gfp); 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds /* Do dynamic interleaving for a process */ 17781da177e4SLinus Torvalds static unsigned interleave_nodes(struct mempolicy *policy) 17791da177e4SLinus Torvalds { 17801da177e4SLinus Torvalds unsigned nid, next; 17811da177e4SLinus Torvalds struct task_struct *me = current; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds nid = me->il_next; 1784dfcd3c0dSAndi Kleen next = next_node(nid, policy->v.nodes); 17851da177e4SLinus Torvalds if (next >= MAX_NUMNODES) 1786dfcd3c0dSAndi Kleen next = first_node(policy->v.nodes); 1787f5b087b5SDavid Rientjes if (next < MAX_NUMNODES) 17881da177e4SLinus Torvalds me->il_next = next; 17891da177e4SLinus Torvalds return nid; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 1792dc85da15SChristoph Lameter /* 1793dc85da15SChristoph Lameter * Depending on the memory policy provide a node from which to allocate the 1794dc85da15SChristoph Lameter * next slab entry. 179552cd3b07SLee Schermerhorn * @policy must be protected by freeing by the caller. If @policy is 179652cd3b07SLee Schermerhorn * the current task's mempolicy, this protection is implicit, as only the 179752cd3b07SLee Schermerhorn * task can change it's policy. The system default policy requires no 179852cd3b07SLee Schermerhorn * such protection. 1799dc85da15SChristoph Lameter */ 1800e7b691b0SAndi Kleen unsigned slab_node(void) 1801dc85da15SChristoph Lameter { 1802e7b691b0SAndi Kleen struct mempolicy *policy; 1803e7b691b0SAndi Kleen 1804e7b691b0SAndi Kleen if (in_interrupt()) 1805e7b691b0SAndi Kleen return numa_node_id(); 1806e7b691b0SAndi Kleen 1807e7b691b0SAndi Kleen policy = current->mempolicy; 1808fc36b8d3SLee Schermerhorn if (!policy || policy->flags & MPOL_F_LOCAL) 1809bea904d5SLee Schermerhorn return numa_node_id(); 1810765c4507SChristoph Lameter 1811bea904d5SLee Schermerhorn switch (policy->mode) { 1812bea904d5SLee Schermerhorn case MPOL_PREFERRED: 1813fc36b8d3SLee Schermerhorn /* 1814fc36b8d3SLee Schermerhorn * handled MPOL_F_LOCAL above 1815fc36b8d3SLee Schermerhorn */ 1816bea904d5SLee Schermerhorn return policy->v.preferred_node; 1817bea904d5SLee Schermerhorn 1818dc85da15SChristoph Lameter case MPOL_INTERLEAVE: 1819dc85da15SChristoph Lameter return interleave_nodes(policy); 1820dc85da15SChristoph Lameter 1821dd1a239fSMel Gorman case MPOL_BIND: { 1822dc85da15SChristoph Lameter /* 1823dc85da15SChristoph Lameter * Follow bind policy behavior and start allocation at the 1824dc85da15SChristoph Lameter * first node. 1825dc85da15SChristoph Lameter */ 182619770b32SMel Gorman struct zonelist *zonelist; 182719770b32SMel Gorman struct zone *zone; 182819770b32SMel Gorman enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL); 182919770b32SMel Gorman zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0]; 183019770b32SMel Gorman (void)first_zones_zonelist(zonelist, highest_zoneidx, 183119770b32SMel Gorman &policy->v.nodes, 183219770b32SMel Gorman &zone); 1833800416f7SEric Dumazet return zone ? zone->node : numa_node_id(); 1834dd1a239fSMel Gorman } 1835dc85da15SChristoph Lameter 1836dc85da15SChristoph Lameter default: 1837bea904d5SLee Schermerhorn BUG(); 1838dc85da15SChristoph Lameter } 1839dc85da15SChristoph Lameter } 1840dc85da15SChristoph Lameter 18411da177e4SLinus Torvalds /* Do static interleaving for a VMA with known offset. */ 18421da177e4SLinus Torvalds static unsigned offset_il_node(struct mempolicy *pol, 18431da177e4SLinus Torvalds struct vm_area_struct *vma, unsigned long off) 18441da177e4SLinus Torvalds { 1845dfcd3c0dSAndi Kleen unsigned nnodes = nodes_weight(pol->v.nodes); 1846f5b087b5SDavid Rientjes unsigned target; 18471da177e4SLinus Torvalds int c; 18481da177e4SLinus Torvalds int nid = -1; 18491da177e4SLinus Torvalds 1850f5b087b5SDavid Rientjes if (!nnodes) 1851f5b087b5SDavid Rientjes return numa_node_id(); 1852f5b087b5SDavid Rientjes target = (unsigned int)off % nnodes; 18531da177e4SLinus Torvalds c = 0; 18541da177e4SLinus Torvalds do { 1855dfcd3c0dSAndi Kleen nid = next_node(nid, pol->v.nodes); 18561da177e4SLinus Torvalds c++; 18571da177e4SLinus Torvalds } while (c <= target); 18581da177e4SLinus Torvalds return nid; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds 18615da7ca86SChristoph Lameter /* Determine a node number for interleave */ 18625da7ca86SChristoph Lameter static inline unsigned interleave_nid(struct mempolicy *pol, 18635da7ca86SChristoph Lameter struct vm_area_struct *vma, unsigned long addr, int shift) 18645da7ca86SChristoph Lameter { 18655da7ca86SChristoph Lameter if (vma) { 18665da7ca86SChristoph Lameter unsigned long off; 18675da7ca86SChristoph Lameter 18683b98b087SNishanth Aravamudan /* 18693b98b087SNishanth Aravamudan * for small pages, there is no difference between 18703b98b087SNishanth Aravamudan * shift and PAGE_SHIFT, so the bit-shift is safe. 18713b98b087SNishanth Aravamudan * for huge pages, since vm_pgoff is in units of small 18723b98b087SNishanth Aravamudan * pages, we need to shift off the always 0 bits to get 18733b98b087SNishanth Aravamudan * a useful offset. 18743b98b087SNishanth Aravamudan */ 18753b98b087SNishanth Aravamudan BUG_ON(shift < PAGE_SHIFT); 18763b98b087SNishanth Aravamudan off = vma->vm_pgoff >> (shift - PAGE_SHIFT); 18775da7ca86SChristoph Lameter off += (addr - vma->vm_start) >> shift; 18785da7ca86SChristoph Lameter return offset_il_node(pol, vma, off); 18795da7ca86SChristoph Lameter } else 18805da7ca86SChristoph Lameter return interleave_nodes(pol); 18815da7ca86SChristoph Lameter } 18825da7ca86SChristoph Lameter 1883778d3b0fSMichal Hocko /* 1884778d3b0fSMichal Hocko * Return the bit number of a random bit set in the nodemask. 1885778d3b0fSMichal Hocko * (returns -1 if nodemask is empty) 1886778d3b0fSMichal Hocko */ 1887778d3b0fSMichal Hocko int node_random(const nodemask_t *maskp) 1888778d3b0fSMichal Hocko { 1889778d3b0fSMichal Hocko int w, bit = -1; 1890778d3b0fSMichal Hocko 1891778d3b0fSMichal Hocko w = nodes_weight(*maskp); 1892778d3b0fSMichal Hocko if (w) 1893778d3b0fSMichal Hocko bit = bitmap_ord_to_pos(maskp->bits, 1894778d3b0fSMichal Hocko get_random_int() % w, MAX_NUMNODES); 1895778d3b0fSMichal Hocko return bit; 1896778d3b0fSMichal Hocko } 1897778d3b0fSMichal Hocko 189800ac59adSChen, Kenneth W #ifdef CONFIG_HUGETLBFS 1899480eccf9SLee Schermerhorn /* 1900480eccf9SLee Schermerhorn * huge_zonelist(@vma, @addr, @gfp_flags, @mpol) 1901480eccf9SLee Schermerhorn * @vma = virtual memory area whose policy is sought 1902480eccf9SLee Schermerhorn * @addr = address in @vma for shared policy lookup and interleave policy 1903480eccf9SLee Schermerhorn * @gfp_flags = for requested zone 190419770b32SMel Gorman * @mpol = pointer to mempolicy pointer for reference counted mempolicy 190519770b32SMel Gorman * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask 1906480eccf9SLee Schermerhorn * 190752cd3b07SLee Schermerhorn * Returns a zonelist suitable for a huge page allocation and a pointer 190852cd3b07SLee Schermerhorn * to the struct mempolicy for conditional unref after allocation. 190952cd3b07SLee Schermerhorn * If the effective policy is 'BIND, returns a pointer to the mempolicy's 191052cd3b07SLee Schermerhorn * @nodemask for filtering the zonelist. 1911c0ff7453SMiao Xie * 1912c0ff7453SMiao Xie * Must be protected by get_mems_allowed() 1913480eccf9SLee Schermerhorn */ 1914396faf03SMel Gorman struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr, 191519770b32SMel Gorman gfp_t gfp_flags, struct mempolicy **mpol, 191619770b32SMel Gorman nodemask_t **nodemask) 19175da7ca86SChristoph Lameter { 1918480eccf9SLee Schermerhorn struct zonelist *zl; 19195da7ca86SChristoph Lameter 192052cd3b07SLee Schermerhorn *mpol = get_vma_policy(current, vma, addr); 192119770b32SMel Gorman *nodemask = NULL; /* assume !MPOL_BIND */ 19225da7ca86SChristoph Lameter 192352cd3b07SLee Schermerhorn if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) { 192452cd3b07SLee Schermerhorn zl = node_zonelist(interleave_nid(*mpol, vma, addr, 1925a5516438SAndi Kleen huge_page_shift(hstate_vma(vma))), gfp_flags); 192652cd3b07SLee Schermerhorn } else { 19272f5f9486SAndi Kleen zl = policy_zonelist(gfp_flags, *mpol, numa_node_id()); 192852cd3b07SLee Schermerhorn if ((*mpol)->mode == MPOL_BIND) 192952cd3b07SLee Schermerhorn *nodemask = &(*mpol)->v.nodes; 1930480eccf9SLee Schermerhorn } 1931480eccf9SLee Schermerhorn return zl; 19325da7ca86SChristoph Lameter } 193306808b08SLee Schermerhorn 193406808b08SLee Schermerhorn /* 193506808b08SLee Schermerhorn * init_nodemask_of_mempolicy 193606808b08SLee Schermerhorn * 193706808b08SLee Schermerhorn * If the current task's mempolicy is "default" [NULL], return 'false' 193806808b08SLee Schermerhorn * to indicate default policy. Otherwise, extract the policy nodemask 193906808b08SLee Schermerhorn * for 'bind' or 'interleave' policy into the argument nodemask, or 194006808b08SLee Schermerhorn * initialize the argument nodemask to contain the single node for 194106808b08SLee Schermerhorn * 'preferred' or 'local' policy and return 'true' to indicate presence 194206808b08SLee Schermerhorn * of non-default mempolicy. 194306808b08SLee Schermerhorn * 194406808b08SLee Schermerhorn * We don't bother with reference counting the mempolicy [mpol_get/put] 194506808b08SLee Schermerhorn * because the current task is examining it's own mempolicy and a task's 194606808b08SLee Schermerhorn * mempolicy is only ever changed by the task itself. 194706808b08SLee Schermerhorn * 194806808b08SLee Schermerhorn * N.B., it is the caller's responsibility to free a returned nodemask. 194906808b08SLee Schermerhorn */ 195006808b08SLee Schermerhorn bool init_nodemask_of_mempolicy(nodemask_t *mask) 195106808b08SLee Schermerhorn { 195206808b08SLee Schermerhorn struct mempolicy *mempolicy; 195306808b08SLee Schermerhorn int nid; 195406808b08SLee Schermerhorn 195506808b08SLee Schermerhorn if (!(mask && current->mempolicy)) 195606808b08SLee Schermerhorn return false; 195706808b08SLee Schermerhorn 1958c0ff7453SMiao Xie task_lock(current); 195906808b08SLee Schermerhorn mempolicy = current->mempolicy; 196006808b08SLee Schermerhorn switch (mempolicy->mode) { 196106808b08SLee Schermerhorn case MPOL_PREFERRED: 196206808b08SLee Schermerhorn if (mempolicy->flags & MPOL_F_LOCAL) 196306808b08SLee Schermerhorn nid = numa_node_id(); 196406808b08SLee Schermerhorn else 196506808b08SLee Schermerhorn nid = mempolicy->v.preferred_node; 196606808b08SLee Schermerhorn init_nodemask_of_node(mask, nid); 196706808b08SLee Schermerhorn break; 196806808b08SLee Schermerhorn 196906808b08SLee Schermerhorn case MPOL_BIND: 197006808b08SLee Schermerhorn /* Fall through */ 197106808b08SLee Schermerhorn case MPOL_INTERLEAVE: 197206808b08SLee Schermerhorn *mask = mempolicy->v.nodes; 197306808b08SLee Schermerhorn break; 197406808b08SLee Schermerhorn 197506808b08SLee Schermerhorn default: 197606808b08SLee Schermerhorn BUG(); 197706808b08SLee Schermerhorn } 1978c0ff7453SMiao Xie task_unlock(current); 197906808b08SLee Schermerhorn 198006808b08SLee Schermerhorn return true; 198106808b08SLee Schermerhorn } 198200ac59adSChen, Kenneth W #endif 19835da7ca86SChristoph Lameter 19846f48d0ebSDavid Rientjes /* 19856f48d0ebSDavid Rientjes * mempolicy_nodemask_intersects 19866f48d0ebSDavid Rientjes * 19876f48d0ebSDavid Rientjes * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default 19886f48d0ebSDavid Rientjes * policy. Otherwise, check for intersection between mask and the policy 19896f48d0ebSDavid Rientjes * nodemask for 'bind' or 'interleave' policy. For 'perferred' or 'local' 19906f48d0ebSDavid Rientjes * policy, always return true since it may allocate elsewhere on fallback. 19916f48d0ebSDavid Rientjes * 19926f48d0ebSDavid Rientjes * Takes task_lock(tsk) to prevent freeing of its mempolicy. 19936f48d0ebSDavid Rientjes */ 19946f48d0ebSDavid Rientjes bool mempolicy_nodemask_intersects(struct task_struct *tsk, 19956f48d0ebSDavid Rientjes const nodemask_t *mask) 19966f48d0ebSDavid Rientjes { 19976f48d0ebSDavid Rientjes struct mempolicy *mempolicy; 19986f48d0ebSDavid Rientjes bool ret = true; 19996f48d0ebSDavid Rientjes 20006f48d0ebSDavid Rientjes if (!mask) 20016f48d0ebSDavid Rientjes return ret; 20026f48d0ebSDavid Rientjes task_lock(tsk); 20036f48d0ebSDavid Rientjes mempolicy = tsk->mempolicy; 20046f48d0ebSDavid Rientjes if (!mempolicy) 20056f48d0ebSDavid Rientjes goto out; 20066f48d0ebSDavid Rientjes 20076f48d0ebSDavid Rientjes switch (mempolicy->mode) { 20086f48d0ebSDavid Rientjes case MPOL_PREFERRED: 20096f48d0ebSDavid Rientjes /* 20106f48d0ebSDavid Rientjes * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to 20116f48d0ebSDavid Rientjes * allocate from, they may fallback to other nodes when oom. 20126f48d0ebSDavid Rientjes * Thus, it's possible for tsk to have allocated memory from 20136f48d0ebSDavid Rientjes * nodes in mask. 20146f48d0ebSDavid Rientjes */ 20156f48d0ebSDavid Rientjes break; 20166f48d0ebSDavid Rientjes case MPOL_BIND: 20176f48d0ebSDavid Rientjes case MPOL_INTERLEAVE: 20186f48d0ebSDavid Rientjes ret = nodes_intersects(mempolicy->v.nodes, *mask); 20196f48d0ebSDavid Rientjes break; 20206f48d0ebSDavid Rientjes default: 20216f48d0ebSDavid Rientjes BUG(); 20226f48d0ebSDavid Rientjes } 20236f48d0ebSDavid Rientjes out: 20246f48d0ebSDavid Rientjes task_unlock(tsk); 20256f48d0ebSDavid Rientjes return ret; 20266f48d0ebSDavid Rientjes } 20276f48d0ebSDavid Rientjes 20281da177e4SLinus Torvalds /* Allocate a page in interleaved policy. 20291da177e4SLinus Torvalds Own path because it needs to do special accounting. */ 2030662f3a0bSAndi Kleen static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, 2031662f3a0bSAndi Kleen unsigned nid) 20321da177e4SLinus Torvalds { 20331da177e4SLinus Torvalds struct zonelist *zl; 20341da177e4SLinus Torvalds struct page *page; 20351da177e4SLinus Torvalds 20360e88460dSMel Gorman zl = node_zonelist(nid, gfp); 20371da177e4SLinus Torvalds page = __alloc_pages(gfp, order, zl); 2038dd1a239fSMel Gorman if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0])) 2039ca889e6cSChristoph Lameter inc_zone_page_state(page, NUMA_INTERLEAVE_HIT); 20401da177e4SLinus Torvalds return page; 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds /** 20440bbbc0b3SAndrea Arcangeli * alloc_pages_vma - Allocate a page for a VMA. 20451da177e4SLinus Torvalds * 20461da177e4SLinus Torvalds * @gfp: 20471da177e4SLinus Torvalds * %GFP_USER user allocation. 20481da177e4SLinus Torvalds * %GFP_KERNEL kernel allocations, 20491da177e4SLinus Torvalds * %GFP_HIGHMEM highmem/user allocations, 20501da177e4SLinus Torvalds * %GFP_FS allocation should not call back into a file system. 20511da177e4SLinus Torvalds * %GFP_ATOMIC don't sleep. 20521da177e4SLinus Torvalds * 20530bbbc0b3SAndrea Arcangeli * @order:Order of the GFP allocation. 20541da177e4SLinus Torvalds * @vma: Pointer to VMA or NULL if not available. 20551da177e4SLinus Torvalds * @addr: Virtual Address of the allocation. Must be inside the VMA. 20561da177e4SLinus Torvalds * 20571da177e4SLinus Torvalds * This function allocates a page from the kernel page pool and applies 20581da177e4SLinus Torvalds * a NUMA policy associated with the VMA or the current process. 20591da177e4SLinus Torvalds * When VMA is not NULL caller must hold down_read on the mmap_sem of the 20601da177e4SLinus Torvalds * mm_struct of the VMA to prevent it from going away. Should be used for 20611da177e4SLinus Torvalds * all allocations for pages that will be mapped into 20621da177e4SLinus Torvalds * user space. Returns NULL when no page can be allocated. 20631da177e4SLinus Torvalds * 20641da177e4SLinus Torvalds * Should be called with the mm_sem of the vma hold. 20651da177e4SLinus Torvalds */ 20661da177e4SLinus Torvalds struct page * 20670bbbc0b3SAndrea Arcangeli alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, 20682f5f9486SAndi Kleen unsigned long addr, int node) 20691da177e4SLinus Torvalds { 2070cc9a6c87SMel Gorman struct mempolicy *pol; 2071480eccf9SLee Schermerhorn struct zonelist *zl; 2072c0ff7453SMiao Xie struct page *page; 2073cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 20741da177e4SLinus Torvalds 2075cc9a6c87SMel Gorman retry_cpuset: 2076cc9a6c87SMel Gorman pol = get_vma_policy(current, vma, addr); 2077cc9a6c87SMel Gorman cpuset_mems_cookie = get_mems_allowed(); 2078cc9a6c87SMel Gorman 207945c4745aSLee Schermerhorn if (unlikely(pol->mode == MPOL_INTERLEAVE)) { 20801da177e4SLinus Torvalds unsigned nid; 20815da7ca86SChristoph Lameter 20828eac563cSAndi Kleen nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order); 208352cd3b07SLee Schermerhorn mpol_cond_put(pol); 20840bbbc0b3SAndrea Arcangeli page = alloc_page_interleave(gfp, order, nid); 2085cc9a6c87SMel Gorman if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page)) 2086cc9a6c87SMel Gorman goto retry_cpuset; 2087cc9a6c87SMel Gorman 2088c0ff7453SMiao Xie return page; 20891da177e4SLinus Torvalds } 20902f5f9486SAndi Kleen zl = policy_zonelist(gfp, pol, node); 209152cd3b07SLee Schermerhorn if (unlikely(mpol_needs_cond_ref(pol))) { 2092480eccf9SLee Schermerhorn /* 209352cd3b07SLee Schermerhorn * slow path: ref counted shared policy 2094480eccf9SLee Schermerhorn */ 20950bbbc0b3SAndrea Arcangeli struct page *page = __alloc_pages_nodemask(gfp, order, 209652cd3b07SLee Schermerhorn zl, policy_nodemask(gfp, pol)); 2097f0be3d32SLee Schermerhorn __mpol_put(pol); 2098cc9a6c87SMel Gorman if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page)) 2099cc9a6c87SMel Gorman goto retry_cpuset; 2100480eccf9SLee Schermerhorn return page; 2101480eccf9SLee Schermerhorn } 2102480eccf9SLee Schermerhorn /* 2103480eccf9SLee Schermerhorn * fast path: default or task policy 2104480eccf9SLee Schermerhorn */ 21050bbbc0b3SAndrea Arcangeli page = __alloc_pages_nodemask(gfp, order, zl, 21060bbbc0b3SAndrea Arcangeli policy_nodemask(gfp, pol)); 2107cc9a6c87SMel Gorman if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page)) 2108cc9a6c87SMel Gorman goto retry_cpuset; 2109c0ff7453SMiao Xie return page; 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds /** 21131da177e4SLinus Torvalds * alloc_pages_current - Allocate pages. 21141da177e4SLinus Torvalds * 21151da177e4SLinus Torvalds * @gfp: 21161da177e4SLinus Torvalds * %GFP_USER user allocation, 21171da177e4SLinus Torvalds * %GFP_KERNEL kernel allocation, 21181da177e4SLinus Torvalds * %GFP_HIGHMEM highmem allocation, 21191da177e4SLinus Torvalds * %GFP_FS don't call back into a file system. 21201da177e4SLinus Torvalds * %GFP_ATOMIC don't sleep. 21211da177e4SLinus Torvalds * @order: Power of two of allocation size in pages. 0 is a single page. 21221da177e4SLinus Torvalds * 21231da177e4SLinus Torvalds * Allocate a page from the kernel page pool. When not in 21241da177e4SLinus Torvalds * interrupt context and apply the current process NUMA policy. 21251da177e4SLinus Torvalds * Returns NULL when no page can be allocated. 21261da177e4SLinus Torvalds * 2127cf2a473cSPaul Jackson * Don't call cpuset_update_task_memory_state() unless 21281da177e4SLinus Torvalds * 1) it's ok to take cpuset_sem (can WAIT), and 21291da177e4SLinus Torvalds * 2) allocating for current task (not interrupt). 21301da177e4SLinus Torvalds */ 2131dd0fc66fSAl Viro struct page *alloc_pages_current(gfp_t gfp, unsigned order) 21321da177e4SLinus Torvalds { 21331da177e4SLinus Torvalds struct mempolicy *pol = current->mempolicy; 2134c0ff7453SMiao Xie struct page *page; 2135cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie; 21361da177e4SLinus Torvalds 21379b819d20SChristoph Lameter if (!pol || in_interrupt() || (gfp & __GFP_THISNODE)) 21381da177e4SLinus Torvalds pol = &default_policy; 213952cd3b07SLee Schermerhorn 2140cc9a6c87SMel Gorman retry_cpuset: 2141cc9a6c87SMel Gorman cpuset_mems_cookie = get_mems_allowed(); 2142cc9a6c87SMel Gorman 214352cd3b07SLee Schermerhorn /* 214452cd3b07SLee Schermerhorn * No reference counting needed for current->mempolicy 214552cd3b07SLee Schermerhorn * nor system default_policy 214652cd3b07SLee Schermerhorn */ 214745c4745aSLee Schermerhorn if (pol->mode == MPOL_INTERLEAVE) 2148c0ff7453SMiao Xie page = alloc_page_interleave(gfp, order, interleave_nodes(pol)); 2149c0ff7453SMiao Xie else 2150c0ff7453SMiao Xie page = __alloc_pages_nodemask(gfp, order, 21515c4b4be3SAndi Kleen policy_zonelist(gfp, pol, numa_node_id()), 21525c4b4be3SAndi Kleen policy_nodemask(gfp, pol)); 2153cc9a6c87SMel Gorman 2154cc9a6c87SMel Gorman if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page)) 2155cc9a6c87SMel Gorman goto retry_cpuset; 2156cc9a6c87SMel Gorman 2157c0ff7453SMiao Xie return page; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_pages_current); 21601da177e4SLinus Torvalds 21614225399aSPaul Jackson /* 2162846a16bfSLee Schermerhorn * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it 21634225399aSPaul Jackson * rebinds the mempolicy its copying by calling mpol_rebind_policy() 21644225399aSPaul Jackson * with the mems_allowed returned by cpuset_mems_allowed(). This 21654225399aSPaul Jackson * keeps mempolicies cpuset relative after its cpuset moves. See 21664225399aSPaul Jackson * further kernel/cpuset.c update_nodemask(). 2167708c1bbcSMiao Xie * 2168708c1bbcSMiao Xie * current's mempolicy may be rebinded by the other task(the task that changes 2169708c1bbcSMiao Xie * cpuset's mems), so we needn't do rebind work for current task. 21704225399aSPaul Jackson */ 21714225399aSPaul Jackson 2172846a16bfSLee Schermerhorn /* Slow path of a mempolicy duplicate */ 2173846a16bfSLee Schermerhorn struct mempolicy *__mpol_dup(struct mempolicy *old) 21741da177e4SLinus Torvalds { 21751da177e4SLinus Torvalds struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL); 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds if (!new) 21781da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 2179708c1bbcSMiao Xie 2180708c1bbcSMiao Xie /* task's mempolicy is protected by alloc_lock */ 2181708c1bbcSMiao Xie if (old == current->mempolicy) { 2182708c1bbcSMiao Xie task_lock(current); 2183708c1bbcSMiao Xie *new = *old; 2184708c1bbcSMiao Xie task_unlock(current); 2185708c1bbcSMiao Xie } else 2186708c1bbcSMiao Xie *new = *old; 2187708c1bbcSMiao Xie 218899ee4ca7SPaul E. McKenney rcu_read_lock(); 21894225399aSPaul Jackson if (current_cpuset_is_being_rebound()) { 21904225399aSPaul Jackson nodemask_t mems = cpuset_mems_allowed(current); 2191708c1bbcSMiao Xie if (new->flags & MPOL_F_REBINDING) 2192708c1bbcSMiao Xie mpol_rebind_policy(new, &mems, MPOL_REBIND_STEP2); 2193708c1bbcSMiao Xie else 2194708c1bbcSMiao Xie mpol_rebind_policy(new, &mems, MPOL_REBIND_ONCE); 21954225399aSPaul Jackson } 219699ee4ca7SPaul E. McKenney rcu_read_unlock(); 21971da177e4SLinus Torvalds atomic_set(&new->refcnt, 1); 21981da177e4SLinus Torvalds return new; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 220152cd3b07SLee Schermerhorn /* 220252cd3b07SLee Schermerhorn * If *frompol needs [has] an extra ref, copy *frompol to *tompol , 220352cd3b07SLee Schermerhorn * eliminate the * MPOL_F_* flags that require conditional ref and 220452cd3b07SLee Schermerhorn * [NOTE!!!] drop the extra ref. Not safe to reference *frompol directly 220552cd3b07SLee Schermerhorn * after return. Use the returned value. 220652cd3b07SLee Schermerhorn * 220752cd3b07SLee Schermerhorn * Allows use of a mempolicy for, e.g., multiple allocations with a single 220852cd3b07SLee Schermerhorn * policy lookup, even if the policy needs/has extra ref on lookup. 220952cd3b07SLee Schermerhorn * shmem_readahead needs this. 221052cd3b07SLee Schermerhorn */ 221152cd3b07SLee Schermerhorn struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol, 221252cd3b07SLee Schermerhorn struct mempolicy *frompol) 221352cd3b07SLee Schermerhorn { 221452cd3b07SLee Schermerhorn if (!mpol_needs_cond_ref(frompol)) 221552cd3b07SLee Schermerhorn return frompol; 221652cd3b07SLee Schermerhorn 221752cd3b07SLee Schermerhorn *tompol = *frompol; 221852cd3b07SLee Schermerhorn tompol->flags &= ~MPOL_F_SHARED; /* copy doesn't need unref */ 221952cd3b07SLee Schermerhorn __mpol_put(frompol); 222052cd3b07SLee Schermerhorn return tompol; 222152cd3b07SLee Schermerhorn } 222252cd3b07SLee Schermerhorn 22231da177e4SLinus Torvalds /* Slow path of a mempolicy comparison */ 2224fcfb4dccSKOSAKI Motohiro bool __mpol_equal(struct mempolicy *a, struct mempolicy *b) 22251da177e4SLinus Torvalds { 22261da177e4SLinus Torvalds if (!a || !b) 2227fcfb4dccSKOSAKI Motohiro return false; 222845c4745aSLee Schermerhorn if (a->mode != b->mode) 2229fcfb4dccSKOSAKI Motohiro return false; 223019800502SBob Liu if (a->flags != b->flags) 2231fcfb4dccSKOSAKI Motohiro return false; 223219800502SBob Liu if (mpol_store_user_nodemask(a)) 223319800502SBob Liu if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask)) 2234fcfb4dccSKOSAKI Motohiro return false; 223519800502SBob Liu 223645c4745aSLee Schermerhorn switch (a->mode) { 223719770b32SMel Gorman case MPOL_BIND: 223819770b32SMel Gorman /* Fall through */ 22391da177e4SLinus Torvalds case MPOL_INTERLEAVE: 2240fcfb4dccSKOSAKI Motohiro return !!nodes_equal(a->v.nodes, b->v.nodes); 22411da177e4SLinus Torvalds case MPOL_PREFERRED: 224275719661SNamhyung Kim return a->v.preferred_node == b->v.preferred_node; 22431da177e4SLinus Torvalds default: 22441da177e4SLinus Torvalds BUG(); 2245fcfb4dccSKOSAKI Motohiro return false; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds /* 22501da177e4SLinus Torvalds * Shared memory backing store policy support. 22511da177e4SLinus Torvalds * 22521da177e4SLinus Torvalds * Remember policies even when nobody has shared memory mapped. 22531da177e4SLinus Torvalds * The policies are kept in Red-Black tree linked from the inode. 22541da177e4SLinus Torvalds * They are protected by the sp->lock spinlock, which should be held 22551da177e4SLinus Torvalds * for any accesses to the tree. 22561da177e4SLinus Torvalds */ 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds /* lookup first element intersecting start-end */ 2259b22d127aSMel Gorman /* Caller holds sp->mutex */ 22601da177e4SLinus Torvalds static struct sp_node * 22611da177e4SLinus Torvalds sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end) 22621da177e4SLinus Torvalds { 22631da177e4SLinus Torvalds struct rb_node *n = sp->root.rb_node; 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds while (n) { 22661da177e4SLinus Torvalds struct sp_node *p = rb_entry(n, struct sp_node, nd); 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds if (start >= p->end) 22691da177e4SLinus Torvalds n = n->rb_right; 22701da177e4SLinus Torvalds else if (end <= p->start) 22711da177e4SLinus Torvalds n = n->rb_left; 22721da177e4SLinus Torvalds else 22731da177e4SLinus Torvalds break; 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds if (!n) 22761da177e4SLinus Torvalds return NULL; 22771da177e4SLinus Torvalds for (;;) { 22781da177e4SLinus Torvalds struct sp_node *w = NULL; 22791da177e4SLinus Torvalds struct rb_node *prev = rb_prev(n); 22801da177e4SLinus Torvalds if (!prev) 22811da177e4SLinus Torvalds break; 22821da177e4SLinus Torvalds w = rb_entry(prev, struct sp_node, nd); 22831da177e4SLinus Torvalds if (w->end <= start) 22841da177e4SLinus Torvalds break; 22851da177e4SLinus Torvalds n = prev; 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds return rb_entry(n, struct sp_node, nd); 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds /* Insert a new shared policy into the list. */ 22911da177e4SLinus Torvalds /* Caller holds sp->lock */ 22921da177e4SLinus Torvalds static void sp_insert(struct shared_policy *sp, struct sp_node *new) 22931da177e4SLinus Torvalds { 22941da177e4SLinus Torvalds struct rb_node **p = &sp->root.rb_node; 22951da177e4SLinus Torvalds struct rb_node *parent = NULL; 22961da177e4SLinus Torvalds struct sp_node *nd; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds while (*p) { 22991da177e4SLinus Torvalds parent = *p; 23001da177e4SLinus Torvalds nd = rb_entry(parent, struct sp_node, nd); 23011da177e4SLinus Torvalds if (new->start < nd->start) 23021da177e4SLinus Torvalds p = &(*p)->rb_left; 23031da177e4SLinus Torvalds else if (new->end > nd->end) 23041da177e4SLinus Torvalds p = &(*p)->rb_right; 23051da177e4SLinus Torvalds else 23061da177e4SLinus Torvalds BUG(); 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds rb_link_node(&new->nd, parent, p); 23091da177e4SLinus Torvalds rb_insert_color(&new->nd, &sp->root); 2310140d5a49SPaul Mundt pr_debug("inserting %lx-%lx: %d\n", new->start, new->end, 231145c4745aSLee Schermerhorn new->policy ? new->policy->mode : 0); 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds /* Find shared policy intersecting idx */ 23151da177e4SLinus Torvalds struct mempolicy * 23161da177e4SLinus Torvalds mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) 23171da177e4SLinus Torvalds { 23181da177e4SLinus Torvalds struct mempolicy *pol = NULL; 23191da177e4SLinus Torvalds struct sp_node *sn; 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds if (!sp->root.rb_node) 23221da177e4SLinus Torvalds return NULL; 2323b22d127aSMel Gorman mutex_lock(&sp->mutex); 23241da177e4SLinus Torvalds sn = sp_lookup(sp, idx, idx+1); 23251da177e4SLinus Torvalds if (sn) { 23261da177e4SLinus Torvalds mpol_get(sn->policy); 23271da177e4SLinus Torvalds pol = sn->policy; 23281da177e4SLinus Torvalds } 2329b22d127aSMel Gorman mutex_unlock(&sp->mutex); 23301da177e4SLinus Torvalds return pol; 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 233363f74ca2SKOSAKI Motohiro static void sp_free(struct sp_node *n) 233463f74ca2SKOSAKI Motohiro { 233563f74ca2SKOSAKI Motohiro mpol_put(n->policy); 233663f74ca2SKOSAKI Motohiro kmem_cache_free(sn_cache, n); 233763f74ca2SKOSAKI Motohiro } 233863f74ca2SKOSAKI Motohiro 2339771fb4d8SLee Schermerhorn /** 2340771fb4d8SLee Schermerhorn * mpol_misplaced - check whether current page node is valid in policy 2341771fb4d8SLee Schermerhorn * 2342771fb4d8SLee Schermerhorn * @page - page to be checked 2343771fb4d8SLee Schermerhorn * @vma - vm area where page mapped 2344771fb4d8SLee Schermerhorn * @addr - virtual address where page mapped 2345771fb4d8SLee Schermerhorn * 2346771fb4d8SLee Schermerhorn * Lookup current policy node id for vma,addr and "compare to" page's 2347771fb4d8SLee Schermerhorn * node id. 2348771fb4d8SLee Schermerhorn * 2349771fb4d8SLee Schermerhorn * Returns: 2350771fb4d8SLee Schermerhorn * -1 - not misplaced, page is in the right node 2351771fb4d8SLee Schermerhorn * node - node id where the page should be 2352771fb4d8SLee Schermerhorn * 2353771fb4d8SLee Schermerhorn * Policy determination "mimics" alloc_page_vma(). 2354771fb4d8SLee Schermerhorn * Called from fault path where we know the vma and faulting address. 2355771fb4d8SLee Schermerhorn */ 2356771fb4d8SLee Schermerhorn int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long addr) 2357771fb4d8SLee Schermerhorn { 2358771fb4d8SLee Schermerhorn struct mempolicy *pol; 2359771fb4d8SLee Schermerhorn struct zone *zone; 2360771fb4d8SLee Schermerhorn int curnid = page_to_nid(page); 2361771fb4d8SLee Schermerhorn unsigned long pgoff; 2362771fb4d8SLee Schermerhorn int polnid = -1; 2363771fb4d8SLee Schermerhorn int ret = -1; 2364771fb4d8SLee Schermerhorn 2365771fb4d8SLee Schermerhorn BUG_ON(!vma); 2366771fb4d8SLee Schermerhorn 2367771fb4d8SLee Schermerhorn pol = get_vma_policy(current, vma, addr); 2368771fb4d8SLee Schermerhorn if (!(pol->flags & MPOL_F_MOF)) 2369771fb4d8SLee Schermerhorn goto out; 2370771fb4d8SLee Schermerhorn 2371771fb4d8SLee Schermerhorn switch (pol->mode) { 2372771fb4d8SLee Schermerhorn case MPOL_INTERLEAVE: 2373771fb4d8SLee Schermerhorn BUG_ON(addr >= vma->vm_end); 2374771fb4d8SLee Schermerhorn BUG_ON(addr < vma->vm_start); 2375771fb4d8SLee Schermerhorn 2376771fb4d8SLee Schermerhorn pgoff = vma->vm_pgoff; 2377771fb4d8SLee Schermerhorn pgoff += (addr - vma->vm_start) >> PAGE_SHIFT; 2378771fb4d8SLee Schermerhorn polnid = offset_il_node(pol, vma, pgoff); 2379771fb4d8SLee Schermerhorn break; 2380771fb4d8SLee Schermerhorn 2381771fb4d8SLee Schermerhorn case MPOL_PREFERRED: 2382771fb4d8SLee Schermerhorn if (pol->flags & MPOL_F_LOCAL) 2383771fb4d8SLee Schermerhorn polnid = numa_node_id(); 2384771fb4d8SLee Schermerhorn else 2385771fb4d8SLee Schermerhorn polnid = pol->v.preferred_node; 2386771fb4d8SLee Schermerhorn break; 2387771fb4d8SLee Schermerhorn 2388771fb4d8SLee Schermerhorn case MPOL_BIND: 2389771fb4d8SLee Schermerhorn /* 2390771fb4d8SLee Schermerhorn * allows binding to multiple nodes. 2391771fb4d8SLee Schermerhorn * use current page if in policy nodemask, 2392771fb4d8SLee Schermerhorn * else select nearest allowed node, if any. 2393771fb4d8SLee Schermerhorn * If no allowed nodes, use current [!misplaced]. 2394771fb4d8SLee Schermerhorn */ 2395771fb4d8SLee Schermerhorn if (node_isset(curnid, pol->v.nodes)) 2396771fb4d8SLee Schermerhorn goto out; 2397771fb4d8SLee Schermerhorn (void)first_zones_zonelist( 2398771fb4d8SLee Schermerhorn node_zonelist(numa_node_id(), GFP_HIGHUSER), 2399771fb4d8SLee Schermerhorn gfp_zone(GFP_HIGHUSER), 2400771fb4d8SLee Schermerhorn &pol->v.nodes, &zone); 2401771fb4d8SLee Schermerhorn polnid = zone->node; 2402771fb4d8SLee Schermerhorn break; 2403771fb4d8SLee Schermerhorn 2404771fb4d8SLee Schermerhorn default: 2405771fb4d8SLee Schermerhorn BUG(); 2406771fb4d8SLee Schermerhorn } 2407771fb4d8SLee Schermerhorn if (curnid != polnid) 2408771fb4d8SLee Schermerhorn ret = polnid; 2409771fb4d8SLee Schermerhorn out: 2410771fb4d8SLee Schermerhorn mpol_cond_put(pol); 2411771fb4d8SLee Schermerhorn 2412771fb4d8SLee Schermerhorn return ret; 2413771fb4d8SLee Schermerhorn } 2414771fb4d8SLee Schermerhorn 24151da177e4SLinus Torvalds static void sp_delete(struct shared_policy *sp, struct sp_node *n) 24161da177e4SLinus Torvalds { 2417140d5a49SPaul Mundt pr_debug("deleting %lx-l%lx\n", n->start, n->end); 24181da177e4SLinus Torvalds rb_erase(&n->nd, &sp->root); 241963f74ca2SKOSAKI Motohiro sp_free(n); 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds 2422dbcb0f19SAdrian Bunk static struct sp_node *sp_alloc(unsigned long start, unsigned long end, 2423dbcb0f19SAdrian Bunk struct mempolicy *pol) 24241da177e4SLinus Torvalds { 2425869833f2SKOSAKI Motohiro struct sp_node *n; 2426869833f2SKOSAKI Motohiro struct mempolicy *newpol; 24271da177e4SLinus Torvalds 2428869833f2SKOSAKI Motohiro n = kmem_cache_alloc(sn_cache, GFP_KERNEL); 24291da177e4SLinus Torvalds if (!n) 24301da177e4SLinus Torvalds return NULL; 2431869833f2SKOSAKI Motohiro 2432869833f2SKOSAKI Motohiro newpol = mpol_dup(pol); 2433869833f2SKOSAKI Motohiro if (IS_ERR(newpol)) { 2434869833f2SKOSAKI Motohiro kmem_cache_free(sn_cache, n); 2435869833f2SKOSAKI Motohiro return NULL; 2436869833f2SKOSAKI Motohiro } 2437869833f2SKOSAKI Motohiro newpol->flags |= MPOL_F_SHARED; 2438869833f2SKOSAKI Motohiro 24391da177e4SLinus Torvalds n->start = start; 24401da177e4SLinus Torvalds n->end = end; 2441869833f2SKOSAKI Motohiro n->policy = newpol; 2442869833f2SKOSAKI Motohiro 24431da177e4SLinus Torvalds return n; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds /* Replace a policy range. */ 24471da177e4SLinus Torvalds static int shared_policy_replace(struct shared_policy *sp, unsigned long start, 24481da177e4SLinus Torvalds unsigned long end, struct sp_node *new) 24491da177e4SLinus Torvalds { 2450b22d127aSMel Gorman struct sp_node *n; 2451b22d127aSMel Gorman int ret = 0; 24521da177e4SLinus Torvalds 2453b22d127aSMel Gorman mutex_lock(&sp->mutex); 24541da177e4SLinus Torvalds n = sp_lookup(sp, start, end); 24551da177e4SLinus Torvalds /* Take care of old policies in the same range. */ 24561da177e4SLinus Torvalds while (n && n->start < end) { 24571da177e4SLinus Torvalds struct rb_node *next = rb_next(&n->nd); 24581da177e4SLinus Torvalds if (n->start >= start) { 24591da177e4SLinus Torvalds if (n->end <= end) 24601da177e4SLinus Torvalds sp_delete(sp, n); 24611da177e4SLinus Torvalds else 24621da177e4SLinus Torvalds n->start = end; 24631da177e4SLinus Torvalds } else { 24641da177e4SLinus Torvalds /* Old policy spanning whole new range. */ 24651da177e4SLinus Torvalds if (n->end > end) { 2466b22d127aSMel Gorman struct sp_node *new2; 24671da177e4SLinus Torvalds new2 = sp_alloc(end, n->end, n->policy); 2468b22d127aSMel Gorman if (!new2) { 2469b22d127aSMel Gorman ret = -ENOMEM; 2470b22d127aSMel Gorman goto out; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds n->end = start; 24731da177e4SLinus Torvalds sp_insert(sp, new2); 24741da177e4SLinus Torvalds break; 24751da177e4SLinus Torvalds } else 24761da177e4SLinus Torvalds n->end = start; 24771da177e4SLinus Torvalds } 24781da177e4SLinus Torvalds if (!next) 24791da177e4SLinus Torvalds break; 24801da177e4SLinus Torvalds n = rb_entry(next, struct sp_node, nd); 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds if (new) 24831da177e4SLinus Torvalds sp_insert(sp, new); 2484b22d127aSMel Gorman out: 2485b22d127aSMel Gorman mutex_unlock(&sp->mutex); 2486b22d127aSMel Gorman return ret; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 248971fe804bSLee Schermerhorn /** 249071fe804bSLee Schermerhorn * mpol_shared_policy_init - initialize shared policy for inode 249171fe804bSLee Schermerhorn * @sp: pointer to inode shared policy 249271fe804bSLee Schermerhorn * @mpol: struct mempolicy to install 249371fe804bSLee Schermerhorn * 249471fe804bSLee Schermerhorn * Install non-NULL @mpol in inode's shared policy rb-tree. 249571fe804bSLee Schermerhorn * On entry, the current task has a reference on a non-NULL @mpol. 249671fe804bSLee Schermerhorn * This must be released on exit. 24974bfc4495SKAMEZAWA Hiroyuki * This is called at get_inode() calls and we can use GFP_KERNEL. 249871fe804bSLee Schermerhorn */ 249971fe804bSLee Schermerhorn void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) 25007339ff83SRobin Holt { 250158568d2aSMiao Xie int ret; 250258568d2aSMiao Xie 250371fe804bSLee Schermerhorn sp->root = RB_ROOT; /* empty tree == default mempolicy */ 2504b22d127aSMel Gorman mutex_init(&sp->mutex); 25057339ff83SRobin Holt 250671fe804bSLee Schermerhorn if (mpol) { 25077339ff83SRobin Holt struct vm_area_struct pvma; 250871fe804bSLee Schermerhorn struct mempolicy *new; 25094bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH(scratch); 25107339ff83SRobin Holt 25114bfc4495SKAMEZAWA Hiroyuki if (!scratch) 25125c0c1654SLee Schermerhorn goto put_mpol; 251371fe804bSLee Schermerhorn /* contextualize the tmpfs mount point mempolicy */ 251471fe804bSLee Schermerhorn new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask); 251515d77835SLee Schermerhorn if (IS_ERR(new)) 25160cae3457SDan Carpenter goto free_scratch; /* no valid nodemask intersection */ 251758568d2aSMiao Xie 251858568d2aSMiao Xie task_lock(current); 25194bfc4495SKAMEZAWA Hiroyuki ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch); 252058568d2aSMiao Xie task_unlock(current); 252115d77835SLee Schermerhorn if (ret) 25225c0c1654SLee Schermerhorn goto put_new; 252371fe804bSLee Schermerhorn 252471fe804bSLee Schermerhorn /* Create pseudo-vma that contains just the policy */ 25257339ff83SRobin Holt memset(&pvma, 0, sizeof(struct vm_area_struct)); 252671fe804bSLee Schermerhorn pvma.vm_end = TASK_SIZE; /* policy covers entire file */ 252771fe804bSLee Schermerhorn mpol_set_shared_policy(sp, &pvma, new); /* adds ref */ 252815d77835SLee Schermerhorn 25295c0c1654SLee Schermerhorn put_new: 253071fe804bSLee Schermerhorn mpol_put(new); /* drop initial ref */ 25310cae3457SDan Carpenter free_scratch: 25324bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH_FREE(scratch); 25335c0c1654SLee Schermerhorn put_mpol: 25345c0c1654SLee Schermerhorn mpol_put(mpol); /* drop our incoming ref on sb mpol */ 25357339ff83SRobin Holt } 25367339ff83SRobin Holt } 25377339ff83SRobin Holt 25381da177e4SLinus Torvalds int mpol_set_shared_policy(struct shared_policy *info, 25391da177e4SLinus Torvalds struct vm_area_struct *vma, struct mempolicy *npol) 25401da177e4SLinus Torvalds { 25411da177e4SLinus Torvalds int err; 25421da177e4SLinus Torvalds struct sp_node *new = NULL; 25431da177e4SLinus Torvalds unsigned long sz = vma_pages(vma); 25441da177e4SLinus Torvalds 2545028fec41SDavid Rientjes pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n", 25461da177e4SLinus Torvalds vma->vm_pgoff, 254745c4745aSLee Schermerhorn sz, npol ? npol->mode : -1, 2548028fec41SDavid Rientjes npol ? npol->flags : -1, 2549dfcd3c0dSAndi Kleen npol ? nodes_addr(npol->v.nodes)[0] : -1); 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds if (npol) { 25521da177e4SLinus Torvalds new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); 25531da177e4SLinus Torvalds if (!new) 25541da177e4SLinus Torvalds return -ENOMEM; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); 25571da177e4SLinus Torvalds if (err && new) 255863f74ca2SKOSAKI Motohiro sp_free(new); 25591da177e4SLinus Torvalds return err; 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds /* Free a backing policy store on inode delete. */ 25631da177e4SLinus Torvalds void mpol_free_shared_policy(struct shared_policy *p) 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds struct sp_node *n; 25661da177e4SLinus Torvalds struct rb_node *next; 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds if (!p->root.rb_node) 25691da177e4SLinus Torvalds return; 2570b22d127aSMel Gorman mutex_lock(&p->mutex); 25711da177e4SLinus Torvalds next = rb_first(&p->root); 25721da177e4SLinus Torvalds while (next) { 25731da177e4SLinus Torvalds n = rb_entry(next, struct sp_node, nd); 25741da177e4SLinus Torvalds next = rb_next(&n->nd); 257563f74ca2SKOSAKI Motohiro sp_delete(p, n); 25761da177e4SLinus Torvalds } 2577b22d127aSMel Gorman mutex_unlock(&p->mutex); 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds /* assumes fs == KERNEL_DS */ 25811da177e4SLinus Torvalds void __init numa_policy_init(void) 25821da177e4SLinus Torvalds { 2583b71636e2SPaul Mundt nodemask_t interleave_nodes; 2584b71636e2SPaul Mundt unsigned long largest = 0; 2585b71636e2SPaul Mundt int nid, prefer = 0; 2586b71636e2SPaul Mundt 25871da177e4SLinus Torvalds policy_cache = kmem_cache_create("numa_policy", 25881da177e4SLinus Torvalds sizeof(struct mempolicy), 258920c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds sn_cache = kmem_cache_create("shared_policy_node", 25921da177e4SLinus Torvalds sizeof(struct sp_node), 259320c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 25941da177e4SLinus Torvalds 2595b71636e2SPaul Mundt /* 2596b71636e2SPaul Mundt * Set interleaving policy for system init. Interleaving is only 2597b71636e2SPaul Mundt * enabled across suitably sized nodes (default is >= 16MB), or 2598b71636e2SPaul Mundt * fall back to the largest node if they're all smaller. 2599b71636e2SPaul Mundt */ 2600b71636e2SPaul Mundt nodes_clear(interleave_nodes); 260156bbd65dSChristoph Lameter for_each_node_state(nid, N_HIGH_MEMORY) { 2602b71636e2SPaul Mundt unsigned long total_pages = node_present_pages(nid); 26031da177e4SLinus Torvalds 2604b71636e2SPaul Mundt /* Preserve the largest node */ 2605b71636e2SPaul Mundt if (largest < total_pages) { 2606b71636e2SPaul Mundt largest = total_pages; 2607b71636e2SPaul Mundt prefer = nid; 2608b71636e2SPaul Mundt } 2609b71636e2SPaul Mundt 2610b71636e2SPaul Mundt /* Interleave this node? */ 2611b71636e2SPaul Mundt if ((total_pages << PAGE_SHIFT) >= (16 << 20)) 2612b71636e2SPaul Mundt node_set(nid, interleave_nodes); 2613b71636e2SPaul Mundt } 2614b71636e2SPaul Mundt 2615b71636e2SPaul Mundt /* All too small, use the largest */ 2616b71636e2SPaul Mundt if (unlikely(nodes_empty(interleave_nodes))) 2617b71636e2SPaul Mundt node_set(prefer, interleave_nodes); 2618b71636e2SPaul Mundt 2619028fec41SDavid Rientjes if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes)) 26201da177e4SLinus Torvalds printk("numa_policy_init: interleaving failed\n"); 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 26238bccd85fSChristoph Lameter /* Reset policy of current process to default */ 26241da177e4SLinus Torvalds void numa_default_policy(void) 26251da177e4SLinus Torvalds { 2626028fec41SDavid Rientjes do_set_mempolicy(MPOL_DEFAULT, 0, NULL); 26271da177e4SLinus Torvalds } 262868860ec1SPaul Jackson 26294225399aSPaul Jackson /* 2630095f1fc4SLee Schermerhorn * Parse and format mempolicy from/to strings 2631095f1fc4SLee Schermerhorn */ 2632095f1fc4SLee Schermerhorn 2633095f1fc4SLee Schermerhorn /* 2634fc36b8d3SLee Schermerhorn * "local" is pseudo-policy: MPOL_PREFERRED with MPOL_F_LOCAL flag 26353f226aa1SLee Schermerhorn * Used only for mpol_parse_str() and mpol_to_str() 26361a75a6c8SChristoph Lameter */ 2637345ace9cSLee Schermerhorn static const char * const policy_modes[] = 2638345ace9cSLee Schermerhorn { 2639345ace9cSLee Schermerhorn [MPOL_DEFAULT] = "default", 2640345ace9cSLee Schermerhorn [MPOL_PREFERRED] = "prefer", 2641345ace9cSLee Schermerhorn [MPOL_BIND] = "bind", 2642345ace9cSLee Schermerhorn [MPOL_INTERLEAVE] = "interleave", 2643d3a71033SLee Schermerhorn [MPOL_LOCAL] = "local", 2644d3a71033SLee Schermerhorn [MPOL_NOOP] = "noop", /* should not actually be used */ 2645345ace9cSLee Schermerhorn }; 26461a75a6c8SChristoph Lameter 2647095f1fc4SLee Schermerhorn 2648095f1fc4SLee Schermerhorn #ifdef CONFIG_TMPFS 2649095f1fc4SLee Schermerhorn /** 2650095f1fc4SLee Schermerhorn * mpol_parse_str - parse string to mempolicy 2651095f1fc4SLee Schermerhorn * @str: string containing mempolicy to parse 265271fe804bSLee Schermerhorn * @mpol: pointer to struct mempolicy pointer, returned on success. 265371fe804bSLee Schermerhorn * @no_context: flag whether to "contextualize" the mempolicy 2654095f1fc4SLee Schermerhorn * 2655095f1fc4SLee Schermerhorn * Format of input: 2656095f1fc4SLee Schermerhorn * <mode>[=<flags>][:<nodelist>] 2657095f1fc4SLee Schermerhorn * 265871fe804bSLee Schermerhorn * if @no_context is true, save the input nodemask in w.user_nodemask in 265971fe804bSLee Schermerhorn * the returned mempolicy. This will be used to "clone" the mempolicy in 266071fe804bSLee Schermerhorn * a specific context [cpuset] at a later time. Used to parse tmpfs mpol 266171fe804bSLee Schermerhorn * mount option. Note that if 'static' or 'relative' mode flags were 266271fe804bSLee Schermerhorn * specified, the input nodemask will already have been saved. Saving 266371fe804bSLee Schermerhorn * it again is redundant, but safe. 266471fe804bSLee Schermerhorn * 266571fe804bSLee Schermerhorn * On success, returns 0, else 1 2666095f1fc4SLee Schermerhorn */ 266771fe804bSLee Schermerhorn int mpol_parse_str(char *str, struct mempolicy **mpol, int no_context) 2668095f1fc4SLee Schermerhorn { 266971fe804bSLee Schermerhorn struct mempolicy *new = NULL; 2670b4652e84SLee Schermerhorn unsigned short mode; 267171fe804bSLee Schermerhorn unsigned short uninitialized_var(mode_flags); 267271fe804bSLee Schermerhorn nodemask_t nodes; 2673095f1fc4SLee Schermerhorn char *nodelist = strchr(str, ':'); 2674095f1fc4SLee Schermerhorn char *flags = strchr(str, '='); 2675095f1fc4SLee Schermerhorn int err = 1; 2676095f1fc4SLee Schermerhorn 2677095f1fc4SLee Schermerhorn if (nodelist) { 2678095f1fc4SLee Schermerhorn /* NUL-terminate mode or flags string */ 2679095f1fc4SLee Schermerhorn *nodelist++ = '\0'; 268071fe804bSLee Schermerhorn if (nodelist_parse(nodelist, nodes)) 2681095f1fc4SLee Schermerhorn goto out; 268271fe804bSLee Schermerhorn if (!nodes_subset(nodes, node_states[N_HIGH_MEMORY])) 2683095f1fc4SLee Schermerhorn goto out; 268471fe804bSLee Schermerhorn } else 268571fe804bSLee Schermerhorn nodes_clear(nodes); 268671fe804bSLee Schermerhorn 2687095f1fc4SLee Schermerhorn if (flags) 2688095f1fc4SLee Schermerhorn *flags++ = '\0'; /* terminate mode string */ 2689095f1fc4SLee Schermerhorn 2690479e2802SPeter Zijlstra for (mode = 0; mode < MPOL_MAX; mode++) { 2691345ace9cSLee Schermerhorn if (!strcmp(str, policy_modes[mode])) { 2692095f1fc4SLee Schermerhorn break; 2693095f1fc4SLee Schermerhorn } 2694095f1fc4SLee Schermerhorn } 2695d3a71033SLee Schermerhorn if (mode >= MPOL_MAX || mode == MPOL_NOOP) 2696095f1fc4SLee Schermerhorn goto out; 2697095f1fc4SLee Schermerhorn 269871fe804bSLee Schermerhorn switch (mode) { 2699095f1fc4SLee Schermerhorn case MPOL_PREFERRED: 270071fe804bSLee Schermerhorn /* 270171fe804bSLee Schermerhorn * Insist on a nodelist of one node only 270271fe804bSLee Schermerhorn */ 2703095f1fc4SLee Schermerhorn if (nodelist) { 2704095f1fc4SLee Schermerhorn char *rest = nodelist; 2705095f1fc4SLee Schermerhorn while (isdigit(*rest)) 2706095f1fc4SLee Schermerhorn rest++; 2707926f2ae0SKOSAKI Motohiro if (*rest) 2708926f2ae0SKOSAKI Motohiro goto out; 2709095f1fc4SLee Schermerhorn } 2710095f1fc4SLee Schermerhorn break; 2711095f1fc4SLee Schermerhorn case MPOL_INTERLEAVE: 2712095f1fc4SLee Schermerhorn /* 2713095f1fc4SLee Schermerhorn * Default to online nodes with memory if no nodelist 2714095f1fc4SLee Schermerhorn */ 2715095f1fc4SLee Schermerhorn if (!nodelist) 271671fe804bSLee Schermerhorn nodes = node_states[N_HIGH_MEMORY]; 27173f226aa1SLee Schermerhorn break; 271871fe804bSLee Schermerhorn case MPOL_LOCAL: 27193f226aa1SLee Schermerhorn /* 272071fe804bSLee Schermerhorn * Don't allow a nodelist; mpol_new() checks flags 27213f226aa1SLee Schermerhorn */ 272271fe804bSLee Schermerhorn if (nodelist) 27233f226aa1SLee Schermerhorn goto out; 272471fe804bSLee Schermerhorn mode = MPOL_PREFERRED; 27253f226aa1SLee Schermerhorn break; 2726413b43deSRavikiran G Thirumalai case MPOL_DEFAULT: 2727413b43deSRavikiran G Thirumalai /* 2728413b43deSRavikiran G Thirumalai * Insist on a empty nodelist 2729413b43deSRavikiran G Thirumalai */ 2730413b43deSRavikiran G Thirumalai if (!nodelist) 2731413b43deSRavikiran G Thirumalai err = 0; 2732413b43deSRavikiran G Thirumalai goto out; 2733d69b2e63SKOSAKI Motohiro case MPOL_BIND: 273471fe804bSLee Schermerhorn /* 2735d69b2e63SKOSAKI Motohiro * Insist on a nodelist 273671fe804bSLee Schermerhorn */ 2737d69b2e63SKOSAKI Motohiro if (!nodelist) 2738d69b2e63SKOSAKI Motohiro goto out; 2739095f1fc4SLee Schermerhorn } 2740095f1fc4SLee Schermerhorn 274171fe804bSLee Schermerhorn mode_flags = 0; 2742095f1fc4SLee Schermerhorn if (flags) { 2743095f1fc4SLee Schermerhorn /* 2744095f1fc4SLee Schermerhorn * Currently, we only support two mutually exclusive 2745095f1fc4SLee Schermerhorn * mode flags. 2746095f1fc4SLee Schermerhorn */ 2747095f1fc4SLee Schermerhorn if (!strcmp(flags, "static")) 274871fe804bSLee Schermerhorn mode_flags |= MPOL_F_STATIC_NODES; 2749095f1fc4SLee Schermerhorn else if (!strcmp(flags, "relative")) 275071fe804bSLee Schermerhorn mode_flags |= MPOL_F_RELATIVE_NODES; 2751095f1fc4SLee Schermerhorn else 2752926f2ae0SKOSAKI Motohiro goto out; 2753095f1fc4SLee Schermerhorn } 275471fe804bSLee Schermerhorn 275571fe804bSLee Schermerhorn new = mpol_new(mode, mode_flags, &nodes); 275671fe804bSLee Schermerhorn if (IS_ERR(new)) 2757926f2ae0SKOSAKI Motohiro goto out; 2758926f2ae0SKOSAKI Motohiro 2759e17f74afSLee Schermerhorn if (no_context) { 2760e17f74afSLee Schermerhorn /* save for contextualization */ 2761e17f74afSLee Schermerhorn new->w.user_nodemask = nodes; 2762e17f74afSLee Schermerhorn } else { 276358568d2aSMiao Xie int ret; 27644bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH(scratch); 27654bfc4495SKAMEZAWA Hiroyuki if (scratch) { 276658568d2aSMiao Xie task_lock(current); 27674bfc4495SKAMEZAWA Hiroyuki ret = mpol_set_nodemask(new, &nodes, scratch); 276858568d2aSMiao Xie task_unlock(current); 27694bfc4495SKAMEZAWA Hiroyuki } else 27704bfc4495SKAMEZAWA Hiroyuki ret = -ENOMEM; 27714bfc4495SKAMEZAWA Hiroyuki NODEMASK_SCRATCH_FREE(scratch); 27724bfc4495SKAMEZAWA Hiroyuki if (ret) { 27734bfc4495SKAMEZAWA Hiroyuki mpol_put(new); 2774926f2ae0SKOSAKI Motohiro goto out; 2775926f2ae0SKOSAKI Motohiro } 2776926f2ae0SKOSAKI Motohiro } 2777926f2ae0SKOSAKI Motohiro err = 0; 277871fe804bSLee Schermerhorn 2779095f1fc4SLee Schermerhorn out: 2780095f1fc4SLee Schermerhorn /* Restore string for error message */ 2781095f1fc4SLee Schermerhorn if (nodelist) 2782095f1fc4SLee Schermerhorn *--nodelist = ':'; 2783095f1fc4SLee Schermerhorn if (flags) 2784095f1fc4SLee Schermerhorn *--flags = '='; 278571fe804bSLee Schermerhorn if (!err) 278671fe804bSLee Schermerhorn *mpol = new; 2787095f1fc4SLee Schermerhorn return err; 2788095f1fc4SLee Schermerhorn } 2789095f1fc4SLee Schermerhorn #endif /* CONFIG_TMPFS */ 2790095f1fc4SLee Schermerhorn 279171fe804bSLee Schermerhorn /** 279271fe804bSLee Schermerhorn * mpol_to_str - format a mempolicy structure for printing 279371fe804bSLee Schermerhorn * @buffer: to contain formatted mempolicy string 279471fe804bSLee Schermerhorn * @maxlen: length of @buffer 279571fe804bSLee Schermerhorn * @pol: pointer to mempolicy to be formatted 279671fe804bSLee Schermerhorn * @no_context: "context free" mempolicy - use nodemask in w.user_nodemask 279771fe804bSLee Schermerhorn * 27981a75a6c8SChristoph Lameter * Convert a mempolicy into a string. 27991a75a6c8SChristoph Lameter * Returns the number of characters in buffer (if positive) 28001a75a6c8SChristoph Lameter * or an error (negative) 28011a75a6c8SChristoph Lameter */ 280271fe804bSLee Schermerhorn int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol, int no_context) 28031a75a6c8SChristoph Lameter { 28041a75a6c8SChristoph Lameter char *p = buffer; 28051a75a6c8SChristoph Lameter int l; 28061a75a6c8SChristoph Lameter nodemask_t nodes; 2807bea904d5SLee Schermerhorn unsigned short mode; 2808f5b087b5SDavid Rientjes unsigned short flags = pol ? pol->flags : 0; 28091a75a6c8SChristoph Lameter 28102291990aSLee Schermerhorn /* 28112291990aSLee Schermerhorn * Sanity check: room for longest mode, flag and some nodes 28122291990aSLee Schermerhorn */ 28132291990aSLee Schermerhorn VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16); 28142291990aSLee Schermerhorn 2815bea904d5SLee Schermerhorn if (!pol || pol == &default_policy) 2816bea904d5SLee Schermerhorn mode = MPOL_DEFAULT; 2817bea904d5SLee Schermerhorn else 2818bea904d5SLee Schermerhorn mode = pol->mode; 2819bea904d5SLee Schermerhorn 28201a75a6c8SChristoph Lameter switch (mode) { 28211a75a6c8SChristoph Lameter case MPOL_DEFAULT: 28221a75a6c8SChristoph Lameter nodes_clear(nodes); 28231a75a6c8SChristoph Lameter break; 28241a75a6c8SChristoph Lameter 28251a75a6c8SChristoph Lameter case MPOL_PREFERRED: 28261a75a6c8SChristoph Lameter nodes_clear(nodes); 2827fc36b8d3SLee Schermerhorn if (flags & MPOL_F_LOCAL) 282853f2556bSLee Schermerhorn mode = MPOL_LOCAL; /* pseudo-policy */ 282953f2556bSLee Schermerhorn else 2830fc36b8d3SLee Schermerhorn node_set(pol->v.preferred_node, nodes); 28311a75a6c8SChristoph Lameter break; 28321a75a6c8SChristoph Lameter 28331a75a6c8SChristoph Lameter case MPOL_BIND: 283419770b32SMel Gorman /* Fall through */ 28351a75a6c8SChristoph Lameter case MPOL_INTERLEAVE: 283671fe804bSLee Schermerhorn if (no_context) 283771fe804bSLee Schermerhorn nodes = pol->w.user_nodemask; 283871fe804bSLee Schermerhorn else 28391a75a6c8SChristoph Lameter nodes = pol->v.nodes; 28401a75a6c8SChristoph Lameter break; 28411a75a6c8SChristoph Lameter 28421a75a6c8SChristoph Lameter default: 284380de7c31SDave Jones return -EINVAL; 28441a75a6c8SChristoph Lameter } 28451a75a6c8SChristoph Lameter 2846345ace9cSLee Schermerhorn l = strlen(policy_modes[mode]); 28471a75a6c8SChristoph Lameter if (buffer + maxlen < p + l + 1) 28481a75a6c8SChristoph Lameter return -ENOSPC; 28491a75a6c8SChristoph Lameter 2850345ace9cSLee Schermerhorn strcpy(p, policy_modes[mode]); 28511a75a6c8SChristoph Lameter p += l; 28521a75a6c8SChristoph Lameter 2853fc36b8d3SLee Schermerhorn if (flags & MPOL_MODE_FLAGS) { 2854f5b087b5SDavid Rientjes if (buffer + maxlen < p + 2) 2855f5b087b5SDavid Rientjes return -ENOSPC; 2856f5b087b5SDavid Rientjes *p++ = '='; 2857f5b087b5SDavid Rientjes 28582291990aSLee Schermerhorn /* 28592291990aSLee Schermerhorn * Currently, the only defined flags are mutually exclusive 28602291990aSLee Schermerhorn */ 2861f5b087b5SDavid Rientjes if (flags & MPOL_F_STATIC_NODES) 28622291990aSLee Schermerhorn p += snprintf(p, buffer + maxlen - p, "static"); 28632291990aSLee Schermerhorn else if (flags & MPOL_F_RELATIVE_NODES) 28642291990aSLee Schermerhorn p += snprintf(p, buffer + maxlen - p, "relative"); 2865f5b087b5SDavid Rientjes } 2866f5b087b5SDavid Rientjes 28671a75a6c8SChristoph Lameter if (!nodes_empty(nodes)) { 28681a75a6c8SChristoph Lameter if (buffer + maxlen < p + 2) 28691a75a6c8SChristoph Lameter return -ENOSPC; 2870095f1fc4SLee Schermerhorn *p++ = ':'; 28711a75a6c8SChristoph Lameter p += nodelist_scnprintf(p, buffer + maxlen - p, nodes); 28721a75a6c8SChristoph Lameter } 28731a75a6c8SChristoph Lameter return p - buffer; 28741a75a6c8SChristoph Lameter } 2875