Skip to content

Single worker during init - #1326

Merged
bsbernd merged 2 commits into
libfuse:masterfrom
bsbernd:single-worker-during-init
Sep 22, 2025
Merged

bsbernd merged 2 commits into
libfuse:masterfrom
bsbernd:single-worker-during-init

Conversation

@bsbernd

@bsbernd bsbernd commented Aug 31, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@bsbernd
bsbernd force-pushed the single-worker-during-init branch from e69311d to e9f106a Compare August 31, 2025 21:39
@bsbernd
bsbernd force-pushed the single-worker-during-init branch 3 times, most recently from ec0d76c to e27c78e Compare September 1, 2025 18:35
@bsbernd
bsbernd force-pushed the single-worker-during-init branch from e27c78e to 400834e Compare September 9, 2025 13:31
This is to prevent startup races. For example it is
hard for threads to know the buffer size the kernel usage.
In the past kernel buffer size always was max 1MB, but in
the mean time this might be more, depending on system tunings.
I thought I had fixed all races, but the report in issue libfuse#1296
proves otherwise.

Closes: libfuse#1296
Signed-off-by: Bernd Schubert <[email protected]>
@bsbernd
bsbernd force-pushed the single-worker-during-init branch from 400834e to 8a6cbbf Compare September 9, 2025 13:55
@bsbernd

bsbernd commented Sep 9, 2025

Copy link
Copy Markdown
Collaborator Author

@joannekoong @szmi Any chance you could review? I'm also going to add a commit on top later this week to avoid buffer re-allocation when reading /dev/fuse, but have fuse_do_work in two steps - first read FUSE_INIT and then go in to the current while loop. fuse_do_work can then free the initial buffer. Almost done, just a bit cleanup missing.

Comment thread lib/fuse_loop_mt.c
mt->numavail--;
if (mt->numavail == 0 && mt->numworker < mt->max_threads)
if (mt->numavail == 0 && mt->numworker < mt->max_threads &&
likely(se->got_init))

@joannekoong joannekoong Sep 11, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does se->got_init = 1 also need to be moved in _do_init() to be set after (instead of before) the

if (se->op.init) {
    ...
     se->op.init(se->userdata, &se->conn);
 }

call?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Let me add this into a different commit. I would like to back port the 1st commit in this series to 3.17.x, as I think it is rather safe. Setting se->op.init later has the slight risk to change behavior, as it is check in send_notify_iov() - isn't there at least slight risk that application call notify from their ->init method?

Technically moving got_init is not needed in this commit, although I didn't mention it in the commit message. New worker threads are created by fuse_do_work - it will only read got_init after returning from fuse_session_process_buf_internal - returns after completely handling FUSE_INIT.

Comment thread lib/fuse_lowlevel.c Outdated
Comment on lines +4075 to +4076
se->bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() +
FUSE_BUFFER_HEADER_SIZE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this run into issues later when the init processing is handled (in particular when this line gets hit se->conn.max_write = MIN(se->conn.max_write, bufsize - FUSE_BUFFER_HEADER_SIZE);)? I think in most of the cases, when we reach that line, bufsize is se->bufsize, which means se->conn.max_write would be truncated too short?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point!

The line before has

	if (buf_reallocable)
	    bufsize = UINT_MAX;

Which is true for all internal libfuse users. And buf_reallocable is also set to false when FUSE_MAX_PAGES is not set - technically setting it to false is actually not correct - we just don't need a larger buffer in that case.

My generic issue with fuse_session_receive_buf, i.e. external callers is, how do these callers actually know the needed buffer size? struct fuse_session is internal - these callers have no knowledge which buffer size has to be used? At best we can add an assumption into the code that when called through fuse_session_receive_buf that it is an external caller and that this caller has set a buffer size of 1MB - i.e. traditional libfuse3 behavior?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I removed this commit for now, will added in a different PR

@bsbernd
bsbernd force-pushed the single-worker-during-init branch from 8a6cbbf to 6f0a8f1 Compare September 22, 2025 09:08
@bsbernd bsbernd changed the title Single worker during init and lower default buffer size Single worker during init Sep 22, 2025
@bsbernd
bsbernd force-pushed the single-worker-during-init branch 2 times, most recently from 32b80b6 to ffaee98 Compare September 22, 2025 09:23
A previous commit added single thread behavior as long as se->got_init is
not set, especially as buffer allocation should be done after got_init
only. Technically it should not make a difference in current libfuse
design, as more worker threads are only allocated after the FUSE_INIT
handler is completely done. That might at some point change, though.
In general it is cleaner to set se->got_init, when the init handler
is done.

Done as separate commit, as this introduces a slight risk to break
something - backport to older version should be done carefully.

Signed-off-by: Bernd Schubert <[email protected]>
@bsbernd
bsbernd force-pushed the single-worker-during-init branch from ffaee98 to 8f7fae1 Compare September 22, 2025 10:48
@bsbernd
bsbernd merged commit 59cf26a into libfuse:master Sep 22, 2025
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.

2 participants