From nobody Sat Sep 26 05:28:06 2026 Received: from mta1.migadu.com (out-246.mta1.migadu.com [95.215.58.246]) (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 8053D446BE8 for ; Fri, 4 Sep 2026 09:46:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.246 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788515184; cv=none; b=kgeCr9unBeg2iX+YvC2iPBvJj/TzwGjs3VlxJoqX5fl3I60Th3M3eRSofYj4vWXqCyW4xv/Nu60yqM8ERXqVitwD0zsmWAlqqy35xy6K+FmdP6/NglUsoVQBpm2XfPBanKsNbN5hWed3cxHfEAkxt7EA7HRSeJ8r8a2k+DeD3OI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788515184; c=relaxed/simple; bh=l6I893eMOABjOB0M92Iaqwew/NDGht/r0iDtXoMxBjA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oFcshaM4RyMsXZxRTDTPYMfsxPOMNIMJNdFZK9c5zUVbRzudHWq7kioEo16W8+uBSaeRfarR8fp+eTv7rjyPu7ZaB84u9G/Nr/5bXNPZdESIk9eSxaW0jL5jt0wfiB1S6YDgQyPJLU7cLAtwl8n7dv6v2S6ncGhK9qc+GKFU1dQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=F8CdhhJG; arc=none smtp.client-ip=95.215.58.246 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) header.d=linux.dev header.i=@linux.dev header.b="F8CdhhJG" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=l6I893eMOABjOB0M92Iaqwew/NDGht/r0iDtXoMxBjA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788515180; v=1; x=1789119980; b=F8CdhhJGU+uXXlZUT/P0KokyYv9tS8ia0VRNcfI06OJvwdLPq+Z6cjqqJX0gJkQuFUXhEHnI cTZwdY0Lp5TA7roERAgKKIbOnqzUcbTuXCY6yMOzmoSNILyhyL/8pDgq3jbDFInh7uN/V2ovLbN ThTDbSL5aFfQWKnXVvR9PlfU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 66135b2da841d307; Fri, 04 Sep 2026 09:46:19 +0000 X-Mizu-Trace-ID: 66135b2da841d307 X-Migadu-Flow: FLOW_OUT From: "Hui Zhu" To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Qi Zheng , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hui Zhu , stable@vger.kernel.org Subject: [PATCH v3 1/3] mm: memcg: redirect stats updates of dying memcgs for all hierarchies Date: Fri, 4 Sep 2026 17:45:54 +0800 Message-ID: <8a3fe5e6a076cdd9ac997125cb6c6a0948e1a6b6.1788514750.git.zhuhui@kylinos.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hui Zhu get_non_dying_memcg_start() redirects the stat updates of a dying memcg to its closest non-dying ancestor, but only on cgroup v1; on cgroup v2 the stats keep being accounted to the dying memcg itself. A later patch in this series restores lruvec_page_state_local() in count_shadow_nodes() to fix the broken workingset shadow node budget under MGLRU. count_shadow_nodes() is the only reader of those non-hierarchical state_locals on cgroup v2: when a memcg is offlined, its pages are reparented to the ancestor but their stat updates keep being accounted to the dying memcg, so count_shadow_nodes() computes a wrong shadow node budget and workingset thrashing protection is lost. This is user visible as premature reclaim of hot page cache and degraded performance under memory pressure. Apply the redirection to all hierarchies to fix this. Offlining is rare, so the added cost on the stat update fast path is limited to an rcu_read_lock() and a css_is_dying() check; the upward walk happens only while a memcg is dying. Fixes: 7404bd37cfbe ("mm: workingset: use lruvec_lru_size() to get the numb= er of lru pages") Cc: stable@vger.kernel.org Signed-off-by: Hui Zhu Acked-by: Shakeel Butt --- mm/memcontrol.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 8319ad8c5c23..b3d1ac3fe0aa 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -805,20 +805,14 @@ static long memcg_state_val_in_pages(int idx, long va= l) return val < 0 ? -res : res; } =20 -#ifdef CONFIG_MEMCG_V1 /* - * Used in mod_memcg_state() and mod_memcg_lruvec_state() to avoid race wi= th - * reparenting of non-hierarchical state_locals. + * Used in mod_memcg_state() and mod_memcg_lruvec_state() to avoid race + * with reparenting of non-hierarchical state_locals. Offlining a + * memcg is rare, so do the redirection for all cgroup hierarchies. */ -static inline struct mem_cgroup *get_non_dying_memcg_start(struct mem_cgro= up *memcg, - bool *rcu_locked) +static inline struct mem_cgroup * +get_non_dying_memcg_start(struct mem_cgroup *memcg, bool *rcu_locked) { - /* Rebinding can cause this value to be changed at runtime */ - if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) { - *rcu_locked =3D false; - return memcg; - } - rcu_read_lock(); *rcu_locked =3D true; =20 @@ -830,22 +824,8 @@ static inline struct mem_cgroup *get_non_dying_memcg_s= tart(struct mem_cgroup *me =20 static inline void get_non_dying_memcg_end(bool rcu_locked) { - if (!rcu_locked) - return; - rcu_read_unlock(); } -#else -static inline struct mem_cgroup *get_non_dying_memcg_start(struct mem_cgro= up *memcg, - bool *rcu_locked) -{ - return memcg; -} - -static inline void get_non_dying_memcg_end(bool rcu_locked) -{ -} -#endif =20 static void __mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx, long val) --=20 2.53.0 From nobody Sat Sep 26 05:28:06 2026 Received: from mta0.migadu.com (out-169.mta0.migadu.com [91.218.175.169]) (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 D644F446C18 for ; Fri, 4 Sep 2026 09:46:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788515191; cv=none; b=ghiK1p+0Gu5rhxAfoTvubm3TRLVjG/JcGm3V0x220TKtgWxJz0vCj860rumVhm5c9Wh0rQM5g1CrKrtwrMRa8MY7BCXT2ItQpEYaJ+YZf95mSL6gYoa6SM/CTu4Mb6AFrW0zSEdIGpO2I1fte4p9rCoFOR8XFfFoCRTZN+mmOQg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788515191; c=relaxed/simple; bh=oWXn58rPBwvnPsDKjYWUwvvP2+3EylLcllR8rhtC+xQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n/EAhBLIekSCDnfx/C6SGhy0rwKRrYilPEjreKt4DmVVqOnCcyv7eDx4jia4GR6SSXjwAuTQD4wQqxIp8g10jmY/u08hkJceiuSI8Z7YdWQa1dzIFXdP5OzEV1goTj6Hy43OkRbfbvxAT3ZJDRT5WN4pfCmdEoo0BU1zvFGjjgw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=dDcqYoSi; arc=none smtp.client-ip=91.218.175.169 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) header.d=linux.dev header.i=@linux.dev header.b="dDcqYoSi" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=oWXn58rPBwvnPsDKjYWUwvvP2+3EylLcllR8rhtC+xQ=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788515187; v=1; x=1789119987; b=dDcqYoSi1RteUgPJPCBFyZOuwP5ogGsrAXXNvEAhbKiquyCWaRfgRxD5O1pP4dE0FG2xcNFX jUe8on/tbg+iHuRz+Nl3YS/AtLF8+/s4lgk/k9KhxnvEmM9/az56bC65da64WthuN2CIUCItehY ZWr5GEw/NZtOy5PF1Kd+h2mo= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 9ecf6e52d081f39c; Fri, 04 Sep 2026 09:46:27 +0000 X-Mizu-Trace-ID: 9ecf6e52d081f39c X-Migadu-Flow: FLOW_OUT From: "Hui Zhu" To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Qi Zheng , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hui Zhu , stable@vger.kernel.org Subject: [PATCH v3 2/3] mm: workingset: use lruvec_page_state_local() to count lru pages Date: Fri, 4 Sep 2026 17:45:55 +0800 Message-ID: <2ec3a7f73e85ee5f5739c9dc2e58e9a0e2091925.1788514750.git.zhuhui@kylinos.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hui Zhu Commit 7404bd37cfbe ("mm: workingset: use lruvec_lru_size() to get the number of lru pages") switched count_shadow_nodes() to lruvec_lru_size(). With CONFIG_MEMCG enabled, lruvec_lru_size() reads mz->lru_zone_size, which only the classic LRU paths maintain. MGLRU accounts its pages through __update_lru_size(), which skips that array, so with MGLRU on the four evictable LRU lists are always seen as empty. The shadow node budget (pages >> 3) then collapses to slab plus unevictable pages, and the workingset shadow shrinker reclaims eviction tokens almost as fast as they are created, losing thrashing protection. lruvec_page_state_local() reads lruvec_stats->state_local instead, which both classic LRU and MGLRU maintain. Switch back to it. The reparenting race this re-exposes on cgroup v2 is closed by the follow-up patch that redirects dying-memcg stat updates for all hierarchies. Fixes: 7404bd37cfbe ("mm: workingset: use lruvec_lru_size() to get the numb= er of lru pages") Cc: stable@vger.kernel.org Signed-off-by: Hui Zhu Acked-by: Shakeel Butt --- mm/workingset.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index f351798e723a..85a4e14e95d5 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -693,10 +693,9 @@ static unsigned long count_shadow_nodes(struct shrinke= r *shrinker, =20 mem_cgroup_flush_stats_ratelimited(sc->memcg); lruvec =3D mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); - for (pages =3D 0, i =3D 0; i < NR_LRU_LISTS; i++) - pages +=3D lruvec_lru_size(lruvec, i, MAX_NR_ZONES - 1); - + pages +=3D lruvec_page_state_local(lruvec, + NR_LRU_BASE + i); pages +=3D lruvec_page_state_local( lruvec, NR_SLAB_RECLAIMABLE_B) >> PAGE_SHIFT; pages +=3D lruvec_page_state_local( --=20 2.53.0 From nobody Sat Sep 26 05:28:06 2026 Received: from mta1.migadu.com (out-11.mta1.migadu.com [95.215.58.11]) (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 0E29C446C18 for ; Fri, 4 Sep 2026 09:46:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788515198; cv=none; b=IeTT1sb3dV3QHlA/qMs1w7MPGapMXbrhZkoBb5cTuBAFybpZYMWY2jp0+oah8Mkj5KUDWyB3KST6GAVfoTN177sz5m7jHswyJZ8e9TKIbgS8tJXwxc5y6l42z1xbEODB36XOGlRWYoGeNathqEPCbH7Db4k5Twc9PTIOf0a8RmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788515198; c=relaxed/simple; bh=/egS0JLq3DdP+ELRV2+UfNnFQyripc/bTgQpMQb2RyA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AsCRj1gHZoZ1O2u+44xvoYZG81Vp57UwEwIBu/PXE8F8KxWaVo+DvNFM0ydP69YM8XJKf1z5PWIWBZzSkECRM+rvjoqXfyFZ6kHgRErXXxOgbTJ/9aXvjLDbfkhM3QS0F8L4hwydwEG24NA2dYW/f3eIYokiuWafHXsyZqZ81ok= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=qW3oOuJB; arc=none smtp.client-ip=95.215.58.11 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) header.d=linux.dev header.i=@linux.dev header.b="qW3oOuJB" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=/egS0JLq3DdP+ELRV2+UfNnFQyripc/bTgQpMQb2RyA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788515195; v=1; x=1789119995; b=qW3oOuJBe29Tupcqm9ATJ9aRgS3psFQXuPso1euycC7858JUW3MBvmxVRvaPD+zzUfcrM3rb 00VakCJQPwc+h8kR+79Z/WbkPVEFCi1Epwmau5Nz+ojPkq6GlC//NVhlb3sFC2nLYH79tCgIsA2 H6mQkg8uB6wDBdNuWqEMvqSk= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 8f911b791d4dc657; Fri, 04 Sep 2026 09:46:34 +0000 X-Mizu-Trace-ID: 8f911b791d4dc657 X-Migadu-Flow: FLOW_OUT From: "Hui Zhu" To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Qi Zheng , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hui Zhu Subject: [PATCH v3 3/3] mm: memcg: skip the RCU lock when the memcg is not dying Date: Fri, 4 Sep 2026 17:45:56 +0800 Message-ID: <0db657472c5278b2e2b87fbca7590b47ca520303.1788514750.git.zhuhui@kylinos.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hui Zhu get_non_dying_memcg_start() takes rcu_read_lock() on every stat update, but the lock only protects the upward walk to a non-dying ancestor, which happens solely while a memcg is being offlined. The dying check itself reads the CSS_DYING flag of a memcg the caller already holds a reference to, so it is safe without the lock. Check memcg_is_dying() first and return immediately when the memcg is alive, taking the RCU lock only on the rare dying path. On an anon fault/charge churn workload in a memcg this recovers the ~0.6% overhead added by the previous patch (4368077 vs 4343159 pages/s before, back to ~4377000 pages/s after). Signed-off-by: Hui Zhu Acked-by: Shakeel Butt --- mm/memcontrol.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b3d1ac3fe0aa..f454d02746e9 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -813,6 +813,17 @@ static long memcg_state_val_in_pages(int idx, long val) static inline struct mem_cgroup * get_non_dying_memcg_start(struct mem_cgroup *memcg, bool *rcu_locked) { + /* + * Fast path: the caller holds a reference to @memcg, so reading + * its CSS_DYING flag without the RCU lock is safe. The RCU lock + * is only needed to walk up to a non-dying ancestor, which + * happens only while a memcg is actually being offlined. + */ + if (!memcg_is_dying(memcg)) { + *rcu_locked =3D false; + return memcg; + } + rcu_read_lock(); *rcu_locked =3D true; =20 @@ -824,6 +835,9 @@ get_non_dying_memcg_start(struct mem_cgroup *memcg, boo= l *rcu_locked) =20 static inline void get_non_dying_memcg_end(bool rcu_locked) { + if (!rcu_locked) + return; + rcu_read_unlock(); } =20 --=20 2.53.0