mm,slab,vmscan: accumulate gradual pressure on small slabs
Subsystems (4)
PATCH 1/1 mm,slab,vmscan: accumulate gradual pressure on small slabs
There are a few issues with the way the number of slab objects to scan is calculated in do_shrink_slab. First, for zero-seek slabs, we could leave the last object around forever. That could result in pinning a dying cgroup into memory, instead of reclaiming it. The fix for that is trivial.
Secondly, small slabs receive much more pressure, relative to their size, than larger slabs, due to "rounding up" the minimum number of scanned objects to batch_size.
We can keep the pressure on all slabs equal relative to their size by accumulating the scan pressure on small slabs over time, resulting in sometimes scanning an object, instead of always scanning several.
This results in lower system CPU use, and a lower major fault rate, as actively used entries from smaller caches get reclaimed less aggressively, and need to be reloaded/recreated less often.
--- include/linux/shrinker.h | 1 + mm/vmscan.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-)
include/linux/shrinker.h +1 -0
@@ -65,6 +65,7 @@ struct shrinker {long batch;/* reclaim batch size, 0 = default */ int seeks;/* seeks to recreate an obj */ +int small_scan;/* accumulate pressure on slabs with few objects */ unsigned flags;/* These are for internal use */
mm/vmscan.c +13 -3
@@ -488,18 +488,28 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,* them aggressively under memory pressure to keep * them from causing refetches in the IO caches. */ - delta = freeable /2 ; + delta = (freeable +1 )/2 ; }/* * Make sure we apply some minimal pressure on default priority -* even on small cgroups. Stale objects are not only consuming memory +* even on small cgroups, by accumulating pressure across multiple +* slab shrinker runs. Stale objects are not only consuming memory * by themselves, but can also hold a reference to a dying cgroup, * preventing it from being reclaimed. A dying cgroup with all * corresponding structures like per-cpu stats and kmem caches * can be really big, so it may lead to a significant waste of memory. */ - delta = max_t(unsigned long long, delta, min(freeable, batch_size)); +if (!delta) { + shrinker->small_scan += freeable; + + delta = shrinker->small_scan >> priority; + shrinker->small_scan -= delta << priority; + + delta *=4 ; +do_div (delta, shrinker->seeks); + + } total_scan += delta;if (total_scan <0 ) {
Quoted 18 lines · Rik van Riel
There are a few issues with the way the number of slab objects to scan is calculated in
do_shrink_slab. First, for zero-seek slabs, we could leave the last object around forever. That could result in pinning a dying cgroup into memory, instead of reclaiming it. The fix for that is trivial.Secondly, small slabs receive much more pressure, relative to their size, than larger slabs, due to "rounding up" the minimum number of scanned objects to
batch_size.We can keep the pressure on all slabs equal relative to their size by accumulating the scan pressure on small slabs over time, resulting in sometimes scanning an object, instead of always scanning several.
This results in lower system CPU use, and a lower major fault rate, as actively used entries from smaller caches get reclaimed less aggressively, and need to be reloaded/recreated less often.
Quoted 7 lines
Fixes 4b85afbdacd2 ("mm: zero-seek shrinkers")Fixes 172b06c32b94 ("mm: slowly shrink slabs with a relatively small number of objects")Cc Johannes Weiner <[email protected]>Cc Chris Mason <[email protected]>Cc Roman Gushchin <[email protected]>Tested-by Chris Mason <[email protected]>
Hi, Rik!
There is a couple of formatting issues (see below), but other than that the patch looks very good to me. Thanks!
Quoted 22 lines
--- include/linux/shrinker.h | 1 + mm/vmscan.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-)include/linux/shrinker.h +1
-0@@ -65,6 +65,7 @@ struct shrinker {long batch;/* reclaim batch size, 0 = default */ int seeks;/* seeks to recreate an obj */ +int small_scan;/* accumulate pressure on slabs with few objects */ unsigned flags;/* These are for internal use */ mm/vmscan.c +0
-0
mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 491
Quoted 5 lines
mm/vmscan.c +1
-1@@ -488,18 +488,28 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,* them aggressively under memory pressure to keep * them from causing refetches in the IO caches. */ - delta = freeable /2 ; + delta = (freeable +1 )/2 ;
^
A space is missing here.
Quoted 12 lines
mm/vmscan.c +2
-1}/* * Make sure we apply some minimal pressure on default priority -* even on small cgroups. Stale objects are not only consuming memory +* even on small cgroups, by accumulating pressure across multiple +* slab shrinker runs. Stale objects are not only consuming memory * by themselves, but can also hold a reference to a dying cgroup, * preventing it from being reclaimed. A dying cgroup with all * corresponding structures like per-cpu stats and kmem caches * can be really big, so it may lead to a significant waste of memory. */
mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 511
Quoted 10 lines
mm/vmscan.c +9
-1- delta = max_t(unsigned long long, delta, min(freeable, batch_size)); +if (!delta) { + shrinker->small_scan += freeable; + + delta = shrinker->small_scan >> priority; + shrinker->small_scan -= delta << priority; + + delta *=4 ; +do_div (delta, shrinker->seeks); +
This empty line can be removed, I believe.
Quoted 5 lines
mm/vmscan.c +1
-0+ } total_scan += delta;if (total_scan <0 ) {
Andrew Morton
maintainerQuoted 18 lines · On Mon, 28 Jan 2019 14:35:35 -0500 Rik van Riel <[email protected]> wrote:
There are a few issues with the way the number of slab objects to scan is calculated in
do_shrink_slab. First, for zero-seek slabs, we could leave the last object around forever. That could result in pinning a dying cgroup into memory, instead of reclaiming it. The fix for that is trivial.Secondly, small slabs receive much more pressure, relative to their size, than larger slabs, due to "rounding up" the minimum number of scanned objects to
batch_size.We can keep the pressure on all slabs equal relative to their size by accumulating the scan pressure on small slabs over time, resulting in sometimes scanning an object, instead of always scanning several.
This results in lower system CPU use, and a lower major fault rate, as actively used entries from smaller caches get reclaimed less aggressively, and need to be reloaded/recreated less often.
Quoted 7 lines
Fixes 4b85afbdacd2 ("mm: zero-seek shrinkers")Fixes 172b06c32b94 ("mm: slowly shrink slabs with a relatively small number of objects")Cc Johannes Weiner <[email protected]>Cc Chris Mason <[email protected]>Cc Roman Gushchin <[email protected]>Tested-by Chris Mason <[email protected]>
I added your Signed-off-by:
Quoted 19 lines
mm/vmscan.c +3
-2@@ -488,18 +488,28 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,* them aggressively under memory pressure to keep * them from causing refetches in the IO caches. */ - delta = freeable /2 ; + delta = (freeable +1 )/2 ; }/* * Make sure we apply some minimal pressure on default priority -* even on small cgroups. Stale objects are not only consuming memory +* even on small cgroups, by accumulating pressure across multiple +* slab shrinker runs. Stale objects are not only consuming memory * by themselves, but can also hold a reference to a dying cgroup, * preventing it from being reclaimed. A dying cgroup with all * corresponding structures like per-cpu stats and kmem caches * can be really big, so it may lead to a significant waste of memory.
mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 510
Quoted 10 lines
mm/vmscan.c +8
-1*/ - delta = max_t(unsigned long long, delta, min(freeable, batch_size)); +if (!delta) { + shrinker->small_scan += freeable; + + delta = shrinker->small_scan >> priority; + shrinker->small_scan -= delta << priority; + + delta *=4 ; +do_div (delta, shrinker->seeks);
What prevents shrinker->small_scan from over- or underflowing over time?
mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 514
Quoted 4 lines
mm/vmscan.c +1
-0+ } total_scan += delta;if (total_scan <0 ) {
I'll add this:
whitespace fixes, per Roman
mm/vmscan.c renamed from mm/vmscan.c~mmslabvmscan-accumulate-gradual-pressure-on-small-slabs-fix +1 -2
@@ -488,7 +488,7 @@ static unsigned long do_shrink_slab(stru* them aggressively under memory pressure to keep * them from causing refetches in the IO caches. */ - delta = (freeable +1 )/2 ; + delta = (freeable +1 ) /2 ; }/*
@@ -508,7 +508,6 @@ static unsigned long do_shrink_slab(stru delta *=4 ;do_div (delta, shrinker->seeks); - } total_scan += delta;
_
Quoted 23 lines · Andrew Morton
Quoted 20 lines · On Mon, 28 Jan 2019 14:35:35 -0500 Rik van Riel <[email protected]> wrote:
(quoted diff) +0
-1/* * Make sure we apply some minimal pressure on default priority - * even on small cgroups. Stale objects are not only consumingmemory
(quoted diff) +1
-0+ * even on small cgroups, by accumulating pressure acrossmultiple
(quoted diff) +1
-0+ * slab shrinker runs. Stale objects are not only consumingmemory
* by themselves, but can also hold a reference to a dying
cgroup,(quoted diff) +0
-0* preventing it from being reclaimed. A dying cgroup with all * corresponding structures like per-cpu stats and kmem caches * can be really big, so it may lead to a significant waste ofmemory.
(quoted diff) +0
-1*/ - delta = max_t(unsigned long long, delta, min(freeable,
batch_size));(quoted diff) +3
-0+ if (!delta) { + shrinker->small_scan += freeable; +
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ delta = shrinker->small_scan >> priority; + shrinker->small_scan -= delta << priority; + + delta *= 4; + do_div(delta, shrinker->seeks);What prevents
shrinker->small_scanfrom over- or underflowing over time?
We only go into this code path if
delta >> DEF_PRIORITY is zero.
That is, freeable is smaller than 4096.
Quoted 3 lines
I'll add this:
whitespace fixes, per Roman
Awesome, thank you!
Signature · 1 line
All Rights Reversed.
Andrew Morton
maintainerQuoted 33 lines · On Mon, 28 Jan 2019 15:03:28 -0500 Rik van Riel <[email protected]> wrote:
Quoted 23 lines · Andrew Morton
Quoted 20 lines · On Mon, 28 Jan 2019 14:35:35 -0500 Rik van Riel <[email protected]> wrote:
(quoted diff) +0
-1/* * Make sure we apply some minimal pressure on default priority - * even on small cgroups. Stale objects are not only consumingmemory
(quoted diff) +1
-0+ * even on small cgroups, by accumulating pressure acrossmultiple
(quoted diff) +1
-0+ * slab shrinker runs. Stale objects are not only consumingmemory
* by themselves, but can also hold a reference to a dying
cgroup,(quoted diff) +0
-0* preventing it from being reclaimed. A dying cgroup with all * corresponding structures like per-cpu stats and kmem caches * can be really big, so it may lead to a significant waste ofmemory.
(quoted diff) +0
-1*/ - delta = max_t(unsigned long long, delta, min(freeable,
batch_size));(quoted diff) +3
-0+ if (!delta) { + shrinker->small_scan += freeable; +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ delta = shrinker->small_scan >> priority; + shrinker->small_scan -= delta << priority; + + delta *= 4; + do_div(delta, shrinker->seeks);What prevents
shrinker->small_scanfrom over- or underflowing over time?
Quoted 5 lines
We only go into this code path if
delta >>DEF_PRIORITYis zero.That is, freeable is smaller than 4096.
I'm still not understanding. If `freeable' always has a value of (say) 1, we'll eventually overflow shrinker->small_scan? Or at least, it's unobvious why this cannot happen.
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Mon, 28 Jan 2019 15:03:28 -0500 Rik van Riel <[email protected]> wrote:
Quoted 4 lines · Andrew Morton
On Mon, 28 Jan 2019 14:35:35 -0500 Rik van Riel <[email protected]
Quoted 1 line
wrote:
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Review
Quoted 9 lines
memory.
(quoted diff) +0
-1*/ - delta = max_t(unsigned long long, delta, min(freeable,
batch_size));(quoted diff) +5
-0+ if (!delta) { + shrinker->small_scan += freeable; + + delta = shrinker->small_scan >> priority; + shrinker->small_scan -= delta << priority;
When delta is a non-zero number, we subtract (delta << priority) from shrinker->small_scan.
That should happen every time delta >= (1<<priority), which is 4096 for DEF_PRIORITY.
Quoted 18 lines
Quoted 8 lines
Quoted 7 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ + delta *= 4; + do_div(delta, shrinker->seeks);What prevents
shrinker->small_scanfrom over- or underflowing over
time?Quoted 5 lines
We only go into this code path if
delta >>DEF_PRIORITYis zero.That is, freeable is smaller than 4096.
I'm still not understanding. If `freeable' always has a value of (say)
1, we'll eventually overflowshrinker->small_scan? Or at least, it's unobvious why this cannot happen.
Signature · 1 line
All Rights Reversed.
Quoted 18 lines · Rik van Riel
There are a few issues with the way the number of slab objects to scan is calculated in
do_shrink_slab. First, for zero-seek slabs, we could leave the last object around forever. That could result in pinning a dying cgroup into memory, instead of reclaiming it. The fix for that is trivial.Secondly, small slabs receive much more pressure, relative to their size, than larger slabs, due to "rounding up" the minimum number of scanned objects to
batch_size.We can keep the pressure on all slabs equal relative to their size by accumulating the scan pressure on small slabs over time, resulting in sometimes scanning an object, instead of always scanning several.
This results in lower system CPU use, and a lower major fault rate, as actively used entries from smaller caches get reclaimed less aggressively, and need to be reloaded/recreated less often.
Quoted 7 lines
Fixes 4b85afbdacd2 ("mm: zero-seek shrinkers")Fixes 172b06c32b94 ("mm: slowly shrink slabs with a relatively small number of objects")Cc Johannes Weiner <[email protected]>Cc Chris Mason <[email protected]>Cc Roman Gushchin <[email protected]>Tested-by Chris Mason <[email protected]>