From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796321950; b=ge4SnMvPOXa3fYMCrBLhrWU517ZotlC/cgRusCjO4ckbpmnyz/xpVfgLVdNGsJb4EbSB MR9hHJxJ69kWCWNAlkFheES9vbiYA5Z8/gHR1V+6HMTWkZTxrujt7Q0u8FYSWnAiZITxz hMfI2mQrs46+euT58AyV4fIMwrpY6vteTeJ8zCZFNED7M141pExYRcBRYHQYcBaF9uOG+ nLPPoOwoDEYXR5DESTC8PJDh1JlDalnk2BuQAr5Cz5N635hqieyJTTBmmwDVUPTdf4iMP MVY1v1flfTPZurkR/Kc1udeQSSCgivjUNmiigphcTneVZ315Qvb12vQyZW8IlDZSEzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796321950; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=T1VFhJcszLqnyAPwIRxBRlwzlupS6SHsaFU4faB0oAw=; b=TOTdOtnvEDcI/LuUvGv/q4/RyxFFzobODKSSu6AGTijkei0SQv9TLJNT2lZuMoOBK2ce /Puvlit4nkhfEHOV3oomhB6iFVRqvn0+d1pYIhUi+F5eUEFV0nywRYiOearGUvhGb0iNE Qoh5FPzd+Ca4j1GR9d6IhAGM7uILNiBFt9kvrS0bfVGZcRwWErQTf5nHnb6TUBt/gCJaj /t6RaymlnBGeHeH2DvFJGa89gG6daVZY8MvdEWKSyLsJCQjoeU5OC44rt1ATafLryiEpG xHPCyv1s+09BLMkL8uUsLV2gzj9r+oAlNz4arCSTdIX1hLPmmPslu+FwhkKYEWHOLKQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=nMkAazjo; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CCE8606A32D for ; Thu, 3 Dec 2026 18:19:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=nMkAazjo Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC4AD1F000C1; Thu, 3 Dec 2026 18:19:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796321950; bh=T1VFhJcszLqnyAPwIRxBRlwzlupS6SHsaFU4faB0oAw=; h=Date:To:From:In-Reply-To:References:Subject; b=nMkAazjooeudifzduTiRNk/69F6YdSduhoHFoizwXFmuNpLT1KF5/Fl8f6drbrP4m /kVG2Nmx5fxkR8TBnPcWL2YJm6csLuBNGn1xbak8U9ynFFE73x/sASYJeMqejyRBko Sx+i6t1DWh+zHk9P4phIAPz71etAggmZoAEiuzaU= Date: Thu, 03 Dec 2026 10:19:09 -0800 To: mm-commits@vger.kernel.org,kasong@tencent.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261204-mglru-fg-v8-3-64f81c876c15@tencent.com> References: <20261204-mglru-fg-v8-3-64f81c876c15@tencent.com> Subject: + mm-memcg-add-folio-based-lruvec-live-helper.patch added to mm-unstable branch Message-Id: <20261203181909.DC4AD1F000C1@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/memcg: add folio-based lruvec live helper has been added to the -mm mm-unstable branch. Its filename is mm-memcg-add-folio-based-lruvec-live-helper.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-memcg-add-folio-based-lruvec-live-helper.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Kairui Song Subject: mm/memcg: add folio-based lruvec live helper Date: Fri, 04 Dec 2026 01:02:34 +0800 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. Link: https://patch.msgid.link/20261204-mglru-fg-v8-3-64f81c876c15@tencent.com Signed-off-by: Kairui Song Signed-off-by: Andrew Morton Tested-by: Zhao Zhengzhuo Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Reviewed-by: Qi Zheng Reviewed-by: Muchun Song (ByteDance) Reviewed-by: Ridong Chen Cc: Johannes Weiner Cc: Ying Huang Cc: Christopher Li Cc: Baoquan He Cc: Nico Pache Cc: Usama Arif Cc: Michal Hocko Cc: Roman Gushchin Cc: Shakeel Butt Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: Liam R. Howlett Cc: Barry Song Cc: Axel Rasmussen Cc: Yuanchu Xie Cc: Wei Xu Cc: Vlastimil Babka Cc: Suren Baghdasaryan Cc: Nhat Pham Cc: Youngjun Park Cc: Zi Yan Cc: Gregory Price Cc: Matthew Wilcox (Oracle) Cc: Baolin Wang Cc: Ryan Roberts Cc: Dev Jain Cc: Lance Yang Cc: Hugh Dickins Cc: SJ Park Cc: David Rientjes Cc: Yu Zhao Cc: Vernon Yang Cc: wangzicheng Cc: Tal Zussman Cc: Kairui Song Cc: Xiang Liu Cc: Ehab Ababneh Cc: wang lian Cc: Joanne Koong Cc: Hui Zhu Cc: Maxim Muzafarov Cc: Tianyi Chen Cc: Aveline Noir include/linux/memcontrol.h | 71 +++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 15 deletions(-) --- a/include/linux/memcontrol.h~mm-memcg-add-folio-based-lruvec-live-helper +++ a/include/linux/memcontrol.h @@ -573,6 +573,11 @@ static inline bool mem_cgroup_disabled(v return !cgroup_subsys_enabled(memory_cgrp_subsys); } +static inline bool memcg_is_dying(struct mem_cgroup *memcg) +{ + 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, @@ -1352,6 +1357,11 @@ static inline bool mem_cgroup_online(str 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) @@ -1517,28 +1527,41 @@ static inline void lruvec_lock_irq(struc 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) { #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; } @@ -1568,6 +1591,34 @@ static inline bool folio_matches_lruvec( 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(const struct folio *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(const struct lruvec *lruvec) +{ +#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) @@ -1833,11 +1884,6 @@ void mem_cgroup_node_filter_allowed(stru 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) { @@ -1908,11 +1954,6 @@ static inline void mem_cgroup_node_filte 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) _ Patches currently in -mm which might be from kasong@tencent.com are mm-mglru-report-evictable-sizes-per-type.patch mm-mglru-extract-lruvec_populated_min_seq-helper.patch mm-mglru-reclaim-from-the-oldest-populated-gen.patch mm-mglru-lift-the-forced-protection-of-reclaimer.patch mm-mglru-skip-zones-above-reclaim_idx-instead-of-rotating-them.patch mm-mglru-return-for-unavailable-anon-reclaim-earlier.patch mm-mglru-balance-anon-and-file-scan-budget-by-refault-rate.patch mm-mglru-lift-youngest-gen-protection-for-higher-priority.patch mm-memcontrol-allow-update-of-lru-statistic-without-holding-lru-lock.patch mm-memcontrol-avoid-potential-counter-leak-when-reparenting-local-stats.patch mm-memcg-add-folio-based-lruvec-live-helper.patch mm-mglru-make-generation-page-counters-atomic.patch mm-mglru-frequency-guided-workingset-promotion-mglru-fg.patch mm-mglru-make-folio-lru-referenced-times-count-a-generic-api.patch mm-mglru-move-add-del-lru-size-accounting-out-of-lru_gen_update_size.patch mm-mglru-gup-mark-folios-referenced-via-a-fast-helper.patch mm-mglru-smaps-convert-to-lru-refs-based-operations-for-mglru.patch mm-damon-convert-to-lru-refs-based-operations-for-mglru.patch mm-khugepaged-convert-to-lru-refs-based-operations-for-mglru.patch mm-madvise-adapt-for-lru-refs-based-operations-for-mglru.patch mm-factor-out-folio_mark_accessed_vma.patch mm-huge_memory-mark-folio-as-accessed-more-accurately-on-split.patch mm-mglru-make-pg_workingset-helpers-work-based-on-folio-lru-refs.patch mm-mglru-folio-lru-refs-based-active-inactive-number-accounting.patch mm-mglru-make-folio_inc_lru_refs-lruvec-lockless.patch