Re: [PATCH v5 10/19] mm/swap: mask the off-list bit when checking whether swap is full
From: Chris Li
Date: Thu Nov 26 2026 - 15:09:40 EST
On Mon, Nov 23, 2026 at 6:18 AM Lian Wang <lianux.mm@xxxxxxxxx> wrote:
>
> add_to_avail_list() clears SWAP_USAGE_OFFLIST_BIT with an atomic
> fetch-and. The returned value is the old count, including the bit.
> If a freed slot has already been allocated again, comparing that
> value directly with si->pages misses a full device.
>
> Mask the state bit for the comparison. Keep the cmpxchg on the live
> counter so that a concurrent free can still make the device
> available. This also applies to the existing plist allocator.
>
> Fixes: b228386cf237 ("mm, swap: clean up plist removal and adding")
> Link: https://lore.kernel.org/all/f53b388e1bcf6840457fe5041e989a3c0d0cc4df@xxxxxxxxx/
> Signed-off-by: Lian Wang <lianux.mm@xxxxxxxxx>
Acked-by: Chris Li <chrisl@xxxxxxxxxx>
Chris
> ---
> mm/swapfile.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 70913402a265..1ef5f1ead656 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1347,7 +1347,7 @@ static void add_to_avail_list(struct swap_info_struct *si)
> * that updater happen to be here, just skip adding.
> */
> pages = si->pages;
> - if (val == pages) {
> + if ((val & SWAP_USAGE_COUNTER_MASK) == pages) {
> /* Just like the cmpxchg in del_from_avail_list */
> if (atomic_long_try_cmpxchg(&si->inuse_pages, &pages,
> pages | SWAP_USAGE_OFFLIST_BIT))