mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hungyu Lin <dennylin0707@gmail.com>
To: jlayton@kernel.org
Cc: cel@kernel.org, alex.aring@gmail.com, viro@zeniv.linux.org.uk,
	brauner@kernel.org, jack@suse.cz, dai.ngo@oracle.com,
	mjguzik@gmail.com, zhaogongyi@bytedance.com,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	dennylin0707@gmail.com
Subject: [PATCH] filelock: don't hand out a delegation that is being recalled
Date: Mon, 16 Nov 2026 06:15:02 +0000	[thread overview]
Message-ID: <20261116061502.44674-1-dennylin0707@gmail.com> (raw)

An operation that finds a conflicting delegation gets -EWOULDBLOCK from
try_break_deleg(), drops its locks, waits in break_deleg_wait() and then
retries. Nothing stops a new delegation from being set on the inode in
the meantime.

With F_SETDELEG the owner of a file can do that from userspace: remove
the delegation when it is recalled and request it again right away. The
waiter in __break_lease() then finds a delegation that was never asked
to be returned. It has no break time, so it never times out and its
holder is not signalled again, and the waiter keeps waiting for it.

As a result unlink(), rename(), chmod() and chown() of the file hang
until they are interrupted, no matter who calls them and what
lease-break-time is set to. A delegation on a directory does the same
to a create in that directory.

Record in the lock context that a delegation is being recalled and
refuse to hand out a new userland delegation with -EAGAIN while that is
the case. The block is lifted as soon as an operation gets past its
delegation check with nothing left to break, so a delegation can be
requested again once the conflicting operation went through. If the
breaker gives up instead, the block expires after twice the lease break
time. nfsd already keeps track of recalled delegations by itself and is
left alone.

Fixes: 1602bad16d7d ("vfs: expose delegation support to userland")
Cc: stable@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
 fs/locks.c               | 24 +++++++++++++++++++++++-
 include/linux/filelock.h |  5 +++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/fs/locks.c b/fs/locks.c
index 6e4ff7fcec05..f7d1a376da5c 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -190,6 +190,7 @@ locks_get_lock_context(struct inode *inode, int type)
 	INIT_LIST_HEAD(&ctx->flc_flock);
 	INIT_LIST_HEAD(&ctx->flc_posix);
 	INIT_LIST_HEAD(&ctx->flc_lease);
+	ctx->flc_deleg_blocked_until = 0;
 
 	/*
 	 * Assign the pointer if it's not already assigned. If it is, then
@@ -1705,8 +1706,11 @@ int __break_lease(struct inode *inode, unsigned int flags)
 
 	time_out_leases(inode, &dispose);
 
-	if (!first_visible_lease(inode, new_fl, flags))
+	if (!first_visible_lease(inode, new_fl, flags)) {
+		if (type == FL_DELEG && (flags & LEASE_BREAK_NONBLOCK))
+			ctx->flc_deleg_blocked_until = 0;
 		goto out;
+	}
 
 	break_time = 0;
 	if (lease_break_time > 0) {
@@ -1739,6 +1743,10 @@ int __break_lease(struct inode *inode, unsigned int flags)
 	if (!fl)
 		goto out;
 
+	if (type == FL_DELEG && lease_break_time > 0)
+		ctx->flc_deleg_blocked_until = get_jiffies_64() +
+					       2 * (u64)lease_break_time * HZ;
+
 	if (flags & LEASE_BREAK_NONBLOCK) {
 		trace_break_lease_noblock(inode, new_fl);
 		error = -EWOULDBLOCK;
@@ -1791,6 +1799,15 @@ int __break_lease(struct inode *inode, unsigned int flags)
 }
 EXPORT_SYMBOL(__break_lease);
 
+void locks_unblock_deleg(struct file_lock_context *ctx)
+{
+	spin_lock(&ctx->flc_lock);
+	if (list_empty(&ctx->flc_lease))
+		ctx->flc_deleg_blocked_until = 0;
+	spin_unlock(&ctx->flc_lock);
+}
+EXPORT_SYMBOL(locks_unblock_deleg);
+
 /**
  *	lease_get_mtime - update modified time of an inode with exclusive lease
  *	@inode: the inode
@@ -1968,6 +1985,11 @@ generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **pr
 	if (!leases_enable)
 		goto out;
 
+	error = -EAGAIN;
+	if (is_deleg && lease->fl_lmops == &lease_manager_ops &&
+	    time_before64(get_jiffies_64(), ctx->flc_deleg_blocked_until))
+		goto out;
+
 	locks_insert_lock_ctx(&lease->c, &ctx->flc_lease);
 	/*
 	 * The check in break_lease() is lockless. It's possible for another
diff --git a/include/linux/filelock.h b/include/linux/filelock.h
index e909db319067..1a48c5550fa9 100644
--- a/include/linux/filelock.h
+++ b/include/linux/filelock.h
@@ -158,6 +158,7 @@ struct file_lock_context {
 	struct list_head	flc_flock;
 	struct list_head	flc_posix;
 	struct list_head	flc_lease;
+	u64			flc_deleg_blocked_until;
 };
 
 #ifdef CONFIG_FILE_LOCKING
@@ -229,6 +230,7 @@ void locks_init_lease(struct file_lease *);
 void locks_free_lease(struct file_lease *fl);
 struct file_lease *locks_alloc_lease(void);
 int __break_lease(struct inode *inode, unsigned int flags);
+void locks_unblock_deleg(struct file_lock_context *ctx);
 void lease_get_mtime(struct inode *, struct timespec64 *time);
 int generic_setlease(struct file *, int, struct file_lease **, void **priv);
 int kernel_setlease(struct file *, int, struct file_lease **, void **);
@@ -510,6 +512,9 @@ static inline int break_deleg(struct inode *inode, unsigned int flags)
 		flags |= LEASE_BREAK_DELEG;
 		return __break_lease(inode, flags);
 	}
+	if ((flags & LEASE_BREAK_NONBLOCK) &&
+	    READ_ONCE(flctx->flc_deleg_blocked_until))
+		locks_unblock_deleg(flctx);
 	return 0;
 }
 
-- 
2.43.0

                 reply	other threads:[~2026-11-16  6:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261116061502.44674-1-dennylin0707@gmail.com \
    --to=dennylin0707@gmail.com \
    --cc=alex.aring@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cel@kernel.org \
    --cc=dai.ngo@oracle.com \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mjguzik@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zhaogongyi@bytedance.com \
    /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

all inboxes | Powered by JetHome®