Skip to content

Create an adequately sized J9Heap to store the UpcallThunk #20926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions runtime/vm/UpcallThunkMem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "j9.h"
#include "j9protos.h"
#include "OMR/Bytes.hpp"
#include "ut_j9vm.h"
#include "vm_internal.h"

Expand Down Expand Up @@ -175,6 +176,7 @@ allocateThunkHeap(J9UpcallMetaData *data)
J9HashTable *metaDataHashTable = NULL;
void *allocMemPtr = NULL;
J9Heap *thunkHeap = NULL;
uintptr_t heapSize = pageSize;
J9PortVmemIdentifier vmemID;

Trc_VM_allocateThunkHeap_Entry(thunkSize);
Expand All @@ -193,27 +195,35 @@ allocateThunkHeap(J9UpcallMetaData *data)
goto freeAllMemoryThenExit;
}

if (thunkSize > heapSize) {
/* If page size is insufficient to store the thunk, create a heap that is adequately sized
* and aligned to the page size to store the thunk.
*/
heapSize = OMR::align((size_t)thunkSize, (size_t)pageSize);
}

/* Reserve a block of memory with the fixed page size for heap creation. */
allocMemPtr = j9vmem_reserve_memory(NULL, pageSize, &vmemID,
J9PORT_VMEM_MEMORY_MODE_READ | J9PORT_VMEM_MEMORY_MODE_WRITE | J9PORT_VMEM_MEMORY_MODE_EXECUTE | J9PORT_VMEM_MEMORY_MODE_COMMIT,
pageSize, J9MEM_CATEGORY_VM_FFI);
allocMemPtr = j9vmem_reserve_memory(
NULL, heapSize, &vmemID,
J9PORT_VMEM_MEMORY_MODE_READ | J9PORT_VMEM_MEMORY_MODE_WRITE | J9PORT_VMEM_MEMORY_MODE_EXECUTE | J9PORT_VMEM_MEMORY_MODE_COMMIT,
pageSize, J9MEM_CATEGORY_VM_FFI);
if (NULL == allocMemPtr) {
Trc_VM_allocateThunkHeap_reserve_memory_failed(pageSize);
Trc_VM_allocateThunkHeap_reserve_memory_failed(heapSize);
goto freeAllMemoryThenExit;
}

omrthread_jit_write_protect_disable();

/* Initialize the allocated memory as a J9Heap. */
thunkHeap = j9heap_create(allocMemPtr, pageSize, 0);
thunkHeap = j9heap_create(allocMemPtr, heapSize, 0);
if (NULL == thunkHeap) {
Trc_VM_allocateThunkHeap_create_heap_failed(allocMemPtr, pageSize);
Trc_VM_allocateThunkHeap_create_heap_failed(allocMemPtr, heapSize);
goto freeAllMemoryThenExit;
}

/* Store the heap handle in J9UpcallThunkHeapWrapper if the thunk heap is successfully created. */
thunkHeapWrapper->heap = thunkHeap;
thunkHeapWrapper->heapSize = pageSize;
thunkHeapWrapper->heapSize = heapSize;
thunkHeapWrapper->vmemID = vmemID;
thunkHeapNode->thunkHeapWrapper = thunkHeapWrapper;

Expand Down Expand Up @@ -258,7 +268,7 @@ allocateThunkHeap(J9UpcallMetaData *data)
}
if (NULL != allocMemPtr) {
omrthread_jit_write_protect_enable();
j9vmem_free_memory(allocMemPtr, pageSize, &vmemID);
j9vmem_free_memory(allocMemPtr, heapSize, &vmemID);
allocMemPtr = NULL;
}
goto done;
Expand Down
4 changes: 2 additions & 2 deletions runtime/vm/j9vm.tdf
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ TraceExit=Trc_VM_subAllocateThunkFromHeap_Exit NoEnv Overhead=1 Level=3 Template
TraceEntry=Trc_VM_allocateThunkHeap_Entry NoEnv Overhead=1 Level=3 Template="Enter allocateThunkHeap - The requested thunk size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_allocate_thunk_heap_wrapper_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate memory for the thunk heap wrapper"
TraceEvent=Trc_VM_allocateThunkHeap_create_metadata_hash_table_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to create hashtable for the generated thunk and the corresponding metadata in upcall"
TraceEvent=Trc_VM_allocateThunkHeap_reserve_memory_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate reserve the memory with the requested page size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_create_heap_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to create a heap from the reserved memory (%p) with the requested page size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_reserve_memory_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate reserve the memory with the requested size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_create_heap_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to create a heap from the reserved memory (%p) with the requested size(%zu)"
TraceExit=Trc_VM_allocateThunkHeap_Exit NoEnv Overhead=1 Level=3 Template="Exit allocateThunkHeap - The suballocated thunk heap is %p"

TraceEvent=Trc_VM_allocateThunkHeap_allocate_thunk_heap_node_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate memory for the thunk heap node"
Expand Down