Skip to content

Linux 7.0: fuse: drop OFD and flock() locks at the last close instead of at RELEASE - #289

Merged
hbirth merged 3 commits into
DDNStorage:redfs-ubuntu-resolute-7.0.0-14.14from
achhenderson:fix-redfs-7.0-flock-unlock-on-close
Nov 16, 2026
Merged

hbirth merged 3 commits into
DDNStorage:redfs-ubuntu-resolute-7.0.0-14.14from
achhenderson:fix-redfs-7.0-flock-unlock-on-close

Conversation

@achhenderson

Copy link
Copy Markdown
Collaborator

Summary

With a server that takes fcntl and flock locks itself (redfsd since RED-46944), an OFD lock or a flock() lock outlives the last close() of its open file description. The server drops the lock when it processes RELEASE, and RELEASE is queued in the background by the final fput(), after close() has returned. Until then F_OFD_GETLK through another description still reports the lock, a non-blocking F_OFD_SETLK fails with EAGAIN, and flock(LOCK_EX | LOCK_NB) fails with EWOULDBLOCK. The window is 0.2 to 3.2 ms.

generic/478 fails on this in its CLONE_FILES cases ("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 final fput() can run on a thread of the server.

Three commits:

  1. fuse: drop OFD locks at the last close instead of at RELEASE. ->flush() runs in the closing task before the final fput(). 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.
  2. fuse: refactor fuse_ofd_unlock_on_close(). Hoists the request into fuse_unlock_file_owner(). No functional change.
  3. fuse: drop a flock() lock at the last close as well. The same unlock with FUSE_LK_FLOCK: two lines of code.

A description that loses its last reference in another way (munmap() after close(), 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.ko built from this branch, RED release/2.5, with and without the writeback cache:

  • A loop of lock, 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.
  • An OFD lock and a flock() lock each survive the close of a dup() and the exit of a forked child in 200 of 200 cases.
  • generic/478 passes in 6 of 6 runs without the writeback cache and 3 of 3 with it; without the change it fails in 5 of 5 and 2 of 2. generic/131 and generic/571 pass in 3 of 3 runs in each mode with all three commits.
  • The refactor commit builds on its own and behaves like commit 1 alone: the OFD window is closed and the flock() window is unchanged.
  • Request counts from a trace: the 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.

Allison Henderson and others added 3 commits November 12, 2026 10:01
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

hbirth commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

I would like @yongzech opinion on this, too

@hbirth

hbirth commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@yongzech I'm not that comfortable merging this without you taking a look at it.

@yongzech

Copy link
Copy Markdown
Collaborator

@yongzech I'm not that comfortable merging this without you taking a look at it.

Yes, this is correct, and we need this also in all the supported kernel versions.

I assigned this to you @achhenderson
https://ime-ddn.atlassian.net/browse/RED-54356

@yongzech yongzech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants