linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yang Shi <shy828301@gmail.com>
To: Roman Gushchin <guro@fb.com>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>,
	Kirill Tkhai <ktkhai@virtuozzo.com>,
	 Shakeel Butt <shakeelb@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	 Linux MM <linux-mm@kvack.org>,
	 Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] mm: list_lru: hold nlru lock to avoid reading transient negative nr_items
Date: Mon, 30 Nov 2020 14:57:23 -0800	[thread overview]
Message-ID: <CAHbLzkoK+oFmmpmK7D7vc7fs__fdos+Ckuuj6CBVoP0Yb90YCw@mail.gmail.com> (raw)
In-Reply-To: <20201130225255.GF840171@carbon.dhcp.thefacebook.com>

On Mon, Nov 30, 2020 at 2:53 PM Roman Gushchin <guro@fb.com> wrote:
>
> On Mon, Nov 30, 2020 at 12:57:47PM -0800, Yang Shi wrote:
> > On Mon, Nov 30, 2020 at 12:09 PM Roman Gushchin <guro@fb.com> wrote:
> > >
> > > On Mon, Nov 30, 2020 at 10:45:14AM -0800, Yang Shi wrote:
> > > > When investigating a slab cache bloat problem, significant amount of
> > > > negative dentry cache was seen, but confusingly they neither got shrunk
> > > > by reclaimer (the host has very tight memory) nor be shrunk by dropping
> > > > cache.  The vmcore shows there are over 14M negative dentry objects on lru,
> > > > but tracing result shows they were even not scanned at all.  The further
> > > > investigation shows the memcg's vfs shrinker_map bit is not set.  So the
> > > > reclaimer or dropping cache just skip calling vfs shrinker.  So we have
> > > > to reboot the hosts to get the memory back.
> > > >
> > > > I didn't manage to come up with a reproducer in test environment, and the
> > > > problem can't be reproduced after rebooting.  But it seems there is race
> > > > between shrinker map bit clear and reparenting by code inspection.  The
> > > > hypothesis is elaborated as below.
> > > >
> > > > The memcg hierarchy on our production environment looks like:
> > > >                 root
> > > >                /    \
> > > >           system   user
> > > >
> > > > The main workloads are running under user slice's children, and it creates
> > > > and removes memcg frequently.  So reparenting happens very often under user
> > > > slice, but no task is under user slice directly.
> > > >
> > > > So with the frequent reparenting and tight memory pressure, the below
> > > > hypothetical race condition may happen:
> > > >
> > > >     CPU A                            CPU B                         CPU C
> > > > reparent
> > > >     dst->nr_items == 0
> > > >                                  shrinker:
> > > >                                      total_objects == 0
> > > >     add src->nr_items to dst
> > > >     set_bit
> > > >                                      retrun SHRINK_EMPTY
> > > >                                      clear_bit
> > > >                                                                   list_lru_del()
> > > > reparent again
> > > >     dst->nr_items may go negative
> > > >     due to current list_lru_del()
> > > >     on CPU C
> > > >                                  The second run of shrinker:
> > > >                                      read nr_items without any
> > > >                                      synchronization, so it may
> > > >                                      see intermediate negative
> > > >                                      nr_items then total_objects
> > > >                                      may return 0 conincidently
> > > >
> > > >                                      keep the bit cleared
> > > >     dst->nr_items != 0
> > > >     skip set_bit
> > > >     add scr->nr_item to dst
>
> Btw, I think I have a simpler explanation:
>
> A (0 objects)
> |
> B (N objects)
>
> Let's say the reparenting races with the deletion of a single slab object.
> list_lru_del() can see parent's lru list and substract 1 from nr_items == 0,
> setting A's nr_items to -1 (the item is actually still in B's list).
>
> memcg_drain_list_lru_node() will check !dst->nr_items && src->nr_items
> !-1 && N => 0 and not set the bit. But now we have (N-1) objects in A's list
> and the shrinker bit not set.

Yes, this is the exact race I elaborated in the commit log.

>
> My proposed fix should resolve it. Alternatively, we maybe can check if
> dst->nr_items <= 0 and only then set the bit, but it seems to be an unnecessary
> optimization.

Yes, I think "src->nr_items != 0" is good enough.

>
> Thanks!


  reply	other threads:[~2020-11-30 22:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-30 18:45 Yang Shi
2020-11-30 20:09 ` Roman Gushchin
2020-11-30 20:57   ` Yang Shi
2020-11-30 22:33     ` Roman Gushchin
2020-11-30 22:54       ` Yang Shi
2020-11-30 23:07         ` Roman Gushchin
2020-11-30 22:52     ` Roman Gushchin
2020-11-30 22:57       ` Yang Shi [this message]
2020-11-30 23:09         ` Roman Gushchin
2020-12-01 10:25   ` Kirill Tkhai
2020-12-01 17:15     ` Yang Shi
2020-12-01 17:17       ` Kirill Tkhai
2020-12-01 17:20         ` Yang Shi
2020-12-01 10:09 ` Kirill Tkhai
2020-12-01 17:19   ` Yang Shi

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=CAHbLzkoK+oFmmpmK7D7vc7fs__fdos+Ckuuj6CBVoP0Yb90YCw@mail.gmail.com \
    --to=shy828301@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=guro@fb.com \
    --cc=ktkhai@virtuozzo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=shakeelb@google.com \
    --cc=vdavydov.dev@gmail.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