Re: [PATCH v5 03/19] mm/swap: cleanup and document swap device availability flag usage

From: Lian Wang

Date: Thu Nov 26 2026 - 07:30:42 EST


Hi Chris,

Thanks for the careful review. I kept the enable/disable helpers and
the call to add_to_avail_list() outside swap_lock from Kairui's
original patch, and later revised the locking around device lookup
and SWP_WRITEOK updates.

> Why is this change safe?

Both before and after this patch, setting or clearing SWP_WRITEOK
takes all three locks in order: swap_lock, si->lock, then
swap_avail_lock. A reader holding any one of them is serialized with
those updates. I'll make that distinction clear in the comment.

On the swapoff side, what changes is that we release si->lock earlier.
We still hold swap_lock while removing the device from the active list
and updating the global counters. The old code already dropped
swap_lock before wait_for_allocation() walked the cluster locks.

> what lock protects the avail_list, is that swap_avail_lock?

Yes. Previously, we set SWP_WRITEOK and added the device to the
available list under the same swap_avail_lock critical section. With
this patch, add_to_avail_list() takes that lock again and rechecks
SWP_WRITEOK and the off-list bit. If swapoff has already cleared
SWP_WRITEOK, the add is skipped. If the add happens first, swapoff
removes the entry.

> May I suggest splitting this patch into two?

Agreed. I'll keep the first patch to extracting the helpers with the
existing locking and operation order. In the second patch, I'll spell
out which operations move outside each lock and why that's safe.
I'll also keep the original if (swapoff) branch order.

Thanks,
Lian