From: Yosry Ahmed <yosryahmed@google.com>
To: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Minchan Kim <minchan@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCHv3] zsmalloc: allow only one active pool compaction context
Date: Tue, 18 Apr 2023 12:49:04 -0700 [thread overview]
Message-ID: <CAJD7tka8HxkFkXST_DRD6VCgCvvABZNt+aSWSjxOzBC3DXOUSQ@mail.gmail.com> (raw)
In-Reply-To: <20230418074639.1903197-1-senozhatsky@chromium.org>
On Tue, Apr 18, 2023 at 12:46 AM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> zsmalloc pool can be compacted concurrently by many contexts,
> e.g.
>
> cc1 handle_mm_fault()
> do_anonymous_page()
> __alloc_pages_slowpath()
> try_to_free_pages()
> do_try_to_free_pages(
> lru_gen_shrink_node()
> shrink_slab()
> do_shrink_slab()
> zs_shrinker_scan()
> zs_compact()
>
> Pool compaction is currently (basically) single-threaded as
> it is performed under pool->lock. Having multiple compaction
> threads results in unnecessary contention, as each thread
> competes for pool->lock. This, in turn, affects all zsmalloc
> operations such as zs_malloc(), zs_map_object(), zs_free(), etc.
>
> Introduce the pool->compaction_in_progress atomic variable,
> which ensures that only one compaction context can run at a
> time. This reduces overall pool->lock contention in (corner)
> cases when many contexts attempt to shrink zspool simultaneously.
>
> Fixes: c0547d0b6a4b ("zsmalloc: consolidate zs_pool's migrate_lock and size_class's locks")
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reviewed-by: Yosry Ahmed <yosryahmed@google.com>
> ---
> mm/zsmalloc.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index cc81dfba05a0..44ddaf5d601e 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -264,6 +264,7 @@ struct zs_pool {
> struct work_struct free_work;
> #endif
> spinlock_t lock;
> + atomic_t compaction_in_progress;
> };
>
> struct zspage {
> @@ -2274,6 +2275,15 @@ unsigned long zs_compact(struct zs_pool *pool)
> struct size_class *class;
> unsigned long pages_freed = 0;
>
> + /*
> + * Pool compaction is performed under pool->lock so it is basically
> + * single-threaded. Having more than one thread in __zs_compact()
> + * will increase pool->lock contention, which will impact other
> + * zsmalloc operations that need pool->lock.
> + */
> + if (atomic_xchg(&pool->compaction_in_progress, 1))
> + return 0;
> +
> for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
> class = pool->size_class[i];
> if (class->index != i)
> @@ -2281,6 +2291,7 @@ unsigned long zs_compact(struct zs_pool *pool)
> pages_freed += __zs_compact(pool, class);
> }
> atomic_long_add(pages_freed, &pool->stats.pages_compacted);
> + atomic_set(&pool->compaction_in_progress, 0);
>
> return pages_freed;
> }
> @@ -2388,6 +2399,7 @@ struct zs_pool *zs_create_pool(const char *name)
>
> init_deferred_free(pool);
> spin_lock_init(&pool->lock);
> + atomic_set(&pool->compaction_in_progress, 0);
>
> pool->name = kstrdup(name, GFP_KERNEL);
> if (!pool->name)
> --
> 2.40.0.634.g4ca3ef3211-goog
>
prev parent reply other threads:[~2023-04-18 19:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-18 7:46 Sergey Senozhatsky
2023-04-18 19:49 ` Yosry Ahmed [this message]
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=CAJD7tka8HxkFkXST_DRD6VCgCvvABZNt+aSWSjxOzBC3DXOUSQ@mail.gmail.com \
--to=yosryahmed@google.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=senozhatsky@chromium.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