From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBE1DC4338F for ; Tue, 3 Aug 2021 10:50:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 364C260FC2 for ; Tue, 3 Aug 2021 10:50:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 364C260FC2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id C342A6B0033; Tue, 3 Aug 2021 06:50:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE49D6B0036; Tue, 3 Aug 2021 06:50:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD3D48D0001; Tue, 3 Aug 2021 06:50:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id 91D726B0033 for ; Tue, 3 Aug 2021 06:50:37 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id CE6FE1F35C for ; Tue, 3 Aug 2021 10:50:36 +0000 (UTC) X-FDA: 78433450872.20.EA5E3EA Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf15.hostedemail.com (Postfix) with ESMTP id 55FE3D00498A for ; Tue, 3 Aug 2021 10:50:35 +0000 (UTC) Received: from dggeme703-chm.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4GfBQ80YYQzckM6; Tue, 3 Aug 2021 18:46:56 +0800 (CST) Received: from [10.174.179.25] (10.174.179.25) by dggeme703-chm.china.huawei.com (10.1.199.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2176.2; Tue, 3 Aug 2021 18:50:28 +0800 Subject: Re: [PATCH 2/5] mm, memcg: narrow the scope of percpu_charge_mutex To: Muchun Song , Michal Hocko , Roman Gushchin CC: Roman Gushchin , Michal Hocko , Johannes Weiner , Vladimir Davydov , Andrew Morton , Shakeel Butt , Matthew Wilcox , Alex Shi , Wei Yang , Linux Memory Management List , LKML , Cgroups References: <20210729125755.16871-1-linmiaohe@huawei.com> <20210729125755.16871-3-linmiaohe@huawei.com> <4a3c23c4-054c-2896-29c5-8cf9a4deee98@huawei.com> <95629d91-6ae8-b445-e7fc-b51c888cad59@huawei.com> From: Miaohe Lin Message-ID: <702c05c6-fd8b-e1de-21e7-4be5b206958a@huawei.com> Date: Tue, 3 Aug 2021 18:50:28 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US X-Originating-IP: [10.174.179.25] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggeme703-chm.china.huawei.com (10.1.199.99) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 55FE3D00498A Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=huawei.com; spf=pass (imf15.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Stat-Signature: mtof356wmrj4ycb7cntce3mkn8hbjrz8 X-HE-Tag: 1627987835-643953 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 2021/8/3 17:33, Muchun Song wrote: > On Tue, Aug 3, 2021 at 2:29 PM Miaohe Lin wrote: >> >> On 2021/8/3 11:40, Roman Gushchin wrote: >>> On Sat, Jul 31, 2021 at 10:29:52AM +0800, Miaohe Lin wrote: >>>> On 2021/7/30 14:50, Michal Hocko wrote: >>>>> On Thu 29-07-21 20:06:45, Roman Gushchin wrote: >>>>>> On Thu, Jul 29, 2021 at 08:57:52PM +0800, Miaohe Lin wrote: >>>>>>> Since percpu_charge_mutex is only used inside drain_all_stock(), = we can >>>>>>> narrow the scope of percpu_charge_mutex by moving it here. >>>>>>> >>>>>>> Signed-off-by: Miaohe Lin >>>>>>> --- >>>>>>> mm/memcontrol.c | 2 +- >>>>>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>>>>> >>>>>>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >>>>>>> index 6580c2381a3e..a03e24e57cd9 100644 >>>>>>> --- a/mm/memcontrol.c >>>>>>> +++ b/mm/memcontrol.c >>>>>>> @@ -2050,7 +2050,6 @@ struct memcg_stock_pcp { >>>>>>> #define FLUSHING_CACHED_CHARGE 0 >>>>>>> }; >>>>>>> static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); >>>>>>> -static DEFINE_MUTEX(percpu_charge_mutex); >>>>>>> >>>>>>> #ifdef CONFIG_MEMCG_KMEM >>>>>>> static void drain_obj_stock(struct obj_stock *stock); >>>>>>> @@ -2209,6 +2208,7 @@ static void refill_stock(struct mem_cgroup = *memcg, unsigned int nr_pages) >>>>>>> */ >>>>>>> static void drain_all_stock(struct mem_cgroup *root_memcg) >>>>>>> { >>>>>>> + static DEFINE_MUTEX(percpu_charge_mutex); >>>>>>> int cpu, curcpu; >>>>>> >>>>>> It's considered a good practice to protect data instead of code pa= ths. After >>>>>> the proposed change it becomes obvious that the opposite is done h= ere: the mutex >>>>>> is used to prevent a simultaneous execution of the code of the dra= in_all_stock() >>>>>> function. >>>>> >>>>> The purpose of the lock was indeed to orchestrate callers more than= any >>>>> data structure consistency. >>>>> >>>>>> Actually we don't need a mutex here: nobody ever sleeps on it. So = I'd replace >>>>>> it with a simple atomic variable or even a single bitfield. Then t= he change will >>>>>> be better justified, IMO. >>>>> >>>>> Yes, mutex can be replaced by an atomic in a follow up patch. >>>>> >>>> >>>> Thanks for both of you. It's a really good suggestion. What do you m= ean is something like below=EF=BC=9F >>>> >>>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >>>> index 616d1a72ece3..508a96e80980 100644 >>>> --- a/mm/memcontrol.c >>>> +++ b/mm/memcontrol.c >>>> @@ -2208,11 +2208,11 @@ static void refill_stock(struct mem_cgroup *= memcg, unsigned int nr_pages) >>>> */ >>>> static void drain_all_stock(struct mem_cgroup *root_memcg) >>>> { >>>> - static DEFINE_MUTEX(percpu_charge_mutex); >>>> int cpu, curcpu; >>>> + static atomic_t drain_all_stocks =3D ATOMIC_INIT(-1); >>>> >>>> /* If someone's already draining, avoid adding running more = workers. */ >>>> - if (!mutex_trylock(&percpu_charge_mutex)) >>>> + if (!atomic_inc_not_zero(&drain_all_stocks)) >>>> return; >>> >>> It should work, but why not a simple atomic_cmpxchg(&drain_all_stocks= , 0, 1) and >>> initialize it to 0? Maybe it's just my preference, but IMO (0, 1) is = easier >>> to understand than (-1, 0) here. Not a strong opinion though, up to y= ou. >>> >> >> I think this would improve the readability. What you mean is something= like below ? >> >> Many thanks. >> >> diff --git a/mm/memcontrol.c b/mm/memcontrol.c >> index 616d1a72ece3..6210b1124929 100644 >> --- a/mm/memcontrol.c >> +++ b/mm/memcontrol.c >> @@ -2208,11 +2208,11 @@ static void refill_stock(struct mem_cgroup *me= mcg, unsigned int nr_pages) >> */ >> static void drain_all_stock(struct mem_cgroup *root_memcg) >> { >> - static DEFINE_MUTEX(percpu_charge_mutex); >> int cpu, curcpu; >> + static atomic_t drainer =3D ATOMIC_INIT(0); >> >> /* If someone's already draining, avoid adding running more wo= rkers. */ >> - if (!mutex_trylock(&percpu_charge_mutex)) >> + if (atomic_cmpxchg(&drainer, 0, 1) !=3D 0) >=20 > I'd like to use atomic_cmpxchg_acquire() here. >=20 >> return; >> /* >> * Notify other cpus that system-wide "drain" is running >> @@ -2244,7 +2244,7 @@ static void drain_all_stock(struct mem_cgroup *r= oot_memcg) >> } >> } >> put_cpu(); >> - mutex_unlock(&percpu_charge_mutex); >> + atomic_set(&drainer, 0); >=20 > So use atomic_set_release() here to cooperate with > atomic_cmpxchg_acquire(). I think this will work well. Many thanks! >=20 > Thanks. >=20 >> } >> >>> Thanks! >>> . >>> >> > . >=20