RFR: 8272807: Permit use of memory concurrent with pretouch [v2]
Kim Barrett
kbarrett at openjdk.java.net
Tue Feb 8 08:47:05 UTC 2022
On Fri, 4 Feb 2022 16:17:19 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:
>> Also, that won't touch the last page when first page != last page, unless the last page is made exclusive, which calculation could also suffer from UB pointer arithmetic overflow. Of course, the caller could suffer from that too; we can't so easily fix that. Representing address ranges as [start, end) just generally suffers from that problem.
>
> All right, what irked me originally is that `cur == last` break assumes both `cur` and `last` are multiples of `page_size`, otherwise this loops "indefinitely" (until successive overflows help, I guess). This is true now, but it is not even asserted. Maybe it should be `cur >= last`?
I assumed that having both values be initialized as page-aligned just
before the loop made that obvious.
A slightly different way to write it might be:
void os::pretouch_memory(void* start, void* end, size_t page_size) {
assert(start <= end, "invalid range: " PTR_FORMAT " -> " PTR_FORMAT, p2i(start), p2i(end));
assert(is_power_of_2(page_size), "page size misaligned: %zu", page_size);
assert(page_size >= sizeof(int), "page size too small: %zu", page_size);
if (start < end) {
// We're doing concurrent-safe touch and memory state has page
// granularity, so we can touch anywhere in a page. We need int
// alignment for the touch.
int* cur = static_cast<int*>(align_down(start, sizeof(int)));
void* last = align_down(static_cast<char*>(end) - 1, page_size);
assert(cur <= last, "invariant");
// Iterate from first page through last (inclusive), being careful to
// avoid overflow if the last page abuts the end of the address range.
for ( ; true; cur += (page_size / sizeof(int))) {
Atomic::add(cur, 0, memory_order_relaxed);
if (cur >= last) break;
}
}
}
I don't know if you'd find that clearer.
-------------
PR: https://git.openjdk.java.net/jdk/pull/7343
More information about the hotspot-runtime-dev
mailing list