12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
275a9b8a6SAneesh Kumar K.V #ifndef _ASM_POWERPC_BOOK3S_64_PGALLOC_H
375a9b8a6SAneesh Kumar K.V #define _ASM_POWERPC_BOOK3S_64_PGALLOC_H
4101ad5c6SAneesh Kumar K.V /*
5101ad5c6SAneesh Kumar K.V */
6101ad5c6SAneesh Kumar K.V
7101ad5c6SAneesh Kumar K.V #include <linux/slab.h>
8101ad5c6SAneesh Kumar K.V #include <linux/cpumask.h>
9a984506cSMichael Ellerman #include <linux/kmemleak.h>
10101ad5c6SAneesh Kumar K.V #include <linux/percpu.h>
11101ad5c6SAneesh Kumar K.V
12101ad5c6SAneesh Kumar K.V struct vmemmap_backing {
13101ad5c6SAneesh Kumar K.V struct vmemmap_backing *list;
14101ad5c6SAneesh Kumar K.V unsigned long phys;
15101ad5c6SAneesh Kumar K.V unsigned long virt_addr;
16101ad5c6SAneesh Kumar K.V };
17101ad5c6SAneesh Kumar K.V extern struct vmemmap_backing *vmemmap_list;
18101ad5c6SAneesh Kumar K.V
198a6c697bSAneesh Kumar K.V extern pmd_t *pmd_fragment_alloc(struct mm_struct *, unsigned long);
208a6c697bSAneesh Kumar K.V extern void pmd_fragment_free(unsigned long *);
21934828edSAneesh Kumar K.V extern void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int shift);
22934828edSAneesh Kumar K.V extern void __tlb_remove_table(void *_table);
23a95d133cSChristophe Leroy void pte_frag_destroy(void *pte_frag);
24934828edSAneesh Kumar K.V
radix__pgd_alloc(struct mm_struct * mm)25a2f41eb9SAneesh Kumar K.V static inline pgd_t *radix__pgd_alloc(struct mm_struct *mm)
26a2f41eb9SAneesh Kumar K.V {
27a2f41eb9SAneesh Kumar K.V #ifdef CONFIG_PPC_64K_PAGES
28de3b8761SBalbir Singh return (pgd_t *)__get_free_page(pgtable_gfp_flags(mm, PGALLOC_GFP));
29a2f41eb9SAneesh Kumar K.V #else
30a2f41eb9SAneesh Kumar K.V struct page *page;
31dcda9b04SMichal Hocko page = alloc_pages(pgtable_gfp_flags(mm, PGALLOC_GFP | __GFP_RETRY_MAYFAIL),
32de3b8761SBalbir Singh 4);
33a2f41eb9SAneesh Kumar K.V if (!page)
34a2f41eb9SAneesh Kumar K.V return NULL;
35a2f41eb9SAneesh Kumar K.V return (pgd_t *) page_address(page);
36a2f41eb9SAneesh Kumar K.V #endif
37a2f41eb9SAneesh Kumar K.V }
38a2f41eb9SAneesh Kumar K.V
radix__pgd_free(struct mm_struct * mm,pgd_t * pgd)39a2f41eb9SAneesh Kumar K.V static inline void radix__pgd_free(struct mm_struct *mm, pgd_t *pgd)
40a2f41eb9SAneesh Kumar K.V {
41a2f41eb9SAneesh Kumar K.V #ifdef CONFIG_PPC_64K_PAGES
42a2f41eb9SAneesh Kumar K.V free_page((unsigned long)pgd);
43a2f41eb9SAneesh Kumar K.V #else
44a2f41eb9SAneesh Kumar K.V free_pages((unsigned long)pgd, 4);
45a2f41eb9SAneesh Kumar K.V #endif
46a2f41eb9SAneesh Kumar K.V }
47a2f41eb9SAneesh Kumar K.V
pgd_alloc(struct mm_struct * mm)48101ad5c6SAneesh Kumar K.V static inline pgd_t *pgd_alloc(struct mm_struct *mm)
49101ad5c6SAneesh Kumar K.V {
50fc5c2f4aSAneesh Kumar K.V pgd_t *pgd;
51fc5c2f4aSAneesh Kumar K.V
52a2f41eb9SAneesh Kumar K.V if (radix_enabled())
53a2f41eb9SAneesh Kumar K.V return radix__pgd_alloc(mm);
54fc5c2f4aSAneesh Kumar K.V
55fc5c2f4aSAneesh Kumar K.V pgd = kmem_cache_alloc(PGT_CACHE(PGD_INDEX_SIZE),
56de3b8761SBalbir Singh pgtable_gfp_flags(mm, GFP_KERNEL));
57f3935626SRick Lindsley if (unlikely(!pgd))
58f3935626SRick Lindsley return pgd;
59f3935626SRick Lindsley
60872a100aSAneesh Kumar K.V /*
61a984506cSMichael Ellerman * Don't scan the PGD for pointers, it contains references to PUDs but
62a984506cSMichael Ellerman * those references are not full pointers and so can't be recognised by
63a984506cSMichael Ellerman * kmemleak.
64a984506cSMichael Ellerman */
65a984506cSMichael Ellerman kmemleak_no_scan(pgd);
66a984506cSMichael Ellerman
67a984506cSMichael Ellerman /*
68872a100aSAneesh Kumar K.V * With hugetlb, we don't clear the second half of the page table.
69872a100aSAneesh Kumar K.V * If we share the same slab cache with the pmd or pud level table,
70872a100aSAneesh Kumar K.V * we need to make sure we zero out the full table on alloc.
71872a100aSAneesh Kumar K.V * With 4K we don't store slot in the second half. Hence we don't
72872a100aSAneesh Kumar K.V * need to do this for 4k.
73872a100aSAneesh Kumar K.V */
74872a100aSAneesh Kumar K.V #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_PPC_64K_PAGES) && \
75738f9645SAneesh Kumar K.V (H_PGD_INDEX_SIZE == H_PUD_CACHE_INDEX)
76fc5c2f4aSAneesh Kumar K.V memset(pgd, 0, PGD_TABLE_SIZE);
77872a100aSAneesh Kumar K.V #endif
78fc5c2f4aSAneesh Kumar K.V return pgd;
79101ad5c6SAneesh Kumar K.V }
80101ad5c6SAneesh Kumar K.V
pgd_free(struct mm_struct * mm,pgd_t * pgd)81101ad5c6SAneesh Kumar K.V static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
82101ad5c6SAneesh Kumar K.V {
83a2f41eb9SAneesh Kumar K.V if (radix_enabled())
84a2f41eb9SAneesh Kumar K.V return radix__pgd_free(mm, pgd);
85101ad5c6SAneesh Kumar K.V kmem_cache_free(PGT_CACHE(PGD_INDEX_SIZE), pgd);
86101ad5c6SAneesh Kumar K.V }
87101ad5c6SAneesh Kumar K.V
p4d_populate(struct mm_struct * mm,p4d_t * pgd,pud_t * pud)882fb47060SMike Rapoport static inline void p4d_populate(struct mm_struct *mm, p4d_t *pgd, pud_t *pud)
8975a9b8a6SAneesh Kumar K.V {
902fb47060SMike Rapoport *pgd = __p4d(__pgtable_ptr_val(pud) | PGD_VAL_BITS);
9175a9b8a6SAneesh Kumar K.V }
92101ad5c6SAneesh Kumar K.V
pud_alloc_one(struct mm_struct * mm,unsigned long addr)93101ad5c6SAneesh Kumar K.V static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
94101ad5c6SAneesh Kumar K.V {
95a984506cSMichael Ellerman pud_t *pud;
96a984506cSMichael Ellerman
97a984506cSMichael Ellerman pud = kmem_cache_alloc(PGT_CACHE(PUD_CACHE_INDEX),
98de3b8761SBalbir Singh pgtable_gfp_flags(mm, GFP_KERNEL));
99a984506cSMichael Ellerman /*
100a984506cSMichael Ellerman * Tell kmemleak to ignore the PUD, that means don't scan it for
101a984506cSMichael Ellerman * pointers and don't consider it a leak. PUDs are typically only
102a984506cSMichael Ellerman * referred to by their PGD, but kmemleak is not able to recognise those
103a984506cSMichael Ellerman * as pointers, leading to false leak reports.
104a984506cSMichael Ellerman */
105a984506cSMichael Ellerman kmemleak_ignore(pud);
106a984506cSMichael Ellerman
107a984506cSMichael Ellerman return pud;
108101ad5c6SAneesh Kumar K.V }
109101ad5c6SAneesh Kumar K.V
__pud_free(pud_t * pud)110645d5ce2SAneesh Kumar K.V static inline void __pud_free(pud_t *pud)
111645d5ce2SAneesh Kumar K.V {
112645d5ce2SAneesh Kumar K.V struct page *page = virt_to_page(pud);
113645d5ce2SAneesh Kumar K.V
114645d5ce2SAneesh Kumar K.V /*
115645d5ce2SAneesh Kumar K.V * Early pud pages allocated via memblock allocator
116*a5edf981SNicholas Miehlbradt * can't be directly freed to slab. KFENCE pages have
117*a5edf981SNicholas Miehlbradt * both reserved and slab flags set so need to be freed
118*a5edf981SNicholas Miehlbradt * kmem_cache_free.
119645d5ce2SAneesh Kumar K.V */
120*a5edf981SNicholas Miehlbradt if (PageReserved(page) && !PageSlab(page))
121645d5ce2SAneesh Kumar K.V free_reserved_page(page);
122645d5ce2SAneesh Kumar K.V else
123645d5ce2SAneesh Kumar K.V kmem_cache_free(PGT_CACHE(PUD_CACHE_INDEX), pud);
124645d5ce2SAneesh Kumar K.V }
125645d5ce2SAneesh Kumar K.V
pud_free(struct mm_struct * mm,pud_t * pud)126101ad5c6SAneesh Kumar K.V static inline void pud_free(struct mm_struct *mm, pud_t *pud)
127101ad5c6SAneesh Kumar K.V {
128645d5ce2SAneesh Kumar K.V return __pud_free(pud);
129101ad5c6SAneesh Kumar K.V }
130101ad5c6SAneesh Kumar K.V
pud_populate(struct mm_struct * mm,pud_t * pud,pmd_t * pmd)131101ad5c6SAneesh Kumar K.V static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
132101ad5c6SAneesh Kumar K.V {
133c746ca00SAneesh Kumar K.V *pud = __pud(__pgtable_ptr_val(pmd) | PUD_VAL_BITS);
134101ad5c6SAneesh Kumar K.V }
135101ad5c6SAneesh Kumar K.V
__pud_free_tlb(struct mmu_gather * tlb,pud_t * pud,unsigned long address)136934828edSAneesh Kumar K.V static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
137934828edSAneesh Kumar K.V unsigned long address)
138934828edSAneesh Kumar K.V {
1390c4d2680SAneesh Kumar K.V pgtable_free_tlb(tlb, pud, PUD_INDEX);
140934828edSAneesh Kumar K.V }
141934828edSAneesh Kumar K.V
pmd_alloc_one(struct mm_struct * mm,unsigned long addr)142934828edSAneesh Kumar K.V static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
143934828edSAneesh Kumar K.V {
144738f9645SAneesh Kumar K.V return pmd_fragment_alloc(mm, addr);
145934828edSAneesh Kumar K.V }
146934828edSAneesh Kumar K.V
pmd_free(struct mm_struct * mm,pmd_t * pmd)147934828edSAneesh Kumar K.V static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
148934828edSAneesh Kumar K.V {
149738f9645SAneesh Kumar K.V pmd_fragment_free((unsigned long *)pmd);
150934828edSAneesh Kumar K.V }
151934828edSAneesh Kumar K.V
__pmd_free_tlb(struct mmu_gather * tlb,pmd_t * pmd,unsigned long address)152934828edSAneesh Kumar K.V static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
153934828edSAneesh Kumar K.V unsigned long address)
154934828edSAneesh Kumar K.V {
1550c4d2680SAneesh Kumar K.V return pgtable_free_tlb(tlb, pmd, PMD_INDEX);
156934828edSAneesh Kumar K.V }
157934828edSAneesh Kumar K.V
pmd_populate_kernel(struct mm_struct * mm,pmd_t * pmd,pte_t * pte)158101ad5c6SAneesh Kumar K.V static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
159101ad5c6SAneesh Kumar K.V pte_t *pte)
160101ad5c6SAneesh Kumar K.V {
161c746ca00SAneesh Kumar K.V *pmd = __pmd(__pgtable_ptr_val(pte) | PMD_VAL_BITS);
162101ad5c6SAneesh Kumar K.V }
163934828edSAneesh Kumar K.V
pmd_populate(struct mm_struct * mm,pmd_t * pmd,pgtable_t pte_page)164101ad5c6SAneesh Kumar K.V static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
165101ad5c6SAneesh Kumar K.V pgtable_t pte_page)
166101ad5c6SAneesh Kumar K.V {
167c746ca00SAneesh Kumar K.V *pmd = __pmd(__pgtable_ptr_val(pte_page) | PMD_VAL_BITS);
168101ad5c6SAneesh Kumar K.V }
169101ad5c6SAneesh Kumar K.V
__pte_free_tlb(struct mmu_gather * tlb,pgtable_t table,unsigned long address)170101ad5c6SAneesh Kumar K.V static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t table,
171101ad5c6SAneesh Kumar K.V unsigned long address)
172101ad5c6SAneesh Kumar K.V {
1730c4d2680SAneesh Kumar K.V pgtable_free_tlb(tlb, table, PTE_INDEX);
174101ad5c6SAneesh Kumar K.V }
175101ad5c6SAneesh Kumar K.V
176a2dc009aSAneesh Kumar K.V extern atomic_long_t direct_pages_count[MMU_PAGE_COUNT];
update_page_count(int psize,long count)177a2dc009aSAneesh Kumar K.V static inline void update_page_count(int psize, long count)
178a2dc009aSAneesh Kumar K.V {
179a2dc009aSAneesh Kumar K.V if (IS_ENABLED(CONFIG_PROC_FS))
180a2dc009aSAneesh Kumar K.V atomic_long_add(count, &direct_pages_count[psize]);
181a2dc009aSAneesh Kumar K.V }
182a2dc009aSAneesh Kumar K.V
18375a9b8a6SAneesh Kumar K.V #endif /* _ASM_POWERPC_BOOK3S_64_PGALLOC_H */
184