Bug 262535 - Linuxulator sendfile does not support all file descriptors
Summary: Linuxulator sendfile does not support all file descriptors
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Dmitry Chagin
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-03-13 23:17 UTC by James McLaughlin
Modified: 2023-11-06 18:40 UTC (History)
3 users (show)

See Also:


Attachments
Patch to add a fallback to Linuxulator sendfile to support any FDs (5.46 KB, patch)
2022-03-13 23:17 UTC, James McLaughlin
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description James McLaughlin 2022-03-13 23:17:07 UTC
Created attachment 232438 [details]
Patch to add a fallback to Linuxulator sendfile to support any FDs

In Linux < 2.6.33, sendfile could only be used to send from a file to a socket. This is the behaviour implemented by the FreeBSD Linuxulator. However, since 2.6.33, sendfile can send from any FD to any other FD, which is not implemented by FreeBSD sendfile or by the Linuxulator.

I ran into this problem trying to run the game "Factorio" using the Linuxulator, which relies on sendfile to copy files on disk. Here is another thread on freebsd-emulation describing the same problem:

https://www.mail-archive.com/freebsd-emulation@freebsd.org/msg11340.html

Attached is a patch which changes the Linuxulator sendfile such that it will use FreeBSD sendfile where possible, but otherwise fallback on a simple read/write loop for compatibility.

I would very much appreciate some feedback on the patch, as I have never contributed to the FreeBSD kernel before. In particular, any suggestion for a better way to choose the buffer size than the arbitrary 8 KB I have hardcoded would be welcome.
Comment 1 James McLaughlin 2022-03-13 23:28:22 UTC
Patch also viewable as a PR on GitHub:

https://github.com/freebsd/freebsd-src/pull/590
Comment 2 Nathan Whitehorn freebsd_committer freebsd_triage 2023-04-04 16:53:44 UTC
I just hit the same issue with the Microchip Libero FPGA software. Will attempt to triage the patch.
Comment 3 commit-hook freebsd_committer freebsd_triage 2023-08-17 19:59:28 UTC
A commit in branch main references this bug:

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

commit bb66c59753836cd8abb596fe316dcdb77ea66999
Author:     James McLaughlin <james_mclgh.net>
AuthorDate: 2023-08-17 19:57:17 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2023-08-17 19:57:17 +0000

    linux(4): Add sendfile fallback for non-socket fds

    Before Linux 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33
    it can be any file.
    The patch was originally provided by James McLaughlin and adapted by me
    for copy_file_range.

    PR:                     262535
    Differential revision:  https://reviews.freebsd.org/D34555
    MFC after:              1 month

 sys/compat/linux/linux_socket.c | 209 ++++++++++++++++++++++++++++++++++------
 1 file changed, 177 insertions(+), 32 deletions(-)
Comment 4 Dmitry Chagin freebsd_committer freebsd_triage 2023-09-25 11:43:52 UTC
James, Nathan, are you tried latest main or releng/14.0?
Comment 5 Dmitry Chagin freebsd_committer freebsd_triage 2023-11-06 18:40:07 UTC
Done, 10x