Re: [PATCH 2/3] mm/zram: handle swap read/write via swap_ops

From: Jianyue Wu

Date: Mon Jun 15 2026 - 09:24:48 EST


On Mon, Jun 15, 2026 at 2:39 PM YoungJun Park <youngjun.park@xxxxxxx> wrote:
>
> On Sun, Jun 14, 2026 at 11:35:30PM +0800, Jianyue Wu wrote:
>
> Hello!
>
> > +static void zram_swap_submit_read(struct swap_io_ctx *ctx)
> > +{
> > + struct zram *zram = ctx->sis->bdev->bd_disk->private_data;
>
> A passing thought. accessing `zram` here is too indirect. We might
> need a `private_data` in the swap device struct someday?
>
> (And If there is a real value like some swap-side only private data really needed.)
>
> > + struct swap_iocb *sio = ctx->sio;
> > + int nr = swap_iocb_nr_folios(sio);
> > + bool failed = false;
> > + int i, j;
> > + /*
> > + * read_from_zspool() and mark_slot_accessed() must run
> > + * under the same slot_lock. zram_read_page() unlocks
> > + * before returning, which leaves a window where
> > + * writeback can pick an idle slot we just read.
> > + */
>
> Regarding the comment about the "window" where writeback can pick an
> idle slot. I think this reasoning is a bit of a gray area. Writeback
> could just as easily pick the slot right before entering this routine,
> so the race condition seems fundamentally the same.
>
> Isn't the actual justification here to separate the non-backend logic
> and ensure mark_slot_accessed() is called under the lock, given that
> zram_read_page() can call the backend device?
>
> If the "window" mentioned in the comment is indeed a valid issue, then
> zram_read_page() has the exact same problem and needs to be fixed as
> well?
>
> If not, IMHO I suggest revising or removing this comment to clarify
> the true(?) intention. :)
>
> > + slot_lock(zram, idx);
> > + ret = read_from_zspool(zram, page, idx);
> > + if (!ret)
> > + mark_slot_accessed(zram, idx);
> > + slot_unlock(zram, idx);
>

Hello Youngjun,

Agree. Walking ctx->sis->bdev->bd_disk->private_data
from every swap_ops callback is too indirect. I will add an opaque
private_data field to struct swap_info_struct, set it from
->swap_activate() when the swap area is set up, and clear it on
swapoff. The zram callbacks will then use ctx->sis->private_data directly.

You are right. The writeback "window" reasoning was overstated.
Writeback could already have picked the slot before we enter the swap
read path, we have ZRAM_PP_SLOT to ensure it.
0. // condition 1 write pick the slot before the lock.
1. lock → read_from_zspool → unlock
2. // condition 2 write pick the slot inside the lock.
3. lock → mark_slot_accessed() → unlock // clear ZRAM_IDLE and ZRAM_PP_SLOT flag

I think simply removing this comment is good.

Thanks,
Jianyue