linux/arch/powerpc/mm/hugetlbpage.c
<<
>>
Prefs
   1/*
   2 * PPC Huge TLB Page Support for Kernel.
   3 *
   4 * Copyright (C) 2003 David Gibson, IBM Corporation.
   5 * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
   6 *
   7 * Based on the IA-32 version:
   8 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
   9 */
  10
  11#include <linux/mm.h>
  12#include <linux/io.h>
  13#include <linux/slab.h>
  14#include <linux/hugetlb.h>
  15#include <linux/export.h>
  16#include <linux/of_fdt.h>
  17#include <linux/memblock.h>
  18#include <linux/bootmem.h>
  19#include <linux/moduleparam.h>
  20#include <asm/pgtable.h>
  21#include <asm/pgalloc.h>
  22#include <asm/tlb.h>
  23#include <asm/setup.h>
  24#include <asm/hugetlb.h>
  25
  26#ifdef CONFIG_HUGETLB_PAGE
  27
  28#define PAGE_SHIFT_64K  16
  29#define PAGE_SHIFT_16M  24
  30#define PAGE_SHIFT_16G  34
  31
  32bool hugetlb_disabled = false;
  33
  34unsigned int HPAGE_SHIFT;
  35
  36/*
  37 * Tracks gpages after the device tree is scanned and before the
  38 * huge_boot_pages list is ready.  On non-Freescale implementations, this is
  39 * just used to track 16G pages and so is a single array.  FSL-based
  40 * implementations may have more than one gpage size, so we need multiple
  41 * arrays
  42 */
  43#ifdef CONFIG_PPC_FSL_BOOK3E
  44#define MAX_NUMBER_GPAGES       128
  45struct psize_gpages {
  46        u64 gpage_list[MAX_NUMBER_GPAGES];
  47        unsigned int nr_gpages;
  48};
  49static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT];
  50#else
  51#define MAX_NUMBER_GPAGES       1024
  52static u64 gpage_freearray[MAX_NUMBER_GPAGES];
  53static unsigned nr_gpages;
  54#endif
  55
  56#define hugepd_none(hpd)        ((hpd).pd == 0)
  57
  58#ifdef CONFIG_PPC_BOOK3S_64
  59/*
  60 * At this point we do the placement change only for BOOK3S 64. This would
  61 * possibly work on other subarchs.
  62 */
  63
  64/*
  65 * We have PGD_INDEX_SIZ = 12 and PTE_INDEX_SIZE = 8, so that we can have
  66 * 16GB hugepage pte in PGD and 16MB hugepage pte at PMD;
  67 */
  68int pmd_huge(pmd_t pmd)
  69{
  70        /*
  71         * leaf pte for huge page, bottom two bits != 00
  72         */
  73        return ((pmd_val(pmd) & 0x3) != 0x0);
  74}
  75
  76int pud_huge(pud_t pud)
  77{
  78        /*
  79         * leaf pte for huge page, bottom two bits != 00
  80         */
  81        return ((pud_val(pud) & 0x3) != 0x0);
  82}
  83
  84int pgd_huge(pgd_t pgd)
  85{
  86        /*
  87         * leaf pte for huge page, bottom two bits != 00
  88         */
  89        return ((pgd_val(pgd) & 0x3) != 0x0);
  90}
  91#else
  92int pmd_huge(pmd_t pmd)
  93{
  94        return 0;
  95}
  96
  97int pud_huge(pud_t pud)
  98{
  99        return 0;
 100}
 101
 102int pgd_huge(pgd_t pgd)
 103{
 104        return 0;
 105}
 106#endif
 107
 108pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 109{
 110        /* Only called for hugetlbfs pages, hence can ignore THP */
 111        return __find_linux_pte_or_hugepte(mm->pgd, addr, NULL, NULL);
 112}
 113
 114static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
 115                           unsigned long address, unsigned pdshift, unsigned pshift)
 116{
 117        struct kmem_cache *cachep;
 118        pte_t *new;
 119
 120#ifdef CONFIG_PPC_FSL_BOOK3E
 121        int i;
 122        int num_hugepd = 1 << (pshift - pdshift);
 123        cachep = hugepte_cache;
 124#else
 125        cachep = PGT_CACHE(pdshift - pshift);
 126#endif
 127
 128        new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT);
 129
 130        BUG_ON(pshift > HUGEPD_SHIFT_MASK);
 131        BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
 132
 133        if (! new)
 134                return -ENOMEM;
 135
 136        spin_lock(&mm->page_table_lock);
 137#ifdef CONFIG_PPC_FSL_BOOK3E
 138        /*
 139         * We have multiple higher-level entries that point to the same
 140         * actual pte location.  Fill in each as we go and backtrack on error.
 141         * We need all of these so the DTLB pgtable walk code can find the
 142         * right higher-level entry without knowing if it's a hugepage or not.
 143         */
 144        for (i = 0; i < num_hugepd; i++, hpdp++) {
 145                if (unlikely(!hugepd_none(*hpdp)))
 146                        break;
 147                else
 148                        /* We use the old format for PPC_FSL_BOOK3E */
 149                        hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
 150        }
 151        /* If we bailed from the for loop early, an error occurred, clean up */
 152        if (i < num_hugepd) {
 153                for (i = i - 1 ; i >= 0; i--, hpdp--)
 154                        hpdp->pd = 0;
 155                kmem_cache_free(cachep, new);
 156        }
 157#else
 158        if (!hugepd_none(*hpdp))
 159                kmem_cache_free(cachep, new);
 160        else {
 161#ifdef CONFIG_PPC_BOOK3S_64
 162                hpdp->pd = (unsigned long)new |
 163                            (shift_to_mmu_psize(pshift) << 2);
 164#else
 165                hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
 166#endif
 167        }
 168#endif
 169        spin_unlock(&mm->page_table_lock);
 170        return 0;
 171}
 172
 173/*
 174 * These macros define how to determine which level of the page table holds
 175 * the hpdp.
 176 */
 177#ifdef CONFIG_PPC_FSL_BOOK3E
 178#define HUGEPD_PGD_SHIFT PGDIR_SHIFT
 179#define HUGEPD_PUD_SHIFT PUD_SHIFT
 180#else
 181#define HUGEPD_PGD_SHIFT PUD_SHIFT
 182#define HUGEPD_PUD_SHIFT PMD_SHIFT
 183#endif
 184
 185#ifdef CONFIG_PPC_BOOK3S_64
 186/*
 187 * At this point we do the placement change only for BOOK3S 64. This would
 188 * possibly work on other subarchs.
 189 */
 190pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
 191{
 192        pgd_t *pg;
 193        pud_t *pu;
 194        pmd_t *pm;
 195        hugepd_t *hpdp = NULL;
 196        unsigned pshift = __ffs(sz);
 197        unsigned pdshift = PGDIR_SHIFT;
 198
 199        addr &= ~(sz-1);
 200        pg = pgd_offset(mm, addr);
 201
 202        if (pshift == PGDIR_SHIFT)
 203                /* 16GB huge page */
 204                return (pte_t *) pg;
 205        else if (pshift > PUD_SHIFT)
 206                /*
 207                 * We need to use hugepd table
 208                 */
 209                hpdp = (hugepd_t *)pg;
 210        else {
 211                pdshift = PUD_SHIFT;
 212                pu = pud_alloc(mm, pg, addr);
 213                if (pshift == PUD_SHIFT)
 214                        return (pte_t *)pu;
 215                else if (pshift > PMD_SHIFT)
 216                        hpdp = (hugepd_t *)pu;
 217                else {
 218                        pdshift = PMD_SHIFT;
 219                        pm = pmd_alloc(mm, pu, addr);
 220                        if (pshift == PMD_SHIFT)
 221                                /* 16MB hugepage */
 222                                return (pte_t *)pm;
 223                        else
 224                                hpdp = (hugepd_t *)pm;
 225                }
 226        }
 227        if (!hpdp)
 228                return NULL;
 229
 230        BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
 231
 232        if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
 233                return NULL;
 234
 235        return hugepte_offset(hpdp, addr, pdshift);
 236}
 237
 238#else
 239
 240pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
 241{
 242        pgd_t *pg;
 243        pud_t *pu;
 244        pmd_t *pm;
 245        hugepd_t *hpdp = NULL;
 246        unsigned pshift = __ffs(sz);
 247        unsigned pdshift = PGDIR_SHIFT;
 248
 249        addr &= ~(sz-1);
 250
 251        pg = pgd_offset(mm, addr);
 252
 253        if (pshift >= HUGEPD_PGD_SHIFT) {
 254                hpdp = (hugepd_t *)pg;
 255        } else {
 256                pdshift = PUD_SHIFT;
 257                pu = pud_alloc(mm, pg, addr);
 258                if (pshift >= HUGEPD_PUD_SHIFT) {
 259                        hpdp = (hugepd_t *)pu;
 260                } else {
 261                        pdshift = PMD_SHIFT;
 262                        pm = pmd_alloc(mm, pu, addr);
 263                        hpdp = (hugepd_t *)pm;
 264                }
 265        }
 266
 267        if (!hpdp)
 268                return NULL;
 269
 270        BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
 271
 272        if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
 273                return NULL;
 274
 275        return hugepte_offset(hpdp, addr, pdshift);
 276}
 277#endif
 278
 279#ifdef CONFIG_PPC_FSL_BOOK3E
 280/* Build list of addresses of gigantic pages.  This function is used in early
 281 * boot before the buddy or bootmem allocator is setup.
 282 */
 283void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
 284{
 285        unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
 286        int i;
 287
 288        if (addr == 0)
 289                return;
 290
 291        gpage_freearray[idx].nr_gpages = number_of_pages;
 292
 293        for (i = 0; i < number_of_pages; i++) {
 294                gpage_freearray[idx].gpage_list[i] = addr;
 295                addr += page_size;
 296        }
 297}
 298
 299/*
 300 * Moves the gigantic page addresses from the temporary list to the
 301 * huge_boot_pages list.
 302 */
 303int alloc_bootmem_huge_page(struct hstate *hstate)
 304{
 305        struct huge_bootmem_page *m;
 306        int idx = shift_to_mmu_psize(huge_page_shift(hstate));
 307        int nr_gpages = gpage_freearray[idx].nr_gpages;
 308
 309        if (nr_gpages == 0)
 310                return 0;
 311
 312#ifdef CONFIG_HIGHMEM
 313        /*
 314         * If gpages can be in highmem we can't use the trick of storing the
 315         * data structure in the page; allocate space for this
 316         */
 317        m = alloc_bootmem(sizeof(struct huge_bootmem_page));
 318        m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
 319#else
 320        m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
 321#endif
 322
 323        list_add(&m->list, &huge_boot_pages);
 324        gpage_freearray[idx].nr_gpages = nr_gpages;
 325        gpage_freearray[idx].gpage_list[nr_gpages] = 0;
 326        m->hstate = hstate;
 327
 328        return 1;
 329}
 330/*
 331 * Scan the command line hugepagesz= options for gigantic pages; store those in
 332 * a list that we use to allocate the memory once all options are parsed.
 333 */
 334
 335unsigned long gpage_npages[MMU_PAGE_COUNT];
 336
 337static int __init do_gpage_early_setup(char *param, char *val,
 338                                       const char *unused)
 339{
 340        static phys_addr_t size;
 341        unsigned long npages;
 342
 343        /*
 344         * The hugepagesz and hugepages cmdline options are interleaved.  We
 345         * use the size variable to keep track of whether or not this was done
 346         * properly and skip over instances where it is incorrect.  Other
 347         * command-line parsing code will issue warnings, so we don't need to.
 348         *
 349         */
 350        if ((strcmp(param, "default_hugepagesz") == 0) ||
 351            (strcmp(param, "hugepagesz") == 0)) {
 352                size = memparse(val, NULL);
 353        } else if (strcmp(param, "hugepages") == 0) {
 354                if (size != 0) {
 355                        if (sscanf(val, "%lu", &npages) <= 0)
 356                                npages = 0;
 357                        gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
 358                        size = 0;
 359                }
 360        }
 361        return 0;
 362}
 363
 364
 365/*
 366 * This function allocates physical space for pages that are larger than the
 367 * buddy allocator can handle.  We want to allocate these in highmem because
 368 * the amount of lowmem is limited.  This means that this function MUST be
 369 * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
 370 * allocate to grab highmem.
 371 */
 372void __init reserve_hugetlb_gpages(void)
 373{
 374        static __initdata char cmdline[COMMAND_LINE_SIZE];
 375        phys_addr_t size, base;
 376        int i;
 377
 378        strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
 379        parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0,
 380                        &do_gpage_early_setup);
 381
 382        /*
 383         * Walk gpage list in reverse, allocating larger page sizes first.
 384         * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
 385         * When we reach the point in the list where pages are no longer
 386         * considered gpages, we're done.
 387         */
 388        for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
 389                if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
 390                        continue;
 391                else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
 392                        break;
 393
 394                size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
 395                base = memblock_alloc_base(size * gpage_npages[i], size,
 396                                           MEMBLOCK_ALLOC_ANYWHERE);
 397                add_gpage(base, size, gpage_npages[i]);
 398        }
 399}
 400
 401#else /* !PPC_FSL_BOOK3E */
 402
 403/* Build list of addresses of gigantic pages.  This function is used in early
 404 * boot before the buddy or bootmem allocator is setup.
 405 */
 406void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
 407{
 408        if (!addr)
 409                return;
 410        while (number_of_pages > 0) {
 411                gpage_freearray[nr_gpages] = addr;
 412                nr_gpages++;
 413                number_of_pages--;
 414                addr += page_size;
 415        }
 416}
 417
 418/* Moves the gigantic page addresses from the temporary list to the
 419 * huge_boot_pages list.
 420 */
 421int alloc_bootmem_huge_page(struct hstate *hstate)
 422{
 423        struct huge_bootmem_page *m;
 424        if (nr_gpages == 0)
 425                return 0;
 426        m = phys_to_virt(gpage_freearray[--nr_gpages]);
 427        gpage_freearray[nr_gpages] = 0;
 428        list_add(&m->list, &huge_boot_pages);
 429        m->hstate = hstate;
 430        return 1;
 431}
 432#endif
 433
 434int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
 435{
 436        return 0;
 437}
 438
 439#ifdef CONFIG_PPC_FSL_BOOK3E
 440#define HUGEPD_FREELIST_SIZE \
 441        ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
 442
 443struct hugepd_freelist {
 444        struct rcu_head rcu;
 445        unsigned int index;
 446        void *ptes[0];
 447};
 448
 449static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
 450
 451static void hugepd_free_rcu_callback(struct rcu_head *head)
 452{
 453        struct hugepd_freelist *batch =
 454                container_of(head, struct hugepd_freelist, rcu);
 455        unsigned int i;
 456
 457        for (i = 0; i < batch->index; i++)
 458                kmem_cache_free(hugepte_cache, batch->ptes[i]);
 459
 460        free_page((unsigned long)batch);
 461}
 462
 463static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
 464{
 465        struct hugepd_freelist **batchp;
 466
 467        batchp = &get_cpu_var(hugepd_freelist_cur);
 468
 469        if (atomic_read(&tlb->mm->mm_users) < 2 ||
 470            cpumask_equal(mm_cpumask(tlb->mm),
 471                          cpumask_of(smp_processor_id()))) {
 472                kmem_cache_free(hugepte_cache, hugepte);
 473        put_cpu_var(hugepd_freelist_cur);
 474                return;
 475        }
 476
 477        if (*batchp == NULL) {
 478                *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
 479                (*batchp)->index = 0;
 480        }
 481
 482        (*batchp)->ptes[(*batchp)->index++] = hugepte;
 483        if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
 484                call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
 485                *batchp = NULL;
 486        }
 487        put_cpu_var(hugepd_freelist_cur);
 488}
 489#endif
 490
 491static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
 492                              unsigned long start, unsigned long end,
 493                              unsigned long floor, unsigned long ceiling)
 494{
 495        pte_t *hugepte = hugepd_page(*hpdp);
 496        int i;
 497
 498        unsigned long pdmask = ~((1UL << pdshift) - 1);
 499        unsigned int num_hugepd = 1;
 500
 501#ifdef CONFIG_PPC_FSL_BOOK3E
 502        /* Note: On fsl the hpdp may be the first of several */
 503        num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift));
 504#else
 505        unsigned int shift = hugepd_shift(*hpdp);
 506#endif
 507
 508        start &= pdmask;
 509        if (start < floor)
 510                return;
 511        if (ceiling) {
 512                ceiling &= pdmask;
 513                if (! ceiling)
 514                        return;
 515        }
 516        if (end - 1 > ceiling - 1)
 517                return;
 518
 519        for (i = 0; i < num_hugepd; i++, hpdp++)
 520                hpdp->pd = 0;
 521
 522#ifdef CONFIG_PPC_FSL_BOOK3E
 523        hugepd_free(tlb, hugepte);
 524#else
 525        pgtable_free_tlb(tlb, hugepte, pdshift - shift);
 526#endif
 527}
 528
 529static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
 530                                   unsigned long addr, unsigned long end,
 531                                   unsigned long floor, unsigned long ceiling)
 532{
 533        pmd_t *pmd;
 534        unsigned long next;
 535        unsigned long start;
 536
 537        start = addr;
 538        do {
 539                pmd = pmd_offset(pud, addr);
 540                next = pmd_addr_end(addr, end);
 541                if (!is_hugepd(pmd)) {
 542                        /*
 543                         * if it is not hugepd pointer, we should already find
 544                         * it cleared.
 545                         */
 546                        WARN_ON(!pmd_none_or_clear_bad(pmd));
 547                        continue;
 548                }
 549#ifdef CONFIG_PPC_FSL_BOOK3E
 550                /*
 551                 * Increment next by the size of the huge mapping since
 552                 * there may be more than one entry at this level for a
 553                 * single hugepage, but all of them point to
 554                 * the same kmem cache that holds the hugepte.
 555                 */
 556                next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
 557#endif
 558                free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
 559                                  addr, next, floor, ceiling);
 560        } while (addr = next, addr != end);
 561
 562        start &= PUD_MASK;
 563        if (start < floor)
 564                return;
 565        if (ceiling) {
 566                ceiling &= PUD_MASK;
 567                if (!ceiling)
 568                        return;
 569        }
 570        if (end - 1 > ceiling - 1)
 571                return;
 572
 573        pmd = pmd_offset(pud, start);
 574        pud_clear(pud);
 575        pmd_free_tlb(tlb, pmd, start);
 576}
 577
 578static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
 579                                   unsigned long addr, unsigned long end,
 580                                   unsigned long floor, unsigned long ceiling)
 581{
 582        pud_t *pud;
 583        unsigned long next;
 584        unsigned long start;
 585
 586        start = addr;
 587        do {
 588                pud = pud_offset(pgd, addr);
 589                next = pud_addr_end(addr, end);
 590                if (!is_hugepd(pud)) {
 591                        if (pud_none_or_clear_bad(pud))
 592                                continue;
 593                        hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
 594                                               ceiling);
 595                } else {
 596#ifdef CONFIG_PPC_FSL_BOOK3E
 597                        /*
 598                         * Increment next by the size of the huge mapping since
 599                         * there may be more than one entry at this level for a
 600                         * single hugepage, but all of them point to
 601                         * the same kmem cache that holds the hugepte.
 602                         */
 603                        next = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
 604#endif
 605                        free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
 606                                          addr, next, floor, ceiling);
 607                }
 608        } while (addr = next, addr != end);
 609
 610        start &= PGDIR_MASK;
 611        if (start < floor)
 612                return;
 613        if (ceiling) {
 614                ceiling &= PGDIR_MASK;
 615                if (!ceiling)
 616                        return;
 617        }
 618        if (end - 1 > ceiling - 1)
 619                return;
 620
 621        pud = pud_offset(pgd, start);
 622        pgd_clear(pgd);
 623        pud_free_tlb(tlb, pud, start);
 624}
 625
 626/*
 627 * This function frees user-level page tables of a process.
 628 *
 629 * Must be called with pagetable lock held.
 630 */
 631void hugetlb_free_pgd_range(struct mmu_gather *tlb,
 632                            unsigned long addr, unsigned long end,
 633                            unsigned long floor, unsigned long ceiling)
 634{
 635        pgd_t *pgd;
 636        unsigned long next;
 637
 638        /*
 639         * Because there are a number of different possible pagetable
 640         * layouts for hugepage ranges, we limit knowledge of how
 641         * things should be laid out to the allocation path
 642         * (huge_pte_alloc(), above).  Everything else works out the
 643         * structure as it goes from information in the hugepd
 644         * pointers.  That means that we can't here use the
 645         * optimization used in the normal page free_pgd_range(), of
 646         * checking whether we're actually covering a large enough
 647         * range to have to do anything at the top level of the walk
 648         * instead of at the bottom.
 649         *
 650         * To make sense of this, you should probably go read the big
 651         * block comment at the top of the normal free_pgd_range(),
 652         * too.
 653         */
 654
 655        do {
 656                next = pgd_addr_end(addr, end);
 657                pgd = pgd_offset(tlb->mm, addr);
 658                if (!is_hugepd(pgd)) {
 659                        if (pgd_none_or_clear_bad(pgd))
 660                                continue;
 661                        hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
 662                } else {
 663#ifdef CONFIG_PPC_FSL_BOOK3E
 664                        /*
 665                         * Increment next by the size of the huge mapping since
 666                         * there may be more than one entry at the pgd level
 667                         * for a single hugepage, but all of them point to the
 668                         * same kmem cache that holds the hugepte.
 669                         */
 670                        next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
 671#endif
 672                        free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
 673                                          addr, next, floor, ceiling);
 674                }
 675        } while (addr = next, addr != end);
 676}
 677
 678/*
 679 * We are holding mmap_sem, so a parallel huge page collapse cannot run.
 680 * To prevent hugepage split, disable irq.
 681 */
 682struct page *
 683follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
 684{
 685        bool is_thp;
 686        pte_t *ptep, pte;
 687        unsigned shift;
 688        unsigned long mask, flags;
 689        struct page *page = ERR_PTR(-EINVAL);
 690
 691        local_irq_save(flags);
 692        ptep = find_linux_pte_or_hugepte(mm->pgd, address, &is_thp, &shift);
 693        if (!ptep)
 694                goto no_page;
 695        pte = READ_ONCE(*ptep);
 696        /*
 697         * Verify it is a huge page else bail.
 698         * Transparent hugepages are handled by generic code. We can skip them
 699         * here.
 700         */
 701        if (!shift || is_thp)
 702                goto no_page;
 703
 704        if (!pte_present(pte)) {
 705                page = NULL;
 706                goto no_page;
 707        }
 708        mask = (1UL << shift) - 1;
 709        page = pte_page(pte);
 710        if (page)
 711                page += (address & mask) / PAGE_SIZE;
 712
 713no_page:
 714        local_irq_restore(flags);
 715        return page;
 716}
 717
 718struct page *
 719follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 720                pmd_t *pmd, int write)
 721{
 722        BUG();
 723        return NULL;
 724}
 725
 726struct page *
 727follow_huge_pud(struct mm_struct *mm, unsigned long address,
 728                pud_t *pud, int write)
 729{
 730        BUG();
 731        return NULL;
 732}
 733
 734static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
 735                                      unsigned long sz)
 736{
 737        unsigned long __boundary = (addr + sz) & ~(sz-1);
 738        return (__boundary - 1 < end - 1) ? __boundary : end;
 739}
 740
 741int gup_hugepd(hugepd_t *hugepd, unsigned pdshift,
 742               unsigned long addr, unsigned long end,
 743               int write, struct page **pages, int *nr)
 744{
 745        pte_t *ptep;
 746        unsigned long sz = 1UL << hugepd_shift(*hugepd);
 747        unsigned long next;
 748
 749        ptep = hugepte_offset(hugepd, addr, pdshift);
 750        do {
 751                next = hugepte_addr_end(addr, end, sz);
 752                if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
 753                        return 0;
 754        } while (ptep++, addr = next, addr != end);
 755
 756        return 1;
 757}
 758
 759#ifdef CONFIG_PPC_MM_SLICES
 760unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 761                                        unsigned long len, unsigned long pgoff,
 762                                        unsigned long flags)
 763{
 764        struct hstate *hstate = hstate_file(file);
 765        int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
 766
 767        return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
 768}
 769#endif
 770
 771unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
 772{
 773#ifdef CONFIG_PPC_MM_SLICES
 774        unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
 775
 776        return 1UL << mmu_psize_to_shift(psize);
 777#else
 778        if (!is_vm_hugetlb_page(vma))
 779                return PAGE_SIZE;
 780
 781        return huge_page_size(hstate_vma(vma));
 782#endif
 783}
 784
 785static inline bool is_power_of_4(unsigned long x)
 786{
 787        if (is_power_of_2(x))
 788                return (__ilog2(x) % 2) ? false : true;
 789        return false;
 790}
 791
 792static int __init add_huge_page_size(unsigned long long size)
 793{
 794        int shift = __ffs(size);
 795        int mmu_psize;
 796
 797        /* Check that it is a page size supported by the hardware and
 798         * that it fits within pagetable and slice limits. */
 799#ifdef CONFIG_PPC_FSL_BOOK3E
 800        if ((size < PAGE_SIZE) || !is_power_of_4(size))
 801                return -EINVAL;
 802#else
 803        if (!is_power_of_2(size)
 804            || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
 805                return -EINVAL;
 806#endif
 807
 808        if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
 809                return -EINVAL;
 810
 811#ifdef CONFIG_PPC_BOOK3S_64
 812        /*
 813         * We need to make sure that for different page sizes reported by
 814         * firmware we only add hugetlb support for page sizes that can be
 815         * supported by linux page table layout.
 816         * For now we have
 817         * Hash: 16M and 16G
 818         */
 819        if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
 820                return -EINVAL;
 821#endif
 822
 823#ifdef CONFIG_SPU_FS_64K_LS
 824        /* Disable support for 64K huge pages when 64K SPU local store
 825         * support is enabled as the current implementation conflicts.
 826         */
 827        if (shift == PAGE_SHIFT_64K)
 828                return -EINVAL;
 829#endif /* CONFIG_SPU_FS_64K_LS */
 830
 831        BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
 832
 833        /* Return if huge page size has already been setup */
 834        if (size_to_hstate(size))
 835                return 0;
 836
 837        hugetlb_add_hstate(shift - PAGE_SHIFT);
 838
 839        return 0;
 840}
 841
 842static int __init hugepage_setup_sz(char *str)
 843{
 844        unsigned long long size;
 845
 846        size = memparse(str, &str);
 847
 848        if (add_huge_page_size(size) != 0) {
 849                hugetlb_bad_size();
 850                pr_err("Invalid huge page size specified(%llu)\n", size);
 851        }
 852
 853        return 1;
 854}
 855__setup("hugepagesz=", hugepage_setup_sz);
 856
 857#ifdef CONFIG_PPC_FSL_BOOK3E
 858struct kmem_cache *hugepte_cache;
 859static int __init hugetlbpage_init(void)
 860{
 861        int psize;
 862
 863        for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
 864                unsigned shift;
 865
 866                if (!mmu_psize_defs[psize].shift)
 867                        continue;
 868
 869                shift = mmu_psize_to_shift(psize);
 870
 871                /* Don't treat normal page sizes as huge... */
 872                if (shift != PAGE_SHIFT)
 873                        if (add_huge_page_size(1ULL << shift) < 0)
 874                                continue;
 875        }
 876
 877        /*
 878         * Create a kmem cache for hugeptes.  The bottom bits in the pte have
 879         * size information encoded in them, so align them to allow this
 880         */
 881        hugepte_cache =  kmem_cache_create("hugepte-cache", sizeof(pte_t),
 882                                           HUGEPD_SHIFT_MASK + 1, 0, NULL);
 883        if (hugepte_cache == NULL)
 884                panic("%s: Unable to create kmem cache for hugeptes\n",
 885                      __func__);
 886
 887        /* Default hpage size = 4M */
 888        if (mmu_psize_defs[MMU_PAGE_4M].shift)
 889                HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
 890        else
 891                panic("%s: Unable to set default huge page size\n", __func__);
 892
 893
 894        return 0;
 895}
 896#else
 897static int __init hugetlbpage_init(void)
 898{
 899        int psize;
 900
 901        if (hugetlb_disabled) {
 902                pr_info("HugeTLB support is disabled!\n");
 903                return 0;
 904        }
 905
 906        if (!mmu_has_feature(MMU_FTR_16M_PAGE))
 907                return -ENODEV;
 908
 909        for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
 910                unsigned shift;
 911                unsigned pdshift;
 912
 913                if (!mmu_psize_defs[psize].shift)
 914                        continue;
 915
 916                shift = mmu_psize_to_shift(psize);
 917
 918                if (add_huge_page_size(1ULL << shift) < 0)
 919                        continue;
 920
 921                if (shift < PMD_SHIFT)
 922                        pdshift = PMD_SHIFT;
 923                else if (shift < PUD_SHIFT)
 924                        pdshift = PUD_SHIFT;
 925                else
 926                        pdshift = PGDIR_SHIFT;
 927                /*
 928                 * if we have pdshift and shift value same, we don't
 929                 * use pgt cache for hugepd.
 930                 */
 931                if (pdshift != shift) {
 932                        pgtable_cache_add(pdshift - shift, NULL);
 933                        if (!PGT_CACHE(pdshift - shift))
 934                                panic("hugetlbpage_init(): could not create "
 935                                      "pgtable cache for %d bit pagesize\n", shift);
 936                }
 937        }
 938
 939        /* Set default large page size. Currently, we pick 16M or 1M
 940         * depending on what is available
 941         */
 942        if (mmu_psize_defs[MMU_PAGE_16M].shift)
 943                HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
 944        else if (mmu_psize_defs[MMU_PAGE_1M].shift)
 945                HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
 946
 947        return 0;
 948}
 949#endif
 950module_init(hugetlbpage_init);
 951
 952void flush_dcache_icache_hugepage(struct page *page)
 953{
 954        int i;
 955        void *start;
 956
 957        BUG_ON(!PageCompound(page));
 958
 959        for (i = 0; i < (1UL << compound_order(page)); i++) {
 960                if (!PageHighMem(page)) {
 961                        __flush_dcache_icache(page_address(page+i));
 962                } else {
 963                        start = kmap_atomic(page+i);
 964                        __flush_dcache_icache(start);
 965                        kunmap_atomic(start);
 966                }
 967        }
 968}
 969
 970#endif /* CONFIG_HUGETLB_PAGE */
 971
 972/*
 973 * We have 4 cases for pgds and pmds:
 974 * (1) invalid (all zeroes)
 975 * (2) pointer to next table, as normal; bottom 6 bits == 0
 976 * (3) leaf pte for huge page, bottom two bits != 00
 977 * (4) hugepd pointer, bottom two bits == 00, next 4 bits indicate size of table
 978 *
 979 * So long as we atomically load page table pointers we are safe against teardown,
 980 * we can follow the address down to the the page and take a ref on it.
 981 * This function need to be called with interrupts disabled. We use this variant
 982 * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
 983 */
 984
 985pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
 986                                   bool *is_thp, unsigned *shift)
 987{
 988        pgd_t pgd, *pgdp;
 989        pud_t pud, *pudp;
 990        pmd_t pmd, *pmdp;
 991        pte_t *ret_pte;
 992        hugepd_t *hpdp = NULL;
 993        unsigned pdshift = PGDIR_SHIFT;
 994
 995        if (shift)
 996                *shift = 0;
 997
 998        if (is_thp)
 999                *is_thp = false;
1000
1001        pgdp = pgdir + pgd_index(ea);
1002        pgd  = READ_ONCE(*pgdp);
1003        /*
1004         * Always operate on the local stack value. This make sure the
1005         * value don't get updated by a parallel THP split/collapse,
1006         * page fault or a page unmap. The return pte_t * is still not
1007         * stable. So should be checked there for above conditions.
1008         */
1009        if (pgd_none(pgd))
1010                return NULL;
1011        else if (pgd_huge(pgd)) {
1012                ret_pte = (pte_t *) pgdp;
1013                goto out;
1014        } else if (is_hugepd(&pgd))
1015                hpdp = (hugepd_t *)&pgd;
1016        else {
1017                /*
1018                 * Even if we end up with an unmap, the pgtable will not
1019                 * be freed, because we do an rcu free and here we are
1020                 * irq disabled
1021                 */
1022                pdshift = PUD_SHIFT;
1023                pudp = pud_offset(&pgd, ea);
1024                pud  = ACCESS_ONCE(*pudp);
1025
1026                if (pud_none(pud))
1027                        return NULL;
1028                else if (pud_huge(pud)) {
1029                        ret_pte = (pte_t *) pudp;
1030                        goto out;
1031                } else if (is_hugepd(&pud))
1032                        hpdp = (hugepd_t *)&pud;
1033                else {
1034                        pdshift = PMD_SHIFT;
1035                        pmdp = pmd_offset(&pud, ea);
1036                        pmd  = ACCESS_ONCE(*pmdp);
1037                        /*
1038                         * A hugepage collapse is captured by pmd_none, because
1039                         * it mark the pmd none and do a hpte invalidate.
1040                         *
1041                         * We don't worry about pmd_trans_splitting here, The
1042                         * caller if it needs to handle the splitting case
1043                         * should check for that.
1044                         */
1045                        if (pmd_none(pmd))
1046                                return NULL;
1047
1048                        if (pmd_trans_huge(pmd)) {
1049                                if (is_thp)
1050                                        *is_thp = true;
1051                                ret_pte = (pte_t *) pmdp;
1052                                goto out;
1053                        }
1054
1055                        if (pmd_huge(pmd)) {
1056                                ret_pte = (pte_t *) pmdp;
1057                                goto out;
1058                        } else if (is_hugepd(&pmd))
1059                                hpdp = (hugepd_t *)&pmd;
1060                        else
1061                                return pte_offset_kernel(&pmd, ea);
1062                }
1063        }
1064        if (!hpdp)
1065                return NULL;
1066
1067        ret_pte = hugepte_offset(hpdp, ea, pdshift);
1068        pdshift = hugepd_shift(*hpdp);
1069out:
1070        if (shift)
1071                *shift = pdshift;
1072        return ret_pte;
1073}
1074EXPORT_SYMBOL_GPL(__find_linux_pte_or_hugepte);
1075
1076int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
1077                unsigned long end, int write, struct page **pages, int *nr)
1078{
1079        unsigned long mask;
1080        unsigned long pte_end;
1081        struct page *head, *page, *tail;
1082        pte_t pte;
1083        int refs;
1084
1085        pte_end = (addr + sz) & ~(sz-1);
1086        if (pte_end < end)
1087                end = pte_end;
1088
1089        pte = READ_ONCE(*ptep);
1090        mask = _PAGE_PRESENT | _PAGE_USER;
1091        if (write)
1092                mask |= _PAGE_RW;
1093
1094        if ((pte_val(pte) & mask) != mask)
1095                return 0;
1096
1097#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1098        /*
1099         * check for splitting here
1100         */
1101        if (pmd_trans_splitting(pte_pmd(pte)))
1102                return 0;
1103#endif
1104
1105        /* hugepages are never "special" */
1106        VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1107
1108        refs = 0;
1109        head = pte_page(pte);
1110
1111        page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
1112        tail = page;
1113        do {
1114                VM_BUG_ON(compound_head(page) != head);
1115                pages[*nr] = page;
1116                (*nr)++;
1117                page++;
1118                refs++;
1119        } while (addr += PAGE_SIZE, addr != end);
1120
1121        if (!page_cache_add_speculative(head, refs)) {
1122                *nr -= refs;
1123                return 0;
1124        }
1125
1126        if (unlikely(pte_val(pte) != pte_val(*ptep))) {
1127                /* Could be optimized better */
1128                *nr -= refs;
1129                while (refs--)
1130                        put_page(head);
1131                return 0;
1132        }
1133
1134        /*
1135         * Any tail page need their mapcount reference taken before we
1136         * return.
1137         */
1138        while (refs--) {
1139                if (PageTail(tail))
1140                        get_huge_page_tail(tail);
1141                tail++;
1142        }
1143
1144        return 1;
1145}
1146