* Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
2025-12-11 1:30 ` [PATCH -next v3 2/2] memcg: remove mem_cgroup_size() Chen Ridong
@ 2025-12-11 2:58 ` Johannes Weiner
2025-12-11 10:48 ` Michal Hocko
` (2 subsequent siblings)
3 siblings, 0 replies; 11+ messages in thread
From: Johannes Weiner @ 2025-12-11 2:58 UTC (permalink / raw)
To: Chen Ridong
Cc: mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
axelrasmussen, yuanchu, weixugc, david, zhengqi.arch,
lorenzo.stoakes, cgroups, linux-mm, linux-kernel, lujialin4
On Thu, Dec 11, 2025 at 01:30:19AM +0000, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> The mem_cgroup_size helper is used only in apply_proportional_protection
> to read the current memory usage. Its semantics are unclear and
> inconsistent with other sites, which directly call page_counter_read for
> the same purpose.
>
> Remove this helper and get its usage via mem_cgroup_protection for
> clarity. Additionally, rename the local variable 'cgroup_size' to 'usage'
> to better reflect its meaning.
>
> No functional changes intended.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
2025-12-11 1:30 ` [PATCH -next v3 2/2] memcg: remove mem_cgroup_size() Chen Ridong
2025-12-11 2:58 ` Johannes Weiner
@ 2025-12-11 10:48 ` Michal Hocko
2025-12-12 0:37 ` Shakeel Butt
2025-12-15 16:28 ` Michal Koutný
3 siblings, 0 replies; 11+ messages in thread
From: Michal Hocko @ 2025-12-11 10:48 UTC (permalink / raw)
To: Chen Ridong
Cc: hannes, roman.gushchin, shakeel.butt, muchun.song, akpm,
axelrasmussen, yuanchu, weixugc, david, zhengqi.arch,
lorenzo.stoakes, cgroups, linux-mm, linux-kernel, lujialin4
On Thu 11-12-25 01:30:19, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> The mem_cgroup_size helper is used only in apply_proportional_protection
> to read the current memory usage. Its semantics are unclear and
> inconsistent with other sites, which directly call page_counter_read for
> the same purpose.
>
> Remove this helper and get its usage via mem_cgroup_protection for
> clarity. Additionally, rename the local variable 'cgroup_size' to 'usage'
> to better reflect its meaning.
>
> No functional changes intended.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
Yes, this looks much better.
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
> include/linux/memcontrol.h | 18 +++++++-----------
> mm/memcontrol.c | 5 -----
> mm/vmscan.c | 9 ++++-----
> 3 files changed, 11 insertions(+), 21 deletions(-)
>
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index 6a48398a1f4e..603252e3169c 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -557,13 +557,15 @@ static inline bool mem_cgroup_disabled(void)
> static inline void mem_cgroup_protection(struct mem_cgroup *root,
> struct mem_cgroup *memcg,
> unsigned long *min,
> - unsigned long *low)
> + unsigned long *low,
> + unsigned long *usage)
> {
> - *min = *low = 0;
> + *min = *low = *usage = 0;
>
> if (mem_cgroup_disabled())
> return;
>
> + *usage = page_counter_read(&memcg->memory);
> /*
> * There is no reclaim protection applied to a targeted reclaim.
> * We are special casing this specific case here because
> @@ -919,8 +921,6 @@ static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
>
> unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
>
> -unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
> -
> void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
> struct task_struct *p);
>
> @@ -1102,9 +1102,10 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm,
> static inline void mem_cgroup_protection(struct mem_cgroup *root,
> struct mem_cgroup *memcg,
> unsigned long *min,
> - unsigned long *low)
> + unsigned long *low,
> + unsigned long *usage)
> {
> - *min = *low = 0;
> + *min = *low = *usage = 0;
> }
>
> static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
> @@ -1328,11 +1329,6 @@ static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
> return 0;
> }
>
> -static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
> -{
> - return 0;
> -}
> -
> static inline void
> mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
> {
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index dbe7d8f93072..659ce171b1b3 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1621,11 +1621,6 @@ unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
> return max;
> }
>
> -unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
> -{
> - return page_counter_read(&memcg->memory);
> -}
> -
> void __memcg_memory_event(struct mem_cgroup *memcg,
> enum memcg_memory_event event, bool allow_spinning)
> {
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 670fe9fae5ba..9a6ee80275fc 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -2451,9 +2451,9 @@ static inline void calculate_pressure_balance(struct scan_control *sc,
> static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
> struct scan_control *sc, unsigned long scan)
> {
> - unsigned long min, low;
> + unsigned long min, low, usage;
>
> - mem_cgroup_protection(sc->target_mem_cgroup, memcg, &min, &low);
> + mem_cgroup_protection(sc->target_mem_cgroup, memcg, &min, &low, &usage);
>
> if (min || low) {
> /*
> @@ -2485,7 +2485,6 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
> * again by how much of the total memory used is under
> * hard protection.
> */
> - unsigned long cgroup_size = mem_cgroup_size(memcg);
> unsigned long protection;
>
> /* memory.low scaling, make sure we retry before OOM */
> @@ -2497,9 +2496,9 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
> }
>
> /* Avoid TOCTOU with earlier protection check */
> - cgroup_size = max(cgroup_size, protection);
> + usage = max(usage, protection);
>
> - scan -= scan * protection / (cgroup_size + 1);
> + scan -= scan * protection / (usage + 1);
>
> /*
> * Minimally target SWAP_CLUSTER_MAX pages to keep
> --
> 2.34.1
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
2025-12-11 1:30 ` [PATCH -next v3 2/2] memcg: remove mem_cgroup_size() Chen Ridong
2025-12-11 2:58 ` Johannes Weiner
2025-12-11 10:48 ` Michal Hocko
@ 2025-12-12 0:37 ` Shakeel Butt
2025-12-15 16:28 ` Michal Koutný
3 siblings, 0 replies; 11+ messages in thread
From: Shakeel Butt @ 2025-12-12 0:37 UTC (permalink / raw)
To: Chen Ridong
Cc: hannes, mhocko, roman.gushchin, muchun.song, akpm, axelrasmussen,
yuanchu, weixugc, david, zhengqi.arch, lorenzo.stoakes, cgroups,
linux-mm, linux-kernel, lujialin4
On Thu, Dec 11, 2025 at 01:30:19AM +0000, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> The mem_cgroup_size helper is used only in apply_proportional_protection
> to read the current memory usage. Its semantics are unclear and
> inconsistent with other sites, which directly call page_counter_read for
> the same purpose.
>
> Remove this helper and get its usage via mem_cgroup_protection for
> clarity. Additionally, rename the local variable 'cgroup_size' to 'usage'
> to better reflect its meaning.
>
> No functional changes intended.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
2025-12-11 1:30 ` [PATCH -next v3 2/2] memcg: remove mem_cgroup_size() Chen Ridong
` (2 preceding siblings ...)
2025-12-12 0:37 ` Shakeel Butt
@ 2025-12-15 16:28 ` Michal Koutný
2025-12-16 12:34 ` Chen Ridong
3 siblings, 1 reply; 11+ messages in thread
From: Michal Koutný @ 2025-12-15 16:28 UTC (permalink / raw)
To: Chen Ridong
Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
axelrasmussen, yuanchu, weixugc, david, zhengqi.arch,
lorenzo.stoakes, cgroups, linux-mm, linux-kernel, lujialin4
[-- Attachment #1: Type: text/plain, Size: 1700 bytes --]
Hi Ridong.
On Thu, Dec 11, 2025 at 01:30:19AM +0000, Chen Ridong <chenridong@huaweicloud.com> wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> The mem_cgroup_size helper is used only in apply_proportional_protection
> to read the current memory usage. Its semantics are unclear and
> inconsistent with other sites, which directly call page_counter_read for
> the same purpose.
>
> Remove this helper and get its usage via mem_cgroup_protection for
> clarity. Additionally, rename the local variable 'cgroup_size' to 'usage'
> to better reflect its meaning.
>
> No functional changes intended.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
Why does mem_cgroup_calculate_protection "calculate" usage for its
callers? Couldn't you just the change source in
apply_proportional_protection()?
Thanks,
Michal
> @@ -2485,7 +2485,6 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
> * again by how much of the total memory used is under
> * hard protection.
> */
> - unsigned long cgroup_size = mem_cgroup_size(memcg);
+ unsigned long cgroup_size = page_counter_read(memcg);
> unsigned long protection;
>
> /* memory.low scaling, make sure we retry before OOM */
> @@ -2497,9 +2496,9 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
> }
>
> /* Avoid TOCTOU with earlier protection check */
> - cgroup_size = max(cgroup_size, protection);
> + usage = max(usage, protection);
>
> - scan -= scan * protection / (cgroup_size + 1);
> + scan -= scan * protection / (usage + 1);
>
> /*
> * Minimally target SWAP_CLUSTER_MAX pages to keep
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
2025-12-15 16:28 ` Michal Koutný
@ 2025-12-16 12:34 ` Chen Ridong
2025-12-16 13:55 ` Michal Koutný
0 siblings, 1 reply; 11+ messages in thread
From: Chen Ridong @ 2025-12-16 12:34 UTC (permalink / raw)
To: Michal Koutný
Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
axelrasmussen, yuanchu, weixugc, david, zhengqi.arch,
lorenzo.stoakes, cgroups, linux-mm, linux-kernel, lujialin4
On 2025/12/16 0:28, Michal Koutný wrote:
> Hi Ridong.
>
> On Thu, Dec 11, 2025 at 01:30:19AM +0000, Chen Ridong <chenridong@huaweicloud.com> wrote:
>> From: Chen Ridong <chenridong@huawei.com>
>>
>> The mem_cgroup_size helper is used only in apply_proportional_protection
>> to read the current memory usage. Its semantics are unclear and
>> inconsistent with other sites, which directly call page_counter_read for
>> the same purpose.
>>
>> Remove this helper and get its usage via mem_cgroup_protection for
>> clarity. Additionally, rename the local variable 'cgroup_size' to 'usage'
>> to better reflect its meaning.
>>
>> No functional changes intended.
>>
>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>
> Why does mem_cgroup_calculate_protection "calculate" usage for its
> callers? Couldn't you just the change source in
> apply_proportional_protection()?
>
> Thanks,
> Michal
>
I apologize for missing this message earlier.
In my v2 patch, I was reading the usage directly:
+ unsigned long usage = page_counter_read(&memcg->memory);
This works fine when CONFIG_MEMCG=y, but fails to compile when memory cgroups are disabled. To
handle this, I initially added #ifdef CONFIG_MEMCG guards.
Following Johannes's suggestion, I have now moved this logic into mem_cgroup_protection() to
eliminate the #ifdef and keep the code cleaner.
Discussion:
https://lore.kernel.org/all/20251210163634.GB643576@cmpxchg.org/
>> @@ -2485,7 +2485,6 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
>> * again by how much of the total memory used is under
>> * hard protection.
>> */
>> - unsigned long cgroup_size = mem_cgroup_size(memcg);
> + unsigned long cgroup_size = page_counter_read(memcg);
>
>> unsigned long protection;
>>
>> /* memory.low scaling, make sure we retry before OOM */
>> @@ -2497,9 +2496,9 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
>> }
>>
>> /* Avoid TOCTOU with earlier protection check */
>> - cgroup_size = max(cgroup_size, protection);
>> + usage = max(usage, protection);
>>
>> - scan -= scan * protection / (cgroup_size + 1);
>> + scan -= scan * protection / (usage + 1);
>>
>> /*
>> * Minimally target SWAP_CLUSTER_MAX pages to keep
--
Best regards,
Ridong
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
2025-12-16 12:34 ` Chen Ridong
@ 2025-12-16 13:55 ` Michal Koutný
0 siblings, 0 replies; 11+ messages in thread
From: Michal Koutný @ 2025-12-16 13:55 UTC (permalink / raw)
To: Chen Ridong
Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
axelrasmussen, yuanchu, weixugc, david, zhengqi.arch,
lorenzo.stoakes, cgroups, linux-mm, linux-kernel, lujialin4
[-- Attachment #1: Type: text/plain, Size: 442 bytes --]
On Tue, Dec 16, 2025 at 08:34:40PM +0800, Chen Ridong <chenridong@huaweicloud.com> wrote:
> In my v2 patch, I was reading the usage directly:
>
> + unsigned long usage = page_counter_read(&memcg->memory);
>
> This works fine when CONFIG_MEMCG=y, but fails to compile when memory cgroups are disabled. To
> handle this, I initially added #ifdef CONFIG_MEMCG guards.
Ah, !CONFIG_MEMCG, I get it now. Sorry for the noise.
Michal
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread