From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (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 63D773B1B3 for ; Mon, 17 Aug 2026 04:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786941051; cv=none; b=aPdEDNNtR3+LjWxJmE7lCumy+ch3NEhUZpjxf/qLATaxtYBuFiTnVxSV465ujUKax6eNXRKwe89AJ2sVYog+Xs5/PYVp6cp8F/0r9hLUfH2VkP0Kxi/HKaaJsZR1EDkMq23bf6HWH/010PjAKNDFy1NbRHAYznGEml9JTwnNfVY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786941051; c=relaxed/simple; bh=dfWYSuiUAiueZVM08DAYOsI7XXNJhmiSC34xDHS08nk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QR2GYTeiGewSCFno7MsBPEeBB1giDPn2vJyYuRFXAdVr4ghF1HHJs1S/D5gAY6TREtY6e+qoOdG4jjEcCyXQsG6SIKG5qwKuxr5qJyHp5JzWgZspRvF4Ool28hxu5z7WMfJAOZTdERsjwzBy0vleH+aUrnbokJ6th7mqYaXpuz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lOQAc8Ra; arc=none smtp.client-ip=209.85.160.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lOQAc8Ra" Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-44856d185bcso1839288fac.3 for ; Sun, 16 Aug 2026 21:30:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786941049; x=1787545849; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XzHa46yoeqtsHFObqgwtZinJ/lnkplRZ68OVnoGli10=; b=lOQAc8Ra8L2UrnGY+OawyjFF4LOZ3JqTKUt9dG+vNf4IFkSaL3lLNPXLXYWwWf0WUJ vb+V7YNsC+yX7wDicNW/DCS80vEzNodzR2qCuRLsQ2DVFUpRqkc6RWE8KiAgzk+k1wnM 9gBxpbAMUp9nlmhKCWDIMitE6XG89+FErqBmizGOVCT9EFoFyKzwfbHaxDXB663RmCe3 /ll09gL9WvppHYyTGrcgyGpWoSDM0Az17EI60S3v7A+5Onr7lZb2lqCt5YTNVJOOmErV SJisTmjckd4pxF0YAz63TKvhSDLmrKSeQ51pTjHPXTch8tzjRGtZssZPAUGIOj27w9sc NCzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786941049; x=1787545849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=XzHa46yoeqtsHFObqgwtZinJ/lnkplRZ68OVnoGli10=; b=LqUzwlDNv1R4bRlXIausG61DXZ8kFMhPTWqJltzJe0DVGtHJ9kw2JifMrJ2FciodyT +SWXzEBaEKiA+NBot5zXVRntYfsY6pqhPszbNBrJ8H8RL3U7f9kOeP2N3WyLwnI30M0p EyqCGMY95k+1+Hwvp3tk3xgCTwnSDjnnie+1hCYMkaEhgvDHGs1Cmujb6EB6MxZSv2r4 lW+skXDMeSIWhemJGh7wkrXrVBPhPVkWiSGyBkgFprtPLLSL2umS9SkcmdSrlmae3/c1 KtqWMY0/2y5/2qZeNyoltmO0eW5yEMOnux2Mk9Ster+F8Rd6N9h+zt+WAK9FIiiS2SqA pVEA== X-Forwarded-Encrypted: i=1; AHgh+Rr7jf9K34QhBSFSXfkuyf39Rr2HYZBCgCOTZxl1oWp2k03hGoL6CKBLjBd+LCBchC9JAGFm/gdmRdiJkq0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw2IgxEvw9ygfzWf940YesVQkgRxQxttiuSjLCOmNfkXYvHbrs2 quxdXsm3Ai9inLQ8HHKmCK9eRzZEMyL7i9lQw9KvehhXEErxfIhcgsnF X-Gm-Gg: AR+sD11Ra+yND8GCHw+U84hWnVzPGHWt1p5b8JdDXuxbGUGCsu1PjBf2RzxfAXpo9Q4 jt8onLXpQvyLuwtkd0LeLtdw8tSSNPj4WBhdy3AAWtzzUL83ppcIadIsx90kWLjKIJXoKDSz2nw Vjbr713bbxnDHmBvlHG8eMXePwfyX8AjHrBxPuCiBz4HYscg6+IHvuz9ve9WaUkPYGQlnvUNUo2 MReobqlonwAqnopWcdCQXSOp2znSZuQnxyxb0em7xtMxkP3b7e9dmc7FCU42h61gtMPpadEw5v3 umE7Jfj4A3PylYOzknrLY2GijOx37Ooml01kIsTsS5KfMoFPkCUUQyFc7H/JDlMNLmGiVpxarv4 OdR4y0KtjzJdgxnZ2RDk7auanLAqoGwT5YyCeiXLCfO+rCJfvwAFFPfj3dwe2pfuLWn42e7cquf 5PZ7Y44WcIJ+0koB6D5ltWWyYxSTOfeUmBoxfBkpVek1X1UIgxX8pxSL2/YFvdqMnFTDJARuT07 61PGknTq9gLQ1E9wi0Y8ynS3gJvG9+j6Q== X-Received: by 2002:a05:6870:a085:b0:442:175:cea9 with SMTP id 586e51a60fabf-45e91df60e8mr22649897fac.7.1786941049279; Sun, 16 Aug 2026 21:30:49 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:7::]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-45f22a7f3dbsm274098fac.1.2026.08.16.21.30.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Aug 2026 21:30:48 -0700 (PDT) From: Joshua Hahn To: Song Hu Cc: akpm@linux-foundation.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, zhuhui@kylinos.cn, joshua.hahnjy@gmail.com, audra@redhat.com, bingfangguo@tencent.com Subject: Re: [PATCH] mm: memcg: flush empty per-cpu stock slots on memcg offlining Date: Sun, 16 Aug 2026 21:30:45 -0700 Message-ID: <20260817043047.1553572-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260817025917.66233-1-husong@kylinos.cn> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Mon, 17 Aug 2026 10:59:17 +0800 Song Hu wrote: > refill_stock() takes a css reference for each cached memcg slot and > mem_cgroup_css_offline() relies on drain_all_stock() to release those > references before the cgroup can finish dying. > > consume_stock() can drive a slot's nr_pages to zero while its cached[] > pointer stays set. is_memcg_drain_needed() skips such empty slots, so > the offlining drain is a no-op for them: the pinned css reference is > released only if the slot happens to be evicted by later unrelated > charges on the same CPU, or by CPU hotplug. The memcg then lingers > in the dying state indefinitely. On a CPU with container churn, a > per-cpu stock can pin up to NR_MEMCG_STOCK (7) zombie memcgs. > > The sibling obj_stock_flush_required() has no such gate: a cached > objcgs slot is flushed regardless of its byte count. Drop the > nr_pages gate from is_memcg_drain_needed() accordingly. Draining an > empty slot is a single css_put() and a NULL store, and it happens at > most once per slot per drain, so the extra work is negligible. Hi Song, I feel that this above statement is quite misleading. Scheduling a drain on a memcg that doesn't have stock eventually performs a drain operation on every memcg cached in that CPU. drain_local_memcg_stock drain_stock_fully { for (i = 0; i < NR_MEMCG_STOCK; ++i) drain_stock(stock, i); { Sure, for any empty stock during this iteration drain_stock is trivial, but for nonempty stock that happen to co-exist on the CPU, it's forcing a cache miss on the next charge. The problem that you note does seem quite real though. I think a less invasive solution could be something like if (mem_cgroup_is_descendant(memcg, root_memct) && (READ_ONCE(stock->nr_pages[i]) || css_is_dying(&memcg->css)) By the way, I think the argument that "obj_stock_flush_required has no such gate so is_memcg_drain_needed doesn't need one too" is also not really correct since a dying objcg doesn't flush anyways. I hope you have a great day, Joshua > This is easily reproduced with short-lived cgroups pinned to one CPU: > 12 charge/exit/rmdir cycles leave nr_dying_subsys_memory at +2, stable > across later slot displacement. > > Fixes: d1a05b6973c7 ("memcg: do not try to drain per-cpu caches without pages") > Signed-off-by: Song Hu > --- > mm/memcontrol.c | 8 ++++++-- > 1 file changed, 6 insertions(+), 2 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 17da1f43b7d3..b931ec16bb82 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -2291,8 +2291,12 @@ static bool is_memcg_drain_needed(struct memcg_stock_pcp *stock, > if (!memcg) > continue; > > - if (READ_ONCE(stock->nr_pages[i]) && > - mem_cgroup_is_descendant(memcg, root_memcg)) { > + /* > + * An empty slot still pins a css reference which > + * mem_cgroup_css_offline() relies on drain_all_stock() > + * to release. > + */ > + if (mem_cgroup_is_descendant(memcg, root_memcg)) { > flush = true; > break; > } > -- > 2.43.0