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=1795038423; b=u/1s3SN/1zuAUg/d2WSEEm2Sbgr/JpTStab2iUXHtF9UzN1gcU+T0g6Hc6RzFgj7itcn niMhwhFk4lJ2iiCxN5pk0vKPjzVtqXT/RRDAyhceEhsnp5EcC3vG4HrXj7VXaR14xtdPy VlcTAP+e3UHdZ577LHA2HCdpYVoRVJW4tDYdO25e0ItcaVbj2aqcI5YFESI6biLKCSfIT PLmtA+yywGkbkayx3t5Nw57WxaqjkexSaodYrXFuc4FXpZB0T7gIASl28FAw6WComWAqf O5AUiorI1vZ6P9NqT84ls0hfz1eCmBZn/6yQTsONEQQqgsfz/D9eO2hEJqvzgsOq9Lg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795038423; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=IOavgzBp+36zwCHaVHe2HQ0zfKV3j+6ZYrHRQUfXGfM=; b=xm2Wp/Dk2VSdkabBqoDARDtVulkbJua1fcJD4YWADjdBoVPJD4DVxnCJfNrt8k2lId2c dD2nd21fCrjWThPy6ONXVPZ4/W33j4cJ7Sm9r/hCI5XrPN0wpzbzCRhLucnzA/HP8qH77 3wC/hNonUHSR/BbfkGhgZ4jCOYUCtAo7LP5h8auHsA59mAw4Y0j8GmgpT2xKTKOFjG0Jn C823EzMU+/Hz353/RuREwJ9/IsRbH1xh+aEAEVHM8IejXuAI1e4iCmGCWg2cGDzRUmYLa 9ehqAqoEcV0HvyvR226LdYWyS1Pd93ku9XaGuPeXiCquvJHOkJ4XQrZjtJJPA4EPnxA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=i05NlYEF; 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 E72AC606A325; Wed, 18 Nov 2026 21:47:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=i05NlYEF Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19C811F000C4; Wed, 18 Nov 2026 21:47:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795038422; bh=IOavgzBp+36zwCHaVHe2HQ0zfKV3j+6ZYrHRQUfXGfM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=i05NlYEFJoQLTPsQqbcTNUxyZkUQHODjr9Byi2iOcjSWz7+sScKVoxcorBvWkV2j3 3OeAtbOUTgyomzMQdNbU6t3XLKK9o7e85L1T4tG8iT1FCaugcEN2GlQP0mZJRJ3xdD n4w0JiF+vsbkDCULsNXkk0Ojl+yPDFtr9Y5zAc8w= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ridong Chen , Andrew Morton , Barry Song , Johannes Weiner , Axel Rasmussen , Chris Down , David Hildenbrand , Kairui Song , Lorenzo Stoakes , Michal Hocko , Muchun Song , Roman Gushchin , Shakeel Butt , Tejun Heo , Wei Xu , Yuanchu Xie , Yu Zhao , Sasha Levin Subject: [PATCH 6.6 1697/3318] mm/mglru: fix ineffective memory protection for non-kswapd reclaim Date: Wed, 18 Nov 2026 15:38:39 +0100 Message-ID: <20261118150412.743303892@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150313.527275416@linuxfoundation.org> References: <20261118150313.527275416@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ridong Chen [ Upstream commit 15da1e4978dd4ea8f5faa6e15ee967ba4ad9a30a ] For MGLRU, memory.min/low is not honored during global proactive reclaim (writing to the root memory.reclaim) and global direct reclaim, because these paths shrink memcgs using stale protection (emin/elow). It can be reproduced as follows: # echo 7 > /sys/kernel/mm/lru_gen/enabled # cd /sys/fs/cgroup # mkdir -p a/b # echo 100M > a/memory.min # echo +memory > a/cgroup.subtree_control # echo 100M > a/b/memory.min # echo $$ > a/b/cgroup.procs # dd if=/dev/zero of=/tmp/testfile bs=1M count=200 # cat a/b/memory.current 222650368 # echo 500M > memory.reclaim -bash: echo: write error: Resource temporarily unavailable # cat a/b/memory.current 6070272 memory.min is 100M, yet reclaim drops a/b down to 6M, breaking the protection. The traditional LRU path is not affected because shrink_node() calls mem_cgroup_calculate_protection() for each memcg it visits during a top-down tree walk. Commit 30d77b7eef01 ("mm/mglru: fix ineffective protection calculation") moved the protection computation into lru_gen_age_node(), which only runs for kswapd. Non-kswapd global reclaim reaches shrink_one() through lru_gen_shrink_node() -> shrink_many() without any protection computation, so emin/elow are whatever a previous kswapd run left behind - or zero if kswapd never ran on this node. Relying on a prior kswapd pass is not correct either: a memcg's emin/elow are derived from its ancestors' memory.min/low settings and from children_min_usage, both of which change over time, so emin/elow go stale even after kswapd has run and must be recomputed at the point of reclaim. Introduce mem_cgroup_calculate_protection_path() which computes emin/elow along the root-to-target path only, by iterating through the cgroup ancestors array top-down. This avoids the full tree traversal that would be needed with mem_cgroup_calculate_protection(), limiting the cost to O(depth) per memcg - typically 3-5 levels. Call it from shrink_one() for the non-kswapd path so that each memcg about to be shrunk has correct protection values. Link: https://lore.kernel.org/20260907025445.1836238-3-ridong.chen@linux.dev Fixes: e4dde56cd208 ("mm: multi-gen LRU: per-node lru_gen_folio lists") Signed-off-by: Ridong Chen Signed-off-by: Andrew Morton Reviewed-by: Barry Song Reviewed-by: Johannes Weiner Assisted-by: Claude:claude-opus-4-8 Cc: Axel Rasmussen Cc: Chris Down Cc: David Hildenbrand Cc: Kairui Song Cc: Lorenzo Stoakes Cc: Michal Hocko Cc: Muchun Song Cc: Roman Gushchin Cc: Shakeel Butt Cc: Tejun Heo Cc: Wei Xu Cc: Yuanchu Xie Cc: Yu Zhao Cc: [ sashal: adapted protection recalculation to use direct RCU controller lookup and the existing mem_cgroup_calculate_protection() helper ] Signed-off-by: Sasha Levin --- include/linux/memcontrol.h | 10 ++++++++++ mm/memcontrol.c | 41 ++++++++++++++++++++++++++++++++++++++ mm/vmscan.c | 8 +++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 8aee8b75aad01..687179e5ea4d0 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1871,6 +1871,16 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, #endif /* CONFIG_MEMCG_KMEM */ +#if defined(CONFIG_MEMCG) && defined(CONFIG_LRU_GEN) +void mem_cgroup_calculate_protection_path(struct mem_cgroup *root, + struct mem_cgroup *memcg); +#else +static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root, + struct mem_cgroup *memcg) +{ +} +#endif + #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) bool obj_cgroup_may_zswap(struct obj_cgroup *objcg); void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 852fa93efe1be..e6fffb91742a8 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7117,6 +7117,47 @@ void mem_cgroup_calculate_protection(struct mem_cgroup *root, atomic_long_read(&parent->memory.children_low_usage))); } +#ifdef CONFIG_LRU_GEN +/** + * mem_cgroup_calculate_protection_path - compute protection along a path + * @root: the top ancestor of the sub-tree being checked (NULL for root_mem_cgroup) + * @memcg: the target memory cgroup + * + * Walk the ancestor path from @root down to @memcg and compute the effective + * protection at each level. This is safe for isolated queries because it + * ensures parents are computed before children. + */ +void mem_cgroup_calculate_protection_path(struct mem_cgroup *root, + struct mem_cgroup *memcg) +{ + struct cgroup *cg; + int root_level, i; + + if (mem_cgroup_disabled()) + return; + + if (!root) + root = root_mem_cgroup; + + if (memcg == root) + return; + + root_level = root->css.cgroup->level; + cg = memcg->css.cgroup; + + rcu_read_lock(); + for (i = root_level + 1; i <= cg->level; i++) { + struct mem_cgroup *cur; + + cur = mem_cgroup_from_css(rcu_dereference( + cg->ancestors[i]->subsys[memory_cgrp_id])); + if (cur) + mem_cgroup_calculate_protection(root, cur); + } + rcu_read_unlock(); +} +#endif /* CONFIG_LRU_GEN */ + static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg, gfp_t gfp) { diff --git a/mm/vmscan.c b/mm/vmscan.c index cbc2de77db3ed..d099623bd3ceb 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -5459,7 +5459,13 @@ static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) struct mem_cgroup *memcg = lruvec_memcg(lruvec); struct pglist_data *pgdat = lruvec_pgdat(lruvec); - /* lru_gen_age_node() called mem_cgroup_calculate_protection() */ + /* + * For kswapd, mem_cgroup_calculate_protection() has already + * been called during the top-down cgroup traversal. + */ + if (!current_is_kswapd()) + mem_cgroup_calculate_protection_path(NULL, memcg); + if (mem_cgroup_below_min(NULL, memcg)) return MEMCG_LRU_YOUNG; -- 2.53.0