Repository navigation
Linux 7.0: fuse: drop OFD and flock() locks at the last close instead of at RELEASE - #289
Merged
hbirth merged 3 commits intoNov 16, 2026
Conversation
With a server that takes fcntl locks itself (FUSE_POSIX_LOCKS), an OFD
lock outlives the last close() of its open file description. The server
learns that the description is gone from RELEASE and drops the lock
there, but RELEASE is queued as a background request by the final
fput(), after close() has returned. Until the server has processed it,
F_OFD_GETLK through another description still reports the lock and a
non-blocking F_OFD_SETLK fails with EAGAIN.
generic/478 fails on this in its CLONE_FILES cases ("get wrlck" where
"lock could be placed" is expected) since redfsd handles fcntl locks
(RED-46944). Before that it answered ENOSYS and the kernel kept the
locks locally. In a loop of F_OFD_SETLK, close(), F_OFD_GETLK through a
second description, the lock was still reported after 49 of 50 closes
and stayed for 0.2 to 2.9 ms.
The VFS does not help here: locks_remove_posix() returns early when the
inode has no lock on its own list, and fuse does not record a lock the
server holds. RELEASE cannot be made synchronous either, because the
final fput() can run on a thread of the server (see
fuse_file_release()).
Do it from ->flush(), which close() calls in the closing task before
that fput(). When ->flush() holds the only reference to the file, the
descriptor it runs for was the last one: it has left the descriptor
table already, and a mapping, an I/O in flight or a descriptor in
transit would hold a reference of its own, so no new reference can
appear. In that case send the unlock of the whole file for the OFD
owner and wait for it, after the data has been flushed. It is sent only
for a file through which an OFD lock was requested from the server.
Closing one of several descriptors of a description (dup(), fork())
leaves the lock in place, as it must. A description that loses its last
reference in another way (munmap() after close(), an aio completion) is
unlocked by RELEASE, as before.
flock() locks have the same window, because FUSE_RELEASE_FLOCK_UNLOCK
travels in the same RELEASE. This change does not touch them.
Tested on 7.0.0-42-generic against RED 2.5.2 (497d797afb + red#30365),
with and without the writeback cache: the loop above reports the lock
after 0 of 200 closes, and a lock survives the close of a dup() in 200
of 200 cases. generic/478 passes in 6 of 6 runs without the cache and
in 3 of 3 with it; without the change it fails in 5 of 5 and in 2 of 2.
Signed-off-by: Allison Henderson <allison.henderson@ddn.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Hoist the unlock request out of fuse_ofd_unlock_on_close() into a common helper, fuse_unlock_file_owner(). The helper takes the lk_flags of the request, so the same logic can be reused for another kind of lock that is owned by the file. What stays in the caller is the decision when to unlock: first whether this is the last close, then what there is to unlock. Rename it fuse_unlock_on_last_close(), since it will not stay specific to OFD locks. This is a preparatory patch for closing the same window for flock() locks. No functional change. Signed-off-by: Allison Henderson <allison.henderson@ddn.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A flock() lock that the server holds (FUSE_FLOCK_LOCKS) has the window an OFD lock had: the server drops it when RELEASE arrives with FUSE_RELEASE_FLOCK_UNLOCK, and RELEASE is sent in the background after close() has returned. Until then flock(LOCK_EX | LOCK_NB) through another description fails with EWOULDBLOCK although the only holder has closed its file. In a loop of flock(LOCK_EX), close(), flock(LOCK_EX | LOCK_NB) through a second description, that happened after 187 of 200 closes and lasted up to 3.2 ms. Send that unlock from fuse_unlock_on_last_close() as well, which ->flush() calls and which acts when ->flush() holds the only reference to the file: fuse_unlock_file_owner() once more, with FUSE_LK_FLOCK, for a file that was locked with flock() through the server. RELEASE keeps its FUSE_RELEASE_FLOCK_UNLOCK flag for a description whose last reference goes away without a close(); after the unlock from ->flush() it finds nothing to drop. The cost is one more request at the last close of a file that was ever locked with flock(), also when the lock was dropped with LOCK_UN before. A file that never took a lock is closed with the same requests as before. Tested on 7.0.0-42-generic against RED 2.5.2 (497d797afb + red#30365), with and without the writeback cache: the loop above finds the lock after 0 of 200 closes; a flock() lock and an OFD lock survive the close of a dup() and the exit of a forked child in 200 of 200 cases each; and generic/131, 478 and 571 pass in 3 of 3 runs in each mode. Signed-off-by: Allison Henderson <allison.henderson@ddn.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
hbirth
approved these changes
Nov 12, 2026
Collaborator
|
I would like @yongzech opinion on this, too |
Collaborator
|
@yongzech I'm not that comfortable merging this without you taking a look at it. |
Collaborator
Yes, this is correct, and we need this also in all the supported kernel versions. I assigned this to you @achhenderson |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With a server that takes fcntl and flock locks itself (redfsd since RED-46944), an OFD lock or a
flock()lock outlives the lastclose()of its open file description. The server drops the lock when it processes RELEASE, and RELEASE is queued in the background by the finalfput(), afterclose()has returned. Until thenF_OFD_GETLKthrough another description still reports the lock, a non-blockingF_OFD_SETLKfails withEAGAIN, andflock(LOCK_EX | LOCK_NB)fails withEWOULDBLOCK. The window is 0.2 to 3.2 ms.generic/478 fails on this in its
CLONE_FILEScases ("get wrlck" where "lock could be placed" is expected).The VFS does not send the unlock:
locks_remove_posix()returns early because fuse records no lock on the inode. RELEASE cannot be made synchronous, because the finalfput()can run on a thread of the server.Three commits:
->flush()runs in the closing task before the finalfput(). When it holds the only reference to the file, the descriptor it runs for was the last one: it has left the descriptor table, and a mapping, an I/O in flight or a descriptor in transit would hold a reference of its own. In that case it sends the whole-file unlock for the OFD owner and waits for it, after the data has been flushed. This is the fix for generic/478.fuse_unlock_file_owner(). No functional change.FUSE_LK_FLOCK: two lines of code.A description that loses its last reference in another way (
munmap()afterclose(), an aio completion) is unlocked by RELEASE, as before. Closing one of several descriptors of a description (dup(),fork()) leaves the lock in place.Cost: one more request, a SETLK unlock, at the last close of a file that ever took such a lock through the server, also when it was unlocked explicitly before. A file that never took a lock is closed with the same requests as before. If the extra request is not wanted for
flock()users, commit 3 can be left out; commits 1 and 2 stand without it.This is not specific to 7.0. generic/478 fails the same way on 6.8, and the 6.17, 6.12 and 6.8 branches need the same change. Ports will follow once this is reviewed.
Testing
Ubuntu 7.0.0-42-generic with
redfs.kobuilt from this branch, RED release/2.5, with and without the writeback cache:close(), query through a second description. OFD: the lock is still reported after 0 of 200 closes; without the change after 49 and 50 of 50 without the writeback cache and 195 of 200 with it.flock(): still held after 0 of 200 closes; without the change after 187 of 200 and 83 of 200.flock()lock each survive the close of adup()and the exit of a forked child in 200 of 200 cases.flock()window is unchanged.flock()loop sends one extra SETLK per closed description; 100 open/close pairs without a lock send 100 OPEN, FLUSH and RELEASE and no SETLK.