020-v6.1-07-mm-multi-gen-LRU-exploit-locality-in-rmap.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. From e4277535f6d6708bb19b88c4bad155832671d69b Mon Sep 17 00:00:00 2001
  2. From: Yu Zhao <[email protected]>
  3. Date: Sun, 18 Sep 2022 02:00:04 -0600
  4. Subject: [PATCH 07/29] mm: multi-gen LRU: exploit locality in rmap
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Searching the rmap for PTEs mapping each page on an LRU list (to test and
  9. clear the accessed bit) can be expensive because pages from different VMAs
  10. (PA space) are not cache friendly to the rmap (VA space). For workloads
  11. mostly using mapped pages, searching the rmap can incur the highest CPU
  12. cost in the reclaim path.
  13. This patch exploits spatial locality to reduce the trips into the rmap.
  14. When shrink_page_list() walks the rmap and finds a young PTE, a new
  15. function lru_gen_look_around() scans at most BITS_PER_LONG-1 adjacent
  16. PTEs. On finding another young PTE, it clears the accessed bit and
  17. updates the gen counter of the page mapped by this PTE to
  18. (max_seq%MAX_NR_GENS)+1.
  19. Server benchmark results:
  20. Single workload:
  21. fio (buffered I/O): no change
  22. Single workload:
  23. memcached (anon): +[3, 5]%
  24. Ops/sec KB/sec
  25. patch1-6: 1106168.46 43025.04
  26. patch1-7: 1147696.57 44640.29
  27. Configurations:
  28. no change
  29. Client benchmark results:
  30. kswapd profiles:
  31. patch1-6
  32. 39.03% lzo1x_1_do_compress (real work)
  33. 18.47% page_vma_mapped_walk (overhead)
  34. 6.74% _raw_spin_unlock_irq
  35. 3.97% do_raw_spin_lock
  36. 2.49% ptep_clear_flush
  37. 2.48% anon_vma_interval_tree_iter_first
  38. 1.92% page_referenced_one
  39. 1.88% __zram_bvec_write
  40. 1.48% memmove
  41. 1.31% vma_interval_tree_iter_next
  42. patch1-7
  43. 48.16% lzo1x_1_do_compress (real work)
  44. 8.20% page_vma_mapped_walk (overhead)
  45. 7.06% _raw_spin_unlock_irq
  46. 2.92% ptep_clear_flush
  47. 2.53% __zram_bvec_write
  48. 2.11% do_raw_spin_lock
  49. 2.02% memmove
  50. 1.93% lru_gen_look_around
  51. 1.56% free_unref_page_list
  52. 1.40% memset
  53. Configurations:
  54. no change
  55. Link: https://lkml.kernel.org/r/[email protected]
  56. Signed-off-by: Yu Zhao <[email protected]>
  57. Acked-by: Barry Song <[email protected]>
  58. Acked-by: Brian Geffon <[email protected]>
  59. Acked-by: Jan Alexander Steffens (heftig) <[email protected]>
  60. Acked-by: Oleksandr Natalenko <[email protected]>
  61. Acked-by: Steven Barrett <[email protected]>
  62. Acked-by: Suleiman Souhlal <[email protected]>
  63. Tested-by: Daniel Byrne <[email protected]>
  64. Tested-by: Donald Carr <[email protected]>
  65. Tested-by: Holger Hoffstätte <[email protected]>
  66. Tested-by: Konstantin Kharlamov <[email protected]>
  67. Tested-by: Shuang Zhai <[email protected]>
  68. Tested-by: Sofia Trinh <[email protected]>
  69. Tested-by: Vaibhav Jain <[email protected]>
  70. Cc: Andi Kleen <[email protected]>
  71. Cc: Aneesh Kumar K.V <[email protected]>
  72. Cc: Catalin Marinas <[email protected]>
  73. Cc: Dave Hansen <[email protected]>
  74. Cc: Hillf Danton <[email protected]>
  75. Cc: Jens Axboe <[email protected]>
  76. Cc: Johannes Weiner <[email protected]>
  77. Cc: Jonathan Corbet <[email protected]>
  78. Cc: Linus Torvalds <[email protected]>
  79. Cc: Matthew Wilcox <[email protected]>
  80. Cc: Mel Gorman <[email protected]>
  81. Cc: Miaohe Lin <[email protected]>
  82. Cc: Michael Larabel <[email protected]>
  83. Cc: Michal Hocko <[email protected]>
  84. Cc: Mike Rapoport <[email protected]>
  85. Cc: Mike Rapoport <[email protected]>
  86. Cc: Peter Zijlstra <[email protected]>
  87. Cc: Qi Zheng <[email protected]>
  88. Cc: Tejun Heo <[email protected]>
  89. Cc: Vlastimil Babka <[email protected]>
  90. Cc: Will Deacon <[email protected]>
  91. Signed-off-by: Andrew Morton <[email protected]>
  92. ---
  93. include/linux/memcontrol.h | 31 +++++++
  94. include/linux/mmzone.h | 6 ++
  95. mm/internal.h | 1 +
  96. mm/memcontrol.c | 1 +
  97. mm/rmap.c | 7 ++
  98. mm/swap.c | 4 +-
  99. mm/vmscan.c | 184 +++++++++++++++++++++++++++++++++++++
  100. 7 files changed, 232 insertions(+), 2 deletions(-)
  101. --- a/include/linux/memcontrol.h
  102. +++ b/include/linux/memcontrol.h
  103. @@ -447,6 +447,7 @@ static inline struct obj_cgroup *__page_
  104. * - LRU isolation
  105. * - lock_page_memcg()
  106. * - exclusive reference
  107. + * - mem_cgroup_trylock_pages()
  108. *
  109. * For a kmem page a caller should hold an rcu read lock to protect memcg
  110. * associated with a kmem page from being released.
  111. @@ -502,6 +503,7 @@ static inline struct mem_cgroup *page_me
  112. * - LRU isolation
  113. * - lock_page_memcg()
  114. * - exclusive reference
  115. + * - mem_cgroup_trylock_pages()
  116. *
  117. * For a kmem page a caller should hold an rcu read lock to protect memcg
  118. * associated with a kmem page from being released.
  119. @@ -958,6 +960,23 @@ void unlock_page_memcg(struct page *page
  120. void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
  121. +/* try to stablize page_memcg() for all the pages in a memcg */
  122. +static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
  123. +{
  124. + rcu_read_lock();
  125. +
  126. + if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account))
  127. + return true;
  128. +
  129. + rcu_read_unlock();
  130. + return false;
  131. +}
  132. +
  133. +static inline void mem_cgroup_unlock_pages(void)
  134. +{
  135. + rcu_read_unlock();
  136. +}
  137. +
  138. /* idx can be of type enum memcg_stat_item or node_stat_item */
  139. static inline void mod_memcg_state(struct mem_cgroup *memcg,
  140. int idx, int val)
  141. @@ -1374,6 +1393,18 @@ static inline void unlock_page_memcg(str
  142. {
  143. }
  144. +static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg)
  145. +{
  146. + /* to match page_memcg_rcu() */
  147. + rcu_read_lock();
  148. + return true;
  149. +}
  150. +
  151. +static inline void mem_cgroup_unlock_pages(void)
  152. +{
  153. + rcu_read_unlock();
  154. +}
  155. +
  156. static inline void mem_cgroup_handle_over_high(void)
  157. {
  158. }
  159. --- a/include/linux/mmzone.h
  160. +++ b/include/linux/mmzone.h
  161. @@ -352,6 +352,7 @@ enum lruvec_flags {
  162. #ifndef __GENERATING_BOUNDS_H
  163. struct lruvec;
  164. +struct page_vma_mapped_walk;
  165. #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF)
  166. #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF)
  167. @@ -407,6 +408,7 @@ struct lru_gen_struct {
  168. };
  169. void lru_gen_init_lruvec(struct lruvec *lruvec);
  170. +void lru_gen_look_around(struct page_vma_mapped_walk *pvmw);
  171. #ifdef CONFIG_MEMCG
  172. void lru_gen_init_memcg(struct mem_cgroup *memcg);
  173. @@ -419,6 +421,10 @@ static inline void lru_gen_init_lruvec(s
  174. {
  175. }
  176. +static inline void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
  177. +{
  178. +}
  179. +
  180. #ifdef CONFIG_MEMCG
  181. static inline void lru_gen_init_memcg(struct mem_cgroup *memcg)
  182. {
  183. --- a/mm/internal.h
  184. +++ b/mm/internal.h
  185. @@ -35,6 +35,7 @@
  186. void page_writeback_init(void);
  187. vm_fault_t do_swap_page(struct vm_fault *vmf);
  188. +void activate_page(struct page *page);
  189. void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
  190. unsigned long floor, unsigned long ceiling);
  191. --- a/mm/memcontrol.c
  192. +++ b/mm/memcontrol.c
  193. @@ -2798,6 +2798,7 @@ static void commit_charge(struct page *p
  194. * - LRU isolation
  195. * - lock_page_memcg()
  196. * - exclusive reference
  197. + * - mem_cgroup_trylock_pages()
  198. */
  199. page->memcg_data = (unsigned long)memcg;
  200. }
  201. --- a/mm/rmap.c
  202. +++ b/mm/rmap.c
  203. @@ -73,6 +73,7 @@
  204. #include <linux/page_idle.h>
  205. #include <linux/memremap.h>
  206. #include <linux/userfaultfd_k.h>
  207. +#include <linux/mm_inline.h>
  208. #include <asm/tlbflush.h>
  209. @@ -793,6 +794,12 @@ static bool page_referenced_one(struct p
  210. }
  211. if (pvmw.pte) {
  212. + if (lru_gen_enabled() && pte_young(*pvmw.pte) &&
  213. + !(vma->vm_flags & (VM_SEQ_READ | VM_RAND_READ))) {
  214. + lru_gen_look_around(&pvmw);
  215. + referenced++;
  216. + }
  217. +
  218. if (ptep_clear_flush_young_notify(vma, address,
  219. pvmw.pte)) {
  220. /*
  221. --- a/mm/swap.c
  222. +++ b/mm/swap.c
  223. @@ -325,7 +325,7 @@ static bool need_activate_page_drain(int
  224. return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0;
  225. }
  226. -static void activate_page(struct page *page)
  227. +void activate_page(struct page *page)
  228. {
  229. page = compound_head(page);
  230. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  231. @@ -345,7 +345,7 @@ static inline void activate_page_drain(i
  232. {
  233. }
  234. -static void activate_page(struct page *page)
  235. +void activate_page(struct page *page)
  236. {
  237. struct lruvec *lruvec;
  238. --- a/mm/vmscan.c
  239. +++ b/mm/vmscan.c
  240. @@ -1409,6 +1409,11 @@ retry:
  241. if (!sc->may_unmap && page_mapped(page))
  242. goto keep_locked;
  243. + /* page_update_gen() tried to promote this page? */
  244. + if (lru_gen_enabled() && !ignore_references &&
  245. + page_mapped(page) && PageReferenced(page))
  246. + goto keep_locked;
  247. +
  248. may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  249. (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
  250. @@ -2990,6 +2995,29 @@ static bool positive_ctrl_err(struct ctr
  251. * the aging
  252. ******************************************************************************/
  253. +/* promote pages accessed through page tables */
  254. +static int page_update_gen(struct page *page, int gen)
  255. +{
  256. + unsigned long new_flags, old_flags = READ_ONCE(page->flags);
  257. +
  258. + VM_WARN_ON_ONCE(gen >= MAX_NR_GENS);
  259. + VM_WARN_ON_ONCE(!rcu_read_lock_held());
  260. +
  261. + do {
  262. + /* lru_gen_del_page() has isolated this page? */
  263. + if (!(old_flags & LRU_GEN_MASK)) {
  264. + /* for shrink_page_list() */
  265. + new_flags = old_flags | BIT(PG_referenced);
  266. + continue;
  267. + }
  268. +
  269. + new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
  270. + new_flags |= (gen + 1UL) << LRU_GEN_PGOFF;
  271. + } while (!try_cmpxchg(&page->flags, &old_flags, new_flags));
  272. +
  273. + return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
  274. +}
  275. +
  276. /* protect pages accessed multiple times through file descriptors */
  277. static int page_inc_gen(struct lruvec *lruvec, struct page *page, bool reclaiming)
  278. {
  279. @@ -3001,6 +3029,11 @@ static int page_inc_gen(struct lruvec *l
  280. VM_WARN_ON_ONCE_PAGE(!(old_flags & LRU_GEN_MASK), page);
  281. do {
  282. + new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
  283. + /* page_update_gen() has promoted this page? */
  284. + if (new_gen >= 0 && new_gen != old_gen)
  285. + return new_gen;
  286. +
  287. new_gen = (old_gen + 1) % MAX_NR_GENS;
  288. new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS);
  289. @@ -3015,6 +3048,43 @@ static int page_inc_gen(struct lruvec *l
  290. return new_gen;
  291. }
  292. +static unsigned long get_pte_pfn(pte_t pte, struct vm_area_struct *vma, unsigned long addr)
  293. +{
  294. + unsigned long pfn = pte_pfn(pte);
  295. +
  296. + VM_WARN_ON_ONCE(addr < vma->vm_start || addr >= vma->vm_end);
  297. +
  298. + if (!pte_present(pte) || is_zero_pfn(pfn))
  299. + return -1;
  300. +
  301. + if (WARN_ON_ONCE(pte_devmap(pte) || pte_special(pte)))
  302. + return -1;
  303. +
  304. + if (WARN_ON_ONCE(!pfn_valid(pfn)))
  305. + return -1;
  306. +
  307. + return pfn;
  308. +}
  309. +
  310. +static struct page *get_pfn_page(unsigned long pfn, struct mem_cgroup *memcg,
  311. + struct pglist_data *pgdat)
  312. +{
  313. + struct page *page;
  314. +
  315. + /* try to avoid unnecessary memory loads */
  316. + if (pfn < pgdat->node_start_pfn || pfn >= pgdat_end_pfn(pgdat))
  317. + return NULL;
  318. +
  319. + page = compound_head(pfn_to_page(pfn));
  320. + if (page_to_nid(page) != pgdat->node_id)
  321. + return NULL;
  322. +
  323. + if (page_memcg_rcu(page) != memcg)
  324. + return NULL;
  325. +
  326. + return page;
  327. +}
  328. +
  329. static void inc_min_seq(struct lruvec *lruvec, int type)
  330. {
  331. struct lru_gen_struct *lrugen = &lruvec->lrugen;
  332. @@ -3214,6 +3284,114 @@ static void lru_gen_age_node(struct pgli
  333. } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)));
  334. }
  335. +/*
  336. + * This function exploits spatial locality when shrink_page_list() walks the
  337. + * rmap. It scans the adjacent PTEs of a young PTE and promotes hot pages.
  338. + */
  339. +void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
  340. +{
  341. + int i;
  342. + pte_t *pte;
  343. + unsigned long start;
  344. + unsigned long end;
  345. + unsigned long addr;
  346. + unsigned long bitmap[BITS_TO_LONGS(MIN_LRU_BATCH)] = {};
  347. + struct page *page = pvmw->page;
  348. + struct mem_cgroup *memcg = page_memcg(page);
  349. + struct pglist_data *pgdat = page_pgdat(page);
  350. + struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
  351. + DEFINE_MAX_SEQ(lruvec);
  352. + int old_gen, new_gen = lru_gen_from_seq(max_seq);
  353. +
  354. + lockdep_assert_held(pvmw->ptl);
  355. + VM_WARN_ON_ONCE_PAGE(PageLRU(page), page);
  356. +
  357. + if (spin_is_contended(pvmw->ptl))
  358. + return;
  359. +
  360. + start = max(pvmw->address & PMD_MASK, pvmw->vma->vm_start);
  361. + end = min(pvmw->address | ~PMD_MASK, pvmw->vma->vm_end - 1) + 1;
  362. +
  363. + if (end - start > MIN_LRU_BATCH * PAGE_SIZE) {
  364. + if (pvmw->address - start < MIN_LRU_BATCH * PAGE_SIZE / 2)
  365. + end = start + MIN_LRU_BATCH * PAGE_SIZE;
  366. + else if (end - pvmw->address < MIN_LRU_BATCH * PAGE_SIZE / 2)
  367. + start = end - MIN_LRU_BATCH * PAGE_SIZE;
  368. + else {
  369. + start = pvmw->address - MIN_LRU_BATCH * PAGE_SIZE / 2;
  370. + end = pvmw->address + MIN_LRU_BATCH * PAGE_SIZE / 2;
  371. + }
  372. + }
  373. +
  374. + pte = pvmw->pte - (pvmw->address - start) / PAGE_SIZE;
  375. +
  376. + rcu_read_lock();
  377. + arch_enter_lazy_mmu_mode();
  378. +
  379. + for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) {
  380. + unsigned long pfn;
  381. +
  382. + pfn = get_pte_pfn(pte[i], pvmw->vma, addr);
  383. + if (pfn == -1)
  384. + continue;
  385. +
  386. + if (!pte_young(pte[i]))
  387. + continue;
  388. +
  389. + page = get_pfn_page(pfn, memcg, pgdat);
  390. + if (!page)
  391. + continue;
  392. +
  393. + if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i))
  394. + VM_WARN_ON_ONCE(true);
  395. +
  396. + if (pte_dirty(pte[i]) && !PageDirty(page) &&
  397. + !(PageAnon(page) && PageSwapBacked(page) &&
  398. + !PageSwapCache(page)))
  399. + set_page_dirty(page);
  400. +
  401. + old_gen = page_lru_gen(page);
  402. + if (old_gen < 0)
  403. + SetPageReferenced(page);
  404. + else if (old_gen != new_gen)
  405. + __set_bit(i, bitmap);
  406. + }
  407. +
  408. + arch_leave_lazy_mmu_mode();
  409. + rcu_read_unlock();
  410. +
  411. + if (bitmap_weight(bitmap, MIN_LRU_BATCH) < PAGEVEC_SIZE) {
  412. + for_each_set_bit(i, bitmap, MIN_LRU_BATCH) {
  413. + page = pte_page(pte[i]);
  414. + activate_page(page);
  415. + }
  416. + return;
  417. + }
  418. +
  419. + /* page_update_gen() requires stable page_memcg() */
  420. + if (!mem_cgroup_trylock_pages(memcg))
  421. + return;
  422. +
  423. + spin_lock_irq(&lruvec->lru_lock);
  424. + new_gen = lru_gen_from_seq(lruvec->lrugen.max_seq);
  425. +
  426. + for_each_set_bit(i, bitmap, MIN_LRU_BATCH) {
  427. + page = compound_head(pte_page(pte[i]));
  428. + if (page_memcg_rcu(page) != memcg)
  429. + continue;
  430. +
  431. + old_gen = page_update_gen(page, new_gen);
  432. + if (old_gen < 0 || old_gen == new_gen)
  433. + continue;
  434. +
  435. + lru_gen_update_size(lruvec, page, old_gen, new_gen);
  436. + }
  437. +
  438. + spin_unlock_irq(&lruvec->lru_lock);
  439. +
  440. + mem_cgroup_unlock_pages();
  441. +}
  442. +
  443. /******************************************************************************
  444. * the eviction
  445. ******************************************************************************/
  446. @@ -3250,6 +3428,12 @@ static bool sort_page(struct lruvec *lru
  447. return true;
  448. }
  449. + /* promoted */
  450. + if (gen != lru_gen_from_seq(lrugen->min_seq[type])) {
  451. + list_move(&page->lru, &lrugen->lists[gen][type][zone]);
  452. + return true;
  453. + }
  454. +
  455. /* protected */
  456. if (tier > tier_idx) {
  457. int hist = lru_hist_from_seq(lrugen->min_seq[type]);