Re: [PATCH v5 04/19] mm/swap: introduce swap device iteration helper

From: Lian Wang

Date: Sat Nov 28 2026 - 02:37:39 EST


On Thu, 26 Nov 2026 09:26:29 -0800 Chris Li <chrisl@xxxxxxxxxx> wrote:

> Nitpick: Suggest have a internal version of "int _i", then sync to
> "int *i" at function exit.

Thanks for the ack. The macro passes its own local index, but I agree
a local int in __swap_iter() makes it clear that the bounds check and
the lookup use the same value. I'll write back the next index on exit.
I'd keep it an int for now; this iterator has no other state to carry.

> You don't need this WARN ON. When i < nr_swapfile, the swap_info[i]
> array can never the NULL. If you are paranoid you can continue if "si"
> is NULL.

Agreed. The pointer is published before nr_swapfiles is incremented,
and the descriptor is kept for reuse. I'll drop the warning and use
the _dev names you suggested, with a comment that the unfiltered form
also visits unused descriptors.

> I think for_each_swap_info() requested flags as SWP_USED |
> SWP_WRITEOK. Is this intent here to catch the race that SWP_WRITEOK
> get dropped after the for_each_swap_info()?

No. It's for_each_avail_swap() that passes SWP_USED | SWP_WRITEOK.
for_each_swap_info() passes zero, so the flag check in __swap_iter()
short-circuits without reading si->flags. In si_swapinfo() the
READ_ONCE() in the loop body is the only flags load. It counts usage
on a device that is still marked used but is no longer writable; it
doesn't depend on observing a change between two reads.

Thanks,
Lian