mm: move LRU size stat updates out of lru_lock in folio batches
Subsystems (4)
PATCH 1/1 mm: move LRU size stat updates out of lru_lock in folio batches
folio_batch_move_lru() and folios_put_refs() take the lruvec's lru_lock once per batch, but they update the node and memcg lruvec stats for every folio in the batch while holding the lock. Only the LRU lists and the memcg lru_zone_size need lru_lock. Updating the stats while holding the lru_lock is unnecessary, and they make up a large part of the critical section, as seen in [1]. With many threads faulting and freeing memory in a single memcg, about half of all cycles are spent waiting for lru_lock, and the hold time of the lru_lock directly limits throughput.
Accumulate the stat changes per LRU list while holding the lock instead, and only apply them after dropping the lock (while still inside the RCU read section that keeps the lruvec's memcg alive). This moves the stat updates out of the lru_lock critical section and turns the per-folio updates into at most one update per LRU list per batch.
The batch is attached to the lruvec while lru_lock is held so that the LRU add/del helpers don't need to pass it down. To keep struct lruvec from growing, cost_lock is moved into the hole after lru_lock, which leaves room for the batch pointer in the same cacheline as lru_lock.
The stats can now lag behind the LRU lists by one batch, which is well within the per-cpu batching they already have.
On a 2-socket Skylake (80 CPUs, 2 NUMA nodes), with threads pinned to CPUs repeatedly faulting in and freeing memory in a single memcg, average pages per second over 7 freshly created memcgs per run, 2 boots each:
before after
shmem, 20 threads 4.74M 5.47M (+15%) shmem, 80 threads 7.94M 10.33M (+30%) anon, 20 threads 3.04M 3.35M (+10%) anon, 80 threads 3.23M 3.41M (+5%)
---
An alternative that avoids adding the lru_size_batch pointer to the lruvec struct is to pass the batch down explicitly, but the plumbing for this is a bit ugly (and doesn't cover mglru paths). The code looks like [2]. Happy to go with this if that is preferred.
[2] https://github.com/joannekoong/linux/commit/2f20765b01f98113e3c83ea0d0f98580502bfb96
include/linux/mm_inline.h | 12 ++++++- include/linux/mmzone.h | 11 +++++-- mm/folio.c | 67 +++++++++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 13 deletions(-)
include/linux/mm_inline.h +11 -1
@@ -50,7 +50,17 @@ static __always_inline void __update_lru_size(struct lruvec *lruvec,lockdep_assert_held (&lruvec->lru_lock);WARN_ON_ONCE (nr_pages != (int )nr_pages); -mod_lruvec_state (lruvec,NR_LRU_BASE + lru, nr_pages); +/* +* Batches in mm/folio.c attach an lru_size_batch while holding +* lru_lock, to apply the node and memcg lruvec stats only after the +* lock is dropped. The stats don't need lru_lock and updating them is +* a large part of the critical section when batching. The zone stats +* are still updated right away. +*/ +if (lruvec->lru_size_batch) + lruvec->lru_size_batch->nr_pages[lru] += nr_pages; +else +mod_lruvec_state (lruvec,NR_LRU_BASE + lru, nr_pages);__mod_zone_page_state (&pgdat->node_zones[zid],NR_ZONE_LRU_BASE + lru, nr_pages); }
include/linux/mmzone.h +9 -2
@@ -739,10 +739,19 @@ struct lru_cost {unsigned long last_io; }; +/* LRU size changes made under lru_lock, applied after the lock is dropped */ +struct lru_size_batch { +long nr_pages[NR_LRU_LISTS ]; +}; +struct lruvec {struct list_head lists[NR_LRU_LISTS ];/* per lruvec lru_lock for memcg */ spinlock_t lru_lock; +/* Protects cost[]. */ +spinlock_t cost_lock; +/* If set, defers lruvec stat updates until lru_lock is dropped */ +struct lru_size_batch *lru_size_batch;/* * These track the cost of reclaiming one LRU - file or anon - * over the other. As the observed cost of reclaiming one LRU
@@ -751,8 +760,6 @@ struct lruvec {* serialises that update. */ struct lru_cost cost[ANON_AND_FILE ]; -/* Protects cost[]. */ -spinlock_t cost_lock;/* Non-resident age, driven by LRU movement */ atomic_long_t nonresident_age;/* Refaults at the time of last reclaim cycle */
mm/folio.c +57 -10
133 lines not on this page · raw message
Signature · 1 line
2.52.0
Shakeel Butt
reviewerQuoted 6 lines · Joanne Koong
folio_batch_move_lru()andfolios_put_refs()take the lruvec'slru_lockonce per batch, but they update the node and memcg lruvec stats for every folio in the batch while holding the lock. Only the LRU lists and the memcglru_zone_sizeneedlru_lock. Updating the stats while holding thelru_lockis unnecessary, and they make up a large part of the critical section, as seen in [1].
Can you please add a tldr from [1] here? Too much text in that link.
Quoted 3 lines
With many threads faulting and freeing
memory in a single memcg, about half of all cycles are spent waiting forlru_lock, and the hold time of thelru_lockdirectly limits throughput.
Do you have perf profile showing this? Mainly I want to know where in stats update we are spending the CPU most.
I wonder if we can find a lower bound here by removing the stats update altogether in this code path (just for experimentation) and measure the throughput we get.
Quoted 26 lines
Accumulate the stat changes per LRU list while holding the lock instead, and only apply them after dropping the lock (while still inside the RCU read section that keeps the lruvec's memcg alive). This moves the stat updates out of the
lru_lockcritical section and turns the per-folio updates into at most one update per LRU list per batch.The batch is attached to the lruvec while
lru_lockis held so that the LRU add/del helpers don't need to pass it down. To keep struct lruvec from growing,cost_lockis moved into the hole afterlru_lock, which leaves room for the batch pointer in the same cacheline aslru_lock.The stats can now lag behind the LRU lists by one batch, which is well within the per-cpu batching they already have.
On a 2-socket Skylake (80 CPUs, 2 NUMA nodes), with threads pinned to CPUs repeatedly faulting in and freeing memory in a single memcg, average pages per second over 7 freshly created memcgs per run, 2 boots each:
before after
shmem, 20 threads 4.74M 5.47M (+15%) shmem, 80 threads 7.94M 10.33M (+30%) anon, 20 threads 3.04M 3.35M (+10%) anon, 80 threads 3.23M 3.41M (+5%)
Quoted 7 lines
Link https://lore.kernel.org/linux-fsdevel/CAJnrk1ZezmmzrYdf8KP6re_a76izMS=qGPeC8ZZXiteHyZZC-Q@mail.gmail.com/ [1] open hereSigned-off-by Joanne Koong <[email protected]>---
An alternative that avoids adding thelru_size_batchpointer to the lruvec struct is to pass the batch down explicitly, but the plumbing for this is a bit ugly (and doesn't cover mglru paths). The code looks like [2]. Happy to go with this if that is preferred.
Do you expect there might be opportunity to batch the stats other than LRU stats you batched in this patch? If there is then I think we should at least explore if a more general stat batching can be done.
This code looks good though.
Quoted 10 lines · Shakeel Butt
Quoted 6 lines · Joanne Koong
folio_batch_move_lru()andfolios_put_refs()take the lruvec'slru_lockonce per batch, but they update the node and memcg lruvec stats for every folio in the batch while holding the lock. Only the LRU lists and the memcglru_zone_sizeneedlru_lock. Updating the stats while holding thelru_lockis unnecessary, and they make up a large part of the critical section, as seen in [1].Can you please add a tldr from [1] here? Too much text in that link.
Will do in v2. THe relevant part of [1] is that a change that added just a few instructions to the memcg stat update path cost about 2% throughput because the LRU stat updates run under lru_lock and the overhead went away once they were moved outside of the lock, which is what motivated this patch.
Quoted 7 lines
Quoted 4 lines
With many threads faulting and freeing
memory in a single memcg, about half of all cycles are spent waiting forlru_lock, and the hold time of thelru_lockdirectly limits throughput.Do you have perf profile showing this? Mainly I want to know where in stats update we are spending the CPU most.
The perf profile run on the top commit of mm-new (commit fcec4f4bcc "riscv: mm: fix concurrency in mark_new_valid_map()) using the same benchmark setup (80 threads, shmem) shows:
- 52.8% of all cycles are spent spinning on the lru_lock (evenly split between folio_batch_move_lru() and folios_put_refs())
- About 25% of the time spent holding lru_lock in those 2 paths goes to the LRU stat updates (the ones this patch moves to outside the lock) and the rest is the list manipulation itself
- Most of the cost in __mod_memcg_lruvec_state() is cache misses (loading pn->memcg, pn->lruvec_stats_percpu, and the per-cpu counters).
The stat updates are only 0.7% of total cycles but I think it's the lock contention on callers waiting for the lock that ends up hurting throughput.
Quoted 4 lines
I wonder if we can find a lower bound here by removing the stats update altogether in this code path (just for experimentation) and measure the throughput we get.
With the LRU stat updates removed from these paths entirely, I'm seeing that throughput is essentially the same as with this patch (within noise):
mm-new this patch no stat updates
shmem, 80 threads 7.94M 10.33M 10.31M
anon, 20 threads 3.04M 3.35M 3.33MI think once the updates are moved out of the critical section entirely, they don't cost much to do.
Quoted 35 lines
Quoted 27 lines
Accumulate the stat changes per LRU list while holding the lock instead, and only apply them after dropping the lock (while still inside the RCU read section that keeps the lruvec's memcg alive). This moves the stat updates out of the
lru_lockcritical section and turns the per-folio updates into at most one update per LRU list per batch.The batch is attached to the lruvec while
lru_lockis held so that the LRU add/del helpers don't need to pass it down. To keep struct lruvec from growing,cost_lockis moved into the hole afterlru_lock, which leaves room for the batch pointer in the same cacheline aslru_lock.The stats can now lag behind the LRU lists by one batch, which is well within the per-cpu batching they already have.
On a 2-socket Skylake (80 CPUs, 2 NUMA nodes), with threads pinned to CPUs repeatedly faulting in and freeing memory in a single memcg, average pages per second over 7 freshly created memcgs per run, 2 boots each:
before after
shmem, 20 threads 4.74M 5.47M (+15%) shmem, 80 threads 7.94M 10.33M (+30%) anon, 20 threads 3.04M 3.35M (+10%) anon, 80 threads 3.23M 3.41M (+5%)Quoted 7 lines
Link https://lore.kernel.org/linux-fsdevel/CAJnrk1ZezmmzrYdf8KP6re_a76izMS=qGPeC8ZZXiteHyZZC-Q@mail.gmail.com/ [1] open hereSigned-off-by Joanne Koong <[email protected]>---
An alternative that avoids adding thelru_size_batchpointer to the lruvec struct is to pass the batch down explicitly, but the plumbing for this is a bit ugly (and doesn't cover mglru paths). The code looks like [2]. Happy to go with this if that is preferred.
Quoted 3 lines
Do you expect there might be opportunity to batch the stats other than LRU stats you batched in this patch? If there is then I think we should at least explore if a more general stat batching can be done.
I had experimented a little with doing this for LRU size stats in the reclaim paths in vmscan and deferring the count_memcg_events() calls in the same batch paths, but none of them made a measurable difference to throughput. For the reclaim paths, I think it's because there reclaim rather than the lru_lock is the bottleneck, and my benchmark doesn't really exercise the paths that call count_memcg_events(). As far as I can tell, the rmap stats (NR_ANON_MAPPED, NR_FILE_MAPPED) are under the page table lock but that lock isn't really contended. I think the other maybe-potentially-promising candidate is the page cache stats (NR_FILE_PAGES, NR_SHMEM, ...) that are updated per folio under the mapping's i_pages lock but that would matter only for threads working on a single shared file. I can look more into the page cache stats and see whether the complexity would be worth it there.
Thanks for taking a look at this patch!
Thanks,
Joanne