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 --- 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