mm/mglru: advance memcg generation when reclaimers pile up
Subsystems (4)
RFC PATCH 1/1 mm/mglru: advance memcg generation when reclaimers pile up
From: Ridong Chen <[email protected]>
On Android, memcgs are used to group and account memory rather than to limit it, so direct reclaim never hits a memcg limit and is always global reclaim through shrink_many(). We observed long direct reclaim latency on Android devices under memory pressure, when many tasks enter direct reclaim at the same time and all of them walk the same per-node memcg LRU. Some of the slow calls piled up on a few memcgs.
shrink_many() picks a random bin in the old memcg generation and moves on to the next bin if it is empty. When only a few old bins still hold memcgs, all reclaimers end up on those bins and pile up on the same memcgs, which makes direct reclaim take much longer.
Count the reclaimers in shrink_many() per node. Before walking a bin, advance the memcg generation when there are more reclaimers than non-empty old bins and advancing would add bins: splice each old bin in front of the young bin with the same index, so in each bin the remaining old memcgs are still reclaimed first in the same order, and bump the generation counter. The condition is checked again under memcg_lru->lock before splicing. The per-memcg segment is kept, so a memcg already moved to the tail segment (below memory.low or too small) still gets its second visit before the young memcgs in its bin.
shrink_many() now reads the generation again at each restart instead of once at entry, so reclaimers in the loop follow the new old generation instead of walking drained bins. A walker can also go more than one generation stale while it is in shrink_one(), and two advances would wrap its stale generation around to the young one. Skip the rest of the walk once the generation no longer matches the counter.
Advancing earlier means memcgs that were just rotated to the young generation become eligible for reclaim sooner than before, when the old generation had to drain first, and the order is only kept within each bin. The splice walks the remaining old memcgs under memcg_lru->lock with interrupts off; it only runs when some old bin is empty while the young bin with the same index is not.
The pattern can be reproduced on x86. Test on an x86_64 machine (i7-13700HX, 24 CPUs, zram swap): 64 memcgs each churning 24MiB of anon memory, 16 threads writing 128K to the root memory.reclaim in a loop, which goes through shrink_many(). Each run is 120s; the patch is turned off (A) and on (B) at runtime with a debug knob that is not part of this patch, alternating A B A B A B in the same boot.
Latency of each memory.reclaim write, in ms, averaged over the three runs:
A B change
p50 1.95 2.08 +6%
p95 4.88 4.29 -12%
p99 8.06 6.25 -22%
p99.9 13.08 10.40 -20%
avg 2.32 2.32 0%
>= 10ms 3240 894 -72% (calls per run)The time with at most 4 non-empty old bins drops from 2.6% to 0.02%. The cost is that each call visits about 7% more memcgs (shrink_one() calls per call 118 -> 126), since reclaimers following the new old generation may revisit memcgs they rotated earlier in the same call, which shows up as the higher p50. Anon refaults, pswpin and pswpout are within 2% between A and B.
--- include/linux/mmzone.h | 9 ++++- mm/vmscan.c | 86 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 5 deletions(-)
include/linux/mmzone.h +7 -2
@@ -624,7 +624,9 @@ struct lru_gen_mm_walk {* An onlining memcg is added to the tail of a random bin in the old generation. * The eviction starts at the head of a random bin in the old generation. The * per-node memcg generation counter, whose reminder (mod MEMCG_NR_GENS) indexes -* the old generation, is incremented when all its bins become empty. +* the old generation, is incremented when all its bins become empty, or when +* there are more reclaimers than non-empty old bins and advancing adds bins +* (see try_to_inc_memcg_seq()). * * There are two operations: * 1. MEMCG_LRU_TAIL, which moves a memcg to the tail of a random bin in its
@@ -649,7 +651,8 @@ struct lru_gen_mm_walk {* memcgs. For memcg reclaim, it still relies on mem_cgroup_iter(). * 2. There are only two valid generations: old (seq) and young (seq+1). * MEMCG_NR_GENS is set to three so that when reading the generation counter -* locklessly, a stale value (seq-1) does not wraparound to young. +* locklessly, a stale value (seq-1) does not wraparound to young. Walkers +* that can be more stale, e.g. across shrink_one(), recheck seq. */ #define MEMCG_NR_GENS 3#define MEMCG_NR_BINS 8
@@ -663,6 +666,8 @@ struct lru_gen_memcg {struct hlist_nulls_head fifo[MEMCG_NR_GENS ][MEMCG_NR_BINS ];/* protects the above */ spinlock_t lock; +/* the number of reclaimers in shrink_many() */ +atomic_t nr_reclaimers; };void lru_gen_init_pgdat (struct pglist_data *pgdat);
mm/vmscan.c +83 -3
124 lines not on this page · raw message
Signature · 1 line
2.34.1