Re: [PATCH] mm/filemap: Adding missing mem_cgroup_uncharge() to __add_to_page_cache_locked()

From: Michal Hocko
Date: Tue Jan 26 2021 - 12:40:20 EST


On Mon 25-01-21 13:57:18, Waiman Long wrote:
> On 1/25/21 1:52 PM, Johannes Weiner wrote:
> > On Mon, Jan 25, 2021 at 01:23:58PM -0500, Waiman Long wrote:
> > > On 1/25/21 1:14 PM, Michal Hocko wrote:
[...]
> > > > With the proposed simplification by Willy
> > > > Acked-by: Michal Hocko <mhocko@xxxxxxxx>
> > > Thank for the ack. However, I am a bit confused about what you mean by
> > > simplification. There is another linux-next patch that changes the condition
> > > for mem_cgroup_charge() to
> > >
> > > -       if (!huge) {
> > > +       if (!huge && !page_is_secretmem(page)) {
> > >                 error = mem_cgroup_charge(page, current->mm, gfp);
> > >
> > > That is the main reason why I introduced the boolean variable as I don't
> > > want to call the external page_is_secretmem() function twice.
> > The variable works for me.
> >
> > On the other hand, as Michal points out, the uncharge function will be
> > called again on the page when it's being freed (in non-fscache cases),
> > so you're already relying on being able to call it on any page -
> > charged, uncharged, never charged. It would be fine to call it
> > unconditionally in the error path. Aesthetic preference, I guess.

Yes aesthetic preference... Just compare to
diff --git a/mm/filemap.c b/mm/filemap.c
index 5c9d564317a5..7aa05420107e 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -896,11 +896,14 @@ noinline int __add_to_page_cache_locked(struct page *page,

if (xas_error(&xas)) {
error = xas_error(&xas);
- goto error;
+ goto error_uncharge;
}

trace_mm_filemap_add_to_page_cache(page);
return 0;
+error_uncharge:
+ /* memcg will ignore uncharged pages */
+ mem_cgroup_uncharge(page);
error:
page->mapping = NULL;
/* Leave page->index set: truncation relies upon it */

which resembles our usual state unwinding style much more.

> That may be true. However, I haven't fully studied how the huge page memory
> accounting work to make sure the uncharge function can be called for huge
> pages.

... but this is rather lame argument to make, don't you think. This
sounds like a ducktaping engineering to me. Over time this leads to a
terrible code. Seriously!

All that being said I do not want to block this or bother people with
more emails but geez
--
Michal Hocko
SUSE Labs