RFR: 8234930: Use MAP_JIT when allocating pages for code cache on macOS [v6]
Thomas Stuefe
stuefe at openjdk.java.net
Fri Dec 4 12:18:15 UTC 2020
On Thu, 3 Dec 2020 20:26:23 GMT, Anton Kozlov <akozlov at openjdk.org> wrote:
> > >
> > > madvise(FREE) is not sufficient unfortunately. For executable memory, it's best we can do. But we should not use it for regular memory.
> >
> >
> > How so? What is the difference?
>
> madvise is really a hint and doesn't have exact effect as a real uncommit. A real, immediately accountable uncommit is a
>
> https://github.com/openjdk/jdk/blob/114d9cffd62cab42790b65091648fe75345c4533/src/hotspot/os/bsd/os_bsd.cpp#L2015
>
> . As I cannot do this for executable memory, I use madvise as an alternative to nothing.
I may have not been clear, sorry. My point was:
For uncommit, we seem to have the option of either:
1) mmap(MAP_FIXED|MAP_NORESERVE, PROT_NONE)
2) madvise(MADV_FREE) + mprotect(PROT_NONE)
You do (1) for !exec, (2) for exec. Why?
Either (2) works - reclaims memory for the OS. Then it can be used in all cases, exec or !exec. The commit would be a simple mprotect(PROT_RW). For uncommit, we would need no exec parameter.
Or (2) does not work, as you claim. Then why bother at all?
Interestingly, your initial proposal would have resulted in the following sequence of calls when committing executable memory:
anon_mmap() -> mmap(MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS | MAP_JIT, PROT_NONE)
commit_memory() -> mprotect(PROT_READ|PROT_WRITE|PROT_EXEC)
Note how on commit_memory() we never clear the MAP_NORESERVE flag. And still commit works? And does not trap on access? Because if that works this is an indication that MAP_NORESERVE has no meaning on MacOS.
I found nothing about MAP_NORESERVE in the kernel source you posted [1], in the MacOS manpage for mmap [2] nor the OpenBSD mmap manpage [3]. MAP_NORESERVE is a non-Posix extension, so I wonder if it gets even honored on MacOS or if they just provided the flag to avoid build errors.
If MAP_NORESERVE has no meaning, we do not need to call mmap() for committing and uncommitting; mprotect, maybe combined with madvise(MADV_FREE), should suffice.
Thanks, Thomas
[1] https://github.com/apple/darwin-xnu/blob/master/bsd/kern/kern_mman.c#L227
[2] https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/mmap.2.html
[3] https://man.openbsd.org/mmap.2
-------------
PR: https://git.openjdk.java.net/jdk/pull/294
More information about the hotspot-dev
mailing list