mmu_gather.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include <linux/gfp.h>
  2. #include <linux/highmem.h>
  3. #include <linux/kernel.h>
  4. #include <linux/mmdebug.h>
  5. #include <linux/mm_types.h>
  6. #include <linux/mm_inline.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/rcupdate.h>
  9. #include <linux/smp.h>
  10. #include <linux/swap.h>
  11. #include <linux/rmap.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/tlb.h>
  14. #ifndef CONFIG_MMU_GATHER_NO_GATHER
  15. static bool tlb_next_batch(struct mmu_gather *tlb)
  16. {
  17. struct mmu_gather_batch *batch;
  18. /* Limit batching if we have delayed rmaps pending */
  19. if (tlb->delayed_rmap && tlb->active != &tlb->local)
  20. return false;
  21. batch = tlb->active;
  22. if (batch->next) {
  23. tlb->active = batch->next;
  24. return true;
  25. }
  26. if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
  27. return false;
  28. batch = (void *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  29. if (!batch)
  30. return false;
  31. tlb->batch_count++;
  32. batch->next = NULL;
  33. batch->nr = 0;
  34. batch->max = MAX_GATHER_BATCH;
  35. tlb->active->next = batch;
  36. tlb->active = batch;
  37. return true;
  38. }
  39. #ifdef CONFIG_SMP
  40. static void tlb_flush_rmap_batch(struct mmu_gather_batch *batch, struct vm_area_struct *vma)
  41. {
  42. struct encoded_page **pages = batch->encoded_pages;
  43. for (int i = 0; i < batch->nr; i++) {
  44. struct encoded_page *enc = pages[i];
  45. if (encoded_page_flags(enc) & ENCODED_PAGE_BIT_DELAY_RMAP) {
  46. struct page *page = encoded_page_ptr(enc);
  47. unsigned int nr_pages = 1;
  48. if (unlikely(encoded_page_flags(enc) &
  49. ENCODED_PAGE_BIT_NR_PAGES_NEXT))
  50. nr_pages = encoded_nr_pages(pages[++i]);
  51. folio_remove_rmap_ptes(page_folio(page), page, nr_pages,
  52. vma);
  53. }
  54. }
  55. }
  56. /**
  57. * tlb_flush_rmaps - do pending rmap removals after we have flushed the TLB
  58. * @tlb: the current mmu_gather
  59. * @vma: The memory area from which the pages are being removed.
  60. *
  61. * Note that because of how tlb_next_batch() above works, we will
  62. * never start multiple new batches with pending delayed rmaps, so
  63. * we only need to walk through the current active batch and the
  64. * original local one.
  65. */
  66. void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma)
  67. {
  68. if (!tlb->delayed_rmap)
  69. return;
  70. tlb_flush_rmap_batch(&tlb->local, vma);
  71. if (tlb->active != &tlb->local)
  72. tlb_flush_rmap_batch(tlb->active, vma);
  73. tlb->delayed_rmap = 0;
  74. }
  75. #endif
  76. /*
  77. * We might end up freeing a lot of pages. Reschedule on a regular
  78. * basis to avoid soft lockups in configurations without full
  79. * preemption enabled. The magic number of 512 folios seems to work.
  80. */
  81. #define MAX_NR_FOLIOS_PER_FREE 512
  82. static void __tlb_batch_free_encoded_pages(struct mmu_gather_batch *batch)
  83. {
  84. struct encoded_page **pages = batch->encoded_pages;
  85. unsigned int nr, nr_pages;
  86. while (batch->nr) {
  87. if (!page_poisoning_enabled_static() && !want_init_on_free()) {
  88. nr = min(MAX_NR_FOLIOS_PER_FREE, batch->nr);
  89. /*
  90. * Make sure we cover page + nr_pages, and don't leave
  91. * nr_pages behind when capping the number of entries.
  92. */
  93. if (unlikely(encoded_page_flags(pages[nr - 1]) &
  94. ENCODED_PAGE_BIT_NR_PAGES_NEXT))
  95. nr++;
  96. } else {
  97. /*
  98. * With page poisoning and init_on_free, the time it
  99. * takes to free memory grows proportionally with the
  100. * actual memory size. Therefore, limit based on the
  101. * actual memory size and not the number of involved
  102. * folios.
  103. */
  104. for (nr = 0, nr_pages = 0;
  105. nr < batch->nr && nr_pages < MAX_NR_FOLIOS_PER_FREE;
  106. nr++) {
  107. if (unlikely(encoded_page_flags(pages[nr]) &
  108. ENCODED_PAGE_BIT_NR_PAGES_NEXT))
  109. nr_pages += encoded_nr_pages(pages[++nr]);
  110. else
  111. nr_pages++;
  112. }
  113. }
  114. free_pages_and_swap_cache(pages, nr);
  115. pages += nr;
  116. batch->nr -= nr;
  117. cond_resched();
  118. }
  119. }
  120. static void tlb_batch_pages_flush(struct mmu_gather *tlb)
  121. {
  122. struct mmu_gather_batch *batch;
  123. for (batch = &tlb->local; batch && batch->nr; batch = batch->next)
  124. __tlb_batch_free_encoded_pages(batch);
  125. tlb->active = &tlb->local;
  126. }
  127. static void tlb_batch_list_free(struct mmu_gather *tlb)
  128. {
  129. struct mmu_gather_batch *batch, *next;
  130. for (batch = tlb->local.next; batch; batch = next) {
  131. next = batch->next;
  132. free_pages((unsigned long)batch, 0);
  133. }
  134. tlb->local.next = NULL;
  135. }
  136. static bool __tlb_remove_folio_pages_size(struct mmu_gather *tlb,
  137. struct page *page, unsigned int nr_pages, bool delay_rmap,
  138. int page_size)
  139. {
  140. int flags = delay_rmap ? ENCODED_PAGE_BIT_DELAY_RMAP : 0;
  141. struct mmu_gather_batch *batch;
  142. VM_BUG_ON(!tlb->end);
  143. #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
  144. VM_WARN_ON(tlb->page_size != page_size);
  145. VM_WARN_ON_ONCE(nr_pages != 1 && page_size != PAGE_SIZE);
  146. VM_WARN_ON_ONCE(page_folio(page) != page_folio(page + nr_pages - 1));
  147. #endif
  148. batch = tlb->active;
  149. /*
  150. * Add the page and check if we are full. If so
  151. * force a flush.
  152. */
  153. if (likely(nr_pages == 1)) {
  154. batch->encoded_pages[batch->nr++] = encode_page(page, flags);
  155. } else {
  156. flags |= ENCODED_PAGE_BIT_NR_PAGES_NEXT;
  157. batch->encoded_pages[batch->nr++] = encode_page(page, flags);
  158. batch->encoded_pages[batch->nr++] = encode_nr_pages(nr_pages);
  159. }
  160. /*
  161. * Make sure that we can always add another "page" + "nr_pages",
  162. * requiring two entries instead of only a single one.
  163. */
  164. if (batch->nr >= batch->max - 1) {
  165. if (!tlb_next_batch(tlb))
  166. return true;
  167. batch = tlb->active;
  168. }
  169. VM_BUG_ON_PAGE(batch->nr > batch->max - 1, page);
  170. return false;
  171. }
  172. bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
  173. unsigned int nr_pages, bool delay_rmap)
  174. {
  175. return __tlb_remove_folio_pages_size(tlb, page, nr_pages, delay_rmap,
  176. PAGE_SIZE);
  177. }
  178. bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page,
  179. bool delay_rmap, int page_size)
  180. {
  181. return __tlb_remove_folio_pages_size(tlb, page, 1, delay_rmap, page_size);
  182. }
  183. #endif /* MMU_GATHER_NO_GATHER */
  184. #ifdef CONFIG_MMU_GATHER_TABLE_FREE
  185. static void __tlb_remove_table_free(struct mmu_table_batch *batch)
  186. {
  187. int i;
  188. for (i = 0; i < batch->nr; i++)
  189. __tlb_remove_table(batch->tables[i]);
  190. free_page((unsigned long)batch);
  191. }
  192. #ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE
  193. /*
  194. * Semi RCU freeing of the page directories.
  195. *
  196. * This is needed by some architectures to implement software pagetable walkers.
  197. *
  198. * gup_fast() and other software pagetable walkers do a lockless page-table
  199. * walk and therefore needs some synchronization with the freeing of the page
  200. * directories. The chosen means to accomplish that is by disabling IRQs over
  201. * the walk.
  202. *
  203. * Architectures that use IPIs to flush TLBs will then automagically DTRT,
  204. * since we unlink the page, flush TLBs, free the page. Since the disabling of
  205. * IRQs delays the completion of the TLB flush we can never observe an already
  206. * freed page.
  207. *
  208. * Architectures that do not have this (PPC) need to delay the freeing by some
  209. * other means, this is that means.
  210. *
  211. * What we do is batch the freed directory pages (tables) and RCU free them.
  212. * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
  213. * holds off grace periods.
  214. *
  215. * However, in order to batch these pages we need to allocate storage, this
  216. * allocation is deep inside the MM code and can thus easily fail on memory
  217. * pressure. To guarantee progress we fall back to single table freeing, see
  218. * the implementation of tlb_remove_table_one().
  219. *
  220. */
  221. static void tlb_remove_table_smp_sync(void *arg)
  222. {
  223. /* Simply deliver the interrupt */
  224. }
  225. void tlb_remove_table_sync_one(void)
  226. {
  227. /*
  228. * This isn't an RCU grace period and hence the page-tables cannot be
  229. * assumed to be actually RCU-freed.
  230. *
  231. * It is however sufficient for software page-table walkers that rely on
  232. * IRQ disabling.
  233. */
  234. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  235. }
  236. static void tlb_remove_table_rcu(struct rcu_head *head)
  237. {
  238. __tlb_remove_table_free(container_of(head, struct mmu_table_batch, rcu));
  239. }
  240. static void tlb_remove_table_free(struct mmu_table_batch *batch)
  241. {
  242. call_rcu(&batch->rcu, tlb_remove_table_rcu);
  243. }
  244. #else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */
  245. static void tlb_remove_table_free(struct mmu_table_batch *batch)
  246. {
  247. __tlb_remove_table_free(batch);
  248. }
  249. #endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */
  250. /*
  251. * If we want tlb_remove_table() to imply TLB invalidates.
  252. */
  253. static inline void tlb_table_invalidate(struct mmu_gather *tlb)
  254. {
  255. if (tlb_needs_table_invalidate()) {
  256. /*
  257. * Invalidate page-table caches used by hardware walkers. Then
  258. * we still need to RCU-sched wait while freeing the pages
  259. * because software walkers can still be in-flight.
  260. */
  261. tlb_flush_mmu_tlbonly(tlb);
  262. }
  263. }
  264. static void tlb_remove_table_one(void *table)
  265. {
  266. tlb_remove_table_sync_one();
  267. __tlb_remove_table(table);
  268. }
  269. static void tlb_table_flush(struct mmu_gather *tlb)
  270. {
  271. struct mmu_table_batch **batch = &tlb->batch;
  272. if (*batch) {
  273. tlb_table_invalidate(tlb);
  274. tlb_remove_table_free(*batch);
  275. *batch = NULL;
  276. }
  277. }
  278. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  279. {
  280. struct mmu_table_batch **batch = &tlb->batch;
  281. if (*batch == NULL) {
  282. *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  283. if (*batch == NULL) {
  284. tlb_table_invalidate(tlb);
  285. tlb_remove_table_one(table);
  286. return;
  287. }
  288. (*batch)->nr = 0;
  289. }
  290. (*batch)->tables[(*batch)->nr++] = table;
  291. if ((*batch)->nr == MAX_TABLE_BATCH)
  292. tlb_table_flush(tlb);
  293. }
  294. static inline void tlb_table_init(struct mmu_gather *tlb)
  295. {
  296. tlb->batch = NULL;
  297. }
  298. #else /* !CONFIG_MMU_GATHER_TABLE_FREE */
  299. static inline void tlb_table_flush(struct mmu_gather *tlb) { }
  300. static inline void tlb_table_init(struct mmu_gather *tlb) { }
  301. #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
  302. static void tlb_flush_mmu_free(struct mmu_gather *tlb)
  303. {
  304. tlb_table_flush(tlb);
  305. #ifndef CONFIG_MMU_GATHER_NO_GATHER
  306. tlb_batch_pages_flush(tlb);
  307. #endif
  308. }
  309. void tlb_flush_mmu(struct mmu_gather *tlb)
  310. {
  311. tlb_flush_mmu_tlbonly(tlb);
  312. tlb_flush_mmu_free(tlb);
  313. }
  314. static void __tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm,
  315. bool fullmm)
  316. {
  317. tlb->mm = mm;
  318. tlb->fullmm = fullmm;
  319. #ifndef CONFIG_MMU_GATHER_NO_GATHER
  320. tlb->need_flush_all = 0;
  321. tlb->local.next = NULL;
  322. tlb->local.nr = 0;
  323. tlb->local.max = ARRAY_SIZE(tlb->__pages);
  324. tlb->active = &tlb->local;
  325. tlb->batch_count = 0;
  326. #endif
  327. tlb->delayed_rmap = 0;
  328. tlb_table_init(tlb);
  329. #ifdef CONFIG_MMU_GATHER_PAGE_SIZE
  330. tlb->page_size = 0;
  331. #endif
  332. __tlb_reset_range(tlb);
  333. inc_tlb_flush_pending(tlb->mm);
  334. }
  335. /**
  336. * tlb_gather_mmu - initialize an mmu_gather structure for page-table tear-down
  337. * @tlb: the mmu_gather structure to initialize
  338. * @mm: the mm_struct of the target address space
  339. *
  340. * Called to initialize an (on-stack) mmu_gather structure for page-table
  341. * tear-down from @mm.
  342. */
  343. void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm)
  344. {
  345. __tlb_gather_mmu(tlb, mm, false);
  346. }
  347. /**
  348. * tlb_gather_mmu_fullmm - initialize an mmu_gather structure for page-table tear-down
  349. * @tlb: the mmu_gather structure to initialize
  350. * @mm: the mm_struct of the target address space
  351. *
  352. * In this case, @mm is without users and we're going to destroy the
  353. * full address space (exit/execve).
  354. *
  355. * Called to initialize an (on-stack) mmu_gather structure for page-table
  356. * tear-down from @mm.
  357. */
  358. void tlb_gather_mmu_fullmm(struct mmu_gather *tlb, struct mm_struct *mm)
  359. {
  360. __tlb_gather_mmu(tlb, mm, true);
  361. }
  362. /**
  363. * tlb_finish_mmu - finish an mmu_gather structure
  364. * @tlb: the mmu_gather structure to finish
  365. *
  366. * Called at the end of the shootdown operation to free up any resources that
  367. * were required.
  368. */
  369. void tlb_finish_mmu(struct mmu_gather *tlb)
  370. {
  371. /*
  372. * If there are parallel threads are doing PTE changes on same range
  373. * under non-exclusive lock (e.g., mmap_lock read-side) but defer TLB
  374. * flush by batching, one thread may end up seeing inconsistent PTEs
  375. * and result in having stale TLB entries. So flush TLB forcefully
  376. * if we detect parallel PTE batching threads.
  377. *
  378. * However, some syscalls, e.g. munmap(), may free page tables, this
  379. * needs force flush everything in the given range. Otherwise this
  380. * may result in having stale TLB entries for some architectures,
  381. * e.g. aarch64, that could specify flush what level TLB.
  382. */
  383. if (mm_tlb_flush_nested(tlb->mm)) {
  384. /*
  385. * The aarch64 yields better performance with fullmm by
  386. * avoiding multiple CPUs spamming TLBI messages at the
  387. * same time.
  388. *
  389. * On x86 non-fullmm doesn't yield significant difference
  390. * against fullmm.
  391. */
  392. tlb->fullmm = 1;
  393. __tlb_reset_range(tlb);
  394. tlb->freed_tables = 1;
  395. }
  396. tlb_flush_mmu(tlb);
  397. #ifndef CONFIG_MMU_GATHER_NO_GATHER
  398. tlb_batch_list_free(tlb);
  399. #endif
  400. dec_tlb_flush_pending(tlb->mm);
  401. }