From: Uros Bizjak <ubizjak@gmail.com>
To: Sergey Senozhatsky <senozhatsky@chromium.org>,
Andrew Morton <akpm@linux-foundation.org>,
Minchan Kim <minchan@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Yosry Ahmed <yosry.ahmed@linux.dev>,
Nhat Pham <nphamcs@gmail.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 2/6] zsmalloc: make zspage lock preemptible
Date: Mon, 27 Jan 2025 21:23:53 +0100 [thread overview]
Message-ID: <ca793c5a-5ba8-2fb3-a51d-8b028f5e3c22@gmail.com> (raw)
In-Reply-To: <20250127080254.1302026-3-senozhatsky@chromium.org>
On 27. 01. 25 08:59, Sergey Senozhatsky wrote:
> Switch over from rwlock_t to a atomic_t variable that takes
> negative value when the page is under migration, or positive
> values when the page is used by zsmalloc users (object map,
> etc.) Using a rwsem per-zspage is a little too memory heavy,
> a simple atomic_t should suffice, after all we only need to
> mark zspage as either used-for-write or used-for-read. This
> is needed to make zsmalloc preemtible in the future.
>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> ---
> mm/zsmalloc.c | 112 +++++++++++++++++++++++++++++---------------------
> 1 file changed, 66 insertions(+), 46 deletions(-)
>
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index 817626a351f8..28a75bfbeaa6 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -257,6 +257,9 @@ static inline void free_zpdesc(struct zpdesc *zpdesc)
> __free_page(page);
> }
>
> +#define ZS_PAGE_UNLOCKED 0
> +#define ZS_PAGE_WRLOCKED -1
> +
> struct zspage {
> struct {
> unsigned int huge:HUGE_BITS;
> @@ -269,7 +272,7 @@ struct zspage {
> struct zpdesc *first_zpdesc;
> struct list_head list; /* fullness list */
> struct zs_pool *pool;
> - rwlock_t lock;
> + atomic_t lock;
> };
>
> struct mapping_area {
> @@ -290,11 +293,53 @@ static bool ZsHugePage(struct zspage *zspage)
> return zspage->huge;
> }
>
> -static void migrate_lock_init(struct zspage *zspage);
> -static void migrate_read_lock(struct zspage *zspage);
> -static void migrate_read_unlock(struct zspage *zspage);
> -static void migrate_write_lock(struct zspage *zspage);
> -static void migrate_write_unlock(struct zspage *zspage);
> +static void zspage_lock_init(struct zspage *zspage)
> +{
> + atomic_set(&zspage->lock, ZS_PAGE_UNLOCKED);
> +}
> +
> +static void zspage_read_lock(struct zspage *zspage)
> +{
> + atomic_t *lock = &zspage->lock;
> + int old;
> +
> + while (1) {
> + old = atomic_read(lock);
> + if (old == ZS_PAGE_WRLOCKED) {
> + cpu_relax();
> + continue;
> + }
> +
> + if (atomic_cmpxchg(lock, old, old + 1) == old)
> + return;
You can use atomic_try_cmpxchg() here:
if (atomic_try_cmpxchg(lock, &old, old + 1))
return;
> +
> + cpu_relax();
> + }
> +}
> +
> +static void zspage_read_unlock(struct zspage *zspage)
> +{
> + atomic_dec(&zspage->lock);
> +}
> +
> +static void zspage_write_lock(struct zspage *zspage)
> +{
> + atomic_t *lock = &zspage->lock;
> + int old;
> +
> + while (1) {
> + old = atomic_cmpxchg(lock, ZS_PAGE_UNLOCKED, ZS_PAGE_WRLOCKED);
> + if (old == ZS_PAGE_UNLOCKED)
> + return;
Also, the above code can be rewritten as:
while (1) {
old = ZS_PAGE_UNLOCKED;
if (atomic_try_cmpxchg (lock, &old, ZS_PAGE_WRLOCKED))
return;
> +
> + cpu_relax();
> + }
> +}
The above change will result in a slightly better generated asm.
Uros.
next prev parent reply other threads:[~2025-01-27 20:24 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-27 7:59 [RFC PATCH 0/6] zsmalloc: make zsmalloc preemptible Sergey Senozhatsky
2025-01-27 7:59 ` [RFC PATCH 1/6] zram: deffer slot free notification Sergey Senozhatsky
2025-01-27 7:59 ` [RFC PATCH 2/6] zsmalloc: make zspage lock preemptible Sergey Senozhatsky
2025-01-27 20:23 ` Uros Bizjak [this message]
2025-01-28 0:29 ` Sergey Senozhatsky
2025-01-27 7:59 ` [RFC PATCH 3/6] zsmalloc: convert to sleepable pool lock Sergey Senozhatsky
2025-01-27 7:59 ` [RFC PATCH 4/6] zsmalloc: make class lock sleepable Sergey Senozhatsky
2025-01-27 7:59 ` [RFC PATCH 5/6] zsmalloc: introduce handle mapping API Sergey Senozhatsky
2025-01-27 21:26 ` Yosry Ahmed
2025-01-28 0:37 ` Sergey Senozhatsky
2025-01-28 0:49 ` Yosry Ahmed
2025-01-28 1:13 ` Sergey Senozhatsky
2025-01-27 21:58 ` Yosry Ahmed
2025-01-28 0:59 ` Sergey Senozhatsky
2025-01-28 1:36 ` Yosry Ahmed
2025-01-28 5:29 ` Sergey Senozhatsky
2025-01-28 9:38 ` Sergey Senozhatsky
2025-01-28 17:21 ` Yosry Ahmed
2025-01-29 3:32 ` Sergey Senozhatsky
2025-01-28 11:10 ` Sergey Senozhatsky
2025-01-28 17:22 ` Yosry Ahmed
2025-01-28 23:01 ` Sergey Senozhatsky
2025-01-29 5:40 ` Sergey Senozhatsky
2025-01-27 7:59 ` [RFC PATCH 6/6] zram: switch over to zshandle " Sergey Senozhatsky
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=ca793c5a-5ba8-2fb3-a51d-8b028f5e3c22@gmail.com \
--to=ubizjak@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=senozhatsky@chromium.org \
--cc=yosry.ahmed@linux.dev \
/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