tlb_hash64.c 7.6 KB

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