Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4246366imm; Fri, 18 May 2018 01:43:17 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqjwB5C3s699AaGGEcudq+/t6cWqaMsLraVEamITG41N8goHB5M2qEAmzFNnU7JySha5cKR X-Received: by 2002:a65:628c:: with SMTP id f12-v6mr1694783pgv.36.1526632997475; Fri, 18 May 2018 01:43:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526632997; cv=none; d=google.com; s=arc-20160816; b=HZtOq6+q+U5cTVKLXgFvNoQUb9AnKudbDfBHxL4/Y5qrLR5CYarGVPUtplM6qaRKjO 3rNPs0sKoqorUIwMuNxdjTQBfsQFv3aqxjUO69S6N0sVEu2AoxAdDQrVZjAFNQIwFD05 H0zgCxVrzjJH55AI0kkUm/vJ0p5xNqTtn/JHn38Q/XVSTSbby+tC6gLl9MPH8lMz3KAv 9Zuvx4bf2v1bMRUjoZZnesJ7loihXtY8OnP3YI0N/7SK7Ji9IlgEN58hJYhzWooDc6qq qXUiHDJZHduFGC5q+OCTqpXbF0v+rXR/DVDnjFKXdy5IvjA6dV9S00BBu77uQCa9Kf1c dTNA== 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=WAev2mRvESnMpiOMdA6nMo3DX576lizRTUAOQJk2Ssc=; b=nWYRx2y5NySC9XqgxXHYqIVFMaGsqksMkITFYfC2G2UD/GWPnZoxe/hasELRShCqIa fGM6yuoqghDr7KgOZ3/xBT8Y0peDnxgNr8WKUmZvckldhJfBG9I4eGxb2vlhAF2+A50B 1e9qFsudAgQY/t/1xHEUrcM5qQIsWYMh6QKfl7OIICjJZt40iA+rmfTMQGwNrVQUSZkx GPtEHzPVNYRw1csp0SDXq2c53mRep4pz+dkYaJPHuIOcCvs+/4JQKqAheDi/+hAm4NnE KmFvbtIqY3she1eZEeXnaABjr+p5NqTTkvLCr6wircv1oEcn7Eig54aqKFtZcfRggaWA hQgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=selector1 header.b=afetB9wk; 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 f75-v6si7487651pfh.90.2018.05.18.01.43.03; Fri, 18 May 2018 01:43:17 -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=afetB9wk; 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 S1753833AbeERImv (ORCPT + 99 others); Fri, 18 May 2018 04:42:51 -0400 Received: from mail-ve1eur01on0100.outbound.protection.outlook.com ([104.47.1.100]:48288 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752348AbeERImp (ORCPT ); Fri, 18 May 2018 04:42:45 -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=WAev2mRvESnMpiOMdA6nMo3DX576lizRTUAOQJk2Ssc=; b=afetB9wk1HCu2dRdmGKncSth1cbzr5LARrDjsE7daewQIb4c6w6czFKoNddTIrXzhsx/wyciRxGNK7srURAtocmk0qNpA/Wx7FJw0J6I5pE7hKuq3Ok+6ar8AOfAzxxlZBobmMmuO4hqg8Bz2oD/XK7GwOFmhjqjqajHD6cEj5g= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ktkhai@virtuozzo.com; Received: from localhost.localdomain (195.214.232.6) by DB6PR0801MB1336.eurprd08.prod.outlook.com (2603:10a6:4:b::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.776.11; Fri, 18 May 2018 08:42:39 +0000 Subject: [PATCH v6 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: Fri, 18 May 2018 11:42:37 +0300 Message-ID: <152663295709.5308.12103481076537943325.stgit@localhost.localdomain> In-Reply-To: <152663268383.5308.8660992135988724014.stgit@localhost.localdomain> References: <152663268383.5308.8660992135988724014.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: HE1PR05CA0160.eurprd05.prod.outlook.com (2603:10a6:7:28::47) To DB6PR0801MB1336.eurprd08.prod.outlook.com (2603:10a6:4:b::8) 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:DB6PR0801MB1336; X-Microsoft-Exchange-Diagnostics: 1;DB6PR0801MB1336;3:7UVe0R9RLPQuRIOWh5fbAjhdgApwKkxUlV5IkeVaY6FSLXc4yp8U2pd5/iQdZRFtyhIV6wMDfEZqneqYBTHX6EsiYJG2Ka3xsFGEPJtmeVL17x2kbg8gCtXnQgW1Fnbpn9U67yvXlP6/gJ7V5XmjR9hbpISVKuOWv2/Dk/OwtiVdsxkImJW8m+JT/JAMh1XL+qlaRcHpKcIzYmvCvO420fPjKQHxPhfryOCNKAApg8U83OOJtQZLxPqca/YndUwt;25:QlLNxA1eNLTxd0thV4UFtH5pc1Dhq01QunfsmaIPDTMo2pNnB4opJFFvplwbx7ri7gibDdJYtydrpoYYdLVW3gSeVnBzF10U76CYRzVr0XtOyzMa48ePt7ELlbd7oryZkbKQIDIafWJYw76QshboKMfLdDWQlumcqFlFtHm8H4/dt/U9owAlNVCR2aU33ttiO5Rx0L8bid4yOStltew4EBSJ8Pgq1jA5K536j7hnSdrxuh9OMh9riohCVaoDu+OrwcHS3TrF3wuzU8E1ndiZA8w89uV1ogE4XU/1cQpGHc9gaVPLOl6rpMdrfcSyC67KTnrWdogzE4vS0JrM2KH7vg==;31:BWWFygInPmFy8lEmxn0GGZn4J+fzcorbKaj8CbIUST8F3BOTWh+ltf2UKIJ/YQLPybKie0TawmudVNREjX9tz1MPlX59VFvOk1GBJ6MZX+t/NxVfCC9U73ata/Q9nBJO/dDWiIT+yOs2W734Jk6bQhnFeVCbolTRdQxHXb6FFA6cSA0wYbIuwjCfMbMoW99Jd0ouqVDEmKXjyFQ7o/oS0Jpo3ZN4GfYG49M3IK4iuyE= X-MS-TrafficTypeDiagnostic: DB6PR0801MB1336: X-Microsoft-Exchange-Diagnostics: 1;DB6PR0801MB1336;20:w5uqU8uv2F1CvOHi6POMEF/BWY8Y5570bVIMSaAcDjq6Z5bl80kbyTEtT+2KlVJyaJpeB2j66wT57RoCnmeU5hDb+gKa2j1UO+EANZAC6GTqJGPvonSkh58UMHGodx4BgwnE1KPsOD7Y+7SGz1WkbOJyGpJFLwtOVAd/v8KXCXbJsTzUXLFsZH5cT7LNKV8BYPsFRPoU1nbDNxIbwBEjn4u2CwHJYlPmFuGn0mFVYD5KUdNB7GGBVCsYBtW6qZfd2pTy27ir2JmCsSS4EC8AsN8m/8/gAQBpusu/QGwOj0qFB2yBmM1ANKVl4Y4upJIQolYWnrqdoz7vIXifJh4PYvwjX6xHmYwE0Po5dieorwFkkNsxAKVSqBy9vHnEklTY8Y2JJ+n6g/PdzXT5gYQIAGlafbN3lIw1cvdFryHFGmkyVKESHtygExov7pIxMadWlj9o239fRYxEcgi+MPMWAhYDhNkaUm/irqqTFBsTVCHCwp8eni8jL58Rk0Z/ys52;4:udYzS9hNPWffRnRJY4XgzlZ1gEoNdR02sHC/qs4+Pv+qbGPhr1YePRVtRes+r3LsBv2WigBgJ40CIiYEDSuzdy8OI74bau3OwVXv4poiY15+VwqvpcN7AZ9jEWZv9EsUEbDTijAO8UtteCVSjqZip7+mO57ydT0PxOaplSVjLpiM6X4tZjy5yrOkKoC0siIpRPQ/2+T9j+iYdeGXrpoDznQsR7BCjPYNQqJ5C6ZUwy2WR01XQYHq0ZVNyIiPb9cDf8YZ0Hst1fLrwBSNmXAYLtm7HYnD/wxvNZEn9n1SGK1xEBAuo20ZQ2BuoOQMzuj1uNjHuDJh+uTQVd2HqxIZyrHRl7sNRaBzLI9m/+A1ug4= 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)(93006095)(93001095)(10201501046)(149027)(150027)(6041310)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123564045)(20161123562045)(6072148)(201708071742011)(7699016);SRVR:DB6PR0801MB1336;BCL:0;PCL:0;RULEID:;SRVR:DB6PR0801MB1336; X-Forefront-PRVS: 0676F530A9 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(6069001)(396003)(366004)(39850400004)(39380400002)(346002)(376002)(189003)(199004)(316002)(25786009)(58126008)(6636002)(6116002)(39060400002)(3846002)(53936002)(9686003)(105586002)(106356001)(230700001)(97736004)(55016002)(50466002)(11346002)(23676004)(2486003)(52116002)(7696005)(7736002)(8676002)(81156014)(486006)(81166006)(305945005)(476003)(47776003)(8936002)(956004)(26005)(68736007)(16526019)(186003)(7416002)(76176011)(33896004)(86362001)(2906002)(386003)(59450400001)(478600001)(61506002)(446003)(103116003)(55236004)(6506007)(66066001)(5660300001)(921003)(1121003);DIR:OUT;SFP:1102;SCL:1;SRVR:DB6PR0801MB1336;H:localhost.localdomain;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtEQjZQUjA4MDFNQjEzMzY7MjM6Uld0Wm5zaDAwRDI3cjhlTmRrWFRDR0Zu?= =?utf-8?B?SnBtR3J2V3VzemdTdHVJOThXYlFQL2ZQRGk1aFpLeGQzR2lrSjh0eCtwaUgx?= =?utf-8?B?TXduR1orc1E2Um4yODlPekpyM09rREVzQ1Nza2czZEhHbFErZGV0OWdUbHdU?= =?utf-8?B?SzZvcFNWVnF5NUV4R0dCWWJyMUZramQ3bFg5WHFDcXByL3JnTFBORXZkTGpx?= =?utf-8?B?T01VWGZQanU2ZjZJQTd6ZzdrQW1lQWhGcUlXcmNYUmN6eVM2SXk5VlQ1OGx2?= =?utf-8?B?ZXNPN0JjeUc4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UmJ06C8YrRToRFyL+Jh4rXB+CGdd94lg3QUcUHzPT5kK1gsewBkDJahdbv5V4oj2L7fIYBtk80srTN4xw939WkM/XW6uUBCR1L+r07En3s= X-MS-Office365-Filtering-Correlation-Id: 4bc14661-dadf-4f49-f111-08d5bc9b51c0 X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 May 2018 08:42:39.9545 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4bc14661-dadf-4f49-f111-08d5bc9b51c0 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0801MB1336 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 | 10 ++++ 3 files changed, 144 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..317a72137b95 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( + memcg->nodeinfo[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 nr) +{ + int size, old_size, ret = 0; + struct mem_cgroup *memcg; + + size = DIV_ROUND_UP(nr, 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 3de12a9bdf85..f09ea20d7270 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -171,6 +171,7 @@ static DECLARE_RWSEM(shrinker_rwsem); #ifdef CONFIG_MEMCG_KMEM static DEFINE_IDR(shrinker_idr); +static int memcg_shrinker_nr_max; static int prealloc_memcg_shrinker(struct shrinker *shrinker) { @@ -181,6 +182,15 @@ static int prealloc_memcg_shrinker(struct shrinker *shrinker) ret = id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL); if (ret < 0) goto unlock; + + if (id >= memcg_shrinker_nr_max) { + if (memcg_expand_shrinker_maps(id + 1)) { + idr_remove(&shrinker_idr, id); + goto unlock; + } + memcg_shrinker_nr_max = id + 1; + } + shrinker->id = id; ret = 0; unlock: