Skip to contentSkip to conversation

mm: do not call add_nr_deferred() with zero deferred

PATCH In review Roman Gushchin 4 participants 7 messages Apr 16, 2022 – Apr 22, 2022

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

PATCH 1/1 mm: do not call add_nr_deferred() with zero deferred

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0	             \
  last shrinker return val 0

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

---
 mm/vmscan.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmscan.c +4 -1
index d4a7d2bd276d..19d3d4fa1aad 100644
@@ -808,7 +808,10 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
 	 * move the unused scan count back into the shrinker in a
 	 * manner that handles concurrent updates.
 	 */
-	new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	if (next_deferred)
+		new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	else
+		new_nr = nr;
 
 	trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
 	return freed;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 19 lines · Roman Gushchin

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0	             \
  last shrinker return val 0
Quoted 5 lines

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

Sufficient improvement to justify added complexity for anybody reading that code?

Quoted 12 lines
---
 mm/vmscan.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmscan.c +0 -0
index d4a7d2bd276d..19d3d4fa1aad 100644
@@ -808,7 +808,10 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
 	 * move the unused scan count back into the shrinker in a

mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 817

Quoted 10 lines
mm/vmscan.c +4 -1
 	 * manner that handles concurrent updates.
 	 */
-	new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	if (next_deferred)
+		new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	else
+		new_nr = nr;
 
 	trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
 	return freed;

And if we still want to do this optimization, why not put it into add_nr_deferred()?

Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
Quoted 20 lines · David Hildenbrand
Quoted 19 lines · Roman Gushchin

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0	             \
  last shrinker return val 0
Quoted 8 lines
Quoted 5 lines

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

Sufficient improvement to justify added complexity for anybody reading that code?

I don't have any numbers and really doubt the difference is significant, however the added complexity is also small: one "if" statement. Anyway, if you feel strongly against this change, I'm fine with dropping it.

Quoted 21 lines
Quoted 21 lines
---
 mm/vmscan.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmscan.c +4 -1
index d4a7d2bd276d..19d3d4fa1aad 100644
@@ -808,7 +808,10 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
 	 * move the unused scan count back into the shrinker in a
 	 * manner that handles concurrent updates.
 	 */
-	new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	if (next_deferred)
+		new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	else
+		new_nr = nr;
 
Quoted 5 lines

mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 817

Quoted 2 lines
mm/vmscan.c +0 -0
 	trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
 	return freed;

And if we still want to do this optimization, why not put it into add_nr_deferred()?

Because of the semantics of add_nr_deferred(), which returns the deferred value. It's not used for anything except tracing, so maybe it's a place for another change.

Thanks!

#3 lore raw
Quoted 27 lines · Roman Gushchin
Quoted 26 lines · David Hildenbrand
Quoted 24 lines · Roman Gushchin

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0	             \
  last shrinker return val 0

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

Quoted 7 lines
Quoted 2 lines

Sufficient improvement to justify added complexity for anybody reading that code?

I don't have any numbers and really doubt the difference is significant, however the added complexity is also small: one "if" statement. Anyway, if you feel strongly against this change, I'm fine with dropping it.

No strong opinion, naturally, more conditions make the code harder to read -- that's why I'm asking.

Quoted 27 lines
Quoted 21 lines
Quoted 21 lines
---
 mm/vmscan.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmscan.c +4 -1
index d4a7d2bd276d..19d3d4fa1aad 100644
@@ -808,7 +808,10 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
 	 * move the unused scan count back into the shrinker in a
 	 * manner that handles concurrent updates.
 	 */
-	new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	if (next_deferred)
+		new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+	else
+		new_nr = nr;
 
Quoted 5 lines

mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 817

Quoted 2 lines
mm/vmscan.c +0 -0
 	trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
 	return freed;

And if we still want to do this optimization, why not put it into add_nr_deferred()?

Quoted 3 lines

Because of the semantics of add_nr_deferred(), which returns the deferred value. It's not used for anything except tracing, so maybe it's a place for another change.

Skimming over the code I somehow missed that add_nr_deferred() doesn't have "nr" naturally available.

LGTM

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw
Quoted 32 lines · David Hildenbrand
Quoted 30 lines · Roman Gushchin
Quoted 28 lines · David Hildenbrand
Quoted 19 lines · Roman Gushchin

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1            \
  unused scan count 0 new scan count 0 total_scan 0      \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1            \
  unused scan count 0 new scan count 0 total_scan 0      \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0                     \
  last shrinker return val 0
Quoted 5 lines

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

Sufficient improvement to justify added complexity for anybody reading that code?

Quoted 7 lines
Quoted 4 lines

I don't have any numbers and really doubt the difference is significant, however the added complexity is also small: one "if" statement. Anyway, if you feel strongly against this change, I'm fine with dropping it.

No strong opinion, naturally, more conditions make the code harder to read -- that's why I'm asking.

This is why that "if" was removed by commit 867508304685 ("mm: vmscan:
use per memcg nr_deferred of shrinker") since it didn't bring in measurable performance improvement.

TBH I'm not sure whether it is worth it with no measurable performance boost but harder to read code and potential outdated "new scan count".

Quoted 45 lines
Quoted 28 lines
Quoted 22 lines
Quoted 22 lines
---
 mm/vmscan.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmscan.c +4 -1
index d4a7d2bd276d..19d3d4fa1aad 100644
@@ -808,7 +808,10 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
     * move the unused scan count back into the shrinker in a
     * manner that handles concurrent updates.
     */
-   new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+   if (next_deferred)
+           new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
+   else
+           new_nr = nr;
 
Quoted 5 lines

mm/vmscan.c static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, · line 817

Quoted 2 lines
mm/vmscan.c +0 -0
    trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan);
    return freed;

And if we still want to do this optimization, why not put it into add_nr_deferred()?

Quoted 3 lines

Because of the semantics of add_nr_deferred(), which returns the deferred value. It's not used for anything except tracing, so maybe it's a place for another change.

Skimming over the code I somehow missed that add_nr_deferred() doesn't have "nr" naturally available.

LGTM

--
Thanks,

David / dhildenb

#5 lore raw
In reply to Roman Gushchin
Quoted 27 lines · Roman Gushchin
Quoted 26 lines · David Hildenbrand
Quoted 24 lines · Roman Gushchin

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0	             \
  last shrinker return val 0

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

Quoted 4 lines
Quoted 2 lines

Sufficient improvement to justify added complexity for anybody reading that code?

I don't have any numbers and really doubt the difference is significant,

Never been able to measure it myself.

HwoeverI'd much prefer the tracepoint output stays accurate - I've had to post-process and/or graph the shrinker progress as reported by the start/end tracpoints to find problems in the algorithms in the past. That's why there is the additional complexity in the code to make sure the coutners are accurate in the first place.

Quoted 1 line

however the added complexity is also small: one "if" statement.

Yeah, complexity is not the problem here - it's that accuracy of the tracepoints has actually mattered to me in the past...

Cheers,

DAve.

Signature · 2 lines
Dave Chinner
[email protected]
#6 lore raw
Quoted 33 lines · Dave Chinner
Quoted 27 lines · Roman Gushchin
Quoted 26 lines · David Hildenbrand
Quoted 24 lines · Roman Gushchin

add_nr_deferred() is often called with next_deferred equal to 0. For instance, it's happening under low memory pressure for any shrinkers with a low number of cached objects. A corresponding trace looks like:
<...>-619914 [005] .... 467456.345160: mm_shrink_slab_end: \

  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0
  <...>-619914 [005] .... 467456.345371: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1	     \
  unused scan count 0 new scan count 0 total_scan 0	     \
  last shrinker return val 0

  <...>-619914 [005] .... 467456.345380: mm_shrink_slab_end: \
  super_cache_scan+0x0/0x1a0 0000000087027f06: nid: 1 unused \
  scan count 0 new scan count 0 total_scan 0	             \
  last shrinker return val 0

This lead to unnecessary checks and atomic operations, which can be avoided by checking next_deferred for not being zero before calling add_nr_deferred(). In this case the mm_shrink_slab_end trace point will get a potentially slightly outdated "new scan count" value, but it's totally fine.

Quoted 4 lines
Quoted 2 lines

Sufficient improvement to justify added complexity for anybody reading that code?

I don't have any numbers and really doubt the difference is significant,

Quoted 7 lines

Never been able to measure it myself.

HwoeverI'd much prefer the tracepoint output stays accurate - I've had to post-process and/or graph the shrinker progress as reported by the start/end tracpoints to find problems in the algorithms in the past. That's why there is the additional complexity in the code to make sure the coutners are accurate in the first place.

Sure, no problems.

Andrew, can you, please, drop this patch?

Thanks!