Merge tag 'firewire-update' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[linux-2.6-microblaze.git] / mm / mempolicy.c
index d79fa29..1592b08 100644 (file)
@@ -31,6 +31,9 @@
  *                but useful to set in a VMA when you have a non default
  *                process policy.
  *
+ * preferred many Try a set of nodes first before normal fallback. This is
+ *                similar to preferred without the special case.
+ *
  * default        Allocate on the local node first, or when on a VMA
  *                use the process policy. This is what Linux always did
  *               in a NUMA aware kernel and still does by, ahem, default.
@@ -121,8 +124,7 @@ enum zone_type policy_zone = 0;
  */
 static struct mempolicy default_policy = {
        .refcnt = ATOMIC_INIT(1), /* never free it */
-       .mode = MPOL_PREFERRED,
-       .flags = MPOL_F_LOCAL,
+       .mode = MPOL_LOCAL,
 };
 
 static struct mempolicy preferred_node_policy[MAX_NUMNODES];
@@ -190,38 +192,28 @@ static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
        nodes_onto(*ret, tmp, *rel);
 }
 
-static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
+static int mpol_new_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
 {
        if (nodes_empty(*nodes))
                return -EINVAL;
-       pol->v.nodes = *nodes;
+       pol->nodes = *nodes;
        return 0;
 }
 
 static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
-{
-       if (!nodes)
-               pol->flags |= MPOL_F_LOCAL;     /* local allocation */
-       else if (nodes_empty(*nodes))
-               return -EINVAL;                 /*  no allowed nodes */
-       else
-               pol->v.preferred_node = first_node(*nodes);
-       return 0;
-}
-
-static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
 {
        if (nodes_empty(*nodes))
                return -EINVAL;
-       pol->v.nodes = *nodes;
+
+       nodes_clear(pol->nodes);
+       node_set(first_node(*nodes), pol->nodes);
        return 0;
 }
 
 /*
  * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
  * any, for the new policy.  mpol_new() has already validated the nodes
- * parameter with respect to the policy mode and flags.  But, we need to
- * handle an empty nodemask with MPOL_PREFERRED here.
+ * parameter with respect to the policy mode and flags.
  *
  * Must be called holding task's alloc_lock to protect task's mems_allowed
  * and mempolicy.  May also be called holding the mmap_lock for write.
@@ -231,33 +223,31 @@ static int mpol_set_nodemask(struct mempolicy *pol,
 {
        int ret;
 
-       /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
-       if (pol == NULL)
+       /*
+        * Default (pol==NULL) resp. local memory policies are not a
+        * subject of any remapping. They also do not need any special
+        * constructor.
+        */
+       if (!pol || pol->mode == MPOL_LOCAL)
                return 0;
+
        /* Check N_MEMORY */
        nodes_and(nsc->mask1,
                  cpuset_current_mems_allowed, node_states[N_MEMORY]);
 
        VM_BUG_ON(!nodes);
-       if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
-               nodes = NULL;   /* explicit local allocation */
-       else {
-               if (pol->flags & MPOL_F_RELATIVE_NODES)
-                       mpol_relative_nodemask(&nsc->mask2, nodes, &nsc->mask1);
-               else
-                       nodes_and(nsc->mask2, *nodes, nsc->mask1);
 
-               if (mpol_store_user_nodemask(pol))
-                       pol->w.user_nodemask = *nodes;
-               else
-                       pol->w.cpuset_mems_allowed =
-                                               cpuset_current_mems_allowed;
-       }
+       if (pol->flags & MPOL_F_RELATIVE_NODES)
+               mpol_relative_nodemask(&nsc->mask2, nodes, &nsc->mask1);
+       else
+               nodes_and(nsc->mask2, *nodes, nsc->mask1);
 
-       if (nodes)
-               ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
+       if (mpol_store_user_nodemask(pol))
+               pol->w.user_nodemask = *nodes;
        else
-               ret = mpol_ops[pol->mode].create(pol, NULL);
+               pol->w.cpuset_mems_allowed = cpuset_current_mems_allowed;
+
+       ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
        return ret;
 }
 
@@ -290,13 +280,14 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
                        if (((flags & MPOL_F_STATIC_NODES) ||
                             (flags & MPOL_F_RELATIVE_NODES)))
                                return ERR_PTR(-EINVAL);
+
+                       mode = MPOL_LOCAL;
                }
        } else if (mode == MPOL_LOCAL) {
                if (!nodes_empty(*nodes) ||
                    (flags & MPOL_F_STATIC_NODES) ||
                    (flags & MPOL_F_RELATIVE_NODES))
                        return ERR_PTR(-EINVAL);
-               mode = MPOL_PREFERRED;
        } else if (nodes_empty(*nodes))
                return ERR_PTR(-EINVAL);
        policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
@@ -330,7 +321,7 @@ static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
        else if (pol->flags & MPOL_F_RELATIVE_NODES)
                mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
        else {
-               nodes_remap(tmp, pol->v.nodes, pol->w.cpuset_mems_allowed,
+               nodes_remap(tmp, pol->nodes, pol->w.cpuset_mems_allowed,
                                                                *nodes);
                pol->w.cpuset_mems_allowed = *nodes;
        }
@@ -338,31 +329,13 @@ static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes)
        if (nodes_empty(tmp))
                tmp = *nodes;
 
-       pol->v.nodes = tmp;
+       pol->nodes = tmp;
 }
 
 static void mpol_rebind_preferred(struct mempolicy *pol,
                                                const nodemask_t *nodes)
 {
-       nodemask_t tmp;
-
-       if (pol->flags & MPOL_F_STATIC_NODES) {
-               int node = first_node(pol->w.user_nodemask);
-
-               if (node_isset(node, *nodes)) {
-                       pol->v.preferred_node = node;
-                       pol->flags &= ~MPOL_F_LOCAL;
-               } else
-                       pol->flags |= MPOL_F_LOCAL;
-       } else if (pol->flags & MPOL_F_RELATIVE_NODES) {
-               mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
-               pol->v.preferred_node = first_node(tmp);
-       } else if (!(pol->flags & MPOL_F_LOCAL)) {
-               pol->v.preferred_node = node_remap(pol->v.preferred_node,
-                                                  pol->w.cpuset_mems_allowed,
-                                                  *nodes);
-               pol->w.cpuset_mems_allowed = *nodes;
-       }
+       pol->w.cpuset_mems_allowed = *nodes;
 }
 
 /*
@@ -376,7 +349,7 @@ static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask)
 {
        if (!pol)
                return;
-       if (!mpol_store_user_nodemask(pol) && !(pol->flags & MPOL_F_LOCAL) &&
+       if (!mpol_store_user_nodemask(pol) &&
            nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
                return;
 
@@ -416,7 +389,7 @@ static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
                .rebind = mpol_rebind_default,
        },
        [MPOL_INTERLEAVE] = {
-               .create = mpol_new_interleave,
+               .create = mpol_new_nodemask,
                .rebind = mpol_rebind_nodemask,
        },
        [MPOL_PREFERRED] = {
@@ -424,9 +397,16 @@ static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
                .rebind = mpol_rebind_preferred,
        },
        [MPOL_BIND] = {
-               .create = mpol_new_bind,
+               .create = mpol_new_nodemask,
                .rebind = mpol_rebind_nodemask,
        },
+       [MPOL_LOCAL] = {
+               .rebind = mpol_rebind_default,
+       },
+       [MPOL_PREFERRED_MANY] = {
+               .create = mpol_new_nodemask,
+               .rebind = mpol_rebind_preferred,
+       },
 };
 
 static int migrate_page_add(struct page *page, struct list_head *pagelist,
@@ -458,7 +438,8 @@ static inline bool queue_pages_required(struct page *page,
 
 /*
  * queue_pages_pmd() has four possible return values:
- * 0 - pages are placed on the right node or queued successfully.
+ * 0 - pages are placed on the right node or queued successfully, or
+ *     special page is met, i.e. huge zero page.
  * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were
  *     specified.
  * 2 - THP was split.
@@ -482,8 +463,7 @@ static int queue_pages_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr,
        page = pmd_page(*pmd);
        if (is_huge_zero_page(page)) {
                spin_unlock(ptl);
-               __split_huge_pmd(walk->vma, pmd, addr, false, NULL);
-               ret = 2;
+               walk->action = ACTION_CONTINUE;
                goto out;
        }
        if (!queue_pages_required(page, qp))
@@ -510,7 +490,8 @@ out:
  * and move them to the pagelist if they do.
  *
  * queue_pages_pte_range() has three possible return values:
- * 0 - pages are placed on the right node or queued successfully.
+ * 0 - pages are placed on the right node or queued successfully, or
+ *     special page is met, i.e. zero page.
  * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were
  *     specified.
  * -EIO - only MPOL_MF_STRICT was specified and an existing page was already
@@ -917,12 +898,12 @@ static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
        switch (p->mode) {
        case MPOL_BIND:
        case MPOL_INTERLEAVE:
-               *nodes = p->v.nodes;
-               break;
        case MPOL_PREFERRED:
-               if (!(p->flags & MPOL_F_LOCAL))
-                       node_set(p->v.preferred_node, *nodes);
-               /* else return empty node mask for local allocation */
+       case MPOL_PREFERRED_MANY:
+               *nodes = p->nodes;
+               break;
+       case MPOL_LOCAL:
+               /* return empty node mask for local allocation */
                break;
        default:
                BUG();
@@ -975,7 +956,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask,
                 * want to return MPOL_DEFAULT in this case.
                 */
                mmap_read_lock(mm);
-               vma = find_vma_intersection(mm, addr, addr+1);
+               vma = vma_lookup(mm, addr);
                if (!vma) {
                        mmap_read_unlock(mm);
                        return -EFAULT;
@@ -1007,7 +988,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask,
                        *policy = err;
                } else if (pol == current->mempolicy &&
                                pol->mode == MPOL_INTERLEAVE) {
-                       *policy = next_node_in(current->il_prev, pol->v.nodes);
+                       *policy = next_node_in(current->il_prev, pol->nodes);
                } else {
                        err = -EINVAL;
                        goto out;
@@ -1103,7 +1084,7 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest,
 
        if (!list_empty(&pagelist)) {
                err = migrate_pages(&pagelist, alloc_migration_target, NULL,
-                               (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL);
+                               (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
                if (err)
                        putback_movable_pages(&pagelist);
        }
@@ -1357,7 +1338,7 @@ static long do_mbind(unsigned long start, unsigned long len,
                if (!list_empty(&pagelist)) {
                        WARN_ON_ONCE(flags & MPOL_MF_LAZY);
                        nr_failed = migrate_pages(&pagelist, new_page, NULL,
-                               start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND);
+                               start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND, NULL);
                        if (nr_failed)
                                putback_movable_pages(&pagelist);
                }
@@ -1381,16 +1362,33 @@ mpol_out:
 /*
  * User space interface with variable sized bitmaps for nodelists.
  */
+static int get_bitmap(unsigned long *mask, const unsigned long __user *nmask,
+                     unsigned long maxnode)
+{
+       unsigned long nlongs = BITS_TO_LONGS(maxnode);
+       int ret;
+
+       if (in_compat_syscall())
+               ret = compat_get_bitmap(mask,
+                                       (const compat_ulong_t __user *)nmask,
+                                       maxnode);
+       else
+               ret = copy_from_user(mask, nmask,
+                                    nlongs * sizeof(unsigned long));
+
+       if (ret)
+               return -EFAULT;
+
+       if (maxnode % BITS_PER_LONG)
+               mask[nlongs - 1] &= (1UL << (maxnode % BITS_PER_LONG)) - 1;
+
+       return 0;
+}
 
 /* Copy a node mask from user space. */
 static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
                     unsigned long maxnode)
 {
-       unsigned long k;
-       unsigned long t;
-       unsigned long nlongs;
-       unsigned long endmask;
-
        --maxnode;
        nodes_clear(*nodes);
        if (maxnode == 0 || !nmask)
@@ -1398,49 +1396,29 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
        if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
                return -EINVAL;
 
-       nlongs = BITS_TO_LONGS(maxnode);
-       if ((maxnode % BITS_PER_LONG) == 0)
-               endmask = ~0UL;
-       else
-               endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
-
        /*
         * When the user specified more nodes than supported just check
-        * if the non supported part is all zero.
-        *
-        * If maxnode have more longs than MAX_NUMNODES, check
-        * the bits in that area first. And then go through to
-        * check the rest bits which equal or bigger than MAX_NUMNODES.
-        * Otherwise, just check bits [MAX_NUMNODES, maxnode).
+        * if the non supported part is all zero, one word at a time,
+        * starting at the end.
         */
-       if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
-               for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
-                       if (get_user(t, nmask + k))
-                               return -EFAULT;
-                       if (k == nlongs - 1) {
-                               if (t & endmask)
-                                       return -EINVAL;
-                       } else if (t)
-                               return -EINVAL;
-               }
-               nlongs = BITS_TO_LONGS(MAX_NUMNODES);
-               endmask = ~0UL;
-       }
-
-       if (maxnode > MAX_NUMNODES && MAX_NUMNODES % BITS_PER_LONG != 0) {
-               unsigned long valid_mask = endmask;
+       while (maxnode > MAX_NUMNODES) {
+               unsigned long bits = min_t(unsigned long, maxnode, BITS_PER_LONG);
+               unsigned long t;
 
-               valid_mask &= ~((1UL << (MAX_NUMNODES % BITS_PER_LONG)) - 1);
-               if (get_user(t, nmask + nlongs - 1))
+               if (get_bitmap(&t, &nmask[maxnode / BITS_PER_LONG], bits))
                        return -EFAULT;
-               if (t & valid_mask)
+
+               if (maxnode - bits >= MAX_NUMNODES) {
+                       maxnode -= bits;
+               } else {
+                       maxnode = MAX_NUMNODES;
+                       t &= ~((1UL << (MAX_NUMNODES % BITS_PER_LONG)) - 1);
+               }
+               if (t)
                        return -EINVAL;
        }
 
-       if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
-               return -EFAULT;
-       nodes_addr(*nodes)[nlongs-1] &= endmask;
-       return 0;
+       return get_bitmap(nodes_addr(*nodes), nmask, maxnode);
 }
 
 /* Copy a kernel node mask to user space */
@@ -1449,6 +1427,10 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
 {
        unsigned long copy = ALIGN(maxnode-1, 64) / 8;
        unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long);
+       bool compat = in_compat_syscall();
+
+       if (compat)
+               nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
 
        if (copy > nbytes) {
                if (copy > PAGE_SIZE)
@@ -1456,30 +1438,49 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
                if (clear_user((char __user *)mask + nbytes, copy - nbytes))
                        return -EFAULT;
                copy = nbytes;
+               maxnode = nr_node_ids;
        }
+
+       if (compat)
+               return compat_put_bitmap((compat_ulong_t __user *)mask,
+                                        nodes_addr(*nodes), maxnode);
+
        return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
 }
 
+/* Basic parameter sanity check used by both mbind() and set_mempolicy() */
+static inline int sanitize_mpol_flags(int *mode, unsigned short *flags)
+{
+       *flags = *mode & MPOL_MODE_FLAGS;
+       *mode &= ~MPOL_MODE_FLAGS;
+
+       if ((unsigned int)(*mode) >=  MPOL_MAX)
+               return -EINVAL;
+       if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
+               return -EINVAL;
+
+       return 0;
+}
+
 static long kernel_mbind(unsigned long start, unsigned long len,
                         unsigned long mode, const unsigned long __user *nmask,
                         unsigned long maxnode, unsigned int flags)
 {
+       unsigned short mode_flags;
        nodemask_t nodes;
+       int lmode = mode;
        int err;
-       unsigned short mode_flags;
 
        start = untagged_addr(start);
-       mode_flags = mode & MPOL_MODE_FLAGS;
-       mode &= ~MPOL_MODE_FLAGS;
-       if (mode >= MPOL_MAX)
-               return -EINVAL;
-       if ((mode_flags & MPOL_F_STATIC_NODES) &&
-           (mode_flags & MPOL_F_RELATIVE_NODES))
-               return -EINVAL;
+       err = sanitize_mpol_flags(&lmode, &mode_flags);
+       if (err)
+               return err;
+
        err = get_nodes(&nodes, nmask, maxnode);
        if (err)
                return err;
-       return do_mbind(start, len, mode, mode_flags, &nodes, flags);
+
+       return do_mbind(start, len, lmode, mode_flags, &nodes, flags);
 }
 
 SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
@@ -1493,20 +1494,20 @@ SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
 static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask,
                                 unsigned long maxnode)
 {
-       int err;
+       unsigned short mode_flags;
        nodemask_t nodes;
-       unsigned short flags;
+       int lmode = mode;
+       int err;
+
+       err = sanitize_mpol_flags(&lmode, &mode_flags);
+       if (err)
+               return err;
 
-       flags = mode & MPOL_MODE_FLAGS;
-       mode &= ~MPOL_MODE_FLAGS;
-       if ((unsigned int)mode >= MPOL_MAX)
-               return -EINVAL;
-       if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
-               return -EINVAL;
        err = get_nodes(&nodes, nmask, maxnode);
        if (err)
                return err;
-       return do_set_mempolicy(mode, flags, &nodes);
+
+       return do_set_mempolicy(lmode, mode_flags, &nodes);
 }
 
 SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask,
@@ -1648,116 +1649,6 @@ SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
        return kernel_get_mempolicy(policy, nmask, maxnode, addr, flags);
 }
 
-#ifdef CONFIG_COMPAT
-
-COMPAT_SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
-                      compat_ulong_t __user *, nmask,
-                      compat_ulong_t, maxnode,
-                      compat_ulong_t, addr, compat_ulong_t, flags)
-{
-       long err;
-       unsigned long __user *nm = NULL;
-       unsigned long nr_bits, alloc_size;
-       DECLARE_BITMAP(bm, MAX_NUMNODES);
-
-       nr_bits = min_t(unsigned long, maxnode-1, nr_node_ids);
-       alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
-
-       if (nmask)
-               nm = compat_alloc_user_space(alloc_size);
-
-       err = kernel_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
-
-       if (!err && nmask) {
-               unsigned long copy_size;
-               copy_size = min_t(unsigned long, sizeof(bm), alloc_size);
-               err = copy_from_user(bm, nm, copy_size);
-               /* ensure entire bitmap is zeroed */
-               err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
-               err |= compat_put_bitmap(nmask, bm, nr_bits);
-       }
-
-       return err;
-}
-
-COMPAT_SYSCALL_DEFINE3(set_mempolicy, int, mode, compat_ulong_t __user *, nmask,
-                      compat_ulong_t, maxnode)
-{
-       unsigned long __user *nm = NULL;
-       unsigned long nr_bits, alloc_size;
-       DECLARE_BITMAP(bm, MAX_NUMNODES);
-
-       nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
-       alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
-
-       if (nmask) {
-               if (compat_get_bitmap(bm, nmask, nr_bits))
-                       return -EFAULT;
-               nm = compat_alloc_user_space(alloc_size);
-               if (copy_to_user(nm, bm, alloc_size))
-                       return -EFAULT;
-       }
-
-       return kernel_set_mempolicy(mode, nm, nr_bits+1);
-}
-
-COMPAT_SYSCALL_DEFINE6(mbind, compat_ulong_t, start, compat_ulong_t, len,
-                      compat_ulong_t, mode, compat_ulong_t __user *, nmask,
-                      compat_ulong_t, maxnode, compat_ulong_t, flags)
-{
-       unsigned long __user *nm = NULL;
-       unsigned long nr_bits, alloc_size;
-       nodemask_t bm;
-
-       nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
-       alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
-
-       if (nmask) {
-               if (compat_get_bitmap(nodes_addr(bm), nmask, nr_bits))
-                       return -EFAULT;
-               nm = compat_alloc_user_space(alloc_size);
-               if (copy_to_user(nm, nodes_addr(bm), alloc_size))
-                       return -EFAULT;
-       }
-
-       return kernel_mbind(start, len, mode, nm, nr_bits+1, flags);
-}
-
-COMPAT_SYSCALL_DEFINE4(migrate_pages, compat_pid_t, pid,
-                      compat_ulong_t, maxnode,
-                      const compat_ulong_t __user *, old_nodes,
-                      const compat_ulong_t __user *, new_nodes)
-{
-       unsigned long __user *old = NULL;
-       unsigned long __user *new = NULL;
-       nodemask_t tmp_mask;
-       unsigned long nr_bits;
-       unsigned long size;
-
-       nr_bits = min_t(unsigned long, maxnode - 1, MAX_NUMNODES);
-       size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
-       if (old_nodes) {
-               if (compat_get_bitmap(nodes_addr(tmp_mask), old_nodes, nr_bits))
-                       return -EFAULT;
-               old = compat_alloc_user_space(new_nodes ? size * 2 : size);
-               if (new_nodes)
-                       new = old + size / sizeof(unsigned long);
-               if (copy_to_user(old, nodes_addr(tmp_mask), size))
-                       return -EFAULT;
-       }
-       if (new_nodes) {
-               if (compat_get_bitmap(nodes_addr(tmp_mask), new_nodes, nr_bits))
-                       return -EFAULT;
-               if (new == NULL)
-                       new = compat_alloc_user_space(size);
-               if (copy_to_user(new, nodes_addr(tmp_mask), size))
-                       return -EFAULT;
-       }
-       return kernel_migrate_pages(pid, nr_bits + 1, old, new);
-}
-
-#endif /* CONFIG_COMPAT */
-
 bool vma_migratable(struct vm_area_struct *vma)
 {
        if (vma->vm_flags & (VM_IO | VM_PFNMAP))
@@ -1863,14 +1754,14 @@ static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
        BUG_ON(dynamic_policy_zone == ZONE_MOVABLE);
 
        /*
-        * if policy->v.nodes has movable memory only,
+        * if policy->nodes has movable memory only,
         * we apply policy when gfp_zone(gfp) = ZONE_MOVABLE only.
         *
-        * policy->v.nodes is intersect with node_states[N_MEMORY].
+        * policy->nodes is intersect with node_states[N_MEMORY].
         * so if the following test fails, it implies
-        * policy->v.nodes has movable memory only.
+        * policy->nodes has movable memory only.
         */
-       if (!nodes_intersects(policy->v.nodes, node_states[N_HIGH_MEMORY]))
+       if (!nodes_intersects(policy->nodes, node_states[N_HIGH_MEMORY]))
                dynamic_policy_zone = ZONE_MOVABLE;
 
        return zone >= dynamic_policy_zone;
@@ -1882,21 +1773,32 @@ static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
  */
 nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
 {
+       int mode = policy->mode;
+
        /* Lower zones don't get a nodemask applied for MPOL_BIND */
-       if (unlikely(policy->mode == MPOL_BIND) &&
-                       apply_policy_zone(policy, gfp_zone(gfp)) &&
-                       cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
-               return &policy->v.nodes;
+       if (unlikely(mode == MPOL_BIND) &&
+               apply_policy_zone(policy, gfp_zone(gfp)) &&
+               cpuset_nodemask_valid_mems_allowed(&policy->nodes))
+               return &policy->nodes;
+
+       if (mode == MPOL_PREFERRED_MANY)
+               return &policy->nodes;
 
        return NULL;
 }
 
-/* Return the node id preferred by the given mempolicy, or the given id */
+/*
+ * Return the  preferred node id for 'prefer' mempolicy, and return
+ * the given id for all other policies.
+ *
+ * policy_node() is always coupled with policy_nodemask(), which
+ * secures the nodemask limit for 'bind' and 'prefer-many' policy.
+ */
 static int policy_node(gfp_t gfp, struct mempolicy *policy, int nd)
 {
-       if (policy->mode == MPOL_PREFERRED && !(policy->flags & MPOL_F_LOCAL))
-               nd = policy->v.preferred_node;
-       else {
+       if (policy->mode == MPOL_PREFERRED) {
+               nd = first_node(policy->nodes);
+       else {
                /*
                 * __GFP_THISNODE shouldn't even be used with the bind policy
                 * because we might easily break the expectation to stay on the
@@ -1914,7 +1816,7 @@ static unsigned interleave_nodes(struct mempolicy *policy)
        unsigned next;
        struct task_struct *me = current;
 
-       next = next_node_in(me->il_prev, policy->v.nodes);
+       next = next_node_in(me->il_prev, policy->nodes);
        if (next < MAX_NUMNODES)
                me->il_prev = next;
        return next;
@@ -1929,24 +1831,23 @@ unsigned int mempolicy_slab_node(void)
        struct mempolicy *policy;
        int node = numa_mem_id();
 
-       if (in_interrupt())
+       if (!in_task())
                return node;
 
        policy = current->mempolicy;
-       if (!policy || policy->flags & MPOL_F_LOCAL)
+       if (!policy)
                return node;
 
        switch (policy->mode) {
        case MPOL_PREFERRED:
-               /*
-                * handled MPOL_F_LOCAL above
-                */
-               return policy->v.preferred_node;
+               return first_node(policy->nodes);
 
        case MPOL_INTERLEAVE:
                return interleave_nodes(policy);
 
-       case MPOL_BIND: {
+       case MPOL_BIND:
+       case MPOL_PREFERRED_MANY:
+       {
                struct zoneref *z;
 
                /*
@@ -1957,9 +1858,11 @@ unsigned int mempolicy_slab_node(void)
                enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
                zonelist = &NODE_DATA(node)->node_zonelists[ZONELIST_FALLBACK];
                z = first_zones_zonelist(zonelist, highest_zoneidx,
-                                                       &policy->v.nodes);
+                                                       &policy->nodes);
                return z->zone ? zone_to_nid(z->zone) : node;
        }
+       case MPOL_LOCAL:
+               return node;
 
        default:
                BUG();
@@ -1968,22 +1871,31 @@ unsigned int mempolicy_slab_node(void)
 
 /*
  * Do static interleaving for a VMA with known offset @n.  Returns the n'th
- * node in pol->v.nodes (starting from n=0), wrapping around if n exceeds the
+ * node in pol->nodes (starting from n=0), wrapping around if n exceeds the
  * number of present nodes.
  */
 static unsigned offset_il_node(struct mempolicy *pol, unsigned long n)
 {
-       unsigned nnodes = nodes_weight(pol->v.nodes);
-       unsigned target;
+       nodemask_t nodemask = pol->nodes;
+       unsigned int target, nnodes;
        int i;
        int nid;
+       /*
+        * The barrier will stabilize the nodemask in a register or on
+        * the stack so that it will stop changing under the code.
+        *
+        * Between first_node() and next_node(), pol->nodes could be changed
+        * by other threads. So we put pol->nodes in a local stack.
+        */
+       barrier();
 
+       nnodes = nodes_weight(nodemask);
        if (!nnodes)
                return numa_node_id();
        target = (unsigned int)n % nnodes;
-       nid = first_node(pol->v.nodes);
+       nid = first_node(nodemask);
        for (i = 0; i < target; i++)
-               nid = next_node(nid, pol->v.nodes);
+               nid = next_node(nid, nodemask);
        return nid;
 }
 
@@ -2016,12 +1928,12 @@ static inline unsigned interleave_nid(struct mempolicy *pol,
  * @addr: address in @vma for shared policy lookup and interleave policy
  * @gfp_flags: for requested zone
  * @mpol: pointer to mempolicy pointer for reference counted mempolicy
- * @nodemask: pointer to nodemask pointer for MPOL_BIND nodemask
+ * @nodemask: pointer to nodemask pointer for 'bind' and 'prefer-many' policy
  *
  * Returns a nid suitable for a huge page allocation and a pointer
  * to the struct mempolicy for conditional unref after allocation.
- * If the effective policy is 'BIND, returns a pointer to the mempolicy's
- * @nodemask for filtering the zonelist.
+ * If the effective policy is 'bind' or 'prefer-many', returns a pointer
+ * to the mempolicy's @nodemask for filtering the zonelist.
  *
  * Must be protected by read_mems_allowed_begin()
  */
@@ -2029,17 +1941,19 @@ int huge_node(struct vm_area_struct *vma, unsigned long addr, gfp_t gfp_flags,
                                struct mempolicy **mpol, nodemask_t **nodemask)
 {
        int nid;
+       int mode;
 
        *mpol = get_vma_policy(vma, addr);
-       *nodemask = NULL;       /* assume !MPOL_BIND */
+       *nodemask = NULL;
+       mode = (*mpol)->mode;
 
-       if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
+       if (unlikely(mode == MPOL_INTERLEAVE)) {
                nid = interleave_nid(*mpol, vma, addr,
                                        huge_page_shift(hstate_vma(vma)));
        } else {
                nid = policy_node(gfp_flags, *mpol, numa_node_id());
-               if ((*mpol)->mode == MPOL_BIND)
-                       *nodemask = &(*mpol)->v.nodes;
+               if (mode == MPOL_BIND || mode == MPOL_PREFERRED_MANY)
+                       *nodemask = &(*mpol)->nodes;
        }
        return nid;
 }
@@ -2063,7 +1977,6 @@ int huge_node(struct vm_area_struct *vma, unsigned long addr, gfp_t gfp_flags,
 bool init_nodemask_of_mempolicy(nodemask_t *mask)
 {
        struct mempolicy *mempolicy;
-       int nid;
 
        if (!(mask && current->mempolicy))
                return false;
@@ -2072,16 +1985,14 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
        mempolicy = current->mempolicy;
        switch (mempolicy->mode) {
        case MPOL_PREFERRED:
-               if (mempolicy->flags & MPOL_F_LOCAL)
-                       nid = numa_node_id();
-               else
-                       nid = mempolicy->v.preferred_node;
-               init_nodemask_of_node(mask, nid);
-               break;
-
+       case MPOL_PREFERRED_MANY:
        case MPOL_BIND:
        case MPOL_INTERLEAVE:
-               *mask =  mempolicy->v.nodes;
+               *mask = mempolicy->nodes;
+               break;
+
+       case MPOL_LOCAL:
+               init_nodemask_of_node(mask, numa_node_id());
                break;
 
        default:
@@ -2094,16 +2005,16 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
 #endif
 
 /*
- * mempolicy_nodemask_intersects
+ * mempolicy_in_oom_domain
  *
- * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
- * policy.  Otherwise, check for intersection between mask and the policy
- * nodemask for 'bind' or 'interleave' policy.  For 'preferred' or 'local'
- * policy, always return true since it may allocate elsewhere on fallback.
+ * If tsk's mempolicy is "bind", check for intersection between mask and
+ * the policy nodemask. Otherwise, return true for all other policies
+ * including "interleave", as a tsk with "interleave" policy may have
+ * memory allocated from all nodes in system.
  *
  * Takes task_lock(tsk) to prevent freeing of its mempolicy.
  */
-bool mempolicy_nodemask_intersects(struct task_struct *tsk,
+bool mempolicy_in_oom_domain(struct task_struct *tsk,
                                        const nodemask_t *mask)
 {
        struct mempolicy *mempolicy;
@@ -2111,29 +2022,13 @@ bool mempolicy_nodemask_intersects(struct task_struct *tsk,
 
        if (!mask)
                return ret;
+
        task_lock(tsk);
        mempolicy = tsk->mempolicy;
-       if (!mempolicy)
-               goto out;
-
-       switch (mempolicy->mode) {
-       case MPOL_PREFERRED:
-               /*
-                * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
-                * allocate from, they may fallback to other nodes when oom.
-                * Thus, it's possible for tsk to have allocated memory from
-                * nodes in mask.
-                */
-               break;
-       case MPOL_BIND:
-       case MPOL_INTERLEAVE:
-               ret = nodes_intersects(mempolicy->v.nodes, *mask);
-               break;
-       default:
-               BUG();
-       }
-out:
+       if (mempolicy && mempolicy->mode == MPOL_BIND)
+               ret = nodes_intersects(mempolicy->nodes, *mask);
        task_unlock(tsk);
+
        return ret;
 }
 
@@ -2150,12 +2045,33 @@ static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
                return page;
        if (page && page_to_nid(page) == nid) {
                preempt_disable();
-               __inc_numa_state(page_zone(page), NUMA_INTERLEAVE_HIT);
+               __count_numa_event(page_zone(page), NUMA_INTERLEAVE_HIT);
                preempt_enable();
        }
        return page;
 }
 
+static struct page *alloc_pages_preferred_many(gfp_t gfp, unsigned int order,
+                                               int nid, struct mempolicy *pol)
+{
+       struct page *page;
+       gfp_t preferred_gfp;
+
+       /*
+        * This is a two pass approach. The first pass will only try the
+        * preferred nodes but skip the direct reclaim and allow the
+        * allocation to fail, while the second pass will try all the
+        * nodes in system.
+        */
+       preferred_gfp = gfp | __GFP_NOWARN;
+       preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
+       page = __alloc_pages(preferred_gfp, order, nid, &pol->nodes);
+       if (!page)
+               page = __alloc_pages(gfp, order, numa_node_id(), NULL);
+
+       return page;
+}
+
 /**
  * alloc_pages_vma - Allocate a page for a VMA.
  * @gfp: GFP flags.
@@ -2191,6 +2107,12 @@ struct page *alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
                goto out;
        }
 
+       if (pol->mode == MPOL_PREFERRED_MANY) {
+               page = alloc_pages_preferred_many(gfp, order, node, pol);
+               mpol_cond_put(pol);
+               goto out;
+       }
+
        if (unlikely(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage)) {
                int hpage_node = node;
 
@@ -2201,11 +2123,11 @@ struct page *alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
                 * node and don't fall back to other nodes, as the cost of
                 * remote accesses would likely offset THP benefits.
                 *
-                * If the policy is interleave, or does not allow the current
+                * If the policy is interleave or does not allow the current
                 * node in its nodemask, we allocate the standard way.
                 */
-               if (pol->mode == MPOL_PREFERRED && !(pol->flags & MPOL_F_LOCAL))
-                       hpage_node = pol->v.preferred_node;
+               if (pol->mode == MPOL_PREFERRED)
+                       hpage_node = first_node(pol->nodes);
 
                nmask = policy_nodemask(gfp, pol);
                if (!nmask || node_isset(hpage_node, *nmask)) {
@@ -2268,6 +2190,9 @@ struct page *alloc_pages(gfp_t gfp, unsigned order)
         */
        if (pol->mode == MPOL_INTERLEAVE)
                page = alloc_page_interleave(gfp, order, interleave_nodes(pol));
+       else if (pol->mode == MPOL_PREFERRED_MANY)
+               page = alloc_pages_preferred_many(gfp, order,
+                               numa_node_id(), pol);
        else
                page = __alloc_pages(gfp, order,
                                policy_node(gfp, pol, numa_node_id()),
@@ -2338,12 +2263,11 @@ bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
        switch (a->mode) {
        case MPOL_BIND:
        case MPOL_INTERLEAVE:
-               return !!nodes_equal(a->v.nodes, b->v.nodes);
        case MPOL_PREFERRED:
-               /* a's ->flags is the same as b's */
-               if (a->flags & MPOL_F_LOCAL)
-                       return true;
-               return a->v.preferred_node == b->v.preferred_node;
+       case MPOL_PREFERRED_MANY:
+               return !!nodes_equal(a->nodes, b->nodes);
+       case MPOL_LOCAL:
+               return true;
        default:
                BUG();
                return false;
@@ -2455,8 +2379,8 @@ static void sp_free(struct sp_node *n)
  * node id.  Policy determination "mimics" alloc_page_vma().
  * Called from fault path where we know the vma and faulting address.
  *
- * Return: -1 if the page is in a node that is valid for this policy, or a
- * suitable node ID to allocate a replacement page from.
+ * Return: NUMA_NO_NODE if the page is in a node that is valid for this
+ * policy, or a suitable node ID to allocate a replacement page from.
  */
 int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long addr)
 {
@@ -2467,7 +2391,7 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long
        int thiscpu = raw_smp_processor_id();
        int thisnid = cpu_to_node(thiscpu);
        int polnid = NUMA_NO_NODE;
-       int ret = -1;
+       int ret = NUMA_NO_NODE;
 
        pol = get_vma_policy(vma, addr);
        if (!(pol->flags & MPOL_F_MOF))
@@ -2481,32 +2405,36 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long
                break;
 
        case MPOL_PREFERRED:
-               if (pol->flags & MPOL_F_LOCAL)
-                       polnid = numa_node_id();
-               else
-                       polnid = pol->v.preferred_node;
+               if (node_isset(curnid, pol->nodes))
+                       goto out;
+               polnid = first_node(pol->nodes);
+               break;
+
+       case MPOL_LOCAL:
+               polnid = numa_node_id();
                break;
 
        case MPOL_BIND:
                /* Optimize placement among multiple nodes via NUMA balancing */
                if (pol->flags & MPOL_F_MORON) {
-                       if (node_isset(thisnid, pol->v.nodes))
+                       if (node_isset(thisnid, pol->nodes))
                                break;
                        goto out;
                }
+               fallthrough;
 
+       case MPOL_PREFERRED_MANY:
                /*
-                * allows binding to multiple nodes.
                 * use current page if in policy nodemask,
                 * else select nearest allowed node, if any.
                 * If no allowed nodes, use current [!misplaced].
                 */
-               if (node_isset(curnid, pol->v.nodes))
+               if (node_isset(curnid, pol->nodes))
                        goto out;
                z = first_zones_zonelist(
                                node_zonelist(numa_node_id(), GFP_HIGHUSER),
                                gfp_zone(GFP_HIGHUSER),
-                               &pol->v.nodes);
+                               &pol->nodes);
                polnid = zone_to_nid(z->zone);
                break;
 
@@ -2709,7 +2637,7 @@ int mpol_set_shared_policy(struct shared_policy *info,
                 vma->vm_pgoff,
                 sz, npol ? npol->mode : -1,
                 npol ? npol->flags : -1,
-                npol ? nodes_addr(npol->v.nodes)[0] : NUMA_NO_NODE);
+                npol ? nodes_addr(npol->nodes)[0] : NUMA_NO_NODE);
 
        if (npol) {
                new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
@@ -2807,7 +2735,7 @@ void __init numa_policy_init(void)
                        .refcnt = ATOMIC_INIT(1),
                        .mode = MPOL_PREFERRED,
                        .flags = MPOL_F_MOF | MPOL_F_MORON,
-                       .v = { .preferred_node = nid, },
+                       .nodes = nodemask_of_node(nid),
                };
        }
 
@@ -2851,9 +2779,6 @@ void numa_default_policy(void)
  * Parse and format mempolicy from/to strings
  */
 
-/*
- * "local" is implemented internally by MPOL_PREFERRED with MPOL_F_LOCAL flag.
- */
 static const char * const policy_modes[] =
 {
        [MPOL_DEFAULT]    = "default",
@@ -2861,6 +2786,7 @@ static const char * const policy_modes[] =
        [MPOL_BIND]       = "bind",
        [MPOL_INTERLEAVE] = "interleave",
        [MPOL_LOCAL]      = "local",
+       [MPOL_PREFERRED_MANY]  = "prefer (many)",
 };
 
 
@@ -2931,7 +2857,6 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
                 */
                if (nodelist)
                        goto out;
-               mode = MPOL_PREFERRED;
                break;
        case MPOL_DEFAULT:
                /*
@@ -2940,6 +2865,7 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
                if (!nodelist)
                        err = 0;
                goto out;
+       case MPOL_PREFERRED_MANY:
        case MPOL_BIND:
                /*
                 * Insist on a nodelist
@@ -2970,12 +2896,14 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
         * Save nodes for mpol_to_str() to show the tmpfs mount options
         * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo.
         */
-       if (mode != MPOL_PREFERRED)
-               new->v.nodes = nodes;
-       else if (nodelist)
-               new->v.preferred_node = first_node(nodes);
-       else
-               new->flags |= MPOL_F_LOCAL;
+       if (mode != MPOL_PREFERRED) {
+               new->nodes = nodes;
+       } else if (nodelist) {
+               nodes_clear(new->nodes);
+               node_set(first_node(nodes), new->nodes);
+       } else {
+               new->mode = MPOL_LOCAL;
+       }
 
        /*
         * Save nodes for contextualization: this will be used to "clone"
@@ -3021,16 +2949,13 @@ void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
 
        switch (mode) {
        case MPOL_DEFAULT:
+       case MPOL_LOCAL:
                break;
        case MPOL_PREFERRED:
-               if (flags & MPOL_F_LOCAL)
-                       mode = MPOL_LOCAL;
-               else
-                       node_set(pol->v.preferred_node, nodes);
-               break;
+       case MPOL_PREFERRED_MANY:
        case MPOL_BIND:
        case MPOL_INTERLEAVE:
-               nodes = pol->v.nodes;
+               nodes = pol->nodes;
                break;
        default:
                WARN_ON_ONCE(1);
@@ -3056,3 +2981,64 @@ void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
                p += scnprintf(p, buffer + maxlen - p, ":%*pbl",
                               nodemask_pr_args(&nodes));
 }
+
+bool numa_demotion_enabled = false;
+
+#ifdef CONFIG_SYSFS
+static ssize_t numa_demotion_enabled_show(struct kobject *kobj,
+                                         struct kobj_attribute *attr, char *buf)
+{
+       return sysfs_emit(buf, "%s\n",
+                         numa_demotion_enabled? "true" : "false");
+}
+
+static ssize_t numa_demotion_enabled_store(struct kobject *kobj,
+                                          struct kobj_attribute *attr,
+                                          const char *buf, size_t count)
+{
+       if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
+               numa_demotion_enabled = true;
+       else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
+               numa_demotion_enabled = false;
+       else
+               return -EINVAL;
+
+       return count;
+}
+
+static struct kobj_attribute numa_demotion_enabled_attr =
+       __ATTR(demotion_enabled, 0644, numa_demotion_enabled_show,
+              numa_demotion_enabled_store);
+
+static struct attribute *numa_attrs[] = {
+       &numa_demotion_enabled_attr.attr,
+       NULL,
+};
+
+static const struct attribute_group numa_attr_group = {
+       .attrs = numa_attrs,
+};
+
+static int __init numa_init_sysfs(void)
+{
+       int err;
+       struct kobject *numa_kobj;
+
+       numa_kobj = kobject_create_and_add("numa", mm_kobj);
+       if (!numa_kobj) {
+               pr_err("failed to create numa kobject\n");
+               return -ENOMEM;
+       }
+       err = sysfs_create_group(numa_kobj, &numa_attr_group);
+       if (err) {
+               pr_err("failed to register numa group\n");
+               goto delete_obj;
+       }
+       return 0;
+
+delete_obj:
+       kobject_put(numa_kobj);
+       return err;
+}
+subsys_initcall(numa_init_sysfs);
+#endif