From patchwork Wed Jan 27 23:33:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12051223 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 AEF9BC433E0 for ; Wed, 27 Jan 2021 23:34:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5C13464D7F for ; Wed, 27 Jan 2021 23:34:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5C13464D7F 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 009416B0080; Wed, 27 Jan 2021 18:34:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EFA7A6B007D; Wed, 27 Jan 2021 18:34:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFAFC6B007E; Wed, 27 Jan 2021 18:34:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0171.hostedemail.com [216.40.44.171]) by kanga.kvack.org (Postfix) with ESMTP id C9AA36B0078 for ; Wed, 27 Jan 2021 18:34:12 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 92EF1181AEF32 for ; Wed, 27 Jan 2021 23:34:12 +0000 (UTC) X-FDA: 77753160744.22.brick92_441216e2759b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 3CB861803B8DC for ; Wed, 27 Jan 2021 23:34:10 +0000 (UTC) X-HE-Tag: brick92_441216e2759b X-Filterd-Recvd-Size: 8333 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf37.hostedemail.com (Postfix) with ESMTP for ; Wed, 27 Jan 2021 23:34:09 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id u4so2681514pjn.4 for ; Wed, 27 Jan 2021 15:34:09 -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=KjfIUTm7EyLVGh5kUmhdOqVLNJ4R1Pj2IbptP7JZXIY=; b=CJGu6zGoPLDDnlpgOZuwWPp5CWHGWPlM7mMwoQ0ox5SqOSYYVS5mq9g8fqQEiW9V2H XgRxKyb1+rTefGeCguck/SZyFuSAXs9DnQjGQNpl7we4T/YKjYZ6uBdDkb7EEc4X4PtM YbK8jzIXjJKyq5w27JRArLsiyxFfepRLTdq45qlJjRtNOZlzqqJNcdVlhKFb0gwGOhBg F2eMiUlzjiGI0mhL3gz2cEG1CL50nND1/42YUNuOgIRDVnxffBzoI4DueKCvpPn+pU3S CdqkmaTB+n0BgXDkuJ5F1ufWkQOxEnzai+vb7GmdMYUCRcvdAnxn3GeG+MJZK4K1leYE 4Plw== 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=KjfIUTm7EyLVGh5kUmhdOqVLNJ4R1Pj2IbptP7JZXIY=; b=a85iuaoKLj06UAMd0JZxS5x25K8Rv+ywJGWEy6kdLXJjaDbITp8Y1bji7WPs5cVfyG KwkgzOmHtmgUePLiN60KKhf4Wc2pmVPRW/1A1zo5X/BRaiPFIVw3wU8qEYlorpEQiAS/ FBYd5tQKVmn384h5u7rH1ek/3psfGRIFrrkMoR7Ui8L0zuZAdUxqDdnh3rausdx+cb5n HZDZGO8cJeONBA5a06IKl7xrzoIqlOLQvG+HhSb10Z02FHmlClCd6HrorGFP4Qy64KUX CTcNBjzQW55qvATTb03GDog+mIc7ERHVBSDXBkH5BdzKyMdYDQMQAMN5aKHFLYR1e2/m HwCg== X-Gm-Message-State: AOAM530rz9ZmZlDSNXF0b9vw8yWWNc66/597lBsaAaXOAwk+Caa5BZBT IVcE6K1nEz3L8d3MBhInBfw= X-Google-Smtp-Source: ABdhPJx+sxQ8J5zuiqS4QK0aWVfSSOBe71N3VVys+iKEeQQxQLYUqN96B/84mTeDUBCUzzatPEJ2fQ== X-Received: by 2002:a17:90a:da02:: with SMTP id e2mr8105516pjv.173.1611790448927; Wed, 27 Jan 2021 15:34:08 -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 124sm3498648pfd.59.2021.01.27.15.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Jan 2021 15:34:08 -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: [v5 PATCH 08/11] mm: vmscan: use per memcg nr_deferred of shrinker Date: Wed, 27 Jan 2021 15:33:42 -0800 Message-Id: <20210127233345.339910-9-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210127233345.339910-1-shy828301@gmail.com> References: <20210127233345.339910-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 | 87 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 14 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 20be0db291fe..e1f8960f5cf6 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -205,7 +205,8 @@ static int expand_one_shrinker_info(struct mem_cgroup *memcg, for_each_node(nid) { old = rcu_dereference_protected( - mem_cgroup_nodeinfo(memcg, nid)->shrinker_info, true); + mem_cgroup_nodeinfo(memcg, nid)->shrinker_info, + lockdep_is_held(&shrinker_rwsem)); /* Not yet online memcg */ if (!old) return 0; @@ -239,7 +240,8 @@ void free_shrinker_info(struct mem_cgroup *memcg) for_each_node(nid) { pn = mem_cgroup_nodeinfo(memcg, nid); - info = rcu_dereference_protected(pn->shrinker_info, true); + info = rcu_dereference_protected(pn->shrinker_info, + lockdep_is_held(&shrinker_rwsem)); if (info) kvfree(info); rcu_assign_pointer(pn->shrinker_info, NULL); @@ -360,6 +362,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, + lockdep_is_held(&shrinker_rwsem)); + 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, + lockdep_is_held(&shrinker_rwsem)); + + return atomic_long_add_return(nr, &info->nr_deferred[shrinker->id]); +} + static bool cgroup_reclaim(struct scan_control *sc) { return sc->target_mem_cgroup; @@ -398,6 +421,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; @@ -409,6 +444,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 @@ -545,14 +613,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; @@ -562,7 +626,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) { @@ -653,14 +717,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;