News for hackers. Stuff that matters.
Re:Async hazard: MMAP is blocking IO by a-dub on Saturday August 24, 2024 @03:45PM (#41340907)
it outsources buffer management and user thread i/o scheduling to the kernel. for some use cases it's a great way to simplify implementation or boost performance. for others it may not perform as well.

the blog post points (in my mind) at some more general advice when programming which is not to mix and match paradigms unless you really know what you're doing. if you want to do user space async io, cool. if using kernel features tickles your fancy, also cool.

mixing both without a deep understanding of what's going on under the hood will probably give you trouble.

Re:Async hazard: MMAP is blocking IO by rbanffy on Saturday August 24, 2024 @06:08PM (#41342107)
Making it work asynchronously would require the compiler to split the memory access into two parts, a non-blocking IO dispatch and a blocking access to the mapped address. The OS would need to support that, however, and the language would need to keep track of what is a materialised array and what’s not.
Re:Async hazard: MMAP is blocking IO by a-dub on Saturday August 24, 2024 @06:55PM (#41342501)
as i understand, mmap is only efficient because it can leverage hardware support for trapping into the kernel when a page needs to be loaded to satisfy an access attempt.

i think adding software indirection to every access in the mapped region would be really slow.

i think a better answer would be to impose more structure on the planned memory access, then maybe given some constraints (like say, "this loop is embarrassingly parallel") the system could be smarter about working on the stuff in ram first while the rest is loaded in.

Re:Async hazard: MMAP is blocking IO by rbanffy on Sunday August 25, 2024 @02:07PM (#41349625)
> every access in the mapped region would be really slow.

Would certainly be slower. The compiler would need to be aware we want this behaviour and split the access in two parts, one to trigger the page read and yield to the app’s async loop, and another to resolve the read when the page has loaded. This would only need to happen for explicitly marked asynchronous memory reads (doing that without hardware support for all memory reads would be painful).

Re:Async hazard: MMAP is blocking IO by gpderetta on Sunday August 25, 2024 @03:30PM (#41350493)
Normal syscalls also "leverage hardware support for trapping into the kernel". Mmap is usually used because it is a simple way to do 0-copy disk I/O.
Re:Async hazard: MMAP is blocking IO by a-dub on Tuesday August 27, 2024 @10:27AM (#41367925)
i can't think of any other syscall that makes use of tlb caches and page fault machinery to enter the kernel as needed in response to ordinary user space memory access.
Re:Async hazard: MMAP is blocking IO by noctune on Sunday August 25, 2024 @03:54AM (#41345360)
I think you could make by with some kind of async memory-touch system call, i.e. "page in this range of memory, notify me when finished". The application would have to call this on blocks of the mmap prior to actually reading it.

This of course means you lose some of the benefits of mmap (few system calls, automatic paging), but would maybe still be beneficial from a performance perspective.

Re:Async hazard: MMAP is blocking IO by rbanffy on Sunday August 25, 2024 @02:16PM (#41349724)
It would allow a memory read to yield to the async loop, but overall performance of the read itself would always be lower.

It’s the kind of thing that would be better implemented as a special “async buffer” where reads are guarded by a page fault handler that returns as soon as the read is scheduled and a read that yields on an unresolved page load.

Re:Async hazard: MMAP is blocking IO by gpderetta on Sunday August 25, 2024 @03:18PM (#41350358)
io_uring + madvise is probably the the closest solution.

Although if you are using uring, there are other options for async disk Io.