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. When __break_lease() wakes up and still finds a conflicting lease, go back and break the ones that were set while it was waiting, the same way as the ones it found on entry. That alone is not enough, because the holder can keep returning the delegation and requesting it again. So when a breaker that has already waited once finds a new delegation, either on waking up or when its caller retries after break_deleg_wait(), stop handing out userland delegations on that inode for twice the lease break time. That leaves the breaker enough time to get the last one back and to go through. As long as no delegation is set while one is being recalled, nothing changes: a delegation can be requested again as soon as the conflicting operation went through. 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 --- Changes in v2: - v1 lifted the block as soon as any non-blocking delegation break found nothing left to break. The holder could cause that itself, for example with futimens() on the file, and take the delegation again. The block is no longer lifted early. Instead it is only set once a breaker has to wait a second time. - Break the leases that are set while __break_lease() is waiting. v1: https://lore.kernel.org/all/20261116061502.44674-1-dennylin0707@gmail.com/ fs/locks.c | 21 ++++++++++++++++++--- include/linux/filelock.h | 9 ++++++++- include/trace/events/filelock.h | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/fs/locks.c b/fs/locks.c index 6e4ff7fcec05..2c7af1b7b9c6 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 @@ -1681,6 +1682,7 @@ int __break_lease(struct inode *inode, unsigned int flags) unsigned int type; LIST_HEAD(dispose); bool want_write = !(flags & LEASE_BREAK_OPEN_RDONLY); + bool retry = flags & LEASE_BREAK_RETRY; int error = 0; trace_break_lease(inode, flags); @@ -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); } @@ -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; } out: @@ -1968,6 +1974,15 @@ generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **pr if (!leases_enable) goto out; + /* + * Don't hand out a new delegation while __break_lease() is having + * trouble getting the previous ones returned. + */ + 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..6b5ec34c9b6e 100644 --- a/include/linux/filelock.h +++ b/include/linux/filelock.h @@ -37,6 +37,7 @@ #define LEASE_BREAK_DIR_CREATE BIT(5) // dir deleg create event #define LEASE_BREAK_DIR_DELETE BIT(6) // dir deleg delete event #define LEASE_BREAK_DIR_RENAME BIT(7) // dir deleg rename event +#define LEASE_BREAK_RETRY BIT(8) // retry after waiting for a break struct file_lock; struct file_lease; @@ -158,6 +159,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 @@ -515,6 +517,7 @@ static inline int break_deleg(struct inode *inode, unsigned int flags) struct delegated_inode { struct inode *di_inode; + bool di_waited; }; static inline bool is_delegated(struct delegated_inode *di) @@ -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; } diff --git a/include/trace/events/filelock.h b/include/trace/events/filelock.h index f11284be01ea..02b0f0687afc 100644 --- a/include/trace/events/filelock.h +++ b/include/trace/events/filelock.h @@ -129,7 +129,8 @@ DEFINE_EVENT(filelock_lock, flock_lock_inode, { LEASE_BREAK_OPEN_RDONLY, "OPEN_RDONLY" }, \ { LEASE_BREAK_DIR_CREATE, "DIR_CREATE" }, \ { LEASE_BREAK_DIR_DELETE, "DIR_DELETE" }, \ - { LEASE_BREAK_DIR_RENAME, "DIR_RENAME" }) + { LEASE_BREAK_DIR_RENAME, "DIR_RENAME" }, \ + { LEASE_BREAK_RETRY, "RETRY" }) TRACE_EVENT(break_lease, TP_PROTO(struct inode *inode, unsigned int flags), base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb -- 2.43.0