Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2044640pxb; Mon, 8 Mar 2021 12:34:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7UPIMvpmVQtG/zl8znePagEc1Da9jEq4PAXdP3RY4df9f3OEFo0qh3FC3VmXmCcCJB+Om X-Received: by 2002:a50:ed83:: with SMTP id h3mr426552edr.140.1615235674174; Mon, 08 Mar 2021 12:34:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615235674; cv=none; d=google.com; s=arc-20160816; b=EtQMaVTqtmnG18K/q7GtWbfZCEbPxNOjsimSGKT/wlW+Ct4/vaJl7rmRkPgeIsSnhW TFjWnIaGvEm/n3eikOvkCOQv1SvayY6rI9iGy+NcFmmBig7w5X/wsiRc37QS0q65klrd ZDBsXniXjTYvDbXI0l9yx0DU/MHKdBdRSmvXXRL9bcL9JgN3NHbebIRTUGg2K/OznjZs agjZ8AGZhn+H6+wIHKeZ9Lu8Th1RsF1Imweqh+EL4XpLhEHDFtPGENKa84SbybZ07RYw AsG9+b5Znn/fVDhl7PXCkR2zbPvap3VrNiGdG4XoKCVr4YwF7a1E7chVFpy1Ub9V4glH qHLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Bng89PIihxzY7Jr/zcM+BtxNNqk4o89SmQXzo/gPHmU=; b=MJmB8gh+nkIlyG4n7aexwfF9r3X8bnaKVwHrs1ck9KJMnLvdC2yrCNQIaDMvpoEkSF Lt0er4uEpePGjXXxOJvmn+ADA8eF00K0/oTf6y6RTWsgFHGpSPp8IpieQ3CNygZ0gVA/ 2XhVmIWKFONQL8hBEaP1ZDj261nToxSvuGxpc74wkhbivfb4ZK3oRreU7pnm3LKMMVAh lI3hGuKpQaVz1Dyb/GEOSrmsXFFmeyeA08TXN6FjwIFeaHQ75A7Rttq+uaO3TMW34PNv NzBMulfjAICCOduYoElkqJeZSXw9AzhK7uqDfKmE4o6/LJ3ODXhMO3ykbDM/ynsZHUff JMhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QbbEPERB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jl16si7197360ejc.330.2021.03.08.12.34.10; Mon, 08 Mar 2021 12:34:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QbbEPERB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230341AbhCHUa6 (ORCPT + 99 others); Mon, 8 Mar 2021 15:30:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229756AbhCHUao (ORCPT ); Mon, 8 Mar 2021 15:30:44 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10503C06174A; Mon, 8 Mar 2021 12:30:44 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id r17so22964621ejy.13; Mon, 08 Mar 2021 12:30:43 -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=Bng89PIihxzY7Jr/zcM+BtxNNqk4o89SmQXzo/gPHmU=; b=QbbEPERB65dedudzcs6DWkwg4Eeg+TXLOZVdLXE+Z11qwfm/V27fEwxC0aajud/bO8 OssuGoTO0ZOTHAt2lMpfldVhx4eW4eYr7X/vG+sqfTBb8SXsM9Cdi7vt78ste/laRmZ/ Sy6qUd5Mb1SMV2LwDYVFZk5NYrjSfOYVcV+Y0FoEzeckY/V2hgkLrqO79sbWUOUEWa7M 9S8VRymQaWCaZDY7QeDRkkLe2MXE6SvSNZX0ON8VWtW36GRXV20Gd/0WSMeLbHTyL6dO eG3z0+WsVSQE1SNBtSXHesZAgflfqMon+fR1yIcDjHAAXcUSRTAe6xQB8QIrZ4jVjzbB 3yfw== 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=Bng89PIihxzY7Jr/zcM+BtxNNqk4o89SmQXzo/gPHmU=; b=OpEWKu0idawIWILRcYdJfvqMEsojB+FiqRYsCkBDeNV3ZiLQ1XiFrIkf6t0dqRA2Lt MpF3QJ+lOAfUrksWO1KTLbG8e5KkKXuS02ywO8l5pr6CtCG9X1rKp5Bpfn2tTUKIus9A d/25stq2ok1ap7+ZorTIJbc64PGBw4SbM4OnVTXapcSHmT8j4pF3WxTncy8+Uwbc0wZQ IUxbU0s0Ur/Ax16Ax+qfjOS5OwNZreoecrwUeoUeKZQ5WDozmE6QPydzVp11doY6lXA4 /g5hZp5cu+fPOGCjv4k++1/AKtO5FENyLsCGqq/S2eIQsKCx8ClYyCHW87D1CbCvf7YB DS6g== X-Gm-Message-State: AOAM531M2wsapkbOpnvFIEhs+VXamO7yxTitenyRuWTP6nQ4BQ6DNueb eu35UcIHHkGI4S7lNrsCIgA2p83UI6r76QuOXhU= X-Received: by 2002:a17:906:304a:: with SMTP id d10mr16667995ejd.507.1615235442779; Mon, 08 Mar 2021 12:30:42 -0800 (PST) MIME-Version: 1.0 References: <20210217001322.2226796-1-shy828301@gmail.com> <20210217001322.2226796-10-shy828301@gmail.com> In-Reply-To: From: Yang Shi Date: Mon, 8 Mar 2021 12:30:31 -0800 Message-ID: Subject: Re: [v8 PATCH 09/13] mm: vmscan: add per memcg shrinker nr_deferred To: Shakeel Butt Cc: Roman Gushchin , Kirill Tkhai , Vlastimil Babka , Dave Chinner , Johannes Weiner , Michal Hocko , Andrew Morton , Linux MM , linux-fsdevel , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 8, 2021 at 11:12 AM Shakeel Butt wrote: > > On Tue, Feb 16, 2021 at 4:13 PM 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 ratio 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 | 60 ++++++++++++++++++++++++++------------ > > 2 files changed, 46 insertions(+), 21 deletions(-) > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > index 4c9253896e25..c457fc7bc631 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[]; > > + atomic_long_t *nr_deferred; > > + unsigned long *map; > > }; > > > > /* > > diff --git a/mm/vmscan.c b/mm/vmscan.c > > index a1047ea60ecf..fcb399e18fc3 100644 > > --- a/mm/vmscan.c > > +++ b/mm/vmscan.c > > @@ -187,11 +187,17 @@ static DECLARE_RWSEM(shrinker_rwsem); > > #ifdef CONFIG_MEMCG > > static int shrinker_nr_max; > > > > +/* The shrinker_info is expanded in a batch of BITS_PER_LONG */ > > static inline int shrinker_map_size(int nr_items) > > { > > return (DIV_ROUND_UP(nr_items, BITS_PER_LONG) * sizeof(unsigned long)); > > } > > > > +static inline int shrinker_defer_size(int nr_items) > > +{ > > + return (round_up(nr_items, BITS_PER_LONG) * sizeof(atomic_long_t)); > > +} > > + > > static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, > > int nid) > > { > > @@ -200,10 +206,12 @@ static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg, > > } > > > > static int expand_one_shrinker_info(struct mem_cgroup *memcg, > > - int size, int old_size) > > + int map_size, int defer_size, > > + int old_map_size, int old_defer_size) > > { > > struct shrinker_info *new, *old; > > int nid; > > + int size = map_size + defer_size; > > > > for_each_node(nid) { > > old = shrinker_info_protected(memcg, nid); > > @@ -215,9 +223,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->nr_deferred = (atomic_long_t *)(new + 1); > > + new->map = (void *)new->nr_deferred + defer_size; > > + > > + /* map: set all old bits, clear all new bits */ > > + memset(new->map, (int)0xff, old_map_size); > > + memset((void *)new->map + old_map_size, 0, map_size - old_map_size); > > + /* nr_deferred: copy old values, clear all new values */ > > + memcpy(new->nr_deferred, old->nr_deferred, old_defer_size); > > + memset((void *)new->nr_deferred + old_defer_size, 0, > > + defer_size - old_defer_size); > > > > rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, new); > > kvfree_rcu(old); > > @@ -232,9 +247,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 = shrinker_info_protected(memcg, nid); > > @@ -247,12 +259,12 @@ 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; > > Can you please comment on the consequences on allowing to allocate > shrinker_info for root memcg? Why didn't we do that before but now it > is fine (or maybe required)? Please add the explanation in the commit > message. Before the patchset shrinker_info just tracks shrinker_maps which is not required for root memcg. But the newly added nr_deferred is needed in root memcg otherwise the nr_deferred work would get lost once the memcgs are reparented to root. How's about adding the below paragraph to the commit log: "To preserve nr_deferred when reparenting memcgs to root, root memcg needs shrinker_info allocated too." > > > + int map_size, defer_size = 0; > > > > down_write(&shrinker_rwsem); > > - size = shrinker_map_size(shrinker_nr_max); > > + map_size = shrinker_map_size(shrinker_nr_max); > > + defer_size = shrinker_defer_size(shrinker_nr_max); > > + size = map_size + defer_size; > > for_each_node(nid) { > > info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid); > > if (!info) { > > @@ -260,6 +272,8 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) > > ret = -ENOMEM; > > break; > > } > > + info->nr_deferred = (atomic_long_t *)(info + 1); > > + info->map = (void *)info->nr_deferred + defer_size; > > rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info); > > } > > up_write(&shrinker_rwsem); > > @@ -267,15 +281,21 @@ int alloc_shrinker_info(struct mem_cgroup *memcg) > > return ret; > > } > > > > +static inline bool need_expand(int nr_max) > > +{ > > + return round_up(nr_max, BITS_PER_LONG) > > > + round_up(shrinker_nr_max, BITS_PER_LONG); > > +} > > + > > static int expand_shrinker_info(int new_id) > > { > > - int size, old_size, ret = 0; > > + int ret = 0; > > int new_nr_max = new_id + 1; > > + int map_size, defer_size = 0; > > + int old_map_size, old_defer_size = 0; > > struct mem_cgroup *memcg; > > > > - size = shrinker_map_size(new_nr_max); > > - old_size = shrinker_map_size(shrinker_nr_max); > > - if (size <= old_size) > > + if (!need_expand(new_nr_max)) > > goto out; > > > > if (!root_mem_cgroup) > > @@ -283,11 +303,15 @@ static int expand_shrinker_info(int new_id) > > > > lockdep_assert_held(&shrinker_rwsem); > > > > + map_size = shrinker_map_size(new_nr_max); > > + defer_size = shrinker_defer_size(new_nr_max); > > + old_map_size = shrinker_map_size(shrinker_nr_max); > > + old_defer_size = shrinker_defer_size(shrinker_nr_max); > > + > > 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, map_size, defer_size, > > + old_map_size, old_defer_size); > > if (ret) { > > mem_cgroup_iter_break(NULL, memcg); > > goto out; > > -- > > 2.26.2 > >