tlb.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* asm-generic/tlb.h
  2. *
  3. * Generic TLB shootdown code
  4. *
  5. * Copyright 2001 Red Hat, Inc.
  6. * Based on code from mm/memory.c Copyright Linus Torvalds and others.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #ifndef _ASM_GENERIC__TLB_H
  14. #define _ASM_GENERIC__TLB_H
  15. #include <linux/config.h>
  16. #include <linux/swap.h>
  17. #include <asm/pgalloc.h>
  18. #include <asm/tlbflush.h>
  19. /*
  20. * For UP we don't need to worry about TLB flush
  21. * and page free order so much..
  22. */
  23. #ifdef CONFIG_SMP
  24. #ifdef ARCH_FREE_PTR_NR
  25. #define FREE_PTR_NR ARCH_FREE_PTR_NR
  26. #else
  27. #define FREE_PTE_NR 506
  28. #endif
  29. #define tlb_fast_mode(tlb) ((tlb)->nr == ~0U)
  30. #else
  31. #define FREE_PTE_NR 1
  32. #define tlb_fast_mode(tlb) 1
  33. #endif
  34. /* struct mmu_gather is an opaque type used by the mm code for passing around
  35. * any data needed by arch specific code for tlb_remove_page. This structure
  36. * can be per-CPU or per-MM as the page table lock is held for the duration of
  37. * TLB shootdown.
  38. */
  39. struct mmu_gather {
  40. struct mm_struct *mm;
  41. unsigned int nr; /* set to ~0U means fast mode */
  42. unsigned int need_flush;/* Really unmapped some ptes? */
  43. unsigned int fullmm; /* non-zero means full mm flush */
  44. unsigned long freed;
  45. struct page * pages[FREE_PTE_NR];
  46. };
  47. /* Users of the generic TLB shootdown code must declare this storage space. */
  48. DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
  49. /* tlb_gather_mmu
  50. * Return a pointer to an initialized struct mmu_gather.
  51. */
  52. static inline struct mmu_gather *
  53. tlb_gather_mmu(struct mm_struct *mm, unsigned int full_mm_flush)
  54. {
  55. struct mmu_gather *tlb = &per_cpu(mmu_gathers, smp_processor_id());
  56. tlb->mm = mm;
  57. /* Use fast mode if only one CPU is online */
  58. tlb->nr = num_online_cpus() > 1 ? 0U : ~0U;
  59. tlb->fullmm = full_mm_flush;
  60. tlb->freed = 0;
  61. return tlb;
  62. }
  63. static inline void
  64. tlb_flush_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  65. {
  66. if (!tlb->need_flush)
  67. return;
  68. tlb->need_flush = 0;
  69. tlb_flush(tlb);
  70. if (!tlb_fast_mode(tlb)) {
  71. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  72. tlb->nr = 0;
  73. }
  74. }
  75. /* tlb_finish_mmu
  76. * Called at the end of the shootdown operation to free up any resources
  77. * that were required. The page table lock is still held at this point.
  78. */
  79. static inline void
  80. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  81. {
  82. int freed = tlb->freed;
  83. struct mm_struct *mm = tlb->mm;
  84. int rss = get_mm_counter(mm, rss);
  85. if (rss < freed)
  86. freed = rss;
  87. add_mm_counter(mm, rss, -freed);
  88. tlb_flush_mmu(tlb, start, end);
  89. /* keep the page table cache within bounds */
  90. check_pgt_cache();
  91. }
  92. static inline unsigned int
  93. tlb_is_full_mm(struct mmu_gather *tlb)
  94. {
  95. return tlb->fullmm;
  96. }
  97. /* tlb_remove_page
  98. * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
  99. * handling the additional races in SMP caused by other CPUs caching valid
  100. * mappings in their TLBs.
  101. */
  102. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  103. {
  104. tlb->need_flush = 1;
  105. if (tlb_fast_mode(tlb)) {
  106. free_page_and_swap_cache(page);
  107. return;
  108. }
  109. tlb->pages[tlb->nr++] = page;
  110. if (tlb->nr >= FREE_PTE_NR)
  111. tlb_flush_mmu(tlb, 0, 0);
  112. }
  113. /**
  114. * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
  115. *
  116. * Record the fact that pte's were really umapped in ->need_flush, so we can
  117. * later optimise away the tlb invalidate. This helps when userspace is
  118. * unmapping already-unmapped pages, which happens quite a lot.
  119. */
  120. #define tlb_remove_tlb_entry(tlb, ptep, address) \
  121. do { \
  122. tlb->need_flush = 1; \
  123. __tlb_remove_tlb_entry(tlb, ptep, address); \
  124. } while (0)
  125. #define pte_free_tlb(tlb, ptep) \
  126. do { \
  127. tlb->need_flush = 1; \
  128. __pte_free_tlb(tlb, ptep); \
  129. } while (0)
  130. #ifndef __ARCH_HAS_4LEVEL_HACK
  131. #define pud_free_tlb(tlb, pudp) \
  132. do { \
  133. tlb->need_flush = 1; \
  134. __pud_free_tlb(tlb, pudp); \
  135. } while (0)
  136. #endif
  137. #define pmd_free_tlb(tlb, pmdp) \
  138. do { \
  139. tlb->need_flush = 1; \
  140. __pmd_free_tlb(tlb, pmdp); \
  141. } while (0)
  142. #define tlb_migrate_finish(mm) do {} while (0)
  143. #endif /* _ASM_GENERIC__TLB_H */