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

From: Yosry Ahmed
Date: Wed Jul 24 2024 - 19:36:23 EST


On Wed, Jul 24, 2024 at 4:32 PM Roman Gushchin <roman.gushchin@xxxxxxxxx> wrote:
>
> On Wed, Jul 24, 2024 at 04:13:17PM -0700, Yosry Ahmed wrote:
> > 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?
>
> No strong opinion here. There are basically 2 call sites and I don't expect
> this number to grow, so not sure if it makes sense to add 2 new helpers.
>
> Another option I thought about is to leave page_counter_init() as it is
> and add a separate function to enable the protection tracking.

Yeah this should work too, in fact it should give us a chance to
choose a more descriptive name that page_counter_init_protected()
(e.g. page_counter_enable_protection()).

My main concern is the naked booleans, and secondarily the unnecessary
diff to other init sites. I also don't feel strongly about it though.

>
> Thanks!