From: Xueyuan Chen <xueyuan.chen21@gmail.com>
To: haowenchao22@gmail.com
Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev,
axboe@kernel.dk, hannes@cmpxchg.org, minchan@kernel.org,
nphamcs@gmail.com, senozhatsky@chromium.org, yosry@kernel.org,
linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, baohua@kernel.org, xueyuan.chen21@gmail.com,
haowenchao@xiaomi.com
Subject: Re: [RFC PATCH v2 0/4] mm/zsmalloc: reduce zs_free() latency on swap release path
Date: Sun, 26 Apr 2026 16:50:17 +0800 [thread overview]
Message-ID: <20260426085017.166935-1-xueyuan.chen21@gmail.com> (raw)
In-Reply-To: <CAOptpSPs-1UrEa8AHg19e590=SiV6bpnex7gCbif8=aY7BtpuA@mail.gmail.com>
On Sun, Apr 26, 2026 at 12:13:02PM +0800, Wenchao Hao wrote:
[...]
>2. Per-cpu deferred free with lockless buffer swap
>
>Defer zs_free() to per-cpu dynamically-allocated buffers (~2048 entries).
>Enqueue: one array write + WRITE_ONCE under preempt_disable — no lock,
>no atomic. When buffers full, schedule a drain worker; overflow falls back
>to sync zs_free().
>
>Drain: allocate a fresh buffer, swap it in, reset count. Since
>the producer stops writing at count==SIZE, the handoff is
>race-free without any lock.
>
>Pseudo-code:
>
> /* enqueue - hot path */
> def = get_cpu_ptr(pool->deferred);
> if (def->count < SIZE) {
> def->handles[def->count] = handle;
> WRITE_ONCE(def->count, def->count + 1);
> if (def->count == SIZE)
> schedule_work(&pool->drain_work);
> } else {
> zs_free(pool, handle); /* fallback */
> }
> put_cpu_ptr(pool->deferred);
>
> /* drain - worker */
> for_each_possible_cpu(cpu) {
> def = per_cpu_ptr(pool->deferred, cpu);
> if (def->count < SIZE)
> continue;
> new_buf = kvmalloc_array(SIZE, sizeof(long));
> old_buf = def->handles;
> old_count = def->count;
> def->handles = new_buf;
> WRITE_ONCE(def->count, 0);
> /* now drain old_buf[0..old_count-1] */
> ...
> kvfree(old_buf);
> }
>
Hi Wenchao,
I suspect there is a memory ordering issue here:
def->handles = new_buf;
WRITE_ONCE(def->count, 0);
Since there are no explicit memory barriers, we cannot guarantee the
order of these stores. If def->count is cleared to 0 first, an enqueue
might end up operating on the old_buf.
This race condition is more likely to be triggered when the size is
smaller. Perhaps we should consider using smp_store_release() to enforce
the ordering?
Thanks
Xueyuan
next prev parent reply other threads:[~2026-04-26 8:50 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-21 12:16 [RFC PATCH v2 0/4] mm/zsmalloc: reduce zs_free() latency on swap release path Wenchao Hao
2026-04-21 12:16 ` [RFC PATCH v2 1/4] mm:zsmalloc: drop class lock before freeing zspage Wenchao Hao
2026-04-21 12:16 ` [RFC PATCH v2 2/4] mm/zsmalloc: introduce zs_free_deferred() for async handle freeing Wenchao Hao
2026-04-21 19:46 ` Nhat Pham
2026-04-21 21:42 ` Barry Song
2026-04-23 16:40 ` Nhat Pham
2026-04-21 12:16 ` [RFC PATCH v2 3/4] zram: defer zs_free() in swap slot free notification path Wenchao Hao
2026-04-21 12:16 ` [RFC PATCH v2 4/4] mm/zswap: defer zs_free() in zswap_invalidate() path Wenchao Hao
2026-04-21 17:03 ` Nhat Pham
2026-04-21 15:54 ` [RFC PATCH v2 0/4] mm/zsmalloc: reduce zs_free() latency on swap release path Nhat Pham
2026-04-21 17:17 ` Kairui Song
2026-04-21 18:07 ` Nhat Pham
2026-04-21 18:25 ` Nhat Pham
2026-04-22 0:34 ` Xueyuan Chen
2026-04-26 4:13 ` Wenchao Hao
2026-04-26 8:50 ` Xueyuan Chen [this message]
2026-04-27 3:10 ` Wenchao Hao
2026-04-27 18:17 ` Yosry Ahmed
2026-04-28 13:51 ` Wenchao Hao
2026-04-28 13:55 ` Wenchao Hao
2026-04-29 22:44 ` Yosry Ahmed
2026-04-30 7:38 ` Wenchao Hao
2026-04-30 8:00 ` Kairui Song
2026-04-30 15:15 ` Wenchao Hao
2026-05-02 7:21 ` Nhat Pham
2026-05-06 13:55 ` Wenchao Hao
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=20260426085017.166935-1-xueyuan.chen21@gmail.com \
--to=xueyuan.chen21@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=baohua@kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=hannes@cmpxchg.org \
--cc=haowenchao22@gmail.com \
--cc=haowenchao@xiaomi.com \
--cc=linux-block@vger.kernel.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@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