tlb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if (pmd_val(pmd) & _PAGE_PMD_HUGE)
  141. mm->context.huge_pte_count++;
  142. else
  143. mm->context.huge_pte_count--;
  144. /* Do not try to allocate the TSB hash table if we
  145. * don't have one already. We have various locks held
  146. * and thus we'll end up doing a GFP_KERNEL allocation
  147. * in an atomic context.
  148. *
  149. * Instead, we let the first TLB miss on a hugepage
  150. * take care of this.
  151. */
  152. }
  153. if (!pmd_none(orig)) {
  154. addr &= HPAGE_MASK;
  155. if (pmd_trans_huge(orig)) {
  156. pte_t orig_pte = __pte(pmd_val(orig));
  157. bool exec = pte_exec(orig_pte);
  158. tlb_batch_add_one(mm, addr, exec, true);
  159. tlb_batch_add_one(mm, addr + REAL_HPAGE_SIZE, exec,
  160. true);
  161. } else {
  162. tlb_batch_pmd_scan(mm, addr, orig);
  163. }
  164. }
  165. }
  166. void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  167. pmd_t *pmdp)
  168. {
  169. pmd_t entry = *pmdp;
  170. pmd_val(entry) &= ~_PAGE_VALID;
  171. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  172. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  173. }
  174. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  175. pgtable_t pgtable)
  176. {
  177. struct list_head *lh = (struct list_head *) pgtable;
  178. assert_spin_locked(&mm->page_table_lock);
  179. /* FIFO */
  180. if (!pmd_huge_pte(mm, pmdp))
  181. INIT_LIST_HEAD(lh);
  182. else
  183. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  184. pmd_huge_pte(mm, pmdp) = pgtable;
  185. }
  186. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  187. {
  188. struct list_head *lh;
  189. pgtable_t pgtable;
  190. assert_spin_locked(&mm->page_table_lock);
  191. /* FIFO */
  192. pgtable = pmd_huge_pte(mm, pmdp);
  193. lh = (struct list_head *) pgtable;
  194. if (list_empty(lh))
  195. pmd_huge_pte(mm, pmdp) = NULL;
  196. else {
  197. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  198. list_del(lh);
  199. }
  200. pte_val(pgtable[0]) = 0;
  201. pte_val(pgtable[1]) = 0;
  202. return pgtable;
  203. }
  204. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */