Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1378027imm; Tue, 22 May 2018 03:09:20 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrTImXMzr/MYy0bh6TV9ij+N98GGY+/l6vIOUtoFdYAfNapQHpkdrNDiVSJ9EApQPGmlB1n X-Received: by 2002:a62:3745:: with SMTP id e66-v6mr23508666pfa.43.1526983760654; Tue, 22 May 2018 03:09:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526983760; cv=none; d=google.com; s=arc-20160816; b=vDxmP/W1eAJ9lmi/qhGGmUTYWWZjoYINTy0mj4+sRKKVczFTk4cIhRlGjqL+hGkzyt yDGsEHTDAlTVqkb+sYTCENBF+UcSmT+f0VL2GINCuiaCcpaTtdPBR8efBjA4X1v7W+XW dQaZAnRp6B4NuQVa+UQ2LG6hSxOlYliU0aLHOH7rkuN5kJzWELpwUsOxqLxN/EqS7FQM V5riAJurdKw2GmlNNe26q5Z9ZTzd5Q3ebQ64dz/3/dCOs11k+jgDuF2P6AD1OVfeRTvN GyTikfyIH+ARW37jKkg4PuDP66/FICzoJVTmxKwNKSb5mpAZSz4TeTDrhmYHUHAbd5VT ZLsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:to:from:subject :dkim-signature:arc-authentication-results; bh=VOWwB5ZnqTu3nporWKwr+0OgmPO12yhjPMJYPufCoPs=; b=onu4MB08Nvm82PXXY/Wdnc5+ftpakqNL6Q6oSRIacwyePOAqwoTclkCmYVmoaDslVF F7aBTES5XdAMA4eRZQEwAgSjIklOGBUOWVdcevr7eUrPFz+AiYqbDTeaE0YdPGCvz2AQ OOtJmn333N+nTXRW3wDpfTHaKt4OGBP/qNgmbN2VIx1PWNyok02VPOrfOzF6Fp5xzsI0 qj9Ok8wMGhktir59qQ4m4+PipCKxKqigtSOyHF0sOYndKo3WEkd+cjpAM2KSpt4+LtXT VtPl13G1X5oPpj3E+G7n6DMjXPPCawz/h+sv0E0wunF27ITEJob9sR+HEf4bTY086lpS 1pRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=selector1 header.b=cjR5AZR1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b13-v6si10313166pgu.558.2018.05.22.03.09.05; Tue, 22 May 2018 03:09:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=selector1 header.b=cjR5AZR1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752016AbeEVKIT (ORCPT + 99 others); Tue, 22 May 2018 06:08:19 -0400 Received: from mail-eopbgr30090.outbound.protection.outlook.com ([40.107.3.90]:62176 "EHLO EUR03-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750733AbeEVKIN (ORCPT ); Tue, 22 May 2018 06:08:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VOWwB5ZnqTu3nporWKwr+0OgmPO12yhjPMJYPufCoPs=; b=cjR5AZR1sq7Q/+YAZaU+TZa8ElxhOnW61DR2Vpd8RImZarT1KB651rdN1e0k9Gsym8X6aY/W4+munpvoabUQAShktOtuyQjdDUvGxBUjkb5vi9/gjAFq1cZuKz3DtfXN+LZJIlnvNXyiy+15biN+Em2tGvIPCT6a2Ik1IP/od1U= Received: from localhost.localdomain (195.214.232.6) by AM5PR0801MB1332.eurprd08.prod.outlook.com (2603:10a6:203:1f::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.797.11; Tue, 22 May 2018 10:08:07 +0000 Subject: [PATCH v7 05/17] mm: Assign memcg-aware shrinkers bitmap to memcg From: Kirill Tkhai To: akpm@linux-foundation.org, vdavydov.dev@gmail.com, shakeelb@google.com, viro@zeniv.linux.org.uk, hannes@cmpxchg.org, mhocko@kernel.org, ktkhai@virtuozzo.com, tglx@linutronix.de, pombredanne@nexb.com, stummala@codeaurora.org, gregkh@linuxfoundation.org, sfr@canb.auug.org.au, guro@fb.com, mka@chromium.org, penguin-kernel@I-love.SAKURA.ne.jp, chris@chris-wilson.co.uk, longman@redhat.com, minchan@kernel.org, ying.huang@intel.com, mgorman@techsingularity.net, jbacik@fb.com, linux@roeck-us.net, linux-kernel@vger.kernel.org, linux-mm@kvack.org, willy@infradead.org, lirongqing@baidu.com, aryabinin@virtuozzo.com Date: Tue, 22 May 2018 13:08:05 +0300 Message-ID: <152698368548.3393.17660352117908550018.stgit@localhost.localdomain> In-Reply-To: <152698356466.3393.5351712806709424140.stgit@localhost.localdomain> References: <152698356466.3393.5351712806709424140.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: AM5P190CA0026.EURP190.PROD.OUTLOOK.COM (2603:10a6:206:14::39) To AM5PR0801MB1332.eurprd08.prod.outlook.com (2603:10a6:203:1f::10) X-MS-PublicTrafficType: Email X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(5600026)(4534165)(7168020)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020);SRVR:AM5PR0801MB1332; X-Microsoft-Exchange-Diagnostics: 1;AM5PR0801MB1332;3:CLmOOanXf9tAsVKj+2dpKGrROPMRETHJfqgiGndEUIi9P6z2eCA8f4hMiMbekUAQZu+SrH49LoiysaQ5rL+4hRKdgvguckvFG/UWQC2mxK7kbv1+Zg5LqTEG/95vmwcVpMVnQqZ3mrc5Owo+QkYjS/jh+JTU0cLXCHFfzfRKlngcxf2JuehilGy8Soaz9xCJk1e/1j99zcATcJ5nJQrnDQaxF0rYW+rP8DVCC//pK+bA7JXuu6eeYffv7y0PXLZG;25:oDBCZ7PNQyvE2TlnKUXgMuXD07YW91bVnKbXTWjTlfs58/HfLu2LA9pZvKeiUtBU6HhM1n1pBTYuNHp/e8RVQHlnXqSP3fGEehQBNCGZVrUYZUt9zEo/BQ8/2QCVAogicNIOo3mBSrmLG/gMRIAd9nf5BOu0mG+m2ELpugQ6hMfBrH0MiSLxa4SJ8L9Q5ezcGJdTd3P5LRv+HwpKZh+X2VHNvdyjft+WsYHPVYVts6waGLT4LvF0p6hCTC7b5o2RVX5gRcpLUeSDvLUJTgyohXG2saEgS/11/2zVXBC4eIqXIAqqVCUHsJFI2g4SmpOCYrMPiKLQNv6djKirPZn/Gg==;31:Y3yM0NcemRXYGVoe/CwANiQ/ZcdNjRwjhm33Bv0OSf/VqjJ+/8K/799on6EHqp/wDIN+EYD8rwbktHBpET+V10BeWXM0ePubg9BtyCbiHNBGEGhvY9Eb2kAw7ZdZE/suO5Le6UKKCi9AQDEKO9r3ZYyTfdK0SEp+qSivdZ+93t14KoKsJbLS5ltTYpx/hxkpY62B1U+VImdD8lAy+7VLy7dGvMTqphS210tWJttneIA= X-MS-TrafficTypeDiagnostic: AM5PR0801MB1332: Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ktkhai@virtuozzo.com; X-Microsoft-Exchange-Diagnostics: 1;AM5PR0801MB1332;20:ZSO3fmf94Sg3rkeHEBCpdHHvWK+HhagDZXAPSOKYaoheD3deasK9PUZHKadLANfo5OWwLBtIoXcQ0gkj91sRWlckFgsaZ2hgic/ZI+u98H8K3utuUFzU9MU9Jng1eC171SEd/cI4blLmoyvfAnE2z58j8jSkErfmyzRdXRvo9Tu2m3FFzQyB5ymyqLp9+GuKHavVbuDTIFhsRs1OjZSCoNJUMV7Rt4ulxJ500C36t6Sdeil/Yj6VEIHwyX2GHyFhV/ncx7jIV9ZHoK09tJrtKIVvdNRQzASZ1Czo89f6Oemmu03yyb6qnfRQDcVebwQ2fSjgEsUY5pT9nr9eSWUxqKK084Ls4lrUqBW8hyBZtrPlXu0X+Y7Vpm75FwrbMBuNmwI3SFjfvgTefULJR6xBbRdqDzF2GeQ/TGWi2bmFvEROPZZhT3FJBvA2QJxzdntmukhfCh7ILICtnLOPaQh+Av2XO2MTR/2+Ann571zdsoqBOCiDFNJvs+WmvGz546G4;4:AcOZF8Uj14KojfYOoeujZlInVpnxUuFO7C3uuqeMsHar7ee9xAVwDc9GCOT9ZiQ+vTIyT132zZxgjvAU+yKRhmDLezT57lerIPtCUPQOJhQvPyIM2G28sPTQh53obQK132SLTxiA5vL5quJvZak2sXFK/HRkYiNf11LoPYPNErW0pg3IKoI5Bd9ZdKFAo/lo3BJN1zIjp657itwLTeuwFUXrUogpcr7wewaHcHXG0OSMe700VfxxvME3Fut/wx2aT9hwtsRLwrGn/skcgwMitpvWXcthXl+ERKrSWy1aPaP7Uz1izgTg6AvOORMQj1hPtw7pG9nAc0aEeRifLjkqdCtVShFEBWV9yGc/5ul/KSk= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(209352067349851)(131327999870524); X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040522)(2401047)(5005006)(8121501046)(3002001)(3231254)(944501410)(52105095)(10201501046)(93006095)(93001095)(149027)(150027)(6041310)(20161123562045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123560045)(6072148)(201708071742011)(7699016);SRVR:AM5PR0801MB1332;BCL:0;PCL:0;RULEID:;SRVR:AM5PR0801MB1332; X-Forefront-PRVS: 0680FADD48 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(6069001)(39850400004)(366004)(376002)(396003)(346002)(39380400002)(199004)(189003)(2906002)(33896004)(55236004)(6506007)(386003)(52116002)(7696005)(59450400001)(2486003)(23676004)(305945005)(6636002)(7736002)(3846002)(6116002)(66066001)(47776003)(26005)(230700001)(103116003)(7416002)(186003)(61506002)(16526019)(50466002)(76176011)(956004)(476003)(486006)(11346002)(446003)(39060400002)(86362001)(53936002)(9686003)(5660300001)(55016002)(8676002)(8936002)(81166006)(81156014)(68736007)(58126008)(316002)(478600001)(106356001)(105586002)(25786009)(97736004)(921003)(1121003);DIR:OUT;SFP:1102;SCL:1;SRVR:AM5PR0801MB1332;H:localhost.localdomain;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtBTTVQUjA4MDFNQjEzMzI7MjM6QUYzTjFTMmp0RS9sTnJaS3NzREdhdmlL?= =?utf-8?B?UTZySWpVNXE4alVKRkVUZ3FzRkdveC9maC9ldkRCc21xaUJNeHloRnc3QXMw?= =?utf-8?B?bGFSQ1c3dFI5bXZsS3duaGdncUVLbXdrL0tzaVhGL1hzbStPQW04WTZJbmYv?= =?utf-8?B?a1pyR2xSWHlzZEp5NmxGTHo4SFU0WFU3UU5oa2VkY1pidFZDNkhBYUp1QjFQ?= =?utf-8?B?OEFwVUp2cjg0QzZiQTVncWNiVjNEUmFadFV4MUZ6ZUdxbGh1TW1mT0l6enAv?= =?utf-8?B?V01iY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t8bh95E0hmDap0xG1tRLR8so6DCYkPU1Ltmu2iGTwUREMGnoEnkDD+kVxPLWQrPEPQ8vaQp/mCcp/a/OYDS5cB8WFqBtChIKCLYi6ZM7v7quA1LgheS2XrgWqoLku0= X-MS-Office365-Filtering-Correlation-Id: c26b3adb-87fb-4763-9825-08d5bfcbebb7 X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 May 2018 10:08:07.7880 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c26b3adb-87fb-4763-9825-08d5bfcbebb7 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0801MB1332 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Imagine a big node with many cpus, memory cgroups and containers. Let we have 200 containers, every container has 10 mounts, and 10 cgroups. All container tasks don't touch foreign containers mounts. If there is intensive pages write, and global reclaim happens, a writing task has to iterate over all memcgs to shrink slab, before it's able to go to shrink_page_list(). Iteration over all the memcg slabs is very expensive: the task has to visit 200 * 10 = 2000 shrinkers for every memcg, and since there are 2000 memcgs, the total calls are 2000 * 2000 = 4000000. So, the shrinker makes 4 million do_shrink_slab() calls just to try to isolate SWAP_CLUSTER_MAX pages in one of the actively writing memcg via shrink_page_list(). I've observed a node spending almost 100% in kernel, making useless iteration over already shrinked slab. This patch adds bitmap of memcg-aware shrinkers to memcg. The size of the bitmap depends on bitmap_nr_ids, and during memcg life it's maintained to be enough to fit bitmap_nr_ids shrinkers. Every bit in the map is related to corresponding shrinker id. Next patches will maintain set bit only for really charged memcg. This will allow shrink_slab() to increase its performance in significant way. See the last patch for the numbers. Signed-off-by: Kirill Tkhai --- include/linux/memcontrol.h | 14 +++++ mm/memcontrol.c | 120 ++++++++++++++++++++++++++++++++++++++++++++ mm/vmscan.c | 5 ++ 3 files changed, 139 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 996469bc2b82..e51c6e953d7a 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -112,6 +112,15 @@ struct lruvec_stat { long count[NR_VM_NODE_STAT_ITEMS]; }; +/* + * Bitmap of shrinker::id corresponding to memcg-aware shrinkers, + * which have elements charged to this memcg. + */ +struct memcg_shrinker_map { + struct rcu_head rcu; + unsigned long map[0]; +}; + /* * per-zone information in memory controller. */ @@ -125,6 +134,9 @@ struct mem_cgroup_per_node { struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1]; +#ifdef CONFIG_MEMCG_KMEM + struct memcg_shrinker_map __rcu *shrinker_map; +#endif struct rb_node tree_node; /* RB tree node */ unsigned long usage_in_excess;/* Set to the value by which */ /* the soft limit is exceeded*/ @@ -1261,6 +1273,8 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg) return memcg ? memcg->kmemcg_id : -1; } +extern int memcg_expand_shrinker_maps(int new_id); + #else #define for_each_memcg_cache_index(_idx) \ for (; NULL; ) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 023a1e9c900e..b454f077de44 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -320,6 +320,120 @@ EXPORT_SYMBOL(memcg_kmem_enabled_key); struct workqueue_struct *memcg_kmem_cache_wq; +static int memcg_shrinker_map_size; +static DEFINE_MUTEX(memcg_shrinker_map_mutex); + +static void memcg_free_shrinker_map_rcu(struct rcu_head *head) +{ + kvfree(container_of(head, struct memcg_shrinker_map, rcu)); +} + +static int memcg_expand_one_shrinker_map(struct mem_cgroup *memcg, + int size, int old_size) +{ + struct memcg_shrinker_map *new, *old; + int nid; + + lockdep_assert_held(&memcg_shrinker_map_mutex); + + for_each_node(nid) { + old = rcu_dereference_protected( + mem_cgroup_nodeinfo(memcg, nid)->shrinker_map, true); + /* Not yet online memcg */ + if (!old) + return 0; + + new = kvmalloc(sizeof(*new) + size, GFP_KERNEL); + 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); + + rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, new); + if (old) + call_rcu(&old->rcu, memcg_free_shrinker_map_rcu); + } + + return 0; +} + +static void memcg_free_shrinker_maps(struct mem_cgroup *memcg) +{ + struct mem_cgroup_per_node *pn; + struct memcg_shrinker_map *map; + int nid; + + if (mem_cgroup_is_root(memcg)) + return; + + for_each_node(nid) { + pn = mem_cgroup_nodeinfo(memcg, nid); + map = rcu_dereference_protected(pn->shrinker_map, true); + if (map) + kvfree(map); + rcu_assign_pointer(pn->shrinker_map, NULL); + } +} + +static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg) +{ + struct memcg_shrinker_map *map; + int nid, size, ret = 0; + + if (mem_cgroup_is_root(memcg)) + return 0; + + mutex_lock(&memcg_shrinker_map_mutex); + size = memcg_shrinker_map_size; + for_each_node(nid) { + map = kvzalloc(sizeof(*map) + size, GFP_KERNEL); + if (!map) { + memcg_free_shrinker_maps(memcg); + ret = -ENOMEM; + break; + } + rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, map); + } + mutex_unlock(&memcg_shrinker_map_mutex); + + return ret; +} + +int memcg_expand_shrinker_maps(int new_id) +{ + int size, old_size, ret = 0; + struct mem_cgroup *memcg; + + size = DIV_ROUND_UP(new_id + 1, BITS_PER_BYTE); + old_size = memcg_shrinker_map_size; + if (size <= old_size) + return 0; + + mutex_lock(&memcg_shrinker_map_mutex); + if (!root_mem_cgroup) + goto unlock; + + for_each_mem_cgroup(memcg) { + if (mem_cgroup_is_root(memcg)) + continue; + ret = memcg_expand_one_shrinker_map(memcg, size, old_size); + if (ret) + goto unlock; + } +unlock: + if (!ret) + memcg_shrinker_map_size = size; + mutex_unlock(&memcg_shrinker_map_mutex); + return ret; +} +#else /* CONFIG_MEMCG_KMEM */ +static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg) +{ + return 0; +} +static void memcg_free_shrinker_maps(struct mem_cgroup *memcg) { } #endif /* CONFIG_MEMCG_KMEM */ /** @@ -4482,6 +4596,11 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css) { struct mem_cgroup *memcg = mem_cgroup_from_css(css); + if (memcg_alloc_shrinker_maps(memcg)) { + mem_cgroup_id_remove(memcg); + return -ENOMEM; + } + /* Online state pins memcg ID, memcg ID pins CSS */ atomic_set(&memcg->id.ref, 1); css_get(css); @@ -4534,6 +4653,7 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css) vmpressure_cleanup(&memcg->vmpressure); cancel_work_sync(&memcg->high_work); mem_cgroup_remove_from_trees(memcg); + memcg_free_shrinker_maps(memcg); memcg_free_kmem(memcg); mem_cgroup_free(memcg); } diff --git a/mm/vmscan.c b/mm/vmscan.c index 75b00e6ff9db..d8a1ca350a58 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -182,6 +182,11 @@ static int prealloc_memcg_shrinker(struct shrinker *shrinker) if (id < 0) goto unlock; + if (memcg_expand_shrinker_maps(id)) { + idr_remove(&shrinker_idr, id); + goto unlock; + } + if (id >= shrinker_nr_max) shrinker_nr_max = id + 1; shrinker->id = id;