Atomic mmap replacement
Ken Brown
kbrown@cornell.edu
Mon Dec 16 19:58:35 GMT 2024
On 12/16/2024 8:32 AM, Corinna Vinschen via Cygwin wrote:
> Right now, mmaping with PROT_NONE and then re-mmaping with PROT_WRITE
> doesn't work. Cygwin implements PROT_NONE not as MAP_RESERVE, but as
> MEM_COMMIT with PAGE_NOACCESS. mmap() doesn't check if the requested
> pages are already allocated with PAGE_NOACCESS and then succeeds while
> in fact just changing the page protection. This is basically what
> you want. Right now, you'd have to call mprotect() instead.
>
> With anonymous mappings only, this is all just adding a bit of code to
> mmap() to do what mprotect() does in this case.
[...]
> So only anonymous mappings would be possible, assuming we tweak mmap()
> to check if the old mapping was anonymous either and then allow to
> just change the page protection, as if mprotect has been called.
>
> And, funny enough, something pretty similar already exists in mmap().
> See mmap.cc, line 1051 and the mmap_list::try_map() method. Right
> now it only checks if an anonymous mapping has been partially unmapped
> and can be recycled. But it could be improved by allowing to recycle
> the anonymous mapping either way, as long as the new mapping is also
> anonymous and the SHARED/PRIVATE flags match.
Thanks! This looks doable. I hope to get back to you with a patch in
the not-too-distant future.
Ken
More information about the Cygwin
mailing list