Re: [PATCH -next v2 2/2] memcg: remove mem_cgroup_size()
From: Chen Ridong
Date: Wed Dec 10 2025 - 03:45:32 EST
On 2025/12/10 16:05, Michal Hocko wrote:
> On Wed 10-12-25 07:11:42, Chen Ridong wrote:
>> From: Chen Ridong <chenridong@xxxxxxxxxx>
>>
>> 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 replace its usage with page_counter_read for
>> clarity. Additionally, rename the local variable 'cgroup_size' to 'usage'
>> to better reflect its meaning.
>>
>> This change is safe because page_counter_read() is only called when memcg
>> is enabled in the apply_proportional_protection.
>>
>> No functional changes intended.
>
> I would prefer to keep the code as is.
>
I find the mem_cgroup_size() function name misleading—it suggests counting the number of memory
cgroups, but it actually returns the current memory usage.
When looking for a clearer alternative, I found mem_cgroup_usage(), which is only called by v1. This
raised the question of whether mem_cgroup_size() is truly necessary. Moreover, I noticed other code
locations simply call page_counter_read() directly to obtain current usage.
> Btw.
> [...]
>> diff --git a/mm/vmscan.c b/mm/vmscan.c
>> index 670fe9fae5ba..fe48d0376e7c 100644
>> --- a/mm/vmscan.c
>> +++ b/mm/vmscan.c
>> @@ -2451,6 +2451,7 @@ 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)
>> {
>> +#ifdef CONFIG_MEMCG
>> unsigned long min, low;
>>
>> mem_cgroup_protection(sc->target_mem_cgroup, memcg, &min, &low);
> [...]
>> @@ -2508,6 +2509,7 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg,
>> */
>> scan = max(scan, SWAP_CLUSTER_MAX);
>> }
>> +#endif
>> return scan;
>> }
>
> This returns a random garbage for !CONFIG_MEMCG, doesn't it?
>
--
Best regards,
Ridong