From mboxrd@z Thu Jan 1 00:00:00 1970 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=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 CA832C433E0 for ; Mon, 25 Jan 2021 21:08:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 422A4207B1 for ; Mon, 25 Jan 2021 21:08:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 422A4207B1 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 9A0298D004A; Mon, 25 Jan 2021 16:08:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 92B148D0023; Mon, 25 Jan 2021 16:08:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CB0E8D004A; Mon, 25 Jan 2021 16:08:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 641E38D0023 for ; Mon, 25 Jan 2021 16:08:37 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 27A5F1EF1 for ; Mon, 25 Jan 2021 21:08:37 +0000 (UTC) X-FDA: 77745536274.16.board98_2a00a6327588 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin16.hostedemail.com (Postfix) with ESMTP id 07DD2100E690C for ; Mon, 25 Jan 2021 21:08:37 +0000 (UTC) X-HE-Tag: board98_2a00a6327588 X-Filterd-Recvd-Size: 11543 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Mon, 25 Jan 2021 21:08:36 +0000 (UTC) Received: by mail-ej1-f47.google.com with SMTP id by1so20090773ejc.0 for ; Mon, 25 Jan 2021 13:08:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bNXeXc3vGhxTCm0XPiMscGdXWu7Ggi1oyXI1subSlPc=; b=eNT5m3XBY71AOp5C+JofQOqSiVSnVIHplG8Hv4i4ZXn0o3BHGJ68hHRKj/jS3KgtW9 tKhGTx+B9nQkKCdr76/T+W0hf7jUvHT/cMdrBiq0QPDYooOKCCVQDLTGAjtV5OLNulqq MtLz2U3ObznDzaFsCT/1ArxlbsiSZq9wedFqEiwylDJHJ+pNGSmDBklA2LJvqbcPIpq3 rSVtuWp6B1N9FnRWY8+0mheqDDGnyWJBXK6XOepbN+yltO58LL4C23mmlZEhpV942pQQ MdNToxw4s+8ADzWI+fHC+LL0I02wjVHIu61oOuv6BseNeg/PQ9pkIVNpXTEIRto6hGQC vZlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bNXeXc3vGhxTCm0XPiMscGdXWu7Ggi1oyXI1subSlPc=; b=lYXPVRHY1/oTa4dJofzxhWSK7WCkkwOUJbTxG0rFOo3U6g0YrajeycmEF8muGQAqdD 5bQGBs2kN95LTetBva/STw5AodQmz3Sqg/kZH0ZrP+wsCyiRhP47vC7ltIBY+0RU5yTq kuunVPUpOKo2eoYZ8vBNXFjhSQ0LdigqVXj/HVH1coaba1whbXRth+S+6lvPGnFCmcJM A60W0oP6BQ2K1BWrndbrqUueQX0bRs9IWwJfd252WAck9vrSwYLXw36yP/i43YdpCU5v Gm5rdCIya7uFIQ2xPXYT8QAhT2sc5simQQP8ShzUpHUxBuaxucraFAbskhCx15BYCov6 bB4Q== X-Gm-Message-State: AOAM530MCFIP0wC4MaM8i0HUQUAHsnSsmwVsO2w986STGOfQmdN8iFss w/k80ltBfDDFKAh02abzHnv3kFbRl9lbG3DTJpw= X-Google-Smtp-Source: ABdhPJwhZLoyEyyuQP9vDiDAuzHGfDislX6Yw/pL7fd9JAcf0IFFTYreblvhZn3h5HVUFyQ9FMmMbPGDFxdk4xCN9K8= X-Received: by 2002:a17:906:f841:: with SMTP id ks1mr1477717ejb.507.1611608915557; Mon, 25 Jan 2021 13:08:35 -0800 (PST) MIME-Version: 1.0 References: <20210121230621.654304-1-shy828301@gmail.com> <20210121230621.654304-8-shy828301@gmail.com> <1c621cd8-7d13-ddfa-bb83-d4260a1bb754@virtuozzo.com> In-Reply-To: <1c621cd8-7d13-ddfa-bb83-d4260a1bb754@virtuozzo.com> From: Yang Shi Date: Mon, 25 Jan 2021 13:08:23 -0800 Message-ID: Subject: Re: [v4 PATCH 07/11] mm: vmscan: add per memcg shrinker nr_deferred To: Kirill Tkhai Cc: Roman Gushchin , Shakeel Butt , Dave Chinner , Johannes Weiner , Michal Hocko , Andrew Morton , Linux MM , Linux FS-devel Mailing List , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" 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: On Mon, Jan 25, 2021 at 1:31 AM Kirill Tkhai wrote: > > On 22.01.2021 02:06, Yang Shi wrote: > > Currently the number of deferred objects are per shrinker, but some slabs, for example, > > vfs inode/dentry cache are per memcg, this would result in poor isolation among memcgs. > > > > The deferred objects typically are generated by __GFP_NOFS allocations, one memcg with > > excessive __GFP_NOFS allocations may blow up deferred objects, then other innocent memcgs > > may suffer from over shrink, excessive reclaim latency, etc. > > > > For example, two workloads run in memcgA and memcgB respectively, workload in B is vfs > > heavy workload. Workload in A generates excessive deferred objects, then B's vfs cache > > might be hit heavily (drop half of caches) by B's limit reclaim or global reclaim. > > > > We observed this hit in our production environment which was running vfs heavy workload > > shown as the below tracing log: > > > > <...>-409454 [016] .... 28286961.747146: mm_shrink_slab_start: super_cache_scan+0x0/0x1a0 ffff9a83046f3458: > > nid: 1 objects to shrink 3641681686040 gfp_flags GFP_HIGHUSER_MOVABLE|__GFP_ZERO pgs_scanned 1 lru_pgs 15721 > > cache items 246404277 delta 31345 total_scan 123202138 > > <...>-409454 [022] .... 28287105.928018: mm_shrink_slab_end: super_cache_scan+0x0/0x1a0 ffff9a83046f3458: > > nid: 1 unused scan count 3641681686040 new scan count 3641798379189 total_scan 602 > > last shrinker return val 123186855 > > > > The vfs cache and page cache ration was 10:1 on this machine, and half of caches were dropped. > > This also resulted in significant amount of page caches were dropped due to inodes eviction. > > > > Make nr_deferred per memcg for memcg aware shrinkers would solve the unfairness and bring > > better isolation. > > > > When memcg is not enabled (!CONFIG_MEMCG or memcg disabled), the shrinker's nr_deferred > > would be used. And non memcg aware shrinkers use shrinker's nr_deferred all the time. > > > > Signed-off-by: Yang Shi > > --- > > include/linux/memcontrol.h | 7 +++--- > > mm/vmscan.c | 49 +++++++++++++++++++++++++------------- > > 2 files changed, 36 insertions(+), 20 deletions(-) > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > index 62b888b88a5f..e0384367e07d 100644 > > --- a/include/linux/memcontrol.h > > +++ b/include/linux/memcontrol.h > > @@ -93,12 +93,13 @@ struct lruvec_stat { > > }; > > > > /* > > - * Bitmap of shrinker::id corresponding to memcg-aware shrinkers, > > - * which have elements charged to this memcg. > > + * Bitmap and deferred work of shrinker::id corresponding to memcg-aware > > + * shrinkers, which have elements charged to this memcg. > > */ > > struct shrinker_info { > > struct rcu_head rcu; > > - unsigned long map[]; > > + unsigned long *map; > > + atomic_long_t *nr_deferred; > > }; > > > > /* > > diff --git a/mm/vmscan.c b/mm/vmscan.c > > index 018e1beb24c9..722aa71b13b2 100644 > > --- a/mm/vmscan.c > > +++ b/mm/vmscan.c > > @@ -192,11 +192,13 @@ static void free_shrinker_info_rcu(struct rcu_head *head) > > kvfree(container_of(head, struct shrinker_info, rcu)); > > } > > > > -static int expand_one_shrinker_info(struct mem_cgroup *memcg, > > - int size, int old_size) > > +static int expand_one_shrinker_info(struct mem_cgroup *memcg, int nr_max, > > + int m_size, int d_size, > > + int old_m_size, int old_d_size) > > { > > struct shrinker_info *new, *old; > > int nid; > > + int size = m_size + d_size; > > > > for_each_node(nid) { > > old = rcu_dereference_protected( > > @@ -209,9 +211,16 @@ static int expand_one_shrinker_info(struct mem_cgroup *memcg, > > if (!new) > > return -ENOMEM; > > > > - /* Set all old bits, clear all new bits */ > > - memset(new->map, (int)0xff, old_size); > > - memset((void *)new->map + old_size, 0, size - old_size); > > + new->map = (unsigned long *)(new + 1); > > + new->nr_deferred = (atomic_long_t *)(new->map + > > + nr_max / BITS_PER_LONG + 1); > > Why not > > new->nr_deferred = (void *)new->map + m_size; > ? > > > + > > + /* map: set all old bits, clear all new bits */ > > + memset(new->map, (int)0xff, old_m_size); > > + memset((void *)new->map + old_m_size, 0, m_size - old_m_size); > > + /* nr_deferred: copy old values, clear all new values */ > > + memcpy(new->nr_deferred, old->nr_deferred, old_d_size); > > + memset((void *)new->nr_deferred + old_d_size, 0, d_size - old_d_size); > > > > rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, new); > > call_rcu(&old->rcu, free_shrinker_info_rcu); > > @@ -226,9 +235,6 @@ void free_shrinker_info(struct mem_cgroup *memcg) > > struct shrinker_info *info; > > int nid; > > > > - if (mem_cgroup_is_root(memcg)) > > - return; > > - > > for_each_node(nid) { > > pn = mem_cgroup_nodeinfo(memcg, nid); > > info = rcu_dereference_protected(pn->shrinker_info, true); > > @@ -242,12 +248,13 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) > > { > > struct shrinker_info *info; > > int nid, size, ret = 0; > > - > > - if (mem_cgroup_is_root(memcg)) > > - return 0; > > + int m_size, d_size = 0; > > > > down_write(&shrinker_rwsem); > > - size = (shrinker_nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long); > > + m_size = (shrinker_nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long); > > + d_size = shrinker_nr_max * sizeof(atomic_long_t); > > + size = m_size + d_size; > > + > > for_each_node(nid) { > > info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); > > if (!info) { > > @@ -255,6 +262,9 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) > > ret = -ENOMEM; > > break; > > } > > + info->map = (unsigned long *)(info + 1); > > + info->nr_deferred = (atomic_long_t *)(info->map + > > + shrinker_nr_max / BITS_PER_LONG + 1); > > Why not: > info->nr_deferred = (void*)info->map + m_size; Yes, definitely. Will fix in v5. > ? > > > rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); > > } > > up_write(&shrinker_rwsem); > > @@ -266,10 +276,16 @@ static int expand_shrinker_info(int new_id) > > { > > int size, old_size, ret = 0; > > int new_nr_max = new_id + 1; > > + int m_size, d_size = 0; > > + int old_m_size, old_d_size = 0; > > struct mem_cgroup *memcg; > > > > - size = (new_nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long); > > - old_size = (shrinker_nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long); > > + m_size = (new_nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long); > > + d_size = new_nr_max * sizeof(atomic_long_t); > > + size = m_size + d_size; > > + old_m_size = (shrinker_nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long); > > Could you please pack this twice repeating pattern into some macro? E.g., > > #define NR_MAX_TO_SHR_MAP_SIZE(nr_max) \ > ((nr_max / BITS_PER_LONG + 1) * sizeof(unsigned long)) Sure. Will incorporate in v5. > > > + old_d_size = shrinker_nr_max * sizeof(atomic_long_t); > > + old_size = old_m_size + old_d_size; > > if (size <= old_size) > > return 0; > > > > @@ -278,9 +294,8 @@ static int expand_shrinker_info(int new_id) > > > > memcg = mem_cgroup_iter(NULL, NULL, NULL); > > do { > > - if (mem_cgroup_is_root(memcg)) > > - continue; > > - ret = expand_one_shrinker_info(memcg, size, old_size); > > + ret = expand_one_shrinker_info(memcg, new_nr_max, m_size, d_size, > > + old_m_size, old_d_size); > > if (ret) { > > mem_cgroup_iter_break(NULL, memcg); > > goto out; > > > >