Re: [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard()

From: albin_yang

Date: Fri Dec 04 2026 - 21:56:02 EST


Hi Youngjun,

On Fri, 4 Dec 2026 13:52:06 +0900, Youngjun Park wrote:
> Looking at your use case, there seems to be no discard device at all.
>
> What about counting SWP_PAGE_DISCARD devices like nr_rotate_swap, so we
> can skip the walk (and swap_lock) entirely when it's zero? Might be a
> bit local, but swap on discard devices isn't that common.
> It is just opinion for curios about your thought. no serious consideration plz :)

Interesting idea, but a counter would need to stay consistent with
swap_active_head across swapon/swapoff, and it only saves the last
lock/unlock pair now that the scan is down to 2 operations. Better to
leave that to the structural rework and keep this fix minimal.

> This inner check should be redundant now, right? Can we drop it?

Yes. All runtime writes to si->flags for an enabled device hold
swap_lock, so the loop-head check is stable. After the unlock those
writes only touch SWP_WRITEOK/SWP_HIBERNATION; the only runtime place
clearing SWP_PAGE_DISCARD is swapoff's p->flags = 0, which comes after
percpu_ref_kill(), so a live ref from get_swap_device_info() cannot
reach it. The inner check is therefore always true, and is itself a
read outside swap_lock. I'll drop it in v3.

Thanks,
Wei Yang