Re: [PATCH v8 1/7] mm/memcontrol: flatten try_charge_memcg control flow
From: Balbir Singh
Date: Thu Nov 05 2026 - 01:09:06 EST
On Wed, Nov 04, 2026 at 03:30:22PM -0800, Joshua Hahn wrote:
> Refactor try_charge_memcg by flattening the nested memsw/memory
> page_counter operations to separate the logic between the two.
>
> When page_counter_try_charge is made stock-aware, this flattening makes
> the control flow easier to follow since each page counter now has its
> own success/failure paths.
>
> No functional changes intended.
>
> Acked-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
> Reviewed-by: Harry Yoo (Meta) <harry@xxxxxxxxxx>
> Signed-off-by: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>
> ---
> mm/memcontrol.c | 19 +++++++++++--------
> 1 file changed, 11 insertions(+), 8 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index ffbcea28a608c..66f38efb800ab 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2811,18 +2811,21 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
> batch = nr_pages;
>
> reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
> - if (!do_memsw_account() ||
> - page_counter_try_charge(&memcg->memsw, batch, &counter)) {
> - if (page_counter_try_charge(&memcg->memory, batch, &counter))
> - goto done_restock;
> - if (do_memsw_account())
> - page_counter_uncharge(&memcg->memsw, batch);
> - mem_over_limit = mem_cgroup_from_counter(counter, memory);
> - } else {
> + if (do_memsw_account() &&
> + !page_counter_try_charge(&memcg->memsw, batch, &counter)) {
> mem_over_limit = mem_cgroup_from_counter(counter, memsw);
> reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP;
> + goto reclaim;
> }
>
> + if (page_counter_try_charge(&memcg->memory, batch, &counter))
> + goto done_restock;
> +
> + if (do_memsw_account())
> + page_counter_uncharge(&memcg->memsw, batch);
> + mem_over_limit = mem_cgroup_from_counter(counter, memory);
> +
> +reclaim:
> if (batch > nr_pages) {
> batch = nr_pages;
> goto retry;
>
Acked-by: Balbir Singh <balbirs@xxxxxxxxxx>