Path: blob/master/arch/powerpc/mm/book3s64/hash_hugepage.c
26481 views
/*1* Copyright IBM Corporation, 20132* Author Aneesh Kumar K.V <[email protected]>3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of version 2.1 of the GNU Lesser General Public License6* as published by the Free Software Foundation.7*8* This program is distributed in the hope that it would be useful, but9* WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.11*12*/1314/*15* PPC64 THP Support for hash based MMUs16*/17#include <linux/mm.h>18#include <asm/machdep.h>1920int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid,21pmd_t *pmdp, unsigned long trap, unsigned long flags,22int ssize, unsigned int psize)23{24unsigned int index, valid;25unsigned char *hpte_slot_array;26unsigned long rflags, pa, hidx;27unsigned long old_pmd, new_pmd;28int ret, lpsize = MMU_PAGE_16M;29unsigned long vpn, hash, shift, slot;3031/*32* atomically mark the linux large page PMD busy and dirty33*/34do {35pmd_t pmd = READ_ONCE(*pmdp);3637old_pmd = pmd_val(pmd);38/* If PMD busy, retry the access */39if (unlikely(old_pmd & H_PAGE_BUSY))40return 0;41/* If PMD permissions don't match, take page fault */42if (unlikely(!check_pte_access(access, old_pmd)))43return 1;44/*45* Try to lock the PTE, add ACCESSED and DIRTY if it was46* a write access47*/48new_pmd = old_pmd | H_PAGE_BUSY | _PAGE_ACCESSED;49if (access & _PAGE_WRITE)50new_pmd |= _PAGE_DIRTY;51} while (!pmd_xchg(pmdp, __pmd(old_pmd), __pmd(new_pmd)));5253/*54* Make sure this is thp or devmap entry55*/56if (!(old_pmd & H_PAGE_THP_HUGE))57return 0;5859rflags = htab_convert_pte_flags(new_pmd, flags);6061/*62* THPs are only supported on platforms that can do mixed page size63* segments (MPSS) and all such platforms have coherent icache. Hence we64* don't need to do lazy icache flush (hash_page_do_lazy_icache()) on65* noexecute fault.66*/6768/*69* Find the slot index details for this ea, using base page size.70*/71shift = mmu_psize_defs[psize].shift;72index = (ea & ~HPAGE_PMD_MASK) >> shift;73BUG_ON(index >= PTE_FRAG_SIZE);7475vpn = hpt_vpn(ea, vsid, ssize);76hpte_slot_array = get_hpte_slot_array(pmdp);77if (psize == MMU_PAGE_4K) {78/*79* invalidate the old hpte entry if we have that mapped via 64K80* base page size. This is because demote_segment won't flush81* hash page table entries.82*/83if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) {84flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,85ssize, flags);86/*87* With THP, we also clear the slot information with88* respect to all the 64K hash pte mapping the 16MB89* page. They are all invalid now. This make sure we90* don't find the slot valid when we fault with 4k91* base page size.92*93*/94memset(hpte_slot_array, 0, PTE_FRAG_SIZE);95}96}9798valid = hpte_valid(hpte_slot_array, index);99if (valid) {100/* update the hpte bits */101hash = hpt_hash(vpn, shift, ssize);102hidx = hpte_hash_index(hpte_slot_array, index);103if (hidx & _PTEIDX_SECONDARY)104hash = ~hash;105slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;106slot += hidx & _PTEIDX_GROUP_IX;107108ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,109psize, lpsize, ssize, flags);110/*111* We failed to update, try to insert a new entry.112*/113if (ret == -1) {114/*115* large pte is marked busy, so we can be sure116* nobody is looking at hpte_slot_array. hence we can117* safely update this here.118*/119valid = 0;120hpte_slot_array[index] = 0;121}122}123124if (!valid) {125unsigned long hpte_group;126127hash = hpt_hash(vpn, shift, ssize);128/* insert new entry */129pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;130new_pmd |= H_PAGE_HASHPTE;131132repeat:133hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;134135/* Insert into the hash table, primary slot */136slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,137psize, lpsize, ssize);138/*139* Primary is full, try the secondary140*/141if (unlikely(slot == -1)) {142hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;143slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,144rflags,145HPTE_V_SECONDARY,146psize, lpsize, ssize);147if (slot == -1) {148if (mftb() & 0x1)149hpte_group = (hash & htab_hash_mask) *150HPTES_PER_GROUP;151152mmu_hash_ops.hpte_remove(hpte_group);153goto repeat;154}155}156/*157* Hypervisor failure. Restore old pmd and return -1158* similar to __hash_page_*159*/160if (unlikely(slot == -2)) {161*pmdp = __pmd(old_pmd);162hash_failure_debug(ea, access, vsid, trap, ssize,163psize, lpsize, old_pmd);164return -1;165}166/*167* large pte is marked busy, so we can be sure168* nobody is looking at hpte_slot_array. hence we can169* safely update this here.170*/171mark_hpte_slot_valid(hpte_slot_array, index, slot);172}173/*174* Mark the pte with H_PAGE_COMBO, if we are trying to hash it with175* base page size 4k.176*/177if (psize == MMU_PAGE_4K)178new_pmd |= H_PAGE_COMBO;179/*180* The hpte valid is stored in the pgtable whose address is in the181* second half of the PMD. Order this against clearing of the busy bit in182* huge pmd.183*/184smp_wmb();185*pmdp = __pmd(new_pmd & ~H_PAGE_BUSY);186return 0;187}188189190