Re: [PATCH v2 1/5] mm: memcg: don't call propagate_protected_usage() needlessly

From: Yosry Ahmed
Date: Wed Jul 24 2024 - 19:14:06 EST


On Wed, Jul 24, 2024 at 1:21 PM Roman Gushchin <roman.gushchin@xxxxxxxxx> wrote:
>
> Memory protection (min/low) requires a constant tracking of
> protected memory usage. propagate_protected_usage() is called
> on each page counters update and does a number of operations
> even in cases when the actual memory protection functionality
> is not supported (e.g. hugetlb cgroups or memcg swap counters).
>
> It's obviously inefficient and leads to a waste of CPU cycles.
> It can be addressed by calling propagate_protected_usage() only
> for the counters which do support memory guarantees. As of now
> it's only memcg->memory - the unified memory memcg counter.
>
> Signed-off-by: Roman Gushchin <roman.gushchin@xxxxxxxxx>
> ---
> include/linux/page_counter.h | 8 +++++++-
> mm/hugetlb_cgroup.c | 4 ++--
> mm/memcontrol.c | 16 ++++++++--------
> mm/page_counter.c | 16 +++++++++++++---
> 4 files changed, 30 insertions(+), 14 deletions(-)
>
> diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
> index 860f313182e7..b31fd5b208aa 100644
> --- a/include/linux/page_counter.h
> +++ b/include/linux/page_counter.h
> @@ -32,6 +32,7 @@ struct page_counter {
> /* Keep all the read most fields in a separete cacheline. */
> CACHELINE_PADDING(_pad2_);
>
> + bool protection_support;
> unsigned long min;
> unsigned long low;
> unsigned long high;
> @@ -45,12 +46,17 @@ struct page_counter {
> #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
> #endif
>
> +/*
> + * Protection is supported only for the first counter (with id 0).
> + */
> static inline void page_counter_init(struct page_counter *counter,
> - struct page_counter *parent)
> + struct page_counter *parent,
> + bool protection_support)

Would it be better to make this an internal helper (e.g.
__page_counter_init()), and add another API function that passes in
protection_support=true, for example:

static inline void page_counter_init_protected(..)
{
__page_counter_init(.., true);
}

This will get rid of the naked booleans at the callsites of
page_counter_init(), which are more difficult to interpret. It will
also reduce the diff as we only need to change the page_counter_init()
calls of memcg->memory.

WDYT?

> {
> atomic_long_set(&counter->usage, 0);
> counter->max = PAGE_COUNTER_MAX;
> counter->parent = parent;
> + counter->protection_support = protection_support;
> }
[..]