tlb.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* include/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/swap.h>
  16. #include <asm/pgalloc.h>
  17. #include <asm/tlbflush.h>
  18. /*
  19. * For UP we don't need to worry about TLB flush
  20. * and page free order so much..
  21. */
  22. #ifdef CONFIG_SMP
  23. #ifdef ARCH_FREE_PTR_NR
  24. #define FREE_PTR_NR ARCH_FREE_PTR_NR
  25. #else
  26. #define FREE_PTE_NR 506
  27. #endif
  28. #define tlb_fast_mode(tlb) ((tlb)->nr == ~0U)
  29. #else
  30. #define FREE_PTE_NR 1
  31. #define tlb_fast_mode(tlb) 1
  32. #endif
  33. /* struct mmu_gather is an opaque type used by the mm code for passing around
  34. * any data needed by arch specific code for tlb_remove_page.
  35. */
  36. struct mmu_gather {
  37. struct mm_struct *mm;
  38. unsigned int nr; /* set to ~0U means fast mode */
  39. unsigned int need_flush;/* Really unmapped some ptes? */
  40. unsigned int fullmm; /* non-zero means full mm flush */
  41. struct page * pages[FREE_PTE_NR];
  42. };
  43. /* Users of the generic TLB shootdown code must declare this storage space. */
  44. DECLARE_PER_CPU(struct mmu_gather, mmu_gathers);
  45. /* tlb_gather_mmu
  46. * Return a pointer to an initialized struct mmu_gather.
  47. */
  48. static inline struct mmu_gather *
  49. tlb_gather_mmu(struct mm_struct *mm, unsigned int full_mm_flush)
  50. {
  51. struct mmu_gather *tlb = &get_cpu_var(mmu_gathers);
  52. tlb->mm = mm;
  53. /* Use fast mode if only one CPU is online */
  54. tlb->nr = num_online_cpus() > 1 ? 0U : ~0U;
  55. tlb->fullmm = full_mm_flush;
  56. return tlb;
  57. }
  58. static inline void
  59. tlb_flush_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  60. {
  61. if (!tlb->need_flush)
  62. return;
  63. tlb->need_flush = 0;
  64. tlb_flush(tlb);
  65. if (!tlb_fast_mode(tlb)) {
  66. free_pages_and_swap_cache(tlb->pages, tlb->nr);
  67. tlb->nr = 0;
  68. }
  69. }
  70. /* tlb_finish_mmu
  71. * Called at the end of the shootdown operation to free up any resources
  72. * that were required.
  73. */
  74. static inline void
  75. tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  76. {
  77. tlb_flush_mmu(tlb, start, end);
  78. /* keep the page table cache within bounds */
  79. check_pgt_cache();
  80. put_cpu_var(mmu_gathers);
  81. }
  82. /* tlb_remove_page
  83. * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
  84. * handling the additional races in SMP caused by other CPUs caching valid
  85. * mappings in their TLBs.
  86. */
  87. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  88. {
  89. tlb->need_flush = 1;
  90. if (tlb_fast_mode(tlb)) {
  91. free_page_and_swap_cache(page);
  92. return;
  93. }
  94. tlb->pages[tlb->nr++] = page;
  95. if (tlb->nr >= FREE_PTE_NR)
  96. tlb_flush_mmu(tlb, 0, 0);
  97. }
  98. /**
  99. * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
  100. *
  101. * Record the fact that pte's were really umapped in ->need_flush, so we can
  102. * later optimise away the tlb invalidate. This helps when userspace is
  103. * unmapping already-unmapped pages, which happens quite a lot.
  104. */
  105. #define tlb_remove_tlb_entry(tlb, ptep, address) \
  106. do { \
  107. tlb->need_flush = 1; \
  108. __tlb_remove_tlb_entry(tlb, ptep, address); \
  109. } while (0)
  110. #define pte_free_tlb(tlb, ptep) \
  111. do { \
  112. tlb->need_flush = 1; \
  113. __pte_free_tlb(tlb, ptep); \
  114. } while (0)
  115. #ifndef __ARCH_HAS_4LEVEL_HACK
  116. #define pud_free_tlb(tlb, pudp) \
  117. do { \
  118. tlb->need_flush = 1; \
  119. __pud_free_tlb(tlb, pudp); \
  120. } while (0)
  121. #endif
  122. #define pmd_free_tlb(tlb, pmdp) \
  123. do { \
  124. tlb->need_flush = 1; \
  125. __pmd_free_tlb(tlb, pmdp); \
  126. } while (0)
  127. #define tlb_migrate_finish(mm) do {} while (0)
  128. #endif /* _ASM_GENERIC__TLB_H */