Skip to contentSkip to conversation

mm: move LRU size stat updates out of lru_lock in folio batches

PATCH Applied Joanne Koong 3 participants 7 messages Oct 29 – Nov 6

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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] where 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.

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. A perf profile with 80 threads faulting in and freeing shmem in a single memcg shows 52.8% of all cycles spent spinning on lru_lock, split evenly between folio_batch_move_lru() and folios_put_refs(). About 25% of the time spent holding lru_lock in those two paths goes to the LRU stat updates, mostly cache misses loading pn->memcg, pn->lruvec_stats_percpu and the per-cpu counters. The stat updates are only 0.7% of all cycles, but they lengthen the critical section that the other threads are waiting on.

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%)

---
v1: https://lore.kernel.org/linux-mm/[email protected]/
v1 -> v2:
* Added summary of [1] to the commit message as well as the perf profile findings (Shakeel)

 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
index ab69b9930893..b3d85fe91385 100644
@@ -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
index ecd1ebd49756..8618e9543983 100644
@@ -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

base-commit: 9c449f885765cf8d80b2c60b1971e774bba51a3f

Signature · 1 line
2.52.0
reviewer Acked-by on v2 1/1
#1 lore raw
Quoted 21 lines · Joanne Koong

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] where 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.

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. A perf profile with 80 threads faulting in and freeing shmem in a single memcg shows 52.8% of all cycles spent spinning on lru_lock, split evenly between folio_batch_move_lru() and folios_put_refs(). About 25% of the time spent holding lru_lock in those two paths goes to the LRU stat updates, mostly cache misses loading pn->memcg, pn->lruvec_stats_percpu and the per-cpu counters. The stat updates are only 0.7% of all cycles, but they lengthen the critical section that the other threads are waiting on.

Quoted 5 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_lock critical section and turns the per-folio updates into at most one update per LRU list per batch.

This updating of stats after dropping the lru lock does introduce one behavior change i.e. race between reparenting on offlining and stats update. I think it is safe and correct though. Better to document it.

Quoted 14 lines

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:

Quoted 8 lines

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%)
#2 lore raw
Quoted 52 lines · Shakeel Butt
Quoted 21 lines · Joanne Koong

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] where 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.

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. A perf profile with 80 threads faulting in and freeing shmem in a single memcg shows 52.8% of all cycles spent spinning on lru_lock, split evenly between folio_batch_move_lru() and folios_put_refs(). About 25% of the time spent holding lru_lock in those two paths goes to the LRU stat updates, mostly cache misses loading pn->memcg, pn->lruvec_stats_percpu and the per-cpu counters. The stat updates are only 0.7% of all cycles, but they lengthen the critical section that the other threads are waiting on.

Quoted 5 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_lock critical section and turns the per-folio updates into at most one update per LRU list per batch.

This updating of stats after dropping the lru lock does introduce one behavior change i.e. race between reparenting on offlining and stats update. I think it is safe and correct though. Better to document it.

Quoted 20 lines

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%)
Quoted 5 lines
Quoted 2 lines

Thanks, I'm planning on queue this.

Signature · 3 lines
Cheers,

David
#3 lore raw
Quoted 54 lines · David Hildenbrand (Arm)
Quoted 52 lines · Shakeel Butt
Quoted 21 lines · Joanne Koong

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] where 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.

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. A perf profile with 80 threads faulting in and freeing shmem in a single memcg shows 52.8% of all cycles spent spinning on lru_lock, split evenly between folio_batch_move_lru() and folios_put_refs(). About 25% of the time spent holding lru_lock in those two paths goes to the LRU stat updates, mostly cache misses loading pn->memcg, pn->lruvec_stats_percpu and the per-cpu counters. The stat updates are only 0.7% of all cycles, but they lengthen the critical section that the other threads are waiting on.

Quoted 5 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_lock critical section and turns the per-folio updates into at most one update per LRU list per batch.

This updating of stats after dropping the lru lock does introduce one behavior change i.e. race between reparenting on offlining and stats update. I think it is safe and correct though. Better to document it.

Quoted 20 lines

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%)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Thanks, I'm planning on queue this.

I'll send out a v3 today that adds the documentation Shakeel suggested about the race between reparenting on offlining and stats update.

Thanks,
Joanne

#4 lore raw
Quoted 60 lines · Joanne Koong
Quoted 57 lines · David Hildenbrand (Arm)
Quoted 55 lines · Shakeel Butt
Quoted 21 lines · Joanne Koong

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] where 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.

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. A perf profile with 80 threads faulting in and freeing shmem in a single memcg shows 52.8% of all cycles spent spinning on lru_lock, split evenly between folio_batch_move_lru() and folios_put_refs(). About 25% of the time spent holding lru_lock in those two paths goes to the LRU stat updates, mostly cache misses loading pn->memcg, pn->lruvec_stats_percpu and the per-cpu counters. The stat updates are only 0.7% of all cycles, but they lengthen the critical section that the other threads are waiting on.

Quoted 5 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_lock critical section and turns the per-folio updates into at most one update per LRU list per batch.

This updating of stats after dropping the lru lock does introduce one behavior change i.e. race between reparenting on offlining and stats update. I think it is safe and correct though. Better to document it.

Quoted 22 lines

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%)
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Thanks, I'm planning on queue this.

I'll send out a v3 today that adds the documentation Shakeel suggested about the race between reparenting on offlining and stats update.

I'm seeing that the patch was picked up yesterday into mm-new. Could you or Andrew add this paragraph to the commit message after the "Accumulate the stats changes..." paragraph?:

"Applying the stats after dropping lru_lock means a batched update can race with reparenting when the memcg is being offlined. This is ok because updates to a dying memcg are redirected to its nearest non-dying ancestor, which is where its folios end up after reparenting."

Apologies for any inconveniences.

Thanks,
Joanne

#5 lore raw
Quoted 6 lines · Joanne Koong
Quoted 5 lines · Joanne Koong
Quoted 2 lines · David Hildenbrand (Arm)
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Thanks, I'm planning on queue this.

I'll send out a v3 today that adds the documentation Shakeel suggested about the race between reparenting on offlining and stats update.

I'm seeing that the patch was picked up yesterday into mm-new.

Yes, soon Andrew should probably slow down on picking things that I am supposed to pick now.

Will resolve itself once patches only cleanly apply against mm/core.git :)

For now, he will drop patches once I pick them.

Quoted 11 lines

Could
you or Andrew add this paragraph to the commit message after the "Accumulate the stats changes..." paragraph?:

"Applying the stats after dropping lru_lock means a batched update can race with reparenting when the memcg is being offlined. This is ok because updates to a dying memcg are redirected to its nearest non-dying ancestor, which is where its folios end up after reparenting."

Apologies for any inconveniences.

Done, thanks!

Signature · 3 lines
Cheers,

David
maintainer Applied
#6 lore raw
In reply to Joanne Koong
Quoted 11 lines · Joanne Koong

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] where 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.

[...]

Applied, thanks!

tree: mm/core.git
branch: core-7.5.misc

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.

[1/1] mm: move LRU size stat updates out of lru_lock in folio batches

      https://git.kernel.org/mm/core/c/ee65a1da
Signature · 3 lines
Cheers,

David