Skip to content

Commit 18bb473

Browse files
yang-shitorvalds
authored andcommitted
mm: vmscan: shrink deferred objects proportional to priority
The number of deferred objects might get windup to an absurd number, and it results in clamp of slab objects. It is undesirable for sustaining workingset. So shrink deferred objects proportional to priority and cap nr_deferred to twice of cache items. The idea is borrowed from Dave Chinner's patch: https://lore.kernel.org/linux-xfs/20191031234618.15403-13-david@fromorbit.com/ Tested with kernel build and vfs metadata heavy workload in our production environment, no regression is spotted so far. Link: https://lkml.kernel.org/r/20210311190845.9708-14-shy828301@gmail.com Signed-off-by: Yang Shi <shy828301@gmail.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Kirill Tkhai <ktkhai@virtuozzo.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Roman Gushchin <guro@fb.com> Cc: Shakeel Butt <shakeelb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a178015 commit 18bb473

1 file changed

Lines changed: 11 additions & 35 deletions

File tree

‎mm/vmscan.c‎

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,6 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
664664
*/
665665
nr = xchg_nr_deferred(shrinker, shrinkctl);
666666

667-
total_scan = nr;
668667
if (shrinker->seeks) {
669668
delta = freeable >> priority;
670669
delta *= 4;
@@ -678,37 +677,9 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
678677
delta = freeable / 2;
679678
}
680679

680+
total_scan = nr >> priority;
681681
total_scan += delta;
682-
if (total_scan < 0) {
683-
pr_err("shrink_slab: %pS negative objects to delete nr=%ld\n",
684-
shrinker->scan_objects, total_scan);
685-
total_scan = freeable;
686-
next_deferred = nr;
687-
} else
688-
next_deferred = total_scan;
689-
690-
/*
691-
* We need to avoid excessive windup on filesystem shrinkers
692-
* due to large numbers of GFP_NOFS allocations causing the
693-
* shrinkers to return -1 all the time. This results in a large
694-
* nr being built up so when a shrink that can do some work
695-
* comes along it empties the entire cache due to nr >>>
696-
* freeable. This is bad for sustaining a working set in
697-
* memory.
698-
*
699-
* Hence only allow the shrinker to scan the entire cache when
700-
* a large delta change is calculated directly.
701-
*/
702-
if (delta < freeable / 4)
703-
total_scan = min(total_scan, freeable / 2);
704-
705-
/*
706-
* Avoid risking looping forever due to too large nr value:
707-
* never try to free more than twice the estimate number of
708-
* freeable entries.
709-
*/
710-
if (total_scan > freeable * 2)
711-
total_scan = freeable * 2;
682+
total_scan = min(total_scan, (2 * freeable));
712683

713684
trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
714685
freeable, delta, total_scan, priority);
@@ -747,10 +718,15 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
747718
cond_resched();
748719
}
749720

750-
if (next_deferred >= scanned)
751-
next_deferred -= scanned;
752-
else
753-
next_deferred = 0;
721+
/*
722+
* The deferred work is increased by any new work (delta) that wasn't
723+
* done, decreased by old deferred work that was done now.
724+
*
725+
* And it is capped to two times of the freeable items.
726+
*/
727+
next_deferred = max_t(long, (nr + delta - scanned), 0);
728+
next_deferred = min(next_deferred, (2 * freeable));
729+
754730
/*
755731
* move the unused scan count back into the shrinker in a
756732
* manner that handles concurrent updates.

0 commit comments

Comments
 (0)