Skip to contentSkip to conversation

mm, shrinker: make shrinker_list lockless

PATCH Superseded Shakeel Butt 1 participant 2 messages Nov 7, 2017 – Nov 8, 2017

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, shrinker: make shrinker_list lockless

In our production, we have observed that the job loader gets stuck for 10s of seconds while doing mount operation. It turns out that it was stuck in register_shrinker() and some unrelated job was under memory pressure and spending time in shrink_slab(). Our machines have a lot of shrinkers registered and jobs under memory pressure has to traverse all of those memcg-aware shrinkers and do affect unrelated jobs which want to register their own shrinkers.

This patch has made the shrinker_list traversal lockless and shrinker register remain fast. For the shrinker unregister, atomic counter has been introduced to avoid synchronize_rcu() call. The fields of struct shrinker has been rearraged to make sure that the size does not increase.

---
 include/linux/shrinker.h |  4 +++-
 mm/vmscan.c              | 54 +++++++++++++++++++++++++++++-------------------
 2 files changed, 36 insertions(+), 22 deletions(-)
include/linux/shrinker.h +3 -1
index 388ff2936a87..434b76ef9367 100644
@@ -60,14 +60,16 @@ struct shrinker {
 	unsigned long (*scan_objects)(struct shrinker *,
 				      struct shrink_control *sc);
 
+	unsigned int flags;
 	int seeks;	/* seeks to recreate an obj */
 	long batch;	/* reclaim batch size, 0 = default */
-	unsigned long flags;
 
 	/* These are for internal use */
 	struct list_head list;
 	/* objs pending delete, per node */
 	atomic_long_t *nr_deferred;
+	/* Number of active do_shrink_slab calls to this shrinker */
+	atomic_t nr_active;
 };
 #define DEFAULT_SEEKS 2 /* A good number if you don't know better. */
 
mm/vmscan.c +33 -21

97 lines not on this page · raw message

Signature · 7 lines
2.15.0.403.gc27cc4dac6-goog

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 6 lines

if (next_deferred >= scanned)

mm/vmscan.c +0 -1
@@ -468,18 +487,9 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
        if (nr_scanned == 0)
                nr_scanned = SWAP_CLUSTER_MAX;
 
-       if (!down_read_trylock(&shrinker_rwsem)) {

mm/vmscan.c static unsigned long shrink_slab(gfp_t gfp_mask, int nid, · line 490

Quoted 10 lines
mm/vmscan.c +1 -9
-               /*
-                * If we would return 0, our callers would understand that we
-                * have nothing else to shrink and give up trying. By returning
-                * 1 we keep it going and assume we'll be able to shrink next
-                * time.
-                */
-               freed = 1;
-               goto out;
-       }
+       rcu_read_lock();

Sorry, the rcu_read_lock() will not work. I am currently testing with srcu_read_lock() and see if it gives any error.

Quoted 23 lines
mm/vmscan.c +5 -4
-       list_for_each_entry(shrinker, &shrinker_list, list) {
+       list_for_each_entry_rcu(shrinker, &shrinker_list, list) {
                struct shrink_control sc = {
                        .gfp_mask = gfp_mask,
                        .nid = nid,
@@ -498,11 +508,13 @@ static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
                if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
                        sc.nid = 0;
 
+               get_shrinker(shrinker);
                freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible);
+               put_shrinker(shrinker);
        }
 
-       up_read(&shrinker_rwsem);
-out:
+       rcu_read_unlock();
+
        cond_resched();
        return freed;
 }
--
Quoted 2 lines

2.15.0.403.gc27cc4dac6-goog

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>