Re: [PATCH v5 06/19] mm/swap: serialize swap publication before removing swapon mutex
From: Lian Wang
Date: Sat Nov 28 2026 - 02:45:26 EST
On Thu, 26 Nov 2026 10:17:44 -0800 Chris Li <chrisl@xxxxxxxxxx> wrote:
> The current commit message describes what changed. This is the most
> critical part of the locking change in this series; it deserves a
> better description of why this new locking protocol is safe to use.
Before this patch, /proc/swaps held swapon_mutex from ->start() to
->stop(). On swapon, the mutex covered publishing si->swap_file, while
swap_device_enable() separately took the write side of swapon_rwsem.
With this patch, swapon sets swap_file, resurrects si->users and makes
the device writable, in that order, in one writer section. /proc/swaps
holds the read side across the walk and skips entries with no swap_file.
On swapoff, the writer clears swap_file before the file is closed: a
reader that saw the file finishes before the writer can clear it, and
a later reader skips the entry.
That protects the proc reader, not all users or the whole teardown.
Users that don't take the rwsem still rely on the live reference;
swapoff kills it, waits for RCU and drains the references before
dismantling their state. I'll spell out those two guarantees in the
commit message.
> The function name can reflect that it is locked. Maybe
> __swap_device_enable_locked()
Great suggestion. I'll rename the helper to __swap_device_enable_locked()
in the next revision.
Thanks,
Lian