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=1796176291; b=MP1kMCAVUsWhUtmlC1vsIyFnvmxhMhC/RREfV/psj2O4TfZI08Uc/A+2GZyGi2wmSbCu s59pwR26zdgk+0PsTWiu36X37AlZAuP1HCWHhu7Uwy4oFDbAXldWVeWE+SgsRixvnaG3G 4UD666U7wE0pE8lArn967H3p+NWEltK6U4ZUmp8HKhbaEwEJU9Z3mx7O2OqD0Hk+p++rK 1+fno67sfSgDZeA68Fz6E0GS0lksq/4ldZNARpCrLmKT1KNK/uBx4dpuUZB+xKTgTDlsN NrV8JuN89sbCiGSUA507Me69AxTYXpDdeVwzB2hG7xthiukH/TERUcDfjePJe6zdGBw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796176291; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=xhLA1XTiptHDsZlFnVit6Fw64mm0WV7QdjQRVLWysTQ=; b=ZRnipCDj+YUWWhfBmmGe5CaZvW3FUhsW9NSHNw4nIqNWskOjfGJ4oyLmvb/ao7rfS6a8 UUIy9huEte2DEGwRV15aEKpZYXtIzkP3pWQdy6UmydkkH3HQpvRV55yfvCjPj55UwVHm9 WP93u+jSOLRv+JM2RtLV1q+0BDpbA+0oDRlc9WlpCIHtEBJMG4iKbuwMZx2M852FCAqDn zfi/89Yuw5od5A0GjE/lf5BkvTFoyJptMMRN3aJzoUTF+RIg1kXoC17uWxKIOb5lE0WYJ Id3HbgK7w8OZVUzLE9iZX5K2NIsrK2EiAcaHq1V9Y1Aee+NTROk4ntooeX3eU7K0pOA== 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=0PeOE6C0; 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 B7986606A31A for ; Wed, 2 Dec 2026 01:51:31 +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=0PeOE6C0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7FDC21F00893; Wed, 2 Dec 2026 01:51:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796176291; bh=xhLA1XTiptHDsZlFnVit6Fw64mm0WV7QdjQRVLWysTQ=; h=Date:To:From:In-Reply-To:References:Subject; b=0PeOE6C0tedqjuu96XtLl1eUY88UVagAztDKwfy1fiCWheRxo/+vImFfij7ijQl9b VCY9IuXyB5ZpwTCjqGEaCxidP83hL+j2hywdpVUwOzjCWFV/vqPuoqAbKT1XsGG8sk gn09PP9UIhNOlq39MrHIvKTDP+cCJBhmBrI9pD44= Date: Tue, 01 Dec 2026 17:51:31 -0800 To: mm-commits@vger.kernel.org,ziy@nvidia.com,zhaozhengzhuo@uniontech.com, yuzhao@google.com,yuanchu@google.com,youngjun.park@lge.com, ying.huang@linux.alibaba.com,willy@infradead.org,weixugc@google.com, wangzicheng@honor.com,vernon2gm@gmail.com,vbabka@kernel.org, usama.arif@linux.dev,tz2294@columbia.edu,surenb@google.com,sj@kernel.org, shakeel.butt@linux.dev,ryncsn@gmail.com,ryan.roberts@arm.com, roman.gushchin@linux.dev,rientjes@google.com,ridong.chen@linux.dev, qi.zheng@linux.dev,nphamcs@gmail.com,nico.pache@linux.dev, muchun.song@linux.dev,mhocko@kernel.org,maxmuzaf@gmail.com,ljs@kernel.org, liuxiang.277@bytedance.com,lianux.mm@gmail.com,liam@infradead.org, lance.yang@linux.dev,kunwu.chan@gmail.com,joannelkoong@gmail.com, hui.zhu@linux.dev,hughd@google.com,hi@tychen.cc,hannes@cmpxchg.org, gourry@gourry.net,ehab.ababneh@intel.com,dev.jain@arm.com,david@kernel.org, chrisl@kernel.org,baoquan.he@linux.dev,baolin.wang@linux.alibaba.com, baohua@kernel.org,axelrasmussen@google.com,akpm@linux-foundation.org, kasong@tencent.com From: Andrew Morton In-Reply-To: <20261202-mglru-fg-v7-5-5965a96d86d0@tencent.com> References: <20261202-mglru-fg-v7-5-5965a96d86d0@tencent.com> Subject: + mm-memcg-add-folio-based-lruvec-live-helper.patch added to mm-new branch Message-Id: <20261202015131.7FDC21F00893@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-new 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-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next 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: Wed, 02 Dec 2026 04:18:45 +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/20261202-mglru-fg-v7-5-5965a96d86d0@tencent.com Signed-off-by: Kairui Song Signed-off-by: Andrew Morton Tested-by: Zhao Zhenzhuo 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 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-mglru-make-generation-page-counters-atomic.patch mm-memcg-add-folio-based-lruvec-live-helper.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