hugetlbfs: add arch_hugetlb_valid_size
authorMike Kravetz <mike.kravetz@oracle.com>
Wed, 3 Jun 2020 23:00:34 +0000 (16:00 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 4 Jun 2020 03:09:46 +0000 (20:09 -0700)
Patch series "Clean up hugetlb boot command line processing", v4.

Longpeng(Mike) reported a weird message from hugetlb command line
processing and proposed a solution [1].  While the proposed patch does
address the specific issue, there are other related issues in command line
processing.  As hugetlbfs evolved, updates to command line processing have
been made to meet immediate needs and not necessarily in a coordinated
manner.  The result is that some processing is done in arch specific code,
some is done in arch independent code and coordination is problematic.
Semantics can vary between architectures.

The patch series does the following:
- Define arch specific arch_hugetlb_valid_size routine used to validate
  passed huge page sizes.
- Move hugepagesz= command line parsing out of arch specific code and into
  an arch independent routine.
- Clean up command line processing to follow desired semantics and
  document those semantics.

[1] https://lore.kernel.org/linux-mm/20200305033014.1152-1-longpeng2@huawei.com

This patch (of 3):

The architecture independent routine hugetlb_default_setup sets up the
default huge pages size.  It has no way to verify if the passed value is
valid, so it accepts it and attempts to validate at a later time.  This
requires undocumented cooperation between the arch specific and arch
independent code.

For architectures that support more than one huge page size, provide a
routine arch_hugetlb_valid_size to validate a huge page size.
hugetlb_default_setup can use this to validate passed values.

arch_hugetlb_valid_size will also be used in a subsequent patch to move
processing of the "hugepagesz=" in arch specific code to a common routine
in arch independent code.

Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Gerald Schaefer <gerald.schaefer@de.ibm.com> [s390]
Acked-by: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Longpeng <longpeng2@huawei.com>
Cc: Christophe Leroy <christophe.leroy@c-s.fr>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Mina Almasry <almasrymina@google.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Nitesh Narayan Lal <nitesh@redhat.com>
Cc: Anders Roxell <anders.roxell@linaro.org>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
Cc: Qian Cai <cai@lca.pw>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Link: http://lkml.kernel.org/r/20200428205614.246260-1-mike.kravetz@oracle.com
Link: http://lkml.kernel.org/r/20200428205614.246260-2-mike.kravetz@oracle.com
Link: http://lkml.kernel.org/r/20200417185049.275845-1-mike.kravetz@oracle.com
Link: http://lkml.kernel.org/r/20200417185049.275845-2-mike.kravetz@oracle.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
arch/arm64/mm/hugetlbpage.c
arch/powerpc/mm/hugetlbpage.c
arch/riscv/mm/hugetlbpage.c
arch/s390/mm/hugetlbpage.c
arch/sparc/mm/init_64.c
arch/x86/mm/hugetlbpage.c
include/linux/hugetlb.h
mm/hugetlb.c

index 0be3355e34997544aa43cd652c733a996b9e3ff6..2ac41cefe699a6da5072dc5aa333548086a21270 100644 (file)
@@ -464,17 +464,26 @@ static int __init hugetlbpage_init(void)
 }
 arch_initcall(hugetlbpage_init);
 
-static __init int setup_hugepagesz(char *opt)
+bool __init arch_hugetlb_valid_size(unsigned long size)
 {
-       unsigned long ps = memparse(opt, &opt);
-
-       switch (ps) {
+       switch (size) {
 #ifdef CONFIG_ARM64_4K_PAGES
        case PUD_SIZE:
 #endif
        case CONT_PMD_SIZE:
        case PMD_SIZE:
        case CONT_PTE_SIZE:
+               return true;
+       }
+
+       return false;
+}
+
+static __init int setup_hugepagesz(char *opt)
+{
+       unsigned long ps = memparse(opt, &opt);
+
+       if (arch_hugetlb_valid_size(ps)) {
                add_huge_page_size(ps);
                return 1;
        }
index 33b3461d91e8db0e7b19f569a5c20d06a9a827be..de54d2a37830096dcd25e304da98efe96de442c6 100644 (file)
@@ -558,7 +558,7 @@ unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
        return vma_kernel_pagesize(vma);
 }
 
-static int __init add_huge_page_size(unsigned long long size)
+bool __init arch_hugetlb_valid_size(unsigned long size)
 {
        int shift = __ffs(size);
        int mmu_psize;
@@ -566,20 +566,26 @@ static int __init add_huge_page_size(unsigned long long size)
        /* Check that it is a page size supported by the hardware and
         * that it fits within pagetable and slice limits. */
        if (size <= PAGE_SIZE || !is_power_of_2(size))
-               return -EINVAL;
+               return false;
 
        mmu_psize = check_and_get_huge_psize(shift);
        if (mmu_psize < 0)
-               return -EINVAL;
+               return false;
 
        BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
 
-       /* Return if huge page size has already been setup */
-       if (size_to_hstate(size))
-               return 0;
+       return true;
+}
 
-       hugetlb_add_hstate(shift - PAGE_SHIFT);
+static int __init add_huge_page_size(unsigned long long size)
+{
+       int shift = __ffs(size);
+
+       if (!arch_hugetlb_valid_size((unsigned long)size))
+               return -EINVAL;
 
+       if (!size_to_hstate(size))
+               hugetlb_add_hstate(shift - PAGE_SHIFT);
        return 0;
 }
 
index a6189ed36c5fbdef1b257c5c714d4afe26729965..da1f516bc451b245b81306a00f004c884133f8c7 100644 (file)
@@ -12,21 +12,29 @@ int pmd_huge(pmd_t pmd)
        return pmd_leaf(pmd);
 }
 
+bool __init arch_hugetlb_valid_size(unsigned long size)
+{
+       if (size == HPAGE_SIZE)
+               return true;
+       else if (IS_ENABLED(CONFIG_64BIT) && size == PUD_SIZE)
+               return true;
+       else
+               return false;
+}
+
 static __init int setup_hugepagesz(char *opt)
 {
        unsigned long ps = memparse(opt, &opt);
 
-       if (ps == HPAGE_SIZE) {
-               hugetlb_add_hstate(HPAGE_SHIFT - PAGE_SHIFT);
-       } else if (IS_ENABLED(CONFIG_64BIT) && ps == PUD_SIZE) {
-               hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
-       } else {
-               hugetlb_bad_size();
-               pr_err("hugepagesz: Unsupported page size %lu M\n", ps >> 20);
-               return 0;
+       if (arch_hugetlb_valid_size(ps)) {
+               hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
+               return 1;
        }
 
-       return 1;
+       hugetlb_bad_size();
+       pr_err("hugepagesz: Unsupported page size %lu M\n", ps >> 20);
+       return 0;
+
 }
 __setup("hugepagesz=", setup_hugepagesz);
 
index 4632d4e26b66218d0bc63a368b16eaddf48a6eb4..2f2b6b5b3d296b559e59bdc860779013ea892afd 100644 (file)
@@ -254,16 +254,24 @@ follow_huge_pud(struct mm_struct *mm, unsigned long address,
        return pud_page(*pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
 }
 
+bool __init arch_hugetlb_valid_size(unsigned long size)
+{
+       if (MACHINE_HAS_EDAT1 && size == PMD_SIZE)
+               return true;
+       else if (MACHINE_HAS_EDAT2 && size == PUD_SIZE)
+               return true;
+       else
+               return false;
+}
+
 static __init int setup_hugepagesz(char *opt)
 {
        unsigned long size;
        char *string = opt;
 
        size = memparse(opt, &opt);
-       if (MACHINE_HAS_EDAT1 && size == PMD_SIZE) {
-               hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
-       } else if (MACHINE_HAS_EDAT2 && size == PUD_SIZE) {
-               hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+       if (arch_hugetlb_valid_size(size)) {
+               hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
        } else {
                hugetlb_bad_size();
                pr_err("hugepagesz= specifies an unsupported page size %s\n",
index 79d3c5e0802e70d50fdd91f2ae9b767fc5939d86..24475b73657a1fb3b01b8126a1f5971a3a585583 100644 (file)
@@ -360,16 +360,11 @@ static void __init pud_huge_patch(void)
        __asm__ __volatile__("flush %0" : : "r" (addr));
 }
 
-static int __init setup_hugepagesz(char *string)
+bool __init arch_hugetlb_valid_size(unsigned long size)
 {
-       unsigned long long hugepage_size;
-       unsigned int hugepage_shift;
+       unsigned int hugepage_shift = ilog2(size);
        unsigned short hv_pgsz_idx;
        unsigned int hv_pgsz_mask;
-       int rc = 0;
-
-       hugepage_size = memparse(string, &string);
-       hugepage_shift = ilog2(hugepage_size);
 
        switch (hugepage_shift) {
        case HPAGE_16GB_SHIFT:
@@ -397,7 +392,20 @@ static int __init setup_hugepagesz(char *string)
                hv_pgsz_mask = 0;
        }
 
-       if ((hv_pgsz_mask & cpu_pgsz_mask) == 0U) {
+       if ((hv_pgsz_mask & cpu_pgsz_mask) == 0U)
+               return false;
+
+       return true;
+}
+
+static int __init setup_hugepagesz(char *string)
+{
+       unsigned long long hugepage_size;
+       int rc = 0;
+
+       hugepage_size = memparse(string, &string);
+
+       if (!arch_hugetlb_valid_size((unsigned long)hugepage_size)) {
                hugetlb_bad_size();
                pr_err("hugepagesz=%llu not supported by MMU.\n",
                        hugepage_size);
index 5bfd5aef53787a349519fea0ee73086d0dcfc7e7..1c4372bfe7826f69f21617a8c72dc097444c466f 100644 (file)
@@ -181,13 +181,22 @@ get_unmapped_area:
 #endif /* CONFIG_HUGETLB_PAGE */
 
 #ifdef CONFIG_X86_64
+bool __init arch_hugetlb_valid_size(unsigned long size)
+{
+       if (size == PMD_SIZE)
+               return true;
+       else if (size == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES))
+               return true;
+       else
+               return false;
+}
+
 static __init int setup_hugepagesz(char *opt)
 {
        unsigned long ps = memparse(opt, &opt);
-       if (ps == PMD_SIZE) {
-               hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
-       } else if (ps == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES)) {
-               hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
+
+       if (arch_hugetlb_valid_size(ps)) {
+               hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
        } else {
                hugetlb_bad_size();
                printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
index 43a1cef8f0f163b0102badf965098c38129e1f23..2eb15f5ab01ee201d0674ea97dd3c54ce763d148 100644 (file)
@@ -521,6 +521,7 @@ int __init alloc_bootmem_huge_page(struct hstate *h);
 
 void __init hugetlb_bad_size(void);
 void __init hugetlb_add_hstate(unsigned order);
+bool __init arch_hugetlb_valid_size(unsigned long size);
 struct hstate *size_to_hstate(unsigned long size);
 
 #ifndef HUGE_MAX_HSTATE
index bcabbe02192b1c8471b789adb77f333babc85110..63ca4241ea87c35f066322004a709e86923b2f25 100644 (file)
@@ -3256,6 +3256,12 @@ static int __init hugetlb_init(void)
 }
 subsys_initcall(hugetlb_init);
 
+/* Overwritten by architectures with more huge page sizes */
+bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
+{
+       return size == HPAGE_SIZE;
+}
+
 /* Should be called on processing a hugepagesz=... option */
 void __init hugetlb_bad_size(void)
 {
@@ -3331,12 +3337,21 @@ static int __init hugetlb_nrpages_setup(char *s)
 }
 __setup("hugepages=", hugetlb_nrpages_setup);
 
-static int __init hugetlb_default_setup(char *s)
+static int __init default_hugepagesz_setup(char *s)
 {
-       default_hstate_size = memparse(s, &s);
+       unsigned long size;
+
+       size = (unsigned long)memparse(s, NULL);
+
+       if (!arch_hugetlb_valid_size(size)) {
+               pr_err("HugeTLB: unsupported default_hugepagesz %s\n", s);
+               return 0;
+       }
+
+       default_hstate_size = size;
        return 1;
 }
-__setup("default_hugepagesz=", hugetlb_default_setup);
+__setup("default_hugepagesz=", default_hugepagesz_setup);
 
 static unsigned int cpuset_mems_nr(unsigned int *array)
 {