Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp642235pxf; Thu, 11 Mar 2021 11:12:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwC7IDIXwJcn1bai2I879Kny4hNEFEAudeaVv2h5oF+D+0x4J8In1p8XA5VRu88AM0avsj X-Received: by 2002:a05:6402:32a:: with SMTP id q10mr9942501edw.15.1615489954892; Thu, 11 Mar 2021 11:12:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615489954; cv=none; d=google.com; s=arc-20160816; b=Um83alQDcXGmBwhaJkO58ENSS9Vy74o3bzj8uVqmEBYFpjbquVfGUa9XE/oNQwGBkA KbKq0E+FcomOBD+Qv3qxYuv49K90HUKLNjT1T6Hh3Np0tOOFJzhVZ3U5Ab/Bgh7EGewy VNOj9wmKsBQLxAgM7foXczIeoW3S9Yr1jx5NRvl00M2onidMcPZOecNWuectlY/WueK9 YrmZEx9RBG/KO9OveNpYLSflotFmFToDnZ+ks0ltyaHofTS6YJ0yW3j2O/2xOdnQTtJE PamOPu8QAWsFwPjBSK7SxmYqyJo01q67VOVZkFlmPU8jLSuYqkHuM2+psexIPECOqO3x z+NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O2soj3VRJNBvxi9PWCn4DecTfTo8NOmelRWHo0xmQP0=; b=F46iekg1RzUZKo78CiMjns3sFPdrYpBqJWSXS9UQSzOciTekzTVuQ8Cwrj5nixZ+06 HrdivLf9RTAiqYPXH7fd0AYb78kfte3CINOhzecuoNFd52XPaXt56u/08Vpye75BGFiR Ca4TnP4+AH79L0L8XqRwBAOTJFa91grUFnivF7L6ABJ/Tdj4yyVakYagI1F5ah9Woy98 8COuIlswmIE1fAZX/GWhOqHUNfBiV82N0W0ub4nkcNzlMOZ69LAzK5xCDmGAO83HfcBE 6dValp80qSgZFdC5hHO6lmeskIZJA6QMEmcHh00+fO8PZUpqtv7wyxgzN8EQpAxvMo5K PKuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uRBsx+Ka; 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 g11si2335913ejd.733.2021.03.11.11.12.10; Thu, 11 Mar 2021 11:12: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=uRBsx+Ka; 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 S230511AbhCKTKD (ORCPT + 99 others); Thu, 11 Mar 2021 14:10:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbhCKTJU (ORCPT ); Thu, 11 Mar 2021 14:09:20 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2EF9C061574; Thu, 11 Mar 2021 11:09:19 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id a24so10679609plm.11; Thu, 11 Mar 2021 11:09:19 -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=O2soj3VRJNBvxi9PWCn4DecTfTo8NOmelRWHo0xmQP0=; b=uRBsx+Kax6NkZ9uc+8SG6XZVcl8rkcflBM04WQkKe1hczZa1EixUiP1LbAu/TXH5hg aubdIpn0WNho5jsbSkooNSxqL+0DoAV7/yLncRMbbAPHNrWjm0Gx0FQ7c/1xiSsF0T7w NB1Cnzo8DSLhgxKQt7ME2aPCPXHczsjcj6JDCf4SK1FyAU2ZoLaSanGJLs89l2972BFU ns5j3EUPu5KWJXRwJ83uO2U93T1inYVPo7DB7nLqglpWDOIEky7EVHBnbI73wmB8P8Cp WvhGvR4WOhdI6nKogLkB1VXMSkTfFg/FEdhqY8kvdoWm5K6DD4F0QHchHO42vyknUHq0 2Chw== 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=O2soj3VRJNBvxi9PWCn4DecTfTo8NOmelRWHo0xmQP0=; b=JxiIKglifiH09/N52sE9koody0hFHz4mkOSpxYwo82O4F81KRxCcTxW5tb/3/I+XOW 7lX1Ae/an7dHslxhUmDU7N9wm5wONar6D4LRjzXDleyhX/yKWvq7D9MtoMFAJjPA7bRT pqp6ad3miyeGx6scp05OApjlPARzVTRGikyPClSQIg67se8Y9XgzMj1h0WNlOcQW+nX8 KJOk2mLDWASmgnUNki7+RmsorMacRbo291JYzJbfbIDuuXFPbaamsrZgOuw6H9RaH+/5 q12S80GmLIl0sKO8tTl0A+aCADg57Dq6w6p45wfwLq70idvfMcodOyAkF4Lofczo6wpj qXuQ== X-Gm-Message-State: AOAM533ncRW2cFq2sUxdLdxe8M2bAzQ0ufprTozFnLC/8SS3vdnQxryP 1akzK0o3A6qtGLSmnQ2valg= X-Received: by 2002:a17:902:aa0a:b029:e4:c090:ad76 with SMTP id be10-20020a170902aa0ab02900e4c090ad76mr9687150plb.2.1615489759072; Thu, 11 Mar 2021 11:09:19 -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 t12sm2999111pfe.203.2021.03.11.11.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Mar 2021 11:09:18 -0800 (PST) From: Yang Shi To: guro@fb.com, ktkhai@virtuozzo.com, vbabka@suse.cz, 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: [v10 PATCH 09/13] mm: vmscan: add per memcg shrinker nr_deferred Date: Thu, 11 Mar 2021 11:08:41 -0800 Message-Id: <20210311190845.9708-10-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210311190845.9708-1-shy828301@gmail.com> References: <20210311190845.9708-1-shy828301@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. The following patch will add nr_deferred to parent memcg when memcg offline. To preserve nr_deferred when reparenting memcgs to root, root memcg needs shrinker_info allocated too. 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. Acked-by: Roman Gushchin Acked-by: Kirill Tkhai Reviewed-by: Shakeel Butt 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 dc7d0e2cb3ad..24e735434a46 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -114,12 +114,13 @@ struct batched_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 34cf3d84309c..397f3b67bad8 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,11 +206,13 @@ 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; struct mem_cgroup_per_node *pn; int nid; + int size = map_size + defer_size; for_each_node(nid) { pn = memcg->nodeinfo[nid]; @@ -217,9 +225,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(pn->shrinker_info, new); kvfree_rcu(old, rcu); @@ -234,9 +249,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 = memcg->nodeinfo[nid]; info = shrinker_info_protected(memcg, nid); @@ -249,12 +261,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; + 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) { @@ -262,6 +274,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); @@ -269,15 +283,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) @@ -285,11 +305,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