From: Yosry Ahmed <yosryahmed@google.com>
To: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeelb@google.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@suse.com>, Yu Zhao <yuzhao@google.com>,
Muchun Song <songmuchun@bytedance.com>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
Vasily Averin <vasily.averin@linux.dev>,
Vlastimil Babka <vbabka@suse.cz>,
Chris Down <chris@chrisdown.name>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH v2 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until()
Date: Wed, 30 Nov 2022 10:25:48 -0800 [thread overview]
Message-ID: <CAJD7tkaqS1x4UrN7Amq2tzhrtxifG8wzbtw8=ROVpoM0TE4LBw@mail.gmail.com> (raw)
In-Reply-To: <Y4eQupgWwX0/ItIX@P9FQF9L96D.corp.robot.car>
On Wed, Nov 30, 2022 at 9:20 AM Roman Gushchin <roman.gushchin@linux.dev> wrote:
>
> On Tue, Nov 29, 2022 at 11:42:31AM -0800, Yosry Ahmed wrote:
> > On Wed, Nov 23, 2022 at 7:16 PM Yosry Ahmed <yosryahmed@google.com> wrote:
> > >
> > > On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin <roman.gushchin@linux.dev> wrote:
> > > >
> > > > On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote:
> > > > > Refactor the code that drives writing to memory.reclaim (retrying, error
> > > > > handling, etc) from test_memcg_reclaim() to a helper called
> > > > > reclaim_until(), which proactively reclaims from a memcg until its
> > > > > usage reaches a certain value.
> > > > >
> > > > > This will be used in a following patch in another test.
> > > > >
> > > > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
> > > > > ---
> > > > > .../selftests/cgroup/test_memcontrol.c | 85 +++++++++++--------
> > > > > 1 file changed, 49 insertions(+), 36 deletions(-)
> > > > >
> > > > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
> > > > > index 8833359556f3..d4182e94945e 100644
> > > > > --- a/tools/testing/selftests/cgroup/test_memcontrol.c
> > > > > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c
> > > > > @@ -645,6 +645,53 @@ static int test_memcg_max(const char *root)
> > > > > return ret;
> > > >
> > > >
> > > > The code below looks correct, but can be simplified a bit.
> > > > And btw thank you for adding a test!
> > > >
> > > > Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev>
> > > > (idk if you want invest your time in further simplication of this code,
> > > > it was this way before this patch, so up to you).
> > >
> > > I don't "want" to, but the voices in my head won't shut up until I do so..
> > >
> > > Here's a patch that simplifies the code, I inlined it here to avoid
> > > sending a new version. If it looks good to you, it can be squashed
> > > into this patch or merged separately (whatever you and Andrew prefer).
> > > I can also send it in a separate thread if preferred.
> >
> > Roman, any thoughts on this?
> >
> > >
> > >
> > > From 18c40d61dac05b33cfc9233b17979b54422ed7c5 Mon Sep 17 00:00:00 2001
> > > From: Yosry Ahmed <yosryahmed@google.com>
> > > Date: Thu, 24 Nov 2022 02:21:12 +0000
> > > Subject: [PATCH] selftests: cgroup: simplify memcg reclaim code
> > >
> > > Simplify the code for the reclaim_until() helper used for memcg reclaim
> > > through memory.reclaim.
> > >
> > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
> > > ---
> > > .../selftests/cgroup/test_memcontrol.c | 65 ++++++++++---------
> > > 1 file changed, 33 insertions(+), 32 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c
> > > b/tools/testing/selftests/cgroup/test_memcontrol.c
> > > index bac3b91f1579..2e2bde44a6f7 100644
> > > --- a/tools/testing/selftests/cgroup/test_memcontrol.c
> > > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c
> > > @@ -17,6 +17,7 @@
> > > #include <netdb.h>
> > > #include <errno.h>
> > > #include <sys/mman.h>
> > > +#include <limits.h>
> > >
> > > #include "../kselftest.h"
> > > #include "cgroup_util.h"
> > > @@ -656,51 +657,51 @@ static int test_memcg_max(const char *root)
> > > return ret;
> > > }
> > >
> > > -/* Reclaim from @memcg until usage reaches @goal_usage */
> > > +/*
> > > + * Reclaim from @memcg until usage reaches @goal_usage by writing to
> > > + * memory.reclaim.
> > > + *
> > > + * This function will return false if the usage is already below the
> > > + * goal.
> > > + *
> > > + * This function assumes that writing to memory.reclaim is the only
> > > + * source of change in memory.current (no concurrent allocations or
> > > + * reclaim).
> > > + *
> > > + * This function makes sure memory.reclaim is sane. It will return
> > > + * false if memory.reclaim's error codes do not make sense, even if
> > > + * the usage goal was satisfied.
> > > + */
> > > static bool reclaim_until(const char *memcg, long goal_usage)
> > > {
> > > char buf[64];
> > > int retries = 5;
> > > - int err;
> > > + int err = INT_MAX;
> > > long current, to_reclaim;
> > >
> > > - /* Nothing to do here */
> > > - if (cg_read_long(memcg, "memory.current") <= goal_usage)
> > > - return true;
> > > -
>
> Hi Yosry!
>
> Thank you for working on this!
> I feel like it's still way more complex than it can be.
> How about something like this? (completely untested, treat is
> as a pseudo-code).
Thanks Roman!
This looks much simpler, and it nicely and subtly catches the false
negative case (where we return -EAGAIN but have actually reclaimed the
requested amount), but I think it doesn't catch the false positive
case (where memory.reclaim returns 0 but hasn't reclaimed enough
memory). In this case I think we will just keep retrying and ignore
the false positive?
Maybe with the following added check?
>
>
> {
> ...
> bool ret = false;
>
> for (retries = 5; retries > 0; retries--) {
> current = cg_read_long(memcg, "memory.current");
>
> if (current <= goal) // replace with values_close?
> break;
else if (ret) { // false positive?
ret = false;
break;
}
>
> to_reclaim = current - goal_usage;
> snprintf(buf, sizeof(buf), "%ld", to_reclaim);
> err = cg_write(memcg, "memory.reclaim", buf);
> if (!err)
> ret = true;
> else if (err != -AGAIN)
> break;
> }
>
> return ret;
> }
Also, please let me know if you prefer that I send this cleanup in the
same thread like the above, in a completely separate patch that
depends on this series, or have it squashed into this patch in a v3.
Thanks again!
next prev parent reply other threads:[~2022-11-30 18:26 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-23 9:21 [PATCH v2 0/3] mm: memcg: fix protection of reclaim target memcg Yosry Ahmed
2022-11-23 9:21 ` [PATCH v2 1/3] mm: memcg: fix stale " Yosry Ahmed
2022-11-24 0:40 ` Roman Gushchin
2022-11-24 0:57 ` Yosry Ahmed
2022-11-23 9:21 ` [PATCH v2 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until() Yosry Ahmed
2022-11-24 1:03 ` Roman Gushchin
2022-11-24 3:16 ` Yosry Ahmed
2022-11-29 19:42 ` Yosry Ahmed
2022-11-30 17:19 ` Roman Gushchin
2022-11-30 18:25 ` Yosry Ahmed [this message]
2022-12-02 3:19 ` Yosry Ahmed
2022-11-23 9:21 ` [PATCH v2 3/3] selftests: cgroup: make sure reclaim target memcg is unprotected Yosry Ahmed
2022-11-24 1:04 ` Roman Gushchin
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='CAJD7tkaqS1x4UrN7Amq2tzhrtxifG8wzbtw8=ROVpoM0TE4LBw@mail.gmail.com' \
--to=yosryahmed@google.com \
--cc=chris@chrisdown.name \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeelb@google.com \
--cc=songmuchun@bytedance.com \
--cc=vasily.averin@linux.dev \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=yuzhao@google.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