Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp304384pxb; Wed, 14 Apr 2021 16:09:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8sxffPp9OFjd2IeqiQLcOhGSSdjvZOyb3f23GBY+goBmhE8pRtOwnGHqNL+IDAq95fkaQ X-Received: by 2002:aa7:df81:: with SMTP id b1mr666827edy.1.1618441788810; Wed, 14 Apr 2021 16:09:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618441788; cv=none; d=google.com; s=arc-20160816; b=dRbAPjQfbTO+piO6azW2w3vXadfg7JoJBmVy/m1totJNReT0cYg5QVs4Z1X2/q0nSB GkRZ8y6l15FQvxIpuQN2lmEHBeV/kDq7bI1J4VT00wupjyFtJu45pnp1XS+famnSueF1 e5NNagaLqxNQtq5/x1IS/Zmsgo6Za1wtGtAvxPBFc98RwyWz5DWBUKTbTXZLDFcvT4+t vkiG/OeEIAVxzR5dMh3EW0hcLHoxvXbOzBccST6pCrPoP9bDMd4j0Rg8DVs4uQOm9zLR 08TXIhmsqKsLoG9PAaVTegdIGvWN8K7pYFYfRC/kxzVhCebw/8pEE9HxpIuQqXJ4u21e tmrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WNtB4o+0je6m6Iv/hxpsZ7k1lBzZu9C3dQfxcFge/sY=; b=dZgC93zzghBAJf5aR6PIv3OZ74OWSyKhAdV+LOirmLVAHPanCsc6UTMnFqKRHiMl76 S6FmBvDRGhPN9K0/l9KsH6bPVuBUTOwvdppcKKmE+kN1mAtue88PVspPNU4gGC1/LG1H Ody6CPYsM9wjisYMPf1BvWb2wueiTPRBs7PWx5BH3jAoWXyqahlDfz1DKHJ/AZoPxr0/ sP8B3pnmkjP9egppP92o3FkaI59HZOhCyIAEmq6Wan/V91fgZMghaprMv3nEQdLp8YCE yjXQHmJV4EpmRPgFWxc1lzup9N0GVnxa1FgeAOtcDq5BPQ68Kz+6ok1lHzBFWAWGc+4h aVTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=K2suukuN; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l33si971303ede.161.2021.04.14.16.08.25; Wed, 14 Apr 2021 16:09: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=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=K2suukuN; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233216AbhDNKFi (ORCPT + 99 others); Wed, 14 Apr 2021 06:05:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231645AbhDNKFe (ORCPT ); Wed, 14 Apr 2021 06:05:34 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39966C061574 for ; Wed, 14 Apr 2021 03:05:13 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id l76so14040712pga.6 for ; Wed, 14 Apr 2021 03:05:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WNtB4o+0je6m6Iv/hxpsZ7k1lBzZu9C3dQfxcFge/sY=; b=K2suukuNiVkLANxIkI3yCx5pOloVIYD8+TW6JJAGH7k9zrO0/A+tXKSMs1r8qgYHom dKbXsUqNx28Q1ZC8G+CqSgBLdX/6p97YfgyLWtPv37D/rqB4Sn5HjJFW2vgyxEyR/6rv KfdYTYJFgCb3TUdmWTr+qXWg6r8FlYO5GvL7FjU0O/4CSHXO99VPHM8FXvmOjPlTOWYt Qah3E3fu04pTj1qB0Q6sOAt3dsQCAKxKpfgfN8zHRKSqlI6bjeDoRZwXNWDd2Iezhb+G 2n5xuyRPsm7In+XuOJKGE5csNq5ddTP5NFsWGLCFD5LckfegyRDWdcTUiDz8yJK80jcr dqqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WNtB4o+0je6m6Iv/hxpsZ7k1lBzZu9C3dQfxcFge/sY=; b=AbeuUwGKubrPXSo3/c8HnDHu3/9OrNK/FMMkr7e3L9nlqlilJ+9F7J2WmBBwXw7Xrz U/2wJK2ZnI7fCnUBHRhAYss85ZZzPvEMUwbd3rGPcG8Wtp2xXcdzRQgEfqKlMqzUCCy2 lWtJmaGSBaaJtM6OvJ2OXft8i0U540JiIg4NQJowfBejAoWit/K3C7vJ65kaPRU8+QHJ QzjysCrsRBRc2k2tkh1pAhVqxDkGVxaX/G0ptJtPrgb5gn490L+sMKRU0tQGw9gxOdMx AjTHEPYAijtnCqX8aV61Y5ofYeKwS9a95E19ZGP+BmsoEAUPkCL6MZP9CygTXMFUgqxi fK3A== X-Gm-Message-State: AOAM5337LL86LDUa4ViHBs/h1mDVEJJXF+x2ukHMyx/VOKwGo+DvuD7X 3xK5amrlsc6WyL/qCwXUvaexCSGvqcIgJd6I+Ayt7Q== X-Received: by 2002:a65:6645:: with SMTP id z5mr35991057pgv.273.1618394712780; Wed, 14 Apr 2021 03:05:12 -0700 (PDT) MIME-Version: 1.0 References: <20210413065153.63431-1-songmuchun@bytedance.com> <20210413065153.63431-3-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Wed, 14 Apr 2021 18:04:35 +0800 Message-ID: Subject: Re: [External] Re: [PATCH 2/7] mm: memcontrol: bail out early when !mm in get_mem_cgroup_from_mm To: Michal Hocko Cc: guro@fb.com, hannes@cmpxchg.org, akpm@linux-foundation.org, shakeelb@google.com, vdavydov.dev@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 14, 2021 at 5:24 PM Michal Hocko wrote: > > On Tue 13-04-21 14:51:48, Muchun Song wrote: > > When mm is NULL, we do not need to hold rcu lock and call css_tryget for > > the root memcg. And we also do not need to check !mm in every loop of > > while. So bail out early when !mm. > > mem_cgroup_charge and other callers unconditionally drop the reference > so how come this does not underflow reference count? For the root memcg, the CSS_NO_REF flag is set, so css_get and css_put do not get or put reference. Thanks. > > > Signed-off-by: Muchun Song > > Acked-by: Johannes Weiner > > Reviewed-by: Shakeel Butt > > --- > > mm/memcontrol.c | 21 ++++++++++----------- > > 1 file changed, 10 insertions(+), 11 deletions(-) > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index f229de925aa5..9cbfff59b171 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -901,20 +901,19 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) > > if (mem_cgroup_disabled()) > > return NULL; > > > > + /* > > + * Page cache insertions can happen without an > > + * actual mm context, e.g. during disk probing > > + * on boot, loopback IO, acct() writes etc. > > + */ > > + if (unlikely(!mm)) > > + return root_mem_cgroup; > > + > > rcu_read_lock(); > > do { > > - /* > > - * Page cache insertions can happen without an > > - * actual mm context, e.g. during disk probing > > - * on boot, loopback IO, acct() writes etc. > > - */ > > - if (unlikely(!mm)) > > + memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); > > + if (unlikely(!memcg)) > > memcg = root_mem_cgroup; > > - else { > > - memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); > > - if (unlikely(!memcg)) > > - memcg = root_mem_cgroup; > > - } > > } while (!css_tryget(&memcg->css)); > > rcu_read_unlock(); > > return memcg; > > -- > > 2.11.0 > > -- > Michal Hocko > SUSE Labs