tlb.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * Copyright 2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #ifndef _ASM_GENERIC__TLB_H
  16. #define _ASM_GENERIC__TLB_H
  17. #include <linux/swap.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/tlbflush.h>
  20. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  21. /*
  22. * Semi RCU freeing of the page directories.
  23. *
  24. * This is needed by some architectures to implement software pagetable walkers.
  25. *
  26. * gup_fast() and other software pagetable walkers do a lockless page-table
  27. * walk and therefore needs some synchronization with the freeing of the page
  28. * directories. The chosen means to accomplish that is by disabling IRQs over
  29. * the walk.
  30. *
  31. * Architectures that use IPIs to flush TLBs will then automagically DTRT,
  32. * since we unlink the page, flush TLBs, free the page. Since the disabling of
  33. * IRQs delays the completion of the TLB flush we can never observe an already
  34. * freed page.
  35. *
  36. * Architectures that do not have this (PPC) need to delay the freeing by some
  37. * other means, this is that means.
  38. *
  39. * What we do is batch the freed directory pages (tables) and RCU free them.
  40. * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
  41. * holds off grace periods.
  42. *
  43. * However, in order to batch these pages we need to allocate storage, this
  44. * allocation is deep inside the MM code and can thus easily fail on memory
  45. * pressure. To guarantee progress we fall back to single table freeing, see
  46. * the implementation of tlb_remove_table_one().
  47. *
  48. */
  49. struct mmu_table_batch {
  50. struct rcu_head rcu;
  51. unsigned int nr;
  52. void *tables[0];
  53. };
  54. #define MAX_TABLE_BATCH \
  55. ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
  56. extern void tlb_table_flush(struct mmu_gather *tlb);
  57. extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
  58. #endif
  59. /*
  60. * If we can't allocate a page to make a big batch of page pointers
  61. * to work on, then just handle a few from the on-stack structure.
  62. */
  63. #define MMU_GATHER_BUNDLE 8
  64. struct mmu_gather_batch {
  65. struct mmu_gather_batch *next;
  66. unsigned int nr;
  67. unsigned int max;
  68. struct page *pages[0];
  69. };
  70. #define MAX_GATHER_BATCH \
  71. ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
  72. /* struct mmu_gather is an opaque type used by the mm code for passing around
  73. * any data needed by arch specific code for tlb_remove_page.
  74. */
  75. struct mmu_gather {
  76. struct mm_struct *mm;
  77. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  78. struct mmu_table_batch *batch;
  79. #endif
  80. unsigned int need_flush : 1, /* Did free PTEs */
  81. fast_mode : 1; /* No batching */
  82. unsigned int fullmm;
  83. struct mmu_gather_batch *active;
  84. struct mmu_gather_batch local;
  85. struct page *__pages[MMU_GATHER_BUNDLE];
  86. };
  87. #define HAVE_GENERIC_MMU_GATHER
  88. static inline int tlb_fast_mode(struct mmu_gather *tlb)
  89. {
  90. #ifdef CONFIG_SMP
  91. return tlb->fast_mode;
  92. #else
  93. /*
  94. * For UP we don't need to worry about TLB flush
  95. * and page free order so much..
  96. */
  97. return 1;
  98. #endif
  99. }
  100. void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm);
  101. void tlb_flush_mmu(struct mmu_gather *tlb);
  102. void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end);
  103. int __tlb_remove_page(struct mmu_gather *tlb, struct page *page);
  104. /* tlb_remove_page
  105. * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
  106. * required.
  107. */
  108. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  109. {
  110. if (!__tlb_remove_page(tlb, page))
  111. tlb_flush_mmu(tlb);
  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, address) \
  126. do { \
  127. tlb->need_flush = 1; \
  128. __pte_free_tlb(tlb, ptep, address); \
  129. } while (0)
  130. #ifndef __ARCH_HAS_4LEVEL_HACK
  131. #define pud_free_tlb(tlb, pudp, address) \
  132. do { \
  133. tlb->need_flush = 1; \
  134. __pud_free_tlb(tlb, pudp, address); \
  135. } while (0)
  136. #endif
  137. #define pmd_free_tlb(tlb, pmdp, address) \
  138. do { \
  139. tlb->need_flush = 1; \
  140. __pmd_free_tlb(tlb, pmdp, address); \
  141. } while (0)
  142. #define tlb_migrate_finish(mm) do {} while (0)
  143. #endif /* _ASM_GENERIC__TLB_H */