From: Chen Ridong <chenridong@huaweicloud.com>
To: "Michal Koutný" <mkoutny@suse.com>
Cc: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev,
shakeel.butt@linux.dev, muchun.song@linux.dev,
akpm@linux-foundation.org, axelrasmussen@google.com,
yuanchu@google.com, weixugc@google.com, david@kernel.org,
zhengqi.arch@bytedance.com, lorenzo.stoakes@oracle.com,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, lujialin4@huawei.com
Subject: Re: [PATCH -next v3 2/2] memcg: remove mem_cgroup_size()
Date: Tue, 16 Dec 2025 20:34:40 +0800 [thread overview]
Message-ID: <476fcde5-d54d-4b15-9870-844b3b8c700a@huaweicloud.com> (raw)
In-Reply-To: <o3hmzratjkcxms3ylnjiuashclllf7mvz6ttkfrz4lybdiwhhp@yeo5my374trx>
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
next prev parent reply other threads:[~2025-12-16 12:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 1:30 [PATCH -next v3 0/2] memcg cleanups Chen Ridong
2025-12-11 1:30 ` [PATCH -next v3 1/2] memcg: move mem_cgroup_usage memcontrol-v1.c Chen Ridong
2025-12-12 0:36 ` Shakeel Butt
2025-12-15 16:28 ` Michal Koutný
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ý
2025-12-16 12:34 ` Chen Ridong [this message]
2025-12-16 13:55 ` Michal Koutný
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=476fcde5-d54d-4b15-9870-844b3b8c700a@huaweicloud.com \
--to=chenridong@huaweicloud.com \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=cgroups@vger.kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lujialin4@huawei.com \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=weixugc@google.com \
--cc=yuanchu@google.com \
--cc=zhengqi.arch@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).