tlb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 = &__get_cpu_var(tlb_batch);
  45. tb->active = 1;
  46. }
  47. void arch_leave_lazy_mmu_mode(void)
  48. {
  49. struct tlb_batch *tb = &__get_cpu_var(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, bool exec)
  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. tlb_batch_add_one(mm, vaddr, exec);
  117. pte++;
  118. vaddr += PAGE_SIZE;
  119. }
  120. pte_unmap(pte);
  121. }
  122. void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  123. pmd_t *pmdp, pmd_t pmd)
  124. {
  125. pmd_t orig = *pmdp;
  126. *pmdp = pmd;
  127. if (mm == &init_mm)
  128. return;
  129. if ((pmd_val(pmd) ^ pmd_val(orig)) & _PAGE_PMD_HUGE) {
  130. if (pmd_val(pmd) & _PAGE_PMD_HUGE)
  131. mm->context.huge_pte_count++;
  132. else
  133. mm->context.huge_pte_count--;
  134. /* Do not try to allocate the TSB hash table if we
  135. * don't have one already. We have various locks held
  136. * and thus we'll end up doing a GFP_KERNEL allocation
  137. * in an atomic context.
  138. *
  139. * Instead, we let the first TLB miss on a hugepage
  140. * take care of this.
  141. */
  142. }
  143. if (!pmd_none(orig)) {
  144. pte_t orig_pte = __pte(pmd_val(orig));
  145. bool exec = pte_exec(orig_pte);
  146. addr &= HPAGE_MASK;
  147. if (pmd_trans_huge(orig)) {
  148. tlb_batch_add_one(mm, addr, exec);
  149. tlb_batch_add_one(mm, addr + REAL_HPAGE_SIZE, exec);
  150. } else {
  151. tlb_batch_pmd_scan(mm, addr, orig, exec);
  152. }
  153. }
  154. }
  155. void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  156. pgtable_t pgtable)
  157. {
  158. struct list_head *lh = (struct list_head *) pgtable;
  159. assert_spin_locked(&mm->page_table_lock);
  160. /* FIFO */
  161. if (!pmd_huge_pte(mm, pmdp))
  162. INIT_LIST_HEAD(lh);
  163. else
  164. list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
  165. pmd_huge_pte(mm, pmdp) = pgtable;
  166. }
  167. pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
  168. {
  169. struct list_head *lh;
  170. pgtable_t pgtable;
  171. assert_spin_locked(&mm->page_table_lock);
  172. /* FIFO */
  173. pgtable = pmd_huge_pte(mm, pmdp);
  174. lh = (struct list_head *) pgtable;
  175. if (list_empty(lh))
  176. pmd_huge_pte(mm, pmdp) = NULL;
  177. else {
  178. pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
  179. list_del(lh);
  180. }
  181. pte_val(pgtable[0]) = 0;
  182. pte_val(pgtable[1]) = 0;
  183. return pgtable;
  184. }
  185. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */