filelock: don't hand out a delegation that is being recalled
Subsystems (3)
PATCH v2 1/1 filelock: don't hand out a delegation that is being recalled
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.
---
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/[email protected]/
fs/locks.c | 21 ++++++++++++++++++--- include/linux/filelock.h | 9 ++++++++- include/trace/events/filelock.h | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-)
fs/locks.c +18 -3
@@ -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 **prif (!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
include/linux/filelock.h +8 -1
@@ -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 breakstruct 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; }
include/trace/events/filelock.h +2 -1
@@ -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
Signature · 1 line
2.43.0
[email protected]
AI reviewThank 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…
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 <[email protected]>
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.
Quoted 4 lines
fs/locks.c +0
-0
[ ... ]
Quoted 9 lines
fs/locks.c +1
-0@@ -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 ;
fs/locks.c int __break_lease(struct inode *inode, unsigned int flags) · line 1742
Quoted 9 lines
fs/locks.c +3
-0@@ -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() -> -EWOULDBLOCKbreak_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.
Quoted 10 lines
fs/locks.c +0
-1@@ -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)*/
fs/locks.c int __break_lease(struct inode *inode, unsigned int flags) · line 1789
Quoted 10 lines
fs/locks.c +3
-2if (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.
[ ... ]
Quoted 4 lines
include/linux/filelock.h +0
-0
[ ... ]
Quoted 13 lines
include/linux/filelock.h +4
-1@@ -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);
include/linux/filelock.h static inline int break_deleg_wait(struct delegated_inode *di) · line 572
Quoted 6 lines
include/linux/filelock.h +1
-0@@ -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).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jeff Layton
maintainerQuoted 189 lines · Hungyu Lin
An operation that finds a conflicting delegation gets
-EWOULDBLOCKfromtry_break_deleg(), drops its locks, waits inbreak_deleg_wait()and then retries. Nothing stops a new delegation from being set on the inode in the meantime.With
F_SETDELEGthe 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()andchown()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")Signed-off-by Hungyu Lin <[email protected]>---
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 withfutimens()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/[email protected]/
fs/locks.c | 21 ++++++++++++++++++--- include/linux/filelock.h | 9 ++++++++- include/trace/events/filelock.h | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-)fs/locks.c +18
-3@@ -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 **prif (!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 include/linux/filelock.h +8
-1@@ -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 breakstruct 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; }include/trace/events/filelock.h +2
-1@@ -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),
Quoted 1 line
base-commit:
7475ea8b58616b33c04afb976cf0fbaad9dd63fb
I'm not sure I like plumbing this logic into file locking layer. We've never made this guarantee at this level. In nfsd this is handled by higher-level logic it has a bloom filter that prevents delegs from being handed out too quickly after recall.
I'll ponder this, but for the moment I don't see a compelling reason to do this here.
Thanks,
Signature · 1 line
Jeff Layton <[email protected]>