Repository navigation
RED-54356: Linux 6.17: fuse: drop OFD and flock() locks at the last close instead of at RELEASE - #293
Merged
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>
(cherry picked from commit 4bee006)
[ahenderson: there is one fuse_flush() here instead of fuse_wt_flush() and
fuse_wb_flush(); its body becomes fuse_flush_data() and fuse_flush()
calls the unlock after it]
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> (cherry picked from commit cd6cd00) [ahenderson: one caller, fuse_flush(), instead of two]
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> (cherry picked from commit 5fe0a97) [ahenderson: applies unchanged]
hbirth
approved these changes
Nov 17, 2026
hbirth
left a comment
Collaborator
There was a problem hiding this comment.
The backport of an approved change
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
6.17 port of #289 (
4bee006ce65e,cd6cd0014a1a,5fe0a9765c18on the 7.0 branch).With a server that takes fcntl and flock locks itself, 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. generic/478 fails on this in itsCLONE_FILEScases.Three commits, as on 7.0:
->flush()holds the only reference to the file, it sends the whole-file unlock for the OFD owner and waits for it, after the data has been flushed.FUSE_LK_FLOCK.Adaptation: this branch has one
fuse_flush()where 7.0 hasfuse_wt_flush()andfuse_wb_flush(). Its body becomesfuse_flush_data(), andfuse_flush()calls the unlock after it. The helper functions are identical to the 7.0 ones.Cost, as on 7.0: one more request, a SETLK unlock, at the last close of a file that ever took such a lock through the server. A file that never took a lock is closed with the same requests as before.
Testing
Ubuntu 6.17.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 in both modes; without the change after 194 of 200 without the writeback cache and 200 of 200 with it.flock(): still held after 0 of 200 closes; without the change after 187 of 200 and 159 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, as intended.JIRA Ticket
https://ime-ddn.atlassian.net/browse/RED-54356