tlb.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* include/asm-generic/tlb.h
  3. *
  4. * Generic TLB shootdown code
  5. *
  6. * Copyright 2001 Red Hat, Inc.
  7. * Based on code from mm/memory.c Copyright Linus Torvalds and others.
  8. *
  9. * Copyright 2011 Red Hat, Inc., Peter Zijlstra
  10. */
  11. #ifndef _ASM_GENERIC__TLB_H
  12. #define _ASM_GENERIC__TLB_H
  13. #include <linux/mmu_notifier.h>
  14. #include <linux/swap.h>
  15. #include <linux/hugetlb_inline.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/cacheflush.h>
  18. /*
  19. * Blindly accessing user memory from NMI context can be dangerous
  20. * if we're in the middle of switching the current user task or switching
  21. * the loaded mm.
  22. */
  23. #ifndef nmi_uaccess_okay
  24. # define nmi_uaccess_okay() true
  25. #endif
  26. #ifdef CONFIG_MMU
  27. /*
  28. * Generic MMU-gather implementation.
  29. *
  30. * The mmu_gather data structure is used by the mm code to implement the
  31. * correct and efficient ordering of freeing pages and TLB invalidations.
  32. *
  33. * This correct ordering is:
  34. *
  35. * 1) unhook page
  36. * 2) TLB invalidate page
  37. * 3) free page
  38. *
  39. * That is, we must never free a page before we have ensured there are no live
  40. * translations left to it. Otherwise it might be possible to observe (or
  41. * worse, change) the page content after it has been reused.
  42. *
  43. * The mmu_gather API consists of:
  44. *
  45. * - tlb_gather_mmu() / tlb_gather_mmu_fullmm() / tlb_finish_mmu()
  46. *
  47. * start and finish a mmu_gather
  48. *
  49. * Finish in particular will issue a (final) TLB invalidate and free
  50. * all (remaining) queued pages.
  51. *
  52. * - tlb_start_vma() / tlb_end_vma(); marks the start / end of a VMA
  53. *
  54. * Defaults to flushing at tlb_end_vma() to reset the range; helps when
  55. * there's large holes between the VMAs.
  56. *
  57. * - tlb_remove_table()
  58. *
  59. * tlb_remove_table() is the basic primitive to free page-table directories
  60. * (__p*_free_tlb()). In it's most primitive form it is an alias for
  61. * tlb_remove_page() below, for when page directories are pages and have no
  62. * additional constraints.
  63. *
  64. * See also MMU_GATHER_TABLE_FREE and MMU_GATHER_RCU_TABLE_FREE.
  65. *
  66. * - tlb_remove_page() / __tlb_remove_page()
  67. * - tlb_remove_page_size() / __tlb_remove_page_size()
  68. * - __tlb_remove_folio_pages()
  69. *
  70. * __tlb_remove_page_size() is the basic primitive that queues a page for
  71. * freeing. __tlb_remove_page() assumes PAGE_SIZE. Both will return a
  72. * boolean indicating if the queue is (now) full and a call to
  73. * tlb_flush_mmu() is required.
  74. *
  75. * tlb_remove_page() and tlb_remove_page_size() imply the call to
  76. * tlb_flush_mmu() when required and has no return value.
  77. *
  78. * __tlb_remove_folio_pages() is similar to __tlb_remove_page(), however,
  79. * instead of removing a single page, remove the given number of consecutive
  80. * pages that are all part of the same (large) folio: just like calling
  81. * __tlb_remove_page() on each page individually.
  82. *
  83. * - tlb_change_page_size()
  84. *
  85. * call before __tlb_remove_page*() to set the current page-size; implies a
  86. * possible tlb_flush_mmu() call.
  87. *
  88. * - tlb_flush_mmu() / tlb_flush_mmu_tlbonly()
  89. *
  90. * tlb_flush_mmu_tlbonly() - does the TLB invalidate (and resets
  91. * related state, like the range)
  92. *
  93. * tlb_flush_mmu() - in addition to the above TLB invalidate, also frees
  94. * whatever pages are still batched.
  95. *
  96. * - mmu_gather::fullmm
  97. *
  98. * A flag set by tlb_gather_mmu_fullmm() to indicate we're going to free
  99. * the entire mm; this allows a number of optimizations.
  100. *
  101. * - We can ignore tlb_{start,end}_vma(); because we don't
  102. * care about ranges. Everything will be shot down.
  103. *
  104. * - (RISC) architectures that use ASIDs can cycle to a new ASID
  105. * and delay the invalidation until ASID space runs out.
  106. *
  107. * - mmu_gather::need_flush_all
  108. *
  109. * A flag that can be set by the arch code if it wants to force
  110. * flush the entire TLB irrespective of the range. For instance
  111. * x86-PAE needs this when changing top-level entries.
  112. *
  113. * And allows the architecture to provide and implement tlb_flush():
  114. *
  115. * tlb_flush() may, in addition to the above mentioned mmu_gather fields, make
  116. * use of:
  117. *
  118. * - mmu_gather::start / mmu_gather::end
  119. *
  120. * which provides the range that needs to be flushed to cover the pages to
  121. * be freed.
  122. *
  123. * - mmu_gather::freed_tables
  124. *
  125. * set when we freed page table pages
  126. *
  127. * - tlb_get_unmap_shift() / tlb_get_unmap_size()
  128. *
  129. * returns the smallest TLB entry size unmapped in this range.
  130. *
  131. * If an architecture does not provide tlb_flush() a default implementation
  132. * based on flush_tlb_range() will be used, unless MMU_GATHER_NO_RANGE is
  133. * specified, in which case we'll default to flush_tlb_mm().
  134. *
  135. * Additionally there are a few opt-in features:
  136. *
  137. * MMU_GATHER_PAGE_SIZE
  138. *
  139. * This ensures we call tlb_flush() every time tlb_change_page_size() actually
  140. * changes the size and provides mmu_gather::page_size to tlb_flush().
  141. *
  142. * This might be useful if your architecture has size specific TLB
  143. * invalidation instructions.
  144. *
  145. * MMU_GATHER_TABLE_FREE
  146. *
  147. * This provides tlb_remove_table(), to be used instead of tlb_remove_page()
  148. * for page directores (__p*_free_tlb()).
  149. *
  150. * Useful if your architecture has non-page page directories.
  151. *
  152. * When used, an architecture is expected to provide __tlb_remove_table()
  153. * which does the actual freeing of these pages.
  154. *
  155. * MMU_GATHER_RCU_TABLE_FREE
  156. *
  157. * Like MMU_GATHER_TABLE_FREE, and adds semi-RCU semantics to the free (see
  158. * comment below).
  159. *
  160. * Useful if your architecture doesn't use IPIs for remote TLB invalidates
  161. * and therefore doesn't naturally serialize with software page-table walkers.
  162. *
  163. * MMU_GATHER_NO_FLUSH_CACHE
  164. *
  165. * Indicates the architecture has flush_cache_range() but it needs *NOT* be called
  166. * before unmapping a VMA.
  167. *
  168. * NOTE: strictly speaking we shouldn't have this knob and instead rely on
  169. * flush_cache_range() being a NOP, except Sparc64 seems to be
  170. * different here.
  171. *
  172. * MMU_GATHER_MERGE_VMAS
  173. *
  174. * Indicates the architecture wants to merge ranges over VMAs; typical when
  175. * multiple range invalidates are more expensive than a full invalidate.
  176. *
  177. * MMU_GATHER_NO_RANGE
  178. *
  179. * Use this if your architecture lacks an efficient flush_tlb_range(). This
  180. * option implies MMU_GATHER_MERGE_VMAS above.
  181. *
  182. * MMU_GATHER_NO_GATHER
  183. *
  184. * If the option is set the mmu_gather will not track individual pages for
  185. * delayed page free anymore. A platform that enables the option needs to
  186. * provide its own implementation of the __tlb_remove_page_size() function to
  187. * free pages.
  188. *
  189. * This is useful if your architecture already flushes TLB entries in the
  190. * various ptep_get_and_clear() functions.
  191. */
  192. #ifdef CONFIG_MMU_GATHER_TABLE_FREE
  193. struct mmu_table_batch {
  194. #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
  195. struct rcu_head rcu;
  196. #endif
  197. unsigned int nr;
  198. void *tables[];
  199. };
  200. #define MAX_TABLE_BATCH \
  201. ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
  202. extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
  203. #else /* !CONFIG_MMU_GATHER_HAVE_TABLE_FREE */
  204. /*
  205. * Without MMU_GATHER_TABLE_FREE the architecture is assumed to have page based
  206. * page directories and we can use the normal page batching to free them.
  207. */
  208. #define tlb_remove_table(tlb, page) tlb_remove_page((tlb), (page))
  209. #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
  210. #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
  211. /*
  212. * This allows an architecture that does not use the linux page-tables for
  213. * hardware to skip the TLBI when freeing page tables.
  214. */
  215. #ifndef tlb_needs_table_invalidate
  216. #define tlb_needs_table_invalidate() (true)
  217. #endif
  218. void tlb_remove_table_sync_one(void);
  219. #else
  220. #ifdef tlb_needs_table_invalidate
  221. #error tlb_needs_table_invalidate() requires MMU_GATHER_RCU_TABLE_FREE
  222. #endif
  223. static inline void tlb_remove_table_sync_one(void) { }
  224. #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
  225. #ifndef CONFIG_MMU_GATHER_NO_GATHER
  226. /*
  227. * If we can't allocate a page to make a big batch of page pointers
  228. * to work on, then just handle a few from the on-stack structure.
  229. */
  230. #define MMU_GATHER_BUNDLE 8
  231. struct mmu_gather_batch {
  232. struct mmu_gather_batch *next;
  233. unsigned int nr;
  234. unsigned int max;
  235. struct encoded_page *encoded_pages[];
  236. };
  237. #define MAX_GATHER_BATCH \
  238. ((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
  239. /*
  240. * Limit the maximum number of mmu_gather batches to reduce a risk of soft
  241. * lockups for non-preemptible kernels on huge machines when a lot of memory
  242. * is zapped during unmapping.
  243. * 10K pages freed at once should be safe even without a preemption point.
  244. */
  245. #define MAX_GATHER_BATCH_COUNT (10000UL/MAX_GATHER_BATCH)
  246. extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
  247. bool delay_rmap, int page_size);
  248. bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
  249. unsigned int nr_pages, bool delay_rmap);
  250. #ifdef CONFIG_SMP
  251. /*
  252. * This both sets 'delayed_rmap', and returns true. It would be an inline
  253. * function, except we define it before the 'struct mmu_gather'.
  254. */
  255. #define tlb_delay_rmap(tlb) (((tlb)->delayed_rmap = 1), true)
  256. extern void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma);
  257. #endif
  258. #endif
  259. /*
  260. * We have a no-op version of the rmap removal that doesn't
  261. * delay anything. That is used on S390, which flushes remote
  262. * TLBs synchronously, and on UP, which doesn't have any
  263. * remote TLBs to flush and is not preemptible due to this
  264. * all happening under the page table lock.
  265. */
  266. #ifndef tlb_delay_rmap
  267. #define tlb_delay_rmap(tlb) (false)
  268. static inline void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
  269. #endif
  270. /*
  271. * struct mmu_gather is an opaque type used by the mm code for passing around
  272. * any data needed by arch specific code for tlb_remove_page.
  273. */
  274. struct mmu_gather {
  275. struct mm_struct *mm;
  276. #ifdef CONFIG_MMU_GATHER_TABLE_FREE
  277. struct mmu_table_batch *batch;
  278. #endif
  279. unsigned long start;
  280. unsigned long end;
  281. /*
  282. * we are in the middle of an operation to clear
  283. * a full mm and can make some optimizations
  284. */
  285. unsigned int fullmm : 1;
  286. /*
  287. * we have performed an operation which
  288. * requires a complete flush of the tlb
  289. */
  290. unsigned int need_flush_all : 1;
  291. /*
  292. * we have removed page directories
  293. */
  294. unsigned int freed_tables : 1;
  295. /*
  296. * Do we have pending delayed rmap removals?
  297. */
  298. unsigned int delayed_rmap : 1;
  299. /*
  300. * at which levels have we cleared entries?
  301. */
  302. unsigned int cleared_ptes : 1;
  303. unsigned int cleared_pmds : 1;
  304. unsigned int cleared_puds : 1;
  305. unsigned int cleared_p4ds : 1;
  306. /*
  307. * tracks VM_EXEC | VM_HUGETLB in tlb_start_vma
  308. */
  309. unsigned int vma_exec : 1;
  310. unsigned int vma_huge : 1;
  311. unsigned int vma_pfn : 1;
  312. unsigned int batch_count;
  313. #ifndef CONFIG_MMU_GATHER_NO_GATHER
  314. struct mmu_gather_batch *active;
  315. struct mmu_gather_batch local;
  316. struct page *__pages[MMU_GATHER_BUNDLE];
  317. #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
  318. unsigned int page_size;
  319. #endif
  320. #endif
  321. };
  322. void tlb_flush_mmu(struct mmu_gather *tlb);
  323. static inline void __tlb_adjust_range(struct mmu_gather *tlb,
  324. unsigned long address,
  325. unsigned int range_size)
  326. {
  327. tlb->start = min(tlb->start, address);
  328. tlb->end = max(tlb->end, address + range_size);
  329. }
  330. static inline void __tlb_reset_range(struct mmu_gather *tlb)
  331. {
  332. if (tlb->fullmm) {
  333. tlb->start = tlb->end = ~0;
  334. } else {
  335. tlb->start = TASK_SIZE;
  336. tlb->end = 0;
  337. }
  338. tlb->freed_tables = 0;
  339. tlb->cleared_ptes = 0;
  340. tlb->cleared_pmds = 0;
  341. tlb->cleared_puds = 0;
  342. tlb->cleared_p4ds = 0;
  343. /*
  344. * Do not reset mmu_gather::vma_* fields here, we do not
  345. * call into tlb_start_vma() again to set them if there is an
  346. * intermediate flush.
  347. */
  348. }
  349. #ifdef CONFIG_MMU_GATHER_NO_RANGE
  350. #if defined(tlb_flush)
  351. #error MMU_GATHER_NO_RANGE relies on default tlb_flush()
  352. #endif
  353. /*
  354. * When an architecture does not have efficient means of range flushing TLBs
  355. * there is no point in doing intermediate flushes on tlb_end_vma() to keep the
  356. * range small. We equally don't have to worry about page granularity or other
  357. * things.
  358. *
  359. * All we need to do is issue a full flush for any !0 range.
  360. */
  361. static inline void tlb_flush(struct mmu_gather *tlb)
  362. {
  363. if (tlb->end)
  364. flush_tlb_mm(tlb->mm);
  365. }
  366. #else /* CONFIG_MMU_GATHER_NO_RANGE */
  367. #ifndef tlb_flush
  368. /*
  369. * When an architecture does not provide its own tlb_flush() implementation
  370. * but does have a reasonably efficient flush_vma_range() implementation
  371. * use that.
  372. */
  373. static inline void tlb_flush(struct mmu_gather *tlb)
  374. {
  375. if (tlb->fullmm || tlb->need_flush_all) {
  376. flush_tlb_mm(tlb->mm);
  377. } else if (tlb->end) {
  378. struct vm_area_struct vma = {
  379. .vm_mm = tlb->mm,
  380. .vm_flags = (tlb->vma_exec ? VM_EXEC : 0) |
  381. (tlb->vma_huge ? VM_HUGETLB : 0),
  382. };
  383. flush_tlb_range(&vma, tlb->start, tlb->end);
  384. }
  385. }
  386. #endif
  387. #endif /* CONFIG_MMU_GATHER_NO_RANGE */
  388. static inline void
  389. tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma)
  390. {
  391. /*
  392. * flush_tlb_range() implementations that look at VM_HUGETLB (tile,
  393. * mips-4k) flush only large pages.
  394. *
  395. * flush_tlb_range() implementations that flush I-TLB also flush D-TLB
  396. * (tile, xtensa, arm), so it's ok to just add VM_EXEC to an existing
  397. * range.
  398. *
  399. * We rely on tlb_end_vma() to issue a flush, such that when we reset
  400. * these values the batch is empty.
  401. */
  402. tlb->vma_huge = is_vm_hugetlb_page(vma);
  403. tlb->vma_exec = !!(vma->vm_flags & VM_EXEC);
  404. tlb->vma_pfn = !!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP));
  405. }
  406. static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
  407. {
  408. /*
  409. * Anything calling __tlb_adjust_range() also sets at least one of
  410. * these bits.
  411. */
  412. if (!(tlb->freed_tables || tlb->cleared_ptes || tlb->cleared_pmds ||
  413. tlb->cleared_puds || tlb->cleared_p4ds))
  414. return;
  415. tlb_flush(tlb);
  416. __tlb_reset_range(tlb);
  417. }
  418. static inline void tlb_remove_page_size(struct mmu_gather *tlb,
  419. struct page *page, int page_size)
  420. {
  421. if (__tlb_remove_page_size(tlb, page, false, page_size))
  422. tlb_flush_mmu(tlb);
  423. }
  424. static __always_inline bool __tlb_remove_page(struct mmu_gather *tlb,
  425. struct page *page, bool delay_rmap)
  426. {
  427. return __tlb_remove_page_size(tlb, page, delay_rmap, PAGE_SIZE);
  428. }
  429. /* tlb_remove_page
  430. * Similar to __tlb_remove_page but will call tlb_flush_mmu() itself when
  431. * required.
  432. */
  433. static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  434. {
  435. return tlb_remove_page_size(tlb, page, PAGE_SIZE);
  436. }
  437. static inline void tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)
  438. {
  439. tlb_remove_table(tlb, pt);
  440. }
  441. /* Like tlb_remove_ptdesc, but for page-like page directories. */
  442. static inline void tlb_remove_page_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
  443. {
  444. tlb_remove_page(tlb, ptdesc_page(pt));
  445. }
  446. static inline void tlb_change_page_size(struct mmu_gather *tlb,
  447. unsigned int page_size)
  448. {
  449. #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
  450. if (tlb->page_size && tlb->page_size != page_size) {
  451. if (!tlb->fullmm && !tlb->need_flush_all)
  452. tlb_flush_mmu(tlb);
  453. }
  454. tlb->page_size = page_size;
  455. #endif
  456. }
  457. static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
  458. {
  459. if (tlb->cleared_ptes)
  460. return PAGE_SHIFT;
  461. if (tlb->cleared_pmds)
  462. return PMD_SHIFT;
  463. if (tlb->cleared_puds)
  464. return PUD_SHIFT;
  465. if (tlb->cleared_p4ds)
  466. return P4D_SHIFT;
  467. return PAGE_SHIFT;
  468. }
  469. static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
  470. {
  471. return 1UL << tlb_get_unmap_shift(tlb);
  472. }
  473. /*
  474. * In the case of tlb vma handling, we can optimise these away in the
  475. * case where we're doing a full MM flush. When we're doing a munmap,
  476. * the vmas are adjusted to only cover the region to be torn down.
  477. */
  478. static inline void tlb_start_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  479. {
  480. if (tlb->fullmm)
  481. return;
  482. tlb_update_vma_flags(tlb, vma);
  483. #ifndef CONFIG_MMU_GATHER_NO_FLUSH_CACHE
  484. flush_cache_range(vma, vma->vm_start, vma->vm_end);
  485. #endif
  486. }
  487. static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma)
  488. {
  489. if (tlb->fullmm)
  490. return;
  491. /*
  492. * VM_PFNMAP is more fragile because the core mm will not track the
  493. * page mapcount -- there might not be page-frames for these PFNs after
  494. * all. Force flush TLBs for such ranges to avoid munmap() vs
  495. * unmap_mapping_range() races.
  496. */
  497. if (tlb->vma_pfn || !IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) {
  498. /*
  499. * Do a TLB flush and reset the range at VMA boundaries; this avoids
  500. * the ranges growing with the unused space between consecutive VMAs.
  501. */
  502. tlb_flush_mmu_tlbonly(tlb);
  503. }
  504. }
  505. /*
  506. * tlb_flush_{pte|pmd|pud|p4d}_range() adjust the tlb->start and tlb->end,
  507. * and set corresponding cleared_*.
  508. */
  509. static inline void tlb_flush_pte_range(struct mmu_gather *tlb,
  510. unsigned long address, unsigned long size)
  511. {
  512. __tlb_adjust_range(tlb, address, size);
  513. tlb->cleared_ptes = 1;
  514. }
  515. static inline void tlb_flush_pmd_range(struct mmu_gather *tlb,
  516. unsigned long address, unsigned long size)
  517. {
  518. __tlb_adjust_range(tlb, address, size);
  519. tlb->cleared_pmds = 1;
  520. }
  521. static inline void tlb_flush_pud_range(struct mmu_gather *tlb,
  522. unsigned long address, unsigned long size)
  523. {
  524. __tlb_adjust_range(tlb, address, size);
  525. tlb->cleared_puds = 1;
  526. }
  527. static inline void tlb_flush_p4d_range(struct mmu_gather *tlb,
  528. unsigned long address, unsigned long size)
  529. {
  530. __tlb_adjust_range(tlb, address, size);
  531. tlb->cleared_p4ds = 1;
  532. }
  533. #ifndef __tlb_remove_tlb_entry
  534. static inline void __tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep, unsigned long address)
  535. {
  536. }
  537. #endif
  538. /**
  539. * tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
  540. *
  541. * Record the fact that pte's were really unmapped by updating the range,
  542. * so we can later optimise away the tlb invalidate. This helps when
  543. * userspace is unmapping already-unmapped pages, which happens quite a lot.
  544. */
  545. #define tlb_remove_tlb_entry(tlb, ptep, address) \
  546. do { \
  547. tlb_flush_pte_range(tlb, address, PAGE_SIZE); \
  548. __tlb_remove_tlb_entry(tlb, ptep, address); \
  549. } while (0)
  550. /**
  551. * tlb_remove_tlb_entries - remember unmapping of multiple consecutive ptes for
  552. * later tlb invalidation.
  553. *
  554. * Similar to tlb_remove_tlb_entry(), but remember unmapping of multiple
  555. * consecutive ptes instead of only a single one.
  556. */
  557. static inline void tlb_remove_tlb_entries(struct mmu_gather *tlb,
  558. pte_t *ptep, unsigned int nr, unsigned long address)
  559. {
  560. tlb_flush_pte_range(tlb, address, PAGE_SIZE * nr);
  561. for (;;) {
  562. __tlb_remove_tlb_entry(tlb, ptep, address);
  563. if (--nr == 0)
  564. break;
  565. ptep++;
  566. address += PAGE_SIZE;
  567. }
  568. }
  569. #define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
  570. do { \
  571. unsigned long _sz = huge_page_size(h); \
  572. if (_sz >= P4D_SIZE) \
  573. tlb_flush_p4d_range(tlb, address, _sz); \
  574. else if (_sz >= PUD_SIZE) \
  575. tlb_flush_pud_range(tlb, address, _sz); \
  576. else if (_sz >= PMD_SIZE) \
  577. tlb_flush_pmd_range(tlb, address, _sz); \
  578. else \
  579. tlb_flush_pte_range(tlb, address, _sz); \
  580. __tlb_remove_tlb_entry(tlb, ptep, address); \
  581. } while (0)
  582. /**
  583. * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
  584. * This is a nop so far, because only x86 needs it.
  585. */
  586. #ifndef __tlb_remove_pmd_tlb_entry
  587. #define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
  588. #endif
  589. #define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
  590. do { \
  591. tlb_flush_pmd_range(tlb, address, HPAGE_PMD_SIZE); \
  592. __tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
  593. } while (0)
  594. /**
  595. * tlb_remove_pud_tlb_entry - remember a pud mapping for later tlb
  596. * invalidation. This is a nop so far, because only x86 needs it.
  597. */
  598. #ifndef __tlb_remove_pud_tlb_entry
  599. #define __tlb_remove_pud_tlb_entry(tlb, pudp, address) do {} while (0)
  600. #endif
  601. #define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
  602. do { \
  603. tlb_flush_pud_range(tlb, address, HPAGE_PUD_SIZE); \
  604. __tlb_remove_pud_tlb_entry(tlb, pudp, address); \
  605. } while (0)
  606. /*
  607. * For things like page tables caches (ie caching addresses "inside" the
  608. * page tables, like x86 does), for legacy reasons, flushing an
  609. * individual page had better flush the page table caches behind it. This
  610. * is definitely how x86 works, for example. And if you have an
  611. * architected non-legacy page table cache (which I'm not aware of
  612. * anybody actually doing), you're going to have some architecturally
  613. * explicit flushing for that, likely *separate* from a regular TLB entry
  614. * flush, and thus you'd need more than just some range expansion..
  615. *
  616. * So if we ever find an architecture
  617. * that would want something that odd, I think it is up to that
  618. * architecture to do its own odd thing, not cause pain for others
  619. * http://lkml.kernel.org/r/CA+55aFzBggoXtNXQeng5d_mRoDnaMBE5Y+URs+PHR67nUpMtaw@mail.gmail.com
  620. *
  621. * For now w.r.t page table cache, mark the range_size as PAGE_SIZE
  622. */
  623. #ifndef pte_free_tlb
  624. #define pte_free_tlb(tlb, ptep, address) \
  625. do { \
  626. tlb_flush_pmd_range(tlb, address, PAGE_SIZE); \
  627. tlb->freed_tables = 1; \
  628. __pte_free_tlb(tlb, ptep, address); \
  629. } while (0)
  630. #endif
  631. #ifndef pmd_free_tlb
  632. #define pmd_free_tlb(tlb, pmdp, address) \
  633. do { \
  634. tlb_flush_pud_range(tlb, address, PAGE_SIZE); \
  635. tlb->freed_tables = 1; \
  636. __pmd_free_tlb(tlb, pmdp, address); \
  637. } while (0)
  638. #endif
  639. #ifndef pud_free_tlb
  640. #define pud_free_tlb(tlb, pudp, address) \
  641. do { \
  642. tlb_flush_p4d_range(tlb, address, PAGE_SIZE); \
  643. tlb->freed_tables = 1; \
  644. __pud_free_tlb(tlb, pudp, address); \
  645. } while (0)
  646. #endif
  647. #ifndef p4d_free_tlb
  648. #define p4d_free_tlb(tlb, pudp, address) \
  649. do { \
  650. __tlb_adjust_range(tlb, address, PAGE_SIZE); \
  651. tlb->freed_tables = 1; \
  652. __p4d_free_tlb(tlb, pudp, address); \
  653. } while (0)
  654. #endif
  655. #ifndef pte_needs_flush
  656. static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
  657. {
  658. return true;
  659. }
  660. #endif
  661. #ifndef huge_pmd_needs_flush
  662. static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
  663. {
  664. return true;
  665. }
  666. #endif
  667. #endif /* CONFIG_MMU */
  668. #endif /* _ASM_GENERIC__TLB_H */