Path: blob/master/arch/powerpc/mm/book3s64/hash_pgtable.c
52395 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright 2005, Paul Mackerras, IBM Corporation.3* Copyright 2009, Benjamin Herrenschmidt, IBM Corporation.4* Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.5*/67#include <linux/sched.h>8#include <linux/mm_types.h>9#include <linux/mm.h>10#include <linux/page_table_check.h>11#include <linux/stop_machine.h>1213#include <asm/sections.h>14#include <asm/mmu.h>15#include <asm/tlb.h>16#include <asm/firmware.h>1718#include <mm/mmu_decl.h>1920#include <trace/events/thp.h>2122#if H_PGTABLE_RANGE > (USER_VSID_RANGE * (TASK_SIZE_USER64 / TASK_CONTEXT_SIZE))23#warning Limited user VSID range means pagetable space is wasted24#endif2526#ifdef CONFIG_SPARSEMEM_VMEMMAP27/*28* vmemmap is the starting address of the virtual address space where29* struct pages are allocated for all possible PFNs present on the system30* including holes and bad memory (hence sparse). These virtual struct31* pages are stored in sequence in this virtual address space irrespective32* of the fact whether the corresponding PFN is valid or not. This achieves33* constant relationship between address of struct page and its PFN.34*35* During boot or memory hotplug operation when a new memory section is36* added, physical memory allocation (including hash table bolting) will37* be performed for the set of struct pages which are part of the memory38* section. This saves memory by not allocating struct pages for PFNs39* which are not valid.40*41* ----------------------------------------------42* | PHYSICAL ALLOCATION OF VIRTUAL STRUCT PAGES|43* ----------------------------------------------44*45* f000000000000000 c00000000000000046* vmemmap +--------------+ +--------------+47* + | page struct | +--------------> | page struct |48* | +--------------+ +--------------+49* | | page struct | +--------------> | page struct |50* | +--------------+ | +--------------+51* | | page struct | + +------> | page struct |52* | +--------------+ | +--------------+53* | | page struct | | +--> | page struct |54* | +--------------+ | | +--------------+55* | | page struct | | |56* | +--------------+ | |57* | | page struct | | |58* | +--------------+ | |59* | | page struct | | |60* | +--------------+ | |61* | | page struct | | |62* | +--------------+ | |63* | | page struct | +-------+ |64* | +--------------+ |65* | | page struct | +-----------+66* | +--------------+67* | | page struct | No mapping68* | +--------------+69* | | page struct | No mapping70* v +--------------+71*72* -----------------------------------------73* | RELATION BETWEEN STRUCT PAGES AND PFNS|74* -----------------------------------------75*76* vmemmap +--------------+ +---------------+77* + | page struct | +-------------> | PFN |78* | +--------------+ +---------------+79* | | page struct | +-------------> | PFN |80* | +--------------+ +---------------+81* | | page struct | +-------------> | PFN |82* | +--------------+ +---------------+83* | | page struct | +-------------> | PFN |84* | +--------------+ +---------------+85* | | |86* | +--------------+87* | | |88* | +--------------+89* | | |90* | +--------------+ +---------------+91* | | page struct | +-------------> | PFN |92* | +--------------+ +---------------+93* | | |94* | +--------------+95* | | |96* | +--------------+ +---------------+97* | | page struct | +-------------> | PFN |98* | +--------------+ +---------------+99* | | page struct | +-------------> | PFN |100* v +--------------+ +---------------+101*/102/*103* On hash-based CPUs, the vmemmap is bolted in the hash table.104*105*/106int __meminit hash__vmemmap_create_mapping(unsigned long start,107unsigned long page_size,108unsigned long phys)109{110int rc;111112if ((start + page_size) >= H_VMEMMAP_END) {113pr_warn("Outside the supported range\n");114return -1;115}116117rc = htab_bolt_mapping(start, start + page_size, phys,118pgprot_val(PAGE_KERNEL),119mmu_vmemmap_psize, mmu_kernel_ssize);120if (rc < 0) {121int rc2 = htab_remove_mapping(start, start + page_size,122mmu_vmemmap_psize,123mmu_kernel_ssize);124BUG_ON(rc2 && (rc2 != -ENOENT));125}126return rc;127}128129#ifdef CONFIG_MEMORY_HOTPLUG130void hash__vmemmap_remove_mapping(unsigned long start,131unsigned long page_size)132{133int rc = htab_remove_mapping(start, start + page_size,134mmu_vmemmap_psize,135mmu_kernel_ssize);136BUG_ON((rc < 0) && (rc != -ENOENT));137WARN_ON(rc == -ENOENT);138}139#endif140#endif /* CONFIG_SPARSEMEM_VMEMMAP */141142/*143* map_kernel_page currently only called by __ioremap144* map_kernel_page adds an entry to the ioremap page table145* and adds an entry to the HPT, possibly bolting it146*/147int hash__map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot)148{149pgd_t *pgdp;150p4d_t *p4dp;151pud_t *pudp;152pmd_t *pmdp;153pte_t *ptep;154155BUILD_BUG_ON(TASK_SIZE_USER64 > H_PGTABLE_RANGE);156if (slab_is_available()) {157pgdp = pgd_offset_k(ea);158p4dp = p4d_offset(pgdp, ea);159pudp = pud_alloc(&init_mm, p4dp, ea);160if (!pudp)161return -ENOMEM;162pmdp = pmd_alloc(&init_mm, pudp, ea);163if (!pmdp)164return -ENOMEM;165ptep = pte_alloc_kernel(pmdp, ea);166if (!ptep)167return -ENOMEM;168set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, prot));169} else {170/*171* If the mm subsystem is not fully up, we cannot create a172* linux page table entry for this mapping. Simply bolt an173* entry in the hardware page table.174*175*/176if (htab_bolt_mapping(ea, ea + PAGE_SIZE, pa, pgprot_val(prot),177mmu_io_psize, mmu_kernel_ssize)) {178printk(KERN_ERR "Failed to do bolted mapping IO "179"memory at %016lx !\n", pa);180return -ENOMEM;181}182}183184smp_wmb();185return 0;186}187188#ifdef CONFIG_TRANSPARENT_HUGEPAGE189190unsigned long hash__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr,191pmd_t *pmdp, unsigned long clr,192unsigned long set)193{194__be64 old_be, tmp;195unsigned long old;196197#ifdef CONFIG_DEBUG_VM198WARN_ON(!hash__pmd_trans_huge(*pmdp));199assert_spin_locked(pmd_lockptr(mm, pmdp));200#endif201202__asm__ __volatile__(203"1: ldarx %0,0,%3\n\204and. %1,%0,%6\n\205bne- 1b \n\206andc %1,%0,%4 \n\207or %1,%1,%7\n\208stdcx. %1,0,%3 \n\209bne- 1b"210: "=&r" (old_be), "=&r" (tmp), "=m" (*pmdp)211: "r" (pmdp), "r" (cpu_to_be64(clr)), "m" (*pmdp),212"r" (cpu_to_be64(H_PAGE_BUSY)), "r" (cpu_to_be64(set))213: "cc" );214215old = be64_to_cpu(old_be);216217trace_hugepage_update_pmd(addr, old, clr, set);218if (old & H_PAGE_HASHPTE)219hpte_do_hugepage_flush(mm, addr, pmdp, old);220return old;221}222223pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long address,224pmd_t *pmdp)225{226pmd_t pmd;227228VM_BUG_ON(address & ~HPAGE_PMD_MASK);229VM_BUG_ON(pmd_trans_huge(*pmdp));230231pmd = *pmdp;232pmd_clear(pmdp);233234page_table_check_pmd_clear(vma->vm_mm, address, pmd);235236/*237* Wait for all pending hash_page to finish. This is needed238* in case of subpage collapse. When we collapse normal pages239* to hugepage, we first clear the pmd, then invalidate all240* the PTE entries. The assumption here is that any low level241* page fault will see a none pmd and take the slow path that242* will wait on mmap_lock. But we could very well be in a243* hash_page with local ptep pointer value. Such a hash page244* can result in adding new HPTE entries for normal subpages.245* That means we could be modifying the page content as we246* copy them to a huge page. So wait for parallel hash_page247* to finish before invalidating HPTE entries. We can do this248* by sending an IPI to all the cpus and executing a dummy249* function there.250*/251serialize_against_pte_lookup(vma->vm_mm);252/*253* Now invalidate the hpte entries in the range254* covered by pmd. This make sure we take a255* fault and will find the pmd as none, which will256* result in a major fault which takes mmap_lock and257* hence wait for collapse to complete. Without this258* the __collapse_huge_page_copy can result in copying259* the old content.260*/261flush_hash_table_pmd_range(vma->vm_mm, &pmd, address);262return pmd;263}264265/*266* We want to put the pgtable in pmd and use pgtable for tracking267* the base page size hptes268*/269void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,270pgtable_t pgtable)271{272pgtable_t *pgtable_slot;273274assert_spin_locked(pmd_lockptr(mm, pmdp));275/*276* we store the pgtable in the second half of PMD277*/278pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;279*pgtable_slot = pgtable;280/*281* expose the deposited pgtable to other cpus.282* before we set the hugepage PTE at pmd level283* hash fault code looks at the deposted pgtable284* to store hash index values.285*/286smp_wmb();287}288289pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)290{291pgtable_t pgtable;292pgtable_t *pgtable_slot;293294assert_spin_locked(pmd_lockptr(mm, pmdp));295296pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;297pgtable = *pgtable_slot;298/*299* Once we withdraw, mark the entry NULL.300*/301*pgtable_slot = NULL;302/*303* We store HPTE information in the deposited PTE fragment.304* zero out the content on withdraw.305*/306memset(pgtable, 0, PTE_FRAG_SIZE);307return pgtable;308}309310/*311* A linux hugepage PMD was changed and the corresponding hash table entries312* neesd to be flushed.313*/314void hpte_do_hugepage_flush(struct mm_struct *mm, unsigned long addr,315pmd_t *pmdp, unsigned long old_pmd)316{317int ssize;318unsigned int psize;319unsigned long vsid;320unsigned long flags = 0;321322/* get the base page size,vsid and segment size */323#ifdef CONFIG_DEBUG_VM324psize = get_slice_psize(mm, addr);325BUG_ON(psize == MMU_PAGE_16M);326#endif327if (old_pmd & H_PAGE_COMBO)328psize = MMU_PAGE_4K;329else330psize = MMU_PAGE_64K;331332if (!is_kernel_addr(addr)) {333ssize = user_segment_size(addr);334vsid = get_user_vsid(&mm->context, addr, ssize);335WARN_ON(vsid == 0);336} else {337vsid = get_kernel_vsid(addr, mmu_kernel_ssize);338ssize = mmu_kernel_ssize;339}340341if (mm_is_thread_local(mm))342flags |= HPTE_LOCAL_UPDATE;343344return flush_hash_hugepage(vsid, addr, pmdp, psize, ssize, flags);345}346347pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm,348unsigned long addr, pmd_t *pmdp)349{350pmd_t old_pmd;351pgtable_t pgtable;352unsigned long old;353pgtable_t *pgtable_slot;354355old = pmd_hugepage_update(mm, addr, pmdp, ~0UL, 0);356old_pmd = __pmd(old);357/*358* We have pmd == none and we are holding page_table_lock.359* So we can safely go and clear the pgtable hash360* index info.361*/362pgtable_slot = (pgtable_t *)pmdp + PTRS_PER_PMD;363pgtable = *pgtable_slot;364/*365* Let's zero out old valid and hash index details366* hash fault look at them.367*/368memset(pgtable, 0, PTE_FRAG_SIZE);369return old_pmd;370}371372int hash__has_transparent_hugepage(void)373{374375if (!mmu_has_feature(MMU_FTR_16M_PAGE))376return 0;377/*378* We support THP only if PMD_SIZE is 16MB.379*/380if (mmu_psize_defs[MMU_PAGE_16M].shift != PMD_SHIFT)381return 0;382/*383* We need to make sure that we support 16MB hugepage in a segment384* with base page size 64K or 4K. We only enable THP with a PAGE_SIZE385* of 64K.386*/387/*388* If we have 64K HPTE, we will be using that by default389*/390if (mmu_psize_defs[MMU_PAGE_64K].shift &&391(mmu_psize_defs[MMU_PAGE_64K].penc[MMU_PAGE_16M] == -1))392return 0;393/*394* Ok we only have 4K HPTE395*/396if (mmu_psize_defs[MMU_PAGE_4K].penc[MMU_PAGE_16M] == -1)397return 0;398399return 1;400}401EXPORT_SYMBOL_GPL(hash__has_transparent_hugepage);402403#endif /* CONFIG_TRANSPARENT_HUGEPAGE */404405#ifdef CONFIG_STRICT_KERNEL_RWX406407struct change_memory_parms {408unsigned long start, end, newpp;409unsigned int step, nr_cpus;410atomic_t master_cpu;411atomic_t cpu_counter;412};413414// We'd rather this was on the stack but it has to be in the RMO415static struct change_memory_parms chmem_parms;416417// And therefore we need a lock to protect it from concurrent use418static DEFINE_MUTEX(chmem_lock);419420static void change_memory_range(unsigned long start, unsigned long end,421unsigned int step, unsigned long newpp)422{423unsigned long idx;424425pr_debug("Changing page protection on range 0x%lx-0x%lx, to 0x%lx, step 0x%x\n",426start, end, newpp, step);427428for (idx = start; idx < end; idx += step)429/* Not sure if we can do much with the return value */430mmu_hash_ops.hpte_updateboltedpp(newpp, idx, mmu_linear_psize,431mmu_kernel_ssize);432}433434static int notrace chmem_secondary_loop(struct change_memory_parms *parms)435{436unsigned long msr, tmp, flags;437int *p;438439p = &parms->cpu_counter.counter;440441local_irq_save(flags);442hard_irq_disable();443444asm volatile (445// Switch to real mode and leave interrupts off446"mfmsr %[msr] ;"447"li %[tmp], %[MSR_IR_DR] ;"448"andc %[tmp], %[msr], %[tmp] ;"449"mtmsrd %[tmp] ;"450451// Tell the master we are in real mode452"1: "453"lwarx %[tmp], 0, %[p] ;"454"addic %[tmp], %[tmp], -1 ;"455"stwcx. %[tmp], 0, %[p] ;"456"bne- 1b ;"457458// Spin until the counter goes to zero459"2: ;"460"lwz %[tmp], 0(%[p]) ;"461"cmpwi %[tmp], 0 ;"462"bne- 2b ;"463464// Switch back to virtual mode465"mtmsrd %[msr] ;"466467: // outputs468[msr] "=&r" (msr), [tmp] "=&b" (tmp), "+m" (*p)469: // inputs470[p] "b" (p), [MSR_IR_DR] "i" (MSR_IR | MSR_DR)471: // clobbers472"cc", "xer"473);474475local_irq_restore(flags);476477return 0;478}479480static int change_memory_range_fn(void *data)481{482struct change_memory_parms *parms = data;483484// First CPU goes through, all others wait.485if (atomic_xchg(&parms->master_cpu, 1) == 1)486return chmem_secondary_loop(parms);487488// Wait for all but one CPU (this one) to call-in489while (atomic_read(&parms->cpu_counter) > 1)490barrier();491492change_memory_range(parms->start, parms->end, parms->step, parms->newpp);493494mb();495496// Signal the other CPUs that we're done497atomic_dec(&parms->cpu_counter);498499return 0;500}501502static bool hash__change_memory_range(unsigned long start, unsigned long end,503unsigned long newpp)504{505unsigned int step, shift;506507shift = mmu_psize_defs[mmu_linear_psize].shift;508step = 1 << shift;509510start = ALIGN_DOWN(start, step);511end = ALIGN(end, step); // aligns up512513if (start >= end)514return false;515516if (firmware_has_feature(FW_FEATURE_LPAR)) {517mutex_lock(&chmem_lock);518519chmem_parms.start = start;520chmem_parms.end = end;521chmem_parms.step = step;522chmem_parms.newpp = newpp;523atomic_set(&chmem_parms.master_cpu, 0);524525cpus_read_lock();526527atomic_set(&chmem_parms.cpu_counter, num_online_cpus());528529// Ensure state is consistent before we call the other CPUs530mb();531532stop_machine_cpuslocked(change_memory_range_fn, &chmem_parms,533cpu_online_mask);534535cpus_read_unlock();536mutex_unlock(&chmem_lock);537} else538change_memory_range(start, end, step, newpp);539540return true;541}542543void hash__mark_rodata_ro(void)544{545unsigned long start, end, pp;546547start = (unsigned long)_stext;548end = (unsigned long)__end_rodata;549550pp = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL_ROX), HPTE_USE_KERNEL_KEY);551552WARN_ON(!hash__change_memory_range(start, end, pp));553}554555void hash__mark_initmem_nx(void)556{557unsigned long start, end, pp;558559start = (unsigned long)__init_begin;560end = (unsigned long)__init_end;561562pp = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL), HPTE_USE_KERNEL_KEY);563564WARN_ON(!hash__change_memory_range(start, end, pp));565}566#endif567568569