Bug 293489 - readv(2) on cuse device never returns
Summary: readv(2) on cuse device never returns
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-RELEASE
Hardware: amd64 Any
: --- Affects Only Me
Assignee: Christos Margiolis
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-02-27 15:41 UTC by elephant
Modified: 2026-10-07 06:26 UTC (History)
1 user (show)

See Also:


Attachments
Cuse sample with test code to write to and read from the cuse device. (4.38 KB, application/gzip)
2026-09-26 20:15 UTC, elephant
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description elephant 2026-02-27 15:41:15 UTC
Issuing a read(2) on a cuse device that has data to returns works as expected. When switching to readv(2) with eight iovec-s, the readv call never returns.

Logging from an instrumented cuse device shows the first read is passed the correct buffer address and size. The data appears to be transferred. All subsequent read requires processed inside the cuse device have a correct buffer address but the length is zero.

Code snippet follows:

#include <unistd.h>
#include <sys/uio.h>

int okay;
char chunk[8];
struct iovec list[8] =
{
    {chunk + 0, 1},
    {chunk + 1, 1},
    {chunk + 2, 1},
    {chunk + 3, 1},
    {chunk + 4, 1},
    {chunk + 5, 1},
    {chunk + 6, 1},
    {chunk + 7, 1}
};

int handle = open(/dev/cuse123", O_RDONLY);

do
{
    okay = readv(handle, list, 8);
    if (okay < 0) break;
    write(2, chunk, okay);
}
while (true);
close(handle);

Switching back to a simple read(2) call works as expected.

The underlying issue is likely inside the cuse kernel module but this is just a guess.
Comment 1 elephant 2026-02-27 15:44:52 UTC
typo: requires=request
Comment 2 elephant 2026-02-27 17:00:07 UTC
I updated the instrumented cuse drive to return -1 on read operations with a length of zero. readv(2) now returns "Device busy".

I'll take a look at the cuse kmod.
Comment 3 elephant 2026-02-27 17:08:54 UTC
I changed the readv call to pass only one iovec. Data was transferred but once all data was exhausted and the cuse device returns zero bytes copied, the readv call blocks indefinitely. This, I think is WAD since readv doesn't know when new data will eventually become available.
Comment 4 Christos Margiolis freebsd_committer freebsd_triage 2026-09-18 19:43:26 UTC
Can you apply the following patch and let me know if it fixes the issue?

https://reviews.freebsd.org/D59822
Comment 5 elephant 2026-09-19 19:26:39 UTC
Thanks Christos! Let me see what I can do.
Comment 6 Christos Margiolis freebsd_committer freebsd_triage 2026-09-25 10:35:34 UTC
(In reply to elephant from comment #5)
Any news from this?
Comment 7 elephant 2026-09-25 20:11:11 UTC
(In reply to Christos Margiolis from comment #6)
Sorry Christos. It will be weeks before I can do anything.
Comment 8 Christos Margiolis freebsd_committer freebsd_triage 2026-09-26 12:57:53 UTC
(In reply to elephant from comment #7)
You can just send me your test program and I will test myself if you want.
Comment 9 elephant 2026-09-26 20:15:30 UTC
Created attachment 275158 [details]
Cuse sample with test code to write to and read from the cuse device.

This tar includes a cmake file to create main, drip and drain programs. The main program is the cuse device. The drip program writes data to the device. The drain program reads from the device. Inside drain.cpp are some '#if 1/#else/#endif clauses for experimentation. The drain program most resenbles the code snippet at the start of this bug.

Ping here if you have questions.
Comment 10 Christos Margiolis freebsd_committer freebsd_triage 2026-09-28 12:01:13 UTC
I tested. My patch [1] fixes the issue.

[1] https://reviews.freebsd.org/D59822
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-09-30 09:51:54 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=872c36cb6f2de17278c559a300c2163d8b39b3c6

commit 872c36cb6f2de17278c559a300c2163d8b39b3c6
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-09-30 09:49:35 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-09-30 09:49:35 +0000

    cuse: Fix hang on readv(2) and writev(2) with multiple iovecs

    uiomove() leaves an iovec it has just emptied as the current one, so
    cuse_client_read() and cuse_client_write() picked it up again on the
    next iteration, sent the server a zero-length command, and got zero
    bytes back. That left the residual count unchanged, so the loop never
    terminated and the call never returned.

    Step past empty iovecs at the start of every iteration. This also covers
    caller-supplied zero-length iovecs, which hung in the same way

    PR:             293489
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    kib, markj
    Differential Revision:  https://reviews.freebsd.org/D59822

 sys/fs/cuse/cuse.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
Comment 12 commit-hook freebsd_committer freebsd_triage 2026-10-07 06:25:18 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=b1aea70753bca974af6b8e4814aecd1aaff29e2a

commit b1aea70753bca974af6b8e4814aecd1aaff29e2a
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-09-30 09:49:35 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-10-07 06:23:31 +0000

    cuse: Fix hang on readv(2) and writev(2) with multiple iovecs

    uiomove() leaves an iovec it has just emptied as the current one, so
    cuse_client_read() and cuse_client_write() picked it up again on the
    next iteration, sent the server a zero-length command, and got zero
    bytes back. That left the residual count unchanged, so the loop never
    terminated and the call never returned.

    Step past empty iovecs at the start of every iteration. This also covers
    caller-supplied zero-length iovecs, which hung in the same way

    PR:             293489
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    kib, markj
    Differential Revision:  https://reviews.freebsd.org/D59822

    (cherry picked from commit 872c36cb6f2de17278c559a300c2163d8b39b3c6)

 sys/fs/cuse/cuse.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)