Page MenuHomeFreeBSD

linux(4): Implement close_range
ClosedPublic

Authored by dchagin on Apr 3 2023, 12:14 PM.
Referenced Files
Unknown Object (File)
Fri, May 3, 9:59 AM
Unknown Object (File)
Dec 20 2023, 2:07 AM
Unknown Object (File)
Dec 12 2023, 6:19 PM
Unknown Object (File)
Nov 6 2023, 6:17 AM
Unknown Object (File)
Oct 15 2023, 2:46 PM
Unknown Object (File)
Oct 14 2023, 7:20 AM
Unknown Object (File)
Oct 5 2023, 4:57 AM
Unknown Object (File)
Sep 3 2023, 11:35 PM
Subscribers

Details

Summary

MFC after: 2 weeks

Test Plan

glibc tests suite io/tst-close_range

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

dchagin added reviewers: mjg, Linux Emulation.
dchagin added a project: Linux Emulation.

linux "fd unsharing" is *not* accurately modeled by fdunshare, which is why I said trying to provide one for native binaries would be weird. they unshare at thread level, while freebsd is doing it at process level. It's all legacy crap.

do you have a program which uses the flag? it would be rather cumbersome to implement and highly doubt it is worth any effort

that said, it is ackable modulo the unshare thing, which should einval or whatever linux did prior to the addition of the flag

In D39398#896738, @mjg wrote:

linux "fd unsharing" is *not* accurately modeled by fdunshare, which is why I said trying to provide one for native binaries would be weird. they unshare at thread level, while freebsd is doing it at process level. It's all legacy crap.

ah, I completely forgot that the Linux kernel does not make distinction between processes and threads.
I have read only man page )) which is "Unshare the specified file descriptors from any other processes before closing them, avoiding races with other threads sharing the file descriptor table." I.e., it should be process-wide.
So, calling close_range(UNSHARE) from a thread in multi-threaded app can cause unexpected behavior? As that thread can open a new file for which the fd will be reused.

do you have a program which uses the flag? it would be rather cumbersome to implement and highly doubt it is worth any effort

No, only tests from glibc and kernel.org

that said, it is ackable modulo the unshare thing, which should einval or whatever linux did prior to the addition of the flag

Or we can check P_HADTHREADS and fdunshare if not, return EINVAL if p had threads. It will work in a mostly cases

that would introduce wtf in debugging, just don't try to mess with the feature

sys/compat/linux/linux_file.c
2093

this block needs to start with a "A note about CLOSE_RANGE_UNSHARE" or similar, otherwise it is unclear what the fuss is about, and even then I don't think the comment adds any value. Personally I would just note that freebsd does not have compatible unsharing mechanism and it does not look like adding one here is worth any effort.

sys/compat/linux/linux_file.h
200

this should be added but commented out

200

with a note to take a look at linux_close_range for an explanation

ok,
what if we simply ignore UNSHARE flag and call kern_close_range with flags = 0?
I dont see problem as file manipulation is under FILEDESC_XLOCK.

ignoring the flag would result in a massive wtf for the unlikely case where someone needs it

just error out

In D39398#896979, @mjg wrote:

ignoring the flag would result in a massive wtf for the unlikely case where someone needs it

just error out

well, done exactly as you say, what else?

This revision is now accepted and ready to land.Apr 4 2023, 6:43 PM
sys/compat/linux/linux_file.h
201

rnage

This revision was automatically updated to reflect the committed changes.