<p dir="ltr">Hi Bengt,</p>
<p dir="ltr">Great write-up! What was in the hs_err file that led you (or Mikael) here?</p>
<p dir="ltr">One question I have is why alignment is a multiple of page size and not page size? I imagine callers want to reserve X space and have it page aligned up if necessary. When would someone request multiple page size alignment rather than just increase requested allocation amount and keep alignment at page size? If huge pages are used then vm_allocation_granularity() respects that, right? Or is this for transparent huge page support? Hope that makes sense.</p>
<p dir="ltr">The other question is whether it's worth it to check for overflow in the new os_posix function? There are some additions and subtractions on size_t values and maybe on 32 bit some of those adds can overflow a temporary? Specifically, line 118. Disregard if that's not needed :).</p>
<p dir="ltr">Thanks<br></p>
<p dir="ltr">Sent from my phone</p>
<div class="gmail_quote">On Dec 13, 2012 7:59 AM, "Bengt Rutisson" <<a href="mailto:bengt.rutisson@oracle.com">bengt.rutisson@oracle.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Hi David,<br>
<br>
Updated webrev:<br>
<a href="http://cr.openjdk.java.net/~brutisso/7173959/webrev.02/" target="_blank">http://cr.openjdk.java.net/~<u></u>brutisso/7173959/webrev.02/</a><br>
<br>
I moved the alignment of "size" back into ReservedSpace::initialize() since we actually store the size in an instance variable (_size), so I think it is a bit risky to just do the align_up in os::reserve_memory_aligned(). We probably want the instance variables _size and _alignment in ReservedSpace to be consistent.<br>
<br>
I added an assert in os::reserve_memory_aligned() to verify that the size is correctly aligned. I also added the assert you suggested to check that alignment is page size aligned.<br>
<br>
Bengt<br>
<br>
On 12/13/12 11:50 AM, David Holmes wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 13/12/2012 8:37 PM, Bengt Rutisson wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Hi again,<br>
<br>
Updated webrev:<br>
<a href="http://cr.openjdk.java.net/~brutisso/7173959/webrev.01/" target="_blank">http://cr.openjdk.java.net/~<u></u>brutisso/7173959/webrev.01/</a><br>
<br>
I removed the comment and the alignment.<br>
<br>
But I did not add an assert just yet.<br>
<br>
At the top of ReservedSpace::initialize() we have this code:<br>
<br>
const size_t granularity = os::vm_allocation_granularity(<u></u>);<br>
assert((size & (granularity - 1)) == 0,<br>
"size not aligned to os::vm_allocation_granularity(<u></u>)");<br>
assert((alignment & (granularity - 1)) == 0,<br>
"alignment not aligned to os::vm_allocation_granularity(<u></u>)");<br>
<br>
<br>
Where os::vm_allocation_granularity(<u></u>) is implemented as page size on all<br>
platforms except Windows. There we look it up from the Windows API. I<br>
assume that is a page size too, but since the Windows code in our patch<br>
does not unmap based on the alignment it should be safe either way.<br>
<br>
Is this assert enough or would you like me to add an assert closer to<br>
the code block were I did the changes?<br>
</blockquote>
<br>
As this is a separate method now I think an assert in the method itself would not go astray.<br>
<br>
David<br>
-----<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks,<br>
Bengt<br>
<br>
<br>
On 12/13/12 11:02 AM, Bengt Rutisson wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Hi David,<br>
<br>
Thanks for looking at this!<br>
<br>
On 12/13/12 8:33 AM, David Holmes wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Bengt,<br>
<br>
This has to be one of the absolute best review requests I've ever<br>
read :) Thank you.<br>
</blockquote>
<br>
Wow! Thanks! :)<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Just a couple of queries.<br>
<br>
os_posix.cpp:<br>
<br>
Love the diagram :)<br>
</blockquote>
<br>
It was Mikael's way of making sure we got it right.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm assuming that "alignment" has to be >= the underlying page size,<br>
and in fact must be a multiple of the underlying page size ? (As I<br>
assume you can only unmap whole numbers of pages). If so does that<br>
need to be checked somewhere?<br>
</blockquote>
<br>
Good point. I'll add an assert to check that.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In virtualSpace.cpp:<br>
<br>
// Reserve size large enough to do manual alignment and<br>
// increase size to a multiple of the desired alignment<br>
size = align_size_up(size, alignment);<br>
! base = os::reserve_memory_aligned(<u></u>size, alignment);<br>
<br>
The comment doesn't seem necessary now, and the align_size_up seems<br>
redundant.<br>
</blockquote>
<br>
You are right. I'll remove the comment and the alignment.<br>
<br>
Thanks,<br>
Bengt<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks,<br>
David<br>
<br>
On 13/12/2012 4:42 PM, Bengt Rutisson wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Hi all,<br>
<br>
Could I have a couple of reviews for this change?<br>
<br>
<a href="http://cr.openjdk.java.net/~brutisso/7173959/webrev.00/" target="_blank">http://cr.openjdk.java.net/~<u></u>brutisso/7173959/webrev.00/</a><br>
<br>
This is for a bug originally reported by the Coherence team:<br>
<br>
7173959 : Jvm crashed during coherence exabus (tmb) testing<br>
<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7173959" target="_blank">http://bugs.sun.com/<u></u>bugdatabase/view_bug.do?bug_<u></u>id=7173959</a><br>
<br>
The original analysis and proposed fix was done by Mikael Gerdin and me<br>
together. I'll handle the webrev and push since Mikael is on vacation<br>
starting today. But Mikael did a great job tracking down this very<br>
difficult bug, so he should have a large part of the credit for this<br>
bug<br>
fix,<br>
<br>
Description from the CR:<br>
<br>
The reason that we crash is due to how we re-map memory when we want to<br>
align it for large pages in ReservedSpace::initialize().<br>
<br>
Here is what happens:<br>
<br>
The reservation of memory is split up to a few steps. When we want a<br>
chunk of memory with large pages we first just reserve some memory<br>
large<br>
enough for what we need. Then we realize that we want large pages,<br>
so we<br>
want to re-map the reserved memory to use large pages. But since this<br>
requires that we have a large-page-aligned memory chunk we may have to<br>
fix the recently reserved memory chunk up a bit.<br>
<br>
We do this in ReservedSpace::initialize() by first releasing the memory<br>
we just reserved. Then requesting more memory than we actually need to<br>
make sure that we have enough space to do manual large page alignment.<br>
After we have gotten this memory we figure out a nicely aligned start<br>
address. We then release the memory again and then reserve just enough<br>
memory but using the aligned start address as a fixed address to make<br>
sure that we get the memory we wanted in an aligned way.<br>
<br>
This is done in a loop to make sure that we eventually get some memory.<br>
The interesting code looks like this:<br>
<br>
do {<br>
char* extra_base = os::reserve_memory(extra_size, NULL, alignment);<br>
if (extra_base == NULL) return;<br>
// Do manual alignement<br>
base = (char*) align_size_up((uintptr_t) extra_base, alignment);<br>
assert(base >= extra_base, "just checking");<br>
// Re-reserve the region at the aligned base address.<br>
os::release_memory(extra_base, extra_size); // (1)<br>
base = os::reserve_memory(size, base); // (2)<br>
} while (base == NULL);<br>
<br>
<br>
There is a race here between releasing the memory in (1) and<br>
re-reserving it in (2). But the loop is supposed to handle this race.<br>
<br>
The problem is that on posix platforms you can remap the same memory<br>
area several times. The call in (2) will use mmap with MAP_FIXED. This<br>
means that the OS will think that you know exactly what you are doing.<br>
So, if part of the memory has been mapped already by the process it<br>
will<br>
just go ahead and reuse that memory.<br>
<br>
This means that if we are having multiple threads that do mmap. We can<br>
end up with a situation where we release our mapping in (1). Then<br>
another thread comes in and maps part of the memory that we used to<br>
have. Then we remap over that memory again in (2) with MAP_FIXED.<br>
Now we<br>
have a situation where two threads in our process have mapped the same<br>
memory. If both threads try to use it or if one of the threads unmap<br>
part or all of the memory we will crash.<br>
<br>
On posix it is possible to unmap any part of a mapped chunk. So, our<br>
proposed solution to the race described above is to not unmap all<br>
memory<br>
in (1) but rather just unmap the section at the start and at the end of<br>
the chunk that we mapped to get alignment. This also removes the need<br>
for the loop.<br>
<br>
However, on Windows you can only unmap _all_ of the memory that you<br>
have<br>
mapped. On the other hand Windows also will not allow you to map over<br>
other mappings, so the original code is actually safe. If we keep<br>
the loop.<br>
<br>
So, our solution is to treat this differently on Windows and posix<br>
platforms.<br>
<br>
<br>
Thanks,<br>
Bengt<br>
</blockquote></blockquote>
<br>
</blockquote>
<br>
</blockquote></blockquote>
<br>
</blockquote></div>