Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp6709744pxv; Thu, 29 Jul 2021 23:52:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzooStIuQ6AtFaXlQbo+Zlpx+lQati4k/y1JqWmlVgB3+9I6s/3tCIrKSdldEn0EzuAu/sj X-Received: by 2002:a50:ed14:: with SMTP id j20mr1170744eds.323.1627627935736; Thu, 29 Jul 2021 23:52:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627627935; cv=none; d=google.com; s=arc-20160816; b=YEBFRmFgo8yHSvUMEvuNQnebbBmHymhUSSF96Jd7evTyiTXVpi12A6F8SBK84Y+6kU X6lny2I6Uf2CSJwrx7P6vx6rbFQ8jjw+saHI33LvboUPb2Sv7ZtomokfOrPBojrnfFeU HLKXMGF1wiQAs1C8F0hTpunZlrd/TVeqCp3nqz5AhNWpd8aJScq74fh1uX6EVY+YBwaG 5m+4Ff8RjFdbCLGn9mOhv25/a2IgdiqTM71PbpDehbwQ7ovr8rGXAaqekJJ9a8qpZFqP LJ8A7EOMvFz/aQv7D8qC3HwZ0EMySxmyfqoVS4W7HrcJ1LTglR127/aviomjTDUiRo6Z eMLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=xojnLgK/32nxOjCn8ZrhjIhAgNUPqzZVxeCUL5Yhwn8=; b=Hmm3Fk5hXG/QjEVnlQJSgEYzF2IJGvbkFsoalEEUOG0JpXMD2WU+Yi4p9ArMZnDEJW CCRJ9wkcxf2uCl2ffiJsmujaauM87md48gUa5V+e7tvrLOTVX4LBsTg8sfhQcMVu+Ibc 0NtyKIMSwRdNfhSNvXzNhMCirzoJlijv0+LAttd1xabGnudnQJzCK/C79ARRLtDnSabr /fJ777BOKwYaYzo70Ar7scYonYqkrkAnzMEpNXK5rXs4BZAl0JA95YPJj7oXPdueTu3a sL9HxLFFBqTiKb/xTPwM3vlkMiay/XsU1lZ9MYOth2pIT2mQ96nF4yxkTocKzr36Ztqg 5H/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="hAr/6b2q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f19si779739ejb.459.2021.07.29.23.51.52; Thu, 29 Jul 2021 23:52:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="hAr/6b2q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237638AbhG3GuK (ORCPT + 99 others); Fri, 30 Jul 2021 02:50:10 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:41742 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237278AbhG3GuI (ORCPT ); Fri, 30 Jul 2021 02:50:08 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 4A6841FDB2; Fri, 30 Jul 2021 06:50:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1627627803; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xojnLgK/32nxOjCn8ZrhjIhAgNUPqzZVxeCUL5Yhwn8=; b=hAr/6b2qqY+vI9OuPMkGd1wYvnwVpqYOzmgAWJVVfz/cuy4qh4vYDL8ENtabW+YJ+NeJuV S+MgrWHeMKCcm8MUeT3IoYVeA5003ijTHbYjXne5Fz+LyJAh2BJittkpTk5+5blrGinP4y b0qhbxN2wMXENaebAmqI7+QsEl98acY= Received: from suse.cz (unknown [10.100.201.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 0D53EA3B87; Fri, 30 Jul 2021 06:50:02 +0000 (UTC) Date: Fri, 30 Jul 2021 08:50:02 +0200 From: Michal Hocko To: Roman Gushchin Cc: Miaohe Lin , hannes@cmpxchg.org, vdavydov.dev@gmail.com, akpm@linux-foundation.org, shakeelb@google.com, willy@infradead.org, alexs@kernel.org, richard.weiyang@gmail.com, songmuchun@bytedance.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: Re: [PATCH 2/5] mm, memcg: narrow the scope of percpu_charge_mutex Message-ID: References: <20210729125755.16871-1-linmiaohe@huawei.com> <20210729125755.16871-3-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 paths. After > the proposed change it becomes obvious that the opposite is done here: the mutex > is used to prevent a simultaneous execution of the code of the drain_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 the change will > be better justified, IMO. Yes, mutex can be replaced by an atomic in a follow up patch. -- Michal Hocko SUSE Labs