Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3548856pxb; Wed, 14 Apr 2021 08:01:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2VN9kTwMRWn2sR90t9cCiFHwWBIkGmdnrm66SHB8W8bSunV2jX60WaYW7eHkqG+f9wAzI X-Received: by 2002:aa7:d588:: with SMTP id r8mr2028945edq.318.1618412505781; Wed, 14 Apr 2021 08:01:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618412505; cv=none; d=google.com; s=arc-20160816; b=abE0f3gRjqPzPFvlflmlask5oMpudK7M1I60hC+BkumzkVczC/kWeo6r6wedGjXAl3 eVljyzuVBXTH5aMnEkcKdt0ZnkoNmLoxstfCZKig8vfYCMcmNUffTeat07C/7dwU8q3g 1RDCDg7g+1qScTcerIjMUeRIcJtROcAWgIlg1jt7VR5CWVFCnmZa5mmjPmIdgf2KSCh/ HlxJ705ewC/ObOb0uN8Po2oXUG7NoxJKzoABbLxgPXdvhsIhnLM2UW9pOenonGxC/0PQ GlvJVXfsH4g7mW44bHIKkFY4CWQoz2eP77rsO1QVXMekoVHRcAMEBSGJ3xqaOq/aaiL/ M4UA== 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=eU2ARRKP2HKvM+HAl1bRPX/7KV3rG7+C5uUtQ33atXM=; b=H2Te/jA3jbLhA/mN0+wdNzLT1jHigJTZNAqLWC1GJLJp+Y6PO3e6nRYHh9sFx5woy/ 8+Od+68nvJMW2nRdicWWWU4UdGcQ/I6Jq1IK0ipXMBFuOhcLFg4PCT79RVJN25WmCZSZ Cf4IWeVRzm6VWdki8yfyH08LsA1tNiRid9GQOuQoHsGPVh2/8VHVJR5Qh2ppltaYI+kc yYN+cvzFw5FJIZ7+Wa7iPpWnuu04h/3SCWg+qftqnBJ2uP+l4sX5nmEoLn7yn1FMiZXl m3+Czz8VboCayA24JorBAA/g90tOSElJRAZZpZIqwCfCp08PegpAfGjJykxyMKr/2/rx wnyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=IGgGnIQt; 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 y11si6572424ejw.467.2021.04.14.08.01.21; Wed, 14 Apr 2021 08:01:45 -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=IGgGnIQt; 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 S1350359AbhDNJZN (ORCPT + 99 others); Wed, 14 Apr 2021 05:25:13 -0400 Received: from mx2.suse.de ([195.135.220.15]:35028 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350373AbhDNJY5 (ORCPT ); Wed, 14 Apr 2021 05:24:57 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1618392275; 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=eU2ARRKP2HKvM+HAl1bRPX/7KV3rG7+C5uUtQ33atXM=; b=IGgGnIQtjcrIzFVumv1YMozZ6hq3GY17VTmUjGWyzKv4uT67rU/hjhLEcBTyrE9Zhyx0jR kEPfF1i9Euyvldus/fo4V4iS0STdYp/aVRUQgQ8AM+AaUOwUjyb+wz6d3TQ33Js2M28Q82 g3v7PveyPuDVZ9u3lhscBZJ21+IPvwA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8BC63ACC4; Wed, 14 Apr 2021 09:24:35 +0000 (UTC) Date: Wed, 14 Apr 2021 11:24:34 +0200 From: Michal Hocko To: Muchun Song 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 Subject: Re: [PATCH 2/7] mm: memcontrol: bail out early when !mm in get_mem_cgroup_from_mm Message-ID: References: <20210413065153.63431-1-songmuchun@bytedance.com> <20210413065153.63431-3-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210413065153.63431-3-songmuchun@bytedance.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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? > 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