tlb.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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)
  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);
  69. global_flush_tlb_page(mm, vaddr);
  70. goto out;
  71. }
  72. if (nr == 0)
  73. tb->mm = mm;
  74. tb->vaddrs[nr] = vaddr;
  75. tb->tlb_nr = ++nr;
  76. if (nr >= TLB_BATCH_NR)
  77. flush_tlb_pending();
  78. out:
  79. put_cpu_var(tlb_batch);
  80. }
  81. void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr,
  82. pte_t *ptep, pte_t orig, int fullmm)
  83. {
  84. if (tlb_type != hypervisor &&
  85. pte_dirty(orig)) {
  86. unsigned long paddr, pfn = pte_pfn(orig);
  87. struct address_space *mapping;
  88. struct page *page;
  89. if (!pfn_valid(pfn))
  90. goto no_cache_flush;
  91. page = pfn_to_page(pfn);
  92. if (PageReserved(page))
  93. goto no_cache_flush;
  94. /* A real file page? */
  95. mapping = page_mapping(page);
  96. if (!mapping)
  97. goto no_cache_flush;
  98. paddr = (unsigned long) page_address(page);
  99. if ((paddr ^ vaddr) & (1 << 13))
  100. flush_dcache_page_all(mm, page);
  101. }
  102. no_cache_flush:
  103. if (!fullmm)
  104. tlb_batch_add_one(mm, vaddr, pte_exec(orig));
  105. }
  106. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  107. static void tlb_batch_pmd_scan(struct mm_struct *mm, unsigned long vaddr,
  108. pmd_t pmd)
  109. {
  110. unsigned long end;
  111. pte_t *pte;
  112. pte = pte_offset_map(&pmd, vaddr);
  113. end = vaddr + HPAGE_SIZE;
  114. while (vaddr < end) {
  115. if (pte_val(*pte) & _PAGE_VALID) {
  116. bool exec = pte_exec(*pte);
  117. tlb_batch_add_one(mm, vaddr, exec);
  118. }
  119. pte++;
  120. vaddr += PAGE_SIZE;
  121. }
  122. pte_unmap(pte);
  123. }
  124. void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  125. pmd_t *pmdp, pmd_t pmd)
  126. {
  127. pmd_t orig = *pmdp;
  128. *pmdp = pmd;
  129. if (mm == &init_mm)
  130. return;
  131. if ((pmd_val(pmd) ^ pmd_val(orig)) & _PAGE_PMD_HUGE) {
  132. if (pmd_val(pmd) & _PAGE_PMD_HUGE)
  133. mm->context.huge_pte_count++;
  134. else
  135. mm->context.huge_pte_count--;
  136. /* Do not try to allocate the TSB hash table if we
  137. * don't have one already. We have various locks held
  138. * and thus we'll end up doing a GFP_KERNEL allocation
  139. * in an atomic context.
  140. *
  141. * Instead, we let the first TLB miss on a hugepage
  142. * take care of this.
  143. */
  144. }
  145. if (!pmd_none(orig)) {
  146. addr &= HPAGE_MASK;
  147. if (pmd_trans_huge(orig)) {
  148. pte_t orig_pte = __pte(pmd_val(orig));
  149. bool exec = pte_exec(orig_pte);
  150. tlb_batch_add_one(mm, addr, exec);
  151. tlb_batch_add_one(mm, addr + REAL_HPAGE_SIZE, exec);
  152. } else {
  153. tlb_batch_pmd_scan(mm, addr, orig);
  154. }
  155. }
  156. }
  157. void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
  158. pmd_t *pmdp)
  159. {
  160. pmd_t entry = *pmdp;
  161. pmd_val(entry) &= ~_PAGE_VALID;
  162. set_pmd_at(vma->vm_mm, address, pmdp, entry);
  163. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  164. }
  165. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  166. pgtable_t pgtable)
  167. {
  168. struct list_head *lh = (struct list_head *) pgtable;
  169. assert_spin_locked(&mm->page_table_lock);
  170. /* FIFO */
  171. if (!pmd_huge_pte(mm, pmdp))
  172. INIT_LIST_HEAD(lh);
  173. else
  174. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  175. pmd_huge_pte(mm, pmdp) = pgtable;
  176. }
  177. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  178. {
  179. struct list_head *lh;
  180. pgtable_t pgtable;
  181. assert_spin_locked(&mm->page_table_lock);
  182. /* FIFO */
  183. pgtable = pmd_huge_pte(mm, pmdp);
  184. lh = (struct list_head *) pgtable;
  185. if (list_empty(lh))
  186. pmd_huge_pte(mm, pmdp) = NULL;
  187. else {
  188. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  189. list_del(lh);
  190. }
  191. pte_val(pgtable[0]) = 0;
  192. pte_val(pgtable[1]) = 0;
  193. return pgtable;
  194. }
  195. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */