From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f66.google.com (mail-qv1-f66.google.com [209.85.219.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC44828689B for ; Fri, 19 Dec 2025 03:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.66 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766116442; cv=none; b=dvTGi/MSRFxTON/QqVFW5me0gExuFFxmmq7pd/KSgFdixbXPmqYhTmGHQzWl0QmKFa+hkNkyEb1rYvSLbMGGvEbqMqTWzWQ3TOu90rnKnCkdLVadwpoGeQdgn+FyTrbPkeqgE/79QA5wBMs+7DOjWq8DwW9Xp4fmHYzUPzYaGbk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766116442; c=relaxed/simple; bh=m8ZL37y0DIZeNwZv7PopgpSgmg4+LbNmSlodWcrWwnU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rpL/pa5K3R52nsIIkQWa93EvIUzes5Ejd0bLPThFE1Tsfzk0KSF/S7Kld0si3TmKMWHb54AvQcR7peg1suenVtww2avsvRc8p9RXiPT/qLvdmjFQMI+/QiY/u+uHlrKzinzv5UM9958aJOQLIindy6cQK+EizHAF5zS0SePNBTo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b=hRcoc9nF; arc=none smtp.client-ip=209.85.219.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b="hRcoc9nF" Received: by mail-qv1-f66.google.com with SMTP id 6a1803df08f44-88a379ca088so14141176d6.0 for ; Thu, 18 Dec 2025 19:53:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1766116437; x=1766721237; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=wO8ZD48aUCpKc6SiZGez/drh8VR8QFTGkKklbJiyBj4=; b=hRcoc9nFPOMC3LyqBhmfLePHPgwIfEFE6LDuTV6c8SaYCeA9fxWJU2D0chayz6E2Jr go7WV4AHkTRsWNI5EvselxNeNn49xvUd8yggep+nw0ve5Umr9/WV/UR/WCOiG0tHK4j5 d5pe0+Cer/ajFdsvvP0NXqeDCiD3h264g2kd51H2adthv9kJng8LkQVIPcZCB49sXNDf S92K2SqeAkX+IfG9eIrLmWdXJYfXrEMhnDs3CYDgKlRZqA3wTJB0F0zypmQSSzE7s8VU 2b0ht4hKVAjjF960/vlv2mjIQyJCugZO4sC2XwF7NpnKmVtzg1FNTFpY4j4par5iXln6 pmBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766116437; x=1766721237; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wO8ZD48aUCpKc6SiZGez/drh8VR8QFTGkKklbJiyBj4=; b=Tr4KmFNDirxSu8rS2t1aXpvpTGrIzbfQA30MXGqshHiu7emtEgjQVkn3FIZpOdA2Ga 47evSwFPzp8ADZvYqDCioVoewG81Bwb2aOQzSkAyT6h2aCuEhKDZBa1ZAW+wfwHQbw+b momnK4DrP4PWB0A7fnd4b8399vaWhrAilgg1DFEzE+zT8RB2ZDyeSoi5qtkP3hLFg/Ni mwCyyrTOv9s58XfhUCQvL2QWHYyoMWFyPpnZaxXi7Iyk8Wq/vN4fzl9tbNEV8uGXif2q n5x5sAru+eaCmXx/XbGTv0Z3nlExGFuL8HTs0musTuqjBFJ+uxZ8PC6yjPiKD0n3i4vz eGJQ== X-Forwarded-Encrypted: i=1; AJvYcCVEytw9hMSnMyH6Ti3RSjW5zzUVUVZVxHplLaTopYiQOsVlJkE7rruPuLamiARTnCs1heeuKKrW6S7HJ7A=@vger.kernel.org X-Gm-Message-State: AOJu0YyJnLVz7K6F+pS/fG9jAslSznkYVZaCDU5fzWM0TPMHa3UEczpe xFPK1OW0ifY4H7d1u2T0OE7TuepO2lIdfn3tS6RTtnIc9/bNfUAFes/2ZnYQmJbdYTU= X-Gm-Gg: AY/fxX4flZ7ofh80WWWUt0gboMWYggtVNBO+crT6VBUsrqVKclHOwHbtO95nofk8jYp Jg2ahrXqm8U8T2VFDQW3YmWe4E9y5j4mh8pEL4V+U5pQpnZucXuppyB9Hf2sFgw7yvex2y8fLeH USet9Bz52OjKF2nCvRiAEq0YqSVtRN0jwgAmdkGRLrEpweJN301K611Agf8SBAeMZ+rtSfbhdVP jeOaGY4xCjdM86XyGY7Q44AflLO0Q2ttVst0NSJbleSsIM01Fe7ns1QCvxhrlvrYljyYtC7I6Z2 GVX6UDR5N4PB6X0axNxchi5ggqWKcX7yYOsqUOPfAJw7TZkLTNMpLk366wHYsAx4PKfAg5qOX8L RjrqyxG2btzl1i5irxV7gcqBo2RCn6mCcoxxdpfXq3x299RdgiudAeJoiSM4EJH2DdW1cAflqen UxiFLEwEzYLA== X-Google-Smtp-Source: AGHT+IGuYY97o/y/ARn4XW14juzlOImdtCYWCwBKScTq7uY6qi729cqFufMxSnRLQQHTYA8ofovkJA== X-Received: by 2002:a0c:f40f:0:b0:87f:b2d8:6020 with SMTP id 6a1803df08f44-88d86290cffmr27126906d6.38.1766116437011; Thu, 18 Dec 2025 19:53:57 -0800 (PST) Received: from localhost ([2603:7000:c01:2716:365a:60ff:fe62:ff29]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-88d9623ffd2sm10768806d6.9.2025.12.18.19.53.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 19:53:56 -0800 (PST) Date: Thu, 18 Dec 2025 22:53:52 -0500 From: Johannes Weiner To: Shakeel Butt Cc: Qi Zheng , hughd@google.com, mhocko@suse.com, roman.gushchin@linux.dev, muchun.song@linux.dev, david@kernel.org, lorenzo.stoakes@oracle.com, ziy@nvidia.com, harry.yoo@oracle.com, imran.f.khan@oracle.com, kamalesh.babulal@oracle.com, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, chenridong@huaweicloud.com, mkoutny@suse.com, akpm@linux-foundation.org, hamzamahfooz@linux.microsoft.com, apais@linux.microsoft.com, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Muchun Song , Qi Zheng Subject: Re: [PATCH v2 08/28] mm: memcontrol: prevent memory cgroup release in get_mem_cgroup_from_folio() Message-ID: References: <29e5c116de15e55be082a544e3f24d8ddb6b3476.1765956025.git.zhengqi.arch@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Dec 18, 2025 at 06:09:50PM -0800, Shakeel Butt wrote: > On Wed, Dec 17, 2025 at 04:45:06PM -0500, Johannes Weiner wrote: > > On Wed, Dec 17, 2025 at 03:27:32PM +0800, Qi Zheng wrote: > > > From: Muchun Song > > > > > > In the near future, a folio will no longer pin its corresponding > > > memory cgroup. To ensure safety, it will only be appropriate to > > > hold the rcu read lock or acquire a reference to the memory cgroup > > > returned by folio_memcg(), thereby preventing it from being released. > > > > > > In the current patch, the rcu read lock is employed to safeguard > > > against the release of the memory cgroup in get_mem_cgroup_from_folio(). > > > > > > This serves as a preparatory measure for the reparenting of the > > > LRU pages. > > > > > > Signed-off-by: Muchun Song > > > Signed-off-by: Qi Zheng > > > Reviewed-by: Harry Yoo > > > --- > > > mm/memcontrol.c | 11 ++++++++--- > > > 1 file changed, 8 insertions(+), 3 deletions(-) > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > index 21b5aad34cae7..431b3154c70c5 100644 > > > --- a/mm/memcontrol.c > > > +++ b/mm/memcontrol.c > > > @@ -973,14 +973,19 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) > > > */ > > > struct mem_cgroup *get_mem_cgroup_from_folio(struct folio *folio) > > > { > > > - struct mem_cgroup *memcg = folio_memcg(folio); > > > + struct mem_cgroup *memcg; > > > > > > if (mem_cgroup_disabled()) > > > return NULL; > > > > > > + if (!folio_memcg_charged(folio)) > > > + return root_mem_cgroup; > > > + > > > rcu_read_lock(); > > > - if (!memcg || WARN_ON_ONCE(!css_tryget(&memcg->css))) > > > - memcg = root_mem_cgroup; > > > +retry: > > > + memcg = folio_memcg(folio); > > > + if (unlikely(!css_tryget(&memcg->css))) > > > + goto retry; > > > > So starting in patch 27, the tryget can fail if the memcg is offlined, > > offlined or on its way to free? It is css_tryget() without online. Sorry, I did mean freeing. But in the new scheme, they will happen much closer together than before, since charges don't hold a reference to the css anymore. So when css_killed_work_fn() does offline_css(css); css_put(css); on rmdir, that's now the css_put() we expect to drop the refcount to 0 even with folios in circulation. The race is then: get_mem_cgroup_from_folio() cgroup_rmdir() memcg = folio_memcg(folio); folio->objcg->memcg offline_css() reparent_objcgs() objcg->memcg = objcg->memcg->parent css_put() -> 0 !css_tryget(&memcg->css) and the retry ensures we'll look up objcg->memcg again and find the live parent and new owner of the folio.