Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp156322imm; Mon, 14 May 2018 23:00:28 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoV1+xaGzheBbZjsfuPgO/Abz465/DNRXea9w+Cxp4b/pvPzStq6d16p9nW3ZqQkiqDI9yd X-Received: by 2002:a63:9612:: with SMTP id c18-v6mr10930652pge.361.1526364028640; Mon, 14 May 2018 23:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526364028; cv=none; d=google.com; s=arc-20160816; b=QWjnvu3vAUHLkeqgVWmMmaSB3tkInmWK6RZouRoNTvIEUV2Np12UxQzvXN0gzx9o9E l3f7QvMJk2CpZ6JF/ykTix6QxZB17mhYUEyxt5cKTCyZu4s26dIxHt3xj3Ri434U5zXE ZALbOzIt8nQcclQQ+j0gaIjr5F3XOF21MV0oiBFqzyks6044REwdzhCSNAurQnE8LCJm SdCrywA+aGpql4EEmz9UzLLSQpaWqbRauybfQue+zoFRWpjUFmGxtU3jcMKCpQ0YEh12 f5j1KKytdLDKILnjwLerjKXY4fFWm6zrlNomvVBjnItEXUidpUBc2sKG+qHSyACtg8HP wHNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:arc-authentication-results; bh=btwH76miO6IK7i6I98tTNpgcKlgMYfZdJ8LX7GfkoYU=; b=HrG8wiZcfaU3kzilax4eKk/mRpBdHyBEf/65NNsfHmq3NUwoGL1dwMhRbBZfSRdZUr joPeYGvQ4eygNz0USevcK4llBD/VeyCv8YAlFvuwJapd1z4qCLY/9LaYJeQvGQ42MzKt QbgRABK4sPRJLlOJiuXWB8Dac6atTb0uAVwMtp3ZNeTPssfGc7xR1+4oitbeCpvuRUT/ srxJPaS78BOqrQw1RCUV3uGm9YCPGGSOwaeB3OshTz66fs60e5yil2Mfs0+DxtVHQyQ9 QTcnxZP+GbTzE2QXAsgBqmycAsDQdqWg5n2VoXtCJ0p3vVtVJfLE6T4ZT35qJnlAydsB EWqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T6OwfZ//; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a1-v6si10921122pls.523.2018.05.14.23.00.14; Mon, 14 May 2018 23:00:28 -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=@gmail.com header.s=20161025 header.b=T6OwfZ//; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752410AbeEOF7T (ORCPT + 99 others); Tue, 15 May 2018 01:59:19 -0400 Received: from mail-lf0-f65.google.com ([209.85.215.65]:45093 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752090AbeEOF7R (ORCPT ); Tue, 15 May 2018 01:59:17 -0400 Received: by mail-lf0-f65.google.com with SMTP id w202-v6so720461lff.12 for ; Mon, 14 May 2018 22:59:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=btwH76miO6IK7i6I98tTNpgcKlgMYfZdJ8LX7GfkoYU=; b=T6OwfZ//HlOE1PayOCiv9QvrQLIG/TnVlkacux4bHPDT6/t8rOyjI+GD24t7CTi/O0 clZIRkixTAyWvvZlZYWGw7VNkZiI+T1daeFPRIbWxuFrK//RGQAsY5h1clQsJ6vL5Kce swn7b6+nM4f/YT2eqXydufJyOrZXvqBw6L1zBMevYSfUQVQmM2Iy0+cvjKTpejOM5OE6 iQ7mAKHuMzrGP/UJx5FkVxiln1/xq5pUGAcjaDtkAwbfcjqNU2/h9Xn85r/Bf0XruVON z3Qa37eZ/4R1nROf6jijuYaFPaEDoYWISi0SQ0evAmCLtG6PEzoUrTELED8rob5eyol/ n/OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=btwH76miO6IK7i6I98tTNpgcKlgMYfZdJ8LX7GfkoYU=; b=CcpM0Ky3xgdd4ue8nA8IMh8eLIrVi8coFRtTUC7RHZI7SFxU9jZ0JX4FH/DeKwrKRW sGf2BhuyeQXE943fZxro3+y0ksJRcPgxiXcQFYx8KnQpq+lLUScXtEf9jSHRj1bS2Na8 bVHz08La5D2x9nG3g/jZulKqxx5Bd9dnTBqlAvN2VnF48y3hDSJ9HpJS0KuUSjQQA3+6 UihcbRWCdchAtCCkPdsQpqYiGV57HzCvonxkVs6/3r8+xdruvtVj5rv3KFi1f0ZRIy7S Nx1T0sIhKvajXcKneSFkFOgJ+nmNdppla9ihqh2ShvZAOaqC608wc7ET4vMwP+IB8aJn 5MTA== X-Gm-Message-State: ALKqPwda/o7OB7CpySIMaA21fykkmmRpVo0saiwIsevYaTSMu7wfrSAP wXa8fAzCS6mYvntwao4d+FQ= X-Received: by 2002:a19:1fc6:: with SMTP id f189-v6mr10291203lff.98.1526363956121; Mon, 14 May 2018 22:59:16 -0700 (PDT) Received: from esperanza (81.5.110.211.dhcp.mipt-telecom.ru. [81.5.110.211]) by smtp.gmail.com with ESMTPSA id z78-v6sm2076033lje.11.2018.05.14.22.59.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 14 May 2018 22:59:15 -0700 (PDT) Date: Tue, 15 May 2018 08:59:13 +0300 From: Vladimir Davydov To: Kirill Tkhai Cc: akpm@linux-foundation.org, shakeelb@google.com, viro@zeniv.linux.org.uk, hannes@cmpxchg.org, mhocko@kernel.org, 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 Subject: Re: [PATCH v5 13/13] mm: Clear shrinker bit if there are no objects related to memcg Message-ID: <20180515055913.alk3pau43e3jps3y@esperanza> References: <152594582808.22949.8353313986092337675.stgit@localhost.localdomain> <152594605549.22949.16491037134168999424.stgit@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <152594605549.22949.16491037134168999424.stgit@localhost.localdomain> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 10, 2018 at 12:54:15PM +0300, Kirill Tkhai wrote: > To avoid further unneed calls of do_shrink_slab() > for shrinkers, which already do not have any charged > objects in a memcg, their bits have to be cleared. > > This patch introduces a lockless mechanism to do that > without races without parallel list lru add. After > do_shrink_slab() returns SHRINK_EMPTY the first time, > we clear the bit and call it once again. Then we restore > the bit, if the new return value is different. > > Note, that single smp_mb__after_atomic() in shrink_slab_memcg() > covers two situations: > > 1)list_lru_add() shrink_slab_memcg > list_add_tail() for_each_set_bit() <--- read bit > do_shrink_slab() <--- missed list update (no barrier) > > set_bit() do_shrink_slab() <--- seen list update > > This situation, when the first do_shrink_slab() sees set bit, > but it doesn't see list update (i.e., race with the first element > queueing), is rare. So we don't add before the first call > of do_shrink_slab() instead of this to do not slow down generic > case. Also, it's need the second call as seen in below in (2). > > 2)list_lru_add() shrink_slab_memcg() > list_add_tail() ... > set_bit() ... > ... for_each_set_bit() > do_shrink_slab() do_shrink_slab() > clear_bit() ... > ... ... > list_lru_add() ... > list_add_tail() clear_bit() > > set_bit() do_shrink_slab() > > The barriers guarantees, the second do_shrink_slab() > in the right side task sees list update if really > cleared the bit. This case is drawn in the code comment. > > [Results/performance of the patchset] > > After the whole patchset applied the below test shows signify > increase of performance: > > $echo 1 > /sys/fs/cgroup/memory/memory.use_hierarchy > $mkdir /sys/fs/cgroup/memory/ct > $echo 4000M > /sys/fs/cgroup/memory/ct/memory.kmem.limit_in_bytes > $for i in `seq 0 4000`; do mkdir /sys/fs/cgroup/memory/ct/$i; echo $$ > /sys/fs/cgroup/memory/ct/$i/cgroup.procs; mkdir -p s/$i; mount -t tmpfs $i s/$i; touch s/$i/file; done > > Then, 5 sequential calls of drop caches: > $time echo 3 > /proc/sys/vm/drop_caches > > 1)Before: > 0.00user 13.78system 0:13.78elapsed 99%CPU > 0.00user 5.59system 0:05.60elapsed 99%CPU > 0.00user 5.48system 0:05.48elapsed 99%CPU > 0.00user 8.35system 0:08.35elapsed 99%CPU > 0.00user 8.34system 0:08.35elapsed 99%CPU > > 2)After > 0.00user 1.10system 0:01.10elapsed 99%CPU > 0.00user 0.00system 0:00.01elapsed 64%CPU > 0.00user 0.01system 0:00.01elapsed 82%CPU > 0.00user 0.00system 0:00.01elapsed 64%CPU > 0.00user 0.01system 0:00.01elapsed 82%CPU > > The results show the performance increases at least in 548 times. > > Signed-off-by: Kirill Tkhai > --- > include/linux/memcontrol.h | 2 ++ > mm/vmscan.c | 19 +++++++++++++++++-- > 2 files changed, 19 insertions(+), 2 deletions(-) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index 436691a66500..82c0bf2d0579 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1283,6 +1283,8 @@ static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg, int nid, int > > rcu_read_lock(); > map = MEMCG_SHRINKER_MAP(memcg, nid); > + /* Pairs with smp mb in shrink_slab() */ > + smp_mb__before_atomic(); > set_bit(nr, map->map); > rcu_read_unlock(); > } > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 7b0075612d73..189b163bef4a 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -586,8 +586,23 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid, > continue; > > ret = do_shrink_slab(&sc, shrinker, priority); > - if (ret == SHRINK_EMPTY) > - ret = 0; > + if (ret == SHRINK_EMPTY) { > + clear_bit(i, map->map); > + /* > + * Pairs with mb in memcg_set_shrinker_bit(): > + * > + * list_lru_add() shrink_slab_memcg() > + * list_add_tail() clear_bit() > + * > + * set_bit() do_shrink_slab() > + */ Please improve the comment so that it isn't just a diagram. > + smp_mb__after_atomic(); > + ret = do_shrink_slab(&sc, shrinker, priority); > + if (ret == SHRINK_EMPTY) > + ret = 0; > + else > + memcg_set_shrinker_bit(memcg, nid, i); > + } > freed += ret; > > if (rwsem_is_contended(&shrinker_rwsem)) { >