Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754027AbdHWM0N (ORCPT ); Wed, 23 Aug 2017 08:26:13 -0400 Received: from mail-eopbgr40112.outbound.protection.outlook.com ([40.107.4.112]:36958 "EHLO EUR03-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753902AbdHWM0K (ORCPT ); Wed, 23 Aug 2017 08:26:10 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ktkhai@virtuozzo.com; Subject: Re: [PATCH 3/3] mm: Count list_lru_one::nr_items lockless To: Vladimir Davydov Cc: apolyakov@beget.ru, linux-kernel@vger.kernel.org, linux-mm@kvack.org, aryabinin@virtuozzo.com, akpm@linux-foundation.org References: <150340381428.3845.6099251634440472539.stgit@localhost.localdomain> <150340497499.3845.3045559119569209195.stgit@localhost.localdomain> <20170822194725.ik3xwxu67wcthisb@esperanza> <20170823082712.tw6qtyllctn25puq@esperanza> From: Kirill Tkhai Message-ID: <6f4a624d-047f-6455-d8fa-e9e73871df03@virtuozzo.com> Date: Wed, 23 Aug 2017 15:26:12 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20170823082712.tw6qtyllctn25puq@esperanza> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: HE1PR0202CA0019.eurprd02.prod.outlook.com (2603:10a6:3:8c::29) To HE1PR0801MB1338.eurprd08.prod.outlook.com (2603:10a6:3:39::28) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 818fe9f0-f3ed-4bfc-e3db-08d4ea22208f X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:HE1PR0801MB1338; X-Microsoft-Exchange-Diagnostics: 1;HE1PR0801MB1338;3:utzF5vlxdO+3+xVM/lvKpVwbj77u0at20UpYooBT2prsCb7Ski/CQ4RZwedADpXpX+YyAPoOv3o/CzRwnoLdga1fETrkqllwuVQZE8Y4lV/IQ+sxmhZ23NbbrCprAJcKRssPXqPNexS0+34/wtchUjjVA1Dbi+zUh+hBhvG3x+8bGw+v1ysWjLorr2wG3dR0H6ZfKev9ZINhYdHg7FXkcYg6j70Qn6Ho2S/Xjp2YFGqa3eI4ttkDyze0dM7/h2X7;25:HLw+MNY7KD4cv0KwT897vwIkKRnPRgB9xhSsWZWSASaLlYMPRgfTow3C2QTZ/+Gs1Tb95RvlW5HnNSWWRf4nC6xlJy3CxlgISU5uRNCRpv8F4clbJEVZOItKy+WK8hnke0bo4QfiWb2EiZNAmh7S1P2oAAbJlMNfo/HA8uIxevdBVQLP0dO8ES1ztUX2becY+8xyyCgTPolVxgqn5uc1HPNH9Wg11NZQm6tnyRt06tMK5Hd0CzDE2f8fGDLnj9WJgDctlH8mEACJKfVvgk1lXDXSaTA/lWhtcj7W7FdsvBPfTlHilmuhcc5rJsN/vvPKYxM3rQOo/L3y+t6mbToLIg==;31:DyqaZfoQYviTDwNu0xaGQvOetZFs/sUjMfL6O2yLSBkl31jtxSZDUfdCHpBQFzkklQdAvcdwsWDvmOBky09E50MJ+bkSCnO8cHiZLLpAg6pBNYsSu3jtnCk/JROA2i9On5GfrAvGX9dNNZo3KF2kkVX2Z5hefFTuUPrQBvWihY0d8olQs51deDWnA7A1PPm0frsNlzyrUMb3v+/75L1UgOtLycA+lpB92V9qT7jMzIY= X-MS-TrafficTypeDiagnostic: HE1PR0801MB1338: X-Microsoft-Exchange-Diagnostics: 1;HE1PR0801MB1338;20:ZawP0N3+8ZaELphrHK3FR/8AiNh+IBJGV8PlAHDUXQ5vZMs8ame6EPmsGyZsbAV2VFyrA9m74aVXGoMidwaXx/29JsKPo+XPSklAQO1DchSfTC3KZPv7rBDsx5KP97QAHKLrr+od/iRrpB83HPWBhvTp3LHrVuma40AcWqZo0A3WLDpfZWaQ2CXodKfUNGhyjv6I3V8OAbJZgQZU4jypOoIuS9N9PqDNIdR1OFxkbZbKwWNsEKNuXyIpFQiZrWOfM94KNoLDW3jliFhNDe1HbikNflj39vCqNnEHUvFePOeXB+IvusFcSXzwCcUvZfaMIxAJn+Xl94L2dXcIDjhaVOU9//RmRD//iByO94N46/npIElJJBaTGZ/yLM8Zd88PxIPhTvJ0Ecm+wKjgiHKEJvXoI+3eSDgbStTT+nUFHos=;4:vQiMTv7jHvMBkboEYkonVX9KzRBliVVCOsfvMF/bbJGO0AelqBQ8eKXgqnhd64yDGNwS5ULH9Nb3n3iQpobF2WfJNRNinrOQDn/TuSQ5VetfGZjkGL07vTWeX1apjqKV3Kwo4AS/MA1ai/Qf8OWgpKAQazqKghLydKDURK7vYl7+BBwtFOO9xPsCFkXoZpG+TvcKt346AeMT4KkgRI4hMZ64nLyuzQvqmfDzC/27I25jHqwjNMTb4sPqVrIJOBra42OSkdyZDWSPpCuMpyMxNQv6EXjIyhQWBXErIgBI0co= X-Exchange-Antispam-Report-Test: UriScan:(190756311086443); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93001095)(100000703101)(100105400095)(3002001)(6041248)(20161123564025)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123555025)(20161123560025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:HE1PR0801MB1338;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:HE1PR0801MB1338; X-Forefront-PRVS: 040866B734 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(7370300001)(4630300001)(6009001)(6049001)(199003)(189002)(24454002)(36756003)(3846002)(23676002)(68736007)(65956001)(66066001)(230700001)(65806001)(50466002)(47776003)(2906002)(93886005)(189998001)(81166006)(64126003)(4326008)(83506001)(81156014)(8676002)(6116002)(86362001)(105586002)(31686004)(110136004)(31696002)(7350300001)(42186005)(478600001)(6666003)(54356999)(305945005)(53546010)(106356001)(97736004)(2950100002)(229853002)(5660300001)(4001350100001)(7736002)(6916009)(6486002)(65826007)(25786009)(33646002)(76176999)(77096006)(53936002)(6246003)(50986999)(101416001);DIR:OUT;SFP:1102;SCL:1;SRVR:HE1PR0801MB1338;H:[172.16.24.149];FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtIRTFQUjA4MDFNQjEzMzg7MjM6UlcxK1JFVVVRR1IvVncwWk1VeGJkaFds?= =?utf-8?B?UHVOeHRVaXFoV2pCSVZzQmZaQTVPUUNRU3FFbExDamIzVUNOWnlMOFpJcmYz?= =?utf-8?B?UVMvS2R0K2ZyK0FuWUx3N1ZQakhQQ3VWZzB1dUxubVhlZ2gxVUVuRzFpTTdX?= =?utf-8?B?ZVlZT056Z0d4Z0xnUkhKUWx6WmJLNFVnZTVhU2pOYVBodFhkbzl1NTRCYU5V?= =?utf-8?B?bmdiTlRWOVlPYysxUmJ0VUZVYUh2SE5kS3hEY3l2clNTVGtpTjFlQTVxRzQw?= =?utf-8?B?R0huaWVKTDBlUWlIL1BmeTdPOFdQL3cydmJNbGVoZWE2VGtGbjlhYkNyNjdF?= =?utf-8?B?UlFKbXJYWHp2eHl4NjBjM0lhSURhd0R5UFlKTFVEZzhhSzcxNnUrbmRrTkFr?= =?utf-8?B?ZjZhdGlhQ0ZRUVdrczRTQlMxcU50MzFpV2RnbzB6QjZndzFiRi9ZRlJQK0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GGG8X+W8Rbg/J3Lm1G4EvcqUznXgVr9ikk56USxFrT8c+r8IveRUa8otWb7OVcf1y+3w==;24:kmTlDndCPXyDEFyn2PK6sylVnyzIfvBohopczSaTCoCcvdDm/7G/I8dCq8Ydn5BW0KW38uTPjVdQzTb5XkGqebB9+eYCd2Lf/m2+ewHaALI=;7:rozg/3UVf8ee+yBCf2kC3Npd3Isr6NHTuR1UpvKLGGUv+nE0GJrHdpgEtrWEykeNBMkeHeDlYYYNTjq5krkNurAdQnROqCQjnPaNY1Gh3zYS1HhdMiSddJpDLBSqf5SowIec9aPxA4uCynAv3qYGXhjvs+nPY8n9MW7cTUim5CklPiJXYwxU5EMvMUV/YgUL7127YmRAtoQqYkiNqwyiaWPiOYbRxnhrQINjya42NAM= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;HE1PR0801MB1338;20:ZrFG9ig6fxOM+NEXB1RbKZq0GXJOOs/y6l5yw+6rKGYejcjLNpcgxdkc4z+QG67NKpzaFdhNdMbRPuIYnKSI2cIcYt0GD663MFkLyNowgQmQRWobEf5wtO7sJQAISrUe/6mqJ58mBKGiZpS2VN0A4Ygah3d+XraKExSUh36cXCQ= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Aug 2017 12:26:05.7952 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0801MB1338 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 10119 Lines: 235 On 23.08.2017 11:27, Vladimir Davydov wrote: > On Wed, Aug 23, 2017 at 11:00:56AM +0300, Kirill Tkhai wrote: >> On 22.08.2017 22:47, Vladimir Davydov wrote: >>> On Tue, Aug 22, 2017 at 03:29:35PM +0300, Kirill Tkhai wrote: >>>> During the reclaiming slab of a memcg, shrink_slab iterates >>>> over all registered shrinkers in the system, and tries to count >>>> and consume objects related to the cgroup. In case of memory >>>> pressure, this behaves bad: I observe high system time and >>>> time spent in list_lru_count_one() for many processes on RHEL7 >>>> kernel (collected via $perf record --call-graph fp -j k -a): >>>> >>>> 0,50% nixstatsagent [kernel.vmlinux] [k] _raw_spin_lock [k] _raw_spin_lock >>>> 0,26% nixstatsagent [kernel.vmlinux] [k] shrink_slab [k] shrink_slab >>>> 0,23% nixstatsagent [kernel.vmlinux] [k] super_cache_count [k] super_cache_count >>>> 0,15% nixstatsagent [kernel.vmlinux] [k] __list_lru_count_one.isra.2 [k] _raw_spin_lock >>>> 0,15% nixstatsagent [kernel.vmlinux] [k] list_lru_count_one [k] __list_lru_count_one.isra.2 >>>> >>>> 0,94% mysqld [kernel.vmlinux] [k] _raw_spin_lock [k] _raw_spin_lock >>>> 0,57% mysqld [kernel.vmlinux] [k] shrink_slab [k] shrink_slab >>>> 0,51% mysqld [kernel.vmlinux] [k] super_cache_count [k] super_cache_count >>>> 0,32% mysqld [kernel.vmlinux] [k] __list_lru_count_one.isra.2 [k] _raw_spin_lock >>>> 0,32% mysqld [kernel.vmlinux] [k] list_lru_count_one [k] __list_lru_count_one.isra.2 >>>> >>>> 0,73% sshd [kernel.vmlinux] [k] _raw_spin_lock [k] _raw_spin_lock >>>> 0,35% sshd [kernel.vmlinux] [k] shrink_slab [k] shrink_slab >>>> 0,32% sshd [kernel.vmlinux] [k] super_cache_count [k] super_cache_count >>>> 0,21% sshd [kernel.vmlinux] [k] __list_lru_count_one.isra.2 [k] _raw_spin_lock >>>> 0,21% sshd [kernel.vmlinux] [k] list_lru_count_one [k] __list_lru_count_one.isra.2 >>> >>> It would be nice to see how this is improved by this patch. >>> Can you try to record the traces on the vanilla kernel with >>> and without this patch? >> >> Sadly, the talk is about a production node, and it's impossible to use vanila kernel there. > > I see :-( Then maybe you could try to come up with a contrived test? I've tried and I'm not sure I'm able to reproduce on my test 8-cpu node the situation like I saw on production node via a test. Maybe you have an idea how to measure that? I've changed the places, you commented, and the merged patch is below. How are you about it? [PATCH]mm: Make count list_lru_one::nr_items lockless During the reclaiming slab of a memcg, shrink_slab iterates over all registered shrinkers in the system, and tries to count and consume objects related to the cgroup. In case of memory pressure, this behaves bad: I observe high system time and time spent in list_lru_count_one() for many processes on RHEL7 kernel (collected via $perf record --call-graph fp -j k -a): 0,50% nixstatsagent [kernel.vmlinux] [k] _raw_spin_lock [k] _raw_spin_lock 0,26% nixstatsagent [kernel.vmlinux] [k] shrink_slab [k] shrink_slab 0,23% nixstatsagent [kernel.vmlinux] [k] super_cache_count [k] super_cache_count 0,15% nixstatsagent [kernel.vmlinux] [k] __list_lru_count_one.isra.2 [k] _raw_spin_lock 0,15% nixstatsagent [kernel.vmlinux] [k] list_lru_count_one [k] __list_lru_count_one.isra.2 0,94% mysqld [kernel.vmlinux] [k] _raw_spin_lock [k] _raw_spin_lock 0,57% mysqld [kernel.vmlinux] [k] shrink_slab [k] shrink_slab 0,51% mysqld [kernel.vmlinux] [k] super_cache_count [k] super_cache_count 0,32% mysqld [kernel.vmlinux] [k] __list_lru_count_one.isra.2 [k] _raw_spin_lock 0,32% mysqld [kernel.vmlinux] [k] list_lru_count_one [k] __list_lru_count_one.isra.2 0,73% sshd [kernel.vmlinux] [k] _raw_spin_lock [k] _raw_spin_lock 0,35% sshd [kernel.vmlinux] [k] shrink_slab [k] shrink_slab 0,32% sshd [kernel.vmlinux] [k] super_cache_count [k] super_cache_count 0,21% sshd [kernel.vmlinux] [k] __list_lru_count_one.isra.2 [k] _raw_spin_lock 0,21% sshd [kernel.vmlinux] [k] list_lru_count_one [k] __list_lru_count_one.isra.2 This patch aims to make super_cache_count() (and other functions, which count LRU nr_items) more effective. It allows list_lru_node::memcg_lrus to be RCU-accessed, and makes __list_lru_count_one() count nr_items lockless to minimize overhead introduced by locking operation, and to make parallel reclaims more scalable. Signed-off-by: Kirill Tkhai --- include/linux/list_lru.h | 3 ++- mm/list_lru.c | 59 ++++++++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/include/linux/list_lru.h b/include/linux/list_lru.h index fa7fd03cb5f9..a55258100e40 100644 --- a/include/linux/list_lru.h +++ b/include/linux/list_lru.h @@ -31,6 +31,7 @@ struct list_lru_one { }; struct list_lru_memcg { + struct rcu_head rcu; /* array of per cgroup lists, indexed by memcg_cache_id */ struct list_lru_one *lru[0]; }; @@ -42,7 +43,7 @@ struct list_lru_node { struct list_lru_one lru; #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB) /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */ - struct list_lru_memcg *memcg_lrus; + struct list_lru_memcg __rcu *memcg_lrus; #endif long nr_items; } ____cacheline_aligned_in_smp; diff --git a/mm/list_lru.c b/mm/list_lru.c index 7a40fa2be858..9fdb24818dae 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -52,14 +52,15 @@ static inline bool list_lru_memcg_aware(struct list_lru *lru) static inline struct list_lru_one * list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx) { + struct list_lru_memcg *memcg_lrus; /* - * The lock protects the array of per cgroup lists from relocation - * (see memcg_update_list_lru_node). + * Either lock or RCU protects the array of per cgroup lists + * from relocation (see memcg_update_list_lru_node). */ - lockdep_assert_held(&nlru->lock); - if (nlru->memcg_lrus && idx >= 0) - return nlru->memcg_lrus->lru[idx]; - + memcg_lrus = rcu_dereference_check(nlru->memcg_lrus, + lockdep_is_held(&nlru->lock)); + if (memcg_lrus && idx >= 0) + return memcg_lrus->lru[idx]; return &nlru->lru; } @@ -168,10 +169,10 @@ static unsigned long __list_lru_count_one(struct list_lru *lru, struct list_lru_one *l; unsigned long count; - spin_lock(&nlru->lock); + rcu_read_lock(); l = list_lru_from_memcg_idx(nlru, memcg_idx); count = l->nr_items; - spin_unlock(&nlru->lock); + rcu_read_unlock(); return count; } @@ -323,24 +324,33 @@ static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus, static int memcg_init_list_lru_node(struct list_lru_node *nlru) { + struct list_lru_memcg *memcg_lrus; int size = memcg_nr_cache_ids; - nlru->memcg_lrus = kmalloc(size * sizeof(void *), GFP_KERNEL); - if (!nlru->memcg_lrus) + memcg_lrus = kmalloc(sizeof(*memcg_lrus) + + size * sizeof(void *), GFP_KERNEL); + if (!memcg_lrus) return -ENOMEM; - if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) { - kfree(nlru->memcg_lrus); + if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) { + kfree(memcg_lrus); return -ENOMEM; } + RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus); return 0; } static void memcg_destroy_list_lru_node(struct list_lru_node *nlru) { - __memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids); - kfree(nlru->memcg_lrus); + struct list_lru_memcg *memcg_lrus; + /* + * This is called when shrinker has already been unregistered, + * and nobody can use it. So, there is no need to use kfree_rcu(). + */ + memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true); + __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids); + kfree(memcg_lrus); } static int memcg_update_list_lru_node(struct list_lru_node *nlru, @@ -350,8 +360,9 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru, BUG_ON(old_size > new_size); - old = nlru->memcg_lrus; - new = kmalloc(new_size * sizeof(void *), GFP_KERNEL); + old = rcu_dereference_protected(nlru->memcg_lrus, + lockdep_is_held(&list_lrus_mutex)); + new = kmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL); if (!new) return -ENOMEM; @@ -360,29 +371,33 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru, return -ENOMEM; } - memcpy(new, old, old_size * sizeof(void *)); + memcpy(&new->lru, &old->lru, old_size * sizeof(void *)); /* - * The lock guarantees that we won't race with a reader - * (see list_lru_from_memcg_idx). + * The locking below allows readers that hold nlru->lock avoid taking + * rcu_read_lock (see list_lru_from_memcg_idx). * * Since list_lru_{add,del} may be called under an IRQ-safe lock, * we have to use IRQ-safe primitives here to avoid deadlock. */ spin_lock_irq(&nlru->lock); - nlru->memcg_lrus = new; + rcu_assign_pointer(nlru->memcg_lrus, new); spin_unlock_irq(&nlru->lock); - kfree(old); + kfree_rcu(old, rcu); return 0; } static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru, int old_size, int new_size) { + struct list_lru_memcg *memcg_lrus; + + memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, + lockdep_is_held(&list_lrus_mutex)); /* do not bother shrinking the array back to the old size, because we * cannot handle allocation failures here */ - __memcg_destroy_list_lru_node(nlru->memcg_lrus, old_size, new_size); + __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size); } static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)