Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 #ifndef _ASM_POWERPC_BOOK3S_64_PGALLOC_H
0003 #define _ASM_POWERPC_BOOK3S_64_PGALLOC_H
0004 /*
0005  */
0006 
0007 #include <linux/slab.h>
0008 #include <linux/cpumask.h>
0009 #include <linux/kmemleak.h>
0010 #include <linux/percpu.h>
0011 
0012 struct vmemmap_backing {
0013     struct vmemmap_backing *list;
0014     unsigned long phys;
0015     unsigned long virt_addr;
0016 };
0017 extern struct vmemmap_backing *vmemmap_list;
0018 
0019 extern pmd_t *pmd_fragment_alloc(struct mm_struct *, unsigned long);
0020 extern void pmd_fragment_free(unsigned long *);
0021 extern void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int shift);
0022 extern void __tlb_remove_table(void *_table);
0023 void pte_frag_destroy(void *pte_frag);
0024 
0025 static inline pgd_t *radix__pgd_alloc(struct mm_struct *mm)
0026 {
0027 #ifdef CONFIG_PPC_64K_PAGES
0028     return (pgd_t *)__get_free_page(pgtable_gfp_flags(mm, PGALLOC_GFP));
0029 #else
0030     struct page *page;
0031     page = alloc_pages(pgtable_gfp_flags(mm, PGALLOC_GFP | __GFP_RETRY_MAYFAIL),
0032                 4);
0033     if (!page)
0034         return NULL;
0035     return (pgd_t *) page_address(page);
0036 #endif
0037 }
0038 
0039 static inline void radix__pgd_free(struct mm_struct *mm, pgd_t *pgd)
0040 {
0041 #ifdef CONFIG_PPC_64K_PAGES
0042     free_page((unsigned long)pgd);
0043 #else
0044     free_pages((unsigned long)pgd, 4);
0045 #endif
0046 }
0047 
0048 static inline pgd_t *pgd_alloc(struct mm_struct *mm)
0049 {
0050     pgd_t *pgd;
0051 
0052     if (radix_enabled())
0053         return radix__pgd_alloc(mm);
0054 
0055     pgd = kmem_cache_alloc(PGT_CACHE(PGD_INDEX_SIZE),
0056                    pgtable_gfp_flags(mm, GFP_KERNEL));
0057     if (unlikely(!pgd))
0058         return pgd;
0059 
0060     /*
0061      * Don't scan the PGD for pointers, it contains references to PUDs but
0062      * those references are not full pointers and so can't be recognised by
0063      * kmemleak.
0064      */
0065     kmemleak_no_scan(pgd);
0066 
0067     /*
0068      * With hugetlb, we don't clear the second half of the page table.
0069      * If we share the same slab cache with the pmd or pud level table,
0070      * we need to make sure we zero out the full table on alloc.
0071      * With 4K we don't store slot in the second half. Hence we don't
0072      * need to do this for 4k.
0073      */
0074 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_PPC_64K_PAGES) && \
0075     (H_PGD_INDEX_SIZE == H_PUD_CACHE_INDEX)
0076     memset(pgd, 0, PGD_TABLE_SIZE);
0077 #endif
0078     return pgd;
0079 }
0080 
0081 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
0082 {
0083     if (radix_enabled())
0084         return radix__pgd_free(mm, pgd);
0085     kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
0086 }
0087 
0088 static inline void p4d_populate(struct mm_struct *mm, p4d_t *pgd, pud_t *pud)
0089 {
0090     *pgd =  __p4d(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
0091 }
0092 
0093 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
0094 {
0095     pud_t *pud;
0096 
0097     pud = kmem_cache_alloc(PGT_CACHE(PUD_CACHE_INDEX),
0098                    pgtable_gfp_flags(mm, GFP_KERNEL));
0099     /*
0100      * Tell kmemleak to ignore the PUD, that means don't scan it for
0101      * pointers and don't consider it a leak. PUDs are typically only
0102      * referred to by their PGD, but kmemleak is not able to recognise those
0103      * as pointers, leading to false leak reports.
0104      */
0105     kmemleak_ignore(pud);
0106 
0107     return pud;
0108 }
0109 
0110 static inline void __pud_free(pud_t *pud)
0111 {
0112     struct page *page = virt_to_page(pud);
0113 
0114     /*
0115      * Early pud pages allocated via memblock allocator
0116      * can't be directly freed to slab
0117      */
0118     if (PageReserved(page))
0119         free_reserved_page(page);
0120     else
0121         kmem_cache_free(PGT_CACHE(PUD_CACHE_INDEX), pud);
0122 }
0123 
0124 static inline void pud_free(struct mm_struct *mm, pud_t *pud)
0125 {
0126     return __pud_free(pud);
0127 }
0128 
0129 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
0130 {
0131     *pud = __pud(__pgtable_ptr_val(pmd) | PUD_VAL_BITS);
0132 }
0133 
0134 static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
0135                   unsigned long address)
0136 {
0137     pgtable_free_tlb(tlb, pud, PUD_INDEX);
0138 }
0139 
0140 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
0141 {
0142     return pmd_fragment_alloc(mm, addr);
0143 }
0144 
0145 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
0146 {
0147     pmd_fragment_free((unsigned long *)pmd);
0148 }
0149 
0150 static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
0151                   unsigned long address)
0152 {
0153     return pgtable_free_tlb(tlb, pmd, PMD_INDEX);
0154 }
0155 
0156 static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
0157                        pte_t *pte)
0158 {
0159     *pmd = __pmd(__pgtable_ptr_val(pte) | PMD_VAL_BITS);
0160 }
0161 
0162 static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
0163                 pgtable_t pte_page)
0164 {
0165     *pmd = __pmd(__pgtable_ptr_val(pte_page) | PMD_VAL_BITS);
0166 }
0167 
0168 static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t table,
0169                   unsigned long address)
0170 {
0171     pgtable_free_tlb(tlb, table, PTE_INDEX);
0172 }
0173 
0174 extern atomic_long_t direct_pages_count[MMU_PAGE_COUNT];
0175 static inline void update_page_count(int psize, long count)
0176 {
0177     if (IS_ENABLED(CONFIG_PROC_FS))
0178         atomic_long_add(count, &direct_pages_count[psize]);
0179 }
0180 
0181 #endif /* _ASM_POWERPC_BOOK3S_64_PGALLOC_H */