From patchwork Thu Jan 21 23:06:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12037943 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E142C433E6 for ; Thu, 21 Jan 2021 23:06:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 419AB2389F for ; Thu, 21 Jan 2021 23:06:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 419AB2389F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C77096B0012; Thu, 21 Jan 2021 18:06:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C28656B0022; Thu, 21 Jan 2021 18:06:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7D406B0023; Thu, 21 Jan 2021 18:06:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0078.hostedemail.com [216.40.44.78]) by kanga.kvack.org (Postfix) with ESMTP id 865FB6B0012 for ; Thu, 21 Jan 2021 18:06:55 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4AF86181AF5D7 for ; Thu, 21 Jan 2021 23:06:55 +0000 (UTC) X-FDA: 77731319190.19.tiger91_5c1684627567 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin19.hostedemail.com (Postfix) with ESMTP id 33A8A1ACEBC for ; Thu, 21 Jan 2021 23:06:55 +0000 (UTC) X-HE-Tag: tiger91_5c1684627567 X-Filterd-Recvd-Size: 7583 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf38.hostedemail.com (Postfix) with ESMTP for ; Thu, 21 Jan 2021 23:06:54 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id i5so2394527pgo.1 for ; Thu, 21 Jan 2021 15:06:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3SlHfZhSiZNKbs0mjvUox6d28geo/ZvBRCAhNI+IEVc=; b=sDJz2yo6fVhLXppFA6DLaTHwx1TBzkOJPIpEiu9J0oik+Hl6Jdnzr52Nz7mYZFxtwl lQyYLhO+PcOLm5Xg9Gu2HIrGORhm22/JGifPt9t4qtHquP4ORMLfwutzjnYyTdZFBHps nOfABIw8MRYXjFcsZzvgApucTjH6re4Se4lIlrIua9lwe+ZP6u3iS3wEhJ4w8cV1b1Kn xwG+nNpiXQZuHGi2BtGfmu9aSnlgh2djyA5XF8n0NqMclHGN0WWyuS5K5cD7Apd05kue +KxQ56Tbe8sgrdPGkAOcUO86FaDS7ZO9hRHqzXqDZTGoqhXdYlplbyXZGLSwFq/ZHaVI jSSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3SlHfZhSiZNKbs0mjvUox6d28geo/ZvBRCAhNI+IEVc=; b=OpV+f9MGK8JqUNPc/TXtI1MAcn/U7Cjo8xUA7pvbAw/FpvoBIZDVaplfwDcgu2qC8g ponmhq0LUWk855j0hLO28tTYS37eKppRegKo560IrExHzXEynx+OmIaSlj5u5SD+uA6x puuAwyTMTrLuPzGSjhhP2DOY7YbJrbesMBVEPcJm6/yufrHZJtnzBnt6oUol9XvDPAbG tYF9EfvRajfKk6L4uOnwxMkBRIrX1a8n6k1aDs1QRx+3EOFcZXfkklomojrjPRxjqkmu huCPVmgZOk4X8V9Xrz+lclRyjWGmvlH+R07AHUp/fniJ/O80uDGUXIjy486S2V8ICOhG Yh+Q== X-Gm-Message-State: AOAM5327FyYYPdKBWLVxG4m0t205JOWyrKlP/n9wjKQuxoLapsLcuYNZ A0komy9RWHRr4brD9FGHXIU= X-Google-Smtp-Source: ABdhPJwIU5G2rXN+/5fiO72dLGOTVGko9BsBg3ezB7RegtX65G41XVMMa1YdR7nAzZtQ8X/DwgrLUQ== X-Received: by 2002:a62:5207:0:b029:1b7:6c49:f971 with SMTP id g7-20020a6252070000b02901b76c49f971mr1836224pfb.23.1611270413916; Thu, 21 Jan 2021 15:06:53 -0800 (PST) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id y16sm6722921pfb.83.2021.01.21.15.06.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jan 2021 15:06:52 -0800 (PST) From: Yang Shi To: guro@fb.com, ktkhai@virtuozzo.com, shakeelb@google.com, david@fromorbit.com, hannes@cmpxchg.org, mhocko@suse.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 08/11] mm: vmscan: use per memcg nr_deferred of shrinker Date: Thu, 21 Jan 2021 15:06:18 -0800 Message-Id: <20210121230621.654304-9-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210121230621.654304-1-shy828301@gmail.com> References: <20210121230621.654304-1-shy828301@gmail.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use per memcg's nr_deferred for memcg aware shrinkers. The shrinker's nr_deferred will be used in the following cases: 1. Non memcg aware shrinkers 2. !CONFIG_MEMCG 3. memcg is disabled by boot parameter Signed-off-by: Yang Shi --- mm/vmscan.c | 81 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 722aa71b13b2..d8e77ea13815 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -359,6 +359,27 @@ static void unregister_memcg_shrinker(struct shrinker *shrinker) up_write(&shrinker_rwsem); } +static long count_nr_deferred_memcg(int nid, struct shrinker *shrinker, + struct mem_cgroup *memcg) +{ + struct shrinker_info *info; + + info = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, + true); + return atomic_long_xchg(&info->nr_deferred[shrinker->id], 0); +} + +static long set_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, + struct mem_cgroup *memcg) +{ + struct shrinker_info *info; + + info = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_info, + true); + + return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); +} + static bool cgroup_reclaim(struct scan_control *sc) { return sc->target_mem_cgroup; @@ -397,6 +418,18 @@ static void unregister_memcg_shrinker(struct shrinker *shrinker) { } +static long count_nr_deferred_memcg(int nid, struct shrinker *shrinker, + struct mem_cgroup *memcg) +{ + return 0; +} + +static long set_nr_deferred_memcg(long nr, int nid, struct shrinker *shrinker, + struct mem_cgroup *memcg) +{ + return 0; +} + static bool cgroup_reclaim(struct scan_control *sc) { return false; @@ -408,6 +441,39 @@ static bool writeback_throttling_sane(struct scan_control *sc) } #endif +static long count_nr_deferred(struct shrinker *shrinker, + struct shrink_control *sc) +{ + int nid = sc->nid; + + if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) + nid = 0; + + if (sc->memcg && + (shrinker->flags & SHRINKER_MEMCG_AWARE)) + return count_nr_deferred_memcg(nid, shrinker, + sc->memcg); + + return atomic_long_xchg(&shrinker->nr_deferred[nid], 0); +} + + +static long set_nr_deferred(long nr, struct shrinker *shrinker, + struct shrink_control *sc) +{ + int nid = sc->nid; + + if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) + nid = 0; + + if (sc->memcg && + (shrinker->flags & SHRINKER_MEMCG_AWARE)) + return set_nr_deferred_memcg(nr, nid, shrinker, + sc->memcg); + + return atomic_long_add_return(nr, &shrinker->nr_deferred[nid]); +} + /* * This misses isolated pages which are not accounted for to save counters. * As the data only determines if reclaim or compaction continues, it is @@ -544,14 +610,10 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, long freeable; long nr; long new_nr; - int nid = shrinkctl->nid; long batch_size = shrinker->batch ? shrinker->batch : SHRINK_BATCH; long scanned = 0, next_deferred; - if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) - nid = 0; - freeable = shrinker->count_objects(shrinker, shrinkctl); if (freeable == 0 || freeable == SHRINK_EMPTY) return freeable; @@ -561,7 +623,7 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, * and zero it so that other concurrent shrinker invocations * don't also do this scanning work. */ - nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); + nr = count_nr_deferred(shrinker, shrinkctl); total_scan = nr; if (shrinker->seeks) { @@ -652,14 +714,9 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl, next_deferred = 0; /* * move the unused scan count back into the shrinker in a - * manner that handles concurrent updates. If we exhausted the - * scan, there is no need to do an update. + * manner that handles concurrent updates. */ - if (next_deferred > 0) - new_nr = atomic_long_add_return(next_deferred, - &shrinker->nr_deferred[nid]); - else - new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); + new_nr = set_nr_deferred(next_deferred, shrinker, shrinkctl); trace_mm_shrink_slab_end(shrinker, shrinkctl->nid, freed, nr, new_nr, total_scan); return freed;