RFR: 8234930: Use MAP_JIT when allocating pages for code cache on macOS [v7]
Bernhard Urban-Forster
burban at openjdk.java.net
Thu Dec 10 21:46:58 UTC 2020
On Fri, 4 Dec 2020 22:29:25 GMT, Anton Kozlov <akozlov at openjdk.org> wrote:
>> Please review an updated RFR from https://mail.openjdk.java.net/pipermail/hotspot-runtime-dev/2020-August/041463.html
>>
>> On macOS, MAP_JIT cannot be used with MAP_FIXED[1]. So pd_reserve_memory have to provide MAP_JIT for mmap(NULL, PROT_NONE), the function was made aware of exec permissions.
>>
>> For executable and data regions, pd_commit_memory only unlocks the memory with mprotect, this should make no difference compared with old code.
>>
>> For data regions, pd_uncommit_memory still uses a new overlapping anonymous mmap which returns pages to the OS and immediately reflects this in diagnostic tools like ps. For executable regions it would require MAP_FIXED|MAP_JIT, so instead madvise(MADV_FREE)+mprotect(PROT_NONE) are used. They should also allow OS to reclaim pages, but apparently this does not happen immediately. In practice, it should not be a problem for executable regions, as codecache does not shrink (if I haven't missed anything, by the implementation and in principle).
>>
>> Tested:
>> * local tier1
>> * jdk-submit
>> * codesign[2] with hardened runtime and allow-jit but without
>> allow-unsigned-executable-memory entitlements[3] produce a working bundle.
>>
>> (adding GC group as suggested by @dholmes-ora)
>>
>>
>> [1] https://github.com/apple/darwin-xnu/blob/master/bsd/kern/kern_mman.c#L227
>> [2]
>>
>> codesign \
>> --sign - \
>> --options runtime \
>> --entitlements ents.plist \
>> --timestamp \
>> $J/bin/* $J/lib/server/*.dylib $J/lib/*.dylib
>> [3]
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
>> <plist version="1.0">
>> <dict>
>> <key>com.apple.security.cs.allow-jit</key>
>> <true/>
>> <key>com.apple.security.cs.disable-library-validation</key>
>> <true/>
>> <key>com.apple.security.cs.allow-dyld-environment-variables</key>
>> <true/>
>> </dict>
>> </plist>
>
> Anton Kozlov has updated the pull request incrementally with three additional commits since the last revision:
>
> - Fix style
> - JDK-8234930 v4: Use MAP_JIT when allocating pages for code cache on macOS
> - Revert "Separate executable_memory interface"
>
> This reverts commit 49253d8fe8963ce069f10783dcea5327079ba848.
I read through the discussion and it makes sense to me. Thanks for the `mprotect`/`madvice` tests, they are pretty interesting.
Patch looks good too (but I'm not a Reviewer).
src/hotspot/os/bsd/os_bsd.cpp line 1937:
> 1935: // Bsd mmap allows caller to pass an address as hint; give it a try first,
> 1936: // if kernel honors the hint then we can return immediately.
> 1937: char * addr = anon_mmap(requested_addr, bytes, false/*executable*/);
use `!ExecMem`?
src/hotspot/os/linux/os_linux.cpp line 3275:
> 3273: struct bitmask* os::Linux::_numa_membind_bitmask;
> 3274:
> 3275: bool os::pd_uncommit_memory(char* addr, size_t size, bool exec) {
nit: I'm irritated by `bool exec` in `pd_uncommit_memory`, but `bool executable` in `pd_reserve_memory`. Choose one :-)
-------------
Marked as reviewed by burban (Author).
PR: https://git.openjdk.java.net/jdk/pull/294
More information about the hotspot-dev
mailing list