hash_tlb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file contains the routines for flushing entries from the
  4. * TLB and MMU hash table.
  5. *
  6. * Derived from arch/ppc64/mm/init.c:
  7. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  8. *
  9. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  10. * and Cort Dougan (PReP) ([email protected])
  11. * Copyright (C) 1996 Paul Mackerras
  12. *
  13. * Derived from "arch/i386/mm/init.c"
  14. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  15. *
  16. * Dave Engebretsen <[email protected]>
  17. * Rework for PPC64 port.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/percpu.h>
  22. #include <linux/hardirq.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/tlb.h>
  25. #include <asm/bug.h>
  26. #include <asm/pte-walk.h>
  27. #include <trace/events/thp.h>
  28. DEFINE_PER_CPU(struct ppc64_tlb_batch, ppc64_tlb_batch);
  29. /*
  30. * A linux PTE was changed and the corresponding hash table entry
  31. * neesd to be flushed. This function will either perform the flush
  32. * immediately or will batch it up if the current CPU has an active
  33. * batch on it.
  34. */
  35. void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
  36. pte_t *ptep, unsigned long pte, int huge)
  37. {
  38. unsigned long vpn;
  39. struct ppc64_tlb_batch *batch = &get_cpu_var(ppc64_tlb_batch);
  40. unsigned long vsid;
  41. unsigned int psize;
  42. int ssize;
  43. real_pte_t rpte;
  44. int i, offset;
  45. i = batch->index;
  46. /*
  47. * Get page size (maybe move back to caller).
  48. *
  49. * NOTE: when using special 64K mappings in 4K environment like
  50. * for SPEs, we obtain the page size from the slice, which thus
  51. * must still exist (and thus the VMA not reused) at the time
  52. * of this call
  53. */
  54. if (huge) {
  55. #ifdef CONFIG_HUGETLB_PAGE
  56. psize = get_slice_psize(mm, addr);
  57. /* Mask the address for the correct page size */
  58. addr &= ~((1UL << mmu_psize_defs[psize].shift) - 1);
  59. if (unlikely(psize == MMU_PAGE_16G))
  60. offset = PTRS_PER_PUD;
  61. else
  62. offset = PTRS_PER_PMD;
  63. #else
  64. BUG();
  65. psize = pte_pagesize_index(mm, addr, pte); /* shutup gcc */
  66. #endif
  67. } else {
  68. psize = pte_pagesize_index(mm, addr, pte);
  69. /*
  70. * Mask the address for the standard page size. If we
  71. * have a 64k page kernel, but the hardware does not
  72. * support 64k pages, this might be different from the
  73. * hardware page size encoded in the slice table.
  74. */
  75. addr &= PAGE_MASK;
  76. offset = PTRS_PER_PTE;
  77. }
  78. /* Build full vaddr */
  79. if (!is_kernel_addr(addr)) {
  80. ssize = user_segment_size(addr);
  81. vsid = get_user_vsid(&mm->context, addr, ssize);
  82. } else {
  83. vsid = get_kernel_vsid(addr, mmu_kernel_ssize);
  84. ssize = mmu_kernel_ssize;
  85. }
  86. WARN_ON(vsid == 0);
  87. vpn = hpt_vpn(addr, vsid, ssize);
  88. rpte = __real_pte(__pte(pte), ptep, offset);
  89. /*
  90. * Check if we have an active batch on this CPU. If not, just
  91. * flush now and return.
  92. */
  93. if (!batch->active) {
  94. flush_hash_page(vpn, rpte, psize, ssize, mm_is_thread_local(mm));
  95. put_cpu_var(ppc64_tlb_batch);
  96. return;
  97. }
  98. /*
  99. * This can happen when we are in the middle of a TLB batch and
  100. * we encounter memory pressure (eg copy_page_range when it tries
  101. * to allocate a new pte). If we have to reclaim memory and end
  102. * up scanning and resetting referenced bits then our batch context
  103. * will change mid stream.
  104. *
  105. * We also need to ensure only one page size is present in a given
  106. * batch
  107. */
  108. if (i != 0 && (mm != batch->mm || batch->psize != psize ||
  109. batch->ssize != ssize)) {
  110. __flush_tlb_pending(batch);
  111. i = 0;
  112. }
  113. if (i == 0) {
  114. batch->mm = mm;
  115. batch->psize = psize;
  116. batch->ssize = ssize;
  117. }
  118. batch->pte[i] = rpte;
  119. batch->vpn[i] = vpn;
  120. batch->index = ++i;
  121. if (i >= PPC64_TLB_BATCH_NR)
  122. __flush_tlb_pending(batch);
  123. put_cpu_var(ppc64_tlb_batch);
  124. }
  125. /*
  126. * This function is called when terminating an mmu batch or when a batch
  127. * is full. It will perform the flush of all the entries currently stored
  128. * in a batch.
  129. *
  130. * Must be called from within some kind of spinlock/non-preempt region...
  131. */
  132. void __flush_tlb_pending(struct ppc64_tlb_batch *batch)
  133. {
  134. int i, local;
  135. i = batch->index;
  136. local = mm_is_thread_local(batch->mm);
  137. if (i == 1)
  138. flush_hash_page(batch->vpn[0], batch->pte[0],
  139. batch->psize, batch->ssize, local);
  140. else
  141. flush_hash_range(i, local);
  142. batch->index = 0;
  143. }
  144. void hash__tlb_flush(struct mmu_gather *tlb)
  145. {
  146. struct ppc64_tlb_batch *tlbbatch = &get_cpu_var(ppc64_tlb_batch);
  147. /*
  148. * If there's a TLB batch pending, then we must flush it because the
  149. * pages are going to be freed and we really don't want to have a CPU
  150. * access a freed page because it has a stale TLB
  151. */
  152. if (tlbbatch->index)
  153. __flush_tlb_pending(tlbbatch);
  154. put_cpu_var(ppc64_tlb_batch);
  155. }
  156. /**
  157. * __flush_hash_table_range - Flush all HPTEs for a given address range
  158. * from the hash table (and the TLB). But keeps
  159. * the linux PTEs intact.
  160. *
  161. * @start : starting address
  162. * @end : ending address (not included in the flush)
  163. *
  164. * This function is mostly to be used by some IO hotplug code in order
  165. * to remove all hash entries from a given address range used to map IO
  166. * space on a removed PCI-PCI bidge without tearing down the full mapping
  167. * since 64K pages may overlap with other bridges when using 64K pages
  168. * with 4K HW pages on IO space.
  169. *
  170. * Because of that usage pattern, it is implemented for small size rather
  171. * than speed.
  172. */
  173. void __flush_hash_table_range(unsigned long start, unsigned long end)
  174. {
  175. int hugepage_shift;
  176. unsigned long flags;
  177. start = ALIGN_DOWN(start, PAGE_SIZE);
  178. end = ALIGN(end, PAGE_SIZE);
  179. /*
  180. * Note: Normally, we should only ever use a batch within a
  181. * PTE locked section. This violates the rule, but will work
  182. * since we don't actually modify the PTEs, we just flush the
  183. * hash while leaving the PTEs intact (including their reference
  184. * to being hashed). This is not the most performance oriented
  185. * way to do things but is fine for our needs here.
  186. */
  187. local_irq_save(flags);
  188. arch_enter_lazy_mmu_mode();
  189. for (; start < end; start += PAGE_SIZE) {
  190. pte_t *ptep = find_init_mm_pte(start, &hugepage_shift);
  191. unsigned long pte;
  192. if (ptep == NULL)
  193. continue;
  194. pte = pte_val(*ptep);
  195. if (!(pte & H_PAGE_HASHPTE))
  196. continue;
  197. hpte_need_flush(&init_mm, start, ptep, pte, hugepage_shift);
  198. }
  199. arch_leave_lazy_mmu_mode();
  200. local_irq_restore(flags);
  201. }
  202. void flush_hash_table_pmd_range(struct mm_struct *mm, pmd_t *pmd, unsigned long addr)
  203. {
  204. pte_t *pte;
  205. pte_t *start_pte;
  206. unsigned long flags;
  207. addr = ALIGN_DOWN(addr, PMD_SIZE);
  208. /*
  209. * Note: Normally, we should only ever use a batch within a
  210. * PTE locked section. This violates the rule, but will work
  211. * since we don't actually modify the PTEs, we just flush the
  212. * hash while leaving the PTEs intact (including their reference
  213. * to being hashed). This is not the most performance oriented
  214. * way to do things but is fine for our needs here.
  215. */
  216. local_irq_save(flags);
  217. arch_enter_lazy_mmu_mode();
  218. start_pte = pte_offset_map(pmd, addr);
  219. for (pte = start_pte; pte < start_pte + PTRS_PER_PTE; pte++) {
  220. unsigned long pteval = pte_val(*pte);
  221. if (pteval & H_PAGE_HASHPTE)
  222. hpte_need_flush(mm, addr, pte, pteval, 0);
  223. addr += PAGE_SIZE;
  224. }
  225. arch_leave_lazy_mmu_mode();
  226. local_irq_restore(flags);
  227. }