linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosryahmed@google.com>
To: Jesper Dangaard Brouer <hawk@kernel.org>
Cc: tj@kernel.org, cgroups@vger.kernel.org, shakeel.butt@linux.dev,
	 hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com,
	 kernel-team@cloudflare.com, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,  mfleming@cloudflare.com
Subject: Re: [PATCH V10] cgroup/rstat: Avoid flushing if there is an ongoing root flush
Date: Thu, 5 Sep 2024 10:31:37 -0700	[thread overview]
Message-ID: <CAJD7tkYv8oDsPkVrUkmBrUxB02nEi-Suf=arsd5g4gM7tP2KxA@mail.gmail.com> (raw)
In-Reply-To: <f957dbe3-d669-40b7-8b90-08fa40a3c23d@kernel.org>

[..]
> >> @@ -299,6 +301,67 @@ static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop)
> >>          spin_unlock_irq(&cgroup_rstat_lock);
> >>   }
> >>
> >> +static inline bool cgroup_is_root(struct cgroup *cgrp)
> >> +{
> >> +       return cgroup_parent(cgrp) == NULL;
> >> +}
> >> +
> >> +/**
> >> + * cgroup_rstat_trylock_flusher - Trylock that checks for on ongoing flusher
> >> + * @cgrp: target cgroup
> >> + *
> >> + * Function return value follow trylock semantics. Returning true when lock is
> >> + * obtained. Returning false when not locked and it detected flushing can be
> >> + * skipped as another ongoing flusher is taking care of the flush.
> >> + *
> >> + * For callers that depend on flush completing before returning a strict option
> >> + * is provided.
> >> + */
> >> +static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp, bool strict)
> >> +{
> >> +       struct cgroup *ongoing;
> >> +
> >> +       if (strict)
> >> +               goto lock;
> >> +
> >> +       /*
> >> +        * Check if ongoing flusher is already taking care of this.  Descendant
> >> +        * check is necessary due to cgroup v1 supporting multiple root's.
> >> +        */
> >> +       ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher);
> >> +       if (ongoing && cgroup_is_descendant(cgrp, ongoing))
> >> +               return false;
> >
> > Why did we drop the agreed upon method of waiting until the flushers
> > are done? This is now a much more intrusive patch which makes all
> > flushers skip if a root is currently flushing. This causes
> > user-visible problems and is something that I worked hard to fix. I
> > thought we got good results with waiting for the ongoing flusher as
> > long as it is a root? What changed?
> >
>
> I disagree with the idea of waiting until the flusher is done.
> As Shakeel have pointed out before, we don't need accurate stats.
> This caused issues and 'completions' complicated the code too much.

I think Shakeel was referring specifically to the flush in the reclaim
path. I don't think this statement holds for all cgroup flushers,
especially those exposed to userspace.

>
> When multiple (12) kswapd's are running, then waiting for ongoing
> flusher will cause us to delay all other kswapd threads, for on my
> production system approx 24 ms (see attached prod graph).
> Matt (Cc) is currently[1] looking into page alloc failures that are
> happening across the fleet, when NIC RX packets as those allocs are
> GFP_ATOMIC.  So, basically kswapd isn't reclaiming memory fast enough on
> our systems, which could be related to this flush latency.  (Quick calc,
> prod server RX 1,159,695 pps, thus in 24 ms period 27,832 packets are
> handled, that exceed RX ring size 1024).
>
>   [1]
> https://lore.kernel.org/all/CAGis_TWzSu=P7QJmjD58WWiu3zjMTVKSzdOwWE8ORaGytzWJwQ@mail.gmail.com/
>
> For this reason, I don't want to have code that waits for ongoing
> flushers to finish.  This is why I changed the code.

My understanding was that the previous versions solved most of the
problem. However, if it's not enough and we need to completely skip
the flush, then I don't think this patch is the right way to go. This
affects all flushers, not just the reclaim path, and not even just the
memcg flushers. Waiting for ongoing flushers was a generic approach
that should work for all flushers, but completely skipping the flush
is not.

If your problem is specifically the flush in the reclaim path, then
Shakeel's patch to replace that flush with the ratelimited version
should fix your problem. It was already merged into mm-stable (so
headed toward v6.11 AFAICT).

>
>
> > You also never addressed my concern here about 'ongoing' while we are
> > accessing it, and never responded to my question in v8 about expanding
> > this to support non-root cgroups once we shift to a mutex.
> >
>
> I don't think we should expand this to non-root cgroups.  My production
> data from this V10 shows we don't need this for non-root cgroups.

Right, because you are concerned with the flush in the kswapd path
specifically. This patch touches affects much more than that.

>
>
> > I don't appreciate the silent yet drastic change made in this version
> > and without addressing concerns raised in previous versions. Please
> > let me know if I missed something.
> >
>
> IMHO we needed a drastic change, because patch was getting too
> complicated, and my production experiments showed that it was no-longer
> solving the contention issue (due to allowing non-root cgroups to become
> ongoing).

I thought we agreed to wait for the ongoing flusher to complete, but
only allow root cgroups to become the ongoing flusher (at least
initially). Not sure what changed.

>
> Production servers with this V10 patch applied shows HUGE improvements.
> Let me grab a graf showing level-0 contention events being reduced from
> 1360 event/sec to 0.277 events/sec.  I had to change to a log-scale graf
> to make improvement visible.  The wait-time is also basically gone.  The
> improvements are so convincing and highly needed, that we are going to
> deploy this improvement.  I usually have a very strong upstream first
> principle, but we simply cannot wait any-longer for a solution to this
> production issue.

Of course there is a huge improvement, you are completely skipping the
flush :) You are gaining a lot of performance but you'll also lose
something, there is no free lunch here. This may be an acceptable
tradeoff for the reclaim path, but definitely not for all flushers.

>
>
> >> +
> >> +       /* Grab right to be ongoing flusher */
> >> +       if (!ongoing && cgroup_is_root(cgrp)) {
> >> +               struct cgroup *old;
> >> +
> >> +               old = cmpxchg(&cgrp_rstat_ongoing_flusher, NULL, cgrp);
> >> +               if (old) {
> >> +                       /* Lost race for being ongoing flusher */
> >> +                       if (cgroup_is_descendant(cgrp, old))
> >> +                               return false;
> >> +               }
> >> +               /* Due to lock yield combined with strict mode record ID */
> >> +               WRITE_ONCE(cgrp_rstat_ongoing_flusher_ID, current);
> >
> > I am not sure I understand why we need this, do you mind elaborating?
>
> Let me expand the comment. Due to lock yield an ongoing (root) flusher
> can yield the lock, which would allow a root flush in strict mode to
> obtain the lock, which then in the unlock call (see below) will clear
> cgrp_rstat_ongoing_flusher (as cgrp in both cases have "root" cgrp ptr),
> unless it have this flush_ID to tell them apart.

The pointers should be different for different roots though, right?
Why do we need the ID to tell them apart? I am not sure I follow.


  reply	other threads:[~2024-09-05 17:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-04 19:41 Jesper Dangaard Brouer
2024-09-04 21:10 ` Yosry Ahmed
2024-09-05 14:08   ` Jesper Dangaard Brouer
2024-09-05 17:31     ` Yosry Ahmed [this message]
2024-09-10 14:16       ` Jesper Dangaard Brouer
2024-09-10 18:55         ` Yosry Ahmed
2024-09-12 15:41           ` Jesper Dangaard Brouer
2024-09-12 16:34             ` Yosry Ahmed
     [not found]               ` <CAKEwX=PTA0OxisvY12Wa95s5KqzvQTXe1rZ7nw29nP+wR2dxkA@mail.gmail.com>
     [not found]                 ` <CAJD7tkbMph337XbBTbWfF8kp_fStP3-rN77vfR5tcn2+wYfJPQ@mail.gmail.com>
2024-09-12 18:24                   ` Nhat Pham
2024-09-12 18:50                     ` Yosry Ahmed
2024-09-05 20:20 ` kernel test robot
2024-09-10  5:23 ` kernel test robot

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='CAJD7tkYv8oDsPkVrUkmBrUxB02nEi-Suf=arsd5g4gM7tP2KxA@mail.gmail.com' \
    --to=yosryahmed@google.com \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hawk@kernel.org \
    --cc=kernel-team@cloudflare.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lizefan.x@bytedance.com \
    --cc=longman@redhat.com \
    --cc=mfleming@cloudflare.com \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    /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