1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2005, Paul Mackerras, IBM Corporation.
4 * Copyright 2009, Benjamin Herrenschmidt, IBM Corporation.
5 * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
8 #include <linux/sched.h>
9 #include <linux/mm_types.h>
12 #include <asm/pgalloc.h>
13 #include <asm/pgtable.h>
14 #include <asm/sections.h>
18 #include <mm/mmu_decl.h>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/thp.h>
23 #if H_PGTABLE_RANGE > (USER_VSID_RANGE * (TASK_SIZE_USER64 / TASK_CONTEXT_SIZE))
24 #warning Limited user VSID range means pagetable space is wasted
27 #ifdef CONFIG_SPARSEMEM_VMEMMAP
29 * vmemmap is the starting address of the virtual address space where
30 * struct pages are allocated for all possible PFNs present on the system
31 * including holes and bad memory (hence sparse). These virtual struct
32 * pages are stored in sequence in this virtual address space irrespective
33 * of the fact whether the corresponding PFN is valid or not. This achieves
34 * constant relationship between address of struct page and its PFN.
36 * During boot or memory hotplug operation when a new memory section is
37 * added, physical memory allocation (including hash table bolting) will
38 * be performed for the set of struct pages which are part of the memory
39 * section. This saves memory by not allocating struct pages for PFNs
40 * which are not valid.
42 * ----------------------------------------------
43 * | PHYSICAL ALLOCATION OF VIRTUAL STRUCT PAGES|
44 * ----------------------------------------------
46 * f000000000000000 c000000000000000
47 * vmemmap +--------------+ +--------------+
48 * + | page struct | +--------------> | page struct |
49 * | +--------------+ +--------------+
50 * | | page struct | +--------------> | page struct |
51 * | +--------------+ | +--------------+
52 * | | page struct | + +------> | page struct |
53 * | +--------------+ | +--------------+
54 * | | page struct | | +--> | page struct |
55 * | +--------------+ | | +--------------+
56 * | | page struct | | |
57 * | +--------------+ | |
58 * | | page struct | | |
59 * | +--------------+ | |
60 * | | page struct | | |
61 * | +--------------+ | |
62 * | | page struct | | |
63 * | +--------------+ | |
64 * | | page struct | +-------+ |
65 * | +--------------+ |
66 * | | page struct | +-----------+
68 * | | page struct | No mapping
70 * | | page struct | No mapping
73 * -----------------------------------------
74 * | RELATION BETWEEN STRUCT PAGES AND PFNS|
75 * -----------------------------------------
77 * vmemmap +--------------+ +---------------+
78 * + | page struct | +-------------> | PFN |
79 * | +--------------+ +---------------+
80 * | | page struct | +-------------> | PFN |
81 * | +--------------+ +---------------+
82 * | | page struct | +-------------> | PFN |
83 * | +--------------+ +---------------+
84 * | | page struct | +-------------> | PFN |
85 * | +--------------+ +---------------+
91 * | +--------------+ +---------------+
92 * | | page struct | +-------------> | PFN |
93 * | +--------------+ +---------------+
97 * | +--------------+ +---------------+
98 * | | page struct | +-------------> | PFN |
99 * | +--------------+ +---------------+
100 * | | page struct | +-------------> | PFN |
101 * v +--------------+ +---------------+
104 * On hash-based CPUs, the vmemmap is bolted in the hash table.
107 int __meminit
hash__vmemmap_create_mapping(unsigned long start
,
108 unsigned long page_size
,
113 if ((start
+ page_size
) >= H_VMEMMAP_END
) {
114 pr_warn("Outside the supported range\n");
118 rc
= htab_bolt_mapping(start
, start
+ page_size
, phys
,
119 pgprot_val(PAGE_KERNEL
),
120 mmu_vmemmap_psize
, mmu_kernel_ssize
);
122 int rc2
= htab_remove_mapping(start
, start
+ page_size
,
125 BUG_ON(rc2
&& (rc2
!= -ENOENT
));
130 #ifdef CONFIG_MEMORY_HOTPLUG
131 void hash__vmemmap_remove_mapping(unsigned long start
,
132 unsigned long page_size
)
134 int rc
= htab_remove_mapping(start
, start
+ page_size
,
137 BUG_ON((rc
< 0) && (rc
!= -ENOENT
));
138 WARN_ON(rc
== -ENOENT
);
141 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
144 * map_kernel_page currently only called by __ioremap
145 * map_kernel_page adds an entry to the ioremap page table
146 * and adds an entry to the HPT, possibly bolting it
148 int hash__map_kernel_page(unsigned long ea
, unsigned long pa
, pgprot_t prot
)
156 BUILD_BUG_ON(TASK_SIZE_USER64
> H_PGTABLE_RANGE
);
157 if (slab_is_available()) {
158 pgdp
= pgd_offset_k(ea
);
159 p4dp
= p4d_offset(pgdp
, ea
);
160 pudp
= pud_alloc(&init_mm
, p4dp
, ea
);
163 pmdp
= pmd_alloc(&init_mm
, pudp
, ea
);
166 ptep
= pte_alloc_kernel(pmdp
, ea
);
169 set_pte_at(&init_mm
, ea
, ptep
, pfn_pte(pa
>> PAGE_SHIFT
, prot
));
172 * If the mm subsystem is not fully up, we cannot create a
173 * linux page table entry for this mapping. Simply bolt an
174 * entry in the hardware page table.
177 if (htab_bolt_mapping(ea
, ea
+ PAGE_SIZE
, pa
, pgprot_val(prot
),
178 mmu_io_psize
, mmu_kernel_ssize
)) {
179 printk(KERN_ERR
"Failed to do bolted mapping IO "
180 "memory at %016lx !\n", pa
);
189 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
191 unsigned long hash__pmd_hugepage_update(struct mm_struct
*mm
, unsigned long addr
,
192 pmd_t
*pmdp
, unsigned long clr
,
198 #ifdef CONFIG_DEBUG_VM
199 WARN_ON(!hash__pmd_trans_huge(*pmdp
) && !pmd_devmap(*pmdp
));
200 assert_spin_locked(pmd_lockptr(mm
, pmdp
));
203 __asm__
__volatile__(
211 : "=&r" (old_be
), "=&r" (tmp
), "=m" (*pmdp
)
212 : "r" (pmdp
), "r" (cpu_to_be64(clr
)), "m" (*pmdp
),
213 "r" (cpu_to_be64(H_PAGE_BUSY
)), "r" (cpu_to_be64(set
))
216 old
= be64_to_cpu(old_be
);
218 trace_hugepage_update(addr
, old
, clr
, set
);
219 if (old
& H_PAGE_HASHPTE
)
220 hpte_do_hugepage_flush(mm
, addr
, pmdp
, old
);
224 pmd_t
hash__pmdp_collapse_flush(struct vm_area_struct
*vma
, unsigned long address
,
229 VM_BUG_ON(address
& ~HPAGE_PMD_MASK
);
230 VM_BUG_ON(pmd_trans_huge(*pmdp
));
231 VM_BUG_ON(pmd_devmap(*pmdp
));
236 * Wait for all pending hash_page to finish. This is needed
237 * in case of subpage collapse. When we collapse normal pages
238 * to hugepage, we first clear the pmd, then invalidate all
239 * the PTE entries. The assumption here is that any low level
240 * page fault will see a none pmd and take the slow path that
241 * will wait on mmap_sem. But we could very well be in a
242 * hash_page with local ptep pointer value. Such a hash page
243 * can result in adding new HPTE entries for normal subpages.
244 * That means we could be modifying the page content as we
245 * copy them to a huge page. So wait for parallel hash_page
246 * to finish before invalidating HPTE entries. We can do this
247 * by sending an IPI to all the cpus and executing a dummy
250 serialize_against_pte_lookup(vma
->vm_mm
);
252 * Now invalidate the hpte entries in the range
253 * covered by pmd. This make sure we take a
254 * fault and will find the pmd as none, which will
255 * result in a major fault which takes mmap_sem and
256 * hence wait for collapse to complete. Without this
257 * the __collapse_huge_page_copy can result in copying
260 flush_tlb_pmd_range(vma
->vm_mm
, &pmd
, address
);
265 * We want to put the pgtable in pmd and use pgtable for tracking
266 * the base page size hptes
268 void hash__pgtable_trans_huge_deposit(struct mm_struct
*mm
, pmd_t
*pmdp
,
271 pgtable_t
*pgtable_slot
;
273 assert_spin_locked(pmd_lockptr(mm
, pmdp
));
275 * we store the pgtable in the second half of PMD
277 pgtable_slot
= (pgtable_t
*)pmdp
+ PTRS_PER_PMD
;
278 *pgtable_slot
= pgtable
;
280 * expose the deposited pgtable to other cpus.
281 * before we set the hugepage PTE at pmd level
282 * hash fault code looks at the deposted pgtable
283 * to store hash index values.
288 pgtable_t
hash__pgtable_trans_huge_withdraw(struct mm_struct
*mm
, pmd_t
*pmdp
)
291 pgtable_t
*pgtable_slot
;
293 assert_spin_locked(pmd_lockptr(mm
, pmdp
));
295 pgtable_slot
= (pgtable_t
*)pmdp
+ PTRS_PER_PMD
;
296 pgtable
= *pgtable_slot
;
298 * Once we withdraw, mark the entry NULL.
300 *pgtable_slot
= NULL
;
302 * We store HPTE information in the deposited PTE fragment.
303 * zero out the content on withdraw.
305 memset(pgtable
, 0, PTE_FRAG_SIZE
);
310 * A linux hugepage PMD was changed and the corresponding hash table entries
311 * neesd to be flushed.
313 void hpte_do_hugepage_flush(struct mm_struct
*mm
, unsigned long addr
,
314 pmd_t
*pmdp
, unsigned long old_pmd
)
319 unsigned long flags
= 0;
321 /* get the base page size,vsid and segment size */
322 #ifdef CONFIG_DEBUG_VM
323 psize
= get_slice_psize(mm
, addr
);
324 BUG_ON(psize
== MMU_PAGE_16M
);
326 if (old_pmd
& H_PAGE_COMBO
)
329 psize
= MMU_PAGE_64K
;
331 if (!is_kernel_addr(addr
)) {
332 ssize
= user_segment_size(addr
);
333 vsid
= get_user_vsid(&mm
->context
, addr
, ssize
);
336 vsid
= get_kernel_vsid(addr
, mmu_kernel_ssize
);
337 ssize
= mmu_kernel_ssize
;
340 if (mm_is_thread_local(mm
))
341 flags
|= HPTE_LOCAL_UPDATE
;
343 return flush_hash_hugepage(vsid
, addr
, pmdp
, psize
, ssize
, flags
);
346 pmd_t
hash__pmdp_huge_get_and_clear(struct mm_struct
*mm
,
347 unsigned long addr
, pmd_t
*pmdp
)
352 pgtable_t
*pgtable_slot
;
354 old
= pmd_hugepage_update(mm
, addr
, pmdp
, ~0UL, 0);
355 old_pmd
= __pmd(old
);
357 * We have pmd == none and we are holding page_table_lock.
358 * So we can safely go and clear the pgtable hash
361 pgtable_slot
= (pgtable_t
*)pmdp
+ PTRS_PER_PMD
;
362 pgtable
= *pgtable_slot
;
364 * Let's zero out old valid and hash index details
365 * hash fault look at them.
367 memset(pgtable
, 0, PTE_FRAG_SIZE
);
369 * Serialize against find_current_mm_pte variants which does lock-less
370 * lookup in page tables with local interrupts disabled. For huge pages
371 * it casts pmd_t to pte_t. Since format of pte_t is different from
372 * pmd_t we want to prevent transit from pmd pointing to page table
373 * to pmd pointing to huge page (and back) while interrupts are disabled.
374 * We clear pmd to possibly replace it with page table pointer in
375 * different code paths. So make sure we wait for the parallel
376 * find_curren_mm_pte to finish.
378 serialize_against_pte_lookup(mm
);
382 int hash__has_transparent_hugepage(void)
385 if (!mmu_has_feature(MMU_FTR_16M_PAGE
))
388 * We support THP only if PMD_SIZE is 16MB.
390 if (mmu_psize_defs
[MMU_PAGE_16M
].shift
!= PMD_SHIFT
)
393 * We need to make sure that we support 16MB hugepage in a segement
394 * with base page size 64K or 4K. We only enable THP with a PAGE_SIZE
398 * If we have 64K HPTE, we will be using that by default
400 if (mmu_psize_defs
[MMU_PAGE_64K
].shift
&&
401 (mmu_psize_defs
[MMU_PAGE_64K
].penc
[MMU_PAGE_16M
] == -1))
404 * Ok we only have 4K HPTE
406 if (mmu_psize_defs
[MMU_PAGE_4K
].penc
[MMU_PAGE_16M
] == -1)
411 EXPORT_SYMBOL_GPL(hash__has_transparent_hugepage
);
413 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
415 #ifdef CONFIG_STRICT_KERNEL_RWX
416 static bool hash__change_memory_range(unsigned long start
, unsigned long end
,
420 unsigned int step
, shift
;
422 shift
= mmu_psize_defs
[mmu_linear_psize
].shift
;
425 start
= ALIGN_DOWN(start
, step
);
426 end
= ALIGN(end
, step
); // aligns up
431 pr_debug("Changing page protection on range 0x%lx-0x%lx, to 0x%lx, step 0x%x\n",
432 start
, end
, newpp
, step
);
434 for (idx
= start
; idx
< end
; idx
+= step
)
435 /* Not sure if we can do much with the return value */
436 mmu_hash_ops
.hpte_updateboltedpp(newpp
, idx
, mmu_linear_psize
,
442 void hash__mark_rodata_ro(void)
444 unsigned long start
, end
;
446 start
= (unsigned long)_stext
;
447 end
= (unsigned long)__init_begin
;
449 WARN_ON(!hash__change_memory_range(start
, end
, PP_RXXX
));
452 void hash__mark_initmem_nx(void)
454 unsigned long start
, end
, pp
;
456 start
= (unsigned long)__init_begin
;
457 end
= (unsigned long)__init_end
;
459 pp
= htab_convert_pte_flags(pgprot_val(PAGE_KERNEL
));
461 WARN_ON(!hash__change_memory_range(start
, end
, pp
));