Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1393325pxy; Sun, 1 Aug 2021 23:51:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6r3lw+a/bqORHcBOB7kz1PyxLOZkVbRD5uOZjvVoggdLttW9i0sGn81A4ASANq4OCYiKs X-Received: by 2002:a17:906:49d5:: with SMTP id w21mr13865216ejv.30.1627887108153; Sun, 01 Aug 2021 23:51:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627887108; cv=none; d=google.com; s=arc-20160816; b=yUeWet9TY1tDvrllSFI9Stiw68GpkMmj6CyZ4fEyRezS3zfscPQEZo/0818wlE01vL ff8MaFZhWqt5Yh5W16eZ2qR0DeYU9yGdlU5hnrKRgTfBQ++VXQyKWuRFXCNpjLXe0FiN QUCqoT0rnWV3oy6LRKA9NJD1UlA2jlEJXaYbg/vhxk8zWhgV1uDmYqs2fb0ANK4addUa ouHxO/O9oD2tm6cD8bO3unynGLRW3BOpSwFL/PDFppSOUTRSjW0H4g3boSSHXxBDVbgY 8oCi0yubALfhEYYoosvcp3D+w1870I0oelk01IARUDOiOwYiaxLZ1NihEdpWnBxjNUDv Tzbg== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=q7PGCIa5dKr+zd9QXo1w6BYL7jP8T2ubQzOp7fxIYBo=; b=J/KPVbYNv1nB4vOdFxxJsn2tLzCV8lRwZNP9GTwieKiWwuuijZ9SKZkf/7wdur7WDx sgD7MgPpEZnbvq65buOyuwJjrGxpIal/8E0JYtMHU/gCD6T9vIpFeWeGZVfcs9zvbW4I sUwjcnvDR62upxynsnRpJzj2S2YOkADkA5LI8zcFRqakgxwD9Xfsyw1Zs+bO0Gv594nk ednKIsdNMLAG63MTkVtqG+xniEIJkD+v75COVn+lFi8KFM75CLKCw2PExdSLwbcmCP8+ ckxogYEG8WdCB/KkJ2BK6xqOtB2XqOZOqttzlLbsW7rbdmsdpC/hFhrRZRJ8yxCIYRYo oYdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=HU962FsC; 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 t23si9582993ejy.650.2021.08.01.23.51.25; Sun, 01 Aug 2021 23:51:48 -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=HU962FsC; 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 S232298AbhHBGtb (ORCPT + 99 others); Mon, 2 Aug 2021 02:49:31 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:36690 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229734AbhHBGta (ORCPT ); Mon, 2 Aug 2021 02:49:30 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id A13871FF2C; Mon, 2 Aug 2021 06:49:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1627886960; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q7PGCIa5dKr+zd9QXo1w6BYL7jP8T2ubQzOp7fxIYBo=; b=HU962FsCvbDVraAaI8paM+qVfe5IewCn44+dP/CX81ZlPfpNkBA8DsBLvPSoYPFZoqjIiN xNW7R0UwCkOFg8/qZuytNpEVqMkSnSw/rnXR4hQuuwbd3VXQhH/3XeLGwPZFQH5L/4+NIz E1gqlLeGGJ84o7CSyorpMjlhAUSJQC0= 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 73312A3B85; Mon, 2 Aug 2021 06:49:20 +0000 (UTC) Date: Mon, 2 Aug 2021 08:49:04 +0200 From: Michal Hocko To: Miaohe Lin Cc: Roman Gushchin , 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> <4a3c23c4-054c-2896-29c5-8cf9a4deee98@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4a3c23c4-054c-2896-29c5-8cf9a4deee98@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat 31-07-21 10:29:52, 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 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. > > > > Thanks for both of you. It's a really good suggestion. What do you mean is something like below? > > 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 = 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; > /* > * Notify other cpus that system-wide "drain" is running > @@ -2244,7 +2244,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg) > } > } > put_cpu(); > - mutex_unlock(&percpu_charge_mutex); > + atomic_dec(&drain_all_stocks); Yes this would work. I would just s@drain_all_stocks@drainers@ or something similar to better express the intention. > } > > static int memcg_hotplug_cpu_dead(unsigned int cpu) -- Michal Hocko SUSE Labs