News for hackers. Stuff that matters.
Re:How memory maps (mmap) deliver faster file access in Go by zozbot234 on Friday October 24, 2025 @05:05AM (#45692503)
> You would need an hardware instruction that tells you if a load or store would fault.

You have MADV_FREE pages/ranges. They get cleared when purged, so reading zeros tells you that the load would have faulted and needs to be populated from storage.

Re:How memory maps (mmap) deliver faster file access in Go by vlovich123 on Friday October 24, 2025 @08:10AM (#45693813)
MADV_FREE is insufficient - userspace doesn’t get a signal from the OS to know when there’s system wide memory pressure and having userspace try to respond to such a signal would be counter productive and slow in a kernel operation that needs to be a fast path. It’s more that you want MADV (page cache) a memory range and then have some way to have a shared data structure where you are told if it’s still resident and can lock it from being paged out.
Re:How memory maps (mmap) deliver faster file access in Go by zozbot234 on Friday October 24, 2025 @09:56AM (#45694704)
> userspace doesn’t get a signal from the OS to know when there’s system wide memory pressure

Memory pressure indicators exist, https://docs.kernel.org/accounting/psi.html

> have some way to have a shared data structure where you are told if it’s still resident and can lock it from being paged out.

What's more efficient than fetching data and comparing it with zero? Any write within the range will then cancel the MADV_FREE property on the written-to page thus "locking" it again, and this is also very efficient.

Re:How memory maps (mmap) deliver faster file access in Go by bcrl on Friday October 24, 2025 @08:40PM (#45700490)
MADV_FREE is also extremely expensive. CPU vendors have finally simplified TLB shootdown in recent CPUs with both AMD and Intel now having instructions to broadcast TLB flushes in hardware, which gets rid of one of the worst sources of performance degradation in threaded multicore applications (oh the pain of IPIs mixed with TLB flushing!). However, it's still very expensive to walk page tables and free pages.

Hardware reference counting of memory allocations would be very interesting. It would be shockingly simple to implement compared to many other features hardware already has to tackle.

Re:How memory maps (mmap) deliver faster file access in Go by zozbot234 on Saturday October 25, 2025 @04:48AM (#45702304)
> MADV_FREE is also extremely expensive.

It's quite expensive to free pages under memory pressure (though it's not clear that there's any other choice to be made), but if the pages are never freed it should be cheap, AIUI.