ARM: tegra: Pass multiple versions in opp-supported-hw property
[linux-2.6-microblaze.git] / mm / hugetlb.c
index bcabbe0..a301c2d 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/memblock.h>
 #include <linux/sysfs.h>
 #include <linux/slab.h>
+#include <linux/sched/mm.h>
 #include <linux/mmdebug.h>
 #include <linux/sched/signal.h>
 #include <linux/rmap.h>
@@ -31,7 +32,7 @@
 #include <linux/cma.h>
 
 #include <asm/page.h>
-#include <asm/pgtable.h>
+#include <asm/pgalloc.h>
 #include <asm/tlb.h>
 
 #include <linux/io.h>
@@ -46,7 +47,10 @@ int hugetlb_max_hstate __read_mostly;
 unsigned int default_hstate_idx;
 struct hstate hstates[HUGE_MAX_HSTATE];
 
+#ifdef CONFIG_CMA
 static struct cma *hugetlb_cma[MAX_NUMNODES];
+#endif
+static unsigned long hugetlb_cma_size __initdata;
 
 /*
  * Minimum page order among possible hugepage sizes, set to a proper value
@@ -59,8 +63,8 @@ __initdata LIST_HEAD(huge_boot_pages);
 /* for command line parsing */
 static struct hstate * __initdata parsed_hstate;
 static unsigned long __initdata default_hstate_max_huge_pages;
-static unsigned long __initdata default_hstate_size;
 static bool __initdata parsed_valid_hugepagesz = true;
+static bool __initdata parsed_default_hugepagesz;
 
 /*
  * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
@@ -85,7 +89,7 @@ static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
        spin_unlock(&spool->lock);
 
        /* If no pages are used, and no other handles to the subpool
-        * remain, give up any reservations mased on minimum size and
+        * remain, give up any reservations based on minimum size and
         * free the subpool */
        if (free) {
                if (spool->min_hpages != -1)
@@ -130,10 +134,10 @@ void hugepage_put_subpool(struct hugepage_subpool *spool)
 /*
  * Subpool accounting for allocating and reserving pages.
  * Return -ENOMEM if there are not enough resources to satisfy the
- * the request.  Otherwise, return the number of pages by which the
+ * request.  Otherwise, return the number of pages by which the
  * global pools must be adjusted (upward).  The returned value may
  * only be different than the passed value (delta) in the case where
- * a subpool minimum size must be manitained.
+ * a subpool minimum size must be maintained.
  */
 static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
                                      long delta)
@@ -473,7 +477,7 @@ out_of_memory:
  *
  * Return the number of new huge pages added to the map.  This number is greater
  * than or equal to zero.  If file_region entries needed to be allocated for
- * this operation and we were not able to allocate, it ruturns -ENOMEM.
+ * this operation and we were not able to allocate, it returns -ENOMEM.
  * region_add of regions of length 1 never allocate file_regions and cannot
  * fail; region_chg will always allocate at least 1 entry and a region_add for
  * 1 page will only require at most 1 entry.
@@ -988,7 +992,7 @@ static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
                 * We know VM_NORESERVE is not set.  Therefore, there SHOULD
                 * be a region map for all pages.  The only situation where
                 * there is no region map is if a hole was punched via
-                * fallocate.  In this case, there really are no reverves to
+                * fallocate.  In this case, there really are no reserves to
                 * use.  This situation is indicated if chg != 0.
                 */
                if (chg)
@@ -1037,10 +1041,16 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)
 static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
 {
        struct page *page;
+       bool nocma = !!(current->flags & PF_MEMALLOC_NOCMA);
+
+       list_for_each_entry(page, &h->hugepage_freelists[nid], lru) {
+               if (nocma && is_migrate_cma_page(page))
+                       continue;
 
-       list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
                if (!PageHWPoison(page))
                        break;
+       }
+
        /*
         * if 'non-isolated free hugepage' not found on the list,
         * the allocation fails.
@@ -1090,15 +1100,6 @@ retry_cpuset:
        return NULL;
 }
 
-/* Movability of hugepages depends on migration support. */
-static inline gfp_t htlb_alloc_mask(struct hstate *h)
-{
-       if (hugepage_movable_supported(h))
-               return GFP_HIGHUSER_MOVABLE;
-       else
-               return GFP_HIGHUSER;
-}
-
 static struct page *dequeue_huge_page_vma(struct hstate *h,
                                struct vm_area_struct *vma,
                                unsigned long address, int avoid_reserve,
@@ -1236,9 +1237,10 @@ static void free_gigantic_page(struct page *page, unsigned int order)
         * If the page isn't allocated using the cma allocator,
         * cma_release() returns false.
         */
-       if (IS_ENABLED(CONFIG_CMA) &&
-           cma_release(hugetlb_cma[page_to_nid(page)], page, 1 << order))
+#ifdef CONFIG_CMA
+       if (cma_release(hugetlb_cma[page_to_nid(page)], page, 1 << order))
                return;
+#endif
 
        free_contig_range(page_to_pfn(page), 1 << order);
 }
@@ -1249,7 +1251,8 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
 {
        unsigned long nr_pages = 1UL << huge_page_order(h);
 
-       if (IS_ENABLED(CONFIG_CMA)) {
+#ifdef CONFIG_CMA
+       {
                struct page *page;
                int node;
 
@@ -1263,6 +1266,7 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
                                return page;
                }
        }
+#endif
 
        return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
 }
@@ -1519,7 +1523,7 @@ static void prep_compound_gigantic_page(struct page *page, unsigned int order)
                 * For gigantic hugepages allocated through bootmem at
                 * boot, it's safer to be consistent with the not-gigantic
                 * hugepages and clear the PG_reserved bit from all tail pages
-                * too.  Otherwse drivers using get_user_pages() to access tail
+                * too.  Otherwise drivers using get_user_pages() to access tail
                 * pages may get the reference counting wrong if they see
                 * PG_reserved set on a tail page (despite the head page not
                 * having PG_reserved set).  Enforcing this consistency between
@@ -1594,7 +1598,7 @@ static struct address_space *_get_hugetlb_page_mapping(struct page *hpage)
 
        /* Use first found vma */
        pgoff_start = page_to_pgoff(hpage);
-       pgoff_end = pgoff_start + hpage_nr_pages(hpage) - 1;
+       pgoff_end = pgoff_start + pages_per_huge_page(page_hstate(hpage)) - 1;
        anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
                                        pgoff_start, pgoff_end) {
                struct vm_area_struct *vma = avc->vma;
@@ -1938,7 +1942,7 @@ out_unlock:
        return page;
 }
 
-struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
+static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
                                     int nid, nodemask_t *nmask)
 {
        struct page *page;
@@ -1979,32 +1983,10 @@ struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
        return page;
 }
 
-/* page migration callback function */
-struct page *alloc_huge_page_node(struct hstate *h, int nid)
-{
-       gfp_t gfp_mask = htlb_alloc_mask(h);
-       struct page *page = NULL;
-
-       if (nid != NUMA_NO_NODE)
-               gfp_mask |= __GFP_THISNODE;
-
-       spin_lock(&hugetlb_lock);
-       if (h->free_huge_pages - h->resv_huge_pages > 0)
-               page = dequeue_huge_page_nodemask(h, gfp_mask, nid, NULL);
-       spin_unlock(&hugetlb_lock);
-
-       if (!page)
-               page = alloc_migrate_huge_page(h, gfp_mask, nid, NULL);
-
-       return page;
-}
-
 /* page migration callback function */
 struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
-               nodemask_t *nmask)
+               nodemask_t *nmask, gfp_t gfp_mask)
 {
-       gfp_t gfp_mask = htlb_alloc_mask(h);
-
        spin_lock(&hugetlb_lock);
        if (h->free_huge_pages - h->resv_huge_pages > 0) {
                struct page *page;
@@ -2032,7 +2014,7 @@ struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
 
        gfp_mask = htlb_alloc_mask(h);
        node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
-       page = alloc_huge_page_nodemask(h, node, nodemask);
+       page = alloc_huge_page_nodemask(h, node, nodemask, gfp_mask);
        mpol_cond_put(mpol);
 
        return page;
@@ -2161,7 +2143,7 @@ static void return_unused_surplus_pages(struct hstate *h,
         * evenly across all nodes with memory. Iterate across these nodes
         * until we can no longer free unreserved surplus pages. This occurs
         * when the nodes with surplus pages have no free pages.
-        * free_pool_huge_page() will balance the the freed pages across the
+        * free_pool_huge_page() will balance the freed pages across the
         * on-line nodes with memory and will handle the hstate accounting.
         *
         * Note that we decrement resv_huge_pages as we free the pages.  If
@@ -2572,7 +2554,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
 
        for (i = 0; i < h->max_huge_pages; ++i) {
                if (hstate_is_gigantic(h)) {
-                       if (IS_ENABLED(CONFIG_CMA) && hugetlb_cma[0]) {
+                       if (hugetlb_cma_size) {
                                pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
                                break;
                        }
@@ -3060,7 +3042,7 @@ static void __init hugetlb_sysfs_init(void)
                err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
                                         hstate_kobjs, &hstate_attr_group);
                if (err)
-                       pr_err("Hugetlb: Unable to add hstate %s", h->name);
+                       pr_err("HugeTLB: Unable to add hstate %s", h->name);
        }
 }
 
@@ -3164,7 +3146,7 @@ static void hugetlb_register_node(struct node *node)
                                                nhs->hstate_kobjs,
                                                &per_node_hstate_attr_group);
                if (err) {
-                       pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
+                       pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
                                h->name, node->dev.id);
                        hugetlb_unregister_node(node);
                        break;
@@ -3212,23 +3194,41 @@ static int __init hugetlb_init(void)
 {
        int i;
 
-       if (!hugepages_supported())
+       if (!hugepages_supported()) {
+               if (hugetlb_max_hstate || default_hstate_max_huge_pages)
+                       pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n");
                return 0;
+       }
 
-       if (!size_to_hstate(default_hstate_size)) {
-               if (default_hstate_size != 0) {
-                       pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n",
-                              default_hstate_size, HPAGE_SIZE);
+       /*
+        * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists.  Some
+        * architectures depend on setup being done here.
+        */
+       hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
+       if (!parsed_default_hugepagesz) {
+               /*
+                * If we did not parse a default huge page size, set
+                * default_hstate_idx to HPAGE_SIZE hstate. And, if the
+                * number of huge pages for this default size was implicitly
+                * specified, set that here as well.
+                * Note that the implicit setting will overwrite an explicit
+                * setting.  A warning will be printed in this case.
+                */
+               default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
+               if (default_hstate_max_huge_pages) {
+                       if (default_hstate.max_huge_pages) {
+                               char buf[32];
+
+                               string_get_size(huge_page_size(&default_hstate),
+                                       1, STRING_UNITS_2, buf, 32);
+                               pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
+                                       default_hstate.max_huge_pages, buf);
+                               pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
+                                       default_hstate_max_huge_pages);
+                       }
+                       default_hstate.max_huge_pages =
+                               default_hstate_max_huge_pages;
                }
-
-               default_hstate_size = HPAGE_SIZE;
-               if (!size_to_hstate(default_hstate_size))
-                       hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
-       }
-       default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
-       if (default_hstate_max_huge_pages) {
-               if (!default_hstate.max_huge_pages)
-                       default_hstate.max_huge_pages = default_hstate_max_huge_pages;
        }
 
        hugetlb_cma_check();
@@ -3256,10 +3256,10 @@ static int __init hugetlb_init(void)
 }
 subsys_initcall(hugetlb_init);
 
-/* Should be called on processing a hugepagesz=... option */
-void __init hugetlb_bad_size(void)
+/* Overwritten by architectures with more huge page sizes */
+bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
 {
-       parsed_valid_hugepagesz = false;
+       return size == HPAGE_SIZE;
 }
 
 void __init hugetlb_add_hstate(unsigned int order)
@@ -3268,7 +3268,6 @@ void __init hugetlb_add_hstate(unsigned int order)
        unsigned long i;
 
        if (size_to_hstate(PAGE_SIZE << order)) {
-               pr_warn("hugepagesz= specified twice, ignoring\n");
                return;
        }
        BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
@@ -3289,20 +3288,29 @@ void __init hugetlb_add_hstate(unsigned int order)
        parsed_hstate = h;
 }
 
-static int __init hugetlb_nrpages_setup(char *s)
+/*
+ * hugepages command line processing
+ * hugepages normally follows a valid hugepagsz or default_hugepagsz
+ * specification.  If not, ignore the hugepages value.  hugepages can also
+ * be the first huge page command line  option in which case it implicitly
+ * specifies the number of huge pages for the default size.
+ */
+static int __init hugepages_setup(char *s)
 {
        unsigned long *mhp;
        static unsigned long *last_mhp;
 
        if (!parsed_valid_hugepagesz) {
-               pr_warn("hugepages = %s preceded by "
-                       "an unsupported hugepagesz, ignoring\n", s);
+               pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
                parsed_valid_hugepagesz = true;
-               return 1;
+               return 0;
        }
+
        /*
-        * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
-        * so this hugepages= parameter goes to the "default hstate".
+        * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
+        * yet, so this hugepages= parameter goes to the "default hstate".
+        * Otherwise, it goes with the previously parsed hugepagesz or
+        * default_hugepagesz.
         */
        else if (!hugetlb_max_hstate)
                mhp = &default_hstate_max_huge_pages;
@@ -3310,8 +3318,8 @@ static int __init hugetlb_nrpages_setup(char *s)
                mhp = &parsed_hstate->max_huge_pages;
 
        if (mhp == last_mhp) {
-               pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
-               return 1;
+               pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
+               return 0;
        }
 
        if (sscanf(s, "%lu", mhp) <= 0)
@@ -3329,22 +3337,118 @@ static int __init hugetlb_nrpages_setup(char *s)
 
        return 1;
 }
-__setup("hugepages=", hugetlb_nrpages_setup);
+__setup("hugepages=", hugepages_setup);
+
+/*
+ * hugepagesz command line processing
+ * A specific huge page size can only be specified once with hugepagesz.
+ * hugepagesz is followed by hugepages on the command line.  The global
+ * variable 'parsed_valid_hugepagesz' is used to determine if prior
+ * hugepagesz argument was valid.
+ */
+static int __init hugepagesz_setup(char *s)
+{
+       unsigned long size;
+       struct hstate *h;
+
+       parsed_valid_hugepagesz = false;
+       size = (unsigned long)memparse(s, NULL);
+
+       if (!arch_hugetlb_valid_size(size)) {
+               pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
+               return 0;
+       }
+
+       h = size_to_hstate(size);
+       if (h) {
+               /*
+                * hstate for this size already exists.  This is normally
+                * an error, but is allowed if the existing hstate is the
+                * default hstate.  More specifically, it is only allowed if
+                * the number of huge pages for the default hstate was not
+                * previously specified.
+                */
+               if (!parsed_default_hugepagesz ||  h != &default_hstate ||
+                   default_hstate.max_huge_pages) {
+                       pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
+                       return 0;
+               }
+
+               /*
+                * No need to call hugetlb_add_hstate() as hstate already
+                * exists.  But, do set parsed_hstate so that a following
+                * hugepages= parameter will be applied to this hstate.
+                */
+               parsed_hstate = h;
+               parsed_valid_hugepagesz = true;
+               return 1;
+       }
+
+       hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
+       parsed_valid_hugepagesz = true;
+       return 1;
+}
+__setup("hugepagesz=", hugepagesz_setup);
 
-static int __init hugetlb_default_setup(char *s)
+/*
+ * default_hugepagesz command line input
+ * Only one instance of default_hugepagesz allowed on command line.
+ */
+static int __init default_hugepagesz_setup(char *s)
 {
-       default_hstate_size = memparse(s, &s);
+       unsigned long size;
+
+       parsed_valid_hugepagesz = false;
+       if (parsed_default_hugepagesz) {
+               pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
+               return 0;
+       }
+
+       size = (unsigned long)memparse(s, NULL);
+
+       if (!arch_hugetlb_valid_size(size)) {
+               pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
+               return 0;
+       }
+
+       hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
+       parsed_valid_hugepagesz = true;
+       parsed_default_hugepagesz = true;
+       default_hstate_idx = hstate_index(size_to_hstate(size));
+
+       /*
+        * The number of default huge pages (for this size) could have been
+        * specified as the first hugetlb parameter: hugepages=X.  If so,
+        * then default_hstate_max_huge_pages is set.  If the default huge
+        * page size is gigantic (>= MAX_ORDER), then the pages must be
+        * allocated here from bootmem allocator.
+        */
+       if (default_hstate_max_huge_pages) {
+               default_hstate.max_huge_pages = default_hstate_max_huge_pages;
+               if (hstate_is_gigantic(&default_hstate))
+                       hugetlb_hstate_alloc_pages(&default_hstate);
+               default_hstate_max_huge_pages = 0;
+       }
+
        return 1;
 }
-__setup("default_hugepagesz=", hugetlb_default_setup);
+__setup("default_hugepagesz=", default_hugepagesz_setup);
 
-static unsigned int cpuset_mems_nr(unsigned int *array)
+static unsigned int allowed_mems_nr(struct hstate *h)
 {
        int node;
        unsigned int nr = 0;
+       nodemask_t *mpol_allowed;
+       unsigned int *array = h->free_huge_pages_node;
+       gfp_t gfp_mask = htlb_alloc_mask(h);
 
-       for_each_node_mask(node, cpuset_current_mems_allowed)
-               nr += array[node];
+       mpol_allowed = policy_nodemask_current(gfp_mask);
+
+       for_each_node_mask(node, cpuset_current_mems_allowed) {
+               if (!mpol_allowed ||
+                   (mpol_allowed && node_isset(node, *mpol_allowed)))
+                       nr += array[node];
+       }
 
        return nr;
 }
@@ -3352,7 +3456,7 @@ static unsigned int cpuset_mems_nr(unsigned int *array)
 #ifdef CONFIG_SYSCTL
 static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
                         struct ctl_table *table, int write,
-                        void __user *buffer, size_t *length, loff_t *ppos)
+                        void *buffer, size_t *length, loff_t *ppos)
 {
        struct hstate *h = &default_hstate;
        unsigned long tmp = h->max_huge_pages;
@@ -3375,7 +3479,7 @@ out:
 }
 
 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
-                         void __user *buffer, size_t *length, loff_t *ppos)
+                         void *buffer, size_t *length, loff_t *ppos)
 {
 
        return hugetlb_sysctl_handler_common(false, table, write,
@@ -3384,7 +3488,7 @@ int hugetlb_sysctl_handler(struct ctl_table *table, int write,
 
 #ifdef CONFIG_NUMA
 int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
-                         void __user *buffer, size_t *length, loff_t *ppos)
+                         void *buffer, size_t *length, loff_t *ppos)
 {
        return hugetlb_sysctl_handler_common(true, table, write,
                                                        buffer, length, ppos);
@@ -3392,8 +3496,7 @@ int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
 #endif /* CONFIG_NUMA */
 
 int hugetlb_overcommit_handler(struct ctl_table *table, int write,
-                       void __user *buffer,
-                       size_t *length, loff_t *ppos)
+               void *buffer, size_t *length, loff_t *ppos)
 {
        struct hstate *h = &default_hstate;
        unsigned long tmp;
@@ -3524,12 +3627,18 @@ static int hugetlb_acct_memory(struct hstate *h, long delta)
         * we fall back to check against current free page availability as
         * a best attempt and hopefully to minimize the impact of changing
         * semantics that cpuset has.
+        *
+        * Apart from cpuset, we also have memory policy mechanism that
+        * also determines from which node the kernel will allocate memory
+        * in a NUMA system. So similar to cpuset, we also should consider
+        * the memory policy of the current task. Similar to the description
+        * above.
         */
        if (delta > 0) {
                if (gather_surplus_pages(h, delta) < 0)
                        goto out;
 
-               if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
+               if (delta > allowed_mems_nr(h)) {
                        return_unused_surplus_pages(h, delta);
                        goto out;
                }
@@ -3834,7 +3943,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
                        continue;
 
                ptl = huge_pte_lock(h, mm, ptep);
-               if (huge_pmd_unshare(mm, &address, ptep)) {
+               if (huge_pmd_unshare(mm, vma, &address, ptep)) {
                        spin_unlock(ptl);
                        /*
                         * We just unmapped a page of PMDs by clearing a PUD.
@@ -4421,10 +4530,6 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
                } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
                        return VM_FAULT_HWPOISON_LARGE |
                                VM_FAULT_SET_HINDEX(hstate_index(h));
-       } else {
-               ptep = huge_pte_alloc(mm, haddr, huge_page_size(h));
-               if (!ptep)
-                       return VM_FAULT_OOM;
        }
 
        /*
@@ -4466,9 +4571,9 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
        /*
         * entry could be a migration/hwpoison entry at this point, so this
         * check prevents the kernel from going below assuming that we have
-        * a active hugepage in pagecache. This goto expects the 2nd page fault,
-        * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
-        * handle it.
+        * an active hugepage in pagecache. This goto expects the 2nd page
+        * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will
+        * properly handle it.
         */
        if (!pte_present(entry))
                goto out_mutex;
@@ -4583,7 +4688,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
                                                (const void __user *) src_addr,
                                                pages_per_huge_page(h), false);
 
-               /* fallback to copy_from_user outside mmap_sem */
+               /* fallback to copy_from_user outside mmap_lock */
                if (unlikely(ret)) {
                        ret = -ENOENT;
                        *pagep = page;
@@ -4901,7 +5006,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
                if (!ptep)
                        continue;
                ptl = huge_pte_lock(h, mm, ptep);
-               if (huge_pmd_unshare(mm, &address, ptep)) {
+               if (huge_pmd_unshare(mm, vma, &address, ptep)) {
                        pages++;
                        spin_unlock(ptl);
                        shared_pmd = true;
@@ -5195,25 +5300,21 @@ static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
                                unsigned long *start, unsigned long *end)
 {
-       unsigned long check_addr;
+       unsigned long a_start, a_end;
 
        if (!(vma->vm_flags & VM_MAYSHARE))
                return;
 
-       for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) {
-               unsigned long a_start = check_addr & PUD_MASK;
-               unsigned long a_end = a_start + PUD_SIZE;
+       /* Extend the range to be PUD aligned for a worst case scenario */
+       a_start = ALIGN_DOWN(*start, PUD_SIZE);
+       a_end = ALIGN(*end, PUD_SIZE);
 
-               /*
-                * If sharing is possible, adjust start/end if necessary.
-                */
-               if (range_in_vma(vma, a_start, a_end)) {
-                       if (a_start < *start)
-                               *start = a_start;
-                       if (a_end > *end)
-                               *end = a_end;
-               }
-       }
+       /*
+        * Intersect the range with the vma range, since pmd sharing won't be
+        * across vma after all
+        */
+       *start = max(vma->vm_start, a_start);
+       *end = min(vma->vm_end, a_end);
 }
 
 /*
@@ -5286,12 +5387,14 @@ out:
  * returns: 1 successfully unmapped a shared pte page
  *         0 the underlying pte page is not shared, or it is the last user
  */
-int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
+int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
+                                       unsigned long *addr, pte_t *ptep)
 {
        pgd_t *pgd = pgd_offset(mm, *addr);
        p4d_t *p4d = p4d_offset(pgd, *addr);
        pud_t *pud = pud_offset(p4d, *addr);
 
+       i_mmap_assert_write_locked(vma->vm_file->f_mapping);
        BUG_ON(page_count(virt_to_page(ptep)) == 0);
        if (page_count(virt_to_page(ptep)) == 1)
                return 0;
@@ -5309,7 +5412,8 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
        return NULL;
 }
 
-int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
+int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
+                               unsigned long *addr, pte_t *ptep)
 {
        return 0;
 }
@@ -5355,8 +5459,8 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
  * huge_pte_offset() - Walk the page table to resolve the hugepage
  * entry at address @addr
  *
- * Return: Pointer to page table or swap entry (PUD or PMD) for
- * address @addr, or NULL if a p*d_none() entry is encountered and the
+ * Return: Pointer to page table entry (PUD or PMD) for
+ * address @addr, or NULL if a !p*d_present() entry is encountered and the
  * size @sz doesn't match the hugepage size at this level of the page
  * table.
  */
@@ -5365,8 +5469,8 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
 {
        pgd_t *pgd;
        p4d_t *p4d;
-       pud_t *pud, pud_entry;
-       pmd_t *pmd, pmd_entry;
+       pud_t *pud;
+       pmd_t *pmd;
 
        pgd = pgd_offset(mm, addr);
        if (!pgd_present(*pgd))
@@ -5376,22 +5480,16 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
                return NULL;
 
        pud = pud_offset(p4d, addr);
-       pud_entry = READ_ONCE(*pud);
-       if (sz != PUD_SIZE && pud_none(pud_entry))
-               return NULL;
-       /* hugepage or swap? */
-       if (pud_huge(pud_entry) || !pud_present(pud_entry))
+       if (sz == PUD_SIZE)
+               /* must be pud huge, non-present or none */
                return (pte_t *)pud;
-
-       pmd = pmd_offset(pud, addr);
-       pmd_entry = READ_ONCE(*pmd);
-       if (sz != PMD_SIZE && pmd_none(pmd_entry))
+       if (!pud_present(*pud))
                return NULL;
-       /* hugepage or swap? */
-       if (pmd_huge(pmd_entry) || !pmd_present(pmd_entry))
-               return (pte_t *)pmd;
+       /* must have a valid entry and size to go further */
 
-       return NULL;
+       pmd = pmd_offset(pud, addr);
+       /* must be pmd huge, non-present or none */
+       return (pte_t *)pmd;
 }
 
 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
@@ -5548,7 +5646,6 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
 }
 
 #ifdef CONFIG_CMA
-static unsigned long hugetlb_cma_size __initdata;
 static bool cma_reserve_called __initdata;
 
 static int __init cmdline_parse_hugetlb_cma(char *p)
@@ -5586,12 +5683,14 @@ void __init hugetlb_cma_reserve(int order)
        reserved = 0;
        for_each_node_state(nid, N_ONLINE) {
                int res;
+               char name[20];
 
                size = min(per_node, hugetlb_cma_size - reserved);
                size = round_up(size, PAGE_SIZE << order);
 
+               snprintf(name, 20, "hugetlb%d", nid);
                res = cma_declare_contiguous_nid(0, size, 0, PAGE_SIZE << order,
-                                                0, false, "hugetlb",
+                                                0, false, name,
                                                 &hugetlb_cma[nid], nid);
                if (res) {
                        pr_warn("hugetlb_cma: reservation failed: err %d, node %d",