From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.103 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795334039; b=lkGhln1g7jyFiqVCGqLBn/h7qS8Nt3iDiTYEt9QoyGpxtbnNjYSh+AF+If20rCXkASsO IEjRbzST8QNdNo7oB/adUzQfJI8Z0NvhaZ/rbY2yQU1w7TyrFgWSCKy08cX7/pTPsUCvc YSu9CfyDDSuWK8Djc5KiV0rw0PZNy/JrSg+CyqyI+p7Z9/NeW23HcMa8dN4CQRewKx3tw eNM8yyUBX3f49HpDRIpFCuQD1sylAajvFhVdQ7nIoGJI0A3yYba/cz/iufgk7PIBc2/sE 5WsWq1V3uHykt3Iq6VhE5fR84gLANUCNjKfZCrIFj2QeBQlErIVbEN7rpoAxBSLg3Og== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795334039; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=mOcmoHBzKwzqRhuvzie4cVIDXlVg/sxRWH6argNn2FA=; b=wEZVWBrTBGWsiwRwc1jtYLdB/8nU6ApjXfYykIHXNPy9IXarWE5eNQmqkM5iHc8nKgmS j8vi61y/OTbb/DDuy8eeA/Habydzzpwe3AW1ABnUHvDPNLaCJ2z8wq74eoOjNTnS/s9k6 Wb557clsz4Fu4T2MJDaNDMIze9/dquq8pjJAhGUO+Kk4OzbxFhBi6GYIIVJ4vQUS7/5pi qCRwodJYBhMi9O+sElmS7bBMzSdC+lVjtPZla72BXZb8yS+Ia6i3NOQ0AC5jMPpuZ0FT0 h+K8S1bEPRayGL93Vhd0gvb/lBDNTOQu3AIKBVkFQSdzk1IOev0cDaC/bZ46c05odYQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=LHuP4vQ0; arc=none smtp.remote-ip=91.218.175.103 Received: from mta0.migadu.com (out-103.mta0.migadu.com [91.218.175.103]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E56F606A320 for ; Sun, 22 Nov 2026 07:53:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5E56F606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=LHuP4vQ0 X-Envelope-To: linux-doc@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=zkTOqn+rNjlOfmFIyhoF6rAk683YaTwi6c+aGGYoa60=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795334036; v=1; x=1795938836; b=LHuP4vQ0LbfH0TkiIc7OPnMdIxBu3k1zCmUWVwzAtOnjVmf0a7rwmnpud7efP4ws1s4i4kHF 1hxO0Ln+Tqt7z1JUMNar2v1E2mG9HikoNFNE88J416rT3vWkwiJ2+7S1dIDKNCSy/sr0MuM4ANQ MzPpOjU+kVGOeQw/LXMgJJJo= X-Envelope-To: linux-doc@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 862c2fb0d81c39f5; Sun, 22 Nov 2026 07:53:56 +0000 X-Mizu-Trace-ID: 862c2fb0d81c39f5 X-Migadu-Flow: FLOW_OUT Message-ID: <42207e26-ba7f-4df5-8c80-71fa1b84d26e@linux.dev> Date: Sun, 22 Nov 2026 15:53:44 +0800 X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 04/17] mm/memcg: add folio-based lruvec live helper To: kasong@tencent.com Cc: Andrew Morton , Johannes Weiner , Qi Zheng , Ying Huang , Chris Li , Baoquan He , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , linux-mm@kvack.org, Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SJ Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Chen Ridong , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , Zhao Zhenzhuo , Lian Wang , Joanne Koong , Kunwu Chan , Hui Zhu , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org References: <20261119-mglru-fg-v5-0-9eee2fe97a2d@tencent.com> <20261119-mglru-fg-v5-4-9eee2fe97a2d@tencent.com> Content-Language: en-US From: Muchun Song In-Reply-To: <20261119-mglru-fg-v5-4-9eee2fe97a2d@tencent.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2026/11/19 05:03, Kairui Song via B4 Relay wrote: > From: Kairui Song > > Add a helper that resolves a live lruvec for a folio under RCU > without taking the lruvec lock. It takes a folio directly so the > lruvec lookup happens inside the RCU read-side critical section, > which a lruvec-based interface cannot guarantee. The first user is > the lockless folio_inc_lru_refs() path later in the series. > > Factor the ancestor walk out of lruvec_live_lock_irq() into > __lruvec_get_live(), so that both entry points share it, and spell > out why the walk is enough while only the RCU read lock is held: > reparenting of a memcg that starts dying waits for a grace period, > so it cannot race with the caller until the lock is released. > > Move memcg_is_dying() up in memcontrol.h, next to the other memcg state > predicates, and use it for the walk rather than repeating the open coded > css_is_dying() test a few definitions away from where it is explained. > > No functional change for existing callers. > > Tested-by: Zhao Zhenzhuo > Signed-off-by: Kairui Song Reviewed-by: Muchun Song (ByteDance) Some nits bellow: > --- > include/linux/memcontrol.h | 71 ++++++++++++++++++++++++++++++++++++---------- > 1 file changed, 56 insertions(+), 15 deletions(-) > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index 980358bc6f49..441a5d57e723 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -571,6 +571,11 @@ static inline bool mem_cgroup_disabled(void) > return !cgroup_subsys_enabled(memory_cgrp_subsys); > } > > +static inline bool memcg_is_dying(struct mem_cgroup *memcg) Let's make it const for @memcg along the way. > +{ > + return memcg ? css_is_dying(&memcg->css) : false; > +} > + > static inline void mem_cgroup_protection(const struct mem_cgroup *root, > const struct mem_cgroup *memcg, > unsigned long *min, > @@ -1349,6 +1354,11 @@ static inline bool mem_cgroup_online(struct mem_cgroup *memcg) > return true; > } > > +static inline bool memcg_is_dying(struct mem_cgroup *memcg) > +{ > + return false; > +} > + > static inline > unsigned long mem_cgroup_get_zone_lru_size(const struct lruvec *lruvec, > enum lru_list lru, int zone_idx) > @@ -1510,28 +1520,41 @@ static inline void lruvec_lock_irq(struct lruvec *lruvec) > spin_lock_irq(&lruvec->lru_lock); > } > > -static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec) > +/* > + * Reparenting moves a dying memcg's LRU state to its parent at any time, > + * so anything added to that LRU afterwards would be lost. This helper > + * walks up to the nearest live ancestor while the caller must hold the > + * RCU read lock: reparenting of a memcg that starts dying waits for a > + * grace period, so it cannot race with the caller until the lock is > + * released. > + */ > +static inline struct lruvec *__lruvec_get_live(struct lruvec *lruvec) Cound @lruvec be const as well? > { > #ifdef CONFIG_MEMCG > struct pglist_data *pgdat = lruvec_pgdat(lruvec); > struct mem_cgroup *memcg = lruvec_memcg(lruvec); > > - rcu_read_lock(); > - > /* > * The memcg can be NULL when the memory controller is disabled. > * Otherwise, the caller keeps the memcg owning @lruvec alive. > */ > - while (unlikely(memcg && css_is_dying(&memcg->css))) { > + while (unlikely(memcg_is_dying(memcg))) { > memcg = parent_mem_cgroup(memcg); > lruvec = mem_cgroup_lruvec(memcg, pgdat); > } > +#endif > + return lruvec; > +} > > +static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec) > +{ > +#ifdef CONFIG_MEMCG > + rcu_read_lock(); > + lruvec = __lruvec_get_live(lruvec); > spin_lock_irq(&lruvec->lru_lock); > #else > lruvec_lock_irq(lruvec); > #endif > - > return lruvec; > } > > @@ -1561,6 +1584,34 @@ static inline bool folio_matches_lruvec(const struct folio *folio, > lruvec_memcg(lruvec) == folio_memcg(folio); > } > > +/** > + * folio_lruvec_live_get - get a live lruvec for a folio under RCU > + * @folio: the folio > + * > + * Same as folio_lruvec(), except that a folio whose memcg is dying is > + * accounted to the nearest live ancestor, which is where reparenting > + * would put it anyway. > + * > + * Return: the live lruvec, with rcu_read_lock() held. Pair it with > + * folio_lruvec_live_put(). > + */ > +static inline struct lruvec *folio_lruvec_live_get(struct folio *folio) const folio? > +{ > +#ifdef CONFIG_MEMCG > + rcu_read_lock(); > + return __lruvec_get_live(folio_lruvec(folio)); > +#else > + return folio_lruvec(folio); > +#endif > +} > + > +static inline void folio_lruvec_live_put(struct lruvec *lruvec) Ditto. Thanks, Muchun > +{ > +#ifdef CONFIG_MEMCG > + rcu_read_unlock(); > +#endif > +} > + > /* Don't lock again iff page's lruvec locked */ > static inline struct lruvec *folio_lruvec_relock_irq(const struct folio *folio, > struct lruvec *locked_lruvec) > @@ -1826,11 +1877,6 @@ void mem_cgroup_node_filter_allowed(struct mem_cgroup *memcg, nodemask_t *mask); > > void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg); > > -static inline bool memcg_is_dying(struct mem_cgroup *memcg) > -{ > - return memcg ? css_is_dying(&memcg->css) : false; > -} > - > #else > static inline bool mem_cgroup_kmem_disabled(void) > { > @@ -1901,11 +1947,6 @@ static inline void mem_cgroup_node_filter_allowed(struct mem_cgroup *memcg, > static inline void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg) > { > } > - > -static inline bool memcg_is_dying(struct mem_cgroup *memcg) > -{ > - return false; > -} > #endif /* CONFIG_MEMCG */ > > #if defined(CONFIG_MEMCG) && defined(CONFIG_LRU_GEN) >