linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: Yang Shi <shy828301@gmail.com>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	Shakeel Butt <shakeelb@google.com>,
	Dave Chinner <david@fromorbit.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@suse.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linux MM <linux-mm@kvack.org>,
	Linux FS-devel Mailing List <linux-fsdevel@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [v7 PATCH 08/12] mm: vmscan: add per memcg shrinker nr_deferred
Date: Tue, 9 Feb 2021 17:40:18 -0800	[thread overview]
Message-ID: <20210210014018.GR524633@carbon.DHCP.thefacebook.com> (raw)
In-Reply-To: <CAHbLzkpHRO1-iamUvwrg41MyzAonCPcUiObo7LFKLTbCuZptvQ@mail.gmail.com>

On Tue, Feb 09, 2021 at 05:25:16PM -0800, Yang Shi wrote:
> On Tue, Feb 9, 2021 at 5:10 PM Roman Gushchin <guro@fb.com> wrote:
> >
> > On Tue, Feb 09, 2021 at 09:46:42AM -0800, Yang Shi wrote:
> > > Currently the number of deferred objects are per shrinker, but some slabs, for example,
> > > vfs inode/dentry cache are per memcg, this would result in poor isolation among memcgs.
> > >
> > > The deferred objects typically are generated by __GFP_NOFS allocations, one memcg with
> > > excessive __GFP_NOFS allocations may blow up deferred objects, then other innocent memcgs
> > > may suffer from over shrink, excessive reclaim latency, etc.
> > >
> > > For example, two workloads run in memcgA and memcgB respectively, workload in B is vfs
> > > heavy workload.  Workload in A generates excessive deferred objects, then B's vfs cache
> > > might be hit heavily (drop half of caches) by B's limit reclaim or global reclaim.
> > >
> > > We observed this hit in our production environment which was running vfs heavy workload
> > > shown as the below tracing log:
> > >
> > > <...>-409454 [016] .... 28286961.747146: mm_shrink_slab_start: super_cache_scan+0x0/0x1a0 ffff9a83046f3458:
> > > nid: 1 objects to shrink 3641681686040 gfp_flags GFP_HIGHUSER_MOVABLE|__GFP_ZERO pgs_scanned 1 lru_pgs 15721
> > > cache items 246404277 delta 31345 total_scan 123202138
> > > <...>-409454 [022] .... 28287105.928018: mm_shrink_slab_end: super_cache_scan+0x0/0x1a0 ffff9a83046f3458:
> > > nid: 1 unused scan count 3641681686040 new scan count 3641798379189 total_scan 602
> > > last shrinker return val 123186855
> > >
> > > The vfs cache and page cache ratio was 10:1 on this machine, and half of caches were dropped.
> > > This also resulted in significant amount of page caches were dropped due to inodes eviction.
> > >
> > > Make nr_deferred per memcg for memcg aware shrinkers would solve the unfairness and bring
> > > better isolation.
> > >
> > > When memcg is not enabled (!CONFIG_MEMCG or memcg disabled), the shrinker's nr_deferred
> > > would be used.  And non memcg aware shrinkers use shrinker's nr_deferred all the time.
> > >
> > > Signed-off-by: Yang Shi <shy828301@gmail.com>
> > > ---
> > >  include/linux/memcontrol.h |  7 +++---
> > >  mm/vmscan.c                | 49 +++++++++++++++++++++++++-------------
> > >  2 files changed, 37 insertions(+), 19 deletions(-)
> > >
> > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> > > index 4c9253896e25..c457fc7bc631 100644
> > > --- a/include/linux/memcontrol.h
> > > +++ b/include/linux/memcontrol.h
> > > @@ -93,12 +93,13 @@ struct lruvec_stat {
> > >  };
> > >
> > >  /*
> > > - * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
> > > - * which have elements charged to this memcg.
> > > + * Bitmap and deferred work of shrinker::id corresponding to memcg-aware
> > > + * shrinkers, which have elements charged to this memcg.
> > >   */
> > >  struct shrinker_info {
> > >       struct rcu_head rcu;
> > > -     unsigned long map[];
> > > +     atomic_long_t *nr_deferred;
> > > +     unsigned long *map;
> > >  };
> > >
> > >  /*
> > > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > > index a047980536cf..d4b030a0b2a9 100644
> > > --- a/mm/vmscan.c
> > > +++ b/mm/vmscan.c
> > > @@ -187,9 +187,13 @@ static DECLARE_RWSEM(shrinker_rwsem);
> > >  #ifdef CONFIG_MEMCG
> > >  static int shrinker_nr_max;
> > >
> > > +/* The shrinker_info is expanded in a batch of BITS_PER_LONG */
> > >  #define NR_MAX_TO_SHR_MAP_SIZE(nr_max) \
> > >       (DIV_ROUND_UP(nr_max, BITS_PER_LONG) * sizeof(unsigned long))
> > >
> > > +#define NR_MAX_TO_SHR_DEF_SIZE(nr_max) \
> > > +     (round_up(nr_max, BITS_PER_LONG) * sizeof(atomic_long_t))
> > > +
> > >  static struct shrinker_info *shrinker_info_protected(struct mem_cgroup *memcg,
> > >                                                    int nid)
> > >  {
> > > @@ -203,10 +207,12 @@ static void free_shrinker_info_rcu(struct rcu_head *head)
> > >  }
> > >
> > >  static int expand_one_shrinker_info(struct mem_cgroup *memcg,
> > > -                                int size, int old_size)
> > > +                                 int m_size, int d_size,
> > > +                                 int old_m_size, int old_d_size)
> > >  {
> > >       struct shrinker_info *new, *old;
> > >       int nid;
> > > +     int size = m_size + d_size;
> > >
> > >       for_each_node(nid) {
> > >               old = shrinker_info_protected(memcg, nid);
> > > @@ -218,9 +224,15 @@ static int expand_one_shrinker_info(struct mem_cgroup *memcg,
> > >               if (!new)
> > >                       return -ENOMEM;
> > >
> > > -             /* Set all old bits, clear all new bits */
> > > -             memset(new->map, (int)0xff, old_size);
> > > -             memset((void *)new->map + old_size, 0, size - old_size);
> > > +             new->nr_deferred = (atomic_long_t *)(new + 1);
> > > +             new->map = (void *)new->nr_deferred + d_size;
> > > +
> > > +             /* map: set all old bits, clear all new bits */
> > > +             memset(new->map, (int)0xff, old_m_size);
> > > +             memset((void *)new->map + old_m_size, 0, m_size - old_m_size);
> > > +             /* nr_deferred: copy old values, clear all new values */
> > > +             memcpy(new->nr_deferred, old->nr_deferred, old_d_size);
> > > +             memset((void *)new->nr_deferred + old_d_size, 0, d_size - old_d_size);
> > >
> > >               rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, new);
> > >               call_rcu(&old->rcu, free_shrinker_info_rcu);
> > > @@ -235,9 +247,6 @@ void free_shrinker_info(struct mem_cgroup *memcg)
> > >       struct shrinker_info *info;
> > >       int nid;
> > >
> > > -     if (mem_cgroup_is_root(memcg))
> > > -             return;
> > > -
> > >       for_each_node(nid) {
> > >               pn = mem_cgroup_nodeinfo(memcg, nid);
> > >               info = shrinker_info_protected(memcg, nid);
> > > @@ -250,12 +259,13 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
> > >  {
> > >       struct shrinker_info *info;
> > >       int nid, size, ret = 0;
> > > -
> > > -     if (mem_cgroup_is_root(memcg))
> > > -             return 0;
> > > +     int m_size, d_size = 0;
> > >
> > >       down_write(&shrinker_rwsem);
> > > -     size = NR_MAX_TO_SHR_MAP_SIZE(shrinker_nr_max);
> > > +     m_size = NR_MAX_TO_SHR_MAP_SIZE(shrinker_nr_max);
> > > +     d_size = NR_MAX_TO_SHR_DEF_SIZE(shrinker_nr_max);
> > > +     size = m_size + d_size;
> > > +
> > >       for_each_node(nid) {
> > >               info = kvzalloc_node(sizeof(*info) + size, GFP_KERNEL, nid);
> > >               if (!info) {
> > > @@ -263,6 +273,8 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
> > >                       ret = -ENOMEM;
> > >                       break;
> > >               }
> > > +             info->nr_deferred = (atomic_long_t *)(info + 1);
> > > +             info->map = (void *)info->nr_deferred + d_size;
> > >               rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_info, info);
> > >       }
> > >       up_write(&shrinker_rwsem);
> > > @@ -274,10 +286,16 @@ static int expand_shrinker_info(int new_id)
> > >  {
> > >       int size, old_size, ret = 0;
> > >       int new_nr_max = new_id + 1;
> > > +     int m_size, d_size = 0;
> > > +     int old_m_size, old_d_size = 0;
> > >       struct mem_cgroup *memcg;
> > >
> > > -     size = NR_MAX_TO_SHR_MAP_SIZE(new_nr_max);
> > > -     old_size = NR_MAX_TO_SHR_MAP_SIZE(shrinker_nr_max);
> > > +     m_size = NR_MAX_TO_SHR_MAP_SIZE(new_nr_max);
> > > +     d_size = NR_MAX_TO_SHR_DEF_SIZE(new_nr_max);
> > > +     size = m_size + d_size;
> > > +     old_m_size = NR_MAX_TO_SHR_MAP_SIZE(shrinker_nr_max);
> > > +     old_d_size = NR_MAX_TO_SHR_DEF_SIZE(shrinker_nr_max);
> > > +     old_size = old_m_size + old_d_size;
> > >       if (size <= old_size)
> > >               goto out;
> >
> > It looks correct, but a bit bulky. Can we check that the new maximum
> > number of elements is larger than then the old one here?
> 
> Seems not to me. For example, we have shrinker_nr_max as 1, then a new
> shrinker is registered and the new_nr_max is 2, but actually the new
> size is equal to the old size.

I see.

> 
> We should be able to do:
> if (round_up(new_nr_max, BITS_PER_LONG) <= round_up(shrinker_nr_mx,
> BITS_PER_LONG))
> 
> Does it seem better?

Yes, I think so.

> 
> >
> > >
> > > @@ -286,9 +304,8 @@ static int expand_shrinker_info(int new_id)
> > >
> > >       memcg = mem_cgroup_iter(NULL, NULL, NULL);
> > >       do {
> > > -             if (mem_cgroup_is_root(memcg))
> > > -                     continue;
> > > -             ret = expand_one_shrinker_info(memcg, size, old_size);
> > > +             ret = expand_one_shrinker_info(memcg, m_size, d_size,
> > > +                                            old_m_size, old_d_size);
> >
> > Pass the old and the new numbers to expand_one_shrinker_info() and
> > have all size manipulation there?
> 
> With the above proposal we could move the size manipulation right
> before the memcg iter, we could save some cycles if we don't have to
> expand it.

I mostly dislike passing 4 arguments to expand_one_shrinker_info():
old_m_size, old_d_size, etc. But you're right, there is no good reason
to calculate them for each cgroup, if we can do it once. Can you, please,
rename arguments to map_size and defer_size (or something more obvious than
m and d on your taste)?

Thanks!

  reply	other threads:[~2021-02-10  2:23 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-09 17:46 [v7 PATCH 0/12] Make shrinker's nr_deferred memcg aware Yang Shi
2021-02-09 17:46 ` [v7 PATCH 01/12] mm: vmscan: use nid from shrink_control for tracepoint Yang Shi
2021-02-09 19:14   ` Shakeel Butt
2021-02-10 16:58     ` Yang Shi
2021-02-09 19:21   ` Roman Gushchin
2021-02-09 17:46 ` [v7 PATCH 02/12] mm: vmscan: consolidate shrinker_maps handling code Yang Shi
2021-02-09 20:27   ` Roman Gushchin
2021-02-10 14:19   ` Shakeel Butt
2021-02-09 17:46 ` [v7 PATCH 03/12] mm: vmscan: use shrinker_rwsem to protect shrinker_maps allocation Yang Shi
2021-02-09 20:33   ` Roman Gushchin
2021-02-09 23:28     ` Yang Shi
2021-02-09 17:46 ` [v7 PATCH 04/12] mm: vmscan: remove memcg_shrinker_map_size Yang Shi
2021-02-09 20:43   ` Roman Gushchin
2021-02-09 23:31     ` Yang Shi
2021-02-10 18:14     ` Vlastimil Babka
2021-02-09 17:46 ` [v7 PATCH 05/12] mm: memcontrol: rename shrinker_map to shrinker_info Yang Shi
2021-02-09 20:50   ` Roman Gushchin
2021-02-09 23:33     ` Yang Shi
2021-02-10  0:16       ` Roman Gushchin
2021-02-11 16:47       ` Kirill Tkhai
2021-02-11 17:29         ` Yang Shi
2021-02-09 17:46 ` [v7 PATCH 06/12] mm: vmscan: add shrinker_info_protected() helper Yang Shi
2021-02-10  0:22   ` Roman Gushchin
2021-02-10  1:07     ` Yang Shi
2021-02-10  1:29       ` Roman Gushchin
2021-02-10 12:12   ` Kirill Tkhai
2021-02-10 18:17   ` Vlastimil Babka
2021-02-12  6:54   ` [mm] bd741fb2ad: WARNING:suspicious_RCU_usage kernel test robot
2021-02-09 17:46 ` [v7 PATCH 07/12] mm: vmscan: use a new flag to indicate shrinker is registered Yang Shi
2021-02-10  0:39   ` Roman Gushchin
2021-02-10  1:12     ` Yang Shi
2021-02-10  1:34       ` Roman Gushchin
2021-02-10  1:55         ` Yang Shi
2021-02-10 18:45     ` Yang Shi
2021-02-10 18:23   ` Vlastimil Babka
2021-02-09 17:46 ` [v7 PATCH 08/12] mm: vmscan: add per memcg shrinker nr_deferred Yang Shi
2021-02-10  1:10   ` Roman Gushchin
2021-02-10  1:25     ` Yang Shi
2021-02-10  1:40       ` Roman Gushchin [this message]
2021-02-10  1:57         ` Yang Shi
2021-02-09 17:46 ` [v7 PATCH 09/12] mm: vmscan: use per memcg nr_deferred of shrinker Yang Shi
2021-02-10  1:27   ` Roman Gushchin
2021-02-10  1:52     ` Yang Shi
2021-02-10 14:36       ` Kirill Tkhai
2021-02-10 16:41         ` Yang Shi
2021-02-09 17:46 ` [v7 PATCH 10/12] mm: vmscan: don't need allocate shrinker->nr_deferred for memcg aware shrinkers Yang Shi
2021-02-10  1:23   ` Roman Gushchin
2021-02-09 17:46 ` [v7 PATCH 11/12] mm: memcontrol: reparent nr_deferred when memcg offline Yang Shi
2021-02-10  1:18   ` Roman Gushchin
2021-02-09 17:46 ` [v7 PATCH 12/12] mm: vmscan: shrink deferred objects proportional to priority Yang Shi
2021-02-11 13:10   ` Vlastimil Babka
2021-02-11 17:29     ` Yang Shi
2021-02-11 18:52       ` Vlastimil Babka
2021-02-11 19:15         ` 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=20210210014018.GR524633@carbon.DHCP.thefacebook.com \
    --to=guro@fb.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@fromorbit.com \
    --cc=hannes@cmpxchg.org \
    --cc=ktkhai@virtuozzo.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=shakeelb@google.com \
    --cc=shy828301@gmail.com \
    --cc=vbabka@suse.cz \
    /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;
as well as URLs for NNTP newsgroup(s).