Re: [PATCH] fs: order the i_readcount increment before the break_lease() check
From: Mateusz Guzik
Date: Sat Dec 05 2026 - 12:38:28 EST
On Sat, Dec 5, 2026 at 2:27 PM Zhenwen Liang <zhenwenliang@xxxxxxxxxx> wrote:
>
> A read-only open increments i_readcount and then calls break_lease(),
> which returns before its smp_mb() when the inode has no lock context;
> that check is an acquire load, which orders only later accesses, not the
> earlier increment. So on weakly ordered CPUs a racing
> generic_add_lease() can miss the new reader while the opener misses the
> new context: a write lease is granted although a read-only open is in
> progress. The lease side is already ordered by its smp_mb().
>
> The window is narrow: the first lease or lock on the inode, a read-only
> open on a built-in filesystem, and no barrier from LSM or fanotify hooks
> (not audited). Add smp_mb__after_atomic() after the increment.
>
> The problem was found, and this patch prepared, with the help of a
> formal analysis tool we are developing that uses AI assistance; I have
> reviewed the analysis and the patch.
>
> Fixes: 087adb4f0f91 ("vfs: dodge smp_mb in break_lease and break_deleg in the common case")
> Signed-off-by: Zhenwen Liang <zhenwenliang@xxxxxxxxxx>
> ---
> Compile-tested only (arm64, W=1). smp_mb__after_atomic() emits no code
> on x86, so only weakly ordered architectures pay for the barrier.
>
> fs/open.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/fs/open.c b/fs/open.c
> index bb484b0..f3a7596 100644
> --- a/fs/open.c
> +++ b/fs/open.c
> @@ -957,6 +957,8 @@ static int do_dentry_open(struct file *f,
>
> if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
> i_readcount_inc(inode);
> + /* Order before break_lease(); pairs with generic_add_lease() */
> + smp_mb__after_atomic();
> } else if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
> error = file_get_write_access(f);
> if (unlikely(error))
Semantically the patch looks good.
I think generic_add_lease() wants a comment denoting what it pairs
with in the open codepath.