020-v6.3-23-mm-multi-gen-LRU-remove-eviction-fairness-safeguard.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. From ce45f1c4b32cf69b166f56ef5bc6c761e06ed4e5 Mon Sep 17 00:00:00 2001
  2. From: Yu Zhao <[email protected]>
  3. Date: Wed, 21 Dec 2022 21:19:01 -0700
  4. Subject: [PATCH 23/29] mm: multi-gen LRU: remove eviction fairness safeguard
  5. Recall that the eviction consumes the oldest generation: first it
  6. bucket-sorts pages whose gen counters were updated by the aging and
  7. reclaims the rest; then it increments lrugen->min_seq.
  8. The current eviction fairness safeguard for global reclaim has a
  9. dilemma: when there are multiple eligible memcgs, should it continue
  10. or stop upon meeting the reclaim goal? If it continues, it overshoots
  11. and increases direct reclaim latency; if it stops, it loses fairness
  12. between memcgs it has taken memory away from and those it has yet to.
  13. With memcg LRU, the eviction, while ensuring eventual fairness, will
  14. stop upon meeting its goal. Therefore the current eviction fairness
  15. safeguard for global reclaim will not be needed.
  16. Note that memcg LRU only applies to global reclaim. For memcg reclaim,
  17. the eviction will continue, even if it is overshooting. This becomes
  18. unconditional due to code simplification.
  19. Link: https://lkml.kernel.org/r/[email protected]
  20. Signed-off-by: Yu Zhao <[email protected]>
  21. Cc: Johannes Weiner <[email protected]>
  22. Cc: Jonathan Corbet <[email protected]>
  23. Cc: Michael Larabel <[email protected]>
  24. Cc: Michal Hocko <[email protected]>
  25. Cc: Mike Rapoport <[email protected]>
  26. Cc: Roman Gushchin <[email protected]>
  27. Cc: Suren Baghdasaryan <[email protected]>
  28. Signed-off-by: Andrew Morton <[email protected]>
  29. ---
  30. mm/vmscan.c | 82 +++++++++++++++--------------------------------------
  31. 1 file changed, 23 insertions(+), 59 deletions(-)
  32. --- a/mm/vmscan.c
  33. +++ b/mm/vmscan.c
  34. @@ -443,6 +443,11 @@ static bool cgroup_reclaim(struct scan_c
  35. return sc->target_mem_cgroup;
  36. }
  37. +static bool global_reclaim(struct scan_control *sc)
  38. +{
  39. + return !sc->target_mem_cgroup || mem_cgroup_is_root(sc->target_mem_cgroup);
  40. +}
  41. +
  42. /**
  43. * writeback_throttling_sane - is the usual dirty throttling mechanism available?
  44. * @sc: scan_control in question
  45. @@ -493,6 +498,11 @@ static bool cgroup_reclaim(struct scan_c
  46. return false;
  47. }
  48. +static bool global_reclaim(struct scan_control *sc)
  49. +{
  50. + return true;
  51. +}
  52. +
  53. static bool writeback_throttling_sane(struct scan_control *sc)
  54. {
  55. return true;
  56. @@ -4722,8 +4732,7 @@ static int isolate_pages(struct lruvec *
  57. return scanned;
  58. }
  59. -static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swappiness,
  60. - bool *need_swapping)
  61. +static int evict_pages(struct lruvec *lruvec, struct scan_control *sc, int swappiness)
  62. {
  63. int type;
  64. int scanned;
  65. @@ -4812,9 +4821,6 @@ retry:
  66. goto retry;
  67. }
  68. - if (need_swapping && type == LRU_GEN_ANON)
  69. - *need_swapping = true;
  70. -
  71. return scanned;
  72. }
  73. @@ -4853,68 +4859,26 @@ done:
  74. return min_seq[!can_swap] + MIN_NR_GENS <= max_seq ? nr_to_scan : 0;
  75. }
  76. -static bool should_abort_scan(struct lruvec *lruvec, unsigned long seq,
  77. - struct scan_control *sc, bool need_swapping)
  78. +static unsigned long get_nr_to_reclaim(struct scan_control *sc)
  79. {
  80. - int i;
  81. - DEFINE_MAX_SEQ(lruvec);
  82. -
  83. - if (!current_is_kswapd()) {
  84. - /* age each memcg once to ensure fairness */
  85. - if (max_seq - seq > 1)
  86. - return true;
  87. -
  88. - /* over-swapping can increase allocation latency */
  89. - if (sc->nr_reclaimed >= sc->nr_to_reclaim && need_swapping)
  90. - return true;
  91. -
  92. - /* give this thread a chance to exit and free its memory */
  93. - if (fatal_signal_pending(current)) {
  94. - sc->nr_reclaimed += MIN_LRU_BATCH;
  95. - return true;
  96. - }
  97. -
  98. - if (cgroup_reclaim(sc))
  99. - return false;
  100. - } else if (sc->nr_reclaimed - sc->last_reclaimed < sc->nr_to_reclaim)
  101. - return false;
  102. -
  103. - /* keep scanning at low priorities to ensure fairness */
  104. - if (sc->priority > DEF_PRIORITY - 2)
  105. - return false;
  106. -
  107. - /*
  108. - * A minimum amount of work was done under global memory pressure. For
  109. - * kswapd, it may be overshooting. For direct reclaim, the target isn't
  110. - * met, and yet the allocation may still succeed, since kswapd may have
  111. - * caught up. In either case, it's better to stop now, and restart if
  112. - * necessary.
  113. - */
  114. - for (i = 0; i <= sc->reclaim_idx; i++) {
  115. - unsigned long wmark;
  116. - struct zone *zone = lruvec_pgdat(lruvec)->node_zones + i;
  117. -
  118. - if (!managed_zone(zone))
  119. - continue;
  120. -
  121. - wmark = current_is_kswapd() ? high_wmark_pages(zone) : low_wmark_pages(zone);
  122. - if (wmark > zone_page_state(zone, NR_FREE_PAGES))
  123. - return false;
  124. - }
  125. + /* don't abort memcg reclaim to ensure fairness */
  126. + if (!global_reclaim(sc))
  127. + return -1;
  128. - sc->nr_reclaimed += MIN_LRU_BATCH;
  129. + /* discount the previous progress for kswapd */
  130. + if (current_is_kswapd())
  131. + return sc->nr_to_reclaim + sc->last_reclaimed;
  132. - return true;
  133. + return max(sc->nr_to_reclaim, compact_gap(sc->order));
  134. }
  135. static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc)
  136. {
  137. struct blk_plug plug;
  138. bool need_aging = false;
  139. - bool need_swapping = false;
  140. unsigned long scanned = 0;
  141. unsigned long reclaimed = sc->nr_reclaimed;
  142. - DEFINE_MAX_SEQ(lruvec);
  143. + unsigned long nr_to_reclaim = get_nr_to_reclaim(sc);
  144. lru_add_drain();
  145. @@ -4938,7 +4902,7 @@ static void lru_gen_shrink_lruvec(struct
  146. if (!nr_to_scan)
  147. goto done;
  148. - delta = evict_pages(lruvec, sc, swappiness, &need_swapping);
  149. + delta = evict_pages(lruvec, sc, swappiness);
  150. if (!delta)
  151. goto done;
  152. @@ -4946,7 +4910,7 @@ static void lru_gen_shrink_lruvec(struct
  153. if (scanned >= nr_to_scan)
  154. break;
  155. - if (should_abort_scan(lruvec, max_seq, sc, need_swapping))
  156. + if (sc->nr_reclaimed >= nr_to_reclaim)
  157. break;
  158. cond_resched();
  159. @@ -5393,7 +5357,7 @@ static int run_eviction(struct lruvec *l
  160. if (sc->nr_reclaimed >= nr_to_reclaim)
  161. return 0;
  162. - if (!evict_pages(lruvec, sc, swappiness, NULL))
  163. + if (!evict_pages(lruvec, sc, swappiness))
  164. return 0;
  165. cond_resched();