Take the FUSE file slot before the session lock

Merged
MA
8d ago
+521-15

fuse_open_path held session->lock across the path walk and then took fuse_lock to allocate the file slot, while fuse_path_lookup takes fuse_lock and then session->lock. The session lock is held across daemon round trips, so a lookup waiting on it while holding fuse_lock and an open waiting on fuse_lock while holding it deadlock each other.

The slot is now allocated under the fuse_lock the open already holds after it the session.


Summary by cubic

Fixes a deadlock in the FUSE open path by reserving the file slot before taking the session lock.

fuse_open_path held session->lock across the path walk and then took fuse_lock for the slot, while fuse_path_lookup takes fuse_lock then session->lock. An open waiting on fuse_lock while holding the session lock and a lookup waiting on the session lock while holding fuse_lock deadlocked. The slot is now reserved under the fuse_lock the open already holds for the mount lookup, before the session lock is taken, and every early return hands it back.

Nothing reaches the daemon for an open that cannot get a slot, so a full table reports ENFILE instead of the earlier ENOMEM for a name that resolves, and ENFILE instead of ENOENT for a missing name. An O_TRUNC open cannot truncate a file it then fails to open. Each open holds a slot for the whole walk, so a burst of concurrent opens can fill the table and turn away an open that would have succeeded.

  • Adds test-fuse-open-lock and the ELFUSE_FUSE_OPEN_LOCK_DELAY_US hook to hold an open inside the session lock while a stat starts, which deadlocked on every run before this change.