* Re: [PATCH v2] mm: memcg: release the css reference when a stock slot empties
2026-08-18 13:01 [PATCH v2] mm: memcg: release the css reference when a stock slot empties Song Hu
@ 2026-08-18 14:29 ` Michal Hocko
2026-08-18 15:09 ` Shakeel Butt
2026-08-19 4:24 ` Joshua Hahn
2 siblings, 0 replies; 4+ messages in thread
From: Michal Hocko @ 2026-08-18 14:29 UTC (permalink / raw)
To: Song Hu
Cc: akpm, joshua.hahnjy, willy, shakeel.butt, linux-mm, cgroups,
linux-kernel, hannes, roman.gushchin, muchun.song, zhuhui, audra,
bingfangguo
On Tue 18-08-26 21:01:35, Song Hu wrote:
> consume_stock() can drive a stock slot's nr_pages to zero while its
> cached[] pointer stays set, so the slot keeps pinning the css
> reference that refill_stock() took. The offlining drain only
> flushes slots with cached pages, so the reference is never released
> unless the slot happens to be displaced by an unrelated charge or
> by CPU hotplug, and the memcg lingers in the dying state - up to
> NR_MEMCG_STOCK (7) of them per CPU under container churn.
>
> Keeping the slot populated past the last page only saves a
> css_get()/css_put() pair on the next charge of the same memcg, and
> costs more than that: the offlining drain has to know about empty
> slots, and refill_stock() cannot reuse them either, so a charge
> under a different memcg evicts a live batch through the drain_idx
> rotation instead.
>
> Drop the reference in consume_stock() when the slot empties.
> Empty slots stop existing, so is_memcg_drain_needed() and the drain
> path stay as they are, and refill_stock() reuses emptied slots
> directly. The cost is one refcount pair per emptied slot, at most
> once per MEMCG_CHARGE_BATCH pages.
>
> Fixes: d1a05b6973c7 ("memcg: do not try to drain per-cpu caches without pages")
> Signed-off-by: Song Hu <husong@kylinos.cn>
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
>
> Changes since v1: drop the reference at consume time instead of flushing
> empty slots from the offlining drain, as discussed with Michal Hocko and
> Joshua Hahn (full-stock drain cost, refill reuse).
>
> v1: https://lore.kernel.org/all/20260817025917.66233-1-husong@kylinos.cn/
>
> mm/memcontrol.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 17da1f43b7d3..1271d390b617 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2140,7 +2140,12 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
>
> stock_pages = READ_ONCE(stock->nr_pages[i]);
> if (stock_pages >= nr_pages) {
> - WRITE_ONCE(stock->nr_pages[i], stock_pages - nr_pages);
> + stock_pages -= nr_pages;
> + WRITE_ONCE(stock->nr_pages[i], stock_pages);
> + if (!stock_pages) {
> + css_put(&memcg->css);
> + WRITE_ONCE(stock->cached[i], NULL);
> + }
> ret = true;
> }
> break;
> --
> 2.43.0
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] mm: memcg: release the css reference when a stock slot empties
2026-08-18 13:01 [PATCH v2] mm: memcg: release the css reference when a stock slot empties Song Hu
2026-08-18 14:29 ` Michal Hocko
@ 2026-08-18 15:09 ` Shakeel Butt
2026-08-19 4:24 ` Joshua Hahn
2 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-08-18 15:09 UTC (permalink / raw)
To: Song Hu
Cc: akpm, mhocko, mhocko, joshua.hahnjy, willy, linux-mm, cgroups,
linux-kernel, hannes, roman.gushchin, muchun.song, zhuhui, audra,
bingfangguo
On Tue, Aug 18, 2026 at 09:01:35PM +0800, Song Hu wrote:
> consume_stock() can drive a stock slot's nr_pages to zero while its
> cached[] pointer stays set, so the slot keeps pinning the css
> reference that refill_stock() took. The offlining drain only
> flushes slots with cached pages, so the reference is never released
> unless the slot happens to be displaced by an unrelated charge or
> by CPU hotplug, and the memcg lingers in the dying state - up to
> NR_MEMCG_STOCK (7) of them per CPU under container churn.
>
> Keeping the slot populated past the last page only saves a
> css_get()/css_put() pair on the next charge of the same memcg, and
> costs more than that: the offlining drain has to know about empty
> slots, and refill_stock() cannot reuse them either, so a charge
> under a different memcg evicts a live batch through the drain_idx
> rotation instead.
>
> Drop the reference in consume_stock() when the slot empties.
> Empty slots stop existing, so is_memcg_drain_needed() and the drain
> path stay as they are, and refill_stock() reuses emptied slots
> directly. The cost is one refcount pair per emptied slot, at most
> once per MEMCG_CHARGE_BATCH pages.
>
> Fixes: d1a05b6973c7 ("memcg: do not try to drain per-cpu caches without pages")
> Signed-off-by: Song Hu <husong@kylinos.cn>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] mm: memcg: release the css reference when a stock slot empties
2026-08-18 13:01 [PATCH v2] mm: memcg: release the css reference when a stock slot empties Song Hu
2026-08-18 14:29 ` Michal Hocko
2026-08-18 15:09 ` Shakeel Butt
@ 2026-08-19 4:24 ` Joshua Hahn
2 siblings, 0 replies; 4+ messages in thread
From: Joshua Hahn @ 2026-08-19 4:24 UTC (permalink / raw)
To: Song Hu
Cc: akpm, mhocko, mhocko, joshua.hahnjy, willy, shakeel.butt,
linux-mm, cgroups, linux-kernel, hannes, roman.gushchin,
muchun.song, zhuhui, audra, bingfangguo
On Tue, 18 Aug 2026 21:01:35 +0800 Song Hu <husong@kylinos.cn> wrote:
> consume_stock() can drive a stock slot's nr_pages to zero while its
> cached[] pointer stays set, so the slot keeps pinning the css
> reference that refill_stock() took. The offlining drain only
> flushes slots with cached pages, so the reference is never released
> unless the slot happens to be displaced by an unrelated charge or
> by CPU hotplug, and the memcg lingers in the dying state - up to
> NR_MEMCG_STOCK (7) of them per CPU under container churn.
>
> Keeping the slot populated past the last page only saves a
> css_get()/css_put() pair on the next charge of the same memcg, and
> costs more than that: the offlining drain has to know about empty
> slots, and refill_stock() cannot reuse them either, so a charge
> under a different memcg evicts a live batch through the drain_idx
> rotation instead.
>
> Drop the reference in consume_stock() when the slot empties.
> Empty slots stop existing, so is_memcg_drain_needed() and the drain
> path stay as they are, and refill_stock() reuses emptied slots
> directly. The cost is one refcount pair per emptied slot, at most
> once per MEMCG_CHARGE_BATCH pages.
Thakns for the v2, Song. This looks good to me. Have a great day!
Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> Fixes: d1a05b6973c7 ("memcg: do not try to drain per-cpu caches without pages")
> Signed-off-by: Song Hu <husong@kylinos.cn>
> ---
>
> Changes since v1: drop the reference at consume time instead of flushing
> empty slots from the offlining drain, as discussed with Michal Hocko and
> Joshua Hahn (full-stock drain cost, refill reuse).
>
> v1: https://lore.kernel.org/all/20260817025917.66233-1-husong@kylinos.cn/
>
> mm/memcontrol.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 17da1f43b7d3..1271d390b617 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2140,7 +2140,12 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
>
> stock_pages = READ_ONCE(stock->nr_pages[i]);
> if (stock_pages >= nr_pages) {
> - WRITE_ONCE(stock->nr_pages[i], stock_pages - nr_pages);
> + stock_pages -= nr_pages;
> + WRITE_ONCE(stock->nr_pages[i], stock_pages);
> + if (!stock_pages) {
> + css_put(&memcg->css);
> + WRITE_ONCE(stock->cached[i], NULL);
> + }
> ret = true;
> }
> break;
> --
> 2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread