tlb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* arch/sparc64/mm/tlb.c
  2. *
  3. * Copyright (C) 2004 David S. Miller <davem@redhat.com>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/percpu.h>
  7. #include <linux/mm.h>
  8. #include <linux/swap.h>
  9. #include <linux/preempt.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/pgalloc.h>
  12. #include <asm/tlbflush.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/tlb.h>
  16. /* Heavily inspired by the ppc64 code. */
  17. static DEFINE_PER_CPU(struct tlb_batch, tlb_batch);
  18. void flush_tlb_pending(void)
  19. {
  20. struct tlb_batch *tb = &get_cpu_var(tlb_batch);
  21. struct mm_struct *mm = tb->mm;
  22. if (!tb->tlb_nr)
  23. goto out;
  24. flush_tsb_user(tb);
  25. if (CTX_VALID(mm->context)) {
  26. if (tb->tlb_nr == 1) {
  27. global_flush_tlb_page(mm, tb->vaddrs[0]);
  28. } else {
  29. #ifdef CONFIG_SMP
  30. smp_flush_tlb_pending(tb->mm, tb->tlb_nr,
  31. &tb->vaddrs[0]);
  32. #else
  33. __flush_tlb_pending(CTX_HWBITS(tb->mm->context),
  34. tb->tlb_nr, &tb->vaddrs[0]);
  35. #endif
  36. }
  37. }
  38. tb->tlb_nr = 0;
  39. out:
  40. put_cpu_var(tlb_batch);
  41. }
  42. void arch_enter_lazy_mmu_mode(void)
  43. {
  44. struct tlb_batch *tb = this_cpu_ptr(&tlb_batch);
  45. tb->active = 1;
  46. }
  47. void arch_leave_lazy_mmu_mode(void)
  48. {
  49. struct tlb_batch *tb = this_cpu_ptr(&tlb_batch);
  50. if (tb->tlb_nr)
  51. flush_tlb_pending();
  52. tb->active = 0;
  53. }
  54. static void tlb_batch_add_one(struct mm_struct *mm, unsigned long vaddr,
  55. bool exec, bool huge)
  56. {
  57. struct tlb_batch *tb = &get_cpu_var(tlb_batch);
  58. unsigned long nr;
  59. vaddr &= PAGE_MASK;
  60. if (exec)
  61. vaddr |= 0x1UL;
  62. nr = tb->tlb_nr;
  63. if (unlikely(nr != 0 && mm != tb->mm)) {
  64. flush_tlb_pending();
  65. nr = 0;
  66. }
  67. if (!tb->active) {
  68. flush_tsb_user_page(mm, vaddr, huge);
  69. global_flush_tlb_page(mm, vaddr);
  70. goto out;
  71. }
  72. if (nr == 0) {
  73. tb->mm = mm;
  74. tb->huge = huge;
  75. }
  76. if (tb->huge != huge) {
  77. flush_tlb_pending();
  78. tb->huge = huge;
  79. nr = 0;
  80. }
  81. tb->vaddrs[nr] = vaddr;
  82. tb->tlb_nr = ++nr;
  83. if (nr >= TLB_BATCH_NR)
  84. flush_tlb_pending();
  85. out:
  86. put_cpu_var(tlb_batch);
  87. }
  88. void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr,
  89. pte_t *ptep, pte_t orig, int fullmm)
  90. {
  91. bool huge = is_hugetlb_pte(orig);
  92. if (tlb_type != hypervisor &&
  93. pte_dirty(orig)) {
  94. unsigned long paddr, pfn = pte_pfn(orig);
  95. struct address_space *mapping;
  96. struct page *page;
  97. if (!pfn_valid(pfn))
  98. goto no_cache_flush;
  99. page = pfn_to_page(pfn);
  100. if (PageReserved(page))
  101. goto no_cache_flush;
  102. /* A real file page? */
  103. mapping = page_mapping(page);
  104. if (!mapping)
  105. goto no_cache_flush;
  106. paddr = (unsigned long) page_address(page);
  107. if ((paddr ^ vaddr) & (1 << 13))
  108. flush_dcache_page_all(mm, page);
  109. }
  110. no_cache_flush:
  111. if (!fullmm)
  112. tlb_batch_add_one(mm, vaddr, pte_exec(orig), huge);
  113. }
  114. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  115. static void tlb_batch_pmd_scan(struct mm_struct *mm, unsigned long vaddr,
  116. pmd_t pmd)
  117. {
  118. unsigned long end;
  119. pte_t *pte;
  120. pte = pte_offset_map(&pmd, vaddr);
  121. end = vaddr + HPAGE_SIZE;
  122. while (vaddr < end) {
  123. if (pte_val(*pte) & _PAGE_VALID) {
  124. bool exec = pte_exec(*pte);
  125. tlb_batch_add_one(mm, vaddr, exec, false);
  126. }
  127. pte++;
  128. vaddr += PAGE_SIZE;
  129. }
  130. pte_unmap(pte);
  131. }
  132. void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  133. pmd_t *pmdp, pmd_t pmd)
  134. {
  135. pmd_t orig = *pmdp;
  136. *pmdp = pmd;
  137. if (mm == &init_mm)
  138. return;
  139. if ((pmd_val(pmd) ^ pmd_val(orig)) & _PAGE_PMD_HUGE) {
  140. /*
  141. * Note that this routine only sets pmds for THP pages.
  142. * Hugetlb pages are handled elsewhere. We need to check
  143. * for huge zero page. Huge zero pages are like hugetlb
  144. * pages in that there is no RSS, but there is the need
  145. * for TSB entries. So, huge zero page counts go into
  146. * hugetlb_pte_count.
  147. */
  148. if (pmd_val(pmd) & _PAGE_PMD_HUGE) {
  149. if (is_huge_zero_page(pmd_page(pmd)))
  150. mm->context.hugetlb_pte_count++;
  151. else
  152. mm->context.thp_pte_count++;
  153. } else {
  154. if (is_huge_zero_page(pmd_page(orig)))
  155. mm->context.hugetlb_pte_count--;
  156. else
  157. mm->context.thp_pte_count--;
  158. }
  159. /* Do not try to allocate the TSB hash table if we
  160. * don't have one already. We have various locks held
  161. * and thus we'll end up doing a GFP_KERNEL allocation
  162. * in an atomic context.
  163. *
  164. * Instead, we let the first TLB miss on a hugepage
  165. * take care of this.
  166. */
  167. }
  168. if (!pmd_none(orig)) {
  169. addr &= HPAGE_MASK;
  170. if (pmd_trans_huge(orig)) {
  171. pte_t orig_pte = __pte(pmd_val(orig));
  172. bool exec = pte_exec(orig_pte);
  173. tlb_batch_add_one(mm, addr, exec, true);
  174. tlb_batch_add_one(mm, addr + REAL_HPAGE_SIZE, exec,
  175. true);
  176. } else {
  177. tlb_batch_pmd_scan(mm, addr, orig);
  178. }
  179. }
  180. }
  181. /*
  182. * This routine is only called when splitting a THP
  183. */
  184. void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  185. pmd_t *pmdp)
  186. {
  187. pmd_t entry = *pmdp;
  188. pmd_val(entry) &= ~_PAGE_VALID;
  189. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  190. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  191. /*
  192. * set_pmd_at() will not be called in a way to decrement
  193. * thp_pte_count when splitting a THP, so do it now.
  194. * Sanity check pmd before doing the actual decrement.
  195. */
  196. if ((pmd_val(entry) & _PAGE_PMD_HUGE) &&
  197. !is_huge_zero_page(pmd_page(entry)))
  198. (vma->vm_mm)->context.thp_pte_count--;
  199. }
  200. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  201. pgtable_t pgtable)
  202. {
  203. struct list_head *lh = (struct list_head *) pgtable;
  204. assert_spin_locked(&mm->page_table_lock);
  205. /* FIFO */
  206. if (!pmd_huge_pte(mm, pmdp))
  207. INIT_LIST_HEAD(lh);
  208. else
  209. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  210. pmd_huge_pte(mm, pmdp) = pgtable;
  211. }
  212. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  213. {
  214. struct list_head *lh;
  215. pgtable_t pgtable;
  216. assert_spin_locked(&mm->page_table_lock);
  217. /* FIFO */
  218. pgtable = pmd_huge_pte(mm, pmdp);
  219. lh = (struct list_head *) pgtable;
  220. if (list_empty(lh))
  221. pmd_huge_pte(mm, pmdp) = NULL;
  222. else {
  223. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  224. list_del(lh);
  225. }
  226. pte_val(pgtable[0]) = 0;
  227. pte_val(pgtable[1]) = 0;
  228. return pgtable;
  229. }
  230. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */