From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (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 945F130DECC for ; Wed, 17 Dec 2025 21:45:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766007915; cv=none; b=QNg3FXwsO0+kFPfPTpCnBb6TgGVgygfSAixrdb35FSOTnKW/eSWXlddvsFPNziiMWXp6Y0w1SXa12TX2b6KY0k7hQVAjao/Tq3y4sDpsPXCuhUftq6o0x4fQ20/7kW+s6kyX4yiyiMGTNLC8agd3PTlIr7Z+RIhd5BnTxpmo5Ag= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766007915; c=relaxed/simple; bh=c3Mj8ueLcWSuPg9cu2uv4HjE8fHYx/hD1+nTe4fdLX0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Uq321toRcGhUF5ifL0yyPVf7Hdjz0AgNCDhWmi3WqLMaY1X0fLDWzhItRf5wl6UnW9rSNld/P5sMUWzgIWV/MfRDFkAZKqoArcDxa8iXmVV4W+P3Pt+pj2ruTWt7aY+N4Hp6VMVcttEv3bMIRo1Yamp9891xiooofdxFNVq/bCY= 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=bEjpfkas; arc=none smtp.client-ip=209.85.222.182 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="bEjpfkas" Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-8b2dec4d115so270885a.0 for ; Wed, 17 Dec 2025 13:45:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1766007911; x=1766612711; 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=cCQw+7EYcHrhI0VW083rNx16bRSH0hX/loU89idlDA8=; b=bEjpfkasKhSi32kMrLT76QNWhnSLIPAHmYjIG36CMKu7Yjw9EaaC2WLTTqXlb8Qttp BTVCDBLMcoKM5KFwIhNE6doQPmVpUpSqddB+ywgFgk4NyJlnLY7OGDJGaKEDcHmAXMWY KEhRFky6fNSTFTRl/HJ/VOZD7/8Wmo9uFH8UuXOsrJBwYoE5ftsoQsn0Q3wYWixM7wTK H1yY6DQR4ox3n9OKwAcHkg45qK5c8Oz3Nn9VEzrjISP9ARejTSYBrcvVVEeKIu5JV/IW QoL4/I4mYwKeGdB/ox/Xdl2EKVi5HIiWc9uTCLSG43VKCvgzYmwWwhmVOUy1IRup9acj UjKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766007911; x=1766612711; 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=cCQw+7EYcHrhI0VW083rNx16bRSH0hX/loU89idlDA8=; b=JAZJ5EMrXFbSYWtowQDwS00i/8tScGpQWAmw54QrNxOHpDKB+bpMCLIEPYsqb0ql7x j9BuObD4X9aD6b0LS50Rmv9qKNvcqahQ0kaizgIF7PzZPSXIKHGRCETMPRIUzjXBwRGk RE7SDZ0UXFjubGN4h2hN0QYvWy1mj5cfUMKeQclEBDJhnPm3p13pJoKXsb5/ISSfHZLH bEDIla93y+nsZ5kaboKxWqEdTvF9bkdt7z19/Yg91Ld1TALe1TGEbvdRMDlLt7mML5cD tcK2A7n/NBLV4mSQMFeuQfApBvi0WiRFWI0Dh6tfRB4pnm/gP9Xl2ocZbhaKLqHYS6HA 2eKg== X-Forwarded-Encrypted: i=1; AJvYcCWTm8zC8MCsQYuo8YoT01betUOFDrU0OvWI7/TZhRdVKWBWzyw+zUE1LhdWPd9m9FDqSfWf/DdwyPE8z6w=@vger.kernel.org X-Gm-Message-State: AOJu0YzHeA8NKt9KYLHBKjqgwIPKn1sv14euqVWY+pUt5b3fEJSpfDzm CXCoXE5Lf48qWUZ6QzEOkbNMHtRW/SCqAQnJIBXaiKxiG8s63hb5kjMs6vUWI9Edlf8= X-Gm-Gg: AY/fxX5QpSpzjpuKMqgoliFpbGYwNP3rPqHN1Jz82gcLd6G9V6tHY3hCqhftWnanHE2 VXucQT/XVkr/Lb1Acfrc0p5zD3LuvML5mmRLdvLHrf/OUrgtjIY8wt7hSIFLcbxcm+5Pbzlk8VH bltaGnG7+2LypJkrxt9Ex+4vnvUFhQoqh4eZnQNEWt7kBQzikhEzFZEeHN+w1JMz4QPnVjCTI1L 3ayJgK1p7u8l+baD38+E/bDzOScwqhe7RQ9X2ifsu3HRWyDakeTy7LYGB15h+ipYlbk/NDyRumh iUTlGyMumIfn0uRcLHdg/whGJwwz4ACOmfniqYpMztRwLyCRJh1i2YzY2dfxLccqOZ3JCIyaju0 aIRUC4CIKzom5toNMZ4hndUHmqUU5Ix/9xqMnL0zkphwxpiYp+aNDZyeks6xOF+pQAfraHI3vJe HBXtVWlB00Tw== X-Google-Smtp-Source: AGHT+IE7IeAzYEmDHvM2P4dl1qsqF97yDx6kfts+1/gZKkwJR3IpfyAacSXi+sv6340TQRr4kVA0Gw== X-Received: by 2002:a05:620a:198a:b0:8b2:7679:4d2d with SMTP id af79cd13be357-8bb3a388998mr2861684185a.63.1766007911186; Wed, 17 Dec 2025 13:45:11 -0800 (PST) Received: from localhost ([2603:7000:c01:2716:929a:4aff:fe16:c778]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8beeba2bca5sm31763985a.27.2025.12.17.13.45.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Dec 2025 13:45:10 -0800 (PST) Date: Wed, 17 Dec 2025 16:45:06 -0500 From: Johannes Weiner To: Qi Zheng Cc: hughd@google.com, mhocko@suse.com, roman.gushchin@linux.dev, shakeel.butt@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: <29e5c116de15e55be082a544e3f24d8ddb6b3476.1765956025.git.zhengqi.arch@bytedance.com> 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, and the folio's objcg is reparented concurrently. We'll retry until we find a memcg that isn't dead yet. There's always root_mem_cgroup. It makes sense, but a loop like this begs the question of how it is bounded. I pieced it together looking ahead. Since this is a small diff, it would be nicer to fold it into 27. I didn't see anything in between depending on it, but correct me if I'm wrong. Minor style preference: /* Comment explaining the above */ do { memcg = folio_memcg(folio); } while (!css_tryget(&memcg->css));