Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hungyu Lin" <dennylin0707@gmail.com>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH v2] filelock: don't hand out a delegation that is being recalled
Date: Fri, 04 Dec 2026 03:14:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-249027@kernel.org> (raw)
In-Reply-To: <20261204024948.26010-1-dennylin0707@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] filelock: __break_lease() skips delegation block when
  FL_UNLOCK_PENDING is set
- [Medium] filelock: di_waited in struct delegated_inode triggers retry
  across inodes
- [Medium] filelock: __break_lease() and try_break_deleg() miss open(3)
  and nfsd
- [Medium] filelock: __break_lease() rescans all leases on every wakeup
  (O(N^2))
--

commit df9b5ca846e82763521dadf099ffefce3e79aadd
Author: Hungyu Lin <dennylin0707@gmail.com>

filelock: don't hand out a delegation that is being recalled

This commit changes __break_lease() to loop back and break leases added while
waiting, and blocks new userland F_SETDELEG delegations on the inode for
twice lease_break_time when a breaker encounters a conflicting delegation on
retry.

> diff --git a/fs/locks.c b/fs/locks.c
> index 6e4ff7fcec053..2c7af1b7b9c63 100644
> --- a/fs/locks.c
> +++ b/fs/locks.c

[ ... ]

> @@ -1708,6 +1710,7 @@ int __break_lease(struct inode *inode, unsigned int flags)
>  	if (!first_visible_lease(inode, new_fl, flags))
>  		goto out;
>  
> +restart:
>  	break_time = 0;
>  	if (lease_break_time > 0) {
>  		break_time = jiffies + lease_break_time * HZ;
> @@ -1731,6 +1734,9 @@ int __break_lease(struct inode *inode, unsigned int flags)
>  			fl->c.flc_flags |= FL_DOWNGRADE_PENDING;
>  			fl->fl_downgrade_time = break_time;
>  		}
> +		if (retry && type == FL_DELEG && lease_break_time > 0)
> +			ctx->flc_deleg_blocked_until = get_jiffies_64() +
> +				2 * (u64)lease_break_time * HZ;
>  		if (fl->fl_lmops->lm_break(fl))
>  			locks_delete_lock_ctx(&fl->c, &dispose);
>  	}

[Severity: High]
Can an unprivileged file owner bypass ctx->flc_deleg_blocked_until because it
is set after the FL_UNLOCK_PENDING and lease_breaking() continue statements?

CPU0 (victim)                     CPU1 (delegation holder)
try_break_deleg() -> -EWOULDBLOCK
break_deleg_wait() -> sleeps
                                  fcntl(F_SETDELEG, F_UNLCK)
                                  fcntl(F_SETDELEG, F_RDLCK) [D2]
                                  open(O_WRONLY | O_NONBLOCK)
                                    sets FL_UNLOCK_PENDING on D2
wakes, retry=true, goto restart
  FL_UNLOCK_PENDING -> continue
  flc_deleg_blocked_until stays 0  <-- sleeps on D2 again

Because open(O_WRONLY | O_NONBLOCK) enters do_dentry_open() -> break_lease()
with retry == false, it marks the freshly re-acquired delegation D2 with
FL_UNLOCK_PENDING without arming flc_deleg_blocked_until and returns
-EWOULDBLOCK immediately.  When the victim wakes or retries with retry ==
true, the loop hits continue and skips setting flc_deleg_blocked_until,
allowing the holder to hang unlink(), rename(), chmod(), or chown() until
interrupted.

Separately, break_deleg_wait() calls break_deleg(inode, 0) without passing
LEASE_BREAK_RETRY when di->di_waited is already true, so if D2 is replaced
before break_deleg_wait() runs, retry starts false again.  Updating
flc_deleg_blocked_until before the continue skips (or after
first_visible_lease() succeeds) and forwarding LEASE_BREAK_RETRY from
break_deleg_wait() would close both gaps.

[Severity: Medium]
Should this check fl->c.flc_flags & FL_DELEG rather than type == FL_DELEG?

break_lease() passes LEASE_BREAK_LEASE (type == FL_LEASE), so it never sets
flc_deleg_blocked_until when breaking a userland delegation.  An open() with
O_ACCMODE == 3 (O_WRONLY | O_RDWR) leaves f->f_mode == 0, so do_dentry_open()
increments neither i_readcount nor i_writecount before calling break_lease(),
letting the holder unlock on SIGIO and re-acquire the delegation indefinitely
without hitting lease_open_conflict() and hanging open(path, 3).

Separately, try_break_deleg() only sets LEASE_BREAK_RETRY when di &&
di->di_waited is true, so nfsd callers that pass a NULL delegated_inode (such
as nfsd_unlink() -> vfs_unlink(..., NULL)) never arm flc_deleg_blocked_until
across retries.  A local file owner can repeatedly unlock and re-acquire an
F_SETDELEG delegation on recall and starve remote NFS clients with
NFS4ERR_DELAY/NFS3ERR_JUKEBOX.

> @@ -1745,7 +1751,6 @@ int __break_lease(struct inode *inode, unsigned int flags)
>  		goto out;
>  	}
>  
> -restart:
>  	break_time = fl->fl_break_time;
>  	if (break_time != 0) {
>  		if (time_after(jiffies, break_time)) {
> @@ -1776,9 +1781,10 @@ int __break_lease(struct inode *inode, unsigned int flags)
>  		 */
>  		if (error == 0)
>  			time_out_leases(inode, &dispose);
> -		fl = first_visible_lease(inode, new_fl, flags);
> -		if (fl)
> +		if (first_visible_lease(inode, new_fl, flags)) {
> +			retry = true;
>  			goto restart;
> +		}
>  		error = 0;
>  	}

[Severity: Medium]
Does jumping back to restart above list_for_each_entry_safe() turn
multi-holder recalls into O(N^2) scanning under ctx->flc_lock?

When N shared read leases or delegations are returned in order, each wakeup
discards the first_visible_lease() result, rescans all remaining entries on
ctx->flc_lease under spin_lock(&ctx->flc_lock) (calling leases_conflict() on
each before skipping FL_UNLOCK_PENDING), and calls first_visible_lease() a
second time.  For NFSv4 delegations, where nfsd_break_deleg_cb() sets
fl->fl_break_time = 0 and waiters wake every jiffy, this also adds a second
full-list scan on top of time_out_leases() every tick.

Tracking insertions with a generation counter in struct file_lock_context
(incremented in generic_add_lease()) would let __break_lease() rescan only
when a new lease was actually inserted while waiting.

[ ... ]

> diff --git a/include/linux/filelock.h b/include/linux/filelock.h
> index e909db319067f..6b5ec34c9b6e6 100644
> --- a/include/linux/filelock.h
> +++ b/include/linux/filelock.h

[ ... ]

> @@ -541,7 +544,10 @@ static inline int try_break_deleg(struct inode *inode, unsigned int flags,
>  {
>  	int ret;
>  
> -	ret = break_deleg(inode, flags | LEASE_BREAK_NONBLOCK);
> +	flags |= LEASE_BREAK_NONBLOCK;
> +	if (di && di->di_waited)
> +		flags |= LEASE_BREAK_RETRY;
> +	ret = break_deleg(inode, flags);
>  	if (ret == -EWOULDBLOCK && di) {
>  		di->di_inode = inode;
>  		ihold(inode);
> @@ -561,6 +567,7 @@ static inline int break_deleg_wait(struct delegated_inode *di)
>  	iput(inode);
>  	deactivate_super(sb);
>  	di->di_inode = NULL;
> +	di->di_waited = true;
>  	return ret;
>  }

[Severity: Medium]
Can di_waited apply LEASE_BREAK_RETRY to an unrelated inode in multi-inode
operations?

vfs_rename(), vfs_link(), and vfs_unlink() reuse a single struct
delegated_inode across distinct inodes (for example old_dir, new_dir, source,
and target in vfs_rename()).  Once break_deleg_wait() waits on the first
inode and sets di->di_waited = true, the very first try_break_deleg() on the
next inode passes LEASE_BREAK_RETRY and immediately arms
flc_deleg_blocked_until on it.

Even when all holders return their delegations promptly without re-acquiring,
subsequent F_SETDELEG requests on that surviving file or directory then fail
with -EAGAIN for 2 * lease_break_time (90 seconds by default).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204024948.26010-1-dennylin0707@gmail.com?part=1

      reply	other threads:[~2026-12-04  3:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  2:49 [PATCH v2] filelock: don't hand out a delegation that is being recalled Hungyu Lin
2026-12-04  3:14 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-249027@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dennylin0707@gmail.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox